Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 1 | //===- RegionPrinter.cpp - Print regions tree pass ------------------------===// |
| 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 | // Print out the region tree of a function using dotty/graphviz. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "llvm/Analysis/RegionInfo.h" |
| 13 | #include "llvm/Analysis/RegionIterator.h" |
| 14 | #include "llvm/Analysis/RegionPrinter.h" |
| 15 | #include "llvm/Analysis/Passes.h" |
| 16 | #include "llvm/Analysis/DOTGraphTraitsPass.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/ADT/PostOrderIterator.h" |
| 19 | #include "llvm/ADT/DepthFirstIterator.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/CommandLine.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | /// onlySimpleRegion - Show only the simple regions in the RegionViewer. |
| 28 | static cl::opt<bool> |
| 29 | onlySimpleRegions("only-simple-regions", |
| 30 | cl::desc("Show only simple regions in the graphviz viewer"), |
| 31 | cl::Hidden, |
| 32 | cl::init(false)); |
| 33 | |
| 34 | namespace llvm { |
| 35 | template<> |
| 36 | struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits { |
| 37 | |
| 38 | DOTGraphTraits (bool isSimple=false) |
| 39 | : DefaultDOTGraphTraits(isSimple) {} |
| 40 | |
| 41 | std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) { |
| 42 | |
| 43 | if (!Node->isSubRegion()) { |
| 44 | BasicBlock *BB = Node->getNodeAs<BasicBlock>(); |
| 45 | |
| 46 | if (isSimple()) |
| 47 | return DOTGraphTraits<const Function*> |
| 48 | ::getSimpleNodeLabel(BB, BB->getParent()); |
| 49 | else |
| 50 | return DOTGraphTraits<const Function*> |
| 51 | ::getCompleteNodeLabel(BB, BB->getParent()); |
| 52 | } |
| 53 | |
| 54 | return "Not implemented"; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | template<> |
| 59 | struct DOTGraphTraits<RegionInfo*> : public DOTGraphTraits<RegionNode*> { |
| 60 | |
| 61 | DOTGraphTraits (bool isSimple=false) |
| 62 | : DOTGraphTraits<RegionNode*>(isSimple) {} |
| 63 | |
| 64 | static std::string getGraphName(RegionInfo *DT) { |
| 65 | return "Region Graph"; |
| 66 | } |
| 67 | |
| 68 | std::string getNodeLabel(RegionNode *Node, RegionInfo *G) { |
| 69 | return DOTGraphTraits<RegionNode*>::getNodeLabel(Node, |
| 70 | G->getTopLevelRegion()); |
| 71 | } |
| 72 | |
Tobias Grosser | 3091c92 | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 73 | std::string getEdgeAttributes(RegionNode *srcNode, |
| 74 | GraphTraits<RegionInfo*>::ChildIteratorType CI, RegionInfo *RI) { |
| 75 | |
| 76 | RegionNode *destNode = *CI; |
| 77 | |
| 78 | if (srcNode->isSubRegion() || destNode->isSubRegion()) |
| 79 | return ""; |
| 80 | |
| 81 | // In case of a backedge, do not use it to define the layout of the nodes. |
| 82 | BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>(); |
| 83 | BasicBlock *destBB = destNode->getNodeAs<BasicBlock>(); |
| 84 | |
| 85 | Region *R = RI->getRegionFor(destBB); |
| 86 | |
| 87 | while (R && R->getParent()) |
| 88 | if (R->getParent()->getEntry() == destBB) |
| 89 | R = R->getParent(); |
| 90 | else |
| 91 | break; |
| 92 | |
| 93 | if (R->getEntry() == destBB && R->contains(srcBB)) |
| 94 | return "constraint=false"; |
| 95 | |
| 96 | return ""; |
| 97 | } |
| 98 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 99 | // Print the cluster of the subregions. This groups the single basic blocks |
| 100 | // and adds a different background color for each group. |
| 101 | static void printRegionCluster(const Region *R, GraphWriter<RegionInfo*> &GW, |
| 102 | unsigned depth = 0) { |
| 103 | raw_ostream &O = GW.getOStream(); |
| 104 | O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(R) |
| 105 | << " {\n"; |
| 106 | O.indent(2 * (depth + 1)) << "label = \"\";\n"; |
| 107 | |
| 108 | if (!onlySimpleRegions || R->isSimple()) { |
| 109 | O.indent(2 * (depth + 1)) << "style = filled;\n"; |
| 110 | O.indent(2 * (depth + 1)) << "color = " |
| 111 | << ((R->getDepth() * 2 % 12) + 1) << "\n"; |
| 112 | |
| 113 | } else { |
| 114 | O.indent(2 * (depth + 1)) << "style = solid;\n"; |
| 115 | O.indent(2 * (depth + 1)) << "color = " |
| 116 | << ((R->getDepth() * 2 % 12) + 2) << "\n"; |
| 117 | } |
| 118 | |
| 119 | for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI) |
| 120 | printRegionCluster(*RI, GW, depth + 1); |
| 121 | |
| 122 | RegionInfo *RI = R->getRegionInfo(); |
| 123 | |
| 124 | for (Region::const_block_iterator BI = R->block_begin(), |
Chandler Carruth | 7c52c97 | 2012-05-04 20:55:23 +0000 | [diff] [blame^] | 125 | BE = R->block_end(); BI != BE; ++BI) |
| 126 | if (RI->getRegionFor(*BI) == R) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 127 | O.indent(2 * (depth + 1)) << "Node" |
Chandler Carruth | 7c52c97 | 2012-05-04 20:55:23 +0000 | [diff] [blame^] | 128 | << static_cast<const void*>(RI->getTopLevelRegion()->getBBNode(*BI)) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 129 | << ";\n"; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 130 | |
| 131 | O.indent(2 * depth) << "}\n"; |
| 132 | } |
| 133 | |
| 134 | static void addCustomGraphFeatures(const RegionInfo* RI, |
| 135 | GraphWriter<RegionInfo*> &GW) { |
| 136 | raw_ostream &O = GW.getOStream(); |
| 137 | O << "\tcolorscheme = \"paired12\"\n"; |
| 138 | printRegionCluster(RI->getTopLevelRegion(), GW, 4); |
| 139 | } |
| 140 | }; |
| 141 | } //end namespace llvm |
| 142 | |
| 143 | namespace { |
| 144 | |
| 145 | struct RegionViewer |
| 146 | : public DOTGraphTraitsViewer<RegionInfo, false> { |
| 147 | static char ID; |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 148 | RegionViewer() : DOTGraphTraitsViewer<RegionInfo, false>("reg", ID){ |
| 149 | initializeRegionViewerPass(*PassRegistry::getPassRegistry()); |
| 150 | } |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 151 | }; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 152 | char RegionViewer::ID = 0; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 153 | |
| 154 | struct RegionOnlyViewer |
| 155 | : public DOTGraphTraitsViewer<RegionInfo, true> { |
| 156 | static char ID; |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 157 | RegionOnlyViewer() : DOTGraphTraitsViewer<RegionInfo, true>("regonly", ID) { |
| 158 | initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry()); |
| 159 | } |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 160 | }; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 161 | char RegionOnlyViewer::ID = 0; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 162 | |
| 163 | struct RegionPrinter |
| 164 | : public DOTGraphTraitsPrinter<RegionInfo, false> { |
| 165 | static char ID; |
| 166 | RegionPrinter() : |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 167 | DOTGraphTraitsPrinter<RegionInfo, false>("reg", ID) { |
| 168 | initializeRegionPrinterPass(*PassRegistry::getPassRegistry()); |
| 169 | } |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 170 | }; |
Owen Anderson | 7180234 | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 171 | char RegionPrinter::ID = 0; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 172 | } //end anonymous namespace |
| 173 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 174 | INITIALIZE_PASS(RegionPrinter, "dot-regions", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 175 | "Print regions of function to 'dot' file", true, true) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 176 | |
Owen Anderson | 7180234 | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 177 | INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 178 | true, true) |
Owen Anderson | 7180234 | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 179 | |
| 180 | INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only", |
| 181 | "View regions of function (with no function bodies)", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 182 | true, true) |
Owen Anderson | 7180234 | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 183 | |
Dan Gohman | 811edc1 | 2010-08-02 18:50:06 +0000 | [diff] [blame] | 184 | namespace { |
| 185 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 186 | struct RegionOnlyPrinter |
| 187 | : public DOTGraphTraitsPrinter<RegionInfo, true> { |
| 188 | static char ID; |
| 189 | RegionOnlyPrinter() : |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 190 | DOTGraphTraitsPrinter<RegionInfo, true>("reg", ID) { |
| 191 | initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry()); |
| 192 | } |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Dan Gohman | 811edc1 | 2010-08-02 18:50:06 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 197 | char RegionOnlyPrinter::ID = 0; |
| 198 | INITIALIZE_PASS(RegionOnlyPrinter, "dot-regions-only", |
| 199 | "Print regions of function to 'dot' file " |
| 200 | "(with no function bodies)", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 201 | true, true) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 202 | |
| 203 | FunctionPass* llvm::createRegionViewerPass() { |
| 204 | return new RegionViewer(); |
| 205 | } |
| 206 | |
| 207 | FunctionPass* llvm::createRegionOnlyViewerPass() { |
| 208 | return new RegionOnlyViewer(); |
| 209 | } |
| 210 | |
| 211 | FunctionPass* llvm::createRegionPrinterPass() { |
| 212 | return new RegionPrinter(); |
| 213 | } |
| 214 | |
| 215 | FunctionPass* llvm::createRegionOnlyPrinterPass() { |
| 216 | return new RegionOnlyPrinter(); |
| 217 | } |
| 218 | |