Improve debug output for MemOperandSDNode. PseudoSourceValue nodes
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGPrinter.cpp
1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ScheduleDAG.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Support/GraphWriter.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Config/config.h"
27 #include <fstream>
28 #include <sstream>
29 using namespace llvm;
30
31 namespace llvm {
32   template<>
33   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
34     static std::string getGraphName(const SelectionDAG *G) {
35       return G->getMachineFunction().getFunction()->getName();
36     }
37
38     static bool renderGraphFromBottomUp() {
39       return true;
40     }
41     
42     static bool hasNodeAddressLabel(const SDNode *Node,
43                                     const SelectionDAG *Graph) {
44       return true;
45     }
46     
47     /// If you want to override the dot attributes printed for a particular
48     /// edge, override this method.
49     template<typename EdgeIter>
50     static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
51       SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
52       MVT VT = Op.getValueType();
53       if (VT == MVT::Flag)
54         return "color=red,style=bold";
55       else if (VT == MVT::Other)
56         return "color=blue,style=dashed";
57       return "";
58     }
59     
60
61     static std::string getNodeLabel(const SDNode *Node,
62                                     const SelectionDAG *Graph);
63     static std::string getNodeAttributes(const SDNode *N,
64                                          const SelectionDAG *Graph) {
65 #ifndef NDEBUG
66       const std::string &Attrs = Graph->getGraphAttrs(N);
67       if (!Attrs.empty()) {
68         if (Attrs.find("shape=") == std::string::npos)
69           return std::string("shape=Mrecord,") + Attrs;
70         else
71           return Attrs;
72       }
73 #endif
74       return "shape=Mrecord";
75     }
76
77     static void addCustomGraphFeatures(SelectionDAG *G,
78                                        GraphWriter<SelectionDAG*> &GW) {
79       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
80       if (G->getRoot().Val)
81         GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
82     }
83   };
84 }
85
86 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
87                                                         const SelectionDAG *G) {
88   std::string Op = Node->getOperationName(G);
89
90   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
91     if (Node->getValueType(i) == MVT::Other)
92       Op += ":ch";
93     else
94       Op = Op + ":" + Node->getValueType(i).getMVTString();
95     
96   if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
97     Op += ": " + utostr(CSDN->getValue());
98   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
99     Op += ": " + ftostr(CSDN->getValueAPF());
100   } else if (const GlobalAddressSDNode *GADN =
101              dyn_cast<GlobalAddressSDNode>(Node)) {
102     int offset = GADN->getOffset();
103     Op += ": " + GADN->getGlobal()->getName();
104     if (offset > 0)
105       Op += "+" + itostr(offset);
106     else
107       Op += itostr(offset);
108   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
109     Op += " " + itostr(FIDN->getIndex());
110   } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
111     Op += " " + itostr(JTDN->getIndex());
112   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
113     if (CP->isMachineConstantPoolEntry()) {
114       std::ostringstream SS;
115       CP->getMachineCPVal()->print(SS);
116       Op += "<" + SS.str() + ">";
117     } else {
118       if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
119         Op += "<" + ftostr(CFP->getValueAPF()) + ">";
120       else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
121         Op += "<" + utostr(CI->getZExtValue()) + ">";
122       else {
123         std::ostringstream SS;
124         WriteAsOperand(SS, CP->getConstVal(), false);
125         Op += "<" + SS.str() + ">";
126       }
127     }
128   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
129     Op = "BB: ";
130     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
131     if (LBB)
132       Op += LBB->getName();
133     //Op += " " + (const void*)BBDN->getBasicBlock();
134   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
135     if (G && R->getReg() != 0 &&
136         TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
137       Op = Op + " " +
138         G->getTarget().getRegisterInfo()->getName(R->getReg());
139     } else {
140       Op += " #" + utostr(R->getReg());
141     }
142   } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
143     Op += ": " + D->getCompileUnit()->getFileName();
144     Op += ":" + utostr(D->getLine());
145     if (D->getColumn() != 0)
146       Op += ":" + utostr(D->getColumn());
147   } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
148     Op += ": LabelID=" + utostr(L->getLabelID());
149   } else if (const ExternalSymbolSDNode *ES =
150              dyn_cast<ExternalSymbolSDNode>(Node)) {
151     Op += "'" + std::string(ES->getSymbol()) + "'";
152   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
153     if (M->getValue())
154       Op += "<" + M->getValue()->getName() + ">";
155     else
156       Op += "<null>";
157   } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
158     if (M->MO.getValue()) {
159       std::ostringstream SS;
160       M->MO.getValue()->print(SS);
161       Op += "<" + SS.str() + "+" + itostr(M->MO.getOffset()) + ">";
162     } else {
163       Op += "<(unknown)+" + itostr(M->MO.getOffset()) + ">";
164     }
165   } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
166     Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
167   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
168     Op = Op + " VT=" + N->getVT().getMVTString();
169   } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
170     bool doExt = true;
171     switch (LD->getExtensionType()) {
172     default: doExt = false; break;
173     case ISD::EXTLOAD:
174       Op = Op + "<anyext ";
175       break;
176     case ISD::SEXTLOAD:
177       Op = Op + " <sext ";
178       break;
179     case ISD::ZEXTLOAD:
180       Op = Op + " <zext ";
181       break;
182     }
183     if (doExt)
184       Op += LD->getMemoryVT().getMVTString() + ">";
185     if (LD->isVolatile())
186       Op += "<V>";
187     Op += LD->getIndexedModeName(LD->getAddressingMode());
188     if (LD->getAlignment() > 1)
189       Op += " A=" + utostr(LD->getAlignment());
190   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
191     if (ST->isTruncatingStore())
192       Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
193     if (ST->isVolatile())
194       Op += "<V>";
195     Op += ST->getIndexedModeName(ST->getAddressingMode());
196     if (ST->getAlignment() > 1)
197       Op += " A=" + utostr(ST->getAlignment());
198   }
199
200 #if 0
201   Op += " Id=" + itostr(Node->getNodeId());
202 #endif
203   
204   return Op;
205 }
206
207
208 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
209 /// rendered using 'dot'.
210 ///
211 void SelectionDAG::viewGraph() {
212 // This code is only for debugging!
213 #ifndef NDEBUG
214   ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
215 #else
216   cerr << "SelectionDAG::viewGraph is only available in debug builds on "
217        << "systems with Graphviz or gv!\n";
218 #endif  // NDEBUG
219 }
220
221
222 /// clearGraphAttrs - Clear all previously defined node graph attributes.
223 /// Intended to be used from a debugging tool (eg. gdb).
224 void SelectionDAG::clearGraphAttrs() {
225 #ifndef NDEBUG
226   NodeGraphAttrs.clear();
227 #else
228   cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
229        << " on systems with Graphviz or gv!\n";
230 #endif
231 }
232
233
234 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
235 ///
236 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
237 #ifndef NDEBUG
238   NodeGraphAttrs[N] = Attrs;
239 #else
240   cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
241        << " on systems with Graphviz or gv!\n";
242 #endif
243 }
244
245
246 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
247 /// Used from getNodeAttributes.
248 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
249 #ifndef NDEBUG
250   std::map<const SDNode *, std::string>::const_iterator I =
251     NodeGraphAttrs.find(N);
252     
253   if (I != NodeGraphAttrs.end())
254     return I->second;
255   else
256     return "";
257 #else
258   cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
259        << " on systems with Graphviz or gv!\n";
260   return std::string("");
261 #endif
262 }
263
264 /// setGraphColor - Convenience for setting node color attribute.
265 ///
266 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
267 #ifndef NDEBUG
268   NodeGraphAttrs[N] = std::string("color=") + Color;
269 #else
270   cerr << "SelectionDAG::setGraphColor is only available in debug builds"
271        << " on systems with Graphviz or gv!\n";
272 #endif
273 }
274
275 namespace llvm {
276   template<>
277   struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
278     static std::string getGraphName(const ScheduleDAG *G) {
279       return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
280     }
281
282     static bool renderGraphFromBottomUp() {
283       return true;
284     }
285     
286     static bool hasNodeAddressLabel(const SUnit *Node,
287                                     const ScheduleDAG *Graph) {
288       return true;
289     }
290     
291     /// If you want to override the dot attributes printed for a particular
292     /// edge, override this method.
293     template<typename EdgeIter>
294     static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
295       if (EI.isSpecialDep())
296         return "color=cyan,style=dashed";
297       if (EI.isCtrlDep())
298         return "color=blue,style=dashed";
299       return "";
300     }
301     
302
303     static std::string getNodeLabel(const SUnit *Node,
304                                     const ScheduleDAG *Graph);
305     static std::string getNodeAttributes(const SUnit *N,
306                                          const ScheduleDAG *Graph) {
307       return "shape=Mrecord";
308     }
309
310     static void addCustomGraphFeatures(ScheduleDAG *G,
311                                        GraphWriter<ScheduleDAG*> &GW) {
312       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
313       const SDNode *N = G->DAG.getRoot().Val;
314       if (N && N->getNodeId() != -1)
315         GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, "");
316     }
317   };
318 }
319
320 std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
321                                                        const ScheduleDAG *G) {
322   std::string Op;
323
324   for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
325     Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
326                                                       &G->DAG) + "\n";
327   }
328
329   if (SU->Node)
330     Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
331   else
332     Op += "<CROSS RC COPY>";
333
334   return Op;
335 }
336
337
338 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
339 /// rendered using 'dot'.
340 ///
341 void ScheduleDAG::viewGraph() {
342 // This code is only for debugging!
343 #ifndef NDEBUG
344   ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
345 #else
346   cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
347        << "systems with Graphviz or gv!\n";
348 #endif  // NDEBUG
349 }