1 //===- GraphPrinter.cpp - Create a DOT output describing the Scop. --------===//
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 //
9 // Create a DOT output describing the Scop.
10 //
11 // For each function a dot file is created that shows the control flow graph of
12 // the function and highlights the detected Scops.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "polly/LinkAllPasses.h"
17 #include "polly/ScopDetection.h"
18 #include "polly/Support/ScopLocation.h"
19 #include "llvm/Analysis/DOTGraphTraitsPass.h"
20 #include "llvm/Analysis/RegionInfo.h"
21 #include "llvm/Analysis/RegionIterator.h"
22 #include "llvm/Support/CommandLine.h"
23
24 using namespace polly;
25 using namespace llvm;
26 static cl::opt<std::string>
27 ViewFilter("polly-view-only",
28 cl::desc("Only view functions that match this pattern"),
29 cl::Hidden, cl::init(""), cl::ZeroOrMore);
30
31 static cl::opt<bool> ViewAll("polly-view-all",
32 cl::desc("Also show functions without any scops"),
33 cl::Hidden, cl::init(false), cl::ZeroOrMore);
34
35 namespace llvm {
36 template <>
37 struct GraphTraits<ScopDetection *> : public GraphTraits<RegionInfo *> {
getEntryNodellvm::GraphTraits38 static NodeRef getEntryNode(ScopDetection *SD) {
39 return GraphTraits<RegionInfo *>::getEntryNode(SD->getRI());
40 }
nodes_beginllvm::GraphTraits41 static nodes_iterator nodes_begin(ScopDetection *SD) {
42 return nodes_iterator::begin(getEntryNode(SD));
43 }
nodes_endllvm::GraphTraits44 static nodes_iterator nodes_end(ScopDetection *SD) {
45 return nodes_iterator::end(getEntryNode(SD));
46 }
47 };
48
49 template <>
50 struct GraphTraits<ScopDetectionWrapperPass *>
51 : public GraphTraits<ScopDetection *> {
getEntryNodellvm::GraphTraits52 static NodeRef getEntryNode(ScopDetectionWrapperPass *P) {
53 return GraphTraits<ScopDetection *>::getEntryNode(&P->getSD());
54 }
nodes_beginllvm::GraphTraits55 static nodes_iterator nodes_begin(ScopDetectionWrapperPass *P) {
56 return nodes_iterator::begin(getEntryNode(P));
57 }
nodes_endllvm::GraphTraits58 static nodes_iterator nodes_end(ScopDetectionWrapperPass *P) {
59 return nodes_iterator::end(getEntryNode(P));
60 }
61 };
62
63 template <> struct DOTGraphTraits<RegionNode *> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits64 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
65
getNodeLabelllvm::DOTGraphTraits66 std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
67 if (!Node->isSubRegion()) {
68 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
69
70 if (isSimple())
71 return DOTGraphTraits<DOTFuncInfo *>::getSimpleNodeLabel(BB, nullptr);
72
73 else
74 return DOTGraphTraits<DOTFuncInfo *>::getCompleteNodeLabel(BB, nullptr);
75 }
76
77 return "Not implemented";
78 }
79 };
80
81 template <>
82 struct DOTGraphTraits<ScopDetectionWrapperPass *>
83 : public DOTGraphTraits<RegionNode *> {
DOTGraphTraitsllvm::DOTGraphTraits84 DOTGraphTraits(bool isSimple = false)
85 : DOTGraphTraits<RegionNode *>(isSimple) {}
getGraphNamellvm::DOTGraphTraits86 static std::string getGraphName(ScopDetectionWrapperPass *SD) {
87 return "Scop Graph";
88 }
89
getEdgeAttributesllvm::DOTGraphTraits90 std::string getEdgeAttributes(RegionNode *srcNode,
91 GraphTraits<RegionInfo *>::ChildIteratorType CI,
92 ScopDetectionWrapperPass *P) {
93 RegionNode *destNode = *CI;
94 auto *SD = &P->getSD();
95
96 if (srcNode->isSubRegion() || destNode->isSubRegion())
97 return "";
98
99 // In case of a backedge, do not use it to define the layout of the nodes.
100 BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
101 BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
102
103 RegionInfo *RI = SD->getRI();
104 Region *R = RI->getRegionFor(destBB);
105
106 while (R && R->getParent())
107 if (R->getParent()->getEntry() == destBB)
108 R = R->getParent();
109 else
110 break;
111
112 if (R && R->getEntry() == destBB && R->contains(srcBB))
113 return "constraint=false";
114
115 return "";
116 }
117
getNodeLabelllvm::DOTGraphTraits118 std::string getNodeLabel(RegionNode *Node, ScopDetectionWrapperPass *P) {
119 return DOTGraphTraits<RegionNode *>::getNodeLabel(
120 Node, reinterpret_cast<RegionNode *>(
121 P->getSD().getRI()->getTopLevelRegion()));
122 }
123
escapeStringllvm::DOTGraphTraits124 static std::string escapeString(std::string String) {
125 std::string Escaped;
126
127 for (const auto &C : String) {
128 if (C == '"')
129 Escaped += '\\';
130
131 Escaped += C;
132 }
133 return Escaped;
134 }
135
136 // Print the cluster of the subregions. This groups the single basic blocks
137 // and adds a different background color for each group.
printRegionClusterllvm::DOTGraphTraits138 static void printRegionCluster(const ScopDetection *SD, const Region *R,
139 raw_ostream &O, unsigned depth = 0) {
140 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void *>(R)
141 << " {\n";
142 unsigned LineBegin, LineEnd;
143 std::string FileName;
144
145 getDebugLocation(R, LineBegin, LineEnd, FileName);
146
147 std::string Location;
148 if (LineBegin != (unsigned)-1) {
149 Location = escapeString(FileName + ":" + std::to_string(LineBegin) + "-" +
150 std::to_string(LineEnd) + "\n");
151 }
152
153 std::string ErrorMessage = SD->regionIsInvalidBecause(R);
154 ErrorMessage = escapeString(ErrorMessage);
155 O.indent(2 * (depth + 1))
156 << "label = \"" << Location << ErrorMessage << "\";\n";
157
158 if (SD->isMaxRegionInScop(*R)) {
159 O.indent(2 * (depth + 1)) << "style = filled;\n";
160
161 // Set color to green.
162 O.indent(2 * (depth + 1)) << "color = 3";
163 } else {
164 O.indent(2 * (depth + 1)) << "style = solid;\n";
165
166 int color = (R->getDepth() * 2 % 12) + 1;
167
168 // We do not want green again.
169 if (color == 3)
170 color = 6;
171
172 O.indent(2 * (depth + 1)) << "color = " << color << "\n";
173 }
174
175 for (const auto &SubRegion : *R)
176 printRegionCluster(SD, SubRegion.get(), O, depth + 1);
177
178 RegionInfo *RI = R->getRegionInfo();
179
180 for (BasicBlock *BB : R->blocks())
181 if (RI->getRegionFor(BB) == R)
182 O.indent(2 * (depth + 1))
183 << "Node"
184 << static_cast<void *>(RI->getTopLevelRegion()->getBBNode(BB))
185 << ";\n";
186
187 O.indent(2 * depth) << "}\n";
188 }
189 static void
addCustomGraphFeaturesllvm::DOTGraphTraits190 addCustomGraphFeatures(const ScopDetectionWrapperPass *SD,
191 GraphWriter<ScopDetectionWrapperPass *> &GW) {
192 raw_ostream &O = GW.getOStream();
193 O << "\tcolorscheme = \"paired12\"\n";
194 printRegionCluster(&SD->getSD(), SD->getSD().getRI()->getTopLevelRegion(),
195 O, 4);
196 }
197 };
198 } // end namespace llvm
199
200 struct ScopViewer
201 : public DOTGraphTraitsViewer<ScopDetectionWrapperPass, false> {
202 static char ID;
ScopViewerScopViewer203 ScopViewer()
204 : DOTGraphTraitsViewer<ScopDetectionWrapperPass, false>("scops", ID) {}
processFunctionScopViewer205 bool processFunction(Function &F, ScopDetectionWrapperPass &SD) override {
206 if (ViewFilter != "" && !F.getName().count(ViewFilter))
207 return false;
208
209 if (ViewAll)
210 return true;
211
212 // Check that at least one scop was detected.
213 return std::distance(SD.getSD().begin(), SD.getSD().end()) > 0;
214 }
215 };
216 char ScopViewer::ID = 0;
217
218 struct ScopOnlyViewer
219 : public DOTGraphTraitsViewer<ScopDetectionWrapperPass, true> {
220 static char ID;
ScopOnlyViewerScopOnlyViewer221 ScopOnlyViewer()
222 : DOTGraphTraitsViewer<ScopDetectionWrapperPass, true>("scopsonly", ID) {}
223 };
224 char ScopOnlyViewer::ID = 0;
225
226 struct ScopPrinter
227 : public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false> {
228 static char ID;
ScopPrinterScopPrinter229 ScopPrinter()
230 : DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false>("scops", ID) {}
231 };
232 char ScopPrinter::ID = 0;
233
234 struct ScopOnlyPrinter
235 : public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true> {
236 static char ID;
ScopOnlyPrinterScopOnlyPrinter237 ScopOnlyPrinter()
238 : DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true>("scopsonly", ID) {
239 }
240 };
241 char ScopOnlyPrinter::ID = 0;
242
243 static RegisterPass<ScopViewer> X("view-scops",
244 "Polly - View Scops of function");
245
246 static RegisterPass<ScopOnlyViewer>
247 Y("view-scops-only",
248 "Polly - View Scops of function (with no function bodies)");
249
250 static RegisterPass<ScopPrinter> M("dot-scops",
251 "Polly - Print Scops of function");
252
253 static RegisterPass<ScopOnlyPrinter>
254 N("dot-scops-only",
255 "Polly - Print Scops of function (with no function bodies)");
256
createDOTViewerPass()257 Pass *polly::createDOTViewerPass() { return new ScopViewer(); }
258
createDOTOnlyViewerPass()259 Pass *polly::createDOTOnlyViewerPass() { return new ScopOnlyViewer(); }
260
createDOTPrinterPass()261 Pass *polly::createDOTPrinterPass() { return new ScopPrinter(); }
262
createDOTOnlyPrinterPass()263 Pass *polly::createDOTOnlyPrinterPass() { return new ScopOnlyPrinter(); }
264