diff mbox series

[v1] Match: Support form 2 for the .SAT_TRUNC

Message ID 20240705124839.3901430-1-pan2.li@intel.com
State New
Headers show
Series [v1] Match: Support form 2 for the .SAT_TRUNC | expand

Commit Message

Li, Pan2 July 5, 2024, 12:48 p.m. UTC
From: Pan Li <pan2.li@intel.com>

This patch would like to add form 2 support for the .SAT_TRUNC.  Aka:

Form 2:
  #define DEF_SAT_U_TRUC_FMT_2(NT, WT)     \
  NT __attribute__((noinline))             \
  sat_u_truc_##WT##_to_##NT##_fmt_2 (WT x) \
  {                                        \
    bool overflow = x > (WT)(NT)(-1);      \
    return overflow ? (NT)-1 : (NT)x;      \
  }

DEF_SAT_U_TRUC_FMT_2(uint32, uint64)

Before this patch:
   3   │
   4   │ __attribute__((noinline))
   5   │ uint32_t sat_u_truc_uint64_t_to_uint32_t_fmt_2 (uint64_t x)
   6   │ {
   7   │   uint32_t _1;
   8   │   long unsigned int _3;
   9   │
  10   │ ;;   basic block 2, loop depth 0
  11   │ ;;    pred:       ENTRY
  12   │   _3 = MIN_EXPR <x_2(D), 4294967295>;
  13   │   _1 = (uint32_t) _3;
  14   │   return _1;
  15   │ ;;    succ:       EXIT
  16   │
  17   │ }

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

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

gcc/ChangeLog:

	* match.pd: Add form 2 for .SAT_TRUNC.
	* tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children):
	Add new case NOP_EXPR,  and try to match SAT_TRUNC.

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

Comments

Richard Biener July 10, 2024, 9:23 a.m. UTC | #1
On Fri, Jul 5, 2024 at 2:48 PM <pan2.li@intel.com> wrote:
>
> From: Pan Li <pan2.li@intel.com>
>
> This patch would like to add form 2 support for the .SAT_TRUNC.  Aka:
>
> Form 2:
>   #define DEF_SAT_U_TRUC_FMT_2(NT, WT)     \
>   NT __attribute__((noinline))             \
>   sat_u_truc_##WT##_to_##NT##_fmt_2 (WT x) \
>   {                                        \
>     bool overflow = x > (WT)(NT)(-1);      \
>     return overflow ? (NT)-1 : (NT)x;      \
>   }
>
> DEF_SAT_U_TRUC_FMT_2(uint32, uint64)
>
> Before this patch:
>    3   │
>    4   │ __attribute__((noinline))
>    5   │ uint32_t sat_u_truc_uint64_t_to_uint32_t_fmt_2 (uint64_t x)
>    6   │ {
>    7   │   uint32_t _1;
>    8   │   long unsigned int _3;
>    9   │
>   10   │ ;;   basic block 2, loop depth 0
>   11   │ ;;    pred:       ENTRY
>   12   │   _3 = MIN_EXPR <x_2(D), 4294967295>;
>   13   │   _1 = (uint32_t) _3;
>   14   │   return _1;
>   15   │ ;;    succ:       EXIT
>   16   │
>   17   │ }
>
> After this patch:
>    3   │
>    4   │ __attribute__((noinline))
>    5   │ uint32_t sat_u_truc_uint64_t_to_uint32_t_fmt_2 (uint64_t x)
>    6   │ {
>    7   │   uint32_t _1;
>    8   │
>    9   │ ;;   basic block 2, loop depth 0
>   10   │ ;;    pred:       ENTRY
>   11   │   _1 = .SAT_TRUNC (x_2(D)); [tail call]
>   12   │   return _1;
>   13   │ ;;    succ:       EXIT
>   14   │
>   15   │ }
>
> The below test suites are passed for this patch:
> 1. The x86 bootstrap test.
> 2. The x86 fully regression test.
> 3. The rv64gcv fully regresssion test.

OK.

Thanks,
Richard.

