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/Function.h"
17 #include "llvm/Support/GraphWriter.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Config/config.h"
25 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
26 static std::string getGraphName(const SelectionDAG *G) {
27 return G->getMachineFunction().getFunction()->getName();
30 static bool renderGraphFromBottomUp() {
34 static std::string getNodeLabel(const SDNode *Node,
35 const SelectionDAG *Graph);
36 static std::string getNodeAttributes(const SDNode *N) {
37 return "shape=Mrecord";
40 static void addCustomGraphFeatures(SelectionDAG *G,
41 GraphWriter<SelectionDAG*> &GW) {
42 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
43 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
48 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
49 const SelectionDAG *G) {
50 std::string Op = Node->getOperationName(G);
52 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
53 if (Node->getValueType(i) == MVT::Other)
56 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
58 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
59 Op += ": " + utostr(CSDN->getValue());
60 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
61 Op += ": " + ftostr(CSDN->getValue());
62 } else if (const GlobalAddressSDNode *GADN =
63 dyn_cast<GlobalAddressSDNode>(Node)) {
64 Op += ": " + GADN->getGlobal()->getName();
65 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
66 Op += " " + itostr(FIDN->getIndex());
67 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
68 Op += "<" + utostr(CP->getIndex()) + ">";
69 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
71 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
74 //Op += " " + (const void*)BBDN->getBasicBlock();
75 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
76 Op += " #" + utostr(R->getReg());
77 } else if (const ExternalSymbolSDNode *ES =
78 dyn_cast<ExternalSymbolSDNode>(Node)) {
79 Op += "'" + std::string(ES->getSymbol()) + "'";
80 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
82 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
84 Op += "<null:" + itostr(M->getOffset()) + ">";
85 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
86 std::cerr << ":" << getValueTypeString(N->getVT());
92 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
93 /// rendered using 'dot'.
95 void SelectionDAG::viewGraph() {
96 // This code is only for debugging!
98 std::string Filename = "/tmp/dag." +
99 getMachineFunction().getFunction()->getName() + ".dot";
100 std::cerr << "Writing '" << Filename << "'... ";
101 std::ofstream F(Filename.c_str());
104 std::cerr << " error opening file for writing!\n";
113 std::cerr << "Running 'Graphviz' program... " << std::flush;
114 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
115 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
117 system(("rm " + Filename).c_str());
120 #endif // HAVE_GRAPHVIZ
123 std::cerr << "Running 'dot' program... " << std::flush;
124 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
125 + " > /tmp/dag.tempgraph.ps").c_str())) {
126 std::cerr << "Error viewing graph: 'dot' not in path?\n";
129 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
131 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
135 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
136 << "systems with Graphviz or gv!\n";
139 system(("rm " + Filename).c_str());