Bug 9059 - unused arrays not optimized away
Summary: unused arrays not optimized away
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 3.4.0
: P3 enhancement
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: 14840
  Show dependency treegraph
 
Reported: 2002-12-26 06:56 UTC by Stefaan De Roeck
Modified: 2004-07-22 06:33 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-03-03 06:14:33


Attachments
test3.ii (132 bytes, text/plain)
2003-05-21 15:17 UTC, Stefaan De Roeck
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Stefaan De Roeck 2002-12-26 06:56:00 UTC
attached file says it all, i suppose

stack-variables which are never consumed, should not appear in the assembler output anymore.  they are indeed removed when the variable is no array (or an array of size 1), but they're in case of this source file, even though they go out of scope without ever being used.  
please note that removing the external function call stops this from being triggered.

Reading specs from /tmp/inst/lib/gcc-lib/i686-pc-linux-gnu/3.4/specs
Configured with:
Thread model: posix
gcc version 3.4 20021225 (experimental)
 /tmp/inst/lib/gcc-lib/i686-pc-linux-gnu/3.4/cc1plus -E -D__GNUG__=3 -quiet -v -D__GNUC__=3 -D__GNUC_MINOR__=4 -D__GNUC_PATCHLEVEL__=0 -D_GNU_SOURCE test3.cpp -Wall -W -O3 test3.ii
ignoring nonexistent directory "/tmp/inst/i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /tmp/inst/include/c++/3.4
 /tmp/inst/include/c++/3.4/i686-pc-linux-gnu
 /tmp/inst/include/c++/3.4/backward
 /usr/local/include
 /tmp/inst/include
 /tmp/inst/lib/gcc-lib/i686-pc-linux-gnu/3.4/include
 /usr/include
End of search list.
 /tmp/inst/lib/gcc-lib/i686-pc-linux-gnu/3.4/cc1plus -fpreprocessed test3.ii -quiet -dumpbase test3.cpp -auxbase-strip test3.s -O3 -Wall -W -version -o test3.s
GNU C++ version 3.4 20021225 (experimental) (i686-pc-linux-gnu)
        compiled by GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7).

Release:
gcc (GCC) 3.4 20021225 (experimental)

Environment:
intel piii, redhat 8.0

How-To-Repeat:
look at the assembler output of the attached source with -O3
Comment 1 Eric Botcazou 2003-02-18 10:33:34 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed on all versions I tested.
Comment 2 Andrew Pinski 2003-06-03 19:07:15 UTC
Simpler testcase:

f()
{
  int a[1024];
}

I think this is a duplicate but I cannot find the bug number.
Comment 3 Andrew Pinski 2003-07-21 02:54:19 UTC
This problem is already fixed on the tree-ssa branch so suspending untill it merges into 
the mainline.
Comment 4 Andrew Pinski 2003-12-17 00:33:25 UTC
Weird:
The testcase:
f()
{
  int a[1024];
}
is not optimized but the testcase is:
f()
{
  int a[1024];
  a[0]=0;
}
Comment 5 Andrew Pinski 2004-03-03 07:18:19 UTC
The problem here is that pass_remove_useless_vars is not being called for the last 
testcase, the one without any real code.
Comment 6 Steven Bosscher 2004-05-17 10:47:16 UTC
Ehm, pass_remove_useless_vars _is_ called, but there is no variable annotation
for unused variables, so:

  /* Remove all unused, unaliased temporaries.  Also remove unused, unaliased
     local variables during highly optimizing compilations.  */
  ann = var_ann (var);
  if (ann
      && ! ann->may_aliases
      && ! ann->used
      && ! ann->has_hidden_use
      && ! TREE_ADDRESSABLE (var)

we already have (!ann).  Something like this should work:

+ if (!ann)
+   return false;
! else if (! ann->may_aliases
(etc.)

I'll make a patch and try if that works.
Comment 7 Andrew Pinski 2004-05-23 23:32:26 UTC
Steven did your patch work?
Comment 8 Steven Bosscher 2004-05-24 06:46:51 UTC
Subject: Re:  unused arrays not optimized away

On Monday 24 May 2004 01:32, pinskia at gcc dot gnu dot org wrote:
> ------- Additional Comments From pinskia at gcc dot gnu dot org  2004-05-23
> 23:32 ------- Steven did your patch work?

no, but a modified one does. it's not pretty though, so i'm
looking for something else now.

Comment 9 Andrew Pinski 2004-07-22 06:33:14 UTC
Fixed for 3.5.0 by:
2004-07-21  Richard Henderson  <rth@redhat.com>

        * gimple-low.c (expand_var_p): Don't look at TREE_ADDRESSABLE,
        TREE_THIS_VOLATILE, may_aliases, or optimization level.
        (remove_useless_vars): Dump debugging info.
        (expand_used_vars): Move ...
        * cfgexpand.c (expand_used_vars): ... here.  Make static.
        * tree-flow-inline.h (set_is_used): New.
        (set_default_def): Use get_var_ann.
        * tree-flow.h: Update decls.
        * tree-ssa-live.c (mark_all_vars_used_1, mark_all_vars_used): New.
        (create_ssa_var_map): Use it.
        * tree-ssa.c (set_is_used): Remove.