Skip to content

Commit aca7aab

Browse files
committed
[PatternMatch] Add support for matching intrinsics with 5 operands.
Summary: Also adds a test to the pattern matching unit tests. Reviewers: spatel, craig.topper, RKSimon, majnemer, lebedev.ri Reviewed By: spatel Subscribers: merge_guards_bot, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70892
1 parent 9655203 commit aca7aab

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,12 @@ struct m_Intrinsic_Ty<T0, T1, T2, T3> {
17361736
Argument_match<T3>>;
17371737
};
17381738

1739+
template <typename T0, typename T1, typename T2, typename T3, typename T4>
1740+
struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
1741+
using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty,
1742+
Argument_match<T4>>;
1743+
};
1744+
17391745
/// Match intrinsic calls like this:
17401746
/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
17411747
template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
@@ -1766,6 +1772,15 @@ m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
17661772
return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
17671773
}
17681774

1775+
template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1776+
typename T3, typename T4>
1777+
inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty
1778+
m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
1779+
const T4 &Op4) {
1780+
return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
1781+
m_Argument<4>(Op4));
1782+
}
1783+
17691784
// Helper intrinsic matching specializations.
17701785
template <typename Opnd0>
17711786
inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,85 @@ TEST_F(PatternMatchTest, WithOverflowInst) {
11331133
EXPECT_EQ(Add, WOI);
11341134
}
11351135

1136+
TEST_F(PatternMatchTest, IntrinsicMatcher) {
1137+
Value *Name = IRB.CreateAlloca(IRB.getInt8Ty());
1138+
Value *Hash = IRB.getInt64(0);
1139+
Value *Num = IRB.getInt32(1);
1140+
Value *Index = IRB.getInt32(2);
1141+
Value *Step = IRB.getInt64(3);
1142+
1143+
Value *Ops[] = {Name, Hash, Num, Index, Step};
1144+
Module *M = BB->getParent()->getParent();
1145+
Function *TheFn =
1146+
Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step);
1147+
1148+
Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB);
1149+
1150+
// Match without capturing.
1151+
EXPECT_TRUE(match(
1152+
Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1153+
m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1154+
EXPECT_FALSE(match(
1155+
Intrinsic5, m_Intrinsic<Intrinsic::memmove>(
1156+
m_Value(), m_Value(), m_Value(), m_Value(), m_Value())));
1157+
1158+
// Match with capturing.
1159+
Value *Arg1 = nullptr;
1160+
Value *Arg2 = nullptr;
1161+
Value *Arg3 = nullptr;
1162+
Value *Arg4 = nullptr;
1163+
Value *Arg5 = nullptr;
1164+
EXPECT_TRUE(
1165+
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1166+
m_Value(Arg1), m_Value(Arg2), m_Value(Arg3),
1167+
m_Value(Arg4), m_Value(Arg5))));
1168+
EXPECT_EQ(Arg1, Name);
1169+
EXPECT_EQ(Arg2, Hash);
1170+
EXPECT_EQ(Arg3, Num);
1171+
EXPECT_EQ(Arg4, Index);
1172+
EXPECT_EQ(Arg5, Step);
1173+
1174+
// Match specific second argument.
1175+
EXPECT_TRUE(
1176+
match(Intrinsic5,
1177+
m_Intrinsic<Intrinsic::instrprof_increment_step>(
1178+
m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value())));
1179+
EXPECT_FALSE(
1180+
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1181+
m_Value(), m_SpecificInt(10), m_Value(), m_Value(),
1182+
m_Value())));
1183+
1184+
// Match specific third argument.
1185+
EXPECT_TRUE(
1186+
match(Intrinsic5,
1187+
m_Intrinsic<Intrinsic::instrprof_increment_step>(
1188+
m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value())));
1189+
EXPECT_FALSE(
1190+
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1191+
m_Value(), m_Value(), m_SpecificInt(10), m_Value(),
1192+
m_Value())));
1193+
1194+
// Match specific fourth argument.
1195+
EXPECT_TRUE(
1196+
match(Intrinsic5,
1197+
m_Intrinsic<Intrinsic::instrprof_increment_step>(
1198+
m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value())));
1199+
EXPECT_FALSE(
1200+
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1201+
m_Value(), m_Value(), m_Value(), m_SpecificInt(10),
1202+
m_Value())));
1203+
1204+
// Match specific fifth argument.
1205+
EXPECT_TRUE(
1206+
match(Intrinsic5,
1207+
m_Intrinsic<Intrinsic::instrprof_increment_step>(
1208+
m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3))));
1209+
EXPECT_FALSE(
1210+
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>(
1211+
m_Value(), m_Value(), m_Value(), m_Value(),
1212+
m_SpecificInt(10))));
1213+
}
1214+
11361215
template <typename T> struct MutableConstTest : PatternMatchTest { };
11371216

11381217
typedef ::testing::Types<std::tuple<Value*, Instruction*>,

0 commit comments

Comments
 (0)