From 9e43193a301b704e93aed625d0e73adf2176f807 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Thu, 25 Jul 2019 13:34:14 +0000 Subject: [PATCH] [IR][PatternMatch] introduce m_Unless() matcher Summary: I don't think it already exists? I don't see it at least. It is important to have it because else we'll do some checks after `match()`, and that may result in missed folds in commutative nodes. Reviewers: spatel, craig.topper, RKSimon, majnemer Reviewed By: spatel Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D64037 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367016 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/PatternMatch.h | 14 ++++++++++++++ unittests/IR/PatternMatch.cpp | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/llvm/IR/PatternMatch.h b/include/llvm/IR/PatternMatch.h index b6b2fa88f5b..f39678347d3 100644 --- a/include/llvm/IR/PatternMatch.h +++ b/include/llvm/IR/PatternMatch.h @@ -88,6 +88,20 @@ inline class_match m_Undef() { return class_match(); } /// Match an arbitrary Constant and ignore it. inline class_match m_Constant() { return class_match(); } +/// Inverting matcher +template struct match_unless { + Ty M; + + match_unless(const Ty &Matcher) : M(Matcher) {} + + template bool match(ITy *V) { return !M.match(V); } +}; + +/// Match if the inner matcher does *NOT* match. +template inline match_unless m_Unless(const Ty &M) { + return match_unless(M); +} + /// Matching combinators template struct match_combine_or { LTy L; diff --git a/unittests/IR/PatternMatch.cpp b/unittests/IR/PatternMatch.cpp index 600494fba26..8263acb59a0 100644 --- a/unittests/IR/PatternMatch.cpp +++ b/unittests/IR/PatternMatch.cpp @@ -454,6 +454,22 @@ TEST_F(PatternMatchTest, SpecificIntSLE) { .match(NegOne)); } +TEST_F(PatternMatchTest, Unless) { + Value *X = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0)); + + EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X)); + EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X)); + + EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(X)); + EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(X)); + + EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X)); + EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X)); + + EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(X)); + EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X)); +} + TEST_F(PatternMatchTest, CommutativeDeferredValue) { Value *X = IRB.getInt32(1); Value *Y = IRB.getInt32(2); -- 2.40.0