https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116001
            Bug ID: 116001
           Summary: Missing optimization: Unoptimized indirect virtual
                    call
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: user202729 at protonmail dot com
  Target Milestone: ---

Code:

#include<typeinfo>
#include<new>

struct Base {
        virtual ~Base();
};
struct Derived: Base {
    int x;
        ~Derived();
};

static_assert(sizeof(Base)<sizeof(Derived), "");

void f(bool x){
        char a[sizeof(Derived)];
        Base* c=x ? new (&a) Base : new (&a) Derived;
        c->~Base();
}

The assembly include instructions such as the following

        mov     eax, OFFSET FLAT:Derived::~Derived() [complete object
destructor]
        call    rax

gcc goes so far to constant propagate the pointer value, but it did not replace
the call with a direct call.

https://godbolt.org/z/vE33WvhE8

Reply via email to