For PR798:
[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/MachineFunction.h"
19 #include "llvm/Target/MRegisterInfo.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/GraphWriter.h"
22 #include "llvm/System/Path.h"
23 #include "llvm/System/Program.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Config/config.h"
26 #include <fstream>
27 #include <sstream>
28 using namespace llvm;
29
30 namespace llvm {
31   template<>
32   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
33     static std::string getGraphName(const SelectionDAG *G) {
34       return G->getMachineFunction().getFunction()->getName();
35     }
36
37     static bool renderGraphFromBottomUp() {
38       return true;
39     }
40     
41     static bool hasNodeAddressLabel(const SDNode *Node,
42                                     const SelectionDAG *Graph) {
43       return true;
44     }
45
46     static std::string getNodeLabel(const SDNode *Node,
47                                     const SelectionDAG *Graph);
48     static std::string getNodeAttributes(const SDNode *N) {
49       return "shape=Mrecord";
50     }
51
52     static void addCustomGraphFeatures(SelectionDAG *G,
53                                        GraphWriter<SelectionDAG*> &GW) {
54       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
55       GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
56     }
57   };
58 }
59
60 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
61                                                         const SelectionDAG *G) {
62   std::string Op = Node->getOperationName(G);
63
64   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
65     if (Node->getValueType(i) == MVT::Other)
66       Op += ":ch";
67     else
68       Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
69     
70   if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
71     Op += ": " + utostr(CSDN->getValue());
72   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
73     Op += ": " + ftostr(CSDN->getValue());
74   } else if (const GlobalAddressSDNode *GADN =
75              dyn_cast<GlobalAddressSDNode>(Node)) {
76     int offset = GADN->getOffset();
77     Op += ": " + GADN->getGlobal()->getName();
78     if (offset > 0)
79       Op += "+" + itostr(offset);
80     else
81       Op += itostr(offset);
82   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
83     Op += " " + itostr(FIDN->getIndex());
84   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
85     if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
86       Op += "<" + ftostr(CFP->getValue()) + ">";
87     else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->get()))
88       Op += "<" + utostr(CI->getZExtValue()) + ">";
89     else {
90       std::ostringstream SS;
91       WriteAsOperand(SS, CP->get(), false);
92       Op += "<" + SS.str() + ">";
93     }
94   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
95     Op = "BB: ";
96     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
97     if (LBB)
98       Op += LBB->getName();
99     //Op += " " + (const void*)BBDN->getBasicBlock();
100   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
101     if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
102       Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
103     } else {
104       Op += " #" + utostr(R->getReg());
105     }
106   } else if (const ExternalSymbolSDNode *ES =
107              dyn_cast<ExternalSymbolSDNode>(Node)) {
108     Op += "'" + std::string(ES->getSymbol()) + "'";
109   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
110     if (M->getValue())
111       Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
112     else
113       Op += "<null:" + itostr(M->getOffset()) + ">";
114   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
115     Op = Op + " VT=" + getValueTypeString(N->getVT());
116   } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
117     Op = Op + "\"" + N->getValue() + "\"";
118   }
119   
120   return Op;
121 }
122
123
124 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
125 /// rendered using 'dot'.
126 ///
127 void SelectionDAG::viewGraph() {
128 // This code is only for debugging!
129 #ifndef NDEBUG
130   char pathsuff[9];
131
132   sprintf(pathsuff, "%06u", unsigned(rand()));
133
134   sys::Path TempDir = sys::Path::GetTemporaryDirectory();
135   sys::Path Filename = TempDir;
136   Filename.appendComponent("dag." + getMachineFunction().getFunction()->getName() + "." + pathsuff + ".dot");
137   std::cerr << "Writing '" << Filename.toString() << "'... ";
138   std::ofstream F(Filename.toString().c_str());
139
140   if (!F) {
141     std::cerr << "  error opening file for writing!\n";
142     return;
143   }
144
145   WriteGraph(F, this);
146   F.close();
147   std::cerr << "\n";
148
149 #if HAVE_GRAPHVIZ
150   sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
151   std::vector<const char*> args;
152   args.push_back(Graphviz.c_str());
153   args.push_back(Filename.c_str());
154   args.push_back(0);
155   
156   std::cerr << "Running 'Graphviz' program... " << std::flush;
157   if (sys::Program::ExecuteAndWait(Graphviz, &args[0])) {
158     std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
159   } else {
160     Filename.eraseFromDisk();
161     return;
162   }
163 #elif (HAVE_GV && HAVE_DOT)
164   sys::Path PSFilename = TempDir;
165   PSFilename.appendComponent(std::string("dag.tempgraph") + "." + pathsuff + ".ps");
166
167   sys::Path dot(LLVM_PATH_DOT);
168   std::vector<const char*> args;
169   args.push_back(dot.c_str());
170   args.push_back("-Tps");
171   args.push_back("-Nfontname=Courier");
172   args.push_back("-Gsize=7.5,10");
173   args.push_back(Filename.c_str());
174   args.push_back("-o");
175   args.push_back(PSFilename.c_str());
176   args.push_back(0);
177   
178   std::cerr << "Running 'dot' program... " << std::flush;
179   if (sys::Program::ExecuteAndWait(dot, &args[0])) {
180     std::cerr << "Error viewing graph: 'dot' not in path?\n";
181   } else {
182     std::cerr << "\n";
183
184     sys::Path gv(LLVM_PATH_GV);
185     args.clear();
186     args.push_back(gv.c_str());
187     args.push_back(PSFilename.c_str());
188     args.push_back(0);
189     
190     sys::Program::ExecuteAndWait(gv, &args[0]);
191   }
192   Filename.eraseFromDisk();
193   PSFilename.eraseFromDisk();
194   return;
195 #elif HAVE_DOTTY
196   sys::Path dotty(LLVM_PATH_DOTTY);
197   std::vector<const char*> args;
198   args.push_back(dotty.c_str());
199   args.push_back(Filename.c_str());
200   args.push_back(0);
201   
202   std::cerr << "Running 'dotty' program... " << std::flush;
203   if (sys::Program::ExecuteAndWait(dotty, &args[0])) {
204     std::cerr << "Error viewing graph: 'dotty' not in path?\n";
205   } else {
206 #ifndef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
207     Filename.eraseFromDisk();
208 #endif
209     return;
210   }
211 #endif
212   
213 #endif  // NDEBUG
214   std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
215             << "systems with Graphviz or gv!\n";
216
217 #ifndef NDEBUG
218   Filename.eraseFromDisk();
219   TempDir.eraseFromDisk(true);
220 #endif
221 }