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