48#define DEBUG_TYPE "instsimplify"
81 if (
auto *BO = dyn_cast<BinaryOperator>(
Cond))
82 BinOpCode = BO->getOpcode();
87 if (BinOpCode == BinaryOperator::Or) {
88 ExpectedPred = ICmpInst::ICMP_NE;
89 }
else if (BinOpCode == BinaryOperator::And) {
90 ExpectedPred = ICmpInst::ICMP_EQ;
111 Pred1 != Pred2 || Pred1 != ExpectedPred)
114 if (
X == TrueVal ||
X == FalseVal ||
Y == TrueVal ||
Y == FalseVal)
115 return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal;
131 CmpInst *Cmp = dyn_cast<CmpInst>(V);
135 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
136 if (CPred == Pred && CLHS ==
LHS && CRHS ==
RHS)
152 if (SimplifiedCmp ==
Cond) {
160 return SimplifiedCmp;
167 unsigned MaxRecurse) {
176 unsigned MaxRecurse) {
186 unsigned MaxRecurse) {
220 if (!
I->getParent() || !
P->getParent() || !
I->getFunction())
229 if (
I->getParent()->isEntryBlock() && !isa<InvokeInst>(
I) &&
242 auto *
B = dyn_cast<BinaryOperator>(V);
243 if (!
B ||
B->getOpcode() != OpcodeToExpand)
245 Value *B0 =
B->getOperand(0), *B1 =
B->getOperand(1);
256 if ((L == B0 && R == B1) ||
277 unsigned MaxRecurse) {
294 unsigned MaxRecurse) {
397 unsigned MaxRecurse) {
403 if (isa<SelectInst>(
LHS)) {
404 SI = cast<SelectInst>(
LHS);
406 assert(isa<SelectInst>(
RHS) &&
"No select instruction operand!");
407 SI = cast<SelectInst>(
RHS);
434 if (TV ==
SI->getTrueValue() && FV ==
SI->getFalseValue())
440 if ((FV && !TV) || (TV && !FV)) {
443 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
444 if (Simplified && Simplified->getOpcode() ==
unsigned(Opcode)) {
448 Value *UnsimplifiedBranch = FV ?
SI->getTrueValue() :
SI->getFalseValue();
449 Value *UnsimplifiedLHS =
SI ==
LHS ? UnsimplifiedBranch :
LHS;
450 Value *UnsimplifiedRHS =
SI ==
LHS ?
RHS : UnsimplifiedBranch;
451 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
452 Simplified->getOperand(1) == UnsimplifiedRHS)
454 if (Simplified->isCommutative() &&
455 Simplified->getOperand(1) == UnsimplifiedLHS &&
456 Simplified->getOperand(0) == UnsimplifiedRHS)
475 unsigned MaxRecurse) {
481 if (!isa<SelectInst>(
LHS)) {
485 assert(isa<SelectInst>(
LHS) &&
"Not comparing with a select instruction!");
488 Value *TV =
SI->getTrueValue();
489 Value *FV =
SI->getFalseValue();
521 unsigned MaxRecurse) {
527 if (isa<PHINode>(
LHS)) {
528 PI = cast<PHINode>(
LHS);
533 assert(isa<PHINode>(
RHS) &&
"No PHI instruction operand!");
534 PI = cast<PHINode>(
RHS);
541 Value *CommonValue =
nullptr;
554 if (!V || (CommonValue && V != CommonValue))
573 if (!isa<PHINode>(
LHS)) {
577 assert(isa<PHINode>(
LHS) &&
"Not comparing with a phi instruction!");
585 Value *CommonValue =
nullptr;
599 if (!V || (CommonValue && V != CommonValue))
610 if (
auto *CLHS = dyn_cast<Constant>(Op0)) {
611 if (
auto *CRHS = dyn_cast<Constant>(Op1)) {
615 case Instruction::FAdd:
616 case Instruction::FSub:
617 case Instruction::FMul:
618 case Instruction::FDiv:
619 case Instruction::FRem:
620 if (Q.
CxtI !=
nullptr)
641 if (isa<PoisonValue>(Op1))
704 return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query,
RecursionLimit);
717 bool AllowNonInbounds =
false) {
718 assert(V->getType()->isPtrOrPtrVectorTy());
721 V = V->stripAndAccumulateConstantOffsets(
DL,
Offset, AllowNonInbounds);
724 return Offset.sextOrTrunc(
DL.getIndexTypeSizeInBits(V->getType()));
744 if (
auto *VecTy = dyn_cast<VectorType>(
LHS->
getType()))
759 std::optional<bool> Imp =
764 case Instruction::Sub:
765 case Instruction::Xor:
766 case Instruction::URem:
767 case Instruction::SRem:
770 case Instruction::SDiv:
771 case Instruction::UDiv:
774 case Instruction::And:
775 case Instruction::Or:
794 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
830 Value *
X =
nullptr, *
Y =
nullptr, *Z = Op1;
888 if (
X->getType() ==
Y->getType())
924 return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
935 if (isa<PoisonValue>(Op1))
959 return ConstantInt::getNullValue(Op0->
getType());
974 Instruction::Add, Q, MaxRecurse))
979 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
986 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
996 return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
1003 unsigned MaxRecurse) {
1004 bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
1005 bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
1022 auto *Op1C = dyn_cast<Constant>(Op1);
1023 auto *VTy = dyn_cast<FixedVectorType>(Ty);
1025 unsigned NumElts = VTy->getNumElements();
1026 for (
unsigned i = 0; i != NumElts; ++i) {
1035 if (isa<PoisonValue>(Op0))
1067 auto *
Mul = cast<OverflowingBinaryOperator>(Op0);
1090 Constant *
C = dyn_cast_or_null<Constant>(V);
1091 return (
C &&
C->isAllOnesValue());
1097 unsigned MaxRecurse,
bool IsSigned) {
1110 Type *Ty =
X->getType();
1126 if (
C->isMinSignedValue())
1153 return isICmpTrue(ICmpInst::ICMP_ULT,
X,
Y, Q, MaxRecurse);
1159 unsigned MaxRecurse) {
1176 bool IsSigned = Opcode == Instruction::SDiv;
1195 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1201 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1205 if (
isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1221 if ((Opcode == Instruction::SRem &&
1223 (Opcode == Instruction::URem &&
1229 ((Opcode == Instruction::SRem &&
1231 (Opcode == Instruction::URem &&
1237 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1243 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1248 if (
isDivZero(Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem))
1262 return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1274 return simplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1285 unsigned MaxRecurse) {
1290 return ConstantInt::getNullValue(Op0->
getType());
1294 return ConstantInt::getNullValue(Op0->
getType());
1296 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1306 unsigned MaxRecurse) {
1307 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1316 Constant *
C = dyn_cast<Constant>(Amount);
1326 const APInt *AmountC;
1332 if (isa<ConstantVector>(
C) || isa<ConstantDataVector>(
C)) {
1333 for (
unsigned I = 0,
1334 E = cast<FixedVectorType>(
C->getType())->getNumElements();
1348 unsigned MaxRecurse) {
1353 if (isa<PoisonValue>(Op0))
1374 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1380 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1398 assert(Opcode == Instruction::Shl &&
"Expected shl for nsw instruction");
1417 Value *Op1,
bool IsExact,
1437 if (Op0Known.
One[0])
1449 simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
1473 if (IsNSW && IsNUW &&
1482 return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
1504 const APInt *ShRAmt, *ShLAmt;
1507 *ShRAmt == *ShLAmt) {
1510 if (ShRAmt->
uge(EffWidthY))
1558 ICmpInst *UnsignedICmp,
bool IsAnd,
1572 if (
match(UnsignedICmp,
1574 ICmpInst::isUnsigned(UnsignedPred)) {
1576 if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1577 UnsignedPred == ICmpInst::ICMP_ULE) &&
1578 EqPred == ICmpInst::ICMP_NE && !IsAnd)
1581 if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1582 UnsignedPred == ICmpInst::ICMP_UGT) &&
1583 EqPred == ICmpInst::ICMP_EQ && IsAnd)
1588 if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1589 UnsignedPred == ICmpInst::ICMP_UGT))
1590 return IsAnd ? UnsignedICmp : ZeroICmp;
1594 if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1595 UnsignedPred == ICmpInst::ICMP_UGE))
1596 return IsAnd ? ZeroICmp : UnsignedICmp;
1602 if (
match(UnsignedICmp,
1604 if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1605 EqPred == ICmpInst::ICMP_NE &&
1607 return UnsignedICmp;
1608 if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1609 EqPred == ICmpInst::ICMP_EQ &&
1611 return UnsignedICmp;
1616 ICmpInst::isUnsigned(UnsignedPred))
1618 else if (
match(UnsignedICmp,
1620 ICmpInst::isUnsigned(UnsignedPred))
1621 UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1627 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1629 return IsAnd ? ZeroICmp : UnsignedICmp;
1633 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1635 return IsAnd ? UnsignedICmp : ZeroICmp;
1644 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1645 return IsAnd ? UnsignedICmp : ZeroICmp;
1649 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1650 return IsAnd ? ZeroICmp : UnsignedICmp;
1653 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1658 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1674 const APInt *C0, *C1;
1684 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1689 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1697 if (Range0.contains(Range1))
1698 return IsAnd ? Cmp1 : Cmp0;
1699 if (Range1.contains(Range0))
1700 return IsAnd ? Cmp0 : Cmp1;
1712 if ((IsAnd && P0 != ICmpInst::ICMP_NE) || (!IsAnd && P1 != ICmpInst::ICMP_EQ))
1746 const APInt *C0, *C1;
1754 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->
getOperand(0));
1755 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1762 const APInt Delta = *C1 - *C0;
1765 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1767 if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1771 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1773 if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1779 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1782 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1815 MinMaxC = HasNotOp ? ~*
C : *
C;
1816 else if (isa<ConstantPointerNull>(Cmp0->
getOperand(1)))
1823 Pred0 = ICmpInst::getInversePredicate(Pred0);
1824 Pred1 = ICmpInst::getInversePredicate(Pred1);
1829 if (ICmpInst::isSigned(Pred1)) {
1837 if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT)
1843 if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_UGT)
1861 if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1864 if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1903 const APInt *C0, *C1;
1911 auto *AddInst = cast<BinaryOperator>(Op0->
getOperand(0));
1912 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1919 const APInt Delta = *C1 - *C0;
1922 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1924 if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1928 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1930 if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1936 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1939 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1977 Value *LHS0 =
LHS->getOperand(0), *LHS1 =
LHS->getOperand(1);
1978 Value *RHS0 =
RHS->getOperand(0), *RHS1 =
RHS->getOperand(1);
1983 if ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) ||
1984 (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO && !IsAnd)) {
2014 Value *Op1,
bool IsAnd) {
2016 auto *Cast0 = dyn_cast<CastInst>(Op0);
2017 auto *Cast1 = dyn_cast<CastInst>(Op1);
2018 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
2019 Cast0->getSrcTy() == Cast1->getSrcTy()) {
2020 Op0 = Cast0->getOperand(0);
2021 Op1 = Cast1->getOperand(0);
2025 auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
2026 auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
2031 auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
2032 auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
2043 if (
auto *
C = dyn_cast<Constant>(V))
2054 assert(BinaryOperator::isBitwiseLogicOp(Opcode) &&
"Expected logic op");
2066 return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
2067 : ConstantInt::getAllOnesValue(Ty);
2076 unsigned MaxRecurse) {
2081 if (isa<PoisonValue>(Op1))
2131 (~(*Mask)).lshr(*ShAmt).isZero())
2137 (~(*Mask)).shl(*ShAmt).isZero())
2180 Instruction::Or, Q, MaxRecurse))
2185 Instruction::Xor, Q, MaxRecurse))
2188 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2206 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2230 if (EffWidthY <= ShftCnt) {
2256 if (*Implied ==
true)
2259 if (*Implied ==
false)
2284 assert(
X->getType() ==
Y->getType() &&
"Expected same type for 'or' ops");
2285 Type *Ty =
X->getType();
2289 return ConstantInt::getAllOnesValue(Ty);
2293 return ConstantInt::getAllOnesValue(Ty);
2311 return ConstantInt::getAllOnesValue(Ty);
2335 return ConstantInt::getAllOnesValue(Ty);
2377 unsigned MaxRecurse) {
2382 if (isa<PoisonValue>(Op1))
2416 C->ule(
X->getType()->getScalarSizeInBits())) {
2417 return ConstantInt::getAllOnesValue(
X->getType());
2464 Instruction::And, Q, MaxRecurse))
2467 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2485 const APInt *C1, *C2;
2511 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2516 if (std::optional<bool> Implied =
2519 if (*Implied ==
false)
2522 if (*Implied ==
true)
2525 if (std::optional<bool> Implied =
2528 if (*Implied ==
false)
2531 if (*Implied ==
true)
2549 unsigned MaxRecurse) {
2554 if (isa<PoisonValue>(Op1))
2592 if (
Value *R = foldAndOrNot(Op0, Op1))
2594 if (
Value *R = foldAndOrNot(Op1, Op0))
2636 CmpInst *Cmp = dyn_cast<CmpInst>(
SI->getCondition());
2639 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2640 if (Pred == Cmp->getPredicate() &&
LHS == CmpLHS &&
RHS == CmpRHS)
2643 LHS == CmpRHS &&
RHS == CmpLHS)
2656 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2657 return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
2658 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2659 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2660 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2661 !GV->isThreadLocal();
2662 if (
const Argument *
A = dyn_cast<Argument>(V))
2663 return A->hasByValAttr();
2696 auto isByValArg = [](
const Value *V) {
2697 const Argument *
A = dyn_cast<Argument>(V);
2698 return A &&
A->hasByValAttr();
2704 return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2706 return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2708 return isa<AllocaInst>(V1) &&
2709 (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
2809 Opts.
EvalMode = ObjectSizeOpts::Mode::Min;
2811 if (
auto *
I = dyn_cast<Instruction>(V))
2812 return I->getFunction();
2813 if (
auto *
A = dyn_cast<Argument>(V))
2814 return A->getParent();
2821 LHSOffset.
ult(LHSSize) && RHSOffset.
ult(RHSSize)) {
2848 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2849 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2871 bool Captured =
false;
2874 if (
auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2878 unsigned OtherIdx = 1 - U->getOperandNo();
2879 auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2880 if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2888 CustomCaptureTracker Tracker;
2890 if (!Tracker.Captured)
2912 auto ExtractNotLHS = [](
Value *V) ->
Value * {
2974 case ICmpInst::ICMP_UGE:
2978 case ICmpInst::ICMP_SGE:
2989 case ICmpInst::ICMP_ULE:
2993 case ICmpInst::ICMP_SLE:
3013 case ICmpInst::ICMP_ULT:
3015 case ICmpInst::ICMP_UGE:
3017 case ICmpInst::ICMP_EQ:
3018 case ICmpInst::ICMP_ULE:
3022 case ICmpInst::ICMP_NE:
3023 case ICmpInst::ICMP_UGT:
3027 case ICmpInst::ICMP_SLT: {
3035 case ICmpInst::ICMP_SLE: {
3044 case ICmpInst::ICMP_SGE: {
3052 case ICmpInst::ICMP_SGT: {
3107 *MulC != 0 &&
C->urem(*MulC) != 0) ||
3109 *MulC != 0 &&
C->srem(*MulC) != 0)))
3118 unsigned MaxRecurse) {
3124 if (Pred == ICmpInst::ICMP_ULT)
3126 if (Pred == ICmpInst::ICMP_UGE)
3129 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3141 if (Pred == ICmpInst::ICMP_UGT)
3143 if (Pred == ICmpInst::ICMP_ULE)
3152 case ICmpInst::ICMP_SGT:
3153 case ICmpInst::ICMP_SGE: {
3159 case ICmpInst::ICMP_EQ:
3160 case ICmpInst::ICMP_UGT:
3161 case ICmpInst::ICMP_UGE:
3163 case ICmpInst::ICMP_SLT:
3164 case ICmpInst::ICMP_SLE: {
3170 case ICmpInst::ICMP_NE:
3171 case ICmpInst::ICMP_ULT:
3172 case ICmpInst::ICMP_ULE:
3179 if (Pred == ICmpInst::ICMP_ULE)
3181 if (Pred == ICmpInst::ICMP_UGT)
3192 if (Pred == ICmpInst::ICMP_UGT)
3194 if (Pred == ICmpInst::ICMP_ULE)
3215 case ICmpInst::ICMP_EQ:
3216 case ICmpInst::ICMP_UGE:
3218 case ICmpInst::ICMP_NE:
3219 case ICmpInst::ICMP_ULT:
3221 case ICmpInst::ICMP_UGT:
3222 case ICmpInst::ICMP_ULE:
3238 const APInt *C1, *C2;
3245 if (Pred == ICmpInst::ICMP_UGT)
3247 if (Pred == ICmpInst::ICMP_ULE)
3285 const APInt *C1, *C2;
3299 unsigned MaxRecurse) {
3302 if (MaxRecurse && (LBO || RBO)) {
3304 Value *
A =
nullptr, *
B =
nullptr, *
C =
nullptr, *
D =
nullptr;
3306 bool NoLHSWrapProblem =
false, NoRHSWrapProblem =
false;
3307 if (LBO && LBO->
getOpcode() == Instruction::Add) {
3317 if (RBO && RBO->
getOpcode() == Instruction::Add) {
3329 if ((
A ==
RHS ||
B ==
RHS) && NoLHSWrapProblem)
3336 if ((
C ==
LHS ||
D ==
LHS) && NoRHSWrapProblem)
3339 C ==
LHS ?
D :
C, Q, MaxRecurse - 1))
3343 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3345 if (
A &&
C && (
A ==
C ||
A ==
D ||
B ==
C ||
B ==
D) && CanSimplify) {
3352 }
else if (
A ==
D) {
3356 }
else if (
B ==
C) {
3377 ICmpInst::getSwappedPredicate(Pred), RBO,
LHS, Q, MaxRecurse))
3384 if (
C->isStrictlyPositive()) {
3385 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3387 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3390 if (
C->isNonNegative()) {
3391 if (Pred == ICmpInst::ICMP_SLE)
3393 if (Pred == ICmpInst::ICMP_SGT)
3416 if (Pred == ICmpInst::ICMP_EQ)
3418 if (Pred == ICmpInst::ICMP_NE)
3427 if (Pred == ICmpInst::ICMP_UGT)
3429 if (Pred == ICmpInst::ICMP_ULE)
3440 case Instruction::Shl:
3443 if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3456 case Instruction::UDiv:
3457 case Instruction::LShr:
3458 if (ICmpInst::isSigned(Pred) || !Q.
IIQ.
isExact(LBO) ||
3465 case Instruction::SDiv:
3473 case Instruction::AShr:
3480 case Instruction::Shl: {
3485 if (!NSW && ICmpInst::isSigned(Pred))
3501 unsigned MaxRecurse) {
3657 Pred = ICmpInst::getSwappedPredicate(Pred);
3663 (
A ==
C ||
A ==
D ||
B ==
C ||
B ==
D)) {
3672 (
A ==
C ||
A ==
D ||
B ==
C ||
B ==
D)) {
3696 CallInst *Assume = cast<CallInst>(AssumeVH);
3722 assert(!isa<UndefValue>(
LHS) &&
"Unexpected icmp undef,%X");
3727 if (isa<PoisonValue>(
RHS))
3754 if (isa<Instruction>(
RHS) && isa<Instruction>(
LHS)) {
3755 auto RHS_Instr = cast<Instruction>(
RHS);
3756 auto LHS_Instr = cast<Instruction>(
LHS);
3761 *RHS_Instr->getMetadata(LLVMContext::MD_range));
3763 *LHS_Instr->getMetadata(LLVMContext::MD_range));
3765 if (LHS_CR.icmp(Pred, RHS_CR))
3774 if (isa<CastInst>(
LHS) && (isa<Constant>(
RHS) || isa<CastInst>(
RHS))) {
3782 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3791 if (RI->getOperand(0)->getType() == SrcTy)
3799 if (isa<ZExtInst>(
LHS)) {
3803 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3807 RI->getOperand(0), Q, MaxRecurse - 1))
3811 else if (
SExtInst *RI = dyn_cast<SExtInst>(
RHS)) {
3812 if (
SrcOp == RI->getOperand(0)) {
3813 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3815 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3829 if (RExt == CI && MaxRecurse)
3831 SrcOp, Trunc, Q, MaxRecurse - 1))
3841 case ICmpInst::ICMP_EQ:
3842 case ICmpInst::ICMP_UGT:
3843 case ICmpInst::ICMP_UGE:
3846 case ICmpInst::ICMP_NE:
3847 case ICmpInst::ICMP_ULT:
3848 case ICmpInst::ICMP_ULE:
3853 case ICmpInst::ICMP_SGT:
3854 case ICmpInst::ICMP_SGE:
3855 return CI->getValue().isNegative()
3859 case ICmpInst::ICMP_SLT:
3860 case ICmpInst::ICMP_SLE:
3861 return CI->getValue().isNegative()
3869 if (isa<SExtInst>(
LHS)) {
3873 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3880 else if (
ZExtInst *RI = dyn_cast<ZExtInst>(
RHS)) {
3881 if (
SrcOp == RI->getOperand(0)) {
3882 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3884 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3898 if (RExt == CI && MaxRecurse)
3909 case ICmpInst::ICMP_EQ:
3911 case ICmpInst::ICMP_NE:
3916 case ICmpInst::ICMP_SGT:
3917 case ICmpInst::ICMP_SGE:
3918 return CI->getValue().isNegative()
3921 case ICmpInst::ICMP_SLT:
3922 case ICmpInst::ICMP_SLE:
3923 return CI->getValue().isNegative()
3929 case ICmpInst::ICMP_UGT:
3930 case ICmpInst::ICMP_UGE:
3938 case ICmpInst::ICMP_ULT:
3939 case ICmpInst::ICMP_ULE:
3970 if (std::optional<bool> Res =
3979 if (
auto *CLHS = dyn_cast<PtrToIntOperator>(
LHS))
3980 if (
auto *CRHS = dyn_cast<PtrToIntOperator>(
RHS))
3986 CRHS->getPointerOperand(), Q))
3991 if (isa<SelectInst>(
LHS) || isa<SelectInst>(
RHS))
3997 if (isa<PHINode>(
LHS) || isa<PHINode>(
RHS))
4013 unsigned MaxRecurse) {
4029 if (Pred == FCmpInst::FCMP_FALSE)
4031 if (Pred == FCmpInst::FCMP_TRUE)
4035 if (Pred == FCmpInst::FCMP_UNO || Pred == FCmpInst::FCMP_ORD)
4041 assert((FCmpInst::isOrdered(Pred) || FCmpInst::isUnordered(Pred)) &&
4042 "Comparison must be either ordered or unordered");
4048 if (isa<PoisonValue>(
LHS) || isa<PoisonValue>(
RHS))
4073 if (
C->isInfinity()) {
4074 if (
C->isNegative()) {
4076 case FCmpInst::FCMP_OLT:
4079 case FCmpInst::FCMP_UGE:
4087 case FCmpInst::FCMP_OGT:
4090 case FCmpInst::FCMP_ULE:
4113 if (
C->isNegative() && !
C->isNegZero()) {
4114 assert(!
C->isNaN() &&
"Unexpected NaN constant!");
4118 case FCmpInst::FCMP_UGE:
4119 case FCmpInst::FCMP_UGT:
4120 case FCmpInst::FCMP_UNE:
4125 case FCmpInst::FCMP_OEQ:
4126 case FCmpInst::FCMP_OLE:
4127 case FCmpInst::FCMP_OLT:
4144 cast<IntrinsicInst>(
LHS)->getIntrinsicID() == Intrinsic::maxnum;
4148 case FCmpInst::FCMP_OEQ:
4149 case FCmpInst::FCMP_UEQ:
4153 case FCmpInst::FCMP_ONE:
4154 case FCmpInst::FCMP_UNE:
4158 case FCmpInst::FCMP_OGE:
4159 case FCmpInst::FCMP_UGE:
4160 case FCmpInst::FCMP_OGT:
4161 case FCmpInst::FCMP_UGT:
4167 case FCmpInst::FCMP_OLE:
4168 case FCmpInst::FCMP_ULE:
4169 case FCmpInst::FCMP_OLT:
4170 case FCmpInst::FCMP_ULT:
4185 case FCmpInst::FCMP_OGE:
4186 case FCmpInst::FCMP_ULT:
4193 case FCmpInst::FCMP_UGE:
4194 case FCmpInst::FCMP_OLT: