32#if LLVM_ENABLE_ABI_BREAKING_CHECKS
33#define SCEV_DEBUG_WITH_TYPE(TYPE, X) DEBUG_WITH_TYPE(TYPE, X)
35#define SCEV_DEBUG_WITH_TYPE(TYPE, X)
42 cl::desc(
"When performing SCEV expansion only if it is cheap to do, this "
43 "controls the budget that is considered cheap (default = 4)"));
57 NUW = OBO->hasNoUnsignedWrap();
58 NSW = OBO->hasNoSignedWrap();
61 Exact = PEO->isExact();
65 NNeg = PNI->hasNonNeg();
67 NUW = TI->hasNoUnsignedWrap();
68 NSW = TI->hasNoSignedWrap();
78 I->setHasNoUnsignedWrap(
NUW);
79 I->setHasNoSignedWrap(
NSW);
88 I->setHasNoUnsignedWrap(
NUW);
89 I->setHasNoSignedWrap(
NSW);
114 Value *Ret =
nullptr;
119 if (U->getType() != Ty)
128 if (IP->getParent() == CI->
getParent() && &*BIP != CI &&
138 SCEVInsertPointGuard Guard(Builder,
this);
139 Builder.SetInsertPoint(&*IP);
140 Ret = Builder.CreateCast(
Op, V, Ty,
V->getName());
157 IP =
II->getNormalDest()->begin();
165 IP = MustDominate->
getParent()->getFirstInsertionPt();
167 assert(!IP->isEHPad() &&
"unexpected eh pad!");
183 while (!WorkList.
empty()) {
192 InsertedValues.erase(
I);
193 InsertedPostIncValues.erase(
I);
195 I->eraseFromParent();
200SCEVExpander::GetOptimalInsertionPointForCastOf(
Value *V)
const {
219 "Expected the cast argument to be a global/constant");
220 return Builder.GetInsertBlock()
223 .getFirstInsertionPt();
231 assert((
Op == Instruction::BitCast ||
232 Op == Instruction::PtrToInt ||
233 Op == Instruction::IntToPtr) &&
234 "InsertNoopCastOfTo cannot perform non-noop casts!");
235 assert(SE.getTypeSizeInBits(
V->getType()) == SE.getTypeSizeInBits(Ty) &&
236 "InsertNoopCastOfTo cannot change sizes!");
243 if (
Op == Instruction::IntToPtr) {
245 if (DL.isNonIntegralPointerType(PtrTy))
249 if (
Op == Instruction::BitCast) {
250 if (
V->getType() == Ty)
258 if ((
Op == Instruction::PtrToInt ||
Op == Instruction::IntToPtr) &&
259 SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(
V->getType())) {
261 if ((CI->
getOpcode() == Instruction::PtrToInt ||
262 CI->
getOpcode() == Instruction::IntToPtr) &&
263 SE.getTypeSizeInBits(CI->
getType()) ==
267 if ((
CE->getOpcode() == Instruction::PtrToInt ||
268 CE->getOpcode() == Instruction::IntToPtr) &&
269 SE.getTypeSizeInBits(
CE->getType()) ==
270 SE.getTypeSizeInBits(
CE->getOperand(0)->getType()))
271 return CE->getOperand(0);
279 return ReuseOrCreateCast(V, Ty,
Op, GetOptimalInsertionPointForCastOf(V));
295 unsigned ScanLimit = 6;
299 if (IP != BlockBegin) {
301 for (; ScanLimit; --IP, --ScanLimit) {
316 if (IP->getOpcode() == (
unsigned)Opcode && IP->getOperand(0) ==
LHS &&
317 IP->getOperand(1) ==
RHS && !canGenerateIncompatiblePoison(&*IP))
319 if (IP == BlockBegin)
break;
324 DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc();
325 SCEVInsertPointGuard Guard(Builder,
this);
329 while (
const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
330 if (!
L->isLoopInvariant(
LHS) || !
L->isLoopInvariant(
RHS))
break;
332 if (!Preheader)
break;
391 return Builder.CreatePtrAdd(CLHS, CRHS,
"", NW);
394 unsigned ScanLimit = 6;
398 if (IP != BlockBegin) {
400 for (; ScanLimit; --IP, --ScanLimit) {
402 if (
GEP->getPointerOperand() == V &&
403 GEP->getSourceElementType() == Builder.getInt8Ty() &&
404 GEP->getOperand(1) == Idx) {
406 GEP->setNoWrapFlags(
GEP->getNoWrapFlags() & NW);
410 if (IP == BlockBegin)
break;
415 SCEVInsertPointGuard Guard(Builder,
this);
418 while (
const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
419 if (!
L->isLoopInvariant(V) || !
L->isLoopInvariant(Idx))
break;
421 if (!Preheader)
break;
428 return Builder.CreatePtrAdd(V, Idx,
"scevgep", NW);
438 if (
A->contains(
B))
return B;
439 if (
B->contains(
A))
return A;
440 if (DT.
dominates(
A->getHeader(),
B->getHeader()))
return B;
441 if (DT.
dominates(
B->getHeader(),
A->getHeader()))
return A;
447const Loop *SCEVExpander::getRelevantLoop(
const SCEV *S) {
449 auto Pair = RelevantLoops.try_emplace(S);
451 return Pair.first->second;
471 const Loop *
L =
nullptr;
476 return RelevantLoops[S] =
L;
481 return Pair.first->second = SE.LI.getLoopFor(
I->getParent());
497 explicit LoopCompare(DominatorTree &dt) : DT(dt) {}
499 bool operator()(std::pair<const Loop *, const SCEV *>
LHS,
500 std::pair<const Loop *, const SCEV *>
RHS)
const {
507 if (
LHS.first !=
RHS.first)
513 if (
LHS.second->isNonConstantNegative()) {
514 if (!
RHS.second->isNonConstantNegative())
516 }
else if (
RHS.second->isNonConstantNegative())
528 const SCEV *URemLHS =
nullptr;
529 const SCEV *URemRHS =
nullptr;
543 OpsAndLoops.
push_back(std::make_pair(getRelevantLoop(
Op),
Op));
551 Value *Sum =
nullptr;
552 for (
auto I = OpsAndLoops.
begin(),
E = OpsAndLoops.
end();
I !=
E;) {
553 const Loop *CurLoop =
I->first;
554 const SCEV *
Op =
I->second;
562 assert(!
Op->getType()->isPointerTy() &&
"Only first op can be pointer");
567 for (;
I !=
E &&
I->first == CurLoop; ++
I) {
570 const SCEV *
X =
I->second;
573 X = SE.getSCEV(
U->getValue());
576 Sum = expandAddToGEP(SE.getAddExpr(NewOps), Sum, S->
getNoWrapFlags());
577 }
else if (
Op->isNonConstantNegative()) {
579 Value *
W = expand(SE.getNegativeSCEV(
Op));
605 OpsAndLoops.
push_back(std::make_pair(getRelevantLoop(
Op),
Op));
612 Value *Prod =
nullptr;
613 auto I = OpsAndLoops.
begin();
618 const auto ExpandOpBinPowN = [
this, &
I, &OpsAndLoops]() {
628 while (
E != OpsAndLoops.
end() && *
I == *
E &&
Exponent != MaxExponent) {
632 assert(
Exponent > 0 &&
"Trying to calculate a zeroth exponent of operand?");
640 for (uint64_t BinExp = 2; BinExp <=
Exponent; BinExp <<= 1) {
651 assert(Result &&
"Nothing was expanded?");
655 while (
I != OpsAndLoops.
end()) {
658 Prod = ExpandOpBinPowN();
659 }
else if (
I->second->isAllOnesValue()) {
666 Value *
W = ExpandOpBinPowN();
675 if (
RHS->logBase2() ==
RHS->getBitWidth() - 1)
677 Prod = InsertBinop(Instruction::Shl, Prod,
678 ConstantInt::get(Ty,
RHS->logBase2()), NWFlags,
681 Prod = InsertBinop(Instruction::Mul, Prod, W, S->
getNoWrapFlags(),
693 const APInt &
RHS = SC->getAPInt();
694 if (
RHS.isPowerOf2())
695 return InsertBinop(Instruction::LShr,
LHS,
696 ConstantInt::get(SC->getType(),
RHS.logBase2()),
700 const SCEV *RHSExpr = S->
getRHS();
703 bool GuaranteedNotPoison =
704 ScalarEvolution::isGuaranteedNotToBePoison(RHSExpr);
705 if (!GuaranteedNotPoison)
706 RHS = Builder.CreateFreeze(
RHS);
711 if (!SE.isKnownNonZero(RHSExpr) || !GuaranteedNotPoison)
712 RHS = Builder.CreateIntrinsic(
RHS->
getType(), Intrinsic::umax,
713 {RHS, ConstantInt::get(RHS->getType(), 1)});
716 SE.isKnownNonZero(S->
getRHS()));
729 if (L == IVIncInsertLoop) {
732 if (!SE.DT.dominates(OInst, IVIncInsertPos))
746 return isNormalAddRecExprPHI(PN, IncV, L);
761 if (IncV == InsertPos)
768 case Instruction::Add:
769 case Instruction::Sub: {
771 if (!OInst || SE.DT.dominates(OInst, InsertPos))
775 case Instruction::BitCast:
777 case Instruction::GetElementPtr:
782 if (!SE.DT.dominates(OInst, InsertPos))
808 if (Builder.GetInsertPoint() == It)
809 Builder.SetInsertPoint(&*NewInsertPt);
810 for (
auto *InsertPtGuard : InsertPointGuards)
811 if (InsertPtGuard->GetInsertPoint() == It)
812 InsertPtGuard->SetInsertPoint(NewInsertPt);
819 bool RecomputePoisonFlags) {
824 I->dropPoisonGeneratingFlags();
826 if (
auto Flags = SE.getStrengthenedNoWrapFlagsFromBinOp(OBO)) {
835 if (SE.DT.dominates(IncV, InsertPos)) {
836 if (RecomputePoisonFlags)
837 FixupPoisonFlags(IncV);
847 if (!SE.LI.movementPreservesLCSSAForm(IncV, InsertPos))
859 if (SE.DT.dominates(IncV, InsertPos))
863 fixupInsertPoints(
I);
865 if (RecomputePoisonFlags)
888 (IVOper =
getIVIncOperand(IVOper, L->getLoopPreheader()->getTerminator(),
905 IncV = Builder.CreatePtrAdd(PN, StepV,
"scevgep");
908 Builder.CreateSub(PN, StepV, Twine(IVName) +
".iv.next") :
909 Builder.CreateAdd(PN, StepV, Twine(IVName) +
".iv.next");
921 Type *PhiTy = Phi->getType();
935 if (Phi == Requested) {
958 const SCEV *ExtendAfterOp =
960 return ExtendAfterOp == OpAfterExtend;
972 const SCEV *ExtendAfterOp =
974 return ExtendAfterOp == OpAfterExtend;
981SCEVExpander::getAddRecExprPHILiterally(
const SCEVAddRecExpr *Normalized,
984 assert((!IVIncInsertLoop || IVIncInsertPos) &&
985 "Uninitialized insert position");
990 PHINode *AddRecPhiMatch =
nullptr;
997 bool TryNonMatchingSCEV =
999 SE.DT.properlyDominates(LatchBlock, IVIncInsertLoop->getHeader());
1001 for (PHINode &PN :
L->getHeader()->phis()) {
1002 if (!SE.isSCEVable(PN.
getType()))
1009 DebugType,
dbgs() <<
"One incomplete PHI is found: " << PN <<
"\n");
1017 bool IsMatchingSCEV = PhiSCEV == Normalized;
1021 if (!IsMatchingSCEV && !TryNonMatchingSCEV)
1032 if (!isExpandedAddRecExprPHI(&PN, TempIncV, L))
1035 if (!isNormalAddRecExprPHI(&PN, TempIncV, L))
1040 if (IsMatchingSCEV) {
1044 AddRecPhiMatch = &PN;
1050 if ((!TruncTy || InvertStep) &&
1054 AddRecPhiMatch = &PN;
1056 TruncTy = Normalized->
getType();
1060 if (AddRecPhiMatch) {
1063 InsertedValues.insert(AddRecPhiMatch);
1065 rememberInstruction(IncV);
1067 ReusedValues.insert(AddRecPhiMatch);
1068 ReusedValues.insert(IncV);
1069 return AddRecPhiMatch;
1074 SCEVInsertPointGuard Guard(Builder,
this);
1084 PostIncLoops.
clear();
1087 assert(
L->getLoopPreheader() &&
1088 "Can't expand add recurrences without a loop preheader!");
1090 expand(Normalized->
getStart(),
L->getLoopPreheader()->getTerminator());
1107 Step = SE.getNegativeSCEV(Step);
1109 Value *StepV = expand(Step,
L->getHeader()->getFirstInsertionPt());
1114 bool IncrementIsNUW = !useSubtract &&
IsIncrementNUW(SE, Normalized);
1115 bool IncrementIsNSW = !useSubtract &&
IsIncrementNSW(SE, Normalized);
1119 Builder.SetInsertPoint(Header, Header->begin());
1121 Builder.CreatePHI(ExpandTy,
pred_size(Header), Twine(IVName) +
".iv");
1126 if (!
L->contains(Pred)) {
1135 IVIncInsertPos : Pred->getTerminator();
1136 Builder.SetInsertPoint(InsertPos);
1137 Value *IncV = expandIVInc(PN, StepV, L, useSubtract);
1150 PostIncLoops = SavedPostIncLoops;
1154 InsertedValues.
insert(PN);
1155 InsertedIVs.push_back(PN);
1164 const SCEVAddRecExpr *Normalized = S;
1165 if (PostIncLoops.count(L)) {
1172 [[maybe_unused]]
const SCEV *
Start = Normalized->
getStart();
1174 assert(SE.properlyDominates(Start,
L->getHeader()) &&
1175 "Start does not properly dominate loop header");
1176 assert(SE.dominates(Step,
L->getHeader()) &&
"Step not dominate loop header");
1180 Type *TruncTy =
nullptr;
1181 bool InvertStep =
false;
1182 PHINode *PN = getAddRecExprPHILiterally(Normalized, L, TruncTy, InvertStep);
1186 if (!PostIncLoops.count(L))
1191 assert(LatchBlock &&
"PostInc mode requires a unique loop latch!");
1200 I->setHasNoUnsignedWrap(
false);
1202 I->setHasNoSignedWrap(
false);
1210 &*Builder.GetInsertPoint())) {
1223 Step = SE.getNegativeSCEV(Step);
1227 SCEVInsertPointGuard Guard(Builder,
this);
1228 StepV = expand(Step,
L->getHeader()->getFirstInsertionPt());
1230 Result = expandIVInc(PN, StepV, L, useSubtract);
1238 if (TruncTy !=
Result->getType())
1239 Result = Builder.CreateTrunc(Result, TruncTy);
1243 Result = Builder.CreateSub(expand(Normalized->
getStart()), Result);
1254 !SE.DT.dominates(EB, Builder.GetInsertBlock()))
1257 for (
auto &PN : EB->
phis()) {
1258 if (!SE.isSCEVable(PN.
getType()))
1260 auto *ExitSCEV = SE.getSCEV(&PN);
1265 ExitSCEV = SE.getPtrToIntExpr(ExitSCEV, STy);
1274 const SCEV *Diff = SE.getMinusSCEV(S, ExitSCEV);
1275 const SCEV *
Op = Diff;
1283 "difference must be of integer type");
1284 Value *DiffV = expand(Diff);
1285 Value *BaseV = fixupLCSSAFormFor(&PN);
1288 return Builder.CreatePtrAdd(BaseV, DiffV);
1289 BaseV = Builder.CreatePtrToInt(BaseV, DiffV->
getType());
1291 return Builder.CreateAdd(BaseV, DiffV);
1309 return expandAddRecExprLiterally(S);
1315 PHINode *CanonicalIV =
nullptr;
1316 if (PHINode *PN =
L->getCanonicalInductionVariable())
1317 if (SE.getTypeSizeInBits(PN->
getType()) >= SE.getTypeSizeInBits(Ty))
1323 SE.getTypeSizeInBits(CanonicalIV->
getType()) > SE.getTypeSizeInBits(Ty) &&
1332 V = expand(SE.getTruncateExpr(SE.getUnknown(V), Ty), NewInsertPt);
1338 if (
Value *V = tryToReuseLCSSAPhi(S))
1344 Value *StartV = expand(SE.getPointerBase(S));
1345 return expandAddToGEP(SE.removePointerBase(S), StartV,
1350 NewOps[0] = SE.getConstant(Ty, 0);
1351 const SCEV *Rest = SE.getAddRecExpr(NewOps, L,
1358 const SCEV *AddExprLHS = SE.getUnknown(expand(S->
getStart()));
1359 const SCEV *AddExprRHS = SE.getUnknown(expand(Rest));
1360 return expand(SE.getAddExpr(AddExprLHS, AddExprRHS));
1371 rememberInstruction(CanonicalIV);
1373 SmallPtrSet<BasicBlock *, 4> PredSeen;
1374 Constant *One = ConstantInt::get(Ty, 1);
1377 if (!PredSeen.
insert(HP).second) {
1384 if (
L->contains(HP)) {
1391 rememberInstruction(
Add);
1401 assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->
getType()) &&
1402 "IVs with types different from the canonical IV should "
1403 "already have been handled!");
1412 expand(SE.getTruncateOrNoop(
1413 SE.getMulExpr(SE.getUnknown(CanonicalIV),
1422 const SCEV *IH = SE.getUnknown(CanonicalIV);
1425 const SCEV *NewS = S;
1426 const SCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->
getType());
1433 const SCEV *
T = SE.getTruncateOrNoop(V, Ty);
1439 return ReuseOrCreateCast(V, S->
getType(), CastInst::PtrToAddr,
1440 GetOptimalInsertionPointForCastOf(V));
1445 return ReuseOrCreateCast(V, S->
getType(), CastInst::PtrToInt,
1446 GetOptimalInsertionPointForCastOf(V));
1451 return Builder.CreateTrunc(V, S->
getType());
1456 return Builder.CreateZExt(V, S->
getType(),
"",
1462 return Builder.CreateSExt(V, S->
getType());
1467 bool IsSequential) {
1468 bool PrevSafeMode = SafeUDivMode;
1469 SafeUDivMode |= IsSequential;
1473 LHS = Builder.CreateFreeze(
LHS);
1475 SafeUDivMode = (IsSequential && i != 0) || PrevSafeMode;
1477 if (IsSequential && i != 0)
1478 RHS = Builder.CreateFreeze(
RHS);
1481 Sel = Builder.CreateIntrinsic(IntrinID, {Ty}, {
LHS,
RHS},
1486 Sel = Builder.CreateSelect(ICmp,
LHS,
RHS, Name);
1490 SafeUDivMode = PrevSafeMode;
1495 return expandMinMaxExpr(S, Intrinsic::smax,
"smax");
1499 return expandMinMaxExpr(S, Intrinsic::umax,
"umax");
1503 return expandMinMaxExpr(S, Intrinsic::smin,
"smin");
1507 return expandMinMaxExpr(S, Intrinsic::umin,
"umin");
1511 return expandMinMaxExpr(S, Intrinsic::umin,
"umin",
true);
1515 return Builder.CreateVScale(S->
getType());
1527 Value *V = expand(SH);
1529 if (Ty && Ty != V->getType()) {
1530 assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->
getType()) &&
1531 "non-trivial casts should be done with the SCEVs directly!");
1532 V = InsertNoopCastOfTo(V, Ty);
1537Value *SCEVExpander::FindValueInExprValueMap(
1549 for (
Value *V : SE.getSCEVValues(S)) {
1566 DropPoisonGeneratingInsts.
clear();
1577Value *SCEVExpander::expand(
const SCEV *S) {
1584 auto SafeToHoist = [](
const SCEV *S) {
1589 return SC->getValue()->isZero();
1599 if (SafeToHoist(S)) {
1600 for (Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock());;
1601 L =
L->getParentLoop()) {
1602 if (SE.isLoopInvariant(S, L)) {
1604 if (BasicBlock *Preheader =
L->getLoopPreheader()) {
1610 InsertPt =
L->getHeader()->getFirstInsertionPt();
1616 if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
1617 InsertPt =
L->getHeader()->getFirstInsertionPt();
1619 while (InsertPt != Builder.GetInsertPoint() &&
1621 InsertPt = std::next(InsertPt);
1629 auto I = InsertedExpressions.find(std::make_pair(S, &*InsertPt));
1630 if (
I != InsertedExpressions.end())
1633 SCEVInsertPointGuard Guard(Builder,
this);
1634 Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
1638 Value *
V = FindValueInExprValueMap(S, &*InsertPt, DropPoisonGeneratingInsts);
1641 V = fixupLCSSAFormFor(V);
1643 for (Instruction *
I : DropPoisonGeneratingInsts) {
1645 I->dropPoisonGeneratingAnnotations();
1649 if (
auto Flags = SE.getStrengthenedNoWrapFlagsFromBinOp(OBO)) {
1657 auto *Src = NNI->getOperand(0);
1660 DL).value_or(
false))
1661 NNI->setNonNeg(
true);
1671 InsertedExpressions[std::make_pair(S, &*InsertPt)] =
V;
1675void SCEVExpander::rememberInstruction(
Value *
I) {
1676 auto DoInsert = [
this](
Value *
V) {
1677 if (!PostIncLoops.empty())
1678 InsertedPostIncValues.insert(V);
1680 InsertedValues.insert(V);
1687 OrigFlags.try_emplace(
I, PoisonFlags(
I));
1690void SCEVExpander::replaceCongruentIVInc(
1701 if (!OrigInc || !IsomorphicInc)
1708 bool Chained = ChainedPhis.contains(Phi);
1709 if (!(Chained || isExpandedAddRecExprPHI(OrigPhi, OrigInc, L)) &&
1710 (Chained || isExpandedAddRecExprPHI(Phi, IsomorphicInc, L))) {
1725 const SCEV *TruncExpr =
1726 SE.getTruncateOrNoop(SE.getSCEV(OrigInc), IsomorphicInc->
getType());
1727 if (OrigInc == IsomorphicInc || TruncExpr != SE.getSCEV(IsomorphicInc) ||
1728 !SE.LI.replacementPreservesLCSSAForm(IsomorphicInc, OrigInc))
1731 bool BothHaveNUW =
false;
1732 bool BothHaveNSW =
false;
1735 if (OBOIncV && OBOIsomorphic) {
1737 OBOIncV->hasNoUnsignedWrap() && OBOIsomorphic->hasNoUnsignedWrap();
1739 OBOIncV->hasNoSignedWrap() && OBOIsomorphic->hasNoSignedWrap();
1752 "Should only replace an increment with a wider one.");
1753 if (BothHaveNUW || BothHaveNSW) {
1759 dbgs() <<
"INDVARS: Eliminated congruent iv.inc: "
1760 << *IsomorphicInc <<
'\n');
1761 Value *NewInc = OrigInc;
1765 IP = PN->
getParent()->getFirstInsertionPt();
1770 Builder.SetCurrentDebugLocation(IsomorphicInc->
getDebugLoc());
1772 Builder.CreateTruncOrBitCast(OrigInc, IsomorphicInc->
getType(), IVName);
1797 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
1798 return RHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy();
1799 return RHS->getType()->getPrimitiveSizeInBits().getFixedValue() <
1800 LHS->getType()->getPrimitiveSizeInBits().getFixedValue();
1803 unsigned NumElim = 0;
1811 if (!SE.isSCEVable(PN->
getType()))
1816 return Const->getValue();
1821 if (
Value *V = SimplifyPHINode(Phi)) {
1822 if (V->getType() != Phi->getType())
1824 SE.forgetValue(Phi);
1825 Phi->replaceAllUsesWith(V);
1829 dbgs() <<
"INDVARS: Eliminated constant iv: " << *Phi
1834 if (!SE.isSCEVable(Phi->getType()))
1837 PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)];
1840 if (Phi->getType()->isIntegerTy() &&
TTI &&
1841 TTI->isTruncateFree(Phi->getType(), Phis.
back()->getType())) {
1845 const SCEV *PhiExpr = SE.getSCEV(Phi);
1849 const SCEV *TruncExpr =
1850 SE.getTruncateExpr(PhiExpr, Phis.
back()->getType());
1851 ExprToIVMap[TruncExpr] = Phi;
1862 replaceCongruentIVInc(Phi, OrigPhiRef, L, DT, DeadInsts);
1864 dbgs() <<
"INDVARS: Eliminated congruent iv: " << *Phi
1867 DebugType,
dbgs() <<
"INDVARS: Original iv: " << *OrigPhiRef <<
'\n');
1869 Value *NewIV = OrigPhiRef;
1870 if (OrigPhiRef->
getType() != Phi->getType()) {
1872 L->getHeader()->getFirstInsertionPt());
1873 Builder.SetCurrentDebugLocation(Phi->getDebugLoc());
1874 NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->getType(), IVName);
1876 Phi->replaceAllUsesWith(NewIV);
1888 L->getExitingBlocks(ExitingBlocks);
1895 if (!
match(BB->getTerminator(),
1900 if (SE.getSCEV(LHS) == S && SE.DT.dominates(LHS, At))
1903 if (SE.getSCEV(RHS) == S && SE.DT.dominates(RHS, At))
1912 return FindValueInExprValueMap(S, At, DropPoisonGeneratingInsts) !=
nullptr;
1923 struct OperationIndices {
1924 OperationIndices(
unsigned Opc,
size_t min,
size_t max) :
1925 Opcode(
Opc), MinIdx(min), MaxIdx(
max) { }
1938 return TTI.getCastInstrCost(Opcode, S->getType(),
1939 S->getOperand(0)->getType(),
1943 auto ArithCost = [&](
unsigned Opcode,
unsigned NumRequired,
1944 unsigned MinIdx = 0,
1947 return NumRequired *
1948 TTI.getArithmeticInstrCost(Opcode, S->getType(),
CostKind);
1951 auto CmpSelCost = [&](
unsigned Opcode,
unsigned NumRequired,
unsigned MinIdx,
1954 Type *OpType = S->getType();
1955 return NumRequired *
TTI.getCmpSelInstrCost(
1960 switch (S->getSCEVType()) {
1968 Cost = CastCost(Instruction::PtrToAddr);
1971 Cost = CastCost(Instruction::PtrToInt);
1974 Cost = CastCost(Instruction::Trunc);
1977 Cost = CastCost(Instruction::ZExt);
1980 Cost = CastCost(Instruction::SExt);
1983 unsigned Opcode = Instruction::UDiv;
1985 if (SC->getAPInt().isPowerOf2())
1986 Opcode = Instruction::LShr;
1987 Cost = ArithCost(Opcode, 1);
1991 Cost = ArithCost(Instruction::Add, S->getNumOperands() - 1);
1997 Cost = ArithCost(Instruction::Mul, S->getNumOperands() - 1);
2006 Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 1);
2007 Cost += CmpSelCost(Instruction::Select, S->getNumOperands() - 1, 0, 2);
2008 switch (S->getSCEVType()) {
2012 Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 0);
2013 Cost += ArithCost(Instruction::Or,
2014 S->getNumOperands() > 2 ? S->getNumOperands() - 2 : 0);
2015 Cost += CmpSelCost(Instruction::Select, 1, 0, 1);
2020 "Unhandled SCEV expression type?");
2027 unsigned NumRecurrences = S->getNumOperands() - 1;
2028 Cost +=
TTI.getCFInstrCost(Instruction::PHI,
CostKind) * NumRecurrences;
2030 TTI.getArithmeticInstrCost(Instruction::Add, S->getType(),
CostKind) *
2033 Worklist.
emplace_back(Instruction::PHI, 0, S->getOperand(0));
2035 for (
const SCEV *
Op : S->operands().drop_front())
2041 for (
auto &CostOp : Operations) {
2042 for (
auto SCEVOp :
enumerate(S->operands())) {
2044 size_t MinIdx = std::max(SCEVOp.index(), CostOp.MinIdx);
2045 size_t OpIdx = std::min(MinIdx, CostOp.MaxIdx);
2052bool SCEVExpander::isHighCostExpansionHelper(
2060 const SCEV *S = WorkItem.
S;
2071 L->getHeader()->getParent()->hasMinSize()
2090 return Cost > Budget;
2111 SE.getAddExpr(S, SE.getConstant(S->
getType(), 1)), &At, L))
2126 "Nary expr should have more than 1 operand.");
2131 return Cost > Budget;
2135 "Polynomial should be at least linear");
2138 return Cost > Budget;
2147 switch (Pred->getKind()) {
2162 Value *Expr0 = expand(Pred->getLHS(), IP);
2163 Value *Expr1 = expand(Pred->getRHS(), IP);
2165 Builder.SetInsertPoint(IP);
2167 auto *
I = Builder.CreateICmp(InvPred, Expr0, Expr1,
"ident.check");
2174 "non-affine expression");
2178 const SCEV *ExitCount =
2179 SE.getPredicatedSymbolicMaxBackedgeTakenCount(AR->
getLoop(), Pred);
2187 unsigned SrcBits = SE.getTypeSizeInBits(ExitCount->
getType());
2188 unsigned DstBits = SE.getTypeSizeInBits(ARTy);
2195 Builder.SetInsertPoint(
Loc);
2196 Value *TripCountVal = expand(ExitCount,
Loc);
2201 Value *StepValue = expand(Step,
Loc);
2202 Value *NegStepValue = expand(SE.getNegativeSCEV(Step),
Loc);
2203 Value *StartValue = expand(Start,
Loc);
2208 Builder.SetInsertPoint(
Loc);
2211 Value *AbsStep = Builder.CreateSelect(StepCompare, NegStepValue, StepValue);
2221 auto ComputeEndCheck = [&]() ->
Value * {
2223 Value *TruncTripCount = Builder.CreateZExtOrTrunc(TripCountVal, Ty);
2225 CallInst *
Mul = Builder.CreateIntrinsic(Intrinsic::umul_with_overflow, Ty,
2226 {AbsStep, TruncTripCount},
2228 Value *MulV = Builder.CreateExtractValue(
Mul, 0,
"mul.result");
2229 Value *OfMul = Builder.CreateExtractValue(
Mul, 1,
"mul.overflow");
2232 bool NeedPosCheck = !SE.isKnownNegative(Step);
2233 bool NeedNegCheck = !SE.isKnownPositive(Step);
2236 Value *NegMulV = Builder.CreateNeg(MulV);
2238 Add = Builder.CreatePtrAdd(StartValue, MulV);
2240 Sub = Builder.CreatePtrAdd(StartValue, NegMulV);
2243 Add = Builder.CreateAdd(StartValue, MulV);
2245 Sub = Builder.CreateSub(StartValue, MulV);
2248 Value *EndCompareLT =
nullptr;
2249 Value *EndCompareGT =
nullptr;
2250 Value *EndCheck =
nullptr;
2252 EndCheck = EndCompareLT = Builder.CreateICmp(
2255 EndCheck = EndCompareGT = Builder.CreateICmp(
2257 if (NeedPosCheck && NeedNegCheck) {
2259 EndCheck = Builder.CreateSelect(StepCompare, EndCompareGT, EndCompareLT);
2261 return Builder.CreateOr(EndCheck, OfMul);
2263 Value *EndCheck = ComputeEndCheck();
2268 if (SrcBits > DstBits) {
2270 auto *BackedgeCheck =
2272 ConstantInt::get(
Loc->getContext(), MaxVal));
2273 BackedgeCheck = Builder.CreateAnd(
2276 EndCheck = Builder.CreateOr(EndCheck, BackedgeCheck);
2285 Value *NSSWCheck =
nullptr, *NUSWCheck =
nullptr;
2295 if (NUSWCheck && NSSWCheck)
2296 return Builder.CreateOr(NUSWCheck, NSSWCheck);
2311 for (
const auto *Pred : Union->getPredicates()) {
2313 Builder.SetInsertPoint(IP);
2318 return Builder.CreateOr(Checks);
2321Value *SCEVExpander::fixupLCSSAFormFor(
Value *V) {
2323 if (!PreserveLCSSA || !DefI)
2329 if (!DefLoop || UseLoop == DefLoop || DefLoop->
contains(UseLoop))
2340 if (DefI->getType()->isIntegerTy())
2354 for (
PHINode *PN : InsertedPHIs)
2355 rememberInstruction(PN);
2356 for (
PHINode *PN : PHIsToRemove) {
2359 InsertedValues.erase(PN);
2360 InsertedPostIncValues.erase(PN);
2364 return User->getOperand(0);
2386struct SCEVFindUnsafe {
2387 ScalarEvolution &SE;
2389 bool IsUnsafe =
false;
2391 SCEVFindUnsafe(ScalarEvolution &SE,
bool CanonicalMode)
2392 : SE(SE), CanonicalMode(CanonicalMode) {}
2394 bool follow(
const SCEV *S) {
2404 if (!AR->getLoop()->getLoopPreheader() &&
2405 (!CanonicalMode || !AR->isAffine())) {
2412 bool isDone()
const {
return IsUnsafe; }
2417 SCEVFindUnsafe Search(SE, CanonicalMode);
2419 return !Search.IsUnsafe;
2450 for (
auto [
I, Flags] : Expander.OrigFlags)
2453 auto InsertedInstructions = Expander.getAllInsertedInstructions();
2456 InsertedInstructions);
2466 [&InsertedSet](
Value *U) {
2467 return InsertedSet.contains(cast<Instruction>(U));
2469 "removed instruction should only be used by instructions inserted "
2470 "during expansion");
2472 assert(!
I->getType()->isVoidTy() &&
2473 "inserted instruction should have non-void types");
2475 I->eraseFromParent();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static cl::opt< OutputCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(OutputCostKind::RecipThroughput), cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(OutputCostKind::Latency, "latency", "Instruction latency"), clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"), clEnumValN(OutputCostKind::SizeAndLatency, "size-latency", "Code size and latency"), clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")))
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR)
static const Loop * PickMostRelevantLoop(const Loop *A, const Loop *B, DominatorTree &DT)
PickMostRelevantLoop - Given two loops pick the one that's most relevant for SCEV expansion.
static InstructionCost costAndCollectOperands(const SCEVOperand &WorkItem, const TargetTransformInfo &TTI, TargetTransformInfo::TargetCostKind CostKind, SmallVectorImpl< SCEVOperand > &Worklist)
static bool IsIncrementNSW(ScalarEvolution &SE, const SCEVAddRecExpr *AR)
static bool canBeCheaplyTransformed(ScalarEvolution &SE, const SCEVAddRecExpr *Phi, const SCEVAddRecExpr *Requested, bool &InvertStep)
Check whether we can cheaply express the requested SCEV in terms of the available PHI SCEV by truncat...
#define SCEV_DEBUG_WITH_TYPE(TYPE, X)
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
This class represents an incoming formal argument to a Function.
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 BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
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...
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.
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
static LLVM_ABI Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
static LLVM_ABI CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
@ ICMP_SLT
signed less than
@ ICMP_UGT
unsigned greater than
@ ICMP_SGT
signed greater than
@ ICMP_ULT
unsigned less than
@ ICMP_SGE
signed greater or equal
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
This is the shared class of boolean and integer constants.
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
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.
static GEPNoWrapFlags noUnsignedWrap()
static GEPNoWrapFlags none()
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
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 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 void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
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.
Class to represent integer types.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
bool isComplete() const
If the PHI node is complete which means all of its parent's predecessors have incoming value in this ...
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 PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This node represents an addition of some number of SCEVs.
This node represents a polynomial recurrence on the trip count of the specified loop.
const SCEV * getStart() const
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
const Loop * getLoop() const
const SCEV * getOperand() const
This class represents an assumption that the expression LHS Pred RHS evaluates to true,...
LLVM_ABI Value * generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed)
Generates code that evaluates if the AR expression will overflow.
LLVM_ABI bool hasRelatedExistingExpansion(const SCEV *S, const Instruction *At, Loop *L)
Determine whether there is an existing expansion of S that can be reused.
SmallVector< Instruction *, 32 > getAllInsertedInstructions() const
Return a vector containing all instructions inserted during expansion.
LLVM_ABI bool isSafeToExpand(const SCEV *S) const
Return true if the given expression is safe to expand in the sense that all materialized values are s...
LLVM_ABI bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint) const
Return true if the given expression is safe to expand in the sense that all materialized values are d...
LLVM_ABI unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr)
replace congruent phis with their most canonical representative.
LLVM_ABI Value * expandUnionPredicate(const SCEVUnionPredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
LLVM_ABI bool hoistIVInc(Instruction *IncV, Instruction *InsertPos, bool RecomputePoisonFlags=false)
Utility for hoisting IncV (with all subexpressions requried for its computation) before InsertPos.
bool isInsertedInstruction(Instruction *I) const
Return true if the specified instruction was inserted by the code rewriter.
LLVM_ABI Value * expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc)
Generates a code sequence that evaluates this predicate.
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...
LLVM_ABI Value * expandComparePredicate(const SCEVComparePredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
LLVM_ABI Value * expandCodeFor(const SCEV *SH, Type *Ty, BasicBlock::iterator I)
Insert code to directly compute the specified SCEV expression into the program.
LLVM_ABI Value * expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
LLVM_ABI Instruction * getIVIncOperand(Instruction *IncV, Instruction *InsertPos, bool allowScale)
Return the induction variable increment's IV operand.
void eraseDeadInstructions(Value *Root)
Remove inserted instructions that are dead, e.g.
LLVM_ABI BasicBlock::iterator findInsertPointAfter(Instruction *I, Instruction *MustDominate) const
Returns a suitable insert point after I, that dominates MustDominate.
void setInsertPoint(Instruction *IP)
Set the current insertion point.
This node represents multiplication of some number of SCEVs.
This node is a base class providing common functionality for n'ary operators.
bool hasNoUnsignedWrap() const
size_t getNumOperands() const
bool hasNoSignedWrap() const
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
const SCEV * getOperand(unsigned i) const
ArrayRef< const SCEV * > operands() const
This class represents an assumption made using SCEV expressions which can be checked at run-time.
This class represents a cast from a pointer to a pointer-sized integer value, without capturing the p...
This class represents a cast from a pointer to a pointer-sized integer value.
This class represents a signed maximum selection.
This class represents a signed minimum selection.
This class represents a sequential/in-order unsigned minimum selection.
This class represents a sign extension of a small integer value to a larger integer value.
This class represents a truncation of an integer value to a smaller integer value.
This class represents a binary unsigned division operation.
const SCEV * getLHS() const
const SCEV * getRHS() const
This class represents an unsigned maximum selection.
This class represents an unsigned minimum selection.
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents the value of vscale, as used when defining the length of a scalable vector or r...
This class represents an assumption made on an AddRec expression.
This class represents a zero extension of a small integer value to a larger integer value.
This class represents an analyzed expression in the program.
LLVM_ABI ArrayRef< const SCEV * > operands() const
Return operands of this SCEV expression.
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 bool isNonConstantNegative() const
Return true if the specified scev is negated, but not a constant.
SCEVTypes getSCEVType() const
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
The main scalar evolution driver.
LLVM_ABI bool isKnownNonZero(const SCEV *S)
Test if the given expression is known to be non-zero.
LLVM_ABI const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI bool containsAddRecurrence(const SCEV *S)
Return true if the SCEV is a scAddRecExpr or it contains scAddRecExpr.
LLVM_ABI const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
static SCEV::NoWrapFlags clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags)
LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags, int Mask)
Convenient NoWrapFlags manipulation that hides enum casts and is visible in the ScalarEvolution name ...
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 const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
bool isPointerTy() const
True if this is an instance of PointerType.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
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.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVMContext & getContext() const
All values hold a context through their type.
iterator_range< user_iterator > users()
const ParentTy * getParent() const
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
@ BasicBlock
Various leaf nodes.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
bool match(Val *V, const Pattern &P)
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
cst_pred_ty< is_all_ones > m_scev_AllOnes()
Match an integer with all bits set.
class_match< const SCEVConstant > m_SCEVConstant()
bind_ty< const SCEVMulExpr > m_scev_Mul(const SCEVMulExpr *&V)
bind_ty< const SCEVAddExpr > m_scev_Add(const SCEVAddExpr *&V)
SCEVUnaryExpr_match< SCEVPtrToIntExpr, Op0_t > m_scev_PtrToInt(const Op0_t &Op0)
SCEVURem_match< Op0_t, Op1_t > m_scev_URem(Op0_t LHS, Op1_t RHS, ScalarEvolution &SE)
Match the mathematical pattern A - (A / B) * B, where A and B can be arbitrary expressions.
class_match< const SCEV > m_SCEV()
@ CE
Windows NT (Windows on ARM)
initializer< Ty > init(const Ty &Val)
@ User
could "use" a pointer
NodeAddr< PhiNode * > Phi
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
FunctionAddr VTableAddr Value
void stable_sort(R &&Range)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
auto pred_end(const MachineBasicBlock *BB)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
constexpr from_range_t from_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
auto pred_size(const MachineBasicBlock *BB)
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
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.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
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...
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI const SCEV * normalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, ScalarEvolution &SE, bool CheckInvertible=true)
Normalize S to be post-increment for all loops present in Loops.
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
PredIterator< BasicBlock, Value::user_iterator > pred_iterator
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.
auto pred_begin(const MachineBasicBlock *BB)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
SmallPtrSet< const Loop *, 2 > PostIncLoopSet
auto predecessors(const MachineBasicBlock *BB)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
bool SCEVExprContains(const SCEV *Root, PredTy Pred)
Return true if any node in Root satisfies the predicate Pred.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
LLVM_ABI void apply(Instruction *I)
LLVM_ABI PoisonFlags(const Instruction *I)
struct for holding enough information to help calculate the cost of the given SCEV when expanded into...
const SCEV * S
The SCEV operand to be costed.
unsigned ParentOpcode
LLVM instruction opcode that uses the operand.
int OperandIdx
The use index of an expanded instruction.
Value * visit(const SCEV *S)