Issue 233789
Summary [AArch64] Widened rounding shifts, saturating narrows and usqadd not selected to single NEON ops
Labels new issue
Assignees
Reporter guyfischman
    Each of these is the exact widened definition of a single NEON instruction, but llc expands them.

```llvm
define <8 x i16> @srshr(<8 x i16> %x) {
  %e = sext <8 x i16> %x to <8 x i32>
  %a = add nsw <8 x i32> %e, splat (i32 8)
  %s = ashr <8 x i32> %a, splat (i32 4)
  %t = trunc <8 x i32> %s to <8 x i16>
  ret <8 x i16> %t
}

define <4 x i16> @sqrshrn(<4 x i32> %x) {
  %e = sext <4 x i32> %x to <4 x i64>
 %a = add nsw <4 x i64> %e, splat (i64 32)
  %s = ashr <4 x i64> %a, splat (i64 6)
  %c1 = call <4 x i64> @llvm.smax.v4i64(<4 x i64> %s, <4 x i64> splat (i64 -32768))
  %c2 = call <4 x i64> @llvm.smin.v4i64(<4 x i64> %c1, <4 x i64> splat (i64 32767))
  %t = trunc <4 x i64> %c2 to <4 x i16>
  ret <4 x i16> %t
}

define <16 x i8> @usqadd(<16 x i8> %a, <16 x i8> %b) {
 %za = zext <16 x i8> %a to <16 x i16>
  %sb = sext <16 x i8> %b to <16 x i16>
  %s = add nsw <16 x i16> %za, %sb
  %c1 = call <16 x i16> @llvm.smax.v16i16(<16 x i16> %s, <16 x i16> zeroinitializer)
  %c2 = call <16 x i16> @llvm.smin.v16i16(<16 x i16> %c1, <16 x i16> splat (i16 255))
 %t = trunc <16 x i16> %c2 to <16 x i8>
  ret <16 x i8> %t
}
```

produces https://godbolt.org/z/zTbhM9Y7z:
```llc
srshr: // @srshr
        sshll   v1.4s, v0.4h, #0
 sshll2  v0.4s, v0.8h, #0
        rshrn   v1.4h, v1.4s, #4
        rshrn2 v1.8h, v0.4s, #4
        mov     v0.16b, v1.16b
        ret
sqrshrn: // @sqrshrn
        mov     w8, #32 // =0x20
        dup     v1.2d, x8
        mov     x8, #-32768 // =0xffffffffffff8000
        saddw   v2.2d, v1.2d, v0.2s
 saddw2  v0.2d, v1.2d, v0.4s
        sshr    v0.2d, v0.2d, #6
 sshr    v1.2d, v2.2d, #6
        dup     v2.2d, x8
        mov     w8, #32767                      // =0x7fff
        cmgt    v3.2d, v1.2d, v2.2d
 cmgt    v4.2d, v0.2d, v2.2d
        bif     v1.16b, v2.16b, v3.16b
 bif     v0.16b, v2.16b, v4.16b
        dup     v2.2d, x8
        cmgt v3.2d, v2.2d, v0.2d
        cmgt    v4.2d, v2.2d, v1.2d
        bif v0.16b, v2.16b, v3.16b
        bif     v1.16b, v2.16b, v4.16b
        uzp1 v0.4s, v1.4s, v0.4s
        xtn     v0.4h, v0.4s
        ret
usqadd: // @usqadd
        ushll   v2.8h, v0.8b, #0
 ushll2  v3.8h, v0.16b, #0
        saddw   v2.8h, v2.8h, v1.8b
 saddw2  v1.8h, v3.8h, v1.16b
        sqxtun  v0.8b, v2.8h
        sqxtun2 v0.16b, v1.8h
        ret
```

Expected:
```
srshr:
  srshr   v0.8h, v0.8h, \#4
sqrshrn:
  sqrshrn v0.4h, v0.4s, \#6
usqadd:
  usqadd v0.16b, v1.16b
```

The same goes for rshrn, srshl/urshl with a splat runtime amount, and the sqshrn/sqrshrun/uqrshrn family.

Related: #147863 and #133262.

I plan to send fixes split thusly:
Generic: let foldToSaturated look through an extend to a legal source type, so truncssat_s(sext y) becomes a legal truncssat of y.
AArch64: turn exact widened rounding shifts into SRSHR_I/URSHR_I (and srshl/urshl for splat amounts), so the existing truncssat patterns pick up sqrshrn and friends.
AArch64: usqadd from the clamped mixed-sign add, and saddw/uaddw from ext(add nsw/nuw x, C).

Does that look right?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to