[llvm] [PatternMatch] Match commuted patterns in `Signum_match` (PR #121911)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 02:25:44 PST 2025


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/121911

Closes https://github.com/llvm/llvm-project/issues/121776.


>From aa5e48a9b71f83adeeea35141f9dbcb206572330 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 7 Jan 2025 18:15:46 +0800
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests. NFC.

---
 .../Transforms/InstCombine/compare-signs.ll     | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/compare-signs.ll b/llvm/test/Transforms/InstCombine/compare-signs.ll
index 9703b47b44d0c2..30cea688d1a731 100644
--- a/llvm/test/Transforms/InstCombine/compare-signs.ll
+++ b/llvm/test/Transforms/InstCombine/compare-signs.ll
@@ -152,6 +152,23 @@ define i1 @test4a(i32 %a) {
   ret i1 %c
 }
 
+define i1 @test4a_commuted(i32 %a) {
+; CHECK-LABEL: @test4a_commuted(
+; CHECK-NEXT:    [[L:%.*]] = ashr i32 [[A:%.*]], 31
+; CHECK-NEXT:    [[NA:%.*]] = sub i32 0, [[A]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i32 [[NA]], 31
+; CHECK-NEXT:    [[SIGNUM:%.*]] = or i32 [[R]], [[L]]
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[SIGNUM]], 1
+; CHECK-NEXT:    ret i1 [[C]]
+;
+  %l = ashr i32 %a, 31
+  %na = sub i32 0, %a
+  %r = lshr i32 %na, 31
+  %signum = or i32 %r, %l
+  %c = icmp slt i32 %signum, 1
+  ret i1 %c
+}
+
 define <2 x i1> @test4a_vec(<2 x i32> %a) {
 ; CHECK-LABEL: @test4a_vec(
 ; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i32> [[A:%.*]], splat (i32 1)

>From f5783208ca1dbf8877485ea73f95a16f53d9d6d7 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 7 Jan 2025 18:24:29 +0800
Subject: [PATCH 2/2] [PatternMatch] Match commuted patterns in `Signum_match`

---
 llvm/include/llvm/IR/PatternMatch.h               | 10 +++++-----
 llvm/test/Transforms/InstCombine/compare-signs.ll |  6 +-----
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index b37f967191aaa8..cd9a36029e6dbd 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -2870,7 +2870,7 @@ template <typename Opnd_t> struct Signum_match {
       return false;
 
     unsigned ShiftWidth = TypeSize - 1;
-    Value *OpL = nullptr, *OpR = nullptr;
+    Value *Op;
 
     // This is the representation of signum we match:
     //
@@ -2882,11 +2882,11 @@ template <typename Opnd_t> struct Signum_match {
     //
     // for i1 values.
 
-    auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
-    auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
-    auto Signum = m_Or(LHS, RHS);
+    auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
+    auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
+    auto Signum = m_c_Or(LHS, RHS);
 
-    return Signum.match(V) && OpL == OpR && Val.match(OpL);
+    return Signum.match(V) && Val.match(Op);
   }
 };
 
diff --git a/llvm/test/Transforms/InstCombine/compare-signs.ll b/llvm/test/Transforms/InstCombine/compare-signs.ll
index 30cea688d1a731..59ec9adb30b9ee 100644
--- a/llvm/test/Transforms/InstCombine/compare-signs.ll
+++ b/llvm/test/Transforms/InstCombine/compare-signs.ll
@@ -154,11 +154,7 @@ define i1 @test4a(i32 %a) {
 
 define i1 @test4a_commuted(i32 %a) {
 ; CHECK-LABEL: @test4a_commuted(
-; CHECK-NEXT:    [[L:%.*]] = ashr i32 [[A:%.*]], 31
-; CHECK-NEXT:    [[NA:%.*]] = sub i32 0, [[A]]
-; CHECK-NEXT:    [[R:%.*]] = lshr i32 [[NA]], 31
-; CHECK-NEXT:    [[SIGNUM:%.*]] = or i32 [[R]], [[L]]
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[SIGNUM]], 1
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[SIGNUM:%.*]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %l = ashr i32 %a, 31



More information about the llvm-commits mailing list