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 {
85 GenericGraphViewer(std::string GraphName) : FunctionPass(&ID) {
89 virtual bool runOnFunction(Function &F) {
92 Graph = &getAnalysis<Analysis>();
93 ViewGraph(Graph, Name, OnlyBBS);
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
100 AU.addRequired<Analysis>();
105 : public GenericGraphViewer<DominatorTree, false> {
107 DomViewer() : GenericGraphViewer<DominatorTree, false>("dom"){}
111 : public GenericGraphViewer<DominatorTree, true> {
113 DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly"){}
117 : public GenericGraphViewer<PostDominatorTree, false> {
120 GenericGraphViewer<PostDominatorTree, false>("postdom"){}
123 struct PostDomOnlyViewer
124 : public GenericGraphViewer<PostDominatorTree, true> {
126 PostDomOnlyViewer() :
127 GenericGraphViewer<PostDominatorTree, true>("postdomonly"){}
129 } // end anonymous namespace
131 char DomViewer::ID = 0;
132 RegisterPass<DomViewer> A("view-dom",
133 "View dominance tree of function");
135 char DomOnlyViewer::ID = 0;
136 RegisterPass<DomOnlyViewer> B("view-dom-only",
137 "View dominance tree of function "
138 "(with no function bodies)");
140 char PostDomViewer::ID = 0;
141 RegisterPass<PostDomViewer> C("view-postdom",
142 "View postdominance tree of function");
144 char PostDomOnlyViewer::ID = 0;
145 RegisterPass<PostDomOnlyViewer> D("view-postdom-only",
146 "View postdominance tree of function "
147 "(with no function bodies)");
150 template <class Analysis, bool OnlyBBS>
151 struct GenericGraphPrinter : public FunctionPass {
156 GenericGraphPrinter(std::string GraphName) : FunctionPass(&ID) {
160 virtual bool runOnFunction(Function &F) {
162 std::string Filename = Name + "." + F.getNameStr() + ".dot";
163 errs() << "Writing '" << Filename << "'...";
165 std::string ErrorInfo;
166 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
167 Graph = &getAnalysis<Analysis>();
169 if (ErrorInfo.empty())
170 WriteGraph(File, Graph, OnlyBBS);
172 errs() << " error opening file for writing!";
177 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
178 AU.setPreservesAll();
179 AU.addRequired<Analysis>();
184 : public GenericGraphPrinter<DominatorTree, false> {
186 DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom"){}
189 struct DomOnlyPrinter
190 : public GenericGraphPrinter<DominatorTree, true> {
192 DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly"){}
195 struct PostDomPrinter
196 : public GenericGraphPrinter<PostDominatorTree, false> {
199 GenericGraphPrinter<PostDominatorTree, false>("postdom"){}
202 struct PostDomOnlyPrinter
203 : public GenericGraphPrinter<PostDominatorTree, true> {
205 PostDomOnlyPrinter() :
206 GenericGraphPrinter<PostDominatorTree, true>("postdomonly"){}
208 } // end anonymous namespace
212 char DomPrinter::ID = 0;
213 RegisterPass<DomPrinter> E("dot-dom",
214 "Print dominance tree of function "
217 char DomOnlyPrinter::ID = 0;
218 RegisterPass<DomOnlyPrinter> F("dot-dom-only",
219 "Print dominance tree of function "
221 "(with no function bodies)");
223 char PostDomPrinter::ID = 0;
224 RegisterPass<PostDomPrinter> G("dot-postdom",
225 "Print postdominance tree of function "
228 char PostDomOnlyPrinter::ID = 0;
229 RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only",
230 "Print postdominance tree of function "
232 "(with no function bodies)");
234 // Create methods available outside of this file, to use them
235 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
236 // the link time optimization.
238 FunctionPass *llvm::createDomPrinterPass() {
239 return new DomPrinter();
242 FunctionPass *llvm::createDomOnlyPrinterPass() {
243 return new DomOnlyPrinter();
246 FunctionPass *llvm::createDomViewerPass() {
247 return new DomViewer();
250 FunctionPass *llvm::createDomOnlyViewerPass() {
251 return new DomOnlyViewer();
254 FunctionPass *llvm::createPostDomPrinterPass() {
255 return new PostDomPrinter();
258 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
259 return new PostDomOnlyPrinter();
262 FunctionPass *llvm::createPostDomViewerPass() {
263 return new PostDomViewer();
266 FunctionPass *llvm::createPostDomOnlyViewerPass() {
267 return new PostDomOnlyViewer();