Lines Matching +full:ignore +full:- +full:power +full:- +full:on +full:- +full:sel
1 //===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
12 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
13 // ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been
17 //===----------------------------------------------------------------------===//
87 BinOpCode = BO->getOpcode();
103 // -->
110 // -->
133 /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
139 CmpInst::Predicate CPred = Cmp->getPredicate();
140 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
148 /// %sel = select i1 %cond, i32 %tv, i32 %fv
149 /// %cmp = icmp sle i32 %sel, %rhs
150 /// Compose new comparison by substituting %sel with either %tv or %fv
174 getTrue(Cond->getType()));
183 getFalse(Cond->getType()));
195 // Folding select to and/or isn't poison-safe in general; impliesPoison
196 // checks whether folding it does not convert a well-defined value into
210 Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse))
224 return DT->dominates(I, P);
228 if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
242 if (!B || B->getOpcode() != OpcodeToExpand)
244 Value *B0 = B->getOperand(0), *B1 = B->getOperand(1);
278 if (!MaxRecurse--)
297 if (!MaxRecurse--)
304 if (Op0 && Op0->getOpcode() == Opcode) {
305 Value *A = Op0->getOperand(0);
306 Value *B = Op0->getOperand(1);
324 if (Op1 && Op1->getOpcode() == Opcode) {
326 Value *B = Op1->getOperand(0);
327 Value *C = Op1->getOperand(1);
348 if (Op0 && Op0->getOpcode() == Opcode) {
349 Value *A = Op0->getOperand(0);
350 Value *B = Op0->getOperand(1);
368 if (Op1 && Op1->getOpcode() == Opcode) {
370 Value *B = Op1->getOperand(0);
371 Value *C = Op1->getOperand(1);
391 /// try to simplify the binop by seeing whether evaluating it on both branches
398 if (!MaxRecurse--)
409 // Evaluate the BinOp on the true and false branches of the select.
413 TV = simplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
414 FV = simplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
416 TV = simplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
417 FV = simplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
433 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
438 // For example, select (cond, X, X & Z) & Z -> X & Z.
443 if (Simplified && Simplified->getOpcode() == unsigned(Opcode) &&
444 !Simplified->hasPoisonGeneratingFlags()) {
448 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
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)
477 if (!MaxRecurse--)
480 // Make sure the select is on the LHS.
487 Value *Cond = SI->getCondition();
488 Value *TV = SI->getTrueValue();
489 Value *FV = SI->getFalseValue();
509 if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy())
516 /// try to simplify the binop by seeing whether evaluating it on the incoming
523 if (!MaxRecurse--)
540 // Evaluate the BinOp on the incoming phi values.
542 for (Use &Incoming : PI->incoming_values()) {
546 Instruction *InTI = PI->getIncomingBlock(Incoming)->getTerminator();
569 if (!MaxRecurse--)
572 // Make sure the phi is on the LHS.
584 // Evaluate the BinOp on the incoming phi values.
586 for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
587 Value *Incoming = PI->getIncomingValue(u);
588 Instruction *InTI = PI->getIncomingBlock(u)->getTerminator();
640 // X + poison -> poison
644 // X + undef -> undef
648 // X + 0 -> X
654 return Constant::getNullValue(Op0->getType());
656 // X + (Y - X) -> Y
657 // (Y - X) + X -> Y
658 // Eg: X + -X -> 0
664 // X + ~X -> -1 since ~X = -X-1
665 Type *Ty = Op0->getType();
669 // add nsw/nuw (xor Y, signmask), signmask --> Y
670 // The no-wrapping add guarantees that the top bit will be set by the add.
676 // add nuw %x, -1 -> -1, because %x can only be 0.
678 return Op1; // Which is -1.
680 /// i1 add -> xor.
681 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
682 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
718 assert(V->getType()->isPtrOrPtrVectorTy());
720 APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
721 V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
724 return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType()));
739 // Otherwise, the difference of LHS - RHS can be computed as:
740 // LHS - RHS
741 // = (LHSOffset + Base) - (RHSOffset + Base)
742 // = LHSOffset - RHSOffset
743 Constant *Res = ConstantInt::get(LHS->getContext(), LHSOffset - RHSOffset);
744 if (auto *VecTy = dyn_cast<VectorType>(LHS->getType()))
745 Res = ConstantVector::getSplat(VecTy->getElementCount(), Res);
752 /// Example: Op0 - Op1 --> 0 when Op0 == Op1
762 Type *Ty = Op0->getType();
776 // Could be either one - choose Op1 since that's more likely a constant.
792 // X - poison -> poison
793 // poison - X -> poison
795 return PoisonValue::get(Op0->getType());
797 // X - undef -> undef
798 // undef - X -> undef
800 return UndefValue::get(Op0->getType());
802 // X - 0 -> X
806 // X - X -> 0
808 return Constant::getNullValue(Op0->getType());
812 // 0 - X -> 0 if the sub is NUW.
814 return Constant::getNullValue(Op0->getType());
821 return Constant::getNullValue(Op0->getType());
823 // 0 - X -> X if X is 0 or the minimum signed value.
828 // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
829 // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
831 if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
832 // See if "V === Y - Z" simplifies.
833 if (Value *V = simplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1))
835 if (Value *W = simplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) {
840 // See if "V === X - Z" simplifies.
841 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
843 if (Value *W = simplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) {
850 // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
851 // For example, X - (X + 1) -> -1
853 if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
854 // See if "V === X - Y" simplifies.
855 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
856 // It does! Now see if "V - Z" simplifies.
857 if (Value *W = simplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) {
862 // See if "V === X - Z" simplifies.
863 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
864 // It does! Now see if "V - Y" simplifies.
865 if (Value *W = simplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) {
872 // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
873 // For example, X - (X - Y) -> Y.
875 if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
876 // See if "V === Z - X" simplifies.
877 if (Value *V = simplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1))
879 if (Value *W = simplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) {
885 // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
888 if (X->getType() == Y->getType())
889 // See if "V === X - Y" simplifies.
890 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
892 if (Value *W = simplifyCastInst(Instruction::Trunc, V, Op0->getType(),
893 Q, MaxRecurse - 1))
897 // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
900 return ConstantFoldIntegerCast(Result, Op0->getType(), /*IsSigned*/ true,
903 // i1 sub -> xor.
904 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
905 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
909 // Threading over the select in "A - select(cond, B, C)" means evaluating
910 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
914 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
935 // X * poison -> poison
939 // X * undef -> 0
940 // X * 0 -> 0
942 return Constant::getNullValue(Op0->getType());
944 // X * 1 -> X
948 // (X / Y) * Y -> X if the division is exact.
956 if (Op0->getType()->isIntOrIntVectorTy(1)) {
957 // mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not
960 return ConstantInt::getNullValue(Op0->getType());
964 if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
973 // Mul distributes over Add. Try some generic simplifications based on this.
979 // operating on either branch of the select always yields the same value.
986 // operating on all incoming values of the phi always yields the same value.
1007 return (C && C->isAllOnesValue());
1015 if (!MaxRecurse--)
1019 // (X srem Y) sdiv Y --> 0
1023 // |X| / |Y| --> 0
1030 Type *Ty = X->getType();
1032 if (match(X, m_APInt(C)) && !C->isMinSignedValue()) {
1035 // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
1036 Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
1037 Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
1043 // Special-case: we can't take the abs() of a minimum signed value. If
1046 if (C->isMinSignedValue())
1051 // |X| < |C| --> X > -abs(C) and X < abs(C)
1052 Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
1053 Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
1084 Type *Ty = Op0->getType();
1086 // X / undef -> poison
1087 // X % undef -> poison
1091 // X / 0 -> poison
1092 // X % 0 -> poison
1102 unsigned NumElts = VTy->getNumElements();
1104 Constant *Elt = Op1C->getAggregateElement(i);
1105 if (Elt && (Elt->isNullValue() || Q.isUndefValue(Elt)))
1110 // poison / X -> poison
1111 // poison % X -> poison
1115 // undef / X -> 0
1116 // undef % X -> 0
1120 // 0 / X -> 0
1121 // 0 % X -> 0
1123 return Constant::getNullValue(Op0->getType());
1125 // X / X -> 1
1126 // X % X -> 0
1131 // X / 0 -> poison
1132 // X % 0 -> poison
1139 // X / 1 -> X
1140 // X % 1 -> 0
1141 // If the divisor can only be zero or one, we can't have division-by-zero
1142 // or remainder-by-zero, so assume the divisor is 1.
1144 if (Known.countMinLeadingZeros() == Known.getBitWidth() - 1)
1148 // X * Y / Y -> X
1149 // X * Y % Y -> 0
1159 return IsDiv ? X : Constant::getNullValue(Op0->getType());
1164 return IsDiv ? Constant::getNullValue(Op0->getType()) : Op0;
1170 // operating on either branch of the select always yields the same value.
1176 // operating on all incoming values of the phi always yields the same value.
1199 if (DivC->countr_zero()) {
1201 if (KnownOp0.countMaxTrailingZeros() < DivC->countr_zero())
1202 return PoisonValue::get(Op0->getType());
1205 // udiv exact (mul nsw X, C), C --> X
1206 // sdiv exact (mul nuw X, C), C --> X
1207 // where C is not a power of 2.
1209 if (!DivC->isPowerOf2() &&
1228 // (X << Y) % X -> 0
1234 return Constant::getNullValue(Op0->getType());
1238 // (srem (mul nsw X, C1), C0) -> 0 if C1 s% C0 == 0
1239 // (urem (mul nuw X, C1), C0) -> 0 if C1 u% C0 == 0
1249 return Constant::getNullValue(Op0->getType());
1259 // If two operands are negated and no signed overflow, return -1.
1261 return Constant::getAllOnesValue(Op0->getType());
1287 // If the divisor is 0, the result is undefined, so assume the divisor is -1.
1288 // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
1290 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
1291 return ConstantInt::getNullValue(Op0->getType());
1295 return ConstantInt::getNullValue(Op0->getType());
1321 // X shift by undef -> poison because it may shift by the bitwidth.
1328 if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth()))
1331 // Try harder for fixed-length vectors:
1335 E = cast<FixedVectorType>(C->getType())->getNumElements();
1337 if (!isPoisonShift(C->getAggregateElement(I), Q))
1353 // poison shift by X -> poison
1357 // 0 shift by X -> 0
1359 return Constant::getNullValue(Op0->getType());
1361 // X shift by 0 -> X
1362 // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
1366 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1371 return PoisonValue::get(Op0->getType());
1374 // operating on either branch of the select always yields the same value.
1380 // operating on all incoming values of the phi always yields the same value.
1389 return PoisonValue::get(Op0->getType());
1409 return PoisonValue::get(Op0->getType());
1424 // X >> X -> 0
1426 return Constant::getNullValue(Op0->getType());
1428 // undef >> X -> 0
1429 // undef >> X -> undef (if it's exact)
1431 return IsExact ? Op0 : Constant::getNullValue(Op0->getType());
1452 Type *Ty = Op0->getType();
1453 // undef << X -> 0
1454 // undef << X -> undef if (if it's NSW/NUW)
1458 // (X >> A) << A -> X
1464 // shl nuw i8 C, %x -> C iff C has sign bit set.
1468 // but the cost-benefit analysis suggests it isn't worth it.
1471 // that the sign-bit does not change, so the only input that does not
1472 // produce poison is 0, and "0 << (bitwidth-1) --> 0".
1474 match(Op1, m_SpecificInt(Ty->getScalarSizeInBits() - 1)))
1493 // (X << A) >> A -> X
1498 // ((X << A) | Y) >> A -> X if effective width of Y is not larger than A.
1510 if (ShRAmt->uge(EffWidthY))
1530 // -1 >>a X --> -1
1531 // (-1 << X) a>> X --> -1
1532 // We could return the original -1 constant to preserve poison elements.
1535 return Constant::getAllOnesValue(Op0->getType());
1537 // (X << A) >> A -> X
1542 // Arithmetic shifting an all-sign-bit value is a no-op.
1544 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1570 // Y = (A - B);
1575 // A >=/<= B || (A - B) != 0 <--> true
1579 return ConstantInt::getTrue(UnsignedICmp->getType());
1580 // A </> B && (A - B) == 0 <--> false
1584 return ConstantInt::getFalse(UnsignedICmp->getType());
1586 // A </> B && (A - B) != 0 <--> A </> B
1587 // A </> B || (A - B) != 0 <--> (A - B) != 0
1592 // A <=/>= B && (A - B) == 0 <--> (A - B) == 0
1593 // A <=/>= B || (A - B) == 0 <--> A <=/>= B
1599 // Given Y = (A - B)
1600 // Y >= A && Y != 0 --> Y >= A iff B != 0
1601 // Y < A || Y == 0 --> Y < A iff B != 0
1623 // X > Y && Y == 0 --> Y == 0 iff X != 0
1624 // X > Y || Y == 0 --> X > Y iff X != 0
1629 // X <= Y && Y != 0 --> X <= Y iff X != 0
1630 // X <= Y || Y != 0 --> Y != 0 iff X != 0
1640 // X < Y && Y != 0 --> X < Y
1641 // X < Y || Y != 0 --> Y != 0
1645 // X >= Y && Y == 0 --> Y == 0
1646 // X >= Y || Y == 0 --> X >= Y
1650 // X < Y && Y == 0 --> false
1653 return getFalse(UnsignedICmp->getType());
1655 // X >= Y || Y != 0 --> true
1658 return getTrue(UnsignedICmp->getType());
1669 if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
1673 if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
1674 !match(Cmp1->getOperand(1), m_APInt(C1)))
1677 auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
1678 auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
1680 // For and-of-compares, check if the intersection is empty:
1681 // (icmp X, C0) && (icmp X, C1) --> empty set --> false
1683 return getFalse(Cmp0->getType());
1685 // For or-of-compares, check if the union is full:
1686 // (icmp X, C0) || (icmp X, C1) --> full set --> true
1688 return getTrue(Cmp0->getType());
1691 // If this is and-of-compares, take the smaller set:
1692 // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
1693 // If this is or-of-compares, take the larger set:
1694 // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
1715 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
1716 if (AddInst->getOperand(1) != Op1->getOperand(1))
1719 Type *ITy = Op0->getType();
1723 const APInt Delta = *C1 - *C0;
1724 if (C0->isStrictlyPositive()) {
1738 if (C0->getBoolValue() && IsNUW) {
1758 !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())) || C->isZero())
1761 // (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0
1764 // (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0
1806 auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1807 if (AddInst->getOperand(1) != Op1->getOperand(1))
1810 Type *ITy = Op0->getType();
1814 const APInt Delta = *C1 - *C0;
1815 if (C0->isStrictlyPositive()) {
1829 if (C0->getBoolValue() && IsNUW) {
1866 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1867 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1868 if (LHS0->getType() != RHS0->getType())
1871 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1875 // (fcmp ord X, 0) & (fcmp o** X, Y) --> fcmp o** X, Y
1876 // (fcmp uno X, 0) & (fcmp o** X, Y) --> false
1877 // (fcmp uno X, 0) | (fcmp u** X, Y) --> fcmp u** X, Y
1878 // (fcmp ord X, 0) | (fcmp u** X, Y) --> true
1882 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1888 // (fcmp o** X, Y) & (fcmp ord X, 0) --> fcmp o** X, Y
1889 // (fcmp o** X, Y) & (fcmp uno X, 0) --> false
1890 // (fcmp u** X, Y) | (fcmp uno X, 0) --> fcmp u** X, Y
1891 // (fcmp u** X, Y) | (fcmp ord X, 0) --> true
1895 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1906 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1907 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1908 Op0 = Cast0->getOperand(0);
1909 Op1 = Cast1->getOperand(0);
1932 return ConstantFoldCastOperand(Cast0->getOpcode(), C, Cast0->getType(),
1955 auto Simplify = [&](Value *Res) -> Value * {
1956 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, Res->getType());
1965 if (Res == ConstantExpr::getBinOpIdentity(Opcode, Res->getType()))
1980 // disable undef-based folds here.
1994 /// source value and inverted constant (identity: C - X -> ~(X + ~C)).
1997 assert(Op0->getType() == Op1->getType() && "Mismatched binop types");
2006 // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0
2007 // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1
2008 // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1
2009 Type *Ty = Op0->getType();
2023 return Constant::getNullValue(Op0->getType());
2029 // (X | ~Y) & (X | Y) --> X
2041 // -A & A = A if A is a power of two or zero.
2046 // This is a similar pattern used for checking if a value is a power-of-2:
2047 // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
2050 return Constant::getNullValue(Op1->getType());
2052 // (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and
2059 Shift1->uge(*Shift2))
2060 return Constant::getNullValue(Op0->getType());
2076 // X & poison -> poison
2080 // X & undef -> 0
2082 return Constant::getNullValue(Op0->getType());
2090 return Constant::getNullValue(Op0->getType());
2092 // X & -1 = X
2104 // A mask that only clears known zeros of a shifted value is a no-op.
2110 // and (shl X, ShAmt), Mask --> shl X, ShAmt
2116 // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
2122 // and 2^x-1, 2^C --> 0 where x <= C.
2130 // Use getActiveBits() to make use of the additional power of two knowledge
2131 if (PowerC->getActiveBits() >= Known.getMaxValue().getActiveBits())
2132 return ConstantInt::getNullValue(Op1->getType());
2143 // And distributes over Or. Try some generic simplifications based on this.
2148 // And distributes over Xor. Try some generic simplifications based on this.
2154 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2155 // A & (A && B) -> A && B
2162 // whether operating on either branch of the select always yields the same
2170 // operating on all incoming values of the phi always yields the same value.
2180 // ((X << A) | Y) & Mask -> Y,
2181 // if Mask = ((1 << effective_width_of(Y)) - 1)
2182 // ((X << A) | Y) & Mask -> X << A,
2183 // if Mask = ((1 << effective_width_of(X)) - 1) << A
2191 const unsigned Width = Op0->getType()->getScalarSizeInBits();
2192 const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
2209 // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
2210 // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
2216 return Constant::getNullValue(Op0->getType());
2220 // (A ^ C) & (A ^ ~C) -> 0
2223 return Constant::getNullValue(Op0->getType());
2225 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2232 return ConstantInt::getFalse(Op0->getType());
2240 return ConstantInt::getFalse(Op1->getType());
2256 assert(X->getType() == Y->getType() && "Expected same type for 'or' ops");
2257 Type *Ty = X->getType();
2259 // X | ~X --> -1
2263 // X | ~(X & ?) = -1
2267 // X | (X & ?) --> X
2273 // (A ^ B) | (A | B) --> A | B
2274 // (A ^ B) | (B | A) --> B | A
2279 // ~(A ^ B) | (A | B) --> -1
2280 // ~(A ^ B) | (B | A) --> -1
2285 // (A & ~B) | (A ^ B) --> A ^ B
2286 // (~B & A) | (A ^ B) --> A ^ B
2287 // (A & ~B) | (B ^ A) --> B ^ A
2288 // (~B & A) | (B ^ A) --> B ^ A
2293 // (~A ^ B) | (A & B) --> ~A ^ B
2294 // (B ^ ~A) | (A & B) --> B ^ ~A
2295 // (~A ^ B) | (B & A) --> ~A ^ B
2296 // (B ^ ~A) | (B & A) --> B ^ ~A
2301 // (~A | B) | (A ^ B) --> -1
2302 // (~A | B) | (B ^ A) --> -1
2303 // (B | ~A) | (A ^ B) --> -1
2304 // (B | ~A) | (B ^ A) --> -1
2309 // (~A & B) | ~(A | B) --> ~A
2310 // (~A & B) | ~(B | A) --> ~A
2311 // (B & ~A) | ~(A | B) --> ~A
2312 // (B & ~A) | ~(B | A) --> ~A
2326 // ~(A ^ B) | (A & B) --> ~(A ^ B)
2327 // ~(A ^ B) | (B & A) --> ~(A ^ B)
2334 // ~(A & B) | (A ^ B) --> ~(A & B)
2335 // ~(A & B) | (B ^ A) --> ~(A & B)
2351 // X | poison -> poison
2355 // X | undef -> -1
2356 // X | -1 = -1
2359 return Constant::getAllOnesValue(Op0->getType());
2374 // Rotated -1 is still -1:
2375 // (-1 << X) | (-1 >> (C - X)) --> -1
2376 // (-1 >> X) | (-1 << (C - X)) --> -1
2386 C->ule(X->getType()->getScalarSizeInBits())) {
2387 return ConstantInt::getAllOnesValue(X->getType());
2394 // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y
2395 // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y
2405 // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y
2406 // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y
2439 // Or distributes over And. Try some generic simplifications based on this.
2445 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2446 // A | (A || B) -> A || B
2453 // whether operating on either branch of the select always yields the same
2471 if (C2->isMask() && // C2 == 0+1+
2478 if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))) {
2487 // operating on all incoming values of the phi always yields the same value.
2492 // (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one.
2495 return Constant::getAllOnesValue(Op0->getType());
2497 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2505 return ConstantInt::getTrue(Op0->getType());
2514 return ConstantInt::getTrue(Op1->getType());
2535 // X ^ poison -> poison
2539 // A ^ undef -> undef
2549 return Constant::getNullValue(Op0->getType());
2551 // A ^ ~A = ~A ^ A = -1
2553 return Constant::getAllOnesValue(Op0->getType());
2555 auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
2557 // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
2562 // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
2563 // The 'not' op must contain a complete -1 operand (no undef elements for
2606 return CmpInst::makeCmpResultType(Op->getType());
2617 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2620 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2621 if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2623 if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2634 // live with the compared-to allocation). For globals, we exclude symbols
2635 // that might be resolve lazily to symbols in another dynamically-loaded
2638 return AI->isStaticAlloca();
2640 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2641 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2642 !GV->isThreadLocal();
2644 return A->hasByValAttr();
2654 // non-overlapping storage, but since their addresses are constants, the
2675 // So, we'll assume that two non-empty allocas have different addresses
2679 return A && A->hasByValAttr();
2715 // couldn't be a one-past-the-end value for a stack object in the caller and be
2723 assert(LHS->getType() == RHS->getType() && "Must have same types");
2727 // We can only fold certain predicates on pointer comparisons.
2737 // We can only handle unsigned relational comparisons because 'inbounds' on
2752 // numerous hazards. AliasAnalysis and its utilities rely on special rules
2756 // Even if an non-inbounds GEP occurs along the path we can still optimize
2759 unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
2761 LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
2762 RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);
2772 // Different non-empty allocations that exist at the same time have
2774 // within the bounds of their allocations (and not one-past-the-end!
2781 auto *F = [](Value *V) -> Function * {
2783 return I->getFunction();
2785 return A->getParent();
2791 APInt Dist = LHSOffset - RHSOffset;
2792 if (Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize))
2800 // come from a pointer that cannot overlap with dynamically-allocated
2824 // Fold comparisons for non-escaping pointer even if the allocation call
2827 // the other operand can not be based on the alloc - if it were, then
2843 if (auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2847 unsigned OtherIdx = 1 - U->getOperandNo();
2848 auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2849 if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2873 Type *OpTy = LHS->getType(); // The operand type.
2874 if (!OpTy->isIntOrIntVectorTy(1))
2881 auto ExtractNotLHS = [](Value *V) -> Value * {
2890 case CmpInst::ICMP_NE: // X != 0 -> X
2891 case CmpInst::ICMP_UGT: // X >u 0 -> X
2892 case CmpInst::ICMP_SLT: // X <s 0 -> X
2895 case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
2896 case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2897 case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2902 case CmpInst::ICMP_ULT: // X <u 0 -> false
2903 case CmpInst::ICMP_SGT: // X >s 0 -> false
2906 case CmpInst::ICMP_UGE: // X >=u 0 -> true
2907 case CmpInst::ICMP_SLE: // X <=s 0 -> true
2915 case CmpInst::ICMP_EQ: // X == 1 -> X
2916 case CmpInst::ICMP_UGE: // X >=u 1 -> X
2917 case CmpInst::ICMP_SLE: // X <=s -1 -> X
2920 case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
2921 case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
2922 case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
2927 case CmpInst::ICMP_UGT: // X >u 1 -> false
2928 case CmpInst::ICMP_SLT: // X <s -1 -> false
2931 case CmpInst::ICMP_ULE: // X <=u 1 -> true
2932 case CmpInst::ICMP_SGE: // X >=s -1 -> true
2948 /// For signed comparison, the values for an i1 are 0 and -1
2952 /// 0 | 1 | 1 (0 >= -1) | 1
2953 /// 1 | 0 | 0 (-1 >= 0) | 0
2954 /// 1 | 1 | 1 (-1 >= -1) | 1
3042 // Sign-bit checks can be optimized to true/false after unsigned
3043 // floating-point casts:
3044 // icmp slt (bitcast (uitofp X)), 0 --> false
3045 // icmp sgt (bitcast (uitofp X)), -1 --> true
3068 // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
3069 // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
3073 *MulC != 0 && C->urem(*MulC) != 0) ||
3075 *MulC != 0 && C->srem(*MulC) != 0)))
3151 // x >>u y <=u x --> true.
3152 // x >>u y >u x --> false.
3153 // x udiv y <=u x --> true.
3154 // x udiv y >u x --> false.
3165 // x >>u C <u x --> true for C != 0.
3166 // x >>u C != x --> true for C != 0.
3167 // x >>u C >=u x --> false for C != 0.
3168 // x >>u C == x --> false for C != 0.
3169 // x udiv C <u x --> true for C != 1.
3170 // x udiv C != x --> true for C != 1.
3171 // x udiv C >=u x --> false for C != 1.
3172 // x udiv C == x --> false for C != 1.
3173 // TODO: allow non-constant shift amount/divisor
3198 // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3206 C1->ule(*C2)) ||
3208 C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
3210 (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
3217 // (sub C, X) == X, C is odd --> false
3218 // (sub C, X) != X, C is odd --> true
3256 return (C1->slt(*C2) && C1->isNonNegative()) ||
3257 (C2->slt(*C1) && C1->isNonPositive());
3273 if (LBO && LBO->getOpcode() == Instruction::Add) {
3274 A = LBO->getOperand(0);
3275 B = LBO->getOperand(1);
3283 if (RBO && RBO->getOpcode() == Instruction::Add) {
3284 C = RBO->getOperand(0);
3285 D = RBO->getOperand(1);
3294 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3297 Constant::getNullValue(RHS->getType()), Q,
3298 MaxRecurse - 1))
3301 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3304 simplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()),
3305 C == LHS ? D : C, Q, MaxRecurse - 1))
3308 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3315 // C + B == C + D -> B == D
3319 // D + B == C + D -> B == C
3323 // A + C == C + D -> A == D
3328 // A + D == C + D -> A == C
3332 if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
3346 // 0 - (zext X) pred C
3350 if (C->isStrictlyPositive()) {
3356 if (C->isNonNegative()) {
3365 // If C2 is a power-of-2 and C is not:
3366 // (C2 << X) == C --> false
3367 // (C2 << X) != C --> true
3370 match(RHS, m_APIntAllowPoison(C)) && !C->isPowerOf2()) {
3375 // - The shift is nsw. We can't shift out the one bit.
3376 // - The shift is nuw. We can't shift out the one bit.
3377 // - C2 is one.
3378 // - C isn't zero.
3381 match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
3389 // If C is a power-of-2:
3390 // (C << X) >u 0x8000 --> false
3391 // (C << X) <=u 0x8000 --> true
3399 if (!MaxRecurse || !LBO || !RBO || LBO->getOpcode() != RBO->getOpcode())
3402 if (LBO->getOperand(0) == RBO->getOperand(0)) {
3403 switch (LBO->getOpcode()) {
3410 !isKnownNonZero(LBO->getOperand(0), Q))
3412 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1),
3413 RBO->getOperand(1), Q, MaxRecurse - 1))
3418 // icmp ule A, B -> true
3419 // icmp ugt A, B -> false
3420 // icmp sle A, B -> true (C1 and C2 are the same sign)
3421 // icmp sgt A, B -> false (C1 and C2 are the same sign)
3426 match(LBO->getOperand(1), m_APInt(C1)) &&
3427 match(RBO->getOperand(1), m_APInt(C2))) {
3428 if (!C1->isSubsetOf(*C2)) {
3432 if (C1->isSubsetOf(*C2)) {
3437 if (C1->isNonNegative() == C2->isNonNegative()) {
3450 if (LBO->getOperand(1) == RBO->getOperand(1)) {
3451 switch (LBO->getOpcode()) {
3459 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3460 RBO->getOperand(0), Q, MaxRecurse - 1))
3467 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3468 RBO->getOperand(0), Q, MaxRecurse - 1))
3474 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3475 RBO->getOperand(0), Q, MaxRecurse - 1))
3485 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3486 RBO->getOperand(0), Q, MaxRecurse - 1))
3505 // Signed variants on "max(a,b)>=a -> true".
3517 // We analyze this as smax(A, B) swapped-pred A.
3524 // We analyze this as smax(-A, -B) swapped-pred -A.
3525 // Note that we do not need to actually form -A or -B thanks to EqP.
3532 // We analyze this as smax(-A, -B) pred -A.
3533 // Note that we do not need to actually form -A or -B thanks to EqP.
3551 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3565 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3578 // Unsigned variants on "max(a,b)>=a -> true".
3591 // We analyze this as umax(A, B) swapped-pred A.
3598 // We analyze this as umax(-A, -B) swapped-pred -A.
3599 // Note that we do not need to actually form -A or -B thanks to EqP.
3606 // We analyze this as umax(-A, -B) pred -A.
3607 // Note that we do not need to actually form -A or -B thanks to EqP.
3625 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3639 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3662 // smax(A, B) >=s smin(A, D) --> true
3665 // smax(A, B) <s smin(A, D) --> false
3671 // umax(A, B) >=u umin(A, D) --> true
3674 // umax(A, B) <u umin(A, D) --> false
3690 for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
3696 Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL))
3711 switch (II->getIntrinsicID()) {
3714 if (II->getArgOperand(0) == RHS || II->getArgOperand(1) == RHS) {
3723 if (II->getArgOperand(0) == RHS) {
3743 return A->getRange();
3745 return CB->getRange();
3761 // If we have a constant, make sure it is on the RHS.
3769 // icmp poison, X -> poison
3779 // icmp X, X -> true/false
3780 // icmp X, undef -> true/false because undef could be X.
3799 if (LhsCr->icmp(Pred, *RhsCr))
3802 if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr))
3806 // Compare of cast, for example (zext X) != 0 -> X != 0
3809 Value *SrcOp = LI->getOperand(0);
3810 Type *SrcTy = SrcOp->getType();
3811 Type *DstTy = LI->getType();
3816 Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3821 Q, MaxRecurse - 1))
3824 if (RI->getOperand(0)->getType() == SrcTy)
3826 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3827 MaxRecurse - 1))
3836 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3840 RI->getOperand(0), Q, MaxRecurse - 1))
3845 if (SrcOp == RI->getOperand(0)) {
3862 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3865 assert(RExt && "Constant-fold of ImmConstant should not fail");
3868 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3870 // If the re-extended constant didn't change any of the elements then
3871 // this is effectively also a case of comparing two zero-extended
3873 if (AnyEq->isAllOnesValue() && MaxRecurse)
3875 SrcOp, Trunc, Q, MaxRecurse - 1))
3878 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3880 if (AnyEq->isNullValue()) {
3895 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
3896 // is non-negative then LHS <s RHS.
3900 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3905 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3916 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3918 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3919 MaxRecurse - 1))
3924 if (SrcOp == RI->getOperand(0)) {
3940 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3943 assert(RExt && "Constant-fold of ImmConstant should not fail");
3946 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3948 // If the re-extended constant didn't change then this is effectively
3949 // also a case of comparing two sign-extended values.
3950 if (AnyEq->isAllOnesValue() && MaxRecurse)
3952 simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1))
3957 if (AnyEq->isNullValue()) {
3966 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
3971 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3976 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3979 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
3987 MaxRecurse - 1))
3996 MaxRecurse - 1))
4005 // icmp eq|ne X, Y -> false|true if X != Y
4007 // compares with 0 above here, so only try this for a non-zero compare.
4033 // GEP-walk when we have target data available..
4034 if (LHS->getType()->isPointerTy())
4039 if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4040 Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4041 Q.DL.getTypeSizeInBits(CLHS->getType()))
4042 if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
4043 CRHS->getPointerOperand(), Q))
4079 // If we have a constant, make sure it is on the RHS.
4104 // fcmp x,x -> true/false. Not all compares are foldable.
4115 // class-like compare.
4149 const Function *ParentF = Q.CxtI->getFunction();
4153 if ((FullKnownClassLHS->KnownFPClasses & ClassTest) == fcNone)
4155 if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) == fcNone)
4164 if (C->isNaN())
4170 if (C->isNegative() && !C->isNegZero()) {
4174 // relying on CannotBeOrderedLessThanZero.
4207 cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
4213 // minnum(X, LesserC) == C --> false
4214 // maxnum(X, GreaterC) == C --> false
4218 // minnum(X, LesserC) != C --> true
4219 // maxnum(X, GreaterC) != C --> true
4225 // minnum(X, LesserC) >= C --> false
4226 // minnum(X, LesserC) > C --> false
4227 // maxnum(X, GreaterC) >= C --> true
4228 // maxnum(X, GreaterC) > C --> true
4234 // minnum(X, LesserC) <= C --> true
4235 // minnum(X, LesserC) < C --> true
4236 // maxnum(X, GreaterC) <= C --> false
4237 // maxnum(X, GreaterC) < C --> false
4247 // classes in a non-splat vector.
4258 // Positive or zero X >= 0.0 --> true
4259 // Positive or zero X < 0.0 --> false
4270 // Positive or zero or nan X >= 0.0 --> true
4271 // Positive or zero or nan X < 0.0 --> false
4313 if (!MaxRecurse--)
4329 if (Op->getType()->isVectorTy()) {
4330 // For vector types, the simplification must hold per-lane, so forbid
4331 // potentially cross-lane operations like shufflevector.
4332 if (!I->getType()->isVectorTy() || isa<ShuffleVectorInst>(I) ||
4337 // Don't fold away llvm.is.constant checks based on assumptions.
4348 for (Value *InstOp : I->operands()) {
4369 // a few non-refining but profitable transforms here.
4372 unsigned Opcode = BO->getOpcode();
4373 // id op x -> x, x op id -> x
4374 if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType()))
4376 if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(),
4380 // x & x -> x, x | x -> x
4385 if (PDI->isDisjoint()) {
4388 DropFlags->push_back(BO);
4394 // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison
4399 return Constant::getNullValue(I->getType());
4402 // poison can't leak if we remove the select -- because both operands of
4403 // the binop are based on the same value -- then it may be safe to replace
4405 // (Op == 0) ? 0 : (Op & -Op) --> Op & -Op
4406 // (Op == 0) ? 0 : (Op * (binop Op, C)) --> Op * (binop Op, C)
4407 // (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op)
4409 ConstantExpr::getBinOpAbsorber(Opcode, I->getType());
4416 // getelementptr x, 0 -> x.
4426 // %sel = select i1 %cmp, i32 %div, i32 undef
4452 // %sel = select i1 %cmp, i32 -2147483648, i32 %add
4454 // We can't replace %sel with %add unless we strip away the flags (which
4462 II && II->getIntrinsicID() == Intrinsic::abs) {
4463 if (!ConstOps[0]->isNotMinSignedValue())
4469 if (DropFlags && Res && I->hasPoisonGeneratingAnnotations())
4470 DropFlags->push_back(I);
4496 // (X & Y) == 0 ? X & ~Y : X --> X
4497 // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
4502 // (X & Y) == 0 ? X : X & ~Y --> X & ~Y
4503 // (X & Y) != 0 ? X : X & ~Y --> X
4508 if (Y->isPowerOf2()) {
4509 // (X & Y) == 0 ? X | Y : X --> X | Y
4510 // (X & Y) != 0 ? X | Y : X --> X
4514 if (TrueWhenUnset && cast<PossiblyDisjointInst>(TrueVal)->isDisjoint())
4519 // (X & Y) == 0 ? X : X | Y --> X
4520 // (X & Y) != 0 ? X : X | Y --> X | Y
4524 if (!TrueWhenUnset && cast<PossiblyDisjointInst>(FalseVal)->isDisjoint())
4536 // Canonicalize common cmp+sel operand as CmpLHS.
4542 // Canonicalize common cmp+sel operand as TVal.
4549 // based on the max/min/select relationship.
4553 if (Shuf && Shuf->isSelect()) {
4554 if (Shuf->getOperand(0) == Y)
4555 FVal = Shuf->getOperand(1);
4556 else if (Shuf->getOperand(1) == Y)
4557 FVal = Shuf->getOperand(0);
4569 // (X > Y) ? X : max(X, Y) --> max(X, Y)
4570 // (X >= Y) ? X : max(X, Y) --> max(X, Y)
4571 // (X < Y) ? X : min(X, Y) --> min(X, Y)
4572 // (X <= Y) ? X : min(X, Y) --> min(X, Y)
4577 // (X > Y) ? X : Y --> max(X, Y)
4578 ICmpInst::Predicate MMPred = MMI->getPredicate();
4586 // (X == Y) ? X : max/min(X, Y) --> max/min(X, Y)
4590 // (X != Y) ? X : max/min(X, Y) --> X
4594 // (X < Y) ? X : max(X, Y) --> X
4595 // (X <= Y) ? X : max(X, Y) --> X
4596 // (X > Y) ? X : min(X, Y) --> X
4597 // (X >= Y) ? X : min(X, Y) --> X
4658 // X > MIN_INT ? X : MIN_INT --> X
4659 // X < MAX_INT ? X : MAX_INT --> X
4660 if (TrueVal->getType()->isIntOrIntVectorTy()) {
4668 X->getType()->getScalarSizeInBits());
4682 // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
4686 // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
4687 // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
4691 // Test for a zero-shift-guard-op around rotates. These are used to
4699 // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4700 // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4705 // X == 0 ? abs(X) : -abs(X) --> -abs(X)
4706 // X == 0 ? -abs(X) : abs(X) --> abs(X)
4734 // select((X | Y) == 0 ? X : 0) --> 0 (commuted 2 ways)
4746 // select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
4749 // (X & Y) == -1 implies X == -1 and Y == -1.
4763 /// floating-point comparison.
4771 // This transform is safe if we do not have (do not care about) -0.0 or if
4772 // at least one operand is known to not be -0.0. Otherwise, the select can
4775 Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros();
4777 if (HasNoSignedZeros || (match(T, m_APFloat(C)) && C->isNonZero()) ||
4778 (match(F, m_APFloat(C)) && C->isNonZero())) {
4779 // (T == F) ? T : F --> F
4780 // (F == T) ? T : F --> F
4784 // (T != F) ? T : F --> T
4785 // (F != T) ? T : F --> T
4803 // select poison, X, Y -> poison
4805 return PoisonValue::get(TrueVal->getType());
4807 // select undef, X, Y -> X or Y
4811 // select true, X, Y --> X
4812 // select false, X, Y --> Y
4821 assert(Cond->getType()->isIntOrIntVectorTy(1) &&
4823 assert(TrueVal->getType() == FalseVal->getType() &&
4826 if (Cond->getType() == TrueVal->getType()) {
4827 // select i1 Cond, i1 true, i1 false --> i1 Cond
4831 // (X && Y) ? X : Y --> Y (commuted 2 ways)
4835 // (X || Y) ? X : Y --> X (commuted 2 ways)
4839 // (X || Y) ? false : X --> false (commuted 2 ways)
4842 return ConstantInt::getFalse(Cond->getType());
4844 // Match patterns that end in logical-and.
4846 // !(X || Y) && X --> false (commuted 2 ways)
4848 return ConstantInt::getFalse(Cond->getType());
4849 // X && !(X || Y) --> false (commuted 2 ways)
4851 return ConstantInt::getFalse(Cond->getType());
4853 // (X || Y) && Y --> Y (commuted 2 ways)
4856 // Y && (X || Y) --> Y (commuted 2 ways)
4860 // (X || Y) && (X || !Y) --> X (commuted 8 ways)
4870 // Match patterns that end in logical-or.
4872 // !(X && Y) || X --> true (commuted 2 ways)
4874 return ConstantInt::getTrue(Cond->getType());
4875 // X || !(X && Y) --> true (commuted 2 ways)
4877 return ConstantInt::getTrue(Cond->getType());
4879 // (X && Y) || Y --> Y (commuted 2 ways)
4882 // Y || (X && Y) --> Y (commuted 2 ways)
4888 // select ?, X, X -> X
4893 // select i1 X, i1 X, i1 false --> X (logical-and)
4896 // select i1 X, i1 X, i1 true --> true
4898 return ConstantInt::getTrue(Cond->getType());
4901 // select i1 X, i1 true, i1 X --> X (logical-or)
4904 // select i1 X, i1 false, i1 X --> false
4906 return ConstantInt::getFalse(Cond->getType());
4912 // select ?, poison, X -> X
4913 // select ?, undef, X -> X
4917 // select ?, X, poison -> X
4918 // select ?, X, undef -> X
4923 // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
4925 if (isa<FixedVectorType>(TrueVal->getType()) &&
4929 cast<FixedVectorType>(TrueC->getType())->getNumElements();
4932 // Bail out on incomplete vector constants.
4933 Constant *TEltC = TrueC->getAggregateElement(i);
4934 Constant *FEltC = FalseC->getAggregateElement(i);
4984 cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace();
4986 // getelementptr P -> P.
4992 Type *GEPTy = Ptr->getType();
4993 if (!GEPTy->isVectorTy()) {
4997 if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) {
4998 GEPTy = VectorType::get(GEPTy, VT->getElementCount());
5004 // All-zero GEP is a no-op, unless it performs a vector splat.
5005 if (Ptr->getType() == GEPTy &&
5009 // getelementptr poison, idx -> poison
5010 // getelementptr baseptr, poison -> poison
5015 // getelementptr undef, idx -> undef
5020 SrcTy->isScalableTy() || any_of(Indices, [](const Value *V) {
5021 return isa<ScalableVectorType>(V->getType());
5026 if (!IsScalableVec && Ty->isSized()) {
5030 // getelementptr P, N -> P if P points to a type of zero size.
5031 if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
5036 if (Indices[0]->getType()->getScalarSizeInBits() ==
5038 auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
5039 return P->getType() == GEPTy &&
5042 // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
5049 // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of
5057 // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of
5072 Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
5073 if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
5076 Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset);
5082 // gep (gep V, C), (sub 0, V) -> C
5086 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
5089 // gep (gep V, C), (xor V, -1) -> C-1
5093 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
5127 // insertvalue x, poison, n -> x
5128 // insertvalue x, undef, n -> x if x cannot be poison
5135 if (EV->getAggregateOperand()->getType() == Agg->getType() &&
5136 EV->getIndices() == Idxs) {
5137 // insertvalue poison, (extractvalue y, n), n -> y
5138 // insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison
5141 isGuaranteedNotToBePoison(EV->getAggregateOperand())))
5142 return EV->getAggregateOperand();
5144 // insertvalue y, (extractvalue y, n), n -> y
5145 if (Agg == EV->getAggregateOperand())
5167 // For fixed-length vector, fold into poison if index is out of bounds.
5169 if (isa<FixedVectorType>(Vec->getType()) &&
5170 CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements()))
5171 return PoisonValue::get(Vec->getType());
5176 return PoisonValue::get(Vec->getType());
5186 // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
5200 // extractvalue x, (insertvalue y, elt, n), n -> elt
5203 IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
5204 ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
5210 return IVI->getInsertedValueOperand();
5227 auto *VecVTy = cast<VectorType>(Vec->getType());
5233 return UndefValue::get(VecVTy->getElementType());
5236 // An undef extract index can be arbitrarily chosen to be an out-of-range
5239 return PoisonValue::get(VecVTy->getElementType());
5244 // For fixed-length vector, fold into undef if index is out of bounds.
5245 unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
5246 if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
5247 return PoisonValue::get(VecVTy->getElementType());
5249 if (IdxC->getValue().ult(MinNumElts))
5252 if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
5255 // extractelt x, (insertelt y, elt, n), n -> elt
5256 // If the possibly-variable indices are trivially known to be equal
5260 if (IE && IE->getOperand(2) == Idx)
5261 return IE->getOperand(1);
5280 // def-reachable from the original PHI!
5296 // Remember that we saw an undef value, but otherwise ignore them.
5308 return HasUndefInput ? UndefValue::get(PN->getType())
5309 : PoisonValue::get(PN->getType());
5327 auto *Src = CI->getOperand(0);
5328 Type *SrcTy = Src->getType();
5329 Type *MidTy = CI->getType();
5331 if (Src->getType() == Ty) {
5332 auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
5335 SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
5337 MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
5339 DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
5347 // bitcast x -> x
5349 if (Op->getType() == Ty)
5352 // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
5357 X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
5374 if (!MaxRecurse--)
5378 // simplified further based on demanded bits or other folds.
5379 if (MaskVal == -1)
5383 int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
5387 RootElt = MaskVal - InVecNumElts;
5395 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
5396 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
5423 auto *InVecTy = cast<VectorType>(Op0->getType());
5425 ElementCount InVecEltCount = InVecTy->getElementCount();
5438 if (Indices[i] == -1)
5455 // transformation depends on the value of the mask which is not known at
5461 // second one. This transformation depends on the value of the mask which
5470 // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
5473 // NOTE: This transformation depends on the value of the mask which is not
5480 int InsertIndex = IndexC->getZExtValue();
5482 return MaskElt == InsertIndex || MaskElt == -1;
5489 if (Indices[i] == -1)
5490 VecC[i] = PoisonValue::get(C->getType());
5499 all_equal(OpShuf->getShuffleMask()))
5502 // All remaining transformation depend on the value of the mask, which is
5510 if (is_contained(Indices, -1))
5525 if (!RootVec || RootVec->getType() != RetTy)
5568 Type *Ty = In->getType();
5570 unsigned NumElts = VecTy->getNumElements();
5573 Constant *EltC = In->getAggregateElement(i);
5578 else if (EltC && EltC->isNaN())
5580 EltC->getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet());
5582 NewC[i] = ConstantFP::getNaN(VecTy->getElementType());
5589 if (!In->isNaN())
5593 // on our hands. Grab that before splatting a QNaN constant.
5595 auto *Splat = In->getSplatValue();
5596 assert(Splat && Splat->isNaN() &&
5597 "Found a scalable-vector NaN but not a splat");
5603 return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet());
5606 /// Perform folds that are common to any floating-point operation. This implies
5607 /// transforms based on poison/undef/NaN because the operation itself makes no
5616 return PoisonValue::get(Ops[0]->getType());
5627 return PoisonValue::get(V->getType());
5629 return PoisonValue::get(V->getType());
5632 // Undef does not propagate because undef means that all bits can take on
5637 return ConstantFP::getNaN(V->getType());
5662 // fadd X, -0 ==> X
5665 // fadd SNaN, -0.0 --> QNaN
5666 // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
5673 // fadd X, 0 ==> X, when we know X is not -0
5683 // With nnan: X + {+/-}Inf --> {+/-}Inf
5687 // With nnan: -X + X --> 0.0 (and commuted variant)
5688 // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
5690 // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5691 // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5692 // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
5693 // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
5696 return ConstantFP::getZero(Op0->getType());
5700 return ConstantFP::getZero(Op0->getType());
5703 // (X - Y) + Y --> X
5704 // Y + (X - Y) --> X
5735 // fsub X, -0 ==> X, when we know X is not -0
5741 // fsub -0.0, (fsub -0.0, X) ==> X
5742 // fsub -0.0, (fneg X) ==> X
5762 return Constant::getNullValue(Op0->getType());
5764 // With nnan: {+/-}Inf - X --> {+/-}Inf
5768 // With nnan: X - {+/-}Inf --> {-/+}Inf
5773 // Y - (Y - X) --> X
5774 // (X + Y) - Y --> X
5797 // X * 1.0 --> X
5802 // X * 0.0 --> 0.0 (with nnan and nsz)
5804 return ConstantFP::getZero(Op0->getType());
5809 // +normal number * (-)0.0 --> (-)0.0
5812 // -normal number * (-)0.0 --> -(-)0.0
5818 // sqrt(X) * sqrt(X) --> X, if we can:
5820 // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
5821 // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
5891 // X / 1.0 -> X
5895 // 0 / X -> 0
5899 return ConstantFP::getZero(Op0->getType());
5902 // X / X -> 1.0 is legal when NaNs are ignored.
5903 // We can ignore infinities because INF/INF is NaN.
5905 return ConstantFP::get(Op0->getType(), 1.0);
5907 // (X * Y) / Y --> X if we can reassociate to the above form.
5912 // -X / X -> -1.0 and
5913 // X / -X -> -1.0 are legal when NaNs are ignored.
5914 // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
5917 return ConstantFP::get(Op0->getType(), -1.0);
5919 // nnan ninf X / [-]0.0 -> poison
5921 return PoisonValue::get(Op1->getType());
5954 // +0 % X -> 0
5956 return ConstantFP::getZero(Op0->getType());
5957 // -0 % X -> -0
5959 return ConstantFP::getNegativeZero(Op0->getType());
6123 /// Return true if the intrinsic rounds a floating-point value to an integral
6124 /// floating-point value (not an integer type).
6148 Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
6151 if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
6154 APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc(
6155 DL.getIndexTypeSizeInBits(Ptr->getType()));
6168 if (LoadedCE->getOpcode() == Instruction::Trunc) {
6169 LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6174 if (LoadedCE->getOpcode() != Instruction::Sub)
6177 auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6178 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
6180 auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
6182 Constant *LoadedRHS = LoadedCE->getOperand(1);
6196 // ldexp(poison, x) -> poison
6197 // ldexp(x, poison) -> poison
6201 // ldexp(undef, x) -> nan
6203 return ConstantFP::getNaN(Op0->getType());
6208 // ldexp(x, undef) -> x
6217 // ldexp(0.0, x) -> 0.0
6218 // ldexp(-0.0, x) -> -0.0
6219 // ldexp(inf, x) -> inf
6220 // ldexp(-inf, x) -> -inf
6221 if (C && (C->isZero() || C->isInfinity()))
6225 // ignore denormal flushes and target handling of nan payload bits.
6230 if (C && C->isNaN())
6231 return ConstantFP::get(Op0->getType(), C->makeQuiet());
6233 // ldexp(x, 0) -> x
6247 Intrinsic::ID IID = F->getIntrinsicID();
6250 if (II->getIntrinsicID() == IID)
6257 // floor (sitofp x) -> sitofp x
6258 // round (ceil x) -> ceil x
6260 if ((II && removesFPFraction(II->getIntrinsicID())) ||
6272 // bswap(bswap(x)) -> x
6277 // bitreverse(bitreverse(x)) -> x
6282 // ctpop(X) -> 1 iff X is non-zero power of 2.
6285 return ConstantInt::get(Op0->getType(), 1);
6286 // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
6287 // ctpop(and X, 1) --> and X, 1
6288 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6289 if (MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, BitWidth - 1),
6295 // exp(log(x)) -> x
6296 if (Call->hasAllowReassoc() &&
6301 // exp2(log2(x)) -> x
6302 if (Call->hasAllowReassoc() &&
6307 // exp10(log10(x)) -> x
6308 if (Call->hasAllowReassoc() &&
6313 // log(exp(x)) -> x
6314 if (Call->hasAllowReassoc() &&
6319 // log2(exp2(x)) -> x
6320 if (Call->hasAllowReassoc() &&
6327 // log10(pow(10.0, x)) -> x
6328 // log10(exp10(x)) -> x
6329 if (Call->hasAllowReassoc() &&
6336 // vector.reverse(vector.reverse(x)) -> x
6339 // vector.reverse(splat(X)) -> splat(X)
6359 /// Given a min/max intrinsic, see if it can be removed based on having an
6370 Intrinsic::ID IID0 = MM0->getIntrinsicID();
6374 // max (max X, Y), X --> max X, Y
6377 // max (min X, Y), X --> X
6384 /// Given a min/max intrinsic, see if it can be removed based on having an
6397 if (!M0 || M0->getIntrinsicID() != IID)
6399 Value *X0 = M0->getOperand(0);
6400 Value *Y0 = M0->getOperand(1);
6413 Value *X1 = M1->getOperand(0);
6414 Value *Y1 = M1->getOperand(1);
6415 Intrinsic::ID IID1 = M1->getIntrinsicID();
6433 unsigned BitWidth = ReturnType->getScalarSizeInBits();
6436 // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
6438 // on the outer abs.
6459 return PoisonValue::get(Op0->getType());
6461 // NOTE: We can't apply this simplifications based on the value of Op1
6464 return Constant::getNullValue(Op0->getType());
6466 assert(Op1->getType()->getScalarSizeInBits() ==
6467 Q.DL.getIndexTypeSizeInBits(Op0->getType()) &&
6469 // If index-width (mask size) is less than pointer-size then mask is
6470 // 1-extended.
6486 PtrKnown.Zero.zextOrTrunc(C->getType()->getScalarSizeInBits());
6488 Instruction::Or, C, ConstantInt::get(C->getType(), IrrelevantPtrBits),
6490 if (C != nullptr && C->isAllOnesValue())
6499 // If the arguments are the same, this is a no-op.
6515 // umax(i8 %x, i8 255) --> 255
6521 // umin(i8 %x, i8 255) --> %x
6527 // max (max X, 7), 5 -> max X, 7
6529 if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
6531 Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
6570 return ConstantInt::getSigned(ReturnType, -1);
6576 // X - X -> { 0, false }
6577 // X - undef -> { 0, false }
6578 // undef - X -> { 0, false }
6584 // X + undef -> { -1, false }
6585 // undef + x -> { -1, false }
6589 {Constant::getAllOnesValue(ReturnType->getStructElementType(0)),
6590 Constant::getNullValue(ReturnType->getStructElementType(1))});
6595 // 0 * X -> { 0, false }
6596 // X * 0 -> { 0, false }
6599 // undef * X -> { 0, false }
6600 // X * undef -> { 0, false }
6605 // sat(MAX + X) -> MAX
6606 // sat(X + MAX) -> MAX
6611 // sat(X + undef) -> -1
6612 // sat(undef + X) -> -1
6613 // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
6614 // For signed: Assume undef is ~X, in which case X + ~X = -1.
6618 // X + 0 -> X
6621 // 0 + X -> X
6626 // sat(0 - X) -> 0, sat(X - MAX) -> 0
6631 // X - X -> 0, X - undef -> 0, undef - X -> 0
6634 // X - 0 -> X
6644 if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
6645 // powi(x, 0) -> 1.0
6646 if (Power->isZero())
6647 return ConstantFP::get(Op0->getType(), 1.0);
6648 // powi(x, 1) -> x
6649 if (Power->isOne())
6656 // copysign X, X --> X
6659 // copysign -X, X --> X
6660 // copysign X, -X --> -X
6669 uint64_t Mask = cast<ConstantInt>(Op1)->getZExtValue();
6683 // If the arguments are the same, this is a no-op.
6698 // minnum(X, nan) -> X
6699 // maxnum(X, nan) -> X
6700 // minimum(X, nan) -> nan
6701 // maximum(X, nan) -> nan
6709 (C->isInfinity() || (Call && Call->hasNoInfs() && C->isLargest()))) {
6710 // minnum(X, -inf) -> -inf
6711 // maxnum(X, +inf) -> +inf
6712 // minimum(X, -inf) -> -inf if nnan
6713 // maximum(X, +inf) -> +inf if nnan
6714 if (C->isNegative() == IsMin &&
6715 (!PropagateNaN || (Call && Call->hasNoNaNs())))
6718 // minnum(X, +inf) -> X if nnan
6719 // maxnum(X, -inf) -> X if nnan
6720 // minimum(X, +inf) -> X
6721 // maximum(X, -inf) -> X
6722 if (C->isNegative() != IsMin &&
6723 (PropagateNaN || (Call && Call->hasNoNaNs())))
6728 // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
6737 // (extract_vector (insert_vector _, X, 0), 0) -> X
6738 unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue();
6742 IdxN == 0 && X->getType() == ReturnType)
6758 assert(Call->arg_size() == Args.size());
6761 Intrinsic::ID IID = F->getIntrinsicID();
6768 Type *RetTy = F->getReturnType();
6769 ConstantRange CR = getVScaleRange(Call->getFunction(), 64);
6771 return ConstantInt::get(RetTy, C->getZExtValue());
6783 return simplifyBinaryIntrinsic(IID, F->getReturnType(), Args[0], Args[1], Q,
6803 return UndefValue::get(F->getReturnType());
6812 APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth());
6813 if (ShAmtC->urem(BitWidth).isZero())
6819 return ConstantInt::getNullValue(F->getReturnType());
6821 // Rotating -1 by anything is -1.
6823 return ConstantInt::getAllOnesValue(F->getReturnType());
6829 if (Value *V = simplifyFPOp(Args, {}, Q, *FPI->getExceptionBehavior(),
6830 *FPI->getRoundingMode()))
6846 Type *ReturnType = F->getReturnType();
6854 // X * 0 -> 0
6858 // X * undef -> 0
6862 // X * (1 << Scale) -> X
6864 APInt::getOneBitSet(ReturnType->getScalarSizeInBits(),
6865 cast<ConstantInt>(Op2)->getZExtValue());
6875 Type *ReturnType = F->getReturnType();
6877 // (insert_vector Y, (extract_vector X, 0), 0) -> X
6879 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
6884 X->getType() == ReturnType)
6891 return simplifyFAddInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6892 *FPI->getExceptionBehavior(),
6893 *FPI->getRoundingMode());
6897 return simplifyFSubInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6898 *FPI->getExceptionBehavior(),
6899 *FPI->getRoundingMode());
6903 return simplifyFMulInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6904 *FPI->getExceptionBehavior(),
6905 *FPI->getRoundingMode());
6909 return simplifyFDivInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6910 *FPI->getExceptionBehavior(),
6911 *FPI->getRoundingMode());
6915 return simplifyFRemInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6916 *FPI->getExceptionBehavior(),
6917 *FPI->getRoundingMode());
6936 // Use null-pointer of gc_relocate's type to replace it.
6972 assert(Call->arg_size() == Args.size());
6976 if (Call->isMustTailCall())
6979 // call undef -> poison
6980 // call null -> poison
6982 return PoisonValue::get(Call->getType());
6988 if (F && F->isIntrinsic())
6997 SmallVector<Value *, 4> Args(Call->args());
6998 if (Value *V = tryConstantFoldCall(Call, Call->getCalledOperand(), Args, Q))
7000 if (Value *Ret = simplifyIntrinsic(Call, Call->getCalledOperand(), Args, Q))
7020 if (LI->isVolatile())
7024 return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Q.DL);
7029 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7034 if (Constant *C = ConstantFoldLoadFromUniformValue(GV->getInitializer(),
7035 LI->getType(), Q.DL))
7040 APInt Offset(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
7041 PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
7046 Offset = Offset.sextOrTrunc(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()));
7047 return ConstantFoldLoadFromConstPtr(GV, LI->getType(), std::move(Offset),
7061 assert(I->getFunction() && "instruction should be inserted in a function");
7062 assert((!SQ.CxtI || SQ.CxtI->getFunction() == I->getFunction()) &&
7067 switch (I->getOpcode()) {
7077 return simplifyFNegInst(NewOps[0], I->getFastMathFlags(), Q, MaxRecurse);
7079 return simplifyFAddInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7086 return simplifyFSubInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7093 return simplifyFMulInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7108 return simplifyFDivInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7115 return simplifyFRemInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7136 return simplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), NewOps[0],
7139 return simplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), NewOps[0],
7140 NewOps[1], I->getFastMathFlags(), Q, MaxRecurse);
7146 return simplifyGEPInst(GEPI->getSourceElementType(), NewOps[0],
7147 ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q,
7152 return simplifyInsertValueInst(NewOps[0], NewOps[1], IV->getIndices(), Q,
7159 return simplifyExtractValueInst(NewOps[0], EVI->getIndices(), Q,
7167 SVI->getShuffleMask(), SVI->getType(), Q,
7175 NewOps.drop_back(1 + cast<CallInst>(I)->getNumTotalBundleOperands()), Q);
7181 return simplifyCastInst(I->getOpcode(), NewOps[0], I->getType(), Q,
7194 assert(NewOps.size() == I->getNumOperands() &&
7200 SmallVector<Value *, 8> Ops(I->operands());
7203 /// If called on unreachable code, the instruction may simplify to itself.
7206 return Result == I ? PoisonValue::get(I->getType()) : Result;
7213 /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
7228 const DataLayout &DL = I->getDataLayout();
7233 for (User *U : I->users())
7238 I->replaceAllUsesWith(SimpleV);
7240 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7241 I->eraseFromParent();
7246 // Note that we must test the size on each iteration, the worklist can grow.
7254 UnsimplifiedUsers->insert(I);
7262 // uses of To on the recursive step in most cases.
7263 for (User *U : I->users())
7267 I->replaceAllUsesWith(SimpleV);
7269 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7270 I->eraseFromParent();
7288 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
7290 auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
7292 auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;