108#define DEBUG_TYPE "instcombine"
116 "Number of instruction combining iterations performed");
117STATISTIC(NumOneIteration,
"Number of functions with one iteration");
118STATISTIC(NumTwoIterations,
"Number of functions with two iterations");
119STATISTIC(NumThreeIterations,
"Number of functions with three iterations");
121 "Number of functions with four or more iterations");
125STATISTIC(NumDeadInst ,
"Number of dead inst eliminated");
131 "Controls which instructions are visited");
138 "instcombine-max-sink-users",
cl::init(32),
139 cl::desc(
"Maximum number of undroppable users for instruction sinking"));
143 cl::desc(
"Maximum array size considered when doing a combine"));
155std::optional<Instruction *>
158 if (
II.getCalledFunction()->isTargetIntrinsic()) {
166 bool &KnownBitsComputed) {
168 if (
II.getCalledFunction()->isTargetIntrinsic()) {
181 if (
II.getCalledFunction()->isTargetIntrinsic()) {
183 *
this,
II, DemandedElts, PoisonElts, PoisonElts2, PoisonElts3,
198 auto *Inst = dyn_cast<Instruction>(
GEP);
205 if (Inst && !
GEP->hasOneUse() && !
GEP->hasAllConstantIndices() &&
206 !
GEP->getSourceElementType()->isIntegerTy(8)) {
220bool InstCombinerImpl::isDesirableIntType(
unsigned BitWidth)
const {
239bool InstCombinerImpl::shouldChangeType(
unsigned FromWidth,
240 unsigned ToWidth)
const {
246 if (ToWidth < FromWidth && isDesirableIntType(ToWidth))
251 if ((FromLegal || isDesirableIntType(FromWidth)) && !ToLegal)
256 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
267bool InstCombinerImpl::shouldChangeType(
Type *
From,
Type *To)
const {
273 unsigned FromWidth =
From->getPrimitiveSizeInBits();
275 return shouldChangeType(FromWidth, ToWidth);
284 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
285 if (!OBO || !OBO->hasNoSignedWrap())
290 if (Opcode != Instruction::Add && Opcode != Instruction::Sub)
293 const APInt *BVal, *CVal;
297 bool Overflow =
false;
298 if (Opcode == Instruction::Add)
299 (void)BVal->
sadd_ov(*CVal, Overflow);
301 (
void)BVal->
ssub_ov(*CVal, Overflow);
307 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
308 return OBO && OBO->hasNoUnsignedWrap();
312 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
313 return OBO && OBO->hasNoSignedWrap();
322 I.clearSubclassOptionalData();
327 I.clearSubclassOptionalData();
328 I.setFastMathFlags(FMF);
337 auto *Cast = dyn_cast<CastInst>(BinOp1->
getOperand(0));
338 if (!Cast || !Cast->hasOneUse())
342 auto CastOpcode = Cast->getOpcode();
343 if (CastOpcode != Instruction::ZExt)
351 auto *BinOp2 = dyn_cast<BinaryOperator>(Cast->getOperand(0));
352 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
378 Cast->dropPoisonGeneratingFlags();
384Value *InstCombinerImpl::simplifyIntToPtrRoundTripCast(
Value *Val) {
385 auto *IntToPtr = dyn_cast<IntToPtrInst>(Val);
388 auto *PtrToInt = dyn_cast<PtrToIntInst>(IntToPtr->getOperand(0));
389 Type *CastTy = IntToPtr->getDestTy();
392 PtrToInt->getSrcTy()->getPointerAddressSpace() &&
395 return PtrToInt->getOperand(0);
422 bool Changed =
false;
430 Changed = !
I.swapOperands();
432 if (
I.isCommutative()) {
433 if (
auto Pair = matchSymmetricPair(
I.getOperand(0),
I.getOperand(1))) {
443 if (
I.isAssociative()) {
466 I.setHasNoUnsignedWrap(
true);
469 I.setHasNoSignedWrap(
true);
498 if (
I.isAssociative() &&
I.isCommutative()) {
561 if (isa<FPMathOperator>(NewBO)) {
575 I.setHasNoUnsignedWrap(
true);
593 if (LOp == Instruction::And)
594 return ROp == Instruction::Or || ROp == Instruction::Xor;
597 if (LOp == Instruction::Or)
598 return ROp == Instruction::And;
602 if (LOp == Instruction::Mul)
603 return ROp == Instruction::Add || ROp == Instruction::Sub;
626 if (isa<Constant>(V))
640 assert(
Op &&
"Expected a binary operator");
641 LHS =
Op->getOperand(0);
642 RHS =
Op->getOperand(1);
643 if (TopOpcode == Instruction::Add || TopOpcode == Instruction::Sub) {
648 Instruction::Shl, ConstantInt::get(
Op->getType(), 1),
C);
649 assert(
RHS &&
"Constant folding of immediate constants failed");
650 return Instruction::Mul;
655 if (OtherOp && OtherOp->
getOpcode() == Instruction::AShr &&
658 return Instruction::AShr;
661 return Op->getOpcode();
670 assert(
A &&
B &&
C &&
D &&
"All values must be provided");
673 Value *RetVal =
nullptr;
684 if (
A ==
C || (InnerCommutative &&
A ==
D)) {
704 if (
B ==
D || (InnerCommutative &&
B ==
C)) {
727 if (isa<OverflowingBinaryOperator>(RetVal)) {
730 if (isa<OverflowingBinaryOperator>(&
I)) {
731 HasNSW =
I.hasNoSignedWrap();
732 HasNUW =
I.hasNoUnsignedWrap();
734 if (
auto *LOBO = dyn_cast<OverflowingBinaryOperator>(
LHS)) {
735 HasNSW &= LOBO->hasNoSignedWrap();
736 HasNUW &= LOBO->hasNoUnsignedWrap();
739 if (
auto *ROBO = dyn_cast<OverflowingBinaryOperator>(
RHS)) {
740 HasNSW &= ROBO->hasNoSignedWrap();
741 HasNUW &= ROBO->hasNoUnsignedWrap();
744 if (TopLevelOpcode == Instruction::Add && InnerOpcode == Instruction::Mul) {
754 cast<Instruction>(RetVal)->setHasNoSignedWrap(HasNSW);
757 cast<Instruction>(RetVal)->setHasNoUnsignedWrap(HasNUW);
772 unsigned Opc =
I->getOpcode();
773 unsigned ConstIdx = 1;
780 case Instruction::Sub:
783 case Instruction::ICmp:
790 case Instruction::Or:
794 case Instruction::Add:
800 if (!
match(
I->getOperand(1 - ConstIdx),
813 if (Opc == Instruction::ICmp && !cast<ICmpInst>(
I)->isEquality()) {
816 if (!Cmp || !Cmp->isZeroValue())
821 bool Consumes =
false;
825 assert(NotOp !=
nullptr &&
826 "Desync between isFreeToInvert and getFreelyInverted");
835 case Instruction::Sub:
838 case Instruction::Or:
839 case Instruction::Add:
842 case Instruction::ICmp:
878 auto IsValidBinOpc = [](
unsigned Opc) {
882 case Instruction::And:
883 case Instruction::Or:
884 case Instruction::Xor:
885 case Instruction::Add:
894 auto IsCompletelyDistributable = [](
unsigned BinOpc1,
unsigned BinOpc2,
896 assert(ShOpc != Instruction::AShr);
897 return (BinOpc1 != Instruction::Add && BinOpc2 != Instruction::Add) ||
898 ShOpc == Instruction::Shl;
901 auto GetInvShift = [](
unsigned ShOpc) {
902 assert(ShOpc != Instruction::AShr);
903 return ShOpc == Instruction::LShr ? Instruction::Shl : Instruction::LShr;
906 auto CanDistributeBinops = [&](
unsigned BinOpc1,
unsigned BinOpc2,
910 if (BinOpc1 == Instruction::And)
915 if (!IsCompletelyDistributable(BinOpc1, BinOpc2, ShOpc))
921 if (BinOpc2 == Instruction::And)
932 auto MatchBinOp = [&](
unsigned ShOpnum) ->
Instruction * {
934 Value *
X, *
Y, *ShiftedX, *Mask, *Shift;
935 if (!
match(
I.getOperand(ShOpnum),
938 if (!
match(
I.getOperand(1 - ShOpnum),
946 auto *IY = dyn_cast<Instruction>(
I.getOperand(ShOpnum));
947 auto *IX = dyn_cast<Instruction>(ShiftedX);
952 unsigned ShOpc = IY->getOpcode();
953 if (ShOpc != IX->getOpcode())
957 auto *BO2 = dyn_cast<Instruction>(
I.getOperand(1 - ShOpnum));
961 unsigned BinOpc = BO2->getOpcode();
963 if (!IsValidBinOpc(
I.getOpcode()) || !IsValidBinOpc(BinOpc))
966 if (ShOpc == Instruction::AShr) {
980 if (BinOpc ==
I.getOpcode() &&
981 IsCompletelyDistributable(
I.getOpcode(), BinOpc, ShOpc)) {
996 if (!CanDistributeBinops(
I.getOpcode(), BinOpc, ShOpc, CMask, CShift))
1010 return MatchBinOp(1);
1028 Value *
A, *CondVal, *TrueVal, *FalseVal;
1031 auto MatchSelectAndCast = [&](
Value *CastOp,
Value *SelectOp) {
1033 A->getType()->getScalarSizeInBits() == 1 &&
1040 if (MatchSelectAndCast(
LHS,
RHS))
1042 else if (MatchSelectAndCast(
RHS,
LHS))
1047 auto NewFoldedConst = [&](
bool IsTrueArm,
Value *V) {
1048 bool IsCastOpRHS = (CastOp ==
RHS);
1049 bool IsZExt = isa<ZExtInst>(CastOp);
1054 }
else if (IsZExt) {
1055 unsigned BitWidth = V->getType()->getScalarSizeInBits();
1068 Value *NewTrueVal = NewFoldedConst(
false, TrueVal);
1070 NewFoldedConst(
true, FalseVal));
1074 Value *NewTrueVal = NewFoldedConst(
true, TrueVal);
1076 NewFoldedConst(
false, FalseVal));
1097 if (Op0 && Op1 && LHSOpcode == RHSOpcode)
1217static std::optional<std::pair<Value *, Value *>>
1219 if (
LHS->getParent() !=
RHS->getParent())
1220 return std::nullopt;
1222 if (
LHS->getNumIncomingValues() < 2)
1223 return std::nullopt;
1226 return std::nullopt;
1228 Value *L0 =
LHS->getIncomingValue(0);
1229 Value *R0 =
RHS->getIncomingValue(0);
1231 for (
unsigned I = 1, E =
LHS->getNumIncomingValues();
I != E; ++
I) {
1235 if ((L0 == L1 && R0 == R1) || (L0 == R1 && R0 == L1))
1238 return std::nullopt;
1241 return std::optional(std::pair(L0, R0));
1244std::optional<std::pair<Value *, Value *>>
1245InstCombinerImpl::matchSymmetricPair(
Value *LHS,
Value *RHS) {
1246 Instruction *LHSInst = dyn_cast<Instruction>(LHS);
1247 Instruction *RHSInst = dyn_cast<Instruction>(RHS);
1249 return std::nullopt;
1251 case Instruction::PHI:
1253 case Instruction::Select: {
1259 return std::pair(TrueVal, FalseVal);
1260 return std::nullopt;
1262 case Instruction::Call: {
1266 if (LHSMinMax && RHSMinMax &&
1273 return std::pair(LHSMinMax->
getLHS(), LHSMinMax->
getRHS());
1274 return std::nullopt;
1277 return std::nullopt;
1287 if (!LHSIsSelect && !RHSIsSelect)
1292 if (isa<FPMathOperator>(&
I)) {
1293 FMF =
I.getFastMathFlags();
1300 Value *
Cond, *True =
nullptr, *False =
nullptr;
1308 if (Opcode != Instruction::Add || (!True && !False) || (True && False))
1323 if (LHSIsSelect && RHSIsSelect &&
A ==
D) {
1332 else if (True && !False)
1340 if (
Value *NewSel = foldAddNegate(
B,
C,
RHS))
1347 if (
Value *NewSel = foldAddNegate(E,
F,
LHS))
1351 if (!True || !False)
1362 assert(!isa<Constant>(
I) &&
"Shouldn't invert users of constant");
1364 if (U == IgnoredUser)
1366 switch (cast<Instruction>(U)->
getOpcode()) {
1367 case Instruction::Select: {
1368 auto *SI = cast<SelectInst>(U);
1370 SI->swapProfMetadata();
1373 case Instruction::Br: {
1380 case Instruction::Xor:
1387 "canFreelyInvertAllUsersOf() ?");
1394Value *InstCombinerImpl::dyn_castNegVal(
Value *V)
const {
1404 if (
C->getType()->getElementType()->isIntegerTy())
1408 for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1413 if (isa<UndefValue>(Elt))
1416 if (!isa<ConstantInt>(Elt))
1423 if (
auto *CV = dyn_cast<Constant>(V))
1424 if (CV->getType()->isVectorTy() &&
1425 CV->getType()->getScalarType()->isIntegerTy() && CV->getSplatValue())
1438Instruction *InstCombinerImpl::foldFBinOpOfIntCastsFromSign(
1439 BinaryOperator &BO,
bool OpsFromSigned, std::array<Value *, 2> IntOps,
1443 Type *IntTy = IntOps[0]->getType();
1448 unsigned MaxRepresentableBits =
1453 unsigned NumUsedLeadingBits[2] = {IntSz, IntSz};
1457 auto IsNonZero = [&](
unsigned OpNo) ->
bool {
1458 if (OpsKnown[OpNo].hasKnownBits() &&
1459 OpsKnown[OpNo].getKnownBits(
SQ).isNonZero())
1464 auto IsNonNeg = [&](
unsigned OpNo) ->
bool {
1468 return OpsKnown[OpNo].getKnownBits(
SQ).isNonNegative();
1472 auto IsValidPromotion = [&](
unsigned OpNo) ->
bool {
1474 if (OpsFromSigned != isa<SIToFPInst>(BO.
getOperand(OpNo)) &&
1483 if (MaxRepresentableBits < IntSz) {
1493 NumUsedLeadingBits[OpNo] =
1494 IntSz - OpsKnown[OpNo].getKnownBits(
SQ).countMinLeadingZeros();
1502 if (MaxRepresentableBits < NumUsedLeadingBits[OpNo])
1505 return !OpsFromSigned || BO.
getOpcode() != Instruction::FMul ||
1510 if (Op1FpC !=
nullptr) {
1512 if (OpsFromSigned && BO.
getOpcode() == Instruction::FMul &&
1517 OpsFromSigned ? Instruction::FPToSI : Instruction::FPToUI, Op1FpC,
1519 if (Op1IntC ==
nullptr)
1522 : Instruction::UIToFP,
1523 Op1IntC, FPTy,
DL) != Op1FpC)
1527 IntOps[1] = Op1IntC;
1531 if (IntTy != IntOps[1]->
getType())
1534 if (Op1FpC ==
nullptr) {
1535 if (!IsValidPromotion(1))
1538 if (!IsValidPromotion(0))
1544 bool NeedsOverflowCheck =
true;
1547 unsigned OverflowMaxOutputBits = OpsFromSigned ? 2 : 1;
1548 unsigned OverflowMaxCurBits =
1549 std::max(NumUsedLeadingBits[0], NumUsedLeadingBits[1]);
1550 bool OutputSigned = OpsFromSigned;
1552 case Instruction::FAdd:
1553 IntOpc = Instruction::Add;
1554 OverflowMaxOutputBits += OverflowMaxCurBits;
1556 case Instruction::FSub:
1557 IntOpc = Instruction::Sub;
1558 OverflowMaxOutputBits += OverflowMaxCurBits;
1560 case Instruction::FMul:
1561 IntOpc = Instruction::Mul;
1562 OverflowMaxOutputBits += OverflowMaxCurBits * 2;
1568 if (OverflowMaxOutputBits < IntSz) {
1569 NeedsOverflowCheck =
false;
1572 if (IntOpc == Instruction::Sub)
1573 OutputSigned =
true;
1579 if (NeedsOverflowCheck &&
1580 !willNotOverflow(IntOpc, IntOps[0], IntOps[1], BO, OutputSigned))
1584 if (
auto *IntBO = dyn_cast<BinaryOperator>(IntBinOp)) {
1585 IntBO->setHasNoSignedWrap(OutputSigned);
1586 IntBO->setHasNoUnsignedWrap(!OutputSigned);
1599 std::array<Value *, 2> IntOps = {
nullptr,
nullptr};
1619 if (
Instruction *R = foldFBinOpOfIntCastsFromSign(BO,
false,
1620 IntOps, Op1FpC, OpsKnown))
1622 return foldFBinOpOfIntCastsFromSign(BO,
true, IntOps,
1638 !
X->getType()->isIntOrIntVectorTy(1))
1656 C = dyn_cast<Constant>(IsTrueArm ? SI->getTrueValue()
1657 : SI->getFalseValue());
1658 }
else if (
match(SI->getCondition(),
1665 C = dyn_cast<Constant>(
Op);
1686 bool FoldWithMultiUse) {
1688 if (!SI->hasOneUse() && !FoldWithMultiUse)
1691 Value *TV = SI->getTrueValue();
1692 Value *FV = SI->getFalseValue();
1693 if (!(isa<Constant>(TV) || isa<Constant>(FV)))
1697 if (SI->getType()->isIntOrIntVectorTy(1))
1707 if (
auto *CI = dyn_cast<FCmpInst>(SI->getCondition())) {
1708 if (CI->hasOneUse()) {
1709 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1710 if ((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1))
1718 if (!NewTV && !NewFV)
1756 const ICmpInst *ICmp = dyn_cast<ICmpInst>(&
I);
1760 std::optional<bool> ImpliedCond =
1762 Ops[0], Ops[1],
DL, LHSIsTrue);
1772 if (NumPHIValues == 0)
1782 if (UI != &
I && !
I.isIdenticalTo(UI))
1793 Value *NonSimplifiedInVal =
nullptr;
1794 for (
unsigned i = 0; i != NumPHIValues; ++i) {
1803 if (NonSimplifiedBB)
return nullptr;
1805 NonSimplifiedBB = InBB;
1806 NonSimplifiedInVal = InVal;
1811 if (isa<InvokeInst>(InVal))
1812 if (cast<Instruction>(InVal)->
getParent() == NonSimplifiedBB)
1829 if (NonSimplifiedBB !=
nullptr) {
1845 if (NonSimplifiedBB) {
1849 U = NonSimplifiedInVal;
1851 U = U->DoPHITranslation(PN->
getParent(), NonSimplifiedBB);
1856 for (
unsigned i = 0; i != NumPHIValues; ++i) {
1857 if (NewPhiValues[i])
1865 if (
User == &
I)
continue;
1871 const_cast<PHINode &
>(*NewPN),
1880 auto *Phi0 = dyn_cast<PHINode>(BO.
getOperand(0));
1881 auto *Phi1 = dyn_cast<PHINode>(BO.
getOperand(1));
1882 if (!Phi0 || !Phi1 || !Phi0->hasOneUse() || !Phi1->hasOneUse() ||
1883 Phi0->getNumOperands() != Phi1->getNumOperands())
1887 if (BO.
getParent() != Phi0->getParent() ||
1904 auto CanFoldIncomingValuePair = [&](std::tuple<Use &, Use &>
T) {
1905 auto &Phi0Use = std::get<0>(
T);
1906 auto &Phi1Use = std::get<1>(
T);
1907 if (Phi0->getIncomingBlock(Phi0Use) != Phi1->getIncomingBlock(Phi1Use))
1909 Value *Phi0UseV = Phi0Use.get();
1910 Value *Phi1UseV = Phi1Use.get();
1913 else if (Phi1UseV ==
C)
1920 if (
all_of(
zip(Phi0->operands(), Phi1->operands()),
1921 CanFoldIncomingValuePair)) {
1924 assert(NewIncomingValues.
size() == Phi0->getNumOperands() &&
1925 "The number of collected incoming values should equal the number "
1926 "of the original PHINode operands!");
1927 for (
unsigned I = 0;
I < Phi0->getNumOperands();
I++)
1928 NewPhi->
addIncoming(NewIncomingValues[
I], Phi0->getIncomingBlock(
I));
1933 if (Phi0->getNumOperands() != 2 || Phi1->getNumOperands() != 2)
1940 ConstBB = Phi0->getIncomingBlock(0);
1941 OtherBB = Phi0->getIncomingBlock(1);
1943 ConstBB = Phi0->getIncomingBlock(1);
1944 OtherBB = Phi0->getIncomingBlock(0);
1954 auto *PredBlockBranch = dyn_cast<BranchInst>(OtherBB->
getTerminator());
1955 if (!PredBlockBranch || PredBlockBranch->isConditional() ||
1962 for (
auto BBIter = BO.
getParent()->begin(); &*BBIter != &BO; ++BBIter)
1975 Phi0->getIncomingValueForBlock(OtherBB),
1976 Phi1->getIncomingValueForBlock(OtherBB));
1977 if (
auto *NotFoldedNewBO = dyn_cast<BinaryOperator>(NewBO))
1978 NotFoldedNewBO->copyIRFlags(&BO);
1988 if (!isa<Constant>(
I.getOperand(1)))
1991 if (
auto *Sel = dyn_cast<SelectInst>(
I.getOperand(0))) {
1994 }
else if (
auto *PN = dyn_cast<PHINode>(
I.getOperand(0))) {
2005 if (
GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
2012 if (!isa<VectorType>(Inst.
getType()))
2018 cast<VectorType>(Inst.
getType())->getElementCount());
2020 cast<VectorType>(Inst.
getType())->getElementCount());
2025 Value *L0, *L1, *R0, *R1;
2030 cast<ShuffleVectorInst>(
LHS)->isConcat() &&
2031 cast<ShuffleVectorInst>(
RHS)->isConcat()) {
2038 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO0))
2041 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO1))
2048 if (
auto *BO = dyn_cast<BinaryOperator>(V))
2065 return createBinOpReverse(V1, V2);
2069 return createBinOpReverse(V1,
RHS);
2073 return createBinOpReverse(
LHS, V2);
2083 if (
auto *BO = dyn_cast<BinaryOperator>(XY))
2092 V1->
getType() == V2->getType() &&
2095 return createBinOpShuffle(V1, V2, Mask);
2104 auto *LShuf = cast<ShuffleVectorInst>(
LHS);
2105 auto *RShuf = cast<ShuffleVectorInst>(
RHS);
2110 if (LShuf->isSelect() &&
2112 RShuf->isSelect() &&
2130 auto *InstVTy = dyn_cast<FixedVectorType>(Inst.
getType());
2135 cast<FixedVectorType>(V1->
getType())->getNumElements() <=
2136 InstVTy->getNumElements()) {
2138 "Shuffle should not change scalar type");
2145 bool ConstOp1 = isa<Constant>(
RHS);
2147 unsigned SrcVecNumElts =
2148 cast<FixedVectorType>(V1->
getType())->getNumElements();
2151 bool MayChange =
true;
2152 unsigned NumElts = InstVTy->getNumElements();
2153 for (
unsigned I = 0;
I < NumElts; ++
I) {
2155 if (ShMask[
I] >= 0) {
2156 assert(ShMask[
I] < (
int)NumElts &&
"Not expecting narrowing shuffle");
2164 if (!CElt || (!isa<PoisonValue>(NewCElt) && NewCElt != CElt) ||
2165 I >= SrcVecNumElts) {
2169 NewVecC[ShMask[
I]] = CElt;
2180 if (
I >= SrcVecNumElts || ShMask[
I] < 0) {
2185 if (!MaybePoison || !isa<PoisonValue>(MaybePoison)) {
2202 Value *NewLHS = ConstOp1 ? V1 : NewC;
2203 Value *NewRHS = ConstOp1 ? NewC : V1;
2204 return createBinOpShuffle(NewLHS, NewRHS, Mask);
2211 if (isa<ShuffleVectorInst>(
RHS))
2244 if (isa<FPMathOperator>(R)) {
2245 R->copyFastMathFlags(&Inst);
2248 if (
auto *NewInstBO = dyn_cast<BinaryOperator>(NewBO))
2249 NewInstBO->copyIRFlags(R);
2278 cast<Operator>(Op1)->getOpcode() == CastOpc &&
2279 (Op0->
hasOneUse() || Op1->hasOneUse()))) {
2297 if (!willNotOverflow(BO.
getOpcode(),
X,
Y, BO, IsSext))
2303 if (
auto *NewBinOp = dyn_cast<BinaryOperator>(NarrowBO)) {
2305 NewBinOp->setHasNoSignedWrap();
2307 NewBinOp->setHasNoUnsignedWrap();
2320 if (!
GEP.hasAllConstantIndices())
2336 Type *Ty =
GEP.getSourceElementType();
2338 Value *NewFalseC = Builder.
CreateGEP(Ty, FalseC, IndexC,
"", NW);
2348 if (
GEP.getNumIndices() != 1)
2357 Type *PtrTy = Src->getType()->getScalarType();
2358 unsigned IndexSizeInBits =
DL.getIndexTypeSizeInBits(PtrTy);
2365 if (isa<ScalableVectorType>(
BaseType))
2369 if (NewOffset.
isZero() ||
2370 (Src->hasOneUse() &&
GEP.getOperand(1)->hasOneUse())) {
2391 Type *PtrTy = Src->getType()->getScalarType();
2392 if (
GEP.hasAllConstantIndices() &&
2393 (Src->hasOneUse() || Src->hasAllConstantIndices())) {
2397 bool IsFirstType =
true;
2398 unsigned NumVarIndices = 0;
2399 for (
auto Pair :
enumerate(Src->indices())) {
2400 if (!isa<ConstantInt>(Pair.value())) {
2402 IsFirstType =
false;
2403 NumVarIndices = Pair.index() + 1;
2410 if (NumVarIndices != Src->getNumIndices()) {
2431 if (!
Offset.isZero() || (!IsFirstType && !ConstIndices[0].isZero())) {
2434 if (Src->hasAllConstantIndices())
2446 Src->getNumIndices() - NumVarIndices));
2453 IsInBounds &=
Idx.isNonNegative() == ConstIndices[0].isNonNegative();
2458 Indices,
"", IsInBounds));
2461 if (Src->getResultElementType() !=
GEP.getSourceElementType())
2467 bool EndsWithSequential =
false;
2470 EndsWithSequential =
I.isSequential();
2473 if (EndsWithSequential) {
2476 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
2494 if (Src->getNumOperands() == 2) {
2500 Indices.
append(Src->op_begin()+1, Src->op_end()-1);
2503 }
else if (isa<Constant>(*
GEP.idx_begin()) &&
2504 cast<Constant>(*
GEP.idx_begin())->isNullValue() &&
2505 Src->getNumOperands() != 1) {
2507 Indices.
append(Src->op_begin()+1, Src->op_end());
2511 if (!Indices.
empty())
2514 Src->getSourceElementType(), Src->getOperand(0), Indices,
"",
2522 bool &DoesConsume,
unsigned Depth) {
2523 static Value *
const NonNull =
reinterpret_cast<Value *
>(uintptr_t(1));
2541 if (!WillInvertAllUses)
2546 if (
auto *
I = dyn_cast<CmpInst>(V)) {
2557 DoesConsume,
Depth))
2560 DoesConsume,
Depth))
2569 DoesConsume,
Depth))
2572 DoesConsume,
Depth))
2581 DoesConsume,
Depth))
2590 DoesConsume,
Depth))
2602 bool LocalDoesConsume = DoesConsume;
2604 LocalDoesConsume,
Depth))
2607 LocalDoesConsume,
Depth)) {
2608 DoesConsume = LocalDoesConsume;
2611 DoesConsume,
Depth);
2612 assert(NotB !=
nullptr &&
2613 "Unable to build inverted value for known freely invertable op");
2614 if (
auto *
II = dyn_cast<IntrinsicInst>(V))
2623 if (
PHINode *PN = dyn_cast<PHINode>(V)) {
2624 bool LocalDoesConsume = DoesConsume;
2626 for (
Use &U : PN->operands()) {
2627 BasicBlock *IncomingBlock = PN->getIncomingBlock(U);
2631 if (NewIncomingVal ==
nullptr)
2634 if (NewIncomingVal == V)
2637 IncomingValues.
emplace_back(NewIncomingVal, IncomingBlock);
2640 DoesConsume = LocalDoesConsume;
2646 for (
auto [Val, Pred] : IncomingValues)
2655 DoesConsume,
Depth))
2662 DoesConsume,
Depth))
2671 bool IsLogical,
Value *
A,
2673 bool LocalDoesConsume = DoesConsume;
2675 LocalDoesConsume,
Depth))
2678 LocalDoesConsume,
Depth)) {
2680 LocalDoesConsume,
Depth);
2681 DoesConsume = LocalDoesConsume;
2691 return TryInvertAndOrUsingDeMorgan(Instruction::And,
false,
A,
2695 return TryInvertAndOrUsingDeMorgan(Instruction::Or,
false,
A,
2699 return TryInvertAndOrUsingDeMorgan(Instruction::And,
true,
A,
2703 return TryInvertAndOrUsingDeMorgan(Instruction::Or,
true,
A,
2712 Type *GEPType =
GEP.getType();
2713 Type *GEPEltType =
GEP.getSourceElementType();
2722 if (
auto *GEPFVTy = dyn_cast<FixedVectorType>(GEPType)) {
2723 auto VWidth = GEPFVTy->getNumElements();
2724 APInt PoisonElts(VWidth, 0);
2740 bool MadeChange =
false;
2744 Type *NewScalarIndexTy =
2754 Type *IndexTy = (*I)->getType();
2755 Type *NewIndexType =
2758 cast<VectorType>(IndexTy)->getElementCount())
2770 if (IndexTy != NewIndexType) {
2782 if (!GEPEltType->
isIntegerTy(8) &&
GEP.hasAllConstantIndices()) {
2787 GEP.getNoWrapFlags()));
2806 if (
auto *PN = dyn_cast<PHINode>(PtrOp)) {
2807 auto *Op1 = dyn_cast<GetElementPtrInst>(PN->getOperand(0));
2822 for (
auto I = PN->op_begin()+1, E = PN->op_end();
I !=E; ++
I) {
2823 auto *Op2 = dyn_cast<GetElementPtrInst>(*
I);
2824 if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands() ||
2825 Op1->getSourceElementType() != Op2->getSourceElementType())
2833 Type *CurTy =
nullptr;
2835 for (
unsigned J = 0,
F = Op1->getNumOperands(); J !=
F; ++J) {
2836 if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
2839 if (Op1->getOperand(J) != Op2->getOperand(J)) {
2848 assert(CurTy &&
"No current type?");
2868 CurTy = Op1->getSourceElementType();
2880 if (DI != -1 && !PN->hasOneUse())
2883 auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
2896 PN->getNumOperands());
2899 for (
auto &
I : PN->operands())
2900 NewPN->
addIncoming(cast<GEPOperator>(
I)->getOperand(DI),
2901 PN->getIncomingBlock(
I));
2903 NewGEP->setOperand(DI, NewPN);
2906 NewGEP->insertBefore(*
GEP.getParent(),
GEP.getParent()->getFirstInsertionPt());
2910 if (
auto *Src = dyn_cast<GEPOperator>(PtrOp))
2914 if (
GEP.getNumIndices() == 1) {
2915 unsigned AS =
GEP.getPointerAddressSpace();
2916 if (
GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
2920 if (TyAllocSize == 1) {
2929 GEPType ==
Y->getType()) {
2930 bool HasSameUnderlyingObject =
2932 bool Changed =
false;
2933 GEP.replaceUsesWithIf(
Y, [&](
Use &U) {
2934 bool ShouldReplace = HasSameUnderlyingObject ||
2935 isa<ICmpInst>(U.getUser()) ||
2936 isa<PtrToIntInst>(U.getUser());
2937 Changed |= ShouldReplace;
2938 return ShouldReplace;
2940 return Changed ? &
GEP :
nullptr;
2942 }
else if (
auto *ExactIns =
2943 dyn_cast<PossiblyExactOperator>(
GEP.getOperand(1))) {
2946 if (ExactIns->isExact()) {
2954 GEP.getPointerOperand(), V,
2955 GEP.getNoWrapFlags());
2958 if (ExactIns->isExact() && ExactIns->hasOneUse()) {
2964 std::optional<APInt> NewC;
2984 if (NewC.has_value()) {
2987 ConstantInt::get(V->getType(), *NewC));
2988 cast<BinaryOperator>(NewOp)->setIsExact();
2990 GEP.getPointerOperand(), NewOp,
2991 GEP.getNoWrapFlags());
3001 if (
GEP.getNumIndices() == 1) {
3004 auto CanPreserveInBounds = [&](
bool AddIsNSW,
Value *Idx1,
Value *Idx2) {
3019 bool IsInBounds = CanPreserveInBounds(
3020 cast<OverflowingBinaryOperator>(
GEP.getOperand(1))->hasNoSignedWrap(),
3024 Idx1,
"", IsInBounds);
3038 bool IsInBounds = CanPreserveInBounds(
3041 GEP.getSourceElementType(),
GEP.getPointerOperand(),
3052 if (!
GEP.isInBounds()) {
3055 APInt BasePtrOffset(IdxWidth, 0);
3056 Value *UnderlyingPtrOp =
3059 bool CanBeNull, CanBeFreed;
3061 DL, CanBeNull, CanBeFreed);
3062 if (!CanBeNull && !CanBeFreed && DerefBytes != 0) {
3063 if (
GEP.accumulateConstantOffset(
DL, BasePtrOffset) &&
3065 APInt AllocSize(IdxWidth, DerefBytes);
3066 if (BasePtrOffset.
ule(AllocSize)) {
3068 GEP.getSourceElementType(), PtrOp, Indices,
GEP.getName());
3082 if (isa<ConstantPointerNull>(V))
3084 if (
auto *LI = dyn_cast<LoadInst>(V))
3085 return isa<GlobalVariable>(LI->getPointerOperand());
3109 return Dest && Dest->Ptr == UsedV;
3123 switch (
I->getOpcode()) {
3128 case Instruction::AddrSpaceCast:
3129 case Instruction::BitCast:
3130 case Instruction::GetElementPtr:
3135 case Instruction::ICmp: {
3142 unsigned OtherIndex = (ICI->
getOperand(0) == PI) ? 1 : 0;
3149 auto AlignmentAndSizeKnownValid = [](
CallBase *CB) {
3153 const APInt *Alignment;
3155 return match(CB->getArgOperand(0),
m_APInt(Alignment)) &&
3159 auto *CB = dyn_cast<CallBase>(AI);
3161 if (CB && TLI.
getLibFunc(*CB->getCalledFunction(), TheLibFunc) &&
3162 TLI.
has(TheLibFunc) && TheLibFunc == LibFunc_aligned_alloc &&
3163 !AlignmentAndSizeKnownValid(CB))
3169 case Instruction::Call:
3172 switch (
II->getIntrinsicID()) {
3176 case Intrinsic::memmove:
3177 case Intrinsic::memcpy:
3178 case Intrinsic::memset: {
3180 if (
MI->isVolatile() ||
MI->getRawDest() != PI)
3184 case Intrinsic::assume:
3185 case Intrinsic::invariant_start:
3186 case Intrinsic::invariant_end:
3187 case Intrinsic::lifetime_start:
3188 case Intrinsic::lifetime_end:
3189 case Intrinsic::objectsize:
3192 case Intrinsic::launder_invariant_group:
3193 case Intrinsic::strip_invariant_group:
3222 case Instruction::Store: {
3224 if (SI->isVolatile() || SI->getPointerOperand() != PI)
3232 }
while (!Worklist.
empty());
3255 std::unique_ptr<DIBuilder> DIB;
3256 if (isa<AllocaInst>(
MI)) {
3262 for (
unsigned i = 0, e =
Users.size(); i != e; ++i) {
3271 if (
II->getIntrinsicID() == Intrinsic::objectsize) {
3274 II,
DL, &
TLI,
AA,
true, &InsertedInstructions);
3275 for (
Instruction *Inserted : InsertedInstructions)
3283 for (
unsigned i = 0, e =
Users.size(); i != e; ++i) {
3292 C->isFalseWhenEqual()));
3293 }
else if (
auto *SI = dyn_cast<StoreInst>(
I)) {
3294 for (
auto *DVI : DVIs)
3295 if (DVI->isAddressOfVariable())
3297 for (
auto *DVR : DVRs)
3298 if (DVR->isAddressOfVariable())
3313 std::nullopt,
"",
II->getParent());
3341 for (
auto *DVI : DVIs)
3342 if (DVI->isAddressOfVariable() || DVI->getExpression()->startsWithDeref())
3343 DVI->eraseFromParent();
3344 for (
auto *DVR : DVRs)
3345 if (DVR->isAddressOfVariable() || DVR->getExpression()->startsWithDeref())
3346 DVR->eraseFromParent();
3392 if (FreeInstrBB->
size() != 2) {
3394 if (&Inst == &FI || &Inst == FreeInstrBBTerminator)
3396 auto *Cast = dyn_cast<CastInst>(&Inst);
3397 if (!Cast || !Cast->isNoopCast(
DL))
3418 "Broken CFG: missing edge from predecessor to successor");
3423 if (&Instr == FreeInstrBBTerminator)
3425 Instr.moveBeforePreserving(TI);
3428 "Only the branch instruction should remain");
3439 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0, Attribute::NonNull);
3440 Attribute Dereferenceable = Attrs.getParamAttr(0, Attribute::Dereferenceable);
3441 if (Dereferenceable.
isValid()) {
3443 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0,
3444 Attribute::Dereferenceable);
3445 Attrs = Attrs.addDereferenceableOrNullParamAttr(FI.
getContext(), 0, Bytes);
3454 if (isa<UndefValue>(
Op)) {
3462 if (isa<ConstantPointerNull>(
Op))
3498 FPClassTest ReturnClass =
F->getAttributes().getRetNoFPClass();
3499 if (ReturnClass ==
fcNone)
3516 bool Changed =
false;
3517 while (
Instruction *Prev =
I.getPrevNonDebugInstruction()) {
3522 if (Prev->isEHPad())
3553 return BBI->isDebugOrPseudoInst() ||
3554 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy());
3559 if (BBI != FirstInstr)
3561 }
while (BBI != FirstInstr && IsNoopInstrForStoreMerging(BBI));
3563 return dyn_cast<StoreInst>(BBI);
3575 if (!
DeadEdges.insert({From, To}).second)
3580 for (
Use &U : PN.incoming_values())
3581 if (PN.getIncomingBlock(U) ==
From && !isa<PoisonValue>(U)) {
3597 std::next(
I->getReverseIterator())))) {
3598 if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
3602 if (Inst.isEHPad() || Inst.getType()->isTokenTy())
3605 Inst.dropDbgRecords();
3613 for (
Value *V : Changed)
3640 if (Succ == LiveSucc)
3668 if (isa<SelectInst>(
Cond) &&
3689 auto *Cmp = cast<CmpInst>(
Cond);
3698 if (isa<UndefValue>(
Cond)) {
3702 if (
auto *CI = dyn_cast<ConstantInt>(
Cond)) {
3718 unsigned CstOpIdx = IsTrueArm ? 1 : 2;
3719 auto *
C = dyn_cast<ConstantInt>(
Select->getOperand(CstOpIdx));
3723 BasicBlock *CstBB = SI.findCaseValue(
C)->getCaseSuccessor();
3724 if (CstBB != SI.getDefaultDest())
3737 for (
auto Case : SI.cases())
3738 if (!CR.
contains(Case.getCaseValue()->getValue()))
3750 for (
auto Case : SI.cases()) {
3752 assert(isa<ConstantInt>(NewCase) &&
3753 "Result of expression should be constant");
3754 Case.setValue(cast<ConstantInt>(NewCase));
3762 for (
auto Case : SI.cases()) {
3764 assert(isa<ConstantInt>(NewCase) &&
3765 "Result of expression should be constant");
3766 Case.setValue(cast<ConstantInt>(NewCase));
3774 all_of(SI.cases(), [&](
const auto &Case) {
3775 return Case.getCaseValue()->getValue().countr_zero() >= ShiftAmt;
3781 Value *NewCond = Op0;
3788 for (
auto Case : SI.cases()) {
3789 const APInt &CaseVal = Case.getCaseValue()->getValue();
3791 : CaseVal.
lshr(ShiftAmt);
3792 Case.setValue(ConstantInt::get(SI.getContext(), ShiftedCase));
3800 bool IsZExt = isa<ZExtInst>(
Cond);
3804 if (
all_of(SI.cases(), [&](
const auto &Case) {
3805 const APInt &CaseVal = Case.getCaseValue()->getValue();
3806 return IsZExt ? CaseVal.isIntN(NewWidth)
3807 : CaseVal.isSignedIntN(NewWidth);
3809 for (
auto &Case : SI.cases()) {
3810 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
3811 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
3818 if (
auto *
Select = dyn_cast<SelectInst>(
Cond)) {
3833 for (
const auto &
C : SI.cases()) {
3835 std::min(LeadingKnownZeros,
C.getCaseValue()->getValue().countl_zero());
3837 std::min(LeadingKnownOnes,
C.getCaseValue()->getValue().countl_one());
3840 unsigned NewWidth = Known.
getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes);
3846 if (NewWidth > 0 && NewWidth < Known.
getBitWidth() &&
3847 shouldChangeType(Known.
getBitWidth(), NewWidth)) {
3852 for (
auto Case : SI.cases()) {
3853 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
3854 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
3859 if (isa<UndefValue>(
Cond)) {
3863 if (
auto *CI = dyn_cast<ConstantInt>(
Cond)) {
3865 SI.findCaseValue(CI)->getCaseSuccessor());
3879 const APInt *
C =
nullptr;
3881 if (*EV.
idx_begin() == 0 && (OvID == Intrinsic::smul_with_overflow ||
3882 OvID == Intrinsic::umul_with_overflow)) {
3887 if (
C->isPowerOf2()) {
3888 return BinaryOperator::CreateShl(
3890 ConstantInt::get(WO->getLHS()->getType(),
C->logBase2()));
3898 if (!WO->hasOneUse())
3912 assert(*EV.
idx_begin() == 1 &&
"Unexpected extract index for overflow inst");
3915 if (OvID == Intrinsic::usub_with_overflow)
3920 if (OvID == Intrinsic::smul_with_overflow &&
3921 WO->getLHS()->getType()->isIntOrIntVectorTy(1))
3922 return BinaryOperator::CreateAnd(WO->getLHS(), WO->getRHS());
3925 if (OvID == Intrinsic::umul_with_overflow && WO->getLHS() == WO->getRHS()) {
3926 unsigned BitWidth = WO->getLHS()->getType()->getScalarSizeInBits();
3931 ConstantInt::get(WO->getLHS()->getType(),
3942 WO->getBinaryOp(), *
C, WO->getNoWrapKind());
3947 auto *OpTy = WO->getRHS()->getType();
3948 auto *NewLHS = WO->getLHS();
3952 ConstantInt::get(OpTy, NewRHSC));
3970 const unsigned *exti, *exte, *insi, *inse;
3971 for (exti = EV.
idx_begin(), insi =
IV->idx_begin(),
3972 exte = EV.
idx_end(), inse =
IV->idx_end();
3973 exti != exte && insi != inse;
3987 if (exti == exte && insi == inse)
4020 if (
Instruction *R = foldExtractOfOverflowIntrinsic(EV))
4023 if (
LoadInst *L = dyn_cast<LoadInst>(Agg)) {
4025 if (
auto *STy = dyn_cast<StructType>(Agg->
getType());
4026 STy && STy->containsScalableVectorType())
4034 if (L->isSimple() && L->hasOneUse()) {
4046 L->getPointerOperand(), Indices);
4050 NL->setAAMetadata(L->getAAMetadata());
4057 if (
auto *PN = dyn_cast<PHINode>(Agg))
4063 if (
auto *SI = dyn_cast<SelectInst>(Agg))
4080 switch (Personality) {
4110 cast<ArrayType>(
LHS->
getType())->getNumElements()
4112 cast<ArrayType>(
RHS->
getType())->getNumElements();
4124 bool MakeNewInstruction =
false;
4126 bool CleanupFlag =
LI.isCleanup();
4129 for (
unsigned i = 0, e =
LI.getNumClauses(); i != e; ++i) {
4130 bool isLastClause = i + 1 == e;
4131 if (
LI.isCatch(i)) {
4138 if (AlreadyCaught.
insert(TypeInfo).second) {
4143 MakeNewInstruction =
true;
4150 MakeNewInstruction =
true;
4151 CleanupFlag =
false;
4162 assert(
LI.isFilter(i) &&
"Unsupported landingpad clause!");
4170 if (!NumTypeInfos) {
4173 MakeNewInstruction =
true;
4174 CleanupFlag =
false;
4178 bool MakeNewFilter =
false;
4180 if (isa<ConstantAggregateZero>(FilterClause)) {
4182 assert(NumTypeInfos > 0 &&
"Should have handled empty filter already!");
4188 MakeNewInstruction =
true;
4195 if (NumTypeInfos > 1)
4196 MakeNewFilter =
true;
4200 NewFilterElts.
reserve(NumTypeInfos);
4205 bool SawCatchAll =
false;
4206 for (
unsigned j = 0; j != NumTypeInfos; ++j) {
4234 if (SeenInFilter.
insert(TypeInfo).second)
4235 NewFilterElts.
push_back(cast<Constant>(Elt));
4240 MakeNewInstruction =
true;
4245 if (NewFilterElts.
size() < NumTypeInfos)
4246 MakeNewFilter =
true;
4248 if (MakeNewFilter) {
4250 NewFilterElts.
size());
4252 MakeNewInstruction =
true;
4261 if (MakeNewFilter && !NewFilterElts.
size()) {
4262 assert(MakeNewInstruction &&
"New filter but not a new instruction!");
4263 CleanupFlag =
false;
4274 for (
unsigned i = 0, e = NewClauses.
size(); i + 1 < e; ) {
4277 for (j = i; j != e; ++j)
4278 if (!isa<ArrayType>(NewClauses[j]->
getType()))
4284 for (
unsigned k = i; k + 1 < j; ++k)
4288 std::stable_sort(NewClauses.
begin() + i, NewClauses.
begin() + j,
4290 MakeNewInstruction =
true;
4309 for (
unsigned i = 0; i + 1 < NewClauses.
size(); ++i) {
4319 for (
unsigned j = NewClauses.
size() - 1; j != i; --j) {
4320 Value *LFilter = NewClauses[j];
4331 NewClauses.
erase(J);
4332 MakeNewInstruction =
true;
4342 if (isa<ConstantAggregateZero>(LFilter)) {
4345 if (isa<ConstantAggregateZero>(
Filter)) {
4346 assert(FElts <= LElts &&
"Should have handled this case earlier!");
4348 NewClauses.
erase(J);
4349 MakeNewInstruction =
true;
4355 if (isa<ConstantAggregateZero>(
Filter)) {
4358 assert(FElts > 0 &&
"Should have eliminated the empty filter earlier!");
4359 for (
unsigned l = 0; l != LElts; ++l)
4362 NewClauses.
erase(J);
4363 MakeNewInstruction =
true;
4374 bool AllFound =
true;
4375 for (
unsigned f = 0; f != FElts; ++f) {
4378 for (
unsigned l = 0; l != LElts; ++l) {
4380 if (LTypeInfo == FTypeInfo) {
4390 NewClauses.
erase(J);
4391 MakeNewInstruction =
true;
4399 if (MakeNewInstruction) {
4407 if (NewClauses.empty())
4415 if (
LI.isCleanup() != CleanupFlag) {
4416 assert(!CleanupFlag &&
"Adding a cleanup, not removing one?!");
4417 LI.setCleanup(CleanupFlag);
4441 auto *OrigOpInst = dyn_cast<Instruction>(OrigOp);
4446 if (!OrigOpInst || !OrigOpInst->hasOneUse() || isa<PHINode>(OrigOp))
4460 Use *MaybePoisonOperand =
nullptr;
4461 for (
Use &U : OrigOpInst->operands()) {
4462 if (isa<MetadataAsValue>(U.get()) ||
4465 if (!MaybePoisonOperand)
4466 MaybePoisonOperand = &U;
4471 OrigOpInst->dropPoisonGeneratingAnnotations();
4474 if (!MaybePoisonOperand)
4479 MaybePoisonOperand->get(), MaybePoisonOperand->get()->
getName() +
".fr");
4481 replaceUse(*MaybePoisonOperand, FrozenMaybePoisonOperand);
4492 Use *StartU =
nullptr;
4510 Value *StartV = StartU->get();
4522 if (!Visited.
insert(V).second)
4525 if (Visited.
size() > 32)
4542 I->dropPoisonGeneratingAnnotations();
4544 if (StartNeedsFreeze) {
4556 if (isa<Constant>(
Op) ||
Op->hasOneUse())
4565 if (isa<Argument>(
Op)) {
4569 auto MoveBeforeOpt = cast<Instruction>(
Op)->getInsertionPointAfterDef();
4572 MoveBefore = *MoveBeforeOpt;
4576 if (isa<DbgInfoIntrinsic>(MoveBefore))
4577 MoveBefore = MoveBefore->getNextNonDebugInstruction()->getIterator();
4580 MoveBefore.setHeadBit(
false);
4582 bool Changed =
false;
4583 if (&FI != &*MoveBefore) {
4584 FI.
moveBefore(*MoveBefore->getParent(), MoveBefore);
4588 Op->replaceUsesWithIf(&FI, [&](
Use &U) ->
bool {
4590 Changed |= Dominates;
4599 for (
auto *U : V->users()) {
4600 if (isa<ShuffleVectorInst>(U))