match: Simplify `ptr0 - (ptr0 - ptr1)` into ptr1 [PR121921]

Message ID 20250914181521.272481-1-andrew.pinski@oss.qualcomm.com
State New
Headers
Series match: Simplify `ptr0 - (ptr0 - ptr1)` into ptr1 [PR121921] |

Commit Message

Andrew Pinski Sept. 14, 2025, 6:15 p.m. UTC
  This pattern shows up with some C++ code (std::vector<bool>) where we get:
```
  _9 = _201 - _36;
  _10 = (long unsigned int) _9;
  _11 = -_10;
  _12 = _201 + _11;
```

In the original code it was `end - (end - begin)` but with inlined functions so it
is not direct fixable.

This patch adds a new amtch pattern like what was added in
r0-128019-g77574c353464b3 (and then moved to match with r5-4705-ga499aac5dfa5d9).

Bootstrapped and tested on x86_64-linux-gnu.

	PR tree-optimization/121921
gcc/ChangeLog:

	* match.pd (`eptr - (eptr - bptr)`): New patterns.

gcc/testsuite/ChangeLog:
	* gcc.dg/pr121921-1.c: New testcase.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
---
 gcc/match.pd                      | 24 ++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/pr121921-1.c | 14 ++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr121921-1.c
  

Comments

Richard Biener Sept. 15, 2025, 8:02 a.m. UTC | #1
On Sun, Sep 14, 2025 at 8:17 PM Andrew Pinski
<andrew.pinski@oss.qualcomm.com> wrote:
>
> This pattern shows up with some C++ code (std::vector<bool>) where we get:
> ```
>   _9 = _201 - _36;
>   _10 = (long unsigned int) _9;
>   _11 = -_10;
>   _12 = _201 + _11;
> ```
>
> In the original code it was `end - (end - begin)` but with inlined functions so it
> is not direct fixable.
>
> This patch adds a new amtch pattern like what was added in
> r0-128019-g77574c353464b3 (and then moved to match with r5-4705-ga499aac5dfa5d9).
>
> Bootstrapped and tested on x86_64-linux-gnu.

I wonder, if in general, -(ptr1 - ptr2) can be changed to (ptr2 -
ptr1).  I think
this should be OK?  Of course with the unsigned conversion in the way
it's (long unsigned int)(ptr2 - ptr2)?

Thanks,
Richard.

>         PR tree-optimization/121921
> gcc/ChangeLog:
>
>         * match.pd (`eptr - (eptr - bptr)`): New patterns.
>
> gcc/testsuite/ChangeLog:
>         * gcc.dg/pr121921-1.c: New testcase.
>
> Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
> ---
>  gcc/match.pd                      | 24 ++++++++++++++++++++++++
>  gcc/testsuite/gcc.dg/pr121921-1.c | 14 ++++++++++++++
>  2 files changed, 38 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/pr121921-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index dbbb7b51275..5dea74aabe2 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3047,6 +3047,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
>     (convert @1)))
>
> +/* Likewise but with an additional negate here
> +   to swap the order of the subtraction. */
> +/* Pattern match
> +     tem1 = (long) ptr1;
> +     tem2 = (long) ptr2;
> +     tem3 = tem1 - tem2;
> +     tem4 = (unsigned long) tem3;
> +     tem5 = -tem3;
> +     tem5 = ptr1 + tem5;
> +   and produce
> +     tem5 = ptr2;  */
> +(simplify
> +  (pointer_plus @0 (negate (convert?@2 (minus@3 (convert @0) (convert @1)))))
> +  /* Conditionally look through a sign-changing conversion.  */
> +  (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
> +       && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
> +           || (GENERIC && type == TREE_TYPE (@1))))
> +   @1))
> +/* ptr0 - (ptr0 - ptr1) -> ptr1 */
> +(simplify
> + (pointer_plus @0 (negate (convert?@2 (pointer_diff@3 @@0 @1))))
> +  (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
> +   (convert @1)))
> +
>  /* Pattern match
>       tem = (sizetype) ptr;
>       tem = tem & algn;
> diff --git a/gcc/testsuite/gcc.dg/pr121921-1.c b/gcc/testsuite/gcc.dg/pr121921-1.c
> new file mode 100644
> index 00000000000..b11afc819da
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr121921-1.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O -fdump-tree-cddce1" } */
> +
> +/* PR tree-optimization/121921 */
> +
> +int *
> +fx (int *b, int *e)
> +{
> +  __SIZE_TYPE__ p = b - e;
> +  /* The first forwprop pass should optimize this to return e;  */
> +  return b - p;
> +}
> +
> +/* { dg-final { scan-tree-dump "return e" "cddce1" } } */
> --
> 2.43.0
>
  
Andrew Pinski Oct. 5, 2025, 3:51 p.m. UTC | #2
On Mon, Sep 15, 2025 at 1:02 AM Richard Biener
<richard.guenther@gmail.com> wrote:
>
> On Sun, Sep 14, 2025 at 8:17 PM Andrew Pinski
> <andrew.pinski@oss.qualcomm.com> wrote:
> >
> > This pattern shows up with some C++ code (std::vector<bool>) where we get:
> > ```
> >   _9 = _201 - _36;
> >   _10 = (long unsigned int) _9;
> >   _11 = -_10;
> >   _12 = _201 + _11;
> > ```
> >
> > In the original code it was `end - (end - begin)` but with inlined functions so it
> > is not direct fixable.
> >
> > This patch adds a new amtch pattern like what was added in
> > r0-128019-g77574c353464b3 (and then moved to match with r5-4705-ga499aac5dfa5d9).
> >
> > Bootstrapped and tested on x86_64-linux-gnu.
>
> I wonder, if in general, -(ptr1 - ptr2) can be changed to (ptr2 -
> ptr1).  I think
> this should be OK?  Of course with the unsigned conversion in the way
> it's (long unsigned int)(ptr2 - ptr2)?

