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