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