public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/112706] New: missed simplification in FRE
@ 2023-11-24 19:52 hubicka at gcc dot gnu.org
  2023-11-24 19:58 ` [Bug tree-optimization/112706] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: hubicka at gcc dot gnu.org @ 2023-11-24 19:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

            Bug ID: 112706
           Summary: missed simplification in FRE
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hubicka at gcc dot gnu.org
  Target Milestone: ---

Compiling the following testcase (simplified from repeated
std::vector::push_back expansion):

int *ptr;
void link_error ();
void
test ()
{

        int *ptr1 = ptr + 10;
        int *ptr2 = ptr + 20;
        if (ptr1 == ptr2)
                link_error ();
}

with gcc -O2 t.C -fdump-tree-all-details
one can check that link_error is optimized away really late:

jh@ryzen4:/tmp> grep link_error a-t.C*
....
a-t.C.106t.cunrolli:  link_error ();
a-t.C.107t.backprop:  link_error ();
a-t.C.108t.phiprop:  link_error ();
a-t.C.109t.forwprop2:link_error ();

this is too late for some optimization to catch up (in the case of std::vector
we end up missing DSE since the transform is delayed to forwprop3)

I think this is something value numbering should catch.

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

* [Bug tree-optimization/112706] missed simplification in FRE
  2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
@ 2023-11-24 19:58 ` pinskia at gcc dot gnu.org
  2023-11-24 21:36 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-24 19:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-11-24
          Component|middle-end                  |tree-optimization
           Keywords|                            |missed-optimization
             Blocks|110287                      |
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This should have been even folded during CCP1.

```
/* For equality and subtraction, this is also true with wrapping overflow.  */
(for op (eq ne minus)
 (simplify
  (op (plus:c @0 @2) (plus:c @1 @2))
  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
   (op @0 @1))))
```

Should be extended for PointerPlus or done for it.

That is:
```
(for op  (eq ne)
 (simplify
  (op (pointer_plus @0 @1) (pointer_plus @0 @2))
  (op @1 @2))

```

That should fix it during CCP1 (and fre).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110287
[Bug 110287] _M_check_len is expensive

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

* [Bug tree-optimization/112706] missed simplification in FRE
  2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
  2023-11-24 19:58 ` [Bug tree-optimization/112706] " pinskia at gcc dot gnu.org
@ 2023-11-24 21:36 ` pinskia at gcc dot gnu.org
  2023-11-24 21:52 ` hubicka at ucw dot cz
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-24 21:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> That is:
> ```
> (for op  (eq ne)
>  (simplify
>   (op (pointer_plus @0 @1) (pointer_plus @0 @2))
>   (op @1 @2))
> 
> ```

Note I am missing a extra `)` but then I Noticed in the full testcase (that is
located in https://gcc.gnu.org/pipermail/gcc-patches/2023-November/638130.html)
which was originally reported we still end up with:
```
  _76 = _71 + 4;
  # .MEM_154 = VDEF <.MEM_153>
  x_3(D)->D.25942._M_implD.25172.D.25249._M_finishD.25175 = _76;
  # .MEM_7 = VDEF <.MEM_154>
  D.26033 = 0;
  # .MEM_157 = VDEF <.MEM_7>
  *_76 = 0;
  # PT = nonlocal escaped 
  _82 = _71 + 8;
  # .MEM_158 = VDEF <.MEM_157>
  x_3(D)->D.25942._M_implD.25172.D.25249._M_finishD.25175 = _82;
  # .MEM_8 = VDEF <.MEM_158>
  D.26033 ={v} {CLOBBER(eol)};
  # .MEM_9 = VDEF <.MEM_8>
  D.26034 = 0;
  if (_66 != _82)
```
After pre (note the first comparison is gone but not the second one and maybe a
3rd). So this patch helps but it looks like a PRE/VN improvement is still
needed to fix the others.

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

* [Bug tree-optimization/112706] missed simplification in FRE
  2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
  2023-11-24 19:58 ` [Bug tree-optimization/112706] " pinskia at gcc dot gnu.org
  2023-11-24 21:36 ` pinskia at gcc dot gnu.org
@ 2023-11-24 21:52 ` hubicka at ucw dot cz
  2023-11-27  8:03 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: hubicka at ucw dot cz @ 2023-11-24 21:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

