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