1 // RUN: %clang_cc1 -triple x86_64-unknown-linux -flto -flto-unit -fvirtual-function-elimination -fwhole-program-vtables -emit-llvm -o - %s | FileCheck %s
2 
3 
4 struct __attribute__((visibility("default"))) A {
5   virtual void foo();
6 };
7 
8 void test_1(A *p) {
9   // A has default visibility, so no need for type.checked.load.
10 // CHECK-LABEL: define void @_Z6test_1P1A
11 // CHECK: [[FN_PTR_ADDR:%.+]] = getelementptr inbounds void (%struct.A*)*, void (%struct.A*)** {{%.+}}, i64 0
12 // CHECK: [[FN_PTR:%.+]] = load void (%struct.A*)*, void (%struct.A*)** [[FN_PTR_ADDR]]
13 // CHECK: call void [[FN_PTR]](
14   p->foo();
15 }
16 
17 
18 struct __attribute__((visibility("hidden"))) [[clang::lto_visibility_public]] B {
19   virtual void foo();
20 };
21 
22 void test_2(B *p) {
23   // B has public LTO visibility, so no need for type.checked.load.
24 // CHECK-LABEL: define void @_Z6test_2P1B
25 // CHECK: [[FN_PTR_ADDR:%.+]] = getelementptr inbounds void (%struct.B*)*, void (%struct.B*)** {{%.+}}, i64 0
26 // CHECK: [[FN_PTR:%.+]] = load void (%struct.B*)*, void (%struct.B*)** [[FN_PTR_ADDR]]
27 // CHECK: call void [[FN_PTR]](
28   p->foo();
29 }
30 
31 
32 struct __attribute__((visibility("hidden"))) C {
33   virtual void foo();
34   virtual void bar();
35 };
36 
37 void test_3(C *p) {
38   // C has hidden visibility, so we generate type.checked.load to allow VFE.
39 // CHECK-LABEL: define void @_Z6test_3P1C
40 // CHECK: [[LOAD:%.+]] = call { i8*, i1 } @llvm.type.checked.load(i8* {{%.+}}, i32 0, metadata !"_ZTS1C")
41 // CHECK: [[FN_PTR_I8:%.+]] = extractvalue { i8*, i1 } [[LOAD]], 0
42 // CHECK: [[FN_PTR:%.+]] = bitcast i8* [[FN_PTR_I8]] to void (%struct.C*)*
43 // CHECK: call void [[FN_PTR]](
44   p->foo();
45 }
46 
47 void test_4(C *p) {
48   // When using type.checked.load, we pass the vtable offset to the intrinsic,
49   // rather than adding it to the pointer with a GEP.
50 // CHECK-LABEL: define void @_Z6test_4P1C
51 // CHECK: [[LOAD:%.+]] = call { i8*, i1 } @llvm.type.checked.load(i8* {{%.+}}, i32 8, metadata !"_ZTS1C")
52 // CHECK: [[FN_PTR_I8:%.+]] = extractvalue { i8*, i1 } [[LOAD]], 0
53 // CHECK: [[FN_PTR:%.+]] = bitcast i8* [[FN_PTR_I8]] to void (%struct.C*)*
54 // CHECK: call void [[FN_PTR]](
55   p->bar();
56 }
57 
58 void test_5(C *p, void (C::*q)(void)) {
59   // We also use type.checked.load for the virtual side of member function
60   // pointer calls. We use a GEP to calculate the address to load from and pass
61   // 0 as the offset to the intrinsic, because we know that the load must be
62   // from exactly the point marked by one of the function-type metadatas (in
63   // this case "_ZTSM1CFvvE.virtual"). If we passed the offset from the member
64   // function pointer to the intrinsic, this information would be lost. No
65   // codegen changes on the non-virtual side.
66 // CHECK-LABEL: define void @_Z6test_5P1CMS_FvvE(
67 // CHECK: [[FN_PTR_ADDR:%.+]] = getelementptr i8, i8* %vtable, i64 {{%.+}}
68 // CHECK: [[LOAD:%.+]] = call { i8*, i1 } @llvm.type.checked.load(i8* [[FN_PTR_ADDR]], i32 0, metadata !"_ZTSM1CFvvE.virtual")
69 // CHECK: [[FN_PTR_I8:%.+]] = extractvalue { i8*, i1 } [[LOAD]], 0
70 // CHECK: [[FN_PTR:%.+]] = bitcast i8* [[FN_PTR_I8]] to void (%struct.C*)*
71 
72 // CHECK: [[PHI:%.+]] = phi void (%struct.C*)* {{.*}}[ [[FN_PTR]], {{.*}} ]
73 // CHECK: call void [[PHI]](
74   (p->*q)();
75 }
76