2005-01-10 23:08:40 +00:00
|
|
|
//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2005-01-10 23:08:40 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2005-01-10 23:08:40 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements the SelectionDAG::viewGraph method.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-08-26 17:15:30 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
2006-03-05 09:38:03 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2005-01-10 23:08:40 +00:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
2007-08-28 20:32:58 +00:00
|
|
|
#include "llvm/CodeGen/ScheduleDAG.h"
|
2006-09-12 21:00:35 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2005-01-10 23:08:40 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2005-08-19 21:21:16 +00:00
|
|
|
#include "llvm/Target/MRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2005-01-10 23:08:40 +00:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2005-01-11 00:34:33 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2005-07-15 22:48:31 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2005-01-10 23:08:40 +00:00
|
|
|
#include <fstream>
|
2006-03-05 09:38:03 +00:00
|
|
|
#include <sstream>
|
2005-01-10 23:08:40 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2005-01-10 23:26:00 +00:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const SelectionDAG *G) {
|
|
|
|
return G->getMachineFunction().getFunction()->getName();
|
|
|
|
}
|
2005-01-11 00:34:33 +00:00
|
|
|
|
|
|
|
static bool renderGraphFromBottomUp() {
|
|
|
|
return true;
|
2005-01-10 23:26:00 +00:00
|
|
|
}
|
2005-10-01 00:17:07 +00:00
|
|
|
|
|
|
|
static bool hasNodeAddressLabel(const SDNode *Node,
|
|
|
|
const SelectionDAG *Graph) {
|
|
|
|
return true;
|
|
|
|
}
|
2006-10-20 18:06:09 +00:00
|
|
|
|
|
|
|
/// If you want to override the dot attributes printed for a particular
|
|
|
|
/// edge, override this method.
|
|
|
|
template<typename EdgeIter>
|
|
|
|
static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
|
|
|
|
SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
|
|
|
|
MVT::ValueType VT = Op.getValueType();
|
|
|
|
if (VT == MVT::Flag)
|
|
|
|
return "color=red,style=bold";
|
|
|
|
else if (VT == MVT::Other)
|
2007-06-18 15:30:16 +00:00
|
|
|
return "color=blue,style=dashed";
|
2006-10-20 18:06:09 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2005-01-10 23:26:00 +00:00
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
static std::string getNodeLabel(const SDNode *Node,
|
|
|
|
const SelectionDAG *Graph);
|
2006-10-02 12:26:53 +00:00
|
|
|
static std::string getNodeAttributes(const SDNode *N,
|
|
|
|
const SelectionDAG *Graph) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
const std::string &Attrs = Graph->getGraphAttrs(N);
|
|
|
|
if (!Attrs.empty()) {
|
|
|
|
if (Attrs.find("shape=") == std::string::npos)
|
|
|
|
return std::string("shape=Mrecord,") + Attrs;
|
|
|
|
else
|
|
|
|
return Attrs;
|
|
|
|
}
|
|
|
|
#endif
|
2005-01-10 23:26:00 +00:00
|
|
|
return "shape=Mrecord";
|
|
|
|
}
|
2005-01-10 23:52:04 +00:00
|
|
|
|
|
|
|
static void addCustomGraphFeatures(SelectionDAG *G,
|
|
|
|
GraphWriter<SelectionDAG*> &GW) {
|
|
|
|
GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
|
2006-10-17 19:33:52 +00:00
|
|
|
if (G->getRoot().Val)
|
|
|
|
GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
|
2005-01-10 23:52:04 +00:00
|
|
|
}
|
2005-01-10 23:26:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
|
|
|
|
const SelectionDAG *G) {
|
2005-08-16 18:31:23 +00:00
|
|
|
std::string Op = Node->getOperationName(G);
|
2005-01-11 22:21:04 +00:00
|
|
|
|
2005-08-16 18:31:23 +00:00
|
|
|
for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
|
|
|
|
if (Node->getValueType(i) == MVT::Other)
|
|
|
|
Op += ":ch";
|
|
|
|
else
|
|
|
|
Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
|
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
|
|
|
|
Op += ": " + utostr(CSDN->getValue());
|
|
|
|
} else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
|
2007-08-31 04:03:46 +00:00
|
|
|
Op += ": " + ftostr(CSDN->getValueAPF());
|
2005-04-21 22:36:52 +00:00
|
|
|
} else if (const GlobalAddressSDNode *GADN =
|
2005-01-11 00:34:33 +00:00
|
|
|
dyn_cast<GlobalAddressSDNode>(Node)) {
|
2005-11-30 02:04:11 +00:00
|
|
|
int offset = GADN->getOffset();
|
2005-01-11 00:34:33 +00:00
|
|
|
Op += ": " + GADN->getGlobal()->getName();
|
2005-11-30 02:04:11 +00:00
|
|
|
if (offset > 0)
|
|
|
|
Op += "+" + itostr(offset);
|
|
|
|
else
|
|
|
|
Op += itostr(offset);
|
2005-04-22 04:01:18 +00:00
|
|
|
} else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
|
2005-01-11 00:34:33 +00:00
|
|
|
Op += " " + itostr(FIDN->getIndex());
|
2006-11-01 04:48:30 +00:00
|
|
|
} else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
|
|
|
|
Op += " " + itostr(JTDN->getIndex());
|
2005-01-11 00:34:33 +00:00
|
|
|
} else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
|
2006-09-12 21:00:35 +00:00
|
|
|
if (CP->isMachineConstantPoolEntry()) {
|
2006-03-05 09:38:03 +00:00
|
|
|
std::ostringstream SS;
|
2006-09-12 21:00:35 +00:00
|
|
|
CP->getMachineCPVal()->print(SS);
|
2006-03-05 09:38:03 +00:00
|
|
|
Op += "<" + SS.str() + ">";
|
2006-09-12 21:00:35 +00:00
|
|
|
} else {
|
|
|
|
if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
|
2007-08-31 04:03:46 +00:00
|
|
|
Op += "<" + ftostr(CFP->getValueAPF()) + ">";
|
2006-09-12 21:00:35 +00:00
|
|
|
else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
|
|
|
|
Op += "<" + utostr(CI->getZExtValue()) + ">";
|
|
|
|
else {
|
|
|
|
std::ostringstream SS;
|
|
|
|
WriteAsOperand(SS, CP->getConstVal(), false);
|
|
|
|
Op += "<" + SS.str() + ">";
|
|
|
|
}
|
2006-03-05 09:38:03 +00:00
|
|
|
}
|
2005-04-22 04:01:18 +00:00
|
|
|
} else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
|
2005-01-11 00:34:33 +00:00
|
|
|
Op = "BB: ";
|
|
|
|
const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
|
|
|
|
if (LBB)
|
|
|
|
Op += LBB->getName();
|
|
|
|
//Op += " " + (const void*)BBDN->getBasicBlock();
|
2005-08-16 21:55:35 +00:00
|
|
|
} else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
|
2006-10-20 18:06:09 +00:00
|
|
|
if (G && R->getReg() != 0 &&
|
|
|
|
MRegisterInfo::isPhysicalRegister(R->getReg())) {
|
2005-08-19 21:21:16 +00:00
|
|
|
Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
|
|
|
|
} else {
|
|
|
|
Op += " #" + utostr(R->getReg());
|
|
|
|
}
|
2005-01-11 00:34:33 +00:00
|
|
|
} else if (const ExternalSymbolSDNode *ES =
|
|
|
|
dyn_cast<ExternalSymbolSDNode>(Node)) {
|
|
|
|
Op += "'" + std::string(ES->getSymbol()) + "'";
|
2005-05-09 04:08:27 +00:00
|
|
|
} else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
|
|
|
|
if (M->getValue())
|
|
|
|
Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
|
|
|
|
else
|
|
|
|
Op += "<null:" + itostr(M->getOffset()) + ">";
|
2005-08-18 03:31:02 +00:00
|
|
|
} else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
|
2007-05-18 17:52:13 +00:00
|
|
|
Op = Op + " VT=" + MVT::getValueTypeString(N->getVT());
|
2005-11-29 06:21:05 +00:00
|
|
|
} else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
|
|
|
|
Op = Op + "\"" + N->getValue() + "\"";
|
2006-10-10 20:11:26 +00:00
|
|
|
} else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
|
|
|
|
bool doExt = true;
|
|
|
|
switch (LD->getExtensionType()) {
|
|
|
|
default: doExt = false; break;
|
|
|
|
case ISD::EXTLOAD:
|
|
|
|
Op = Op + "<anyext ";
|
|
|
|
break;
|
|
|
|
case ISD::SEXTLOAD:
|
|
|
|
Op = Op + " <sext ";
|
|
|
|
break;
|
|
|
|
case ISD::ZEXTLOAD:
|
|
|
|
Op = Op + " <zext ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (doExt)
|
2006-10-11 07:10:22 +00:00
|
|
|
Op = Op + MVT::getValueTypeString(LD->getLoadedVT()) + ">";
|
2006-10-10 20:11:26 +00:00
|
|
|
|
2006-11-09 18:44:21 +00:00
|
|
|
Op += LD->getIndexedModeName(LD->getAddressingMode());
|
2006-10-17 21:18:26 +00:00
|
|
|
} else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
|
|
|
|
if (ST->isTruncatingStore())
|
|
|
|
Op = Op + "<trunc " + MVT::getValueTypeString(ST->getStoredVT()) + ">";
|
2006-11-09 18:44:21 +00:00
|
|
|
Op += ST->getIndexedModeName(ST->getAddressingMode());
|
2005-01-11 00:34:33 +00:00
|
|
|
}
|
2007-10-15 05:32:43 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
Op += " Id=" + itostr(Node->getNodeId());
|
|
|
|
#endif
|
2005-11-29 06:21:05 +00:00
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
return Op;
|
|
|
|
}
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
|
2005-01-10 23:08:40 +00:00
|
|
|
/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
|
|
|
|
/// rendered using 'dot'.
|
|
|
|
///
|
|
|
|
void SelectionDAG::viewGraph() {
|
2005-07-14 05:17:43 +00:00
|
|
|
// This code is only for debugging!
|
2005-07-14 05:33:13 +00:00
|
|
|
#ifndef NDEBUG
|
2006-06-27 16:49:46 +00:00
|
|
|
ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
|
|
|
|
#else
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2006-06-27 16:49:46 +00:00
|
|
|
#endif // NDEBUG
|
2005-01-10 23:08:40 +00:00
|
|
|
}
|
2006-10-02 12:26:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// clearGraphAttrs - Clear all previously defined node graph attributes.
|
|
|
|
/// Intended to be used from a debugging tool (eg. gdb).
|
|
|
|
void SelectionDAG::clearGraphAttrs() {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
NodeGraphAttrs.clear();
|
|
|
|
#else
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
|
|
|
|
<< " on systems with Graphviz or gv!\n";
|
2006-10-02 12:26:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
|
|
|
|
///
|
|
|
|
void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
NodeGraphAttrs[N] = Attrs;
|
|
|
|
#else
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
|
|
|
|
<< " on systems with Graphviz or gv!\n";
|
2006-10-02 12:26:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
|
|
|
|
/// Used from getNodeAttributes.
|
|
|
|
const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::map<const SDNode *, std::string>::const_iterator I =
|
|
|
|
NodeGraphAttrs.find(N);
|
|
|
|
|
|
|
|
if (I != NodeGraphAttrs.end())
|
|
|
|
return I->second;
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
#else
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
|
|
|
|
<< " on systems with Graphviz or gv!\n";
|
2006-10-02 12:26:53 +00:00
|
|
|
return std::string("");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/// setGraphColor - Convenience for setting node color attribute.
|
|
|
|
///
|
|
|
|
void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
NodeGraphAttrs[N] = std::string("color=") + Color;
|
|
|
|
#else
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "SelectionDAG::setGraphColor is only available in debug builds"
|
|
|
|
<< " on systems with Graphviz or gv!\n";
|
2006-10-02 12:26:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-28 20:32:58 +00:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const ScheduleDAG *G) {
|
|
|
|
return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool renderGraphFromBottomUp() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool hasNodeAddressLabel(const SUnit *Node,
|
|
|
|
const ScheduleDAG *Graph) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If you want to override the dot attributes printed for a particular
|
|
|
|
/// edge, override this method.
|
|
|
|
template<typename EdgeIter>
|
|
|
|
static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
|
2007-09-19 01:38:40 +00:00
|
|
|
if (EI.isCtrlDep())
|
2007-08-28 20:32:58 +00:00
|
|
|
return "color=blue,style=dashed";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::string getNodeLabel(const SUnit *Node,
|
|
|
|
const ScheduleDAG *Graph);
|
|
|
|
static std::string getNodeAttributes(const SUnit *N,
|
|
|
|
const ScheduleDAG *Graph) {
|
|
|
|
return "shape=Mrecord";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addCustomGraphFeatures(ScheduleDAG *G,
|
|
|
|
GraphWriter<ScheduleDAG*> &GW) {
|
|
|
|
GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
|
|
|
|
if (G->DAG.getRoot().Val)
|
2007-09-25 01:54:36 +00:00
|
|
|
GW.emitEdge(0, -1, G->SUnitMap[G->DAG.getRoot().Val].front(), -1, "");
|
2007-08-28 20:32:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
|
|
|
|
const ScheduleDAG *G) {
|
|
|
|
std::string Op;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
|
|
|
|
Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
|
|
|
|
&G->DAG) + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
|
|
|
|
|
|
|
|
return Op;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
|
|
|
|
/// rendered using 'dot'.
|
|
|
|
///
|
|
|
|
void ScheduleDAG::viewGraph() {
|
|
|
|
// This code is only for debugging!
|
|
|
|
#ifndef NDEBUG
|
|
|
|
ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
|
|
|
|
#else
|
|
|
|
cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
|
|
|
#endif // NDEBUG
|
|
|
|
}
|