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)
35 : DefaultDOTGraphTraits(isSimple) {}
37 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
39 BasicBlock *BB = Node->getBlock();
42 return "Post dominance root node";
46 return DOTGraphTraits<const Function*>
47 ::getSimpleNodeLabel(BB, BB->getParent());
49 return DOTGraphTraits<const Function*>
50 ::getCompleteNodeLabel(BB, BB->getParent());
55 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
57 DOTGraphTraits (bool isSimple=false)
58 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
60 static std::string getGraphName(DominatorTree *DT) {
61 return "Dominator tree";
64 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
65 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
70 struct DOTGraphTraits<PostDominatorTree*>
71 : public DOTGraphTraits<DomTreeNode*> {
73 DOTGraphTraits (bool isSimple=false)
74 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
76 static std::string getGraphName(PostDominatorTree *DT) {
77 return "Post dominator tree";
80 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
81 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
87 template <class Analysis, bool OnlyBBS>
88 struct GenericGraphViewer : public FunctionPass {
91 GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) {
95 virtual bool runOnFunction(Function &F) {
97 std::string Title, GraphName;
98 Graph = &getAnalysis<Analysis>();
99 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
100 Title = GraphName + " for '" + F.getNameStr() + "' function";
101 ViewGraph(Graph, Name, OnlyBBS, Title);
106 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
107 AU.setPreservesAll();
108 AU.addRequired<Analysis>();
113 : public GenericGraphViewer<DominatorTree, false> {
115 DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){}
119 : public GenericGraphViewer<DominatorTree, true> {
121 DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){}
125 : public GenericGraphViewer<PostDominatorTree, false> {
128 GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){}
131 struct PostDomOnlyViewer
132 : public GenericGraphViewer<PostDominatorTree, true> {
134 PostDomOnlyViewer() :
135 GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){}
137 } // end anonymous namespace
139 char DomViewer::ID = 0;
140 RegisterPass<DomViewer> A("view-dom",
141 "View dominance tree of function");
143 char DomOnlyViewer::ID = 0;
144 RegisterPass<DomOnlyViewer> B("view-dom-only",
145 "View dominance tree of function "
146 "(with no function bodies)");
148 char PostDomViewer::ID = 0;
149 RegisterPass<PostDomViewer> C("view-postdom",
150 "View postdominance tree of function");
152 char PostDomOnlyViewer::ID = 0;
153 RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
154 "View postdominance tree of function "
155 "(with no function bodies)");
158 template <class Analysis, bool OnlyBBS>
159 struct GenericGraphPrinter : public FunctionPass {
163 GenericGraphPrinter(std::string GraphName, const void *ID)
168 virtual bool runOnFunction(Function &F) {
170 std::string Filename = Name + "." + F.getNameStr() + ".dot";
171 errs() << "Writing '" << Filename << "'...";
173 std::string ErrorInfo;
174 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
175 Graph = &getAnalysis<Analysis>();
177 std::string Title, GraphName;
178 GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
179 Title = GraphName + " for '" + F.getNameStr() + "' function";
181 if (ErrorInfo.empty())
182 WriteGraph(File, Graph, OnlyBBS, Name, Title);
184 errs() << " error opening file for writing!";
189 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
190 AU.setPreservesAll();
191 AU.addRequired<Analysis>();
196 : public GenericGraphPrinter<DominatorTree, false> {
198 DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom", &ID) {}
201 struct DomOnlyPrinter
202 : public GenericGraphPrinter<DominatorTree, true> {
204 DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly", &ID) {}
207 struct PostDomPrinter
208 : public GenericGraphPrinter<PostDominatorTree, false> {
211 GenericGraphPrinter<PostDominatorTree, false>("postdom", &ID) {}
214 struct PostDomOnlyPrinter
215 : public GenericGraphPrinter<PostDominatorTree, true> {
217 PostDomOnlyPrinter() :
218 GenericGraphPrinter<PostDominatorTree, true>("postdomonly", &ID) {}
220 } // end anonymous namespace
224 char DomPrinter::ID = 0;
225 RegisterPass<DomPrinter> E("dot-dom",
226 "Print dominance tree of function "
229 char DomOnlyPrinter::ID = 0;
230 RegisterPass<DomOnlyPrinter> F("dot-dom-only",
231 "Print dominance tree of function "
233 "(with no function bodies)");
235 char PostDomPrinter::ID = 0;
236 RegisterPass<PostDomPrinter> G("dot-postdom",
237 "Print postdominance tree of function "
240 char PostDomOnlyPrinter::ID = 0;
241 RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
242 "Print postdominance tree of function "
244 "(with no function bodies)");
246 // Create methods available outside of this file, to use them
247 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
248 // the link time optimization.
250 FunctionPass *llvm::createDomPrinterPass() {
251 return new DomPrinter();
254 FunctionPass *llvm::createDomOnlyPrinterPass() {
255 return new DomOnlyPrinter();
258 FunctionPass *llvm::createDomViewerPass() {
259 return new DomViewer();
262 FunctionPass *llvm::createDomOnlyViewerPass() {
263 return new DomOnlyViewer();
266 FunctionPass *llvm::createPostDomPrinterPass() {
267 return new PostDomPrinter();
270 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
271 return new PostDomOnlyPrinter();
274 FunctionPass *llvm::createPostDomViewerPass() {
275 return new PostDomViewer();
278 FunctionPass *llvm::createPostDomOnlyViewerPass() {
279 return new PostDomOnlyViewer();