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/LoopPass.h"
22 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "indvars"
36 STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
37 STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
38 STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
39 STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
42 /// This is a utility for simplifying induction variables
43 /// based on ScalarEvolution. It is the primary instrument of the
44 /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
45 /// other loop passes that preserve SCEV.
46 class SimplifyIndvar {
51 SmallVectorImpl<WeakVH> &DeadInsts;
56 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, LoopInfo *LI,
57 SmallVectorImpl<WeakVH> &Dead)
58 : L(Loop), LI(LI), SE(SE), DeadInsts(Dead), Changed(false) {
59 assert(LI && "IV simplification requires LoopInfo");
62 bool hasChanged() const { return Changed; }
64 /// Iteratively perform simplification on a worklist of users of the
65 /// specified induction variable. This is the top-level driver that applies
66 /// all simplifications to users of an IV.
67 void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
69 Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
71 bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
72 void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
73 void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand,
75 bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
77 Instruction *splitOverflowIntrinsic(Instruction *IVUser,
78 const DominatorTree *DT);
82 /// Fold an IV operand into its use. This removes increments of an
83 /// aligned IV when used by a instruction that ignores the low bits.
85 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
87 /// Return the operand of IVOperand for this induction variable if IVOperand can
88 /// be folded (in case more folding opportunities have been exposed).
89 /// Otherwise return null.
90 Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
91 Value *IVSrc = nullptr;
93 const SCEV *FoldedExpr = nullptr;
94 switch (UseInst->getOpcode()) {
97 case Instruction::UDiv:
98 case Instruction::LShr:
99 // We're only interested in the case where we know something about
100 // the numerator and have a constant denominator.
101 if (IVOperand != UseInst->getOperand(OperIdx) ||
102 !isa<ConstantInt>(UseInst->getOperand(1)))
105 // Attempt to fold a binary operator with constant operand.
106 // e.g. ((I + 1) >> 2) => I >> 2
107 if (!isa<BinaryOperator>(IVOperand)
108 || !isa<ConstantInt>(IVOperand->getOperand(1)))
111 IVSrc = IVOperand->getOperand(0);
112 // IVSrc must be the (SCEVable) IV, since the other operand is const.
113 assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
115 ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
116 if (UseInst->getOpcode() == Instruction::LShr) {
117 // Get a constant for the divisor. See createSCEV.
118 uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
119 if (D->getValue().uge(BitWidth))
122 D = ConstantInt::get(UseInst->getContext(),
123 APInt::getOneBitSet(BitWidth, D->getZExtValue()));
125 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
127 // We have something that might fold it's operand. Compare SCEVs.
128 if (!SE->isSCEVable(UseInst->getType()))
131 // Bypass the operand if SCEV can prove it has no effect.
132 if (SE->getSCEV(UseInst) != FoldedExpr)
135 DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
136 << " -> " << *UseInst << '\n');
138 UseInst->setOperand(OperIdx, IVSrc);
139 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
143 if (IVOperand->use_empty())
144 DeadInsts.emplace_back(IVOperand);
148 /// SimplifyIVUsers helper for eliminating useless
149 /// comparisons against an induction variable.
150 void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
151 unsigned IVOperIdx = 0;
152 ICmpInst::Predicate Pred = ICmp->getPredicate();
153 if (IVOperand != ICmp->getOperand(0)) {
155 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
157 Pred = ICmpInst::getSwappedPredicate(Pred);
160 // Get the SCEVs for the ICmp operands.
161 const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx));
162 const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx));
164 // Simplify unnecessary loops away.
165 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
166 S = SE->getSCEVAtScope(S, ICmpLoop);
167 X = SE->getSCEVAtScope(X, ICmpLoop);
169 ICmpInst::Predicate InvariantPredicate;
170 const SCEV *InvariantLHS, *InvariantRHS;
172 // If the condition is always true or always false, replace it with
174 if (SE->isKnownPredicate(Pred, S, X)) {
175 ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
176 DeadInsts.emplace_back(ICmp);
177 DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
178 } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) {
179 ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
180 DeadInsts.emplace_back(ICmp);
181 DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
182 } else if (isa<PHINode>(IVOperand) &&
183 SE->isLoopInvariantPredicate(Pred, S, X, ICmpLoop,
184 InvariantPredicate, InvariantLHS,
187 // Rewrite the comparison to a loop invariant comparison if it can be done
188 // cheaply, where cheaply means "we don't need to emit any new
191 Value *NewLHS = nullptr, *NewRHS = nullptr;
193 if (S == InvariantLHS || X == InvariantLHS)
195 ICmp->getOperand(S == InvariantLHS ? IVOperIdx : (1 - IVOperIdx));
197 if (S == InvariantRHS || X == InvariantRHS)
199 ICmp->getOperand(S == InvariantRHS ? IVOperIdx : (1 - IVOperIdx));
201 for (Value *Incoming : cast<PHINode>(IVOperand)->incoming_values()) {
202 if (NewLHS && NewRHS)
205 const SCEV *IncomingS = SE->getSCEV(Incoming);
207 if (!NewLHS && IncomingS == InvariantLHS)
209 if (!NewRHS && IncomingS == InvariantRHS)
213 if (!NewLHS || !NewRHS)
214 // We could not find an existing value to replace either LHS or RHS.
215 // Generating new instructions has subtler tradeoffs, so avoid doing that
219 DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
220 ICmp->setPredicate(InvariantPredicate);
221 ICmp->setOperand(0, NewLHS);
222 ICmp->setOperand(1, NewRHS);
230 /// SimplifyIVUsers helper for eliminating useless
231 /// remainder operations operating on an induction variable.
232 void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem,
235 // We're only interested in the case where we know something about
237 if (IVOperand != Rem->getOperand(0))
240 // Get the SCEVs for the ICmp operands.
241 const SCEV *S = SE->getSCEV(Rem->getOperand(0));
242 const SCEV *X = SE->getSCEV(Rem->getOperand(1));
244 // Simplify unnecessary loops away.
245 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
246 S = SE->getSCEVAtScope(S, ICmpLoop);
247 X = SE->getSCEVAtScope(X, ICmpLoop);
249 // i % n --> i if i is in [0,n).
250 if ((!IsSigned || SE->isKnownNonNegative(S)) &&
251 SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
253 Rem->replaceAllUsesWith(Rem->getOperand(0));
255 // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
256 const SCEV *LessOne =
257 SE->getMinusSCEV(S, SE->getConstant(S->getType(), 1));
258 if (IsSigned && !SE->isKnownNonNegative(LessOne))
261 if (!SE->isKnownPredicate(IsSigned ?
262 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
266 ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ,
267 Rem->getOperand(0), Rem->getOperand(1));
269 SelectInst::Create(ICmp,
270 ConstantInt::get(Rem->getType(), 0),
271 Rem->getOperand(0), "tmp", Rem);
272 Rem->replaceAllUsesWith(Sel);
275 DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
278 DeadInsts.emplace_back(Rem);
281 /// Eliminate an operation that consumes a simple IV and has
282 /// no observable side-effect given the range of IV values.
283 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
284 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
285 Instruction *IVOperand) {
286 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
287 eliminateIVComparison(ICmp, IVOperand);
290 if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) {
291 bool IsSigned = Rem->getOpcode() == Instruction::SRem;
292 if (IsSigned || Rem->getOpcode() == Instruction::URem) {
293 eliminateIVRemainder(Rem, IVOperand, IsSigned);
298 // Eliminate any operation that SCEV can prove is an identity function.
299 if (!SE->isSCEVable(UseInst->getType()) ||
300 (UseInst->getType() != IVOperand->getType()) ||
301 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
304 DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
306 UseInst->replaceAllUsesWith(IVOperand);
309 DeadInsts.emplace_back(UseInst);
313 /// Annotate BO with nsw / nuw if it provably does not signed-overflow /
314 /// unsigned-overflow. Returns true if anything changed, false otherwise.
315 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
318 // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
319 if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
322 const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
325 switch (BO->getOpcode()) {
329 case Instruction::Add:
330 GetExprForBO = &ScalarEvolution::getAddExpr;
333 case Instruction::Sub:
334 GetExprForBO = &ScalarEvolution::getMinusSCEV;
337 case Instruction::Mul:
338 GetExprForBO = &ScalarEvolution::getMulExpr;
342 unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
343 Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
344 const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
345 const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
347 bool Changed = false;
349 if (!BO->hasNoUnsignedWrap()) {
350 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
351 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
352 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
354 if (ExtendAfterOp == OpAfterExtend) {
355 BO->setHasNoUnsignedWrap();
361 if (!BO->hasNoSignedWrap()) {
362 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
363 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
364 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
366 if (ExtendAfterOp == OpAfterExtend) {
367 BO->setHasNoSignedWrap();
376 /// \brief Split sadd.with.overflow into add + sadd.with.overflow to allow
377 /// analysis and optimization.
379 /// \return A new value representing the non-overflowing add if possible,
380 /// otherwise return the original value.
381 Instruction *SimplifyIndvar::splitOverflowIntrinsic(Instruction *IVUser,
382 const DominatorTree *DT) {
383 IntrinsicInst *II = dyn_cast<IntrinsicInst>(IVUser);
384 if (!II || II->getIntrinsicID() != Intrinsic::sadd_with_overflow)
387 // Find a branch guarded by the overflow check.
388 BranchInst *Branch = nullptr;
389 Instruction *AddVal = nullptr;
390 for (User *U : II->users()) {
391 if (ExtractValueInst *ExtractInst = dyn_cast<ExtractValueInst>(U)) {
392 if (ExtractInst->getNumIndices() != 1)
394 if (ExtractInst->getIndices()[0] == 0)
395 AddVal = ExtractInst;
396 else if (ExtractInst->getIndices()[0] == 1 && ExtractInst->hasOneUse())
397 Branch = dyn_cast<BranchInst>(ExtractInst->user_back());
400 if (!AddVal || !Branch)
403 BasicBlock *ContinueBB = Branch->getSuccessor(1);
404 if (std::next(pred_begin(ContinueBB)) != pred_end(ContinueBB))
407 // Check if all users of the add are provably NSW.
409 for (Use &U : AddVal->uses()) {
410 if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser())) {
411 BasicBlock *UseBB = UseInst->getParent();
412 if (PHINode *PHI = dyn_cast<PHINode>(UseInst))
413 UseBB = PHI->getIncomingBlock(U);
414 if (!DT->dominates(ContinueBB, UseBB)) {
424 IRBuilder<> Builder(IVUser);
425 Instruction *AddInst = dyn_cast<Instruction>(
426 Builder.CreateNSWAdd(II->getOperand(0), II->getOperand(1)));
428 // The caller expects the new add to have the same form as the intrinsic. The
429 // IV operand position must be the same.
430 assert((AddInst->getOpcode() == Instruction::Add &&
431 AddInst->getOperand(0) == II->getOperand(0)) &&
432 "Bad add instruction created from overflow intrinsic.");
434 AddVal->replaceAllUsesWith(AddInst);
435 DeadInsts.emplace_back(AddVal);
439 /// Add all uses of Def to the current IV's worklist.
440 static void pushIVUsers(
442 SmallPtrSet<Instruction*,16> &Simplified,
443 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
445 for (User *U : Def->users()) {
446 Instruction *UI = cast<Instruction>(U);
448 // Avoid infinite or exponential worklist processing.
449 // Also ensure unique worklist users.
450 // If Def is a LoopPhi, it may not be in the Simplified set, so check for
452 if (UI != Def && Simplified.insert(UI).second)
453 SimpleIVUsers.push_back(std::make_pair(UI, Def));
457 /// Return true if this instruction generates a simple SCEV
458 /// expression in terms of that IV.
460 /// This is similar to IVUsers' isInteresting() but processes each instruction
461 /// non-recursively when the operand is already known to be a simpleIVUser.
463 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
464 if (!SE->isSCEVable(I->getType()))
467 // Get the symbolic expression for this instruction.
468 const SCEV *S = SE->getSCEV(I);
470 // Only consider affine recurrences.
471 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
472 if (AR && AR->getLoop() == L)
478 /// Iteratively perform simplification on a worklist of users
479 /// of the specified induction variable. Each successive simplification may push
480 /// more users which may themselves be candidates for simplification.
482 /// This algorithm does not require IVUsers analysis. Instead, it simplifies
483 /// instructions in-place during analysis. Rather than rewriting induction
484 /// variables bottom-up from their users, it transforms a chain of IVUsers
485 /// top-down, updating the IR only when it encounters a clear optimization
488 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
490 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
491 if (!SE->isSCEVable(CurrIV->getType()))
494 // Instructions processed by SimplifyIndvar for CurrIV.
495 SmallPtrSet<Instruction*,16> Simplified;
497 // Use-def pairs if IV users waiting to be processed for CurrIV.
498 SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
500 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
501 // called multiple times for the same LoopPhi. This is the proper thing to
502 // do for loop header phis that use each other.
503 pushIVUsers(CurrIV, Simplified, SimpleIVUsers);
505 while (!SimpleIVUsers.empty()) {
506 std::pair<Instruction*, Instruction*> UseOper =
507 SimpleIVUsers.pop_back_val();
508 Instruction *UseInst = UseOper.first;
510 // Bypass back edges to avoid extra work.
511 if (UseInst == CurrIV) continue;
513 if (V && V->shouldSplitOverflowInstrinsics()) {
514 UseInst = splitOverflowIntrinsic(UseInst, V->getDomTree());
519 Instruction *IVOperand = UseOper.second;
520 for (unsigned N = 0; IVOperand; ++N) {
521 assert(N <= Simplified.size() && "runaway iteration");
523 Value *NewOper = foldIVUser(UseOper.first, IVOperand);
525 break; // done folding
526 IVOperand = dyn_cast<Instruction>(NewOper);
531 if (eliminateIVUser(UseOper.first, IVOperand)) {
532 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
536 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) {
537 if (isa<OverflowingBinaryOperator>(BO) &&
538 strengthenOverflowingOperation(BO, IVOperand)) {
539 // re-queue uses of the now modified binary operator and fall
540 // through to the checks that remain.
541 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
545 CastInst *Cast = dyn_cast<CastInst>(UseOper.first);
550 if (isSimpleIVUser(UseOper.first, L, SE)) {
551 pushIVUsers(UseOper.first, Simplified, SimpleIVUsers);
558 void IVVisitor::anchor() { }
560 /// Simplify instructions that use this induction variable
561 /// by using ScalarEvolution to analyze the IV's recurrence.
562 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM,
563 SmallVectorImpl<WeakVH> &Dead, IVVisitor *V)
565 LoopInfo *LI = &LPM->getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
566 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, LI, Dead);
567 SIV.simplifyUsers(CurrIV, V);
568 return SIV.hasChanged();
571 /// Simplify users of induction variables within this
572 /// loop. This does not actually change or add IVs.
573 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM,
574 SmallVectorImpl<WeakVH> &Dead) {
575 bool Changed = false;
576 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
577 Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, LPM, Dead);