Issue 243395
Summary [X86][AVX10.2] improve llvm.fptoui.sat and llvm.fptosi.sat (scalar+vector) avx10.2 codegen
Labels good first issue, backend:X86, missed-optimization
Assignees
Reporter Andarwinux
    avx10.2 include native SAT F2I, but the coverage is not complete. It should be possible to use SAT F2I + SAT I2I to improve codegen.
https://godbolt.org/z/cE7qEeof1
```llvm
define i16 @test_unsigned_i16_f64(double %f) nounwind {
 %x = call i16 @llvm.fptoui.sat(double %f)
  ret i16 %x
}

define i16 @test_signed_i16_f64(double %f) nounwind {
 %x = call i16 @llvm.fptosi.sat(double %f)
  ret i16 %x
}

define i8 @test_unsigned_i8_f64(double %f) nounwind {
 %x = call i8 @llvm.fptoui.sat(double %f)
  ret i8 %x
}

define i8 @test_signed_i8_f64(double %f) nounwind {
 %x = call i8 @llvm.fptosi.sat(double %f)
  ret i8 %x
}

define i16 @test_unsigned_i16_f32(float %f) nounwind {
 %x = call i16 @llvm.fptoui.sat(float %f)
  ret i16 %x
}

define i16 @test_signed_i16_f32(float %f) nounwind {
 %x = call i16 @llvm.fptosi.sat(float %f)
  ret i16 %x
}

define i8 @test_unsigned_i8_f32(float %f) nounwind {
 %x = call i8 @llvm.fptoui.sat(float %f)
  ret i8 %x
}

define i8 @test_signed_i8_f32(float %f) nounwind {
 %x = call i8 @llvm.fptosi.sat(float %f)
  ret i8 %x
}

define i16 @test_unsigned_i16_f16(half %f) nounwind {
 %x = call i16 @llvm.fptoui.sat(half %f)
  ret i16 %x
}

define i16 @test_signed_i16_f16(half %f) nounwind {
 %x = call i16 @llvm.fptosi.sat(half %f)
  ret i16 %x
}

define i8 @test_unsigned_i8_f16(half %f) nounwind {
 %x = call i8 @llvm.fptoui.sat(half %f)
  ret i8 %x
}

define i8 @test_signed_i8_f16(half %f) nounwind {
 %x = call i8 @llvm.fptosi.sat(half %f)
  ret i8 %x
}
```
```asm
.LCPI0_0:
        .quad 0x40efffe000000000              # double 65535
test_unsigned_i16_f64: # @test_unsigned_i16_f64
        vxorpd  xmm1, xmm1, xmm1
 vmaxsd  xmm0, xmm1, xmm0
        vmovsd  xmm1, qword ptr [rip + .LCPI0_0] # xmm1 = [6.5535E+4,0.0E+0]
        vminsd  xmm0, xmm1, xmm0
 vcvttsd2si      eax, xmm0
        ret
.LCPI1_0:
        .quad 0xc0e0000000000000              # double -32768
.LCPI1_1:
        .quad 0x40dfffc000000000              # double 32767
test_signed_i16_f64: # @test_signed_i16_f64
        vmovsd  xmm1, qword ptr [rip + .LCPI1_0] # xmm1 = [-3.2768E+4,0.0E+0]
        vmaxsd  xmm0, xmm1, xmm0
 vmovsd  xmm1, qword ptr [rip + .LCPI1_1] # xmm1 = [3.2767E+4,0.0E+0]
 vminsd  xmm0, xmm1, xmm0
        vcvttsd2si      eax, xmm0
 ret
.LCPI2_0:
        .quad   0x406fe00000000000              # double 255
test_unsigned_i8_f64:                   # @test_unsigned_i8_f64
 vxorpd  xmm1, xmm1, xmm1
        vmaxsd  xmm0, xmm1, xmm0
        vmovsd xmm1, qword ptr [rip + .LCPI2_0] # xmm1 = [2.55E+2,0.0E+0]
        vminsd xmm0, xmm1, xmm0
        vcvttsd2si      eax, xmm0
        ret
.LCPI3_0:
 .quad   0xc060000000000000              # double -128
.LCPI3_1:
 .quad   0x405fc00000000000              # double 127
test_signed_i8_f64: # @test_signed_i8_f64
        vmovsd  xmm1, qword ptr [rip + .LCPI3_0] # xmm1 = [-1.28E+2,0.0E+0]
        vmaxsd  xmm0, xmm1, xmm0
        vmovsd  xmm1, qword ptr [rip + .LCPI3_1] # xmm1 = [1.27E+2,0.0E+0]
        vminsd  xmm0, xmm1, xmm0
        vcvttsd2si eax, xmm0
        ret
.LCPI4_0:
        .long   0x477fff00 # float 65535
test_unsigned_i16_f32:                  # @test_unsigned_i16_f32
        vxorps  xmm1, xmm1, xmm1
        vmaxss xmm0, xmm1, xmm0
        vmovss  xmm1, dword ptr [rip + .LCPI4_0] # xmm1 = [6.5535E+4,0.0E+0,0.0E+0,0.0E+0]
        vminss  xmm0, xmm1, xmm0
 vcvttss2si      eax, xmm0
        ret
.LCPI5_0:
        .long   0xc7000000 # float -32768
.LCPI5_1:
        .long   0x46fffe00 # float 32767
test_signed_i16_f32:                    # @test_signed_i16_f32
        vmovss  xmm1, dword ptr [rip + .LCPI5_0] # xmm1 = [-3.2768E+4,0.0E+0,0.0E+0,0.0E+0]
        vmaxss  xmm0, xmm1, xmm0
 vmovss  xmm1, dword ptr [rip + .LCPI5_1] # xmm1 = [3.2767E+4,0.0E+0,0.0E+0,0.0E+0]
        vminss  xmm0, xmm1, xmm0
 vcvttss2si      eax, xmm0
        ret
.LCPI6_0:
        .long   0x437f0000 # float 255
test_unsigned_i8_f32:                   # @test_unsigned_i8_f32
        vxorps  xmm1, xmm1, xmm1
        vmaxss xmm0, xmm1, xmm0
        vmovss  xmm1, dword ptr [rip + .LCPI6_0] # xmm1 = [2.55E+2,0.0E+0,0.0E+0,0.0E+0]
        vminss  xmm0, xmm1, xmm0
 vcvttss2si      eax, xmm0
        ret
.LCPI7_0:
        .long   0xc3000000 # float -128
.LCPI7_1:
        .long   0x42fe0000 # float 127
test_signed_i8_f32:                     # @test_signed_i8_f32
        vmovss  xmm1, dword ptr [rip + .LCPI7_0] # xmm1 = [-1.28E+2,0.0E+0,0.0E+0,0.0E+0]
        vmaxss  xmm0, xmm1, xmm0
 vmovss  xmm1, dword ptr [rip + .LCPI7_1] # xmm1 = [1.27E+2,0.0E+0,0.0E+0,0.0E+0]
        vminss  xmm0, xmm1, xmm0
 vcvttss2si      eax, xmm0
        ret
.LCPI8_0:
        .short  0x7bff # half 65504
test_unsigned_i16_f16:                  # @test_unsigned_i16_f16
        vcvttsh2si      eax, xmm0
        xor ecx, ecx
        vxorps  xmm1, xmm1, xmm1
        vucomish        xmm0, xmm1
        cmovb   eax, ecx
        vucomish        xmm0, word ptr [rip + .LCPI8_0]
        mov     ecx, 65535
        cmova   eax, ecx
 ret
.LCPI9_0:
        .short  0xf800                          # half -32768
.LCPI9_1:
        .short  0x77ff                          # half 32752
test_signed_i16_f16:                    # @test_signed_i16_f16
 vcvttsh2si      eax, xmm0
        vmovsh  xmm1, word ptr [rip + .LCPI9_0] # xmm1 = [-3.2768E+4,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0]
 vucomish        xmm1, xmm0
        mov     ecx, 32768
        cmova   eax, ecx
        vucomish        xmm0, word ptr [rip + .LCPI9_1]
        mov ecx, 32767
        cmova   eax, ecx
        ret
.LCPI10_0:
        .short 0x5bf8                          # half 255
test_unsigned_i8_f16: # @test_unsigned_i8_f16
        vxorps  xmm1, xmm1, xmm1
 vmaxsh  xmm0, xmm1, xmm0
        vmovsh  xmm1, word ptr [rip + .LCPI10_0] # xmm1 = [2.55E+2,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0]
 vminsh  xmm0, xmm1, xmm0
        vcvttsh2si      eax, xmm0
 ret
.LCPI11_0:
        .short  0xd800                          # half -128
.LCPI11_1:
        .short  0x57f0                          # half 127
test_signed_i8_f16:                     # @test_signed_i8_f16
 vmovsh  xmm1, word ptr [rip + .LCPI11_0] # xmm1 = [-1.28E+2,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0]
        vmaxsh xmm0, xmm1, xmm0
        vmovsh  xmm1, word ptr [rip + .LCPI11_1] # xmm1 = [1.27E+2,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0,0.0E+0]
        vminsh xmm0, xmm1, xmm0
        vcvttsh2si      eax, xmm0
        ret
```
should be
```c
#include <stdint.h>
#include <immintrin.h>

uint16_t test_unsigned_i16_f64(__m128d a){
    return _mm_cvtusepi32_epi16(_mm_cvtts_pd_epu32(a))[0];
}

int16_t test_signed_i16_f64(__m128d a){
    return _mm_cvtsepi32_epi16(_mm_cvtts_pd_epi32(a))[0];
}

uint8_t test_unsigned_i8_f64(__m128d a){
    return _mm_cvtusepi32_epi8(_mm_cvtts_pd_epu32(a))[0];
}

int8_t test_signed_i8_f64(__m128d a){
    return _mm_cvtsepi32_epi8(_mm_cvtts_pd_epi32(a))[0];
}

uint16_t test_unsigned_i16_f32(__m128 a){
    return _mm_cvtusepi32_epi16(_mm_cvtts_ps_epu32(a))[0];
}

int16_t test_signed_i16_f32(__m128 a){
    return _mm_cvtsepi32_epi16(_mm_cvtts_ps_epi32(a))[0];
}

uint8_t test_unsigned_i8_f32(__m128 a){
    return _mm_ipcvtts_ps_epu8(a)[0];
}

int8_t test_signed_i8_f32(__m128 a){
 return _mm_ipcvtts_ps_epi8(a)[0];
}

uint16_t test_unsigned_i16_f16(__m128h a){
    return _mm_cvtusepi32_epi16(_mm_cvtts_ps_epu32(_mm_cvtxph_ps(a)))[0];
}

int16_t test_signed_i16_f16(__m128h a){
    return _mm_cvtsepi32_epi16(_mm_cvtts_ps_epi32(_mm_cvtxph_ps(a)))[0];
}

uint8_t test_unsigned_i8_f16(__m128h a){
    return _mm_ipcvtts_ph_epu8(a)[0];
}

int8_t test_signed_i8_f16(__m128h a){
 return _mm_ipcvtts_ph_epi8(a)[0];
}
```
```asm
test_unsigned_i16_f64:
 vcvttpd2udqs    xmm0, xmm0
        vpmovusdw       xmm0, xmm0
 vmovw   eax, xmm0
        ret

test_signed_i16_f64:
        vcvttpd2dqs xmm0, xmm0
        vpmovsdw        xmm0, xmm0
        vmovw   eax, xmm0
 ret

test_unsigned_i8_f64:
        vcvttpd2udqs    xmm0, xmm0
 vpmovusdb       xmm0, xmm0
        vmovd   eax, xmm0
 ret

test_signed_i8_f64:
        vcvttpd2dqs     xmm0, xmm0
 vpmovsdb        xmm0, xmm0
        vmovd   eax, xmm0
 ret

test_unsigned_i16_f32:
        vcvttps2udqs    xmm0, xmm0
 vpmovusdw       xmm0, xmm0
        vmovw   eax, xmm0
 ret

test_signed_i16_f32:
        vcvttps2dqs     xmm0, xmm0
 vpmovsdw        xmm0, xmm0
        vmovw   eax, xmm0
 ret

test_unsigned_i8_f32:
        vcvttps2iubs    xmm0, xmm0
 vmovd   eax, xmm0
        ret

test_signed_i8_f32:
        vcvttps2ibs xmm0, xmm0
        vmovd   eax, xmm0
 ret

test_unsigned_i16_f16:
        vcvtph2psx      xmm0, xmm0
 vcvttps2udqs    xmm0, xmm0
        vpmovusdw       xmm0, xmm0
        vmovw eax, xmm0
        ret

test_signed_i16_f16:
        vcvtph2psx xmm0, xmm0
        vcvttps2dqs     xmm0, xmm0
        vpmovsdw        xmm0, xmm0
        vmovw   eax, xmm0
        ret

test_unsigned_i8_f16:
 vcvttph2iubs    xmm0, xmm0
        vmovd   eax, xmm0
 ret

test_signed_i8_f16:
        vcvttph2ibs     xmm0, xmm0
        vmovd eax, xmm0
        ret
```
This also works for vector, the only thing to note is that vcvttps2iubs/vcvttps2ibs/vcvttph2iubs/vcvttph2ibs output i32/i16, so still need to be truncated.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to