--- Comment #3 from Jan Hubicka <hubicka at ucw dot cz> ---
Thanks, new pattern looks like noticeable improvement :)
Base+offset is effective for alias analysis and I suppose it happens
reasonably enough for compares as well.
>   _76 = _71 + 4;
>   # .MEM_154 = VDEF <.MEM_153>
>   x_3(D)->D.25942._M_implD.25172.D.25249._M_finishD.25175 = _76;
>   # .MEM_7 = VDEF <.MEM_154>
>   D.26033 = 0;
>   # .MEM_157 = VDEF <.MEM_7>
>   *_76 = 0;
>   # PT = nonlocal escaped 
>   _82 = _71 + 8;
>   # .MEM_158 = VDEF <.MEM_157>
>   x_3(D)->D.25942._M_implD.25172.D.25249._M_finishD.25175 = _82;
>   # .MEM_8 = VDEF <.MEM_158>
>   D.26033 ={v} {CLOBBER(eol)};
>   # .MEM_9 = VDEF <.MEM_8>
>   D.26034 = 0;
>   if (_66 != _82)
> ```
> After pre (note the first comparison is gone but not the second one and maybe a
> 3rd). So this patch helps but it looks like a PRE/VN improvement is still
> needed to fix the others.
I think it is missing predication in VN. At each execution of CCP or VN
we work out one conditional to be true, but we stil account both paths
for the value number of the pointer used in next compare.

If vector used base+size pair instead of base+endptr VRP would help
here, but we can't vrp finish-start range...

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

* [Bug tree-optimization/112706] missed simplification in FRE
  2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-11-24 21:52 ` hubicka at ucw dot cz
@ 2023-11-27  8:03 ` rguenth at gcc dot gnu.org
  2023-11-27  8:43 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-27  8:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
            Version|unknown                     |14.0
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
I will have a look.

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

* [Bug tree-optimization/112706] missed simplification in FRE
  2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-11-27  8:03 ` rguenth at gcc dot gnu.org
@ 2023-11-27  8:43 ` rguenth at gcc dot gnu.org
  2023-11-27  9:43 ` cvs-commit at gcc dot gnu.org
  2023-11-27  9:44 ` rguenth at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-27  8:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
As Andrew says (forwprop builds trees and feeds it fold-const.cc which
eventually gets it).

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

* [Bug tree-optimization/112706] missed simplification in FRE
  2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-11-27  8:43 ` rguenth at gcc dot gnu.org
@ 2023-11-27  9:43 ` cvs-commit at gcc dot gnu.org
  2023-11-27  9:44 ` rguenth at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-11-27  9:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:d9abaa8d58f5729925b1db735d4723a9ea825eaa

commit r14-5872-gd9abaa8d58f5729925b1db735d4723a9ea825eaa
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Nov 27 09:45:04 2023 +0100

    tree-optimization/112706 - missed simplification of condition

    We lack a match.pd pattern recognizing ptr + o ==/!= ptr + o'.
    The following extends handling we have for integral types to
    pointers.

            PR tree-optimization/112706
            * match.pd (ptr + o ==/!=/- ptr + o'): New patterns.

            * gcc.dg/tree-ssa/pr112706.c: New testcase.

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

* [Bug tree-optimization/112706] missed simplification in FRE
  2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-11-27  9:43 ` cvs-commit at gcc dot gnu.org
@ 2023-11-27  9:44 ` rguenth at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-27  9:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112706

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |14.0

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2023-11-27  9:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-24 19:52 [Bug middle-end/112706] New: missed simplification in FRE hubicka at gcc dot gnu.org
2023-11-24 19:58 ` [Bug tree-optimization/112706] " pinskia at gcc dot gnu.org
2023-11-24 21:36 ` pinskia at gcc dot gnu.org
2023-11-24 21:52 ` hubicka at ucw dot cz
2023-11-27  8:03 ` rguenth at gcc dot gnu.org
2023-11-27  8:43 ` rguenth at gcc dot gnu.org
2023-11-27  9:43 ` cvs-commit at gcc dot gnu.org
2023-11-27  9:44 ` rguenth at gcc dot gnu.org

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).