diff mbox series

[v1,1/2] Match: Support form 2 for scalar signed integer SAT_SUB

Message ID 20240926122438.3536337-1-pan2.li@intel.com
State New
Headers show
Series [v1,1/2] Match: Support form 2 for scalar signed integer SAT_SUB | expand

Commit Message

Li, Pan2 Sept. 26, 2024, 12:24 p.m. UTC
From: Pan Li <pan2.li@intel.com>

This patch would like to support the form 2 of the scalar signed
integer SAT_SUB.  Aka below example:

Form 2:
  #define DEF_SAT_S_SUB_FMT_2(T, UT, MIN, MAX) \
  T __attribute__((noinline))                  \
  sat_s_sub_##T##_fmt_1 (T x, T y)             \
  {                                            \
    T minus = (UT)x - (UT)y;                   \
    if ((x ^ y) >= 0 || (minus ^ x) >= 0)      \
      return minus;                            \
    return x < 0 ? MIN : MAX;                  \
  }

DEF_SAT_S_SUB_FMT_2(int8_t, uint8_t, INT8_MIN, INT8_MAX)

Before this patch:
   4   │ __attribute__((noinline))
   5   │ int8_t sat_s_sub_int8_t_fmt_2 (int8_t x, int8_t y)
   6   │ {
   7   │   int8_t minus;
   8   │   unsigned char x.0_1;
   9   │   unsigned char y.1_2;
  10   │   unsigned char _3;
  11   │   signed char _4;
  12   │   signed char _5;
  13   │   int8_t _6;
  14   │   _Bool _11;
  15   │   signed char _12;
  16   │   signed char _13;
  17   │   signed char _14;
  18   │   signed char _15;
  19   │
  20   │ ;;   basic block 2, loop depth 0
  21   │ ;;    pred:       ENTRY
  22   │   x.0_1 = (unsigned char) x_7(D);
  23   │   y.1_2 = (unsigned char) y_8(D);
  24   │   _3 = x.0_1 - y.1_2;
  25   │   minus_9 = (int8_t) _3;
  26   │   _4 = x_7(D) ^ y_8(D);
  27   │   _5 = x_7(D) ^ minus_9;
  28   │   _15 = _4 & _5;
  29   │   if (_15 >= 0)
  30   │     goto <bb 4>; [42.57%]
  31   │   else
  32   │     goto <bb 3>; [57.43%]
  33   │ ;;    succ:       4
  34   │ ;;                3
  35   │
  36   │ ;;   basic block 3, loop depth 0
  37   │ ;;    pred:       2
  38   │   _11 = x_7(D) < 0;
  39   │   _12 = (signed char) _11;
  40   │   _13 = -_12;
  41   │   _14 = _13 ^ 127;
  42   │ ;;    succ:       4
  43   │
  44   │ ;;   basic block 4, loop depth 0
  45   │ ;;    pred:       2
  46   │ ;;                3
  47   │   # _6 = PHI <minus_9(2), _14(3)>
  48   │   return _6;
  49   │ ;;    succ:       EXIT
  50   │
  51   │ }

