This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
[oota-llvm.git] / lib / Transforms / Utils / SimplifyIndVar.cpp
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.
13 //
14 //===----------------------------------------------------------------------===//
15
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/IVUsers.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/Analysis/LoopPass.h"
23 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Dominators.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32
33 using namespace llvm;
34
35 #define DEBUG_TYPE "indvars"
36
37 STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
38 STATISTIC(NumElimOperand,  "Number of IV operands folded into a use");
39 STATISTIC(NumElimRem     , "Number of IV remainder operations eliminated");
40 STATISTIC(NumElimCmp     , "Number of IV comparisons eliminated");
41
42 namespace {
43   /// This is a utility for simplifying induction variables
44   /// based on ScalarEvolution. It is the primary instrument of the
45   /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
46   /// other loop passes that preserve SCEV.
47   class SimplifyIndvar {
48     Loop             *L;
49     LoopInfo         *LI;
50     ScalarEvolution  *SE;
51     const DataLayout *DL; // May be NULL
52
53     SmallVectorImpl<WeakVH> &DeadInsts;
54
55     bool Changed;
56
57   public:
58     SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, LPPassManager *LPM,
59                    SmallVectorImpl<WeakVH> &Dead, IVUsers *IVU = nullptr) :
60       L(Loop),
61       LI(LPM->getAnalysisIfAvailable<LoopInfo>()),
62       SE(SE),
63       DeadInsts(Dead),
64       Changed(false) {
65       DataLayoutPass *DLP = LPM->getAnalysisIfAvailable<DataLayoutPass>();
66       DL = DLP ? &DLP->getDataLayout() : nullptr;
67       assert(LI && "IV simplification requires LoopInfo");
68     }
69
70     bool hasChanged() const { return Changed; }
71
72     /// Iteratively perform simplification on a worklist of users of the
73     /// specified induction variable. This is the top-level driver that applies
74     /// all simplicitions to users of an IV.
75     void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
76
77     Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
78
79     bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
80     void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
81     void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand,
82                               bool IsSigned);
83     bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
84
85     Instruction *splitOverflowIntrinsic(Instruction *IVUser,
86                                         const DominatorTree *DT);
87   };
88 }
89
90 /// Fold an IV operand into its use.  This removes increments of an
91 /// aligned IV when used by a instruction that ignores the low bits.
92 ///
93 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
94 ///
95 /// Return the operand of IVOperand for this induction variable if IVOperand can
96 /// be folded (in case more folding opportunities have been exposed).
97 /// Otherwise return null.
98 Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
99   Value *IVSrc = nullptr;
100   unsigned OperIdx = 0;
101   const SCEV *FoldedExpr = nullptr;
102   switch (UseInst->getOpcode()) {
103   default:
104     return nullptr;
105   case Instruction::UDiv:
106   case Instruction::LShr:
107     // We're only interested in the case where we know something about
108     // the numerator and have a constant denominator.
109     if (IVOperand != UseInst->getOperand(OperIdx) ||
110         !isa<ConstantInt>(UseInst->getOperand(1)))
111       return nullptr;
112
113     // Attempt to fold a binary operator with constant operand.
114     // e.g. ((I + 1) >> 2) => I >> 2
115     if (!isa<BinaryOperator>(IVOperand)
116         || !isa<ConstantInt>(IVOperand->getOperand(1)))
117       return nullptr;
118
119     IVSrc = IVOperand->getOperand(0);
120     // IVSrc must be the (SCEVable) IV, since the other operand is const.
121     assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
122
123     ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
124     if (UseInst->getOpcode() == Instruction::LShr) {
125       // Get a constant for the divisor. See createSCEV.
126       uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
127       if (D->getValue().uge(BitWidth))
128         return nullptr;
129
130       D = ConstantInt::get(UseInst->getContext(),
131                            APInt::getOneBitSet(BitWidth, D->getZExtValue()));
132     }
133     FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
134   }
135   // We have something that might fold it's operand. Compare SCEVs.
136   if (!SE->isSCEVable(UseInst->getType()))
137     return nullptr;
138
139   // Bypass the operand if SCEV can prove it has no effect.
140   if (SE->getSCEV(UseInst) != FoldedExpr)
141     return nullptr;
142
143   DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
144         << " -> " << *UseInst << '\n');
145
146   UseInst->setOperand(OperIdx, IVSrc);
147   assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
148
149   ++NumElimOperand;
150   Changed = true;
151   if (IVOperand->use_empty())
152     DeadInsts.push_back(IVOperand);
153   return IVSrc;
154 }
155
156 /// SimplifyIVUsers helper for eliminating useless
157 /// comparisons against an induction variable.
158 void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
159   unsigned IVOperIdx = 0;
160   ICmpInst::Predicate Pred = ICmp->getPredicate();
161   if (IVOperand != ICmp->getOperand(0)) {
162     // Swapped
163     assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
164     IVOperIdx = 1;
165     Pred = ICmpInst::getSwappedPredicate(Pred);
166   }
167
168   // Get the SCEVs for the ICmp operands.
169   const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx));
170   const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx));
171
172   // Simplify unnecessary loops away.
173   const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
174   S = SE->getSCEVAtScope(S, ICmpLoop);
175   X = SE->getSCEVAtScope(X, ICmpLoop);
176
177   // If the condition is always true or always false, replace it with
178   // a constant value.
179   if (SE->isKnownPredicate(Pred, S, X))
180     ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
181   else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X))
182     ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
183   else
184     return;
185
186   DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
187   ++NumElimCmp;
188   Changed = true;
189   DeadInsts.push_back(ICmp);
190 }
191
192 /// SimplifyIVUsers helper for eliminating useless
193 /// remainder operations operating on an induction variable.
194 void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem,
195                                       Value *IVOperand,
196                                       bool IsSigned) {
197   // We're only interested in the case where we know something about
198   // the numerator.
199   if (IVOperand != Rem->getOperand(0))
200     return;
201
202   // Get the SCEVs for the ICmp operands.
203   const SCEV *S = SE->getSCEV(Rem->getOperand(0));
204   const SCEV *X = SE->getSCEV(Rem->getOperand(1));
205
206   // Simplify unnecessary loops away.
207   const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
208   S = SE->getSCEVAtScope(S, ICmpLoop);
209   X = SE->getSCEVAtScope(X, ICmpLoop);
210
211   // i % n  -->  i  if i is in [0,n).
212   if ((!IsSigned || SE->isKnownNonNegative(S)) &&
213       SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
214                            S, X))
215     Rem->replaceAllUsesWith(Rem->getOperand(0));
216   else {
217     // (i+1) % n  -->  (i+1)==n?0:(i+1)  if i is in [0,n).
218     const SCEV *LessOne =
219       SE->getMinusSCEV(S, SE->getConstant(S->getType(), 1));
220     if (IsSigned && !SE->isKnownNonNegative(LessOne))
221       return;
222
223     if (!SE->isKnownPredicate(IsSigned ?
224                               ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
225                               LessOne, X))
226       return;
227
228     ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ,
229                                   Rem->getOperand(0), Rem->getOperand(1));
230     SelectInst *Sel =
231       SelectInst::Create(ICmp,
232                          ConstantInt::get(Rem->getType(), 0),
233                          Rem->getOperand(0), "tmp", Rem);
234     Rem->replaceAllUsesWith(Sel);
235   }
236
237   DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
238   ++NumElimRem;
239   Changed = true;
240   DeadInsts.push_back(Rem);
241 }
242
243 /// Eliminate an operation that consumes a simple IV and has
244 /// no observable side-effect given the range of IV values.
245 /// IVOperand is guaranteed SCEVable, but UseInst may not be.
246 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
247                                      Instruction *IVOperand) {
248   if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
249     eliminateIVComparison(ICmp, IVOperand);
250     return true;
251   }
252   if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) {
253     bool IsSigned = Rem->getOpcode() == Instruction::SRem;
254     if (IsSigned || Rem->getOpcode() == Instruction::URem) {
255       eliminateIVRemainder(Rem, IVOperand, IsSigned);
256       return true;
257     }
258   }
259
260   // Eliminate any operation that SCEV can prove is an identity function.
261   if (!SE->isSCEVable(UseInst->getType()) ||
262       (UseInst->getType() != IVOperand->getType()) ||
263       (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
264     return false;
265
266   DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
267
268   UseInst->replaceAllUsesWith(IVOperand);
269   ++NumElimIdentity;
270   Changed = true;
271   DeadInsts.push_back(UseInst);
272   return true;
273 }
274
275 /// Annotate BO with nsw / nuw if it provably does not signed-overflow /
276 /// unsigned-overflow.  Returns true if anything changed, false otherwise.
277 bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
278                                                     Value *IVOperand) {
279
280   // Currently we only handle instructions of the form "add <indvar> <value>"
281   // and "sub <indvar> <value>".
282   unsigned Op = BO->getOpcode();
283   if (!(Op == Instruction::Add || Op == Instruction::Sub))
284     return false;
285
286   // If BO is already both nuw and nsw then there is nothing left to do
287   if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
288     return false;
289
290   IntegerType *IT = cast<IntegerType>(IVOperand->getType());
291   Value *OtherOperand = nullptr;
292   int OtherOperandIdx = -1;
293   if (BO->getOperand(0) == IVOperand) {
294     OtherOperand = BO->getOperand(1);
295     OtherOperandIdx = 1;
296   } else {
297     assert(BO->getOperand(1) == IVOperand && "only other use!");
298     OtherOperand = BO->getOperand(0);
299     OtherOperandIdx = 0;
300   }
301
302   bool Changed = false;
303   const SCEV *OtherOpSCEV = SE->getSCEV(OtherOperand);
304   if (OtherOpSCEV == SE->getCouldNotCompute())
305     return false;
306
307   if (Op == Instruction::Sub) {
308     // If the subtraction is of the form "sub <indvar>, <op>", then pretend it
309     // is "add <indvar>, -<op>" and continue, else bail out.
310     if (OtherOperandIdx != 1)
311       return false;
312
313     OtherOpSCEV = SE->getNegativeSCEV(OtherOpSCEV);
314   }
315
316   const SCEV *IVOpSCEV = SE->getSCEV(IVOperand);
317   const SCEV *ZeroSCEV = SE->getConstant(IVOpSCEV->getType(), 0);
318
319   if (!BO->hasNoSignedWrap()) {
320     // Upgrade the add to an "add nsw" if we can prove that it will never
321     // sign-overflow or sign-underflow.
322
323     const SCEV *SignedMax =
324       SE->getConstant(APInt::getSignedMaxValue(IT->getBitWidth()));
325     const SCEV *SignedMin =
326       SE->getConstant(APInt::getSignedMinValue(IT->getBitWidth()));
327
328     // The addition "IVOperand + OtherOp" does not sign-overflow if the result
329     // is sign-representable in 2's complement in the given bit-width.
330     //
331     // If OtherOp is SLT 0, then for an IVOperand in [SignedMin - OtherOp,
332     // SignedMax], "IVOperand + OtherOp" is in [SignedMin, SignedMax + OtherOp].
333     // Everything in [SignedMin, SignedMax + OtherOp] is representable since
334     // SignedMax + OtherOp is at least -1.
335     //
336     // If OtherOp is SGE 0, then for an IVOperand in [SignedMin, SignedMax -
337     // OtherOp], "IVOperand + OtherOp" is in [SignedMin + OtherOp, SignedMax].
338     // Everything in [SignedMin + OtherOp, SignedMax] is representable since
339     // SignedMin + OtherOp is at most -1.
340     //
341     // It follows that for all values of IVOperand in [SignedMin - smin(0,
342     // OtherOp), SignedMax - smax(0, OtherOp)] the result of the add is
343     // representable (i.e. there is no sign-overflow).
344
345     const SCEV *UpperDelta = SE->getSMaxExpr(ZeroSCEV, OtherOpSCEV);
346     const SCEV *UpperLimit = SE->getMinusSCEV(SignedMax, UpperDelta);
347
348     bool NeverSignedOverflows =
349       SE->isKnownPredicate(ICmpInst::ICMP_SLE, IVOpSCEV, UpperLimit);
350
351     if (NeverSignedOverflows) {
352       const SCEV *LowerDelta = SE->getSMinExpr(ZeroSCEV, OtherOpSCEV);
353       const SCEV *LowerLimit = SE->getMinusSCEV(SignedMin, LowerDelta);
354
355       bool NeverSignedUnderflows =
356         SE->isKnownPredicate(ICmpInst::ICMP_SGE, IVOpSCEV, LowerLimit);
357       if (NeverSignedUnderflows) {
358         BO->setHasNoSignedWrap(true);
359         Changed = true;
360       }
361     }
362   }
363
364   if (!BO->hasNoUnsignedWrap()) {
365     // Upgrade the add computing "IVOperand + OtherOp" to an "add nuw" if we can
366     // prove that it will never unsigned-overflow (i.e. the result will always
367     // be representable in the given bit-width).
368     //
369     // "IVOperand + OtherOp" is unsigned-representable in 2's complement iff it
370     // does not produce a carry.  "IVOperand + OtherOp" produces no carry iff
371     // IVOperand ULE (UnsignedMax - OtherOp).
372
373     const SCEV *UnsignedMax =
374       SE->getConstant(APInt::getMaxValue(IT->getBitWidth()));
375     const SCEV *UpperLimit = SE->getMinusSCEV(UnsignedMax, OtherOpSCEV);
376
377     bool NeverUnsignedOverflows =
378         SE->isKnownPredicate(ICmpInst::ICMP_ULE, IVOpSCEV, UpperLimit);
379
380     if (NeverUnsignedOverflows) {
381       BO->setHasNoUnsignedWrap(true);
382       Changed = true;
383     }
384   }
385
386   return Changed;
387 }
388
389 /// \brief Split sadd.with.overflow into add + sadd.with.overflow to allow
390 /// analysis and optimization.
391 ///
392 /// \return A new value representing the non-overflowing add if possible,
393 /// otherwise return the original value.
394 Instruction *SimplifyIndvar::splitOverflowIntrinsic(Instruction *IVUser,
395                                                     const DominatorTree *DT) {
396   IntrinsicInst *II = dyn_cast<IntrinsicInst>(IVUser);
397   if (!II || II->getIntrinsicID() != Intrinsic::sadd_with_overflow)
398     return IVUser;
399
400   // Find a branch guarded by the overflow check.
401   BranchInst *Branch = nullptr;
402   Instruction *AddVal = nullptr;
403   for (User *U : II->users()) {
404     if (ExtractValueInst *ExtractInst = dyn_cast<ExtractValueInst>(U)) {
405       if (ExtractInst->getNumIndices() != 1)
406         continue;
407       if (ExtractInst->getIndices()[0] == 0)
408         AddVal = ExtractInst;
409       else if (ExtractInst->getIndices()[0] == 1 && ExtractInst->hasOneUse())
410         Branch = dyn_cast<BranchInst>(ExtractInst->user_back());
411     }
412   }
413   if (!AddVal || !Branch)
414     return IVUser;
415
416   BasicBlock *ContinueBB = Branch->getSuccessor(1);
417   if (std::next(pred_begin(ContinueBB)) != pred_end(ContinueBB))
418     return IVUser;
419
420   // Check if all users of the add are provably NSW.
421   bool AllNSW = true;
422   for (Use &U : AddVal->uses()) {
423     if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser())) {
424       BasicBlock *UseBB = UseInst->getParent();
425       if (PHINode *PHI = dyn_cast<PHINode>(UseInst))
426         UseBB = PHI->getIncomingBlock(U);
427       if (!DT->dominates(ContinueBB, UseBB)) {
428         AllNSW = false;
429         break;
430       }
431     }
432   }
433   if (!AllNSW)
434     return IVUser;
435
436   // Go for it...
437   IRBuilder<> Builder(IVUser);
438   Instruction *AddInst = dyn_cast<Instruction>(
439     Builder.CreateNSWAdd(II->getOperand(0), II->getOperand(1)));
440
441   // The caller expects the new add to have the same form as the intrinsic. The
442   // IV operand position must be the same.
443   assert((AddInst->getOpcode() == Instruction::Add &&
444           AddInst->getOperand(0) == II->getOperand(0)) &&
445          "Bad add instruction created from overflow intrinsic.");
446
447   AddVal->replaceAllUsesWith(AddInst);
448   DeadInsts.push_back(AddVal);
449   return AddInst;
450 }
451
452 /// Add all uses of Def to the current IV's worklist.
453 static void pushIVUsers(
454   Instruction *Def,
455   SmallPtrSet<Instruction*,16> &Simplified,
456   SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
457
458   for (User *U : Def->users()) {
459     Instruction *UI = cast<Instruction>(U);
460
461     // Avoid infinite or exponential worklist processing.
462     // Also ensure unique worklist users.
463     // If Def is a LoopPhi, it may not be in the Simplified set, so check for
464     // self edges first.
465     if (UI != Def && Simplified.insert(UI).second)
466       SimpleIVUsers.push_back(std::make_pair(UI, Def));
467   }
468 }
469
470 /// Return true if this instruction generates a simple SCEV
471 /// expression in terms of that IV.
472 ///
473 /// This is similar to IVUsers' isInteresting() but processes each instruction
474 /// non-recursively when the operand is already known to be a simpleIVUser.
475 ///
476 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
477   if (!SE->isSCEVable(I->getType()))
478     return false;
479
480   // Get the symbolic expression for this instruction.
481   const SCEV *S = SE->getSCEV(I);
482
483   // Only consider affine recurrences.
484   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
485   if (AR && AR->getLoop() == L)
486     return true;
487
488   return false;
489 }
490
491 /// Iteratively perform simplification on a worklist of users
492 /// of the specified induction variable. Each successive simplification may push
493 /// more users which may themselves be candidates for simplification.
494 ///
495 /// This algorithm does not require IVUsers analysis. Instead, it simplifies
496 /// instructions in-place during analysis. Rather than rewriting induction
497 /// variables bottom-up from their users, it transforms a chain of IVUsers
498 /// top-down, updating the IR only when it encouters a clear optimization
499 /// opportunitiy.
500 ///
501 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
502 ///
503 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
504   if (!SE->isSCEVable(CurrIV->getType()))
505     return;
506
507   // Instructions processed by SimplifyIndvar for CurrIV.
508   SmallPtrSet<Instruction*,16> Simplified;
509
510   // Use-def pairs if IV users waiting to be processed for CurrIV.
511   SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
512
513   // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
514   // called multiple times for the same LoopPhi. This is the proper thing to
515   // do for loop header phis that use each other.
516   pushIVUsers(CurrIV, Simplified, SimpleIVUsers);
517
518   while (!SimpleIVUsers.empty()) {
519     std::pair<Instruction*, Instruction*> UseOper =
520       SimpleIVUsers.pop_back_val();
521     Instruction *UseInst = UseOper.first;
522
523     // Bypass back edges to avoid extra work.
524     if (UseInst == CurrIV) continue;
525
526     if (V && V->shouldSplitOverflowInstrinsics()) {
527       UseInst = splitOverflowIntrinsic(UseInst, V->getDomTree());
528       if (!UseInst)
529         continue;
530     }
531
532     Instruction *IVOperand = UseOper.second;
533     for (unsigned N = 0; IVOperand; ++N) {
534       assert(N <= Simplified.size() && "runaway iteration");
535
536       Value *NewOper = foldIVUser(UseOper.first, IVOperand);
537       if (!NewOper)
538         break; // done folding
539       IVOperand = dyn_cast<Instruction>(NewOper);
540     }
541     if (!IVOperand)
542       continue;
543
544     if (eliminateIVUser(UseOper.first, IVOperand)) {
545       pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
546       continue;
547     }
548
549     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) {
550       if (isa<OverflowingBinaryOperator>(BO) &&
551           strengthenOverflowingOperation(BO, IVOperand)) {
552         // re-queue uses of the now modified binary operator and fall
553         // through to the checks that remain.
554         pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
555       }
556     }
557
558     CastInst *Cast = dyn_cast<CastInst>(UseOper.first);
559     if (V && Cast) {
560       V->visitCast(Cast);
561       continue;
562     }
563     if (isSimpleIVUser(UseOper.first, L, SE)) {
564       pushIVUsers(UseOper.first, Simplified, SimpleIVUsers);
565     }
566   }
567 }
568
569 namespace llvm {
570
571 void IVVisitor::anchor() { }
572
573 /// Simplify instructions that use this induction variable
574 /// by using ScalarEvolution to analyze the IV's recurrence.
575 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM,
576                        SmallVectorImpl<WeakVH> &Dead, IVVisitor *V)
577 {
578   LoopInfo *LI = &LPM->getAnalysis<LoopInfo>();
579   SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, LPM, Dead);
580   SIV.simplifyUsers(CurrIV, V);
581   return SIV.hasChanged();
582 }
583
584 /// Simplify users of induction variables within this
585 /// loop. This does not actually change or add IVs.
586 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM,
587                      SmallVectorImpl<WeakVH> &Dead) {
588   bool Changed = false;
589   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
590     Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, LPM, Dead);
591   }
592   return Changed;
593 }
594
595 } // namespace llvm