blob: 8abc0e7d0df957fbc74375f109023645a60ce2d0 [file] [log] [blame]
Chris Lattnerb839c552009-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 Grosser23279f12010-01-16 10:56:41 +000022#include "llvm/Analysis/DOTGraphTraitsPass.h"
Chris Lattnerb839c552009-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 Grossera10d5982009-11-30 12:38:13 +000030
Tobias Grosser56f4ef32009-11-30 12:38:47 +000031 DOTGraphTraits (bool isSimple=false)
32 : DefaultDOTGraphTraits(isSimple) {}
Tobias Grossera10d5982009-11-30 12:38:13 +000033
Tobias Grosser56f4ef32009-11-30 12:38:47 +000034 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
Chris Lattnerb839c552009-10-18 04:10:40 +000035
36 BasicBlock *BB = Node->getBlock();
37
38 if (!BB)
39 return "Post dominance root node";
40
Sean Fertile551913f2018-06-29 17:48:58 +000041
Tobias Grosser56f4ef32009-11-30 12:38:47 +000042 if (isSimple())
Sean Fertile551913f2018-06-29 17:48:58 +000043 return DOTGraphTraits<const Function*>
44 ::getSimpleNodeLabel(BB, BB->getParent());
Tobias Grosser56f4ef32009-11-30 12:38:47 +000045 else
Sean Fertile551913f2018-06-29 17:48:58 +000046 return DOTGraphTraits<const Function*>
47 ::getCompleteNodeLabel(BB, BB->getParent());
Chris Lattnerb839c552009-10-18 04:10:40 +000048 }
49};
50
51template<>
52struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
53
Tobias Grossera10d5982009-11-30 12:38:13 +000054 DOTGraphTraits (bool isSimple=false)
55 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
56
Chris Lattnerb839c552009-10-18 04:10:40 +000057 static std::string getGraphName(DominatorTree *DT) {
58 return "Dominator tree";
59 }
60
Tobias Grosser56f4ef32009-11-30 12:38:47 +000061 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
62 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattnerb839c552009-10-18 04:10:40 +000063 }
64};
65
66template<>
67struct DOTGraphTraits<PostDominatorTree*>
68 : public DOTGraphTraits<DomTreeNode*> {
Tobias Grossera10d5982009-11-30 12:38:13 +000069
70 DOTGraphTraits (bool isSimple=false)
71 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
72
Chris Lattnerb839c552009-10-18 04:10:40 +000073 static std::string getGraphName(PostDominatorTree *DT) {
74 return "Post dominator tree";
75 }
Tobias Grosser56f4ef32009-11-30 12:38:47 +000076
77 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
78 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattnerb839c552009-10-18 04:10:40 +000079 }
80};
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +000081}
Chris Lattnerb839c552009-10-18 04:10:40 +000082
Davide Italianoc1808792017-04-24 17:48:44 +000083void DominatorTree::viewGraph(const Twine &Name, const Twine &Title) {
84#ifndef NDEBUG
85 ViewGraph(this, Name, false, Title);
86#else
87 errs() << "DomTree dump not available, build with DEBUG\n";
88#endif // NDEBUG
89}
90
91void DominatorTree::viewGraph() {
92#ifndef NDEBUG
93 this->viewGraph("domtree", "Dominator Tree for function");
94#else
95 errs() << "DomTree dump not available, build with DEBUG\n";
96#endif // NDEBUG
97}
98
Chris Lattnerb839c552009-10-18 04:10:40 +000099namespace {
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000100struct DominatorTreeWrapperPassAnalysisGraphTraits {
101 static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {
102 return &DTWP->getDomTree();
103 }
104};
105
106struct DomViewer : public DOTGraphTraitsViewer<
107 DominatorTreeWrapperPass, false, DominatorTree *,
108 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000109 static char ID;
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000110 DomViewer()
111 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, false, DominatorTree *,
112 DominatorTreeWrapperPassAnalysisGraphTraits>(
113 "dom", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000114 initializeDomViewerPass(*PassRegistry::getPassRegistry());
115 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000116};
117
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000118struct DomOnlyViewer : public DOTGraphTraitsViewer<
119 DominatorTreeWrapperPass, true, DominatorTree *,
120 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000121 static char ID;
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000122 DomOnlyViewer()
123 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, true, DominatorTree *,
124 DominatorTreeWrapperPassAnalysisGraphTraits>(
125 "domonly", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000126 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
127 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000128};
129
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000130struct PostDominatorTreeWrapperPassAnalysisGraphTraits {
131 static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
132 return &PDTWP->getPostDomTree();
133 }
134};
135
136struct PostDomViewer : public DOTGraphTraitsViewer<
137 PostDominatorTreeWrapperPass, false,
138 PostDominatorTree *,
139 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000140 static char ID;
141 PostDomViewer() :
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000142 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, false,
143 PostDominatorTree *,
144 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
145 "postdom", ID){
Owen Anderson081c34b2010-10-19 17:21:58 +0000146 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
147 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000148};
149
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000150struct PostDomOnlyViewer : public DOTGraphTraitsViewer<
151 PostDominatorTreeWrapperPass, true,
152 PostDominatorTree *,
153 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000154 static char ID;
155 PostDomOnlyViewer() :
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000156 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, true,
157 PostDominatorTree *,
158 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
159 "postdomonly", ID){
Owen Anderson081c34b2010-10-19 17:21:58 +0000160 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
161 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000162};
163} // end anonymous namespace
164
165char DomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000166INITIALIZE_PASS(DomViewer, "view-dom",
Owen Andersonce665bd2010-10-07 22:25:06 +0000167 "View dominance tree of function", false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000168
169char DomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000170INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
171 "View dominance tree of function (with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000172 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000173
174char PostDomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000175INITIALIZE_PASS(PostDomViewer, "view-postdom",
Owen Andersonce665bd2010-10-07 22:25:06 +0000176 "View postdominance tree of function", false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000177
178char PostDomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000179INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
180 "View postdominance tree of function "
181 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000182 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000183
184namespace {
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000185struct DomPrinter : public DOTGraphTraitsPrinter<
186 DominatorTreeWrapperPass, false, DominatorTree *,
187 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000188 static char ID;
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000189 DomPrinter()
190 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, false, DominatorTree *,
191 DominatorTreeWrapperPassAnalysisGraphTraits>(
192 "dom", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000193 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
194 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000195};
196
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000197struct DomOnlyPrinter : public DOTGraphTraitsPrinter<
198 DominatorTreeWrapperPass, true, DominatorTree *,
199 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000200 static char ID;
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000201 DomOnlyPrinter()
202 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, true, DominatorTree *,
203 DominatorTreeWrapperPassAnalysisGraphTraits>(
204 "domonly", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000205 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
206 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000207};
208
209struct PostDomPrinter
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000210 : public DOTGraphTraitsPrinter<
211 PostDominatorTreeWrapperPass, false,
212 PostDominatorTree *,
213 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000214 static char ID;
215 PostDomPrinter() :
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000216 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, false,
217 PostDominatorTree *,
218 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
219 "postdom", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000220 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
221 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000222};
223
224struct PostDomOnlyPrinter
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000225 : public DOTGraphTraitsPrinter<
226 PostDominatorTreeWrapperPass, true,
227 PostDominatorTree *,
228 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000229 static char ID;
230 PostDomOnlyPrinter() :
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000231 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, true,
232 PostDominatorTree *,
233 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
234 "postdomonly", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000235 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
236 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000237};
238} // end anonymous namespace
239
240
241
242char DomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000243INITIALIZE_PASS(DomPrinter, "dot-dom",
244 "Print dominance tree of function to 'dot' file",
Owen Andersonce665bd2010-10-07 22:25:06 +0000245 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000246
247char DomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000248INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
249 "Print dominance tree of function to 'dot' file "
250 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000251 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000252
253char PostDomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000254INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
255 "Print postdominance tree of function to 'dot' file",
Owen Andersonce665bd2010-10-07 22:25:06 +0000256 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000257
258char PostDomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000259INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
260 "Print postdominance tree of function to 'dot' file "
261 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000262 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000263
Chris Lattnerb839c552009-10-18 04:10:40 +0000264// Create methods available outside of this file, to use them
265// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
266// the link time optimization.
267
268FunctionPass *llvm::createDomPrinterPass() {
269 return new DomPrinter();
270}
271
272FunctionPass *llvm::createDomOnlyPrinterPass() {
273 return new DomOnlyPrinter();
274}
275
276FunctionPass *llvm::createDomViewerPass() {
277 return new DomViewer();
278}
279
280FunctionPass *llvm::createDomOnlyViewerPass() {
281 return new DomOnlyViewer();
282}
283
284FunctionPass *llvm::createPostDomPrinterPass() {
285 return new PostDomPrinter();
286}
287
288FunctionPass *llvm::createPostDomOnlyPrinterPass() {
289 return new PostDomOnlyPrinter();
290}
291
292FunctionPass *llvm::createPostDomViewerPass() {
293 return new PostDomViewer();
294}
295
296FunctionPass *llvm::createPostDomOnlyViewerPass() {
297 return new PostDomOnlyViewer();
298}