[PATCH] D64038: [IR][PatternMatch] Introduce m_NegatedPower2() matcher

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 1 14:34:27 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 parent revision: D64037: [IR][PatternMatch] introduce m_Unless() matcher.

It is a good idea to do as much matching inside of `match()` as possible.
If some checking is done afterwards, and we don't fold because of it,
chances are we may have missed some commutative pattern.


Repository:
  rL LLVM

https://reviews.llvm.org/D64038

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


Index: unittests/IR/PatternMatch.cpp
===================================================================
--- unittests/IR/PatternMatch.cpp
+++ unittests/IR/PatternMatch.cpp
@@ -101,6 +101,17 @@
   EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(One));
 }
 
+TEST_F(PatternMatchTest, Power2) {
+  Value *One = IRB.getInt32(128);
+  Value *NegOne = ConstantExpr::getNeg(cast<Constant>(One));
+
+  EXPECT_TRUE(m_Power2().match(One));
+  EXPECT_FALSE(m_Power2().match(NegOne));
+
+  EXPECT_FALSE(m_NegatedPower2().match(One));
+  EXPECT_TRUE(m_NegatedPower2().match(NegOne));
+}
+
 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
@@ -402,6 +402,18 @@
   return V;
 }
 
+struct is_negated_power2 {
+  bool isValue(const APInt &C) { return (-C).isPowerOf2(); }
+};
+/// Match a integer or vector negated power-of-2.
+/// For vectors, this includes constants with undefined elements.
+inline cst_pred_ty<is_negated_power2> m_NegatedPower2() {
+  return cst_pred_ty<is_negated_power2>();
+}
+inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) {
+  return V;
+}
+
 struct is_power2_or_zero {
   bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
 };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64038.207411.patch
Type: text/x-patch
Size: 1423 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190701/1d3a865b/attachment.bin>


More information about the llvm-commits mailing list