blob: 6b1d3dc4133052dfd0d97908bb566c052857c65f [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"
Andrew Trick3ec331e2011-08-10 03:46:27 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
Andrew Trick3ec331e2011-08-10 03:46:27 +000030
31using namespace llvm;
32
Chandler Carruth964daaa2014-04-22 02:55:47 +000033#define DEBUG_TYPE "indvars"
34
Andrew Trick3ec331e2011-08-10 03:46:27 +000035STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
36STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
37STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
38STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
39
40namespace {
Sanjay Patel7777b502014-11-12 18:07:42 +000041 /// This is a utility for simplifying induction variables
Andrew Trick3ec331e2011-08-10 03:46:27 +000042 /// based on ScalarEvolution. It is the primary instrument of the
43 /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
44 /// other loop passes that preserve SCEV.
45 class SimplifyIndvar {
46 Loop *L;
47 LoopInfo *LI;
Andrew Trick3ec331e2011-08-10 03:46:27 +000048 ScalarEvolution *SE;
Sanjoy Das5c8bead2015-10-06 21:44:49 +000049 DominatorTree *DT;
Andrew Trick3ec331e2011-08-10 03:46:27 +000050
51 SmallVectorImpl<WeakVH> &DeadInsts;
52
53 bool Changed;
54
55 public:
Sanjoy Das5c8bead2015-10-06 21:44:49 +000056 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
57 LoopInfo *LI,SmallVectorImpl<WeakVH> &Dead)
58 : L(Loop), LI(LI), SE(SE), DT(DT), DeadInsts(Dead), Changed(false) {
Andrew Tricke629d002011-08-10 04:22:26 +000059 assert(LI && "IV simplification requires LoopInfo");
Andrew Trick3ec331e2011-08-10 03:46:27 +000060 }
61
62 bool hasChanged() const { return Changed; }
63
64 /// Iteratively perform simplification on a worklist of users of the
65 /// specified induction variable. This is the top-level driver that applies
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000066 /// all simplifications to users of an IV.
Craig Topperf40110f2014-04-25 05:29:35 +000067 void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
Andrew Trick3ec331e2011-08-10 03:46:27 +000068
Andrew Trick74664d52011-08-10 04:01:31 +000069 Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
Andrew Trick3ec331e2011-08-10 03:46:27 +000070
Sanjoy Das088bb0e2015-10-06 21:44:39 +000071 bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand);
72
Sanjoy Dasae09b3c2016-05-29 00:36:25 +000073 bool eliminateOverflowIntrinsic(CallInst *CI);
Andrew Trick3ec331e2011-08-10 03:46:27 +000074 bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
75 void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
76 void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand,
77 bool IsSigned);
Sanjoy Das7c0ce262015-01-06 19:02:56 +000078 bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
Andrew Trick3ec331e2011-08-10 03:46:27 +000079 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000080}
Andrew Trick3ec331e2011-08-10 03:46:27 +000081
Sanjay Patel7777b502014-11-12 18:07:42 +000082/// Fold an IV operand into its use. This removes increments of an
Andrew Trick3ec331e2011-08-10 03:46:27 +000083/// aligned IV when used by a instruction that ignores the low bits.
Andrew Trick74664d52011-08-10 04:01:31 +000084///
Andrew Trick7251e412011-09-19 17:54:39 +000085/// IVOperand is guaranteed SCEVable, but UseInst may not be.
86///
Andrew Trick74664d52011-08-10 04:01:31 +000087/// Return the operand of IVOperand for this induction variable if IVOperand can
Andrew Trick6dbb0602011-08-10 18:07:05 +000088/// be folded (in case more folding opportunities have been exposed).
Andrew Trick74664d52011-08-10 04:01:31 +000089/// Otherwise return null.
90Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
Craig Topperf40110f2014-04-25 05:29:35 +000091 Value *IVSrc = nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +000092 unsigned OperIdx = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000093 const SCEV *FoldedExpr = nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +000094 switch (UseInst->getOpcode()) {
95 default:
Craig Topperf40110f2014-04-25 05:29:35 +000096 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +000097 case Instruction::UDiv:
98 case Instruction::LShr:
99 // We're only interested in the case where we know something about
100 // the numerator and have a constant denominator.
101 if (IVOperand != UseInst->getOperand(OperIdx) ||
102 !isa<ConstantInt>(UseInst->getOperand(1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000103 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000104
105 // Attempt to fold a binary operator with constant operand.
106 // e.g. ((I + 1) >> 2) => I >> 2
Andrew Trick94904582011-11-17 23:36:35 +0000107 if (!isa<BinaryOperator>(IVOperand)
108 || !isa<ConstantInt>(IVOperand->getOperand(1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000109 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000110
111 IVSrc = IVOperand->getOperand(0);
112 // IVSrc must be the (SCEVable) IV, since the other operand is const.
113 assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
114
115 ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
116 if (UseInst->getOpcode() == Instruction::LShr) {
117 // Get a constant for the divisor. See createSCEV.
118 uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
119 if (D->getValue().uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000120 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000121
122 D = ConstantInt::get(UseInst->getContext(),
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +0000123 APInt::getOneBitSet(BitWidth, D->getZExtValue()));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000124 }
125 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
126 }
127 // We have something that might fold it's operand. Compare SCEVs.
128 if (!SE->isSCEVable(UseInst->getType()))
Craig Topperf40110f2014-04-25 05:29:35 +0000129 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000130
131 // Bypass the operand if SCEV can prove it has no effect.
132 if (SE->getSCEV(UseInst) != FoldedExpr)
Craig Topperf40110f2014-04-25 05:29:35 +0000133 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000134
135 DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
136 << " -> " << *UseInst << '\n');
137
138 UseInst->setOperand(OperIdx, IVSrc);
139 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
140
141 ++NumElimOperand;
142 Changed = true;
143 if (IVOperand->use_empty())
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000144 DeadInsts.emplace_back(IVOperand);
Andrew Trick74664d52011-08-10 04:01:31 +0000145 return IVSrc;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000146}
147
Sanjay Patel7777b502014-11-12 18:07:42 +0000148/// SimplifyIVUsers helper for eliminating useless
Andrew Trick3ec331e2011-08-10 03:46:27 +0000149/// comparisons against an induction variable.
150void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
151 unsigned IVOperIdx = 0;
152 ICmpInst::Predicate Pred = ICmp->getPredicate();
153 if (IVOperand != ICmp->getOperand(0)) {
154 // Swapped
155 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
156 IVOperIdx = 1;
157 Pred = ICmpInst::getSwappedPredicate(Pred);
158 }
159
160 // Get the SCEVs for the ICmp operands.
161 const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx));
162 const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx));
163
164 // Simplify unnecessary loops away.
165 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
166 S = SE->getSCEVAtScope(S, ICmpLoop);
167 X = SE->getSCEVAtScope(X, ICmpLoop);
168
Sanjoy Das5dab2052015-07-27 21:42:49 +0000169 ICmpInst::Predicate InvariantPredicate;
170 const SCEV *InvariantLHS, *InvariantRHS;
171
Andrew Trick3ec331e2011-08-10 03:46:27 +0000172 // If the condition is always true or always false, replace it with
173 // a constant value.
Sanjoy Das5dab2052015-07-27 21:42:49 +0000174 if (SE->isKnownPredicate(Pred, S, X)) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000175 ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000176 DeadInsts.emplace_back(ICmp);
Sanjoy Dasc18115d2015-08-06 20:43:28 +0000177 DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
Sanjoy Das5dab2052015-07-27 21:42:49 +0000178 } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000179 ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000180 DeadInsts.emplace_back(ICmp);
Sanjoy Dasc18115d2015-08-06 20:43:28 +0000181 DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
Sanjoy Das5dab2052015-07-27 21:42:49 +0000182 } else if (isa<PHINode>(IVOperand) &&
Sanjoy Das60fb8992016-03-18 20:37:07 +0000183 SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
184 InvariantLHS, InvariantRHS)) {
Sanjoy Das5dab2052015-07-27 21:42:49 +0000185
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000186 // Rewrite the comparison to a loop invariant comparison if it can be done
Sanjoy Das5dab2052015-07-27 21:42:49 +0000187 // cheaply, where cheaply means "we don't need to emit any new
188 // instructions".
189
190 Value *NewLHS = nullptr, *NewRHS = nullptr;
191
192 if (S == InvariantLHS || X == InvariantLHS)
193 NewLHS =
194 ICmp->getOperand(S == InvariantLHS ? IVOperIdx : (1 - IVOperIdx));
195
196 if (S == InvariantRHS || X == InvariantRHS)
197 NewRHS =
198 ICmp->getOperand(S == InvariantRHS ? IVOperIdx : (1 - IVOperIdx));
199
Sanjoy Das74af78e32016-03-18 20:37:11 +0000200 auto *PN = cast<PHINode>(IVOperand);
201 for (unsigned i = 0, e = PN->getNumIncomingValues();
202 i != e && (!NewLHS || !NewRHS);
203 ++i) {
204
205 // If this is a value incoming from the backedge, then it cannot be a loop
206 // invariant value (since we know that IVOperand is an induction variable).
207 if (L->contains(PN->getIncomingBlock(i)))
208 continue;
209
210 // NB! This following assert does not fundamentally have to be true, but
211 // it is true today given how SCEV analyzes induction variables.
212 // Specifically, today SCEV will *not* recognize %iv as an induction
213 // variable in the following case:
214 //
215 // define void @f(i32 %k) {
216 // entry:
217 // br i1 undef, label %r, label %l
218 //
219 // l:
220 // %k.inc.l = add i32 %k, 1
221 // br label %loop
222 //
223 // r:
224 // %k.inc.r = add i32 %k, 1
225 // br label %loop
226 //
227 // loop:
228 // %iv = phi i32 [ %k.inc.l, %l ], [ %k.inc.r, %r ], [ %iv.inc, %loop ]
229 // %iv.inc = add i32 %iv, 1
230 // br label %loop
231 // }
232 //
233 // but if it starts to, at some point, then the assertion below will have
234 // to be changed to a runtime check.
235
236 Value *Incoming = PN->getIncomingValue(i);
237
238#ifndef NDEBUG
239 if (auto *I = dyn_cast<Instruction>(Incoming))
240 assert(DT->dominates(I, ICmp) && "Should be a unique loop dominating value!");
241#endif
Sanjoy Das5dab2052015-07-27 21:42:49 +0000242
243 const SCEV *IncomingS = SE->getSCEV(Incoming);
244
245 if (!NewLHS && IncomingS == InvariantLHS)
246 NewLHS = Incoming;
247 if (!NewRHS && IncomingS == InvariantRHS)
248 NewRHS = Incoming;
249 }
250
251 if (!NewLHS || !NewRHS)
252 // We could not find an existing value to replace either LHS or RHS.
253 // Generating new instructions has subtler tradeoffs, so avoid doing that
254 // for now.
255 return;
256
Sanjoy Dasc18115d2015-08-06 20:43:28 +0000257 DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
Sanjoy Das5dab2052015-07-27 21:42:49 +0000258 ICmp->setPredicate(InvariantPredicate);
259 ICmp->setOperand(0, NewLHS);
260 ICmp->setOperand(1, NewRHS);
261 } else
Andrew Trick3ec331e2011-08-10 03:46:27 +0000262 return;
263
Andrew Trick3ec331e2011-08-10 03:46:27 +0000264 ++NumElimCmp;
265 Changed = true;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000266}
267
Sanjay Patel7777b502014-11-12 18:07:42 +0000268/// SimplifyIVUsers helper for eliminating useless
Andrew Trick3ec331e2011-08-10 03:46:27 +0000269/// remainder operations operating on an induction variable.
270void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem,
271 Value *IVOperand,
272 bool IsSigned) {
273 // We're only interested in the case where we know something about
274 // the numerator.
275 if (IVOperand != Rem->getOperand(0))
276 return;
277
278 // Get the SCEVs for the ICmp operands.
279 const SCEV *S = SE->getSCEV(Rem->getOperand(0));
280 const SCEV *X = SE->getSCEV(Rem->getOperand(1));
281
282 // Simplify unnecessary loops away.
283 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
284 S = SE->getSCEVAtScope(S, ICmpLoop);
285 X = SE->getSCEVAtScope(X, ICmpLoop);
286
287 // i % n --> i if i is in [0,n).
288 if ((!IsSigned || SE->isKnownNonNegative(S)) &&
289 SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
290 S, X))
291 Rem->replaceAllUsesWith(Rem->getOperand(0));
292 else {
293 // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000294 const SCEV *LessOne = SE->getMinusSCEV(S, SE->getOne(S->getType()));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000295 if (IsSigned && !SE->isKnownNonNegative(LessOne))
296 return;
297
298 if (!SE->isKnownPredicate(IsSigned ?
299 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
300 LessOne, X))
301 return;
302
303 ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ,
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000304 Rem->getOperand(0), Rem->getOperand(1));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000305 SelectInst *Sel =
306 SelectInst::Create(ICmp,
307 ConstantInt::get(Rem->getType(), 0),
308 Rem->getOperand(0), "tmp", Rem);
309 Rem->replaceAllUsesWith(Sel);
310 }
311
Andrew Trick3ec331e2011-08-10 03:46:27 +0000312 DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
313 ++NumElimRem;
314 Changed = true;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000315 DeadInsts.emplace_back(Rem);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000316}
317
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000318bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) {
319 auto *F = CI->getCalledFunction();
320 if (!F)
321 return false;
322
323 typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)(
324 const SCEV *, const SCEV *, SCEV::NoWrapFlags);
325 typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)(
326 const SCEV *, Type *);
327
328 OperationFunctionTy Operation;
329 ExtensionFunctionTy Extension;
330
331 Instruction::BinaryOps RawOp;
332
333 // We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we
334 // have nuw.
335 bool NoSignedOverflow;
336
337 switch (F->getIntrinsicID()) {
338 default:
339 return false;
340
341 case Intrinsic::sadd_with_overflow:
342 Operation = &ScalarEvolution::getAddExpr;
343 Extension = &ScalarEvolution::getSignExtendExpr;
344 RawOp = Instruction::Add;
345 NoSignedOverflow = true;
346 break;
347
348 case Intrinsic::uadd_with_overflow:
349 Operation = &ScalarEvolution::getAddExpr;
350 Extension = &ScalarEvolution::getZeroExtendExpr;
351 RawOp = Instruction::Add;
352 NoSignedOverflow = false;
353 break;
354
355 case Intrinsic::ssub_with_overflow:
356 Operation = &ScalarEvolution::getMinusSCEV;
357 Extension = &ScalarEvolution::getSignExtendExpr;
358 RawOp = Instruction::Sub;
359 NoSignedOverflow = true;
360 break;
361
362 case Intrinsic::usub_with_overflow:
363 Operation = &ScalarEvolution::getMinusSCEV;
364 Extension = &ScalarEvolution::getZeroExtendExpr;
365 RawOp = Instruction::Sub;
366 NoSignedOverflow = false;
367 break;
368 }
369
370 const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0));
371 const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1));
372
373 auto *NarrowTy = cast<IntegerType>(LHS->getType());
374 auto *WideTy =
375 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
376
377 const SCEV *A =
378 (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap), WideTy);
379 const SCEV *B =
380 (SE->*Operation)((SE->*Extension)(LHS, WideTy),
381 (SE->*Extension)(RHS, WideTy), SCEV::FlagAnyWrap);
382
383 if (A != B)
384 return false;
385
386 // Proved no overflow, nuke the overflow check and, if possible, the overflow
387 // intrinsic as well.
388
389 BinaryOperator *NewResult = BinaryOperator::Create(
390 RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI);
391
392 if (NoSignedOverflow)
393 NewResult->setHasNoSignedWrap(true);
394 else
395 NewResult->setHasNoUnsignedWrap(true);
396
397 SmallVector<ExtractValueInst *, 4> ToDelete;
398
399 for (auto *U : CI->users()) {
400 if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
401 if (EVI->getIndices()[0] == 1)
402 EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext()));
403 else {
404 assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
405 EVI->replaceAllUsesWith(NewResult);
406 }
407 ToDelete.push_back(EVI);
408 }
409 }
410
411 for (auto *EVI : ToDelete)
412 EVI->eraseFromParent();
413
414 if (CI->use_empty())
415 CI->eraseFromParent();
416
417 return true;
418}
419
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000420/// Eliminate an operation that consumes a simple IV and has no observable
421/// side-effect given the range of IV values. IVOperand is guaranteed SCEVable,
422/// but UseInst may not be.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000423bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
424 Instruction *IVOperand) {
425 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
426 eliminateIVComparison(ICmp, IVOperand);
427 return true;
428 }
429 if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) {
430 bool IsSigned = Rem->getOpcode() == Instruction::SRem;
431 if (IsSigned || Rem->getOpcode() == Instruction::URem) {
432 eliminateIVRemainder(Rem, IVOperand, IsSigned);
433 return true;
434 }
435 }
436
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000437 if (auto *CI = dyn_cast<CallInst>(UseInst))
438 if (eliminateOverflowIntrinsic(CI))
439 return true;
440
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000441 if (eliminateIdentitySCEV(UseInst, IVOperand))
442 return true;
443
444 return false;
445}
446
447/// Eliminate any operation that SCEV can prove is an identity function.
448bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
449 Instruction *IVOperand) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000450 if (!SE->isSCEVable(UseInst->getType()) ||
451 (UseInst->getType() != IVOperand->getType()) ||
452 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
453 return false;
454
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000455 // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
456 // dominator tree, even if X is an operand to Y. For instance, in
457 //
458 // %iv = phi i32 {0,+,1}
459 // br %cond, label %left, label %merge
460 //
461 // left:
462 // %X = add i32 %iv, 0
463 // br label %merge
464 //
465 // merge:
466 // %M = phi (%X, %iv)
467 //
468 // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
469 // %M.replaceAllUsesWith(%X) would be incorrect.
470
471 if (isa<PHINode>(UseInst))
472 // If UseInst is not a PHI node then we know that IVOperand dominates
473 // UseInst directly from the legality of SSA.
474 if (!DT || !DT->dominates(IVOperand, UseInst))
475 return false;
476
Sanjoy Das0015e5a2015-10-07 17:38:31 +0000477 if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
478 return false;
479
Andrew Trick3ec331e2011-08-10 03:46:27 +0000480 DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
481
482 UseInst->replaceAllUsesWith(IVOperand);
483 ++NumElimIdentity;
484 Changed = true;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000485 DeadInsts.emplace_back(UseInst);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000486 return true;
487}
488
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000489/// Annotate BO with nsw / nuw if it provably does not signed-overflow /
490/// unsigned-overflow. Returns true if anything changed, false otherwise.
491bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
492 Value *IVOperand) {
493
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000494 // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000495 if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
496 return false;
497
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000498 const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
499 SCEV::NoWrapFlags);
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000500
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000501 switch (BO->getOpcode()) {
502 default:
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000503 return false;
504
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000505 case Instruction::Add:
506 GetExprForBO = &ScalarEvolution::getAddExpr;
507 break;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000508
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000509 case Instruction::Sub:
510 GetExprForBO = &ScalarEvolution::getMinusSCEV;
511 break;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000512
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000513 case Instruction::Mul:
514 GetExprForBO = &ScalarEvolution::getMulExpr;
515 break;
516 }
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000517
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000518 unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
519 Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
520 const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
521 const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000522
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000523 bool Changed = false;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000524
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000525 if (!BO->hasNoUnsignedWrap()) {
526 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
527 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
528 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
529 SCEV::FlagAnyWrap);
530 if (ExtendAfterOp == OpAfterExtend) {
531 BO->setHasNoUnsignedWrap();
532 SE->forgetValue(BO);
533 Changed = true;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000534 }
535 }
536
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000537 if (!BO->hasNoSignedWrap()) {
538 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
539 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
540 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
541 SCEV::FlagAnyWrap);
542 if (ExtendAfterOp == OpAfterExtend) {
543 BO->setHasNoSignedWrap();
544 SE->forgetValue(BO);
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000545 Changed = true;
546 }
547 }
548
549 return Changed;
550}
551
Sanjay Patel7777b502014-11-12 18:07:42 +0000552/// Add all uses of Def to the current IV's worklist.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000553static void pushIVUsers(
554 Instruction *Def,
555 SmallPtrSet<Instruction*,16> &Simplified,
556 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
557
Chandler Carruthcdf47882014-03-09 03:16:01 +0000558 for (User *U : Def->users()) {
559 Instruction *UI = cast<Instruction>(U);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000560
561 // Avoid infinite or exponential worklist processing.
562 // Also ensure unique worklist users.
563 // If Def is a LoopPhi, it may not be in the Simplified set, so check for
564 // self edges first.
David Blaikie70573dc2014-11-19 07:49:26 +0000565 if (UI != Def && Simplified.insert(UI).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000566 SimpleIVUsers.push_back(std::make_pair(UI, Def));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000567 }
568}
569
Sanjay Patel7777b502014-11-12 18:07:42 +0000570/// Return true if this instruction generates a simple SCEV
Andrew Trick3ec331e2011-08-10 03:46:27 +0000571/// expression in terms of that IV.
572///
Andrew Trick6dbb0602011-08-10 18:07:05 +0000573/// This is similar to IVUsers' isInteresting() but processes each instruction
Andrew Trick3ec331e2011-08-10 03:46:27 +0000574/// non-recursively when the operand is already known to be a simpleIVUser.
575///
576static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
577 if (!SE->isSCEVable(I->getType()))
578 return false;
579
580 // Get the symbolic expression for this instruction.
581 const SCEV *S = SE->getSCEV(I);
582
583 // Only consider affine recurrences.
584 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
585 if (AR && AR->getLoop() == L)
586 return true;
587
588 return false;
589}
590
Sanjay Patel7777b502014-11-12 18:07:42 +0000591/// Iteratively perform simplification on a worklist of users
Andrew Trick3ec331e2011-08-10 03:46:27 +0000592/// of the specified induction variable. Each successive simplification may push
593/// more users which may themselves be candidates for simplification.
594///
595/// This algorithm does not require IVUsers analysis. Instead, it simplifies
596/// instructions in-place during analysis. Rather than rewriting induction
597/// variables bottom-up from their users, it transforms a chain of IVUsers
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000598/// top-down, updating the IR only when it encounters a clear optimization
599/// opportunity.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000600///
601/// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
602///
603void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
Andrew Trick7251e412011-09-19 17:54:39 +0000604 if (!SE->isSCEVable(CurrIV->getType()))
605 return;
606
Andrew Trick3ec331e2011-08-10 03:46:27 +0000607 // Instructions processed by SimplifyIndvar for CurrIV.
608 SmallPtrSet<Instruction*,16> Simplified;
609
610 // Use-def pairs if IV users waiting to be processed for CurrIV.
611 SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
612
613 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
614 // called multiple times for the same LoopPhi. This is the proper thing to
615 // do for loop header phis that use each other.
616 pushIVUsers(CurrIV, Simplified, SimpleIVUsers);
617
618 while (!SimpleIVUsers.empty()) {
619 std::pair<Instruction*, Instruction*> UseOper =
620 SimpleIVUsers.pop_back_val();
Andrew Trick0ba77a02013-12-23 23:31:49 +0000621 Instruction *UseInst = UseOper.first;
622
Andrew Trick3ec331e2011-08-10 03:46:27 +0000623 // Bypass back edges to avoid extra work.
Andrew Trick0ba77a02013-12-23 23:31:49 +0000624 if (UseInst == CurrIV) continue;
625
Andrew Trick74664d52011-08-10 04:01:31 +0000626 Instruction *IVOperand = UseOper.second;
627 for (unsigned N = 0; IVOperand; ++N) {
628 assert(N <= Simplified.size() && "runaway iteration");
Andrew Trick3ec331e2011-08-10 03:46:27 +0000629
Andrew Trick74664d52011-08-10 04:01:31 +0000630 Value *NewOper = foldIVUser(UseOper.first, IVOperand);
631 if (!NewOper)
632 break; // done folding
633 IVOperand = dyn_cast<Instruction>(NewOper);
634 }
635 if (!IVOperand)
636 continue;
637
638 if (eliminateIVUser(UseOper.first, IVOperand)) {
639 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000640 continue;
641 }
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000642
643 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) {
644 if (isa<OverflowingBinaryOperator>(BO) &&
645 strengthenOverflowingOperation(BO, IVOperand)) {
646 // re-queue uses of the now modified binary operator and fall
647 // through to the checks that remain.
648 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
649 }
650 }
651
Andrew Trick3ec331e2011-08-10 03:46:27 +0000652 CastInst *Cast = dyn_cast<CastInst>(UseOper.first);
653 if (V && Cast) {
654 V->visitCast(Cast);
655 continue;
656 }
657 if (isSimpleIVUser(UseOper.first, L, SE)) {
658 pushIVUsers(UseOper.first, Simplified, SimpleIVUsers);
659 }
660 }
661}
662
663namespace llvm {
664
David Blaikiea379b1812011-12-20 02:50:00 +0000665void IVVisitor::anchor() { }
666
Sanjay Patel7777b502014-11-12 18:07:42 +0000667/// Simplify instructions that use this induction variable
Andrew Trick3ec331e2011-08-10 03:46:27 +0000668/// by using ScalarEvolution to analyze the IV's recurrence.
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000669bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
Justin Bogner843fb202015-12-15 19:40:57 +0000670 LoopInfo *LI, SmallVectorImpl<WeakVH> &Dead,
671 IVVisitor *V) {
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000672 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Dead);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000673 SIV.simplifyUsers(CurrIV, V);
674 return SIV.hasChanged();
675}
676
Sanjay Patel7777b502014-11-12 18:07:42 +0000677/// Simplify users of induction variables within this
Andrew Trick3ec331e2011-08-10 03:46:27 +0000678/// loop. This does not actually change or add IVs.
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000679bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
Justin Bogner843fb202015-12-15 19:40:57 +0000680 LoopInfo *LI, SmallVectorImpl<WeakVH> &Dead) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000681 bool Changed = false;
682 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
Justin Bogner843fb202015-12-15 19:40:57 +0000683 Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000684 }
685 return Changed;
686}
687
Andrew Trick3ec331e2011-08-10 03:46:27 +0000688} // namespace llvm