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