1 ; REQUIRES: x86-registered-target
3 ; Test devirtualization through the thin link and backend, when vtables
4 ; have vcall_visibility metadata with public visibility.
7 ; Generate unsplit module with summary for ThinLTO index-based WPD.
8 ; RUN: opt -thinlto-bc -o %t2.o %s
9 ; RUN: llvm-lto2 run %t2.o -save-temps -pass-remarks=. \
10 ; RUN: -whole-program-visibility \
12 ; RUN: -r=%t2.o,test,px \
13 ; RUN: -r=%t2.o,test_public,px \
14 ; RUN: -r=%t2.o,_ZN1A1nEi,p \
15 ; RUN: -r=%t2.o,_ZN1B1fEi,p \
16 ; RUN: -r=%t2.o,_ZN1C1fEi,p \
17 ; RUN: -r=%t2.o,_ZN1D1mEi,p \
18 ; RUN: -r=%t2.o,_ZTV1B,px \
19 ; RUN: -r=%t2.o,_ZTV1C,px \
20 ; RUN: -r=%t2.o,_ZTV1D,px 2>&1 | FileCheck %s --check-prefix=REMARK
21 ; RUN: llvm-dis %t3.1.3.import.bc -o - | FileCheck %s --check-prefix=CHECK-TT
22 ; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
25 ; Generate split module with summary for hybrid Thin/Regular LTO WPD.
26 ; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t.o %s
27 ; RUN: llvm-lto2 run %t.o -save-temps -pass-remarks=. \
28 ; RUN: -whole-program-visibility \
30 ; RUN: -r=%t.o,test,px \
31 ; RUN: -r=%t.o,test_public,px \
32 ; RUN: -r=%t.o,_ZN1A1nEi,p \
33 ; RUN: -r=%t.o,_ZN1B1fEi,p \
34 ; RUN: -r=%t.o,_ZN1C1fEi,p \
35 ; RUN: -r=%t.o,_ZN1D1mEi,p \
36 ; RUN: -r=%t.o,_ZTV1B, \
37 ; RUN: -r=%t.o,_ZTV1C, \
38 ; RUN: -r=%t.o,_ZTV1D, \
39 ; RUN: -r=%t.o,_ZN1A1nEi, \
40 ; RUN: -r=%t.o,_ZN1B1fEi, \
41 ; RUN: -r=%t.o,_ZN1C1fEi, \
42 ; RUN: -r=%t.o,_ZN1D1mEi, \
43 ; RUN: -r=%t.o,_ZTV1B,px \
44 ; RUN: -r=%t.o,_ZTV1C,px \
45 ; RUN: -r=%t.o,_ZTV1D,px 2>&1 | FileCheck %s --check-prefix=REMARK --dump-input=fail
46 ; RUN: llvm-dis %t3.1.3.import.bc -o - | FileCheck %s --check-prefix=CHECK-TT
47 ; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
50 ; RUN: opt -o %t4.o %s
51 ; RUN: llvm-lto2 run %t4.o -save-temps -pass-remarks=. \
52 ; RUN: -whole-program-visibility \
54 ; RUN: -r=%t4.o,test,px \
55 ; RUN: -r=%t4.o,test_public,px \
56 ; RUN: -r=%t4.o,_ZN1A1nEi,p \
57 ; RUN: -r=%t4.o,_ZN1B1fEi,p \
58 ; RUN: -r=%t4.o,_ZN1C1fEi,p \
59 ; RUN: -r=%t4.o,_ZN1D1mEi,p \
60 ; RUN: -r=%t4.o,_ZTV1B,px \
61 ; RUN: -r=%t4.o,_ZTV1C,px \
62 ; RUN: -r=%t4.o,_ZTV1D,px 2>&1 | FileCheck %s --check-prefix=REMARK
63 ; RUN: llvm-dis %t5.0.0.preopt.bc -o - | FileCheck %s --check-prefix=CHECK-TT
64 ; RUN: llvm-dis %t5.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
66 ; REMARK-DAG: single-impl: devirtualized a call to _ZN1A1nEi
67 ; REMARK-DAG: single-impl: devirtualized a call to _ZN1D1mEi
68 ; REMARK-DAG: single-impl: devirtualized a call to _ZN1A1nEi
69 ; REMARK-DAG: single-impl: devirtualized a call to _ZN1D1mEi
71 ; Try everything again but without -whole-program-visibility to confirm
75 ; RUN: llvm-lto2 run %t2.o -save-temps -pass-remarks=. \
77 ; RUN: -r=%t2.o,test,px \
78 ; RUN: -r=%t2.o,test_public,px \
79 ; RUN: -r=%t2.o,_ZN1A1nEi,p \
80 ; RUN: -r=%t2.o,_ZN1B1fEi,p \
81 ; RUN: -r=%t2.o,_ZN1C1fEi,p \
82 ; RUN: -r=%t2.o,_ZN1D1mEi,p \
83 ; RUN: -r=%t2.o,_ZTV1B,px \
84 ; RUN: -r=%t2.o,_ZTV1C,px \
85 ; RUN: -r=%t2.o,_ZTV1D,px 2>&1 | FileCheck %s --implicit-check-not single-impl --allow-empty
86 ; RUN: llvm-dis %t3.1.3.import.bc -o - | FileCheck %s --check-prefix=CHECK-TT
87 ; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
90 ; RUN: llvm-lto2 run %t.o -save-temps -pass-remarks=. \
92 ; RUN: -r=%t.o,test,px \
93 ; RUN: -r=%t.o,test_public,px \
94 ; RUN: -r=%t.o,_ZN1A1nEi,p \
95 ; RUN: -r=%t.o,_ZN1B1fEi,p \
96 ; RUN: -r=%t.o,_ZN1C1fEi,p \
97 ; RUN: -r=%t.o,_ZN1D1mEi,p \
98 ; RUN: -r=%t.o,_ZTV1B, \
99 ; RUN: -r=%t.o,_ZTV1C, \
100 ; RUN: -r=%t.o,_ZTV1D, \
101 ; RUN: -r=%t.o,_ZN1A1nEi, \
102 ; RUN: -r=%t.o,_ZN1B1fEi, \
103 ; RUN: -r=%t.o,_ZN1C1fEi, \
104 ; RUN: -r=%t.o,_ZN1D1mEi, \
105 ; RUN: -r=%t.o,_ZTV1B,px \
106 ; RUN: -r=%t.o,_ZTV1C,px \
107 ; RUN: -r=%t.o,_ZTV1D,px 2>&1 | FileCheck %s --implicit-check-not single-impl --allow-empty
108 ; RUN: llvm-dis %t3.1.3.import.bc -o - | FileCheck %s --check-prefix=CHECK-TT
109 ; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
112 ; RUN: llvm-lto2 run %t4.o -save-temps -pass-remarks=. \
114 ; RUN: -r=%t4.o,test,px \
115 ; RUN: -r=%t4.o,test_public,px \
116 ; RUN: -r=%t4.o,_ZN1A1nEi,p \
117 ; RUN: -r=%t4.o,_ZN1B1fEi,p \
118 ; RUN: -r=%t4.o,_ZN1C1fEi,p \
119 ; RUN: -r=%t4.o,_ZN1D1mEi,p \
120 ; RUN: -r=%t4.o,_ZTV1B,px \
121 ; RUN: -r=%t4.o,_ZTV1C,px \
122 ; RUN: -r=%t4.o,_ZTV1D,px 2>&1 | FileCheck %s --implicit-check-not single-impl --allow-empty
123 ; RUN: llvm-dis %t5.0.0.preopt.bc -o - | FileCheck %s --check-prefix=CHECK-TT
124 ; RUN: llvm-dis %t5.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
126 ; Try index-based WPD again with both -whole-program-visibility and
127 ; -disable-whole-program-visibility to confirm the latter overrides
128 ; the former and that WPD fails.
129 ; RUN: llvm-lto2 run %t2.o -save-temps -pass-remarks=. \
130 ; RUN: -whole-program-visibility \
131 ; RUN: -disable-whole-program-visibility \
133 ; RUN: -r=%t2.o,test,px \
134 ; RUN: -r=%t2.o,test_public,px \
135 ; RUN: -r=%t2.o,_ZN1A1nEi,p \
136 ; RUN: -r=%t2.o,_ZN1B1fEi,p \
137 ; RUN: -r=%t2.o,_ZN1C1fEi,p \
138 ; RUN: -r=%t2.o,_ZN1D1mEi,p \
139 ; RUN: -r=%t2.o,_ZTV1B,px \
140 ; RUN: -r=%t2.o,_ZTV1C,px \
141 ; RUN: -r=%t2.o,_ZTV1D,px 2>&1 | FileCheck %s --implicit-check-not single-impl --allow-empty
142 ; RUN: llvm-dis %t3.1.3.import.bc -o - | FileCheck %s --check-prefix=CHECK-TT
143 ; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
145 ; CHECK-TT-NOT: call {{.*}}@llvm.public.type.test
147 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
148 target triple = "x86_64-grtev4-linux-gnu"
150 %struct.A = type { ptr }
151 %struct.B = type { %struct.A }
152 %struct.C = type { %struct.A }
153 %struct.D = type { ptr }
155 @_ZTV1B = constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr undef, ptr @_ZN1B1fEi, ptr @_ZN1A1nEi] }, !type !0, !type !1, !vcall_visibility !5
156 @_ZTV1C = constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr undef, ptr @_ZN1C1fEi, ptr @_ZN1A1nEi] }, !type !0, !type !2, !vcall_visibility !5
157 @_ZTV1D = constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr undef, ptr @_ZN1D1mEi] }, !type !3, !vcall_visibility !5
160 ; CHECK-IR-LABEL: define {{(noundef )?}}i32 @test
161 define i32 @test(ptr %obj, ptr %obj2, i32 %a) {
163 %vtable = load ptr, ptr %obj
164 %p = call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS1A")
165 call void @llvm.assume(i1 %p)
166 %fptrptr = getelementptr ptr, ptr %vtable, i32 1
167 %fptr1 = load ptr, ptr %fptrptr, align 8
169 ; Check that the call was devirtualized.
170 ; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
171 ; CHECK-NODEVIRT-IR: %call = tail call i32 %fptr1
172 %call = tail call i32 %fptr1(ptr nonnull %obj, i32 %a)
174 %fptr22 = load ptr, ptr %vtable, align 8
176 ; We still have to call it as virtual.
177 ; CHECK-IR: %call3 = tail call i32 %fptr22
178 ; CHECK-NODEVIRT-IR: %call3 = tail call i32 %fptr22
179 %call3 = tail call i32 %fptr22(ptr nonnull %obj, i32 %call)
181 %vtable2 = load ptr, ptr %obj2
182 %p2 = call i1 @llvm.type.test(ptr %vtable2, metadata !4)
183 call void @llvm.assume(i1 %p2)
185 %fptr33 = load ptr, ptr %vtable2, align 8
187 ; Check that the call was devirtualized.
188 ; CHECK-IR: %call4 = tail call i32 @_ZN1D1mEi
189 ; CHECK-NODEVIRT-IR: %call4 = tail call i32 %fptr33
190 %call4 = tail call i32 %fptr33(ptr nonnull %obj2, i32 %call3)
193 ; CHECK-IR-LABEL: ret i32
196 ; CHECK-IR-LABEL: define {{(noundef )?}}i32 @test_public
197 define i32 @test_public(ptr %obj, ptr %obj2, i32 %a) {
199 %vtable = load ptr, ptr %obj
200 %p = call i1 @llvm.public.type.test(ptr %vtable, metadata !"_ZTS1A")
201 call void @llvm.assume(i1 %p)
202 %fptrptr = getelementptr ptr, ptr %vtable, i32 1
203 %fptr1 = load ptr, ptr %fptrptr, align 8
205 ; Check that the call was devirtualized.
206 ; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
207 ; CHECK-NODEVIRT-IR: %call = tail call i32 %fptr1
208 %call = tail call i32 %fptr1(ptr nonnull %obj, i32 %a)
210 %fptr22 = load ptr, ptr %vtable, align 8
212 ; We still have to call it as virtual.
213 ; CHECK-IR: %call3 = tail call i32 %fptr22
214 ; CHECK-NODEVIRT-IR: %call3 = tail call i32 %fptr22
215 %call3 = tail call i32 %fptr22(ptr nonnull %obj, i32 %call)
217 %vtable2 = load ptr, ptr %obj2
218 %p2 = call i1 @llvm.public.type.test(ptr %vtable2, metadata !4)
219 call void @llvm.assume(i1 %p2)
221 %fptr33 = load ptr, ptr %vtable2, align 8
223 ; Check that the call was devirtualized.
224 ; CHECK-IR: %call4 = tail call i32 @_ZN1D1mEi
225 ; CHECK-NODEVIRT-IR: %call4 = tail call i32 %fptr33
226 %call4 = tail call i32 %fptr33(ptr nonnull %obj2, i32 %call3)
229 ; CHECK-IR-LABEL: ret i32
232 declare i1 @llvm.type.test(ptr, metadata)
233 declare i1 @llvm.public.type.test(ptr, metadata)
234 declare void @llvm.assume(i1)
236 define i32 @_ZN1B1fEi(ptr %this, i32 %a) #0 {
240 define i32 @_ZN1A1nEi(ptr %this, i32 %a) #0 {
244 define i32 @_ZN1C1fEi(ptr %this, i32 %a) #0 {
248 define i32 @_ZN1D1mEi(ptr %this, i32 %a) #0 {
252 ; Make sure we don't inline or otherwise optimize out the direct calls.
253 attributes #0 = { noinline optnone }
255 !0 = !{i64 16, !"_ZTS1A"}
256 !1 = !{i64 16, !"_ZTS1B"}
257 !2 = !{i64 16, !"_ZTS1C"}