Skip to content

Commit 4e066b6

Browse files
authored
[PatternMatch] Match commuted patterns in Signum_match (#121911)
Closes #121776.
1 parent a629d9e commit 4e066b6

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,7 +2870,7 @@ template <typename Opnd_t> struct Signum_match {
28702870
return false;
28712871

28722872
unsigned ShiftWidth = TypeSize - 1;
2873-
Value *OpL = nullptr, *OpR = nullptr;
2873+
Value *Op;
28742874

28752875
// This is the representation of signum we match:
28762876
//
@@ -2882,11 +2882,11 @@ template <typename Opnd_t> struct Signum_match {
28822882
//
28832883
// for i1 values.
28842884

2885-
auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
2886-
auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
2887-
auto Signum = m_Or(LHS, RHS);
2885+
auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth));
2886+
auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth));
2887+
auto Signum = m_c_Or(LHS, RHS);
28882888

2889-
return Signum.match(V) && OpL == OpR && Val.match(OpL);
2889+
return Signum.match(V) && Val.match(Op);
28902890
}
28912891
};
28922892

llvm/test/Transforms/InstCombine/compare-signs.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ define i1 @test4a(i32 %a) {
152152
ret i1 %c
153153
}
154154

155+
define i1 @test4a_commuted(i32 %a) {
156+
; CHECK-LABEL: @test4a_commuted(
157+
; CHECK-NEXT: [[C:%.*]] = icmp slt i32 [[SIGNUM:%.*]], 1
158+
; CHECK-NEXT: ret i1 [[C]]
159+
;
160+
%l = ashr i32 %a, 31
161+
%na = sub i32 0, %a
162+
%r = lshr i32 %na, 31
163+
%signum = or i32 %r, %l
164+
%c = icmp slt i32 %signum, 1
165+
ret i1 %c
166+
}
167+
155168
define <2 x i1> @test4a_vec(<2 x i32> %a) {
156169
; CHECK-LABEL: @test4a_vec(
157170
; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i32> [[A:%.*]], splat (i32 1)

0 commit comments

Comments
 (0)