[llvm] ba1e04b - [ConstantRange] Fix sdiv() with one bit values (PR56333)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 1 06:45:07 PDT 2022
Author: Nikita Popov
Date: 2022-07-01T15:44:59+02:00
New Revision: ba1e04b9668b3502362f2ee36232f5f56e72b4e2
URL: https://github.com/llvm/llvm-project/commit/ba1e04b9668b3502362f2ee36232f5f56e72b4e2
DIFF: https://github.com/llvm/llvm-project/commit/ba1e04b9668b3502362f2ee36232f5f56e72b4e2.diff
LOG: [ConstantRange] Fix sdiv() with one bit values (PR56333)
Signed one bit values can only be -1 or 0, not positive. The code
was interpreting the 1 as -1 and intersecting with a full range
rather than an empty one.
Fixes https://github.com/llvm/llvm-project/issues/56333.
Added:
Modified:
llvm/lib/IR/ConstantRange.cpp
llvm/unittests/IR/ConstantRangeTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index be6386c571b59..9d239101d8fde 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -1238,7 +1238,10 @@ ConstantRange ConstantRange::sdiv(const ConstantRange &RHS) const {
// separately by combining division results with the appropriate signs.
APInt Zero = APInt::getZero(getBitWidth());
APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
- ConstantRange PosFilter(APInt(getBitWidth(), 1), SignedMin);
+ // 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);
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index ced38a28977b2..edea097aae763 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -1259,6 +1259,9 @@ TEST_F(ConstantRangeTest, UDiv) {
}
TEST_F(ConstantRangeTest, SDiv) {
+ ConstantRange OneBit = ConstantRange::getFull(1);
+ EXPECT_EQ(OneBit.sdiv(OneBit), ConstantRange(APInt(1, 0)));
+
unsigned Bits = 4;
EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1,
const ConstantRange &CR2) {
More information about the llvm-commits
mailing list