[PATCH] D64037: [IR][PatternMatch] introduce m_Unless() matcher

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 1 14:34:26 PDT 2019


lebedev.ri created this revision.
lebedev.ri added reviewers: spatel, craig.topper, RKSimon.
lebedev.ri added a project: LLVM.
lebedev.ri added a child revision: D64038: [IR][PatternMatch] Introduce m_NegatedPower2() matcher.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D64037

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


Index: unittests/IR/PatternMatch.cpp
===================================================================
--- unittests/IR/PatternMatch.cpp
+++ unittests/IR/PatternMatch.cpp
@@ -85,6 +85,22 @@
   EXPECT_FALSE(m_SpecificInt_ULT(APInt(BitWidth, -1)).match(NegOne));
 }
 
+TEST_F(PatternMatchTest, Unless) {
+  Value *One = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0));
+
+  EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(One));
+  EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(One));
+
+  EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(One));
+  EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(One));
+
+  EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(One));
+  EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(One));
+
+  EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(One));
+  EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(One));
+}
+
 TEST_F(PatternMatchTest, CommutativeDeferredValue) {
   Value *X = IRB.getInt32(1);
   Value *Y = IRB.getInt32(2);
Index: include/llvm/IR/PatternMatch.h
===================================================================
--- include/llvm/IR/PatternMatch.h
+++ include/llvm/IR/PatternMatch.h
@@ -88,6 +88,20 @@
 /// Match an arbitrary Constant and ignore it.
 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
 
+/// Inverting matcher
+template <typename Ty> struct match_unless {
+  Ty M;
+
+  match_unless(const Ty &Matcher) : M(Matcher) {}
+
+  template <typename ITy> bool match(ITy *V) { return !M.match(V); }
+};
+
+/// Match if the inner matcher does *NOT* match.
+template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
+  return match_unless<Ty>(M);
+}
+
 /// Matching combinators
 template <typename LTy, typename RTy> struct match_combine_or {
   LTy L;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64037.207410.patch
Type: text/x-patch
Size: 1766 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190701/c6618e67/attachment.bin>


More information about the llvm-commits mailing list