SelectionDAGPrinter.cpp revision e39db07713641cb933d1183a343ffe4eb8f44492
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/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" 22#include <fstream> 23using namespace llvm; 24 25namespace llvm { 26 template<> 27 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits { 28 static std::string getGraphName(const SelectionDAG *G) { 29 return G->getMachineFunction().getFunction()->getName(); 30 } 31 32 static bool renderGraphFromBottomUp() { 33 return true; 34 } 35 36 static std::string getNodeLabel(const SDNode *Node, 37 const SelectionDAG *Graph); 38 static std::string getNodeAttributes(const SDNode *N) { 39 return "shape=Mrecord"; 40 } 41 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, ""); 46 } 47 }; 48} 49 50std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node, 51 const SelectionDAG *G) { 52 std::string Op = Node->getOperationName(G); 53 54 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 55 if (Node->getValueType(i) == MVT::Other) 56 Op += ":ch"; 57 else 58 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i)); 59 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)) { 72 Op = "BB: "; 73 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 74 if (LBB) 75 Op += LBB->getName(); 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()); 80 } else { 81 Op += " #" + utostr(R->getReg()); 82 } 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)) { 87 if (M->getValue()) 88 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">"; 89 else 90 Op += "<null:" + itostr(M->getOffset()) + ">"; 91 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) { 92 Op = Op + " VT=" + getValueTypeString(N->getVT()); 93 } 94 return Op; 95} 96 97 98/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG 99/// rendered using 'dot'. 100/// 101void SelectionDAG::viewGraph() { 102// This code is only for debugging! 103#ifndef NDEBUG 104 std::string Filename = "/tmp/dag." + 105 getMachineFunction().getFunction()->getName() + ".dot"; 106 std::cerr << "Writing '" << Filename << "'... "; 107 std::ofstream F(Filename.c_str()); 108 109 if (!F) { 110 std::cerr << " error opening file for writing!\n"; 111 return; 112 } 113 114 WriteGraph(F, this); 115 F.close(); 116 std::cerr << "\n"; 117 118#ifdef HAVE_GRAPHVIZ 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"; 122 } else { 123 system(("rm " + Filename).c_str()); 124 return; 125 } 126#endif // HAVE_GRAPHVIZ 127 128#ifdef HAVE_GV 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"; 133 } else { 134 std::cerr << "\n"; 135 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps"); 136 } 137 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str()); 138 return; 139#endif // HAVE_GV 140#endif // NDEBUG 141 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on " 142 << "systems with Graphviz or gv!\n"; 143 144#ifndef NDEBUG 145 system(("rm " + Filename).c_str()); 146#endif 147} 148