public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v1] Match: Support imm form for unsigned scalar .SAT_ADD
@ 2024-06-28  3:44 pan2.li
  2024-06-28 13:11 ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: pan2.li @ 2024-06-28  3:44 UTC (permalink / raw)
  To: gcc-patches
  Cc: juzhe.zhong, kito.cheng, richard.guenther, jeffreyalaw,
	rdapp.gcc, Pan Li

From: Pan Li <pan2.li@intel.com>

This patch would like to support the form of unsigned scalar .SAT_ADD
when one of the op is IMM.  For example as below:

Form IMM:
  #define DEF_SAT_U_ADD_IMM_FMT_1(T)       \
  T __attribute__((noinline))              \
  sat_u_add_imm_##T##_fmt_1 (T x)          \
  {                                        \
    return (T)(x + 9) >= x ? (x + 9) : -1; \
  }

DEF_SAT_U_ADD_IMM_FMT_1(uint64_t)

Before this patch:
__attribute__((noinline))
uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x)
{
  long unsigned int _1;
  uint64_t _3;

;;   basic block 2, loop depth 0
;;    pred:       ENTRY
  _1 = MIN_EXPR <x_2(D), 18446744073709551606>;
  _3 = _1 + 9;
  return _3;
;;    succ:       EXIT

}

After this patch:
__attribute__((noinline))
uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x)
{
  uint64_t _3;

;;   basic block 2, loop depth 0
;;    pred:       ENTRY
  _3 = .SAT_ADD (x_2(D), 9); [tail call]
  return _3;
;;    succ:       EXIT

}

The below test suites are passed for this patch:
1. The rv64gcv fully regression test with newlib.
2. The x86 bootstrap test.
3. The x86 fully regression test.

gcc/ChangeLog:

	* match.pd: Add imm form for .SAT_ADD matching.
	* tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children):
	Add .SAT_ADD matching under PLUS_EXPR.

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

diff --git a/gcc/match.pd b/gcc/match.pd
index 3fa3f2e8296..d738c7ee9b4 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3154,6 +3154,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (match (unsigned_integer_sat_add @0 @1)
  (cond^ (gt @0 (usadd_left_part_1@2 @0 @1)) integer_minus_onep @2))
 
