1 //===- DomPrinter.cpp - DOT printer for the dominance trees ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Analysis/DomPrinter.h"
23 #include "llvm/Analysis/Dominators.h"
24 #include "llvm/Analysis/DOTGraphTraitsPass.h"
25 #include "llvm/Analysis/PostDominators.h"
31 struct DOTGraphTraits
<DomTreeNode
*> : public DefaultDOTGraphTraits
{
33 DOTGraphTraits (bool isSimple
=false)
34 : DefaultDOTGraphTraits(isSimple
) {}
36 std::string
getNodeLabel(DomTreeNode
*Node
, DomTreeNode
*Graph
) {
38 BasicBlock
*BB
= Node
->getBlock();
41 return "Post dominance root node";
45 return DOTGraphTraits
<const Function
*>
46 ::getSimpleNodeLabel(BB
, BB
->getParent());
48 return DOTGraphTraits
<const Function
*>
49 ::getCompleteNodeLabel(BB
, BB
->getParent());
54 struct DOTGraphTraits
<DominatorTree
*> : public DOTGraphTraits
<DomTreeNode
*> {
56 DOTGraphTraits (bool isSimple
=false)
57 : DOTGraphTraits
<DomTreeNode
*>(isSimple
) {}
59 static std::string
getGraphName(DominatorTree
*DT
) {
60 return "Dominator tree";
63 std::string
getNodeLabel(DomTreeNode
*Node
, DominatorTree
*G
) {
64 return DOTGraphTraits
<DomTreeNode
*>::getNodeLabel(Node
, G
->getRootNode());
69 struct DOTGraphTraits
<PostDominatorTree
*>
70 : public DOTGraphTraits
<DomTreeNode
*> {
72 DOTGraphTraits (bool isSimple
=false)
73 : DOTGraphTraits
<DomTreeNode
*>(isSimple
) {}
75 static std::string
getGraphName(PostDominatorTree
*DT
) {
76 return "Post dominator tree";
79 std::string
getNodeLabel(DomTreeNode
*Node
, PostDominatorTree
*G
) {
80 return DOTGraphTraits
<DomTreeNode
*>::getNodeLabel(Node
, G
->getRootNode());
87 : public DOTGraphTraitsViewer
<DominatorTree
, false> {
89 DomViewer() : DOTGraphTraitsViewer
<DominatorTree
, false>("dom", ID
){
90 initializeDomViewerPass(*PassRegistry::getPassRegistry());
95 : public DOTGraphTraitsViewer
<DominatorTree
, true> {
97 DomOnlyViewer() : DOTGraphTraitsViewer
<DominatorTree
, true>("domonly", ID
){
98 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
103 : public DOTGraphTraitsViewer
<PostDominatorTree
, false> {
106 DOTGraphTraitsViewer
<PostDominatorTree
, false>("postdom", ID
){
107 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
111 struct PostDomOnlyViewer
112 : public DOTGraphTraitsViewer
<PostDominatorTree
, true> {
114 PostDomOnlyViewer() :
115 DOTGraphTraitsViewer
<PostDominatorTree
, true>("postdomonly", ID
){
116 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
119 } // end anonymous namespace
121 char DomViewer::ID
= 0;
122 INITIALIZE_PASS(DomViewer
, "view-dom",
123 "View dominance tree of function", false, false)
125 char DomOnlyViewer::ID
= 0;
126 INITIALIZE_PASS(DomOnlyViewer
, "view-dom-only",
127 "View dominance tree of function (with no function bodies)",
130 char PostDomViewer::ID
= 0;
131 INITIALIZE_PASS(PostDomViewer
, "view-postdom",
132 "View postdominance tree of function", false, false)
134 char PostDomOnlyViewer::ID
= 0;
135 INITIALIZE_PASS(PostDomOnlyViewer
, "view-postdom-only",
136 "View postdominance tree of function "
137 "(with no function bodies)",
142 : public DOTGraphTraitsPrinter
<DominatorTree
, false> {
144 DomPrinter() : DOTGraphTraitsPrinter
<DominatorTree
, false>("dom", ID
) {
145 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
149 struct DomOnlyPrinter
150 : public DOTGraphTraitsPrinter
<DominatorTree
, true> {
152 DomOnlyPrinter() : DOTGraphTraitsPrinter
<DominatorTree
, true>("domonly", ID
) {
153 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
157 struct PostDomPrinter
158 : public DOTGraphTraitsPrinter
<PostDominatorTree
, false> {
161 DOTGraphTraitsPrinter
<PostDominatorTree
, false>("postdom", ID
) {
162 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
166 struct PostDomOnlyPrinter
167 : public DOTGraphTraitsPrinter
<PostDominatorTree
, true> {
169 PostDomOnlyPrinter() :
170 DOTGraphTraitsPrinter
<PostDominatorTree
, true>("postdomonly", ID
) {
171 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
174 } // end anonymous namespace
178 char DomPrinter::ID
= 0;
179 INITIALIZE_PASS(DomPrinter
, "dot-dom",
180 "Print dominance tree of function to 'dot' file",
183 char DomOnlyPrinter::ID
= 0;
184 INITIALIZE_PASS(DomOnlyPrinter
, "dot-dom-only",
185 "Print dominance tree of function to 'dot' file "
186 "(with no function bodies)",
189 char PostDomPrinter::ID
= 0;
190 INITIALIZE_PASS(PostDomPrinter
, "dot-postdom",
191 "Print postdominance tree of function to 'dot' file",
194 char PostDomOnlyPrinter::ID
= 0;
195 INITIALIZE_PASS(PostDomOnlyPrinter
, "dot-postdom-only",
196 "Print postdominance tree of function to 'dot' file "
197 "(with no function bodies)",
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.
204 FunctionPass
*llvm::createDomPrinterPass() {
205 return new DomPrinter();
208 FunctionPass
*llvm::createDomOnlyPrinterPass() {
209 return new DomOnlyPrinter();
212 FunctionPass
*llvm::createDomViewerPass() {
213 return new DomViewer();
216 FunctionPass
*llvm::createDomOnlyViewerPass() {
217 return new DomOnlyViewer();
220 FunctionPass
*llvm::createPostDomPrinterPass() {
221 return new PostDomPrinter();
224 FunctionPass
*llvm::createPostDomOnlyPrinterPass() {
225 return new PostDomOnlyPrinter();
228 FunctionPass
*llvm::createPostDomViewerPass() {
229 return new PostDomViewer();
232 FunctionPass
*llvm::createPostDomOnlyViewerPass() {
233 return new PostDomOnlyViewer();