Skip to content

Commit c9de2e8

Browse files
committed
[AArch64] Use isKnownNonZero to optimize to cmn instead of cmp
1 parent bcd19d6 commit c9de2e8

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3396,9 +3396,11 @@ static bool isLegalArithImmed(uint64_t C) {
33963396
// So, finally, the only LLVM-native comparisons that don't mention C and V
33973397
// are SETEQ and SETNE. They're the only ones we can safely use CMN for in
33983398
// the absence of information about op2.
3399-
static bool isCMN(SDValue Op, ISD::CondCode CC) {
3399+
static bool isCMN(SDValue Op, SDValue CheckedVal, ISD::CondCode CC,
3400+
SelectionDAG &DAG) {
34003401
return Op.getOpcode() == ISD::SUB && isNullConstant(Op.getOperand(0)) &&
3401-
(CC == ISD::SETEQ || CC == ISD::SETNE);
3402+
(CC == ISD::SETEQ || CC == ISD::SETNE ||
3403+
DAG.isKnownNeverZero(CheckedVal));
34023404
}
34033405

34043406
static SDValue emitStrictFPComparison(SDValue LHS, SDValue RHS, const SDLoc &dl,
@@ -3443,15 +3445,27 @@ static SDValue emitComparison(SDValue LHS, SDValue RHS, ISD::CondCode CC,
34433445
// register to WZR/XZR if it ends up being unused.
34443446
unsigned Opcode = AArch64ISD::SUBS;
34453447

3446-
if (isCMN(RHS, CC)) {
3448+
if (isCMN(RHS, RHS.getOperand(1), CC, DAG)) {
34473449
// Can we combine a (CMP op1, (sub 0, op2) into a CMN instruction ?
34483450
Opcode = AArch64ISD::ADDS;
34493451
RHS = RHS.getOperand(1);
3450-
} else if (isCMN(LHS, CC)) {
3452+
} else if (isCMN(LHS, RHS, CC, DAG) &&
3453+
(!isUnsignedIntSetCC(CC) ||
3454+
isCMN(LHS, LHS.getOperand(1), CC, DAG))) {
34513455
// As we are looking for EQ/NE compares, the operands can be commuted ; can
34523456
// we combine a (CMP (sub 0, op1), op2) into a CMN instruction ?
3457+
// Not swapping operands, but negation requires inversion
3458+
CC = ISD::getSetCCSwappedOperands(CC);
34533459
Opcode = AArch64ISD::ADDS;
34543460
LHS = LHS.getOperand(1);
3461+
} else if (isCMN(LHS, LHS.getOperand(1), CC, DAG) &&
3462+
(!isUnsignedIntSetCC(CC) || isCMN(LHS, RHS, CC, DAG))) {
3463+
// As we are looking for EQ/NE compares, the operands can be commuted ; can
3464+
// we combine a (CMP (sub 0, op1), op2) into a CMN instruction ?
3465+
std::swap(LHS, RHS);
3466+
CC = ISD::getSetCCSwappedOperands(CC);
3467+
Opcode = AArch64ISD::ADDS;
3468+
RHS = RHS.getOperand(1);
34553469
} else if (isNullConstant(RHS) && !isUnsignedIntSetCC(CC)) {
34563470
if (LHS.getOpcode() == ISD::AND) {
34573471
// Similarly, (CMP (and X, Y), 0) can be implemented with a TST
@@ -3551,11 +3565,24 @@ static SDValue emitConditionalComparison(SDValue LHS, SDValue RHS,
35513565
}
35523566
} else if (RHS.getOpcode() == ISD::SUB) {
35533567
SDValue SubOp0 = RHS.getOperand(0);
3554-
if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
3568+
if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE ||
3569+
DAG.isKnownNeverZero(RHS.getOperand(1)))) {
35553570
// See emitComparison() on why we can only do this for SETEQ and SETNE.
35563571
Opcode = AArch64ISD::CCMN;
35573572
RHS = RHS.getOperand(1);
35583573
}
3574+
} else if (LHS.getOpcode() == ISD::SUB) {
3575+
SDValue SubOp0 = RHS.getOperand(0);
3576+
if (isNullConstant(SubOp0) &&
3577+
(CC == ISD::SETEQ || CC == ISD::SETNE ||
3578+
(DAG.isKnownNeverZero(LHS.getOperand(1)) &&
3579+
(!isUnsignedIntSetCC(CC) || DAG.isKnownNeverZero(RHS))))) {
3580+
// See emitComparison() on why we can only do this for SETEQ and SETNE.
3581+
std::swap(LHS, RHS);
3582+
CC = ISD::getSetCCSwappedOperands(CC);
3583+
Opcode = AArch64ISD::CCMN;
3584+
RHS = RHS.getOperand(1);
3585+
}
35593586
}
35603587
if (Opcode == 0)
35613588
Opcode = AArch64ISD::CCMP;
@@ -3872,8 +3899,8 @@ static SDValue getAArch64Cmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
38723899
// can be turned into:
38733900
// cmp w12, w11, lsl #1
38743901
if (!isa<ConstantSDNode>(RHS) || !isLegalArithImmed(RHS->getAsZExtVal())) {
3875-
SDValue TheLHS = isCMN(LHS, CC) ? LHS.getOperand(1) : LHS;
3876-
3902+
SDValue TheLHS =
3903+
isCMN(LHS, LHS.getOperand(1), CC, DAG) ? LHS.getOperand(1) : LHS;
38773904
if (getCmpOperandFoldingProfit(TheLHS) > getCmpOperandFoldingProfit(RHS)) {
38783905
std::swap(LHS, RHS);
38793906
CC = ISD::getSetCCSwappedOperands(CC);

llvm/test/CodeGen/AArch64/cmp-chains.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ define i32 @neg_range_int_cmn(i32 %a, i32 %b, i32 %c) {
263263
; SDISEL-LABEL: neg_range_int_cmn:
264264
; SDISEL: // %bb.0:
265265
; SDISEL-NEXT: orr w8, w2, #0x1
266-
; SDISEL-NEXT: neg w8, w8
267-
; SDISEL-NEXT: cmp w8, w0
266+
; SDISEL-NEXT: cmn w0, w8
268267
; SDISEL-NEXT: ccmn w1, #3, #0, le
269268
; SDISEL-NEXT: csel w0, w1, w0, gt
270269
; SDISEL-NEXT: ret

llvm/test/CodeGen/AArch64/cmp-select-sign.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ define i32 @or_neg(i32 %x, i32 %y) {
266266
; CHECK-LABEL: or_neg:
267267
; CHECK: // %bb.0:
268268
; CHECK-NEXT: orr w8, w0, #0x1
269-
; CHECK-NEXT: neg w8, w8
270-
; CHECK-NEXT: cmp w8, w1
269+
; CHECK-NEXT: cmn w1, w8
271270
; CHECK-NEXT: cset w0, gt
272271
; CHECK-NEXT: ret
273272
%3 = or i32 %x, 1

0 commit comments

Comments
 (0)