[llvm] r367017 - [IR][PatternMatch] Introduce m_NegatedPower2() matcher

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 06:34:24 PDT 2019


Author: lebedevri
Date: Thu Jul 25 06:34:24 2019
New Revision: 367017

URL: http://llvm.org/viewvc/llvm-project?rev=367017&view=rev
Log:
[IR][PatternMatch] Introduce m_NegatedPower2() matcher

Summary:
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.

Reviewers: spatel, craig.topper, RKSimon

Reviewed By: spatel, RKSimon

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D64038

Modified:
    llvm/trunk/include/llvm/IR/PatternMatch.h
    llvm/trunk/unittests/IR/PatternMatch.cpp

Modified: llvm/trunk/include/llvm/IR/PatternMatch.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/PatternMatch.h?rev=367017&r1=367016&r2=367017&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/PatternMatch.h (original)
+++ llvm/trunk/include/llvm/IR/PatternMatch.h Thu Jul 25 06:34:24 2019
@@ -411,6 +411,18 @@ inline api_pred_ty<is_power2> m_Power2(c
   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(); }
 };

Modified: llvm/trunk/unittests/IR/PatternMatch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/PatternMatch.cpp?rev=367017&r1=367016&r2=367017&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/PatternMatch.cpp (original)
+++ llvm/trunk/unittests/IR/PatternMatch.cpp Thu Jul 25 06:34:24 2019
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/IR/PatternMatch.h"
+#include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/IR/BasicBlock.h"
@@ -470,6 +471,26 @@ TEST_F(PatternMatchTest, Unless) {
   EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X));
 }
 
+TEST_F(PatternMatchTest, Power2) {
+  Value *C128 = IRB.getInt32(128);
+  Value *CNeg128 = ConstantExpr::getNeg(cast<Constant>(C128));
+
+  EXPECT_TRUE(m_Power2().match(C128));
+  EXPECT_FALSE(m_Power2().match(CNeg128));
+
+  EXPECT_FALSE(m_NegatedPower2().match(C128));
+  EXPECT_TRUE(m_NegatedPower2().match(CNeg128));
+
+  Value *CIntMin = IRB.getInt64(APSInt::getSignedMinValue(64).getSExtValue());
+  Value *CNegIntMin = ConstantExpr::getNeg(cast<Constant>(CIntMin));
+
+  EXPECT_TRUE(m_Power2().match(CIntMin));
+  EXPECT_TRUE(m_Power2().match(CNegIntMin));
+
+  EXPECT_TRUE(m_NegatedPower2().match(CIntMin));
+  EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin));
+}
+
 TEST_F(PatternMatchTest, CommutativeDeferredValue) {
   Value *X = IRB.getInt32(1);
   Value *Y = IRB.getInt32(2);




More information about the llvm-commits mailing list