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 
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**
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* %[[THIS_i8]], i32 0
32   // ...
33   // CHECK:   %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
34   // CHECK:   %[[VFPTR_i8:.*]] = getelementptr inbounds i8* %[[THIS_i8]], i32 %{{.*}}
35   // 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 
53 B::~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* %[[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** %[[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* %[[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 
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** {{.*}}
89   // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
90   // CHECK2: %[[B_i8:.*]] = getelementptr 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* %[[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*
101   // CHECK2:   %[[THIS_i8:.*]] = getelementptr inbounds 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** %[[THIS_ADDR]]
105   // CHECK2:   call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[THIS]])
106   // ...
107   // CHECK2: ret
108 }
109 
110 void 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* %[[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** %[[THIS_ADDR]]
123 // CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
124 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[THIS8]], i32 0
125 // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i8**
126 // CHECK: %[[VBTABLE:.*]] = load i8** %[[VBPTR8]]
127 // CHECK: %[[VBENTRY8:.*]] = getelementptr inbounds i8* %[[VBTABLE]], i32 4
128 // CHECK: %[[VBENTRY:.*]] = bitcast i8* %[[VBENTRY8]] to i32*
129 // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
130 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
131 // CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
132 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[THIS8]], i32 %[[VBOFFSET]]
133 // CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase*
134 // CHECK: %[[FIELD:.*]] = getelementptr inbounds %struct.VBase* %[[VBASE]], i32 0, i32 1
135 // CHECK: store i32 42, i32* %[[FIELD]], align 4
136 //
137 // CHECK: ret void
138 }
139 
140 void call_vbase_bar(B *obj) {
141 // CHECK-LABEL: define void @"\01?call_vbase_bar@@YAXPAUB@@@Z"(%struct.B* %obj)
142 // CHECK: %[[OBJ:.*]] = load %struct.B
143 
144   obj->bar();
145 // When calling a vbase's virtual method, one needs to adjust 'this'
146 // at the caller site.
147 //
148 // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
149 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
150 // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i8**
151 // CHECK: %[[VBTABLE:.*]] = load i8** %[[VBPTR8]]
152 // CHECK: %[[VBENTRY8:.*]] = getelementptr inbounds i8* %[[VBTABLE]], i32 4
153 // CHECK: %[[VBENTRY:.*]] = bitcast i8* %[[VBENTRY8]] to i32*
154 // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
155 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
156 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
157 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to void (i8*)***
158 // CHECK: %[[VFTABLE:.*]] = load void (i8*)*** %[[VFPTR]]
159 // CHECK: %[[VFUN:.*]] = getelementptr inbounds void (i8*)** %[[VFTABLE]], i64 2
160 // CHECK: %[[VFUN_VALUE:.*]] = load void (i8*)** %[[VFUN]]
161 //
162 // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
163 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
164 // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i8**
165 // CHECK: %[[VBTABLE:.*]] = load i8** %[[VBPTR8]]
166 // CHECK: %[[VBENTRY8:.*]] = getelementptr inbounds i8* %[[VBTABLE]], i32 4
167 // CHECK: %[[VBENTRY:.*]] = bitcast i8* %[[VBENTRY8]] to i32*
168 // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
169 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
170 // CHECK: %[[VBASE:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
171 //
172 // CHECK: call x86_thiscallcc void %[[VFUN_VALUE]](i8* %[[VBASE]])
173 //
174 // CHECK: ret void
175 }
176 
177 void delete_B(B *obj) {
178 // CHECK-LABEL: define void @"\01?delete_B@@YAXPAUB@@@Z"(%struct.B* %obj)
179 // CHECK: %[[OBJ:.*]] = load %struct.B
180 
181   delete obj;
182 // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
183 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
184 // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i8**
185 // CHECK: %[[VBTABLE:.*]] = load i8** %[[VBPTR8]]
186 // CHECK: %[[VBENTRY8:.*]] = getelementptr inbounds i8* %[[VBTABLE]], i32 4
187 // CHECK: %[[VBENTRY:.*]] = bitcast i8* %[[VBENTRY8]] to i32*
188 // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
189 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
190 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
191 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to void (%struct.B*, i32)***
192 // CHECK: %[[VFTABLE:.*]] = load void (%struct.B*, i32)*** %[[VFPTR]]
193 // CHECK: %[[VFUN:.*]] = getelementptr inbounds void (%struct.B*, i32)** %[[VFTABLE]], i64 0
194 // CHECK: %[[VFUN_VALUE:.*]] = load void (%struct.B*, i32)** %[[VFUN]]
195 //
196 // CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
197 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 0
198 // CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i8**
199 // CHECK: %[[VBTABLE:.*]] = load i8** %[[VBPTR8]]
200 // CHECK: %[[VBENTRY8:.*]] = getelementptr inbounds i8* %[[VBTABLE]], i32 4
201 // CHECK: %[[VBENTRY:.*]] = bitcast i8* %[[VBENTRY8]] to i32*
202 // CHECK: %[[VBOFFSET32:.*]] = load i32* %[[VBENTRY]]
203 // CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
204 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
205 // CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.B*
206 //
207 // CHECK: call x86_thiscallcc void %[[VFUN_VALUE]](%struct.B* %[[VBASE]], i32 1)
208 // CHECK: ret void
209 }
210 
211 void call_complete_dtor() {
212   // CHECK-LABEL: define void @"\01?call_complete_dtor@@YAXXZ"
213   B b;
214   // CHECK: call x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ"(%struct.B* %[[B:.*]], i32 1)
215   // CHECK-NOT: getelementptr
216   // CHECK: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[B]])
217   // CHECK: ret
218 }
219 
220 struct X : virtual VBase {
221   int x;
222 };
223 
224 
225 struct C : X {
226   C();
227   // has an implicit vdtor.
228 };
229 
230 // Used to crash on an assertion.
231 C::C() {
232 // CHECK-LABEL: define x86_thiscallcc %struct.C* @"\01??0C@@QAE@XZ"
233 }
234 
235 namespace multiple_vbases {
236 struct A {
237   virtual void a();
238 };
239 
240 struct B {
241   virtual void b();
242 };
243 
244 struct C {
245   virtual void c();
246 };
247 
248 struct D : virtual A, virtual B, virtual C {
249   virtual void a();
250   virtual void b();
251   virtual void c();
252   D();
253 };
254 
255 D::D() {
256   // CHECK-LABEL: define x86_thiscallcc %"struct.multiple_vbases::D"* @"\01??0D@multiple_vbases@@QAE@XZ"
257   // Just make sure we emit 3 vtordisps after initializing vfptrs.
258   // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BA@1@@", [1 x i8*]** %{{.*}}
259   // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BB@1@@", [1 x i8*]** %{{.*}}
260   // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BC@1@@", [1 x i8*]** %{{.*}}
261   // ...
262   // CHECK: store i32 %{{.*}}, i32* %{{.*}}
263   // CHECK: store i32 %{{.*}}, i32* %{{.*}}
264   // CHECK: store i32 %{{.*}}, i32* %{{.*}}
265   // CHECK: ret
266 }
267 }
268 
269 namespace diamond {
270 struct A {
271   A();
272   virtual ~A();
273 };
274 
275 struct B : virtual A {
276   B();
277   ~B();
278 };
279 
280 struct C : virtual A {
281   C();
282   ~C();
283   int c1, c2, c3;
284 };
285 
286 struct Z {
287   int z;
288 };
289 
290 struct D : virtual Z, B, C {
291   D();
292   ~D();
293 } d;
294 
295 D::~D() {
296   // CHECK-LABEL: define x86_thiscallcc void @"\01??1D@diamond@@UAE@XZ"(%"struct.diamond::D"*)
297   // CHECK: %[[ARG_i8:.*]] = bitcast %"struct.diamond::D"* %{{.*}} to i8*
298   // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[ARG_i8]], i32 -24
299   // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %"struct.diamond::D"*
300   // CHECK: store %"struct.diamond::D"* %[[THIS]], %"struct.diamond::D"** %[[THIS_VAL:.*]], align 4
301   // CHECK: %[[THIS:.*]] = load %"struct.diamond::D"** %[[THIS_VAL]]
302   // CHECK: %[[D_i8:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to i8*
303   // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8* %[[D_i8]], i64 4
304   // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.diamond::C"*
305   // CHECK: %[[C_i8:.*]] = bitcast %"struct.diamond::C"* %[[C]] to i8*
306   // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %{{.*}}, i32 16
307   // FIXME: We might consider changing the dtor this parameter type to i8*.
308   // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::C"*
309   // CHECK: call x86_thiscallcc void @"\01??1C@diamond@@UAE@XZ"(%"struct.diamond::C"* %[[ARG]])
310 
311   // CHECK: %[[B:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to %"struct.diamond::B"*
312   // CHECK: %[[B_i8:.*]] = bitcast %"struct.diamond::B"* %[[B]] to i8*
313   // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %[[B_i8]], i32 4
314   // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::B"*
315   // CHECK: call x86_thiscallcc void @"\01??1B@diamond@@UAE@XZ"(%"struct.diamond::B"* %[[ARG]])
316   // CHECK: ret void
317 }
318 
319 }
320 
321 namespace test2 {
322 struct A { A(); };
323 struct B : virtual A { B() {} };
324 struct C : B, A { C() {} };
325 
326 // PR18435: Order mattered here.  We were generating code for the delegating
327 // call to B() from C().
328 void callC() { C x; }
329 
330 // CHECK-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::C"* @"\01??0C@test2@@QAE@XZ"
331 // CHECK:           (%"struct.test2::C"* returned %this, i32 %is_most_derived)
332 // CHECK: br i1
333 //   Virtual bases
334 // CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
335 // CHECK: br label
336 //   Non-virtual bases
337 // CHECK: call x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ"(%"struct.test2::B"* %{{.*}}, i32 0)
338 // CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
339 // CHECK: ret
340 
341 // CHECK2-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ"
342 // CHECK2:           (%"struct.test2::B"* returned %this, i32 %is_most_derived)
343 // CHECK2: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
344 // CHECK2: ret
345 
346 }
347