[llvm] fd07583 - [ConstantRange] Fix single bit abs range (PR59887)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 9 07:34:23 PST 2023
Author: Nikita Popov
Date: 2023-01-09T16:34:09+01:00
New Revision: fd07583ca439494891662d5db58895f63e904cc5
URL: https://github.com/llvm/llvm-project/commit/fd07583ca439494891662d5db58895f63e904cc5
DIFF: https://github.com/llvm/llvm-project/commit/fd07583ca439494891662d5db58895f63e904cc5.diff
LOG: [ConstantRange] Fix single bit abs range (PR59887)
For a full range input, we would produce an empty range instead
of a full range. The change to the SMin.isNonNegative() branch is
an optimality fix, because we should account for the potentially
discarded SMin value in the IntMinIsPoison case.
Change TestUnaryOpExhaustive to test both 4 and 1 bits, to both
cover this specific case in unit tests, and make sure all other
unary operations deal with 1-bit inputs correctly.
Fixes https://github.com/llvm/llvm-project/issues/59887.
Added:
Modified:
llvm/lib/IR/ConstantRange.cpp
llvm/test/Transforms/CorrelatedValuePropagation/abs.ll
llvm/unittests/IR/ConstantRangeTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 2f0d3d1e3a5ac..0dbccaa1a66aa 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -1656,15 +1656,15 @@ ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
// All non-negative.
if (SMin.isNonNegative())
- return *this;
+ return ConstantRange(SMin, SMax + 1);
// All negative.
if (SMax.isNegative())
return ConstantRange(-SMax, -SMin + 1);
// Range crosses zero.
- return ConstantRange(APInt::getZero(getBitWidth()),
- APIntOps::umax(-SMin, SMax) + 1);
+ return ConstantRange::getNonEmpty(APInt::getZero(getBitWidth()),
+ APIntOps::umax(-SMin, SMax) + 1);
}
ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow(
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/abs.ll b/llvm/test/Transforms/CorrelatedValuePropagation/abs.ll
index 47ed7892b5d3f..6231b05a851cb 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/abs.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/abs.ll
@@ -381,7 +381,7 @@ define i1 @pr59887(i1 %x, i1 %c) {
; CHECK-LABEL: @pr59887(
; CHECK-NEXT: [[ABS:%.*]] = call i1 @llvm.abs.i1(i1 [[X:%.*]], i1 false)
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C:%.*]], i1 [[ABS]], i1 false
-; CHECK-NEXT: ret i1 false
+; CHECK-NEXT: ret i1 [[RES]]
;
%abs = call i1 @llvm.abs.i1(i1 %x, i1 false)
%res = select i1 %c, i1 %abs, i1 false
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index cb53801851d42..45398ec6a6176 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -179,15 +179,16 @@ using UnaryIntFn = llvm::function_ref<std::optional<APInt>(const APInt &)>;
static void TestUnaryOpExhaustive(UnaryRangeFn RangeFn, UnaryIntFn IntFn,
PreferFn PreferenceFn = PreferSmallest) {
- unsigned Bits = 4;
- EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
- SmallBitVector Elems(1 << Bits);
- ForeachNumInConstantRange(CR, [&](const APInt &N) {
- if (std::optional<APInt> ResultN = IntFn(N))
- Elems.set(ResultN->getZExtValue());
+ for (unsigned Bits : {1, 4}) {
+ EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
+ SmallBitVector Elems(1 << Bits);
+ ForeachNumInConstantRange(CR, [&](const APInt &N) {
+ if (std::optional<APInt> ResultN = IntFn(N))
+ Elems.set(ResultN->getZExtValue());
+ });
+ TestRange(RangeFn(CR), Elems, PreferenceFn, {CR});
});
- TestRange(RangeFn(CR), Elems, PreferenceFn, {CR});
- });
+ }
}
using BinaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &,
More information about the llvm-commits
mailing list