1 // Sparc64 doesn't support musttail (yet), so it uses method cloning for
2 // variadic thunks. Use it for testing.
3 // RUN: %clang_cc1 %s -triple=sparc64-pc-linux-gnu -funwind-tables=2 -emit-llvm -o - \
4 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-CLONE,CHECK-NONOPT %s
5 // RUN: %clang_cc1 %s -triple=sparc64-pc-linux-gnu -debug-info-kind=standalone -dwarf-version=5 -funwind-tables=2 -emit-llvm -o - \
6 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-CLONE,CHECK-NONOPT,CHECK-DBG %s
7 // RUN: %clang_cc1 %s -triple=sparc64-pc-linux-gnu -funwind-tables=2 -emit-llvm -o - -O1 -disable-llvm-passes \
8 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-CLONE,CHECK-OPT %s
10 // Test x86_64, which uses musttail for variadic thunks.
11 // RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -funwind-tables=2 -emit-llvm -o - -O1 -disable-llvm-passes \
12 // RUN: | FileCheck --check-prefixes=CHECK,CHECK-TAIL,CHECK-OPT %s
14 // Finally, reuse these tests for the MS ABI.
15 // RUN: %clang_cc1 %s -triple=x86_64-windows-msvc -funwind-tables=2 -emit-llvm -o - -O1 -disable-llvm-passes \
16 // RUN: | FileCheck --check-prefixes=WIN64 %s
21 // Check that we emit a non-virtual thunk for C::f.
37 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test11C1fEv(
38 // CHECK-DBG-NOT: dbg.declare
41 // WIN64-LABEL: define dso_local void @"?f@C@Test1@@UEAAXXZ"(
42 // WIN64-LABEL: define linkonce_odr dso_local void @"?f@C@Test1@@W7EAAXXZ"(
43 // WIN64: getelementptr i8, ptr {{.*}}, i32 -8
51 // Check that we emit a thunk for B::f since it's overriding a virtual base.
57 struct B
: virtual A
{
62 // CHECK-LABEL: define{{.*}} void @_ZTv0_n24_N5Test21B1fEv(
63 // CHECK-DBG-NOT: dbg.declare
67 // No thunk is used for this case in the MS ABI.
68 // WIN64-LABEL: define dso_local void @"?f@B@Test2@@UEAAXXZ"(
69 // WIN64-NOT: define {{.*}} void @"?f@B@Test2
75 // Check that we emit a covariant thunk for B::f.
78 struct V2
: virtual V1
{ };
90 // CHECK: define{{.*}} ptr @_ZTch0_v0_n24_N5Test31B1fEv(
91 // WIN64: define weak_odr dso_local noundef ptr @"?f@B@Test3@@QEAAPEAUV1@2@XZ"(
92 V2
*B::f() { return 0; }
98 // Check that the thunk for 'C::f' has the same visibility as the function itself.
108 struct __attribute__((visibility("protected"))) C
: A
, B
{
114 // CHECK-LABEL: define protected void @_ZThn8_N5Test41C1fEv(
115 // CHECK-DBG-NOT: dbg.declare
119 // Visibility doesn't matter on COFF, but whatever. We could add an ELF test
121 // WIN64-LABEL: define protected void @"?f@C@Test4@@UEAAXXZ"(
122 // WIN64-LABEL: define linkonce_odr protected void @"?f@C@Test4@@W7EAAXXZ"(
125 // Check that the thunk gets internal linkage.
144 // Force C::f to be used.
150 // Not sure why this isn't delayed like in Itanium.
151 // WIN64-LABEL: define internal void @"?f@C@?A{{.*}}@Test4B@@UEAAXXZ"(
155 // Check that the thunk for 'B::f' gets the same linkage as the function itself.
160 struct B
: virtual A
{
167 // No thunk in MS ABI in this case.
174 X
&operator=(const X
&);
191 virtual X
f() { return X(); }
197 virtual X
f() { return X(); }
200 struct Thunks
: Base1
, Base2
{
206 // CHECK-LABEL: define{{.*}} void @_ZThn16_N5Test66Thunks1fEv
207 // CHECK-DBG-NOT: dbg.declare
209 // CHECK: {{call void @_ZN5Test66Thunks1fEv.*sret(.+) align 1}}
211 X
Thunks::f() { return X(); }
213 // WIN64-LABEL: define linkonce_odr dso_local void @"?f@Thunks@Test6@@WBA@EAA?AUX@2@XZ"({{.*}} sret({{.*}}) align 1 %{{.*}})
215 // WIN64: tail call void @"?f@Thunks@Test6@@UEAA?AUX@2@XZ"({{.*}} sret({{.*}}) align 1 %{{.*}})
223 X
&operator=(const X
&);
227 struct Small
{ short s
; };
234 virtual void foo() = 0;
239 virtual void bar() = 0;
244 virtual void baz(X
, X
&, _Complex
float, Small
, Small
&, Large
) = 0;
252 void baz(X
, X
&, _Complex
float, Small
, Small
&, Large
);
255 void D::baz(X
, X
&, _Complex
float, Small
, Small
&, Large
) { }
257 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test71D3bazENS_1XERS1_CfNS_5SmallERS4_NS_5LargeE(
258 // CHECK-DBG-NOT: dbg.declare
261 void testD() { D d
; }
263 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.
267 struct NonPOD
{ ~NonPOD(); int x
, y
, z
; };
268 struct A
{ virtual void foo(); };
269 struct B
{ virtual void bar(NonPOD
); };
270 struct C
: A
, B
{ virtual void bar(NonPOD
); static void helper(NonPOD
); };
272 // CHECK: define{{.*}} void @_ZN5Test81C6helperENS_6NonPODE(ptr
273 void C::helper(NonPOD var
) {}
275 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test81C3barENS_6NonPODE(
276 // CHECK-DBG-NOT: dbg.declare
277 // CHECK-NOT: load [[NONPODTYPE:%.*]], ptr
280 void C::bar(NonPOD var
) {}
282 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.
285 // PR7241: Emitting thunks for a method shouldn't require the vtable for
286 // that class to be emitted.
288 struct A
{ virtual ~A() { } };
289 struct B
: A
{ virtual void test() const {} };
290 struct C
: B
{ C(); ~C(); };
291 struct D
: C
{ D() {} };
298 struct A
{ virtual void foo(); };
299 struct B
{ virtual void foo(); };
300 struct C
: A
, B
{ void foo() {} };
310 struct A
{ virtual A
* f(); };
311 struct B
: virtual A
{ virtual A
* f(); };
312 struct C
: B
{ virtual C
* f(); };
313 C
* C::f() { return 0; }
316 // CHECK: define {{.*}} @_ZN6Test111C1fEv(
318 // The this-adjustment and return-adjustment thunk required when
319 // C::f appears in a vtable where A is at a nonzero offset from C.
320 // CHECK: define {{.*}} @_ZTcv0_n24_v0_n32_N6Test111C1fEv(
321 // CHECK-DBG-NOT: dbg.declare
324 // The return-adjustment thunk required when C::f appears in a vtable
325 // where A is at a zero offset from C.
326 // CHECK: define {{.*}} @_ZTch0_v0_n32_N6Test111C1fEv(
327 // CHECK-DBG-NOT: dbg.declare
330 // WIN64-LABEL: define dso_local noundef ptr @"?f@C@Test11@@UEAAPEAU12@XZ"(ptr
332 // WIN64-LABEL: define weak_odr dso_local noundef ptr @"?f@C@Test11@@QEAAPEAUA@2@XZ"(ptr
333 // WIN64: call noundef ptr @"?f@C@Test11@@UEAAPEAU12@XZ"(ptr noundef %{{.*}})
335 // Match the vbtable return adjustment.
336 // WIN64: load ptr, ptr %{{[^,]*}}, align 8
337 // WIN64: getelementptr inbounds i32, ptr %{{[^,]*}}, i32 1
338 // WIN64: load i32, ptr %{{[^,]*}}, align 4
341 // Varargs thunk test.
344 virtual A
* f(int x
, ...);
347 virtual B
* f(int x
, ...);
351 virtual C
* f(int x
, ...);
354 C
* C::f(int x
, ...) { return makeC(); }
357 // CHECK: define {{.*}} @_ZN6Test121C1fEiz
359 // Varargs thunk; check that both the this and covariant adjustments
361 // CHECK: define {{.*}} @_ZTchn8_h8_N6Test121C1fEiz
362 // CHECK-DBG-NOT: dbg.declare
363 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -8
364 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 8
366 // The vtable layout goes:
368 // - f impl, no adjustment
370 // - f thunk 2, covariant, clone
371 // - f thunk 2, musttail this adjust to impl
372 // FIXME: The weak_odr linkage is probably not necessary and just an artifact
373 // of Itanium ABI details.
374 // WIN64-LABEL: define dso_local {{.*}} @"?f@C@Test12@@UEAAPEAU12@HZZ"(
375 // WIN64: call noundef ptr @"?makeC@Test12@@YAPEAUC@1@XZ"()
377 // This thunk needs return adjustment, clone.
378 // WIN64-LABEL: define weak_odr dso_local {{.*}} @"?f@C@Test12@@W7EAAPEAUB@2@HZZ"(
379 // WIN64: call noundef ptr @"?makeC@Test12@@YAPEAUC@1@XZ"()
380 // WIN64: getelementptr inbounds i8, ptr %{{.*}}, i32 8
382 // Musttail call back to the A implementation after this adjustment from B to A.
383 // WIN64-LABEL: define linkonce_odr dso_local noundef ptr @"?f@C@Test12@@W7EAAPEAU12@HZZ"(
384 // WIN64: getelementptr i8, ptr %{{[^,]*}}, i32 -8
385 // WIN64: musttail call {{.*}} @"?f@C@Test12@@UEAAPEAU12@HZZ"(
397 struct Proxy1
: Pad1
, B1
{
400 struct D
: virtual Proxy1
{
407 // CHECK: define {{.*}} @_ZTcvn8_n32_v8_n24_N6Test131D4foo1Ev
408 // CHECK-DBG-NOT: dbg.declare
409 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -8
410 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -32
411 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -24
412 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 8
415 // WIN64-LABEL: define weak_odr dso_local noundef ptr @"?foo1@D@Test13@@$4PPPPPPPE@A@EAAAEAUB1@2@XZ"(
417 // WIN64: getelementptr inbounds i8, ptr {{.*}}, i64 -12
418 // Call implementation.
419 // WIN64: call {{.*}} @"?foo1@D@Test13@@UEAAAEAU12@XZ"(ptr {{.*}})
420 // Virtual + nonvirtual return adjustment.
421 // WIN64: load ptr, ptr %{{[^,]*}}, align 8
422 // WIN64: getelementptr inbounds i32, ptr %{{[^,]*}}, i32 1
423 // WIN64: load i32, ptr %{{[^,]*}}, align 4
424 // WIN64: getelementptr inbounds i8, ptr %{{[^,]*}}, i32 %{{[^,]*}}
434 class C
: public A
, public B
{
439 // CHECK: define{{.*}} void @_ZThn8_N6Test141C1fEv({{.*}}) unnamed_addr [[NUW:#[0-9]+]]
440 // CHECK-DBG-NOT: dbg.declare
444 // Varargs non-covariant thunk test.
451 virtual void f(int x
, ...);
455 virtual void f(int x
, ...);
460 // CHECK-CLONE: declare void @_ZN6Test151C1fEiz
461 // non-virtual thunk to C::f
462 // CHECK-CLONE: declare void @_ZThn8_N6Test151C1fEiz
464 // If we have musttail, then we emit the thunk as available_externally.
465 // CHECK-TAIL: declare void @_ZN6Test151C1fEiz
466 // CHECK-TAIL: define available_externally void @_ZThn8_N6Test151C1fEiz({{.*}})
467 // CHECK-TAIL: musttail call void (ptr, i32, ...) @_ZN6Test151C1fEiz({{.*}}, ...)
469 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.
479 struct C
: public A
, public B
{
482 struct D
: public C
{
486 // CHECK: define linkonce_odr void @_ZThn8_N6Test161C3fooEv({{.*}}) {{.*}} comdat
487 // CHECK-DBG-NOT: dbg.declare
493 virtual void f(const char *, ...);
496 virtual void f(const char *, ...);
499 virtual void anchor();
500 void f(const char *, ...) override
;
502 // Key method and object anchor vtable for Itanium and MSVC.
506 // CHECK-CLONE-LABEL: declare void @_ZThn8_N6Test171C1fEPKcz(
508 // CHECK-TAIL-LABEL: define available_externally void @_ZThn8_N6Test171C1fEPKcz(
509 // CHECK-TAIL: getelementptr inbounds i8, ptr %{{.*}}, i64 -8
510 // CHECK-TAIL: musttail call {{.*}} @_ZN6Test171C1fEPKcz({{.*}}, ...)
512 // MSVC-LABEL: define linkonce_odr dso_local void @"?f@C@Test17@@G7EAAXPEBDZZ"
513 // MSVC-SAME: (ptr %this, ptr %[[ARG:[^,]+]], ...)
514 // MSVC: getelementptr i8, ptr %{{.*}}, i32 -8
515 // MSVC: musttail call void (ptr, ptr, ...) @"?f@C@Test17@@EEAAXPEBDZZ"(ptr %{{.*}}, ptr noundef %[[ARG]], ...)
518 /**** The following has to go at the end of the file ****/
520 // checking without opt
521 // CHECK-NONOPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(
522 // CHECK-NONOPT-NOT: comdat
524 // This is from Test5:
525 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv
527 // This is from Test10:
528 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv
529 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv
532 // CHECK-OPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(ptr noundef %this) unnamed_addr #1 align 2
534 // This is from Test5:
535 // CHECK-OPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv
537 // This is from Test10:
538 // CHECK-OPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv
539 // CHECK-OPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv
541 // This is from Test10:
542 // WIN64-LABEL: define linkonce_odr dso_local void @"?foo@C@Test10@@UEAAXXZ"(
543 // WIN64-LABEL: define linkonce_odr dso_local void @"?foo@C@Test10@@W7EAAXXZ"(
545 // CHECK-NONOPT: attributes [[NUW]] = { noinline nounwind optnone uwtable{{.*}} }
546 // CHECK-OPT: attributes [[NUW]] = { nounwind uwtable{{.*}} }