1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG::viewGraph method.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Constants.h"
15 #include "llvm/Function.h"
16 #include "llvm/Assembly/Writer.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
18 #include "llvm/CodeGen/MachineConstantPool.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Support/GraphWriter.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Config/config.h"
31 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
32 static std::string getGraphName(const SelectionDAG *G) {
33 return G->getMachineFunction().getFunction()->getName();
36 static bool renderGraphFromBottomUp() {
40 static bool hasNodeAddressLabel(const SDNode *Node,
41 const SelectionDAG *Graph) {
45 static std::string getNodeLabel(const SDNode *Node,
46 const SelectionDAG *Graph);
47 static std::string getNodeAttributes(const SDNode *N) {
48 return "shape=Mrecord";
51 static void addCustomGraphFeatures(SelectionDAG *G,
52 GraphWriter<SelectionDAG*> &GW) {
53 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
54 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
59 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
60 const SelectionDAG *G) {
61 std::string Op = Node->getOperationName(G);
63 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
64 if (Node->getValueType(i) == MVT::Other)
67 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
69 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
70 Op += ": " + utostr(CSDN->getValue());
71 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
72 Op += ": " + ftostr(CSDN->getValue());
73 } else if (const GlobalAddressSDNode *GADN =
74 dyn_cast<GlobalAddressSDNode>(Node)) {
75 int offset = GADN->getOffset();
76 Op += ": " + GADN->getGlobal()->getName();
78 Op += "+" + itostr(offset);
81 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
82 Op += " " + itostr(FIDN->getIndex());
83 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
84 if (CP->isMachineConstantPoolEntry()) {
85 std::ostringstream SS;
86 CP->getMachineCPVal()->print(SS);
87 Op += "<" + SS.str() + ">";
89 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
90 Op += "<" + ftostr(CFP->getValue()) + ">";
91 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
92 Op += "<" + utostr(CI->getZExtValue()) + ">";
94 std::ostringstream SS;
95 WriteAsOperand(SS, CP->getConstVal(), false);
96 Op += "<" + SS.str() + ">";
99 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
101 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
103 Op += LBB->getName();
104 //Op += " " + (const void*)BBDN->getBasicBlock();
105 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
106 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
107 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
109 Op += " #" + utostr(R->getReg());
111 } else if (const ExternalSymbolSDNode *ES =
112 dyn_cast<ExternalSymbolSDNode>(Node)) {
113 Op += "'" + std::string(ES->getSymbol()) + "'";
114 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
116 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
118 Op += "<null:" + itostr(M->getOffset()) + ">";
119 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
120 Op = Op + " VT=" + getValueTypeString(N->getVT());
121 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
122 Op = Op + "\"" + N->getValue() + "\"";
129 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
130 /// rendered using 'dot'.
132 void SelectionDAG::viewGraph() {
133 // This code is only for debugging!
135 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
137 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
138 << "systems with Graphviz or gv!\n";