Skip to content

Commit 6381114

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

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
@@ -1706,7 +1706,8 @@ template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
17061706
};
17071707

17081708
/// Matches instructions with Opcode and three operands.
1709-
template <typename T0, typename T1, typename T2, unsigned Opcode>
1709+
template <typename T0, typename T1, typename T2, unsigned Opcode,
1710+
bool CommutableOp2Op3 = false>
17101711
struct ThreeOps_match {
17111712
T0 Op1;
17121713
T1 Op2;
@@ -1718,8 +1719,12 @@ struct ThreeOps_match {
17181719
template <typename OpTy> bool match(OpTy *V) {
17191720
if (V->getValueID() == Value::InstructionVal + Opcode) {
17201721
auto *I = cast<Instruction>(V);
1721-
return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1722-
Op3.match(I->getOperand(2));
1722+
if (!Op1.match(I->getOperand(0)))
1723+
return false;
1724+
if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1725+
return true;
1726+
return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1727+
Op3.match(I->getOperand(1));
17231728
}
17241729
return false;
17251730
}
@@ -1771,6 +1776,13 @@ m_SelectCst(const Cond &C) {
17711776
return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
17721777
}
17731778

1779+
/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1780+
template <typename Cond, typename LHS, typename RHS>
1781+
inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select, true>
1782+
m_c_Select(const Cond &C, const LHS &L, const RHS &R) {
1783+
return ThreeOps_match<Cond, LHS, RHS, Instruction::Select, true>(C, L, R);
1784+
}
1785+
17741786
/// Matches FreezeInst.
17751787
template <typename OpTy>
17761788
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
@@ -2241,8 +2241,7 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
22412241
if (!UI)
22422242
return false;
22432243
return match(UI,
2244-
m_Select(m_Value(), m_Specific(Op1), m_Specific(&I))) ||
2245-
match(UI, m_Select(m_Value(), m_Specific(&I), m_Specific(Op1)));
2244+
m_c_Select(m_Value(), m_Specific(Op1), m_Specific(&I)));
22462245
})) {
22472246
if (Value *NegOp1 = Negator::Negate(IsNegation, /* IsNSW */ IsNegation &&
22482247
I.hasNoSignedWrap(),

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,9 +1664,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16641664
Value *X;
16651665
if (match(IIOperand, m_Neg(m_Value(X))))
16661666
return replaceOperand(*II, 0, X);
1667-
if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
1668-
return replaceOperand(*II, 0, X);
1669-
if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
1667+
if (match(IIOperand,
1668+
m_c_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
16701669
return replaceOperand(*II, 0, X);
16711670

16721671
Value *Y;

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

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

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,10 +3810,8 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38103810
// These can often be turned into switches and other things.
38113811
auto IsBinOpOrAnd = [](Value *V) {
38123812
return match(
3813-
V, m_CombineOr(
3814-
m_BinOp(),
3815-
m_CombineOr(m_Select(m_Value(), m_ImmConstant(), m_Value()),
3816-
m_Select(m_Value(), m_Value(), m_ImmConstant()))));
3813+
V, m_CombineOr(m_BinOp(),
3814+
m_c_Select(m_Value(), m_ImmConstant(), m_Value())));
38173815
};
38183816
if (PN->getType()->isIntegerTy(1) &&
38193817
(IsBinOpOrAnd(PN->getIncomingValue(0)) ||

0 commit comments

Comments
 (0)