Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
10 changes: 10 additions & 0 deletions llvm/include/llvm/CodeGen/SDPatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,16 @@ m_InsertSubvector(const LHS &Base, const RHS &Sub, const IDX &Idx) {
return TernaryOpc_match<LHS, RHS, IDX>(ISD::INSERT_SUBVECTOR, Base, Sub, Idx);
}

template <typename LTy, typename RTy, typename TTy, typename FTy, typename CCTy>
inline auto m_SelectCC(LTy L, RTy R, TTy T, FTy F, CCTy CC) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still need to by ref m_SelectCC(LTy &L, RTy &R, TTy &T, FTy &F, CCTy &CC) (just not rvalue)

return m_Node(ISD::SELECT_CC, L, R, T, F, CC);
}

template <typename LTy, typename RTy, typename TTy, typename FTy, typename CCTy>
inline auto m_SelectCCLike(LTy L, RTy R, TTy T, FTy F, CCTy CC) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_SelectCCLike(LTy &L, RTy &R, TTy &T, FTy &F, CCTy &CC)

return m_AnyOf(m_Select(m_SetCC(L, R, CC), T, F), m_SelectCC(L, R, T, F, CC));
}

// === Binary operations ===
template <typename LHS_P, typename RHS_P, bool Commutable = false,
bool ExcludeChain = false>
Expand Down
12 changes: 6 additions & 6 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4099,12 +4099,12 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
// (sub x, ([v]select (uge x, y), y, 0)) -> (umin x, (sub x, y))
if (N1.hasOneUse() && hasUMin(VT)) {
SDValue Y;
if (sd_match(N1, m_Select(m_SetCC(m_Specific(N0), m_Value(Y),
m_SpecificCondCode(ISD::SETULT)),
m_Zero(), m_Deferred(Y))) ||
sd_match(N1, m_Select(m_SetCC(m_Specific(N0), m_Value(Y),
m_SpecificCondCode(ISD::SETUGE)),
m_Deferred(Y), m_Zero())) ||
if (sd_match(N1, m_SelectCCLike(m_Specific(N0), m_Value(Y), m_Zero(),
m_Deferred(Y),
m_SpecificCondCode(ISD::SETULT))) ||
sd_match(N1,
m_SelectCCLike(m_Specific(N0), m_Value(Y), m_Deferred(Y),
m_Zero(), m_SpecificCondCode(ISD::SETUGE))) ||
sd_match(N1, m_VSelect(m_SetCC(m_Specific(N0), m_Value(Y),
m_SpecificCondCode(ISD::SETULT)),
m_Zero(), m_Deferred(Y))) ||
Expand Down
59 changes: 59 additions & 0 deletions llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,3 +949,62 @@ TEST_F(SelectionDAGPatternMatchTest, MatchZeroOneAllOnes) {
EXPECT_TRUE(sd_match(Vec, DAG.get(), m_AllOnes(true)));
}
}

TEST_F(SelectionDAGPatternMatchTest, MatchSelectCCLike) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you add m_SelectCC unit test coverage

using namespace SDPatternMatch;

SDValue LHS = DAG->getConstant(1, SDLoc(), MVT::i32);
SDValue RHS = DAG->getConstant(2, SDLoc(), MVT::i32);
SDValue TVal = DAG->getConstant(3, SDLoc(), MVT::i32);
SDValue FVal = DAG->getConstant(4, SDLoc(), MVT::i32);
SDValue Select = DAG->getNode(ISD::SELECT_CC, SDLoc(), MVT::i32, LHS, RHS,
TVal, FVal, DAG->getCondCode(ISD::SETLT));

ISD::CondCode CC = ISD::SETLT;
auto Matcher =
m_SelectCCLike(m_Specific(LHS), m_Specific(RHS), m_Specific(TVal),
m_Specific(FVal), m_CondCode(CC));

struct DAGMatchContext {
SelectionDAG &DAG;
DAGMatchContext(SelectionDAG &DAG) : DAG(DAG) {}

bool match(SDValue N, unsigned Opcode) const {
return N.getOpcode() == Opcode;
}

unsigned getNumOperands(SDValue N) const { return N.getNumOperands(); }
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to create a context here. There are variants of sd_match that do not require context or explicit SelectionDAG: https://llvm.org/doxygen/namespacellvm_1_1SDPatternMatch.html#a22e69d98d1e6e8f2308ede1c5809d0ac

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto unnecessary context


DAGMatchContext Ctx(*DAG);
EXPECT_TRUE(Matcher.match(Ctx, Select));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use sd_match like other tests in this file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto sd_match

}

TEST_F(SelectionDAGPatternMatchTest, MatchSelectCC) {
using namespace SDPatternMatch;

SDValue LHS = DAG->getConstant(1, SDLoc(), MVT::i32);
SDValue RHS = DAG->getConstant(2, SDLoc(), MVT::i32);
SDValue TVal = DAG->getConstant(3, SDLoc(), MVT::i32);
SDValue FVal = DAG->getConstant(4, SDLoc(), MVT::i32);
SDValue Select = DAG->getNode(ISD::SELECT_CC, SDLoc(), MVT::i32, LHS, RHS,
TVal, FVal, DAG->getCondCode(ISD::SETLT));

ISD::CondCode CC = ISD::SETLT;
auto Matcher = m_SelectCC(m_Specific(LHS), m_Specific(RHS), m_Specific(TVal),
m_Specific(FVal), m_CondCode(CC));

struct DAGMatchContext {
SelectionDAG &DAG;
DAGMatchContext(SelectionDAG &DAG) : DAG(DAG) {}

bool match(SDValue N, unsigned Opcode) const {
return N.getOpcode() == Opcode;
}

unsigned getNumOperands(SDValue N) const { return N.getNumOperands(); }
};

DAGMatchContext Ctx(*DAG);
EXPECT_TRUE(Matcher.match(Ctx, Select));
}
Loading