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"));
157std::optional<Instruction *>
160 if (
II.getCalledFunction()->isTargetIntrinsic()) {
168 bool &KnownBitsComputed) {
170 if (
II.getCalledFunction()->isTargetIntrinsic()) {
172 *
this,
II, DemandedMask, Known, KnownBitsComputed);
183 if (
II.getCalledFunction()->isTargetIntrinsic()) {
185 *
this,
II, DemandedElts, PoisonElts, PoisonElts2, PoisonElts3,
203 auto *Inst = dyn_cast<Instruction>(
GEP);
209 if (Inst && !
GEP->hasAllConstantIndices() &&
210 !
GEP->getSourceElementType()->isIntegerTy(8)) {
230 Value *Sum =
nullptr;
231 Value *OneUseSum =
nullptr;
232 Value *OneUseBase =
nullptr;
240 auto *Inst = dyn_cast<Instruction>(
GEP);
241 if (RewriteGEPs && Inst)
245 if (
Offset->getType() != IdxTy)
247 cast<VectorType>(IdxTy)->getElementCount(),
Offset);
248 if (
GEP->hasOneUse()) {
253 OneUseBase =
GEP->getPointerOperand();
262 if (RewriteGEPs && Inst &&
263 !(
GEP->getSourceElementType()->isIntegerTy(8) &&
268 OneUseBase ? OneUseBase :
GEP->getPointerOperand(),
Offset,
"",
275 OneUseSum = OneUseBase =
nullptr;
279 Sum =
Add(Sum, OneUseSum);
290bool InstCombinerImpl::isDesirableIntType(
unsigned BitWidth)
const {
309bool InstCombinerImpl::shouldChangeType(
unsigned FromWidth,
310 unsigned ToWidth)
const {
316 if (ToWidth < FromWidth && isDesirableIntType(ToWidth))
321 if ((FromLegal || isDesirableIntType(FromWidth)) && !ToLegal)
326 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
337bool InstCombinerImpl::shouldChangeType(
Type *
From,
Type *To)
const {
343 unsigned FromWidth =
From->getPrimitiveSizeInBits();
345 return shouldChangeType(FromWidth, ToWidth);
354 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
355 if (!OBO || !OBO->hasNoSignedWrap())
358 const APInt *BVal, *CVal;
363 bool Overflow =
false;
364 switch (
I.getOpcode()) {
365 case Instruction::Add:
366 (void)BVal->
sadd_ov(*CVal, Overflow);
368 case Instruction::Sub:
369 (void)BVal->
ssub_ov(*CVal, Overflow);
371 case Instruction::Mul:
372 (void)BVal->
smul_ov(*CVal, Overflow);
382 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
383 return OBO && OBO->hasNoUnsignedWrap();
387 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
388 return OBO && OBO->hasNoSignedWrap();
397 I.clearSubclassOptionalData();
402 I.clearSubclassOptionalData();
403 I.setFastMathFlags(FMF);
412 auto *Cast = dyn_cast<CastInst>(BinOp1->
getOperand(0));
413 if (!Cast || !Cast->hasOneUse())
417 auto CastOpcode = Cast->getOpcode();
418 if (CastOpcode != Instruction::ZExt)
426 auto *BinOp2 = dyn_cast<BinaryOperator>(Cast->getOperand(0));
427 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
453 Cast->dropPoisonGeneratingFlags();
459Value *InstCombinerImpl::simplifyIntToPtrRoundTripCast(
Value *Val) {
460 auto *IntToPtr = dyn_cast<IntToPtrInst>(Val);
463 auto *PtrToInt = dyn_cast<PtrToIntInst>(IntToPtr->getOperand(0));
464 Type *CastTy = IntToPtr->getDestTy();
467 PtrToInt->getSrcTy()->getPointerAddressSpace() &&
470 return PtrToInt->getOperand(0);
497 bool Changed =
false;
505 Changed = !
I.swapOperands();
507 if (
I.isCommutative()) {
508 if (
auto Pair = matchSymmetricPair(
I.getOperand(0),
I.getOperand(1))) {
518 if (
I.isAssociative()) {
541 I.setHasNoUnsignedWrap(
true);
544 I.setHasNoSignedWrap(
true);
573 if (
I.isAssociative() &&
I.isCommutative()) {
636 if (isa<FPMathOperator>(NewBO)) {
650 I.setHasNoUnsignedWrap(
true);
668 if (LOp == Instruction::And)
669 return ROp == Instruction::Or || ROp == Instruction::Xor;
672 if (LOp == Instruction::Or)
673 return ROp == Instruction::And;
677 if (LOp == Instruction::Mul)
678 return ROp == Instruction::Add || ROp == Instruction::Sub;
701 if (isa<Constant>(V))
715 assert(
Op &&
"Expected a binary operator");
716 LHS =
Op->getOperand(0);
717 RHS =
Op->getOperand(1);
718 if (TopOpcode == Instruction::Add || TopOpcode == Instruction::Sub) {
723 Instruction::Shl, ConstantInt::get(
Op->getType(), 1),
C);
724 assert(
RHS &&
"Constant folding of immediate constants failed");
725 return Instruction::Mul;
730 if (OtherOp && OtherOp->
getOpcode() == Instruction::AShr &&
733 return Instruction::AShr;
736 return Op->getOpcode();
745 assert(
A &&
B &&
C &&
D &&
"All values must be provided");
748 Value *RetVal =
nullptr;
759 if (
A ==
C || (InnerCommutative &&
A ==
D)) {
779 if (
B ==
D || (InnerCommutative &&
B ==
C)) {
802 if (isa<BinaryOperator>(RetVal)) {
805 if (isa<OverflowingBinaryOperator>(&
I)) {
806 HasNSW =
I.hasNoSignedWrap();
807 HasNUW =
I.hasNoUnsignedWrap();
809 if (
auto *LOBO = dyn_cast<OverflowingBinaryOperator>(
LHS)) {
810 HasNSW &= LOBO->hasNoSignedWrap();
811 HasNUW &= LOBO->hasNoUnsignedWrap();
814 if (
auto *ROBO = dyn_cast<OverflowingBinaryOperator>(
RHS)) {
815 HasNSW &= ROBO->hasNoSignedWrap();
816 HasNUW &= ROBO->hasNoUnsignedWrap();
819 if (TopLevelOpcode == Instruction::Add && InnerOpcode == Instruction::Mul) {
829 cast<Instruction>(RetVal)->setHasNoSignedWrap(HasNSW);
832 cast<Instruction>(RetVal)->setHasNoUnsignedWrap(HasNUW);
847 unsigned Opc =
I->getOpcode();
848 unsigned ConstIdx = 1;
855 case Instruction::Sub:
858 case Instruction::ICmp:
865 case Instruction::Or:
869 case Instruction::Add:
875 if (!
match(
I->getOperand(1 - ConstIdx),
888 if (
Opc == Instruction::ICmp && !cast<ICmpInst>(
I)->isEquality()) {
891 if (!Cmp || !Cmp->isZeroValue())
896 bool Consumes =
false;
900 assert(NotOp !=
nullptr &&
901 "Desync between isFreeToInvert and getFreelyInverted");
910 case Instruction::Sub:
913 case Instruction::Or:
914 case Instruction::Add:
917 case Instruction::ICmp:
953 auto IsValidBinOpc = [](
unsigned Opc) {
957 case Instruction::And:
958 case Instruction::Or:
959 case Instruction::Xor:
960 case Instruction::Add:
969 auto IsCompletelyDistributable = [](
unsigned BinOpc1,
unsigned BinOpc2,
971 assert(ShOpc != Instruction::AShr);
972 return (BinOpc1 != Instruction::Add && BinOpc2 != Instruction::Add) ||
973 ShOpc == Instruction::Shl;
976 auto GetInvShift = [](
unsigned ShOpc) {
977 assert(ShOpc != Instruction::AShr);
978 return ShOpc == Instruction::LShr ? Instruction::Shl : Instruction::LShr;
981 auto CanDistributeBinops = [&](
unsigned BinOpc1,
unsigned BinOpc2,
985 if (BinOpc1 == Instruction::And)
990 if (!IsCompletelyDistributable(BinOpc1, BinOpc2, ShOpc))
996 if (BinOpc2 == Instruction::And)
1007 auto MatchBinOp = [&](
unsigned ShOpnum) ->
Instruction * {
1009 Value *
X, *
Y, *ShiftedX, *Mask, *Shift;
1010 if (!
match(
I.getOperand(ShOpnum),
1013 if (!
match(
I.getOperand(1 - ShOpnum),
1020 auto *IY = dyn_cast<Instruction>(
I.getOperand(ShOpnum));
1021 auto *IX = dyn_cast<Instruction>(ShiftedX);
1026 unsigned ShOpc = IY->getOpcode();
1027 if (ShOpc != IX->getOpcode())
1031 auto *BO2 = dyn_cast<Instruction>(
I.getOperand(1 - ShOpnum));
1035 unsigned BinOpc = BO2->getOpcode();
1037 if (!IsValidBinOpc(
I.getOpcode()) || !IsValidBinOpc(BinOpc))
1040 if (ShOpc == Instruction::AShr) {
1054 if (BinOpc ==
I.getOpcode() &&
1055 IsCompletelyDistributable(
I.getOpcode(), BinOpc, ShOpc)) {
1070 if (!CanDistributeBinops(
I.getOpcode(), BinOpc, ShOpc, CMask, CShift))
1084 return MatchBinOp(1);
1102 Value *
A, *CondVal, *TrueVal, *FalseVal;
1105 auto MatchSelectAndCast = [&](
Value *CastOp,
Value *SelectOp) {
1107 A->getType()->getScalarSizeInBits() == 1 &&
1114 if (MatchSelectAndCast(
LHS,
RHS))
1116 else if (MatchSelectAndCast(
RHS,
LHS))
1121 auto NewFoldedConst = [&](
bool IsTrueArm,
Value *V) {
1122 bool IsCastOpRHS = (CastOp ==
RHS);
1123 bool IsZExt = isa<ZExtInst>(CastOp);
1128 }
else if (IsZExt) {
1129 unsigned BitWidth = V->getType()->getScalarSizeInBits();
1142 Value *NewTrueVal = NewFoldedConst(
false, TrueVal);
1144 NewFoldedConst(
true, FalseVal));
1148 Value *NewTrueVal = NewFoldedConst(
true, TrueVal);
1150 NewFoldedConst(
false, FalseVal));
1171 if (Op0 && Op1 && LHSOpcode == RHSOpcode)
1291static std::optional<std::pair<Value *, Value *>>
1293 if (
LHS->getParent() !=
RHS->getParent())
1294 return std::nullopt;
1296 if (
LHS->getNumIncomingValues() < 2)
1297 return std::nullopt;
1300 return std::nullopt;
1302 Value *L0 =
LHS->getIncomingValue(0);
1303 Value *R0 =
RHS->getIncomingValue(0);
1305 for (
unsigned I = 1, E =
LHS->getNumIncomingValues();
I != E; ++
I) {
1309 if ((L0 == L1 && R0 == R1) || (L0 == R1 && R0 == L1))
1312 return std::nullopt;
1315 return std::optional(std::pair(L0, R0));
1318std::optional<std::pair<Value *, Value *>>
1319InstCombinerImpl::matchSymmetricPair(
Value *LHS,
Value *RHS) {
1320 Instruction *LHSInst = dyn_cast<Instruction>(LHS);
1321 Instruction *RHSInst = dyn_cast<Instruction>(RHS);
1323 return std::nullopt;
1325 case Instruction::PHI:
1327 case Instruction::Select: {
1333 return std::pair(TrueVal, FalseVal);
1334 return std::nullopt;
1336 case Instruction::Call: {
1340 if (LHSMinMax && RHSMinMax &&
1347 return std::pair(LHSMinMax->
getLHS(), LHSMinMax->
getRHS());
1348 return std::nullopt;
1351 return std::nullopt;
1361 if (!LHSIsSelect && !RHSIsSelect)
1366 if (isa<FPMathOperator>(&
I)) {
1367 FMF =
I.getFastMathFlags();
1374 Value *
Cond, *True =
nullptr, *False =
nullptr;
1382 if (Opcode != Instruction::Add || (!True && !False) || (True && False))
1397 if (LHSIsSelect && RHSIsSelect &&
A ==
D) {
1406 else if (True && !False)
1414 if (
Value *NewSel = foldAddNegate(
B,
C,
RHS))
1421 if (
Value *NewSel = foldAddNegate(E,
F,
LHS))
1425 if (!True || !False)
1436 assert(!isa<Constant>(
I) &&
"Shouldn't invert users of constant");
1438 if (U == IgnoredUser)
1440 switch (cast<Instruction>(U)->
getOpcode()) {
1441 case Instruction::Select: {
1442 auto *SI = cast<SelectInst>(U);
1444 SI->swapProfMetadata();
1447 case Instruction::Br: {
1454 case Instruction::Xor:
1461 "canFreelyInvertAllUsersOf() ?");
1471 for (
unsigned Idx = 0,
End = DbgVal->getNumVariableLocationOps();
1473 if (DbgVal->getVariableLocationOp(
Idx) ==
I)
1474 DbgVal->setExpression(
1481Value *InstCombinerImpl::dyn_castNegVal(
Value *V)
const {
1491 if (
C->getType()->getElementType()->isIntegerTy())
1495 for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1500 if (isa<UndefValue>(Elt))
1503 if (!isa<ConstantInt>(Elt))
1510 if (
auto *CV = dyn_cast<Constant>(V))
1511 if (CV->getType()->isVectorTy() &&
1512 CV->getType()->getScalarType()->isIntegerTy() && CV->getSplatValue())
1525Instruction *InstCombinerImpl::foldFBinOpOfIntCastsFromSign(
1526 BinaryOperator &BO,
bool OpsFromSigned, std::array<Value *, 2> IntOps,
1530 Type *IntTy = IntOps[0]->getType();
1535 unsigned MaxRepresentableBits =
1540 unsigned NumUsedLeadingBits[2] = {IntSz, IntSz};
1544 auto IsNonZero = [&](
unsigned OpNo) ->
bool {
1545 if (OpsKnown[OpNo].hasKnownBits() &&
1546 OpsKnown[OpNo].getKnownBits(
SQ).isNonZero())
1551 auto IsNonNeg = [&](
unsigned OpNo) ->
bool {
1555 return OpsKnown[OpNo].getKnownBits(
SQ).isNonNegative();
1559 auto IsValidPromotion = [&](
unsigned OpNo) ->
bool {
1561 if (OpsFromSigned != isa<SIToFPInst>(BO.
getOperand(OpNo)) &&
1570 if (MaxRepresentableBits < IntSz) {
1580 NumUsedLeadingBits[OpNo] =
1581 IntSz - OpsKnown[OpNo].getKnownBits(
SQ).countMinLeadingZeros();
1589 if (MaxRepresentableBits < NumUsedLeadingBits[OpNo])
1592 return !OpsFromSigned || BO.
getOpcode() != Instruction::FMul ||
1597 if (Op1FpC !=
nullptr) {
1599 if (OpsFromSigned && BO.
getOpcode() == Instruction::FMul &&
1604 OpsFromSigned ? Instruction::FPToSI : Instruction::FPToUI, Op1FpC,
1606 if (Op1IntC ==
nullptr)
1609 : Instruction::UIToFP,
1610 Op1IntC, FPTy,
DL) != Op1FpC)
1614 IntOps[1] = Op1IntC;
1618 if (IntTy != IntOps[1]->
getType())
1621 if (Op1FpC ==
nullptr) {
1622 if (!IsValidPromotion(1))
1625 if (!IsValidPromotion(0))
1631 bool NeedsOverflowCheck =
true;
1634 unsigned OverflowMaxOutputBits = OpsFromSigned ? 2 : 1;
1635 unsigned OverflowMaxCurBits =
1636 std::max(NumUsedLeadingBits[0], NumUsedLeadingBits[1]);
1637 bool OutputSigned = OpsFromSigned;
1639 case Instruction::FAdd:
1640 IntOpc = Instruction::Add;
1641 OverflowMaxOutputBits += OverflowMaxCurBits;
1643 case Instruction::FSub:
1644 IntOpc = Instruction::Sub;
1645 OverflowMaxOutputBits += OverflowMaxCurBits;
1647 case Instruction::FMul:
1648 IntOpc = Instruction::Mul;
1649 OverflowMaxOutputBits += OverflowMaxCurBits * 2;
1655 if (OverflowMaxOutputBits < IntSz) {
1656 NeedsOverflowCheck =
false;
1659 if (IntOpc == Instruction::Sub)
1660 OutputSigned =
true;
1666 if (NeedsOverflowCheck &&
1667 !willNotOverflow(IntOpc, IntOps[0], IntOps[1], BO, OutputSigned))
1671 if (
auto *IntBO = dyn_cast<BinaryOperator>(IntBinOp)) {
1672 IntBO->setHasNoSignedWrap(OutputSigned);
1673 IntBO->setHasNoUnsignedWrap(!OutputSigned);
1686 std::array<Value *, 2> IntOps = {
nullptr,
nullptr};
1706 if (
Instruction *R = foldFBinOpOfIntCastsFromSign(BO,
false,
1707 IntOps, Op1FpC, OpsKnown))
1709 return foldFBinOpOfIntCastsFromSign(BO,
true, IntOps,
1725 !
X->getType()->isIntOrIntVectorTy(1))
1742 V = IsTrueArm ? SI->getTrueValue() : SI->getFalseValue();
1743 }
else if (
match(SI->getCondition(),
1768 bool FoldWithMultiUse) {
1770 if (!SI->hasOneUse() && !FoldWithMultiUse)
1773 Value *TV = SI->getTrueValue();
1774 Value *FV = SI->getFalseValue();
1777 if (SI->getType()->isIntOrIntVectorTy(1))
1782 if (isa<MinMaxIntrinsic>(&
Op))
1783 for (
Value *IntrinOp :
Op.operands())
1784 if (
auto *PN = dyn_cast<PHINode>(IntrinOp))
1785 for (
Value *PhiOp : PN->operands())
1796 if (
auto *CI = dyn_cast<FCmpInst>(SI->getCondition())) {
1797 if (CI->hasOneUse()) {
1798 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1799 if (((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1)) &&
1800 !CI->isCommutative())
1809 if (!NewTV && !NewFV)
1846 const ICmpInst *ICmp = dyn_cast<ICmpInst>(&
I);
1861 bool AllowMultipleUses) {
1863 if (NumPHIValues == 0)
1870 bool IdenticalUsers =
false;
1871 if (!AllowMultipleUses && !OneUse) {
1875 if (UI != &
I && !
I.isIdenticalTo(UI))
1879 IdenticalUsers =
true;
1888 auto *
I = dyn_cast<Instruction>(
Op);
1893 if (isa<PHINode>(
I))
1909 bool SeenNonSimplifiedInVal =
false;
1910 for (
unsigned i = 0; i != NumPHIValues; ++i) {
1921 auto WillFold = [&]() {
1926 const APInt *Ignored;
1927 if (isa<CmpIntrinsic>(InVal) &&
1932 if (isa<ZExtInst>(InVal) &&
1933 cast<ZExtInst>(InVal)->getSrcTy()->isIntOrIntVectorTy(1) &&
1947 if (!OneUse && !IdenticalUsers)
1950 if (SeenNonSimplifiedInVal)
1952 SeenNonSimplifiedInVal =
true;
1968 if (isa<InvokeInst>(InVal))
1969 if (cast<Instruction>(InVal)->
getParent() == InBB)
1982 for (
auto OpIndex : OpsToMoveUseToIncomingBB) {
1993 U = U->DoPHITranslation(PN->
getParent(), OpBB);
1996 Clones.
insert({OpBB, Clone});
2001 NewPhiValues[
OpIndex] = Clone;
2010 for (
unsigned i = 0; i != NumPHIValues; ++i)
2013 if (IdenticalUsers) {
2031 const_cast<PHINode &
>(*NewPN),
2043 auto *BO0 = dyn_cast<BinaryOperator>(BO.
getOperand(0));
2044 auto *BO1 = dyn_cast<BinaryOperator>(BO.
getOperand(1));
2046 BO0->getOpcode() !=
Opc || BO1->getOpcode() !=
Opc ||
2047 !BO0->isAssociative() || !BO1->isAssociative() ||
2048 BO0->getParent() != BO1->getParent())
2052 "Expected commutative instructions!");
2056 Value *Start0, *Step0, *Start1, *Step1;
2063 "Expected PHIs with two incoming values!");
2066 auto *Init0 = dyn_cast<Constant>(Start0);
2067 auto *Init1 = dyn_cast<Constant>(Start1);
2068 auto *C0 = dyn_cast<Constant>(Step0);
2069 auto *C1 = dyn_cast<Constant>(Step1);
2070 if (!Init0 || !Init1 || !C0 || !C1)
2085 if (
Opc == Instruction::FAdd ||
Opc == Instruction::FMul) {
2089 NewBO->setFastMathFlags(Intersect);
2092 Flags.AllKnownNonNegative =
false;
2093 Flags.AllKnownNonZero =
false;
2094 Flags.mergeFlags(*BO0);
2095 Flags.mergeFlags(*BO1);
2096 Flags.mergeFlags(BO);
2097 Flags.applyFlags(*NewBO);
2099 NewBO->takeName(&BO);
2109 "Invalid incoming block!");
2110 NewPN->addIncoming(
Init, BB);
2111 }
else if (V == BO0) {
2116 "Invalid incoming block!");
2117 NewPN->addIncoming(NewBO, BB);
2123 <<
"\n with " << *PN1 <<
"\n " << *BO1
2148 auto *Phi0 = dyn_cast<PHINode>(BO.
getOperand(0));
2149 auto *Phi1 = dyn_cast<PHINode>(BO.
getOperand(1));
2150 if (!Phi0 || !Phi1 || !Phi0->hasOneUse() || !Phi1->hasOneUse() ||
2151 Phi0->getNumOperands() != Phi1->getNumOperands())
2155 if (BO.
getParent() != Phi0->getParent() ||
2172 auto CanFoldIncomingValuePair = [&](std::tuple<Use &, Use &>
T) {
2173 auto &Phi0Use = std::get<0>(
T);
2174 auto &Phi1Use = std::get<1>(
T);
2175 if (Phi0->getIncomingBlock(Phi0Use) != Phi1->getIncomingBlock(Phi1Use))
2177 Value *Phi0UseV = Phi0Use.get();
2178 Value *Phi1UseV = Phi1Use.get();
2181 else if (Phi1UseV ==
C)
2188 if (
all_of(
zip(Phi0->operands(), Phi1->operands()),
2189 CanFoldIncomingValuePair)) {
2192 assert(NewIncomingValues.
size() == Phi0->getNumOperands() &&
2193 "The number of collected incoming values should equal the number "
2194 "of the original PHINode operands!");
2195 for (
unsigned I = 0;
I < Phi0->getNumOperands();
I++)
2196 NewPhi->
addIncoming(NewIncomingValues[
I], Phi0->getIncomingBlock(
I));
2201 if (Phi0->getNumOperands() != 2 || Phi1->getNumOperands() != 2)
2208 ConstBB = Phi0->getIncomingBlock(0);
2209 OtherBB = Phi0->getIncomingBlock(1);
2211 ConstBB = Phi0->getIncomingBlock(1);
2212 OtherBB = Phi0->getIncomingBlock(0);
2222 auto *PredBlockBranch = dyn_cast<BranchInst>(OtherBB->
getTerminator());
2223 if (!PredBlockBranch || PredBlockBranch->isConditional() ||
2230 for (
auto BBIter = BO.
getParent()->begin(); &*BBIter != &BO; ++BBIter)
2243 Phi0->getIncomingValueForBlock(OtherBB),
2244 Phi1->getIncomingValueForBlock(OtherBB));
2245 if (
auto *NotFoldedNewBO = dyn_cast<BinaryOperator>(NewBO))
2246 NotFoldedNewBO->copyIRFlags(&BO);
2256 if (!isa<Constant>(
I.getOperand(1)))
2259 if (
auto *Sel = dyn_cast<SelectInst>(
I.getOperand(0))) {
2262 }
else if (
auto *PN = dyn_cast<PHINode>(
I.getOperand(0))) {
2273 if (
GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
2287 if (isa<ScalableVectorType>(NewCTy)) {
2295 cast<FixedVectorType>(
C->getType())->getNumElements())
2298 unsigned NewCNumElts = cast<FixedVectorType>(NewCTy)->getNumElements();
2301 unsigned NumElts = cast<FixedVectorType>(
C->getType())->getNumElements();
2302 for (
unsigned I = 0;
I < NumElts; ++
I) {
2304 if (ShMask[
I] >= 0) {
2305 assert(ShMask[
I] < (
int)NumElts &&
"Not expecting narrowing shuffle");
2313 if (!CElt || (!isa<PoisonValue>(NewCElt) && NewCElt != CElt) ||
2316 NewVecC[ShMask[
I]] = CElt;
2323 if (!isa<VectorType>(Inst.
getType()))
2329 cast<VectorType>(Inst.
getType())->getElementCount());
2331 cast<VectorType>(Inst.
getType())->getElementCount());
2336 Value *L0, *L1, *R0, *R1;
2341 cast<ShuffleVectorInst>(
LHS)->isConcat() &&
2342 cast<ShuffleVectorInst>(
RHS)->isConcat()) {
2349 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO0))
2352 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO1))
2359 if (
auto *BO = dyn_cast<BinaryOperator>(V))
2363 M, Intrinsic::vector_reverse, V->getType());
2376 return createBinOpReverse(V1, V2);
2380 return createBinOpReverse(V1,
RHS);
2384 return createBinOpReverse(
LHS, V2);
2388 if (
auto *BO = dyn_cast<BinaryOperator>(V))
2391 ElementCount EC = cast<VectorType>(V->getType())->getElementCount();
2395 M, Intrinsic::experimental_vp_reverse, V->getType());
2400 if (
match(
LHS, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
2403 if (
match(
RHS, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
2407 return createBinOpVPReverse(V1, V2, EVL);
2411 return createBinOpVPReverse(V1,
RHS, EVL);
2415 match(
RHS, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
2417 return createBinOpVPReverse(
LHS, V2, EVL);
2427 if (
auto *BO = dyn_cast<BinaryOperator>(XY))
2439 return createBinOpShuffle(V1, V2, Mask);
2448 auto *LShuf = cast<ShuffleVectorInst>(
LHS);
2449 auto *RShuf = cast<ShuffleVectorInst>(
RHS);
2454 if (LShuf->isSelect() &&
2456 RShuf->isSelect() &&
2478 "Shuffle should not change scalar type");
2480 bool ConstOp1 = isa<Constant>(
RHS);
2490 Value *NewLHS = ConstOp1 ? V1 : NewC;
2491 Value *NewRHS = ConstOp1 ? NewC : V1;
2492 return createBinOpShuffle(NewLHS, NewRHS, Mask);
2499 if (isa<ShuffleVectorInst>(
RHS))
2532 if (isa<FPMathOperator>(R)) {
2533 R->copyFastMathFlags(&Inst);
2536 if (
auto *NewInstBO = dyn_cast<BinaryOperator>(NewBO))
2537 NewInstBO->copyIRFlags(R);
2566 cast<Operator>(Op1)->getOpcode() == CastOpc &&
2567 (Op0->
hasOneUse() || Op1->hasOneUse()))) {
2585 if (!willNotOverflow(BO.
getOpcode(),
X,
Y, BO, IsSext))
2591 if (
auto *NewBinOp = dyn_cast<BinaryOperator>(NarrowBO)) {
2593 NewBinOp->setHasNoSignedWrap();
2595 NewBinOp->setHasNoUnsignedWrap();
2611 if (!
GEP.hasAllConstantIndices())
2627 Type *Ty =
GEP.getSourceElementType();
2629 Value *NewFalseC = Builder.
CreateGEP(Ty, FalseC, IndexC,
"", NW);
2639 if (
GEP.getNumIndices() != 1)
2648 Type *PtrTy = Src->getType()->getScalarType();
2649 unsigned IndexSizeInBits =
DL.getIndexTypeSizeInBits(PtrTy);
2656 if (isa<ScalableVectorType>(
BaseType))
2660 if (NewOffset.
isZero() ||
2661 (Src->hasOneUse() &&
GEP.getOperand(1)->hasOneUse())) {
2663 if (
GEP.hasNoUnsignedWrap() &&
2664 cast<GEPOperator>(Src)->hasNoUnsignedWrap() &&
2667 if (
GEP.isInBounds() && cast<GEPOperator>(Src)->isInBounds())
2691 Type *PtrTy = Src->getType()->getScalarType();
2692 if (
GEP.hasAllConstantIndices() &&
2693 (Src->hasOneUse() || Src->hasAllConstantIndices())) {
2697 bool IsFirstType =
true;
2698 unsigned NumVarIndices = 0;
2699 for (
auto Pair :
enumerate(Src->indices())) {
2700 if (!isa<ConstantInt>(Pair.value())) {
2702 IsFirstType =
false;
2703 NumVarIndices = Pair.index() + 1;
2710 if (NumVarIndices != Src->getNumIndices()) {
2730 if (!
Offset.isZero() || (!IsFirstType && !ConstIndices[0].isZero()))
2735 drop_end(Src->indices(), Src->getNumIndices() - NumVarIndices));
2742 if (
Idx.isNonNegative() != ConstIndices[0].isNonNegative())
2744 if (!
Idx.isNonNegative())
2753 if (Src->getResultElementType() !=
GEP.getSourceElementType())
2759 bool EndsWithSequential =
false;
2762 EndsWithSequential =
I.isSequential();
2765 if (EndsWithSequential) {
2768 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
2785 Indices.
append(Src->op_begin()+1, Src->op_end()-1);
2788 }
else if (isa<Constant>(*
GEP.idx_begin()) &&
2789 cast<Constant>(*
GEP.idx_begin())->isNullValue() &&
2790 Src->getNumOperands() != 1) {
2792 Indices.
append(Src->op_begin()+1, Src->op_end());
2797 unsigned NumVarIndices =
2799 if (NumVarIndices > 1)
2802 if (!Indices.
empty())
2805 Src->getSourceElementType(), Src->getOperand(0), Indices,
"",
2813 bool &DoesConsume,
unsigned Depth) {
2814 static Value *
const NonNull =
reinterpret_cast<Value *
>(uintptr_t(1));
2832 if (!WillInvertAllUses)
2837 if (
auto *
I = dyn_cast<CmpInst>(V)) {
2848 DoesConsume,
Depth))
2851 DoesConsume,
Depth))
2860 DoesConsume,
Depth))
2863 DoesConsume,
Depth))
2872 DoesConsume,
Depth))
2881 DoesConsume,
Depth))
2893 bool LocalDoesConsume = DoesConsume;
2895 LocalDoesConsume,
Depth))
2898 LocalDoesConsume,
Depth)) {
2899 DoesConsume = LocalDoesConsume;
2902 DoesConsume,
Depth);
2903 assert(NotB !=
nullptr &&
2904 "Unable to build inverted value for known freely invertable op");
2905 if (
auto *
II = dyn_cast<IntrinsicInst>(V))
2914 if (
PHINode *PN = dyn_cast<PHINode>(V)) {
2915 bool LocalDoesConsume = DoesConsume;
2917 for (
Use &U : PN->operands()) {
2918 BasicBlock *IncomingBlock = PN->getIncomingBlock(U);
2922 if (NewIncomingVal ==
nullptr)
2925 if (NewIncomingVal == V)
2928 IncomingValues.
emplace_back(NewIncomingVal, IncomingBlock);
2931 DoesConsume = LocalDoesConsume;
2937 for (
auto [Val, Pred] : IncomingValues)
2946 DoesConsume,
Depth))
2953 DoesConsume,
Depth))
2962 bool IsLogical,
Value *
A,
2964 bool LocalDoesConsume = DoesConsume;
2966 LocalDoesConsume,
Depth))
2969 LocalDoesConsume,
Depth)) {
2971 LocalDoesConsume,
Depth);
2972 DoesConsume = LocalDoesConsume;
2982 return TryInvertAndOrUsingDeMorgan(Instruction::And,
false,
A,
2986 return TryInvertAndOrUsingDeMorgan(Instruction::Or,
false,
A,
2990 return TryInvertAndOrUsingDeMorgan(Instruction::And,
true,
A,
2994 return TryInvertAndOrUsingDeMorgan(Instruction::Or,
true,
A,
3003 Type *GEPEltType =
GEP.getSourceElementType();
3014 if (
GEP.getNumIndices() == 1 &&
3022 auto PtrOpGep = dyn_cast<GEPOperator>(PtrOp);
3023 return PtrOpGep && PtrOpGep->hasAllConstantIndices() &&
3026 return match(V, m_APInt(C)) && !C->isZero();
3032 auto *Op1 = dyn_cast<GetElementPtrInst>(PN->
getOperand(0));
3049 auto *Op2 = dyn_cast<GetElementPtrInst>(*
I);
3050 if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands() ||
3051 Op1->getSourceElementType() != Op2->getSourceElementType())
3059 Type *CurTy =
nullptr;
3061 for (
unsigned J = 0,
F = Op1->getNumOperands(); J !=
F; ++J) {
3062 if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
3065 if (Op1->getOperand(J) != Op2->getOperand(J)) {
3074 assert(CurTy &&
"No current type?");
3094 CurTy = Op1->getSourceElementType();
3102 NW &= Op2->getNoWrapFlags();
3111 auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
3112 NewGEP->setNoWrapFlags(NW);
3125 NewPN = Builder.
CreatePHI(Op1->getOperand(DI)->getType(),
3130 NewPN->
addIncoming(cast<GEPOperator>(
I)->getOperand(DI),
3133 NewGEP->setOperand(DI, NewPN);
3136 NewGEP->insertBefore(*
GEP.getParent(),
GEP.getParent()->getFirstInsertionPt());
3143 Type *GEPType =
GEP.getType();
3144 Type *GEPEltType =
GEP.getSourceElementType();
3153 if (
auto *GEPFVTy = dyn_cast<FixedVectorType>(GEPType)) {
3154 auto VWidth = GEPFVTy->getNumElements();
3155 APInt PoisonElts(VWidth, 0);
3167 bool MadeChange =
false;
3171 Type *NewScalarIndexTy =
3181 Type *IndexTy = (*I)->getType();
3182 Type *NewIndexType =
3185 cast<VectorType>(IndexTy)->getElementCount())
3197 if (IndexTy != NewIndexType) {
3203 if (
GEP.hasNoUnsignedWrap() &&
GEP.hasNoUnsignedSignedWrap())
3209 GEP.hasNoUnsignedSignedWrap());
3218 if (!GEPEltType->
isIntegerTy(8) &&
GEP.hasAllConstantIndices()) {
3223 GEP.getNoWrapFlags()));
3234 auto *LastIdx = dyn_cast<Constant>(Indices.
back());
3235 if (LastIdx && LastIdx->isNullValue() && !LastIdx->getType()->isVectorTy()) {
3245 return Op->getType()->isVectorTy() && getSplatValue(Op);
3248 for (
auto &
Op :
GEP.operands()) {
3249 if (
Op->getType()->isVectorTy())
3259 GEP.getNoWrapFlags());
3261 ElementCount EC = cast<VectorType>(GEPType)->getElementCount();
3267 bool SeenVarIndex =
false;
3269 if (isa<Constant>(
Idx))
3272 if (!SeenVarIndex) {
3273 SeenVarIndex =
true;
3281 GEP.getName() +
".split",
GEP.getNoWrapFlags());
3288 BackIndices,
GEP.getNoWrapFlags());
3292 if (
auto *PN = dyn_cast<PHINode>(PtrOp)) {
3297 if (
auto *Src = dyn_cast<GEPOperator>(PtrOp))
3301 if (
GEP.getNumIndices() == 1) {
3302 unsigned AS =
GEP.getPointerAddressSpace();
3303 if (
GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
3307 if (TyAllocSize == 1) {
3316 GEPType ==
Y->getType()) {
3317 bool HasSameUnderlyingObject =
3319 bool Changed =
false;
3320 GEP.replaceUsesWithIf(
Y, [&](
Use &U) {
3321 bool ShouldReplace = HasSameUnderlyingObject ||
3322 isa<ICmpInst>(U.getUser()) ||
3323 isa<PtrToIntInst>(U.getUser());
3324 Changed |= ShouldReplace;
3325 return ShouldReplace;
3327 return Changed ? &
GEP :
nullptr;
3329 }
else if (
auto *ExactIns =
3330 dyn_cast<PossiblyExactOperator>(
GEP.getOperand(1))) {
3333 if (ExactIns->isExact()) {
3341 GEP.getPointerOperand(), V,
3342 GEP.getNoWrapFlags());
3345 if (ExactIns->isExact() && ExactIns->hasOneUse()) {
3351 std::optional<APInt> NewC;
3371 if (NewC.has_value()) {
3374 ConstantInt::get(V->getType(), *NewC));
3375 cast<BinaryOperator>(NewOp)->setIsExact();
3377 GEP.getPointerOperand(), NewOp,
3378 GEP.getNoWrapFlags());
3388 if (!
GEP.isInBounds()) {
3391 APInt BasePtrOffset(IdxWidth, 0);
3392 Value *UnderlyingPtrOp =
3394 bool CanBeNull, CanBeFreed;
3396 DL, CanBeNull, CanBeFreed);
3397 if (!CanBeNull && !CanBeFreed && DerefBytes != 0) {
3398 if (
GEP.accumulateConstantOffset(
DL, BasePtrOffset) &&
3400 APInt AllocSize(IdxWidth, DerefBytes);
3401 if (BasePtrOffset.
ule(AllocSize)) {
3403 GEP.getSourceElementType(), PtrOp, Indices,
GEP.getName());
3410 if (
GEP.hasNoUnsignedSignedWrap() && !
GEP.hasNoUnsignedWrap() &&
3412 return isKnownNonNegative(Idx, SQ.getWithInstruction(&GEP));
3420 if (
GEP.getNumIndices() == 1) {
3423 auto GetPreservedNoWrapFlags = [&](
bool AddIsNUW) {
3426 if (
GEP.hasNoUnsignedWrap() && AddIsNUW)
3427 return GEP.getNoWrapFlags();
3447 NewPtr, Idx2,
"", NWFlags));
3458 bool NUW =
match(
GEP.getOperand(1),
3462 GEP.getSourceElementType(),
GEP.getPointerOperand(),
3480 if (isa<ConstantPointerNull>(V))
3482 if (
auto *LI = dyn_cast<LoadInst>(V))
3483 return isa<GlobalVariable>(LI->getPointerOperand());
3507 return Dest && Dest->Ptr == UsedV;
3510static std::optional<ModRefInfo>
3522 switch (
I->getOpcode()) {
3525 return std::nullopt;
3527 case Instruction::AddrSpaceCast:
3528 case Instruction::BitCast:
3529 case Instruction::GetElementPtr:
3534 case Instruction::ICmp: {
3540 return std::nullopt;
3541 unsigned OtherIndex = (ICI->
getOperand(0) == PI) ? 1 : 0;
3543 return std::nullopt;
3548 auto AlignmentAndSizeKnownValid = [](
CallBase *CB) {
3552 const APInt *Alignment;
3554 return match(CB->getArgOperand(0),
m_APInt(Alignment)) &&
3558 auto *CB = dyn_cast<CallBase>(AI);
3560 if (CB && TLI.
getLibFunc(*CB->getCalledFunction(), TheLibFunc) &&
3561 TLI.
has(TheLibFunc) && TheLibFunc == LibFunc_aligned_alloc &&
3562 !AlignmentAndSizeKnownValid(CB))
3563 return std::nullopt;
3568 case Instruction::Call:
3571 switch (
II->getIntrinsicID()) {
3573 return std::nullopt;
3575 case Intrinsic::memmove:
3576 case Intrinsic::memcpy:
3577 case Intrinsic::memset: {
3579 if (
MI->isVolatile())
3580 return std::nullopt;
3586 return std::nullopt;
3590 case Intrinsic::assume:
3591 case Intrinsic::invariant_start:
3592 case Intrinsic::invariant_end:
3593 case Intrinsic::lifetime_start:
3594 case Intrinsic::lifetime_end:
3595 case Intrinsic::objectsize:
3598 case Intrinsic::launder_invariant_group:
3599 case Intrinsic::strip_invariant_group:
3626 return std::nullopt;
3628 case Instruction::Store: {
3630 if (SI->isVolatile() || SI->getPointerOperand() != PI)
3631 return std::nullopt;
3633 return std::nullopt;
3639 case Instruction::Load: {
3642 return std::nullopt;
3644 return std::nullopt;
3652 }
while (!Worklist.
empty());
3676 std::unique_ptr<DIBuilder> DIB;
3677 if (isa<AllocaInst>(
MI)) {
3684 bool KnowInitUndef =
false;
3685 bool KnowInitZero =
false;
3689 if (isa<UndefValue>(
Init))
3690 KnowInitUndef =
true;
3691 else if (
Init->isNullValue())
3692 KnowInitZero =
true;
3696 auto &
F = *
MI.getFunction();
3697 if (
F.hasFnAttribute(Attribute::SanitizeMemory) ||
3698 F.hasFnAttribute(Attribute::SanitizeAddress))
3699 KnowInitUndef =
false;
3713 if (
II->getIntrinsicID() == Intrinsic::objectsize) {
3716 II,
DL, &
TLI,
AA,
true, &InsertedInstructions);
3717 for (
Instruction *Inserted : InsertedInstructions)
3724 if (
auto *MTI = dyn_cast<MemTransferInst>(
I)) {
3725 if (KnowInitZero &&
isRefSet(*Removable)) {
3731 MTI->getLength(), MTI->getDestAlign());
3732 M->copyMetadata(*MTI);
3746 C->isFalseWhenEqual()));
3747 }
else if (
auto *SI = dyn_cast<StoreInst>(
I)) {
3748 for (
auto *DVR : DVRs)
3749 if (DVR->isAddressOfVariable())
3755 if (isa<LoadInst>(
I)) {
3756 assert(KnowInitZero || KnowInitUndef);
3771 F,
II->getNormalDest(),
II->getUnwindDest(), {},
"",
II->getParent());
3772 NewII->setDebugLoc(
II->getDebugLoc());
3800 for (
auto *DVR : DVRs)
3801 if (DVR->isAddressOfVariable() || DVR->getExpression()->startsWithDeref())
3802 DVR->eraseFromParent();
3848 if (FreeInstrBB->
size() != 2) {
3850 if (&Inst == &FI || &Inst == FreeInstrBBTerminator)
3852 auto *Cast = dyn_cast<CastInst>(&Inst);
3853 if (!Cast || !Cast->isNoopCast(
DL))
3874 "Broken CFG: missing edge from predecessor to successor");
3879 if (&Instr == FreeInstrBBTerminator)
3884 "Only the branch instruction should remain");
3895 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0, Attribute::NonNull);
3896 Attribute Dereferenceable = Attrs.getParamAttr(0, Attribute::Dereferenceable);
3897 if (Dereferenceable.
isValid()) {
3899 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0,
3900 Attribute::Dereferenceable);
3901 Attrs = Attrs.addDereferenceableOrNullParamAttr(FI.
getContext(), 0, Bytes);
3910 if (isa<UndefValue>(
Op)) {
3918 if (isa<ConstantPointerNull>(
Op))
3955 if (
RetTy->isPointerTy()) {
3956 bool HasDereferenceable =
3957 F->getAttributes().getRetDereferenceableBytes() > 0;
3958 if (
F->hasRetAttribute(Attribute::NonNull) ||
3959 (HasDereferenceable &&
3961 if (
Value *V = simplifyNonNullOperand(RetVal, HasDereferenceable))
3969 FPClassTest ReturnClass =
F->getAttributes().getRetNoFPClass();
3970 if (ReturnClass ==
fcNone)
3987 bool Changed =
false;
3993 if (Prev->isEHPad())
4025 if (BBI != FirstInstr)
4027 }
while (BBI != FirstInstr && BBI->isDebugOrPseudoInst());
4029 return dyn_cast<StoreInst>(BBI);
4041 if (!
DeadEdges.insert({From, To}).second)
4046 for (
Use &U : PN.incoming_values())
4047 if (PN.getIncomingBlock(U) ==
From && !isa<PoisonValue>(U)) {
4063 std::next(
I->getReverseIterator())))) {
4064 if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
4068 if (Inst.isEHPad() || Inst.getType()->isTokenTy())
4071 Inst.dropDbgRecords();
4079 for (
Value *V : Changed)
4106 if (Succ == LiveSucc)
4134 if (isa<SelectInst>(
Cond) &&
4155 auto *Cmp = cast<CmpInst>(
Cond);
4164 if (isa<UndefValue>(
Cond)) {
4168 if (
auto *CI = dyn_cast<ConstantInt>(
Cond)) {
4203 unsigned CstOpIdx = IsTrueArm ? 1 : 2;
4204 auto *
C = dyn_cast<ConstantInt>(
Select->getOperand(CstOpIdx));
4208 BasicBlock *CstBB = SI.findCaseValue(
C)->getCaseSuccessor();
4209 if (CstBB != SI.getDefaultDest())
4222 for (
auto Case : SI.cases())
4223 if (!CR.
contains(Case.getCaseValue()->getValue()))
4235 for (
auto Case : SI.cases()) {
4237 assert(isa<ConstantInt>(NewCase) &&
4238 "Result of expression should be constant");
4239 Case.setValue(cast<ConstantInt>(NewCase));
4247 for (
auto Case : SI.cases()) {
4249 assert(isa<ConstantInt>(NewCase) &&
4250 "Result of expression should be constant");
4251 Case.setValue(cast<ConstantInt>(NewCase));
4259 all_of(SI.cases(), [&](
const auto &Case) {
4260 return Case.getCaseValue()->getValue().countr_zero() >= ShiftAmt;
4266 Value *NewCond = Op0;
4273 for (
auto Case : SI.cases()) {
4274 const APInt &CaseVal = Case.getCaseValue()->getValue();
4276 : CaseVal.
lshr(ShiftAmt);
4277 Case.setValue(ConstantInt::get(SI.getContext(), ShiftedCase));
4285 bool IsZExt = isa<ZExtInst>(
Cond);
4289 if (
all_of(SI.cases(), [&](
const auto &Case) {
4290 const APInt &CaseVal = Case.getCaseValue()->getValue();
4291 return IsZExt ? CaseVal.isIntN(NewWidth)
4292 : CaseVal.isSignedIntN(NewWidth);
4294 for (
auto &Case : SI.cases()) {
4295 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
4296 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
4303 if (
auto *
Select = dyn_cast<SelectInst>(
Cond)) {
4318 for (
const auto &
C : SI.cases()) {
4320 std::min(LeadingKnownZeros,
C.getCaseValue()->getValue().countl_zero());
4322 std::min(LeadingKnownOnes,
C.getCaseValue()->getValue().countl_one());
4325 unsigned NewWidth = Known.
getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes);
4331 if (NewWidth > 0 && NewWidth < Known.
getBitWidth() &&
4332 shouldChangeType(Known.
getBitWidth(), NewWidth)) {
4337 for (
auto Case : SI.cases()) {
4338 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
4339 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
4344 if (isa<UndefValue>(
Cond)) {
4348 if (
auto *CI = dyn_cast<ConstantInt>(
Cond)) {
4350 SI.findCaseValue(CI)->getCaseSuccessor());
4364 const APInt *
C =
nullptr;
4366 if (*EV.
idx_begin() == 0 && (OvID == Intrinsic::smul_with_overflow ||
4367 OvID == Intrinsic::umul_with_overflow)) {
4372 if (
C->isPowerOf2()) {
4373 return BinaryOperator::CreateShl(
4375 ConstantInt::get(WO->getLHS()->getType(),
C->logBase2()));
4383 if (!WO->hasOneUse())
4397 assert(*EV.
idx_begin() == 1 &&
"Unexpected extract index for overflow inst");
4400 if (OvID == Intrinsic::usub_with_overflow)
4405 if (OvID == Intrinsic::smul_with_overflow &&
4406 WO->getLHS()->getType()->isIntOrIntVectorTy(1))
4407 return BinaryOperator::CreateAnd(WO->getLHS(), WO->getRHS());
4410 if (OvID == Intrinsic::umul_with_overflow && WO->getLHS() == WO->getRHS()) {
4411 unsigned BitWidth = WO->getLHS()->getType()->getScalarSizeInBits();
4416 ConstantInt::get(WO->getLHS()->getType(),
4427 WO->getBinaryOp(), *
C, WO->getNoWrapKind());
4432 auto *OpTy = WO->getRHS()->getType();
4433 auto *NewLHS = WO->getLHS();
4437 ConstantInt::get(OpTy, NewRHSC));
4454 const APFloat *ConstVal =
nullptr;
4455 Value *VarOp =
nullptr;
4456 bool ConstIsTrue =
false;
4463 ConstIsTrue =
false;
4479 Constant *ConstantMantissa = ConstantFP::get(TrueVal->getType(), Mantissa);
4482 Cond, ConstIsTrue ? ConstantMantissa : NewEV,
4483 ConstIsTrue ? NewEV : ConstantMantissa,
SelectInst,
"select.frexp");
4497 if (
match(&EV, m_ExtractValue<0>(m_Intrinsic<Intrinsic::frexp>(
m_Select(
4500 cast<SelectInst>(cast<IntrinsicInst>(Agg)->getArgOperand(0));
4507 const unsigned *exti, *exte, *insi, *inse;
4508 for (exti = EV.
idx_begin(), insi =
IV->idx_begin(),
4509 exte = EV.
idx_end(), inse =
IV->idx_end();
4510 exti != exte && insi != inse;
4524 if (exti == exte && insi == inse)
4557 if (
Instruction *R = foldExtractOfOverflowIntrinsic(EV))
4560 if (
LoadInst *L = dyn_cast<LoadInst>(Agg)) {
4562 if (
auto *STy = dyn_cast<StructType>(Agg->
getType());
4563 STy && STy->isScalableTy())
4571 if (L->isSimple() && L->hasOneUse()) {
4583 L->getPointerOperand(), Indices);
4594 if (
auto *PN = dyn_cast<PHINode>(Agg))
4600 if (
auto *SI = dyn_cast<SelectInst>(Agg))
4617 switch (Personality) {
4647 cast<ArrayType>(
LHS->
getType())->getNumElements()
4649 cast<ArrayType>(
RHS->
getType())->getNumElements();
4661 bool MakeNewInstruction =
false;
4667 bool isLastClause = i + 1 == e;
4675 if (AlreadyCaught.
insert(TypeInfo).second) {
4680 MakeNewInstruction =
true;
4687 MakeNewInstruction =
true;
4688 CleanupFlag =
false;
4707 if (!NumTypeInfos) {
4710 MakeNewInstruction =
true;
4711 CleanupFlag =
false;
4715 bool MakeNewFilter =
false;
4717 if (isa<ConstantAggregateZero>(FilterClause)) {
4719 assert(NumTypeInfos > 0 &&
"Should have handled empty filter already!");
4725 MakeNewInstruction =
true;
4732 if (NumTypeInfos > 1)
4733 MakeNewFilter =
true;
4737 NewFilterElts.
reserve(NumTypeInfos);
4742 bool SawCatchAll =
false;
4743 for (
unsigned j = 0; j != NumTypeInfos; ++j) {
4771 if (SeenInFilter.
insert(TypeInfo).second)
4772 NewFilterElts.
push_back(cast<Constant>(Elt));
4777 MakeNewInstruction =
true;
4782 if (NewFilterElts.
size() < NumTypeInfos)
4783 MakeNewFilter =
true;
4785 if (MakeNewFilter) {
4787 NewFilterElts.
size());
4789 MakeNewInstruction =
true;
4798 if (MakeNewFilter && !NewFilterElts.
size()) {
4799 assert(MakeNewInstruction &&
"New filter but not a new instruction!");
4800 CleanupFlag =
false;
4811 for (
unsigned i = 0, e = NewClauses.
size(); i + 1 < e; ) {
4814 for (j = i; j != e; ++j)
4815 if (!isa<ArrayType>(NewClauses[j]->
getType()))
4821 for (
unsigned k = i; k + 1 < j; ++k)
4825 std::stable_sort(NewClauses.
begin() + i, NewClauses.
begin() + j,
4827 MakeNewInstruction =
true;
4846 for (
unsigned i = 0; i + 1 < NewClauses.
size(); ++i) {
4856 for (
unsigned j = NewClauses.
size() - 1; j != i; --j) {
4857 Value *LFilter = NewClauses[j];
4868 NewClauses.
erase(J);
4869 MakeNewInstruction =
true;
4879 if (isa<ConstantAggregateZero>(LFilter)) {
4882 if (isa<ConstantAggregateZero>(
Filter)) {
4883 assert(FElts <= LElts &&
"Should have handled this case earlier!");
4885 NewClauses.
erase(J);
4886 MakeNewInstruction =
true;
4892 if (isa<ConstantAggregateZero>(
Filter)) {
4895 assert(FElts > 0 &&
"Should have eliminated the empty filter earlier!");
4896 for (
unsigned l = 0; l != LElts; ++l)
4899 NewClauses.
erase(J);
4900 MakeNewInstruction =
true;
4911 bool AllFound =
true;
4912 for (
unsigned f = 0; f != FElts; ++f) {
4915 for (
unsigned l = 0; l != LElts; ++l) {
4917 if (LTypeInfo == FTypeInfo) {
4927 NewClauses.
erase(J);
4928 MakeNewInstruction =
true;
4936 if (MakeNewInstruction) {
4944 if (NewClauses.empty())
4953 assert(!CleanupFlag &&
"Adding a cleanup, not removing one?!");
4975 auto CanPushFreeze = [](
Value *V) {
4976 if (!isa<Instruction>(V) || isa<PHINode>(V))
4996 Value *V = U->get();
4997 if (!CanPushFreeze(V)) {
5002 auto *UserI = cast<Instruction>(U->getUser());
5009 auto *
I = cast<Instruction>(V);
5010 if (!Visited.
insert(
I).second)
5021 I->dropPoisonGeneratingAnnotations();
5022 this->Worklist.add(
I);
5025 return OrigUse->get();
5035 Use *StartU =
nullptr;
5053 Value *StartV = StartU->get();
5065 if (!Visited.
insert(V).second)
5068 if (Visited.
size() > 32)
5085 I->dropPoisonGeneratingAnnotations();
5087 if (StartNeedsFreeze) {
5099 if (isa<Constant>(
Op) ||
Op->hasOneUse())
5108 if (isa<Argument>(
Op)) {
5112 auto MoveBeforeOpt = cast<Instruction>(
Op)->getInsertionPointAfterDef();
5115 MoveBefore = *MoveBeforeOpt;
5119 MoveBefore.setHeadBit(
false);
5121 bool Changed =
false;
5122 if (&FI != &*MoveBefore) {
5123 FI.
moveBefore(*MoveBefore->getParent(), MoveBefore);
5127 Op->replaceUsesWithIf(&FI, [&](
Use &U) ->
bool {
5129 Changed |= Dominates;
5138 for (
auto *U : V->users()) {
5139 if (isa<ShuffleVectorInst>(U))
5148 Value *Op0 =
I.getOperand(0);
5154 if (
auto *PN = dyn_cast<PHINode>(Op0)) {
5177 auto getUndefReplacement = [&](
Type *Ty) {
5178 Value *BestValue =
nullptr;
5180 for (
const auto *U :
I.users()) {
5181 Value *V = NullValue;
5193 else if (BestValue != V)
5194 BestValue = NullValue;
5196 assert(BestValue &&
"Must have at least one use");
5210 Type *Ty =
C->getType();
5211 auto *VTy = dyn_cast<FixedVectorType>(Ty);
5214 unsigned NumElts = VTy->getNumElements();
5216 for (
unsigned i = 0; i != NumElts; ++i) {
5217 Constant *EltC =
C->getAggregateElement(i);
5228 !
C->containsConstantExpression()) {
5229 if (
Constant *Repl = getFreezeVectorReplacement(
C))
5244 auto *CB = dyn_cast<CallBase>(
I);
5263 for (
const User *U :
I.users()) {
5264 if (Visited.
insert(U).second)
5269 while (!AllocaUsers.
empty()) {
5270 auto *UserI = cast<Instruction>(AllocaUsers.
pop_back_val());
5271 if (isa<GetElementPtrInst>(UserI) || isa<AddrSpaceCastInst>(UserI)) {
5292 if (isa<PHINode>(
I) ||
I->isEHPad() ||
I->mayThrow() || !
I->willReturn() ||
5300 if (isa<AllocaInst>(
I))
5308 if (
auto *CI = dyn_cast<CallInst>(
I)) {
5309 if (CI->isConvergent())
5315 if (
I->mayWriteToMemory()) {
5322 if (
I->mayReadFromMemory() &&
5323 !
I->hasMetadata(LLVMContext::MD_invariant_load)) {
5330 E =
I->getParent()->end();
5332 if (Scan->mayWriteToMemory())
5336 I->dropDroppableUses([&](
const Use *U) {
5337 auto *
I = dyn_cast<Instruction>(U->getUser());
5338 if (
I &&
I->getParent() != DestBlock) {
5348 I->moveBefore(*DestBlock, InsertPos);
5358 if (!DbgVariableRecords.
empty())
5360 DbgVariableRecords);
5383 for (
auto &DVR : DbgVariableRecords)
5384 if (DVR->getParent() != DestBlock)
5385 DbgVariableRecordsToSalvage.
push_back(DVR);
5391 if (DVR->getParent() == SrcBlock)
5392 DbgVariableRecordsToSink.
push_back(DVR);
5399 return B->getInstruction()->comesBefore(
A->getInstruction());
5406 using InstVarPair = std::pair<const Instruction *, DebugVariable>;
5408 if (DbgVariableRecordsToSink.
size() > 1) {
5414 DVR->getDebugLoc()->getInlinedAt());
5415 CountMap[std::make_pair(DVR->getInstruction(), DbgUserVariable)] += 1;
5421 for (
auto It : CountMap) {
5422 if (It.second > 1) {
5423 FilterOutMap[It.first] =
nullptr;
5424 DupSet.
insert(It.first.first);
5435 DVR.getDebugLoc()->getInlinedAt());
5437 FilterOutMap.
find(std::make_pair(Inst, DbgUserVariable));
5438 if (FilterIt == FilterOutMap.
end())
5440 if (FilterIt->second !=
nullptr)
5442 FilterIt->second = &DVR;
5457 DVR->getDebugLoc()->getInlinedAt());
5461 if (!FilterOutMap.
empty()) {
5462 InstVarPair IVP = std::make_pair(DVR->getInstruction(), DbgUserVariable);
5463 auto It = FilterOutMap.
find(IVP);
5466 if (It != FilterOutMap.
end() && It->second != DVR)
5470 if (!SunkVariables.
insert(DbgUserVariable).second)
5473 if (DVR->isDbgAssign())
5481 if (DVRClones.
empty())
5495 assert(InsertPos.getHeadBit());
5497 InsertPos->getParent()->insertDbgRecordBefore(DVRClone, InsertPos);
5521 if (
I ==
nullptr)
continue;
5536 auto getOptionalSinkBlockForInst =
5537 [
this](
Instruction *
I) -> std::optional<BasicBlock *> {
5539 return std::nullopt;
5543 unsigned NumUsers = 0;
5545 for (
Use &U :
I->uses()) {
5550 return std::nullopt;
5555 if (
PHINode *PN = dyn_cast<PHINode>(UserInst))
5556 UserBB = PN->getIncomingBlock(U);
5560 if (UserParent && UserParent != UserBB)
5561 return std::nullopt;
5562 UserParent = UserBB;
5566 if (NumUsers == 0) {
5570 return std::nullopt;
5582 return std::nullopt;
5592 return std::nullopt;
5597 auto OptBB = getOptionalSinkBlockForInst(
I);
5599 auto *UserParent = *OptBB;
5607 for (
Use &U :
I->operands())
5608 if (
Instruction *OpI = dyn_cast<Instruction>(U.get()))
5616 I, {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
5629 <<
" New = " << *Result <<
'\n');
5634 Result->setDebugLoc(Result->getDebugLoc().orElse(
I->getDebugLoc()));
5636 Result->copyMetadata(*
I, LLVMContext::MD_annotation);
5638 I->replaceAllUsesWith(Result);
5641 Result->takeName(
I);
5648 if (isa<PHINode>(Result) != isa<PHINode>(
I)) {
5650 if (isa<PHINode>(
I))
5656 Result->insertInto(InstParent, InsertPos);
5665 <<
" New = " << *
I <<
'\n');
5697 if (!
I->hasMetadataOtherThanDebugLoc())
5700 auto Track = [](
Metadata *ScopeList,
auto &Container) {
5701 const auto *MDScopeList = dyn_cast_or_null<MDNode>(ScopeList);
5702 if (!MDScopeList || !Container.insert(MDScopeList).second)
5704 for (
const auto &
MDOperand : MDScopeList->operands())
5705 if (
auto *MDScope = dyn_cast<MDNode>(
MDOperand))
5706 Container.insert(MDScope);
5709 Track(
I->getMetadata(LLVMContext::MD_alias_scope), UsedAliasScopesAndLists);
5710 Track(
I->getMetadata(LLVMContext::MD_noalias), UsedNoAliasScopesAndLists);
5719 "llvm.experimental.noalias.scope.decl in use ?");
5722 "llvm.experimental.noalias.scope should refer to a single scope");
5724 if (
auto *MD = dyn_cast<MDNode>(
MDOperand))
5725 return !UsedAliasScopesAndLists.
contains(MD) ||
5726 !UsedNoAliasScopesAndLists.
contains(MD);
5750 if (Succ != LiveSucc &&
DeadEdges.insert({BB, Succ}).second)
5751 for (
PHINode &PN : Succ->phis())
5752 for (
Use &U : PN.incoming_values())
5753 if (PN.getIncomingBlock(U) == BB && !isa<PoisonValue>(U)) {
5763 HandleOnlyLiveSuccessor(BB,
nullptr);
5770 if (!Inst.use_empty() &&
5771 (Inst.getNumOperands() == 0 || isa<Constant>(Inst.getOperand(0))))
5775 Inst.replaceAllUsesWith(
C);
5778 Inst.eraseFromParent();
5784 for (
Use &U : Inst.operands()) {
5785 if (!isa<ConstantVector>(U) && !isa<ConstantExpr>(U))
5788 auto *
C = cast<Constant>(U);
5789 Constant *&FoldRes = FoldedConstants[
C];
5795 <<
"\n Old = " << *
C
5796 <<
"\n New = " << *FoldRes <<
'\n');
5805 if (!Inst.isDebugOrPseudoInst()) {
5806 InstrsForInstructionWorklist.
push_back(&Inst);
5807 SeenAliasScopes.
analyse(&Inst);
5815 if (isa<UndefValue>(BI->getCondition())) {
5817 HandleOnlyLiveSuccessor(BB,
nullptr);
5820 if (
auto *
Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
5821 bool CondVal =
Cond->getZExtValue();
5822 HandleOnlyLiveSuccessor(BB, BI->getSuccessor(!CondVal));
5825 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
5826 if (isa<UndefValue>(SI->getCondition())) {
5828 HandleOnlyLiveSuccessor(BB,
nullptr);
5831 if (
auto *
Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
5832 HandleOnlyLiveSuccessor(BB,
5833 SI->findCaseValue(
Cond)->getCaseSuccessor());
5843 if (LiveBlocks.
count(&BB))
5846 unsigned NumDeadInstInBB;
5850 NumDeadInst += NumDeadInstInBB;
5867 Inst->eraseFromParent();
5896 auto &
DL =
F.getDataLayout();
5898 !
F.hasFnAttribute(
"instcombine-no-verify-fixpoint");
5906 if (
auto *Assume = dyn_cast<AssumeInst>(
I))
5914 bool MadeIRChange =
false;
5919 unsigned Iteration = 0;
5923 <<
" on " <<
F.getName()
5924 <<
" reached; stopping without verifying fixpoint\n");
5929 ++NumWorklistIterations;
5930 LLVM_DEBUG(
dbgs() <<
"\n\nINSTCOMBINE ITERATION #" << Iteration <<
" on "
5931 <<
F.getName() <<
"\n");
5934 ORE, BFI, BPI, PSI,
DL, RPOT);
5937 MadeChangeInThisIteration |= IC.
run();
5938 if (!MadeChangeInThisIteration)
5941 MadeIRChange =
true;
5944 "Instruction Combining on " +
Twine(
F.getName()) +
5947 "Use 'instcombine<no-verify-fixpoint>' or function attribute "
5948 "'instcombine-no-verify-fixpoint' to suppress this error.");
5954 else if (Iteration == 2)
5956 else if (Iteration == 3)
5957 ++NumThreeIterations;
5959 ++NumFourOrMoreIterations;
5961 return MadeIRChange;
5969 OS, MapClassName2PassName);
5976char InstCombinePass::ID = 0;
5982 if (LRT.shouldSkip(&
ID))
5995 auto *BFI = (PSI && PSI->hasProfileSummary()) ?
6000 BFI, BPI, PSI, Options)) {
6002 LRT.update(&
ID,
false);
6008 LRT.update(&
ID,
true);
6035 auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
6036 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
F);
6037 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
F);
6038 auto &
TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
F);
6039 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
6040 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
6044 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
6047 &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() :
6050 if (
auto *WrapperPass =
6051 getAnalysisIfAvailable<BranchProbabilityInfoWrapperPass>())
6052 BPI = &WrapperPass->getBPI();
6065 "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
Expand Atomic instructions
static const Function * getParent(const Value *V)
This is the interface for LLVM's primary stateless and local alias analysis.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file defines the DenseMap class.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int 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 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 bool shorter_filter(const Value *LHS, const Value *RHS)
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 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 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 bool rightDistributesOverLeft(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Return whether "(X LOp Y) ROp Z" is always equal to "(X ROp Z) LOp (Y ROp Z)".
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 std::optional< ModRefInfo > isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< WeakTrackingVH > &Users, const TargetLibraryInfo &TLI, bool KnowInit)
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.
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
static bool IsSelect(MachineInstr &MI)
#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)
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 SymbolRef::Type getType(const Symbol *Sym)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
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.
A private abstract base class describing the concept of an individual alias analysis implementation.
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 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.
A container for analyses that lazily runs them and caches their results.
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:
ArrayRef - 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
size - 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.
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 iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug(bool SkipPseudoOp=true) const
Return a const iterator range over the instructions in the block, skipping any debug instructions.
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
const Instruction & front() const
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.
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 if the block is well formed or null if the block is not well forme...
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...
Conditional or Unconditional Branch instruction.
LLVM_ABI void swapSuccessors()
Swap the successors of this branch instruction.
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
Value * getCondition() const
Analysis pass which computes BranchProbabilityInfo.
Analysis providing branch probability information.
LLVM_ABI void swapSuccEdgesProbabilities(const BasicBlock *Src)
Swap outgoing edges probabilities for Src with branch terminator.
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...
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 * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
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...
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
LLVM_ABI SmallVector< APInt > getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const
Get GEP indices to access Offset inside ElemTy.
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
unsigned getIndexSizeInBits(unsigned AS) const
The size in bits of indices used for address calculation in getelementptr and for addresses in the gi...
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
LLVM_ABI int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
static bool shouldExecute(unsigned CounterName)
Identifies a unique instance of a variable.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void registerBranch(BranchInst *BI)
Add a branch condition to the cache.
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.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
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()
GEPNoWrapFlags withoutNoUnsignedSignedWrap() const
static GEPNoWrapFlags all()
static GEPNoWrapFlags noUnsignedWrap()
bool hasNoUnsignedWrap() const
GEPNoWrapFlags intersectForOffsetAdd(GEPNoWrapFlags Other) const
Given (gep (gep p, x), y), determine the nowrap flags for (gep p, x+y).
GEPNoWrapFlags withoutNoUnsignedWrap() const
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 * CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2, const Twine &Name="")
LLVM_ABI Value * CreateSelectFMF(Value *C, Value *True, Value *False, FMFSource FMFSource, const Twine &Name="", Instruction *MDFrom=nullptr)
LLVM_ABI Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
ConstantInt * getTrue()
Get the constant value for i1 true.
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateFreeze(Value *V, const Twine &Name="")
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
void CollectMetadataToCopy(Instruction *Src, ArrayRef< unsigned > MetadataKinds)
Collect metadata with IDs MetadataKinds from Src which should be added to all created instructions.
LLVM_ABI Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
LLVM_ABI CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Value * CreateNot(Value *V, const Twine &Name="")
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, MaybeAlign Align, bool isVolatile=false, const AAMDNodes &AAInfo=AAMDNodes())
Create and insert a memset to the specified pointer and the specified value.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name="")
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
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 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 * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I)
Tries to simplify binops of select and cast of the select condition.
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)
void handlePotentiallyDeadBlocks(SmallVectorImpl< BasicBlock * > &Worklist)
bool prepareWorklist(Function &F)
Perform early cleanup and prepare the InstCombine worklist.
Instruction * visitFree(CallInst &FI, Value *FreedOp)
Instruction * visitExtractValueInst(ExtractValueInst &EV)
void handlePotentiallyDeadSuccessors(BasicBlock *BB, BasicBlock *LiveSucc)
Instruction * visitUnconditionalBranchInst(BranchInst &BI)
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.
Constant * getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp)
bool mergeStoreIntoSuccessor(StoreInst &SI)
Try to transform: if () { *P = v1; } else { *P = v2 } or: *P = v1; if () { *P = v2; } into a phi node...
Instruction * tryFoldInstWithCtpopWithNot(Instruction *I)
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)
Instruction * visitBranchInst(BranchInst &BI)
Value * tryFactorizationFolds(BinaryOperator &I)
This tries to simplify binary operations by factorizing out common terms (e.
Instruction * foldFreezeIntoRecurrence(FreezeInst &I, PHINode *PN)
Value * SimplifyDemandedUseFPClass(Value *V, FPClassTest DemandedMask, KnownFPClass &Known, Instruction *CxtI, unsigned Depth=0)
Attempts to replace V with a simpler value based on the demanded floating-point classes.
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
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)
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 ...
Instruction * removeOne()
void pushUsersToWorkList(Instruction &I)
When an instruction is simplified, add all users of the instruction to the work lists because they mi...
void add(Instruction *I)
Add instruction to the worklist.
void push(Instruction *I)
Push the instruction onto the worklist stack.
Instruction * popDeferred()
void zap()
Check that the worklist is empty and nuke the backing store for the map.
void reserve(size_t Size)
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.
This class represents min/max intrinsics.
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
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
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...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
This class represents a cast from signed integer to floating point.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
const Value * getFalseValue() const
const Value * getCondition() const
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.
bool contains(ConstPtrType Ptr) const
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.
StringRef - 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.
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isVectorTy() const
True if this is an instance of VectorType.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
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 isStructTy() const
True if this is an instance of StructType.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
LLVM_ABI const fltSemantics & getFltSemantics() const
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.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
This class represents a cast unsigned integer to floating point.
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 replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
const Use & getOperandUse(unsigned i) const
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM_ABI bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
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.
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 LLVMContext & getContext() const
All values hold a context through their type.
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 isZero() const
An efficient, type-erasing, non-owning reference to a callable.
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.
LLVM_ABI bool isNoFPClassCompatibleType(Type *Ty)
Returns true if this is a type legal for the 'nofpclass' attribute.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
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)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
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)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
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)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
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)
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.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
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.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
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)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
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".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
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)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
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".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
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)
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(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".
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
initializer< Ty > init(const Ty &Val)
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.
void stable_sort(R &&Range)
LLVM_ABI void initializeInstructionCombiningPassPass(PassRegistry &)
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,...
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.
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 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.
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.
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.
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.
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...
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...
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.
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....
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.
static constexpr roundingMode rmNearestTiesToEven
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
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
SimplifyQuery getWithoutUndef() const