public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113821] New: Missed optimization for final classes: expensive check for result of dynamic cast
@ 2024-02-08 0:00 janschultke at googlemail dot com
2024-02-08 0:04 ` [Bug c++/113821] " pinskia at gcc dot gnu.org
2024-02-08 0:05 ` pinskia at gcc dot gnu.org
0 siblings, 2 replies; 3+ messages in thread
From: janschultke at googlemail dot com @ 2024-02-08 0:00 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113821
Bug ID: 113821
Summary: Missed optimization for final classes: expensive check
for result of dynamic cast
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: janschultke at googlemail dot com
Target Milestone: ---
Code to reproduce (https://godbolt.org/z/48cabYT78)
===================================================
struct Base {
virtual ~Base() = default;
};
struct Derived final : Base {};
bool is_derived(Base& a) {
return dynamic_cast<Derived*>(&a);
}
Expected output (clang)
=======================
is_derived(Base&):
lea rax, [rip + vtable for Derived+16]
cmp qword ptr [rdi], rax
sete al
ret
Actual output (GCC)
===================
is_derived(Base&):
sub rsp, 8
xor ecx, ecx
mov edx, OFFSET FLAT:typeinfo for Derived
mov esi, OFFSET FLAT:typeinfo for Base
call __dynamic_cast
test rax, rax
setne al
add rsp, 8
ret
Explanation
===========
For final classes, checking for success of dynamic_cast is equivalent to
checking whether the vtable pointer equals that of the destination type.
GCC is overly pessimistic by calling __dynamic_cast, which is much more
expensive, I'd imagine. Clang emits the same kind of pessimistic code only when
Derived is not final.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-02-08 0:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-08 0:00 [Bug c++/113821] New: Missed optimization for final classes: expensive check for result of dynamic cast janschultke at googlemail dot com
2024-02-08 0:04 ` [Bug c++/113821] " pinskia at gcc dot gnu.org
2024-02-08 0:05 ` pinskia 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).