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 std::map<const SDNode *, std::string> DagNodeColor;
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
35 static std::string getGraphName(const SelectionDAG *G) {
36 return G->getMachineFunction().getFunction()->getName();
39 static bool renderGraphFromBottomUp() {
43 static bool hasNodeAddressLabel(const SDNode *Node,
44 const SelectionDAG *Graph) {
48 static std::string getNodeLabel(const SDNode *Node,
49 const SelectionDAG *Graph);
50 static std::string getNodeAttributes(const SDNode *N,
51 const SelectionDAG *Graph) {
53 const std::string &Attrs = Graph->getGraphAttrs(N);
55 if (Attrs.find("shape=") == std::string::npos)
56 return std::string("shape=Mrecord,") + Attrs;
61 return "shape=Mrecord";
64 static void addCustomGraphFeatures(SelectionDAG *G,
65 GraphWriter<SelectionDAG*> &GW) {
66 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
67 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
72 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
73 const SelectionDAG *G) {
74 std::string Op = Node->getOperationName(G);
76 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
77 if (Node->getValueType(i) == MVT::Other)
80 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
82 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
83 Op += ": " + utostr(CSDN->getValue());
84 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
85 Op += ": " + ftostr(CSDN->getValue());
86 } else if (const GlobalAddressSDNode *GADN =
87 dyn_cast<GlobalAddressSDNode>(Node)) {
88 int offset = GADN->getOffset();
89 Op += ": " + GADN->getGlobal()->getName();
91 Op += "+" + itostr(offset);
94 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
95 Op += " " + itostr(FIDN->getIndex());
96 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
97 if (CP->isMachineConstantPoolEntry()) {
98 std::ostringstream SS;
99 CP->getMachineCPVal()->print(SS);
100 Op += "<" + SS.str() + ">";
102 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
103 Op += "<" + ftostr(CFP->getValue()) + ">";
104 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
105 Op += "<" + utostr(CI->getZExtValue()) + ">";
107 std::ostringstream SS;
108 WriteAsOperand(SS, CP->getConstVal(), false);
109 Op += "<" + SS.str() + ">";
112 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
114 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
116 Op += LBB->getName();
117 //Op += " " + (const void*)BBDN->getBasicBlock();
118 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
119 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
120 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
122 Op += " #" + utostr(R->getReg());
124 } else if (const ExternalSymbolSDNode *ES =
125 dyn_cast<ExternalSymbolSDNode>(Node)) {
126 Op += "'" + std::string(ES->getSymbol()) + "'";
127 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
129 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
131 Op += "<null:" + itostr(M->getOffset()) + ">";
132 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
133 Op = Op + " VT=" + getValueTypeString(N->getVT());
134 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
135 Op = Op + "\"" + N->getValue() + "\"";
142 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
143 /// rendered using 'dot'.
145 void SelectionDAG::viewGraph() {
146 // This code is only for debugging!
148 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
150 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
151 << "systems with Graphviz or gv!\n";
156 /// clearGraphAttrs - Clear all previously defined node graph attributes.
157 /// Intended to be used from a debugging tool (eg. gdb).
158 void SelectionDAG::clearGraphAttrs() {
160 NodeGraphAttrs.clear();
162 std::cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
163 << " on systems with Graphviz or gv!\n";
168 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
170 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
172 NodeGraphAttrs[N] = Attrs;
174 std::cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
175 << " on systems with Graphviz or gv!\n";
180 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
181 /// Used from getNodeAttributes.
182 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
184 std::map<const SDNode *, std::string>::const_iterator I =
185 NodeGraphAttrs.find(N);
187 if (I != NodeGraphAttrs.end())
192 std::cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
193 << " on systems with Graphviz or gv!\n";
194 return std::string("");
198 /// setGraphColor - Convenience for setting node color attribute.
200 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
202 NodeGraphAttrs[N] = std::string("color=") + Color;
204 std::cerr << "SelectionDAG::setGraphColor is only available in debug builds"
205 << " on systems with Graphviz or gv!\n";