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/CodeGen/SelectionDAG.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/Target/MRegisterInfo.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Function.h"
19 #include "llvm/Support/GraphWriter.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Config/config.h"
27 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
28 static std::string getGraphName(const SelectionDAG *G) {
29 return G->getMachineFunction().getFunction()->getName();
32 static bool renderGraphFromBottomUp() {
36 static std::string getNodeLabel(const SDNode *Node,
37 const SelectionDAG *Graph);
38 static std::string getNodeAttributes(const SDNode *N) {
39 return "shape=Mrecord";
42 static void addCustomGraphFeatures(SelectionDAG *G,
43 GraphWriter<SelectionDAG*> &GW) {
44 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
45 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
50 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
51 const SelectionDAG *G) {
52 std::string Op = Node->getOperationName(G);
54 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
55 if (Node->getValueType(i) == MVT::Other)
58 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
60 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
61 Op += ": " + utostr(CSDN->getValue());
62 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
63 Op += ": " + ftostr(CSDN->getValue());
64 } else if (const GlobalAddressSDNode *GADN =
65 dyn_cast<GlobalAddressSDNode>(Node)) {
66 Op += ": " + GADN->getGlobal()->getName();
67 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
68 Op += " " + itostr(FIDN->getIndex());
69 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
70 Op += "<" + utostr(CP->getIndex()) + ">";
71 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
73 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
76 //Op += " " + (const void*)BBDN->getBasicBlock();
77 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
78 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
79 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
81 Op += " #" + utostr(R->getReg());
83 } else if (const ExternalSymbolSDNode *ES =
84 dyn_cast<ExternalSymbolSDNode>(Node)) {
85 Op += "'" + std::string(ES->getSymbol()) + "'";
86 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
88 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
90 Op += "<null:" + itostr(M->getOffset()) + ">";
91 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
92 std::cerr << ":" << getValueTypeString(N->getVT());
98 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
99 /// rendered using 'dot'.
101 void SelectionDAG::viewGraph() {
102 // This code is only for debugging!
104 std::string Filename = "/tmp/dag." +
105 getMachineFunction().getFunction()->getName() + ".dot";
106 std::cerr << "Writing '" << Filename << "'... ";
107 std::ofstream F(Filename.c_str());
110 std::cerr << " error opening file for writing!\n";
119 std::cerr << "Running 'Graphviz' program... " << std::flush;
120 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
121 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
123 system(("rm " + Filename).c_str());
126 #endif // HAVE_GRAPHVIZ
129 std::cerr << "Running 'dot' program... " << std::flush;
130 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
131 + " > /tmp/dag.tempgraph.ps").c_str())) {
132 std::cerr << "Error viewing graph: 'dot' not in path?\n";
135 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
137 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
141 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
142 << "systems with Graphviz or gv!\n";
145 system(("rm " + Filename).c_str());