blob: cde431459d5018d9c9f0a83170e5e0d86044d93e [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#include "llvm/Analysis/DOTGraphTraitsPass.h"
Chris Lattnerb839c552009-10-18 04:10:40 +000023#include "llvm/Analysis/PostDominators.h"
24
25using namespace llvm;
26
27namespace llvm {
28template<>
29struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +000030
Tobias Grosser56f4ef32009-11-30 12:38:47 +000031 DOTGraphTraits (bool isSimple=false)
32 : DefaultDOTGraphTraits(isSimple) {}
Tobias Grossera10d5982009-11-30 12:38:13 +000033
Tobias Grosser56f4ef32009-11-30 12:38:47 +000034 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
Chris Lattnerb839c552009-10-18 04:10:40 +000035
36 BasicBlock *BB = Node->getBlock();
37
38 if (!BB)
39 return "Post dominance root node";
40
Tobias Grosser56f4ef32009-11-30 12:38:47 +000041
42 if (isSimple())
43 return DOTGraphTraits<const Function*>
Duncan Sands34727662010-07-12 08:16:59 +000044 ::getSimpleNodeLabel(BB, BB->getParent());
Tobias Grosser56f4ef32009-11-30 12:38:47 +000045 else
46 return DOTGraphTraits<const Function*>
Duncan Sands34727662010-07-12 08:16:59 +000047 ::getCompleteNodeLabel(BB, BB->getParent());
Chris Lattnerb839c552009-10-18 04:10:40 +000048 }
49};
50
51template<>
52struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
53
Tobias Grossera10d5982009-11-30 12:38:13 +000054 DOTGraphTraits (bool isSimple=false)
55 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
56
Chris Lattnerb839c552009-10-18 04:10:40 +000057 static std::string getGraphName(DominatorTree *DT) {
58 return "Dominator tree";
59 }
60
Tobias Grosser56f4ef32009-11-30 12:38:47 +000061 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
62 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattnerb839c552009-10-18 04:10:40 +000063 }
64};
65
66template<>
67struct DOTGraphTraits<PostDominatorTree*>
68 : public DOTGraphTraits<DomTreeNode*> {
Tobias Grossera10d5982009-11-30 12:38:13 +000069
70 DOTGraphTraits (bool isSimple=false)
71 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
72
Chris Lattnerb839c552009-10-18 04:10:40 +000073 static std::string getGraphName(PostDominatorTree *DT) {
74 return "Post dominator tree";
75 }
Tobias Grosser56f4ef32009-11-30 12:38:47 +000076
77 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
78 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
Chris Lattnerb839c552009-10-18 04:10:40 +000079 }
80};
81}
82
83namespace {
Chris Lattnerb839c552009-10-18 04:10:40 +000084struct DomViewer
Tobias Grosser23279f12010-01-16 10:56:41 +000085 : public DOTGraphTraitsViewer<DominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +000086 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +000087 DomViewer() : DOTGraphTraitsViewer<DominatorTree, false>("dom", ID){
88 initializeDomViewerPass(*PassRegistry::getPassRegistry());
89 }
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;
Owen Anderson081c34b2010-10-19 17:21:58 +000095 DomOnlyViewer() : DOTGraphTraitsViewer<DominatorTree, true>("domonly", ID){
96 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
97 }
Chris Lattnerb839c552009-10-18 04:10:40 +000098};
99
100struct PostDomViewer
Tobias Grosser23279f12010-01-16 10:56:41 +0000101 : public DOTGraphTraitsViewer<PostDominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000102 static char ID;
103 PostDomViewer() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000104 DOTGraphTraitsViewer<PostDominatorTree, false>("postdom", ID){
105 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
106 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000107};
108
109struct PostDomOnlyViewer
Tobias Grosser23279f12010-01-16 10:56:41 +0000110 : public DOTGraphTraitsViewer<PostDominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000111 static char ID;
112 PostDomOnlyViewer() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000113 DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", ID){
114 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
115 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000116};
117} // end anonymous namespace
118
119char DomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000120INITIALIZE_PASS(DomViewer, "view-dom",
Owen Andersonce665bd2010-10-07 22:25:06 +0000121 "View dominance tree of function", false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000122
123char DomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000124INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
125 "View dominance tree of function (with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000126 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000127
128char PostDomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000129INITIALIZE_PASS(PostDomViewer, "view-postdom",
Owen Andersonce665bd2010-10-07 22:25:06 +0000130 "View postdominance tree of function", false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000131
132char PostDomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000133INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
134 "View postdominance tree of function "
135 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000136 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000137
138namespace {
Chris Lattnerb839c552009-10-18 04:10:40 +0000139struct DomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000140 : public DOTGraphTraitsPrinter<DominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000141 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000142 DomPrinter() : DOTGraphTraitsPrinter<DominatorTree, false>("dom", ID) {
143 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
144 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000145};
146
147struct DomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000148 : public DOTGraphTraitsPrinter<DominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000149 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000150 DomOnlyPrinter() : DOTGraphTraitsPrinter<DominatorTree, true>("domonly", ID) {
151 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
152 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000153};
154
155struct PostDomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000156 : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000157 static char ID;
158 PostDomPrinter() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000159 DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", ID) {
160 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
161 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000162};
163
164struct PostDomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000165 : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000166 static char ID;
167 PostDomOnlyPrinter() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000168 DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", ID) {
169 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
170 }
Chris Lattnerb839c552009-10-18 04:10:40 +0000171};
172} // end anonymous namespace
173
174
175
176char DomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000177INITIALIZE_PASS(DomPrinter, "dot-dom",
178 "Print dominance tree of function to 'dot' file",
Owen Andersonce665bd2010-10-07 22:25:06 +0000179 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000180
181char DomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000182INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
183 "Print dominance tree of function to 'dot' file "
184 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000185 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000186
187char PostDomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000188INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
189 "Print postdominance tree of function to 'dot' file",
Owen Andersonce665bd2010-10-07 22:25:06 +0000190 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000191
192char PostDomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000193INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
194 "Print postdominance tree of function to 'dot' file "
195 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000196 false, false)
Chris Lattnerb839c552009-10-18 04:10:40 +0000197
198// Create methods available outside of this file, to use them
199// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
200// the link time optimization.
201
202FunctionPass *llvm::createDomPrinterPass() {
203 return new DomPrinter();
204}
205
206FunctionPass *llvm::createDomOnlyPrinterPass() {
207 return new DomOnlyPrinter();
208}
209
210FunctionPass *llvm::createDomViewerPass() {
211 return new DomViewer();
212}
213
214FunctionPass *llvm::createDomOnlyViewerPass() {
215 return new DomOnlyViewer();
216}
217
218FunctionPass *llvm::createPostDomPrinterPass() {
219 return new PostDomPrinter();
220}
221
222FunctionPass *llvm::createPostDomOnlyPrinterPass() {
223 return new PostDomOnlyPrinter();
224}
225
226FunctionPass *llvm::createPostDomViewerPass() {
227 return new PostDomViewer();
228}
229
230FunctionPass *llvm::createPostDomOnlyViewerPass() {
231 return new PostDomOnlyViewer();
232}