public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused
@ 2020-08-11 10:56 gabravier at gmail dot com
  2020-08-11 11:52 ` [Bug tree-optimization/96565] " glisse at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: gabravier at gmail dot com @ 2020-08-11 10:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96565
           Summary: Failure to optimize out VLA even though it is left
                    unused
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

bool g(void);

void f(int x)
{
    char arr[x];

    arr[0] = 0;

    if (g())
        abort();
}

The VLA can be removed. This transformation is done by LLVM, but not by GCC.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
@ 2020-08-11 11:52 ` glisse at gcc dot gnu.org
  2020-08-11 12:39 ` glisse at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-08-11 11:52 UTC (permalink / raw)
  To: gcc-bugs

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

Marc Glisse <glisse at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-08-11

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> ---
We simplify for alloca, for malloc, but not for __builtin_alloca_with_align
(aka VLA).

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
  2020-08-11 11:52 ` [Bug tree-optimization/96565] " glisse at gcc dot gnu.org
@ 2020-08-11 12:39 ` glisse at gcc dot gnu.org
  2020-08-25  8:25 ` rguenth at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-08-11 12:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Marc Glisse <glisse at gcc dot gnu.org> ---
Actually, it isn't so much the alloca call itself, it seems to be
__builtin_stack_save / __builtin_stack_restore that prevent DSE from removing
arr[0] = 0 (without that write, DCE can remove __builtin_alloca_with_align, and
__builtin_stack_* disappear in FAB).

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
  2020-08-11 11:52 ` [Bug tree-optimization/96565] " glisse at gcc dot gnu.org
  2020-08-11 12:39 ` glisse at gcc dot gnu.org
@ 2020-08-25  8:25 ` rguenth at gcc dot gnu.org
  2020-08-25  9:21 ` glisse at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-25  8:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
So we have

  <bb 2> :
  saved_stack.2_7 = __builtin_stack_save ();
  _1 = (long int) x_6(D);
  _2 = _1 + -1;
  _13 = (sizetype) _2;
  _4 = (sizetype) x_6(D);
  _5 = (bitsizetype) _4;
  _14 = _5 * 8;
  arr.1_19 = __builtin_alloca_with_align (_4, 8);
  (*arr.1_19)[0] = 0;
  _12 = g ();
  if (_12 != 0)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  abort ();

  <bb 4> :
  __builtin_stack_restore (saved_stack.2_7);
  return;

I guess the "usual" way of dealing with this would be to have
CLOBBERs for all VLAs before the __builtin_stack_restore.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2020-08-25  8:25 ` rguenth at gcc dot gnu.org
@ 2020-08-25  9:21 ` glisse at gcc dot gnu.org
  2020-08-25 13:16 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-08-25  9:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Marc Glisse <glisse at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #3)
> I guess the "usual" way of dealing with this would be to have
> CLOBBERs for all VLAs before the __builtin_stack_restore.

That looks like a good idea.

I didn't try to follow in a debugger why DSE fails to remove the write when
those 2 builtins are present while it manages if I call
__builtin_alloca_with_align directly, but I don't immediately see a reason for
that difference, even in the absence of clobbers. Or maybe that's just the
usual limitations of DSE (there is a branch after all...).

I first thought that __builtin_stack_save/restore might need some extra
attributes (advertising for instance that they do not read/write memory or let
anything escape, without weakening them to the point where the compiler would
move them around too much or remove them), but since the call to the opaque g
does not seem to prevent DSE from removing the write, that's probably not the
issue.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2020-08-25  9:21 ` glisse at gcc dot gnu.org
@ 2020-08-25 13:16 ` jakub at gcc dot gnu.org
  2020-08-25 15:55 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-08-25 13:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Wouldn't many spots in the middle-end be upset about the gimple clobber having
a variable length type (both the MEM_REF on the lhs and corresponding type on
the CONSTRUCTOR)?
I think tree DCE should have everything it needs for the removals even without
such CLOBBERs, except that it is harder but not impossible to find out which
__builtin_stack_restore frees what.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2020-08-25 13:16 ` jakub at gcc dot gnu.org
@ 2020-08-25 15:55 ` jakub at gcc dot gnu.org
  2020-08-26  6:29 ` rguenther at suse dot de
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-08-25 15:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I'd say that the VLAs are more like "malloc" with the stack restore than
performing one or more "free" conceptually, so perhaps we should instead emit
calls to free-like internal function .FREE_VLA (ptr); right before the
__builtin_stack_restore for the VLAs freed at that point, and handle the
__builtin_alloca_with_align and .FREE_VLA pairs like we treat malloc/free in
tree DSE, and then throw away those ifn calls at some point.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2020-08-25 15:55 ` jakub at gcc dot gnu.org
@ 2020-08-26  6:29 ` rguenther at suse dot de
  2020-08-26  6:41 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenther at suse dot de @ 2020-08-26  6:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from rguenther at suse dot de <rguenther at suse dot de> ---
On Tue, 25 Aug 2020, jakub at gcc dot gnu.org wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96565
> 
> --- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
> Wouldn't many spots in the middle-end be upset about the gimple clobber having
> a variable length type (both the MEM_REF on the lhs and corresponding type on
> the CONSTRUCTOR)?

Well, it of course would need to be wrapped in a WITH_SIZE_EXPR (ick).
Maybe we can clobber the original VAR_DECL ...

> I think tree DCE should have everything it needs for the removals even without
> such CLOBBERs, except that it is harder but not impossible to find out which
> __builtin_stack_restore frees what.

I'm not sure we can do this, that is, treat __builtin_stack_restore as
"free".  What we can possibly do is avoid treating __builtin_stack_restore
as use (but we still need to consider it clobbering things).  That is,
fixing ref_maybe_used_by_stmt_p should be possible but stmt_kills_ref_p
is way harder.

Let me see if I can do ref_maybe_used_by_stmt_p.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2020-08-26  6:29 ` rguenther at suse dot de
@ 2020-08-26  6:41 ` rguenth at gcc dot gnu.org
  2020-08-26 11:36 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-26  6:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
OK, the case is quite simple to fix.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (7 preceding siblings ...)
  2020-08-26  6:41 ` rguenth at gcc dot gnu.org
