[PATCH] D156845: [ConstantRange] Calculate precise range for shl by -1

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 1 19:36:43 PDT 2023


Allen created this revision.
Allen added reviewers: goldstein.w.n, nikic, StephenFan, arsenm.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Allen requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

According https://reviews.llvm.org/D154953#inline-1516362, this fold
should be handled via simple range propagation.

proofs: https://alive2.llvm.org/ce/z/rnBKHZ


https://reviews.llvm.org/D156845

Files:
  llvm/lib/IR/ConstantRange.cpp
  llvm/test/Transforms/FunctionSpecialization/and-add-shl.ll
  llvm/test/Transforms/InstCombine/and-add-shl.ll


Index: llvm/test/Transforms/FunctionSpecialization/and-add-shl.ll
===================================================================
--- llvm/test/Transforms/FunctionSpecialization/and-add-shl.ll
+++ llvm/test/Transforms/FunctionSpecialization/and-add-shl.ll
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
-; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+; RUN: opt < %s -passes=ipsccp -S | FileCheck %s
 
 declare void @llvm.assume(i1)
 declare i8 @llvm.ctpop.i8(i8)
@@ -8,12 +8,11 @@
 define i8 @and_add_shl(i8 %x) {
 ; CHECK-LABEL: define i8 @and_add_shl
 ; CHECK-SAME: (i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[OP1_P2:%.*]] = icmp ult i8 [[X]], 6
+; CHECK-NEXT:    [[OP1_P2:%.*]] = icmp ule i8 [[X]], 5
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[OP1_P2]])
-; CHECK-NEXT:    [[NOTMASK:%.*]] = shl nsw i8 -1, [[X]]
-; CHECK-NEXT:    [[SUB:%.*]] = and i8 [[NOTMASK]], 32
-; CHECK-NEXT:    [[R:%.*]] = xor i8 [[SUB]], 32
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    [[SHIFT:%.*]] = shl nuw nsw i8 1, [[X]]
+; CHECK-NEXT:    [[SUB:%.*]] = add nsw i8 [[SHIFT]], -1
+; CHECK-NEXT:    ret i8 0
 ;
   %op1_p2 = icmp ule i8 %x, 5
   call void @llvm.assume(i1 %op1_p2)
@@ -27,12 +26,11 @@
 define i8 @and_not_shl(i8 %x) {
 ; CHECK-LABEL: define i8 @and_not_shl
 ; CHECK-SAME: (i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[OP1_P2:%.*]] = icmp ult i8 [[X]], 6
+; CHECK-NEXT:    [[OP1_P2:%.*]] = icmp ule i8 [[X]], 5
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[OP1_P2]])
-; CHECK-NEXT:    [[SHIFT:%.*]] = shl i8 -1, [[X]]
-; CHECK-NEXT:    [[NOT:%.*]] = and i8 [[SHIFT]], 32
-; CHECK-NEXT:    [[R:%.*]] = xor i8 [[NOT]], 32
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    [[SHIFT:%.*]] = shl nsw i8 -1, [[X]]
+; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[SHIFT]], -1
+; CHECK-NEXT:    ret i8 0
 ;
   %op1_p2 = icmp ule i8 %x, 5
   call void @llvm.assume(i1 %op1_p2)
@@ -46,11 +44,11 @@
 define i8 @and_add_shl_overlap(i8 %x) {
 ; CHECK-LABEL: define i8 @and_add_shl_overlap
 ; CHECK-SAME: (i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[OP1_P2:%.*]] = icmp ult i8 [[X]], 7
+; CHECK-NEXT:    [[OP1_P2:%.*]] = icmp ule i8 [[X]], 6
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[OP1_P2]])
-; CHECK-NEXT:    [[NOTMASK:%.*]] = shl nsw i8 -1, [[X]]
-; CHECK-NEXT:    [[SUB:%.*]] = and i8 [[NOTMASK]], 32
-; CHECK-NEXT:    [[R:%.*]] = xor i8 [[SUB]], 32
+; CHECK-NEXT:    [[SHIFT:%.*]] = shl nuw nsw i8 1, [[X]]
+; CHECK-NEXT:    [[SUB:%.*]] = add nsw i8 [[SHIFT]], -1
+; CHECK-NEXT:    [[R:%.*]] = and i8 [[SUB]], 32
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %op1_p2 = icmp ule i8 %x, 6
Index: llvm/lib/IR/ConstantRange.cpp
===================================================================
--- llvm/lib/IR/ConstantRange.cpp
+++ llvm/lib/IR/ConstantRange.cpp
@@ -1478,6 +1478,13 @@
 
   APInt OtherMax = Other.getUnsignedMax();
 
+  bool Neg = false;
+  if (getSingleElement() && isAllNegative()) {
+    Neg = true;
+    Min = Min.abs();
+    Max = Max.abs();
+  }
+
   // There's overflow!
   if (OtherMax.ugt(Max.countl_zero()))
     return getFull();
@@ -1487,7 +1494,12 @@
   Min <<= Other.getUnsignedMin();
   Max <<= OtherMax;
 
-  return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
+  if (Neg) {
+    Min = -Min;
+    Max = -Max;
+    return ConstantRange::getNonEmpty(std::move(Max), std::move(Min) + 1);
+  } else
+    return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
 }
 
 ConstantRange


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156845.546294.patch
Type: text/x-patch
Size: 3472 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230802/e1d9bec0/attachment.bin>


More information about the llvm-commits mailing list