Skip to content

Commit a24b0de

Browse files
committed
[DAG] Add SDPatternMatch::m_BitwiseLogic common matcher for AND/OR/XOR nodes
1 parent f6ac527 commit a24b0de

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,11 @@ inline BinaryOpc_match<LHS, RHS, true> m_Xor(const LHS &L, const RHS &R) {
729729
return BinaryOpc_match<LHS, RHS, true>(ISD::XOR, L, R);
730730
}
731731

732+
template <typename LHS, typename RHS>
733+
inline auto m_BitwiseLogic(const LHS &L, const RHS &R) {
734+
return m_AnyOf(m_And(L, R), m_Or(L, R), m_Xor(L, R));
735+
}
736+
732737
template <typename LHS, typename RHS>
733738
inline BinaryOpc_match<LHS, RHS, true> m_SMin(const LHS &L, const RHS &R) {
734739
return BinaryOpc_match<LHS, RHS, true>(ISD::SMIN, L, R);

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10553,10 +10553,10 @@ static SDValue foldBitOrderCrossLogicOp(SDNode *N, SelectionDAG &DAG) {
1055310553
SDValue N0 = N->getOperand(0);
1055410554
EVT VT = N->getValueType(0);
1055510555
SDLoc DL(N);
10556-
if (ISD::isBitwiseLogicOp(N0.getOpcode()) && N0.hasOneUse()) {
10557-
SDValue OldLHS = N0.getOperand(0);
10558-
SDValue OldRHS = N0.getOperand(1);
1055910556

10557+
SDValue OldLHS, OldRHS;
10558+
if (sd_match(N0,
10559+
m_OneUse(m_BitwiseLogic(m_Value(OldLHS), m_Value(OldRHS))))) {
1056010560
// If both operands are bswap/bitreverse, ignore the multiuse
1056110561
// Otherwise need to ensure logic_op and bswap/bitreverse(x) have one use.
1056210562
if (OldLHS.getOpcode() == Opcode && OldRHS.getOpcode() == Opcode) {

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,18 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
302302
EXPECT_TRUE(
303303
sd_match(SFAdd, m_ChainedBinOp(ISD::STRICT_FADD, m_SpecificVT(Float32VT),
304304
m_SpecificVT(Float32VT))));
305+
EXPECT_FALSE(sd_match(Add, m_BitwiseLogic(m_Value(), m_Value())));
306+
EXPECT_FALSE(sd_match(Sub, m_BitwiseLogic(m_Value(), m_Value())));
305307

306308
EXPECT_TRUE(sd_match(And, m_c_BinOp(ISD::AND, m_Value(), m_Value())));
307309
EXPECT_TRUE(sd_match(And, m_And(m_Value(), m_Value())));
310+
EXPECT_TRUE(sd_match(And, m_BitwiseLogic(m_Value(), m_Value())));
308311
EXPECT_TRUE(sd_match(Xor, m_c_BinOp(ISD::XOR, m_Value(), m_Value())));
309312
EXPECT_TRUE(sd_match(Xor, m_Xor(m_Value(), m_Value())));
313+
EXPECT_TRUE(sd_match(Xor, m_BitwiseLogic(m_Value(), m_Value())));
310314
EXPECT_TRUE(sd_match(Or, m_c_BinOp(ISD::OR, m_Value(), m_Value())));
311315
EXPECT_TRUE(sd_match(Or, m_Or(m_Value(), m_Value())));
316+
EXPECT_TRUE(sd_match(Or, m_BitwiseLogic(m_Value(), m_Value())));
312317
EXPECT_FALSE(sd_match(Or, m_DisjointOr(m_Value(), m_Value())));
313318

314319
EXPECT_TRUE(sd_match(DisOr, m_Or(m_Value(), m_Value())));

0 commit comments

Comments
 (0)