LLVM 22.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
18#ifndef NDEBUG
20#endif
21
22using namespace llvm;
23
24//===----------------------------------------------------------------------===//
25/// onlySimpleRegion - Show only the simple regions in the RegionViewer.
26static cl::opt<bool>
27onlySimpleRegions("only-simple-regions",
28 cl::desc("Show only simple regions in the graphviz viewer"),
30 cl::init(false));
31
32namespace llvm {
33
35 RegionNode *Graph) {
36 if (!Node->isSubRegion()) {
37 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
38
39 if (isSimple())
41 else
43 }
44
45 return "Not implemented";
46}
47
48template <>
50
51 DOTGraphTraits (bool isSimple = false)
53
54 static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
55
56 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
58 Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
59 }
60
61 std::string getEdgeAttributes(RegionNode *srcNode,
63 RegionInfo *G) {
64 RegionNode *destNode = *CI;
65
66 if (srcNode->isSubRegion() || destNode->isSubRegion())
67 return "";
68
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>();
72
73 Region *R = G->getRegionFor(destBB);
74
75 while (R && R->getParent())
76 if (R->getParent()->getEntry() == destBB)
77 R = R->getParent();
78 else
79 break;
80
81 if (R && R->getEntry() == destBB && R->contains(srcBB))
82 return "constraint=false";
83
84 return "";
85 }
86
87 // Print the cluster of the subregions. This groups the single basic blocks
88 // and adds a different background color for each group.
90 unsigned depth = 0) {
91 raw_ostream &O = GW.getOStream();
92 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
93 << " {\n";
94 O.indent(2 * (depth + 1)) << "label = \"\";\n";
95
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";
100
101 } else {
102 O.indent(2 * (depth + 1)) << "style = solid;\n";
103 O.indent(2 * (depth + 1)) << "color = "
104 << ((R.getDepth() * 2 % 12) + 2) << "\n";
105 }
106
107 for (const auto &RI : R)
108 printRegionCluster(*RI, GW, depth + 1);
109
110 const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
111
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))
116 << ";\n";
117
118 O.indent(2 * depth) << "}\n";
119 }
120
123 raw_ostream &O = GW.getOStream();
124 O << "\tcolorscheme = \"paired12\"\n";
125 printRegionCluster(*G->getTopLevelRegion(), GW, 4);
126 }
127};
128} // end namespace llvm
129
130namespace {
131
132struct RegionInfoPassGraphTraits {
133 static RegionInfo *getGraph(RegionInfoPass *RIP) {
134 return &RIP->getRegionInfo();
135 }
136};
137
138struct RegionPrinter
140 RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
141 static char ID;
142 RegionPrinter()
144 RegionInfoPassGraphTraits>("reg", ID) {
145 }
146};
147char RegionPrinter::ID = 0;
148
149struct RegionOnlyPrinter
151 RegionInfoPass, true, RegionInfo *, RegionInfoPassGraphTraits> {
152 static char ID;
153 RegionOnlyPrinter()
155 RegionInfoPassGraphTraits>("reg", ID) {
156 }
157};
158char RegionOnlyPrinter::ID = 0;
159
160struct RegionViewer
162 RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
163 static char ID;
164 RegionViewer()
166 RegionInfoPassGraphTraits>("reg", ID) {}
167};
168char RegionViewer::ID = 0;
169
170struct RegionOnlyViewer
171 : public DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
172 RegionInfoPassGraphTraits> {
173 static char ID;
174 RegionOnlyViewer()
176 RegionInfoPassGraphTraits>("regonly",
177 ID) {}
178};
179char RegionOnlyViewer::ID = 0;
180
181} //end anonymous namespace
182
183INITIALIZE_PASS(RegionPrinter, "dot-regions",
184 "Print regions of function to 'dot' file", true, true)
185
187 RegionOnlyPrinter, "dot-regions-only",
188 "Print regions of function to 'dot' file (with no function bodies)", true,
189 true)
190
191INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
192 true, true)
193
194INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
195 "View regions of function (with no function bodies)",
196 true, true)
197
198FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
199
201 return new RegionOnlyPrinter();
202}
203
205 return new RegionViewer();
206}
207
209 return new RegionOnlyViewer();
210}
211
212#ifndef NDEBUG
213static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
214 assert(RI && "Argument must be non-null");
215
216 llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
217 std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
218
219 llvm::ViewGraph(RI, "reg", ShortNames,
220 Twine(GraphName) + " for '" + F->getName() + "' function");
221}
222
223static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
224 assert(F && "Argument must be non-null");
225 assert(!F->isDeclaration() && "Function must have an implementation");
226
227 // The viewer and analysis passes do not modify anything, so we can safely
228 // remove the const qualifier
229 auto NonConstF = const_cast<Function *>(F);
230
231 llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
232 FPM.add(ViewerPass);
233 FPM.doInitialization();
234 FPM.run(*NonConstF);
235 FPM.doFinalization();
236}
237
239
242}
243
245
248}
249#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Performs the initial survey of the specified function
#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:56
regions
Definition: RegionInfo.cpp:166
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)
static bool isSimple(Instruction *I)
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
raw_ostream & getOStream()
getOStream - Get the raw output stream into the graph file.
Definition: GraphWriter.h:362
RegionT * getTopLevelRegion() const
Definition: RegionInfo.h:864
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
RegionInfo & getRegionInfo()
Definition: RegionInfo.h:949
bool isSubRegion() const
Is this RegionNode a subregion?
Definition: RegionInfo.h:186
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:82
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:53
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:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI FunctionPass * createRegionOnlyViewerPass()
LLVM_ABI FunctionPass * createRegionPrinterPass()
LLVM_ABI 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:443
LLVM_ABI FunctionPass * createRegionViewerPass()
void viewRegion(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
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...