Skip to content

Commit da55483

Browse files
davemgreenjmciver
authored andcommitted
[PatternMatch] Introduce m_c_Select (llvm#114328)
This matches m_Select(m_Value(), L, R) or m_Select(m_Value(), R, L).
1 parent 37616f6 commit da55483

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
@@ -1628,7 +1628,8 @@ template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
16281628
};
16291629

16301630
/// Matches instructions with Opcode and three operands.
1631-
template <typename T0, typename T1, typename T2, unsigned Opcode>
1631+
template <typename T0, typename T1, typename T2, unsigned Opcode,
1632+
bool CommutableOp2Op3 = false>
16321633
struct ThreeOps_match {
16331634
T0 Op1;
16341635
T1 Op2;
@@ -1640,8 +1641,12 @@ struct ThreeOps_match {
16401641
template <typename OpTy> bool match(OpTy *V) {
16411642
if (V->getValueID() == Value::InstructionVal + Opcode) {
16421643
auto *I = cast<Instruction>(V);
1643-
return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1644-
Op3.match(I->getOperand(2));
1644+
if (!Op1.match(I->getOperand(0)))
1645+
return false;
1646+
if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2)))
1647+
return true;
1648+
return CommutableOp2Op3 && Op2.match(I->getOperand(2)) &&
1649+
Op3.match(I->getOperand(1));
16451650
}
16461651
return false;
16471652
}
@@ -1693,6 +1698,14 @@ m_SelectCst(const Cond &C) {
16931698
return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
16941699
}
16951700

1701+
/// Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
1702+
template <typename LHS, typename RHS>
1703+
inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true>
1704+
m_c_Select(const LHS &L, const RHS &R) {
1705+
return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select,
1706+
true>(m_Value(), L, R);
1707+
}
1708+
16961709
/// Matches FreezeInst.
16971710
template <typename OpTy>
16981711
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
@@ -2186,9 +2186,7 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
21862186
const Instruction *UI = dyn_cast<Instruction>(U);
21872187
if (!UI)
21882188
return false;
2189-
return match(UI,
2190-
m_Select(m_Value(), m_Specific(Op1), m_Specific(&I))) ||
2191-
match(UI, m_Select(m_Value(), m_Specific(&I), m_Specific(Op1)));
2189+
return match(UI, m_c_Select(m_Specific(Op1), m_Specific(&I)));
21922190
})) {
21932191
if (Value *NegOp1 = Negator::Negate(IsNegation, /* IsNSW */ IsNegation &&
21942192
I.hasNoSignedWrap(),

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,9 +1605,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16051605
Value *X;
16061606
if (match(IIOperand, m_Neg(m_Value(X))))
16071607
return replaceOperand(*II, 0, X);
1608-
if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
1609-
return replaceOperand(*II, 0, X);
1610-
if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
1608+
if (match(IIOperand, m_c_Select(m_Neg(m_Value(X)), m_Deferred(X))))
16111609
return replaceOperand(*II, 0, X);
16121610

16131611
Value *Y;

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8091,9 +8091,7 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
80918091
case Instruction::Select:
80928092
// fcmp eq (cond ? x : -x), 0 --> fcmp eq x, 0
80938093
if (FCmpInst::isEquality(Pred) && match(RHSC, m_AnyZeroFP()) &&
8094-
(match(LHSI,
8095-
m_Select(m_Value(), m_Value(X), m_FNeg(m_Deferred(X)))) ||
8096-
match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
8094+
match(LHSI, m_c_Select(m_FNeg(m_Value(X)), m_Deferred(X))))
80978095
return replaceOperand(I, 0, X);
80988096
if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
80998097
return NV;

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,10 +3605,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
36053605
// These can often be turned into switches and other things.
36063606
auto IsBinOpOrAnd = [](Value *V) {
36073607
return match(
3608-
V, m_CombineOr(
3609-
m_BinOp(),
3610-
m_CombineOr(m_Select(m_Value(), m_ImmConstant(), m_Value()),
3611-
m_Select(m_Value(), m_Value(), m_ImmConstant()))));
3608+
V, m_CombineOr(m_BinOp(), m_c_Select(m_ImmConstant(), m_Value())));
36123609
};
36133610
if (PN->getType()->isIntegerTy(1) &&
36143611
(IsBinOpOrAnd(PN->getIncomingValue(0)) ||

0 commit comments

Comments
 (0)