1 //===-- SimplifyIndVar.cpp - Induction variable simplification ------------===//
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 // This file implements induction variable simplification. It does
10 // not define any actual pass or policy, but provides a single function to
11 // simplify a loop's induction variables based on ScalarEvolution.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/ScalarEvolutionExpander.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/PatternMatch.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "indvars"
34 
35 STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
36 STATISTIC(NumElimOperand,  "Number of IV operands folded into a use");
37 STATISTIC(NumFoldedUser, "Number of IV users folded into a constant");
38 STATISTIC(NumElimRem     , "Number of IV remainder operations eliminated");
39 STATISTIC(
40     NumSimplifiedSDiv,
41     "Number of IV signed division operations converted to unsigned division");
42 STATISTIC(
43     NumSimplifiedSRem,
44     "Number of IV signed remainder operations converted to unsigned remainder");
45 STATISTIC(NumElimCmp     , "Number of IV comparisons eliminated");
46 
47 namespace {
48   /// This is a utility for simplifying induction variables
49   /// based on ScalarEvolution. It is the primary instrument of the
50   /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
51   /// other loop passes that preserve SCEV.
52   class SimplifyIndvar {
53     Loop             *L;
54     LoopInfo         *LI;
55     ScalarEvolution  *SE;
56     DominatorTree    *DT;
57     SCEVExpander     &Rewriter;
58     SmallVectorImpl<WeakTrackingVH> &DeadInsts;
59 
60     bool Changed;
61 
62   public:
63     SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
64                    LoopInfo *LI, SCEVExpander &Rewriter,
65                    SmallVectorImpl<WeakTrackingVH> &Dead)
66         : L(Loop), LI(LI), SE(SE), DT(DT), Rewriter(Rewriter), DeadInsts(Dead),
67           Changed(false) {
68       assert(LI && "IV simplification requires LoopInfo");
69     }
70 
71     bool hasChanged() const { return Changed; }
72 
73     /// Iteratively perform simplification on a worklist of users of the
74     /// specified induction variable. This is the top-level driver that applies
75     /// all simplifications to users of an IV.
76     void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
77 
78     Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
79 
80     bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand);
81     bool replaceIVUserWithLoopInvariant(Instruction *UseInst);
82 
83     bool eliminateOverflowIntrinsic(WithOverflowInst *WO);
84     bool eliminateTrunc(TruncInst *TI);
85     bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
86     bool makeIVComparisonInvariant(ICmpInst *ICmp, Value *IVOperand);
87     void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
88     void simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
89                              bool IsSigned);
90     void replaceRemWithNumerator(BinaryOperator *Rem);
91     void replaceRemWithNumeratorOrZero(BinaryOperator *Rem);
92     void replaceSRemWithURem(BinaryOperator *Rem);
93     bool eliminateSDiv(BinaryOperator *SDiv);
94     bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
95     bool strengthenRightShift(BinaryOperator *BO, Value *IVOperand);
96   };
97 }
98 
99 /// Fold an IV operand into its use.  This removes increments of an
100 /// aligned IV when used by a instruction that ignores the low bits.
101 ///
102 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
103 ///
104 /// Return the operand of IVOperand for this induction variable if IVOperand can
105 /// be folded (in case more folding opportunities have been exposed).
106 /// Otherwise return null.
107 Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
108   Value *IVSrc = nullptr;
109   const unsigned OperIdx = 0;
110   const SCEV *FoldedExpr = nullptr;
111   bool MustDropExactFlag = false;
112   switch (UseInst->getOpcode()) {
113   default:
114     return nullptr;
115   case Instruction::UDiv:
116   case Instruction::LShr:
117     // We're only interested in the case where we know something about
118     // the numerator and have a constant denominator.
119     if (IVOperand != UseInst->getOperand(OperIdx) ||
120         !isa<ConstantInt>(UseInst->getOperand(1)))
121       return nullptr;
122 
123     // Attempt to fold a binary operator with constant operand.
124     // e.g. ((I + 1) >> 2) => I >> 2
125     if (!isa<BinaryOperator>(IVOperand)
126         || !isa<ConstantInt>(IVOperand->getOperand(1)))
127       return nullptr;
128 
129     IVSrc = IVOperand->getOperand(0);
130     // IVSrc must be the (SCEVable) IV, since the other operand is const.
131     assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
132 
133     ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
134     if (UseInst->getOpcode() == Instruction::LShr) {
135       // Get a constant for the divisor. See createSCEV.
136       uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
137       if (D->getValue().uge(BitWidth))
138         return nullptr;
139 
140       D = ConstantInt::get(UseInst->getContext(),
141                            APInt::getOneBitSet(BitWidth, D->getZExtValue()));
142     }
143     FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
144     // We might have 'exact' flag set at this point which will no longer be
145     // correct after we make the replacement.
146     if (UseInst->isExact() &&
147         SE->getSCEV(IVSrc) != SE->getMulExpr(FoldedExpr, SE->getSCEV(D)))
148       MustDropExactFlag = true;
149   }
150   // We have something that might fold it's operand. Compare SCEVs.
151   if (!SE->isSCEVable(UseInst->getType()))
152     return nullptr;
153 
154   // Bypass the operand if SCEV can prove it has no effect.
155   if (SE->getSCEV(UseInst) != FoldedExpr)
156     return nullptr;
157 
158   LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
159                     << " -> " << *UseInst << '\n');
160 
161   UseInst->setOperand(OperIdx, IVSrc);
162   assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
163 
164   if (MustDropExactFlag)
165     UseInst->dropPoisonGeneratingFlags();
166 
167   ++NumElimOperand;
168   Changed = true;
169   if (IVOperand->use_empty())
170     DeadInsts.emplace_back(IVOperand);
171   return IVSrc;
172 }
173 
174 bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
175                                                Value *IVOperand) {
176   unsigned IVOperIdx = 0;
177   ICmpInst::Predicate Pred = ICmp->getPredicate();
178   if (IVOperand != ICmp->getOperand(0)) {
179     // Swapped
180     assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
181     IVOperIdx = 1;
182     Pred = ICmpInst::getSwappedPredicate(Pred);
183   }
184 
185   // Get the SCEVs for the ICmp operands (in the specific context of the
186   // current loop)
187   const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
188   const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
189   const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
190 
191   ICmpInst::Predicate InvariantPredicate;
192   const SCEV *InvariantLHS, *InvariantRHS;
193 
194   auto *PN = dyn_cast<PHINode>(IVOperand);
195   if (!PN)
196     return false;
197   if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
198                                     InvariantLHS, InvariantRHS))
199     return false;
200 
201   // Rewrite the comparison to a loop invariant comparison if it can be done
202   // cheaply, where cheaply means "we don't need to emit any new
203   // instructions".
204 
205   SmallDenseMap<const SCEV*, Value*> CheapExpansions;
206   CheapExpansions[S] = ICmp->getOperand(IVOperIdx);
207   CheapExpansions[X] = ICmp->getOperand(1 - IVOperIdx);
208 
209   // TODO: Support multiple entry loops?  (We currently bail out of these in
210   // the IndVarSimplify pass)
211   if (auto *BB = L->getLoopPredecessor()) {
212     const int Idx = PN->getBasicBlockIndex(BB);
213     if (Idx >= 0) {
214       Value *Incoming = PN->getIncomingValue(Idx);
215       const SCEV *IncomingS = SE->getSCEV(Incoming);
216       CheapExpansions[IncomingS] = Incoming;
217     }
218   }
219   Value *NewLHS = CheapExpansions[InvariantLHS];
220   Value *NewRHS = CheapExpansions[InvariantRHS];
221 
222   if (!NewLHS)
223     if (auto *ConstLHS = dyn_cast<SCEVConstant>(InvariantLHS))
224       NewLHS = ConstLHS->getValue();
225   if (!NewRHS)
226     if (auto *ConstRHS = dyn_cast<SCEVConstant>(InvariantRHS))
227       NewRHS = ConstRHS->getValue();
228 
229   if (!NewLHS || !NewRHS)
230     // We could not find an existing value to replace either LHS or RHS.
231     // Generating new instructions has subtler tradeoffs, so avoid doing that
232     // for now.
233     return false;
234 
235   LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
236   ICmp->setPredicate(InvariantPredicate);
237   ICmp->setOperand(0, NewLHS);
238   ICmp->setOperand(1, NewRHS);
239   return true;
240 }
241 
242 /// SimplifyIVUsers helper for eliminating useless
243 /// comparisons against an induction variable.
244 void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
245   unsigned IVOperIdx = 0;
246   ICmpInst::Predicate Pred = ICmp->getPredicate();
247   ICmpInst::Predicate OriginalPred = Pred;
248   if (IVOperand != ICmp->getOperand(0)) {
249     // Swapped
250     assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
251     IVOperIdx = 1;
252     Pred = ICmpInst::getSwappedPredicate(Pred);
253   }
254 
255   // Get the SCEVs for the ICmp operands (in the specific context of the
256   // current loop)
257   const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
258   const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
259   const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
260 
261   // If the condition is always true or always false, replace it with
262   // a constant value.
263   if (SE->isKnownPredicate(Pred, S, X)) {
264     ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
265     DeadInsts.emplace_back(ICmp);
266     LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
267   } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) {
268     ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
269     DeadInsts.emplace_back(ICmp);
270     LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
271   } else if (makeIVComparisonInvariant(ICmp, IVOperand)) {
272     // fallthrough to end of function
273   } else if (ICmpInst::isSigned(OriginalPred) &&
274              SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) {
275     // If we were unable to make anything above, all we can is to canonicalize
276     // the comparison hoping that it will open the doors for other
277     // optimizations. If we find out that we compare two non-negative values,
278     // we turn the instruction's predicate to its unsigned version. Note that
279     // we cannot rely on Pred here unless we check if we have swapped it.
280     assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
281     LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp
282                       << '\n');
283     ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
284   } else
285     return;
286 
287   ++NumElimCmp;
288   Changed = true;
289 }
290 
291 bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
292   // Get the SCEVs for the ICmp operands.
293   auto *N = SE->getSCEV(SDiv->getOperand(0));
294   auto *D = SE->getSCEV(SDiv->getOperand(1));
295 
296   // Simplify unnecessary loops away.
297   const Loop *L = LI->getLoopFor(SDiv->getParent());
298   N = SE->getSCEVAtScope(N, L);
299   D = SE->getSCEVAtScope(D, L);
300 
301   // Replace sdiv by udiv if both of the operands are non-negative
302   if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
303     auto *UDiv = BinaryOperator::Create(
304         BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
305         SDiv->getName() + ".udiv", SDiv);
306     UDiv->setIsExact(SDiv->isExact());
307     SDiv->replaceAllUsesWith(UDiv);
308     LLVM_DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
309     ++NumSimplifiedSDiv;
310     Changed = true;
311     DeadInsts.push_back(SDiv);
312     return true;
313   }
314 
315   return false;
316 }
317 
318 // i %s n -> i %u n if i >= 0 and n >= 0
319 void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) {
320   auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
321   auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D,
322                                       Rem->getName() + ".urem", Rem);
323   Rem->replaceAllUsesWith(URem);
324   LLVM_DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n');
325   ++NumSimplifiedSRem;
326   Changed = true;
327   DeadInsts.emplace_back(Rem);
328 }
329 
330 // i % n  -->  i  if i is in [0,n).
331 void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) {
332   Rem->replaceAllUsesWith(Rem->getOperand(0));
333   LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
334   ++NumElimRem;
335   Changed = true;
336   DeadInsts.emplace_back(Rem);
337 }
338 
339 // (i+1) % n  -->  (i+1)==n?0:(i+1)  if i is in [0,n).
340 void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) {
341   auto *T = Rem->getType();
342   auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
343   ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D);
344   SelectInst *Sel =
345       SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem);
346   Rem->replaceAllUsesWith(Sel);
347   LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
348   ++NumElimRem;
349   Changed = true;
350   DeadInsts.emplace_back(Rem);
351 }
352 
353 /// SimplifyIVUsers helper for eliminating useless remainder operations
354 /// operating on an induction variable or replacing srem by urem.
355 void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
356                                          bool IsSigned) {
357   auto *NValue = Rem->getOperand(0);
358   auto *DValue = Rem->getOperand(1);
359   // We're only interested in the case where we know something about
360   // the numerator, unless it is a srem, because we want to replace srem by urem
361   // in general.
362   bool UsedAsNumerator = IVOperand == NValue;
363   if (!UsedAsNumerator && !IsSigned)
364     return;
365 
366   const SCEV *N = SE->getSCEV(NValue);
367 
368   // Simplify unnecessary loops away.
369   const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
370   N = SE->getSCEVAtScope(N, ICmpLoop);
371 
372   bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
373 
374   // Do not proceed if the Numerator may be negative
375   if (!IsNumeratorNonNegative)
376     return;
377 
378   const SCEV *D = SE->getSCEV(DValue);
379   D = SE->getSCEVAtScope(D, ICmpLoop);
380 
381   if (UsedAsNumerator) {
382     auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
383     if (SE->isKnownPredicate(LT, N, D)) {
384       replaceRemWithNumerator(Rem);
385       return;
386     }
387 
388     auto *T = Rem->getType();
389     const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
390     if (SE->isKnownPredicate(LT, NLessOne, D)) {
391       replaceRemWithNumeratorOrZero(Rem);
392       return;
393     }
394   }
395 
396   // Try to replace SRem with URem, if both N and D are known non-negative.
397   // Since we had already check N, we only need to check D now
398   if (!IsSigned || !SE->isKnownNonNegative(D))
399     return;
400 
401   replaceSRemWithURem(Rem);
402 }
403 
404 static bool willNotOverflow(ScalarEvolution *SE, Instruction::BinaryOps BinOp,
405                             bool Signed, const SCEV *LHS, const SCEV *RHS) {
406   const SCEV *(ScalarEvolution::*Operation)(const SCEV *, const SCEV *,
407                                             SCEV::NoWrapFlags, unsigned);
408   switch (BinOp) {
409   default:
410     llvm_unreachable("Unsupported binary op");
411   case Instruction::Add:
412     Operation = &ScalarEvolution::getAddExpr;
413     break;
414   case Instruction::Sub:
415     Operation = &ScalarEvolution::getMinusSCEV;
416     break;
417   case Instruction::Mul:
418     Operation = &ScalarEvolution::getMulExpr;
419     break;
420   }
421 
422   const SCEV *(ScalarEvolution::*Extension)(const SCEV *, Type *, unsigned) =
423       Signed ? &ScalarEvolution::getSignExtendExpr
424              : &ScalarEvolution::getZeroExtendExpr;
425 
426   // Check ext(LHS op RHS) == ext(LHS) op ext(RHS)
427   auto *NarrowTy = cast<IntegerType>(LHS->getType());
428   auto *WideTy =
429     IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
430 
431   const SCEV *A =
432       (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0),
433                        WideTy, 0);
434   const SCEV *B =
435       (SE->*Operation)((SE->*Extension)(LHS, WideTy, 0),
436                        (SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0);
437   return A == B;
438 }
439 
440 bool SimplifyIndvar::eliminateOverflowIntrinsic(WithOverflowInst *WO) {
441   const SCEV *LHS = SE->getSCEV(WO->getLHS());
442   const SCEV *RHS = SE->getSCEV(WO->getRHS());
443   if (!willNotOverflow(SE, WO->getBinaryOp(), WO->isSigned(), LHS, RHS))
444     return false;
445 
446   // Proved no overflow, nuke the overflow check and, if possible, the overflow
447   // intrinsic as well.
448 
449   BinaryOperator *NewResult = BinaryOperator::Create(
450       WO->getBinaryOp(), WO->getLHS(), WO->getRHS(), "", WO);
451 
452   if (WO->isSigned())
453     NewResult->setHasNoSignedWrap(true);
454   else
455     NewResult->setHasNoUnsignedWrap(true);
456 
457   SmallVector<ExtractValueInst *, 4> ToDelete;
458 
459   for (auto *U : WO->users()) {
460     if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
461       if (EVI->getIndices()[0] == 1)
462         EVI->replaceAllUsesWith(ConstantInt::getFalse(WO->getContext()));
463       else {
464         assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
465         EVI->replaceAllUsesWith(NewResult);
466       }
467       ToDelete.push_back(EVI);
468     }
469   }
470 
471   for (auto *EVI : ToDelete)
472     EVI->eraseFromParent();
473 
474   if (WO->use_empty())
475     WO->eraseFromParent();
476 
477   return true;
478 }
479 
480 bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) {
481   // It is always legal to replace
482   //   icmp <pred> i32 trunc(iv), n
483   // with
484   //   icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate.
485   // Or with
486   //   icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate.
487   // Or with either of these if pred is an equality predicate.
488   //
489   // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for
490   // every comparison which uses trunc, it means that we can replace each of
491   // them with comparison of iv against sext/zext(n). We no longer need trunc
492   // after that.
493   //
494   // TODO: Should we do this if we can widen *some* comparisons, but not all
495   // of them? Sometimes it is enough to enable other optimizations, but the
496   // trunc instruction will stay in the loop.
497   Value *IV = TI->getOperand(0);
498   Type *IVTy = IV->getType();
499   const SCEV *IVSCEV = SE->getSCEV(IV);
500   const SCEV *TISCEV = SE->getSCEV(TI);
501 
502   // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can
503   // get rid of trunc
504   bool DoesSExtCollapse = false;
505   bool DoesZExtCollapse = false;
506   if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy))
507     DoesSExtCollapse = true;
508   if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy))
509     DoesZExtCollapse = true;
510 
511   // If neither sext nor zext does collapse, it is not profitable to do any
512   // transform. Bail.
513   if (!DoesSExtCollapse && !DoesZExtCollapse)
514     return false;
515 
516   // Collect users of the trunc that look like comparisons against invariants.
517   // Bail if we find something different.
518   SmallVector<ICmpInst *, 4> ICmpUsers;
519   for (auto *U : TI->users()) {
520     // We don't care about users in unreachable blocks.
521     if (isa<Instruction>(U) &&
522         !DT->isReachableFromEntry(cast<Instruction>(U)->getParent()))
523       continue;
524     if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) {
525       if (ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) {
526         assert(L->contains(ICI->getParent()) && "LCSSA form broken?");
527         // If we cannot get rid of trunc, bail.
528         if (ICI->isSigned() && !DoesSExtCollapse)
529           return false;
530         if (ICI->isUnsigned() && !DoesZExtCollapse)
531           return false;
532         // For equality, either signed or unsigned works.
533         ICmpUsers.push_back(ICI);
534       } else
535         return false;
536     } else
537       return false;
538   }
539 
540   auto CanUseZExt = [&](ICmpInst *ICI) {
541     // Unsigned comparison can be widened as unsigned.
542     if (ICI->isUnsigned())
543       return true;
544     // Is it profitable to do zext?
545     if (!DoesZExtCollapse)
546       return false;
547     // For equality, we can safely zext both parts.
548     if (ICI->isEquality())
549       return true;
550     // Otherwise we can only use zext when comparing two non-negative or two
551     // negative values. But in practice, we will never pass DoesZExtCollapse
552     // check for a negative value, because zext(trunc(x)) is non-negative. So
553     // it only make sense to check for non-negativity here.
554     const SCEV *SCEVOP1 = SE->getSCEV(ICI->getOperand(0));
555     const SCEV *SCEVOP2 = SE->getSCEV(ICI->getOperand(1));
556     return SE->isKnownNonNegative(SCEVOP1) && SE->isKnownNonNegative(SCEVOP2);
557   };
558   // Replace all comparisons against trunc with comparisons against IV.
559   for (auto *ICI : ICmpUsers) {
560     auto *Op1 = ICI->getOperand(1);
561     Instruction *Ext = nullptr;
562     // For signed/unsigned predicate, replace the old comparison with comparison
563     // of immediate IV against sext/zext of the invariant argument. If we can
564     // use either sext or zext (i.e. we are dealing with equality predicate),
565     // then prefer zext as a more canonical form.
566     // TODO: If we see a signed comparison which can be turned into unsigned,
567     // we can do it here for canonicalization purposes.
568     ICmpInst::Predicate Pred = ICI->getPredicate();
569     if (CanUseZExt(ICI)) {
570       assert(DoesZExtCollapse && "Unprofitable zext?");
571       Ext = new ZExtInst(Op1, IVTy, "zext", ICI);
572       Pred = ICmpInst::getUnsignedPredicate(Pred);
573     } else {
574       assert(DoesSExtCollapse && "Unprofitable sext?");
575       Ext = new SExtInst(Op1, IVTy, "sext", ICI);
576       assert(Pred == ICmpInst::getSignedPredicate(Pred) && "Must be signed!");
577     }
578     bool Changed;
579     L->makeLoopInvariant(Ext, Changed);
580     (void)Changed;
581     ICmpInst *NewICI = new ICmpInst(ICI, Pred, IV, Ext);
582     ICI->replaceAllUsesWith(NewICI);
583     DeadInsts.emplace_back(ICI);
584   }
585 
586   // Trunc no longer needed.
587   TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
588   DeadInsts.emplace_back(TI);
589   return true;
590 }
591 
592 /// Eliminate an operation that consumes a simple IV and has no observable
593 /// side-effect given the range of IV values.  IVOperand is guaranteed SCEVable,
594 /// but UseInst may not be.
595 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
596                                      Instruction *IVOperand) {
597   if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
598     eliminateIVComparison(ICmp, IVOperand);
599     return true;
600   }
601   if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
602     bool IsSRem = Bin->getOpcode() == Instruction::SRem;
603     if (IsSRem || Bin->getOpcode() == Instruction::URem) {
604       simplifyIVRemainder(Bin, IVOperand, IsSRem);
605       return true;
606     }
607 
608     if (Bin->getOpcode() == Instruction::SDiv)
609       return eliminateSDiv(Bin);
610   }
611 
612   if (auto *WO = dyn_cast<WithOverflowInst>(UseInst))
613     if (eliminateOverflowIntrinsic(WO))
614       return true;
615 
616   if (auto *TI = dyn_cast<TruncInst>(UseInst))
617     if (eliminateTrunc(TI))
618       return true;
619 
620   if (eliminateIdentitySCEV(UseInst, IVOperand))
621     return true;
622 
623   return false;
624 }
625 
626 static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) {
627   if (auto *BB = L->getLoopPreheader())
628     return BB->getTerminator();
629 
630   return Hint;
631 }
632 
633 /// Replace the UseInst with a constant if possible.
634 bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) {
635   if (!SE->isSCEVable(I->getType()))
636     return false;
637 
638   // Get the symbolic expression for this instruction.
639   const SCEV *S = SE->getSCEV(I);
640 
641   if (!SE->isLoopInvariant(S, L))
642     return false;
643 
644   // Do not generate something ridiculous even if S is loop invariant.
645   if (Rewriter.isHighCostExpansion(S, L, I))
646     return false;
647 
648   auto *IP = GetLoopInvariantInsertPosition(L, I);
649   auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
650 
651   I->replaceAllUsesWith(Invariant);
652   LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
653                     << " with loop invariant: " << *S << '\n');
654   ++NumFoldedUser;
655   Changed = true;
656   DeadInsts.emplace_back(I);
657   return true;
658 }
659 
660 /// Eliminate any operation that SCEV can prove is an identity function.
661 bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
662                                            Instruction *IVOperand) {
663   if (!SE->isSCEVable(UseInst->getType()) ||
664       (UseInst->getType() != IVOperand->getType()) ||
665       (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
666     return false;
667 
668   // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
669   // dominator tree, even if X is an operand to Y.  For instance, in
670   //
671   //     %iv = phi i32 {0,+,1}
672   //     br %cond, label %left, label %merge
673   //
674   //   left:
675   //     %X = add i32 %iv, 0
676   //     br label %merge
677   //
678   //   merge:
679   //     %M = phi (%X, %iv)
680   //
681   // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
682   // %M.replaceAllUsesWith(%X) would be incorrect.
683 
684   if (isa<PHINode>(UseInst))
685     // If UseInst is not a PHI node then we know that IVOperand dominates
686     // UseInst directly from the legality of SSA.
687     if (!DT || !DT->dominates(IVOperand, UseInst))
688       return false;
689 
690   if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
691     return false;
692 
693   LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
694 
695   UseInst->replaceAllUsesWith(IVOperand);
696   ++NumElimIdentity;
697   Changed = true;
698   DeadInsts.emplace_back(UseInst);
699   return true;
700 }
701 
702 /// Annotate BO with nsw / nuw if it provably does not signed-overflow /
703 /// unsigned-overflow.  Returns true if anything changed, false otherwise.
704 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
705                                                     Value *IVOperand) {
706   // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
707   if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
708     return false;
709 
710   if (BO->getOpcode() != Instruction::Add &&
711       BO->getOpcode() != Instruction::Sub &&
712       BO->getOpcode() != Instruction::Mul)
713     return false;
714 
715   const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
716   const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
717   bool Changed = false;
718 
719   if (!BO->hasNoUnsignedWrap() &&
720       willNotOverflow(SE, BO->getOpcode(), /* Signed */ false, LHS, RHS)) {
721     BO->setHasNoUnsignedWrap();
722     SE->forgetValue(BO);
723     Changed = true;
724   }
725 
726   if (!BO->hasNoSignedWrap() &&
727       willNotOverflow(SE, BO->getOpcode(), /* Signed */ true, LHS, RHS)) {
728     BO->setHasNoSignedWrap();
729     SE->forgetValue(BO);
730     Changed = true;
731   }
732 
733   return Changed;
734 }
735 
736 /// Annotate the Shr in (X << IVOperand) >> C as exact using the
737 /// information from the IV's range. Returns true if anything changed, false
738 /// otherwise.
739 bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
740                                           Value *IVOperand) {
741   using namespace llvm::PatternMatch;
742 
743   if (BO->getOpcode() == Instruction::Shl) {
744     bool Changed = false;
745     ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
746     for (auto *U : BO->users()) {
747       const APInt *C;
748       if (match(U,
749                 m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
750           match(U,
751                 m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
752         BinaryOperator *Shr = cast<BinaryOperator>(U);
753         if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
754           Shr->setIsExact(true);
755           Changed = true;
756         }
757       }
758     }
759     return Changed;
760   }
761 
762   return false;
763 }
764 
765 /// Add all uses of Def to the current IV's worklist.
766 static void pushIVUsers(
767   Instruction *Def, Loop *L,
768   SmallPtrSet<Instruction*,16> &Simplified,
769   SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
770 
771   for (User *U : Def->users()) {
772     Instruction *UI = cast<Instruction>(U);
773 
774     // Avoid infinite or exponential worklist processing.
775     // Also ensure unique worklist users.
776     // If Def is a LoopPhi, it may not be in the Simplified set, so check for
777     // self edges first.
778     if (UI == Def)
779       continue;
780 
781     // Only change the current Loop, do not change the other parts (e.g. other
782     // Loops).
783     if (!L->contains(UI))
784       continue;
785 
786     // Do not push the same instruction more than once.
787     if (!Simplified.insert(UI).second)
788       continue;
789 
790     SimpleIVUsers.push_back(std::make_pair(UI, Def));
791   }
792 }
793 
794 /// Return true if this instruction generates a simple SCEV
795 /// expression in terms of that IV.
796 ///
797 /// This is similar to IVUsers' isInteresting() but processes each instruction
798 /// non-recursively when the operand is already known to be a simpleIVUser.
799 ///
800 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
801   if (!SE->isSCEVable(I->getType()))
802     return false;
803 
804   // Get the symbolic expression for this instruction.
805   const SCEV *S = SE->getSCEV(I);
806 
807   // Only consider affine recurrences.
808   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
809   if (AR && AR->getLoop() == L)
810     return true;
811 
812   return false;
813 }
814 
815 /// Iteratively perform simplification on a worklist of users
816 /// of the specified induction variable. Each successive simplification may push
817 /// more users which may themselves be candidates for simplification.
818 ///
819 /// This algorithm does not require IVUsers analysis. Instead, it simplifies
820 /// instructions in-place during analysis. Rather than rewriting induction
821 /// variables bottom-up from their users, it transforms a chain of IVUsers
822 /// top-down, updating the IR only when it encounters a clear optimization
823 /// opportunity.
824 ///
825 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
826 ///
827 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
828   if (!SE->isSCEVable(CurrIV->getType()))
829     return;
830 
831   // Instructions processed by SimplifyIndvar for CurrIV.
832   SmallPtrSet<Instruction*,16> Simplified;
833 
834   // Use-def pairs if IV users waiting to be processed for CurrIV.
835   SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
836 
837   // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
838   // called multiple times for the same LoopPhi. This is the proper thing to
839   // do for loop header phis that use each other.
840   pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers);
841 
842   while (!SimpleIVUsers.empty()) {
843     std::pair<Instruction*, Instruction*> UseOper =
844       SimpleIVUsers.pop_back_val();
845     Instruction *UseInst = UseOper.first;
846 
847     // If a user of the IndVar is trivially dead, we prefer just to mark it dead
848     // rather than try to do some complex analysis or transformation (such as
849     // widening) basing on it.
850     // TODO: Propagate TLI and pass it here to handle more cases.
851     if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) {
852       DeadInsts.emplace_back(UseInst);
853       continue;
854     }
855 
856     // Bypass back edges to avoid extra work.
857     if (UseInst == CurrIV) continue;
858 
859     // Try to replace UseInst with a loop invariant before any other
860     // simplifications.
861     if (replaceIVUserWithLoopInvariant(UseInst))
862       continue;
863 
864     Instruction *IVOperand = UseOper.second;
865     for (unsigned N = 0; IVOperand; ++N) {
866       assert(N <= Simplified.size() && "runaway iteration");
867 
868       Value *NewOper = foldIVUser(UseInst, IVOperand);
869       if (!NewOper)
870         break; // done folding
871       IVOperand = dyn_cast<Instruction>(NewOper);
872     }
873     if (!IVOperand)
874       continue;
875 
876     if (eliminateIVUser(UseInst, IVOperand)) {
877       pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
878       continue;
879     }
880 
881     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) {
882       if ((isa<OverflowingBinaryOperator>(BO) &&
883            strengthenOverflowingOperation(BO, IVOperand)) ||
884           (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) {
885         // re-queue uses of the now modified binary operator and fall
886         // through to the checks that remain.
887         pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
888       }
889     }
890 
891     CastInst *Cast = dyn_cast<CastInst>(UseInst);
892     if (V && Cast) {
893       V->visitCast(Cast);
894       continue;
895     }
896     if (isSimpleIVUser(UseInst, L, SE)) {
897       pushIVUsers(UseInst, L, Simplified, SimpleIVUsers);
898     }
899   }
900 }
901 
902 namespace llvm {
903 
904 void IVVisitor::anchor() { }
905 
906 /// Simplify instructions that use this induction variable
907 /// by using ScalarEvolution to analyze the IV's recurrence.
908 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
909                        LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead,
910                        SCEVExpander &Rewriter, IVVisitor *V) {
911   SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Rewriter,
912                      Dead);
913   SIV.simplifyUsers(CurrIV, V);
914   return SIV.hasChanged();
915 }
916 
917 /// Simplify users of induction variables within this
918 /// loop. This does not actually change or add IVs.
919 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
920                      LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) {
921   SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars");
922 #ifndef NDEBUG
923   Rewriter.setDebugType(DEBUG_TYPE);
924 #endif
925   bool Changed = false;
926   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
927     Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead, Rewriter);
928   }
929   return Changed;
930 }
931 
932 } // namespace llvm
933