diff mbox series

[v1] Match: Support form 4 for unsigned integer .SAT_TRUNC

Message ID 20240820075136.2378782-1-pan2.li@intel.com
State New
Headers show
Series [v1] Match: Support form 4 for unsigned integer .SAT_TRUNC | expand

Commit Message

Li, Pan2 Aug. 20, 2024, 7:51 a.m. UTC
From: Pan Li <pan2.li@intel.com>

This patch would like to support the form 4 of the unsigned integer
.SAT_TRUNC. Aka below example:

Form 4:
  #define DEF_SAT_U_TRUC_FMT_4(NT, WT)           \
  NT __attribute__((noinline))                   \
  sat_u_truc_##WT##_to_##NT##_fmt_4 (WT x)       \
  {                                              \
    bool not_overflow = x <= (WT)(NT)(-1);       \
    return ((NT)x) | (NT)((NT)not_overflow - 1); \
  }

DEF_SAT_U_TRUC_FMT_4(uint32_t, uint64_t)

Before this patch:
   4   │ __attribute__((noinline))
   5   │ uint8_t sat_u_truc_uint32_t_to_uint8_t_fmt_4 (uint32_t x)
   6   │ {
   7   │   _Bool not_overflow;
   8   │   unsigned char _1;
   9   │   unsigned char _2;
  10   │   unsigned char _3;
  11   │   uint8_t _6;
  12   │
  13   │ ;;   basic block 2, loop depth 0
  14   │ ;;    pred:       ENTRY
  15   │   not_overflow_5 = x_4(D) <= 255;
  16   │   _1 = (unsigned char) x_4(D);
  17   │   _2 = (unsigned char) not_overflow_5;
  18   │   _3 = _2 + 255;
  19   │   _6 = _1 | _3;
  20   │   return _6;
  21   │ ;;    succ:       EXIT
  22   │
  23   │ }

After this patch:
   4   │ __attribute__((noinline))
   5   │ uint8_t sat_u_truc_uint32_t_to_uint8_t_fmt_4 (uint32_t x)
   6   │ {
   7   │   uint8_t _6;
   8   │
   9   │ ;;   basic block 2, loop depth 0
  10   │ ;;    pred:       ENTRY
  11   │   _6 = .SAT_TRUNC (x_4(D)); [tail call]
  12   │   return _6;
  13   │ ;;    succ:       EXIT
  14   │
  15   │ }

The below test suites are passed for this patch.
* The rv64gcv fully regression test.
* The x86 bootstrap test.
* The x86 fully regression test.

gcc/ChangeLog:

	* match.pd: Add form 4 for unsigned .SAT_TRUNC matching.

Signed-off-by: Pan Li <pan2.li@intel.com>
---
 gcc/match.pd | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Richard Biener Aug. 22, 2024, 10:39 a.m. UTC | #1
