[Bug c++/106991] New: new+delete pair not optimized by g++ at -O3 but optimized at -Os

hiraditya at msn dot com gcc-bugzilla@gcc.gnu.org
Wed Sep 21 04:53:47 GMT 2022


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

            Bug ID: 106991
           Summary: new+delete pair not optimized by g++ at -O3 but
                    optimized at -Os
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hiraditya at msn dot com
  Target Milestone: ---

https://godbolt.org/z/PeYcoqTKn

-----------------------------------------------------------
#include<cassert>
#include<cstdlib>

int volatile gv = 0;

void* operator new(long unsigned sz ) {
    ++gv;
    return malloc( sz );
}

void operator delete(void *p) noexcept {
    --gv;
    free(p);
}

class c {
    int l;
    public:
        c() : l(0) {}
        int get(){ return l; }
};

int caller( void ){
    c *f = new c();
    assert( f->get() == 0 );
    delete f;
    return gv;
}
-----------------------------------------------------------

$ g++ -std=c++20 -O3

operator new(unsigned long):
        mov     eax, DWORD PTR gv[rip]
        add     eax, 1
        mov     DWORD PTR gv[rip], eax
        jmp     malloc
operator delete(void*):
        mov     eax, DWORD PTR gv[rip]
        sub     eax, 1
        mov     DWORD PTR gv[rip], eax
        jmp     free
caller():
        sub     rsp, 8
        mov     eax, DWORD PTR gv[rip]
        mov     edi, 4
        add     eax, 1
        mov     DWORD PTR gv[rip], eax
        call    malloc
        mov     esi, 4
        mov     rdi, rax
        call    operator delete(void*, unsigned long)
        mov     eax, DWORD PTR gv[rip]
        add     rsp, 8
        ret
gv:
        .zero   4
-----------------------------------------------------------
$ g++ -std=c++20 -Os

operator new(unsigned long):
        mov     eax, DWORD PTR gv[rip]
        inc     eax
        mov     DWORD PTR gv[rip], eax
        jmp     malloc
operator delete(void*):
        mov     eax, DWORD PTR gv[rip]
        dec     eax
        mov     DWORD PTR gv[rip], eax
        jmp     free
caller():
        mov     eax, DWORD PTR gv[rip]
        ret
gv:
        .zero   4


More information about the Gcc-bugs mailing list