1 //===- GraphPrinters.cpp - DOT printers for various graph types -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines several printers for various different types of graphs used 11 // by the LLVM infrastructure. It uses the generic graph interface to convert 12 // the graph into a .dot graph. These graphs can then be processed with the 13 // "dot" tool to convert them to postscript or some other suitable format. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/Support/GraphWriter.h" 18 #include "llvm/Pass.h" 19 #include "llvm/Value.h" 20 #include "llvm/Analysis/CallGraph.h" 21 #include "llvm/Analysis/Dominators.h" 22 #include <iostream> 23 #include <fstream> 24 using namespace llvm; 25 26 template<typename GraphType> 27 static void WriteGraphToFile(std::ostream &O, const std::string &GraphName, 28 const GraphType >) { 29 std::string Filename = GraphName + ".dot"; 30 O << "Writing '" << Filename << "'..."; 31 std::ofstream F(Filename.c_str()); 32 33 if (F.good()) 34 WriteGraph(F, GT); 35 else 36 O << " error opening file for writing!"; 37 O << "\n"; 38 } 39 40 41 //===----------------------------------------------------------------------===// 42 // Call Graph Printer 43 //===----------------------------------------------------------------------===// 44 45 namespace llvm { 46 template<> 47 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits { 48 static std::string getGraphName(CallGraph *F) { 49 return "Call Graph"; 50 } 51 52 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph, 53 bool ShortNames) { 54 if (Node->getFunction()) 55 return ((Value*)Node->getFunction())->getName(); 56 else 57 return "Indirect call node"; 58 } 59 }; 60 } 61 62 63 namespace { 64 struct CallGraphPrinter : public ModulePass { 65 static char ID; // Pass ID, replacement for typeid 66 CallGraphPrinter() : ModulePass(&ID) {} 67 68 virtual bool runOnModule(Module &M) { 69 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>()); 70 return false; 71 } 72 73 void print(std::ostream &OS) const {} 74 void print(std::ostream &OS, const llvm::Module*) const {} 75 76 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 77 AU.addRequired<CallGraph>(); 78 AU.setPreservesAll(); 79 } 80 }; 81 82 char CallGraphPrinter::ID = 0; 83 RegisterPass<CallGraphPrinter> P2("dot-callgraph", 84 "Print Call Graph to 'dot' file"); 85 } 86 87 //===----------------------------------------------------------------------===// 88 // DomInfoPrinter Pass 89 //===----------------------------------------------------------------------===// 90 91 namespace { 92 class DomInfoPrinter : public FunctionPass { 93 public: 94 static char ID; // Pass identification, replacement for typeid 95 DomInfoPrinter() : FunctionPass(&ID) {} 96 97 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 98 AU.setPreservesAll(); 99 AU.addRequired<DominatorTree>(); 100 AU.addRequired<DominanceFrontier>(); 101 102 } 103 104 virtual bool runOnFunction(Function &F) { 105 DominatorTree &DT = getAnalysis<DominatorTree>(); 106 DT.dump(); 107 DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); 108 DF.dump(); 109 return false; 110 } 111 }; 112 113 char DomInfoPrinter::ID = 0; 114 static RegisterPass<DomInfoPrinter> 115 DIP("print-dom-info", "Dominator Info Printer", true, true); 116 } 117