LLVM 18.0.0git
RegionPrinter.cpp
Go to the documentation of this file.
1//===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8// Print out the region tree of a function using dotty/graphviz.
9//===----------------------------------------------------------------------===//
10
19#ifndef NDEBUG
21#endif
22
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// onlySimpleRegion - Show only the simple regions in the RegionViewer.
27static cl::opt<bool>
28onlySimpleRegions("only-simple-regions",
29 cl::desc("Show only simple regions in the graphviz viewer"),
31 cl::init(false));
32
33namespace llvm {
34
36 RegionNode *Graph) {
37 if (!Node->isSubRegion()) {
38 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
39
40 if (isSimple())
42 else
44 }
45
46 return "Not implemented";
47}
48
49template <>
51
52 DOTGraphTraits (bool isSimple = false)
54
55 static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
56
57 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
59 Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
60 }
61
62 std::string getEdgeAttributes(RegionNode *srcNode,
64 RegionInfo *G) {
65 RegionNode *destNode = *CI;
66
67 if (srcNode->isSubRegion() || destNode->isSubRegion())
68 return "";
69
70 // In case of a backedge, do not use it to define the layout of the nodes.
71 BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
72 BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
73
74 Region *R = G->getRegionFor(destBB);
75
76 while (R && R->getParent())
77 if (R->getParent()->getEntry() == destBB)
78 R = R->getParent();
79 else
80 break;
81
82 if (R && R->getEntry() == destBB && R->contains(srcBB))
83 return "constraint=false";
84
85 return "";
86 }
87
88 // Print the cluster of the subregions. This groups the single basic blocks
89 // and adds a different background color for each group.
91 unsigned depth = 0) {
92 raw_ostream &O = GW.getOStream();
93 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
94 << " {\n";
95 O.indent(2 * (depth + 1)) << "label = \"\";\n";
96
97 if (!onlySimpleRegions || R.isSimple()) {
98 O.indent(2 * (depth + 1)) << "style = filled;\n";
99 O.indent(2 * (depth + 1)) << "color = "
100 << ((R.getDepth() * 2 % 12) + 1) << "\n";
101
102 } else {
103 O.indent(2 * (depth + 1)) << "style = solid;\n";
104 O.indent(2 * (depth + 1)) << "color = "
105 << ((R.getDepth() * 2 % 12) + 2) << "\n";
106 }
107
108 for (const auto &RI : R)
109 printRegionCluster(*RI, GW, depth + 1);
110
111 const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
112
113 for (auto *BB : R.blocks())
114 if (RI.getRegionFor(BB) == &R)
115 O.indent(2 * (depth + 1)) << "Node"
116 << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
117 << ";\n";
118
119 O.indent(2 * depth) << "}\n";
120 }
121
124 raw_ostream &O = GW.getOStream();
125 O << "\tcolorscheme = \"paired12\"\n";
126 printRegionCluster(*G->getTopLevelRegion(), GW, 4);
127 }
128};
129} // end namespace llvm
130
131namespace {
132
133struct RegionInfoPassGraphTraits {
134 static RegionInfo *getGraph(RegionInfoPass *RIP) {
135 return &RIP->getRegionInfo();
136 }
137};
138
139struct RegionPrinter
141 RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
142 static char ID;
143 RegionPrinter()
145 RegionInfoPassGraphTraits>("reg", ID) {
147 }
148};
149char RegionPrinter::ID = 0;
150
151struct RegionOnlyPrinter
153 RegionInfoPass, true, RegionInfo *, RegionInfoPassGraphTraits> {
154 static char ID;
155 RegionOnlyPrinter()
157 RegionInfoPassGraphTraits>("reg", ID) {
159 }
160};
161char RegionOnlyPrinter::ID = 0;
162
163struct RegionViewer
165 RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
166 static char ID;
167 RegionViewer()
169 RegionInfoPassGraphTraits>("reg", ID) {
171 }
172};
173char RegionViewer::ID = 0;
174
175struct RegionOnlyViewer
176 : public DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
177 RegionInfoPassGraphTraits> {
178 static char ID;
179 RegionOnlyViewer()
181 RegionInfoPassGraphTraits>("regonly",
182 ID) {
184 }
185};
186char RegionOnlyViewer::ID = 0;
187
188} //end anonymous namespace
189
190INITIALIZE_PASS(RegionPrinter, "dot-regions",
191 "Print regions of function to 'dot' file", true, true)
192
194 RegionOnlyPrinter, "dot-regions-only",
195 "Print regions of function to 'dot' file (with no function bodies)", true,
196 true)
197
198INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
199 true, true)
200
201INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
202 "View regions of function (with no function bodies)",
203 true, true)
204
205FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
206
208 return new RegionOnlyPrinter();
209}
210
212 return new RegionViewer();
213}
214
216 return new RegionOnlyViewer();
217}
218
219#ifndef NDEBUG
220static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
221 assert(RI && "Argument must be non-null");
222
223 llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
224 std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
225
226 llvm::ViewGraph(RI, "reg", ShortNames,
227 Twine(GraphName) + " for '" + F->getName() + "' function");
228}
229
230static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
231 assert(F && "Argument must be non-null");
232 assert(!F->isDeclaration() && "Function must have an implementation");
233
234 // The viewer and analysis passes do not modify anything, so we can safely
235 // remove the const qualifier
236 auto NonConstF = const_cast<Function *>(F);
237
238 llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
239 FPM.add(ViewerPass);
240 FPM.doInitialization();
241 FPM.run(*NonConstF);
242 FPM.doFinalization();
243}
244
246
249}
250
252
255}
256#endif
basic Basic Alias true
Performs the initial survey of the specified function
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
#define F(x, y, z)
Definition: MD5.cpp:55
#define G(x, y, z)
Definition: MD5.cpp:56
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
regions
Definition: RegionInfo.cpp:167
static void viewRegionInfo(RegionInfo *RI, bool ShortNames)
dot regions only
dot regions Print regions of function to dot file(with no function bodies)"
static cl::opt< bool > onlySimpleRegions("only-simple-regions", cl::desc("Show only simple regions in the graphviz viewer"), cl::Hidden, cl::init(false))
onlySimpleRegion - Show only the simple regions in the RegionViewer.
static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass)
dot regions Print regions of function to dot true
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isSimple(Instruction *I)
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
raw_ostream & getOStream()
getOStream - Get the raw output stream into the graph file.
Definition: GraphWriter.h:353
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
RegionT * getTopLevelRegion() const
Definition: RegionInfo.h:866
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
RegionInfo & getRegionInfo()
Definition: RegionInfo.h:951
bool isSubRegion() const
Is this RegionNode a subregion?
Definition: RegionInfo.h:188
T * getNodeAs() const
Get the content of this RegionNode.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
FunctionPassManager manages FunctionPasses.
bool run(Function &F)
run - Execute all of the passes scheduled for execution.
void add(Pass *P) override
Add a pass to the queue of passes to run.
bool doInitialization()
doInitialization - Run all of the initializers for the function passes.
bool doFinalization()
doFinalization - Run all of the finalizers for the function passes.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
TargetPassConfig.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeRegionViewerPass(PassRegistry &)
FunctionPass * createRegionOnlyViewerPass()
FunctionPass * createRegionPrinterPass()
void initializeRegionOnlyPrinterPass(PassRegistry &)
void initializeRegionOnlyViewerPass(PassRegistry &)
FunctionPass * createRegionOnlyPrinterPass()
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
Definition: GraphWriter.h:427
FunctionPass * createRegionViewerPass()
void viewRegion(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
void initializeRegionPrinterPass(PassRegistry &)
void viewRegionOnly(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
static void addCustomGraphFeatures(const RegionInfo *G, GraphWriter< RegionInfo * > &GW)
std::string getNodeLabel(RegionNode *Node, RegionInfo *G)
std::string getEdgeAttributes(RegionNode *srcNode, GraphTraits< RegionInfo * >::ChildIteratorType CI, RegionInfo *G)
static void printRegionCluster(const Region &R, GraphWriter< RegionInfo * > &GW, unsigned depth=0)
static std::string getGraphName(const RegionInfo *)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
std::string getNodeLabel(const void *, const GraphType &)
getNodeLabel - Given a node and a pointer to the top level graph, return the label to print in the no...