[PATCH] D156845: [ConstantRange] Calculate precise range for shl by -1
Allen zhong via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 7 07:52:55 PDT 2023
Allen updated this revision to Diff 547796.
Allen added a comment.
address comment
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D156845/new/
https://reviews.llvm.org/D156845
Files:
llvm/lib/IR/ConstantRange.cpp
llvm/test/Transforms/FunctionSpecialization/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
@@ -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,6 +1478,14 @@
APInt OtherMax = Other.getUnsignedMax();
+ bool Neg = false;
+ if (getSingleElement() && isAllNegative()) {
+ // Negative the number to easily calculate its shifted value.
+ Neg = true;
+ Min = -Min;
+ Max = -Max;
+ }
+
// There's overflow!
if (OtherMax.ugt(Max.countl_zero()))
return getFull();
@@ -1487,6 +1495,11 @@
Min <<= Other.getUnsignedMin();
Max <<= OtherMax;
+ if (Neg) {
+ Min = -Min;
+ Max = -Max;
+ return ConstantRange::getNonEmpty(std::move(Max), std::move(Min) + 1);
+ }
return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156845.547796.patch
Type: text/x-patch
Size: 1796 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230807/aefdbd1d/attachment.bin>
More information about the llvm-commits
mailing list