public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/102951] New: failure to optimize MIN_EXPR of subobject addresses of the same object
@ 2021-10-26 15:24 msebor at gcc dot gnu.org
  2021-10-26 16:49 ` [Bug tree-optimization/102951] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-10-26 15:24 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102951
           Summary: failure to optimize MIN_EXPR of subobject addresses of
                    the same object
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

GCC fails to fold the MIN_EXPR below to the lower address.  The test case
affects C because the C front end emits a MIN_EXPR but not C++ because the C++
front end emits an if statement.

$ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c
extern int a[];

void f (void)
{
  int *p1 = &a[1];
  int *p2 = &a[2];

  int *d = p1 < p2 ? p1 : p2;
  *d = 0;
}

;; Function f (f, funcdef_no=0, decl_uid=1979, cgraph_uid=1, symbol_order=0)

void f ()
{
  int * d;

  <bb 2> [local count: 1073741824]:
  d_1 = MIN_EXPR <&a[2], &a[1]>;   // the two statements can be folded into
  *d_1 = 0;                        //   MEM[(int *)&a + 4B] = 0
  return;

}

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

* [Bug tree-optimization/102951] failure to optimize MIN_EXPR of subobject addresses of the same object
  2021-10-26 15:24 [Bug tree-optimization/102951] New: failure to optimize MIN_EXPR of subobject addresses of the same object msebor at gcc dot gnu.org
@ 2021-10-26 16:49 ` jakub at gcc dot gnu.org
  2021-10-27  2:43 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-26 16:49 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
extern int a[];

int *
foo (void)
{
  int *p1 = &a[1];
  int *p2 = &a[2];
  return p1 < p2 ? p1 : p2;
}

int
bar (void)
{
  int *p1 = &a[1];
  int *p2 = &a[2];
  return p1 < p2;
}

For the latter function, we optimize it in match.pd:
/* When the addresses are not directly of decls compare base and offset.
   This implements some remaining parts of fold_comparison address
   comparisons but still no complete part of it.  Still it is good
   enough to make fold_stmt not regress when not dispatching to fold_binary. 
*/
(for cmp (simple_comparison)
 (simplify
  (cmp (convert1?@2 addr@0) (convert2? addr@1))
  (with
   {
     poly_int64 off0, off1;
...

So, I guess for MIN_EXPR/MAX_EXPR with ADDR_EXPR operands, we can optimize it
similarly, the question is if we should try to do that through
repeating that huge code from there, or try to outline big parts of that into a
helper function, or perhaps could we e.g. do
  (with
    {
#if GENERIC
      tree l = generic_simplify (..., LT_EXPR, ...);
#else
      tree l = gimple_simplify (..., LT_EXPR, ...);
#endif
    }
    (if (l && integer_zerop (l))
      @0)
     (if (l && integer_nonzerop (l))
       @1)))
or so?
and therefore try to fold LT_EXPR instead of MIN_EXPR or MAX_EXPR and if that
folds into integer_zerop or integer_nonzerop

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

* [Bug tree-optimization/102951] failure to optimize MIN_EXPR of subobject addresses of the same object
  2021-10-26 15:24 [Bug tree-optimization/102951] New: failure to optimize MIN_EXPR of subobject addresses of the same object msebor at gcc dot gnu.org
  2021-10-26 16:49 ` [Bug tree-optimization/102951] " jakub at gcc dot gnu.org
@ 2021-10-27  2:43 ` pinskia at gcc dot gnu.org
  2021-10-27  6:56 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-27  2:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-10-27
                 CC|                            |pinskia at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, this is funny because clang does not do it either.

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

* [Bug tree-optimization/102951] failure to optimize MIN_EXPR of subobject addresses of the same object
  2021-10-26 15:24 [Bug tree-optimization/102951] New: failure to optimize MIN_EXPR of subobject addresses of the same object msebor at gcc dot gnu.org
  2021-10-26 16:49 ` [Bug tree-optimization/102951] " jakub at gcc dot gnu.org
  2021-10-27  2:43 ` pinskia at gcc dot gnu.org
@ 2021-10-27  6:56 ` rguenth at gcc dot gnu.org
  2021-10-27 10:37 ` jakub at gcc dot gnu.org
  2021-10-28 18:11 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-10-27  6:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
Factoring this somehow would be nicer, but not sure if the result will be
"nice" ...

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

* [Bug tree-optimization/102951] failure to optimize MIN_EXPR of subobject addresses of the same object
  2021-10-26 15:24 [Bug tree-optimization/102951] New: failure to optimize MIN_EXPR of subobject addresses of the same object msebor at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-10-27  6:56 ` rguenth at gcc dot gnu.org
@ 2021-10-27 10:37 ` jakub at gcc dot gnu.org
  2021-10-28 18:11 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-27 10:37 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 51674
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51674&action=edit
gcc12-pr102951.patch

Untested fix.

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

* [Bug tree-optimization/102951] failure to optimize MIN_EXPR of subobject addresses of the same object
  2021-10-26 15:24 [Bug tree-optimization/102951] New: failure to optimize MIN_EXPR of subobject addresses of the same object msebor at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-10-27 10:37 ` jakub at gcc dot gnu.org
@ 2021-10-28 18:11 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-10-28 18:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:6123b998b185572abac7d7224b34f03955bb91a2

commit r12-4776-g6123b998b185572abac7d7224b34f03955bb91a2
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Oct 28 20:10:15 2021 +0200

    match.pd: Optimize MIN_EXPR <addr1, addr2> etc. addr1 < addr2 would be
simplified [PR102951]

    This patch outlines the decision whether address comparison can be folded
    or not from the match.pd simple comparison simplification and uses it
    both there and in a new minmax simplification, such that we fold e.g.
    MAX (&a[2], &a[1]) etc.
    Some of the Wstringop-overflow-62.c changes might look weird, but that
    seems to be mainly due to gimple_fold_builtin_memset not bothering to
    copy over location, will fix that incrementally.

    2021-10-28  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/102951
            * fold-const.h (address_compare): Declare.
            * fold-const.c (address_compare): New function.
            * match.pd (cmp (convert1?@2 addr@0) (convert2? addr@1)): Use
            address_compare helper.
            (minmax cmp (convert1?@2 addr@0) (convert2?@3 addr@1)): New
            simplification.

            * gcc.dg/tree-ssa/pr102951.c: New test.
            * gcc.dg/Wstringop-overflow-62.c: Adjust expected diagnostics.

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

end of thread, other threads:[~2021-10-28 18:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-26 15:24 [Bug tree-optimization/102951] New: failure to optimize MIN_EXPR of subobject addresses of the same object msebor at gcc dot gnu.org
2021-10-26 16:49 ` [Bug tree-optimization/102951] " jakub at gcc dot gnu.org
2021-10-27  2:43 ` pinskia at gcc dot gnu.org
2021-10-27  6:56 ` rguenth at gcc dot gnu.org
2021-10-27 10:37 ` jakub at gcc dot gnu.org
2021-10-28 18:11 ` cvs-commit 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).