> gcc/ChangeLog:
>
>         * match.pd: Add form 2 for .SAT_TRUNC.
>         * tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children):
>         Add new case NOP_EXPR,  and try to match SAT_TRUNC.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/match.pd              | 17 ++++++++++++++++-
>  gcc/tree-ssa-math-opts.cc |  4 ++++
>  2 files changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 4edfa2ae2c9..3759c64d461 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3234,7 +3234,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
>        && types_match (type, @0, @1))))
>
> -/* Unsigned saturation truncate, case 1 (), sizeof (WT) > sizeof (NT).
> +/* 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)
>   (bit_ior:c (negate (convert (gt @0 INTEGER_CST@1)))
> @@ -3250,6 +3250,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    }
>    (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
>
> +/* Unsigned saturation truncate, case 2, sizeof (WT) > sizeof (NT).
> +   SAT_U_TRUNC = (NT)(MIN_EXPR (X, 255)).  */
> +(match (unsigned_integer_sat_trunc @0)
> + (convert (min @0 INTEGER_CST@1))
> + (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 int_cst = wi::to_wide (@1, itype_precision);
> +  }
> +  (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
> +
>  /* x >  y  &&  x != XXX_MIN  -->  x > y
>     x >  y  &&  x == XXX_MIN  -->  false . */
>  (for eqne (eq ne)
> diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
> index a35caf5f058..ac86be8eb94 100644
> --- a/gcc/tree-ssa-math-opts.cc
> +++ b/gcc/tree-ssa-math-opts.cc
> @@ -6170,6 +6170,10 @@ math_opts_dom_walker::after_dom_children (basic_block bb)
>               match_unsigned_saturation_sub (&gsi, as_a<gassign *> (stmt));
>               break;
>
> +           case NOP_EXPR:
> +             match_unsigned_saturation_trunc (&gsi, as_a<gassign *> (stmt));
> +             break;
> +
>             default:;
>             }
>         }
> --
> 2.34.1
>
Li, Pan2 July 10, 2024, 10:59 a.m. UTC | #2
> OK.

Committed, thanks Richard.

Pan

-----Original Message-----
From: Richard Biener <richard.guenther@gmail.com> 
Sent: Wednesday, July 10, 2024 5:24 PM
To: Li, Pan2 <pan2.li@intel.com>
Cc: gcc-patches@gcc.gnu.org; juzhe.zhong@rivai.ai; kito.cheng@gmail.com; tamar.christina@arm.com; jeffreyalaw@gmail.com; rdapp.gcc@gmail.com; Liu, Hongtao <hongtao.liu@intel.com>
Subject: Re: [PATCH v1] Match: Support form 2 for the .SAT_TRUNC

On Fri, Jul 5, 2024 at 2:48 PM <pan2.li@intel.com> wrote:
>
> From: Pan Li <pan2.li@intel.com>
>
> This patch would like to add form 2 support for the .SAT_TRUNC.  Aka:
>
> Form 2:
>   #define DEF_SAT_U_TRUC_FMT_2(NT, WT)     \
>   NT __attribute__((noinline))             \
>   sat_u_truc_##WT##_to_##NT##_fmt_2 (WT x) \
>   {                                        \
>     bool overflow = x > (WT)(NT)(-1);      \
>     return overflow ? (NT)-1 : (NT)x;      \
>   }
>
> DEF_SAT_U_TRUC_FMT_2(uint32, uint64)
>
> Before this patch:
>    3   │
>    4   │ __attribute__((noinline))
>    5   │ uint32_t sat_u_truc_uint64_t_to_uint32_t_fmt_2 (uint64_t x)
>    6   │ {
>    7   │   uint32_t _1;
>    8   │   long unsigned int _3;
>    9   │
>   10   │ ;;   basic block 2, loop depth 0
>   11   │ ;;    pred:       ENTRY
>   12   │   _3 = MIN_EXPR <x_2(D), 4294967295>;
>   13   │   _1 = (uint32_t) _3;
>   14   │   return _1;
>   15   │ ;;    succ:       EXIT
>   16   │
>   17   │ }
>
> After this patch:
>    3   │
>    4   │ __attribute__((noinline))
>    5   │ uint32_t sat_u_truc_uint64_t_to_uint32_t_fmt_2 (uint64_t x)
>    6   │ {
>    7   │   uint32_t _1;
>    8   │
>    9   │ ;;   basic block 2, loop depth 0
>   10   │ ;;    pred:       ENTRY
>   11   │   _1 = .SAT_TRUNC (x_2(D)); [tail call]
>   12   │   return _1;
>   13   │ ;;    succ:       EXIT
>   14   │
>   15   │ }
>
> The below test suites are passed for this patch:
> 1. The x86 bootstrap test.
> 2. The x86 fully regression test.
> 3. The rv64gcv fully regresssion test.

OK.

Thanks,
Richard.

> gcc/ChangeLog:
>
>         * match.pd: Add form 2 for .SAT_TRUNC.
>         * tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children):
>         Add new case NOP_EXPR,  and try to match SAT_TRUNC.
>
> Signed-off-by: Pan Li <pan2.li@intel.com>
> ---
>  gcc/match.pd              | 17 ++++++++++++++++-
>  gcc/tree-ssa-math-opts.cc |  4 ++++
>  2 files changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 4edfa2ae2c9..3759c64d461 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3234,7 +3234,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
>        && types_match (type, @0, @1))))
>
> -/* Unsigned saturation truncate, case 1 (), sizeof (WT) > sizeof (NT).
> +/* 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)
>   (bit_ior:c (negate (convert (gt @0 INTEGER_CST@1)))
> @@ -3250,6 +3250,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    }
>    (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
>
> +/* Unsigned saturation truncate, case 2, sizeof (WT) > sizeof (NT).
> +   SAT_U_TRUNC = (NT)(MIN_EXPR (X, 255)).  */
> +(match (unsigned_integer_sat_trunc @0)
> + (convert (min @0 INTEGER_CST@1))
> + (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 int_cst = wi::to_wide (@1, itype_precision);
> +  }
> +  (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
> +
>  /* x >  y  &&  x != XXX_MIN  -->  x > y
>     x >  y  &&  x == XXX_MIN  -->  false . */
>  (for eqne (eq ne)
> diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
> index a35caf5f058..ac86be8eb94 100644
> --- a/gcc/tree-ssa-math-opts.cc
> +++ b/gcc/tree-ssa-math-opts.cc
> @@ -6170,6 +6170,10 @@ math_opts_dom_walker::after_dom_children (basic_block bb)
>               match_unsigned_saturation_sub (&gsi, as_a<gassign *> (stmt));
>               break;
>
> +           case NOP_EXPR:
> +             match_unsigned_saturation_trunc (&gsi, as_a<gassign *> (stmt));
> +             break;
> +
>             default:;
>             }
>         }
> --
> 2.34.1
>
diff mbox series

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 4edfa2ae2c9..3759c64d461 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3234,7 +3234,7 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
       && types_match (type, @0, @1))))
 
