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
8 // CHECK-NEXT: ret ptr %b
11 // CHECK: define{{.*}} ptr @_Z8downcastP1A(ptr noundef readonly %a) local_unnamed_addr
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]]
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
27 // CHECK-NEXT: ret ptr %b
30 // CHECK: define{{.*}} ptr @_Z9void_castP1B(ptr noundef readonly %b) local_unnamed_addr
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]]
60 return dynamic_cast<A
*>(b
);
64 return dynamic_cast<B
*>(a
);
68 return dynamic_cast<B
*>(b
);
71 void *void_cast(B
*b
) {
72 return dynamic_cast<void *>(b
);