Skip to content

Commit ff1efe9

Browse files
authored
[AArch64] Combine subtract with borrow to SBC. (#165271)
Specifically, this patch adds the following combines: SUB x, (CSET LO, (CMP a, b)) -> SBC x, 0, (CMP a, b) SUB (SUB x, y), (CSET LO, (CMP a, b)) -> SBC x, y, (CMP a, b) The CSET may be preceded by a ZEXT. Fixes #164748.
1 parent d84a911 commit ff1efe9

File tree

2 files changed

+425
-0
lines changed

2 files changed

+425
-0
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22308,6 +22308,37 @@ static SDValue performExtBinopLoadFold(SDNode *N, SelectionDAG &DAG) {
2230822308
return DAG.getNode(N->getOpcode(), DL, VT, Ext0, NShift);
2230922309
}
2231022310

22311+
// Attempt to combine the following patterns:
22312+
// SUB x, (CSET LO, (CMP a, b)) -> SBC x, 0, (CMP a, b)
22313+
// SUB (SUB x, y), (CSET LO, (CMP a, b)) -> SBC x, y, (CMP a, b)
22314+
// The CSET may be preceded by a ZEXT.
22315+
static SDValue performSubWithBorrowCombine(SDNode *N, SelectionDAG &DAG) {
22316+
if (N->getOpcode() != ISD::SUB)
22317+
return SDValue();
22318+
22319+
EVT VT = N->getValueType(0);
22320+
if (VT != MVT::i32 && VT != MVT::i64)
22321+
return SDValue();
22322+
22323+
SDValue N1 = N->getOperand(1);
22324+
if (N1.getOpcode() == ISD::ZERO_EXTEND && N1.hasOneUse())
22325+
N1 = N1.getOperand(0);
22326+
if (!N1.hasOneUse() || getCSETCondCode(N1) != AArch64CC::LO)
22327+
return SDValue();
22328+
22329+
SDValue Flags = N1.getOperand(3);
22330+
if (Flags.getOpcode() != AArch64ISD::SUBS)
22331+
return SDValue();
22332+
22333+
SDLoc DL(N);
22334+
SDValue N0 = N->getOperand(0);
22335+
if (N0->getOpcode() == ISD::SUB)
22336+
return DAG.getNode(AArch64ISD::SBC, DL, VT, N0.getOperand(0),
22337+
N0.getOperand(1), Flags);
22338+
return DAG.getNode(AArch64ISD::SBC, DL, VT, N0, DAG.getConstant(0, DL, VT),
22339+
Flags);
22340+
}
22341+
2231122342
static SDValue performAddSubCombine(SDNode *N,
2231222343
TargetLowering::DAGCombinerInfo &DCI) {
2231322344
// Try to change sum of two reductions.
@@ -22329,6 +22360,8 @@ static SDValue performAddSubCombine(SDNode *N,
2232922360
return Val;
2233022361
if (SDValue Val = performAddSubIntoVectorOp(N, DCI.DAG))
2233122362
return Val;
22363+
if (SDValue Val = performSubWithBorrowCombine(N, DCI.DAG))
22364+
return Val;
2233222365

2233322366
if (SDValue Val = performExtBinopLoadFold(N, DCI.DAG))
2233422367
return Val;

0 commit comments

Comments
 (0)