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) {
89 Graph = &getAnalysis<Analysis>();
90 ViewGraph(Graph, Name, OnlyBBS);
95 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 AU.addRequired<Analysis>();
102 : public GenericGraphViewer<DominatorTree, false> {
104 DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){}
108 : public GenericGraphViewer<DominatorTree, true> {
110 DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){}
114 : public GenericGraphViewer<PostDominatorTree, false> {
117 GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){}
120 struct PostDomOnlyViewer
121 : public GenericGraphViewer<PostDominatorTree, true> {
123 PostDomOnlyViewer() :
124 GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){}
126 } // end anonymous namespace
128 char DomViewer::ID = 0;
129 RegisterPass<DomViewer> A("view-dom",
130 "View dominance tree of function");
132 char DomOnlyViewer::ID = 0;
133 RegisterPass<DomOnlyViewer> B("view-dom-only",
134 "View dominance tree of function "
135 "(with no function bodies)");
137 char PostDomViewer::ID = 0;
138 RegisterPass<PostDomViewer> C("view-postdom",
139 "View postdominance tree of function");
141 char PostDomOnlyViewer::ID = 0;
142 RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
143 "View postdominance tree of function "
144 "(with no function bodies)");
147 template <class Analysis, bool OnlyBBS>
148 struct GenericGraphPrinter : public FunctionPass {
152 GenericGraphPrinter(std::string GraphName, const void *ID)
157 virtual bool runOnFunction(Function &F) {
159 std::string Filename = Name + "." + F.getNameStr() + ".dot";
160 errs() << "Writing '" << Filename << "'...";
162 std::string ErrorInfo;
163 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
164 Graph = &getAnalysis<Analysis>();
166 if (ErrorInfo.empty())
167 WriteGraph(File, Graph, OnlyBBS);
169 errs() << " error opening file for writing!";
174 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
175 AU.setPreservesAll();
176 AU.addRequired<Analysis>();
181 : public GenericGraphPrinter<DominatorTree, false> {
183 DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom", &ID) {}
186 struct DomOnlyPrinter
187 : public GenericGraphPrinter<DominatorTree, true> {
189 DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly", &ID) {}
192 struct PostDomPrinter
193 : public GenericGraphPrinter<PostDominatorTree, false> {
196 GenericGraphPrinter<PostDominatorTree, false>("postdom", &ID) {}
199 struct PostDomOnlyPrinter
200 : public GenericGraphPrinter<PostDominatorTree, true> {
202 PostDomOnlyPrinter() :
203 GenericGraphPrinter<PostDominatorTree, true>("postdomonly", &ID) {}
205 } // end anonymous namespace
209 char DomPrinter::ID = 0;
210 RegisterPass<DomPrinter> E("dot-dom",
211 "Print dominance tree of function "
214 char DomOnlyPrinter::ID = 0;
215 RegisterPass<DomOnlyPrinter> F("dot-dom-only",
216 "Print dominance tree of function "
218 "(with no function bodies)");
220 char PostDomPrinter::ID = 0;
221 RegisterPass<PostDomPrinter> G("dot-postdom",
222 "Print postdominance tree of function "
225 char PostDomOnlyPrinter::ID = 0;
226 RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
227 "Print postdominance tree of function "
229 "(with no function bodies)");
231 // Create methods available outside of this file, to use them
232 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
233 // the link time optimization.
235 FunctionPass *llvm::createDomPrinterPass() {
236 return new DomPrinter();
239 FunctionPass *llvm::createDomOnlyPrinterPass() {
240 return new DomOnlyPrinter();
243 FunctionPass *llvm::createDomViewerPass() {
244 return new DomViewer();
247 FunctionPass *llvm::createDomOnlyViewerPass() {
248 return new DomOnlyViewer();
251 FunctionPass *llvm::createPostDomPrinterPass() {
252 return new PostDomPrinter();
255 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
256 return new PostDomOnlyPrinter();
259 FunctionPass *llvm::createPostDomViewerPass() {
260 return new PostDomViewer();
263 FunctionPass *llvm::createPostDomOnlyViewerPass() {
264 return new PostDomOnlyViewer();