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