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