Skip to content

ConstRange: factor and introduce splitPosNeg (NFC) #126528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/ConstantRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class [[nodiscard]] ConstantRange {
/// unsigned domain.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned);

/// Split the ConstantRange into positive and negative components, ignoring
/// zero values.
std::pair<ConstantRange, ConstantRange> splitPosNeg() const;

/// Produce the smallest range such that all values that may satisfy the given
/// predicate with any value contained within Other is contained in the
/// returned range. Formally, this returns a superset of
Expand Down
27 changes: 16 additions & 11 deletions llvm/lib/IR/ConstantRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ KnownBits ConstantRange::toKnownBits() const {
return Known;
}

std::pair<ConstantRange, ConstantRange> ConstantRange::splitPosNeg() const {
uint32_t BW = getBitWidth();
APInt Zero = APInt::getZero(BW), One = APInt(BW, 1);
APInt SignedMin = APInt::getSignedMinValue(BW);
// There are no positive 1-bit values. The 1 would get interpreted as -1.
ConstantRange PosFilter =
BW == 1 ? getEmpty() : ConstantRange(One, SignedMin);
ConstantRange NegFilter(SignedMin, Zero);
return {intersectWith(PosFilter), intersectWith(NegFilter)};
}

ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
const ConstantRange &CR) {
if (CR.isEmptySet())
Expand Down Expand Up @@ -1356,20 +1367,14 @@ ConstantRange::udiv(const ConstantRange &RHS) const {
}

ConstantRange ConstantRange::sdiv(const ConstantRange &RHS) const {
APInt Zero = APInt::getZero(getBitWidth());
APInt SignedMin = APInt::getSignedMinValue(getBitWidth());

// We split up the LHS and RHS into positive and negative components
// and then also compute the positive and negative components of the result
// separately by combining division results with the appropriate signs.
APInt Zero = APInt::getZero(getBitWidth());
APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
// There are no positive 1-bit values. The 1 would get interpreted as -1.
ConstantRange PosFilter =
getBitWidth() == 1 ? getEmpty()
: ConstantRange(APInt(getBitWidth(), 1), SignedMin);
ConstantRange NegFilter(SignedMin, Zero);
ConstantRange PosL = intersectWith(PosFilter);
ConstantRange NegL = intersectWith(NegFilter);
ConstantRange PosR = RHS.intersectWith(PosFilter);
ConstantRange NegR = RHS.intersectWith(NegFilter);
auto [PosL, NegL] = splitPosNeg();
auto [PosR, NegR] = RHS.splitPosNeg();

ConstantRange PosRes = getEmpty();
if (!PosL.isEmptySet() && !PosR.isEmptySet())
Expand Down
10 changes: 10 additions & 0 deletions llvm/unittests/IR/ConstantRangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,16 @@ TEST(ConstantRange, GetEquivalentICmp) {
});
}

TEST(ConstantRange, SplitPosNeg) {
EnumerateInterestingConstantRanges([](const ConstantRange &CR) {
auto [Pos, Neg] = CR.splitPosNeg();
EXPECT_TRUE(Pos.isAllPositive());
EXPECT_TRUE(Neg.isAllNegative());
if (CR.getBitWidth() == 1)
EXPECT_TRUE(Pos.isEmptySet());
});
}

#define EXPECT_MAY_OVERFLOW(op) \
EXPECT_EQ(ConstantRange::OverflowResult::MayOverflow, (op))
#define EXPECT_ALWAYS_OVERFLOWS_LOW(op) \
Expand Down