blob: f75c2e98dca813444e05e9657e9920425c7a6e1d [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 Grosser75805372011-04-29 06:27:02 +000048#include "polly/LinkAllPasses.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000049#include "polly/Options.h"
Andreas Simbuerger01a37a02014-04-02 11:54:01 +000050#include "polly/ScopDetectionDiagnostic.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000051#include "polly/ScopDetection.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +000052#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000053#include "polly/Support/ScopHelper.h"
Tobias Grosser75805372011-04-29 06:27:02 +000054#include "llvm/ADT/Statistic.h"
55#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000056#include "llvm/Analysis/LoopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000057#include "llvm/Analysis/RegionIterator.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000058#include "llvm/Analysis/ScalarEvolution.h"
Tobias Grosserb8710b52011-11-10 12:44:50 +000059#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruth6b96c242014-03-06 00:47:27 +000060#include "llvm/IR/DebugInfo.h"
Tobias Grosser8519f892013-12-18 10:49:53 +000061#include "llvm/IR/DiagnosticInfo.h"
62#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth6b96c242014-03-06 00:47:27 +000063#include "llvm/IR/LLVMContext.h"
Tobias Grosser75805372011-04-29 06:27:02 +000064#include "llvm/Support/Debug.h"
Tobias Grosser60b54f12011-11-08 15:41:28 +000065#include <set>
66
Tobias Grosser75805372011-04-29 06:27:02 +000067using namespace llvm;
68using namespace polly;
69
Chandler Carruth95fef942014-04-22 03:30:19 +000070#define DEBUG_TYPE "polly-detect"
71
Sebastian Pop8fe6d112013-05-30 17:47:32 +000072static cl::opt<bool>
73DetectScopsWithoutLoops("polly-detect-scops-in-functions-without-loops",
74 cl::desc("Detect scops in functions without loops"),
Tobias Grosser64e8e372014-03-13 23:37:43 +000075 cl::Hidden, cl::init(false), cl::ZeroOrMore,
76 cl::cat(PollyCategory));
Sebastian Pop8fe6d112013-05-30 17:47:32 +000077
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +000078static cl::opt<bool>
79DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops",
80 cl::desc("Detect scops in regions without loops"),
Tobias Grosser64e8e372014-03-13 23:37:43 +000081 cl::Hidden, cl::init(false), cl::ZeroOrMore,
82 cl::cat(PollyCategory));
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +000083
Tobias Grosser2ff87232011-10-23 11:17:06 +000084static cl::opt<std::string>
Tobias Grossera3ab27e2014-05-07 11:23:32 +000085OnlyFunction("polly-only-func",
86 cl::desc("Only run on functions that contain a certain string"),
87 cl::value_desc("string"), cl::ValueRequired, cl::init(""),
Tobias Grosser637bd632013-05-07 07:31:10 +000088 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000089
Tobias Grosser4449e522014-01-27 14:24:53 +000090static cl::opt<std::string>
91OnlyRegion("polly-only-region",
92 cl::desc("Only run on certain regions (The provided identifier must "
93 "appear in the name of the region's entry block"),
94 cl::value_desc("identifier"), cl::ValueRequired, cl::init(""),
95 cl::cat(PollyCategory));
96
Tobias Grosser60cd9322011-11-10 12:47:26 +000097static cl::opt<bool>
98IgnoreAliasing("polly-ignore-aliasing",
99 cl::desc("Ignore possible aliasing of the array bases"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000100 cl::Hidden, cl::init(false), cl::ZeroOrMore,
101 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +0000102
Tobias Grosser637bd632013-05-07 07:31:10 +0000103static cl::opt<bool>
104ReportLevel("polly-report",
105 cl::desc("Print information about the activities of Polly"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000106 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosser531891e2012-11-01 16:45:20 +0000107
108static cl::opt<bool>
Tobias Grossera1879642011-12-20 10:43:14 +0000109AllowNonAffine("polly-allow-nonaffine",
110 cl::desc("Allow non affine access functions in arrays"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000111 cl::Hidden, cl::init(false), cl::ZeroOrMore,
112 cl::cat(PollyCategory));
Tobias Grossera1879642011-12-20 10:43:14 +0000113
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000114static cl::opt<bool, true>
115TrackFailures("polly-detect-track-failures",
116 cl::desc("Track failure strings in detecting scop regions"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000117 cl::location(PollyTrackFailures), cl::Hidden, cl::ZeroOrMore,
118 cl::init(false), cl::cat(PollyCategory));
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000119
Sebastian Pop18016682014-04-08 21:20:44 +0000120static cl::opt<bool, true>
121PollyDelinearizeX("polly-delinearize",
122 cl::desc("Delinearize array access functions"),
123 cl::location(PollyDelinearize), cl::Hidden, cl::ZeroOrMore,
124 cl::init(false), cl::cat(PollyCategory));
125
Tobias Grossera1689932014-02-18 18:49:49 +0000126static cl::opt<bool>
127VerifyScops("polly-detect-verify",
128 cl::desc("Verify the detected SCoPs after each transformation"),
Tobias Grosser64e8e372014-03-13 23:37:43 +0000129 cl::Hidden, cl::init(false), cl::ZeroOrMore,
130 cl::cat(PollyCategory));
Tobias Grossera1689932014-02-18 18:49:49 +0000131
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000132bool polly::PollyTrackFailures = false;
Sebastian Pop18016682014-04-08 21:20:44 +0000133bool polly::PollyDelinearize = false;
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000134
Tobias Grosser75805372011-04-29 06:27:02 +0000135//===----------------------------------------------------------------------===//
136// Statistics.
137
138STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
139
Tobias Grosser8519f892013-12-18 10:49:53 +0000140class DiagnosticScopFound : public DiagnosticInfo {
141private:
142 static int PluginDiagnosticKind;
143
144 Function &F;
145 std::string FileName;
146 unsigned EntryLine, ExitLine;
147
148public:
Tobias Grosser1b12f462013-12-18 11:14:36 +0000149 DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine,
150 unsigned ExitLine)
Tobias Grosser8519f892013-12-18 10:49:53 +0000151 : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName),
Tobias Grosser1b12f462013-12-18 11:14:36 +0000152 EntryLine(EntryLine), ExitLine(ExitLine) {}
Tobias Grosser8519f892013-12-18 10:49:53 +0000153
154 virtual void print(DiagnosticPrinter &DP) const;
155
156 static bool classof(const DiagnosticInfo *DI) {
157 return DI->getKind() == PluginDiagnosticKind;
158 }
159};
160
161int DiagnosticScopFound::PluginDiagnosticKind = 10;
162
Tobias Grosser8519f892013-12-18 10:49:53 +0000163void DiagnosticScopFound::print(DiagnosticPrinter &DP) const {
Tobias Grosser1b12f462013-12-18 11:14:36 +0000164 DP << "Polly detected an optimizable loop region (scop) in function '" << F
165 << "'\n";
Tobias Grosser8519f892013-12-18 10:49:53 +0000166
167 if (FileName.empty()) {
168 DP << "Scop location is unknown. Compile with debug info "
169 "(-g) to get more precise information. ";
Tobias Grosser1b12f462013-12-18 11:14:36 +0000170 return;
Tobias Grosser8519f892013-12-18 10:49:53 +0000171 }
172
173 DP << FileName << ":" << EntryLine << ": Start of scop\n";
174 DP << FileName << ":" << ExitLine << ": End of scop";
175}
176
Tobias Grosser75805372011-04-29 06:27:02 +0000177//===----------------------------------------------------------------------===//
178// ScopDetection.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000179
180template <class RR, typename... Args>
181inline bool ScopDetection::invalid(DetectionContext &Context, bool Assert,
182 Args &&... Arguments) const {
183
184 if (!Context.Verifying) {
185 RR RejectReason = RR(Arguments...);
186 if (PollyTrackFailures)
187 LastFailure = RejectReason.getMessage();
188
189 DEBUG(dbgs() << RejectReason.getMessage());
190 DEBUG(dbgs() << "\n");
191 } else {
192 assert(!Assert && "Verification of detected scop failed");
193 }
194
195 return false;
196}
197
Tobias Grossera1689932014-02-18 18:49:49 +0000198bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const {
199 if (!ValidRegions.count(&R))
200 return false;
201
202 if (Verify)
203 return isValidRegion(const_cast<Region &>(R));
204
205 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000206}
207
Tobias Grosser4f129a62011-10-08 00:30:55 +0000208std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
209 if (!InvalidRegions.count(R))
210 return "";
211
212 return InvalidRegions.find(R)->second;
213}
214
Tobias Grossere602a072013-05-07 07:30:56 +0000215bool ScopDetection::isValidCFG(BasicBlock &BB,
216 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000217 Region &RefRegion = Context.CurRegion;
218 TerminatorInst *TI = BB.getTerminator();
219
220 // Return instructions are only valid if the region is the top level region.
221 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
222 return true;
223
224 BranchInst *Br = dyn_cast<BranchInst>(TI);
225
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000226 if (!Br)
227 return invalid<ReportNonBranchTerminator>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000228
Tobias Grosser74394f02013-01-14 22:40:23 +0000229 if (Br->isUnconditional())
230 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000231
232 Value *Condition = Br->getCondition();
233
234 // UndefValue is not allowed as condition.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000235 if (isa<UndefValue>(Condition))
236 return invalid<ReportUndefCond>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000237
238 // Only Constant and ICmpInst are allowed as condition.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000239 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
240 return invalid<ReportInvalidCond>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000241
242 // Allow perfectly nested conditions.
243 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
244
245 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
246 // Unsigned comparisons are not allowed. They trigger overflow problems
247 // in the code generation.
248 //
249 // TODO: This is not sufficient and just hides bugs. However it does pretty
250 // well.
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000251 if (ICmp->isUnsigned())
Tobias Grosser75805372011-04-29 06:27:02 +0000252 return false;
253
254 // Are both operands of the ICmp affine?
Tobias Grosser74394f02013-01-14 22:40:23 +0000255 if (isa<UndefValue>(ICmp->getOperand(0)) ||
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000256 isa<UndefValue>(ICmp->getOperand(1)))
257 return invalid<ReportUndefOperand>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000258
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000259 Loop *L = LI->getLoopFor(ICmp->getParent());
260 const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
261 const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
Tobias Grosser75805372011-04-29 06:27:02 +0000262
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000263 if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000264 !isAffineExpr(&Context.CurRegion, RHS, *SE))
265 return invalid<ReportNonAffBranch>(Context, /*Assert=*/true, &BB, LHS,
266 RHS);
Tobias Grosser75805372011-04-29 06:27:02 +0000267 }
268
269 // Allow loop exit conditions.
270 Loop *L = LI->getLoopFor(&BB);
271 if (L && L->getExitingBlock() == &BB)
272 return true;
273
274 // Allow perfectly nested conditions.
275 Region *R = RI->getRegionFor(&BB);
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000276 if (R->getEntry() != &BB)
277 return invalid<ReportCondition>(Context, /*Assert=*/true, &BB);
Tobias Grosser75805372011-04-29 06:27:02 +0000278
279 return true;
280}
281
282bool ScopDetection::isValidCallInst(CallInst &CI) {
283 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
284 return false;
285
286 if (CI.doesNotAccessMemory())
287 return true;
288
289 Function *CalledFunction = CI.getCalledFunction();
290
291 // Indirect calls are not supported.
292 if (CalledFunction == 0)
293 return false;
294
295 // TODO: Intrinsics.
296 return false;
297}
298
Tobias Grosser458fb782014-01-28 12:58:58 +0000299bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const {
300 // A reference to function argument or constant value is invariant.
301 if (isa<Argument>(Val) || isa<Constant>(Val))
302 return true;
303
304 const Instruction *I = dyn_cast<Instruction>(&Val);
305 if (!I)
306 return false;
307
308 if (!Reg.contains(I))
309 return true;
310
311 if (I->mayHaveSideEffects())
312 return false;
313
314 // When Val is a Phi node, it is likely not invariant. We do not check whether
315 // Phi nodes are actually invariant, we assume that Phi nodes are usually not
316 // invariant. Recursively checking the operators of Phi nodes would lead to
317 // infinite recursion.
318 if (isa<PHINode>(*I))
319 return false;
320
Tobias Grosser26108892014-04-02 20:18:19 +0000321 for (const Use &Operand : I->operands())
Tobias Grosser1d191902014-03-03 13:13:55 +0000322 if (!isInvariant(*Operand, Reg))
Tobias Grosser458fb782014-01-28 12:58:58 +0000323 return false;
Tobias Grosser458fb782014-01-28 12:58:58 +0000324
325 // When the instruction is a load instruction, check that no write to memory
326 // in the region aliases with the load.
327 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
328 AliasAnalysis::Location Loc = AA->getLocation(LI);
329 const Region::const_block_iterator BE = Reg.block_end();
330 // Check if any basic block in the region can modify the location pointed to
331 // by 'Loc'. If so, 'Val' is (likely) not invariant in the region.
Tobias Grosser26108892014-04-02 20:18:19 +0000332 for (const BasicBlock *BB : Reg.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000333 if (AA->canBasicBlockModify(*BB, Loc))
Tobias Grosser458fb782014-01-28 12:58:58 +0000334 return false;
Tobias Grosser458fb782014-01-28 12:58:58 +0000335 }
336
337 return true;
338}
339
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000340bool
Sebastian Pope8863b82014-05-12 19:02:02 +0000341ScopDetection::hasAffineMemoryAccesses(DetectionContext &Context) const {
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000342 for (auto P : Context.NonAffineAccesses) {
343 const SCEVUnknown *BasePointer = P.first;
344 Value *BaseValue = BasePointer->getValue();
345
346 // First step: collect parametric terms in all array references.
347 SmallVector<const SCEV *, 4> Terms;
348 for (const SCEVAddRecExpr *AF : Context.NonAffineAccesses[BasePointer])
349 AF->collectParametricTerms(*SE, Terms);
350
Sebastian Pope8863b82014-05-12 19:02:02 +0000351 // Also collect terms from the affine memory accesses.
352 for (const SCEVAddRecExpr *AF : Context.AffineAccesses[BasePointer])
353 AF->collectParametricTerms(*SE, Terms);
354
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000355 // Second step: find array shape.
356 SmallVector<const SCEV *, 4> Sizes;
357 SE->findArrayDimensions(Terms, Sizes);
358
359 // Third step: compute the access functions for each subscript.
360 for (const SCEVAddRecExpr *AF : Context.NonAffineAccesses[BasePointer]) {
Sebastian Pope8863b82014-05-12 19:02:02 +0000361 if (Sizes.empty())
362 return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF);
363
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000364 SmallVector<const SCEV *, 4> Subscripts;
Sebastian Pope8863b82014-05-12 19:02:02 +0000365 if (!AF->computeAccessFunctions(*SE, Subscripts, Sizes) ||
366 Sizes.empty() || Subscripts.empty())
367 return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF);
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000368
369 // Check that the delinearized subscripts are affine.
370 for (const SCEV *S : Subscripts)
371 if (!isAffineExpr(&Context.CurRegion, S, *SE, BaseValue))
372 return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true, AF);
373 }
374 }
Sebastian Pope8863b82014-05-12 19:02:02 +0000375 return true;
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000376}
377
Tobias Grosser75805372011-04-29 06:27:02 +0000378bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
379 DetectionContext &Context) const {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000380 Value *Ptr = getPointerOperand(Inst);
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000381 Loop *L = LI->getLoopFor(Inst.getParent());
382 const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000383 const SCEVUnknown *BasePointer;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000384 Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000385
Tobias Grosserb8710b52011-11-10 12:44:50 +0000386 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
387
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000388 if (!BasePointer)
389 return invalid<ReportNoBasePtr>(Context, /*Assert=*/true);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000390
391 BaseValue = BasePointer->getValue();
392
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000393 if (isa<UndefValue>(BaseValue))
394 return invalid<ReportUndefBasePtr>(Context, /*Assert=*/true);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000395
Tobias Grosser458fb782014-01-28 12:58:58 +0000396 // Check that the base address of the access is invariant in the current
397 // region.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000398 if (!isInvariant(*BaseValue, Context.CurRegion))
Tobias Grosserab2227a2014-01-28 13:43:24 +0000399 // Verification of this property is difficult as the independent blocks
400 // pass may introduce aliasing that we did not have when running the
401 // scop detection.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000402 return invalid<ReportVariantBasePtr>(Context, /*Assert=*/false, BaseValue);
Tobias Grosser458fb782014-01-28 12:58:58 +0000403
Tobias Grosserb8710b52011-11-10 12:44:50 +0000404 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
405
Sebastian Pop18016682014-04-08 21:20:44 +0000406 if (AllowNonAffine) {
407 // Do not check whether AccessFunction is affine.
Sebastian Popcd3bb592014-04-10 16:08:11 +0000408 } else if (!isAffineExpr(&Context.CurRegion, AccessFunction, *SE,
409 BaseValue)) {
410 const SCEVAddRecExpr *AF = dyn_cast<SCEVAddRecExpr>(AccessFunction);
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000411
Sebastian Popcd3bb592014-04-10 16:08:11 +0000412 if (!PollyDelinearize || !AF)
413 return invalid<ReportNonAffineAccess>(Context, /*Assert=*/true,
414 AccessFunction);
415
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000416 // Collect all non affine memory accesses, and check whether they are linear
417 // at the end of scop detection. That way we can delinearize all the memory
418 // accesses to the same array in a unique step.
419 if (Context.NonAffineAccesses[BasePointer].size() == 0)
420 Context.NonAffineAccesses[BasePointer] = AFs();
421 Context.NonAffineAccesses[BasePointer].push_back(AF);
Sebastian Pope8863b82014-05-12 19:02:02 +0000422 } else if (const SCEVAddRecExpr *AF =
423 dyn_cast<SCEVAddRecExpr>(AccessFunction)) {
424 if (Context.AffineAccesses[BasePointer].size() == 0)
425 Context.AffineAccesses[BasePointer] = AFs();
426 Context.AffineAccesses[BasePointer].push_back(AF);
Sebastian Pop18016682014-04-08 21:20:44 +0000427 }
Tobias Grosser75805372011-04-29 06:27:02 +0000428
429 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
430 // created by IndependentBlocks Pass.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000431 if (isa<IntToPtrInst>(BaseValue))
432 return invalid<ReportIntToPtr>(Context, /*Assert=*/true, BaseValue);
Tobias Grosser75805372011-04-29 06:27:02 +0000433
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000434 if (IgnoreAliasing)
435 return true;
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000436
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000437 // Check if the base pointer of the memory access does alias with
438 // any other pointer. This cannot be handled at the moment.
Tobias Grosser298a7642013-07-14 18:09:43 +0000439 AliasSet &AS =
440 Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
441 Inst.getMetadata(LLVMContext::MD_tbaa));
Tobias Grosser428b3e42013-02-04 15:46:25 +0000442
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000443 // INVALID triggers an assertion in verifying mode, if it detects that a
444 // SCoP was detected by SCoP detection and that this SCoP was invalidated by
445 // a pass that stated it would preserve the SCoPs. We disable this check as
446 // the independent blocks pass may create memory references which seem to
447 // alias, if -basicaa is not available. They actually do not, but as we can
448 // not proof this without -basicaa we would fail. We disable this check to
449 // not cause irrelevant verification failures.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000450 if (!AS.isMustAlias())
451 return invalid<ReportAlias>(Context, /*Assert=*/true, &AS);
Tobias Grosser75805372011-04-29 06:27:02 +0000452
453 return true;
454}
455
Tobias Grosser75805372011-04-29 06:27:02 +0000456bool ScopDetection::isValidInstruction(Instruction &Inst,
457 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000458 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000459 if (!canSynthesize(PN, LI, SE, &Context.CurRegion)) {
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000460 if (SCEVCodegen)
461 return invalid<ReportPhiNodeRefInRegion>(Context, /*Assert=*/true,
462 &Inst);
463 else
464 return invalid<ReportNonCanonicalPhiNode>(Context, /*Assert=*/true,
465 &Inst);
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000466 }
Tobias Grosser75805372011-04-29 06:27:02 +0000467
Tobias Grosser75805372011-04-29 06:27:02 +0000468 // We only check the call instruction but not invoke instruction.
469 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
470 if (isValidCallInst(*CI))
471 return true;
472
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000473 return invalid<ReportFuncCall>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000474 }
475
476 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000477 if (!isa<AllocaInst>(Inst))
478 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000479
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000480 return invalid<ReportAlloca>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000481 }
482
483 // Check the access function.
484 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
485 return isValidMemoryAccess(Inst, Context);
486
487 // We do not know this instruction, therefore we assume it is invalid.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000488 return invalid<ReportUnknownInst>(Context, /*Assert=*/true, &Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000489}
490
Tobias Grosser75805372011-04-29 06:27:02 +0000491bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
Tobias Grosser826b2af2013-03-21 16:14:50 +0000492 if (!SCEVCodegen) {
493 // If code generation is not in scev based mode, we need to ensure that
494 // each loop has a canonical induction variable.
495 PHINode *IndVar = L->getCanonicalInductionVariable();
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000496 if (!IndVar)
497 return invalid<ReportLoopHeader>(Context, /*Assert=*/true, L);
Tobias Grosser826b2af2013-03-21 16:14:50 +0000498 }
Tobias Grosser75805372011-04-29 06:27:02 +0000499
500 // Is the loop count affine?
501 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000502 if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE))
503 return invalid<ReportLoopBound>(Context, /*Assert=*/true, L, LoopCount);
Tobias Grosser75805372011-04-29 06:27:02 +0000504
505 return true;
506}
507
508Region *ScopDetection::expandRegion(Region &R) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000509 // Initial no valid region was found (greater than R)
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000510 Region *LastValidRegion = nullptr;
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000511 Region *ExpandedRegion = R.getExpandedRegion();
Tobias Grosser75805372011-04-29 06:27:02 +0000512
513 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
514
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000515 while (ExpandedRegion) {
516 DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */);
517 DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000518
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000519 // Check the exit first (cheap)
Tobias Grosser75805372011-04-29 06:27:02 +0000520 if (isValidExit(Context)) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000521 // If the exit is valid check all blocks
522 // - if true, a valid region was found => store it + keep expanding
523 // - if false, .tbd. => stop (should this really end the loop?)
524 if (!allBlocksValid(Context))
525 break;
Tobias Grosser75805372011-04-29 06:27:02 +0000526
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000527 // Delete unnecessary regions (allocated by getExpandedRegion)
528 if (LastValidRegion)
529 delete LastValidRegion;
530
Tobias Grosserd7e58642013-04-10 06:55:45 +0000531 // Store this region, because it is the greatest valid (encountered so
532 // far).
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000533 LastValidRegion = ExpandedRegion;
534
535 // Create and test the next greater region (if any)
536 ExpandedRegion = ExpandedRegion->getExpandedRegion();
537
538 } else {
539 // Create and test the next greater region (if any)
540 Region *TmpRegion = ExpandedRegion->getExpandedRegion();
541
542 // Delete unnecessary regions (allocated by getExpandedRegion)
543 delete ExpandedRegion;
544
545 ExpandedRegion = TmpRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000546 }
Tobias Grosser75805372011-04-29 06:27:02 +0000547 }
548
Tobias Grosser378a9f22013-11-16 19:34:11 +0000549 DEBUG({
550 if (LastValidRegion)
551 dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
552 else
553 dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
554 });
Tobias Grosser75805372011-04-29 06:27:02 +0000555
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000556 return LastValidRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000557}
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000558static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
Tobias Grosser26108892014-04-02 20:18:19 +0000559 for (const BasicBlock *BB : R.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000560 if (R.contains(LI->getLoopFor(BB)))
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000561 return false;
562
563 return true;
564}
Tobias Grosser75805372011-04-29 06:27:02 +0000565
Tobias Grosser28a70c52014-01-29 19:05:30 +0000566// Remove all direct and indirect children of region R from the region set Regs,
567// but do not recurse further if the first child has been found.
568//
569// Return the number of regions erased from Regs.
570static unsigned eraseAllChildren(std::set<const Region *> &Regs,
David Blaikieb035f6d2014-04-15 18:45:27 +0000571 const Region &R) {
Tobias Grosser28a70c52014-01-29 19:05:30 +0000572 unsigned Count = 0;
David Blaikieb035f6d2014-04-15 18:45:27 +0000573 for (auto &SubRegion : R) {
574 if (Regs.find(SubRegion.get()) != Regs.end()) {
Tobias Grosser28a70c52014-01-29 19:05:30 +0000575 ++Count;
David Blaikieb035f6d2014-04-15 18:45:27 +0000576 Regs.erase(SubRegion.get());
Tobias Grosser28a70c52014-01-29 19:05:30 +0000577 } else {
David Blaikieb035f6d2014-04-15 18:45:27 +0000578 Count += eraseAllChildren(Regs, *SubRegion);
Tobias Grosser28a70c52014-01-29 19:05:30 +0000579 }
580 }
581 return Count;
582}
583
Tobias Grosser75805372011-04-29 06:27:02 +0000584void ScopDetection::findScops(Region &R) {
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000585 if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI))
586 return;
587
Tobias Grosser4eb73812011-11-10 12:45:15 +0000588 LastFailure = "";
589
Tobias Grosser9b1100b2014-02-18 18:49:46 +0000590 if (isValidRegion(R)) {
Tobias Grosser75805372011-04-29 06:27:02 +0000591 ++ValidRegion;
592 ValidRegions.insert(&R);
593 return;
594 }
595
Tobias Grosser4f129a62011-10-08 00:30:55 +0000596 InvalidRegions[&R] = LastFailure;
597
David Blaikieb035f6d2014-04-15 18:45:27 +0000598 for (auto &SubRegion : R)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000599 findScops(*SubRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000600
601 // Try to expand regions.
602 //
603 // As the region tree normally only contains canonical regions, non canonical
604 // regions that form a Scop are not found. Therefore, those non canonical
605 // regions are checked by expanding the canonical ones.
606
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000607 std::vector<Region *> ToExpand;
Tobias Grosser75805372011-04-29 06:27:02 +0000608
David Blaikieb035f6d2014-04-15 18:45:27 +0000609 for (auto &SubRegion : R)
610 ToExpand.push_back(SubRegion.get());
Tobias Grosser75805372011-04-29 06:27:02 +0000611
Tobias Grosser26108892014-04-02 20:18:19 +0000612 for (Region *CurrentRegion : ToExpand) {
Tobias Grosser75805372011-04-29 06:27:02 +0000613 // Skip invalid regions. Regions may become invalid, if they are element of
614 // an already expanded region.
615 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
616 continue;
617
618 Region *ExpandedR = expandRegion(*CurrentRegion);
619
620 if (!ExpandedR)
621 continue;
622
623 R.addSubRegion(ExpandedR, true);
624 ValidRegions.insert(ExpandedR);
625 ValidRegions.erase(CurrentRegion);
626
Tobias Grosser28a70c52014-01-29 19:05:30 +0000627 // Erase all (direct and indirect) children of ExpandedR from the valid
628 // regions and update the number of valid regions.
David Blaikieb035f6d2014-04-15 18:45:27 +0000629 ValidRegion -= eraseAllChildren(ValidRegions, *ExpandedR);
Tobias Grosser75805372011-04-29 06:27:02 +0000630 }
631}
632
633bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
634 Region &R = Context.CurRegion;
635
Tobias Grosser26108892014-04-02 20:18:19 +0000636 for (const BasicBlock *BB : R.blocks()) {
Tobias Grosser1d191902014-03-03 13:13:55 +0000637 Loop *L = LI->getLoopFor(BB);
638 if (L && L->getHeader() == BB && !isValidLoop(L, Context))
Sebastian Popb88ea5e2013-06-11 22:20:32 +0000639 return false;
640 }
641
Tobias Grosser26108892014-04-02 20:18:19 +0000642 for (BasicBlock *BB : R.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000643 if (!isValidCFG(*BB, Context))
Sebastian Pop9e3d2dd2013-06-11 22:20:27 +0000644 return false;
645
Tobias Grosser26108892014-04-02 20:18:19 +0000646 for (BasicBlock *BB : R.blocks())
Tobias Grosser1d191902014-03-03 13:13:55 +0000647 for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; ++I)
Sebastian Pop8ca899c2013-06-14 20:20:43 +0000648 if (!isValidInstruction(*I, Context))
649 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000650
Sebastian Pope8863b82014-05-12 19:02:02 +0000651 if (!hasAffineMemoryAccesses(Context))
Sebastian Pop46e1ecd2014-05-09 22:45:15 +0000652 return false;
653
Tobias Grosser75805372011-04-29 06:27:02 +0000654 return true;
655}
656
657bool ScopDetection::isValidExit(DetectionContext &Context) const {
658 Region &R = Context.CurRegion;
659
660 // PHI nodes are not allowed in the exit basic block.
661 if (BasicBlock *Exit = R.getExit()) {
662 BasicBlock::iterator I = Exit->begin();
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000663 if (I != Exit->end() && isa<PHINode>(*I))
664 return invalid<ReportPHIinExit>(Context, /*Assert=*/true);
Tobias Grosser75805372011-04-29 06:27:02 +0000665 }
666
667 return true;
668}
669
Tobias Grosser9b1100b2014-02-18 18:49:46 +0000670bool ScopDetection::isValidRegion(Region &R) const {
671 DetectionContext Context(R, *AA, false /*verifying*/);
672 return isValidRegion(Context);
673}
674
Tobias Grosser75805372011-04-29 06:27:02 +0000675bool ScopDetection::isValidRegion(DetectionContext &Context) const {
676 Region &R = Context.CurRegion;
677
678 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
679
Tobias Grosseraeabcf22013-04-02 06:41:48 +0000680 if (R.isTopLevelRegion()) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000681 DEBUG(dbgs() << "Top level region is invalid"; dbgs() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000682 return false;
683 }
684
Tobias Grosser4449e522014-01-27 14:24:53 +0000685 if (!R.getEntry()->getName().count(OnlyRegion)) {
686 DEBUG({
687 dbgs() << "Region entry does not match -polly-region-only";
688 dbgs() << "\n";
689 });
690 return false;
691 }
692
Tobias Grossere602a072013-05-07 07:30:56 +0000693 if (!R.getEnteringBlock()) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000694 BasicBlock *entry = R.getEntry();
695 Loop *L = LI->getLoopFor(entry);
696
697 if (L) {
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000698 if (!L->isLoopSimplifyForm())
699 return invalid<ReportSimpleLoop>(Context, /*Assert=*/true);
Sebastian Pop9d632342013-06-11 22:20:40 +0000700
701 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
702 ++PI) {
703 // Region entering edges come from the same loop but outside the region
704 // are not allowed.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000705 if (L->contains(*PI) && !R.contains(*PI))
706 return invalid<ReportIndEdge>(Context, /*Assert=*/true);
Sebastian Pop9d632342013-06-11 22:20:40 +0000707 }
708 }
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000709 }
710
Tobias Grosserd654c252012-04-10 18:12:19 +0000711 // SCoP cannot contain the entry block of the function, because we need
Tobias Grosser75805372011-04-29 06:27:02 +0000712 // to insert alloca instruction there when translate scalar to array.
Andreas Simbuerger01a37a02014-04-02 11:54:01 +0000713 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
714 return invalid<ReportEntry>(Context, /*Assert=*/true);
Tobias Grosser75805372011-04-29 06:27:02 +0000715
Hongbin Zheng94868e62012-04-07 12:29:17 +0000716 if (!isValidExit(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000717 return false;
718
Hongbin Zheng94868e62012-04-07 12:29:17 +0000719 if (!allBlocksValid(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000720 return false;
721
722 DEBUG(dbgs() << "OK\n");
723 return true;
724}
725
726bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000727 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000728}
729
Tobias Grosser531891e2012-11-01 16:45:20 +0000730void ScopDetection::getDebugLocation(const Region *R, unsigned &LineBegin,
731 unsigned &LineEnd, std::string &FileName) {
732 LineBegin = -1;
733 LineEnd = 0;
734
Tobias Grosser26108892014-04-02 20:18:19 +0000735 for (const BasicBlock *BB : R->blocks())
736 for (const Instruction &Inst : *BB) {
737 DebugLoc DL = Inst.getDebugLoc();
Tobias Grosser531891e2012-11-01 16:45:20 +0000738 if (DL.isUnknown())
739 continue;
740
Tobias Grosser26108892014-04-02 20:18:19 +0000741 DIScope Scope(DL.getScope(Inst.getContext()));
Tobias Grosser531891e2012-11-01 16:45:20 +0000742
743 if (FileName.empty())
744 FileName = Scope.getFilename();
745
746 unsigned NewLine = DL.getLine();
747
748 LineBegin = std::min(LineBegin, NewLine);
749 LineEnd = std::max(LineEnd, NewLine);
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000750 }
Tobias Grosser531891e2012-11-01 16:45:20 +0000751}
752
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000753void ScopDetection::printLocations(llvm::Function &F) {
Tobias Grosser26108892014-04-02 20:18:19 +0000754 for (const Region *R : *this) {
Tobias Grosser531891e2012-11-01 16:45:20 +0000755 unsigned LineEntry, LineExit;
756 std::string FileName;
757
Tobias Grosser00dc3092014-03-02 12:02:46 +0000758 getDebugLocation(R, LineEntry, LineExit, FileName);
Tobias Grosser8519f892013-12-18 10:49:53 +0000759 DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit);
760 F.getContext().diagnose(Diagnostic);
Tobias Grosser531891e2012-11-01 16:45:20 +0000761 }
762}
763
Tobias Grosser75805372011-04-29 06:27:02 +0000764bool ScopDetection::runOnFunction(llvm::Function &F) {
Sebastian Pop8fe6d112013-05-30 17:47:32 +0000765 LI = &getAnalysis<LoopInfo>();
Tobias Grosser9a26f292014-01-02 22:28:53 +0000766 RI = &getAnalysis<RegionInfo>();
Sebastian Pop8fe6d112013-05-30 17:47:32 +0000767 if (!DetectScopsWithoutLoops && LI->empty())
768 return false;
769
Tobias Grosser75805372011-04-29 06:27:02 +0000770 AA = &getAnalysis<AliasAnalysis>();
771 SE = &getAnalysis<ScalarEvolution>();
Tobias Grosser75805372011-04-29 06:27:02 +0000772 Region *TopRegion = RI->getTopLevelRegion();
773
Tobias Grosser2ff87232011-10-23 11:17:06 +0000774 releaseMemory();
775
Tobias Grossera3ab27e2014-05-07 11:23:32 +0000776 if (OnlyFunction != "" && !F.getName().count(OnlyFunction))
Tobias Grosser2ff87232011-10-23 11:17:06 +0000777 return false;
778
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000779 if (!isValidFunction(F))
Tobias Grosser75805372011-04-29 06:27:02 +0000780 return false;
781
782 findScops(*TopRegion);
Tobias Grosser531891e2012-11-01 16:45:20 +0000783
784 if (ReportLevel >= 1)
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000785 printLocations(F);
Tobias Grosser531891e2012-11-01 16:45:20 +0000786
Tobias Grosser75805372011-04-29 06:27:02 +0000787 return false;
788}
789
Tobias Grosser75805372011-04-29 06:27:02 +0000790void polly::ScopDetection::verifyRegion(const Region &R) const {
791 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000792 DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/);
Tobias Grosser75805372011-04-29 06:27:02 +0000793 isValidRegion(Context);
794}
795
796void polly::ScopDetection::verifyAnalysis() const {
Tobias Grossera1689932014-02-18 18:49:49 +0000797 if (!VerifyScops)
798 return;
799
Tobias Grosser26108892014-04-02 20:18:19 +0000800 for (const Region *R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000801 verifyRegion(*R);
Tobias Grosser75805372011-04-29 06:27:02 +0000802}
803
804void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser42aff302014-01-13 22:29:56 +0000805 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser75805372011-04-29 06:27:02 +0000806 AU.addRequired<PostDominatorTree>();
807 AU.addRequired<LoopInfo>();
808 AU.addRequired<ScalarEvolution>();
809 // We also need AA and RegionInfo when we are verifying analysis.
810 AU.addRequiredTransitive<AliasAnalysis>();
811 AU.addRequiredTransitive<RegionInfo>();
812 AU.setPreservesAll();
813}
814
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000815void ScopDetection::print(raw_ostream &OS, const Module *) const {
Tobias Grosser26108892014-04-02 20:18:19 +0000816 for (const Region *R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000817 OS << "Valid Region for Scop: " << R->getNameStr() << '\n';
Tobias Grosser75805372011-04-29 06:27:02 +0000818
819 OS << "\n";
820}
821
822void ScopDetection::releaseMemory() {
823 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000824 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000825 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000826}
827
828char ScopDetection::ID = 0;
829
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000830Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
831
Tobias Grosser73600b82011-10-08 00:30:40 +0000832INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
833 "Polly - Detect static control parts (SCoPs)", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000834 false);
835INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Tobias Grosser42aff302014-01-13 22:29:56 +0000836INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000837INITIALIZE_PASS_DEPENDENCY(LoopInfo);
838INITIALIZE_PASS_DEPENDENCY(PostDominatorTree);
839INITIALIZE_PASS_DEPENDENCY(RegionInfo);
840INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
Tobias Grosser73600b82011-10-08 00:30:40 +0000841INITIALIZE_PASS_END(ScopDetection, "polly-detect",
842 "Polly - Detect static control parts (SCoPs)", false, false)