[llvm] [InstSimplify] Provide information about the range of possible values that `ucmp`/`scmp` can return (PR #96410)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 23 07:36:34 PDT 2024


https://github.com/Poseydon42 updated https://github.com/llvm/llvm-project/pull/96410

>From 57ff233b2ad52471cc7482e3a9ad3ebb1ffa3a3e Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Sun, 23 Jun 2024 00:04:24 +0100
Subject: [PATCH 1/3] [InstSimplify] Added tests for comparisons that are known
 to be true/false based on the range of possible values of UCMP/SCMP

---
 llvm/test/Transforms/InstSimplify/uscmp.ll | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/llvm/test/Transforms/InstSimplify/uscmp.ll b/llvm/test/Transforms/InstSimplify/uscmp.ll
index db96aa39192d6..363c351e63ba7 100644
--- a/llvm/test/Transforms/InstSimplify/uscmp.ll
+++ b/llvm/test/Transforms/InstSimplify/uscmp.ll
@@ -229,6 +229,30 @@ define <4 x i8> @ucmp_with_addition_vec(<4 x i32> %x) {
   ret <4 x i8> %2
 }
 
+define i1 @scmp_eq_4(i32 %x, i32 %y) {
+; CHECK-LABEL: define i1 @scmp_eq_4(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 4
+; CHECK-NEXT:    ret i1 [[TMP2]]
+;
+  %1 = call i8 @llvm.scmp(i32 %x, i32 %y)
+  %2 = icmp eq i8 %1, 4
+  ret i1 %2
+}
+
+define i1 @ucmp_ne_negative_2(i32 %x, i32 %y) {
+; CHECK-LABEL: define i1 @ucmp_ne_negative_2(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i8 [[TMP1]], -2
+; CHECK-NEXT:    ret i1 [[TMP2]]
+;
+  %1 = call i8 @llvm.ucmp(i32 %x, i32 %y)
+  %2 = icmp ne i8 %1, -2
+  ret i1 %2
+}
+
 ; Negative case: mismatched signedness of predicates
 define i8 @scmp_known_ugt(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i8 @scmp_known_ugt(

>From efe07d839ced486982eb3be9255b364a1324ed18 Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Sun, 23 Jun 2024 00:05:26 +0100
Subject: [PATCH 2/3] [InstSimplify] Provide information about the range of
 values that UCMP/SCMP can produce to fold some comparisons

---
 llvm/lib/Analysis/ValueTracking.cpp        | 4 ++++
 llvm/test/Transforms/InstSimplify/uscmp.ll | 8 ++------
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 0df061923f625..a30a5f86d9515 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9393,6 +9393,10 @@ static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) {
     if (!II.getParent() || !II.getFunction())
       break;
     return getVScaleRange(II.getFunction(), Width);
+  case Intrinsic::scmp:
+  case Intrinsic::ucmp:
+    return ConstantRange::getNonEmpty(APInt::getAllOnes(Width),
+                                      APInt::getOneBitSet(Width, 0));
   default:
     break;
   }
diff --git a/llvm/test/Transforms/InstSimplify/uscmp.ll b/llvm/test/Transforms/InstSimplify/uscmp.ll
index 363c351e63ba7..47720060acb52 100644
--- a/llvm/test/Transforms/InstSimplify/uscmp.ll
+++ b/llvm/test/Transforms/InstSimplify/uscmp.ll
@@ -232,9 +232,7 @@ define <4 x i8> @ucmp_with_addition_vec(<4 x i32> %x) {
 define i1 @scmp_eq_4(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i1 @scmp_eq_4(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[X]], i32 [[Y]])
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 4
-; CHECK-NEXT:    ret i1 [[TMP2]]
+; CHECK-NEXT:    ret i1 false
 ;
   %1 = call i8 @llvm.scmp(i32 %x, i32 %y)
   %2 = icmp eq i8 %1, 4
@@ -244,9 +242,7 @@ define i1 @scmp_eq_4(i32 %x, i32 %y) {
 define i1 @ucmp_ne_negative_2(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i1 @ucmp_ne_negative_2(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[X]], i32 [[Y]])
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i8 [[TMP1]], -2
-; CHECK-NEXT:    ret i1 [[TMP2]]
+; CHECK-NEXT:    ret i1 true
 ;
   %1 = call i8 @llvm.ucmp(i32 %x, i32 %y)
   %2 = icmp ne i8 %1, -2

>From 77ff1589855f59e4ed31f149e2e8907fc97ff985 Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Sun, 23 Jun 2024 15:36:13 +0100
Subject: [PATCH 3/3] Fixed range

---
 llvm/lib/Analysis/ValueTracking.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index a30a5f86d9515..777b2792a1ed2 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9396,7 +9396,7 @@ static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) {
   case Intrinsic::scmp:
   case Intrinsic::ucmp:
     return ConstantRange::getNonEmpty(APInt::getAllOnes(Width),
-                                      APInt::getOneBitSet(Width, 0));
+                                      APInt(Width, 2));
   default:
     break;
   }



More information about the llvm-commits mailing list