[llvm] [InstCombine] Fold negation of calls to `ucmp/scmp` by swapping its operands (PR #98360)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 10 11:51:24 PDT 2024


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

>From 5f688810e86220ad4ee8eed3995868f07589311c Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Wed, 10 Jul 2024 18:52:46 +0100
Subject: [PATCH 1/3] [InstCombine] Add tests for folding -ucmp/scmp(x, y) =>
 ucmp/scmp(y, x)

---
 llvm/test/Transforms/InstCombine/scmp.ll | 13 +++++++++++++
 llvm/test/Transforms/InstCombine/ucmp.ll | 13 +++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/scmp.ll b/llvm/test/Transforms/InstCombine/scmp.ll
index 4f903a79afd5d..b2967d966ee99 100644
--- a/llvm/test/Transforms/InstCombine/scmp.ll
+++ b/llvm/test/Transforms/InstCombine/scmp.ll
@@ -154,3 +154,16 @@ define i1 @scmp_sle_neg_1(i32 %x, i32 %y) {
   %2 = icmp sle i8 %1, -1
   ret i1 %2
 }
+
+; ========== Fold -scmp(x, y) => scmp(y, x) ==========
+define i8 @scmp_negated(i32 %x, i32 %y) {
+; CHECK-LABEL: define i8 @scmp_negated(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[TMP2:%.*]] = sub nsw i8 0, [[TMP1]]
+; CHECK-NEXT:    ret i8 [[TMP2]]
+;
+  %1 = call i8 @llvm.scmp(i32 %x, i32 %y)
+  %2 = sub i8 0, %1
+  ret i8 %2
+}
diff --git a/llvm/test/Transforms/InstCombine/ucmp.ll b/llvm/test/Transforms/InstCombine/ucmp.ll
index 9ab67560c9117..5e422042d65e8 100644
--- a/llvm/test/Transforms/InstCombine/ucmp.ll
+++ b/llvm/test/Transforms/InstCombine/ucmp.ll
@@ -154,3 +154,16 @@ define i1 @ucmp_sle_neg_1(i32 %x, i32 %y) {
   %2 = icmp sle i8 %1, -1
   ret i1 %2
 }
+
+; ========== Fold -ucmp(x, y) => ucmp(y, x) ==========
+define i8 @ucmp_negated(i32 %x, i32 %y) {
+; CHECK-LABEL: define i8 @ucmp_negated(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[TMP2:%.*]] = sub nsw i8 0, [[TMP1]]
+; CHECK-NEXT:    ret i8 [[TMP2]]
+;
+  %1 = call i8 @llvm.ucmp(i32 %x, i32 %y)
+  %2 = sub i8 0, %1
+  ret i8 %2
+}

>From de83a0b200d3e6169228928fd3d23525952e83a5 Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Wed, 10 Jul 2024 18:54:24 +0100
Subject: [PATCH 2/3] [InstCombine] Fold -ucmp/scmp(x, y) into ucmp/scmp(y, x)

---
 llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp | 5 +++++
 llvm/test/Transforms/InstCombine/scmp.ll               | 3 +--
 llvm/test/Transforms/InstCombine/ucmp.ll               | 3 +--
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index b3426562a4d87..9ad594a6d7d61 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -222,6 +222,11 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
     }
     break;
   }
+  case Instruction::Call:
+    if (auto *CI = dyn_cast<CmpIntrinsic>(I))
+      return Builder.CreateIntrinsic(CI->getType(), CI->getIntrinsicID(),
+                                     {CI->getRHS(), CI->getLHS()});
+    break;
   default:
     break; // Other instructions require recursive reasoning.
   }