+/* Unsigned saturation add, case 9 (one op is imm):
+   SAT_U_ADD = (X + 3) >= x ? (X + 3) : -1.  */
+(match (unsigned_integer_sat_add @0 @1)
+ (plus:c (min @0 INTEGER_CST@2) INTEGER_CST@1)
+ (with {
+   unsigned precision = TYPE_PRECISION (type);
+   wide_int cst_1 = wi::to_wide (@1, precision);
+   wide_int cst_2 = wi::to_wide (@2, precision);
+   wide_int max = wi::mask (precision, false, precision);
+   wide_int sum = wi::add (cst_1, cst_2);
+  }
+  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
+      && types_match (type, @0, @1) && wi::eq_p (max, sum)))))
+
+/* Unsigned saturation add, case 10 (one op is imm):
+   SAT_U_ADD = __builtin_add_overflow (X, 3, &ret) == 0 ? ret : -1.  */
+(match (unsigned_integer_sat_add @0 @1)
+ (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c@2 @0 INTEGER_CST@1)) integer_zerop)
+  integer_minus_onep (realpart @2))
+  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
+      && types_match (type, @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 3783a874699..3b5433ec000 100644
--- a/gcc/tree-ssa-math-opts.cc
+++ b/gcc/tree-ssa-math-opts.cc
@@ -6195,6 +6195,8 @@ math_opts_dom_walker::after_dom_children (basic_block bb)
 	      break;
 
 	    case PLUS_EXPR:
+	      match_unsigned_saturation_add (&gsi, as_a<gassign *> (stmt));
+	      /* fall-through  */
 	    case MINUS_EXPR:
 	      if (!convert_plusminus_to_widen (&gsi, stmt, code))
 		{
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] Match: Support imm form for unsigned scalar .SAT_ADD
  2024-06-28  3:44 [PATCH v1] Match: Support imm form for unsigned scalar .SAT_ADD pan2.li
@ 2024-06-28 13:11 ` Richard Biener
  2024-06-28 14:57   ` Li, Pan2
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Biener @ 2024-06-28 13:11 UTC (permalink / raw)
  To: pan2.li; +Cc: gcc-patches, juzhe.zhong, kito.cheng, jeffreyalaw, rdapp.gcc

On Fri, Jun 28, 2024 at 5:44 AM <pan2.li@intel.com> wrote:
>
> From: Pan Li <pan2.li@intel.com>
>
> This patch would like to support the form of unsigned scalar .SAT_ADD
> when one of the op is IMM.  For example as below:
>
> Form IMM:
>   #define DEF_SAT_U_ADD_IMM_FMT_1(T)       \
>   T __attribute__((noinline))              \
>   sat_u_add_imm_##T##_fmt_1 (T x)          \
>   {                                        \
>     return (T)(x + 9) >= x ? (x + 9) : -1; \
>   }
>
> DEF_SAT_U_ADD_IMM_FMT_1(uint64_t)
>
> Before this patch:
> __attribute__((noinline))
> uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x)
> {
>   long unsigned int _1;
>   uint64_t _3;
>
> ;;   basic block 2, loop depth 0
> ;;    pred:       ENTRY
>   _1 = MIN_EXPR <x_2(D), 18446744073709551606>;
>   _3 = _1 + 9;
>   return _3;
> ;;    succ:       EXIT
>
> }
>
> After this patch:
> __attribute__((noinline))
> uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x)
> {
>   uint64_t _3;
>
> ;;   basic block 2, loop depth 0
> ;;    pred:       ENTRY
>   _3 = .SAT_ADD (x_2(D), 9); [tail call]
>   return _3;
> ;;    succ:       EXIT
>
> }
>
> The below test suites are passed for this patch:
> 1. The rv64gcv fully regression test with newlib.
> 2. The x86 bootstrap test.
> 3. The x86 fully regression test.
>
> gcc/ChangeLog:
>
>         * match.pd: Add imm form for .SAT_ADD matching.
>         * tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children):
>         Add .SAT_ADD matching under PLUS_EXPR.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/match.pd              | 22 ++++++++++++++++++++++
>  gcc/tree-ssa-math-opts.cc |  2 ++
>  2 files changed, 24 insertions(+)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 3fa3f2e8296..d738c7ee9b4 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3154,6 +3154,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  (match (unsigned_integer_sat_add @0 @1)
>   (cond^ (gt @0 (usadd_left_part_1@2 @0 @1)) integer_minus_onep @2))
>
> +/* Unsigned saturation add, case 9 (one op is imm):
> +   SAT_U_ADD = (X + 3) >= x ? (X + 3) : -1.  */
> +(match (unsigned_integer_sat_add @0 @1)
> + (plus:c (min @0 INTEGER_CST@2) INTEGER_CST@1)

No :c necessary on the plus.

> + (with {
> +   unsigned precision = TYPE_PRECISION (type);
> +   wide_int cst_1 = wi::to_wide (@1, precision);
> +   wide_int cst_2 = wi::to_wide (@2, precision);

Just use wi::to_wide (@1/@2);

> +   wide_int max = wi::mask (precision, false, precision);
> +   wide_int sum = wi::add (cst_1, cst_2);
> +  }
> +  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
> +      && types_match (type, @0, @1) && wi::eq_p (max, sum)))))

Can you refactor to put the non-max/sum tests before the (with {...}?

> +
> +/* Unsigned saturation add, case 10 (one op is imm):
> +   SAT_U_ADD = __builtin_add_overflow (X, 3, &ret) == 0 ? ret : -1.  */
> +(match (unsigned_integer_sat_add @0 @1)
> + (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c@2 @0 INTEGER_CST@1)) integer_zerop)

No need for :c on the IFN_ADD_OVERFLOW.

OK with those changes.

Richard.

