[llvm] 4dd392f - [ConstantRange] Make shl() for negative LHS more precise

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 29 05:48:41 PDT 2023


Author: Nikita Popov
Date: 2023-08-29T14:48:33+02:00
New Revision: 4dd392fcd7847ef73233cd2f24623064ebc90a16

URL: https://github.com/llvm/llvm-project/commit/4dd392fcd7847ef73233cd2f24623064ebc90a16
DIFF: https://github.com/llvm/llvm-project/commit/4dd392fcd7847ef73233cd2f24623064ebc90a16.diff

LOG: [ConstantRange] Make shl() for negative LHS more precise

This differs from the positive case in that shifting by a larger
amount makes the result smaller, not larger.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantRange.cpp
    llvm/test/Transforms/SCCP/and-add-shl.ll
    llvm/unittests/IR/ConstantRangeTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index e9344a8815c089..d22d56e40c08f2 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -1477,6 +1477,13 @@ ConstantRange::shl(const ConstantRange &Other) const {
   }
 
   APInt OtherMax = Other.getUnsignedMax();
+  if (isAllNegative() && OtherMax.ule(Min.countl_one())) {
+    // For negative numbers, if the shift does not overflow in a signed sense,
+    // a larger shift will make the number smaller.
+    Max <<= Other.getUnsignedMin();
+    Min <<= OtherMax;
+    return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
+  }
 
   // There's overflow!
   if (OtherMax.ugt(Max.countl_zero()))

diff  --git a/llvm/test/Transforms/SCCP/and-add-shl.ll b/llvm/test/Transforms/SCCP/and-add-shl.ll
index 427372c2d670f1..7c037ffa6bf640 100644
--- a/llvm/test/Transforms/SCCP/and-add-shl.ll
+++ b/llvm/test/Transforms/SCCP/and-add-shl.ll
@@ -30,8 +30,7 @@ define i8 @and_not_shl(i8 %x) {
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[OP1_P2]])
 ; CHECK-NEXT:    [[SHIFT:%.*]] = shl nsw i8 -1, [[X]]
 ; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[SHIFT]], -1
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[NOT]], 32
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 0
 ;
   %op1_p2 = icmp ule i8 %x, 5
   call void @llvm.assume(i1 %op1_p2)
@@ -48,8 +47,7 @@ define i8 @and_not_shl_1(i8 %x) {
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[OP1_P2]])
 ; CHECK-NEXT:    [[SHIFT:%.*]] = shl nsw i8 -1, [[X]]
 ; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[SHIFT]], -1
-; CHECK-NEXT:    [[R:%.*]] = and i8 [[NOT]], 48
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 0
 ;
   %op1_p2 = icmp ule i8 %x, 4
   call void @llvm.assume(i1 %op1_p2)

diff  --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index 38d1119e7f85bd..1cb358a26062ca 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -1373,6 +1373,13 @@ TEST_F(ConstantRangeTest, Shl) {
       ConstantRange(APInt(16, 0xfff << 0x1), APInt(16, 0x7fff << 0x1) + 1));
   EXPECT_EQ(One.shl(WrapNullMax), Full);
 
+  ConstantRange NegOne(APInt(16, 0xffff));
+  EXPECT_EQ(NegOne.shl(ConstantRange(APInt(16, 0), APInt(16, 5))),
+            ConstantRange(APInt(16, 0xfff0), APInt(16, 0)));
+  EXPECT_EQ(ConstantRange(APInt(16, 0xfffe), APInt(16, 0))
+                .shl(ConstantRange(APInt(16, 0), APInt(16, 5))),
+            ConstantRange(APInt(16, 0xffe0), APInt(16, 0)));
+
   TestBinaryOpExhaustive(
       [](const ConstantRange &CR1, const ConstantRange &CR2) {
         return CR1.shl(CR2);


        


More information about the llvm-commits mailing list