Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / RelativeVTablesABI / dynamic-cast.cpp
blob20b9557bcc1a593836ed61510cacdc6e512379c3
1 // dynamic_cast
2 // Ensure that dynamic casting works normally
4 // RUN: %clang_cc1 %s -triple=aarch64-unknown-fuchsia -O3 -S -o - -emit-llvm | FileCheck %s
6 // CHECK: define{{.*}} ptr @_Z6upcastP1B(ptr noundef readnone returned %b) local_unnamed_addr
7 // CHECK-NEXT: entry:
8 // CHECK-NEXT: ret ptr %b
9 // CHECK-NEXT: }
11 // CHECK: define{{.*}} ptr @_Z8downcastP1A(ptr noundef readonly %a) local_unnamed_addr
12 // CHECK-NEXT: entry:
13 // CHECK-NEXT: [[isnull:%[0-9]+]] = icmp eq ptr %a, null
14 // CHECK-NEXT: br i1 [[isnull]], label %[[dynamic_cast_end:[a-z0-9._]+]], label %[[dynamic_cast_notnull:[a-z0-9._]+]]
15 // CHECK: [[dynamic_cast_notnull]]:
16 // CHECK-NEXT: [[as_b:%[0-9]+]] = tail call ptr @__dynamic_cast(ptr nonnull %a, ptr nonnull @_ZTI1A, ptr nonnull @_ZTI1B, i64 0)
17 // CHECK-NEXT: br label %[[dynamic_cast_end]]
18 // CHECK: [[dynamic_cast_end]]:
19 // CHECK-NEXT: [[res:%[0-9]+]] = phi ptr [ [[as_b]], %[[dynamic_cast_notnull]] ], [ null, %entry ]
20 // CHECK-NEXT: ret ptr [[res]]
21 // CHECK-NEXT: }
23 // CHECK: declare ptr @__dynamic_cast(ptr, ptr, ptr, i64) local_unnamed_addr
25 // CHECK: define{{.*}} ptr @_Z8selfcastP1B(ptr noundef readnone returned %b) local_unnamed_addr
26 // CHECK-NEXT: entry
27 // CHECK-NEXT: ret ptr %b
28 // CHECK-NEXT: }
30 // CHECK: define{{.*}} ptr @_Z9void_castP1B(ptr noundef readonly %b) local_unnamed_addr
31 // CHECK-NEXT: entry:
32 // CHECK-NEXT: [[isnull:%[0-9]+]] = icmp eq ptr %b, null
33 // CHECK-NEXT: br i1 [[isnull]], label %[[dynamic_cast_end:[a-z0-9._]+]], label %[[dynamic_cast_notnull:[a-z0-9._]+]]
34 // CHECK: [[dynamic_cast_notnull]]:
35 // CHECK-DAG: [[vtable:%[a-z0-9]+]] = load ptr, ptr %b, align 8
36 // CHECK-DAG: [[offset_ptr:%.+]] = getelementptr inbounds i32, ptr [[vtable]], i64 -2
37 // CHECK-DAG: [[offset_to_top:%.+]] = load i32, ptr [[offset_ptr]], align 4
38 // CHECK-DAG: [[offset_to_top2:%.+]] = sext i32 [[offset_to_top]] to i64
39 // CHECK-DAG: [[casted:%.+]] = getelementptr inbounds i8, ptr %b, i64 [[offset_to_top2]]
40 // CHECK-NEXT: br label %[[dynamic_cast_end]]
41 // CHECK: [[dynamic_cast_end]]:
42 // CHECK-NEXT: [[res:%[0-9]+]] = phi ptr [ [[casted]], %[[dynamic_cast_notnull]] ], [ null, %entry ]
43 // CHECK-NEXT: ret ptr [[res]]
44 // CHECK-NEXT: }
46 class A {
47 public:
48 virtual void foo();
51 class B : public A {
52 public:
53 void foo() override;
56 void A::foo() {}
57 void B::foo() {}
59 A *upcast(B *b) {
60 return dynamic_cast<A *>(b);
63 B *downcast(A *a) {
64 return dynamic_cast<B *>(a);
67 B *selfcast(B *b) {
68 return dynamic_cast<B *>(b);
71 void *void_cast(B *b) {
72 return dynamic_cast<void *>(b);