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

            Bug ID: 116868
           Summary: GCC trunk doesn't eliminate a superfluous new/delete
                    pair
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dccitaliano at gmail dot com
  Target Milestone: ---

Maybe this is known, but i wasn't able to find the exact duplicate.
(this is the closest I found -- apologies for the churn, if any)
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78104

https://godbolt.org/z/579rGG95z

#include <vector>
int sumVector() {
    const std::vector<int> vec = {1};
    int sum = 0;
    for (int i = 0; i < vec.size(); i++) {
        sum += vec[i];
    }
    return sum;
}

At -O3 emits:

sumVector():
        sub     rsp, 8
        mov     edi, 4
        call    operator new(unsigned long)
        mov     esi, 4
        mov     DWORD PTR [rax], 1
        mov     rdi, rax
        call    operator delete(void*, unsigned long)
        mov     eax, 1
        add     rsp, 8
        ret

I'd expect to emit something like (clang does it):

sumVector():
        mov     eax, 1
        ret

Reply via email to