1// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-linux | FileCheck -check-prefix LINUX %s
2// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fno-rtti | FileCheck -check-prefix WIN32 %s
3// RUN: %clang_cc1 -emit-llvm %s -o - -triple=x86_64-pc-win32 -mconstructor-aliases -fno-rtti | FileCheck -check-prefix WIN64 %s
4
5struct Empty {};
6
7struct EmptyWithCtor {
8  EmptyWithCtor() {}
9};
10
11struct Small {
12  int x;
13};
14
15// This is a C++11 trivial and standard-layout struct but not a C++03 POD.
16struct SmallCpp11NotCpp03Pod : Empty {
17  int x;
18};
19
20struct SmallWithCtor {
21  SmallWithCtor() {}
22  int x;
23};
24
25struct SmallWithDtor {
26  SmallWithDtor();
27  ~SmallWithDtor();
28  int x;
29};
30
31struct SmallWithVftable {
32  int x;
33  virtual void foo();
34};
35
36struct Medium {
37  int x, y;
38};
39
40struct MediumWithCopyCtor {
41  MediumWithCopyCtor();
42  MediumWithCopyCtor(const struct MediumWithCopyCtor &);
43  int x, y;
44};
45
46struct Big {
47  int a, b, c, d, e, f;
48};
49
50struct BigWithDtor {
51  BigWithDtor();
52  ~BigWithDtor();
53  int a, b, c, d, e, f;
54};
55
56// WIN32: declare void @"{{.*take_bools_and_chars.*}}"
57// WIN32:       (<{ i8, [3 x i8], i8, [3 x i8], %struct.SmallWithDtor,
58// WIN32:           i8, [3 x i8], i8, [3 x i8], i32, i8, [3 x i8] }>* inalloca)
59void take_bools_and_chars(char a, char b, SmallWithDtor c, char d, bool e, int f, bool g);
60void call_bools_and_chars() {
61  take_bools_and_chars('A', 'B', SmallWithDtor(), 'D', true, 13, false);
62}
63
64// Returning structs that fit into a register.
65Small small_return() { return Small(); }
66// LINUX-LABEL: define void @_Z12small_returnv(%struct.Small* noalias sret %agg.result)
67// WIN32: define i32 @"\01?small_return@@YA?AUSmall@@XZ"()
68// WIN64: define i32 @"\01?small_return@@YA?AUSmall@@XZ"()
69
70Medium medium_return() { return Medium(); }
71// LINUX-LABEL: define void @_Z13medium_returnv(%struct.Medium* noalias sret %agg.result)
72// WIN32: define i64 @"\01?medium_return@@YA?AUMedium@@XZ"()
73// WIN64: define i64 @"\01?medium_return@@YA?AUMedium@@XZ"()
74
75// Returning structs that fit into a register but are not POD.
76SmallCpp11NotCpp03Pod small_non_pod_return() { return SmallCpp11NotCpp03Pod(); }
77// LINUX-LABEL: define void @_Z20small_non_pod_returnv(%struct.SmallCpp11NotCpp03Pod* noalias sret %agg.result)
78// WIN32: define void @"\01?small_non_pod_return@@YA?AUSmallCpp11NotCpp03Pod@@XZ"(%struct.SmallCpp11NotCpp03Pod* noalias sret %agg.result)
79// WIN64: define void @"\01?small_non_pod_return@@YA?AUSmallCpp11NotCpp03Pod@@XZ"(%struct.SmallCpp11NotCpp03Pod* noalias sret %agg.result)
80
81SmallWithCtor small_with_ctor_return() { return SmallWithCtor(); }
82// LINUX-LABEL: define void @_Z22small_with_ctor_returnv(%struct.SmallWithCtor* noalias sret %agg.result)
83// WIN32: define void @"\01?small_with_ctor_return@@YA?AUSmallWithCtor@@XZ"(%struct.SmallWithCtor* noalias sret %agg.result)
84// WIN64: define void @"\01?small_with_ctor_return@@YA?AUSmallWithCtor@@XZ"(%struct.SmallWithCtor* noalias sret %agg.result)
85
86SmallWithVftable small_with_vftable_return() { return SmallWithVftable(); }
87// LINUX-LABEL: define void @_Z25small_with_vftable_returnv(%struct.SmallWithVftable* noalias sret %agg.result)
88// WIN32: define void @"\01?small_with_vftable_return@@YA?AUSmallWithVftable@@XZ"(%struct.SmallWithVftable* noalias sret %agg.result)
89// WIN64: define void @"\01?small_with_vftable_return@@YA?AUSmallWithVftable@@XZ"(%struct.SmallWithVftable* noalias sret %agg.result)
90
91MediumWithCopyCtor medium_with_copy_ctor_return() { return MediumWithCopyCtor(); }
92// LINUX-LABEL: define void @_Z28medium_with_copy_ctor_returnv(%struct.MediumWithCopyCtor* noalias sret %agg.result)
93// WIN32: define void @"\01?medium_with_copy_ctor_return@@YA?AUMediumWithCopyCtor@@XZ"(%struct.MediumWithCopyCtor* noalias sret %agg.result)
94// WIN64: define void @"\01?medium_with_copy_ctor_return@@YA?AUMediumWithCopyCtor@@XZ"(%struct.MediumWithCopyCtor* noalias sret %agg.result)
95
96// Returning a large struct that doesn't fit into a register.
97Big big_return() { return Big(); }
98// LINUX-LABEL: define void @_Z10big_returnv(%struct.Big* noalias sret %agg.result)
99// WIN32: define void @"\01?big_return@@YA?AUBig@@XZ"(%struct.Big* noalias sret %agg.result)
100// WIN64: define void @"\01?big_return@@YA?AUBig@@XZ"(%struct.Big* noalias sret %agg.result)
101
102
103void small_arg(Small s) {}
104// LINUX-LABEL: define void @_Z9small_arg5Small(i32 %s.0)
105// WIN32: define void @"\01?small_arg@@YAXUSmall@@@Z"(%struct.Small* byval align 4 %s)
106// WIN64: define void @"\01?small_arg@@YAXUSmall@@@Z"(i32 %s.coerce)
107
108void medium_arg(Medium s) {}
109// LINUX-LABEL: define void @_Z10medium_arg6Medium(i32 %s.0, i32 %s.1)
110// WIN32: define void @"\01?medium_arg@@YAXUMedium@@@Z"(%struct.Medium* byval align 4 %s)
111// WIN64: define void @"\01?medium_arg@@YAXUMedium@@@Z"(i64 %s.coerce)
112
113void small_arg_with_ctor(SmallWithCtor s) {}
114// LINUX-LABEL: define void @_Z19small_arg_with_ctor13SmallWithCtor(%struct.SmallWithCtor* byval align 4 %s)
115// WIN32: define void @"\01?small_arg_with_ctor@@YAXUSmallWithCtor@@@Z"(%struct.SmallWithCtor* byval align 4 %s)
116// WIN64: define void @"\01?small_arg_with_ctor@@YAXUSmallWithCtor@@@Z"(i32 %s.coerce)
117
118// Test that dtors are invoked in the callee.
119void small_arg_with_dtor(SmallWithDtor s) {}
120// WIN32: define void @"\01?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(<{ %struct.SmallWithDtor }>* inalloca) {{.*}} {
121// WIN32:   call x86_thiscallcc void @"\01??1SmallWithDtor@@QAE@XZ"
122// WIN32: }
123// WIN64: define void @"\01?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(i32 %s.coerce) {{.*}} {
124// WIN64:   call void @"\01??1SmallWithDtor@@QEAA@XZ"
125// WIN64: }
126
127void call_small_arg_with_dtor() {
128  small_arg_with_dtor(SmallWithDtor());
129}
130// The temporary is copied, so it's destroyed in the caller as well as the
131// callee.
132// WIN64-LABEL: define void @"\01?call_small_arg_with_dtor@@YAXXZ"()
133// WIN64:   call %struct.SmallWithDtor* @"\01??0SmallWithDtor@@QEAA@XZ"
134// WIN64:   call void @"\01?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(i32 %{{.*}})
135// WIN64:   call void @"\01??1SmallWithDtor@@QEAA@XZ"
136// WIN64:   ret void
137
138// Test that references aren't destroyed in the callee.
139void ref_small_arg_with_dtor(const SmallWithDtor &s) { }
140// WIN32: define void @"\01?ref_small_arg_with_dtor@@YAXABUSmallWithDtor@@@Z"(%struct.SmallWithDtor* dereferenceable({{[0-9]+}}) %s) {{.*}} {
141// WIN32-NOT:   call x86_thiscallcc void @"\01??1SmallWithDtor@@QAE@XZ"
142// WIN32: }
143// WIN64-LABEL: define void @"\01?ref_small_arg_with_dtor@@YAXAEBUSmallWithDtor@@@Z"(%struct.SmallWithDtor* dereferenceable({{[0-9]+}}) %s)
144
145void big_arg_with_dtor(BigWithDtor s) {}
146// WIN64-LABEL: define void @"\01?big_arg_with_dtor@@YAXUBigWithDtor@@@Z"(%struct.BigWithDtor* %s)
147// WIN64:   call void @"\01??1BigWithDtor@@QEAA@XZ"
148// WIN64: }
149
150void call_big_arg_with_dtor() {
151  big_arg_with_dtor(BigWithDtor());
152}
153// We can elide the copy of the temporary in the caller, because this object is
154// larger than 8 bytes and is passed indirectly.
155// WIN64-LABEL: define void @"\01?call_big_arg_with_dtor@@YAXXZ"()
156// WIN64:   call %struct.BigWithDtor* @"\01??0BigWithDtor@@QEAA@XZ"
157// WIN64:   call void @"\01?big_arg_with_dtor@@YAXUBigWithDtor@@@Z"(%struct.BigWithDtor* %{{.*}})
158// WIN64-NOT: call void @"\01??1BigWithDtor@@QEAA@XZ"
159// WIN64:   ret void
160
161// Test that temporaries passed by reference are destroyed in the caller.
162void temporary_ref_with_dtor() {
163  ref_small_arg_with_dtor(SmallWithDtor());
164}
165// WIN32: define void @"\01?temporary_ref_with_dtor@@YAXXZ"() {{.*}} {
166// WIN32:   call x86_thiscallcc %struct.SmallWithDtor* @"\01??0SmallWithDtor@@QAE@XZ"
167// WIN32:   call void @"\01?ref_small_arg_with_dtor@@YAXABUSmallWithDtor@@@Z"
168// WIN32:   call x86_thiscallcc void @"\01??1SmallWithDtor@@QAE@XZ"
169// WIN32: }
170
171void takes_two_by_val_with_dtor(SmallWithDtor a, SmallWithDtor b);
172void eh_cleanup_arg_with_dtor() {
173  takes_two_by_val_with_dtor(SmallWithDtor(), SmallWithDtor());
174}
175//   When exceptions are off, we don't have any cleanups.  See
176//   microsoft-abi-exceptions.cpp for these cleanups.
177// WIN32: define void @"\01?eh_cleanup_arg_with_dtor@@YAXXZ"() {{.*}} {
178// WIN32:   call x86_thiscallcc %struct.SmallWithDtor* @"\01??0SmallWithDtor@@QAE@XZ"
179// WIN32:   call x86_thiscallcc %struct.SmallWithDtor* @"\01??0SmallWithDtor@@QAE@XZ"
180// WIN32:   call void @"\01?takes_two_by_val_with_dtor@@YAXUSmallWithDtor@@0@Z"
181// WIN32-NOT: call x86_thiscallcc void @"\01??1SmallWithDtor@@QAE@XZ"
182// WIN32: }
183
184void small_arg_with_vftable(SmallWithVftable s) {}
185// LINUX-LABEL: define void @_Z22small_arg_with_vftable16SmallWithVftable(%struct.SmallWithVftable* %s)
186// WIN32: define void @"\01?small_arg_with_vftable@@YAXUSmallWithVftable@@@Z"(<{ %struct.SmallWithVftable }>* inalloca)
187// WIN64: define void @"\01?small_arg_with_vftable@@YAXUSmallWithVftable@@@Z"(%struct.SmallWithVftable* %s)
188
189void medium_arg_with_copy_ctor(MediumWithCopyCtor s) {}
190// LINUX-LABEL: define void @_Z25medium_arg_with_copy_ctor18MediumWithCopyCtor(%struct.MediumWithCopyCtor* %s)
191// WIN32: define void @"\01?medium_arg_with_copy_ctor@@YAXUMediumWithCopyCtor@@@Z"(<{ %struct.MediumWithCopyCtor }>* inalloca)
192// WIN64: define void @"\01?medium_arg_with_copy_ctor@@YAXUMediumWithCopyCtor@@@Z"(%struct.MediumWithCopyCtor* %s)
193
194void big_arg(Big s) {}
195// LINUX-LABEL: define void @_Z7big_arg3Big(%struct.Big* byval align 4 %s)
196// WIN32: define void @"\01?big_arg@@YAXUBig@@@Z"(%struct.Big* byval align 4 %s)
197// WIN64: define void @"\01?big_arg@@YAXUBig@@@Z"(%struct.Big* %s)
198
199class Class {
200 public:
201  Small thiscall_method_small() { return Small(); }
202  // LINUX: define {{.*}} void @_ZN5Class21thiscall_method_smallEv(%struct.Small* noalias sret %agg.result, %class.Class* %this)
203  // WIN32: define {{.*}} x86_thiscallcc void @"\01?thiscall_method_small@Class@@QAE?AUSmall@@XZ"(%class.Class* %this, %struct.Small* noalias sret %agg.result)
204  // WIN64: define linkonce_odr void @"\01?thiscall_method_small@Class@@QEAA?AUSmall@@XZ"(%class.Class* %this, %struct.Small* noalias sret %agg.result)
205
206  SmallWithCtor thiscall_method_small_with_ctor() { return SmallWithCtor(); }
207  // LINUX: define {{.*}} void @_ZN5Class31thiscall_method_small_with_ctorEv(%struct.SmallWithCtor* noalias sret %agg.result, %class.Class* %this)
208  // WIN32: define {{.*}} x86_thiscallcc void @"\01?thiscall_method_small_with_ctor@Class@@QAE?AUSmallWithCtor@@XZ"(%class.Class* %this, %struct.SmallWithCtor* noalias sret %agg.result)
209  // WIN64: define linkonce_odr void @"\01?thiscall_method_small_with_ctor@Class@@QEAA?AUSmallWithCtor@@XZ"(%class.Class* %this, %struct.SmallWithCtor* noalias sret %agg.result)
210
211  Small __cdecl cdecl_method_small() { return Small(); }
212  // LINUX: define {{.*}} void @_ZN5Class18cdecl_method_smallEv(%struct.Small* noalias sret %agg.result, %class.Class* %this)
213  // WIN32: define {{.*}} void @"\01?cdecl_method_small@Class@@QAA?AUSmall@@XZ"(%class.Class* %this, %struct.Small* noalias sret %agg.result)
214  // WIN64: define linkonce_odr void @"\01?cdecl_method_small@Class@@QEAA?AUSmall@@XZ"(%class.Class* %this, %struct.Small* noalias sret %agg.result)
215
216  Big __cdecl cdecl_method_big() { return Big(); }
217  // LINUX: define {{.*}} void @_ZN5Class16cdecl_method_bigEv(%struct.Big* noalias sret %agg.result, %class.Class* %this)
218  // WIN32: define {{.*}} void @"\01?cdecl_method_big@Class@@QAA?AUBig@@XZ"(%class.Class* %this, %struct.Big* noalias sret %agg.result)
219  // WIN64: define linkonce_odr void @"\01?cdecl_method_big@Class@@QEAA?AUBig@@XZ"(%class.Class* %this, %struct.Big* noalias sret %agg.result)
220
221  void thiscall_method_arg(Empty s) {}
222  // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE5Empty(%class.Class* %this)
223  // WIN32: define {{.*}} void @"\01?thiscall_method_arg@Class@@QAEXUEmpty@@@Z"(%class.Class* %this, %struct.Empty* byval align 4 %s)
224  // WIN64: define linkonce_odr void @"\01?thiscall_method_arg@Class@@QEAAXUEmpty@@@Z"(%class.Class* %this, i8 %s.coerce)
225
226  void thiscall_method_arg(EmptyWithCtor s) {}
227  // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE13EmptyWithCtor(%class.Class* %this)
228  // WIN32: define {{.*}} void @"\01?thiscall_method_arg@Class@@QAEXUEmptyWithCtor@@@Z"(%class.Class* %this, %struct.EmptyWithCtor* byval align 4 %s)
229  // WIN64: define linkonce_odr void @"\01?thiscall_method_arg@Class@@QEAAXUEmptyWithCtor@@@Z"(%class.Class* %this, i8 %s.coerce)
230
231  void thiscall_method_arg(Small s) {}
232  // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE5Small(%class.Class* %this, i32 %s.0)
233  // WIN32: define {{.*}} void @"\01?thiscall_method_arg@Class@@QAEXUSmall@@@Z"(%class.Class* %this, %struct.Small* byval align 4 %s)
234  // WIN64: define linkonce_odr void @"\01?thiscall_method_arg@Class@@QEAAXUSmall@@@Z"(%class.Class* %this, i32 %s.coerce)
235
236  void thiscall_method_arg(SmallWithCtor s) {}
237  // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE13SmallWithCtor(%class.Class* %this, %struct.SmallWithCtor* byval align 4 %s)
238  // WIN32: define {{.*}} void @"\01?thiscall_method_arg@Class@@QAEXUSmallWithCtor@@@Z"(%class.Class* %this, %struct.SmallWithCtor* byval align 4 %s)
239  // WIN64: define linkonce_odr void @"\01?thiscall_method_arg@Class@@QEAAXUSmallWithCtor@@@Z"(%class.Class* %this, i32 %s.coerce)
240
241  void thiscall_method_arg(Big s) {}
242  // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE3Big(%class.Class* %this, %struct.Big* byval align 4 %s)
243  // WIN32: define {{.*}} void @"\01?thiscall_method_arg@Class@@QAEXUBig@@@Z"(%class.Class* %this, %struct.Big* byval align 4 %s)
244  // WIN64: define linkonce_odr void @"\01?thiscall_method_arg@Class@@QEAAXUBig@@@Z"(%class.Class* %this, %struct.Big* %s)
245};
246
247void use_class() {
248  Class c;
249  c.thiscall_method_small();
250  c.thiscall_method_small_with_ctor();
251
252  c.cdecl_method_small();
253  c.cdecl_method_big();
254
255  c.thiscall_method_arg(Empty());
256  c.thiscall_method_arg(EmptyWithCtor());
257  c.thiscall_method_arg(Small());
258  c.thiscall_method_arg(SmallWithCtor());
259  c.thiscall_method_arg(Big());
260}
261
262struct X {
263  X();
264  ~X();
265};
266void g(X) {
267}
268// WIN32: define void @"\01?g@@YAXUX@@@Z"(<{ %struct.X, [3 x i8] }>* inalloca) {{.*}} {
269// WIN32:   call x86_thiscallcc void @"\01??1X@@QAE@XZ"(%struct.X* {{.*}})
270// WIN32: }
271void f() {
272  g(X());
273}
274// WIN32: define void @"\01?f@@YAXXZ"() {{.*}} {
275// WIN32-NOT: call {{.*}} @"\01??1X@@QAE@XZ"
276// WIN32: }
277
278
279namespace test2 {
280// We used to crash on this due to the mixture of POD byval and non-trivial
281// byval.
282
283struct NonTrivial {
284  NonTrivial();
285  NonTrivial(const NonTrivial &o);
286  ~NonTrivial();
287  int a;
288};
289struct POD { int b; };
290
291int foo(NonTrivial a, POD b);
292void bar() {
293  POD b;
294  b.b = 13;
295  int c = foo(NonTrivial(), b);
296}
297// WIN32-LABEL: define void @"\01?bar@test2@@YAXXZ"() {{.*}} {
298// WIN32:   %[[argmem:[^ ]*]] = alloca inalloca [[argmem_ty:<{ %"struct.test2::NonTrivial", %"struct.test2::POD" }>]]
299// WIN32:   getelementptr inbounds [[argmem_ty]], [[argmem_ty]]* %[[argmem]], i32 0, i32 1
300// WIN32:   call void @llvm.memcpy
301// WIN32:   getelementptr inbounds [[argmem_ty]], [[argmem_ty]]* %[[argmem]], i32 0, i32 0
302// WIN32:   call x86_thiscallcc %"struct.test2::NonTrivial"* @"\01??0NonTrivial@test2@@QAE@XZ"
303// WIN32:   call i32 @"\01?foo@test2@@YAHUNonTrivial@1@UPOD@1@@Z"([[argmem_ty]]* inalloca %argmem)
304// WIN32:   ret void
305// WIN32: }
306
307}
308
309namespace test3 {
310
311// Check that we padded the inalloca struct to a multiple of 4.
312struct NonTrivial {
313  NonTrivial();
314  NonTrivial(const NonTrivial &o);
315  ~NonTrivial();
316  int a;
317};
318void foo(NonTrivial a, bool b) { }
319// WIN32-LABEL: define void @"\01?foo@test3@@YAXUNonTrivial@1@_N@Z"(<{ %"struct.test3::NonTrivial", i8, [3 x i8] }>* inalloca)
320
321}
322
323// We would crash here because the later definition of ForwardDeclare1 results
324// in a different IR type for the value we want to store.  However, the alloca's
325// type will use the argument type selected by fn1.
326struct ForwardDeclare1;
327
328typedef void (*FnPtr1)(ForwardDeclare1);
329void fn1(FnPtr1 a, SmallWithDtor b) { }
330
331struct ForwardDeclare1 {};
332
333void fn2(FnPtr1 a, SmallWithDtor b) { fn1(a, b); };
334// WIN32-LABEL: define void @"\01?fn2@@YAXP6AXUForwardDeclare1@@@ZUSmallWithDtor@@@Z"
335// WIN32:   %[[a:[^ ]*]] = getelementptr inbounds [[argmem_ty:<{ {}\*, %struct.SmallWithDtor }>]], [[argmem_ty:<{ {}\*, %struct.SmallWithDtor }>]]* %{{.*}}, i32 0, i32 0
336// WIN32:   %[[a1:[^ ]*]] = bitcast {}** %[[a]] to void [[dst_ty:\(%struct.ForwardDeclare1\*\)\*]]*
337// WIN32:   %[[argmem:[^ ]*]] = alloca inalloca [[argmem_ty]]
338// WIN32:   %[[gep1:[^ ]*]] = getelementptr inbounds [[argmem_ty]], [[argmem_ty]]* %[[argmem]], i32 0, i32 1
339// WIN32:   %[[bc1:[^ ]*]] = bitcast %struct.SmallWithDtor* %[[gep1]] to i8*
340// WIN32:   call void @llvm.memcpy.p0i8.p0i8.i32(i8* %[[bc1]], i8* {{.*}}, i32 4, i32 4, i1 false)
341// WIN32:   %[[a2:[^ ]*]] = load void [[dst_ty]], void [[dst_ty]]* %[[a1]], align 4
342// WIN32:   %[[gep2:[^ ]*]] = getelementptr inbounds [[argmem_ty]], [[argmem_ty]]* %[[argmem]], i32 0, i32 0
343// WIN32:   %[[addr:[^ ]*]] = bitcast {}** %[[gep2]] to void [[dst_ty]]*
344// WIN32:   store void [[dst_ty]] %[[a2]], void [[dst_ty]]* %[[addr]], align 4
345// WIN32:   call void @"\01?fn1@@YAXP6AXUForwardDeclare1@@@ZUSmallWithDtor@@@Z"([[argmem_ty]]* inalloca %[[argmem]])
346