80using namespace PatternMatch;
81using namespace SCEVPatternMatch;
83#define DEBUG_TYPE "indvars"
86STATISTIC(NumReplaced ,
"Number of exit values replaced");
87STATISTIC(NumLFTR ,
"Number of loop exit tests replaced");
88STATISTIC(NumElimExt ,
"Number of IV sign/zero extends eliminated");
89STATISTIC(NumElimIV ,
"Number of congruent IVs eliminated");
93 cl::desc(
"Choose the strategy to replace exit value in IndVarSimplify"),
97 "only replace exit value when the cost is cheap"),
100 "only replace exit value when it is an unused "
101 "induction variable in the loop and has cheap replacement cost"),
103 "only replace exit values when loop def likely dead"),
105 "always replace exit value whenever possible")));
109 cl::desc(
"Use post increment control-dependent ranges in IndVarSimplify"),
114 cl::desc(
"Disable Linear Function Test Replace optimization"));
118 cl::desc(
"Predicate conditions in read only loops"));
122 cl::desc(
"Allow widening of indvars to eliminate s/zext"));
126class IndVarSimplify {
133 std::unique_ptr<MemorySSAUpdater> MSSAU;
138 bool RunUnswitching =
false;
141 bool rewriteNonIntegerIVs(
Loop *L);
147 bool canonicalizeExitCondition(
Loop *L);
154 bool rewriteFirstIterationLoopExitValues(
Loop *L);
157 const SCEV *ExitCount,
160 bool sinkUnusedInvariants(
Loop *L);
166 : LI(LI), SE(SE), DT(DT),
DL(
DL), TLI(TLI),
TTI(
TTI),
167 WidenIndVars(WidenIndVars) {
169 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
174 bool runUnswitching()
const {
return RunUnswitching; }
185 bool isExact =
false;
189 APFloat::rmTowardZero, &isExact) != APFloat::opOK ||
204bool IndVarSimplify::handleFloatingPointIV(
Loop *L,
PHINode *PN) {
206 unsigned BackEdge = IncomingEdge^1;
209 auto *InitValueVal = dyn_cast<ConstantFP>(PN->
getIncomingValue(IncomingEdge));
212 if (!InitValueVal || !
ConvertToSInt(InitValueVal->getValueAPF(), InitValue))
218 if (Incr ==
nullptr || Incr->getOpcode() != Instruction::FAdd)
return false;
222 ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1));
224 if (IncValueVal ==
nullptr || Incr->getOperand(0) != PN ||
232 if (IncrUse == Incr->user_end())
return false;
234 if (IncrUse != Incr->user_end())
return false;
240 Compare = dyn_cast<FCmpInst>(U2);
241 if (!Compare || !
Compare->hasOneUse() ||
242 !isa<BranchInst>(
Compare->user_back()))
261 if (ExitValueVal ==
nullptr ||
267 switch (
Compare->getPredicate()) {
268 default:
return false;
290 if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue))
301 if (InitValue >= ExitValue)
308 if (++
Range == 0)
return false;
322 if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue)
327 if (InitValue <= ExitValue)
334 if (++
Range == 0)
return false;
348 if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue)
363 Incr->
getName() +
".int", Incr->getIterator());
379 Compare->replaceAllUsesWith(NewCompare);
403bool IndVarSimplify::rewriteNonIntegerIVs(
Loop *L) {
411 bool Changed =
false;
413 if (
PHINode *PN = dyn_cast_or_null<PHINode>(&*
PHI))
414 Changed |= handleFloatingPointIV(L, PN);
433bool IndVarSimplify::rewriteFirstIterationLoopExitValues(
Loop *L) {
438 L->getUniqueExitBlocks(ExitBlocks);
440 bool MadeAnyChanges =
false;
441 for (
auto *ExitBB : ExitBlocks) {
444 for (
PHINode &PN : ExitBB->phis()) {
446 IncomingValIdx != E; ++IncomingValIdx) {
454 if (!
L->getLoopLatch() ||
455 !DT->
dominates(IncomingBB,
L->getLoopLatch()))
462 if (
auto *BI = dyn_cast<BranchInst>(TermInst)) {
465 Cond = BI->getCondition();
466 }
else if (
auto *SI = dyn_cast<SwitchInst>(TermInst))
467 Cond =
SI->getCondition();
471 if (!
L->isLoopInvariant(
Cond))
477 if (!ExitVal || ExitVal->getParent() !=
L->getHeader())
483 auto *LoopPreheader =
L->getLoopPreheader();
484 assert(LoopPreheader &&
"Invalid loop");
485 int PreheaderIdx = ExitVal->getBasicBlockIndex(LoopPreheader);
486 if (PreheaderIdx != -1) {
487 assert(ExitVal->getParent() ==
L->getHeader() &&
488 "ExitVal must be in loop header");
489 MadeAnyChanges =
true;
491 ExitVal->getIncomingValue(PreheaderIdx));
492 SE->forgetValue(&PN);
497 return MadeAnyChanges;
510 bool IsSigned = Cast->
getOpcode() == Instruction::SExt;
511 if (!IsSigned && Cast->
getOpcode() != Instruction::ZExt)
524 if (NarrowIVWidth >= Width)
564class IndVarSimplifyVisitor :
public IVVisitor {
591bool IndVarSimplify::simplifyAndExtend(
Loop *L,
597 L->getBlocks()[0]->getModule(), Intrinsic::experimental_guard);
598 bool HasGuards = GuardDecl && !GuardDecl->use_empty();
607 bool Changed =
false;
608 while (!LoopPhis.empty()) {
616 PHINode *CurrIV = LoopPhis.pop_back_val();
619 IndVarSimplifyVisitor Visitor(CurrIV, SE,
TTI, DT);
626 if (Visitor.WI.WidestNativeType) {
629 }
while(!LoopPhis.empty());
639 DT, DeadInsts, ElimExt, Widened,
641 NumElimExt += ElimExt;
642 NumWidened += Widened;
644 LoopPhis.push_back(WidePhi);
664 case Instruction::Add:
665 case Instruction::Sub:
667 case Instruction::GetElementPtr:
677 if (Phi && Phi->getParent() == L->getHeader()) {
682 if (IncI->
getOpcode() == Instruction::GetElementPtr)
687 if (Phi && Phi->getParent() == L->getHeader()) {
710 assert(L->getLoopLatch() &&
"Must be in simplified form");
727 if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
733 if (!L->isLoopInvariant(
RHS)) {
734 if (!L->isLoopInvariant(
LHS))
747 int Idx = Phi->getBasicBlockIndex(L->getLoopLatch());
752 Value *IncV = Phi->getIncomingValue(
Idx);
761 if (isa<Constant>(V))
762 return !isa<UndefValue>(V);
774 if(
I->mayReadFromMemory() || isa<CallInst>(
I) || isa<InvokeInst>(
I))
803 assert(Phi->getParent() == L->getHeader());
804 assert(L->getLoopLatch());
813 int LatchIdx = Phi->getBasicBlockIndex(L->getLoopLatch());
814 Value *IncV = Phi->getIncomingValue(LatchIdx);
816 isa<SCEVAddRecExpr>(SE->
getSCEV(IncV)));
835 const SCEV *BestInit =
nullptr;
837 assert(LatchBlock &&
"Must be in simplified form");
845 const auto *AR = cast<SCEVAddRecExpr>(SE->
getSCEV(Phi));
851 if (PhiWidth < BCWidth || !
DL.isLegalInteger(PhiWidth))
860 Value *IncPhi = Phi->getIncomingValueForBlock(LatchBlock);
874 if (!Phi->getType()->isIntegerTy() &&
894 else if (PhiWidth <= SE->getTypeSizeInBits(BestPhi->
getType()))
907 const SCEV *ExitCount,
bool UsePostInc,
Loop *L,
923 if (!isa<SCEVConstant>(IVInit) || !isa<SCEVConstant>(ExitCount))
930 "Computed iteration count is not loop invariant!");
942 const SCEV *ExitCount,
944 assert(
L->getLoopLatch() &&
"Loop no longer in simplified form?");
950 Value *CmpIndVar = IndVar;
951 bool UsePostInc =
false;
956 if (ExitingBB ==
L->getLoopLatch()) {
983 if (
auto *BO = dyn_cast<BinaryOperator>(IncVar)) {
984 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IncVar));
985 if (BO->hasNoUnsignedWrap())
987 if (BO->hasNoSignedWrap())
992 IndVar, ExitingBB, ExitCount, UsePostInc, L, Rewriter, SE);
995 "genLoopLimit missed a cast");
1001 P = ICmpInst::ICMP_NE;
1003 P = ICmpInst::ICMP_EQ;
1010 Builder.SetCurrentDebugLocation(
Cond->getDebugLoc());
1017 unsigned CmpIndVarSize = SE->getTypeSizeInBits(CmpIndVar->
getType());
1018 unsigned ExitCntSize = SE->getTypeSizeInBits(ExitCnt->
getType());
1019 if (CmpIndVarSize > ExitCntSize) {
1029 const SCEV *
IV = SE->getSCEV(CmpIndVar);
1030 const SCEV *TruncatedIV = SE->getTruncateExpr(
IV, ExitCnt->
getType());
1031 const SCEV *ZExtTrunc =
1032 SE->getZeroExtendExpr(TruncatedIV, CmpIndVar->
getType());
1034 if (ZExtTrunc ==
IV) {
1036 ExitCnt = Builder.CreateZExt(ExitCnt, IndVar->
getType(),
1039 const SCEV *SExtTrunc =
1040 SE->getSignExtendExpr(TruncatedIV, CmpIndVar->
getType());
1041 if (SExtTrunc ==
IV) {
1043 ExitCnt = Builder.CreateSExt(ExitCnt, IndVar->
getType(),
1050 L->makeLoopInvariant(ExitCnt, Discard);
1052 CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->
getType(),
1055 LLVM_DEBUG(
dbgs() <<
"INDVARS: Rewriting loop exit condition to:\n"
1056 <<
" LHS:" << *CmpIndVar <<
'\n'
1057 <<
" op:\t" << (
P == ICmpInst::ICMP_NE ?
"!=" :
"==")
1059 <<
" RHS:\t" << *ExitCnt <<
"\n"
1060 <<
"ExitCount:\t" << *ExitCount <<
"\n"
1063 Value *
Cond = Builder.CreateICmp(
P, CmpIndVar, ExitCnt,
"exitcond");
1071 DeadInsts.emplace_back(OrigCond);
1084bool IndVarSimplify::sinkUnusedInvariants(
Loop *L) {
1086 if (!ExitBlock)
return false;
1089 if (!Preheader)
return false;
1091 bool MadeAnyChanges =
false;
1099 if (isa<PHINode>(
I))
1108 if (
I.mayHaveSideEffects() ||
I.mayReadFromMemory())
1112 if (
I.isDebugOrPseudoInst())
1123 if (isa<AllocaInst>(&
I))
1128 bool UsedInLoop =
false;
1129 for (
Use &U :
I.uses()) {
1135 UseBB =
P->getIncomingBlock(i);
1137 if (UseBB == Preheader ||
L->contains(UseBB)) {
1149 SE->forgetValue(&
I);
1150 MadeAnyChanges =
true;
1153 return MadeAnyChanges;
1159 LLVM_DEBUG(
dbgs() <<
"Replacing condition of loop-exiting branch " << *BI
1160 <<
" with " << *NewCond <<
"\n");
1162 if (OldCond->use_empty())
1169 bool ExitIfTrue = !L->contains(*
succ_begin(ExitingBB));
1171 return ConstantInt::get(OldCond->getType(),
1172 IsTaken ? ExitIfTrue : !ExitIfTrue);
1185 assert(L->isLoopSimplifyForm() &&
"Should only do it in simplify form!");
1186 auto *LoopPreheader = L->getLoopPreheader();
1187 auto *LoopHeader = L->getHeader();
1189 for (
auto &PN : LoopHeader->phis()) {
1192 Worklist.
push_back(cast<Instruction>(U));
1201 while (!Worklist.
empty()) {
1203 if (!Visited.
insert(
I).second)
1207 if (!L->contains(
I))
1212 for (
User *U :
I->users())
1213 Worklist.
push_back(cast<Instruction>(U));
1214 I->replaceAllUsesWith(Res);
1225 BasicBlock *Preheader = L->getLoopPreheader();
1226 assert(Preheader &&
"Preheader doesn't exist");
1230 bool ExitIfTrue = !L->contains(*
succ_begin(ExitingBB));
1232 InvariantPred = ICmpInst::getInversePredicate(InvariantPred);
1235 return Builder.
CreateICmp(InvariantPred, LHSV, RHSV,
1239static std::optional<Value *>
1241 const SCEV *MaxIter,
bool Inverted,
bool SkipLastIter,
1259 auto *MaxIterTy = MaxIter->
getType();
1276 if (
auto *
UMin = dyn_cast<SCEVUMinExpr>(MaxIter)) {
1288 return std::nullopt;
1303 "Not a loop exit!");
1316 auto GoThrough = [&](
Value *V) {
1337 if (!GoThrough(Curr))
1338 if (
auto *ICmp = dyn_cast<ICmpInst>(Curr))
1340 }
while (!Worklist.
empty());
1347 if (!SkipLastIter && LeafConditions.
size() > 1 &&
1349 ScalarEvolution::ExitCountKind::SymbolicMaximum) ==
1351 for (
auto *ICmp : LeafConditions) {
1354 const SCEV *ExitMax = EL.SymbolicMaxNotTaken;
1355 if (isa<SCEVCouldNotCompute>(ExitMax))
1363 if (WideExitMax == WideMaxIter)
1364 ICmpsFailingOnLastIter.
insert(ICmp);
1367 bool Changed =
false;
1368 for (
auto *OldCond : LeafConditions) {
1373 bool OptimisticSkipLastIter = SkipLastIter;
1374 if (!OptimisticSkipLastIter) {
1375 if (ICmpsFailingOnLastIter.
size() > 1)
1376 OptimisticSkipLastIter =
true;
1377 else if (ICmpsFailingOnLastIter.
size() == 1)
1378 OptimisticSkipLastIter = !ICmpsFailingOnLastIter.
count(OldCond);
1382 OptimisticSkipLastIter, SE,
Rewriter)) {
1384 auto *NewCond = *Replaced;
1385 if (
auto *NCI = dyn_cast<Instruction>(NewCond)) {
1386 NCI->setName(OldCond->
getName() +
".first_iter");
1388 LLVM_DEBUG(
dbgs() <<
"Unknown exit count: Replacing " << *OldCond
1389 <<
" with " << *NewCond <<
"\n");
1395 ICmpsFailingOnLastIter.
erase(OldCond);
1401bool IndVarSimplify::canonicalizeExitCondition(
Loop *L) {
1412 L->getExitingBlocks(ExitingBlocks);
1413 bool Changed =
false;
1414 for (
auto *ExitingBB : ExitingBlocks) {
1415 auto *BI = dyn_cast<BranchInst>(ExitingBB->
getTerminator());
1421 if (!ICmp || !ICmp->hasOneUse())
1424 auto *
LHS = ICmp->getOperand(0);
1425 auto *
RHS = ICmp->getOperand(1);
1429 if (!
L->isLoopInvariant(RHS)) {
1430 if (!
L->isLoopInvariant(LHS))
1437 Value *LHSOp =
nullptr;
1441 const unsigned InnerBitWidth =
DL.getTypeSizeInBits(LHSOp->
getType());
1442 const unsigned OuterBitWidth =
DL.getTypeSizeInBits(
RHS->
getType());
1443 auto FullCR = ConstantRange::getFull(InnerBitWidth);
1444 FullCR = FullCR.zeroExtend(OuterBitWidth);
1445 auto RHSCR = SE->getUnsignedRange(SE->applyLoopGuards(SE->getSCEV(RHS), L));
1446 if (FullCR.contains(RHSCR)) {
1449 ICmp->setPredicate(ICmp->getUnsignedPredicate());
1459 for (
auto *ExitingBB : ExitingBlocks) {
1460 auto *BI = dyn_cast<BranchInst>(ExitingBB->
getTerminator());
1466 if (!ICmp || !ICmp->hasOneUse() || !ICmp->isUnsigned())
1469 bool Swapped =
false;
1470 auto *
LHS = ICmp->getOperand(0);
1471 auto *
RHS = ICmp->getOperand(1);
1472 if (
L->isLoopInvariant(LHS) ==
L->isLoopInvariant(RHS))
1475 if (
L->isLoopInvariant(LHS)) {
1481 assert(!
L->isLoopInvariant(LHS) &&
L->isLoopInvariant(RHS));
1486 Value *LHSOp =
nullptr;
1496 if (!
LHS->
hasOneUse() && !isa<SCEVAddRecExpr>(SE->getSCEV(LHSOp)))
1503 auto doRotateTransform = [&]() {
1504 assert(ICmp->isUnsigned() &&
"must have proven unsigned already");
1506 Instruction::Trunc, RHS, LHSOp->
getType(),
"",
1507 L->getLoopPreheader()->getTerminator()->getIterator());
1511 ICmp->setOperand(Swapped ? 1 : 0, LHSOp);
1512 ICmp->setOperand(Swapped ? 0 : 1, NewRHS);
1514 ICmp->setSameSign(
false);
1516 DeadInsts.push_back(LHS);
1519 const unsigned InnerBitWidth =
DL.getTypeSizeInBits(LHSOp->
getType());
1520 const unsigned OuterBitWidth =
DL.getTypeSizeInBits(
RHS->
getType());
1521 auto FullCR = ConstantRange::getFull(InnerBitWidth);
1522 FullCR = FullCR.zeroExtend(OuterBitWidth);
1523 auto RHSCR = SE->getUnsignedRange(SE->applyLoopGuards(SE->getSCEV(RHS), L));
1524 if (FullCR.contains(RHSCR)) {
1525 doRotateTransform();
1539 L->getExitingBlocks(ExitingBlocks);
1556 if (!DT->
dominates(ExitingBB,
L->getLoopLatch()))
1563 if (!L->contains(BI->getSuccessor(CI->isNullValue())))
1564 replaceLoopPHINodesWithPreheaderValues(LI, L, DeadInsts, *SE);
1571 if (ExitingBlocks.
empty())
1575 const SCEV *MaxBECount = SE->getSymbolicMaxBackedgeTakenCount(L);
1576 if (isa<SCEVCouldNotCompute>(MaxBECount))
1585 if (
A ==
B)
return false;
1589 assert(DT->properlyDominates(B, A) &&
1590 "expected total dominance order!");
1595 for (
unsigned i = 1; i < ExitingBlocks.
size(); i++) {
1600 bool Changed =
false;
1601 bool SkipLastIter =
false;
1602 const SCEV *CurrMaxExit = SE->getCouldNotCompute();
1603 auto UpdateSkipLastIter = [&](
const SCEV *MaxExitCount) {
1604 if (SkipLastIter || isa<SCEVCouldNotCompute>(MaxExitCount))
1606 if (isa<SCEVCouldNotCompute>(CurrMaxExit))
1607 CurrMaxExit = MaxExitCount;
1609 CurrMaxExit = SE->getUMinFromMismatchedTypes(CurrMaxExit, MaxExitCount);
1612 if (CurrMaxExit == MaxBECount)
1613 SkipLastIter =
true;
1616 for (
BasicBlock *ExitingBB : ExitingBlocks) {
1617 const SCEV *ExactExitCount = SE->getExitCount(L, ExitingBB);
1618 const SCEV *MaxExitCount = SE->getExitCount(
1619 L, ExitingBB, ScalarEvolution::ExitCountKind::SymbolicMaximum);
1620 if (isa<SCEVCouldNotCompute>(ExactExitCount)) {
1624 auto OptimizeCond = [&](
bool SkipLastIter) {
1626 MaxBECount, SkipLastIter,
1627 SE, Rewriter, DeadInsts);
1646 if (OptimizeCond(
false))
1648 else if (SkipLastIter && OptimizeCond(
true))
1650 UpdateSkipLastIter(MaxExitCount);
1654 UpdateSkipLastIter(ExactExitCount);
1661 if (ExactExitCount->
isZero()) {
1662 foldExit(L, ExitingBB,
true, DeadInsts);
1670 "Exit counts must be integers");
1673 SE->getWiderType(MaxBECount->
getType(), ExactExitCount->
getType());
1674 ExactExitCount = SE->getNoopOrZeroExtend(ExactExitCount, WiderType);
1675 MaxBECount = SE->getNoopOrZeroExtend(MaxBECount, WiderType);
1682 foldExit(L, ExitingBB,
false, DeadInsts);
1691 if (!DominatingExactExitCounts.
insert(ExactExitCount).second) {
1692 foldExit(L, ExitingBB,
false, DeadInsts);
1709 L->getExitingBlocks(ExitingBlocks);
1726 const SCEV *ExactBTC = SE->getBackedgeTakenCount(L);
1727 if (isa<SCEVCouldNotCompute>(ExactBTC) || !
Rewriter.isSafeToExpand(ExactBTC))
1730 assert(SE->isLoopInvariant(ExactBTC, L) &&
"BTC must be loop invariant");
1754 if (!ExitBlock->
phis().empty())
1757 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
1758 if (isa<SCEVCouldNotCompute>(ExitCount) ||
1759 !
Rewriter.isSafeToExpand(ExitCount))
1762 assert(SE->isLoopInvariant(ExitCount, L) &&
1763 "Exit count must be loop invariant");
1796 for (
unsigned i = 1; i < ExitingBlocks.size(); i++)
1798 "Not sorted by dominance");
1802 for (
unsigned i = 0, e = ExitingBlocks.size(); i < e; i++)
1803 if (BadExit(ExitingBlocks[i])) {
1804 ExitingBlocks.resize(i);
1808 if (ExitingBlocks.empty())
1822 if (
I.mayHaveSideEffects())
1825 bool Changed =
false;
1836 Rewriter.setInsertPoint(
L->getLoopPreheader()->getTerminator());
1838 Value *ExactBTCV =
nullptr;
1839 for (
BasicBlock *ExitingBB : ExitingBlocks) {
1840 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
1844 if (ExitCount == ExactBTC) {
1846 B.getFalse() :
B.getTrue();
1850 ExactBTCV =
Rewriter.expandCodeFor(ExactBTC);
1854 ECV =
B.CreateZExt(ECV, WiderTy);
1855 RHS =
B.CreateZExt(RHS, WiderTy);
1858 ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
1859 NewCond =
B.CreateICmp(Pred, ECV, RHS);
1864 DeadInsts.emplace_back(OldCond);
1866 RunUnswitching =
true;
1876bool IndVarSimplify::run(
Loop *L) {
1878 assert(
L->isRecursivelyLCSSAForm(*DT, *LI) &&
1879 "LCSSA required to run indvars!");
1890 if (!
L->isLoopSimplifyForm())
1893 bool Changed =
false;
1896 Changed |= rewriteNonIntegerIVs(L);
1900#if LLVM_ENABLE_ABI_BREAKING_CHECKS
1911 Changed |= simplifyAndExtend(L, Rewriter, LI);
1920 NumReplaced += Rewrites;
1926 NumElimIV +=
Rewriter.replaceCongruentIVs(L, DT, DeadInsts,
TTI);
1930 Changed |= canonicalizeExitCondition(L);
1933 if (optimizeLoopExits(L, Rewriter)) {
1938 SE->forgetTopmostLoop(L);
1943 if (predicateLoopExits(L, Rewriter)) {
1955 L->getExitingBlocks(ExitingBlocks);
1956 for (
BasicBlock *ExitingBB : ExitingBlocks) {
1970 const SCEV *ExitCount = SE->getExitCount(L, ExitingBB);
1971 if (isa<SCEVCouldNotCompute>(ExitCount))
1991 if (!
Rewriter.isSafeToExpand(ExitCount))
1994 Changed |= linearFunctionTestReplace(L, ExitingBB,
2006 while (!DeadInsts.empty()) {
2007 Value *
V = DeadInsts.pop_back_val();
2009 if (
PHINode *
PHI = dyn_cast_or_null<PHINode>(V))
2011 else if (
Instruction *Inst = dyn_cast_or_null<Instruction>(V))
2020 Changed |= sinkUnusedInvariants(L);
2025 Changed |= rewriteFirstIterationLoopExitValues(L);
2031 assert(
L->isRecursivelyLCSSAForm(*DT, *LI) &&
2032 "Indvars did not preserve LCSSA!");
2034 MSSAU->getMemorySSA()->verifyMemorySSA();
2042 Function *
F = L.getHeader()->getParent();
2052 if (IVS.runUnswitching()) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
static Value * genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB, const SCEV *ExitCount, bool UsePostInc, Loop *L, SCEVExpander &Rewriter, ScalarEvolution *SE)
Insert an IR expression which computes the value held by the IV IndVar (which must be an loop counter...
static void replaceExitCond(BranchInst *BI, Value *NewCond, SmallVectorImpl< WeakTrackingVH > &DeadInsts)
static cl::opt< bool > DisableLFTR("disable-lftr", cl::Hidden, cl::init(false), cl::desc("Disable Linear Function Test Replace optimization"))
static bool isLoopExitTestBasedOn(Value *V, BasicBlock *ExitingBB)
Whether the current loop exit test is based on this value.
static cl::opt< ReplaceExitVal > ReplaceExitValue("replexitval", cl::Hidden, cl::init(OnlyCheapRepl), cl::desc("Choose the strategy to replace exit value in IndVarSimplify"), cl::values(clEnumValN(NeverRepl, "never", "never replace exit value"), clEnumValN(OnlyCheapRepl, "cheap", "only replace exit value when the cost is cheap"), clEnumValN(UnusedIndVarInLoop, "unusedindvarinloop", "only replace exit value when it is an unused " "induction variable in the loop and has cheap replacement cost"), clEnumValN(NoHardUse, "noharduse", "only replace exit values when loop def likely dead"), clEnumValN(AlwaysRepl, "always", "always replace exit value whenever possible")))
static void visitIVCast(CastInst *Cast, WideIVInfo &WI, ScalarEvolution *SE, const TargetTransformInfo *TTI)
Update information about the induction variable that is extended by this sign or zero extend operatio...
static void replaceLoopPHINodesWithPreheaderValues(LoopInfo *LI, Loop *L, SmallVectorImpl< WeakTrackingVH > &DeadInsts, ScalarEvolution &SE)
static bool needsLFTR(Loop *L, BasicBlock *ExitingBB)
linearFunctionTestReplace policy.
static bool optimizeLoopExitWithUnknownExitCount(const Loop *L, BranchInst *BI, BasicBlock *ExitingBB, const SCEV *MaxIter, bool SkipLastIter, ScalarEvolution *SE, SCEVExpander &Rewriter, SmallVectorImpl< WeakTrackingVH > &DeadInsts)
static Value * createInvariantCond(const Loop *L, BasicBlock *ExitingBB, const ScalarEvolution::LoopInvariantPredicate &LIP, SCEVExpander &Rewriter)
static bool isLoopCounter(PHINode *Phi, Loop *L, ScalarEvolution *SE)
Return true if the given phi is a "counter" in L.
static std::optional< Value * > createReplacement(ICmpInst *ICmp, const Loop *L, BasicBlock *ExitingBB, const SCEV *MaxIter, bool Inverted, bool SkipLastIter, ScalarEvolution *SE, SCEVExpander &Rewriter)
static bool hasConcreteDefImpl(Value *V, SmallPtrSetImpl< Value * > &Visited, unsigned Depth)
Recursive helper for hasConcreteDef().
static bool hasConcreteDef(Value *V)
Return true if the given value is concrete.
static void foldExit(const Loop *L, BasicBlock *ExitingBB, bool IsTaken, SmallVectorImpl< WeakTrackingVH > &DeadInsts)
static PHINode * getLoopPhiForCounter(Value *IncV, Loop *L)
Given an Value which is hoped to be part of an add recurance in the given loop, return the associated...
static Constant * createFoldedExitCond(const Loop *L, BasicBlock *ExitingBB, bool IsTaken)
static cl::opt< bool > UsePostIncrementRanges("indvars-post-increment-ranges", cl::Hidden, cl::desc("Use post increment control-dependent ranges in IndVarSimplify"), cl::init(true))
static PHINode * FindLoopCounter(Loop *L, BasicBlock *ExitingBB, const SCEV *BECount, ScalarEvolution *SE, DominatorTree *DT)
Search the loop header for a loop counter (anadd rec w/step of one) suitable for use by LFTR.
static cl::opt< bool > AllowIVWidening("indvars-widen-indvars", cl::Hidden, cl::init(true), cl::desc("Allow widening of indvars to eliminate s/zext"))
static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal)
Convert APF to an integer, if possible.
static cl::opt< bool > LoopPredication("indvars-predicate-loops", cl::Hidden, cl::init(true), cl::desc("Predicate conditions in read only loops"))
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallPtrSet class.
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)
Virtual Register Rewriter
static const uint32_t IV[8]
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
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...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Conditional or Unconditional Branch instruction.
void setCondition(Value *V)
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
Value * getCondition() const
Represents analyses that only rely on functions' control flow.
This is the base class for all instructions that perform data casts.
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
@ ICMP_SLT
signed less than
@ ICMP_SLE
signed less or equal
@ FCMP_OLT
0 1 0 0 True if ordered and less than
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
@ ICMP_SGT
signed greater than
@ FCMP_ULT
1 1 0 0 True if unordered or less than
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
@ ICMP_ULT
unsigned less than
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
@ ICMP_SGE
signed greater or equal
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
ConstantFP - Floating Point Values [float, double].
const APFloat & getValueAPF() const
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
This is an important base class in LLVM.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
static DebugLoc getDropped()
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
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.
This instruction compares its operands according to the predicate given to the constructor.
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Interface for visiting interesting IV users that are recognized but not simplified by this utility.
virtual void visitCast(CastInst *Cast)=0
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Class to represent integer types.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
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.
Represents a single loop in the control flow graph.
An analysis that produces MemorySSA for a function.
Encapsulates MemorySSA, including all data associated with memory accesses.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingValue(unsigned i, Value *V)
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static unsigned getIncomingValueNumForOperand(unsigned i)
unsigned getNumIncomingValues() const
Return the number of incoming edges.
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.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
This node represents a polynomial recurrence on the trip count of the specified loop.
const SCEV * getStart() const
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
LLVM_ABI const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
Return an expression representing the value of this expression one iteration of the loop ahead.
This class uses information about analyze scalars to rewrite expressions in canonical form.
bool hasNoUnsignedWrap() const
bool hasNoSignedWrap() const
This class represents an analyzed expression in the program.
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
This class represents a cast from signed integer to floating point.
The main scalar evolution driver.
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterations(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L at given Context duri...
LLVM_ABI Type * getWiderType(Type *Ty1, Type *Ty2) const
LLVM_ABI const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
LLVM_ABI ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, bool AllowPredicates=false)
Compute the number of times the backedge of the specified loop will execute if its exit condition wer...
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.
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.
LLVM_ABI const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
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 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 * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
const SCEV * getMinusOne(Type *Ty)
Return a SCEV for the constant -1 of a specific type.
LLVM_ABI const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
Promote the operands to the wider of the types using zero-extension, and then perform a umin operatio...
LLVM_ABI const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
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,...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
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 is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isPointerTy() const
True if this is an instance of PointerType.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
iterator_range< user_iterator > users()
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
user_iterator_impl< User > user_iterator
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Value handle that is nullable, but tries to track the Value.
const ParentTy * getParent() const
self_iterator getIterator()
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
specificloop_ty m_SpecificLoop(const Loop *L)
SCEVAffineAddRec_match< Op0_t, Op1_t, class_match< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
bool match(const SCEV *S, const Pattern &P)
class_match< const SCEV > m_SCEV()
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
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.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
LLVM_ABI bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Examine each PHI in the given block and delete it if it is dead.
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
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...
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
LLVM_ABI bool isAlmostDeadIV(PHINode *IV, BasicBlock *LatchBlock, Value *Cond)
Return true if the induction variable IV in a Loop whose latch is LatchBlock would become dead if the...
LLVM_ABI int rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI, ScalarEvolution *SE, const TargetTransformInfo *TTI, SCEVExpander &Rewriter, DominatorTree *DT, ReplaceExitVal ReplaceExitValue, SmallVector< WeakTrackingVH, 16 > &DeadInsts)
If the final value of any expressions that are recurrent in the loop can be computed,...
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
LLVM_ABI bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
TargetTransformInfo & TTI
Collect information about induction variables that are used by sign/zero extend operations.