Lines Matching defs:RHS
21 static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS,
24 APInt PossibleSumZero = LHS.getMaxValue() + RHS.getMaxValue() + !CarryZero;
25 APInt PossibleSumOne = LHS.getMinValue() + RHS.getMinValue() + CarryOne;
28 APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
29 APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
33 APInt RHSKnownUnion = RHS.Zero | RHS.One;
45 const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
48 LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
53 const KnownBits &RHS) {
58 if (LHS.isUnknown() && RHS.isUnknown())
61 if (!LHS.isUnknown() && !RHS.isUnknown()) {
63 // Sum = LHS + RHS + 0
64 KnownOut = ::computeForAddCarry(LHS, RHS, /*CarryZero=*/true,
67 // Sum = LHS + ~RHS + 1
68 KnownBits NotRHS = RHS;
79 APInt MinVal = LHS.getMinValue().uadd_sat(RHS.getMinValue());
91 APInt MaxVal = LHS.getMaxValue().usub_sat(RHS.getMinValue());
109 MinVal = LHS.getSignedMinValue().sadd_sat(RHS.getSignedMinValue());
110 MaxVal = LHS.getSignedMaxValue().sadd_sat(RHS.getSignedMaxValue());
113 MinVal = LHS.getSignedMinValue().ssub_sat(RHS.getSignedMaxValue());
114 MaxVal = LHS.getSignedMaxValue().ssub_sat(RHS.getSignedMinValue());
137 KnownBits KnownBits::computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
141 // LHS - RHS = LHS + ~RHS + 1
143 std::swap(RHS.Zero, RHS.One);
144 return ::computeForAddCarry(LHS, RHS,
178 KnownBits KnownBits::umax(const KnownBits &LHS, const KnownBits &RHS) {
179 // If we can prove that LHS >= RHS then use LHS as the result. Likewise for
180 // RHS. Ideally our caller would already have spotted these cases and
183 if (LHS.getMinValue().uge(RHS.getMaxValue()))
185 if (RHS.getMinValue().uge(LHS.getMaxValue()))
186 return RHS;
189 // the minimum possible value of RHS. Likewise for RHS. Any known bits that
191 KnownBits L = LHS.makeGE(RHS.getMinValue());
192 KnownBits R = RHS.makeGE(LHS.getMinValue());
196 KnownBits KnownBits::umin(const KnownBits &LHS, const KnownBits &RHS) {
199 return Flip(umax(Flip(LHS), Flip(RHS)));
202 KnownBits KnownBits::smax(const KnownBits &LHS, const KnownBits &RHS) {
212 return Flip(umax(Flip(LHS), Flip(RHS)));
215 KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) {
225 return Flip(umax(Flip(LHS), Flip(RHS)));
228 KnownBits KnownBits::abdu(const KnownBits &LHS, const KnownBits &RHS) {
229 // If we know which argument is larger, return (sub LHS, RHS) or
230 // (sub RHS, LHS) directly.
231 if (LHS.getMinValue().uge(RHS.getMaxValue()))
233 RHS);
234 if (RHS.getMinValue().uge(LHS.getMaxValue()))
235 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, RHS,
239 // Find the common bits between (sub nuw LHS, RHS) and (sub nuw RHS, LHS).
241 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, LHS, RHS);
243 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, RHS, LHS);
247 KnownBits KnownBits::abds(KnownBits LHS, KnownBits RHS) {
248 // If we know which argument is larger, return (sub LHS, RHS) or
249 // (sub RHS, LHS) directly.
250 if (LHS.getSignedMinValue().sge(RHS.getSignedMaxValue()))
252 RHS);
253 if (RHS.getSignedMinValue().sge(LHS.getSignedMaxValue()))
254 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, RHS,
264 for (auto Arg : {&LHS, &RHS}) {
270 // Find the common bits between (sub nuw LHS, RHS) and (sub nuw RHS, LHS).
272 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, LHS, RHS);
274 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, RHS, LHS);
285 KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW,
311 unsigned MinShiftAmount = RHS.getMinValue().getLimitedValue(BitWidth);
322 APInt MaxValue = RHS.getMaxValue();
349 unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
350 unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
370 KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS,
384 unsigned MinShiftAmount = RHS.getMinValue().getLimitedValue(BitWidth);
393 APInt MaxValue = RHS.getMaxValue();
407 unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
408 unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
428 KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
440 unsigned MinShiftAmount = RHS.getMinValue().getLimitedValue(BitWidth);
453 APInt MaxValue = RHS.getMaxValue();
467 unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
468 unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
488 std::optional<bool> KnownBits::eq(const KnownBits &LHS, const KnownBits &RHS) {
489 if (LHS.isConstant() && RHS.isConstant())
490 return std::optional<bool>(LHS.getConstant() == RHS.getConstant());
491 if (LHS.One.intersects(RHS.Zero) || RHS.One.intersects(LHS.Zero))
496 std::optional<bool> KnownBits::ne(const KnownBits &LHS, const KnownBits &RHS) {
497 if (std::optional<bool> KnownEQ = eq(LHS, RHS))
502 std::optional<bool> KnownBits::ugt(const KnownBits &LHS, const KnownBits &RHS) {
503 // LHS >u RHS -> false if umax(LHS) <= umax(RHS)
504 if (LHS.getMaxValue().ule(RHS.getMinValue()))
506 // LHS >u RHS -> true if umin(LHS) > umax(RHS)
507 if (LHS.getMinValue().ugt(RHS.getMaxValue()))
512 std::optional<bool> KnownBits::uge(const KnownBits &LHS, const KnownBits &RHS) {
513 if (std::optional<bool> IsUGT = ugt(RHS, LHS))
518 std::optional<bool> KnownBits::ult(const KnownBits &LHS, const KnownBits &RHS) {
519 return ugt(RHS, LHS);
522 std::optional<bool> KnownBits::ule(const KnownBits &LHS, const KnownBits &RHS) {
523 return uge(RHS, LHS);
526 std::optional<bool> KnownBits::sgt(const KnownBits &LHS, const KnownBits &RHS) {
527 // LHS >s RHS -> false if smax(LHS) <= smax(RHS)
528 if (LHS.getSignedMaxValue().sle(RHS.getSignedMinValue()))
530 // LHS >s RHS -> true if smin(LHS) > smax(RHS)
531 if (LHS.getSignedMinValue().sgt(RHS.getSignedMaxValue()))
536 std::optional<bool> KnownBits::sge(const KnownBits &LHS, const KnownBits &RHS) {
537 if (std::optional<bool> KnownSGT = sgt(RHS, LHS))
542 std::optional<bool> KnownBits::slt(const KnownBits &LHS, const KnownBits &RHS) {
543 return sgt(RHS, LHS);
546 std::optional<bool> KnownBits::sle(const KnownBits &LHS, const KnownBits &RHS) {
547 return sge(RHS, LHS);
610 const KnownBits &RHS) {
614 KnownBits::computeForAddSub(Add, /*NSW=*/false, /*NUW=*/false, LHS, RHS);
624 if (SignBitKnown(LHS) && SignBitKnown(RHS) && SignBitKnown(Res)) {
627 Overflow = (LHS.isNonNegative() == RHS.isNonNegative() &&
631 Overflow = (LHS.isNonNegative() != RHS.isNonNegative() &&
638 (void)LHS.getMaxValue().uadd_ov(RHS.getMaxValue(), Of);
642 (void)LHS.getMinValue().uadd_ov(RHS.getMinValue(), Of);
649 (void)LHS.getMinValue().usub_ov(RHS.getMaxValue(), Of);
653 (void)LHS.getMaxValue().usub_ov(RHS.getMinValue(), Of);
661 if (LHS.isNonNegative() && RHS.isNonNegative()) {
666 if (LHS.isNegative() && RHS.isNegative()) {
672 if (LHS.isNegative() && RHS.isNonNegative()) {
676 } else if (LHS.isNonNegative() && RHS.isNegative()) {
684 // Sub: Leading zeros of LHS and leading ones of RHS are preserved
689 std::max(LHS.countMinLeadingOnes(), RHS.countMinLeadingOnes());
692 std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingOnes());
753 KnownBits KnownBits::sadd_sat(const KnownBits &LHS, const KnownBits &RHS) {
754 return computeForSatAddSub(/*Add*/ true, /*Signed*/ true, LHS, RHS);
756 KnownBits KnownBits::ssub_sat(const KnownBits &LHS, const KnownBits &RHS) {
757 return computeForSatAddSub(/*Add*/ false, /*Signed*/ true, LHS, RHS);
759 KnownBits KnownBits::uadd_sat(const KnownBits &LHS, const KnownBits &RHS) {
760 return computeForSatAddSub(/*Add*/ true, /*Signed*/ false, LHS, RHS);
762 KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
763 return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
766 static KnownBits avgCompute(KnownBits LHS, KnownBits RHS, bool IsCeil,
770 RHS = IsSigned ? RHS.sext(BitWidth + 1) : RHS.zext(BitWidth + 1);
772 computeForAddCarry(LHS, RHS, /*CarryZero*/ !IsCeil, /*CarryOne*/ IsCeil);
777 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
778 return avgCompute(LHS, RHS, /* IsCeil */ false,
782 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
783 return avgCompute(LHS, RHS, /* IsCeil */ false,
787 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
788 return avgCompute(LHS, RHS, /* IsCeil */ true,
792 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
793 return avgCompute(LHS, RHS, /* IsCeil */ true,
797 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
800 assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
801 assert((!NoUndefSelfMultiply || LHS == RHS) &&
810 APInt UMaxRHS = RHS.getMaxValue();
861 const APInt &Bottom1 = RHS.One;
866 unsigned TrailBitsKnown1 = (RHS.Zero | RHS.One).countr_one();
868 unsigned TrailZero1 = RHS.countMinTrailingZeros();
894 KnownBits KnownBits::mulhs(const KnownBits &LHS, const KnownBits &RHS) {
896 assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
898 KnownBits WideRHS = RHS.sext(2 * BitWidth);
902 KnownBits KnownBits::mulhu(const KnownBits &LHS, const KnownBits &RHS) {
904 assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
906 KnownBits WideRHS = RHS.zext(2 * BitWidth);
911 const KnownBits &RHS, bool Exact) {
923 (int)LHS.countMinTrailingZeros() - (int)RHS.countMaxTrailingZeros();
925 (int)LHS.countMaxTrailingZeros() - (int)RHS.countMinTrailingZeros();
946 KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
949 if (LHS.isNonNegative() && RHS.isNonNegative())
950 return udiv(LHS, RHS, Exact);
955 if (LHS.isZero() || RHS.isZero()) {
963 if (LHS.isNegative() && RHS.isNegative()) {
965 APInt Denom = RHS.getSignedMaxValue();
972 } else if (LHS.isNegative() && RHS.isNonNegative()) {
973 // Result is negative if Exact OR -LHS u>= RHS.
974 if (Exact || (-LHS.getSignedMaxValue()).uge(RHS.getSignedMaxValue())) {
975 APInt Denom = RHS.getSignedMinValue();
979 } else if (LHS.isStrictlyPositive() && RHS.isNegative()) {
980 // Result is negative if Exact OR LHS u>= -RHS.
981 if (Exact || LHS.getSignedMinValue().uge(-RHS.getSignedMinValue())) {
982 APInt Denom = RHS.getSignedMaxValue();
998 Known = divComputeLowBit(Known, LHS, RHS, Exact);
1002 KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
1007 if (LHS.isZero() || RHS.isZero()) {
1017 APInt MinDenom = RHS.getMinValue();
1024 Known = divComputeLowBit(Known, LHS, RHS, Exact);
1029 KnownBits KnownBits::remGetLowBits(const KnownBits &LHS, const KnownBits &RHS) {
1031 if (!RHS.isZero() && RHS.Zero[0]) {
1033 unsigned RHSZeros = RHS.countMinTrailingZeros();
1042 KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) {
1043 KnownBits Known = remGetLowBits(LHS, RHS);
1044 if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
1046 APInt HighBits = ~(RHS.getConstant() - 1);
1054 std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingZeros());
1059 KnownBits KnownBits::srem(const KnownBits &LHS, const KnownBits &RHS) {
1060 KnownBits Known = remGetLowBits(LHS, RHS);
1061 if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
1063 APInt LowBits = RHS.getConstant() - 1;
1084 KnownBits &KnownBits::operator&=(const KnownBits &RHS) {
1086 Zero |= RHS.Zero;
1088 One &= RHS.One;
1092 KnownBits &KnownBits::operator|=(const KnownBits &RHS) {
1094 Zero &= RHS.Zero;
1096 One |= RHS.One;
1100 KnownBits &KnownBits::operator^=(const KnownBits &RHS) {
1102 APInt Z = (Zero & RHS.Zero) | (One & RHS.One);
1104 One = (Zero & RHS.One) | (One & RHS.Zero);