547 lines · cpp
1// Sparc64 doesn't support musttail (yet), so it uses method cloning for2// 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 %s5// 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 %s7// 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 %s9 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 %s13 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 %s17 18 19namespace Test1 {20 21// Check that we emit a non-virtual thunk for C::f.22 23struct A {24 virtual void f();25};26 27struct B {28 virtual void f();29};30 31struct C : A, B {32 virtual void c();33 34 virtual void f();35};36 37// CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test11C1fEv(38// CHECK-DBG-NOT: dbg.declare39// CHECK: ret void40//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 -844// WIN64: ret void45void C::f() { }46 47}48 49namespace Test2 {50 51// Check that we emit a thunk for B::f since it's overriding a virtual base.52 53struct A {54 virtual void f();55};56 57struct B : virtual A {58 virtual void b();59 virtual void f();60};61 62// CHECK-LABEL: define{{.*}} void @_ZTv0_n24_N5Test21B1fEv(63// CHECK-DBG-NOT: dbg.declare64// CHECK: ret void65void B::f() { }66 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@Test270 71}72 73namespace Test3 {74 75// Check that we emit a covariant thunk for B::f.76 77struct V1 { };78struct V2 : virtual V1 { };79 80struct A {81 virtual V1 *f();82};83 84struct B : A {85 virtual void b();86 87 virtual V2 *f();88};89 90// CHECK: define{{.*}} ptr @_ZTch0_v0_n24_N5Test31B1fEv(91// WIN64: define weak_odr dso_local noundef ptr @"?f@B@Test3@@QEAAPEAUV1@2@XZ"(92V2 *B::f() { return 0; }93 94}95 96namespace Test4 {97 98// Check that the thunk for 'C::f' has the same visibility as the function itself.99 100struct A {101 virtual void f();102};103 104struct B {105 virtual void f();106};107 108struct __attribute__((visibility("protected"))) C : A, B {109 virtual void c();110 111 virtual void f();112};113 114// CHECK-LABEL: define protected void @_ZThn8_N5Test41C1fEv(115// CHECK-DBG-NOT: dbg.declare116// CHECK: ret void117void C::f() { }118 119// Visibility doesn't matter on COFF, but whatever. We could add an ELF test120// mode later.121// WIN64-LABEL: define protected void @"?f@C@Test4@@UEAAXXZ"(122// WIN64-LABEL: define linkonce_odr protected void @"?f@C@Test4@@W7EAAXXZ"(123}124 125// Check that the thunk gets internal linkage.126namespace Test4B {127 struct A {128 virtual void f();129 };130 131 struct B {132 virtual void f();133 };134 135 namespace {136 struct C : A, B {137 virtual void c();138 virtual void f();139 };140 }141 void C::c() {}142 void C::f() {}143 144 // Force C::f to be used.145 void f() {146 C c;147 c.f();148 }149}150// Not sure why this isn't delayed like in Itanium.151// WIN64-LABEL: define internal void @"?f@C@?A{{.*}}@Test4B@@UEAAXXZ"(152 153namespace Test5 {154 155// Check that the thunk for 'B::f' gets the same linkage as the function itself.156struct A {157 virtual void f();158};159 160struct B : virtual A {161 virtual void f() { }162};163 164void f(B b) {165 b.f();166}167// No thunk in MS ABI in this case.168}169 170namespace Test6 {171 struct X {172 X();173 X(const X&);174 X &operator=(const X&);175 ~X();176 };177 178 struct P {179 P();180 P(const P&);181 ~P();182 X first;183 X second;184 };185 186 P getP();187 188 struct Base1 {189 int i;190 191 virtual X f() { return X(); }192 };193 194 struct Base2 {195 float real;196 197 virtual X f() { return X(); }198 };199 200 struct Thunks : Base1, Base2 {201 long l;202 203 virtual X f();204 };205 206 // CHECK-LABEL: define{{.*}} void @_ZThn16_N5Test66Thunks1fEv207 // CHECK-DBG-NOT: dbg.declare208 // CHECK-NOT: memcpy209 // CHECK: {{call void @_ZN5Test66Thunks1fEv.*sret(.+) align 1}}210 // CHECK: ret void211 X Thunks::f() { return X(); }212 213 // WIN64-LABEL: define linkonce_odr dso_local void @"?f@Thunks@Test6@@WBA@EAA?AUX@2@XZ"({{.*}} sret({{.*}}) align 1 %{{.*}})214 // WIN64-NOT: memcpy215 // WIN64: tail call void @"?f@Thunks@Test6@@UEAA?AUX@2@XZ"({{.*}} sret({{.*}}) align 1 %{{.*}})216}217 218namespace Test7 {219 // PR7188220 struct X {221 X();222 X(const X&);223 X &operator=(const X&);224 ~X();225 };226 227 struct Small { short s; };228 struct Large {229 char array[1024];230 };231 232 class A {233 protected:234 virtual void foo() = 0;235 };236 237 class B : public A {238 protected:239 virtual void bar() = 0;240 };241 242 class C : public A {243 protected:244 virtual void baz(X, X&, _Complex float, Small, Small&, Large) = 0;245 };246 247 class D : public B,248 public C {249 250 void foo() {}251 void bar() {}252 void baz(X, X&, _Complex float, Small, Small&, Large);253 };254 255 void D::baz(X, X&, _Complex float, Small, Small&, Large) { }256 257 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test71D3bazENS_1XERS1_CfNS_5SmallERS4_NS_5LargeE(258 // CHECK-DBG-NOT: dbg.declare259 // CHECK-NOT: memcpy260 // CHECK: ret void261 void testD() { D d; }262 263 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.264}265 266namespace Test8 {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); };271 272 // CHECK: define{{.*}} void @_ZN5Test81C6helperENS_6NonPODE(ptr273 void C::helper(NonPOD var) {}274 275 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test81C3barENS_6NonPODE(276 // CHECK-DBG-NOT: dbg.declare277 // CHECK-NOT: load [[NONPODTYPE:%.*]], ptr278 // CHECK-NOT: memcpy279 // CHECK: ret void280 void C::bar(NonPOD var) {}281 282 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.283}284 285// PR7241: Emitting thunks for a method shouldn't require the vtable for286// that class to be emitted.287namespace Test9 {288 struct A { virtual ~A() { } };289 struct B : A { virtual void test() const {} };290 struct C : B { C(); ~C(); };291 struct D : C { D() {} };292 void test() {293 D d;294 }295}296 297namespace Test10 {298 struct A { virtual void foo(); };299 struct B { virtual void foo(); };300 struct C : A, B { void foo() {} };301 302 // Test later.303 void test() {304 C c;305 }306}307 308// PR7611309namespace Test11 {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; }314 315 // C::f itself.316 // CHECK: define {{.*}} @_ZN6Test111C1fEv(317 318 // The this-adjustment and return-adjustment thunk required when319 // 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.declare322 // CHECK: ret323 324 // The return-adjustment thunk required when C::f appears in a vtable325 // where A is at a zero offset from C.326 // CHECK: define {{.*}} @_ZTch0_v0_n32_N6Test111C1fEv(327 // CHECK-DBG-NOT: dbg.declare328 // CHECK: ret329 330 // WIN64-LABEL: define dso_local noundef ptr @"?f@C@Test11@@UEAAPEAU12@XZ"(ptr331 332 // WIN64-LABEL: define weak_odr dso_local noundef ptr @"?f@C@Test11@@QEAAPEAUA@2@XZ"(ptr333 // WIN64: call noundef ptr @"?f@C@Test11@@UEAAPEAU12@XZ"(ptr noundef %{{.*}})334 //335 // Match the vbtable return adjustment.336 // WIN64: load ptr, ptr %{{[^,]*}}, align 8337 // WIN64: getelementptr inbounds i32, ptr %{{[^,]*}}, i32 1338 // WIN64: load i32, ptr %{{[^,]*}}, align 4339}340 341// Varargs thunk test.342namespace Test12 {343 struct A {344 virtual A* f(int x, ...);345 };346 struct B {347 virtual B* f(int x, ...);348 };349 struct C : A, B {350 virtual void c();351 virtual C* f(int x, ...);352 };353 C* makeC();354 C* C::f(int x, ...) { return makeC(); }355 356 // C::f357 // CHECK: define {{.*}} @_ZN6Test121C1fEiz358 359 // Varargs thunk; check that both the this and covariant adjustments360 // are generated.361 // CHECK: define {{.*}} @_ZTchn8_h8_N6Test121C1fEiz362 // CHECK-DBG-NOT: dbg.declare363 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -8364 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 8365 366 // The vtable layout goes:367 // C vtable in A:368 // - f impl, no adjustment369 // C vtable in B:370 // - f thunk 2, covariant, clone371 // - f thunk 2, musttail this adjust to impl372 // FIXME: The weak_odr linkage is probably not necessary and just an artifact373 // 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"()376 //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 8381 //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 -8385 // WIN64: musttail call {{.*}} @"?f@C@Test12@@UEAAPEAU12@HZZ"(386 C c;387}388 389// PR13832390namespace Test13 {391 struct B1 {392 virtual B1 &foo1();393 };394 struct Pad1 {395 virtual ~Pad1();396 };397 struct Proxy1 : Pad1, B1 {398 virtual ~Proxy1();399 };400 struct D : virtual Proxy1 {401 virtual ~D();402 virtual D &foo1();403 };404 D& D::foo1() {405 return *this;406 }407 // CHECK: define {{.*}} @_ZTcvn8_n32_v8_n24_N6Test131D4foo1Ev408 // CHECK-DBG-NOT: dbg.declare409 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -8410 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -32411 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 -24412 // CHECK: getelementptr inbounds i8, ptr {{.*}}, i64 8413 // CHECK: ret ptr414 415 // WIN64-LABEL: define weak_odr dso_local noundef ptr @"?foo1@D@Test13@@$4PPPPPPPE@A@EAAAEAUB1@2@XZ"(416 // This adjustment.417 // WIN64: getelementptr inbounds i8, ptr {{.*}}, i64 -12418 // Call implementation.419 // WIN64: call {{.*}} @"?foo1@D@Test13@@UEAAAEAU12@XZ"(ptr {{.*}})420 // Virtual + nonvirtual return adjustment.421 // WIN64: load ptr, ptr %{{[^,]*}}, align 8422 // WIN64: getelementptr inbounds i32, ptr %{{[^,]*}}, i32 1423 // WIN64: load i32, ptr %{{[^,]*}}, align 4424 // WIN64: getelementptr inbounds i8, ptr %{{[^,]*}}, i32 %{{[^,]*}}425}426 427namespace Test14 {428 class A {429 virtual void f();430 };431 class B {432 virtual void f();433 };434 class C : public A, public B {435 virtual void f();436 };437 void C::f() {438 }439 // CHECK: define{{.*}} void @_ZThn8_N6Test141C1fEv({{.*}}) unnamed_addr [[NUW:#[0-9]+]]440 // CHECK-DBG-NOT: dbg.declare441 // CHECK: ret void442}443 444// Varargs non-covariant thunk test.445// PR18098446namespace Test15 {447 struct A {448 virtual ~A();449 };450 struct B {451 virtual void f(int x, ...);452 };453 struct C : A, B {454 virtual void c();455 virtual void f(int x, ...);456 };457 void C::c() {}458 459 // C::c460 // CHECK-CLONE: declare void @_ZN6Test151C1fEiz461 // non-virtual thunk to C::f462 // CHECK-CLONE: declare void @_ZThn8_N6Test151C1fEiz463 464 // If we have musttail, then we emit the thunk as available_externally.465 // CHECK-TAIL: declare void @_ZN6Test151C1fEiz466 // CHECK-TAIL: define available_externally void @_ZThn8_N6Test151C1fEiz({{.*}})467 // CHECK-TAIL: musttail call void (ptr, i32, ...) @_ZN6Test151C1fEiz({{.*}}, ...)468 469 // MS C++ ABI doesn't use a thunk, so this case isn't interesting.470}471 472namespace Test16 {473struct A {474 virtual ~A();475};476struct B {477 virtual void foo();478};479struct C : public A, public B {480 void foo() {}481};482struct D : public C {483 ~D();484};485D::~D() {}486// CHECK: define linkonce_odr void @_ZThn8_N6Test161C3fooEv({{.*}}) {{.*}} comdat487// CHECK-DBG-NOT: dbg.declare488// CHECK: ret void489}490 491namespace Test17 {492class A {493 virtual void f(const char *, ...);494};495class B {496 virtual void f(const char *, ...);497};498class C : A, B {499 virtual void anchor();500 void f(const char *, ...) override;501};502// Key method and object anchor vtable for Itanium and MSVC.503void C::anchor() {}504C c;505 506// CHECK-CLONE-LABEL: declare void @_ZThn8_N6Test171C1fEPKcz(507 508// CHECK-TAIL-LABEL: define available_externally void @_ZThn8_N6Test171C1fEPKcz(509// CHECK-TAIL: getelementptr inbounds i8, ptr %{{.*}}, i64 -8510// CHECK-TAIL: musttail call {{.*}} @_ZN6Test171C1fEPKcz({{.*}}, ...)511 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 -8515// MSVC: musttail call void (ptr, ptr, ...) @"?f@C@Test17@@EEAAXPEBDZZ"(ptr %{{.*}}, ptr noundef %[[ARG]], ...)516}517 518/**** The following has to go at the end of the file ****/519 520// checking without opt521// CHECK-NONOPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(522// CHECK-NONOPT-NOT: comdat523 524// This is from Test5:525// CHECK-NONOPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv526 527// This is from Test10:528// CHECK-NONOPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv529// CHECK-NONOPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv530 531// Checking with opt532// CHECK-OPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(ptr noundef %this) unnamed_addr #1 align 2533 534// This is from Test5:535// CHECK-OPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv536 537// This is from Test10:538// CHECK-OPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv539// CHECK-OPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv540 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"(544 545// CHECK-NONOPT: attributes [[NUW]] = { noinline nounwind optnone uwtable{{.*}} }546// CHECK-OPT: attributes [[NUW]] = { nounwind uwtable{{.*}} }547