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/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/System/Path.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Config/config.h"
29 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
30 static std::string getGraphName(const SelectionDAG *G) {
31 return G->getMachineFunction().getFunction()->getName();
34 static bool renderGraphFromBottomUp() {
38 static bool hasNodeAddressLabel(const SDNode *Node,
39 const SelectionDAG *Graph) {
43 static std::string getNodeLabel(const SDNode *Node,
44 const SelectionDAG *Graph);
45 static std::string getNodeAttributes(const SDNode *N) {
46 return "shape=Mrecord";
49 static void addCustomGraphFeatures(SelectionDAG *G,
50 GraphWriter<SelectionDAG*> &GW) {
51 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
52 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
57 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
58 const SelectionDAG *G) {
59 std::string Op = Node->getOperationName(G);
61 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
62 if (Node->getValueType(i) == MVT::Other)
65 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
67 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
68 Op += ": " + utostr(CSDN->getValue());
69 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
70 Op += ": " + ftostr(CSDN->getValue());
71 } else if (const GlobalAddressSDNode *GADN =
72 dyn_cast<GlobalAddressSDNode>(Node)) {
73 int offset = GADN->getOffset();
74 Op += ": " + GADN->getGlobal()->getName();
76 Op += "+" + itostr(offset);
79 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
80 Op += " " + itostr(FIDN->getIndex());
81 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
82 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
83 Op += "<" + ftostr(CFP->getValue()) + ">";
84 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
86 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
89 //Op += " " + (const void*)BBDN->getBasicBlock();
90 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
91 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
92 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
94 Op += " #" + utostr(R->getReg());
96 } else if (const ExternalSymbolSDNode *ES =
97 dyn_cast<ExternalSymbolSDNode>(Node)) {
98 Op += "'" + std::string(ES->getSymbol()) + "'";
99 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
101 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
103 Op += "<null:" + itostr(M->getOffset()) + ">";
104 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
105 Op = Op + " VT=" + getValueTypeString(N->getVT());
106 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
107 Op = Op + "\"" + N->getValue() + "\"";
114 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
115 /// rendered using 'dot'.
117 void SelectionDAG::viewGraph() {
118 // This code is only for debugging!
120 sys::Path TempDir = sys::Path::GetTemporaryDirectory();
121 sys::Path Filename = TempDir;
122 Filename.appendComponent("dag." + getMachineFunction().getFunction()->getName() + ".dot");
123 std::cerr << "Writing '" << Filename.toString() << "'... ";
124 std::ofstream F(Filename.toString().c_str());
127 std::cerr << " error opening file for writing!\n";
136 std::cerr << "Running 'Graphviz' program... " << std::flush;
137 if (system((LLVM_PATH_GRAPHVIZ " " + Filename.toString()).c_str())) {
138 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
140 Filename.eraseFromDisk();
143 #endif // HAVE_GRAPHVIZ
146 std::cerr << "Running 'dot' program... " << std::flush;
147 sys::Path PSFilename = TempDir;
148 PSFilename.appendComponent("dag.tempgraph.ps");
149 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename.toString()
150 + " > " + PSFilename.toString()).c_str())) {
151 std::cerr << "Error viewing graph: 'dot' not in path?\n";
154 system((LLVM_PATH_GV " " + PSFilename.toString()).c_str());
155 system((LLVM_PATH_GV " "+TempDir.toString()+ "dag.tempgraph.ps").c_str());
157 Filename.eraseFromDisk();
158 PSFilename.eraseFromDisk();
162 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
163 << "systems with Graphviz or gv!\n";
166 Filename.eraseFromDisk();
167 TempDir.eraseFromDisk(true);