1 // RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefix=PPC
2 // RUN: %clang_cc1 -mfloat-abi hard -triple armv7-unknown-linux-gnueabi -emit-llvm -o - %s | FileCheck %s --check-prefix=ARM32
3 // RUN: %clang_cc1 -mfloat-abi hard -triple aarch64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefix=ARM64
4 // RUN: %clang_cc1 -mfloat-abi hard -triple x86_64-unknown-windows-gnu -emit-llvm -o - %s | FileCheck %s --check-prefix=X64
5 // RUN: %clang_cc1 -mfloat-abi hard -triple aarch64-unknown-windows-msvc -emit-llvm -o - %s | FileCheck %s --check-prefix=WOA64
6
7 #if defined(__x86_64__)
8 #define CC __attribute__((vectorcall))
9 #else
10 #define CC
11 #endif
12
13 // Test that C++ classes are correctly classified as homogeneous aggregates.
14
15 struct Base1 {
16 int x;
17 };
18 struct Base2 {
19 double x;
20 };
21 struct Base3 {
22 double x;
23 };
24 struct D1 : Base1 { // non-homogeneous aggregate
25 double y, z;
26 };
27 struct D2 : Base2 { // homogeneous aggregate
28 double y, z;
29 };
30 struct D3 : Base1, Base2 { // non-homogeneous aggregate
31 double y, z;
32 };
33 struct D4 : Base2, Base3 { // homogeneous aggregate
34 double y, z;
35 };
36
37 struct I1 : Base2 {};
38 struct I2 : Base2 {};
39 struct I3 : Base2 {};
40 struct D5 : I1, I2, I3 {}; // homogeneous aggregate
41
42 // PPC: define{{.*}} void @_Z7func_D12D1(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, [3 x i64] %x.coerce)
43 // ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z7func_D12D1(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, [3 x i64] %x.coerce)
44 // ARM64: define{{.*}} void @_Z7func_D12D1(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, ptr noundef %x)
45 // X64: define dso_local x86_vectorcallcc void @"\01_Z7func_D12D1@@24"(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, ptr noundef %x)
func_D1(D1 x)46 D1 CC func_D1(D1 x) { return x; }
47
48 // PPC: define{{.*}} [3 x double] @_Z7func_D22D2([3 x double] %x.coerce)
49 // ARM32: define{{.*}} arm_aapcs_vfpcc %struct.D2 @_Z7func_D22D2(%struct.D2 %x.coerce)
50 // ARM64: define{{.*}} %struct.D2 @_Z7func_D22D2([3 x double] alignstack(8) %x.coerce)
51 // X64: define dso_local x86_vectorcallcc %struct.D2 @"\01_Z7func_D22D2@@24"(%struct.D2 inreg %x.coerce)
func_D2(D2 x)52 D2 CC func_D2(D2 x) { return x; }
53
54 // PPC: define{{.*}} void @_Z7func_D32D3(ptr dead_on_unwind noalias writable sret(%struct.D3) align 8 %agg.result, [4 x i64] %x.coerce)
55 // ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z7func_D32D3(ptr dead_on_unwind noalias writable sret(%struct.D3) align 8 %agg.result, [4 x i64] %x.coerce)
56 // ARM64: define{{.*}} void @_Z7func_D32D3(ptr dead_on_unwind noalias writable sret(%struct.D3) align 8 %agg.result, ptr noundef %x)
func_D3(D3 x)57 D3 CC func_D3(D3 x) { return x; }
58
59 // PPC: define{{.*}} [4 x double] @_Z7func_D42D4([4 x double] %x.coerce)
60 // ARM32: define{{.*}} arm_aapcs_vfpcc %struct.D4 @_Z7func_D42D4(%struct.D4 %x.coerce)
61 // ARM64: define{{.*}} %struct.D4 @_Z7func_D42D4([4 x double] alignstack(8) %x.coerce)
func_D4(D4 x)62 D4 CC func_D4(D4 x) { return x; }
63
func_D5(D5 x)64 D5 CC func_D5(D5 x) { return x; }
65 // PPC: define{{.*}} [3 x double] @_Z7func_D52D5([3 x double] %x.coerce)
66 // ARM32: define{{.*}} arm_aapcs_vfpcc %struct.D5 @_Z7func_D52D5(%struct.D5 %x.coerce)
67
68 // The C++ multiple inheritance expansion case is a little more complicated, so
69 // do some extra checking.
70 //
71 // ARM64-LABEL: define{{.*}} %struct.D5 @_Z7func_D52D5([3 x double] alignstack(8) %x.coerce)
72 // ARM64: store [3 x double] %x.coerce, ptr
73
call_D5(D5 * p)74 void call_D5(D5 *p) {
75 func_D5(*p);
76 }
77
78 // Check the call site.
79 //
80 // ARM64-LABEL: define{{.*}} void @_Z7call_D5P2D5(ptr noundef %p)
81 // ARM64: load [3 x double], ptr
82 // ARM64: call %struct.D5 @_Z7func_D52D5([3 x double] alignstack(8) %{{.*}})
83
84 struct Empty { };
85 struct Float1 { float x; };
86 struct Float2 { float y; };
87 struct HVAWithEmptyBase : Float1, Empty, Float2 { float z; };
88
89 // PPC: define{{.*}} void @_Z15with_empty_base16HVAWithEmptyBase([3 x float] %a.coerce)
90 // ARM64: define{{.*}} void @_Z15with_empty_base16HVAWithEmptyBase([3 x float] alignstack(8) %a.coerce)
91 // ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z15with_empty_base16HVAWithEmptyBase(%struct.HVAWithEmptyBase %a.coerce)
with_empty_base(HVAWithEmptyBase a)92 void CC with_empty_base(HVAWithEmptyBase a) {}
93
94 // WOA64: define dso_local void @"?with_empty_base@@YAXUHVAWithEmptyBase@@@Z"([2 x i64] %{{.*}})
95 // X64: define dso_local x86_vectorcallcc void @"\01_Z15with_empty_base16HVAWithEmptyBase@@16"(%struct.HVAWithEmptyBase inreg %a.coerce)
96
97 struct HVAWithEmptyBitField : Float1, Float2 {
98 int : 0; // Takes no space.
99 float z;
100 };
101
102 // PPC: define{{.*}} void @_Z19with_empty_bitfield20HVAWithEmptyBitField([3 x float] %a.coerce)
103 // ARM64: define{{.*}} void @_Z19with_empty_bitfield20HVAWithEmptyBitField([3 x float] alignstack(8) %a.coerce)
104 // ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z19with_empty_bitfield20HVAWithEmptyBitField(%struct.HVAWithEmptyBitField %a.coerce)
105 // X64: define dso_local x86_vectorcallcc void @"\01_Z19with_empty_bitfield20HVAWithEmptyBitField@@16"(%struct.HVAWithEmptyBitField inreg %a.coerce)
with_empty_bitfield(HVAWithEmptyBitField a)106 void CC with_empty_bitfield(HVAWithEmptyBitField a) {}
107
108 namespace pr47611 {
109 // MSVC on Arm includes "isCXX14Aggregate" as part of its definition of
110 // Homogeneous Floating-point Aggregate (HFA). Additionally, it has a different
111 // handling of C++14 aggregates, which can lead to confusion.
112
113 // Pod is a trivial HFA.
114 struct Pod {
115 double b[2];
116 };
117 // Not an aggregate according to C++14 spec => not HFA according to MSVC.
118 struct NotCXX14Aggregate {
119 NotCXX14Aggregate();
120 Pod p;
121 };
122 // NotPod is a C++14 aggregate. But not HFA, because it contains
123 // NotCXX14Aggregate (which itself is not HFA because it's not a C++14
124 // aggregate).
125 struct NotPod {
126 NotCXX14Aggregate x;
127 };
128 struct Empty {};
129 // A class with a base is returned using the sret calling convetion by MSVC.
130 struct HasEmptyBase : public Empty {
131 double b[2];
132 };
133 struct HasPodBase : public Pod {};
134 // WOA64-LABEL: define dso_local %"struct.pr47611::Pod" @"?copy@pr47611@@YA?AUPod@1@PEAU21@@Z"(ptr noundef %x)
copy(Pod * x)135 Pod copy(Pod *x) { return *x; } // MSVC: ldp d0,d1,[x0], Clang: ldp d0,d1,[x0]
136 // WOA64-LABEL: define dso_local void @"?copy@pr47611@@YA?AUNotCXX14Aggregate@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr47611::NotCXX14Aggregate") align 8 %agg.result, ptr noundef %x)
copy(NotCXX14Aggregate * x)137 NotCXX14Aggregate copy(NotCXX14Aggregate *x) { return *x; } // MSVC: stp x8,x9,[x0], Clang: str q0,[x0]
138 // WOA64-LABEL: define dso_local [2 x i64] @"?copy@pr47611@@YA?AUNotPod@1@PEAU21@@Z"(ptr noundef %x)
copy(NotPod * x)139 NotPod copy(NotPod *x) { return *x; }
140 // WOA64-LABEL: define dso_local void @"?copy@pr47611@@YA?AUHasEmptyBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr47611::HasEmptyBase") align 8 %agg.result, ptr noundef %x)
copy(HasEmptyBase * x)141 HasEmptyBase copy(HasEmptyBase *x) { return *x; }
142 // WOA64-LABEL: define dso_local void @"?copy@pr47611@@YA?AUHasPodBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr47611::HasPodBase") align 8 %agg.result, ptr noundef %x)
copy(HasPodBase * x)143 HasPodBase copy(HasPodBase *x) { return *x; }
144
call_copy_pod(Pod * pod)145 void call_copy_pod(Pod *pod) {
146 *pod = copy(pod);
147 // WOA64-LABEL: define dso_local void @"?call_copy_pod@pr47611@@YAXPEAUPod@1@@Z"
148 // WOA64: %{{.*}} = call %"struct.pr47611::Pod" @"?copy@pr47611@@YA?AUPod@1@PEAU21@@Z"(ptr noundef %{{.*}})
149 }
150
call_copy_notcxx14aggregate(NotCXX14Aggregate * notcxx14aggregate)151 void call_copy_notcxx14aggregate(NotCXX14Aggregate *notcxx14aggregate) {
152 *notcxx14aggregate = copy(notcxx14aggregate);
153 // WOA64-LABEL: define dso_local void @"?call_copy_notcxx14aggregate@pr47611@@YAXPEAUNotCXX14Aggregate@1@@Z"
154 // WOA64: call void @"?copy@pr47611@@YA?AUNotCXX14Aggregate@1@PEAU21@@Z"(ptr dead_on_unwind inreg writable sret(%"struct.pr47611::NotCXX14Aggregate") align 8 %{{.*}}, ptr noundef %{{.*}})
155 }
156
call_copy_notpod(NotPod * notPod)157 void call_copy_notpod(NotPod *notPod) {
158 *notPod = copy(notPod);
159 // WOA64-LABEL: define dso_local void @"?call_copy_notpod@pr47611@@YAXPEAUNotPod@1@@Z"
160 // WOA64: %{{.*}} = call [2 x i64] @"?copy@pr47611@@YA?AUNotPod@1@PEAU21@@Z"(ptr noundef %{{.*}})
161 }
162
call_copy_hasemptybase(HasEmptyBase * hasEmptyBase)163 void call_copy_hasemptybase(HasEmptyBase *hasEmptyBase) {
164 *hasEmptyBase = copy(hasEmptyBase);
165 // WOA64-LABEL: define dso_local void @"?call_copy_hasemptybase@pr47611@@YAXPEAUHasEmptyBase@1@@Z"
166 // WOA64: call void @"?copy@pr47611@@YA?AUHasEmptyBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg writable sret(%"struct.pr47611::HasEmptyBase") align 8 %{{.*}}, ptr noundef %{{.*}})
167 }
168
call_copy_haspodbase(HasPodBase * hasPodBase)169 void call_copy_haspodbase(HasPodBase *hasPodBase) {
170 *hasPodBase = copy(hasPodBase);
171 // WOA64-LABEL: define dso_local void @"?call_copy_haspodbase@pr47611@@YAXPEAUHasPodBase@1@@Z"
172 // WOA64: call void @"?copy@pr47611@@YA?AUHasPodBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg writable sret(%"struct.pr47611::HasPodBase") align 8 %{{.*}}, ptr noundef %{{.*}})
173 }
174 } // namespace pr47611
175
176 namespace protected_member {
177 struct HFA {
178 double x;
179 double y;
180 protected:
181 double z;
182 };
foo(HFA v)183 double foo(HFA v) { return v.x + v.y; }
184 // WOA64: define dso_local noundef double @"?foo@protected_member@@YANUHFA@1@@Z"([3 x double] %{{.*}})
185 }
186 namespace private_member {
187 struct HFA {
188 double x;
189 double y;
190 private:
191 double z;
192 };
foo(HFA v)193 double foo(HFA v) { return v.x + v.y; }
194 // WOA64: define dso_local noundef double @"?foo@private_member@@YANUHFA@1@@Z"([3 x double] %{{.*}})
195 }
196 namespace polymorphic {
197 struct NonHFA {
198 double x;
199 double y;
200 double z;
201 virtual void f1();
202 };
foo(NonHFA v)203 double foo(NonHFA v) { return v.x + v.y; }
204 // WOA64: define dso_local noundef double @"?foo@polymorphic@@YANUNonHFA@1@@Z"(ptr noundef %{{.*}})
205 }
206 namespace trivial_copy_assignment {
207 struct HFA {
208 double x;
209 double y;
210 double z;
211 HFA &operator=(const HFA&) = default;
212 };
foo(HFA v)213 double foo(HFA v) { return v.x + v.y; }
214 // WOA64: define dso_local noundef double @"?foo@trivial_copy_assignment@@YANUHFA@1@@Z"([3 x double] %{{.*}})
215 }
216 namespace non_trivial_copy_assignment {
217 struct NonHFA {
218 double x;
219 double y;
220 double z;
221 NonHFA &operator=(const NonHFA&);
222 };
foo(NonHFA v)223 double foo(NonHFA v) { return v.x + v.y; }
224 // WOA64: define dso_local noundef double @"?foo@non_trivial_copy_assignment@@YANUNonHFA@1@@Z"(ptr noundef %{{.*}})
225 }
226 namespace user_provided_ctor {
227 struct HFA {
228 double x;
229 double y;
230 double z;
231 HFA(int);
232 };
foo(HFA v)233 double foo(HFA v) { return v.x + v.y; }
234 // WOA64: define dso_local noundef double @"?foo@user_provided_ctor@@YANUHFA@1@@Z"([3 x double] %{{.*}})
235 }
236 namespace trivial_dtor {
237 struct HFA {
238 double x;
239 double y;
240 double z;
241 ~HFA() = default;
242 };
foo(HFA v)243 double foo(HFA v) { return v.x + v.y; }
244 // WOA64: define dso_local noundef double @"?foo@trivial_dtor@@YANUHFA@1@@Z"([3 x double] %{{.*}})
245 }
246 namespace non_trivial_dtor {
247 struct NonHFA {
248 double x;
249 double y;
250 double z;
251 ~NonHFA();
252 };
foo(NonHFA v)253 double foo(NonHFA v) { return v.x + v.y; }
254 // WOA64: define dso_local noundef double @"?foo@non_trivial_dtor@@YANUNonHFA@1@@Z"(ptr noundef %{{.*}})
255 }
256 namespace non_empty_base {
257 struct non_empty_base { double d; };
258 struct HFA : non_empty_base {
259 double x;
260 double y;
261 double z;
262 };
foo(HFA v)263 double foo(HFA v) { return v.x + v.y; }
264 // WOA64: define dso_local noundef double @"?foo@non_empty_base@@YANUHFA@1@@Z"([4 x double] %{{.*}})
265 }
266 namespace empty_field {
267 struct empty { };
268 struct NonHFA {
269 double x;
270 double y;
271 double z;
272 empty e;
273 };
foo(NonHFA v)274 double foo(NonHFA v) { return v.x + v.y; }
275 // WOA64: define dso_local noundef double @"?foo@empty_field@@YANUNonHFA@1@@Z"(ptr noundef %{{.*}})
276 }
277 namespace non_empty_field {
278 struct non_empty { double d; };
279 struct HFA {
280 double x;
281 double y;
282 double z;
283 non_empty e;
284 };
foo(HFA v)285 double foo(HFA v) { return v.x + v.y; }
286 // WOA64: define dso_local noundef double @"?foo@non_empty_field@@YANUHFA@1@@Z"([4 x double] %{{.*}})
287 }
288
289 namespace pr62223 {
290 // HVAs don't follow the normal rules for return values. That means they can
291 // have base classes, user-defined ctors, and protected/private members.
292 // (The same restrictions that apply to HVA arguments still apply.)
293 typedef double V __attribute((ext_vector_type(2)));
294 struct base { V v; };
295 struct test : base { test(double); protected: V v2;};
f(test * x)296 test f(test *x) { return *x; }
297 // WOA64: define dso_local %"struct.pr62223::test" @"?f@pr62223@@YA?AUtest@1@PEAU21@@Z"(ptr noundef %{{.*}})
298
299 // The above rule only apples to HVAs, not HFAs.
300 struct base2 { double v; };
301 struct test2 : base2 { test2(double); protected: double v2;};
f(test2 * x)302 test2 f(test2 *x) { return *x; }
303 // WOA64: define dso_local void @"?f@pr62223@@YA?AUtest2@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr62223::test2") align 8 %{{.*}}, ptr noundef %{{.*}})
304 }
305