Skip to content

Commit c1f6f30

Browse files
committed
[PatternMatch] Add single index InsertValue matcher.
This patch adds a new matcher for single index InsertValue instructions, similar to the existing matcher for ExtractValue. Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D91352
1 parent 07b568a commit c1f6f30

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,29 @@ inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) {
22982298
return ExtractValue_match<Ind, Val_t>(V);
22992299
}
23002300

2301+
/// Matcher for a single index InsertValue instruction.
2302+
template <int Ind, typename T0, typename T1> struct InsertValue_match {
2303+
T0 Op0;
2304+
T1 Op1;
2305+
2306+
InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
2307+
2308+
template <typename OpTy> bool match(OpTy *V) {
2309+
if (auto *I = dyn_cast<InsertValueInst>(V)) {
2310+
return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
2311+
I->getNumIndices() == 1 && Ind == I->getIndices()[0];
2312+
}
2313+
return false;
2314+
}
2315+
};
2316+
2317+
/// Matches a single index InsertValue instruction.
2318+
template <int Ind, typename Val_t, typename Elt_t>
2319+
inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val,
2320+
const Elt_t &Elt) {
2321+
return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
2322+
}
2323+
23012324
/// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
23022325
/// the constant expression
23032326
/// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,27 @@ TEST_F(PatternMatchTest, ConstantPredicateType) {
15811581
match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>()));
15821582
}
15831583

1584+
TEST_F(PatternMatchTest, InsertValue) {
1585+
Type *StructTy = StructType::create(IRB.getContext(),
1586+
{IRB.getInt32Ty(), IRB.getInt64Ty()});
1587+
Value *Ins0 =
1588+
IRB.CreateInsertValue(UndefValue::get(StructTy), IRB.getInt32(20), 0);
1589+
Value *Ins1 = IRB.CreateInsertValue(Ins0, IRB.getInt64(90), 1);
1590+
1591+
EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Value(), m_Value())));
1592+
EXPECT_FALSE(match(Ins0, m_InsertValue<1>(m_Value(), m_Value())));
1593+
EXPECT_FALSE(match(Ins1, m_InsertValue<0>(m_Value(), m_Value())));
1594+
EXPECT_TRUE(match(Ins1, m_InsertValue<1>(m_Value(), m_Value())));
1595+
1596+
EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(20))));
1597+
EXPECT_FALSE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(0))));
1598+
1599+
EXPECT_TRUE(
1600+
match(Ins1, m_InsertValue<1>(m_InsertValue<0>(m_Value(), m_Value()),
1601+
m_SpecificInt(90))));
1602+
EXPECT_FALSE(match(IRB.getInt64(99), m_InsertValue<0>(m_Value(), m_Value())));
1603+
}
1604+
15841605
template <typename T> struct MutableConstTest : PatternMatchTest { };
15851606

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

0 commit comments

Comments
 (0)