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