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