[llvm] [PatternMatch] Add `m_c_XorLike` matcher; NFC (PR #122642)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 23:57:15 PST 2025
================
@@ -1430,6 +1430,34 @@ m_NUWAddLike(const LHS &L, const RHS &R) {
return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R));
}
+template <typename LHS, typename RHS>
+struct XorLike_match {
+ LHS L;
+ RHS R;
+
+ XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {}
+
+ template <typename OpTy> bool match(OpTy *V) {
+ if (auto *Op = dyn_cast<BinaryOperator>(V)) {
+ if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() &&
+ PatternMatch::match(Op->getOperand(0), m_LowBitMask()))
+ ; // Pass
+ else if (Op->getOpcode() != Instruction::Xor)
+ return false;
+ return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) ||
+ (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0)));
+ }
+ return false;
+ }
+};
+
+// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
+// Only commutative matcher as the `sub` will need to swap the L and R.
----------------
dtcxzyw wrote:
```suggestion
/// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()`
/// Only commutative matcher as the `sub` will need to swap the L and R.
```
https://github.com/llvm/llvm-project/pull/122642
More information about the llvm-commits
mailing list