blob: cef8fe1a614abe9623964fb65105d34d9519ad90 [file] [log] [blame]
Andrew Trick3ec331e2011-08-10 03:46:27 +00001//===-- 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
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Transforms/Utils/SimplifyIndVar.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
Andrew Trick3ec331e2011-08-10 03:46:27 +000020#include "llvm/Analysis/LoopInfo.h"
21#include "llvm/Analysis/LoopPass.h"
22#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000024#include "llvm/IR/Dominators.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Instructions.h"
Andrew Trick0ba77a02013-12-23 23:31:49 +000027#include "llvm/IR/IntrinsicInst.h"
David Greenb26a0a42017-07-05 13:25:58 +000028#include "llvm/IR/PatternMatch.h"
Andrew Trick3ec331e2011-08-10 03:46:27 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Andrew Trick3ec331e2011-08-10 03:46:27 +000031
32using namespace llvm;
33
Chandler Carruth964daaa2014-04-22 02:55:47 +000034#define DEBUG_TYPE "indvars"
35
Andrew Trick3ec331e2011-08-10 03:46:27 +000036STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
37STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +000038STATISTIC(NumFoldedUser, "Number of IV users folded into a constant");
Andrew Trick3ec331e2011-08-10 03:46:27 +000039STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
Hongbin Zhengbfd7c382017-03-30 21:56:56 +000040STATISTIC(
41 NumSimplifiedSDiv,
42 "Number of IV signed division operations converted to unsigned division");
Hongbin Zhengf0093e42017-09-25 17:39:40 +000043STATISTIC(
44 NumSimplifiedSRem,
45 "Number of IV signed remainder operations converted to unsigned remainder");
Andrew Trick3ec331e2011-08-10 03:46:27 +000046STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
47
48namespace {
Sanjay Patel7777b502014-11-12 18:07:42 +000049 /// This is a utility for simplifying induction variables
Andrew Trick3ec331e2011-08-10 03:46:27 +000050 /// based on ScalarEvolution. It is the primary instrument of the
51 /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
52 /// other loop passes that preserve SCEV.
53 class SimplifyIndvar {
54 Loop *L;
55 LoopInfo *LI;
Andrew Trick3ec331e2011-08-10 03:46:27 +000056 ScalarEvolution *SE;
Sanjoy Das5c8bead2015-10-06 21:44:49 +000057 DominatorTree *DT;
Andrew Trick3ec331e2011-08-10 03:46:27 +000058
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000059 SmallVectorImpl<WeakTrackingVH> &DeadInsts;
Andrew Trick3ec331e2011-08-10 03:46:27 +000060
61 bool Changed;
62
63 public:
Sanjoy Das5c8bead2015-10-06 21:44:49 +000064 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000065 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead)
Sanjoy Das5c8bead2015-10-06 21:44:49 +000066 : L(Loop), LI(LI), SE(SE), DT(DT), DeadInsts(Dead), Changed(false) {
Andrew Tricke629d002011-08-10 04:22:26 +000067 assert(LI && "IV simplification requires LoopInfo");
Andrew Trick3ec331e2011-08-10 03:46:27 +000068 }
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
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000074 /// all simplifications to users of an IV.
Craig Topperf40110f2014-04-25 05:29:35 +000075 void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
Andrew Trick3ec331e2011-08-10 03:46:27 +000076
Andrew Trick74664d52011-08-10 04:01:31 +000077 Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
Andrew Trick3ec331e2011-08-10 03:46:27 +000078
Sanjoy Das088bb0e2015-10-06 21:44:39 +000079 bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand);
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +000080 bool foldConstantSCEV(Instruction *UseInst);
Sanjoy Das088bb0e2015-10-06 21:44:39 +000081
Sanjoy Dasae09b3c2016-05-29 00:36:25 +000082 bool eliminateOverflowIntrinsic(CallInst *CI);
Andrew Trick3ec331e2011-08-10 03:46:27 +000083 bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
84 void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
Hongbin Zhengf0093e42017-09-25 17:39:40 +000085 void simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
86 bool IsSigned);
87 void replaceRemWithNumerator(BinaryOperator *Rem);
88 void replaceRemWithNumeratorOrZero(BinaryOperator *Rem);
89 void replaceSRemWithURem(BinaryOperator *Rem);
Hongbin Zhengbfd7c382017-03-30 21:56:56 +000090 bool eliminateSDiv(BinaryOperator *SDiv);
Sanjoy Das7c0ce262015-01-06 19:02:56 +000091 bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
David Greenb26a0a42017-07-05 13:25:58 +000092 bool strengthenRightShift(BinaryOperator *BO, Value *IVOperand);
Andrew Trick3ec331e2011-08-10 03:46:27 +000093 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000094}
Andrew Trick3ec331e2011-08-10 03:46:27 +000095
Sanjay Patel7777b502014-11-12 18:07:42 +000096/// Fold an IV operand into its use. This removes increments of an
Andrew Trick3ec331e2011-08-10 03:46:27 +000097/// aligned IV when used by a instruction that ignores the low bits.
Andrew Trick74664d52011-08-10 04:01:31 +000098///
Andrew Trick7251e412011-09-19 17:54:39 +000099/// IVOperand is guaranteed SCEVable, but UseInst may not be.
100///
Andrew Trick74664d52011-08-10 04:01:31 +0000101/// Return the operand of IVOperand for this induction variable if IVOperand can
Andrew Trick6dbb0602011-08-10 18:07:05 +0000102/// be folded (in case more folding opportunities have been exposed).
Andrew Trick74664d52011-08-10 04:01:31 +0000103/// Otherwise return null.
104Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
Craig Topperf40110f2014-04-25 05:29:35 +0000105 Value *IVSrc = nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000106 unsigned OperIdx = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000107 const SCEV *FoldedExpr = nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000108 switch (UseInst->getOpcode()) {
109 default:
Craig Topperf40110f2014-04-25 05:29:35 +0000110 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000111 case Instruction::UDiv:
112 case Instruction::LShr:
113 // We're only interested in the case where we know something about
114 // the numerator and have a constant denominator.
115 if (IVOperand != UseInst->getOperand(OperIdx) ||
116 !isa<ConstantInt>(UseInst->getOperand(1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000117 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000118
119 // Attempt to fold a binary operator with constant operand.
120 // e.g. ((I + 1) >> 2) => I >> 2
Andrew Trick94904582011-11-17 23:36:35 +0000121 if (!isa<BinaryOperator>(IVOperand)
122 || !isa<ConstantInt>(IVOperand->getOperand(1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000123 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000124
125 IVSrc = IVOperand->getOperand(0);
126 // IVSrc must be the (SCEVable) IV, since the other operand is const.
127 assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
128
129 ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
130 if (UseInst->getOpcode() == Instruction::LShr) {
131 // Get a constant for the divisor. See createSCEV.
132 uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
133 if (D->getValue().uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000134 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000135
136 D = ConstantInt::get(UseInst->getContext(),
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +0000137 APInt::getOneBitSet(BitWidth, D->getZExtValue()));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000138 }
139 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
140 }
141 // We have something that might fold it's operand. Compare SCEVs.
142 if (!SE->isSCEVable(UseInst->getType()))
Craig Topperf40110f2014-04-25 05:29:35 +0000143 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000144
145 // Bypass the operand if SCEV can prove it has no effect.
146 if (SE->getSCEV(UseInst) != FoldedExpr)
Craig Topperf40110f2014-04-25 05:29:35 +0000147 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000148
149 DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
150 << " -> " << *UseInst << '\n');
151
152 UseInst->setOperand(OperIdx, IVSrc);
153 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
154
155 ++NumElimOperand;
156 Changed = true;
157 if (IVOperand->use_empty())
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000158 DeadInsts.emplace_back(IVOperand);
Andrew Trick74664d52011-08-10 04:01:31 +0000159 return IVSrc;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000160}
161
Sanjay Patel7777b502014-11-12 18:07:42 +0000162/// SimplifyIVUsers helper for eliminating useless
Andrew Trick3ec331e2011-08-10 03:46:27 +0000163/// comparisons against an induction variable.
164void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
165 unsigned IVOperIdx = 0;
166 ICmpInst::Predicate Pred = ICmp->getPredicate();
Max Kazantsevb9edcbc2017-07-08 17:17:30 +0000167 ICmpInst::Predicate OriginalPred = Pred;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000168 if (IVOperand != ICmp->getOperand(0)) {
169 // Swapped
170 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
171 IVOperIdx = 1;
172 Pred = ICmpInst::getSwappedPredicate(Pred);
173 }
174
175 // Get the SCEVs for the ICmp operands.
176 const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx));
177 const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx));
178
179 // Simplify unnecessary loops away.
180 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
181 S = SE->getSCEVAtScope(S, ICmpLoop);
182 X = SE->getSCEVAtScope(X, ICmpLoop);
183
Sanjoy Das5dab2052015-07-27 21:42:49 +0000184 ICmpInst::Predicate InvariantPredicate;
185 const SCEV *InvariantLHS, *InvariantRHS;
186
Andrew Trick3ec331e2011-08-10 03:46:27 +0000187 // If the condition is always true or always false, replace it with
188 // a constant value.
Sanjoy Das5dab2052015-07-27 21:42:49 +0000189 if (SE->isKnownPredicate(Pred, S, X)) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000190 ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000191 DeadInsts.emplace_back(ICmp);
Sanjoy Dasc18115d2015-08-06 20:43:28 +0000192 DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
Sanjoy Das5dab2052015-07-27 21:42:49 +0000193 } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000194 ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000195 DeadInsts.emplace_back(ICmp);
Sanjoy Dasc18115d2015-08-06 20:43:28 +0000196 DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
Sanjoy Das5dab2052015-07-27 21:42:49 +0000197 } else if (isa<PHINode>(IVOperand) &&
Sanjoy Das60fb8992016-03-18 20:37:07 +0000198 SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
199 InvariantLHS, InvariantRHS)) {
Sanjoy Das5dab2052015-07-27 21:42:49 +0000200
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000201 // Rewrite the comparison to a loop invariant comparison if it can be done
Sanjoy Das5dab2052015-07-27 21:42:49 +0000202 // cheaply, where cheaply means "we don't need to emit any new
203 // instructions".
204
205 Value *NewLHS = nullptr, *NewRHS = nullptr;
206
207 if (S == InvariantLHS || X == InvariantLHS)
208 NewLHS =
209 ICmp->getOperand(S == InvariantLHS ? IVOperIdx : (1 - IVOperIdx));
210
211 if (S == InvariantRHS || X == InvariantRHS)
212 NewRHS =
213 ICmp->getOperand(S == InvariantRHS ? IVOperIdx : (1 - IVOperIdx));
214
Sanjoy Das74af78e32016-03-18 20:37:11 +0000215 auto *PN = cast<PHINode>(IVOperand);
216 for (unsigned i = 0, e = PN->getNumIncomingValues();
217 i != e && (!NewLHS || !NewRHS);
218 ++i) {
219
220 // If this is a value incoming from the backedge, then it cannot be a loop
221 // invariant value (since we know that IVOperand is an induction variable).
222 if (L->contains(PN->getIncomingBlock(i)))
223 continue;
224
225 // NB! This following assert does not fundamentally have to be true, but
226 // it is true today given how SCEV analyzes induction variables.
227 // Specifically, today SCEV will *not* recognize %iv as an induction
228 // variable in the following case:
229 //
230 // define void @f(i32 %k) {
231 // entry:
232 // br i1 undef, label %r, label %l
233 //
234 // l:
235 // %k.inc.l = add i32 %k, 1
236 // br label %loop
237 //
238 // r:
239 // %k.inc.r = add i32 %k, 1
240 // br label %loop
241 //
242 // loop:
243 // %iv = phi i32 [ %k.inc.l, %l ], [ %k.inc.r, %r ], [ %iv.inc, %loop ]
244 // %iv.inc = add i32 %iv, 1
245 // br label %loop
246 // }
247 //
248 // but if it starts to, at some point, then the assertion below will have
249 // to be changed to a runtime check.
250
251 Value *Incoming = PN->getIncomingValue(i);
252
253#ifndef NDEBUG
254 if (auto *I = dyn_cast<Instruction>(Incoming))
255 assert(DT->dominates(I, ICmp) && "Should be a unique loop dominating value!");
256#endif
Sanjoy Das5dab2052015-07-27 21:42:49 +0000257
258 const SCEV *IncomingS = SE->getSCEV(Incoming);
259
260 if (!NewLHS && IncomingS == InvariantLHS)
261 NewLHS = Incoming;
262 if (!NewRHS && IncomingS == InvariantRHS)
263 NewRHS = Incoming;
264 }
265
266 if (!NewLHS || !NewRHS)
267 // We could not find an existing value to replace either LHS or RHS.
268 // Generating new instructions has subtler tradeoffs, so avoid doing that
269 // for now.
270 return;
271
Sanjoy Dasc18115d2015-08-06 20:43:28 +0000272 DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
Sanjoy Das5dab2052015-07-27 21:42:49 +0000273 ICmp->setPredicate(InvariantPredicate);
274 ICmp->setOperand(0, NewLHS);
275 ICmp->setOperand(1, NewRHS);
Max Kazantsevb9edcbc2017-07-08 17:17:30 +0000276 } else if (ICmpInst::isSigned(OriginalPred) &&
277 SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) {
278 // If we were unable to make anything above, all we can is to canonicalize
279 // the comparison hoping that it will open the doors for other
280 // optimizations. If we find out that we compare two non-negative values,
281 // we turn the instruction's predicate to its unsigned version. Note that
282 // we cannot rely on Pred here unless we check if we have swapped it.
283 assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
284 DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp << '\n');
285 ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000286 } else
Andrew Trick3ec331e2011-08-10 03:46:27 +0000287 return;
288
Andrew Trick3ec331e2011-08-10 03:46:27 +0000289 ++NumElimCmp;
290 Changed = true;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000291}
292
Hongbin Zhengbfd7c382017-03-30 21:56:56 +0000293bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
294 // Get the SCEVs for the ICmp operands.
295 auto *N = SE->getSCEV(SDiv->getOperand(0));
296 auto *D = SE->getSCEV(SDiv->getOperand(1));
297
298 // Simplify unnecessary loops away.
299 const Loop *L = LI->getLoopFor(SDiv->getParent());
300 N = SE->getSCEVAtScope(N, L);
301 D = SE->getSCEVAtScope(D, L);
302
303 // Replace sdiv by udiv if both of the operands are non-negative
304 if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
305 auto *UDiv = BinaryOperator::Create(
306 BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
307 SDiv->getName() + ".udiv", SDiv);
308 UDiv->setIsExact(SDiv->isExact());
309 SDiv->replaceAllUsesWith(UDiv);
310 DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
311 ++NumSimplifiedSDiv;
312 Changed = true;
313 DeadInsts.push_back(SDiv);
314 return true;
315 }
316
317 return false;
318}
319
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000320// i %s n -> i %u n if i >= 0 and n >= 0
321void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) {
322 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
323 auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D,
324 Rem->getName() + ".urem", Rem);
325 Rem->replaceAllUsesWith(URem);
326 DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n');
327 ++NumSimplifiedSRem;
Hongbin Zhengbbe448a2017-09-25 18:10:36 +0000328 Changed = true;
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000329 DeadInsts.emplace_back(Rem);
330}
Andrew Trick3ec331e2011-08-10 03:46:27 +0000331
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000332// i % n --> i if i is in [0,n).
333void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) {
334 Rem->replaceAllUsesWith(Rem->getOperand(0));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000335 DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
336 ++NumElimRem;
337 Changed = true;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000338 DeadInsts.emplace_back(Rem);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000339}
340
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000341// (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
342void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) {
343 auto *T = Rem->getType();
344 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
345 ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D);
346 SelectInst *Sel =
347 SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem);
348 Rem->replaceAllUsesWith(Sel);
349 DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
350 ++NumElimRem;
351 Changed = true;
352 DeadInsts.emplace_back(Rem);
353}
354
355/// SimplifyIVUsers helper for eliminating useless remainder operations
356/// operating on an induction variable or replacing srem by urem.
357void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
358 bool IsSigned) {
359 auto *NValue = Rem->getOperand(0);
360 auto *DValue = Rem->getOperand(1);
361 // We're only interested in the case where we know something about
362 // the numerator, unless it is a srem, because we want to replace srem by urem
363 // in general.
364 bool UsedAsNumerator = IVOperand == NValue;
365 if (!UsedAsNumerator && !IsSigned)
366 return;
367
368 const SCEV *N = SE->getSCEV(NValue);
369
370 // Simplify unnecessary loops away.
371 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
372 N = SE->getSCEVAtScope(N, ICmpLoop);
373
374 bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
375
376 // Do not proceed if the Numerator may be negative
377 if (!IsNumeratorNonNegative)
378 return;
379
380 const SCEV *D = SE->getSCEV(DValue);
381 D = SE->getSCEVAtScope(D, ICmpLoop);
382
383 if (UsedAsNumerator) {
384 auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
385 if (SE->isKnownPredicate(LT, N, D)) {
386 replaceRemWithNumerator(Rem);
387 return;
388 }
389
390 auto *T = Rem->getType();
391 const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
392 if (SE->isKnownPredicate(LT, NLessOne, D)) {
393 replaceRemWithNumeratorOrZero(Rem);
394 return;
395 }
396 }
397
398 // Try to replace SRem with URem, if both N and D are known non-negative.
399 // Since we had already check N, we only need to check D now
400 if (!IsSigned || !SE->isKnownNonNegative(D))
401 return;
402
403 replaceSRemWithURem(Rem);
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000404}
405
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000406bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) {
407 auto *F = CI->getCalledFunction();
408 if (!F)
409 return false;
410
411 typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)(
Max Kazantsevdc803662017-06-15 11:48:21 +0000412 const SCEV *, const SCEV *, SCEV::NoWrapFlags, unsigned);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000413 typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)(
Max Kazantsev8d0322e2017-06-30 05:04:09 +0000414 const SCEV *, Type *, unsigned);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000415
416 OperationFunctionTy Operation;
417 ExtensionFunctionTy Extension;
418
419 Instruction::BinaryOps RawOp;
420
421 // We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we
422 // have nuw.
423 bool NoSignedOverflow;
424
425 switch (F->getIntrinsicID()) {
426 default:
427 return false;
428
429 case Intrinsic::sadd_with_overflow:
430 Operation = &ScalarEvolution::getAddExpr;
431 Extension = &ScalarEvolution::getSignExtendExpr;
432 RawOp = Instruction::Add;
433 NoSignedOverflow = true;
434 break;
435
436 case Intrinsic::uadd_with_overflow:
437 Operation = &ScalarEvolution::getAddExpr;
438 Extension = &ScalarEvolution::getZeroExtendExpr;
439 RawOp = Instruction::Add;
440 NoSignedOverflow = false;
441 break;
442
443 case Intrinsic::ssub_with_overflow:
444 Operation = &ScalarEvolution::getMinusSCEV;
445 Extension = &ScalarEvolution::getSignExtendExpr;
446 RawOp = Instruction::Sub;
447 NoSignedOverflow = true;
448 break;
449
450 case Intrinsic::usub_with_overflow:
451 Operation = &ScalarEvolution::getMinusSCEV;
452 Extension = &ScalarEvolution::getZeroExtendExpr;
453 RawOp = Instruction::Sub;
454 NoSignedOverflow = false;
455 break;
456 }
457
458 const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0));
459 const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1));
460
461 auto *NarrowTy = cast<IntegerType>(LHS->getType());
462 auto *WideTy =
463 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
464
465 const SCEV *A =
Max Kazantsev8d0322e2017-06-30 05:04:09 +0000466 (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0),
467 WideTy, 0);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000468 const SCEV *B =
Max Kazantsev8d0322e2017-06-30 05:04:09 +0000469 (SE->*Operation)((SE->*Extension)(LHS, WideTy, 0),
470 (SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000471
472 if (A != B)
473 return false;
474
475 // Proved no overflow, nuke the overflow check and, if possible, the overflow
476 // intrinsic as well.
477
478 BinaryOperator *NewResult = BinaryOperator::Create(
479 RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI);
480
481 if (NoSignedOverflow)
482 NewResult->setHasNoSignedWrap(true);
483 else
484 NewResult->setHasNoUnsignedWrap(true);
485
486 SmallVector<ExtractValueInst *, 4> ToDelete;
487
488 for (auto *U : CI->users()) {
489 if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
490 if (EVI->getIndices()[0] == 1)
491 EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext()));
492 else {
493 assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
494 EVI->replaceAllUsesWith(NewResult);
495 }
496 ToDelete.push_back(EVI);
497 }
498 }
499
500 for (auto *EVI : ToDelete)
501 EVI->eraseFromParent();
502
503 if (CI->use_empty())
504 CI->eraseFromParent();
505
506 return true;
507}
508
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000509/// Eliminate an operation that consumes a simple IV and has no observable
510/// side-effect given the range of IV values. IVOperand is guaranteed SCEVable,
511/// but UseInst may not be.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000512bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
513 Instruction *IVOperand) {
514 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
515 eliminateIVComparison(ICmp, IVOperand);
516 return true;
517 }
Hongbin Zhengbfd7c382017-03-30 21:56:56 +0000518 if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
519 bool IsSRem = Bin->getOpcode() == Instruction::SRem;
520 if (IsSRem || Bin->getOpcode() == Instruction::URem) {
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000521 simplifyIVRemainder(Bin, IVOperand, IsSRem);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000522 return true;
523 }
Hongbin Zhengbfd7c382017-03-30 21:56:56 +0000524
525 if (Bin->getOpcode() == Instruction::SDiv)
526 return eliminateSDiv(Bin);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000527 }
528
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000529 if (auto *CI = dyn_cast<CallInst>(UseInst))
530 if (eliminateOverflowIntrinsic(CI))
531 return true;
532
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000533 if (eliminateIdentitySCEV(UseInst, IVOperand))
534 return true;
535
536 return false;
537}
538
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +0000539/// Replace the UseInst with a constant if possible
540bool SimplifyIndvar::foldConstantSCEV(Instruction *I) {
541 if (!SE->isSCEVable(I->getType()))
542 return false;
543
544 // Get the symbolic expression for this instruction.
545 const SCEV *S = SE->getSCEV(I);
546
547 const Loop *L = LI->getLoopFor(I->getParent());
548 S = SE->getSCEVAtScope(S, L);
549
550 if (auto *C = dyn_cast<SCEVConstant>(S)) {
551 I->replaceAllUsesWith(C->getValue());
552 DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
553 << " with constant: " << *C << '\n');
554 ++NumFoldedUser;
555 Changed = true;
556 DeadInsts.emplace_back(I);
557 return true;
558 }
559
560 return false;
561}
562
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000563/// Eliminate any operation that SCEV can prove is an identity function.
564bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
565 Instruction *IVOperand) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000566 if (!SE->isSCEVable(UseInst->getType()) ||
567 (UseInst->getType() != IVOperand->getType()) ||
568 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
569 return false;
570
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000571 // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
572 // dominator tree, even if X is an operand to Y. For instance, in
573 //
574 // %iv = phi i32 {0,+,1}
575 // br %cond, label %left, label %merge
576 //
577 // left:
578 // %X = add i32 %iv, 0
579 // br label %merge
580 //
581 // merge:
582 // %M = phi (%X, %iv)
583 //
584 // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
585 // %M.replaceAllUsesWith(%X) would be incorrect.
586
587 if (isa<PHINode>(UseInst))
588 // If UseInst is not a PHI node then we know that IVOperand dominates
589 // UseInst directly from the legality of SSA.
590 if (!DT || !DT->dominates(IVOperand, UseInst))
591 return false;
592
Sanjoy Das0015e5a2015-10-07 17:38:31 +0000593 if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
594 return false;
595
Andrew Trick3ec331e2011-08-10 03:46:27 +0000596 DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
597
598 UseInst->replaceAllUsesWith(IVOperand);
599 ++NumElimIdentity;
600 Changed = true;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000601 DeadInsts.emplace_back(UseInst);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000602 return true;
603}
604
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000605/// Annotate BO with nsw / nuw if it provably does not signed-overflow /
606/// unsigned-overflow. Returns true if anything changed, false otherwise.
607bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
608 Value *IVOperand) {
609
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000610 // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000611 if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
612 return false;
613
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000614 const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
Max Kazantsevdc803662017-06-15 11:48:21 +0000615 SCEV::NoWrapFlags, unsigned);
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000616 switch (BO->getOpcode()) {
617 default:
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000618 return false;
619
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000620 case Instruction::Add:
621 GetExprForBO = &ScalarEvolution::getAddExpr;
622 break;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000623
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000624 case Instruction::Sub:
625 GetExprForBO = &ScalarEvolution::getMinusSCEV;
626 break;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000627
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000628 case Instruction::Mul:
629 GetExprForBO = &ScalarEvolution::getMulExpr;
630 break;
631 }
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000632
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000633 unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
634 Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
635 const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
636 const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000637
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000638 bool Changed = false;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000639
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000640 if (!BO->hasNoUnsignedWrap()) {
641 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
642 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
643 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
Max Kazantsevdc803662017-06-15 11:48:21 +0000644 SCEV::FlagAnyWrap, 0u);
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000645 if (ExtendAfterOp == OpAfterExtend) {
646 BO->setHasNoUnsignedWrap();
647 SE->forgetValue(BO);
648 Changed = true;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000649 }
650 }
651
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000652 if (!BO->hasNoSignedWrap()) {
653 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
654 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
655 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
Max Kazantsevdc803662017-06-15 11:48:21 +0000656 SCEV::FlagAnyWrap, 0u);
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000657 if (ExtendAfterOp == OpAfterExtend) {
658 BO->setHasNoSignedWrap();
659 SE->forgetValue(BO);
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000660 Changed = true;
661 }
662 }
663
664 return Changed;
665}
666
David Greenb26a0a42017-07-05 13:25:58 +0000667/// Annotate the Shr in (X << IVOperand) >> C as exact using the
668/// information from the IV's range. Returns true if anything changed, false
669/// otherwise.
670bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
671 Value *IVOperand) {
672 using namespace llvm::PatternMatch;
673
674 if (BO->getOpcode() == Instruction::Shl) {
675 bool Changed = false;
676 ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
677 for (auto *U : BO->users()) {
678 const APInt *C;
679 if (match(U,
680 m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
681 match(U,
682 m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
683 BinaryOperator *Shr = cast<BinaryOperator>(U);
684 if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
685 Shr->setIsExact(true);
686 Changed = true;
687 }
688 }
689 }
690 return Changed;
691 }
692
693 return false;
694}
695
Sanjay Patel7777b502014-11-12 18:07:42 +0000696/// Add all uses of Def to the current IV's worklist.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000697static void pushIVUsers(
698 Instruction *Def,
699 SmallPtrSet<Instruction*,16> &Simplified,
700 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
701
Chandler Carruthcdf47882014-03-09 03:16:01 +0000702 for (User *U : Def->users()) {
703 Instruction *UI = cast<Instruction>(U);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000704
705 // Avoid infinite or exponential worklist processing.
706 // Also ensure unique worklist users.
707 // If Def is a LoopPhi, it may not be in the Simplified set, so check for
708 // self edges first.
David Blaikie70573dc2014-11-19 07:49:26 +0000709 if (UI != Def && Simplified.insert(UI).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000710 SimpleIVUsers.push_back(std::make_pair(UI, Def));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000711 }
712}
713
Sanjay Patel7777b502014-11-12 18:07:42 +0000714/// Return true if this instruction generates a simple SCEV
Andrew Trick3ec331e2011-08-10 03:46:27 +0000715/// expression in terms of that IV.
716///
Andrew Trick6dbb0602011-08-10 18:07:05 +0000717/// This is similar to IVUsers' isInteresting() but processes each instruction
Andrew Trick3ec331e2011-08-10 03:46:27 +0000718/// non-recursively when the operand is already known to be a simpleIVUser.
719///
720static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
721 if (!SE->isSCEVable(I->getType()))
722 return false;
723
724 // Get the symbolic expression for this instruction.
725 const SCEV *S = SE->getSCEV(I);
726
727 // Only consider affine recurrences.
728 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
729 if (AR && AR->getLoop() == L)
730 return true;
731
732 return false;
733}
734
Sanjay Patel7777b502014-11-12 18:07:42 +0000735/// Iteratively perform simplification on a worklist of users
Andrew Trick3ec331e2011-08-10 03:46:27 +0000736/// of the specified induction variable. Each successive simplification may push
737/// more users which may themselves be candidates for simplification.
738///
739/// This algorithm does not require IVUsers analysis. Instead, it simplifies
740/// instructions in-place during analysis. Rather than rewriting induction
741/// variables bottom-up from their users, it transforms a chain of IVUsers
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000742/// top-down, updating the IR only when it encounters a clear optimization
743/// opportunity.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000744///
745/// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
746///
747void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
Andrew Trick7251e412011-09-19 17:54:39 +0000748 if (!SE->isSCEVable(CurrIV->getType()))
749 return;
750
Andrew Trick3ec331e2011-08-10 03:46:27 +0000751 // Instructions processed by SimplifyIndvar for CurrIV.
752 SmallPtrSet<Instruction*,16> Simplified;
753
754 // Use-def pairs if IV users waiting to be processed for CurrIV.
755 SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
756
757 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
758 // called multiple times for the same LoopPhi. This is the proper thing to
759 // do for loop header phis that use each other.
760 pushIVUsers(CurrIV, Simplified, SimpleIVUsers);
761
762 while (!SimpleIVUsers.empty()) {
763 std::pair<Instruction*, Instruction*> UseOper =
764 SimpleIVUsers.pop_back_val();
Andrew Trick0ba77a02013-12-23 23:31:49 +0000765 Instruction *UseInst = UseOper.first;
766
Andrew Trick3ec331e2011-08-10 03:46:27 +0000767 // Bypass back edges to avoid extra work.
Andrew Trick0ba77a02013-12-23 23:31:49 +0000768 if (UseInst == CurrIV) continue;
769
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +0000770 // Try to replace UseInst with a constant before any other simplifications
771 if (foldConstantSCEV(UseInst))
772 continue;
773
Andrew Trick74664d52011-08-10 04:01:31 +0000774 Instruction *IVOperand = UseOper.second;
775 for (unsigned N = 0; IVOperand; ++N) {
776 assert(N <= Simplified.size() && "runaway iteration");
Andrew Trick3ec331e2011-08-10 03:46:27 +0000777
Andrew Trick74664d52011-08-10 04:01:31 +0000778 Value *NewOper = foldIVUser(UseOper.first, IVOperand);
779 if (!NewOper)
780 break; // done folding
781 IVOperand = dyn_cast<Instruction>(NewOper);
782 }
783 if (!IVOperand)
784 continue;
785
786 if (eliminateIVUser(UseOper.first, IVOperand)) {
787 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000788 continue;
789 }
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000790
791 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) {
David Greenb26a0a42017-07-05 13:25:58 +0000792 if ((isa<OverflowingBinaryOperator>(BO) &&
793 strengthenOverflowingOperation(BO, IVOperand)) ||
794 (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) {
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000795 // re-queue uses of the now modified binary operator and fall
796 // through to the checks that remain.
797 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
798 }
799 }
800
Andrew Trick3ec331e2011-08-10 03:46:27 +0000801 CastInst *Cast = dyn_cast<CastInst>(UseOper.first);
802 if (V && Cast) {
803 V->visitCast(Cast);
804 continue;
805 }
806 if (isSimpleIVUser(UseOper.first, L, SE)) {
807 pushIVUsers(UseOper.first, Simplified, SimpleIVUsers);
808 }
809 }
810}
811
812namespace llvm {
813
David Blaikiea379b1812011-12-20 02:50:00 +0000814void IVVisitor::anchor() { }
815
Sanjay Patel7777b502014-11-12 18:07:42 +0000816/// Simplify instructions that use this induction variable
Andrew Trick3ec331e2011-08-10 03:46:27 +0000817/// by using ScalarEvolution to analyze the IV's recurrence.
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000818bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000819 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead,
Justin Bogner843fb202015-12-15 19:40:57 +0000820 IVVisitor *V) {
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000821 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Dead);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000822 SIV.simplifyUsers(CurrIV, V);
823 return SIV.hasChanged();
824}
825
Sanjay Patel7777b502014-11-12 18:07:42 +0000826/// Simplify users of induction variables within this
Andrew Trick3ec331e2011-08-10 03:46:27 +0000827/// loop. This does not actually change or add IVs.
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000828bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000829 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000830 bool Changed = false;
831 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
Justin Bogner843fb202015-12-15 19:40:57 +0000832 Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000833 }
834 return Changed;
835}
836
Andrew Trick3ec331e2011-08-10 03:46:27 +0000837} // namespace llvm