23 bool CarryZero,
bool CarryOne) {
24 assert(!(CarryZero && CarryOne) &&
25 "Carry can't be zero and one at the same time");
27 APInt PossibleSumZero =
LHS.getMaxValue() +
RHS.getMaxValue() + !CarryZero;
28 APInt PossibleSumOne =
LHS.getMinValue() +
RHS.getMinValue() + CarryOne;
31 APInt CarryKnownZero = ~(PossibleSumZero ^
LHS.Zero ^
RHS.Zero);
32 APInt CarryKnownOne = PossibleSumOne ^
LHS.One ^
RHS.One;
37 APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
38 APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;
40 assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
41 "known bits of sum differ");
46 KnownOut.
One = std::move(PossibleSumOne) & Known;
53 return ::computeForAddCarry(
76 if (
LHS.isNonNegative() &&
RHS.isNonNegative())
80 else if (
LHS.isNegative() &&
RHS.isNegative())
91 "Illegal sext-in-register");
96 unsigned ExtBits =
BitWidth - SrcBitWidth;
98 Result.One =
One << ExtBits;
99 Result.Zero =
Zero << ExtBits;
100 Result.One.ashrInPlace(ExtBits);
101 Result.Zero.ashrInPlace(ExtBits);
112 APInt MaskedVal(Val);
122 if (
LHS.getMinValue().uge(
RHS.getMaxValue()))
124 if (
RHS.getMinValue().uge(
LHS.getMaxValue()))
132 return L.intersectWith(R);
148 One.
setBitVal(SignBitPosition, Val.Zero[SignBitPosition]);
161 One.
setBitVal(SignBitPosition, Val.One[SignBitPosition]);
175 bool NSW,
bool ShAmtNonZero) {
177 auto ShiftByConst = [&](
const KnownBits &
LHS,
unsigned ShiftAmt) {
179 bool ShiftedOutZero, ShiftedOutOne;
180 Known.
Zero =
LHS.Zero.ushl_ov(ShiftAmt, ShiftedOutZero);
182 Known.
One =
LHS.One.ushl_ov(ShiftAmt, ShiftedOutOne);
186 if (NUW && ShiftAmt != 0)
188 ShiftedOutZero =
true;
192 else if (ShiftedOutOne)
200 unsigned MinShiftAmount =
RHS.getMinValue().getLimitedValue(
BitWidth);
201 if (MinShiftAmount == 0 && ShAmtNonZero)
203 if (
LHS.isUnknown()) {
205 if (NUW && NSW && MinShiftAmount != 0)
214 MaxShiftAmount = std::min(MaxShiftAmount,
LHS.countMaxLeadingZeros() - 1);
216 MaxShiftAmount = std::min(MaxShiftAmount,
LHS.countMaxLeadingZeros());
218 MaxShiftAmount = std::min(
220 std::max(
LHS.countMaxLeadingZeros(),
LHS.countMaxLeadingOnes()) - 1);
223 if (MinShiftAmount == 0 && MaxShiftAmount ==
BitWidth - 1 &&
229 if (
LHS.isNonNegative())
231 if (
LHS.isNegative())
238 unsigned ShiftAmtZeroMask =
RHS.Zero.zextOrTrunc(32).getZExtValue();
239 unsigned ShiftAmtOneMask =
RHS.One.zextOrTrunc(32).getZExtValue();
242 for (
unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
245 if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
246 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
262 auto ShiftByConst = [&](
const KnownBits &
LHS,
unsigned ShiftAmt) {
273 unsigned MinShiftAmount =
RHS.getMinValue().getLimitedValue(
BitWidth);
274 if (MinShiftAmount == 0 && ShAmtNonZero)
276 if (
LHS.isUnknown()) {
284 unsigned ShiftAmtZeroMask =
RHS.Zero.zextOrTrunc(32).getZExtValue();
285 unsigned ShiftAmtOneMask =
RHS.One.zextOrTrunc(32).getZExtValue();
288 for (
unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
291 if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
292 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
308 auto ShiftByConst = [&](
const KnownBits &
LHS,
unsigned ShiftAmt) {
317 unsigned MinShiftAmount =
RHS.getMinValue().getLimitedValue(
BitWidth);
318 if (MinShiftAmount == 0 && ShAmtNonZero)
320 if (
LHS.isUnknown()) {
332 unsigned ShiftAmtZeroMask =
RHS.Zero.zextOrTrunc(32).getZExtValue();
333 unsigned ShiftAmtOneMask =
RHS.One.zextOrTrunc(32).getZExtValue();
336 for (
unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
339 if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
340 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
354 if (
LHS.isConstant() &&
RHS.isConstant())
355 return std::optional<bool>(
LHS.getConstant() ==
RHS.getConstant());
356 if (
LHS.One.intersects(
RHS.Zero) ||
RHS.One.intersects(
LHS.Zero))
357 return std::optional<bool>(
false);
362 if (std::optional<bool> KnownEQ =
eq(
LHS,
RHS))
363 return std::optional<bool>(!*KnownEQ);
369 if (
LHS.getMaxValue().ule(
RHS.getMinValue()))
370 return std::optional<bool>(
false);
372 if (
LHS.getMinValue().ugt(
RHS.getMaxValue()))
373 return std::optional<bool>(
true);
378 if (std::optional<bool> IsUGT =
ugt(
RHS,
LHS))
379 return std::optional<bool>(!*IsUGT);
393 if (
LHS.getSignedMaxValue().sle(
RHS.getSignedMinValue()))
394 return std::optional<bool>(
false);
396 if (
LHS.getSignedMinValue().sgt(
RHS.getSignedMaxValue()))
397 return std::optional<bool>(
true);
402 if (std::optional<bool> KnownSGT =
sgt(
RHS,
LHS))
403 return std::optional<bool>(!*KnownSGT);
434 false, IntMinIsPoison,
477 assert(!
LHS.hasConflict() && !
RHS.hasConflict() &&
"Bad inputs");
482 auto SignBitKnown = [&](
const KnownBits &K) {
485 std::optional<bool> Overflow;
490 if (SignBitKnown(
LHS) && SignBitKnown(
RHS) && SignBitKnown(Res)) {
493 Overflow = (
LHS.isNonNegative() ==
RHS.isNonNegative() &&
497 Overflow = (
LHS.isNonNegative() !=
RHS.isNonNegative() &&
504 (void)
LHS.getMaxValue().uadd_ov(
RHS.getMaxValue(), Of);
508 (void)
LHS.getMinValue().uadd_ov(
RHS.getMinValue(), Of);
515 (void)
LHS.getMinValue().usub_ov(
RHS.getMaxValue(), Of);
519 (void)
LHS.getMaxValue().usub_ov(
RHS.getMinValue(), Of);
527 if (
LHS.isNonNegative() &&
RHS.isNonNegative()) {
532 if (
LHS.isNegative() &&
RHS.isNegative()) {
538 if (
LHS.isNegative() &&
RHS.isNonNegative()) {
542 }
else if (
LHS.isNonNegative() &&
RHS.isNegative()) {
552 unsigned LeadingKnown;
555 std::max(
LHS.countMinLeadingOnes(),
RHS.countMinLeadingOnes());
558 std::max(
LHS.countMinLeadingZeros(),
RHS.countMinLeadingOnes());
585 "We somehow know overflow without knowing input sign");
636 bool NoUndefSelfMultiply) {
639 !
RHS.hasConflict() &&
"Operand mismatch");
641 "Self multiplication knownbits mismatch");
654 APInt UMaxResult = UMaxLHS.
umul_ov(UMaxRHS, HasOverflow);
655 unsigned LeadZ = HasOverflow ? 0 : UMaxResult.
countl_zero();
706 unsigned TrailZero0 =
LHS.countMinTrailingZeros();
707 unsigned TrailZero1 =
RHS.countMinTrailingZeros();
708 unsigned TrailZ = TrailZero0 + TrailZero1;
711 unsigned SmallestOperand =
712 std::min(TrailBitsKnown0 - TrailZero0, TrailBitsKnown1 - TrailZero1);
713 unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ,
BitWidth);
720 Res.
Zero |= (~BottomKnown).getLoBits(ResultBitsKnown);
724 if (NoUndefSelfMultiply &&
BitWidth > 1) {
726 "Self-multiplication failed Quadratic Reciprocity!");
736 !
RHS.hasConflict() &&
"Operand mismatch");
745 !
RHS.hasConflict() &&
"Operand mismatch");
764 (int)
LHS.countMinTrailingZeros() - (int)
RHS.countMaxTrailingZeros();
766 (int)
LHS.countMaxTrailingZeros() - (int)
RHS.countMinTrailingZeros();
770 if (MinTZ == MaxTZ) {
774 }
else if (MaxTZ < 0) {
790 if (
LHS.isNonNegative() &&
RHS.isNonNegative())
794 assert(!
LHS.hasConflict() && !
RHS.hasConflict() &&
"Bad inputs");
797 if (
LHS.isZero() ||
RHS.isZero()) {
804 std::optional<APInt> Res;
805 if (
LHS.isNegative() &&
RHS.isNegative()) {
807 APInt Denom =
RHS.getSignedMaxValue();
808 APInt Num =
LHS.getSignedMinValue();
814 }
else if (
LHS.isNegative() &&
RHS.isNonNegative()) {
816 if (Exact || (-
LHS.getSignedMaxValue()).uge(
RHS.getSignedMaxValue())) {
817 APInt Denom =
RHS.getSignedMinValue();
818 APInt Num =
LHS.getSignedMinValue();
821 }
else if (
LHS.isStrictlyPositive() &&
RHS.isNegative()) {
823 if (Exact ||
LHS.getSignedMinValue().uge(-
RHS.getSignedMinValue())) {
824 APInt Denom =
RHS.getSignedMaxValue();
825 APInt Num =
LHS.getSignedMaxValue();
826 Res = Num.
sdiv(Denom);
831 if (Res->isNonNegative()) {
835 unsigned LeadO = Res->countLeadingOnes();
852 if (
LHS.isZero() ||
RHS.isZero()) {
877 if (!
RHS.isZero() &&
RHS.Zero[0]) {
879 unsigned RHSZeros =
RHS.countMinTrailingZeros();
892 if (
RHS.isConstant() &&
RHS.getConstant().isPowerOf2()) {
894 APInt HighBits = ~(
RHS.getConstant() - 1);
895 Known.
Zero |= HighBits;
902 std::max(
LHS.countMinLeadingZeros(),
RHS.countMinLeadingZeros());
911 if (
RHS.isConstant() &&
RHS.getConstant().isPowerOf2()) {
913 APInt LowBits =
RHS.getConstant() - 1;
917 Known.
Zero |= ~LowBits;
922 Known.
One |= ~LowBits;
static KnownBits computeForSatAddSub(bool Add, bool Signed, const KnownBits &LHS, const KnownBits &RHS)
static KnownBits divComputeLowBit(KnownBits Known, const KnownBits &LHS, const KnownBits &RHS, bool Exact)
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, bool CarryZero, bool CarryOne)
static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
APInt udiv(const APInt &RHS) const
Unsigned division operation.
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
unsigned popcount() const
Count the number of bits set.
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
void setSignBit()
Set the sign bit to 1.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
void clearAllBits()
Set every bit to 0.
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
unsigned countl_zero() const
The APInt version of std::countl_zero.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
unsigned countLeadingZeros() const
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
void setAllBits()
Set every bit to 1.
bool getBoolValue() const
Convert APInt to a boolean value.
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
void setBitVal(unsigned BitPosition, bool BitValue)
Set a given bit to a given value.
void clearSignBit()
Set the sign bit to 0.
This class implements an extremely fast bulk output stream that can only output to a stream.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
static std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
KnownBits makeGE(const APInt &Val) const
Return KnownBits based on this, but updated given that the underlying value is known to be greater th...
KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
bool hasConflict() const
Returns true if there is conflicting information.
static std::optional< bool > sge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGE result.
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
void setAllZero()
Make all bits known to be zero and discard any previous information.
KnownBits & operator|=(const KnownBits &RHS)
Update known bits based on ORing with RHS.
void print(raw_ostream &OS) const
unsigned getBitWidth() const
Get the bit width of this value.
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false)
Compute known bits for lshr(LHS, RHS).
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
KnownBits & operator&=(const KnownBits &RHS)
Update known bits based on ANDing with RHS.
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static std::optional< bool > ugt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGT result.
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static std::optional< bool > slt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLT result.
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false)
Compute known bits for ashr(LHS, RHS).
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static std::optional< bool > ult(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_ULT result.
static std::optional< bool > ule(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_ULE result.
bool isNegative() const
Returns true if this value is known to be negative.
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static std::optional< bool > sle(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLE result.
static std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
unsigned countMinPopulation() const
Returns the number of bits known to be one.
static std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
KnownBits & operator^=(const KnownBits &RHS)
Update known bits based on XORing with RHS.
static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS, KnownBits RHS)
Compute known bits resulting from adding LHS and RHS.
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).