[llvm] [InstCombine] Replace Signum_match matcher with checks for scmp with 0 directly (NFCI) (PR #207390)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 3 07:52:37 PDT 2026


https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/207390

>From 84ca370318f4fa15aac63ff5f7791a1414a31a65 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Fri, 3 Jul 2026 08:56:16 -0400
Subject: [PATCH 1/2] [PatternMatch] Remove obsolete Signum_match matcher
 (NFCI)

Now, we just look for scmp with 0 instead. The i1 code for signum is also dead anyway.
---
 llvm/include/llvm/IR/PatternMatch.h           | 40 -------------------
 .../InstCombine/InstCombineCompares.cpp       | 14 ++-----
 .../Transforms/InstCombine/compare-signs.ll   | 38 +++++++++++++++---
 3 files changed, 36 insertions(+), 56 deletions(-)

diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 95f5a9c5bba80..9dd40c9e1beee 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -3162,46 +3162,6 @@ m_c_FMul(const LHS &L, const RHS &R) {
   return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
 }
 
-template <typename Opnd_t> struct Signum_match {
-  Opnd_t Val;
-  Signum_match(const Opnd_t &V) : Val(V) {}
-
-  template <typename OpTy> bool match(OpTy *V) const {
-    unsigned TypeSize = V->getType()->getScalarSizeInBits();
-    if (TypeSize == 0)
-      return false;
-
-    unsigned ShiftWidth = TypeSize - 1;
-    Value *Op;
-
-    // This is the representation of signum we match:
-    //
-    //  signum(x) == (x >> 63) | (-x >>u 63)
-    //
-    // An i1 value is its own signum, so it's correct to match
-    //
-    //  signum(x) == (x >> 0)  | (-x >>u 0)
-    //
-    // for i1 values.
-
-    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) && Val.match(Op);
-  }
-};
-
-/// Matches a signum pattern.
-///
-/// signum(x) =
-///      x >  0  ->  1
-///      x == 0  ->  0
-///      x <  0  -> -1
-template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
-  return Signum_match<Val_t>(V);
-}
-
 template <int Ind, typename Opnd_t> struct ExtractValue_match {
   Opnd_t Val;
   ExtractValue_match(const Opnd_t &V) : Val(V) {}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index e6deb548819e8..0edb528f37344 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1463,9 +1463,11 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
   }
 
   if (C.isOne() && C.getBitWidth() > 1) {
-    // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
+    // icmp slt trunc(scmp(V, 0)) 1 --> icmp slt V, 1
     Value *V = nullptr;
-    if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
+    if (Pred == ICmpInst::ICMP_SLT &&
+        match(X, m_Intrinsic<Intrinsic::scmp>(m_Value(V), m_ZeroInt())) &&
+        V->getType()->getScalarSizeInBits() > 1)
       return new ICmpInst(ICmpInst::ICMP_SLT, V,
                           ConstantInt::get(V->getType(), 1));
   }
@@ -2084,14 +2086,6 @@ Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,
                                                   BinaryOperator *Or,
                                                   const APInt &C) {
   ICmpInst::Predicate Pred = Cmp.getPredicate();
-  if (C.isOne()) {
-    // icmp slt signum(V) 1 --> icmp slt V, 1
-    Value *V = nullptr;
-    if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V))))
-      return new ICmpInst(ICmpInst::ICMP_SLT, V,
-                          ConstantInt::get(V->getType(), 1));
-  }
-
   Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
 
   // (icmp eq/ne (or disjoint x, C0), C1)
