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

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).