llvm-project/clang/test/CodeGenCXX/dynamic-cast-always-null.cpp
Richard Smith 9d525bf94b 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
2023-07-21 19:07:59 -07:00

58 lines
1.3 KiB
C++

// RUN: %clang_cc1 -I%S %s -triple x86_64-apple-darwin10 -emit-llvm -fcxx-exceptions -fexceptions -std=c++11 -o - | FileCheck %s
struct A { virtual ~A(); };
struct B final : A { };
struct C { virtual ~C(); int c; };
// CHECK: @_Z8nonnull1P1C
A *nonnull1(C* c) {
// CHECK: call {{.*}} @__dynamic_cast
return dynamic_cast<A*>(c);
}
// CHECK: @_Z8nonnull2P1A
C *nonnull2(A* a) {
// CHECK: call {{.*}} @__dynamic_cast
return dynamic_cast<C*>(a);
}
// CHECK: @_Z1fP1B
C *f(B* b) {
// CHECK-NOT: call {{.*}} @__dynamic_cast
// CHECK: ret ptr null
return dynamic_cast<C*>(b);
}
// CHECK: @_Z1fR1B
C &f(B& b) {
// CHECK-NOT: call {{.*}} @__dynamic_cast
// CHECK: call void @__cxa_bad_cast() [[NR:#[0-9]+]]
// CHECK: unreachable
//
// CHECK: ret ptr poison
return dynamic_cast<C&>(b);
}
// CHECK: @_Z1gP1C
B *g(C* c) {
// CHECK-NOT: call {{.*}} @__dynamic_cast
// CHECK: ret ptr null
return dynamic_cast<B*>(c);
}
// CHECK: @_Z1gR1C
B &g(C& c) {
// CHECK-NOT: call {{.*}} @__dynamic_cast
// CHECK: call void @__cxa_bad_cast() [[NR:#[0-9]+]]
// CHECK: unreachable
//
// CHECK: ret ptr poison
return dynamic_cast<B&>(c);
}
void dont_crash() {
(void) dynamic_cast<void*>((A*)0);
(void) dynamic_cast<void*>((B*)0);
}
// CHECK: attributes [[NR]] = { noreturn }