18fb3d57eSArtur Pilipenko //===-- LoopPredication.cpp - Guard based loop predication pass -----------===//
28fb3d57eSArtur Pilipenko //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68fb3d57eSArtur Pilipenko //
78fb3d57eSArtur Pilipenko //===----------------------------------------------------------------------===//
88fb3d57eSArtur Pilipenko //
98fb3d57eSArtur Pilipenko // The LoopPredication pass tries to convert loop variant range checks to loop
108fb3d57eSArtur Pilipenko // invariant by widening checks across loop iterations. For example, it will
118fb3d57eSArtur Pilipenko // convert
128fb3d57eSArtur Pilipenko //
138fb3d57eSArtur Pilipenko //   for (i = 0; i < n; i++) {
148fb3d57eSArtur Pilipenko //     guard(i < len);
158fb3d57eSArtur Pilipenko //     ...
168fb3d57eSArtur Pilipenko //   }
178fb3d57eSArtur Pilipenko //
188fb3d57eSArtur Pilipenko // to
198fb3d57eSArtur Pilipenko //
208fb3d57eSArtur Pilipenko //   for (i = 0; i < n; i++) {
218fb3d57eSArtur Pilipenko //     guard(n - 1 < len);
228fb3d57eSArtur Pilipenko //     ...
238fb3d57eSArtur Pilipenko //   }
248fb3d57eSArtur Pilipenko //
258fb3d57eSArtur Pilipenko // After this transformation the condition of the guard is loop invariant, so
268fb3d57eSArtur Pilipenko // loop-unswitch can later unswitch the loop by this condition which basically
278fb3d57eSArtur Pilipenko // predicates the loop by the widened condition:
288fb3d57eSArtur Pilipenko //
298fb3d57eSArtur Pilipenko //   if (n - 1 < len)
308fb3d57eSArtur Pilipenko //     for (i = 0; i < n; i++) {
318fb3d57eSArtur Pilipenko //       ...
328fb3d57eSArtur Pilipenko //     }
338fb3d57eSArtur Pilipenko //   else
348fb3d57eSArtur Pilipenko //     deoptimize
358fb3d57eSArtur Pilipenko //
36889dc1e3SArtur Pilipenko // It's tempting to rely on SCEV here, but it has proven to be problematic.
37889dc1e3SArtur Pilipenko // Generally the facts SCEV provides about the increment step of add
38889dc1e3SArtur Pilipenko // recurrences are true if the backedge of the loop is taken, which implicitly
39889dc1e3SArtur Pilipenko // assumes that the guard doesn't fail. Using these facts to optimize the
40889dc1e3SArtur Pilipenko // guard results in a circular logic where the guard is optimized under the
41889dc1e3SArtur Pilipenko // assumption that it never fails.
42889dc1e3SArtur Pilipenko //
43889dc1e3SArtur Pilipenko // For example, in the loop below the induction variable will be marked as nuw
44889dc1e3SArtur Pilipenko // basing on the guard. Basing on nuw the guard predicate will be considered
45889dc1e3SArtur Pilipenko // monotonic. Given a monotonic condition it's tempting to replace the induction
46889dc1e3SArtur Pilipenko // variable in the condition with its value on the last iteration. But this
47889dc1e3SArtur Pilipenko // transformation is not correct, e.g. e = 4, b = 5 breaks the loop.
48889dc1e3SArtur Pilipenko //
49889dc1e3SArtur Pilipenko //   for (int i = b; i != e; i++)
50889dc1e3SArtur Pilipenko //     guard(i u< len)
51889dc1e3SArtur Pilipenko //
52889dc1e3SArtur Pilipenko // One of the ways to reason about this problem is to use an inductive proof
53889dc1e3SArtur Pilipenko // approach. Given the loop:
54889dc1e3SArtur Pilipenko //
558aadc643SArtur Pilipenko //   if (B(0)) {
56889dc1e3SArtur Pilipenko //     do {
578aadc643SArtur Pilipenko //       I = PHI(0, I.INC)
58889dc1e3SArtur Pilipenko //       I.INC = I + Step
59889dc1e3SArtur Pilipenko //       guard(G(I));
608aadc643SArtur Pilipenko //     } while (B(I));
61889dc1e3SArtur Pilipenko //   }
62889dc1e3SArtur Pilipenko //
63889dc1e3SArtur Pilipenko // where B(x) and G(x) are predicates that map integers to booleans, we want a
64889dc1e3SArtur Pilipenko // loop invariant expression M such the following program has the same semantics
65889dc1e3SArtur Pilipenko // as the above:
66889dc1e3SArtur Pilipenko //
678aadc643SArtur Pilipenko //   if (B(0)) {
68889dc1e3SArtur Pilipenko //     do {
698aadc643SArtur Pilipenko //       I = PHI(0, I.INC)
70889dc1e3SArtur Pilipenko //       I.INC = I + Step
718aadc643SArtur Pilipenko //       guard(G(0) && M);
728aadc643SArtur Pilipenko //     } while (B(I));
73889dc1e3SArtur Pilipenko //   }
74889dc1e3SArtur Pilipenko //
758aadc643SArtur Pilipenko // One solution for M is M = forall X . (G(X) && B(X)) => G(X + Step)
76889dc1e3SArtur Pilipenko //
77889dc1e3SArtur Pilipenko // Informal proof that the transformation above is correct:
78889dc1e3SArtur Pilipenko //
79889dc1e3SArtur Pilipenko //   By the definition of guards we can rewrite the guard condition to:
808aadc643SArtur Pilipenko //     G(I) && G(0) && M
81889dc1e3SArtur Pilipenko //
82889dc1e3SArtur Pilipenko //   Let's prove that for each iteration of the loop:
838aadc643SArtur Pilipenko //     G(0) && M => G(I)
84889dc1e3SArtur Pilipenko //   And the condition above can be simplified to G(Start) && M.
85889dc1e3SArtur Pilipenko //
86889dc1e3SArtur Pilipenko //   Induction base.
878aadc643SArtur Pilipenko //     G(0) && M => G(0)
88889dc1e3SArtur Pilipenko //
898aadc643SArtur Pilipenko //   Induction step. Assuming G(0) && M => G(I) on the subsequent
90889dc1e3SArtur Pilipenko //   iteration:
91889dc1e3SArtur Pilipenko //
928aadc643SArtur Pilipenko //     B(I) is true because it's the backedge condition.
93889dc1e3SArtur Pilipenko //     G(I) is true because the backedge is guarded by this condition.
94889dc1e3SArtur Pilipenko //
958aadc643SArtur Pilipenko //   So M = forall X . (G(X) && B(X)) => G(X + Step) implies G(I + Step).
96889dc1e3SArtur Pilipenko //
97889dc1e3SArtur Pilipenko // Note that we can use anything stronger than M, i.e. any condition which
98889dc1e3SArtur Pilipenko // implies M.
99889dc1e3SArtur Pilipenko //
1007b360434SAnna Thomas // When S = 1 (i.e. forward iterating loop), the transformation is supported
1017b360434SAnna Thomas // when:
102b4527e1cSArtur Pilipenko //   * The loop has a single latch with the condition of the form:
1038aadc643SArtur Pilipenko //     B(X) = latchStart + X <pred> latchLimit,
1048aadc643SArtur Pilipenko //     where <pred> is u<, u<=, s<, or s<=.
1058aadc643SArtur Pilipenko //   * The guard condition is of the form
1068aadc643SArtur Pilipenko //     G(X) = guardStart + X u< guardLimit
107889dc1e3SArtur Pilipenko //
108b4527e1cSArtur Pilipenko //   For the ult latch comparison case M is:
1098aadc643SArtur Pilipenko //     forall X . guardStart + X u< guardLimit && latchStart + X <u latchLimit =>
1108aadc643SArtur Pilipenko //        guardStart + X + 1 u< guardLimit
111889dc1e3SArtur Pilipenko //
112889dc1e3SArtur Pilipenko //   The only way the antecedent can be true and the consequent can be false is
113889dc1e3SArtur Pilipenko //   if
1148aadc643SArtur Pilipenko //     X == guardLimit - 1 - guardStart
115889dc1e3SArtur Pilipenko //   (and guardLimit is non-zero, but we won't use this latter fact).
1168aadc643SArtur Pilipenko //   If X == guardLimit - 1 - guardStart then the second half of the antecedent is
1178aadc643SArtur Pilipenko //     latchStart + guardLimit - 1 - guardStart u< latchLimit
118889dc1e3SArtur Pilipenko //   and its negation is
1198aadc643SArtur Pilipenko //     latchStart + guardLimit - 1 - guardStart u>= latchLimit
120889dc1e3SArtur Pilipenko //
1218aadc643SArtur Pilipenko //   In other words, if
1228aadc643SArtur Pilipenko //     latchLimit u<= latchStart + guardLimit - 1 - guardStart
1238aadc643SArtur Pilipenko //   then:
124889dc1e3SArtur Pilipenko //   (the ranges below are written in ConstantRange notation, where [A, B) is the
125889dc1e3SArtur Pilipenko //   set for (I = A; I != B; I++ /*maywrap*/) yield(I);)
126889dc1e3SArtur Pilipenko //
1278aadc643SArtur Pilipenko //      forall X . guardStart + X u< guardLimit &&
1288aadc643SArtur Pilipenko //                 latchStart + X u< latchLimit =>
1298aadc643SArtur Pilipenko //        guardStart + X + 1 u< guardLimit
1308aadc643SArtur Pilipenko //   == forall X . guardStart + X u< guardLimit &&
1318aadc643SArtur Pilipenko //                 latchStart + X u< latchStart + guardLimit - 1 - guardStart =>
1328aadc643SArtur Pilipenko //        guardStart + X + 1 u< guardLimit
1338aadc643SArtur Pilipenko //   == forall X . (guardStart + X) in [0, guardLimit) &&
1348aadc643SArtur Pilipenko //                 (latchStart + X) in [0, latchStart + guardLimit - 1 - guardStart) =>
1358aadc643SArtur Pilipenko //        (guardStart + X + 1) in [0, guardLimit)
1368aadc643SArtur Pilipenko //   == forall X . X in [-guardStart, guardLimit - guardStart) &&
1378aadc643SArtur Pilipenko //                 X in [-latchStart, guardLimit - 1 - guardStart) =>
1388aadc643SArtur Pilipenko //         X in [-guardStart - 1, guardLimit - guardStart - 1)
139889dc1e3SArtur Pilipenko //   == true
140889dc1e3SArtur Pilipenko //
141889dc1e3SArtur Pilipenko //   So the widened condition is:
1428aadc643SArtur Pilipenko //     guardStart u< guardLimit &&
1438aadc643SArtur Pilipenko //     latchStart + guardLimit - 1 - guardStart u>= latchLimit
1448aadc643SArtur Pilipenko //   Similarly for ule condition the widened condition is:
1458aadc643SArtur Pilipenko //     guardStart u< guardLimit &&
1468aadc643SArtur Pilipenko //     latchStart + guardLimit - 1 - guardStart u> latchLimit
1478aadc643SArtur Pilipenko //   For slt condition the widened condition is:
1488aadc643SArtur Pilipenko //     guardStart u< guardLimit &&
1498aadc643SArtur Pilipenko //     latchStart + guardLimit - 1 - guardStart s>= latchLimit
1508aadc643SArtur Pilipenko //   For sle condition the widened condition is:
1518aadc643SArtur Pilipenko //     guardStart u< guardLimit &&
1528aadc643SArtur Pilipenko //     latchStart + guardLimit - 1 - guardStart s> latchLimit
153889dc1e3SArtur Pilipenko //
1547b360434SAnna Thomas // When S = -1 (i.e. reverse iterating loop), the transformation is supported
1557b360434SAnna Thomas // when:
1567b360434SAnna Thomas //   * The loop has a single latch with the condition of the form:
157c8016e7aSSerguei Katkov //     B(X) = X <pred> latchLimit, where <pred> is u>, u>=, s>, or s>=.
1587b360434SAnna Thomas //   * The guard condition is of the form
1597b360434SAnna Thomas //     G(X) = X - 1 u< guardLimit
1607b360434SAnna Thomas //
1617b360434SAnna Thomas //   For the ugt latch comparison case M is:
1627b360434SAnna Thomas //     forall X. X-1 u< guardLimit and X u> latchLimit => X-2 u< guardLimit
1637b360434SAnna Thomas //
1647b360434SAnna Thomas //   The only way the antecedent can be true and the consequent can be false is if
1657b360434SAnna Thomas //     X == 1.
1667b360434SAnna Thomas //   If X == 1 then the second half of the antecedent is
1677b360434SAnna Thomas //     1 u> latchLimit, and its negation is latchLimit u>= 1.
1687b360434SAnna Thomas //
1697b360434SAnna Thomas //   So the widened condition is:
1707b360434SAnna Thomas //     guardStart u< guardLimit && latchLimit u>= 1.
1717b360434SAnna Thomas //   Similarly for sgt condition the widened condition is:
1727b360434SAnna Thomas //     guardStart u< guardLimit && latchLimit s>= 1.
173c8016e7aSSerguei Katkov //   For uge condition the widened condition is:
174c8016e7aSSerguei Katkov //     guardStart u< guardLimit && latchLimit u> 1.
175c8016e7aSSerguei Katkov //   For sge condition the widened condition is:
176c8016e7aSSerguei Katkov //     guardStart u< guardLimit && latchLimit s> 1.
1778fb3d57eSArtur Pilipenko //===----------------------------------------------------------------------===//
1788fb3d57eSArtur Pilipenko 
1798fb3d57eSArtur Pilipenko #include "llvm/Transforms/Scalar/LoopPredication.h"
180c297e84bSFedor Sergeev #include "llvm/ADT/Statistic.h"
1819b1176b0SAnna Thomas #include "llvm/Analysis/BranchProbabilityInfo.h"
18228298e96SMax Kazantsev #include "llvm/Analysis/GuardUtils.h"
1838fb3d57eSArtur Pilipenko #include "llvm/Analysis/LoopInfo.h"
1848fb3d57eSArtur Pilipenko #include "llvm/Analysis/LoopPass.h"
1858fb3d57eSArtur Pilipenko #include "llvm/Analysis/ScalarEvolution.h"
1868fb3d57eSArtur Pilipenko #include "llvm/Analysis/ScalarEvolutionExpander.h"
1878fb3d57eSArtur Pilipenko #include "llvm/Analysis/ScalarEvolutionExpressions.h"
1888fb3d57eSArtur Pilipenko #include "llvm/IR/Function.h"
1898fb3d57eSArtur Pilipenko #include "llvm/IR/GlobalValue.h"
1908fb3d57eSArtur Pilipenko #include "llvm/IR/IntrinsicInst.h"
1918fb3d57eSArtur Pilipenko #include "llvm/IR/Module.h"
1928fb3d57eSArtur Pilipenko #include "llvm/IR/PatternMatch.h"
1936bda14b3SChandler Carruth #include "llvm/Pass.h"
1948fb3d57eSArtur Pilipenko #include "llvm/Support/Debug.h"
1958fb3d57eSArtur Pilipenko #include "llvm/Transforms/Scalar.h"
1968fb3d57eSArtur Pilipenko #include "llvm/Transforms/Utils/LoopUtils.h"
1978fb3d57eSArtur Pilipenko 
1988fb3d57eSArtur Pilipenko #define DEBUG_TYPE "loop-predication"
1998fb3d57eSArtur Pilipenko 
200c297e84bSFedor Sergeev STATISTIC(TotalConsidered, "Number of guards considered");
201c297e84bSFedor Sergeev STATISTIC(TotalWidened, "Number of checks widened");
202c297e84bSFedor Sergeev 
2038fb3d57eSArtur Pilipenko using namespace llvm;
2048fb3d57eSArtur Pilipenko 
2051d02b13eSAnna Thomas static cl::opt<bool> EnableIVTruncation("loop-predication-enable-iv-truncation",
2061d02b13eSAnna Thomas                                         cl::Hidden, cl::init(true));
2071d02b13eSAnna Thomas 
2087b360434SAnna Thomas static cl::opt<bool> EnableCountDownLoop("loop-predication-enable-count-down-loop",
2097b360434SAnna Thomas                                         cl::Hidden, cl::init(true));
2109b1176b0SAnna Thomas 
2119b1176b0SAnna Thomas static cl::opt<bool>
2129b1176b0SAnna Thomas     SkipProfitabilityChecks("loop-predication-skip-profitability-checks",
2139b1176b0SAnna Thomas                             cl::Hidden, cl::init(false));
2149b1176b0SAnna Thomas 
2159b1176b0SAnna Thomas // This is the scale factor for the latch probability. We use this during
2169b1176b0SAnna Thomas // profitability analysis to find other exiting blocks that have a much higher
2179b1176b0SAnna Thomas // probability of exiting the loop instead of loop exiting via latch.
2189b1176b0SAnna Thomas // This value should be greater than 1 for a sane profitability check.
2199b1176b0SAnna Thomas static cl::opt<float> LatchExitProbabilityScale(
2209b1176b0SAnna Thomas     "loop-predication-latch-probability-scale", cl::Hidden, cl::init(2.0),
2219b1176b0SAnna Thomas     cl::desc("scale factor for the latch probability. Value should be greater "
2229b1176b0SAnna Thomas              "than 1. Lower values are ignored"));
2239b1176b0SAnna Thomas 
224feb475f4SMax Kazantsev static cl::opt<bool> PredicateWidenableBranchGuards(
225feb475f4SMax Kazantsev     "loop-predication-predicate-widenable-branches-to-deopt", cl::Hidden,
226feb475f4SMax Kazantsev     cl::desc("Whether or not we should predicate guards "
227feb475f4SMax Kazantsev              "expressed as widenable branches to deoptimize blocks"),
228feb475f4SMax Kazantsev     cl::init(true));
229feb475f4SMax Kazantsev 
2308fb3d57eSArtur Pilipenko namespace {
2318fb3d57eSArtur Pilipenko class LoopPredication {
232a6c27804SArtur Pilipenko   /// Represents an induction variable check:
233a6c27804SArtur Pilipenko   ///   icmp Pred, <induction variable>, <loop invariant limit>
234a6c27804SArtur Pilipenko   struct LoopICmp {
235a6c27804SArtur Pilipenko     ICmpInst::Predicate Pred;
236a6c27804SArtur Pilipenko     const SCEVAddRecExpr *IV;
237a6c27804SArtur Pilipenko     const SCEV *Limit;
238c488dfabSArtur Pilipenko     LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV,
239c488dfabSArtur Pilipenko              const SCEV *Limit)
240a6c27804SArtur Pilipenko         : Pred(Pred), IV(IV), Limit(Limit) {}
241a6c27804SArtur Pilipenko     LoopICmp() {}
24268797214SAnna Thomas     void dump() {
24368797214SAnna Thomas       dbgs() << "LoopICmp Pred = " << Pred << ", IV = " << *IV
24468797214SAnna Thomas              << ", Limit = " << *Limit << "\n";
24568797214SAnna Thomas     }
246a6c27804SArtur Pilipenko   };
247c488dfabSArtur Pilipenko 
248c488dfabSArtur Pilipenko   ScalarEvolution *SE;
2499b1176b0SAnna Thomas   BranchProbabilityInfo *BPI;
250c488dfabSArtur Pilipenko 
251c488dfabSArtur Pilipenko   Loop *L;
252c488dfabSArtur Pilipenko   const DataLayout *DL;
253c488dfabSArtur Pilipenko   BasicBlock *Preheader;
254889dc1e3SArtur Pilipenko   LoopICmp LatchCheck;
255c488dfabSArtur Pilipenko 
25668797214SAnna Thomas   bool isSupportedStep(const SCEV* Step);
257889dc1e3SArtur Pilipenko   Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI) {
258889dc1e3SArtur Pilipenko     return parseLoopICmp(ICI->getPredicate(), ICI->getOperand(0),
259889dc1e3SArtur Pilipenko                          ICI->getOperand(1));
260889dc1e3SArtur Pilipenko   }
261889dc1e3SArtur Pilipenko   Optional<LoopICmp> parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS,
262889dc1e3SArtur Pilipenko                                    Value *RHS);
263889dc1e3SArtur Pilipenko 
264889dc1e3SArtur Pilipenko   Optional<LoopICmp> parseLoopLatchICmp();
265a6c27804SArtur Pilipenko 
26668797214SAnna Thomas   bool CanExpand(const SCEV* S);
2676780ba65SArtur Pilipenko   Value *expandCheck(SCEVExpander &Expander, IRBuilder<> &Builder,
268*3d4e1082SPhilip Reames                      ICmpInst::Predicate Pred, const SCEV *LHS,
269*3d4e1082SPhilip Reames                      const SCEV *RHS);
2706780ba65SArtur Pilipenko 
2718fb3d57eSArtur Pilipenko   Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander,
2728fb3d57eSArtur Pilipenko                                         IRBuilder<> &Builder);
27368797214SAnna Thomas   Optional<Value *> widenICmpRangeCheckIncrementingLoop(LoopICmp LatchCheck,
27468797214SAnna Thomas                                                         LoopICmp RangeCheck,
27568797214SAnna Thomas                                                         SCEVExpander &Expander,
27668797214SAnna Thomas                                                         IRBuilder<> &Builder);
2777b360434SAnna Thomas   Optional<Value *> widenICmpRangeCheckDecrementingLoop(LoopICmp LatchCheck,
2787b360434SAnna Thomas                                                         LoopICmp RangeCheck,
2797b360434SAnna Thomas                                                         SCEVExpander &Expander,
2807b360434SAnna Thomas                                                         IRBuilder<> &Builder);
281ca450878SMax Kazantsev   unsigned collectChecks(SmallVectorImpl<Value *> &Checks, Value *Condition,
282ca450878SMax Kazantsev                          SCEVExpander &Expander, IRBuilder<> &Builder);
2838fb3d57eSArtur Pilipenko   bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander);
284feb475f4SMax Kazantsev   bool widenWidenableBranchGuardConditions(BranchInst *Guard, SCEVExpander &Expander);
2859b1176b0SAnna Thomas   // If the loop always exits through another block in the loop, we should not
2869b1176b0SAnna Thomas   // predicate based on the latch check. For example, the latch check can be a
2879b1176b0SAnna Thomas   // very coarse grained check and there can be more fine grained exit checks
2889b1176b0SAnna Thomas   // within the loop. We identify such unprofitable loops through BPI.
2899b1176b0SAnna Thomas   bool isLoopProfitableToPredicate();
2909b1176b0SAnna Thomas 
2911d02b13eSAnna Thomas   // When the IV type is wider than the range operand type, we can still do loop
2921d02b13eSAnna Thomas   // predication, by generating SCEVs for the range and latch that are of the
2931d02b13eSAnna Thomas   // same type. We achieve this by generating a SCEV truncate expression for the
2941d02b13eSAnna Thomas   // latch IV. This is done iff truncation of the IV is a safe operation,
2951d02b13eSAnna Thomas   // without loss of information.
2961d02b13eSAnna Thomas   // Another way to achieve this is by generating a wider type SCEV for the
2971d02b13eSAnna Thomas   // range check operand, however, this needs a more involved check that
2981d02b13eSAnna Thomas   // operands do not overflow. This can lead to loss of information when the
2991d02b13eSAnna Thomas   // range operand is of the form: add i32 %offset, %iv. We need to prove that
3001d02b13eSAnna Thomas   // sext(x + y) is same as sext(x) + sext(y).
3011d02b13eSAnna Thomas   // This function returns true if we can safely represent the IV type in
3021d02b13eSAnna Thomas   // the RangeCheckType without loss of information.
3031d02b13eSAnna Thomas   bool isSafeToTruncateWideIVType(Type *RangeCheckType);
3041d02b13eSAnna Thomas   // Return the loopLatchCheck corresponding to the RangeCheckType if safe to do
3051d02b13eSAnna Thomas   // so.
3061d02b13eSAnna Thomas   Optional<LoopICmp> generateLoopLatchCheck(Type *RangeCheckType);
307ebc9031bSSerguei Katkov 
3088fb3d57eSArtur Pilipenko public:
3099b1176b0SAnna Thomas   LoopPredication(ScalarEvolution *SE, BranchProbabilityInfo *BPI)
3109b1176b0SAnna Thomas       : SE(SE), BPI(BPI){};
3118fb3d57eSArtur Pilipenko   bool runOnLoop(Loop *L);
3128fb3d57eSArtur Pilipenko };
3138fb3d57eSArtur Pilipenko 
3148fb3d57eSArtur Pilipenko class LoopPredicationLegacyPass : public LoopPass {
3158fb3d57eSArtur Pilipenko public:
3168fb3d57eSArtur Pilipenko   static char ID;
3178fb3d57eSArtur Pilipenko   LoopPredicationLegacyPass() : LoopPass(ID) {
3188fb3d57eSArtur Pilipenko     initializeLoopPredicationLegacyPassPass(*PassRegistry::getPassRegistry());
3198fb3d57eSArtur Pilipenko   }
3208fb3d57eSArtur Pilipenko 
3218fb3d57eSArtur Pilipenko   void getAnalysisUsage(AnalysisUsage &AU) const override {
3229b1176b0SAnna Thomas     AU.addRequired<BranchProbabilityInfoWrapperPass>();
3238fb3d57eSArtur Pilipenko     getLoopAnalysisUsage(AU);
3248fb3d57eSArtur Pilipenko   }
3258fb3d57eSArtur Pilipenko 
3268fb3d57eSArtur Pilipenko   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
3278fb3d57eSArtur Pilipenko     if (skipLoop(L))
3288fb3d57eSArtur Pilipenko       return false;
3298fb3d57eSArtur Pilipenko     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
3309b1176b0SAnna Thomas     BranchProbabilityInfo &BPI =
3319b1176b0SAnna Thomas         getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
3329b1176b0SAnna Thomas     LoopPredication LP(SE, &BPI);
3338fb3d57eSArtur Pilipenko     return LP.runOnLoop(L);
3348fb3d57eSArtur Pilipenko   }
3358fb3d57eSArtur Pilipenko };
3368fb3d57eSArtur Pilipenko 
3378fb3d57eSArtur Pilipenko char LoopPredicationLegacyPass::ID = 0;
3388fb3d57eSArtur Pilipenko } // end namespace llvm
3398fb3d57eSArtur Pilipenko 
3408fb3d57eSArtur Pilipenko INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication",
3418fb3d57eSArtur Pilipenko                       "Loop predication", false, false)
3429b1176b0SAnna Thomas INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
3438fb3d57eSArtur Pilipenko INITIALIZE_PASS_DEPENDENCY(LoopPass)
3448fb3d57eSArtur Pilipenko INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication",
3458fb3d57eSArtur Pilipenko                     "Loop predication", false, false)
3468fb3d57eSArtur Pilipenko 
3478fb3d57eSArtur Pilipenko Pass *llvm::createLoopPredicationPass() {
3488fb3d57eSArtur Pilipenko   return new LoopPredicationLegacyPass();
3498fb3d57eSArtur Pilipenko }
3508fb3d57eSArtur Pilipenko 
3518fb3d57eSArtur Pilipenko PreservedAnalyses LoopPredicationPass::run(Loop &L, LoopAnalysisManager &AM,
3528fb3d57eSArtur Pilipenko                                            LoopStandardAnalysisResults &AR,
3538fb3d57eSArtur Pilipenko                                            LPMUpdater &U) {
3549b1176b0SAnna Thomas   const auto &FAM =
3559b1176b0SAnna Thomas       AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager();
3569b1176b0SAnna Thomas   Function *F = L.getHeader()->getParent();
3579b1176b0SAnna Thomas   auto *BPI = FAM.getCachedResult<BranchProbabilityAnalysis>(*F);
3589b1176b0SAnna Thomas   LoopPredication LP(&AR.SE, BPI);
3598fb3d57eSArtur Pilipenko   if (!LP.runOnLoop(&L))
3608fb3d57eSArtur Pilipenko     return PreservedAnalyses::all();
3618fb3d57eSArtur Pilipenko 
3628fb3d57eSArtur Pilipenko   return getLoopPassPreservedAnalyses();
3638fb3d57eSArtur Pilipenko }
3648fb3d57eSArtur Pilipenko 
365a6c27804SArtur Pilipenko Optional<LoopPredication::LoopICmp>
366889dc1e3SArtur Pilipenko LoopPredication::parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS,
367889dc1e3SArtur Pilipenko                                Value *RHS) {
368a6c27804SArtur Pilipenko   const SCEV *LHSS = SE->getSCEV(LHS);
369a6c27804SArtur Pilipenko   if (isa<SCEVCouldNotCompute>(LHSS))
370a6c27804SArtur Pilipenko     return None;
371a6c27804SArtur Pilipenko   const SCEV *RHSS = SE->getSCEV(RHS);
372a6c27804SArtur Pilipenko   if (isa<SCEVCouldNotCompute>(RHSS))
373a6c27804SArtur Pilipenko     return None;
374a6c27804SArtur Pilipenko 
375a6c27804SArtur Pilipenko   // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV
376a6c27804SArtur Pilipenko   if (SE->isLoopInvariant(LHSS, L)) {
377a6c27804SArtur Pilipenko     std::swap(LHS, RHS);
378a6c27804SArtur Pilipenko     std::swap(LHSS, RHSS);
379a6c27804SArtur Pilipenko     Pred = ICmpInst::getSwappedPredicate(Pred);
380a6c27804SArtur Pilipenko   }
381a6c27804SArtur Pilipenko 
382a6c27804SArtur Pilipenko   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS);
383a6c27804SArtur Pilipenko   if (!AR || AR->getLoop() != L)
384a6c27804SArtur Pilipenko     return None;
385a6c27804SArtur Pilipenko 
386a6c27804SArtur Pilipenko   return LoopICmp(Pred, AR, RHSS);
387a6c27804SArtur Pilipenko }
388a6c27804SArtur Pilipenko 
3896780ba65SArtur Pilipenko Value *LoopPredication::expandCheck(SCEVExpander &Expander,
3906780ba65SArtur Pilipenko                                     IRBuilder<> &Builder,
3916780ba65SArtur Pilipenko                                     ICmpInst::Predicate Pred, const SCEV *LHS,
392*3d4e1082SPhilip Reames                                     const SCEV *RHS) {
393889dc1e3SArtur Pilipenko   // TODO: we can check isLoopEntryGuardedByCond before emitting the check
394889dc1e3SArtur Pilipenko 
3956780ba65SArtur Pilipenko   Type *Ty = LHS->getType();
3966780ba65SArtur Pilipenko   assert(Ty == RHS->getType() && "expandCheck operands have different types?");
397ead69ee4SArtur Pilipenko 
398ead69ee4SArtur Pilipenko   if (SE->isLoopEntryGuardedByCond(L, Pred, LHS, RHS))
399ead69ee4SArtur Pilipenko     return Builder.getTrue();
400ead69ee4SArtur Pilipenko 
401*3d4e1082SPhilip Reames   Instruction *InsertAt = &*Builder.GetInsertPoint();
4026780ba65SArtur Pilipenko   Value *LHSV = Expander.expandCodeFor(LHS, Ty, InsertAt);
4036780ba65SArtur Pilipenko   Value *RHSV = Expander.expandCodeFor(RHS, Ty, InsertAt);
4046780ba65SArtur Pilipenko   return Builder.CreateICmp(Pred, LHSV, RHSV);
4056780ba65SArtur Pilipenko }
4066780ba65SArtur Pilipenko 
4071d02b13eSAnna Thomas Optional<LoopPredication::LoopICmp>
4081d02b13eSAnna Thomas LoopPredication::generateLoopLatchCheck(Type *RangeCheckType) {
4091d02b13eSAnna Thomas 
4101d02b13eSAnna Thomas   auto *LatchType = LatchCheck.IV->getType();
4111d02b13eSAnna Thomas   if (RangeCheckType == LatchType)
4121d02b13eSAnna Thomas     return LatchCheck;
4131d02b13eSAnna Thomas   // For now, bail out if latch type is narrower than range type.
4141d02b13eSAnna Thomas   if (DL->getTypeSizeInBits(LatchType) < DL->getTypeSizeInBits(RangeCheckType))
4151d02b13eSAnna Thomas     return None;
4161d02b13eSAnna Thomas   if (!isSafeToTruncateWideIVType(RangeCheckType))
4171d02b13eSAnna Thomas     return None;
4181d02b13eSAnna Thomas   // We can now safely identify the truncated version of the IV and limit for
4191d02b13eSAnna Thomas   // RangeCheckType.
4201d02b13eSAnna Thomas   LoopICmp NewLatchCheck;
4211d02b13eSAnna Thomas   NewLatchCheck.Pred = LatchCheck.Pred;
4221d02b13eSAnna Thomas   NewLatchCheck.IV = dyn_cast<SCEVAddRecExpr>(
4231d02b13eSAnna Thomas       SE->getTruncateExpr(LatchCheck.IV, RangeCheckType));
4241d02b13eSAnna Thomas   if (!NewLatchCheck.IV)
4251d02b13eSAnna Thomas     return None;
4261d02b13eSAnna Thomas   NewLatchCheck.Limit = SE->getTruncateExpr(LatchCheck.Limit, RangeCheckType);
427d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "IV of type: " << *LatchType
428d34e60caSNicola Zaghen                     << "can be represented as range check type:"
429d34e60caSNicola Zaghen                     << *RangeCheckType << "\n");
430d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "LatchCheck.IV: " << *NewLatchCheck.IV << "\n");
431d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "LatchCheck.Limit: " << *NewLatchCheck.Limit << "\n");
4321d02b13eSAnna Thomas   return NewLatchCheck;
4331d02b13eSAnna Thomas }
4341d02b13eSAnna Thomas 
43568797214SAnna Thomas bool LoopPredication::isSupportedStep(const SCEV* Step) {
4367b360434SAnna Thomas   return Step->isOne() || (Step->isAllOnesValue() && EnableCountDownLoop);
4371d02b13eSAnna Thomas }
4388fb3d57eSArtur Pilipenko 
43968797214SAnna Thomas bool LoopPredication::CanExpand(const SCEV* S) {
44068797214SAnna Thomas   return SE->isLoopInvariant(S, L) && isSafeToExpand(S, *SE);
44168797214SAnna Thomas }
44268797214SAnna Thomas 
44368797214SAnna Thomas Optional<Value *> LoopPredication::widenICmpRangeCheckIncrementingLoop(
44468797214SAnna Thomas     LoopPredication::LoopICmp LatchCheck, LoopPredication::LoopICmp RangeCheck,
44568797214SAnna Thomas     SCEVExpander &Expander, IRBuilder<> &Builder) {
44668797214SAnna Thomas   auto *Ty = RangeCheck.IV->getType();
44768797214SAnna Thomas   // Generate the widened condition for the forward loop:
4488aadc643SArtur Pilipenko   //   guardStart u< guardLimit &&
4498aadc643SArtur Pilipenko   //   latchLimit <pred> guardLimit - 1 - guardStart + latchStart
450b4527e1cSArtur Pilipenko   // where <pred> depends on the latch condition predicate. See the file
451b4527e1cSArtur Pilipenko   // header comment for the reasoning.
45268797214SAnna Thomas   // guardLimit - guardStart + latchStart - 1
45368797214SAnna Thomas   const SCEV *GuardStart = RangeCheck.IV->getStart();
45468797214SAnna Thomas   const SCEV *GuardLimit = RangeCheck.Limit;
45568797214SAnna Thomas   const SCEV *LatchStart = LatchCheck.IV->getStart();
45668797214SAnna Thomas   const SCEV *LatchLimit = LatchCheck.Limit;
4578aadc643SArtur Pilipenko 
4588aadc643SArtur Pilipenko   // guardLimit - guardStart + latchStart - 1
4598aadc643SArtur Pilipenko   const SCEV *RHS =
4608aadc643SArtur Pilipenko       SE->getAddExpr(SE->getMinusSCEV(GuardLimit, GuardStart),
4618aadc643SArtur Pilipenko                      SE->getMinusSCEV(LatchStart, SE->getOne(Ty)));
46268797214SAnna Thomas   if (!CanExpand(GuardStart) || !CanExpand(GuardLimit) ||
46368797214SAnna Thomas       !CanExpand(LatchLimit) || !CanExpand(RHS)) {
464d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
46568797214SAnna Thomas     return None;
46668797214SAnna Thomas   }
4673cb4c34aSSerguei Katkov   auto LimitCheckPred =
4683cb4c34aSSerguei Katkov       ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred);
469aab28666SArtur Pilipenko 
470d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "LHS: " << *LatchLimit << "\n");
471d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "RHS: " << *RHS << "\n");
472d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Pred: " << LimitCheckPred << "\n");
4738aadc643SArtur Pilipenko 
4748aadc643SArtur Pilipenko   auto *LimitCheck =
475*3d4e1082SPhilip Reames       expandCheck(Expander, Builder, LimitCheckPred, LatchLimit, RHS);
47668797214SAnna Thomas   auto *FirstIterationCheck = expandCheck(Expander, Builder, RangeCheck.Pred,
477*3d4e1082SPhilip Reames                                           GuardStart, GuardLimit);
478889dc1e3SArtur Pilipenko   return Builder.CreateAnd(FirstIterationCheck, LimitCheck);
4798fb3d57eSArtur Pilipenko }
4807b360434SAnna Thomas 
4817b360434SAnna Thomas Optional<Value *> LoopPredication::widenICmpRangeCheckDecrementingLoop(
4827b360434SAnna Thomas     LoopPredication::LoopICmp LatchCheck, LoopPredication::LoopICmp RangeCheck,
4837b360434SAnna Thomas     SCEVExpander &Expander, IRBuilder<> &Builder) {
4847b360434SAnna Thomas   auto *Ty = RangeCheck.IV->getType();
4857b360434SAnna Thomas   const SCEV *GuardStart = RangeCheck.IV->getStart();
4867b360434SAnna Thomas   const SCEV *GuardLimit = RangeCheck.Limit;
4877b360434SAnna Thomas   const SCEV *LatchLimit = LatchCheck.Limit;
4887b360434SAnna Thomas   if (!CanExpand(GuardStart) || !CanExpand(GuardLimit) ||
4897b360434SAnna Thomas       !CanExpand(LatchLimit)) {
490d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
4917b360434SAnna Thomas     return None;
4927b360434SAnna Thomas   }
4937b360434SAnna Thomas   // The decrement of the latch check IV should be the same as the
4947b360434SAnna Thomas   // rangeCheckIV.
4957b360434SAnna Thomas   auto *PostDecLatchCheckIV = LatchCheck.IV->getPostIncExpr(*SE);
4967b360434SAnna Thomas   if (RangeCheck.IV != PostDecLatchCheckIV) {
497d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Not the same. PostDecLatchCheckIV: "
4987b360434SAnna Thomas                       << *PostDecLatchCheckIV
4997b360434SAnna Thomas                       << "  and RangeCheckIV: " << *RangeCheck.IV << "\n");
5007b360434SAnna Thomas     return None;
5017b360434SAnna Thomas   }
5027b360434SAnna Thomas 
5037b360434SAnna Thomas   // Generate the widened condition for CountDownLoop:
5047b360434SAnna Thomas   // guardStart u< guardLimit &&
5057b360434SAnna Thomas   // latchLimit <pred> 1.
5067b360434SAnna Thomas   // See the header comment for reasoning of the checks.
5073cb4c34aSSerguei Katkov   auto LimitCheckPred =
5083cb4c34aSSerguei Katkov       ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred);
5097b360434SAnna Thomas   auto *FirstIterationCheck = expandCheck(Expander, Builder, ICmpInst::ICMP_ULT,
510*3d4e1082SPhilip Reames                                           GuardStart, GuardLimit);
5117b360434SAnna Thomas   auto *LimitCheck = expandCheck(Expander, Builder, LimitCheckPred, LatchLimit,
512*3d4e1082SPhilip Reames                                  SE->getOne(Ty));
5137b360434SAnna Thomas   return Builder.CreateAnd(FirstIterationCheck, LimitCheck);
5147b360434SAnna Thomas }
5157b360434SAnna Thomas 
51668797214SAnna Thomas /// If ICI can be widened to a loop invariant condition emits the loop
51768797214SAnna Thomas /// invariant condition in the loop preheader and return it, otherwise
51868797214SAnna Thomas /// returns None.
51968797214SAnna Thomas Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI,
52068797214SAnna Thomas                                                        SCEVExpander &Expander,
52168797214SAnna Thomas                                                        IRBuilder<> &Builder) {
522d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Analyzing ICmpInst condition:\n");
523d34e60caSNicola Zaghen   LLVM_DEBUG(ICI->dump());
52468797214SAnna Thomas 
52568797214SAnna Thomas   // parseLoopStructure guarantees that the latch condition is:
52668797214SAnna Thomas   //   ++i <pred> latchLimit, where <pred> is u<, u<=, s<, or s<=.
52768797214SAnna Thomas   // We are looking for the range checks of the form:
52868797214SAnna Thomas   //   i u< guardLimit
52968797214SAnna Thomas   auto RangeCheck = parseLoopICmp(ICI);
53068797214SAnna Thomas   if (!RangeCheck) {
531d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n");
53268797214SAnna Thomas     return None;
53368797214SAnna Thomas   }
534d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Guard check:\n");
535d34e60caSNicola Zaghen   LLVM_DEBUG(RangeCheck->dump());
53668797214SAnna Thomas   if (RangeCheck->Pred != ICmpInst::ICMP_ULT) {
537d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Unsupported range check predicate("
538d34e60caSNicola Zaghen                       << RangeCheck->Pred << ")!\n");
53968797214SAnna Thomas     return None;
54068797214SAnna Thomas   }
54168797214SAnna Thomas   auto *RangeCheckIV = RangeCheck->IV;
54268797214SAnna Thomas   if (!RangeCheckIV->isAffine()) {
543d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Range check IV is not affine!\n");
54468797214SAnna Thomas     return None;
54568797214SAnna Thomas   }
54668797214SAnna Thomas   auto *Step = RangeCheckIV->getStepRecurrence(*SE);
54768797214SAnna Thomas   // We cannot just compare with latch IV step because the latch and range IVs
54868797214SAnna Thomas   // may have different types.
54968797214SAnna Thomas   if (!isSupportedStep(Step)) {
550d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Range check and latch have IVs different steps!\n");
55168797214SAnna Thomas     return None;
55268797214SAnna Thomas   }
55368797214SAnna Thomas   auto *Ty = RangeCheckIV->getType();
55468797214SAnna Thomas   auto CurrLatchCheckOpt = generateLoopLatchCheck(Ty);
55568797214SAnna Thomas   if (!CurrLatchCheckOpt) {
556d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Failed to generate a loop latch check "
55768797214SAnna Thomas                          "corresponding to range type: "
55868797214SAnna Thomas                       << *Ty << "\n");
55968797214SAnna Thomas     return None;
56068797214SAnna Thomas   }
56168797214SAnna Thomas 
56268797214SAnna Thomas   LoopICmp CurrLatchCheck = *CurrLatchCheckOpt;
5637b360434SAnna Thomas   // At this point, the range and latch step should have the same type, but need
5647b360434SAnna Thomas   // not have the same value (we support both 1 and -1 steps).
5657b360434SAnna Thomas   assert(Step->getType() ==
5667b360434SAnna Thomas              CurrLatchCheck.IV->getStepRecurrence(*SE)->getType() &&
5677b360434SAnna Thomas          "Range and latch steps should be of same type!");
5687b360434SAnna Thomas   if (Step != CurrLatchCheck.IV->getStepRecurrence(*SE)) {
569d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Range and latch have different step values!\n");
5707b360434SAnna Thomas     return None;
5717b360434SAnna Thomas   }
57268797214SAnna Thomas 
5737b360434SAnna Thomas   if (Step->isOne())
57468797214SAnna Thomas     return widenICmpRangeCheckIncrementingLoop(CurrLatchCheck, *RangeCheck,
57568797214SAnna Thomas                                                Expander, Builder);
5767b360434SAnna Thomas   else {
5777b360434SAnna Thomas     assert(Step->isAllOnesValue() && "Step should be -1!");
5787b360434SAnna Thomas     return widenICmpRangeCheckDecrementingLoop(CurrLatchCheck, *RangeCheck,
5797b360434SAnna Thomas                                                Expander, Builder);
5807b360434SAnna Thomas   }
58168797214SAnna Thomas }
5828fb3d57eSArtur Pilipenko 
583ca450878SMax Kazantsev unsigned LoopPredication::collectChecks(SmallVectorImpl<Value *> &Checks,
584ca450878SMax Kazantsev                                         Value *Condition,
585ca450878SMax Kazantsev                                         SCEVExpander &Expander,
586ca450878SMax Kazantsev                                         IRBuilder<> &Builder) {
587ca450878SMax Kazantsev   unsigned NumWidened = 0;
5888fb3d57eSArtur Pilipenko   // The guard condition is expected to be in form of:
5898fb3d57eSArtur Pilipenko   //   cond1 && cond2 && cond3 ...
5900909ca13SHiroshi Inoue   // Iterate over subconditions looking for icmp conditions which can be
5918fb3d57eSArtur Pilipenko   // widened across loop iterations. Widening these conditions remember the
5928fb3d57eSArtur Pilipenko   // resulting list of subconditions in Checks vector.
593ca450878SMax Kazantsev   SmallVector<Value *, 4> Worklist(1, Condition);
5948fb3d57eSArtur Pilipenko   SmallPtrSet<Value *, 4> Visited;
5958fb3d57eSArtur Pilipenko   do {
5968fb3d57eSArtur Pilipenko     Value *Condition = Worklist.pop_back_val();
5978fb3d57eSArtur Pilipenko     if (!Visited.insert(Condition).second)
5988fb3d57eSArtur Pilipenko       continue;
5998fb3d57eSArtur Pilipenko 
6008fb3d57eSArtur Pilipenko     Value *LHS, *RHS;
6018fb3d57eSArtur Pilipenko     using namespace llvm::PatternMatch;
6028fb3d57eSArtur Pilipenko     if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) {
6038fb3d57eSArtur Pilipenko       Worklist.push_back(LHS);
6048fb3d57eSArtur Pilipenko       Worklist.push_back(RHS);
6058fb3d57eSArtur Pilipenko       continue;
6068fb3d57eSArtur Pilipenko     }
6078fb3d57eSArtur Pilipenko 
6088fb3d57eSArtur Pilipenko     if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) {
609*3d4e1082SPhilip Reames       if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander,
610*3d4e1082SPhilip Reames                                                    Builder)) {
6118fb3d57eSArtur Pilipenko         Checks.push_back(NewRangeCheck.getValue());
6128fb3d57eSArtur Pilipenko         NumWidened++;
6138fb3d57eSArtur Pilipenko         continue;
6148fb3d57eSArtur Pilipenko       }
6158fb3d57eSArtur Pilipenko     }
6168fb3d57eSArtur Pilipenko 
6178fb3d57eSArtur Pilipenko     // Save the condition as is if we can't widen it
6188fb3d57eSArtur Pilipenko     Checks.push_back(Condition);
619ca450878SMax Kazantsev   } while (!Worklist.empty());
620ca450878SMax Kazantsev   return NumWidened;
621ca450878SMax Kazantsev }
6228fb3d57eSArtur Pilipenko 
623ca450878SMax Kazantsev bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard,
624ca450878SMax Kazantsev                                            SCEVExpander &Expander) {
625ca450878SMax Kazantsev   LLVM_DEBUG(dbgs() << "Processing guard:\n");
626ca450878SMax Kazantsev   LLVM_DEBUG(Guard->dump());
627ca450878SMax Kazantsev 
628ca450878SMax Kazantsev   TotalConsidered++;
629ca450878SMax Kazantsev   SmallVector<Value *, 4> Checks;
630ca450878SMax Kazantsev   IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator()));
631ca450878SMax Kazantsev   unsigned NumWidened = collectChecks(Checks, Guard->getOperand(0), Expander,
632ca450878SMax Kazantsev                                       Builder);
6338fb3d57eSArtur Pilipenko   if (NumWidened == 0)
6348fb3d57eSArtur Pilipenko     return false;
6358fb3d57eSArtur Pilipenko 
636c297e84bSFedor Sergeev   TotalWidened += NumWidened;
637c297e84bSFedor Sergeev 
6388fb3d57eSArtur Pilipenko   // Emit the new guard condition
6398fb3d57eSArtur Pilipenko   Builder.SetInsertPoint(Guard);
6408fb3d57eSArtur Pilipenko   Value *LastCheck = nullptr;
6418fb3d57eSArtur Pilipenko   for (auto *Check : Checks)
6428fb3d57eSArtur Pilipenko     if (!LastCheck)
6438fb3d57eSArtur Pilipenko       LastCheck = Check;
6448fb3d57eSArtur Pilipenko     else
6458fb3d57eSArtur Pilipenko       LastCheck = Builder.CreateAnd(LastCheck, Check);
6468fb3d57eSArtur Pilipenko   Guard->setOperand(0, LastCheck);
6478fb3d57eSArtur Pilipenko 
648d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n");
6498fb3d57eSArtur Pilipenko   return true;
6508fb3d57eSArtur Pilipenko }
6518fb3d57eSArtur Pilipenko 
652feb475f4SMax Kazantsev bool LoopPredication::widenWidenableBranchGuardConditions(
653feb475f4SMax Kazantsev     BranchInst *Guard, SCEVExpander &Expander) {
654feb475f4SMax Kazantsev   assert(isGuardAsWidenableBranch(Guard) && "Must be!");
655feb475f4SMax Kazantsev   LLVM_DEBUG(dbgs() << "Processing guard:\n");
656feb475f4SMax Kazantsev   LLVM_DEBUG(Guard->dump());
657feb475f4SMax Kazantsev 
658feb475f4SMax Kazantsev   TotalConsidered++;
659feb475f4SMax Kazantsev   SmallVector<Value *, 4> Checks;
660feb475f4SMax Kazantsev   IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator()));
661feb475f4SMax Kazantsev   Value *Condition = nullptr, *WidenableCondition = nullptr;
662feb475f4SMax Kazantsev   BasicBlock *GBB = nullptr, *DBB = nullptr;
663feb475f4SMax Kazantsev   parseWidenableBranch(Guard, Condition, WidenableCondition, GBB, DBB);
664feb475f4SMax Kazantsev   unsigned NumWidened = collectChecks(Checks, Condition, Expander, Builder);
665feb475f4SMax Kazantsev   if (NumWidened == 0)
666feb475f4SMax Kazantsev     return false;
667feb475f4SMax Kazantsev 
668feb475f4SMax Kazantsev   TotalWidened += NumWidened;
669feb475f4SMax Kazantsev 
670feb475f4SMax Kazantsev   // Emit the new guard condition
671feb475f4SMax Kazantsev   Builder.SetInsertPoint(Guard);
672feb475f4SMax Kazantsev   Value *LastCheck = nullptr;
673feb475f4SMax Kazantsev   for (auto *Check : Checks)
674feb475f4SMax Kazantsev     if (!LastCheck)
675feb475f4SMax Kazantsev       LastCheck = Check;
676feb475f4SMax Kazantsev     else
677feb475f4SMax Kazantsev       LastCheck = Builder.CreateAnd(LastCheck, Check);
678feb475f4SMax Kazantsev   // Make sure that the check contains widenable condition and therefore can be
679feb475f4SMax Kazantsev   // further widened.
680feb475f4SMax Kazantsev   LastCheck = Builder.CreateAnd(LastCheck, WidenableCondition);
681feb475f4SMax Kazantsev   Guard->setOperand(0, LastCheck);
682feb475f4SMax Kazantsev   assert(isGuardAsWidenableBranch(Guard) &&
683feb475f4SMax Kazantsev          "Stopped being a guard after transform?");
684feb475f4SMax Kazantsev 
685feb475f4SMax Kazantsev   LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n");
686feb475f4SMax Kazantsev   return true;
687feb475f4SMax Kazantsev }
688feb475f4SMax Kazantsev 
689889dc1e3SArtur Pilipenko Optional<LoopPredication::LoopICmp> LoopPredication::parseLoopLatchICmp() {
690889dc1e3SArtur Pilipenko   using namespace PatternMatch;
691889dc1e3SArtur Pilipenko 
692889dc1e3SArtur Pilipenko   BasicBlock *LoopLatch = L->getLoopLatch();
693889dc1e3SArtur Pilipenko   if (!LoopLatch) {
694d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "The loop doesn't have a single latch!\n");
695889dc1e3SArtur Pilipenko     return None;
696889dc1e3SArtur Pilipenko   }
697889dc1e3SArtur Pilipenko 
698889dc1e3SArtur Pilipenko   ICmpInst::Predicate Pred;
699889dc1e3SArtur Pilipenko   Value *LHS, *RHS;
700889dc1e3SArtur Pilipenko   BasicBlock *TrueDest, *FalseDest;
701889dc1e3SArtur Pilipenko 
702889dc1e3SArtur Pilipenko   if (!match(LoopLatch->getTerminator(),
703889dc1e3SArtur Pilipenko              m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TrueDest,
704889dc1e3SArtur Pilipenko                   FalseDest))) {
705d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Failed to match the latch terminator!\n");
706889dc1e3SArtur Pilipenko     return None;
707889dc1e3SArtur Pilipenko   }
708889dc1e3SArtur Pilipenko   assert((TrueDest == L->getHeader() || FalseDest == L->getHeader()) &&
709889dc1e3SArtur Pilipenko          "One of the latch's destinations must be the header");
710889dc1e3SArtur Pilipenko   if (TrueDest != L->getHeader())
711889dc1e3SArtur Pilipenko     Pred = ICmpInst::getInversePredicate(Pred);
712889dc1e3SArtur Pilipenko 
713889dc1e3SArtur Pilipenko   auto Result = parseLoopICmp(Pred, LHS, RHS);
714889dc1e3SArtur Pilipenko   if (!Result) {
715d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n");
716889dc1e3SArtur Pilipenko     return None;
717889dc1e3SArtur Pilipenko   }
718889dc1e3SArtur Pilipenko 
719889dc1e3SArtur Pilipenko   // Check affine first, so if it's not we don't try to compute the step
720889dc1e3SArtur Pilipenko   // recurrence.
721889dc1e3SArtur Pilipenko   if (!Result->IV->isAffine()) {
722d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "The induction variable is not affine!\n");
723889dc1e3SArtur Pilipenko     return None;
724889dc1e3SArtur Pilipenko   }
725889dc1e3SArtur Pilipenko 
726889dc1e3SArtur Pilipenko   auto *Step = Result->IV->getStepRecurrence(*SE);
72768797214SAnna Thomas   if (!isSupportedStep(Step)) {
728d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Unsupported loop stride(" << *Step << ")!\n");
729889dc1e3SArtur Pilipenko     return None;
730889dc1e3SArtur Pilipenko   }
731889dc1e3SArtur Pilipenko 
73268797214SAnna Thomas   auto IsUnsupportedPredicate = [](const SCEV *Step, ICmpInst::Predicate Pred) {
7337b360434SAnna Thomas     if (Step->isOne()) {
73468797214SAnna Thomas       return Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_SLT &&
73568797214SAnna Thomas              Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_SLE;
7367b360434SAnna Thomas     } else {
7377b360434SAnna Thomas       assert(Step->isAllOnesValue() && "Step should be -1!");
738c8016e7aSSerguei Katkov       return Pred != ICmpInst::ICMP_UGT && Pred != ICmpInst::ICMP_SGT &&
739c8016e7aSSerguei Katkov              Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_SGE;
7407b360434SAnna Thomas     }
74168797214SAnna Thomas   };
74268797214SAnna Thomas 
74368797214SAnna Thomas   if (IsUnsupportedPredicate(Step, Result->Pred)) {
744d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Unsupported loop latch predicate(" << Result->Pred
74568797214SAnna Thomas                       << ")!\n");
74668797214SAnna Thomas     return None;
74768797214SAnna Thomas   }
748889dc1e3SArtur Pilipenko   return Result;
749889dc1e3SArtur Pilipenko }
750889dc1e3SArtur Pilipenko 
7511d02b13eSAnna Thomas // Returns true if its safe to truncate the IV to RangeCheckType.
7521d02b13eSAnna Thomas bool LoopPredication::isSafeToTruncateWideIVType(Type *RangeCheckType) {
7531d02b13eSAnna Thomas   if (!EnableIVTruncation)
7541d02b13eSAnna Thomas     return false;
7551d02b13eSAnna Thomas   assert(DL->getTypeSizeInBits(LatchCheck.IV->getType()) >
7561d02b13eSAnna Thomas              DL->getTypeSizeInBits(RangeCheckType) &&
7571d02b13eSAnna Thomas          "Expected latch check IV type to be larger than range check operand "
7581d02b13eSAnna Thomas          "type!");
7591d02b13eSAnna Thomas   // The start and end values of the IV should be known. This is to guarantee
7601d02b13eSAnna Thomas   // that truncating the wide type will not lose information.
7611d02b13eSAnna Thomas   auto *Limit = dyn_cast<SCEVConstant>(LatchCheck.Limit);
7621d02b13eSAnna Thomas   auto *Start = dyn_cast<SCEVConstant>(LatchCheck.IV->getStart());
7631d02b13eSAnna Thomas   if (!Limit || !Start)
7641d02b13eSAnna Thomas     return false;
7651d02b13eSAnna Thomas   // This check makes sure that the IV does not change sign during loop
7661d02b13eSAnna Thomas   // iterations. Consider latchType = i64, LatchStart = 5, Pred = ICMP_SGE,
7671d02b13eSAnna Thomas   // LatchEnd = 2, rangeCheckType = i32. If it's not a monotonic predicate, the
7681d02b13eSAnna Thomas   // IV wraps around, and the truncation of the IV would lose the range of
7691d02b13eSAnna Thomas   // iterations between 2^32 and 2^64.
7701d02b13eSAnna Thomas   bool Increasing;
7711d02b13eSAnna Thomas   if (!SE->isMonotonicPredicate(LatchCheck.IV, LatchCheck.Pred, Increasing))
7721d02b13eSAnna Thomas     return false;
7731d02b13eSAnna Thomas   // The active bits should be less than the bits in the RangeCheckType. This
7741d02b13eSAnna Thomas   // guarantees that truncating the latch check to RangeCheckType is a safe
7751d02b13eSAnna Thomas   // operation.
7761d02b13eSAnna Thomas   auto RangeCheckTypeBitSize = DL->getTypeSizeInBits(RangeCheckType);
7771d02b13eSAnna Thomas   return Start->getAPInt().getActiveBits() < RangeCheckTypeBitSize &&
7781d02b13eSAnna Thomas          Limit->getAPInt().getActiveBits() < RangeCheckTypeBitSize;
7791d02b13eSAnna Thomas }
7801d02b13eSAnna Thomas 
7819b1176b0SAnna Thomas bool LoopPredication::isLoopProfitableToPredicate() {
7829b1176b0SAnna Thomas   if (SkipProfitabilityChecks || !BPI)
7839b1176b0SAnna Thomas     return true;
7849b1176b0SAnna Thomas 
7859b1176b0SAnna Thomas   SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 8> ExitEdges;
7869b1176b0SAnna Thomas   L->getExitEdges(ExitEdges);
7879b1176b0SAnna Thomas   // If there is only one exiting edge in the loop, it is always profitable to
7889b1176b0SAnna Thomas   // predicate the loop.
7899b1176b0SAnna Thomas   if (ExitEdges.size() == 1)
7909b1176b0SAnna Thomas     return true;
7919b1176b0SAnna Thomas 
7929b1176b0SAnna Thomas   // Calculate the exiting probabilities of all exiting edges from the loop,
7939b1176b0SAnna Thomas   // starting with the LatchExitProbability.
7949b1176b0SAnna Thomas   // Heuristic for profitability: If any of the exiting blocks' probability of
7959b1176b0SAnna Thomas   // exiting the loop is larger than exiting through the latch block, it's not
7969b1176b0SAnna Thomas   // profitable to predicate the loop.
7979b1176b0SAnna Thomas   auto *LatchBlock = L->getLoopLatch();
7989b1176b0SAnna Thomas   assert(LatchBlock && "Should have a single latch at this point!");
7999b1176b0SAnna Thomas   auto *LatchTerm = LatchBlock->getTerminator();
8009b1176b0SAnna Thomas   assert(LatchTerm->getNumSuccessors() == 2 &&
8019b1176b0SAnna Thomas          "expected to be an exiting block with 2 succs!");
8029b1176b0SAnna Thomas   unsigned LatchBrExitIdx =
8039b1176b0SAnna Thomas       LatchTerm->getSuccessor(0) == L->getHeader() ? 1 : 0;
8049b1176b0SAnna Thomas   BranchProbability LatchExitProbability =
8059b1176b0SAnna Thomas       BPI->getEdgeProbability(LatchBlock, LatchBrExitIdx);
8069b1176b0SAnna Thomas 
8079b1176b0SAnna Thomas   // Protect against degenerate inputs provided by the user. Providing a value
8089b1176b0SAnna Thomas   // less than one, can invert the definition of profitable loop predication.
8099b1176b0SAnna Thomas   float ScaleFactor = LatchExitProbabilityScale;
8109b1176b0SAnna Thomas   if (ScaleFactor < 1) {
811d34e60caSNicola Zaghen     LLVM_DEBUG(
8129b1176b0SAnna Thomas         dbgs()
8139b1176b0SAnna Thomas         << "Ignored user setting for loop-predication-latch-probability-scale: "
8149b1176b0SAnna Thomas         << LatchExitProbabilityScale << "\n");
815d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "The value is set to 1.0\n");
8169b1176b0SAnna Thomas     ScaleFactor = 1.0;
8179b1176b0SAnna Thomas   }
8189b1176b0SAnna Thomas   const auto LatchProbabilityThreshold =
8199b1176b0SAnna Thomas       LatchExitProbability * ScaleFactor;
8209b1176b0SAnna Thomas 
8219b1176b0SAnna Thomas   for (const auto &ExitEdge : ExitEdges) {
8229b1176b0SAnna Thomas     BranchProbability ExitingBlockProbability =
8239b1176b0SAnna Thomas         BPI->getEdgeProbability(ExitEdge.first, ExitEdge.second);
8249b1176b0SAnna Thomas     // Some exiting edge has higher probability than the latch exiting edge.
8259b1176b0SAnna Thomas     // No longer profitable to predicate.
8269b1176b0SAnna Thomas     if (ExitingBlockProbability > LatchProbabilityThreshold)
8279b1176b0SAnna Thomas       return false;
8289b1176b0SAnna Thomas   }
8299b1176b0SAnna Thomas   // Using BPI, we have concluded that the most probable way to exit from the
8309b1176b0SAnna Thomas   // loop is through the latch (or there's no profile information and all
8319b1176b0SAnna Thomas   // exits are equally likely).
8329b1176b0SAnna Thomas   return true;
8339b1176b0SAnna Thomas }
8349b1176b0SAnna Thomas 
8358fb3d57eSArtur Pilipenko bool LoopPredication::runOnLoop(Loop *Loop) {
8368fb3d57eSArtur Pilipenko   L = Loop;
8378fb3d57eSArtur Pilipenko 
838d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Analyzing ");
839d34e60caSNicola Zaghen   LLVM_DEBUG(L->dump());
8408fb3d57eSArtur Pilipenko 
8418fb3d57eSArtur Pilipenko   Module *M = L->getHeader()->getModule();
8428fb3d57eSArtur Pilipenko 
8438fb3d57eSArtur Pilipenko   // There is nothing to do if the module doesn't use guards
8448fb3d57eSArtur Pilipenko   auto *GuardDecl =
8458fb3d57eSArtur Pilipenko       M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard));
846feb475f4SMax Kazantsev   bool HasIntrinsicGuards = GuardDecl && !GuardDecl->use_empty();
847feb475f4SMax Kazantsev   auto *WCDecl = M->getFunction(
848feb475f4SMax Kazantsev       Intrinsic::getName(Intrinsic::experimental_widenable_condition));
849feb475f4SMax Kazantsev   bool HasWidenableConditions =
850feb475f4SMax Kazantsev       PredicateWidenableBranchGuards && WCDecl && !WCDecl->use_empty();
851feb475f4SMax Kazantsev   if (!HasIntrinsicGuards && !HasWidenableConditions)
8528fb3d57eSArtur Pilipenko     return false;
8538fb3d57eSArtur Pilipenko 
8548fb3d57eSArtur Pilipenko   DL = &M->getDataLayout();
8558fb3d57eSArtur Pilipenko 
8568fb3d57eSArtur Pilipenko   Preheader = L->getLoopPreheader();
8578fb3d57eSArtur Pilipenko   if (!Preheader)
8588fb3d57eSArtur Pilipenko     return false;
8598fb3d57eSArtur Pilipenko 
860889dc1e3SArtur Pilipenko   auto LatchCheckOpt = parseLoopLatchICmp();
861889dc1e3SArtur Pilipenko   if (!LatchCheckOpt)
862889dc1e3SArtur Pilipenko     return false;
863889dc1e3SArtur Pilipenko   LatchCheck = *LatchCheckOpt;
864889dc1e3SArtur Pilipenko 
865d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "Latch check:\n");
866d34e60caSNicola Zaghen   LLVM_DEBUG(LatchCheck.dump());
86768797214SAnna Thomas 
8689b1176b0SAnna Thomas   if (!isLoopProfitableToPredicate()) {
869d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Loop not profitable to predicate!\n");
8709b1176b0SAnna Thomas     return false;
8719b1176b0SAnna Thomas   }
8728fb3d57eSArtur Pilipenko   // Collect all the guards into a vector and process later, so as not
8738fb3d57eSArtur Pilipenko   // to invalidate the instruction iterator.
8748fb3d57eSArtur Pilipenko   SmallVector<IntrinsicInst *, 4> Guards;
875feb475f4SMax Kazantsev   SmallVector<BranchInst *, 4> GuardsAsWidenableBranches;
876feb475f4SMax Kazantsev   for (const auto BB : L->blocks()) {
8778fb3d57eSArtur Pilipenko     for (auto &I : *BB)
87828298e96SMax Kazantsev       if (isGuard(&I))
87928298e96SMax Kazantsev         Guards.push_back(cast<IntrinsicInst>(&I));
880feb475f4SMax Kazantsev     if (PredicateWidenableBranchGuards &&
881feb475f4SMax Kazantsev         isGuardAsWidenableBranch(BB->getTerminator()))
882feb475f4SMax Kazantsev       GuardsAsWidenableBranches.push_back(
883feb475f4SMax Kazantsev           cast<BranchInst>(BB->getTerminator()));
884feb475f4SMax Kazantsev   }
8858fb3d57eSArtur Pilipenko 
886feb475f4SMax Kazantsev   if (Guards.empty() && GuardsAsWidenableBranches.empty())
88746c4e0a4SArtur Pilipenko     return false;
88846c4e0a4SArtur Pilipenko 
8898fb3d57eSArtur Pilipenko   SCEVExpander Expander(*SE, *DL, "loop-predication");
8908fb3d57eSArtur Pilipenko 
8918fb3d57eSArtur Pilipenko   bool Changed = false;
8928fb3d57eSArtur Pilipenko   for (auto *Guard : Guards)
8938fb3d57eSArtur Pilipenko     Changed |= widenGuardConditions(Guard, Expander);
894feb475f4SMax Kazantsev   for (auto *Guard : GuardsAsWidenableBranches)
895feb475f4SMax Kazantsev     Changed |= widenWidenableBranchGuardConditions(Guard, Expander);
8968fb3d57eSArtur Pilipenko 
8978fb3d57eSArtur Pilipenko   return Changed;
8988fb3d57eSArtur Pilipenko }
899