[llvm] [InstCombine] Drop samesign in `foldLogOpOfMaskedICmps` (PR #125829)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 07:56:17 PST 2025


https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/125829

>From feed257e9d1ea26cb65473679cfb482e1869557a Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 5 Feb 2025 17:54:34 +0800
Subject: [PATCH 1/4] [InstCombine] Add pre-commit tests. NFC.

---
 .../InstCombine/sign-test-and-or.ll           | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/sign-test-and-or.ll b/llvm/test/Transforms/InstCombine/sign-test-and-or.ll
index 65363620563bedd..a9d078e09447e17 100644
--- a/llvm/test/Transforms/InstCombine/sign-test-and-or.ll
+++ b/llvm/test/Transforms/InstCombine/sign-test-and-or.ll
@@ -349,6 +349,29 @@ define i1 @test9_logical(i32 %a) {
   ret i1 %or.cond
 }
 
+define i1 @test9_logical_samesign(i32 %a) {
+; CHECK-LABEL: @test9_logical_samesign(
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp samesign sgt i32 [[A:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP2]]
+;
+  %masked = and i32 %a, -1073741825
+  %cmp1 = icmp eq i32 %masked, 0
+  %cmp2 = icmp samesign sgt i32 %a, -1
+  %or.cond = select i1 %cmp1, i1 true, i1 %cmp2
+  ret i1 %or.cond
+}
+
+define i1 @test_logical_or_icmp_icmp_samesign(i32 %a) {
+; CHECK-LABEL: @test_logical_or_icmp_icmp_samesign(
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp samesign sgt i32 [[A:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP2]]
+;
+  %cmp1 = icmp eq i32 %a, 0
+  %cmp2 = icmp samesign sgt i32 %a, -1
+  %or = select i1 %cmp1, i1 true, i1 %cmp2
+  ret i1 %or
+}
+
 define i1 @test10(i32 %a) {
 ; CHECK-LABEL: @test10(
 ; CHECK-NEXT:    [[OR_COND:%.*]] = icmp ult i32 [[A:%.*]], 2

>From c9dc2952887ec78c549102bfdf64d55e12583548 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 5 Feb 2025 18:02:52 +0800
Subject: [PATCH 2/4] [InstCombine] Drop samesign in `foldLogOpOfMaskedICmps`

---
 llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 5 ++++-
 llvm/test/Transforms/InstCombine/sign-test-and-or.ll    | 4 ++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 8701f7c28a39fc4..29f67f34a017cae 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -610,8 +610,11 @@ static Value *foldLogOpOfMaskedICmps(Value *LHS, Value *RHS, bool IsAnd,
       APInt NewMask = *ConstB & *ConstD;
       if (NewMask == *ConstB)
         return LHS;
-      if (NewMask == *ConstD)
+      if (NewMask == *ConstD) {
+        if (IsLogical)
+          cast<ICmpInst>(RHS)->setSameSign(false);
         return RHS;
+      }
     }
 
     if (Mask & AMask_NotAllOnes) {
diff --git a/llvm/test/Transforms/InstCombine/sign-test-and-or.ll b/llvm/test/Transforms/InstCombine/sign-test-and-or.ll
index a9d078e09447e17..3e9ff63869d646c 100644
--- a/llvm/test/Transforms/InstCombine/sign-test-and-or.ll
+++ b/llvm/test/Transforms/InstCombine/sign-test-and-or.ll
@@ -351,7 +351,7 @@ define i1 @test9_logical(i32 %a) {
 
 define i1 @test9_logical_samesign(i32 %a) {
 ; CHECK-LABEL: @test9_logical_samesign(
-; CHECK-NEXT:    [[CMP2:%.*]] = icmp samesign sgt i32 [[A:%.*]], -1
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp sgt i32 [[A:%.*]], -1
 ; CHECK-NEXT:    ret i1 [[CMP2]]
 ;
   %masked = and i32 %a, -1073741825
@@ -363,7 +363,7 @@ define i1 @test9_logical_samesign(i32 %a) {
 
 define i1 @test_logical_or_icmp_icmp_samesign(i32 %a) {
 ; CHECK-LABEL: @test_logical_or_icmp_icmp_samesign(
-; CHECK-NEXT:    [[CMP2:%.*]] = icmp samesign sgt i32 [[A:%.*]], -1
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp sgt i32 [[A:%.*]], -1
 ; CHECK-NEXT:    ret i1 [[CMP2]]
 ;
   %cmp1 = icmp eq i32 %a, 0

>From 6c08d527d57ca77be4a79cb78f0ac22603a97a33 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 6 Feb 2025 10:03:50 +0800
Subject: [PATCH 3/4] [InstCombine] Handle non-icmp

---
 llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 29f67f34a017cae..02f9e705b89df5f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -611,8 +611,10 @@ static Value *foldLogOpOfMaskedICmps(Value *LHS, Value *RHS, bool IsAnd,
       if (NewMask == *ConstB)
         return LHS;
       if (NewMask == *ConstD) {
-        if (IsLogical)
-          cast<ICmpInst>(RHS)->setSameSign(false);
+        if (IsLogical) {
+          if (auto *ICmp = dyn_cast<ICmpInst>(RHS))
+            ICmp->setSameSign(false);
+        }
         return RHS;
       }
     }

>From 3d4f9bf504143324d3bac20572407f3fc5acbe87 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 6 Feb 2025 23:55:09 +0800
Subject: [PATCH 4/4] [InstCombine] Address review comments. NFC.

---
 llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 02f9e705b89df5f..70f2997f2c2ad31 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -612,8 +612,8 @@ static Value *foldLogOpOfMaskedICmps(Value *LHS, Value *RHS, bool IsAnd,
         return LHS;
       if (NewMask == *ConstD) {
         if (IsLogical) {
-          if (auto *ICmp = dyn_cast<ICmpInst>(RHS))
-            ICmp->setSameSign(false);
+          if (auto *RHSI = dyn_cast<Instruction>(RHS))
+            RHSI->dropPoisonGeneratingFlags();
         }
         return RHS;
       }



More information about the llvm-commits mailing list