@ 2020-08-26 11:36 ` rguenth at gcc dot gnu.org
  2020-08-26 11:42 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-26 11:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
OK, well - but the fix exposes (IIRC I ran into this at some time in the past
already) that GIMPLE_RESX does not have virtual operands but it needs to
represent a use of global memory at least in the case it exits the current
function.  So with the fix which is simply

diff --git a/gcc/tree-ssa-dse.c b/gcc/tree-ssa-dse.c
index cc93f559286..6b2f64c0250 100644
--- a/gcc/tree-ssa-dse.c
+++ b/gcc/tree-ssa-dse.c
@@ -888,11 +888,16 @@ dse_classify_store (ao_ref *ref, gimple *stmt,
          gimple *def = defs[i];
          gimple *use_stmt;
          use_operand_p use_p;
+         /* If the path ends here we do not need to process it further.
+            This for example happens with calls to noreturn functions.  */
+         if (gimple_code (def) != GIMPLE_PHI
+             && has_zero_uses (gimple_vdef (def)))
+           defs.unordered_remove (i);
          /* If the path to check starts with a kill we do not need to
             process it further.
             ???  With byte tracking we need only kill the bytes currently
             live.  */
-         if (stmt_kills_ref_p (def, ref))
+         else if (stmt_kills_ref_p (def, ref))
            {
              if (by_clobber_p && !gimple_clobber_p (def))
                *by_clobber_p = false;

I see

FAIL: g++.dg/eh/spec7.C  -std=gnu++98 execution test
FAIL: g++.dg/eh/spec7.C  -std=gnu++14 execution test
FAIL: g++.dg/eh/spec7.C  -std=gnu++17 execution test
FAIL: g++.dg/eh/spec7.C  -std=gnu++2a execution test
FAIL: gcc.target/i386/cleanup-1.c execution test
FAIL: gcc.target/i386/cleanup-2.c execution test

also (a testsuite issue)

FAIL: gcc.dg/builtin-object-size-4.c execution test

the RESX issue is possibly latent even without the above patch.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (8 preceding siblings ...)
  2020-08-26 11:36 ` rguenth at gcc dot gnu.org
@ 2020-08-26 11:42 ` rguenth at gcc dot gnu.org
  2020-08-27  6:07 ` cvs-commit at gcc dot gnu.org
  2020-08-27  6:08 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-26 11:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Richard Biener <rguenth at gcc dot gnu.org> ---
Ah, no - error in my patch.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (9 preceding siblings ...)
  2020-08-26 11:42 ` rguenth at gcc dot gnu.org
@ 2020-08-27  6:07 ` cvs-commit at gcc dot gnu.org
  2020-08-27  6:08 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-08-27  6:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from CVS 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:989bc4ca2f2978baecff00f6d0532994b82897ef

commit r11-2899-g989bc4ca2f2978baecff00f6d0532994b82897ef
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Aug 26 08:44:59 2020 +0200

    tree-optimization/96565 - improve DSE with paths ending in noreturn

    This improves DSEs stmt walking by not considering a DEF without
    uses for further processing (and thus giving up when there's two
    paths to follow).

    2020-08-26  Richard Biener  <rguenther@suse.de>

            PR tree-optimization/96565
            * tree-ssa-dse.c (dse_classify_store): Remove defs with
            no uses from further processing.

            * gcc.dg/tree-ssa/ssa-dse-40.c: New testcase.
            * gcc.dg/builtin-object-size-4.c: Adjust.

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

* [Bug tree-optimization/96565] Failure to optimize out VLA even though it is left unused
  2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
                   ` (10 preceding siblings ...)
  2020-08-27  6:07 ` cvs-commit at gcc dot gnu.org
@ 2020-08-27  6:08 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-27  6:08 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

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

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

end of thread, other threads:[~2020-08-27  6:08 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-11 10:56 [Bug tree-optimization/96565] New: Failure to optimize out VLA even though it is left unused gabravier at gmail dot com
2020-08-11 11:52 ` [Bug tree-optimization/96565] " glisse at gcc dot gnu.org
2020-08-11 12:39 ` glisse at gcc dot gnu.org
2020-08-25  8:25 ` rguenth at gcc dot gnu.org
2020-08-25  9:21 ` glisse at gcc dot gnu.org
2020-08-25 13:16 ` jakub at gcc dot gnu.org
2020-08-25 15:55 ` jakub at gcc dot gnu.org
2020-08-26  6:29 ` rguenther at suse dot de
2020-08-26  6:41 ` rguenth at gcc dot gnu.org
2020-08-26 11:36 ` rguenth at gcc dot gnu.org
2020-08-26 11:42 ` rguenth at gcc dot gnu.org
2020-08-27  6:07 ` cvs-commit at gcc dot gnu.org
2020-08-27  6:08 ` 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).