blob: d9f43dd746efb7c2c7a4fcaf8207fb087489ad87 [file] [log] [blame]
Chris Lattner9e6882c2009-10-18 04:10:40 +00001//===- DomPrinter.cpp - DOT printer for the dominance trees ------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner9e6882c2009-10-18 04:10:40 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines '-dot-dom' and '-dot-postdom' analysis passes, which emit
10// a dom.<fnname>.dot or postdom.<fnname>.dot file for each function in the
11// program, with a graph of the dominance/postdominance tree of that
12// function.
13//
14// There are also passes available to directly call dotty ('-view-dom' or
15// '-view-postdom'). By appending '-only' like '-dot-dom-only' only the
16// names of the bbs are printed, but the content is hidden.
17//
18//===----------------------------------------------------------------------===//
19
20#include "llvm/Analysis/DomPrinter.h"
Tobias Grosser53da3f82010-01-16 10:56:41 +000021#include "llvm/Analysis/DOTGraphTraitsPass.h"
Chris Lattner9e6882c2009-10-18 04:10:40 +000022#include "llvm/Analysis/PostDominators.h"
23
24using namespace llvm;
25
26namespace llvm {
27template<>
28struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
Tobias Grosser90d33402009-11-30 12:38:13 +000029
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000030 DOTGraphTraits (bool isSimple=false)
31 : DefaultDOTGraphTraits(isSimple) {}
Tobias Grosser90d33402009-11-30 12:38:13 +000032
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000033 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
Chris Lattner9e6882c2009-10-18 04:10:40 +000034
35 BasicBlock *BB = Node->getBlock();
36
37 if (!BB)
38 return "Post dominance root node";
39
Sean Fertilecd0d7632018-06-29 17:48:58 +000040
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000041 if (isSimple())
Sean Fertilecd0d7632018-06-29 17:48:58 +000042 return DOTGraphTraits<const Function*>
43 ::getSimpleNodeLabel(BB, BB->getParent());
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000044 else
Sean Fertilecd0d7632018-06-29 17:48:58 +000045 return DOTGraphTraits<const Function*>
46 ::getCompleteNodeLabel(BB, BB->getParent());
Chris Lattner9e6882c2009-10-18 04:10:40 +000047 }
48};
49
50template<>
51struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
52
Tobias Grosser90d33402009-11-30 12:38:13 +000053 DOTGraphTraits (bool isSimple=false)
54 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
55
Chris Lattner9e6882c2009-10-18 04:10:40 +000056 static std::string getGraphName(DominatorTree *DT) {
57 return "Dominator tree";
58 }
59
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000060 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
61 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattner9e6882c2009-10-18 04:10:40 +000062 }
63};
64
65template<>
66struct DOTGraphTraits<PostDominatorTree*>
67 : public DOTGraphTraits<DomTreeNode*> {
Tobias Grosser90d33402009-11-30 12:38:13 +000068
69 DOTGraphTraits (bool isSimple=false)
70 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
71
Chris Lattner9e6882c2009-10-18 04:10:40 +000072 static std::string getGraphName(PostDominatorTree *DT) {
73 return "Post dominator tree";
74 }
Tobias Grosserdd7f2e72009-11-30 12:38:47 +000075
76 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
77 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattner9e6882c2009-10-18 04:10:40 +000078 }
79};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000080}
Chris Lattner9e6882c2009-10-18 04:10:40 +000081
Davide Italianoebd77642017-04-24 17:48:44 +000082void DominatorTree::viewGraph(const Twine &Name, const Twine &Title) {
83#ifndef NDEBUG
84 ViewGraph(this, Name, false, Title);
85#else
86 errs() << "DomTree dump not available, build with DEBUG\n";
87#endif // NDEBUG
88}
89
90void DominatorTree::viewGraph() {
91#ifndef NDEBUG
92 this->viewGraph("domtree", "Dominator Tree for function");
93#else
94 errs() << "DomTree dump not available, build with DEBUG\n";
95#endif // NDEBUG
96}
97
Chris Lattner9e6882c2009-10-18 04:10:40 +000098namespace {
Chandler Carruth73523022014-01-13 13:07:17 +000099struct DominatorTreeWrapperPassAnalysisGraphTraits {
100 static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {
101 return &DTWP->getDomTree();
102 }
103};
104
105struct DomViewer : public DOTGraphTraitsViewer<
106 DominatorTreeWrapperPass, false, DominatorTree *,
107 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000108 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000109 DomViewer()
110 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, false, DominatorTree *,
111 DominatorTreeWrapperPassAnalysisGraphTraits>(
112 "dom", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000113 initializeDomViewerPass(*PassRegistry::getPassRegistry());
114 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000115};
116
Chandler Carruth73523022014-01-13 13:07:17 +0000117struct DomOnlyViewer : public DOTGraphTraitsViewer<
118 DominatorTreeWrapperPass, true, DominatorTree *,
119 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000120 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000121 DomOnlyViewer()
122 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, true, DominatorTree *,
123 DominatorTreeWrapperPassAnalysisGraphTraits>(
124 "domonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000125 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
126 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000127};
128
Hongbin Zheng3f978402016-02-25 17:54:07 +0000129struct PostDominatorTreeWrapperPassAnalysisGraphTraits {
130 static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
131 return &PDTWP->getPostDomTree();
132 }
133};
134
135struct PostDomViewer : public DOTGraphTraitsViewer<
136 PostDominatorTreeWrapperPass, false,
137 PostDominatorTree *,
138 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000139 static char ID;
140 PostDomViewer() :
Hongbin Zheng3f978402016-02-25 17:54:07 +0000141 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, false,
142 PostDominatorTree *,
143 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
144 "postdom", ID){
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000145 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
146 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000147};
148
Hongbin Zheng3f978402016-02-25 17:54:07 +0000149struct PostDomOnlyViewer : public DOTGraphTraitsViewer<
150 PostDominatorTreeWrapperPass, true,
151 PostDominatorTree *,
152 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000153 static char ID;
154 PostDomOnlyViewer() :
Hongbin Zheng3f978402016-02-25 17:54:07 +0000155 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, true,
156 PostDominatorTree *,
157 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
158 "postdomonly", ID){
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000159 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
160 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000161};
162} // end anonymous namespace
163
164char DomViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000165INITIALIZE_PASS(DomViewer, "view-dom",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000166 "View dominance tree of function", false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000167
168char DomOnlyViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000169INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
170 "View dominance tree of function (with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000171 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000172
173char PostDomViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000174INITIALIZE_PASS(PostDomViewer, "view-postdom",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000175 "View postdominance tree of function", false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000176
177char PostDomOnlyViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000178INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
179 "View postdominance tree of function "
180 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000181 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000182
183namespace {
Chandler Carruth73523022014-01-13 13:07:17 +0000184struct DomPrinter : public DOTGraphTraitsPrinter<
185 DominatorTreeWrapperPass, false, DominatorTree *,
186 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000187 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000188 DomPrinter()
189 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, false, DominatorTree *,
190 DominatorTreeWrapperPassAnalysisGraphTraits>(
191 "dom", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000192 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
193 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000194};
195
Chandler Carruth73523022014-01-13 13:07:17 +0000196struct DomOnlyPrinter : public DOTGraphTraitsPrinter<
197 DominatorTreeWrapperPass, true, DominatorTree *,
198 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000199 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000200 DomOnlyPrinter()
201 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, true, DominatorTree *,
202 DominatorTreeWrapperPassAnalysisGraphTraits>(
203 "domonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000204 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
205 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000206};
207
208struct PostDomPrinter
Hongbin Zheng3f978402016-02-25 17:54:07 +0000209 : public DOTGraphTraitsPrinter<
210 PostDominatorTreeWrapperPass, false,
211 PostDominatorTree *,
212 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000213 static char ID;
214 PostDomPrinter() :
Hongbin Zheng3f978402016-02-25 17:54:07 +0000215 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, false,
216 PostDominatorTree *,
217 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
218 "postdom", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000219 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
220 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000221};
222
223struct PostDomOnlyPrinter
Hongbin Zheng3f978402016-02-25 17:54:07 +0000224 : public DOTGraphTraitsPrinter<
225 PostDominatorTreeWrapperPass, true,
226 PostDominatorTree *,
227 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000228 static char ID;
229 PostDomOnlyPrinter() :
Hongbin Zheng3f978402016-02-25 17:54:07 +0000230 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, true,
231 PostDominatorTree *,
232 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
233 "postdomonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000234 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
235 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000236};
237} // end anonymous namespace
238
239
240
241char DomPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000242INITIALIZE_PASS(DomPrinter, "dot-dom",
243 "Print dominance tree of function to 'dot' file",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000244 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000245
246char DomOnlyPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000247INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
248 "Print dominance tree of function to 'dot' file "
249 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000250 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000251
252char PostDomPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000253INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
254 "Print postdominance tree of function to 'dot' file",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000255 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000256
257char PostDomOnlyPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000258INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
259 "Print postdominance tree of function to 'dot' file "
260 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000261 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000262
Chris Lattner9e6882c2009-10-18 04:10:40 +0000263// Create methods available outside of this file, to use them
264// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
265// the link time optimization.
266
267FunctionPass *llvm::createDomPrinterPass() {
268 return new DomPrinter();
269}
270
271FunctionPass *llvm::createDomOnlyPrinterPass() {
272 return new DomOnlyPrinter();
273}
274
275FunctionPass *llvm::createDomViewerPass() {
276 return new DomViewer();
277}
278
279FunctionPass *llvm::createDomOnlyViewerPass() {
280 return new DomOnlyViewer();
281}
282
283FunctionPass *llvm::createPostDomPrinterPass() {
284 return new PostDomPrinter();
285}
286
287FunctionPass *llvm::createPostDomOnlyPrinterPass() {
288 return new PostDomOnlyPrinter();
289}
290
291FunctionPass *llvm::createPostDomViewerPass() {
292 return new PostDomViewer();
293}
294
295FunctionPass *llvm::createPostDomOnlyViewerPass() {
296 return new PostDomOnlyViewer();
297}