Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions llvm/include/llvm/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,8 @@ template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
};

/// Matches instructions with Opcode and three operands.
template <typename T0, typename T1, typename T2, unsigned Opcode>
template <typename T0, typename T1, typename T2, unsigned Opcode,
bool CommutableOp2Op3 = false>
struct ThreeOps_match {
T0 Op1;
T1 Op2;
Expand All @@ -1728,8 +1729,12 @@ struct ThreeOps_match {
template <typename OpTy> bool match(OpTy *V) {
if (V->getValueID() == Value::InstructionVal + Opcode) {
auto *I = cast<Instruction>(V);
return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
Op3.match(I->getOperand(2));
if (!Op1.match(I->getOperand(0)))
return false;
if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
return true;
return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
Op3.match(I->getOperand(1));
}
return false;
}
Expand Down Expand Up @@ -1781,6 +1786,14 @@ m_SelectCst(const Cond &C) {
return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
}

/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
template <typename LHS, typename RHS>
inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
m_c_Select(const LHS &L, const RHS &R) {
return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
true>(m_Value(), L, R);
}

/// Matches FreezeInst.
template <typename OpTy>
inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2245,9 +2245,7 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
const Instruction *UI = dyn_cast<Instruction>(U);
if (!UI)
return false;
return match(UI,
m_Select(m_Value(), m_Specific(Op1), m_Specific(&I))) ||
match(UI, m_Select(m_Value(), m_Specific(&I), m_Specific(Op1)));
return match(UI, m_c_Select(m_Specific(Op1), m_Specific(&I)));
})) {
if (Value *NegOp1 = Negator::Negate(IsNegation, /* IsNSW */ IsNegation &&
I.hasNoSignedWrap(),
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1736,9 +1736,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
Value *X;
if (match(IIOperand, m_Neg(m_Value(X))))
return replaceOperand(*II, 0, X);
if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
return replaceOperand(*II, 0, X);
if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
if (match(IIOperand, m_c_Select(m_Neg(m_Value(X)), m_Deferred(X))))
return replaceOperand(*II, 0, X);

Value *Y;
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8437,9 +8437,7 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
case Instruction::Select:
// fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
(match(LHSI,
m_Select(m_Value(), m_Value(X), m_FNeg(m_Deferred(X)))) ||
match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
match(LHSI, m_c_Select(m_FNeg(m_Value(X)), m_Deferred(X))))
return replaceOperand(I, 0, X);
if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
return NV;
Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3814,10 +3814,7 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
// These can often be turned into switches and other things.
auto IsBinOpOrAnd = [](Value *V) {
return match(
V, m_CombineOr(
m_BinOp(),
m_CombineOr(m_Select(m_Value(), m_ImmConstant(), m_Value()),
m_Select(m_Value(), m_Value(), m_ImmConstant()))));
V, m_CombineOr(m_BinOp(), m_c_Select(m_ImmConstant(), m_Value())));
};
if (PN->getType()->isIntegerTy(1) &&
(IsBinOpOrAnd(PN->getIncomingValue(0)) ||
Expand Down
Loading