Skip to content

Commit 18abc7e

Browse files
authored
[PatternMatch] Introduce m_c_Select (#114328)
This matches m_Select(m_Value(), L, R) or m_Select(m_Value(), R, L).
1 parent 3699931 commit 18abc7e

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 16 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,14 @@ 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 LHS, typename RHS>
1791+
inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1792+
m_c_Select(const LHS &L, const RHS &R) {
1793+
return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1794+
true>(m_Value(), L, R);
1795+
}
1796+
17841797
/// Matches FreezeInst.
17851798
template <typename OpTy>
17861799
inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,9 +2245,7 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
22452245
const Instruction *UI = dyn_cast<Instruction>(U);
22462246
if (!UI)
22472247
return false;
2248-
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)));
2248+
return match(UI, m_c_Select(m_Specific(Op1), m_Specific(&I)));
22512249
})) {
22522250
if (Value *NegOp1 = Negator::Negate(IsNegation, /* IsNSW */ IsNegation &&
22532251
I.hasNoSignedWrap(),

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,9 +1736,7 @@ 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, m_c_Select(m_Neg(m_Value(X)), m_Deferred(X))))
17421740
return replaceOperand(*II, 0, X);
17431741

17441742
Value *Y;

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8460,9 +8460,7 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
84608460
case Instruction::Select:
84618461
// fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
84628462
if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8463-
(match(LHSI,
8464-
m_Select(m_Value(), m_Value(X), m_FNeg(m_Deferred(X)))) ||
8465-
match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
8463+
match(LHSI, m_c_Select(m_FNeg(m_Value(X)), m_Deferred(X))))
84668464
return replaceOperand(I, 0, X);
84678465
if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
84688466
return NV;

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,10 +3842,7 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38423842
// These can often be turned into switches and other things.
38433843
auto IsBinOpOrAnd = [](Value *V) {
38443844
return match(
3845-
V, m_CombineOr(
3846-
m_BinOp(),
3847-
m_CombineOr(m_Select(m_Value(), m_ImmConstant(), m_Value()),
3848-
m_Select(m_Value(), m_Value(), m_ImmConstant()))));
3845+
V, m_CombineOr(m_BinOp(), m_c_Select(m_ImmConstant(), m_Value())));
38493846
};
38503847
if (PN->getType()->isIntegerTy(1) &&
38513848
(IsBinOpOrAnd(PN->getIncomingValue(0)) ||

0 commit comments

Comments
 (0)