Fast-isel no longer an experiment.
[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/CodeGen/PseudoSourceValue.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/GraphWriter.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Config/config.h"
29 #include <fstream>
30 using namespace llvm;
31
32 namespace llvm {
33   template<>
34   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
35     static bool hasEdgeDestLabels() {
36       return true;
37     }
38
39     static unsigned numEdgeDestLabels(const void *Node) {
40       return ((const SDNode *) Node)->getNumValues();
41     }
42
43     static std::string getEdgeDestLabel(const void *Node, unsigned i) {
44       return ((const SDNode *) Node)->getValueType(i).getMVTString();
45     }
46
47     /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
48     /// should actually target another edge source, not a node.  If this method is
49     /// implemented, getEdgeTarget should be implemented.
50     template<typename EdgeIter>
51     static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
52       return true;
53     }
54
55     /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
56     /// called to determine which outgoing edge of Node is the target of this
57     /// edge.
58     template<typename EdgeIter>
59     static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
60       SDNode *TargetNode = *I;
61       SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
62       std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
63       return NI;
64     }
65
66     static std::string getGraphName(const SelectionDAG *G) {
67       return G->getMachineFunction().getFunction()->getName();
68     }
69
70     static bool renderGraphFromBottomUp() {
71       return true;
72     }
73     
74     static bool hasNodeAddressLabel(const SDNode *Node,
75                                     const SelectionDAG *Graph) {
76       return true;
77     }
78     
79     /// If you want to override the dot attributes printed for a particular
80     /// edge, override this method.
81     template<typename EdgeIter>
82     static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
83       SDValue Op = EI.getNode()->getOperand(EI.getOperand());
84       MVT VT = Op.getValueType();
85       if (VT == MVT::Flag)
86         return "color=red,style=bold";
87       else if (VT == MVT::Other)
88         return "color=blue,style=dashed";
89       return "";
90     }
91     
92
93     static std::string getNodeLabel(const SDNode *Node,
94                                     const SelectionDAG *Graph);
95     static std::string getNodeAttributes(const SDNode *N,
96                                          const SelectionDAG *Graph) {
97 #ifndef NDEBUG
98       const std::string &Attrs = Graph->getGraphAttrs(N);
99       if (!Attrs.empty()) {
100         if (Attrs.find("shape=") == std::string::npos)
101           return std::string("shape=Mrecord,") + Attrs;
102         else
103           return Attrs;
104       }
105 #endif
106       return "shape=Mrecord";
107     }
108
109     static void addCustomGraphFeatures(SelectionDAG *G,
110                                        GraphWriter<SelectionDAG*> &GW) {
111       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
112       if (G->getRoot().getNode())
113         GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
114                     "color=blue,style=dashed");
115     }
116   };
117 }
118
119 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
120                                                         const SelectionDAG *G) {
121   std::string Op = Node->getOperationName(G);
122
123   if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
124     Op += ": " + utostr(CSDN->getZExtValue());
125   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
126     Op += ": " + ftostr(CSDN->getValueAPF());
127   } else if (const GlobalAddressSDNode *GADN =
128              dyn_cast<GlobalAddressSDNode>(Node)) {
129     Op += ": " + GADN->getGlobal()->getName();
130     if (int64_t Offset = GADN->getOffset()) {
131       if (Offset > 0)
132         Op += "+" + itostr(Offset);
133       else
134         Op += itostr(Offset);
135     }
136   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
137     Op += " " + itostr(FIDN->getIndex());
138   } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
139     Op += " " + itostr(JTDN->getIndex());
140   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
141     if (CP->isMachineConstantPoolEntry()) {
142       Op += '<';
143       {
144         raw_string_ostream OSS(Op);
145         OSS << *CP->getMachineCPVal();
146       }
147       Op += '>';
148     } else {
149       if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
150         Op += "<" + ftostr(CFP->getValueAPF()) + ">";
151       else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
152         Op += "<" + utostr(CI->getZExtValue()) + ">";
153       else {
154         Op += '<';
155         {
156           raw_string_ostream OSS(Op);
157           WriteAsOperand(OSS, CP->getConstVal(), false);
158         }
159         Op += '>';
160       }
161     }
162     Op += " A=" + itostr(1 << CP->getAlignment());
163   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
164     Op = "BB: ";
165     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
166     if (LBB)
167       Op += LBB->getName();
168     //Op += " " + (const void*)BBDN->getBasicBlock();
169   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
170     if (G && R->getReg() != 0 &&
171         TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
172       Op = Op + " " +
173         G->getTarget().getRegisterInfo()->getName(R->getReg());
174     } else {
175       Op += " #" + utostr(R->getReg());
176     }
177   } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
178     Op += ": " + D->getCompileUnit()->getFileName();
179     Op += ":" + utostr(D->getLine());
180     if (D->getColumn() != 0)
181       Op += ":" + utostr(D->getColumn());
182   } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
183     Op += ": LabelID=" + utostr(L->getLabelID());
184   } else if (const CallSDNode *C = dyn_cast<CallSDNode>(Node)) {
185     Op += ": CallingConv=" + utostr(C->getCallingConv());
186     if (C->isVarArg())
187       Op += ", isVarArg";
188     if (C->isTailCall())
189       Op += ", isTailCall";
190   } else if (const ExternalSymbolSDNode *ES =
191              dyn_cast<ExternalSymbolSDNode>(Node)) {
192     Op += "'" + std::string(ES->getSymbol()) + "'";
193   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
194     if (M->getValue())
195       Op += "<" + M->getValue()->getName() + ">";
196     else
197       Op += "<null>";
198   } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
199     const Value *V = M->MO.getValue();
200     Op += '<';
201     if (!V) {
202       Op += "(unknown)";
203     } else if (isa<PseudoSourceValue>(V)) {
204       // PseudoSourceValues don't have names, so use their print method.
205       {
206         raw_string_ostream OSS(Op);
207         OSS << *M->MO.getValue();
208       }
209     } else {
210       Op += V->getName();
211     }
212     Op += '+' + itostr(M->MO.getOffset()) + '>';
213   } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
214     Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
215   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
216     Op = Op + " VT=" + N->getVT().getMVTString();
217   } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
218     bool doExt = true;
219     switch (LD->getExtensionType()) {
220     default: doExt = false; break;
221     case ISD::EXTLOAD:
222       Op = Op + "<anyext ";
223       break;
224     case ISD::SEXTLOAD:
225       Op = Op + " <sext ";
226       break;
227     case ISD::ZEXTLOAD:
228       Op = Op + " <zext ";
229       break;
230     }
231     if (doExt)
232       Op += LD->getMemoryVT().getMVTString() + ">";
233     if (LD->isVolatile())
234       Op += "<V>";
235     Op += LD->getIndexedModeName(LD->getAddressingMode());
236     if (LD->getAlignment() > 1)
237       Op += " A=" + utostr(LD->getAlignment());
238   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
239     if (ST->isTruncatingStore())
240       Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
241     if (ST->isVolatile())
242       Op += "<V>";
243     Op += ST->getIndexedModeName(ST->getAddressingMode());
244     if (ST->getAlignment() > 1)
245       Op += " A=" + utostr(ST->getAlignment());
246   }
247
248 #if 0
249   Op += " Id=" + itostr(Node->getNodeId());
250 #endif
251   
252   return Op;
253 }
254
255
256 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
257 /// rendered using 'dot'.
258 ///
259 void SelectionDAG::viewGraph(const std::string &Title) {
260 // This code is only for debugging!
261 #ifndef NDEBUG
262   ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
263             Title);
264 #else
265   cerr << "SelectionDAG::viewGraph is only available in debug builds on "
266        << "systems with Graphviz or gv!\n";
267 #endif  // NDEBUG
268 }
269
270 // This overload is defined out-of-line here instead of just using a
271 // default parameter because this is easiest for gdb to call.
272 void SelectionDAG::viewGraph() {
273   viewGraph("");
274 }
275
276 /// clearGraphAttrs - Clear all previously defined node graph attributes.
277 /// Intended to be used from a debugging tool (eg. gdb).
278 void SelectionDAG::clearGraphAttrs() {
279 #ifndef NDEBUG
280   NodeGraphAttrs.clear();
281 #else
282   cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
283        << " on systems with Graphviz or gv!\n";
284 #endif
285 }
286
287
288 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
289 ///
290 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
291 #ifndef NDEBUG
292   NodeGraphAttrs[N] = Attrs;
293 #else
294   cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
295        << " on systems with Graphviz or gv!\n";
296 #endif
297 }
298
299
300 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
301 /// Used from getNodeAttributes.
302 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
303 #ifndef NDEBUG
304   std::map<const SDNode *, std::string>::const_iterator I =
305     NodeGraphAttrs.find(N);
306     
307   if (I != NodeGraphAttrs.end())
308     return I->second;
309   else
310     return "";
311 #else
312   cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
313        << " on systems with Graphviz or gv!\n";
314   return std::string("");
315 #endif
316 }
317
318 /// setGraphColor - Convenience for setting node color attribute.
319 ///
320 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
321 #ifndef NDEBUG
322   NodeGraphAttrs[N] = std::string("color=") + Color;
323 #else
324   cerr << "SelectionDAG::setGraphColor is only available in debug builds"
325        << " on systems with Graphviz or gv!\n";
326 #endif
327 }
328
329 namespace llvm {
330   template<>
331   struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
332     static std::string getGraphName(const ScheduleDAG *G) {
333       return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
334     }
335
336     static bool renderGraphFromBottomUp() {
337       return true;
338     }
339     
340     static bool hasNodeAddressLabel(const SUnit *Node,
341                                     const ScheduleDAG *Graph) {
342       return true;
343     }
344     
345     /// If you want to override the dot attributes printed for a particular
346     /// edge, override this method.
347     template<typename EdgeIter>
348     static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
349       if (EI.isSpecialDep())
350         return "color=cyan,style=dashed";
351       if (EI.isCtrlDep())
352         return "color=blue,style=dashed";
353       return "";
354     }
355     
356
357     static std::string getNodeLabel(const SUnit *Node,
358                                     const ScheduleDAG *Graph);
359     static std::string getNodeAttributes(const SUnit *N,
360                                          const ScheduleDAG *Graph) {
361       return "shape=Mrecord";
362     }
363
364     static void addCustomGraphFeatures(ScheduleDAG *G,
365                                        GraphWriter<ScheduleDAG*> &GW) {
366       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
367       const SDNode *N = G->DAG.getRoot().getNode();
368       if (N && N->getNodeId() != -1)
369         GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1,
370                     "color=blue,style=dashed");
371     }
372   };
373 }
374
375 std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
376                                                        const ScheduleDAG *G) {
377   std::string Op;
378
379   for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
380     Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
381                                                       &G->DAG) + "\n";
382   }
383
384   if (SU->Node)
385     Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
386   else
387     Op += "<CROSS RC COPY>";
388
389   return Op;
390 }
391
392
393 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
394 /// rendered using 'dot'.
395 ///
396 void ScheduleDAG::viewGraph() {
397 // This code is only for debugging!
398 #ifndef NDEBUG
399   ViewGraph(this, "dag." + MF->getFunction()->getName(),
400             "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' +
401             BB->getBasicBlock()->getName());
402 #else
403   cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
404        << "systems with Graphviz or gv!\n";
405 #endif  // NDEBUG
406 }