public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/62112] New: Optimize out malloc when block is unused or write-only
@ 2014-08-12 20:19 zackw at panix dot com
  2014-08-12 20:57 ` [Bug tree-optimization/62112] " glisse at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: zackw at panix dot com @ 2014-08-12 20:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 62112
           Summary: Optimize out malloc when block is unused or write-only
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zackw at panix dot com

This program

    #include <string.h>
    #include <stdlib.h>

    int
    main(void)
    {
        size_t n = 1000;
        float *x = calloc(n,sizeof(float));
        float *y = malloc(n*sizeof(float));
        if (x && y)
          memcpy(y,x,sizeof(float)*n);
        return 0;
    }

can be optimized (in the absence of `-fno-builtin-(memcpy|malloc|calloc)`) to

    int main(void) { return 0; }

because: the memory block pointed to by `y` is write-only, so the `memcpy` and
`malloc` can be discarded; after that is done, the memory block pointed to by
`x` is unused, so that allocation can be discarded as well.

`calloc` is used here to avoid any question of UB due to reading uninitialized
memory even within `memcpy`.  The optimization should apply to all
heap-allocation functions, including especially C++ operator new (as long as
the constructor has no side effects outside the just-allocated object).

Clang 3.5 does perform this optimization.


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

* [Bug tree-optimization/62112] Optimize out malloc when block is unused or write-only
  2014-08-12 20:19 [Bug tree-optimization/62112] New: Optimize out malloc when block is unused or write-only zackw at panix dot com
@ 2014-08-12 20:57 ` glisse at gcc dot gnu.org
  2014-08-12 21:06 ` zackw at panix dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: glisse at gcc dot gnu.org @ 2014-08-12 20:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> ---
Main issue here is that DSE only applies to assignments and not function calls
like memcpy (there must be a few dups somewhere), so we never remove memcpy,
even if we call free(x);free(y); afterwards. With a for loop instead we
optimize it just fine.


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

* [Bug tree-optimization/62112] Optimize out malloc when block is unused or write-only
  2014-08-12 20:19 [Bug tree-optimization/62112] New: Optimize out malloc when block is unused or write-only zackw at panix dot com
  2014-08-12 20:57 ` [Bug tree-optimization/62112] " glisse at gcc dot gnu.org
@ 2014-08-12 21:06 ` zackw at panix dot com
  2014-08-13  8:20 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: zackw at panix dot com @ 2014-08-12 21:06 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Zack Weinberg <zackw at panix dot com> ---
I observe that the `memcpy` does get lowered to inline code.  Is it just a
phase-ordering problem that we then don't detect the stores as dead?


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

* [Bug tree-optimization/62112] Optimize out malloc when block is unused or write-only
  2014-08-12 20:19 [Bug tree-optimization/62112] New: Optimize out malloc when block is unused or write-only zackw at panix dot com
  2014-08-12 20:57 ` [Bug tree-optimization/62112] " glisse at gcc dot gnu.org
  2014-08-12 21:06 ` zackw at panix dot com
@ 2014-08-13  8:20 ` rguenth at gcc dot gnu.org
  2014-08-21  9:32 ` glisse at gcc dot gnu.org
  2014-08-21  9:39 ` glisse at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-08-13  8:20 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-08-13
     Ever confirmed|0                           |1

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
We don't aggressively "lower" memcpy even if we could and in this case we
miss the point where x and y become "dead" (you don't call free()).

Note that the issue Marc mentions is simply that we don't DSE calls
(usually not important apart from for memcpy like builtins).  It shouldn't
be terribly hard to add this though.

Confirmed.


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

* [Bug tree-optimization/62112] Optimize out malloc when block is unused or write-only
  2014-08-12 20:19 [Bug tree-optimization/62112] New: Optimize out malloc when block is unused or write-only zackw at panix dot com
                   ` (2 preceding siblings ...)
  2014-08-13  8:20 ` rguenth at gcc dot gnu.org
@ 2014-08-21  9:32 ` glisse at gcc dot gnu.org
  2014-08-21  9:39 ` glisse at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: glisse at gcc dot gnu.org @ 2014-08-21  9:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Marc Glisse <glisse at gcc dot gnu.org> ---
Author: glisse
Date: Thu Aug 21 09:32:21 2014
New Revision: 214262

URL: https://gcc.gnu.org/viewcvs?rev=214262&root=gcc&view=rev
Log:
2014-08-21  Marc Glisse  <marc.glisse@inria.fr>

    PR tree-optimization/62112
gcc/
    * gimple-iterator.c (gsi_replace): Return whether EH cleanup is needed.
    * gimple-iterator.h (gsi_replace): Return bool.
    * tree-ssa-alias.c (ref_may_alias_global_p_1): New helper, code
    moved from ref_may_alias_global_p.
    (ref_may_alias_global_p, refs_may_alias_p, ref_maybe_used_by_stmt_p):
    New overloads.
    (ref_maybe_used_by_call_p): Take ao_ref* instead of tree.
    (stmt_kills_ref_p_1): Rename...
    (stmt_kills_ref_p): ... to this.
    * tree-ssa-alias.h (ref_may_alias_global_p, ref_maybe_used_by_stmt_p,
    stmt_kills_ref_p): Declare.
    * tree-ssa-dse.c (dse_possible_dead_store_p): New argument, use it.
    Move the self-assignment case...
    (dse_optimize_stmt): ... here. Handle builtin calls. Remove dead code.
gcc/testsuite/
    * gcc.dg/tree-ssa/pr62112-1.c: New file.
    * gcc.dg/tree-ssa/pr62112-2.c: Likewise.
    * gcc.c-torture/execute/pr35472.c: Add noclone attribute.
    * gcc.c-torture/execute/20071219-1.c: Likewise.


Added:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/pr62112-1.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/pr62112-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-iterator.c
    trunk/gcc/gimple-iterator.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.c-torture/execute/20071219-1.c
    trunk/gcc/testsuite/gcc.c-torture/execute/pr35472.c
    trunk/gcc/tree-ssa-alias.c
    trunk/gcc/tree-ssa-alias.h
    trunk/gcc/tree-ssa-dse.c


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

* [Bug tree-optimization/62112] Optimize out malloc when block is unused or write-only
  2014-08-12 20:19 [Bug tree-optimization/62112] New: Optimize out malloc when block is unused or write-only zackw at panix dot com
                   ` (3 preceding siblings ...)
  2014-08-21  9:32 ` glisse at gcc dot gnu.org
@ 2014-08-21  9:39 ` glisse at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: glisse at gcc dot gnu.org @ 2014-08-21  9:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |5.0

--- Comment #6 from Marc Glisse <glisse at gcc dot gnu.org> ---
The original testcase is now optimized to just "return 0;". Performing similar
optimizations for new/delete as for malloc/free is a different issue, that I am
sure is already covered by several PRs. Currently, the simplest workaround is
to inline new and delete.


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

end of thread, other threads:[~2014-08-21  9:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-12 20:19 [Bug tree-optimization/62112] New: Optimize out malloc when block is unused or write-only zackw at panix dot com
2014-08-12 20:57 ` [Bug tree-optimization/62112] " glisse at gcc dot gnu.org
2014-08-12 21:06 ` zackw at panix dot com
2014-08-13  8:20 ` rguenth at gcc dot gnu.org
2014-08-21  9:32 ` glisse at gcc dot gnu.org
2014-08-21  9:39 ` glisse 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).