Small PostDominatorTree improvements
[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 #include "llvm/Pass.h"
23 #include "llvm/Function.h"
24 #include "llvm/Analysis/CFGPrinter.h"
25 #include "llvm/Analysis/Dominators.h"
26 #include "llvm/Analysis/PostDominators.h"
27
28 using namespace llvm;
29
30 namespace llvm {
31 template<>
32 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
33   static std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph,
34                                   bool ShortNames) {
35
36     BasicBlock *BB = Node->getBlock();
37
38     if (!BB)
39       return "Post dominance root node";
40
41     return DOTGraphTraits<const Function*>::getNodeLabel(BB, BB->getParent(),
42                                                          ShortNames);
43   }
44 };
45
46 template<>
47 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
48
49   static std::string getGraphName(DominatorTree *DT) {
50     return "Dominator tree";
51   }
52
53   static std::string getNodeLabel(DomTreeNode *Node,
54                                   DominatorTree *G,
55                                   bool ShortNames) {
56     return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode(),
57                                                       ShortNames);
58   }
59 };
60
61 template<>
62 struct DOTGraphTraits<PostDominatorTree*>
63   : public DOTGraphTraits<DomTreeNode*> {
64   static std::string getGraphName(PostDominatorTree *DT) {
65     return "Post dominator tree";
66   }
67   static std::string getNodeLabel(DomTreeNode *Node,
68                                   PostDominatorTree *G,
69                                   bool ShortNames) {
70     return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node,
71                                                       G->getRootNode(),
72                                                       ShortNames);
73   }
74 };
75 }
76
77 namespace {
78 template <class Analysis, bool OnlyBBS>
79 struct GenericGraphViewer : public FunctionPass {
80   std::string Name;
81
82   GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) {
83     Name = GraphName;
84   }
85
86   virtual bool runOnFunction(Function &F) {
87     Analysis *Graph;
88     std::string Title, GraphName;
89     Graph = &getAnalysis<Analysis>();
90     GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
91     Title = GraphName + " for '" + F.getNameStr() + "' function";
92     ViewGraph(Graph, Name, OnlyBBS, Title);
93
94     return false;
95   }
96
97   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98     AU.setPreservesAll();
99     AU.addRequired<Analysis>();
100   }
101 };
102
103 struct DomViewer
104   : public GenericGraphViewer<DominatorTree, false> {
105   static char ID;
106   DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){}
107 };
108
109 struct DomOnlyViewer
110   : public GenericGraphViewer<DominatorTree, true> {
111   static char ID;
112   DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){}
113 };
114
115 struct PostDomViewer
116   : public GenericGraphViewer<PostDominatorTree, false> {
117   static char ID;
118   PostDomViewer() :
119     GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){}
120 };
121
122 struct PostDomOnlyViewer
123   : public GenericGraphViewer<PostDominatorTree, true> {
124   static char ID;
125   PostDomOnlyViewer() :
126     GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){}
127 };
128 } // end anonymous namespace
129
130 char DomViewer::ID = 0;
131 RegisterPass<DomViewer> A("view-dom",
132                           "View dominance tree of function");
133
134 char DomOnlyViewer::ID = 0;
135 RegisterPass<DomOnlyViewer> B("view-dom-only",
136                               "View dominance tree of function "
137                               "(with no function bodies)");
138
139 char PostDomViewer::ID = 0;
140 RegisterPass<PostDomViewer> C("view-postdom",
141                               "View postdominance tree of function");
142
143 char PostDomOnlyViewer::ID = 0;
144 RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
145                                   "View postdominance tree of function "
146                                   "(with no function bodies)");
147
148 namespace {
149 template <class Analysis, bool OnlyBBS>
150 struct GenericGraphPrinter : public FunctionPass {
151
152   std::string Name;
153
154   GenericGraphPrinter(std::string GraphName, const void *ID)
155     : FunctionPass(ID) {
156     Name = GraphName;
157   }
158
159   virtual bool runOnFunction(Function &F) {
160     Analysis *Graph;
161     std::string Filename = Name + "." + F.getNameStr() + ".dot";
162     errs() << "Writing '" << Filename << "'...";
163
164     std::string ErrorInfo;
165     raw_fd_ostream File(Filename.c_str(), ErrorInfo);
166     Graph = &getAnalysis<Analysis>();
167
168     std::string Title, GraphName;
169     GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
170     Title = GraphName + " for '" + F.getNameStr() + "' function";
171
172     if (ErrorInfo.empty())
173       WriteGraph(File, Graph, OnlyBBS, Name, Title);
174     else
175       errs() << "  error opening file for writing!";
176     errs() << "\n";
177     return false;
178   }
179
180   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
181     AU.setPreservesAll();
182     AU.addRequired<Analysis>();
183   }
184 };
185
186 struct DomPrinter
187   : public GenericGraphPrinter<DominatorTree, false> {
188   static char ID;
189   DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom", &ID) {}
190 };
191
192 struct DomOnlyPrinter
193   : public GenericGraphPrinter<DominatorTree, true> {
194   static char ID;
195   DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly", &ID) {}
196 };
197
198 struct PostDomPrinter
199   : public GenericGraphPrinter<PostDominatorTree, false> {
200   static char ID;
201   PostDomPrinter() :
202     GenericGraphPrinter<PostDominatorTree, false>("postdom", &ID) {}
203 };
204
205 struct PostDomOnlyPrinter
206   : public GenericGraphPrinter<PostDominatorTree, true> {
207   static char ID;
208   PostDomOnlyPrinter() :
209     GenericGraphPrinter<PostDominatorTree, true>("postdomonly", &ID) {}
210 };
211 } // end anonymous namespace
212
213
214
215 char DomPrinter::ID = 0;
216 RegisterPass<DomPrinter> E("dot-dom",
217                            "Print dominance tree of function "
218                            "to 'dot' file");
219
220 char DomOnlyPrinter::ID = 0;
221 RegisterPass<DomOnlyPrinter> F("dot-dom-only",
222                                "Print dominance tree of function "
223                                "to 'dot' file "
224                                "(with no function bodies)");
225
226 char PostDomPrinter::ID = 0;
227 RegisterPass<PostDomPrinter> G("dot-postdom",
228                                "Print postdominance tree of function "
229                                "to 'dot' file");
230
231 char PostDomOnlyPrinter::ID = 0;
232 RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
233                                    "Print postdominance tree of function "
234                                    "to 'dot' file "
235                                    "(with no function bodies)");
236
237 // Create methods available outside of this file, to use them
238 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
239 // the link time optimization.
240
241 FunctionPass *llvm::createDomPrinterPass() {
242   return new DomPrinter();
243 }
244
245 FunctionPass *llvm::createDomOnlyPrinterPass() {
246   return new DomOnlyPrinter();
247 }
248
249 FunctionPass *llvm::createDomViewerPass() {
250   return new DomViewer();
251 }
252
253 FunctionPass *llvm::createDomOnlyViewerPass() {
254   return new DomOnlyViewer();
255 }
256
257 FunctionPass *llvm::createPostDomPrinterPass() {
258   return new PostDomPrinter();
259 }
260
261 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
262   return new PostDomOnlyPrinter();
263 }
264
265 FunctionPass *llvm::createPostDomViewerPass() {
266   return new PostDomViewer();
267 }
268
269 FunctionPass *llvm::createPostDomOnlyViewerPass() {
270   return new PostDomOnlyViewer();
271 }