llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[DAG] missed "X div/rem/fdiv (select C, C1, C2) --> select C, (X div/rem/fdiv C1), (X div/rem/fdiv C2)"

Open Andarwinux opened this issue 3 months ago • 7 comments

https://godbolt.org/z/W6Kajbcsr

originally found from https://github.com/gcc-mirror/gcc/commit/298071eb7e88b01adc6a88f9eba797edaedec170

expand: Split divisions with near-power-of-two divisors [PR middle-end/125708]
When range info proves that a TRUNC_DIV_EXPR divisor is either N or N
+ 1, and one value is a positive power of two, expand the operation as
two constant divisions selected by a conditional move.

Only do this for speed, when conditional moves are available.  Cost
the split sequence against a plain DIV/UDIV and keep the original
expansion unless the split is cheaper.
unsigned
foo3 (unsigned a, _Bool b)
{
  return a / (4 + b);
}

gcc:

"foo3":
        mov     eax, edi
        shr     eax, 2
        mov     edx, 3435973837
        mov     edi, edi
        imul    rdi, rdx
        shr     rdi, 34
        test    sil, sil
        cmovne  eax, edi
        ret

clang:

foo3:
        mov     eax, edi
        or      esi, 4
        xor     edx, edx
        div     esi
        ret

gcc avoided the costly division.

Update:

The pattern can be generalized to X div/rem/fdiv (select C, C1, C2) --> select C, (X div/rem/fdiv C1), (X div/rem/fdiv C2), since this exposes significantly lower cost division-by-constant patterns for autovectorization, even when the pairs are not pow2+nonpow2, it is highly meaningful.

Andarwinux avatar Aug 06 '26 12:08 Andarwinux

I'm wondering if this optimization might not be suitable for the middle-end, as we aren't sure if the target machine supports cmov, which could lead to a performance regression.

TPPPP72 avatar Aug 11 '26 09:08 TPPPP72

I'm wondering if this optimization might not be suitable for the middle-end, as we aren't sure if the target machine supports cmov, which could lead to a performance regression.

Will SDAG be a more suitable choice?

Andarwinux avatar Aug 11 '26 09:08 Andarwinux

Will SDAG be a more suitable choice?

I'm not entirely sure which specific part of the backend this falls under, as I don't know much about backend development >_<|||.

TPPPP72 avatar Aug 11 '26 09:08 TPPPP72

Will SDAG be a more suitable choice?

I'm not entirely sure which specific part of the backend this falls under, as I don't know much about backend development >_<|||.

Constant division to multiplication shift optimization is handled by SDAG, but I think best to do canonicalization in InstCombine earlier, as this may help subsequent vectorization.

I think most backends support cmov, and even backends that unsupported can undo this later.

Andarwinux avatar Aug 11 '26 09:08 Andarwinux

vector case: https://godbolt.org/z/eWdW6vsbd

void foo3(unsigned *__restrict a, bool *__restrict b, unsigned *__restrict c) {
  for (int i = 0; i < 16; i++) {
    c[i] = a[i] / (4 + b[i]);
  }
}

void foo4(unsigned *__restrict a, bool *__restrict b, unsigned *__restrict c) {
  for (int i = 0; i < 16; i++) {
    c[i] = b[i] ? a[i] / 5 : a[i] / 4;
  }
}
_Z4foo3PjPbS_:
  movq %rdx, %rcx
  xorl %r8d, %r8d
.LBB0_1:
  movl (%rdi,%r8,4), %eax
  movzbl (%rsi,%r8), %r9d
  orl $4, %r9d
  xorl %edx, %edx
  divl %r9d
  movl %eax, (%rcx,%r8,4)
  incq %r8
  cmpq $16, %r8
  jne .LBB0_1
  retq

.LCPI1_0:
  .long 3435973837
.LCPI1_1:
  .long 1
  .long 17
  .long 3
  .long 19
  .long 5
  .long 21
  .long 7
  .long 23
  .long 9
  .long 25
  .long 11
  .long 27
  .long 13
  .long 29
  .long 15
  .long 31
