From 96d92eadca24761a1379993b197e7476f105bfc9 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Wed, 12 Apr 2017 05:49:28 +0000 Subject: [PATCH] [InstCombine][IR] Add a commutable BinOp matcher. Use it to reduce some code. NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300030 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/PatternMatch.h | 8 ++++++++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 3 +-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/llvm/IR/PatternMatch.h b/include/llvm/IR/PatternMatch.h index 34f3724805b..0788690fb41 100644 --- a/include/llvm/IR/PatternMatch.h +++ b/include/llvm/IR/PatternMatch.h @@ -1348,6 +1348,14 @@ template inline Signum_match m_Signum(const Val_t &V) { // Matchers for two-operands operators with the operators in either order // +/// \brief Matches a Add with LHS and RHS in either order. +template +inline match_combine_or, + AnyBinaryOp_match> +m_c_BinOp(const LHS &L, const RHS &R) { + return m_CombineOr(m_BinOp(L, R), m_BinOp(R, L)); +} + /// \brief Matches an ICmp with a predicate over LHS and RHS in either order. /// Does not swap the predicate. template diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 9bbc5b281ab..ea8c0a6d38b 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1272,8 +1272,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { case Instruction::Sub: Value *X; ConstantInt *C1; - if (match(Op0I, m_BinOp(m_ZExt(m_Value(X)), m_ConstantInt(C1))) || - match(Op0I, m_BinOp(m_ConstantInt(C1), m_ZExt(m_Value(X))))) { + if (match(Op0I, m_c_BinOp(m_ZExt(m_Value(X)), m_ConstantInt(C1)))) { if (AndRHSMask.isIntN(X->getType()->getScalarSizeInBits())) { auto *TruncC1 = ConstantExpr::getTrunc(C1, X->getType()); Value *BinOp; -- 2.40.0