Added an index field to GlobalAddressSDNode so it can represent X+12, etc.
[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/CodeGen/SelectionDAG.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/Target/MRegisterInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Support/GraphWriter.h"
21 #include "llvm/System/Path.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Config/config.h"
24 #include <fstream>
25 using namespace llvm;
26
27 namespace llvm {
28   template<>
29   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
30     static std::string getGraphName(const SelectionDAG *G) {
31       return G->getMachineFunction().getFunction()->getName();
32     }
33
34     static bool renderGraphFromBottomUp() {
35       return true;
36     }
37     
38     static bool hasNodeAddressLabel(const SDNode *Node,
39                                     const SelectionDAG *Graph) {
40       return true;
41     }
42
43     static std::string getNodeLabel(const SDNode *Node,
44                                     const SelectionDAG *Graph);
45     static std::string getNodeAttributes(const SDNode *N) {
46       return "shape=Mrecord";
47     }
48
49     static void addCustomGraphFeatures(SelectionDAG *G,
50                                        GraphWriter<SelectionDAG*> &GW) {
51       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
52       GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
53     }
54   };
55 }
56
57 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
58                                                         const SelectionDAG *G) {
59   std::string Op = Node->getOperationName(G);
60
61   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
62     if (Node->getValueType(i) == MVT::Other)
63       Op += ":ch";
64     else
65       Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
66     
67   if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
68     Op += ": " + utostr(CSDN->getValue());
69   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
70     Op += ": " + ftostr(CSDN->getValue());
71   } else if (const GlobalAddressSDNode *GADN =
72              dyn_cast<GlobalAddressSDNode>(Node)) {
73     int offset = GADN->getOffset();
74     Op += ": " + GADN->getGlobal()->getName();
75     if (offset > 0)
76       Op += "+" + itostr(offset);
77     else
78       Op += itostr(offset);
79   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
80     Op += " " + itostr(FIDN->getIndex());
81   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
82     if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
83       Op += "<" + ftostr(CFP->getValue()) + ">";
84   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
85     Op = "BB: ";
86     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
87     if (LBB)
88       Op += LBB->getName();
89     //Op += " " + (const void*)BBDN->getBasicBlock();
90   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
91     if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
92       Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
93     } else {
94       Op += " #" + utostr(R->getReg());
95     }
96   } else if (const ExternalSymbolSDNode *ES =
97              dyn_cast<ExternalSymbolSDNode>(Node)) {
98     Op += "'" + std::string(ES->getSymbol()) + "'";
99   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
100     if (M->getValue())
101       Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
102     else
103       Op += "<null:" + itostr(M->getOffset()) + ">";
104   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
105     Op = Op + " VT=" + getValueTypeString(N->getVT());
106   } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
107     Op = Op + "\"" + N->getValue() + "\"";
108   }
109   
110   return Op;
111 }
112
113
114 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
115 /// rendered using 'dot'.
116 ///
117 void SelectionDAG::viewGraph() {
118 // This code is only for debugging!
119 #ifndef NDEBUG
120   sys::Path TempDir = sys::Path::GetTemporaryDirectory();
121   sys::Path Filename = TempDir;
122   Filename.appendComponent("dag." + getMachineFunction().getFunction()->getName() + ".dot");
123   std::cerr << "Writing '" << Filename.toString() << "'... ";
124   std::ofstream F(Filename.toString().c_str());
125
126   if (!F) {
127     std::cerr << "  error opening file for writing!\n";
128     return;
129   }
130
131   WriteGraph(F, this);
132   F.close();
133   std::cerr << "\n";
134
135 #ifdef HAVE_GRAPHVIZ
136   std::cerr << "Running 'Graphviz' program... " << std::flush;
137   if (system((LLVM_PATH_GRAPHVIZ " " + Filename.toString()).c_str())) {
138     std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
139   } else {
140         Filename.eraseFromDisk();
141     return;
142   }
143 #endif  // HAVE_GRAPHVIZ
144
145 #ifdef HAVE_GV
146   std::cerr << "Running 'dot' program... " << std::flush;
147   sys::Path PSFilename = TempDir;
148   PSFilename.appendComponent("dag.tempgraph.ps");
149   if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename.toString()
150               + " > " + PSFilename.toString()).c_str())) {
151     std::cerr << "Error viewing graph: 'dot' not in path?\n";
152   } else {
153     std::cerr << "\n";
154     system((LLVM_PATH_GV " " + PSFilename.toString()).c_str());
155     system((LLVM_PATH_GV " "+TempDir.toString()+ "dag.tempgraph.ps").c_str());
156   }
157   Filename.eraseFromDisk();
158   PSFilename.eraseFromDisk();
159   return;
160 #endif  // HAVE_GV
161 #endif  // NDEBUG
162   std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
163             << "systems with Graphviz or gv!\n";
164
165 #ifndef NDEBUG
166   Filename.eraseFromDisk();
167   TempDir.eraseFromDisk(true);
168 #endif
169 }