xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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/SmallVector.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/PatternMatch.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Transforms/Utils/Local.h"
27 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
28 
29 using namespace llvm;
30 using namespace llvm::PatternMatch;
31 
32 #define DEBUG_TYPE "indvars"
33 
34 STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
35 STATISTIC(NumElimOperand,  "Number of IV operands folded into a use");
36 STATISTIC(NumFoldedUser, "Number of IV users folded into a constant");
37 STATISTIC(NumElimRem     , "Number of IV remainder operations eliminated");
38 STATISTIC(
39     NumSimplifiedSDiv,
40     "Number of IV signed division operations converted to unsigned division");
41 STATISTIC(
42     NumSimplifiedSRem,
43     "Number of IV signed remainder operations converted to unsigned remainder");
44 STATISTIC(NumElimCmp     , "Number of IV comparisons eliminated");
45 
46 namespace {
47   /// This is a utility for simplifying induction variables
48   /// based on ScalarEvolution. It is the primary instrument of the
49   /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
50   /// other loop passes that preserve SCEV.
51   class SimplifyIndvar {
52     Loop             *L;
53     LoopInfo         *LI;
54     ScalarEvolution  *SE;
55     DominatorTree    *DT;
56     const TargetTransformInfo *TTI;
57     SCEVExpander     &Rewriter;
58     SmallVectorImpl<WeakTrackingVH> &DeadInsts;
59 
60     bool Changed = false;
61 
62   public:
63     SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
64                    LoopInfo *LI, const TargetTransformInfo *TTI,
65                    SCEVExpander &Rewriter,
66                    SmallVectorImpl<WeakTrackingVH> &Dead)
67         : L(Loop), LI(LI), SE(SE), DT(DT), TTI(TTI), Rewriter(Rewriter),
68           DeadInsts(Dead) {
69       assert(LI && "IV simplification requires LoopInfo");
70     }
71 
72     bool hasChanged() const { return Changed; }
73 
74     /// Iteratively perform simplification on a worklist of users of the
75     /// specified induction variable. This is the top-level driver that applies
76     /// all simplifications to users of an IV.
77     void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
78 
79     Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
80 
81     bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand);
82     bool replaceIVUserWithLoopInvariant(Instruction *UseInst);
83     bool replaceFloatIVWithIntegerIV(Instruction *UseInst);
84 
85     bool eliminateOverflowIntrinsic(WithOverflowInst *WO);
86     bool eliminateSaturatingIntrinsic(SaturatingInst *SI);
87     bool eliminateTrunc(TruncInst *TI);
88     bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
89     bool makeIVComparisonInvariant(ICmpInst *ICmp, Instruction *IVOperand);
90     void eliminateIVComparison(ICmpInst *ICmp, Instruction *IVOperand);
91     void simplifyIVRemainder(BinaryOperator *Rem, Instruction *IVOperand,
92                              bool IsSigned);
93     void replaceRemWithNumerator(BinaryOperator *Rem);
94     void replaceRemWithNumeratorOrZero(BinaryOperator *Rem);
95     void replaceSRemWithURem(BinaryOperator *Rem);
96     bool eliminateSDiv(BinaryOperator *SDiv);
97     bool strengthenBinaryOp(BinaryOperator *BO, Instruction *IVOperand);
98     bool strengthenOverflowingOperation(BinaryOperator *OBO,
99                                         Instruction *IVOperand);
100     bool strengthenRightShift(BinaryOperator *BO, Instruction *IVOperand);
101   };
102 }
103 
104 /// Find a point in code which dominates all given instructions. We can safely
105 /// assume that, whatever fact we can prove at the found point, this fact is
106 /// also true for each of the given instructions.
107 static Instruction *findCommonDominator(ArrayRef<Instruction *> Instructions,
108                                         DominatorTree &DT) {
109   Instruction *CommonDom = nullptr;
110   for (auto *Insn : Instructions)
111     CommonDom =
112         CommonDom ? DT.findNearestCommonDominator(CommonDom, Insn) : Insn;
113   assert(CommonDom && "Common dominator not found?");
114   return CommonDom;
115 }
116 
117 /// Fold an IV operand into its use.  This removes increments of an
118 /// aligned IV when used by a instruction that ignores the low bits.
119 ///
120 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
121 ///
122 /// Return the operand of IVOperand for this induction variable if IVOperand can
123 /// be folded (in case more folding opportunities have been exposed).
124 /// Otherwise return null.
125 Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
126   Value *IVSrc = nullptr;
127   const unsigned OperIdx = 0;
128   const SCEV *FoldedExpr = nullptr;
129   bool MustDropExactFlag = false;
130   switch (UseInst->getOpcode()) {
131   default:
132     return nullptr;
133   case Instruction::UDiv:
134   case Instruction::LShr:
135     // We're only interested in the case where we know something about
136     // the numerator and have a constant denominator.
137     if (IVOperand != UseInst->getOperand(OperIdx) ||
138         !isa<ConstantInt>(UseInst->getOperand(1)))
139       return nullptr;
140 
141     // Attempt to fold a binary operator with constant operand.
142     // e.g. ((I + 1) >> 2) => I >> 2
143     if (!isa<BinaryOperator>(IVOperand)
144         || !isa<ConstantInt>(IVOperand->getOperand(1)))
145       return nullptr;
146 
147     IVSrc = IVOperand->getOperand(0);
148     // IVSrc must be the (SCEVable) IV, since the other operand is const.
149     assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
150 
151     ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
152     if (UseInst->getOpcode() == Instruction::LShr) {
153       // Get a constant for the divisor. See createSCEV.
154       uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
155       if (D->getValue().uge(BitWidth))
156         return nullptr;
157 
158       D = ConstantInt::get(UseInst->getContext(),
159                            APInt::getOneBitSet(BitWidth, D->getZExtValue()));
160     }
161     const auto *LHS = SE->getSCEV(IVSrc);
162     const auto *RHS = SE->getSCEV(D);
163     FoldedExpr = SE->getUDivExpr(LHS, RHS);
164     // We might have 'exact' flag set at this point which will no longer be
165     // correct after we make the replacement.
166     if (UseInst->isExact() && LHS != SE->getMulExpr(FoldedExpr, RHS))
167       MustDropExactFlag = true;
168   }
169   // We have something that might fold it's operand. Compare SCEVs.
170   if (!SE->isSCEVable(UseInst->getType()))
171     return nullptr;
172 
173   // Bypass the operand if SCEV can prove it has no effect.
174   if (SE->getSCEV(UseInst) != FoldedExpr)
175     return nullptr;
176 
177   LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
178                     << " -> " << *UseInst << '\n');
179 
180   UseInst->setOperand(OperIdx, IVSrc);
181   assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
182 
183   if (MustDropExactFlag)
184     UseInst->dropPoisonGeneratingFlags();
185 
186   ++NumElimOperand;
187   Changed = true;
188   if (IVOperand->use_empty())
189     DeadInsts.emplace_back(IVOperand);
190   return IVSrc;
191 }
192 
193 bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
194                                                Instruction *IVOperand) {
195   auto *Preheader = L->getLoopPreheader();
196   if (!Preheader)
197     return false;
198   unsigned IVOperIdx = 0;
199   ICmpInst::Predicate Pred = ICmp->getPredicate();
200   if (IVOperand != ICmp->getOperand(0)) {
201     // Swapped
202     assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
203     IVOperIdx = 1;
204     Pred = ICmpInst::getSwappedPredicate(Pred);
205   }
206 
207   // Get the SCEVs for the ICmp operands (in the specific context of the
208   // current loop)
209   const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
210   const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
211   const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
212   auto LIP = SE->getLoopInvariantPredicate(Pred, S, X, L, ICmp);
213   if (!LIP)
214     return false;
215   ICmpInst::Predicate InvariantPredicate = LIP->Pred;
216   const SCEV *InvariantLHS = LIP->LHS;
217   const SCEV *InvariantRHS = LIP->RHS;
218 
219   // Do not generate something ridiculous.
220   auto *PHTerm = Preheader->getTerminator();
221   if (Rewriter.isHighCostExpansion({InvariantLHS, InvariantRHS}, L,
222                                    2 * SCEVCheapExpansionBudget, TTI, PHTerm) ||
223       !Rewriter.isSafeToExpandAt(InvariantLHS, PHTerm) ||
224       !Rewriter.isSafeToExpandAt(InvariantRHS, PHTerm))
225     return false;
226   auto *NewLHS =
227       Rewriter.expandCodeFor(InvariantLHS, IVOperand->getType(), PHTerm);
228   auto *NewRHS =
229       Rewriter.expandCodeFor(InvariantRHS, IVOperand->getType(), PHTerm);
230   LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
231   ICmp->setPredicate(InvariantPredicate);
232   ICmp->setOperand(0, NewLHS);
233   ICmp->setOperand(1, NewRHS);
234   return true;
235 }
236 
237 /// SimplifyIVUsers helper for eliminating useless
238 /// comparisons against an induction variable.
239 void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
240                                            Instruction *IVOperand) {
241   unsigned IVOperIdx = 0;
242   ICmpInst::Predicate Pred = ICmp->getPredicate();
243   ICmpInst::Predicate OriginalPred = Pred;
244   if (IVOperand != ICmp->getOperand(0)) {
245     // Swapped
246     assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
247     IVOperIdx = 1;
248     Pred = ICmpInst::getSwappedPredicate(Pred);
249   }
250 
251   // Get the SCEVs for the ICmp operands (in the specific context of the
252   // current loop)
253   const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
254   const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
255   const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
256 
257   // If the condition is always true or always false in the given context,
258   // replace it with a constant value.
259   SmallVector<Instruction *, 4> Users;
260   for (auto *U : ICmp->users())
261     Users.push_back(cast<Instruction>(U));
262   const Instruction *CtxI = findCommonDominator(Users, *DT);
263   if (auto Ev = SE->evaluatePredicateAt(Pred, S, X, CtxI)) {
264     SE->forgetValue(ICmp);
265     ICmp->replaceAllUsesWith(ConstantInt::getBool(ICmp->getContext(), *Ev));
266     DeadInsts.emplace_back(ICmp);
267     LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
268   } else if (makeIVComparisonInvariant(ICmp, IVOperand)) {
269     // fallthrough to end of function
270   } else if (ICmpInst::isSigned(OriginalPred) &&
271              SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) {
272     // If we were unable to make anything above, all we can is to canonicalize
273     // the comparison hoping that it will open the doors for other
274     // optimizations. If we find out that we compare two non-negative values,
275     // we turn the instruction's predicate to its unsigned version. Note that
276     // we cannot rely on Pred here unless we check if we have swapped it.
277     assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
278     LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp
279                       << '\n');
280     ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
281   } else
282     return;
283 
284   ++NumElimCmp;
285   Changed = true;
286 }
287 
288 bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
289   // Get the SCEVs for the ICmp operands.
290   auto *N = SE->getSCEV(SDiv->getOperand(0));
291   auto *D = SE->getSCEV(SDiv->getOperand(1));
292 
293   // Simplify unnecessary loops away.
294   const Loop *L = LI->getLoopFor(SDiv->getParent());
295   N = SE->getSCEVAtScope(N, L);
296   D = SE->getSCEVAtScope(D, L);
297 
298   // Replace sdiv by udiv if both of the operands are non-negative
299   if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
300     auto *UDiv = BinaryOperator::Create(
301         BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
302         SDiv->getName() + ".udiv", SDiv);
303     UDiv->setIsExact(SDiv->isExact());
304     SDiv->replaceAllUsesWith(UDiv);
305     LLVM_DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
306     ++NumSimplifiedSDiv;
307     Changed = true;
308     DeadInsts.push_back(SDiv);
309     return true;
310   }
311 
312   return false;
313 }
314 
315 // i %s n -> i %u n if i >= 0 and n >= 0
316 void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) {
317   auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
318   auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D,
319                                       Rem->getName() + ".urem", Rem);
320   Rem->replaceAllUsesWith(URem);
321   LLVM_DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n');
322   ++NumSimplifiedSRem;
323   Changed = true;
324   DeadInsts.emplace_back(Rem);
325 }
326 
327 // i % n  -->  i  if i is in [0,n).
328 void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) {
329   Rem->replaceAllUsesWith(Rem->getOperand(0));
330   LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
331   ++NumElimRem;
332   Changed = true;
333   DeadInsts.emplace_back(Rem);
334 }
335 
336 // (i+1) % n  -->  (i+1)==n?0:(i+1)  if i is in [0,n).
337 void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) {
338   auto *T = Rem->getType();
339   auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
340   ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D);
341   SelectInst *Sel =
342       SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem);
343   Rem->replaceAllUsesWith(Sel);
344   LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
345   ++NumElimRem;
346   Changed = true;
347   DeadInsts.emplace_back(Rem);
348 }
349 
350 /// SimplifyIVUsers helper for eliminating useless remainder operations
351 /// operating on an induction variable or replacing srem by urem.
352 void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem,
353                                          Instruction *IVOperand,
354                                          bool IsSigned) {
355   auto *NValue = Rem->getOperand(0);
356   auto *DValue = Rem->getOperand(1);
357   // We're only interested in the case where we know something about
358   // the numerator, unless it is a srem, because we want to replace srem by urem
359   // in general.
360   bool UsedAsNumerator = IVOperand == NValue;
361   if (!UsedAsNumerator && !IsSigned)
362     return;
363 
364   const SCEV *N = SE->getSCEV(NValue);
365 
366   // Simplify unnecessary loops away.
367   const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
368   N = SE->getSCEVAtScope(N, ICmpLoop);
369 
370   bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
371 
372   // Do not proceed if the Numerator may be negative
373   if (!IsNumeratorNonNegative)
374     return;
375 
376   const SCEV *D = SE->getSCEV(DValue);
377   D = SE->getSCEVAtScope(D, ICmpLoop);
378 
379   if (UsedAsNumerator) {
380     auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
381     if (SE->isKnownPredicate(LT, N, D)) {
382       replaceRemWithNumerator(Rem);
383       return;
384     }
385 
386     auto *T = Rem->getType();
387     const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
388     if (SE->isKnownPredicate(LT, NLessOne, D)) {
389       replaceRemWithNumeratorOrZero(Rem);
390       return;
391     }
392   }
393 
394   // Try to replace SRem with URem, if both N and D are known non-negative.
395   // Since we had already check N, we only need to check D now
396   if (!IsSigned || !SE->isKnownNonNegative(D))
397     return;
398 
399   replaceSRemWithURem(Rem);
400 }
401 
402 bool SimplifyIndvar::eliminateOverflowIntrinsic(WithOverflowInst *WO) {
403   const SCEV *LHS = SE->getSCEV(WO->getLHS());
404   const SCEV *RHS = SE->getSCEV(WO->getRHS());
405   if (!SE->willNotOverflow(WO->getBinaryOp(), WO->isSigned(), LHS, RHS))
406     return false;
407 
408   // Proved no overflow, nuke the overflow check and, if possible, the overflow
409   // intrinsic as well.
410 
411   BinaryOperator *NewResult = BinaryOperator::Create(
412       WO->getBinaryOp(), WO->getLHS(), WO->getRHS(), "", WO);
413 
414   if (WO->isSigned())
415     NewResult->setHasNoSignedWrap(true);
416   else
417     NewResult->setHasNoUnsignedWrap(true);
418 
419   SmallVector<ExtractValueInst *, 4> ToDelete;
420 
421   for (auto *U : WO->users()) {
422     if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
423       if (EVI->getIndices()[0] == 1)
424         EVI->replaceAllUsesWith(ConstantInt::getFalse(WO->getContext()));
425       else {
426         assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
427         EVI->replaceAllUsesWith(NewResult);
428       }
429       ToDelete.push_back(EVI);
430     }
431   }
432 
433   for (auto *EVI : ToDelete)
434     EVI->eraseFromParent();
435 
436   if (WO->use_empty())
437     WO->eraseFromParent();
438 
439   Changed = true;
440   return true;
441 }
442 
443 bool SimplifyIndvar::eliminateSaturatingIntrinsic(SaturatingInst *SI) {
444   const SCEV *LHS = SE->getSCEV(SI->getLHS());
445   const SCEV *RHS = SE->getSCEV(SI->getRHS());
446   if (!SE->willNotOverflow(SI->getBinaryOp(), SI->isSigned(), LHS, RHS))
447     return false;
448 
449   BinaryOperator *BO = BinaryOperator::Create(
450       SI->getBinaryOp(), SI->getLHS(), SI->getRHS(), SI->getName(), SI);
451   if (SI->isSigned())
452     BO->setHasNoSignedWrap();
453   else
454     BO->setHasNoUnsignedWrap();
455 
456   SI->replaceAllUsesWith(BO);
457   DeadInsts.emplace_back(SI);
458   Changed = true;
459   return true;
460 }
461 
462 bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) {
463   // It is always legal to replace
464   //   icmp <pred> i32 trunc(iv), n
465   // with
466   //   icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate.
467   // Or with
468   //   icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate.
469   // Or with either of these if pred is an equality predicate.
470   //
471   // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for
472   // every comparison which uses trunc, it means that we can replace each of
473   // them with comparison of iv against sext/zext(n). We no longer need trunc
474   // after that.
475   //
476   // TODO: Should we do this if we can widen *some* comparisons, but not all
477   // of them? Sometimes it is enough to enable other optimizations, but the
478   // trunc instruction will stay in the loop.
479   Value *IV = TI->getOperand(0);
480   Type *IVTy = IV->getType();
481   const SCEV *IVSCEV = SE->getSCEV(IV);
482   const SCEV *TISCEV = SE->getSCEV(TI);
483 
484   // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can
485   // get rid of trunc
486   bool DoesSExtCollapse = false;
487   bool DoesZExtCollapse = false;
488   if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy))
489     DoesSExtCollapse = true;
490   if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy))
491     DoesZExtCollapse = true;
492 
493   // If neither sext nor zext does collapse, it is not profitable to do any
494   // transform. Bail.
495   if (!DoesSExtCollapse && !DoesZExtCollapse)
496     return false;
497 
498   // Collect users of the trunc that look like comparisons against invariants.
499   // Bail if we find something different.
500   SmallVector<ICmpInst *, 4> ICmpUsers;
501   for (auto *U : TI->users()) {
502     // We don't care about users in unreachable blocks.
503     if (isa<Instruction>(U) &&
504         !DT->isReachableFromEntry(cast<Instruction>(U)->getParent()))
505       continue;
506     ICmpInst *ICI = dyn_cast<ICmpInst>(U);
507     if (!ICI) return false;
508     assert(L->contains(ICI->getParent()) && "LCSSA form broken?");
509     if (!(ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) &&
510         !(ICI->getOperand(1) == TI && L->isLoopInvariant(ICI->getOperand(0))))
511       return false;
512     // If we cannot get rid of trunc, bail.
513     if (ICI->isSigned() && !DoesSExtCollapse)
514       return false;
515     if (ICI->isUnsigned() && !DoesZExtCollapse)
516       return false;
517     // For equality, either signed or unsigned works.
518     ICmpUsers.push_back(ICI);
519   }
520 
521   auto CanUseZExt = [&](ICmpInst *ICI) {
522     // Unsigned comparison can be widened as unsigned.
523     if (ICI->isUnsigned())
524       return true;
525     // Is it profitable to do zext?
526     if (!DoesZExtCollapse)
527       return false;
528     // For equality, we can safely zext both parts.
529     if (ICI->isEquality())
530       return true;
531     // Otherwise we can only use zext when comparing two non-negative or two
532     // negative values. But in practice, we will never pass DoesZExtCollapse
533     // check for a negative value, because zext(trunc(x)) is non-negative. So
534     // it only make sense to check for non-negativity here.
535     const SCEV *SCEVOP1 = SE->getSCEV(ICI->getOperand(0));
536     const SCEV *SCEVOP2 = SE->getSCEV(ICI->getOperand(1));
537     return SE->isKnownNonNegative(SCEVOP1) && SE->isKnownNonNegative(SCEVOP2);
538   };
539   // Replace all comparisons against trunc with comparisons against IV.
540   for (auto *ICI : ICmpUsers) {
541     bool IsSwapped = L->isLoopInvariant(ICI->getOperand(0));
542     auto *Op1 = IsSwapped ? ICI->getOperand(0) : ICI->getOperand(1);
543     IRBuilder<> Builder(ICI);
544     Value *Ext = nullptr;
545     // For signed/unsigned predicate, replace the old comparison with comparison
546     // of immediate IV against sext/zext of the invariant argument. If we can
547     // use either sext or zext (i.e. we are dealing with equality predicate),
548     // then prefer zext as a more canonical form.
549     // TODO: If we see a signed comparison which can be turned into unsigned,
550     // we can do it here for canonicalization purposes.
551     ICmpInst::Predicate Pred = ICI->getPredicate();
552     if (IsSwapped) Pred = ICmpInst::getSwappedPredicate(Pred);
553     if (CanUseZExt(ICI)) {
554       assert(DoesZExtCollapse && "Unprofitable zext?");
555       Ext = Builder.CreateZExt(Op1, IVTy, "zext");
556       Pred = ICmpInst::getUnsignedPredicate(Pred);
557     } else {
558       assert(DoesSExtCollapse && "Unprofitable sext?");
559       Ext = Builder.CreateSExt(Op1, IVTy, "sext");
560       assert(Pred == ICmpInst::getSignedPredicate(Pred) && "Must be signed!");
561     }
562     bool Changed;
563     L->makeLoopInvariant(Ext, Changed);
564     (void)Changed;
565     auto *NewCmp = Builder.CreateICmp(Pred, IV, Ext);
566     ICI->replaceAllUsesWith(NewCmp);
567     DeadInsts.emplace_back(ICI);
568   }
569 
570   // Trunc no longer needed.
571   TI->replaceAllUsesWith(PoisonValue::get(TI->getType()));
572   DeadInsts.emplace_back(TI);
573   return true;
574 }
575 
576 /// Eliminate an operation that consumes a simple IV and has no observable
577 /// side-effect given the range of IV values.  IVOperand is guaranteed SCEVable,
578 /// but UseInst may not be.
579 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
580                                      Instruction *IVOperand) {
581   if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
582     eliminateIVComparison(ICmp, IVOperand);
583     return true;
584   }
585   if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
586     bool IsSRem = Bin->getOpcode() == Instruction::SRem;
587     if (IsSRem || Bin->getOpcode() == Instruction::URem) {
588       simplifyIVRemainder(Bin, IVOperand, IsSRem);
589       return true;
590     }
591 
592     if (Bin->getOpcode() == Instruction::SDiv)
593       return eliminateSDiv(Bin);
594   }
595 
596   if (auto *WO = dyn_cast<WithOverflowInst>(UseInst))
597     if (eliminateOverflowIntrinsic(WO))
598       return true;
599 
600   if (auto *SI = dyn_cast<SaturatingInst>(UseInst))
601     if (eliminateSaturatingIntrinsic(SI))
602       return true;
603 
604   if (auto *TI = dyn_cast<TruncInst>(UseInst))
605     if (eliminateTrunc(TI))
606       return true;
607 
608   if (eliminateIdentitySCEV(UseInst, IVOperand))
609     return true;
610 
611   return false;
612 }
613 
614 static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) {
615   if (auto *BB = L->getLoopPreheader())
616     return BB->getTerminator();
617 
618   return Hint;
619 }
620 
621 /// Replace the UseInst with a loop invariant expression if it is safe.
622 bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) {
623   if (!SE->isSCEVable(I->getType()))
624     return false;
625 
626   // Get the symbolic expression for this instruction.
627   const SCEV *S = SE->getSCEV(I);
628 
629   if (!SE->isLoopInvariant(S, L))
630     return false;
631 
632   // Do not generate something ridiculous even if S is loop invariant.
633   if (Rewriter.isHighCostExpansion(S, L, SCEVCheapExpansionBudget, TTI, I))
634     return false;
635 
636   auto *IP = GetLoopInvariantInsertPosition(L, I);
637 
638   if (!Rewriter.isSafeToExpandAt(S, IP)) {
639     LLVM_DEBUG(dbgs() << "INDVARS: Can not replace IV user: " << *I
640                       << " with non-speculable loop invariant: " << *S << '\n');
641     return false;
642   }
643 
644   auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
645 
646   I->replaceAllUsesWith(Invariant);
647   LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
648                     << " with loop invariant: " << *S << '\n');
649   ++NumFoldedUser;
650   Changed = true;
651   DeadInsts.emplace_back(I);
652   return true;
653 }
654 
655 /// Eliminate redundant type cast between integer and float.
656 bool SimplifyIndvar::replaceFloatIVWithIntegerIV(Instruction *UseInst) {
657   if (UseInst->getOpcode() != CastInst::SIToFP &&
658       UseInst->getOpcode() != CastInst::UIToFP)
659     return false;
660 
661   Instruction *IVOperand = cast<Instruction>(UseInst->getOperand(0));
662   // Get the symbolic expression for this instruction.
663   const SCEV *IV = SE->getSCEV(IVOperand);
664   int MaskBits;
665   if (UseInst->getOpcode() == CastInst::SIToFP)
666     MaskBits = (int)SE->getSignedRange(IV).getMinSignedBits();
667   else
668     MaskBits = (int)SE->getUnsignedRange(IV).getActiveBits();
669   int DestNumSigBits = UseInst->getType()->getFPMantissaWidth();
670   if (MaskBits <= DestNumSigBits) {
671     for (User *U : UseInst->users()) {
672       // Match for fptosi/fptoui of sitofp and with same type.
673       auto *CI = dyn_cast<CastInst>(U);
674       if (!CI)
675         continue;
676 
677       CastInst::CastOps Opcode = CI->getOpcode();
678       if (Opcode != CastInst::FPToSI && Opcode != CastInst::FPToUI)
679         continue;
680 
681       Value *Conv = nullptr;
682       if (IVOperand->getType() != CI->getType()) {
683         IRBuilder<> Builder(CI);
684         StringRef Name = IVOperand->getName();
685         // To match InstCombine logic, we only need sext if both fptosi and
686         // sitofp are used. If one of them is unsigned, then we can use zext.
687         if (SE->getTypeSizeInBits(IVOperand->getType()) >
688             SE->getTypeSizeInBits(CI->getType())) {
689           Conv = Builder.CreateTrunc(IVOperand, CI->getType(), Name + ".trunc");
690         } else if (Opcode == CastInst::FPToUI ||
691                    UseInst->getOpcode() == CastInst::UIToFP) {
692           Conv = Builder.CreateZExt(IVOperand, CI->getType(), Name + ".zext");
693         } else {
694           Conv = Builder.CreateSExt(IVOperand, CI->getType(), Name + ".sext");
695         }
696       } else
697         Conv = IVOperand;
698 
699       CI->replaceAllUsesWith(Conv);
700       DeadInsts.push_back(CI);
701       LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *CI
702                         << " with: " << *Conv << '\n');
703 
704       ++NumFoldedUser;
705       Changed = true;
706     }
707   }
708 
709   return Changed;
710 }
711 
712 /// Eliminate any operation that SCEV can prove is an identity function.
713 bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
714                                            Instruction *IVOperand) {
715   if (!SE->isSCEVable(UseInst->getType()) ||
716       (UseInst->getType() != IVOperand->getType()) ||
717       (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
718     return false;
719 
720   // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
721   // dominator tree, even if X is an operand to Y.  For instance, in
722   //
723   //     %iv = phi i32 {0,+,1}
724   //     br %cond, label %left, label %merge
725   //
726   //   left:
727   //     %X = add i32 %iv, 0
728   //     br label %merge
729   //
730   //   merge:
731   //     %M = phi (%X, %iv)
732   //
733   // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
734   // %M.replaceAllUsesWith(%X) would be incorrect.
735 
736   if (isa<PHINode>(UseInst))
737     // If UseInst is not a PHI node then we know that IVOperand dominates
738     // UseInst directly from the legality of SSA.
739     if (!DT || !DT->dominates(IVOperand, UseInst))
740       return false;
741 
742   if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
743     return false;
744 
745   LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
746 
747   SE->forgetValue(UseInst);
748   UseInst->replaceAllUsesWith(IVOperand);
749   ++NumElimIdentity;
750   Changed = true;
751   DeadInsts.emplace_back(UseInst);
752   return true;
753 }
754 
755 bool SimplifyIndvar::strengthenBinaryOp(BinaryOperator *BO,
756                                         Instruction *IVOperand) {
757   return (isa<OverflowingBinaryOperator>(BO) &&
758           strengthenOverflowingOperation(BO, IVOperand)) ||
759          (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand));
760 }
761 
762 /// Annotate BO with nsw / nuw if it provably does not signed-overflow /
763 /// unsigned-overflow.  Returns true if anything changed, false otherwise.
764 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
765                                                     Instruction *IVOperand) {
766   auto Flags = SE->getStrengthenedNoWrapFlagsFromBinOp(
767       cast<OverflowingBinaryOperator>(BO));
768 
769   if (!Flags)
770     return false;
771 
772   BO->setHasNoUnsignedWrap(ScalarEvolution::maskFlags(*Flags, SCEV::FlagNUW) ==
773                            SCEV::FlagNUW);
774   BO->setHasNoSignedWrap(ScalarEvolution::maskFlags(*Flags, SCEV::FlagNSW) ==
775                          SCEV::FlagNSW);
776 
777   // The getStrengthenedNoWrapFlagsFromBinOp() check inferred additional nowrap
778   // flags on addrecs while performing zero/sign extensions. We could call
779   // forgetValue() here to make sure those flags also propagate to any other
780   // SCEV expressions based on the addrec. However, this can have pathological
781   // compile-time impact, see https://bugs.llvm.org/show_bug.cgi?id=50384.
782   return true;
783 }
784 
785 /// Annotate the Shr in (X << IVOperand) >> C as exact using the
786 /// information from the IV's range. Returns true if anything changed, false
787 /// otherwise.
788 bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
789                                           Instruction *IVOperand) {
790   if (BO->getOpcode() == Instruction::Shl) {
791     bool Changed = false;
792     ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
793     for (auto *U : BO->users()) {
794       const APInt *C;
795       if (match(U,
796                 m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
797           match(U,
798                 m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
799         BinaryOperator *Shr = cast<BinaryOperator>(U);
800         if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
801           Shr->setIsExact(true);
802           Changed = true;
803         }
804       }
805     }
806     return Changed;
807   }
808 
809   return false;
810 }
811 
812 /// Add all uses of Def to the current IV's worklist.
813 static void pushIVUsers(
814   Instruction *Def, Loop *L,
815   SmallPtrSet<Instruction*,16> &Simplified,
816   SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
817 
818   for (User *U : Def->users()) {
819     Instruction *UI = cast<Instruction>(U);
820 
821     // Avoid infinite or exponential worklist processing.
822     // Also ensure unique worklist users.
823     // If Def is a LoopPhi, it may not be in the Simplified set, so check for
824     // self edges first.
825     if (UI == Def)
826       continue;
827 
828     // Only change the current Loop, do not change the other parts (e.g. other
829     // Loops).
830     if (!L->contains(UI))
831       continue;
832 
833     // Do not push the same instruction more than once.
834     if (!Simplified.insert(UI).second)
835       continue;
836 
837     SimpleIVUsers.push_back(std::make_pair(UI, Def));
838   }
839 }
840 
841 /// Return true if this instruction generates a simple SCEV
842 /// expression in terms of that IV.
843 ///
844 /// This is similar to IVUsers' isInteresting() but processes each instruction
845 /// non-recursively when the operand is already known to be a simpleIVUser.
846 ///
847 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
848   if (!SE->isSCEVable(I->getType()))
849     return false;
850 
851   // Get the symbolic expression for this instruction.
852   const SCEV *S = SE->getSCEV(I);
853 
854   // Only consider affine recurrences.
855   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
856   if (AR && AR->getLoop() == L)
857     return true;
858 
859   return false;
860 }
861 
862 /// Iteratively perform simplification on a worklist of users
863 /// of the specified induction variable. Each successive simplification may push
864 /// more users which may themselves be candidates for simplification.
865 ///
866 /// This algorithm does not require IVUsers analysis. Instead, it simplifies
867 /// instructions in-place during analysis. Rather than rewriting induction
868 /// variables bottom-up from their users, it transforms a chain of IVUsers
869 /// top-down, updating the IR only when it encounters a clear optimization
870 /// opportunity.
871 ///
872 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
873 ///
874 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
875   if (!SE->isSCEVable(CurrIV->getType()))
876     return;
877 
878   // Instructions processed by SimplifyIndvar for CurrIV.
879   SmallPtrSet<Instruction*,16> Simplified;
880 
881   // Use-def pairs if IV users waiting to be processed for CurrIV.
882   SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
883 
884   // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
885   // called multiple times for the same LoopPhi. This is the proper thing to
886   // do for loop header phis that use each other.
887   pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers);
888 
889   while (!SimpleIVUsers.empty()) {
890     std::pair<Instruction*, Instruction*> UseOper =
891       SimpleIVUsers.pop_back_val();
892     Instruction *UseInst = UseOper.first;
893 
894     // If a user of the IndVar is trivially dead, we prefer just to mark it dead
895     // rather than try to do some complex analysis or transformation (such as
896     // widening) basing on it.
897     // TODO: Propagate TLI and pass it here to handle more cases.
898     if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) {
899       DeadInsts.emplace_back(UseInst);
900       continue;
901     }
902 
903     // Bypass back edges to avoid extra work.
904     if (UseInst == CurrIV) continue;
905 
906     // Try to replace UseInst with a loop invariant before any other
907     // simplifications.
908     if (replaceIVUserWithLoopInvariant(UseInst))
909       continue;
910 
911     // Go further for the bitcast 'prtoint ptr to i64' or if the cast is done
912     // by truncation
913     if ((isa<PtrToIntInst>(UseInst)) || (isa<TruncInst>(UseInst)))
914       for (Use &U : UseInst->uses()) {
915         Instruction *User = cast<Instruction>(U.getUser());
916         if (replaceIVUserWithLoopInvariant(User))
917           break; // done replacing
918       }
919 
920     Instruction *IVOperand = UseOper.second;
921     for (unsigned N = 0; IVOperand; ++N) {
922       assert(N <= Simplified.size() && "runaway iteration");
923       (void) N;
924 
925       Value *NewOper = foldIVUser(UseInst, IVOperand);
926       if (!NewOper)
927         break; // done folding
928       IVOperand = dyn_cast<Instruction>(NewOper);
929     }
930     if (!IVOperand)
931       continue;
932 
933     if (eliminateIVUser(UseInst, IVOperand)) {
934       pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
935       continue;
936     }
937 
938     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) {
939       if (strengthenBinaryOp(BO, IVOperand)) {
940         // re-queue uses of the now modified binary operator and fall
941         // through to the checks that remain.
942         pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
943       }
944     }
945 
946     // Try to use integer induction for FPToSI of float induction directly.
947     if (replaceFloatIVWithIntegerIV(UseInst)) {
948       // Re-queue the potentially new direct uses of IVOperand.
949       pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
950       continue;
951     }
952 
953     CastInst *Cast = dyn_cast<CastInst>(UseInst);
954     if (V && Cast) {
955       V->visitCast(Cast);
956       continue;
957     }
958     if (isSimpleIVUser(UseInst, L, SE)) {
959       pushIVUsers(UseInst, L, Simplified, SimpleIVUsers);
960     }
961   }
962 }
963 
964 namespace llvm {
965 
966 void IVVisitor::anchor() { }
967 
968 /// Simplify instructions that use this induction variable
969 /// by using ScalarEvolution to analyze the IV's recurrence.
970 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
971                        LoopInfo *LI, const TargetTransformInfo *TTI,
972                        SmallVectorImpl<WeakTrackingVH> &Dead,
973                        SCEVExpander &Rewriter, IVVisitor *V) {
974   SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, TTI,
975                      Rewriter, Dead);
976   SIV.simplifyUsers(CurrIV, V);
977   return SIV.hasChanged();
978 }
979 
980 /// Simplify users of induction variables within this
981 /// loop. This does not actually change or add IVs.
982 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
983                      LoopInfo *LI, const TargetTransformInfo *TTI,
984                      SmallVectorImpl<WeakTrackingVH> &Dead) {
985   SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars");
986 #ifndef NDEBUG
987   Rewriter.setDebugType(DEBUG_TYPE);
988 #endif
989   bool Changed = false;
990   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
991     Changed |=
992         simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, TTI, Dead, Rewriter);
993   }
994   return Changed;
995 }
996 
997 } // namespace llvm
998 
999 namespace {
1000 //===----------------------------------------------------------------------===//
1001 // Widen Induction Variables - Extend the width of an IV to cover its
1002 // widest uses.
1003 //===----------------------------------------------------------------------===//
1004 
1005 class WidenIV {
1006   // Parameters
1007   PHINode *OrigPhi;
1008   Type *WideType;
1009 
1010   // Context
1011   LoopInfo        *LI;
1012   Loop            *L;
1013   ScalarEvolution *SE;
1014   DominatorTree   *DT;
1015 
1016   // Does the module have any calls to the llvm.experimental.guard intrinsic
1017   // at all? If not we can avoid scanning instructions looking for guards.
1018   bool HasGuards;
1019 
1020   bool UsePostIncrementRanges;
1021 
1022   // Statistics
1023   unsigned NumElimExt = 0;
1024   unsigned NumWidened = 0;
1025 
1026   // Result
1027   PHINode *WidePhi = nullptr;
1028   Instruction *WideInc = nullptr;
1029   const SCEV *WideIncExpr = nullptr;
1030   SmallVectorImpl<WeakTrackingVH> &DeadInsts;
1031 
1032   SmallPtrSet<Instruction *,16> Widened;
1033 
1034   enum class ExtendKind { Zero, Sign, Unknown };
1035 
1036   // A map tracking the kind of extension used to widen each narrow IV
1037   // and narrow IV user.
1038   // Key: pointer to a narrow IV or IV user.
1039   // Value: the kind of extension used to widen this Instruction.
1040   DenseMap<AssertingVH<Instruction>, ExtendKind> ExtendKindMap;
1041 
1042   using DefUserPair = std::pair<AssertingVH<Value>, AssertingVH<Instruction>>;
1043 
1044   // A map with control-dependent ranges for post increment IV uses. The key is
1045   // a pair of IV def and a use of this def denoting the context. The value is
1046   // a ConstantRange representing possible values of the def at the given
1047   // context.
1048   DenseMap<DefUserPair, ConstantRange> PostIncRangeInfos;
1049 
1050   std::optional<ConstantRange> getPostIncRangeInfo(Value *Def,
1051                                                    Instruction *UseI) {
1052     DefUserPair Key(Def, UseI);
1053     auto It = PostIncRangeInfos.find(Key);
1054     return It == PostIncRangeInfos.end()
1055                ? std::optional<ConstantRange>(std::nullopt)
1056                : std::optional<ConstantRange>(It->second);
1057   }
1058 
1059   void calculatePostIncRanges(PHINode *OrigPhi);
1060   void calculatePostIncRange(Instruction *NarrowDef, Instruction *NarrowUser);
1061 
1062   void updatePostIncRangeInfo(Value *Def, Instruction *UseI, ConstantRange R) {
1063     DefUserPair Key(Def, UseI);
1064     auto It = PostIncRangeInfos.find(Key);
1065     if (It == PostIncRangeInfos.end())
1066       PostIncRangeInfos.insert({Key, R});
1067     else
1068       It->second = R.intersectWith(It->second);
1069   }
1070 
1071 public:
1072   /// Record a link in the Narrow IV def-use chain along with the WideIV that
1073   /// computes the same value as the Narrow IV def.  This avoids caching Use*
1074   /// pointers.
1075   struct NarrowIVDefUse {
1076     Instruction *NarrowDef = nullptr;
1077     Instruction *NarrowUse = nullptr;
1078     Instruction *WideDef = nullptr;
1079 
1080     // True if the narrow def is never negative.  Tracking this information lets
1081     // us use a sign extension instead of a zero extension or vice versa, when
1082     // profitable and legal.
1083     bool NeverNegative = false;
1084 
1085     NarrowIVDefUse(Instruction *ND, Instruction *NU, Instruction *WD,
1086                    bool NeverNegative)
1087         : NarrowDef(ND), NarrowUse(NU), WideDef(WD),
1088           NeverNegative(NeverNegative) {}
1089   };
1090 
1091   WidenIV(const WideIVInfo &WI, LoopInfo *LInfo, ScalarEvolution *SEv,
1092           DominatorTree *DTree, SmallVectorImpl<WeakTrackingVH> &DI,
1093           bool HasGuards, bool UsePostIncrementRanges = true);
1094 
1095   PHINode *createWideIV(SCEVExpander &Rewriter);
1096 
1097   unsigned getNumElimExt() { return NumElimExt; };
1098   unsigned getNumWidened() { return NumWidened; };
1099 
1100 protected:
1101   Value *createExtendInst(Value *NarrowOper, Type *WideType, bool IsSigned,
1102                           Instruction *Use);
1103 
1104   Instruction *cloneIVUser(NarrowIVDefUse DU, const SCEVAddRecExpr *WideAR);
1105   Instruction *cloneArithmeticIVUser(NarrowIVDefUse DU,
1106                                      const SCEVAddRecExpr *WideAR);
1107   Instruction *cloneBitwiseIVUser(NarrowIVDefUse DU);
1108 
1109   ExtendKind getExtendKind(Instruction *I);
1110 
1111   using WidenedRecTy = std::pair<const SCEVAddRecExpr *, ExtendKind>;
1112 
1113   WidenedRecTy getWideRecurrence(NarrowIVDefUse DU);
1114 
1115   WidenedRecTy getExtendedOperandRecurrence(NarrowIVDefUse DU);
1116 
1117   const SCEV *getSCEVByOpCode(const SCEV *LHS, const SCEV *RHS,
1118                               unsigned OpCode) const;
1119 
1120   Instruction *widenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter);
1121 
1122   bool widenLoopCompare(NarrowIVDefUse DU);
1123   bool widenWithVariantUse(NarrowIVDefUse DU);
1124 
1125   void pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef);
1126 
1127 private:
1128   SmallVector<NarrowIVDefUse, 8> NarrowIVUsers;
1129 };
1130 } // namespace
1131 
1132 /// Determine the insertion point for this user. By default, insert immediately
1133 /// before the user. SCEVExpander or LICM will hoist loop invariants out of the
1134 /// loop. For PHI nodes, there may be multiple uses, so compute the nearest
1135 /// common dominator for the incoming blocks. A nullptr can be returned if no
1136 /// viable location is found: it may happen if User is a PHI and Def only comes
1137 /// to this PHI from unreachable blocks.
1138 static Instruction *getInsertPointForUses(Instruction *User, Value *Def,
1139                                           DominatorTree *DT, LoopInfo *LI) {
1140   PHINode *PHI = dyn_cast<PHINode>(User);
1141   if (!PHI)
1142     return User;
1143 
1144   Instruction *InsertPt = nullptr;
1145   for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
1146     if (PHI->getIncomingValue(i) != Def)
1147       continue;
1148 
1149     BasicBlock *InsertBB = PHI->getIncomingBlock(i);
1150 
1151     if (!DT->isReachableFromEntry(InsertBB))
1152       continue;
1153 
1154     if (!InsertPt) {
1155       InsertPt = InsertBB->getTerminator();
1156       continue;
1157     }
1158     InsertBB = DT->findNearestCommonDominator(InsertPt->getParent(), InsertBB);
1159     InsertPt = InsertBB->getTerminator();
1160   }
1161 
1162   // If we have skipped all inputs, it means that Def only comes to Phi from
1163   // unreachable blocks.
1164   if (!InsertPt)
1165     return nullptr;
1166 
1167   auto *DefI = dyn_cast<Instruction>(Def);
1168   if (!DefI)
1169     return InsertPt;
1170 
1171   assert(DT->dominates(DefI, InsertPt) && "def does not dominate all uses");
1172 
1173   auto *L = LI->getLoopFor(DefI->getParent());
1174   assert(!L || L->contains(LI->getLoopFor(InsertPt->getParent())));
1175 
1176   for (auto *DTN = (*DT)[InsertPt->getParent()]; DTN; DTN = DTN->getIDom())
1177     if (LI->getLoopFor(DTN->getBlock()) == L)
1178       return DTN->getBlock()->getTerminator();
1179 
1180   llvm_unreachable("DefI dominates InsertPt!");
1181 }
1182 
1183 WidenIV::WidenIV(const WideIVInfo &WI, LoopInfo *LInfo, ScalarEvolution *SEv,
1184           DominatorTree *DTree, SmallVectorImpl<WeakTrackingVH> &DI,
1185           bool HasGuards, bool UsePostIncrementRanges)
1186       : OrigPhi(WI.NarrowIV), WideType(WI.WidestNativeType), LI(LInfo),
1187         L(LI->getLoopFor(OrigPhi->getParent())), SE(SEv), DT(DTree),
1188         HasGuards(HasGuards), UsePostIncrementRanges(UsePostIncrementRanges),
1189         DeadInsts(DI) {
1190     assert(L->getHeader() == OrigPhi->getParent() && "Phi must be an IV");
1191     ExtendKindMap[OrigPhi] = WI.IsSigned ? ExtendKind::Sign : ExtendKind::Zero;
1192 }
1193 
1194 Value *WidenIV::createExtendInst(Value *NarrowOper, Type *WideType,
1195                                  bool IsSigned, Instruction *Use) {
1196   // Set the debug location and conservative insertion point.
1197   IRBuilder<> Builder(Use);
1198   // Hoist the insertion point into loop preheaders as far as possible.
1199   for (const Loop *L = LI->getLoopFor(Use->getParent());
1200        L && L->getLoopPreheader() && L->isLoopInvariant(NarrowOper);
1201        L = L->getParentLoop())
1202     Builder.SetInsertPoint(L->getLoopPreheader()->getTerminator());
1203 
1204   return IsSigned ? Builder.CreateSExt(NarrowOper, WideType) :
1205                     Builder.CreateZExt(NarrowOper, WideType);
1206 }
1207 
1208 /// Instantiate a wide operation to replace a narrow operation. This only needs
1209 /// to handle operations that can evaluation to SCEVAddRec. It can safely return
1210 /// 0 for any operation we decide not to clone.
1211 Instruction *WidenIV::cloneIVUser(WidenIV::NarrowIVDefUse DU,
1212                                   const SCEVAddRecExpr *WideAR) {
1213   unsigned Opcode = DU.NarrowUse->getOpcode();
1214   switch (Opcode) {
1215   default:
1216     return nullptr;
1217   case Instruction::Add:
1218   case Instruction::Mul:
1219   case Instruction::UDiv:
1220   case Instruction::Sub:
1221     return cloneArithmeticIVUser(DU, WideAR);
1222 
1223   case Instruction::And:
1224   case Instruction::Or:
1225   case Instruction::Xor:
1226   case Instruction::Shl:
1227   case Instruction::LShr:
1228   case Instruction::AShr:
1229     return cloneBitwiseIVUser(DU);
1230   }
1231 }
1232 
1233 Instruction *WidenIV::cloneBitwiseIVUser(WidenIV::NarrowIVDefUse DU) {
1234   Instruction *NarrowUse = DU.NarrowUse;
1235   Instruction *NarrowDef = DU.NarrowDef;
1236   Instruction *WideDef = DU.WideDef;
1237 
1238   LLVM_DEBUG(dbgs() << "Cloning bitwise IVUser: " << *NarrowUse << "\n");
1239 
1240   // Replace NarrowDef operands with WideDef. Otherwise, we don't know anything
1241   // about the narrow operand yet so must insert a [sz]ext. It is probably loop
1242   // invariant and will be folded or hoisted. If it actually comes from a
1243   // widened IV, it should be removed during a future call to widenIVUse.
1244   bool IsSigned = getExtendKind(NarrowDef) == ExtendKind::Sign;
1245   Value *LHS = (NarrowUse->getOperand(0) == NarrowDef)
1246                    ? WideDef
1247                    : createExtendInst(NarrowUse->getOperand(0), WideType,
1248                                       IsSigned, NarrowUse);
1249   Value *RHS = (NarrowUse->getOperand(1) == NarrowDef)
1250                    ? WideDef
1251                    : createExtendInst(NarrowUse->getOperand(1), WideType,
1252                                       IsSigned, NarrowUse);
1253 
1254   auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1255   auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1256                                         NarrowBO->getName());
1257   IRBuilder<> Builder(NarrowUse);
1258   Builder.Insert(WideBO);
1259   WideBO->copyIRFlags(NarrowBO);
1260   return WideBO;
1261 }
1262 
1263 Instruction *WidenIV::cloneArithmeticIVUser(WidenIV::NarrowIVDefUse DU,
1264                                             const SCEVAddRecExpr *WideAR) {
1265   Instruction *NarrowUse = DU.NarrowUse;
1266   Instruction *NarrowDef = DU.NarrowDef;
1267   Instruction *WideDef = DU.WideDef;
1268 
1269   LLVM_DEBUG(dbgs() << "Cloning arithmetic IVUser: " << *NarrowUse << "\n");
1270 
1271   unsigned IVOpIdx = (NarrowUse->getOperand(0) == NarrowDef) ? 0 : 1;
1272 
1273   // We're trying to find X such that
1274   //
1275   //  Widen(NarrowDef `op` NonIVNarrowDef) == WideAR == WideDef `op.wide` X
1276   //
1277   // We guess two solutions to X, sext(NonIVNarrowDef) and zext(NonIVNarrowDef),
1278   // and check using SCEV if any of them are correct.
1279 
1280   // Returns true if extending NonIVNarrowDef according to `SignExt` is a
1281   // correct solution to X.
1282   auto GuessNonIVOperand = [&](bool SignExt) {
1283     const SCEV *WideLHS;
1284     const SCEV *WideRHS;
1285 
1286     auto GetExtend = [this, SignExt](const SCEV *S, Type *Ty) {
1287       if (SignExt)
1288         return SE->getSignExtendExpr(S, Ty);
1289       return SE->getZeroExtendExpr(S, Ty);
1290     };
1291 
1292     if (IVOpIdx == 0) {
1293       WideLHS = SE->getSCEV(WideDef);
1294       const SCEV *NarrowRHS = SE->getSCEV(NarrowUse->getOperand(1));
1295       WideRHS = GetExtend(NarrowRHS, WideType);
1296     } else {
1297       const SCEV *NarrowLHS = SE->getSCEV(NarrowUse->getOperand(0));
1298       WideLHS = GetExtend(NarrowLHS, WideType);
1299       WideRHS = SE->getSCEV(WideDef);
1300     }
1301 
1302     // WideUse is "WideDef `op.wide` X" as described in the comment.
1303     const SCEV *WideUse =
1304       getSCEVByOpCode(WideLHS, WideRHS, NarrowUse->getOpcode());
1305 
1306     return WideUse == WideAR;
1307   };
1308 
1309   bool SignExtend = getExtendKind(NarrowDef) == ExtendKind::Sign;
1310   if (!GuessNonIVOperand(SignExtend)) {
1311     SignExtend = !SignExtend;
1312     if (!GuessNonIVOperand(SignExtend))
1313       return nullptr;
1314   }
1315 
1316   Value *LHS = (NarrowUse->getOperand(0) == NarrowDef)
1317                    ? WideDef
1318                    : createExtendInst(NarrowUse->getOperand(0), WideType,
1319                                       SignExtend, NarrowUse);
1320   Value *RHS = (NarrowUse->getOperand(1) == NarrowDef)
1321                    ? WideDef
1322                    : createExtendInst(NarrowUse->getOperand(1), WideType,
1323                                       SignExtend, NarrowUse);
1324 
1325   auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1326   auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1327                                         NarrowBO->getName());
1328 
1329   IRBuilder<> Builder(NarrowUse);
1330   Builder.Insert(WideBO);
1331   WideBO->copyIRFlags(NarrowBO);
1332   return WideBO;
1333 }
1334 
1335 WidenIV::ExtendKind WidenIV::getExtendKind(Instruction *I) {
1336   auto It = ExtendKindMap.find(I);
1337   assert(It != ExtendKindMap.end() && "Instruction not yet extended!");
1338   return It->second;
1339 }
1340 
1341 const SCEV *WidenIV::getSCEVByOpCode(const SCEV *LHS, const SCEV *RHS,
1342                                      unsigned OpCode) const {
1343   switch (OpCode) {
1344   case Instruction::Add:
1345     return SE->getAddExpr(LHS, RHS);
1346   case Instruction::Sub:
1347     return SE->getMinusSCEV(LHS, RHS);
1348   case Instruction::Mul:
1349     return SE->getMulExpr(LHS, RHS);
1350   case Instruction::UDiv:
1351     return SE->getUDivExpr(LHS, RHS);
1352   default:
1353     llvm_unreachable("Unsupported opcode.");
1354   };
1355 }
1356 
1357 /// No-wrap operations can transfer sign extension of their result to their
1358 /// operands. Generate the SCEV value for the widened operation without
1359 /// actually modifying the IR yet. If the expression after extending the
1360 /// operands is an AddRec for this loop, return the AddRec and the kind of
1361 /// extension used.
1362 WidenIV::WidenedRecTy
1363 WidenIV::getExtendedOperandRecurrence(WidenIV::NarrowIVDefUse DU) {
1364   // Handle the common case of add<nsw/nuw>
1365   const unsigned OpCode = DU.NarrowUse->getOpcode();
1366   // Only Add/Sub/Mul instructions supported yet.
1367   if (OpCode != Instruction::Add && OpCode != Instruction::Sub &&
1368       OpCode != Instruction::Mul)
1369     return {nullptr, ExtendKind::Unknown};
1370 
1371   // One operand (NarrowDef) has already been extended to WideDef. Now determine
1372   // if extending the other will lead to a recurrence.
1373   const unsigned ExtendOperIdx =
1374       DU.NarrowUse->getOperand(0) == DU.NarrowDef ? 1 : 0;
1375   assert(DU.NarrowUse->getOperand(1-ExtendOperIdx) == DU.NarrowDef && "bad DU");
1376 
1377   const OverflowingBinaryOperator *OBO =
1378     cast<OverflowingBinaryOperator>(DU.NarrowUse);
1379   ExtendKind ExtKind = getExtendKind(DU.NarrowDef);
1380   if (!(ExtKind == ExtendKind::Sign && OBO->hasNoSignedWrap()) &&
1381       !(ExtKind == ExtendKind::Zero && OBO->hasNoUnsignedWrap())) {
1382     ExtKind = ExtendKind::Unknown;
1383 
1384     // For a non-negative NarrowDef, we can choose either type of
1385     // extension.  We want to use the current extend kind if legal
1386     // (see above), and we only hit this code if we need to check
1387     // the opposite case.
1388     if (DU.NeverNegative) {
1389       if (OBO->hasNoSignedWrap()) {
1390         ExtKind = ExtendKind::Sign;
1391       } else if (OBO->hasNoUnsignedWrap()) {
1392         ExtKind = ExtendKind::Zero;
1393       }
1394     }
1395   }
1396 
1397   const SCEV *ExtendOperExpr =
1398       SE->getSCEV(DU.NarrowUse->getOperand(ExtendOperIdx));
1399   if (ExtKind == ExtendKind::Sign)
1400     ExtendOperExpr = SE->getSignExtendExpr(ExtendOperExpr, WideType);
1401   else if (ExtKind == ExtendKind::Zero)
1402     ExtendOperExpr = SE->getZeroExtendExpr(ExtendOperExpr, WideType);
1403   else
1404     return {nullptr, ExtendKind::Unknown};
1405 
1406   // When creating this SCEV expr, don't apply the current operations NSW or NUW
1407   // flags. This instruction may be guarded by control flow that the no-wrap
1408   // behavior depends on. Non-control-equivalent instructions can be mapped to
1409   // the same SCEV expression, and it would be incorrect to transfer NSW/NUW
1410   // semantics to those operations.
1411   const SCEV *lhs = SE->getSCEV(DU.WideDef);
1412   const SCEV *rhs = ExtendOperExpr;
1413 
1414   // Let's swap operands to the initial order for the case of non-commutative
1415   // operations, like SUB. See PR21014.
1416   if (ExtendOperIdx == 0)
1417     std::swap(lhs, rhs);
1418   const SCEVAddRecExpr *AddRec =
1419       dyn_cast<SCEVAddRecExpr>(getSCEVByOpCode(lhs, rhs, OpCode));
1420 
1421   if (!AddRec || AddRec->getLoop() != L)
1422     return {nullptr, ExtendKind::Unknown};
1423 
1424   return {AddRec, ExtKind};
1425 }
1426 
1427 /// Is this instruction potentially interesting for further simplification after
1428 /// widening it's type? In other words, can the extend be safely hoisted out of
1429 /// the loop with SCEV reducing the value to a recurrence on the same loop. If
1430 /// so, return the extended recurrence and the kind of extension used. Otherwise
1431 /// return {nullptr, ExtendKind::Unknown}.
1432 WidenIV::WidenedRecTy WidenIV::getWideRecurrence(WidenIV::NarrowIVDefUse DU) {
1433   if (!DU.NarrowUse->getType()->isIntegerTy())
1434     return {nullptr, ExtendKind::Unknown};
1435 
1436   const SCEV *NarrowExpr = SE->getSCEV(DU.NarrowUse);
1437   if (SE->getTypeSizeInBits(NarrowExpr->getType()) >=
1438       SE->getTypeSizeInBits(WideType)) {
1439     // NarrowUse implicitly widens its operand. e.g. a gep with a narrow
1440     // index. So don't follow this use.
1441     return {nullptr, ExtendKind::Unknown};
1442   }
1443 
1444   const SCEV *WideExpr;
1445   ExtendKind ExtKind;
1446   if (DU.NeverNegative) {
1447     WideExpr = SE->getSignExtendExpr(NarrowExpr, WideType);
1448     if (isa<SCEVAddRecExpr>(WideExpr))
1449       ExtKind = ExtendKind::Sign;
1450     else {
1451       WideExpr = SE->getZeroExtendExpr(NarrowExpr, WideType);
1452       ExtKind = ExtendKind::Zero;
1453     }
1454   } else if (getExtendKind(DU.NarrowDef) == ExtendKind::Sign) {
1455     WideExpr = SE->getSignExtendExpr(NarrowExpr, WideType);
1456     ExtKind = ExtendKind::Sign;
1457   } else {
1458     WideExpr = SE->getZeroExtendExpr(NarrowExpr, WideType);
1459     ExtKind = ExtendKind::Zero;
1460   }
1461   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(WideExpr);
1462   if (!AddRec || AddRec->getLoop() != L)
1463     return {nullptr, ExtendKind::Unknown};
1464   return {AddRec, ExtKind};
1465 }
1466 
1467 /// This IV user cannot be widened. Replace this use of the original narrow IV
1468 /// with a truncation of the new wide IV to isolate and eliminate the narrow IV.
1469 static void truncateIVUse(WidenIV::NarrowIVDefUse DU, DominatorTree *DT,
1470                           LoopInfo *LI) {
1471   auto *InsertPt = getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT, LI);
1472   if (!InsertPt)
1473     return;
1474   LLVM_DEBUG(dbgs() << "INDVARS: Truncate IV " << *DU.WideDef << " for user "
1475                     << *DU.NarrowUse << "\n");
1476   IRBuilder<> Builder(InsertPt);
1477   Value *Trunc = Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType());
1478   DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc);
1479 }
1480 
1481 /// If the narrow use is a compare instruction, then widen the compare
1482 //  (and possibly the other operand).  The extend operation is hoisted into the
1483 // loop preheader as far as possible.
1484 bool WidenIV::widenLoopCompare(WidenIV::NarrowIVDefUse DU) {
1485   ICmpInst *Cmp = dyn_cast<ICmpInst>(DU.NarrowUse);
1486   if (!Cmp)
1487     return false;
1488 
1489   // We can legally widen the comparison in the following two cases:
1490   //
1491   //  - The signedness of the IV extension and comparison match
1492   //
1493   //  - The narrow IV is always positive (and thus its sign extension is equal
1494   //    to its zero extension).  For instance, let's say we're zero extending
1495   //    %narrow for the following use
1496   //
1497   //      icmp slt i32 %narrow, %val   ... (A)
1498   //
1499   //    and %narrow is always positive.  Then
1500   //
1501   //      (A) == icmp slt i32 sext(%narrow), sext(%val)
1502   //          == icmp slt i32 zext(%narrow), sext(%val)
1503   bool IsSigned = getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
1504   if (!(DU.NeverNegative || IsSigned == Cmp->isSigned()))
1505     return false;
1506 
1507   Value *Op = Cmp->getOperand(Cmp->getOperand(0) == DU.NarrowDef ? 1 : 0);
1508   unsigned CastWidth = SE->getTypeSizeInBits(Op->getType());
1509   unsigned IVWidth = SE->getTypeSizeInBits(WideType);
1510   assert(CastWidth <= IVWidth && "Unexpected width while widening compare.");
1511 
1512   // Widen the compare instruction.
1513   DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
1514 
1515   // Widen the other operand of the compare, if necessary.
1516   if (CastWidth < IVWidth) {
1517     Value *ExtOp = createExtendInst(Op, WideType, Cmp->isSigned(), Cmp);
1518     DU.NarrowUse->replaceUsesOfWith(Op, ExtOp);
1519   }
1520   return true;
1521 }
1522 
1523 // The widenIVUse avoids generating trunc by evaluating the use as AddRec, this
1524 // will not work when:
1525 //    1) SCEV traces back to an instruction inside the loop that SCEV can not
1526 // expand, eg. add %indvar, (load %addr)
1527 //    2) SCEV finds a loop variant, eg. add %indvar, %loopvariant
1528 // While SCEV fails to avoid trunc, we can still try to use instruction
1529 // combining approach to prove trunc is not required. This can be further
1530 // extended with other instruction combining checks, but for now we handle the
1531 // following case (sub can be "add" and "mul", "nsw + sext" can be "nus + zext")
1532 //
1533 // Src:
1534 //   %c = sub nsw %b, %indvar
1535 //   %d = sext %c to i64
1536 // Dst:
1537 //   %indvar.ext1 = sext %indvar to i64
1538 //   %m = sext %b to i64
1539 //   %d = sub nsw i64 %m, %indvar.ext1
1540 // Therefore, as long as the result of add/sub/mul is extended to wide type, no
1541 // trunc is required regardless of how %b is generated. This pattern is common
1542 // when calculating address in 64 bit architecture
1543 bool WidenIV::widenWithVariantUse(WidenIV::NarrowIVDefUse DU) {
1544   Instruction *NarrowUse = DU.NarrowUse;
1545   Instruction *NarrowDef = DU.NarrowDef;
1546   Instruction *WideDef = DU.WideDef;
1547 
1548   // Handle the common case of add<nsw/nuw>
1549   const unsigned OpCode = NarrowUse->getOpcode();
1550   // Only Add/Sub/Mul instructions are supported.
1551   if (OpCode != Instruction::Add && OpCode != Instruction::Sub &&
1552       OpCode != Instruction::Mul)
1553     return false;
1554 
1555   // The operand that is not defined by NarrowDef of DU. Let's call it the
1556   // other operand.
1557   assert((NarrowUse->getOperand(0) == NarrowDef ||
1558           NarrowUse->getOperand(1) == NarrowDef) &&
1559          "bad DU");
1560 
1561   const OverflowingBinaryOperator *OBO =
1562     cast<OverflowingBinaryOperator>(NarrowUse);
1563   ExtendKind ExtKind = getExtendKind(NarrowDef);
1564   bool CanSignExtend = ExtKind == ExtendKind::Sign && OBO->hasNoSignedWrap();
1565   bool CanZeroExtend = ExtKind == ExtendKind::Zero && OBO->hasNoUnsignedWrap();
1566   auto AnotherOpExtKind = ExtKind;
1567 
1568   // Check that all uses are either:
1569   // - narrow def (in case of we are widening the IV increment);
1570   // - single-input LCSSA Phis;
1571   // - comparison of the chosen type;
1572   // - extend of the chosen type (raison d'etre).
1573   SmallVector<Instruction *, 4> ExtUsers;
1574   SmallVector<PHINode *, 4> LCSSAPhiUsers;
1575   SmallVector<ICmpInst *, 4> ICmpUsers;
1576   for (Use &U : NarrowUse->uses()) {
1577     Instruction *User = cast<Instruction>(U.getUser());
1578     if (User == NarrowDef)
1579       continue;
1580     if (!L->contains(User)) {
1581       auto *LCSSAPhi = cast<PHINode>(User);
1582       // Make sure there is only 1 input, so that we don't have to split
1583       // critical edges.
1584       if (LCSSAPhi->getNumOperands() != 1)
1585         return false;
1586       LCSSAPhiUsers.push_back(LCSSAPhi);
1587       continue;
1588     }
1589     if (auto *ICmp = dyn_cast<ICmpInst>(User)) {
1590       auto Pred = ICmp->getPredicate();
1591       // We have 3 types of predicates: signed, unsigned and equality
1592       // predicates. For equality, it's legal to widen icmp for either sign and
1593       // zero extend. For sign extend, we can also do so for signed predicates,
1594       // likeweise for zero extend we can widen icmp for unsigned predicates.
1595       if (ExtKind == ExtendKind::Zero && ICmpInst::isSigned(Pred))
1596         return false;
1597       if (ExtKind == ExtendKind::Sign && ICmpInst::isUnsigned(Pred))
1598         return false;
1599       ICmpUsers.push_back(ICmp);
1600       continue;
1601     }
1602     if (ExtKind == ExtendKind::Sign)
1603       User = dyn_cast<SExtInst>(User);
1604     else
1605       User = dyn_cast<ZExtInst>(User);
1606     if (!User || User->getType() != WideType)
1607       return false;
1608     ExtUsers.push_back(User);
1609   }
1610   if (ExtUsers.empty()) {
1611     DeadInsts.emplace_back(NarrowUse);
1612     return true;
1613   }
1614 
1615   // We'll prove some facts that should be true in the context of ext users. If
1616   // there is no users, we are done now. If there are some, pick their common
1617   // dominator as context.
1618   const Instruction *CtxI = findCommonDominator(ExtUsers, *DT);
1619 
1620   if (!CanSignExtend && !CanZeroExtend) {
1621     // Because InstCombine turns 'sub nuw' to 'add' losing the no-wrap flag, we
1622     // will most likely not see it. Let's try to prove it.
1623     if (OpCode != Instruction::Add)
1624       return false;
1625     if (ExtKind != ExtendKind::Zero)
1626       return false;
1627     const SCEV *LHS = SE->getSCEV(OBO->getOperand(0));
1628     const SCEV *RHS = SE->getSCEV(OBO->getOperand(1));
1629     // TODO: Support case for NarrowDef = NarrowUse->getOperand(1).
1630     if (NarrowUse->getOperand(0) != NarrowDef)
1631       return false;
1632     if (!SE->isKnownNegative(RHS))
1633       return false;
1634     bool ProvedSubNUW = SE->isKnownPredicateAt(ICmpInst::ICMP_UGE, LHS,
1635                                                SE->getNegativeSCEV(RHS), CtxI);
1636     if (!ProvedSubNUW)
1637       return false;
1638     // In fact, our 'add' is 'sub nuw'. We will need to widen the 2nd operand as
1639     // neg(zext(neg(op))), which is basically sext(op).
1640     AnotherOpExtKind = ExtendKind::Sign;
1641   }
1642 
1643   // Verifying that Defining operand is an AddRec
1644   const SCEV *Op1 = SE->getSCEV(WideDef);
1645   const SCEVAddRecExpr *AddRecOp1 = dyn_cast<SCEVAddRecExpr>(Op1);
1646   if (!AddRecOp1 || AddRecOp1->getLoop() != L)
1647     return false;
1648 
1649   LLVM_DEBUG(dbgs() << "Cloning arithmetic IVUser: " << *NarrowUse << "\n");
1650 
1651   // Generating a widening use instruction.
1652   Value *LHS =
1653       (NarrowUse->getOperand(0) == NarrowDef)
1654           ? WideDef
1655           : createExtendInst(NarrowUse->getOperand(0), WideType,
1656                              AnotherOpExtKind == ExtendKind::Sign, NarrowUse);
1657   Value *RHS =
1658       (NarrowUse->getOperand(1) == NarrowDef)
1659           ? WideDef
1660           : createExtendInst(NarrowUse->getOperand(1), WideType,
1661                              AnotherOpExtKind == ExtendKind::Sign, NarrowUse);
1662 
1663   auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1664   auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1665                                         NarrowBO->getName());
1666   IRBuilder<> Builder(NarrowUse);
1667   Builder.Insert(WideBO);
1668   WideBO->copyIRFlags(NarrowBO);
1669   ExtendKindMap[NarrowUse] = ExtKind;
1670 
1671   for (Instruction *User : ExtUsers) {
1672     assert(User->getType() == WideType && "Checked before!");
1673     LLVM_DEBUG(dbgs() << "INDVARS: eliminating " << *User << " replaced by "
1674                       << *WideBO << "\n");
1675     ++NumElimExt;
1676     User->replaceAllUsesWith(WideBO);
1677     DeadInsts.emplace_back(User);
1678   }
1679 
1680   for (PHINode *User : LCSSAPhiUsers) {
1681     assert(User->getNumOperands() == 1 && "Checked before!");
1682     Builder.SetInsertPoint(User);
1683     auto *WidePN =
1684         Builder.CreatePHI(WideBO->getType(), 1, User->getName() + ".wide");
1685     BasicBlock *LoopExitingBlock = User->getParent()->getSinglePredecessor();
1686     assert(LoopExitingBlock && L->contains(LoopExitingBlock) &&
1687            "Not a LCSSA Phi?");
1688     WidePN->addIncoming(WideBO, LoopExitingBlock);
1689     Builder.SetInsertPoint(User->getParent(),
1690                            User->getParent()->getFirstInsertionPt());
1691     auto *TruncPN = Builder.CreateTrunc(WidePN, User->getType());
1692     User->replaceAllUsesWith(TruncPN);
1693     DeadInsts.emplace_back(User);
1694   }
1695 
1696   for (ICmpInst *User : ICmpUsers) {
1697     Builder.SetInsertPoint(User);
1698     auto ExtendedOp = [&](Value * V)->Value * {
1699       if (V == NarrowUse)
1700         return WideBO;
1701       if (ExtKind == ExtendKind::Zero)
1702         return Builder.CreateZExt(V, WideBO->getType());
1703       else
1704         return Builder.CreateSExt(V, WideBO->getType());
1705     };
1706     auto Pred = User->getPredicate();
1707     auto *LHS = ExtendedOp(User->getOperand(0));
1708     auto *RHS = ExtendedOp(User->getOperand(1));
1709     auto *WideCmp =
1710         Builder.CreateICmp(Pred, LHS, RHS, User->getName() + ".wide");
1711     User->replaceAllUsesWith(WideCmp);
1712     DeadInsts.emplace_back(User);
1713   }
1714 
1715   return true;
1716 }
1717 
1718 /// Determine whether an individual user of the narrow IV can be widened. If so,
1719 /// return the wide clone of the user.
1720 Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU, SCEVExpander &Rewriter) {
1721   assert(ExtendKindMap.count(DU.NarrowDef) &&
1722          "Should already know the kind of extension used to widen NarrowDef");
1723 
1724   // Stop traversing the def-use chain at inner-loop phis or post-loop phis.
1725   if (PHINode *UsePhi = dyn_cast<PHINode>(DU.NarrowUse)) {
1726     if (LI->getLoopFor(UsePhi->getParent()) != L) {
1727       // For LCSSA phis, sink the truncate outside the loop.
1728       // After SimplifyCFG most loop exit targets have a single predecessor.
1729       // Otherwise fall back to a truncate within the loop.
1730       if (UsePhi->getNumOperands() != 1)
1731         truncateIVUse(DU, DT, LI);
1732       else {
1733         // Widening the PHI requires us to insert a trunc.  The logical place
1734         // for this trunc is in the same BB as the PHI.  This is not possible if
1735         // the BB is terminated by a catchswitch.
1736         if (isa<CatchSwitchInst>(UsePhi->getParent()->getTerminator()))
1737           return nullptr;
1738 
1739         PHINode *WidePhi =
1740           PHINode::Create(DU.WideDef->getType(), 1, UsePhi->getName() + ".wide",
1741                           UsePhi);
1742         WidePhi->addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0));
1743         BasicBlock *WidePhiBB = WidePhi->getParent();
1744         IRBuilder<> Builder(WidePhiBB, WidePhiBB->getFirstInsertionPt());
1745         Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType());
1746         UsePhi->replaceAllUsesWith(Trunc);
1747         DeadInsts.emplace_back(UsePhi);
1748         LLVM_DEBUG(dbgs() << "INDVARS: Widen lcssa phi " << *UsePhi << " to "
1749                           << *WidePhi << "\n");
1750       }
1751       return nullptr;
1752     }
1753   }
1754 
1755   // This narrow use can be widened by a sext if it's non-negative or its narrow
1756   // def was widended by a sext. Same for zext.
1757   auto canWidenBySExt = [&]() {
1758     return DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
1759   };
1760   auto canWidenByZExt = [&]() {
1761     return DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Zero;
1762   };
1763 
1764   // Our raison d'etre! Eliminate sign and zero extension.
1765   if ((match(DU.NarrowUse, m_SExtLike(m_Value())) && canWidenBySExt()) ||
1766       (isa<ZExtInst>(DU.NarrowUse) && canWidenByZExt())) {
1767     Value *NewDef = DU.WideDef;
1768     if (DU.NarrowUse->getType() != WideType) {
1769       unsigned CastWidth = SE->getTypeSizeInBits(DU.NarrowUse->getType());
1770       unsigned IVWidth = SE->getTypeSizeInBits(WideType);
1771       if (CastWidth < IVWidth) {
1772         // The cast isn't as wide as the IV, so insert a Trunc.
1773         IRBuilder<> Builder(DU.NarrowUse);
1774         NewDef = Builder.CreateTrunc(DU.WideDef, DU.NarrowUse->getType());
1775       }
1776       else {
1777         // A wider extend was hidden behind a narrower one. This may induce
1778         // another round of IV widening in which the intermediate IV becomes
1779         // dead. It should be very rare.
1780         LLVM_DEBUG(dbgs() << "INDVARS: New IV " << *WidePhi
1781                           << " not wide enough to subsume " << *DU.NarrowUse
1782                           << "\n");
1783         DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
1784         NewDef = DU.NarrowUse;
1785       }
1786     }
1787     if (NewDef != DU.NarrowUse) {
1788       LLVM_DEBUG(dbgs() << "INDVARS: eliminating " << *DU.NarrowUse
1789                         << " replaced by " << *DU.WideDef << "\n");
1790       ++NumElimExt;
1791       DU.NarrowUse->replaceAllUsesWith(NewDef);
1792       DeadInsts.emplace_back(DU.NarrowUse);
1793     }
1794     // Now that the extend is gone, we want to expose it's uses for potential
1795     // further simplification. We don't need to directly inform SimplifyIVUsers
1796     // of the new users, because their parent IV will be processed later as a
1797     // new loop phi. If we preserved IVUsers analysis, we would also want to
1798     // push the uses of WideDef here.
1799 
1800     // No further widening is needed. The deceased [sz]ext had done it for us.
1801     return nullptr;
1802   }
1803 
1804   auto tryAddRecExpansion = [&]() -> Instruction* {
1805     // Does this user itself evaluate to a recurrence after widening?
1806     WidenedRecTy WideAddRec = getExtendedOperandRecurrence(DU);
1807     if (!WideAddRec.first)
1808       WideAddRec = getWideRecurrence(DU);
1809     assert((WideAddRec.first == nullptr) ==
1810            (WideAddRec.second == ExtendKind::Unknown));
1811     if (!WideAddRec.first)
1812       return nullptr;
1813 
1814     // Reuse the IV increment that SCEVExpander created as long as it dominates
1815     // NarrowUse.
1816     Instruction *WideUse = nullptr;
1817     if (WideAddRec.first == WideIncExpr &&
1818         Rewriter.hoistIVInc(WideInc, DU.NarrowUse))
1819       WideUse = WideInc;
1820     else {
1821       WideUse = cloneIVUser(DU, WideAddRec.first);
1822       if (!WideUse)
1823         return nullptr;
1824     }
1825     // Evaluation of WideAddRec ensured that the narrow expression could be
1826     // extended outside the loop without overflow. This suggests that the wide use
1827     // evaluates to the same expression as the extended narrow use, but doesn't
1828     // absolutely guarantee it. Hence the following failsafe check. In rare cases
1829     // where it fails, we simply throw away the newly created wide use.
1830     if (WideAddRec.first != SE->getSCEV(WideUse)) {
1831       LLVM_DEBUG(dbgs() << "Wide use expression mismatch: " << *WideUse << ": "
1832                  << *SE->getSCEV(WideUse) << " != " << *WideAddRec.first
1833                  << "\n");
1834       DeadInsts.emplace_back(WideUse);
1835       return nullptr;
1836     };
1837 
1838     // if we reached this point then we are going to replace
1839     // DU.NarrowUse with WideUse. Reattach DbgValue then.
1840     replaceAllDbgUsesWith(*DU.NarrowUse, *WideUse, *WideUse, *DT);
1841 
1842     ExtendKindMap[DU.NarrowUse] = WideAddRec.second;
1843     // Returning WideUse pushes it on the worklist.
1844     return WideUse;
1845   };
1846 
1847   if (auto *I = tryAddRecExpansion())
1848     return I;
1849 
1850   // If use is a loop condition, try to promote the condition instead of
1851   // truncating the IV first.
1852   if (widenLoopCompare(DU))
1853     return nullptr;
1854 
1855   // We are here about to generate a truncate instruction that may hurt
1856   // performance because the scalar evolution expression computed earlier
1857   // in WideAddRec.first does not indicate a polynomial induction expression.
1858   // In that case, look at the operands of the use instruction to determine
1859   // if we can still widen the use instead of truncating its operand.
1860   if (widenWithVariantUse(DU))
1861     return nullptr;
1862 
1863   // This user does not evaluate to a recurrence after widening, so don't
1864   // follow it. Instead insert a Trunc to kill off the original use,
1865   // eventually isolating the original narrow IV so it can be removed.
1866   truncateIVUse(DU, DT, LI);
1867   return nullptr;
1868 }
1869 
1870 /// Add eligible users of NarrowDef to NarrowIVUsers.
1871 void WidenIV::pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef) {
1872   const SCEV *NarrowSCEV = SE->getSCEV(NarrowDef);
1873   bool NonNegativeDef =
1874       SE->isKnownPredicate(ICmpInst::ICMP_SGE, NarrowSCEV,
1875                            SE->getZero(NarrowSCEV->getType()));
1876   for (User *U : NarrowDef->users()) {
1877     Instruction *NarrowUser = cast<Instruction>(U);
1878 
1879     // Handle data flow merges and bizarre phi cycles.
1880     if (!Widened.insert(NarrowUser).second)
1881       continue;
1882 
1883     bool NonNegativeUse = false;
1884     if (!NonNegativeDef) {
1885       // We might have a control-dependent range information for this context.
1886       if (auto RangeInfo = getPostIncRangeInfo(NarrowDef, NarrowUser))
1887         NonNegativeUse = RangeInfo->getSignedMin().isNonNegative();
1888     }
1889 
1890     NarrowIVUsers.emplace_back(NarrowDef, NarrowUser, WideDef,
1891                                NonNegativeDef || NonNegativeUse);
1892   }
1893 }
1894 
1895 /// Process a single induction variable. First use the SCEVExpander to create a
1896 /// wide induction variable that evaluates to the same recurrence as the
1897 /// original narrow IV. Then use a worklist to forward traverse the narrow IV's
1898 /// def-use chain. After widenIVUse has processed all interesting IV users, the
1899 /// narrow IV will be isolated for removal by DeleteDeadPHIs.
1900 ///
1901 /// It would be simpler to delete uses as they are processed, but we must avoid
1902 /// invalidating SCEV expressions.
1903 PHINode *WidenIV::createWideIV(SCEVExpander &Rewriter) {
1904   // Is this phi an induction variable?
1905   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(OrigPhi));
1906   if (!AddRec)
1907     return nullptr;
1908 
1909   // Widen the induction variable expression.
1910   const SCEV *WideIVExpr = getExtendKind(OrigPhi) == ExtendKind::Sign
1911                                ? SE->getSignExtendExpr(AddRec, WideType)
1912                                : SE->getZeroExtendExpr(AddRec, WideType);
1913 
1914   assert(SE->getEffectiveSCEVType(WideIVExpr->getType()) == WideType &&
1915          "Expect the new IV expression to preserve its type");
1916 
1917   // Can the IV be extended outside the loop without overflow?
1918   AddRec = dyn_cast<SCEVAddRecExpr>(WideIVExpr);
1919   if (!AddRec || AddRec->getLoop() != L)
1920     return nullptr;
1921 
1922   // An AddRec must have loop-invariant operands. Since this AddRec is
1923   // materialized by a loop header phi, the expression cannot have any post-loop
1924   // operands, so they must dominate the loop header.
1925   assert(
1926       SE->properlyDominates(AddRec->getStart(), L->getHeader()) &&
1927       SE->properlyDominates(AddRec->getStepRecurrence(*SE), L->getHeader()) &&
1928       "Loop header phi recurrence inputs do not dominate the loop");
1929 
1930   // Iterate over IV uses (including transitive ones) looking for IV increments
1931   // of the form 'add nsw %iv, <const>'. For each increment and each use of
1932   // the increment calculate control-dependent range information basing on
1933   // dominating conditions inside of the loop (e.g. a range check inside of the
1934   // loop). Calculated ranges are stored in PostIncRangeInfos map.
1935   //
1936   // Control-dependent range information is later used to prove that a narrow
1937   // definition is not negative (see pushNarrowIVUsers). It's difficult to do
1938   // this on demand because when pushNarrowIVUsers needs this information some
1939   // of the dominating conditions might be already widened.
1940   if (UsePostIncrementRanges)
1941     calculatePostIncRanges(OrigPhi);
1942 
1943   // The rewriter provides a value for the desired IV expression. This may
1944   // either find an existing phi or materialize a new one. Either way, we
1945   // expect a well-formed cyclic phi-with-increments. i.e. any operand not part
1946   // of the phi-SCC dominates the loop entry.
1947   Instruction *InsertPt = &*L->getHeader()->getFirstInsertionPt();
1948   Value *ExpandInst = Rewriter.expandCodeFor(AddRec, WideType, InsertPt);
1949   // If the wide phi is not a phi node, for example a cast node, like bitcast,
1950   // inttoptr, ptrtoint, just skip for now.
1951   if (!(WidePhi = dyn_cast<PHINode>(ExpandInst))) {
1952     // if the cast node is an inserted instruction without any user, we should
1953     // remove it to make sure the pass don't touch the function as we can not
1954     // wide the phi.
1955     if (ExpandInst->hasNUses(0) &&
1956         Rewriter.isInsertedInstruction(cast<Instruction>(ExpandInst)))
1957       DeadInsts.emplace_back(ExpandInst);
1958     return nullptr;
1959   }
1960 
1961   // Remembering the WideIV increment generated by SCEVExpander allows
1962   // widenIVUse to reuse it when widening the narrow IV's increment. We don't
1963   // employ a general reuse mechanism because the call above is the only call to
1964   // SCEVExpander. Henceforth, we produce 1-to-1 narrow to wide uses.
1965   if (BasicBlock *LatchBlock = L->getLoopLatch()) {
1966     WideInc =
1967         dyn_cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));
1968     if (WideInc) {
1969       WideIncExpr = SE->getSCEV(WideInc);
1970       // Propagate the debug location associated with the original loop
1971       // increment to the new (widened) increment.
1972       auto *OrigInc =
1973           cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
1974       WideInc->setDebugLoc(OrigInc->getDebugLoc());
1975     }
1976   }
1977 
1978   LLVM_DEBUG(dbgs() << "Wide IV: " << *WidePhi << "\n");
1979   ++NumWidened;
1980 
1981   // Traverse the def-use chain using a worklist starting at the original IV.
1982   assert(Widened.empty() && NarrowIVUsers.empty() && "expect initial state" );
1983 
1984   Widened.insert(OrigPhi);
1985   pushNarrowIVUsers(OrigPhi, WidePhi);
1986 
1987   while (!NarrowIVUsers.empty()) {
1988     WidenIV::NarrowIVDefUse DU = NarrowIVUsers.pop_back_val();
1989 
1990     // Process a def-use edge. This may replace the use, so don't hold a
1991     // use_iterator across it.
1992     Instruction *WideUse = widenIVUse(DU, Rewriter);
1993 
1994     // Follow all def-use edges from the previous narrow use.
1995     if (WideUse)
1996       pushNarrowIVUsers(DU.NarrowUse, WideUse);
1997 
1998     // widenIVUse may have removed the def-use edge.
1999     if (DU.NarrowDef->use_empty())
2000       DeadInsts.emplace_back(DU.NarrowDef);
2001   }
2002 
2003   // Attach any debug information to the new PHI.
2004   replaceAllDbgUsesWith(*OrigPhi, *WidePhi, *WidePhi, *DT);
2005 
2006   return WidePhi;
2007 }
2008 
2009 /// Calculates control-dependent range for the given def at the given context
2010 /// by looking at dominating conditions inside of the loop
2011 void WidenIV::calculatePostIncRange(Instruction *NarrowDef,
2012                                     Instruction *NarrowUser) {
2013   Value *NarrowDefLHS;
2014   const APInt *NarrowDefRHS;
2015   if (!match(NarrowDef, m_NSWAdd(m_Value(NarrowDefLHS),
2016                                  m_APInt(NarrowDefRHS))) ||
2017       !NarrowDefRHS->isNonNegative())
2018     return;
2019 
2020   auto UpdateRangeFromCondition = [&] (Value *Condition,
2021                                        bool TrueDest) {
2022     CmpInst::Predicate Pred;
2023     Value *CmpRHS;
2024     if (!match(Condition, m_ICmp(Pred, m_Specific(NarrowDefLHS),
2025                                  m_Value(CmpRHS))))
2026       return;
2027 
2028     CmpInst::Predicate P =
2029             TrueDest ? Pred : CmpInst::getInversePredicate(Pred);
2030 
2031     auto CmpRHSRange = SE->getSignedRange(SE->getSCEV(CmpRHS));
2032     auto CmpConstrainedLHSRange =
2033             ConstantRange::makeAllowedICmpRegion(P, CmpRHSRange);
2034     auto NarrowDefRange = CmpConstrainedLHSRange.addWithNoWrap(
2035         *NarrowDefRHS, OverflowingBinaryOperator::NoSignedWrap);
2036 
2037     updatePostIncRangeInfo(NarrowDef, NarrowUser, NarrowDefRange);
2038   };
2039 
2040   auto UpdateRangeFromGuards = [&](Instruction *Ctx) {
2041     if (!HasGuards)
2042       return;
2043 
2044     for (Instruction &I : make_range(Ctx->getIterator().getReverse(),
2045                                      Ctx->getParent()->rend())) {
2046       Value *C = nullptr;
2047       if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(C))))
2048         UpdateRangeFromCondition(C, /*TrueDest=*/true);
2049     }
2050   };
2051 
2052   UpdateRangeFromGuards(NarrowUser);
2053 
2054   BasicBlock *NarrowUserBB = NarrowUser->getParent();
2055   // If NarrowUserBB is statically unreachable asking dominator queries may
2056   // yield surprising results. (e.g. the block may not have a dom tree node)
2057   if (!DT->isReachableFromEntry(NarrowUserBB))
2058     return;
2059 
2060   for (auto *DTB = (*DT)[NarrowUserBB]->getIDom();
2061        L->contains(DTB->getBlock());
2062        DTB = DTB->getIDom()) {
2063     auto *BB = DTB->getBlock();
2064     auto *TI = BB->getTerminator();
2065     UpdateRangeFromGuards(TI);
2066 
2067     auto *BI = dyn_cast<BranchInst>(TI);
2068     if (!BI || !BI->isConditional())
2069       continue;
2070 
2071     auto *TrueSuccessor = BI->getSuccessor(0);
2072     auto *FalseSuccessor = BI->getSuccessor(1);
2073 
2074     auto DominatesNarrowUser = [this, NarrowUser] (BasicBlockEdge BBE) {
2075       return BBE.isSingleEdge() &&
2076              DT->dominates(BBE, NarrowUser->getParent());
2077     };
2078 
2079     if (DominatesNarrowUser(BasicBlockEdge(BB, TrueSuccessor)))
2080       UpdateRangeFromCondition(BI->getCondition(), /*TrueDest=*/true);
2081 
2082     if (DominatesNarrowUser(BasicBlockEdge(BB, FalseSuccessor)))
2083       UpdateRangeFromCondition(BI->getCondition(), /*TrueDest=*/false);
2084   }
2085 }
2086 
2087 /// Calculates PostIncRangeInfos map for the given IV
2088 void WidenIV::calculatePostIncRanges(PHINode *OrigPhi) {
2089   SmallPtrSet<Instruction *, 16> Visited;
2090   SmallVector<Instruction *, 6> Worklist;
2091   Worklist.push_back(OrigPhi);
2092   Visited.insert(OrigPhi);
2093 
2094   while (!Worklist.empty()) {
2095     Instruction *NarrowDef = Worklist.pop_back_val();
2096 
2097     for (Use &U : NarrowDef->uses()) {
2098       auto *NarrowUser = cast<Instruction>(U.getUser());
2099 
2100       // Don't go looking outside the current loop.
2101       auto *NarrowUserLoop = (*LI)[NarrowUser->getParent()];
2102       if (!NarrowUserLoop || !L->contains(NarrowUserLoop))
2103         continue;
2104 
2105       if (!Visited.insert(NarrowUser).second)
2106         continue;
2107 
2108       Worklist.push_back(NarrowUser);
2109 
2110       calculatePostIncRange(NarrowDef, NarrowUser);
2111     }
2112   }
2113 }
2114 
2115 PHINode *llvm::createWideIV(const WideIVInfo &WI,
2116     LoopInfo *LI, ScalarEvolution *SE, SCEVExpander &Rewriter,
2117     DominatorTree *DT, SmallVectorImpl<WeakTrackingVH> &DeadInsts,
2118     unsigned &NumElimExt, unsigned &NumWidened,
2119     bool HasGuards, bool UsePostIncrementRanges) {
2120   WidenIV Widener(WI, LI, SE, DT, DeadInsts, HasGuards, UsePostIncrementRanges);
2121   PHINode *WidePHI = Widener.createWideIV(Rewriter);
2122   NumElimExt = Widener.getNumElimExt();
2123   NumWidened = Widener.getNumWidened();
2124   return WidePHI;
2125 }
2126