blob: 051e44b150b506a538d4145f76c6440ab626f595 [file] [log] [blame]
Chris Lattnerb839c552009-10-18 04:10:40 +00001//===- 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"
Tobias Grosser23279f12010-01-16 10:56:41 +000022
Chris Lattnerb839c552009-10-18 04:10:40 +000023#include "llvm/Analysis/Dominators.h"
Tobias Grosser23279f12010-01-16 10:56:41 +000024#include "llvm/Analysis/DOTGraphTraitsPass.h"
Chris Lattnerb839c552009-10-18 04:10:40 +000025#include "llvm/Analysis/PostDominators.h"
26
27using namespace llvm;
28
29namespace llvm {
30template<>
31struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +000032
Tobias Grosser56f4ef32009-11-30 12:38:47 +000033 DOTGraphTraits (bool isSimple=false)
34 : DefaultDOTGraphTraits(isSimple) {}
Tobias Grossera10d5982009-11-30 12:38:13 +000035
Tobias Grosser56f4ef32009-11-30 12:38:47 +000036 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
Chris Lattnerb839c552009-10-18 04:10:40 +000037
38 BasicBlock *BB = Node->getBlock();
39
40 if (!BB)
41 return "Post dominance root node";
42
Tobias Grosser56f4ef32009-11-30 12:38:47 +000043
44 if (isSimple())
45 return DOTGraphTraits<const Function*>
Duncan Sands34727662010-07-12 08:16:59 +000046 ::getSimpleNodeLabel(BB, BB->getParent());
Tobias Grosser56f4ef32009-11-30 12:38:47 +000047 else
48 return DOTGraphTraits<const Function*>
Duncan Sands34727662010-07-12 08:16:59 +000049 ::getCompleteNodeLabel(BB, BB->getParent());
Chris Lattnerb839c552009-10-18 04:10:40 +000050 }
51};
52
53template<>
54struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
55
Tobias Grossera10d5982009-11-30 12:38:13 +000056 DOTGraphTraits (bool isSimple=false)
57 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
58
Chris Lattnerb839c552009-10-18 04:10:40 +000059 static std::string getGraphName(DominatorTree *DT) {
60 return "Dominator tree";
61 }
62
Tobias Grosser56f4ef32009-11-30 12:38:47 +000063 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
64 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattnerb839c552009-10-18 04:10:40 +000065 }
66};
67
68template<>
69struct DOTGraphTraits<PostDominatorTree*>
70 : public DOTGraphTraits<DomTreeNode*> {
Tobias Grossera10d5982009-11-30 12:38:13 +000071
72 DOTGraphTraits (bool isSimple=false)
73 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
74
Chris Lattnerb839c552009-10-18 04:10:40 +000075 static std::string getGraphName(PostDominatorTree *DT) {
76 return "Post dominator tree";
77 }
Tobias Grosser56f4ef32009-11-30 12:38:47 +000078
79 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
80 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattnerb839c552009-10-18 04:10:40 +000081 }
82};
83}
84
85namespace {
Chris Lattnerb839c552009-10-18 04:10:40 +000086struct DomViewer
Tobias Grosser23279f12010-01-16 10:56:41 +000087 : public DOTGraphTraitsViewer<DominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +000088 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +000089 DomViewer() : DOTGraphTraitsViewer<DominatorTree, false>("dom", ID){
90 initializeDomViewerPass(*PassRegistry::getPassRegistry());
91 }
Chris Lattnerb839c552009-10-18 04:10:40 +000092};
93
94struct DomOnlyViewer
Tobias Grosser23279f12010-01-16 10:56:41 +000095 : public DOTGraphTraitsViewer<DominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +000096 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +000097 DomOnlyViewer() : DOTGraphTraitsViewer<DominatorTree, true>("domonly", ID){
98 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
99 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000100};
101
102struct PostDomViewer
Tobias Grosser23279f12010-01-16 10:56:41 +0000103 : public DOTGraphTraitsViewer<PostDominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000104 static char ID;
105 PostDomViewer() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000106 DOTGraphTraitsViewer<PostDominatorTree, false>("postdom", ID){
107 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
108 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000109};
110
111struct PostDomOnlyViewer
Tobias Grosser23279f12010-01-16 10:56:41 +0000112 : public DOTGraphTraitsViewer<PostDominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000113 static char ID;
114 PostDomOnlyViewer() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000115 DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", ID){
116 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
117 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000118};
119} // end anonymous namespace
120
121char DomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000122INITIALIZE_PASS(DomViewer, "view-dom",
Owen Andersonce665bd2010-10-07 22:25:06 +0000123 "View dominance tree of function", false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000124
125char DomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000126INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
127 "View dominance tree of function (with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000128 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000129
130char PostDomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000131INITIALIZE_PASS(PostDomViewer, "view-postdom",
Owen Andersonce665bd2010-10-07 22:25:06 +0000132 "View postdominance tree of function", false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000133
134char PostDomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000135INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
136 "View postdominance tree of function "
137 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000138 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000139
140namespace {
Chris Lattnerb839c552009-10-18 04:10:40 +0000141struct DomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000142 : public DOTGraphTraitsPrinter<DominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000143 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000144 DomPrinter() : DOTGraphTraitsPrinter<DominatorTree, false>("dom", ID) {
145 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
146 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000147};
148
149struct DomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000150 : public DOTGraphTraitsPrinter<DominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000151 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000152 DomOnlyPrinter() : DOTGraphTraitsPrinter<DominatorTree, true>("domonly", ID) {
153 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
154 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000155};
156
157struct PostDomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000158 : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000159 static char ID;
160 PostDomPrinter() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000161 DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", ID) {
162 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
163 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000164};
165
166struct PostDomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000167 : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000168 static char ID;
169 PostDomOnlyPrinter() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000170 DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", ID) {
171 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
172 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000173};
174} // end anonymous namespace
175
176
177
178char DomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000179INITIALIZE_PASS(DomPrinter, "dot-dom",
180 "Print dominance tree of function to 'dot' file",
Owen Andersonce665bd2010-10-07 22:25:06 +0000181 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000182
183char DomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000184INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
185 "Print dominance tree of function to 'dot' file "
186 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000187 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000188
189char PostDomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000190INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
191 "Print postdominance tree of function to 'dot' file",
Owen Andersonce665bd2010-10-07 22:25:06 +0000192 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000193
194char PostDomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000195INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
196 "Print postdominance tree of function to 'dot' file "
197 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000198 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000199
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
204FunctionPass *llvm::createDomPrinterPass() {
205 return new DomPrinter();
206}
207
208FunctionPass *llvm::createDomOnlyPrinterPass() {
209 return new DomOnlyPrinter();
210}
211
212FunctionPass *llvm::createDomViewerPass() {
213 return new DomViewer();
214}
215
216FunctionPass *llvm::createDomOnlyViewerPass() {
217 return new DomOnlyViewer();
218}
219
220FunctionPass *llvm::createPostDomPrinterPass() {
221 return new PostDomPrinter();
222}
223
224FunctionPass *llvm::createPostDomOnlyPrinterPass() {
225 return new PostDomOnlyPrinter();
226}
227
228FunctionPass *llvm::createPostDomViewerPass() {
229 return new PostDomViewer();
230}
231
232FunctionPass *llvm::createPostDomOnlyViewerPass() {
233 return new PostDomOnlyViewer();
234}