Optimize emission of `dynamic_cast` to final classes.
authorRichard Smith <richard@metafoo.co.uk>
Sat, 22 Jul 2023 00:37:55 +0000 (17:37 -0700)
committerRichard Smith <richard@metafoo.co.uk>
Sat, 22 Jul 2023 02:07:59 +0000 (19:07 -0700)
commit9d525bf94b255df89587db955b5fa2d3c03c2c3e
treeea9df28908ba14a54facb966e9d077b7dadcdf9c
parent57bd882343f8e4cca598b6ad47da93476cffb987
Optimize emission of `dynamic_cast` to final classes.

- When the destination is a final class type that does not derive from
  the source type, the cast always fails and is now emitted as a null
  pointer or call to __cxa_bad_cast.

- When the destination is a final class type that does derive from the
  source type, emit a direct comparison against the corresponding base
  class vptr value(s). There may be more than one such value in the case
  of multiple inheritance; check them all.

For now, this is supported only for the Itanium ABI. I expect the same thing is
possible for the MS ABI too, but I don't know what guarantees are made about
vfptr uniqueness.

Reviewed By: rjmccall

Differential Revision: https://reviews.llvm.org/D154658
14 files changed:
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Driver/Options.td
clang/lib/AST/ExprCXX.cpp
clang/lib/CodeGen/CGCXXABI.h
clang/lib/CodeGen/CGExprCXX.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/CodeGen/CodeGenModule.h
clang/lib/CodeGen/ItaniumCXXABI.cpp
clang/lib/CodeGen/MicrosoftCXXABI.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/CodeGenCXX/dynamic-cast-always-null.cpp
clang/test/CodeGenCXX/dynamic-cast-exact-disabled.cpp [new file with mode: 0644]
clang/test/CodeGenCXX/dynamic-cast-exact.cpp [new file with mode: 0644]
clang/test/Driver/fassume-unique-vtables.cpp [new file with mode: 0644]