diff --git a/llvm/test/Transforms/InstCombine/scmp.ll b/llvm/test/Transforms/InstCombine/scmp.ll
index b2967d966ee99..80b32e73a757d 100644
--- a/llvm/test/Transforms/InstCombine/scmp.ll
+++ b/llvm/test/Transforms/InstCombine/scmp.ll
@@ -159,8 +159,7 @@ define i1 @scmp_sle_neg_1(i32 %x, i32 %y) {
 define i8 @scmp_negated(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i8 @scmp_negated(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[X]], i32 [[Y]])
-; CHECK-NEXT:    [[TMP2:%.*]] = sub nsw i8 0, [[TMP1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[Y]], i32 [[X]])
 ; CHECK-NEXT:    ret i8 [[TMP2]]
 ;
   %1 = call i8 @llvm.scmp(i32 %x, i32 %y)
diff --git a/llvm/test/Transforms/InstCombine/ucmp.ll b/llvm/test/Transforms/InstCombine/ucmp.ll
index 5e422042d65e8..74313403d7c0c 100644
--- a/llvm/test/Transforms/InstCombine/ucmp.ll
+++ b/llvm/test/Transforms/InstCombine/ucmp.ll
@@ -159,8 +159,7 @@ define i1 @ucmp_sle_neg_1(i32 %x, i32 %y) {
 define i8 @ucmp_negated(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i8 @ucmp_negated(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[X]], i32 [[Y]])
-; CHECK-NEXT:    [[TMP2:%.*]] = sub nsw i8 0, [[TMP1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[Y]], i32 [[X]])
 ; CHECK-NEXT:    ret i8 [[TMP2]]
 ;
   %1 = call i8 @llvm.ucmp(i32 %x, i32 %y)

>From 6c68deb005af476aee496da1d593335a4c99cb33 Mon Sep 17 00:00:00 2001
From: Poseydon42 <vvmposeydon at gmail.com>
Date: Wed, 10 Jul 2024 19:50:56 +0100
Subject: [PATCH 3/3] Do not fold when the original result is used more than
 once

---
 .../InstCombine/InstCombineNegator.cpp          |  2 +-
 llvm/test/Transforms/InstCombine/scmp.ll        | 17 +++++++++++++++++
 llvm/test/Transforms/InstCombine/ucmp.ll        | 17 +++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index 9ad594a6d7d61..e4895b59f4b4a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -223,7 +223,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
     break;
   }
   case Instruction::Call:
-    if (auto *CI = dyn_cast<CmpIntrinsic>(I))
+    if (auto *CI = dyn_cast<CmpIntrinsic>(I); CI && CI->hasOneUse())
       return Builder.CreateIntrinsic(CI->getType(), CI->getIntrinsicID(),
                                      {CI->getRHS(), CI->getLHS()});
     break;
diff --git a/llvm/test/Transforms/InstCombine/scmp.ll b/llvm/test/Transforms/InstCombine/scmp.ll
index 80b32e73a757d..2523872562cad 100644
--- a/llvm/test/Transforms/InstCombine/scmp.ll
+++ b/llvm/test/Transforms/InstCombine/scmp.ll
@@ -1,6 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 
+declare void @use(i8 %value)
+
 define i1 @scmp_eq_0(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i1 @scmp_eq_0(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
@@ -166,3 +168,18 @@ define i8 @scmp_negated(i32 %x, i32 %y) {
   %2 = sub i8 0, %1
   ret i8 %2
 }
+
+; Negative test: do not fold if the original scmp result is already used
+define i8 @scmp_negated_multiuse(i32 %x, i32 %y) {
+; CHECK-LABEL: define i8 @scmp_negated_multiuse(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.scmp.i8.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    call void @use(i8 [[TMP1]])
+; CHECK-NEXT:    [[TMP2:%.*]] = sub nsw i8 0, [[TMP1]]
+; CHECK-NEXT:    ret i8 [[TMP2]]
+;
+  %1 = call i8 @llvm.scmp(i32 %x, i32 %y)
+  call void @use(i8 %1)
+  %2 = sub i8 0, %1
+  ret i8 %2
+}
diff --git a/llvm/test/Transforms/InstCombine/ucmp.ll b/llvm/test/Transforms/InstCombine/ucmp.ll
index 74313403d7c0c..7210455094baa 100644
--- a/llvm/test/Transforms/InstCombine/ucmp.ll
+++ b/llvm/test/Transforms/InstCombine/ucmp.ll
@@ -1,6 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 
+declare void @use(i8 %value)
+
 define i1 @ucmp_eq_0(i32 %x, i32 %y) {
 ; CHECK-LABEL: define i1 @ucmp_eq_0(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
@@ -166,3 +168,18 @@ define i8 @ucmp_negated(i32 %x, i32 %y) {
   %2 = sub i8 0, %1
   ret i8 %2
 }
+
+; Negative test: do not fold if the original ucmp result is already used
+define i8 @ucmp_negated_multiuse(i32 %x, i32 %y) {
+; CHECK-LABEL: define i8 @ucmp_negated_multiuse(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i8 @llvm.ucmp.i8.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    call void @use(i8 [[TMP1]])
+; CHECK-NEXT:    [[TMP2:%.*]] = sub nsw i8 0, [[TMP1]]
+; CHECK-NEXT:    ret i8 [[TMP2]]
+;
+  %1 = call i8 @llvm.ucmp(i32 %x, i32 %y)
+  call void @use(i8 %1)
+  %2 = sub i8 0, %1
+  ret i8 %2
+}



More information about the llvm-commits mailing list