public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/65143] New: [C++11] missing devirtualization for virtual base in  "final" classes
@ 2015-02-20 17:52 balakrishnan.erode at gmail dot com
  2015-02-20 18:06 ` [Bug c++/65143] " balakrishnan.erode at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: balakrishnan.erode at gmail dot com @ 2015-02-20 17:52 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 65143
           Summary: [C++11] missing devirtualization for virtual base in
                    "final" classes
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: balakrishnan.erode at gmail dot com

When a class is marked final, it can devirtualize access to all base classes as
its layout is known. This is missing in gcc. 

struct A
{
  int i();
};

struct B : public virtual A
{
  int get()
  {
    return A::i() + 1;
  }
};

struct C final : public B
{
  int get()
  {
    return A::i() + 2;
  }
};

int foo(C& c)
{  
  return c.get(); // Need not go via vtable pointer as class C is final
}

int foo(B& b2)
{
  return b2.get(); // This has to go via vtable as most derived class can
change the location of A
}

Assembly: Both do virtual dispatch

foo(C&):
    subq    $8, %rsp
    movq    (%rdi), %rax
    addq    -24(%rax), %rdi
    call    one::A::i()
    addq    $8, %rsp
    addl    $2, %eax
    ret
foo(B&):
    subq    $8, %rsp
    movq    (%rdi), %rax
    addq    -24(%rax), %rdi
    call    one::A::i()
    addq    $8, %rsp
    addl    $1, %eax
    ret

Complete example:
gcc: http://goo.gl/U4KEvj
clang: http://goo.gl/PpQCkd  -- Clang does this optimization


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

end of thread, other threads:[~2015-08-13 10:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-20 17:52 [Bug c++/65143] New: [C++11] missing devirtualization for virtual base in "final" classes balakrishnan.erode at gmail dot com
2015-02-20 18:06 ` [Bug c++/65143] " balakrishnan.erode at gmail dot com
2015-03-13 16:12 ` balakrishnan.erode at gmail dot com
2015-03-15  6:45 ` maltsevm at gmail dot com
2015-03-17 14:48 ` balakrishnan.erode at gmail dot com
2015-08-12 10:30 ` trippels at gcc dot gnu.org
2015-08-12 10:37 ` trippels at gcc dot gnu.org
2015-08-13 10:42 ` paolo.carlini at oracle dot com

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