• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "llvm/Support/ToolOutputFile.h"
23 using namespace llvm;
24 
25 template<typename GraphType>
WriteGraphToFile(raw_ostream & O,const std::string & GraphName,const GraphType & GT)26 static void WriteGraphToFile(raw_ostream &O, const std::string &GraphName,
27                              const GraphType &GT) {
28   std::string Filename = GraphName + ".dot";
29   O << "Writing '" << Filename << "'...";
30   std::string ErrInfo;
31   tool_output_file F(Filename.c_str(), ErrInfo);
32 
33   if (ErrInfo.empty()) {
34     WriteGraph(F.os(), GT);
35     F.os().close();
36     if (!F.os().has_error()) {
37       O << "\n";
38       F.keep();
39       return;
40     }
41   }
42   O << "  error opening file for writing!\n";
43   F.os().clear_error();
44 }
45 
46 
47 //===----------------------------------------------------------------------===//
48 //                              Call Graph Printer
49 //===----------------------------------------------------------------------===//
50 
51 namespace llvm {
52   template<>
53   struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
54 
DOTGraphTraitsllvm::DOTGraphTraits55   DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
56 
getGraphNamellvm::DOTGraphTraits57     static std::string getGraphName(CallGraph *F) {
58       return "Call Graph";
59     }
60 
getNodeLabelllvm::DOTGraphTraits61     static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
62       if (Node->getFunction())
63         return ((Value*)Node->getFunction())->getName();
64       return "external node";
65     }
66   };
67 }
68 
69 
70 namespace {
71   struct CallGraphPrinter : public ModulePass {
72     static char ID; // Pass ID, replacement for typeid
CallGraphPrinter__anon46ecb24c0111::CallGraphPrinter73     CallGraphPrinter() : ModulePass(ID) {}
74 
runOnModule__anon46ecb24c0111::CallGraphPrinter75     virtual bool runOnModule(Module &M) {
76       WriteGraphToFile(llvm::errs(), "callgraph", &getAnalysis<CallGraph>());
77       return false;
78     }
79 
print__anon46ecb24c0111::CallGraphPrinter80     void print(raw_ostream &OS, const llvm::Module*) const {}
81 
getAnalysisUsage__anon46ecb24c0111::CallGraphPrinter82     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
83       AU.addRequired<CallGraph>();
84       AU.setPreservesAll();
85     }
86   };
87 }
88 
89 char CallGraphPrinter::ID = 0;
90 static RegisterPass<CallGraphPrinter> P2("dot-callgraph",
91                                          "Print Call Graph to 'dot' file");
92 
93 //===----------------------------------------------------------------------===//
94 //                            DomInfoPrinter Pass
95 //===----------------------------------------------------------------------===//
96 
97 namespace {
98   class DomInfoPrinter : public FunctionPass {
99   public:
100     static char ID; // Pass identification, replacement for typeid
DomInfoPrinter()101     DomInfoPrinter() : FunctionPass(ID) {}
102 
getAnalysisUsage(AnalysisUsage & AU) const103     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
104       AU.setPreservesAll();
105       AU.addRequired<DominatorTree>();
106 
107     }
108 
runOnFunction(Function & F)109     virtual bool runOnFunction(Function &F) {
110       getAnalysis<DominatorTree>().dump();
111       return false;
112     }
113   };
114 }
115 
116 char DomInfoPrinter::ID = 0;
117 static RegisterPass<DomInfoPrinter>
118 DIP("print-dom-info", "Dominator Info Printer", true, true);
119