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" |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 181 | #include "llvm/Analysis/AliasAnalysis.h" |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 182 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Max Kazantsev | 28298e9 | 2018-12-26 08:22:25 +0000 | [diff] [blame] | 183 | #include "llvm/Analysis/GuardUtils.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 184 | #include "llvm/Analysis/LoopInfo.h" |
| 185 | #include "llvm/Analysis/LoopPass.h" |
| 186 | #include "llvm/Analysis/ScalarEvolution.h" |
| 187 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 188 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 189 | #include "llvm/IR/Function.h" |
| 190 | #include "llvm/IR/GlobalValue.h" |
| 191 | #include "llvm/IR/IntrinsicInst.h" |
| 192 | #include "llvm/IR/Module.h" |
| 193 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 194 | #include "llvm/Pass.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 195 | #include "llvm/Support/Debug.h" |
| 196 | #include "llvm/Transforms/Scalar.h" |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 197 | #include "llvm/Transforms/Utils/Local.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 198 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 199 | |
| 200 | #define DEBUG_TYPE "loop-predication" |
| 201 | |
Fedor Sergeev | c297e84 | 2018-10-17 09:02:54 +0000 | [diff] [blame] | 202 | STATISTIC(TotalConsidered, "Number of guards considered"); |
| 203 | STATISTIC(TotalWidened, "Number of checks widened"); |
| 204 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 205 | using namespace llvm; |
| 206 | |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 207 | static cl::opt<bool> EnableIVTruncation("loop-predication-enable-iv-truncation", |
| 208 | cl::Hidden, cl::init(true)); |
| 209 | |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 210 | static cl::opt<bool> EnableCountDownLoop("loop-predication-enable-count-down-loop", |
| 211 | cl::Hidden, cl::init(true)); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 212 | |
| 213 | static cl::opt<bool> |
| 214 | SkipProfitabilityChecks("loop-predication-skip-profitability-checks", |
| 215 | cl::Hidden, cl::init(false)); |
| 216 | |
| 217 | // This is the scale factor for the latch probability. We use this during |
| 218 | // profitability analysis to find other exiting blocks that have a much higher |
| 219 | // probability of exiting the loop instead of loop exiting via latch. |
| 220 | // This value should be greater than 1 for a sane profitability check. |
| 221 | static cl::opt<float> LatchExitProbabilityScale( |
| 222 | "loop-predication-latch-probability-scale", cl::Hidden, cl::init(2.0), |
| 223 | cl::desc("scale factor for the latch probability. Value should be greater " |
| 224 | "than 1. Lower values are ignored")); |
| 225 | |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 226 | static cl::opt<bool> PredicateWidenableBranchGuards( |
| 227 | "loop-predication-predicate-widenable-branches-to-deopt", cl::Hidden, |
| 228 | cl::desc("Whether or not we should predicate guards " |
| 229 | "expressed as widenable branches to deoptimize blocks"), |
| 230 | cl::init(true)); |
| 231 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 232 | namespace { |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +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; |
| 239 | LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV, |
| 240 | const SCEV *Limit) |
| 241 | : Pred(Pred), IV(IV), Limit(Limit) {} |
| 242 | LoopICmp() {} |
| 243 | void dump() { |
| 244 | dbgs() << "LoopICmp Pred = " << Pred << ", IV = " << *IV |
| 245 | << ", Limit = " << *Limit << "\n"; |
| 246 | } |
| 247 | }; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 248 | |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 249 | class LoopPredication { |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 250 | AliasAnalysis *AA; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 251 | ScalarEvolution *SE; |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 252 | BranchProbabilityInfo *BPI; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 253 | |
| 254 | Loop *L; |
| 255 | const DataLayout *DL; |
| 256 | BasicBlock *Preheader; |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 257 | LoopICmp LatchCheck; |
Artur Pilipenko | c488dfa | 2017-05-22 12:01:32 +0000 | [diff] [blame] | 258 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 259 | bool isSupportedStep(const SCEV* Step); |
Philip Reames | 19afdf7 | 2019-06-01 03:09:28 +0000 | [diff] [blame] | 260 | Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 261 | Optional<LoopICmp> parseLoopLatchICmp(); |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 262 | |
Philip Reames | fbe64a2 | 2019-04-15 15:53:25 +0000 | [diff] [blame] | 263 | /// Return an insertion point suitable for inserting a safe to speculate |
| 264 | /// instruction whose only user will be 'User' which has operands 'Ops'. A |
| 265 | /// trivial result would be the at the User itself, but we try to return a |
| 266 | /// loop invariant location if possible. |
| 267 | Instruction *findInsertPt(Instruction *User, ArrayRef<Value*> Ops); |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 268 | /// Same as above, *except* that this uses the SCEV definition of invariant |
| 269 | /// which is that an expression *can be made* invariant via SCEVExpander. |
| 270 | /// Thus, this version is only suitable for finding an insert point to be be |
| 271 | /// passed to SCEVExpander! |
| 272 | Instruction *findInsertPt(Instruction *User, ArrayRef<const SCEV*> Ops); |
Philip Reames | fbe64a2 | 2019-04-15 15:53:25 +0000 | [diff] [blame] | 273 | |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 274 | /// Return true if the value is known to produce a single fixed value across |
| 275 | /// all iterations on which it executes. Note that this does not imply |
| 276 | /// speculation safety. That must be established seperately. |
| 277 | bool isLoopInvariantValue(const SCEV* S); |
| 278 | |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 279 | Value *expandCheck(SCEVExpander &Expander, Instruction *Guard, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 280 | ICmpInst::Predicate Pred, const SCEV *LHS, |
| 281 | const SCEV *RHS); |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 282 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 283 | Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 284 | Instruction *Guard); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 285 | Optional<Value *> widenICmpRangeCheckIncrementingLoop(LoopICmp LatchCheck, |
| 286 | LoopICmp RangeCheck, |
| 287 | SCEVExpander &Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 288 | Instruction *Guard); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 289 | Optional<Value *> widenICmpRangeCheckDecrementingLoop(LoopICmp LatchCheck, |
| 290 | LoopICmp RangeCheck, |
| 291 | SCEVExpander &Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 292 | Instruction *Guard); |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 293 | unsigned collectChecks(SmallVectorImpl<Value *> &Checks, Value *Condition, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 294 | SCEVExpander &Expander, Instruction *Guard); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 295 | bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 296 | bool widenWidenableBranchGuardConditions(BranchInst *Guard, SCEVExpander &Expander); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 297 | // If the loop always exits through another block in the loop, we should not |
| 298 | // predicate based on the latch check. For example, the latch check can be a |
| 299 | // very coarse grained check and there can be more fine grained exit checks |
| 300 | // within the loop. We identify such unprofitable loops through BPI. |
| 301 | bool isLoopProfitableToPredicate(); |
| 302 | |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 303 | // Return the loopLatchCheck corresponding to the RangeCheckType if safe to do |
| 304 | // so. |
| 305 | Optional<LoopICmp> generateLoopLatchCheck(Type *RangeCheckType); |
Serguei Katkov | ebc9031 | 2018-02-07 06:53:37 +0000 | [diff] [blame] | 306 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 307 | public: |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 308 | LoopPredication(AliasAnalysis *AA, ScalarEvolution *SE, |
| 309 | BranchProbabilityInfo *BPI) |
| 310 | : AA(AA), SE(SE), BPI(BPI){}; |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 311 | bool runOnLoop(Loop *L); |
| 312 | }; |
| 313 | |
| 314 | class LoopPredicationLegacyPass : public LoopPass { |
| 315 | public: |
| 316 | static char ID; |
| 317 | LoopPredicationLegacyPass() : LoopPass(ID) { |
| 318 | initializeLoopPredicationLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 319 | } |
| 320 | |
| 321 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 322 | AU.addRequired<BranchProbabilityInfoWrapperPass>(); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 323 | getLoopAnalysisUsage(AU); |
| 324 | } |
| 325 | |
| 326 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { |
| 327 | if (skipLoop(L)) |
| 328 | return false; |
| 329 | auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 330 | BranchProbabilityInfo &BPI = |
| 331 | getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI(); |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 332 | auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 333 | LoopPredication LP(AA, SE, &BPI); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 334 | return LP.runOnLoop(L); |
| 335 | } |
| 336 | }; |
| 337 | |
| 338 | char LoopPredicationLegacyPass::ID = 0; |
| 339 | } // end namespace llvm |
| 340 | |
| 341 | INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication", |
| 342 | "Loop predication", false, false) |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 343 | INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 344 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 345 | INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication", |
| 346 | "Loop predication", false, false) |
| 347 | |
| 348 | Pass *llvm::createLoopPredicationPass() { |
| 349 | return new LoopPredicationLegacyPass(); |
| 350 | } |
| 351 | |
| 352 | PreservedAnalyses LoopPredicationPass::run(Loop &L, LoopAnalysisManager &AM, |
| 353 | LoopStandardAnalysisResults &AR, |
| 354 | LPMUpdater &U) { |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 355 | const auto &FAM = |
| 356 | AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager(); |
| 357 | Function *F = L.getHeader()->getParent(); |
| 358 | auto *BPI = FAM.getCachedResult<BranchProbabilityAnalysis>(*F); |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 359 | LoopPredication LP(&AR.AA, &AR.SE, BPI); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 360 | if (!LP.runOnLoop(&L)) |
| 361 | return PreservedAnalyses::all(); |
| 362 | |
| 363 | return getLoopPassPreservedAnalyses(); |
| 364 | } |
| 365 | |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 366 | Optional<LoopICmp> |
Philip Reames | 19afdf7 | 2019-06-01 03:09:28 +0000 | [diff] [blame] | 367 | LoopPredication::parseLoopICmp(ICmpInst *ICI) { |
| 368 | auto Pred = ICI->getPredicate(); |
| 369 | auto *LHS = ICI->getOperand(0); |
| 370 | auto *RHS = ICI->getOperand(1); |
| 371 | |
Artur Pilipenko | a6c27804 | 2017-05-19 14:02:46 +0000 | [diff] [blame] | 372 | const SCEV *LHSS = SE->getSCEV(LHS); |
| 373 | if (isa<SCEVCouldNotCompute>(LHSS)) |
| 374 | return None; |
| 375 | const SCEV *RHSS = SE->getSCEV(RHS); |
| 376 | if (isa<SCEVCouldNotCompute>(RHSS)) |
| 377 | return None; |
| 378 | |
| 379 | // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV |
| 380 | if (SE->isLoopInvariant(LHSS, L)) { |
| 381 | std::swap(LHS, RHS); |
| 382 | std::swap(LHSS, RHSS); |
| 383 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 384 | } |
| 385 | |
| 386 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS); |
| 387 | if (!AR || AR->getLoop() != L) |
| 388 | return None; |
| 389 | |
| 390 | return LoopICmp(Pred, AR, RHSS); |
| 391 | } |
| 392 | |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 393 | Value *LoopPredication::expandCheck(SCEVExpander &Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 394 | Instruction *Guard, |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 395 | ICmpInst::Predicate Pred, const SCEV *LHS, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 396 | const SCEV *RHS) { |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 397 | Type *Ty = LHS->getType(); |
| 398 | assert(Ty == RHS->getType() && "expandCheck operands have different types?"); |
Artur Pilipenko | ead69ee | 2017-10-12 21:21:17 +0000 | [diff] [blame] | 399 | |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 400 | if (SE->isLoopInvariant(LHS, L) && SE->isLoopInvariant(RHS, L)) { |
| 401 | IRBuilder<> Builder(Guard); |
| 402 | if (SE->isLoopEntryGuardedByCond(L, Pred, LHS, RHS)) |
| 403 | return Builder.getTrue(); |
| 404 | if (SE->isLoopEntryGuardedByCond(L, ICmpInst::getInversePredicate(Pred), |
| 405 | LHS, RHS)) |
| 406 | return Builder.getFalse(); |
| 407 | } |
Artur Pilipenko | ead69ee | 2017-10-12 21:21:17 +0000 | [diff] [blame] | 408 | |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 409 | Value *LHSV = Expander.expandCodeFor(LHS, Ty, findInsertPt(Guard, {LHS})); |
| 410 | Value *RHSV = Expander.expandCodeFor(RHS, Ty, findInsertPt(Guard, {RHS})); |
| 411 | IRBuilder<> Builder(findInsertPt(Guard, {LHSV, RHSV})); |
Artur Pilipenko | 6780ba6 | 2017-05-19 14:00:58 +0000 | [diff] [blame] | 412 | return Builder.CreateICmp(Pred, LHSV, RHSV); |
| 413 | } |
| 414 | |
Philip Reames | 0912b06 | 2019-06-03 16:17:14 +0000 | [diff] [blame^] | 415 | |
| 416 | // Returns true if its safe to truncate the IV to RangeCheckType. |
| 417 | // When the IV type is wider than the range operand type, we can still do loop |
| 418 | // predication, by generating SCEVs for the range and latch that are of the |
| 419 | // same type. We achieve this by generating a SCEV truncate expression for the |
| 420 | // latch IV. This is done iff truncation of the IV is a safe operation, |
| 421 | // without loss of information. |
| 422 | // Another way to achieve this is by generating a wider type SCEV for the |
| 423 | // range check operand, however, this needs a more involved check that |
| 424 | // operands do not overflow. This can lead to loss of information when the |
| 425 | // range operand is of the form: add i32 %offset, %iv. We need to prove that |
| 426 | // sext(x + y) is same as sext(x) + sext(y). |
| 427 | // This function returns true if we can safely represent the IV type in |
| 428 | // the RangeCheckType without loss of information. |
| 429 | bool isSafeToTruncateWideIVType(const DataLayout &DL, ScalarEvolution &SE, |
| 430 | const LoopICmp LatchCheck, |
| 431 | Type *RangeCheckType) { |
| 432 | if (!EnableIVTruncation) |
| 433 | return false; |
| 434 | assert(DL.getTypeSizeInBits(LatchCheck.IV->getType()) > |
| 435 | DL.getTypeSizeInBits(RangeCheckType) && |
| 436 | "Expected latch check IV type to be larger than range check operand " |
| 437 | "type!"); |
| 438 | // The start and end values of the IV should be known. This is to guarantee |
| 439 | // that truncating the wide type will not lose information. |
| 440 | auto *Limit = dyn_cast<SCEVConstant>(LatchCheck.Limit); |
| 441 | auto *Start = dyn_cast<SCEVConstant>(LatchCheck.IV->getStart()); |
| 442 | if (!Limit || !Start) |
| 443 | return false; |
| 444 | // This check makes sure that the IV does not change sign during loop |
| 445 | // iterations. Consider latchType = i64, LatchStart = 5, Pred = ICMP_SGE, |
| 446 | // LatchEnd = 2, rangeCheckType = i32. If it's not a monotonic predicate, the |
| 447 | // IV wraps around, and the truncation of the IV would lose the range of |
| 448 | // iterations between 2^32 and 2^64. |
| 449 | bool Increasing; |
| 450 | if (!SE.isMonotonicPredicate(LatchCheck.IV, LatchCheck.Pred, Increasing)) |
| 451 | return false; |
| 452 | // The active bits should be less than the bits in the RangeCheckType. This |
| 453 | // guarantees that truncating the latch check to RangeCheckType is a safe |
| 454 | // operation. |
| 455 | auto RangeCheckTypeBitSize = DL.getTypeSizeInBits(RangeCheckType); |
| 456 | return Start->getAPInt().getActiveBits() < RangeCheckTypeBitSize && |
| 457 | Limit->getAPInt().getActiveBits() < RangeCheckTypeBitSize; |
| 458 | } |
| 459 | |
| 460 | |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 461 | Optional<LoopICmp> |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 462 | LoopPredication::generateLoopLatchCheck(Type *RangeCheckType) { |
| 463 | |
| 464 | auto *LatchType = LatchCheck.IV->getType(); |
| 465 | if (RangeCheckType == LatchType) |
| 466 | return LatchCheck; |
| 467 | // For now, bail out if latch type is narrower than range type. |
| 468 | if (DL->getTypeSizeInBits(LatchType) < DL->getTypeSizeInBits(RangeCheckType)) |
| 469 | return None; |
Philip Reames | 0912b06 | 2019-06-03 16:17:14 +0000 | [diff] [blame^] | 470 | if (!isSafeToTruncateWideIVType(*DL, *SE, LatchCheck, RangeCheckType)) |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 471 | return None; |
| 472 | // We can now safely identify the truncated version of the IV and limit for |
| 473 | // RangeCheckType. |
| 474 | LoopICmp NewLatchCheck; |
| 475 | NewLatchCheck.Pred = LatchCheck.Pred; |
| 476 | NewLatchCheck.IV = dyn_cast<SCEVAddRecExpr>( |
| 477 | SE->getTruncateExpr(LatchCheck.IV, RangeCheckType)); |
| 478 | if (!NewLatchCheck.IV) |
| 479 | return None; |
| 480 | NewLatchCheck.Limit = SE->getTruncateExpr(LatchCheck.Limit, RangeCheckType); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 481 | LLVM_DEBUG(dbgs() << "IV of type: " << *LatchType |
| 482 | << "can be represented as range check type:" |
| 483 | << *RangeCheckType << "\n"); |
| 484 | LLVM_DEBUG(dbgs() << "LatchCheck.IV: " << *NewLatchCheck.IV << "\n"); |
| 485 | LLVM_DEBUG(dbgs() << "LatchCheck.Limit: " << *NewLatchCheck.Limit << "\n"); |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 486 | return NewLatchCheck; |
| 487 | } |
| 488 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 489 | bool LoopPredication::isSupportedStep(const SCEV* Step) { |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 490 | return Step->isOne() || (Step->isAllOnesValue() && EnableCountDownLoop); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 491 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 492 | |
Philip Reames | fbe64a2 | 2019-04-15 15:53:25 +0000 | [diff] [blame] | 493 | Instruction *LoopPredication::findInsertPt(Instruction *Use, |
| 494 | ArrayRef<Value*> Ops) { |
| 495 | for (Value *Op : Ops) |
| 496 | if (!L->isLoopInvariant(Op)) |
| 497 | return Use; |
| 498 | return Preheader->getTerminator(); |
| 499 | } |
| 500 | |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 501 | Instruction *LoopPredication::findInsertPt(Instruction *Use, |
| 502 | ArrayRef<const SCEV*> Ops) { |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 503 | // Subtlety: SCEV considers things to be invariant if the value produced is |
| 504 | // the same across iterations. This is not the same as being able to |
| 505 | // evaluate outside the loop, which is what we actually need here. |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 506 | for (const SCEV *Op : Ops) |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 507 | if (!SE->isLoopInvariant(Op, L) || |
| 508 | !isSafeToExpandAt(Op, Preheader->getTerminator(), *SE)) |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 509 | return Use; |
| 510 | return Preheader->getTerminator(); |
| 511 | } |
| 512 | |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 513 | bool LoopPredication::isLoopInvariantValue(const SCEV* S) { |
| 514 | // Handling expressions which produce invariant results, but *haven't* yet |
| 515 | // been removed from the loop serves two important purposes. |
| 516 | // 1) Most importantly, it resolves a pass ordering cycle which would |
| 517 | // otherwise need us to iteration licm, loop-predication, and either |
| 518 | // loop-unswitch or loop-peeling to make progress on examples with lots of |
| 519 | // predicable range checks in a row. (Since, in the general case, we can't |
| 520 | // hoist the length checks until the dominating checks have been discharged |
| 521 | // as we can't prove doing so is safe.) |
| 522 | // 2) As a nice side effect, this exposes the value of peeling or unswitching |
| 523 | // much more obviously in the IR. Otherwise, the cost modeling for other |
| 524 | // transforms would end up needing to duplicate all of this logic to model a |
| 525 | // check which becomes predictable based on a modeled peel or unswitch. |
| 526 | // |
| 527 | // The cost of doing so in the worst case is an extra fill from the stack in |
| 528 | // the loop to materialize the loop invariant test value instead of checking |
| 529 | // against the original IV which is presumable in a register inside the loop. |
| 530 | // Such cases are presumably rare, and hint at missing oppurtunities for |
| 531 | // other passes. |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 532 | |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 533 | if (SE->isLoopInvariant(S, L)) |
| 534 | // Note: This the SCEV variant, so the original Value* may be within the |
| 535 | // loop even though SCEV has proven it is loop invariant. |
| 536 | return true; |
| 537 | |
| 538 | // Handle a particular important case which SCEV doesn't yet know about which |
| 539 | // shows up in range checks on arrays with immutable lengths. |
| 540 | // TODO: This should be sunk inside SCEV. |
| 541 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) |
| 542 | if (const auto *LI = dyn_cast<LoadInst>(U->getValue())) |
Philip Reames | adf288c | 2019-04-18 17:01:19 +0000 | [diff] [blame] | 543 | if (LI->isUnordered() && L->hasLoopInvariantOperands(LI)) |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 544 | if (AA->pointsToConstantMemory(LI->getOperand(0)) || |
| 545 | LI->getMetadata(LLVMContext::MD_invariant_load) != nullptr) |
| 546 | return true; |
| 547 | return false; |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 548 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 549 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 550 | Optional<Value *> LoopPredication::widenICmpRangeCheckIncrementingLoop( |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 551 | LoopICmp LatchCheck, LoopICmp RangeCheck, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 552 | SCEVExpander &Expander, Instruction *Guard) { |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 553 | auto *Ty = RangeCheck.IV->getType(); |
| 554 | // Generate the widened condition for the forward loop: |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 555 | // guardStart u< guardLimit && |
| 556 | // latchLimit <pred> guardLimit - 1 - guardStart + latchStart |
Artur Pilipenko | b4527e1 | 2017-10-12 20:40:27 +0000 | [diff] [blame] | 557 | // where <pred> depends on the latch condition predicate. See the file |
| 558 | // header comment for the reasoning. |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 559 | // guardLimit - guardStart + latchStart - 1 |
| 560 | const SCEV *GuardStart = RangeCheck.IV->getStart(); |
| 561 | const SCEV *GuardLimit = RangeCheck.Limit; |
| 562 | const SCEV *LatchStart = LatchCheck.IV->getStart(); |
| 563 | const SCEV *LatchLimit = LatchCheck.Limit; |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 564 | // Subtlety: We need all the values to be *invariant* across all iterations, |
| 565 | // but we only need to check expansion safety for those which *aren't* |
| 566 | // already guaranteed to dominate the guard. |
| 567 | if (!isLoopInvariantValue(GuardStart) || |
| 568 | !isLoopInvariantValue(GuardLimit) || |
| 569 | !isLoopInvariantValue(LatchStart) || |
| 570 | !isLoopInvariantValue(LatchLimit)) { |
| 571 | LLVM_DEBUG(dbgs() << "Can't expand limit check!\n"); |
| 572 | return None; |
| 573 | } |
| 574 | if (!isSafeToExpandAt(LatchStart, Guard, *SE) || |
| 575 | !isSafeToExpandAt(LatchLimit, Guard, *SE)) { |
| 576 | LLVM_DEBUG(dbgs() << "Can't expand limit check!\n"); |
| 577 | return None; |
| 578 | } |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 579 | |
| 580 | // guardLimit - guardStart + latchStart - 1 |
| 581 | const SCEV *RHS = |
| 582 | SE->getAddExpr(SE->getMinusSCEV(GuardLimit, GuardStart), |
| 583 | SE->getMinusSCEV(LatchStart, SE->getOne(Ty))); |
Serguei Katkov | 3cb4c34 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 584 | auto LimitCheckPred = |
| 585 | ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred); |
Artur Pilipenko | aab2866 | 2017-05-19 14:00:04 +0000 | [diff] [blame] | 586 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 587 | LLVM_DEBUG(dbgs() << "LHS: " << *LatchLimit << "\n"); |
| 588 | LLVM_DEBUG(dbgs() << "RHS: " << *RHS << "\n"); |
| 589 | LLVM_DEBUG(dbgs() << "Pred: " << LimitCheckPred << "\n"); |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 590 | |
Artur Pilipenko | 8aadc64 | 2017-10-27 14:46:17 +0000 | [diff] [blame] | 591 | auto *LimitCheck = |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 592 | expandCheck(Expander, Guard, LimitCheckPred, LatchLimit, RHS); |
| 593 | auto *FirstIterationCheck = expandCheck(Expander, Guard, RangeCheck.Pred, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 594 | GuardStart, GuardLimit); |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 595 | IRBuilder<> Builder(findInsertPt(Guard, {FirstIterationCheck, LimitCheck})); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 596 | return Builder.CreateAnd(FirstIterationCheck, LimitCheck); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 597 | } |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 598 | |
| 599 | Optional<Value *> LoopPredication::widenICmpRangeCheckDecrementingLoop( |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 600 | LoopICmp LatchCheck, LoopICmp RangeCheck, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 601 | SCEVExpander &Expander, Instruction *Guard) { |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 602 | auto *Ty = RangeCheck.IV->getType(); |
| 603 | const SCEV *GuardStart = RangeCheck.IV->getStart(); |
| 604 | const SCEV *GuardLimit = RangeCheck.Limit; |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 605 | const SCEV *LatchStart = LatchCheck.IV->getStart(); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 606 | const SCEV *LatchLimit = LatchCheck.Limit; |
Philip Reames | 92a7177 | 2019-04-18 16:33:17 +0000 | [diff] [blame] | 607 | // Subtlety: We need all the values to be *invariant* across all iterations, |
| 608 | // but we only need to check expansion safety for those which *aren't* |
| 609 | // already guaranteed to dominate the guard. |
| 610 | if (!isLoopInvariantValue(GuardStart) || |
| 611 | !isLoopInvariantValue(GuardLimit) || |
| 612 | !isLoopInvariantValue(LatchStart) || |
| 613 | !isLoopInvariantValue(LatchLimit)) { |
| 614 | LLVM_DEBUG(dbgs() << "Can't expand limit check!\n"); |
| 615 | return None; |
| 616 | } |
| 617 | if (!isSafeToExpandAt(LatchStart, Guard, *SE) || |
| 618 | !isSafeToExpandAt(LatchLimit, Guard, *SE)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 619 | LLVM_DEBUG(dbgs() << "Can't expand limit check!\n"); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 620 | return None; |
| 621 | } |
| 622 | // The decrement of the latch check IV should be the same as the |
| 623 | // rangeCheckIV. |
| 624 | auto *PostDecLatchCheckIV = LatchCheck.IV->getPostIncExpr(*SE); |
| 625 | if (RangeCheck.IV != PostDecLatchCheckIV) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 626 | LLVM_DEBUG(dbgs() << "Not the same. PostDecLatchCheckIV: " |
| 627 | << *PostDecLatchCheckIV |
| 628 | << " and RangeCheckIV: " << *RangeCheck.IV << "\n"); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 629 | return None; |
| 630 | } |
| 631 | |
| 632 | // Generate the widened condition for CountDownLoop: |
| 633 | // guardStart u< guardLimit && |
| 634 | // latchLimit <pred> 1. |
| 635 | // See the header comment for reasoning of the checks. |
Serguei Katkov | 3cb4c34 | 2018-02-09 07:59:07 +0000 | [diff] [blame] | 636 | auto LimitCheckPred = |
| 637 | ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred); |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 638 | auto *FirstIterationCheck = expandCheck(Expander, Guard, |
| 639 | ICmpInst::ICMP_ULT, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 640 | GuardStart, GuardLimit); |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 641 | auto *LimitCheck = expandCheck(Expander, Guard, LimitCheckPred, LatchLimit, |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 642 | SE->getOne(Ty)); |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 643 | IRBuilder<> Builder(findInsertPt(Guard, {FirstIterationCheck, LimitCheck})); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 644 | return Builder.CreateAnd(FirstIterationCheck, LimitCheck); |
| 645 | } |
| 646 | |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 647 | static void normalizePredicate(ScalarEvolution *SE, Loop *L, |
| 648 | LoopICmp& RC) { |
| 649 | // LFTR canonicalizes checks to the ICMP_NE form instead of an ULT/SLT form. |
| 650 | // Normalize back to the ULT/SLT form for ease of handling. |
| 651 | if (RC.Pred == ICmpInst::ICMP_NE && |
| 652 | RC.IV->getStepRecurrence(*SE)->isOne() && |
| 653 | SE->isKnownPredicate(ICmpInst::ICMP_ULE, RC.IV->getStart(), RC.Limit)) |
| 654 | RC.Pred = ICmpInst::ICMP_ULT; |
| 655 | } |
| 656 | |
| 657 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 658 | /// If ICI can be widened to a loop invariant condition emits the loop |
| 659 | /// invariant condition in the loop preheader and return it, otherwise |
| 660 | /// returns None. |
| 661 | Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI, |
| 662 | SCEVExpander &Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 663 | Instruction *Guard) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 664 | LLVM_DEBUG(dbgs() << "Analyzing ICmpInst condition:\n"); |
| 665 | LLVM_DEBUG(ICI->dump()); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 666 | |
| 667 | // parseLoopStructure guarantees that the latch condition is: |
| 668 | // ++i <pred> latchLimit, where <pred> is u<, u<=, s<, or s<=. |
| 669 | // We are looking for the range checks of the form: |
| 670 | // i u< guardLimit |
| 671 | auto RangeCheck = parseLoopICmp(ICI); |
| 672 | if (!RangeCheck) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 673 | LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 674 | return None; |
| 675 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 676 | LLVM_DEBUG(dbgs() << "Guard check:\n"); |
| 677 | LLVM_DEBUG(RangeCheck->dump()); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 678 | if (RangeCheck->Pred != ICmpInst::ICMP_ULT) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 679 | LLVM_DEBUG(dbgs() << "Unsupported range check predicate(" |
| 680 | << RangeCheck->Pred << ")!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 681 | return None; |
| 682 | } |
| 683 | auto *RangeCheckIV = RangeCheck->IV; |
| 684 | if (!RangeCheckIV->isAffine()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 685 | LLVM_DEBUG(dbgs() << "Range check IV is not affine!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 686 | return None; |
| 687 | } |
| 688 | auto *Step = RangeCheckIV->getStepRecurrence(*SE); |
| 689 | // We cannot just compare with latch IV step because the latch and range IVs |
| 690 | // may have different types. |
| 691 | if (!isSupportedStep(Step)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 692 | LLVM_DEBUG(dbgs() << "Range check and latch have IVs different steps!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 693 | return None; |
| 694 | } |
| 695 | auto *Ty = RangeCheckIV->getType(); |
| 696 | auto CurrLatchCheckOpt = generateLoopLatchCheck(Ty); |
| 697 | if (!CurrLatchCheckOpt) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 698 | LLVM_DEBUG(dbgs() << "Failed to generate a loop latch check " |
| 699 | "corresponding to range type: " |
| 700 | << *Ty << "\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 701 | return None; |
| 702 | } |
| 703 | |
| 704 | LoopICmp CurrLatchCheck = *CurrLatchCheckOpt; |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 705 | // At this point, the range and latch step should have the same type, but need |
| 706 | // not have the same value (we support both 1 and -1 steps). |
| 707 | assert(Step->getType() == |
| 708 | CurrLatchCheck.IV->getStepRecurrence(*SE)->getType() && |
| 709 | "Range and latch steps should be of same type!"); |
| 710 | if (Step != CurrLatchCheck.IV->getStepRecurrence(*SE)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 711 | LLVM_DEBUG(dbgs() << "Range and latch have different step values!\n"); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 712 | return None; |
| 713 | } |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 714 | |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 715 | if (Step->isOne()) |
| 716 | return widenICmpRangeCheckIncrementingLoop(CurrLatchCheck, *RangeCheck, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 717 | Expander, Guard); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 718 | else { |
| 719 | assert(Step->isAllOnesValue() && "Step should be -1!"); |
| 720 | return widenICmpRangeCheckDecrementingLoop(CurrLatchCheck, *RangeCheck, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 721 | Expander, Guard); |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 722 | } |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 723 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 724 | |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 725 | unsigned LoopPredication::collectChecks(SmallVectorImpl<Value *> &Checks, |
| 726 | Value *Condition, |
| 727 | SCEVExpander &Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 728 | Instruction *Guard) { |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 729 | unsigned NumWidened = 0; |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 730 | // The guard condition is expected to be in form of: |
| 731 | // cond1 && cond2 && cond3 ... |
Hiroshi Inoue | 0909ca1 | 2018-01-26 08:15:29 +0000 | [diff] [blame] | 732 | // Iterate over subconditions looking for icmp conditions which can be |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 733 | // widened across loop iterations. Widening these conditions remember the |
| 734 | // resulting list of subconditions in Checks vector. |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 735 | SmallVector<Value *, 4> Worklist(1, Condition); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 736 | SmallPtrSet<Value *, 4> Visited; |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 737 | Value *WideableCond = nullptr; |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 738 | do { |
| 739 | Value *Condition = Worklist.pop_back_val(); |
| 740 | if (!Visited.insert(Condition).second) |
| 741 | continue; |
| 742 | |
| 743 | Value *LHS, *RHS; |
| 744 | using namespace llvm::PatternMatch; |
| 745 | if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) { |
| 746 | Worklist.push_back(LHS); |
| 747 | Worklist.push_back(RHS); |
| 748 | continue; |
| 749 | } |
| 750 | |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 751 | if (match(Condition, |
| 752 | m_Intrinsic<Intrinsic::experimental_widenable_condition>())) { |
| 753 | // Pick any, we don't care which |
| 754 | WideableCond = Condition; |
| 755 | continue; |
| 756 | } |
| 757 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 758 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) { |
Philip Reames | 3d4e108 | 2019-03-29 23:06:57 +0000 | [diff] [blame] | 759 | if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 760 | Guard)) { |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 761 | Checks.push_back(NewRangeCheck.getValue()); |
| 762 | NumWidened++; |
| 763 | continue; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | // Save the condition as is if we can't widen it |
| 768 | Checks.push_back(Condition); |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 769 | } while (!Worklist.empty()); |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 770 | // At the moment, our matching logic for wideable conditions implicitly |
| 771 | // assumes we preserve the form: (br (and Cond, WC())). FIXME |
| 772 | // Note that if there were multiple calls to wideable condition in the |
| 773 | // traversal, we only need to keep one, and which one is arbitrary. |
| 774 | if (WideableCond) |
| 775 | Checks.push_back(WideableCond); |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 776 | return NumWidened; |
| 777 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 778 | |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 779 | bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard, |
| 780 | SCEVExpander &Expander) { |
| 781 | LLVM_DEBUG(dbgs() << "Processing guard:\n"); |
| 782 | LLVM_DEBUG(Guard->dump()); |
| 783 | |
| 784 | TotalConsidered++; |
| 785 | SmallVector<Value *, 4> Checks; |
Max Kazantsev | ca45087 | 2019-01-22 10:13:36 +0000 | [diff] [blame] | 786 | unsigned NumWidened = collectChecks(Checks, Guard->getOperand(0), Expander, |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 787 | Guard); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 788 | if (NumWidened == 0) |
| 789 | return false; |
| 790 | |
Fedor Sergeev | c297e84 | 2018-10-17 09:02:54 +0000 | [diff] [blame] | 791 | TotalWidened += NumWidened; |
| 792 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 793 | // Emit the new guard condition |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 794 | IRBuilder<> Builder(findInsertPt(Guard, Checks)); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 795 | Value *LastCheck = nullptr; |
| 796 | for (auto *Check : Checks) |
| 797 | if (!LastCheck) |
| 798 | LastCheck = Check; |
| 799 | else |
| 800 | LastCheck = Builder.CreateAnd(LastCheck, Check); |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 801 | auto *OldCond = Guard->getOperand(0); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 802 | Guard->setOperand(0, LastCheck); |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 803 | RecursivelyDeleteTriviallyDeadInstructions(OldCond); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 804 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 805 | LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n"); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 806 | return true; |
| 807 | } |
| 808 | |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 809 | bool LoopPredication::widenWidenableBranchGuardConditions( |
Philip Reames | f608678 | 2019-04-01 22:39:54 +0000 | [diff] [blame] | 810 | BranchInst *BI, SCEVExpander &Expander) { |
| 811 | assert(isGuardAsWidenableBranch(BI) && "Must be!"); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 812 | LLVM_DEBUG(dbgs() << "Processing guard:\n"); |
Philip Reames | f608678 | 2019-04-01 22:39:54 +0000 | [diff] [blame] | 813 | LLVM_DEBUG(BI->dump()); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 814 | |
| 815 | TotalConsidered++; |
| 816 | SmallVector<Value *, 4> Checks; |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 817 | unsigned NumWidened = collectChecks(Checks, BI->getCondition(), |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 818 | Expander, BI); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 819 | if (NumWidened == 0) |
| 820 | return false; |
| 821 | |
| 822 | TotalWidened += NumWidened; |
| 823 | |
| 824 | // Emit the new guard condition |
Philip Reames | e46d77d | 2019-04-15 18:15:08 +0000 | [diff] [blame] | 825 | IRBuilder<> Builder(findInsertPt(BI, Checks)); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 826 | Value *LastCheck = nullptr; |
| 827 | for (auto *Check : Checks) |
| 828 | if (!LastCheck) |
| 829 | LastCheck = Check; |
| 830 | else |
| 831 | LastCheck = Builder.CreateAnd(LastCheck, Check); |
Philip Reames | adb3ece | 2019-04-02 02:42:57 +0000 | [diff] [blame] | 832 | auto *OldCond = BI->getCondition(); |
| 833 | BI->setCondition(LastCheck); |
Philip Reames | f608678 | 2019-04-01 22:39:54 +0000 | [diff] [blame] | 834 | assert(isGuardAsWidenableBranch(BI) && |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 835 | "Stopped being a guard after transform?"); |
Philip Reames | d109e2a | 2019-04-01 16:05:15 +0000 | [diff] [blame] | 836 | RecursivelyDeleteTriviallyDeadInstructions(OldCond); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 837 | |
| 838 | LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n"); |
| 839 | return true; |
| 840 | } |
| 841 | |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 842 | Optional<LoopICmp> LoopPredication::parseLoopLatchICmp() { |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 843 | using namespace PatternMatch; |
| 844 | |
| 845 | BasicBlock *LoopLatch = L->getLoopLatch(); |
| 846 | if (!LoopLatch) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 847 | LLVM_DEBUG(dbgs() << "The loop doesn't have a single latch!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 848 | return None; |
| 849 | } |
| 850 | |
Philip Reames | 19afdf7 | 2019-06-01 03:09:28 +0000 | [diff] [blame] | 851 | auto *BI = dyn_cast<BranchInst>(LoopLatch->getTerminator()); |
| 852 | if (!BI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 853 | LLVM_DEBUG(dbgs() << "Failed to match the latch terminator!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 854 | return None; |
| 855 | } |
Philip Reames | 19afdf7 | 2019-06-01 03:09:28 +0000 | [diff] [blame] | 856 | BasicBlock *TrueDest = BI->getSuccessor(0); |
Richard Trieu | 4e87546 | 2019-06-01 03:32:20 +0000 | [diff] [blame] | 857 | assert( |
| 858 | (TrueDest == L->getHeader() || BI->getSuccessor(1) == L->getHeader()) && |
| 859 | "One of the latch's destinations must be the header"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 860 | |
Philip Reames | 19afdf7 | 2019-06-01 03:09:28 +0000 | [diff] [blame] | 861 | auto *ICI = dyn_cast<ICmpInst>(BI->getCondition()); |
| 862 | if (!ICI || !BI->isConditional()) { |
| 863 | LLVM_DEBUG(dbgs() << "Failed to match the latch condition!\n"); |
| 864 | return None; |
| 865 | } |
| 866 | auto Result = parseLoopICmp(ICI); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 867 | if (!Result) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 868 | LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 869 | return None; |
| 870 | } |
| 871 | |
Philip Reames | 19afdf7 | 2019-06-01 03:09:28 +0000 | [diff] [blame] | 872 | if (TrueDest != L->getHeader()) |
| 873 | Result->Pred = ICmpInst::getInversePredicate(Result->Pred); |
| 874 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 875 | // Check affine first, so if it's not we don't try to compute the step |
| 876 | // recurrence. |
| 877 | if (!Result->IV->isAffine()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 878 | LLVM_DEBUG(dbgs() << "The induction variable is not affine!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 879 | return None; |
| 880 | } |
| 881 | |
| 882 | auto *Step = Result->IV->getStepRecurrence(*SE); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 883 | if (!isSupportedStep(Step)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 884 | LLVM_DEBUG(dbgs() << "Unsupported loop stride(" << *Step << ")!\n"); |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 885 | return None; |
| 886 | } |
| 887 | |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 888 | auto IsUnsupportedPredicate = [](const SCEV *Step, ICmpInst::Predicate Pred) { |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 889 | if (Step->isOne()) { |
| 890 | return Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_SLT && |
| 891 | Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_SLE; |
| 892 | } else { |
| 893 | assert(Step->isAllOnesValue() && "Step should be -1!"); |
Serguei Katkov | c8016e7 | 2018-02-08 10:34:08 +0000 | [diff] [blame] | 894 | return Pred != ICmpInst::ICMP_UGT && Pred != ICmpInst::ICMP_SGT && |
| 895 | Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_SGE; |
Anna Thomas | 7b36043 | 2017-12-04 15:11:48 +0000 | [diff] [blame] | 896 | } |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 897 | }; |
| 898 | |
Philip Reames | 099eca8 | 2019-06-01 00:31:58 +0000 | [diff] [blame] | 899 | normalizePredicate(SE, L, *Result); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 900 | if (IsUnsupportedPredicate(Step, Result->Pred)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 901 | LLVM_DEBUG(dbgs() << "Unsupported loop latch predicate(" << Result->Pred |
| 902 | << ")!\n"); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 903 | return None; |
| 904 | } |
Philip Reames | 19afdf7 | 2019-06-01 03:09:28 +0000 | [diff] [blame] | 905 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 906 | return Result; |
| 907 | } |
| 908 | |
Anna Thomas | 1d02b13 | 2017-11-02 21:21:02 +0000 | [diff] [blame] | 909 | |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 910 | bool LoopPredication::isLoopProfitableToPredicate() { |
| 911 | if (SkipProfitabilityChecks || !BPI) |
| 912 | return true; |
| 913 | |
| 914 | SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 8> ExitEdges; |
| 915 | L->getExitEdges(ExitEdges); |
| 916 | // If there is only one exiting edge in the loop, it is always profitable to |
| 917 | // predicate the loop. |
| 918 | if (ExitEdges.size() == 1) |
| 919 | return true; |
| 920 | |
| 921 | // Calculate the exiting probabilities of all exiting edges from the loop, |
| 922 | // starting with the LatchExitProbability. |
| 923 | // Heuristic for profitability: If any of the exiting blocks' probability of |
| 924 | // exiting the loop is larger than exiting through the latch block, it's not |
| 925 | // profitable to predicate the loop. |
| 926 | auto *LatchBlock = L->getLoopLatch(); |
| 927 | assert(LatchBlock && "Should have a single latch at this point!"); |
| 928 | auto *LatchTerm = LatchBlock->getTerminator(); |
| 929 | assert(LatchTerm->getNumSuccessors() == 2 && |
| 930 | "expected to be an exiting block with 2 succs!"); |
| 931 | unsigned LatchBrExitIdx = |
| 932 | LatchTerm->getSuccessor(0) == L->getHeader() ? 1 : 0; |
| 933 | BranchProbability LatchExitProbability = |
| 934 | BPI->getEdgeProbability(LatchBlock, LatchBrExitIdx); |
| 935 | |
| 936 | // Protect against degenerate inputs provided by the user. Providing a value |
| 937 | // less than one, can invert the definition of profitable loop predication. |
| 938 | float ScaleFactor = LatchExitProbabilityScale; |
| 939 | if (ScaleFactor < 1) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 940 | LLVM_DEBUG( |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 941 | dbgs() |
| 942 | << "Ignored user setting for loop-predication-latch-probability-scale: " |
| 943 | << LatchExitProbabilityScale << "\n"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 944 | LLVM_DEBUG(dbgs() << "The value is set to 1.0\n"); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 945 | ScaleFactor = 1.0; |
| 946 | } |
| 947 | const auto LatchProbabilityThreshold = |
| 948 | LatchExitProbability * ScaleFactor; |
| 949 | |
| 950 | for (const auto &ExitEdge : ExitEdges) { |
| 951 | BranchProbability ExitingBlockProbability = |
| 952 | BPI->getEdgeProbability(ExitEdge.first, ExitEdge.second); |
| 953 | // Some exiting edge has higher probability than the latch exiting edge. |
| 954 | // No longer profitable to predicate. |
| 955 | if (ExitingBlockProbability > LatchProbabilityThreshold) |
| 956 | return false; |
| 957 | } |
| 958 | // Using BPI, we have concluded that the most probable way to exit from the |
| 959 | // loop is through the latch (or there's no profile information and all |
| 960 | // exits are equally likely). |
| 961 | return true; |
| 962 | } |
| 963 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 964 | bool LoopPredication::runOnLoop(Loop *Loop) { |
| 965 | L = Loop; |
| 966 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 967 | LLVM_DEBUG(dbgs() << "Analyzing "); |
| 968 | LLVM_DEBUG(L->dump()); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 969 | |
| 970 | Module *M = L->getHeader()->getModule(); |
| 971 | |
| 972 | // There is nothing to do if the module doesn't use guards |
| 973 | auto *GuardDecl = |
| 974 | M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard)); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 975 | bool HasIntrinsicGuards = GuardDecl && !GuardDecl->use_empty(); |
| 976 | auto *WCDecl = M->getFunction( |
| 977 | Intrinsic::getName(Intrinsic::experimental_widenable_condition)); |
| 978 | bool HasWidenableConditions = |
| 979 | PredicateWidenableBranchGuards && WCDecl && !WCDecl->use_empty(); |
| 980 | if (!HasIntrinsicGuards && !HasWidenableConditions) |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 981 | return false; |
| 982 | |
| 983 | DL = &M->getDataLayout(); |
| 984 | |
| 985 | Preheader = L->getLoopPreheader(); |
| 986 | if (!Preheader) |
| 987 | return false; |
| 988 | |
Artur Pilipenko | 889dc1e | 2017-09-22 13:13:57 +0000 | [diff] [blame] | 989 | auto LatchCheckOpt = parseLoopLatchICmp(); |
| 990 | if (!LatchCheckOpt) |
| 991 | return false; |
| 992 | LatchCheck = *LatchCheckOpt; |
| 993 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 994 | LLVM_DEBUG(dbgs() << "Latch check:\n"); |
| 995 | LLVM_DEBUG(LatchCheck.dump()); |
Anna Thomas | 6879721 | 2017-11-03 14:25:39 +0000 | [diff] [blame] | 996 | |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 997 | if (!isLoopProfitableToPredicate()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 998 | LLVM_DEBUG(dbgs() << "Loop not profitable to predicate!\n"); |
Anna Thomas | 9b1176b | 2018-03-22 16:03:59 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1001 | // Collect all the guards into a vector and process later, so as not |
| 1002 | // to invalidate the instruction iterator. |
| 1003 | SmallVector<IntrinsicInst *, 4> Guards; |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 1004 | SmallVector<BranchInst *, 4> GuardsAsWidenableBranches; |
| 1005 | for (const auto BB : L->blocks()) { |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1006 | for (auto &I : *BB) |
Max Kazantsev | 28298e9 | 2018-12-26 08:22:25 +0000 | [diff] [blame] | 1007 | if (isGuard(&I)) |
| 1008 | Guards.push_back(cast<IntrinsicInst>(&I)); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 1009 | if (PredicateWidenableBranchGuards && |
| 1010 | isGuardAsWidenableBranch(BB->getTerminator())) |
| 1011 | GuardsAsWidenableBranches.push_back( |
| 1012 | cast<BranchInst>(BB->getTerminator())); |
| 1013 | } |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1014 | |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 1015 | if (Guards.empty() && GuardsAsWidenableBranches.empty()) |
Artur Pilipenko | 46c4e0a | 2017-05-19 13:59:34 +0000 | [diff] [blame] | 1016 | return false; |
| 1017 | |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1018 | SCEVExpander Expander(*SE, *DL, "loop-predication"); |
| 1019 | |
| 1020 | bool Changed = false; |
| 1021 | for (auto *Guard : Guards) |
| 1022 | Changed |= widenGuardConditions(Guard, Expander); |
Max Kazantsev | feb475f | 2019-01-22 11:49:06 +0000 | [diff] [blame] | 1023 | for (auto *Guard : GuardsAsWidenableBranches) |
| 1024 | Changed |= widenWidenableBranchGuardConditions(Guard, Expander); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1025 | |
| 1026 | return Changed; |
| 1027 | } |