diff --git a/llvm/test/Transforms/InstCombine/compare-signs.ll b/llvm/test/Transforms/InstCombine/compare-signs.ll
index bed0f6966dd89..5c9392f08a881 100644
--- a/llvm/test/Transforms/InstCombine/compare-signs.ll
+++ b/llvm/test/Transforms/InstCombine/compare-signs.ll
@@ -142,7 +142,11 @@ define i32 @test3i(i32 %a, i32 %b) nounwind readnone {
 
 define i1 @test4a(i32 %a) {
 ; CHECK-LABEL: @test4a(
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[A:%.*]], 1
+; CHECK-NEXT:    [[L:%.*]] = ashr i32 [[A1:%.*]], 31
+; CHECK-NEXT:    [[NA:%.*]] = sub i32 0, [[A1]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i32 [[NA]], 31
+; CHECK-NEXT:    [[A:%.*]] = or i32 [[L]], [[R]]
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[A]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %l = ashr i32 %a, 31
@@ -155,7 +159,11 @@ define i1 @test4a(i32 %a) {
 
 define i1 @test4a_commuted(i32 %a) {
 ; CHECK-LABEL: @test4a_commuted(
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[SIGNUM:%.*]], 1
+; 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
@@ -168,7 +176,11 @@ define i1 @test4a_commuted(i32 %a) {
 
 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)
+; CHECK-NEXT:    [[L:%.*]] = ashr <2 x i32> [[A1:%.*]], splat (i32 31)
+; CHECK-NEXT:    [[NA:%.*]] = sub <2 x i32> zeroinitializer, [[A1]]
+; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i32> [[NA]], splat (i32 31)
+; CHECK-NEXT:    [[A:%.*]] = or <2 x i32> [[L]], [[R]]
+; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i32> [[A]], splat (i32 1)
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %l = ashr <2 x i32> %a, <i32 31, i32 31>
@@ -181,7 +193,11 @@ define <2 x i1> @test4a_vec(<2 x i32> %a) {
 
 define i1 @test4b(i64 %a) {
 ; CHECK-LABEL: @test4b(
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
+; CHECK-NEXT:    [[L:%.*]] = ashr i64 [[A1:%.*]], 63
+; CHECK-NEXT:    [[NA:%.*]] = sub i64 0, [[A1]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i64 [[NA]], 63
+; CHECK-NEXT:    [[A:%.*]] = or i64 [[L]], [[R]]
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i64 [[A]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %l = ashr i64 %a, 63
@@ -194,7 +210,12 @@ define i1 @test4b(i64 %a) {
 
 define i1 @test4c(i64 %a) {
 ; CHECK-LABEL: @test4c(
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
+; CHECK-NEXT:    [[L:%.*]] = ashr i64 [[A:%.*]], 63
+; CHECK-NEXT:    [[NA:%.*]] = sub i64 0, [[A]]
+; CHECK-NEXT:    [[R:%.*]] = lshr i64 [[NA]], 63
+; CHECK-NEXT:    [[SIGNUM:%.*]] = or i64 [[L]], [[R]]
+; CHECK-NEXT:    [[SIGNUM_TRUNC:%.*]] = trunc nsw i64 [[SIGNUM]] to i32
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[SIGNUM_TRUNC]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %l = ashr i64 %a, 63
@@ -208,7 +229,12 @@ define i1 @test4c(i64 %a) {
 
 define <2 x i1> @test4c_vec(<2 x i64> %a) {
 ; CHECK-LABEL: @test4c_vec(
-; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i64> [[A:%.*]], splat (i64 1)
+; CHECK-NEXT:    [[L:%.*]] = ashr <2 x i64> [[A:%.*]], splat (i64 63)
+; CHECK-NEXT:    [[NA:%.*]] = sub <2 x i64> zeroinitializer, [[A]]
+; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i64> [[NA]], splat (i64 63)
+; CHECK-NEXT:    [[SIGNUM:%.*]] = or <2 x i64> [[L]], [[R]]
+; CHECK-NEXT:    [[SIGNUM_TRUNC:%.*]] = trunc nsw <2 x i64> [[SIGNUM]] to <2 x i32>
+; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i32> [[SIGNUM_TRUNC]], splat (i32 1)
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %l = ashr <2 x i64> %a, <i64 63, i64 63>

>From a99e8e2a98a38c852afb9a6f76962084be142b3b Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Fri, 3 Jul 2026 09:57:10 -0400
Subject: [PATCH 2/2] Fix regression

Adding the signum: or (ashr X, BW-1), (lshr (-X), BW-1) --> scmp(X, 0) fixes this problem!
---
 .../InstCombine/InstCombineAndOrXor.cpp       | 16 ++++++--
 .../Transforms/InstCombine/compare-signs.ll   | 38 +++----------------
 llvm/test/Transforms/InstCombine/or.ll        | 36 ++++++++++++++++++
 3 files changed, 55 insertions(+), 35 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index b6f4a55c07e8a..eb178bd61fd2c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4673,10 +4673,10 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
   if (Value *Res = FoldOrOfSelectSmaxToAbs(I, Builder))
     return replaceInstUsesWith(I, Res);
 
-  // signum: or (ashr X, BW-1), zext (icmp ne|sgt X, 0) --> scmp(X, 0)
-  // The ashr already supplies -1 for negative X, so any predicate that
-  // produces 1 for positive X and 0 for X == 0 yields the same result here.
   {
+    // signum: or (ashr X, BW-1), zext (icmp ne|sgt X, 0) --> scmp(X, 0)
+    // The ashr already supplies -1 for negative X, so any predicate that
+    // produces 1 for positive X and 0 for X == 0 yields the same result here.
     Value *X;
     CmpPredicate SignPred;
     unsigned BitWidth = Ty->getScalarSizeInBits();
@@ -4688,6 +4688,16 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
       return replaceInstUsesWith(
           I, Builder.CreateIntrinsic(Ty, Intrinsic::scmp,
                                      {X, Constant::getNullValue(Ty)}));
+
+    // signum: or (ashr X, BW-1), (lshr (-X), BW-1) --> scmp(X, 0)
+    if (match(&I,
+              m_c_Or(m_AShr(m_Value(X), m_SpecificIntAllowPoison(BitWidth - 1)),
+                     m_LShr(m_Neg(m_Deferred(X)),
+                            m_SpecificIntAllowPoison(BitWidth - 1)))) &&
+        (Op0->hasOneUse() || Op1->hasOneUse()))
+      return replaceInstUsesWith(
+          I, Builder.CreateIntrinsic(Ty, Intrinsic::scmp,
+                                     {X, Constant::getNullValue(Ty)}));
   }
 
   return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/compare-signs.ll b/llvm/test/Transforms/InstCombine/compare-signs.ll
index 5c9392f08a881..bed0f6966dd89 100644
--- a/llvm/test/Transforms/InstCombine/compare-signs.ll
+++ b/llvm/test/Transforms/InstCombine/compare-signs.ll
@@ -142,11 +142,7 @@ define i32 @test3i(i32 %a, i32 %b) nounwind readnone {
 
 define i1 @test4a(i32 %a) {
 ; CHECK-LABEL: @test4a(
-; CHECK-NEXT:    [[L:%.*]] = ashr i32 [[A1:%.*]], 31
-; CHECK-NEXT:    [[NA:%.*]] = sub i32 0, [[A1]]
-; CHECK-NEXT:    [[R:%.*]] = lshr i32 [[NA]], 31
-; CHECK-NEXT:    [[A:%.*]] = or i32 [[L]], [[R]]
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[A]], 1
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[A:%.*]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %l = ashr i32 %a, 31
@@ -159,11 +155,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
@@ -176,11 +168,7 @@ define i1 @test4a_commuted(i32 %a) {
 
 define <2 x i1> @test4a_vec(<2 x i32> %a) {
 ; CHECK-LABEL: @test4a_vec(
-; CHECK-NEXT:    [[L:%.*]] = ashr <2 x i32> [[A1:%.*]], splat (i32 31)
-; CHECK-NEXT:    [[NA:%.*]] = sub <2 x i32> zeroinitializer, [[A1]]
-; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i32> [[NA]], splat (i32 31)
-; CHECK-NEXT:    [[A:%.*]] = or <2 x i32> [[L]], [[R]]
-; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i32> [[A]], splat (i32 1)
+; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i32> [[A:%.*]], splat (i32 1)
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %l = ashr <2 x i32> %a, <i32 31, i32 31>
@@ -193,11 +181,7 @@ define <2 x i1> @test4a_vec(<2 x i32> %a) {
 
 define i1 @test4b(i64 %a) {
 ; CHECK-LABEL: @test4b(
-; CHECK-NEXT:    [[L:%.*]] = ashr i64 [[A1:%.*]], 63
-; CHECK-NEXT:    [[NA:%.*]] = sub i64 0, [[A1]]
-; CHECK-NEXT:    [[R:%.*]] = lshr i64 [[NA]], 63
-; CHECK-NEXT:    [[A:%.*]] = or i64 [[L]], [[R]]
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i64 [[A]], 1
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %l = ashr i64 %a, 63
@@ -210,12 +194,7 @@ define i1 @test4b(i64 %a) {
 
 define i1 @test4c(i64 %a) {
 ; CHECK-LABEL: @test4c(
-; CHECK-NEXT:    [[L:%.*]] = ashr i64 [[A:%.*]], 63
-; CHECK-NEXT:    [[NA:%.*]] = sub i64 0, [[A]]
-; CHECK-NEXT:    [[R:%.*]] = lshr i64 [[NA]], 63
-; CHECK-NEXT:    [[SIGNUM:%.*]] = or i64 [[L]], [[R]]
-; CHECK-NEXT:    [[SIGNUM_TRUNC:%.*]] = trunc nsw i64 [[SIGNUM]] to i32
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[SIGNUM_TRUNC]], 1
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %l = ashr i64 %a, 63
@@ -229,12 +208,7 @@ define i1 @test4c(i64 %a) {
 
 define <2 x i1> @test4c_vec(<2 x i64> %a) {
 ; CHECK-LABEL: @test4c_vec(
-; CHECK-NEXT:    [[L:%.*]] = ashr <2 x i64> [[A:%.*]], splat (i64 63)
-; CHECK-NEXT:    [[NA:%.*]] = sub <2 x i64> zeroinitializer, [[A]]
-; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i64> [[NA]], splat (i64 63)
-; CHECK-NEXT:    [[SIGNUM:%.*]] = or <2 x i64> [[L]], [[R]]
-; CHECK-NEXT:    [[SIGNUM_TRUNC:%.*]] = trunc nsw <2 x i64> [[SIGNUM]] to <2 x i32>
-; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i32> [[SIGNUM_TRUNC]], splat (i32 1)
+; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i64> [[A:%.*]], splat (i64 1)
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %l = ashr <2 x i64> %a, <i64 63, i64 63>
diff --git a/llvm/test/Transforms/InstCombine/or.ll b/llvm/test/Transforms/InstCombine/or.ll
index 4126d653d2ad1..da14ca1f5b8c3 100644
--- a/llvm/test/Transforms/InstCombine/or.ll
+++ b/llvm/test/Transforms/InstCombine/or.ll
@@ -2386,3 +2386,39 @@ define i32 @signum_i32_or_wrong_ext(i32 %x) {
   %r = or i32 %signbit, %sgt0ext
   ret i32 %r
 }
+
+define i32 @signum_i32_or_lshr_neg(i32 %x) {
+; CHECK-LABEL: @signum_i32_or_lshr_neg(
+; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.scmp.i32.i32(i32 [[X:%.*]], i32 0)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %signbit = ashr i32 %x, 31
+  %neg = sub i32 0, %x
+  %zgt0 = lshr i32 %neg, 31
+  %r = or i32 %signbit, %zgt0
+  ret i32 %r
+}
+
+define i32 @signum_i32_or_lshr_neg_commuted(i32 %x) {
+; CHECK-LABEL: @signum_i32_or_lshr_neg_commuted(
+; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.scmp.i32.i32(i32 [[X:%.*]], i32 0)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %signbit = ashr i32 %x, 31
+  %neg = sub i32 0, %x
+  %zgt0 = lshr i32 %neg, 31
+  %r = or i32 %zgt0, %signbit
+  ret i32 %r
+}
+
+define <2 x i5> @signum_v2i5_or_lshr_neg(<2 x i5> %x) {
+; CHECK-LABEL: @signum_v2i5_or_lshr_neg(
+; CHECK-NEXT:    [[R:%.*]] = call <2 x i5> @llvm.scmp.v2i5.v2i5(<2 x i5> [[X:%.*]], <2 x i5> zeroinitializer)
+; CHECK-NEXT:    ret <2 x i5> [[R]]
+;
+  %signbit = ashr <2 x i5> %x, <i5 4, i5 4>
+  %neg = sub <2 x i5> zeroinitializer, %x
+  %zgt0 = lshr <2 x i5> %neg, <i5 4, i5 4>
+  %r = or <2 x i5> %signbit, %zgt0
+  ret <2 x i5> %r
+}



More information about the llvm-commits mailing list