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