public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50243] New: vtable for pure abstract class (interface) shouldn't be emitted
@ 2011-08-30 21:08 congruwer at yahoo dot co.uk
  2011-08-30 21:27 ` [Bug c++/50243] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: congruwer at yahoo dot co.uk @ 2011-08-30 21:08 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 50243
           Summary: vtable for pure abstract class (interface) shouldn't
                    be emitted
    Classification: Unclassified
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: congruwer@yahoo.co.uk


Note: this is probably easier to do if ‘Bug 34949 - Dead code in empty
destructors.’ is done.

Consider the following class:

class iface
{
protected:
    ~iface() { }
public:
    virtual void a() = 0;
    virtual void b() = 0;
    virtual void c() = 0;
};

This class cannot be instantiated, so a, b and c cannot be called from outside.
The only possible call site for them would be the destructor ~iface() but from
the fact that a, b and c are pure and the fact that it compiles, we know that
this doesn't happen. So the vtable for this class shouldn't be emitted.

But it is:

__ZTV5iface:
    .long    0
    .long    0
    .long    ___cxa_pure_virtual
    .long    ___cxa_pure_virtual
    .long    ___cxa_pure_virtual

To make matters worse, it's needlessly referenced in destructors of derived
classes:

__ZN4impl1cEv:
    pushl    %ebx
    subl    $8, %esp
    movl    16(%esp), %ebx
    testl    %ebx, %ebx
    je    L3
    movl    $__ZTV4impl+8, (%ebx)     <--- Strictly speaking unnecessary
    call    __Z7dostuffv
    movl    $__ZTV5iface+8, (%ebx)    <--- OOPS
    movl    %ebx, 16(%esp)              What follows is the inlined
    addl    $8, %esp                    destructor of iface, the
    popl    %ebx                        iface vtable isn't needed.
    jmp    __ZdlPv

For this example I deliberately used a small interface, but I have found that
in larger software projects the unnecessary vtables can add up.

For reference, the rest of code used to demonstrate the problem follows:

void dostuff();

class impl : public iface
{
private:
    ~impl() { dostuff(); }
public:
    void a() { dostuff(); }
    void b() { dostuff(); }
    void c() { delete this; }
};

void test()
{
    iface * y = new impl();
    y->a();
    y->b();
    y->c();
}


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-11-15 18:22 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-30 21:08 [Bug c++/50243] New: vtable for pure abstract class (interface) shouldn't be emitted congruwer at yahoo dot co.uk
2011-08-30 21:27 ` [Bug c++/50243] " pinskia at gcc dot gnu.org
2011-08-31 15:44 ` congruwer at yahoo dot co.uk
2013-03-25 22:11 ` jason at gcc dot gnu.org
2013-04-02 21:19 ` jason at gcc dot gnu.org
2013-04-03  7:15 ` jakub at gcc dot gnu.org
2013-04-08 13:51 ` jakub at gcc dot gnu.org
2014-11-15 18:22 ` fxcoudert at gcc dot gnu.org

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).