_Z4foo4PjPbS_:
  vpsllw $7, (%rsi), %xmm0
  vpmovb2m %xmm0, %k1
  vmovdqu64 (%rdi), %zmm0
  vpsrld $2, %zmm0, %zmm1
  vpbroadcastd .LCPI1_0(%rip), %zmm2
  vpmuludq %zmm2, %zmm0, %zmm3
  vpshufd $245, %zmm0, %zmm0
  vpmuludq %zmm2, %zmm0, %zmm0
  vmovdqa64 .LCPI1_1(%rip), %zmm2
  vpermi2d %zmm0, %zmm3, %zmm2
  vpsrld $2, %zmm2, %zmm1 {%k1}
  vmovdqu64 %zmm1, (%rdx)
  vzeroupper
  retq

Andarwinux avatar Aug 11 '26 09:08 Andarwinux

For general cases where the divisor is not near pow2, there may be another optimization method: vectorization.

from

uint32_t foo(uint32_t a, bool b) { return a / (12 + b); }

uint32_t bar(uint32_t a, bool b) { return b ? a / 13 : a / 12; }

to

typedef uint32_t u32 [[clang::ext_vector_type(2)]];
uint32_t baz(uint32_t a, bool b) {
  u32 q = (u32){a, a} / (u32){13, 12};
  return b ? q[0] : q[1];
}
foo(unsigned int, bool):
        or      esi, 12
        mov     eax, edi
        xor     edx, edx
        div     esi
        ret

bar(unsigned int, bool):
        test    esi, esi
        je      .LBB1_2
        mov     eax, edi
        imul    rax, rax, 1321528399
        shr     rax, 34
        ret
.LBB1_2:
        mov     eax, 2863311531
        mov     ecx, edi
        imul    rax, rcx
        shr     rax, 35
        ret
.LCPI0_2:
        .long   2
        .long   3
        .zero   4
        .zero   4
.LCPI0_3:
        .byte   1
        .byte   5
        .byte   0
        .byte   0
.LCPI0_4:
        .long   1321528399
        .long   2863311531
baz(unsigned int, bool):
        mov     eax, -1431655765
        vpbroadcastd    xmm0, edi
        vpmovsxbd       xmm2, dword ptr [rip + .LCPI0_3]
        test    esi, esi
        vmovd   xmm1, eax
        vpmuludq        xmm1, xmm0, xmm1
        vpmuludq        xmm0, xmm0, qword ptr [rip + .LCPI0_4]{1to2}
        vpermi2d        xmm2, xmm0, xmm1
        vpsrlvd xmm0, xmm2, xmmword ptr [rip + .LCPI0_2]
        vmovd   ecx, xmm0
        vpextrd eax, xmm0, 1
        cmovne  eax, ecx
        ret

This method has some problems. To be truly profitable, it requires backend support like #175305 to improve vector codegen for division by constant (for i32).

FYI, for uint16, looks like it works great:

.LCPI0_0:
        .short  20165
        .short  43691
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
.LCPI0_1:
        .short  2
        .short  3
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
baz(unsigned short, bool):
        vpbroadcastw    xmm0, edi
        vpmulhuw        xmm0, xmm0, xmmword ptr [rip + .LCPI0_0]
        test    esi, esi
        vpsrlvw xmm0, xmm0, xmmword ptr [rip + .LCPI0_1]
        vmovd   ecx, xmm0
        vpextrw eax, xmm0, 1
        cmovne  eax, ecx
        ret

and vector case:

#include <stdint.h>

typedef uint16_t v2u16 [[clang::ext_vector_type(2)]];
typedef uint16_t v4u16 [[clang::ext_vector_type(4)]];
typedef bool v2i1 [[clang::ext_vector_type(2)]];

v2u16 foo(v2u16 a, v2i1 b) {
  return a / (12 + __builtin_convertvector(b, v2u16));
}

v2u16 bar(v2u16 a, v2i1 b) {
  v4u16 q = (v4u16){a[0], a[1], a[0], a[1]} / (v4u16){13, 13, 12, 12};
  return (v2u16){b[0] ? q[0] : q[2], b[1] ? q[1] : q[3]};
}

