1 //===-- SimplifyIndVar.cpp - Induction variable simplification ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements induction variable simplification. It does
11 // not define any actual pass or policy, but provides a single function to
12 // simplify a loop's induction variables based on ScalarEvolution.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/ScalarEvolutionExpander.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/Instructions.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"
33 #define DEBUG_TYPE "indvars"
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");
41 "Number of IV signed division operations converted to unsigned division");
44 "Number of IV signed remainder operations converted to unsigned remainder");
45 STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
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 {
57 SCEVExpander &Rewriter;
58 SmallVectorImpl<WeakTrackingVH> &DeadInsts;
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),
68 assert(LI && "IV simplification requires LoopInfo");
71 bool hasChanged() const { return Changed; }
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);
78 Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
80 bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand);
81 bool replaceIVUserWithLoopInvariant(Instruction *UseInst);
83 bool eliminateOverflowIntrinsic(CallInst *CI);
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,
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);
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.
102 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
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()) {
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)))
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)))
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");
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))
140 D = ConstantInt::get(UseInst->getContext(),
141 APInt::getOneBitSet(BitWidth, D->getZExtValue()));
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;
150 // We have something that might fold it's operand. Compare SCEVs.
151 if (!SE->isSCEVable(UseInst->getType()))
154 // Bypass the operand if SCEV can prove it has no effect.
155 if (SE->getSCEV(UseInst) != FoldedExpr)
158 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
159 << " -> " << *UseInst << '\n');
161 UseInst->setOperand(OperIdx, IVSrc);
162 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
164 if (MustDropExactFlag)
165 UseInst->dropPoisonGeneratingFlags();
169 if (IVOperand->use_empty())
170 DeadInsts.emplace_back(IVOperand);
174 bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
176 unsigned IVOperIdx = 0;
177 ICmpInst::Predicate Pred = ICmp->getPredicate();
178 if (IVOperand != ICmp->getOperand(0)) {
180 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
182 Pred = ICmpInst::getSwappedPredicate(Pred);
185 // Get the SCEVs for the ICmp operands (in the specific context of the
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);
191 ICmpInst::Predicate InvariantPredicate;
192 const SCEV *InvariantLHS, *InvariantRHS;
194 auto *PN = dyn_cast<PHINode>(IVOperand);
197 if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
198 InvariantLHS, InvariantRHS))
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
205 SmallDenseMap<const SCEV*, Value*> CheapExpansions;
206 CheapExpansions[S] = ICmp->getOperand(IVOperIdx);
207 CheapExpansions[X] = ICmp->getOperand(1 - IVOperIdx);
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);
214 Value *Incoming = PN->getIncomingValue(Idx);
215 const SCEV *IncomingS = SE->getSCEV(Incoming);
216 CheapExpansions[IncomingS] = Incoming;
219 Value *NewLHS = CheapExpansions[InvariantLHS];
220 Value *NewRHS = CheapExpansions[InvariantRHS];
223 if (auto *ConstLHS = dyn_cast<SCEVConstant>(InvariantLHS))
224 NewLHS = ConstLHS->getValue();
226 if (auto *ConstRHS = dyn_cast<SCEVConstant>(InvariantRHS))
227 NewRHS = ConstRHS->getValue();
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
235 LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
236 ICmp->setPredicate(InvariantPredicate);
237 ICmp->setOperand(0, NewLHS);
238 ICmp->setOperand(1, NewRHS);
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)) {
250 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
252 Pred = ICmpInst::getSwappedPredicate(Pred);
255 // Get the SCEVs for the ICmp operands (in the specific context of the
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);
261 // If the condition is always true or always false, replace it with
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
283 ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
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));
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);
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');
311 DeadInsts.push_back(SDiv);
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');
327 DeadInsts.emplace_back(Rem);
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');
336 DeadInsts.emplace_back(Rem);
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);
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');
350 DeadInsts.emplace_back(Rem);
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,
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
362 bool UsedAsNumerator = IVOperand == NValue;
363 if (!UsedAsNumerator && !IsSigned)
366 const SCEV *N = SE->getSCEV(NValue);
368 // Simplify unnecessary loops away.
369 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
370 N = SE->getSCEVAtScope(N, ICmpLoop);
372 bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
374 // Do not proceed if the Numerator may be negative
375 if (!IsNumeratorNonNegative)
378 const SCEV *D = SE->getSCEV(DValue);
379 D = SE->getSCEVAtScope(D, ICmpLoop);
381 if (UsedAsNumerator) {
382 auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
383 if (SE->isKnownPredicate(LT, N, D)) {
384 replaceRemWithNumerator(Rem);
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);
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))
401 replaceSRemWithURem(Rem);
404 bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) {
405 auto *F = CI->getCalledFunction();
409 typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)(
410 const SCEV *, const SCEV *, SCEV::NoWrapFlags, unsigned);
411 typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)(
412 const SCEV *, Type *, unsigned);
414 OperationFunctionTy Operation;
415 ExtensionFunctionTy Extension;
417 Instruction::BinaryOps RawOp;
419 // We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we
421 bool NoSignedOverflow;
423 switch (F->getIntrinsicID()) {
427 case Intrinsic::sadd_with_overflow:
428 Operation = &ScalarEvolution::getAddExpr;
429 Extension = &ScalarEvolution::getSignExtendExpr;
430 RawOp = Instruction::Add;
431 NoSignedOverflow = true;
434 case Intrinsic::uadd_with_overflow:
435 Operation = &ScalarEvolution::getAddExpr;
436 Extension = &ScalarEvolution::getZeroExtendExpr;
437 RawOp = Instruction::Add;
438 NoSignedOverflow = false;
441 case Intrinsic::ssub_with_overflow:
442 Operation = &ScalarEvolution::getMinusSCEV;
443 Extension = &ScalarEvolution::getSignExtendExpr;
444 RawOp = Instruction::Sub;
445 NoSignedOverflow = true;
448 case Intrinsic::usub_with_overflow:
449 Operation = &ScalarEvolution::getMinusSCEV;
450 Extension = &ScalarEvolution::getZeroExtendExpr;
451 RawOp = Instruction::Sub;
452 NoSignedOverflow = false;
456 const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0));
457 const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1));
459 auto *NarrowTy = cast<IntegerType>(LHS->getType());
461 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
464 (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0),
467 (SE->*Operation)((SE->*Extension)(LHS, WideTy, 0),
468 (SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0);
473 // Proved no overflow, nuke the overflow check and, if possible, the overflow
474 // intrinsic as well.
476 BinaryOperator *NewResult = BinaryOperator::Create(
477 RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI);
479 if (NoSignedOverflow)
480 NewResult->setHasNoSignedWrap(true);
482 NewResult->setHasNoUnsignedWrap(true);
484 SmallVector<ExtractValueInst *, 4> ToDelete;
486 for (auto *U : CI->users()) {
487 if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
488 if (EVI->getIndices()[0] == 1)
489 EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext()));
491 assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
492 EVI->replaceAllUsesWith(NewResult);
494 ToDelete.push_back(EVI);
498 for (auto *EVI : ToDelete)
499 EVI->eraseFromParent();
502 CI->eraseFromParent();
507 bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) {
508 // It is always legal to replace
509 // icmp <pred> i32 trunc(iv), n
511 // icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate.
513 // icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate.
514 // Or with either of these if pred is an equality predicate.
516 // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for
517 // every comparison which uses trunc, it means that we can replace each of
518 // them with comparison of iv against sext/zext(n). We no longer need trunc
521 // TODO: Should we do this if we can widen *some* comparisons, but not all
522 // of them? Sometimes it is enough to enable other optimizations, but the
523 // trunc instruction will stay in the loop.
524 Value *IV = TI->getOperand(0);
525 Type *IVTy = IV->getType();
526 const SCEV *IVSCEV = SE->getSCEV(IV);
527 const SCEV *TISCEV = SE->getSCEV(TI);
529 // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can
531 bool DoesSExtCollapse = false;
532 bool DoesZExtCollapse = false;
533 if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy))
534 DoesSExtCollapse = true;
535 if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy))
536 DoesZExtCollapse = true;
538 // If neither sext nor zext does collapse, it is not profitable to do any
540 if (!DoesSExtCollapse && !DoesZExtCollapse)
543 // Collect users of the trunc that look like comparisons against invariants.
544 // Bail if we find something different.
545 SmallVector<ICmpInst *, 4> ICmpUsers;
546 for (auto *U : TI->users()) {
547 // We don't care about users in unreachable blocks.
548 if (isa<Instruction>(U) &&
549 !DT->isReachableFromEntry(cast<Instruction>(U)->getParent()))
551 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) {
552 if (ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) {
553 assert(L->contains(ICI->getParent()) && "LCSSA form broken?");
554 // If we cannot get rid of trunc, bail.
555 if (ICI->isSigned() && !DoesSExtCollapse)
557 if (ICI->isUnsigned() && !DoesZExtCollapse)
559 // For equality, either signed or unsigned works.
560 ICmpUsers.push_back(ICI);
567 auto CanUseZExt = [&](ICmpInst *ICI) {
568 // Unsigned comparison can be widened as unsigned.
569 if (ICI->isUnsigned())
571 // Is it profitable to do zext?
572 if (!DoesZExtCollapse)
574 // For equality, we can safely zext both parts.
575 if (ICI->isEquality())
577 // Otherwise we can only use zext when comparing two non-negative or two
578 // negative values. But in practice, we will never pass DoesZExtCollapse
579 // check for a negative value, because zext(trunc(x)) is non-negative. So
580 // it only make sense to check for non-negativity here.
581 const SCEV *SCEVOP1 = SE->getSCEV(ICI->getOperand(0));
582 const SCEV *SCEVOP2 = SE->getSCEV(ICI->getOperand(1));
583 return SE->isKnownNonNegative(SCEVOP1) && SE->isKnownNonNegative(SCEVOP2);
585 // Replace all comparisons against trunc with comparisons against IV.
586 for (auto *ICI : ICmpUsers) {
587 auto *Op1 = ICI->getOperand(1);
588 Instruction *Ext = nullptr;
589 // For signed/unsigned predicate, replace the old comparison with comparison
590 // of immediate IV against sext/zext of the invariant argument. If we can
591 // use either sext or zext (i.e. we are dealing with equality predicate),
592 // then prefer zext as a more canonical form.
593 // TODO: If we see a signed comparison which can be turned into unsigned,
594 // we can do it here for canonicalization purposes.
595 ICmpInst::Predicate Pred = ICI->getPredicate();
596 if (CanUseZExt(ICI)) {
597 assert(DoesZExtCollapse && "Unprofitable zext?");
598 Ext = new ZExtInst(Op1, IVTy, "zext", ICI);
599 Pred = ICmpInst::getUnsignedPredicate(Pred);
601 assert(DoesSExtCollapse && "Unprofitable sext?");
602 Ext = new SExtInst(Op1, IVTy, "sext", ICI);
603 assert(Pred == ICmpInst::getSignedPredicate(Pred) && "Must be signed!");
606 L->makeLoopInvariant(Ext, Changed);
608 ICmpInst *NewICI = new ICmpInst(ICI, Pred, IV, Ext);
609 ICI->replaceAllUsesWith(NewICI);
610 DeadInsts.emplace_back(ICI);
613 // Trunc no longer needed.
614 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
615 DeadInsts.emplace_back(TI);
619 /// Eliminate an operation that consumes a simple IV and has no observable
620 /// side-effect given the range of IV values. IVOperand is guaranteed SCEVable,
621 /// but UseInst may not be.
622 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
623 Instruction *IVOperand) {
624 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
625 eliminateIVComparison(ICmp, IVOperand);
628 if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
629 bool IsSRem = Bin->getOpcode() == Instruction::SRem;
630 if (IsSRem || Bin->getOpcode() == Instruction::URem) {
631 simplifyIVRemainder(Bin, IVOperand, IsSRem);
635 if (Bin->getOpcode() == Instruction::SDiv)
636 return eliminateSDiv(Bin);
639 if (auto *CI = dyn_cast<CallInst>(UseInst))
640 if (eliminateOverflowIntrinsic(CI))
643 if (auto *TI = dyn_cast<TruncInst>(UseInst))
644 if (eliminateTrunc(TI))
647 if (eliminateIdentitySCEV(UseInst, IVOperand))
653 static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) {
654 if (auto *BB = L->getLoopPreheader())
655 return BB->getTerminator();
660 /// Replace the UseInst with a constant if possible.
661 bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) {
662 if (!SE->isSCEVable(I->getType()))
665 // Get the symbolic expression for this instruction.
666 const SCEV *S = SE->getSCEV(I);
668 if (!SE->isLoopInvariant(S, L))
671 // Do not generate something ridiculous even if S is loop invariant.
672 if (Rewriter.isHighCostExpansion(S, L, I))
675 auto *IP = GetLoopInvariantInsertPosition(L, I);
676 auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
678 I->replaceAllUsesWith(Invariant);
679 LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
680 << " with loop invariant: " << *S << '\n');
683 DeadInsts.emplace_back(I);
687 /// Eliminate any operation that SCEV can prove is an identity function.
688 bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
689 Instruction *IVOperand) {
690 if (!SE->isSCEVable(UseInst->getType()) ||
691 (UseInst->getType() != IVOperand->getType()) ||
692 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
695 // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
696 // dominator tree, even if X is an operand to Y. For instance, in
698 // %iv = phi i32 {0,+,1}
699 // br %cond, label %left, label %merge
702 // %X = add i32 %iv, 0
706 // %M = phi (%X, %iv)
708 // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
709 // %M.replaceAllUsesWith(%X) would be incorrect.
711 if (isa<PHINode>(UseInst))
712 // If UseInst is not a PHI node then we know that IVOperand dominates
713 // UseInst directly from the legality of SSA.
714 if (!DT || !DT->dominates(IVOperand, UseInst))
717 if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
720 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
722 UseInst->replaceAllUsesWith(IVOperand);
725 DeadInsts.emplace_back(UseInst);
729 /// Annotate BO with nsw / nuw if it provably does not signed-overflow /
730 /// unsigned-overflow. Returns true if anything changed, false otherwise.
731 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
734 // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
735 if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
738 const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
739 SCEV::NoWrapFlags, unsigned);
740 switch (BO->getOpcode()) {
744 case Instruction::Add:
745 GetExprForBO = &ScalarEvolution::getAddExpr;
748 case Instruction::Sub:
749 GetExprForBO = &ScalarEvolution::getMinusSCEV;
752 case Instruction::Mul:
753 GetExprForBO = &ScalarEvolution::getMulExpr;
757 unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
758 Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
759 const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
760 const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
762 bool Changed = false;
764 if (!BO->hasNoUnsignedWrap()) {
765 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
766 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
767 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
768 SCEV::FlagAnyWrap, 0u);
769 if (ExtendAfterOp == OpAfterExtend) {
770 BO->setHasNoUnsignedWrap();
776 if (!BO->hasNoSignedWrap()) {
777 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
778 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
779 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
780 SCEV::FlagAnyWrap, 0u);
781 if (ExtendAfterOp == OpAfterExtend) {
782 BO->setHasNoSignedWrap();
791 /// Annotate the Shr in (X << IVOperand) >> C as exact using the
792 /// information from the IV's range. Returns true if anything changed, false
794 bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
796 using namespace llvm::PatternMatch;
798 if (BO->getOpcode() == Instruction::Shl) {
799 bool Changed = false;
800 ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
801 for (auto *U : BO->users()) {
804 m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
806 m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
807 BinaryOperator *Shr = cast<BinaryOperator>(U);
808 if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
809 Shr->setIsExact(true);
820 /// Add all uses of Def to the current IV's worklist.
821 static void pushIVUsers(
822 Instruction *Def, Loop *L,
823 SmallPtrSet<Instruction*,16> &Simplified,
824 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
826 for (User *U : Def->users()) {
827 Instruction *UI = cast<Instruction>(U);
829 // Avoid infinite or exponential worklist processing.
830 // Also ensure unique worklist users.
831 // If Def is a LoopPhi, it may not be in the Simplified set, so check for
836 // Only change the current Loop, do not change the other parts (e.g. other
838 if (!L->contains(UI))
841 // Do not push the same instruction more than once.
842 if (!Simplified.insert(UI).second)
845 SimpleIVUsers.push_back(std::make_pair(UI, Def));
849 /// Return true if this instruction generates a simple SCEV
850 /// expression in terms of that IV.
852 /// This is similar to IVUsers' isInteresting() but processes each instruction
853 /// non-recursively when the operand is already known to be a simpleIVUser.
855 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
856 if (!SE->isSCEVable(I->getType()))
859 // Get the symbolic expression for this instruction.
860 const SCEV *S = SE->getSCEV(I);
862 // Only consider affine recurrences.
863 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
864 if (AR && AR->getLoop() == L)
870 /// Iteratively perform simplification on a worklist of users
871 /// of the specified induction variable. Each successive simplification may push
872 /// more users which may themselves be candidates for simplification.
874 /// This algorithm does not require IVUsers analysis. Instead, it simplifies
875 /// instructions in-place during analysis. Rather than rewriting induction
876 /// variables bottom-up from their users, it transforms a chain of IVUsers
877 /// top-down, updating the IR only when it encounters a clear optimization
880 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
882 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
883 if (!SE->isSCEVable(CurrIV->getType()))
886 // Instructions processed by SimplifyIndvar for CurrIV.
887 SmallPtrSet<Instruction*,16> Simplified;
889 // Use-def pairs if IV users waiting to be processed for CurrIV.
890 SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
892 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
893 // called multiple times for the same LoopPhi. This is the proper thing to
894 // do for loop header phis that use each other.
895 pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers);
897 while (!SimpleIVUsers.empty()) {
898 std::pair<Instruction*, Instruction*> UseOper =
899 SimpleIVUsers.pop_back_val();
900 Instruction *UseInst = UseOper.first;
902 // If a user of the IndVar is trivially dead, we prefer just to mark it dead
903 // rather than try to do some complex analysis or transformation (such as
904 // widening) basing on it.
905 // TODO: Propagate TLI and pass it here to handle more cases.
906 if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) {
907 DeadInsts.emplace_back(UseInst);
911 // Bypass back edges to avoid extra work.
912 if (UseInst == CurrIV) continue;
914 // Try to replace UseInst with a loop invariant before any other
916 if (replaceIVUserWithLoopInvariant(UseInst))
919 Instruction *IVOperand = UseOper.second;
920 for (unsigned N = 0; IVOperand; ++N) {
921 assert(N <= Simplified.size() && "runaway iteration");
923 Value *NewOper = foldIVUser(UseInst, IVOperand);
925 break; // done folding
926 IVOperand = dyn_cast<Instruction>(NewOper);
931 if (eliminateIVUser(UseInst, IVOperand)) {
932 pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
936 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) {
937 if ((isa<OverflowingBinaryOperator>(BO) &&
938 strengthenOverflowingOperation(BO, IVOperand)) ||
939 (isa<ShlOperator>(BO) && strengthenRightShift(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);
946 CastInst *Cast = dyn_cast<CastInst>(UseInst);
951 if (isSimpleIVUser(UseInst, L, SE)) {
952 pushIVUsers(UseInst, L, Simplified, SimpleIVUsers);
959 void IVVisitor::anchor() { }
961 /// Simplify instructions that use this induction variable
962 /// by using ScalarEvolution to analyze the IV's recurrence.
963 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
964 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead,
965 SCEVExpander &Rewriter, IVVisitor *V) {
966 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Rewriter,
968 SIV.simplifyUsers(CurrIV, V);
969 return SIV.hasChanged();
972 /// Simplify users of induction variables within this
973 /// loop. This does not actually change or add IVs.
974 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
975 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) {
976 SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars");
978 Rewriter.setDebugType(DEBUG_TYPE);
980 bool Changed = false;
981 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
982 Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead, Rewriter);