public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison
@ 2014-09-05  8:32 rguenth at gcc dot gnu.org
  2014-09-05  8:34 ` [Bug middle-end/63184] " rguenth at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-09-05  8:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 63184
           Summary: [4.8/4.9/5 Regression] Fails to simplify comparison
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rguenth at gcc dot gnu.org

c-c++-common/pr19807-2.c

/* { dg-do link } */

extern void link_error(void);
int i;
int main()
{
  int a[4];
  if ((char*)&a[1] + 4*i + 4 != (char*)&a[i+2])
    link_error();
  return 0;
}

Fails with all optimization levels for all compilers.

c-c++-common/pr19807-3.c

/* { dg-do link } */

extern void link_error(void);
int i;
int main()
{
  int a[4];
  if (&a[1] + i + 1 != &a[i+2])
    link_error();
  return 0;
}

gcc 4.7 passes with -O[01] but fails with -O2+
gcc 4.8 and 4.9 fail with -O0 but passes with -O1+ (thanks to SLSR)
gcc 5.0 since the fix for PR63148 fails

which is a regression.

We don't seem to systematically use sth like the tree-affine.c framework
to simplify address comparisons and the fact that tree-reassoc.c doesn't
in any way associate pointer arithmetic doesn't help either.


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

* [Bug middle-end/63184] [4.8/4.9/5 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
@ 2014-09-05  8:34 ` rguenth at gcc dot gnu.org
  2014-10-17 11:29 ` rguenth at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-09-05  8:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2014-09-05
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
   Target Milestone|---                         |5.0
     Ever confirmed|0                           |1
           Severity|normal                      |minor

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note that due to the inconsistency with -fstrict-overflow I don't consider this
an important optimization regression.

But I'll see if I can improve some generic code to handle these better (with
optimization).  But for 5.0 only.


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

* [Bug middle-end/63184] [4.8/4.9/5 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
  2014-09-05  8:34 ` [Bug middle-end/63184] " rguenth at gcc dot gnu.org
@ 2014-10-17 11:29 ` rguenth at gcc dot gnu.org
  2014-11-24 13:17 ` rguenth at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-10-17 11:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Ok, so we can (and do) forward addresses like &a[_9] into plain dereferences,
so lowering these addresses early is probably not a good idea.  We could
lower them as a very first thing in GIMPLE reassoc but that would still need
to apply transforms like (i + 1) * 4 -> i*4 + 4 to be most effective
(though that generally applies).

Otherwise I can't see an easy place to hook in simplification of

  i.0_3 = i;
  _4 = i.0_3 * 4;
  _5 = (sizetype) _4;
  _6 = _5 + 4;
  _7 = &a[1] + _6;
  _9 = i.0_3 + 2;
  _10 = &a[_9];
  if (_7 != _10)

which is what we get for the first testcase after initial scalar cleanups.
SLSR helps somewhat in some cases but it's run very late.

We could detect address comparisons with the same base and use the
affine combination machinery to simplify them though.  From somewhere.
tree-ssa-forwprop.c probably.  Simplify them to _7 - _10 CMP 0 (if
the addresses have the same base object at least that should cancel).
We canonicalize (base p+ off1) p+ off2 to base p+ (off1 + off2) which
should help here as well.

We could also only "fold" away the base object from the chain (not using
the affine machinery).  match-and-simplify doesn't help here because
the replacement involves an expression created by get_inner_reference
(well, a manual transform would work here, of course).


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

