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 9 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 13 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 17 18 19 namespace Test1 { 20 21 // Check that we emit a non-virtual thunk for C::f. 22 23 struct A { 24 virtual void f(); 25 }; 26 27 struct B { 28 virtual void f(); 29 }; 30 31 struct 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.declare 39 // CHECK: ret void 40 // 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, i8* {{.*}}, i32 -8 44 // WIN64: ret void 45 void C::f() { } 46 47 } 48 49 namespace Test2 { 50 51 // Check that we emit a thunk for B::f since it's overriding a virtual base. 52 53 struct A { 54 virtual void f(); 55 }; 56 57 struct 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.declare 64 // CHECK: ret void 65 void 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@Test2 70 71 } 72 73 namespace Test3 { 74 75 // Check that we emit a covariant thunk for B::f. 76 77 struct V1 { }; 78 struct V2 : virtual V1 { }; 79 80 struct A { 81 virtual V1 *f(); 82 }; 83 84 struct B : A { 85 virtual void b(); 86 87 virtual V2 *f(); 88 }; 89 90 // CHECK: define{{.*}} %{{.*}}* @_ZTch0_v0_n24_N5Test31B1fEv( 91 // WIN64: define weak_odr dso_local %{{.*}} @"?f@B@Test3@@QEAAPEAUV1@2@XZ"( 92 V2 *B::f() { return 0; } 93 94 } 95 96 namespace Test4 { 97 98 // Check that the thunk for 'C::f' has the same visibility as the function itself. 99 100 struct A { 101 virtual void f(); 102 }; 103 104 struct B { 105 virtual void f(); 106 }; 107 108 struct __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.declare 116 // CHECK: ret void 117 void C::f() { } 118 119 // Visibility doesn't matter on COFF, but whatever. We could add an ELF test 120 // 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. 126 namespace 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 153 namespace Test5 { 154 155 // Check that the thunk for 'B::f' gets the same linkage as the function itself. 156 struct A { 157 virtual void f(); 158 }; 159 160 struct B : virtual A { 161 virtual void f() { } 162 }; 163 164 void f(B b) { 165 b.f(); 166 } 167 // No thunk in MS ABI in this case. 168 } 169 170 namespace 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_N5Test66Thunks1fEv 207 // CHECK-DBG-NOT: dbg.declare 208 // CHECK-NOT: memcpy 209 // CHECK: {{call void @_ZN5Test66Thunks1fEv.*sret(.+) align 1}} 210 // CHECK: ret void 211 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: memcpy 215 // WIN64: tail call void @"?f@Thunks@Test6@@UEAA?AUX@2@XZ"({{.*}} sret({{.*}}) align 1 %{{.*}}) 216 } 217 218 namespace Test7 { 219 // PR7188 220 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.declare 259 // CHECK-NOT: memcpy 260 // CHECK: ret void 261 void testD() { D d; } 262 263 // MS C++ ABI doesn't use a thunk, so this case isn't interesting. 264 } 265 266 namespace 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([[NONPODTYPE:%.*]]* 273 void C::helper(NonPOD var) {} 274 275 // CHECK-LABEL: define{{.*}} void @_ZThn8_N5Test81C3barENS_6NonPODE( 276 // CHECK-DBG-NOT: dbg.declare 277 // CHECK-NOT: load [[NONPODTYPE]], [[NONPODTYPE]]* 278 // CHECK-NOT: memcpy 279 // CHECK: ret void 280 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 for 286 // that class to be emitted. 287 namespace 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 297 namespace 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 // PR7611 309 namespace 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 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 322 // CHECK: ret 323 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 328 // CHECK: ret 329 330 // WIN64-LABEL: define dso_local %{{.*}}* @"?f@C@Test11@@UEAAPEAU12@XZ"(i8* 331 332 // WIN64-LABEL: define weak_odr dso_local %{{.*}}* @"?f@C@Test11@@QEAAPEAUA@2@XZ"(i8* 333 // WIN64: call %{{.*}}* @"?f@C@Test11@@UEAAPEAU12@XZ"(i8* %{{.*}}) 334 // 335 // Match the vbtable return adjustment. 336 // WIN64: load i32*, i32** %{{[^,]*}}, align 8 337 // WIN64: getelementptr inbounds i32, i32* %{{[^,]*}}, i32 1 338 // WIN64: load i32, i32* %{{[^,]*}}, align 4 339 } 340 341 // Varargs thunk test. 342 namespace 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::f 357 // CHECK: define {{.*}} @_ZN6Test121C1fEiz 358 359 // Varargs thunk; check that both the this and covariant adjustments 360 // are generated. 361 // CHECK: define {{.*}} @_ZTchn8_h8_N6Test121C1fEiz 362 // CHECK-DBG-NOT: dbg.declare 363 // CHECK: getelementptr inbounds i8, i8* {{.*}}, i64 -8 364 // CHECK: getelementptr inbounds i8, i8* {{.*}}, i64 8 365 366 // The vtable layout goes: 367 // C vtable in A: 368 // - f impl, no adjustment 369 // C vtable in B: 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 %{{.*}}* @"?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 %{{.*}}* @"?makeC@Test12@@YAPEAUC@1@XZ"() 380 // WIN64: getelementptr inbounds i8, i8* %{{.*}}, i32 8 381 // 382 // Musttail call back to the A implementation after this adjustment from B to A. 383 // WIN64-LABEL: define linkonce_odr dso_local %{{.*}}* @"?f@C@Test12@@W7EAAPEAU12@HZZ"( 384 // WIN64: getelementptr i8, i8* %{{[^,]*}}, i32 -8 385 // WIN64: musttail call {{.*}} @"?f@C@Test12@@UEAAPEAU12@HZZ"( 386 C c; 387 } 388 389 // PR13832 390 namespace 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_N6Test131D4foo1Ev 408 // CHECK-DBG-NOT: dbg.declare 409 // CHECK: getelementptr inbounds i8, i8* {{.*}}, i64 -8 410 // CHECK: getelementptr inbounds i8, i8* {{.*}}, i64 -32 411 // CHECK: getelementptr inbounds i8, i8* {{.*}}, i64 -24 412 // CHECK: getelementptr inbounds i8, i8* {{.*}}, i64 8 413 // CHECK: ret %"struct.Test13::D"* 414 415 // WIN64-LABEL: define weak_odr dso_local %"struct.Test13::D"* @"?foo1@D@Test13@@$4PPPPPPPE@A@EAAAEAUB1@2@XZ"( 416 // This adjustment. 417 // WIN64: getelementptr inbounds i8, i8* {{.*}}, i64 -12 418 // Call implementation. 419 // WIN64: call {{.*}} @"?foo1@D@Test13@@UEAAAEAU12@XZ"(i8* {{.*}}) 420 // Virtual + nonvirtual return adjustment. 421 // WIN64: load i32*, i32** %{{[^,]*}}, align 8 422 // WIN64: getelementptr inbounds i32, i32* %{{[^,]*}}, i32 1 423 // WIN64: load i32, i32* %{{[^,]*}}, align 4 424 // WIN64: getelementptr inbounds i8, i8* %{{[^,]*}}, i32 %{{[^,]*}} 425 } 426 427 namespace 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.declare 441 // CHECK: ret void 442 } 443 444 // Varargs non-covariant thunk test. 445 // PR18098 446 namespace 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::c 460 // CHECK-CLONE: declare void @_ZN6Test151C1fEiz 461 // non-virtual thunk to C::f 462 // CHECK-CLONE: declare void @_ZThn8_N6Test151C1fEiz 463 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 (%"struct.Test15::C"*, i32, ...) @_ZN6Test151C1fEiz({{.*}}, ...) 468 469 // MS C++ ABI doesn't use a thunk, so this case isn't interesting. 470 } 471 472 namespace Test16 { 473 struct A { 474 virtual ~A(); 475 }; 476 struct B { 477 virtual void foo(); 478 }; 479 struct C : public A, public B { 480 void foo() {} 481 }; 482 struct D : public C { 483 ~D(); 484 }; 485 D::~D() {} 486 // CHECK: define linkonce_odr void @_ZThn8_N6Test161C3fooEv({{.*}}) {{.*}} comdat 487 // CHECK-DBG-NOT: dbg.declare 488 // CHECK: ret void 489 } 490 491 namespace Test17 { 492 class A { 493 virtual void f(const char *, ...); 494 }; 495 class B { 496 virtual void f(const char *, ...); 497 }; 498 class 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. 503 void C::anchor() {} 504 C 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, i8* %{{.*}}, i64 -8 510 // CHECK-TAIL: musttail call {{.*}} @_ZN6Test171C1fEPKcz({{.*}}, ...) 511 512 // MSVC-LABEL: define linkonce_odr dso_local void @"?f@C@Test17@@G7EAAXPEBDZZ" 513 // MSVC-SAME: (%"class.Test17::C"* %this, i8* %[[ARG:[^,]+]], ...) 514 // MSVC: getelementptr i8, i8* %{{.*}}, i32 -8 515 // MSVC: musttail call void (%"class.Test17::C"*, i8*, ...) @"?f@C@Test17@@EEAAXPEBDZZ"(%"class.Test17::C"* %{{.*}}, i8* %[[ARG]], ...) 516 } 517 518 /**** The following has to go at the end of the file ****/ 519 520 // checking without opt 521 // CHECK-NONOPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv( 522 // CHECK-NONOPT-NOT: comdat 523 524 // This is from Test5: 525 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv 526 527 // This is from Test10: 528 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv 529 // CHECK-NONOPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv 530 531 // Checking with opt 532 // CHECK-OPT-LABEL: define internal void @_ZThn8_N6Test4B12_GLOBAL__N_11C1fEv(%"struct.Test4B::(anonymous namespace)::C"* %this) unnamed_addr #1 align 2 533 534 // This is from Test5: 535 // CHECK-OPT-LABEL: define linkonce_odr void @_ZTv0_n24_N5Test51B1fEv 536 537 // This is from Test10: 538 // CHECK-OPT-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv 539 // CHECK-OPT-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv 540 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