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" |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 22 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Dominators.h" |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 24 | #include "llvm/Analysis/DOTGraphTraitsPass.h" |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/PostDominators.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace llvm { |
| 30 | template<> |
| 31 | struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits { |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 32 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 33 | DOTGraphTraits (bool isSimple=false) |
| 34 | : DefaultDOTGraphTraits(isSimple) {} |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 35 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 36 | std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 37 | |
| 38 | BasicBlock *BB = Node->getBlock(); |
| 39 | |
| 40 | if (!BB) |
| 41 | return "Post dominance root node"; |
| 42 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 43 | |
| 44 | if (isSimple()) |
| 45 | return DOTGraphTraits<const Function*> |
| 46 | ::getSimpleNodeLabel(BB, BB->getParent()); |
| 47 | else |
| 48 | return DOTGraphTraits<const Function*> |
| 49 | ::getCompleteNodeLabel(BB, BB->getParent()); |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | template<> |
| 54 | struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> { |
| 55 | |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 56 | DOTGraphTraits (bool isSimple=false) |
| 57 | : DOTGraphTraits<DomTreeNode*>(isSimple) {} |
| 58 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 59 | static std::string getGraphName(DominatorTree *DT) { |
| 60 | return "Dominator tree"; |
| 61 | } |
| 62 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 63 | std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) { |
| 64 | return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode()); |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 65 | } |
| 66 | }; |
| 67 | |
| 68 | template<> |
| 69 | struct DOTGraphTraits<PostDominatorTree*> |
| 70 | : public DOTGraphTraits<DomTreeNode*> { |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 71 | |
| 72 | DOTGraphTraits (bool isSimple=false) |
| 73 | : DOTGraphTraits<DomTreeNode*>(isSimple) {} |
| 74 | |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 75 | static std::string getGraphName(PostDominatorTree *DT) { |
| 76 | return "Post dominator tree"; |
| 77 | } |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 78 | |
| 79 | std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) { |
| 80 | return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode()); |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | namespace { |
| 86 | template <class Analysis, bool OnlyBBS> |
| 87 | struct GenericGraphViewer : public FunctionPass { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 88 | std::string Name; |
| 89 | |
Chris Lattner | 28717a8 | 2009-10-18 04:27:14 +0000 | [diff] [blame] | 90 | GenericGraphViewer(std::string GraphName, const void *ID) : FunctionPass(ID) { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 91 | Name = GraphName; |
| 92 | } |
| 93 | |
| 94 | virtual bool runOnFunction(Function &F) { |
| 95 | Analysis *Graph; |
Tobias Grosser | 2833fac | 2009-11-30 12:06:37 +0000 | [diff] [blame] | 96 | std::string Title, GraphName; |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 97 | Graph = &getAnalysis<Analysis>(); |
Tobias Grosser | 2833fac | 2009-11-30 12:06:37 +0000 | [diff] [blame] | 98 | GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph); |
| 99 | Title = GraphName + " for '" + F.getNameStr() + "' function"; |
| 100 | ViewGraph(Graph, Name, OnlyBBS, Title); |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 106 | AU.setPreservesAll(); |
| 107 | AU.addRequired<Analysis>(); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | struct DomViewer |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 112 | : public DOTGraphTraitsViewer<DominatorTree, false> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 113 | static char ID; |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 114 | DomViewer() : DOTGraphTraitsViewer<DominatorTree, false>("dom", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | struct DomOnlyViewer |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 118 | : public DOTGraphTraitsViewer<DominatorTree, true> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 119 | static char ID; |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 120 | DomOnlyViewer() : DOTGraphTraitsViewer<DominatorTree, true>("domonly", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct PostDomViewer |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 124 | : public DOTGraphTraitsViewer<PostDominatorTree, false> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 125 | static char ID; |
| 126 | PostDomViewer() : |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 127 | DOTGraphTraitsViewer<PostDominatorTree, false>("postdom", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct PostDomOnlyViewer |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 131 | : public DOTGraphTraitsViewer<PostDominatorTree, true> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 132 | static char ID; |
| 133 | PostDomOnlyViewer() : |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 134 | DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", &ID){} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 135 | }; |
| 136 | } // end anonymous namespace |
| 137 | |
| 138 | char DomViewer::ID = 0; |
| 139 | RegisterPass<DomViewer> A("view-dom", |
| 140 | "View dominance tree of function"); |
| 141 | |
| 142 | char DomOnlyViewer::ID = 0; |
| 143 | RegisterPass<DomOnlyViewer> B("view-dom-only", |
| 144 | "View dominance tree of function " |
| 145 | "(with no function bodies)"); |
| 146 | |
| 147 | char PostDomViewer::ID = 0; |
| 148 | RegisterPass<PostDomViewer> C("view-postdom", |
| 149 | "View postdominance tree of function"); |
| 150 | |
| 151 | char PostDomOnlyViewer::ID = 0; |
| 152 | RegisterPass<PostDomOnlyViewer> D("view-postdom-only", |
| 153 | "View postdominance tree of function " |
| 154 | "(with no function bodies)"); |
| 155 | |
| 156 | namespace { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 157 | struct DomPrinter |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 158 | : public DOTGraphTraitsPrinter<DominatorTree, false> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 159 | static char ID; |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 160 | DomPrinter() : DOTGraphTraitsPrinter<DominatorTree, false>("dom", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | struct DomOnlyPrinter |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 164 | : public DOTGraphTraitsPrinter<DominatorTree, true> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 165 | static char ID; |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 166 | DomOnlyPrinter() : DOTGraphTraitsPrinter<DominatorTree, true>("domonly", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | struct PostDomPrinter |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 170 | : public DOTGraphTraitsPrinter<PostDominatorTree, false> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 171 | static char ID; |
| 172 | PostDomPrinter() : |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 173 | DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | struct PostDomOnlyPrinter |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 177 | : public DOTGraphTraitsPrinter<PostDominatorTree, true> { |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 178 | static char ID; |
| 179 | PostDomOnlyPrinter() : |
Tobias Grosser | 23279f1 | 2010-01-16 10:56:41 +0000 | [diff] [blame^] | 180 | DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", &ID) {} |
Chris Lattner | b839c55 | 2009-10-18 04:10:40 +0000 | [diff] [blame] | 181 | }; |
| 182 | } // end anonymous namespace |
| 183 | |
| 184 | |
| 185 | |
| 186 | char DomPrinter::ID = 0; |
| 187 | RegisterPass<DomPrinter> E("dot-dom", |
| 188 | "Print dominance tree of function " |
| 189 | "to 'dot' file"); |
| 190 | |
| 191 | char DomOnlyPrinter::ID = 0; |
| 192 | RegisterPass<DomOnlyPrinter> F("dot-dom-only", |
| 193 | "Print dominance tree of function " |
| 194 | "to 'dot' file " |
| 195 | "(with no function bodies)"); |
| 196 | |
| 197 | char PostDomPrinter::ID = 0; |
| 198 | RegisterPass<PostDomPrinter> G("dot-postdom", |
| 199 | "Print postdominance tree of function " |
| 200 | "to 'dot' file"); |
| 201 | |
| 202 | char PostDomOnlyPrinter::ID = 0; |
| 203 | RegisterPass<PostDomOnlyPrinter> H("dot-postdom-only", |
| 204 | "Print postdominance tree of function " |
| 205 | "to 'dot' file " |
| 206 | "(with no function bodies)"); |
| 207 | |
| 208 | // Create methods available outside of this file, to use them |
| 209 | // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by |
| 210 | // the link time optimization. |
| 211 | |
| 212 | FunctionPass *llvm::createDomPrinterPass() { |
| 213 | return new DomPrinter(); |
| 214 | } |
| 215 | |
| 216 | FunctionPass *llvm::createDomOnlyPrinterPass() { |
| 217 | return new DomOnlyPrinter(); |
| 218 | } |
| 219 | |
| 220 | FunctionPass *llvm::createDomViewerPass() { |
| 221 | return new DomViewer(); |
| 222 | } |
| 223 | |
| 224 | FunctionPass *llvm::createDomOnlyViewerPass() { |
| 225 | return new DomOnlyViewer(); |
| 226 | } |
| 227 | |
| 228 | FunctionPass *llvm::createPostDomPrinterPass() { |
| 229 | return new PostDomPrinter(); |
| 230 | } |
| 231 | |
| 232 | FunctionPass *llvm::createPostDomOnlyPrinterPass() { |
| 233 | return new PostDomOnlyPrinter(); |
| 234 | } |
| 235 | |
| 236 | FunctionPass *llvm::createPostDomViewerPass() { |
| 237 | return new PostDomViewer(); |
| 238 | } |
| 239 | |
| 240 | FunctionPass *llvm::createPostDomOnlyViewerPass() { |
| 241 | return new PostDomOnlyViewer(); |
| 242 | } |