@@ -7118,21 +7118,12 @@ SDValue AArch64TargetLowering::LowerABS(SDValue Op, SelectionDAG &DAG) const {
71187118 return LowerToPredicatedOp(Op, DAG, AArch64ISD::ABS_MERGE_PASSTHRU);
71197119
71207120 SDLoc DL(Op);
7121- SDValue Val = Op.getOperand(0);
7122- SDValue Neg = DAG.getNegative(Val, DL, VT);
7123- SDValue Cmp;
7124-
7125- // For abs(sub(lhs, rhs)), we can compare lhs and rhs directly. This allows
7126- // reusing the subs operation for the calculation and comparison.
7127- if (Val.getOpcode() == ISD::SUB)
7128- Cmp = DAG.getNode(AArch64ISD::SUBS, DL, DAG.getVTList(VT, FlagsVT),
7129- Val.getOperand(0), Val.getOperand(1));
7130- else
7131- // Otherwise, compare with zero.
7132- Cmp = DAG.getNode(AArch64ISD::SUBS, DL, DAG.getVTList(VT, FlagsVT), Val,
7133- DAG.getConstant(0, DL, VT));
7134-
7135- return DAG.getNode(AArch64ISD::CSEL, DL, VT, Val, Neg,
7121+ SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
7122+ Op.getOperand(0));
7123+ // Generate SUBS & CSEL.
7124+ SDValue Cmp = DAG.getNode(AArch64ISD::SUBS, DL, DAG.getVTList(VT, FlagsVT),
7125+ Op.getOperand(0), DAG.getConstant(0, DL, VT));
7126+ return DAG.getNode(AArch64ISD::CSEL, DL, VT, Op.getOperand(0), Neg,
71367127 DAG.getConstant(AArch64CC::PL, DL, MVT::i32),
71377128 Cmp.getValue(1));
71387129}
@@ -25479,6 +25470,24 @@ static SDValue performCSELCombine(SDNode *N,
2547925470 }
2548025471 }
2548125472
25473+ // CSEL a, b, cc, SUBS(SUB(x,y), 0) -> CSEL a, b, cc, SUBS(x,y) if cc doesn't
25474+ // use overflow flags to avoid the comparison with zero.
25475+ if (Cond.getOpcode() == AArch64ISD::SUBS &&
25476+ isNullConstant(Cond.getOperand(1))) {
25477+ SDValue Sub = Cond.getOperand(0);
25478+ AArch64CC::CondCode CC =
25479+ static_cast<AArch64CC::CondCode>(N->getConstantOperandVal(2));
25480+ if (Sub.getOpcode() == ISD::SUB &&
25481+ (CC == AArch64CC::EQ || CC == AArch64CC::NE || CC == AArch64CC::MI ||
25482+ CC == AArch64CC::PL)) {
25483+ SDLoc DL(N);
25484+ SDValue Subs = DAG.getNode(AArch64ISD::SUBS, DL, Cond->getVTList(),
25485+ Sub.getOperand(0), Sub.getOperand(1));
25486+ return DAG.getNode(AArch64ISD::CSEL, DL, N->getVTList(), N->getOperand(0),
25487+ N->getOperand(1), N->getOperand(2), Subs.getValue(1));
25488+ }
25489+ }
25490+
2548225491 // CSEL (LASTB P, Z), X, NE(ANY P) -> CLASTB P, X, Z
2548325492 if (SDValue CondLast = foldCSELofLASTB(N, DAG))
2548425493 return CondLast;
0 commit comments