[llvm] r278570 - Add support to paternmatch for simple const Value cases.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 12 15:16:05 PDT 2016


Author: pete
Date: Fri Aug 12 17:16:05 2016
New Revision: 278570

URL: http://llvm.org/viewvc/llvm-project?rev=278570&view=rev
Log:
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.

Modified:
    llvm/trunk/include/llvm/IR/PatternMatch.h
    llvm/trunk/unittests/IR/PatternMatch.cpp

Modified: llvm/trunk/include/llvm/IR/PatternMatch.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/PatternMatch.h?rev=278570&r1=278569&r2=278570&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/PatternMatch.h (original)
+++ llvm/trunk/include/llvm/IR/PatternMatch.h Fri Aug 12 17:16:05 2016
@@ -294,10 +294,10 @@ template <typename Class> struct bind_ty
 
 /// \brief Match a value, capturing it if we match.
 inline bind_ty<Value> m_Value(Value *&V) { return V; }
+inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
 
 /// \brief Match an instruction, capturing it if we match.
 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
-
 /// \brief Match a binary operator, capturing it if we match.
 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
 
@@ -682,7 +682,7 @@ template <typename SubPattern_t> struct
   Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
 
   template <typename OpTy> bool match(OpTy *V) {
-    if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
+    if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
       return PEO->isExact() && SubPattern.match(V);
     return false;
   }
@@ -706,7 +706,7 @@ struct CmpClass_match {
       : Predicate(Pred), L(LHS), R(RHS) {}
 
   template <typename OpTy> bool match(OpTy *V) {
-    if (Class *I = dyn_cast<Class>(V))
+    if (auto *I = dyn_cast<Class>(V))
       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
         Predicate = I->getPredicate();
         return true;

Modified: llvm/trunk/unittests/IR/PatternMatch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/PatternMatch.cpp?rev=278570&r1=278569&r2=278570&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/PatternMatch.cpp (original)
+++ llvm/trunk/unittests/IR/PatternMatch.cpp Fri Aug 12 17:16:05 2016
@@ -295,4 +295,31 @@ TEST_F(PatternMatchTest, OverflowingBinO
   EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
 }
 
+template <typename T> struct MutableConstTest : PatternMatchTest { };
+
+typedef ::testing::Types<std::tuple<Value*, Instruction*>,
+                         std::tuple<const Value*, const Instruction *>>
+    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.




More information about the llvm-commits mailing list