48#define DEBUG_TYPE "instsimplify"
85 if (
auto *BO = dyn_cast<BinaryOperator>(
Cond))
86 BinOpCode = BO->getOpcode();
91 if (BinOpCode == BinaryOperator::Or) {
92 ExpectedPred = ICmpInst::ICMP_NE;
93 }
else if (BinOpCode == BinaryOperator::And) {
94 ExpectedPred = ICmpInst::ICMP_EQ;
115 Pred1 != Pred2 || Pred1 != ExpectedPred)
118 if (
X == TrueVal ||
X == FalseVal ||
Y == TrueVal ||
Y == FalseVal)
119 return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal;
135 CmpInst *Cmp = dyn_cast<CmpInst>(V);
139 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
140 if (CPred == Pred && CLHS ==
LHS && CRHS ==
RHS)
156 if (SimplifiedCmp ==
Cond) {
164 return SimplifiedCmp;
171 unsigned MaxRecurse) {
180 unsigned MaxRecurse) {
190 unsigned MaxRecurse) {
227 if (
I->getParent()->isEntryBlock() && !isa<InvokeInst>(
I) &&
240 auto *
B = dyn_cast<BinaryOperator>(V);
241 if (!
B ||
B->getOpcode() != OpcodeToExpand)
243 Value *B0 =
B->getOperand(0), *B1 =
B->getOperand(1);
254 if ((L == B0 && R == B1) ||
275 unsigned MaxRecurse) {
292 unsigned MaxRecurse) {
395 unsigned MaxRecurse) {
401 if (isa<SelectInst>(
LHS)) {
402 SI = cast<SelectInst>(
LHS);
404 assert(isa<SelectInst>(
RHS) &&
"No select instruction operand!");
405 SI = cast<SelectInst>(
RHS);
432 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
438 if ((FV && !TV) || (TV && !FV)) {
441 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
442 if (Simplified && Simplified->getOpcode() ==
unsigned(Opcode)) {
446 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
447 Value *UnsimplifiedLHS = SI ==
LHS ? UnsimplifiedBranch :
LHS;
448 Value *UnsimplifiedRHS = SI ==
LHS ?
RHS : UnsimplifiedBranch;
449 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
450 Simplified->getOperand(1) == UnsimplifiedRHS)
452 if (Simplified->isCommutative() &&
453 Simplified->getOperand(1) == UnsimplifiedLHS &&
454 Simplified->getOperand(0) == UnsimplifiedRHS)
473 unsigned MaxRecurse) {
479 if (!isa<SelectInst>(
LHS)) {
483 assert(isa<SelectInst>(
LHS) &&
"Not comparing with a select instruction!");
486 Value *TV = SI->getTrueValue();
487 Value *FV = SI->getFalseValue();
519 unsigned MaxRecurse) {
525 if (isa<PHINode>(
LHS)) {
526 PI = cast<PHINode>(
LHS);
531 assert(isa<PHINode>(
RHS) &&
"No PHI instruction operand!");
532 PI = cast<PHINode>(
RHS);
539 Value *CommonValue =
nullptr;
552 if (!V || (CommonValue && V != CommonValue))
571 if (!isa<PHINode>(
LHS)) {
575 assert(isa<PHINode>(
LHS) &&
"Not comparing with a phi instruction!");
583 Value *CommonValue =
nullptr;
597 if (!V || (CommonValue && V != CommonValue))
608 if (
auto *CLHS = dyn_cast<Constant>(Op0)) {
609 if (
auto *CRHS = dyn_cast<Constant>(Op1)) {
613 case Instruction::FAdd:
614 case Instruction::FSub:
615 case Instruction::FMul:
616 case Instruction::FDiv:
617 case Instruction::FRem:
618 if (Q.
CxtI !=
nullptr)
639 if (isa<PoisonValue>(Op1))
702 return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query,
RecursionLimit);
715 bool AllowNonInbounds =
false) {
716 assert(V->getType()->isPtrOrPtrVectorTy());
719 V = V->stripAndAccumulateConstantOffsets(
DL,
Offset, AllowNonInbounds);
722 return Offset.sextOrTrunc(
DL.getIndexTypeSizeInBits(V->getType()));
742 if (
auto *VecTy = dyn_cast<VectorType>(
LHS->
getType()))
757 std::optional<bool> Imp =
762 case Instruction::Sub:
763 case Instruction::Xor:
764 case Instruction::URem:
765 case Instruction::SRem:
768 case Instruction::SDiv:
769 case Instruction::UDiv:
772 case Instruction::And:
773 case Instruction::Or:
792 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
828 Value *
X =
nullptr, *
Y =
nullptr, *Z = Op1;
886 if (
X->getType() ==
Y->getType())
922 return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
933 if (isa<PoisonValue>(Op1))
957 return ConstantInt::getNullValue(Op0->
getType());
972 Instruction::Add, Q, MaxRecurse))
977 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
984 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
994 return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
1003 Constant *
C = dyn_cast_or_null<Constant>(V);
1004 return (
C &&
C->isAllOnesValue());
1010 unsigned MaxRecurse,
bool IsSigned) {
1027 Type *Ty =
X->getType();
1043 if (
C->isMinSignedValue())
1070 return isICmpTrue(ICmpInst::ICMP_ULT,
X,
Y, Q, MaxRecurse);
1077 unsigned MaxRecurse) {
1078 bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
1079 bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
1096 auto *Op1C = dyn_cast<Constant>(Op1);
1097 auto *VTy = dyn_cast<FixedVectorType>(Ty);
1099 unsigned NumElts = VTy->getNumElements();
1100 for (
unsigned i = 0; i != NumElts; ++i) {
1109 if (isa<PoisonValue>(Op0))
1150 auto *
Mul = cast<OverflowingBinaryOperator>(Op0);
1161 if (
isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1169 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1175 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1185 unsigned MaxRecurse) {
1216 ((Opcode == Instruction::SRem &&
1218 (Opcode == Instruction::URem &&
1233 return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1245 return simplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1256 unsigned MaxRecurse) {
1261 return ConstantInt::getNullValue(Op0->
getType());
1265 return ConstantInt::getNullValue(Op0->
getType());
1267 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1277 unsigned MaxRecurse) {
1278 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1287 Constant *
C = dyn_cast<Constant>(Amount);
1297 const APInt *AmountC;
1303 if (isa<ConstantVector>(
C) || isa<ConstantDataVector>(
C)) {
1304 for (
unsigned I = 0,
1305 E = cast<FixedVectorType>(
C->getType())->getNumElements();
1319 unsigned MaxRecurse) {
1324 if (isa<PoisonValue>(Op0))
1345 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1351 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1369 assert(Opcode == Instruction::Shl &&
"Expected shl for nsw instruction");
1388 Value *Op1,
bool IsExact,
1408 if (Op0Known.
One[0])
1420 simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
1444 if (IsNSW && IsNUW &&
1453 return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
1475 const APInt *ShRAmt, *ShLAmt;
1478 *ShRAmt == *ShLAmt) {
1481 if (ShRAmt->
uge(EffWidthY))
1529 ICmpInst *UnsignedICmp,
bool IsAnd,
1543 if (
match(UnsignedICmp,
1545 ICmpInst::isUnsigned(UnsignedPred)) {
1547 if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1548 UnsignedPred == ICmpInst::ICMP_ULE) &&
1549 EqPred == ICmpInst::ICMP_NE && !IsAnd)
1552 if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1553 UnsignedPred == ICmpInst::ICMP_UGT) &&
1554 EqPred == ICmpInst::ICMP_EQ && IsAnd)
1559 if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1560 UnsignedPred == ICmpInst::ICMP_UGT))
1561 return IsAnd ? UnsignedICmp : ZeroICmp;
1565 if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1566 UnsignedPred == ICmpInst::ICMP_UGE))
1567 return IsAnd ? ZeroICmp : UnsignedICmp;
1573 if (
match(UnsignedICmp,
1575 if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1576 EqPred == ICmpInst::ICMP_NE &&
1578 return UnsignedICmp;
1579 if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1580 EqPred == ICmpInst::ICMP_EQ &&
1582 return UnsignedICmp;
1587 ICmpInst::isUnsigned(UnsignedPred))
1589 else if (
match(UnsignedICmp,
1591 ICmpInst::isUnsigned(UnsignedPred))
1592 UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1598 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1600 return IsAnd ? ZeroICmp : UnsignedICmp;
1604 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1606 return IsAnd ? UnsignedICmp : ZeroICmp;
1615 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1616 return IsAnd ? UnsignedICmp : ZeroICmp;
1620 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1621 return IsAnd ? ZeroICmp : UnsignedICmp;
1624 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1629 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1645 const APInt *C0, *C1;
1655 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1660 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1668 if (Range0.contains(Range1))
1669 return IsAnd ? Cmp1 : Cmp0;
1670 if (Range1.contains(Range0))
1671 return IsAnd ? Cmp0 : Cmp1;
1683 if ((IsAnd && P0 != ICmpInst::ICMP_NE) || (!IsAnd && P1 != ICmpInst::ICMP_EQ))
1717 const APInt *C0, *C1;
1725 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->
getOperand(0));
1726 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1733 const APInt Delta = *C1 - *C0;
1736 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1738 if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1742 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1744 if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1750 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1753 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1786 MinMaxC = HasNotOp ? ~*
C : *
C;
1787 else if (isa<ConstantPointerNull>(Cmp0->
getOperand(1)))
1794 Pred0 = ICmpInst::getInversePredicate(Pred0);
1795 Pred1 = ICmpInst::getInversePredicate(Pred1);
1800 if (ICmpInst::isSigned(Pred1)) {
1808 if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT)
1814 if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_UGT)
1832 if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1835 if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1874 const APInt *C0, *C1;
1882 auto *AddInst = cast<BinaryOperator>(Op0->
getOperand(0));
1883 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1890 const APInt Delta = *C1 - *C0;
1893 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1895 if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1899 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1901 if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1907 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1910 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1948 Value *LHS0 =
LHS->getOperand(0), *LHS1 =
LHS->getOperand(1);
1949 Value *RHS0 =
RHS->getOperand(0), *RHS1 =
RHS->getOperand(1);
1957 if ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) ||
1958 (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO && !IsAnd)) {
1967 if (((LHS1 == RHS0 || LHS1 == RHS1) &&
1969 ((LHS0 == RHS0 || LHS0 == RHS1) &&
1981 if (((RHS1 == LHS0 || RHS1 == LHS1) &&
1983 ((RHS0 == LHS0 || RHS0 == LHS1) &&
1992 Value *Op1,
bool IsAnd) {
1994 auto *Cast0 = dyn_cast<CastInst>(Op0);
1995 auto *Cast1 = dyn_cast<CastInst>(Op1);
1996 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1997 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1998 Op0 = Cast0->getOperand(0);
1999 Op1 = Cast1->getOperand(0);
2003 auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
2004 auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
2009 auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
2010 auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
2021 if (
auto *
C = dyn_cast<Constant>(V))
2032 assert(BinaryOperator::isBitwiseLogicOp(Opcode) &&
"Expected logic op");
2044 return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
2045 : ConstantInt::getAllOnesValue(Ty);
2054 unsigned MaxRecurse) {
2059 if (isa<PoisonValue>(Op1))
2109 (~(*Mask)).lshr(*ShAmt).isZero())
2115 (~(*Mask)).shl(*ShAmt).isZero())
2120 const APInt *PowerC;
2129 return ConstantInt::getNullValue(Op1->
getType());
2171 Instruction::Or, Q, MaxRecurse))
2176 Instruction::Xor, Q, MaxRecurse))
2179 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2197 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2221 if (EffWidthY <= ShftCnt) {
2247 if (*Implied ==
true)
2250 if (*Implied ==
false)
2275 assert(
X->getType() ==
Y->getType() &&
"Expected same type for 'or' ops");
2276 Type *Ty =
X->getType();
2280 return ConstantInt::getAllOnesValue(Ty);
2284 return ConstantInt::getAllOnesValue(Ty);
2302 return ConstantInt::getAllOnesValue(Ty);
2326 return ConstantInt::getAllOnesValue(Ty);
2368 unsigned MaxRecurse) {
2373 if (isa<PoisonValue>(Op1))
2407 C->ule(
X->getType()->getScalarSizeInBits())) {
2408 return ConstantInt::getAllOnesValue(
X->getType());
2455 Instruction::And, Q, MaxRecurse))
2458 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2476 const APInt *C1, *C2;
2502 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2507 if (std::optional<bool> Implied =
2510 if (*Implied ==
false)
2513 if (*Implied ==
true)
2516 if (std::optional<bool> Implied =
2519 if (*Implied ==
false)
2522 if (*Implied ==
true)
2540 unsigned MaxRecurse) {
2545 if (isa<PoisonValue>(Op1))
2583 if (
Value *R = foldAndOrNot(Op0, Op1))
2585 if (
Value *R = foldAndOrNot(Op1, Op0))
2627 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2630 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2631 if (Pred == Cmp->getPredicate() &&
LHS == CmpLHS &&
RHS == CmpRHS)
2634 LHS == CmpRHS &&
RHS == CmpLHS)
2647 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2648 return AI->isStaticAlloca();
2649 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2650 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2651 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2652 !GV->isThreadLocal();
2653 if (
const Argument *
A = dyn_cast<Argument>(V))
2654 return A->hasByValAttr();
2687 auto isByValArg = [](
const Value *V) {
2688 const Argument *
A = dyn_cast<Argument>(V);
2689 return A &&
A->hasByValAttr();
2695 return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2697 return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2699 return isa<AllocaInst>(V1) &&
2700 (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
2778 unsigned IndexSize =
DL.getIndexTypeSizeInBits(
LHS->
getType());
2779 APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2799 Opts.
EvalMode = ObjectSizeOpts::Mode::Min;
2801 if (
auto *
I = dyn_cast<Instruction>(V))
2802 return I->getFunction();
2803 if (
auto *
A = dyn_cast<Argument>(V))
2804 return A->getParent();
2810 APInt Dist = LHSOffset - RHSOffset;
2838 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2839 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2861 bool Captured =
false;
2864 if (
auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2868 unsigned OtherIdx = 1 - U->getOperandNo();
2869 auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2870 if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2878 CustomCaptureTracker Tracker;
2880 if (!Tracker.Captured)
2902 auto ExtractNotLHS = [](
Value *V) ->
Value * {
2964 case ICmpInst::ICMP_UGE:
2968 case ICmpInst::ICMP_SGE:
2979 case ICmpInst::ICMP_ULE:
2983 case ICmpInst::ICMP_SLE:
3003 case ICmpInst::ICMP_ULT:
3005 case ICmpInst::ICMP_UGE:
3007 case ICmpInst::ICMP_EQ:
3008 case ICmpInst::ICMP_ULE:
3012 case ICmpInst::ICMP_NE:
3013 case ICmpInst::ICMP_UGT:
3017 case ICmpInst::ICMP_SLT: {
3025 case ICmpInst::ICMP_SLE: {
3034 case ICmpInst::ICMP_SGE: {
3042 case ICmpInst::ICMP_SGT: {
3097 *MulC != 0 &&
C->urem(*MulC) != 0) ||
3099 *MulC != 0 &&
C->srem(*MulC) != 0)))
3108 unsigned MaxRecurse) {
3114 if (Pred == ICmpInst::ICMP_ULT)
3116 if (Pred == ICmpInst::ICMP_UGE)
3119 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3131 if (Pred == ICmpInst::ICMP_UGT)
3133 if (Pred == ICmpInst::ICMP_ULE)
3142 case ICmpInst::ICMP_SGT:
3143 case ICmpInst::ICMP_SGE: {
3149 case ICmpInst::ICMP_EQ:
3150 case ICmpInst::ICMP_UGT:
3151 case ICmpInst::ICMP_UGE:
3153 case ICmpInst::ICMP_SLT:
3154 case ICmpInst::ICMP_SLE: {
3160 case ICmpInst::ICMP_NE:
3161 case ICmpInst::ICMP_ULT:
3162 case ICmpInst::ICMP_ULE:
3169 if (Pred == ICmpInst::ICMP_ULE)
3171 if (Pred == ICmpInst::ICMP_UGT)
3182 if (Pred == ICmpInst::ICMP_UGT)
3184 if (Pred == ICmpInst::ICMP_ULE)
3205 case ICmpInst::ICMP_EQ:
3206 case ICmpInst::ICMP_UGE:
3208 case ICmpInst::ICMP_NE:
3209 case ICmpInst::ICMP_ULT:
3211 case ICmpInst::ICMP_UGT:
3212 case ICmpInst::ICMP_ULE:
3228 const APInt *C1, *C2;
3235 if (Pred == ICmpInst::ICMP_UGT)
3237 if (Pred == ICmpInst::ICMP_ULE)
3275 const APInt *C1, *C2;
3289 unsigned MaxRecurse) {
3292 if (MaxRecurse && (LBO || RBO)) {
3294 Value *
A =
nullptr, *
B =
nullptr, *
C =
nullptr, *
D =
nullptr;
3296 bool NoLHSWrapProblem =
false, NoRHSWrapProblem =
false;
3297 if (LBO && LBO->
getOpcode() == Instruction::Add) {
3307 if (RBO && RBO->
getOpcode() == Instruction::Add) {
3319 if ((
A ==
RHS ||
B ==
RHS) && NoLHSWrapProblem)
3326 if ((
C ==
LHS ||
D ==
LHS) && NoRHSWrapProblem)
3329 C ==
LHS ?
D :
C, Q, MaxRecurse - 1))
3333 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3335 if (
A &&
C && (
A ==
C ||
A ==
D ||
B ==
C ||
B ==
D) && CanSimplify) {
3342 }
else if (
A ==
D) {
3346 }
else if (
B ==
C) {
3367 ICmpInst::getSwappedPredicate(Pred), RBO,
LHS, Q, MaxRecurse))
3374 if (
C->isStrictlyPositive()) {
3375 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3377 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3380 if (
C->isNonNegative()) {
3381 if (Pred == ICmpInst::ICMP_SLE)
3383 if (Pred == ICmpInst::ICMP_SGT)
3406 if (Pred == ICmpInst::ICMP_EQ)
3408 if (Pred == ICmpInst::ICMP_NE)
3417 if (Pred == ICmpInst::ICMP_UGT)
3419 if (Pred == ICmpInst::ICMP_ULE)
3430 case Instruction::Shl: {
3433 if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3446 case Instruction::And:
3447 case Instruction::Or: {
3448 const APInt *C1, *C2;
3454 Pred = ICmpInst::getSwappedPredicate(Pred);
3457 if (Pred == ICmpInst::ICMP_ULE)
3459 if (Pred == ICmpInst::ICMP_UGT)
3462 if (Pred == ICmpInst::ICMP_SLE)
3464 if (Pred == ICmpInst::ICMP_SGT)
3478 case Instruction::UDiv:
3479 case Instruction::LShr:
3480 if (ICmpInst::isSigned(Pred) || !Q.
IIQ.
isExact(LBO) ||
3487 case Instruction::SDiv:
3495 case Instruction::AShr:
3502 case Instruction::Shl: {
3507 if (!NSW && ICmpInst::isSigned(Pred))
3523 unsigned MaxRecurse) {
3679 Pred = ICmpInst::getSwappedPredicate(Pred);
3685 (
A ==
C ||
A ==
D ||
B ==
C ||
B ==
D)) {
3694 (
A ==
C ||
A ==
D ||
B ==
C ||
B ==
D)) {
3718 CallInst *Assume = cast<CallInst>(AssumeVH);
3731 auto *II = dyn_cast<IntrinsicInst>(
LHS);
3735 switch (II->getIntrinsicID()) {
3736 case Intrinsic::uadd_sat:
3738 if (II->getArgOperand(0) ==
RHS || II->getArgOperand(1) ==
RHS) {
3739 if (Pred == ICmpInst::ICMP_UGE)
3741 if (Pred == ICmpInst::ICMP_ULT)
3745 case Intrinsic::usub_sat:
3747 if (II->getArgOperand(0) ==
RHS) {
3748 if (Pred == ICmpInst::ICMP_ULE)
3750 if (Pred == ICmpInst::ICMP_UGT)
3774 assert(!isa<UndefValue>(
LHS) &&
"Unexpected icmp undef,%X");
3779 if (isa<PoisonValue>(
RHS))
3806 if (isa<Instruction>(
RHS) && isa<Instruction>(
LHS)) {
3807 auto RHS_Instr = cast<Instruction>(
RHS);
3808 auto LHS_Instr = cast<Instruction>(
LHS);
3813 *RHS_Instr->getMetadata(LLVMContext::MD_range));
3815 *LHS_Instr->getMetadata(LLVMContext::MD_range));
3817 if (LHS_CR.icmp(Pred, RHS_CR))
3826 if (isa<CastInst>(
LHS) && (isa<Constant>(
RHS) || isa<CastInst>(
RHS))) {
3834 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3843 if (RI->getOperand(0)->getType() == SrcTy)
3851 if (isa<ZExtInst>(
LHS)) {
3855 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3859 RI->getOperand(0), Q, MaxRecurse - 1))
3863 else if (
SExtInst *RI = dyn_cast<SExtInst>(
RHS)) {
3864 if (
SrcOp == RI->getOperand(0)) {
3865 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3867 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3888 SrcOp, Trunc, Q, MaxRecurse - 1))
3898 case ICmpInst::ICMP_EQ:
3899 case ICmpInst::ICMP_UGT:
3900 case ICmpInst::ICMP_UGE:
3903 case ICmpInst::ICMP_NE:
3904 case ICmpInst::ICMP_ULT:
3905 case ICmpInst::ICMP_ULE:
3910 case ICmpInst::ICMP_SGT:
3911 case ICmpInst::ICMP_SGE:
3914 case ICmpInst::ICMP_SLT:
3915 case ICmpInst::ICMP_SLE:
3923 if (isa<SExtInst>(
LHS)) {
3927 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3934 else if (
ZExtInst *RI = dyn_cast<ZExtInst>(
RHS)) {
3935 if (
SrcOp == RI->getOperand(0)) {
3936 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3938 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3967 case ICmpInst::ICMP_EQ:
3969 case ICmpInst::ICMP_NE:
3974 case ICmpInst::ICMP_SGT:
3975 case ICmpInst::ICMP_SGE:
3978 case ICmpInst::ICMP_SLT:
3979 case ICmpInst::ICMP_SLE:
3985 case ICmpInst::ICMP_UGT:
3986 case ICmpInst::ICMP_UGE:
3994 case ICmpInst::ICMP_ULT:
3995 case ICmpInst::ICMP_ULE:
4026 ICmpInst::getSwappedPredicate(Pred),
RHS,
LHS))
4032 if (std::optional<bool> Res =
4041 if (
auto *CLHS = dyn_cast<PtrToIntOperator>(
LHS))
4042 if (
auto *CRHS = dyn_cast<PtrToIntOperator>(
RHS))
4043 if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4047 CRHS->getPointerOperand(), Q))
4052 if (isa<SelectInst>(
LHS) || isa<SelectInst>(
RHS))
4058 if (isa<PHINode>(
LHS) || isa<PHINode>(
RHS))
4074 unsigned MaxRecurse) {
4090 if (Pred == FCmpInst::FCMP_FALSE)
4092 if (Pred == FCmpInst::FCMP_TRUE)
4097 if (isa<PoisonValue>(
LHS) || isa<PoisonValue>(
RHS))
4120 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4129 std::optional<KnownFPClass> FullKnownClassLHS;
4133 auto computeLHSClass = [=, &FullKnownClassLHS](
FPClassTest InterestedFlags =
4135 if (FullKnownClassLHS)
4136 return *FullKnownClassLHS;
4150 FullKnownClassLHS = computeLHSClass();
4151 if ((FullKnownClassLHS->KnownFPClasses & ClassTest) ==
fcNone)
4153 if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) ==
fcNone)
4168 if (
C->isNegative() && !
C->isNegZero()) {
4174 case FCmpInst::FCMP_UGE:
4175 case FCmpInst::FCMP_UGT:
4176 case FCmpInst::FCMP_UNE: {