1 //===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
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"
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
<< "'...";
32 raw_fd_ostream
F(Filename
.c_str(), ErrInfo
);
37 O
<< " error opening file for writing!";
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
48 struct DOTGraphTraits
<CallGraph
*> : public DefaultDOTGraphTraits
{
49 static std::string
getGraphName(CallGraph
*F
) {
53 static std::string
getNodeLabel(CallGraphNode
*Node
, CallGraph
*Graph
,
55 if (Node
->getFunction())
56 return ((Value
*)Node
->getFunction())->getName();
58 return "Indirect call node";
65 struct CallGraphPrinter
: public ModulePass
{
66 static char ID
; // Pass ID, replacement for typeid
67 CallGraphPrinter() : ModulePass(&ID
) {}
69 virtual bool runOnModule(Module
&M
) {
70 WriteGraphToFile(std::cerr
, "callgraph", &getAnalysis
<CallGraph
>());
74 void print(raw_ostream
&OS
, const llvm::Module
*) const {}
76 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
77 AU
.addRequired
<CallGraph
>();
82 char CallGraphPrinter::ID
= 0;
83 RegisterPass
<CallGraphPrinter
> P2("dot-callgraph",
84 "Print Call Graph to 'dot' file");
87 //===----------------------------------------------------------------------===//
88 // DomInfoPrinter Pass
89 //===----------------------------------------------------------------------===//
92 class DomInfoPrinter
: public FunctionPass
{
94 static char ID
; // Pass identification, replacement for typeid
95 DomInfoPrinter() : FunctionPass(&ID
) {}
97 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
99 AU
.addRequired
<DominatorTree
>();
100 AU
.addRequired
<DominanceFrontier
>();
104 virtual bool runOnFunction(Function
&F
) {
105 DominatorTree
&DT
= getAnalysis
<DominatorTree
>();
107 DominanceFrontier
&DF
= getAnalysis
<DominanceFrontier
>();
113 char DomInfoPrinter::ID
= 0;
114 static RegisterPass
<DomInfoPrinter
>
115 DIP("print-dom-info", "Dominator Info Printer", true, true);