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/Pass.h"
24 #include "llvm/Function.h"
25 #include "llvm/Analysis/CFGPrinter.h"
26 #include "llvm/Analysis/Dominators.h"
27 #include "llvm/Analysis/PostDominators.h"
33 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
34 static std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph,
37 BasicBlock *BB = Node->getBlock();
40 return "Post dominance root node";
42 return DOTGraphTraits<const Function*>::getNodeLabel(BB, BB->getParent(),
48 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
50 static std::string getGraphName(DominatorTree *DT) {
51 return "Dominator tree";
54 static std::string getNodeLabel(DomTreeNode *Node,
57 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode(),
63 struct DOTGraphTraits<PostDominatorTree*>
64 : public DOTGraphTraits<DomTreeNode*> {
65 static std::string getGraphName(PostDominatorTree *DT) {
66 return "Post dominator tree";
68 static std::string getNodeLabel(DomTreeNode *Node,
71 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node,
79 template <class Analysis, bool OnlyBBS>
80 struct GenericGraphViewer : public FunctionPass {
83 GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) {
87 virtual bool runOnFunction(Function &F) {
90 Graph = &getAnalysis<Analysis>();
91 ViewGraph(Graph, Name, OnlyBBS);
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.addRequired<Analysis>();
103 : public GenericGraphViewer<DominatorTree, false> {
105 DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){}
109 : public GenericGraphViewer<DominatorTree, true> {
111 DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){}
115 : public GenericGraphViewer<PostDominatorTree, false> {
118 GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){}
121 struct PostDomOnlyViewer
122 : public GenericGraphViewer<PostDominatorTree, true> {
124 PostDomOnlyViewer() :
125 GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){}
127 } // end anonymous namespace
129 char DomViewer::ID = 0;
130 RegisterPass<DomViewer> A("view-dom",
131 "View dominance tree of function");
133 char DomOnlyViewer::ID = 0;
134 RegisterPass<DomOnlyViewer> B("view-dom-only",
135 "View dominance tree of function "
136 "(with no function bodies)");
138 char PostDomViewer::ID = 0;
139 RegisterPass<PostDomViewer> C("view-postdom",
140 "View postdominance tree of function");
142 char PostDomOnlyViewer::ID = 0;
143 RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
144 "View postdominance tree of function "
145 "(with no function bodies)");
148 template <class Analysis, bool OnlyBBS>
149 struct GenericGraphPrinter : public FunctionPass {
154 GenericGraphPrinter(std::string GraphName) : FunctionPass(&ID) {
158 virtual bool runOnFunction(Function &F) {
160 std::string Filename = Name + "." + F.getNameStr() + ".dot";
161 errs() << "Writing '" << Filename << "'...";
163 std::string ErrorInfo;
164 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
165 Graph = &getAnalysis<Analysis>();
167 if (ErrorInfo.empty())
168 WriteGraph(File, Graph, OnlyBBS);
170 errs() << " error opening file for writing!";
175 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
176 AU.setPreservesAll();
177 AU.addRequired<Analysis>();
182 : public GenericGraphPrinter<DominatorTree, false> {
184 DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom"){}
187 struct DomOnlyPrinter
188 : public GenericGraphPrinter<DominatorTree, true> {
190 DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly"){}
193 struct PostDomPrinter
194 : public GenericGraphPrinter<PostDominatorTree, false> {
197 GenericGraphPrinter<PostDominatorTree, false>("postdom"){}
200 struct PostDomOnlyPrinter
201 : public GenericGraphPrinter<PostDominatorTree, true> {
203 PostDomOnlyPrinter() :
204 GenericGraphPrinter<PostDominatorTree, true>("postdomonly"){}
206 } // end anonymous namespace
210 char DomPrinter::ID = 0;
211 RegisterPass<DomPrinter> E("dot-dom",
212 "Print dominance tree of function "
215 char DomOnlyPrinter::ID = 0;
216 RegisterPass<DomOnlyPrinter> F("dot-dom-only",
217 "Print dominance tree of function "
219 "(with no function bodies)");
221 char PostDomPrinter::ID = 0;
222 RegisterPass<PostDomPrinter> G("dot-postdom",
223 "Print postdominance tree of function "
226 char PostDomOnlyPrinter::ID = 0;
227 RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
228 "Print postdominance tree of function "
230 "(with no function bodies)");
232 // Create methods available outside of this file, to use them
233 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
234 // the link time optimization.
236 FunctionPass *llvm::createDomPrinterPass() {
237 return new DomPrinter();
240 FunctionPass *llvm::createDomOnlyPrinterPass() {
241 return new DomOnlyPrinter();
244 FunctionPass *llvm::createDomViewerPass() {
245 return new DomViewer();
248 FunctionPass *llvm::createDomOnlyViewerPass() {
249 return new DomOnlyViewer();
252 FunctionPass *llvm::createPostDomPrinterPass() {
253 return new PostDomPrinter();
256 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
257 return new PostDomOnlyPrinter();
260 FunctionPass *llvm::createPostDomViewerPass() {
261 return new PostDomViewer();
264 FunctionPass *llvm::createPostDomOnlyViewerPass() {
265 return new PostDomOnlyViewer();