1 //===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 // Print out the region tree of a function using dotty/graphviz.
9 //===----------------------------------------------------------------------===//
11 #include "llvm/Analysis/RegionPrinter.h"
12 #include "llvm/Analysis/DOTGraphTraitsPass.h"
13 #include "llvm/Analysis/RegionInfo.h"
14 #include "llvm/Analysis/RegionIterator.h"
15 #include "llvm/InitializePasses.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/IR/LegacyPassManager.h"
24 //===----------------------------------------------------------------------===//
25 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
27 onlySimpleRegions("only-simple-regions",
28 cl::desc("Show only simple regions in the graphviz viewer"),
34 std::string DOTGraphTraits
<RegionNode
*>::getNodeLabel(RegionNode
*Node
,
36 if (!Node
->isSubRegion()) {
37 BasicBlock
*BB
= Node
->getNodeAs
<BasicBlock
>();
40 return DOTGraphTraits
<DOTFuncInfo
*>::getSimpleNodeLabel(BB
, nullptr);
42 return DOTGraphTraits
<DOTFuncInfo
*>::getCompleteNodeLabel(BB
, nullptr);
45 return "Not implemented";
49 struct DOTGraphTraits
<RegionInfo
*> : public DOTGraphTraits
<RegionNode
*> {
51 DOTGraphTraits (bool isSimple
= false)
52 : DOTGraphTraits
<RegionNode
*>(isSimple
) {}
54 static std::string
getGraphName(const RegionInfo
*) { return "Region Graph"; }
56 std::string
getNodeLabel(RegionNode
*Node
, RegionInfo
*G
) {
57 return DOTGraphTraits
<RegionNode
*>::getNodeLabel(
58 Node
, reinterpret_cast<RegionNode
*>(G
->getTopLevelRegion()));
61 std::string
getEdgeAttributes(RegionNode
*srcNode
,
62 GraphTraits
<RegionInfo
*>::ChildIteratorType CI
,
64 RegionNode
*destNode
= *CI
;
66 if (srcNode
->isSubRegion() || destNode
->isSubRegion())
69 // In case of a backedge, do not use it to define the layout of the nodes.
70 BasicBlock
*srcBB
= srcNode
->getNodeAs
<BasicBlock
>();
71 BasicBlock
*destBB
= destNode
->getNodeAs
<BasicBlock
>();
73 Region
*R
= G
->getRegionFor(destBB
);
75 while (R
&& R
->getParent())
76 if (R
->getParent()->getEntry() == destBB
)
81 if (R
&& R
->getEntry() == destBB
&& R
->contains(srcBB
))
82 return "constraint=false";
87 // Print the cluster of the subregions. This groups the single basic blocks
88 // and adds a different background color for each group.
89 static void printRegionCluster(const Region
&R
, GraphWriter
<RegionInfo
*> &GW
,
91 raw_ostream
&O
= GW
.getOStream();
92 O
.indent(2 * depth
) << "subgraph cluster_" << static_cast<const void*>(&R
)
94 O
.indent(2 * (depth
+ 1)) << "label = \"\";\n";
96 if (!onlySimpleRegions
|| R
.isSimple()) {
97 O
.indent(2 * (depth
+ 1)) << "style = filled;\n";
98 O
.indent(2 * (depth
+ 1)) << "color = "
99 << ((R
.getDepth() * 2 % 12) + 1) << "\n";
102 O
.indent(2 * (depth
+ 1)) << "style = solid;\n";
103 O
.indent(2 * (depth
+ 1)) << "color = "
104 << ((R
.getDepth() * 2 % 12) + 2) << "\n";
107 for (const auto &RI
: R
)
108 printRegionCluster(*RI
, GW
, depth
+ 1);
110 const RegionInfo
&RI
= *static_cast<const RegionInfo
*>(R
.getRegionInfo());
112 for (auto *BB
: R
.blocks())
113 if (RI
.getRegionFor(BB
) == &R
)
114 O
.indent(2 * (depth
+ 1)) << "Node"
115 << static_cast<const void*>(RI
.getTopLevelRegion()->getBBNode(BB
))
118 O
.indent(2 * depth
) << "}\n";
121 static void addCustomGraphFeatures(const RegionInfo
*G
,
122 GraphWriter
<RegionInfo
*> &GW
) {
123 raw_ostream
&O
= GW
.getOStream();
124 O
<< "\tcolorscheme = \"paired12\"\n";
125 printRegionCluster(*G
->getTopLevelRegion(), GW
, 4);
128 } // end namespace llvm
132 struct RegionInfoPassGraphTraits
{
133 static RegionInfo
*getGraph(RegionInfoPass
*RIP
) {
134 return &RIP
->getRegionInfo();
139 : public DOTGraphTraitsPrinterWrapperPass
<
140 RegionInfoPass
, false, RegionInfo
*, RegionInfoPassGraphTraits
> {
143 : DOTGraphTraitsPrinterWrapperPass
<RegionInfoPass
, false, RegionInfo
*,
144 RegionInfoPassGraphTraits
>("reg", ID
) {
145 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
148 char RegionPrinter::ID
= 0;
150 struct RegionOnlyPrinter
151 : public DOTGraphTraitsPrinterWrapperPass
<
152 RegionInfoPass
, true, RegionInfo
*, RegionInfoPassGraphTraits
> {
155 : DOTGraphTraitsPrinterWrapperPass
<RegionInfoPass
, true, RegionInfo
*,
156 RegionInfoPassGraphTraits
>("reg", ID
) {
157 initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
160 char RegionOnlyPrinter::ID
= 0;
163 : public DOTGraphTraitsViewerWrapperPass
<
164 RegionInfoPass
, false, RegionInfo
*, RegionInfoPassGraphTraits
> {
167 : DOTGraphTraitsViewerWrapperPass
<RegionInfoPass
, false, RegionInfo
*,
168 RegionInfoPassGraphTraits
>("reg", ID
) {
169 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
172 char RegionViewer::ID
= 0;
174 struct RegionOnlyViewer
175 : public DOTGraphTraitsViewerWrapperPass
<RegionInfoPass
, true, RegionInfo
*,
176 RegionInfoPassGraphTraits
> {
179 : DOTGraphTraitsViewerWrapperPass
<RegionInfoPass
, true, RegionInfo
*,
180 RegionInfoPassGraphTraits
>("regonly",
182 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
185 char RegionOnlyViewer::ID
= 0;
187 } //end anonymous namespace
189 INITIALIZE_PASS(RegionPrinter
, "dot-regions",
190 "Print regions of function to 'dot' file", true, true)
193 RegionOnlyPrinter
, "dot-regions-only",
194 "Print regions of function to 'dot' file (with no function bodies)", true,
197 INITIALIZE_PASS(RegionViewer
, "view-regions", "View regions of function",
200 INITIALIZE_PASS(RegionOnlyViewer
, "view-regions-only",
201 "View regions of function (with no function bodies)",
204 FunctionPass
*llvm::createRegionPrinterPass() { return new RegionPrinter(); }
206 FunctionPass
*llvm::createRegionOnlyPrinterPass() {
207 return new RegionOnlyPrinter();
210 FunctionPass
* llvm::createRegionViewerPass() {
211 return new RegionViewer();
214 FunctionPass
* llvm::createRegionOnlyViewerPass() {
215 return new RegionOnlyViewer();
219 static void viewRegionInfo(RegionInfo
*RI
, bool ShortNames
) {
220 assert(RI
&& "Argument must be non-null");
222 llvm::Function
*F
= RI
->getTopLevelRegion()->getEntry()->getParent();
223 std::string GraphName
= DOTGraphTraits
<RegionInfo
*>::getGraphName(RI
);
225 llvm::ViewGraph(RI
, "reg", ShortNames
,
226 Twine(GraphName
) + " for '" + F
->getName() + "' function");
229 static void invokeFunctionPass(const Function
*F
, FunctionPass
*ViewerPass
) {
230 assert(F
&& "Argument must be non-null");
231 assert(!F
->isDeclaration() && "Function must have an implementation");
233 // The viewer and analysis passes do not modify anything, so we can safely
234 // remove the const qualifier
235 auto NonConstF
= const_cast<Function
*>(F
);
237 llvm::legacy::FunctionPassManager
FPM(NonConstF
->getParent());
239 FPM
.doInitialization();
241 FPM
.doFinalization();
244 void llvm::viewRegion(RegionInfo
*RI
) { viewRegionInfo(RI
, false); }
246 void llvm::viewRegion(const Function
*F
) {
247 invokeFunctionPass(F
, createRegionViewerPass());
250 void llvm::viewRegionOnly(RegionInfo
*RI
) { viewRegionInfo(RI
, true); }
252 void llvm::viewRegionOnly(const Function
*F
) {
253 invokeFunctionPass(F
, createRegionOnlyViewerPass());