llvm.org GIT mirror llvm / 6bfac2f
[PatternMatch] Add m_Store pattern match helper Differential Revision: https://reviews.llvm.org/D48279 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335100 91177308-0d34-0410-b5e6-96231b3b80d8 Sjoerd Meijer 1 year, 4 months ago
2 changed file(s) with 63 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11891189 /// Matches LoadInst.
11901190 template inline LoadClass_match m_Load(const OpTy &Op) {
11911191 return LoadClass_match(Op);
1192 }
1193
1194 //===----------------------------------------------------------------------===//
1195 // Matcher for StoreInst classes
1196 //
1197
1198 template struct StoreClass_match {
1199 ValueOp_t ValueOp;
1200 PointerOp_t PointerOp;
1201
1202 StoreClass_match(const ValueOp_t &ValueOpMatch,
1203 const PointerOp_t &PointerOpMatch) :
1204 ValueOp(ValueOpMatch), PointerOp(PointerOpMatch) {}
1205
1206 template bool match(OpTy *V) {
1207 if (auto *LI = dyn_cast(V))
1208 return ValueOp.match(LI->getValueOperand()) &&
1209 PointerOp.match(LI->getPointerOperand());
1210 return false;
1211 }
1212 };
1213
1214 /// Matches StoreInst.
1215 template
1216 inline StoreClass_match
1217 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1218 return StoreClass_match(ValueOp, PointerOp);
11921219 }
11931220
11941221 //===----------------------------------------------------------------------===//
389389 EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R)));
390390 }
391391
392 TEST_F(PatternMatchTest, LoadStoreOps) {
393 // Create this load/store sequence:
394 //
395 // %p = alloca i32*
396 // %0 = load i32*, i32** %p
397 // store i32 42, i32* %0
398
399 Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty());
400 Value *LoadInst = IRB.CreateLoad(Alloca);
401 Value *FourtyTwo = IRB.getInt32(42);
402 Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca);
403 Value *MatchLoad, *MatchStoreVal, *MatchStorePointer;
404
405 EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst));
406 EXPECT_EQ(Alloca, MatchLoad);
407
408 EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst));
409
410 EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca));
411
412 EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
413 .match(StoreInst));
414 EXPECT_EQ(FourtyTwo, MatchStoreVal);
415 EXPECT_EQ(Alloca, MatchStorePointer);
416
417 EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer))
418 .match(Alloca));
419
420 EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca))
421 .match(StoreInst));
422 EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo))
423 .match(StoreInst));
424 EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca))
425 .match(StoreInst));
426 }
427
392428 TEST_F(PatternMatchTest, VectorOps) {
393429 // Build up small tree of vector operations
394430 //