Skip to content

Commit d140713

Browse files
committed
[AArch64] Fix Fold of Compare with Right-shifted Value
This commit folds (setcc ne (lshr x c) 0) for 64-bit types and constants >= 32. This fold already existed for other types or smaller constants but was not applicable to 64-bit types and constants >= 32 due to a comparison of the constant c with the bit size of the setcc operation. The type of this operation is legalized to i32, which does not necessarily match the type of the lshr operation. Use the bit size of the type of the lshr operation instead for the comparison. Fixes #122380.
1 parent a5d619e commit d140713

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25070,10 +25070,10 @@ static SDValue performSETCCCombine(SDNode *N,
2507025070
// setcc (srl x, imm), 0, ne ==> setcc (and x, (-1 << imm)), 0, ne
2507125071
if (Cond == ISD::SETNE && isNullConstant(RHS) &&
2507225072
LHS->getOpcode() == ISD::SRL && isa<ConstantSDNode>(LHS->getOperand(1)) &&
25073-
LHS->getConstantOperandVal(1) < VT.getScalarSizeInBits() &&
2507425073
LHS->hasOneUse()) {
2507525074
EVT TstVT = LHS->getValueType(0);
25076-
if (TstVT.isScalarInteger() && TstVT.getFixedSizeInBits() <= 64) {
25075+
if (TstVT.isScalarInteger() && TstVT.getFixedSizeInBits() <= 64 &&
25076+
LHS->getConstantOperandVal(1) < TstVT.getFixedSizeInBits()) {
2507725077
// this pattern will get better opt in emitComparison
2507825078
uint64_t TstImm = -1ULL << LHS->getConstantOperandVal(1);
2507925079
SDValue TST = DAG.getNode(ISD::AND, DL, TstVT, LHS->getOperand(0),

llvm/test/CodeGen/AArch64/shift-const-ne-0.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ define i1 @lsr_31_ne_0_64(i64 %x) {
6969
define i1 @lsr_32_ne_0_64(i64 %x) {
7070
; CHECK-LABEL: lsr_32_ne_0_64:
7171
; CHECK: // %bb.0:
72-
; CHECK-NEXT: lsr x8, x0, #32
73-
; CHECK-NEXT: cmp x8, #0
72+
; CHECK-NEXT: tst x0, #0xffffffff00000000
7473
; CHECK-NEXT: cset w0, ne
7574
; CHECK-NEXT: ret
7675
%shr = lshr i64 %x, 32
@@ -81,8 +80,7 @@ define i1 @lsr_32_ne_0_64(i64 %x) {
8180
define i1 @lsr_33_ne_0_64(i64 %x) {
8281
; CHECK-LABEL: lsr_33_ne_0_64:
8382
; CHECK: // %bb.0:
84-
; CHECK-NEXT: lsr x8, x0, #33
85-
; CHECK-NEXT: cmp x8, #0
83+
; CHECK-NEXT: tst x0, #0xfffffffe00000000
8684
; CHECK-NEXT: cset w0, ne
8785
; CHECK-NEXT: ret
8886
%shr = lshr i64 %x, 33
@@ -93,8 +91,7 @@ define i1 @lsr_33_ne_0_64(i64 %x) {
9391
define i1 @lsr_62_ne_0_64(i64 %x) {
9492
; CHECK-LABEL: lsr_62_ne_0_64:
9593
; CHECK: // %bb.0:
96-
; CHECK-NEXT: lsr x8, x0, #62
97-
; CHECK-NEXT: cmp x8, #0
94+
; CHECK-NEXT: tst x0, #0xc000000000000000
9895
; CHECK-NEXT: cset w0, ne
9996
; CHECK-NEXT: ret
10097
%shr = lshr i64 %x, 62

0 commit comments

Comments
 (0)