blob: 9f340942f2ccb6926938210728ee41d4c453c013 [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 Anderson9ccaf532010-08-05 23:42:04 +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;
Owen Anderson9ccaf532010-08-05 23:42:04 +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() :
Owen Anderson9ccaf532010-08-05 23:42:04 +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() :
Owen Anderson9ccaf532010-08-05 23:42:04 +0000109 DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", ID){}
Chris Lattnerb839c552009-10-18 04:10:40 +0000110};
111} // end anonymous namespace
112
113char DomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000114INITIALIZE_PASS(DomViewer, "view-dom",
115 "View dominance tree of function", false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000116
117char DomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000118INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
119 "View dominance tree of function (with no function bodies)",
120 false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000121
122char PostDomViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000123INITIALIZE_PASS(PostDomViewer, "view-postdom",
124 "View postdominance tree of function", false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000125
126char PostDomOnlyViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000127INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
128 "View postdominance tree of function "
129 "(with no function bodies)",
130 false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000131
132namespace {
Chris Lattnerb839c552009-10-18 04:10:40 +0000133struct DomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000134 : public DOTGraphTraitsPrinter<DominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000135 static char ID;
Owen Anderson9ccaf532010-08-05 23:42:04 +0000136 DomPrinter() : DOTGraphTraitsPrinter<DominatorTree, false>("dom", ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000137};
138
139struct DomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000140 : public DOTGraphTraitsPrinter<DominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000141 static char ID;
Owen Anderson9ccaf532010-08-05 23:42:04 +0000142 DomOnlyPrinter() : DOTGraphTraitsPrinter<DominatorTree, true>("domonly", ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000143};
144
145struct PostDomPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000146 : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000147 static char ID;
148 PostDomPrinter() :
Owen Anderson9ccaf532010-08-05 23:42:04 +0000149 DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000150};
151
152struct PostDomOnlyPrinter
Tobias Grosser23279f12010-01-16 10:56:41 +0000153 : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
Chris Lattnerb839c552009-10-18 04:10:40 +0000154 static char ID;
155 PostDomOnlyPrinter() :
Owen Anderson9ccaf532010-08-05 23:42:04 +0000156 DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", ID) {}
Chris Lattnerb839c552009-10-18 04:10:40 +0000157};
158} // end anonymous namespace
159
160
161
162char DomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000163INITIALIZE_PASS(DomPrinter, "dot-dom",
164 "Print dominance tree of function to 'dot' file",
165 false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000166
167char DomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000168INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
169 "Print dominance tree of function to 'dot' file "
170 "(with no function bodies)",
171 false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000172
173char PostDomPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000174INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
175 "Print postdominance tree of function to 'dot' file",
176 false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000177
178char PostDomOnlyPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000179INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
180 "Print postdominance tree of function to 'dot' file "
181 "(with no function bodies)",
182 false, false);
Chris Lattnerb839c552009-10-18 04:10:40 +0000183
184// Create methods available outside of this file, to use them
185// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
186// the link time optimization.
187
188FunctionPass *llvm::createDomPrinterPass() {
189 return new DomPrinter();
190}
191
192FunctionPass *llvm::createDomOnlyPrinterPass() {
193 return new DomOnlyPrinter();
194}
195
196FunctionPass *llvm::createDomViewerPass() {
197 return new DomViewer();
198}
199
200FunctionPass *llvm::createDomOnlyViewerPass() {
201 return new DomOnlyViewer();
202}
203
204FunctionPass *llvm::createPostDomPrinterPass() {
205 return new PostDomPrinter();
206}
207
208FunctionPass *llvm::createPostDomOnlyPrinterPass() {
209 return new PostDomOnlyPrinter();
210}
211
212FunctionPass *llvm::createPostDomViewerPass() {
213 return new PostDomViewer();
214}
215
216FunctionPass *llvm::createPostDomOnlyViewerPass() {
217 return new PostDomOnlyViewer();
218}