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 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/RegionPrinter.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DepthFirstIterator.h" |
| 14 | #include "llvm/ADT/PostOrderIterator.h" |
| 15 | #include "llvm/ADT/Statistic.h" |
| 16 | #include "llvm/Analysis/DOTGraphTraitsPass.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Passes.h" |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/RegionInfo.h" |
| 19 | #include "llvm/Analysis/RegionIterator.h" |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Michael Kruse | 5864ac7 | 2015-08-10 13:21:59 +0000 | [diff] [blame] | 23 | #ifndef NDEBUG |
| 24 | #include "llvm/IR/LegacyPassManager.h" |
| 25 | #endif |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | /// onlySimpleRegion - Show only the simple regions in the RegionViewer. |
| 31 | static cl::opt<bool> |
| 32 | onlySimpleRegions("only-simple-regions", |
| 33 | cl::desc("Show only simple regions in the graphviz viewer"), |
| 34 | cl::Hidden, |
| 35 | cl::init(false)); |
| 36 | |
| 37 | namespace llvm { |
| 38 | template<> |
| 39 | struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits { |
| 40 | |
| 41 | DOTGraphTraits (bool isSimple=false) |
| 42 | : DefaultDOTGraphTraits(isSimple) {} |
| 43 | |
| 44 | std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) { |
| 45 | |
| 46 | if (!Node->isSubRegion()) { |
| 47 | BasicBlock *BB = Node->getNodeAs<BasicBlock>(); |
| 48 | |
| 49 | if (isSimple()) |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 50 | return DOTGraphTraits<const Function*> |
| 51 | ::getSimpleNodeLabel(BB, BB->getParent()); |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 52 | else |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 53 | return DOTGraphTraits<const Function*> |
| 54 | ::getCompleteNodeLabel(BB, BB->getParent()); |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return "Not implemented"; |
| 58 | } |
| 59 | }; |
| 60 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 61 | template <> |
| 62 | struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> { |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 63 | |
Matt Arsenault | 5e1c96a | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 64 | DOTGraphTraits (bool isSimple = false) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 65 | : DOTGraphTraits<RegionNode*>(isSimple) {} |
| 66 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 67 | static std::string getGraphName(const RegionInfo *) { return "Region Graph"; } |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 68 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 69 | std::string getNodeLabel(RegionNode *Node, RegionInfo *G) { |
| 70 | return DOTGraphTraits<RegionNode *>::getNodeLabel( |
| 71 | Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion())); |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Tobias Grosser | 3091c92 | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 74 | std::string getEdgeAttributes(RegionNode *srcNode, |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 75 | GraphTraits<RegionInfo *>::ChildIteratorType CI, |
| 76 | RegionInfo *G) { |
Tobias Grosser | 3091c92 | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 77 | RegionNode *destNode = *CI; |
| 78 | |
| 79 | if (srcNode->isSubRegion() || destNode->isSubRegion()) |
| 80 | return ""; |
| 81 | |
| 82 | // In case of a backedge, do not use it to define the layout of the nodes. |
| 83 | BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>(); |
| 84 | BasicBlock *destBB = destNode->getNodeAs<BasicBlock>(); |
| 85 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 86 | Region *R = G->getRegionFor(destBB); |
Tobias Grosser | 3091c92 | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 87 | |
| 88 | while (R && R->getParent()) |
| 89 | if (R->getParent()->getEntry() == destBB) |
| 90 | R = R->getParent(); |
| 91 | else |
| 92 | break; |
| 93 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 94 | if (R && R->getEntry() == destBB && R->contains(srcBB)) |
Tobias Grosser | 3091c92 | 2011-02-27 04:11:07 +0000 | [diff] [blame] | 95 | return "constraint=false"; |
| 96 | |
| 97 | return ""; |
| 98 | } |
| 99 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 100 | // Print the cluster of the subregions. This groups the single basic blocks |
| 101 | // and adds a different background color for each group. |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 102 | static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW, |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 103 | unsigned depth = 0) { |
| 104 | raw_ostream &O = GW.getOStream(); |
David Blaikie | 2bbc5a7 | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 105 | O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 106 | << " {\n"; |
| 107 | O.indent(2 * (depth + 1)) << "label = \"\";\n"; |
| 108 | |
David Blaikie | 2bbc5a7 | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 109 | if (!onlySimpleRegions || R.isSimple()) { |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 110 | O.indent(2 * (depth + 1)) << "style = filled;\n"; |
| 111 | O.indent(2 * (depth + 1)) << "color = " |
David Blaikie | 2bbc5a7 | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 112 | << ((R.getDepth() * 2 % 12) + 1) << "\n"; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 113 | |
| 114 | } else { |
| 115 | O.indent(2 * (depth + 1)) << "style = solid;\n"; |
| 116 | O.indent(2 * (depth + 1)) << "color = " |
David Blaikie | 2bbc5a7 | 2014-04-15 18:32:43 +0000 | [diff] [blame] | 117 | << ((R.getDepth() * 2 % 12) + 2) << "\n"; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Benjamin Kramer | 8d0d2b6 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 120 | for (const auto &RI : R) |
| 121 | printRegionCluster(*RI, GW, depth + 1); |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 122 | |
Matt Arsenault | 5e1c96a | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 123 | const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo()); |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 124 | |
Richard Trieu | f422262 | 2015-04-15 21:40:50 +0000 | [diff] [blame] | 125 | for (auto *BB : R.blocks()) |
Matt Arsenault | 5e1c96a | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 126 | if (RI.getRegionFor(BB) == &R) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 127 | O.indent(2 * (depth + 1)) << "Node" |
Matt Arsenault | 5e1c96a | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 128 | << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB)) |
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 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 134 | static void addCustomGraphFeatures(const RegionInfo *G, |
| 135 | GraphWriter<RegionInfo *> &GW) { |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 136 | raw_ostream &O = GW.getOStream(); |
| 137 | O << "\tcolorscheme = \"paired12\"\n"; |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 138 | printRegionCluster(*G->getTopLevelRegion(), GW, 4); |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 139 | } |
| 140 | }; |
| 141 | } //end namespace llvm |
| 142 | |
| 143 | namespace { |
| 144 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 145 | struct RegionInfoPassGraphTraits { |
| 146 | static RegionInfo *getGraph(RegionInfoPass *RIP) { |
| 147 | return &RIP->getRegionInfo(); |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | struct RegionPrinter |
| 152 | : public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *, |
| 153 | RegionInfoPassGraphTraits> { |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 154 | static char ID; |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 155 | RegionPrinter() |
| 156 | : DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *, |
| 157 | RegionInfoPassGraphTraits>("reg", ID) { |
| 158 | initializeRegionPrinterPass(*PassRegistry::getPassRegistry()); |
| 159 | } |
| 160 | }; |
| 161 | char RegionPrinter::ID = 0; |
| 162 | |
| 163 | struct RegionOnlyPrinter |
| 164 | : public DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *, |
| 165 | RegionInfoPassGraphTraits> { |
| 166 | static char ID; |
| 167 | RegionOnlyPrinter() |
| 168 | : DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *, |
| 169 | RegionInfoPassGraphTraits>("reg", ID) { |
| 170 | initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry()); |
| 171 | } |
| 172 | }; |
| 173 | char RegionOnlyPrinter::ID = 0; |
| 174 | |
| 175 | struct RegionViewer |
| 176 | : public DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *, |
| 177 | RegionInfoPassGraphTraits> { |
| 178 | static char ID; |
| 179 | RegionViewer() |
| 180 | : DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *, |
| 181 | RegionInfoPassGraphTraits>("reg", ID) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 182 | initializeRegionViewerPass(*PassRegistry::getPassRegistry()); |
| 183 | } |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 184 | }; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 185 | char RegionViewer::ID = 0; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 186 | |
| 187 | struct RegionOnlyViewer |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 188 | : public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *, |
| 189 | RegionInfoPassGraphTraits> { |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 190 | static char ID; |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 191 | RegionOnlyViewer() |
| 192 | : DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *, |
| 193 | RegionInfoPassGraphTraits>("regonly", ID) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 194 | initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry()); |
| 195 | } |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 196 | }; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 197 | char RegionOnlyViewer::ID = 0; |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 198 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 199 | } //end anonymous namespace |
| 200 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 201 | INITIALIZE_PASS(RegionPrinter, "dot-regions", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 202 | "Print regions of function to 'dot' file", true, true) |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 203 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 204 | INITIALIZE_PASS( |
| 205 | RegionOnlyPrinter, "dot-regions-only", |
| 206 | "Print regions of function to 'dot' file (with no function bodies)", true, |
| 207 | true) |
| 208 | |
Owen Anderson | 7180234 | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 209 | INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 210 | true, true) |
Matt Arsenault | 5e1c96a | 2014-07-19 18:29:29 +0000 | [diff] [blame] | 211 | |
Owen Anderson | 7180234 | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 212 | INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only", |
| 213 | "View regions of function (with no function bodies)", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 214 | true, true) |
Owen Anderson | 7180234 | 2010-10-07 04:13:08 +0000 | [diff] [blame] | 215 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 216 | FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); } |
Dan Gohman | 811edc1 | 2010-08-02 18:50:06 +0000 | [diff] [blame] | 217 | |
Michael Kruse | 6b23651 | 2015-08-10 12:57:23 +0000 | [diff] [blame] | 218 | FunctionPass *llvm::createRegionOnlyPrinterPass() { |
| 219 | return new RegionOnlyPrinter(); |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 220 | } |
Dan Gohman | 811edc1 | 2010-08-02 18:50:06 +0000 | [diff] [blame] | 221 | |
Tobias Grosser | f96b006 | 2010-07-22 07:46:31 +0000 | [diff] [blame] | 222 | FunctionPass* llvm::createRegionViewerPass() { |
| 223 | return new RegionViewer(); |
| 224 | } |
| 225 | |
| 226 | FunctionPass* llvm::createRegionOnlyViewerPass() { |
| 227 | return new RegionOnlyViewer(); |
| 228 | } |
| 229 | |
Michael Kruse | 5864ac7 | 2015-08-10 13:21:59 +0000 | [diff] [blame] | 230 | #ifndef NDEBUG |
| 231 | static void viewRegionInfo(RegionInfo *RI, bool ShortNames) { |
| 232 | assert(RI && "Argument must be non-null"); |
| 233 | |
| 234 | llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent(); |
| 235 | std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI); |
| 236 | |
| 237 | llvm::ViewGraph(RI, "reg", ShortNames, |
| 238 | Twine(GraphName) + " for '" + F->getName() + "' function"); |
| 239 | } |
| 240 | |
| 241 | static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) { |
| 242 | assert(F && "Argument must be non-null"); |
| 243 | assert(!F->isDeclaration() && "Function must have an implementation"); |
| 244 | |
| 245 | // The viewer and analysis passes do not modify anything, so we can safely |
| 246 | // remove the const qualifier |
| 247 | auto NonConstF = const_cast<Function *>(F); |
| 248 | |
| 249 | llvm::legacy::FunctionPassManager FPM(NonConstF->getParent()); |
| 250 | FPM.add(ViewerPass); |
| 251 | FPM.doInitialization(); |
| 252 | FPM.run(*NonConstF); |
| 253 | FPM.doFinalization(); |
| 254 | } |
| 255 | |
| 256 | void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); } |
| 257 | |
| 258 | void llvm::viewRegion(const Function *F) { |
| 259 | invokeFunctionPass(F, createRegionViewerPass()); |
| 260 | } |
| 261 | |
| 262 | void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); } |
| 263 | |
| 264 | void llvm::viewRegionOnly(const Function *F) { |
| 265 | invokeFunctionPass(F, createRegionOnlyViewerPass()); |
| 266 | } |
| 267 | #endif |