Skip to content

Commit 88dddd6

Browse files
committed
[AArch64] Use isKnownNonZero to optimize to cmn instead of cmp
1 parent b27ca1b commit 88dddd6

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

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

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

3444-
if (isCMN(RHS, CC)) {
3446+
if (isCMN(RHS, RHS.getOperand(1), CC, DAG)) {
34453447
// Can we combine a (CMP op1, (sub 0, op2) into a CMN instruction ?
34463448
Opcode = AArch64ISD::ADDS;
34473449
RHS = RHS.getOperand(1);
3448-
} else if (isCMN(LHS, CC)) {
3450+
} else if (isCMN(LHS, RHS, CC, DAG)) {
34493451
// As we are looking for EQ/NE compares, the operands can be commuted ; can
34503452
// we combine a (CMP (sub 0, op1), op2) into a CMN instruction ?
3453+
// Not swapping operands, but negation requires inversion
3454+
CC = ISD::getSetCCSwappedOperands(CC);
34513455
Opcode = AArch64ISD::ADDS;
34523456
LHS = LHS.getOperand(1);
3457+
} else if (isCMN(LHS, LHS.getOperand(1), CC, DAG)) {
3458+
// As we are looking for EQ/NE compares, the operands can be commuted ; can
3459+
// we combine a (CMP (sub 0, op1), op2) into a CMN instruction ?
3460+
std::swap(LHS, RHS);
3461+
CC = ISD::getSetCCSwappedOperands(CC);
3462+
Opcode = AArch64ISD::ADDS;
3463+
RHS = RHS.getOperand(1);
34533464
} else if (isNullConstant(RHS) && !isUnsignedIntSetCC(CC)) {
34543465
if (LHS.getOpcode() == ISD::AND) {
34553466
// Similarly, (CMP (and X, Y), 0) can be implemented with a TST
@@ -3549,11 +3560,22 @@ static SDValue emitConditionalComparison(SDValue LHS, SDValue RHS,
35493560
}
35503561
} else if (RHS.getOpcode() == ISD::SUB) {
35513562
SDValue SubOp0 = RHS.getOperand(0);
3552-
if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
3563+
if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE ||
3564+
DAG.isKnownNeverZero(RHS.getOperand(1)))) {
35533565
// See emitComparison() on why we can only do this for SETEQ and SETNE.
35543566
Opcode = AArch64ISD::CCMN;
35553567
RHS = RHS.getOperand(1);
35563568
}
3569+
} else if (LHS.getOpcode() == ISD::SUB) {
3570+
SDValue SubOp0 = RHS.getOperand(0);
3571+
if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE ||
3572+
DAG.isKnownNeverZero(LHS.getOperand(1)))) {
3573+
// See emitComparison() on why we can only do this for SETEQ and SETNE.
3574+
std::swap(LHS, RHS);
3575+
CC = ISD::getSetCCSwappedOperands(CC);
3576+
Opcode = AArch64ISD::CCMN;
3577+
RHS = RHS.getOperand(1);
3578+
}
35573579
}
35583580
if (Opcode == 0)
35593581
Opcode = AArch64ISD::CCMP;
@@ -3870,9 +3892,9 @@ static SDValue getAArch64Cmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
38703892
// can be turned into:
38713893
// cmp w12, w11, lsl #1
38723894
if (!isa<ConstantSDNode>(RHS) || !isLegalArithImmed(RHS->getAsZExtVal())) {
3873-
SDValue TheLHS = isCMN(LHS, CC) ? LHS.getOperand(1) : LHS;
3874-
3875-
if (getCmpOperandFoldingProfit(TheLHS) > getCmpOperandFoldingProfit(RHS)) {
3895+
SDValue TheLHS = isCMN(LHS, LHS.getOperand(1), CC, DAG) ? LHS.getOperand(1) : LHS;
3896+
SDValue TheRHS = isCMN(RHS, RHS.getOperand(1), CC, DAG) ? RHS.getOperand(1) : RHS;
3897+
if (getCmpOperandFoldingProfit(TheLHS) > getCmpOperandFoldingProfit(TheRHS)) {
38763898
std::swap(LHS, RHS);
38773899
CC = ISD::getSetCCSwappedOperands(CC);
38783900
}

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

llvm/test/CodeGen/AArch64/urem-seteq.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ define i32 @test_urem_int_min(i32 %X) nounwind {
242242
define i32 @test_urem_allones(i32 %X) nounwind {
243243
; CHECK-LABEL: test_urem_allones:
244244
; CHECK: // %bb.0:
245-
; CHECK-NEXT: neg w8, w0
246-
; CHECK-NEXT: cmp w8, #2
245+
; CHECK-NEXT: cmn w0, #2
247246
; CHECK-NEXT: cset w0, lo
248247
; CHECK-NEXT: ret
249248
%urem = urem i32 %X, 4294967295

0 commit comments

Comments
 (0)