blob: 5046285245cf608b73f896dd7f123c3be9f21e8f [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===----- ScopDetection.cpp - Detect Scops --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Detect the maximal Scops of a function.
11//
12// A static control part (Scop) is a subgraph of the control flow graph (CFG)
13// that only has statically known control flow and can therefore be described
14// within the polyhedral model.
15//
16// Every Scop fullfills these restrictions:
17//
18// * It is a single entry single exit region
19//
20// * Only affine linear bounds in the loops
21//
22// Every natural loop in a Scop must have a number of loop iterations that can
23// be described as an affine linear function in surrounding loop iterators or
24// parameters. (A parameter is a scalar that does not change its value during
25// execution of the Scop).
26//
27// * Only comparisons of affine linear expressions in conditions
28//
29// * All loops and conditions perfectly nested
30//
31// The control flow needs to be structured such that it could be written using
32// just 'for' and 'if' statements, without the need for any 'goto', 'break' or
33// 'continue'.
34//
35// * Side effect free functions call
36//
37// Only function calls and intrinsics that do not have side effects are allowed
38// (readnone).
39//
40// The Scop detection finds the largest Scops by checking if the largest
41// region is a Scop. If this is not the case, its canonical subregions are
42// checked until a region is a Scop. It is now tried to extend this Scop by
43// creating a larger non canonical region.
44//
45//===----------------------------------------------------------------------===//
46
Tobias Grosserecfe21b2013-03-20 18:03:18 +000047#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000048#include "polly/CodeGen/CodeGeneration.h"
Tobias Grosser75805372011-04-29 06:27:02 +000049#include "polly/LinkAllPasses.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000050#include "polly/Options.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000051#include "polly/ScopDetection.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000052#include "polly/ScopDetectionDiagnostic.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +000053#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000054#include "polly/Support/ScopHelper.h"
Tobias Grossera63b7ce2015-05-03 05:21:36 +000055#include "polly/Support/ScopLocation.h"
Tobias Grosser75805372011-04-29 06:27:02 +000056#include "llvm/ADT/Statistic.h"
57#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000058#include "llvm/Analysis/LoopInfo.h"
Matt Arsenault8ca36812014-07-19 18:40:17 +000059#include "llvm/Analysis/PostDominators.h"
Tobias Grosser75805372011-04-29 06:27:02 +000060#include "llvm/Analysis/RegionIterator.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000061#include "llvm/Analysis/ScalarEvolution.h"
Tobias Grosserb8710b52011-11-10 12:44:50 +000062#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruth6b96c242014-03-06 00:47:27 +000063#include "llvm/IR/DebugInfo.h"
Tobias Grosser8519f892013-12-18 10:49:53 +000064#include "llvm/IR/DiagnosticInfo.h"
65#include "llvm/IR/DiagnosticPrinter.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000066#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6b96c242014-03-06 00:47:27 +000067#include "llvm/IR/LLVMContext.h"
Tobias Grosser75805372011-04-29 06:27:02 +000068#include "llvm/Support/Debug.h"
Tobias Grosser60b54f12011-11-08 15:41:28 +000069#include <set>
70
Tobias Grosser75805372011-04-29 06:27:02 +000071using namespace llvm;
72using namespace polly;
73
Chandler Carruth95fef942014-04-22 03:30:19 +000074#define DEBUG_TYPE "polly-detect"
75
Tobias Grosserd1e33e72015-02-19 05:31:07 +000076static cl::opt<bool> DetectUnprofitable("polly-detect-unprofitable",
77 cl::desc("Detect unprofitable scops"),
78 cl::Hidden, cl::init(false),
79 cl::ZeroOrMore, cl::cat(PollyCategory));
80
Tobias Grosser483a90d2014-07-09 10:50:10 +000081static cl::opt<std::string> OnlyFunction(
82 "polly-only-func",
83 cl::desc("Only run on functions that contain a certain string"),
84 cl::value_desc("string"), cl::ValueRequired, cl::init(""),
85 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000086
Tobias Grosser483a90d2014-07-09 10:50:10 +000087static cl::opt<std::string> OnlyRegion(
88 "polly-only-region",
89 cl::desc("Only run on certain regions (The provided identifier must "
90 "appear in the name of the region's entry block"),
91 cl::value_desc("identifier"), cl::ValueRequired, cl::init(""),
92 cl::cat(PollyCategory));
Tobias Grosser4449e522014-01-27 14:24:53 +000093
Tobias Grosser60cd9322011-11-10 12:47:26 +000094static cl::opt<bool>
Tobias Grosser483a90d2014-07-09 10:50:10 +000095 IgnoreAliasing("polly-ignore-aliasing",
96 cl::desc("Ignore possible aliasing of the array bases"),
97 cl::Hidden, cl::init(false), cl::ZeroOrMore,
98 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000099
Johannes Doerfertb164c792014-09-18 11:17:17 +0000100bool polly::PollyUseRuntimeAliasChecks;
101static cl::opt<bool, true> XPollyUseRuntimeAliasChecks(
102 "polly-use-runtime-alias-checks",
103 cl::desc("Use runtime alias checks to resolve possible aliasing."),
104 cl::location(PollyUseRuntimeAliasChecks), cl::Hidden, cl::ZeroOrMore,
105 cl::init(true), cl::cat(PollyCategory));
106
Tobias Grosser637bd632013-05-07 07:31:10 +0000107static cl::opt<bool>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000108 ReportLevel("polly-report",
109 cl::desc("Print information about the activities of Polly"),
110 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser531891e2012-11-01 16:45:20 +0000111
112static cl::opt<bool>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000113 AllowNonAffine("polly-allow-nonaffine",
114 cl::desc("Allow non affine access functions in arrays"),
115 cl::Hidden, cl::init(false), cl::ZeroOrMore,
116 cl::cat(PollyCategory));
Tobias Grossera1879642011-12-20 10:43:14 +0000117
Johannes Doerfertba65c162015-02-24 11:45:21 +0000118static cl::opt<bool> AllowNonAffineSubRegions(
119 "polly-allow-nonaffine-branches",
120 cl::desc("Allow non affine conditions for branches"), cl::Hidden,
Johannes Doerferta36842f2015-02-26 11:09:24 +0000121 cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
Johannes Doerfertba65c162015-02-24 11:45:21 +0000122
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000123static cl::opt<bool>
124 AllowNonAffineSubLoops("polly-allow-nonaffine-loops",
125 cl::desc("Allow non affine conditions for loops"),
126 cl::Hidden, cl::init(false), cl::ZeroOrMore,
127 cl::cat(PollyCategory));
128
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000129static cl::opt<bool> AllowUnsigned("polly-allow-unsigned",
130 cl::desc("Allow unsigned expressions"),
131 cl::Hidden, cl::init(false), cl::ZeroOrMore,
132 cl::cat(PollyCategory));
133
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000134static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000135 TrackFailures("polly-detect-track-failures",
136 cl::desc("Track failure strings in detecting scop regions"),
137 cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore,
Andreas Simbuerger3efe40b2014-08-17 10:09:03 +0000138 cl::init(true), cl::cat(PollyCategory));
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000139
Andreas Simbuerger04472402014-05-24 09:25:10 +0000140static cl::opt<bool> KeepGoing("polly-detect-keep-going",
141 cl::desc("Do not fail on the first error."),
142 cl::Hidden, cl::ZeroOrMore, cl::init(false),
143 cl::cat(PollyCategory));
144
Sebastian Pop18016682014-04-08 21:20:44 +0000145static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000146 PollyDelinearizeX("polly-delinearize",
147 cl::desc("Delinearize array access functions"),
148 cl::location(PollyDelinearize), cl::Hidden,
Tobias Grosser6973cb62015-03-08 15:21:18 +0000149 cl::ZeroOrMore, cl::init(true), cl::cat(PollyCategory));
Sebastian Pop18016682014-04-08 21:20:44 +0000150
Tobias Grossera1689932014-02-18 18:49:49 +0000151static cl::opt<bool>
Tobias Grosser483a90d2014-07-09 10:50:10 +0000152 VerifyScops("polly-detect-verify",
153 cl::desc("Verify the detected SCoPs after each transformation"),
154 cl::Hidden, cl::init(false), cl::ZeroOrMore,
155 cl::cat(PollyCategory));
Tobias Grossera1689932014-02-18 18:49:49 +0000156
Johannes Doerfertd020b772015-08-27 06:53:52 +0000157static cl::opt<bool> AllowNonSCEVBackedgeTakenCount(
158 "polly-allow-non-scev-backedge-taken-count",
159 cl::desc("Allow loops even if SCEV cannot provide a trip count"),
160 cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::cat(PollyCategory));
161
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000162bool polly::PollyTrackFailures = false;
Sebastian Pop18016682014-04-08 21:20:44 +0000163bool polly::PollyDelinearize = false;
Johannes Doerfert43e1ead2014-07-15 21:06:48 +0000164StringRef polly::PollySkipFnAttr = "polly.skip.fn";
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000165
Tobias Grosser75805372011-04-29 06:27:02 +0000166//===----------------------------------------------------------------------===//
167// Statistics.
168
169STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
170
Tobias Grosser8519f892013-12-18 10:49:53 +0000171class DiagnosticScopFound : public DiagnosticInfo {
172private:
173 static int PluginDiagnosticKind;
174
175 Function &F;
176 std::string FileName;
177 unsigned EntryLine, ExitLine;
178
179public:
Tobias Grosser1b12f462013-12-18 11:14:36 +0000180 DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine,
181 unsigned ExitLine)
Tobias Grosser8519f892013-12-18 10:49:53 +0000182 : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName),
Tobias Grosser1b12f462013-12-18 11:14:36 +0000183 EntryLine(EntryLine), ExitLine(ExitLine) {}
Tobias Grosser8519f892013-12-18 10:49:53 +0000184
185 virtual void print(DiagnosticPrinter &DP) const;
186
187 static bool classof(const DiagnosticInfo *DI) {
188 return DI->getKind() == PluginDiagnosticKind;
189 }
190};
191
192int DiagnosticScopFound::PluginDiagnosticKind = 10;
193
Tobias Grosser8519f892013-12-18 10:49:53 +0000194void DiagnosticScopFound::print(DiagnosticPrinter &DP) const {
Tobias Grosser1b12f462013-12-18 11:14:36 +0000195 DP << "Polly detected an optimizable loop region (scop) in function '" << F
196 << "'\n";
Tobias Grosser8519f892013-12-18 10:49:53 +0000197
198 if (FileName.empty()) {
199 DP << "Scop location is unknown. Compile with debug info "
200 "(-g) to get more precise information. ";
Tobias Grosser1b12f462013-12-18 11:14:36 +0000201 return;
Tobias Grosser8519f892013-12-18 10:49:53 +0000202 }
203
204 DP << FileName << ":" << EntryLine << ": Start of scop\n";
205 DP << FileName << ":" << ExitLine << ": End of scop";
206}
207
Tobias Grosser75805372011-04-29 06:27:02 +0000208//===----------------------------------------------------------------------===//
209// ScopDetection.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000210
Johannes Doerfertb164c792014-09-18 11:17:17 +0000211ScopDetection::ScopDetection() : FunctionPass(ID) {
212 if (!PollyUseRuntimeAliasChecks)
213 return;
214
Johannes Doerfert928229f2014-09-29 17:06:29 +0000215 // Disable runtime alias checks if we ignore aliasing all together.
216 if (IgnoreAliasing) {
217 PollyUseRuntimeAliasChecks = false;
218 return;
219 }
220
Johannes Doerfertb164c792014-09-18 11:17:17 +0000221 if (AllowNonAffine) {
222 DEBUG(errs() << "WARNING: We disable runtime alias checks as non affine "
223 "accesses are enabled.\n");
224 PollyUseRuntimeAliasChecks = false;
225 }
Johannes Doerfertb164c792014-09-18 11:17:17 +0000226}
227
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000228template <class RR, typename... Args>
229inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert,
230 Args &&... Arguments) const {
231
232 if (!Context.Verifying) {
Andreas Simbuerger4870e092014-05-24 09:25:01 +0000233 RejectLog &Log = Context.Log;
234 std::shared_ptr<RR> RejectReason = std::make_shared<RR>(Arguments...);
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000235
Andreas Simbuerger8a00c9b2014-05-24 09:25:06 +0000236 if (PollyTrackFailures)
Andreas Simbuerger4870e092014-05-24 09:25:01 +0000237 Log.report(RejectReason);
Andreas Simbuerger4870e092014-05-24 09:25:01 +0000238
239 DEBUG(dbgs() << RejectReason->getMessage());
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000240 DEBUG(dbgs() << "\n");
241 } else {
242 assert(!Assert && "Verification of detected scop failed");
243 }
244
245 return false;
246}
247
Tobias Grossera1689932014-02-18 18:49:49 +0000248bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const {
249 if (!ValidRegions.count(&R))
250 return false;
251
Johannes Doerfert3f1c2852015-02-19 18:11:50 +0000252 if (Verify) {
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000253 BoxedLoopsSetTy DummyBoxedLoopsSet;
Johannes Doerfertba65c162015-02-24 11:45:21 +0000254 NonAffineSubRegionSetTy DummyNonAffineSubRegionSet;
255 DetectionContext Context(const_cast<Region &>(R), *AA,
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000256 DummyNonAffineSubRegionSet, DummyBoxedLoopsSet,
257 false /*verifying*/);
Johannes Doerfert3f1c2852015-02-19 18:11:50 +0000258 return isValidRegion(Context);
259 }
Tobias Grossera1689932014-02-18 18:49:49 +0000260
261 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000262}
263
Tobias Grosser4f129a62011-10-08 00:30:55 +0000264std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
Andreas Simbuerger8a00c9b2014-05-24 09:25:06 +0000265 if (!RejectLogs.count(R))
Tobias Grosser4f129a62011-10-08 00:30:55 +0000266 return "";
267
Andreas Simbuerger8a00c9b2014-05-24 09:25:06 +0000268 // Get the first error we found. Even in keep-going mode, this is the first
269 // reason that caused the candidate to be rejected.
270 RejectLog Errors = RejectLogs.at(R);
Andreas Simbuerger83ed8612014-06-12 07:25:08 +0000271
272 // This can happen when we marked a region invalid, but didn't track
273 // an error for it.
274 if (Errors.size() == 0)
275 return "";
276
277 RejectReasonPtr RR = *Errors.begin();
278 return RR->getMessage();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000279}
280
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000281bool ScopDetection::addOverApproximatedRegion(Region *AR,
282 DetectionContext &Context) const {
283
284 // If we already know about Ar we can exit.
285 if (!Context.NonAffineSubRegionSet.insert(AR))
286 return true;
287
288 // All loops in the region have to be overapproximated too if there
289 // are accesses that depend on the iteration count.
290 for (BasicBlock *BB : AR->blocks()) {
Johannes Doerfertba65c162015-02-24 11:45:21 +0000291 Loop *L = LI->getLoopFor(BB);
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000292 if (AR->contains(L))
293 Context.BoxedLoopsSet.insert(L);
Johannes Doerfertba65c162015-02-24 11:45:21 +0000294 }
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000295
296 return (AllowNonAffineSubLoops || Context.BoxedLoopsSet.empty());
Johannes Doerfertba65c162015-02-24 11:45:21 +0000297}
298
Tobias Grossere602a072013-05-07 07:30:56 +0000299bool ScopDetection::isValidCFG(BasicBlock &BB,
300 DetectionContext &Context) const {
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000301 Region &CurRegion = Context.CurRegion;
302
Tobias Grosser75805372011-04-29 06:27:02 +0000303 TerminatorInst *TI = BB.getTerminator();
304
305 // Return instructions are only valid if the region is the top level region.
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000306 if (isa<ReturnInst>(TI) && !CurRegion.getExit() && TI->getNumOperands() == 0)
Tobias Grosser75805372011-04-29 06:27:02 +0000307 return true;
308
309 BranchInst *Br = dyn_cast<BranchInst>(TI);
310
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000311 if (!Br)
312 return invalid<ReportNonBranchTerminator>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000313
Tobias Grosser74394f02013-01-14 22:40:23 +0000314 if (Br->isUnconditional())
315 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000316
317 Value *Condition = Br->getCondition();
318
319 // UndefValue is not allowed as condition.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000320 if (isa<UndefValue>(Condition))
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000321 return invalid<ReportUndefCond>(Context, /*Assert=*/true, Br, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000322
323 // Only Constant and ICmpInst are allowed as condition.
Johannes Doerfertba65c162015-02-24 11:45:21 +0000324 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) {
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000325 if (!AllowNonAffineSubRegions ||
326 !addOverApproximatedRegion(RI->getRegionFor(&BB), Context))
Johannes Doerfertba65c162015-02-24 11:45:21 +0000327 return invalid<ReportInvalidCond>(Context, /*Assert=*/true, Br, &BB);
328 }
Tobias Grosser75805372011-04-29 06:27:02 +0000329
330 // Allow perfectly nested conditions.
331 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
332
333 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
334 // Unsigned comparisons are not allowed. They trigger overflow problems
335 // in the code generation.
336 //
337 // TODO: This is not sufficient and just hides bugs. However it does pretty
338 // well.
Tobias Grosserbfbc3692015-01-09 00:01:33 +0000339 if (ICmp->isUnsigned() && !AllowUnsigned)
Tobias Grossera8512b12015-05-20 15:37:11 +0000340 return invalid<ReportUnsignedCond>(Context, /*Assert=*/true, Br, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000341
342 // Are both operands of the ICmp affine?
Tobias Grosser74394f02013-01-14 22:40:23 +0000343 if (isa<UndefValue>(ICmp->getOperand(0)) ||
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000344 isa<UndefValue>(ICmp->getOperand(1)))
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000345 return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB, ICmp);
Tobias Grosser75805372011-04-29 06:27:02 +0000346
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000347 Loop *L = LI->getLoopFor(ICmp->getParent());
348 const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
349 const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
Tobias Grosser75805372011-04-29 06:27:02 +0000350
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000351 if (!isAffineExpr(&CurRegion, LHS, *SE) ||
Johannes Doerfertba65c162015-02-24 11:45:21 +0000352 !isAffineExpr(&CurRegion, RHS, *SE)) {
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000353 if (!AllowNonAffineSubRegions ||
354 !addOverApproximatedRegion(RI->getRegionFor(&BB), Context))
Johannes Doerfertba65c162015-02-24 11:45:21 +0000355 return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS,
356 RHS, ICmp);
357 }
Tobias Grosser75805372011-04-29 06:27:02 +0000358 }
359
360 // Allow loop exit conditions.
361 Loop *L = LI->getLoopFor(&BB);
362 if (L && L->getExitingBlock() == &BB)
363 return true;
364
365 // Allow perfectly nested conditions.
366 Region *R = RI->getRegionFor(&BB);
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000367 if (R->getEntry() != &BB)
368 return invalid<ReportCondition>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000369
370 return true;
371}
372
373bool ScopDetection::isValidCallInst(CallInst &CI) {
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000374 if (CI.doesNotReturn())
Tobias Grosser75805372011-04-29 06:27:02 +0000375 return false;
376
377 if (CI.doesNotAccessMemory())
378 return true;
379
380 Function *CalledFunction = CI.getCalledFunction();
381
382 // Indirect calls are not supported.
383 if (CalledFunction == 0)
384 return false;
385
Tobias Grosser9c0ffe32015-08-30 16:57:15 +0000386 if (isIgnoredIntrinsic(&CI))
387 return true;
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000388
Tobias Grosser75805372011-04-29 06:27:02 +0000389 return false;
390}
391
Tobias Grosser458fb782014-01-28 12:58:58 +0000392bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const {
393 // A reference to function argument or constant value is invariant.
394 if (isa<Argument>(Val) || isa<Constant>(Val))
395 return true;
396
397 const Instruction *I = dyn_cast<Instruction>(&Val);
398 if (!I)
399 return false;
400
401 if (!Reg.contains(I))
402 return true;
403
404 if (I->mayHaveSideEffects())
405 return false;
406
407 // When Val is a Phi node, it is likely not invariant. We do not check whether
408 // Phi nodes are actually invariant, we assume that Phi nodes are usually not
409 // invariant. Recursively checking the operators of Phi nodes would lead to
410 // infinite recursion.
411 if (isa<PHINode>(*I))
412 return false;
413
Tobias Grosser26108892014-04-02 20:18:19 +0000414 for (const Use &Operand : I->operands())
Tobias Grosser1d191902014-03-03 13:13:55 +0000415 if (!isInvariant(*Operand, Reg))
Tobias Grosser458fb782014-01-28 12:58:58 +0000416 return false;
Tobias Grosser458fb782014-01-28 12:58:58 +0000417
418 // When the instruction is a load instruction, check that no write to memory
419 // in the region aliases with the load.
420 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
Chandler Carruthbdb4a392015-06-04 03:49:46 +0000421 auto Loc = MemoryLocation::get(LI);
Johannes Doerfertca08c442015-02-21 16:18:28 +0000422
Tobias Grosser458fb782014-01-28 12:58:58 +0000423 // Check if any basic block in the region can modify the location pointed to
424 // by 'Loc'. If so, 'Val' is (likely) not invariant in the region.
Tobias Grosser26108892014-04-02 20:18:19 +0000425 for (const BasicBlock *BB : Reg.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000426 if (AA->canBasicBlockModify(*BB, Loc))
Tobias Grosser458fb782014-01-28 12:58:58 +0000427 return false;
Tobias Grosser458fb782014-01-28 12:58:58 +0000428 }
429
430 return true;
431}
432
Sebastian Pop422e33f2014-06-03 18:16:31 +0000433MapInsnToMemAcc InsnToMemAcc;
434
Sebastian Popb57c0992014-05-12 20:24:26 +0000435bool ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const {
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000436 Region &CurRegion = Context.CurRegion;
437
Tobias Grosser230acc42014-09-13 14:47:55 +0000438 for (const SCEVUnknown *BasePointer : Context.NonAffineAccesses) {
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000439 Value *BaseValue = BasePointer->getValue();
Tobias Grossera5c092d2015-06-04 16:03:16 +0000440 auto Shape = std::shared_ptr<ArrayShape>(new ArrayShape(BasePointer));
Tobias Grosser230acc42014-09-13 14:47:55 +0000441 bool BasePtrHasNonAffine = false;
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000442
443 // First step: collect parametric terms in all array references.
444 SmallVector<const SCEV *, 4> Terms;
Tobias Grosser230acc42014-09-13 14:47:55 +0000445 for (const auto &Pair : Context.Accesses[BasePointer]) {
Tobias Grosser1b13dde2015-06-29 14:44:22 +0000446 if (auto *AF = dyn_cast<SCEVAddRecExpr>(Pair.second))
Tobias Grosser23bceb22015-06-29 14:44:17 +0000447 SE->collectParametricTerms(AF, Terms);
Tobias Grosser1b13dde2015-06-29 14:44:22 +0000448
449 // In case the outermost expression is a plain add, we check if any of its
450 // terms has the form 4 * %inst * %param * %param ..., aka a term that
451 // contains a product between a parameter and an instruction that is
452 // inside the scop. Such instructions, if allowed at all, are instructions
453 // SCEV can not represent, but Polly is still looking through. As a
454 // result, these instructions can depend on induction variables and are
455 // most likely no array sizes. However, terms that are multiplied with
456 // them are likely candidates for array sizes.
457 if (auto *AF = dyn_cast<SCEVAddExpr>(Pair.second)) {
458 for (auto Op : AF->operands()) {
459 if (auto *AF2 = dyn_cast<SCEVAddRecExpr>(Op))
460 SE->collectParametricTerms(AF2, Terms);
461 if (auto *AF2 = dyn_cast<SCEVMulExpr>(Op)) {
462 SmallVector<const SCEV *, 0> Operands;
Tobias Grosser1b13dde2015-06-29 14:44:22 +0000463
464 for (auto *MulOp : AF2->operands()) {
465 if (auto *Const = dyn_cast<SCEVConstant>(MulOp))
466 Operands.push_back(Const);
467 if (auto *Unknown = dyn_cast<SCEVUnknown>(MulOp)) {
468 if (auto *Inst = dyn_cast<Instruction>(Unknown->getValue())) {
469 if (!Context.CurRegion.contains(Inst))
470 Operands.push_back(MulOp);
Tobias Grosser1b13dde2015-06-29 14:44:22 +0000471
472 } else {
473 Operands.push_back(MulOp);
474 }
475 }
476 }
477 Terms.push_back(SE->getMulExpr(Operands));
478 }
479 }
480 }
Tobias Grosser230acc42014-09-13 14:47:55 +0000481 }
Sebastian Pope8863b82014-05-12 19:02:02 +0000482
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000483 // Second step: find array shape.
Sebastian Pop422e33f2014-06-03 18:16:31 +0000484 SE->findArrayDimensions(Terms, Shape->DelinearizedSizes,
485 Context.ElementSize[BasePointer]);
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000486
Johannes Doerfert89830312015-05-03 16:03:01 +0000487 if (!AllowNonAffine)
Tobias Grosser80e237b2015-07-29 13:52:05 +0000488 for (const SCEV *DelinearizedSize : Shape->DelinearizedSizes) {
489 if (auto *Unknown = dyn_cast<SCEVUnknown>(DelinearizedSize)) {
490 auto *value = dyn_cast<Value>(Unknown->getValue());
491 if (isa<UndefValue>(value)) {
492 invalid<ReportDifferentArrayElementSize>(
493 Context, /*Assert=*/true,
494 Context.Accesses[BasePointer].front().first, BaseValue);
495 return false;
496 }
497 }
Johannes Doerfert89830312015-05-03 16:03:01 +0000498 if (hasScalarDepsInsideRegion(DelinearizedSize, &CurRegion))
499 invalid<ReportNonAffineAccess>(
500 Context, /*Assert=*/true, DelinearizedSize,
501 Context.Accesses[BasePointer].front().first, BaseValue);
Tobias Grosser80e237b2015-07-29 13:52:05 +0000502 }
Johannes Doerfert89830312015-05-03 16:03:01 +0000503
Tobias Grosser230acc42014-09-13 14:47:55 +0000504 // No array shape derived.
505 if (Shape->DelinearizedSizes.empty()) {
506 if (AllowNonAffine)
507 continue;
Sebastian Pope8863b82014-05-12 19:02:02 +0000508
Tobias Grosser230acc42014-09-13 14:47:55 +0000509 for (const auto &Pair : Context.Accesses[BasePointer]) {
510 const Instruction *Insn = Pair.first;
511 const SCEV *AF = Pair.second;
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000512
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000513 if (!isAffineExpr(&CurRegion, AF, *SE, BaseValue)) {
Tobias Grosser230acc42014-09-13 14:47:55 +0000514 invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF, Insn,
515 BaseValue);
516 if (!KeepGoing)
517 return false;
518 }
519 }
520 continue;
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000521 }
Tobias Grosser230acc42014-09-13 14:47:55 +0000522
523 // Third step: compute the access functions for each subscript.
524 //
525 // We first store the resulting memory accesses in TempMemoryAccesses. Only
526 // if the access functions for all memory accesses have been successfully
527 // delinearized we continue. Otherwise, we either report a failure or, if
528 // non-affine accesses are allowed, we drop the information. In case the
529 // information is dropped the memory accesses need to be overapproximated
530 // when translated to a polyhedral representation.
531 MapInsnToMemAcc TempMemoryAccesses;
532 for (const auto &Pair : Context.Accesses[BasePointer]) {
533 const Instruction *Insn = Pair.first;
Tobias Grosser1b13dde2015-06-29 14:44:22 +0000534 auto *AF = Pair.second;
Tobias Grosser230acc42014-09-13 14:47:55 +0000535 bool IsNonAffine = false;
Tobias Grosserd8308fb2015-06-05 05:52:15 +0000536 TempMemoryAccesses.insert(std::make_pair(Insn, MemAcc(Insn, Shape)));
Tobias Grossera5c092d2015-06-04 16:03:16 +0000537 MemAcc *Acc = &TempMemoryAccesses.find(Insn)->second;
Tobias Grosser230acc42014-09-13 14:47:55 +0000538
539 if (!AF) {
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000540 if (isAffineExpr(&CurRegion, Pair.second, *SE, BaseValue))
Tobias Grosser230acc42014-09-13 14:47:55 +0000541 Acc->DelinearizedSubscripts.push_back(Pair.second);
542 else
543 IsNonAffine = true;
544 } else {
Tobias Grosser23bceb22015-06-29 14:44:17 +0000545 SE->computeAccessFunctions(AF, Acc->DelinearizedSubscripts,
Tobias Grosser230acc42014-09-13 14:47:55 +0000546 Shape->DelinearizedSizes);
547 if (Acc->DelinearizedSubscripts.size() == 0)
548 IsNonAffine = true;
549 for (const SCEV *S : Acc->DelinearizedSubscripts)
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000550 if (!isAffineExpr(&CurRegion, S, *SE, BaseValue))
Tobias Grosser230acc42014-09-13 14:47:55 +0000551 IsNonAffine = true;
552 }
553
554 // (Possibly) report non affine access
555 if (IsNonAffine) {
556 BasePtrHasNonAffine = true;
557 if (!AllowNonAffine)
Tobias Grosser021eaef2015-01-08 19:03:10 +0000558 invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, Pair.second,
559 Insn, BaseValue);
Tobias Grosser230acc42014-09-13 14:47:55 +0000560 if (!KeepGoing && !AllowNonAffine)
561 return false;
562 }
563 }
564
565 if (!BasePtrHasNonAffine)
566 InsnToMemAcc.insert(TempMemoryAccesses.begin(), TempMemoryAccesses.end());
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000567 }
Sebastian Pope8863b82014-05-12 19:02:02 +0000568 return true;
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000569}
570
Tobias Grosser75805372011-04-29 06:27:02 +0000571bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
572 DetectionContext &Context) const {
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000573 Region &CurRegion = Context.CurRegion;
574
Tobias Grossere5e171e2011-11-10 12:45:03 +0000575 Value *Ptr = getPointerOperand(Inst);
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000576 Loop *L = LI->getLoopFor(Inst.getParent());
577 const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000578 const SCEVUnknown *BasePointer;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000579 Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000580
Tobias Grosserb8710b52011-11-10 12:44:50 +0000581 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
582
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000583 if (!BasePointer)
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000584 return invalid<ReportNoBasePtr>(Context, /*Assert=*/true, &Inst);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000585
586 BaseValue = BasePointer->getValue();
587
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000588 if (isa<UndefValue>(BaseValue))
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000589 return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true, &Inst);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000590
Tobias Grosser458fb782014-01-28 12:58:58 +0000591 // Check that the base address of the access is invariant in the current
592 // region.
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000593 if (!isInvariant(*BaseValue, CurRegion))
Tobias Grosserab2227a2014-01-28 13:43:24 +0000594 // Verification of this property is difficult as the independent blocks
595 // pass may introduce aliasing that we did not have when running the
596 // scop detection.
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000597 return invalid<ReportVariantBasePtr>(Context, /*Assert=*/false, BaseValue,
598 &Inst);
Tobias Grosser458fb782014-01-28 12:58:58 +0000599
Tobias Grosserb8710b52011-11-10 12:44:50 +0000600 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
601
Tobias Grosserbcd4eff2014-09-13 14:47:40 +0000602 const SCEV *Size = SE->getElementSize(&Inst);
603 if (Context.ElementSize.count(BasePointer)) {
604 if (Context.ElementSize[BasePointer] != Size)
605 return invalid<ReportDifferentArrayElementSize>(Context, /*Assert=*/true,
606 &Inst, BaseValue);
607 } else {
608 Context.ElementSize[BasePointer] = Size;
609 }
610
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000611 bool isVariantInNonAffineLoop = false;
612 SetVector<const Loop *> Loops;
613 findLoops(AccessFunction, Loops);
614 for (const Loop *L : Loops)
615 if (Context.BoxedLoopsSet.count(L))
616 isVariantInNonAffineLoop = true;
617
618 if (PollyDelinearize && !isVariantInNonAffineLoop) {
Tobias Grosser230acc42014-09-13 14:47:55 +0000619 Context.Accesses[BasePointer].push_back({&Inst, AccessFunction});
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000620
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000621 if (!isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue))
Tobias Grosser230acc42014-09-13 14:47:55 +0000622 Context.NonAffineAccesses.insert(BasePointer);
623 } else if (!AllowNonAffine) {
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000624 if (isVariantInNonAffineLoop ||
625 !isAffineExpr(&CurRegion, AccessFunction, *SE, BaseValue))
Sebastian Popcd3bb592014-04-10 16:08:11 +0000626 return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true,
Andreas Simbuergerd46b9352014-08-17 10:09:11 +0000627 AccessFunction, &Inst, BaseValue);
Sebastian Pop18016682014-04-08 21:20:44 +0000628 }
Tobias Grosser75805372011-04-29 06:27:02 +0000629
630 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
631 // created by IndependentBlocks Pass.
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000632 if (IntToPtrInst *Inst = dyn_cast<IntToPtrInst>(BaseValue))
633 return invalid<ReportIntToPtr>(Context, /*Assert=*/true, Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000634
Tobias Grosser1eedb672014-09-24 21:04:29 +0000635 if (IgnoreAliasing)
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000636 return true;
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000637
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000638 // Check if the base pointer of the memory access does alias with
639 // any other pointer. This cannot be handled at the moment.
Benjamin Kramerae81abf2014-10-05 11:58:57 +0000640 AAMDNodes AATags;
641 Inst.getAAMetadata(AATags);
642 AliasSet &AS = Context.AST.getAliasSetForPointer(
Chandler Carruthafa4ea72015-06-17 08:29:32 +0000643 BaseValue, MemoryLocation::UnknownSize, AATags);
Tobias Grosser428b3e42013-02-04 15:46:25 +0000644
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000645 // INVALID triggers an assertion in verifying mode, if it detects that a
646 // SCoP was detected by SCoP detection and that this SCoP was invalidated by
647 // a pass that stated it would preserve the SCoPs. We disable this check as
648 // the independent blocks pass may create memory references which seem to
649 // alias, if -basicaa is not available. They actually do not, but as we can
650 // not proof this without -basicaa we would fail. We disable this check to
651 // not cause irrelevant verification failures.
Tobias Grosser1eedb672014-09-24 21:04:29 +0000652 if (!AS.isMustAlias()) {
653 if (PollyUseRuntimeAliasChecks) {
654 bool CanBuildRunTimeCheck = true;
655 // The run-time alias check places code that involves the base pointer at
656 // the beginning of the SCoP. This breaks if the base pointer is defined
657 // inside the scop. Hence, we can only create a run-time check if we are
658 // sure the base pointer is not an instruction defined inside the scop.
659 for (const auto &Ptr : AS) {
660 Instruction *Inst = dyn_cast<Instruction>(Ptr.getValue());
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000661 if (Inst && CurRegion.contains(Inst)) {
Tobias Grosser1eedb672014-09-24 21:04:29 +0000662 CanBuildRunTimeCheck = false;
663 break;
664 }
665 }
666
667 if (CanBuildRunTimeCheck)
668 return true;
669 }
Andreas Simbuergere2c92432014-06-26 10:19:57 +0000670 return invalid<ReportAlias>(Context, /*Assert=*/false, &Inst, AS);
Tobias Grosser1eedb672014-09-24 21:04:29 +0000671 }
Tobias Grosser75805372011-04-29 06:27:02 +0000672
673 return true;
674}
675
Tobias Grosser75805372011-04-29 06:27:02 +0000676bool ScopDetection::isValidInstruction(Instruction &Inst,
677 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000678 // We only check the call instruction but not invoke instruction.
679 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
680 if (isValidCallInst(*CI))
681 return true;
682
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000683 return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000684 }
685
686 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000687 if (!isa<AllocaInst>(Inst))
688 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000689
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000690 return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000691 }
692
693 // Check the access function.
Tobias Grosserd1e33e72015-02-19 05:31:07 +0000694 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) {
695 Context.hasStores |= isa<StoreInst>(Inst);
696 Context.hasLoads |= isa<LoadInst>(Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000697 return isValidMemoryAccess(Inst, Context);
Tobias Grosserd1e33e72015-02-19 05:31:07 +0000698 }
Tobias Grosser75805372011-04-29 06:27:02 +0000699
700 // We do not know this instruction, therefore we assume it is invalid.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000701 return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000702}
703
Johannes Doerfertd020b772015-08-27 06:53:52 +0000704bool ScopDetection::canUseISLTripCount(Loop *L,
705 DetectionContext &Context) const {
706
707 Region &CurRegion = Context.CurRegion;
708
709 // Ensure the loop has a single back edge.
710 if (L->getNumBackEdges() != 1)
711 return false;
712
713 // Ensure the loop has a single exiting block.
714 BasicBlock *ExitingBB = L->getExitingBlock();
715 if (!ExitingBB)
716 return false;
717
718 // Ensure the exiting block is terminated by a conditional branch.
719 BranchInst *Term = dyn_cast<BranchInst>(ExitingBB->getTerminator());
720 if (!Term || !Term->isConditional())
721 return false;
722
723 Value *Cond = Term->getCondition();
724
725 // If the terminating condition is an integer comparison, ensure that it is a
726 // comparison between a recurrence and an invariant value.
727 if (ICmpInst *I = dyn_cast<ICmpInst>(Cond)) {
728 const Value *Op0 = I->getOperand(0);
729 const Value *Op1 = I->getOperand(1);
730 const SCEV *LHS = SE->getSCEVAtScope(const_cast<Value *>(Op0), L);
731 const SCEV *RHS = SE->getSCEVAtScope(const_cast<Value *>(Op1), L);
732 if ((isa<SCEVAddRecExpr>(LHS) && !isInvariant(*Op1, CurRegion)) ||
733 (isa<SCEVAddRecExpr>(RHS) && !isInvariant(*Op0, CurRegion)))
734 return false;
735 }
736
737 // If the terminating condition is not an integer comparison, ensure that it
738 // is a constant.
739 else if (!isa<ConstantInt>(Cond))
740 return false;
741
742 // We can use ISL to compute the trip count of L.
743 return true;
744}
745
Tobias Grosser75805372011-04-29 06:27:02 +0000746bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000747 // Is the loop count affine?
Johannes Doerfertd020b772015-08-27 06:53:52 +0000748 bool IsLoopCountAffine = false;
Tobias Grosser75805372011-04-29 06:27:02 +0000749 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Johannes Doerfertd020b772015-08-27 06:53:52 +0000750 if (!isa<SCEVCouldNotCompute>(LoopCount))
751 IsLoopCountAffine = isAffineExpr(&Context.CurRegion, LoopCount, *SE);
752 else
753 IsLoopCountAffine = canUseISLTripCount(L, Context);
754 if (IsLoopCountAffine) {
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000755 Context.hasAffineLoops = true;
Johannes Doerfertba65c162015-02-24 11:45:21 +0000756 return true;
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000757 }
758
759 if (AllowNonAffineSubRegions) {
760 Region *R = RI->getRegionFor(L->getHeader());
761 if (R->contains(L))
762 if (addOverApproximatedRegion(R, Context))
763 return true;
764 }
Tobias Grosser75805372011-04-29 06:27:02 +0000765
Johannes Doerfertba65c162015-02-24 11:45:21 +0000766 return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount);
Tobias Grosser75805372011-04-29 06:27:02 +0000767}
768
Tobias Grossered21a1f2015-08-27 16:55:18 +0000769bool ScopDetection::hasMoreThanOneLoop(Region *R) const {
Tobias Grosser050e0cb2015-08-31 12:08:11 +0000770 auto LoopNum = 0;
Tobias Grossered21a1f2015-08-27 16:55:18 +0000771
Tobias Grosser050e0cb2015-08-31 12:08:11 +0000772 auto L = LI->getLoopFor(R->getEntry());
773 L = L ? R->outermostLoopInRegion(L) : nullptr;
774 L = L ? L->getParentLoop() : nullptr;
Tobias Grossered21a1f2015-08-27 16:55:18 +0000775
Tobias Grosser050e0cb2015-08-31 12:08:11 +0000776 auto SubLoops =
777 L ? L->getSubLoopsVector() : std::vector<Loop *>(LI->begin(), LI->end());
778
779 for (auto &SubLoop : SubLoops)
780 if (R->contains(SubLoop)) {
781 LoopNum++;
Tobias Grosser40820ca2015-08-31 21:04:51 +0000782 if (SubLoop->getSubLoopsVector().size() > 0)
Tobias Grosser050e0cb2015-08-31 12:08:11 +0000783 LoopNum++;
784
785 if (LoopNum >= 2)
Tobias Grossered21a1f2015-08-27 16:55:18 +0000786 return true;
Tobias Grosser050e0cb2015-08-31 12:08:11 +0000787 }
Tobias Grossered21a1f2015-08-27 16:55:18 +0000788 return false;
789}
790
Tobias Grosser75805372011-04-29 06:27:02 +0000791Region *ScopDetection::expandRegion(Region &R) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000792 // Initial no valid region was found (greater than R)
Tobias Grosserd5d93ec2015-06-04 17:59:54 +0000793 std::unique_ptr<Region> LastValidRegion;
794 auto ExpandedRegion = std::unique_ptr<Region>(R.getExpandedRegion());
Tobias Grosser75805372011-04-29 06:27:02 +0000795
796 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
797
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000798 while (ExpandedRegion) {
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000799 DetectionContext Context(
Tobias Grosserd5d93ec2015-06-04 17:59:54 +0000800 *ExpandedRegion, *AA, NonAffineSubRegionMap[ExpandedRegion.get()],
801 BoxedLoopsMap[ExpandedRegion.get()], false /* verifying */);
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000802 DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
Andreas Simbuergerb379edb2014-06-27 06:21:14 +0000803 // Only expand when we did not collect errors.
Tobias Grosser75805372011-04-29 06:27:02 +0000804
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000805 // Check the exit first (cheap)
Andreas Simbuergerb379edb2014-06-27 06:21:14 +0000806 if (isValidExit(Context) && !Context.Log.hasErrors()) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000807 // If the exit is valid check all blocks
808 // - if true, a valid region was found => store it + keep expanding
809 // - if false, .tbd. => stop (should this really end the loop?)
Andreas Simbuergerb379edb2014-06-27 06:21:14 +0000810 if (!allBlocksValid(Context) || Context.Log.hasErrors())
811 break;
812
Tobias Grosserd7e58642013-04-10 06:55:45 +0000813 // Store this region, because it is the greatest valid (encountered so
814 // far).
Tobias Grosserd5d93ec2015-06-04 17:59:54 +0000815 LastValidRegion = std::move(ExpandedRegion);
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000816
817 // Create and test the next greater region (if any)
Tobias Grosserd5d93ec2015-06-04 17:59:54 +0000818 ExpandedRegion =
819 std::unique_ptr<Region>(LastValidRegion->getExpandedRegion());
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000820
821 } else {
822 // Create and test the next greater region (if any)
Tobias Grosserd5d93ec2015-06-04 17:59:54 +0000823 ExpandedRegion =
824 std::unique_ptr<Region>(ExpandedRegion->getExpandedRegion());
Tobias Grosser75805372011-04-29 06:27:02 +0000825 }
Tobias Grosser75805372011-04-29 06:27:02 +0000826 }
827
Tobias Grosser378a9f22013-11-16 19:34:11 +0000828 DEBUG({
829 if (LastValidRegion)
830 dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
831 else
832 dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
833 });
Tobias Grosser75805372011-04-29 06:27:02 +0000834
Tobias Grosserd5d93ec2015-06-04 17:59:54 +0000835 return LastValidRegion.release();
Tobias Grosser75805372011-04-29 06:27:02 +0000836}
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000837static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
Tobias Grosser26108892014-04-02 20:18:19 +0000838 for (const BasicBlock *BB : R.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000839 if (R.contains(LI->getLoopFor(BB)))
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000840 return false;
841
842 return true;
843}
Tobias Grosser75805372011-04-29 06:27:02 +0000844
Tobias Grosser28a70c52014-01-29 19:05:30 +0000845// Remove all direct and indirect children of region R from the region set Regs,
846// but do not recurse further if the first child has been found.
847//
848// Return the number of regions erased from Regs.
David Peixotto8da2b932014-10-22 20:39:07 +0000849static unsigned eraseAllChildren(ScopDetection::RegionSet &Regs,
David Blaikieb035f6d2014-04-15 18:45:27 +0000850 const Region &R) {
Tobias Grosser28a70c52014-01-29 19:05:30 +0000851 unsigned Count = 0;
David Blaikieb035f6d2014-04-15 18:45:27 +0000852 for (auto &SubRegion : R) {
David Peixotto8da2b932014-10-22 20:39:07 +0000853 if (Regs.count(SubRegion.get())) {
Tobias Grosser28a70c52014-01-29 19:05:30 +0000854 ++Count;
David Peixotto8da2b932014-10-22 20:39:07 +0000855 Regs.remove(SubRegion.get());
Tobias Grosser28a70c52014-01-29 19:05:30 +0000856 } else {
David Blaikieb035f6d2014-04-15 18:45:27 +0000857 Count += eraseAllChildren(Regs, *SubRegion);
Tobias Grosser28a70c52014-01-29 19:05:30 +0000858 }
859 }
860 return Count;
861}
862
Tobias Grosser75805372011-04-29 06:27:02 +0000863void ScopDetection::findScops(Region &R) {
Johannes Doerfertf3e98f42015-04-12 22:52:20 +0000864 DetectionContext Context(R, *AA, NonAffineSubRegionMap[&R], BoxedLoopsMap[&R],
Johannes Doerfertba65c162015-02-24 11:45:21 +0000865 false /*verifying*/);
Johannes Doerfert6a4d81c2015-03-08 15:11:50 +0000866
867 bool RegionIsValid = false;
Tobias Grosser02e65892015-09-08 19:46:41 +0000868 if (!DetectUnprofitable && regionWithoutLoops(R, LI))
Johannes Doerfert6a4d81c2015-03-08 15:11:50 +0000869 invalid<ReportUnprofitable>(Context, /*Assert=*/true, &R);
870 else
871 RegionIsValid = isValidRegion(Context);
872
Johannes Doerfert3f1c2852015-02-19 18:11:50 +0000873 bool HasErrors = !RegionIsValid || Context.Log.size() > 0;
Andreas Simbuerger04472402014-05-24 09:25:10 +0000874
Johannes Doerfert3f1c2852015-02-19 18:11:50 +0000875 if (PollyTrackFailures && HasErrors)
876 RejectLogs.insert(std::make_pair(&R, Context.Log));
877
878 if (!HasErrors) {
Tobias Grosser75805372011-04-29 06:27:02 +0000879 ++ValidRegion;
880 ValidRegions.insert(&R);
881 return;
882 }
883
David Blaikieb035f6d2014-04-15 18:45:27 +0000884 for (auto &SubRegion : R)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000885 findScops(*SubRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000886
887 // Try to expand regions.
888 //
889 // As the region tree normally only contains canonical regions, non canonical
890 // regions that form a Scop are not found. Therefore, those non canonical
891 // regions are checked by expanding the canonical ones.
892
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000893 std::vector<Region *> ToExpand;
Tobias Grosser75805372011-04-29 06:27:02 +0000894
David Blaikieb035f6d2014-04-15 18:45:27 +0000895 for (auto &SubRegion : R)
896 ToExpand.push_back(SubRegion.get());
Tobias Grosser75805372011-04-29 06:27:02 +0000897
Tobias Grosser26108892014-04-02 20:18:19 +0000898 for (Region *CurrentRegion : ToExpand) {
Andreas Simbuergerb379edb2014-06-27 06:21:14 +0000899 // Skip regions that had errors.
900 bool HadErrors = RejectLogs.hasErrors(CurrentRegion);
901 if (HadErrors)
902 continue;
903
Tobias Grosser75805372011-04-29 06:27:02 +0000904 // Skip invalid regions. Regions may become invalid, if they are element of
905 // an already expanded region.
David Peixotto8da2b932014-10-22 20:39:07 +0000906 if (!ValidRegions.count(CurrentRegion))
Tobias Grosser75805372011-04-29 06:27:02 +0000907 continue;
908
909 Region *ExpandedR = expandRegion(*CurrentRegion);
910
911 if (!ExpandedR)
912 continue;
913
914 R.addSubRegion(ExpandedR, true);
915 ValidRegions.insert(ExpandedR);
David Peixotto8da2b932014-10-22 20:39:07 +0000916 ValidRegions.remove(CurrentRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000917
Tobias Grosser28a70c52014-01-29 19:05:30 +0000918 // Erase all (direct and indirect) children of ExpandedR from the valid
919 // regions and update the number of valid regions.
David Blaikieb035f6d2014-04-15 18:45:27 +0000920 ValidRegion -= eraseAllChildren(ValidRegions, *ExpandedR);
Tobias Grosser75805372011-04-29 06:27:02 +0000921 }
922}
923
924bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000925 Region &CurRegion = Context.CurRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000926
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000927 for (const BasicBlock *BB : CurRegion.blocks()) {
Tobias Grosser1d191902014-03-03 13:13:55 +0000928 Loop *L = LI->getLoopFor(BB);
Andreas Simbuerger04472402014-05-24 09:25:10 +0000929 if (L && L->getHeader() == BB && (!isValidLoop(L, Context) && !KeepGoing))
Sebastian Popb88ea5e2013-06-11 22:20:32 +0000930 return false;
931 }
932
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000933 for (BasicBlock *BB : CurRegion.blocks())
Andreas Simbuerger04472402014-05-24 09:25:10 +0000934 if (!isValidCFG(*BB, Context) && !KeepGoing)
Sebastian Pop9e3d2dd2013-06-11 22:20:27 +0000935 return false;
936
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000937 for (BasicBlock *BB : CurRegion.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000938 for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I)
Andreas Simbuerger04472402014-05-24 09:25:10 +0000939 if (!isValidInstruction(*I, Context) && !KeepGoing)
Sebastian Pop8ca899c2013-06-14 20:20:43 +0000940 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000941
Sebastian Pope8863b82014-05-12 19:02:02 +0000942 if (!hasAffineMemoryAccesses(Context))
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000943 return false;
944
Tobias Grosser75805372011-04-29 06:27:02 +0000945 return true;
946}
947
948bool ScopDetection::isValidExit(DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000949
950 // PHI nodes are not allowed in the exit basic block.
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000951 if (BasicBlock *Exit = Context.CurRegion.getExit()) {
Tobias Grosser75805372011-04-29 06:27:02 +0000952 BasicBlock::iterator I = Exit->begin();
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000953 if (I != Exit->end() && isa<PHINode>(*I))
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000954 return invalid<ReportPHIinExit>(Context, /*Assert=*/true, I);
Tobias Grosser75805372011-04-29 06:27:02 +0000955 }
956
957 return true;
958}
959
960bool ScopDetection::isValidRegion(DetectionContext &Context) const {
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000961 Region &CurRegion = Context.CurRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000962
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000963 DEBUG(dbgs() << "Checking region: " << CurRegion.getNameStr() << "\n\t");
Tobias Grosser75805372011-04-29 06:27:02 +0000964
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000965 if (CurRegion.isTopLevelRegion()) {
Tobias Grosserf084edd2014-10-22 23:00:03 +0000966 DEBUG(dbgs() << "Top level region is invalid\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000967 return false;
968 }
969
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000970 if (!CurRegion.getEntry()->getName().count(OnlyRegion)) {
Tobias Grosser4449e522014-01-27 14:24:53 +0000971 DEBUG({
972 dbgs() << "Region entry does not match -polly-region-only";
973 dbgs() << "\n";
974 });
975 return false;
976 }
977
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000978 if (!CurRegion.getEnteringBlock()) {
979 BasicBlock *entry = CurRegion.getEntry();
Sebastian Pop9d632342013-06-11 22:20:40 +0000980 Loop *L = LI->getLoopFor(entry);
981
982 if (L) {
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000983 if (!L->isLoopSimplifyForm())
984 return invalid<ReportSimpleLoop>(Context, /*Assert=*/true);
Sebastian Pop9d632342013-06-11 22:20:40 +0000985
986 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
987 ++PI) {
988 // Region entering edges come from the same loop but outside the region
989 // are not allowed.
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000990 if (L->contains(*PI) && !CurRegion.contains(*PI))
Andreas Simbuerger5569bf32014-06-26 10:06:40 +0000991 return invalid<ReportIndEdge>(Context, /*Assert=*/true, *PI);
Sebastian Pop9d632342013-06-11 22:20:40 +0000992 }
993 }
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000994 }
995
Tobias Grosserd654c252012-04-10 18:12:19 +0000996 // SCoP cannot contain the entry block of the function, because we need
Tobias Grosser75805372011-04-29 06:27:02 +0000997 // to insert alloca instruction there when translate scalar to array.
Johannes Doerfertfb79a962015-02-23 14:18:28 +0000998 if (CurRegion.getEntry() ==
999 &(CurRegion.getEntry()->getParent()->getEntryBlock()))
1000 return invalid<ReportEntry>(Context, /*Assert=*/true, CurRegion.getEntry());
Tobias Grosser75805372011-04-29 06:27:02 +00001001
Tobias Grossered21a1f2015-08-27 16:55:18 +00001002 if (!DetectUnprofitable && !hasMoreThanOneLoop(&CurRegion))
1003 invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion);
1004
Hongbin Zheng94868e62012-04-07 12:29:17 +00001005 if (!isValidExit(Context))
Tobias Grosser75805372011-04-29 06:27:02 +00001006 return false;
1007
Hongbin Zheng94868e62012-04-07 12:29:17 +00001008 if (!allBlocksValid(Context))
Tobias Grosser75805372011-04-29 06:27:02 +00001009 return false;
1010
Tobias Grosserd1e33e72015-02-19 05:31:07 +00001011 // We can probably not do a lot on scops that only write or only read
1012 // data.
1013 if (!DetectUnprofitable && (!Context.hasStores || !Context.hasLoads))
Johannes Doerfert6a4d81c2015-03-08 15:11:50 +00001014 invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion);
Tobias Grosserd1e33e72015-02-19 05:31:07 +00001015
Johannes Doerfertf3e98f42015-04-12 22:52:20 +00001016 // Check if there was at least one non-overapproximated loop in the region or
1017 // we allow regions without loops.
Tobias Grosser02e65892015-09-08 19:46:41 +00001018 if (!DetectUnprofitable && !Context.hasAffineLoops)
Johannes Doerfertf3e98f42015-04-12 22:52:20 +00001019 invalid<ReportUnprofitable>(Context, /*Assert=*/true, &CurRegion);
1020
Tobias Grosser75805372011-04-29 06:27:02 +00001021 DEBUG(dbgs() << "OK\n");
1022 return true;
1023}
1024
Johannes Doerfert43e1ead2014-07-15 21:06:48 +00001025void ScopDetection::markFunctionAsInvalid(Function *F) const {
1026 F->addFnAttr(PollySkipFnAttr);
1027}
1028
Tobias Grosser75805372011-04-29 06:27:02 +00001029bool ScopDetection::isValidFunction(llvm::Function &F) {
Johannes Doerfert43e1ead2014-07-15 21:06:48 +00001030 return !F.hasFnAttribute(PollySkipFnAttr);
Tobias Grosser75805372011-04-29 06:27:02 +00001031}
1032
Tobias Grosserb2863ca2013-03-04 19:49:51 +00001033void ScopDetection::printLocations(llvm::Function &F) {
Tobias Grosser26108892014-04-02 20:18:19 +00001034 for (const Region *R : *this) {
Tobias Grosser531891e2012-11-01 16:45:20 +00001035 unsigned LineEntry, LineExit;
1036 std::string FileName;
1037
Tobias Grosser00dc3092014-03-02 12:02:46 +00001038 getDebugLocation(R, LineEntry, LineExit, FileName);
Tobias Grosser8519f892013-12-18 10:49:53 +00001039 DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit);
1040 F.getContext().diagnose(Diagnostic);
Tobias Grosser531891e2012-11-01 16:45:20 +00001041 }
1042}
1043
Daniel Jasper8a1dea02014-10-27 19:45:31 +00001044void ScopDetection::emitMissedRemarksForValidRegions(
1045 const Function &F, const RegionSet &ValidRegions) {
Andreas Simbuerger5569bf32014-06-26 10:06:40 +00001046 for (const Region *R : ValidRegions) {
1047 const Region *Parent = R->getParent();
1048 if (Parent && !Parent->isTopLevelRegion() && RejectLogs.count(Parent))
1049 emitRejectionRemarks(F, RejectLogs.at(Parent));
1050 }
1051}
1052
1053void ScopDetection::emitMissedRemarksForLeaves(const Function &F,
1054 const Region *R) {
1055 for (const std::unique_ptr<Region> &Child : *R) {
1056 bool IsValid = ValidRegions.count(Child.get());
1057 if (IsValid)
1058 continue;
1059
1060 bool IsLeaf = Child->begin() == Child->end();
1061 if (!IsLeaf)
1062 emitMissedRemarksForLeaves(F, Child.get());
1063 else {
1064 if (RejectLogs.count(Child.get())) {
1065 emitRejectionRemarks(F, RejectLogs.at(Child.get()));
1066 }
1067 }
1068 }
1069}
1070
Tobias Grosser75805372011-04-29 06:27:02 +00001071bool ScopDetection::runOnFunction(llvm::Function &F) {
Chandler Carruthf5579872015-01-17 14:16:56 +00001072 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Matt Arsenault8ca36812014-07-19 18:40:17 +00001073 RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
Tobias Grosser02e65892015-09-08 19:46:41 +00001074 if (!DetectUnprofitable && LI->empty())
Sebastian Pop8fe6d112013-05-30 17:47:32 +00001075 return false;
1076
Tobias Grosser75805372011-04-29 06:27:02 +00001077 AA = &getAnalysis<AliasAnalysis>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +00001078 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Tobias Grosser75805372011-04-29 06:27:02 +00001079 Region *TopRegion = RI->getTopLevelRegion();
1080
Tobias Grosser2ff87232011-10-23 11:17:06 +00001081 releaseMemory();
1082
Tobias Grossera3ab27e2014-05-07 11:23:32 +00001083 if (OnlyFunction != "" && !F.getName().count(OnlyFunction))
Tobias Grosser2ff87232011-10-23 11:17:06 +00001084 return false;
1085
Tobias Grosser1bb59b02012-12-29 23:47:38 +00001086 if (!isValidFunction(F))
Tobias Grosser75805372011-04-29 06:27:02 +00001087 return false;
1088
1089 findScops(*TopRegion);
Tobias Grosser531891e2012-11-01 16:45:20 +00001090
Andreas Simbuerger5569bf32014-06-26 10:06:40 +00001091 // Only makes sense when we tracked errors.
1092 if (PollyTrackFailures) {
1093 emitMissedRemarksForValidRegions(F, ValidRegions);
1094 emitMissedRemarksForLeaves(F, TopRegion);
1095 }
1096
1097 for (const Region *R : ValidRegions)
1098 emitValidRemarks(F, R);
1099
Johannes Doerferta05214f2014-10-15 23:24:28 +00001100 if (ReportLevel)
Tobias Grosserb2863ca2013-03-04 19:49:51 +00001101 printLocations(F);
Tobias Grosser531891e2012-11-01 16:45:20 +00001102
Tobias Grosser75805372011-04-29 06:27:02 +00001103 return false;
1104}
1105
Johannes Doerfertba65c162015-02-24 11:45:21 +00001106bool ScopDetection::isNonAffineSubRegion(const Region *SubR,
1107 const Region *ScopR) const {
1108 return NonAffineSubRegionMap.lookup(ScopR).count(SubR);
1109}
1110
Johannes Doerfertf3e98f42015-04-12 22:52:20 +00001111const ScopDetection::BoxedLoopsSetTy *
1112ScopDetection::getBoxedLoops(const Region *R) const {
1113 auto BLMIt = BoxedLoopsMap.find(R);
1114 if (BLMIt == BoxedLoopsMap.end())
1115 return nullptr;
1116 return &BLMIt->second;
1117}
1118
Tobias Grosser75805372011-04-29 06:27:02 +00001119void polly::ScopDetection::verifyRegion(const Region &R) const {
1120 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
Johannes Doerfertf3e98f42015-04-12 22:52:20 +00001121
1122 BoxedLoopsSetTy DummyBoxedLoopsSet;
Johannes Doerfertba65c162015-02-24 11:45:21 +00001123 NonAffineSubRegionSetTy DummyNonAffineSubRegionSet;
1124 DetectionContext Context(const_cast<Region &>(R), *AA,
Johannes Doerfertf3e98f42015-04-12 22:52:20 +00001125 DummyNonAffineSubRegionSet, DummyBoxedLoopsSet,
1126 true /*verifying*/);
Tobias Grosser75805372011-04-29 06:27:02 +00001127 isValidRegion(Context);
1128}
1129
1130void polly::ScopDetection::verifyAnalysis() const {
Tobias Grossera1689932014-02-18 18:49:49 +00001131 if (!VerifyScops)
1132 return;
1133
Tobias Grosser26108892014-04-02 20:18:19 +00001134 for (const Region *R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +00001135 verifyRegion(*R);
Tobias Grosser75805372011-04-29 06:27:02 +00001136}
1137
1138void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruthf5579872015-01-17 14:16:56 +00001139 AU.addRequired<LoopInfoWrapperPass>();
Tobias Grosserc5bcf242015-08-17 10:57:08 +00001140 AU.addRequired<ScalarEvolutionWrapperPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00001141 // We also need AA and RegionInfo when we are verifying analysis.
1142 AU.addRequiredTransitive<AliasAnalysis>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00001143 AU.addRequiredTransitive<RegionInfoPass>();
Tobias Grosser75805372011-04-29 06:27:02 +00001144 AU.setPreservesAll();
1145}
1146
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001147void ScopDetection::print(raw_ostream &OS, const Module *) const {
Tobias Grosser26108892014-04-02 20:18:19 +00001148 for (const Region *R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +00001149 OS << "Valid Region for Scop: " << R->getNameStr() << '\n';
Tobias Grosser75805372011-04-29 06:27:02 +00001150
1151 OS << "\n";
1152}
1153
1154void ScopDetection::releaseMemory() {
1155 ValidRegions.clear();
Andreas Simbuerger4870e092014-05-24 09:25:01 +00001156 RejectLogs.clear();
Johannes Doerfertba65c162015-02-24 11:45:21 +00001157 NonAffineSubRegionMap.clear();
Tobias Grosser4b6aa6e2015-04-18 11:01:25 +00001158 InsnToMemAcc.clear();
Andreas Simbuerger4870e092014-05-24 09:25:01 +00001159
Hongbin Zheng94c5df12011-05-06 02:38:20 +00001160 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +00001161}
1162
1163char ScopDetection::ID = 0;
1164
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001165Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
1166
Tobias Grosser73600b82011-10-08 00:30:40 +00001167INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
1168 "Polly - Detect static control parts (SCoPs)", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +00001169 false);
1170INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Chandler Carruthf5579872015-01-17 14:16:56 +00001171INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00001172INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosserc5bcf242015-08-17 10:57:08 +00001173INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
Tobias Grosser73600b82011-10-08 00:30:40 +00001174INITIALIZE_PASS_END(ScopDetection, "polly-detect",
1175 "Polly - Detect static control parts (SCoPs)", false, false)