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"
22 #include "llvm/Analysis/DOTGraphTraitsPass.h"
23 #include "llvm/Analysis/PostDominators.h"
29 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
31 DOTGraphTraits (bool isSimple=false)
32 : DefaultDOTGraphTraits(isSimple) {}
34 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
36 BasicBlock *BB = Node->getBlock();
39 return "Post dominance root node";
43 return DOTGraphTraits<const Function*>
44 ::getSimpleNodeLabel(BB, BB->getParent());
46 return DOTGraphTraits<const Function*>
47 ::getCompleteNodeLabel(BB, BB->getParent());
52 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
54 DOTGraphTraits (bool isSimple=false)
55 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
57 static std::string getGraphName(DominatorTree *DT) {
58 return "Dominator tree";
61 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
62 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
67 struct DOTGraphTraits<PostDominatorTree*>
68 : public DOTGraphTraits<DomTreeNode*> {
70 DOTGraphTraits (bool isSimple=false)
71 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
73 static std::string getGraphName(PostDominatorTree *DT) {
74 return "Post dominator tree";
77 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
78 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
83 void DominatorTree::viewGraph(const Twine &Name, const Twine &Title) {
85 ViewGraph(this, Name, false, Title);
87 errs() << "DomTree dump not available, build with DEBUG\n";
91 void DominatorTree::viewGraph() {
93 this->viewGraph("domtree", "Dominator Tree for function");
95 errs() << "DomTree dump not available, build with DEBUG\n";
100 struct DominatorTreeWrapperPassAnalysisGraphTraits {
101 static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {
102 return &DTWP->getDomTree();
106 struct DomViewer : public DOTGraphTraitsViewer<
107 DominatorTreeWrapperPass, false, DominatorTree *,
108 DominatorTreeWrapperPassAnalysisGraphTraits> {
111 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, false, DominatorTree *,
112 DominatorTreeWrapperPassAnalysisGraphTraits>(
114 initializeDomViewerPass(*PassRegistry::getPassRegistry());
118 struct DomOnlyViewer : public DOTGraphTraitsViewer<
119 DominatorTreeWrapperPass, true, DominatorTree *,
120 DominatorTreeWrapperPassAnalysisGraphTraits> {
123 : DOTGraphTraitsViewer<DominatorTreeWrapperPass, true, DominatorTree *,
124 DominatorTreeWrapperPassAnalysisGraphTraits>(
126 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
130 struct PostDominatorTreeWrapperPassAnalysisGraphTraits {
131 static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
132 return &PDTWP->getPostDomTree();
136 struct PostDomViewer : public DOTGraphTraitsViewer<
137 PostDominatorTreeWrapperPass, false,
139 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
142 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, false,
144 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
146 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
150 struct PostDomOnlyViewer : public DOTGraphTraitsViewer<
151 PostDominatorTreeWrapperPass, true,
153 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
155 PostDomOnlyViewer() :
156 DOTGraphTraitsViewer<PostDominatorTreeWrapperPass, true,
158 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
160 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
163 } // end anonymous namespace
165 char DomViewer::ID = 0;
166 INITIALIZE_PASS(DomViewer, "view-dom",
167 "View dominance tree of function", false, false)
169 char DomOnlyViewer::ID = 0;
170 INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
171 "View dominance tree of function (with no function bodies)",
174 char PostDomViewer::ID = 0;
175 INITIALIZE_PASS(PostDomViewer, "view-postdom",
176 "View postdominance tree of function", false, false)
178 char PostDomOnlyViewer::ID = 0;
179 INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
180 "View postdominance tree of function "
181 "(with no function bodies)",
185 struct DomPrinter : public DOTGraphTraitsPrinter<
186 DominatorTreeWrapperPass, false, DominatorTree *,
187 DominatorTreeWrapperPassAnalysisGraphTraits> {
190 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, false, DominatorTree *,
191 DominatorTreeWrapperPassAnalysisGraphTraits>(
193 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
197 struct DomOnlyPrinter : public DOTGraphTraitsPrinter<
198 DominatorTreeWrapperPass, true, DominatorTree *,
199 DominatorTreeWrapperPassAnalysisGraphTraits> {
202 : DOTGraphTraitsPrinter<DominatorTreeWrapperPass, true, DominatorTree *,
203 DominatorTreeWrapperPassAnalysisGraphTraits>(
205 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
209 struct PostDomPrinter
210 : public DOTGraphTraitsPrinter<
211 PostDominatorTreeWrapperPass, false,
213 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
216 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, false,
218 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
220 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
224 struct PostDomOnlyPrinter
225 : public DOTGraphTraitsPrinter<
226 PostDominatorTreeWrapperPass, true,
228 PostDominatorTreeWrapperPassAnalysisGraphTraits> {
230 PostDomOnlyPrinter() :
231 DOTGraphTraitsPrinter<PostDominatorTreeWrapperPass, true,
233 PostDominatorTreeWrapperPassAnalysisGraphTraits>(
235 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
238 } // end anonymous namespace
242 char DomPrinter::ID = 0;
243 INITIALIZE_PASS(DomPrinter, "dot-dom",
244 "Print dominance tree of function to 'dot' file",
247 char DomOnlyPrinter::ID = 0;
248 INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
249 "Print dominance tree of function to 'dot' file "
250 "(with no function bodies)",
253 char PostDomPrinter::ID = 0;
254 INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
255 "Print postdominance tree of function to 'dot' file",
258 char PostDomOnlyPrinter::ID = 0;
259 INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
260 "Print postdominance tree of function to 'dot' file "
261 "(with no function bodies)",
264 // Create methods available outside of this file, to use them
265 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
266 // the link time optimization.
268 FunctionPass *llvm::createDomPrinterPass() {
269 return new DomPrinter();
272 FunctionPass *llvm::createDomOnlyPrinterPass() {
273 return new DomOnlyPrinter();
276 FunctionPass *llvm::createDomViewerPass() {
277 return new DomViewer();
280 FunctionPass *llvm::createDomOnlyViewerPass() {
281 return new DomOnlyViewer();
284 FunctionPass *llvm::createPostDomPrinterPass() {
285 return new PostDomPrinter();
288 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
289 return new PostDomOnlyPrinter();
292 FunctionPass *llvm::createPostDomViewerPass() {
293 return new PostDomViewer();
296 FunctionPass *llvm::createPostDomOnlyViewerPass() {
297 return new PostDomOnlyViewer();