Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1 | //===-- LoopPredication.cpp - Guard based loop predication pass -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // The LoopPredication pass tries to convert loop variant range checks to loop |
| 10 | // invariant by widening checks across loop iterations. For example, it will |
| 11 | // convert |
| 12 | // |
| 13 | // for (i = 0; i < n; i++) { |
| 14 | // guard(i < len); |
| 15 | // ... |
| 16 | // } |
| 17 | // |
| 18 | // to |
| 19 | // |
| 20 | // for (i = 0; i < n; i++) { |
| 21 | // guard(n - 1 < len); |
| 22 | // ... |
| 23 | // } |
| 24 | // |
| 25 | // After this transformation the condition of the guard is loop invariant, so |
| 26 | // loop-unswitch can later unswitch the loop by this condition which basically |
| 27 | // predicates the loop by the widened condition: |
| 28 | // |
| 29 | // if (n - 1 < len) |
| 30 | // for (i = 0; i < n; i++) { |
| 31 | // ... |
| 32 | // } |
| 33 | // else |
| 34 | // deoptimize |
| 35 | // |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 36 | // It's tempting to rely on SCEV here, but it has proven to be problematic. |
| 37 | // Generally the facts SCEV provides about the increment step of add |
| 38 | // recurrences are true if the backedge of the loop is taken, which implicitly |
| 39 | // assumes that the guard doesn't fail. Using these facts to optimize the |
| 40 | // guard results in a circular logic where the guard is optimized under the |
| 41 | // assumption that it never fails. |
| 42 | // |
| 43 | // For example, in the loop below the induction variable will be marked as nuw |
| 44 | // basing on the guard. Basing on nuw the guard predicate will be considered |
| 45 | // monotonic. Given a monotonic condition it's tempting to replace the induction |
| 46 | // variable in the condition with its value on the last iteration. But this |
| 47 | // transformation is not correct, e.g. e = 4, b = 5 breaks the loop. |
| 48 | // |
| 49 | // for (int i = b; i != e; i++) |
| 50 | // guard(i u< len) |
| 51 | // |
| 52 | // One of the ways to reason about this problem is to use an inductive proof |
| 53 | // approach. Given the loop: |
| 54 | // |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 55 | // if (B(0)) { |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 56 | // do { |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 57 | // I = PHI(0, I.INC) |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 58 | // I.INC = I + Step |
| 59 | // guard(G(I)); |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 60 | // } while (B(I)); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 61 | // } |
| 62 | // |
| 63 | // where B(x) and G(x) are predicates that map integers to booleans, we want a |
| 64 | // loop invariant expression M such the following program has the same semantics |
| 65 | // as the above: |
| 66 | // |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 67 | // if (B(0)) { |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 68 | // do { |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 69 | // I = PHI(0, I.INC) |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 70 | // I.INC = I + Step |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 71 | // guard(G(0) && M); |
| 72 | // } while (B(I)); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 73 | // } |
| 74 | // |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 75 | // One solution for M is M = forall X . (G(X) && B(X)) => G(X + Step) |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 76 | // |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 77 | // Informal proof that the transformation above is correct: |
| 78 | // |
| 79 | // By the definition of guards we can rewrite the guard condition to: |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 80 | // G(I) && G(0) && M |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 81 | // |
| 82 | // Let's prove that for each iteration of the loop: |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 83 | // G(0) && M => G(I) |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 84 | // And the condition above can be simplified to G(Start) && M. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 85 | // |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 86 | // Induction base. |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 87 | // G(0) && M => G(0) |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 88 | // |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 89 | // Induction step. Assuming G(0) && M => G(I) on the subsequent |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 90 | // iteration: |
| 91 | // |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 92 | // B(I) is true because it's the backedge condition. |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 93 | // G(I) is true because the backedge is guarded by this condition. |
| 94 | // |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 95 | // So M = forall X . (G(X) && B(X)) => G(X + Step) implies G(I + Step). |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 96 | // |
| 97 | // Note that we can use anything stronger than M, i.e. any condition which |
| 98 | // implies M. |
| 99 | // |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 100 | // When S = 1 (i.e. forward iterating loop), the transformation is supported |
| 101 | // when: |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 102 | // * The loop has a single latch with the condition of the form: |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 103 | // B(X) = latchStart + X <pred> latchLimit, |
| 104 | // where <pred> is u<, u<=, s<, or s<=. |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 105 | // * The guard condition is of the form |
| 106 | // G(X) = guardStart + X u< guardLimit |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 107 | // |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 108 | // For the ult latch comparison case M is: |
| 109 | // forall X . guardStart + X u< guardLimit && latchStart + X <u latchLimit => |
| 110 | // guardStart + X + 1 u< guardLimit |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 111 | // |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 112 | // The only way the antecedent can be true and the consequent can be false is |
| 113 | // if |
| 114 | // X == guardLimit - 1 - guardStart |
| 115 | // (and guardLimit is non-zero, but we won't use this latter fact). |
| 116 | // If X == guardLimit - 1 - guardStart then the second half of the antecedent is |
| 117 | // latchStart + guardLimit - 1 - guardStart u< latchLimit |
| 118 | // and its negation is |
| 119 | // latchStart + guardLimit - 1 - guardStart u>= latchLimit |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 120 | // |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 121 | // In other words, if |
| 122 | // latchLimit u<= latchStart + guardLimit - 1 - guardStart |
| 123 | // then: |
| 124 | // (the ranges below are written in ConstantRange notation, where [A, B) is the |
| 125 | // set for (I = A; I != B; I++ /*maywrap*/) yield(I);) |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 126 | // |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 127 | // forall X . guardStart + X u< guardLimit && |
| 128 | // latchStart + X u< latchLimit => |
| 129 | // guardStart + X + 1 u< guardLimit |
| 130 | // == forall X . guardStart + X u< guardLimit && |
| 131 | // latchStart + X u< latchStart + guardLimit - 1 - guardStart => |
| 132 | // guardStart + X + 1 u< guardLimit |
| 133 | // == forall X . (guardStart + X) in [0, guardLimit) && |
| 134 | // (latchStart + X) in [0, latchStart + guardLimit - 1 - guardStart) => |
| 135 | // (guardStart + X + 1) in [0, guardLimit) |
| 136 | // == forall X . X in [-guardStart, guardLimit - guardStart) && |
| 137 | // X in [-latchStart, guardLimit - 1 - guardStart) => |
| 138 | // X in [-guardStart - 1, guardLimit - guardStart - 1) |
| 139 | // == true |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 140 | // |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 141 | // So the widened condition is: |
| 142 | // guardStart u< guardLimit && |
| 143 | // latchStart + guardLimit - 1 - guardStart u>= latchLimit |
| 144 | // Similarly for ule condition the widened condition is: |
| 145 | // guardStart u< guardLimit && |
| 146 | // latchStart + guardLimit - 1 - guardStart u> latchLimit |
| 147 | // For slt condition the widened condition is: |
| 148 | // guardStart u< guardLimit && |
| 149 | // latchStart + guardLimit - 1 - guardStart s>= latchLimit |
| 150 | // For sle condition the widened condition is: |
| 151 | // guardStart u< guardLimit && |
| 152 | // latchStart + guardLimit - 1 - guardStart s> latchLimit |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 153 | // |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 154 | // When S = -1 (i.e. reverse iterating loop), the transformation is supported |
| 155 | // when: |
| 156 | // * The loop has a single latch with the condition of the form: |
Serguei Katkov | c8016e7 | 2018-02-08 10:34:08 +0000 | [diff] [blame] | 157 | // B(X) = X <pred> latchLimit, where <pred> is u>, u>=, s>, or s>=. |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 158 | // * The guard condition is of the form |
| 159 | // G(X) = X - 1 u< guardLimit |
| 160 | // |
| 161 | // For the ugt latch comparison case M is: |
| 162 | // forall X. X-1 u< guardLimit and X u> latchLimit => X-2 u< guardLimit |
| 163 | // |
| 164 | // The only way the antecedent can be true and the consequent can be false is if |
| 165 | // X == 1. |
| 166 | // If X == 1 then the second half of the antecedent is |
| 167 | // 1 u> latchLimit, and its negation is latchLimit u>= 1. |
| 168 | // |
| 169 | // So the widened condition is: |
| 170 | // guardStart u< guardLimit && latchLimit u>= 1. |
| 171 | // Similarly for sgt condition the widened condition is: |
| 172 | // guardStart u< guardLimit && latchLimit s>= 1. |
Serguei Katkov | c8016e7 | 2018-02-08 10:34:08 +0000 | [diff] [blame] | 173 | // For uge condition the widened condition is: |
| 174 | // guardStart u< guardLimit && latchLimit u> 1. |
| 175 | // For sge condition the widened condition is: |
| 176 | // guardStart u< guardLimit && latchLimit s> 1. |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 177 | //===----------------------------------------------------------------------===// |
| 178 | |
| 179 | #include "llvm/Transforms/Scalar/LoopPredication.h" |
Fedor Sergeev | c297e84 | 2018-10-17 09:02:54 +0000 | [diff] [blame] | 180 | #include "llvm/ADT/Statistic.h" |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 181 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Max Kazantsev | 28298e9 | 2018-12-26 08:22:25 +0000 | [diff] [blame] | 182 | #include "llvm/Analysis/GuardUtils.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 183 | #include "llvm/Analysis/LoopInfo.h" |
| 184 | #include "llvm/Analysis/LoopPass.h" |
| 185 | #include "llvm/Analysis/ScalarEvolution.h" |
| 186 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 187 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 188 | #include "llvm/IR/Function.h" |
| 189 | #include "llvm/IR/GlobalValue.h" |
| 190 | #include "llvm/IR/IntrinsicInst.h" |
| 191 | #include "llvm/IR/Module.h" |
| 192 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 193 | #include "llvm/Pass.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 194 | #include "llvm/Support/Debug.h" |
| 195 | #include "llvm/Transforms/Scalar.h" |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 196 | #include "llvm/Transforms/Utils/Local.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 197 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 198 | |
| 199 | #define DEBUG_TYPE "loop-predication" |
| 200 | |
Fedor Sergeev | c297e84 | 2018-10-17 09:02:54 +0000 | [diff] [blame] | 201 | STATISTIC(TotalConsidered, "Number of guards considered"); |
| 202 | STATISTIC(TotalWidened, "Number of checks widened"); |
| 203 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 204 | using namespace llvm; |
| 205 | |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 206 | static cl::opt<bool> EnableIVTruncation("loop-predication-enable-iv-truncation", |
| 207 | cl::Hidden, cl::init(true)); |
| 208 | |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 209 | static cl::opt<bool> EnableCountDownLoop("loop-predication-enable-count-down-loop", |
| 210 | cl::Hidden, cl::init(true)); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 211 | |
| 212 | static cl::opt<bool> |
| 213 | SkipProfitabilityChecks("loop-predication-skip-profitability-checks", |
| 214 | cl::Hidden, cl::init(false)); |
| 215 | |
| 216 | // This is the scale factor for the latch probability. We use this during |
| 217 | // profitability analysis to find other exiting blocks that have a much higher |
| 218 | // probability of exiting the loop instead of loop exiting via latch. |
| 219 | // This value should be greater than 1 for a sane profitability check. |
| 220 | static cl::opt<float> LatchExitProbabilityScale( |
| 221 | "loop-predication-latch-probability-scale", cl::Hidden, cl::init(2.0), |
| 222 | cl::desc("scale factor for the latch probability. Value should be greater " |
| 223 | "than 1. Lower values are ignored")); |
| 224 | |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 225 | static cl::opt<bool> PredicateWidenableBranchGuards( |
| 226 | "loop-predication-predicate-widenable-branches-to-deopt", cl::Hidden, |
| 227 | cl::desc("Whether or not we should predicate guards " |
| 228 | "expressed as widenable branches to deoptimize blocks"), |
| 229 | cl::init(true)); |
| 230 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 231 | namespace { |
| 232 | class LoopPredication { |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 233 | /// Represents an induction variable check: |
| 234 | /// icmp Pred, <induction variable>, <loop invariant limit> |
| 235 | struct LoopICmp { |
| 236 | ICmpInst::Predicate Pred; |
| 237 | const SCEVAddRecExpr *IV; |
| 238 | const SCEV *Limit; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 239 | LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV, |
| 240 | const SCEV *Limit) |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 241 | : Pred(Pred), IV(IV), Limit(Limit) {} |
| 242 | LoopICmp() {} |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 243 | void dump() { |
| 244 | dbgs() << "LoopICmp Pred = " << Pred << ", IV = " << *IV |
| 245 | << ", Limit = " << *Limit << "\n"; |
| 246 | } |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 247 | }; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 248 | |
| 249 | ScalarEvolution *SE; |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 250 | BranchProbabilityInfo *BPI; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 251 | |
| 252 | Loop *L; |
| 253 | const DataLayout *DL; |
| 254 | BasicBlock *Preheader; |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 255 | LoopICmp LatchCheck; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 256 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 257 | bool isSupportedStep(const SCEV* Step); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 258 | Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI) { |
| 259 | return parseLoopICmp(ICI->getPredicate(), ICI->getOperand(0), |
| 260 | ICI->getOperand(1)); |
| 261 | } |
| 262 | Optional<LoopICmp> parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS, |
| 263 | Value *RHS); |
| 264 | |
| 265 | Optional<LoopICmp> parseLoopLatchICmp(); |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 266 | |
Philip Reames | fbe64a2 | 2019-04-15 15:53:25 +0000 | [diff] [blame^] | 267 | /// Return an insertion point suitable for inserting a safe to speculate |
| 268 | /// instruction whose only user will be 'User' which has operands 'Ops'. A |
| 269 | /// trivial result would be the at the User itself, but we try to return a |
| 270 | /// loop invariant location if possible. |
| 271 | Instruction *findInsertPt(Instruction *User, ArrayRef<Value*> Ops); |
| 272 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 273 | bool CanExpand(const SCEV* S); |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 274 | Value *expandCheck(SCEVExpander &Expander, IRBuilder<> &Builder, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 275 | ICmpInst::Predicate Pred, const SCEV *LHS, |
| 276 | const SCEV *RHS); |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 277 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 278 | Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander, |
| 279 | IRBuilder<> &Builder); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 280 | Optional<Value *> widenICmpRangeCheckIncrementingLoop(LoopICmp LatchCheck, |
| 281 | LoopICmp RangeCheck, |
| 282 | SCEVExpander &Expander, |
| 283 | IRBuilder<> &Builder); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 284 | Optional<Value *> widenICmpRangeCheckDecrementingLoop(LoopICmp LatchCheck, |
| 285 | LoopICmp RangeCheck, |
| 286 | SCEVExpander &Expander, |
| 287 | IRBuilder<> &Builder); |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 288 | unsigned collectChecks(SmallVectorImpl<Value *> &Checks, Value *Condition, |
| 289 | SCEVExpander &Expander, IRBuilder<> &Builder); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 290 | bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 291 | bool widenWidenableBranchGuardConditions(BranchInst *Guard, SCEVExpander &Expander); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 292 | // If the loop always exits through another block in the loop, we should not |
| 293 | // predicate based on the latch check. For example, the latch check can be a |
| 294 | // very coarse grained check and there can be more fine grained exit checks |
| 295 | // within the loop. We identify such unprofitable loops through BPI. |
| 296 | bool isLoopProfitableToPredicate(); |
| 297 | |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 298 | // When the IV type is wider than the range operand type, we can still do loop |
| 299 | // predication, by generating SCEVs for the range and latch that are of the |
| 300 | // same type. We achieve this by generating a SCEV truncate expression for the |
| 301 | // latch IV. This is done iff truncation of the IV is a safe operation, |
| 302 | // without loss of information. |
| 303 | // Another way to achieve this is by generating a wider type SCEV for the |
| 304 | // range check operand, however, this needs a more involved check that |
| 305 | // operands do not overflow. This can lead to loss of information when the |
| 306 | // range operand is of the form: add i32 %offset, %iv. We need to prove that |
| 307 | // sext(x + y) is same as sext(x) + sext(y). |
| 308 | // This function returns true if we can safely represent the IV type in |
| 309 | // the RangeCheckType without loss of information. |
| 310 | bool isSafeToTruncateWideIVType(Type *RangeCheckType); |
| 311 | // Return the loopLatchCheck corresponding to the RangeCheckType if safe to do |
| 312 | // so. |
| 313 | Optional<LoopICmp> generateLoopLatchCheck(Type *RangeCheckType); |
Serguei Katkov | ebc9031 | 2018-02-07 06:53:37 +0000 | [diff] [blame] | 314 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 315 | public: |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 316 | LoopPredication(ScalarEvolution *SE, BranchProbabilityInfo *BPI) |
| 317 | : SE(SE), BPI(BPI){}; |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 318 | bool runOnLoop(Loop *L); |
| 319 | }; |
| 320 | |
| 321 | class LoopPredicationLegacyPass : public LoopPass { |
| 322 | public: |
| 323 | static char ID; |
| 324 | LoopPredicationLegacyPass() : LoopPass(ID) { |
| 325 | initializeLoopPredicationLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 326 | } |
| 327 | |
| 328 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 329 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 330 | getLoopAnalysisUsage(AU); |
| 331 | } |
| 332 | |
| 333 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { |
| 334 | if (skipLoop(L)) |
| 335 | return false; |
| 336 | auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 337 | BranchProbabilityInfo &BPI = |
| 338 | getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); |
| 339 | LoopPredication LP(SE, &BPI); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 340 | return LP.runOnLoop(L); |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | char LoopPredicationLegacyPass::ID = 0; |
| 345 | } // end namespace llvm |
| 346 | |
| 347 | INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication", |
| 348 | "Loop predication", false, false) |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 349 | INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 350 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 351 | INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication", |
| 352 | "Loop predication", false, false) |
| 353 | |
| 354 | Pass *llvm::createLoopPredicationPass() { |
| 355 | return new LoopPredicationLegacyPass(); |
| 356 | } |
| 357 | |
| 358 | PreservedAnalyses LoopPredicationPass::run(Loop &L, LoopAnalysisManager &AM, |
| 359 | LoopStandardAnalysisResults &AR, |
| 360 | LPMUpdater &U) { |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 361 | const auto &FAM = |
| 362 | AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager(); |
| 363 | Function *F = L.getHeader()->getParent(); |
| 364 | auto *BPI = FAM.getCachedResult<BranchProbabilityAnalysis>(*F); |
| 365 | LoopPredication LP(&AR.SE, BPI); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 366 | if (!LP.runOnLoop(&L)) |
| 367 | return PreservedAnalyses::all(); |
| 368 | |
| 369 | return getLoopPassPreservedAnalyses(); |
| 370 | } |
| 371 | |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 372 | Optional<LoopPredication::LoopICmp> |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 373 | LoopPredication::parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS, |
| 374 | Value *RHS) { |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 375 | const SCEV *LHSS = SE->getSCEV(LHS); |
| 376 | if (isa<SCEVCouldNotCompute>(LHSS)) |
| 377 | return None; |
| 378 | const SCEV *RHSS = SE->getSCEV(RHS); |
| 379 | if (isa<SCEVCouldNotCompute>(RHSS)) |
| 380 | return None; |
| 381 | |
| 382 | // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV |
| 383 | if (SE->isLoopInvariant(LHSS, L)) { |
| 384 | std::swap(LHS, RHS); |
| 385 | std::swap(LHSS, RHSS); |
| 386 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 387 | } |
| 388 | |
| 389 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS); |
| 390 | if (!AR || AR->getLoop() != L) |
| 391 | return None; |
| 392 | |
| 393 | return LoopICmp(Pred, AR, RHSS); |
| 394 | } |
| 395 | |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 396 | Value *LoopPredication::expandCheck(SCEVExpander &Expander, |
| 397 | IRBuilder<> &Builder, |
| 398 | ICmpInst::Predicate Pred, const SCEV *LHS, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 399 | const SCEV *RHS) { |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 400 | Type *Ty = LHS->getType(); |
| 401 | assert(Ty == RHS->getType() && "expandCheck operands have different types?"); |
Artur Pilipenko | ead69ee | 2017-10-12 21:21:17 +0000 | [diff] [blame] | 402 | |
| 403 | if (SE->isLoopEntryGuardedByCond(L, Pred, LHS, RHS)) |
| 404 | return Builder.getTrue(); |
Philip Reames | 05e3e55 | 2019-04-01 16:26:08 +0000 | [diff] [blame] | 405 | if (SE->isLoopEntryGuardedByCond(L, ICmpInst::getInversePredicate(Pred), |
| 406 | LHS, RHS)) |
| 407 | return Builder.getFalse(); |
Artur Pilipenko | ead69ee | 2017-10-12 21:21:17 +0000 | [diff] [blame] | 408 | |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 409 | Instruction *InsertAt = &*Builder.GetInsertPoint(); |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 410 | Value *LHSV = Expander.expandCodeFor(LHS, Ty, InsertAt); |
| 411 | Value *RHSV = Expander.expandCodeFor(RHS, Ty, InsertAt); |
| 412 | return Builder.CreateICmp(Pred, LHSV, RHSV); |
| 413 | } |
| 414 | |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 415 | Optional<LoopPredication::LoopICmp> |
| 416 | LoopPredication::generateLoopLatchCheck(Type *RangeCheckType) { |
| 417 | |
| 418 | auto *LatchType = LatchCheck.IV->getType(); |
| 419 | if (RangeCheckType == LatchType) |
| 420 | return LatchCheck; |
| 421 | // For now, bail out if latch type is narrower than range type. |
| 422 | if (DL->getTypeSizeInBits(LatchType) < DL->getTypeSizeInBits(RangeCheckType)) |
| 423 | return None; |
| 424 | if (!isSafeToTruncateWideIVType(RangeCheckType)) |
| 425 | return None; |
| 426 | // We can now safely identify the truncated version of the IV and limit for |
| 427 | // RangeCheckType. |
| 428 | LoopICmp NewLatchCheck; |
| 429 | NewLatchCheck.Pred = LatchCheck.Pred; |
| 430 | NewLatchCheck.IV = dyn_cast<SCEVAddRecExpr>( |
| 431 | SE->getTruncateExpr(LatchCheck.IV, RangeCheckType)); |
| 432 | if (!NewLatchCheck.IV) |
| 433 | return None; |
| 434 | NewLatchCheck.Limit = SE->getTruncateExpr(LatchCheck.Limit, RangeCheckType); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 435 | LLVM_DEBUG(dbgs() << "IV of type: " << *LatchType |
| 436 | << "can be represented as range check type:" |
| 437 | << *RangeCheckType << "\n"); |
| 438 | LLVM_DEBUG(dbgs() << "LatchCheck.IV: " << *NewLatchCheck.IV << "\n"); |
| 439 | LLVM_DEBUG(dbgs() << "LatchCheck.Limit: " << *NewLatchCheck.Limit << "\n"); |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 440 | return NewLatchCheck; |
| 441 | } |
| 442 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 443 | bool LoopPredication::isSupportedStep(const SCEV* Step) { |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 444 | return Step->isOne() || (Step->isAllOnesValue() && EnableCountDownLoop); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 445 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 446 | |
Philip Reames | fbe64a2 | 2019-04-15 15:53:25 +0000 | [diff] [blame^] | 447 | Instruction *LoopPredication::findInsertPt(Instruction *Use, |
| 448 | ArrayRef<Value*> Ops) { |
| 449 | for (Value *Op : Ops) |
| 450 | if (!L->isLoopInvariant(Op)) |
| 451 | return Use; |
| 452 | return Preheader->getTerminator(); |
| 453 | } |
| 454 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 455 | bool LoopPredication::CanExpand(const SCEV* S) { |
| 456 | return SE->isLoopInvariant(S, L) && isSafeToExpand(S, *SE); |
| 457 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 458 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 459 | Optional<Value *> LoopPredication::widenICmpRangeCheckIncrementingLoop( |
| 460 | LoopPredication::LoopICmp LatchCheck, LoopPredication::LoopICmp RangeCheck, |
| 461 | SCEVExpander &Expander, IRBuilder<> &Builder) { |
| 462 | auto *Ty = RangeCheck.IV->getType(); |
| 463 | // Generate the widened condition for the forward loop: |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 464 | // guardStart u< guardLimit && |
| 465 | // latchLimit <pred> guardLimit - 1 - guardStart + latchStart |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 466 | // where <pred> depends on the latch condition predicate. See the file |
| 467 | // header comment for the reasoning. |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 468 | // guardLimit - guardStart + latchStart - 1 |
| 469 | const SCEV *GuardStart = RangeCheck.IV->getStart(); |
| 470 | const SCEV *GuardLimit = RangeCheck.Limit; |
| 471 | const SCEV *LatchStart = LatchCheck.IV->getStart(); |
| 472 | const SCEV *LatchLimit = LatchCheck.Limit; |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 473 | |
| 474 | // guardLimit - guardStart + latchStart - 1 |
| 475 | const SCEV *RHS = |
| 476 | SE->getAddExpr(SE->getMinusSCEV(GuardLimit, GuardStart), |
| 477 | SE->getMinusSCEV(LatchStart, SE->getOne(Ty))); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 478 | if (!CanExpand(GuardStart) || !CanExpand(GuardLimit) || |
| 479 | !CanExpand(LatchLimit) || !CanExpand(RHS)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 480 | LLVM_DEBUG(dbgs() << "Can't expand limit check!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 481 | return None; |
| 482 | } |
Serguei Katkov | 3cb4c34 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 483 | auto LimitCheckPred = |
| 484 | ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred); |
Artur Pilipenko | aab2866 | 2017-05-19 14:00:04 +0000 | [diff] [blame] | 485 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 486 | LLVM_DEBUG(dbgs() << "LHS: " << *LatchLimit << "\n"); |
| 487 | LLVM_DEBUG(dbgs() << "RHS: " << *RHS << "\n"); |
| 488 | LLVM_DEBUG(dbgs() << "Pred: " << LimitCheckPred << "\n"); |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 489 | |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 490 | auto *LimitCheck = |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 491 | expandCheck(Expander, Builder, LimitCheckPred, LatchLimit, RHS); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 492 | auto *FirstIterationCheck = expandCheck(Expander, Builder, RangeCheck.Pred, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 493 | GuardStart, GuardLimit); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 494 | return Builder.CreateAnd(FirstIterationCheck, LimitCheck); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 495 | } |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 496 | |
| 497 | Optional<Value *> LoopPredication::widenICmpRangeCheckDecrementingLoop( |
| 498 | LoopPredication::LoopICmp LatchCheck, LoopPredication::LoopICmp RangeCheck, |
| 499 | SCEVExpander &Expander, IRBuilder<> &Builder) { |
| 500 | auto *Ty = RangeCheck.IV->getType(); |
| 501 | const SCEV *GuardStart = RangeCheck.IV->getStart(); |
| 502 | const SCEV *GuardLimit = RangeCheck.Limit; |
| 503 | const SCEV *LatchLimit = LatchCheck.Limit; |
| 504 | if (!CanExpand(GuardStart) || !CanExpand(GuardLimit) || |
| 505 | !CanExpand(LatchLimit)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 506 | LLVM_DEBUG(dbgs() << "Can't expand limit check!\n"); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 507 | return None; |
| 508 | } |
| 509 | // The decrement of the latch check IV should be the same as the |
| 510 | // rangeCheckIV. |
| 511 | auto *PostDecLatchCheckIV = LatchCheck.IV->getPostIncExpr(*SE); |
| 512 | if (RangeCheck.IV != PostDecLatchCheckIV) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 513 | LLVM_DEBUG(dbgs() << "Not the same. PostDecLatchCheckIV: " |
| 514 | << *PostDecLatchCheckIV |
| 515 | << " and RangeCheckIV: " << *RangeCheck.IV << "\n"); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 516 | return None; |
| 517 | } |
| 518 | |
| 519 | // Generate the widened condition for CountDownLoop: |
| 520 | // guardStart u< guardLimit && |
| 521 | // latchLimit <pred> 1. |
| 522 | // See the header comment for reasoning of the checks. |
Serguei Katkov | 3cb4c34 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 523 | auto LimitCheckPred = |
| 524 | ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 525 | auto *FirstIterationCheck = expandCheck(Expander, Builder, ICmpInst::ICMP_ULT, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 526 | GuardStart, GuardLimit); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 527 | auto *LimitCheck = expandCheck(Expander, Builder, LimitCheckPred, LatchLimit, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 528 | SE->getOne(Ty)); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 529 | return Builder.CreateAnd(FirstIterationCheck, LimitCheck); |
| 530 | } |
| 531 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 532 | /// If ICI can be widened to a loop invariant condition emits the loop |
| 533 | /// invariant condition in the loop preheader and return it, otherwise |
| 534 | /// returns None. |
| 535 | Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI, |
| 536 | SCEVExpander &Expander, |
| 537 | IRBuilder<> &Builder) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 538 | LLVM_DEBUG(dbgs() << "Analyzing ICmpInst condition:\n"); |
| 539 | LLVM_DEBUG(ICI->dump()); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 540 | |
| 541 | // parseLoopStructure guarantees that the latch condition is: |
| 542 | // ++i <pred> latchLimit, where <pred> is u<, u<=, s<, or s<=. |
| 543 | // We are looking for the range checks of the form: |
| 544 | // i u< guardLimit |
| 545 | auto RangeCheck = parseLoopICmp(ICI); |
| 546 | if (!RangeCheck) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 547 | LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 548 | return None; |
| 549 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 550 | LLVM_DEBUG(dbgs() << "Guard check:\n"); |
| 551 | LLVM_DEBUG(RangeCheck->dump()); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 552 | if (RangeCheck->Pred != ICmpInst::ICMP_ULT) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 553 | LLVM_DEBUG(dbgs() << "Unsupported range check predicate(" |
| 554 | << RangeCheck->Pred << ")!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 555 | return None; |
| 556 | } |
| 557 | auto *RangeCheckIV = RangeCheck->IV; |
| 558 | if (!RangeCheckIV->isAffine()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 559 | LLVM_DEBUG(dbgs() << "Range check IV is not affine!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 560 | return None; |
| 561 | } |
| 562 | auto *Step = RangeCheckIV->getStepRecurrence(*SE); |
| 563 | // We cannot just compare with latch IV step because the latch and range IVs |
| 564 | // may have different types. |
| 565 | if (!isSupportedStep(Step)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 566 | LLVM_DEBUG(dbgs() << "Range check and latch have IVs different steps!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 567 | return None; |
| 568 | } |
| 569 | auto *Ty = RangeCheckIV->getType(); |
| 570 | auto CurrLatchCheckOpt = generateLoopLatchCheck(Ty); |
| 571 | if (!CurrLatchCheckOpt) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 572 | LLVM_DEBUG(dbgs() << "Failed to generate a loop latch check " |
| 573 | "corresponding to range type: " |
| 574 | << *Ty << "\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 575 | return None; |
| 576 | } |
| 577 | |
| 578 | LoopICmp CurrLatchCheck = *CurrLatchCheckOpt; |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 579 | // At this point, the range and latch step should have the same type, but need |
| 580 | // not have the same value (we support both 1 and -1 steps). |
| 581 | assert(Step->getType() == |
| 582 | CurrLatchCheck.IV->getStepRecurrence(*SE)->getType() && |
| 583 | "Range and latch steps should be of same type!"); |
| 584 | if (Step != CurrLatchCheck.IV->getStepRecurrence(*SE)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 585 | LLVM_DEBUG(dbgs() << "Range and latch have different step values!\n"); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 586 | return None; |
| 587 | } |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 588 | |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 589 | if (Step->isOne()) |
| 590 | return widenICmpRangeCheckIncrementingLoop(CurrLatchCheck, *RangeCheck, |
| 591 | Expander, Builder); |
| 592 | else { |
| 593 | assert(Step->isAllOnesValue() && "Step should be -1!"); |
| 594 | return widenICmpRangeCheckDecrementingLoop(CurrLatchCheck, *RangeCheck, |
| 595 | Expander, Builder); |
| 596 | } |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 597 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 598 | |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 599 | unsigned LoopPredication::collectChecks(SmallVectorImpl<Value *> &Checks, |
| 600 | Value *Condition, |
| 601 | SCEVExpander &Expander, |
| 602 | IRBuilder<> &Builder) { |
| 603 | unsigned NumWidened = 0; |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 604 | // The guard condition is expected to be in form of: |
| 605 | // cond1 && cond2 && cond3 ... |
Hiroshi Inoue | 0909ca1 | 2018-01-26 08:15:29 +0000 | [diff] [blame] | 606 | // Iterate over subconditions looking for icmp conditions which can be |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 607 | // widened across loop iterations. Widening these conditions remember the |
| 608 | // resulting list of subconditions in Checks vector. |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 609 | SmallVector<Value *, 4> Worklist(1, Condition); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 610 | SmallPtrSet<Value *, 4> Visited; |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 611 | Value *WideableCond = nullptr; |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 612 | do { |
| 613 | Value *Condition = Worklist.pop_back_val(); |
| 614 | if (!Visited.insert(Condition).second) |
| 615 | continue; |
| 616 | |
| 617 | Value *LHS, *RHS; |
| 618 | using namespace llvm::PatternMatch; |
| 619 | if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) { |
| 620 | Worklist.push_back(LHS); |
| 621 | Worklist.push_back(RHS); |
| 622 | continue; |
| 623 | } |
| 624 | |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 625 | if (match(Condition, |
| 626 | m_Intrinsic<Intrinsic::experimental_widenable_condition>())) { |
| 627 | // Pick any, we don't care which |
| 628 | WideableCond = Condition; |
| 629 | continue; |
| 630 | } |
| 631 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 632 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) { |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 633 | if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, |
| 634 | Builder)) { |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 635 | Checks.push_back(NewRangeCheck.getValue()); |
| 636 | NumWidened++; |
| 637 | continue; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | // Save the condition as is if we can't widen it |
| 642 | Checks.push_back(Condition); |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 643 | } while (!Worklist.empty()); |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 644 | // At the moment, our matching logic for wideable conditions implicitly |
| 645 | // assumes we preserve the form: (br (and Cond, WC())). FIXME |
| 646 | // Note that if there were multiple calls to wideable condition in the |
| 647 | // traversal, we only need to keep one, and which one is arbitrary. |
| 648 | if (WideableCond) |
| 649 | Checks.push_back(WideableCond); |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 650 | return NumWidened; |
| 651 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 652 | |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 653 | bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard, |
| 654 | SCEVExpander &Expander) { |
| 655 | LLVM_DEBUG(dbgs() << "Processing guard:\n"); |
| 656 | LLVM_DEBUG(Guard->dump()); |
| 657 | |
| 658 | TotalConsidered++; |
| 659 | SmallVector<Value *, 4> Checks; |
| 660 | IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator())); |
| 661 | unsigned NumWidened = collectChecks(Checks, Guard->getOperand(0), Expander, |
| 662 | Builder); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 663 | if (NumWidened == 0) |
| 664 | return false; |
| 665 | |
Fedor Sergeev | c297e84 | 2018-10-17 09:02:54 +0000 | [diff] [blame] | 666 | TotalWidened += NumWidened; |
| 667 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 668 | // Emit the new guard condition |
Philip Reames | fbe64a2 | 2019-04-15 15:53:25 +0000 | [diff] [blame^] | 669 | Builder.SetInsertPoint(findInsertPt(Guard, Checks)); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 670 | Value *LastCheck = nullptr; |
| 671 | for (auto *Check : Checks) |
| 672 | if (!LastCheck) |
| 673 | LastCheck = Check; |
| 674 | else |
| 675 | LastCheck = Builder.CreateAnd(LastCheck, Check); |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 676 | auto *OldCond = Guard->getOperand(0); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 677 | Guard->setOperand(0, LastCheck); |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 678 | RecursivelyDeleteTriviallyDeadInstructions(OldCond); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 679 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 680 | LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n"); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 681 | return true; |
| 682 | } |
| 683 | |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 684 | bool LoopPredication::widenWidenableBranchGuardConditions( |
Philip Reames | f608678 | 2019-04-01 22:39:54 +0000 | [diff] [blame] | 685 | BranchInst *BI, SCEVExpander &Expander) { |
| 686 | assert(isGuardAsWidenableBranch(BI) && "Must be!"); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 687 | LLVM_DEBUG(dbgs() << "Processing guard:\n"); |
Philip Reames | f608678 | 2019-04-01 22:39:54 +0000 | [diff] [blame] | 688 | LLVM_DEBUG(BI->dump()); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 689 | |
| 690 | TotalConsidered++; |
| 691 | SmallVector<Value *, 4> Checks; |
| 692 | IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator())); |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 693 | unsigned NumWidened = collectChecks(Checks, BI->getCondition(), |
| 694 | Expander, Builder); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 695 | if (NumWidened == 0) |
| 696 | return false; |
| 697 | |
| 698 | TotalWidened += NumWidened; |
| 699 | |
| 700 | // Emit the new guard condition |
Philip Reames | fbe64a2 | 2019-04-15 15:53:25 +0000 | [diff] [blame^] | 701 | Builder.SetInsertPoint(findInsertPt(BI, Checks)); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 702 | Value *LastCheck = nullptr; |
| 703 | for (auto *Check : Checks) |
| 704 | if (!LastCheck) |
| 705 | LastCheck = Check; |
| 706 | else |
| 707 | LastCheck = Builder.CreateAnd(LastCheck, Check); |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 708 | auto *OldCond = BI->getCondition(); |
| 709 | BI->setCondition(LastCheck); |
Philip Reames | f608678 | 2019-04-01 22:39:54 +0000 | [diff] [blame] | 710 | assert(isGuardAsWidenableBranch(BI) && |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 711 | "Stopped being a guard after transform?"); |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 712 | RecursivelyDeleteTriviallyDeadInstructions(OldCond); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 713 | |
| 714 | LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n"); |
| 715 | return true; |
| 716 | } |
| 717 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 718 | Optional<LoopPredication::LoopICmp> LoopPredication::parseLoopLatchICmp() { |
| 719 | using namespace PatternMatch; |
| 720 | |
| 721 | BasicBlock *LoopLatch = L->getLoopLatch(); |
| 722 | if (!LoopLatch) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 723 | LLVM_DEBUG(dbgs() << "The loop doesn't have a single latch!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 724 | return None; |
| 725 | } |
| 726 | |
| 727 | ICmpInst::Predicate Pred; |
| 728 | Value *LHS, *RHS; |
| 729 | BasicBlock *TrueDest, *FalseDest; |
| 730 | |
| 731 | if (!match(LoopLatch->getTerminator(), |
| 732 | m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TrueDest, |
| 733 | FalseDest))) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 734 | LLVM_DEBUG(dbgs() << "Failed to match the latch terminator!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 735 | return None; |
| 736 | } |
| 737 | assert((TrueDest == L->getHeader() || FalseDest == L->getHeader()) && |
| 738 | "One of the latch's destinations must be the header"); |
| 739 | if (TrueDest != L->getHeader()) |
| 740 | Pred = ICmpInst::getInversePredicate(Pred); |
| 741 | |
| 742 | auto Result = parseLoopICmp(Pred, LHS, RHS); |
| 743 | if (!Result) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 744 | LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 745 | return None; |
| 746 | } |
| 747 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 748 | // Check affine first, so if it's not we don't try to compute the step |
| 749 | // recurrence. |
| 750 | if (!Result->IV->isAffine()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 751 | LLVM_DEBUG(dbgs() << "The induction variable is not affine!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 752 | return None; |
| 753 | } |
| 754 | |
| 755 | auto *Step = Result->IV->getStepRecurrence(*SE); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 756 | if (!isSupportedStep(Step)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 757 | LLVM_DEBUG(dbgs() << "Unsupported loop stride(" << *Step << ")!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 758 | return None; |
| 759 | } |
| 760 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 761 | auto IsUnsupportedPredicate = [](const SCEV *Step, ICmpInst::Predicate Pred) { |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 762 | if (Step->isOne()) { |
| 763 | return Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_SLT && |
| 764 | Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_SLE; |
| 765 | } else { |
| 766 | assert(Step->isAllOnesValue() && "Step should be -1!"); |
Serguei Katkov | c8016e7 | 2018-02-08 10:34:08 +0000 | [diff] [blame] | 767 | return Pred != ICmpInst::ICMP_UGT && Pred != ICmpInst::ICMP_SGT && |
| 768 | Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_SGE; |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 769 | } |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 770 | }; |
| 771 | |
| 772 | if (IsUnsupportedPredicate(Step, Result->Pred)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 773 | LLVM_DEBUG(dbgs() << "Unsupported loop latch predicate(" << Result->Pred |
| 774 | << ")!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 775 | return None; |
| 776 | } |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 777 | return Result; |
| 778 | } |
| 779 | |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 780 | // Returns true if its safe to truncate the IV to RangeCheckType. |
| 781 | bool LoopPredication::isSafeToTruncateWideIVType(Type *RangeCheckType) { |
| 782 | if (!EnableIVTruncation) |
| 783 | return false; |
| 784 | assert(DL->getTypeSizeInBits(LatchCheck.IV->getType()) > |
| 785 | DL->getTypeSizeInBits(RangeCheckType) && |
| 786 | "Expected latch check IV type to be larger than range check operand " |
| 787 | "type!"); |
| 788 | // The start and end values of the IV should be known. This is to guarantee |
| 789 | // that truncating the wide type will not lose information. |
| 790 | auto *Limit = dyn_cast<SCEVConstant>(LatchCheck.Limit); |
| 791 | auto *Start = dyn_cast<SCEVConstant>(LatchCheck.IV->getStart()); |
| 792 | if (!Limit || !Start) |
| 793 | return false; |
| 794 | // This check makes sure that the IV does not change sign during loop |
| 795 | // iterations. Consider latchType = i64, LatchStart = 5, Pred = ICMP_SGE, |
| 796 | // LatchEnd = 2, rangeCheckType = i32. If it's not a monotonic predicate, the |
| 797 | // IV wraps around, and the truncation of the IV would lose the range of |
| 798 | // iterations between 2^32 and 2^64. |
| 799 | bool Increasing; |
| 800 | if (!SE->isMonotonicPredicate(LatchCheck.IV, LatchCheck.Pred, Increasing)) |
| 801 | return false; |
| 802 | // The active bits should be less than the bits in the RangeCheckType. This |
| 803 | // guarantees that truncating the latch check to RangeCheckType is a safe |
| 804 | // operation. |
| 805 | auto RangeCheckTypeBitSize = DL->getTypeSizeInBits(RangeCheckType); |
| 806 | return Start->getAPInt().getActiveBits() < RangeCheckTypeBitSize && |
| 807 | Limit->getAPInt().getActiveBits() < RangeCheckTypeBitSize; |
| 808 | } |
| 809 | |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 810 | bool LoopPredication::isLoopProfitableToPredicate() { |
| 811 | if (SkipProfitabilityChecks || !BPI) |
| 812 | return true; |
| 813 | |
| 814 | SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 8> ExitEdges; |
| 815 | L->getExitEdges(ExitEdges); |
| 816 | // If there is only one exiting edge in the loop, it is always profitable to |
| 817 | // predicate the loop. |
| 818 | if (ExitEdges.size() == 1) |
| 819 | return true; |
| 820 | |
| 821 | // Calculate the exiting probabilities of all exiting edges from the loop, |
| 822 | // starting with the LatchExitProbability. |
| 823 | // Heuristic for profitability: If any of the exiting blocks' probability of |
| 824 | // exiting the loop is larger than exiting through the latch block, it's not |
| 825 | // profitable to predicate the loop. |
| 826 | auto *LatchBlock = L->getLoopLatch(); |
| 827 | assert(LatchBlock && "Should have a single latch at this point!"); |
| 828 | auto *LatchTerm = LatchBlock->getTerminator(); |
| 829 | assert(LatchTerm->getNumSuccessors() == 2 && |
| 830 | "expected to be an exiting block with 2 succs!"); |
| 831 | unsigned LatchBrExitIdx = |
| 832 | LatchTerm->getSuccessor(0) == L->getHeader() ? 1 : 0; |
| 833 | BranchProbability LatchExitProbability = |
| 834 | BPI->getEdgeProbability(LatchBlock, LatchBrExitIdx); |
| 835 | |
| 836 | // Protect against degenerate inputs provided by the user. Providing a value |
| 837 | // less than one, can invert the definition of profitable loop predication. |
| 838 | float ScaleFactor = LatchExitProbabilityScale; |
| 839 | if (ScaleFactor < 1) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 840 | LLVM_DEBUG( |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 841 | dbgs() |
| 842 | << "Ignored user setting for loop-predication-latch-probability-scale: " |
| 843 | << LatchExitProbabilityScale << "\n"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 844 | LLVM_DEBUG(dbgs() << "The value is set to 1.0\n"); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 845 | ScaleFactor = 1.0; |
| 846 | } |
| 847 | const auto LatchProbabilityThreshold = |
| 848 | LatchExitProbability * ScaleFactor; |
| 849 | |
| 850 | for (const auto &ExitEdge : ExitEdges) { |
| 851 | BranchProbability ExitingBlockProbability = |
| 852 | BPI->getEdgeProbability(ExitEdge.first, ExitEdge.second); |
| 853 | // Some exiting edge has higher probability than the latch exiting edge. |
| 854 | // No longer profitable to predicate. |
| 855 | if (ExitingBlockProbability > LatchProbabilityThreshold) |
| 856 | return false; |
| 857 | } |
| 858 | // Using BPI, we have concluded that the most probable way to exit from the |
| 859 | // loop is through the latch (or there's no profile information and all |
| 860 | // exits are equally likely). |
| 861 | return true; |
| 862 | } |
| 863 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 864 | bool LoopPredication::runOnLoop(Loop *Loop) { |
| 865 | L = Loop; |
| 866 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 867 | LLVM_DEBUG(dbgs() << "Analyzing "); |
| 868 | LLVM_DEBUG(L->dump()); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 869 | |
| 870 | Module *M = L->getHeader()->getModule(); |
| 871 | |
| 872 | // There is nothing to do if the module doesn't use guards |
| 873 | auto *GuardDecl = |
| 874 | M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard)); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 875 | bool HasIntrinsicGuards = GuardDecl && !GuardDecl->use_empty(); |
| 876 | auto *WCDecl = M->getFunction( |
| 877 | Intrinsic::getName(Intrinsic::experimental_widenable_condition)); |
| 878 | bool HasWidenableConditions = |
| 879 | PredicateWidenableBranchGuards && WCDecl && !WCDecl->use_empty(); |
| 880 | if (!HasIntrinsicGuards && !HasWidenableConditions) |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 881 | return false; |
| 882 | |
| 883 | DL = &M->getDataLayout(); |
| 884 | |
| 885 | Preheader = L->getLoopPreheader(); |
| 886 | if (!Preheader) |
| 887 | return false; |
| 888 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 889 | auto LatchCheckOpt = parseLoopLatchICmp(); |
| 890 | if (!LatchCheckOpt) |
| 891 | return false; |
| 892 | LatchCheck = *LatchCheckOpt; |
| 893 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 894 | LLVM_DEBUG(dbgs() << "Latch check:\n"); |
| 895 | LLVM_DEBUG(LatchCheck.dump()); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 896 | |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 897 | if (!isLoopProfitableToPredicate()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 898 | LLVM_DEBUG(dbgs() << "Loop not profitable to predicate!\n"); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 899 | return false; |
| 900 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 901 | // Collect all the guards into a vector and process later, so as not |
| 902 | // to invalidate the instruction iterator. |
| 903 | SmallVector<IntrinsicInst *, 4> Guards; |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 904 | SmallVector<BranchInst *, 4> GuardsAsWidenableBranches; |
| 905 | for (const auto BB : L->blocks()) { |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 906 | for (auto &I : *BB) |
Max Kazantsev | 28298e9 | 2018-12-26 08:22:25 +0000 | [diff] [blame] | 907 | if (isGuard(&I)) |
| 908 | Guards.push_back(cast<IntrinsicInst>(&I)); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 909 | if (PredicateWidenableBranchGuards && |
| 910 | isGuardAsWidenableBranch(BB->getTerminator())) |
| 911 | GuardsAsWidenableBranches.push_back( |
| 912 | cast<BranchInst>(BB->getTerminator())); |
| 913 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 914 | |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 915 | if (Guards.empty() && GuardsAsWidenableBranches.empty()) |
Artur Pilipenko | 46c4e0a | 2017-05-19 13:59:34 +0000 | [diff] [blame] | 916 | return false; |
| 917 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 918 | SCEVExpander Expander(*SE, *DL, "loop-predication"); |
| 919 | |
| 920 | bool Changed = false; |
| 921 | for (auto *Guard : Guards) |
| 922 | Changed |= widenGuardConditions(Guard, Expander); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 923 | for (auto *Guard : GuardsAsWidenableBranches) |
| 924 | Changed |= widenWidenableBranchGuardConditions(Guard, Expander); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 925 | |
| 926 | return Changed; |
| 927 | } |