On Thu, Aug 31, 2023 at 7:25 PM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This patch adds the following match patterns to optimize these:
>  /* (a != b) ? (a - b) : 0 -> (a - b) */
>  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
>  /* (a != b) ? (a & b) : a -> (a & b) */
>  /* (a != b) ? (a | b) : a -> (a | b) */
>  /* (a != b) ? min(a,b) : a -> min(a,b) */
>  /* (a != b) ? max(a,b) : a -> max(a,b) */
>  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
>  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
>  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
> Note currently only integer types (include vector types)
> are handled. Floating point types can be added later on.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu.

OK.

> The first pattern had still shows up in GCC in cse.c's preferable
> function which was the original motivation for this patch.
>
>         PR tree-optimization/19832
>
> gcc/ChangeLog:
>
>         * match.pd: Add pattern to optimize
>         `(a != b) ? a OP b : c`.
>
> gcc/testsuite/ChangeLog:
>
>         * g++.dg/opt/vectcond-1.C: New test.
>         * gcc.dg/tree-ssa/phi-opt-same-1.c: New test.
> ---
>  gcc/match.pd                                  | 31 ++++++++++
>  gcc/testsuite/g++.dg/opt/vectcond-1.C         | 57 ++++++++++++++++++
>  .../gcc.dg/tree-ssa/phi-opt-same-1.c          | 60 +++++++++++++++++++
>  3 files changed, 148 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/opt/vectcond-1.C
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index c01362ee359..487a7e38719 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5261,6 +5261,37 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>         (convert @c0))))))))
>  #endif
>
> +(for cnd (cond vec_cond)
> + /* (a != b) ? (a - b) : 0 -> (a - b) */
> + (simplify
> +  (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
> +  @2)
> + /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
> + (simplify
> +  (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
> +  @2)
> + /* (a != b) ? (a & b) : a -> (a & b) */
> + /* (a != b) ? (a | b) : a -> (a | b) */
> + /* (a != b) ? min(a,b) : a -> min(a,b) */
> + /* (a != b) ? max(a,b) : a -> max(a,b) */
> + (for op (bit_and bit_ior min max)
> +  (simplify
> +   (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
> +   @2))
> + /* (a != b) ? (a * b) : (a * a) -> (a * b) */
> + /* (a != b) ? (a + b) : (a + a) -> (a + b) */
> + (for op (mult plus)
> +  (simplify
> +   (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
> +   (if (ANY_INTEGRAL_TYPE_P (type))
> +    @2)))
> + /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
> + (simplify
> +  (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
> +  (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
> +   @2))
> +)
> +
>  /* These was part of minmax phiopt.  */
>  /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
>     to minmax<min/max<a, b>, c> */
> diff --git a/gcc/testsuite/g++.dg/opt/vectcond-1.C 
> b/gcc/testsuite/g++.dg/opt/vectcond-1.C
> new file mode 100644
> index 00000000000..3877ad11414
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/opt/vectcond-1.C
> @@ -0,0 +1,57 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-ccp1 -fdump-tree-optimized" } */
> +/* This is the vector version of these optimizations. */
> +/* PR tree-optimization/19832 */
> +
> +#define vector __attribute__((vector_size(sizeof(unsigned)*2)))
> +
> +static inline vector int max_(vector int a, vector int b)
> +{
> +   return (a > b)? a : b;
> +}
> +static inline vector int min_(vector int a, vector int b)
> +{
> +  return (a < b) ? a : b;
> +}
> +
> +vector int f_minus(vector int a, vector int b)
> +{
> +  return (a != b) ? a - b : (a - a);
> +}
> +vector int f_xor(vector int a, vector int b)
> +{
> +  return (a != b) ? a ^ b : (a ^ a);
> +}
> +
> +vector int f_ior(vector int a, vector int b)
> +{
> +  return (a != b) ? a | b : (a | a);
> +}
> +vector int f_and(vector int a, vector int b)
> +{
> +  return (a != b) ? a & b : (a & a);
> +}
> +vector int f_max(vector int a, vector int b)
> +{
> +  return (a != b) ? max_(a, b) : max_(a, a);
> +}
> +vector int f_min(vector int a, vector int b)
> +{
> +  return (a != b) ? min_(a, b) : min_(a, a);
> +}
> +vector int f_mult(vector int a, vector int b)
> +{
> +  return (a != b) ? a * b : (a * a);
> +}
> +vector int f_plus(vector int a, vector int b)
> +{
> +  return (a != b) ? a + b : (a + a);
> +}
> +vector int f_plus_alt(vector int a, vector int b)
> +{
> +  return (a != b) ? a + b : (a * 2);
> +}
> +
> +/* All of the above function's VEC_COND_EXPR should have been optimized 
> away. */
> +/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "ccp1" } } */
> +/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
> new file mode 100644
> index 00000000000..24e757b9b9f
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
> @@ -0,0 +1,60 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-phiopt1 -fdump-tree-optimized" } */
> +/* PR tree-optimization/19832 */
> +
> +static inline int max_(int a, int b)
> +{
> +  if (a > b) return a;
> +  return b;
> +}
> +static inline int min_(int a, int b)
> +{
> +  if (a < b) return a;
> +  return b;
> +}
> +
> +int f_minus(int a, int b)
> +{
> +  if (a != b) return a - b;
> +  return a - a;
> +}
> +int f_xor(int a, int b)
> +{
> +  if (a != b) return a ^ b;
> +  return a ^ a;
> +}
> +
> +int f_ior(int a, int b)
> +{
> +  if (a != b) return a | b;
> +  return a | a;
> +}
> +int f_and(int a, int b)
> +{
> +  if (a != b) return a & b;
> +  return a & a;
> +}
> +int f_max(int a, int b)
> +{
> +  if (a != b) return max_(a,b);
> +  return max_(a,a);
> +}
> +int f_min(int a, int b)
> +{
> +  if (a != b) return min_(a,b);
> +  return min_(a,a);
> +}
> +int f_mult(int a, int b)
> +{
> +  if (a != b) return a * b;
> +  return a * a;
> +}
> +int f_plus(int a, int b)
> +{
> +  if (a != b) return a + b;
> +  return a + a;
> +}
> +
> +/* All of the above function's if should have been optimized away even in 
> phiopt1. */
> +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if " "optimized" } } */
> --
> 2.31.1
>

Reply via email to