blob: 7acfb41500d4ece12593f90401c77df694b84a6d [file] [log] [blame]
Chris Lattner9e6882c2009-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"
Tobias Grosser53da3f82010-01-16 10:56:41 +000022#include "llvm/Analysis/DOTGraphTraitsPass.h"
Chris Lattner9e6882c2009-10-18 04:10:40 +000023#include "llvm/Analysis/PostDominators.h"
24
25using namespace llvm;
26
27namespace llvm {
28template<>
29struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
Tobias Grosser90d33402009-11-30 12:38:13 +000030
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000031 DOTGraphTraits (bool isSimple=false)
32 : DefaultDOTGraphTraits(isSimple) {}
Tobias Grosser90d33402009-11-30 12:38:13 +000033
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000034 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
Chris Lattner9e6882c2009-10-18 04:10:40 +000035
36 BasicBlock *BB = Node->getBlock();
37
38 if (!BB)
39 return "Post dominance root node";
40
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000041
42 if (isSimple())
43 return DOTGraphTraits<const Function*>
Duncan Sands41b4a6b2010-07-12 08:16:59 +000044 ::getSimpleNodeLabel(BB, BB->getParent());
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000045 else
46 return DOTGraphTraits<const Function*>
Duncan Sands41b4a6b2010-07-12 08:16:59 +000047 ::getCompleteNodeLabel(BB, BB->getParent());
Chris Lattner9e6882c2009-10-18 04:10:40 +000048 }
49};
50
51template<>
52struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
53
Tobias Grosser90d33402009-11-30 12:38:13 +000054 DOTGraphTraits (bool isSimple=false)
55 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
56
Chris Lattner9e6882c2009-10-18 04:10:40 +000057 static std::string getGraphName(DominatorTree *DT) {
58 return "Dominator tree";
59 }
60
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000061 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
62 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattner9e6882c2009-10-18 04:10:40 +000063 }
64};
65
66template<>
67struct DOTGraphTraits<PostDominatorTree*>
68 : public DOTGraphTraits<DomTreeNode*> {
Tobias Grosser90d33402009-11-30 12:38:13 +000069
70 DOTGraphTraits (bool isSimple=false)
71 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
72
Chris Lattner9e6882c2009-10-18 04:10:40 +000073 static std::string getGraphName(PostDominatorTree *DT) {
74 return "Post dominator tree";
75 }
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000076
77 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
78 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattner9e6882c2009-10-18 04:10:40 +000079 }
80};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000081}
Chris Lattner9e6882c2009-10-18 04:10:40 +000082
83namespace {
Chandler Carruth73523022014-01-13 13:07:17 +000084struct DominatorTreeWrapperPassAnalysisGraphTraits {
85 static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {
86 return &DTWP->getDomTree();
87 }
88};
89
90struct DomViewer : public DOTGraphTraitsViewer<
91 DominatorTreeWrapperPass, false, DominatorTree *,
92 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +000093 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +000094 DomViewer()
95 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, false, DominatorTree *,
96 DominatorTreeWrapperPassAnalysisGraphTraits>(
97 "dom", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000098 initializeDomViewerPass(*PassRegistry::getPassRegistry());
99 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000100};
101
Chandler Carruth73523022014-01-13 13:07:17 +0000102struct DomOnlyViewer : public DOTGraphTraitsViewer<
103 DominatorTreeWrapperPass, true, DominatorTree *,
104 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000105 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000106 DomOnlyViewer()
107 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, true, DominatorTree *,
108 DominatorTreeWrapperPassAnalysisGraphTraits>(
109 "domonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000110 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
111 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000112};
113
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000114struct PostDominatorTreeWrapperPassAnalysisGraphTraits {
115 static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
116 return &PDTWP->getPostDomTree();
117 }
118};
119
120struct PostDomViewer : public DOTGraphTraitsViewer<
121 PostDominatorTreeWrapperPass, false,
122 PostDominatorTree *,
123 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000124 static char ID;
125 PostDomViewer() :
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000126 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, false,
127 PostDominatorTree *,
128 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
129 "postdom", ID){
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000130 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
131 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000132};
133
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000134struct PostDomOnlyViewer : public DOTGraphTraitsViewer<
135 PostDominatorTreeWrapperPass, true,
136 PostDominatorTree *,
137 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000138 static char ID;
139 PostDomOnlyViewer() :
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000140 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, true,
141 PostDominatorTree *,
142 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
143 "postdomonly", ID){
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000144 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
145 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000146};
147} // end anonymous namespace
148
149char DomViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000150INITIALIZE_PASS(DomViewer, "view-dom",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000151 "View dominance tree of function", false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000152
153char DomOnlyViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000154INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
155 "View dominance tree of function (with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000156 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000157
158char PostDomViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000159INITIALIZE_PASS(PostDomViewer, "view-postdom",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000160 "View postdominance tree of function", false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000161
162char PostDomOnlyViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000163INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
164 "View postdominance tree of function "
165 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000166 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000167
168namespace {
Chandler Carruth73523022014-01-13 13:07:17 +0000169struct DomPrinter : public DOTGraphTraitsPrinter<
170 DominatorTreeWrapperPass, false, DominatorTree *,
171 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000172 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000173 DomPrinter()
174 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, false, DominatorTree *,
175 DominatorTreeWrapperPassAnalysisGraphTraits>(
176 "dom", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000177 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
178 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000179};
180
Chandler Carruth73523022014-01-13 13:07:17 +0000181struct DomOnlyPrinter : public DOTGraphTraitsPrinter<
182 DominatorTreeWrapperPass, true, DominatorTree *,
183 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000184 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000185 DomOnlyPrinter()
186 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, true, DominatorTree *,
187 DominatorTreeWrapperPassAnalysisGraphTraits>(
188 "domonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000189 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
190 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000191};
192
193struct PostDomPrinter
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000194 : public DOTGraphTraitsPrinter<
195 PostDominatorTreeWrapperPass, false,
196 PostDominatorTree *,
197 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000198 static char ID;
199 PostDomPrinter() :
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000200 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, false,
201 PostDominatorTree *,
202 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
203 "postdom", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000204 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
205 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000206};
207
208struct PostDomOnlyPrinter
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000209 : public DOTGraphTraitsPrinter<
210 PostDominatorTreeWrapperPass, true,
211 PostDominatorTree *,
212 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000213 static char ID;
214 PostDomOnlyPrinter() :
Hongbin Zhenga0273a02016-02-25 16:33:06 +0000215 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, true,
216 PostDominatorTree *,
217 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
218 "postdomonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000219 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
220 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000221};
222} // end anonymous namespace
223
224
225
226char DomPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000227INITIALIZE_PASS(DomPrinter, "dot-dom",
228 "Print dominance tree of function to 'dot' file",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000229 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000230
231char DomOnlyPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000232INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
233 "Print dominance tree of function to 'dot' file "
234 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000235 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000236
237char PostDomPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000238INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
239 "Print postdominance tree of function to 'dot' file",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000240 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000241
242char PostDomOnlyPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000243INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
244 "Print postdominance tree of function to 'dot' file "
245 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000246 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000247
Chris Lattner9e6882c2009-10-18 04:10:40 +0000248// Create methods available outside of this file, to use them
249// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
250// the link time optimization.
251
252FunctionPass *llvm::createDomPrinterPass() {
253 return new DomPrinter();
254}
255
256FunctionPass *llvm::createDomOnlyPrinterPass() {
257 return new DomOnlyPrinter();
258}
259
260FunctionPass *llvm::createDomViewerPass() {
261 return new DomViewer();
262}
263
264FunctionPass *llvm::createDomOnlyViewerPass() {
265 return new DomOnlyViewer();
266}
267
268FunctionPass *llvm::createPostDomPrinterPass() {
269 return new PostDomPrinter();
270}
271
272FunctionPass *llvm::createPostDomOnlyPrinterPass() {
273 return new PostDomOnlyPrinter();
274}
275
276FunctionPass *llvm::createPostDomViewerPass() {
277 return new PostDomViewer();
278}
279
280FunctionPass *llvm::createPostDomOnlyViewerPass() {
281 return new PostDomOnlyViewer();
282}