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/Pass.h"
23 #include "llvm/Function.h"
24 #include "llvm/Analysis/CFGPrinter.h"
25 #include "llvm/Analysis/Dominators.h"
26 #include "llvm/Analysis/PostDominators.h"
32 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
34 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
36 static std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph,
39 BasicBlock *BB = Node->getBlock();
42 return "Post dominance root node";
44 return DOTGraphTraits<const Function*>::getNodeLabel(BB, BB->getParent(),
50 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
52 DOTGraphTraits (bool isSimple=false)
53 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
55 static std::string getGraphName(DominatorTree *DT) {
56 return "Dominator tree";
59 static std::string getNodeLabel(DomTreeNode *Node,
62 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode(),
68 struct DOTGraphTraits<PostDominatorTree*>
69 : public DOTGraphTraits<DomTreeNode*> {
71 DOTGraphTraits (bool isSimple=false)
72 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
74 static std::string getGraphName(PostDominatorTree *DT) {
75 return "Post dominator tree";
77 static std::string getNodeLabel(DomTreeNode *Node,
80 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node,
88 template <class Analysis, bool OnlyBBS>
89 struct GenericGraphViewer : public FunctionPass {
92 GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) {
96 virtual bool runOnFunction(Function &F) {
98 std::string Title, GraphName;
99 Graph = &getAnalysis<Analysis>();
100 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
101 Title = GraphName + " for '" + F.getNameStr() + "' function";
102 ViewGraph(Graph, Name, OnlyBBS, Title);
107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.setPreservesAll();
109 AU.addRequired<Analysis>();
114 : public GenericGraphViewer<DominatorTree, false> {
116 DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){}
120 : public GenericGraphViewer<DominatorTree, true> {
122 DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){}
126 : public GenericGraphViewer<PostDominatorTree, false> {
129 GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){}
132 struct PostDomOnlyViewer
133 : public GenericGraphViewer<PostDominatorTree, true> {
135 PostDomOnlyViewer() :
136 GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){}
138 } // end anonymous namespace
140 char DomViewer::ID = 0;
141 RegisterPass<DomViewer> A("view-dom",
142 "View dominance tree of function");
144 char DomOnlyViewer::ID = 0;
145 RegisterPass<DomOnlyViewer> B("view-dom-only",
146 "View dominance tree of function "
147 "(with no function bodies)");
149 char PostDomViewer::ID = 0;
150 RegisterPass<PostDomViewer> C("view-postdom",
151 "View postdominance tree of function");
153 char PostDomOnlyViewer::ID = 0;
154 RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
155 "View postdominance tree of function "
156 "(with no function bodies)");
159 template <class Analysis, bool OnlyBBS>
160 struct GenericGraphPrinter : public FunctionPass {
164 GenericGraphPrinter(std::string GraphName, const void *ID)
169 virtual bool runOnFunction(Function &F) {
171 std::string Filename = Name + "." + F.getNameStr() + ".dot";
172 errs() << "Writing '" << Filename << "'...";
174 std::string ErrorInfo;
175 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
176 Graph = &getAnalysis<Analysis>();
178 std::string Title, GraphName;
179 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
180 Title = GraphName + " for '" + F.getNameStr() + "' function";
182 if (ErrorInfo.empty())
183 WriteGraph(File, Graph, OnlyBBS, Name, Title);
185 errs() << " error opening file for writing!";
190 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
191 AU.setPreservesAll();
192 AU.addRequired<Analysis>();
197 : public GenericGraphPrinter<DominatorTree, false> {
199 DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom", &ID) {}
202 struct DomOnlyPrinter
203 : public GenericGraphPrinter<DominatorTree, true> {
205 DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly", &ID) {}
208 struct PostDomPrinter
209 : public GenericGraphPrinter<PostDominatorTree, false> {
212 GenericGraphPrinter<PostDominatorTree, false>("postdom", &ID) {}
215 struct PostDomOnlyPrinter
216 : public GenericGraphPrinter<PostDominatorTree, true> {
218 PostDomOnlyPrinter() :
219 GenericGraphPrinter<PostDominatorTree, true>("postdomonly", &ID) {}
221 } // end anonymous namespace
225 char DomPrinter::ID = 0;
226 RegisterPass<DomPrinter> E("dot-dom",
227 "Print dominance tree of function "
230 char DomOnlyPrinter::ID = 0;
231 RegisterPass<DomOnlyPrinter> F("dot-dom-only",
232 "Print dominance tree of function "
234 "(with no function bodies)");
236 char PostDomPrinter::ID = 0;
237 RegisterPass<PostDomPrinter> G("dot-postdom",
238 "Print postdominance tree of function "
241 char PostDomOnlyPrinter::ID = 0;
242 RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
243 "Print postdominance tree of function "
245 "(with no function bodies)");
247 // Create methods available outside of this file, to use them
248 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
249 // the link time optimization.
251 FunctionPass *llvm::createDomPrinterPass() {
252 return new DomPrinter();
255 FunctionPass *llvm::createDomOnlyPrinterPass() {
256 return new DomOnlyPrinter();
259 FunctionPass *llvm::createDomViewerPass() {
260 return new DomViewer();
263 FunctionPass *llvm::createDomOnlyViewerPass() {
264 return new DomOnlyViewer();
267 FunctionPass *llvm::createPostDomPrinterPass() {
268 return new PostDomPrinter();
271 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
272 return new PostDomOnlyPrinter();
275 FunctionPass *llvm::createPostDomViewerPass() {
276 return new PostDomViewer();
279 FunctionPass *llvm::createPostDomOnlyViewerPass() {
280 return new PostDomOnlyViewer();