1 // RUN: %clang_cc1 %s -fno-rtti -std=c++11 -Wno-inaccessible-base -triple=i386-pc-win32 -emit-llvm -o %t
2 // RUN: FileCheck %s < %t
3 // RUN: FileCheck --check-prefix=CHECK2 %s < %t
5 // For now, just make sure x86_64 doesn't crash.
6 // RUN: %clang_cc1 %s -fno-rtti -std=c++11 -Wno-inaccessible-base -triple=x86_64-pc-win32 -emit-llvm -o %t
15 struct B
: virtual VBase
{
23 // CHECK-LABEL: define dso_local x86_thiscallcc noundef ptr @"??0B@@QAE@XZ"
24 // CHECK: %[[THIS:.*]] = load ptr, ptr
25 // CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
27 // Don't check the INIT_VBASES case as it's covered by the ctor tests.
29 // CHECK: %[[SKIP_VBASES]]
30 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[THIS]], i32 0
32 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS]], i32 %{{.*}}
33 // CHECK: store ptr @"??_7B@@6B@", ptr %[[VFPTR_i8]]
35 // Initialize vtorDisp:
36 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[THIS]], i32 0
38 // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}}
39 // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8
40 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS]], i32 %[[VBASE_OFFSET]]
41 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8, ptr %[[VBASE_i8]], i32 -4
42 // CHECK: store i32 %[[VTORDISP_VAL]], ptr %[[VTORDISP_i8]]
48 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1B@@UAE@XZ"
49 // Store initial this:
50 // CHECK: %[[THIS_ADDR:.*]] = alloca ptr
51 // CHECK: store ptr %{{.*}}, ptr %[[THIS_ADDR]], align 4
52 // Reload and adjust the this parameter:
53 // CHECK: %[[THIS_RELOAD:.*]] = load ptr, ptr %[[THIS_ADDR]]
54 // CHECK: %[[THIS_ADJ_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_RELOAD]], i32 -8
56 // Restore the vfptr that could have been changed by a subclass.
57 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[THIS_ADJ_i8]], i32 0
59 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_ADJ_i8]], i32 %{{.*}}
60 // CHECK: store ptr @"??_7B@@6B@", ptr %[[VFPTR_i8]]
62 // Initialize vtorDisp:
63 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[THIS_ADJ_i8]], i32 0
65 // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}}
66 // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8
67 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_ADJ_i8]], i32 %[[VBASE_OFFSET]]
68 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8, ptr %[[VBASE_i8]], i32 -4
69 // CHECK: store i32 %[[VTORDISP_VAL]], ptr %[[VTORDISP_i8]]
71 foo(); // Avoid the "trivial destructor" optimization.
75 // CHECK2-LABEL: define linkonce_odr dso_local x86_thiscallcc void @"??_DB@@QAEXXZ"(ptr
76 // CHECK2: %[[THIS:.*]] = load ptr, ptr {{.*}}
77 // CHECK2: %[[B_i8:.*]] = getelementptr i8, ptr %[[THIS]], i32 8
78 // CHECK2: call x86_thiscallcc void @"??1B@@UAE@XZ"(ptr{{[^,]*}} %[[B_i8]])
79 // CHECK2: %[[VBASE_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS]], i32 8
80 // CHECK2: call x86_thiscallcc void @"??1VBase@@UAE@XZ"(ptr {{[^,]*}} %[[VBASE_i8]])
83 // CHECK2-LABEL: define linkonce_odr dso_local x86_thiscallcc noundef ptr @"??_GB@@UAEPAXI@Z"
84 // CHECK2: store ptr %{{.*}}, ptr %[[THIS_ADDR:.*]], align 4
85 // CHECK2: %[[THIS_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_PARAM_i8:.*]], i32 -8
86 // CHECK2: call x86_thiscallcc void @"??_DB@@QAEXXZ"(ptr {{[^,]*}} %[[THIS_i8]])
92 // CHECK-LABEL: define dso_local x86_thiscallcc void @"?foo@B@@UAEXXZ"(ptr
94 // B::foo gets 'this' cast to VBase* in ECX (i.e. this+8) so we
95 // need to adjust 'this' before use.
97 // Coerce this to correct type:
98 // CHECK: %[[THIS_ADDR:.*]] = alloca ptr
100 // Store initial this:
101 // CHECK: store ptr {{.*}}, ptr %[[THIS_ADDR]], align 4
103 // Reload and adjust the this parameter:
104 // CHECK: %[[THIS_RELOAD:.*]] = load ptr, ptr %[[THIS_ADDR]]
105 // CHECK: %[[THIS_ADJ_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_RELOAD]], i32 -8
108 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[THIS_ADJ_i8]], i32 0
109 // CHECK: %[[VBTABLE:.*]] = load ptr, ptr %[[VBPTR]]
110 // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, ptr %[[VBTABLE]], i32 1
111 // CHECK: %[[VBOFFSET32:.*]] = load i32, ptr %[[VBENTRY]]
112 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
113 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_ADJ_i8]], i32 %[[VBOFFSET]]
114 // CHECK: %[[FIELD:.*]] = getelementptr inbounds %struct.VBase, ptr %[[VBASE_i8]], i32 0, i32 1
115 // CHECK: store i32 42, ptr %[[FIELD]], align 4
120 void call_vbase_bar(B
*obj
) {
121 // CHECK-LABEL: define dso_local void @"?call_vbase_bar@@YAXPAUB@@@Z"(ptr noundef %obj)
122 // CHECK: %[[OBJ:.*]] = load ptr
125 // When calling a vbase's virtual method, one needs to adjust 'this'
126 // at the caller site.
128 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 0
129 // CHECK: %[[VBTABLE:.*]] = load ptr, ptr %[[VBPTR]]
130 // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, ptr %[[VBTABLE]], i32 1
131 // CHECK: %[[VBOFFSET32:.*]] = load i32, ptr %[[VBENTRY]]
132 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
133 // CHECK: %[[VBASE:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 %[[VBOFFSET]]
135 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 0
136 // CHECK: %[[VBTABLE:.*]] = load ptr, ptr %[[VBPTR]]
137 // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, ptr %[[VBTABLE]], i32 1
138 // CHECK: %[[VBOFFSET32:.*]] = load i32, ptr %[[VBENTRY]]
139 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
140 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 %[[VBOFFSET]]
141 // CHECK: %[[VFTABLE:.*]] = load ptr, ptr %[[VBASE_i8]]
142 // CHECK: %[[VFUN:.*]] = getelementptr inbounds ptr, ptr %[[VFTABLE]], i64 2
143 // CHECK: %[[VFUN_VALUE:.*]] = load ptr, ptr %[[VFUN]]
145 // CHECK: call x86_thiscallcc void %[[VFUN_VALUE]](ptr noundef %[[VBASE]])
150 void delete_B(B
*obj
) {
151 // CHECK-LABEL: define dso_local void @"?delete_B@@YAXPAUB@@@Z"(ptr noundef %obj)
152 // CHECK: %[[OBJ:.*]] = load ptr
155 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 0
156 // CHECK: %[[VBTABLE:.*]] = load ptr, ptr %[[VBPTR]]
157 // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, ptr %[[VBTABLE]], i32 1
158 // CHECK: %[[VBOFFSET32:.*]] = load i32, ptr %[[VBENTRY]]
159 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
160 // CHECK: %[[VBASE:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 %[[VBOFFSET]]
162 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 0
163 // CHECK: %[[VBTABLE:.*]] = load ptr, ptr %[[VBPTR]]
164 // CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, ptr %[[VBTABLE]], i32 1
165 // CHECK: %[[VBOFFSET32:.*]] = load i32, ptr %[[VBENTRY]]
166 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
167 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 %[[VBOFFSET]]
168 // CHECK: %[[VFTABLE:.*]] = load ptr, ptr %[[VBASE_i8]]
169 // CHECK: %[[VFUN:.*]] = getelementptr inbounds ptr, ptr %[[VFTABLE]], i64 0
170 // CHECK: %[[VFUN_VALUE:.*]] = load ptr, ptr %[[VFUN]]
172 // CHECK: call x86_thiscallcc noundef ptr %[[VFUN_VALUE]](ptr {{[^,]*}} %[[VBASE]], i32 noundef 1)
176 void call_complete_dtor() {
177 // CHECK-LABEL: define dso_local void @"?call_complete_dtor@@YAXXZ"
179 // CHECK: call x86_thiscallcc noundef ptr @"??0B@@QAE@XZ"(ptr {{[^,]*}} %[[B:.*]], i32 noundef 1)
180 // CHECK-NOT: getelementptr
181 // CHECK: call x86_thiscallcc void @"??_DB@@QAEXXZ"(ptr {{[^,]*}} %[[B]])
187 // has an implicit vdtor.
190 // Used to crash on an assertion.
192 // CHECK-LABEL: define dso_local x86_thiscallcc noundef ptr @"??0C@@QAE@XZ"
195 namespace multiple_vbases
{
208 struct D
: virtual A
, virtual B
, virtual C
{
216 // CHECK-LABEL: define dso_local x86_thiscallcc noundef ptr @"??0D@multiple_vbases@@QAE@XZ"
217 // Just make sure we emit 3 vtordisps after initializing vfptrs.
218 // CHECK: store ptr @"??_7D@multiple_vbases@@6BA@1@@", ptr %{{.*}}
219 // CHECK: store ptr @"??_7D@multiple_vbases@@6BB@1@@", ptr %{{.*}}
220 // CHECK: store ptr @"??_7D@multiple_vbases@@6BC@1@@", ptr %{{.*}}
222 // CHECK: store i32 %{{.*}}, ptr %{{.*}}
223 // CHECK: store i32 %{{.*}}, ptr %{{.*}}
224 // CHECK: store i32 %{{.*}}, ptr %{{.*}}
235 struct B
: virtual A
{
240 struct C
: virtual A
{
250 struct D
: virtual Z
, B
, C
{
256 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1D@diamond@@UAE@XZ"(ptr{{.*}})
257 // Store initial this:
258 // CHECK: %[[THIS_ADDR:.*]] = alloca ptr
259 // CHECK: store ptr %{{.*}}, ptr %[[THIS_ADDR]], align 4
261 // Reload and adjust the this parameter:
262 // CHECK: %[[THIS_RELOAD:.*]] = load ptr, ptr %[[THIS_ADDR]]
263 // CHECK: %[[THIS_ADJ_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_RELOAD]], i32 -24
265 // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_ADJ_i8]], i32 4
266 // CHECK: %[[ARG:.*]] = getelementptr i8, ptr %{{.*}}, i32 16
267 // CHECK: call x86_thiscallcc void @"??1C@diamond@@UAE@XZ"(ptr{{[^,]*}} %[[ARG]])
269 // CHECK: %[[ARG:.*]] = getelementptr i8, ptr %[[THIS_ADJ_i8]], i32 4
270 // CHECK: call x86_thiscallcc void @"??1B@diamond@@UAE@XZ"(ptr{{[^,]*}} %[[ARG]])
278 struct B
: virtual A
{ B() {} };
279 struct C
: B
, A
{ C() {} };
281 // PR18435: Order mattered here. We were generating code for the delegating
282 // call to B() from C().
283 void callC() { C x
; }
285 // CHECK-LABEL: define linkonce_odr dso_local x86_thiscallcc noundef ptr @"??0C@test2@@QAE@XZ"
286 // CHECK: (ptr {{[^,]*}} returned align 4 dereferenceable(8) %this, i32 noundef %is_most_derived)
289 // CHECK: call x86_thiscallcc noundef ptr @"??0A@test2@@QAE@XZ"(ptr {{[^,]*}} %{{.*}})
292 // CHECK: call x86_thiscallcc noundef ptr @"??0B@test2@@QAE@XZ"(ptr {{[^,]*}} %{{.*}}, i32 noundef 0)
293 // CHECK: call x86_thiscallcc noundef ptr @"??0A@test2@@QAE@XZ"(ptr {{[^,]*}} %{{.*}})
296 // CHECK2-LABEL: define linkonce_odr dso_local x86_thiscallcc noundef ptr @"??0B@test2@@QAE@XZ"
297 // CHECK2: (ptr {{[^,]*}} returned align 4 dereferenceable(4) %this, i32 noundef %is_most_derived)
298 // CHECK2: call x86_thiscallcc noundef ptr @"??0A@test2@@QAE@XZ"(ptr {{[^,]*}} %{{.*}})
304 // PR19104: A non-virtual call of a virtual method doesn't use vftable thunks,
305 // so requires only static adjustment which is different to the one used
306 // for virtual calls.
311 struct B
: virtual A
{
315 struct C
: virtual A
{
321 int field
; // Laid out between C and A subobjects in D.
325 // CHECK-LABEL: define dso_local x86_thiscallcc void @"?bar@D@test3@@UAEXXZ"(ptr {{[^,]*}} %this)
328 // Shouldn't need any vbtable lookups. All we have to do is adjust to C*,
329 // then compensate for the adjustment performed in the C::foo() prologue.
330 // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i32 8
331 // CHECK: %[[ARG:.*]] = getelementptr i8, ptr %[[C_i8]], i32 4
332 // CHECK: call x86_thiscallcc void @"?foo@C@test3@@UAEXXZ"(ptr noundef %[[ARG]])
338 // PR19172: We used to merge method vftable locations wrong.
348 struct C
: virtual A
, B
{
355 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1C@test4@@UAE@XZ"(ptr {{[^,]*}} %this)
357 // In this case "this" points to the most derived class, so no GEPs needed.
358 // CHECK-NOT: getelementptr
359 // CHECK: store ptr @"??_7C@test4@@6BB@1@@", ptr %{{.*}}
365 void destroy(C
*obj
) {
366 // CHECK-LABEL: define dso_local void @"?destroy@test4@@YAXPAUC@1@@Z"(ptr noundef %obj)
369 // CHECK: %[[OBJ:.*]] = load ptr, ptr
370 // CHECK: %[[VFTABLE:.*]] = load ptr, ptr %[[OBJ]]
371 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds ptr, ptr %[[VFTABLE]], i64 0
372 // CHECK: %[[VFUN:.*]] = load ptr, ptr %[[VFTENTRY]]
373 // CHECK: call x86_thiscallcc noundef ptr %[[VFUN]](ptr {{[^,]*}} %[[OBJ]], i32 noundef 1)
381 // The first non-virtual base doesn't have a vdtor,
382 // but "this adjustment" is not needed.
383 struct E
: D
, B
, virtual A
{
388 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1E@test4@@UAE@XZ"(ptr{{[^,]*}} %this)
390 // In this case "this" points to the most derived class, so no GEPs needed.
391 // CHECK-NOT: getelementptr
392 // CHECK: store ptr @"??_7E@test4@@6BD@1@@", ptr %{{.*}}
396 void destroy(E
*obj
) {
397 // CHECK-LABEL: define dso_local void @"?destroy@test4@@YAXPAUE@1@@Z"(ptr noundef %obj)
399 // CHECK-NOT: getelementptr
400 // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8, ptr %[[OBJ]], i32 4
401 // CHECK: %[[B_i8:.*]] = getelementptr inbounds i8, ptr %[[OBJ:.*]], i32 4
402 // CHECK: %[[VFTABLE:.*]] = load ptr, ptr %[[B_i8]]
403 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds ptr, ptr %[[VFTABLE]], i64 0
404 // CHECK: %[[VFUN:.*]] = load ptr, ptr %[[VFTENTRY]]
405 // CHECK: call x86_thiscallcc noundef ptr %[[VFUN]](ptr{{[^,]*}} %[[THIS_i8]], i32 noundef 1)
412 // PR25370: Don't zero-initialize vbptrs in virtual bases.
417 struct B
: virtual A
{
426 // CHECK-LABEL: define dso_local x86_thiscallcc noundef ptr @"??0C@test5@@QAE@XZ"(
427 // CHECK: %[[THIS:.*]] = load ptr, ptr
428 // CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
430 // CHECK: %[[SKIP_VBASES]]
431 // CHECK: %[[FIELD:.*]] = getelementptr inbounds i8, ptr %[[THIS]], i32 4
432 // CHECK: call void @llvm.memset.p0.i32(ptr align 4 %[[FIELD]], i8 0, i32 4, i1 false)
436 // Devirtualization through a static_cast used to make us compute the 'this'
437 // adjustment for B::g instead of C::g. When we directly call C::g, 'this' is a
438 // B*, and the prologue of C::g will adjust it to a C*.
439 struct A
{ virtual void f(); };
440 struct B
{ virtual void g(); };
441 struct C final
: A
, B
{
446 static_cast<B
*>(p
)->g();
448 // CHECK-LABEL: define dso_local void @"?callit@pr27621@@YAXPAUC@1@@Z"(ptr noundef %{{.*}})
449 // CHECK: %[[B_i8:.*]] = getelementptr i8, ptr %{{.*}}, i32 4
450 // CHECK: call x86_thiscallcc void @"?g@C@pr27621@@UAEXXZ"(ptr noundef %[[B_i8]])
455 class B
: virtual A
{};
456 class C
: virtual B
{
457 virtual void m_fn1();
464 // CHECK-LABEL: define dso_local x86_thiscallcc noundef ptr @"??0D@test6@@AAE@XZ"(
465 // CHECK: %[[THIS:.*]] = load ptr, ptr
466 // CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
468 // CHECK: %[[SKIP_VBASES]]
469 // CHECK: %[[FIELD:.*]] = getelementptr inbounds i8, ptr %[[THIS]], i32 8
470 // CHECK: call void @llvm.memset.p0.i32(ptr align 4 %[[FIELD]], i8 0, i32 4, i1 false)
480 struct C
: virtual B
{};
481 struct D
: virtual A
, C
{};
483 // CHECK2-LABEL: define linkonce_odr dso_local x86_thiscallcc noundef ptr @"??_GD@pr36921@@UAEPAXI@Z"(
484 // CHECK2: %[[THIS_RELOAD:.*]] = load ptr, ptr
485 // CHECK2: %[[THIS_ADJ_i8:.*]] = getelementptr inbounds i8, ptr %[[THIS_RELOAD]], i32 -4
488 namespace issue_60465
{
489 // We used to assume the first argument to all destructors was the derived type
490 // even when there was a 'this' adjustment.
495 struct alignas(2 * sizeof(void *)) B
: virtual A
{
501 // The 'this' parameter should not have a type of ptr and
502 // must not have 'align 8', since at least B's copy of A is only 'align 4'.
503 // CHECK-LABEL: define dso_local x86_thiscallcc void @"??1B@issue_60465@@UAE@XZ"(ptr noundef %this)
504 // CHECK: %[[THIS_ADJ_i8:.*]] = getelementptr inbounds i8, ptr %{{.*}}, i32 -12
505 // CHECK: %[[X:.*]] = getelementptr inbounds %"struct.issue_60465::B", ptr %[[THIS_ADJ_i8]], i32 0, i32 1
506 // CHECK: store ptr null, ptr %[[X]], align 4
507 // CHECK: %[[Y:.*]] = getelementptr inbounds %"struct.issue_60465::B", ptr %[[THIS_ADJ_i8]], i32 0, i32 2
508 // CHECK: store ptr null, ptr %[[Y]], align 8