Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 1 | //===- DomPrinter.cpp - DOT printer for the dominance trees ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 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 |
| 13 | // function. |
| 14 | // |
| 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. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "llvm/Analysis/DomPrinter.h" |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 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" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace llvm { |
| 31 | template<> |
| 32 | struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits { |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame^] | 33 | |
| 34 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 35 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 36 | static std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph, |
| 37 | bool ShortNames) { |
| 38 | |
| 39 | BasicBlock *BB = Node->getBlock(); |
| 40 | |
| 41 | if (!BB) |
| 42 | return "Post dominance root node"; |
| 43 | |
| 44 | return DOTGraphTraits<const Function*>::getNodeLabel(BB, BB->getParent(), |
| 45 | ShortNames); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | template<> |
| 50 | struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> { |
| 51 | |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame^] | 52 | DOTGraphTraits (bool isSimple=false) |
| 53 | : DOTGraphTraits<DomTreeNode*>(isSimple) {} |
| 54 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 55 | static std::string getGraphName(DominatorTree *DT) { |
| 56 | return "Dominator tree"; |
| 57 | } |
| 58 | |
| 59 | static std::string getNodeLabel(DomTreeNode *Node, |
| 60 | DominatorTree *G, |
| 61 | bool ShortNames) { |
| 62 | return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode(), |
| 63 | ShortNames); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | template<> |
| 68 | struct DOTGraphTraits<PostDominatorTree*> |
| 69 | : public DOTGraphTraits<DomTreeNode*> { |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame^] | 70 | |
| 71 | DOTGraphTraits (bool isSimple=false) |
| 72 | : DOTGraphTraits<DomTreeNode*>(isSimple) {} |
| 73 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 74 | static std::string getGraphName(PostDominatorTree *DT) { |
| 75 | return "Post dominator tree"; |
| 76 | } |
| 77 | static std::string getNodeLabel(DomTreeNode *Node, |
| 78 | PostDominatorTree *G, |
| 79 | bool ShortNames) { |
| 80 | return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, |
| 81 | G->getRootNode(), |
| 82 | ShortNames); |
| 83 | } |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | namespace { |
| 88 | template <class Analysis, bool OnlyBBS> |
| 89 | struct GenericGraphViewer : public FunctionPass { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 90 | std::string Name; |
| 91 | |
Chris Lattner | 28717a8 | 2009-10-18 04:27:14 +0000 | [diff] [blame] | 92 | GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 93 | Name = GraphName; |
| 94 | } |
| 95 | |
| 96 | virtual bool runOnFunction(Function &F) { |
| 97 | Analysis *Graph; |
Tobias Grosser | 2833fac | 2009-11-30 12:06:37 +0000 | [diff] [blame] | 98 | std::string Title, GraphName; |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 99 | Graph = &getAnalysis<Analysis>(); |
Tobias Grosser | 2833fac | 2009-11-30 12:06:37 +0000 | [diff] [blame] | 100 | GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph); |
| 101 | Title = GraphName + " for '" + F.getNameStr() + "' function"; |
| 102 | ViewGraph(Graph, Name, OnlyBBS, Title); |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 108 | AU.setPreservesAll(); |
| 109 | AU.addRequired<Analysis>(); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | struct DomViewer |
| 114 | : public GenericGraphViewer<DominatorTree, false> { |
| 115 | static char ID; |
Chris Lattner | 28717a8 | 2009-10-18 04:27:14 +0000 | [diff] [blame] | 116 | DomViewer() : GenericGraphViewer<DominatorTree, false>("dom", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | struct DomOnlyViewer |
| 120 | : public GenericGraphViewer<DominatorTree, true> { |
| 121 | static char ID; |
Chris Lattner | 28717a8 | 2009-10-18 04:27:14 +0000 | [diff] [blame] | 122 | DomOnlyViewer() : GenericGraphViewer<DominatorTree, true>("domonly", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | struct PostDomViewer |
| 126 | : public GenericGraphViewer<PostDominatorTree, false> { |
| 127 | static char ID; |
| 128 | PostDomViewer() : |
Chris Lattner | 28717a8 | 2009-10-18 04:27:14 +0000 | [diff] [blame] | 129 | GenericGraphViewer<PostDominatorTree, false>("postdom", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | struct PostDomOnlyViewer |
| 133 | : public GenericGraphViewer<PostDominatorTree, true> { |
| 134 | static char ID; |
| 135 | PostDomOnlyViewer() : |
Chris Lattner | 28717a8 | 2009-10-18 04:27:14 +0000 | [diff] [blame] | 136 | GenericGraphViewer<PostDominatorTree, true>("postdomonly", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 137 | }; |
| 138 | } // end anonymous namespace |
| 139 | |
| 140 | char DomViewer::ID = 0; |
| 141 | RegisterPass<DomViewer> A("view-dom", |
| 142 | "View dominance tree of function"); |
| 143 | |
| 144 | char DomOnlyViewer::ID = 0; |
| 145 | RegisterPass<DomOnlyViewer> B("view-dom-only", |
| 146 | "View dominance tree of function " |
| 147 | "(with no function bodies)"); |
| 148 | |
| 149 | char PostDomViewer::ID = 0; |
| 150 | RegisterPass<PostDomViewer> C("view-postdom", |
| 151 | "View postdominance tree of function"); |
| 152 | |
| 153 | char PostDomOnlyViewer::ID = 0; |
| 154 | RegisterPass<PostDomOnlyViewer> D("view-postdom-only", |
| 155 | "View postdominance tree of function " |
| 156 | "(with no function bodies)"); |
| 157 | |
| 158 | namespace { |
| 159 | template <class Analysis, bool OnlyBBS> |
| 160 | struct GenericGraphPrinter : public FunctionPass { |
| 161 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 162 | std::string Name; |
| 163 | |
Chris Lattner | 0ec5c32 | 2009-10-18 04:58:34 +0000 | [diff] [blame] | 164 | GenericGraphPrinter(std::string GraphName, const void *ID) |
| 165 | : FunctionPass(ID) { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 166 | Name = GraphName; |
| 167 | } |
| 168 | |
| 169 | virtual bool runOnFunction(Function &F) { |
| 170 | Analysis *Graph; |
| 171 | std::string Filename = Name + "." + F.getNameStr() + ".dot"; |
| 172 | errs() << "Writing '" << Filename << "'..."; |
| 173 | |
| 174 | std::string ErrorInfo; |
| 175 | raw_fd_ostream File(Filename.c_str(), ErrorInfo); |
| 176 | Graph = &getAnalysis<Analysis>(); |
| 177 | |
Tobias Grosser | 2833fac | 2009-11-30 12:06:37 +0000 | [diff] [blame] | 178 | std::string Title, GraphName; |
| 179 | GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph); |
| 180 | Title = GraphName + " for '" + F.getNameStr() + "' function"; |
| 181 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 182 | if (ErrorInfo.empty()) |
Tobias Grosser | 2833fac | 2009-11-30 12:06:37 +0000 | [diff] [blame] | 183 | WriteGraph(File, Graph, OnlyBBS, Name, Title); |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 184 | else |
| 185 | errs() << " error opening file for writing!"; |
| 186 | errs() << "\n"; |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 191 | AU.setPreservesAll(); |
| 192 | AU.addRequired<Analysis>(); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | struct DomPrinter |
| 197 | : public GenericGraphPrinter<DominatorTree, false> { |
| 198 | static char ID; |
Chris Lattner | 0ec5c32 | 2009-10-18 04:58:34 +0000 | [diff] [blame] | 199 | DomPrinter() : GenericGraphPrinter<DominatorTree, false>("dom", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | struct DomOnlyPrinter |
| 203 | : public GenericGraphPrinter<DominatorTree, true> { |
| 204 | static char ID; |
Chris Lattner | 0ec5c32 | 2009-10-18 04:58:34 +0000 | [diff] [blame] | 205 | DomOnlyPrinter() : GenericGraphPrinter<DominatorTree, true>("domonly", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | struct PostDomPrinter |
| 209 | : public GenericGraphPrinter<PostDominatorTree, false> { |
| 210 | static char ID; |
| 211 | PostDomPrinter() : |
Chris Lattner | 0ec5c32 | 2009-10-18 04:58:34 +0000 | [diff] [blame] | 212 | GenericGraphPrinter<PostDominatorTree, false>("postdom", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | struct PostDomOnlyPrinter |
| 216 | : public GenericGraphPrinter<PostDominatorTree, true> { |
| 217 | static char ID; |
| 218 | PostDomOnlyPrinter() : |
Chris Lattner | 0ec5c32 | 2009-10-18 04:58:34 +0000 | [diff] [blame] | 219 | GenericGraphPrinter<PostDominatorTree, true>("postdomonly", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 220 | }; |
| 221 | } // end anonymous namespace |
| 222 | |
| 223 | |
| 224 | |
| 225 | char DomPrinter::ID = 0; |
| 226 | RegisterPass<DomPrinter> E("dot-dom", |
| 227 | "Print dominance tree of function " |
| 228 | "to 'dot' file"); |
| 229 | |
| 230 | char DomOnlyPrinter::ID = 0; |
| 231 | RegisterPass<DomOnlyPrinter> F("dot-dom-only", |
| 232 | "Print dominance tree of function " |
| 233 | "to 'dot' file " |
| 234 | "(with no function bodies)"); |
| 235 | |
| 236 | char PostDomPrinter::ID = 0; |
| 237 | RegisterPass<PostDomPrinter> G("dot-postdom", |
| 238 | "Print postdominance tree of function " |
| 239 | "to 'dot' file"); |
| 240 | |
| 241 | char PostDomOnlyPrinter::ID = 0; |
| 242 | RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only", |
| 243 | "Print postdominance tree of function " |
| 244 | "to 'dot' file " |
| 245 | "(with no function bodies)"); |
| 246 | |
| 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. |
| 250 | |
| 251 | FunctionPass *llvm::createDomPrinterPass() { |
| 252 | return new DomPrinter(); |
| 253 | } |
| 254 | |
| 255 | FunctionPass *llvm::createDomOnlyPrinterPass() { |
| 256 | return new DomOnlyPrinter(); |
| 257 | } |
| 258 | |
| 259 | FunctionPass *llvm::createDomViewerPass() { |
| 260 | return new DomViewer(); |
| 261 | } |
| 262 | |
| 263 | FunctionPass *llvm::createDomOnlyViewerPass() { |
| 264 | return new DomOnlyViewer(); |
| 265 | } |
| 266 | |
| 267 | FunctionPass *llvm::createPostDomPrinterPass() { |
| 268 | return new PostDomPrinter(); |
| 269 | } |
| 270 | |
| 271 | FunctionPass *llvm::createPostDomOnlyPrinterPass() { |
| 272 | return new PostDomOnlyPrinter(); |
| 273 | } |
| 274 | |
| 275 | FunctionPass *llvm::createPostDomViewerPass() { |
| 276 | return new PostDomViewer(); |
| 277 | } |
| 278 | |
| 279 | FunctionPass *llvm::createPostDomOnlyViewerPass() { |
| 280 | return new PostDomOnlyViewer(); |
| 281 | } |