blob: 18d47cea4876b38201f35bb9c4240dce950ad7f2 [file] [log] [blame]
Hans Wennborgc9bd88e2014-01-14 19:35:09 +00001// RUN: %clang_cc1 %s -fno-rtti -triple=i386-pc-win32 -emit-llvm -o %t
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +00002// RUN: FileCheck %s < %t
3// RUN: FileCheck --check-prefix=CHECK2 %s < %t
Peter Collingbourne66f82e62013-06-28 20:45:28 +00004
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +00005// For now, just make sure x86_64 doesn't crash.
Hans Wennborgc9bd88e2014-01-14 19:35:09 +00006// RUN: %clang_cc1 %s -fno-rtti -triple=x86_64-pc-win32 -emit-llvm -o %t
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +00007
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00008struct VBase {
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +00009 virtual ~VBase();
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +000010 virtual void foo();
11 virtual void bar();
12 int field;
Peter Collingbourne66f82e62013-06-28 20:45:28 +000013};
14
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +000015struct B : virtual VBase {
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000016 B();
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +000017 virtual ~B();
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +000018 virtual void foo();
19 virtual void bar();
20};
21
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000022B::B() {
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +000023 // CHECK-LABEL: define x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ"
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000024 // CHECK: %[[THIS:.*]] = load %struct.B**
25 // CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +000026
27 // Don't check the INIT_VBASES case as it's covered by the ctor tests.
28
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000029 // CHECK: %[[SKIP_VBASES]]
30 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
31 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 0
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +000032 // ...
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000033 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
34 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 %{{.*}}
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +000035 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to [3 x i8*]**
36 // CHECK: store [3 x i8*]* @"\01??_7B@@6B@", [3 x i8*]** %[[VFPTR]]
37
38 // Initialize vtorDisp:
39 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
40 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds 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* %[[THIS_i8]], i32 %[[VBASE_OFFSET]]
46 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr 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"
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +000055 // Adjust the this parameter:
56 // CHECK: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8*
Timur Iskhodzhanov827365e2013-10-22 18:15:24 +000057 // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[THIS_PARAM_i8]], i32 -8
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +000058 // 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** %[[THIS_ADDR]]
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +000061
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* %[[THIS_i8]], i32 0
65 // ...
66 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
67 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 %{{.*}}
68 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to [3 x i8*]**
69 // CHECK: store [3 x i8*]* @"\01??_7B@@6B@", [3 x i8*]** %[[VFPTR]]
70
71 // Initialize vtorDisp:
72 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
73 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds 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* %[[THIS_i8]], i32 %[[VBASE_OFFSET]]
79 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr 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
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000085 // CHECK: ret
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +000086
87 // CHECK2-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B*
88 // CHECK2: %[[THIS:.*]] = load %struct.B** {{.*}}
89 // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Timur Iskhodzhanov827365e2013-10-22 18:15:24 +000090 // CHECK2: %[[B_i8:.*]] = getelementptr i8* %[[THIS_i8]], i32 8
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +000091 // 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* %[[THIS_i8]], i64 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 void @"\01??_GB@@UAEPAXI@Z"
100 // CHECK2: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8*
Timur Iskhodzhanov827365e2013-10-22 18:15:24 +0000101 // CHECK2: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[THIS_PARAM_i8:.*]], i32 -8
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000102 // 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** %[[THIS_ADDR]]
105 // CHECK2: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[THIS]])
106 // ...
107 // CHECK2: ret
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000108}
109
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000110void B::foo() {
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000111// CHECK-LABEL: define x86_thiscallcc void @"\01?foo@B@@UAEXXZ"(i8*
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000112//
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
Timur Iskhodzhanov827365e2013-10-22 18:15:24 +0000117// CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[ECX:.*]], i32 -8
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000118// 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** %[[THIS_ADDR]]
123// CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
124// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS8]], i32 0
Reid Kleckner0ba8ba42014-10-22 17:26:00 +0000125// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
126// CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
127// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000128// CHECK: %[[VBOFFSET32:.*]] = load 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* %[[THIS8]], i32 %[[VBOFFSET]]
132// CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase*
133// CHECK: %[[FIELD:.*]] = getelementptr inbounds %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) {
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000140// CHECK-LABEL: define void @"\01?call_vbase_bar@@YAXPAUB@@@Z"(%struct.B* %obj)
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000141// 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* %[[OBJ_i8]], i32 0
Reid Kleckner0ba8ba42014-10-22 17:26:00 +0000149// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
150// CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
151// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000152// CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
153// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
154// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
155// CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to void (i8*)***
156// CHECK: %[[VFTABLE:.*]] = load void (i8*)*** %[[VFPTR]]
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000157// CHECK: %[[VFUN:.*]] = getelementptr inbounds void (i8*)** %[[VFTABLE]], i64 2
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000158// CHECK: %[[VFUN_VALUE:.*]] = load void (i8*)** %[[VFUN]]
159//
160// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
161// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
Reid Kleckner0ba8ba42014-10-22 17:26:00 +0000162// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
163// CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
164// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000165// CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
166// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
167// CHECK: %[[VBASE:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
168//
169// CHECK: call x86_thiscallcc void %[[VFUN_VALUE]](i8* %[[VBASE]])
170//
171// CHECK: ret void
172}
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000173
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000174void 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* %[[OBJ_i8]], i32 0
Reid Kleckner0ba8ba42014-10-22 17:26:00 +0000181// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
182// CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
183// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000184// CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
185// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
186// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
187// CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to void (%struct.B*, i32)***
188// CHECK: %[[VFTABLE:.*]] = load void (%struct.B*, i32)*** %[[VFPTR]]
189// CHECK: %[[VFUN:.*]] = getelementptr inbounds void (%struct.B*, i32)** %[[VFTABLE]], i64 0
190// CHECK: %[[VFUN_VALUE:.*]] = load void (%struct.B*, i32)** %[[VFUN]]
191//
192// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
193// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
Reid Kleckner0ba8ba42014-10-22 17:26:00 +0000194// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
195// CHECK: %[[VBTABLE:.*]] = load i32** %[[VBPTR8]]
196// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32* %[[VBTABLE]], i32 1
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000197// CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
198// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
199// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
200// CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.B*
201//
202// CHECK: call x86_thiscallcc void %[[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
Timur Iskhodzhanova8957582014-03-07 09:34:59 +0000215struct C : B {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000216 C();
217 // has an implicit vdtor.
218};
219
220// Used to crash on an assertion.
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000221C::C() {
222// CHECK-LABEL: define x86_thiscallcc %struct.C* @"\01??0C@@QAE@XZ"
223}
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000224
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 [1 x i8*]* @"\01??_7D@multiple_vbases@@6BA@1@@", [1 x i8*]** %{{.*}}
249 // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BB@1@@", [1 x i8*]** %{{.*}}
250 // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BC@1@@", [1 x i8*]** %{{.*}}
251 // ...
252 // CHECK: store i32 %{{.*}}, i32* %{{.*}}
253 // CHECK: store i32 %{{.*}}, i32* %{{.*}}
254 // CHECK: store i32 %{{.*}}, i32* %{{.*}}
255 // CHECK: ret
256}
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000257}
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*
Timur Iskhodzhanov827365e2013-10-22 18:15:24 +0000288 // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[ARG_i8]], i32 -24
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000289 // 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"** %[[THIS_VAL]]
292 // CHECK: %[[D_i8:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to i8*
293 // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8* %[[D_i8]], i64 4
294 // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.diamond::C"*
295 // CHECK: %[[C_i8:.*]] = bitcast %"struct.diamond::C"* %[[C]] to i8*
Timur Iskhodzhanov827365e2013-10-22 18:15:24 +0000296 // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %{{.*}}, i32 16
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000297 // 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*
Timur Iskhodzhanov827365e2013-10-22 18:15:24 +0000303 // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %[[B_i8]], i32 4
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +0000304 // 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}
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +0000308
309}
Reid Kleckner340ad862014-01-13 22:57:31 +0000310
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}
Timur Iskhodzhanovf1749422014-03-14 17:43:37 +0000337
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**
366 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test3::D"* %{{.*}} to i8*
367 // CHECK: %[[C_i8:.*]] = getelementptr inbounds 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* %[[C_i8]], i32 4
371 // CHECK: call x86_thiscallcc void @"\01?foo@C@test3@@UAEXXZ"(i8* %[[ARG]])
372 // CHECK: ret
373}
374}
Timur Iskhodzhanovba557022014-03-20 20:38:34 +0000375
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 [1 x i8*]**
400 // CHECK: store [1 x i8*]* @"\01??_7C@test4@@6BB@1@@", [1 x i8*]** %[[VFPTR_i8]]
401
402 foo(this);
Timur Iskhodzhanov67c96932014-03-21 11:07:24 +0000403 // CHECK: ret
Timur Iskhodzhanovba557022014-03-20 20:38:34 +0000404}
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 void (%"struct.test4::C"*, i32)***
411 // CHECK: %[[VFTABLE:.*]] = load void (%"struct.test4::C"*, i32)*** %[[VPTR]]
412 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds void (%"struct.test4::C"*, i32)** %[[VFTABLE]], i64 0
413 // CHECK: %[[VFUN:.*]] = load void (%"struct.test4::C"*, i32)** %[[VFTENTRY]]
414 // CHECK: call x86_thiscallcc void %[[VFUN]](%"struct.test4::C"* %[[OBJ]], i32 1)
Timur Iskhodzhanov67c96932014-03-21 11:07:24 +0000415 // 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 [1 x i8*]**
435 // CHECK: store [1 x i8*]* @"\01??_7E@test4@@6BD@1@@", [1 x i8*]** %[[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* %[[OBJ_i8]], i32 4
445 // CHECK: %[[VPTR:.*]] = bitcast i8* %[[B_i8]] to void (%"struct.test4::E"*, i32)***
446 // CHECK: %[[VFTABLE:.*]] = load void (%"struct.test4::E"*, i32)*** %[[VPTR]]
447 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds void (%"struct.test4::E"*, i32)** %[[VFTABLE]], i64 0
448 // CHECK: %[[VFUN:.*]] = load void (%"struct.test4::E"*, i32)** %[[VFTENTRY]]
449 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test4::E"* %[[OBJ]] to i8*
450 // CHECK: %[[B_i8:.*]] = getelementptr inbounds 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 void %[[VFUN]](%"struct.test4::E"* %[[B_as_E]], i32 1)
454 delete obj;
Timur Iskhodzhanovba557022014-03-20 20:38:34 +0000455}
456
457}