[RISCV] Refactor predicates for rvv intrinsic patterns.
[llvm-project.git] / llvm / lib / Analysis / RegionPrinter.cpp
blobfbd3d17febff4870361cd13411077ef5bab4adf4
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 //===----------------------------------------------------------------------===//
11 #include "llvm/Analysis/RegionPrinter.h"
12 #include "llvm/ADT/DepthFirstIterator.h"
13 #include "llvm/Analysis/DOTGraphTraitsPass.h"
14 #include "llvm/Analysis/RegionInfo.h"
15 #include "llvm/Analysis/RegionIterator.h"
16 #include "llvm/InitializePasses.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/raw_ostream.h"
19 #ifndef NDEBUG
20 #include "llvm/IR/LegacyPassManager.h"
21 #endif
23 using namespace llvm;
25 //===----------------------------------------------------------------------===//
26 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
27 static cl::opt<bool>
28 onlySimpleRegions("only-simple-regions",
29 cl::desc("Show only simple regions in the graphviz viewer"),
30 cl::Hidden,
31 cl::init(false));
33 namespace llvm {
35 std::string DOTGraphTraits<RegionNode *>::getNodeLabel(RegionNode *Node,
36 RegionNode *Graph) {
37 if (!Node->isSubRegion()) {
38 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
40 if (isSimple())
41 return DOTGraphTraits<DOTFuncInfo *>::getSimpleNodeLabel(BB, nullptr);
42 else
43 return DOTGraphTraits<DOTFuncInfo *>::getCompleteNodeLabel(BB, nullptr);
46 return "Not implemented";
49 template <>
50 struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {
52 DOTGraphTraits (bool isSimple = false)
53 : DOTGraphTraits<RegionNode*>(isSimple) {}
55 static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
57 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
58 return DOTGraphTraits<RegionNode *>::getNodeLabel(
59 Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
62 std::string getEdgeAttributes(RegionNode *srcNode,
63 GraphTraits<RegionInfo *>::ChildIteratorType CI,
64 RegionInfo *G) {
65 RegionNode *destNode = *CI;
67 if (srcNode->isSubRegion() || destNode->isSubRegion())
68 return "";
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>();
74 Region *R = G->getRegionFor(destBB);
76 while (R && R->getParent())
77 if (R->getParent()->getEntry() == destBB)
78 R = R->getParent();
79 else
80 break;
82 if (R && R->getEntry() == destBB && R->contains(srcBB))
83 return "constraint=false";
85 return "";
88 // Print the cluster of the subregions. This groups the single basic blocks
89 // and adds a different background color for each group.
90 static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW,
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";
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";
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";
108 for (const auto &RI : R)
109 printRegionCluster(*RI, GW, depth + 1);
111 const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
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";
119 O.indent(2 * depth) << "}\n";
122 static void addCustomGraphFeatures(const RegionInfo *G,
123 GraphWriter<RegionInfo *> &GW) {
124 raw_ostream &O = GW.getOStream();
125 O << "\tcolorscheme = \"paired12\"\n";
126 printRegionCluster(*G->getTopLevelRegion(), GW, 4);
129 } // end namespace llvm
131 namespace {
133 struct RegionInfoPassGraphTraits {
134 static RegionInfo *getGraph(RegionInfoPass *RIP) {
135 return &RIP->getRegionInfo();
139 struct RegionPrinter
140 : public DOTGraphTraitsPrinterWrapperPass<
141 RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
142 static char ID;
143 RegionPrinter()
144 : DOTGraphTraitsPrinterWrapperPass<RegionInfoPass, false, RegionInfo *,
145 RegionInfoPassGraphTraits>("reg", ID) {
146 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
149 char RegionPrinter::ID = 0;
151 struct RegionOnlyPrinter
152 : public DOTGraphTraitsPrinterWrapperPass<
153 RegionInfoPass, true, RegionInfo *, RegionInfoPassGraphTraits> {
154 static char ID;
155 RegionOnlyPrinter()
156 : DOTGraphTraitsPrinterWrapperPass<RegionInfoPass, true, RegionInfo *,
157 RegionInfoPassGraphTraits>("reg", ID) {
158 initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
161 char RegionOnlyPrinter::ID = 0;
163 struct RegionViewer
164 : public DOTGraphTraitsViewerWrapperPass<
165 RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
166 static char ID;
167 RegionViewer()
168 : DOTGraphTraitsViewerWrapperPass<RegionInfoPass, false, RegionInfo *,
169 RegionInfoPassGraphTraits>("reg", ID) {
170 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
173 char RegionViewer::ID = 0;
175 struct RegionOnlyViewer
176 : public DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
177 RegionInfoPassGraphTraits> {
178 static char ID;
179 RegionOnlyViewer()
180 : DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
181 RegionInfoPassGraphTraits>("regonly",
182 ID) {
183 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
186 char RegionOnlyViewer::ID = 0;
188 } //end anonymous namespace
190 INITIALIZE_PASS(RegionPrinter, "dot-regions",
191 "Print regions of function to 'dot' file", true, true)
193 INITIALIZE_PASS(
194 RegionOnlyPrinter, "dot-regions-only",
195 "Print regions of function to 'dot' file (with no function bodies)", true,
196 true)
198 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
199 true, true)
201 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
202 "View regions of function (with no function bodies)",
203 true, true)
205 FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
207 FunctionPass *llvm::createRegionOnlyPrinterPass() {
208 return new RegionOnlyPrinter();
211 FunctionPass* llvm::createRegionViewerPass() {
212 return new RegionViewer();
215 FunctionPass* llvm::createRegionOnlyViewerPass() {
216 return new RegionOnlyViewer();
219 #ifndef NDEBUG
220 static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
221 assert(RI && "Argument must be non-null");
223 llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
224 std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
226 llvm::ViewGraph(RI, "reg", ShortNames,
227 Twine(GraphName) + " for '" + F->getName() + "' function");
230 static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
231 assert(F && "Argument must be non-null");
232 assert(!F->isDeclaration() && "Function must have an implementation");
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);
238 llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
239 FPM.add(ViewerPass);
240 FPM.doInitialization();
241 FPM.run(*NonConstF);
242 FPM.doFinalization();
245 void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
247 void llvm::viewRegion(const Function *F) {
248 invokeFunctionPass(F, createRegionViewerPass());
251 void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
253 void llvm::viewRegionOnly(const Function *F) {
254 invokeFunctionPass(F, createRegionOnlyViewerPass());
256 #endif