[ConstantRange] Fix single bit abs range (PR59887)
authorNikita Popov <npopov@redhat.com>
Mon, 9 Jan 2023 15:29:37 +0000 (16:29 +0100)
committerNikita Popov <npopov@redhat.com>
Mon, 9 Jan 2023 15:34:09 +0000 (16:34 +0100)
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.

llvm/lib/IR/ConstantRange.cpp
llvm/test/Transforms/CorrelatedValuePropagation/abs.ll
llvm/unittests/IR/ConstantRangeTest.cpp

index 2f0d3d1..0dbccaa 100644 (file)
@@ -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(
index 47ed789..6231b05 100644 (file)
@@ -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
index cb53801..45398ec 100644 (file)
@@ -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 &,