1 // RUN: %clang_cc1 -triple csky -target-feature +fpuv2_sf -target-feature +fpuv2_df -target-feature +hard-float-abi -target-feature +hard-float -emit-llvm %s -o - | FileCheck %s
2
3 #include <stdint.h>
4
5 // Verify that the tracking of used GPRs and FPRs works correctly by checking
6 // that small integers are sign/zero extended when passed in registers.
7
8 // Doubles are passed in FPRs, so argument 'i' will be passed zero-extended
9 // because it will be passed in a GPR.
10
11 // CHECK: define{{.*}} void @f_fpr_tracking(double noundef %a, double noundef %b, double noundef %c, double noundef %d, i8 noundef zeroext %i)
f_fpr_tracking(double a,double b,double c,double d,uint8_t i)12 void f_fpr_tracking(double a, double b, double c, double d, uint8_t i) {}
13
14 // A struct containing just one floating-point real is passed as though it
15 // were a standalone floating-point real.
16 struct double_s {
17 double f;
18 };
19
20 // CHECK: define{{.*}} void @f_double_s_arg(double %a.coerce)
f_double_s_arg(struct double_s a)21 void f_double_s_arg(struct double_s a) {}
22
23 // CHECK: define{{.*}} double @f_ret_double_s()
f_ret_double_s(void)24 struct double_s f_ret_double_s(void) {
25 return (struct double_s){1.0};
26 }
27
28 // A struct containing a double and any number of zero-width bitfields is
29 // passed as though it were a standalone floating-point real.
30
31 struct zbf_double_s {
32 int : 0;
33 double f;
34 };
35 struct zbf_double_zbf_s {
36 int : 0;
37 double f;
38 int : 0;
39 };
40
41 // CHECK: define{{.*}} void @f_zbf_double_s_arg(double %a.coerce)
f_zbf_double_s_arg(struct zbf_double_s a)42 void f_zbf_double_s_arg(struct zbf_double_s a) {}
43
44 // CHECK: define{{.*}} double @f_ret_zbf_double_s()
f_ret_zbf_double_s(void)45 struct zbf_double_s f_ret_zbf_double_s(void) {
46 return (struct zbf_double_s){1.0};
47 }
48
49 // CHECK: define{{.*}} void @f_zbf_double_zbf_s_arg(double %a.coerce)
f_zbf_double_zbf_s_arg(struct zbf_double_zbf_s a)50 void f_zbf_double_zbf_s_arg(struct zbf_double_zbf_s a) {}
51
52 // CHECK: define{{.*}} double @f_ret_zbf_double_zbf_s()
f_ret_zbf_double_zbf_s(void)53 struct zbf_double_zbf_s f_ret_zbf_double_zbf_s(void) {
54 return (struct zbf_double_zbf_s){1.0};
55 }
56
57 // For argument type, the first 4*XLen parts of aggregate will be passed
58 // in registers, and the rest will be passed in stack.
59 // So we can coerce to integers directly and let backend handle it correctly.
60 // For return type, aggregate which <= 2*XLen will be returned in registers.
61 // Otherwise, aggregate will be returned indirectly.
62
63 struct double_double_s {
64 double f;
65 double g;
66 };
67 struct double_float_s {
68 double f;
69 float g;
70 };
71
72 // CHECK: define{{.*}} void @f_double_double_s_arg([4 x i32] %a.coerce)
f_double_double_s_arg(struct double_double_s a)73 void f_double_double_s_arg(struct double_double_s a) {}
74
75 // CHECK: define{{.*}} void @f_ret_double_double_s(ptr dead_on_unwind noalias writable sret(%struct.double_double_s) align 4 %agg.result)
f_ret_double_double_s(void)76 struct double_double_s f_ret_double_double_s(void) {
77 return (struct double_double_s){1.0, 2.0};
78 }
79
80 // CHECK: define{{.*}} void @f_double_float_s_arg([3 x i32] %a.coerce)
f_double_float_s_arg(struct double_float_s a)81 void f_double_float_s_arg(struct double_float_s a) {}
82
83 // CHECK: define{{.*}} void @f_ret_double_float_s(ptr dead_on_unwind noalias writable sret(%struct.double_float_s) align 4 %agg.result)
f_ret_double_float_s(void)84 struct double_float_s f_ret_double_float_s(void) {
85 return (struct double_float_s){1.0, 2.0};
86 }
87
88 // CHECK: define{{.*}} void @f_double_double_s_arg_insufficient_fprs(float noundef %a, double noundef %b, double noundef %c, double noundef %d, double noundef %e, double noundef %f, double noundef %g, double noundef %i, [4 x i32] %h.coerce)
f_double_double_s_arg_insufficient_fprs(float a,double b,double c,double d,double e,double f,double g,double i,struct double_double_s h)89 void f_double_double_s_arg_insufficient_fprs(float a, double b, double c, double d,
90 double e, double f, double g, double i, struct double_double_s h) {}
91
92 struct double_int8_s {
93 double f;
94 int8_t i;
95 };
96 struct double_uint8_s {
97 double f;
98 uint8_t i;
99 };
100 struct double_int32_s {
101 double f;
102 int32_t i;
103 };
104 struct double_int64_s {
105 double f;
106 int64_t i;
107 };
108 struct double_int64bf_s {
109 double f;
110 int64_t i : 32;
111 };
112 struct double_int8_zbf_s {
113 double f;
114 int8_t i;
115 int : 0;
116 };
117
118 // CHECK: define{{.*}} @f_double_int8_s_arg([3 x i32] %a.coerce)
f_double_int8_s_arg(struct double_int8_s a)119 void f_double_int8_s_arg(struct double_int8_s a) {}
120
121 // CHECK: define{{.*}} void @f_ret_double_int8_s(ptr dead_on_unwind noalias writable sret(%struct.double_int8_s) align 4 %agg.result)
f_ret_double_int8_s(void)122 struct double_int8_s f_ret_double_int8_s(void) {
123 return (struct double_int8_s){1.0, 2};
124 }
125
126 // CHECK: define{{.*}} void @f_double_uint8_s_arg([3 x i32] %a.coerce)
f_double_uint8_s_arg(struct double_uint8_s a)127 void f_double_uint8_s_arg(struct double_uint8_s a) {}
128
129 // CHECK: define{{.*}} void @f_ret_double_uint8_s(ptr dead_on_unwind noalias writable sret(%struct.double_uint8_s) align 4 %agg.result)
f_ret_double_uint8_s(void)130 struct double_uint8_s f_ret_double_uint8_s(void) {
131 return (struct double_uint8_s){1.0, 2};
132 }
133
134 // CHECK: define{{.*}} void @f_double_int32_s_arg([3 x i32] %a.coerce)
f_double_int32_s_arg(struct double_int32_s a)135 void f_double_int32_s_arg(struct double_int32_s a) {}
136
137 // CHECK: define{{.*}} void @f_ret_double_int32_s(ptr dead_on_unwind noalias writable sret(%struct.double_int32_s) align 4 %agg.result)
f_ret_double_int32_s(void)138 struct double_int32_s f_ret_double_int32_s(void) {
139 return (struct double_int32_s){1.0, 2};
140 }
141
142 // CHECK: define{{.*}} void @f_double_int64_s_arg([4 x i32] %a.coerce)
f_double_int64_s_arg(struct double_int64_s a)143 void f_double_int64_s_arg(struct double_int64_s a) {}
144
145 // CHECK: define{{.*}} void @f_ret_double_int64_s(ptr dead_on_unwind noalias writable sret(%struct.double_int64_s) align 4 %agg.result)
f_ret_double_int64_s(void)146 struct double_int64_s f_ret_double_int64_s(void) {
147 return (struct double_int64_s){1.0, 2};
148 }
149
150 // CHECK: define{{.*}} void @f_double_int64bf_s_arg([3 x i32] %a.coerce)
f_double_int64bf_s_arg(struct double_int64bf_s a)151 void f_double_int64bf_s_arg(struct double_int64bf_s a) {}
152
153 // CHECK: define{{.*}} void @f_ret_double_int64bf_s(ptr dead_on_unwind noalias writable sret(%struct.double_int64bf_s) align 4 %agg.result)
f_ret_double_int64bf_s(void)154 struct double_int64bf_s f_ret_double_int64bf_s(void) {
155 return (struct double_int64bf_s){1.0, 2};
156 }
157
158 // CHECK: define{{.*}} void @f_double_int8_zbf_s([3 x i32] %a.coerce)
f_double_int8_zbf_s(struct double_int8_zbf_s a)159 void f_double_int8_zbf_s(struct double_int8_zbf_s a) {}
160
161 // CHECK: define{{.*}} void @f_ret_double_int8_zbf_s(ptr dead_on_unwind noalias writable sret(%struct.double_int8_zbf_s) align 4 %agg.result)
f_ret_double_int8_zbf_s(void)162 struct double_int8_zbf_s f_ret_double_int8_zbf_s(void) {
163 return (struct double_int8_zbf_s){1.0, 2};
164 }
165
166 // CHECK: define{{.*}} void @f_double_int8_s_arg_insufficient_gprs(i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d, i32 noundef %e, i32 noundef %f, i32 noundef %g, i32 noundef %h, [3 x i32] %i.coerce)
f_double_int8_s_arg_insufficient_gprs(int a,int b,int c,int d,int e,int f,int g,int h,struct double_int8_s i)167 void f_double_int8_s_arg_insufficient_gprs(int a, int b, int c, int d, int e,
168 int f, int g, int h, struct double_int8_s i) {}
169
170 // CHECK: define{{.*}} void @f_struct_double_int8_insufficient_fprs(float noundef %a, double noundef %b, double noundef %c, double noundef %d, double noundef %e, double noundef %f, double noundef %g, double noundef %h, [3 x i32] %i.coerce)
f_struct_double_int8_insufficient_fprs(float a,double b,double c,double d,double e,double f,double g,double h,struct double_int8_s i)171 void f_struct_double_int8_insufficient_fprs(float a, double b, double c, double d,
172 double e, double f, double g, double h, struct double_int8_s i) {}
173
174 // Complex floating-point values are special in passing argument,
175 // and it's not same as structs containing a single complex.
176 // Complex floating-point value should be passed in two consecutive fprs.
177 // But the return process is same as struct.
178
179 // CHECK: define{{.*}} void @f_doublecomplex(double noundef %a.coerce0, double noundef %a.coerce1)
f_doublecomplex(double __complex__ a)180 void f_doublecomplex(double __complex__ a) {}
181
182 // CHECK: define{{.*}} void @f_ret_doublecomplex(ptr dead_on_unwind noalias writable sret({ double, double }) align 4 %agg.result)
f_ret_doublecomplex(void)183 double __complex__ f_ret_doublecomplex(void) {
184 return 1.0;
185 }
186
187 struct doublecomplex_s {
188 double __complex__ c;
189 };
190
191 // CHECK: define{{.*}} void @f_doublecomplex_s_arg([4 x i32] %a.coerce)
f_doublecomplex_s_arg(struct doublecomplex_s a)192 void f_doublecomplex_s_arg(struct doublecomplex_s a) {}
193
194 // CHECK: define{{.*}} void @f_ret_doublecomplex_s(ptr dead_on_unwind noalias writable sret(%struct.doublecomplex_s) align 4 %agg.result)
f_ret_doublecomplex_s(void)195 struct doublecomplex_s f_ret_doublecomplex_s(void) {
196 return (struct doublecomplex_s){1.0};
197 }
198
199 // Test single or two-element structs that need flattening. e.g. those
200 // containing nested structs, doubles in small arrays, zero-length structs etc.
201
202 struct doublearr1_s {
203 double a[1];
204 };
205
206 // CHECK: define{{.*}} void @f_doublearr1_s_arg(double %a.coerce)
f_doublearr1_s_arg(struct doublearr1_s a)207 void f_doublearr1_s_arg(struct doublearr1_s a) {}
208
209 // CHECK: define{{.*}} double @f_ret_doublearr1_s()
f_ret_doublearr1_s(void)210 struct doublearr1_s f_ret_doublearr1_s(void) {
211 return (struct doublearr1_s){{1.0}};
212 }
213
214 struct doublearr2_s {
215 double a[2];
216 };
217
218 // CHECK: define{{.*}} void @f_doublearr2_s_arg([4 x i32] %a.coerce)
f_doublearr2_s_arg(struct doublearr2_s a)219 void f_doublearr2_s_arg(struct doublearr2_s a) {}
220
221 // CHECK: define{{.*}} void @f_ret_doublearr2_s(ptr dead_on_unwind noalias writable sret(%struct.doublearr2_s) align 4 %agg.result)
f_ret_doublearr2_s(void)222 struct doublearr2_s f_ret_doublearr2_s(void) {
223 return (struct doublearr2_s){{1.0, 2.0}};
224 }
225
226 struct doublearr2_tricky1_s {
227 struct {
228 double f[1];
229 } g[2];
230 };
231
232 // CHECK: define{{.*}} void @f_doublearr2_tricky1_s_arg([4 x i32] %a.coerce)
f_doublearr2_tricky1_s_arg(struct doublearr2_tricky1_s a)233 void f_doublearr2_tricky1_s_arg(struct doublearr2_tricky1_s a) {}
234
235 // CHECK: define{{.*}} void @f_ret_doublearr2_tricky1_s(ptr dead_on_unwind noalias writable sret(%struct.doublearr2_tricky1_s) align 4 %agg.result)
f_ret_doublearr2_tricky1_s(void)236 struct doublearr2_tricky1_s f_ret_doublearr2_tricky1_s(void) {
237 return (struct doublearr2_tricky1_s){{{{1.0}}, {{2.0}}}};
238 }
239
240 struct doublearr2_tricky2_s {
241 struct {};
242 struct {
243 double f[1];
244 } g[2];
245 };
246
247 // CHECK: define{{.*}} void @f_doublearr2_tricky2_s_arg([4 x i32] %a.coerce)
f_doublearr2_tricky2_s_arg(struct doublearr2_tricky2_s a)248 void f_doublearr2_tricky2_s_arg(struct doublearr2_tricky2_s a) {}
249
250 // CHECK: define{{.*}} void @f_ret_doublearr2_tricky2_s(ptr dead_on_unwind noalias writable sret(%struct.doublearr2_tricky2_s) align 4 %agg.result)
f_ret_doublearr2_tricky2_s(void)251 struct doublearr2_tricky2_s f_ret_doublearr2_tricky2_s(void) {
252 return (struct doublearr2_tricky2_s){{}, {{{1.0}}, {{2.0}}}};
253 }
254
255 struct doublearr2_tricky3_s {
256 union {};
257 struct {
258 double f[1];
259 } g[2];
260 };
261
262 // CHECK: define{{.*}} void @f_doublearr2_tricky3_s_arg([4 x i32] %a.coerce)
f_doublearr2_tricky3_s_arg(struct doublearr2_tricky3_s a)263 void f_doublearr2_tricky3_s_arg(struct doublearr2_tricky3_s a) {}
264
265 // CHECK: define{{.*}} void @f_ret_doublearr2_tricky3_s(ptr dead_on_unwind noalias writable sret(%struct.doublearr2_tricky3_s) align 4 %agg.result)
f_ret_doublearr2_tricky3_s(void)266 struct doublearr2_tricky3_s f_ret_doublearr2_tricky3_s(void) {
267 return (struct doublearr2_tricky3_s){{}, {{{1.0}}, {{2.0}}}};
268 }
269
270 struct doublearr2_tricky4_s {
271 union {};
272 struct {
273 struct {};
274 double f[1];
275 } g[2];
276 };
277
278 // CHECK: define{{.*}} void @f_doublearr2_tricky4_s_arg([4 x i32] %a.coerce)
f_doublearr2_tricky4_s_arg(struct doublearr2_tricky4_s a)279 void f_doublearr2_tricky4_s_arg(struct doublearr2_tricky4_s a) {}
280
281 // CHECK: define{{.*}} void @f_ret_doublearr2_tricky4_s(ptr dead_on_unwind noalias writable sret(%struct.doublearr2_tricky4_s) align 4 %agg.result)
f_ret_doublearr2_tricky4_s(void)282 struct doublearr2_tricky4_s f_ret_doublearr2_tricky4_s(void) {
283 return (struct doublearr2_tricky4_s){{}, {{{}, {1.0}}, {{}, {2.0}}}};
284 }
285
286 struct int_double_int_s {
287 int a;
288 double b;
289 int c;
290 };
291
292 // CHECK: define{{.*}} void @f_int_double_int_s_arg([4 x i32] %a.coerce)
f_int_double_int_s_arg(struct int_double_int_s a)293 void f_int_double_int_s_arg(struct int_double_int_s a) {}
294
295 // CHECK: define{{.*}} void @f_ret_int_double_int_s(ptr dead_on_unwind noalias writable sret(%struct.int_double_int_s) align 4 %agg.result)
f_ret_int_double_int_s(void)296 struct int_double_int_s f_ret_int_double_int_s(void) {
297 return (struct int_double_int_s){1, 2.0, 3};
298 }
299
300 struct int64_double_s {
301 int64_t a;
302 double b;
303 };
304
305 // CHECK: define{{.*}} void @f_int64_double_s_arg([4 x i32] %a.coerce)
f_int64_double_s_arg(struct int64_double_s a)306 void f_int64_double_s_arg(struct int64_double_s a) {}
307
308 // CHECK: define{{.*}} void @f_ret_int64_double_s(ptr dead_on_unwind noalias writable sret(%struct.int64_double_s) align 4 %agg.result)
f_ret_int64_double_s(void)309 struct int64_double_s f_ret_int64_double_s(void) {
310 return (struct int64_double_s){1, 2.0};
311 }
312
313 struct char_char_double_s {
314 char a;
315 char b;
316 double c;
317 };
318
319 // CHECK-LABEL: define{{.*}} void @f_char_char_double_s_arg([3 x i32] %a.coerce)
f_char_char_double_s_arg(struct char_char_double_s a)320 void f_char_char_double_s_arg(struct char_char_double_s a) {}
321
322 // CHECK: define{{.*}} void @f_ret_char_char_double_s(ptr dead_on_unwind noalias writable sret(%struct.char_char_double_s) align 4 %agg.result)
f_ret_char_char_double_s(void)323 struct char_char_double_s f_ret_char_char_double_s(void) {
324 return (struct char_char_double_s){1, 2, 3.0};
325 }
326
327 // A union containing just one floating-point real can not be passed as though it
328 // were a standalone floating-point real.
329 union double_u {
330 double a;
331 };
332
333 // CHECK: define{{.*}} void @f_double_u_arg([2 x i32] %a.coerce)
f_double_u_arg(union double_u a)334 void f_double_u_arg(union double_u a) {}
335
336 // CHECK: define{{.*}} [2 x i32] @f_ret_double_u()
f_ret_double_u(void)337 union double_u f_ret_double_u(void) {
338 return (union double_u){1.0};
339 }
340
341 // CHECK: define{{.*}} void @f_ret_double_int32_s_double_int32_s_just_sufficient_gprs(ptr dead_on_unwind noalias writable sret(%struct.double_int32_s) align 4 %agg.result, i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d, i32 noundef %e, i32 noundef %f, i32 noundef %g, [3 x i32] %h.coerce)
f_ret_double_int32_s_double_int32_s_just_sufficient_gprs(int a,int b,int c,int d,int e,int f,int g,struct double_int32_s h)342 struct double_int32_s f_ret_double_int32_s_double_int32_s_just_sufficient_gprs(
343 int a, int b, int c, int d, int e, int f, int g, struct double_int32_s h) {
344 return (struct double_int32_s){1.0, 2};
345 }
346
347 // CHECK: define{{.*}} void @f_ret_double_double_s_double_int32_s_just_sufficient_gprs(ptr dead_on_unwind noalias writable sret(%struct.double_double_s) align 4 %agg.result, i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d, i32 noundef %e, i32 noundef %f, i32 noundef %g, [3 x i32] %h.coerce)
f_ret_double_double_s_double_int32_s_just_sufficient_gprs(int a,int b,int c,int d,int e,int f,int g,struct double_int32_s h)348 struct double_double_s f_ret_double_double_s_double_int32_s_just_sufficient_gprs(
349 int a, int b, int c, int d, int e, int f, int g, struct double_int32_s h) {
350 return (struct double_double_s){1.0, 2.0};
351 }
352
353 // CHECK: define{{.*}} void @f_ret_doublecomplex_double_int32_s_just_sufficient_gprs(ptr dead_on_unwind noalias writable sret({ double, double }) align 4 %agg.result, i32 noundef %a, i32 noundef %b, i32 noundef %c, i32 noundef %d, i32 noundef %e, i32 noundef %f, i32 noundef %g, [3 x i32] %h.coerce)
f_ret_doublecomplex_double_int32_s_just_sufficient_gprs(int a,int b,int c,int d,int e,int f,int g,struct double_int32_s h)354 double __complex__ f_ret_doublecomplex_double_int32_s_just_sufficient_gprs(
355 int a, int b, int c, int d, int e, int f, int g, struct double_int32_s h) {
356 return 1.0;
357 }
358
359 struct tiny {
360 uint8_t a, b, c, d;
361 };
362
363 struct small {
364 int32_t a, *b;
365 };
366
367 struct small_aligned {
368 int64_t a;
369 };
370
371 struct large {
372 int32_t a, b, c, d;
373 };
374
375 // Ensure that scalars passed on the stack are still determined correctly in
376 // the presence of large return values that consume a register due to the need
377 // to pass a pointer.
378
379 // CHECK-LABEL: define{{.*}} void @f_scalar_stack_2(ptr dead_on_unwind noalias writable sret(%struct.large) align 4 %agg.result, float noundef %a, i64 noundef %b, double noundef %c, double noundef %d, i8 noundef zeroext %e, i8 noundef signext %f, i8 noundef zeroext %g)
f_scalar_stack_2(float a,int64_t b,double c,long double d,uint8_t e,int8_t f,uint8_t g)380 struct large f_scalar_stack_2(float a, int64_t b, double c, long double d,
381 uint8_t e, int8_t f, uint8_t g) {
382 return (struct large){a, e, f, g};
383 }
384
385 // Aggregates and >=XLen scalars passed on the stack should be lowered just as
386 // they would be if passed via registers.
387
388 // CHECK-LABEL: define{{.*}} void @f_scalar_stack_3(double noundef %a, i64 noundef %b, double noundef %c, i64 noundef %d, i32 noundef %e, i64 noundef %f, float noundef %g, double noundef %h, double noundef %i)
f_scalar_stack_3(double a,int64_t b,double c,int64_t d,int e,int64_t f,float g,double h,long double i)389 void f_scalar_stack_3(double a, int64_t b, double c, int64_t d, int e,
390 int64_t f, float g, double h, long double i) {}
391
392 // CHECK-LABEL: define{{.*}} void @f_agg_stack(double noundef %a, i64 noundef %b, double noundef %c, i64 noundef %d, i32 %e.coerce, [2 x i32] %f.coerce, i64 %g.coerce, [4 x i32] %h.coerce)
f_agg_stack(double a,int64_t b,double c,int64_t d,struct tiny e,struct small f,struct small_aligned g,struct large h)393 void f_agg_stack(double a, int64_t b, double c, int64_t d, struct tiny e,
394 struct small f, struct small_aligned g, struct large h) {}
395