blob: ebfba7ef0f61ebb082cea111d8947f7155e75ab4 [file] [log] [blame]
Chris Lattner82e74162009-10-18 04:10:40 +00001//===- DomPrinter.cpp - DOT printer for the dominance trees ------------===//
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 '-dot-dom' and '-dot-postdom' analysis passes, which emit
11// a dom.<fnname>.dot or postdom.<fnname>.dot file for each function in the
12// program, with a graph of the dominance/postdominance tree of that
13// function.
14//
15// There are also passes available to directly call dotty ('-view-dom' or
16// '-view-postdom'). By appending '-only' like '-dot-dom-only' only the
17// names of the bbs are printed, but the content is hidden.
18//
19//===----------------------------------------------------------------------===//
20
21#include "llvm/Analysis/DomPrinter.h"
Chris Lattner82e74162009-10-18 04:10:40 +000022#include "llvm/Pass.h"
23#include "llvm/Function.h"
24#include "llvm/Analysis/CFGPrinter.h"
25#include "llvm/Analysis/Dominators.h"
26#include "llvm/Analysis/PostDominators.h"
27
28using namespace llvm;
29
30namespace llvm {
31template<>
32struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
33 static std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph,
34 bool ShortNames) {
35
36 BasicBlock *BB = Node->getBlock();
37
38 if (!BB)
39 return "Post dominance root node";
40
41 return DOTGraphTraits<const Function*>::getNodeLabel(BB, BB->getParent(),
42 ShortNames);
43 }
44};
45
46template<>
47struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
48
49 static std::string getGraphName(DominatorTree *DT) {
50 return "Dominator tree";
51 }
52
53 static std::string getNodeLabel(DomTreeNode *Node,
54 DominatorTree *G,
55 bool ShortNames) {
56 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode(),
57 ShortNames);
58 }
59};
60
61template<>
62struct DOTGraphTraits<PostDominatorTree*>
63 : public DOTGraphTraits<DomTreeNode*> {
64 static std::string getGraphName(PostDominatorTree *DT) {
65 return "Post dominator tree";
66 }
67 static std::string getNodeLabel(DomTreeNode *Node,
68 PostDominatorTree *G,
69 bool ShortNames) {
70 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node,
71 G->getRootNode(),
72 ShortNames);
73 }
74};
75}
76
77namespace {
78template <class Analysis, bool OnlyBBS>
79struct GenericGraphViewer : public FunctionPass {
Chris Lattner82e74162009-10-18 04:10:40 +000080 std::string Name;
81
Chris Lattnerbc8aea92009-10-18 04:27:14 +000082 GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) {
Chris Lattner82e74162009-10-18 04:10:40 +000083 Name = GraphName;
84 }
85
86 virtual bool runOnFunction(Function &F) {
87 Analysis *Graph;
Tobias Grosser6d6054a2009-11-30 12:06:37 +000088 std::string Title, GraphName;
Chris Lattner82e74162009-10-18 04:10:40 +000089 Graph = &getAnalysis<Analysis>();
Tobias Grosser6d6054a2009-11-30 12:06:37 +000090 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
91 Title = GraphName + " for '" + F.getNameStr() + "' function";
92 ViewGraph(Graph, Name, OnlyBBS, Title);
Chris Lattner82e74162009-10-18 04:10:40 +000093
94 return false;
95 }
96
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.setPreservesAll();
99 AU.addRequired<Analysis>();
100 }
101};
102
103struct DomViewer
104 : public GenericGraphViewer<DominatorTree, false> {
105 static char ID;
Chris Lattnerbc8aea92009-10-18 04:27:14 +0000106 DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){}
Chris Lattner82e74162009-10-18 04:10:40 +0000107};
108
109struct DomOnlyViewer
110 : public GenericGraphViewer<DominatorTree, true> {
111 static char ID;
Chris Lattnerbc8aea92009-10-18 04:27:14 +0000112 DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){}
Chris Lattner82e74162009-10-18 04:10:40 +0000113};
114
115struct PostDomViewer
116 : public GenericGraphViewer<PostDominatorTree, false> {
117 static char ID;
118 PostDomViewer() :
Chris Lattnerbc8aea92009-10-18 04:27:14 +0000119 GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){}
Chris Lattner82e74162009-10-18 04:10:40 +0000120};
121
122struct PostDomOnlyViewer
123 : public GenericGraphViewer<PostDominatorTree, true> {
124 static char ID;
125 PostDomOnlyViewer() :
Chris Lattnerbc8aea92009-10-18 04:27:14 +0000126 GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){}
Chris Lattner82e74162009-10-18 04:10:40 +0000127};
128} // end anonymous namespace
129
130char DomViewer::ID = 0;
131RegisterPass<DomViewer> A("view-dom",
132 "View dominance tree of function");
133
134char DomOnlyViewer::ID = 0;
135RegisterPass<DomOnlyViewer> B("view-dom-only",
136 "View dominance tree of function "
137 "(with no function bodies)");
138
139char PostDomViewer::ID = 0;
140RegisterPass<PostDomViewer> C("view-postdom",
141 "View postdominance tree of function");
142
143char PostDomOnlyViewer::ID = 0;
144RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
145 "View postdominance tree of function "
146 "(with no function bodies)");
147
148namespace {
149template <class Analysis, bool OnlyBBS>
150struct GenericGraphPrinter : public FunctionPass {
151
Chris Lattner82e74162009-10-18 04:10:40 +0000152 std::string Name;
153
Chris Lattner22a687f2009-10-18 04:58:34 +0000154 GenericGraphPrinter(std::string GraphName, const void *ID)
155 : FunctionPass(ID) {
Chris Lattner82e74162009-10-18 04:10:40 +0000156 Name = GraphName;
157 }
158
159 virtual bool runOnFunction(Function &F) {
160 Analysis *Graph;
161 std::string Filename = Name + "." + F.getNameStr() + ".dot";
162 errs() << "Writing '" << Filename << "'...";
163
164 std::string ErrorInfo;
165 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
166 Graph = &getAnalysis<Analysis>();
167
Tobias Grosser6d6054a2009-11-30 12:06:37 +0000168 std::string Title, GraphName;
169 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
170 Title = GraphName + " for '" + F.getNameStr() + "' function";
171
Chris Lattner82e74162009-10-18 04:10:40 +0000172 if (ErrorInfo.empty())
Tobias Grosser6d6054a2009-11-30 12:06:37 +0000173 WriteGraph(File, Graph, OnlyBBS, Name, Title);
Chris Lattner82e74162009-10-18 04:10:40 +0000174 else
175 errs() << " error opening file for writing!";
176 errs() << "\n";
177 return false;
178 }
179
180 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
181 AU.setPreservesAll();
182 AU.addRequired<Analysis>();
183 }
184};
185
186struct DomPrinter
187 : public GenericGraphPrinter<DominatorTree, false> {
188 static char ID;
Chris Lattner22a687f2009-10-18 04:58:34 +0000189 DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom", &ID) {}
Chris Lattner82e74162009-10-18 04:10:40 +0000190};
191
192struct DomOnlyPrinter
193 : public GenericGraphPrinter<DominatorTree, true> {
194 static char ID;
Chris Lattner22a687f2009-10-18 04:58:34 +0000195 DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly", &ID) {}
Chris Lattner82e74162009-10-18 04:10:40 +0000196};
197
198struct PostDomPrinter
199 : public GenericGraphPrinter<PostDominatorTree, false> {
200 static char ID;
201 PostDomPrinter() :
Chris Lattner22a687f2009-10-18 04:58:34 +0000202 GenericGraphPrinter<PostDominatorTree, false>("postdom", &ID) {}
Chris Lattner82e74162009-10-18 04:10:40 +0000203};
204
205struct PostDomOnlyPrinter
206 : public GenericGraphPrinter<PostDominatorTree, true> {
207 static char ID;
208 PostDomOnlyPrinter() :
Chris Lattner22a687f2009-10-18 04:58:34 +0000209 GenericGraphPrinter<PostDominatorTree, true>("postdomonly", &ID) {}
Chris Lattner82e74162009-10-18 04:10:40 +0000210};
211} // end anonymous namespace
212
213
214
215char DomPrinter::ID = 0;
216RegisterPass<DomPrinter> E("dot-dom",
217 "Print dominance tree of function "
218 "to 'dot' file");
219
220char DomOnlyPrinter::ID = 0;
221RegisterPass<DomOnlyPrinter> F("dot-dom-only",
222 "Print dominance tree of function "
223 "to 'dot' file "
224 "(with no function bodies)");
225
226char PostDomPrinter::ID = 0;
227RegisterPass<PostDomPrinter> G("dot-postdom",
228 "Print postdominance tree of function "
229 "to 'dot' file");
230
231char PostDomOnlyPrinter::ID = 0;
232RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
233 "Print postdominance tree of function "
234 "to 'dot' file "
235 "(with no function bodies)");
236
237// Create methods available outside of this file, to use them
238// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
239// the link time optimization.
240
241FunctionPass *llvm::createDomPrinterPass() {
242 return new DomPrinter();
243}
244
245FunctionPass *llvm::createDomOnlyPrinterPass() {
246 return new DomOnlyPrinter();
247}
248
249FunctionPass *llvm::createDomViewerPass() {
250 return new DomViewer();
251}
252
253FunctionPass *llvm::createDomOnlyViewerPass() {
254 return new DomOnlyViewer();
255}
256
257FunctionPass *llvm::createPostDomPrinterPass() {
258 return new PostDomPrinter();
259}
260
261FunctionPass *llvm::createPostDomOnlyPrinterPass() {
262 return new PostDomOnlyPrinter();
263}
264
265FunctionPass *llvm::createPostDomViewerPass() {
266 return new PostDomViewer();
267}
268
269FunctionPass *llvm::createPostDomOnlyViewerPass() {
270 return new PostDomOnlyViewer();
271}