blob: b2c7c96192dc0694944490418e28978f9d42aa94 [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
47#include "polly/ScopDetection.h"
48
49#include "polly/LinkAllPasses.h"
50#include "polly/Support/ScopHelper.h"
51#include "polly/Support/AffineSCEVIterator.h"
52
53#include "llvm/LLVMContext.h"
54#include "llvm/ADT/Statistic.h"
55#include "llvm/Analysis/AliasAnalysis.h"
56#include "llvm/Analysis/RegionIterator.h"
57#include "llvm/Support/CommandLine.h"
58#include "llvm/Assembly/Writer.h"
59
60#define DEBUG_TYPE "polly-detect"
61#include "llvm/Support/Debug.h"
62
63using namespace llvm;
64using namespace polly;
65
Tobias Grosser2ff87232011-10-23 11:17:06 +000066static cl::opt<std::string>
67OnlyFunction("polly-detect-only",
68 cl::desc("Only detect scops in function"), cl::Hidden,
69 cl::value_desc("The function name to detect scops in"),
70 cl::ValueRequired, cl::init(""));
71
72
Tobias Grosser75805372011-04-29 06:27:02 +000073//===----------------------------------------------------------------------===//
74// Statistics.
75
76STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
77
78#define BADSCOP_STAT(NAME, DESC) STATISTIC(Bad##NAME##ForScop, \
79 "Number of bad regions for Scop: "\
80 DESC)
81
82#define STATSCOP(NAME); assert(!Context.Verifying && #NAME); \
83 if (!Context.Verifying) ++Bad##NAME##ForScop;
84
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000085#define INVALID(NAME, MESSAGE) \
86 do { \
Tobias Grosser4f129a62011-10-08 00:30:55 +000087 std::string Buf; \
88 raw_string_ostream fmt(Buf); \
89 fmt << MESSAGE; \
90 fmt.flush(); \
91 LastFailure = Buf; \
Tobias Grosserc4a0bd12011-10-08 00:30:48 +000092 DEBUG(dbgs() << MESSAGE); \
93 DEBUG(dbgs() << "\n"); \
94 STATSCOP(NAME); \
95 return false; \
96 } while (0);
97
98
Tobias Grosser75805372011-04-29 06:27:02 +000099BADSCOP_STAT(CFG, "CFG too complex");
100BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
101BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
102BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
103BADSCOP_STAT(AffFunc, "Expression not affine");
104BADSCOP_STAT(Scalar, "Found scalar dependency");
105BADSCOP_STAT(Alias, "Found base address alias");
106BADSCOP_STAT(SimpleRegion, "Region not simple");
107BADSCOP_STAT(Other, "Others");
108
109//===----------------------------------------------------------------------===//
110// ScopDetection.
111
Tobias Grosser3fb49922011-11-02 21:40:08 +0000112namespace SCEVType {
113 enum TYPE {INT, PARAM, IV, INVALID};
114}
115
116/// Check if a SCEV is valid in a SCoP.
117struct SCEVValidator : public SCEVVisitor<SCEVValidator, SCEVType::TYPE> {
118private:
119 const Region *R;
120 ScalarEvolution &SE;
Tobias Grosser76164672011-11-03 21:03:18 +0000121 Value **BaseAddress;
Tobias Grosser3fb49922011-11-02 21:40:08 +0000122
123public:
124 static bool isValid(const Region *R, const SCEV *Scev,
125 ScalarEvolution &SE,
Tobias Grosser76164672011-11-03 21:03:18 +0000126 Value **BaseAddress = NULL) {
Tobias Grosser3fb49922011-11-02 21:40:08 +0000127 if (isa<SCEVCouldNotCompute>(Scev))
128 return false;
129
Tobias Grosser76164672011-11-03 21:03:18 +0000130 if (BaseAddress)
131 *BaseAddress = NULL;
132
Tobias Grosser3fb49922011-11-02 21:40:08 +0000133 SCEVValidator Validator(R, SE, BaseAddress);
134 return Validator.visit(Scev) != SCEVType::INVALID;
135 }
136
137 SCEVValidator(const Region *R, ScalarEvolution &SE,
Tobias Grosser76164672011-11-03 21:03:18 +0000138 Value **BaseAddress) : R(R), SE(SE),
Tobias Grosser3fb49922011-11-02 21:40:08 +0000139 BaseAddress(BaseAddress) {};
140
141 SCEVType::TYPE visitConstant(const SCEVConstant *Constant) {
142 return SCEVType::INT;
143 }
144
145 SCEVType::TYPE visitTruncateExpr(const SCEVTruncateExpr* Expr) {
146 SCEVType::TYPE Op = visit(Expr->getOperand());
147
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000148 // We currently do not represent a truncate expression as an affine
149 // expression. If it is constant during Scop execution, we treat it as a
150 // parameter, otherwise we bail out.
Tobias Grosser3fb49922011-11-02 21:40:08 +0000151 if (Op == SCEVType::INT || Op == SCEVType::PARAM)
152 return SCEVType::PARAM;
153
154 return SCEVType::INVALID;
155 }
156
157 SCEVType::TYPE visitZeroExtendExpr(const SCEVZeroExtendExpr * Expr) {
158 SCEVType::TYPE Op = visit(Expr->getOperand());
159
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000160 // We currently do not represent a zero extend expression as an affine
161 // expression. If it is constant during Scop execution, we treat it as a
162 // parameter, otherwise we bail out.
Tobias Grosser3fb49922011-11-02 21:40:08 +0000163 if (Op == SCEVType::INT || Op == SCEVType::PARAM)
164 return SCEVType::PARAM;
165
166 return SCEVType::INVALID;
167 }
168
169 SCEVType::TYPE visitSignExtendExpr(const SCEVSignExtendExpr* Expr) {
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000170 // We currently allow only signed SCEV expressions. In the case of a
171 // signed value, a sign extend is a noop.
172 //
173 // TODO: Reconsider this when we add support for unsigned values.
Tobias Grosser3fb49922011-11-02 21:40:08 +0000174 return visit(Expr->getOperand());
175 }
176
177 SCEVType::TYPE visitAddExpr(const SCEVAddExpr* Expr) {
178 SCEVType::TYPE Return = SCEVType::INT;
179
180 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
181 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000182
183 if (OpType == SCEVType::INVALID)
184 return SCEVType::INVALID;
185
Tobias Grosser3fb49922011-11-02 21:40:08 +0000186 Return = std::max(Return, OpType);
187 }
188
189 // TODO: Check for NSW and NUW.
190 return Return;
191 }
192
193 SCEVType::TYPE visitMulExpr(const SCEVMulExpr* Expr) {
194 SCEVType::TYPE Return = SCEVType::INT;
195
196 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
197 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
198
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000199 if (OpType == SCEVType::INT)
200 continue;
201
202 if (OpType == SCEVType::INVALID || Return != SCEVType::INT)
Tobias Grosser3fb49922011-11-02 21:40:08 +0000203 return SCEVType::INVALID;
204
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000205 Return = OpType;
Tobias Grosser3fb49922011-11-02 21:40:08 +0000206 }
207
208 // TODO: Check for NSW and NUW.
209 return Return;
210 }
211
212 SCEVType::TYPE visitUDivExpr(const SCEVUDivExpr* Expr) {
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000213 SCEVType::TYPE LHS = visit(Expr->getLHS());
214 SCEVType::TYPE RHS = visit(Expr->getRHS());
215
216 // We currently do not represent a unsigned devision as an affine
217 // expression. If the division is constant during Scop execution we treat it
218 // as a parameter, otherwise we bail out.
219 if (LHS == SCEVType::INT || LHS == SCEVType::PARAM ||
220 RHS == SCEVType::INT || RHS == SCEVType::PARAM)
221 return SCEVType::PARAM;
222
Tobias Grosser3fb49922011-11-02 21:40:08 +0000223 return SCEVType::INVALID;
224 }
225
226 SCEVType::TYPE visitAddRecExpr(const SCEVAddRecExpr* Expr) {
227 if (!Expr->isAffine())
228 return SCEVType::INVALID;
229
230 SCEVType::TYPE Start = visit(Expr->getStart());
Tobias Grosser3fb49922011-11-02 21:40:08 +0000231 SCEVType::TYPE Recurrence = visit(Expr->getStepRecurrence(SE));
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000232
233 if (Start == SCEVType::INVALID ||
234 Recurrence == SCEVType::INVALID ||
235 Recurrence == SCEVType::IV)
236 return SCEVType::INVALID;
237
238 if (!R->contains(Expr->getLoop())) {
239 if (Start == SCEVType::IV)
240 return SCEVType::INVALID;
241 else
242 return SCEVType::PARAM;
243 }
244
Tobias Grosser3fb49922011-11-02 21:40:08 +0000245 if (Recurrence != SCEVType::INT)
246 return SCEVType::INVALID;
247
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000248 return SCEVType::IV;
Tobias Grosser3fb49922011-11-02 21:40:08 +0000249 }
250
251 SCEVType::TYPE visitSMaxExpr(const SCEVSMaxExpr* Expr) {
252 SCEVType::TYPE Return = SCEVType::INT;
253
254 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
255 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
256
257 if (OpType == SCEVType::INVALID)
258 return SCEVType::INVALID;
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000259
260 Return = std::max(Return, OpType);
Tobias Grosser3fb49922011-11-02 21:40:08 +0000261 }
262
263 return Return;
264 }
265
266 SCEVType::TYPE visitUMaxExpr(const SCEVUMaxExpr* Expr) {
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000267 // We do not support unsigned operations. If 'Expr' is constant during Scop
268 // execution we treat this as a parameter, otherwise we bail out.
Tobias Grosser3fb49922011-11-02 21:40:08 +0000269 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
270 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
271
272 if (OpType != SCEVType::INT && OpType != SCEVType::PARAM)
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000273 return SCEVType::INVALID;
Tobias Grosser3fb49922011-11-02 21:40:08 +0000274 }
275
276 return SCEVType::PARAM;
277 }
278
279 SCEVType::TYPE visitUnknown(const SCEVUnknown* Expr) {
Tobias Grosser76164672011-11-03 21:03:18 +0000280 Value *V = Expr->getValue();
281
282 if (isa<UndefValue>(V))
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000283 return SCEVType::INVALID;
Tobias Grosser76164672011-11-03 21:03:18 +0000284
285 if (BaseAddress) {
286 if (*BaseAddress)
287 return SCEVType::INVALID;
288 else
289 *BaseAddress = V;
290 }
291
Tobias Grosserad96c4b2011-11-03 21:03:01 +0000292 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue()))
293 if (R->contains(I))
294 return SCEVType::INVALID;
Tobias Grosser2d0b1f92011-11-04 10:08:08 +0000295
Tobias Grosser3fb49922011-11-02 21:40:08 +0000296 return SCEVType::PARAM;
297 }
298};
299
Tobias Grosser75805372011-04-29 06:27:02 +0000300bool ScopDetection::isMaxRegionInScop(const Region &R) const {
301 // The Region is valid only if it could be found in the set.
302 return ValidRegions.count(&R);
303}
304
Tobias Grosser4f129a62011-10-08 00:30:55 +0000305std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
306 if (!InvalidRegions.count(R))
307 return "";
308
309 return InvalidRegions.find(R)->second;
310}
311
312
Tobias Grosser75805372011-04-29 06:27:02 +0000313bool ScopDetection::isValidAffineFunction(const SCEV *S, Region &RefRegion,
314 Value **BasePtr) const {
315 assert(S && "S must not be null!");
316 bool isMemoryAccess = (BasePtr != 0);
317 if (isMemoryAccess) *BasePtr = 0;
318 DEBUG(dbgs() << "Checking " << *S << " ... ");
319
320 if (isa<SCEVCouldNotCompute>(S)) {
321 DEBUG(dbgs() << "Non Affine: SCEV could not be computed\n");
322 return false;
323 }
324
325 for (AffineSCEVIterator I = affine_begin(S, SE), E = affine_end(); I != E;
326 ++I) {
327 // The constant part must be a SCEVConstant.
328 // TODO: support sizeof in coefficient.
329 if (!isa<SCEVConstant>(I->second)) {
330 DEBUG(dbgs() << "Non Affine: Right hand side is not constant\n");
331 return false;
332 }
333
334 const SCEV *Var = I->first;
335
336 // A constant offset is affine.
337 if(isa<SCEVConstant>(Var))
338 continue;
339
340 // Memory accesses are allowed to have a base pointer.
341 if (Var->getType()->isPointerTy()) {
342 if (!isMemoryAccess) {
343 DEBUG(dbgs() << "Non Affine: Pointer in non memory access\n");
344 return false;
345 }
346
347 assert(I->second->isOne() && "Only one as pointer coefficient allowed.\n");
348 const SCEVUnknown *BaseAddr = dyn_cast<SCEVUnknown>(Var);
349
350 if (!BaseAddr || isa<UndefValue>(BaseAddr->getValue())){
351 DEBUG(dbgs() << "Cannot handle base: " << *Var << "\n");
352 return false;
353 }
354
355 // BaseAddr must be invariant in Scop.
356 if (!isParameter(BaseAddr, RefRegion, *LI, *SE)) {
357 DEBUG(dbgs() << "Non Affine: Base address not invariant in SCoP\n");
358 return false;
359 }
360
361 assert(*BasePtr == 0 && "Found second base pointer.\n");
362 *BasePtr = BaseAddr->getValue();
363 continue;
364 }
365
366 if (isParameter(Var, RefRegion, *LI, *SE)
367 || isIndVar(Var, RefRegion, *LI, *SE))
368 continue;
369
370 DEBUG(dbgs() << "Non Affine: " ;
371 Var->print(dbgs());
372 dbgs() << " is neither parameter nor induction variable\n");
373 return false;
374 }
375
376 DEBUG(dbgs() << " is affine.\n");
377 return !isMemoryAccess || (*BasePtr != 0);
378}
379
380bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const
381{
382 Region &RefRegion = Context.CurRegion;
383 TerminatorInst *TI = BB.getTerminator();
384
385 // Return instructions are only valid if the region is the top level region.
386 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
387 return true;
388
389 BranchInst *Br = dyn_cast<BranchInst>(TI);
390
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000391 if (!Br)
392 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000393
394 if (Br->isUnconditional()) return true;
395
396 Value *Condition = Br->getCondition();
397
398 // UndefValue is not allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000399 if (isa<UndefValue>(Condition))
400 INVALID(AffFunc, "Condition based on 'undef' value in BB: "
401 + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000402
403 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000404 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
405 INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither "
406 "constant nor an icmp instruction");
Tobias Grosser75805372011-04-29 06:27:02 +0000407
408 // Allow perfectly nested conditions.
409 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
410
411 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
412 // Unsigned comparisons are not allowed. They trigger overflow problems
413 // in the code generation.
414 //
415 // TODO: This is not sufficient and just hides bugs. However it does pretty
416 // well.
417 if(ICmp->isUnsigned())
418 return false;
419
420 // Are both operands of the ICmp affine?
421 if (isa<UndefValue>(ICmp->getOperand(0))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000422 || isa<UndefValue>(ICmp->getOperand(1)))
423 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000424
425 const SCEV *ScevLHS = SE->getSCEV(ICmp->getOperand(0));
426 const SCEV *ScevRHS = SE->getSCEV(ICmp->getOperand(1));
427
Tobias Grosser2fea5c62011-11-03 21:03:14 +0000428 bool affineLHS = SCEVValidator::isValid(&Context.CurRegion, ScevLHS, *SE);
429 bool affineRHS = SCEVValidator::isValid(&Context.CurRegion, ScevRHS, *SE);
Tobias Grosser75805372011-04-29 06:27:02 +0000430
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000431 if (!affineLHS || !affineRHS)
432 INVALID(AffFunc, "Non affine branch in BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000433 }
434
435 // Allow loop exit conditions.
436 Loop *L = LI->getLoopFor(&BB);
437 if (L && L->getExitingBlock() == &BB)
438 return true;
439
440 // Allow perfectly nested conditions.
441 Region *R = RI->getRegionFor(&BB);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000442 if (R->getEntry() != &BB)
443 INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000444
445 return true;
446}
447
448bool ScopDetection::isValidCallInst(CallInst &CI) {
449 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
450 return false;
451
452 if (CI.doesNotAccessMemory())
453 return true;
454
455 Function *CalledFunction = CI.getCalledFunction();
456
457 // Indirect calls are not supported.
458 if (CalledFunction == 0)
459 return false;
460
461 // TODO: Intrinsics.
462 return false;
463}
464
465bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
466 DetectionContext &Context) const {
467 Value *Ptr = getPointerOperand(Inst), *BasePtr;
468 const SCEV *AccessFunction = SE->getSCEV(Ptr);
469
Tobias Grosser76164672011-11-03 21:03:18 +0000470 if (!SCEVValidator::isValid(&Context.CurRegion, AccessFunction, *SE,
471 &BasePtr))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000472 INVALID(AffFunc, "Bad memory address " << *AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000473
Tobias Grosser76164672011-11-03 21:03:18 +0000474 // FIXME: Also check with isValidAffineFunction, as for the moment it is
475 // protecting us to fail because of not supported features in TempScop.
476 // As soon as TempScop is fixed, this needs to be removed.
477 if (!isValidAffineFunction(AccessFunction, Context.CurRegion, &BasePtr))
478 INVALID(AffFunc, "Access not supported in TempScop" << *AccessFunction);
479
Tobias Grosser75805372011-04-29 06:27:02 +0000480 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
481 // created by IndependentBlocks Pass.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000482 if (isa<IntToPtrInst>(BasePtr))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000483 INVALID(Other, "Find bad intToptr prt: " << *BasePtr);
Tobias Grosser75805372011-04-29 06:27:02 +0000484
485 // Check if the base pointer of the memory access does alias with
486 // any other pointer. This cannot be handled at the moment.
487 AliasSet &AS =
488 Context.AST.getAliasSetForPointer(BasePtr, AliasAnalysis::UnknownSize,
489 Inst.getMetadata(LLVMContext::MD_tbaa));
490 if (!AS.isMustAlias()) {
491 DEBUG(dbgs() << "Bad pointer alias found:" << *BasePtr << "\nAS:\n" << AS);
492
493 // STATSCOP triggers an assertion if we are in verifying mode.
494 // This is generally good to check that we do not change the SCoP after we
495 // run the SCoP detection and consequently to ensure that we can still
496 // represent that SCoP. However, in case of aliasing this does not work.
497 // The independent blocks pass may create memory references which seem to
498 // alias, if -basicaa is not available. They actually do not. As we do not
499 // not know this and we would fail here if we verify it.
500 if (!Context.Verifying) {
501 STATSCOP(Alias);
502 }
503
504 return false;
505 }
506
507 return true;
508}
509
510
511bool ScopDetection::hasScalarDependency(Instruction &Inst,
512 Region &RefRegion) const {
513 for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
514 UI != UE; ++UI)
515 if (Instruction *Use = dyn_cast<Instruction>(*UI))
516 if (!RefRegion.contains(Use->getParent())) {
517 // DirtyHack 1: PHINode user outside the Scop is not allow, if this
518 // PHINode is induction variable, the scalar to array transform may
519 // break it and introduce a non-indvar PHINode, which is not allow in
520 // Scop.
521 // This can be fix by:
522 // Introduce a IndependentBlockPrepare pass, which translate all
523 // PHINodes not in Scop to array.
524 // The IndependentBlockPrepare pass can also split the entry block of
525 // the function to hold the alloca instruction created by scalar to
526 // array. and split the exit block of the Scop so the new create load
527 // instruction for escape users will not break other Scops.
528 if (isa<PHINode>(Use))
529 return true;
530 }
531
532 return false;
533}
534
535bool ScopDetection::isValidInstruction(Instruction &Inst,
536 DetectionContext &Context) const {
537 // Only canonical IVs are allowed.
538 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000539 if (!isIndVar(PN, LI))
540 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000541
542 // Scalar dependencies are not allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000543 if (hasScalarDependency(Inst, Context.CurRegion))
544 INVALID(Scalar, "Scalar dependency found: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000545
546 // We only check the call instruction but not invoke instruction.
547 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
548 if (isValidCallInst(*CI))
549 return true;
550
Tobias Grosserb43ba822011-10-08 00:49:30 +0000551 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000552 }
553
554 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
555 // Handle cast instruction.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000556 if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000557 INVALID(Other, "Cast instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000558
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000559 if (isa<AllocaInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000560 INVALID(Other, "Alloca instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000561
562 return true;
563 }
564
565 // Check the access function.
566 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
567 return isValidMemoryAccess(Inst, Context);
568
569 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000570 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000571}
572
573bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
574 DetectionContext &Context) const {
575 if (!isValidCFG(BB, Context))
576 return false;
577
578 // Check all instructions, except the terminator instruction.
579 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
580 if (!isValidInstruction(*I, Context))
581 return false;
582
583 Loop *L = LI->getLoopFor(&BB);
584 if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
585 return false;
586
587 return true;
588}
589
590bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
591 PHINode *IndVar = L->getCanonicalInductionVariable();
592 // No canonical induction variable.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000593 if (!IndVar)
Tobias Grosserb43ba822011-10-08 00:49:30 +0000594 INVALID(IndVar, "No canonical IV at loop header: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000595 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000596
597 // Is the loop count affine?
598 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosser3fb49922011-11-02 21:40:08 +0000599 if (!SCEVValidator::isValid(&Context.CurRegion, LoopCount, *SE))
Tobias Grosserbd54f322011-10-26 01:27:49 +0000600 INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000601 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000602
603 return true;
604}
605
606Region *ScopDetection::expandRegion(Region &R) {
607 Region *CurrentRegion = &R;
608 Region *TmpRegion = R.getExpandedRegion();
609
610 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
611
612 while (TmpRegion) {
613 DetectionContext Context(*TmpRegion, *AA, false /*verifying*/);
614 DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n");
615
616 if (!allBlocksValid(Context))
617 break;
618
619 if (isValidExit(Context)) {
620 if (CurrentRegion != &R)
621 delete CurrentRegion;
622
623 CurrentRegion = TmpRegion;
624 }
625
626 Region *TmpRegion2 = TmpRegion->getExpandedRegion();
627
628 if (TmpRegion != &R && TmpRegion != CurrentRegion)
629 delete TmpRegion;
630
631 TmpRegion = TmpRegion2;
632 }
633
634 if (&R == CurrentRegion)
635 return NULL;
636
637 DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n");
638
639 return CurrentRegion;
640}
641
642
643void ScopDetection::findScops(Region &R) {
644 DetectionContext Context(R, *AA, false /*verifying*/);
645
646 if (isValidRegion(Context)) {
647 ++ValidRegion;
648 ValidRegions.insert(&R);
649 return;
650 }
651
Tobias Grosser4f129a62011-10-08 00:30:55 +0000652 InvalidRegions[&R] = LastFailure;
653
Tobias Grosser75805372011-04-29 06:27:02 +0000654 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
655 findScops(**I);
656
657 // Try to expand regions.
658 //
659 // As the region tree normally only contains canonical regions, non canonical
660 // regions that form a Scop are not found. Therefore, those non canonical
661 // regions are checked by expanding the canonical ones.
662
663 std::vector<Region*> ToExpand;
664
665 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
666 ToExpand.push_back(*I);
667
668 for (std::vector<Region*>::iterator RI = ToExpand.begin(),
669 RE = ToExpand.end(); RI != RE; ++RI) {
670 Region *CurrentRegion = *RI;
671
672 // Skip invalid regions. Regions may become invalid, if they are element of
673 // an already expanded region.
674 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
675 continue;
676
677 Region *ExpandedR = expandRegion(*CurrentRegion);
678
679 if (!ExpandedR)
680 continue;
681
682 R.addSubRegion(ExpandedR, true);
683 ValidRegions.insert(ExpandedR);
684 ValidRegions.erase(CurrentRegion);
685
686 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
687 ++I)
688 ValidRegions.erase(*I);
689 }
690}
691
692bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
693 Region &R = Context.CurRegion;
694
695 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
696 ++I)
697 if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context))
698 return false;
699
700 return true;
701}
702
703bool ScopDetection::isValidExit(DetectionContext &Context) const {
704 Region &R = Context.CurRegion;
705
706 // PHI nodes are not allowed in the exit basic block.
707 if (BasicBlock *Exit = R.getExit()) {
708 BasicBlock::iterator I = Exit->begin();
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000709 if (I != Exit->end() && isa<PHINode> (*I))
710 INVALID(Other, "PHI node in exit BB");
Tobias Grosser75805372011-04-29 06:27:02 +0000711 }
712
713 return true;
714}
715
716bool ScopDetection::isValidRegion(DetectionContext &Context) const {
717 Region &R = Context.CurRegion;
718
719 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
720
721 // The toplevel region is no valid region.
722 if (!R.getParent()) {
723 DEBUG(dbgs() << "Top level region is invalid";
724 dbgs() << "\n");
725 return false;
726 }
727
728 // SCoP can not contains the entry block of the function, because we need
729 // to insert alloca instruction there when translate scalar to array.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000730 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
731 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser75805372011-04-29 06:27:02 +0000732
733 // Only a simple region is allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000734 if (!R.isSimple())
735 INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000736
737 if (!allBlocksValid(Context))
738 return false;
739
740 if (!isValidExit(Context))
741 return false;
742
743 DEBUG(dbgs() << "OK\n");
744 return true;
745}
746
747bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000748 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000749}
750
751bool ScopDetection::runOnFunction(llvm::Function &F) {
752 AA = &getAnalysis<AliasAnalysis>();
753 SE = &getAnalysis<ScalarEvolution>();
754 LI = &getAnalysis<LoopInfo>();
755 RI = &getAnalysis<RegionInfo>();
756 Region *TopRegion = RI->getTopLevelRegion();
757
Tobias Grosser2ff87232011-10-23 11:17:06 +0000758 releaseMemory();
759
760 if (OnlyFunction != "" && F.getNameStr() != OnlyFunction)
761 return false;
762
Tobias Grosser75805372011-04-29 06:27:02 +0000763 if(!isValidFunction(F))
764 return false;
765
766 findScops(*TopRegion);
767 return false;
768}
769
770
771void polly::ScopDetection::verifyRegion(const Region &R) const {
772 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
773 DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
774 isValidRegion(Context);
775}
776
777void polly::ScopDetection::verifyAnalysis() const {
778 for (RegionSet::const_iterator I = ValidRegions.begin(),
779 E = ValidRegions.end(); I != E; ++I)
780 verifyRegion(**I);
781}
782
783void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
784 AU.addRequired<DominatorTree>();
785 AU.addRequired<PostDominatorTree>();
786 AU.addRequired<LoopInfo>();
787 AU.addRequired<ScalarEvolution>();
788 // We also need AA and RegionInfo when we are verifying analysis.
789 AU.addRequiredTransitive<AliasAnalysis>();
790 AU.addRequiredTransitive<RegionInfo>();
791 AU.setPreservesAll();
792}
793
794void ScopDetection::print(raw_ostream &OS, const Module *) const {
795 for (RegionSet::const_iterator I = ValidRegions.begin(),
796 E = ValidRegions.end(); I != E; ++I)
797 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
798
799 OS << "\n";
800}
801
802void ScopDetection::releaseMemory() {
803 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000804 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000805 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000806}
807
808char ScopDetection::ID = 0;
809
Tobias Grosser73600b82011-10-08 00:30:40 +0000810INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
811 "Polly - Detect static control parts (SCoPs)", false,
812 false)
813INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
814INITIALIZE_PASS_DEPENDENCY(DominatorTree)
815INITIALIZE_PASS_DEPENDENCY(LoopInfo)
816INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
817INITIALIZE_PASS_DEPENDENCY(RegionInfo)
818INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
819INITIALIZE_PASS_END(ScopDetection, "polly-detect",
820 "Polly - Detect static control parts (SCoPs)", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000821
Tobias Grosser83f5c432011-08-23 22:35:08 +0000822Pass *polly::createScopDetectionPass() {
823 return new ScopDetection();
824}