diff mbox series

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

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

Commit Message

Li, Pan2 Sept. 25, 2024, 2:55 a.m. UTC
From: Pan Li <pan2.li@intel.com>

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

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

DEF_SAT_S_SUB_FMT_1(int8_t, uint8_t, INT8_MIN, INT8_MAX)

Before this patch:
   4   │ __attribute__((noinline))
   5   │ int8_t sat_s_sub_int8_t_fmt_1 (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 3>; [41.00%]
  31   │   else
  32   │     goto <bb 4>; [59.00%]
  33   │ ;;    succ:       3
  34   │ ;;                4
  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_1 (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 1 matching pattern for signed SAT_SUB.
	* tree-ssa-math-opts.cc (gimple_signed_integer_sat_sub): Add new
	decl for generated SAT_SUB matching func.
	(match_unsigned_saturation_sub): Rename from...
	(match_saturation_sub): ...Rename to and add signed SAT_SUB matching.
	(math_opts_dom_walker::after_dom_children): Leverage the named
	match func for both the unsigned and signed SAT_SUB.

Signed-off-by: Pan Li <pan2.li@intel.com>
---
 gcc/match.pd              | 14 ++++++++++++++
 gcc/tree-ssa-math-opts.cc |  8 +++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

Comments

Richard Biener Sept. 25, 2024, 9:31 a.m. UTC | #1
On Wed, Sep 25, 2024 at 4:56 AM <pan2.li@intel.com> wrote:
>
> From: Pan Li <pan2.li@intel.com>
>
> This patch would like to support the form 1 of the scalar signed
> integer SAT_SUB.  Aka below example:
>
> Form 1:
>   #define DEF_SAT_S_SUB_FMT_1(T, UT, MIN, MAX) \
>   T __attribute__((noinline))                  \
>   sat_s_sub_##T##_fmt_1 (T x, T y)             \
>   {                                            \
>     T minus = (UT)x - (UT)y;                   \
>     return (x ^ y) >= 0                        \
>       ? minus                                  \
>       : (minus ^ x) >= 0                       \
>         ? minus                                \
>         : x < 0 ? MIN : MAX;                   \
>   }
>
> DEF_SAT_S_SUB_FMT_1(int8_t, uint8_t, INT8_MIN, INT8_MAX)
>
> Before this patch:
>    4   │ __attribute__((noinline))
>    5   │ int8_t sat_s_sub_int8_t_fmt_1 (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 3>; [41.00%]
>   31   │   else
>   32   │     goto <bb 4>; [59.00%]
>   33   │ ;;    succ:       3
>   34   │ ;;                4
>   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_1 (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.

Thanks,
Richard.

> gcc/ChangeLog:
>
>         * match.pd: Add case 1 matching pattern for signed SAT_SUB.
>         * tree-ssa-math-opts.cc (gimple_signed_integer_sat_sub): Add new
>         decl for generated SAT_SUB matching func.
>         (match_unsigned_saturation_sub): Rename from...
>         (match_saturation_sub): ...Rename to and add signed SAT_SUB matching.
>         (math_opts_dom_walker::after_dom_children): Leverage the named
>         match func for both the unsigned and signed SAT_SUB.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/match.pd              | 14 ++++++++++++++
>  gcc/tree-ssa-math-opts.cc |  8 +++++---
>  2 files changed, 19 insertions(+), 3 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 940292d0d49..63f7f3142c4 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3358,6 +3358,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    }
>    (if (wi::eq_p (sum, wi::uhwi (0, precision)))))))
>
> +/* Signed saturation sub, case 1:
> +   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^ (lt (bit_and:c (bit_xor:c @0 @1)
> +                      (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
> +                                                        (nop_convert @1)))))
> +           integer_zerop)
> +       (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)
> +       @2)
> + (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)
> diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
> index d61668aacfc..f04b17101db 100644
> --- a/gcc/tree-ssa-math-opts.cc
> +++ b/gcc/tree-ssa-math-opts.cc
> @@ -4024,6 +4024,7 @@ extern bool gimple_unsigned_integer_sat_sub (tree, tree*, tree (*)(tree));
>  extern bool gimple_unsigned_integer_sat_trunc (tree, tree*, tree (*)(tree));
>
>  extern bool gimple_signed_integer_sat_add (tree, tree*, tree (*)(tree));
> +extern bool gimple_signed_integer_sat_sub (tree, tree*, tree (*)(tree));
>
>  static void
>  build_saturation_binary_arith_call (gimple_stmt_iterator *gsi, internal_fn fn,
> @@ -4162,7 +4163,7 @@ match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gassign *stmt)
>   *  <bb 4> [local count: 1073741824]:
>   *  _1 = .SAT_SUB (x_2(D), y_3(D));  */
>  static void
> -match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi)
> +match_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi)
>  {
>    if (gimple_phi_num_args (phi) != 2)
>      return;
> @@ -4170,7 +4171,8 @@ match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi)
>    tree ops[2];
>    tree phi_result = gimple_phi_result (phi);
>
> -  if (gimple_unsigned_integer_sat_sub (phi_result, ops, NULL))
> +  if (gimple_unsigned_integer_sat_sub (phi_result, ops, NULL)
> +      || gimple_signed_integer_sat_sub (phi_result, ops, NULL))
>      build_saturation_binary_arith_call (gsi, phi, IFN_SAT_SUB, phi_result,
>                                         ops[0], ops[1]);
>  }
> @@ -6134,7 +6136,7 @@ math_opts_dom_walker::after_dom_children (basic_block bb)
>      {
>        gimple_stmt_iterator gsi = gsi_after_labels (bb);
>        match_saturation_add (&gsi, psi.phi ());
> -      match_unsigned_saturation_sub (&gsi, psi.phi ());
> +      match_saturation_sub (&gsi, psi.phi ());
>      }
>
>    for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 940292d0d49..63f7f3142c4 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3358,6 +3358,20 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   }
   (if (wi::eq_p (sum, wi::uhwi (0, precision)))))))
 
