Skip to content

Commit 1b1e5e2

Browse files
committed
[PatternMatch] Introduce m_c_Select
This matches m_Select(C, L, R) or m_Select(C, R, L).
1 parent fe697ef commit 1b1e5e2

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,8 @@ template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
17161716
};
17171717

17181718
/// Matches instructions with Opcode and three operands.
1719-
template <typename T0, typename T1, typename T2, unsigned Opcode>
1719+
template <typename T0, typename T1, typename T2, unsigned Opcode,
1720+
bool CommutableOp2Op3 = false>
17201721
struct ThreeOps_match {
17211722
T0 Op1;
17221723
T1 Op2;
@@ -1728,8 +1729,12 @@ struct ThreeOps_match {
17281729
template <typename OpTy> bool match(OpTy *V) {
17291730
if (V->getValueID() == Value::InstructionVal + Opcode) {
17301731
auto *I = cast<Instruction>(V);
1731-
return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1732-
Op3.match(I->getOperand(2));
1732+
if (!Op1.match(I->getOperand(0)))
1733+
return false;
1734+
if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1735+
return true;
1736+
return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1737+
Op3.match(I->getOperand(1));
17331738
}
17341739
return false;
17351740
}
@@ -1781,6 +1786,13 @@ m_SelectCst(const Cond &C) {
17811786
return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
17821787
}
17831788

1789+
/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1790+
template <typename Cond, typename LHS, typename RHS>
1791+
inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select, true>
1792+
m_c_Select(const Cond &C, const LHS &L, const RHS &R) {
1793+
return ThreeOps_match<Cond, LHS, RHS, Instruction::Select, true>(C, L, R);
1794+
}
1795+
17841796
/// Matches FreezeInst.
17851797
template <typename OpTy>
17861798
inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,8 +2246,7 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
22462246
if (!UI)
22472247
return false;
22482248
return match(UI,
2249-
m_Select(m_Value(), m_Specific(Op1), m_Specific(&I))) ||
2250-
match(UI, m_Select(m_Value(), m_Specific(&I), m_Specific(Op1)));
2249+
m_c_Select(m_Value(), m_Specific(Op1), m_Specific(&I)));
22512250
})) {
22522251
if (Value *NegOp1 = Negator::Negate(IsNegation, /* IsNSW */ IsNegation &&
22532252
I.hasNoSignedWrap(),

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,9 +1736,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
17361736
Value *X;
17371737
if (match(IIOperand, m_Neg(m_Value(X))))
17381738
return replaceOperand(*II, 0, X);
1739-
if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
1740-
return replaceOperand(*II, 0, X);
1741-
if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
1739+
if (match(IIOperand,
1740+
m_c_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
17421741
return replaceOperand(*II, 0, X);
17431742

17441743
Value *Y;

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8437,9 +8437,7 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
84378437
case Instruction::Select:
84388438
// fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
84398439
if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8440-
(match(LHSI,
8441-
m_Select(m_Value(), m_Value(X), m_FNeg(m_Deferred(X)))) ||
8442-
match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
8440+
match(LHSI, m_c_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X))))
84438441
return replaceOperand(I, 0, X);
84448442
if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
84458443
return NV;

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3814,10 +3814,8 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38143814
// These can often be turned into switches and other things.
38153815
auto IsBinOpOrAnd = [](Value *V) {
38163816
return match(
3817-
V, m_CombineOr(
3818-
m_BinOp(),
3819-
m_CombineOr(m_Select(m_Value(), m_ImmConstant(), m_Value()),
3820-
m_Select(m_Value(), m_Value(), m_ImmConstant()))));
3817+
V, m_CombineOr(m_BinOp(),
3818+
m_c_Select(m_Value(), m_ImmConstant(), m_Value())));
38213819
};
38223820
if (PN->getType()->isIntegerTy(1) &&
38233821
(IsBinOpOrAnd(PN->getIncomingValue(0)) ||

0 commit comments

Comments
 (0)