[PATCH] D34593: [IR][AssumptionCache] Add m_Shift and m_BitwiseLogic matchers to replace a couple m_CombineOr

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 23 23:28:09 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL306206: [IR][AssumptionCache] Add m_Shift and m_BitwiseLogic matchers to replace a… (authored by ctopper).

Changed prior to commit:
  https://reviews.llvm.org/D34593?vs=103830&id=103831#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D34593

Files:
  llvm/trunk/include/llvm/IR/Instruction.h
  llvm/trunk/include/llvm/IR/PatternMatch.h
  llvm/trunk/lib/Analysis/AssumptionCache.cpp


Index: llvm/trunk/lib/Analysis/AssumptionCache.cpp
===================================================================
--- llvm/trunk/lib/Analysis/AssumptionCache.cpp
+++ llvm/trunk/lib/Analysis/AssumptionCache.cpp
@@ -84,18 +84,11 @@
         Value *B;
         ConstantInt *C;
         // (A & B) or (A | B) or (A ^ B).
-        if (match(V,
-                  m_CombineOr(m_And(m_Value(A), m_Value(B)),
-                    m_CombineOr(m_Or(m_Value(A), m_Value(B)),
-                                m_Xor(m_Value(A), m_Value(B)))))) {
+        if (match(V, m_BitwiseLogic(m_Value(A), m_Value(B)))) {
           AddAffected(A);
           AddAffected(B);
         // (A << C) or (A >>_s C) or (A >>_u C) where C is some constant.
-        } else if (match(V,
-                         m_CombineOr(m_Shl(m_Value(A), m_ConstantInt(C)),
-                           m_CombineOr(m_LShr(m_Value(A), m_ConstantInt(C)),
-                                       m_AShr(m_Value(A),
-                                              m_ConstantInt(C)))))) {
+        } else if (match(V, m_Shift(m_Value(A), m_ConstantInt(C)))) {
           AddAffected(A);
         }
       };
Index: llvm/trunk/include/llvm/IR/Instruction.h
===================================================================
--- llvm/trunk/include/llvm/IR/Instruction.h
+++ llvm/trunk/include/llvm/IR/Instruction.h
@@ -152,9 +152,14 @@
     return getOpcode() == AShr;
   }
 
+  /// Determine if the Opcode is and/or/xor.
+  static inline bool isBitwiseLogicOp(unsigned Opcode) {
+    return Opcode == And || Opcode == Or || Opcode == Xor;
+  }
+
   /// Return true if this is and/or/xor.
   inline bool isBitwiseLogicOp() const {
-    return getOpcode() == And || getOpcode() == Or || getOpcode() == Xor;
+    return isBitwiseLogicOp(getOpcode());
   }
 
   /// Determine if the OpCode is one of the CastInst instructions.
Index: llvm/trunk/include/llvm/IR/PatternMatch.h
===================================================================
--- llvm/trunk/include/llvm/IR/PatternMatch.h
+++ llvm/trunk/include/llvm/IR/PatternMatch.h
@@ -704,6 +704,47 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Class that matches a group of binary opcodes.
+//
+template <typename LHS_t, typename RHS_t, typename Predicate>
+struct BinOpPred_match : Predicate {
+  LHS_t L;
+  RHS_t R;
+
+  BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
+
+  template <typename OpTy> bool match(OpTy *V) {
+    if (auto *I = dyn_cast<Instruction>(V))
+      return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) && R.match(I->getOperand(1));
+    if (auto *CE = dyn_cast<ConstantExpr>(V))
+      return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
+    return false;
+  }
+};
+
+struct is_shift_op {
+  bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
+};
+
+struct is_bitwiselogic_op {
+  bool isOpType(unsigned Opcode) { return Instruction::isBitwiseLogicOp(Opcode); }
+};
+
+/// \brief Matches shift operations.
+template <typename LHS, typename RHS>
+inline BinOpPred_match<LHS, RHS, is_shift_op>
+m_Shift(const LHS &L, const RHS &R) {
+  return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
+}
+
+/// \brief Matches bitwise logic operations.
+template <typename LHS, typename RHS>
+inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
+m_BitwiseLogic(const LHS &L, const RHS &R) {
+  return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
+}
+
+//===----------------------------------------------------------------------===//
 // Class that matches exact binary ops.
 //
 template <typename SubPattern_t> struct Exact_match {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34593.103831.patch
Type: text/x-patch
Size: 3717 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170624/f7e8cc0a/attachment.bin>


More information about the llvm-commits mailing list