LLVM 24.0.0git
SimplifyIndVar.cpp
Go to the documentation of this file.
1//===-- SimplifyIndVar.cpp - Induction variable simplification ------------===//
2//
3// 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
6//
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
17#include "llvm/ADT/Statistic.h"
20#include "llvm/IR/Dominators.h"
21#include "llvm/IR/IRBuilder.h"
25#include "llvm/Support/Debug.h"
30
31using namespace llvm;
32using namespace llvm::PatternMatch;
33
34#define DEBUG_TYPE "indvars"
35
36STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
37STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
38STATISTIC(NumFoldedUser, "Number of IV users folded into a constant");
39STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
41 NumSimplifiedSDiv,
42 "Number of IV signed division operations converted to unsigned division");
44 NumSimplifiedSRem,
45 "Number of IV signed remainder operations converted to unsigned remainder");
46STATISTIC(NumElimCmp, "Number of IV comparisons eliminated");
47STATISTIC(NumInvariantCmp, "Number of IV comparisons made loop invariant");
48STATISTIC(NumSameSign, "Number of IV comparisons with new samesign flags");
49
50namespace {
51 /// This is a utility for simplifying induction variables
52 /// based on ScalarEvolution. It is the primary instrument of the
53 /// IndvarSimplify pass, but it may also be directly invoked to cleanup after
54 /// other loop passes that preserve SCEV.
55 class SimplifyIndvar {
56 Loop *L;
57 LoopInfo *LI;
59 DominatorTree *DT;
61 SCEVExpander &Rewriter;
63
64 bool Changed = false;
65 bool RunUnswitching = false;
66
67 public:
68 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
70 SCEVExpander &Rewriter,
72 : L(Loop), LI(LI), SE(SE), DT(DT), TTI(TTI), Rewriter(Rewriter),
73 DeadInsts(Dead) {
74 assert(LI && "IV simplification requires LoopInfo");
75 }
76
77 bool hasChanged() const { return Changed; }
78 bool runUnswitching() const { return RunUnswitching; }
79
80 /// Iteratively perform simplification on a worklist of users of the
81 /// specified induction variable. This is the top-level driver that applies
82 /// all simplifications to users of an IV.
83 void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
84
85 void pushIVUsers(Instruction *Def,
86 SmallPtrSet<Instruction *, 16> &Simplified,
87 SmallVectorImpl<std::pair<Instruction *, Instruction *>>
88 &SimpleIVUsers);
89
90 Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
91
92 bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand);
93 bool replaceIVUserWithLoopInvariant(Instruction *UseInst);
94 bool replaceFloatIVWithIntegerIV(Instruction *UseInst);
95
96 bool eliminateOverflowIntrinsic(WithOverflowInst *WO);
97 bool eliminateSaturatingIntrinsic(SaturatingInst *SI);
98 bool eliminateTrunc(TruncInst *TI);
99 bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
100 bool makeIVComparisonInvariant(ICmpInst *ICmp, Instruction *IVOperand);
101 void eliminateIVComparison(ICmpInst *ICmp, Instruction *IVOperand);
102 void simplifyIVRemainder(BinaryOperator *Rem, Instruction *IVOperand,
103 bool IsSigned);
104 void replaceRemWithNumerator(BinaryOperator *Rem);
105 void replaceRemWithNumeratorOrZero(BinaryOperator *Rem);
106 void replaceSRemWithURem(BinaryOperator *Rem);
107 bool eliminateSDiv(BinaryOperator *SDiv);
108 bool strengthenBinaryOp(BinaryOperator *BO, Instruction *IVOperand);
109 bool strengthenOverflowingOperation(BinaryOperator *OBO,
110 Instruction *IVOperand);
111 bool strengthenRightShift(BinaryOperator *BO, Instruction *IVOperand);
112 };
113}
114
115/// Find a point in code which dominates all given instructions. We can safely
116/// assume that, whatever fact we can prove at the found point, this fact is
117/// also true for each of the given instructions.
119 DominatorTree &DT) {
120 Instruction *CommonDom = nullptr;
121 for (auto *Insn : Instructions)
122 CommonDom =
123 CommonDom ? DT.findNearestCommonDominator(CommonDom, Insn) : Insn;
124 assert(CommonDom && "Common dominator not found?");
125 return CommonDom;
126}
127
128/// Fold an IV operand into its use. This removes increments of an
129/// aligned IV when used by a instruction that ignores the low bits.
130///
131/// IVOperand is guaranteed SCEVable, but UseInst may not be.
132///
133/// Return the operand of IVOperand for this induction variable if IVOperand can
134/// be folded (in case more folding opportunities have been exposed).
135/// Otherwise return null.
136Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
137 Value *IVSrc = nullptr;
138 const unsigned OperIdx = 0;
139 const SCEV *FoldedExpr = nullptr;
140 bool MustDropExactFlag = false;
141 switch (UseInst->getOpcode()) {
142 default:
143 return nullptr;
144 case Instruction::UDiv:
145 case Instruction::LShr:
146 // We're only interested in the case where we know something about
147 // the numerator and have a constant denominator.
148 if (IVOperand != UseInst->getOperand(OperIdx) ||
149 !isa<ConstantInt>(UseInst->getOperand(1)))
150 return nullptr;
151
152 // Attempt to fold a binary operator with constant operand.
153 // e.g. ((I + 1) >> 2) => I >> 2
154 if (!isa<BinaryOperator>(IVOperand)
155 || !isa<ConstantInt>(IVOperand->getOperand(1)))
156 return nullptr;
157
158 IVSrc = IVOperand->getOperand(0);
159 // IVSrc must be the (SCEVable) IV, since the other operand is const.
160 assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
161
162 ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
163 if (UseInst->getOpcode() == Instruction::LShr) {
164 // Get a constant for the divisor. See createSCEV.
165 uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
166 if (D->getValue().uge(BitWidth))
167 return nullptr;
168
169 D = ConstantInt::get(UseInst->getContext(),
170 APInt::getOneBitSet(BitWidth, D->getZExtValue()));
171 }
172 const SCEV *LHS = SE->getSCEV(IVSrc);
173 const SCEV *RHS = SE->getSCEV(D);
174 FoldedExpr = SE->getUDivExpr(LHS, RHS);
175 // We might have 'exact' flag set at this point which will no longer be
176 // correct after we make the replacement.
177 if (UseInst->isExact() && LHS != SE->getMulExpr(FoldedExpr, RHS))
178 MustDropExactFlag = true;
179 }
180 // We have something that might fold it's operand. Compare SCEVs.
181 if (!SE->isSCEVable(UseInst->getType()))
182 return nullptr;
183
184 // Bypass the operand if SCEV can prove it has no effect.
185 if (SE->getSCEV(UseInst) != FoldedExpr)
186 return nullptr;
187
188 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
189 << " -> " << *UseInst << '\n');
190
191 UseInst->setOperand(OperIdx, IVSrc);
192 assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
193
194 if (MustDropExactFlag)
195 UseInst->dropPoisonGeneratingFlags();
196
197 ++NumElimOperand;
198 Changed = true;
199 if (IVOperand->use_empty())
200 DeadInsts.emplace_back(IVOperand);
201 return IVSrc;
202}
203
204bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
205 Instruction *IVOperand) {
206 auto *Preheader = L->getLoopPreheader();
207 if (!Preheader)
208 return false;
209 unsigned IVOperIdx = 0;
210 CmpPredicate Pred = ICmp->getCmpPredicate();
211 if (IVOperand != ICmp->getOperand(0)) {
212 // Swapped
213 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
214 IVOperIdx = 1;
216 }
217
218 // Get the SCEVs for the ICmp operands (in the specific context of the
219 // current loop)
220 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
221 const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
222 const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
223 auto LIP = SE->getLoopInvariantPredicate(Pred, S, X, L, ICmp);
224 if (!LIP)
225 return false;
226 ICmpInst::Predicate InvariantPredicate = LIP->Pred;
227 const SCEV *InvariantLHS = LIP->LHS;
228 const SCEV *InvariantRHS = LIP->RHS;
229
230 // Do not generate something ridiculous.
231 auto *PHTerm = Preheader->getTerminator();
232 if (Rewriter.isHighCostExpansion({InvariantLHS, InvariantRHS}, L,
233 2 * SCEVCheapExpansionBudget, TTI, PHTerm) ||
234 !Rewriter.isSafeToExpandAt(InvariantLHS, PHTerm) ||
235 !Rewriter.isSafeToExpandAt(InvariantRHS, PHTerm))
236 return false;
237 auto *NewLHS =
238 Rewriter.expandCodeFor(InvariantLHS, IVOperand->getType(), PHTerm);
239 auto *NewRHS =
240 Rewriter.expandCodeFor(InvariantRHS, IVOperand->getType(), PHTerm);
241 LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
242 ICmp->setPredicate(InvariantPredicate);
243 ICmp->setOperand(0, NewLHS);
244 ICmp->setOperand(1, NewRHS);
245 RunUnswitching = true;
246 return true;
247}
248
249/// SimplifyIVUsers helper for eliminating useless
250/// comparisons against an induction variable.
251void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
252 Instruction *IVOperand) {
253 unsigned IVOperIdx = 0;
254 CmpPredicate Pred = ICmp->getCmpPredicate();
255 ICmpInst::Predicate OriginalPred = Pred;
256 if (IVOperand != ICmp->getOperand(0)) {
257 // Swapped
258 assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
259 IVOperIdx = 1;
261 }
262
263 // Get the SCEVs for the ICmp operands (in the specific context of the
264 // current loop)
265 const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
266 const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
267 const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
268
269 // If the condition is always true or always false in the given context,
270 // replace it with a constant value.
271 SmallVector<Instruction *, 4> Users;
272 for (auto *U : ICmp->users())
273 Users.push_back(cast<Instruction>(U));
274 const Instruction *CtxI = findCommonDominator(Users, *DT);
275 if (auto Ev = SE->evaluatePredicateAt(Pred, S, X, CtxI)) {
276 SE->forgetValue(ICmp);
278 DeadInsts.emplace_back(ICmp);
279 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
280 ++NumElimCmp;
281 Changed = true;
282 return;
283 }
284
285 if (makeIVComparisonInvariant(ICmp, IVOperand)) {
286 ++NumInvariantCmp;
287 Changed = true;
288 return;
289 }
290
291 if ((ICmpInst::isSigned(OriginalPred) ||
292 (ICmpInst::isUnsigned(OriginalPred) && !ICmp->hasSameSign())) &&
293 SE->haveSameSign(S, X)) {
294 // Set the samesign flag on the compare if legal, and canonicalize to
295 // the unsigned variant (for signed compares) hoping that it will open
296 // the doors for other optimizations. Note that we cannot rely on Pred
297 // here unless we check if we have swapped it.
298 assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
299 LLVM_DEBUG(dbgs() << "INDVARS: Marking comparison samesign: " << *ICmp
300 << '\n');
301 ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
302 ICmp->setSameSign();
303 NumSameSign++;
304 Changed = true;
305 return;
306 }
307}
308
309bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
310 // Get the SCEVs for the ICmp operands.
311 const SCEV *N = SE->getSCEV(SDiv->getOperand(0));
312 const SCEV *D = SE->getSCEV(SDiv->getOperand(1));
313
314 // Simplify unnecessary loops away.
315 const Loop *L = LI->getLoopFor(SDiv->getParent());
316 N = SE->getSCEVAtScope(N, L);
317 D = SE->getSCEVAtScope(D, L);
318
319 // Replace sdiv by udiv if both of the operands are non-negative
320 if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
321 auto *UDiv = BinaryOperator::Create(
322 BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
323 SDiv->getName() + ".udiv", SDiv->getIterator());
324 UDiv->setIsExact(SDiv->isExact());
325 SDiv->replaceAllUsesWith(UDiv);
326 UDiv->setDebugLoc(SDiv->getDebugLoc());
327 LLVM_DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
328 ++NumSimplifiedSDiv;
329 Changed = true;
330 DeadInsts.push_back(SDiv);
331 return true;
332 }
333
334 return false;
335}
336
337// i %s n -> i %u n if i >= 0 and n >= 0
338void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) {
339 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
340 auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D,
341 Rem->getName() + ".urem", Rem->getIterator());
342 Rem->replaceAllUsesWith(URem);
343 URem->setDebugLoc(Rem->getDebugLoc());
344 LLVM_DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n');
345 ++NumSimplifiedSRem;
346 Changed = true;
347 DeadInsts.emplace_back(Rem);
348}
349
350// i % n --> i if i is in [0,n).
351void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) {
352 Rem->replaceAllUsesWith(Rem->getOperand(0));
353 LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
354 ++NumElimRem;
355 Changed = true;
356 DeadInsts.emplace_back(Rem);
357}
358
359// (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
360void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) {
361 auto *T = Rem->getType();
362 auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
363 ICmpInst *ICmp = new ICmpInst(Rem->getIterator(), ICmpInst::ICMP_EQ, N, D);
364 ICmp->setDebugLoc(Rem->getDebugLoc());
365 SelectInst *Sel =
366 SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem->getIterator());
367 Rem->replaceAllUsesWith(Sel);
368 Sel->setDebugLoc(Rem->getDebugLoc());
369 LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
370 ++NumElimRem;
371 Changed = true;
372 DeadInsts.emplace_back(Rem);
373}
374
375/// SimplifyIVUsers helper for eliminating useless remainder operations
376/// operating on an induction variable or replacing srem by urem.
377void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem,
378 Instruction *IVOperand,
379 bool IsSigned) {
380 auto *NValue = Rem->getOperand(0);
381 auto *DValue = Rem->getOperand(1);
382 // We're only interested in the case where we know something about
383 // the numerator, unless it is a srem, because we want to replace srem by urem
384 // in general.
385 bool UsedAsNumerator = IVOperand == NValue;
386 if (!UsedAsNumerator && !IsSigned)
387 return;
388
389 const SCEV *N = SE->getSCEV(NValue);
390
391 // Simplify unnecessary loops away.
392 const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
393 N = SE->getSCEVAtScope(N, ICmpLoop);
394
395 bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
396
397 // Do not proceed if the Numerator may be negative
398 if (!IsNumeratorNonNegative)
399 return;
400
401 const SCEV *D = SE->getSCEV(DValue);
402 D = SE->getSCEVAtScope(D, ICmpLoop);
403
404 if (UsedAsNumerator) {
405 auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
406 if (SE->isKnownPredicate(LT, N, D)) {
407 replaceRemWithNumerator(Rem);
408 return;
409 }
410
411 auto *T = Rem->getType();
412 const SCEV *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
413 if (SE->isKnownPredicate(LT, NLessOne, D)) {
414 replaceRemWithNumeratorOrZero(Rem);
415 return;
416 }
417 }
418
419 // Try to replace SRem with URem, if both N and D are known non-negative.
420 // Since we had already check N, we only need to check D now
421 if (!IsSigned || !SE->isKnownNonNegative(D))
422 return;
423
424 replaceSRemWithURem(Rem);
425}
426
427bool SimplifyIndvar::eliminateOverflowIntrinsic(WithOverflowInst *WO) {
428 const SCEV *LHS = SE->getSCEV(WO->getLHS());
429 const SCEV *RHS = SE->getSCEV(WO->getRHS());
430 if (!SE->willNotOverflow(WO->getBinaryOp(), WO->isSigned(), LHS, RHS))
431 return false;
432
433 // Proved no overflow, nuke the overflow check and, if possible, the overflow
434 // intrinsic as well.
435
436 BinaryOperator *NewResult = BinaryOperator::Create(
437 WO->getBinaryOp(), WO->getLHS(), WO->getRHS(), "", WO->getIterator());
438
439 if (WO->isSigned())
440 NewResult->setHasNoSignedWrap(true);
441 else
442 NewResult->setHasNoUnsignedWrap(true);
443
445
446 for (auto *U : WO->users()) {
447 if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
448 if (EVI->getIndices()[0] == 1)
449 EVI->replaceAllUsesWith(ConstantInt::getFalse(WO->getContext()));
450 else {
451 assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
452 EVI->replaceAllUsesWith(NewResult);
453 NewResult->setDebugLoc(EVI->getDebugLoc());
454 }
455 ToDelete.push_back(EVI);
456 }
457 }
458
459 for (auto *EVI : ToDelete)
460 EVI->eraseFromParent();
461
462 if (WO->use_empty())
463 WO->eraseFromParent();
464
465 Changed = true;
466 return true;
467}
468
469bool SimplifyIndvar::eliminateSaturatingIntrinsic(SaturatingInst *SI) {
470 const SCEV *LHS = SE->getSCEV(SI->getLHS());
471 const SCEV *RHS = SE->getSCEV(SI->getRHS());
472 if (!SE->willNotOverflow(SI->getBinaryOp(), SI->isSigned(), LHS, RHS))
473 return false;
474
475 BinaryOperator *BO = BinaryOperator::Create(
476 SI->getBinaryOp(), SI->getLHS(), SI->getRHS(), SI->getName(), SI->getIterator());
477 if (SI->isSigned())
478 BO->setHasNoSignedWrap();
479 else
481
482 SI->replaceAllUsesWith(BO);
483 BO->setDebugLoc(SI->getDebugLoc());
484 DeadInsts.emplace_back(SI);
485 Changed = true;
486 return true;
487}
488
489bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) {
490 // It is always legal to replace
491 // icmp <pred> i32 trunc(iv), n
492 // with
493 // icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate.
494 // Or with
495 // icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate.
496 // Or with either of these if pred is an equality predicate.
497 //
498 // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for
499 // every comparison which uses trunc, it means that we can replace each of
500 // them with comparison of iv against sext/zext(n). We no longer need trunc
501 // after that.
502 //
503 // TODO: Should we do this if we can widen *some* comparisons, but not all
504 // of them? Sometimes it is enough to enable other optimizations, but the
505 // trunc instruction will stay in the loop.
506 Value *IV = TI->getOperand(0);
507 Type *IVTy = IV->getType();
508 const SCEV *IVSCEV = SE->getSCEV(IV);
509 const SCEV *TISCEV = SE->getSCEV(TI);
510
511 // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can
512 // get rid of trunc
513 bool DoesSExtCollapse = false;
514 bool DoesZExtCollapse = false;
515 if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy))
516 DoesSExtCollapse = true;
517 if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy))
518 DoesZExtCollapse = true;
519
520 // If neither sext nor zext does collapse, it is not profitable to do any
521 // transform. Bail.
522 if (!DoesSExtCollapse && !DoesZExtCollapse)
523 return false;
524
525 // Collect users of the trunc that look like comparisons against invariants.
526 // Bail if we find something different.
528 for (auto *U : TI->users()) {
529 // We don't care about users in unreachable blocks.
530 if (isa<Instruction>(U) &&
531 !DT->isReachableFromEntry(cast<Instruction>(U)->getParent()))
532 continue;
533 ICmpInst *ICI = dyn_cast<ICmpInst>(U);
534 if (!ICI) return false;
535 assert(L->contains(ICI->getParent()) && "LCSSA form broken?");
536 if (!(ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) &&
537 !(ICI->getOperand(1) == TI && L->isLoopInvariant(ICI->getOperand(0))))
538 return false;
539 // If we cannot get rid of trunc, bail.
540 if (ICI->isSigned() && !DoesSExtCollapse)
541 return false;
542 if (ICI->isUnsigned() && !DoesZExtCollapse)
543 return false;
544 // For equality, either signed or unsigned works.
545 ICmpUsers.push_back(ICI);
546 }
547
548 auto CanUseZExt = [&](ICmpInst *ICI) {
549 // Unsigned comparison can be widened as unsigned.
550 if (ICI->isUnsigned())
551 return true;
552 // Is it profitable to do zext?
553 if (!DoesZExtCollapse)
554 return false;
555 // For equality, we can safely zext both parts.
556 if (ICI->isEquality())
557 return true;
558 // Otherwise we can only use zext when comparing two non-negative or two
559 // negative values. But in practice, we will never pass DoesZExtCollapse
560 // check for a negative value, because zext(trunc(x)) is non-negative. So
561 // it only make sense to check for non-negativity here.
562 const SCEV *SCEVOP1 = SE->getSCEV(ICI->getOperand(0));
563 const SCEV *SCEVOP2 = SE->getSCEV(ICI->getOperand(1));
564 return SE->isKnownNonNegative(SCEVOP1) && SE->isKnownNonNegative(SCEVOP2);
565 };
566 // Replace all comparisons against trunc with comparisons against IV.
567 for (auto *ICI : ICmpUsers) {
568 bool IsSwapped = L->isLoopInvariant(ICI->getOperand(0));
569 auto *Op1 = IsSwapped ? ICI->getOperand(0) : ICI->getOperand(1);
570 IRBuilder<> Builder(ICI);
571 Value *Ext = nullptr;
572 // For signed/unsigned predicate, replace the old comparison with comparison
573 // of immediate IV against sext/zext of the invariant argument. If we can
574 // use either sext or zext (i.e. we are dealing with equality predicate),
575 // then prefer zext as a more canonical form.
576 // TODO: If we see a signed comparison which can be turned into unsigned,
577 // we can do it here for canonicalization purposes.
578 ICmpInst::Predicate Pred = ICI->getPredicate();
579 if (IsSwapped) Pred = ICmpInst::getSwappedPredicate(Pred);
580 if (CanUseZExt(ICI)) {
581 assert(DoesZExtCollapse && "Unprofitable zext?");
582 Ext = Builder.CreateZExt(Op1, IVTy, "zext");
584 } else {
585 assert(DoesSExtCollapse && "Unprofitable sext?");
586 Ext = Builder.CreateSExt(Op1, IVTy, "sext");
587 assert(Pred == ICmpInst::getSignedPredicate(Pred) && "Must be signed!");
588 }
589 bool Changed;
590 L->makeLoopInvariant(Ext, Changed);
591 (void)Changed;
592 auto *NewCmp = Builder.CreateICmp(Pred, IV, Ext);
593 ICI->replaceAllUsesWith(NewCmp);
594 DeadInsts.emplace_back(ICI);
595 }
596
597 // Trunc no longer needed.
599 DeadInsts.emplace_back(TI);
600 return true;
601}
602
603/// Eliminate an operation that consumes a simple IV and has no observable
604/// side-effect given the range of IV values. IVOperand is guaranteed SCEVable,
605/// but UseInst may not be.
606bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
607 Instruction *IVOperand) {
608 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
609 eliminateIVComparison(ICmp, IVOperand);
610 return true;
611 }
612 if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
613 bool IsSRem = Bin->getOpcode() == Instruction::SRem;
614 if (IsSRem || Bin->getOpcode() == Instruction::URem) {
615 simplifyIVRemainder(Bin, IVOperand, IsSRem);
616 return true;
617 }
618
619 if (Bin->getOpcode() == Instruction::SDiv)
620 return eliminateSDiv(Bin);
621 }
622
623 if (auto *WO = dyn_cast<WithOverflowInst>(UseInst))
624 if (eliminateOverflowIntrinsic(WO))
625 return true;
626
627 if (auto *SI = dyn_cast<SaturatingInst>(UseInst))
628 if (eliminateSaturatingIntrinsic(SI))
629 return true;
630
631 if (auto *TI = dyn_cast<TruncInst>(UseInst))
632 if (eliminateTrunc(TI))
633 return true;
634
635 if (eliminateIdentitySCEV(UseInst, IVOperand))
636 return true;
637
638 return false;
639}
640
642 if (auto *BB = L->getLoopPreheader())
643 return BB->getTerminator();
644
645 return Hint;
646}
647
648/// Replace the UseInst with a loop invariant expression if it is safe.
649bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) {
650 if (!SE->isSCEVable(I->getType()))
651 return false;
652
653 // Get the symbolic expression for this instruction.
654 const SCEV *S = SE->getSCEV(I);
655
656 if (!SE->isLoopInvariant(S, L))
657 return false;
658
659 // Do not generate something ridiculous even if S is loop invariant.
660 if (Rewriter.isHighCostExpansion(S, L, SCEVCheapExpansionBudget, TTI, I))
661 return false;
662
663 auto *IP = GetLoopInvariantInsertPosition(L, I);
664
665 if (!Rewriter.isSafeToExpandAt(S, IP)) {
666 LLVM_DEBUG(dbgs() << "INDVARS: Can not replace IV user: " << *I
667 << " with non-speculable loop invariant: " << *S << '\n');
668 return false;
669 }
670
671 auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
672 bool NeedToEmitLCSSAPhis = false;
673 if (!LI->replacementPreservesLCSSAForm(I, Invariant))
674 NeedToEmitLCSSAPhis = true;
675
676 I->replaceAllUsesWith(Invariant);
677 LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
678 << " with loop invariant: " << *S << '\n');
679
680 if (NeedToEmitLCSSAPhis) {
681 SmallVector<Instruction *, 1> NeedsLCSSAPhis;
682 NeedsLCSSAPhis.push_back(cast<Instruction>(Invariant));
683 formLCSSAForInstructions(NeedsLCSSAPhis, *DT, *LI, SE);
684 LLVM_DEBUG(dbgs() << " INDVARS: Replacement breaks LCSSA form"
685 << " inserting LCSSA Phis" << '\n');
686 }
687 ++NumFoldedUser;
688 Changed = true;
689 DeadInsts.emplace_back(I);
690 return true;
691}
692
693/// Eliminate redundant type cast between integer and float.
694bool SimplifyIndvar::replaceFloatIVWithIntegerIV(Instruction *UseInst) {
695 if (UseInst->getOpcode() != CastInst::SIToFP &&
696 UseInst->getOpcode() != CastInst::UIToFP)
697 return false;
698
699 Instruction *IVOperand = cast<Instruction>(UseInst->getOperand(0));
700 // Get the symbolic expression for this instruction.
701 const SCEV *IV = SE->getSCEV(IVOperand);
702 int MaskBits;
703 if (UseInst->getOpcode() == CastInst::SIToFP)
704 MaskBits = (int)SE->getSignedRange(IV).getMinSignedBits();
705 else
706 MaskBits = (int)SE->getUnsignedRange(IV).getActiveBits();
707 int DestNumSigBits = UseInst->getType()->getFPMantissaWidth();
708 if (MaskBits <= DestNumSigBits) {
709 for (User *U : UseInst->users()) {
710 // Match for fptosi/fptoui of sitofp and with same type.
711 auto *CI = dyn_cast<CastInst>(U);
712 if (!CI)
713 continue;
714
715 CastInst::CastOps Opcode = CI->getOpcode();
716 if (Opcode != CastInst::FPToSI && Opcode != CastInst::FPToUI)
717 continue;
718
719 Value *Conv = nullptr;
720 if (IVOperand->getType() != CI->getType()) {
721 IRBuilder<> Builder(CI);
722 StringRef Name = IVOperand->getName();
723 // To match InstCombine logic, we only need sext if both fptosi and
724 // sitofp are used. If one of them is unsigned, then we can use zext.
725 if (SE->getTypeSizeInBits(IVOperand->getType()) >
726 SE->getTypeSizeInBits(CI->getType())) {
727 Conv = Builder.CreateTrunc(IVOperand, CI->getType(), Name + ".trunc");
728 } else if (Opcode == CastInst::FPToUI ||
729 UseInst->getOpcode() == CastInst::UIToFP) {
730 Conv = Builder.CreateZExt(IVOperand, CI->getType(), Name + ".zext");
731 } else {
732 Conv = Builder.CreateSExt(IVOperand, CI->getType(), Name + ".sext");
733 }
734 } else
735 Conv = IVOperand;
736
737 CI->replaceAllUsesWith(Conv);
738 DeadInsts.push_back(CI);
739 LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *CI
740 << " with: " << *Conv << '\n');
741
742 ++NumFoldedUser;
743 Changed = true;
744 }
745 }
746
747 return Changed;
748}
749
750/// Eliminate any operation that SCEV can prove is an identity function.
751bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
752 Instruction *IVOperand) {
753 if (!SE->isSCEVable(UseInst->getType()) ||
754 UseInst->getType() != IVOperand->getType())
755 return false;
756
757 const SCEV *UseSCEV = SE->getSCEV(UseInst);
758 if (UseSCEV != SE->getSCEV(IVOperand))
759 return false;
760
761 // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
762 // dominator tree, even if X is an operand to Y. For instance, in
763 //
764 // %iv = phi i32 {0,+,1}
765 // br %cond, label %left, label %merge
766 //
767 // left:
768 // %X = add i32 %iv, 0
769 // br label %merge
770 //
771 // merge:
772 // %M = phi (%X, %iv)
773 //
774 // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
775 // %M.replaceAllUsesWith(%X) would be incorrect.
776
777 if (isa<PHINode>(UseInst))
778 // If UseInst is not a PHI node then we know that IVOperand dominates
779 // UseInst directly from the legality of SSA.
780 if (!DT || !DT->dominates(IVOperand, UseInst))
781 return false;
782
783 if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
784 return false;
785
786 // Make sure the operand is not more poisonous than the instruction.
787 if (!impliesPoison(IVOperand, UseInst)) {
788 SmallVector<Instruction *> DropPoisonGeneratingInsts;
789 if (!SE->canReuseInstruction(UseSCEV, IVOperand, DropPoisonGeneratingInsts))
790 return false;
791
792 for (Instruction *I : DropPoisonGeneratingInsts)
793 I->dropPoisonGeneratingAnnotations();
794 }
795
796 LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
797
798 SE->forgetValue(UseInst);
799 UseInst->replaceAllUsesWith(IVOperand);
800 ++NumElimIdentity;
801 Changed = true;
802 DeadInsts.emplace_back(UseInst);
803 return true;
804}
805
806bool SimplifyIndvar::strengthenBinaryOp(BinaryOperator *BO,
807 Instruction *IVOperand) {
808 return (isa<OverflowingBinaryOperator>(BO) &&
809 strengthenOverflowingOperation(BO, IVOperand)) ||
810 (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand));
811}
812
813/// Annotate BO with nsw / nuw if it provably does not signed-overflow /
814/// unsigned-overflow. Returns true if anything changed, false otherwise.
815bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
816 Instruction *IVOperand) {
819
820 if (!Flags)
821 return false;
822
827
828 // The getStrengthenedNoWrapFlagsFromBinOp() check inferred additional nowrap
829 // flags on addrecs while performing zero/sign extensions. We could call
830 // forgetValue() here to make sure those flags also propagate to any other
831 // SCEV expressions based on the addrec. However, this can have pathological
832 // compile-time impact, see https://bugs.llvm.org/show_bug.cgi?id=50384.
833 return true;
834}
835
836/// Annotate the Shr in (X << IVOperand) >> C as exact using the
837/// information from the IV's range. Returns true if anything changed, false
838/// otherwise.
839bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
840 Instruction *IVOperand) {
841 if (BO->getOpcode() == Instruction::Shl) {
842 bool Changed = false;
843 ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
844 for (auto *U : BO->users()) {
845 const APInt *C;
846 if (match(U,
847 m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
848 match(U,
849 m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
850 BinaryOperator *Shr = cast<BinaryOperator>(U);
851 if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
852 Shr->setIsExact(true);
853 Changed = true;
854 }
855 }
856 }
857 return Changed;
858 }
859
860 return false;
861}
862
863/// Add all uses of Def to the current IV's worklist.
864void SimplifyIndvar::pushIVUsers(
865 Instruction *Def, SmallPtrSet<Instruction *, 16> &Simplified,
866 SmallVectorImpl<std::pair<Instruction *, Instruction *>> &SimpleIVUsers) {
867 for (User *U : Def->users()) {
869
870 // Avoid infinite or exponential worklist processing.
871 // Also ensure unique worklist users.
872 // If Def is a LoopPhi, it may not be in the Simplified set, so check for
873 // self edges first.
874 if (UI == Def)
875 continue;
876
877 // Only change the current Loop, do not change the other parts (e.g. other
878 // Loops).
879 if (!L->contains(UI))
880 continue;
881
882 // Do not push the same instruction more than once.
883 if (!Simplified.insert(UI).second)
884 continue;
885
886 SimpleIVUsers.push_back(std::make_pair(UI, Def));
887 }
888}
889
890/// Return true if this instruction generates a simple SCEV
891/// expression in terms of that IV.
892///
893/// This is similar to IVUsers' isInteresting() but processes each instruction
894/// non-recursively when the operand is already known to be a simpleIVUser.
895///
896static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
897 if (!SE->isSCEVable(I->getType()))
898 return false;
899
900 // Get the symbolic expression for this instruction.
901 const SCEV *S = SE->getSCEV(I);
902
903 // Only consider affine recurrences.
905 if (AR && AR->getLoop() == L)
906 return true;
907
908 return false;
909}
910
911/// Iteratively perform simplification on a worklist of users
912/// of the specified induction variable. Each successive simplification may push
913/// more users which may themselves be candidates for simplification.
914///
915/// This algorithm does not require IVUsers analysis. Instead, it simplifies
916/// instructions in-place during analysis. Rather than rewriting induction
917/// variables bottom-up from their users, it transforms a chain of IVUsers
918/// top-down, updating the IR only when it encounters a clear optimization
919/// opportunity.
920///
921/// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
922///
923void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
924 if (!SE->isSCEVable(CurrIV->getType()))
925 return;
926
927 // Instructions processed by SimplifyIndvar for CurrIV.
928 SmallPtrSet<Instruction*,16> Simplified;
929
930 // Use-def pairs if IV users waiting to be processed for CurrIV.
932
933 // Push users of the current LoopPhi. In rare cases, pushIVUsers may be
934 // called multiple times for the same LoopPhi. This is the proper thing to
935 // do for loop header phis that use each other.
936 pushIVUsers(CurrIV, Simplified, SimpleIVUsers);
937
938 while (!SimpleIVUsers.empty()) {
939 std::pair<Instruction*, Instruction*> UseOper =
940 SimpleIVUsers.pop_back_val();
941 Instruction *UseInst = UseOper.first;
942
943 // If a user of the IndVar is trivially dead, we prefer just to mark it dead
944 // rather than try to do some complex analysis or transformation (such as
945 // widening) basing on it.
946 // TODO: Propagate TLI and pass it here to handle more cases.
947 if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) {
948 DeadInsts.emplace_back(UseInst);
949 continue;
950 }
951
952 // Bypass back edges to avoid extra work.
953 if (UseInst == CurrIV) continue;
954
955 // Try to replace UseInst with a loop invariant before any other
956 // simplifications.
957 if (replaceIVUserWithLoopInvariant(UseInst))
958 continue;
959
960 // Go further for the bitcast 'prtoint ptr to i64' or if the cast is done
961 // by truncation
963 for (Use &U : UseInst->uses()) {
964 Instruction *User = cast<Instruction>(U.getUser());
965 if (replaceIVUserWithLoopInvariant(User))
966 break; // done replacing
967 }
968
969 Instruction *IVOperand = UseOper.second;
970 for (unsigned N = 0; IVOperand; ++N) {
971 assert(N <= Simplified.size() && "runaway iteration");
972 (void) N;
973
974 Value *NewOper = foldIVUser(UseInst, IVOperand);
975 if (!NewOper)
976 break; // done folding
977 IVOperand = dyn_cast<Instruction>(NewOper);
978 }
979 if (!IVOperand)
980 continue;
981
982 if (eliminateIVUser(UseInst, IVOperand)) {
983 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
984 continue;
985 }
986
987 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) {
988 if (strengthenBinaryOp(BO, IVOperand)) {
989 // re-queue uses of the now modified binary operator and fall
990 // through to the checks that remain.
991 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
992 }
993 }
994
995 // Try to use integer induction for FPToSI of float induction directly.
996 if (replaceFloatIVWithIntegerIV(UseInst)) {
997 // Re-queue the potentially new direct uses of IVOperand.
998 pushIVUsers(IVOperand, Simplified, SimpleIVUsers);
999 continue;
1000 }
1001
1002 CastInst *Cast = dyn_cast<CastInst>(UseInst);
1003 if (V && Cast) {
1004 V->visitCast(Cast);
1005 continue;
1006 }
1007 if (isSimpleIVUser(UseInst, L, SE)) {
1008 pushIVUsers(UseInst, Simplified, SimpleIVUsers);
1009 }
1010 }
1011}
1012
1013namespace llvm {
1014
1016
1017/// Simplify instructions that use this induction variable
1018/// by using ScalarEvolution to analyze the IV's recurrence.
1019/// Returns a pair where the first entry indicates that the function makes
1020/// changes and the second entry indicates that it introduced new opportunities
1021/// for loop unswitching.
1022std::pair<bool, bool> simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE,
1023 DominatorTree *DT, LoopInfo *LI,
1024 const TargetTransformInfo *TTI,
1026 SCEVExpander &Rewriter, IVVisitor *V) {
1027 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, TTI,
1028 Rewriter, Dead);
1029 SIV.simplifyUsers(CurrIV, V);
1030 return {SIV.hasChanged(), SIV.runUnswitching()};
1031}
1032
1033/// Simplify users of induction variables within this
1034/// loop. This does not actually change or add IVs.
1036 LoopInfo *LI, const TargetTransformInfo *TTI,
1038 SCEVExpander Rewriter(*SE, "indvars");
1039#if LLVM_ENABLE_ABI_BREAKING_CHECKS
1040 Rewriter.setDebugType(DEBUG_TYPE);
1041#endif
1042 bool Changed = false;
1043 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
1044 const auto &[C, _] =
1045 simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, TTI, Dead, Rewriter);
1046 Changed |= C;
1047 }
1048 return Changed;
1049}
1050
1051} // namespace llvm
1052
1053namespace {
1054//===----------------------------------------------------------------------===//
1055// Widen Induction Variables - Extend the width of an IV to cover its
1056// widest uses.
1057//===----------------------------------------------------------------------===//
1058
1059class WidenIV {
1060 // Parameters
1061 PHINode *OrigPhi;
1062 Type *WideType;
1063
1064 // Context
1065 LoopInfo *LI;
1066 Loop *L;
1067 ScalarEvolution *SE;
1068 DominatorTree *DT;
1069
1070 // Does the module have any calls to the llvm.experimental.guard intrinsic
1071 // at all? If not we can avoid scanning instructions looking for guards.
1072 bool HasGuards;
1073
1074 bool UsePostIncrementRanges;
1075
1076 // Statistics
1077 unsigned NumElimExt = 0;
1078 unsigned NumWidened = 0;
1079
1080 // Result
1081 PHINode *WidePhi = nullptr;
1082 Instruction *WideInc = nullptr;
1083 const SCEV *WideIncExpr = nullptr;
1084 SmallVectorImpl<WeakTrackingVH> &DeadInsts;
1085
1086 SmallPtrSet<Instruction *,16> Widened;
1087
1088 enum class ExtendKind { Zero, Sign, Unknown };
1089
1090 // A map tracking the kind of extension used to widen each narrow IV
1091 // and narrow IV user.
1092 // Key: pointer to a narrow IV or IV user.
1093 // Value: the kind of extension used to widen this Instruction.
1094 DenseMap<AssertingVH<Instruction>, ExtendKind> ExtendKindMap;
1095
1096 using DefUserPair = std::pair<AssertingVH<Value>, AssertingVH<Instruction>>;
1097
1098 // A map with control-dependent ranges for post increment IV uses. The key is
1099 // a pair of IV def and a use of this def denoting the context. The value is
1100 // a ConstantRange representing possible values of the def at the given
1101 // context.
1102 DenseMap<DefUserPair, ConstantRange> PostIncRangeInfos;
1103
1104 std::optional<ConstantRange> getPostIncRangeInfo(Value *Def,
1105 Instruction *UseI) {
1106 DefUserPair Key(Def, UseI);
1107 auto It = PostIncRangeInfos.find(Key);
1108 return It == PostIncRangeInfos.end()
1109 ? std::optional<ConstantRange>(std::nullopt)
1110 : std::optional<ConstantRange>(It->second);
1111 }
1112
1113 void calculatePostIncRanges(PHINode *OrigPhi);
1114 void calculatePostIncRange(Instruction *NarrowDef, Instruction *NarrowUser);
1115
1116 void updatePostIncRangeInfo(Value *Def, Instruction *UseI, ConstantRange R) {
1117 DefUserPair Key(Def, UseI);
1118 auto [It, Inserted] = PostIncRangeInfos.try_emplace(Key, R);
1119 if (!Inserted)
1120 It->second = R.intersectWith(It->second);
1121 }
1122
1123public:
1124 /// Record a link in the Narrow IV def-use chain along with the WideIV that
1125 /// computes the same value as the Narrow IV def. This avoids caching Use*
1126 /// pointers.
1127 struct NarrowIVDefUse {
1128 Instruction *NarrowDef = nullptr;
1129 Instruction *NarrowUse = nullptr;
1130 Instruction *WideDef = nullptr;
1131
1132 // True if the narrow def is never negative. Tracking this information lets
1133 // us use a sign extension instead of a zero extension or vice versa, when
1134 // profitable and legal.
1135 bool NeverNegative = false;
1136
1137 NarrowIVDefUse(Instruction *ND, Instruction *NU, Instruction *WD,
1138 bool NeverNegative)
1139 : NarrowDef(ND), NarrowUse(NU), WideDef(WD),
1140 NeverNegative(NeverNegative) {}
1141 };
1142
1143 WidenIV(const WideIVInfo &WI, LoopInfo *LInfo, ScalarEvolution *SEv,
1144 DominatorTree *DTree, SmallVectorImpl<WeakTrackingVH> &DI,
1145 bool HasGuards, bool UsePostIncrementRanges = true);
1146
1147 PHINode *createWideIV(SCEVExpander &Rewriter);
1148
1149 unsigned getNumElimExt() { return NumElimExt; };
1150 unsigned getNumWidened() { return NumWidened; };
1151
1152protected:
1153 Value *createExtendInst(Value *NarrowOper, Type *WideType, bool IsSigned,
1154 Instruction *Use);
1155
1156 Instruction *cloneIVUser(NarrowIVDefUse DU, const SCEVAddRecExpr *WideAR);
1157 Instruction *cloneArithmeticIVUser(NarrowIVDefUse DU,
1158 const SCEVAddRecExpr *WideAR);
1159 Instruction *cloneBitwiseIVUser(NarrowIVDefUse DU);
1160
1161 ExtendKind getExtendKind(Instruction *I);
1162
1163 using WidenedRecTy = std::pair<const SCEVAddRecExpr *, ExtendKind>;
1164
1165 WidenedRecTy getWideRecurrence(NarrowIVDefUse DU);
1166
1167 WidenedRecTy getExtendedOperandRecurrence(NarrowIVDefUse DU);
1168
1169 const SCEV *getSCEVByOpCode(const SCEV *LHS, const SCEV *RHS,
1170 unsigned OpCode) const;
1171
1172 Instruction *widenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter,
1173 PHINode *OrigPhi, PHINode *WidePhi);
1174 void truncateIVUse(NarrowIVDefUse DU);
1175
1176 bool widenLoopCompare(NarrowIVDefUse DU);
1177 bool widenWithVariantUse(NarrowIVDefUse DU);
1178
1179 void pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef);
1180
1181private:
1182 SmallVector<NarrowIVDefUse, 8> NarrowIVUsers;
1183};
1184} // namespace
1185
1186/// Determine the insertion point for this user. By default, insert immediately
1187/// before the user. SCEVExpander or LICM will hoist loop invariants out of the
1188/// loop. For PHI nodes, there may be multiple uses, so compute the nearest
1189/// common dominator for the incoming blocks. A nullptr can be returned if no
1190/// viable location is found: it may happen if User is a PHI and Def only comes
1191/// to this PHI from unreachable blocks.
1193 DominatorTree *DT, LoopInfo *LI) {
1195 if (!PHI)
1196 return User;
1197
1198 Instruction *InsertPt = nullptr;
1199 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
1200 if (PHI->getIncomingValue(i) != Def)
1201 continue;
1202
1203 BasicBlock *InsertBB = PHI->getIncomingBlock(i);
1204
1205 if (!DT->isReachableFromEntry(InsertBB))
1206 continue;
1207
1208 if (!InsertPt) {
1209 InsertPt = InsertBB->getTerminator();
1210 continue;
1211 }
1212 InsertBB = DT->findNearestCommonDominator(InsertPt->getParent(), InsertBB);
1213 InsertPt = InsertBB->getTerminator();
1214 }
1215
1216 // If we have skipped all inputs, it means that Def only comes to Phi from
1217 // unreachable blocks.
1218 if (!InsertPt)
1219 return nullptr;
1220
1221 auto *DefI = dyn_cast<Instruction>(Def);
1222 if (!DefI)
1223 return InsertPt;
1224
1225 assert(DT->dominates(DefI, InsertPt) && "def does not dominate all uses");
1226
1227 auto *L = LI->getLoopFor(DefI->getParent());
1228 assert(!L || L->contains(LI->getLoopFor(InsertPt->getParent())));
1229
1230 for (auto *DTN = (*DT)[InsertPt->getParent()]; DTN; DTN = DTN->getIDom())
1231 if (LI->getLoopFor(DTN->getBlock()) == L)
1232 return DTN->getBlock()->getTerminator();
1233
1234 llvm_unreachable("DefI dominates InsertPt!");
1235}
1236
1237WidenIV::WidenIV(const WideIVInfo &WI, LoopInfo *LInfo, ScalarEvolution *SEv,
1238 DominatorTree *DTree, SmallVectorImpl<WeakTrackingVH> &DI,
1239 bool HasGuards, bool UsePostIncrementRanges)
1240 : OrigPhi(WI.NarrowIV), WideType(WI.WidestNativeType), LI(LInfo),
1241 L(LI->getLoopFor(OrigPhi->getParent())), SE(SEv), DT(DTree),
1242 HasGuards(HasGuards), UsePostIncrementRanges(UsePostIncrementRanges),
1243 DeadInsts(DI) {
1244 assert(L->getHeader() == OrigPhi->getParent() && "Phi must be an IV");
1245 ExtendKindMap[OrigPhi] = WI.IsSigned ? ExtendKind::Sign : ExtendKind::Zero;
1246}
1247
1248Value *WidenIV::createExtendInst(Value *NarrowOper, Type *WideType,
1249 bool IsSigned, Instruction *Use) {
1250 // Set the debug location and conservative insertion point.
1251 IRBuilder<> Builder(Use);
1252 // Hoist the insertion point into loop preheaders as far as possible.
1253 for (const Loop *L = LI->getLoopFor(Use->getParent());
1254 L && L->getLoopPreheader() && L->isLoopInvariant(NarrowOper);
1255 L = L->getParentLoop())
1256 Builder.SetInsertPoint(L->getLoopPreheader()->getTerminator());
1257
1258 return IsSigned ? Builder.CreateSExt(NarrowOper, WideType) :
1259 Builder.CreateZExt(NarrowOper, WideType);
1260}
1261
1262/// Instantiate a wide operation to replace a narrow operation. This only needs
1263/// to handle operations that can evaluation to SCEVAddRec. It can safely return
1264/// 0 for any operation we decide not to clone.
1265Instruction *WidenIV::cloneIVUser(WidenIV::NarrowIVDefUse DU,
1266 const SCEVAddRecExpr *WideAR) {
1267 unsigned Opcode = DU.NarrowUse->getOpcode();
1268 switch (Opcode) {
1269 default:
1270 return nullptr;
1271 case Instruction::Add:
1272 case Instruction::Mul:
1273 case Instruction::UDiv:
1274 case Instruction::Sub:
1275 return cloneArithmeticIVUser(DU, WideAR);
1276
1277 case Instruction::And:
1278 case Instruction::Or:
1279 case Instruction::Xor:
1280 case Instruction::Shl:
1281 case Instruction::LShr:
1282 case Instruction::AShr:
1283 return cloneBitwiseIVUser(DU);
1284 }
1285}
1286
1287Instruction *WidenIV::cloneBitwiseIVUser(WidenIV::NarrowIVDefUse DU) {
1288 Instruction *NarrowUse = DU.NarrowUse;
1289 Instruction *NarrowDef = DU.NarrowDef;
1290 Instruction *WideDef = DU.WideDef;
1291
1292 LLVM_DEBUG(dbgs() << "Cloning bitwise IVUser: " << *NarrowUse << "\n");
1293
1294 // Replace NarrowDef operands with WideDef. Otherwise, we don't know anything
1295 // about the narrow operand yet so must insert a [sz]ext. It is probably loop
1296 // invariant and will be folded or hoisted. If it actually comes from a
1297 // widened IV, it should be removed during a future call to widenIVUse.
1298 bool IsSigned = getExtendKind(NarrowDef) == ExtendKind::Sign;
1299 Value *LHS = (NarrowUse->getOperand(0) == NarrowDef)
1300 ? WideDef
1301 : createExtendInst(NarrowUse->getOperand(0), WideType,
1302 IsSigned, NarrowUse);
1303 Value *RHS = (NarrowUse->getOperand(1) == NarrowDef)
1304 ? WideDef
1305 : createExtendInst(NarrowUse->getOperand(1), WideType,
1306 IsSigned, NarrowUse);
1307
1308 auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1309 auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1310 NarrowBO->getName());
1311 IRBuilder<> Builder(NarrowUse);
1312 Builder.Insert(WideBO);
1313 WideBO->copyIRFlags(NarrowBO);
1314 return WideBO;
1315}
1316
1317Instruction *WidenIV::cloneArithmeticIVUser(WidenIV::NarrowIVDefUse DU,
1318 const SCEVAddRecExpr *WideAR) {
1319 Instruction *NarrowUse = DU.NarrowUse;
1320 Instruction *NarrowDef = DU.NarrowDef;
1321 Instruction *WideDef = DU.WideDef;
1322
1323 LLVM_DEBUG(dbgs() << "Cloning arithmetic IVUser: " << *NarrowUse << "\n");
1324
1325 unsigned IVOpIdx = (NarrowUse->getOperand(0) == NarrowDef) ? 0 : 1;
1326
1327 // We're trying to find X such that
1328 //
1329 // Widen(NarrowDef `op` NonIVNarrowDef) == WideAR == WideDef `op.wide` X
1330 //
1331 // We guess two solutions to X, sext(NonIVNarrowDef) and zext(NonIVNarrowDef),
1332 // and check using SCEV if any of them are correct.
1333
1334 // Returns true if extending NonIVNarrowDef according to `SignExt` is a
1335 // correct solution to X.
1336 auto GuessNonIVOperand = [&](bool SignExt) {
1337 const SCEV *WideLHS;
1338 const SCEV *WideRHS;
1339
1340 auto GetExtend = [this, SignExt](const SCEV *S, Type *Ty) {
1341 if (SignExt)
1342 return SE->getSignExtendExpr(S, Ty);
1343 return SE->getZeroExtendExpr(S, Ty);
1344 };
1345
1346 if (IVOpIdx == 0) {
1347 WideLHS = SE->getSCEV(WideDef);
1348 const SCEV *NarrowRHS = SE->getSCEV(NarrowUse->getOperand(1));
1349 WideRHS = GetExtend(NarrowRHS, WideType);
1350 } else {
1351 const SCEV *NarrowLHS = SE->getSCEV(NarrowUse->getOperand(0));
1352 WideLHS = GetExtend(NarrowLHS, WideType);
1353 WideRHS = SE->getSCEV(WideDef);
1354 }
1355
1356 // WideUse is "WideDef `op.wide` X" as described in the comment.
1357 const SCEV *WideUse =
1358 getSCEVByOpCode(WideLHS, WideRHS, NarrowUse->getOpcode());
1359
1360 return WideUse == WideAR;
1361 };
1362
1363 bool SignExtend = getExtendKind(NarrowDef) == ExtendKind::Sign;
1364 if (!GuessNonIVOperand(SignExtend)) {
1365 SignExtend = !SignExtend;
1366 if (!GuessNonIVOperand(SignExtend))
1367 return nullptr;
1368 }
1369
1370 Value *LHS = (NarrowUse->getOperand(0) == NarrowDef)
1371 ? WideDef
1372 : createExtendInst(NarrowUse->getOperand(0), WideType,
1373 SignExtend, NarrowUse);
1374 Value *RHS = (NarrowUse->getOperand(1) == NarrowDef)
1375 ? WideDef
1376 : createExtendInst(NarrowUse->getOperand(1), WideType,
1377 SignExtend, NarrowUse);
1378
1379 auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1380 auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1381 NarrowBO->getName());
1382
1383 IRBuilder<> Builder(NarrowUse);
1384 Builder.Insert(WideBO);
1385 WideBO->copyIRFlags(NarrowBO);
1386 return WideBO;
1387}
1388
1389WidenIV::ExtendKind WidenIV::getExtendKind(Instruction *I) {
1390 auto It = ExtendKindMap.find(I);
1391 assert(It != ExtendKindMap.end() && "Instruction not yet extended!");
1392 return It->second;
1393}
1394
1395const SCEV *WidenIV::getSCEVByOpCode(const SCEV *LHS, const SCEV *RHS,
1396 unsigned OpCode) const {
1397 switch (OpCode) {
1398 case Instruction::Add:
1399 return SE->getAddExpr(LHS, RHS);
1400 case Instruction::Sub:
1401 return SE->getMinusSCEV(LHS, RHS);
1402 case Instruction::Mul:
1403 return SE->getMulExpr(LHS, RHS);
1404 case Instruction::UDiv:
1405 return SE->getUDivExpr(LHS, RHS);
1406 default:
1407 llvm_unreachable("Unsupported opcode.");
1408 };
1409}
1410
1411namespace {
1412
1413// Represents a interesting integer binary operation for
1414// getExtendedOperandRecurrence. This may be a shl that is being treated as a
1415// multiply or a 'or disjoint' that is being treated as 'add nsw nuw'.
1416struct BinaryOp {
1417 unsigned Opcode;
1418 std::array<Value *, 2> Operands;
1419 bool IsNSW = false;
1420 bool IsNUW = false;
1421
1422 explicit BinaryOp(Instruction *Op)
1423 : Opcode(Op->getOpcode()),
1424 Operands({Op->getOperand(0), Op->getOperand(1)}) {
1425 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
1426 IsNSW = OBO->hasNoSignedWrap();
1427 IsNUW = OBO->hasNoUnsignedWrap();
1428 }
1429 }
1430
1431 explicit BinaryOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS,
1432 bool IsNSW = false, bool IsNUW = false)
1433 : Opcode(Opcode), Operands({LHS, RHS}), IsNSW(IsNSW), IsNUW(IsNUW) {}
1434};
1435
1436} // end anonymous namespace
1437
1438static std::optional<BinaryOp> matchBinaryOp(Instruction *Op) {
1439 switch (Op->getOpcode()) {
1440 case Instruction::Add:
1441 case Instruction::Sub:
1442 case Instruction::Mul:
1443 return BinaryOp(Op);
1444 case Instruction::Or: {
1445 // Convert or disjoint into add nuw nsw.
1446 if (cast<PossiblyDisjointInst>(Op)->isDisjoint())
1447 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1),
1448 /*IsNSW=*/true, /*IsNUW=*/true);
1449 break;
1450 }
1451 case Instruction::Shl: {
1452 if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
1453 unsigned BitWidth = cast<IntegerType>(SA->getType())->getBitWidth();
1454
1455 // If the shift count is not less than the bitwidth, the result of
1456 // the shift is undefined. Don't try to analyze it, because the
1457 // resolution chosen here may differ from the resolution chosen in
1458 // other parts of the compiler.
1459 if (SA->getValue().ult(BitWidth)) {
1460 // We can safely preserve the nuw flag in all cases. It's also safe to
1461 // turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation
1462 // requires special handling. It can be preserved as long as we're not
1463 // left shifting by bitwidth - 1.
1464 bool IsNUW = Op->hasNoUnsignedWrap();
1465 bool IsNSW = Op->hasNoSignedWrap() &&
1466 (IsNUW || SA->getValue().ult(BitWidth - 1));
1467
1468 ConstantInt *X =
1469 ConstantInt::get(Op->getContext(),
1470 APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
1471 return BinaryOp(Instruction::Mul, Op->getOperand(0), X, IsNSW, IsNUW);
1472 }
1473 }
1474
1475 break;
1476 }
1477 }
1478
1479 return std::nullopt;
1480}
1481
1482/// No-wrap operations can transfer sign extension of their result to their
1483/// operands. Generate the SCEV value for the widened operation without
1484/// actually modifying the IR yet. If the expression after extending the
1485/// operands is an AddRec for this loop, return the AddRec and the kind of
1486/// extension used.
1487WidenIV::WidenedRecTy
1488WidenIV::getExtendedOperandRecurrence(WidenIV::NarrowIVDefUse DU) {
1489 auto Op = matchBinaryOp(DU.NarrowUse);
1490 if (!Op)
1491 return {nullptr, ExtendKind::Unknown};
1492
1493 assert((Op->Opcode == Instruction::Add || Op->Opcode == Instruction::Sub ||
1494 Op->Opcode == Instruction::Mul) &&
1495 "Unexpected opcode");
1496
1497 // One operand (NarrowDef) has already been extended to WideDef. Now determine
1498 // if extending the other will lead to a recurrence.
1499 const unsigned ExtendOperIdx = Op->Operands[0] == DU.NarrowDef ? 1 : 0;
1500 assert(Op->Operands[1 - ExtendOperIdx] == DU.NarrowDef && "bad DU");
1501
1502 ExtendKind ExtKind = getExtendKind(DU.NarrowDef);
1503 if (!(ExtKind == ExtendKind::Sign && Op->IsNSW) &&
1504 !(ExtKind == ExtendKind::Zero && Op->IsNUW)) {
1505 ExtKind = ExtendKind::Unknown;
1506
1507 // For a non-negative NarrowDef, we can choose either type of
1508 // extension. We want to use the current extend kind if legal
1509 // (see above), and we only hit this code if we need to check
1510 // the opposite case.
1511 if (DU.NeverNegative) {
1512 if (Op->IsNSW) {
1513 ExtKind = ExtendKind::Sign;
1514 } else if (Op->IsNUW) {
1515 ExtKind = ExtendKind::Zero;
1516 }
1517 }
1518 }
1519
1520 const SCEV *ExtendOperExpr = SE->getSCEV(Op->Operands[ExtendOperIdx]);
1521 if (ExtKind == ExtendKind::Sign)
1522 ExtendOperExpr = SE->getSignExtendExpr(ExtendOperExpr, WideType);
1523 else if (ExtKind == ExtendKind::Zero)
1524 ExtendOperExpr = SE->getZeroExtendExpr(ExtendOperExpr, WideType);
1525 else
1526 return {nullptr, ExtendKind::Unknown};
1527
1528 // When creating this SCEV expr, don't apply the current operations NSW or NUW
1529 // flags. This instruction may be guarded by control flow that the no-wrap
1530 // behavior depends on. Non-control-equivalent instructions can be mapped to
1531 // the same SCEV expression, and it would be incorrect to transfer NSW/NUW
1532 // semantics to those operations.
1533 const SCEV *lhs = SE->getSCEV(DU.WideDef);
1534 const SCEV *rhs = ExtendOperExpr;
1535
1536 // Let's swap operands to the initial order for the case of non-commutative
1537 // operations, like SUB. See PR21014.
1538 if (ExtendOperIdx == 0)
1539 std::swap(lhs, rhs);
1540 const SCEVAddRecExpr *AddRec =
1541 dyn_cast<SCEVAddRecExpr>(getSCEVByOpCode(lhs, rhs, Op->Opcode));
1542
1543 if (!AddRec || AddRec->getLoop() != L)
1544 return {nullptr, ExtendKind::Unknown};
1545
1546 return {AddRec, ExtKind};
1547}
1548
1549/// Is this instruction potentially interesting for further simplification after
1550/// widening it's type? In other words, can the extend be safely hoisted out of
1551/// the loop with SCEV reducing the value to a recurrence on the same loop. If
1552/// so, return the extended recurrence and the kind of extension used. Otherwise
1553/// return {nullptr, ExtendKind::Unknown}.
1554WidenIV::WidenedRecTy WidenIV::getWideRecurrence(WidenIV::NarrowIVDefUse DU) {
1555 if (!DU.NarrowUse->getType()->isIntegerTy())
1556 return {nullptr, ExtendKind::Unknown};
1557
1558 const SCEV *NarrowExpr = SE->getSCEV(DU.NarrowUse);
1559 if (SE->getTypeSizeInBits(NarrowExpr->getType()) >=
1560 SE->getTypeSizeInBits(WideType)) {
1561 // NarrowUse implicitly widens its operand. e.g. a gep with a narrow
1562 // index. So don't follow this use.
1563 return {nullptr, ExtendKind::Unknown};
1564 }
1565
1566 const SCEV *WideExpr;
1567 ExtendKind ExtKind;
1568 if (DU.NeverNegative) {
1569 WideExpr = SE->getSignExtendExpr(NarrowExpr, WideType);
1570 if (isa<SCEVAddRecExpr>(WideExpr))
1571 ExtKind = ExtendKind::Sign;
1572 else {
1573 WideExpr = SE->getZeroExtendExpr(NarrowExpr, WideType);
1574 ExtKind = ExtendKind::Zero;
1575 }
1576 } else if (getExtendKind(DU.NarrowDef) == ExtendKind::Sign) {
1577 WideExpr = SE->getSignExtendExpr(NarrowExpr, WideType);
1578 ExtKind = ExtendKind::Sign;
1579 } else {
1580 WideExpr = SE->getZeroExtendExpr(NarrowExpr, WideType);
1581 ExtKind = ExtendKind::Zero;
1582 }
1583 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(WideExpr);
1584 if (!AddRec || AddRec->getLoop() != L)
1585 return {nullptr, ExtendKind::Unknown};
1586 return {AddRec, ExtKind};
1587}
1588
1589/// This IV user cannot be widened. Replace this use of the original narrow IV
1590/// with a truncation of the new wide IV to isolate and eliminate the narrow IV.
1591void WidenIV::truncateIVUse(NarrowIVDefUse DU) {
1592 auto *InsertPt = getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT, LI);
1593 if (!InsertPt)
1594 return;
1595 LLVM_DEBUG(dbgs() << "INDVARS: Truncate IV " << *DU.WideDef << " for user "
1596 << *DU.NarrowUse << "\n");
1597 ExtendKind ExtKind = getExtendKind(DU.NarrowDef);
1598 IRBuilder<> Builder(InsertPt);
1599 Value *Trunc =
1600 Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType(), "",
1601 DU.NeverNegative || ExtKind == ExtendKind::Zero,
1602 DU.NeverNegative || ExtKind == ExtendKind::Sign);
1603 DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc);
1604}
1605
1606/// If the narrow use is a compare instruction, then widen the compare
1607// (and possibly the other operand). The extend operation is hoisted into the
1608// loop preheader as far as possible.
1609bool WidenIV::widenLoopCompare(WidenIV::NarrowIVDefUse DU) {
1610 ICmpInst *Cmp = dyn_cast<ICmpInst>(DU.NarrowUse);
1611 if (!Cmp)
1612 return false;
1613
1614 // We can legally widen the comparison in the following two cases:
1615 //
1616 // - The signedness of the IV extension and comparison match
1617 //
1618 // - The narrow IV is always non-negative (and thus its sign extension is
1619 // equal to its zero extension). For instance, let's say we're zero
1620 // extending %narrow for the following use
1621 //
1622 // icmp slt i32 %narrow, %val ... (A)
1623 //
1624 // and %narrow is always non-negative. Then
1625 //
1626 // (A) == icmp slt i32 sext(%narrow), sext(%val)
1627 // == icmp slt i32 zext(%narrow), sext(%val)
1628 bool IsSigned = getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
1629 bool CmpPreferredSign = Cmp->hasSameSign() ? IsSigned : Cmp->isSigned();
1630 if (!DU.NeverNegative && IsSigned != CmpPreferredSign)
1631 return false;
1632
1633 Value *Op = Cmp->getOperand(Cmp->getOperand(0) == DU.NarrowDef ? 1 : 0);
1634 unsigned CastWidth = SE->getTypeSizeInBits(Op->getType());
1635 unsigned IVWidth = SE->getTypeSizeInBits(WideType);
1636 assert(CastWidth <= IVWidth && "Unexpected width while widening compare.");
1637
1638 // Widen the compare instruction.
1639 DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
1640
1641 // Widen the other operand of the compare, if necessary.
1642 if (CastWidth < IVWidth) {
1643 // If the narrow IV is always non-negative and the other operand is sext,
1644 // widen using sext so we can combine them. This works for all non-signed
1645 // comparison predicates.
1646 if (DU.NeverNegative && isa<SExtInst>(Op) && !Cmp->isSigned())
1647 CmpPreferredSign = true;
1648
1649 Value *ExtOp = createExtendInst(Op, WideType, CmpPreferredSign, Cmp);
1650 DU.NarrowUse->replaceUsesOfWith(Op, ExtOp);
1651 }
1652 return true;
1653}
1654
1655// The widenIVUse avoids generating trunc by evaluating the use as AddRec, this
1656// will not work when:
1657// 1) SCEV traces back to an instruction inside the loop that SCEV can not
1658// expand, eg. add %indvar, (load %addr)
1659// 2) SCEV finds a loop variant, eg. add %indvar, %loopvariant
1660// While SCEV fails to avoid trunc, we can still try to use instruction
1661// combining approach to prove trunc is not required. This can be further
1662// extended with other instruction combining checks, but for now we handle the
1663// following case (sub can be "add" and "mul", "nsw + sext" can be "nus + zext")
1664//
1665// Src:
1666// %c = sub nsw %b, %indvar
1667// %d = sext %c to i64
1668// Dst:
1669// %indvar.ext1 = sext %indvar to i64
1670// %m = sext %b to i64
1671// %d = sub nsw i64 %m, %indvar.ext1
1672// Therefore, as long as the result of add/sub/mul is extended to wide type, no
1673// trunc is required regardless of how %b is generated. This pattern is common
1674// when calculating address in 64 bit architecture
1675bool WidenIV::widenWithVariantUse(WidenIV::NarrowIVDefUse DU) {
1676 Instruction *NarrowUse = DU.NarrowUse;
1677 Instruction *NarrowDef = DU.NarrowDef;
1678 Instruction *WideDef = DU.WideDef;
1679
1680 // Handle the common case of add<nsw/nuw>
1681 const unsigned OpCode = NarrowUse->getOpcode();
1682 // Only Add/Sub/Mul instructions are supported.
1683 if (OpCode != Instruction::Add && OpCode != Instruction::Sub &&
1684 OpCode != Instruction::Mul)
1685 return false;
1686
1687 // The operand that is not defined by NarrowDef of DU. Let's call it the
1688 // other operand.
1689 assert((NarrowUse->getOperand(0) == NarrowDef ||
1690 NarrowUse->getOperand(1) == NarrowDef) &&
1691 "bad DU");
1692
1693 const OverflowingBinaryOperator *OBO =
1695 ExtendKind ExtKind = getExtendKind(NarrowDef);
1696 bool CanSignExtend = ExtKind == ExtendKind::Sign && OBO->hasNoSignedWrap();
1697 bool CanZeroExtend = ExtKind == ExtendKind::Zero && OBO->hasNoUnsignedWrap();
1698 auto AnotherOpExtKind = ExtKind;
1699
1700 // Check that all uses are either:
1701 // - narrow def (in case of we are widening the IV increment);
1702 // - single-input LCSSA Phis;
1703 // - comparison of the chosen type;
1704 // - extend of the chosen type (raison d'etre).
1705 SmallVector<Instruction *, 4> ExtUsers;
1706 SmallVector<PHINode *, 4> LCSSAPhiUsers;
1708 for (Use &U : NarrowUse->uses()) {
1709 Instruction *User = cast<Instruction>(U.getUser());
1710 if (User == NarrowDef)
1711 continue;
1712 if (!L->contains(User)) {
1713 auto *LCSSAPhi = cast<PHINode>(User);
1714 // Make sure there is only 1 input, so that we don't have to split
1715 // critical edges.
1716 if (LCSSAPhi->getNumOperands() != 1)
1717 return false;
1718 LCSSAPhiUsers.push_back(LCSSAPhi);
1719 continue;
1720 }
1721 if (auto *ICmp = dyn_cast<ICmpInst>(User)) {
1722 auto Pred = ICmp->getPredicate();
1723 // We have 3 types of predicates: signed, unsigned and equality
1724 // predicates. For equality, it's legal to widen icmp for either sign and
1725 // zero extend. For sign extend, we can also do so for signed predicates,
1726 // likeweise for zero extend we can widen icmp for unsigned predicates.
1727 if (ExtKind == ExtendKind::Zero && ICmpInst::isSigned(Pred))
1728 return false;
1729 if (ExtKind == ExtendKind::Sign && ICmpInst::isUnsigned(Pred))
1730 return false;
1731 ICmpUsers.push_back(ICmp);
1732 continue;
1733 }
1734 if (ExtKind == ExtendKind::Sign)
1735 User = dyn_cast<SExtInst>(User);
1736 else
1737 User = dyn_cast<ZExtInst>(User);
1738 if (!User || User->getType() != WideType)
1739 return false;
1740 ExtUsers.push_back(User);
1741 }
1742 if (ExtUsers.empty()) {
1743 DeadInsts.emplace_back(NarrowUse);
1744 return true;
1745 }
1746
1747 // We'll prove some facts that should be true in the context of ext users. If
1748 // there is no users, we are done now. If there are some, pick their common
1749 // dominator as context.
1750 const Instruction *CtxI = findCommonDominator(ExtUsers, *DT);
1751
1752 if (!CanSignExtend && !CanZeroExtend) {
1753 // Because InstCombine turns 'sub nuw' to 'add' losing the no-wrap flag, we
1754 // will most likely not see it. Let's try to prove it.
1755 if (OpCode != Instruction::Add)
1756 return false;
1757 if (ExtKind != ExtendKind::Zero)
1758 return false;
1759 const SCEV *LHS = SE->getSCEV(OBO->getOperand(0));
1760 const SCEV *RHS = SE->getSCEV(OBO->getOperand(1));
1761 // TODO: Support case for NarrowDef = NarrowUse->getOperand(1).
1762 if (NarrowUse->getOperand(0) != NarrowDef)
1763 return false;
1764 // We cannot use a different extend kind for the same operand.
1765 if (NarrowUse->getOperand(1) == NarrowDef)
1766 return false;
1767 if (!SE->isKnownNegative(RHS))
1768 return false;
1769 bool ProvedSubNUW = SE->isKnownPredicateAt(ICmpInst::ICMP_UGE, LHS,
1770 SE->getNegativeSCEV(RHS), CtxI);
1771 if (!ProvedSubNUW)
1772 return false;
1773 // In fact, our 'add' is 'sub nuw'. We will need to widen the 2nd operand as
1774 // neg(zext(neg(op))), which is basically sext(op).
1775 AnotherOpExtKind = ExtendKind::Sign;
1776 }
1777
1778 // Verifying that Defining operand is an AddRec
1779 const SCEV *Op1 = SE->getSCEV(WideDef);
1780 const SCEVAddRecExpr *AddRecOp1 = dyn_cast<SCEVAddRecExpr>(Op1);
1781 if (!AddRecOp1 || AddRecOp1->getLoop() != L)
1782 return false;
1783
1784 LLVM_DEBUG(dbgs() << "Cloning arithmetic IVUser: " << *NarrowUse << "\n");
1785
1786 // Generating a widening use instruction.
1787 Value *LHS =
1788 (NarrowUse->getOperand(0) == NarrowDef)
1789 ? WideDef
1790 : createExtendInst(NarrowUse->getOperand(0), WideType,
1791 AnotherOpExtKind == ExtendKind::Sign, NarrowUse);
1792 Value *RHS =
1793 (NarrowUse->getOperand(1) == NarrowDef)
1794 ? WideDef
1795 : createExtendInst(NarrowUse->getOperand(1), WideType,
1796 AnotherOpExtKind == ExtendKind::Sign, NarrowUse);
1797
1798 auto *NarrowBO = cast<BinaryOperator>(NarrowUse);
1799 auto *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), LHS, RHS,
1800 NarrowBO->getName());
1801 IRBuilder<> Builder(NarrowUse);
1802 Builder.Insert(WideBO);
1803 WideBO->copyIRFlags(NarrowBO);
1804 ExtendKindMap[NarrowUse] = ExtKind;
1805
1806 for (Instruction *User : ExtUsers) {
1807 assert(User->getType() == WideType && "Checked before!");
1808 LLVM_DEBUG(dbgs() << "INDVARS: eliminating " << *User << " replaced by "
1809 << *WideBO << "\n");
1810 ++NumElimExt;
1811 User->replaceAllUsesWith(WideBO);
1812 DeadInsts.emplace_back(User);
1813 }
1814
1815 for (PHINode *User : LCSSAPhiUsers) {
1816 assert(User->getNumOperands() == 1 && "Checked before!");
1817 Builder.SetInsertPoint(User);
1818 auto *WidePN =
1819 Builder.CreatePHI(WideBO->getType(), 1, User->getName() + ".wide");
1820 BasicBlock *LoopExitingBlock = User->getParent()->getSinglePredecessor();
1821 assert(LoopExitingBlock && L->contains(LoopExitingBlock) &&
1822 "Not a LCSSA Phi?");
1823 WidePN->addIncoming(WideBO, LoopExitingBlock);
1824 Builder.SetInsertPoint(User->getParent()->getFirstInsertionPt());
1825 auto *TruncPN = Builder.CreateTrunc(WidePN, User->getType());
1826 User->replaceAllUsesWith(TruncPN);
1827 DeadInsts.emplace_back(User);
1828 }
1829
1830 for (ICmpInst *User : ICmpUsers) {
1831 Builder.SetInsertPoint(User);
1832 auto ExtendedOp = [&](Value * V)->Value * {
1833 if (V == NarrowUse)
1834 return WideBO;
1835 if (ExtKind == ExtendKind::Zero)
1836 return Builder.CreateZExt(V, WideBO->getType());
1837 else
1838 return Builder.CreateSExt(V, WideBO->getType());
1839 };
1840 auto Pred = User->getPredicate();
1841 auto *LHS = ExtendedOp(User->getOperand(0));
1842 auto *RHS = ExtendedOp(User->getOperand(1));
1843 auto *WideCmp =
1844 Builder.CreateICmp(Pred, LHS, RHS, User->getName() + ".wide");
1845 User->replaceAllUsesWith(WideCmp);
1846 DeadInsts.emplace_back(User);
1847 }
1848
1849 return true;
1850}
1851
1852/// Determine whether an individual user of the narrow IV can be widened. If so,
1853/// return the wide clone of the user.
1854Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU,
1855 SCEVExpander &Rewriter, PHINode *OrigPhi,
1856 PHINode *WidePhi) {
1857 assert(ExtendKindMap.count(DU.NarrowDef) &&
1858 "Should already know the kind of extension used to widen NarrowDef");
1859
1860 // This narrow use can be widened by a sext if it's non-negative or its narrow
1861 // def was widened by a sext. Same for zext.
1862 bool CanWidenBySExt =
1863 DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
1864 bool CanWidenByZExt =
1865 DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Zero;
1866
1867 // Stop traversing the def-use chain at inner-loop phis or post-loop phis.
1868 if (PHINode *UsePhi = dyn_cast<PHINode>(DU.NarrowUse)) {
1869 if (LI->getLoopFor(UsePhi->getParent()) != L) {
1870 // For LCSSA phis, sink the truncate outside the loop.
1871 // After SimplifyCFG most loop exit targets have a single predecessor.
1872 // Otherwise fall back to a truncate within the loop.
1873 if (UsePhi->getNumOperands() != 1)
1874 truncateIVUse(DU);
1875 else {
1876 // Widening the PHI requires us to insert a trunc. The logical place
1877 // for this trunc is in the same BB as the PHI. This is not possible if
1878 // the BB is terminated by a catchswitch.
1879 if (isa<CatchSwitchInst>(UsePhi->getParent()->getTerminator()))
1880 return nullptr;
1881
1882 PHINode *WidePhi =
1883 PHINode::Create(DU.WideDef->getType(), 1, UsePhi->getName() + ".wide",
1884 UsePhi->getIterator());
1885 WidePhi->addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0));
1886 BasicBlock *WidePhiBB = WidePhi->getParent();
1887 IRBuilder<> Builder(WidePhiBB->getFirstInsertionPt());
1888 Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType(), "",
1889 CanWidenByZExt, CanWidenBySExt);
1890 UsePhi->replaceAllUsesWith(Trunc);
1891 DeadInsts.emplace_back(UsePhi);
1892 LLVM_DEBUG(dbgs() << "INDVARS: Widen lcssa phi " << *UsePhi << " to "
1893 << *WidePhi << "\n");
1894 }
1895 return nullptr;
1896 }
1897 }
1898
1899 // Our raison d'etre! Eliminate sign and zero extension.
1900 if ((match(DU.NarrowUse, m_SExtLike(m_Value())) && CanWidenBySExt) ||
1901 (isa<ZExtInst>(DU.NarrowUse) && CanWidenByZExt)) {
1902 Value *NewDef = DU.WideDef;
1903 if (DU.NarrowUse->getType() != WideType) {
1904 unsigned CastWidth = SE->getTypeSizeInBits(DU.NarrowUse->getType());
1905 unsigned IVWidth = SE->getTypeSizeInBits(WideType);
1906 if (CastWidth < IVWidth) {
1907 // The cast isn't as wide as the IV, so insert a Trunc.
1908 IRBuilder<> Builder(DU.NarrowUse);
1909 NewDef = Builder.CreateTrunc(DU.WideDef, DU.NarrowUse->getType(), "",
1910 CanWidenByZExt, CanWidenBySExt);
1911 }
1912 else {
1913 // A wider extend was hidden behind a narrower one. This may induce
1914 // another round of IV widening in which the intermediate IV becomes
1915 // dead. It should be very rare.
1916 LLVM_DEBUG(dbgs() << "INDVARS: New IV " << *WidePhi
1917 << " not wide enough to subsume " << *DU.NarrowUse
1918 << "\n");
1919 DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
1920 NewDef = DU.NarrowUse;
1921 }
1922 }
1923 if (NewDef != DU.NarrowUse) {
1924 LLVM_DEBUG(dbgs() << "INDVARS: eliminating " << *DU.NarrowUse
1925 << " replaced by " << *DU.WideDef << "\n");
1926 ++NumElimExt;
1927 DU.NarrowUse->replaceAllUsesWith(NewDef);
1928 DeadInsts.emplace_back(DU.NarrowUse);
1929 }
1930 // Now that the extend is gone, we want to expose it's uses for potential
1931 // further simplification. We don't need to directly inform SimplifyIVUsers
1932 // of the new users, because their parent IV will be processed later as a
1933 // new loop phi. If we preserved IVUsers analysis, we would also want to
1934 // push the uses of WideDef here.
1935
1936 // No further widening is needed. The deceased [sz]ext had done it for us.
1937 return nullptr;
1938 }
1939
1940 auto tryAddRecExpansion = [&]() -> Instruction* {
1941 // Does this user itself evaluate to a recurrence after widening?
1942 WidenedRecTy WideAddRec = getExtendedOperandRecurrence(DU);
1943 if (!WideAddRec.first)
1944 WideAddRec = getWideRecurrence(DU);
1945 assert((WideAddRec.first == nullptr) ==
1946 (WideAddRec.second == ExtendKind::Unknown));
1947 if (!WideAddRec.first)
1948 return nullptr;
1949
1950 auto CanUseWideInc = [&]() {
1951 if (!WideInc)
1952 return false;
1953 // Reuse the IV increment that SCEVExpander created. Recompute flags,
1954 // unless the flags for both increments agree and it is safe to use the
1955 // ones from the original inc. In that case, the new use of the wide
1956 // increment won't be more poisonous.
1957 bool NeedToRecomputeFlags =
1959 OrigPhi, WidePhi, DU.NarrowUse, WideInc) ||
1960 DU.NarrowUse->hasNoUnsignedWrap() != WideInc->hasNoUnsignedWrap() ||
1961 DU.NarrowUse->hasNoSignedWrap() != WideInc->hasNoSignedWrap();
1962 return WideAddRec.first == WideIncExpr &&
1963 Rewriter.hoistIVInc(WideInc, DU.NarrowUse, NeedToRecomputeFlags);
1964 };
1965
1966 Instruction *WideUse = nullptr;
1967 if (CanUseWideInc())
1968 WideUse = WideInc;
1969 else {
1970 WideUse = cloneIVUser(DU, WideAddRec.first);
1971 if (!WideUse)
1972 return nullptr;
1973 }
1974 // Evaluation of WideAddRec ensured that the narrow expression could be
1975 // extended outside the loop without overflow. This suggests that the wide use
1976 // evaluates to the same expression as the extended narrow use, but doesn't
1977 // absolutely guarantee it. Hence the following failsafe check. In rare cases
1978 // where it fails, we simply throw away the newly created wide use.
1979 if (WideAddRec.first != SE->getSCEV(WideUse)) {
1980 LLVM_DEBUG(dbgs() << "Wide use expression mismatch: " << *WideUse << ": "
1981 << *SE->getSCEV(WideUse) << " != " << *WideAddRec.first
1982 << "\n");
1983 DeadInsts.emplace_back(WideUse);
1984 return nullptr;
1985 };
1986
1987 // if we reached this point then we are going to replace
1988 // DU.NarrowUse with WideUse. Reattach DbgValue then.
1989 replaceAllDbgUsesWith(*DU.NarrowUse, *WideUse, *WideUse, *DT);
1990
1991 ExtendKindMap[DU.NarrowUse] = WideAddRec.second;
1992 // Returning WideUse pushes it on the worklist.
1993 return WideUse;
1994 };
1995
1996 if (auto *I = tryAddRecExpansion())
1997 return I;
1998
1999 // If use is a loop condition, try to promote the condition instead of
2000 // truncating the IV first.
2001 if (widenLoopCompare(DU))
2002 return nullptr;
2003
2004 // We are here about to generate a truncate instruction that may hurt
2005 // performance because the scalar evolution expression computed earlier
2006 // in WideAddRec.first does not indicate a polynomial induction expression.
2007 // In that case, look at the operands of the use instruction to determine
2008 // if we can still widen the use instead of truncating its operand.
2009 if (widenWithVariantUse(DU))
2010 return nullptr;
2011
2012 // This user does not evaluate to a recurrence after widening, so don't
2013 // follow it. Instead insert a Trunc to kill off the original use,
2014 // eventually isolating the original narrow IV so it can be removed.
2015 truncateIVUse(DU);
2016 return nullptr;
2017}
2018
2019/// Add eligible users of NarrowDef to NarrowIVUsers.
2020void WidenIV::pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef) {
2021 const SCEV *NarrowSCEV = SE->getSCEV(NarrowDef);
2022 bool NonNegativeDef =
2023 SE->isKnownPredicate(ICmpInst::ICMP_SGE, NarrowSCEV,
2024 SE->getZero(NarrowSCEV->getType()));
2025 for (User *U : NarrowDef->users()) {
2026 Instruction *NarrowUser = cast<Instruction>(U);
2027
2028 // Handle data flow merges and bizarre phi cycles.
2029 if (!Widened.insert(NarrowUser).second)
2030 continue;
2031
2032 bool NonNegativeUse = false;
2033 if (!NonNegativeDef) {
2034 // We might have a control-dependent range information for this context.
2035 if (auto RangeInfo = getPostIncRangeInfo(NarrowDef, NarrowUser))
2036 NonNegativeUse = RangeInfo->getSignedMin().isNonNegative();
2037 }
2038
2039 NarrowIVUsers.emplace_back(NarrowDef, NarrowUser, WideDef,
2040 NonNegativeDef || NonNegativeUse);
2041 }
2042}
2043
2044/// Process a single induction variable. First use the SCEVExpander to create a
2045/// wide induction variable that evaluates to the same recurrence as the
2046/// original narrow IV. Then use a worklist to forward traverse the narrow IV's
2047/// def-use chain. After widenIVUse has processed all interesting IV users, the
2048/// narrow IV will be isolated for removal by DeleteDeadPHIs.
2049///
2050/// It would be simpler to delete uses as they are processed, but we must avoid
2051/// invalidating SCEV expressions.
2052PHINode *WidenIV::createWideIV(SCEVExpander &Rewriter) {
2053 // Is this phi an induction variable?
2054 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(OrigPhi));
2055 if (!AddRec)
2056 return nullptr;
2057
2058 // Widen the induction variable expression.
2059 const SCEV *WideIVExpr = getExtendKind(OrigPhi) == ExtendKind::Sign
2060 ? SE->getSignExtendExpr(AddRec, WideType)
2061 : SE->getZeroExtendExpr(AddRec, WideType);
2062
2063 assert(SE->getEffectiveSCEVType(WideIVExpr->getType()) == WideType &&
2064 "Expect the new IV expression to preserve its type");
2065
2066 // Can the IV be extended outside the loop without overflow?
2067 AddRec = dyn_cast<SCEVAddRecExpr>(WideIVExpr);
2068 if (!AddRec || AddRec->getLoop() != L)
2069 return nullptr;
2070
2071 // An AddRec must have loop-invariant operands. Since this AddRec is
2072 // materialized by a loop header phi, the expression cannot have any post-loop
2073 // operands, so they must dominate the loop header.
2074 assert(
2075 SE->properlyDominates(AddRec->getStart(), L->getHeader()) &&
2076 SE->properlyDominates(AddRec->getStepRecurrence(*SE), L->getHeader()) &&
2077 "Loop header phi recurrence inputs do not dominate the loop");
2078
2079 // Iterate over IV uses (including transitive ones) looking for IV increments
2080 // of the form 'add nsw %iv, <const>'. For each increment and each use of
2081 // the increment calculate control-dependent range information basing on
2082 // dominating conditions inside of the loop (e.g. a range check inside of the
2083 // loop). Calculated ranges are stored in PostIncRangeInfos map.
2084 //
2085 // Control-dependent range information is later used to prove that a narrow
2086 // definition is not negative (see pushNarrowIVUsers). It's difficult to do
2087 // this on demand because when pushNarrowIVUsers needs this information some
2088 // of the dominating conditions might be already widened.
2089 if (UsePostIncrementRanges)
2090 calculatePostIncRanges(OrigPhi);
2091
2092 // The rewriter provides a value for the desired IV expression. This may
2093 // either find an existing phi or materialize a new one. Either way, we
2094 // expect a well-formed cyclic phi-with-increments. i.e. any operand not part
2095 // of the phi-SCC dominates the loop entry.
2096 Instruction *InsertPt = &*L->getHeader()->getFirstInsertionPt();
2097 Value *ExpandInst = Rewriter.expandCodeFor(AddRec, WideType, InsertPt);
2098 // If the wide phi is not a phi node, for example a cast node, like bitcast,
2099 // inttoptr, ptrtoint, just skip for now.
2100 if (!(WidePhi = dyn_cast<PHINode>(ExpandInst))) {
2101 // if the cast node is an inserted instruction without any user, we should
2102 // remove it to make sure the pass don't touch the function as we can not
2103 // wide the phi.
2104 if (ExpandInst->use_empty() &&
2105 Rewriter.isInsertedInstruction(cast<Instruction>(ExpandInst)))
2106 DeadInsts.emplace_back(ExpandInst);
2107 return nullptr;
2108 }
2109
2110 // Remembering the WideIV increment generated by SCEVExpander allows
2111 // widenIVUse to reuse it when widening the narrow IV's increment. We don't
2112 // employ a general reuse mechanism because the call above is the only call to
2113 // SCEVExpander. Henceforth, we produce 1-to-1 narrow to wide uses.
2114 if (BasicBlock *LatchBlock = L->getLoopLatch()) {
2115 WideInc =
2116 dyn_cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));
2117 if (WideInc) {
2118 WideIncExpr = SE->getSCEV(WideInc);
2119 // Propagate the debug location associated with the original loop
2120 // increment to the new (widened) increment.
2121 auto *OrigInc =
2122 cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
2123
2124 WideInc->setDebugLoc(OrigInc->getDebugLoc());
2125 // We are replacing a narrow IV increment with a wider IV increment. If
2126 // the original (narrow) increment did not wrap, the wider increment one
2127 // should not wrap either. Set the flags to be the union of both wide
2128 // increment and original increment; this ensures we preserve flags SCEV
2129 // could infer for the wider increment. Limit this only to cases where
2130 // both increments directly increment the corresponding PHI nodes and have
2131 // the same opcode. It is not safe to re-use the flags from the original
2132 // increment, if it is more complex and SCEV expansion may have yielded a
2133 // more simplified wider increment.
2135 OrigInc, WideInc) &&
2138 WideInc->setHasNoUnsignedWrap(WideInc->hasNoUnsignedWrap() ||
2139 OrigInc->hasNoUnsignedWrap());
2140 WideInc->setHasNoSignedWrap(WideInc->hasNoSignedWrap() ||
2141 OrigInc->hasNoSignedWrap());
2142 }
2143 }
2144 }
2145
2146 LLVM_DEBUG(dbgs() << "Wide IV: " << *WidePhi << "\n");
2147 ++NumWidened;
2148
2149 // Traverse the def-use chain using a worklist starting at the original IV.
2150 assert(Widened.empty() && NarrowIVUsers.empty() && "expect initial state" );
2151
2152 Widened.insert(OrigPhi);
2153 pushNarrowIVUsers(OrigPhi, WidePhi);
2154
2155 while (!NarrowIVUsers.empty()) {
2156 WidenIV::NarrowIVDefUse DU = NarrowIVUsers.pop_back_val();
2157
2158 // Process a def-use edge. This may replace the use, so don't hold a
2159 // use_iterator across it.
2160 Instruction *WideUse = widenIVUse(DU, Rewriter, OrigPhi, WidePhi);
2161
2162 // Follow all def-use edges from the previous narrow use.
2163 if (WideUse)
2164 pushNarrowIVUsers(DU.NarrowUse, WideUse);
2165
2166 // widenIVUse may have removed the def-use edge.
2167 if (DU.NarrowDef->use_empty())
2168 DeadInsts.emplace_back(DU.NarrowDef);
2169 }
2170
2171 // Attach any debug information to the new PHI.
2172 replaceAllDbgUsesWith(*OrigPhi, *WidePhi, *WidePhi, *DT);
2173
2174 return WidePhi;
2175}
2176
2177/// Calculates control-dependent range for the given def at the given context
2178/// by looking at dominating conditions inside of the loop
2179void WidenIV::calculatePostIncRange(Instruction *NarrowDef,
2180 Instruction *NarrowUser) {
2181 Value *NarrowDefLHS;
2182 const APInt *NarrowDefRHS;
2183 if (!match(NarrowDef, m_NSWAdd(m_Value(NarrowDefLHS),
2184 m_APInt(NarrowDefRHS))) ||
2185 !NarrowDefRHS->isNonNegative())
2186 return;
2187
2188 auto UpdateRangeFromCondition = [&](Value *Condition, bool TrueDest) {
2189 CmpPredicate Pred;
2190 Value *CmpRHS;
2191 if (!match(Condition, m_ICmp(Pred, m_Specific(NarrowDefLHS),
2192 m_Value(CmpRHS))))
2193 return;
2194
2195 CmpPredicate P = TrueDest ? Pred : ICmpInst::getInverseCmpPredicate(Pred);
2196
2197 auto CmpRHSRange = SE->getSignedRange(SE->getSCEV(CmpRHS));
2198 auto CmpConstrainedLHSRange =
2200 auto NarrowDefRange = CmpConstrainedLHSRange.addWithNoWrap(
2202
2203 updatePostIncRangeInfo(NarrowDef, NarrowUser, NarrowDefRange);
2204 };
2205
2206 auto UpdateRangeFromGuards = [&](Instruction *Ctx) {
2207 if (!HasGuards)
2208 return;
2209
2210 for (Instruction &I : make_range(Ctx->getIterator().getReverse(),
2211 Ctx->getParent()->rend())) {
2212 Value *C = nullptr;
2214 UpdateRangeFromCondition(C, /*TrueDest=*/true);
2215 }
2216 };
2217
2218 UpdateRangeFromGuards(NarrowUser);
2219
2220 BasicBlock *NarrowUserBB = NarrowUser->getParent();
2221 // If NarrowUserBB is statically unreachable asking dominator queries may
2222 // yield surprising results. (e.g. the block may not have a dom tree node)
2223 if (!DT->isReachableFromEntry(NarrowUserBB))
2224 return;
2225
2226 for (auto *DTB = (*DT)[NarrowUserBB]->getIDom();
2227 L->contains(DTB->getBlock());
2228 DTB = DTB->getIDom()) {
2229 auto *BB = DTB->getBlock();
2230 auto *TI = BB->getTerminator();
2231 UpdateRangeFromGuards(TI);
2232
2233 auto *BI = dyn_cast<CondBrInst>(TI);
2234 if (!BI)
2235 continue;
2236
2237 auto *TrueSuccessor = BI->getSuccessor(0);
2238 auto *FalseSuccessor = BI->getSuccessor(1);
2239
2240 if (DT->dominates(BasicBlockEdge(BB, TrueSuccessor), NarrowUserBB))
2241 UpdateRangeFromCondition(BI->getCondition(), /*TrueDest=*/true);
2242
2243 if (DT->dominates(BasicBlockEdge(BB, FalseSuccessor), NarrowUserBB))
2244 UpdateRangeFromCondition(BI->getCondition(), /*TrueDest=*/false);
2245 }
2246}
2247
2248/// Calculates PostIncRangeInfos map for the given IV
2249void WidenIV::calculatePostIncRanges(PHINode *OrigPhi) {
2250 SmallPtrSet<Instruction *, 16> Visited;
2252 Worklist.push_back(OrigPhi);
2253 Visited.insert(OrigPhi);
2254
2255 while (!Worklist.empty()) {
2256 Instruction *NarrowDef = Worklist.pop_back_val();
2257
2258 for (Use &U : NarrowDef->uses()) {
2259 auto *NarrowUser = cast<Instruction>(U.getUser());
2260
2261 // Don't go looking outside the current loop.
2262 auto *NarrowUserLoop = (*LI)[NarrowUser->getParent()];
2263 if (!NarrowUserLoop || !L->contains(NarrowUserLoop))
2264 continue;
2265
2266 if (!Visited.insert(NarrowUser).second)
2267 continue;
2268
2269 Worklist.push_back(NarrowUser);
2270
2271 calculatePostIncRange(NarrowDef, NarrowUser);
2272 }
2273 }
2274}
2275
2277 LoopInfo *LI, ScalarEvolution *SE, SCEVExpander &Rewriter,
2279 unsigned &NumElimExt, unsigned &NumWidened,
2280 bool HasGuards, bool UsePostIncrementRanges) {
2281 WidenIV Widener(WI, LI, SE, DT, DeadInsts, HasGuards, UsePostIncrementRanges);
2282 PHINode *WidePHI = Widener.createWideIV(Rewriter);
2283 NumElimExt = Widener.getNumElimExt();
2284 NumWidened = Widener.getNumWidened();
2285 return WidePHI;
2286}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define DEBUG_TYPE
#define _
iv Induction Variable Users
Definition IVUsers.cpp:48
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define P(N)
SI Fold Operands
static Instruction * GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint)
static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE)
Return true if this instruction generates a simple SCEV expression in terms of that IV.
static Instruction * findCommonDominator(ArrayRef< Instruction * > Instructions, DominatorTree &DT)
Find a point in code which dominates all given instructions.
static Instruction * getInsertPointForUses(Instruction *User, Value *Def, DominatorTree *DT, LoopInfo *LI)
Determine the insertion point for this user.
static std::optional< BinaryOp > matchBinaryOp(Instruction *Op)
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
TTI::PartialReductionExtendKind ExtendKind
virt reg Virtual Register Rewriter
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:330
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:235
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1225
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI bool isSigned() const
Whether the intrinsic is signed or unsigned.
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:409
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition InstrTypes.h:831
bool isSigned() const
Definition InstrTypes.h:993
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
bool isUnsigned() const
Definition InstrTypes.h:999
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
LLVM_ABI unsigned getActiveBits() const
Compute the maximal number of active bits needed to represent every value in this range.
LLVM_ABI APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI unsigned getMinSignedBits() const
Compute the maximal number of bits needed to represent every value in this signed range.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:763
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:767
iterator end()
Definition DenseMap.h:687
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:857
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
bool hasSameSign() const
An icmp instruction, which can be marked as "samesign", indicating that the two operands have the sam...
void setSameSign(bool B=true)
CmpPredicate getCmpPredicate() const
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
Interface for visiting interesting IV users that are recognized but not simplified by this utility.
virtual void anchor()
LLVM_ABI void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
LLVM_ABI void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
iterator_range< user_iterator > users()
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
LLVM_ABI void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition LoopInfo.h:466
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
Definition Operator.h:113
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
Definition Operator.h:107
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This node represents a polynomial recurrence on the trip count of the specified loop.
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
This class uses information about analyze scalars to rewrite expressions in canonical form.
static LLVM_ABI bool canReuseFlagsFromOriginalIVInc(PHINode *OrigPhi, PHINode *WidePhi, Instruction *OrigInc, Instruction *WideInc)
Return true if both increments directly increment the corresponding IV PHI nodes and have the same op...
This class represents an analyzed expression in the program.
static constexpr auto FlagNUW
static constexpr auto FlagNSW
Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
LLVM_ABI const SCEV * getUDivExpr(SCEVUse LHS, SCEVUse RHS)
Get a canonical unsigned division expression, or something simpler if possible.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI const SCEV * getZeroExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags=SCEV::FlagNone, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI SCEVUse getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
LLVM_ABI uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
ConstantRange getSignedRange(const SCEV *S)
Determine the signed range for a particular SCEV.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI std::optional< bool > evaluatePredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Check whether the condition described by Pred, LHS, and RHS is true or false in the given Context.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
static SCEVFlags maskFlags(SCEVFlags Flags, SCEVFlags Mask)
Convenient SCEVFlags manipulation.
LLVM_ABI SCEVUse getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
LLVM_ABI bool haveSameSign(const SCEV *S1, const SCEV *S2)
Return true if we know that S1 and S2 must have the same sign.
LLVM_ABI std::optional< SCEVFlags > getStrengthenedNoWrapFlagsFromBinOp(const OverflowingBinaryOperator *OBO)
Parse NSW/NUW flags from add/sub/mul IR binary operation Op into SCEV no-wrap flags,...
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
LLVM_ABI const SCEV * getSignExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI=nullptr)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L, return a LoopInvaria...
LLVM_ABI SCEVUse getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI bool properlyDominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV properly dominate the specified basic block.
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEVFlags Flags=SCEV::FlagNone)
Return the SCEV object corresponding to -V.
LLVM_ABI bool canReuseInstruction(const SCEV *S, Instruction *I, SmallVectorImpl< Instruction * > &DropPoisonGeneratingInsts)
Check whether it is poison-safe to represent the expression S using the instruction I.
LLVM_ABI bool isKnownPredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
Definition Type.cpp:227
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
void setOperand(unsigned i, Value *Val)
Definition User.h:212
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
bool use_empty() const
Definition Value.h:348
iterator_range< use_iterator > uses()
Definition Value.h:382
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
auto m_Value()
Match an arbitrary value and ignore it.
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
@ User
could "use" a pointer
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
unsigned getOpcode(const VPValue *V)
Return the instruction opcode for the recipe defining V or 0 for unsupported recipes and VPValues not...
This is an optimization pass for GlobalISel generic memory operations.
@ Dead
Unused definition.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ BinaryOp
One of the operands is a binary op.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI PHINode * createWideIV(const WideIVInfo &WI, LoopInfo *LI, ScalarEvolution *SE, SCEVExpander &Rewriter, DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, unsigned &NumElimExt, unsigned &NumWidened, bool HasGuards, bool UsePostIncrementRanges)
Widen Induction Variables - Extend the width of an IV to cover its widest uses.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:406
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
LLVM_ABI bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, const TargetTransformInfo *TTI, SmallVectorImpl< WeakTrackingVH > &Dead)
SimplifyLoopIVs - Simplify users of induction variables within this loop.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
Definition Local.cpp:2448
LLVM_ABI std::pair< bool, bool > simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, const TargetTransformInfo *TTI, SmallVectorImpl< WeakTrackingVH > &Dead, SCEVExpander &Rewriter, IVVisitor *V=nullptr)
simplifyUsersOfIV - Simplify instructions that use this induction variable by using ScalarEvolution t...
TargetTransformInfo TTI
DWARFExpression::Operation Op
constexpr unsigned BitWidth
LLVM_ABI bool formLCSSAForInstructions(SmallVectorImpl< Instruction * > &Worklist, const DominatorTree &DT, const LoopInfo &LI, ScalarEvolution *SE, SmallVectorImpl< PHINode * > *PHIsToRemove=nullptr, SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
Ensures LCSSA form for every instruction from the Worklist in the scope of innermost containing loop.
Definition LCSSA.cpp:328
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
Collect information about induction variables that are used by sign/zero extend operations.