Lines Matching defs:LHS
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;
32 APInt LHSKnownUnion = LHS.Zero | LHS.One;
45 const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
48 LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
52 const KnownBits &LHS,
54 unsigned BitWidth = LHS.getBitWidth();
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
70 KnownOut = ::computeForAddCarry(LHS, NotRHS, /*CarryZero=*/false,
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
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
183 if (LHS.getMinValue().uge(RHS.getMaxValue()))
184 return LHS;
185 if (RHS.getMinValue().uge(LHS.getMaxValue()))
188 // If the result of the umax is LHS then it must be greater than or equal to
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()))
232 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
234 if (RHS.getMinValue().uge(LHS.getMaxValue()))
236 LHS);
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()))
251 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
253 if (RHS.getSignedMinValue().sge(LHS.getSignedMaxValue()))
255 LHS);
263 unsigned SignBitPosition = LHS.getBitWidth() - 1;
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,
287 unsigned BitWidth = LHS.getBitWidth();
288 auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
291 Known.Zero = LHS.Zero.ushl_ov(ShiftAmt, ShiftedOutZero);
293 Known.One = LHS.One.ushl_ov(ShiftAmt, ShiftedOutOne);
309 // Fast path for a common case when LHS is completely unknown.
314 if (LHS.isUnknown()) {
325 MaxShiftAmount = std::min(MaxShiftAmount, LHS.countMaxLeadingZeros() - 1);
327 MaxShiftAmount = std::min(MaxShiftAmount, LHS.countMaxLeadingZeros());
331 std::max(LHS.countMaxLeadingZeros(), LHS.countMaxLeadingOnes()) - 1);
336 Known.Zero.setLowBits(LHS.countMinTrailingZeros());
337 if (LHS.isAllOnes())
340 if (LHS.isNonNegative())
342 if (LHS.isNegative())
359 Known = Known.intersectWith(ShiftByConst(LHS, ShiftAmt));
370 KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS,
372 unsigned BitWidth = LHS.getBitWidth();
373 auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
374 KnownBits Known = LHS;
382 // Fast path for a common case when LHS is completely unknown.
387 if (LHS.isUnknown()) {
396 // If exact, bound MaxShiftAmount to first known 1 in LHS.
398 unsigned FirstOne = LHS.countMaxTrailingZeros();
417 Known = Known.intersectWith(ShiftByConst(LHS, ShiftAmt));
428 KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
430 unsigned BitWidth = LHS.getBitWidth();
431 auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
432 KnownBits Known = LHS;
438 // Fast path for a common case when LHS is completely unknown.
443 if (LHS.isUnknown()) {
456 // If exact, bound MaxShiftAmount to first known 1 in LHS.
458 unsigned FirstOne = LHS.countMaxTrailingZeros();
477 Known = Known.intersectWith(ShiftByConst(LHS, ShiftAmt));
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);
609 const KnownBits &LHS,
614 KnownBits::computeForAddSub(Add, /*NSW=*/false, /*NUW=*/false, LHS, RHS);
624 if (SignBitKnown(LHS) && SignBitKnown(RHS) && SignBitKnown(Res)) {
627 Overflow = (LHS.isNonNegative() == RHS.isNonNegative() &&
628 Res.isNonNegative() != LHS.isNonNegative());
631 Overflow = (LHS.isNonNegative() != RHS.isNonNegative() &&
632 Res.isNonNegative() != LHS.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());
717 assert(SignBitKnown(LHS) &&
719 C = LHS.isNegative() ? APInt::getSignedMinValue(BitWidth)
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,
768 unsigned BitWidth = LHS.getBitWidth();
769 LHS = IsSigned ? LHS.sext(BitWidth + 1) : LHS.zext(BitWidth + 1);
771 LHS =
772 computeForAddCarry(LHS, RHS, /*CarryZero*/ !IsCeil, /*CarryOne*/ IsCeil);
773 LHS = LHS.extractBits(BitWidth, 1);
774 return LHS;
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,
799 unsigned BitWidth = LHS.getBitWidth();
801 assert((!NoUndefSelfMultiply || LHS == RHS) &&
809 APInt UMaxLHS = LHS.getMaxValue();
860 const APInt &Bottom0 = LHS.One;
865 unsigned TrailBitsKnown0 = (LHS.Zero | LHS.One).countr_one();
867 unsigned TrailZero0 = LHS.countMinTrailingZeros();
894 KnownBits KnownBits::mulhs(const KnownBits &LHS, const KnownBits &RHS) {
895 unsigned BitWidth = LHS.getBitWidth();
897 KnownBits WideLHS = LHS.sext(2 * BitWidth);
902 KnownBits KnownBits::mulhu(const KnownBits &LHS, const KnownBits &RHS) {
903 unsigned BitWidth = LHS.getBitWidth();
905 KnownBits WideLHS = LHS.zext(2 * BitWidth);
910 static KnownBits divComputeLowBit(KnownBits Known, const KnownBits &LHS,
916 // If LHS is Odd, the result is Odd no matter what.
919 if (LHS.One[0])
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);
952 unsigned BitWidth = LHS.getBitWidth();
955 if (LHS.isZero() || RHS.isZero()) {
963 if (LHS.isNegative() && RHS.isNegative()) {
966 APInt Num = LHS.getSignedMinValue();
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())) {
976 APInt Num = LHS.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())) {
983 APInt Num = LHS.getSignedMaxValue();
998 Known = divComputeLowBit(Known, LHS, RHS, Exact);
1002 KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
1004 unsigned BitWidth = LHS.getBitWidth();
1007 if (LHS.isZero() || RHS.isZero()) {
1018 APInt MaxNum = LHS.getMaxValue();
1024 Known = divComputeLowBit(Known, LHS, RHS, Exact);
1029 KnownBits KnownBits::remGetLowBits(const KnownBits &LHS, const KnownBits &RHS) {
1030 unsigned BitWidth = LHS.getBitWidth();
1035 APInt OnesMask = LHS.One & Mask;
1036 APInt ZerosMask = LHS.Zero & Mask;
1042 KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) {
1043 KnownBits Known = remGetLowBits(LHS, RHS);
1054 std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingZeros());
1059 KnownBits KnownBits::srem(const KnownBits &LHS, const KnownBits &RHS) {
1060 KnownBits Known = remGetLowBits(LHS, RHS);
1066 if (LHS.isNonNegative() || LowBits.isSubsetOf(LHS.Zero))
1071 if (LHS.isNegative() && LowBits.intersects(LHS.One))
1076 // The sign bit is the LHS's sign bit, except when the result of the
1078 // equal to the magnitude of the LHS. Therefore any leading zeros that exist
1080 Known.Zero.setHighBits(LHS.countMinLeadingZeros());