]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/aarch64/aarch64-sve-builtins.cc
[AArch64] Add support for the SVE2 ACLE
[thirdparty/gcc.git] / gcc / config / aarch64 / aarch64-sve-builtins.cc
1 /* ACLE support for AArch64 SVE
2 Copyright (C) 2018-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #define IN_TARGET_CODE 1
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "memmodel.h"
30 #include "insn-codes.h"
31 #include "optabs.h"
32 #include "recog.h"
33 #include "diagnostic.h"
34 #include "expr.h"
35 #include "basic-block.h"
36 #include "function.h"
37 #include "fold-const.h"
38 #include "gimple.h"
39 #include "gimple-iterator.h"
40 #include "gimplify.h"
41 #include "explow.h"
42 #include "emit-rtl.h"
43 #include "tree-vector-builder.h"
44 #include "stor-layout.h"
45 #include "regs.h"
46 #include "alias.h"
47 #include "gimple-fold.h"
48 #include "langhooks.h"
49 #include "stringpool.h"
50 #include "attribs.h"
51 #include "aarch64-sve-builtins.h"
52 #include "aarch64-sve-builtins-base.h"
53 #include "aarch64-sve-builtins-sve2.h"
54 #include "aarch64-sve-builtins-shapes.h"
55
56 namespace aarch64_sve {
57
58 /* Static information about each single-predicate or single-vector
59 ABI and ACLE type. */
60 struct vector_type_info
61 {
62 /* The name of the type as declared by arm_sve.h. */
63 const char *acle_name;
64
65 /* The name of the type specified in AAPCS64. The type is always
66 available under this name, even when arm_sve.h isn't included. */
67 const char *abi_name;
68
69 /* The C++ mangling of ABI_NAME. */
70 const char *mangled_name;
71 };
72
73 /* Describes a function decl. */
74 class GTY(()) registered_function
75 {
76 public:
77 /* The ACLE function that the decl represents. */
78 function_instance instance GTY ((skip));
79
80 /* The decl itself. */
81 tree decl;
82
83 /* The architecture extensions that the function requires, as a set of
84 AARCH64_FL_* flags. */
85 uint64_t required_extensions;
86
87 /* True if the decl represents an overloaded function that needs to be
88 resolved by function_resolver. */
89 bool overloaded_p;
90 };
91
92 /* Hash traits for registered_function. */
93 struct registered_function_hasher : nofree_ptr_hash <registered_function>
94 {
95 typedef function_instance compare_type;
96
97 static hashval_t hash (value_type);
98 static bool equal (value_type, const compare_type &);
99 };
100
101 /* Information about each single-predicate or single-vector type. */
102 static CONSTEXPR const vector_type_info vector_types[] = {
103 #define DEF_SVE_TYPE(ACLE_NAME, NCHARS, ABI_NAME, SCALAR_TYPE) \
104 { #ACLE_NAME, #ABI_NAME, #NCHARS #ABI_NAME },
105 #include "aarch64-sve-builtins.def"
106 };
107
108 /* The function name suffix associated with each predication type. */
109 static const char *const pred_suffixes[NUM_PREDS + 1] = {
110 "",
111 "",
112 "_m",
113 "_x",
114 "_z",
115 ""
116 };
117
118 /* Static information about each mode_suffix_index. */
119 CONSTEXPR const mode_suffix_info mode_suffixes[] = {
120 #define VECTOR_TYPE_none NUM_VECTOR_TYPES
121 #define DEF_SVE_MODE(NAME, BASE, DISPLACEMENT, UNITS) \
122 { "_" #NAME, VECTOR_TYPE_##BASE, VECTOR_TYPE_##DISPLACEMENT, UNITS_##UNITS },
123 #include "aarch64-sve-builtins.def"
124 #undef VECTOR_TYPE_none
125 { "", NUM_VECTOR_TYPES, NUM_VECTOR_TYPES, UNITS_none }
126 };
127
128 /* Static information about each type_suffix_index. */
129 CONSTEXPR const type_suffix_info type_suffixes[NUM_TYPE_SUFFIXES + 1] = {
130 #define DEF_SVE_TYPE_SUFFIX(NAME, ACLE_TYPE, CLASS, BITS, MODE) \
131 { "_" #NAME, \
132 VECTOR_TYPE_##ACLE_TYPE, \
133 TYPE_##CLASS, \
134 BITS, \
135 BITS / BITS_PER_UNIT, \
136 TYPE_##CLASS == TYPE_signed || TYPE_##CLASS == TYPE_unsigned, \
137 TYPE_##CLASS == TYPE_unsigned, \
138 TYPE_##CLASS == TYPE_float, \
139 TYPE_##CLASS == TYPE_bool, \
140 0, \
141 MODE },
142 #include "aarch64-sve-builtins.def"
143 { "", NUM_VECTOR_TYPES, TYPE_bool, 0, 0, false, false, false, false,
144 0, VOIDmode }
145 };
146
147 /* Define a TYPES_<combination> macro for each combination of type
148 suffixes that an ACLE function can have, where <combination> is the
149 name used in DEF_SVE_FUNCTION entries.
150
151 Use S (T) for single type suffix T and D (T1, T2) for a pair of type
152 suffixes T1 and T2. Use commas to separate the suffixes.
153
154 Although the order shouldn't matter, the convention is to sort the
155 suffixes lexicographically after dividing suffixes into a type
156 class ("b", "f", etc.) and a numerical bit count. */
157
158 /* _b8 _b16 _b32 _b64. */
159 #define TYPES_all_pred(S, D) \
160 S (b8), S (b16), S (b32), S (b64)
161
162 /* _f16 _f32 _f64. */
163 #define TYPES_all_float(S, D) \
164 S (f16), S (f32), S (f64)
165
166 /* _s8 _s16 _s32 _s64. */
167 #define TYPES_all_signed(S, D) \
168 S (s8), S (s16), S (s32), S (s64)
169
170 /* _f16 _f32 _f64
171 _s8 _s16 _s32 _s64. */
172 #define TYPES_all_float_and_signed(S, D) \
173 TYPES_all_float (S, D), TYPES_all_signed (S, D)
174
175 /* _u8 _u16 _u32 _u64. */
176 #define TYPES_all_unsigned(S, D) \
177 S (u8), S (u16), S (u32), S (u64)
178
179 /* _s8 _s16 _s32 _s64
180 _u8 _u16 _u32 _u64. */
181 #define TYPES_all_integer(S, D) \
182 TYPES_all_signed (S, D), TYPES_all_unsigned (S, D)
183
184 /* _f16 _f32 _f64
185 _s8 _s16 _s32 _s64
186 _u8 _u16 _u32 _u64. */
187 #define TYPES_all_data(S, D) \
188 TYPES_all_float (S, D), TYPES_all_integer (S, D)
189
190 /* _b only. */
191 #define TYPES_b(S, D) \
192 S (b)
193
194 /* _u8. */
195 #define TYPES_b_unsigned(S, D) \
196 S (u8)
197
198 /* _s8
199 _u8. */
200 #define TYPES_b_integer(S, D) \
201 S (s8), TYPES_b_unsigned (S, D)
202
203 /* _s8 _s16
204 _u8 _u16. */
205 #define TYPES_bh_integer(S, D) \
206 S (s8), S (s16), S (u8), S (u16)
207
208 /* _u8 _u32. */
209 #define TYPES_bs_unsigned(S, D) \
210 S (u8), S (u32)
211
212 /* _s8 _s16 _s32. */
213 #define TYPES_bhs_signed(S, D) \
214 S (s8), S (s16), S (s32)
215
216 /* _u8 _u16 _u32. */
217 #define TYPES_bhs_unsigned(S, D) \
218 S (u8), S (u16), S (u32)
219
220 /* _s8 _s16 _s32
221 _u8 _u16 _u32. */
222 #define TYPES_bhs_integer(S, D) \
223 TYPES_bhs_signed (S, D), TYPES_bhs_unsigned (S, D)
224
225 /* _s16
226 _u16. */
227 #define TYPES_h_integer(S, D) \
228 S (s16), S (u16)
229
230 /* _s16 _s32. */
231 #define TYPES_hs_signed(S, D) \
232 S (s16), S (s32)
233
234 /* _s16 _s32
235 _u16 _u32. */
236 #define TYPES_hs_integer(S, D) \
237 TYPES_hs_signed (S, D), S (u16), S (u32)
238
239 /* _f16 _f32. */
240 #define TYPES_hs_float(S, D) \
241 S (f16), S (f32)
242
243 /* _u16 _u64. */
244 #define TYPES_hd_unsigned(S, D) \
245 S (u16), S (u64)
246
247 /* _s16 _s32 _s64. */
248 #define TYPES_hsd_signed(S, D) \
249 S (s16), S (s32), S (s64)
250
251 /* _s16 _s32 _s64
252 _u16 _u32 _u64. */
253 #define TYPES_hsd_integer(S, D) \
254 TYPES_hsd_signed (S, D), S (u16), S (u32), S (u64)
255
256 /* _f32
257 _s16 _s32 _s64
258 _u16 _u32 _u64. */
259 #define TYPES_s_float_hsd_integer(S, D) \
260 S (f32), TYPES_hsd_integer (S, D)
261
262 /* _f32
263 _s32 _s64
264 _u32 _u64. */
265 #define TYPES_s_float_sd_integer(S, D) \
266 S (f32), TYPES_sd_integer (S, D)
267
268 /* _u32. */
269 #define TYPES_s_unsigned(S, D) \
270 S (u32)
271
272 /* _s32 _u32. */
273 #define TYPES_s_integer(S, D) \
274 S (s32), TYPES_s_unsigned (S, D)
275
276 /* _s32 _s64. */
277 #define TYPES_sd_signed(S, D) \
278 S (s32), S (s64)
279
280 /* _u32 _u64. */
281 #define TYPES_sd_unsigned(S, D) \
282 S (u32), S (u64)
283
284 /* _s32 _s64
285 _u32 _u64. */
286 #define TYPES_sd_integer(S, D) \
287 TYPES_sd_signed (S, D), TYPES_sd_unsigned (S, D)
288
289 /* _f32 _f64
290 _s32 _s64
291 _u32 _u64. */
292 #define TYPES_sd_data(S, D) \
293 S (f32), S (f64), TYPES_sd_integer (S, D)
294
295 /* _f16 _f32 _f64
296 _s32 _s64
297 _u32 _u64. */
298 #define TYPES_all_float_and_sd_integer(S, D) \
299 TYPES_all_float (S, D), TYPES_sd_integer (S, D)
300
301 /* _u64. */
302 #define TYPES_d_unsigned(S, D) \
303 S (u64)
304
305 /* _s64
306 _u64. */
307 #define TYPES_d_integer(S, D) \
308 S (s64), TYPES_d_unsigned (S, D)
309
310 /* _f64
311 _s64
312 _u64. */
313 #define TYPES_d_data(S, D) \
314 S (f64), TYPES_d_integer (S, D)
315
316 /* All the type combinations allowed by svcvt. */
317 #define TYPES_cvt(S, D) \
318 D (f16, f32), D (f16, f64), \
319 D (f16, s16), D (f16, s32), D (f16, s64), \
320 D (f16, u16), D (f16, u32), D (f16, u64), \
321 \
322 D (f32, f16), D (f32, f64), \
323 D (f32, s32), D (f32, s64), \
324 D (f32, u32), D (f32, u64), \
325 \
326 D (f64, f16), D (f64, f32), \
327 D (f64, s32), D (f64, s64), \
328 D (f64, u32), D (f64, u64), \
329 \
330 D (s16, f16), \
331 D (s32, f16), D (s32, f32), D (s32, f64), \
332 D (s64, f16), D (s64, f32), D (s64, f64), \
333 \
334 D (u16, f16), \
335 D (u32, f16), D (u32, f32), D (u32, f64), \
336 D (u64, f16), D (u64, f32), D (u64, f64)
337
338 /* _f32_f16
339 _f64_f32. */
340 #define TYPES_cvt_long(S, D) \
341 D (f32, f16), D (f64, f32)
342
343 /* _f16_f32. */
344 #define TYPES_cvt_narrow_s(S, D) \
345 D (f32, f64)
346
347 /* _f16_f32
348 _f32_f64. */
349 #define TYPES_cvt_narrow(S, D) \
350 D (f16, f32), TYPES_cvt_narrow_s (S, D)
351
352 /* { _s32 _s64 } x { _b8 _b16 _b32 _b64 }
353 { _u32 _u64 }. */
354 #define TYPES_inc_dec_n1(D, A) \
355 D (A, b8), D (A, b16), D (A, b32), D (A, b64)
356 #define TYPES_inc_dec_n(S, D) \
357 TYPES_inc_dec_n1 (D, s32), \
358 TYPES_inc_dec_n1 (D, s64), \
359 TYPES_inc_dec_n1 (D, u32), \
360 TYPES_inc_dec_n1 (D, u64)
361
362 /* { _f16 _f32 _f64 } { _f16 _f32 _f64 }
363 { _s8 _s16 _s32 _s64 } x { _s8 _s16 _s32 _s64 }
364 { _u8 _u16 _u32 _u64 } { _u8 _u16 _u32 _u64 }. */
365 #define TYPES_reinterpret1(D, A) \
366 D (A, f16), D (A, f32), D (A, f64), \
367 D (A, s8), D (A, s16), D (A, s32), D (A, s64), \
368 D (A, u8), D (A, u16), D (A, u32), D (A, u64)
369 #define TYPES_reinterpret(S, D) \
370 TYPES_reinterpret1 (D, f16), \
371 TYPES_reinterpret1 (D, f32), \
372 TYPES_reinterpret1 (D, f64), \
373 TYPES_reinterpret1 (D, s8), \
374 TYPES_reinterpret1 (D, s16), \
375 TYPES_reinterpret1 (D, s32), \
376 TYPES_reinterpret1 (D, s64), \
377 TYPES_reinterpret1 (D, u8), \
378 TYPES_reinterpret1 (D, u16), \
379 TYPES_reinterpret1 (D, u32), \
380 TYPES_reinterpret1 (D, u64)
381
382 /* { _b8 _b16 _b32 _b64 } x { _s32 _s64 }
383 { _u32 _u64 } */
384 #define TYPES_while1(D, bn) \
385 D (bn, s32), D (bn, s64), D (bn, u32), D (bn, u64)
386 #define TYPES_while(S, D) \
387 TYPES_while1 (D, b8), \
388 TYPES_while1 (D, b16), \
389 TYPES_while1 (D, b32), \
390 TYPES_while1 (D, b64)
391
392 /* Describe a pair of type suffixes in which only the first is used. */
393 #define DEF_VECTOR_TYPE(X) { TYPE_SUFFIX_ ## X, NUM_TYPE_SUFFIXES }
394
395 /* Describe a pair of type suffixes in which both are used. */
396 #define DEF_DOUBLE_TYPE(X, Y) { TYPE_SUFFIX_ ## X, TYPE_SUFFIX_ ## Y }
397
398 /* Create an array that can be used in aarch64-sve-builtins.def to
399 select the type suffixes in TYPES_<NAME>. */
400 #define DEF_SVE_TYPES_ARRAY(NAME) \
401 static const type_suffix_pair types_##NAME[] = { \
402 TYPES_##NAME (DEF_VECTOR_TYPE, DEF_DOUBLE_TYPE), \
403 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES } \
404 }
405
406 /* For functions that don't take any type suffixes. */
407 static const type_suffix_pair types_none[] = {
408 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES },
409 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES }
410 };
411
412 /* Create an array for each TYPES_<combination> macro above. */
413 DEF_SVE_TYPES_ARRAY (all_pred);
414 DEF_SVE_TYPES_ARRAY (all_float);
415 DEF_SVE_TYPES_ARRAY (all_signed);
416 DEF_SVE_TYPES_ARRAY (all_float_and_signed);
417 DEF_SVE_TYPES_ARRAY (all_unsigned);
418 DEF_SVE_TYPES_ARRAY (all_integer);
419 DEF_SVE_TYPES_ARRAY (all_data);
420 DEF_SVE_TYPES_ARRAY (b);
421 DEF_SVE_TYPES_ARRAY (b_unsigned);
422 DEF_SVE_TYPES_ARRAY (b_integer);
423 DEF_SVE_TYPES_ARRAY (bh_integer);
424 DEF_SVE_TYPES_ARRAY (bs_unsigned);
425 DEF_SVE_TYPES_ARRAY (bhs_signed);
426 DEF_SVE_TYPES_ARRAY (bhs_unsigned);
427 DEF_SVE_TYPES_ARRAY (bhs_integer);
428 DEF_SVE_TYPES_ARRAY (h_integer);
429 DEF_SVE_TYPES_ARRAY (hs_signed);
430 DEF_SVE_TYPES_ARRAY (hs_integer);
431 DEF_SVE_TYPES_ARRAY (hs_float);
432 DEF_SVE_TYPES_ARRAY (hd_unsigned);
433 DEF_SVE_TYPES_ARRAY (hsd_signed);
434 DEF_SVE_TYPES_ARRAY (hsd_integer);
435 DEF_SVE_TYPES_ARRAY (s_float_hsd_integer);
436 DEF_SVE_TYPES_ARRAY (s_float_sd_integer);
437 DEF_SVE_TYPES_ARRAY (s_unsigned);
438 DEF_SVE_TYPES_ARRAY (s_integer);
439 DEF_SVE_TYPES_ARRAY (sd_signed);
440 DEF_SVE_TYPES_ARRAY (sd_unsigned);
441 DEF_SVE_TYPES_ARRAY (sd_integer);
442 DEF_SVE_TYPES_ARRAY (sd_data);
443 DEF_SVE_TYPES_ARRAY (all_float_and_sd_integer);
444 DEF_SVE_TYPES_ARRAY (d_unsigned);
445 DEF_SVE_TYPES_ARRAY (d_integer);
446 DEF_SVE_TYPES_ARRAY (d_data);
447 DEF_SVE_TYPES_ARRAY (cvt);
448 DEF_SVE_TYPES_ARRAY (cvt_long);
449 DEF_SVE_TYPES_ARRAY (cvt_narrow_s);
450 DEF_SVE_TYPES_ARRAY (cvt_narrow);
451 DEF_SVE_TYPES_ARRAY (inc_dec_n);
452 DEF_SVE_TYPES_ARRAY (reinterpret);
453 DEF_SVE_TYPES_ARRAY (while);
454
455 /* Used by functions that have no governing predicate. */
456 static const predication_index preds_none[] = { PRED_none, NUM_PREDS };
457
458 /* Used by functions that have a governing predicate but do not have an
459 explicit suffix. */
460 static const predication_index preds_implicit[] = { PRED_implicit, NUM_PREDS };
461
462 /* Used by functions that allow merging and "don't care" predication,
463 but are not suitable for predicated MOVPRFX. */
464 static const predication_index preds_mx[] = {
465 PRED_m, PRED_x, NUM_PREDS
466 };
467
468 /* Used by functions that allow merging, zeroing and "don't care"
469 predication. */
470 static const predication_index preds_mxz[] = {
471 PRED_m, PRED_x, PRED_z, NUM_PREDS
472 };
473
474 /* Used by functions that have the mxz predicated forms above, and in addition
475 have an unpredicated form. */
476 static const predication_index preds_mxz_or_none[] = {
477 PRED_m, PRED_x, PRED_z, PRED_none, NUM_PREDS
478 };
479
480 /* Used by functions that allow merging and zeroing predication but have
481 no "_x" form. */
482 static const predication_index preds_mz[] = { PRED_m, PRED_z, NUM_PREDS };
483
484 /* Used by functions that have an unpredicated form and a _z predicated
485 form. */
486 static const predication_index preds_z_or_none[] = {
487 PRED_z, PRED_none, NUM_PREDS
488 };
489
490 /* Used by (mostly predicate) functions that only support "_z" predication. */
491 static const predication_index preds_z[] = { PRED_z, NUM_PREDS };
492
493 /* A list of all SVE ACLE functions. */
494 static CONSTEXPR const function_group_info function_groups[] = {
495 #define DEF_SVE_FUNCTION(NAME, SHAPE, TYPES, PREDS) \
496 { #NAME, &functions::NAME, &shapes::SHAPE, types_##TYPES, preds_##PREDS, \
497 REQUIRED_EXTENSIONS | AARCH64_FL_SVE },
498 #include "aarch64-sve-builtins.def"
499 };
500
501 /* The scalar type associated with each vector type. */
502 GTY(()) tree scalar_types[NUM_VECTOR_TYPES];
503
504 /* The single-predicate and single-vector types, with their built-in
505 "__SV..._t" name. Allow an index of NUM_VECTOR_TYPES, which always
506 yields a null tree. */
507 static GTY(()) tree abi_vector_types[NUM_VECTOR_TYPES + 1];
508
509 /* Same, but with the arm_sve.h "sv..._t" name. */
510 GTY(()) tree acle_vector_types[MAX_TUPLE_SIZE][NUM_VECTOR_TYPES + 1];
511
512 /* The svpattern enum type. */
513 GTY(()) tree acle_svpattern;
514
515 /* The svprfop enum type. */
516 GTY(()) tree acle_svprfop;
517
518 /* The list of all registered function decls, indexed by code. */
519 static GTY(()) vec<registered_function *, va_gc> *registered_functions;
520
521 /* All registered function decls, hashed on the function_instance
522 that they implement. This is used for looking up implementations of
523 overloaded functions. */
524 static hash_table<registered_function_hasher> *function_table;
525
526 /* True if we've already complained about attempts to use functions
527 when the required extension is disabled. */
528 static bool reported_missing_extension_p;
529
530 /* Record that TYPE is an ABI-defined SVE type that contains NUM_ZR SVE vectors
531 and NUM_PR SVE predicates. MANGLED_NAME, if nonnull, is the ABI-defined
532 mangling of the type. */
533 static void
534 add_sve_type_attribute (tree type, unsigned int num_zr, unsigned int num_pr,
535 const char *mangled_name)
536 {
537 tree mangled_name_tree
538 = (mangled_name ? get_identifier (mangled_name) : NULL_TREE);
539
540 tree value = tree_cons (NULL_TREE, mangled_name_tree, NULL_TREE);
541 value = tree_cons (NULL_TREE, size_int (num_pr), value);
542 value = tree_cons (NULL_TREE, size_int (num_zr), value);
543 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("SVE type"), value,
544 TYPE_ATTRIBUTES (type));
545 }
546
547 /* If TYPE is an ABI-defined SVE type, return its attribute descriptor,
548 otherwise return null. */
549 static tree
550 lookup_sve_type_attribute (const_tree type)
551 {
552 if (type == error_mark_node)
553 return NULL_TREE;
554 return lookup_attribute ("SVE type", TYPE_ATTRIBUTES (type));
555 }
556
557 /* If TYPE is a valid SVE element type, return the corresponding type
558 suffix, otherwise return NUM_TYPE_SUFFIXES. */
559 static type_suffix_index
560 find_type_suffix_for_scalar_type (const_tree type)
561 {
562 /* A linear search should be OK here, since the code isn't hot and
563 the number of types is only small. */
564 type = TYPE_MAIN_VARIANT (type);
565 for (unsigned int suffix_i = 0; suffix_i < NUM_TYPE_SUFFIXES; ++suffix_i)
566 if (!type_suffixes[suffix_i].bool_p)
567 {
568 vector_type_index vector_i = type_suffixes[suffix_i].vector_type;
569 if (type == TYPE_MAIN_VARIANT (scalar_types[vector_i]))
570 return type_suffix_index (suffix_i);
571 }
572 return NUM_TYPE_SUFFIXES;
573 }
574
575 /* Report an error against LOCATION that the user has tried to use
576 function FNDECL when extension EXTENSION is disabled. */
577 static void
578 report_missing_extension (location_t location, tree fndecl,
579 const char *extension)
580 {
581 /* Avoid reporting a slew of messages for a single oversight. */
582 if (reported_missing_extension_p)
583 return;
584
585 error_at (location, "ACLE function %qD requires ISA extension %qs",
586 fndecl, extension);
587 inform (location, "you can enable %qs using the command-line"
588 " option %<-march%>, or by using the %<target%>"
589 " attribute or pragma", extension);
590 reported_missing_extension_p = true;
591 }
592
593 /* Check whether all the AARCH64_FL_* values in REQUIRED_EXTENSIONS are
594 enabled, given that those extensions are required for function FNDECL.
595 Report an error against LOCATION if not. */
596 static bool
597 check_required_extensions (location_t location, tree fndecl,
598 uint64_t required_extensions)
599 {
600 uint64_t missing_extensions = required_extensions & ~aarch64_isa_flags;
601 if (missing_extensions == 0)
602 return true;
603
604 static const struct { uint64_t flag; const char *name; } extensions[] = {
605 #define AARCH64_OPT_EXTENSION(EXT_NAME, FLAG_CANONICAL, FLAGS_ON, FLAGS_OFF, \
606 SYNTHETIC, FEATURE_STRING) \
607 { FLAG_CANONICAL, EXT_NAME },
608 #include "aarch64-option-extensions.def"
609 };
610
611 for (unsigned int i = 0; i < ARRAY_SIZE (extensions); ++i)
612 if (missing_extensions & extensions[i].flag)
613 {
614 report_missing_extension (location, fndecl, extensions[i].name);
615 return false;
616 }
617 gcc_unreachable ();
618 }
619
620 /* Report that LOCATION has a call to FNDECL in which argument ARGNO
621 was not an integer constant expression. ARGNO counts from zero. */
622 static void
623 report_non_ice (location_t location, tree fndecl, unsigned int argno)
624 {
625 error_at (location, "argument %d of %qE must be an integer constant"
626 " expression", argno + 1, fndecl);
627 }
628
629 /* Report that LOCATION has a call to FNDECL in which argument ARGNO has
630 the value ACTUAL, whereas the function requires a value in the range
631 [MIN, MAX]. ARGNO counts from zero. */
632 static void
633 report_out_of_range (location_t location, tree fndecl, unsigned int argno,
634 HOST_WIDE_INT actual, HOST_WIDE_INT min,
635 HOST_WIDE_INT max)
636 {
637 error_at (location, "passing %wd to argument %d of %qE, which expects"
638 " a value in the range [%wd, %wd]", actual, argno + 1, fndecl,
639 min, max);
640 }
641
642 /* Report that LOCATION has a call to FNDECL in which argument ARGNO has
643 the value ACTUAL, whereas the function requires either VALUE0 or
644 VALUE1. ARGNO counts from zero. */
645 static void
646 report_neither_nor (location_t location, tree fndecl, unsigned int argno,
647 HOST_WIDE_INT actual, HOST_WIDE_INT value0,
648 HOST_WIDE_INT value1)
649 {
650 error_at (location, "passing %wd to argument %d of %qE, which expects"
651 " either %wd or %wd", actual, argno + 1, fndecl, value0, value1);
652 }
653
654 /* Report that LOCATION has a call to FNDECL in which argument ARGNO has
655 the value ACTUAL, whereas the function requires one of VALUE0..3.
656 ARGNO counts from zero. */
657 static void
658 report_not_one_of (location_t location, tree fndecl, unsigned int argno,
659 HOST_WIDE_INT actual, HOST_WIDE_INT value0,
660 HOST_WIDE_INT value1, HOST_WIDE_INT value2,
661 HOST_WIDE_INT value3)
662 {
663 error_at (location, "passing %wd to argument %d of %qE, which expects"
664 " %wd, %wd, %wd or %wd", actual, argno + 1, fndecl, value0, value1,
665 value2, value3);
666 }
667
668 /* Report that LOCATION has a call to FNDECL in which argument ARGNO has
669 the value ACTUAL, whereas the function requires a valid value of
670 enum type ENUMTYPE. ARGNO counts from zero. */
671 static void
672 report_not_enum (location_t location, tree fndecl, unsigned int argno,
673 HOST_WIDE_INT actual, tree enumtype)
674 {
675 error_at (location, "passing %wd to argument %d of %qE, which expects"
676 " a valid %qT value", actual, argno + 1, fndecl, enumtype);
677 }
678
679 /* Return a hash code for a function_instance. */
680 hashval_t
681 function_instance::hash () const
682 {
683 inchash::hash h;
684 /* BASE uniquely determines BASE_NAME, so we don't need to hash both. */
685 h.add_ptr (base);
686 h.add_ptr (shape);
687 h.add_int (mode_suffix_id);
688 h.add_int (type_suffix_ids[0]);
689 h.add_int (type_suffix_ids[1]);
690 h.add_int (pred);
691 return h.end ();
692 }
693
694 /* Return a set of CP_* flags that describe what the function could do,
695 taking the command-line flags into account. */
696 unsigned int
697 function_instance::call_properties () const
698 {
699 unsigned int flags = base->call_properties (*this);
700
701 /* -fno-trapping-math means that we can assume any FP exceptions
702 are not user-visible. */
703 if (!flag_trapping_math)
704 flags &= ~CP_RAISE_FP_EXCEPTIONS;
705
706 return flags;
707 }
708
709 /* Return true if calls to the function could read some form of
710 global state. */
711 bool
712 function_instance::reads_global_state_p () const
713 {
714 unsigned int flags = call_properties ();
715
716 /* Preserve any dependence on rounding mode, flush to zero mode, etc.
717 There is currently no way of turning this off; in particular,
718 -fno-rounding-math (which is the default) means that we should make
719 the usual assumptions about rounding mode, which for intrinsics means
720 acting as the instructions do. */
721 if (flags & CP_READ_FPCR)
722 return true;
723
724 /* Handle direct reads of global state. */
725 return flags & (CP_READ_MEMORY | CP_READ_FFR);
726 }
727
728 /* Return true if calls to the function could modify some form of
729 global state. */
730 bool
731 function_instance::modifies_global_state_p () const
732 {
733 unsigned int flags = call_properties ();
734
735 /* Preserve any exception state written back to the FPCR,
736 unless -fno-trapping-math says this is unnecessary. */
737 if (flags & CP_RAISE_FP_EXCEPTIONS)
738 return true;
739
740 /* Treat prefetches as modifying global state, since that's the
741 only means we have of keeping them in their correct position. */
742 if (flags & CP_PREFETCH_MEMORY)
743 return true;
744
745 /* Handle direct modifications of global state. */
746 return flags & (CP_WRITE_MEMORY | CP_WRITE_FFR);
747 }
748
749 /* Return true if calls to the function could raise a signal. */
750 bool
751 function_instance::could_trap_p () const
752 {
753 unsigned int flags = call_properties ();
754
755 /* Handle functions that could raise SIGFPE. */
756 if (flags & CP_RAISE_FP_EXCEPTIONS)
757 return true;
758
759 /* Handle functions that could raise SIGBUS or SIGSEGV. */
760 if (flags & (CP_READ_MEMORY | CP_WRITE_MEMORY))
761 return true;
762
763 return false;
764 }
765
766 inline hashval_t
767 registered_function_hasher::hash (value_type value)
768 {
769 return value->instance.hash ();
770 }
771
772 inline bool
773 registered_function_hasher::equal (value_type value, const compare_type &key)
774 {
775 return value->instance == key;
776 }
777
778 sve_switcher::sve_switcher ()
779 : m_old_isa_flags (aarch64_isa_flags)
780 {
781 /* Changing the ISA flags and have_regs_of_mode should be enough here.
782 We shouldn't need to pay the compile-time cost of a full target
783 switch. */
784 aarch64_isa_flags = (AARCH64_FL_FP | AARCH64_FL_SIMD | AARCH64_FL_F16
785 | AARCH64_FL_SVE);
786
787 memcpy (m_old_have_regs_of_mode, have_regs_of_mode,
788 sizeof (have_regs_of_mode));
789 for (int i = 0; i < NUM_MACHINE_MODES; ++i)
790 if (aarch64_sve_mode_p ((machine_mode) i))
791 have_regs_of_mode[i] = true;
792 }
793
794 sve_switcher::~sve_switcher ()
795 {
796 memcpy (have_regs_of_mode, m_old_have_regs_of_mode,
797 sizeof (have_regs_of_mode));
798 aarch64_isa_flags = m_old_isa_flags;
799 }
800
801 function_builder::function_builder ()
802 {
803 m_overload_type = build_function_type (void_type_node, void_list_node);
804 m_direct_overloads = lang_GNU_CXX ();
805 gcc_obstack_init (&m_string_obstack);
806 }
807
808 function_builder::~function_builder ()
809 {
810 obstack_free (&m_string_obstack, NULL);
811 }
812
813 /* Add NAME to the end of the function name being built. */
814 void
815 function_builder::append_name (const char *name)
816 {
817 obstack_grow (&m_string_obstack, name, strlen (name));
818 }
819
820 /* Zero-terminate and complete the function name being built. */
821 char *
822 function_builder::finish_name ()
823 {
824 obstack_1grow (&m_string_obstack, 0);
825 return (char *) obstack_finish (&m_string_obstack);
826 }
827
828 /* Return the overloaded or full function name for INSTANCE; OVERLOADED_P
829 selects which. Allocate the string on m_string_obstack; the caller
830 must use obstack_free to free it after use. */
831 char *
832 function_builder::get_name (const function_instance &instance,
833 bool overloaded_p)
834 {
835 append_name (instance.base_name);
836 if (overloaded_p)
837 switch (instance.displacement_units ())
838 {
839 case UNITS_none:
840 break;
841
842 case UNITS_bytes:
843 append_name ("_offset");
844 break;
845
846 case UNITS_elements:
847 append_name ("_index");
848 break;
849
850 case UNITS_vectors:
851 append_name ("_vnum");
852 break;
853 }
854 else
855 append_name (instance.mode_suffix ().string);
856 for (unsigned int i = 0; i < 2; ++i)
857 if (!overloaded_p || instance.shape->explicit_type_suffix_p (i))
858 append_name (instance.type_suffix (i).string);
859 append_name (pred_suffixes[instance.pred]);
860 return finish_name ();
861 }
862
863 /* Add attribute NAME to ATTRS. */
864 static tree
865 add_attribute (const char *name, tree attrs)
866 {
867 return tree_cons (get_identifier (name), NULL_TREE, attrs);
868 }
869
870 /* Return the appropriate function attributes for INSTANCE. */
871 tree
872 function_builder::get_attributes (const function_instance &instance)
873 {
874 tree attrs = NULL_TREE;
875
876 if (!instance.modifies_global_state_p ())
877 {
878 if (instance.reads_global_state_p ())
879 attrs = add_attribute ("pure", attrs);
880 else
881 attrs = add_attribute ("const", attrs);
882 }
883
884 if (!flag_non_call_exceptions || !instance.could_trap_p ())
885 attrs = add_attribute ("nothrow", attrs);
886
887 return add_attribute ("leaf", attrs);
888 }
889
890 /* Add a function called NAME with type FNTYPE and attributes ATTRS.
891 INSTANCE describes what the function does and OVERLOADED_P indicates
892 whether it is overloaded. REQUIRED_EXTENSIONS are the set of
893 architecture extensions that the function requires. */
894 registered_function &
895 function_builder::add_function (const function_instance &instance,
896 const char *name, tree fntype, tree attrs,
897 uint64_t required_extensions,
898 bool overloaded_p)
899 {
900 unsigned int code = vec_safe_length (registered_functions);
901 code = (code << AARCH64_BUILTIN_SHIFT) | AARCH64_BUILTIN_SVE;
902 tree decl = simulate_builtin_function_decl (input_location, name, fntype,
903 code, NULL, attrs);
904
905 registered_function &rfn = *ggc_alloc <registered_function> ();
906 rfn.instance = instance;
907 rfn.decl = decl;
908 rfn.required_extensions = required_extensions;
909 rfn.overloaded_p = overloaded_p;
910 vec_safe_push (registered_functions, &rfn);
911
912 return rfn;
913 }
914
915 /* Add a built-in function for INSTANCE, with the argument types given
916 by ARGUMENT_TYPES and the return type given by RETURN_TYPE.
917 REQUIRED_EXTENSIONS are the set of architecture extensions that the
918 function requires. FORCE_DIRECT_OVERLOADS is true if there is a
919 one-to-one mapping between "short" and "full" names, and if standard
920 overload resolution therefore isn't necessary. */
921 void
922 function_builder::add_unique_function (const function_instance &instance,
923 tree return_type,
924 vec<tree> &argument_types,
925 uint64_t required_extensions,
926 bool force_direct_overloads)
927 {
928 /* Add the function under its full (unique) name. */
929 char *name = get_name (instance, false);
930 tree fntype = build_function_type_array (return_type,
931 argument_types.length (),
932 argument_types.address ());
933 tree attrs = get_attributes (instance);
934 registered_function &rfn = add_function (instance, name, fntype, attrs,
935 required_extensions, false);
936
937 /* Enter the function into the hash table. */
938 hashval_t hash = instance.hash ();
939 registered_function **rfn_slot
940 = function_table->find_slot_with_hash (instance, hash, INSERT);
941 gcc_assert (!*rfn_slot);
942 *rfn_slot = &rfn;
943
944 /* Also add the function under its overloaded alias, if we want
945 a separate decl for each instance of an overloaded function. */
946 if (m_direct_overloads || force_direct_overloads)
947 {
948 char *overload_name = get_name (instance, true);
949 if (strcmp (name, overload_name) != 0)
950 {
951 /* Attribute lists shouldn't be shared. */
952 tree attrs = get_attributes (instance);
953 add_function (instance, overload_name, fntype, attrs,
954 required_extensions, false);
955 }
956 }
957
958 obstack_free (&m_string_obstack, name);
959 }
960
961 /* Add one function decl for INSTANCE, to be used with manual overload
962 resolution. REQUIRED_EXTENSIONS are the set of architecture extensions
963 that the function requires.
964
965 For simplicity, deal with duplicate attempts to add the same function,
966 including cases in which the new function requires more features than
967 the original one did. In that case we'll check whether the required
968 features are available as part of resolving the function to the
969 relevant unique function. */
970 void
971 function_builder::add_overloaded_function (const function_instance &instance,
972 uint64_t required_extensions)
973 {
974 char *name = get_name (instance, true);
975 if (registered_function **map_value = m_overload_names.get (name))
976 gcc_assert ((*map_value)->instance == instance
977 && ((*map_value)->required_extensions
978 & ~required_extensions) == 0);
979 else
980 {
981 registered_function &rfn
982 = add_function (instance, name, m_overload_type, NULL_TREE,
983 required_extensions, true);
984 const char *permanent_name = IDENTIFIER_POINTER (DECL_NAME (rfn.decl));
985 m_overload_names.put (permanent_name, &rfn);
986 }
987 obstack_free (&m_string_obstack, name);
988 }
989
990 /* If we are using manual overload resolution, add one function decl
991 for each overloaded function in GROUP. Take the function base name
992 from GROUP and the mode from MODE. */
993 void
994 function_builder::add_overloaded_functions (const function_group_info &group,
995 mode_suffix_index mode)
996 {
997 if (m_direct_overloads)
998 return;
999
1000 unsigned int explicit_type0 = (*group.shape)->explicit_type_suffix_p (0);
1001 unsigned int explicit_type1 = (*group.shape)->explicit_type_suffix_p (1);
1002 for (unsigned int pi = 0; group.preds[pi] != NUM_PREDS; ++pi)
1003 {
1004 if (!explicit_type0 && !explicit_type1)
1005 {
1006 /* Deal with the common case in which there is one overloaded
1007 function for all type combinations. */
1008 function_instance instance (group.base_name, *group.base,
1009 *group.shape, mode, types_none[0],
1010 group.preds[pi]);
1011 add_overloaded_function (instance, group.required_extensions);
1012 }
1013 else
1014 for (unsigned int ti = 0; group.types[ti][0] != NUM_TYPE_SUFFIXES;
1015 ++ti)
1016 {
1017 /* Stub out the types that are determined by overload
1018 resolution. */
1019 type_suffix_pair types = {
1020 explicit_type0 ? group.types[ti][0] : NUM_TYPE_SUFFIXES,
1021 explicit_type1 ? group.types[ti][1] : NUM_TYPE_SUFFIXES
1022 };
1023 function_instance instance (group.base_name, *group.base,
1024 *group.shape, mode, types,
1025 group.preds[pi]);
1026 add_overloaded_function (instance, group.required_extensions);
1027 }
1028 }
1029 }
1030
1031 /* Register all the functions in GROUP. */
1032 void
1033 function_builder::register_function_group (const function_group_info &group)
1034 {
1035 (*group.shape)->build (*this, group);
1036 }
1037
1038 function_call_info::function_call_info (location_t location_in,
1039 const function_instance &instance_in,
1040 tree fndecl_in)
1041 : function_instance (instance_in), location (location_in), fndecl (fndecl_in)
1042 {
1043 }
1044
1045 function_resolver::function_resolver (location_t location,
1046 const function_instance &instance,
1047 tree fndecl, vec<tree, va_gc> &arglist)
1048 : function_call_info (location, instance, fndecl), m_arglist (arglist)
1049 {
1050 }
1051
1052 /* Return the vector type associated with type suffix TYPE. */
1053 tree
1054 function_resolver::get_vector_type (type_suffix_index type)
1055 {
1056 return acle_vector_types[0][type_suffixes[type].vector_type];
1057 }
1058
1059 /* Return the <stdint.h> name associated with TYPE. Using the <stdint.h>
1060 name should be more user-friendly than the underlying canonical type,
1061 since it makes the signedness and bitwidth explicit. */
1062 const char *
1063 function_resolver::get_scalar_type_name (type_suffix_index type)
1064 {
1065 return vector_types[type_suffixes[type].vector_type].acle_name + 2;
1066 }
1067
1068 /* Return the type of argument I, or error_mark_node if it isn't
1069 well-formed. */
1070 tree
1071 function_resolver::get_argument_type (unsigned int i)
1072 {
1073 tree arg = m_arglist[i];
1074 return arg == error_mark_node ? arg : TREE_TYPE (arg);
1075 }
1076
1077 /* Return true if argument I is some form of scalar value. */
1078 bool
1079 function_resolver::scalar_argument_p (unsigned int i)
1080 {
1081 tree type = get_argument_type (i);
1082 return (INTEGRAL_TYPE_P (type)
1083 /* Allow pointer types, leaving the frontend to warn where
1084 necessary. */
1085 || POINTER_TYPE_P (type)
1086 || SCALAR_FLOAT_TYPE_P (type));
1087 }
1088
1089 /* Report that the function has no form that takes type suffix TYPE.
1090 Return error_mark_node. */
1091 tree
1092 function_resolver::report_no_such_form (type_suffix_index type)
1093 {
1094 error_at (location, "%qE has no form that takes %qT arguments",
1095 fndecl, get_vector_type (type));
1096 return error_mark_node;
1097 }
1098
1099 /* Silently check whether there is an instance of the function with the
1100 mode suffix given by MODE and the type suffixes given by TYPE0 and TYPE1.
1101 Return its function decl if so, otherwise return null. */
1102 tree
1103 function_resolver::lookup_form (mode_suffix_index mode,
1104 type_suffix_index type0,
1105 type_suffix_index type1)
1106 {
1107 type_suffix_pair types = { type0, type1 };
1108 function_instance instance (base_name, base, shape, mode, types, pred);
1109 registered_function *rfn
1110 = function_table->find_with_hash (instance, instance.hash ());
1111 return rfn ? rfn->decl : NULL_TREE;
1112 }
1113
1114 /* Resolve the function to one with the mode suffix given by MODE and the
1115 type suffixes given by TYPE0 and TYPE1. Return its function decl on
1116 success, otherwise report an error and return error_mark_node. */
1117 tree
1118 function_resolver::resolve_to (mode_suffix_index mode,
1119 type_suffix_index type0,
1120 type_suffix_index type1)
1121 {
1122 tree res = lookup_form (mode, type0, type1);
1123 if (!res)
1124 {
1125 if (type1 == NUM_TYPE_SUFFIXES)
1126 return report_no_such_form (type0);
1127 if (type0 == type_suffix_ids[0])
1128 return report_no_such_form (type1);
1129 /* To be filled in when we have other cases. */
1130 gcc_unreachable ();
1131 }
1132 return res;
1133 }
1134
1135 /* Require argument ARGNO to be a 32-bit or 64-bit scalar integer type.
1136 Return the associated type suffix on success, otherwise report an
1137 error and return NUM_TYPE_SUFFIXES. */
1138 type_suffix_index
1139 function_resolver::infer_integer_scalar_type (unsigned int argno)
1140 {
1141 tree actual = get_argument_type (argno);
1142 if (actual == error_mark_node)
1143 return NUM_TYPE_SUFFIXES;
1144
1145 /* Allow enums and booleans to decay to integers, for compatibility
1146 with C++ overloading rules. */
1147 if (INTEGRAL_TYPE_P (actual))
1148 {
1149 bool uns_p = TYPE_UNSIGNED (actual);
1150 /* Honor the usual integer promotions, so that resolution works
1151 in the same way as for C++. */
1152 if (TYPE_PRECISION (actual) < 32)
1153 return TYPE_SUFFIX_s32;
1154 if (TYPE_PRECISION (actual) == 32)
1155 return uns_p ? TYPE_SUFFIX_u32 : TYPE_SUFFIX_s32;
1156 if (TYPE_PRECISION (actual) == 64)
1157 return uns_p ? TYPE_SUFFIX_u64 : TYPE_SUFFIX_s64;
1158 }
1159
1160 error_at (location, "passing %qT to argument %d of %qE, which expects"
1161 " a 32-bit or 64-bit integer type", actual, argno + 1, fndecl);
1162 return NUM_TYPE_SUFFIXES;
1163 }
1164
1165 /* Require argument ARGNO to be a pointer to a scalar type that has a
1166 corresponding type suffix. Return that type suffix on success,
1167 otherwise report an error and return NUM_TYPE_SUFFIXES.
1168 GATHER_SCATTER_P is true if the function is a gather/scatter
1169 operation, and so requires a pointer to 32-bit or 64-bit data. */
1170 type_suffix_index
1171 function_resolver::infer_pointer_type (unsigned int argno,
1172 bool gather_scatter_p)
1173 {
1174 tree actual = get_argument_type (argno);
1175 if (actual == error_mark_node)
1176 return NUM_TYPE_SUFFIXES;
1177
1178 if (TREE_CODE (actual) != POINTER_TYPE)
1179 {
1180 error_at (location, "passing %qT to argument %d of %qE, which"
1181 " expects a pointer type", actual, argno + 1, fndecl);
1182 if (VECTOR_TYPE_P (actual) && gather_scatter_p)
1183 inform (location, "an explicit type suffix is needed"
1184 " when using a vector of base addresses");
1185 return NUM_TYPE_SUFFIXES;
1186 }
1187
1188 tree target = TREE_TYPE (actual);
1189 type_suffix_index type = find_type_suffix_for_scalar_type (target);
1190 if (type == NUM_TYPE_SUFFIXES)
1191 {
1192 error_at (location, "passing %qT to argument %d of %qE, but %qT is not"
1193 " a valid SVE element type", actual, argno + 1, fndecl,
1194 build_qualified_type (target, 0));
1195 return NUM_TYPE_SUFFIXES;
1196 }
1197 unsigned int bits = type_suffixes[type].element_bits;
1198 if (gather_scatter_p && bits != 32 && bits != 64)
1199 {
1200 error_at (location, "passing %qT to argument %d of %qE, which"
1201 " expects a pointer to 32-bit or 64-bit elements",
1202 actual, argno + 1, fndecl);
1203 return NUM_TYPE_SUFFIXES;
1204 }
1205
1206 return type;
1207 }
1208
1209 /* Require argument ARGNO to be a single vector or a tuple of NUM_VECTORS
1210 vectors; NUM_VECTORS is 1 for the former. Return the associated type
1211 suffix on success, using TYPE_SUFFIX_b for predicates. Report an error
1212 and return NUM_TYPE_SUFFIXES on failure. */
1213 type_suffix_index
1214 function_resolver::infer_vector_or_tuple_type (unsigned int argno,
1215 unsigned int num_vectors)
1216 {
1217 tree actual = get_argument_type (argno);
1218 if (actual == error_mark_node)
1219 return NUM_TYPE_SUFFIXES;
1220
1221 /* A linear search should be OK here, since the code isn't hot and
1222 the number of types is only small. */
1223 for (unsigned int size_i = 0; size_i < MAX_TUPLE_SIZE; ++size_i)
1224 for (unsigned int suffix_i = 0; suffix_i < NUM_TYPE_SUFFIXES; ++suffix_i)
1225 {
1226 vector_type_index type_i = type_suffixes[suffix_i].vector_type;
1227 tree type = acle_vector_types[size_i][type_i];
1228 if (type && TYPE_MAIN_VARIANT (actual) == TYPE_MAIN_VARIANT (type))
1229 {
1230 if (size_i + 1 == num_vectors)
1231 return type_suffix_index (suffix_i);
1232
1233 if (num_vectors == 1)
1234 error_at (location, "passing %qT to argument %d of %qE, which"
1235 " expects a single SVE vector rather than a tuple",
1236 actual, argno + 1, fndecl);
1237 else if (size_i == 0 && type_i != VECTOR_TYPE_svbool_t)
1238 error_at (location, "passing single vector %qT to argument %d"
1239 " of %qE, which expects a tuple of %d vectors",
1240 actual, argno + 1, fndecl, num_vectors);
1241 else
1242 error_at (location, "passing %qT to argument %d of %qE, which"
1243 " expects a tuple of %d vectors", actual, argno + 1,
1244 fndecl, num_vectors);
1245 return NUM_TYPE_SUFFIXES;
1246 }
1247 }
1248
1249 if (num_vectors == 1)
1250 error_at (location, "passing %qT to argument %d of %qE, which"
1251 " expects an SVE vector type", actual, argno + 1, fndecl);
1252 else
1253 error_at (location, "passing %qT to argument %d of %qE, which"
1254 " expects an SVE tuple type", actual, argno + 1, fndecl);
1255 return NUM_TYPE_SUFFIXES;
1256 }
1257
1258 /* Require argument ARGNO to have some form of vector type. Return the
1259 associated type suffix on success, using TYPE_SUFFIX_b for predicates.
1260 Report an error and return NUM_TYPE_SUFFIXES on failure. */
1261 type_suffix_index
1262 function_resolver::infer_vector_type (unsigned int argno)
1263 {
1264 return infer_vector_or_tuple_type (argno, 1);
1265 }
1266
1267 /* Like infer_vector_type, but also require the type to be integral. */
1268 type_suffix_index
1269 function_resolver::infer_integer_vector_type (unsigned int argno)
1270 {
1271 type_suffix_index type = infer_vector_type (argno);
1272 if (type == NUM_TYPE_SUFFIXES)
1273 return type;
1274
1275 if (!type_suffixes[type].integer_p)
1276 {
1277 error_at (location, "passing %qT to argument %d of %qE, which"
1278 " expects a vector of integers", get_argument_type (argno),
1279 argno + 1, fndecl);
1280 return NUM_TYPE_SUFFIXES;
1281 }
1282
1283 return type;
1284 }
1285
1286 /* Like infer_vector_type, but also require the type to be an unsigned
1287 integer. */
1288 type_suffix_index
1289 function_resolver::infer_unsigned_vector_type (unsigned int argno)
1290 {
1291 type_suffix_index type = infer_vector_type (argno);
1292 if (type == NUM_TYPE_SUFFIXES)
1293 return type;
1294
1295 if (!type_suffixes[type].unsigned_p)
1296 {
1297 error_at (location, "passing %qT to argument %d of %qE, which"
1298 " expects a vector of unsigned integers",
1299 get_argument_type (argno), argno + 1, fndecl);
1300 return NUM_TYPE_SUFFIXES;
1301 }
1302
1303 return type;
1304 }
1305
1306 /* Like infer_vector_type, but also require the element size to be
1307 32 or 64 bits. */
1308 type_suffix_index
1309 function_resolver::infer_sd_vector_type (unsigned int argno)
1310 {
1311 type_suffix_index type = infer_vector_type (argno);
1312 if (type == NUM_TYPE_SUFFIXES)
1313 return type;
1314
1315 unsigned int bits = type_suffixes[type].element_bits;
1316 if (bits != 32 && bits != 64)
1317 {
1318 error_at (location, "passing %qT to argument %d of %qE, which"
1319 " expects a vector of 32-bit or 64-bit elements",
1320 get_argument_type (argno), argno + 1, fndecl);
1321 return NUM_TYPE_SUFFIXES;
1322 }
1323
1324 return type;
1325 }
1326
1327 /* If the function operates on tuples of vectors, require argument ARGNO to be
1328 a tuple with the appropriate number of vectors, otherwise require it to be
1329 a single vector. Return the associated type suffix on success, using
1330 TYPE_SUFFIX_b for predicates. Report an error and return NUM_TYPE_SUFFIXES
1331 on failure. */
1332 type_suffix_index
1333 function_resolver::infer_tuple_type (unsigned int argno)
1334 {
1335 return infer_vector_or_tuple_type (argno, vectors_per_tuple ());
1336 }
1337
1338 /* Require argument ARGNO to be a vector or scalar argument. Return true
1339 if it is, otherwise report an appropriate error. */
1340 bool
1341 function_resolver::require_vector_or_scalar_type (unsigned int argno)
1342 {
1343 tree actual = get_argument_type (argno);
1344 if (actual == error_mark_node)
1345 return false;
1346
1347 if (!scalar_argument_p (argno) && !VECTOR_TYPE_P (actual))
1348 {
1349 error_at (location, "passing %qT to argument %d of %qE, which"
1350 " expects a vector or scalar type", actual, argno + 1, fndecl);
1351 return false;
1352 }
1353
1354 return true;
1355 }
1356
1357 /* Require argument ARGNO to have vector type TYPE, in cases where this
1358 requirement holds for all uses of the function. Return true if the
1359 argument has the right form, otherwise report an appropriate error. */
1360 bool
1361 function_resolver::require_vector_type (unsigned int argno,
1362 vector_type_index type)
1363 {
1364 tree expected = acle_vector_types[0][type];
1365 tree actual = get_argument_type (argno);
1366 if (actual != error_mark_node
1367 && TYPE_MAIN_VARIANT (expected) != TYPE_MAIN_VARIANT (actual))
1368 {
1369 error_at (location, "passing %qT to argument %d of %qE, which"
1370 " expects %qT", actual, argno + 1, fndecl, expected);
1371 return false;
1372 }
1373 return true;
1374 }
1375
1376 /* Like require_vector_type, but TYPE is inferred from previous arguments
1377 rather than being a fixed part of the function signature. This changes
1378 the nature of the error messages. */
1379 bool
1380 function_resolver::require_matching_vector_type (unsigned int argno,
1381 type_suffix_index type)
1382 {
1383 type_suffix_index new_type = infer_vector_type (argno);
1384 if (new_type == NUM_TYPE_SUFFIXES)
1385 return false;
1386
1387 if (type != new_type)
1388 {
1389 error_at (location, "passing %qT to argument %d of %qE, but"
1390 " previous arguments had type %qT",
1391 get_vector_type (new_type), argno + 1, fndecl,
1392 get_vector_type (type));
1393 return false;
1394 }
1395 return true;
1396 }
1397
1398 /* Require argument ARGNO to be a vector type with the following properties:
1399
1400 - the type class must be the same as FIRST_TYPE's if EXPECTED_TCLASS
1401 is SAME_TYPE_CLASS, otherwise it must be EXPECTED_TCLASS itself.
1402
1403 - the element size must be:
1404
1405 - the same as FIRST_TYPE's if EXPECTED_BITS == SAME_SIZE
1406 - half of FIRST_TYPE's if EXPECTED_BITS == HALF_SIZE
1407 - a quarter of FIRST_TYPE's if EXPECTED_BITS == QUARTER_SIZE
1408 - EXPECTED_BITS itself otherwise
1409
1410 Return true if the argument has the required type, otherwise report
1411 an appropriate error.
1412
1413 FIRST_ARGNO is the first argument that is known to have type FIRST_TYPE.
1414 Usually it comes before ARGNO, but sometimes it is more natural to resolve
1415 arguments out of order.
1416
1417 If the required properties depend on FIRST_TYPE then both FIRST_ARGNO and
1418 ARGNO contribute to the resolution process. If the required properties
1419 are fixed, only FIRST_ARGNO contributes to the resolution process.
1420
1421 This function is a bit of a Swiss army knife. The complication comes
1422 from trying to give good error messages when FIRST_ARGNO and ARGNO are
1423 inconsistent, since either of them might be wrong. */
1424 bool function_resolver::
1425 require_derived_vector_type (unsigned int argno,
1426 unsigned int first_argno,
1427 type_suffix_index first_type,
1428 type_class_index expected_tclass,
1429 unsigned int expected_bits)
1430 {
1431 /* If the type needs to match FIRST_ARGNO exactly, use the preferred
1432 error message for that case. The VECTOR_TYPE_P test excludes tuple
1433 types, which we handle below instead. */
1434 bool both_vectors_p = VECTOR_TYPE_P (get_argument_type (first_argno));
1435 if (both_vectors_p
1436 && expected_tclass == SAME_TYPE_CLASS
1437 && expected_bits == SAME_SIZE)
1438 {
1439 /* There's no need to resolve this case out of order. */
1440 gcc_assert (argno > first_argno);
1441 return require_matching_vector_type (argno, first_type);
1442 }
1443
1444 /* Use FIRST_TYPE to get the expected type class and element size. */
1445 type_class_index orig_expected_tclass = expected_tclass;
1446 if (expected_tclass == NUM_TYPE_CLASSES)
1447 expected_tclass = type_suffixes[first_type].tclass;
1448
1449 unsigned int orig_expected_bits = expected_bits;
1450 if (expected_bits == SAME_SIZE)
1451 expected_bits = type_suffixes[first_type].element_bits;
1452 else if (expected_bits == HALF_SIZE)
1453 expected_bits = type_suffixes[first_type].element_bits / 2;
1454 else if (expected_bits == QUARTER_SIZE)
1455 expected_bits = type_suffixes[first_type].element_bits / 4;
1456
1457 /* If the expected type doesn't depend on FIRST_TYPE at all,
1458 just check for the fixed choice of vector type. */
1459 if (expected_tclass == orig_expected_tclass
1460 && expected_bits == orig_expected_bits)
1461 {
1462 const type_suffix_info &expected_suffix
1463 = type_suffixes[find_type_suffix (expected_tclass, expected_bits)];
1464 return require_vector_type (argno, expected_suffix.vector_type);
1465 }
1466
1467 /* Require the argument to be some form of SVE vector type,
1468 without being specific about the type of vector we want. */
1469 type_suffix_index actual_type = infer_vector_type (argno);
1470 if (actual_type == NUM_TYPE_SUFFIXES)
1471 return false;
1472
1473 /* Exit now if we got the right type. */
1474 bool tclass_ok_p = (type_suffixes[actual_type].tclass == expected_tclass);
1475 bool size_ok_p = (type_suffixes[actual_type].element_bits == expected_bits);
1476 if (tclass_ok_p && size_ok_p)
1477 return true;
1478
1479 /* First look for cases in which the actual type contravenes a fixed
1480 size requirement, without having to refer to FIRST_TYPE. */
1481 if (!size_ok_p && expected_bits == orig_expected_bits)
1482 {
1483 error_at (location, "passing %qT to argument %d of %qE, which"
1484 " expects a vector of %d-bit elements",
1485 get_vector_type (actual_type), argno + 1, fndecl,
1486 expected_bits);
1487 return false;
1488 }
1489
1490 /* Likewise for a fixed type class requirement. This is only ever
1491 needed for signed and unsigned types, so don't create unnecessary
1492 translation work for other type classes. */
1493 if (!tclass_ok_p && orig_expected_tclass == TYPE_signed)
1494 {
1495 error_at (location, "passing %qT to argument %d of %qE, which"
1496 " expects a vector of signed integers",
1497 get_vector_type (actual_type), argno + 1, fndecl);
1498 return false;
1499 }
1500 if (!tclass_ok_p && orig_expected_tclass == TYPE_unsigned)
1501 {
1502 error_at (location, "passing %qT to argument %d of %qE, which"
1503 " expects a vector of unsigned integers",
1504 get_vector_type (actual_type), argno + 1, fndecl);
1505 return false;
1506 }
1507
1508 /* Make sure that FIRST_TYPE itself is sensible before using it
1509 as a basis for an error message. */
1510 if (resolve_to (mode_suffix_id, first_type) == error_mark_node)
1511 return false;
1512
1513 /* If the arguments have consistent type classes, but a link between
1514 the sizes has been broken, try to describe the error in those terms. */
1515 if (both_vectors_p && tclass_ok_p && orig_expected_bits == SAME_SIZE)
1516 {
1517 if (argno < first_argno)
1518 {
1519 std::swap (argno, first_argno);
1520 std::swap (actual_type, first_type);
1521 }
1522 error_at (location, "arguments %d and %d of %qE must have the"
1523 " same element size, but the values passed here have type"
1524 " %qT and %qT respectively", first_argno + 1, argno + 1,
1525 fndecl, get_vector_type (first_type),
1526 get_vector_type (actual_type));
1527 return false;
1528 }
1529
1530 /* Likewise in reverse: look for cases in which the sizes are consistent
1531 but a link between the type classes has been broken. */
1532 if (both_vectors_p
1533 && size_ok_p
1534 && orig_expected_tclass == SAME_TYPE_CLASS
1535 && type_suffixes[first_type].integer_p
1536 && type_suffixes[actual_type].integer_p)
1537 {
1538 if (argno < first_argno)
1539 {
1540 std::swap (argno, first_argno);
1541 std::swap (actual_type, first_type);
1542 }
1543 error_at (location, "arguments %d and %d of %qE must have the"
1544 " same signedness, but the values passed here have type"
1545 " %qT and %qT respectively", first_argno + 1, argno + 1,
1546 fndecl, get_vector_type (first_type),
1547 get_vector_type (actual_type));
1548 return false;
1549 }
1550
1551 /* The two arguments are wildly inconsistent. */
1552 type_suffix_index expected_type
1553 = find_type_suffix (expected_tclass, expected_bits);
1554 error_at (location, "passing %qT instead of the expected %qT to argument"
1555 " %d of %qE, after passing %qT to argument %d",
1556 get_vector_type (actual_type), get_vector_type (expected_type),
1557 argno + 1, fndecl, get_argument_type (first_argno),
1558 first_argno + 1);
1559 return false;
1560 }
1561
1562 /* Require argument ARGNO to match argument FIRST_ARGNO, which was inferred
1563 to be a pointer to a scalar element of type TYPE. */
1564 bool
1565 function_resolver::require_matching_pointer_type (unsigned int argno,
1566 unsigned int first_argno,
1567 type_suffix_index type)
1568 {
1569 type_suffix_index new_type = infer_pointer_type (argno);
1570 if (new_type == NUM_TYPE_SUFFIXES)
1571 return false;
1572
1573 if (type != new_type)
1574 {
1575 error_at (location, "passing %qT to argument %d of %qE, but"
1576 " argument %d had type %qT", get_argument_type (argno),
1577 argno + 1, fndecl, first_argno + 1,
1578 get_argument_type (first_argno));
1579 return false;
1580 }
1581 return true;
1582 }
1583
1584 /* Require argument ARGNO to be a (possibly variable) scalar, using EXPECTED
1585 as the name of its expected type. Return true if the argument has the
1586 right form, otherwise report an appropriate error. */
1587 bool
1588 function_resolver::require_scalar_type (unsigned int argno,
1589 const char *expected)
1590 {
1591 if (!scalar_argument_p (argno))
1592 {
1593 error_at (location, "passing %qT to argument %d of %qE, which"
1594 " expects %qs", get_argument_type (argno), argno + 1,
1595 fndecl, expected);
1596 return false;
1597 }
1598 return true;
1599 }
1600
1601 /* Require argument ARGNO to be some form of pointer, without being specific
1602 about its target type. Return true if the argument has the right form,
1603 otherwise report an appropriate error. */
1604 bool
1605 function_resolver::require_pointer_type (unsigned int argno)
1606 {
1607 if (!scalar_argument_p (argno))
1608 {
1609 error_at (location, "passing %qT to argument %d of %qE, which"
1610 " expects a scalar pointer", get_argument_type (argno),
1611 argno + 1, fndecl);
1612 return false;
1613 }
1614 return true;
1615 }
1616
1617 /* Argument FIRST_ARGNO is a scalar with type EXPECTED_TYPE, and argument
1618 ARGNO should be consistent with it. Return true if it is, otherwise
1619 report an appropriate error. */
1620 bool function_resolver::
1621 require_matching_integer_scalar_type (unsigned int argno,
1622 unsigned int first_argno,
1623 type_suffix_index expected_type)
1624 {
1625 type_suffix_index actual_type = infer_integer_scalar_type (argno);
1626 if (actual_type == NUM_TYPE_SUFFIXES)
1627 return false;
1628
1629 if (actual_type == expected_type)
1630 return true;
1631
1632 error_at (location, "call to %qE is ambiguous; argument %d has type"
1633 " %qs but argument %d has type %qs", fndecl,
1634 first_argno + 1, get_scalar_type_name (expected_type),
1635 argno + 1, get_scalar_type_name (actual_type));
1636 return false;
1637 }
1638
1639 /* Require argument ARGNO to be a (possibly variable) scalar, expecting it
1640 to have the following properties:
1641
1642 - the type class must be the same as for type suffix 0 if EXPECTED_TCLASS
1643 is SAME_TYPE_CLASS, otherwise it must be EXPECTED_TCLASS itself.
1644
1645 - the element size must be the same as for type suffix 0 if EXPECTED_BITS
1646 is SAME_TYPE_SIZE, otherwise it must be EXPECTED_BITS itself.
1647
1648 Return true if the argument is valid, otherwise report an appropriate error.
1649
1650 Note that we don't check whether the scalar type actually has the required
1651 properties, since that's subject to implicit promotions and conversions.
1652 Instead we just use the expected properties to tune the error message. */
1653 bool function_resolver::
1654 require_derived_scalar_type (unsigned int argno,
1655 type_class_index expected_tclass,
1656 unsigned int expected_bits)
1657 {
1658 gcc_assert (expected_tclass == SAME_TYPE_CLASS
1659 || expected_tclass == TYPE_signed
1660 || expected_tclass == TYPE_unsigned);
1661
1662 /* If the expected type doesn't depend on the type suffix at all,
1663 just check for the fixed choice of scalar type. */
1664 if (expected_tclass != SAME_TYPE_CLASS && expected_bits != SAME_SIZE)
1665 {
1666 type_suffix_index expected_type
1667 = find_type_suffix (expected_tclass, expected_bits);
1668 return require_scalar_type (argno, get_scalar_type_name (expected_type));
1669 }
1670
1671 if (scalar_argument_p (argno))
1672 return true;
1673
1674 if (expected_tclass == SAME_TYPE_CLASS)
1675 /* It doesn't really matter whether the element is expected to be
1676 the same size as type suffix 0. */
1677 error_at (location, "passing %qT to argument %d of %qE, which"
1678 " expects a scalar element", get_argument_type (argno),
1679 argno + 1, fndecl);
1680 else
1681 /* It doesn't seem useful to distinguish between signed and unsigned
1682 scalars here. */
1683 error_at (location, "passing %qT to argument %d of %qE, which"
1684 " expects a scalar integer", get_argument_type (argno),
1685 argno + 1, fndecl);
1686 return false;
1687 }
1688
1689 /* Require argument ARGNO to be suitable for an integer constant expression.
1690 Return true if it is, otherwise report an appropriate error.
1691
1692 function_checker checks whether the argument is actually constant and
1693 has a suitable range. The reason for distinguishing immediate arguments
1694 here is because it provides more consistent error messages than
1695 require_scalar_type would. */
1696 bool
1697 function_resolver::require_integer_immediate (unsigned int argno)
1698 {
1699 if (!scalar_argument_p (argno))
1700 {
1701 report_non_ice (location, fndecl, argno);
1702 return false;
1703 }
1704 return true;
1705 }
1706
1707 /* Require argument ARGNO to be a vector base in a gather-style address.
1708 Return its type on success, otherwise return NUM_VECTOR_TYPES. */
1709 vector_type_index
1710 function_resolver::infer_vector_base_type (unsigned int argno)
1711 {
1712 type_suffix_index type = infer_vector_type (argno);
1713 if (type == NUM_TYPE_SUFFIXES)
1714 return NUM_VECTOR_TYPES;
1715
1716 if (type == TYPE_SUFFIX_u32 || type == TYPE_SUFFIX_u64)
1717 return type_suffixes[type].vector_type;
1718
1719 error_at (location, "passing %qT to argument %d of %qE, which"
1720 " expects %qs or %qs", get_argument_type (argno),
1721 argno + 1, fndecl, "svuint32_t", "svuint64_t");
1722 return NUM_VECTOR_TYPES;
1723 }
1724
1725 /* Require argument ARGNO to be a vector displacement in a gather-style
1726 address. Return its type on success, otherwise return NUM_VECTOR_TYPES. */
1727 vector_type_index
1728 function_resolver::infer_vector_displacement_type (unsigned int argno)
1729 {
1730 type_suffix_index type = infer_integer_vector_type (argno);
1731 if (type == NUM_TYPE_SUFFIXES)
1732 return NUM_VECTOR_TYPES;
1733
1734 if (type_suffixes[type].integer_p
1735 && (type_suffixes[type].element_bits == 32
1736 || type_suffixes[type].element_bits == 64))
1737 return type_suffixes[type].vector_type;
1738
1739 error_at (location, "passing %qT to argument %d of %qE, which"
1740 " expects a vector of 32-bit or 64-bit integers",
1741 get_argument_type (argno), argno + 1, fndecl);
1742 return NUM_VECTOR_TYPES;
1743 }
1744
1745 /* Require argument ARGNO to be a vector displacement in a gather-style
1746 address. There are three possible uses:
1747
1748 - for loading into elements of type TYPE (when LOAD_P is true)
1749 - for storing from elements of type TYPE (when LOAD_P is false)
1750 - for prefetching data (when TYPE is NUM_TYPE_SUFFIXES)
1751
1752 The overloaded function's mode suffix determines the units of the
1753 displacement (bytes for "_offset", elements for "_index").
1754
1755 Return the associated mode on success, otherwise report an error
1756 and return MODE_none. */
1757 mode_suffix_index
1758 function_resolver::resolve_sv_displacement (unsigned int argno,
1759 type_suffix_index type,
1760 bool load_p)
1761 {
1762 if (type == NUM_TYPE_SUFFIXES)
1763 {
1764 /* For prefetches, the base is a void pointer and the displacement
1765 can be any valid offset or index type. */
1766 vector_type_index displacement_vector_type
1767 = infer_vector_displacement_type (argno);
1768 if (displacement_vector_type == NUM_VECTOR_TYPES)
1769 return MODE_none;
1770
1771 mode_suffix_index mode = find_mode_suffix (NUM_VECTOR_TYPES,
1772 displacement_vector_type,
1773 displacement_units ());
1774 gcc_assert (mode != MODE_none);
1775 return mode;
1776 }
1777
1778 unsigned int required_bits = type_suffixes[type].element_bits;
1779 if (required_bits == 32
1780 && displacement_units () == UNITS_elements
1781 && !lookup_form (MODE_s32index, type)
1782 && !lookup_form (MODE_u32index, type))
1783 {
1784 if (lookup_form (MODE_u32base_index, type))
1785 {
1786 if (type_suffix_ids[0] == NUM_TYPE_SUFFIXES)
1787 {
1788 gcc_assert (!load_p);
1789 error_at (location, "when storing %qT, %qE requires a vector"
1790 " base and a scalar index", get_vector_type (type),
1791 fndecl);
1792 }
1793 else
1794 error_at (location, "%qE requires a vector base and a scalar"
1795 " index", fndecl);
1796 }
1797 else
1798 error_at (location, "%qE does not support 32-bit vector type %qT",
1799 fndecl, get_vector_type (type));
1800 return MODE_none;
1801 }
1802
1803 /* Check for some form of vector type, without naming any in particular
1804 as being expected. */
1805 type_suffix_index displacement_type = infer_vector_type (argno);
1806 if (displacement_type == NUM_TYPE_SUFFIXES)
1807 return MODE_none;
1808
1809 /* If the displacement type is consistent with the data vector type,
1810 try to find the associated mode suffix. This will fall through
1811 for non-integral displacement types. */
1812 if (type_suffixes[displacement_type].element_bits == required_bits)
1813 {
1814 vector_type_index displacement_vector_type
1815 = type_suffixes[displacement_type].vector_type;
1816 mode_suffix_index mode = find_mode_suffix (NUM_VECTOR_TYPES,
1817 displacement_vector_type,
1818 displacement_units ());
1819 if (mode != MODE_none)
1820 {
1821 if (mode == MODE_s32offset
1822 && !lookup_form (mode, type)
1823 && lookup_form (MODE_u32offset, type))
1824 {
1825 if (type_suffix_ids[0] == NUM_TYPE_SUFFIXES)
1826 error_at (location, "%qE does not support 32-bit sign-extended"
1827 " offsets", fndecl);
1828 else
1829 error_at (location, "%qE does not support sign-extended"
1830 " offsets", fndecl);
1831 return MODE_none;
1832 }
1833 return mode;
1834 }
1835 }
1836
1837 if (type_suffix_ids[0] == NUM_TYPE_SUFFIXES)
1838 {
1839 /* TYPE has been inferred rather than specified by the user,
1840 so mention it in the error messages. */
1841 if (load_p)
1842 error_at (location, "passing %qT to argument %d of %qE, which when"
1843 " loading %qT expects a vector of %d-bit integers",
1844 get_argument_type (argno), argno + 1, fndecl,
1845 get_vector_type (type), required_bits);
1846 else
1847 error_at (location, "passing %qT to argument %d of %qE, which when"
1848 " storing %qT expects a vector of %d-bit integers",
1849 get_argument_type (argno), argno + 1, fndecl,
1850 get_vector_type (type), required_bits);
1851 }
1852 else
1853 /* TYPE is part of the function name. */
1854 error_at (location, "passing %qT to argument %d of %qE, which"
1855 " expects a vector of %d-bit integers",
1856 get_argument_type (argno), argno + 1, fndecl, required_bits);
1857 return MODE_none;
1858 }
1859
1860 /* Require the arguments starting at ARGNO to form a gather-style address.
1861 There are three possible uses:
1862
1863 - for loading into elements of type TYPE (when LOAD_P is true)
1864 - for storing from elements of type TYPE (when LOAD_P is false)
1865 - for prefetching data (when TYPE is NUM_TYPE_SUFFIXES)
1866
1867 The three possible addresses are:
1868
1869 - a vector base with no displacement
1870 - a vector base and a scalar displacement
1871 - a scalar (pointer) base and a vector displacement
1872
1873 The overloaded function's mode suffix determines whether there is
1874 a displacement, and if so, what units it uses:
1875
1876 - MODE_none: no displacement
1877 - MODE_offset: the displacement is measured in bytes
1878 - MODE_index: the displacement is measured in elements
1879
1880 Return the mode of the non-overloaded function on success, otherwise
1881 report an error and return MODE_none. */
1882 mode_suffix_index
1883 function_resolver::resolve_gather_address (unsigned int argno,
1884 type_suffix_index type,
1885 bool load_p)
1886 {
1887 tree actual = get_argument_type (argno);
1888 if (actual == error_mark_node)
1889 return MODE_none;
1890
1891 if (displacement_units () != UNITS_none)
1892 {
1893 /* Some form of displacement is needed. First handle a scalar
1894 pointer base and a vector displacement. */
1895 if (scalar_argument_p (argno))
1896 /* Don't check the pointer type here, since there's only one valid
1897 choice. Leave that to the frontend. */
1898 return resolve_sv_displacement (argno + 1, type, load_p);
1899
1900 if (!VECTOR_TYPE_P (actual))
1901 {
1902 error_at (location, "passing %qT to argument %d of %qE,"
1903 " which expects a vector or pointer base address",
1904 actual, argno + 1, fndecl);
1905 return MODE_none;
1906 }
1907 }
1908
1909 /* Check for the correct choice of vector base type. */
1910 vector_type_index base_vector_type;
1911 if (type == NUM_TYPE_SUFFIXES)
1912 {
1913 /* Since prefetches have no type suffix, there is a free choice
1914 between 32-bit and 64-bit base addresses. */
1915 base_vector_type = infer_vector_base_type (argno);
1916 if (base_vector_type == NUM_VECTOR_TYPES)
1917 return MODE_none;
1918 }
1919 else
1920 {
1921 /* Check for some form of vector type, without saying which type
1922 we expect. */
1923 type_suffix_index base_type = infer_vector_type (argno);
1924 if (base_type == NUM_TYPE_SUFFIXES)
1925 return MODE_none;
1926
1927 /* Check whether the type is the right one. */
1928 unsigned int required_bits = type_suffixes[type].element_bits;
1929 gcc_assert (required_bits == 32 || required_bits == 64);
1930 type_suffix_index required_type = (required_bits == 32
1931 ? TYPE_SUFFIX_u32
1932 : TYPE_SUFFIX_u64);
1933 if (required_type != base_type)
1934 {
1935 error_at (location, "passing %qT to argument %d of %qE,"
1936 " which expects %qT", actual, argno + 1, fndecl,
1937 get_vector_type (required_type));
1938 return MODE_none;
1939 }
1940 base_vector_type = type_suffixes[base_type].vector_type;
1941 }
1942
1943 /* Check the scalar displacement, if any. */
1944 if (displacement_units () != UNITS_none
1945 && !require_scalar_type (argno + 1, "int64_t"))
1946 return MODE_none;
1947
1948 /* Find the appropriate mode suffix. The checks above should have
1949 weeded out all erroneous cases. */
1950 for (unsigned int mode_i = 0; mode_i < ARRAY_SIZE (mode_suffixes); ++mode_i)
1951 {
1952 const mode_suffix_info &mode = mode_suffixes[mode_i];
1953 if (mode.base_vector_type == base_vector_type
1954 && mode.displacement_vector_type == NUM_VECTOR_TYPES
1955 && mode.displacement_units == displacement_units ())
1956 return mode_suffix_index (mode_i);
1957 }
1958
1959 gcc_unreachable ();
1960 }
1961
1962 /* Require arguments ARGNO and ARGNO + 1 to form an ADR-style address,
1963 i.e. one with a vector of base addresses and a vector of displacements.
1964 The overloaded function's mode suffix determines the units of the
1965 displacement (bytes for "_offset", elements for "_index").
1966
1967 Return the associated mode suffix on success, otherwise report
1968 an error and return MODE_none. */
1969 mode_suffix_index
1970 function_resolver::resolve_adr_address (unsigned int argno)
1971 {
1972 vector_type_index base_type = infer_vector_base_type (argno);
1973 if (base_type == NUM_VECTOR_TYPES)
1974 return MODE_none;
1975
1976 vector_type_index displacement_type
1977 = infer_vector_displacement_type (argno + 1);
1978 if (displacement_type == NUM_VECTOR_TYPES)
1979 return MODE_none;
1980
1981 mode_suffix_index mode = find_mode_suffix (base_type, displacement_type,
1982 displacement_units ());
1983 if (mode == MODE_none)
1984 {
1985 if (mode_suffix_id == MODE_offset)
1986 error_at (location, "cannot combine a base of type %qT with"
1987 " an offset of type %qT",
1988 get_argument_type (argno), get_argument_type (argno + 1));
1989 else
1990 error_at (location, "cannot combine a base of type %qT with"
1991 " an index of type %qT",
1992 get_argument_type (argno), get_argument_type (argno + 1));
1993 }
1994 return mode;
1995 }
1996
1997 /* Require the function to have exactly EXPECTED arguments. Return true
1998 if it does, otherwise report an appropriate error. */
1999 bool
2000 function_resolver::check_num_arguments (unsigned int expected)
2001 {
2002 if (m_arglist.length () < expected)
2003 error_at (location, "too few arguments to function %qE", fndecl);
2004 else if (m_arglist.length () > expected)
2005 error_at (location, "too many arguments to function %qE", fndecl);
2006 return m_arglist.length () == expected;
2007 }
2008
2009 /* If the function is predicated, check that the first argument is a
2010 suitable governing predicate. Also check that there are NOPS further
2011 arguments after any governing predicate, but don't check what they are.
2012
2013 Return true on success, otherwise report a suitable error.
2014 When returning true:
2015
2016 - set I to the number of the first unchecked argument.
2017 - set NARGS to the total number of arguments. */
2018 bool
2019 function_resolver::check_gp_argument (unsigned int nops,
2020 unsigned int &i, unsigned int &nargs)
2021 {
2022 i = 0;
2023 if (pred != PRED_none)
2024 {
2025 /* Unary merge operations should use resolve_unary instead. */
2026 gcc_assert (nops != 1 || pred != PRED_m);
2027 nargs = nops + 1;
2028 if (!check_num_arguments (nargs)
2029 || !require_vector_type (i, VECTOR_TYPE_svbool_t))
2030 return false;
2031 i += 1;
2032 }
2033 else
2034 {
2035 nargs = nops;
2036 if (!check_num_arguments (nargs))
2037 return false;
2038 }
2039
2040 return true;
2041 }
2042
2043 /* Finish resolving a function whose final argument can be a vector
2044 or a scalar, with the function having an implicit "_n" suffix
2045 in the latter case. This "_n" form might only exist for certain
2046 type suffixes.
2047
2048 ARGNO is the index of the final argument. The inferred type suffix
2049 was obtained from argument FIRST_ARGNO, which has type FIRST_TYPE.
2050 EXPECTED_TCLASS and EXPECTED_BITS describe the expected properties
2051 of the final vector or scalar argument, in the same way as for
2052 require_derived_vector_type. INFERRED_TYPE is the inferred type
2053 suffix itself, or NUM_TYPE_SUFFIXES if it's the same as FIRST_TYPE.
2054
2055 Return the function decl of the resolved function on success,
2056 otherwise report a suitable error and return error_mark_node. */
2057 tree function_resolver::
2058 finish_opt_n_resolution (unsigned int argno, unsigned int first_argno,
2059 type_suffix_index first_type,
2060 type_class_index expected_tclass,
2061 unsigned int expected_bits,
2062 type_suffix_index inferred_type)
2063 {
2064 if (inferred_type == NUM_TYPE_SUFFIXES)
2065 inferred_type = first_type;
2066 tree scalar_form = lookup_form (MODE_n, inferred_type);
2067
2068 /* Allow the final argument to be scalar, if an _n form exists. */
2069 if (scalar_argument_p (argno))
2070 {
2071 if (scalar_form)
2072 return scalar_form;
2073
2074 /* Check the vector form normally. If that succeeds, raise an
2075 error about having no corresponding _n form. */
2076 tree res = resolve_to (mode_suffix_id, inferred_type);
2077 if (res != error_mark_node)
2078 error_at (location, "passing %qT to argument %d of %qE, but its"
2079 " %qT form does not accept scalars",
2080 get_argument_type (argno), argno + 1, fndecl,
2081 get_vector_type (first_type));
2082 return error_mark_node;
2083 }
2084
2085 /* If an _n form does exist, provide a more accurate message than
2086 require_derived_vector_type would for arguments that are neither
2087 vectors nor scalars. */
2088 if (scalar_form && !require_vector_or_scalar_type (argno))
2089 return error_mark_node;
2090
2091 /* Check for the correct vector type. */
2092 if (!require_derived_vector_type (argno, first_argno, first_type,
2093 expected_tclass, expected_bits))
2094 return error_mark_node;
2095
2096 return resolve_to (mode_suffix_id, inferred_type);
2097 }
2098
2099 /* Resolve a (possibly predicated) unary function. If the function uses
2100 merge predication or if TREAT_AS_MERGE_P is true, there is an extra
2101 vector argument before the governing predicate that specifies the
2102 values of inactive elements. This argument has the following
2103 properties:
2104
2105 - the type class must be the same as for active elements if MERGE_TCLASS
2106 is SAME_TYPE_CLASS, otherwise it must be MERGE_TCLASS itself.
2107
2108 - the element size must be the same as for active elements if MERGE_BITS
2109 is SAME_TYPE_SIZE, otherwise it must be MERGE_BITS itself.
2110
2111 Return the function decl of the resolved function on success,
2112 otherwise report a suitable error and return error_mark_node. */
2113 tree
2114 function_resolver::resolve_unary (type_class_index merge_tclass,
2115 unsigned int merge_bits,
2116 bool treat_as_merge_p)
2117 {
2118 type_suffix_index type;
2119 if (pred == PRED_m || treat_as_merge_p)
2120 {
2121 if (!check_num_arguments (3))
2122 return error_mark_node;
2123 if (merge_tclass == SAME_TYPE_CLASS && merge_bits == SAME_SIZE)
2124 {
2125 /* The inactive elements are the same as the active elements,
2126 so we can use normal left-to-right resolution. */
2127 if ((type = infer_vector_type (0)) == NUM_TYPE_SUFFIXES
2128 || !require_vector_type (1, VECTOR_TYPE_svbool_t)
2129 || !require_matching_vector_type (2, type))
2130 return error_mark_node;
2131 }
2132 else
2133 {
2134 /* The inactive element type is a function of the active one,
2135 so resolve the active one first. */
2136 if (!require_vector_type (1, VECTOR_TYPE_svbool_t)
2137 || (type = infer_vector_type (2)) == NUM_TYPE_SUFFIXES
2138 || !require_derived_vector_type (0, 2, type, merge_tclass,
2139 merge_bits))
2140 return error_mark_node;
2141 }
2142 }
2143 else
2144 {
2145 /* We just need to check the predicate (if any) and the single
2146 vector argument. */
2147 unsigned int i, nargs;
2148 if (!check_gp_argument (1, i, nargs)
2149 || (type = infer_vector_type (i)) == NUM_TYPE_SUFFIXES)
2150 return error_mark_node;
2151 }
2152
2153 /* Handle convert-like functions in which the first type suffix is
2154 explicit. */
2155 if (type_suffix_ids[0] != NUM_TYPE_SUFFIXES)
2156 return resolve_to (mode_suffix_id, type_suffix_ids[0], type);
2157
2158 return resolve_to (mode_suffix_id, type);
2159 }
2160
2161 /* Resolve a (possibly predicated) function that takes NOPS like-typed
2162 vector arguments followed by NIMM integer immediates. Return the
2163 function decl of the resolved function on success, otherwise report
2164 a suitable error and return error_mark_node. */
2165 tree
2166 function_resolver::resolve_uniform (unsigned int nops, unsigned int nimm)
2167 {
2168 unsigned int i, nargs;
2169 type_suffix_index type;
2170 if (!check_gp_argument (nops + nimm, i, nargs)
2171 || (type = infer_vector_type (i)) == NUM_TYPE_SUFFIXES)
2172 return error_mark_node;
2173
2174 i += 1;
2175 for (; i < nargs - nimm; ++i)
2176 if (!require_matching_vector_type (i, type))
2177 return error_mark_node;
2178
2179 for (; i < nargs; ++i)
2180 if (!require_integer_immediate (i))
2181 return error_mark_node;
2182
2183 return resolve_to (mode_suffix_id, type);
2184 }
2185
2186 /* Resolve a (possibly predicated) function that offers a choice between
2187 taking:
2188
2189 - NOPS like-typed vector arguments or
2190 - NOPS - 1 like-typed vector arguments followed by a scalar argument
2191
2192 Return the function decl of the resolved function on success,
2193 otherwise report a suitable error and return error_mark_node. */
2194 tree
2195 function_resolver::resolve_uniform_opt_n (unsigned int nops)
2196 {
2197 unsigned int i, nargs;
2198 type_suffix_index type;
2199 if (!check_gp_argument (nops, i, nargs)
2200 || (type = infer_vector_type (i)) == NUM_TYPE_SUFFIXES)
2201 return error_mark_node;
2202
2203 unsigned int first_arg = i++;
2204 for (; i < nargs - 1; ++i)
2205 if (!require_matching_vector_type (i, type))
2206 return error_mark_node;
2207
2208 return finish_opt_n_resolution (i, first_arg, type);
2209 }
2210
2211 /* If the call is erroneous, report an appropriate error and return
2212 error_mark_node. Otherwise, if the function is overloaded, return
2213 the decl of the non-overloaded function. Return NULL_TREE otherwise,
2214 indicating that the call should be processed in the normal way. */
2215 tree
2216 function_resolver::resolve ()
2217 {
2218 return shape->resolve (*this);
2219 }
2220
2221 function_checker::function_checker (location_t location,
2222 const function_instance &instance,
2223 tree fndecl, tree fntype,
2224 unsigned int nargs, tree *args)
2225 : function_call_info (location, instance, fndecl),
2226 m_fntype (fntype), m_nargs (nargs), m_args (args),
2227 /* We don't have to worry about unary _m operations here, since they
2228 never have arguments that need checking. */
2229 m_base_arg (pred != PRED_none ? 1 : 0)
2230 {
2231 }
2232
2233 /* Return true if argument ARGNO exists. which it might not for
2234 erroneous calls. It is safe to wave through checks if this
2235 function returns false. */
2236 bool
2237 function_checker::argument_exists_p (unsigned int argno)
2238 {
2239 gcc_assert (argno < (unsigned int) type_num_arguments (m_fntype));
2240 return argno < m_nargs;
2241 }
2242
2243 /* Check that argument ARGNO is an integer constant expression and
2244 store its value in VALUE_OUT if so. The caller should first
2245 check that argument ARGNO exists. */
2246 bool
2247 function_checker::require_immediate (unsigned int argno,
2248 HOST_WIDE_INT &value_out)
2249 {
2250 gcc_assert (argno < m_nargs);
2251 tree arg = m_args[argno];
2252
2253 /* The type and range are unsigned, so read the argument as an
2254 unsigned rather than signed HWI. */
2255 if (!tree_fits_uhwi_p (arg))
2256 {
2257 report_non_ice (location, fndecl, argno);
2258 return false;
2259 }
2260
2261 /* ...but treat VALUE_OUT as signed for error reporting, since printing
2262 -1 is more user-friendly than the maximum uint64_t value. */
2263 value_out = tree_to_uhwi (arg);
2264 return true;
2265 }
2266
2267 /* Check that argument REL_ARGNO is an integer constant expression that
2268 has the value VALUE0 or VALUE1. REL_ARGNO counts from the end of the
2269 predication arguments. */
2270 bool
2271 function_checker::require_immediate_either_or (unsigned int rel_argno,
2272 HOST_WIDE_INT value0,
2273 HOST_WIDE_INT value1)
2274 {
2275 unsigned int argno = m_base_arg + rel_argno;
2276 if (!argument_exists_p (argno))
2277 return true;
2278
2279 HOST_WIDE_INT actual;
2280 if (!require_immediate (argno, actual))
2281 return false;
2282
2283 if (actual != value0 && actual != value1)
2284 {
2285 report_neither_nor (location, fndecl, argno, actual, 90, 270);
2286 return false;
2287 }
2288
2289 return true;
2290 }
2291
2292 /* Check that argument REL_ARGNO is an integer constant expression that has
2293 a valid value for enumeration type TYPE. REL_ARGNO counts from the end
2294 of the predication arguments. */
2295 bool
2296 function_checker::require_immediate_enum (unsigned int rel_argno, tree type)
2297 {
2298 unsigned int argno = m_base_arg + rel_argno;
2299 if (!argument_exists_p (argno))
2300 return true;
2301
2302 HOST_WIDE_INT actual;
2303 if (!require_immediate (argno, actual))
2304 return false;
2305
2306 for (tree entry = TYPE_VALUES (type); entry; entry = TREE_CHAIN (entry))
2307 {
2308 /* The value is an INTEGER_CST for C and a CONST_DECL wrapper
2309 around an INTEGER_CST for C++. */
2310 tree value = TREE_VALUE (entry);
2311 if (TREE_CODE (value) == CONST_DECL)
2312 value = DECL_INITIAL (value);
2313 if (wi::to_widest (value) == actual)
2314 return true;
2315 }
2316
2317 report_not_enum (location, fndecl, argno, actual, type);
2318 return false;
2319 }
2320
2321 /* Check that argument REL_ARGNO is suitable for indexing argument
2322 REL_ARGNO - 1, in groups of GROUP_SIZE elements. REL_ARGNO counts
2323 from the end of the predication arguments. */
2324 bool
2325 function_checker::require_immediate_lane_index (unsigned int rel_argno,
2326 unsigned int group_size)
2327 {
2328 unsigned int argno = m_base_arg + rel_argno;
2329 if (!argument_exists_p (argno))
2330 return true;
2331
2332 /* Get the type of the previous argument. tree_argument_type wants a
2333 1-based number, whereas ARGNO is 0-based. */
2334 machine_mode mode = TYPE_MODE (type_argument_type (m_fntype, argno));
2335 gcc_assert (VECTOR_MODE_P (mode));
2336 unsigned int nlanes = 128 / (group_size * GET_MODE_UNIT_BITSIZE (mode));
2337 return require_immediate_range (rel_argno, 0, nlanes - 1);
2338 }
2339
2340 /* Check that argument REL_ARGNO is an integer constant expression that
2341 has one of the given values. */
2342 bool
2343 function_checker::require_immediate_one_of (unsigned int rel_argno,
2344 HOST_WIDE_INT value0,
2345 HOST_WIDE_INT value1,
2346 HOST_WIDE_INT value2,
2347 HOST_WIDE_INT value3)
2348 {
2349 unsigned int argno = m_base_arg + rel_argno;
2350 if (!argument_exists_p (argno))
2351 return true;
2352
2353 HOST_WIDE_INT actual;
2354 if (!require_immediate (argno, actual))
2355 return false;
2356
2357 if (actual != value0
2358 && actual != value1
2359 && actual != value2
2360 && actual != value3)
2361 {
2362 report_not_one_of (location, fndecl, argno, actual,
2363 value0, value1, value2, value3);
2364 return false;
2365 }
2366
2367 return true;
2368 }
2369
2370 /* Check that argument REL_ARGNO is an integer constant expression in the
2371 range [MIN, MAX]. REL_ARGNO counts from the end of the predication
2372 arguments. */
2373 bool
2374 function_checker::require_immediate_range (unsigned int rel_argno,
2375 HOST_WIDE_INT min,
2376 HOST_WIDE_INT max)
2377 {
2378 unsigned int argno = m_base_arg + rel_argno;
2379 if (!argument_exists_p (argno))
2380 return true;
2381
2382 /* Required because of the tree_to_uhwi -> HOST_WIDE_INT conversion
2383 in require_immediate. */
2384 gcc_assert (min >= 0 && min <= max);
2385 HOST_WIDE_INT actual;
2386 if (!require_immediate (argno, actual))
2387 return false;
2388
2389 if (!IN_RANGE (actual, min, max))
2390 {
2391 report_out_of_range (location, fndecl, argno, actual, min, max);
2392 return false;
2393 }
2394
2395 return true;
2396 }
2397
2398 /* Perform semantic checks on the call. Return true if the call is valid,
2399 otherwise report a suitable error. */
2400 bool
2401 function_checker::check ()
2402 {
2403 function_args_iterator iter;
2404 tree type;
2405 unsigned int i = 0;
2406 FOREACH_FUNCTION_ARGS (m_fntype, type, iter)
2407 {
2408 if (type == void_type_node || i >= m_nargs)
2409 break;
2410
2411 if (i >= m_base_arg
2412 && TREE_CODE (type) == ENUMERAL_TYPE
2413 && !require_immediate_enum (i - m_base_arg, type))
2414 return false;
2415
2416 i += 1;
2417 }
2418
2419 return shape->check (*this);
2420 }
2421
2422 gimple_folder::gimple_folder (const function_instance &instance, tree fndecl,
2423 gimple_stmt_iterator *gsi_in, gcall *call_in)
2424 : function_call_info (gimple_location (call_in), instance, fndecl),
2425 gsi (gsi_in), call (call_in), lhs (gimple_call_lhs (call_in))
2426 {
2427 }
2428
2429 /* VALUE might be a vector of type VECTYPE or a single scalar element.
2430 Duplicate it into a vector of type VECTYPE in the latter case, adding any
2431 new statements to STMTS. */
2432 tree
2433 gimple_folder::force_vector (gimple_seq &stmts, tree vectype, tree value)
2434 {
2435 if (!VECTOR_TYPE_P (TREE_TYPE (value)))
2436 value = gimple_build_vector_from_val (&stmts, vectype, value);
2437 return value;
2438 }
2439
2440 /* Convert predicate argument ARGNO so that it has the type appropriate for
2441 an operation on VECTYPE. Add any new statements to STMTS. */
2442 tree
2443 gimple_folder::convert_pred (gimple_seq &stmts, tree vectype,
2444 unsigned int argno)
2445 {
2446 tree pred = gimple_call_arg (call, argno);
2447 if (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (pred)),
2448 TYPE_VECTOR_SUBPARTS (vectype)))
2449 return pred;
2450
2451 return gimple_build (&stmts, VIEW_CONVERT_EXPR,
2452 truth_type_for (vectype), pred);
2453 }
2454
2455 /* Return a pointer to the address in a contiguous load or store,
2456 given that each memory vector has type VECTYPE. Add any new
2457 statements to STMTS. */
2458 tree
2459 gimple_folder::fold_contiguous_base (gimple_seq &stmts, tree vectype)
2460 {
2461 tree base = gimple_call_arg (call, 1);
2462 if (mode_suffix_id == MODE_vnum)
2463 {
2464 tree offset = gimple_call_arg (call, 2);
2465 offset = gimple_convert (&stmts, sizetype, offset);
2466 offset = gimple_build (&stmts, MULT_EXPR, sizetype, offset,
2467 TYPE_SIZE_UNIT (vectype));
2468 base = gimple_build (&stmts, POINTER_PLUS_EXPR, TREE_TYPE (base),
2469 base, offset);
2470 }
2471 return base;
2472 }
2473
2474 /* Return the alignment and TBAA argument to an internal load or store
2475 function like IFN_MASK_LOAD or IFN_MASK_STORE, given that it accesses
2476 memory elements of type TYPE. */
2477 tree
2478 gimple_folder::load_store_cookie (tree type)
2479 {
2480 return build_int_cst (build_pointer_type (type), TYPE_ALIGN_UNIT (type));
2481 }
2482
2483 /* Fold the call to a call to INSTANCE, with the same arguments. */
2484 gimple *
2485 gimple_folder::redirect_call (const function_instance &instance)
2486 {
2487 registered_function *rfn
2488 = function_table->find_with_hash (instance, instance.hash ());
2489 if (!rfn)
2490 return NULL;
2491
2492 gimple_call_set_fndecl (call, rfn->decl);
2493 return call;
2494 }
2495
2496 /* Fold the call to a PTRUE, taking the element size from type suffix 0. */
2497 gimple *
2498 gimple_folder::fold_to_ptrue ()
2499 {
2500 tree svbool_type = TREE_TYPE (lhs);
2501 tree bool_type = TREE_TYPE (svbool_type);
2502 unsigned int element_bytes = type_suffix (0).element_bytes;
2503
2504 /* The return type is svbool_t for all type suffixes, thus for b8 we
2505 want { 1, 1, 1, 1, ... }, for b16 we want { 1, 0, 1, 0, ... }, etc. */
2506 tree_vector_builder builder (svbool_type, element_bytes, 1);
2507 builder.quick_push (build_all_ones_cst (bool_type));
2508 for (unsigned int i = 1; i < element_bytes; ++i)
2509 builder.quick_push (build_zero_cst (bool_type));
2510 return gimple_build_assign (lhs, builder.build ());
2511 }
2512
2513 /* Fold the call to a PFALSE. */
2514 gimple *
2515 gimple_folder::fold_to_pfalse ()
2516 {
2517 return gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
2518 }
2519
2520 /* Fold an operation to a constant predicate in which the first VL
2521 elements are set and the rest are clear. Take the element size
2522 from type suffix 0. */
2523 gimple *
2524 gimple_folder::fold_to_vl_pred (unsigned int vl)
2525 {
2526 tree vectype = TREE_TYPE (lhs);
2527 tree element_type = TREE_TYPE (vectype);
2528 tree minus_one = build_all_ones_cst (element_type);
2529 tree zero = build_zero_cst (element_type);
2530 unsigned int element_bytes = type_suffix (0).element_bytes;
2531
2532 /* Construct COUNT elements that contain the ptrue followed by
2533 a repeating sequence of COUNT elements. */
2534 unsigned int count = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vectype));
2535 gcc_assert (vl * element_bytes <= count);
2536 tree_vector_builder builder (vectype, count, 2);
2537 for (unsigned int i = 0; i < count * 2; ++i)
2538 {
2539 bool bit = (i & (element_bytes - 1)) == 0 && i < vl * element_bytes;
2540 builder.quick_push (bit ? minus_one : zero);
2541 }
2542 return gimple_build_assign (lhs, builder.build ());
2543 }
2544
2545 /* Try to fold the call. Return the new statement on success and null
2546 on failure. */
2547 gimple *
2548 gimple_folder::fold ()
2549 {
2550 /* Don't fold anything when SVE is disabled; emit an error during
2551 expansion instead. */
2552 if (!TARGET_SVE)
2553 return NULL;
2554
2555 /* Punt if the function has a return type and no result location is
2556 provided. The attributes should allow target-independent code to
2557 remove the calls if appropriate. */
2558 if (!lhs && TREE_TYPE (gimple_call_fntype (call)) != void_type_node)
2559 return NULL;
2560
2561 return base->fold (*this);
2562 }
2563
2564 function_expander::function_expander (const function_instance &instance,
2565 tree fndecl, tree call_expr_in,
2566 rtx possible_target_in)
2567 : function_call_info (EXPR_LOCATION (call_expr_in), instance, fndecl),
2568 call_expr (call_expr_in), possible_target (possible_target_in)
2569 {
2570 }
2571
2572 /* Return the handler of direct optab OP for type suffix SUFFIX_I. */
2573 insn_code
2574 function_expander::direct_optab_handler (optab op, unsigned int suffix_i)
2575 {
2576 return ::direct_optab_handler (op, vector_mode (suffix_i));
2577 }
2578
2579 /* Choose between signed and unsigned direct optabs SIGNED_OP and
2580 UNSIGNED_OP based on the signedness of type suffix SUFFIX_I, then
2581 pick the appropriate optab handler for the mode. Use MODE as the
2582 mode if given, otherwise use the mode of type suffix SUFFIX_I. */
2583 insn_code
2584 function_expander::direct_optab_handler_for_sign (optab signed_op,
2585 optab unsigned_op,
2586 unsigned int suffix_i,
2587 machine_mode mode)
2588 {
2589 if (mode == VOIDmode)
2590 mode = vector_mode (suffix_i);
2591 optab op = type_suffix (suffix_i).unsigned_p ? unsigned_op : signed_op;
2592 return ::direct_optab_handler (op, mode);
2593 }
2594
2595 /* Return true if X overlaps any input. */
2596 bool
2597 function_expander::overlaps_input_p (rtx x)
2598 {
2599 for (unsigned int i = 0; i < args.length (); ++i)
2600 if (reg_overlap_mentioned_p (x, args[i]))
2601 return true;
2602 return false;
2603 }
2604
2605 /* Return the base address for a contiguous load or store function.
2606 MEM_MODE is the mode of the addressed memory. */
2607 rtx
2608 function_expander::get_contiguous_base (machine_mode mem_mode)
2609 {
2610 rtx base = args[1];
2611 if (mode_suffix_id == MODE_vnum)
2612 {
2613 /* Use the size of the memory mode for extending loads and truncating
2614 stores. Use the size of a full vector for non-extending loads
2615 and non-truncating stores (including svld[234] and svst[234]). */
2616 poly_int64 size = ordered_min (GET_MODE_SIZE (mem_mode),
2617 BYTES_PER_SVE_VECTOR);
2618 rtx offset = gen_int_mode (size, Pmode);
2619 offset = simplify_gen_binary (MULT, Pmode, args[2], offset);
2620 base = simplify_gen_binary (PLUS, Pmode, base, offset);
2621 }
2622 return base;
2623 }
2624
2625 /* For a function that does the equivalent of:
2626
2627 OUTPUT = COND ? FN (INPUTS) : FALLBACK;
2628
2629 return the value of FALLBACK.
2630
2631 MODE is the mode of OUTPUT. NOPS is the number of operands in INPUTS.
2632 MERGE_ARGNO is the argument that provides FALLBACK for _m functions,
2633 or DEFAULT_MERGE_ARGNO if we should apply the usual rules.
2634
2635 ARGNO is the caller's index into args. If the returned value is
2636 argument 0 (as for unary _m operations), increment ARGNO past the
2637 returned argument. */
2638 rtx
2639 function_expander::get_fallback_value (machine_mode mode, unsigned int nops,
2640 unsigned int merge_argno,
2641 unsigned int &argno)
2642 {
2643 if (pred == PRED_z)
2644 return CONST0_RTX (mode);
2645
2646 gcc_assert (pred == PRED_m || pred == PRED_x);
2647 if (merge_argno == DEFAULT_MERGE_ARGNO)
2648 merge_argno = nops == 1 && pred == PRED_m ? 0 : 1;
2649
2650 if (merge_argno == 0)
2651 return args[argno++];
2652
2653 return args[merge_argno];
2654 }
2655
2656 /* Return a REG rtx that can be used for the result of the function,
2657 using the preferred target if suitable. */
2658 rtx
2659 function_expander::get_reg_target ()
2660 {
2661 machine_mode target_mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (fndecl)));
2662 if (!possible_target || GET_MODE (possible_target) != target_mode)
2663 possible_target = gen_reg_rtx (target_mode);
2664 return possible_target;
2665 }
2666
2667 /* As for get_reg_target, but make sure that the returned REG does not
2668 overlap any inputs. */
2669 rtx
2670 function_expander::get_nonoverlapping_reg_target ()
2671 {
2672 if (possible_target && overlaps_input_p (possible_target))
2673 possible_target = NULL_RTX;
2674 return get_reg_target ();
2675 }
2676
2677 /* Add an output operand to the instruction we're building, which has
2678 code ICODE. Bind the output to the preferred target rtx if possible. */
2679 void
2680 function_expander::add_output_operand (insn_code icode)
2681 {
2682 unsigned int opno = m_ops.length ();
2683 machine_mode mode = insn_data[icode].operand[opno].mode;
2684 m_ops.safe_grow (opno + 1);
2685 create_output_operand (&m_ops.last (), possible_target, mode);
2686 }
2687
2688 /* Add an input operand to the instruction we're building, which has
2689 code ICODE. Calculate the value of the operand as follows:
2690
2691 - If the operand is a vector and X is not, broadcast X to fill a
2692 vector of the appropriate mode.
2693
2694 - Otherwise, if the operand is a predicate, coerce X to have the
2695 mode that the instruction expects. In this case X is known to be
2696 VNx16BImode (the mode of svbool_t).
2697
2698 - Otherwise use X directly. The expand machinery checks that X has
2699 the right mode for the instruction. */
2700 void
2701 function_expander::add_input_operand (insn_code icode, rtx x)
2702 {
2703 unsigned int opno = m_ops.length ();
2704 const insn_operand_data &operand = insn_data[icode].operand[opno];
2705 machine_mode mode = operand.mode;
2706 if (mode == VOIDmode)
2707 {
2708 /* The only allowable use of VOIDmode is the wildcard
2709 aarch64_any_register_operand, which is used to avoid
2710 combinatorial explosion in the reinterpret patterns. */
2711 gcc_assert (operand.predicate == aarch64_any_register_operand);
2712 mode = GET_MODE (x);
2713 }
2714 else if (!VECTOR_MODE_P (GET_MODE (x)) && VECTOR_MODE_P (mode))
2715 x = expand_vector_broadcast (mode, x);
2716 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
2717 {
2718 gcc_assert (GET_MODE (x) == VNx16BImode);
2719 x = gen_lowpart (mode, x);
2720 }
2721 m_ops.safe_grow (m_ops.length () + 1);
2722 create_input_operand (&m_ops.last (), x, mode);
2723 }
2724
2725 /* Add an integer operand with value X to the instruction. */
2726 void
2727 function_expander::add_integer_operand (HOST_WIDE_INT x)
2728 {
2729 m_ops.safe_grow (m_ops.length () + 1);
2730 create_integer_operand (&m_ops.last (), x);
2731 }
2732
2733 /* Add a memory operand with mode MODE and address ADDR. */
2734 void
2735 function_expander::add_mem_operand (machine_mode mode, rtx addr)
2736 {
2737 gcc_assert (VECTOR_MODE_P (mode));
2738 rtx mem = gen_rtx_MEM (mode, memory_address (mode, addr));
2739 /* The memory is only guaranteed to be element-aligned. */
2740 set_mem_align (mem, GET_MODE_ALIGNMENT (GET_MODE_INNER (mode)));
2741 add_fixed_operand (mem);
2742 }
2743
2744 /* Add an address operand with value X. The static operand data says
2745 what mode and form the address must have. */
2746 void
2747 function_expander::add_address_operand (rtx x)
2748 {
2749 m_ops.safe_grow (m_ops.length () + 1);
2750 create_address_operand (&m_ops.last (), x);
2751 }
2752
2753 /* Add an operand that must be X. The only way of legitimizing an
2754 invalid X is to reload the address of a MEM. */
2755 void
2756 function_expander::add_fixed_operand (rtx x)
2757 {
2758 m_ops.safe_grow (m_ops.length () + 1);
2759 create_fixed_operand (&m_ops.last (), x);
2760 }
2761
2762 /* Generate instruction ICODE, given that its operands have already
2763 been added to M_OPS. Return the value of the first operand. */
2764 rtx
2765 function_expander::generate_insn (insn_code icode)
2766 {
2767 expand_insn (icode, m_ops.length (), m_ops.address ());
2768 return function_returns_void_p () ? const0_rtx : m_ops[0].value;
2769 }
2770
2771 /* Convert the arguments to a gather/scatter function into the
2772 associated md operands. Argument ARGNO is the scalar or vector base and
2773 argument ARGNO + 1 is the scalar or vector displacement (if applicable).
2774 The md pattern expects:
2775
2776 - a scalar base
2777 - a vector displacement
2778
2779 If SCALED_P is true, it also expects:
2780
2781 - a const_int that is 1 if the displacement is zero-extended from 32 bits
2782 - a scaling multiplier (1 for bytes, 2 for .h indices, etc.).
2783
2784 If SCALED_P is false, the displacement is implicitly zero-extended
2785 and the scaling multiplier is implicitly 1. */
2786 void
2787 function_expander::prepare_gather_address_operands (unsigned int argno,
2788 bool scaled_p)
2789 {
2790 machine_mode mem_mode = memory_vector_mode ();
2791 tree vector_type = base_vector_type ();
2792 units_index units = displacement_units ();
2793 int shift_idx = -1;
2794 if (units == UNITS_none)
2795 {
2796 /* Vector base, no displacement. Convert to an integer zero base
2797 and a vector byte offset. */
2798 args.quick_insert (argno, const0_rtx);
2799 units = UNITS_bytes;
2800 }
2801 else if (vector_type)
2802 {
2803 /* Vector base, scalar displacement. Convert to a scalar base and
2804 a vector byte offset. */
2805 std::swap (args[argno], args[argno + 1]);
2806 if (units == UNITS_elements)
2807 shift_idx = argno;
2808 }
2809 else
2810 {
2811 /* Scalar base, vector displacement. This is the order that the md
2812 pattern wants. */
2813 if (Pmode == SImode)
2814 args[argno] = simplify_gen_unary (ZERO_EXTEND, DImode,
2815 args[argno], SImode);
2816 vector_type = displacement_vector_type ();
2817 if (units == UNITS_elements && !scaled_p)
2818 shift_idx = argno + 1;
2819 }
2820 tree scalar_displacement_type = TREE_TYPE (vector_type);
2821
2822 if (shift_idx >= 0)
2823 {
2824 machine_mode arg_mode = GET_MODE (args[shift_idx]);
2825 if (arg_mode == VOIDmode)
2826 arg_mode = DImode;
2827 unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mem_mode);
2828 rtx shift = gen_int_mode (exact_log2 (elt_bytes), DImode);
2829 args[shift_idx] = simplify_gen_binary (ASHIFT, arg_mode,
2830 args[shift_idx], shift);
2831 units = UNITS_bytes;
2832 }
2833
2834 bool uxtw_p = (TYPE_PRECISION (scalar_displacement_type) == 64
2835 || TYPE_UNSIGNED (scalar_displacement_type));
2836 unsigned int scale = (units == UNITS_bytes
2837 ? 1 : GET_MODE_UNIT_SIZE (mem_mode));
2838
2839 if (scaled_p)
2840 {
2841 args.quick_insert (argno + 2, GEN_INT (uxtw_p));
2842 args.quick_insert (argno + 3, GEN_INT (scale));
2843 }
2844 else
2845 gcc_assert (uxtw_p && scale == 1);
2846 }
2847
2848 /* The final argument is an immediate svprfop value. Add two fake arguments
2849 to represent the rw and locality operands of a PREFETCH rtx. */
2850 void
2851 function_expander::prepare_prefetch_operands ()
2852 {
2853 unsigned int prfop = INTVAL (args.last ());
2854 /* Bit 3 of the prfop selects stores over loads. */
2855 args.quick_push (GEN_INT ((prfop & 8) != 0));
2856 /* Bits 1 and 2 specify the locality; 0-based for svprfop but
2857 1-based for PREFETCH. */
2858 args.quick_push (GEN_INT (((prfop >> 1) & 3) + 1));
2859 }
2860
2861 /* Add a dummy argument to indicate whether predicate argument ARGNO
2862 is all-true when interpreted in mode PRED_MODE. The hint goes
2863 immediately after ARGNO. */
2864 void
2865 function_expander::add_ptrue_hint (unsigned int argno, machine_mode pred_mode)
2866 {
2867 rtx pred = gen_lowpart (pred_mode, args[argno]);
2868 int hint = (pred == CONSTM1_RTX (pred_mode)
2869 ? SVE_KNOWN_PTRUE : SVE_MAYBE_NOT_PTRUE);
2870 args.quick_insert (argno + 1, gen_int_mode (hint, SImode));
2871 }
2872
2873 /* Rotate inputs args[START:END] one position to the left, so that
2874 args[START] becomes args[END - 1]. */
2875 void
2876 function_expander::rotate_inputs_left (unsigned int start, unsigned int end)
2877 {
2878 rtx new_last = args[start];
2879 for (unsigned int i = start; i < end - 1; ++i)
2880 args[i] = args[i + 1];
2881 args[end - 1] = new_last;
2882 }
2883
2884 /* Return true if the negation of argument ARGNO can be folded away,
2885 replacing it with the negated value if so. MODE is the associated
2886 vector mode, but the argument could be a single element. The main
2887 case this handles is constant arguments. */
2888 bool
2889 function_expander::try_negating_argument (unsigned int argno,
2890 machine_mode mode)
2891 {
2892 rtx x = args[argno];
2893 if (!VECTOR_MODE_P (GET_MODE (x)))
2894 mode = GET_MODE_INNER (mode);
2895
2896 x = simplify_unary_operation (NEG, mode, x, mode);
2897 if (!x)
2898 return false;
2899
2900 args[argno] = x;
2901 return true;
2902 }
2903
2904 /* Implement the call using instruction ICODE, with a 1:1 mapping between
2905 arguments and input operands. */
2906 rtx
2907 function_expander::use_exact_insn (insn_code icode)
2908 {
2909 unsigned int nops = insn_data[icode].n_operands;
2910 if (!function_returns_void_p ())
2911 {
2912 add_output_operand (icode);
2913 nops -= 1;
2914 }
2915 for (unsigned int i = 0; i < nops; ++i)
2916 add_input_operand (icode, args[i]);
2917 return generate_insn (icode);
2918 }
2919
2920 /* Implement the call using instruction ICODE, which does not use a
2921 governing predicate. We must therefore drop the GP from an _x call. */
2922 rtx
2923 function_expander::use_unpred_insn (insn_code icode)
2924 {
2925 /* We can't drop the predicate for _z and _m. */
2926 gcc_assert (pred == PRED_x || pred == PRED_none);
2927 /* Discount the output operand. */
2928 unsigned int nops = insn_data[icode].n_operands - 1;
2929 /* Drop the predicate argument in the case of _x predication. */
2930 unsigned int bias = (pred == PRED_x ? 1 : 0);
2931 unsigned int i = 0;
2932
2933 add_output_operand (icode);
2934 for (; i < nops; ++i)
2935 add_input_operand (icode, args[i + bias]);
2936
2937 return generate_insn (icode);
2938 }
2939
2940 /* Implement the call using instruction ICODE, which is a predicated
2941 operation that returns arbitrary values for inactive lanes. */
2942 rtx
2943 function_expander::use_pred_x_insn (insn_code icode)
2944 {
2945 /* At present we never need to handle PRED_none, which would involve
2946 creating a new predicate rather than using one supplied by the user. */
2947 gcc_assert (pred == PRED_x);
2948 /* Discount the output operand. */
2949 unsigned int nops = args.length () - 1;
2950
2951 bool has_float_operand_p = FLOAT_MODE_P (insn_data[icode].operand[0].mode);
2952
2953 /* Add the normal operands. */
2954 add_output_operand (icode);
2955 add_input_operand (icode, args[0]);
2956 for (unsigned int i = 0; i < nops; ++i)
2957 {
2958 add_input_operand (icode, args[i + 1]);
2959 if (FLOAT_MODE_P (GET_MODE (args[i + 1])))
2960 has_float_operand_p = true;
2961 }
2962
2963 if (has_float_operand_p)
2964 {
2965 /* Add a flag that indicates whether unpredicated instructions
2966 are allowed. */
2967 rtx pred = m_ops[1].value;
2968 if (flag_trapping_math && pred != CONST1_RTX (GET_MODE (pred)))
2969 add_integer_operand (SVE_STRICT_GP);
2970 else
2971 add_integer_operand (SVE_RELAXED_GP);
2972 }
2973
2974 return generate_insn (icode);
2975 }
2976
2977 /* Implement the call using instruction ICODE, which does the equivalent of:
2978
2979 OUTPUT = COND ? FN (INPUTS) : FALLBACK;
2980
2981 The instruction operands are in the order above: OUTPUT, COND, INPUTS
2982 and FALLBACK. MERGE_ARGNO is the argument that provides FALLBACK for _m
2983 functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
2984 rtx
2985 function_expander::use_cond_insn (insn_code icode, unsigned int merge_argno)
2986 {
2987 /* At present we never need to handle PRED_none, which would involve
2988 creating a new predicate rather than using one supplied by the user. */
2989 gcc_assert (pred != PRED_none);
2990 /* Discount the output, predicate and fallback value. */
2991 unsigned int nops = insn_data[icode].n_operands - 3;
2992 machine_mode mode = insn_data[icode].operand[0].mode;
2993
2994 unsigned int opno = 0;
2995 rtx fallback_arg = get_fallback_value (mode, nops, merge_argno, opno);
2996 rtx pred = args[opno++];
2997
2998 add_output_operand (icode);
2999 add_input_operand (icode, pred);
3000 for (unsigned int i = 0; i < nops; ++i)
3001 add_input_operand (icode, args[opno + i]);
3002 add_input_operand (icode, fallback_arg);
3003 return generate_insn (icode);
3004 }
3005
3006 /* Implement the call using instruction ICODE, which is a select-like
3007 operation with the following operands:
3008
3009 0: output
3010 1: true value
3011 2: false value
3012 3: predicate
3013
3014 MERGE_ARGNO is the argument that provides the "false" value for _m
3015 functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
3016 rtx
3017 function_expander::use_vcond_mask_insn (insn_code icode,
3018 unsigned int merge_argno)
3019 {
3020 machine_mode mode = vector_mode (0);
3021
3022 unsigned int opno = 0;
3023 rtx false_arg = get_fallback_value (mode, 1, merge_argno, opno);
3024 rtx pred_arg = args[opno++];
3025 rtx true_arg = args[opno++];
3026
3027 add_output_operand (icode);
3028 add_input_operand (icode, true_arg);
3029 add_input_operand (icode, false_arg);
3030 add_input_operand (icode, pred_arg);
3031 return generate_insn (icode);
3032 }
3033
3034 /* Implement the call using instruction ICODE, which loads memory operand 1
3035 into register operand 0 under the control of predicate operand 2.
3036 Extending loads have a further predicate (operand 3) that nominally
3037 controls the extension. */
3038 rtx
3039 function_expander::use_contiguous_load_insn (insn_code icode)
3040 {
3041 machine_mode mem_mode = memory_vector_mode ();
3042
3043 add_output_operand (icode);
3044 add_mem_operand (mem_mode, get_contiguous_base (mem_mode));
3045 add_input_operand (icode, args[0]);
3046 if (GET_MODE_UNIT_BITSIZE (mem_mode) < type_suffix (0).element_bits)
3047 add_input_operand (icode, CONSTM1_RTX (VNx16BImode));
3048 return generate_insn (icode);
3049 }
3050
3051 /* Implement the call using instruction ICODE, which prefetches from
3052 address operand 1 under the control of predicate operand 0.
3053 Operands 2, 3 and 4 respectively specify the svprfop value,
3054 the PREFETCH rw flag and the PREFETCH locality. */
3055 rtx
3056 function_expander::use_contiguous_prefetch_insn (insn_code icode)
3057 {
3058 add_input_operand (icode, args[0]);
3059 add_address_operand (get_contiguous_base (VNx16QImode));
3060 for (unsigned int i = args.length () - 3; i < args.length (); ++i)
3061 add_input_operand (icode, args[i]);
3062 return generate_insn (icode);
3063 }
3064
3065 /* Implement the call using instruction ICODE, which stores register operand 1
3066 into memory operand 0 under the control of predicate operand 2. */
3067 rtx
3068 function_expander::use_contiguous_store_insn (insn_code icode)
3069 {
3070 machine_mode mem_mode = memory_vector_mode ();
3071
3072 add_mem_operand (mem_mode, get_contiguous_base (mem_mode));
3073 add_input_operand (icode, args.last ());
3074 add_input_operand (icode, args[0]);
3075 return generate_insn (icode);
3076 }
3077
3078 /* Implement the call using one of the following strategies, chosen in order:
3079
3080 (1) "aarch64_pred_<optab><mode>_z" for PRED_z predicate functions
3081
3082 (2) "aarch64_pred_<optab><mode>" for PRED_x functions
3083
3084 (3) a normal unpredicated optab for PRED_none and PRED_x functions,
3085 dropping the predicate in the latter case
3086
3087 (4) an unpredicated "aarch64_sve_<code_optab><mode>" for PRED_none and
3088 PRED_x functions, again dropping the predicate for PRED_x
3089
3090 (5) "cond_<optab><mode>" otherwise
3091
3092 where <optab> corresponds to:
3093
3094 - CODE_FOR_SINT for signed integers
3095 - CODE_FOR_UINT for unsigned integers
3096 - UNSPEC_FOR_FP for floating-point values
3097
3098 and where <code_optab> is like <optab>, but uses CODE_FOR_SINT instead
3099 of UNSPEC_FOR_FP for floating-point values.
3100
3101 MERGE_ARGNO is the argument that provides the values of inactive lanes for
3102 _m functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
3103 rtx
3104 function_expander::map_to_rtx_codes (rtx_code code_for_sint,
3105 rtx_code code_for_uint,
3106 int unspec_for_fp,
3107 unsigned int merge_argno)
3108 {
3109 machine_mode mode = vector_mode (0);
3110 rtx_code code = (type_suffix (0).unsigned_p ? code_for_uint : code_for_sint);
3111 insn_code icode;
3112
3113 /* Handle predicate logic operations, which always use _z predication. */
3114 if (type_suffix (0).tclass == TYPE_bool)
3115 {
3116 gcc_assert (pred == PRED_z && code_for_uint == code_for_sint);
3117 return use_exact_insn (code_for_aarch64_pred_z (code, mode));
3118 }
3119
3120 /* First try using UNSPEC_PRED_X patterns for _x predication,
3121 if available. */
3122 if (pred == PRED_x)
3123 {
3124 if (type_suffix (0).integer_p)
3125 icode = maybe_code_for_aarch64_pred (code, mode);
3126 else
3127 icode = maybe_code_for_aarch64_pred (unspec_for_fp, mode);
3128 if (icode != CODE_FOR_nothing)
3129 return use_pred_x_insn (icode);
3130 }
3131
3132 /* Otherwise expand PRED_none and PRED_x operations without a predicate.
3133 Floating-point operations conventionally use the signed rtx code. */
3134 if (pred == PRED_none || pred == PRED_x)
3135 {
3136 icode = direct_optab_handler (code_to_optab (code), 0);
3137 if (icode == CODE_FOR_nothing)
3138 icode = code_for_aarch64_sve (code, mode);
3139 return use_unpred_insn (icode);
3140 }
3141
3142 /* Don't use cond_*_optabs here, since not all codes have one yet. */
3143 if (type_suffix (0).integer_p)
3144 icode = code_for_cond (code, mode);
3145 else
3146 icode = code_for_cond (unspec_for_fp, mode);
3147 return use_cond_insn (icode, merge_argno);
3148 }
3149
3150 /* Implement the call using one of the following strategies, chosen in order:
3151
3152 (1) "aarch64_pred_<optab><mode>" for PRED_x functions; this is a
3153 predicated pattern
3154
3155 (2) "aarch64_sve_<optab><mode>" for PRED_none and PRED_x functions;
3156 this is an unpredicated pattern
3157
3158 (3) "cond_<optab><mode>" otherwise
3159
3160 where <optab> corresponds to:
3161
3162 - UNSPEC_FOR_SINT for signed integers
3163 - UNSPEC_FOR_UINT for unsigned integers
3164 - UNSPEC_FOR_FP for floating-point values
3165
3166 MERGE_ARGNO is the argument that provides the values of inactive lanes for
3167 _m functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
3168 rtx
3169 function_expander::map_to_unspecs (int unspec_for_sint, int unspec_for_uint,
3170 int unspec_for_fp, unsigned int merge_argno)
3171 {
3172 machine_mode mode = vector_mode (0);
3173 int unspec = (!type_suffix (0).integer_p ? unspec_for_fp
3174 : type_suffix (0).unsigned_p ? unspec_for_uint
3175 : unspec_for_sint);
3176
3177 if (pred == PRED_x)
3178 {
3179 insn_code icode = maybe_code_for_aarch64_pred (unspec, mode);
3180 if (icode != CODE_FOR_nothing)
3181 return use_pred_x_insn (icode);
3182 }
3183
3184 if (pred == PRED_none || pred == PRED_x)
3185 {
3186 insn_code icode = maybe_code_for_aarch64_sve (unspec, mode);
3187 if (icode != CODE_FOR_nothing)
3188 return use_unpred_insn (icode);
3189 }
3190
3191 insn_code icode = code_for_cond (unspec, vector_mode (0));
3192 return use_cond_insn (icode, merge_argno);
3193 }
3194
3195 /* Expand the call and return its lhs. */
3196 rtx
3197 function_expander::expand ()
3198 {
3199 unsigned int nargs = call_expr_nargs (call_expr);
3200 args.reserve (nargs);
3201 for (unsigned int i = 0; i < nargs; ++i)
3202 args.quick_push (expand_normal (CALL_EXPR_ARG (call_expr, i)));
3203
3204 return base->expand (*this);
3205 }
3206
3207 /* Register the built-in SVE ABI types, such as __SVBool_t. */
3208 static void
3209 register_builtin_types ()
3210 {
3211 #define DEF_SVE_TYPE(ACLE_NAME, NCHARS, ABI_NAME, SCALAR_TYPE) \
3212 scalar_types[VECTOR_TYPE_ ## ACLE_NAME] = SCALAR_TYPE;
3213 #include "aarch64-sve-builtins.def"
3214
3215 for (unsigned int i = 0; i < NUM_VECTOR_TYPES; ++i)
3216 {
3217 tree eltype = scalar_types[i];
3218 tree vectype;
3219 unsigned int num_zr = 0, num_pr = 0;
3220 if (eltype == boolean_type_node)
3221 {
3222 vectype = build_truth_vector_type_for_mode (BYTES_PER_SVE_VECTOR,
3223 VNx16BImode);
3224 gcc_assert (TYPE_MODE (vectype) == VNx16BImode
3225 && TYPE_MODE (vectype) == TYPE_MODE_RAW (vectype)
3226 && TYPE_ALIGN (vectype) == 16
3227 && known_eq (wi::to_poly_offset (TYPE_SIZE (vectype)),
3228 BYTES_PER_SVE_VECTOR));
3229 num_pr = 1;
3230 }
3231 else
3232 {
3233 unsigned int elbytes = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
3234 poly_uint64 nunits = exact_div (BYTES_PER_SVE_VECTOR, elbytes);
3235 vectype = build_vector_type (eltype, nunits);
3236 gcc_assert (VECTOR_MODE_P (TYPE_MODE (vectype))
3237 && TYPE_MODE (vectype) == TYPE_MODE_RAW (vectype)
3238 && TYPE_ALIGN (vectype) == 128
3239 && known_eq (wi::to_poly_offset (TYPE_SIZE (vectype)),
3240 BITS_PER_SVE_VECTOR));
3241 num_zr = 1;
3242 }
3243 vectype = build_distinct_type_copy (vectype);
3244 gcc_assert (vectype == TYPE_MAIN_VARIANT (vectype));
3245 SET_TYPE_STRUCTURAL_EQUALITY (vectype);
3246 TYPE_ARTIFICIAL (vectype) = 1;
3247 TYPE_INDIVISIBLE_P (vectype) = 1;
3248 add_sve_type_attribute (vectype, num_zr, num_pr,
3249 vector_types[i].mangled_name);
3250 abi_vector_types[i] = vectype;
3251 lang_hooks.types.register_builtin_type (vectype,
3252 vector_types[i].abi_name);
3253 }
3254 }
3255
3256 /* Initialize all compiler built-ins related to SVE that should be
3257 defined at start-up. */
3258 void
3259 init_builtins ()
3260 {
3261 sve_switcher sve;
3262 register_builtin_types ();
3263 if (in_lto_p)
3264 handle_arm_sve_h ();
3265 }
3266
3267 /* Register vector type TYPE under its arm_sve.h name. */
3268 static void
3269 register_vector_type (vector_type_index type)
3270 {
3271 tree vectype = abi_vector_types[type];
3272 tree id = get_identifier (vector_types[type].acle_name);
3273 tree decl = build_decl (input_location, TYPE_DECL, id, vectype);
3274 decl = lang_hooks.decls.pushdecl (decl);
3275
3276 /* Record the new ACLE type if pushdecl succeeded without error. Use
3277 the ABI type otherwise, so that the type we record at least has the
3278 right form, even if it doesn't have the right name. This should give
3279 better error recovery behavior than installing error_mark_node or
3280 installing an incorrect type. */
3281 if (decl
3282 && TREE_CODE (decl) == TYPE_DECL
3283 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == vectype)
3284 vectype = TREE_TYPE (decl);
3285 acle_vector_types[0][type] = vectype;
3286 }
3287
3288 /* Register the tuple type that contains NUM_VECTORS vectors of type TYPE. */
3289 static void
3290 register_tuple_type (unsigned int num_vectors, vector_type_index type)
3291 {
3292 tree tuple_type = lang_hooks.types.make_type (RECORD_TYPE);
3293
3294 /* The contents of the type are opaque, so we can define them in any
3295 way that maps to the correct ABI type.
3296
3297 Here we choose to use the same layout as for arm_neon.h, but with
3298 "__val" instead of "val":
3299
3300 struct svfooxN_t { svfoo_t __val[N]; };
3301
3302 (It wouldn't be possible to write that directly in C or C++ for
3303 sizeless types, but that's not a problem for this function.)
3304
3305 Using arrays simplifies the handling of svget and svset for variable
3306 arguments. */
3307 tree vector_type = acle_vector_types[0][type];
3308 tree array_type = build_array_type_nelts (vector_type, num_vectors);
3309 gcc_assert (VECTOR_MODE_P (TYPE_MODE (array_type))
3310 && TYPE_MODE_RAW (array_type) == TYPE_MODE (array_type)
3311 && TYPE_ALIGN (array_type) == 128);
3312
3313 tree field = build_decl (input_location, FIELD_DECL,
3314 get_identifier ("__val"), array_type);
3315 DECL_FIELD_CONTEXT (field) = tuple_type;
3316 TYPE_FIELDS (tuple_type) = field;
3317 add_sve_type_attribute (tuple_type, num_vectors, 0, NULL);
3318 layout_type (tuple_type);
3319 gcc_assert (VECTOR_MODE_P (TYPE_MODE (tuple_type))
3320 && TYPE_MODE_RAW (tuple_type) == TYPE_MODE (tuple_type)
3321 && TYPE_ALIGN (tuple_type) == 128);
3322
3323 /* Work out the structure name. */
3324 char buffer[sizeof ("svfloat64x4_t")];
3325 const char *vector_type_name = vector_types[type].acle_name;
3326 snprintf (buffer, sizeof (buffer), "%.*sx%d_t",
3327 (int) strlen (vector_type_name) - 2, vector_type_name,
3328 num_vectors);
3329
3330 tree decl = build_decl (input_location, TYPE_DECL,
3331 get_identifier (buffer), tuple_type);
3332 TYPE_NAME (tuple_type) = decl;
3333 TYPE_STUB_DECL (tuple_type) = decl;
3334 lang_hooks.decls.pushdecl (decl);
3335 /* ??? Undo the effect of set_underlying_type for C. The C frontend
3336 doesn't recognize DECL as a built-in because (as intended) the decl has
3337 a real location instead of BUILTINS_LOCATION. The frontend therefore
3338 treats the decl like a normal C "typedef struct foo foo;", expecting
3339 the type for tag "struct foo" to have a dummy unnamed TYPE_DECL instead
3340 of the named one we attached above. It then sets DECL_ORIGINAL_TYPE
3341 on the supposedly unnamed decl, creating a circularity that upsets
3342 dwarf2out.
3343
3344 We don't want to follow the normal C model and create "struct foo"
3345 tags for tuple types since (a) the types are supposed to be opaque
3346 and (b) they couldn't be defined as a real struct anyway. Treating
3347 the TYPE_DECLs as "typedef struct foo foo;" without creating
3348 "struct foo" would lead to confusing error messages. */
3349 DECL_ORIGINAL_TYPE (decl) = NULL_TREE;
3350
3351 acle_vector_types[num_vectors - 1][type] = tuple_type;
3352 }
3353
3354 /* Register the svpattern enum. */
3355 static void
3356 register_svpattern ()
3357 {
3358 auto_vec<string_int_pair, 32> values;
3359 #define PUSH(UPPER, LOWER, VALUE) \
3360 values.quick_push (string_int_pair ("SV_" #UPPER, VALUE));
3361 AARCH64_FOR_SVPATTERN (PUSH)
3362 #undef PUSH
3363
3364 acle_svpattern = lang_hooks.types.simulate_enum_decl (input_location,
3365 "svpattern", values);
3366 }
3367
3368 /* Register the svprfop enum. */
3369 static void
3370 register_svprfop ()
3371 {
3372 auto_vec<string_int_pair, 16> values;
3373 #define PUSH(UPPER, LOWER, VALUE) \
3374 values.quick_push (string_int_pair ("SV_" #UPPER, VALUE));
3375 AARCH64_FOR_SVPRFOP (PUSH)
3376 #undef PUSH
3377
3378 acle_svprfop = lang_hooks.types.simulate_enum_decl (input_location,
3379 "svprfop", values);
3380 }
3381
3382 /* Implement #pragma GCC aarch64 "arm_sve.h". */
3383 void
3384 handle_arm_sve_h ()
3385 {
3386 if (function_table)
3387 {
3388 error ("duplicate definition of %qs", "arm_sve.h");
3389 return;
3390 }
3391
3392 sve_switcher sve;
3393
3394 /* Define the vector and tuple types. */
3395 for (unsigned int type_i = 0; type_i < NUM_VECTOR_TYPES; ++type_i)
3396 {
3397 vector_type_index type = vector_type_index (type_i);
3398 register_vector_type (type);
3399 if (type != VECTOR_TYPE_svbool_t)
3400 for (unsigned int count = 2; count <= MAX_TUPLE_SIZE; ++count)
3401 register_tuple_type (count, type);
3402 }
3403
3404 /* Define the enums. */
3405 register_svpattern ();
3406 register_svprfop ();
3407
3408 /* Define the functions. */
3409 function_table = new hash_table<registered_function_hasher> (1023);
3410 function_builder builder;
3411 for (unsigned int i = 0; i < ARRAY_SIZE (function_groups); ++i)
3412 builder.register_function_group (function_groups[i]);
3413 }
3414
3415 /* Return the function decl with SVE function subcode CODE, or error_mark_node
3416 if no such function exists. */
3417 tree
3418 builtin_decl (unsigned int code, bool)
3419 {
3420 if (code >= vec_safe_length (registered_functions))
3421 return error_mark_node;
3422 return (*registered_functions)[code]->decl;
3423 }
3424
3425 /* If we're implementing manual overloading, check whether the SVE
3426 function with subcode CODE is overloaded, and if so attempt to
3427 determine the corresponding non-overloaded function. The call
3428 occurs at location LOCATION and has the arguments given by ARGLIST.
3429
3430 If the call is erroneous, report an appropriate error and return
3431 error_mark_node. Otherwise, if the function is overloaded, return
3432 the decl of the non-overloaded function. Return NULL_TREE otherwise,
3433 indicating that the call should be processed in the normal way. */
3434 tree
3435 resolve_overloaded_builtin (location_t location, unsigned int code,
3436 vec<tree, va_gc> *arglist)
3437 {
3438 if (code >= vec_safe_length (registered_functions))
3439 return NULL_TREE;
3440
3441 registered_function &rfn = *(*registered_functions)[code];
3442 if (rfn.overloaded_p)
3443 return function_resolver (location, rfn.instance, rfn.decl,
3444 *arglist).resolve ();
3445 return NULL_TREE;
3446 }
3447
3448 /* Perform any semantic checks needed for a call to the SVE function
3449 with subcode CODE, such as testing for integer constant expressions.
3450 The call occurs at location LOCATION and has NARGS arguments,
3451 given by ARGS. FNDECL is the original function decl, before
3452 overload resolution.
3453
3454 Return true if the call is valid, otherwise report a suitable error. */
3455 bool
3456 check_builtin_call (location_t location, vec<location_t>, unsigned int code,
3457 tree fndecl, unsigned int nargs, tree *args)
3458 {
3459 const registered_function &rfn = *(*registered_functions)[code];
3460 if (!check_required_extensions (location, rfn.decl, rfn.required_extensions))
3461 return false;
3462 return function_checker (location, rfn.instance, fndecl,
3463 TREE_TYPE (rfn.decl), nargs, args).check ();
3464 }
3465
3466 /* Attempt to fold STMT, given that it's a call to the SVE function
3467 with subcode CODE. Return the new statement on success and null
3468 on failure. Insert any other new statements at GSI. */
3469 gimple *
3470 gimple_fold_builtin (unsigned int code, gimple_stmt_iterator *gsi, gcall *stmt)
3471 {
3472 registered_function &rfn = *(*registered_functions)[code];
3473 return gimple_folder (rfn.instance, rfn.decl, gsi, stmt).fold ();
3474 }
3475
3476 /* Expand a call to the SVE function with subcode CODE. EXP is the call
3477 expression and TARGET is the preferred location for the result.
3478 Return the value of the lhs. */
3479 rtx
3480 expand_builtin (unsigned int code, tree exp, rtx target)
3481 {
3482 registered_function &rfn = *(*registered_functions)[code];
3483 if (!check_required_extensions (EXPR_LOCATION (exp), rfn.decl,
3484 rfn.required_extensions))
3485 return target;
3486 return function_expander (rfn.instance, rfn.decl, exp, target).expand ();
3487 }
3488
3489 /* If TYPE is a built-in type defined by the SVE ABI, return the mangled name,
3490 otherwise return NULL. */
3491 const char *
3492 mangle_builtin_type (const_tree type)
3493 {
3494 /* ??? The C++ frontend normally strips qualifiers and attributes before
3495 calling this hook, adding separate mangling for attributes that affect
3496 type identity. Fortunately the type copy will have the same TYPE_NAME
3497 as the original, so we can get the attributes from there. */
3498 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
3499 type = TREE_TYPE (TYPE_NAME (type));
3500 if (tree attr = lookup_sve_type_attribute (type))
3501 if (tree id = TREE_VALUE (chain_index (2, TREE_VALUE (attr))))
3502 return IDENTIFIER_POINTER (id);
3503 return NULL;
3504 }
3505
3506 /* Return true if TYPE is a built-in SVE type defined by the ABI or ACLE. */
3507 bool
3508 builtin_type_p (const_tree type)
3509 {
3510 return lookup_sve_type_attribute (type);
3511 }
3512
3513 /* Return true if TYPE is a built-in SVE type defined by the ABI or ACLE.
3514 If so, store the number of constituent SVE vectors in *NUM_ZR and the
3515 number of constituent SVE predicates in *NUM_PR. */
3516 bool
3517 builtin_type_p (const_tree type, unsigned int *num_zr, unsigned int *num_pr)
3518 {
3519 if (tree attr = lookup_sve_type_attribute (type))
3520 {
3521 tree num_zr_node = TREE_VALUE (attr);
3522 tree num_pr_node = TREE_CHAIN (num_zr_node);
3523 *num_zr = tree_to_uhwi (TREE_VALUE (num_zr_node));
3524 *num_pr = tree_to_uhwi (TREE_VALUE (num_pr_node));
3525 return true;
3526 }
3527 return false;
3528 }
3529
3530 /* Implement TARGET_VERIFY_TYPE_CONTEXT for SVE types. */
3531 bool
3532 verify_type_context (location_t loc, type_context_kind context,
3533 const_tree type, bool silent_p)
3534 {
3535 if (!builtin_type_p (type))
3536 return true;
3537
3538 switch (context)
3539 {
3540 case TCTX_SIZEOF:
3541 case TCTX_STATIC_STORAGE:
3542 if (!silent_p)
3543 error_at (loc, "SVE type %qT does not have a fixed size", type);
3544 return false;
3545
3546 case TCTX_ALIGNOF:
3547 if (!silent_p)
3548 error_at (loc, "SVE type %qT does not have a defined alignment", type);
3549 return false;
3550
3551 case TCTX_THREAD_STORAGE:
3552 if (!silent_p)
3553 error_at (loc, "variables of type %qT cannot have thread-local"
3554 " storage duration", type);
3555 return false;
3556
3557 case TCTX_POINTER_ARITH:
3558 if (!silent_p)
3559 error_at (loc, "arithmetic on pointer to SVE type %qT", type);
3560 return false;
3561
3562 case TCTX_FIELD:
3563 if (silent_p)
3564 ;
3565 else if (lang_GNU_CXX ())
3566 error_at (loc, "member variables cannot have SVE type %qT", type);
3567 else
3568 error_at (loc, "fields cannot have SVE type %qT", type);
3569 return false;
3570
3571 case TCTX_ARRAY_ELEMENT:
3572 if (!silent_p)
3573 error_at (loc, "array elements cannot have SVE type %qT", type);
3574 return false;
3575
3576 case TCTX_ALLOCATION:
3577 if (!silent_p)
3578 error_at (loc, "cannot allocate objects with SVE type %qT", type);
3579 return false;
3580
3581 case TCTX_DEALLOCATION:
3582 if (!silent_p)
3583 error_at (loc, "cannot delete objects with SVE type %qT", type);
3584 return false;
3585
3586 case TCTX_EXCEPTIONS:
3587 if (!silent_p)
3588 error_at (loc, "cannot throw or catch SVE type %qT", type);
3589 return false;
3590
3591 case TCTX_CAPTURE_BY_COPY:
3592 if (!silent_p)
3593 error_at (loc, "capture by copy of SVE type %qT", type);
3594 return false;
3595 }
3596 gcc_unreachable ();
3597 }
3598
3599 }
3600
3601 using namespace aarch64_sve;
3602
3603 inline void
3604 gt_ggc_mx (function_instance *)
3605 {
3606 }
3607
3608 inline void
3609 gt_pch_nx (function_instance *)
3610 {
3611 }
3612
3613 inline void
3614 gt_pch_nx (function_instance *, void (*) (void *, void *), void *)
3615 {
3616 }
3617
3618 #include "gt-aarch64-sve-builtins.h"