1 //===- DomPrinter.cpp - DOT printer for the dominance trees ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Analysis/DomPrinter.h"
22 #include "llvm/Analysis/DOTGraphTraitsPass.h"
23 #include "llvm/Analysis/PostDominators.h"
29 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
31 DOTGraphTraits (bool isSimple=false)
32 : DefaultDOTGraphTraits(isSimple) {}
34 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
36 BasicBlock *BB = Node->getBlock();
39 return "Post dominance root node";
43 return DOTGraphTraits<const Function*>
44 ::getSimpleNodeLabel(BB, BB->getParent());
46 return DOTGraphTraits<const Function*>
47 ::getCompleteNodeLabel(BB, BB->getParent());
52 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
54 DOTGraphTraits (bool isSimple=false)
55 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
57 static std::string getGraphName(DominatorTree *DT) {
58 return "Dominator tree";
61 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
62 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
67 struct DOTGraphTraits<PostDominatorTree*>
68 : public DOTGraphTraits<DomTreeNode*> {
70 DOTGraphTraits (bool isSimple=false)
71 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
73 static std::string getGraphName(PostDominatorTree *DT) {
74 return "Post dominator tree";
77 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
78 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
84 struct DominatorTreeWrapperPassAnalysisGraphTraits {
85 static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {
86 return &DTWP->getDomTree();
90 struct DomViewer : public DOTGraphTraitsViewer<
91 DominatorTreeWrapperPass, false, DominatorTree *,
92 DominatorTreeWrapperPassAnalysisGraphTraits> {
95 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, false, DominatorTree *,
96 DominatorTreeWrapperPassAnalysisGraphTraits>(
98 initializeDomViewerPass(*PassRegistry::getPassRegistry());
102 struct DomOnlyViewer : public DOTGraphTraitsViewer<
103 DominatorTreeWrapperPass, true, DominatorTree *,
104 DominatorTreeWrapperPassAnalysisGraphTraits> {
107 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, true, DominatorTree *,
108 DominatorTreeWrapperPassAnalysisGraphTraits>(
110 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
114 struct PostDominatorTreeWrapperPassAnalysisGraphTraits {
115 static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
116 return &PDTWP->getPostDomTree();
120 struct PostDomViewer : public DOTGraphTraitsViewer<
121 PostDominatorTreeWrapperPass, false,
123 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
126 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, false,
128 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
130 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
134 struct PostDomOnlyViewer : public DOTGraphTraitsViewer<
135 PostDominatorTreeWrapperPass, true,
137 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
139 PostDomOnlyViewer() :
140 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, true,
142 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
144 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
147 } // end anonymous namespace
149 char DomViewer::ID = 0;
150 INITIALIZE_PASS(DomViewer, "view-dom",
151 "View dominance tree of function", false, false)
153 char DomOnlyViewer::ID = 0;
154 INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
155 "View dominance tree of function (with no function bodies)",
158 char PostDomViewer::ID = 0;
159 INITIALIZE_PASS(PostDomViewer, "view-postdom",
160 "View postdominance tree of function", false, false)
162 char PostDomOnlyViewer::ID = 0;
163 INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
164 "View postdominance tree of function "
165 "(with no function bodies)",
169 struct DomPrinter : public DOTGraphTraitsPrinter<
170 DominatorTreeWrapperPass, false, DominatorTree *,
171 DominatorTreeWrapperPassAnalysisGraphTraits> {
174 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, false, DominatorTree *,
175 DominatorTreeWrapperPassAnalysisGraphTraits>(
177 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
181 struct DomOnlyPrinter : public DOTGraphTraitsPrinter<
182 DominatorTreeWrapperPass, true, DominatorTree *,
183 DominatorTreeWrapperPassAnalysisGraphTraits> {
186 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, true, DominatorTree *,
187 DominatorTreeWrapperPassAnalysisGraphTraits>(
189 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
193 struct PostDomPrinter
194 : public DOTGraphTraitsPrinter<
195 PostDominatorTreeWrapperPass, false,
197 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
200 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, false,
202 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
204 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
208 struct PostDomOnlyPrinter
209 : public DOTGraphTraitsPrinter<
210 PostDominatorTreeWrapperPass, true,
212 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
214 PostDomOnlyPrinter() :
215 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, true,
217 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
219 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
222 } // end anonymous namespace
226 char DomPrinter::ID = 0;
227 INITIALIZE_PASS(DomPrinter, "dot-dom",
228 "Print dominance tree of function to 'dot' file",
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)",
237 char PostDomPrinter::ID = 0;
238 INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
239 "Print postdominance tree of function to 'dot' file",
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)",
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.
252 FunctionPass *llvm::createDomPrinterPass() {
253 return new DomPrinter();
256 FunctionPass *llvm::createDomOnlyPrinterPass() {
257 return new DomOnlyPrinter();
260 FunctionPass *llvm::createDomViewerPass() {
261 return new DomViewer();
264 FunctionPass *llvm::createDomOnlyViewerPass() {
265 return new DomOnlyViewer();
268 FunctionPass *llvm::createPostDomPrinterPass() {
269 return new PostDomPrinter();
272 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
273 return new PostDomOnlyPrinter();
276 FunctionPass *llvm::createPostDomViewerPass() {
277 return new PostDomViewer();
280 FunctionPass *llvm::createPostDomOnlyViewerPass() {
281 return new PostDomOnlyViewer();