Skip to content

Commit

Permalink
[PatternMatch] Add m_BinOp/m_c_BinOp with specific opcode
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D113508
  • Loading branch information
rampitec committed Nov 15, 2021
1 parent 118757a commit e785f4a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
35 changes: 32 additions & 3 deletions llvm/include/llvm/IR/PatternMatch.h
Expand Up @@ -963,20 +963,22 @@ struct BinaryOp_match {
// The LHS is always matched first.
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}

template <typename OpTy> bool match(OpTy *V) {
if (V->getValueID() == Value::InstructionVal + Opcode) {
template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) {
if (V->getValueID() == Value::InstructionVal + Opc) {
auto *I = cast<BinaryOperator>(V);
return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
(Commutable && L.match(I->getOperand(1)) &&
R.match(I->getOperand(0)));
}
if (auto *CE = dyn_cast<ConstantExpr>(V))
return CE->getOpcode() == Opcode &&
return CE->getOpcode() == Opc &&
((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
(Commutable && L.match(CE->getOperand(1)) &&
R.match(CE->getOperand(0))));
return false;
}

template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); }
};

template <typename LHS, typename RHS>
Expand Down Expand Up @@ -1221,6 +1223,26 @@ m_NUWShl(const LHS &L, const RHS &R) {
L, R);
}

template <typename LHS_t, typename RHS_t, bool Commutable = false>
struct SpecificBinaryOp_match
: public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
unsigned Opcode;

SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
: BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}

template <typename OpTy> bool match(OpTy *V) {
return BinaryOp_match<LHS_t, RHS_t, 0, Commutable>::match(Opcode, V);
}
};

/// Matches a specific opcode.
template <typename LHS, typename RHS>
inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
const RHS &R) {
return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
}

//===----------------------------------------------------------------------===//
// Class that matches a group of binary opcodes.
//
Expand Down Expand Up @@ -2198,6 +2220,13 @@ m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
R);
}

/// Matches a specific opcode with LHS and RHS in either order.
template <typename LHS, typename RHS>
inline SpecificBinaryOp_match<LHS, RHS, true>
m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
}

/// Matches a Add with LHS and RHS in either order.
template <typename LHS, typename RHS>
inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
Expand Down
19 changes: 8 additions & 11 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Expand Up @@ -1418,15 +1418,15 @@ static Instruction *reassociateFCmps(BinaryOperator &BO,
std::swap(Op0, Op1);

// Match inner binop and the predicate for combining 2 NAN checks into 1.
BinaryOperator *BO1;
Value *BO10, *BO11;
FCmpInst::Predicate NanPred = Opcode == Instruction::And ? FCmpInst::FCMP_ORD
: FCmpInst::FCMP_UNO;
if (!match(Op0, m_FCmp(Pred, m_Value(X), m_AnyZeroFP())) || Pred != NanPred ||
!match(Op1, m_BinOp(BO1)) || BO1->getOpcode() != Opcode)
!match(Op1, m_BinOp(Opcode, m_Value(BO10), m_Value(BO11))))
return nullptr;

// The inner logic op must have a matching fcmp operand.
Value *BO10 = BO1->getOperand(0), *BO11 = BO1->getOperand(1), *Y;
Value *Y;
if (!match(BO10, m_FCmp(Pred, m_Value(Y), m_AnyZeroFP())) ||
Pred != NanPred || X->getType() != Y->getType())
std::swap(BO10, BO11);
Expand Down Expand Up @@ -1475,14 +1475,11 @@ static Instruction *matchDeMorgansLaws(BinaryOperator &I,
// (~B & A) & ~C --> A & ~(B | C)
// (A | ~B) | ~C --> A | ~(B & C)
// (~B | A) | ~C --> A | ~(B & C)
BinaryOperator *BO;
if (match(Op0, m_OneUse(m_BinOp(BO))) && BO->getOpcode() == Opcode) {
Value *C;
if (match(BO, m_c_BinOp(m_Value(A), m_Not(m_Value(B)))) &&
match(Op1, m_Not(m_Value(C)))) {
Value *FlippedBO = Builder.CreateBinOp(FlippedOpcode, B, C);
return BinaryOperator::Create(Opcode, A, Builder.CreateNot(FlippedBO));
}
Value *C;
if (match(Op0, m_OneUse(m_c_BinOp(Opcode, m_Value(A), m_Not(m_Value(B))))) &&
match(Op1, m_Not(m_Value(C)))) {
Value *FlippedBO = Builder.CreateBinOp(FlippedOpcode, B, C);
return BinaryOperator::Create(Opcode, A, Builder.CreateNot(FlippedBO));
}

return nullptr;
Expand Down

0 comments on commit e785f4a

Please sign in to comment.