xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/RegionPrinter.cpp (revision 1342eb5a832fa10e689a29faab3acb6054e4778c)
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 
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"
18 #ifndef NDEBUG
19 #include "llvm/IR/LegacyPassManager.h"
20 #endif
21 
22 using namespace llvm;
23 
24 //===----------------------------------------------------------------------===//
25 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
26 static cl::opt<bool>
27 onlySimpleRegions("only-simple-regions",
28                   cl::desc("Show only simple regions in the graphviz viewer"),
29                   cl::Hidden,
30                   cl::init(false));
31 
32 namespace llvm {
33 
34 std::string DOTGraphTraits<RegionNode *>::getNodeLabel(RegionNode *Node,
35                                                        RegionNode *Graph) {
36   if (!Node->isSubRegion()) {
37     BasicBlock *BB = Node->getNodeAs<BasicBlock>();
38 
39     if (isSimple())
40       return DOTGraphTraits<DOTFuncInfo *>::getSimpleNodeLabel(BB, nullptr);
41     else
42       return DOTGraphTraits<DOTFuncInfo *>::getCompleteNodeLabel(BB, nullptr);
43   }
44 
45   return "Not implemented";
46 }
47 
48 template <>
49 struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {
50 
51   DOTGraphTraits (bool isSimple = false)
52     : DOTGraphTraits<RegionNode*>(isSimple) {}
53 
54   static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
55 
56   std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
57     return DOTGraphTraits<RegionNode *>::getNodeLabel(
58         Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
59   }
60 
61   std::string getEdgeAttributes(RegionNode *srcNode,
62                                 GraphTraits<RegionInfo *>::ChildIteratorType CI,
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.
89   static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW,
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 
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);
126   }
127 };
128 } // end namespace llvm
129 
130 namespace {
131 
132 struct RegionInfoPassGraphTraits {
133   static RegionInfo *getGraph(RegionInfoPass *RIP) {
134     return &RIP->getRegionInfo();
135   }
136 };
137 
138 struct RegionPrinter
139     : public DOTGraphTraitsPrinterWrapperPass<
140           RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
141   static char ID;
142   RegionPrinter()
143       : DOTGraphTraitsPrinterWrapperPass<RegionInfoPass, false, RegionInfo *,
144                                          RegionInfoPassGraphTraits>("reg", ID) {
145   }
146 };
147 char RegionPrinter::ID = 0;
148 
149 struct RegionOnlyPrinter
150     : public DOTGraphTraitsPrinterWrapperPass<
151           RegionInfoPass, true, RegionInfo *, RegionInfoPassGraphTraits> {
152   static char ID;
153   RegionOnlyPrinter()
154       : DOTGraphTraitsPrinterWrapperPass<RegionInfoPass, true, RegionInfo *,
155                                          RegionInfoPassGraphTraits>("reg", ID) {
156   }
157 };
158 char RegionOnlyPrinter::ID = 0;
159 
160 struct RegionViewer
161     : public DOTGraphTraitsViewerWrapperPass<
162           RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
163   static char ID;
164   RegionViewer()
165       : DOTGraphTraitsViewerWrapperPass<RegionInfoPass, false, RegionInfo *,
166                                         RegionInfoPassGraphTraits>("reg", ID) {}
167 };
168 char RegionViewer::ID = 0;
169 
170 struct RegionOnlyViewer
171     : public DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
172                                              RegionInfoPassGraphTraits> {
173   static char ID;
174   RegionOnlyViewer()
175       : DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
176                                         RegionInfoPassGraphTraits>("regonly",
177                                                                    ID) {}
178 };
179 char RegionOnlyViewer::ID = 0;
180 
181 } //end anonymous namespace
182 
183 INITIALIZE_PASS(RegionPrinter, "dot-regions",
184                 "Print regions of function to 'dot' file", true, true)
185 
186 INITIALIZE_PASS(
187     RegionOnlyPrinter, "dot-regions-only",
188     "Print regions of function to 'dot' file (with no function bodies)", true,
189     true)
190 
191 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
192                 true, true)
193 
194 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
195                 "View regions of function (with no function bodies)",
196                 true, true)
197 
198 FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
199 
200 FunctionPass *llvm::createRegionOnlyPrinterPass() {
201   return new RegionOnlyPrinter();
202 }
203 
204 FunctionPass* llvm::createRegionViewerPass() {
205   return new RegionViewer();
206 }
207 
208 FunctionPass* llvm::createRegionOnlyViewerPass() {
209   return new RegionOnlyViewer();
210 }
211 
212 #ifndef NDEBUG
213 static 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 
223 static 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 
238 void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
239 
240 void llvm::viewRegion(const Function *F) {
241   invokeFunctionPass(F, createRegionViewerPass());
242 }
243 
244 void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
245 
246 void llvm::viewRegionOnly(const Function *F) {
247   invokeFunctionPass(F, createRegionOnlyViewerPass());
248 }
249 #endif
250