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();
52 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
53 switch (Node->getValueType(i)) {
54 default: Op += ":unknownvt!"; break;
55 case MVT::Other: Op += ":ch"; break;
56 case MVT::i1: Op += ":i1"; break;
57 case MVT::i8: Op += ":i8"; break;
58 case MVT::i16: Op += ":i16"; break;
59 case MVT::i32: Op += ":i32"; break;
60 case MVT::i64: Op += ":i64"; break;
61 case MVT::i128: Op += ":i128"; break;
62 case MVT::f32: Op += ":f32"; break;
63 case MVT::f64: Op += ":f64"; break;
64 case MVT::f80: Op += ":f80"; break;
65 case MVT::f128: Op += ":f128"; break;
66 case MVT::isVoid: Op += ":void"; break;
70 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
71 Op += ": " + utostr(CSDN->getValue());
72 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
73 Op += ": " + ftostr(CSDN->getValue());
74 } else if (const GlobalAddressSDNode *GADN =
75 dyn_cast<GlobalAddressSDNode>(Node)) {
76 Op += ": " + GADN->getGlobal()->getName();
77 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
78 Op += " " + itostr(FIDN->getIndex());
79 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
80 Op += "<" + utostr(CP->getIndex()) + ">";
81 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
83 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
86 //Op += " " + (const void*)BBDN->getBasicBlock();
87 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(Node)) {
88 Op += " #" + utostr(C2V->getReg());
89 } else if (const ExternalSymbolSDNode *ES =
90 dyn_cast<ExternalSymbolSDNode>(Node)) {
91 Op += "'" + std::string(ES->getSymbol()) + "'";
92 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
94 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
96 Op += "<null:" + itostr(M->getOffset()) + ">";
102 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
103 /// rendered using 'dot'.
105 void SelectionDAG::viewGraph() {
106 // This code is only for debugging!
108 std::string Filename = "/tmp/dag." +
109 getMachineFunction().getFunction()->getName() + ".dot";
110 std::cerr << "Writing '" << Filename << "'... ";
111 std::ofstream F(Filename.c_str());
114 std::cerr << " error opening file for writing!\n";
123 std::cerr << "Running 'Graphviz' program... " << std::flush;
124 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
125 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
132 std::cerr << "Running 'dot' program... " << std::flush;
133 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
134 + " > /tmp/dag.tempgraph.ps").c_str())) {
135 std::cerr << "Error viewing graph: 'dot' not in path?\n";
138 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
140 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
144 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
145 << "systems with Graphviz or gv!\n";