Get rid of static constructors for pass registration. Instead, every pass exposes...
[oota-llvm.git] / lib / Analysis / DomPrinter.cpp
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
23 #include "llvm/Analysis/Dominators.h"
24 #include "llvm/Analysis/DOTGraphTraitsPass.h"
25 #include "llvm/Analysis/PostDominators.h"
26
27 using namespace llvm;
28
29 namespace llvm {
30 template<>
31 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
32
33   DOTGraphTraits (bool isSimple=false)
34     : DefaultDOTGraphTraits(isSimple) {}
35
36   std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
37
38     BasicBlock *BB = Node->getBlock();
39
40     if (!BB)
41       return "Post dominance root node";
42
43
44     if (isSimple())
45       return DOTGraphTraits<const Function*>
46         ::getSimpleNodeLabel(BB, BB->getParent());
47     else
48       return DOTGraphTraits<const Function*>
49         ::getCompleteNodeLabel(BB, BB->getParent());
50   }
51 };
52
53 template<>
54 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
55
56   DOTGraphTraits (bool isSimple=false)
57     : DOTGraphTraits<DomTreeNode*>(isSimple) {}
58
59   static std::string getGraphName(DominatorTree *DT) {
60     return "Dominator tree";
61   }
62
63   std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
64     return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
65   }
66 };
67
68 template<>
69 struct DOTGraphTraits<PostDominatorTree*>
70   : public DOTGraphTraits<DomTreeNode*> {
71
72   DOTGraphTraits (bool isSimple=false)
73     : DOTGraphTraits<DomTreeNode*>(isSimple) {}
74
75   static std::string getGraphName(PostDominatorTree *DT) {
76     return "Post dominator tree";
77   }
78
79   std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
80     return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
81   }
82 };
83 }
84
85 namespace {
86 struct DomViewer
87   : public DOTGraphTraitsViewer<DominatorTree, false> {
88   static char ID;
89   DomViewer() : DOTGraphTraitsViewer<DominatorTree, false>("dom", ID){
90     initializeDomViewerPass(*PassRegistry::getPassRegistry());
91   }
92 };
93
94 struct DomOnlyViewer
95   : public DOTGraphTraitsViewer<DominatorTree, true> {
96   static char ID;
97   DomOnlyViewer() : DOTGraphTraitsViewer<DominatorTree, true>("domonly", ID){
98     initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
99   }
100 };
101
102 struct PostDomViewer
103   : public DOTGraphTraitsViewer<PostDominatorTree, false> {
104   static char ID;
105   PostDomViewer() :
106     DOTGraphTraitsViewer<PostDominatorTree, false>("postdom", ID){
107       initializePostDomViewerPass(*PassRegistry::getPassRegistry());
108     }
109 };
110
111 struct PostDomOnlyViewer
112   : public DOTGraphTraitsViewer<PostDominatorTree, true> {
113   static char ID;
114   PostDomOnlyViewer() :
115     DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", ID){
116       initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
117     }
118 };
119 } // end anonymous namespace
120
121 char DomViewer::ID = 0;
122 INITIALIZE_PASS(DomViewer, "view-dom",
123                 "View dominance tree of function", false, false)
124
125 char DomOnlyViewer::ID = 0;
126 INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
127                 "View dominance tree of function (with no function bodies)",
128                 false, false)
129
130 char PostDomViewer::ID = 0;
131 INITIALIZE_PASS(PostDomViewer, "view-postdom",
132                 "View postdominance tree of function", false, false)
133
134 char PostDomOnlyViewer::ID = 0;
135 INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
136                 "View postdominance tree of function "
137                 "(with no function bodies)",
138                 false, false)
139
140 namespace {
141 struct DomPrinter
142   : public DOTGraphTraitsPrinter<DominatorTree, false> {
143   static char ID;
144   DomPrinter() : DOTGraphTraitsPrinter<DominatorTree, false>("dom", ID) {
145     initializeDomPrinterPass(*PassRegistry::getPassRegistry());
146   }
147 };
148
149 struct DomOnlyPrinter
150   : public DOTGraphTraitsPrinter<DominatorTree, true> {
151   static char ID;
152   DomOnlyPrinter() : DOTGraphTraitsPrinter<DominatorTree, true>("domonly", ID) {
153     initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
154   }
155 };
156
157 struct PostDomPrinter
158   : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
159   static char ID;
160   PostDomPrinter() :
161     DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", ID) {
162       initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
163     }
164 };
165
166 struct PostDomOnlyPrinter
167   : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
168   static char ID;
169   PostDomOnlyPrinter() :
170     DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", ID) {
171       initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
172     }
173 };
174 } // end anonymous namespace
175
176
177
178 char DomPrinter::ID = 0;
179 INITIALIZE_PASS(DomPrinter, "dot-dom",
180                 "Print dominance tree of function to 'dot' file",
181                 false, false)
182
183 char DomOnlyPrinter::ID = 0;
184 INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
185                 "Print dominance tree of function to 'dot' file "
186                 "(with no function bodies)",
187                 false, false)
188
189 char PostDomPrinter::ID = 0;
190 INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
191                 "Print postdominance tree of function to 'dot' file",
192                 false, false)
193
194 char PostDomOnlyPrinter::ID = 0;
195 INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
196                 "Print postdominance tree of function to 'dot' file "
197                 "(with no function bodies)",
198                 false, false)
199
200 // Create methods available outside of this file, to use them
201 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
202 // the link time optimization.
203
204 FunctionPass *llvm::createDomPrinterPass() {
205   return new DomPrinter();
206 }
207
208 FunctionPass *llvm::createDomOnlyPrinterPass() {
209   return new DomOnlyPrinter();
210 }
211
212 FunctionPass *llvm::createDomViewerPass() {
213   return new DomViewer();
214 }
215
216 FunctionPass *llvm::createDomOnlyViewerPass() {
217   return new DomOnlyViewer();
218 }
219
220 FunctionPass *llvm::createPostDomPrinterPass() {
221   return new PostDomPrinter();
222 }
223
224 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
225   return new PostDomOnlyPrinter();
226 }
227
228 FunctionPass *llvm::createPostDomViewerPass() {
229   return new PostDomViewer();
230 }
231
232 FunctionPass *llvm::createPostDomOnlyViewerPass() {
233   return new PostDomOnlyViewer();
234 }