1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG::viewGraph method.
12 //===----------------------------------------------------------------------===//
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"
32 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
33 static std::string getGraphName(const SelectionDAG *G) {
34 return G->getMachineFunction().getFunction()->getName();
37 static bool renderGraphFromBottomUp() {
41 static bool hasNodeAddressLabel(const SDNode *Node,
42 const SelectionDAG *Graph) {
46 static std::string getNodeLabel(const SDNode *Node,
47 const SelectionDAG *Graph);
48 static std::string getNodeAttributes(const SDNode *N) {
49 return "shape=Mrecord";
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, "");
60 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
61 const SelectionDAG *G) {
62 std::string Op = Node->getOperationName(G);
64 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
65 if (Node->getValueType(i) == MVT::Other)
68 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
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();
79 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()) + ">";
90 std::ostringstream SS;
91 WriteAsOperand(SS, CP->get(), false);
92 Op += "<" + SS.str() + ">";
94 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
96 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
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());
104 Op += " #" + utostr(R->getReg());
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)) {
111 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
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() + "\"";
124 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
125 /// rendered using 'dot'.
127 void SelectionDAG::viewGraph() {
128 // This code is only for debugging!
132 sprintf(pathsuff, "%06u", unsigned(rand()));
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());
141 std::cerr << " error opening file for writing!\n";
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());
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";
160 Filename.eraseFromDisk();
163 #elif (HAVE_GV && HAVE_DOT)
164 sys::Path PSFilename = TempDir;
165 PSFilename.appendComponent(std::string("dag.tempgraph") + "." + pathsuff + ".ps");
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());
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";
184 sys::Path gv(LLVM_PATH_GV);
186 args.push_back(gv.c_str());
187 args.push_back(PSFilename.c_str());
190 sys::Program::ExecuteAndWait(gv, &args[0]);
192 Filename.eraseFromDisk();
193 PSFilename.eraseFromDisk();
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());
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";
206 #ifndef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
207 Filename.eraseFromDisk();
214 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
215 << "systems with Graphviz or gv!\n";
218 Filename.eraseFromDisk();
219 TempDir.eraseFromDisk(true);