Tobias Grosser | 336734a | 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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/Passes.h" |
| 13 | #include "llvm/ADT/DepthFirstIterator.h" |
| 14 | #include "llvm/ADT/PostOrderIterator.h" |
| 15 | #include "llvm/ADT/Statistic.h" |
| 16 | #include "llvm/Analysis/DOTGraphTraitsPass.h" |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/RegionInfo.h" |
| 18 | #include "llvm/Analysis/RegionIterator.h" |
| 19 | #include "llvm/Analysis/RegionPrinter.h" |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 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 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 58 | template <> |
| 59 | struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> { |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 60 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 61 | DOTGraphTraits (bool isSimple = false) |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 62 | : DOTGraphTraits<RegionNode*>(isSimple) {} |
| 63 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 64 | static std::string getGraphName(const RegionInfo *) { return "Region Graph"; } |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 65 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 66 | std::string getNodeLabel(RegionNode *Node, RegionInfo *G) { |
| 67 | return DOTGraphTraits<RegionNode *>::getNodeLabel( |
| 68 | Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion())); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Tobias Grosser | 98eecaf | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 71 | std::string getEdgeAttributes(RegionNode *srcNode, |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 72 | GraphTraits<RegionInfo *>::ChildIteratorType CI, |
| 73 | RegionInfo *G) { |
Tobias Grosser | 98eecaf | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 74 | RegionNode *destNode = *CI; |
| 75 | |
| 76 | if (srcNode->isSubRegion() || destNode->isSubRegion()) |
| 77 | return ""; |
| 78 | |
| 79 | // In case of a backedge, do not use it to define the layout of the nodes. |
| 80 | BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>(); |
| 81 | BasicBlock *destBB = destNode->getNodeAs<BasicBlock>(); |
| 82 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 83 | Region *R = G->getRegionFor(destBB); |
Tobias Grosser | 98eecaf | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 84 | |
| 85 | while (R && R->getParent()) |
| 86 | if (R->getParent()->getEntry() == destBB) |
| 87 | R = R->getParent(); |
| 88 | else |
| 89 | break; |
| 90 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 91 | if (R && R->getEntry() == destBB && R->contains(srcBB)) |
Tobias Grosser | 98eecaf | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 92 | return "constraint=false"; |
| 93 | |
| 94 | return ""; |
| 95 | } |
| 96 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 97 | // Print the cluster of the subregions. This groups the single basic blocks |
| 98 | // and adds a different background color for each group. |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 99 | static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW, |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 100 | unsigned depth = 0) { |
| 101 | raw_ostream &O = GW.getOStream(); |
David Blaikie | ec649ac | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 102 | O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R) |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 103 | << " {\n"; |
| 104 | O.indent(2 * (depth + 1)) << "label = \"\";\n"; |
| 105 | |
David Blaikie | ec649ac | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 106 | if (!onlySimpleRegions || R.isSimple()) { |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 107 | O.indent(2 * (depth + 1)) << "style = filled;\n"; |
| 108 | O.indent(2 * (depth + 1)) << "color = " |
David Blaikie | ec649ac | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 109 | << ((R.getDepth() * 2 % 12) + 1) << "\n"; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 110 | |
| 111 | } else { |
| 112 | O.indent(2 * (depth + 1)) << "style = solid;\n"; |
| 113 | O.indent(2 * (depth + 1)) << "color = " |
David Blaikie | ec649ac | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 114 | << ((R.getDepth() * 2 % 12) + 2) << "\n"; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 115 | } |
| 116 | |
David Blaikie | ec649ac | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 117 | for (Region::const_iterator RI = R.begin(), RE = R.end(); RI != RE; ++RI) |
| 118 | printRegionCluster(**RI, GW, depth + 1); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 119 | |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 120 | const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo()); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 121 | |
Richard Trieu | a2ee301 | 2015-04-15 21:40:50 +0000 | [diff] [blame] | 122 | for (auto *BB : R.blocks()) |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 123 | if (RI.getRegionFor(BB) == &R) |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 124 | O.indent(2 * (depth + 1)) << "Node" |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 125 | << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB)) |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 126 | << ";\n"; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 127 | |
| 128 | O.indent(2 * depth) << "}\n"; |
| 129 | } |
| 130 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 131 | static void addCustomGraphFeatures(const RegionInfo *G, |
| 132 | GraphWriter<RegionInfo *> &GW) { |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 133 | raw_ostream &O = GW.getOStream(); |
| 134 | O << "\tcolorscheme = \"paired12\"\n"; |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 135 | printRegionCluster(*G->getTopLevelRegion(), GW, 4); |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 136 | } |
| 137 | }; |
| 138 | } //end namespace llvm |
| 139 | |
| 140 | namespace { |
| 141 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 142 | struct RegionInfoPassGraphTraits { |
| 143 | static RegionInfo *getGraph(RegionInfoPass *RIP) { |
| 144 | return &RIP->getRegionInfo(); |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | struct RegionPrinter |
| 149 | : public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *, |
| 150 | RegionInfoPassGraphTraits> { |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 151 | static char ID; |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 152 | RegionPrinter() |
| 153 | : DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *, |
| 154 | RegionInfoPassGraphTraits>("reg", ID) { |
| 155 | initializeRegionPrinterPass(*PassRegistry::getPassRegistry()); |
| 156 | } |
| 157 | }; |
| 158 | char RegionPrinter::ID = 0; |
| 159 | |
| 160 | struct RegionOnlyPrinter |
| 161 | : public DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *, |
| 162 | RegionInfoPassGraphTraits> { |
| 163 | static char ID; |
| 164 | RegionOnlyPrinter() |
| 165 | : DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *, |
| 166 | RegionInfoPassGraphTraits>("reg", ID) { |
| 167 | initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry()); |
| 168 | } |
| 169 | }; |
| 170 | char RegionOnlyPrinter::ID = 0; |
| 171 | |
| 172 | struct RegionViewer |
| 173 | : public DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *, |
| 174 | RegionInfoPassGraphTraits> { |
| 175 | static char ID; |
| 176 | RegionViewer() |
| 177 | : DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *, |
| 178 | RegionInfoPassGraphTraits>("reg", ID) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 179 | initializeRegionViewerPass(*PassRegistry::getPassRegistry()); |
| 180 | } |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 181 | }; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 182 | char RegionViewer::ID = 0; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 183 | |
| 184 | struct RegionOnlyViewer |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 185 | : public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *, |
| 186 | RegionInfoPassGraphTraits> { |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 187 | static char ID; |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 188 | RegionOnlyViewer() |
| 189 | : DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *, |
| 190 | RegionInfoPassGraphTraits>("regonly", ID) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 191 | initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry()); |
| 192 | } |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 193 | }; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 194 | char RegionOnlyViewer::ID = 0; |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 195 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 196 | } //end anonymous namespace |
| 197 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 198 | INITIALIZE_PASS(RegionPrinter, "dot-regions", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 199 | "Print regions of function to 'dot' file", true, true) |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 200 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 201 | INITIALIZE_PASS( |
| 202 | RegionOnlyPrinter, "dot-regions-only", |
| 203 | "Print regions of function to 'dot' file (with no function bodies)", true, |
| 204 | true) |
| 205 | |
Owen Anderson | 5e19bfc | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 206 | INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 207 | true, true) |
Matt Arsenault | 1b8d837 | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 208 | |
Owen Anderson | 5e19bfc | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 209 | INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only", |
| 210 | "View regions of function (with no function bodies)", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 211 | true, true) |
Owen Anderson | 5e19bfc | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 212 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 213 | FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); } |
Dan Gohman | abfafad | 2010-08-02 18:50:06 +0000 | [diff] [blame] | 214 | |
Michael Kruse | e838e72 | 2015-08-10 12:57:23 +0000 | [diff] [blame^] | 215 | FunctionPass *llvm::createRegionOnlyPrinterPass() { |
| 216 | return new RegionOnlyPrinter(); |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 217 | } |
Dan Gohman | abfafad | 2010-08-02 18:50:06 +0000 | [diff] [blame] | 218 | |
Tobias Grosser | 336734a | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 219 | FunctionPass* llvm::createRegionViewerPass() { |
| 220 | return new RegionViewer(); |
| 221 | } |
| 222 | |
| 223 | FunctionPass* llvm::createRegionOnlyViewerPass() { |
| 224 | return new RegionOnlyViewer(); |
| 225 | } |
| 226 | |