[PATCH] D156845: [ConstantRange] Calculate precise range for shl by -1
Allen zhong via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 11 22:05:18 PDT 2023
Allen updated this revision to Diff 549574.
Allen marked an inline comment as done.
Allen added a comment.
add condition **OtherMax.ule(Max.abs().countl_zero())** to fix the failure cases reported in unittests/IR/ConstantRangeTest.cpp
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D156845/new/
https://reviews.llvm.org/D156845
Files:
llvm/lib/IR/ConstantRange.cpp
llvm/test/Transforms/SCCP/and-add-shl.ll
Index: llvm/test/Transforms/SCCP/and-add-shl.ll
===================================================================
--- llvm/test/Transforms/SCCP/and-add-shl.ll
+++ llvm/test/Transforms/SCCP/and-add-shl.ll
@@ -30,8 +30,7 @@
; 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 @@
; 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)
Index: llvm/lib/IR/ConstantRange.cpp
===================================================================
--- llvm/lib/IR/ConstantRange.cpp
+++ llvm/lib/IR/ConstantRange.cpp
@@ -1478,8 +1478,11 @@
APInt OtherMax = Other.getUnsignedMax();
+ bool Neg = getSingleElement() && isAllNegative() &&
+ OtherMax.ule(Max.abs().countl_zero());
+
// There's overflow!
- if (OtherMax.ugt(Max.countl_zero()))
+ if (OtherMax.ugt(Max.countl_zero()) && !Neg)
return getFull();
// FIXME: implement the other tricky cases
@@ -1487,6 +1490,10 @@
Min <<= Other.getUnsignedMin();
Max <<= OtherMax;
+ // For negitive value c, its shifted range is [c<<Other.max) , c<<Other.min)]
+ if (Neg)
+ std::swap(Min, Max);
+
return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156845.549574.patch
Type: text/x-patch
Size: 1746 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230812/da1ee7b5/attachment.bin>
More information about the llvm-commits
mailing list