Skip to content

Commit

Permalink
[ConstantRange] Perform increment on APInt (NFC)
Browse files Browse the repository at this point in the history
This handles the edge case where BitWidth is 1 and doing the
increment gets a value that's not valid in that width, while we
just want wrap-around.

Split out of #80309.
  • Loading branch information
nikic committed Sep 5, 2024
1 parent 233ed51 commit 9707b98
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9596,7 +9596,7 @@ static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) {
case Intrinsic::cttz:
// Maximum of set/clear bits is the bit width.
return ConstantRange::getNonEmpty(APInt::getZero(Width),
APInt(Width, Width + 1));
APInt(Width, Width) + 1);
case Intrinsic::uadd_sat:
// uadd.sat(x, C) produces [C, UINT_MAX].
if (match(II.getOperand(0), m_APInt(C)) ||
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/IR/ConstantRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,7 @@ ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const {
// Zero is either safe or not in the range. The output range is composed by
// the result of countLeadingZero of the two extremes.
return getNonEmpty(APInt(getBitWidth(), getUnsignedMax().countl_zero()),
APInt(getBitWidth(), getUnsignedMin().countl_zero() + 1));
APInt(getBitWidth(), getUnsignedMin().countl_zero()) + 1);
}

static ConstantRange getUnsignedCountTrailingZerosRange(const APInt &Lower,
Expand Down Expand Up @@ -2011,7 +2011,7 @@ ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const {
}

if (isFullSet())
return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
return getNonEmpty(Zero, APInt(BitWidth, BitWidth) + 1);
if (!isWrappedSet())
return getUnsignedCountTrailingZerosRange(Lower, Upper);
// The range is wrapped. We decompose it into two ranges, [0, Upper) and
Expand Down Expand Up @@ -2056,7 +2056,7 @@ ConstantRange ConstantRange::ctpop() const {
unsigned BitWidth = getBitWidth();
APInt Zero = APInt::getZero(BitWidth);
if (isFullSet())
return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
return getNonEmpty(Zero, APInt(BitWidth, BitWidth) + 1);
if (!isWrappedSet())
return getUnsignedPopCountRange(Lower, Upper);
// The range is wrapped. We decompose it into two ranges, [0, Upper) and
Expand Down

0 comments on commit 9707b98

Please sign in to comment.