1 // RUN: %clang_cc1 %s -fno-rtti -cxx-abi microsoft -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 -cxx-abi microsoft -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 C : B {
221   C();
222   // has an implicit vdtor.
223 };
224 
225 // Used to crash on an assertion.
226 C::C() {
227 // CHECK-LABEL: define x86_thiscallcc %struct.C* @"\01??0C@@QAE@XZ"
228 }
229 
230 namespace multiple_vbases {
231 struct A {
232   virtual void a();
233 };
234 
235 struct B {
236   virtual void b();
237 };
238 
239 struct C {
240   virtual void c();
241 };
242 
243 struct D : virtual A, virtual B, virtual C {
244   virtual void a();
245   virtual void b();
246   virtual void c();
247   D();
248 };
249 
250 D::D() {
251   // CHECK-LABEL: define x86_thiscallcc %"struct.multiple_vbases::D"* @"\01??0D@multiple_vbases@@QAE@XZ"
252   // Just make sure we emit 3 vtordisps after initializing vfptrs.
253   // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BA@1@@", [1 x i8*]** %{{.*}}
254   // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BB@1@@", [1 x i8*]** %{{.*}}
255   // CHECK: store [1 x i8*]* @"\01??_7D@multiple_vbases@@6BC@1@@", [1 x i8*]** %{{.*}}
256   // ...
257   // CHECK: store i32 %{{.*}}, i32* %{{.*}}
258   // CHECK: store i32 %{{.*}}, i32* %{{.*}}
259   // CHECK: store i32 %{{.*}}, i32* %{{.*}}
260   // CHECK: ret
261 }
262 }
263 
264 namespace diamond {
265 struct A {
266   A();
267   virtual ~A();
268 };
269 
270 struct B : virtual A {
271   B();
272   ~B();
273 };
274 
275 struct C : virtual A {
276   C();
277   ~C();
278   int c1, c2, c3;
279 };
280 
281 struct Z {
282   int z;
283 };
284 
285 struct D : virtual Z, B, C {
286   D();
287   ~D();
288 } d;
289 
290 D::~D() {
291   // CHECK-LABEL: define x86_thiscallcc void @"\01??1D@diamond@@UAE@XZ"(%"struct.diamond::D"*)
292   // CHECK: %[[ARG_i8:.*]] = bitcast %"struct.diamond::D"* %{{.*}} to i8*
293   // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8* %[[ARG_i8]], i32 -24
294   // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %"struct.diamond::D"*
295   // CHECK: store %"struct.diamond::D"* %[[THIS]], %"struct.diamond::D"** %[[THIS_VAL:.*]], align 4
296   // CHECK: %[[THIS:.*]] = load %"struct.diamond::D"** %[[THIS_VAL]]
297   // CHECK: %[[D_i8:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to i8*
298   // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8* %[[D_i8]], i64 4
299   // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.diamond::C"*
300   // CHECK: %[[C_i8:.*]] = bitcast %"struct.diamond::C"* %[[C]] to i8*
301   // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %{{.*}}, i32 16
302   // FIXME: We might consider changing the dtor this parameter type to i8*.
303   // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::C"*
304   // CHECK: call x86_thiscallcc void @"\01??1C@diamond@@UAE@XZ"(%"struct.diamond::C"* %[[ARG]])
305 
306   // CHECK: %[[B:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to %"struct.diamond::B"*
307   // CHECK: %[[B_i8:.*]] = bitcast %"struct.diamond::B"* %[[B]] to i8*
308   // CHECK: %[[ARG_i8:.*]] = getelementptr i8* %[[B_i8]], i32 4
309   // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::B"*
310   // CHECK: call x86_thiscallcc void @"\01??1B@diamond@@UAE@XZ"(%"struct.diamond::B"* %[[ARG]])
311   // CHECK: ret void
312 }
313 
314 }
315