]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
simplify-rtx: Implement constant folding of SS_TRUNCATE, US_TRUNCATE
authorKyrylo Tkachov <kyrylo.tkachov@arm.com>
Mon, 12 Jun 2023 10:42:29 +0000 (11:42 +0100)
committerKyrylo Tkachov <kyrylo.tkachov@arm.com>
Mon, 12 Jun 2023 12:25:45 +0000 (13:25 +0100)
This patch implements RTL constant-folding for the SS_TRUNCATE and US_TRUNCATE codes.
The semantics are a clamping operation on the argument with the min and max of the narrow mode,
followed by a truncation. The signedness of the clamp and the min/max extrema is derived from
the signedness of the saturating operation.

We have a number of instructions in aarch64 that use SS_TRUNCATE and US_TRUNCATE to represent
their operations and we have pretty thorough runtime tests in gcc.target/aarch64/advsimd-intrinsics/vqmovn*.c.
With this patch the instructions are folded away at optimisation levels and the correctness checks still
pass.

Bootstrapped and tested on aarch64-none-linux-gnu and aarch64_be-none-elf.

gcc/ChangeLog:

* simplify-rtx.cc (simplify_const_unary_operation):
Handle US_TRUNCATE, SS_TRUNCATE.

gcc/simplify-rtx.cc

index 276be67aa67247dd46361ab9badc46ab089d6df0..21b7eb484d05818bb563e086e07a6152a3a3c6b7 100644 (file)
@@ -2131,6 +2131,20 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode,
          result = wide_int::from (op0, width, UNSIGNED);
          break;
 
+       case US_TRUNCATE:
+       case SS_TRUNCATE:
+         {
+           signop sgn = code == US_TRUNCATE ? UNSIGNED : SIGNED;
+           wide_int nmax
+             = wide_int::from (wi::max_value (width, sgn),
+                               GET_MODE_PRECISION (imode), sgn);
+           wide_int nmin
+             = wide_int::from (wi::min_value (width, sgn),
+                               GET_MODE_PRECISION (imode), sgn);
+           result = wi::min (wi::max (op0, nmin, sgn), nmax, sgn);
+           result = wide_int::from (result, width, sgn);
+           break;
+         }
        case SIGN_EXTEND:
          result = wide_int::from (op0, width, SIGNED);
          break;