> +  integer_minus_onep (realpart @2))
> +  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
> +      && types_match (type, @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 3783a874699..3b5433ec000 100644
> --- a/gcc/tree-ssa-math-opts.cc
> +++ b/gcc/tree-ssa-math-opts.cc
> @@ -6195,6 +6195,8 @@ math_opts_dom_walker::after_dom_children (basic_block bb)
>               break;
>
>             case PLUS_EXPR:
> +             match_unsigned_saturation_add (&gsi, as_a<gassign *> (stmt));
> +             /* fall-through  */
>             case MINUS_EXPR:
>               if (!convert_plusminus_to_widen (&gsi, stmt, code))
>                 {
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH v1] Match: Support imm form for unsigned scalar .SAT_ADD
  2024-06-28 13:11 ` Richard Biener
@ 2024-06-28 14:57   ` Li, Pan2
  0 siblings, 0 replies; 3+ messages in thread
From: Li, Pan2 @ 2024-06-28 14:57 UTC (permalink / raw)
  To: Richard Biener
  Cc: gcc-patches, juzhe.zhong, kito.cheng, jeffreyalaw, rdapp.gcc

> OK with those changes.

Thanks Richard for comments, will make the changes and commit if no surprise from test suites.

Pan

-----Original Message-----
From: Richard Biener <richard.guenther@gmail.com> 
Sent: Friday, June 28, 2024 9:12 PM
To: Li, Pan2 <pan2.li@intel.com>
Cc: gcc-patches@gcc.gnu.org; juzhe.zhong@rivai.ai; kito.cheng@gmail.com; jeffreyalaw@gmail.com; rdapp.gcc@gmail.com
Subject: Re: [PATCH v1] Match: Support imm form for unsigned scalar .SAT_ADD

On Fri, Jun 28, 2024 at 5:44 AM <pan2.li@intel.com> wrote:
>
> From: Pan Li <pan2.li@intel.com>
>
> This patch would like to support the form of unsigned scalar .SAT_ADD
> when one of the op is IMM.  For example as below:
>
> Form IMM:
>   #define DEF_SAT_U_ADD_IMM_FMT_1(T)       \
>   T __attribute__((noinline))              \
>   sat_u_add_imm_##T##_fmt_1 (T x)          \
>   {                                        \
>     return (T)(x + 9) >= x ? (x + 9) : -1; \
>   }
>
> DEF_SAT_U_ADD_IMM_FMT_1(uint64_t)
>
> Before this patch:
> __attribute__((noinline))
> uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x)
> {
>   long unsigned int _1;
>   uint64_t _3;
>
> ;;   basic block 2, loop depth 0
> ;;    pred:       ENTRY
>   _1 = MIN_EXPR <x_2(D), 18446744073709551606>;
>   _3 = _1 + 9;
>   return _3;
> ;;    succ:       EXIT
>
> }
>
> After this patch:
> __attribute__((noinline))
> uint64_t sat_u_add_imm_uint64_t_fmt_1 (uint64_t x)
> {
>   uint64_t _3;
>
> ;;   basic block 2, loop depth 0
> ;;    pred:       ENTRY
>   _3 = .SAT_ADD (x_2(D), 9); [tail call]
>   return _3;
> ;;    succ:       EXIT
>
> }
>
> The below test suites are passed for this patch:
> 1. The rv64gcv fully regression test with newlib.
> 2. The x86 bootstrap test.
> 3. The x86 fully regression test.
>
> gcc/ChangeLog:
>
>         * match.pd: Add imm form for .SAT_ADD matching.
>         * tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children):
>         Add .SAT_ADD matching under PLUS_EXPR.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/match.pd              | 22 ++++++++++++++++++++++
>  gcc/tree-ssa-math-opts.cc |  2 ++
>  2 files changed, 24 insertions(+)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 3fa3f2e8296..d738c7ee9b4 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3154,6 +3154,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  (match (unsigned_integer_sat_add @0 @1)
>   (cond^ (gt @0 (usadd_left_part_1@2 @0 @1)) integer_minus_onep @2))
>
> +/* Unsigned saturation add, case 9 (one op is imm):
> +   SAT_U_ADD = (X + 3) >= x ? (X + 3) : -1.  */
> +(match (unsigned_integer_sat_add @0 @1)
> + (plus:c (min @0 INTEGER_CST@2) INTEGER_CST@1)

No :c necessary on the plus.

> + (with {
> +   unsigned precision = TYPE_PRECISION (type);
> +   wide_int cst_1 = wi::to_wide (@1, precision);
> +   wide_int cst_2 = wi::to_wide (@2, precision);

Just use wi::to_wide (@1/@2);

> +   wide_int max = wi::mask (precision, false, precision);
> +   wide_int sum = wi::add (cst_1, cst_2);
> +  }
> +  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
> +      && types_match (type, @0, @1) && wi::eq_p (max, sum)))))

Can you refactor to put the non-max/sum tests before the (with {...}?

> +
> +/* Unsigned saturation add, case 10 (one op is imm):
> +   SAT_U_ADD = __builtin_add_overflow (X, 3, &ret) == 0 ? ret : -1.  */
> +(match (unsigned_integer_sat_add @0 @1)
> + (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c@2 @0 INTEGER_CST@1)) integer_zerop)

No need for :c on the IFN_ADD_OVERFLOW.

OK with those changes.

Richard.

> +  integer_minus_onep (realpart @2))
> +  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
> +      && types_match (type, @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 3783a874699..3b5433ec000 100644
> --- a/gcc/tree-ssa-math-opts.cc
> +++ b/gcc/tree-ssa-math-opts.cc
> @@ -6195,6 +6195,8 @@ math_opts_dom_walker::after_dom_children (basic_block bb)
>               break;
>
>             case PLUS_EXPR:
> +             match_unsigned_saturation_add (&gsi, as_a<gassign *> (stmt));
> +             /* fall-through  */
>             case MINUS_EXPR:
>               if (!convert_plusminus_to_widen (&gsi, stmt, code))
>                 {
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-06-28 14:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-28  3:44 [PATCH v1] Match: Support imm form for unsigned scalar .SAT_ADD pan2.li
2024-06-28 13:11 ` Richard Biener
2024-06-28 14:57   ` Li, Pan2

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).