On Tue, Aug 20, 2024 at 9:52 AM <pan2.li@intel.com> wrote:
>
> From: Pan Li <pan2.li@intel.com>
>
> This patch would like to support the form 4 of the unsigned integer
> .SAT_TRUNC. Aka below example:
>
> Form 4:
>   #define DEF_SAT_U_TRUC_FMT_4(NT, WT)           \
>   NT __attribute__((noinline))                   \
>   sat_u_truc_##WT##_to_##NT##_fmt_4 (WT x)       \
>   {                                              \
>     bool not_overflow = x <= (WT)(NT)(-1);       \
>     return ((NT)x) | (NT)((NT)not_overflow - 1); \
>   }
>
> DEF_SAT_U_TRUC_FMT_4(uint32_t, uint64_t)
>
> Before this patch:
>    4   │ __attribute__((noinline))
>    5   │ uint8_t sat_u_truc_uint32_t_to_uint8_t_fmt_4 (uint32_t x)
>    6   │ {
>    7   │   _Bool not_overflow;
>    8   │   unsigned char _1;
>    9   │   unsigned char _2;
>   10   │   unsigned char _3;
>   11   │   uint8_t _6;
>   12   │
>   13   │ ;;   basic block 2, loop depth 0
>   14   │ ;;    pred:       ENTRY
>   15   │   not_overflow_5 = x_4(D) <= 255;
>   16   │   _1 = (unsigned char) x_4(D);
>   17   │   _2 = (unsigned char) not_overflow_5;
>   18   │   _3 = _2 + 255;
>   19   │   _6 = _1 | _3;
>   20   │   return _6;
>   21   │ ;;    succ:       EXIT
>   22   │
>   23   │ }
>
> After this patch:
>    4   │ __attribute__((noinline))
>    5   │ uint8_t sat_u_truc_uint32_t_to_uint8_t_fmt_4 (uint32_t x)
>    6   │ {
>    7   │   uint8_t _6;
>    8   │
>    9   │ ;;   basic block 2, loop depth 0
>   10   │ ;;    pred:       ENTRY
>   11   │   _6 = .SAT_TRUNC (x_4(D)); [tail call]
>   12   │   return _6;
>   13   │ ;;    succ:       EXIT
>   14   │
>   15   │ }

OK.

Richard.

> The below test suites are passed for this patch.
> * The rv64gcv fully regression test.
> * The x86 bootstrap test.
> * The x86 fully regression test.
>
> gcc/ChangeLog:
>
>         * match.pd: Add form 4 for unsigned .SAT_TRUNC matching.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/match.pd | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index c9c8478d286..5a2f777e65f 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3311,6 +3311,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    }
>    (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
>
> +/* Unsigned saturation truncate, case 3, sizeof (WT) > sizeof (NT).
> +   SAT_U_TRUNC = (NT)X | ((NT)(X <= (WT)-1) + (NT)-1).  */
> +(match (unsigned_integer_sat_trunc @0)
> + (bit_ior:c (plus:c (convert (le @0 INTEGER_CST@1)) INTEGER_CST@2)
> +           (convert @0))
> + (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
> +      && TYPE_UNSIGNED (TREE_TYPE (@0)))
> + (with
> +  {
> +   unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
> +   unsigned otype_precision = TYPE_PRECISION (type);
> +   wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
> +   wide_int max = wi::mask (otype_precision, false, otype_precision);
> +   wide_int int_cst_1 = wi::to_wide (@1);
> +   wide_int int_cst_2 = wi::to_wide (@2);
> +  }
> +  (if (wi::eq_p (trunc_max, int_cst_1) && wi::eq_p (max, int_cst_2))))))
> +
>  /* x >  y  &&  x != XXX_MIN  -->  x > y
>     x >  y  &&  x == XXX_MIN  -->  false . */
>  (for eqne (eq ne)
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index c9c8478d286..5a2f777e65f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3311,6 +3311,24 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   }
   (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
 
+/* Unsigned saturation truncate, case 3, sizeof (WT) > sizeof (NT).
+   SAT_U_TRUNC = (NT)X | ((NT)(X <= (WT)-1) + (NT)-1).  */
+(match (unsigned_integer_sat_trunc @0)
+ (bit_ior:c (plus:c (convert (le @0 INTEGER_CST@1)) INTEGER_CST@2)
+	    (convert @0))
+ (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
+      && TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (with
+  {
+   unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
+   unsigned otype_precision = TYPE_PRECISION (type);
+   wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
+   wide_int max = wi::mask (otype_precision, false, otype_precision);
+   wide_int int_cst_1 = wi::to_wide (@1);
+   wide_int int_cst_2 = wi::to_wide (@2);
+  }
+  (if (wi::eq_p (trunc_max, int_cst_1) && wi::eq_p (max, int_cst_2))))))
+
 /* x >  y  &&  x != XXX_MIN  -->  x > y
    x >  y  &&  x == XXX_MIN  -->  false . */
 (for eqne (eq ne)