Skip to content

Commit 831e1ac

Browse files
authored
[MIPatternMatch] Add m_GUMin and m_GUMax (#121068)
And make all unsigned and signed versions of min/max matchers commutative, since we already made a precedent of m_GAdd that is commutative by default.
1 parent d21f300 commit 831e1ac

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,15 +538,27 @@ m_GAShr(const LHS &L, const RHS &R) {
538538
}
539539

540540
template <typename LHS, typename RHS>
541-
inline BinaryOp_match<LHS, RHS, TargetOpcode::G_SMAX, false>
541+
inline BinaryOp_match<LHS, RHS, TargetOpcode::G_SMAX, true>
542542
m_GSMax(const LHS &L, const RHS &R) {
543-
return BinaryOp_match<LHS, RHS, TargetOpcode::G_SMAX, false>(L, R);
543+
return BinaryOp_match<LHS, RHS, TargetOpcode::G_SMAX, true>(L, R);
544544
}
545545

546546
template <typename LHS, typename RHS>
547-
inline BinaryOp_match<LHS, RHS, TargetOpcode::G_SMIN, false>
547+
inline BinaryOp_match<LHS, RHS, TargetOpcode::G_SMIN, true>
548548
m_GSMin(const LHS &L, const RHS &R) {
549-
return BinaryOp_match<LHS, RHS, TargetOpcode::G_SMIN, false>(L, R);
549+
return BinaryOp_match<LHS, RHS, TargetOpcode::G_SMIN, true>(L, R);
550+
}
551+
552+
template <typename LHS, typename RHS>
553+
inline BinaryOp_match<LHS, RHS, TargetOpcode::G_UMAX, true>
554+
m_GUMax(const LHS &L, const RHS &R) {
555+
return BinaryOp_match<LHS, RHS, TargetOpcode::G_UMAX, true>(L, R);
556+
}
557+
558+
template <typename LHS, typename RHS>
559+
inline BinaryOp_match<LHS, RHS, TargetOpcode::G_UMIN, true>
560+
m_GUMin(const LHS &L, const RHS &R) {
561+
return BinaryOp_match<LHS, RHS, TargetOpcode::G_UMIN, true>(L, R);
550562
}
551563

552564
// Helper for unary instructions (G_[ZSA]EXT/G_TRUNC) etc

llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,32 @@ TEST_F(AArch64GISelMITest, MatchBinaryOp) {
224224
auto MIBAddCst = B.buildAdd(s64, MIBCst, Copies[0]);
225225
auto MIBUnmerge = B.buildUnmerge({s32, s32}, B.buildConstant(s64, 42));
226226

227+
// Match min/max, and make sure they're commutative.
228+
auto SMin = B.buildSMin(s64, Copies[2], MIBAdd);
229+
EXPECT_TRUE(mi_match(SMin.getReg(0), *MRI,
230+
m_GSMin(m_GAdd(m_Reg(Src1), m_Reg(Src2)), m_Reg(Src0))));
231+
EXPECT_EQ(Src0, Copies[2]);
232+
EXPECT_EQ(Src1, Copies[0]);
233+
EXPECT_EQ(Src2, Copies[1]);
234+
auto SMax = B.buildSMax(s64, Copies[2], MIBAdd);
235+
EXPECT_TRUE(mi_match(SMax.getReg(0), *MRI,
236+
m_GSMax(m_GAdd(m_Reg(Src1), m_Reg(Src2)), m_Reg(Src0))));
237+
EXPECT_EQ(Src0, Copies[2]);
238+
EXPECT_EQ(Src1, Copies[0]);
239+
EXPECT_EQ(Src2, Copies[1]);
240+
auto UMin = B.buildUMin(s64, Copies[2], MIBAdd);
241+
EXPECT_TRUE(mi_match(UMin.getReg(0), *MRI,
242+
m_GUMin(m_GAdd(m_Reg(Src1), m_Reg(Src2)), m_Reg(Src0))));
243+
EXPECT_EQ(Src0, Copies[2]);
244+
EXPECT_EQ(Src1, Copies[0]);
245+
EXPECT_EQ(Src2, Copies[1]);
246+
auto UMax = B.buildUMax(s64, Copies[2], MIBAdd);
247+
EXPECT_TRUE(mi_match(UMax.getReg(0), *MRI,
248+
m_GUMax(m_GAdd(m_Reg(Src1), m_Reg(Src2)), m_Reg(Src0))));
249+
EXPECT_EQ(Src0, Copies[2]);
250+
EXPECT_EQ(Src1, Copies[0]);
251+
EXPECT_EQ(Src2, Copies[1]);
252+
227253
// m_BinOp with opcode.
228254
// Match binary instruction, opcode and its non-commutative operands.
229255
match = mi_match(MIBAddCst, *MRI,

0 commit comments

Comments
 (0)