blob: afceebc2a2f0a8c954955516028f33aced2121ef [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;
121 const Value **BaseAddress;
122
123public:
124 static bool isValid(const Region *R, const SCEV *Scev,
125 ScalarEvolution &SE,
126 const Value **BaseAddress = NULL) {
127 if (isa<SCEVCouldNotCompute>(Scev))
128 return false;
129
130 SCEVValidator Validator(R, SE, BaseAddress);
131 return Validator.visit(Scev) != SCEVType::INVALID;
132 }
133
134 SCEVValidator(const Region *R, ScalarEvolution &SE,
135 const Value **BaseAddress) : R(R), SE(SE),
136 BaseAddress(BaseAddress) {};
137
138 SCEVType::TYPE visitConstant(const SCEVConstant *Constant) {
139 return SCEVType::INT;
140 }
141
142 SCEVType::TYPE visitTruncateExpr(const SCEVTruncateExpr* Expr) {
143 SCEVType::TYPE Op = visit(Expr->getOperand());
144
145 // We cannot represent this as a affine expression yet. If it is constant
146 // during Scop execution treat this as a parameter, otherwise bail out.
147 if (Op == SCEVType::INT || Op == SCEVType::PARAM)
148 return SCEVType::PARAM;
149
150 return SCEVType::INVALID;
151 }
152
153 SCEVType::TYPE visitZeroExtendExpr(const SCEVZeroExtendExpr * Expr) {
154 SCEVType::TYPE Op = visit(Expr->getOperand());
155
156 // We cannot represent this as a affine expression yet. If it is constant
157 // during Scop execution treat this as a parameter, otherwise bail out.
158 if (Op == SCEVType::INT || Op == SCEVType::PARAM)
159 return SCEVType::PARAM;
160
161 return SCEVType::INVALID;
162 }
163
164 SCEVType::TYPE visitSignExtendExpr(const SCEVSignExtendExpr* Expr) {
165 // Assuming the value is signed, a sign extension is basically a noop.
166 // TODO: Reconsider this as soon as we support unsigned values.
167 return visit(Expr->getOperand());
168 }
169
170 SCEVType::TYPE visitAddExpr(const SCEVAddExpr* Expr) {
171 SCEVType::TYPE Return = SCEVType::INT;
172
173 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
174 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
175 Return = std::max(Return, OpType);
176 }
177
178 // TODO: Check for NSW and NUW.
179 return Return;
180 }
181
182 SCEVType::TYPE visitMulExpr(const SCEVMulExpr* Expr) {
183 SCEVType::TYPE Return = SCEVType::INT;
184
185 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
186 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
187
188 if (OpType == SCEVType::INVALID)
189 return SCEVType::INVALID;
190
191 if (OpType == SCEVType::IV) {
192 if (Return == SCEVType::PARAM || Return == SCEVType::IV)
193 return SCEVType::INVALID;
194
195 Return = OpType;
196 continue;
197 }
198
199 if (OpType == SCEVType::PARAM) {
200 if (Return == SCEVType::PARAM)
201 return SCEVType::INVALID;
202
203 Return = SCEVType::PARAM;
204 continue;
205 }
206
207 // OpType == SCEVType::INT, no need to change anything.
208 }
209
210 // TODO: Check for NSW and NUW.
211 return Return;
212 }
213
214 SCEVType::TYPE visitUDivExpr(const SCEVUDivExpr* Expr) {
215 // We do not yet support unsigned operations.
216 return SCEVType::INVALID;
217 }
218
219 SCEVType::TYPE visitAddRecExpr(const SCEVAddRecExpr* Expr) {
220 if (!Expr->isAffine())
221 return SCEVType::INVALID;
222
223 SCEVType::TYPE Start = visit(Expr->getStart());
224
225 if (Start == SCEVType::INVALID)
226 return Start;
227
228 SCEVType::TYPE Recurrence = visit(Expr->getStepRecurrence(SE));
229 if (Recurrence != SCEVType::INT)
230 return SCEVType::INVALID;
231
232 return SCEVType::PARAM;
233 }
234
235 SCEVType::TYPE visitSMaxExpr(const SCEVSMaxExpr* Expr) {
236 SCEVType::TYPE Return = SCEVType::INT;
237
238 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
239 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
240
241 if (OpType == SCEVType::INVALID)
242 return SCEVType::INVALID;
243 if (OpType == SCEVType::PARAM)
244 Return = SCEVType::PARAM;
245 }
246
247 return Return;
248 }
249
250 SCEVType::TYPE visitUMaxExpr(const SCEVUMaxExpr* Expr) {
251 // We do not yet support unsigned operations. If 'Expr' is constant
252 // during Scop execution treat this as a parameter, otherwise bail out.
253 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
254 SCEVType::TYPE OpType = visit(Expr->getOperand(i));
255
256 if (OpType != SCEVType::INT && OpType != SCEVType::PARAM)
257 return SCEVType::PARAM;
258 }
259
260 return SCEVType::PARAM;
261 }
262
263 SCEVType::TYPE visitUnknown(const SCEVUnknown* Expr) {
Tobias Grosserad96c4b2011-11-03 21:03:01 +0000264 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue()))
265 if (R->contains(I))
266 return SCEVType::INVALID;
Tobias Grosser3fb49922011-11-02 21:40:08 +0000267 return SCEVType::PARAM;
268 }
269};
270
Tobias Grosser75805372011-04-29 06:27:02 +0000271bool ScopDetection::isMaxRegionInScop(const Region &R) const {
272 // The Region is valid only if it could be found in the set.
273 return ValidRegions.count(&R);
274}
275
Tobias Grosser4f129a62011-10-08 00:30:55 +0000276std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
277 if (!InvalidRegions.count(R))
278 return "";
279
280 return InvalidRegions.find(R)->second;
281}
282
283
Tobias Grosser75805372011-04-29 06:27:02 +0000284bool ScopDetection::isValidAffineFunction(const SCEV *S, Region &RefRegion,
285 Value **BasePtr) const {
286 assert(S && "S must not be null!");
287 bool isMemoryAccess = (BasePtr != 0);
288 if (isMemoryAccess) *BasePtr = 0;
289 DEBUG(dbgs() << "Checking " << *S << " ... ");
290
291 if (isa<SCEVCouldNotCompute>(S)) {
292 DEBUG(dbgs() << "Non Affine: SCEV could not be computed\n");
293 return false;
294 }
295
296 for (AffineSCEVIterator I = affine_begin(S, SE), E = affine_end(); I != E;
297 ++I) {
298 // The constant part must be a SCEVConstant.
299 // TODO: support sizeof in coefficient.
300 if (!isa<SCEVConstant>(I->second)) {
301 DEBUG(dbgs() << "Non Affine: Right hand side is not constant\n");
302 return false;
303 }
304
305 const SCEV *Var = I->first;
306
307 // A constant offset is affine.
308 if(isa<SCEVConstant>(Var))
309 continue;
310
311 // Memory accesses are allowed to have a base pointer.
312 if (Var->getType()->isPointerTy()) {
313 if (!isMemoryAccess) {
314 DEBUG(dbgs() << "Non Affine: Pointer in non memory access\n");
315 return false;
316 }
317
318 assert(I->second->isOne() && "Only one as pointer coefficient allowed.\n");
319 const SCEVUnknown *BaseAddr = dyn_cast<SCEVUnknown>(Var);
320
321 if (!BaseAddr || isa<UndefValue>(BaseAddr->getValue())){
322 DEBUG(dbgs() << "Cannot handle base: " << *Var << "\n");
323 return false;
324 }
325
326 // BaseAddr must be invariant in Scop.
327 if (!isParameter(BaseAddr, RefRegion, *LI, *SE)) {
328 DEBUG(dbgs() << "Non Affine: Base address not invariant in SCoP\n");
329 return false;
330 }
331
332 assert(*BasePtr == 0 && "Found second base pointer.\n");
333 *BasePtr = BaseAddr->getValue();
334 continue;
335 }
336
337 if (isParameter(Var, RefRegion, *LI, *SE)
338 || isIndVar(Var, RefRegion, *LI, *SE))
339 continue;
340
341 DEBUG(dbgs() << "Non Affine: " ;
342 Var->print(dbgs());
343 dbgs() << " is neither parameter nor induction variable\n");
344 return false;
345 }
346
347 DEBUG(dbgs() << " is affine.\n");
348 return !isMemoryAccess || (*BasePtr != 0);
349}
350
351bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const
352{
353 Region &RefRegion = Context.CurRegion;
354 TerminatorInst *TI = BB.getTerminator();
355
356 // Return instructions are only valid if the region is the top level region.
357 if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
358 return true;
359
360 BranchInst *Br = dyn_cast<BranchInst>(TI);
361
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000362 if (!Br)
363 INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000364
365 if (Br->isUnconditional()) return true;
366
367 Value *Condition = Br->getCondition();
368
369 // UndefValue is not allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000370 if (isa<UndefValue>(Condition))
371 INVALID(AffFunc, "Condition based on 'undef' value in BB: "
372 + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000373
374 // Only Constant and ICmpInst are allowed as condition.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000375 if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
376 INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither "
377 "constant nor an icmp instruction");
Tobias Grosser75805372011-04-29 06:27:02 +0000378
379 // Allow perfectly nested conditions.
380 assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
381
382 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
383 // Unsigned comparisons are not allowed. They trigger overflow problems
384 // in the code generation.
385 //
386 // TODO: This is not sufficient and just hides bugs. However it does pretty
387 // well.
388 if(ICmp->isUnsigned())
389 return false;
390
391 // Are both operands of the ICmp affine?
392 if (isa<UndefValue>(ICmp->getOperand(0))
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000393 || isa<UndefValue>(ICmp->getOperand(1)))
394 INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000395
396 const SCEV *ScevLHS = SE->getSCEV(ICmp->getOperand(0));
397 const SCEV *ScevRHS = SE->getSCEV(ICmp->getOperand(1));
398
Tobias Grosser2fea5c62011-11-03 21:03:14 +0000399 bool affineLHS = SCEVValidator::isValid(&Context.CurRegion, ScevLHS, *SE);
400 bool affineRHS = SCEVValidator::isValid(&Context.CurRegion, ScevRHS, *SE);
Tobias Grosser75805372011-04-29 06:27:02 +0000401
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000402 if (!affineLHS || !affineRHS)
403 INVALID(AffFunc, "Non affine branch in BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000404 }
405
406 // Allow loop exit conditions.
407 Loop *L = LI->getLoopFor(&BB);
408 if (L && L->getExitingBlock() == &BB)
409 return true;
410
411 // Allow perfectly nested conditions.
412 Region *R = RI->getRegionFor(&BB);
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000413 if (R->getEntry() != &BB)
414 INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000415
416 return true;
417}
418
419bool ScopDetection::isValidCallInst(CallInst &CI) {
420 if (CI.mayHaveSideEffects() || CI.doesNotReturn())
421 return false;
422
423 if (CI.doesNotAccessMemory())
424 return true;
425
426 Function *CalledFunction = CI.getCalledFunction();
427
428 // Indirect calls are not supported.
429 if (CalledFunction == 0)
430 return false;
431
432 // TODO: Intrinsics.
433 return false;
434}
435
436bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
437 DetectionContext &Context) const {
438 Value *Ptr = getPointerOperand(Inst), *BasePtr;
439 const SCEV *AccessFunction = SE->getSCEV(Ptr);
440
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000441 if (!isValidAffineFunction(AccessFunction, Context.CurRegion, &BasePtr))
442 INVALID(AffFunc, "Bad memory address " << *AccessFunction);
Tobias Grosser75805372011-04-29 06:27:02 +0000443
444 // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
445 // created by IndependentBlocks Pass.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000446 if (isa<IntToPtrInst>(BasePtr))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000447 INVALID(Other, "Find bad intToptr prt: " << *BasePtr);
Tobias Grosser75805372011-04-29 06:27:02 +0000448
449 // Check if the base pointer of the memory access does alias with
450 // any other pointer. This cannot be handled at the moment.
451 AliasSet &AS =
452 Context.AST.getAliasSetForPointer(BasePtr, AliasAnalysis::UnknownSize,
453 Inst.getMetadata(LLVMContext::MD_tbaa));
454 if (!AS.isMustAlias()) {
455 DEBUG(dbgs() << "Bad pointer alias found:" << *BasePtr << "\nAS:\n" << AS);
456
457 // STATSCOP triggers an assertion if we are in verifying mode.
458 // This is generally good to check that we do not change the SCoP after we
459 // run the SCoP detection and consequently to ensure that we can still
460 // represent that SCoP. However, in case of aliasing this does not work.
461 // The independent blocks pass may create memory references which seem to
462 // alias, if -basicaa is not available. They actually do not. As we do not
463 // not know this and we would fail here if we verify it.
464 if (!Context.Verifying) {
465 STATSCOP(Alias);
466 }
467
468 return false;
469 }
470
471 return true;
472}
473
474
475bool ScopDetection::hasScalarDependency(Instruction &Inst,
476 Region &RefRegion) const {
477 for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
478 UI != UE; ++UI)
479 if (Instruction *Use = dyn_cast<Instruction>(*UI))
480 if (!RefRegion.contains(Use->getParent())) {
481 // DirtyHack 1: PHINode user outside the Scop is not allow, if this
482 // PHINode is induction variable, the scalar to array transform may
483 // break it and introduce a non-indvar PHINode, which is not allow in
484 // Scop.
485 // This can be fix by:
486 // Introduce a IndependentBlockPrepare pass, which translate all
487 // PHINodes not in Scop to array.
488 // The IndependentBlockPrepare pass can also split the entry block of
489 // the function to hold the alloca instruction created by scalar to
490 // array. and split the exit block of the Scop so the new create load
491 // instruction for escape users will not break other Scops.
492 if (isa<PHINode>(Use))
493 return true;
494 }
495
496 return false;
497}
498
499bool ScopDetection::isValidInstruction(Instruction &Inst,
500 DetectionContext &Context) const {
501 // Only canonical IVs are allowed.
502 if (PHINode *PN = dyn_cast<PHINode>(&Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000503 if (!isIndVar(PN, LI))
504 INVALID(IndVar, "Non canonical PHI node: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000505
506 // Scalar dependencies are not allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000507 if (hasScalarDependency(Inst, Context.CurRegion))
508 INVALID(Scalar, "Scalar dependency found: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000509
510 // We only check the call instruction but not invoke instruction.
511 if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
512 if (isValidCallInst(*CI))
513 return true;
514
Tobias Grosserb43ba822011-10-08 00:49:30 +0000515 INVALID(FuncCall, "Call instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000516 }
517
518 if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
519 // Handle cast instruction.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000520 if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000521 INVALID(Other, "Cast instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000522
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000523 if (isa<AllocaInst>(Inst))
Tobias Grosserb43ba822011-10-08 00:49:30 +0000524 INVALID(Other, "Alloca instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000525
526 return true;
527 }
528
529 // Check the access function.
530 if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
531 return isValidMemoryAccess(Inst, Context);
532
533 // We do not know this instruction, therefore we assume it is invalid.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000534 INVALID(Other, "Unknown instruction: " << Inst);
Tobias Grosser75805372011-04-29 06:27:02 +0000535}
536
537bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
538 DetectionContext &Context) const {
539 if (!isValidCFG(BB, Context))
540 return false;
541
542 // Check all instructions, except the terminator instruction.
543 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
544 if (!isValidInstruction(*I, Context))
545 return false;
546
547 Loop *L = LI->getLoopFor(&BB);
548 if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
549 return false;
550
551 return true;
552}
553
554bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
555 PHINode *IndVar = L->getCanonicalInductionVariable();
556 // No canonical induction variable.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000557 if (!IndVar)
Tobias Grosserb43ba822011-10-08 00:49:30 +0000558 INVALID(IndVar, "No canonical IV at loop header: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000559 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000560
561 // Is the loop count affine?
562 const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
Tobias Grosser3fb49922011-11-02 21:40:08 +0000563 if (!SCEVValidator::isValid(&Context.CurRegion, LoopCount, *SE))
Tobias Grosserbd54f322011-10-26 01:27:49 +0000564 INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000565 << L->getHeader()->getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000566
567 return true;
568}
569
570Region *ScopDetection::expandRegion(Region &R) {
571 Region *CurrentRegion = &R;
572 Region *TmpRegion = R.getExpandedRegion();
573
574 DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
575
576 while (TmpRegion) {
577 DetectionContext Context(*TmpRegion, *AA, false /*verifying*/);
578 DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n");
579
580 if (!allBlocksValid(Context))
581 break;
582
583 if (isValidExit(Context)) {
584 if (CurrentRegion != &R)
585 delete CurrentRegion;
586
587 CurrentRegion = TmpRegion;
588 }
589
590 Region *TmpRegion2 = TmpRegion->getExpandedRegion();
591
592 if (TmpRegion != &R && TmpRegion != CurrentRegion)
593 delete TmpRegion;
594
595 TmpRegion = TmpRegion2;
596 }
597
598 if (&R == CurrentRegion)
599 return NULL;
600
601 DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n");
602
603 return CurrentRegion;
604}
605
606
607void ScopDetection::findScops(Region &R) {
608 DetectionContext Context(R, *AA, false /*verifying*/);
609
610 if (isValidRegion(Context)) {
611 ++ValidRegion;
612 ValidRegions.insert(&R);
613 return;
614 }
615
Tobias Grosser4f129a62011-10-08 00:30:55 +0000616 InvalidRegions[&R] = LastFailure;
617
Tobias Grosser75805372011-04-29 06:27:02 +0000618 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
619 findScops(**I);
620
621 // Try to expand regions.
622 //
623 // As the region tree normally only contains canonical regions, non canonical
624 // regions that form a Scop are not found. Therefore, those non canonical
625 // regions are checked by expanding the canonical ones.
626
627 std::vector<Region*> ToExpand;
628
629 for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
630 ToExpand.push_back(*I);
631
632 for (std::vector<Region*>::iterator RI = ToExpand.begin(),
633 RE = ToExpand.end(); RI != RE; ++RI) {
634 Region *CurrentRegion = *RI;
635
636 // Skip invalid regions. Regions may become invalid, if they are element of
637 // an already expanded region.
638 if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
639 continue;
640
641 Region *ExpandedR = expandRegion(*CurrentRegion);
642
643 if (!ExpandedR)
644 continue;
645
646 R.addSubRegion(ExpandedR, true);
647 ValidRegions.insert(ExpandedR);
648 ValidRegions.erase(CurrentRegion);
649
650 for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
651 ++I)
652 ValidRegions.erase(*I);
653 }
654}
655
656bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
657 Region &R = Context.CurRegion;
658
659 for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
660 ++I)
661 if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context))
662 return false;
663
664 return true;
665}
666
667bool ScopDetection::isValidExit(DetectionContext &Context) const {
668 Region &R = Context.CurRegion;
669
670 // PHI nodes are not allowed in the exit basic block.
671 if (BasicBlock *Exit = R.getExit()) {
672 BasicBlock::iterator I = Exit->begin();
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000673 if (I != Exit->end() && isa<PHINode> (*I))
674 INVALID(Other, "PHI node in exit BB");
Tobias Grosser75805372011-04-29 06:27:02 +0000675 }
676
677 return true;
678}
679
680bool ScopDetection::isValidRegion(DetectionContext &Context) const {
681 Region &R = Context.CurRegion;
682
683 DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
684
685 // The toplevel region is no valid region.
686 if (!R.getParent()) {
687 DEBUG(dbgs() << "Top level region is invalid";
688 dbgs() << "\n");
689 return false;
690 }
691
692 // SCoP can not contains the entry block of the function, because we need
693 // to insert alloca instruction there when translate scalar to array.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000694 if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
695 INVALID(Other, "Region containing entry block of function is invalid!");
Tobias Grosser75805372011-04-29 06:27:02 +0000696
697 // Only a simple region is allowed.
Tobias Grosserc4a0bd12011-10-08 00:30:48 +0000698 if (!R.isSimple())
699 INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
Tobias Grosser75805372011-04-29 06:27:02 +0000700
701 if (!allBlocksValid(Context))
702 return false;
703
704 if (!isValidExit(Context))
705 return false;
706
707 DEBUG(dbgs() << "OK\n");
708 return true;
709}
710
711bool ScopDetection::isValidFunction(llvm::Function &F) {
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000712 return !InvalidFunctions.count(&F);
Tobias Grosser75805372011-04-29 06:27:02 +0000713}
714
715bool ScopDetection::runOnFunction(llvm::Function &F) {
716 AA = &getAnalysis<AliasAnalysis>();
717 SE = &getAnalysis<ScalarEvolution>();
718 LI = &getAnalysis<LoopInfo>();
719 RI = &getAnalysis<RegionInfo>();
720 Region *TopRegion = RI->getTopLevelRegion();
721
Tobias Grosser2ff87232011-10-23 11:17:06 +0000722 releaseMemory();
723
724 if (OnlyFunction != "" && F.getNameStr() != OnlyFunction)
725 return false;
726
Tobias Grosser75805372011-04-29 06:27:02 +0000727 if(!isValidFunction(F))
728 return false;
729
730 findScops(*TopRegion);
731 return false;
732}
733
734
735void polly::ScopDetection::verifyRegion(const Region &R) const {
736 assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
737 DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
738 isValidRegion(Context);
739}
740
741void polly::ScopDetection::verifyAnalysis() const {
742 for (RegionSet::const_iterator I = ValidRegions.begin(),
743 E = ValidRegions.end(); I != E; ++I)
744 verifyRegion(**I);
745}
746
747void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
748 AU.addRequired<DominatorTree>();
749 AU.addRequired<PostDominatorTree>();
750 AU.addRequired<LoopInfo>();
751 AU.addRequired<ScalarEvolution>();
752 // We also need AA and RegionInfo when we are verifying analysis.
753 AU.addRequiredTransitive<AliasAnalysis>();
754 AU.addRequiredTransitive<RegionInfo>();
755 AU.setPreservesAll();
756}
757
758void ScopDetection::print(raw_ostream &OS, const Module *) const {
759 for (RegionSet::const_iterator I = ValidRegions.begin(),
760 E = ValidRegions.end(); I != E; ++I)
761 OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
762
763 OS << "\n";
764}
765
766void ScopDetection::releaseMemory() {
767 ValidRegions.clear();
Tobias Grosser4f129a62011-10-08 00:30:55 +0000768 InvalidRegions.clear();
Hongbin Zheng94c5df12011-05-06 02:38:20 +0000769 // Do not clear the invalid function set.
Tobias Grosser75805372011-04-29 06:27:02 +0000770}
771
772char ScopDetection::ID = 0;
773
Tobias Grosser73600b82011-10-08 00:30:40 +0000774INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
775 "Polly - Detect static control parts (SCoPs)", false,
776 false)
777INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
778INITIALIZE_PASS_DEPENDENCY(DominatorTree)
779INITIALIZE_PASS_DEPENDENCY(LoopInfo)
780INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
781INITIALIZE_PASS_DEPENDENCY(RegionInfo)
782INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
783INITIALIZE_PASS_END(ScopDetection, "polly-detect",
784 "Polly - Detect static control parts (SCoPs)", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000785
Tobias Grosser83f5c432011-08-23 22:35:08 +0000786Pass *polly::createScopDetectionPass() {
787 return new ScopDetection();
788}