[x86/SLH] Improve name and comments for the main hardening function.
[llvm-complete.git] / lib / Transforms / Utils / SimplifyIndVar.cpp
blob7b07c9a3c4aa3dcd52edcc814c94d96ed763d004
1 //===-- SimplifyIndVar.cpp - Induction variable simplification ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
31 using namespace llvm;
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");
39 STATISTIC(
40 NumSimplifiedSDiv,
41 "Number of IV signed division operations converted to unsigned division");
42 STATISTIC(
43 NumSimplifiedSRem,
44 "Number of IV signed remainder operations converted to unsigned remainder");
45 STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
47 namespace {
48 /// This is a utility for simplifying induction variables
49 /// based on ScalarEvolution. It is the primary instrument of the
50 /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
51 /// other loop passes that preserve SCEV.
52 class SimplifyIndvar {
53 Loop *L;
54 LoopInfo *LI;
55 ScalarEvolution *SE;
56 DominatorTree *DT;
57 SCEVExpander &Rewriter;
58 SmallVectorImpl<WeakTrackingVH> &DeadInsts;
60 bool Changed;
62 public:
63 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
64 LoopInfo *LI, SCEVExpander &Rewriter,
65 SmallVectorImpl<WeakTrackingVH> &Dead)
66 : L(Loop), LI(LI), SE(SE), DT(DT), Rewriter(Rewriter), DeadInsts(Dead),
67 Changed(false) {
68 assert(LI && "IV simplification requires LoopInfo");
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,
89 bool IsSigned);
90 void replaceRemWithNumerator(BinaryOperator *Rem);
91 void replaceRemWithNumeratorOrZero(BinaryOperator *Rem);
92 void replaceSRemWithURem(BinaryOperator *Rem);
93 bool eliminateSDiv(BinaryOperator *SDiv);
94 bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
95 bool strengthenRightShift(BinaryOperator *BO, Value *IVOperand);
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 unsigned OperIdx = 0;
110 const SCEV *FoldedExpr = nullptr;
111 switch (UseInst->getOpcode()) {
112 default:
113 return nullptr;
114 case Instruction::UDiv:
115 case Instruction::LShr:
116 // We're only interested in the case where we know something about
117 // the numerator and have a constant denominator.
118 if (IVOperand != UseInst->getOperand(OperIdx) ||
119 !isa<ConstantInt>(UseInst->getOperand(1)))
120 return nullptr;
122 // Attempt to fold a binary operator with constant operand.
123 // e.g. ((I + 1) >> 2) => I >> 2
124 if (!isa<BinaryOperator>(IVOperand)
125 || !isa<ConstantInt>(IVOperand->getOperand(1)))
126 return nullptr;
128 IVSrc = IVOperand->getOperand(0);
129 // IVSrc must be the (SCEVable) IV, since the other operand is const.
130 assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
132 ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
133 if (UseInst->getOpcode() == Instruction::LShr) {
134 // Get a constant for the divisor. See createSCEV.
135 uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
136 if (D->getValue().uge(BitWidth))
137 return nullptr;
139 D = ConstantInt::get(UseInst->getContext(),
140 APInt::getOneBitSet(BitWidth, D->getZExtValue()));
142 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
144 // We have something that might fold it's operand. Compare SCEVs.
145 if (!SE->isSCEVable(UseInst->getType()))
146 return nullptr;
148 // Bypass the operand if SCEV can prove it has no effect.
149 if (SE->getSCEV(UseInst) != FoldedExpr)
150 return nullptr;
152 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
153 << " -> " << *UseInst << '\n');
155 UseInst->setOperand(OperIdx, IVSrc);
156 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
158 ++NumElimOperand;
159 Changed = true;
160 if (IVOperand->use_empty())
161 DeadInsts.emplace_back(IVOperand);
162 return IVSrc;
165 bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
166 Value *IVOperand) {
167 unsigned IVOperIdx = 0;
168 ICmpInst::Predicate Pred = ICmp->getPredicate();
169 if (IVOperand != ICmp->getOperand(0)) {
170 // Swapped
171 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
172 IVOperIdx = 1;
173 Pred = ICmpInst::getSwappedPredicate(Pred);
176 // Get the SCEVs for the ICmp operands (in the specific context of the
177 // current loop)
178 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
179 const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
180 const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
182 ICmpInst::Predicate InvariantPredicate;
183 const SCEV *InvariantLHS, *InvariantRHS;
185 auto *PN = dyn_cast<PHINode>(IVOperand);
186 if (!PN)
187 return false;
188 if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
189 InvariantLHS, InvariantRHS))
190 return false;
192 // Rewrite the comparison to a loop invariant comparison if it can be done
193 // cheaply, where cheaply means "we don't need to emit any new
194 // instructions".
196 SmallDenseMap<const SCEV*, Value*> CheapExpansions;
197 CheapExpansions[S] = ICmp->getOperand(IVOperIdx);
198 CheapExpansions[X] = ICmp->getOperand(1 - IVOperIdx);
200 // TODO: Support multiple entry loops? (We currently bail out of these in
201 // the IndVarSimplify pass)
202 if (auto *BB = L->getLoopPredecessor()) {
203 const int Idx = PN->getBasicBlockIndex(BB);
204 if (Idx >= 0) {
205 Value *Incoming = PN->getIncomingValue(Idx);
206 const SCEV *IncomingS = SE->getSCEV(Incoming);
207 CheapExpansions[IncomingS] = Incoming;
210 Value *NewLHS = CheapExpansions[InvariantLHS];
211 Value *NewRHS = CheapExpansions[InvariantRHS];
213 if (!NewLHS)
214 if (auto *ConstLHS = dyn_cast<SCEVConstant>(InvariantLHS))
215 NewLHS = ConstLHS->getValue();
216 if (!NewRHS)
217 if (auto *ConstRHS = dyn_cast<SCEVConstant>(InvariantRHS))
218 NewRHS = ConstRHS->getValue();
220 if (!NewLHS || !NewRHS)
221 // We could not find an existing value to replace either LHS or RHS.
222 // Generating new instructions has subtler tradeoffs, so avoid doing that
223 // for now.
224 return false;
226 LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
227 ICmp->setPredicate(InvariantPredicate);
228 ICmp->setOperand(0, NewLHS);
229 ICmp->setOperand(1, NewRHS);
230 return true;
233 /// SimplifyIVUsers helper for eliminating useless
234 /// comparisons against an induction variable.
235 void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
236 unsigned IVOperIdx = 0;
237 ICmpInst::Predicate Pred = ICmp->getPredicate();
238 ICmpInst::Predicate OriginalPred = Pred;
239 if (IVOperand != ICmp->getOperand(0)) {
240 // Swapped
241 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
242 IVOperIdx = 1;
243 Pred = ICmpInst::getSwappedPredicate(Pred);
246 // Get the SCEVs for the ICmp operands (in the specific context of the
247 // current loop)
248 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
249 const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
250 const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
252 // If the condition is always true or always false, replace it with
253 // a constant value.
254 if (SE->isKnownPredicate(Pred, S, X)) {
255 ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
256 DeadInsts.emplace_back(ICmp);
257 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
258 } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) {
259 ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
260 DeadInsts.emplace_back(ICmp);
261 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
262 } else if (makeIVComparisonInvariant(ICmp, IVOperand)) {
263 // fallthrough to end of function
264 } else if (ICmpInst::isSigned(OriginalPred) &&
265 SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) {
266 // If we were unable to make anything above, all we can is to canonicalize
267 // the comparison hoping that it will open the doors for other
268 // optimizations. If we find out that we compare two non-negative values,
269 // we turn the instruction's predicate to its unsigned version. Note that
270 // we cannot rely on Pred here unless we check if we have swapped it.
271 assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
272 LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp
273 << '\n');
274 ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
275 } else
276 return;
278 ++NumElimCmp;
279 Changed = true;
282 bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
283 // Get the SCEVs for the ICmp operands.
284 auto *N = SE->getSCEV(SDiv->getOperand(0));
285 auto *D = SE->getSCEV(SDiv->getOperand(1));
287 // Simplify unnecessary loops away.
288 const Loop *L = LI->getLoopFor(SDiv->getParent());
289 N = SE->getSCEVAtScope(N, L);
290 D = SE->getSCEVAtScope(D, L);
292 // Replace sdiv by udiv if both of the operands are non-negative
293 if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
294 auto *UDiv = BinaryOperator::Create(
295 BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
296 SDiv->getName() + ".udiv", SDiv);
297 UDiv->setIsExact(SDiv->isExact());
298 SDiv->replaceAllUsesWith(UDiv);
299 LLVM_DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
300 ++NumSimplifiedSDiv;
301 Changed = true;
302 DeadInsts.push_back(SDiv);
303 return true;
306 return false;
309 // i %s n -> i %u n if i >= 0 and n >= 0
310 void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) {
311 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
312 auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D,
313 Rem->getName() + ".urem", Rem);
314 Rem->replaceAllUsesWith(URem);
315 LLVM_DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n');
316 ++NumSimplifiedSRem;
317 Changed = true;
318 DeadInsts.emplace_back(Rem);
321 // i % n --> i if i is in [0,n).
322 void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) {
323 Rem->replaceAllUsesWith(Rem->getOperand(0));
324 LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
325 ++NumElimRem;
326 Changed = true;
327 DeadInsts.emplace_back(Rem);
330 // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
331 void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) {
332 auto *T = Rem->getType();
333 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
334 ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D);
335 SelectInst *Sel =
336 SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem);
337 Rem->replaceAllUsesWith(Sel);
338 LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
339 ++NumElimRem;
340 Changed = true;
341 DeadInsts.emplace_back(Rem);
344 /// SimplifyIVUsers helper for eliminating useless remainder operations
345 /// operating on an induction variable or replacing srem by urem.
346 void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
347 bool IsSigned) {
348 auto *NValue = Rem->getOperand(0);
349 auto *DValue = Rem->getOperand(1);
350 // We're only interested in the case where we know something about
351 // the numerator, unless it is a srem, because we want to replace srem by urem
352 // in general.
353 bool UsedAsNumerator = IVOperand == NValue;
354 if (!UsedAsNumerator && !IsSigned)
355 return;
357 const SCEV *N = SE->getSCEV(NValue);
359 // Simplify unnecessary loops away.
360 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
361 N = SE->getSCEVAtScope(N, ICmpLoop);
363 bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
365 // Do not proceed if the Numerator may be negative
366 if (!IsNumeratorNonNegative)
367 return;
369 const SCEV *D = SE->getSCEV(DValue);
370 D = SE->getSCEVAtScope(D, ICmpLoop);
372 if (UsedAsNumerator) {
373 auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
374 if (SE->isKnownPredicate(LT, N, D)) {
375 replaceRemWithNumerator(Rem);
376 return;
379 auto *T = Rem->getType();
380 const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
381 if (SE->isKnownPredicate(LT, NLessOne, D)) {
382 replaceRemWithNumeratorOrZero(Rem);
383 return;
387 // Try to replace SRem with URem, if both N and D are known non-negative.
388 // Since we had already check N, we only need to check D now
389 if (!IsSigned || !SE->isKnownNonNegative(D))
390 return;
392 replaceSRemWithURem(Rem);
395 bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) {
396 auto *F = CI->getCalledFunction();
397 if (!F)
398 return false;
400 typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)(
401 const SCEV *, const SCEV *, SCEV::NoWrapFlags, unsigned);
402 typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)(
403 const SCEV *, Type *, unsigned);
405 OperationFunctionTy Operation;
406 ExtensionFunctionTy Extension;
408 Instruction::BinaryOps RawOp;
410 // We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we
411 // have nuw.
412 bool NoSignedOverflow;
414 switch (F->getIntrinsicID()) {
415 default:
416 return false;
418 case Intrinsic::sadd_with_overflow:
419 Operation = &ScalarEvolution::getAddExpr;
420 Extension = &ScalarEvolution::getSignExtendExpr;
421 RawOp = Instruction::Add;
422 NoSignedOverflow = true;
423 break;
425 case Intrinsic::uadd_with_overflow:
426 Operation = &ScalarEvolution::getAddExpr;
427 Extension = &ScalarEvolution::getZeroExtendExpr;
428 RawOp = Instruction::Add;
429 NoSignedOverflow = false;
430 break;
432 case Intrinsic::ssub_with_overflow:
433 Operation = &ScalarEvolution::getMinusSCEV;
434 Extension = &ScalarEvolution::getSignExtendExpr;
435 RawOp = Instruction::Sub;
436 NoSignedOverflow = true;
437 break;
439 case Intrinsic::usub_with_overflow:
440 Operation = &ScalarEvolution::getMinusSCEV;
441 Extension = &ScalarEvolution::getZeroExtendExpr;
442 RawOp = Instruction::Sub;
443 NoSignedOverflow = false;
444 break;
447 const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0));
448 const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1));
450 auto *NarrowTy = cast<IntegerType>(LHS->getType());
451 auto *WideTy =
452 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
454 const SCEV *A =
455 (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0),
456 WideTy, 0);
457 const SCEV *B =
458 (SE->*Operation)((SE->*Extension)(LHS, WideTy, 0),
459 (SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0);
461 if (A != B)
462 return false;
464 // Proved no overflow, nuke the overflow check and, if possible, the overflow
465 // intrinsic as well.
467 BinaryOperator *NewResult = BinaryOperator::Create(
468 RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI);
470 if (NoSignedOverflow)
471 NewResult->setHasNoSignedWrap(true);
472 else
473 NewResult->setHasNoUnsignedWrap(true);
475 SmallVector<ExtractValueInst *, 4> ToDelete;
477 for (auto *U : CI->users()) {
478 if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
479 if (EVI->getIndices()[0] == 1)
480 EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext()));
481 else {
482 assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
483 EVI->replaceAllUsesWith(NewResult);
485 ToDelete.push_back(EVI);
489 for (auto *EVI : ToDelete)
490 EVI->eraseFromParent();
492 if (CI->use_empty())
493 CI->eraseFromParent();
495 return true;
498 bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) {
499 // It is always legal to replace
500 // icmp <pred> i32 trunc(iv), n
501 // with
502 // icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate.
503 // Or with
504 // icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate.
505 // Or with either of these if pred is an equality predicate.
507 // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for
508 // every comparison which uses trunc, it means that we can replace each of
509 // them with comparison of iv against sext/zext(n). We no longer need trunc
510 // after that.
512 // TODO: Should we do this if we can widen *some* comparisons, but not all
513 // of them? Sometimes it is enough to enable other optimizations, but the
514 // trunc instruction will stay in the loop.
515 Value *IV = TI->getOperand(0);
516 Type *IVTy = IV->getType();
517 const SCEV *IVSCEV = SE->getSCEV(IV);
518 const SCEV *TISCEV = SE->getSCEV(TI);
520 // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can
521 // get rid of trunc
522 bool DoesSExtCollapse = false;
523 bool DoesZExtCollapse = false;
524 if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy))
525 DoesSExtCollapse = true;
526 if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy))
527 DoesZExtCollapse = true;
529 // If neither sext nor zext does collapse, it is not profitable to do any
530 // transform. Bail.
531 if (!DoesSExtCollapse && !DoesZExtCollapse)
532 return false;
534 // Collect users of the trunc that look like comparisons against invariants.
535 // Bail if we find something different.
536 SmallVector<ICmpInst *, 4> ICmpUsers;
537 for (auto *U : TI->users()) {
538 // We don't care about users in unreachable blocks.
539 if (isa<Instruction>(U) &&
540 !DT->isReachableFromEntry(cast<Instruction>(U)->getParent()))
541 continue;
542 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) {
543 if (ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) {
544 assert(L->contains(ICI->getParent()) && "LCSSA form broken?");
545 // If we cannot get rid of trunc, bail.
546 if (ICI->isSigned() && !DoesSExtCollapse)
547 return false;
548 if (ICI->isUnsigned() && !DoesZExtCollapse)
549 return false;
550 // For equality, either signed or unsigned works.
551 ICmpUsers.push_back(ICI);
552 } else
553 return false;
554 } else
555 return false;
558 // Replace all comparisons against trunc with comparisons against IV.
559 for (auto *ICI : ICmpUsers) {
560 auto *Op1 = ICI->getOperand(1);
561 Instruction *Ext = nullptr;
562 // For signed/unsigned predicate, replace the old comparison with comparison
563 // of immediate IV against sext/zext of the invariant argument. If we can
564 // use either sext or zext (i.e. we are dealing with equality predicate),
565 // then prefer zext as a more canonical form.
566 // TODO: If we see a signed comparison which can be turned into unsigned,
567 // we can do it here for canonicalization purposes.
568 if (ICI->isUnsigned() || (ICI->isEquality() && DoesZExtCollapse)) {
569 assert(DoesZExtCollapse && "Unprofitable zext?");
570 Ext = new ZExtInst(Op1, IVTy, "zext", ICI);
571 } else {
572 assert(DoesSExtCollapse && "Unprofitable sext?");
573 Ext = new SExtInst(Op1, IVTy, "sext", ICI);
575 bool Changed;
576 L->makeLoopInvariant(Ext, Changed);
577 (void)Changed;
578 ICmpInst *NewICI = new ICmpInst(ICI, ICI->getPredicate(), IV, Ext);
579 ICI->replaceAllUsesWith(NewICI);
580 DeadInsts.emplace_back(ICI);
583 // Trunc no longer needed.
584 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
585 DeadInsts.emplace_back(TI);
586 return true;
589 /// Eliminate an operation that consumes a simple IV and has no observable
590 /// side-effect given the range of IV values. IVOperand is guaranteed SCEVable,
591 /// but UseInst may not be.
592 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
593 Instruction *IVOperand) {
594 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
595 eliminateIVComparison(ICmp, IVOperand);
596 return true;
598 if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
599 bool IsSRem = Bin->getOpcode() == Instruction::SRem;
600 if (IsSRem || Bin->getOpcode() == Instruction::URem) {
601 simplifyIVRemainder(Bin, IVOperand, IsSRem);
602 return true;
605 if (Bin->getOpcode() == Instruction::SDiv)
606 return eliminateSDiv(Bin);
609 if (auto *CI = dyn_cast<CallInst>(UseInst))
610 if (eliminateOverflowIntrinsic(CI))
611 return true;
613 if (auto *TI = dyn_cast<TruncInst>(UseInst))
614 if (eliminateTrunc(TI))
615 return true;
617 if (eliminateIdentitySCEV(UseInst, IVOperand))
618 return true;
620 return false;
623 static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) {
624 if (auto *BB = L->getLoopPreheader())
625 return BB->getTerminator();
627 return Hint;
630 /// Replace the UseInst with a constant if possible.
631 bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) {
632 if (!SE->isSCEVable(I->getType()))
633 return false;
635 // Get the symbolic expression for this instruction.
636 const SCEV *S = SE->getSCEV(I);
638 if (!SE->isLoopInvariant(S, L))
639 return false;
641 // Do not generate something ridiculous even if S is loop invariant.
642 if (Rewriter.isHighCostExpansion(S, L, I))
643 return false;
645 auto *IP = GetLoopInvariantInsertPosition(L, I);
646 auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
648 I->replaceAllUsesWith(Invariant);
649 LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
650 << " with loop invariant: " << *S << '\n');
651 ++NumFoldedUser;
652 Changed = true;
653 DeadInsts.emplace_back(I);
654 return true;
657 /// Eliminate any operation that SCEV can prove is an identity function.
658 bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
659 Instruction *IVOperand) {
660 if (!SE->isSCEVable(UseInst->getType()) ||
661 (UseInst->getType() != IVOperand->getType()) ||
662 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
663 return false;
665 // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
666 // dominator tree, even if X is an operand to Y. For instance, in
668 // %iv = phi i32 {0,+,1}
669 // br %cond, label %left, label %merge
671 // left:
672 // %X = add i32 %iv, 0
673 // br label %merge
675 // merge:
676 // %M = phi (%X, %iv)
678 // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
679 // %M.replaceAllUsesWith(%X) would be incorrect.
681 if (isa<PHINode>(UseInst))
682 // If UseInst is not a PHI node then we know that IVOperand dominates
683 // UseInst directly from the legality of SSA.
684 if (!DT || !DT->dominates(IVOperand, UseInst))
685 return false;
687 if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
688 return false;
690 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
692 UseInst->replaceAllUsesWith(IVOperand);
693 ++NumElimIdentity;
694 Changed = true;
695 DeadInsts.emplace_back(UseInst);
696 return true;
699 /// Annotate BO with nsw / nuw if it provably does not signed-overflow /
700 /// unsigned-overflow. Returns true if anything changed, false otherwise.
701 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
702 Value *IVOperand) {
704 // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
705 if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
706 return false;
708 const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
709 SCEV::NoWrapFlags, unsigned);
710 switch (BO->getOpcode()) {
711 default:
712 return false;
714 case Instruction::Add:
715 GetExprForBO = &ScalarEvolution::getAddExpr;
716 break;
718 case Instruction::Sub:
719 GetExprForBO = &ScalarEvolution::getMinusSCEV;
720 break;
722 case Instruction::Mul:
723 GetExprForBO = &ScalarEvolution::getMulExpr;
724 break;
727 unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
728 Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
729 const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
730 const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
732 bool Changed = false;
734 if (!BO->hasNoUnsignedWrap()) {
735 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
736 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
737 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
738 SCEV::FlagAnyWrap, 0u);
739 if (ExtendAfterOp == OpAfterExtend) {
740 BO->setHasNoUnsignedWrap();
741 SE->forgetValue(BO);
742 Changed = true;
746 if (!BO->hasNoSignedWrap()) {
747 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
748 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
749 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
750 SCEV::FlagAnyWrap, 0u);
751 if (ExtendAfterOp == OpAfterExtend) {
752 BO->setHasNoSignedWrap();
753 SE->forgetValue(BO);
754 Changed = true;
758 return Changed;
761 /// Annotate the Shr in (X << IVOperand) >> C as exact using the
762 /// information from the IV's range. Returns true if anything changed, false
763 /// otherwise.
764 bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
765 Value *IVOperand) {
766 using namespace llvm::PatternMatch;
768 if (BO->getOpcode() == Instruction::Shl) {
769 bool Changed = false;
770 ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
771 for (auto *U : BO->users()) {
772 const APInt *C;
773 if (match(U,
774 m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
775 match(U,
776 m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
777 BinaryOperator *Shr = cast<BinaryOperator>(U);
778 if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
779 Shr->setIsExact(true);
780 Changed = true;
784 return Changed;
787 return false;
790 /// Add all uses of Def to the current IV's worklist.
791 static void pushIVUsers(
792 Instruction *Def, Loop *L,
793 SmallPtrSet<Instruction*,16> &Simplified,
794 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
796 for (User *U : Def->users()) {
797 Instruction *UI = cast<Instruction>(U);
799 // Avoid infinite or exponential worklist processing.
800 // Also ensure unique worklist users.
801 // If Def is a LoopPhi, it may not be in the Simplified set, so check for
802 // self edges first.
803 if (UI == Def)
804 continue;
806 // Only change the current Loop, do not change the other parts (e.g. other
807 // Loops).
808 if (!L->contains(UI))
809 continue;
811 // Do not push the same instruction more than once.
812 if (!Simplified.insert(UI).second)
813 continue;
815 SimpleIVUsers.push_back(std::make_pair(UI, Def));
819 /// Return true if this instruction generates a simple SCEV
820 /// expression in terms of that IV.
822 /// This is similar to IVUsers' isInteresting() but processes each instruction
823 /// non-recursively when the operand is already known to be a simpleIVUser.
825 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
826 if (!SE->isSCEVable(I->getType()))
827 return false;
829 // Get the symbolic expression for this instruction.
830 const SCEV *S = SE->getSCEV(I);
832 // Only consider affine recurrences.
833 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
834 if (AR && AR->getLoop() == L)
835 return true;
837 return false;
840 /// Iteratively perform simplification on a worklist of users
841 /// of the specified induction variable. Each successive simplification may push
842 /// more users which may themselves be candidates for simplification.
844 /// This algorithm does not require IVUsers analysis. Instead, it simplifies
845 /// instructions in-place during analysis. Rather than rewriting induction
846 /// variables bottom-up from their users, it transforms a chain of IVUsers
847 /// top-down, updating the IR only when it encounters a clear optimization
848 /// opportunity.
850 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
852 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
853 if (!SE->isSCEVable(CurrIV->getType()))
854 return;
856 // Instructions processed by SimplifyIndvar for CurrIV.
857 SmallPtrSet<Instruction*,16> Simplified;
859 // Use-def pairs if IV users waiting to be processed for CurrIV.
860 SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
862 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
863 // called multiple times for the same LoopPhi. This is the proper thing to
864 // do for loop header phis that use each other.
865 pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers);
867 while (!SimpleIVUsers.empty()) {
868 std::pair<Instruction*, Instruction*> UseOper =
869 SimpleIVUsers.pop_back_val();
870 Instruction *UseInst = UseOper.first;
872 // If a user of the IndVar is trivially dead, we prefer just to mark it dead
873 // rather than try to do some complex analysis or transformation (such as
874 // widening) basing on it.
875 // TODO: Propagate TLI and pass it here to handle more cases.
876 if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) {
877 DeadInsts.emplace_back(UseInst);
878 continue;
881 // Bypass back edges to avoid extra work.
882 if (UseInst == CurrIV) continue;
884 // Try to replace UseInst with a loop invariant before any other
885 // simplifications.
886 if (replaceIVUserWithLoopInvariant(UseInst))
887 continue;
889 Instruction *IVOperand = UseOper.second;
890 for (unsigned N = 0; IVOperand; ++N) {
891 assert(N <= Simplified.size() && "runaway iteration");
893 Value *NewOper = foldIVUser(UseInst, IVOperand);
894 if (!NewOper)
895 break; // done folding
896 IVOperand = dyn_cast<Instruction>(NewOper);
898 if (!IVOperand)
899 continue;
901 if (eliminateIVUser(UseInst, IVOperand)) {
902 pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
903 continue;
906 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) {
907 if ((isa<OverflowingBinaryOperator>(BO) &&
908 strengthenOverflowingOperation(BO, IVOperand)) ||
909 (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) {
910 // re-queue uses of the now modified binary operator and fall
911 // through to the checks that remain.
912 pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
916 CastInst *Cast = dyn_cast<CastInst>(UseInst);
917 if (V && Cast) {
918 V->visitCast(Cast);
919 continue;
921 if (isSimpleIVUser(UseInst, L, SE)) {
922 pushIVUsers(UseInst, L, Simplified, SimpleIVUsers);
927 namespace llvm {
929 void IVVisitor::anchor() { }
931 /// Simplify instructions that use this induction variable
932 /// by using ScalarEvolution to analyze the IV's recurrence.
933 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
934 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead,
935 SCEVExpander &Rewriter, IVVisitor *V) {
936 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Rewriter,
937 Dead);
938 SIV.simplifyUsers(CurrIV, V);
939 return SIV.hasChanged();
942 /// Simplify users of induction variables within this
943 /// loop. This does not actually change or add IVs.
944 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
945 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) {
946 SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars");
947 #ifndef NDEBUG
948 Rewriter.setDebugType(DEBUG_TYPE);
949 #endif
950 bool Changed = false;
951 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
952 Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead, Rewriter);
954 return Changed;
957 } // namespace llvm