[PATCH] D48279: [PatternMatch] Add m_Store pattern match helper

Sjoerd Meijer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 19 05:56:34 PDT 2018


SjoerdMeijer updated this revision to Diff 151902.
SjoerdMeijer added a comment.

Tweaked the unittests a bit more.


https://reviews.llvm.org/D48279

Files:
  include/llvm/IR/PatternMatch.h
  unittests/IR/PatternMatch.cpp


Index: unittests/IR/PatternMatch.cpp
===================================================================
--- unittests/IR/PatternMatch.cpp
+++ unittests/IR/PatternMatch.cpp
@@ -390,6 +390,33 @@
   EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
 }
 
+TEST_F(PatternMatchTest, LoadStoreOps) {
+  // Create this load/store sequence:
+  //
+  //  %p = alloca i32*
+  //  %0 = load i32*, i32** %p
+  //  store i32 42, i32* %0
+
+  Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
+  Value *LoadInst = IRB.CreateLoad(Alloca);
+  Value *FourtyTwo = IRB.getInt32(42);
+  Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
+  Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
+
+  EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
+  EXPECT_EQ(Alloca, MatchLoad);
+
+  EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
+
+  EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
+                .match(StoreInst));
+  EXPECT_EQ(FourtyTwo, MatchStoreVal);
+  EXPECT_EQ(Alloca, MatchStorePointer);
+
+  EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
+                .match(Alloca));
+}
+
 TEST_F(PatternMatchTest, VectorOps) {
   // Build up small tree of vector operations
   //
Index: include/llvm/IR/PatternMatch.h
===================================================================
--- include/llvm/IR/PatternMatch.h
+++ include/llvm/IR/PatternMatch.h
@@ -1193,6 +1193,33 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Matcher for StoreInst classes
+//
+
+template <typename ValueOp_t, typename PointerOp_t> struct StoreClass_match {
+  ValueOp_t ValueOp;
+  PointerOp_t PointerOp;
+
+  StoreClass_match(const ValueOp_t &ValueOpMatch,
+                   const PointerOp_t &PointerOpMatch) :
+    ValueOp(ValueOpMatch), PointerOp(PointerOpMatch)  {}
+
+  template <typename OpTy> bool match(OpTy *V) {
+    if (auto *LI = dyn_cast<StoreInst>(V))
+      return ValueOp.match(LI->getValueOperand()) &&
+             PointerOp.match(LI->getPointerOperand());
+    return false;
+  }
+};
+
+/// Matches StoreInst.
+template <typename ValueOpTy, typename PointerOpTy>
+inline StoreClass_match<ValueOpTy, PointerOpTy>
+m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
+  return StoreClass_match<ValueOpTy, PointerOpTy>(ValueOp, PointerOp);
+}
+
+//===----------------------------------------------------------------------===//
 // Matchers for unary operators
 //
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48279.151902.patch
Type: text/x-patch
Size: 2515 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180619/ec20b45b/attachment.bin>


More information about the llvm-commits mailing list