[PATCH] D156845: [ConstantRange] Calculate precise range for shl by -1
Allen zhong via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 9 18:46:59 PDT 2023
Allen updated this revision to Diff 548847.
Allen added a comment.
Add some comment for codes
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,14 @@
Min <<= Other.getUnsignedMin();
Max <<= OtherMax;
+ if (Neg) {
+ // For negitive value c, its shifted range is [-((-c) <<Other.max) , -((-c)
+ // <<Other.min)]
+ Min = -Min;
+ Max = -Max;
+ return ConstantRange::getNonEmpty(std::move(Max), std::move(Min) + 1);
+ }
+ // For positive value c, its shifted range is [c <<Other.min , c <<Other.max]
return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156845.548847.patch
Type: text/x-patch
Size: 1980 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230810/d8d01807/attachment.bin>
More information about the llvm-commits
mailing list