* [Bug middle-end/63184] [4.8/4.9/5 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
  2014-09-05  8:34 ` [Bug middle-end/63184] " rguenth at gcc dot gnu.org
  2014-10-17 11:29 ` rguenth at gcc dot gnu.org
@ 2014-11-24 13:17 ` rguenth at gcc dot gnu.org
  2015-04-22 12:01 ` [Bug middle-end/63184] [4.8/4.9/5/6 " jakub at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-11-24 13:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2


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

* [Bug middle-end/63184] [4.8/4.9/5/6 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2014-11-24 13:17 ` rguenth at gcc dot gnu.org
@ 2015-04-22 12:01 ` jakub at gcc dot gnu.org
  2015-07-16  9:13 ` [Bug middle-end/63184] [4.9/5/6 " rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2015-04-22 12:01 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|5.0                         |5.2

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 5.1 has been released.


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

* [Bug middle-end/63184] [4.9/5/6 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2015-04-22 12:01 ` [Bug middle-end/63184] [4.8/4.9/5/6 " jakub at gcc dot gnu.org
@ 2015-07-16  9:13 ` rguenth at gcc dot gnu.org
  2021-05-14  9:47 ` [Bug middle-end/63184] [9/10/11/12 " jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-07-16  9:13 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|5.2                         |5.3

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 5.2 is being released, adjusting target milestone to 5.3.


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

* [Bug middle-end/63184] [9/10/11/12 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2015-07-16  9:13 ` [Bug middle-end/63184] [4.9/5/6 " rguenth at gcc dot gnu.org
@ 2021-05-14  9:47 ` jakub at gcc dot gnu.org
  2021-06-01  8:06 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-05-14  9:47 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|8.5                         |9.4

--- Comment #24 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 8 branch is being closed.

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

* [Bug middle-end/63184] [9/10/11/12 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2021-05-14  9:47 ` [Bug middle-end/63184] [9/10/11/12 " jakub at gcc dot gnu.org
@ 2021-06-01  8:06 ` rguenth at gcc dot gnu.org
  2021-09-05 23:59 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-06-01  8:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|9.4                         |9.5

--- Comment #25 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 9.4 is being released, retargeting bugs to GCC 9.5.

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

* [Bug middle-end/63184] [9/10/11/12 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2021-06-01  8:06 ` rguenth at gcc dot gnu.org
@ 2021-09-05 23:59 ` pinskia at gcc dot gnu.org
  2021-09-06  1:10 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-05 23:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|rguenth at gcc dot gnu.org         |pinskia at gcc dot gnu.org

--- Comment #26 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So what is interesting is for aarch64, GCC is able to figure out the two values
are equal but only after register allocation (note LLVM has a similar issue
too).

Anyways at forwprop4 we have:
  _5 = &a[1] + _4;
  _13 = _3 + 8;
  _7 = &a + _13;
  if (_5 != _7)

I think I have a patch (I need to format it correctly though):
(for neeq (ne eq)
 (simplify
  (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
   (with { poly_int64 diff; }
    (if (ptr_difference_const (@0, @2, &diff))
     (neeq (plus { build_int_cst_type (type, diff); }
            (convert (minus @1 @3)))
      {build_zero_cst (type); })))))


I also added this one too as I saw that was not being folded either:
(simplify
 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
 (with { poly_int64 diff; }
   (if (ptr_difference_const (@0, @2, &diff))
    (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))

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

* [Bug middle-end/63184] [9/10/11/12 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2021-09-05 23:59 ` pinskia at gcc dot gnu.org
@ 2021-09-06  1:10 ` pinskia at gcc dot gnu.org
  2021-09-06  5:31 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-06  1:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #27 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 51411
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51411&action=edit
patch which I am testing

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

* [Bug middle-end/63184] [9/10/11/12 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2021-09-06  1:10 ` pinskia at gcc dot gnu.org
@ 2021-09-06  5:31 ` pinskia at gcc dot gnu.org
  2021-09-06  7:47 ` cvs-commit at gcc dot gnu.org
  2021-09-06  7:48 ` pinskia at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-06  5:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2021-Septemb
                   |                            |er/578859.html

--- Comment #28 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch posted:
https://gcc.gnu.org/pipermail/gcc-patches/2021-September/578859.html

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

* [Bug middle-end/63184] [9/10/11/12 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2021-09-06  5:31 ` pinskia at gcc dot gnu.org
@ 2021-09-06  7:47 ` cvs-commit at gcc dot gnu.org
  2021-09-06  7:48 ` pinskia at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-09-06  7:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #29 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:564efbf40077c25623cdd6ce2f911c56b5b08f6c

commit r12-3364-g564efbf40077c25623cdd6ce2f911c56b5b08f6c
Author: Andrew Pinski <apinski@marvell.com>
Date:   Mon Sep 6 00:52:18 2021 +0000

    Fix PR tree-optimization/63184: add simplification of (& + A) != (& + B)

    These two testcases have been failing since GCC 5 but things
    have improved such that adding a simplification to match.pd
    for this case is easier than before.
    In the end we have the following IR:
    ....
      _5 = &a[1] + _4;
      _7 = &a + _13;
      if (_5 != _7)

    So we can fold the _5 != _7 into:
    (&a[1] - &a) + _4 != _13

    The subtraction is folded into constant by ptr_difference_const.
    In this case, the full expression gets folded into a constant
    and we are able to remove the if statement.

    OK? Bootstrapped and tested on aarch64-linux-gnu with no regressions.

    gcc/ChangeLog:

            PR tree-optimization/63184
            * match.pd: Add simplification of pointer_diff of two pointer_plus
            with addr_expr in the first operand of each pointer_plus.
            Add simplificatoin of ne/eq of two pointer_plus with addr_expr
            in the first operand of each pointer_plus.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/63184
            * c-c++-common/pr19807-2.c: Enable for all targets and remove the
xfail.
            * c-c++-common/pr19807-3.c: Likewise.

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

* [Bug middle-end/63184] [9/10/11/12 Regression] Fails to simplify comparison
  2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
                   ` (10 preceding siblings ...)
  2021-09-06  7:47 ` cvs-commit at gcc dot gnu.org
@ 2021-09-06  7:48 ` pinskia at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-06  7:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #30 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed on the trunk and I don't think this should be backported as it has been a
missed optimization for 7 years or more.

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

end of thread, other threads:[~2021-09-06  7:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-05  8:32 [Bug middle-end/63184] New: [4.8/4.9/5 Regression] Fails to simplify comparison rguenth at gcc dot gnu.org
2014-09-05  8:34 ` [Bug middle-end/63184] " rguenth at gcc dot gnu.org
2014-10-17 11:29 ` rguenth at gcc dot gnu.org
2014-11-24 13:17 ` rguenth at gcc dot gnu.org
2015-04-22 12:01 ` [Bug middle-end/63184] [4.8/4.9/5/6 " jakub at gcc dot gnu.org
2015-07-16  9:13 ` [Bug middle-end/63184] [4.9/5/6 " rguenth at gcc dot gnu.org
2021-05-14  9:47 ` [Bug middle-end/63184] [9/10/11/12 " jakub at gcc dot gnu.org
2021-06-01  8:06 ` rguenth at gcc dot gnu.org
2021-09-05 23:59 ` pinskia at gcc dot gnu.org
2021-09-06  1:10 ` pinskia at gcc dot gnu.org
2021-09-06  5:31 ` pinskia at gcc dot gnu.org
2021-09-06  7:47 ` cvs-commit at gcc dot gnu.org
2021-09-06  7:48 ` pinskia 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).