Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===----- 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" |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame^] | 51 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 52 | #include "polly/Support/AffineSCEVIterator.h" |
| 53 | |
| 54 | #include "llvm/LLVMContext.h" |
| 55 | #include "llvm/ADT/Statistic.h" |
| 56 | #include "llvm/Analysis/AliasAnalysis.h" |
| 57 | #include "llvm/Analysis/RegionIterator.h" |
| 58 | #include "llvm/Support/CommandLine.h" |
| 59 | #include "llvm/Assembly/Writer.h" |
| 60 | |
| 61 | #define DEBUG_TYPE "polly-detect" |
| 62 | #include "llvm/Support/Debug.h" |
| 63 | |
| 64 | using namespace llvm; |
| 65 | using namespace polly; |
| 66 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 67 | static cl::opt<std::string> |
| 68 | OnlyFunction("polly-detect-only", |
| 69 | cl::desc("Only detect scops in function"), cl::Hidden, |
| 70 | cl::value_desc("The function name to detect scops in"), |
| 71 | cl::ValueRequired, cl::init("")); |
| 72 | |
| 73 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // Statistics. |
| 76 | |
| 77 | STATISTIC(ValidRegion, "Number of regions that a valid part of Scop"); |
| 78 | |
| 79 | #define BADSCOP_STAT(NAME, DESC) STATISTIC(Bad##NAME##ForScop, \ |
| 80 | "Number of bad regions for Scop: "\ |
| 81 | DESC) |
| 82 | |
| 83 | #define STATSCOP(NAME); assert(!Context.Verifying && #NAME); \ |
| 84 | if (!Context.Verifying) ++Bad##NAME##ForScop; |
| 85 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 86 | #define INVALID(NAME, MESSAGE) \ |
| 87 | do { \ |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 88 | std::string Buf; \ |
| 89 | raw_string_ostream fmt(Buf); \ |
| 90 | fmt << MESSAGE; \ |
| 91 | fmt.flush(); \ |
| 92 | LastFailure = Buf; \ |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 93 | DEBUG(dbgs() << MESSAGE); \ |
| 94 | DEBUG(dbgs() << "\n"); \ |
| 95 | STATSCOP(NAME); \ |
| 96 | return false; \ |
| 97 | } while (0); |
| 98 | |
| 99 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 100 | BADSCOP_STAT(CFG, "CFG too complex"); |
| 101 | BADSCOP_STAT(IndVar, "Non canonical induction variable in loop"); |
| 102 | BADSCOP_STAT(LoopBound, "Loop bounds can not be computed"); |
| 103 | BADSCOP_STAT(FuncCall, "Function call with side effects appeared"); |
| 104 | BADSCOP_STAT(AffFunc, "Expression not affine"); |
| 105 | BADSCOP_STAT(Scalar, "Found scalar dependency"); |
| 106 | BADSCOP_STAT(Alias, "Found base address alias"); |
| 107 | BADSCOP_STAT(SimpleRegion, "Region not simple"); |
| 108 | BADSCOP_STAT(Other, "Others"); |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // ScopDetection. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 112 | bool ScopDetection::isMaxRegionInScop(const Region &R) const { |
| 113 | // The Region is valid only if it could be found in the set. |
| 114 | return ValidRegions.count(&R); |
| 115 | } |
| 116 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 117 | std::string ScopDetection::regionIsInvalidBecause(const Region *R) const { |
| 118 | if (!InvalidRegions.count(R)) |
| 119 | return ""; |
| 120 | |
| 121 | return InvalidRegions.find(R)->second; |
| 122 | } |
| 123 | |
| 124 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 125 | bool ScopDetection::isValidAffineFunction(const SCEV *S, Region &RefRegion, |
| 126 | Value **BasePtr) const { |
| 127 | assert(S && "S must not be null!"); |
| 128 | bool isMemoryAccess = (BasePtr != 0); |
| 129 | if (isMemoryAccess) *BasePtr = 0; |
| 130 | DEBUG(dbgs() << "Checking " << *S << " ... "); |
| 131 | |
| 132 | if (isa<SCEVCouldNotCompute>(S)) { |
| 133 | DEBUG(dbgs() << "Non Affine: SCEV could not be computed\n"); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | for (AffineSCEVIterator I = affine_begin(S, SE), E = affine_end(); I != E; |
| 138 | ++I) { |
| 139 | // The constant part must be a SCEVConstant. |
| 140 | // TODO: support sizeof in coefficient. |
| 141 | if (!isa<SCEVConstant>(I->second)) { |
| 142 | DEBUG(dbgs() << "Non Affine: Right hand side is not constant\n"); |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | const SCEV *Var = I->first; |
| 147 | |
| 148 | // A constant offset is affine. |
| 149 | if(isa<SCEVConstant>(Var)) |
| 150 | continue; |
| 151 | |
| 152 | // Memory accesses are allowed to have a base pointer. |
| 153 | if (Var->getType()->isPointerTy()) { |
| 154 | if (!isMemoryAccess) { |
| 155 | DEBUG(dbgs() << "Non Affine: Pointer in non memory access\n"); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | assert(I->second->isOne() && "Only one as pointer coefficient allowed.\n"); |
| 160 | const SCEVUnknown *BaseAddr = dyn_cast<SCEVUnknown>(Var); |
| 161 | |
| 162 | if (!BaseAddr || isa<UndefValue>(BaseAddr->getValue())){ |
| 163 | DEBUG(dbgs() << "Cannot handle base: " << *Var << "\n"); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // BaseAddr must be invariant in Scop. |
| 168 | if (!isParameter(BaseAddr, RefRegion, *LI, *SE)) { |
| 169 | DEBUG(dbgs() << "Non Affine: Base address not invariant in SCoP\n"); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | assert(*BasePtr == 0 && "Found second base pointer.\n"); |
| 174 | *BasePtr = BaseAddr->getValue(); |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | if (isParameter(Var, RefRegion, *LI, *SE) |
| 179 | || isIndVar(Var, RefRegion, *LI, *SE)) |
| 180 | continue; |
| 181 | |
| 182 | DEBUG(dbgs() << "Non Affine: " ; |
| 183 | Var->print(dbgs()); |
| 184 | dbgs() << " is neither parameter nor induction variable\n"); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | DEBUG(dbgs() << " is affine.\n"); |
| 189 | return !isMemoryAccess || (*BasePtr != 0); |
| 190 | } |
| 191 | |
| 192 | bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const |
| 193 | { |
| 194 | Region &RefRegion = Context.CurRegion; |
| 195 | TerminatorInst *TI = BB.getTerminator(); |
| 196 | |
| 197 | // Return instructions are only valid if the region is the top level region. |
| 198 | if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0) |
| 199 | return true; |
| 200 | |
| 201 | BranchInst *Br = dyn_cast<BranchInst>(TI); |
| 202 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 203 | if (!Br) |
| 204 | INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 205 | |
| 206 | if (Br->isUnconditional()) return true; |
| 207 | |
| 208 | Value *Condition = Br->getCondition(); |
| 209 | |
| 210 | // UndefValue is not allowed as condition. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 211 | if (isa<UndefValue>(Condition)) |
| 212 | INVALID(AffFunc, "Condition based on 'undef' value in BB: " |
| 213 | + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 214 | |
| 215 | // Only Constant and ICmpInst are allowed as condition. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 216 | if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) |
| 217 | INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither " |
| 218 | "constant nor an icmp instruction"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 219 | |
| 220 | // Allow perfectly nested conditions. |
| 221 | assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors"); |
| 222 | |
| 223 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) { |
| 224 | // Unsigned comparisons are not allowed. They trigger overflow problems |
| 225 | // in the code generation. |
| 226 | // |
| 227 | // TODO: This is not sufficient and just hides bugs. However it does pretty |
| 228 | // well. |
| 229 | if(ICmp->isUnsigned()) |
| 230 | return false; |
| 231 | |
| 232 | // Are both operands of the ICmp affine? |
| 233 | if (isa<UndefValue>(ICmp->getOperand(0)) |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 234 | || isa<UndefValue>(ICmp->getOperand(1))) |
| 235 | INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 236 | |
| 237 | const SCEV *ScevLHS = SE->getSCEV(ICmp->getOperand(0)); |
| 238 | const SCEV *ScevRHS = SE->getSCEV(ICmp->getOperand(1)); |
| 239 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame^] | 240 | bool affineLHS = isAffineExpr(&Context.CurRegion, ScevLHS, *SE); |
| 241 | bool affineRHS = isAffineExpr(&Context.CurRegion, ScevRHS, *SE); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 242 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 243 | if (!affineLHS || !affineRHS) |
| 244 | INVALID(AffFunc, "Non affine branch in BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Allow loop exit conditions. |
| 248 | Loop *L = LI->getLoopFor(&BB); |
| 249 | if (L && L->getExitingBlock() == &BB) |
| 250 | return true; |
| 251 | |
| 252 | // Allow perfectly nested conditions. |
| 253 | Region *R = RI->getRegionFor(&BB); |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 254 | if (R->getEntry() != &BB) |
| 255 | INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | bool ScopDetection::isValidCallInst(CallInst &CI) { |
| 261 | if (CI.mayHaveSideEffects() || CI.doesNotReturn()) |
| 262 | return false; |
| 263 | |
| 264 | if (CI.doesNotAccessMemory()) |
| 265 | return true; |
| 266 | |
| 267 | Function *CalledFunction = CI.getCalledFunction(); |
| 268 | |
| 269 | // Indirect calls are not supported. |
| 270 | if (CalledFunction == 0) |
| 271 | return false; |
| 272 | |
| 273 | // TODO: Intrinsics. |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | bool ScopDetection::isValidMemoryAccess(Instruction &Inst, |
| 278 | DetectionContext &Context) const { |
| 279 | Value *Ptr = getPointerOperand(Inst), *BasePtr; |
| 280 | const SCEV *AccessFunction = SE->getSCEV(Ptr); |
| 281 | |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame^] | 282 | if (!isAffineExpr(&Context.CurRegion, AccessFunction, *SE, &BasePtr)) |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 283 | INVALID(AffFunc, "Bad memory address " << *AccessFunction); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 284 | |
Tobias Grosser | 7616467 | 2011-11-03 21:03:18 +0000 | [diff] [blame] | 285 | // FIXME: Also check with isValidAffineFunction, as for the moment it is |
| 286 | // protecting us to fail because of not supported features in TempScop. |
| 287 | // As soon as TempScop is fixed, this needs to be removed. |
| 288 | if (!isValidAffineFunction(AccessFunction, Context.CurRegion, &BasePtr)) |
| 289 | INVALID(AffFunc, "Access not supported in TempScop" << *AccessFunction); |
| 290 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 291 | // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions |
| 292 | // created by IndependentBlocks Pass. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 293 | if (isa<IntToPtrInst>(BasePtr)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 294 | INVALID(Other, "Find bad intToptr prt: " << *BasePtr); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 295 | |
| 296 | // Check if the base pointer of the memory access does alias with |
| 297 | // any other pointer. This cannot be handled at the moment. |
| 298 | AliasSet &AS = |
| 299 | Context.AST.getAliasSetForPointer(BasePtr, AliasAnalysis::UnknownSize, |
| 300 | Inst.getMetadata(LLVMContext::MD_tbaa)); |
| 301 | if (!AS.isMustAlias()) { |
| 302 | DEBUG(dbgs() << "Bad pointer alias found:" << *BasePtr << "\nAS:\n" << AS); |
| 303 | |
| 304 | // STATSCOP triggers an assertion if we are in verifying mode. |
| 305 | // This is generally good to check that we do not change the SCoP after we |
| 306 | // run the SCoP detection and consequently to ensure that we can still |
| 307 | // represent that SCoP. However, in case of aliasing this does not work. |
| 308 | // The independent blocks pass may create memory references which seem to |
| 309 | // alias, if -basicaa is not available. They actually do not. As we do not |
| 310 | // not know this and we would fail here if we verify it. |
| 311 | if (!Context.Verifying) { |
| 312 | STATSCOP(Alias); |
| 313 | } |
| 314 | |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | return true; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | bool ScopDetection::hasScalarDependency(Instruction &Inst, |
| 323 | Region &RefRegion) const { |
| 324 | for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end(); |
| 325 | UI != UE; ++UI) |
| 326 | if (Instruction *Use = dyn_cast<Instruction>(*UI)) |
| 327 | if (!RefRegion.contains(Use->getParent())) { |
| 328 | // DirtyHack 1: PHINode user outside the Scop is not allow, if this |
| 329 | // PHINode is induction variable, the scalar to array transform may |
| 330 | // break it and introduce a non-indvar PHINode, which is not allow in |
| 331 | // Scop. |
| 332 | // This can be fix by: |
| 333 | // Introduce a IndependentBlockPrepare pass, which translate all |
| 334 | // PHINodes not in Scop to array. |
| 335 | // The IndependentBlockPrepare pass can also split the entry block of |
| 336 | // the function to hold the alloca instruction created by scalar to |
| 337 | // array. and split the exit block of the Scop so the new create load |
| 338 | // instruction for escape users will not break other Scops. |
| 339 | if (isa<PHINode>(Use)) |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 347 | DetectionContext &Context) const { |
| 348 | // Only canonical IVs are allowed. |
| 349 | if (PHINode *PN = dyn_cast<PHINode>(&Inst)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 350 | if (!isIndVar(PN, LI)) |
| 351 | INVALID(IndVar, "Non canonical PHI node: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 352 | |
| 353 | // Scalar dependencies are not allowed. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 354 | if (hasScalarDependency(Inst, Context.CurRegion)) |
| 355 | INVALID(Scalar, "Scalar dependency found: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 356 | |
| 357 | // We only check the call instruction but not invoke instruction. |
| 358 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
| 359 | if (isValidCallInst(*CI)) |
| 360 | return true; |
| 361 | |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 362 | INVALID(FuncCall, "Call instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) { |
| 366 | // Handle cast instruction. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 367 | if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 368 | INVALID(Other, "Cast instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 369 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 370 | if (isa<AllocaInst>(Inst)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 371 | INVALID(Other, "Alloca instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | // Check the access function. |
| 377 | if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) |
| 378 | return isValidMemoryAccess(Inst, Context); |
| 379 | |
| 380 | // We do not know this instruction, therefore we assume it is invalid. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 381 | INVALID(Other, "Unknown instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | bool ScopDetection::isValidBasicBlock(BasicBlock &BB, |
| 385 | DetectionContext &Context) const { |
| 386 | if (!isValidCFG(BB, Context)) |
| 387 | return false; |
| 388 | |
| 389 | // Check all instructions, except the terminator instruction. |
| 390 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 391 | if (!isValidInstruction(*I, Context)) |
| 392 | return false; |
| 393 | |
| 394 | Loop *L = LI->getLoopFor(&BB); |
| 395 | if (L && L->getHeader() == &BB && !isValidLoop(L, Context)) |
| 396 | return false; |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
| 402 | PHINode *IndVar = L->getCanonicalInductionVariable(); |
| 403 | // No canonical induction variable. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 404 | if (!IndVar) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 405 | INVALID(IndVar, "No canonical IV at loop header: " |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 406 | << L->getHeader()->getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 407 | |
| 408 | // Is the loop count affine? |
| 409 | const SCEV *LoopCount = SE->getBackedgeTakenCount(L); |
Tobias Grosser | 120db6b | 2011-11-07 12:58:54 +0000 | [diff] [blame^] | 410 | if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE)) |
Tobias Grosser | bd54f32 | 2011-10-26 01:27:49 +0000 | [diff] [blame] | 411 | INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: " |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 412 | << L->getHeader()->getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | Region *ScopDetection::expandRegion(Region &R) { |
| 418 | Region *CurrentRegion = &R; |
| 419 | Region *TmpRegion = R.getExpandedRegion(); |
| 420 | |
| 421 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 422 | |
| 423 | while (TmpRegion) { |
| 424 | DetectionContext Context(*TmpRegion, *AA, false /*verifying*/); |
| 425 | DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n"); |
| 426 | |
| 427 | if (!allBlocksValid(Context)) |
| 428 | break; |
| 429 | |
| 430 | if (isValidExit(Context)) { |
| 431 | if (CurrentRegion != &R) |
| 432 | delete CurrentRegion; |
| 433 | |
| 434 | CurrentRegion = TmpRegion; |
| 435 | } |
| 436 | |
| 437 | Region *TmpRegion2 = TmpRegion->getExpandedRegion(); |
| 438 | |
| 439 | if (TmpRegion != &R && TmpRegion != CurrentRegion) |
| 440 | delete TmpRegion; |
| 441 | |
| 442 | TmpRegion = TmpRegion2; |
| 443 | } |
| 444 | |
| 445 | if (&R == CurrentRegion) |
| 446 | return NULL; |
| 447 | |
| 448 | DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n"); |
| 449 | |
| 450 | return CurrentRegion; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | void ScopDetection::findScops(Region &R) { |
| 455 | DetectionContext Context(R, *AA, false /*verifying*/); |
| 456 | |
| 457 | if (isValidRegion(Context)) { |
| 458 | ++ValidRegion; |
| 459 | ValidRegions.insert(&R); |
| 460 | return; |
| 461 | } |
| 462 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 463 | InvalidRegions[&R] = LastFailure; |
| 464 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 465 | for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 466 | findScops(**I); |
| 467 | |
| 468 | // Try to expand regions. |
| 469 | // |
| 470 | // As the region tree normally only contains canonical regions, non canonical |
| 471 | // regions that form a Scop are not found. Therefore, those non canonical |
| 472 | // regions are checked by expanding the canonical ones. |
| 473 | |
| 474 | std::vector<Region*> ToExpand; |
| 475 | |
| 476 | for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 477 | ToExpand.push_back(*I); |
| 478 | |
| 479 | for (std::vector<Region*>::iterator RI = ToExpand.begin(), |
| 480 | RE = ToExpand.end(); RI != RE; ++RI) { |
| 481 | Region *CurrentRegion = *RI; |
| 482 | |
| 483 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 484 | // an already expanded region. |
| 485 | if (ValidRegions.find(CurrentRegion) == ValidRegions.end()) |
| 486 | continue; |
| 487 | |
| 488 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 489 | |
| 490 | if (!ExpandedR) |
| 491 | continue; |
| 492 | |
| 493 | R.addSubRegion(ExpandedR, true); |
| 494 | ValidRegions.insert(ExpandedR); |
| 495 | ValidRegions.erase(CurrentRegion); |
| 496 | |
| 497 | for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E; |
| 498 | ++I) |
| 499 | ValidRegions.erase(*I); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
| 504 | Region &R = Context.CurRegion; |
| 505 | |
| 506 | for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E; |
| 507 | ++I) |
| 508 | if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context)) |
| 509 | return false; |
| 510 | |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | bool ScopDetection::isValidExit(DetectionContext &Context) const { |
| 515 | Region &R = Context.CurRegion; |
| 516 | |
| 517 | // PHI nodes are not allowed in the exit basic block. |
| 518 | if (BasicBlock *Exit = R.getExit()) { |
| 519 | BasicBlock::iterator I = Exit->begin(); |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 520 | if (I != Exit->end() && isa<PHINode> (*I)) |
| 521 | INVALID(Other, "PHI node in exit BB"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
| 528 | Region &R = Context.CurRegion; |
| 529 | |
| 530 | DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t"); |
| 531 | |
| 532 | // The toplevel region is no valid region. |
| 533 | if (!R.getParent()) { |
| 534 | DEBUG(dbgs() << "Top level region is invalid"; |
| 535 | dbgs() << "\n"); |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | // SCoP can not contains the entry block of the function, because we need |
| 540 | // to insert alloca instruction there when translate scalar to array. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 541 | if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock())) |
| 542 | INVALID(Other, "Region containing entry block of function is invalid!"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 543 | |
| 544 | // Only a simple region is allowed. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 545 | if (!R.isSimple()) |
| 546 | INVALID(SimpleRegion, "Region not simple: " << R.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 547 | |
| 548 | if (!allBlocksValid(Context)) |
| 549 | return false; |
| 550 | |
| 551 | if (!isValidExit(Context)) |
| 552 | return false; |
| 553 | |
| 554 | DEBUG(dbgs() << "OK\n"); |
| 555 | return true; |
| 556 | } |
| 557 | |
| 558 | bool ScopDetection::isValidFunction(llvm::Function &F) { |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 559 | return !InvalidFunctions.count(&F); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | bool ScopDetection::runOnFunction(llvm::Function &F) { |
| 563 | AA = &getAnalysis<AliasAnalysis>(); |
| 564 | SE = &getAnalysis<ScalarEvolution>(); |
| 565 | LI = &getAnalysis<LoopInfo>(); |
| 566 | RI = &getAnalysis<RegionInfo>(); |
| 567 | Region *TopRegion = RI->getTopLevelRegion(); |
| 568 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 569 | releaseMemory(); |
| 570 | |
| 571 | if (OnlyFunction != "" && F.getNameStr() != OnlyFunction) |
| 572 | return false; |
| 573 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 574 | if(!isValidFunction(F)) |
| 575 | return false; |
| 576 | |
| 577 | findScops(*TopRegion); |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | void polly::ScopDetection::verifyRegion(const Region &R) const { |
| 583 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
| 584 | DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/); |
| 585 | isValidRegion(Context); |
| 586 | } |
| 587 | |
| 588 | void polly::ScopDetection::verifyAnalysis() const { |
| 589 | for (RegionSet::const_iterator I = ValidRegions.begin(), |
| 590 | E = ValidRegions.end(); I != E; ++I) |
| 591 | verifyRegion(**I); |
| 592 | } |
| 593 | |
| 594 | void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const { |
| 595 | AU.addRequired<DominatorTree>(); |
| 596 | AU.addRequired<PostDominatorTree>(); |
| 597 | AU.addRequired<LoopInfo>(); |
| 598 | AU.addRequired<ScalarEvolution>(); |
| 599 | // We also need AA and RegionInfo when we are verifying analysis. |
| 600 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 601 | AU.addRequiredTransitive<RegionInfo>(); |
| 602 | AU.setPreservesAll(); |
| 603 | } |
| 604 | |
| 605 | void ScopDetection::print(raw_ostream &OS, const Module *) const { |
| 606 | for (RegionSet::const_iterator I = ValidRegions.begin(), |
| 607 | E = ValidRegions.end(); I != E; ++I) |
| 608 | OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n'; |
| 609 | |
| 610 | OS << "\n"; |
| 611 | } |
| 612 | |
| 613 | void ScopDetection::releaseMemory() { |
| 614 | ValidRegions.clear(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 615 | InvalidRegions.clear(); |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 616 | // Do not clear the invalid function set. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | char ScopDetection::ID = 0; |
| 620 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 621 | INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect", |
| 622 | "Polly - Detect static control parts (SCoPs)", false, |
| 623 | false) |
| 624 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 625 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 626 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 627 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTree) |
| 628 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 629 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 630 | INITIALIZE_PASS_END(ScopDetection, "polly-detect", |
| 631 | "Polly - Detect static control parts (SCoPs)", false, false) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 632 | |
Tobias Grosser | 83f5c43 | 2011-08-23 22:35:08 +0000 | [diff] [blame] | 633 | Pass *polly::createScopDetectionPass() { |
| 634 | return new ScopDetection(); |
| 635 | } |