[Bug ipa/106991] 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 21:37:53 GMT 2022


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

--- Comment #3 from AK <hiraditya at msn dot com> ---
Thanks for identifying the underlying issue @Jan 
After modifying the definition of operator delete. gcc does optimize it at -O3
as well.

https://godbolt.org/z/1WPqaWrEr

// source code
#include<cassert>
#include<cstdlib>

int volatile gv = 0;

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

void operator delete(void *p, unsigned long) 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*, unsigned long):
        mov     eax, DWORD PTR gv[rip]
        sub     eax, 1
        mov     DWORD PTR gv[rip], eax
        jmp     free
caller():
        mov     eax, DWORD PTR gv[rip]
        add     eax, 1
        mov     DWORD PTR gv[rip], eax
        mov     eax, DWORD PTR gv[rip]
        sub     eax, 1
        mov     DWORD PTR gv[rip], eax
        mov     eax, DWORD PTR gv[rip]
        ret
gv:
        .zero   4
```


More information about the Gcc-bugs mailing list