Skip to content

Commit 5583bfb

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

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,9 +3394,10 @@ 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, ISD::CondCode CC, SelectionDAG &DAG) {
33983398
return Op.getOpcode() == ISD::SUB && isNullConstant(Op.getOperand(0)) &&
3399-
(CC == ISD::SETEQ || CC == ISD::SETNE);
3399+
(CC == ISD::SETEQ || CC == ISD::SETNE ||
3400+
DAG.isKnownNeverZero(Op.getOperand(1)));
34003401
}
34013402

34023403
static SDValue emitStrictFPComparison(SDValue LHS, SDValue RHS, const SDLoc &dl,
@@ -3441,11 +3442,11 @@ static SDValue emitComparison(SDValue LHS, SDValue RHS, ISD::CondCode CC,
34413442
// register to WZR/XZR if it ends up being unused.
34423443
unsigned Opcode = AArch64ISD::SUBS;
34433444

3444-
if (isCMN(RHS, CC)) {
3445+
if (isCMN(RHS, CC, DAG)) {
34453446
// Can we combine a (CMP op1, (sub 0, op2) into a CMN instruction ?
34463447
Opcode = AArch64ISD::ADDS;
34473448
RHS = RHS.getOperand(1);
3448-
} else if (isCMN(LHS, CC)) {
3449+
} else if (isCMN(LHS, CC, DAG)) {
34493450
// As we are looking for EQ/NE compares, the operands can be commuted ; can
34503451
// we combine a (CMP (sub 0, op1), op2) into a CMN instruction ?
34513452
Opcode = AArch64ISD::ADDS;
@@ -3549,7 +3550,8 @@ static SDValue emitConditionalComparison(SDValue LHS, SDValue RHS,
35493550
}
35503551
} else if (RHS.getOpcode() == ISD::SUB) {
35513552
SDValue SubOp0 = RHS.getOperand(0);
3552-
if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
3553+
if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE ||
3554+
DAG.isKnownNeverZero(RHS.getOperand(1)))) {
35533555
// See emitComparison() on why we can only do this for SETEQ and SETNE.
35543556
Opcode = AArch64ISD::CCMN;
35553557
RHS = RHS.getOperand(1);
@@ -3870,7 +3872,7 @@ static SDValue getAArch64Cmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
38703872
// can be turned into:
38713873
// cmp w12, w11, lsl #1
38723874
if (!isa<ConstantSDNode>(RHS) || !isLegalArithImmed(RHS->getAsZExtVal())) {
3873-
SDValue TheLHS = isCMN(LHS, CC) ? LHS.getOperand(1) : LHS;
3875+
SDValue TheLHS = isCMN(LHS, CC, DAG) ? LHS.getOperand(1) : LHS;
38743876

38753877
if (getCmpOperandFoldingProfit(TheLHS) > getCmpOperandFoldingProfit(RHS)) {
38763878
std::swap(LHS, RHS);

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 w8, w0
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 w8, w1
271270
; CHECK-NEXT: cset w0, gt
272271
; CHECK-NEXT: ret
273272
%3 = or i32 %x, 1

0 commit comments

Comments
 (0)