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