public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97151] New: GCC fails to optimize away uselessly allocated arrays (C++)
@ 2020-09-21 15:13 marat at slonopotamus dot org
  2020-09-21 15:14 ` [Bug c++/97151] " marat at slonopotamus dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: marat at slonopotamus dot org @ 2020-09-21 15:13 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97151
           Summary: GCC fails to optimize away uselessly allocated arrays
                    (C++)
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marat at slonopotamus dot org
  Target Milestone: ---

A test program:

int main()
{
    int *a = new int[10]();
    delete[] a;

    return 0;
}


Tested against: gcc 10.2.0 and gcc trunk as of 20200920 on codebolt.org with
-O2, -O3 and -Ofast

Expected: new/delete are optimized away under -O2 and higher because "a" is
never used for anything useful (like, side effects or whatever).

Actual: new/delete are present in assembly:

main:
        sub     rsp, 8
        mov     edi, 40
        call    operator new[](unsigned long)
        pxor    xmm0, xmm0
        mov     QWORD PTR [rax+32], 0
        mov     rdi, rax
        movups  XMMWORD PTR [rax], xmm0
        movups  XMMWORD PTR [rax+16], xmm0
        call    operator delete[](void*)
        xor     eax, eax
        add     rsp, 8
        ret

Interesting observations:

1. if "delete[]" is commented out, whole program is optimized away
2. if "new int[10]()" is replaced with "new int[10]", whole program is
optimized away
3. if new/delete is replaced with matching malloc/free, whole program is
optimized away

If someone wants to play with this example online, here it is:
https://godbolt.org/z/YoGzxo

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

end of thread, other threads:[~2020-09-23  8:14 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-21 15:13 [Bug c++/97151] New: GCC fails to optimize away uselessly allocated arrays (C++) marat at slonopotamus dot org
2020-09-21 15:14 ` [Bug c++/97151] " marat at slonopotamus dot org
2020-09-22  6:37 ` rguenth at gcc dot gnu.org
2020-09-22  7:05 ` marxin at gcc dot gnu.org
2020-09-22  7:18 ` marat at slonopotamus dot org
2020-09-22  7:19 ` redi at gcc dot gnu.org
2020-09-22  7:57 ` rguenth at gcc dot gnu.org
2020-09-22  9:42 ` rguenth at gcc dot gnu.org
2020-09-22  9:55 ` redi at gcc dot gnu.org
2020-09-23  8:14 ` cvs-commit at gcc dot gnu.org
2020-09-23  8:14 ` 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).