So I see there is already a pattern to do `(negate (pointer_diff @0
@1))` -> `(pointer_diff @1 @0)`. I will be extending it to handle the
nop conversion too.
Will be submitting a new patch with that change soon.

Thanks,
Andrew

>
> Thanks,
> Richard.
>
> >         PR tree-optimization/121921
> > gcc/ChangeLog:
> >
> >         * match.pd (`eptr - (eptr - bptr)`): New patterns.
> >
> > gcc/testsuite/ChangeLog:
> >         * gcc.dg/pr121921-1.c: New testcase.
> >
> > Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
> > ---
> >  gcc/match.pd                      | 24 ++++++++++++++++++++++++
> >  gcc/testsuite/gcc.dg/pr121921-1.c | 14 ++++++++++++++
> >  2 files changed, 38 insertions(+)
> >  create mode 100644 gcc/testsuite/gcc.dg/pr121921-1.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index dbbb7b51275..5dea74aabe2 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -3047,6 +3047,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >    (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
> >     (convert @1)))
> >
> > +/* Likewise but with an additional negate here
> > +   to swap the order of the subtraction. */
> > +/* Pattern match
> > +     tem1 = (long) ptr1;
> > +     tem2 = (long) ptr2;
> > +     tem3 = tem1 - tem2;
> > +     tem4 = (unsigned long) tem3;
> > +     tem5 = -tem3;
> > +     tem5 = ptr1 + tem5;
> > +   and produce
> > +     tem5 = ptr2;  */
> > +(simplify
> > +  (pointer_plus @0 (negate (convert?@2 (minus@3 (convert @0) (convert @1)))))
> > +  /* Conditionally look through a sign-changing conversion.  */
> > +  (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
> > +       && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
> > +           || (GENERIC && type == TREE_TYPE (@1))))
> > +   @1))
> > +/* ptr0 - (ptr0 - ptr1) -> ptr1 */
> > +(simplify
> > + (pointer_plus @0 (negate (convert?@2 (pointer_diff@3 @@0 @1))))
> > +  (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
> > +   (convert @1)))
> > +
> >  /* Pattern match
> >       tem = (sizetype) ptr;
> >       tem = tem & algn;
> > diff --git a/gcc/testsuite/gcc.dg/pr121921-1.c b/gcc/testsuite/gcc.dg/pr121921-1.c
> > new file mode 100644
> > index 00000000000..b11afc819da
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/pr121921-1.c
> > @@ -0,0 +1,14 @@
> > +/* { dg-do compile } */
> > +/* { dg-options "-O -fdump-tree-cddce1" } */
> > +
> > +/* PR tree-optimization/121921 */
> > +
> > +int *
> > +fx (int *b, int *e)
> > +{
> > +  __SIZE_TYPE__ p = b - e;
> > +  /* The first forwprop pass should optimize this to return e;  */
> > +  return b - p;
> > +}
> > +
> > +/* { dg-final { scan-tree-dump "return e" "cddce1" } } */
> > --
> > 2.43.0
> >
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index dbbb7b51275..5dea74aabe2 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3047,6 +3047,30 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
    (convert @1)))
 
+/* Likewise but with an additional negate here
+   to swap the order of the subtraction. */
+/* Pattern match
+     tem1 = (long) ptr1;
+     tem2 = (long) ptr2;
+     tem3 = tem1 - tem2;
+     tem4 = (unsigned long) tem3;
+     tem5 = -tem3;
+     tem5 = ptr1 + tem5;
+   and produce
+     tem5 = ptr2;  */
+(simplify
+  (pointer_plus @0 (negate (convert?@2 (minus@3 (convert @0) (convert @1)))))
+  /* Conditionally look through a sign-changing conversion.  */
+  (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
+       && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
+	    || (GENERIC && type == TREE_TYPE (@1))))
+   @1))
+/* ptr0 - (ptr0 - ptr1) -> ptr1 */
+(simplify
+ (pointer_plus @0 (negate (convert?@2 (pointer_diff@3 @@0 @1))))
+  (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
+   (convert @1)))
+
 /* Pattern match
      tem = (sizetype) ptr;
      tem = tem & algn;
diff --git a/gcc/testsuite/gcc.dg/pr121921-1.c b/gcc/testsuite/gcc.dg/pr121921-1.c
new file mode 100644
index 00000000000..b11afc819da
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr121921-1.c
@@ -0,0 +1,14 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+/* PR tree-optimization/121921 */
+
+int *
+fx (int *b, int *e)
+{
+  __SIZE_TYPE__ p = b - e;
+  /* The first forwprop pass should optimize this to return e;  */
+  return b - p;
+}
+
+/* { dg-final { scan-tree-dump "return e" "cddce1" } } */