blob: d95c3761bee62bd06d83857fdd6db96c1afe59ac [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;
Tobias Grosser23279f12010-01-16 10:56:41 +000089 DomViewer() : DOTGraphTraitsViewer<DominatorTree, false>("dom", &ID){}
Chris Lattnerb839c552009-10-18 04:10:40 +000090};
91
92struct DomOnlyViewer
Tobias Grosser23279f12010-01-16 10:56:41 +000093 : public DOTGraphTraitsViewer<DominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +000094 static char ID;
Tobias Grosser23279f12010-01-16 10:56:41 +000095 DomOnlyViewer() : DOTGraphTraitsViewer<DominatorTree, true>("domonly", &ID){}
Chris Lattnerb839c552009-10-18 04:10:40 +000096};
97
98struct PostDomViewer
Tobias Grosser23279f12010-01-16 10:56:41 +000099 : public DOTGraphTraitsViewer<PostDominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000100 static char ID;
101 PostDomViewer() :
Tobias Grosser23279f12010-01-16 10:56:41 +0000102 DOTGraphTraitsViewer<PostDominatorTree, false>("postdom", &ID){}
Chris Lattnerb839c552009-10-18 04:10:40 +0000103};
104
105struct PostDomOnlyViewer
Tobias Grosser23279f12010-01-16 10:56:41 +0000106 : public DOTGraphTraitsViewer<PostDominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000107 static char ID;
108 PostDomOnlyViewer() :
Tobias Grosser23279f12010-01-16 10:56:41 +0000109 DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", &ID){}
Chris Lattnerb839c552009-10-18 04:10:40 +0000110};
111} // end anonymous namespace
112
113char DomViewer::ID = 0;
114RegisterPass<DomViewer> A("view-dom",
115 "View dominance tree of function");
116
117char DomOnlyViewer::ID = 0;
118RegisterPass<DomOnlyViewer> B("view-dom-only",
119 "View dominance tree of function "
120 "(with no function bodies)");
121
122char PostDomViewer::ID = 0;
123RegisterPass<PostDomViewer> C("view-postdom",
124 "View postdominance tree of function");
125
126char PostDomOnlyViewer::ID = 0;
127RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
128 "View postdominance tree of function "
129 "(with no function bodies)");
130
131namespace {
Chris Lattnerb839c552009-10-18 04:10:40 +0000132struct DomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000133 : public DOTGraphTraitsPrinter<DominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000134 static char ID;
Tobias Grosser23279f12010-01-16 10:56:41 +0000135 DomPrinter() : DOTGraphTraitsPrinter<DominatorTree, false>("dom", &ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000136};
137
138struct DomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000139 : public DOTGraphTraitsPrinter<DominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000140 static char ID;
Tobias Grosser23279f12010-01-16 10:56:41 +0000141 DomOnlyPrinter() : DOTGraphTraitsPrinter<DominatorTree, true>("domonly", &ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000142};
143
144struct PostDomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000145 : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000146 static char ID;
147 PostDomPrinter() :
Tobias Grosser23279f12010-01-16 10:56:41 +0000148 DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", &ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000149};
150
151struct PostDomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000152 : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000153 static char ID;
154 PostDomOnlyPrinter() :
Tobias Grosser23279f12010-01-16 10:56:41 +0000155 DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", &ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000156};
157} // end anonymous namespace
158
159
160
161char DomPrinter::ID = 0;
162RegisterPass<DomPrinter> E("dot-dom",
163 "Print dominance tree of function "
164 "to 'dot' file");
165
166char DomOnlyPrinter::ID = 0;
167RegisterPass<DomOnlyPrinter> F("dot-dom-only",
168 "Print dominance tree of function "
169 "to 'dot' file "
170 "(with no function bodies)");
171
172char PostDomPrinter::ID = 0;
173RegisterPass<PostDomPrinter> G("dot-postdom",
174 "Print postdominance tree of function "
175 "to 'dot' file");
176
177char PostDomOnlyPrinter::ID = 0;
178RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
179 "Print postdominance tree of function "
180 "to 'dot' file "
181 "(with no function bodies)");
182
183// Create methods available outside of this file, to use them
184// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
185// the link time optimization.
186
187FunctionPass *llvm::createDomPrinterPass() {
188 return new DomPrinter();
189}
190
191FunctionPass *llvm::createDomOnlyPrinterPass() {
192 return new DomOnlyPrinter();
193}
194
195FunctionPass *llvm::createDomViewerPass() {
196 return new DomViewer();
197}
198
199FunctionPass *llvm::createDomOnlyViewerPass() {
200 return new DomOnlyViewer();
201}
202
203FunctionPass *llvm::createPostDomPrinterPass() {
204 return new PostDomPrinter();
205}
206
207FunctionPass *llvm::createPostDomOnlyPrinterPass() {
208 return new PostDomOnlyPrinter();
209}
210
211FunctionPass *llvm::createPostDomViewerPass() {
212 return new PostDomViewer();
213}
214
215FunctionPass *llvm::createPostDomOnlyViewerPass() {
216 return new PostDomOnlyViewer();
217}