xref: /llvm-project/llvm/lib/Transforms/Scalar/LoopPredication.cpp (revision ca45087826f66d4e9848a3c10925ff02c924de2c)
1 //===-- LoopPredication.cpp - Guard based loop predication pass -----------===//
2 //
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
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 //
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 //
55 //   if (B(0)) {
56 //     do {
57 //       I = PHI(0, I.INC)
58 //       I.INC = I + Step
59 //       guard(G(I));
60 //     } while (B(I));
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 //
67 //   if (B(0)) {
68 //     do {
69 //       I = PHI(0, I.INC)
70 //       I.INC = I + Step
71 //       guard(G(0) && M);
72 //     } while (B(I));
73 //   }
74 //
75 // One solution for M is M = forall X . (G(X) && B(X)) => G(X + Step)
76 //
77 // Informal proof that the transformation above is correct:
78 //
79 //   By the definition of guards we can rewrite the guard condition to:
80 //     G(I) && G(0) && M
81 //
82 //   Let's prove that for each iteration of the loop:
83 //     G(0) && M => G(I)
84 //   And the condition above can be simplified to G(Start) && M.
85 //
86 //   Induction base.
87 //     G(0) && M => G(0)
88 //
89 //   Induction step. Assuming G(0) && M => G(I) on the subsequent
90 //   iteration:
91 //
92 //     B(I) is true because it's the backedge condition.
93 //     G(I) is true because the backedge is guarded by this condition.
94 //
95 //   So M = forall X . (G(X) && B(X)) => G(X + Step) implies G(I + Step).
96 //
97 // Note that we can use anything stronger than M, i.e. any condition which
98 // implies M.
99 //
100 // When S = 1 (i.e. forward iterating loop), the transformation is supported
101 // when:
102 //   * The loop has a single latch with the condition of the form:
103 //     B(X) = latchStart + X <pred> latchLimit,
104 //     where <pred> is u<, u<=, s<, or s<=.
105 //   * The guard condition is of the form
106 //     G(X) = guardStart + X u< guardLimit
107 //
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
111 //
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
120 //
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);)
126 //
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
140 //
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
153 //
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:
157 //     B(X) = X <pred> latchLimit, where <pred> is u>, u>=, s>, or s>=.
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.
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.
177 //===----------------------------------------------------------------------===//
178 
179 #include "llvm/Transforms/Scalar/LoopPredication.h"
180 #include "llvm/ADT/Statistic.h"
181 #include "llvm/Analysis/BranchProbabilityInfo.h"
182 #include "llvm/Analysis/GuardUtils.h"
183 #include "llvm/Analysis/LoopInfo.h"
184 #include "llvm/Analysis/LoopPass.h"
185 #include "llvm/Analysis/ScalarEvolution.h"
186 #include "llvm/Analysis/ScalarEvolutionExpander.h"
187 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
188 #include "llvm/IR/Function.h"
189 #include "llvm/IR/GlobalValue.h"
190 #include "llvm/IR/IntrinsicInst.h"
191 #include "llvm/IR/Module.h"
192 #include "llvm/IR/PatternMatch.h"
193 #include "llvm/Pass.h"
194 #include "llvm/Support/Debug.h"
195 #include "llvm/Transforms/Scalar.h"
196 #include "llvm/Transforms/Utils/LoopUtils.h"
197 
198 #define DEBUG_TYPE "loop-predication"
199 
200 STATISTIC(TotalConsidered, "Number of guards considered");
201 STATISTIC(TotalWidened, "Number of checks widened");
202 
203 using namespace llvm;
204 
205 static cl::opt<bool> EnableIVTruncation("loop-predication-enable-iv-truncation",
206                                         cl::Hidden, cl::init(true));
207 
208 static cl::opt<bool> EnableCountDownLoop("loop-predication-enable-count-down-loop",
209                                         cl::Hidden, cl::init(true));
210 
211 static cl::opt<bool>
212     SkipProfitabilityChecks("loop-predication-skip-profitability-checks",
213                             cl::Hidden, cl::init(false));
214 
215 // This is the scale factor for the latch probability. We use this during
216 // profitability analysis to find other exiting blocks that have a much higher
217 // probability of exiting the loop instead of loop exiting via latch.
218 // This value should be greater than 1 for a sane profitability check.
219 static cl::opt<float> LatchExitProbabilityScale(
220     "loop-predication-latch-probability-scale", cl::Hidden, cl::init(2.0),
221     cl::desc("scale factor for the latch probability. Value should be greater "
222              "than 1. Lower values are ignored"));
223 
224 namespace {
225 class LoopPredication {
226   /// Represents an induction variable check:
227   ///   icmp Pred, <induction variable>, <loop invariant limit>
228   struct LoopICmp {
229     ICmpInst::Predicate Pred;
230     const SCEVAddRecExpr *IV;
231     const SCEV *Limit;
232     LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV,
233              const SCEV *Limit)
234         : Pred(Pred), IV(IV), Limit(Limit) {}
235     LoopICmp() {}
236     void dump() {
237       dbgs() << "LoopICmp Pred = " << Pred << ", IV = " << *IV
238              << ", Limit = " << *Limit << "\n";
239     }
240   };
241 
242   ScalarEvolution *SE;
243   BranchProbabilityInfo *BPI;
244 
245   Loop *L;
246   const DataLayout *DL;
247   BasicBlock *Preheader;
248   LoopICmp LatchCheck;
249 
250   bool isSupportedStep(const SCEV* Step);
251   Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI) {
252     return parseLoopICmp(ICI->getPredicate(), ICI->getOperand(0),
253                          ICI->getOperand(1));
254   }
255   Optional<LoopICmp> parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS,
256                                    Value *RHS);
257 
258   Optional<LoopICmp> parseLoopLatchICmp();
259 
260   bool CanExpand(const SCEV* S);
261   Value *expandCheck(SCEVExpander &Expander, IRBuilder<> &Builder,
262                      ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
263                      Instruction *InsertAt);
264 
265   Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander,
266                                         IRBuilder<> &Builder);
267   Optional<Value *> widenICmpRangeCheckIncrementingLoop(LoopICmp LatchCheck,
268                                                         LoopICmp RangeCheck,
269                                                         SCEVExpander &Expander,
270                                                         IRBuilder<> &Builder);
271   Optional<Value *> widenICmpRangeCheckDecrementingLoop(LoopICmp LatchCheck,
272                                                         LoopICmp RangeCheck,
273                                                         SCEVExpander &Expander,
274                                                         IRBuilder<> &Builder);
275   unsigned collectChecks(SmallVectorImpl<Value *> &Checks, Value *Condition,
276                          SCEVExpander &Expander, IRBuilder<> &Builder);
277   bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander);
278 
279   // If the loop always exits through another block in the loop, we should not
280   // predicate based on the latch check. For example, the latch check can be a
281   // very coarse grained check and there can be more fine grained exit checks
282   // within the loop. We identify such unprofitable loops through BPI.
283   bool isLoopProfitableToPredicate();
284 
285   // When the IV type is wider than the range operand type, we can still do loop
286   // predication, by generating SCEVs for the range and latch that are of the
287   // same type. We achieve this by generating a SCEV truncate expression for the
288   // latch IV. This is done iff truncation of the IV is a safe operation,
289   // without loss of information.
290   // Another way to achieve this is by generating a wider type SCEV for the
291   // range check operand, however, this needs a more involved check that
292   // operands do not overflow. This can lead to loss of information when the
293   // range operand is of the form: add i32 %offset, %iv. We need to prove that
294   // sext(x + y) is same as sext(x) + sext(y).
295   // This function returns true if we can safely represent the IV type in
296   // the RangeCheckType without loss of information.
297   bool isSafeToTruncateWideIVType(Type *RangeCheckType);
298   // Return the loopLatchCheck corresponding to the RangeCheckType if safe to do
299   // so.
300   Optional<LoopICmp> generateLoopLatchCheck(Type *RangeCheckType);
301 
302 public:
303   LoopPredication(ScalarEvolution *SE, BranchProbabilityInfo *BPI)
304       : SE(SE), BPI(BPI){};
305   bool runOnLoop(Loop *L);
306 };
307 
308 class LoopPredicationLegacyPass : public LoopPass {
309 public:
310   static char ID;
311   LoopPredicationLegacyPass() : LoopPass(ID) {
312     initializeLoopPredicationLegacyPassPass(*PassRegistry::getPassRegistry());
313   }
314 
315   void getAnalysisUsage(AnalysisUsage &AU) const override {
316     AU.addRequired<BranchProbabilityInfoWrapperPass>();
317     getLoopAnalysisUsage(AU);
318   }
319 
320   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
321     if (skipLoop(L))
322       return false;
323     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
324     BranchProbabilityInfo &BPI =
325         getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
326     LoopPredication LP(SE, &BPI);
327     return LP.runOnLoop(L);
328   }
329 };
330 
331 char LoopPredicationLegacyPass::ID = 0;
332 } // end namespace llvm
333 
334 INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication",
335                       "Loop predication", false, false)
336 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
337 INITIALIZE_PASS_DEPENDENCY(LoopPass)
338 INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication",
339                     "Loop predication", false, false)
340 
341 Pass *llvm::createLoopPredicationPass() {
342   return new LoopPredicationLegacyPass();
343 }
344 
345 PreservedAnalyses LoopPredicationPass::run(Loop &L, LoopAnalysisManager &AM,
346                                            LoopStandardAnalysisResults &AR,
347                                            LPMUpdater &U) {
348   const auto &FAM =
349       AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager();
350   Function *F = L.getHeader()->getParent();
351   auto *BPI = FAM.getCachedResult<BranchProbabilityAnalysis>(*F);
352   LoopPredication LP(&AR.SE, BPI);
353   if (!LP.runOnLoop(&L))
354     return PreservedAnalyses::all();
355 
356   return getLoopPassPreservedAnalyses();
357 }
358 
359 Optional<LoopPredication::LoopICmp>
360 LoopPredication::parseLoopICmp(ICmpInst::Predicate Pred, Value *LHS,
361                                Value *RHS) {
362   const SCEV *LHSS = SE->getSCEV(LHS);
363   if (isa<SCEVCouldNotCompute>(LHSS))
364     return None;
365   const SCEV *RHSS = SE->getSCEV(RHS);
366   if (isa<SCEVCouldNotCompute>(RHSS))
367     return None;
368 
369   // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV
370   if (SE->isLoopInvariant(LHSS, L)) {
371     std::swap(LHS, RHS);
372     std::swap(LHSS, RHSS);
373     Pred = ICmpInst::getSwappedPredicate(Pred);
374   }
375 
376   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS);
377   if (!AR || AR->getLoop() != L)
378     return None;
379 
380   return LoopICmp(Pred, AR, RHSS);
381 }
382 
383 Value *LoopPredication::expandCheck(SCEVExpander &Expander,
384                                     IRBuilder<> &Builder,
385                                     ICmpInst::Predicate Pred, const SCEV *LHS,
386                                     const SCEV *RHS, Instruction *InsertAt) {
387   // TODO: we can check isLoopEntryGuardedByCond before emitting the check
388 
389   Type *Ty = LHS->getType();
390   assert(Ty == RHS->getType() && "expandCheck operands have different types?");
391 
392   if (SE->isLoopEntryGuardedByCond(L, Pred, LHS, RHS))
393     return Builder.getTrue();
394 
395   Value *LHSV = Expander.expandCodeFor(LHS, Ty, InsertAt);
396   Value *RHSV = Expander.expandCodeFor(RHS, Ty, InsertAt);
397   return Builder.CreateICmp(Pred, LHSV, RHSV);
398 }
399 
400 Optional<LoopPredication::LoopICmp>
401 LoopPredication::generateLoopLatchCheck(Type *RangeCheckType) {
402 
403   auto *LatchType = LatchCheck.IV->getType();
404   if (RangeCheckType == LatchType)
405     return LatchCheck;
406   // For now, bail out if latch type is narrower than range type.
407   if (DL->getTypeSizeInBits(LatchType) < DL->getTypeSizeInBits(RangeCheckType))
408     return None;
409   if (!isSafeToTruncateWideIVType(RangeCheckType))
410     return None;
411   // We can now safely identify the truncated version of the IV and limit for
412   // RangeCheckType.
413   LoopICmp NewLatchCheck;
414   NewLatchCheck.Pred = LatchCheck.Pred;
415   NewLatchCheck.IV = dyn_cast<SCEVAddRecExpr>(
416       SE->getTruncateExpr(LatchCheck.IV, RangeCheckType));
417   if (!NewLatchCheck.IV)
418     return None;
419   NewLatchCheck.Limit = SE->getTruncateExpr(LatchCheck.Limit, RangeCheckType);
420   LLVM_DEBUG(dbgs() << "IV of type: " << *LatchType
421                     << "can be represented as range check type:"
422                     << *RangeCheckType << "\n");
423   LLVM_DEBUG(dbgs() << "LatchCheck.IV: " << *NewLatchCheck.IV << "\n");
424   LLVM_DEBUG(dbgs() << "LatchCheck.Limit: " << *NewLatchCheck.Limit << "\n");
425   return NewLatchCheck;
426 }
427 
428 bool LoopPredication::isSupportedStep(const SCEV* Step) {
429   return Step->isOne() || (Step->isAllOnesValue() && EnableCountDownLoop);
430 }
431 
432 bool LoopPredication::CanExpand(const SCEV* S) {
433   return SE->isLoopInvariant(S, L) && isSafeToExpand(S, *SE);
434 }
435 
436 Optional<Value *> LoopPredication::widenICmpRangeCheckIncrementingLoop(
437     LoopPredication::LoopICmp LatchCheck, LoopPredication::LoopICmp RangeCheck,
438     SCEVExpander &Expander, IRBuilder<> &Builder) {
439   auto *Ty = RangeCheck.IV->getType();
440   // Generate the widened condition for the forward loop:
441   //   guardStart u< guardLimit &&
442   //   latchLimit <pred> guardLimit - 1 - guardStart + latchStart
443   // where <pred> depends on the latch condition predicate. See the file
444   // header comment for the reasoning.
445   // guardLimit - guardStart + latchStart - 1
446   const SCEV *GuardStart = RangeCheck.IV->getStart();
447   const SCEV *GuardLimit = RangeCheck.Limit;
448   const SCEV *LatchStart = LatchCheck.IV->getStart();
449   const SCEV *LatchLimit = LatchCheck.Limit;
450 
451   // guardLimit - guardStart + latchStart - 1
452   const SCEV *RHS =
453       SE->getAddExpr(SE->getMinusSCEV(GuardLimit, GuardStart),
454                      SE->getMinusSCEV(LatchStart, SE->getOne(Ty)));
455   if (!CanExpand(GuardStart) || !CanExpand(GuardLimit) ||
456       !CanExpand(LatchLimit) || !CanExpand(RHS)) {
457     LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
458     return None;
459   }
460   auto LimitCheckPred =
461       ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred);
462 
463   LLVM_DEBUG(dbgs() << "LHS: " << *LatchLimit << "\n");
464   LLVM_DEBUG(dbgs() << "RHS: " << *RHS << "\n");
465   LLVM_DEBUG(dbgs() << "Pred: " << LimitCheckPred << "\n");
466 
467   Instruction *InsertAt = Preheader->getTerminator();
468   auto *LimitCheck =
469       expandCheck(Expander, Builder, LimitCheckPred, LatchLimit, RHS, InsertAt);
470   auto *FirstIterationCheck = expandCheck(Expander, Builder, RangeCheck.Pred,
471                                           GuardStart, GuardLimit, InsertAt);
472   return Builder.CreateAnd(FirstIterationCheck, LimitCheck);
473 }
474 
475 Optional<Value *> LoopPredication::widenICmpRangeCheckDecrementingLoop(
476     LoopPredication::LoopICmp LatchCheck, LoopPredication::LoopICmp RangeCheck,
477     SCEVExpander &Expander, IRBuilder<> &Builder) {
478   auto *Ty = RangeCheck.IV->getType();
479   const SCEV *GuardStart = RangeCheck.IV->getStart();
480   const SCEV *GuardLimit = RangeCheck.Limit;
481   const SCEV *LatchLimit = LatchCheck.Limit;
482   if (!CanExpand(GuardStart) || !CanExpand(GuardLimit) ||
483       !CanExpand(LatchLimit)) {
484     LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
485     return None;
486   }
487   // The decrement of the latch check IV should be the same as the
488   // rangeCheckIV.
489   auto *PostDecLatchCheckIV = LatchCheck.IV->getPostIncExpr(*SE);
490   if (RangeCheck.IV != PostDecLatchCheckIV) {
491     LLVM_DEBUG(dbgs() << "Not the same. PostDecLatchCheckIV: "
492                       << *PostDecLatchCheckIV
493                       << "  and RangeCheckIV: " << *RangeCheck.IV << "\n");
494     return None;
495   }
496 
497   // Generate the widened condition for CountDownLoop:
498   // guardStart u< guardLimit &&
499   // latchLimit <pred> 1.
500   // See the header comment for reasoning of the checks.
501   Instruction *InsertAt = Preheader->getTerminator();
502   auto LimitCheckPred =
503       ICmpInst::getFlippedStrictnessPredicate(LatchCheck.Pred);
504   auto *FirstIterationCheck = expandCheck(Expander, Builder, ICmpInst::ICMP_ULT,
505                                           GuardStart, GuardLimit, InsertAt);
506   auto *LimitCheck = expandCheck(Expander, Builder, LimitCheckPred, LatchLimit,
507                                  SE->getOne(Ty), InsertAt);
508   return Builder.CreateAnd(FirstIterationCheck, LimitCheck);
509 }
510 
511 /// If ICI can be widened to a loop invariant condition emits the loop
512 /// invariant condition in the loop preheader and return it, otherwise
513 /// returns None.
514 Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI,
515                                                        SCEVExpander &Expander,
516                                                        IRBuilder<> &Builder) {
517   LLVM_DEBUG(dbgs() << "Analyzing ICmpInst condition:\n");
518   LLVM_DEBUG(ICI->dump());
519 
520   // parseLoopStructure guarantees that the latch condition is:
521   //   ++i <pred> latchLimit, where <pred> is u<, u<=, s<, or s<=.
522   // We are looking for the range checks of the form:
523   //   i u< guardLimit
524   auto RangeCheck = parseLoopICmp(ICI);
525   if (!RangeCheck) {
526     LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n");
527     return None;
528   }
529   LLVM_DEBUG(dbgs() << "Guard check:\n");
530   LLVM_DEBUG(RangeCheck->dump());
531   if (RangeCheck->Pred != ICmpInst::ICMP_ULT) {
532     LLVM_DEBUG(dbgs() << "Unsupported range check predicate("
533                       << RangeCheck->Pred << ")!\n");
534     return None;
535   }
536   auto *RangeCheckIV = RangeCheck->IV;
537   if (!RangeCheckIV->isAffine()) {
538     LLVM_DEBUG(dbgs() << "Range check IV is not affine!\n");
539     return None;
540   }
541   auto *Step = RangeCheckIV->getStepRecurrence(*SE);
542   // We cannot just compare with latch IV step because the latch and range IVs
543   // may have different types.
544   if (!isSupportedStep(Step)) {
545     LLVM_DEBUG(dbgs() << "Range check and latch have IVs different steps!\n");
546     return None;
547   }
548   auto *Ty = RangeCheckIV->getType();
549   auto CurrLatchCheckOpt = generateLoopLatchCheck(Ty);
550   if (!CurrLatchCheckOpt) {
551     LLVM_DEBUG(dbgs() << "Failed to generate a loop latch check "
552                          "corresponding to range type: "
553                       << *Ty << "\n");
554     return None;
555   }
556 
557   LoopICmp CurrLatchCheck = *CurrLatchCheckOpt;
558   // At this point, the range and latch step should have the same type, but need
559   // not have the same value (we support both 1 and -1 steps).
560   assert(Step->getType() ==
561              CurrLatchCheck.IV->getStepRecurrence(*SE)->getType() &&
562          "Range and latch steps should be of same type!");
563   if (Step != CurrLatchCheck.IV->getStepRecurrence(*SE)) {
564     LLVM_DEBUG(dbgs() << "Range and latch have different step values!\n");
565     return None;
566   }
567 
568   if (Step->isOne())
569     return widenICmpRangeCheckIncrementingLoop(CurrLatchCheck, *RangeCheck,
570                                                Expander, Builder);
571   else {
572     assert(Step->isAllOnesValue() && "Step should be -1!");
573     return widenICmpRangeCheckDecrementingLoop(CurrLatchCheck, *RangeCheck,
574                                                Expander, Builder);
575   }
576 }
577 
578 unsigned LoopPredication::collectChecks(SmallVectorImpl<Value *> &Checks,
579                                         Value *Condition,
580                                         SCEVExpander &Expander,
581                                         IRBuilder<> &Builder) {
582   unsigned NumWidened = 0;
583   // The guard condition is expected to be in form of:
584   //   cond1 && cond2 && cond3 ...
585   // Iterate over subconditions looking for icmp conditions which can be
586   // widened across loop iterations. Widening these conditions remember the
587   // resulting list of subconditions in Checks vector.
588   SmallVector<Value *, 4> Worklist(1, Condition);
589   SmallPtrSet<Value *, 4> Visited;
590   do {
591     Value *Condition = Worklist.pop_back_val();
592     if (!Visited.insert(Condition).second)
593       continue;
594 
595     Value *LHS, *RHS;
596     using namespace llvm::PatternMatch;
597     if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) {
598       Worklist.push_back(LHS);
599       Worklist.push_back(RHS);
600       continue;
601     }
602 
603     if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) {
604       if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, Builder)) {
605         Checks.push_back(NewRangeCheck.getValue());
606         NumWidened++;
607         continue;
608       }
609     }
610 
611     // Save the condition as is if we can't widen it
612     Checks.push_back(Condition);
613   } while (!Worklist.empty());
614   return NumWidened;
615 }
616 
617 bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard,
618                                            SCEVExpander &Expander) {
619   LLVM_DEBUG(dbgs() << "Processing guard:\n");
620   LLVM_DEBUG(Guard->dump());
621 
622   TotalConsidered++;
623   SmallVector<Value *, 4> Checks;
624   IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator()));
625   unsigned NumWidened = collectChecks(Checks, Guard->getOperand(0), Expander,
626                                       Builder);
627   if (NumWidened == 0)
628     return false;
629 
630   TotalWidened += NumWidened;
631 
632   // Emit the new guard condition
633   Builder.SetInsertPoint(Guard);
634   Value *LastCheck = nullptr;
635   for (auto *Check : Checks)
636     if (!LastCheck)
637       LastCheck = Check;
638     else
639       LastCheck = Builder.CreateAnd(LastCheck, Check);
640   Guard->setOperand(0, LastCheck);
641 
642   LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n");
643   return true;
644 }
645 
646 Optional<LoopPredication::LoopICmp> LoopPredication::parseLoopLatchICmp() {
647   using namespace PatternMatch;
648 
649   BasicBlock *LoopLatch = L->getLoopLatch();
650   if (!LoopLatch) {
651     LLVM_DEBUG(dbgs() << "The loop doesn't have a single latch!\n");
652     return None;
653   }
654 
655   ICmpInst::Predicate Pred;
656   Value *LHS, *RHS;
657   BasicBlock *TrueDest, *FalseDest;
658 
659   if (!match(LoopLatch->getTerminator(),
660              m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TrueDest,
661                   FalseDest))) {
662     LLVM_DEBUG(dbgs() << "Failed to match the latch terminator!\n");
663     return None;
664   }
665   assert((TrueDest == L->getHeader() || FalseDest == L->getHeader()) &&
666          "One of the latch's destinations must be the header");
667   if (TrueDest != L->getHeader())
668     Pred = ICmpInst::getInversePredicate(Pred);
669 
670   auto Result = parseLoopICmp(Pred, LHS, RHS);
671   if (!Result) {
672     LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n");
673     return None;
674   }
675 
676   // Check affine first, so if it's not we don't try to compute the step
677   // recurrence.
678   if (!Result->IV->isAffine()) {
679     LLVM_DEBUG(dbgs() << "The induction variable is not affine!\n");
680     return None;
681   }
682 
683   auto *Step = Result->IV->getStepRecurrence(*SE);
684   if (!isSupportedStep(Step)) {
685     LLVM_DEBUG(dbgs() << "Unsupported loop stride(" << *Step << ")!\n");
686     return None;
687   }
688 
689   auto IsUnsupportedPredicate = [](const SCEV *Step, ICmpInst::Predicate Pred) {
690     if (Step->isOne()) {
691       return Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_SLT &&
692              Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_SLE;
693     } else {
694       assert(Step->isAllOnesValue() && "Step should be -1!");
695       return Pred != ICmpInst::ICMP_UGT && Pred != ICmpInst::ICMP_SGT &&
696              Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_SGE;
697     }
698   };
699 
700   if (IsUnsupportedPredicate(Step, Result->Pred)) {
701     LLVM_DEBUG(dbgs() << "Unsupported loop latch predicate(" << Result->Pred
702                       << ")!\n");
703     return None;
704   }
705   return Result;
706 }
707 
708 // Returns true if its safe to truncate the IV to RangeCheckType.
709 bool LoopPredication::isSafeToTruncateWideIVType(Type *RangeCheckType) {
710   if (!EnableIVTruncation)
711     return false;
712   assert(DL->getTypeSizeInBits(LatchCheck.IV->getType()) >
713              DL->getTypeSizeInBits(RangeCheckType) &&
714          "Expected latch check IV type to be larger than range check operand "
715          "type!");
716   // The start and end values of the IV should be known. This is to guarantee
717   // that truncating the wide type will not lose information.
718   auto *Limit = dyn_cast<SCEVConstant>(LatchCheck.Limit);
719   auto *Start = dyn_cast<SCEVConstant>(LatchCheck.IV->getStart());
720   if (!Limit || !Start)
721     return false;
722   // This check makes sure that the IV does not change sign during loop
723   // iterations. Consider latchType = i64, LatchStart = 5, Pred = ICMP_SGE,
724   // LatchEnd = 2, rangeCheckType = i32. If it's not a monotonic predicate, the
725   // IV wraps around, and the truncation of the IV would lose the range of
726   // iterations between 2^32 and 2^64.
727   bool Increasing;
728   if (!SE->isMonotonicPredicate(LatchCheck.IV, LatchCheck.Pred, Increasing))
729     return false;
730   // The active bits should be less than the bits in the RangeCheckType. This
731   // guarantees that truncating the latch check to RangeCheckType is a safe
732   // operation.
733   auto RangeCheckTypeBitSize = DL->getTypeSizeInBits(RangeCheckType);
734   return Start->getAPInt().getActiveBits() < RangeCheckTypeBitSize &&
735          Limit->getAPInt().getActiveBits() < RangeCheckTypeBitSize;
736 }
737 
738 bool LoopPredication::isLoopProfitableToPredicate() {
739   if (SkipProfitabilityChecks || !BPI)
740     return true;
741 
742   SmallVector<std::pair<const BasicBlock *, const BasicBlock *>, 8> ExitEdges;
743   L->getExitEdges(ExitEdges);
744   // If there is only one exiting edge in the loop, it is always profitable to
745   // predicate the loop.
746   if (ExitEdges.size() == 1)
747     return true;
748 
749   // Calculate the exiting probabilities of all exiting edges from the loop,
750   // starting with the LatchExitProbability.
751   // Heuristic for profitability: If any of the exiting blocks' probability of
752   // exiting the loop is larger than exiting through the latch block, it's not
753   // profitable to predicate the loop.
754   auto *LatchBlock = L->getLoopLatch();
755   assert(LatchBlock && "Should have a single latch at this point!");
756   auto *LatchTerm = LatchBlock->getTerminator();
757   assert(LatchTerm->getNumSuccessors() == 2 &&
758          "expected to be an exiting block with 2 succs!");
759   unsigned LatchBrExitIdx =
760       LatchTerm->getSuccessor(0) == L->getHeader() ? 1 : 0;
761   BranchProbability LatchExitProbability =
762       BPI->getEdgeProbability(LatchBlock, LatchBrExitIdx);
763 
764   // Protect against degenerate inputs provided by the user. Providing a value
765   // less than one, can invert the definition of profitable loop predication.
766   float ScaleFactor = LatchExitProbabilityScale;
767   if (ScaleFactor < 1) {
768     LLVM_DEBUG(
769         dbgs()
770         << "Ignored user setting for loop-predication-latch-probability-scale: "
771         << LatchExitProbabilityScale << "\n");
772     LLVM_DEBUG(dbgs() << "The value is set to 1.0\n");
773     ScaleFactor = 1.0;
774   }
775   const auto LatchProbabilityThreshold =
776       LatchExitProbability * ScaleFactor;
777 
778   for (const auto &ExitEdge : ExitEdges) {
779     BranchProbability ExitingBlockProbability =
780         BPI->getEdgeProbability(ExitEdge.first, ExitEdge.second);
781     // Some exiting edge has higher probability than the latch exiting edge.
782     // No longer profitable to predicate.
783     if (ExitingBlockProbability > LatchProbabilityThreshold)
784       return false;
785   }
786   // Using BPI, we have concluded that the most probable way to exit from the
787   // loop is through the latch (or there's no profile information and all
788   // exits are equally likely).
789   return true;
790 }
791 
792 bool LoopPredication::runOnLoop(Loop *Loop) {
793   L = Loop;
794 
795   LLVM_DEBUG(dbgs() << "Analyzing ");
796   LLVM_DEBUG(L->dump());
797 
798   Module *M = L->getHeader()->getModule();
799 
800   // There is nothing to do if the module doesn't use guards
801   auto *GuardDecl =
802       M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard));
803   if (!GuardDecl || GuardDecl->use_empty())
804     return false;
805 
806   DL = &M->getDataLayout();
807 
808   Preheader = L->getLoopPreheader();
809   if (!Preheader)
810     return false;
811 
812   auto LatchCheckOpt = parseLoopLatchICmp();
813   if (!LatchCheckOpt)
814     return false;
815   LatchCheck = *LatchCheckOpt;
816 
817   LLVM_DEBUG(dbgs() << "Latch check:\n");
818   LLVM_DEBUG(LatchCheck.dump());
819 
820   if (!isLoopProfitableToPredicate()) {
821     LLVM_DEBUG(dbgs() << "Loop not profitable to predicate!\n");
822     return false;
823   }
824   // Collect all the guards into a vector and process later, so as not
825   // to invalidate the instruction iterator.
826   SmallVector<IntrinsicInst *, 4> Guards;
827   for (const auto BB : L->blocks())
828     for (auto &I : *BB)
829       if (isGuard(&I))
830         Guards.push_back(cast<IntrinsicInst>(&I));
831 
832   if (Guards.empty())
833     return false;
834 
835   SCEVExpander Expander(*SE, *DL, "loop-predication");
836 
837   bool Changed = false;
838   for (auto *Guard : Guards)
839     Changed |= widenGuardConditions(Guard, Expander);
840 
841   return Changed;
842 }
843