diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -88,6 +88,20 @@ /// 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/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp --- a/llvm/unittests/IR/PatternMatch.cpp +++ b/llvm/unittests/IR/PatternMatch.cpp @@ -454,6 +454,22 @@ .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);