Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1 | //===-- LoopPredication.cpp - Guard based loop predication pass -----------===// |
| 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 | // The LoopPredication pass tries to convert loop variant range checks to loop |
| 11 | // invariant by widening checks across loop iterations. For example, it will |
| 12 | // convert |
| 13 | // |
| 14 | // for (i = 0; i < n; i++) { |
| 15 | // guard(i < len); |
| 16 | // ... |
| 17 | // } |
| 18 | // |
| 19 | // to |
| 20 | // |
| 21 | // for (i = 0; i < n; i++) { |
| 22 | // guard(n - 1 < len); |
| 23 | // ... |
| 24 | // } |
| 25 | // |
| 26 | // After this transformation the condition of the guard is loop invariant, so |
| 27 | // loop-unswitch can later unswitch the loop by this condition which basically |
| 28 | // predicates the loop by the widened condition: |
| 29 | // |
| 30 | // if (n - 1 < len) |
| 31 | // for (i = 0; i < n; i++) { |
| 32 | // ... |
| 33 | // } |
| 34 | // else |
| 35 | // deoptimize |
| 36 | // |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 37 | // It's tempting to rely on SCEV here, but it has proven to be problematic. |
| 38 | // Generally the facts SCEV provides about the increment step of add |
| 39 | // recurrences are true if the backedge of the loop is taken, which implicitly |
| 40 | // assumes that the guard doesn't fail. Using these facts to optimize the |
| 41 | // guard results in a circular logic where the guard is optimized under the |
| 42 | // assumption that it never fails. |
| 43 | // |
| 44 | // For example, in the loop below the induction variable will be marked as nuw |
| 45 | // basing on the guard. Basing on nuw the guard predicate will be considered |
| 46 | // monotonic. Given a monotonic condition it's tempting to replace the induction |
| 47 | // variable in the condition with its value on the last iteration. But this |
| 48 | // transformation is not correct, e.g. e = 4, b = 5 breaks the loop. |
| 49 | // |
| 50 | // for (int i = b; i != e; i++) |
| 51 | // guard(i u< len) |
| 52 | // |
| 53 | // One of the ways to reason about this problem is to use an inductive proof |
| 54 | // approach. Given the loop: |
| 55 | // |
| 56 | // if (B(Start)) { |
| 57 | // do { |
| 58 | // I = PHI(Start, I.INC) |
| 59 | // I.INC = I + Step |
| 60 | // guard(G(I)); |
| 61 | // } while (B(I.INC)); |
| 62 | // } |
| 63 | // |
| 64 | // where B(x) and G(x) are predicates that map integers to booleans, we want a |
| 65 | // loop invariant expression M such the following program has the same semantics |
| 66 | // as the above: |
| 67 | // |
| 68 | // if (B(Start)) { |
| 69 | // do { |
| 70 | // I = PHI(Start, I.INC) |
| 71 | // I.INC = I + Step |
| 72 | // guard(G(Start) && M); |
| 73 | // } while (B(I.INC)); |
| 74 | // } |
| 75 | // |
| 76 | // One solution for M is M = forall X . (G(X) && B(X + Step)) => G(X + Step) |
| 77 | // |
| 78 | // Informal proof that the transformation above is correct: |
| 79 | // |
| 80 | // By the definition of guards we can rewrite the guard condition to: |
| 81 | // G(I) && G(Start) && M |
| 82 | // |
| 83 | // Let's prove that for each iteration of the loop: |
| 84 | // G(Start) && M => G(I) |
| 85 | // And the condition above can be simplified to G(Start) && M. |
| 86 | // |
| 87 | // Induction base. |
| 88 | // G(Start) && M => G(Start) |
| 89 | // |
| 90 | // Induction step. Assuming G(Start) && M => G(I) on the subsequent |
| 91 | // iteration: |
| 92 | // |
| 93 | // B(I + Step) is true because it's the backedge condition. |
| 94 | // G(I) is true because the backedge is guarded by this condition. |
| 95 | // |
| 96 | // So M = forall X . (G(X) && B(X + Step)) => G(X + Step) implies |
| 97 | // G(I + Step). |
| 98 | // |
| 99 | // Note that we can use anything stronger than M, i.e. any condition which |
| 100 | // implies M. |
| 101 | // |
| 102 | // For now the transformation is limited to the following case: |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 103 | // * The loop has a single latch with the condition of the form: |
| 104 | // ++i <pred> latchLimit, where <pred> is u<, u<=, s<, or s<=. |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 105 | // * The step of the IV used in the latch condition is 1. |
| 106 | // * The IV of the latch condition is the same as the post increment IV of the |
| 107 | // guard condition. |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 108 | // * The guard condition is |
| 109 | // i u< guardLimit. |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 110 | // |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 111 | // For the ult latch comparison case M is: |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 112 | // forall X . X u< guardLimit && (X + 1) u< latchLimit => |
| 113 | // (X + 1) u< guardLimit |
| 114 | // |
| 115 | // This is true if latchLimit u<= guardLimit since then |
| 116 | // (X + 1) u< latchLimit u<= guardLimit == (X + 1) u< guardLimit. |
| 117 | // |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 118 | // So for ult condition the widened condition is: |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 119 | // i.start u< guardLimit && latchLimit u<= guardLimit |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 120 | // Similarly for ule condition the widened condition is: |
| 121 | // i.start u< guardLimit && latchLimit u< guardLimit |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 122 | // |
| 123 | // For the signed latch comparison case M is: |
| 124 | // forall X . X u< guardLimit && (X + 1) s< latchLimit => |
| 125 | // (X + 1) u< guardLimit |
| 126 | // |
| 127 | // The only way the antecedent can be true and the consequent can be false is |
| 128 | // if |
| 129 | // X == guardLimit - 1 |
| 130 | // (and guardLimit is non-zero, but we won't use this latter fact). |
| 131 | // If X == guardLimit - 1 then the second half of the antecedent is |
| 132 | // guardLimit s< latchLimit |
| 133 | // and its negation is |
| 134 | // latchLimit s<= guardLimit. |
| 135 | // |
| 136 | // In other words, if latchLimit s<= guardLimit then: |
| 137 | // (the ranges below are written in ConstantRange notation, where [A, B) is the |
| 138 | // set for (I = A; I != B; I++ /*maywrap*/) yield(I);) |
| 139 | // |
| 140 | // forall X . X u< guardLimit && (X + 1) s< latchLimit => (X + 1) u< guardLimit |
| 141 | // == forall X . X u< guardLimit && (X + 1) s< guardLimit => (X + 1) u< guardLimit |
| 142 | // == forall X . X in [0, guardLimit) && (X + 1) in [INT_MIN, guardLimit) => (X + 1) in [0, guardLimit) |
| 143 | // == forall X . X in [0, guardLimit) && X in [INT_MAX, guardLimit-1) => X in [-1, guardLimit-1) |
| 144 | // == forall X . X in [0, guardLimit-1) => X in [-1, guardLimit-1) |
| 145 | // == true |
| 146 | // |
| 147 | // So the widened condition is: |
| 148 | // i.start u< guardLimit && latchLimit s<= guardLimit |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 149 | // Similarly for sle condition the widened condition is: |
| 150 | // i.start u< guardLimit && latchLimit s< guardLimit |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 151 | // |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 152 | //===----------------------------------------------------------------------===// |
| 153 | |
| 154 | #include "llvm/Transforms/Scalar/LoopPredication.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 155 | #include "llvm/Analysis/LoopInfo.h" |
| 156 | #include "llvm/Analysis/LoopPass.h" |
| 157 | #include "llvm/Analysis/ScalarEvolution.h" |
| 158 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 159 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 160 | #include "llvm/IR/Function.h" |
| 161 | #include "llvm/IR/GlobalValue.h" |
| 162 | #include "llvm/IR/IntrinsicInst.h" |
| 163 | #include "llvm/IR/Module.h" |
| 164 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 165 | #include "llvm/Pass.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 166 | #include "llvm/Support/Debug.h" |
| 167 | #include "llvm/Transforms/Scalar.h" |
| 168 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 169 | |
| 170 | #define DEBUG_TYPE "loop-predication" |
| 171 | |
| 172 | using namespace llvm; |
| 173 | |
| 174 | namespace { |
| 175 | class LoopPredication { |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 176 | /// Represents an induction variable check: |
| 177 | /// icmp Pred, <induction variable>, <loop invariant limit> |
| 178 | struct LoopICmp { |
| 179 | ICmpInst::Predicate Pred; |
| 180 | const SCEVAddRecExpr *IV; |
| 181 | const SCEV *Limit; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 182 | LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV, |
| 183 | const SCEV *Limit) |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 184 | : Pred(Pred), IV(IV), Limit(Limit) {} |
| 185 | LoopICmp() {} |
| 186 | }; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 187 | |
| 188 | ScalarEvolution *SE; |
| 189 | |
| 190 | Loop *L; |
| 191 | const DataLayout *DL; |
| 192 | BasicBlock *Preheader; |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 193 | LoopICmp LatchCheck; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 194 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 195 | Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI) { |
| 196 | return parseLoopICmp(ICI->getPredicate(), ICI->getOperand(0), |
| 197 | ICI->getOperand(1)); |
| 198 | } |
| 199 | Optional<LoopICmp> parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS, |
| 200 | Value *RHS); |
| 201 | |
| 202 | Optional<LoopICmp> parseLoopLatchICmp(); |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 203 | |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 204 | Value *expandCheck(SCEVExpander &Expander, IRBuilder<> &Builder, |
| 205 | ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, |
| 206 | Instruction *InsertAt); |
| 207 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 208 | Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander, |
| 209 | IRBuilder<> &Builder); |
| 210 | bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander); |
| 211 | |
| 212 | public: |
| 213 | LoopPredication(ScalarEvolution *SE) : SE(SE){}; |
| 214 | bool runOnLoop(Loop *L); |
| 215 | }; |
| 216 | |
| 217 | class LoopPredicationLegacyPass : public LoopPass { |
| 218 | public: |
| 219 | static char ID; |
| 220 | LoopPredicationLegacyPass() : LoopPass(ID) { |
| 221 | initializeLoopPredicationLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 222 | } |
| 223 | |
| 224 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 225 | getLoopAnalysisUsage(AU); |
| 226 | } |
| 227 | |
| 228 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { |
| 229 | if (skipLoop(L)) |
| 230 | return false; |
| 231 | auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 232 | LoopPredication LP(SE); |
| 233 | return LP.runOnLoop(L); |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | char LoopPredicationLegacyPass::ID = 0; |
| 238 | } // end namespace llvm |
| 239 | |
| 240 | INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication", |
| 241 | "Loop predication", false, false) |
| 242 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 243 | INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication", |
| 244 | "Loop predication", false, false) |
| 245 | |
| 246 | Pass *llvm::createLoopPredicationPass() { |
| 247 | return new LoopPredicationLegacyPass(); |
| 248 | } |
| 249 | |
| 250 | PreservedAnalyses LoopPredicationPass::run(Loop &L, LoopAnalysisManager &AM, |
| 251 | LoopStandardAnalysisResults &AR, |
| 252 | LPMUpdater &U) { |
| 253 | LoopPredication LP(&AR.SE); |
| 254 | if (!LP.runOnLoop(&L)) |
| 255 | return PreservedAnalyses::all(); |
| 256 | |
| 257 | return getLoopPassPreservedAnalyses(); |
| 258 | } |
| 259 | |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 260 | Optional<LoopPredication::LoopICmp> |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 261 | LoopPredication::parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS, |
| 262 | Value *RHS) { |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 263 | const SCEV *LHSS = SE->getSCEV(LHS); |
| 264 | if (isa<SCEVCouldNotCompute>(LHSS)) |
| 265 | return None; |
| 266 | const SCEV *RHSS = SE->getSCEV(RHS); |
| 267 | if (isa<SCEVCouldNotCompute>(RHSS)) |
| 268 | return None; |
| 269 | |
| 270 | // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV |
| 271 | if (SE->isLoopInvariant(LHSS, L)) { |
| 272 | std::swap(LHS, RHS); |
| 273 | std::swap(LHSS, RHSS); |
| 274 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 275 | } |
| 276 | |
| 277 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS); |
| 278 | if (!AR || AR->getLoop() != L) |
| 279 | return None; |
| 280 | |
| 281 | return LoopICmp(Pred, AR, RHSS); |
| 282 | } |
| 283 | |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 284 | Value *LoopPredication::expandCheck(SCEVExpander &Expander, |
| 285 | IRBuilder<> &Builder, |
| 286 | ICmpInst::Predicate Pred, const SCEV *LHS, |
| 287 | const SCEV *RHS, Instruction *InsertAt) { |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 288 | // TODO: we can check isLoopEntryGuardedByCond before emitting the check |
| 289 | |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 290 | Type *Ty = LHS->getType(); |
| 291 | assert(Ty == RHS->getType() && "expandCheck operands have different types?"); |
Artur Pilipenko | ead69ee | 2017-10-12 21:21:17 +0000 | [diff] [blame^] | 292 | |
| 293 | if (SE->isLoopEntryGuardedByCond(L, Pred, LHS, RHS)) |
| 294 | return Builder.getTrue(); |
| 295 | |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 296 | Value *LHSV = Expander.expandCodeFor(LHS, Ty, InsertAt); |
| 297 | Value *RHSV = Expander.expandCodeFor(RHS, Ty, InsertAt); |
| 298 | return Builder.CreateICmp(Pred, LHSV, RHSV); |
| 299 | } |
| 300 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 301 | /// If ICI can be widened to a loop invariant condition emits the loop |
| 302 | /// invariant condition in the loop preheader and return it, otherwise |
| 303 | /// returns None. |
| 304 | Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI, |
| 305 | SCEVExpander &Expander, |
| 306 | IRBuilder<> &Builder) { |
| 307 | DEBUG(dbgs() << "Analyzing ICmpInst condition:\n"); |
| 308 | DEBUG(ICI->dump()); |
| 309 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 310 | // parseLoopStructure guarantees that the latch condition is: |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 311 | // ++i <pred> latchLimit, where <pred> is u<, u<=, s<, or s<=. |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 312 | // We are looking for the range checks of the form: |
| 313 | // i u< guardLimit |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 314 | auto RangeCheck = parseLoopICmp(ICI); |
Artur Pilipenko | edee251 | 2017-05-22 12:06:57 +0000 | [diff] [blame] | 315 | if (!RangeCheck) { |
| 316 | DEBUG(dbgs() << "Failed to parse the loop latch condition!\n"); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 317 | return None; |
Artur Pilipenko | edee251 | 2017-05-22 12:06:57 +0000 | [diff] [blame] | 318 | } |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 319 | if (RangeCheck->Pred != ICmpInst::ICMP_ULT) { |
| 320 | DEBUG(dbgs() << "Unsupported range check predicate(" << RangeCheck->Pred |
| 321 | << ")!\n"); |
| 322 | return None; |
| 323 | } |
| 324 | auto *RangeCheckIV = RangeCheck->IV; |
| 325 | auto *PostIncRangeCheckIV = RangeCheckIV->getPostIncExpr(*SE); |
| 326 | if (LatchCheck.IV != PostIncRangeCheckIV) { |
| 327 | DEBUG(dbgs() << "Post increment range check IV (" << *PostIncRangeCheckIV |
| 328 | << ") is not the same as latch IV (" << *LatchCheck.IV |
| 329 | << ")!\n"); |
| 330 | return None; |
| 331 | } |
| 332 | assert(RangeCheckIV->getStepRecurrence(*SE)->isOne() && "must be one"); |
| 333 | const SCEV *Start = RangeCheckIV->getStart(); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 334 | |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 335 | // Generate the widened condition: |
| 336 | // i.start u< guardLimit && latchLimit <pred> guardLimit |
| 337 | // where <pred> depends on the latch condition predicate. See the file |
| 338 | // header comment for the reasoning. |
| 339 | ICmpInst::Predicate LimitCheckPred; |
| 340 | switch (LatchCheck.Pred) { |
| 341 | case ICmpInst::ICMP_ULT: |
| 342 | LimitCheckPred = ICmpInst::ICMP_ULE; |
| 343 | break; |
| 344 | case ICmpInst::ICMP_ULE: |
| 345 | LimitCheckPred = ICmpInst::ICMP_ULT; |
| 346 | break; |
| 347 | case ICmpInst::ICMP_SLT: |
| 348 | LimitCheckPred = ICmpInst::ICMP_SLE; |
| 349 | break; |
| 350 | case ICmpInst::ICMP_SLE: |
| 351 | LimitCheckPred = ICmpInst::ICMP_SLT; |
| 352 | break; |
| 353 | default: |
| 354 | llvm_unreachable("Unsupported loop latch!"); |
| 355 | } |
Artur Pilipenko | aab2866 | 2017-05-19 14:00:04 +0000 | [diff] [blame] | 356 | |
| 357 | auto CanExpand = [this](const SCEV *S) { |
| 358 | return SE->isLoopInvariant(S, L) && isSafeToExpand(S, *SE); |
| 359 | }; |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 360 | if (!CanExpand(Start) || !CanExpand(LatchCheck.Limit) || |
| 361 | !CanExpand(RangeCheck->Limit)) |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 362 | return None; |
| 363 | |
Artur Pilipenko | 0860bfc | 2017-02-27 15:44:49 +0000 | [diff] [blame] | 364 | Instruction *InsertAt = Preheader->getTerminator(); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 365 | auto *LimitCheck = expandCheck(Expander, Builder, LimitCheckPred, |
| 366 | LatchCheck.Limit, RangeCheck->Limit, InsertAt); |
Artur Pilipenko | ead69ee | 2017-10-12 21:21:17 +0000 | [diff] [blame^] | 367 | auto *FirstIterationCheck = expandCheck(Expander, Builder, RangeCheck->Pred, |
| 368 | Start, RangeCheck->Limit, InsertAt); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 369 | return Builder.CreateAnd(FirstIterationCheck, LimitCheck); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard, |
| 373 | SCEVExpander &Expander) { |
| 374 | DEBUG(dbgs() << "Processing guard:\n"); |
| 375 | DEBUG(Guard->dump()); |
| 376 | |
| 377 | IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator())); |
| 378 | |
| 379 | // The guard condition is expected to be in form of: |
| 380 | // cond1 && cond2 && cond3 ... |
| 381 | // Iterate over subconditions looking for for icmp conditions which can be |
| 382 | // widened across loop iterations. Widening these conditions remember the |
| 383 | // resulting list of subconditions in Checks vector. |
| 384 | SmallVector<Value *, 4> Worklist(1, Guard->getOperand(0)); |
| 385 | SmallPtrSet<Value *, 4> Visited; |
| 386 | |
| 387 | SmallVector<Value *, 4> Checks; |
| 388 | |
| 389 | unsigned NumWidened = 0; |
| 390 | do { |
| 391 | Value *Condition = Worklist.pop_back_val(); |
| 392 | if (!Visited.insert(Condition).second) |
| 393 | continue; |
| 394 | |
| 395 | Value *LHS, *RHS; |
| 396 | using namespace llvm::PatternMatch; |
| 397 | if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) { |
| 398 | Worklist.push_back(LHS); |
| 399 | Worklist.push_back(RHS); |
| 400 | continue; |
| 401 | } |
| 402 | |
| 403 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) { |
| 404 | if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, Builder)) { |
| 405 | Checks.push_back(NewRangeCheck.getValue()); |
| 406 | NumWidened++; |
| 407 | continue; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Save the condition as is if we can't widen it |
| 412 | Checks.push_back(Condition); |
| 413 | } while (Worklist.size() != 0); |
| 414 | |
| 415 | if (NumWidened == 0) |
| 416 | return false; |
| 417 | |
| 418 | // Emit the new guard condition |
| 419 | Builder.SetInsertPoint(Guard); |
| 420 | Value *LastCheck = nullptr; |
| 421 | for (auto *Check : Checks) |
| 422 | if (!LastCheck) |
| 423 | LastCheck = Check; |
| 424 | else |
| 425 | LastCheck = Builder.CreateAnd(LastCheck, Check); |
| 426 | Guard->setOperand(0, LastCheck); |
| 427 | |
| 428 | DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n"); |
| 429 | return true; |
| 430 | } |
| 431 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 432 | Optional<LoopPredication::LoopICmp> LoopPredication::parseLoopLatchICmp() { |
| 433 | using namespace PatternMatch; |
| 434 | |
| 435 | BasicBlock *LoopLatch = L->getLoopLatch(); |
| 436 | if (!LoopLatch) { |
| 437 | DEBUG(dbgs() << "The loop doesn't have a single latch!\n"); |
| 438 | return None; |
| 439 | } |
| 440 | |
| 441 | ICmpInst::Predicate Pred; |
| 442 | Value *LHS, *RHS; |
| 443 | BasicBlock *TrueDest, *FalseDest; |
| 444 | |
| 445 | if (!match(LoopLatch->getTerminator(), |
| 446 | m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TrueDest, |
| 447 | FalseDest))) { |
| 448 | DEBUG(dbgs() << "Failed to match the latch terminator!\n"); |
| 449 | return None; |
| 450 | } |
| 451 | assert((TrueDest == L->getHeader() || FalseDest == L->getHeader()) && |
| 452 | "One of the latch's destinations must be the header"); |
| 453 | if (TrueDest != L->getHeader()) |
| 454 | Pred = ICmpInst::getInversePredicate(Pred); |
| 455 | |
| 456 | auto Result = parseLoopICmp(Pred, LHS, RHS); |
| 457 | if (!Result) { |
| 458 | DEBUG(dbgs() << "Failed to parse the loop latch condition!\n"); |
| 459 | return None; |
| 460 | } |
| 461 | |
| 462 | if (Result->Pred != ICmpInst::ICMP_ULT && |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 463 | Result->Pred != ICmpInst::ICMP_SLT && |
| 464 | Result->Pred != ICmpInst::ICMP_ULE && |
| 465 | Result->Pred != ICmpInst::ICMP_SLE) { |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 466 | DEBUG(dbgs() << "Unsupported loop latch predicate(" << Result->Pred |
| 467 | << ")!\n"); |
| 468 | return None; |
| 469 | } |
| 470 | |
| 471 | // Check affine first, so if it's not we don't try to compute the step |
| 472 | // recurrence. |
| 473 | if (!Result->IV->isAffine()) { |
| 474 | DEBUG(dbgs() << "The induction variable is not affine!\n"); |
| 475 | return None; |
| 476 | } |
| 477 | |
| 478 | auto *Step = Result->IV->getStepRecurrence(*SE); |
| 479 | if (!Step->isOne()) { |
| 480 | DEBUG(dbgs() << "Unsupported loop stride(" << *Step << ")!\n"); |
| 481 | return None; |
| 482 | } |
| 483 | |
| 484 | return Result; |
| 485 | } |
| 486 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 487 | bool LoopPredication::runOnLoop(Loop *Loop) { |
| 488 | L = Loop; |
| 489 | |
| 490 | DEBUG(dbgs() << "Analyzing "); |
| 491 | DEBUG(L->dump()); |
| 492 | |
| 493 | Module *M = L->getHeader()->getModule(); |
| 494 | |
| 495 | // There is nothing to do if the module doesn't use guards |
| 496 | auto *GuardDecl = |
| 497 | M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard)); |
| 498 | if (!GuardDecl || GuardDecl->use_empty()) |
| 499 | return false; |
| 500 | |
| 501 | DL = &M->getDataLayout(); |
| 502 | |
| 503 | Preheader = L->getLoopPreheader(); |
| 504 | if (!Preheader) |
| 505 | return false; |
| 506 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 507 | auto LatchCheckOpt = parseLoopLatchICmp(); |
| 508 | if (!LatchCheckOpt) |
| 509 | return false; |
| 510 | LatchCheck = *LatchCheckOpt; |
| 511 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 512 | // Collect all the guards into a vector and process later, so as not |
| 513 | // to invalidate the instruction iterator. |
| 514 | SmallVector<IntrinsicInst *, 4> Guards; |
| 515 | for (const auto BB : L->blocks()) |
| 516 | for (auto &I : *BB) |
| 517 | if (auto *II = dyn_cast<IntrinsicInst>(&I)) |
| 518 | if (II->getIntrinsicID() == Intrinsic::experimental_guard) |
| 519 | Guards.push_back(II); |
| 520 | |
Artur Pilipenko | 46c4e0a | 2017-05-19 13:59:34 +0000 | [diff] [blame] | 521 | if (Guards.empty()) |
| 522 | return false; |
| 523 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 524 | SCEVExpander Expander(*SE, *DL, "loop-predication"); |
| 525 | |
| 526 | bool Changed = false; |
| 527 | for (auto *Guard : Guards) |
| 528 | Changed |= widenGuardConditions(Guard, Expander); |
| 529 | |
| 530 | return Changed; |
| 531 | } |