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" |
| 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 | |
| 63 | using namespace llvm; |
| 64 | using namespace polly; |
| 65 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 66 | static cl::opt<std::string> |
| 67 | OnlyFunction("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 Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 73 | //===----------------------------------------------------------------------===// |
| 74 | // Statistics. |
| 75 | |
| 76 | STATISTIC(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 Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 85 | #define INVALID(NAME, MESSAGE) \ |
| 86 | do { \ |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 87 | std::string Buf; \ |
| 88 | raw_string_ostream fmt(Buf); \ |
| 89 | fmt << MESSAGE; \ |
| 90 | fmt.flush(); \ |
| 91 | LastFailure = Buf; \ |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 92 | DEBUG(dbgs() << MESSAGE); \ |
| 93 | DEBUG(dbgs() << "\n"); \ |
| 94 | STATSCOP(NAME); \ |
| 95 | return false; \ |
| 96 | } while (0); |
| 97 | |
| 98 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 99 | BADSCOP_STAT(CFG, "CFG too complex"); |
| 100 | BADSCOP_STAT(IndVar, "Non canonical induction variable in loop"); |
| 101 | BADSCOP_STAT(LoopBound, "Loop bounds can not be computed"); |
| 102 | BADSCOP_STAT(FuncCall, "Function call with side effects appeared"); |
| 103 | BADSCOP_STAT(AffFunc, "Expression not affine"); |
| 104 | BADSCOP_STAT(Scalar, "Found scalar dependency"); |
| 105 | BADSCOP_STAT(Alias, "Found base address alias"); |
| 106 | BADSCOP_STAT(SimpleRegion, "Region not simple"); |
| 107 | BADSCOP_STAT(Other, "Others"); |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // ScopDetection. |
| 111 | |
Tobias Grosser | 3fb4992 | 2011-11-02 21:40:08 +0000 | [diff] [blame^] | 112 | namespace SCEVType { |
| 113 | enum TYPE {INT, PARAM, IV, INVALID}; |
| 114 | } |
| 115 | |
| 116 | /// Check if a SCEV is valid in a SCoP. |
| 117 | struct SCEVValidator : public SCEVVisitor<SCEVValidator, SCEVType::TYPE> { |
| 118 | private: |
| 119 | const Region *R; |
| 120 | ScalarEvolution &SE; |
| 121 | const Value **BaseAddress; |
| 122 | |
| 123 | public: |
| 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) { |
| 264 | return SCEVType::PARAM; |
| 265 | } |
| 266 | }; |
| 267 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 268 | bool ScopDetection::isMaxRegionInScop(const Region &R) const { |
| 269 | // The Region is valid only if it could be found in the set. |
| 270 | return ValidRegions.count(&R); |
| 271 | } |
| 272 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 273 | std::string ScopDetection::regionIsInvalidBecause(const Region *R) const { |
| 274 | if (!InvalidRegions.count(R)) |
| 275 | return ""; |
| 276 | |
| 277 | return InvalidRegions.find(R)->second; |
| 278 | } |
| 279 | |
| 280 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 281 | bool ScopDetection::isValidAffineFunction(const SCEV *S, Region &RefRegion, |
| 282 | Value **BasePtr) const { |
| 283 | assert(S && "S must not be null!"); |
| 284 | bool isMemoryAccess = (BasePtr != 0); |
| 285 | if (isMemoryAccess) *BasePtr = 0; |
| 286 | DEBUG(dbgs() << "Checking " << *S << " ... "); |
| 287 | |
| 288 | if (isa<SCEVCouldNotCompute>(S)) { |
| 289 | DEBUG(dbgs() << "Non Affine: SCEV could not be computed\n"); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | for (AffineSCEVIterator I = affine_begin(S, SE), E = affine_end(); I != E; |
| 294 | ++I) { |
| 295 | // The constant part must be a SCEVConstant. |
| 296 | // TODO: support sizeof in coefficient. |
| 297 | if (!isa<SCEVConstant>(I->second)) { |
| 298 | DEBUG(dbgs() << "Non Affine: Right hand side is not constant\n"); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | const SCEV *Var = I->first; |
| 303 | |
| 304 | // A constant offset is affine. |
| 305 | if(isa<SCEVConstant>(Var)) |
| 306 | continue; |
| 307 | |
| 308 | // Memory accesses are allowed to have a base pointer. |
| 309 | if (Var->getType()->isPointerTy()) { |
| 310 | if (!isMemoryAccess) { |
| 311 | DEBUG(dbgs() << "Non Affine: Pointer in non memory access\n"); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | assert(I->second->isOne() && "Only one as pointer coefficient allowed.\n"); |
| 316 | const SCEVUnknown *BaseAddr = dyn_cast<SCEVUnknown>(Var); |
| 317 | |
| 318 | if (!BaseAddr || isa<UndefValue>(BaseAddr->getValue())){ |
| 319 | DEBUG(dbgs() << "Cannot handle base: " << *Var << "\n"); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | // BaseAddr must be invariant in Scop. |
| 324 | if (!isParameter(BaseAddr, RefRegion, *LI, *SE)) { |
| 325 | DEBUG(dbgs() << "Non Affine: Base address not invariant in SCoP\n"); |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | assert(*BasePtr == 0 && "Found second base pointer.\n"); |
| 330 | *BasePtr = BaseAddr->getValue(); |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | if (isParameter(Var, RefRegion, *LI, *SE) |
| 335 | || isIndVar(Var, RefRegion, *LI, *SE)) |
| 336 | continue; |
| 337 | |
| 338 | DEBUG(dbgs() << "Non Affine: " ; |
| 339 | Var->print(dbgs()); |
| 340 | dbgs() << " is neither parameter nor induction variable\n"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | DEBUG(dbgs() << " is affine.\n"); |
| 345 | return !isMemoryAccess || (*BasePtr != 0); |
| 346 | } |
| 347 | |
| 348 | bool ScopDetection::isValidCFG(BasicBlock &BB, DetectionContext &Context) const |
| 349 | { |
| 350 | Region &RefRegion = Context.CurRegion; |
| 351 | TerminatorInst *TI = BB.getTerminator(); |
| 352 | |
| 353 | // Return instructions are only valid if the region is the top level region. |
| 354 | if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0) |
| 355 | return true; |
| 356 | |
| 357 | BranchInst *Br = dyn_cast<BranchInst>(TI); |
| 358 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 359 | if (!Br) |
| 360 | INVALID(CFG, "Non branch instruction terminates BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 361 | |
| 362 | if (Br->isUnconditional()) return true; |
| 363 | |
| 364 | Value *Condition = Br->getCondition(); |
| 365 | |
| 366 | // UndefValue is not allowed as condition. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 367 | if (isa<UndefValue>(Condition)) |
| 368 | INVALID(AffFunc, "Condition based on 'undef' value in BB: " |
| 369 | + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 370 | |
| 371 | // Only Constant and ICmpInst are allowed as condition. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 372 | if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition))) |
| 373 | INVALID(AffFunc, "Condition in BB '" + BB.getNameStr() + "' neither " |
| 374 | "constant nor an icmp instruction"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 375 | |
| 376 | // Allow perfectly nested conditions. |
| 377 | assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors"); |
| 378 | |
| 379 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) { |
| 380 | // Unsigned comparisons are not allowed. They trigger overflow problems |
| 381 | // in the code generation. |
| 382 | // |
| 383 | // TODO: This is not sufficient and just hides bugs. However it does pretty |
| 384 | // well. |
| 385 | if(ICmp->isUnsigned()) |
| 386 | return false; |
| 387 | |
| 388 | // Are both operands of the ICmp affine? |
| 389 | if (isa<UndefValue>(ICmp->getOperand(0)) |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 390 | || isa<UndefValue>(ICmp->getOperand(1))) |
| 391 | INVALID(AffFunc, "undef operand in branch at BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 392 | |
| 393 | const SCEV *ScevLHS = SE->getSCEV(ICmp->getOperand(0)); |
| 394 | const SCEV *ScevRHS = SE->getSCEV(ICmp->getOperand(1)); |
| 395 | |
| 396 | bool affineLHS = isValidAffineFunction(ScevLHS, RefRegion); |
| 397 | bool affineRHS = isValidAffineFunction(ScevRHS, RefRegion); |
| 398 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 399 | if (!affineLHS || !affineRHS) |
| 400 | INVALID(AffFunc, "Non affine branch in BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Allow loop exit conditions. |
| 404 | Loop *L = LI->getLoopFor(&BB); |
| 405 | if (L && L->getExitingBlock() == &BB) |
| 406 | return true; |
| 407 | |
| 408 | // Allow perfectly nested conditions. |
| 409 | Region *R = RI->getRegionFor(&BB); |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 410 | if (R->getEntry() != &BB) |
| 411 | INVALID(CFG, "Not well structured condition at BB: " + BB.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | bool ScopDetection::isValidCallInst(CallInst &CI) { |
| 417 | if (CI.mayHaveSideEffects() || CI.doesNotReturn()) |
| 418 | return false; |
| 419 | |
| 420 | if (CI.doesNotAccessMemory()) |
| 421 | return true; |
| 422 | |
| 423 | Function *CalledFunction = CI.getCalledFunction(); |
| 424 | |
| 425 | // Indirect calls are not supported. |
| 426 | if (CalledFunction == 0) |
| 427 | return false; |
| 428 | |
| 429 | // TODO: Intrinsics. |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | bool ScopDetection::isValidMemoryAccess(Instruction &Inst, |
| 434 | DetectionContext &Context) const { |
| 435 | Value *Ptr = getPointerOperand(Inst), *BasePtr; |
| 436 | const SCEV *AccessFunction = SE->getSCEV(Ptr); |
| 437 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 438 | if (!isValidAffineFunction(AccessFunction, Context.CurRegion, &BasePtr)) |
| 439 | INVALID(AffFunc, "Bad memory address " << *AccessFunction); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 440 | |
| 441 | // FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions |
| 442 | // created by IndependentBlocks Pass. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 443 | if (isa<IntToPtrInst>(BasePtr)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 444 | INVALID(Other, "Find bad intToptr prt: " << *BasePtr); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 445 | |
| 446 | // Check if the base pointer of the memory access does alias with |
| 447 | // any other pointer. This cannot be handled at the moment. |
| 448 | AliasSet &AS = |
| 449 | Context.AST.getAliasSetForPointer(BasePtr, AliasAnalysis::UnknownSize, |
| 450 | Inst.getMetadata(LLVMContext::MD_tbaa)); |
| 451 | if (!AS.isMustAlias()) { |
| 452 | DEBUG(dbgs() << "Bad pointer alias found:" << *BasePtr << "\nAS:\n" << AS); |
| 453 | |
| 454 | // STATSCOP triggers an assertion if we are in verifying mode. |
| 455 | // This is generally good to check that we do not change the SCoP after we |
| 456 | // run the SCoP detection and consequently to ensure that we can still |
| 457 | // represent that SCoP. However, in case of aliasing this does not work. |
| 458 | // The independent blocks pass may create memory references which seem to |
| 459 | // alias, if -basicaa is not available. They actually do not. As we do not |
| 460 | // not know this and we would fail here if we verify it. |
| 461 | if (!Context.Verifying) { |
| 462 | STATSCOP(Alias); |
| 463 | } |
| 464 | |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | bool ScopDetection::hasScalarDependency(Instruction &Inst, |
| 473 | Region &RefRegion) const { |
| 474 | for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end(); |
| 475 | UI != UE; ++UI) |
| 476 | if (Instruction *Use = dyn_cast<Instruction>(*UI)) |
| 477 | if (!RefRegion.contains(Use->getParent())) { |
| 478 | // DirtyHack 1: PHINode user outside the Scop is not allow, if this |
| 479 | // PHINode is induction variable, the scalar to array transform may |
| 480 | // break it and introduce a non-indvar PHINode, which is not allow in |
| 481 | // Scop. |
| 482 | // This can be fix by: |
| 483 | // Introduce a IndependentBlockPrepare pass, which translate all |
| 484 | // PHINodes not in Scop to array. |
| 485 | // The IndependentBlockPrepare pass can also split the entry block of |
| 486 | // the function to hold the alloca instruction created by scalar to |
| 487 | // array. and split the exit block of the Scop so the new create load |
| 488 | // instruction for escape users will not break other Scops. |
| 489 | if (isa<PHINode>(Use)) |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | bool ScopDetection::isValidInstruction(Instruction &Inst, |
| 497 | DetectionContext &Context) const { |
| 498 | // Only canonical IVs are allowed. |
| 499 | if (PHINode *PN = dyn_cast<PHINode>(&Inst)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 500 | if (!isIndVar(PN, LI)) |
| 501 | INVALID(IndVar, "Non canonical PHI node: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 502 | |
| 503 | // Scalar dependencies are not allowed. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 504 | if (hasScalarDependency(Inst, Context.CurRegion)) |
| 505 | INVALID(Scalar, "Scalar dependency found: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 506 | |
| 507 | // We only check the call instruction but not invoke instruction. |
| 508 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) { |
| 509 | if (isValidCallInst(*CI)) |
| 510 | return true; |
| 511 | |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 512 | INVALID(FuncCall, "Call instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) { |
| 516 | // Handle cast instruction. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 517 | if (isa<IntToPtrInst>(Inst) || isa<BitCastInst>(Inst)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 518 | INVALID(Other, "Cast instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 519 | |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 520 | if (isa<AllocaInst>(Inst)) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 521 | INVALID(Other, "Alloca instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | // Check the access function. |
| 527 | if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst)) |
| 528 | return isValidMemoryAccess(Inst, Context); |
| 529 | |
| 530 | // We do not know this instruction, therefore we assume it is invalid. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 531 | INVALID(Other, "Unknown instruction: " << Inst); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | bool ScopDetection::isValidBasicBlock(BasicBlock &BB, |
| 535 | DetectionContext &Context) const { |
| 536 | if (!isValidCFG(BB, Context)) |
| 537 | return false; |
| 538 | |
| 539 | // Check all instructions, except the terminator instruction. |
| 540 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 541 | if (!isValidInstruction(*I, Context)) |
| 542 | return false; |
| 543 | |
| 544 | Loop *L = LI->getLoopFor(&BB); |
| 545 | if (L && L->getHeader() == &BB && !isValidLoop(L, Context)) |
| 546 | return false; |
| 547 | |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const { |
| 552 | PHINode *IndVar = L->getCanonicalInductionVariable(); |
| 553 | // No canonical induction variable. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 554 | if (!IndVar) |
Tobias Grosser | b43ba82 | 2011-10-08 00:49:30 +0000 | [diff] [blame] | 555 | INVALID(IndVar, "No canonical IV at loop header: " |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 556 | << L->getHeader()->getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 557 | |
| 558 | // Is the loop count affine? |
| 559 | const SCEV *LoopCount = SE->getBackedgeTakenCount(L); |
Tobias Grosser | 3fb4992 | 2011-11-02 21:40:08 +0000 | [diff] [blame^] | 560 | if (!SCEVValidator::isValid(&Context.CurRegion, LoopCount, *SE)) |
Tobias Grosser | bd54f32 | 2011-10-26 01:27:49 +0000 | [diff] [blame] | 561 | INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: " |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 562 | << L->getHeader()->getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 563 | |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | Region *ScopDetection::expandRegion(Region &R) { |
| 568 | Region *CurrentRegion = &R; |
| 569 | Region *TmpRegion = R.getExpandedRegion(); |
| 570 | |
| 571 | DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n"); |
| 572 | |
| 573 | while (TmpRegion) { |
| 574 | DetectionContext Context(*TmpRegion, *AA, false /*verifying*/); |
| 575 | DEBUG(dbgs() << "\t\tTrying " << TmpRegion->getNameStr() << "\n"); |
| 576 | |
| 577 | if (!allBlocksValid(Context)) |
| 578 | break; |
| 579 | |
| 580 | if (isValidExit(Context)) { |
| 581 | if (CurrentRegion != &R) |
| 582 | delete CurrentRegion; |
| 583 | |
| 584 | CurrentRegion = TmpRegion; |
| 585 | } |
| 586 | |
| 587 | Region *TmpRegion2 = TmpRegion->getExpandedRegion(); |
| 588 | |
| 589 | if (TmpRegion != &R && TmpRegion != CurrentRegion) |
| 590 | delete TmpRegion; |
| 591 | |
| 592 | TmpRegion = TmpRegion2; |
| 593 | } |
| 594 | |
| 595 | if (&R == CurrentRegion) |
| 596 | return NULL; |
| 597 | |
| 598 | DEBUG(dbgs() << "\tto " << CurrentRegion->getNameStr() << "\n"); |
| 599 | |
| 600 | return CurrentRegion; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | void ScopDetection::findScops(Region &R) { |
| 605 | DetectionContext Context(R, *AA, false /*verifying*/); |
| 606 | |
| 607 | if (isValidRegion(Context)) { |
| 608 | ++ValidRegion; |
| 609 | ValidRegions.insert(&R); |
| 610 | return; |
| 611 | } |
| 612 | |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 613 | InvalidRegions[&R] = LastFailure; |
| 614 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 615 | for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 616 | findScops(**I); |
| 617 | |
| 618 | // Try to expand regions. |
| 619 | // |
| 620 | // As the region tree normally only contains canonical regions, non canonical |
| 621 | // regions that form a Scop are not found. Therefore, those non canonical |
| 622 | // regions are checked by expanding the canonical ones. |
| 623 | |
| 624 | std::vector<Region*> ToExpand; |
| 625 | |
| 626 | for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 627 | ToExpand.push_back(*I); |
| 628 | |
| 629 | for (std::vector<Region*>::iterator RI = ToExpand.begin(), |
| 630 | RE = ToExpand.end(); RI != RE; ++RI) { |
| 631 | Region *CurrentRegion = *RI; |
| 632 | |
| 633 | // Skip invalid regions. Regions may become invalid, if they are element of |
| 634 | // an already expanded region. |
| 635 | if (ValidRegions.find(CurrentRegion) == ValidRegions.end()) |
| 636 | continue; |
| 637 | |
| 638 | Region *ExpandedR = expandRegion(*CurrentRegion); |
| 639 | |
| 640 | if (!ExpandedR) |
| 641 | continue; |
| 642 | |
| 643 | R.addSubRegion(ExpandedR, true); |
| 644 | ValidRegions.insert(ExpandedR); |
| 645 | ValidRegions.erase(CurrentRegion); |
| 646 | |
| 647 | for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E; |
| 648 | ++I) |
| 649 | ValidRegions.erase(*I); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | bool ScopDetection::allBlocksValid(DetectionContext &Context) const { |
| 654 | Region &R = Context.CurRegion; |
| 655 | |
| 656 | for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E; |
| 657 | ++I) |
| 658 | if (!isValidBasicBlock(*(I->getNodeAs<BasicBlock>()), Context)) |
| 659 | return false; |
| 660 | |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | bool ScopDetection::isValidExit(DetectionContext &Context) const { |
| 665 | Region &R = Context.CurRegion; |
| 666 | |
| 667 | // PHI nodes are not allowed in the exit basic block. |
| 668 | if (BasicBlock *Exit = R.getExit()) { |
| 669 | BasicBlock::iterator I = Exit->begin(); |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 670 | if (I != Exit->end() && isa<PHINode> (*I)) |
| 671 | INVALID(Other, "PHI node in exit BB"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | bool ScopDetection::isValidRegion(DetectionContext &Context) const { |
| 678 | Region &R = Context.CurRegion; |
| 679 | |
| 680 | DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t"); |
| 681 | |
| 682 | // The toplevel region is no valid region. |
| 683 | if (!R.getParent()) { |
| 684 | DEBUG(dbgs() << "Top level region is invalid"; |
| 685 | dbgs() << "\n"); |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | // SCoP can not contains the entry block of the function, because we need |
| 690 | // to insert alloca instruction there when translate scalar to array. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 691 | if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock())) |
| 692 | INVALID(Other, "Region containing entry block of function is invalid!"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 693 | |
| 694 | // Only a simple region is allowed. |
Tobias Grosser | c4a0bd1 | 2011-10-08 00:30:48 +0000 | [diff] [blame] | 695 | if (!R.isSimple()) |
| 696 | INVALID(SimpleRegion, "Region not simple: " << R.getNameStr()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 697 | |
| 698 | if (!allBlocksValid(Context)) |
| 699 | return false; |
| 700 | |
| 701 | if (!isValidExit(Context)) |
| 702 | return false; |
| 703 | |
| 704 | DEBUG(dbgs() << "OK\n"); |
| 705 | return true; |
| 706 | } |
| 707 | |
| 708 | bool ScopDetection::isValidFunction(llvm::Function &F) { |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 709 | return !InvalidFunctions.count(&F); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | bool ScopDetection::runOnFunction(llvm::Function &F) { |
| 713 | AA = &getAnalysis<AliasAnalysis>(); |
| 714 | SE = &getAnalysis<ScalarEvolution>(); |
| 715 | LI = &getAnalysis<LoopInfo>(); |
| 716 | RI = &getAnalysis<RegionInfo>(); |
| 717 | Region *TopRegion = RI->getTopLevelRegion(); |
| 718 | |
Tobias Grosser | 2ff8723 | 2011-10-23 11:17:06 +0000 | [diff] [blame] | 719 | releaseMemory(); |
| 720 | |
| 721 | if (OnlyFunction != "" && F.getNameStr() != OnlyFunction) |
| 722 | return false; |
| 723 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 724 | if(!isValidFunction(F)) |
| 725 | return false; |
| 726 | |
| 727 | findScops(*TopRegion); |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | void polly::ScopDetection::verifyRegion(const Region &R) const { |
| 733 | assert(isMaxRegionInScop(R) && "Expect R is a valid region."); |
| 734 | DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/); |
| 735 | isValidRegion(Context); |
| 736 | } |
| 737 | |
| 738 | void polly::ScopDetection::verifyAnalysis() const { |
| 739 | for (RegionSet::const_iterator I = ValidRegions.begin(), |
| 740 | E = ValidRegions.end(); I != E; ++I) |
| 741 | verifyRegion(**I); |
| 742 | } |
| 743 | |
| 744 | void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const { |
| 745 | AU.addRequired<DominatorTree>(); |
| 746 | AU.addRequired<PostDominatorTree>(); |
| 747 | AU.addRequired<LoopInfo>(); |
| 748 | AU.addRequired<ScalarEvolution>(); |
| 749 | // We also need AA and RegionInfo when we are verifying analysis. |
| 750 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 751 | AU.addRequiredTransitive<RegionInfo>(); |
| 752 | AU.setPreservesAll(); |
| 753 | } |
| 754 | |
| 755 | void ScopDetection::print(raw_ostream &OS, const Module *) const { |
| 756 | for (RegionSet::const_iterator I = ValidRegions.begin(), |
| 757 | E = ValidRegions.end(); I != E; ++I) |
| 758 | OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n'; |
| 759 | |
| 760 | OS << "\n"; |
| 761 | } |
| 762 | |
| 763 | void ScopDetection::releaseMemory() { |
| 764 | ValidRegions.clear(); |
Tobias Grosser | 4f129a6 | 2011-10-08 00:30:55 +0000 | [diff] [blame] | 765 | InvalidRegions.clear(); |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 766 | // Do not clear the invalid function set. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | char ScopDetection::ID = 0; |
| 770 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 771 | INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect", |
| 772 | "Polly - Detect static control parts (SCoPs)", false, |
| 773 | false) |
| 774 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 775 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 776 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 777 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTree) |
| 778 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 779 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 780 | INITIALIZE_PASS_END(ScopDetection, "polly-detect", |
| 781 | "Polly - Detect static control parts (SCoPs)", false, false) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 782 | |
Tobias Grosser | 83f5c43 | 2011-08-23 22:35:08 +0000 | [diff] [blame] | 783 | Pass *polly::createScopDetectionPass() { |
| 784 | return new ScopDetection(); |
| 785 | } |