SelectionDAGPrinter.cpp revision c5f44add43bf9ecb7f1f63a320e4440f8f0784c3
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/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 <fstream> 20using namespace llvm; 21 22namespace llvm { 23 template<> 24 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits { 25 static std::string getGraphName(const SelectionDAG *G) { 26 return G->getMachineFunction().getFunction()->getName(); 27 } 28 29 static bool renderGraphFromBottomUp() { 30 return true; 31 } 32 33 static std::string getNodeLabel(const SDNode *Node, 34 const SelectionDAG *Graph); 35 static std::string getNodeAttributes(const SDNode *N) { 36 return "shape=Mrecord"; 37 } 38 39 static void addCustomGraphFeatures(SelectionDAG *G, 40 GraphWriter<SelectionDAG*> &GW) { 41 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); 42 GW.emitEdge(0, -1, G->getRoot().Val, -1, ""); 43 } 44 }; 45} 46 47std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node, 48 const SelectionDAG *G) { 49 std::string Op = Node->getOperationName(); 50 51 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { 52 switch (Node->getValueType(i)) { 53 default: Op += ":unknownvt!"; break; 54 case MVT::Other: Op += ":ch"; break; 55 case MVT::i1: Op += ":i1"; break; 56 case MVT::i8: Op += ":i8"; break; 57 case MVT::i16: Op += ":i16"; break; 58 case MVT::i32: Op += ":i32"; break; 59 case MVT::i64: Op += ":i64"; break; 60 case MVT::i128: Op += ":i128"; break; 61 case MVT::f32: Op += ":f32"; break; 62 case MVT::f64: Op += ":f64"; break; 63 case MVT::f80: Op += ":f80"; break; 64 case MVT::f128: Op += ":f128"; break; 65 case MVT::isVoid: Op += ":void"; break; 66 } 67 } 68 69 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) { 70 Op += ": " + utostr(CSDN->getValue()); 71 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) { 72 Op += ": " + ftostr(CSDN->getValue()); 73 } else if (const GlobalAddressSDNode *GADN = 74 dyn_cast<GlobalAddressSDNode>(Node)) { 75 Op += ": " + GADN->getGlobal()->getName(); 76 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) { 77 Op += " " + itostr(FIDN->getIndex()); 78 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){ 79 Op += "<" + utostr(CP->getIndex()) + ">"; 80 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) { 81 Op = "BB: "; 82 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 83 if (LBB) 84 Op += LBB->getName(); 85 //Op += " " + (const void*)BBDN->getBasicBlock(); 86 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(Node)) { 87 Op += " #" + utostr(C2V->getReg()); 88 } else if (const ExternalSymbolSDNode *ES = 89 dyn_cast<ExternalSymbolSDNode>(Node)) { 90 Op += "'" + std::string(ES->getSymbol()) + "'"; 91 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) { 92 if (M->getValue()) 93 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">"; 94 else 95 Op += "<null:" + itostr(M->getOffset()) + ">"; 96 } 97 return Op; 98} 99 100 101/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG 102/// rendered using 'dot'. 103/// 104void SelectionDAG::viewGraph() { 105// This code is only for debugging! 106#ifndef NDEBUG 107 std::string Filename = "/tmp/dag." + 108 getMachineFunction().getFunction()->getName() + ".dot"; 109 std::cerr << "Writing '" << Filename << "'... "; 110 std::ofstream F(Filename.c_str()); 111 112 if (!F) { 113 std::cerr << " error opening file for writing!\n"; 114 return; 115 } 116 117 WriteGraph(F, this); 118 F.close(); 119 std::cerr << "\n"; 120 121#ifdef HAVE_GRAPHVIZ 122 std::cerr << "Running 'Graphviz' program... " << std::flush; 123 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) { 124 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n"; 125 } else { 126 return; 127 } 128#endif 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 142#endif 143 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on " 144 << "systems with Graphviz or gv!\n"; 145} 146