https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63164
Bug ID: 63164
Summary: unnecessary calls to __dynamic_cast
Product: gcc
Version: 4.9.1
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
The code
struct A {
virtual ~A() {}
};
struct B final : A {
virtual ~B() {}
};
B* dc(A* a) {
return dynamic_cast<B*>(a);
}
compiles into the following assembly, which contains a call (jump) to
__dynamic_cast:
0000000000000000 <dc(A*)>:
0: 48 85 ff test %rdi,%rdi
3: 74 1b je 20 <dc(A*)+0x20>
5: 31 c9 xor %ecx,%ecx
7: ba 00 00 00 00 mov $0x0,%edx
8: R_X86_64_32 typeinfo for B
c: be 00 00 00 00 mov $0x0,%esi
d: R_X86_64_32 typeinfo for A
11: e9 00 00 00 00 jmpq 16 <dc(A*)+0x16>
12: R_X86_64_PC32 __dynamic_cast-0x4
16: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
1d: 00 00 00
20: 31 c0 xor %eax,%eax
22: c3 retq
However, since B is declared final, a simple compare of a's typeinfo with B's
would suffice. This is a missed optimization opportunity.