v2u16 foo2(v2u16 a, v2i1 b) {
  return b ? a / 137 : a / 71;
}

v2u16 bar2(v2u16 a, v2i1 b) {
  v4u16 q = (v4u16){a[0], a[1], a[0], a[1]} / (v4u16){137, 71, 137, 71};
  return (v2u16){b[0] ? q[0] : q[2], b[1] ? q[1] : q[3]};
}
.LCPI0_1:
        .short  13
        .short  13
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
.LCPI0_2:
        .short  12
foo(unsigned short vector[2], bool vector[2]):
        vpbroadcastw    xmm1, word ptr [rip + .LCPI0_2]
        kmovd   k1, esi
        vmovd   xmm0, edi
        vpmovzxwd       xmm0, xmm0
        vcvtdq2ps       xmm0, xmm0
        vmovdqu16       xmm1 {k1}, xmmword ptr [rip + .LCPI0_1]
        vpmovzxwd       xmm1, xmm1
        vcvtdq2ps       xmm1, xmm1
        vdivps  xmm0, xmm0, xmm1
        vcvttps2dq      xmm0, xmm0
        vpshuflw        xmm0, xmm0, 232
        vmovd   eax, xmm0
        ret

.LCPI1_0:
        .short  20165
        .short  20165
        .short  43691
        .short  43691
        .zero   2
        .zero   2
        .zero   2
        .zero   2
.LCPI1_1:
        .short  2
        .short  2
        .short  3
        .short  3
        .zero   2
        .zero   2
        .zero   2
        .zero   2
bar(unsigned short vector[2], bool vector[2]):
        vpbroadcastd    xmm0, edi
        vpmulhuw        xmm0, xmm0, xmmword ptr [rip + .LCPI1_0]
        kmovd   k1, esi
        vpsrlvw xmm0, xmm0, xmmword ptr [rip + .LCPI1_1]
        vpshufd xmm1, xmm0, 85
        vmovdqu16       xmm1 {k1}, xmm0
        vmovd   eax, xmm1
        ret

.LCPI2_1:
        .short  137
        .short  137
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
        .zero   2
.LCPI2_2:
        .short  71
foo2(unsigned short vector[2], bool vector[2]):
        vpbroadcastw    xmm1, word ptr [rip + .LCPI2_2]
        kmovd   k1, esi
        vmovd   xmm0, edi
        vpmovzxwd       xmm0, xmm0
        vcvtdq2ps       xmm0, xmm0
        vmovdqu16       xmm1 {k1}, xmmword ptr [rip + .LCPI2_1]
        vpmovzxwd       xmm1, xmm1
        vcvtdq2ps       xmm1, xmm1
        vdivps  xmm0, xmm0, xmm1
        vcvttps2dq      xmm0, xmm0
        vpshuflw        xmm0, xmm0, 232
        vmovd   eax, xmm0
        ret

.LCPI3_0:
        .short  61231
        .short  59075
        .short  61231
        .short  59075
        .zero   2
        .zero   2
        .zero   2
        .zero   2
.LCPI3_1:
        .short  7
        .short  6
        .short  7
        .short  6
        .zero   2
        .zero   2
        .zero   2
        .zero   2
bar2(unsigned short vector[2], bool vector[2]):
        vpbroadcastd    xmm0, edi
        vpmulhuw        xmm0, xmm0, xmmword ptr [rip + .LCPI3_0]
        kmovd   k1, esi
        vpsrlvw xmm0, xmm0, xmmword ptr [rip + .LCPI3_1]
        vpshufd xmm1, xmm0, 85
        vmovdqu16       xmm1 {k1}, xmm0
        vmovd   eax, xmm1
        ret

Andarwinux avatar Aug 20 '26 10:08 Andarwinux

@Andarwinux I decided to move this to the DAG only in #232644. The updated cost model already landed in #215124 so we get the majority of the vectorization wins and the InstCombine version #216908 seems stalled for canonicalization reasons.

as4230 avatar Oct 18 '26 23:10 as4230