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