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