http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47671

           Summary: Missed optimization on empty virtual destructors
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: jyass...@gcc.gnu.org


At the beginning of each virtual destructor gcc writes the address of the
current class's vtable into the object's vptr. However, if the destructor is
empty, it doesn't need to do this, since after the return or delete call, the
object is dead, and any read of the vptr is undefined behavior..

Given:
$ cat test.cc
struct Base {
 virtual ~Base() {}
};

struct Derived : Base {
 ~Derived();
};
Derived::~Derived() {}

gcc-4.6 produces:

$ gcc-4.6svn -O3 -S -fverbose-asm test.cc -o -
...
       .globl  _ZN7DerivedD2Ev
       .type   _ZN7DerivedD2Ev, @function
_ZN7DerivedD2Ev:
       movq    $_ZTV4Base+16, (%rdi)   #, MEM[(struct Base
*)this_1(D)]._vptr.Base
       ret
...
       .globl  _ZN7DerivedD0Ev
       .type   _ZN7DerivedD0Ev, @function
_ZN7DerivedD0Ev:
       movq    $_ZTV4Base+16, (%rdi)   #, MEM[(struct Base
*)this_1(D)]._vptr.Base
       jmp     _ZdlPv  #


In PIC mode, this is 3 instructions instead of one:

$ gcc-4.6svn -fPIC -O3 -S -fverbose-asm test.cc -o -
...

_ZN7DerivedD2Ev:
       movq    _ZTV4Base@GOTPCREL(%rip), %rax  #, tmp63
       addq    $16, %rax       #, tmp62
       movq    %rax, (%rdi)    # tmp62, MEM[(struct Base
*)this_1(D)]._vptr.Base
       ret

Reply via email to