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