[PATCH] D91352: [PatternMatch] Add single index InsertValue matcher.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 12 06:47:20 PST 2020
fhahn created this revision.
fhahn added reviewers: RKSimon, spatel, lebedev.ri.
Herald added subscribers: dexonsmith, arphaman.
Herald added a project: LLVM.
fhahn requested review of this revision.
This patch adds a new matcher for single index InsertValue instructions,
similar to the existing matcher for ExtractValue.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D91352
Files:
llvm/include/llvm/IR/PatternMatch.h
llvm/unittests/IR/PatternMatch.cpp
Index: llvm/unittests/IR/PatternMatch.cpp
===================================================================
--- llvm/unittests/IR/PatternMatch.cpp
+++ llvm/unittests/IR/PatternMatch.cpp
@@ -1581,6 +1581,27 @@
match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>()));
}
+TEST_F(PatternMatchTest, InsertValue) {
+ Type *StructTy = StructType::create(IRB.getContext(),
+ {IRB.getInt32Ty(), IRB.getInt64Ty()});
+ Value *Ins0 =
+ IRB.CreateInsertValue(UndefValue::get(StructTy), IRB.getInt32(20), 0);
+ Value *Ins1 = IRB.CreateInsertValue(Ins0, IRB.getInt64(90), 1);
+
+ EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Value(), m_Value())));
+ EXPECT_FALSE(match(Ins0, m_InsertValue<1>(m_Value(), m_Value())));
+ EXPECT_FALSE(match(Ins1, m_InsertValue<0>(m_Value(), m_Value())));
+ EXPECT_TRUE(match(Ins1, m_InsertValue<1>(m_Value(), m_Value())));
+
+ EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(20))));
+ EXPECT_FALSE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(0))));
+
+ EXPECT_TRUE(
+ match(Ins1, m_InsertValue<1>(m_InsertValue<0>(m_Value(), m_Value()),
+ m_SpecificInt(90))));
+ EXPECT_FALSE(match(IRB.getInt64(99), m_InsertValue<0>(m_Value(), m_Value())));
+}
+
template <typename T> struct MutableConstTest : PatternMatchTest { };
typedef ::testing::Types<std::tuple<Value*, Instruction*>,
Index: llvm/include/llvm/IR/PatternMatch.h
===================================================================
--- llvm/include/llvm/IR/PatternMatch.h
+++ llvm/include/llvm/IR/PatternMatch.h
@@ -2298,6 +2298,29 @@
return ExtractValue_match<Ind, Val_t>(V);
}
+/// Matcher for a single index InsertValue instruction.
+template <int Ind, typename T0, typename T1> struct InsertValue_match {
+ T0 Op1;
+ T1 Op2;
+
+ InsertValue_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
+
+ template <typename OpTy> bool match(OpTy *V) {
+ if (auto *I = dyn_cast<InsertValueInst>(V)) {
+ return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
+ I->getNumIndices() == 1 && Ind == I->getIndices()[0];
+ }
+ return false;
+ }
+};
+
+/// Matches a single index InsertValue instruction.
+template <int Ind, typename Val_t, typename Elt_t>
+inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val,
+ const Elt_t &Elt) {
+ return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
+}
+
/// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
/// the constant expression
/// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91352.304822.patch
Type: text/x-patch
Size: 2724 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201112/74090405/attachment-0001.bin>
More information about the llvm-commits
mailing list