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 {
33 static std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph,
36 BasicBlock *BB = Node->getBlock();
39 return "Post dominance root node";
41 return DOTGraphTraits<const Function*>::getNodeLabel(BB, BB->getParent(),
47 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
49 static std::string getGraphName(DominatorTree *DT) {
50 return "Dominator tree";
53 static std::string getNodeLabel(DomTreeNode *Node,
56 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode(),
62 struct DOTGraphTraits<PostDominatorTree*>
63 : public DOTGraphTraits<DomTreeNode*> {
64 static std::string getGraphName(PostDominatorTree *DT) {
65 return "Post dominator tree";
67 static std::string getNodeLabel(DomTreeNode *Node,
70 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node,
78 template <class Analysis, bool OnlyBBS>
79 struct GenericGraphViewer : public FunctionPass {
82 GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) {
86 virtual bool runOnFunction(Function &F) {
88 std::string Title, GraphName;
89 Graph = &getAnalysis<Analysis>();
90 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
91 Title = GraphName + " for '" + F.getNameStr() + "' function";
92 ViewGraph(Graph, Name, OnlyBBS, Title);
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.addRequired<Analysis>();
104 : public GenericGraphViewer<DominatorTree, false> {
106 DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){}
110 : public GenericGraphViewer<DominatorTree, true> {
112 DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){}
116 : public GenericGraphViewer<PostDominatorTree, false> {
119 GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){}
122 struct PostDomOnlyViewer
123 : public GenericGraphViewer<PostDominatorTree, true> {
125 PostDomOnlyViewer() :
126 GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){}
128 } // end anonymous namespace
130 char DomViewer::ID = 0;
131 RegisterPass<DomViewer> A("view-dom",
132 "View dominance tree of function");
134 char DomOnlyViewer::ID = 0;
135 RegisterPass<DomOnlyViewer> B("view-dom-only",
136 "View dominance tree of function "
137 "(with no function bodies)");
139 char PostDomViewer::ID = 0;
140 RegisterPass<PostDomViewer> C("view-postdom",
141 "View postdominance tree of function");
143 char PostDomOnlyViewer::ID = 0;
144 RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
145 "View postdominance tree of function "
146 "(with no function bodies)");
149 template <class Analysis, bool OnlyBBS>
150 struct GenericGraphPrinter : public FunctionPass {
154 GenericGraphPrinter(std::string GraphName, const void *ID)
159 virtual bool runOnFunction(Function &F) {
161 std::string Filename = Name + "." + F.getNameStr() + ".dot";
162 errs() << "Writing '" << Filename << "'...";
164 std::string ErrorInfo;
165 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
166 Graph = &getAnalysis<Analysis>();
168 std::string Title, GraphName;
169 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
170 Title = GraphName + " for '" + F.getNameStr() + "' function";
172 if (ErrorInfo.empty())
173 WriteGraph(File, Graph, OnlyBBS, Name, Title);
175 errs() << " error opening file for writing!";
180 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
181 AU.setPreservesAll();
182 AU.addRequired<Analysis>();
187 : public GenericGraphPrinter<DominatorTree, false> {
189 DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom", &ID) {}
192 struct DomOnlyPrinter
193 : public GenericGraphPrinter<DominatorTree, true> {
195 DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly", &ID) {}
198 struct PostDomPrinter
199 : public GenericGraphPrinter<PostDominatorTree, false> {
202 GenericGraphPrinter<PostDominatorTree, false>("postdom", &ID) {}
205 struct PostDomOnlyPrinter
206 : public GenericGraphPrinter<PostDominatorTree, true> {
208 PostDomOnlyPrinter() :
209 GenericGraphPrinter<PostDominatorTree, true>("postdomonly", &ID) {}
211 } // end anonymous namespace
215 char DomPrinter::ID = 0;
216 RegisterPass<DomPrinter> E("dot-dom",
217 "Print dominance tree of function "
220 char DomOnlyPrinter::ID = 0;
221 RegisterPass<DomOnlyPrinter> F("dot-dom-only",
222 "Print dominance tree of function "
224 "(with no function bodies)");
226 char PostDomPrinter::ID = 0;
227 RegisterPass<PostDomPrinter> G("dot-postdom",
228 "Print postdominance tree of function "
231 char PostDomOnlyPrinter::ID = 0;
232 RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
233 "Print postdominance tree of function "
235 "(with no function bodies)");
237 // Create methods available outside of this file, to use them
238 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
239 // the link time optimization.
241 FunctionPass *llvm::createDomPrinterPass() {
242 return new DomPrinter();
245 FunctionPass *llvm::createDomOnlyPrinterPass() {
246 return new DomOnlyPrinter();
249 FunctionPass *llvm::createDomViewerPass() {
250 return new DomViewer();
253 FunctionPass *llvm::createDomOnlyViewerPass() {
254 return new DomOnlyViewer();
257 FunctionPass *llvm::createPostDomPrinterPass() {
258 return new PostDomPrinter();
261 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
262 return new PostDomOnlyPrinter();
265 FunctionPass *llvm::createPostDomViewerPass() {
266 return new PostDomViewer();
269 FunctionPass *llvm::createPostDomOnlyViewerPass() {
270 return new PostDomOnlyViewer();