On Wed, Nov 6, 2024 at 4:06 AM Li Xu <xu...@eswincomputing.com> wrote:
>
> From: xuli <xu...@eswincomputing.com>
>
> This patch would like to support .SAT_ADD when one of the op
> is singed IMM.
>
> Form1:
> T __attribute__((noinline))                  \
> sat_s_add_imm_##T##_fmt_1##_##INDEX (T x)             \
> {                                            \
>   T sum = (UT)x + (UT)IMM;                     \
>   return (x ^ IMM) < 0                         \
>     ? sum                                    \
>     : (sum ^ x) >= 0                         \
>       ? sum                                  \
>       : x < 0 ? MIN : MAX;                   \
> }
>
> Take below form1 as example:
> DEF_SAT_S_ADD_IMM_FMT_1(0, int8_t, uint8_t, -10, INT8_MIN, INT8_MAX)
>
> Before this patch:
> __attribute__((noinline))
> int8_t sat_s_add_imm_int8_t_fmt_1_0 (int8_t x)
> {
>   int8_t sum;
>   unsigned char x.0_1;
>   unsigned char _2;
>   signed char _4;
>   int8_t _5;
>   _Bool _9;
>   signed char _10;
>   signed char _11;
>   signed char _12;
>   signed char _14;
>   signed char _16;
>
>   <bb 2> [local count: 1073741824]:
>   x.0_1 = (unsigned char) x_6(D);
>   _2 = x.0_1 + 246;
>   sum_7 = (int8_t) _2;
>   _4 = x_6(D) ^ sum_7;
>   _16 = x_6(D) ^ 9;
>   _14 = _4 & _16;
>   if (_14 < 0)
>     goto <bb 3>; [41.00%]
>   else
>     goto <bb 4>; [59.00%]
>
>   <bb 3> [local count: 259738147]:
>   _9 = x_6(D) < 0;
>   _10 = (signed char) _9;
>   _11 = -_10;
>   _12 = _11 ^ 127;
>
>   <bb 4> [local count: 1073741824]:
>   # _5 = PHI <sum_7(2), _12(3)>
>   return _5;
>
> }
>
> After this patch:
> __attribute__((noinline))
> int8_t sat_s_add_imm_int8_t_fmt_1_0 (int8_t x)
> {
>   int8_t _5;
>
>   <bb 2> [local count: 1073741824]:
>   _5 = .SAT_ADD (x_6(D), -10); [tail call]
>   return _5;
>
> }
>
> The below test suites are passed for this patch:
> 1. The rv64gcv fully regression tests.
> 2. The x86 bootstrap tests.
> 3. The x86 fully regression tests.
>
> Signed-off-by: Li Xu <xu...@eswincomputing.com>

OK.

Does tree-vect-patterns.cc also need adjustment?

Richard.

> gcc/ChangeLog:
>
>         * match.pd: Add the form1 of signed imm .SAT_ADD matching.
>         * tree-ssa-math-opts.cc (match_saturation_add): Add fold
>         convert for const_int to the type of operand 0.
> ---
>  gcc/match.pd              | 13 +++++++++++++
>  gcc/tree-ssa-math-opts.cc |  3 +++
>  2 files changed, 16 insertions(+)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index c10bf9a7b80..00988241348 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3277,6 +3277,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>         @2)
>   (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))))
>
> +/* Signed saturation add, case 6 (one op is imm):
> +   T sum = (T)((UT)X + (UT)IMM);
> +   SAT_S_ADD = (X ^ IMM) < 0 ? sum : (X ^ sum) >= 0 ? sum : (x < 0) ? MIN : 
> MAX;
> +   The T and UT are type pair like T=int8_t, UT=uint8_t.  */
> +
> +(match (signed_integer_sat_add @0 @1)
> +(cond^ (lt (bit_and:c (bit_xor:c @0 (nop_convert@2 (plus (nop_convert @0)
> +                      INTEGER_CST@1)))
> +                      (bit_xor:c @0 INTEGER_CST@3)) integer_zerop)
> +       (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value) @2)
> +(if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type)
> +     && wi::bit_and (wi::to_wide (@1), wi::to_wide (@3)) == 0)))
> +
>  /* Unsigned saturation sub, case 1 (branch with gt):
>     SAT_U_SUB = X > Y ? X - Y : 0  */
>  (match (unsigned_integer_sat_sub @0 @1)
> diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
> index 83933df6928..5f521aa6fef 100644
> --- a/gcc/tree-ssa-math-opts.cc
> +++ b/gcc/tree-ssa-math-opts.cc
> @@ -4130,6 +4130,9 @@ match_saturation_add (gimple_stmt_iterator *gsi, gphi 
> *phi)
>        && !gimple_signed_integer_sat_add (phi_result, ops, NULL))
>      return false;
>
> +  if (!TYPE_UNSIGNED (TREE_TYPE (ops[0])) && TREE_CODE (ops[1]) == 
> INTEGER_CST)
> +    ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]);
> +
>    return build_saturation_binary_arith_call_and_insert (gsi, IFN_SAT_ADD,
>                                                         phi_result, ops[0],
>                                                         ops[1]);
> --
> 2.17.1
>

Reply via email to