+/* Signed saturation sub, case 1:
+   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^ (lt (bit_and:c (bit_xor:c @0 @1)
+		       (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
+							 (nop_convert @1)))))
+	    integer_zerop)
+	(bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)
+	@2)
+ (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)
diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
index d61668aacfc..f04b17101db 100644
--- a/gcc/tree-ssa-math-opts.cc
+++ b/gcc/tree-ssa-math-opts.cc
@@ -4024,6 +4024,7 @@  extern bool gimple_unsigned_integer_sat_sub (tree, tree*, tree (*)(tree));
 extern bool gimple_unsigned_integer_sat_trunc (tree, tree*, tree (*)(tree));
 
 extern bool gimple_signed_integer_sat_add (tree, tree*, tree (*)(tree));
+extern bool gimple_signed_integer_sat_sub (tree, tree*, tree (*)(tree));
 
 static void
 build_saturation_binary_arith_call (gimple_stmt_iterator *gsi, internal_fn fn,
@@ -4162,7 +4163,7 @@  match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gassign *stmt)
  *  <bb 4> [local count: 1073741824]:
  *  _1 = .SAT_SUB (x_2(D), y_3(D));  */
 static void
-match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi)
+match_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi)
 {
   if (gimple_phi_num_args (phi) != 2)
     return;
@@ -4170,7 +4171,8 @@  match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi)
   tree ops[2];
   tree phi_result = gimple_phi_result (phi);
 
-  if (gimple_unsigned_integer_sat_sub (phi_result, ops, NULL))
+  if (gimple_unsigned_integer_sat_sub (phi_result, ops, NULL)
+      || gimple_signed_integer_sat_sub (phi_result, ops, NULL))
     build_saturation_binary_arith_call (gsi, phi, IFN_SAT_SUB, phi_result,
 					ops[0], ops[1]);
 }
@@ -6134,7 +6136,7 @@  math_opts_dom_walker::after_dom_children (basic_block bb)
     {
       gimple_stmt_iterator gsi = gsi_after_labels (bb);
       match_saturation_add (&gsi, psi.phi ());
-      match_unsigned_saturation_sub (&gsi, psi.phi ());
+      match_saturation_sub (&gsi, psi.phi ());
     }
 
   for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)