[AArch64] Improve the codegen for sdiv 2 (#98324)

Same as X86, , if X's size is BitWidth, then X sdiv 2 can be
expressived as
```
  X += X >> (BitWidth - 1)
  X = X >> 1
```

Fix https://github.com/llvm/llvm-project/issues/97884
This commit is contained in:
Allen 2024-07-12 09:06:42 +08:00 committed by GitHub
parent 941f794e49
commit 1bafe77d77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 8 deletions

View File

@ -17740,6 +17740,12 @@ AArch64TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
!(Divisor.isPowerOf2() || Divisor.isNegatedPowerOf2()))
return SDValue();
// If the divisor is 2 or -2, the default expansion is better. It will add
// (N->getValueType(0) >> (BitWidth - 1)) to it before shifting right.
if (Divisor == 2 ||
Divisor == APInt(Divisor.getBitWidth(), -2, /*isSigned*/ true))
return SDValue();
return TargetLowering::buildSDIVPow2WithCMov(N, Divisor, DAG, Created);
}

View File

@ -202,9 +202,8 @@ define <4 x i32> @test_bit_sink_operand(<4 x i32> %src, <4 x i32> %dst, <4 x i32
; CHECK-SD: // %bb.0: // %entry
; CHECK-SD-NEXT: sub sp, sp, #32
; CHECK-SD-NEXT: .cfi_def_cfa_offset 32
; CHECK-SD-NEXT: cmp w0, #0
; CHECK-SD-NEXT: add w8, w0, w0, lsr #31
; CHECK-SD-NEXT: mov w9, wzr
; CHECK-SD-NEXT: cinc w8, w0, lt
; CHECK-SD-NEXT: asr w8, w8, #1
; CHECK-SD-NEXT: .LBB11_1: // %do.body
; CHECK-SD-NEXT: // =>This Inner Loop Header: Depth=1

View File

@ -90,8 +90,7 @@ define i64 @test7(i64 %x) {
define i64 @test8(i64 %x) {
; ISEL-LABEL: test8:
; ISEL: // %bb.0:
; ISEL-NEXT: cmp x0, #0
; ISEL-NEXT: cinc x8, x0, lt
; ISEL-NEXT: add x8, x0, x0, lsr #63
; ISEL-NEXT: asr x0, x8, #1
; ISEL-NEXT: ret
;
@ -110,10 +109,8 @@ define i32 @sdiv_int(i32 %begin, i32 %first) #0 {
; ISEL-LABEL: sdiv_int:
; ISEL: // %bb.0:
; ISEL-NEXT: sub w8, w0, w1
; ISEL-NEXT: add w9, w8, #1
; ISEL-NEXT: add w10, w8, #2
; ISEL-NEXT: cmp w9, #0
; ISEL-NEXT: csinc w8, w10, w8, lt
; ISEL-NEXT: add w8, w8, #1
; ISEL-NEXT: add w8, w8, w8, lsr #31
; ISEL-NEXT: sub w0, w0, w8, asr #1
; ISEL-NEXT: ret
;