[llvm] b403004 - ConstRange: factor and introduce splitPosNeg (NFC) (#126528)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 15 09:25:07 PST 2025
Author: Ramkumar Ramachandra
Date: 2025-02-15T17:25:03Z
New Revision: b4030040359656ed20cb29de7b3912b6b249e98e
URL: https://github.com/llvm/llvm-project/commit/b4030040359656ed20cb29de7b3912b6b249e98e
DIFF: https://github.com/llvm/llvm-project/commit/b4030040359656ed20cb29de7b3912b6b249e98e.diff
LOG: ConstRange: factor and introduce splitPosNeg (NFC) (#126528)
Factor out some code that splits a ConstantRange into positive and
negative components, introducing ConstantRange::splitPosNeg.
Added:
Modified:
llvm/include/llvm/IR/ConstantRange.h
llvm/lib/IR/ConstantRange.cpp
llvm/unittests/IR/ConstantRangeTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h
index d086c25390fd2..3561513212ce2 100644
--- a/llvm/include/llvm/IR/ConstantRange.h
+++ b/llvm/include/llvm/IR/ConstantRange.h
@@ -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
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 3566435398992..41e40cdf365d2 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -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())
@@ -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())
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index c390ffea1c352..daa07bf7d840d 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -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) \
More information about the llvm-commits
mailing list