From fe45d1419113f4efb51e3ee52793eb19e77c7125 Mon Sep 17 00:00:00 2001 From: Pete Cooper Date: Fri, 12 Aug 2016 22:16:05 +0000 Subject: [PATCH] Add support to paternmatch for simple const Value cases. Pattern match has some paths which can operate on constant instructions, but not all. This adds a version of m_value() to return const Value* and changes ICmp matching to use auto so that it can match both constant and mutable instructions. Tests also included for both mutable and constant ICmpInst matching. This will be used in a future commit to constify ValueTracking.cpp. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278570 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/PatternMatch.h | 6 +++--- unittests/IR/PatternMatch.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/llvm/IR/PatternMatch.h b/include/llvm/IR/PatternMatch.h index 7da9afcf946..9151afd2295 100644 --- a/include/llvm/IR/PatternMatch.h +++ b/include/llvm/IR/PatternMatch.h @@ -294,10 +294,10 @@ template struct bind_ty { /// \brief Match a value, capturing it if we match. inline bind_ty m_Value(Value *&V) { return V; } +inline bind_ty m_Value(const Value *&V) { return V; } /// \brief Match an instruction, capturing it if we match. inline bind_ty m_Instruction(Instruction *&I) { return I; } - /// \brief Match a binary operator, capturing it if we match. inline bind_ty m_BinOp(BinaryOperator *&I) { return I; } @@ -682,7 +682,7 @@ template struct Exact_match { Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} template bool match(OpTy *V) { - if (PossiblyExactOperator *PEO = dyn_cast(V)) + if (auto *PEO = dyn_cast(V)) return PEO->isExact() && SubPattern.match(V); return false; } @@ -706,7 +706,7 @@ struct CmpClass_match { : Predicate(Pred), L(LHS), R(RHS) {} template bool match(OpTy *V) { - if (Class *I = dyn_cast(V)) + if (auto *I = dyn_cast(V)) if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { Predicate = I->getPredicate(); return true; diff --git a/unittests/IR/PatternMatch.cpp b/unittests/IR/PatternMatch.cpp index 1121d6554db..2d1321def7e 100644 --- a/unittests/IR/PatternMatch.cpp +++ b/unittests/IR/PatternMatch.cpp @@ -295,4 +295,31 @@ TEST_F(PatternMatchTest, OverflowingBinOps) { EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); } +template struct MutableConstTest : PatternMatchTest { }; + +typedef ::testing::Types, + std::tuple> + MutableConstTestTypes; +TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes); + +TYPED_TEST(MutableConstTest, ICmp) { + auto &IRB = PatternMatchTest::IRB; + + typedef typename std::tuple_element<0, TypeParam>::type ValueType; + typedef typename std::tuple_element<1, TypeParam>::type InstructionType; + + Value *L = IRB.getInt32(1); + Value *R = IRB.getInt32(2); + ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT; + + ValueType MatchL; + ValueType MatchR; + ICmpInst::Predicate MatchPred; + + EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR)) + .match((InstructionType)IRB.CreateICmp(Pred, L, R))); + EXPECT_EQ(L, MatchL); + EXPECT_EQ(R, MatchR); +} + } // anonymous namespace.