blob: 6d634d8e917d9ddc2a9fab4bc8e95bf1a056e158 [file] [log] [blame]
Andrew Trick3ec331e2011-08-10 03:46:27 +00001//===-- SimplifyIndVar.cpp - Induction variable simplification ------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Trick3ec331e2011-08-10 03:46:27 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements induction variable simplification. It does
10// not define any actual pass or policy, but provides a single function to
11// simplify a loop's induction variables based on ScalarEvolution.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Transforms/Utils/SimplifyIndVar.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Statistic.h"
Andrew Trick3ec331e2011-08-10 03:46:27 +000019#include "llvm/Analysis/LoopInfo.h"
Hongbin Zhengd36f20302017-10-12 02:54:11 +000020#include "llvm/Analysis/ScalarEvolutionExpander.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000022#include "llvm/IR/Dominators.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000023#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Instructions.h"
David Greenb26a0a42017-07-05 13:25:58 +000025#include "llvm/IR/PatternMatch.h"
Andrew Trick3ec331e2011-08-10 03:46:27 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
Max Kazantsev0ed79622018-06-13 02:25:32 +000028#include "llvm/Transforms/Utils/Local.h"
Andrew Trick3ec331e2011-08-10 03:46:27 +000029
30using namespace llvm;
31
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "indvars"
33
Andrew Trick3ec331e2011-08-10 03:46:27 +000034STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
35STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +000036STATISTIC(NumFoldedUser, "Number of IV users folded into a constant");
Andrew Trick3ec331e2011-08-10 03:46:27 +000037STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
Hongbin Zhengbfd7c382017-03-30 21:56:56 +000038STATISTIC(
39 NumSimplifiedSDiv,
40 "Number of IV signed division operations converted to unsigned division");
Hongbin Zhengf0093e42017-09-25 17:39:40 +000041STATISTIC(
42 NumSimplifiedSRem,
43 "Number of IV signed remainder operations converted to unsigned remainder");
Andrew Trick3ec331e2011-08-10 03:46:27 +000044STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
45
46namespace {
Sanjay Patel7777b502014-11-12 18:07:42 +000047 /// This is a utility for simplifying induction variables
Andrew Trick3ec331e2011-08-10 03:46:27 +000048 /// based on ScalarEvolution. It is the primary instrument of the
49 /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
50 /// other loop passes that preserve SCEV.
51 class SimplifyIndvar {
52 Loop *L;
53 LoopInfo *LI;
Andrew Trick3ec331e2011-08-10 03:46:27 +000054 ScalarEvolution *SE;
Sanjoy Das5c8bead2015-10-06 21:44:49 +000055 DominatorTree *DT;
Hongbin Zhengd36f20302017-10-12 02:54:11 +000056 SCEVExpander &Rewriter;
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000057 SmallVectorImpl<WeakTrackingVH> &DeadInsts;
Andrew Trick3ec331e2011-08-10 03:46:27 +000058
59 bool Changed;
60
61 public:
Sanjoy Das5c8bead2015-10-06 21:44:49 +000062 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
Hongbin Zhengd36f20302017-10-12 02:54:11 +000063 LoopInfo *LI, SCEVExpander &Rewriter,
64 SmallVectorImpl<WeakTrackingVH> &Dead)
65 : L(Loop), LI(LI), SE(SE), DT(DT), Rewriter(Rewriter), DeadInsts(Dead),
66 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 Zhengd36f20302017-10-12 02:54:11 +000080 bool replaceIVUserWithLoopInvariant(Instruction *UseInst);
Sanjoy Das088bb0e2015-10-06 21:44:39 +000081
Sanjoy Dasae09b3c2016-05-29 00:36:25 +000082 bool eliminateOverflowIntrinsic(CallInst *CI);
Max Kazantsev37da4332018-06-19 04:48:34 +000083 bool eliminateTrunc(TruncInst *TI);
Andrew Trick3ec331e2011-08-10 03:46:27 +000084 bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
Philip Reames7b861f02017-11-01 19:49:20 +000085 bool makeIVComparisonInvariant(ICmpInst *ICmp, Value *IVOperand);
Andrew Trick3ec331e2011-08-10 03:46:27 +000086 void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
Hongbin Zhengf0093e42017-09-25 17:39:40 +000087 void simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
88 bool IsSigned);
89 void replaceRemWithNumerator(BinaryOperator *Rem);
90 void replaceRemWithNumeratorOrZero(BinaryOperator *Rem);
91 void replaceSRemWithURem(BinaryOperator *Rem);
Hongbin Zhengbfd7c382017-03-30 21:56:56 +000092 bool eliminateSDiv(BinaryOperator *SDiv);
Sanjoy Das7c0ce262015-01-06 19:02:56 +000093 bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
David Greenb26a0a42017-07-05 13:25:58 +000094 bool strengthenRightShift(BinaryOperator *BO, Value *IVOperand);
Andrew Trick3ec331e2011-08-10 03:46:27 +000095 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000096}
Andrew Trick3ec331e2011-08-10 03:46:27 +000097
Sanjay Patel7777b502014-11-12 18:07:42 +000098/// Fold an IV operand into its use. This removes increments of an
Andrew Trick3ec331e2011-08-10 03:46:27 +000099/// aligned IV when used by a instruction that ignores the low bits.
Andrew Trick74664d52011-08-10 04:01:31 +0000100///
Andrew Trick7251e412011-09-19 17:54:39 +0000101/// IVOperand is guaranteed SCEVable, but UseInst may not be.
102///
Andrew Trick74664d52011-08-10 04:01:31 +0000103/// Return the operand of IVOperand for this induction variable if IVOperand can
Andrew Trick6dbb0602011-08-10 18:07:05 +0000104/// be folded (in case more folding opportunities have been exposed).
Andrew Trick74664d52011-08-10 04:01:31 +0000105/// Otherwise return null.
106Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
Craig Topperf40110f2014-04-25 05:29:35 +0000107 Value *IVSrc = nullptr;
Max Kazantsev1d893bf2018-10-10 04:19:38 +0000108 const unsigned OperIdx = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000109 const SCEV *FoldedExpr = nullptr;
Max Kazantsevb2e51092018-10-11 07:22:26 +0000110 bool MustDropExactFlag = false;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000111 switch (UseInst->getOpcode()) {
112 default:
Craig Topperf40110f2014-04-25 05:29:35 +0000113 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000114 case Instruction::UDiv:
115 case Instruction::LShr:
116 // We're only interested in the case where we know something about
117 // the numerator and have a constant denominator.
118 if (IVOperand != UseInst->getOperand(OperIdx) ||
119 !isa<ConstantInt>(UseInst->getOperand(1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000120 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000121
122 // Attempt to fold a binary operator with constant operand.
123 // e.g. ((I + 1) >> 2) => I >> 2
Andrew Trick94904582011-11-17 23:36:35 +0000124 if (!isa<BinaryOperator>(IVOperand)
125 || !isa<ConstantInt>(IVOperand->getOperand(1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000126 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000127
128 IVSrc = IVOperand->getOperand(0);
129 // IVSrc must be the (SCEVable) IV, since the other operand is const.
130 assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
131
132 ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
133 if (UseInst->getOpcode() == Instruction::LShr) {
134 // Get a constant for the divisor. See createSCEV.
135 uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
136 if (D->getValue().uge(BitWidth))
Craig Topperf40110f2014-04-25 05:29:35 +0000137 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000138
139 D = ConstantInt::get(UseInst->getContext(),
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +0000140 APInt::getOneBitSet(BitWidth, D->getZExtValue()));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000141 }
142 FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
Max Kazantsevb2e51092018-10-11 07:22:26 +0000143 // We might have 'exact' flag set at this point which will no longer be
144 // correct after we make the replacement.
145 if (UseInst->isExact() &&
146 SE->getSCEV(IVSrc) != SE->getMulExpr(FoldedExpr, SE->getSCEV(D)))
147 MustDropExactFlag = true;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000148 }
149 // We have something that might fold it's operand. Compare SCEVs.
150 if (!SE->isSCEVable(UseInst->getType()))
Craig Topperf40110f2014-04-25 05:29:35 +0000151 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000152
153 // Bypass the operand if SCEV can prove it has no effect.
154 if (SE->getSCEV(UseInst) != FoldedExpr)
Craig Topperf40110f2014-04-25 05:29:35 +0000155 return nullptr;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000156
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000157 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
158 << " -> " << *UseInst << '\n');
Andrew Trick3ec331e2011-08-10 03:46:27 +0000159
160 UseInst->setOperand(OperIdx, IVSrc);
161 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
162
Max Kazantsevb2e51092018-10-11 07:22:26 +0000163 if (MustDropExactFlag)
164 UseInst->dropPoisonGeneratingFlags();
165
Andrew Trick3ec331e2011-08-10 03:46:27 +0000166 ++NumElimOperand;
167 Changed = true;
168 if (IVOperand->use_empty())
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000169 DeadInsts.emplace_back(IVOperand);
Andrew Trick74664d52011-08-10 04:01:31 +0000170 return IVSrc;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000171}
172
Philip Reames7b861f02017-11-01 19:49:20 +0000173bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
174 Value *IVOperand) {
175 unsigned IVOperIdx = 0;
176 ICmpInst::Predicate Pred = ICmp->getPredicate();
177 if (IVOperand != ICmp->getOperand(0)) {
178 // Swapped
179 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
180 IVOperIdx = 1;
181 Pred = ICmpInst::getSwappedPredicate(Pred);
182 }
Philip Reamesdc417a92017-10-31 18:04:57 +0000183
Philip Reames7b861f02017-11-01 19:49:20 +0000184 // Get the SCEVs for the ICmp operands (in the specific context of the
185 // current loop)
186 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
187 const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
188 const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
189
190 ICmpInst::Predicate InvariantPredicate;
Philip Reamesdc417a92017-10-31 18:04:57 +0000191 const SCEV *InvariantLHS, *InvariantRHS;
Philip Reames7b861f02017-11-01 19:49:20 +0000192
193 auto *PN = dyn_cast<PHINode>(IVOperand);
194 if (!PN)
195 return false;
196 if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
Philip Reamesdc417a92017-10-31 18:04:57 +0000197 InvariantLHS, InvariantRHS))
198 return false;
199
200 // Rewrite the comparison to a loop invariant comparison if it can be done
201 // cheaply, where cheaply means "we don't need to emit any new
202 // instructions".
Philip Reamesdc417a92017-10-31 18:04:57 +0000203
Philip Reames7b861f02017-11-01 19:49:20 +0000204 SmallDenseMap<const SCEV*, Value*> CheapExpansions;
205 CheapExpansions[S] = ICmp->getOperand(IVOperIdx);
206 CheapExpansions[X] = ICmp->getOperand(1 - IVOperIdx);
Fangrui Songf78650a2018-07-30 19:41:25 +0000207
Philip Reames7b861f02017-11-01 19:49:20 +0000208 // TODO: Support multiple entry loops? (We currently bail out of these in
209 // the IndVarSimplify pass)
210 if (auto *BB = L->getLoopPredecessor()) {
Philip Reames6260cf72017-12-01 20:57:19 +0000211 const int Idx = PN->getBasicBlockIndex(BB);
212 if (Idx >= 0) {
213 Value *Incoming = PN->getIncomingValue(Idx);
214 const SCEV *IncomingS = SE->getSCEV(Incoming);
215 CheapExpansions[IncomingS] = Incoming;
216 }
Philip Reames7b861f02017-11-01 19:49:20 +0000217 }
218 Value *NewLHS = CheapExpansions[InvariantLHS];
219 Value *NewRHS = CheapExpansions[InvariantRHS];
220
Philip Reames6260cf72017-12-01 20:57:19 +0000221 if (!NewLHS)
222 if (auto *ConstLHS = dyn_cast<SCEVConstant>(InvariantLHS))
223 NewLHS = ConstLHS->getValue();
224 if (!NewRHS)
225 if (auto *ConstRHS = dyn_cast<SCEVConstant>(InvariantRHS))
226 NewRHS = ConstRHS->getValue();
227
Philip Reames7b861f02017-11-01 19:49:20 +0000228 if (!NewLHS || !NewRHS)
229 // We could not find an existing value to replace either LHS or RHS.
230 // Generating new instructions has subtler tradeoffs, so avoid doing that
231 // for now.
232 return false;
233
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000234 LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
Philip Reames7b861f02017-11-01 19:49:20 +0000235 ICmp->setPredicate(InvariantPredicate);
236 ICmp->setOperand(0, NewLHS);
237 ICmp->setOperand(1, NewRHS);
238 return true;
Philip Reamesdc417a92017-10-31 18:04:57 +0000239}
240
Sanjay Patel7777b502014-11-12 18:07:42 +0000241/// SimplifyIVUsers helper for eliminating useless
Andrew Trick3ec331e2011-08-10 03:46:27 +0000242/// comparisons against an induction variable.
243void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
244 unsigned IVOperIdx = 0;
245 ICmpInst::Predicate Pred = ICmp->getPredicate();
Max Kazantsevb9edcbc2017-07-08 17:17:30 +0000246 ICmpInst::Predicate OriginalPred = Pred;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000247 if (IVOperand != ICmp->getOperand(0)) {
248 // Swapped
249 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
250 IVOperIdx = 1;
251 Pred = ICmpInst::getSwappedPredicate(Pred);
252 }
253
Philip Reames29dd40b2017-10-26 22:02:16 +0000254 // Get the SCEVs for the ICmp operands (in the specific context of the
255 // current loop)
Andrew Trick3ec331e2011-08-10 03:46:27 +0000256 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
Philip Reames29dd40b2017-10-26 22:02:16 +0000257 const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
258 const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000259
260 // If the condition is always true or always false, replace it with
261 // a constant value.
Sanjoy Das5dab2052015-07-27 21:42:49 +0000262 if (SE->isKnownPredicate(Pred, S, X)) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000263 ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000264 DeadInsts.emplace_back(ICmp);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000265 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
Sanjoy Das5dab2052015-07-27 21:42:49 +0000266 } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000267 ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000268 DeadInsts.emplace_back(ICmp);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000269 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
Philip Reames7b861f02017-11-01 19:49:20 +0000270 } else if (makeIVComparisonInvariant(ICmp, IVOperand)) {
271 // fallthrough to end of function
Max Kazantsevb9edcbc2017-07-08 17:17:30 +0000272 } else if (ICmpInst::isSigned(OriginalPred) &&
273 SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) {
274 // If we were unable to make anything above, all we can is to canonicalize
275 // the comparison hoping that it will open the doors for other
276 // optimizations. If we find out that we compare two non-negative values,
277 // we turn the instruction's predicate to its unsigned version. Note that
278 // we cannot rely on Pred here unless we check if we have swapped it.
279 assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000280 LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp
281 << '\n');
Max Kazantsevb9edcbc2017-07-08 17:17:30 +0000282 ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
Sanjoy Das5dab2052015-07-27 21:42:49 +0000283 } else
Andrew Trick3ec331e2011-08-10 03:46:27 +0000284 return;
285
Andrew Trick3ec331e2011-08-10 03:46:27 +0000286 ++NumElimCmp;
287 Changed = true;
Andrew Trick3ec331e2011-08-10 03:46:27 +0000288}
289
Hongbin Zhengbfd7c382017-03-30 21:56:56 +0000290bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
291 // Get the SCEVs for the ICmp operands.
292 auto *N = SE->getSCEV(SDiv->getOperand(0));
293 auto *D = SE->getSCEV(SDiv->getOperand(1));
294
295 // Simplify unnecessary loops away.
296 const Loop *L = LI->getLoopFor(SDiv->getParent());
297 N = SE->getSCEVAtScope(N, L);
298 D = SE->getSCEVAtScope(D, L);
299
300 // Replace sdiv by udiv if both of the operands are non-negative
301 if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
302 auto *UDiv = BinaryOperator::Create(
303 BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
304 SDiv->getName() + ".udiv", SDiv);
305 UDiv->setIsExact(SDiv->isExact());
306 SDiv->replaceAllUsesWith(UDiv);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000307 LLVM_DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
Hongbin Zhengbfd7c382017-03-30 21:56:56 +0000308 ++NumSimplifiedSDiv;
309 Changed = true;
310 DeadInsts.push_back(SDiv);
311 return true;
312 }
313
314 return false;
315}
316
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000317// i %s n -> i %u n if i >= 0 and n >= 0
318void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) {
319 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
320 auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D,
321 Rem->getName() + ".urem", Rem);
322 Rem->replaceAllUsesWith(URem);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000323 LLVM_DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n');
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000324 ++NumSimplifiedSRem;
Hongbin Zhengbbe448a2017-09-25 18:10:36 +0000325 Changed = true;
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000326 DeadInsts.emplace_back(Rem);
327}
Andrew Trick3ec331e2011-08-10 03:46:27 +0000328
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000329// i % n --> i if i is in [0,n).
330void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) {
331 Rem->replaceAllUsesWith(Rem->getOperand(0));
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000332 LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
Andrew Trick3ec331e2011-08-10 03:46:27 +0000333 ++NumElimRem;
334 Changed = true;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000335 DeadInsts.emplace_back(Rem);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000336}
337
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000338// (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
339void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) {
340 auto *T = Rem->getType();
341 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
342 ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D);
343 SelectInst *Sel =
344 SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem);
345 Rem->replaceAllUsesWith(Sel);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000346 LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000347 ++NumElimRem;
348 Changed = true;
349 DeadInsts.emplace_back(Rem);
350}
351
352/// SimplifyIVUsers helper for eliminating useless remainder operations
353/// operating on an induction variable or replacing srem by urem.
354void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
355 bool IsSigned) {
356 auto *NValue = Rem->getOperand(0);
357 auto *DValue = Rem->getOperand(1);
358 // We're only interested in the case where we know something about
359 // the numerator, unless it is a srem, because we want to replace srem by urem
360 // in general.
361 bool UsedAsNumerator = IVOperand == NValue;
362 if (!UsedAsNumerator && !IsSigned)
363 return;
364
365 const SCEV *N = SE->getSCEV(NValue);
366
367 // Simplify unnecessary loops away.
368 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
369 N = SE->getSCEVAtScope(N, ICmpLoop);
370
371 bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
372
373 // Do not proceed if the Numerator may be negative
374 if (!IsNumeratorNonNegative)
375 return;
376
377 const SCEV *D = SE->getSCEV(DValue);
378 D = SE->getSCEVAtScope(D, ICmpLoop);
379
380 if (UsedAsNumerator) {
381 auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
382 if (SE->isKnownPredicate(LT, N, D)) {
383 replaceRemWithNumerator(Rem);
384 return;
385 }
386
387 auto *T = Rem->getType();
388 const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
389 if (SE->isKnownPredicate(LT, NLessOne, D)) {
390 replaceRemWithNumeratorOrZero(Rem);
391 return;
392 }
393 }
394
395 // Try to replace SRem with URem, if both N and D are known non-negative.
396 // Since we had already check N, we only need to check D now
397 if (!IsSigned || !SE->isKnownNonNegative(D))
398 return;
399
400 replaceSRemWithURem(Rem);
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000401}
402
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000403bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) {
404 auto *F = CI->getCalledFunction();
405 if (!F)
406 return false;
407
408 typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)(
Max Kazantsevdc803662017-06-15 11:48:21 +0000409 const SCEV *, const SCEV *, SCEV::NoWrapFlags, unsigned);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000410 typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)(
Max Kazantsev8d0322e2017-06-30 05:04:09 +0000411 const SCEV *, Type *, unsigned);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000412
413 OperationFunctionTy Operation;
414 ExtensionFunctionTy Extension;
415
416 Instruction::BinaryOps RawOp;
417
418 // We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we
419 // have nuw.
420 bool NoSignedOverflow;
421
422 switch (F->getIntrinsicID()) {
423 default:
424 return false;
425
426 case Intrinsic::sadd_with_overflow:
427 Operation = &ScalarEvolution::getAddExpr;
428 Extension = &ScalarEvolution::getSignExtendExpr;
429 RawOp = Instruction::Add;
430 NoSignedOverflow = true;
431 break;
432
433 case Intrinsic::uadd_with_overflow:
434 Operation = &ScalarEvolution::getAddExpr;
435 Extension = &ScalarEvolution::getZeroExtendExpr;
436 RawOp = Instruction::Add;
437 NoSignedOverflow = false;
438 break;
439
440 case Intrinsic::ssub_with_overflow:
441 Operation = &ScalarEvolution::getMinusSCEV;
442 Extension = &ScalarEvolution::getSignExtendExpr;
443 RawOp = Instruction::Sub;
444 NoSignedOverflow = true;
445 break;
446
447 case Intrinsic::usub_with_overflow:
448 Operation = &ScalarEvolution::getMinusSCEV;
449 Extension = &ScalarEvolution::getZeroExtendExpr;
450 RawOp = Instruction::Sub;
451 NoSignedOverflow = false;
452 break;
453 }
454
455 const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0));
456 const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1));
457
458 auto *NarrowTy = cast<IntegerType>(LHS->getType());
459 auto *WideTy =
460 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
461
462 const SCEV *A =
Max Kazantsev8d0322e2017-06-30 05:04:09 +0000463 (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0),
464 WideTy, 0);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000465 const SCEV *B =
Max Kazantsev8d0322e2017-06-30 05:04:09 +0000466 (SE->*Operation)((SE->*Extension)(LHS, WideTy, 0),
467 (SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0);
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000468
469 if (A != B)
470 return false;
471
472 // Proved no overflow, nuke the overflow check and, if possible, the overflow
473 // intrinsic as well.
474
475 BinaryOperator *NewResult = BinaryOperator::Create(
476 RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI);
477
478 if (NoSignedOverflow)
479 NewResult->setHasNoSignedWrap(true);
480 else
481 NewResult->setHasNoUnsignedWrap(true);
482
483 SmallVector<ExtractValueInst *, 4> ToDelete;
484
485 for (auto *U : CI->users()) {
486 if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
487 if (EVI->getIndices()[0] == 1)
488 EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext()));
489 else {
490 assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
491 EVI->replaceAllUsesWith(NewResult);
492 }
493 ToDelete.push_back(EVI);
494 }
495 }
496
497 for (auto *EVI : ToDelete)
498 EVI->eraseFromParent();
499
500 if (CI->use_empty())
501 CI->eraseFromParent();
502
503 return true;
504}
505
Max Kazantsev37da4332018-06-19 04:48:34 +0000506bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) {
507 // It is always legal to replace
508 // icmp <pred> i32 trunc(iv), n
509 // with
510 // icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate.
511 // Or with
512 // icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate.
513 // Or with either of these if pred is an equality predicate.
514 //
515 // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for
516 // every comparison which uses trunc, it means that we can replace each of
517 // them with comparison of iv against sext/zext(n). We no longer need trunc
518 // after that.
519 //
520 // TODO: Should we do this if we can widen *some* comparisons, but not all
521 // of them? Sometimes it is enough to enable other optimizations, but the
522 // trunc instruction will stay in the loop.
523 Value *IV = TI->getOperand(0);
524 Type *IVTy = IV->getType();
525 const SCEV *IVSCEV = SE->getSCEV(IV);
526 const SCEV *TISCEV = SE->getSCEV(TI);
527
528 // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can
529 // get rid of trunc
530 bool DoesSExtCollapse = false;
531 bool DoesZExtCollapse = false;
532 if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy))
533 DoesSExtCollapse = true;
534 if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy))
535 DoesZExtCollapse = true;
536
537 // If neither sext nor zext does collapse, it is not profitable to do any
538 // transform. Bail.
539 if (!DoesSExtCollapse && !DoesZExtCollapse)
540 return false;
541
542 // Collect users of the trunc that look like comparisons against invariants.
543 // Bail if we find something different.
544 SmallVector<ICmpInst *, 4> ICmpUsers;
545 for (auto *U : TI->users()) {
Max Kazantsevf5ba3712018-06-28 08:20:03 +0000546 // We don't care about users in unreachable blocks.
547 if (isa<Instruction>(U) &&
548 !DT->isReachableFromEntry(cast<Instruction>(U)->getParent()))
549 continue;
Max Kazantsev37da4332018-06-19 04:48:34 +0000550 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) {
551 if (ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) {
552 assert(L->contains(ICI->getParent()) && "LCSSA form broken?");
553 // If we cannot get rid of trunc, bail.
554 if (ICI->isSigned() && !DoesSExtCollapse)
555 return false;
556 if (ICI->isUnsigned() && !DoesZExtCollapse)
557 return false;
558 // For equality, either signed or unsigned works.
559 ICmpUsers.push_back(ICI);
560 } else
561 return false;
562 } else
563 return false;
564 }
565
Max Kazantsev4d980512018-07-27 09:43:39 +0000566 auto CanUseZExt = [&](ICmpInst *ICI) {
567 // Unsigned comparison can be widened as unsigned.
568 if (ICI->isUnsigned())
569 return true;
570 // Is it profitable to do zext?
571 if (!DoesZExtCollapse)
572 return false;
573 // For equality, we can safely zext both parts.
574 if (ICI->isEquality())
575 return true;
576 // Otherwise we can only use zext when comparing two non-negative or two
577 // negative values. But in practice, we will never pass DoesZExtCollapse
578 // check for a negative value, because zext(trunc(x)) is non-negative. So
579 // it only make sense to check for non-negativity here.
580 const SCEV *SCEVOP1 = SE->getSCEV(ICI->getOperand(0));
581 const SCEV *SCEVOP2 = SE->getSCEV(ICI->getOperand(1));
582 return SE->isKnownNonNegative(SCEVOP1) && SE->isKnownNonNegative(SCEVOP2);
583 };
Max Kazantsev37da4332018-06-19 04:48:34 +0000584 // Replace all comparisons against trunc with comparisons against IV.
585 for (auto *ICI : ICmpUsers) {
586 auto *Op1 = ICI->getOperand(1);
587 Instruction *Ext = nullptr;
588 // For signed/unsigned predicate, replace the old comparison with comparison
589 // of immediate IV against sext/zext of the invariant argument. If we can
590 // use either sext or zext (i.e. we are dealing with equality predicate),
591 // then prefer zext as a more canonical form.
592 // TODO: If we see a signed comparison which can be turned into unsigned,
593 // we can do it here for canonicalization purposes.
Max Kazantsev4d980512018-07-27 09:43:39 +0000594 ICmpInst::Predicate Pred = ICI->getPredicate();
595 if (CanUseZExt(ICI)) {
Max Kazantsev37da4332018-06-19 04:48:34 +0000596 assert(DoesZExtCollapse && "Unprofitable zext?");
597 Ext = new ZExtInst(Op1, IVTy, "zext", ICI);
Max Kazantsev4d980512018-07-27 09:43:39 +0000598 Pred = ICmpInst::getUnsignedPredicate(Pred);
Max Kazantsev37da4332018-06-19 04:48:34 +0000599 } else {
600 assert(DoesSExtCollapse && "Unprofitable sext?");
601 Ext = new SExtInst(Op1, IVTy, "sext", ICI);
Max Kazantsev4d980512018-07-27 09:43:39 +0000602 assert(Pred == ICmpInst::getSignedPredicate(Pred) && "Must be signed!");
Max Kazantsev37da4332018-06-19 04:48:34 +0000603 }
604 bool Changed;
605 L->makeLoopInvariant(Ext, Changed);
606 (void)Changed;
Max Kazantsev4d980512018-07-27 09:43:39 +0000607 ICmpInst *NewICI = new ICmpInst(ICI, Pred, IV, Ext);
Max Kazantsev37da4332018-06-19 04:48:34 +0000608 ICI->replaceAllUsesWith(NewICI);
609 DeadInsts.emplace_back(ICI);
610 }
611
612 // Trunc no longer needed.
613 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
614 DeadInsts.emplace_back(TI);
615 return true;
616}
617
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000618/// Eliminate an operation that consumes a simple IV and has no observable
619/// side-effect given the range of IV values. IVOperand is guaranteed SCEVable,
620/// but UseInst may not be.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000621bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
622 Instruction *IVOperand) {
623 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
624 eliminateIVComparison(ICmp, IVOperand);
625 return true;
626 }
Hongbin Zhengbfd7c382017-03-30 21:56:56 +0000627 if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
628 bool IsSRem = Bin->getOpcode() == Instruction::SRem;
629 if (IsSRem || Bin->getOpcode() == Instruction::URem) {
Hongbin Zhengf0093e42017-09-25 17:39:40 +0000630 simplifyIVRemainder(Bin, IVOperand, IsSRem);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000631 return true;
632 }
Hongbin Zhengbfd7c382017-03-30 21:56:56 +0000633
634 if (Bin->getOpcode() == Instruction::SDiv)
635 return eliminateSDiv(Bin);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000636 }
637
Sanjoy Dasae09b3c2016-05-29 00:36:25 +0000638 if (auto *CI = dyn_cast<CallInst>(UseInst))
639 if (eliminateOverflowIntrinsic(CI))
640 return true;
641
Max Kazantsev37da4332018-06-19 04:48:34 +0000642 if (auto *TI = dyn_cast<TruncInst>(UseInst))
643 if (eliminateTrunc(TI))
644 return true;
645
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000646 if (eliminateIdentitySCEV(UseInst, IVOperand))
647 return true;
648
649 return false;
650}
651
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000652static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) {
653 if (auto *BB = L->getLoopPreheader())
654 return BB->getTerminator();
655
656 return Hint;
657}
658
659/// Replace the UseInst with a constant if possible.
660bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) {
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +0000661 if (!SE->isSCEVable(I->getType()))
662 return false;
663
664 // Get the symbolic expression for this instruction.
665 const SCEV *S = SE->getSCEV(I);
666
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000667 if (!SE->isLoopInvariant(S, L))
Hongbin Zhengc8abdf52017-09-29 16:32:12 +0000668 return false;
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +0000669
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000670 // Do not generate something ridiculous even if S is loop invariant.
671 if (Rewriter.isHighCostExpansion(S, L, I))
Hongbin Zhengc8abdf52017-09-29 16:32:12 +0000672 return false;
673
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000674 auto *IP = GetLoopInvariantInsertPosition(L, I);
675 auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
676
677 I->replaceAllUsesWith(Invariant);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000678 LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
679 << " with loop invariant: " << *S << '\n');
Hongbin Zhengc8abdf52017-09-29 16:32:12 +0000680 ++NumFoldedUser;
681 Changed = true;
682 DeadInsts.emplace_back(I);
683 return true;
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +0000684}
685
Sanjoy Das088bb0e2015-10-06 21:44:39 +0000686/// Eliminate any operation that SCEV can prove is an identity function.
687bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
688 Instruction *IVOperand) {
Andrew Trick3ec331e2011-08-10 03:46:27 +0000689 if (!SE->isSCEVable(UseInst->getType()) ||
690 (UseInst->getType() != IVOperand->getType()) ||
691 (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
692 return false;
693
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000694 // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
695 // dominator tree, even if X is an operand to Y. For instance, in
696 //
697 // %iv = phi i32 {0,+,1}
698 // br %cond, label %left, label %merge
699 //
700 // left:
701 // %X = add i32 %iv, 0
702 // br label %merge
703 //
704 // merge:
705 // %M = phi (%X, %iv)
706 //
707 // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
708 // %M.replaceAllUsesWith(%X) would be incorrect.
709
710 if (isa<PHINode>(UseInst))
711 // If UseInst is not a PHI node then we know that IVOperand dominates
712 // UseInst directly from the legality of SSA.
713 if (!DT || !DT->dominates(IVOperand, UseInst))
714 return false;
715
Sanjoy Das0015e5a2015-10-07 17:38:31 +0000716 if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
717 return false;
718
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000719 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
Andrew Trick3ec331e2011-08-10 03:46:27 +0000720
721 UseInst->replaceAllUsesWith(IVOperand);
722 ++NumElimIdentity;
723 Changed = true;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000724 DeadInsts.emplace_back(UseInst);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000725 return true;
726}
727
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000728/// Annotate BO with nsw / nuw if it provably does not signed-overflow /
729/// unsigned-overflow. Returns true if anything changed, false otherwise.
730bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
731 Value *IVOperand) {
732
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000733 // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000734 if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
735 return false;
736
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000737 const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
Max Kazantsevdc803662017-06-15 11:48:21 +0000738 SCEV::NoWrapFlags, unsigned);
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000739 switch (BO->getOpcode()) {
740 default:
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000741 return false;
742
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000743 case Instruction::Add:
744 GetExprForBO = &ScalarEvolution::getAddExpr;
745 break;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000746
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000747 case Instruction::Sub:
748 GetExprForBO = &ScalarEvolution::getMinusSCEV;
749 break;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000750
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000751 case Instruction::Mul:
752 GetExprForBO = &ScalarEvolution::getMulExpr;
753 break;
754 }
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000755
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000756 unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
757 Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
758 const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
759 const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000760
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000761 bool Changed = false;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000762
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000763 if (!BO->hasNoUnsignedWrap()) {
764 const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
765 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
766 SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
Max Kazantsevdc803662017-06-15 11:48:21 +0000767 SCEV::FlagAnyWrap, 0u);
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000768 if (ExtendAfterOp == OpAfterExtend) {
769 BO->setHasNoUnsignedWrap();
770 SE->forgetValue(BO);
771 Changed = true;
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000772 }
773 }
774
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000775 if (!BO->hasNoSignedWrap()) {
776 const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
777 const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
778 SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
Max Kazantsevdc803662017-06-15 11:48:21 +0000779 SCEV::FlagAnyWrap, 0u);
Sanjoy Dasa5397c02015-03-04 22:24:23 +0000780 if (ExtendAfterOp == OpAfterExtend) {
781 BO->setHasNoSignedWrap();
782 SE->forgetValue(BO);
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000783 Changed = true;
784 }
785 }
786
787 return Changed;
788}
789
David Greenb26a0a42017-07-05 13:25:58 +0000790/// Annotate the Shr in (X << IVOperand) >> C as exact using the
791/// information from the IV's range. Returns true if anything changed, false
792/// otherwise.
793bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
794 Value *IVOperand) {
795 using namespace llvm::PatternMatch;
796
797 if (BO->getOpcode() == Instruction::Shl) {
798 bool Changed = false;
799 ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
800 for (auto *U : BO->users()) {
801 const APInt *C;
802 if (match(U,
803 m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
804 match(U,
805 m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
806 BinaryOperator *Shr = cast<BinaryOperator>(U);
807 if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
808 Shr->setIsExact(true);
809 Changed = true;
810 }
811 }
812 }
813 return Changed;
814 }
815
816 return false;
817}
818
Sanjay Patel7777b502014-11-12 18:07:42 +0000819/// Add all uses of Def to the current IV's worklist.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000820static void pushIVUsers(
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000821 Instruction *Def, Loop *L,
Andrew Trick3ec331e2011-08-10 03:46:27 +0000822 SmallPtrSet<Instruction*,16> &Simplified,
823 SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
824
Chandler Carruthcdf47882014-03-09 03:16:01 +0000825 for (User *U : Def->users()) {
826 Instruction *UI = cast<Instruction>(U);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000827
828 // Avoid infinite or exponential worklist processing.
829 // Also ensure unique worklist users.
830 // If Def is a LoopPhi, it may not be in the Simplified set, so check for
831 // self edges first.
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000832 if (UI == Def)
833 continue;
834
835 // Only change the current Loop, do not change the other parts (e.g. other
836 // Loops).
837 if (!L->contains(UI))
838 continue;
839
840 // Do not push the same instruction more than once.
841 if (!Simplified.insert(UI).second)
842 continue;
843
844 SimpleIVUsers.push_back(std::make_pair(UI, Def));
Andrew Trick3ec331e2011-08-10 03:46:27 +0000845 }
846}
847
Sanjay Patel7777b502014-11-12 18:07:42 +0000848/// Return true if this instruction generates a simple SCEV
Andrew Trick3ec331e2011-08-10 03:46:27 +0000849/// expression in terms of that IV.
850///
Andrew Trick6dbb0602011-08-10 18:07:05 +0000851/// This is similar to IVUsers' isInteresting() but processes each instruction
Andrew Trick3ec331e2011-08-10 03:46:27 +0000852/// non-recursively when the operand is already known to be a simpleIVUser.
853///
854static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
855 if (!SE->isSCEVable(I->getType()))
856 return false;
857
858 // Get the symbolic expression for this instruction.
859 const SCEV *S = SE->getSCEV(I);
860
861 // Only consider affine recurrences.
862 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
863 if (AR && AR->getLoop() == L)
864 return true;
865
866 return false;
867}
868
Sanjay Patel7777b502014-11-12 18:07:42 +0000869/// Iteratively perform simplification on a worklist of users
Andrew Trick3ec331e2011-08-10 03:46:27 +0000870/// of the specified induction variable. Each successive simplification may push
871/// more users which may themselves be candidates for simplification.
872///
873/// This algorithm does not require IVUsers analysis. Instead, it simplifies
874/// instructions in-place during analysis. Rather than rewriting induction
875/// variables bottom-up from their users, it transforms a chain of IVUsers
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000876/// top-down, updating the IR only when it encounters a clear optimization
877/// opportunity.
Andrew Trick3ec331e2011-08-10 03:46:27 +0000878///
879/// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
880///
881void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
Andrew Trick7251e412011-09-19 17:54:39 +0000882 if (!SE->isSCEVable(CurrIV->getType()))
883 return;
884
Andrew Trick3ec331e2011-08-10 03:46:27 +0000885 // Instructions processed by SimplifyIndvar for CurrIV.
886 SmallPtrSet<Instruction*,16> Simplified;
887
888 // Use-def pairs if IV users waiting to be processed for CurrIV.
889 SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
890
891 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
892 // called multiple times for the same LoopPhi. This is the proper thing to
893 // do for loop header phis that use each other.
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000894 pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000895
896 while (!SimpleIVUsers.empty()) {
897 std::pair<Instruction*, Instruction*> UseOper =
898 SimpleIVUsers.pop_back_val();
Andrew Trick0ba77a02013-12-23 23:31:49 +0000899 Instruction *UseInst = UseOper.first;
900
Max Kazantsev0ed79622018-06-13 02:25:32 +0000901 // If a user of the IndVar is trivially dead, we prefer just to mark it dead
902 // rather than try to do some complex analysis or transformation (such as
903 // widening) basing on it.
904 // TODO: Propagate TLI and pass it here to handle more cases.
905 if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) {
906 DeadInsts.emplace_back(UseInst);
907 continue;
908 }
909
Andrew Trick3ec331e2011-08-10 03:46:27 +0000910 // Bypass back edges to avoid extra work.
Andrew Trick0ba77a02013-12-23 23:31:49 +0000911 if (UseInst == CurrIV) continue;
912
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000913 // Try to replace UseInst with a loop invariant before any other
914 // simplifications.
915 if (replaceIVUserWithLoopInvariant(UseInst))
Hongbin Zhengd1b7b2e2017-09-27 03:11:46 +0000916 continue;
917
Andrew Trick74664d52011-08-10 04:01:31 +0000918 Instruction *IVOperand = UseOper.second;
919 for (unsigned N = 0; IVOperand; ++N) {
920 assert(N <= Simplified.size() && "runaway iteration");
Andrew Trick3ec331e2011-08-10 03:46:27 +0000921
Max Kazantsevb4b2cce2018-06-07 08:47:19 +0000922 Value *NewOper = foldIVUser(UseInst, IVOperand);
Andrew Trick74664d52011-08-10 04:01:31 +0000923 if (!NewOper)
924 break; // done folding
925 IVOperand = dyn_cast<Instruction>(NewOper);
926 }
927 if (!IVOperand)
928 continue;
929
Max Kazantsevb4b2cce2018-06-07 08:47:19 +0000930 if (eliminateIVUser(UseInst, IVOperand)) {
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000931 pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000932 continue;
933 }
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000934
Max Kazantsevb4b2cce2018-06-07 08:47:19 +0000935 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) {
David Greenb26a0a42017-07-05 13:25:58 +0000936 if ((isa<OverflowingBinaryOperator>(BO) &&
937 strengthenOverflowingOperation(BO, IVOperand)) ||
938 (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) {
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000939 // re-queue uses of the now modified binary operator and fall
940 // through to the checks that remain.
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000941 pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
Sanjoy Das7c0ce262015-01-06 19:02:56 +0000942 }
943 }
944
Max Kazantsevb4b2cce2018-06-07 08:47:19 +0000945 CastInst *Cast = dyn_cast<CastInst>(UseInst);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000946 if (V && Cast) {
947 V->visitCast(Cast);
948 continue;
949 }
Max Kazantsevb4b2cce2018-06-07 08:47:19 +0000950 if (isSimpleIVUser(UseInst, L, SE)) {
951 pushIVUsers(UseInst, L, Simplified, SimpleIVUsers);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000952 }
953 }
954}
955
956namespace llvm {
957
David Blaikiea379b1812011-12-20 02:50:00 +0000958void IVVisitor::anchor() { }
959
Sanjay Patel7777b502014-11-12 18:07:42 +0000960/// Simplify instructions that use this induction variable
Andrew Trick3ec331e2011-08-10 03:46:27 +0000961/// by using ScalarEvolution to analyze the IV's recurrence.
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000962bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000963 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead,
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000964 SCEVExpander &Rewriter, IVVisitor *V) {
965 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Rewriter,
966 Dead);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000967 SIV.simplifyUsers(CurrIV, V);
968 return SIV.hasChanged();
969}
970
Sanjay Patel7777b502014-11-12 18:07:42 +0000971/// Simplify users of induction variables within this
Andrew Trick3ec331e2011-08-10 03:46:27 +0000972/// loop. This does not actually change or add IVs.
Sanjoy Das5c8bead2015-10-06 21:44:49 +0000973bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000974 LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) {
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000975 SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars");
976#ifndef NDEBUG
977 Rewriter.setDebugType(DEBUG_TYPE);
978#endif
Andrew Trick3ec331e2011-08-10 03:46:27 +0000979 bool Changed = false;
980 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
Hongbin Zhengd36f20302017-10-12 02:54:11 +0000981 Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead, Rewriter);
Andrew Trick3ec331e2011-08-10 03:46:27 +0000982 }
983 return Changed;
984}
985
Andrew Trick3ec331e2011-08-10 03:46:27 +0000986} // namespace llvm