blob: 0c880df54f8e9fdb5c7f1ccc605673ae740e4b33 [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};
81}
82
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
114struct PostDomViewer
Tobias Grosser53da3f82010-01-16 10:56:41 +0000115 : public DOTGraphTraitsViewer<PostDominatorTree, false> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000116 static char ID;
117 PostDomViewer() :
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000118 DOTGraphTraitsViewer<PostDominatorTree, false>("postdom", ID){
119 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
120 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000121};
122
123struct PostDomOnlyViewer
Tobias Grosser53da3f82010-01-16 10:56:41 +0000124 : public DOTGraphTraitsViewer<PostDominatorTree, true> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000125 static char ID;
126 PostDomOnlyViewer() :
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000127 DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", ID){
128 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
129 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000130};
131} // end anonymous namespace
132
133char DomViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000134INITIALIZE_PASS(DomViewer, "view-dom",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000135 "View dominance tree of function", false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000136
137char DomOnlyViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000138INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
139 "View dominance tree of function (with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000140 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000141
142char PostDomViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000143INITIALIZE_PASS(PostDomViewer, "view-postdom",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000144 "View postdominance tree of function", false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000145
146char PostDomOnlyViewer::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000147INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
148 "View postdominance tree of function "
149 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000150 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000151
152namespace {
Chandler Carruth73523022014-01-13 13:07:17 +0000153struct DomPrinter : public DOTGraphTraitsPrinter<
154 DominatorTreeWrapperPass, false, DominatorTree *,
155 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000156 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000157 DomPrinter()
158 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, false, DominatorTree *,
159 DominatorTreeWrapperPassAnalysisGraphTraits>(
160 "dom", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000161 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
162 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000163};
164
Chandler Carruth73523022014-01-13 13:07:17 +0000165struct DomOnlyPrinter : public DOTGraphTraitsPrinter<
166 DominatorTreeWrapperPass, true, DominatorTree *,
167 DominatorTreeWrapperPassAnalysisGraphTraits> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000168 static char ID;
Chandler Carruth73523022014-01-13 13:07:17 +0000169 DomOnlyPrinter()
170 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, true, DominatorTree *,
171 DominatorTreeWrapperPassAnalysisGraphTraits>(
172 "domonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000173 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
174 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000175};
176
177struct PostDomPrinter
Tobias Grosser53da3f82010-01-16 10:56:41 +0000178 : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000179 static char ID;
180 PostDomPrinter() :
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000181 DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", ID) {
182 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
183 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000184};
185
186struct PostDomOnlyPrinter
Tobias Grosser53da3f82010-01-16 10:56:41 +0000187 : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
Chris Lattner9e6882c2009-10-18 04:10:40 +0000188 static char ID;
189 PostDomOnlyPrinter() :
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000190 DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", ID) {
191 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
192 }
Chris Lattner9e6882c2009-10-18 04:10:40 +0000193};
194} // end anonymous namespace
195
196
197
198char DomPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000199INITIALIZE_PASS(DomPrinter, "dot-dom",
200 "Print dominance tree of function to 'dot' file",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000201 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000202
203char DomOnlyPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000204INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
205 "Print dominance tree of function to 'dot' file "
206 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000207 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000208
209char PostDomPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000210INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
211 "Print postdominance tree of function to 'dot' file",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000212 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000213
214char PostDomOnlyPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000215INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
216 "Print postdominance tree of function to 'dot' file "
217 "(with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000218 false, false)
Chris Lattner9e6882c2009-10-18 04:10:40 +0000219
Chris Lattner9e6882c2009-10-18 04:10:40 +0000220// Create methods available outside of this file, to use them
221// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
222// the link time optimization.
223
224FunctionPass *llvm::createDomPrinterPass() {
225 return new DomPrinter();
226}
227
228FunctionPass *llvm::createDomOnlyPrinterPass() {
229 return new DomOnlyPrinter();
230}
231
232FunctionPass *llvm::createDomViewerPass() {
233 return new DomViewer();
234}
235
236FunctionPass *llvm::createDomOnlyViewerPass() {
237 return new DomOnlyViewer();
238}
239
240FunctionPass *llvm::createPostDomPrinterPass() {
241 return new PostDomPrinter();
242}
243
244FunctionPass *llvm::createPostDomOnlyPrinterPass() {
245 return new PostDomOnlyPrinter();
246}
247
248FunctionPass *llvm::createPostDomViewerPass() {
249 return new PostDomViewer();
250}
251
252FunctionPass *llvm::createPostDomOnlyViewerPass() {
253 return new PostDomOnlyViewer();
254}