xref: /llvm-project/llvm/test/tools/gold/X86/devirt_vcall_vis_export_dynamic.ll (revision 5c458ed490a01dcc82f9d063732cac4207786fd5)
1;; Test that dynamically exported symbols prevent devirtualization.
2;; Note that --export-dynamic-symbol and --dynamic-list are tested similarly
3;; in the v1.16 directory, because those were not properly passed down to the
4;; plugin until then.
5
6;; First check that we get devirtualization without any export dynamic options.
7
8;; Index based WPD
9;; Generate unsplit module with summary for ThinLTO index-based WPD.
10; RUN: opt -thinlto-bc -o %t2.o %s
11; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
12; RUN:   --plugin-opt=whole-program-visibility \
13; RUN:   --plugin-opt=save-temps \
14; RUN:   --plugin-opt=-pass-remarks=. \
15; RUN:   %t2.o -o %t3 2>&1 | FileCheck %s --check-prefix=REMARK
16; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
17
18;; Hybrid WPD
19;; Generate split module with summary for hybrid Thin/Regular LTO WPD.
20; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t.o %s
21; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
22; RUN:   --plugin-opt=whole-program-visibility \
23; RUN:   --plugin-opt=save-temps \
24; RUN:   --plugin-opt=-pass-remarks=. \
25; RUN:   %t.o -o %t3 2>&1 | FileCheck %s --check-prefix=REMARK
26; RUN: llvm-dis %t.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
27
28;; Regular LTO WPD
29; RUN: opt -o %t4.o %s
30; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
31; RUN:   --plugin-opt=whole-program-visibility \
32; RUN:   --plugin-opt=save-temps \
33; RUN:   --plugin-opt=-pass-remarks=. \
34; RUN:   %t4.o -o %t3 2>&1 | FileCheck %s --check-prefix=REMARK
35; RUN: llvm-dis %t3.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
36
37; REMARK-DAG: single-impl: devirtualized a call to _ZN1A1nEi
38; REMARK-DAG: single-impl: devirtualized a call to _ZN1D1mEi
39
40;; Check that all WPD fails with --export-dynamic.
41
42;; Index based WPD
43; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
44; RUN:   --plugin-opt=whole-program-visibility \
45; RUN:   --plugin-opt=save-temps \
46; RUN:   --plugin-opt=-pass-remarks=. \
47; RUN:   %t2.o -o %t3 \
48; RUN:   --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
49; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
50
51;; Hybrid WPD
52; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
53; RUN:   --plugin-opt=whole-program-visibility \
54; RUN:   --plugin-opt=save-temps \
55; RUN:   --plugin-opt=-pass-remarks=. \
56; RUN:   %t.o -o %t3 \
57; RUN:   --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
58; RUN: llvm-dis %t.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
59
60;; Regular LTO WPD
61; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
62; RUN:   --plugin-opt=whole-program-visibility \
63; RUN:   --plugin-opt=save-temps \
64; RUN:   --plugin-opt=-pass-remarks=. \
65; RUN:   %t4.o -o %t3 \
66; RUN:   --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
67; RUN: llvm-dis %t3.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
68
69;; Check that all WPD fails with when linking against a shared library containing
70;; preemptible versions of the vtables. In this case the symbols in the object being
71;; linked against the shared library must be exported to .dynsym to allow the runtime
72;; preemption, even without any options.
73
74;; Index based WPD
75; RUN: opt -relocation-model=pic --thinlto-bc -o %t5.o %s
76; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
77; RUN: 	 %t5.o -o %t5.so -shared
78; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
79; RUN:   --plugin-opt=whole-program-visibility \
80; RUN:   --plugin-opt=-pass-remarks=. \
81; RUN:   %t5.o %t5.so -o %t5 \
82; RUN:   --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
83
84;; Hybrid WPD
85; RUN: opt -relocation-model=pic --thinlto-bc --thinlto-split-lto-unit -o %t5.o %s
86; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
87; RUN: 	 %t5.o -o %t5.so -shared
88; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
89; RUN:   --plugin-opt=whole-program-visibility \
90; RUN:   --plugin-opt=-pass-remarks=. \
91; RUN:   %t5.o %t5.so -o %t5 \
92; RUN:   --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
93
94;; Regular LTO WPD
95; RUN: opt -relocation-model=pic -o %t5.o %s
96; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
97; RUN: 	 %t5.o -o %t5.so -shared
98; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext \
99; RUN:   --plugin-opt=whole-program-visibility \
100; RUN:   --plugin-opt=-pass-remarks=. \
101; RUN:   %t5.o %t5.so -o %t5 \
102; RUN:   --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
103
104target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
105target triple = "x86_64-grtev4-linux-gnu"
106
107%struct.A = type { ptr }
108%struct.B = type { %struct.A }
109%struct.C = type { %struct.A }
110%struct.D = type { ptr }
111
112@_ZTV1B = linkonce_odr unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr undef, ptr @_ZN1B1fEi, ptr @_ZN1A1nEi] }, !type !0, !type !1, !vcall_visibility !5
113@_ZTV1C = linkonce_odr unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr undef, ptr @_ZN1C1fEi, ptr @_ZN1A1nEi] }, !type !0, !type !2, !vcall_visibility !5
114@_ZTV1D = linkonce_odr unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr undef, ptr @_ZN1D1mEi] }, !type !3, !vcall_visibility !5
115
116;; Prevent the vtables from being dead code eliminated.
117@llvm.used = appending global [3 x ptr] [ ptr @_ZTV1B, ptr @_ZTV1C, ptr @_ZTV1D]
118
119; CHECK-IR-LABEL: define dso_local {{(noundef )?}}i32 @_start
120define i32 @_start(ptr %obj, ptr %obj2, i32 %a) {
121entry:
122  %vtable = load ptr, ptr %obj
123  %p = call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS1A")
124  call void @llvm.assume(i1 %p)
125  %fptrptr = getelementptr ptr, ptr %vtable, i32 1
126  %fptr1 = load ptr, ptr %fptrptr, align 8
127
128  ;; Check that the call was devirtualized.
129  ; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
130  ; CHECK-NODEVIRT-IR: %call = tail call i32 %fptr1
131  %call = tail call i32 %fptr1(ptr nonnull %obj, i32 %a)
132
133  %fptr22 = load ptr, ptr %vtable, align 8
134
135  ;; We still have to call it as virtual.
136  ; CHECK-IR: %call3 = tail call i32 %fptr22
137  ; CHECK-NODEVIRT-IR: %call3 = tail call i32 %fptr22
138  %call3 = tail call i32 %fptr22(ptr nonnull %obj, i32 %call)
139
140  %vtable2 = load ptr, ptr %obj2
141  %p2 = call i1 @llvm.type.test(ptr %vtable2, metadata !4)
142  call void @llvm.assume(i1 %p2)
143
144  %fptr33 = load ptr, ptr %vtable2, align 8
145
146  ;; Check that the call was devirtualized.
147  ; CHECK-IR: %call4 = tail call i32 @_ZN1D1mEi
148  ; CHECK-NODEVIRT-IR: %call4 = tail call i32 %fptr33
149  %call4 = tail call i32 %fptr33(ptr nonnull %obj2, i32 %call3)
150  ret i32 %call4
151}
152; CHECK-IR-LABEL: ret i32
153; CHECK-IR-LABEL: }
154
155declare i1 @llvm.type.test(ptr, metadata)
156declare void @llvm.assume(i1)
157
158define i32 @_ZN1B1fEi(ptr %this, i32 %a) #0 {
159   ret i32 0;
160}
161
162define i32 @_ZN1A1nEi(ptr %this, i32 %a) #0 {
163   ret i32 0;
164}
165
166define i32 @_ZN1C1fEi(ptr %this, i32 %a) #0 {
167   ret i32 0;
168}
169
170define i32 @_ZN1D1mEi(ptr %this, i32 %a) #0 {
171   ret i32 0;
172}
173
174;; Make sure we don't inline or otherwise optimize out the direct calls.
175attributes #0 = { noinline optnone }
176
177!0 = !{i64 16, !"_ZTS1A"}
178!1 = !{i64 16, !"_ZTS1B"}
179!2 = !{i64 16, !"_ZTS1C"}
180!3 = !{i64 16, !4}
181!4 = distinct !{}
182!5 = !{i64 0}
183