1 //===- 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"
22 #include "llvm/Analysis/DOTGraphTraitsPass.h"
23 #include "llvm/Analysis/PostDominators.h"
24 
25 using namespace llvm;
26 
27 namespace llvm {
28 template<>
29 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
30 
DOTGraphTraitsllvm::DOTGraphTraits31   DOTGraphTraits (bool isSimple=false)
32     : DefaultDOTGraphTraits(isSimple) {}
33 
getNodeLabelllvm::DOTGraphTraits34   std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
35 
36     BasicBlock *BB = Node->getBlock();
37 
38     if (!BB)
39       return "Post dominance root node";
40 
41 
42     if (isSimple())
43       return DOTGraphTraits<const Function*>
44         ::getSimpleNodeLabel(BB, BB->getParent());
45     else
46       return DOTGraphTraits<const Function*>
47         ::getCompleteNodeLabel(BB, BB->getParent());
48   }
49 };
50 
51 template<>
52 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
53 
DOTGraphTraitsllvm::DOTGraphTraits54   DOTGraphTraits (bool isSimple=false)
55     : DOTGraphTraits<DomTreeNode*>(isSimple) {}
56 
getGraphNamellvm::DOTGraphTraits57   static std::string getGraphName(DominatorTree *DT) {
58     return "Dominator tree";
59   }
60 
getNodeLabelllvm::DOTGraphTraits61   std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
62     return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
63   }
64 };
65 
66 template<>
67 struct DOTGraphTraits<PostDominatorTree*>
68   : public DOTGraphTraits<DomTreeNode*> {
69 
DOTGraphTraitsllvm::DOTGraphTraits70   DOTGraphTraits (bool isSimple=false)
71     : DOTGraphTraits<DomTreeNode*>(isSimple) {}
72 
getGraphNamellvm::DOTGraphTraits73   static std::string getGraphName(PostDominatorTree *DT) {
74     return "Post dominator tree";
75   }
76 
getNodeLabelllvm::DOTGraphTraits77   std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
78     return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
79   }
80 };
81 }
82 
83 namespace {
84 struct DominatorTreeWrapperPassAnalysisGraphTraits {
getGraph__anonf9991b290111::DominatorTreeWrapperPassAnalysisGraphTraits85   static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {
86     return &DTWP->getDomTree();
87   }
88 };
89 
90 struct DomViewer : public DOTGraphTraitsViewer<
91                        DominatorTreeWrapperPass, false, DominatorTree *,
92                        DominatorTreeWrapperPassAnalysisGraphTraits> {
93   static char ID;
DomViewer__anonf9991b290111::DomViewer94   DomViewer()
95       : DOTGraphTraitsViewer<DominatorTreeWrapperPass, false, DominatorTree *,
96                              DominatorTreeWrapperPassAnalysisGraphTraits>(
97             "dom", ID) {
98     initializeDomViewerPass(*PassRegistry::getPassRegistry());
99   }
100 };
101 
102 struct DomOnlyViewer : public DOTGraphTraitsViewer<
103                            DominatorTreeWrapperPass, true, DominatorTree *,
104                            DominatorTreeWrapperPassAnalysisGraphTraits> {
105   static char ID;
DomOnlyViewer__anonf9991b290111::DomOnlyViewer106   DomOnlyViewer()
107       : DOTGraphTraitsViewer<DominatorTreeWrapperPass, true, DominatorTree *,
108                              DominatorTreeWrapperPassAnalysisGraphTraits>(
109             "domonly", ID) {
110     initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
111   }
112 };
113 
114 struct PostDominatorTreeWrapperPassAnalysisGraphTraits {
getGraph__anonf9991b290111::PostDominatorTreeWrapperPassAnalysisGraphTraits115   static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
116     return &PDTWP->getPostDomTree();
117   }
118 };
119 
120 struct PostDomViewer : public DOTGraphTraitsViewer<
121                           PostDominatorTreeWrapperPass, false,
122                           PostDominatorTree *,
123                           PostDominatorTreeWrapperPassAnalysisGraphTraits> {
124   static char ID;
PostDomViewer__anonf9991b290111::PostDomViewer125   PostDomViewer() :
126     DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, false,
127                          PostDominatorTree *,
128                          PostDominatorTreeWrapperPassAnalysisGraphTraits>(
129         "postdom", ID){
130       initializePostDomViewerPass(*PassRegistry::getPassRegistry());
131     }
132 };
133 
134 struct PostDomOnlyViewer : public DOTGraphTraitsViewer<
135                             PostDominatorTreeWrapperPass, true,
136                             PostDominatorTree *,
137                             PostDominatorTreeWrapperPassAnalysisGraphTraits> {
138   static char ID;
PostDomOnlyViewer__anonf9991b290111::PostDomOnlyViewer139   PostDomOnlyViewer() :
140     DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, true,
141                          PostDominatorTree *,
142                          PostDominatorTreeWrapperPassAnalysisGraphTraits>(
143         "postdomonly", ID){
144       initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
145     }
146 };
147 } // end anonymous namespace
148 
149 char DomViewer::ID = 0;
150 INITIALIZE_PASS(DomViewer, "view-dom",
151                 "View dominance tree of function", false, false)
152 
153 char DomOnlyViewer::ID = 0;
154 INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
155                 "View dominance tree of function (with no function bodies)",
156                 false, false)
157 
158 char PostDomViewer::ID = 0;
159 INITIALIZE_PASS(PostDomViewer, "view-postdom",
160                 "View postdominance tree of function", false, false)
161 
162 char PostDomOnlyViewer::ID = 0;
163 INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
164                 "View postdominance tree of function "
165                 "(with no function bodies)",
166                 false, false)
167 
168 namespace {
169 struct DomPrinter : public DOTGraphTraitsPrinter<
170                         DominatorTreeWrapperPass, false, DominatorTree *,
171                         DominatorTreeWrapperPassAnalysisGraphTraits> {
172   static char ID;
DomPrinter__anonf9991b290211::DomPrinter173   DomPrinter()
174       : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, false, DominatorTree *,
175                               DominatorTreeWrapperPassAnalysisGraphTraits>(
176             "dom", ID) {
177     initializeDomPrinterPass(*PassRegistry::getPassRegistry());
178   }
179 };
180 
181 struct DomOnlyPrinter : public DOTGraphTraitsPrinter<
182                             DominatorTreeWrapperPass, true, DominatorTree *,
183                             DominatorTreeWrapperPassAnalysisGraphTraits> {
184   static char ID;
DomOnlyPrinter__anonf9991b290211::DomOnlyPrinter185   DomOnlyPrinter()
186       : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, true, DominatorTree *,
187                               DominatorTreeWrapperPassAnalysisGraphTraits>(
188             "domonly", ID) {
189     initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
190   }
191 };
192 
193 struct PostDomPrinter
194   : public DOTGraphTraitsPrinter<
195                             PostDominatorTreeWrapperPass, false,
196                             PostDominatorTree *,
197                             PostDominatorTreeWrapperPassAnalysisGraphTraits> {
198   static char ID;
PostDomPrinter__anonf9991b290211::PostDomPrinter199   PostDomPrinter() :
200     DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, false,
201                           PostDominatorTree *,
202                           PostDominatorTreeWrapperPassAnalysisGraphTraits>(
203         "postdom", ID) {
204       initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
205     }
206 };
207 
208 struct PostDomOnlyPrinter
209   : public DOTGraphTraitsPrinter<
210                             PostDominatorTreeWrapperPass, true,
211                             PostDominatorTree *,
212                             PostDominatorTreeWrapperPassAnalysisGraphTraits> {
213   static char ID;
PostDomOnlyPrinter__anonf9991b290211::PostDomOnlyPrinter214   PostDomOnlyPrinter() :
215     DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, true,
216                           PostDominatorTree *,
217                           PostDominatorTreeWrapperPassAnalysisGraphTraits>(
218         "postdomonly", ID) {
219       initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
220     }
221 };
222 } // end anonymous namespace
223 
224 
225 
226 char DomPrinter::ID = 0;
227 INITIALIZE_PASS(DomPrinter, "dot-dom",
228                 "Print dominance tree of function to 'dot' file",
229                 false, false)
230 
231 char DomOnlyPrinter::ID = 0;
232 INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
233                 "Print dominance tree of function to 'dot' file "
234                 "(with no function bodies)",
235                 false, false)
236 
237 char PostDomPrinter::ID = 0;
238 INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
239                 "Print postdominance tree of function to 'dot' file",
240                 false, false)
241 
242 char PostDomOnlyPrinter::ID = 0;
243 INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
244                 "Print postdominance tree of function to 'dot' file "
245                 "(with no function bodies)",
246                 false, false)
247 
248 // 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 
createDomPrinterPass()252 FunctionPass *llvm::createDomPrinterPass() {
253   return new DomPrinter();
254 }
255 
createDomOnlyPrinterPass()256 FunctionPass *llvm::createDomOnlyPrinterPass() {
257   return new DomOnlyPrinter();
258 }
259 
createDomViewerPass()260 FunctionPass *llvm::createDomViewerPass() {
261   return new DomViewer();
262 }
263 
createDomOnlyViewerPass()264 FunctionPass *llvm::createDomOnlyViewerPass() {
265   return new DomOnlyViewer();
266 }
267 
createPostDomPrinterPass()268 FunctionPass *llvm::createPostDomPrinterPass() {
269   return new PostDomPrinter();
270 }
271 
createPostDomOnlyPrinterPass()272 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
273   return new PostDomOnlyPrinter();
274 }
275 
createPostDomViewerPass()276 FunctionPass *llvm::createPostDomViewerPass() {
277   return new PostDomViewer();
278 }
279 
createPostDomOnlyViewerPass()280 FunctionPass *llvm::createPostDomOnlyViewerPass() {
281   return new PostDomOnlyViewer();
282 }
283