1 // RUN: %clang_cc1 -triple riscv64 -target-feature +d -target-abi lp64d -emit-llvm %s -o - \
2 // RUN: | FileCheck %s
3
4 #include <stdint.h>
5
6 // Verify that the tracking of used GPRs and FPRs works correctly by checking
7 // that small integers are sign/zero extended when passed in registers.
8
9 // Doubles are passed in FPRs, so argument 'i' will be passed zero-extended
10 // because it will be passed in a GPR.
11
12 // CHECK: define void @f_fpr_tracking(double %a, double %b, double %c, double %d, double %e, double %f, double %g, double %h, i8 zeroext %i)
f_fpr_tracking(double a,double b,double c,double d,double e,double f,double g,double h,uint8_t i)13 void f_fpr_tracking(double a, double b, double c, double d, double e, double f,
14 double g, double h, uint8_t i) {}
15
16 // Check that fp, fp+fp, and int+fp structs are lowered correctly. These will
17 // be passed in FPR, FPR+FPR, or GPR+FPR regs if sufficient registers are
18 // available the widths are <= XLEN and FLEN, and should be expanded to
19 // separate arguments in IR. They are passed by the same rules for returns,
20 // but will be lowered to simple two-element structs if necessary (as LLVM IR
21 // functions cannot return multiple values).
22
23 // A struct containing just one floating-point real is passed as though it
24 // were a standalone floating-point real.
25
26 struct double_s { double f; };
27
28 // CHECK: define void @f_double_s_arg(double %0)
f_double_s_arg(struct double_s a)29 void f_double_s_arg(struct double_s a) {}
30
31 // CHECK: define double @f_ret_double_s()
f_ret_double_s()32 struct double_s f_ret_double_s() {
33 return (struct double_s){1.0};
34 }
35
36 // A struct containing a double and any number of zero-width bitfields is
37 // passed as though it were a standalone floating-point real.
38
39 struct zbf_double_s { int : 0; double f; };
40 struct zbf_double_zbf_s { int : 0; double f; int : 0; };
41
42 // CHECK: define void @f_zbf_double_s_arg(double %0)
f_zbf_double_s_arg(struct zbf_double_s a)43 void f_zbf_double_s_arg(struct zbf_double_s a) {}
44
45 // CHECK: define double @f_ret_zbf_double_s()
f_ret_zbf_double_s()46 struct zbf_double_s f_ret_zbf_double_s() {
47 return (struct zbf_double_s){1.0};
48 }
49
50 // CHECK: define void @f_zbf_double_zbf_s_arg(double %0)
f_zbf_double_zbf_s_arg(struct zbf_double_zbf_s a)51 void f_zbf_double_zbf_s_arg(struct zbf_double_zbf_s a) {}
52
53 // CHECK: define double @f_ret_zbf_double_zbf_s()
f_ret_zbf_double_zbf_s()54 struct zbf_double_zbf_s f_ret_zbf_double_zbf_s() {
55 return (struct zbf_double_zbf_s){1.0};
56 }
57
58 // Check that structs containing two floating point values (FLEN <= width) are
59 // expanded provided sufficient FPRs are available.
60
61 struct double_double_s { double f; double g; };
62 struct double_float_s { double f; float g; };
63
64 // CHECK: define void @f_double_double_s_arg(double %0, double %1)
f_double_double_s_arg(struct double_double_s a)65 void f_double_double_s_arg(struct double_double_s a) {}
66
67 // CHECK: define { double, double } @f_ret_double_double_s()
f_ret_double_double_s()68 struct double_double_s f_ret_double_double_s() {
69 return (struct double_double_s){1.0, 2.0};
70 }
71
72 // CHECK: define void @f_double_float_s_arg(double %0, float %1)
f_double_float_s_arg(struct double_float_s a)73 void f_double_float_s_arg(struct double_float_s a) {}
74
75 // CHECK: define { double, float } @f_ret_double_float_s()
f_ret_double_float_s()76 struct double_float_s f_ret_double_float_s() {
77 return (struct double_float_s){1.0, 2.0};
78 }
79
80 // CHECK: define void @f_double_double_s_arg_insufficient_fprs(float %a, double %b, double %c, double %d, double %e, double %f, double %g, [2 x i64] %h.coerce)
f_double_double_s_arg_insufficient_fprs(float a,double b,double c,double d,double e,double f,double g,struct double_double_s h)81 void f_double_double_s_arg_insufficient_fprs(float a, double b, double c, double d,
82 double e, double f, double g, struct double_double_s h) {}
83
84 // Check that structs containing int+double values are expanded, provided
85 // sufficient FPRs and GPRs are available. The integer components are neither
86 // sign or zero-extended.
87
88 struct double_int8_s { double f; int8_t i; };
89 struct double_uint8_s { double f; uint8_t i; };
90 struct double_int32_s { double f; int32_t i; };
91 struct double_int64_s { double f; int64_t i; };
92 struct double_int128bf_s { double f; __int128_t i : 64; };
93 struct double_int8_zbf_s { double f; int8_t i; int : 0; };
94
95 // CHECK: define void @f_double_int8_s_arg(double %0, i8 %1)
f_double_int8_s_arg(struct double_int8_s a)96 void f_double_int8_s_arg(struct double_int8_s a) {}
97
98 // CHECK: define { double, i8 } @f_ret_double_int8_s()
f_ret_double_int8_s()99 struct double_int8_s f_ret_double_int8_s() {
100 return (struct double_int8_s){1.0, 2};
101 }
102
103 // CHECK: define void @f_double_uint8_s_arg(double %0, i8 %1)
f_double_uint8_s_arg(struct double_uint8_s a)104 void f_double_uint8_s_arg(struct double_uint8_s a) {}
105
106 // CHECK: define { double, i8 } @f_ret_double_uint8_s()
f_ret_double_uint8_s()107 struct double_uint8_s f_ret_double_uint8_s() {
108 return (struct double_uint8_s){1.0, 2};
109 }
110
111 // CHECK: define void @f_double_int32_s_arg(double %0, i32 %1)
f_double_int32_s_arg(struct double_int32_s a)112 void f_double_int32_s_arg(struct double_int32_s a) {}
113
114 // CHECK: define { double, i32 } @f_ret_double_int32_s()
f_ret_double_int32_s()115 struct double_int32_s f_ret_double_int32_s() {
116 return (struct double_int32_s){1.0, 2};
117 }
118
119 // CHECK: define void @f_double_int64_s_arg(double %0, i64 %1)
f_double_int64_s_arg(struct double_int64_s a)120 void f_double_int64_s_arg(struct double_int64_s a) {}
121
122 // CHECK: define { double, i64 } @f_ret_double_int64_s()
f_ret_double_int64_s()123 struct double_int64_s f_ret_double_int64_s() {
124 return (struct double_int64_s){1.0, 2};
125 }
126
127 // CHECK: define void @f_double_int128bf_s_arg(double %0, i64 %1)
f_double_int128bf_s_arg(struct double_int128bf_s a)128 void f_double_int128bf_s_arg(struct double_int128bf_s a) {}
129
130 // CHECK: define { double, i64 } @f_ret_double_int128bf_s()
f_ret_double_int128bf_s()131 struct double_int128bf_s f_ret_double_int128bf_s() {
132 return (struct double_int128bf_s){1.0, 2};
133 }
134
135 // The zero-width bitfield means the struct can't be passed according to the
136 // floating point calling convention.
137
138 // CHECK: define void @f_double_int8_zbf_s(double %0, i8 %1)
f_double_int8_zbf_s(struct double_int8_zbf_s a)139 void f_double_int8_zbf_s(struct double_int8_zbf_s a) {}
140
141 // CHECK: define { double, i8 } @f_ret_double_int8_zbf_s()
f_ret_double_int8_zbf_s()142 struct double_int8_zbf_s f_ret_double_int8_zbf_s() {
143 return (struct double_int8_zbf_s){1.0, 2};
144 }
145
146 // CHECK: define void @f_double_int8_s_arg_insufficient_gprs(i32 signext %a, i32 signext %b, i32 signext %c, i32 signext %d, i32 signext %e, i32 signext %f, i32 signext %g, i32 signext %h, [2 x i64] %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)147 void f_double_int8_s_arg_insufficient_gprs(int a, int b, int c, int d, int e,
148 int f, int g, int h, struct double_int8_s i) {}
149
150 // CHECK: define void @f_struct_double_int8_insufficient_fprs(float %a, double %b, double %c, double %d, double %e, double %f, double %g, double %h, [2 x i64] %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)151 void f_struct_double_int8_insufficient_fprs(float a, double b, double c, double d,
152 double e, double f, double g, double h, struct double_int8_s i) {}
153
154 // Complex floating-point values or structs containing a single complex
155 // floating-point value should be passed as if it were an fp+fp struct.
156
157 // CHECK: define void @f_doublecomplex(double %a.coerce0, double %a.coerce1)
f_doublecomplex(double __complex__ a)158 void f_doublecomplex(double __complex__ a) {}
159
160 // CHECK: define { double, double } @f_ret_doublecomplex()
f_ret_doublecomplex()161 double __complex__ f_ret_doublecomplex() {
162 return 1.0;
163 }
164
165 struct doublecomplex_s { double __complex__ c; };
166
167 // CHECK: define void @f_doublecomplex_s_arg(double %0, double %1)
f_doublecomplex_s_arg(struct doublecomplex_s a)168 void f_doublecomplex_s_arg(struct doublecomplex_s a) {}
169
170 // CHECK: define { double, double } @f_ret_doublecomplex_s()
f_ret_doublecomplex_s()171 struct doublecomplex_s f_ret_doublecomplex_s() {
172 return (struct doublecomplex_s){1.0};
173 }
174
175 // Test single or two-element structs that need flattening. e.g. those
176 // containing nested structs, doubles in small arrays, zero-length structs etc.
177
178 struct doublearr1_s { double a[1]; };
179
180 // CHECK: define void @f_doublearr1_s_arg(double %0)
f_doublearr1_s_arg(struct doublearr1_s a)181 void f_doublearr1_s_arg(struct doublearr1_s a) {}
182
183 // CHECK: define double @f_ret_doublearr1_s()
f_ret_doublearr1_s()184 struct doublearr1_s f_ret_doublearr1_s() {
185 return (struct doublearr1_s){{1.0}};
186 }
187
188 struct doublearr2_s { double a[2]; };
189
190 // CHECK: define void @f_doublearr2_s_arg(double %0, double %1)
f_doublearr2_s_arg(struct doublearr2_s a)191 void f_doublearr2_s_arg(struct doublearr2_s a) {}
192
193 // CHECK: define { double, double } @f_ret_doublearr2_s()
f_ret_doublearr2_s()194 struct doublearr2_s f_ret_doublearr2_s() {
195 return (struct doublearr2_s){{1.0, 2.0}};
196 }
197
198 struct doublearr2_tricky1_s { struct { double f[1]; } g[2]; };
199
200 // CHECK: define void @f_doublearr2_tricky1_s_arg(double %0, double %1)
f_doublearr2_tricky1_s_arg(struct doublearr2_tricky1_s a)201 void f_doublearr2_tricky1_s_arg(struct doublearr2_tricky1_s a) {}
202
203 // CHECK: define { double, double } @f_ret_doublearr2_tricky1_s()
f_ret_doublearr2_tricky1_s()204 struct doublearr2_tricky1_s f_ret_doublearr2_tricky1_s() {
205 return (struct doublearr2_tricky1_s){{{{1.0}}, {{2.0}}}};
206 }
207
208 struct doublearr2_tricky2_s { struct {}; struct { double f[1]; } g[2]; };
209
210 // CHECK: define void @f_doublearr2_tricky2_s_arg(double %0, double %1)
f_doublearr2_tricky2_s_arg(struct doublearr2_tricky2_s a)211 void f_doublearr2_tricky2_s_arg(struct doublearr2_tricky2_s a) {}
212
213 // CHECK: define { double, double } @f_ret_doublearr2_tricky2_s()
f_ret_doublearr2_tricky2_s()214 struct doublearr2_tricky2_s f_ret_doublearr2_tricky2_s() {
215 return (struct doublearr2_tricky2_s){{}, {{{1.0}}, {{2.0}}}};
216 }
217
218 struct doublearr2_tricky3_s { union {}; struct { double f[1]; } g[2]; };
219
220 // CHECK: define void @f_doublearr2_tricky3_s_arg(double %0, double %1)
f_doublearr2_tricky3_s_arg(struct doublearr2_tricky3_s a)221 void f_doublearr2_tricky3_s_arg(struct doublearr2_tricky3_s a) {}
222
223 // CHECK: define { double, double } @f_ret_doublearr2_tricky3_s()
f_ret_doublearr2_tricky3_s()224 struct doublearr2_tricky3_s f_ret_doublearr2_tricky3_s() {
225 return (struct doublearr2_tricky3_s){{}, {{{1.0}}, {{2.0}}}};
226 }
227
228 struct doublearr2_tricky4_s { union {}; struct { struct {}; double f[1]; } g[2]; };
229
230 // CHECK: define void @f_doublearr2_tricky4_s_arg(double %0, double %1)
f_doublearr2_tricky4_s_arg(struct doublearr2_tricky4_s a)231 void f_doublearr2_tricky4_s_arg(struct doublearr2_tricky4_s a) {}
232
233 // CHECK: define { double, double } @f_ret_doublearr2_tricky4_s()
f_ret_doublearr2_tricky4_s()234 struct doublearr2_tricky4_s f_ret_doublearr2_tricky4_s() {
235 return (struct doublearr2_tricky4_s){{}, {{{}, {1.0}}, {{}, {2.0}}}};
236 }
237
238 // Test structs that should be passed according to the normal integer calling
239 // convention.
240
241 struct int_double_int_s { int a; double b; int c; };
242
243 // CHECK: define void @f_int_double_int_s_arg(%struct.int_double_int_s* %a)
f_int_double_int_s_arg(struct int_double_int_s a)244 void f_int_double_int_s_arg(struct int_double_int_s a) {}
245
246 // CHECK: define void @f_ret_int_double_int_s(%struct.int_double_int_s* noalias sret(%struct.int_double_int_s) align 8 %agg.result)
f_ret_int_double_int_s()247 struct int_double_int_s f_ret_int_double_int_s() {
248 return (struct int_double_int_s){1, 2.0, 3};
249 }
250
251 struct char_char_double_s { char a; char b; double c; };
252
253 // CHECK-LABEL: define void @f_char_char_double_s_arg([2 x i64] %a.coerce)
f_char_char_double_s_arg(struct char_char_double_s a)254 void f_char_char_double_s_arg(struct char_char_double_s a) {}
255
256 // CHECK: define [2 x i64] @f_ret_char_char_double_s()
f_ret_char_char_double_s()257 struct char_char_double_s f_ret_char_char_double_s() {
258 return (struct char_char_double_s){1, 2, 3.0};
259 }
260
261 // Unions are always passed according to the integer calling convention, even
262 // if they can only contain a double.
263
264 union double_u { double a; };
265
266 // CHECK: define void @f_double_u_arg(i64 %a.coerce)
f_double_u_arg(union double_u a)267 void f_double_u_arg(union double_u a) {}
268
269 // CHECK: define i64 @f_ret_double_u()
f_ret_double_u()270 union double_u f_ret_double_u() {
271 return (union double_u){1.0};
272 }
273