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