Skip to content

Commit 44570bc

Browse files
committed
[DAG] Implement SDPatternMatch m_Abs() matcher
Resolves #144474
1 parent d4826cd commit 44570bc

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,10 @@ template <typename Opnd> inline UnaryOpc_match<Opnd> m_Trunc(const Opnd &Op) {
938938
return UnaryOpc_match<Opnd>(ISD::TRUNCATE, Op);
939939
}
940940

941+
template <typename Opnd> inline UnaryOpc_match<Opnd> m_Abs(const Opnd &Op) {
942+
return UnaryOpc_match<Opnd>(ISD::ABS, Op);
943+
}
944+
941945
/// Match a zext or identity
942946
/// Allows to peek through optional extensions
943947
template <typename Opnd> inline auto m_ZExtOrSelf(const Opnd &Op) {

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11252,19 +11252,13 @@ SDValue DAGCombiner::foldABSToABD(SDNode *N, const SDLoc &DL) {
1125211252
if (N->getOpcode() == ISD::TRUNCATE)
1125311253
N = N->getOperand(0).getNode();
1125411254

11255-
if (N->getOpcode() != ISD::ABS)
11256-
return SDValue();
11257-
1125811255
EVT VT = N->getValueType(0);
11259-
SDValue AbsOp1 = N->getOperand(0);
1126011256
SDValue Op0, Op1;
1126111257

11262-
if (AbsOp1.getOpcode() != ISD::SUB)
11258+
if (!sd_match(N, m_Abs(m_Sub(m_Value(Op0), m_Value(Op1)))))
1126311259
return SDValue();
1126411260

11265-
Op0 = AbsOp1.getOperand(0);
11266-
Op1 = AbsOp1.getOperand(1);
11267-
11261+
SDValue AbsOp0 = N->getOperand(0);
1126811262
unsigned Opc0 = Op0.getOpcode();
1126911263

1127011264
// Check if the operands of the sub are (zero|sign)-extended.
@@ -11274,7 +11268,7 @@ SDValue DAGCombiner::foldABSToABD(SDNode *N, const SDLoc &DL) {
1127411268
Opc0 != ISD::SIGN_EXTEND_INREG)) {
1127511269
// fold (abs (sub nsw x, y)) -> abds(x, y)
1127611270
// Don't fold this for unsupported types as we lose the NSW handling.
11277-
if (AbsOp1->getFlags().hasNoSignedWrap() && hasOperation(ISD::ABDS, VT) &&
11271+
if (AbsOp0->getFlags().hasNoSignedWrap() && hasOperation(ISD::ABDS, VT) &&
1127811272
TLI.preferABDSToABSWithNSW(VT)) {
1127911273
SDValue ABD = DAG.getNode(ISD::ABDS, DL, VT, Op0, Op1);
1128011274
return DAG.getZExtOrTrunc(ABD, DL, SrcVT);

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ TEST_F(SelectionDAGPatternMatchTest, matchUnaryOp) {
388388
SDValue SExt = DAG->getNode(ISD::SIGN_EXTEND, DL, Int64VT, Op0);
389389
SDValue Trunc = DAG->getNode(ISD::TRUNCATE, DL, Int32VT, Op1);
390390

391+
SDValue Abs = DAG->getNode(ISD::ABS, DL, Int32VT, Op0);
392+
391393
SDValue Sub = DAG->getNode(ISD::SUB, DL, Int32VT, Trunc, Op0);
392394
SDValue Neg = DAG->getNegative(Op0, DL, Int32VT);
393395
SDValue Not = DAG->getNOT(DL, Op0, Int32VT);
@@ -417,6 +419,8 @@ TEST_F(SelectionDAGPatternMatchTest, matchUnaryOp) {
417419
EXPECT_FALSE(sd_match(ZExt, m_SExtLike(m_Value())));
418420
EXPECT_TRUE(sd_match(Trunc, m_Trunc(m_Specific(Op1))));
419421

422+
EXPECT_TRUE(sd_match(Abs, m_Abs(m_Specific(Op0))));
423+
420424
EXPECT_TRUE(sd_match(Neg, m_Neg(m_Value())));
421425
EXPECT_TRUE(sd_match(Not, m_Not(m_Value())));
422426
EXPECT_FALSE(sd_match(ZExt, m_Neg(m_Value())));

0 commit comments

Comments
 (0)