2 ;; Test that dynamically exported symbols prevent devirtualization.
4 ;; First check that we get devirtualization without any export dynamic options.
7 ;; Generate unsplit module with summary for ThinLTO index-based WPD.
8 ; RUN: opt --thinlto-bc -o %t2.o %s
9 ; RUN: ld.lld %t2.o -o %t3 -save-temps --lto-whole-program-visibility \
10 ; RUN: -mllvm -pass-remarks=. 2>&1 | FileCheck %s --check-prefix=REMARK
11 ; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
14 ;; Generate split module with summary for hybrid Thin/Regular LTO WPD.
15 ; RUN: opt --thinlto-bc --thinlto-split-lto-unit -o %t.o %s
16 ; RUN: ld.lld %t.o -o %t3 -save-temps --lto-whole-program-visibility \
17 ; RUN: -mllvm -pass-remarks=. 2>&1 | FileCheck %s --check-prefix=REMARK
18 ; RUN: llvm-dis %t.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
21 ; RUN: opt -o %t4.o %s
22 ; RUN: ld.lld %t4.o -o %t3 -save-temps --lto-whole-program-visibility \
23 ; RUN: -mllvm -pass-remarks=. 2>&1 | FileCheck %s --check-prefix=REMARK
24 ; RUN: llvm-dis %t3.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
26 ; REMARK-DAG: single-impl: devirtualized a call to _ZN1A1nEi
27 ; REMARK-DAG: single-impl: devirtualized a call to _ZN1D1mEi
29 ;; Check that all WPD fails with --export-dynamic.
32 ; RUN: ld.lld %t2.o -o %t3 -save-temps --lto-whole-program-visibility \
33 ; RUN: -mllvm -pass-remarks=. \
34 ; RUN: --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
35 ; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
38 ; RUN: ld.lld %t.o -o %t3 -save-temps --lto-whole-program-visibility \
39 ; RUN: -mllvm -pass-remarks=. \
40 ; RUN: --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
41 ; RUN: llvm-dis %t.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
44 ; RUN: ld.lld %t4.o -o %t3 -save-temps --lto-whole-program-visibility \
45 ; RUN: -mllvm -pass-remarks=. \
46 ; RUN: --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
47 ; RUN: llvm-dis %t3.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
49 ;; Check that WPD fails for target _ZN1D1mEi with --export-dynamic-symbol=_ZTV1D.
52 ; RUN: ld.lld %t2.o -o %t3 -save-temps --lto-whole-program-visibility \
53 ; RUN: -mllvm -pass-remarks=. \
54 ; RUN: --export-dynamic-symbol=_ZTV1D 2>&1 | FileCheck %s --check-prefix=REMARK-AONLY
55 ; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-AONLY-IR
58 ; RUN: ld.lld %t.o -o %t3 -save-temps --lto-whole-program-visibility \
59 ; RUN: -mllvm -pass-remarks=. \
60 ; RUN: --export-dynamic-symbol=_ZTV1D 2>&1 | FileCheck %s --check-prefix=REMARK-AONLY
61 ; RUN: llvm-dis %t.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-AONLY-IR
64 ; RUN: ld.lld %t4.o -o %t3 -save-temps --lto-whole-program-visibility \
65 ; RUN: -mllvm -pass-remarks=. \
66 ; RUN: --export-dynamic-symbol=_ZTV1D 2>&1 | FileCheck %s --check-prefix=REMARK-AONLY
67 ; RUN: llvm-dis %t3.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-AONLY-IR
69 ; REMARK-AONLY-NOT: single-impl:
70 ; REMARK-AONLY: single-impl: devirtualized a call to _ZN1A1nEi
71 ; REMARK-AONLY-NOT: single-impl:
73 ;; Check that WPD fails for target _ZN1D1mEi with _ZTV1D in --dynamic-list.
74 ; RUN: echo "{ _ZTV1D; };" > %t.list
77 ; RUN: ld.lld %t2.o -o %t3 -save-temps --lto-whole-program-visibility \
78 ; RUN: -mllvm -pass-remarks=. \
79 ; RUN: --dynamic-list=%t.list 2>&1 | FileCheck %s --check-prefix=REMARK-AONLY
80 ; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-AONLY-IR
83 ; RUN: ld.lld %t.o -o %t3 -save-temps --lto-whole-program-visibility \
84 ; RUN: -mllvm -pass-remarks=. \
85 ; RUN: --dynamic-list=%t.list 2>&1 | FileCheck %s --check-prefix=REMARK-AONLY
86 ; RUN: llvm-dis %t.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-AONLY-IR
89 ; RUN: ld.lld %t4.o -o %t3 -save-temps --lto-whole-program-visibility \
90 ; RUN: -mllvm -pass-remarks=. \
91 ; RUN: --dynamic-list=%t.list 2>&1 | FileCheck %s --check-prefix=REMARK-AONLY
92 ; RUN: llvm-dis %t3.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-AONLY-IR
95 ;; Check that all WPD fails with when linking against a shared library containing
96 ;; preemptible versions of the vtables. In this case the symbols in the object being
97 ;; linked against the shared library must be exported to .dynsym to allow the runtime
98 ;; preemption, even without any options.
101 ; RUN: opt -relocation-model=pic --thinlto-bc -o %t5.o %s
102 ; RUN: ld.lld %t5.o -o %t5.so -shared
103 ; RUN: ld.lld %t5.o %t5.so -o %t5 -save-temps --lto-whole-program-visibility \
104 ; RUN: -mllvm -pass-remarks=. 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
107 ; RUN: opt -relocation-model=pic --thinlto-bc --thinlto-split-lto-unit -o %t5.o %s
108 ; RUN: ld.lld %t5.o -o %t5.so -shared
109 ; RUN: ld.lld %t5.o %t5.so -o %t5 -save-temps --lto-whole-program-visibility \
110 ; RUN: -mllvm -pass-remarks=. 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
113 ; RUN: opt -relocation-model=pic -o %t5.o %s
114 ; RUN: ld.lld %t5.o -o %t5.so -shared
115 ; RUN: ld.lld %t5.o %t5.so -o %t5 -save-temps --lto-whole-program-visibility \
116 ; RUN: -mllvm -pass-remarks=. 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
118 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
119 target triple = "x86_64-grtev4-linux-gnu"
121 %struct.A = type { ptr }
122 %struct.B = type { %struct.A }
123 %struct.C = type { %struct.A }
124 %struct.D = type { ptr }
126 @_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
127 @_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
128 @_ZTV1D = linkonce_odr unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr undef, ptr @_ZN1D1mEi] }, !type !3, !vcall_visibility !5
130 ;; Prevent the vtables from being dead code eliminated.
131 @llvm.used = appending global [3 x ptr] [ ptr @_ZTV1B, ptr @_ZTV1C, ptr @_ZTV1D]
133 ; CHECK-IR-LABEL: define dso_local i32 @_start
134 define i32 @_start(ptr %obj, ptr %obj2, i32 %a) {
136 %vtable = load ptr, ptr %obj
137 %p = call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS1A")
138 call void @llvm.assume(i1 %p)
139 %fptrptr = getelementptr ptr, ptr %vtable, i32 1
140 %fptr1 = load ptr, ptr %fptrptr, align 8
142 ;; Check that the call was devirtualized.
143 ; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
144 ; CHECK-AONLY-IR: %call = tail call i32 @_ZN1A1nEi
145 ; CHECK-NODEVIRT-IR: %call = tail call i32 %fptr1
146 %call = tail call i32 %fptr1(ptr nonnull %obj, i32 %a)
148 %fptr22 = load ptr, ptr %vtable, align 8
150 ;; We still have to call it as virtual.
151 ; CHECK-IR: %call3 = tail call i32 %fptr22
152 ; CHECK-AONLY-IR: %call3 = tail call i32 %fptr22
153 ; CHECK-NODEVIRT-IR: %call3 = tail call i32 %fptr22
154 %call3 = tail call i32 %fptr22(ptr nonnull %obj, i32 %call)
156 %vtable2 = load ptr, ptr %obj2
157 %p2 = call i1 @llvm.type.test(ptr %vtable2, metadata !4)
158 call void @llvm.assume(i1 %p2)
160 %fptr33 = load ptr, ptr %vtable2, align 8
162 ;; Check that the call was devirtualized.
163 ; CHECK-IR: %call4 = tail call i32 @_ZN1D1mEi
164 ; CHECK-AONLY-IR: %call4 = tail call i32 %fptr33
165 ; CHECK-NODEVIRT-IR: %call4 = tail call i32 %fptr33
166 %call4 = tail call i32 %fptr33(ptr nonnull %obj2, i32 %call3)
169 ; CHECK-IR-LABEL: ret i32
172 declare i1 @llvm.type.test(ptr, metadata)
173 declare void @llvm.assume(i1)
175 define i32 @_ZN1B1fEi(ptr %this, i32 %a) #0 {
179 define i32 @_ZN1A1nEi(ptr %this, i32 %a) #0 {
183 define i32 @_ZN1C1fEi(ptr %this, i32 %a) #0 {
187 define i32 @_ZN1D1mEi(ptr %this, i32 %a) #0 {
191 ;; Make sure we don't inline or otherwise optimize out the direct calls.
192 attributes #0 = { noinline optnone }
194 !0 = !{i64 16, !"_ZTS1A"}
195 !1 = !{i64 16, !"_ZTS1B"}
196 !2 = !{i64 16, !"_ZTS1C"}