blob: a5cfc2c1aa6b9b4776e537ee39b28063c4478a92 [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"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000050#include "polly/ScopDetection.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +000051#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000052#include "polly/Support/ScopHelper.h"
Tobias Grosser75805372011-04-29 06:27:02 +000053#include "llvm/ADT/Statistic.h"
54#include "llvm/Analysis/AliasAnalysis.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000055#include "llvm/Analysis/LoopInfo.h"
Tobias Grosser75805372011-04-29 06:27:02 +000056#include "llvm/Analysis/RegionIterator.h"
Tobias Grosser6e9f25a2011-11-09 22:35:00 +000057#include "llvm/Analysis/ScalarEvolution.h"
Tobias Grosserb8710b52011-11-10 12:44:50 +000058#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Tobias Grosser83628182013-05-07 08:11:54 +000059#include "llvm/DebugInfo.h"
60#include "llvm/IR/LLVMContext.h"
Tobias Grosser8519f892013-12-18 10:49:53 +000061#include "llvm/IR/DiagnosticInfo.h"
62#include "llvm/IR/DiagnosticPrinter.h"
Tobias Grosser75805372011-04-29 06:27:02 +000063
64#define DEBUG_TYPE "polly-detect"
65#include "llvm/Support/Debug.h"
66
Tobias Grosser60b54f12011-11-08 15:41:28 +000067#include <set>
68
Tobias Grosser75805372011-04-29 06:27:02 +000069using namespace llvm;
70using namespace polly;
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"),
75 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
76
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +000077static cl::opt<bool>
78DetectRegionsWithoutLoops("polly-detect-scops-in-regions-without-loops",
79 cl::desc("Detect scops in regions without loops"),
80 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
81
Tobias Grosser2ff87232011-10-23 11:17:06 +000082static cl::opt<std::string>
Tobias Grosser637bd632013-05-07 07:31:10 +000083OnlyFunction("polly-only-func", cl::desc("Only run on a single function"),
84 cl::value_desc("function-name"), cl::ValueRequired, cl::init(""),
85 cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000086
Tobias Grosser4449e522014-01-27 14:24:53 +000087static cl::opt<std::string>
88OnlyRegion("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));
93
Tobias Grosser60cd9322011-11-10 12:47:26 +000094static cl::opt<bool>
95IgnoreAliasing("polly-ignore-aliasing",
96 cl::desc("Ignore possible aliasing of the array bases"),
Tobias Grosser637bd632013-05-07 07:31:10 +000097 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
Tobias Grosser2ff87232011-10-23 11:17:06 +000098
Tobias Grosser637bd632013-05-07 07:31:10 +000099static cl::opt<bool>
100ReportLevel("polly-report",
101 cl::desc("Print information about the activities of Polly"),
102 cl::init(false), cl::cat(PollyCategory));
Tobias Grosser531891e2012-11-01 16:45:20 +0000103
104static cl::opt<bool>
Tobias Grossera1879642011-12-20 10:43:14 +0000105AllowNonAffine("polly-allow-nonaffine",
106 cl::desc("Allow non affine access functions in arrays"),
Tobias Grosser637bd632013-05-07 07:31:10 +0000107 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
Tobias Grossera1879642011-12-20 10:43:14 +0000108
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000109static cl::opt<bool, true>
110TrackFailures("polly-detect-track-failures",
111 cl::desc("Track failure strings in detecting scop regions"),
112 cl::location(PollyTrackFailures), cl::Hidden, cl::init(false),
113 cl::cat(PollyCategory));
114
Tobias Grossera1689932014-02-18 18:49:49 +0000115static cl::opt<bool>
116VerifyScops("polly-detect-verify",
117 cl::desc("Verify the detected SCoPs after each transformation"),
118 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
119
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000120bool polly::PollyTrackFailures = false;
121
Tobias Grosser75805372011-04-29 06:27:02 +0000122//===----------------------------------------------------------------------===//
123// Statistics.
124
125STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
126
Tobias Grosser74394f02013-01-14 22:40:23 +0000127#define BADSCOP_STAT(NAME, DESC) \
128 STATISTIC(Bad##NAME##ForScop, "Number of bad regions for Scop: " DESC)
Tobias Grosser75805372011-04-29 06:27:02 +0000129
Tobias Grosser74394f02013-01-14 22:40:23 +0000130#define INVALID(NAME, MESSAGE) \
131 do { \
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000132 if (PollyTrackFailures) { \
133 std::string Buf; \
134 raw_string_ostream fmt(Buf); \
135 fmt << MESSAGE; \
136 fmt.flush(); \
137 LastFailure = Buf; \
138 } \
Tobias Grosser74394f02013-01-14 22:40:23 +0000139 DEBUG(dbgs() << MESSAGE); \
140 DEBUG(dbgs() << "\n"); \
Tobias Grosser1c84d802013-11-16 01:07:06 +0000141 assert(!Context.Verifying && #NAME); \
Tobias Grosser74394f02013-01-14 22:40:23 +0000142 if (!Context.Verifying) \
143 ++Bad##NAME##ForScop; \
Tobias Grosser84b81de2013-03-19 21:44:07 +0000144 } while (0)
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000145
Tobias Grosser74394f02013-01-14 22:40:23 +0000146#define INVALID_NOVERIFY(NAME, MESSAGE) \
147 do { \
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000148 if (PollyTrackFailures) { \
149 std::string Buf; \
150 raw_string_ostream fmt(Buf); \
151 fmt << MESSAGE; \
152 fmt.flush(); \
153 LastFailure = Buf; \
154 } \
Tobias Grosser74394f02013-01-14 22:40:23 +0000155 DEBUG(dbgs() << MESSAGE); \
156 DEBUG(dbgs() << "\n"); \
157 /* DISABLED: assert(!Context.Verifying && #NAME); */ \
158 if (!Context.Verifying) \
159 ++Bad##NAME##ForScop; \
Tobias Grosser84b81de2013-03-19 21:44:07 +0000160 } while (0)
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000161
Tobias Grosser74394f02013-01-14 22:40:23 +0000162BADSCOP_STAT(CFG, "CFG too complex");
163BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
Sebastian Pop9d632342013-06-11 22:20:40 +0000164BADSCOP_STAT(IndEdge, "Found invalid region entering edges");
Tobias Grosser74394f02013-01-14 22:40:23 +0000165BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
166BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
167BADSCOP_STAT(AffFunc, "Expression not affine");
Tobias Grosser74394f02013-01-14 22:40:23 +0000168BADSCOP_STAT(Alias, "Found base address alias");
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000169BADSCOP_STAT(SimpleLoop, "Loop not in -loop-simplify form");
Tobias Grosser74394f02013-01-14 22:40:23 +0000170BADSCOP_STAT(Other, "Others");
Tobias Grosser75805372011-04-29 06:27:02 +0000171
Tobias Grosser8519f892013-12-18 10:49:53 +0000172class DiagnosticScopFound : public DiagnosticInfo {
173private:
174 static int PluginDiagnosticKind;
175
176 Function &F;
177 std::string FileName;
178 unsigned EntryLine, ExitLine;
179
180public:
Tobias Grosser1b12f462013-12-18 11:14:36 +0000181 DiagnosticScopFound(Function &F, std::string FileName, unsigned EntryLine,
182 unsigned ExitLine)
Tobias Grosser8519f892013-12-18 10:49:53 +0000183 : DiagnosticInfo(PluginDiagnosticKind, DS_Note), F(F), FileName(FileName),
Tobias Grosser1b12f462013-12-18 11:14:36 +0000184 EntryLine(EntryLine), ExitLine(ExitLine) {}
Tobias Grosser8519f892013-12-18 10:49:53 +0000185
186 virtual void print(DiagnosticPrinter &DP) const;
187
188 static bool classof(const DiagnosticInfo *DI) {
189 return DI->getKind() == PluginDiagnosticKind;
190 }
191};
192
193int DiagnosticScopFound::PluginDiagnosticKind = 10;
194
Tobias Grosser8519f892013-12-18 10:49:53 +0000195void DiagnosticScopFound::print(DiagnosticPrinter &DP) const {
196
Tobias Grosser1b12f462013-12-18 11:14:36 +0000197 DP << "Polly detected an optimizable loop region (scop) in function '" << F
198 << "'\n";
Tobias Grosser8519f892013-12-18 10:49:53 +0000199
200 if (FileName.empty()) {
201 DP << "Scop location is unknown. Compile with debug info "
202 "(-g) to get more precise information. ";
Tobias Grosser1b12f462013-12-18 11:14:36 +0000203 return;
Tobias Grosser8519f892013-12-18 10:49:53 +0000204 }
205
206 DP << FileName << ":" << EntryLine << ": Start of scop\n";
207 DP << FileName << ":" << ExitLine << ": End of scop";
208}
209
Tobias Grosser75805372011-04-29 06:27:02 +0000210//===----------------------------------------------------------------------===//
211// ScopDetection.
Tobias Grossera1689932014-02-18 18:49:49 +0000212bool ScopDetection::isMaxRegionInScop(const Region &R, bool Verify) const {
213 if (!ValidRegions.count(&R))
214 return false;
215
216 if (Verify)
217 return isValidRegion(const_cast<Region &>(R));
218
219 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000220}
221
Tobias Grosser4f129a62011-10-08 00:30:55 +0000222std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
223 if (!InvalidRegions.count(R))
224 return "";
225
226 return InvalidRegions.find(R)->second;
227}
228
Tobias Grossere602a072013-05-07 07:30:56 +0000229bool ScopDetection::isValidCFG(BasicBlock &BB,
230 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000231 Region &RefRegion = Context.CurRegion;
232 TerminatorInst *TI = BB.getTerminator();
233
234 // Return instructions are only valid if the region is the top level region.
235 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
236 return true;
237
238 BranchInst *Br = dyn_cast<BranchInst>(TI);
239
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000240 if (!Br) {
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000241 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000242 return false;
243 }
Tobias Grosser75805372011-04-29 06:27:02 +0000244
Tobias Grosser74394f02013-01-14 22:40:23 +0000245 if (Br->isUnconditional())
246 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000247
248 Value *Condition = Br->getCondition();
249
250 // UndefValue is not allowed as condition.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000251 if (isa<UndefValue>(Condition)) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000252 INVALID(AffFunc, "Condition based on 'undef' value in BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000253 return false;
254 }
Tobias Grosser75805372011-04-29 06:27:02 +0000255
256 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000257 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000258 INVALID(AffFunc, "Condition in BB '" + BB.getName() +
Tobias Grosserd7e58642013-04-10 06:55:45 +0000259 "' neither constant nor an icmp instruction");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000260 return false;
261 }
Tobias Grosser75805372011-04-29 06:27:02 +0000262
263 // Allow perfectly nested conditions.
264 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
265
266 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
267 // Unsigned comparisons are not allowed. They trigger overflow problems
268 // in the code generation.
269 //
270 // TODO: This is not sufficient and just hides bugs. However it does pretty
271 // well.
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000272 if (ICmp->isUnsigned())
Tobias Grosser75805372011-04-29 06:27:02 +0000273 return false;
274
275 // Are both operands of the ICmp affine?
Tobias Grosser74394f02013-01-14 22:40:23 +0000276 if (isa<UndefValue>(ICmp->getOperand(0)) ||
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000277 isa<UndefValue>(ICmp->getOperand(1))) {
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000278 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000279 return false;
280 }
Tobias Grosser75805372011-04-29 06:27:02 +0000281
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000282 Loop *L = LI->getLoopFor(ICmp->getParent());
283 const SCEV *LHS = SE->getSCEVAtScope(ICmp->getOperand(0), L);
284 const SCEV *RHS = SE->getSCEVAtScope(ICmp->getOperand(1), L);
Tobias Grosser75805372011-04-29 06:27:02 +0000285
Tobias Grossera66fa6c2011-11-10 12:45:11 +0000286 if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000287 !isAffineExpr(&Context.CurRegion, RHS, *SE)) {
Tobias Grosser983e7852013-07-28 09:05:20 +0000288 INVALID(AffFunc, "Non affine branch in BB '" << BB.getName()
289 << "' with LHS: " << *LHS
290 << " and RHS: " << *RHS);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000291 return false;
292 }
Tobias Grosser75805372011-04-29 06:27:02 +0000293 }
294
295 // Allow loop exit conditions.
296 Loop *L = LI->getLoopFor(&BB);
297 if (L && L->getExitingBlock() == &BB)
298 return true;
299
300 // Allow perfectly nested conditions.
301 Region *R = RI->getRegionFor(&BB);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000302 if (R->getEntry() != &BB) {
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000303 INVALID(CFG, "Not well structured condition at BB: " + BB.getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000304 return false;
305 }
Tobias Grosser75805372011-04-29 06:27:02 +0000306
307 return true;
308}
309
310bool ScopDetection::isValidCallInst(CallInst &CI) {
311 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
312 return false;
313
314 if (CI.doesNotAccessMemory())
315 return true;
316
317 Function *CalledFunction = CI.getCalledFunction();
318
319 // Indirect calls are not supported.
320 if (CalledFunction == 0)
321 return false;
322
323 // TODO: Intrinsics.
324 return false;
325}
326
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000327std::string ScopDetection::formatInvalidAlias(AliasSet &AS) const {
328 std::string Message;
329 raw_string_ostream OS(Message);
330
331 OS << "Possible aliasing: ";
332
333 std::vector<Value *> Pointers;
334
Tobias Grosserb5846f92014-03-02 17:05:27 +0000335 for (const auto &I : AS)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000336 Pointers.push_back(I.getValue());
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000337
338 std::sort(Pointers.begin(), Pointers.end());
339
340 for (std::vector<Value *>::iterator PI = Pointers.begin(),
341 PE = Pointers.end();
342 ;) {
343 Value *V = *PI;
344
345 if (V->getName().size() == 0)
346 OS << "\"" << *V << "\"";
347 else
348 OS << "\"" << V->getName() << "\"";
349
350 ++PI;
351
352 if (PI != PE)
353 OS << ", ";
354 else
355 break;
356 }
357
358 return OS.str();
359}
360
Tobias Grosser458fb782014-01-28 12:58:58 +0000361bool ScopDetection::isInvariant(const Value &Val, const Region &Reg) const {
362 // A reference to function argument or constant value is invariant.
363 if (isa<Argument>(Val) || isa<Constant>(Val))
364 return true;
365
366 const Instruction *I = dyn_cast<Instruction>(&Val);
367 if (!I)
368 return false;
369
370 if (!Reg.contains(I))
371 return true;
372
373 if (I->mayHaveSideEffects())
374 return false;
375
376 // When Val is a Phi node, it is likely not invariant. We do not check whether
377 // Phi nodes are actually invariant, we assume that Phi nodes are usually not
378 // invariant. Recursively checking the operators of Phi nodes would lead to
379 // infinite recursion.
380 if (isa<PHINode>(*I))
381 return false;
382
383 // Check that all operands of the instruction are
384 // themselves invariant.
385 const Instruction::const_op_iterator OE = I->op_end();
386 for (Instruction::const_op_iterator OI = I->op_begin(); OI != OE; ++OI) {
387 if (!isInvariant(**OI, Reg))
388 return false;
389 }
390
391 // When the instruction is a load instruction, check that no write to memory
392 // in the region aliases with the load.
393 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
394 AliasAnalysis::Location Loc = AA->getLocation(LI);
395 const Region::const_block_iterator BE = Reg.block_end();
396 // Check if any basic block in the region can modify the location pointed to
397 // by 'Loc'. If so, 'Val' is (likely) not invariant in the region.
398 for (Region::const_block_iterator BI = Reg.block_begin(); BI != BE; ++BI) {
399 const BasicBlock &BB = **BI;
400 if (AA->canBasicBlockModify(BB, Loc))
401 return false;
402 }
403 }
404
405 return true;
406}
407
Tobias Grosser75805372011-04-29 06:27:02 +0000408bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
409 DetectionContext &Context) const {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000410 Value *Ptr = getPointerOperand(Inst);
Sebastian Pop9f57c5b2013-04-10 04:05:18 +0000411 Loop *L = LI->getLoopFor(Inst.getParent());
412 const SCEV *AccessFunction = SE->getSCEVAtScope(Ptr, L);
Tobias Grosserb8710b52011-11-10 12:44:50 +0000413 const SCEVUnknown *BasePointer;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000414 Value *BaseValue;
Tobias Grosser75805372011-04-29 06:27:02 +0000415
Tobias Grosserb8710b52011-11-10 12:44:50 +0000416 BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
417
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000418 if (!BasePointer) {
Tobias Grosserb8710b52011-11-10 12:44:50 +0000419 INVALID(AffFunc, "No base pointer");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000420 return false;
421 }
Tobias Grosserb8710b52011-11-10 12:44:50 +0000422
423 BaseValue = BasePointer->getValue();
424
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000425 if (isa<UndefValue>(BaseValue)) {
Tobias Grosserb8710b52011-11-10 12:44:50 +0000426 INVALID(AffFunc, "Undefined base pointer");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000427 return false;
428 }
Tobias Grosserb8710b52011-11-10 12:44:50 +0000429
Tobias Grosser458fb782014-01-28 12:58:58 +0000430 // Check that the base address of the access is invariant in the current
431 // region.
432 if (!isInvariant(*BaseValue, Context.CurRegion)) {
Tobias Grosserab2227a2014-01-28 13:43:24 +0000433 // Verification of this property is difficult as the independent blocks
434 // pass may introduce aliasing that we did not have when running the
435 // scop detection.
436 INVALID_NOVERIFY(
437 AffFunc, "Base address not invariant in current region:" << *BaseValue);
Tobias Grosser458fb782014-01-28 12:58:58 +0000438 return false;
439 }
440
Tobias Grosserb8710b52011-11-10 12:44:50 +0000441 AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
442
Tobias Grosser58032cb2013-06-23 01:29:29 +0000443 if (!AllowNonAffine &&
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000444 !isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue)) {
Tobias Grossereeb776a2012-09-08 14:00:37 +0000445 INVALID(AffFunc, "Non affine access function: " << *AccessFunction);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000446 return false;
447 }
Tobias Grosser75805372011-04-29 06:27:02 +0000448
449 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
450 // created by IndependentBlocks Pass.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000451 if (isa<IntToPtrInst>(BaseValue)) {
Tobias Grossere5e171e2011-11-10 12:45:03 +0000452 INVALID(Other, "Find bad intToptr prt: " << *BaseValue);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000453 return false;
454 }
Tobias Grosser75805372011-04-29 06:27:02 +0000455
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000456 if (IgnoreAliasing)
457 return true;
Tobias Grosserfff5adc2011-11-10 13:21:43 +0000458
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000459 // Check if the base pointer of the memory access does alias with
460 // any other pointer. This cannot be handled at the moment.
Tobias Grosser298a7642013-07-14 18:09:43 +0000461 AliasSet &AS =
462 Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
463 Inst.getMetadata(LLVMContext::MD_tbaa));
Tobias Grosser428b3e42013-02-04 15:46:25 +0000464
Sebastian Pop8c2d7532013-07-03 22:50:36 +0000465 // INVALID triggers an assertion in verifying mode, if it detects that a
466 // SCoP was detected by SCoP detection and that this SCoP was invalidated by
467 // a pass that stated it would preserve the SCoPs. We disable this check as
468 // the independent blocks pass may create memory references which seem to
469 // alias, if -basicaa is not available. They actually do not, but as we can
470 // not proof this without -basicaa we would fail. We disable this check to
471 // not cause irrelevant verification failures.
472 if (!AS.isMustAlias()) {
Tobias Grosserc7d3fc52013-07-25 03:02:29 +0000473 INVALID_NOVERIFY(Alias, formatInvalidAlias(AS));
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000474 return false;
Tobias Grosser428b3e42013-02-04 15:46:25 +0000475 }
Tobias Grosser75805372011-04-29 06:27:02 +0000476
477 return true;
478}
479
Tobias Grosser75805372011-04-29 06:27:02 +0000480bool ScopDetection::isValidInstruction(Instruction &Inst,
481 DetectionContext &Context) const {
Tobias Grosser75805372011-04-29 06:27:02 +0000482 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000483 if (!canSynthesize(PN, LI, SE, &Context.CurRegion)) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000484 if (SCEVCodegen) {
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000485 INVALID(IndVar,
486 "SCEV of PHI node refers to SSA names in region: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000487 return false;
488
489 } else {
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000490 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000491 return false;
492 }
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000493 }
Tobias Grosser75805372011-04-29 06:27:02 +0000494
Tobias Grosser75805372011-04-29 06:27:02 +0000495 // We only check the call instruction but not invoke instruction.
496 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
497 if (isValidCallInst(*CI))
498 return true;
499
Tobias Grosserb43ba822011-10-08 00:49:30 +0000500 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000501 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000502 }
503
504 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000505 if (!isa<AllocaInst>(Inst))
506 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000507
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000508 INVALID(Other, "Alloca instruction: " << Inst);
509 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000510 }
511
512 // Check the access function.
513 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
514 return isValidMemoryAccess(Inst, Context);
515
516 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000517 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000518 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000519}
520
Tobias Grosser75805372011-04-29 06:27:02 +0000521bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
Tobias Grosser826b2af2013-03-21 16:14:50 +0000522 if (!SCEVCodegen) {
523 // If code generation is not in scev based mode, we need to ensure that
524 // each loop has a canonical induction variable.
525 PHINode *IndVar = L->getCanonicalInductionVariable();
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000526 if (!IndVar) {
Tobias Grosser826b2af2013-03-21 16:14:50 +0000527 INVALID(IndVar,
528 "No canonical IV at loop header: " << L->getHeader()->getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000529 return false;
530 }
Tobias Grosser826b2af2013-03-21 16:14:50 +0000531 }
Tobias Grosser75805372011-04-29 06:27:02 +0000532
533 // Is the loop count affine?
534 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000535 if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE)) {
Tobias Grosser58032cb2013-06-23 01:29:29 +0000536 INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
537 << L->getHeader()->getName());
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000538 return false;
539 }
Tobias Grosser75805372011-04-29 06:27:02 +0000540
541 return true;
542}
543
544Region *ScopDetection::expandRegion(Region &R) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000545 // Initial no valid region was found (greater than R)
546 Region *LastValidRegion = NULL;
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000547 Region *ExpandedRegion = R.getExpandedRegion();
Tobias Grosser75805372011-04-29 06:27:02 +0000548
549 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
550
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000551 while (ExpandedRegion) {
552 DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */);
553 DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000554
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000555 // Check the exit first (cheap)
Tobias Grosser75805372011-04-29 06:27:02 +0000556 if (isValidExit(Context)) {
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000557 // If the exit is valid check all blocks
558 // - if true, a valid region was found => store it + keep expanding
559 // - if false, .tbd. => stop (should this really end the loop?)
560 if (!allBlocksValid(Context))
561 break;
Tobias Grosser75805372011-04-29 06:27:02 +0000562
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000563 // Delete unnecessary regions (allocated by getExpandedRegion)
564 if (LastValidRegion)
565 delete LastValidRegion;
566
Tobias Grosserd7e58642013-04-10 06:55:45 +0000567 // Store this region, because it is the greatest valid (encountered so
568 // far).
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000569 LastValidRegion = ExpandedRegion;
570
571 // Create and test the next greater region (if any)
572 ExpandedRegion = ExpandedRegion->getExpandedRegion();
573
574 } else {
575 // Create and test the next greater region (if any)
576 Region *TmpRegion = ExpandedRegion->getExpandedRegion();
577
578 // Delete unnecessary regions (allocated by getExpandedRegion)
579 delete ExpandedRegion;
580
581 ExpandedRegion = TmpRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000582 }
Tobias Grosser75805372011-04-29 06:27:02 +0000583 }
584
Tobias Grosser378a9f22013-11-16 19:34:11 +0000585 DEBUG({
586 if (LastValidRegion)
587 dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
588 else
589 dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
590 });
Tobias Grosser75805372011-04-29 06:27:02 +0000591
Hongbin Zhenged986ab2012-04-07 15:14:28 +0000592 return LastValidRegion;
Tobias Grosser75805372011-04-29 06:27:02 +0000593}
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000594static bool regionWithoutLoops(Region &R, LoopInfo *LI) {
595 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
596 ++I)
597 if (R.contains(LI->getLoopFor(*I)))
598 return false;
599
600 return true;
601}
Tobias Grosser75805372011-04-29 06:27:02 +0000602
Tobias Grosser28a70c52014-01-29 19:05:30 +0000603// Remove all direct and indirect children of region R from the region set Regs,
604// but do not recurse further if the first child has been found.
605//
606// Return the number of regions erased from Regs.
607static unsigned eraseAllChildren(std::set<const Region *> &Regs,
608 const Region *R) {
609 unsigned Count = 0;
Tobias Grosserb5846f92014-03-02 17:05:27 +0000610 for (const auto &SubRegion : *R) {
Tobias Grosser00dc3092014-03-02 12:02:46 +0000611 if (Regs.find(SubRegion) != Regs.end()) {
Tobias Grosser28a70c52014-01-29 19:05:30 +0000612 ++Count;
Tobias Grosser00dc3092014-03-02 12:02:46 +0000613 Regs.erase(SubRegion);
Tobias Grosser28a70c52014-01-29 19:05:30 +0000614 } else {
Tobias Grosser00dc3092014-03-02 12:02:46 +0000615 Count += eraseAllChildren(Regs, SubRegion);
Tobias Grosser28a70c52014-01-29 19:05:30 +0000616 }
617 }
618 return Count;
619}
620
Tobias Grosser75805372011-04-29 06:27:02 +0000621void ScopDetection::findScops(Region &R) {
Sebastian Pop2c9ec2e2013-06-03 16:35:37 +0000622
623 if (!DetectRegionsWithoutLoops && regionWithoutLoops(R, LI))
624 return;
625
Tobias Grosser4eb73812011-11-10 12:45:15 +0000626 LastFailure = "";
627
Tobias Grosser9b1100b2014-02-18 18:49:46 +0000628 if (isValidRegion(R)) {
Tobias Grosser75805372011-04-29 06:27:02 +0000629 ++ValidRegion;
630 ValidRegions.insert(&R);
631 return;
632 }
633
Tobias Grosser4f129a62011-10-08 00:30:55 +0000634 InvalidRegions[&R] = LastFailure;
635
Tobias Grosserb5846f92014-03-02 17:05:27 +0000636 for (const auto &SubRegion : R)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000637 findScops(*SubRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000638
639 // Try to expand regions.
640 //
641 // As the region tree normally only contains canonical regions, non canonical
642 // regions that form a Scop are not found. Therefore, those non canonical
643 // regions are checked by expanding the canonical ones.
644
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000645 std::vector<Region *> ToExpand;
Tobias Grosser75805372011-04-29 06:27:02 +0000646
Tobias Grosserb5846f92014-03-02 17:05:27 +0000647 for (const auto &SubRegion : R)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000648 ToExpand.push_back(SubRegion);
Tobias Grosser75805372011-04-29 06:27:02 +0000649
Tobias Grosserb5846f92014-03-02 17:05:27 +0000650 for (const auto &CurrentRegion : ToExpand) {
Tobias Grosser75805372011-04-29 06:27:02 +0000651 // Skip invalid regions. Regions may become invalid, if they are element of
652 // an already expanded region.
653 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
654 continue;
655
656 Region *ExpandedR = expandRegion(*CurrentRegion);
657
658 if (!ExpandedR)
659 continue;
660
661 R.addSubRegion(ExpandedR, true);
662 ValidRegions.insert(ExpandedR);
663 ValidRegions.erase(CurrentRegion);
664
Tobias Grosser28a70c52014-01-29 19:05:30 +0000665 // Erase all (direct and indirect) children of ExpandedR from the valid
666 // regions and update the number of valid regions.
667 ValidRegion -= eraseAllChildren(ValidRegions, ExpandedR);
Tobias Grosser75805372011-04-29 06:27:02 +0000668 }
669}
670
671bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
672 Region &R = Context.CurRegion;
673
674 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
Sebastian Popb88ea5e2013-06-11 22:20:32 +0000675 ++I) {
676 Loop *L = LI->getLoopFor(*I);
677 if (L && L->getHeader() == *I && !isValidLoop(L, Context))
678 return false;
679 }
680
681 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
Tobias Grosser75805372011-04-29 06:27:02 +0000682 ++I)
Sebastian Pop9e3d2dd2013-06-11 22:20:27 +0000683 if (!isValidCFG(**I, Context))
684 return false;
685
Sebastian Pop8ca899c2013-06-14 20:20:43 +0000686 for (Region::block_iterator BI = R.block_begin(), E = R.block_end(); BI != E;
687 ++BI)
688 for (BasicBlock::iterator I = (*BI)->begin(), E = --(*BI)->end(); I != E;
689 ++I)
690 if (!isValidInstruction(*I, Context))
691 return false;
Tobias Grosser75805372011-04-29 06:27:02 +0000692
693 return true;
694}
695
696bool ScopDetection::isValidExit(DetectionContext &Context) const {
697 Region &R = Context.CurRegion;
698
699 // PHI nodes are not allowed in the exit basic block.
700 if (BasicBlock *Exit = R.getExit()) {
701 BasicBlock::iterator I = Exit->begin();
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000702 if (I != Exit->end() && isa<PHINode>(*I)) {
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000703 INVALID(Other, "PHI node in exit BB");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000704 return false;
705 }
Tobias Grosser75805372011-04-29 06:27:02 +0000706 }
707
708 return true;
709}
710
Tobias Grosser9b1100b2014-02-18 18:49:46 +0000711bool ScopDetection::isValidRegion(Region &R) const {
712 DetectionContext Context(R, *AA, false /*verifying*/);
713 return isValidRegion(Context);
714}
715
Tobias Grosser75805372011-04-29 06:27:02 +0000716bool ScopDetection::isValidRegion(DetectionContext &Context) const {
717 Region &R = Context.CurRegion;
718
719 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
720
Tobias Grosseraeabcf22013-04-02 06:41:48 +0000721 if (R.isTopLevelRegion()) {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000722 DEBUG(dbgs() << "Top level region is invalid"; dbgs() << "\n");
Tobias Grosser75805372011-04-29 06:27:02 +0000723 return false;
724 }
725
Tobias Grosser4449e522014-01-27 14:24:53 +0000726 if (!R.getEntry()->getName().count(OnlyRegion)) {
727 DEBUG({
728 dbgs() << "Region entry does not match -polly-region-only";
729 dbgs() << "\n";
730 });
731 return false;
732 }
733
Tobias Grossere602a072013-05-07 07:30:56 +0000734 if (!R.getEnteringBlock()) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000735 BasicBlock *entry = R.getEntry();
736 Loop *L = LI->getLoopFor(entry);
737
738 if (L) {
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000739 if (!L->isLoopSimplifyForm()) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000740 INVALID(SimpleLoop, "Loop not in simplify form is invalid!");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000741 return false;
742 }
Sebastian Pop9d632342013-06-11 22:20:40 +0000743
744 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
745 ++PI) {
746 // Region entering edges come from the same loop but outside the region
747 // are not allowed.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000748 if (L->contains(*PI) && !R.contains(*PI)) {
Sebastian Pop9d632342013-06-11 22:20:40 +0000749 INVALID(IndEdge, "Region has invalid entering edges!");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000750 return false;
751 }
Sebastian Pop9d632342013-06-11 22:20:40 +0000752 }
753 }
Tobias Grosser8edce4e2013-04-16 08:04:42 +0000754 }
755
Tobias Grosserd654c252012-04-10 18:12:19 +0000756 // SCoP cannot contain the entry block of the function, because we need
Tobias Grosser75805372011-04-29 06:27:02 +0000757 // to insert alloca instruction there when translate scalar to array.
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000758 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock())) {
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000759 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser5b1a7f22013-07-22 03:50:33 +0000760 return false;
761 }
Tobias Grosser75805372011-04-29 06:27:02 +0000762
Hongbin Zheng94868e62012-04-07 12:29:17 +0000763 if (!isValidExit(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000764 return false;
765
Hongbin Zheng94868e62012-04-07 12:29:17 +0000766 if (!allBlocksValid(Context))
Tobias Grosser75805372011-04-29 06:27:02 +0000767 return false;
768
769 DEBUG(dbgs() << "OK\n");
770 return true;
771}
772
773bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000774 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000775}
776
Tobias Grosser531891e2012-11-01 16:45:20 +0000777void ScopDetection::getDebugLocation(const Region *R, unsigned &LineBegin,
778 unsigned &LineEnd, std::string &FileName) {
779 LineBegin = -1;
780 LineEnd = 0;
781
782 for (Region::const_block_iterator RI = R->block_begin(), RE = R->block_end();
783 RI != RE; ++RI)
784 for (BasicBlock::iterator BI = (*RI)->begin(), BE = (*RI)->end(); BI != BE;
785 ++BI) {
786 DebugLoc DL = BI->getDebugLoc();
787 if (DL.isUnknown())
788 continue;
789
790 DIScope Scope(DL.getScope(BI->getContext()));
791
792 if (FileName.empty())
793 FileName = Scope.getFilename();
794
795 unsigned NewLine = DL.getLine();
796
797 LineBegin = std::min(LineBegin, NewLine);
798 LineEnd = std::max(LineEnd, NewLine);
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000799 }
Tobias Grosser531891e2012-11-01 16:45:20 +0000800}
801
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000802void ScopDetection::printLocations(llvm::Function &F) {
Tobias Grosserb5846f92014-03-02 17:05:27 +0000803 for (const auto &R : *this) {
Tobias Grosser531891e2012-11-01 16:45:20 +0000804 unsigned LineEntry, LineExit;
805 std::string FileName;
806
Tobias Grosser00dc3092014-03-02 12:02:46 +0000807 getDebugLocation(R, LineEntry, LineExit, FileName);
Tobias Grosser8519f892013-12-18 10:49:53 +0000808 DiagnosticScopFound Diagnostic(F, FileName, LineEntry, LineExit);
809 F.getContext().diagnose(Diagnostic);
Tobias Grosser531891e2012-11-01 16:45:20 +0000810 }
811}
812
Tobias Grosser75805372011-04-29 06:27:02 +0000813bool ScopDetection::runOnFunction(llvm::Function &F) {
Sebastian Pop8fe6d112013-05-30 17:47:32 +0000814 LI = &getAnalysis<LoopInfo>();
Tobias Grosser9a26f292014-01-02 22:28:53 +0000815 RI = &getAnalysis<RegionInfo>();
Sebastian Pop8fe6d112013-05-30 17:47:32 +0000816 if (!DetectScopsWithoutLoops && LI->empty())
817 return false;
818
Tobias Grosser75805372011-04-29 06:27:02 +0000819 AA = &getAnalysis<AliasAnalysis>();
820 SE = &getAnalysis<ScalarEvolution>();
Tobias Grosser75805372011-04-29 06:27:02 +0000821 Region *TopRegion = RI->getTopLevelRegion();
822
Tobias Grosser2ff87232011-10-23 11:17:06 +0000823 releaseMemory();
824
Tobias Grosser29ee0b12011-11-17 14:52:36 +0000825 if (OnlyFunction != "" && F.getName() != OnlyFunction)
Tobias Grosser2ff87232011-10-23 11:17:06 +0000826 return false;
827
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000828 if (!isValidFunction(F))
Tobias Grosser75805372011-04-29 06:27:02 +0000829 return false;
830
831 findScops(*TopRegion);
Tobias Grosser531891e2012-11-01 16:45:20 +0000832
833 if (ReportLevel >= 1)
Tobias Grosserb2863ca2013-03-04 19:49:51 +0000834 printLocations(F);
Tobias Grosser531891e2012-11-01 16:45:20 +0000835
Tobias Grosser75805372011-04-29 06:27:02 +0000836 return false;
837}
838
Tobias Grosser75805372011-04-29 06:27:02 +0000839void polly::ScopDetection::verifyRegion(const Region &R) const {
840 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
Tobias Grosser0d1eee32013-02-05 11:56:05 +0000841 DetectionContext Context(const_cast<Region &>(R), *AA, true /*verifying*/);
Tobias Grosser75805372011-04-29 06:27:02 +0000842 isValidRegion(Context);
843}
844
845void polly::ScopDetection::verifyAnalysis() const {
Tobias Grossera1689932014-02-18 18:49:49 +0000846 if (!VerifyScops)
847 return;
848
Tobias Grosserb5846f92014-03-02 17:05:27 +0000849 for (const auto &R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000850 verifyRegion(*R);
Tobias Grosser75805372011-04-29 06:27:02 +0000851}
852
853void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser42aff302014-01-13 22:29:56 +0000854 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser75805372011-04-29 06:27:02 +0000855 AU.addRequired<PostDominatorTree>();
856 AU.addRequired<LoopInfo>();
857 AU.addRequired<ScalarEvolution>();
858 // We also need AA and RegionInfo when we are verifying analysis.
859 AU.addRequiredTransitive<AliasAnalysis>();
860 AU.addRequiredTransitive<RegionInfo>();
861 AU.setPreservesAll();
862}
863
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000864void ScopDetection::print(raw_ostream &OS, const Module *) const {
Tobias Grosserb5846f92014-03-02 17:05:27 +0000865 for (const auto &R : ValidRegions)
Tobias Grosser00dc3092014-03-02 12:02:46 +0000866 OS << "Valid Region for Scop: " << R->getNameStr() << '\n';
Tobias Grosser75805372011-04-29 06:27:02 +0000867
868 OS << "\n";
869}
870
871void ScopDetection::releaseMemory() {
872 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000873 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000874 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000875}
876
877char ScopDetection::ID = 0;
878
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000879Pass *polly::createScopDetectionPass() { return new ScopDetection(); }
880
Tobias Grosser73600b82011-10-08 00:30:40 +0000881INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
882 "Polly - Detect static control parts (SCoPs)", false,
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000883 false);
884INITIALIZE_AG_DEPENDENCY(AliasAnalysis);
Tobias Grosser42aff302014-01-13 22:29:56 +0000885INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000886INITIALIZE_PASS_DEPENDENCY(LoopInfo);
887INITIALIZE_PASS_DEPENDENCY(PostDominatorTree);
888INITIALIZE_PASS_DEPENDENCY(RegionInfo);
889INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
Tobias Grosser73600b82011-10-08 00:30:40 +0000890INITIALIZE_PASS_END(ScopDetection, "polly-detect",
891 "Polly - Detect static control parts (SCoPs)", false, false)