110#define DEBUG_TYPE "instcombine"
118 "Number of instruction combining iterations performed");
119STATISTIC(NumOneIteration,
"Number of functions with one iteration");
120STATISTIC(NumTwoIterations,
"Number of functions with two iterations");
121STATISTIC(NumThreeIterations,
"Number of functions with three iterations");
123 "Number of functions with four or more iterations");
127STATISTIC(NumDeadInst ,
"Number of dead inst eliminated");
133 "Controls which instructions are visited");
140 "instcombine-max-sink-users",
cl::init(32),
141 cl::desc(
"Maximum number of undroppable users for instruction sinking"));
145 cl::desc(
"Maximum array size considered when doing a combine"));
149 cl::desc(
"Maximum number of users to visit in alloc-site "
150 "removability analysis"));
166std::optional<Instruction *>
169 if (
II.getCalledFunction()->isTargetIntrinsic()) {
170 return TTIForTargetIntrinsicsOnly.instCombineIntrinsic(*
this,
II);
177 bool &KnownBitsComputed) {
179 if (
II.getCalledFunction()->isTargetIntrinsic()) {
180 return TTIForTargetIntrinsicsOnly.simplifyDemandedUseBitsIntrinsic(
181 *
this,
II, DemandedMask, Known, KnownBitsComputed);
192 if (
II.getCalledFunction()->isTargetIntrinsic()) {
193 return TTIForTargetIntrinsicsOnly.simplifyDemandedVectorEltsIntrinsic(
194 *
this,
II, DemandedElts, PoisonElts, PoisonElts2, PoisonElts3,
204 return TTIForTargetIntrinsicsOnly.isValidAddrSpaceCast(FromAS, ToAS);
214 Builder.SetInsertPoint(Inst);
218 if (Inst && !
GEP->hasAllConstantIndices() &&
219 !
GEP->getSourceElementType()->isIntegerTy(8)) {
221 *Inst, Builder.CreateGEP(Builder.getInt8Ty(),
GEP->getPointerOperand(),
239 Value *Sum =
nullptr;
240 Value *OneUseSum =
nullptr;
241 Value *OneUseBase =
nullptr;
248 IRBuilderBase::InsertPointGuard Guard(
Builder);
250 if (RewriteGEPs && Inst)
254 if (
Offset->getType() != IdxTy)
257 if (
GEP->hasOneUse()) {
262 OneUseBase =
GEP->getPointerOperand();
271 if (RewriteGEPs && Inst &&
272 Offset->getType()->isVectorTy() ==
GEP->getType()->isVectorTy() &&
273 !(
GEP->getSourceElementType()->isIntegerTy(8) &&
278 OneUseBase ? OneUseBase :
GEP->getPointerOperand(),
Offset,
"",
285 OneUseSum = OneUseBase =
nullptr;
289 Sum =
Add(Sum, OneUseSum);
300bool InstCombinerImpl::isDesirableIntType(
unsigned BitWidth)
const {
319bool InstCombinerImpl::shouldChangeType(
unsigned FromWidth,
320 unsigned ToWidth)
const {
321 bool FromLegal = FromWidth == 1 ||
DL.isLegalInteger(FromWidth);
322 bool ToLegal = ToWidth == 1 ||
DL.isLegalInteger(ToWidth);
326 if (ToWidth < FromWidth && isDesirableIntType(ToWidth))
331 if ((FromLegal || isDesirableIntType(FromWidth)) && !ToLegal)
336 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
347bool InstCombinerImpl::shouldChangeType(
Type *From,
Type *To)
const {
355 return shouldChangeType(FromWidth, ToWidth);
365 if (!OBO || !OBO->hasNoSignedWrap())
368 const APInt *BVal, *CVal;
373 bool Overflow =
false;
374 switch (
I.getOpcode()) {
375 case Instruction::Add:
376 (void)BVal->
sadd_ov(*CVal, Overflow);
378 case Instruction::Sub:
379 (void)BVal->
ssub_ov(*CVal, Overflow);
381 case Instruction::Mul:
382 (void)BVal->
smul_ov(*CVal, Overflow);
393 return OBO && OBO->hasNoUnsignedWrap();
398 return OBO && OBO->hasNoSignedWrap();
407 I.clearSubclassOptionalData();
412 I.clearSubclassOptionalData();
413 I.setFastMathFlags(FMF);
423 if (!Cast || !Cast->hasOneUse())
427 auto CastOpcode = Cast->getOpcode();
428 if (CastOpcode != Instruction::ZExt)
437 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
463 Cast->dropPoisonGeneratingFlags();
469Value *InstCombinerImpl::simplifyIntToPtrRoundTripCast(
Value *Val) {
471 if (IntToPtr &&
DL.getTypeSizeInBits(IntToPtr->getDestTy()) ==
472 DL.getTypeSizeInBits(IntToPtr->getSrcTy())) {
474 Type *CastTy = IntToPtr->getDestTy();
477 PtrToInt->getSrcTy()->getPointerAddressSpace() &&
478 DL.getTypeSizeInBits(PtrToInt->getSrcTy()) ==
479 DL.getTypeSizeInBits(PtrToInt->getDestTy()))
480 return PtrToInt->getOperand(0);
517 if (
I.isCommutative()) {
518 if (
auto Pair = matchSymmetricPair(
I.getOperand(0),
I.getOperand(1))) {
528 if (
I.isAssociative()) {
551 I.setHasNoUnsignedWrap(
true);
554 I.setHasNoSignedWrap(
true);
583 if (
I.isAssociative() &&
I.isCommutative()) {
660 I.setHasNoUnsignedWrap(
true);
678 if (LOp == Instruction::And)
679 return ROp == Instruction::Or || ROp == Instruction::Xor;
682 if (LOp == Instruction::Or)
683 return ROp == Instruction::And;
687 if (LOp == Instruction::Mul)
688 return ROp == Instruction::Add || ROp == Instruction::Sub;
725 assert(
Op &&
"Expected a binary operator");
726 LHS =
Op->getOperand(0);
727 RHS =
Op->getOperand(1);
728 if (TopOpcode == Instruction::Add || TopOpcode == Instruction::Sub) {
733 Instruction::Shl, ConstantInt::get(
Op->getType(), 1),
C);
734 assert(
RHS &&
"Constant folding of immediate constants failed");
735 return Instruction::Mul;
740 if (OtherOp && OtherOp->
getOpcode() == Instruction::AShr &&
743 return Instruction::AShr;
746 return Op->getOpcode();
755 assert(
A &&
B &&
C &&
D &&
"All values must be provided");
758 Value *RetVal =
nullptr;
769 if (
A ==
C || (InnerCommutative &&
A ==
D)) {
778 if (!V && (
LHS->hasOneUse() ||
RHS->hasOneUse()))
779 V = Builder.CreateBinOp(TopLevelOpcode,
B,
D,
RHS->getName());
781 RetVal = Builder.CreateBinOp(InnerOpcode,
A, V);
789 if (
B ==
D || (InnerCommutative &&
B ==
C)) {
798 if (!V && (
LHS->hasOneUse() ||
RHS->hasOneUse()))
799 V = Builder.CreateBinOp(TopLevelOpcode,
A,
C,
LHS->getName());
801 RetVal = Builder.CreateBinOp(InnerOpcode, V,
B);
816 HasNSW =
I.hasNoSignedWrap();
817 HasNUW =
I.hasNoUnsignedWrap();
820 HasNSW &= LOBO->hasNoSignedWrap();
821 HasNUW &= LOBO->hasNoUnsignedWrap();
825 HasNSW &= ROBO->hasNoSignedWrap();
826 HasNUW &= ROBO->hasNoUnsignedWrap();
829 if (TopLevelOpcode == Instruction::Add && InnerOpcode == Instruction::Mul) {
857 unsigned Opc =
I->getOpcode();
858 unsigned ConstIdx = 1;
865 case Instruction::Sub:
868 case Instruction::ICmp:
875 case Instruction::Or:
879 case Instruction::Add:
894 Constant *BitWidthC = ConstantInt::get(Ty, Ty->getScalarSizeInBits());
900 if (!Cmp || !Cmp->isNullValue())
905 bool Consumes =
false;
909 assert(NotOp !=
nullptr &&
910 "Desync between isFreeToInvert and getFreelyInverted");
912 Value *CtpopOfNotOp =
Builder.CreateIntrinsic(Ty, Intrinsic::ctpop, NotOp);
919 case Instruction::Sub:
922 case Instruction::Or:
923 case Instruction::Add:
926 case Instruction::ICmp:
962 auto IsValidBinOpc = [](
unsigned Opc) {
966 case Instruction::And:
967 case Instruction::Or:
968 case Instruction::Xor:
969 case Instruction::Add:
978 auto IsCompletelyDistributable = [](
unsigned BinOpc1,
unsigned BinOpc2,
980 assert(ShOpc != Instruction::AShr);
981 return (BinOpc1 != Instruction::Add && BinOpc2 != Instruction::Add) ||
982 ShOpc == Instruction::Shl;
985 auto GetInvShift = [](
unsigned ShOpc) {
986 assert(ShOpc != Instruction::AShr);
987 return ShOpc == Instruction::LShr ? Instruction::Shl : Instruction::LShr;
990 auto CanDistributeBinops = [&](
unsigned BinOpc1,
unsigned BinOpc2,
994 if (BinOpc1 == Instruction::And)
999 if (!IsCompletelyDistributable(BinOpc1, BinOpc2, ShOpc))
1005 if (BinOpc2 == Instruction::And)
1016 auto MatchBinOp = [&](
unsigned ShOpnum) ->
Instruction * {
1018 Value *
X, *
Y, *ShiftedX, *Mask, *Shift;
1019 if (!
match(
I.getOperand(ShOpnum),
1023 I.getOperand(1 - ShOpnum),
1036 unsigned ShOpc = IY->getOpcode();
1037 if (ShOpc != IX->getOpcode())
1045 unsigned BinOpc = BO2->getOpcode();
1047 if (!IsValidBinOpc(
I.getOpcode()) || !IsValidBinOpc(BinOpc))
1050 if (ShOpc == Instruction::AShr) {
1064 if (BinOpc ==
I.getOpcode() &&
1065 IsCompletelyDistributable(
I.getOpcode(), BinOpc, ShOpc)) {
1080 if (!CanDistributeBinops(
I.getOpcode(), BinOpc, ShOpc, CMask, CShift))
1087 Value *NewBinOp1 =
Builder.CreateBinOp(
I.getOpcode(),
Y, NewBinOp2);
1094 return MatchBinOp(1);
1111 Value *LHS =
I.getOperand(0), *RHS =
I.getOperand(1);
1112 Value *
A, *CondVal, *TrueVal, *FalseVal;
1114 Constant *CastTrueVal, *CastFalseVal;
1116 auto MatchSelectAndCast = [&](
Value *CastOp,
Value *SelectOp) {
1125 if (MatchSelectAndCast(LHS, RHS))
1127 else if (MatchSelectAndCast(RHS, LHS))
1136 auto NewFoldedConst = [&](
bool IsTrueArm,
Value *V) {
1137 bool IsCastOpRHS = (CastOp == RHS);
1138 Value *CastVal = IsTrueArm ? CastFalseVal : CastTrueVal;
1140 return IsCastOpRHS ?
Builder.CreateBinOp(
Opc, V, CastVal)
1147 Value *NewTrueVal = NewFoldedConst(
false, TrueVal);
1149 NewFoldedConst(
true, FalseVal),
"",
nullptr,
SI);
1152 Value *NewTrueVal = NewFoldedConst(
true, TrueVal);
1154 NewFoldedConst(
false, FalseVal),
"",
nullptr,
SI);
1161 Value *LHS =
I.getOperand(0), *RHS =
I.getOperand(1);
1175 if (Op0 && Op1 && LHSOpcode == RHSOpcode)
1204 Value *LHS =
I.getOperand(0), *RHS =
I.getOperand(1);
1221 auto SQDistributive =
SQ.getWithInstruction(&
I).getWithoutUndef();
1229 C =
Builder.CreateBinOp(InnerOpcode, L, R);
1238 C =
Builder.CreateBinOp(TopLevelOpcode,
B,
C);
1247 C =
Builder.CreateBinOp(TopLevelOpcode,
A,
C);
1260 auto SQDistributive =
SQ.getWithInstruction(&
I).getWithoutUndef();
1268 A =
Builder.CreateBinOp(InnerOpcode, L, R);
1277 A =
Builder.CreateBinOp(TopLevelOpcode,
A,
C);
1286 A =
Builder.CreateBinOp(TopLevelOpcode,
A,
B);
1295static std::optional<std::pair<Value *, Value *>>
1297 if (
LHS->getParent() !=
RHS->getParent())
1298 return std::nullopt;
1300 if (
LHS->getNumIncomingValues() < 2)
1301 return std::nullopt;
1304 return std::nullopt;
1306 Value *L0 =
LHS->getIncomingValue(0);
1307 Value *R0 =
RHS->getIncomingValue(0);
1309 for (
unsigned I = 1,
E =
LHS->getNumIncomingValues();
I !=
E; ++
I) {
1313 if ((L0 == L1 && R0 == R1) || (L0 == R1 && R0 == L1))
1316 return std::nullopt;
1319 return std::optional(std::pair(L0, R0));
1322std::optional<std::pair<Value *, Value *>>
1327 return std::nullopt;
1329 case Instruction::PHI:
1331 case Instruction::Select: {
1337 return std::pair(TrueVal, FalseVal);
1338 return std::nullopt;
1340 case Instruction::Call: {
1344 if (LHSMinMax && RHSMinMax &&
1351 return std::pair(LHSMinMax->
getLHS(), LHSMinMax->
getRHS());
1352 return std::nullopt;
1355 return std::nullopt;
1365 if (!LHSIsSelect && !RHSIsSelect)
1375 FMF = FPOp->getFastMathFlags();
1376 Builder.setFastMathFlags(FMF);
1382 Value *
Cond, *True =
nullptr, *False =
nullptr;
1390 if (Opcode != Instruction::Add || (!True && !False) || (True && False))
1404 if (LHSIsSelect && RHSIsSelect &&
A ==
D) {
1410 if (LHS->hasOneUse() && RHS->hasOneUse()) {
1412 True =
Builder.CreateBinOp(Opcode,
B, E);
1413 else if (True && !False)
1414 False =
Builder.CreateBinOp(Opcode,
C,
F);
1416 }
else if (LHSIsSelect && LHS->hasOneUse()) {
1421 if (
Value *NewSel = foldAddNegate(
B,
C, RHS))
1423 }
else if (RHSIsSelect && RHS->hasOneUse()) {
1428 if (
Value *NewSel = foldAddNegate(E,
F, LHS))
1432 if (!True || !False)
1445 if (U == IgnoredUser)
1448 case Instruction::Select: {
1451 SI->swapProfMetadata();
1454 case Instruction::CondBr: {
1458 BPI->swapSuccEdgesProbabilities(BI->getParent());
1461 case Instruction::Xor:
1468 "canFreelyInvertAllUsersOf() ?");
1478 for (
unsigned Idx = 0, End = DbgVal->getNumVariableLocationOps();
1480 if (DbgVal->getVariableLocationOp(Idx) ==
I)
1481 DbgVal->setExpression(
1488Value *InstCombinerImpl::dyn_castNegVal(
Value *V)
const {
1498 if (
C->getType()->getElementType()->isIntegerTy())
1502 for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1518 if (CV->getType()->isVectorTy() &&
1519 CV->getType()->getScalarType()->isIntegerTy() && CV->getSplatValue())
1532Instruction *InstCombinerImpl::foldFBinOpOfIntCastsFromSign(
1533 BinaryOperator &BO,
bool OpsFromSigned, std::array<Value *, 2> IntOps,
1537 Type *IntTy = IntOps[0]->getType();
1542 unsigned MaxRepresentableBits =
1547 unsigned NumUsedLeadingBits[2] = {IntSz, IntSz};
1551 auto IsNonZero = [&](
unsigned OpNo) ->
bool {
1552 if (OpsKnown[OpNo].hasKnownBits() &&
1553 OpsKnown[OpNo].getKnownBits(
SQ).isNonZero())
1558 auto IsNonNeg = [&](
unsigned OpNo) ->
bool {
1562 return OpsKnown[OpNo].getKnownBits(
SQ).isNonNegative();
1566 auto IsValidPromotion = [&](
unsigned OpNo) ->
bool {
1577 if (MaxRepresentableBits < IntSz) {
1587 NumUsedLeadingBits[OpNo] =
1588 IntSz - OpsKnown[OpNo].getKnownBits(
SQ).countMinLeadingZeros();
1596 if (MaxRepresentableBits < NumUsedLeadingBits[OpNo])
1599 return !OpsFromSigned || BO.
getOpcode() != Instruction::FMul ||
1604 if (Op1FpC !=
nullptr) {
1606 if (OpsFromSigned && BO.
getOpcode() == Instruction::FMul &&
1611 OpsFromSigned ? Instruction::FPToSI : Instruction::FPToUI, Op1FpC,
1613 if (Op1IntC ==
nullptr)
1616 : Instruction::UIToFP,
1617 Op1IntC, FPTy,
DL) != Op1FpC)
1621 IntOps[1] = Op1IntC;
1625 if (IntTy != IntOps[1]->
getType())
1628 if (Op1FpC ==
nullptr) {
1629 if (!IsValidPromotion(1))
1632 if (!IsValidPromotion(0))
1638 bool NeedsOverflowCheck =
true;
1641 unsigned OverflowMaxOutputBits = OpsFromSigned ? 2 : 1;
1642 unsigned OverflowMaxCurBits =
1643 std::max(NumUsedLeadingBits[0], NumUsedLeadingBits[1]);
1644 bool OutputSigned = OpsFromSigned;
1646 case Instruction::FAdd:
1647 IntOpc = Instruction::Add;
1648 OverflowMaxOutputBits += OverflowMaxCurBits;
1650 case Instruction::FSub:
1651 IntOpc = Instruction::Sub;
1652 OverflowMaxOutputBits += OverflowMaxCurBits;
1654 case Instruction::FMul:
1655 IntOpc = Instruction::Mul;
1656 OverflowMaxOutputBits += OverflowMaxCurBits * 2;
1662 if (OverflowMaxOutputBits < IntSz) {
1663 NeedsOverflowCheck =
false;
1666 if (IntOpc == Instruction::Sub)
1667 OutputSigned =
true;
1673 if (NeedsOverflowCheck &&
1674 !willNotOverflow(IntOpc, IntOps[0], IntOps[1], BO, OutputSigned))
1677 Value *IntBinOp =
Builder.CreateBinOp(IntOpc, IntOps[0], IntOps[1]);
1679 IntBO->setHasNoSignedWrap(OutputSigned);
1680 IntBO->setHasNoUnsignedWrap(!OutputSigned);
1683 return new SIToFPInst(IntBinOp, FPTy);
1684 return new UIToFPInst(IntBinOp, FPTy);
1698 std::array<Value *, 2> IntOps = {
nullptr,
nullptr};
1716 if (Instruction *R = foldFBinOpOfIntCastsFromSign(BO,
false,
1717 IntOps, Op1FpC, OpsKnown))
1719 return foldFBinOpOfIntCastsFromSign(BO,
true, IntOps,
1735 !
X->getType()->isIntOrIntVectorTy(1))
1743 return createSelectInstWithUnknownProfile(
X, TVal, FVal);
1752 V = IsTrueArm ?
SI->getTrueValue() :
SI->getFalseValue();
1753 }
else if (
match(
SI->getCondition(),
1760 V = IsTrueArm ? ConstantInt::get(
Op->getType(), 1)
1781 bool FoldWithMultiUse,
1782 bool SimplifyBothArms) {
1784 if (!
SI->hasOneUser() && !FoldWithMultiUse)
1787 Value *TV =
SI->getTrueValue();
1788 Value *FV =
SI->getFalseValue();
1791 if (
SI->getType()->isIntOrIntVectorTy(1))
1797 for (
Value *IntrinOp :
Op.operands())
1799 for (
Value *PhiOp : PN->operands())
1811 if (CI->hasOneUse()) {
1812 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1813 if (((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1)) &&
1814 !CI->isCommutative())
1823 if (!NewTV && !NewFV)
1826 if (SimplifyBothArms && !(NewTV && NewFV))
1846 Ops.push_back(InValue);
1886 assert(
Op.isAssociative() &&
"The operation must be associative!");
1892 !
Op.hasOneUse() || !
SI->hasOneUse())
1895 Value *TV =
SI->getTrueValue();
1896 Value *FV =
SI->getFalseValue();
1914 if (!NewTV || !NewFV)
1918 Builder.CreateSelect(
SI->getCondition(), NewTV, NewFV,
"",
1924 bool AllowMultipleUses) {
1926 if (NumPHIValues == 0)
1933 bool IdenticalUsers =
false;
1934 if (!AllowMultipleUses && !OneUse) {
1938 if (UI != &
I && !
I.isIdenticalTo(UI))
1942 IdenticalUsers =
true;
1972 bool SeenNonSimplifiedInVal =
false;
1973 for (
unsigned i = 0; i != NumPHIValues; ++i) {
1984 auto WillFold = [&]() {
1989 const APInt *Ignored;
2010 if (!OneUse && !IdenticalUsers)
2013 if (SeenNonSimplifiedInVal)
2015 SeenNonSimplifiedInVal =
true;
2023 if (!BI || !
DT.isReachableFromEntry(InBB))
2039 for (
auto OpIndex : OpsToMoveUseToIncomingBB) {
2050 U = U->DoPHITranslation(PN->
getParent(), OpBB);
2053 Clones.
insert({OpBB, Clone});
2058 NewPhiValues[
OpIndex] = Clone;
2067 for (
unsigned i = 0; i != NumPHIValues; ++i)
2070 if (IdenticalUsers) {
2101 BO0->getOpcode() !=
Opc || BO1->getOpcode() !=
Opc ||
2102 !BO0->isAssociative() || !BO1->isAssociative() ||
2103 BO0->getParent() != BO1->getParent())
2107 "Expected commutative instructions!");
2111 Value *Start0, *Step0, *Start1, *Step1;
2118 "Expected PHIs with two incoming values!");
2125 if (!Init0 || !Init1 || !C0 || !C1)
2140 if (
Opc == Instruction::FAdd ||
Opc == Instruction::FMul) {
2144 NewBO->setFastMathFlags(Intersect);
2148 Flags.AllKnownNonZero =
false;
2149 Flags.mergeFlags(*BO0);
2150 Flags.mergeFlags(*BO1);
2151 Flags.mergeFlags(BO);
2152 Flags.applyFlags(*NewBO);
2154 NewBO->takeName(&BO);
2164 "Invalid incoming block!");
2165 NewPN->addIncoming(
Init, BB);
2166 }
else if (V == BO0) {
2171 "Invalid incoming block!");
2172 NewPN->addIncoming(NewBO, BB);
2178 <<
"\n with " << *PN1 <<
"\n " << *BO1
2205 if (!Phi0 || !Phi1 || !Phi0->hasOneUse() || !Phi1->hasOneUse() ||
2206 Phi0->getNumOperands() != Phi1->getNumOperands())
2210 if (BO.
getParent() != Phi0->getParent() ||
2227 auto CanFoldIncomingValuePair = [&](std::tuple<Use &, Use &>
T) {
2228 auto &Phi0Use = std::get<0>(
T);
2229 auto &Phi1Use = std::get<1>(
T);
2230 if (Phi0->getIncomingBlock(Phi0Use) != Phi1->getIncomingBlock(Phi1Use))
2232 Value *Phi0UseV = Phi0Use.get();
2233 Value *Phi1UseV = Phi1Use.get();
2236 else if (Phi1UseV ==
C)
2243 if (
all_of(
zip(Phi0->operands(), Phi1->operands()),
2244 CanFoldIncomingValuePair)) {
2247 assert(NewIncomingValues.
size() == Phi0->getNumOperands() &&
2248 "The number of collected incoming values should equal the number "
2249 "of the original PHINode operands!");
2250 for (
unsigned I = 0;
I < Phi0->getNumOperands();
I++)
2251 NewPhi->
addIncoming(NewIncomingValues[
I], Phi0->getIncomingBlock(
I));
2256 if (Phi0->getNumOperands() != 2 || Phi1->getNumOperands() != 2)
2263 ConstBB = Phi0->getIncomingBlock(0);
2264 OtherBB = Phi0->getIncomingBlock(1);
2266 ConstBB = Phi0->getIncomingBlock(1);
2267 OtherBB = Phi0->getIncomingBlock(0);
2278 if (!PredBlockBranch || !
DT.isReachableFromEntry(OtherBB))
2284 for (
auto BBIter = BO.
getParent()->begin(); &*BBIter != &BO; ++BBIter)
2295 Builder.SetInsertPoint(PredBlockBranch);
2297 Phi0->getIncomingValueForBlock(OtherBB),
2298 Phi1->getIncomingValueForBlock(OtherBB));
2300 NotFoldedNewBO->copyIRFlags(&BO);
2310 auto TryFoldOperand = [&](
unsigned OpIdx,
2329 if (
GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
2358 for (
unsigned I = 0;
I < NumElts; ++
I) {
2360 if (ShMask[
I] >= 0) {
2361 assert(ShMask[
I] < (
int)NumElts &&
"Not expecting narrowing shuffle");
2372 NewVecC[ShMask[
I]] = CElt;
2390template <Intrinsic::ID SpliceID>
2409 (
LHS->hasOneUse() ||
RHS->hasOneUse() ||
2411 return CreateBinOpSplice(V1, V2,
Offset);
2416 return CreateBinOpSplice(V1,
RHS,
Offset);
2423 return CreateBinOpSplice(
LHS, V2,
Offset);
2443 auto foldConstantsThroughSubVectorInsertSplat =
2444 [&](
Value *MaybeSubVector,
Value *MaybeSplat,
2449 !
match(MaybeSubVector,
2456 if (!SubVector || !Dest)
2458 auto *InsertVector =
2459 Builder.CreateInsertVector(Dest->
getType(), Dest, SubVector, Idx);
2467 if (
Instruction *Folded = foldConstantsThroughSubVectorInsertSplat(
2470 if (
Instruction *Folded = foldConstantsThroughSubVectorInsertSplat(
2477 Value *L0, *L1, *R0, *R1;
2481 LHS->hasOneUse() && RHS->hasOneUse() &&
2504 M, Intrinsic::vector_reverse, V->getType());
2515 (LHS->hasOneUse() || RHS->hasOneUse() ||
2516 (LHS == RHS && LHS->hasNUses(2))))
2517 return createBinOpReverse(V1, V2);
2521 return createBinOpReverse(V1, RHS);
2525 return createBinOpReverse(LHS, V2);
2536 M, Intrinsic::experimental_vp_reverse, V->getType());
2546 (LHS->hasOneUse() || RHS->hasOneUse() ||
2547 (LHS == RHS && LHS->hasNUses(2))))
2548 return createBinOpVPReverse(V1, V2, EVL);
2552 return createBinOpVPReverse(V1, RHS, EVL);
2558 return createBinOpVPReverse(LHS, V2, EVL);
2585 (LHS->hasOneUse() || RHS->hasOneUse() || LHS == RHS)) {
2587 return createBinOpShuffle(V1, V2, Mask);
2602 if (LShuf->isSelect() &&
2604 RShuf->isSelect() &&
2626 "Shuffle should not change scalar type");
2638 Value *NewLHS = ConstOp1 ? V1 : NewC;
2639 Value *NewRHS = ConstOp1 ? NewC : V1;
2640 return createBinOpShuffle(NewLHS, NewRHS, Mask);
2675 Value *NewSplat =
Builder.CreateShuffleVector(NewBO, NewMask);
2681 R->copyFastMathFlags(&Inst);
2685 NewInstBO->copyIRFlags(R);
2715 (Op0->
hasOneUse() || Op1->hasOneUse()))) {
2741 NewBinOp->setHasNoSignedWrap();
2743 NewBinOp->setHasNoUnsignedWrap();
2759 if (!
GEP.hasAllConstantIndices())
2775 Type *Ty =
GEP.getSourceElementType();
2776 Value *NewTrueC = Builder.CreateGEP(Ty, TrueC, IndexC,
"", NW);
2777 Value *NewFalseC = Builder.CreateGEP(Ty, FalseC, IndexC,
"", NW);
2787 if (
GEP.getNumIndices() != 1)
2797 unsigned IndexSizeInBits =
DL.getIndexTypeSizeInBits(PtrTy);
2808 if (NewOffset.
isZero() ||
2809 (Src->hasOneUse() &&
GEP.getOperand(1)->hasOneUse())) {
2811 if (
GEP.hasNoUnsignedWrap() &&
2831 if (!
GEP.hasAllConstantIndices())
2842 if (InnerGEP->hasAllConstantIndices())
2845 if (!InnerGEP->hasOneUse())
2848 Skipped.push_back(InnerGEP);
2854 if (Skipped.empty())
2859 if (!InnerGEP->hasOneUse())
2864 if (InnerGEP->getType() != Ty)
2870 !InnerGEP->accumulateConstantOffset(
DL,
Offset))
2873 IC.
replaceOperand(*Skipped.back(), 0, InnerGEP->getPointerOperand());
2875 SkippedGEP->setNoWrapFlags(NW);
2897 if (Src->getResultElementType() !=
GEP.getSourceElementType())
2903 if (Src->hasOneUse() &&
GEP.getNumIndices() == 1 &&
2904 Src->getNumIndices() == 1) {
2905 Value *SrcIdx = *Src->idx_begin();
2907 const APInt *ConstOffset, *TrueVal, *FalseVal;
2920 if (!
Select->hasOneUse())
2923 if (TrueVal->getBitWidth() != ConstOffset->
getBitWidth() ||
2924 FalseVal->getBitWidth() != ConstOffset->
getBitWidth())
2927 APInt NewTrueVal = *ConstOffset + *TrueVal;
2928 APInt NewFalseVal = *ConstOffset + *FalseVal;
2929 Constant *NewTrue = ConstantInt::get(
Select->getType(), NewTrueVal);
2930 Constant *NewFalse = ConstantInt::get(
Select->getType(), NewFalseVal);
2932 Cond, NewTrue, NewFalse,
"",
2937 Builder.CreateGEP(
GEP.getResultElementType(),
2938 Src->getPointerOperand(),
2939 NewSelect,
"", Flags));
2944 bool EndsWithSequential =
false;
2947 EndsWithSequential =
I.isSequential();
2948 if (!EndsWithSequential)
2953 Value *SO1 = Src->getOperand(Src->getNumOperands() - 1);
2971 Indices.
append(Src->op_begin() + 1, Src->op_end() - 1);
2976 unsigned NumNonZeroIndices =
count_if(Indices, [](
Value *Idx) {
2978 return !
C || !
C->isNullValue();
2980 if (NumNonZeroIndices > 1)
2985 Src->getSourceElementType(), Src->getOperand(0), Indices,
"",
2991 bool &DoesConsume,
unsigned Depth) {
2992 static Value *
const NonNull =
reinterpret_cast<Value *
>(uintptr_t(1));
3010 if (!WillInvertAllUses)
3017 return Builder->CreateCmp(
I->getInversePredicate(),
I->getOperand(0),
3026 DoesConsume,
Depth))
3029 DoesConsume,
Depth))
3038 DoesConsume,
Depth))
3041 DoesConsume,
Depth))
3050 DoesConsume,
Depth))
3059 DoesConsume,
Depth))
3071 bool LocalDoesConsume = DoesConsume;
3073 LocalDoesConsume,
Depth))
3076 LocalDoesConsume,
Depth)) {
3077 DoesConsume = LocalDoesConsume;
3080 DoesConsume,
Depth);
3081 assert(NotB !=
nullptr &&
3082 "Unable to build inverted value for known freely invertable op");
3084 return Builder->CreateBinaryIntrinsic(
3087 Cond, NotA, NotB,
"",
3095 bool LocalDoesConsume = DoesConsume;
3097 for (
Use &U : PN->operands()) {
3098 BasicBlock *IncomingBlock = PN->getIncomingBlock(U);
3102 if (NewIncomingVal ==
nullptr)
3105 if (NewIncomingVal == V)
3108 IncomingValues.
emplace_back(NewIncomingVal, IncomingBlock);
3111 DoesConsume = LocalDoesConsume;
3116 Builder->CreatePHI(PN->getType(), PN->getNumIncomingValues());
3117 for (
auto [Val, Pred] : IncomingValues)
3126 DoesConsume,
Depth))
3127 return Builder ?
Builder->CreateSExt(AV, V->getType()) : NonNull;
3133 DoesConsume,
Depth))
3134 return Builder ?
Builder->CreateTrunc(AV, V->getType()) : NonNull;
3142 bool IsLogical,
Value *
A,
3144 bool LocalDoesConsume = DoesConsume;
3146 LocalDoesConsume,
Depth))
3149 LocalDoesConsume,
Depth)) {
3151 LocalDoesConsume,
Depth);
3152 DoesConsume = LocalDoesConsume;
3154 return Builder ?
Builder->CreateLogicalOp(Opcode, NotA, NotB) : NonNull;
3155 return Builder ?
Builder->CreateBinOp(Opcode, NotA, NotB) : NonNull;
3162 return TryInvertAndOrUsingDeMorgan(Instruction::And,
false,
A,
3166 return TryInvertAndOrUsingDeMorgan(Instruction::Or,
false,
A,
3170 return TryInvertAndOrUsingDeMorgan(Instruction::And,
true,
A,
3174 return TryInvertAndOrUsingDeMorgan(Instruction::Or,
true,
A,
3183 Type *GEPEltType =
GEP.getSourceElementType();
3194 if (
GEP.getNumIndices() == 1 &&
3203 return PtrOpGep && PtrOpGep->hasAllConstantIndices() &&
3206 return match(V, m_APInt(C)) && !C->isZero();
3230 if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands() ||
3231 Op1->getSourceElementType() != Op2->getSourceElementType())
3239 Type *CurTy =
nullptr;
3241 for (
unsigned J = 0,
F = Op1->getNumOperands(); J !=
F; ++J) {
3242 if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
3245 if (Op1->getOperand(J) != Op2->getOperand(J)) {
3254 assert(CurTy &&
"No current type?");
3274 CurTy = Op1->getSourceElementType();
3282 NW &= Op2->getNoWrapFlags();
3292 NewGEP->setNoWrapFlags(NW);
3304 Builder.SetInsertPoint(PN);
3305 NewPN = Builder.CreatePHI(Op1->getOperand(DI)->getType(),
3313 NewGEP->setOperand(DI, NewPN);
3316 NewGEP->insertBefore(*
GEP.getParent(),
GEP.getParent()->getFirstInsertionPt());
3323 Type *GEPType =
GEP.getType();
3324 Type *GEPEltType =
GEP.getSourceElementType();
3327 SQ.getWithInstruction(&
GEP)))
3334 auto VWidth = GEPFVTy->getNumElements();
3335 APInt PoisonElts(VWidth, 0);
3347 bool MadeChange =
false;
3351 Type *NewScalarIndexTy =
3352 DL.getIndexType(
GEP.getPointerOperandType()->getScalarType());
3361 Type *IndexTy = (*I)->getType();
3362 Type *NewIndexType =
3371 if (EltTy->
isSized() &&
DL.getTypeAllocSize(EltTy).isZero())
3377 if (IndexTy != NewIndexType) {
3383 if (
GEP.hasNoUnsignedWrap() &&
GEP.hasNoUnsignedSignedWrap())
3384 *
I =
Builder.CreateZExt(*
I, NewIndexType,
"",
true);
3386 *
I =
Builder.CreateSExt(*
I, NewIndexType);
3388 *
I =
Builder.CreateTrunc(*
I, NewIndexType,
"",
GEP.hasNoUnsignedWrap(),
3389 GEP.hasNoUnsignedSignedWrap());
3398 if (!GEPEltType->
isIntegerTy(8) &&
GEP.hasAllConstantIndices()) {
3403 GEP.getNoWrapFlags()));
3415 if (LastIdx && LastIdx->isNullValue() && !LastIdx->getType()->isVectorTy()) {
3423 if (FirstIdx && FirstIdx->isNullValue() &&
3424 !FirstIdx->getType()->isVectorTy()) {
3430 GEP.getPointerOperand(),
3432 GEP.getNoWrapFlags()));
3439 return Op->getType()->isVectorTy() && getSplatValue(Op);
3442 for (
auto &
Op :
GEP.operands()) {
3443 if (
Op->getType()->isVectorTy())
3453 GEP.getNoWrapFlags());
3456 Res =
Builder.CreateVectorSplat(EC, Res);
3461 bool SeenNonZeroIndex =
false;
3462 for (
auto [IdxNum, Idx] :
enumerate(Indices)) {
3465 if (
C &&
C->isNullValue() && IdxNum == 0)
3468 if (!SeenNonZeroIndex) {
3469 SeenNonZeroIndex =
true;
3476 Builder.CreateGEP(GEPEltType, PtrOp, FrontIndices,
3477 GEP.getName() +
".split",
GEP.getNoWrapFlags());
3484 BackIndices,
GEP.getNoWrapFlags());
3488 auto IsCanonicalType = [](
Type *Ty) {
3490 Ty = AT->getElementType();
3491 return Ty->isIntegerTy(8);
3493 if (Indices.
size() == 1 && !IsCanonicalType(GEPEltType)) {
3494 TypeSize Scale =
DL.getTypeAllocSize(GEPEltType);
3499 GEP.setSourceElementType(NewElemTy);
3500 GEP.setResultElementType(NewElemTy);
3515 if (
GEP.getNumIndices() == 1) {
3516 unsigned AS =
GEP.getPointerAddressSpace();
3517 if (
GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
3518 DL.getIndexSizeInBits(AS)) {
3519 uint64_t TyAllocSize =
DL.getTypeAllocSize(GEPEltType).getFixedValue();
3521 if (TyAllocSize == 1) {
3530 GEPType ==
Y->getType()) {
3531 bool HasNonAddressBits =
3532 DL.getAddressSizeInBits(AS) !=
DL.getPointerSizeInBits(AS);
3539 }
else if (
auto *ExactIns =
3543 if (ExactIns->isExact()) {
3551 GEP.getPointerOperand(), V,
3552 GEP.getNoWrapFlags());
3555 if (ExactIns->isExact() && ExactIns->hasOneUse()) {
3561 std::optional<APInt> NewC;
3581 if (NewC.has_value()) {
3584 ConstantInt::get(V->getType(), *NewC));
3587 GEP.getPointerOperand(), NewOp,
3588 GEP.getNoWrapFlags());
3598 if (!
GEP.isInBounds()) {
3601 APInt BasePtrOffset(IdxWidth, 0);
3602 Value *UnderlyingPtrOp =
3604 bool CanBeNull, CanBeFreed;
3606 DL, CanBeNull, CanBeFreed);
3607 if (!CanBeNull && !CanBeFreed && DerefBytes != 0) {
3608 if (
GEP.accumulateConstantOffset(
DL, BasePtrOffset) &&
3610 APInt AllocSize(IdxWidth, DerefBytes);
3611 if (BasePtrOffset.
ule(AllocSize)) {
3613 GEP.getSourceElementType(), PtrOp, Indices,
GEP.getName());
3620 if (
GEP.hasNoUnsignedSignedWrap() && !
GEP.hasNoUnsignedWrap() &&
3622 return isKnownNonNegative(Idx, SQ.getWithInstruction(&GEP));
3630 if (
GEP.getNumIndices() == 1) {
3633 auto GetPreservedNoWrapFlags = [&](
bool AddIsNUW) {
3636 if (
GEP.hasNoUnsignedWrap() && AddIsNUW)
3637 return GEP.getNoWrapFlags();
3653 Builder.CreateGEP(
GEP.getSourceElementType(),
GEP.getPointerOperand(),
3656 Builder.CreateGEP(
GEP.getSourceElementType(),
3657 NewPtr, Idx2,
"", NWFlags));
3668 bool NUW =
match(
GEP.getOperand(1),
3671 auto *NewPtr =
Builder.CreateGEP(
3672 GEP.getSourceElementType(),
GEP.getPointerOperand(),
3673 Builder.CreateSExt(Idx1,
GEP.getOperand(1)->getType()),
"", NWFlags);
3676 Builder.CreateGEP(
GEP.getSourceElementType(), NewPtr,
3677 Builder.CreateSExt(
C,
GEP.getOperand(1)->getType()),
3686 if (Indices.
size() == 1 &&
GEP.isInBounds() &&
GEP.hasNoUnsignedWrap()) {
3700 GEP.getNoWrapFlags());
3736 return Dest && Dest->Ptr == UsedV;
3739static std::optional<ModRefInfo>
3752 return std::nullopt;
3753 switch (
I->getOpcode()) {
3756 return std::nullopt;
3758 case Instruction::AddrSpaceCast:
3759 case Instruction::BitCast:
3760 case Instruction::GetElementPtr:
3765 case Instruction::ICmp: {
3771 return std::nullopt;
3772 unsigned OtherIndex = (ICI->
getOperand(0) == PI) ? 1 : 0;
3774 return std::nullopt;
3779 auto AlignmentAndSizeKnownValid = [](
CallBase *CB) {
3783 const APInt *Alignment;
3785 return match(CB->getArgOperand(0),
m_APInt(Alignment)) &&
3791 if (CB && TLI.
getLibFunc(*CB->getCalledFunction(), TheLibFunc) &&
3792 TLI.
has(TheLibFunc) && TheLibFunc == LibFunc_aligned_alloc &&
3793 !AlignmentAndSizeKnownValid(CB))
3794 return std::nullopt;
3799 case Instruction::Call:
3802 switch (
II->getIntrinsicID()) {
3804 return std::nullopt;
3806 case Intrinsic::memmove:
3807 case Intrinsic::memcpy:
3808 case Intrinsic::memset: {
3810 if (
MI->isVolatile())
3811 return std::nullopt;
3817 return std::nullopt;
3821 case Intrinsic::assume:
3822 case Intrinsic::invariant_start:
3823 case Intrinsic::invariant_end:
3824 case Intrinsic::lifetime_start:
3825 case Intrinsic::lifetime_end:
3826 case Intrinsic::objectsize:
3829 case Intrinsic::launder_invariant_group:
3830 case Intrinsic::strip_invariant_group:
3857 return std::nullopt;
3859 case Instruction::Store: {
3861 if (
SI->isVolatile() ||
SI->getPointerOperand() != PI)
3862 return std::nullopt;
3864 return std::nullopt;
3870 case Instruction::Load: {
3873 return std::nullopt;
3875 return std::nullopt;
3883 }
while (!Worklist.
empty());
3911 std::unique_ptr<DIBuilder> DIB;
3919 bool KnowInitUndef =
false;
3920 bool KnowInitZero =
false;
3925 KnowInitUndef =
true;
3926 else if (
Init->isNullValue())
3927 KnowInitZero =
true;
3931 auto &
F = *
MI.getFunction();
3932 if (
F.hasFnAttribute(Attribute::SanitizeMemory) ||
3933 F.hasFnAttribute(Attribute::SanitizeAddress))
3934 KnowInitUndef =
false;
3949 if (
II->getIntrinsicID() == Intrinsic::objectsize) {
3952 II,
DL, &
TLI,
AA,
true, &InsertedInstructions);
3953 for (
Instruction *Inserted : InsertedInstructions)
3961 if (KnowInitZero &&
isRefSet(*Removable)) {
3964 auto *M =
Builder.CreateMemSet(
3967 MTI->getLength(), MTI->getDestAlign());
3968 M->copyMetadata(*MTI);
3981 *
C, ConstantInt::get(
C->getType(),
C->isFalseWhenEqual()));
3983 for (
auto *DVR : DVRs)
3984 if (DVR->isAddressOfVariable())
3991 assert(KnowInitZero || KnowInitUndef);
4006 F,
II->getNormalDest(),
II->getUnwindDest(), {},
"",
II->getParent());
4007 NewII->setDebugLoc(
II->getDebugLoc());
4035 for (
auto *DVR : DVRs)
4036 if (DVR->isAddressOfVariable() || DVR->getExpression()->startsWithDeref())
4037 DVR->eraseFromParent();
4083 if (FreeInstrBB->
size() != 2) {
4085 if (&Inst == &FI || &Inst == FreeInstrBBTerminator ||
4089 if (!Cast || !Cast->isNoopCast(
DL))
4110 "Broken CFG: missing edge from predecessor to successor");
4115 if (&Instr == FreeInstrBBTerminator)
4120 "Only the branch instruction should remain");
4131 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0, Attribute::NonNull);
4132 Attribute Dereferenceable = Attrs.getParamAttr(0, Attribute::Dereferenceable);
4133 if (Dereferenceable.
isValid()) {
4135 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0,
4136 Attribute::Dereferenceable);
4137 Attrs = Attrs.addDereferenceableOrNullParamAttr(FI.
getContext(), 0, Bytes);
4176 if (
TLI.getLibFunc(FI, Func) &&
TLI.has(Func) && Func == LibFunc_free)
4192 bool HasDereferenceable =
4193 F->getAttributes().getRetDereferenceableBytes() > 0;
4194 if (
F->hasRetAttribute(Attribute::NonNull) ||
4195 (HasDereferenceable &&
4197 if (
Value *V = simplifyNonNullOperand(RetVal, HasDereferenceable))
4202 if (!AttributeFuncs::isNoFPClassCompatibleType(RetTy))
4205 FPClassTest ReturnClass =
F->getAttributes().getRetNoFPClass();
4206 if (ReturnClass ==
fcNone)
4211 SQ.getWithInstruction(&RI)))
4228 if (Prev->isEHPad())
4258 if (BBI != FirstInstr)
4260 }
while (BBI != FirstInstr && BBI->isDebugOrPseudoInst());
4274 if (!
DeadEdges.insert({From, To}).second)
4279 for (
Use &U : PN.incoming_values())
4296 std::next(
I->getReverseIterator())))) {
4297 if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
4301 if (Inst.isEHPad() || Inst.getType()->isTokenTy())
4304 Inst.dropDbgRecords();
4326 return DeadEdges.contains({Pred, BB}) ||
DT.dominates(BB, Pred);
4339 if (Succ == LiveSucc)
4356 BPI->swapSuccEdgesProbabilities(BI.getParent());
4377 "Unexpected number of branch weights!");
4386 BPI->swapSuccEdgesProbabilities(BI.getParent());
4404 BPI->swapSuccEdgesProbabilities(BI.getParent());
4425 if (
DT.dominates(Edge0, U)) {
4431 if (
DT.dominates(Edge1, U)) {
4438 DC.registerBranch(&BI);
4448 unsigned CstOpIdx = IsTrueArm ? 1 : 2;
4453 BasicBlock *CstBB =
SI.findCaseValue(
C)->getCaseSuccessor();
4454 if (CstBB !=
SI.getDefaultDest())
4467 for (
auto Case :
SI.cases())
4468 if (!CR.
contains(Case.getCaseValue()->getValue()))
4477 const APInt *CondOpC;
4480 auto MaybeInvertible = [&](
Value *
Cond) -> InvertFn {
4483 return [](
const APInt &Case,
const APInt &
C) {
return Case -
C; };
4487 return [](
const APInt &Case,
const APInt &
C) {
return C - Case; };
4493 return [](
const APInt &Case,
const APInt &
C) {
return Case ^
C; };
4500 if (
auto InvertFn = MaybeInvertible(
Cond); InvertFn &&
Cond->hasOneUse()) {
4501 for (
auto &Case :
SI.cases()) {
4502 const APInt &New = InvertFn(Case.getCaseValue()->getValue(), *CondOpC);
4503 Case.setValue(ConstantInt::get(
SI.getContext(), New));
4511 all_of(
SI.cases(), [&](
const auto &Case) {
4512 return Case.getCaseValue()->getValue().countr_zero() >= ShiftAmt;
4518 Value *NewCond = Op0;
4525 for (
auto Case :
SI.cases()) {
4526 const APInt &CaseVal = Case.getCaseValue()->getValue();
4528 : CaseVal.
lshr(ShiftAmt);
4529 Case.setValue(ConstantInt::get(
SI.getContext(), ShiftedCase));
4541 if (
all_of(
SI.cases(), [&](
const auto &Case) {
4542 const APInt &CaseVal = Case.getCaseValue()->getValue();
4543 return IsZExt ? CaseVal.isIntN(NewWidth)
4544 : CaseVal.isSignedIntN(NewWidth);
4546 for (
auto &Case :
SI.cases()) {
4547 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
4548 Case.setValue(ConstantInt::get(
SI.getContext(), TruncatedCase));
4570 for (
const auto &
C :
SI.cases()) {
4572 std::min(LeadingKnownZeros,
C.getCaseValue()->getValue().countl_zero());
4574 std::min(LeadingKnownOnes,
C.getCaseValue()->getValue().countl_one());
4577 unsigned NewWidth = Known.
getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes);
4583 if (NewWidth > 0 && NewWidth < Known.
getBitWidth() &&
4584 shouldChangeType(Known.
getBitWidth(), NewWidth)) {
4589 for (
auto Case :
SI.cases()) {
4590 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
4591 Case.setValue(ConstantInt::get(
SI.getContext(), TruncatedCase));
4602 SI.findCaseValue(CI)->getCaseSuccessor());
4616 const APInt *
C =
nullptr;
4618 if (*EV.
idx_begin() == 0 && (OvID == Intrinsic::smul_with_overflow ||
4619 OvID == Intrinsic::umul_with_overflow)) {
4624 if (
C->isPowerOf2()) {
4625 return BinaryOperator::CreateShl(
4627 ConstantInt::get(WO->getLHS()->getType(),
C->logBase2()));
4635 if (!WO->hasOneUse())
4649 assert(*EV.
idx_begin() == 1 &&
"Unexpected extract index for overflow inst");
4652 if (OvID == Intrinsic::usub_with_overflow)
4657 if (OvID == Intrinsic::smul_with_overflow &&
4658 WO->getLHS()->getType()->isIntOrIntVectorTy(1))
4659 return BinaryOperator::CreateAnd(WO->getLHS(), WO->getRHS());
4662 if (OvID == Intrinsic::umul_with_overflow && WO->getLHS() == WO->getRHS()) {
4663 unsigned BitWidth = WO->getLHS()->getType()->getScalarSizeInBits();
4666 return new ICmpInst(
4668 ConstantInt::get(WO->getLHS()->getType(),
4679 WO->getBinaryOp(), *
C, WO->getNoWrapKind());
4684 auto *OpTy = WO->getRHS()->getType();
4685 auto *NewLHS = WO->getLHS();
4687 NewLHS =
Builder.CreateAdd(NewLHS, ConstantInt::get(OpTy,
Offset));
4689 ConstantInt::get(OpTy, NewRHSC));
4706 const APFloat *ConstVal =
nullptr;
4707 Value *VarOp =
nullptr;
4708 bool ConstIsTrue =
false;
4715 ConstIsTrue =
false;
4720 Builder.SetInsertPoint(&EV);
4726 Value *NewEV = Builder.CreateExtractValue(NewFrexp, 0,
"mantissa");
4731 Constant *ConstantMantissa = ConstantFP::get(TrueVal->getType(), Mantissa);
4733 Value *NewSel = Builder.CreateSelectFMF(
4734 Cond, ConstIsTrue ? ConstantMantissa : NewEV,
4735 ConstIsTrue ? NewEV : ConstantMantissa,
SelectInst,
"select.frexp");
4745 SQ.getWithInstruction(&EV)))
4759 const unsigned *exti, *exte, *insi, *inse;
4760 for (exti = EV.
idx_begin(), insi =
IV->idx_begin(),
4761 exte = EV.
idx_end(), inse =
IV->idx_end();
4762 exti != exte && insi != inse;
4776 if (exti == exte && insi == inse)
4791 Value *NewEV =
Builder.CreateExtractValue(
IV->getAggregateOperand(),
4809 if (
Instruction *R = foldExtractOfOverflowIntrinsic(EV))
4815 STy && STy->isScalableTy())
4823 if (L->isSimple() && L->hasOneUse()) {
4828 for (
unsigned Idx : EV.
indices())
4835 L->getPointerOperand(), Indices);
4869 switch (Personality) {
4913 bool MakeNewInstruction =
false;
4919 bool isLastClause = i + 1 == e;
4927 if (AlreadyCaught.
insert(TypeInfo).second) {
4932 MakeNewInstruction =
true;
4939 MakeNewInstruction =
true;
4940 CleanupFlag =
false;
4959 if (!NumTypeInfos) {
4962 MakeNewInstruction =
true;
4963 CleanupFlag =
false;
4967 bool MakeNewFilter =
false;
4971 assert(NumTypeInfos > 0 &&
"Should have handled empty filter already!");
4977 MakeNewInstruction =
true;
4984 if (NumTypeInfos > 1)
4985 MakeNewFilter =
true;
4989 NewFilterElts.
reserve(NumTypeInfos);
4994 bool SawCatchAll =
false;
4995 for (
unsigned j = 0; j != NumTypeInfos; ++j) {
5023 if (SeenInFilter.
insert(TypeInfo).second)
5029 MakeNewInstruction =
true;
5034 if (NewFilterElts.
size() < NumTypeInfos)
5035 MakeNewFilter =
true;
5037 if (MakeNewFilter) {
5039 NewFilterElts.
size());
5041 MakeNewInstruction =
true;
5050 if (MakeNewFilter && !NewFilterElts.
size()) {
5051 assert(MakeNewInstruction &&
"New filter but not a new instruction!");
5052 CleanupFlag =
false;
5063 for (
unsigned i = 0, e = NewClauses.
size(); i + 1 < e; ) {
5066 for (j = i; j != e; ++j)
5073 for (
unsigned k = i; k + 1 < j; ++k)
5077 std::stable_sort(NewClauses.
begin() + i, NewClauses.
begin() + j,
5079 MakeNewInstruction =
true;
5098 for (
unsigned i = 0; i + 1 < NewClauses.
size(); ++i) {
5108 for (
unsigned j = NewClauses.
size() - 1; j != i; --j) {
5109 Value *LFilter = NewClauses[j];
5120 NewClauses.
erase(J);
5121 MakeNewInstruction =
true;
5125 unsigned LElts = LTy->getNumElements();
5135 assert(FElts <= LElts &&
"Should have handled this case earlier!");
5137 NewClauses.
erase(J);
5138 MakeNewInstruction =
true;
5147 assert(FElts > 0 &&
"Should have eliminated the empty filter earlier!");
5148 for (
unsigned l = 0; l != LElts; ++l)
5151 NewClauses.
erase(J);
5152 MakeNewInstruction =
true;
5163 bool AllFound =
true;
5164 for (
unsigned f = 0; f != FElts; ++f) {
5167 for (
unsigned l = 0; l != LElts; ++l) {
5169 if (LTypeInfo == FTypeInfo) {
5179 NewClauses.
erase(J);
5180 MakeNewInstruction =
true;
5188 if (MakeNewInstruction) {
5196 if (NewClauses.empty())
5205 assert(!CleanupFlag &&
"Adding a cleanup, not removing one?!");
5235 if (!OrigOpInst || !OrigOpInst->hasOneUse() ||
isa<PHINode>(OrigOp))
5249 Value *MaybePoisonOperand =
nullptr;
5250 for (
Value *V : OrigOpInst->operands()) {
5253 (MaybePoisonOperand && MaybePoisonOperand == V))
5255 if (!MaybePoisonOperand)
5256 MaybePoisonOperand = V;
5261 OrigOpInst->dropPoisonGeneratingAnnotations();
5264 if (!MaybePoisonOperand)
5267 Builder.SetInsertPoint(OrigOpInst);
5268 Value *FrozenMaybePoisonOperand =
Builder.CreateFreeze(
5269 MaybePoisonOperand, MaybePoisonOperand->
getName() +
".fr");
5271 OrigOpInst->replaceUsesOfWith(MaybePoisonOperand, FrozenMaybePoisonOperand);
5282 Use *StartU =
nullptr;
5300 Value *StartV = StartU->get();
5312 if (!Visited.
insert(V).second)
5315 if (Visited.
size() > 32)
5332 I->dropPoisonGeneratingAnnotations();
5334 if (StartNeedsFreeze) {
5362 MoveBefore = *MoveBeforeOpt;
5366 MoveBefore.setHeadBit(
false);
5369 if (&FI != &*MoveBefore) {
5370 FI.
moveBefore(*MoveBefore->getParent(), MoveBefore);
5375 Changed |=
Op->replaceUsesWithIf(&FI, [&](
Use &U) ->
bool {
5376 if (!
DT.dominates(&FI, U))
5379 Users.push_back(U.getUser());
5383 for (
auto *U :
Users) {
5384 for (
auto &AssumeVH :
AC.assumptionsFor(U)) {
5396 for (
auto *U : V->users()) {
5406 Value *Op0 =
I.getOperand(0);
5436 auto getUndefReplacement = [&](
Type *Ty) {
5437 auto pickCommonConstantFromPHI = [](
PHINode &PN) ->
Value * {
5441 for (
Value *V : PN.incoming_values()) {
5452 if (BestValue && BestValue !=
C)
5461 Value *BestValue =
nullptr;
5462 for (
auto *U :
I.users()) {
5463 Value *V = NullValue;
5472 if (
Value *MaybeV = pickCommonConstantFromPHI(*
PHI))
5478 else if (BestValue != V)
5479 BestValue = NullValue;
5481 assert(BestValue &&
"Must have at least one use");
5482 assert(BestValue != &
I &&
"Cannot replace with itself");
5496 Type *Ty =
C->getType();
5500 unsigned NumElts = VTy->getNumElements();
5502 for (
unsigned i = 0; i != NumElts; ++i) {
5503 Constant *EltC =
C->getAggregateElement(i);
5514 !
C->containsConstantExpression()) {
5515 if (
Constant *Repl = getFreezeVectorReplacement(
C))
5549 for (
const User *U :
I.users()) {
5550 if (Visited.
insert(U).second)
5555 while (!AllocaUsers.
empty()) {
5578 if (
isa<PHINode>(
I) ||
I->isEHPad() ||
I->mayThrow() || !
I->willReturn() ||
5595 if (CI->isConvergent())
5601 if (
I->mayWriteToMemory()) {
5608 if (
I->mayReadFromMemory() &&
5609 !
I->hasMetadata(LLVMContext::MD_invariant_load)) {
5616 E =
I->getParent()->end();
5618 if (Scan->mayWriteToMemory())
5622 I->dropDroppableUses([&](
const Use *U) {
5624 if (
I &&
I->getParent() != DestBlock) {
5634 I->moveBefore(*DestBlock, InsertPos);
5644 if (!DbgVariableRecords.
empty())
5646 DbgVariableRecords);
5669 for (
auto &DVR : DbgVariableRecords)
5670 if (DVR->getParent() != DestBlock)
5671 DbgVariableRecordsToSalvage.
push_back(DVR);
5677 if (DVR->getParent() == SrcBlock)
5678 DbgVariableRecordsToSink.
push_back(DVR);
5685 return B->getInstruction()->comesBefore(
A->getInstruction());
5692 using InstVarPair = std::pair<const Instruction *, DebugVariable>;
5694 if (DbgVariableRecordsToSink.
size() > 1) {
5700 DVR->getDebugLoc()->getInlinedAt());
5701 CountMap[std::make_pair(DVR->getInstruction(), DbgUserVariable)] += 1;
5707 for (
auto It : CountMap) {
5708 if (It.second > 1) {
5709 FilterOutMap[It.first] =
nullptr;
5710 DupSet.
insert(It.first.first);
5721 DVR.getDebugLoc()->getInlinedAt());
5723 FilterOutMap.
find(std::make_pair(Inst, DbgUserVariable));
5724 if (FilterIt == FilterOutMap.
end())
5726 if (FilterIt->second !=
nullptr)
5728 FilterIt->second = &DVR;
5743 DVR->getDebugLoc()->getInlinedAt());
5747 if (!FilterOutMap.
empty()) {
5748 InstVarPair IVP = std::make_pair(DVR->getInstruction(), DbgUserVariable);
5749 auto It = FilterOutMap.
find(IVP);
5752 if (It != FilterOutMap.
end() && It->second != DVR)
5756 if (!SunkVariables.
insert(DbgUserVariable).second)
5759 if (DVR->isDbgAssign())
5767 if (DVRClones.
empty())
5781 assert(InsertPos.getHeadBit());
5783 InsertPos->getParent()->insertDbgRecordBefore(DVRClone, InsertPos);
5807 if (
I ==
nullptr)
continue;
5822 auto getOptionalSinkBlockForInst =
5823 [
this](
Instruction *
I) -> std::optional<BasicBlock *> {
5825 return std::nullopt;
5829 unsigned NumUsers = 0;
5831 for (
Use &U :
I->uses()) {
5837 if (
II->getIntrinsicID() != Intrinsic::assume ||
5838 !
II->getOperandBundle(
"dereferenceable"))
5843 return std::nullopt;
5849 UserBB = PN->getIncomingBlock(U);
5853 if (UserParent && UserParent != UserBB)
5854 return std::nullopt;
5855 UserParent = UserBB;
5859 if (NumUsers == 0) {
5862 if (UserParent == BB || !
DT.isReachableFromEntry(UserParent))
5863 return std::nullopt;
5875 return std::nullopt;
5877 assert(
DT.dominates(BB, UserParent) &&
"Dominance relation broken?");
5885 return std::nullopt;
5890 auto OptBB = getOptionalSinkBlockForInst(
I);
5892 auto *UserParent = *OptBB;
5900 for (
Use &U :
I->operands())
5908 Builder.CollectMetadataToCopy(
5909 I, {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
5922 <<
" New = " << *Result <<
'\n');
5927 Result->setDebugLoc(Result->getDebugLoc().orElse(
I->getDebugLoc()));
5929 Result->copyMetadata(*
I, LLVMContext::MD_annotation);
5931 I->replaceAllUsesWith(Result);
5934 Result->takeName(
I);
5949 Result->insertInto(InstParent, InsertPos);
5952 Worklist.pushUsersToWorkList(*Result);
5958 <<
" New = " << *
I <<
'\n');
5990 if (!
I->hasMetadataOtherThanDebugLoc())
5993 auto Track = [](
Metadata *ScopeList,
auto &Container) {
5995 if (!MDScopeList || !Container.insert(MDScopeList).second)
5997 for (
const auto &
MDOperand : MDScopeList->operands())
5999 Container.insert(MDScope);
6002 Track(
I->getMetadata(LLVMContext::MD_alias_scope), UsedAliasScopesAndLists);
6003 Track(
I->getMetadata(LLVMContext::MD_noalias), UsedNoAliasScopesAndLists);
6012 "llvm.experimental.noalias.scope.decl in use ?");
6015 "llvm.experimental.noalias.scope should refer to a single scope");
6018 return !UsedAliasScopesAndLists.contains(MD) ||
6019 !UsedNoAliasScopesAndLists.contains(MD);
6043 if (Succ != LiveSucc &&
DeadEdges.insert({BB, Succ}).second)
6044 for (
PHINode &PN : Succ->phis())
6045 for (
Use &U : PN.incoming_values())
6054 return DeadEdges.contains({Pred, BB}) ||
DT.dominates(BB, Pred);
6056 HandleOnlyLiveSuccessor(BB,
nullptr);
6063 if (!Inst.use_empty() &&
6064 (Inst.getNumOperands() == 0 ||
isa<Constant>(Inst.getOperand(0))))
6068 Inst.replaceAllUsesWith(
C);
6071 Inst.eraseFromParent();
6077 for (
Use &U : Inst.operands()) {
6082 Constant *&FoldRes = FoldedConstants[
C];
6088 <<
"\n Old = " << *
C
6089 <<
"\n New = " << *FoldRes <<
'\n');
6098 if (!Inst.isDebugOrPseudoInst()) {
6099 InstrsForInstructionWorklist.
push_back(&Inst);
6100 SeenAliasScopes.
analyse(&Inst);
6110 HandleOnlyLiveSuccessor(BB,
nullptr);
6114 bool CondVal =
Cond->getZExtValue();
6115 HandleOnlyLiveSuccessor(BB, BI->getSuccessor(!CondVal));
6121 HandleOnlyLiveSuccessor(BB,
nullptr);
6125 HandleOnlyLiveSuccessor(BB,
6126 SI->findCaseValue(
Cond)->getCaseSuccessor());
6136 if (LiveBlocks.
count(&BB))
6139 unsigned NumDeadInstInBB;
6143 NumDeadInst += NumDeadInstInBB;
6160 Inst->eraseFromParent();
6175 Visited[BB->getNumber()] =
true;
6177 if (Visited[Succ->getNumber()])
6189 auto &
DL =
F.getDataLayout();
6191 !
F.hasFnAttribute(
"instcombine-no-verify-fixpoint");
6207 bool MadeIRChange =
false;
6212 unsigned Iteration = 0;
6216 <<
" on " <<
F.getName()
6217 <<
" reached; stopping without verifying fixpoint\n");
6222 ++NumWorklistIterations;
6223 LLVM_DEBUG(
dbgs() <<
"\n\nINSTCOMBINE ITERATION #" << Iteration <<
" on "
6224 <<
F.getName() <<
"\n");
6226 InstCombinerImpl IC(Worklist, Builder,
F,
AA, AC, TLI,
TTI, DT, ORE, BFI,
6227 BPI, PSI,
DL, RPOT);
6230 MadeChangeInThisIteration |= IC.
run();
6231 if (!MadeChangeInThisIteration)
6234 MadeIRChange =
true;
6237 "Instruction Combining on " +
Twine(
F.getName()) +
6240 "Use 'instcombine<no-verify-fixpoint>' or function attribute "
6241 "'instcombine-no-verify-fixpoint' to suppress this error.");
6247 else if (Iteration == 2)
6249 else if (Iteration == 3)
6250 ++NumThreeIterations;
6252 ++NumFourOrMoreIterations;
6254 return MadeIRChange;
6262 OS, MapClassName2PassName);
6264 OS <<
"max-iterations=" << Options.MaxIterations <<
";";
6265 OS << (Options.VerifyFixpoint ?
"" :
"no-") <<
"verify-fixpoint";
6269char InstCombinePass::ID = 0;
6275 if (LRT.shouldSkip(&ID))
6288 auto *BFI = (PSI && PSI->hasProfileSummary()) ?
6293 BFI, BPI, PSI, Options)) {
6295 LRT.update(&ID,
false);
6301 LRT.update(&ID,
true);
6343 if (
auto *WrapperPass =
6345 BPI = &WrapperPass->getBPI();
6356 "Combine redundant instructions",
false,
false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This is the interface for LLVM's primary stateless and local alias analysis.
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")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file defines the DenseMap class.
static bool isSigned(unsigned Opcode)
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
iv Induction Variable Users
static bool rightDistributesOverLeft(Instruction::BinaryOps LOp, bool HasNUW, bool HasNSW, Intrinsic::ID ROp)
Return whether "(X ROp Y) LOp Z" is always equal to "(X LOp Z) ROp (Y LOp Z)".
static bool leftDistributesOverRight(Instruction::BinaryOps LOp, bool HasNUW, bool HasNSW, Intrinsic::ID ROp)
Return whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".
This file provides internal interfaces used to implement the InstCombine.
This file provides the primary interface to the instcombine pass.
static Value * simplifySwitchOnSelectUsingRanges(SwitchInst &SI, SelectInst *Select, bool IsTrueArm)
static bool isUsedWithinShuffleVector(Value *V)
static bool isNeverEqualToUnescapedAlloc(Value *V, const TargetLibraryInfo &TLI, Instruction *AI)
static Constant * constantFoldBinOpWithSplat(unsigned Opcode, Constant *Vector, Constant *Splat, bool SplatLHS, const DataLayout &DL)
static bool shorter_filter(const Value *LHS, const Value *RHS)
static Instruction * combineConstantOffsets(GetElementPtrInst &GEP, InstCombinerImpl &IC)
Combine constant offsets separated by variable offsets.
static Instruction * foldSelectGEP(GetElementPtrInst &GEP, InstCombiner::BuilderTy &Builder)
Thread a GEP operation with constant indices through the constant true/false arms of a select.
static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src)
static cl::opt< unsigned > MaxArraySize("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine"))
static Instruction * foldSpliceBinOp(BinaryOperator &Inst, InstCombiner::BuilderTy &Builder)
static cl::opt< unsigned > ShouldLowerDbgDeclare("instcombine-lower-dbg-declare", cl::Hidden, cl::init(true))
static bool hasNoSignedWrap(BinaryOperator &I)
static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1, InstCombinerImpl &IC)
Combine constant operands of associative operations either before or after a cast to eliminate one of...
static bool combineInstructionsOverFunction(Function &F, InstructionWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const InstCombineOptions &Opts)
static Value * simplifyInstructionWithPHI(Instruction &I, PHINode *PN, Value *InValue, BasicBlock *InBB, const DataLayout &DL, const SimplifyQuery SQ)
static bool shouldCanonicalizeGEPToPtrAdd(GetElementPtrInst &GEP)
Return true if we should canonicalize the gep to an i8 ptradd.
static void ClearSubclassDataAfterReassociation(BinaryOperator &I)
Conservatively clears subclassOptionalData after a reassociation or commutation.
static Value * getIdentityValue(Instruction::BinaryOps Opcode, Value *V)
This function returns identity value for given opcode, which can be used to factor patterns like (X *...
static Value * foldFrexpOfSelect(ExtractValueInst &EV, IntrinsicInst *FrexpCall, SelectInst *SelectInst, InstCombiner::BuilderTy &Builder)
static std::optional< std::pair< Value *, Value * > > matchSymmetricPhiNodesPair(PHINode *LHS, PHINode *RHS)
static std::optional< ModRefInfo > isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< Instruction * > &Users, const TargetLibraryInfo &TLI, bool KnowInit)
static cl::opt< unsigned > MaxAllocSiteRemovableUsers("instcombine-max-allocsite-removable-users", cl::Hidden, cl::init(2048), cl::desc("Maximum number of users to visit in alloc-site " "removability analysis"))
static Value * foldOperationIntoSelectOperand(Instruction &I, SelectInst *SI, Value *NewOp, InstCombiner &IC)
static Instruction * canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP, GEPOperator *Src, InstCombinerImpl &IC)
static Instruction * tryToMoveFreeBeforeNullTest(CallInst &FI, const DataLayout &DL)
Move the call to free before a NULL test.
static Value * simplifyOperationIntoSelectOperand(Instruction &I, SelectInst *SI, bool IsTrueArm)
static Value * tryFactorization(BinaryOperator &I, const SimplifyQuery &SQ, InstCombiner::BuilderTy &Builder, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
This tries to simplify binary operations by factorizing out common terms (e.
static bool isRemovableWrite(CallBase &CB, Value *UsedV, const TargetLibraryInfo &TLI)
Given a call CB which uses an address UsedV, return true if we can prove the call's only possible eff...
static Instruction::BinaryOps getBinOpsForFactorization(Instruction::BinaryOps TopOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS, BinaryOperator *OtherOp)
This function predicates factorization using distributive laws.
static bool hasNoUnsignedWrap(BinaryOperator &I)
static bool SoleWriteToDeadLocal(Instruction *I, TargetLibraryInfo &TLI)
Check for case where the call writes to an otherwise dead alloca.
static cl::opt< unsigned > MaxSinkNumUsers("instcombine-max-sink-users", cl::init(32), cl::desc("Maximum number of undroppable users for instruction sinking"))
static Instruction * foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN, IRBuilderBase &Builder)
static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo)
Return 'true' if the given typeinfo will match anything.
static cl::opt< bool > EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"), cl::init(true))
static bool maintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C)
static GEPNoWrapFlags getMergedGEPNoWrapFlags(GEPOperator &GEP1, GEPOperator &GEP2)
Determine nowrap flags for (gep (gep p, x), y) to (gep p, (x + y)) transform.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
static bool IsSelect(unsigned Opcode, bool CheckOnlyCC=false)
Check if the opcode is a SELECT or SELECT_CC variant.
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > & Cond
static unsigned getNumElements(Type *Ty)
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
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)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static SymbolRef::Type getType(const Symbol *Sym)
static const uint32_t IV[8]
bool isNoAliasScopeDeclDead(Instruction *Inst)
void analyse(Instruction *I)
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
static constexpr roundingMode rmNearestTiesToEven
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Class for arbitrary precision integers.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
unsigned getBitWidth() const
Return the number of bits in the APInt.
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
LLVM_ABI APInt smul_ov(const APInt &RHS, bool &Overflow) const
bool isMaxSignedValue() const
Determine if this is the largest signed value.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represent a constant reference to an array (0 or more elements consecutively in memory),...
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
size_t size() const
Get the array size.
Class to represent array types.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
uint64_t getNumElements() const
Type * getElementType() const
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
LLVM_ABI uint64_t getDereferenceableBytes() const
Returns the number of dereferenceable bytes from the dereferenceable attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Legacy wrapper pass to provide the BasicAAResult object.
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...
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const Instruction & front() const
LLVM_ABI const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
InstListType::iterator iterator
Instruction iterators...
LLVM_ABI const_iterator getFirstNonPHIOrDbgOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
static LLVM_ABI BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
BinaryOps getOpcode() const
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.
static BinaryOperator * CreateNUW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Analysis pass which computes BranchProbabilityInfo.
Analysis providing branch probability information.
Represents analyses that only rely on functions' control flow.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
void setAttributes(AttributeList A)
Set the attributes for this call.
bool doesNotThrow() const
Determine if the call cannot unwind.
Value * getArgOperand(unsigned i) const
AttributeList getAttributes() const
Return the attributes for this call.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
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.
@ ICMP_UGT
unsigned greater than
@ ICMP_ULT
unsigned less than
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
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...
Conditional Branch instruction.
LLVM_ABI void swapSuccessors()
Swap the successors of this branch instruction.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
ConstantArray - Constant Array Declarations.
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getNot(Constant *C)
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
This is the shared class of boolean and integer constants.
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
This class represents a range of values.
LLVM_ABI bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
static LLVM_ABI ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
Constant Vector Declarations.
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
const Constant * stripPointerCasts() const
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
A parsed version of the target data layout string in and methods for querying it.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
static bool shouldExecute(CounterInfo &Counter)
Identifies a unique instance of a variable.
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Utility class for floating point operations which can have information about relaxed accuracy require...
Convenience struct for specifying and reasoning about fast-math flags.
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionPass class - This class is used to implement most global optimizations.
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
const BasicBlock & getEntryBlock() const
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags inBounds()
static GEPNoWrapFlags all()
static GEPNoWrapFlags noUnsignedWrap()
GEPNoWrapFlags intersectForReassociate(GEPNoWrapFlags Other) const
Given (gep (gep p, x), y), determine the nowrap flags for (gep (gep, p, y), x).
bool hasNoUnsignedWrap() const
GEPNoWrapFlags intersectForOffsetAdd(GEPNoWrapFlags Other) const
Given (gep (gep p, x), y), determine the nowrap flags for (gep p, x+y).
static GEPNoWrapFlags none()
GEPNoWrapFlags getNoWrapFlags() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static LLVM_ABI Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
static GetElementPtrInst * CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Legacy wrapper pass to provide the GlobalsAAResult object.
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getCmpPredicate() const
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Common base class shared among various IRBuilders.
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI InstCombinePass(InstCombineOptions Opts={})
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Instruction * foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I)
Tries to simplify binops of select and cast of the select condition.
Instruction * visitCondBrInst(CondBrInst &BI)
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Performs a few simplifications for operators which are associative or commutative.
Instruction * visitGEPOfGEP(GetElementPtrInst &GEP, GEPOperator *Src)
Value * foldUsingDistributiveLaws(BinaryOperator &I)
Tries to simplify binary operations which some other binary operation distributes over.
Instruction * foldBinOpShiftWithShift(BinaryOperator &I)
Instruction * visitUnreachableInst(UnreachableInst &I)
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN, bool AllowMultipleUses=false)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
void handleUnreachableFrom(Instruction *I, SmallVectorImpl< BasicBlock * > &Worklist)
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &PoisonElts, unsigned Depth=0, bool AllowMultipleUsers=false) override
The specified value produces a vector with any number of elements.
Instruction * visitFreeze(FreezeInst &I)
Instruction * foldBinOpSelectBinOp(BinaryOperator &Op)
In some cases it is beneficial to fold a select into a binary operator.
void handlePotentiallyDeadBlocks(SmallVectorImpl< BasicBlock * > &Worklist)
bool prepareWorklist(Function &F)
Perform early cleanup and prepare the InstCombine worklist.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false, bool SimplifyBothArms=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * visitFree(CallInst &FI, Value *FreedOp)
Instruction * visitExtractValueInst(ExtractValueInst &EV)
void handlePotentiallyDeadSuccessors(BasicBlock *BB, BasicBlock *LiveSucc)
Instruction * foldBinopWithRecurrence(BinaryOperator &BO)
Try to fold binary operators whose operands are simple interleaved recurrences to a single recurrence...
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * visitLandingPadInst(LandingPadInst &LI)
Instruction * visitReturnInst(ReturnInst &RI)
Instruction * visitSwitchInst(SwitchInst &SI)
Instruction * foldBinopWithPhiOperands(BinaryOperator &BO)
For a binary operator with 2 phi operands, try to hoist the binary operation before the phi.
bool SimplifyDemandedFPClass(Instruction *I, unsigned Op, FPClassTest DemandedMask, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth=0)
bool mergeStoreIntoSuccessor(StoreInst &SI)
Try to transform: if () { *P = v1; } else { *P = v2 } or: *P = v1; if () { *P = v2; }...
Instruction * tryFoldInstWithCtpopWithNot(Instruction *I)
Instruction * visitUncondBrInst(UncondBrInst &BI)
void CreateNonTerminatorUnreachable(Instruction *InsertAt)
Create and insert the idiom we use to indicate a block is unreachable without having to rewrite the C...
Value * pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI)
bool run()
Run the combiner over the entire worklist until it is empty.
Instruction * foldVectorBinop(BinaryOperator &Inst)
Canonicalize the position of binops relative to shufflevector.
bool removeInstructionsBeforeUnreachable(Instruction &I)
Value * SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS, Value *RHS)
void tryToSinkInstructionDbgVariableRecords(Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock, BasicBlock *DestBlock, SmallVectorImpl< DbgVariableRecord * > &DPUsers)
void addDeadEdge(BasicBlock *From, BasicBlock *To, SmallVectorImpl< BasicBlock * > &Worklist)
Constant * unshuffleConstant(ArrayRef< int > ShMask, Constant *C, VectorType *NewCTy)
Find a constant NewC that has property: shuffle(NewC, ShMask) = C Returns nullptr if such a constant ...
Instruction * visitAllocSite(Instruction &FI)
Instruction * visitGetElementPtrInst(GetElementPtrInst &GEP)
Value * tryFactorizationFolds(BinaryOperator &I)
This tries to simplify binary operations by factorizing out common terms (e.
Instruction * foldFreezeIntoRecurrence(FreezeInst &I, PHINode *PN)
bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock)
Try to move the specified instruction from its current block into the beginning of DestBlock,...
bool freezeOtherUses(FreezeInst &FI)
void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser=nullptr)
Freely adapt every user of V as-if V was changed to !V.
The core instruction combiner logic.
const DataLayout & getDataLayout() const
IRBuilder< TargetFolder, IRBuilderCallbackInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
unsigned ComputeNumSignBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
static bool shouldAvoidAbsorbingNotIntoSelect(const SelectInst &SI)
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
static bool isCanonicalPredicate(CmpPredicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Instruction * InsertNewInstWith(Instruction *New, BasicBlock::iterator Old)
Same as InsertNewInstBefore, but also sets the debug loc.
BranchProbabilityInfo * BPI
ReversePostOrderTraversal< BasicBlock * > & RPOT
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
std::optional< Instruction * > targetInstCombineIntrinsic(IntrinsicInst &II)
void addToWorklist(Instruction *I)
Value * getFreelyInvertedImpl(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume, unsigned Depth)
Return nonnull value if V is free to invert under the condition of WillInvertAllUses.
SmallDenseSet< std::pair< const BasicBlock *, const BasicBlock * >, 8 > BackEdges
Backedges, used to avoid pushing instructions across backedges in cases where this may result in infi...
std::optional< Value * > targetSimplifyDemandedVectorEltsIntrinsic(IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts, APInt &UndefElts2, APInt &UndefElts3, std::function< void(Instruction *, unsigned, APInt, APInt &)> SimplifyAndSetOp)
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
static Constant * getSafeVectorConstantForBinop(BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant)
Some binary operators require special handling to avoid poison and undefined behavior.
SmallDenseSet< std::pair< BasicBlock *, BasicBlock * >, 8 > DeadEdges
Edges that are known to never be taken.
std::optional< Value * > targetSimplifyDemandedUseBitsIntrinsic(IntrinsicInst &II, APInt DemandedMask, KnownBits &Known, bool &KnownBitsComputed)
bool isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
bool isBackEdge(const BasicBlock *From, const BasicBlock *To)
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, const Instruction *CxtI=nullptr, unsigned Depth=0)
void visit(Iterator Start, Iterator End)
The legacy pass manager's instcombine pass.
InstructionCombiningPass()
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
InstructionWorklist - This is the worklist management logic for InstCombine and other simplification ...
void add(Instruction *I)
Add instruction to the worklist.
LLVM_ABI void dropUBImplyingAttrsAndMetadata(ArrayRef< unsigned > Keep={})
Drop any attributes or metadata that can cause immediate undefined behavior.
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
LLVM_ABI void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
LLVM_ABI bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
bool isTerminator() const
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI bool willReturn() const LLVM_READONLY
Return true if the instruction will return (unwinding is considered as a form of returning control fl...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
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.
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.
A wrapper class for inspecting calls to intrinsic functions.
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
The landingpad instruction holds all of the information necessary to generate correct exception handl...
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
unsigned getNumClauses() const
Get the number of clauses for this landing pad.
static LLVM_ABI LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
LLVM_ABI void addClause(Constant *ClauseVal)
Add a catch or filter clause to the landing pad.
bool isCatch(unsigned Idx) const
Return 'true' if the clause and index Idx is a catch clause.
bool isFilter(unsigned Idx) const
Return 'true' if the clause and index Idx is a filter clause.
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
A function/module analysis which provides an empty LastRunTrackingInfo.
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
An instruction for reading from memory.
Value * getPointerOperand()
bool isVolatile() const
Return true if this is a load from a volatile memory location.
const MDOperand & getOperand(unsigned I) const
unsigned getNumOperands() const
Return number of MDNode operands.
Tracking metadata reference owned by Metadata.
This is the common base class for memset/memcpy/memmove.
static LLVM_ABI MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
static ICmpInst::Predicate getPredicate(Intrinsic::ID ID)
Returns the comparison predicate underlying the intrinsic.
A Module instance is used to store all the information related to an LLVM module.
MDNode * getScopeList() const
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
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...
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
In order to facilitate speculative execution, many instructions do not invoke immediate undefined beh...
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.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
bool hasProfileSummary() const
Returns true if profile summary is available.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Return a value (possibly void), from a function.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
const Value * getTrueValue() const
bool insert(const value_type &X)
Insert a new element into the SetVector.
This instruction constructs a fixed permutation of two input vectors.
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.
A SetVector that performs no allocations if smaller than a certain size.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
typename SuperClass::iterator iterator
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
TargetFolder - Create constants with target dependent folding.
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
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.
bool isVectorTy() const
True if this is an instance of VectorType.
LLVM_ABI bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
bool isPointerTy() const
True if this is an instance of PointerType.
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
bool isStructTy() const
True if this is an instance of StructType.
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
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.
LLVM_ABI const fltSemantics & getFltSemantics() const
Unconditional Branch instruction.
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
LLVM_ABI bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
LLVM_ABI bool hasOneUser() const
Return true if there is exactly one user of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
LLVMContext & getContext() const
All values hold a context through their type.
iterator_range< user_iterator > users()
bool hasUseList() const
Check if this Value has a use-list.
LLVM_ABI bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
LLVM_ABI uint64_t getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull, bool &CanBeFreed) const
Returns the number of bytes known to be dereferenceable for the pointer value.
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.
Base class of all SIMD vector types.
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Value handle that is nullable, but tries to track the Value.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
Type * getIndexedType() const
const ParentTy * getParent() const
reverse_self_iterator getReverseIterator()
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
match_combine_and< Ty... > m_CombineAnd(const Ty &...Ps)
Combine pattern matchers matching all of Ps patterns.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
auto m_Poison()
Match an arbitrary poison constant.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
ap_match< APInt > m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
match_bind< 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.
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
auto m_BinOp()
Match an arbitrary binary operation and ignore it.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
match_combine_or< CastInst_match< OpTy, UIToFPInst >, CastInst_match< OpTy, SIToFPInst > > m_IToFP(const OpTy &Op)
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_Constant()
Match an arbitrary Constant and ignore it.
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
Splat_match< T > m_ConstantSplat(const T &SubPattern)
Match a constant splat. TODO: Extend this to non-constant splats.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
SelectLike_match< CondTy, LTy, RTy > m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC)
Matches a value that behaves like a boolean-controlled select, i.e.
auto m_MaxOrMin(const LHS &L, const RHS &R)
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
m_Intrinsic_Ty< Opnd0 >::Ty m_Ctpop(const Opnd0 &Op0)
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
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)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_VectorInsert(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
initializer< Ty > init(const Ty &Val)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
FunctionAddr VTableAddr Value
void stable_sort(R &&Range)
LLVM_ABI void initializeInstructionCombiningPassPass(PassRegistry &)
cl::opt< bool > ProfcheckDisableMetadataFixes
LLVM_ABI unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB)
Remove all instructions from a basic block other than its terminator and any present EH pad instructi...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
bool succ_empty(const Instruction *I)
LLVM_ABI Value * simplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
LLVM_ABI FunctionPass * createInstructionCombiningPass()
LLVM_ABI void findDbgValues(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the dbg.values describing a value.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
auto successors(const MachineBasicBlock *BB)
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
LLVM_ABI bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI)
Return true if this is a call to an allocation function that does not have side effects that we are r...
LLVM_ABI std::optional< StringRef > getAllocationFamily(const Value *I, const TargetLibraryInfo *TLI)
If a function is part of an allocation family (e.g.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI Value * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI Value * simplifyInstructionWithOperands(Instruction *I, ArrayRef< Value * > NewOps, const SimplifyQuery &Q)
Like simplifyInstruction but the operands of I are replaced with NewOps.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
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...
gep_type_iterator gep_type_end(const User *GEP)
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI Value * getReallocatedOperand(const CallBase *CB)
If this is a call to a realloc function, return the reallocated operand.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
LLVM_ABI bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc,...
LLVM_ABI bool handleUnreachableTerminator(Instruction *I, SmallVectorImpl< Value * > &PoisonedValues)
If a terminator in an unreachable basic block has an operand of type Instruction, transform it into p...
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
LLVM_ABI Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
auto dyn_cast_or_null(const Y &Val)
constexpr bool has_single_bit(T Value) noexcept
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
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.
LLVM_ABI bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
LLVM_ABI Value * emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
constexpr unsigned MaxAnalysisRecursionDepth
auto reverse(ContainerTy &&C)
bool isModSet(const ModRefInfo MRI)
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI bool LowerDbgDeclare(Function &F)
Lowers dbg.declare records into appropriate set of dbg.value records.
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR, StoreInst *SI, DIBuilder &Builder)
Inserts a dbg.value record before a store to an alloca'd value that has an associated dbg....
LLVM_ABI void salvageDebugInfoForDbgValues(Instruction &I, ArrayRef< DbgVariableRecord * > DPInsns)
Implementation of salvageDebugInfo, applying only to instructions in Insns, rather than all debug use...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
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 Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
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 bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
auto drop_end(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the last N elements excluded.
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
@ Ref
The access may reference the value stored in memory.
@ ModRef
The access may reference and may modify the value stored in memory.
@ Mod
The access may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
bool isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I, bool IgnoreUBImplyingAttrs=true)
Don't use information from its non-constant operands.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)
If this if a call to a free function, return the freed operand.
constexpr unsigned BitWidth
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI Constant * getLosslessInvCast(Constant *C, Type *InvCastTo, unsigned CastOp, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or CastOp(InvC) is a refined valu...
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
gep_type_iterator gep_type_begin(const User *GEP)
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI void initializeInstCombine(PassRegistry &)
Initialize all passes linked into the InstCombine library.
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
LLVM_ABI Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
bool isRefSet(const ModRefInfo MRI)
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
unsigned getBitWidth() const
Get the bit width of this value.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
A CRTP mix-in to automatically provide informational APIs needed for passes.
SimplifyQuery getWithInstruction(const Instruction *I) const