-/* Unsigned saturation truncate, case 1 (), sizeof (WT) > sizeof (NT).
+/* 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)
  (bit_ior:c (negate (convert (gt @0 INTEGER_CST@1)))
@@ -3250,6 +3250,21 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   }
   (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
 
+/* Unsigned saturation truncate, case 2, sizeof (WT) > sizeof (NT).
+   SAT_U_TRUNC = (NT)(MIN_EXPR (X, 255)).  */
+(match (unsigned_integer_sat_trunc @0)
+ (convert (min @0 INTEGER_CST@1))
+ (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 int_cst = wi::to_wide (@1, itype_precision);
+  }
+  (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
+
 /* x >  y  &&  x != XXX_MIN  -->  x > y
    x >  y  &&  x == XXX_MIN  -->  false . */
 (for eqne (eq ne)
diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
index a35caf5f058..ac86be8eb94 100644
--- a/gcc/tree-ssa-math-opts.cc
+++ b/gcc/tree-ssa-math-opts.cc
@@ -6170,6 +6170,10 @@  math_opts_dom_walker::after_dom_children (basic_block bb)
 	      match_unsigned_saturation_sub (&gsi, as_a<gassign *> (stmt));
 	      break;
 
+	    case NOP_EXPR:
+	      match_unsigned_saturation_trunc (&gsi, as_a<gassign *> (stmt));
+	      break;
+
 	    default:;
 	    }
 	}