Add ability to annotate (color) nodes in a viewGraph.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGPrinter.cpp
1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the SelectionDAG::viewGraph method.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
25 #include <fstream>
26 #include <sstream>
27 using namespace llvm;
28
29 namespace llvm {
30 #ifndef NDEBUG
31   std::map<const SDNode *, std::string> DagNodeColor;
32 #endif
33   template<>
34   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
35     static std::string getGraphName(const SelectionDAG *G) {
36       return G->getMachineFunction().getFunction()->getName();
37     }
38
39     static bool renderGraphFromBottomUp() {
40       return true;
41     }
42     
43     static bool hasNodeAddressLabel(const SDNode *Node,
44                                     const SelectionDAG *Graph) {
45       return true;
46     }
47
48     static std::string getNodeLabel(const SDNode *Node,
49                                     const SelectionDAG *Graph);
50     static std::string getNodeAttributes(const SDNode *N,
51                                          const SelectionDAG *Graph) {
52 #ifndef NDEBUG
53       const std::string &Attrs = Graph->getGraphAttrs(N);
54       if (!Attrs.empty()) {
55         if (Attrs.find("shape=") == std::string::npos)
56           return std::string("shape=Mrecord,") + Attrs;
57         else
58           return Attrs;
59       }
60 #endif
61       return "shape=Mrecord";
62     }
63
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, "");
68     }
69   };
70 }
71
72 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
73                                                         const SelectionDAG *G) {
74   std::string Op = Node->getOperationName(G);
75
76   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
77     if (Node->getValueType(i) == MVT::Other)
78       Op += ":ch";
79     else
80       Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
81     
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();
90     if (offset > 0)
91       Op += "+" + itostr(offset);
92     else
93       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() + ">";
101     } else {
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()) + ">";
106       else {
107         std::ostringstream SS;
108         WriteAsOperand(SS, CP->getConstVal(), false);
109         Op += "<" + SS.str() + ">";
110       }
111     }
112   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
113     Op = "BB: ";
114     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
115     if (LBB)
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());
121     } else {
122       Op += " #" + utostr(R->getReg());
123     }
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)) {
128     if (M->getValue())
129       Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
130     else
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() + "\"";
136   }
137   
138   return Op;
139 }
140
141
142 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
143 /// rendered using 'dot'.
144 ///
145 void SelectionDAG::viewGraph() {
146 // This code is only for debugging!
147 #ifndef NDEBUG
148   ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
149 #else
150   std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
151             << "systems with Graphviz or gv!\n";
152 #endif  // NDEBUG
153 }
154
155
156 /// clearGraphAttrs - Clear all previously defined node graph attributes.
157 /// Intended to be used from a debugging tool (eg. gdb).
158 void SelectionDAG::clearGraphAttrs() {
159 #ifndef NDEBUG
160   NodeGraphAttrs.clear();
161 #else
162   std::cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
163             << " on systems with Graphviz or gv!\n";
164 #endif
165 }
166
167
168 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
169 ///
170 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
171 #ifndef NDEBUG
172   NodeGraphAttrs[N] = Attrs;
173 #else
174   std::cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
175             << " on systems with Graphviz or gv!\n";
176 #endif
177 }
178
179
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 {
183 #ifndef NDEBUG
184   std::map<const SDNode *, std::string>::const_iterator I =
185     NodeGraphAttrs.find(N);
186     
187   if (I != NodeGraphAttrs.end())
188     return I->second;
189   else
190     return "";
191 #else
192   std::cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
193             << " on systems with Graphviz or gv!\n";
194   return std::string("");
195 #endif
196 }
197
198 /// setGraphColor - Convenience for setting node color attribute.
199 ///
200 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
201 #ifndef NDEBUG
202   NodeGraphAttrs[N] = std::string("color=") + Color;
203 #else
204   std::cerr << "SelectionDAG::setGraphColor is only available in debug builds"
205             << " on systems with Graphviz or gv!\n";
206 #endif
207 }
208