On Mon, Jun 1, 2020 at 10:37 AM Feng Xue OS via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This patch is meant to add match rules to simplify patterns as:
>
> o. (pointer + offset_a) - (pointer + offset_b)   ->   (ptrdiff_t) (offset_a - 
> offset_b)
> o. (pointer_a + offset) - (pointer_b + offset)   ->   (pointer_a - pointer_b)

You are also changing the existing pattern which IIRC tries to
preserve the undefinedness of overflow in offset_a - offset_b.
Without an explanation it's hard to guess why you think eliding
this conversion is correct.

Adding the TYPE_OVERFLOW_UNDEFINED guard also looks
odd - AFAICS overflow of the pointer type does not matter
but overflow of the generated minus?  Thus at least
a || !TYPE_OVERFLOW_UNDEFINED (type) would be
in order?

Thanks,
Richard.

> Bootstrapped/regtested on x86_64-linux and aarch64-linux.
>
> Feng
> ---
> 2020-06-01  Feng Xue  <f...@os.amperecomputing.com>
>
> gcc/
>         PR tree-optimization/94234
>         * match.pd ((PTR + A) - (PTR + B)) -> (ptrdiff_t)(A - B): New
>         simplification.
>         * ((PTR_A + O) - (PTR_B + O)) -> (PTR_A - PTR_B): New simplification.
>
> gcc/testsuite/
>         PR tree-optimization/94234
>         * gcc.dg/pr94234.c: New test.
> ---
>  gcc/match.pd                   | 19 +++++++++----------
>  gcc/testsuite/gcc.dg/pr94234.c | 24 ++++++++++++++++++++++++
>  2 files changed, 33 insertions(+), 10 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/pr94234.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 33ee1a920bf..6553be4822e 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2515,16 +2515,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>              && TREE_CODE (@2) == INTEGER_CST
>              && tree_int_cst_sign_bit (@2) == 0))
>       (minus (convert @1) (convert @2)))))
> -   (simplify
> -    (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
> -    /* The second argument of pointer_plus must be interpreted as signed, and
> -       thus sign-extended if necessary.  */
> -    (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
> -     /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
> -       second arg is unsigned even when we need to consider it as signed,
> -       we don't want to diagnose overflow here.  */
> -     (minus (convert (view_convert:stype @1))
> -           (convert (view_convert:stype @2)))))))
> +  (simplify
> +   (pointer_diff (pointer_plus@3 @0 @1) (pointer_plus @0 @2))
> +    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)))
> +      (convert (minus @1 @2))))
> +  (simplify
> +   (pointer_diff (pointer_plus@3 @0 @2) (pointer_plus @1 @2))
> +    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
> +        && !integer_zerop (@2))
> +     (pointer_diff @0 @1)))))
>
>  /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
>      Modeled after fold_plusminus_mult_expr.  */
> diff --git a/gcc/testsuite/gcc.dg/pr94234.c b/gcc/testsuite/gcc.dg/pr94234.c
> new file mode 100644
> index 00000000000..ef9076c80da
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr94234.c
> @@ -0,0 +1,24 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-ccp1" } */
> +
> +typedef __SIZE_TYPE__ size_t;
> +typedef __PTRDIFF_TYPE__ ptrdiff_t;
> +
> +ptrdiff_t foo (char *a, size_t n)
> +{
> +  char *b1 = a + 8 * n;
> +  char *b2 = a + 8 * (n - 1);
> +
> +  return b1 - b2;
> +}
> +
> +ptrdiff_t goo (char *a, size_t n, size_t m)
> +{
> +  char *b1 = a + 8 * n;
> +  char *b2 = a + 8 * (n + 1);
> +
> +  return (b1 + m) - (b2 + m);
> +}
> +
> +/* { dg-final { scan-tree-dump-times "return 8;" 1 "ccp1" } } */
> +/* { dg-final { scan-tree-dump-times "return -8;" 1 "ccp1" } } */

Reply via email to