After this patch:
   4   │ __attribute__((noinline))
   5   │ int8_t sat_s_sub_int8_t_fmt_2 (int8_t x, int8_t y)
   6   │ {
   7   │   int8_t _6;
   8   │
   9   │ ;;   basic block 2, loop depth 0
  10   │ ;;    pred:       ENTRY
  11   │   _6 = .SAT_SUB (x_7(D), y_8(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 case 2 matching pattern for signed SAT_SUB.

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

Comments

Richard Biener Oct. 1, 2024, 11:50 a.m. UTC | #1
On Thu, Sep 26, 2024 at 2:25 PM <pan2.li@intel.com> wrote:
>
> From: Pan Li <pan2.li@intel.com>
>
> This patch would like to support the form 2 of the scalar signed
> integer SAT_SUB.  Aka below example:
>
> Form 2:
>   #define DEF_SAT_S_SUB_FMT_2(T, UT, MIN, MAX) \
>   T __attribute__((noinline))                  \
>   sat_s_sub_##T##_fmt_1 (T x, T y)             \
>   {                                            \
>     T minus = (UT)x - (UT)y;                   \
>     if ((x ^ y) >= 0 || (minus ^ x) >= 0)      \
>       return minus;                            \
>     return x < 0 ? MIN : MAX;                  \
>   }
>
> DEF_SAT_S_SUB_FMT_2(int8_t, uint8_t, INT8_MIN, INT8_MAX)
>
> Before this patch:
>    4   │ __attribute__((noinline))
>    5   │ int8_t sat_s_sub_int8_t_fmt_2 (int8_t x, int8_t y)
>    6   │ {
>    7   │   int8_t minus;
>    8   │   unsigned char x.0_1;
>    9   │   unsigned char y.1_2;
>   10   │   unsigned char _3;
>   11   │   signed char _4;
>   12   │   signed char _5;
>   13   │   int8_t _6;
>   14   │   _Bool _11;
>   15   │   signed char _12;
>   16   │   signed char _13;
>   17   │   signed char _14;
>   18   │   signed char _15;
>   19   │
>   20   │ ;;   basic block 2, loop depth 0
>   21   │ ;;    pred:       ENTRY
>   22   │   x.0_1 = (unsigned char) x_7(D);
>   23   │   y.1_2 = (unsigned char) y_8(D);
>   24   │   _3 = x.0_1 - y.1_2;
>   25   │   minus_9 = (int8_t) _3;
>   26   │   _4 = x_7(D) ^ y_8(D);
>   27   │   _5 = x_7(D) ^ minus_9;
>   28   │   _15 = _4 & _5;
>   29   │   if (_15 >= 0)
>   30   │     goto <bb 4>; [42.57%]
>   31   │   else
>   32   │     goto <bb 3>; [57.43%]
>   33   │ ;;    succ:       4
>   34   │ ;;                3
>   35   │
>   36   │ ;;   basic block 3, loop depth 0
>   37   │ ;;    pred:       2
>   38   │   _11 = x_7(D) < 0;
>   39   │   _12 = (signed char) _11;
>   40   │   _13 = -_12;
>   41   │   _14 = _13 ^ 127;
>   42   │ ;;    succ:       4
>   43   │
>   44   │ ;;   basic block 4, loop depth 0
>   45   │ ;;    pred:       2
>   46   │ ;;                3
>   47   │   # _6 = PHI <minus_9(2), _14(3)>
>   48   │   return _6;
>   49   │ ;;    succ:       EXIT
>   50   │
>   51   │ }
>
> After this patch:
>    4   │ __attribute__((noinline))
>    5   │ int8_t sat_s_sub_int8_t_fmt_2 (int8_t x, int8_t y)
>    6   │ {
>    7   │   int8_t _6;
>    8   │
>    9   │ ;;   basic block 2, loop depth 0
>   10   │ ;;    pred:       ENTRY
>   11   │   _6 = .SAT_SUB (x_7(D), y_8(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.

OK.

Richard.

> gcc/ChangeLog:
>
>         * match.pd: Add case 2 matching pattern for signed SAT_SUB.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/match.pd | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 63f7f3142c4..3baf209350b 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3372,6 +3372,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>         @2)
>   (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))))
>
> +/* Signed saturation sub, case 2:
> +   T minus = (T)((UT)X - (UT)Y);
> +   SAT_S_SUB = (X ^ Y) & (X ^ minus) < 0 ? (-(T)(X < 0) ^ MAX) : minus;
> +
> +   The T and UT are type pair like T=int8_t, UT=uint8_t.  */
> +(match (signed_integer_sat_sub @0 @1)
> + (cond^ (ge (bit_and:c (bit_xor:c @0 @1)
> +                      (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
> +                                                        (nop_convert @1)))))
> +           integer_zerop)
> +       @2
> +       (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value))
> + (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))))
> +
>  /* Unsigned saturation truncate, case 1, sizeof (WT) > sizeof (NT).
>     SAT_U_TRUNC = (NT)x | (NT)(-(X > (WT)(NT)(-1))).  */
>  (match (unsigned_integer_sat_trunc @0)
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 63f7f3142c4..3baf209350b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3372,6 +3372,20 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	@2)
  (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))))
 
+/* Signed saturation sub, case 2:
+   T minus = (T)((UT)X - (UT)Y);
+   SAT_S_SUB = (X ^ Y) & (X ^ minus) < 0 ? (-(T)(X < 0) ^ MAX) : minus;
+
+   The T and UT are type pair like T=int8_t, UT=uint8_t.  */
+(match (signed_integer_sat_sub @0 @1)
+ (cond^ (ge (bit_and:c (bit_xor:c @0 @1)
+		       (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
+							 (nop_convert @1)))))
+	    integer_zerop)
+	@2
+	(bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value))
+ (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))))
+
 /* Unsigned saturation truncate, case 1, sizeof (WT) > sizeof (NT).
    SAT_U_TRUNC = (NT)x | (NT)(-(X > (WT)(NT)(-1))).  */
 (match (unsigned_integer_sat_trunc @0)