public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hiraditya at msn dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug ipa/106991] new+delete pair not optimized by g++ at -O3 but optimized at -Os
Date: Wed, 21 Sep 2022 21:37:53 +0000	[thread overview]
Message-ID: <bug-106991-4-Fvem8NP1Eb@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-106991-4@http.gcc.gnu.org/bugzilla/>

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
```

      parent reply	other threads:[~2022-09-21 21:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-21  4:53 [Bug c++/106991] New: " hiraditya at msn dot com
2022-09-21  8:17 ` [Bug ipa/106991] " rguenth at gcc dot gnu.org
2022-09-21 11:18 ` hubicka at ucw dot cz
2022-09-21 21:37 ` hiraditya at msn dot com [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-106991-4-Fvem8NP1Eb@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).