Skip to content

Commit ce097ff

Browse files
committed
Combine subs(sub(a,b), 0) -> subs(a,b) in performCSELCombine.
1 parent 5f4c086 commit ce097ff

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -25399,6 +25390,24 @@ static SDValue performCSELCombine(SDNode *N,
2539925390
}
2540025391
}
2540125392

25393+
// CSEL a, b, cc, SUBS(SUB(x,y), 0) -> CSEL a, b, cc, SUBS(x,y) if cc doesn't
25394+
// use overflow flags to avoid the comparison with zero.
25395+
if (Cond.getOpcode() == AArch64ISD::SUBS &&
25396+
isNullConstant(Cond.getOperand(1))) {
25397+
SDValue Sub = Cond.getOperand(0);
25398+
AArch64CC::CondCode CC =
25399+
static_cast<AArch64CC::CondCode>(N->getConstantOperandVal(2));
25400+
if (Sub.getOpcode() == ISD::SUB &&
25401+
(CC == AArch64CC::EQ || CC == AArch64CC::NE || CC == AArch64CC::MI ||
25402+
CC == AArch64CC::PL)) {
25403+
SDLoc DL(N);
25404+
SDValue Subs = DAG.getNode(AArch64ISD::SUBS, DL, Cond->getVTList(),
25405+
Sub.getOperand(0), Sub.getOperand(1));
25406+
return DAG.getNode(AArch64ISD::CSEL, DL, N->getVTList(), N->getOperand(0),
25407+
N->getOperand(1), N->getOperand(2), Subs.getValue(1));
25408+
}
25409+
}
25410+
2540225411
// CSEL (LASTB P, Z), X, NE(ANY P) -> CLASTB P, X, Z
2540325412
if (SDValue CondLast = foldCSELofLASTB(N, DAG))
2540425413
return CondLast;

0 commit comments

Comments
 (0)