[llvm] 9facaad - [ValueTracking] Improve tracking for constant range of `{s|u}rem C, x`
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 20 08:39:58 PST 2024
Author: Noah Goldstein
Date: 2024-02-20T10:39:34-06:00
New Revision: 9facaaddadd63a1668c212c8a9ef94a5ad4c6629
URL: https://github.com/llvm/llvm-project/commit/9facaaddadd63a1668c212c8a9ef94a5ad4c6629
DIFF: https://github.com/llvm/llvm-project/commit/9facaaddadd63a1668c212c8a9ef94a5ad4c6629.diff
LOG: [ValueTracking] Improve tracking for constant range of `{s|u}rem C, x`
Current we only support `C` as the remainder, but we can also limit
with a constant numerator.
Proofs: https://alive2.llvm.org/ce/z/QB95gU
Closes #82303
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Analysis/ValueTracking/constant-ranges.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1a076adb1bad0a..04f317228b3ea7 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8776,6 +8776,15 @@ static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
// 'srem x, C' produces (-|C|, |C|).
Upper = C->abs();
Lower = (-Upper) + 1;
+ } else if (match(BO.getOperand(0), m_APInt(C))) {
+ if (C->isNegative()) {
+ // 'srem -|C|, x' produces [-|C|, 0].
+ Upper = 1;
+ Lower = *C;
+ } else {
+ // 'srem |C|, x' produces [0, |C|].
+ Upper = *C + 1;
+ }
}
break;
@@ -8783,6 +8792,9 @@ static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
if (match(BO.getOperand(1), m_APInt(C)))
// 'urem x, C' produces [0, C).
Upper = *C;
+ else if (match(BO.getOperand(0), m_APInt(C)))
+ // 'urem C, x' produces [0, C].
+ Upper = *C + 1;
break;
default:
diff --git a/llvm/test/Analysis/ValueTracking/constant-ranges.ll b/llvm/test/Analysis/ValueTracking/constant-ranges.ll
index addfe0fc4c54d6..c440cfad889d3b 100644
--- a/llvm/test/Analysis/ValueTracking/constant-ranges.ll
+++ b/llvm/test/Analysis/ValueTracking/constant-ranges.ll
@@ -133,9 +133,7 @@ define i1 @and_ugt_fail(i8 %xx) {
define i1 @urem_okay(i8 %x) {
; CHECK-LABEL: @urem_okay(
-; CHECK-NEXT: [[VAL:%.*]] = urem i8 34, [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[VAL]], 35
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 true
;
%val = urem i8 34, %x
%r = icmp ule i8 %val, 35
@@ -155,9 +153,7 @@ define i1 @urem_fail(i8 %x) {
define i1 @srem_posC_okay0(i8 %x) {
; CHECK-LABEL: @srem_posC_okay0(
-; CHECK-NEXT: [[VAL:%.*]] = srem i8 34, [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = icmp sle i8 [[VAL]], 34
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 true
;
%val = srem i8 34, %x
%r = icmp sle i8 %val, 34
@@ -166,9 +162,7 @@ define i1 @srem_posC_okay0(i8 %x) {
define i1 @srem_posC_okay1(i8 %x) {
; CHECK-LABEL: @srem_posC_okay1(
-; CHECK-NEXT: [[VAL:%.*]] = srem i8 34, [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = icmp sge i8 [[VAL]], -3
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 true
;
%val = srem i8 34, %x
%r = icmp sge i8 %val, -3
@@ -177,9 +171,7 @@ define i1 @srem_posC_okay1(i8 %x) {
define i1 @srem_negC_okay0(i8 %x) {
; CHECK-LABEL: @srem_negC_okay0(
-; CHECK-NEXT: [[VAL:%.*]] = srem i8 -34, [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = icmp sle i8 [[VAL]], 0
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 true
;
%val = srem i8 -34, %x
%r = icmp sle i8 %val, 0
@@ -188,9 +180,7 @@ define i1 @srem_negC_okay0(i8 %x) {
define i1 @srem_negC_okay1(i8 %x) {
; CHECK-LABEL: @srem_negC_okay1(
-; CHECK-NEXT: [[VAL:%.*]] = srem i8 -34, [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = icmp sge i8 [[VAL]], -34
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 true
;
%val = srem i8 -34, %x
%r = icmp sge i8 %val, -34
More information about the llvm-commits
mailing list