1// RUN: %clang_cc1 %s -fno-rtti -triple=i386-pc-win32 -emit-llvm -o %t 2// RUN: FileCheck %s < %t 3// RUN: FileCheck --check-prefix=CHECK2 %s < %t 4 5// For now, just make sure x86_64 doesn't crash. 6// RUN: %clang_cc1 %s -fno-rtti -triple=x86_64-pc-win32 -emit-llvm -o %t 7 8struct VBase { 9 virtual ~VBase(); 10 virtual void foo(); 11 virtual void bar(); 12 int field; 13}; 14 15struct B : virtual VBase { 16 B(); 17 virtual ~B(); 18 virtual void foo(); 19 virtual void bar(); 20}; 21 22B::B() { 23 // CHECK-LABEL: define x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ" 24 // CHECK: %[[THIS:.*]] = load %struct.B*, %struct.B** 25 // CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]] 26 27 // Don't check the INIT_VBASES case as it's covered by the ctor tests. 28 29 // CHECK: %[[SKIP_VBASES]] 30 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 31 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0 32 // ... 33 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 34 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %{{.*}} 35 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to i32 (...)*** 36 // CHECK: store i32 (...)** bitcast ([3 x i8*]* @"\01??_7B@@6B@" to i32 (...)**), i32 (...)*** %[[VFPTR]] 37 38 // Initialize vtorDisp: 39 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 40 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0 41 // ... 42 // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}} 43 // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8 44 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 45 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %[[VBASE_OFFSET]] 46 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8, i8* %[[VBASE_i8]], i32 -4 47 // CHECK: %[[VTORDISP_PTR:.*]] = bitcast i8* %[[VTORDISP_i8]] to i32* 48 // CHECK: store i32 %[[VTORDISP_VAL]], i32* %[[VTORDISP_PTR]] 49 50 // CHECK: ret 51} 52 53B::~B() { 54 // CHECK-LABEL: define x86_thiscallcc void @"\01??1B@@UAE@XZ" 55 // Adjust the this parameter: 56 // CHECK: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8* 57 // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_PARAM_i8]], i32 -8 58 // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B* 59 // CHECK: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR:.*]], align 4 60 // CHECK: %[[THIS:.*]] = load %struct.B*, %struct.B** %[[THIS_ADDR]] 61 62 // Restore the vfptr that could have been changed by a subclass. 63 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 64 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0 65 // ... 66 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 67 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %{{.*}} 68 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to i32 (...)*** 69 // CHECK: store i32 (...)** bitcast ([3 x i8*]* @"\01??_7B@@6B@" to i32 (...)**), i32 (...)*** %[[VFPTR]] 70 71 // Initialize vtorDisp: 72 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 73 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0 74 // ... 75 // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}} 76 // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8 77 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 78 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %[[VBASE_OFFSET]] 79 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8, i8* %[[VBASE_i8]], i32 -4 80 // CHECK: %[[VTORDISP_PTR:.*]] = bitcast i8* %[[VTORDISP_i8]] to i32* 81 // CHECK: store i32 %[[VTORDISP_VAL]], i32* %[[VTORDISP_PTR]] 82 83 foo(); // Avoid the "trivial destructor" optimization. 84 85 // CHECK: ret 86 87 // CHECK2-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* 88 // CHECK2: %[[THIS:.*]] = load %struct.B*, %struct.B** {{.*}} 89 // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 90 // CHECK2: %[[B_i8:.*]] = getelementptr i8, i8* %[[THIS_i8]], i32 8 91 // CHECK2: %[[B:.*]] = bitcast i8* %[[B_i8]] to %struct.B* 92 // CHECK2: call x86_thiscallcc void @"\01??1B@@UAE@XZ"(%struct.B* %[[B]]) 93 // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 94 // CHECK2: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 8 95 // CHECK2: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase* 96 // CHECK2: call x86_thiscallcc void @"\01??1VBase@@UAE@XZ"(%struct.VBase* %[[VBASE]]) 97 // CHECK2: ret 98 99 // CHECK2-LABEL: define linkonce_odr x86_thiscallcc i8* @"\01??_GB@@UAEPAXI@Z" 100 // CHECK2: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8* 101 // CHECK2: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_PARAM_i8:.*]], i32 -8 102 // CHECK2: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B* 103 // CHECK2: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR:.*]], align 4 104 // CHECK2: %[[THIS:.*]] = load %struct.B*, %struct.B** %[[THIS_ADDR]] 105 // CHECK2: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[THIS]]) 106 // ... 107 // CHECK2: ret 108} 109 110void B::foo() { 111// CHECK-LABEL: define x86_thiscallcc void @"\01?foo@B@@UAEXXZ"(i8* 112// 113// B::foo gets 'this' cast to VBase* in ECX (i.e. this+8) so we 114// need to adjust 'this' before use. 115// 116// CHECK: %[[THIS_ADDR:.*]] = alloca %struct.B*, align 4 117// CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[ECX:.*]], i32 -8 118// CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B* 119// CHECK: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR]], align 4 120 121 field = 42; 122// CHECK: %[[THIS:.*]] = load %struct.B*, %struct.B** %[[THIS_ADDR]] 123// CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 124// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS8]], i32 0 125// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32** 126// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]] 127// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1 128// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]] 129// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]] 130// CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8* 131// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS8]], i32 %[[VBOFFSET]] 132// CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase* 133// CHECK: %[[FIELD:.*]] = getelementptr inbounds %struct.VBase, %struct.VBase* %[[VBASE]], i32 0, i32 1 134// CHECK: store i32 42, i32* %[[FIELD]], align 4 135// 136// CHECK: ret void 137} 138 139void call_vbase_bar(B *obj) { 140// CHECK-LABEL: define void @"\01?call_vbase_bar@@YAXPAUB@@@Z"(%struct.B* %obj) 141// CHECK: %[[OBJ:.*]] = load %struct.B 142 143 obj->bar(); 144// When calling a vbase's virtual method, one needs to adjust 'this' 145// at the caller site. 146// 147// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8* 148// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0 149// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32** 150// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]] 151// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1 152// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]] 153// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]] 154// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]] 155// CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to void (i8*)*** 156// CHECK: %[[VFTABLE:.*]] = load void (i8*)**, void (i8*)*** %[[VFPTR]] 157// CHECK: %[[VFUN:.*]] = getelementptr inbounds void (i8*)*, void (i8*)** %[[VFTABLE]], i64 2 158// CHECK: %[[VFUN_VALUE:.*]] = load void (i8*)*, void (i8*)** %[[VFUN]] 159// 160// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8* 161// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0 162// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32** 163// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]] 164// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1 165// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]] 166// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]] 167// CHECK: %[[VBASE:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]] 168// 169// CHECK: call x86_thiscallcc void %[[VFUN_VALUE]](i8* %[[VBASE]]) 170// 171// CHECK: ret void 172} 173 174void delete_B(B *obj) { 175// CHECK-LABEL: define void @"\01?delete_B@@YAXPAUB@@@Z"(%struct.B* %obj) 176// CHECK: %[[OBJ:.*]] = load %struct.B 177 178 delete obj; 179// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8* 180// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0 181// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32** 182// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]] 183// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1 184// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]] 185// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]] 186// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]] 187// CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to i8* (%struct.B*, i32)*** 188// CHECK: %[[VFTABLE:.*]] = load i8* (%struct.B*, i32)**, i8* (%struct.B*, i32)*** %[[VFPTR]] 189// CHECK: %[[VFUN:.*]] = getelementptr inbounds i8* (%struct.B*, i32)*, i8* (%struct.B*, i32)** %[[VFTABLE]], i64 0 190// CHECK: %[[VFUN_VALUE:.*]] = load i8* (%struct.B*, i32)*, i8* (%struct.B*, i32)** %[[VFUN]] 191// 192// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8* 193// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0 194// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32** 195// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]] 196// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1 197// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]] 198// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]] 199// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]] 200// CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.B* 201// 202// CHECK: call x86_thiscallcc i8* %[[VFUN_VALUE]](%struct.B* %[[VBASE]], i32 1) 203// CHECK: ret void 204} 205 206void call_complete_dtor() { 207 // CHECK-LABEL: define void @"\01?call_complete_dtor@@YAXXZ" 208 B b; 209 // CHECK: call x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ"(%struct.B* %[[B:.*]], i32 1) 210 // CHECK-NOT: getelementptr 211 // CHECK: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[B]]) 212 // CHECK: ret 213} 214 215struct C : B { 216 C(); 217 // has an implicit vdtor. 218}; 219 220// Used to crash on an assertion. 221C::C() { 222// CHECK-LABEL: define x86_thiscallcc %struct.C* @"\01??0C@@QAE@XZ" 223} 224 225namespace multiple_vbases { 226struct A { 227 virtual void a(); 228}; 229 230struct B { 231 virtual void b(); 232}; 233 234struct C { 235 virtual void c(); 236}; 237 238struct D : virtual A, virtual B, virtual C { 239 virtual void a(); 240 virtual void b(); 241 virtual void c(); 242 D(); 243}; 244 245D::D() { 246 // CHECK-LABEL: define x86_thiscallcc %"struct.multiple_vbases::D"* @"\01??0D@multiple_vbases@@QAE@XZ" 247 // Just make sure we emit 3 vtordisps after initializing vfptrs. 248 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BA@1@@" to i32 (...)**), i32 (...)*** %{{.*}} 249 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BB@1@@" to i32 (...)**), i32 (...)*** %{{.*}} 250 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BC@1@@" to i32 (...)**), i32 (...)*** %{{.*}} 251 // ... 252 // CHECK: store i32 %{{.*}}, i32* %{{.*}} 253 // CHECK: store i32 %{{.*}}, i32* %{{.*}} 254 // CHECK: store i32 %{{.*}}, i32* %{{.*}} 255 // CHECK: ret 256} 257} 258 259namespace diamond { 260struct A { 261 A(); 262 virtual ~A(); 263}; 264 265struct B : virtual A { 266 B(); 267 ~B(); 268}; 269 270struct C : virtual A { 271 C(); 272 ~C(); 273 int c1, c2, c3; 274}; 275 276struct Z { 277 int z; 278}; 279 280struct D : virtual Z, B, C { 281 D(); 282 ~D(); 283} d; 284 285D::~D() { 286 // CHECK-LABEL: define x86_thiscallcc void @"\01??1D@diamond@@UAE@XZ"(%"struct.diamond::D"*) 287 // CHECK: %[[ARG_i8:.*]] = bitcast %"struct.diamond::D"* %{{.*}} to i8* 288 // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[ARG_i8]], i32 -24 289 // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %"struct.diamond::D"* 290 // CHECK: store %"struct.diamond::D"* %[[THIS]], %"struct.diamond::D"** %[[THIS_VAL:.*]], align 4 291 // CHECK: %[[THIS:.*]] = load %"struct.diamond::D"*, %"struct.diamond::D"** %[[THIS_VAL]] 292 // CHECK: %[[D_i8:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to i8* 293 // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8, i8* %[[D_i8]], i32 4 294 // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.diamond::C"* 295 // CHECK: %[[C_i8:.*]] = bitcast %"struct.diamond::C"* %[[C]] to i8* 296 // CHECK: %[[ARG_i8:.*]] = getelementptr i8, i8* %{{.*}}, i32 16 297 // FIXME: We might consider changing the dtor this parameter type to i8*. 298 // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::C"* 299 // CHECK: call x86_thiscallcc void @"\01??1C@diamond@@UAE@XZ"(%"struct.diamond::C"* %[[ARG]]) 300 301 // CHECK: %[[B:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to %"struct.diamond::B"* 302 // CHECK: %[[B_i8:.*]] = bitcast %"struct.diamond::B"* %[[B]] to i8* 303 // CHECK: %[[ARG_i8:.*]] = getelementptr i8, i8* %[[B_i8]], i32 4 304 // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::B"* 305 // CHECK: call x86_thiscallcc void @"\01??1B@diamond@@UAE@XZ"(%"struct.diamond::B"* %[[ARG]]) 306 // CHECK: ret void 307} 308 309} 310 311namespace test2 { 312struct A { A(); }; 313struct B : virtual A { B() {} }; 314struct C : B, A { C() {} }; 315 316// PR18435: Order mattered here. We were generating code for the delegating 317// call to B() from C(). 318void callC() { C x; } 319 320// CHECK-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::C"* @"\01??0C@test2@@QAE@XZ" 321// CHECK: (%"struct.test2::C"* returned %this, i32 %is_most_derived) 322// CHECK: br i1 323// Virtual bases 324// CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}}) 325// CHECK: br label 326// Non-virtual bases 327// CHECK: call x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ"(%"struct.test2::B"* %{{.*}}, i32 0) 328// CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}}) 329// CHECK: ret 330 331// CHECK2-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ" 332// CHECK2: (%"struct.test2::B"* returned %this, i32 %is_most_derived) 333// CHECK2: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}}) 334// CHECK2: ret 335 336} 337 338namespace test3 { 339// PR19104: A non-virtual call of a virtual method doesn't use vftable thunks, 340// so requires only static adjustment which is different to the one used 341// for virtual calls. 342struct A { 343 virtual void foo(); 344}; 345 346struct B : virtual A { 347 virtual void bar(); 348}; 349 350struct C : virtual A { 351 virtual void foo(); 352}; 353 354struct D : B, C { 355 virtual void bar(); 356 int field; // Laid out between C and A subobjects in D. 357}; 358 359void D::bar() { 360 // CHECK-LABEL: define x86_thiscallcc void @"\01?bar@D@test3@@UAEXXZ"(%"struct.test3::D"* %this) 361 362 C::foo(); 363 // Shouldn't need any vbtable lookups. All we have to do is adjust to C*, 364 // then compensate for the adjustment performed in the C::foo() prologue. 365 // CHECK-NOT: load i8*, i8** 366 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test3::D"* %{{.*}} to i8* 367 // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 8 368 // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.test3::C"* 369 // CHECK: %[[C_i8:.*]] = bitcast %"struct.test3::C"* %[[C]] to i8* 370 // CHECK: %[[ARG:.*]] = getelementptr i8, i8* %[[C_i8]], i32 4 371 // CHECK: call x86_thiscallcc void @"\01?foo@C@test3@@UAEXXZ"(i8* %[[ARG]]) 372 // CHECK: ret 373} 374} 375 376namespace test4{ 377// PR19172: We used to merge method vftable locations wrong. 378 379struct A { 380 virtual ~A() {} 381}; 382 383struct B { 384 virtual ~B() {} 385}; 386 387struct C : virtual A, B { 388 virtual ~C(); 389}; 390 391void foo(void*); 392 393C::~C() { 394 // CHECK-LABEL: define x86_thiscallcc void @"\01??1C@test4@@UAE@XZ"(%"struct.test4::C"* %this) 395 396 // In this case "this" points to the most derived class, so no GEPs needed. 397 // CHECK-NOT: getelementptr 398 // CHECK-NOT: bitcast 399 // CHECK: %[[VFPTR_i8:.*]] = bitcast %"struct.test4::C"* %{{.*}} to i32 (...)*** 400 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7C@test4@@6BB@1@@" to i32 (...)**), i32 (...)*** %[[VFPTR_i8]] 401 402 foo(this); 403 // CHECK: ret 404} 405 406void destroy(C *obj) { 407 // CHECK-LABEL: define void @"\01?destroy@test4@@YAXPAUC@1@@Z"(%"struct.test4::C"* %obj) 408 409 delete obj; 410 // CHECK: %[[VPTR:.*]] = bitcast %"struct.test4::C"* %[[OBJ:.*]] to i8* (%"struct.test4::C"*, i32)*** 411 // CHECK: %[[VFTABLE:.*]] = load i8* (%"struct.test4::C"*, i32)**, i8* (%"struct.test4::C"*, i32)*** %[[VPTR]] 412 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds i8* (%"struct.test4::C"*, i32)*, i8* (%"struct.test4::C"*, i32)** %[[VFTABLE]], i64 0 413 // CHECK: %[[VFUN:.*]] = load i8* (%"struct.test4::C"*, i32)*, i8* (%"struct.test4::C"*, i32)** %[[VFTENTRY]] 414 // CHECK: call x86_thiscallcc i8* %[[VFUN]](%"struct.test4::C"* %[[OBJ]], i32 1) 415 // CHECK: ret 416} 417 418struct D { 419 virtual void d(); 420}; 421 422// The first non-virtual base doesn't have a vdtor, 423// but "this adjustment" is not needed. 424struct E : D, B, virtual A { 425 virtual ~E(); 426}; 427 428E::~E() { 429 // CHECK-LABEL: define x86_thiscallcc void @"\01??1E@test4@@UAE@XZ"(%"struct.test4::E"* %this) 430 431 // In this case "this" points to the most derived class, so no GEPs needed. 432 // CHECK-NOT: getelementptr 433 // CHECK-NOT: bitcast 434 // CHECK: %[[VFPTR_i8:.*]] = bitcast %"struct.test4::E"* %{{.*}} to i32 (...)*** 435 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7E@test4@@6BD@1@@" to i32 (...)**), i32 (...)*** %[[VFPTR_i8]] 436 foo(this); 437} 438 439void destroy(E *obj) { 440 // CHECK-LABEL: define void @"\01?destroy@test4@@YAXPAUE@1@@Z"(%"struct.test4::E"* %obj) 441 442 // CHECK-NOT: getelementptr 443 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test4::E"* %[[OBJ:.*]] to i8* 444 // CHECK: %[[B_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 4 445 // CHECK: %[[VPTR:.*]] = bitcast i8* %[[B_i8]] to i8* (%"struct.test4::E"*, i32)*** 446 // CHECK: %[[VFTABLE:.*]] = load i8* (%"struct.test4::E"*, i32)**, i8* (%"struct.test4::E"*, i32)*** %[[VPTR]] 447 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds i8* (%"struct.test4::E"*, i32)*, i8* (%"struct.test4::E"*, i32)** %[[VFTABLE]], i64 0 448 // CHECK: %[[VFUN:.*]] = load i8* (%"struct.test4::E"*, i32)*, i8* (%"struct.test4::E"*, i32)** %[[VFTENTRY]] 449 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test4::E"* %[[OBJ]] to i8* 450 // CHECK: %[[B_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 4 451 // FIXME: in fact, the call should take i8* and the bitcast is redundant. 452 // CHECK: %[[B_as_E:.*]] = bitcast i8* %[[B_i8]] to %"struct.test4::E"* 453 // CHECK: call x86_thiscallcc i8* %[[VFUN]](%"struct.test4::E"* %[[B_as_E]], i32 1) 454 delete obj; 455} 456 457} 458 459namespace test5 { 460// PR25370: Don't zero-initialize vbptrs in virtual bases. 461struct A { 462 virtual void f(); 463}; 464 465struct B : virtual A { 466 int Field; 467}; 468 469struct C : B { 470 C(); 471}; 472 473C::C() : B() {} 474// CHECK-LABEL: define x86_thiscallcc %"struct.test5::C"* @"\01??0C@test5@@QAE@XZ"( 475// CHECK: %[[THIS:.*]] = load %"struct.test5::C"*, %"struct.test5::C"** 476// CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]] 477 478// CHECK: %[[SKIP_VBASES]] 479// CHECK: %[[B:.*]] = bitcast %"struct.test5::C"* %[[THIS]] to %"struct.test5::B"* 480// CHECK: %[[B_i8:.*]] = bitcast %"struct.test5::B"* %[[B]] to i8* 481// CHECK: %[[FIELD:.*]] = getelementptr inbounds i8, i8* %[[B_i8]], i32 4 482// CHECK: call void @llvm.memset.p0i8.i32(i8* %[[FIELD]], i8 0, i32 4, i32 4, i1 false) 483} 484 485namespace pr27621 { 486// Devirtualization through a static_cast used to make us compute the 'this' 487// adjustment for B::g instead of C::g. When we directly call C::g, 'this' is a 488// B*, and the prologue of C::g will adjust it to a C*. 489struct A { virtual void f(); }; 490struct B { virtual void g(); }; 491struct C final : A, B { 492 virtual void h(); 493 void g() override; 494}; 495void callit(C *p) { 496 static_cast<B*>(p)->g(); 497} 498// CHECK-LABEL: define void @"\01?callit@pr27621@@YAXPAUC@1@@Z"(%"struct.pr27621::C"* %{{.*}}) 499// CHECK: %[[B_i8:.*]] = getelementptr i8, i8* %{{.*}}, i32 4 500// CHECK: call x86_thiscallcc void @"\01?g@C@pr27621@@UAEXXZ"(i8* %[[B_i8]]) 501} 502 503namespace test6 { 504class A {}; 505class B : virtual A {}; 506class C : virtual B { 507 virtual void m_fn1(); 508 float field; 509}; 510class D : C { 511 D(); 512}; 513D::D() : C() {} 514// CHECK-LABEL: define x86_thiscallcc %"class.test6::D"* @"\01??0D@test6@@AAE@XZ"( 515// CHECK: %[[THIS:.*]] = load %"class.test6::D"*, %"class.test6::D"** 516// CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]] 517 518// CHECK: %[[SKIP_VBASES]] 519// CHECK: %[[C:.*]] = bitcast %"class.test6::D"* %[[THIS]] to %"class.test6::C"* 520// CHECK: %[[C_i8:.*]] = bitcast %"class.test6::C"* %[[C]] to i8* 521// CHECK: %[[FIELD:.*]] = getelementptr inbounds i8, i8* %[[C_i8]], i32 8 522// CHECK: call void @llvm.memset.p0i8.i32(i8* %[[FIELD]], i8 0, i32 4, i32 4, i1 false) 523} 524