blob: 5bdcb31fbe99316af61d15e5daa9c86f275d6372 [file] [log] [blame]
Tobias Grosser336734a2010-07-22 07:46:31 +00001//===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Grosser336734a2010-07-22 07:46:31 +00006//
7//===----------------------------------------------------------------------===//
8// Print out the region tree of a function using dotty/graphviz.
9//===----------------------------------------------------------------------===//
10
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "llvm/Analysis/RegionPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/DepthFirstIterator.h"
13#include "llvm/ADT/PostOrderIterator.h"
14#include "llvm/ADT/Statistic.h"
15#include "llvm/Analysis/DOTGraphTraitsPass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/Analysis/Passes.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000017#include "llvm/Analysis/RegionInfo.h"
18#include "llvm/Analysis/RegionIterator.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000019#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/Debug.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000021#include "llvm/Support/raw_ostream.h"
Michael Kruse20dcc9f2015-08-10 13:21:59 +000022#ifndef NDEBUG
23#include "llvm/IR/LegacyPassManager.h"
24#endif
Tobias Grosser336734a2010-07-22 07:46:31 +000025
26using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29/// onlySimpleRegion - Show only the simple regions in the RegionViewer.
30static cl::opt<bool>
31onlySimpleRegions("only-simple-regions",
32 cl::desc("Show only simple regions in the graphviz viewer"),
33 cl::Hidden,
34 cl::init(false));
35
36namespace llvm {
37template<>
38struct 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 Fertilecd0d7632018-06-29 17:48:58 +000049 return DOTGraphTraits<const Function*>
50 ::getSimpleNodeLabel(BB, BB->getParent());
Tobias Grosser336734a2010-07-22 07:46:31 +000051 else
Sean Fertilecd0d7632018-06-29 17:48:58 +000052 return DOTGraphTraits<const Function*>
53 ::getCompleteNodeLabel(BB, BB->getParent());
Tobias Grosser336734a2010-07-22 07:46:31 +000054 }
55
56 return "Not implemented";
57 }
58};
59
Michael Krusee838e722015-08-10 12:57:23 +000060template <>
61struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {
Tobias Grosser336734a2010-07-22 07:46:31 +000062
Matt Arsenault1b8d8372014-07-19 18:29:29 +000063 DOTGraphTraits (bool isSimple = false)
Tobias Grosser336734a2010-07-22 07:46:31 +000064 : DOTGraphTraits<RegionNode*>(isSimple) {}
65
Michael Krusee838e722015-08-10 12:57:23 +000066 static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
Tobias Grosser336734a2010-07-22 07:46:31 +000067
Michael Krusee838e722015-08-10 12:57:23 +000068 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
69 return DOTGraphTraits<RegionNode *>::getNodeLabel(
70 Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
Tobias Grosser336734a2010-07-22 07:46:31 +000071 }
72
Tobias Grosser98eecaf2011-02-27 04:11:07 +000073 std::string getEdgeAttributes(RegionNode *srcNode,
Michael Krusee838e722015-08-10 12:57:23 +000074 GraphTraits<RegionInfo *>::ChildIteratorType CI,
75 RegionInfo *G) {
Tobias Grosser98eecaf2011-02-27 04:11:07 +000076 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 Krusee838e722015-08-10 12:57:23 +000085 Region *R = G->getRegionFor(destBB);
Tobias Grosser98eecaf2011-02-27 04:11:07 +000086
87 while (R && R->getParent())
88 if (R->getParent()->getEntry() == destBB)
89 R = R->getParent();
90 else
91 break;
92
Michael Krusee838e722015-08-10 12:57:23 +000093 if (R && R->getEntry() == destBB && R->contains(srcBB))
Tobias Grosser98eecaf2011-02-27 04:11:07 +000094 return "constraint=false";
95
96 return "";
97 }
98
Tobias Grosser336734a2010-07-22 07:46:31 +000099 // Print the cluster of the subregions. This groups the single basic blocks
100 // and adds a different background color for each group.
Michael Krusee838e722015-08-10 12:57:23 +0000101 static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW,
Tobias Grosser336734a2010-07-22 07:46:31 +0000102 unsigned depth = 0) {
103 raw_ostream &O = GW.getOStream();
David Blaikieec649ac2014-04-15 18:32:43 +0000104 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
Tobias Grosser336734a2010-07-22 07:46:31 +0000105 << " {\n";
106 O.indent(2 * (depth + 1)) << "label = \"\";\n";
107
David Blaikieec649ac2014-04-15 18:32:43 +0000108 if (!onlySimpleRegions || R.isSimple()) {
Tobias Grosser336734a2010-07-22 07:46:31 +0000109 O.indent(2 * (depth + 1)) << "style = filled;\n";
110 O.indent(2 * (depth + 1)) << "color = "
David Blaikieec649ac2014-04-15 18:32:43 +0000111 << ((R.getDepth() * 2 % 12) + 1) << "\n";
Tobias Grosser336734a2010-07-22 07:46:31 +0000112
113 } else {
114 O.indent(2 * (depth + 1)) << "style = solid;\n";
115 O.indent(2 * (depth + 1)) << "color = "
David Blaikieec649ac2014-04-15 18:32:43 +0000116 << ((R.getDepth() * 2 % 12) + 2) << "\n";
Tobias Grosser336734a2010-07-22 07:46:31 +0000117 }
118
Benjamin Krameraa209152016-06-26 17:27:42 +0000119 for (const auto &RI : R)
120 printRegionCluster(*RI, GW, depth + 1);
Tobias Grosser336734a2010-07-22 07:46:31 +0000121
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000122 const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
Tobias Grosser336734a2010-07-22 07:46:31 +0000123
Richard Trieua2ee3012015-04-15 21:40:50 +0000124 for (auto *BB : R.blocks())
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000125 if (RI.getRegionFor(BB) == &R)
Tobias Grosser336734a2010-07-22 07:46:31 +0000126 O.indent(2 * (depth + 1)) << "Node"
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000127 << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
Tobias Grosser336734a2010-07-22 07:46:31 +0000128 << ";\n";
Tobias Grosser336734a2010-07-22 07:46:31 +0000129
130 O.indent(2 * depth) << "}\n";
131 }
132
Michael Krusee838e722015-08-10 12:57:23 +0000133 static void addCustomGraphFeatures(const RegionInfo *G,
134 GraphWriter<RegionInfo *> &GW) {
Tobias Grosser336734a2010-07-22 07:46:31 +0000135 raw_ostream &O = GW.getOStream();
136 O << "\tcolorscheme = \"paired12\"\n";
Michael Krusee838e722015-08-10 12:57:23 +0000137 printRegionCluster(*G->getTopLevelRegion(), GW, 4);
Tobias Grosser336734a2010-07-22 07:46:31 +0000138 }
139};
140} //end namespace llvm
141
142namespace {
143
Michael Krusee838e722015-08-10 12:57:23 +0000144struct RegionInfoPassGraphTraits {
145 static RegionInfo *getGraph(RegionInfoPass *RIP) {
146 return &RIP->getRegionInfo();
147 }
148};
149
150struct RegionPrinter
151 : public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
152 RegionInfoPassGraphTraits> {
Tobias Grosser336734a2010-07-22 07:46:31 +0000153 static char ID;
Michael Krusee838e722015-08-10 12:57:23 +0000154 RegionPrinter()
155 : DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
156 RegionInfoPassGraphTraits>("reg", ID) {
157 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
158 }
159};
160char RegionPrinter::ID = 0;
161
162struct 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};
172char RegionOnlyPrinter::ID = 0;
173
174struct 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 Anderson6c18d1a2010-10-19 17:21:58 +0000181 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
182 }
Tobias Grosser336734a2010-07-22 07:46:31 +0000183};
Tobias Grosser336734a2010-07-22 07:46:31 +0000184char RegionViewer::ID = 0;
Tobias Grosser336734a2010-07-22 07:46:31 +0000185
186struct RegionOnlyViewer
Michael Krusee838e722015-08-10 12:57:23 +0000187 : public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
188 RegionInfoPassGraphTraits> {
Tobias Grosser336734a2010-07-22 07:46:31 +0000189 static char ID;
Michael Krusee838e722015-08-10 12:57:23 +0000190 RegionOnlyViewer()
191 : DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
192 RegionInfoPassGraphTraits>("regonly", ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000193 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
194 }
Tobias Grosser336734a2010-07-22 07:46:31 +0000195};
Tobias Grosser336734a2010-07-22 07:46:31 +0000196char RegionOnlyViewer::ID = 0;
Tobias Grosser336734a2010-07-22 07:46:31 +0000197
Tobias Grosser336734a2010-07-22 07:46:31 +0000198} //end anonymous namespace
199
Tobias Grosser336734a2010-07-22 07:46:31 +0000200INITIALIZE_PASS(RegionPrinter, "dot-regions",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000201 "Print regions of function to 'dot' file", true, true)
Tobias Grosser336734a2010-07-22 07:46:31 +0000202
Michael Krusee838e722015-08-10 12:57:23 +0000203INITIALIZE_PASS(
204 RegionOnlyPrinter, "dot-regions-only",
205 "Print regions of function to 'dot' file (with no function bodies)", true,
206 true)
207
Owen Anderson5e19bfc2010-10-07 04:13:08 +0000208INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000209 true, true)
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000210
Owen Anderson5e19bfc2010-10-07 04:13:08 +0000211INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
212 "View regions of function (with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000213 true, true)
Owen Anderson5e19bfc2010-10-07 04:13:08 +0000214
Michael Krusee838e722015-08-10 12:57:23 +0000215FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
Dan Gohmanabfafad2010-08-02 18:50:06 +0000216
Michael Krusee838e722015-08-10 12:57:23 +0000217FunctionPass *llvm::createRegionOnlyPrinterPass() {
218 return new RegionOnlyPrinter();
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000219}
Dan Gohmanabfafad2010-08-02 18:50:06 +0000220
Tobias Grosser336734a2010-07-22 07:46:31 +0000221FunctionPass* llvm::createRegionViewerPass() {
222 return new RegionViewer();
223}
224
225FunctionPass* llvm::createRegionOnlyViewerPass() {
226 return new RegionOnlyViewer();
227}
228
Michael Kruse20dcc9f2015-08-10 13:21:59 +0000229#ifndef NDEBUG
230static 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
240static 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
255void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
256
257void llvm::viewRegion(const Function *F) {
258 invokeFunctionPass(F, createRegionViewerPass());
259}
260
261void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
262
263void llvm::viewRegionOnly(const Function *F) {
264 invokeFunctionPass(F, createRegionOnlyViewerPass());
265}
266#endif