LLVM  10.0.0
LoopPredication.cpp
Go to the documentation of this file.
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 
180 #include "llvm/ADT/Statistic.h"
184 #include "llvm/Analysis/LoopInfo.h"
185 #include "llvm/Analysis/LoopPass.h"
189 #include "llvm/IR/Function.h"
190 #include "llvm/IR/GlobalValue.h"
191 #include "llvm/IR/IntrinsicInst.h"
192 #include "llvm/IR/Module.h"
193 #include "llvm/IR/PatternMatch.h"
194 #include "llvm/InitializePasses.h"
195 #include "llvm/Pass.h"
197 #include "llvm/Support/Debug.h"
198 #include "llvm/Transforms/Scalar.h"
202 
203 #define DEBUG_TYPE "loop-predication"
204 
205 STATISTIC(TotalConsidered, "Number of guards considered");
206 STATISTIC(TotalWidened, "Number of checks widened");
207 
208 using namespace llvm;
209 
210 static cl::opt<bool> EnableIVTruncation("loop-predication-enable-iv-truncation",
211  cl::Hidden, cl::init(true));
212 
213 static cl::opt<bool> EnableCountDownLoop("loop-predication-enable-count-down-loop",
214  cl::Hidden, cl::init(true));
215 
216 static cl::opt<bool>
217  SkipProfitabilityChecks("loop-predication-skip-profitability-checks",
218  cl::Hidden, cl::init(false));
219 
220 // This is the scale factor for the latch probability. We use this during
221 // profitability analysis to find other exiting blocks that have a much higher
222 // probability of exiting the loop instead of loop exiting via latch.
223 // This value should be greater than 1 for a sane profitability check.
225  "loop-predication-latch-probability-scale", cl::Hidden, cl::init(2.0),
226  cl::desc("scale factor for the latch probability. Value should be greater "
227  "than 1. Lower values are ignored"));
228 
230  "loop-predication-predicate-widenable-branches-to-deopt", cl::Hidden,
231  cl::desc("Whether or not we should predicate guards "
232  "expressed as widenable branches to deoptimize blocks"),
233  cl::init(true));
234 
235 namespace {
236 /// Represents an induction variable check:
237 /// icmp Pred, <induction variable>, <loop invariant limit>
238 struct LoopICmp {
239  ICmpInst::Predicate Pred;
240  const SCEVAddRecExpr *IV;
241  const SCEV *Limit;
242  LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV,
243  const SCEV *Limit)
244  : Pred(Pred), IV(IV), Limit(Limit) {}
245  LoopICmp() {}
246  void dump() {
247  dbgs() << "LoopICmp Pred = " << Pred << ", IV = " << *IV
248  << ", Limit = " << *Limit << "\n";
249  }
250 };
251 
252 class LoopPredication {
253  AliasAnalysis *AA;
254  DominatorTree *DT;
255  ScalarEvolution *SE;
256  LoopInfo *LI;
258 
259  Loop *L;
260  const DataLayout *DL;
261  BasicBlock *Preheader;
262  LoopICmp LatchCheck;
263 
264  bool isSupportedStep(const SCEV* Step);
265  Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI);
266  Optional<LoopICmp> parseLoopLatchICmp();
267 
268  /// Return an insertion point suitable for inserting a safe to speculate
269  /// instruction whose only user will be 'User' which has operands 'Ops'. A
270  /// trivial result would be the at the User itself, but we try to return a
271  /// loop invariant location if possible.
272  Instruction *findInsertPt(Instruction *User, ArrayRef<Value*> Ops);
273  /// Same as above, *except* that this uses the SCEV definition of invariant
274  /// which is that an expression *can be made* invariant via SCEVExpander.
275  /// Thus, this version is only suitable for finding an insert point to be be
276  /// passed to SCEVExpander!
277  Instruction *findInsertPt(Instruction *User, ArrayRef<const SCEV*> Ops);
278 
279  /// Return true if the value is known to produce a single fixed value across
280  /// all iterations on which it executes. Note that this does not imply
281  /// speculation safety. That must be established seperately.
282  bool isLoopInvariantValue(const SCEV* S);
283 
284  Value *expandCheck(SCEVExpander &Expander, Instruction *Guard,
285  ICmpInst::Predicate Pred, const SCEV *LHS,
286  const SCEV *RHS);
287 
288  Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander,
289  Instruction *Guard);
290  Optional<Value *> widenICmpRangeCheckIncrementingLoop(LoopICmp LatchCheck,
291  LoopICmp RangeCheck,
292  SCEVExpander &Expander,
293  Instruction *Guard);
294  Optional<Value *> widenICmpRangeCheckDecrementingLoop(LoopICmp LatchCheck,
295  LoopICmp RangeCheck,
296  SCEVExpander &Expander,
297  Instruction *Guard);
298  unsigned collectChecks(SmallVectorImpl<Value *> &Checks, Value *Condition,
299  SCEVExpander &Expander, Instruction *Guard);
300  bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander);
301  bool widenWidenableBranchGuardConditions(BranchInst *Guard, SCEVExpander &Expander);
302  // If the loop always exits through another block in the loop, we should not
303  // predicate based on the latch check. For example, the latch check can be a
304  // very coarse grained check and there can be more fine grained exit checks
305  // within the loop. We identify such unprofitable loops through BPI.
306  bool isLoopProfitableToPredicate();
307 
308  bool predicateLoopExits(Loop *L, SCEVExpander &Rewriter);
309 
310 public:
312  ScalarEvolution *SE, LoopInfo *LI,
314  : AA(AA), DT(DT), SE(SE), LI(LI), BPI(BPI) {};
315  bool runOnLoop(Loop *L);
316 };
317 
318 class LoopPredicationLegacyPass : public LoopPass {
319 public:
320  static char ID;
321  LoopPredicationLegacyPass() : LoopPass(ID) {
323  }
324 
325  void getAnalysisUsage(AnalysisUsage &AU) const override {
328  }
329 
330  bool runOnLoop(Loop *L, LPPassManager &LPM) override {
331  if (skipLoop(L))
332  return false;
333  auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
334  auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
335  auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
336  BranchProbabilityInfo &BPI =
337  getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
338  auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
339  LoopPredication LP(AA, DT, SE, LI, &BPI);
340  return LP.runOnLoop(L);
341  }
342 };
343 
345 } // end namespace llvm
346 
347 INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication",
348  "Loop predication", false, false)
351 INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication",
352  "Loop predication", false, false)
353 
355  return new LoopPredicationLegacyPass();
356 }
357 
360  LPMUpdater &U) {
361  const auto &FAM =
362  AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR).getManager();
363  Function *F = L.getHeader()->getParent();
364  auto *BPI = FAM.getCachedResult<BranchProbabilityAnalysis>(*F);
365  LoopPredication LP(&AR.AA, &AR.DT, &AR.SE, &AR.LI, BPI);
366  if (!LP.runOnLoop(&L))
367  return PreservedAnalyses::all();
368 
370 }
371 
373 LoopPredication::parseLoopICmp(ICmpInst *ICI) {
374  auto Pred = ICI->getPredicate();
375  auto *LHS = ICI->getOperand(0);
376  auto *RHS = ICI->getOperand(1);
377 
378  const SCEV *LHSS = SE->getSCEV(LHS);
379  if (isa<SCEVCouldNotCompute>(LHSS))
380  return None;
381  const SCEV *RHSS = SE->getSCEV(RHS);
382  if (isa<SCEVCouldNotCompute>(RHSS))
383  return None;
384 
385  // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV
386  if (SE->isLoopInvariant(LHSS, L)) {
387  std::swap(LHS, RHS);
388  std::swap(LHSS, RHSS);
389  Pred = ICmpInst::getSwappedPredicate(Pred);
390  }
391 
392  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS);
393  if (!AR || AR->getLoop() != L)
394  return None;
395 
396  return LoopICmp(Pred, AR, RHSS);
397 }
398 
399 Value *LoopPredication::expandCheck(SCEVExpander &Expander,
400  Instruction *Guard,
401  ICmpInst::Predicate Pred, const SCEV *LHS,
402  const SCEV *RHS) {
403  Type *Ty = LHS->getType();
404  assert(Ty == RHS->getType() && "expandCheck operands have different types?");
405 
406  if (SE->isLoopInvariant(LHS, L) && SE->isLoopInvariant(RHS, L)) {
407  IRBuilder<> Builder(Guard);
408  if (SE->isLoopEntryGuardedByCond(L, Pred, LHS, RHS))
409  return Builder.getTrue();
410  if (SE->isLoopEntryGuardedByCond(L, ICmpInst::getInversePredicate(Pred),
411  LHS, RHS))
412  return Builder.getFalse();
413  }
414 
415  Value *LHSV = Expander.expandCodeFor(LHS, Ty, findInsertPt(Guard, {LHS}));
416  Value *RHSV = Expander.expandCodeFor(RHS, Ty, findInsertPt(Guard, {RHS}));
417  IRBuilder<> Builder(findInsertPt(Guard, {LHSV, RHSV}));
418  return Builder.CreateICmp(Pred, LHSV, RHSV);
419 }
420 
421 
422 // Returns true if its safe to truncate the IV to RangeCheckType.
423 // When the IV type is wider than the range operand type, we can still do loop
424 // predication, by generating SCEVs for the range and latch that are of the
425 // same type. We achieve this by generating a SCEV truncate expression for the
426 // latch IV. This is done iff truncation of the IV is a safe operation,
427 // without loss of information.
428 // Another way to achieve this is by generating a wider type SCEV for the
429 // range check operand, however, this needs a more involved check that
430 // operands do not overflow. This can lead to loss of information when the
431 // range operand is of the form: add i32 %offset, %iv. We need to prove that
432 // sext(x + y) is same as sext(x) + sext(y).
433 // This function returns true if we can safely represent the IV type in
434 // the RangeCheckType without loss of information.
436  ScalarEvolution &SE,
437  const LoopICmp LatchCheck,
438  Type *RangeCheckType) {
439  if (!EnableIVTruncation)
440  return false;
441  assert(DL.getTypeSizeInBits(LatchCheck.IV->getType()) >
442  DL.getTypeSizeInBits(RangeCheckType) &&
443  "Expected latch check IV type to be larger than range check operand "
444  "type!");
445  // The start and end values of the IV should be known. This is to guarantee
446  // that truncating the wide type will not lose information.
447  auto *Limit = dyn_cast<SCEVConstant>(LatchCheck.Limit);
448  auto *Start = dyn_cast<SCEVConstant>(LatchCheck.IV->getStart());
449  if (!Limit || !Start)
450  return false;
451  // This check makes sure that the IV does not change sign during loop
452  // iterations. Consider latchType = i64, LatchStart = 5, Pred = ICMP_SGE,
453  // LatchEnd = 2, rangeCheckType = i32. If it's not a monotonic predicate, the
454  // IV wraps around, and the truncation of the IV would lose the range of
455  // iterations between 2^32 and 2^64.
456  bool Increasing;
457  if (!SE.isMonotonicPredicate(LatchCheck.IV, LatchCheck.Pred, Increasing))
458  return false;
459  // The active bits should be less than the bits in the RangeCheckType. This
460  // guarantees that truncating the latch check to RangeCheckType is a safe
461  // operation.
462  auto RangeCheckTypeBitSize = DL.getTypeSizeInBits(RangeCheckType);
463  return Start->getAPInt().getActiveBits() < RangeCheckTypeBitSize &&
464  Limit->getAPInt().getActiveBits() < RangeCheckTypeBitSize;
465 }
466 
467 
468 // Return an LoopICmp describing a latch check equivlent to LatchCheck but with
469 // the requested type if safe to do so. May involve the use of a new IV.
471  ScalarEvolution &SE,
472  const LoopICmp LatchCheck,
473  Type *RangeCheckType) {
474 
475  auto *LatchType = LatchCheck.IV->getType();
476  if (RangeCheckType == LatchType)
477  return LatchCheck;
478  // For now, bail out if latch type is narrower than range type.
479  if (DL.getTypeSizeInBits(LatchType) < DL.getTypeSizeInBits(RangeCheckType))
480  return None;
481  if (!isSafeToTruncateWideIVType(DL, SE, LatchCheck, RangeCheckType))
482  return None;
483  // We can now safely identify the truncated version of the IV and limit for
484  // RangeCheckType.
485  LoopICmp NewLatchCheck;
486  NewLatchCheck.Pred = LatchCheck.Pred;
487  NewLatchCheck.IV = dyn_cast<SCEVAddRecExpr>(
488  SE.getTruncateExpr(LatchCheck.IV, RangeCheckType));
489  if (!NewLatchCheck.IV)
490  return None;
491  NewLatchCheck.Limit = SE.getTruncateExpr(LatchCheck.Limit, RangeCheckType);
492  LLVM_DEBUG(dbgs() << "IV of type: " << *LatchType
493  << "can be represented as range check type:"
494  << *RangeCheckType << "\n");
495  LLVM_DEBUG(dbgs() << "LatchCheck.IV: " << *NewLatchCheck.IV << "\n");
496  LLVM_DEBUG(dbgs() << "LatchCheck.Limit: " << *NewLatchCheck.Limit << "\n");
497  return NewLatchCheck;
498 }
499 
500 bool LoopPredication::isSupportedStep(const SCEV* Step) {
501  return Step->isOne() || (Step->isAllOnesValue() && EnableCountDownLoop);
502 }
503 
504 Instruction *LoopPredication::findInsertPt(Instruction *Use,
505  ArrayRef<Value*> Ops) {
506  for (Value *Op : Ops)
507  if (!L->isLoopInvariant(Op))
508  return Use;
509  return Preheader->getTerminator();
510 }
511 
512 Instruction *LoopPredication::findInsertPt(Instruction *Use,
513  ArrayRef<const SCEV*> Ops) {
514  // Subtlety: SCEV considers things to be invariant if the value produced is
515  // the same across iterations. This is not the same as being able to
516  // evaluate outside the loop, which is what we actually need here.
517  for (const SCEV *Op : Ops)
518  if (!SE->isLoopInvariant(Op, L) ||
519  !isSafeToExpandAt(Op, Preheader->getTerminator(), *SE))
520  return Use;
521  return Preheader->getTerminator();
522 }
523 
524 bool LoopPredication::isLoopInvariantValue(const SCEV* S) {
525  // Handling expressions which produce invariant results, but *haven't* yet
526  // been removed from the loop serves two important purposes.
527  // 1) Most importantly, it resolves a pass ordering cycle which would
528  // otherwise need us to iteration licm, loop-predication, and either
529  // loop-unswitch or loop-peeling to make progress on examples with lots of
530  // predicable range checks in a row. (Since, in the general case, we can't
531  // hoist the length checks until the dominating checks have been discharged
532  // as we can't prove doing so is safe.)
533  // 2) As a nice side effect, this exposes the value of peeling or unswitching
534  // much more obviously in the IR. Otherwise, the cost modeling for other
535  // transforms would end up needing to duplicate all of this logic to model a
536  // check which becomes predictable based on a modeled peel or unswitch.
537  //
538  // The cost of doing so in the worst case is an extra fill from the stack in
539  // the loop to materialize the loop invariant test value instead of checking
540  // against the original IV which is presumable in a register inside the loop.
541  // Such cases are presumably rare, and hint at missing oppurtunities for
542  // other passes.
543 
544  if (SE->isLoopInvariant(S, L))
545  // Note: This the SCEV variant, so the original Value* may be within the
546  // loop even though SCEV has proven it is loop invariant.
547  return true;
548 
549  // Handle a particular important case which SCEV doesn't yet know about which
550  // shows up in range checks on arrays with immutable lengths.
551  // TODO: This should be sunk inside SCEV.
552  if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S))
553  if (const auto *LI = dyn_cast<LoadInst>(U->getValue()))
554  if (LI->isUnordered() && L->hasLoopInvariantOperands(LI))
555  if (AA->pointsToConstantMemory(LI->getOperand(0)) ||
556  LI->hasMetadata(LLVMContext::MD_invariant_load))
557  return true;
558  return false;
559 }
560 
561 Optional<Value *> LoopPredication::widenICmpRangeCheckIncrementingLoop(
562  LoopICmp LatchCheck, LoopICmp RangeCheck,
563  SCEVExpander &Expander, Instruction *Guard) {
564  auto *Ty = RangeCheck.IV->getType();
565  // Generate the widened condition for the forward loop:
566  // guardStart u< guardLimit &&
567  // latchLimit <pred> guardLimit - 1 - guardStart + latchStart
568  // where <pred> depends on the latch condition predicate. See the file
569  // header comment for the reasoning.
570  // guardLimit - guardStart + latchStart - 1
571  const SCEV *GuardStart = RangeCheck.IV->getStart();
572  const SCEV *GuardLimit = RangeCheck.Limit;
573  const SCEV *LatchStart = LatchCheck.IV->getStart();
574  const SCEV *LatchLimit = LatchCheck.Limit;
575  // Subtlety: We need all the values to be *invariant* across all iterations,
576  // but we only need to check expansion safety for those which *aren't*
577  // already guaranteed to dominate the guard.
578  if (!isLoopInvariantValue(GuardStart) ||
579  !isLoopInvariantValue(GuardLimit) ||
580  !isLoopInvariantValue(LatchStart) ||
581  !isLoopInvariantValue(LatchLimit)) {
582  LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
583  return None;
584  }
585  if (!isSafeToExpandAt(LatchStart, Guard, *SE) ||
586  !isSafeToExpandAt(LatchLimit, Guard, *SE)) {
587  LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
588  return None;
589  }
590 
591  // guardLimit - guardStart + latchStart - 1
592  const SCEV *RHS =
593  SE->getAddExpr(SE->getMinusSCEV(GuardLimit, GuardStart),
594  SE->getMinusSCEV(LatchStart, SE->getOne(Ty)));
595  auto LimitCheckPred =
597 
598  LLVM_DEBUG(dbgs() << "LHS: " << *LatchLimit << "\n");
599  LLVM_DEBUG(dbgs() << "RHS: " << *RHS << "\n");
600  LLVM_DEBUG(dbgs() << "Pred: " << LimitCheckPred << "\n");
601 
602  auto *LimitCheck =
603  expandCheck(Expander, Guard, LimitCheckPred, LatchLimit, RHS);
604  auto *FirstIterationCheck = expandCheck(Expander, Guard, RangeCheck.Pred,
605  GuardStart, GuardLimit);
606  IRBuilder<> Builder(findInsertPt(Guard, {FirstIterationCheck, LimitCheck}));
607  return Builder.CreateAnd(FirstIterationCheck, LimitCheck);
608 }
609 
610 Optional<Value *> LoopPredication::widenICmpRangeCheckDecrementingLoop(
611  LoopICmp LatchCheck, LoopICmp RangeCheck,
612  SCEVExpander &Expander, Instruction *Guard) {
613  auto *Ty = RangeCheck.IV->getType();
614  const SCEV *GuardStart = RangeCheck.IV->getStart();
615  const SCEV *GuardLimit = RangeCheck.Limit;
616  const SCEV *LatchStart = LatchCheck.IV->getStart();
617  const SCEV *LatchLimit = LatchCheck.Limit;
618  // Subtlety: We need all the values to be *invariant* across all iterations,
619  // but we only need to check expansion safety for those which *aren't*
620  // already guaranteed to dominate the guard.
621  if (!isLoopInvariantValue(GuardStart) ||
622  !isLoopInvariantValue(GuardLimit) ||
623  !isLoopInvariantValue(LatchStart) ||
624  !isLoopInvariantValue(LatchLimit)) {
625  LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
626  return None;
627  }
628  if (!isSafeToExpandAt(LatchStart, Guard, *SE) ||
629  !isSafeToExpandAt(LatchLimit, Guard, *SE)) {
630  LLVM_DEBUG(dbgs() << "Can't expand limit check!\n");
631  return None;
632  }
633  // The decrement of the latch check IV should be the same as the
634  // rangeCheckIV.
635  auto *PostDecLatchCheckIV = LatchCheck.IV->getPostIncExpr(*SE);
636  if (RangeCheck.IV != PostDecLatchCheckIV) {
637  LLVM_DEBUG(dbgs() << "Not the same. PostDecLatchCheckIV: "
638  << *PostDecLatchCheckIV
639  << " and RangeCheckIV: " << *RangeCheck.IV << "\n");
640  return None;
641  }
642 
643  // Generate the widened condition for CountDownLoop:
644  // guardStart u< guardLimit &&
645  // latchLimit <pred> 1.
646  // See the header comment for reasoning of the checks.
647  auto LimitCheckPred =
649  auto *FirstIterationCheck = expandCheck(Expander, Guard,
651  GuardStart, GuardLimit);
652  auto *LimitCheck = expandCheck(Expander, Guard, LimitCheckPred, LatchLimit,
653  SE->getOne(Ty));
654  IRBuilder<> Builder(findInsertPt(Guard, {FirstIterationCheck, LimitCheck}));
655  return Builder.CreateAnd(FirstIterationCheck, LimitCheck);
656 }
657 
659  LoopICmp& RC) {
660  // LFTR canonicalizes checks to the ICMP_NE/EQ form; normalize back to the
661  // ULT/UGE form for ease of handling by our caller.
662  if (ICmpInst::isEquality(RC.Pred) &&
663  RC.IV->getStepRecurrence(*SE)->isOne() &&
664  SE->isKnownPredicate(ICmpInst::ICMP_ULE, RC.IV->getStart(), RC.Limit))
665  RC.Pred = RC.Pred == ICmpInst::ICMP_NE ?
667 }
668 
669 
670 /// If ICI can be widened to a loop invariant condition emits the loop
671 /// invariant condition in the loop preheader and return it, otherwise
672 /// returns None.
673 Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI,
674  SCEVExpander &Expander,
675  Instruction *Guard) {
676  LLVM_DEBUG(dbgs() << "Analyzing ICmpInst condition:\n");
677  LLVM_DEBUG(ICI->dump());
678 
679  // parseLoopStructure guarantees that the latch condition is:
680  // ++i <pred> latchLimit, where <pred> is u<, u<=, s<, or s<=.
681  // We are looking for the range checks of the form:
682  // i u< guardLimit
683  auto RangeCheck = parseLoopICmp(ICI);
684  if (!RangeCheck) {
685  LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n");
686  return None;
687  }
688  LLVM_DEBUG(dbgs() << "Guard check:\n");
689  LLVM_DEBUG(RangeCheck->dump());
690  if (RangeCheck->Pred != ICmpInst::ICMP_ULT) {
691  LLVM_DEBUG(dbgs() << "Unsupported range check predicate("
692  << RangeCheck->Pred << ")!\n");
693  return None;
694  }
695  auto *RangeCheckIV = RangeCheck->IV;
696  if (!RangeCheckIV->isAffine()) {
697  LLVM_DEBUG(dbgs() << "Range check IV is not affine!\n");
698  return None;
699  }
700  auto *Step = RangeCheckIV->getStepRecurrence(*SE);
701  // We cannot just compare with latch IV step because the latch and range IVs
702  // may have different types.
703  if (!isSupportedStep(Step)) {
704  LLVM_DEBUG(dbgs() << "Range check and latch have IVs different steps!\n");
705  return None;
706  }
707  auto *Ty = RangeCheckIV->getType();
708  auto CurrLatchCheckOpt = generateLoopLatchCheck(*DL, *SE, LatchCheck, Ty);
709  if (!CurrLatchCheckOpt) {
710  LLVM_DEBUG(dbgs() << "Failed to generate a loop latch check "
711  "corresponding to range type: "
712  << *Ty << "\n");
713  return None;
714  }
715 
716  LoopICmp CurrLatchCheck = *CurrLatchCheckOpt;
717  // At this point, the range and latch step should have the same type, but need
718  // not have the same value (we support both 1 and -1 steps).
719  assert(Step->getType() ==
720  CurrLatchCheck.IV->getStepRecurrence(*SE)->getType() &&
721  "Range and latch steps should be of same type!");
722  if (Step != CurrLatchCheck.IV->getStepRecurrence(*SE)) {
723  LLVM_DEBUG(dbgs() << "Range and latch have different step values!\n");
724  return None;
725  }
726 
727  if (Step->isOne())
728  return widenICmpRangeCheckIncrementingLoop(CurrLatchCheck, *RangeCheck,
729  Expander, Guard);
730  else {
731  assert(Step->isAllOnesValue() && "Step should be -1!");
732  return widenICmpRangeCheckDecrementingLoop(CurrLatchCheck, *RangeCheck,
733  Expander, Guard);
734  }
735 }
736 
737 unsigned LoopPredication::collectChecks(SmallVectorImpl<Value *> &Checks,
738  Value *Condition,
739  SCEVExpander &Expander,
740  Instruction *Guard) {
741  unsigned NumWidened = 0;
742  // The guard condition is expected to be in form of:
743  // cond1 && cond2 && cond3 ...
744  // Iterate over subconditions looking for icmp conditions which can be
745  // widened across loop iterations. Widening these conditions remember the
746  // resulting list of subconditions in Checks vector.
747  SmallVector<Value *, 4> Worklist(1, Condition);
748  SmallPtrSet<Value *, 4> Visited;
749  Value *WideableCond = nullptr;
750  do {
751  Value *Condition = Worklist.pop_back_val();
752  if (!Visited.insert(Condition).second)
753  continue;
754 
755  Value *LHS, *RHS;
756  using namespace llvm::PatternMatch;
757  if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) {
758  Worklist.push_back(LHS);
759  Worklist.push_back(RHS);
760  continue;
761  }
762 
763  if (match(Condition,
764  m_Intrinsic<Intrinsic::experimental_widenable_condition>())) {
765  // Pick any, we don't care which
766  WideableCond = Condition;
767  continue;
768  }
769 
770  if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) {
771  if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander,
772  Guard)) {
773  Checks.push_back(NewRangeCheck.getValue());
774  NumWidened++;
775  continue;
776  }
777  }
778 
779  // Save the condition as is if we can't widen it
780  Checks.push_back(Condition);
781  } while (!Worklist.empty());
782  // At the moment, our matching logic for wideable conditions implicitly
783  // assumes we preserve the form: (br (and Cond, WC())). FIXME
784  // Note that if there were multiple calls to wideable condition in the
785  // traversal, we only need to keep one, and which one is arbitrary.
786  if (WideableCond)
787  Checks.push_back(WideableCond);
788  return NumWidened;
789 }
790 
791 bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard,
792  SCEVExpander &Expander) {
793  LLVM_DEBUG(dbgs() << "Processing guard:\n");
794  LLVM_DEBUG(Guard->dump());
795 
796  TotalConsidered++;
798  unsigned NumWidened = collectChecks(Checks, Guard->getOperand(0), Expander,
799  Guard);
800  if (NumWidened == 0)
801  return false;
802 
803  TotalWidened += NumWidened;
804 
805  // Emit the new guard condition
806  IRBuilder<> Builder(findInsertPt(Guard, Checks));
807  Value *AllChecks = Builder.CreateAnd(Checks);
808  auto *OldCond = Guard->getOperand(0);
809  Guard->setOperand(0, AllChecks);
811 
812  LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n");
813  return true;
814 }
815 
816 bool LoopPredication::widenWidenableBranchGuardConditions(
817  BranchInst *BI, SCEVExpander &Expander) {
818  assert(isGuardAsWidenableBranch(BI) && "Must be!");
819  LLVM_DEBUG(dbgs() << "Processing guard:\n");
820  LLVM_DEBUG(BI->dump());
821 
822  TotalConsidered++;
824  unsigned NumWidened = collectChecks(Checks, BI->getCondition(),
825  Expander, BI);
826  if (NumWidened == 0)
827  return false;
828 
829  TotalWidened += NumWidened;
830 
831  // Emit the new guard condition
832  IRBuilder<> Builder(findInsertPt(BI, Checks));
833  Value *AllChecks = Builder.CreateAnd(Checks);
834  auto *OldCond = BI->getCondition();
835  BI->setCondition(AllChecks);
838  "Stopped being a guard after transform?");
839 
840  LLVM_DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n");
841  return true;
842 }
843 
844 Optional<LoopICmp> LoopPredication::parseLoopLatchICmp() {
845  using namespace PatternMatch;
846 
847  BasicBlock *LoopLatch = L->getLoopLatch();
848  if (!LoopLatch) {
849  LLVM_DEBUG(dbgs() << "The loop doesn't have a single latch!\n");
850  return None;
851  }
852 
853  auto *BI = dyn_cast<BranchInst>(LoopLatch->getTerminator());
854  if (!BI || !BI->isConditional()) {
855  LLVM_DEBUG(dbgs() << "Failed to match the latch terminator!\n");
856  return None;
857  }
858  BasicBlock *TrueDest = BI->getSuccessor(0);
859  assert(
860  (TrueDest == L->getHeader() || BI->getSuccessor(1) == L->getHeader()) &&
861  "One of the latch's destinations must be the header");
862 
863  auto *ICI = dyn_cast<ICmpInst>(BI->getCondition());
864  if (!ICI) {
865  LLVM_DEBUG(dbgs() << "Failed to match the latch condition!\n");
866  return None;
867  }
868  auto Result = parseLoopICmp(ICI);
869  if (!Result) {
870  LLVM_DEBUG(dbgs() << "Failed to parse the loop latch condition!\n");
871  return None;
872  }
873 
874  if (TrueDest != L->getHeader())
875  Result->Pred = ICmpInst::getInversePredicate(Result->Pred);
876 
877  // Check affine first, so if it's not we don't try to compute the step
878  // recurrence.
879  if (!Result->IV->isAffine()) {
880  LLVM_DEBUG(dbgs() << "The induction variable is not affine!\n");
881  return None;
882  }
883 
884  auto *Step = Result->IV->getStepRecurrence(*SE);
885  if (!isSupportedStep(Step)) {
886  LLVM_DEBUG(dbgs() << "Unsupported loop stride(" << *Step << ")!\n");
887  return None;
888  }
889 
890  auto IsUnsupportedPredicate = [](const SCEV *Step, ICmpInst::Predicate Pred) {
891  if (Step->isOne()) {
892  return Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_SLT &&
893  Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_SLE;
894  } else {
895  assert(Step->isAllOnesValue() && "Step should be -1!");
896  return Pred != ICmpInst::ICMP_UGT && Pred != ICmpInst::ICMP_SGT &&
897  Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_SGE;
898  }
899  };
900 
901  normalizePredicate(SE, L, *Result);
902  if (IsUnsupportedPredicate(Step, Result->Pred)) {
903  LLVM_DEBUG(dbgs() << "Unsupported loop latch predicate(" << Result->Pred
904  << ")!\n");
905  return None;
906  }
907 
908  return Result;
909 }
910 
911 
912 bool LoopPredication::isLoopProfitableToPredicate() {
913  if (SkipProfitabilityChecks || !BPI)
914  return true;
915 
917  L->getExitEdges(ExitEdges);
918  // If there is only one exiting edge in the loop, it is always profitable to
919  // predicate the loop.
920  if (ExitEdges.size() == 1)
921  return true;
922 
923  // Calculate the exiting probabilities of all exiting edges from the loop,
924  // starting with the LatchExitProbability.
925  // Heuristic for profitability: If any of the exiting blocks' probability of
926  // exiting the loop is larger than exiting through the latch block, it's not
927  // profitable to predicate the loop.
928  auto *LatchBlock = L->getLoopLatch();
929  assert(LatchBlock && "Should have a single latch at this point!");
930  auto *LatchTerm = LatchBlock->getTerminator();
931  assert(LatchTerm->getNumSuccessors() == 2 &&
932  "expected to be an exiting block with 2 succs!");
933  unsigned LatchBrExitIdx =
934  LatchTerm->getSuccessor(0) == L->getHeader() ? 1 : 0;
935  BranchProbability LatchExitProbability =
936  BPI->getEdgeProbability(LatchBlock, LatchBrExitIdx);
937 
938  // Protect against degenerate inputs provided by the user. Providing a value
939  // less than one, can invert the definition of profitable loop predication.
940  float ScaleFactor = LatchExitProbabilityScale;
941  if (ScaleFactor < 1) {
942  LLVM_DEBUG(
943  dbgs()
944  << "Ignored user setting for loop-predication-latch-probability-scale: "
945  << LatchExitProbabilityScale << "\n");
946  LLVM_DEBUG(dbgs() << "The value is set to 1.0\n");
947  ScaleFactor = 1.0;
948  }
949  const auto LatchProbabilityThreshold =
950  LatchExitProbability * ScaleFactor;
951 
952  for (const auto &ExitEdge : ExitEdges) {
953  BranchProbability ExitingBlockProbability =
954  BPI->getEdgeProbability(ExitEdge.first, ExitEdge.second);
955  // Some exiting edge has higher probability than the latch exiting edge.
956  // No longer profitable to predicate.
957  if (ExitingBlockProbability > LatchProbabilityThreshold)
958  return false;
959  }
960  // Using BPI, we have concluded that the most probable way to exit from the
961  // loop is through the latch (or there's no profile information and all
962  // exits are equally likely).
963  return true;
964 }
965 
966 /// If we can (cheaply) find a widenable branch which controls entry into the
967 /// loop, return it.
969  // Walk back through any unconditional executed blocks and see if we can find
970  // a widenable condition which seems to control execution of this loop. Note
971  // that we predict that maythrow calls are likely untaken and thus that it's
972  // profitable to widen a branch before a maythrow call with a condition
973  // afterwards even though that may cause the slow path to run in a case where
974  // it wouldn't have otherwise.
975  BasicBlock *BB = L->getLoopPreheader();
976  if (!BB)
977  return nullptr;
978  do {
979  if (BasicBlock *Pred = BB->getSinglePredecessor())
980  if (BB == Pred->getSingleSuccessor()) {
981  BB = Pred;
982  continue;
983  }
984  break;
985  } while (true);
986 
987  if (BasicBlock *Pred = BB->getSinglePredecessor()) {
988  auto *Term = Pred->getTerminator();
989 
990  Value *Cond, *WC;
991  BasicBlock *IfTrueBB, *IfFalseBB;
992  if (parseWidenableBranch(Term, Cond, WC, IfTrueBB, IfFalseBB) &&
993  IfTrueBB == BB)
994  return cast<BranchInst>(Term);
995  }
996  return nullptr;
997 }
998 
999 /// Return the minimum of all analyzeable exit counts. This is an upper bound
1000 /// on the actual exit count. If there are not at least two analyzeable exits,
1001 /// returns SCEVCouldNotCompute.
1003  DominatorTree &DT,
1004  Loop *L) {
1005  SmallVector<BasicBlock *, 16> ExitingBlocks;
1006  L->getExitingBlocks(ExitingBlocks);
1007 
1008  SmallVector<const SCEV *, 4> ExitCounts;
1009  for (BasicBlock *ExitingBB : ExitingBlocks) {
1010  const SCEV *ExitCount = SE.getExitCount(L, ExitingBB);
1011  if (isa<SCEVCouldNotCompute>(ExitCount))
1012  continue;
1013  assert(DT.dominates(ExitingBB, L->getLoopLatch()) &&
1014  "We should only have known counts for exiting blocks that "
1015  "dominate latch!");
1016  ExitCounts.push_back(ExitCount);
1017  }
1018  if (ExitCounts.size() < 2)
1019  return SE.getCouldNotCompute();
1020  return SE.getUMinFromMismatchedTypes(ExitCounts);
1021 }
1022 
1023 /// Return true if we can be fairly sure that executing block BB will probably
1024 /// lead to executing an __llvm_deoptimize. This is a profitability heuristic,
1025 /// not a legality constraint.
1027  while (BB->getUniqueSuccessor())
1028  // Will skip side effects, that's okay
1029  BB = BB->getUniqueSuccessor();
1030 
1031  return BB->getTerminatingDeoptimizeCall();
1032 }
1033 
1034 /// This implements an analogous, but entirely distinct transform from the main
1035 /// loop predication transform. This one is phrased in terms of using a
1036 /// widenable branch *outside* the loop to allow us to simplify loop exits in a
1037 /// following loop. This is close in spirit to the IndVarSimplify transform
1038 /// of the same name, but is materially different widening loosens legality
1039 /// sharply.
1040 bool LoopPredication::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
1041  // The transformation performed here aims to widen a widenable condition
1042  // above the loop such that all analyzeable exit leading to deopt are dead.
1043  // It assumes that the latch is the dominant exit for profitability and that
1044  // exits branching to deoptimizing blocks are rarely taken. It relies on the
1045  // semantics of widenable expressions for legality. (i.e. being able to fall
1046  // down the widenable path spuriously allows us to ignore exit order,
1047  // unanalyzeable exits, side effects, exceptional exits, and other challenges
1048  // which restrict the applicability of the non-WC based version of this
1049  // transform in IndVarSimplify.)
1050  //
1051  // NOTE ON POISON/UNDEF - We're hoisting an expression above guards which may
1052  // imply flags on the expression being hoisted and inserting new uses (flags
1053  // are only correct for current uses). The result is that we may be
1054  // inserting a branch on the value which can be either poison or undef. In
1055  // this case, the branch can legally go either way; we just need to avoid
1056  // introducing UB. This is achieved through the use of the freeze
1057  // instruction.
1058 
1059  SmallVector<BasicBlock *, 16> ExitingBlocks;
1060  L->getExitingBlocks(ExitingBlocks);
1061 
1062  if (ExitingBlocks.empty())
1063  return false; // Nothing to do.
1064 
1065  auto *Latch = L->getLoopLatch();
1066  if (!Latch)
1067  return false;
1068 
1069  auto *WidenableBR = FindWidenableTerminatorAboveLoop(L, *LI);
1070  if (!WidenableBR)
1071  return false;
1072 
1073  const SCEV *LatchEC = SE->getExitCount(L, Latch);
1074  if (isa<SCEVCouldNotCompute>(LatchEC))
1075  return false; // profitability - want hot exit in analyzeable set
1076 
1077  // At this point, we have found an analyzeable latch, and a widenable
1078  // condition above the loop. If we have a widenable exit within the loop
1079  // (for which we can't compute exit counts), drop the ability to further
1080  // widen so that we gain ability to analyze it's exit count and perform this
1081  // transform. TODO: It'd be nice to know for sure the exit became
1082  // analyzeable after dropping widenability.
1083  {
1084  bool Invalidate = false;
1085 
1086  for (auto *ExitingBB : ExitingBlocks) {
1087  if (LI->getLoopFor(ExitingBB) != L)
1088  continue;
1089 
1090  auto *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator());
1091  if (!BI)
1092  continue;
1093 
1094  Use *Cond, *WC;
1095  BasicBlock *IfTrueBB, *IfFalseBB;
1096  if (parseWidenableBranch(BI, Cond, WC, IfTrueBB, IfFalseBB) &&
1097  L->contains(IfTrueBB)) {
1098  WC->set(ConstantInt::getTrue(IfTrueBB->getContext()));
1099  Invalidate = true;
1100  }
1101  }
1102  if (Invalidate)
1103  SE->forgetLoop(L);
1104  }
1105 
1106  // The use of umin(all analyzeable exits) instead of latch is subtle, but
1107  // important for profitability. We may have a loop which hasn't been fully
1108  // canonicalized just yet. If the exit we chose to widen is provably never
1109  // taken, we want the widened form to *also* be provably never taken. We
1110  // can't guarantee this as a current unanalyzeable exit may later become
1111  // analyzeable, but we can at least avoid the obvious cases.
1112  const SCEV *MinEC = getMinAnalyzeableBackedgeTakenCount(*SE, *DT, L);
1113  if (isa<SCEVCouldNotCompute>(MinEC) || MinEC->getType()->isPointerTy() ||
1114  !SE->isLoopInvariant(MinEC, L) ||
1115  !isSafeToExpandAt(MinEC, WidenableBR, *SE))
1116  return false;
1117 
1118  // Subtlety: We need to avoid inserting additional uses of the WC. We know
1119  // that it can only have one transitive use at the moment, and thus moving
1120  // that use to just before the branch and inserting code before it and then
1121  // modifying the operand is legal.
1122  auto *IP = cast<Instruction>(WidenableBR->getCondition());
1123  IP->moveBefore(WidenableBR);
1124  Rewriter.setInsertPoint(IP);
1125  IRBuilder<> B(IP);
1126 
1127  bool Changed = false;
1128  Value *MinECV = nullptr; // lazily generated if needed
1129  for (BasicBlock *ExitingBB : ExitingBlocks) {
1130  // If our exiting block exits multiple loops, we can only rewrite the
1131  // innermost one. Otherwise, we're changing how many times the innermost
1132  // loop runs before it exits.
1133  if (LI->getLoopFor(ExitingBB) != L)
1134  continue;
1135 
1136  // Can't rewrite non-branch yet.
1137  auto *BI = dyn_cast<BranchInst>(ExitingBB->getTerminator());
1138  if (!BI)
1139  continue;
1140 
1141  // If already constant, nothing to do.
1142  if (isa<Constant>(BI->getCondition()))
1143  continue;
1144 
1145  const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
1146  if (isa<SCEVCouldNotCompute>(ExitCount) ||
1147  ExitCount->getType()->isPointerTy() ||
1148  !isSafeToExpandAt(ExitCount, WidenableBR, *SE))
1149  continue;
1150 
1151  const bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
1152  BasicBlock *ExitBB = BI->getSuccessor(ExitIfTrue ? 0 : 1);
1153  if (!isVeryLikelyToDeopt(ExitBB))
1154  // Profitability: indicator of rarely/never taken exit
1155  continue;
1156 
1157  // If we found a widenable exit condition, do two things:
1158  // 1) fold the widened exit test into the widenable condition
1159  // 2) fold the branch to untaken - avoids infinite looping
1160 
1161  Value *ECV = Rewriter.expandCodeFor(ExitCount);
1162  if (!MinECV)
1163  MinECV = Rewriter.expandCodeFor(MinEC);
1164  Value *RHS = MinECV;
1165  if (ECV->getType() != RHS->getType()) {
1166  Type *WiderTy = SE->getWiderType(ECV->getType(), RHS->getType());
1167  ECV = B.CreateZExt(ECV, WiderTy);
1168  RHS = B.CreateZExt(RHS, WiderTy);
1169  }
1170  assert(!Latch || DT->dominates(ExitingBB, Latch));
1171  Value *NewCond = B.CreateICmp(ICmpInst::ICMP_UGT, ECV, RHS);
1172  // Freeze poison or undef to an arbitrary bit pattern to ensure we can
1173  // branch without introducing UB. See NOTE ON POISON/UNDEF above for
1174  // context.
1175  NewCond = B.CreateFreeze(NewCond);
1176 
1177  widenWidenableBranch(WidenableBR, NewCond);
1178 
1179  Value *OldCond = BI->getCondition();
1180  BI->setCondition(ConstantInt::get(OldCond->getType(), !ExitIfTrue));
1181  Changed = true;
1182  }
1183 
1184  if (Changed)
1185  // We just mutated a bunch of loop exits changing there exit counts
1186  // widely. We need to force recomputation of the exit counts given these
1187  // changes. Note that all of the inserted exits are never taken, and
1188  // should be removed next time the CFG is modified.
1189  SE->forgetLoop(L);
1190  return Changed;
1191 }
1192 
1193 bool LoopPredication::runOnLoop(Loop *Loop) {
1194  L = Loop;
1195 
1196  LLVM_DEBUG(dbgs() << "Analyzing ");
1197  LLVM_DEBUG(L->dump());
1198 
1199  Module *M = L->getHeader()->getModule();
1200 
1201  // There is nothing to do if the module doesn't use guards
1202  auto *GuardDecl =
1203  M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard));
1204  bool HasIntrinsicGuards = GuardDecl && !GuardDecl->use_empty();
1205  auto *WCDecl = M->getFunction(
1206  Intrinsic::getName(Intrinsic::experimental_widenable_condition));
1207  bool HasWidenableConditions =
1208  PredicateWidenableBranchGuards && WCDecl && !WCDecl->use_empty();
1209  if (!HasIntrinsicGuards && !HasWidenableConditions)
1210  return false;
1211 
1212  DL = &M->getDataLayout();
1213 
1214  Preheader = L->getLoopPreheader();
1215  if (!Preheader)
1216  return false;
1217 
1218  auto LatchCheckOpt = parseLoopLatchICmp();
1219  if (!LatchCheckOpt)
1220  return false;
1221  LatchCheck = *LatchCheckOpt;
1222 
1223  LLVM_DEBUG(dbgs() << "Latch check:\n");
1224  LLVM_DEBUG(LatchCheck.dump());
1225 
1226  if (!isLoopProfitableToPredicate()) {
1227  LLVM_DEBUG(dbgs() << "Loop not profitable to predicate!\n");
1228  return false;
1229  }
1230  // Collect all the guards into a vector and process later, so as not
1231  // to invalidate the instruction iterator.
1233  SmallVector<BranchInst *, 4> GuardsAsWidenableBranches;
1234  for (const auto BB : L->blocks()) {
1235  for (auto &I : *BB)
1236  if (isGuard(&I))
1237  Guards.push_back(cast<IntrinsicInst>(&I));
1239  isGuardAsWidenableBranch(BB->getTerminator()))
1240  GuardsAsWidenableBranches.push_back(
1241  cast<BranchInst>(BB->getTerminator()));
1242  }
1243 
1244  SCEVExpander Expander(*SE, *DL, "loop-predication");
1245  bool Changed = false;
1246  for (auto *Guard : Guards)
1247  Changed |= widenGuardConditions(Guard, Expander);
1248  for (auto *Guard : GuardsAsWidenableBranches)
1249  Changed |= widenWidenableBranchGuardConditions(Guard, Expander);
1250  Changed |= predicateLoopExits(L, Expander);
1251  return Changed;
1252 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:78
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:899
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:71
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2356
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:209
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
STATISTIC(TotalConsidered, "Number of guards considered")
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:777
This class represents lattice values for constants.
Definition: AllocatorList.h:23
loop predication
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are d...
The main scalar evolution driver.
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition: IRBuilder.h:2584
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:160
unsigned less or equal
Definition: InstrTypes.h:758
unsigned less than
Definition: InstrTypes.h:757
BasicBlock * getSuccessor(unsigned i) const
bool isMonotonicPredicate(const SCEVAddRecExpr *LHS, ICmpInst::Predicate Pred, bool &Increasing)
Return true if, for all loop invariant X, the predicate "LHS `Pred` X" is monotonically increasing or...
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
F(f)
Value * getCondition() const
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:624
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:144
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Definition: LoopInfo.cpp:68
INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication", "Loop predication", false, false) INITIALIZE_PASS_END(LoopPredicationLegacyPass
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4466
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:140
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
Definition: BasicBlock.cpp:181
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:659
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
Definition: InstrTypes.h:831
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
static cl::opt< float > LatchExitProbabilityScale("loop-predication-latch-probability-scale", cl::Hidden, cl::init(2.0), cl::desc("scale factor for the latch probability. Value should be greater " "than 1. Lower values are ignored"))
BlockT * getHeader() const
Definition: LoopInfo.h:105
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:102
Analysis pass which computes BranchProbabilityInfo.
bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS...
static const SCEV * getMinAnalyzeableBackedgeTakenCount(ScalarEvolution &SE, DominatorTree &DT, Loop *L)
Return the minimum of all analyzeable exit counts.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
This node represents a polynomial recurrence on the trip count of the specified loop.
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:275
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
const SCEV * getExitCount(const Loop *L, BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2009
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
Legacy analysis pass which computes BranchProbabilityInfo.
Value * getOperand(unsigned i) const
Definition: User.h:169
static cl::opt< bool > EnableIVTruncation("loop-predication-enable-iv-truncation", cl::Hidden, cl::init(true))
const SCEV * getCouldNotCompute()
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
void dump() const
Definition: LoopInfo.cpp:648
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:240
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
void setInsertPoint(Instruction *IP)
Set the current insertion point.
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:335
Conditional or Unconditional Branch instruction.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:224
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
Represent the analysis usage information of a pass.
bool parseWidenableBranch(const User *U, Value *&Condition, Value *&WidenableCondition, BasicBlock *&IfTrueBB, BasicBlock *&IfFalseBB)
If U is widenable branch looking like: cond = ...
Definition: GuardUtils.cpp:44
Pass * createLoopPredicationPass()
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:732
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:440
void getExitingBlocks(SmallVectorImpl< BlockT *> &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
Definition: LoopInfoImpl.h:34
const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
Promote the operands to the wider of the types using zero-extension, and then perform a umin operatio...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
size_t size() const
Definition: SmallVector.h:52
static void normalizePredicate(ScalarEvolution *SE, Loop *L, LoopICmp &RC)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:62
signed greater than
Definition: InstrTypes.h:759
void getExitEdges(SmallVectorImpl< Edge > &ExitEdges) const
Return all pairs of (inside_block,outside_block).
Definition: LoopInfoImpl.h:141
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:115
static BranchInst * FindWidenableTerminatorAboveLoop(Loop *L, LoopInfo &LI)
If we can (cheaply) find a widenable branch which controls entry into the loop, return it...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
bool isAllOnesValue() const
Return true if the expression is a constant all-ones value.
Type * getType() const
Return the LLVM type of this SCEV expression.
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:1161
static cl::opt< bool > LoopPredication("indvars-predicate-loops", cl::Hidden, cl::init(true), cl::desc("Predicate conditions in read only loops"))
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:249
Module.h This file contains the declarations for the Module class.
signed less than
Definition: InstrTypes.h:761
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
bool isConditional() const
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:660
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:862
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental.guard intrinsic.
Definition: GuardUtils.cpp:18
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
signed less or equal
Definition: InstrTypes.h:762
static cl::opt< bool > EnableCountDownLoop("loop-predication-enable-count-down-loop", cl::Hidden, cl::init(true))
This class uses information about analyze scalars to rewrite expressions in canonical form...
void widenWidenableBranch(BranchInst *WidenableBR, Value *NewCond)
Given a branch we know is widenable (defined per Analysis/GuardUtils.h), widen it such that condition...
Definition: GuardUtils.cpp:82
static cl::opt< bool > PredicateWidenableBranchGuards("loop-predication-predicate-widenable-branches-to-deopt", cl::Hidden, cl::desc("Whether or not we should predicate guards " "expressed as widenable branches to deoptimize blocks"), cl::init(true))
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:340
static Optional< LoopICmp > generateLoopLatchCheck(const DataLayout &DL, ScalarEvolution &SE, const LoopICmp LatchCheck, Type *RangeCheckType)
Virtual Register Rewriter
Definition: VirtRegMap.cpp:220
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:807
Analysis providing branch probability information.
This class represents an analyzed expression in the program.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
unsigned greater or equal
Definition: InstrTypes.h:756
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
bool isEquality() const
Return true if this predicate is either EQ or NE.
#define I(x, y, z)
Definition: MD5.cpp:58
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass&#39;s AnalysisUsage.
Definition: LoopUtils.cpp:138
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
static cl::opt< bool > SkipProfitabilityChecks("loop-predication-skip-profitability-checks", cl::Hidden, cl::init(false))
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1361
void setCondition(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void initializeLoopPredicationLegacyPassPass(PassRegistry &)
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
bool isOne() const
Return true if the expression is a constant one.
LLVM Value Representation.
Definition: Value.h:74
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
bool isGuardAsWidenableBranch(const User *U)
Returns true iff U has semantics of a guard expressed in a form of a widenable conditional branch to ...
Definition: GuardUtils.cpp:29
unsigned greater than
Definition: InstrTypes.h:755
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:847
A container for analyses that lazily runs them and caches their results.
static bool isSafeToTruncateWideIVType(const DataLayout &DL, ScalarEvolution &SE, const LoopICmp LatchCheck, Type *RangeCheckType)
#define LLVM_DEBUG(X)
Definition: Debug.h:122
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:161
bool use_empty() const
Definition: Value.h:343
static bool isVeryLikelyToDeopt(BasicBlock *BB)
Return true if we can be fairly sure that executing block BB will probably lead to executing an __llv...
signed greater or equal
Definition: InstrTypes.h:760
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
This class represents a constant integer value.
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
Definition: BasicBlock.cpp:283