Debian Codegraph @

gcc/config/aarch64/aarch64-sve-builtins.cc

raw
1 /* ACLE support for AArch64 SVE
2 Copyright (C) 2018-2025 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 "alias.h"
46 #include "gimple-fold.h"
47 #include "langhooks.h"
48 #include "stringpool.h"
49 #include "attribs.h"
50 #include "aarch64-sve-builtins.h"
51 #include "aarch64-sve-builtins-base.h"
52 #include "aarch64-sve-builtins-sve2.h"
53 #include "aarch64-sve-builtins-sme.h"
54 #include "aarch64-sve-builtins-shapes.h"
55 #include "aarch64-builtins.h"
56
57 using namespace aarch64;
58
59 namespace aarch64_sve {
60
61 /* Static information about each single-predicate or single-vector
62 ABI and ACLE type. */
63 struct vector_type_info
64 {
65 /* The name of the type as declared by arm_sve.h. */
66 const char *acle_name;
67
68 /* The name of the type specified in AAPCS64. The type is always
69 available under this name, even when arm_sve.h isn't included. */
70 const char *abi_name;
71
72 /* The C++ mangling of ABI_NAME. */
73 const char *mangled_name;
74 };
75
76 /* Describes a function decl. */
77 class GTY(()) registered_function
78 {
79 public:
80 /* The ACLE function that the decl represents. */
81 function_instance instance GTY ((skip));
82
83 /* The decl itself. */
84 tree decl;
85
86 /* The architecture extensions that the function requires. */
87 aarch64_required_extensions required_extensions;
88
89 /* True if the decl represents an overloaded function that needs to be
90 resolved by function_resolver. */
91 bool overloaded_p;
92 };
93
94 /* Hash traits for registered_function. */
95 struct registered_function_hasher : ggc_ptr_hash <registered_function>
96 {
97 typedef function_instance compare_type;
98
99 static hashval_t hash (value_type);
100 static bool equal (value_type, const compare_type &);
101 };
102
103 /* Information about each single-predicate or single-vector type. */
104 static CONSTEXPR const vector_type_info vector_types[] = {
105 #define DEF_SVE_TYPE(ACLE_NAME, NCHARS, ABI_NAME, SCALAR_TYPE) \
106 { #ACLE_NAME, #ABI_NAME, "u" #NCHARS #ABI_NAME },
107 #include "aarch64-sve-builtins.def"
108 };
109
110 /* The function name suffix associated with each predication type. */
111 static const char *const pred_suffixes[NUM_PREDS + 1] = {
112 "",
113 "",
114 "_m",
115 "_x",
116 "_z",
117 "_m",
118 ""
119 };
120
121 /* Static information about each mode_suffix_index. */
122 CONSTEXPR const mode_suffix_info mode_suffixes[] = {
123 #define VECTOR_TYPE_none NUM_VECTOR_TYPES
124 #define DEF_SVE_MODE(NAME, BASE, DISPLACEMENT, UNITS) \
125 { "_" #NAME, VECTOR_TYPE_##BASE, VECTOR_TYPE_##DISPLACEMENT, UNITS_##UNITS },
126 #include "aarch64-sve-builtins.def"
127 #undef VECTOR_TYPE_none
128 { "", NUM_VECTOR_TYPES, NUM_VECTOR_TYPES, UNITS_none }
129 };
130
131 /* Static information about each type_suffix_index. */
132 CONSTEXPR const type_suffix_info type_suffixes[NUM_TYPE_SUFFIXES + 1] = {
133 #define DEF_SVE_NEON_TYPE_SUFFIX(NAME, ACLE_TYPE, CLASS, BITS, MODE, \
134 NEON64, NEON128) \
135 { "_" #NAME, \
136 VECTOR_TYPE_##ACLE_TYPE, \
137 TYPE_##CLASS, \
138 BITS, \
139 BITS / BITS_PER_UNIT, \
140 TYPE_##CLASS == TYPE_signed || TYPE_##CLASS == TYPE_unsigned, \
141 TYPE_##CLASS == TYPE_unsigned, \
142 TYPE_##CLASS == TYPE_float || TYPE_##CLASS == TYPE_bfloat, \
143 TYPE_##CLASS != TYPE_bool, \
144 TYPE_##CLASS == TYPE_bool, \
145 false, \
146 0, \
147 MODE, \
148 NEON64, \
149 NEON128 },
150 #define DEF_SVE_TYPE_SUFFIX(NAME, ACLE_TYPE, CLASS, BITS, MODE) \
151 DEF_SVE_NEON_TYPE_SUFFIX (NAME, ACLE_TYPE, CLASS, BITS, MODE, \
152 ARM_NEON_H_TYPES_LAST, ARM_NEON_H_TYPES_LAST)
153 #define DEF_SME_ZA_SUFFIX(NAME, BITS, MODE) \
154 { "_" #NAME, \
155 NUM_VECTOR_TYPES, \
156 NUM_TYPE_CLASSES, \
157 BITS, \
158 BITS / BITS_PER_UNIT, \
159 false, \
160 false, \
161 false, \
162 false, \
163 false, \
164 true, \
165 0, \
166 MODE, \
167 ARM_NEON_H_TYPES_LAST, \
168 ARM_NEON_H_TYPES_LAST },
169 #include "aarch64-sve-builtins.def"
170 { "", NUM_VECTOR_TYPES, TYPE_bool, 0, 0, false, false, false, false,
171 false, false, 0, VOIDmode, ARM_NEON_H_TYPES_LAST, ARM_NEON_H_TYPES_LAST }
172 };
173
174 CONSTEXPR const group_suffix_info group_suffixes[] = {
175 #define DEF_SVE_GROUP_SUFFIX(NAME, VG, VECTORS_PER_TUPLE) \
176 { "_" #NAME, VG, VECTORS_PER_TUPLE },
177 #include "aarch64-sve-builtins.def"
178 { "", 0, 1 }
179 };
180
181 /* Define a TYPES_<combination> macro for each combination of type
182 suffixes that an ACLE function can have, where <combination> is the
183 name used in DEF_SVE_FUNCTION entries.
184
185 Use S (T) for single type suffix T and D (T1, T2) for a pair of type
186 suffixes T1 and T2. Use commas to separate the suffixes.
187
188 Although the order shouldn't matter, the convention is to sort the
189 suffixes lexicographically after dividing suffixes into a type
190 class ("b", "f", etc.) and a numerical bit count. */
191
192 /* _b8 _b16 _b32 _b64. */
193 #define TYPES_all_pred(S, D) \
194 S (b8), S (b16), S (b32), S (b64)
195
196 /* _c8 _c16 _c32 _c64. */
197 #define TYPES_all_count(S, D) \
198 S (c8), S (c16), S (c32), S (c64)
199
200 /* _b8 _b16 _b32 _b64
201 _c8 _c16 _c32 _c64. */
202 #define TYPES_all_pred_count(S, D) \
203 TYPES_all_pred (S, D), \
204 TYPES_all_count (S, D)
205
206 /* _f16 _f32 _f64. */
207 #define TYPES_all_float(S, D) \
208 S (f16), S (f32), S (f64)
209
210 /* _s8 _s16 _s32 _s64. */
211 #define TYPES_all_signed(S, D) \
212 S (s8), S (s16), S (s32), S (s64)
213
214 /* _f16 _f32 _f64
215 _s8 _s16 _s32 _s64. */
216 #define TYPES_all_float_and_signed(S, D) \
217 TYPES_all_float (S, D), TYPES_all_signed (S, D)
218
219 /* _u8 _u16 _u32 _u64. */
220 #define TYPES_all_unsigned(S, D) \
221 S (u8), S (u16), S (u32), S (u64)
222
223 /* _s8 _s16 _s32 _s64
224 _u8 _u16 _u32 _u64. */
225 #define TYPES_all_integer(S, D) \
226 TYPES_all_signed (S, D), TYPES_all_unsigned (S, D)
227
228 /* _f16 _f32 _f64
229 _s8 _s16 _s32 _s64
230 _u8 _u16 _u32 _u64. */
231 #define TYPES_all_arith(S, D) \
232 TYPES_all_float (S, D), TYPES_all_integer (S, D)
233
234 #define TYPES_all_data(S, D) \
235 TYPES_b_data (S, D), \
236 TYPES_h_data (S, D), \
237 TYPES_s_data (S, D), \
238 TYPES_d_data (S, D)
239
240 /* _b only. */
241 #define TYPES_b(S, D) \
242 S (b)
243
244 /* _c only. */
245 #define TYPES_c(S, D) \
246 S (c)
247
248 /* _u8. */
249 #define TYPES_b_unsigned(S, D) \
250 S (u8)
251
252 /* _s8
253 _u8. */
254 #define TYPES_b_integer(S, D) \
255 S (s8), TYPES_b_unsigned (S, D)
256
257 /* _mf8
258 _s8
259 _u8. */
260 #define TYPES_b_data(S, D) \
261 S (mf8), TYPES_b_integer (S, D)
262
263 /* _s8 _s16
264 _u8 _u16. */
265 #define TYPES_bh_integer(S, D) \
266 S (s8), S (s16), S (u8), S (u16)
267
268 /* _u8 _u32. */
269 #define TYPES_bs_unsigned(S, D) \
270 S (u8), S (u32)
271
272 /* _s8 _s16 _s32. */
273 #define TYPES_bhs_signed(S, D) \
274 S (s8), S (s16), S (s32)
275
276 /* _u8 _u16 _u32. */
277 #define TYPES_bhs_unsigned(S, D) \
278 S (u8), S (u16), S (u32)
279
280 /* _s8 _s16 _s32
281 _u8 _u16 _u32. */
282 #define TYPES_bhs_integer(S, D) \
283 TYPES_bhs_signed (S, D), TYPES_bhs_unsigned (S, D)
284
285 #define TYPES_bh_data(S, D) \
286 TYPES_b_data (S, D), \
287 TYPES_h_data (S, D)
288
289 #define TYPES_bhs_data(S, D) \
290 TYPES_b_data (S, D), \
291 TYPES_h_data (S, D), \
292 TYPES_s_data (S, D)
293
294 /* _s16_s8 _s32_s16 _s64_s32
295 _u16_u8 _u32_u16 _u64_u32. */
296 #define TYPES_bhs_widen(S, D) \
297 D (s16, s8), D (s32, s16), D (s64, s32), \
298 D (u16, u8), D (u32, u16), D (u64, u32)
299
300 /* _bf16. */
301 #define TYPES_h_bfloat(S, D) \
302 S (bf16)
303
304 /* _f16. */
305 #define TYPES_h_float(S, D) \
306 S (f16)
307
308 /* _s16
309 _u16. */
310 #define TYPES_h_integer(S, D) \
311 S (s16), S (u16)
312
313 /* _bf16
314 _f16
315 _s16
316 _u16. */
317 #define TYPES_h_data(S, D) \
318 S (bf16), S (f16), TYPES_h_integer (S, D)
319
320 /* _s16 _s32. */
321 #define TYPES_hs_signed(S, D) \
322 S (s16), S (s32)
323
324 /* _s16 _s32
325 _u16 _u32. */
326 #define TYPES_hs_integer(S, D) \
327 TYPES_hs_signed (S, D), S (u16), S (u32)
328
329 /* _f16 _f32. */
330 #define TYPES_hs_float(S, D) \
331 S (f16), S (f32)
332
333 #define TYPES_hs_data(S, D) \
334 TYPES_h_data (S, D), \
335 TYPES_s_data (S, D)
336
337 /* _u16 _u64. */
338 #define TYPES_hd_unsigned(S, D) \
339 S (u16), S (u64)
340
341 /* _s16 _s32 _s64. */
342 #define TYPES_hsd_signed(S, D) \
343 S (s16), S (s32), S (s64)
344
345 /* _s16 _s32 _s64
346 _u16 _u32 _u64. */
347 #define TYPES_hsd_integer(S, D) \
348 TYPES_hsd_signed (S, D), S (u16), S (u32), S (u64)
349
350 #define TYPES_hsd_data(S, D) \
351 TYPES_h_data (S, D), \
352 TYPES_s_data (S, D), \
353 TYPES_d_data (S, D)
354
355 /* _f16_mf8. */
356 #define TYPES_h_float_mf8(S, D) \
357 D (f16, mf8)
358
359 /* _f32. */
360 #define TYPES_s_float(S, D) \
361 S (f32)
362
363 /* _f32_mf8. */
364 #define TYPES_s_float_mf8(S, D) \
365 D (f32, mf8)
366
367 /* _f32
368 _s16 _s32 _s64
369 _u16 _u32 _u64. */
370 #define TYPES_s_float_hsd_integer(S, D) \
371 TYPES_s_float (S, D), TYPES_hsd_integer (S, D)
372
373 /* _f32
374 _s32 _s64
375 _u32 _u64. */
376 #define TYPES_s_float_sd_integer(S, D) \
377 TYPES_s_float (S, D), TYPES_sd_integer (S, D)
378
379 /* _s32. */
380 #define TYPES_s_signed(S, D) \
381 S (s32)
382
383 /* _u32. */
384 #define TYPES_s_unsigned(S, D) \
385 S (u32)
386
387 /* _s32
388 _u32. */
389 #define TYPES_s_integer(S, D) \
390 TYPES_s_signed (S, D), TYPES_s_unsigned (S, D)
391
392 /* _f32
393 _s32
394 _u32. */
395 #define TYPES_s_data(S, D) \
396 TYPES_s_float (S, D), TYPES_s_integer (S, D)
397
398 /* _s32 _s64. */
399 #define TYPES_sd_signed(S, D) \
400 S (s32), S (s64)
401
402 /* _u32 _u64. */
403 #define TYPES_sd_unsigned(S, D) \
404 S (u32), S (u64)
405
406 /* _s32 _s64
407 _u32 _u64. */
408 #define TYPES_sd_integer(S, D) \
409 TYPES_sd_signed (S, D), TYPES_sd_unsigned (S, D)
410
411 #define TYPES_sd_data(S, D) \
412 TYPES_s_data (S, D), \
413 TYPES_d_data (S, D)
414
415 /* _f16 _f32 _f64
416 _s32 _s64
417 _u32 _u64. */
418 #define TYPES_all_float_and_sd_integer(S, D) \
419 TYPES_all_float (S, D), TYPES_sd_integer (S, D)
420
421 /* _f64. */
422 #define TYPES_d_float(S, D) \
423 S (f64)
424
425 /* _u64. */
426 #define TYPES_d_unsigned(S, D) \
427 S (u64)
428
429 /* _s64
430 _u64. */
431 #define TYPES_d_integer(S, D) \
432 S (s64), TYPES_d_unsigned (S, D)
433
434 /* _f64
435 _s64
436 _u64. */
437 #define TYPES_d_data(S, D) \
438 TYPES_d_float (S, D), TYPES_d_integer (S, D)
439
440 /* All the type combinations allowed by svcvt. */
441 #define TYPES_cvt(S, D) \
442 D (f16, f32), D (f16, f64), \
443 D (f16, s16), D (f16, s32), D (f16, s64), \
444 D (f16, u16), D (f16, u32), D (f16, u64), \
445 \
446 D (f32, f16), D (f32, f64), \
447 D (f32, s32), D (f32, s64), \
448 D (f32, u32), D (f32, u64), \
449 \
450 D (f64, f16), D (f64, f32), \
451 D (f64, s32), D (f64, s64), \
452 D (f64, u32), D (f64, u64), \
453 \
454 D (s16, f16), \
455 D (s32, f16), D (s32, f32), D (s32, f64), \
456 D (s64, f16), D (s64, f32), D (s64, f64), \
457 \
458 D (u16, f16), \
459 D (u32, f16), D (u32, f32), D (u32, f64), \
460 D (u64, f16), D (u64, f32), D (u64, f64)
461
462 /* _bf16_f32. */
463 #define TYPES_cvt_bfloat(S, D) \
464 D (bf16, f32)
465
466 /* { _bf16 _f16 } x _f32. */
467 #define TYPES_cvt_h_s_float(S, D) \
468 D (bf16, f32), D (f16, f32)
469
470 /* _f32_f16. */
471 #define TYPES_cvt_f32_f16(S, D) \
472 D (f32, f16)
473
474 /* _f32_f16
475 _f64_f32. */
476 #define TYPES_cvt_long(S, D) \
477 D (f32, f16), D (f64, f32)
478
479 /* _f16_f32. */
480 #define TYPES_cvt_narrow_s(S, D) \
481 D (f32, f64)
482
483 /* _f16_f32
484 _f32_f64. */
485 #define TYPES_cvt_narrow(S, D) \
486 D (f16, f32), TYPES_cvt_narrow_s (S, D)
487
488 /* { _s32 _u32 } x _f32
489
490 _f32 x { _s32 _u32 }. */
491 #define TYPES_cvt_s_s(S, D) \
492 D (s32, f32), \
493 D (u32, f32), \
494 D (f32, s32), \
495 D (f32, u32)
496
497 /* _f16_mf8
498 _bf16_mf8. */
499 #define TYPES_cvt_mf8(S, D) \
500 D (f16, mf8), D (bf16, mf8)
501
502 /* _mf8_f16
503 _mf8_bf16. */
504 #define TYPES_cvtn_mf8(S, D) \
505 D (mf8, f16), D (mf8, bf16)
506
507 /* _mf8_f32. */
508 #define TYPES_cvtnx_mf8(S, D) \
509 D (mf8, f32)
510
511 /* { _s32 _s64 } x { _b8 _b16 _b32 _b64 }
512 { _u32 _u64 }. */
513 #define TYPES_inc_dec_n1(D, A) \
514 D (A, b8), D (A, b16), D (A, b32), D (A, b64)
515 #define TYPES_inc_dec_n(S, D) \
516 TYPES_inc_dec_n1 (D, s32), \
517 TYPES_inc_dec_n1 (D, s64), \
518 TYPES_inc_dec_n1 (D, u32), \
519 TYPES_inc_dec_n1 (D, u64)
520
521 /* { _s16 _u16 } x _s32
522
523 { _u16 } x _u32. */
524 #define TYPES_qcvt_x2(S, D) \
525 D (s16, s32), \
526 D (u16, u32), \
527 D (u16, s32)
528
529 /* { _s8 _u8 } x _s32
530
531 { _u8 } x _u32
532
533 { _s16 _u16 } x _s64
534
535 { _u16 } x _u64. */
536 #define TYPES_qcvt_x4(S, D) \
537 D (s8, s32), \
538 D (u8, u32), \
539 D (u8, s32), \
540 D (s16, s64), \
541 D (u16, u64), \
542 D (u16, s64)
543
544 /* _s16_s32
545 _u16_u32. */
546 #define TYPES_qrshr_x2(S, D) \
547 D (s16, s32), \
548 D (u16, u32)
549
550 /* _u16_s32. */
551 #define TYPES_qrshru_x2(S, D) \
552 D (u16, s32)
553
554 /* _s8_s32
555 _s16_s64
556 _u8_u32
557 _u16_u64. */
558 #define TYPES_qrshr_x4(S, D) \
559 D (s8, s32), \
560 D (s16, s64), \
561 D (u8, u32), \
562 D (u16, u64)
563
564 /* _u8_s32
565 _u16_s64. */
566 #define TYPES_qrshru_x4(S, D) \
567 D (u8, s32), \
568 D (u16, s64)
569
570 /* { _mf8 _bf16 } { _mf8 _bf16 }
571 { _f16 _f32 _f64 } { _f16 _f32 _f64 }
572 { _s8 _s16 _s32 _s64 } x { _s8 _s16 _s32 _s64 }
573 { _u8 _u16 _u32 _u64 } { _u8 _u16 _u32 _u64 }. */
574 #define TYPES_reinterpret1(D, A) \
575 D (A, mf8), \
576 D (A, bf16), \
577 D (A, f16), D (A, f32), D (A, f64), \
578 D (A, s8), D (A, s16), D (A, s32), D (A, s64), \
579 D (A, u8), D (A, u16), D (A, u32), D (A, u64)
580 #define TYPES_reinterpret(S, D) \
581 TYPES_reinterpret1 (D, mf8), \
582 TYPES_reinterpret1 (D, bf16), \
583 TYPES_reinterpret1 (D, f16), \
584 TYPES_reinterpret1 (D, f32), \
585 TYPES_reinterpret1 (D, f64), \
586 TYPES_reinterpret1 (D, s8), \
587 TYPES_reinterpret1 (D, s16), \
588 TYPES_reinterpret1 (D, s32), \
589 TYPES_reinterpret1 (D, s64), \
590 TYPES_reinterpret1 (D, u8), \
591 TYPES_reinterpret1 (D, u16), \
592 TYPES_reinterpret1 (D, u32), \
593 TYPES_reinterpret1 (D, u64)
594
595 /* _b_c
596 _c_b. */
597 #define TYPES_reinterpret_b(S, D) \
598 D (b, c), \
599 D (c, b)
600
601 /* { _b8 _b16 _b32 _b64 } x { _s32 _s64 }
602 { _u32 _u64 } */
603 #define TYPES_while1(D, bn) \
604 D (bn, s32), D (bn, s64), D (bn, u32), D (bn, u64)
605 #define TYPES_while(S, D) \
606 TYPES_while1 (D, b8), \
607 TYPES_while1 (D, b16), \
608 TYPES_while1 (D, b32), \
609 TYPES_while1 (D, b64)
610
611 /* { _b8 _b16 _b32 _b64 } x { _s64 }
612 { _u64 } */
613 #define TYPES_while_x(S, D) \
614 D (b8, s64), D (b8, u64), \
615 D (b16, s64), D (b16, u64), \
616 D (b32, s64), D (b32, u64), \
617 D (b64, s64), D (b64, u64)
618
619 /* { _c8 _c16 _c32 _c64 } x { _s64 }
620 { _u64 } */
621 #define TYPES_while_x_c(S, D) \
622 D (c8, s64), D (c8, u64), \
623 D (c16, s64), D (c16, u64), \
624 D (c32, s64), D (c32, u64), \
625 D (c64, s64), D (c64, u64)
626
627 /* _f32_f16
628 _s32_s16
629 _u32_u16. */
630 #define TYPES_s_narrow_fsu(S, D) \
631 D (f32, f16), D (s32, s16), D (u32, u16)
632
633 /* _za8 _za16 _za32 _za64 _za128. */
634 #define TYPES_all_za(S, D) \
635 S (za8), S (za16), S (za32), S (za64), S (za128)
636
637 /* _za64. */
638 #define TYPES_d_za(S, D) \
639 S (za64)
640
641 /* { _za8 } x { _s8 _u8 }
642
643 { _za16 } x { _bf16 _f16 _s16 _u16 }
644
645 { _za32 } x { _f32 _s32 _u32 }
646
647 { _za64 } x { _f64 _s64 _u64 }. */
648 #define TYPES_za_bhsd_data(S, D) \
649 D (za8, s8), D (za8, u8), \
650 D (za16, bf16), D (za16, f16), D (za16, s16), D (za16, u16), \
651 D (za32, f32), D (za32, s32), D (za32, u32), \
652 D (za64, f64), D (za64, s64), D (za64, u64)
653
654 /* Likewise, plus:
655
656 { _za128 } x { _bf16 }
657 { _f16 _f32 _f64 }
658 { _s8 _s16 _s32 _s64 }
659 { _u8 _u16 _u32 _u64 }. */
660
661 #define TYPES_za_all_data(S, D) \
662 TYPES_za_bhsd_data (S, D), \
663 TYPES_reinterpret1 (D, za128)
664
665 /* _za16_bf16. */
666 #define TYPES_za_h_bfloat(S, D) \
667 D (za16, bf16)
668
669 /* _za16_f16. */
670 #define TYPES_za_h_float(S, D) \
671 D (za16, f16)
672
673 /* _za32_s8. */
674 #define TYPES_za_s_b_signed(S, D) \
675 D (za32, s8)
676
677 /* _za32_u8. */
678 #define TYPES_za_s_b_unsigned(S, D) \
679 D (za32, u8)
680
681 /* _za32 x { _s8 _u8 }. */
682 #define TYPES_za_s_b_integer(S, D) \
683 D (za32, s8), D (za32, u8)
684
685 /* _za32 x { _s16 _u16 }. */
686 #define TYPES_za_s_h_integer(S, D) \
687 D (za32, s16), D (za32, u16)
688
689 /* _za32 x { _bf16 _f16 _s16 _u16 }. */
690 #define TYPES_za_s_h_data(S, D) \
691 D (za32, bf16), D (za32, f16), D (za32, s16), D (za32, u16)
692
693 /* _za32_u32. */
694 #define TYPES_za_s_unsigned(S, D) \
695 D (za32, u32)
696
697 /* _za32 x { _s32 _u32 }. */
698 #define TYPES_za_s_integer(S, D) \
699 D (za32, s32), D (za32, u32)
700
701 /* _za32_f32. */
702 #define TYPES_za_s_float(S, D) \
703 D (za32, f32)
704
705 /* _za32 x { _f32 _s32 _u32 }. */
706 #define TYPES_za_s_data(S, D) \
707 D (za32, f32), D (za32, s32), D (za32, u32)
708
709 /* _za64 x { _s16 _u16 }. */
710 #define TYPES_za_d_h_integer(S, D) \
711 D (za64, s16), D (za64, u16)
712
713 /* _za64_f64. */
714 #define TYPES_za_d_float(S, D) \
715 D (za64, f64)
716
717 /* _za64 x { _s64 _u64 }. */
718 #define TYPES_za_d_integer(S, D) \
719 D (za64, s64), D (za64, u64)
720
721 /* _za32 x { _s8 _u8 _bf16 _f16 _f32 }. */
722 #define TYPES_mop_base(S, D) \
723 D (za32, s8), D (za32, u8), D (za32, bf16), D (za32, f16), D (za32, f32)
724
725 /* _za32_s8. */
726 #define TYPES_mop_base_signed(S, D) \
727 D (za32, s8)
728
729 /* _za32_u8. */
730 #define TYPES_mop_base_unsigned(S, D) \
731 D (za32, u8)
732
733 /* _za64 x { _s16 _u16 }. */
734 #define TYPES_mop_i16i64(S, D) \
735 D (za64, s16), D (za64, u16)
736
737 /* _za64_s16. */
738 #define TYPES_mop_i16i64_signed(S, D) \
739 D (za64, s16)
740
741 /* _za64_u16. */
742 #define TYPES_mop_i16i64_unsigned(S, D) \
743 D (za64, u16)
744
745 /* _za. */
746 #define TYPES_za(S, D) \
747 S (za)
748
749 /* Describe a pair of type suffixes in which only the first is used. */
750 #define DEF_VECTOR_TYPE(X) { TYPE_SUFFIX_ ## X, NUM_TYPE_SUFFIXES }
751
752 /* Describe a pair of type suffixes in which both are used. */
753 #define DEF_DOUBLE_TYPE(X, Y) { TYPE_SUFFIX_ ## X, TYPE_SUFFIX_ ## Y }
754
755 /* Create an array that can be used in aarch64-sve-builtins.def to
756 select the type suffixes in TYPES_<NAME>. */
757 #define DEF_SVE_TYPES_ARRAY(NAME) \
758 static const type_suffix_pair types_##NAME[] = { \
759 TYPES_##NAME (DEF_VECTOR_TYPE, DEF_DOUBLE_TYPE), \
760 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES } \
761 }
762
763 /* For functions that don't take any type suffixes. */
764 static const type_suffix_pair types_none[] = {
765 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES },
766 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES }
767 };
768
769 /* Create an array for each TYPES_<combination> macro above. */
770 DEF_SVE_TYPES_ARRAY (all_pred);
771 DEF_SVE_TYPES_ARRAY (all_count);
772 DEF_SVE_TYPES_ARRAY (all_pred_count);
773 DEF_SVE_TYPES_ARRAY (all_float);
774 DEF_SVE_TYPES_ARRAY (all_signed);
775 DEF_SVE_TYPES_ARRAY (all_float_and_signed);
776 DEF_SVE_TYPES_ARRAY (all_unsigned);
777 DEF_SVE_TYPES_ARRAY (all_integer);
778 DEF_SVE_TYPES_ARRAY (all_arith);
779 DEF_SVE_TYPES_ARRAY (all_data);
780 DEF_SVE_TYPES_ARRAY (b);
781 DEF_SVE_TYPES_ARRAY (b_unsigned);
782 DEF_SVE_TYPES_ARRAY (b_integer);
783 DEF_SVE_TYPES_ARRAY (bh_integer);
784 DEF_SVE_TYPES_ARRAY (bs_unsigned);
785 DEF_SVE_TYPES_ARRAY (bhs_signed);
786 DEF_SVE_TYPES_ARRAY (bhs_unsigned);
787 DEF_SVE_TYPES_ARRAY (bhs_integer);
788 DEF_SVE_TYPES_ARRAY (bh_data);
789 DEF_SVE_TYPES_ARRAY (bhs_data);
790 DEF_SVE_TYPES_ARRAY (bhs_widen);
791 DEF_SVE_TYPES_ARRAY (c);
792 DEF_SVE_TYPES_ARRAY (h_bfloat);
793 DEF_SVE_TYPES_ARRAY (h_float);
794 DEF_SVE_TYPES_ARRAY (h_float_mf8);
795 DEF_SVE_TYPES_ARRAY (h_integer);
796 DEF_SVE_TYPES_ARRAY (h_data);
797 DEF_SVE_TYPES_ARRAY (hs_signed);
798 DEF_SVE_TYPES_ARRAY (hs_integer);
799 DEF_SVE_TYPES_ARRAY (hs_float);
800 DEF_SVE_TYPES_ARRAY (hs_data);
801 DEF_SVE_TYPES_ARRAY (hd_unsigned);
802 DEF_SVE_TYPES_ARRAY (hsd_signed);
803 DEF_SVE_TYPES_ARRAY (hsd_integer);
804 DEF_SVE_TYPES_ARRAY (hsd_data);
805 DEF_SVE_TYPES_ARRAY (s_float);
806 DEF_SVE_TYPES_ARRAY (s_float_hsd_integer);
807 DEF_SVE_TYPES_ARRAY (s_float_mf8);
808 DEF_SVE_TYPES_ARRAY (s_float_sd_integer);
809 DEF_SVE_TYPES_ARRAY (s_signed);
810 DEF_SVE_TYPES_ARRAY (s_unsigned);
811 DEF_SVE_TYPES_ARRAY (s_integer);
812 DEF_SVE_TYPES_ARRAY (s_data);
813 DEF_SVE_TYPES_ARRAY (sd_signed);
814 DEF_SVE_TYPES_ARRAY (sd_unsigned);
815 DEF_SVE_TYPES_ARRAY (sd_integer);
816 DEF_SVE_TYPES_ARRAY (sd_data);
817 DEF_SVE_TYPES_ARRAY (all_float_and_sd_integer);
818 DEF_SVE_TYPES_ARRAY (d_float);
819 DEF_SVE_TYPES_ARRAY (d_unsigned);
820 DEF_SVE_TYPES_ARRAY (d_integer);
821 DEF_SVE_TYPES_ARRAY (d_data);
822 DEF_SVE_TYPES_ARRAY (cvt);
823 DEF_SVE_TYPES_ARRAY (cvt_bfloat);
824 DEF_SVE_TYPES_ARRAY (cvt_h_s_float);
825 DEF_SVE_TYPES_ARRAY (cvt_f32_f16);
826 DEF_SVE_TYPES_ARRAY (cvt_long);
827 DEF_SVE_TYPES_ARRAY (cvt_mf8);
828 DEF_SVE_TYPES_ARRAY (cvt_narrow_s);
829 DEF_SVE_TYPES_ARRAY (cvt_narrow);
830 DEF_SVE_TYPES_ARRAY (cvt_s_s);
831 DEF_SVE_TYPES_ARRAY (cvtn_mf8);
832 DEF_SVE_TYPES_ARRAY (cvtnx_mf8);
833 DEF_SVE_TYPES_ARRAY (inc_dec_n);
834 DEF_SVE_TYPES_ARRAY (qcvt_x2);
835 DEF_SVE_TYPES_ARRAY (qcvt_x4);
836 DEF_SVE_TYPES_ARRAY (qrshr_x2);
837 DEF_SVE_TYPES_ARRAY (qrshr_x4);
838 DEF_SVE_TYPES_ARRAY (qrshru_x2);
839 DEF_SVE_TYPES_ARRAY (qrshru_x4);
840 DEF_SVE_TYPES_ARRAY (reinterpret);
841 DEF_SVE_TYPES_ARRAY (reinterpret_b);
842 DEF_SVE_TYPES_ARRAY (while);
843 DEF_SVE_TYPES_ARRAY (while_x);
844 DEF_SVE_TYPES_ARRAY (while_x_c);
845 DEF_SVE_TYPES_ARRAY (s_narrow_fsu);
846 DEF_SVE_TYPES_ARRAY (all_za);
847 DEF_SVE_TYPES_ARRAY (d_za);
848 DEF_SVE_TYPES_ARRAY (za_bhsd_data);
849 DEF_SVE_TYPES_ARRAY (za_all_data);
850 DEF_SVE_TYPES_ARRAY (za_h_bfloat);
851 DEF_SVE_TYPES_ARRAY (za_h_float);
852 DEF_SVE_TYPES_ARRAY (za_s_b_signed);
853 DEF_SVE_TYPES_ARRAY (za_s_b_unsigned);
854 DEF_SVE_TYPES_ARRAY (za_s_b_integer);
855 DEF_SVE_TYPES_ARRAY (za_s_h_integer);
856 DEF_SVE_TYPES_ARRAY (za_s_h_data);
857 DEF_SVE_TYPES_ARRAY (za_s_unsigned);
858 DEF_SVE_TYPES_ARRAY (za_s_integer);
859 DEF_SVE_TYPES_ARRAY (za_s_float);
860 DEF_SVE_TYPES_ARRAY (za_s_data);
861 DEF_SVE_TYPES_ARRAY (za_d_h_integer);
862 DEF_SVE_TYPES_ARRAY (za_d_float);
863 DEF_SVE_TYPES_ARRAY (za_d_integer);
864 DEF_SVE_TYPES_ARRAY (mop_base);
865 DEF_SVE_TYPES_ARRAY (mop_base_signed);
866 DEF_SVE_TYPES_ARRAY (mop_base_unsigned);
867 DEF_SVE_TYPES_ARRAY (mop_i16i64);
868 DEF_SVE_TYPES_ARRAY (mop_i16i64_signed);
869 DEF_SVE_TYPES_ARRAY (mop_i16i64_unsigned);
870 DEF_SVE_TYPES_ARRAY (za);
871
872 static const group_suffix_index groups_none[] = {
873 GROUP_none, NUM_GROUP_SUFFIXES
874 };
875
876 static const group_suffix_index groups_x2[] = { GROUP_x2, NUM_GROUP_SUFFIXES };
877
878 static const group_suffix_index groups_x12[] = {
879 GROUP_none, GROUP_x2, NUM_GROUP_SUFFIXES
880 };
881
882 static const group_suffix_index groups_x4[] = { GROUP_x4, NUM_GROUP_SUFFIXES };
883
884 static const group_suffix_index groups_x24[] = {
885 GROUP_x2, GROUP_x4, NUM_GROUP_SUFFIXES
886 };
887
888 static const group_suffix_index groups_x124[] = {
889 GROUP_none, GROUP_x2, GROUP_x4, NUM_GROUP_SUFFIXES
890 };
891
892 static const group_suffix_index groups_x1234[] = {
893 GROUP_none, GROUP_x2, GROUP_x3, GROUP_x4, NUM_GROUP_SUFFIXES
894 };
895
896 static const group_suffix_index groups_vg1x2[] = {
897 GROUP_vg1x2, NUM_GROUP_SUFFIXES
898 };
899
900 static const group_suffix_index groups_vg1x4[] = {
901 GROUP_vg1x4, NUM_GROUP_SUFFIXES
902 };
903
904 static const group_suffix_index groups_vg1x24[] = {
905 GROUP_vg1x2, GROUP_vg1x4, NUM_GROUP_SUFFIXES
906 };
907
908 static const group_suffix_index groups_vg2[] = {
909 GROUP_vg2x1, GROUP_vg2x2, GROUP_vg2x4, NUM_GROUP_SUFFIXES
910 };
911
912 static const group_suffix_index groups_vg4[] = {
913 GROUP_vg4x1, GROUP_vg4x2, GROUP_vg4x4, NUM_GROUP_SUFFIXES
914 };
915
916 static const group_suffix_index groups_vg24[] = {
917 GROUP_vg2, GROUP_vg4, NUM_GROUP_SUFFIXES
918 };
919
920 /* Used by functions that have no governing predicate. */
921 static const predication_index preds_none[] = { PRED_none, NUM_PREDS };
922
923 /* Used by functions that have a governing predicate but do not have an
924 explicit suffix. */
925 static const predication_index preds_implicit[] = { PRED_implicit, NUM_PREDS };
926
927 /* Used by functions that only support "_m" predication. */
928 static const predication_index preds_m[] = { PRED_m, NUM_PREDS };
929
930 /* Used by functions that allow merging and "don't care" predication,
931 but are not suitable for predicated MOVPRFX. */
932 static const predication_index preds_mx[] = {
933 PRED_m, PRED_x, NUM_PREDS
934 };
935
936 /* Used by functions that allow merging, zeroing and "don't care"
937 predication. */
938 static const predication_index preds_mxz[] = {
939 PRED_m, PRED_x, PRED_z, NUM_PREDS
940 };
941
942 /* Used by functions that have the mxz predicated forms above, and in addition
943 have an unpredicated form. */
944 static const predication_index preds_mxz_or_none[] = {
945 PRED_m, PRED_x, PRED_z, PRED_none, NUM_PREDS
946 };
947
948 /* Used by functions that allow merging and zeroing predication but have
949 no "_x" form. */
950 static const predication_index preds_mz[] = { PRED_m, PRED_z, NUM_PREDS };
951
952 /* Used by functions that have an unpredicated form and a _z predicated
953 form. */
954 static const predication_index preds_z_or_none[] = {
955 PRED_z, PRED_none, NUM_PREDS
956 };
957
958 /* Used by (mostly predicate) functions that only support "_z" predication. */
959 static const predication_index preds_z[] = { PRED_z, NUM_PREDS };
960
961 /* Used by SME instructions that always merge into ZA. */
962 static const predication_index preds_za_m[] = { PRED_za_m, NUM_PREDS };
963
964 #define NONSTREAMING_SVE(X) nonstreaming_only (AARCH64_FL_SVE | (X))
965 #define SVE_AND_SME(X, Y) streaming_compatible (AARCH64_FL_SVE | (X), (Y))
966 #define SSVE(X) SVE_AND_SME (X, X)
967
968 /* A list of all arm_sve.h functions. */
969 static CONSTEXPR const function_group_info function_groups[] = {
970 #define DEF_SVE_FUNCTION_GS_FPM(NAME, SHAPE, TYPES, GROUPS, PREDS, FPM_MODE) \
971 { #NAME, &functions::NAME, &shapes::SHAPE, types_##TYPES, groups_##GROUPS, \
972 preds_##PREDS, aarch64_required_extensions::REQUIRED_EXTENSIONS, \
973 FPM_##FPM_MODE },
974 #include "aarch64-sve-builtins.def"
975 };
976
977 /* A list of all arm_neon_sve_bridge.h ACLE functions. */
978 static CONSTEXPR const function_group_info neon_sve_function_groups[] = {
979 #define DEF_NEON_SVE_FUNCTION(NAME, SHAPE, TYPES, GROUPS, PREDS) \
980 { #NAME, &neon_sve_bridge_functions::NAME, &shapes::SHAPE, types_##TYPES, \
981 groups_##GROUPS, preds_##PREDS, aarch64_required_extensions::ssve (0), \
982 FPM_unused },
983 #include "aarch64-neon-sve-bridge-builtins.def"
984 };
985
986 /* A list of all arm_sme.h functions. */
987 static CONSTEXPR const function_group_info sme_function_groups[] = {
988 #define DEF_SME_FUNCTION_GS(NAME, SHAPE, TYPES, GROUPS, PREDS) \
989 { #NAME, &functions::NAME, &shapes::SHAPE, types_##TYPES, groups_##GROUPS, \
990 preds_##PREDS, aarch64_required_extensions::REQUIRED_EXTENSIONS, \
991 FPM_unused },
992 #define DEF_SME_ZA_FUNCTION_GS(NAME, SHAPE, TYPES, GROUPS, PREDS) \
993 { #NAME, &functions::NAME##_za, &shapes::SHAPE, types_##TYPES, \
994 groups_##GROUPS, preds_##PREDS, \
995 aarch64_required_extensions::REQUIRED_EXTENSIONS \
996 .and_also (AARCH64_FL_ZA_ON), FPM_unused },
997 #include "aarch64-sve-builtins-sme.def"
998 };
999
1000 /* The scalar type associated with each vector type. */
1001 extern GTY(()) tree scalar_types[NUM_VECTOR_TYPES + 1];
1002 tree scalar_types[NUM_VECTOR_TYPES + 1];
1003
1004 /* The single-predicate and single-vector types, with their built-in
1005 "__SV..._t" name. Allow an index of NUM_VECTOR_TYPES, which always
1006 yields a null tree. */
1007 static GTY(()) tree abi_vector_types[NUM_VECTOR_TYPES + 1];
1008
1009 /* Same, but with the arm_sve.h "sv..._t" name. */
1010 extern GTY(()) tree acle_vector_types[MAX_TUPLE_SIZE][NUM_VECTOR_TYPES + 1];
1011 tree acle_vector_types[MAX_TUPLE_SIZE][NUM_VECTOR_TYPES + 1];
1012
1013 /* The svpattern enum type. */
1014 extern GTY(()) tree acle_svpattern;
1015 tree acle_svpattern;
1016
1017 /* The svprfop enum type. */
1018 extern GTY(()) tree acle_svprfop;
1019 tree acle_svprfop;
1020
1021 /* The list of all registered function decls, indexed by code. */
1022 static GTY(()) vec<registered_function *, va_gc> *registered_functions;
1023
1024 /* Stores the starting function index for each pragma handler. */
1025 static GTY(()) unsigned int initial_indexes[NUM_PRAGMA_HANDLERS];
1026
1027 /* All registered function decls, hashed on the function_instance
1028 that they implement. This is used for looking up implementations of
1029 overloaded functions. */
1030 static GTY(()) hash_table<registered_function_hasher> *function_table;
1031
1032 /* Index 0 maps all overloaded function names that we've registered so far to
1033 their associated function_instances. Index 1 does the same for functions
1034 that we've skipped over without registering. In both cases, the map keys
1035 are IDENTIFIER_NODEs. */
1036 static GTY(()) hash_map<tree, registered_function *> *overload_names[2];
1037
1038 /* Record that TYPE is an ABI-defined SVE type that contains NUM_ZR SVE vectors
1039 and NUM_PR SVE predicates. MANGLED_NAME, if nonnull, is the ABI-defined
1040 mangling of the type. mangling of the type. ACLE_NAME is the <arm_sve.h>
1041 name of the type, or null if <arm_sve.h> does not provide the type. */
1042 void
1043 add_sve_type_attribute (tree type, unsigned int num_zr, unsigned int num_pr,
1044 const char *mangled_name, const char *acle_name)
1045 {
1046 tree mangled_name_tree
1047 = (mangled_name ? get_identifier (mangled_name) : NULL_TREE);
1048 tree acle_name_tree
1049 = (acle_name ? get_identifier (acle_name) : NULL_TREE);
1050
1051 tree value = tree_cons (NULL_TREE, acle_name_tree, NULL_TREE);
1052 value = tree_cons (NULL_TREE, mangled_name_tree, value);
1053 value = tree_cons (NULL_TREE, size_int (num_pr), value);
1054 value = tree_cons (NULL_TREE, size_int (num_zr), value);
1055 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("SVE type"), value,
1056 TYPE_ATTRIBUTES (type));
1057 }
1058
1059 /* If TYPE is an ABI-defined SVE type, return its attribute descriptor,
1060 otherwise return null. */
1061 static tree
1062 lookup_sve_type_attribute (const_tree type)
1063 {
1064 if (type == error_mark_node)
1065 return NULL_TREE;
1066 return lookup_attribute ("SVE type", TYPE_ATTRIBUTES (type));
1067 }
1068
1069 /* Force TYPE to be a sizeless type. */
1070 static void
1071 make_type_sizeless (tree type)
1072 {
1073 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("SVE sizeless type"),
1074 NULL_TREE, TYPE_ATTRIBUTES (type));
1075 }
1076
1077 /* Return true if TYPE is a sizeless type. */
1078 static bool
1079 sizeless_type_p (const_tree type)
1080 {
1081 if (type == error_mark_node)
1082 return NULL_TREE;
1083 return lookup_attribute ("SVE sizeless type", TYPE_ATTRIBUTES (type));
1084 }
1085
1086 /* Return true if CANDIDATE is equivalent to MODEL_TYPE for overloading
1087 purposes. */
1088 static bool
1089 matches_type_p (const_tree model_type, const_tree candidate)
1090 {
1091 if (VECTOR_TYPE_P (model_type))
1092 {
1093 if (!VECTOR_TYPE_P (candidate)
1094 || maybe_ne (TYPE_VECTOR_SUBPARTS (model_type),
1095 TYPE_VECTOR_SUBPARTS (candidate))
1096 || TYPE_MODE (model_type) != TYPE_MODE (candidate))
1097 return false;
1098
1099 model_type = TREE_TYPE (model_type);
1100 candidate = TREE_TYPE (candidate);
1101 }
1102 return (candidate != error_mark_node
1103 && TYPE_MAIN_VARIANT (model_type) == TYPE_MAIN_VARIANT (candidate));
1104 }
1105
1106 /* If TYPE is a valid SVE element type, return the corresponding type
1107 suffix, otherwise return NUM_TYPE_SUFFIXES. */
1108 static type_suffix_index
1109 find_type_suffix_for_scalar_type (const_tree type)
1110 {
1111 /* A linear search should be OK here, since the code isn't hot and
1112 the number of types is only small. */
1113 for (unsigned int suffix_i = 0; suffix_i < NUM_TYPE_SUFFIXES; ++suffix_i)
1114 if (type_suffixes[suffix_i].vector_p)
1115 {
1116 vector_type_index vector_i = type_suffixes[suffix_i].vector_type;
1117 if (matches_type_p (scalar_types[vector_i], type))
1118 return type_suffix_index (suffix_i);
1119 }
1120 return NUM_TYPE_SUFFIXES;
1121 }
1122
1123 /* Return the implicit group suffix for intrinsics that operate on NVECTORS
1124 vectors. */
1125 static group_suffix_index
1126 num_vectors_to_group (unsigned int nvectors)
1127 {
1128 switch (nvectors)
1129 {
1130 case 1: return GROUP_none;
1131 case 2: return GROUP_x2;
1132 case 3: return GROUP_x3;
1133 case 4: return GROUP_x4;
1134 }
1135 gcc_unreachable ();
1136 }
1137
1138 /* If FNDECL is an SVE builtin, return its function instance, otherwise
1139 return null. */
1140 const function_instance *
1141 lookup_fndecl (tree fndecl)
1142 {
1143 if (!fndecl_built_in_p (fndecl, BUILT_IN_MD))
1144 return nullptr;
1145
1146 unsigned int code = DECL_MD_FUNCTION_CODE (fndecl);
1147 if ((code & AARCH64_BUILTIN_CLASS) != AARCH64_BUILTIN_SVE)
1148 return nullptr;
1149
1150 unsigned int subcode = code >> AARCH64_BUILTIN_SHIFT;
1151 return &(*registered_functions)[subcode]->instance;
1152 }
1153
1154
1155 /* Try to fold constant arguments ARG1 and ARG2 using the given tree_code.
1156 Operations are not treated as overflowing. */
1157 static tree
1158 aarch64_const_binop (enum tree_code code, tree arg1, tree arg2)
1159 {
1160 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1161 {
1162 poly_wide_int poly_res;
1163 tree type = TREE_TYPE (arg1);
1164 signop sign = TYPE_SIGN (type);
1165 wi::overflow_type overflow = wi::OVF_NONE;
1166
1167 /* Return 0 for division by 0, like SDIV and UDIV do. */
1168 if (code == TRUNC_DIV_EXPR && integer_zerop (arg2))
1169 return arg2;
1170 /* Return 0 if shift amount is out of range. */
1171 if (code == LSHIFT_EXPR
1172 && wi::geu_p (wi::to_wide (arg2), TYPE_PRECISION (type)))
1173 return build_int_cst (type, 0);
1174 if (!poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1175 return NULL_TREE;
1176 return force_fit_type (type, poly_res, false,
1177 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1178 }
1179 return NULL_TREE;
1180 }
1181
1182 /* Return the type that a vector base should have in a gather load or
1183 scatter store involving vectors of type TYPE. In an extending load,
1184 TYPE is the result of the extension; in a truncating store, it is the
1185 input to the truncation.
1186
1187 Index vectors have the same width as base vectors, but can be either
1188 signed or unsigned. */
1189 type_suffix_index
1190 function_shape::vector_base_type (type_suffix_index type) const
1191 {
1192 unsigned int required_bits = type_suffixes[type].element_bits;
1193 gcc_assert (required_bits == 32 || required_bits == 64);
1194 return required_bits == 32 ? TYPE_SUFFIX_u32 : TYPE_SUFFIX_u64;
1195 }
1196
1197 /* Return a hash code for a function_instance. */
1198 hashval_t
1199 function_instance::hash () const
1200 {
1201 inchash::hash h;
1202 /* BASE uniquely determines BASE_NAME, so we don't need to hash both. */
1203 h.add_ptr (base);
1204 h.add_ptr (shape);
1205 h.add_int (mode_suffix_id);
1206 h.add_int (type_suffix_ids[0]);
1207 h.add_int (type_suffix_ids[1]);
1208 h.add_int (group_suffix_id);
1209 h.add_int (pred);
1210 h.add_int (fpm_mode);
1211 return h.end ();
1212 }
1213
1214 /* Return a set of CP_* flags that describe what the function could do,
1215 taking the command-line flags into account. */
1216 unsigned int
1217 function_instance::call_properties () const
1218 {
1219 unsigned int flags = base->call_properties (*this);
1220
1221 /* -fno-trapping-math means that we can assume any FP exceptions
1222 are not user-visible. */
1223 if (!flag_trapping_math)
1224 flags &= ~CP_RAISE_FP_EXCEPTIONS;
1225
1226 return flags;
1227 }
1228
1229 /* Return true if calls to the function could read some form of
1230 global state. */
1231 bool
1232 function_instance::reads_global_state_p () const
1233 {
1234 unsigned int flags = call_properties ();
1235
1236 /* Preserve any dependence on rounding mode, flush to zero mode, etc.
1237 There is currently no way of turning this off; in particular,
1238 -fno-rounding-math (which is the default) means that we should make
1239 the usual assumptions about rounding mode, which for intrinsics means
1240 acting as the instructions do. */
1241 if (flags & CP_READ_FPCR)
1242 return true;
1243
1244 /* Handle direct reads of global state. */
1245 return flags & (CP_READ_MEMORY | CP_READ_FFR | CP_READ_ZA | CP_READ_ZT0);
1246 }
1247
1248 /* Return true if calls to the function could modify some form of
1249 global state. */
1250 bool
1251 function_instance::modifies_global_state_p () const
1252 {
1253 unsigned int flags = call_properties ();
1254
1255 /* Preserve any exception state written back to the FPCR,
1256 unless -fno-trapping-math says this is unnecessary. */
1257 if (flags & CP_RAISE_FP_EXCEPTIONS)
1258 return true;
1259
1260 /* Treat prefetches as modifying global state, since that's the
1261 only means we have of keeping them in their correct position. */
1262 if (flags & CP_PREFETCH_MEMORY)
1263 return true;
1264
1265 /* Handle direct modifications of global state. */
1266 return flags & (CP_WRITE_MEMORY | CP_WRITE_FFR | CP_WRITE_ZA | CP_WRITE_ZT0);
1267 }
1268
1269 /* Return true if calls to the function could raise a signal. */
1270 bool
1271 function_instance::could_trap_p () const
1272 {
1273 unsigned int flags = call_properties ();
1274
1275 /* Handle functions that could raise SIGFPE. */
1276 if (flags & CP_RAISE_FP_EXCEPTIONS)
1277 return true;
1278
1279 /* Handle functions that could raise SIGBUS or SIGSEGV. */
1280 if (flags & (CP_READ_MEMORY | CP_WRITE_MEMORY))
1281 return true;
1282
1283 return false;
1284 }
1285
1286 inline hashval_t
1287 registered_function_hasher::hash (value_type value)
1288 {
1289 return value->instance.hash ();
1290 }
1291
1292 inline bool
1293 registered_function_hasher::equal (value_type value, const compare_type &key)
1294 {
1295 return value->instance == key;
1296 }
1297
1298 sve_alignment_switcher::sve_alignment_switcher ()
1299 {
1300 m_old_maximum_field_alignment = maximum_field_alignment;
1301 maximum_field_alignment = 0;
1302 }
1303
1304 sve_alignment_switcher::~sve_alignment_switcher ()
1305 {
1306 maximum_field_alignment = m_old_maximum_field_alignment;
1307 }
1308
1309 function_builder::function_builder (handle_pragma_index pragma_index,
1310 bool function_nulls)
1311 {
1312 m_overload_type = build_function_type (void_type_node, void_list_node);
1313 m_direct_overloads = lang_GNU_CXX () || in_lto_p;
1314
1315 if (initial_indexes[pragma_index] == 0)
1316 {
1317 unsigned int index = vec_safe_length (registered_functions);
1318 initial_indexes[pragma_index] = index;
1319 }
1320
1321 m_function_index = initial_indexes[pragma_index];
1322 m_function_nulls = function_nulls;
1323
1324 gcc_obstack_init (&m_string_obstack);
1325 }
1326
1327 function_builder::~function_builder ()
1328 {
1329 obstack_free (&m_string_obstack, NULL);
1330 }
1331
1332 /* Add NAME to the end of the function name being built. */
1333 void
1334 function_builder::append_name (const char *name)
1335 {
1336 obstack_grow (&m_string_obstack, name, strlen (name));
1337 }
1338
1339 /* Zero-terminate and complete the function name being built. */
1340 char *
1341 function_builder::finish_name ()
1342 {
1343 obstack_1grow (&m_string_obstack, 0);
1344 return (char *) obstack_finish (&m_string_obstack);
1345 }
1346
1347 /* Return the overloaded or full function name for INSTANCE; OVERLOADED_P
1348 selects which. Allocate the string on m_string_obstack; the caller
1349 must use obstack_free to free it after use. */
1350 char *
1351 function_builder::get_name (const function_instance &instance,
1352 bool overloaded_p)
1353 {
1354 /* __arm_* functions are listed as arm_*, so that the associated GCC
1355 code is not in the implementation namespace. */
1356 if (strncmp (instance.base_name, "arm_", 4) == 0)
1357 append_name ("__");
1358 append_name (instance.base_name);
1359 if (overloaded_p)
1360 switch (instance.displacement_units ())
1361 {
1362 case UNITS_none:
1363 break;
1364
1365 case UNITS_bytes:
1366 append_name ("_offset");
1367 break;
1368
1369 case UNITS_elements:
1370 append_name ("_index");
1371 break;
1372
1373 case UNITS_vectors:
1374 append_name ("_vnum");
1375 break;
1376 }
1377 else
1378 append_name (instance.mode_suffix ().string);
1379 for (unsigned int i = 0; i < 2; ++i)
1380 if (!overloaded_p || instance.shape->explicit_type_suffix_p (i))
1381 append_name (instance.type_suffix (i).string);
1382 if (!overloaded_p || instance.shape->explicit_group_suffix_p ())
1383 append_name (instance.group_suffix ().string);
1384 append_name (pred_suffixes[instance.pred]);
1385 if (instance.fpm_mode == FPM_set)
1386 append_name ("_fpm");
1387 return finish_name ();
1388 }
1389
1390 /* Add attribute NAME to ATTRS. */
1391 static tree
1392 add_attribute (const char *name, tree attrs)
1393 {
1394 return tree_cons (get_identifier (name), NULL_TREE, attrs);
1395 }
1396
1397 /* Add attribute NS::NAME to ATTRS. */
1398 static tree
1399 add_attribute (const char *ns, const char *name, tree value, tree attrs)
1400 {
1401 return tree_cons (build_tree_list (get_identifier (ns),
1402 get_identifier (name)),
1403 value, attrs);
1404 }
1405
1406 /* Attribute arm::NAME describes shared state that is an input if IS_IN
1407 and an output if IS_OUT. Check whether a call with call properties
1408 CALL_FLAGS needs such an attribute. Add it to in-progress attribute
1409 list ATTRS if so. Return the new attribute list. */
1410 static tree
1411 add_shared_state_attribute (const char *name, bool is_in, bool is_out,
1412 unsigned int call_flags, tree attrs)
1413 {
1414 struct state_flag_info
1415 {
1416 const char *name;
1417 unsigned int read_flag;
1418 unsigned int write_flag;
1419 };
1420 static state_flag_info state_flags[] =
1421 {
1422 { "za", CP_READ_ZA, CP_WRITE_ZA },
1423 { "zt0", CP_READ_ZT0, CP_WRITE_ZT0 }
1424 };
1425
1426 tree args = NULL_TREE;
1427 for (const auto &state_flag : state_flags)
1428 {
1429 auto all_flags = state_flag.read_flag | state_flag.write_flag;
1430 auto these_flags = ((is_in ? state_flag.read_flag : 0)
1431 | (is_out ? state_flag.write_flag : 0));
1432 if ((call_flags & all_flags) == these_flags)
1433 {
1434 tree value = build_string (strlen (state_flag.name) + 1,
1435 state_flag.name);
1436 args = tree_cons (NULL_TREE, value, args);
1437 }
1438 }
1439 if (args)
1440 attrs = add_attribute ("arm", name, args, attrs);
1441 return attrs;
1442 }
1443
1444 /* Return the appropriate function attributes for INSTANCE, which requires
1445 the architecture extensions in REQUIRED_EXTENSIONS. */
1446 tree
1447 function_builder::get_attributes (const function_instance &instance,
1448 aarch64_required_extensions
1449 required_extensions)
1450 {
1451 tree attrs = NULL_TREE;
1452
1453 if (required_extensions.sm_off == 0)
1454 attrs = add_attribute ("arm", "streaming", NULL_TREE, attrs);
1455 else if (required_extensions.sm_on != 0)
1456 attrs = add_attribute ("arm", "streaming_compatible", NULL_TREE, attrs);
1457
1458 attrs = add_shared_state_attribute ("in", true, false,
1459 instance.call_properties (), attrs);
1460 attrs = add_shared_state_attribute ("out", false, true,
1461 instance.call_properties (), attrs);
1462 attrs = add_shared_state_attribute ("inout", true, true,
1463 instance.call_properties (), attrs);
1464
1465 if (!instance.modifies_global_state_p ())
1466 {
1467 if (instance.reads_global_state_p ())
1468 attrs = add_attribute ("pure", attrs);
1469 else
1470 attrs = add_attribute ("const", attrs);
1471 }
1472
1473 if (!flag_non_call_exceptions || !instance.could_trap_p ())
1474 attrs = add_attribute ("nothrow", attrs);
1475
1476 return add_attribute ("leaf", attrs);
1477 }
1478
1479 /* Add a function called NAME with type FNTYPE and attributes ATTRS.
1480 INSTANCE describes what the function does and OVERLOADED_P indicates
1481 whether it is overloaded. REQUIRED_EXTENSIONS describes the architecture
1482 extensions that the function requires. */
1483 registered_function &
1484 function_builder::add_function (const function_instance &instance,
1485 const char *name, tree fntype, tree attrs,
1486 aarch64_required_extensions
1487 required_extensions,
1488 bool overloaded_p,
1489 bool placeholder_p)
1490 {
1491 unsigned int length = vec_safe_length (registered_functions);
1492 unsigned int code = (m_function_index << AARCH64_BUILTIN_SHIFT) | AARCH64_BUILTIN_SVE;
1493 /* We need to be able to generate placeholders to enusre that we have a
1494 consistent numbering scheme for function codes between the C and C++
1495 frontends, so that everything ties up in LTO.
1496
1497 Currently, tree-streamer-in.cc:unpack_ts_function_decl_value_fields
1498 validates that tree nodes returned by TARGET_BUILTIN_DECL are non-NULL and
1499 some node other than error_mark_node. This is a holdover from when builtin
1500 decls were streamed by code rather than by value.
1501
1502 Ultimately, we should be able to remove this validation of BUILT_IN_MD
1503 nodes and remove the target hook. For now, however, we need to appease the
1504 validation and return a non-NULL, non-error_mark_node node, so we
1505 arbitrarily choose integer_zero_node. */
1506 tree decl = placeholder_p || m_function_nulls
1507 ? integer_zero_node
1508 : simulate_builtin_function_decl (input_location, name, fntype,
1509 code, NULL, attrs);
1510
1511 registered_function &rfn = *ggc_alloc <registered_function> ();
1512 rfn.instance = instance;
1513 rfn.decl = decl;
1514 rfn.required_extensions = required_extensions;
1515 rfn.overloaded_p = overloaded_p;
1516 if (m_function_index >= length)
1517 vec_safe_push (registered_functions, &rfn);
1518 else
1519 (*registered_functions)[m_function_index] = &rfn;
1520 m_function_index++;
1521
1522 return rfn;
1523 }
1524
1525 /* Add a built-in function for INSTANCE, with the argument types given
1526 by ARGUMENT_TYPES and the return type given by RETURN_TYPE.
1527 REQUIRED_EXTENSIONS describes the architecture extensions that the
1528 function requires. FORCE_DIRECT_OVERLOADS is true if there is a
1529 one-to-one mapping between "short" and "full" names, and if standard
1530 overload resolution therefore isn't necessary. */
1531 void
1532 function_builder::
1533 add_unique_function (const function_instance &instance,
1534 tree return_type,
1535 vec<tree> &argument_types,
1536 aarch64_required_extensions required_extensions,
1537 bool force_direct_overloads)
1538 {
1539 /* Add the function under its full (unique) name. */
1540 char *name = get_name (instance, false);
1541 tree fntype = build_function_type_array (return_type,
1542 argument_types.length (),
1543 argument_types.address ());
1544 tree attrs = get_attributes (instance, required_extensions);
1545 registered_function &rfn = add_function (instance, name, fntype, attrs,
1546 required_extensions, false, false);
1547
1548 /* Enter the function into the hash table. */
1549 if (!m_function_nulls)
1550 {
1551 hashval_t hash = instance.hash ();
1552 registered_function **rfn_slot
1553 = function_table->find_slot_with_hash (instance, hash, INSERT);
1554 gcc_assert (!*rfn_slot);
1555 *rfn_slot = &rfn;
1556 }
1557
1558 /* Also add the function under its overloaded alias, if we want
1559 a separate decl for each instance of an overloaded function. */
1560 char *overload_name = get_name (instance, true);
1561 if (strcmp (name, overload_name) != 0)
1562 {
1563 /* Attribute lists shouldn't be shared. */
1564 tree attrs = get_attributes (instance, required_extensions);
1565 bool placeholder_p = !(m_direct_overloads || force_direct_overloads);
1566 add_function (instance, overload_name, fntype, attrs,
1567 required_extensions, false, placeholder_p);
1568 }
1569
1570 obstack_free (&m_string_obstack, name);
1571 }
1572
1573 /* Add one function decl for INSTANCE, to be used with manual overload
1574 resolution. REQUIRED_EXTENSIONS describes the architecture extensions
1575 that the function requires.
1576
1577 For simplicity, deal with duplicate attempts to add the same function,
1578 including cases in which the new function requires more features than
1579 the original one did. In that case we'll check whether the required
1580 features are available as part of resolving the function to the
1581 relevant unique function. */
1582 void
1583 function_builder::
1584 add_overloaded_function (const function_instance &instance,
1585 aarch64_required_extensions required_extensions)
1586 {
1587 auto &name_map = overload_names[m_function_nulls];
1588 if (!name_map)
1589 name_map = hash_map<tree, registered_function *>::create_ggc ();
1590
1591 char *name = get_name (instance, true);
1592 tree id = get_identifier (name);
1593 if (registered_function **map_value = name_map->get (id))
1594 {
1595 auto &dst_extensions = (*map_value)->required_extensions;
1596 /* Make sure that any streaming and streaming-compatible attributes
1597 on the function type are still correct. (It might not matter if
1598 they aren't, so this could be relaxed in future if we're sure that
1599 it's safe.) */
1600 gcc_assert ((*map_value)->instance == instance
1601 && (dst_extensions.sm_off || !required_extensions.sm_off)
1602 && (dst_extensions.sm_on || !required_extensions.sm_on));
1603 dst_extensions = dst_extensions.common_denominator (required_extensions);
1604 }
1605 else
1606 {
1607 registered_function &rfn
1608 = add_function (instance, name, m_overload_type, NULL_TREE,
1609 required_extensions, true, m_direct_overloads);
1610 name_map->put (id, &rfn);
1611 }
1612 obstack_free (&m_string_obstack, name);
1613 }
1614
1615 /* If we are using manual overload resolution, add one function decl
1616 for each overloaded function in GROUP. Take the function base name
1617 from GROUP and the mode from MODE. */
1618 void
1619 function_builder::add_overloaded_functions (const function_group_info &group,
1620 mode_suffix_index mode)
1621 {
1622 bool explicit_type0 = (*group.shape)->explicit_type_suffix_p (0);
1623 bool explicit_type1 = (*group.shape)->explicit_type_suffix_p (1);
1624 bool explicit_group = (*group.shape)->explicit_group_suffix_p ();
1625 auto add_function = [&](const type_suffix_pair &types,
1626 group_suffix_index group_suffix_id,
1627 unsigned int pi)
1628 {
1629 function_instance instance (group.base_name, *group.base,
1630 *group.shape, mode, types,
1631 group_suffix_id, group.preds[pi],
1632 group.fpm_mode);
1633 add_overloaded_function (instance, group.required_extensions);
1634 };
1635
1636 auto add_group_suffix = [&](group_suffix_index group_suffix_id,
1637 unsigned int pi)
1638 {
1639 if (mode == MODE_single
1640 && group_suffixes[group_suffix_id].vectors_per_tuple == 1)
1641 return;
1642
1643 if (!explicit_type0 && !explicit_type1)
1644 /* Deal with the common case in which there is one overloaded
1645 function for all type combinations. */
1646 add_function (types_none[0], group_suffix_id, pi);
1647 else
1648 for (unsigned int ti = 0; group.types[ti][0] != NUM_TYPE_SUFFIXES;
1649 ++ti)
1650 {
1651 /* Stub out the types that are determined by overload
1652 resolution. */
1653 type_suffix_pair types = {
1654 explicit_type0 ? group.types[ti][0] : NUM_TYPE_SUFFIXES,
1655 explicit_type1 ? group.types[ti][1] : NUM_TYPE_SUFFIXES
1656 };
1657 add_function (types, group_suffix_id, pi);
1658 }
1659 };
1660
1661 for (unsigned int pi = 0; group.preds[pi] != NUM_PREDS; ++pi)
1662 if (explicit_group)
1663 for (unsigned int gi = 0; group.groups[gi] != NUM_GROUP_SUFFIXES; ++gi)
1664 add_group_suffix (group.groups[gi], pi);
1665 else
1666 add_group_suffix (GROUP_none, pi);
1667 }
1668
1669 /* Register all the functions in GROUP. */
1670 void
1671 function_builder::register_function_group (const function_group_info &group)
1672 {
1673 (*group.shape)->build (*this, group);
1674 }
1675
1676 function_call_info::function_call_info (location_t location_in,
1677 const function_instance &instance_in,
1678 tree fndecl_in)
1679 : function_instance (instance_in), location (location_in), fndecl (fndecl_in)
1680 {
1681 }
1682
1683 function_resolver::function_resolver (location_t location,
1684 const function_instance &instance,
1685 tree fndecl, vec<tree, va_gc> &arglist)
1686 : function_call_info (location, instance, fndecl), m_arglist (arglist)
1687 {
1688 }
1689
1690 /* Return the <stdint.h> name associated with TYPE. Using the <stdint.h>
1691 name should be more user-friendly than the underlying canonical type,
1692 since it makes the signedness and bitwidth explicit. */
1693 const char *
1694 function_resolver::get_scalar_type_name (type_suffix_index type)
1695 {
1696 return vector_types[type_suffixes[type].vector_type].acle_name + 2;
1697 }
1698
1699 /* Return the type of argument I, or error_mark_node if it isn't
1700 well-formed. */
1701 tree
1702 function_resolver::get_argument_type (unsigned int i)
1703 {
1704 tree arg = m_arglist[i];
1705 return arg == error_mark_node ? arg : TREE_TYPE (arg);
1706 }
1707
1708 /* Return true if argument I is some form of scalar value. */
1709 bool
1710 function_resolver::scalar_argument_p (unsigned int i)
1711 {
1712 tree type = get_argument_type (i);
1713 return (INTEGRAL_TYPE_P (type)
1714 /* Allow pointer types, leaving the frontend to warn where
1715 necessary. */
1716 || POINTER_TYPE_P (type)
1717 || SCALAR_FLOAT_TYPE_P (type));
1718 }
1719
1720 /* Report that argument ARGNO was expected to have NUM_VECTORS vectors.
1721 TYPE is the type that ARGNO actually has. */
1722 void
1723 function_resolver::report_incorrect_num_vectors (unsigned int argno,
1724 sve_type type,
1725 unsigned int num_vectors)
1726 {
1727 if (num_vectors == 1)
1728 error_at (location, "passing %qT to argument %d of %qE, which"
1729 " expects a single SVE vector rather than a tuple",
1730 get_vector_type (type), argno + 1, fndecl);
1731 else if (type.num_vectors == 1
1732 && type.type != TYPE_SUFFIX_b)
1733 /* num_vectors is always != 1, so the singular isn't needed. */
1734 error_n (location, num_vectors, "%qT%d%qE%d",
1735 "passing single vector %qT to argument %d"
1736 " of %qE, which expects a tuple of %d vectors",
1737 get_vector_type (type), argno + 1, fndecl, num_vectors);
1738 else
1739 /* num_vectors is always != 1, so the singular isn't needed. */
1740 error_n (location, num_vectors, "%qT%d%qE%d",
1741 "passing %qT to argument %d of %qE, which"
1742 " expects a tuple of %d vectors", get_vector_type (type),
1743 argno + 1, fndecl, num_vectors);
1744 }
1745
1746 /* Report that arguments FIRST_ARGNO and ARGNO have different numbers
1747 of vectors, but are required to have the same number of vectors.
1748 FIRST_TYPE and TYPE are the types that arguments FIRST_ARGNO and
1749 ARGNO actually have. */
1750 void
1751 function_resolver::report_mismatched_num_vectors (unsigned int first_argno,
1752 sve_type first_type,
1753 unsigned int argno,
1754 sve_type type)
1755 {
1756 /* If the tuple size is implied by the group suffix, and if the first
1757 type had the right number of vectors, treat argument ARGNO as being
1758 individually wrong, rather than wrong in relation to FIRST_ARGNO. */
1759 if (group_suffix_id != GROUP_none
1760 && first_type.num_vectors == vectors_per_tuple ())
1761 {
1762 report_incorrect_num_vectors (argno, type, first_type.num_vectors);
1763 return;
1764 }
1765
1766 /* Make sure that FIRST_TYPE itself is sensible before using it
1767 as a basis for an error message. */
1768 if (resolve_to (mode_suffix_id, first_type) == error_mark_node)
1769 return;
1770
1771 if (type.num_vectors != 1 && first_type.num_vectors == 1)
1772 error_at (location, "passing tuple %qT to argument %d of %qE after"
1773 " passing single vector %qT to argument %d",
1774 get_vector_type (type), argno + 1, fndecl,
1775 get_vector_type (first_type), first_argno + 1);
1776 else if (type.num_vectors == 1 && first_type.num_vectors != 1)
1777 error_at (location, "passing single vector %qT to argument %d"
1778 " of %qE after passing tuple %qT to argument %d",
1779 get_vector_type (type), argno + 1, fndecl,
1780 get_vector_type (first_type), first_argno + 1);
1781 else
1782 error_at (location, "passing mismatched tuple types %qT and %qT"
1783 " to arguments %d and %d of %qE",
1784 get_vector_type (first_type), get_vector_type (type),
1785 first_argno + 1, argno + 1, fndecl);
1786 }
1787
1788 /* Report that the function has no form that takes type TYPE.
1789 Return error_mark_node. */
1790 tree
1791 function_resolver::report_no_such_form (sve_type type)
1792 {
1793 error_at (location, "%qE has no form that takes %qT arguments",
1794 fndecl, get_vector_type (type));
1795 return error_mark_node;
1796 }
1797
1798 /* Silently check whether there is an instance of the function with the
1799 mode suffix given by MODE, the type suffixes given by TYPE0 and TYPE1,
1800 and the group suffix given by GROUP. Return its function decl if so,
1801 otherwise return null. */
1802 tree
1803 function_resolver::lookup_form (mode_suffix_index mode,
1804 type_suffix_index type0,
1805 type_suffix_index type1,
1806 group_suffix_index group)
1807 {
1808 type_suffix_pair types = { type0, type1 };
1809 function_instance instance (base_name, base, shape, mode, types, group, pred,
1810 fpm_mode);
1811 registered_function *rfn
1812 = function_table->find_with_hash (instance, instance.hash ());
1813 return rfn ? rfn->decl : NULL_TREE;
1814 }
1815
1816 /* Silently check whether there is an instance of the function that has the
1817 mode suffix given by MODE and the type and group suffixes implied by TYPE.
1818 If the overloaded function has an explicit first type suffix (like
1819 conversions do), TYPE describes the implicit second type suffix.
1820 Otherwise, TYPE describes the only type suffix.
1821
1822 Return the decl of the function if it exists, otherwise return null. */
1823 tree
1824 function_resolver::lookup_form (mode_suffix_index mode, sve_type type)
1825 {
1826 type_suffix_index type0 = type_suffix_ids[0];
1827 type_suffix_index type1 = type_suffix_ids[1];
1828 (type0 == NUM_TYPE_SUFFIXES ? type0 : type1) = type.type;
1829
1830 group_suffix_index group = group_suffix_id;
1831 if (group == GROUP_none && type.num_vectors != vectors_per_tuple ())
1832 group = num_vectors_to_group (type.num_vectors);
1833
1834 return lookup_form (mode, type0, type1, group);
1835 }
1836
1837 /* Resolve the function to one with the mode suffix given by MODE, the
1838 type suffixes given by TYPE0 and TYPE1, and group suffix given by
1839 GROUP. Return its function decl on success, otherwise report an
1840 error and return error_mark_node. */
1841 tree
1842 function_resolver::resolve_to (mode_suffix_index mode,
1843 type_suffix_index type0,
1844 type_suffix_index type1,
1845 group_suffix_index group)
1846 {
1847 tree res = lookup_form (mode, type0, type1, group);
1848 if (!res)
1849 {
1850 if (type1 == NUM_TYPE_SUFFIXES)
1851 return report_no_such_form (type0);
1852 if (type0 == type_suffix_ids[0])
1853 return report_no_such_form (type1);
1854 error_at (location, "%qE has no form that takes %qT and %qT arguments",
1855 fndecl, get_vector_type (type0), get_vector_type (type1));
1856 return error_mark_node;
1857 }
1858 return res;
1859 }
1860
1861 /* Resolve the function to one that has the suffixes associated with MODE
1862 and TYPE; see lookup_form for how TYPE is interpreted. Return the
1863 function decl on success, otherwise report an error and return
1864 error_mark_node. */
1865 tree
1866 function_resolver::resolve_to (mode_suffix_index mode, sve_type type)
1867 {
1868 if (tree res = lookup_form (mode, type))
1869 return res;
1870
1871 return report_no_such_form (type);
1872 }
1873
1874 /* Like resolve_to, but used for a conversion function with the following
1875 properties:
1876
1877 - The function has an explicit first type suffix.
1878 - The elements of the argument (which has type TYPE) might be narrower
1879 or wider than the elements of the return type.
1880 - The return type has enough vectors to represent the converted value
1881 of every element.
1882 - The group suffix describes the wider of the argument type and the
1883 return type. */
1884 tree
1885 function_resolver::resolve_conversion (mode_suffix_index mode, sve_type type)
1886 {
1887 auto ret_type = type_suffix_ids[0];
1888 unsigned int num_ret_vectors = (type.num_vectors
1889 * type_suffixes[ret_type].element_bits
1890 / type_suffixes[type.type].element_bits);
1891 if (num_ret_vectors == 1
1892 || num_ret_vectors == 2
1893 || num_ret_vectors == 4)
1894 {
1895 unsigned int num_vectors = MAX (num_ret_vectors, type.num_vectors);
1896 if (tree res = lookup_form (mode, { type.type, num_vectors }))
1897 return res;
1898 }
1899 return report_no_such_form (type);
1900 }
1901
1902 /* Require argument ARGNO to be an svbool_t or svcount_t predicate.
1903 Return its type on success, otherwise report an error and return
1904 NUM_VECTOR_TYPES. */
1905 vector_type_index
1906 function_resolver::infer_predicate_type (unsigned int argno)
1907 {
1908 tree actual = get_argument_type (argno);
1909 if (actual == error_mark_node)
1910 return NUM_VECTOR_TYPES;
1911
1912 for (auto index : { VECTOR_TYPE_svbool_t, VECTOR_TYPE_svcount_t })
1913 if (matches_type_p (acle_vector_types[0][index], actual))
1914 return index;
1915
1916 error_at (location, "passing %qT to argument %d of %qE, which expects"
1917 " an %qs or %qs", actual, argno + 1, fndecl, "svbool_t",
1918 "svcount_t");
1919 return NUM_VECTOR_TYPES;
1920 }
1921
1922 /* Require argument ARGNO to be a 32-bit or 64-bit scalar integer type.
1923 Return the associated type suffix on success, otherwise report an
1924 error and return NUM_TYPE_SUFFIXES. */
1925 type_suffix_index
1926 function_resolver::infer_integer_scalar_type (unsigned int argno)
1927 {
1928 tree actual = get_argument_type (argno);
1929 if (actual == error_mark_node)
1930 return NUM_TYPE_SUFFIXES;
1931
1932 /* Allow enums and booleans to decay to integers, for compatibility
1933 with C++ overloading rules. */
1934 if (INTEGRAL_TYPE_P (actual))
1935 {
1936 bool uns_p = TYPE_UNSIGNED (actual);
1937 /* Honor the usual integer promotions, so that resolution works
1938 in the same way as for C++. */
1939 if (TYPE_PRECISION (actual) < 32)
1940 return TYPE_SUFFIX_s32;
1941 if (TYPE_PRECISION (actual) == 32)
1942 return uns_p ? TYPE_SUFFIX_u32 : TYPE_SUFFIX_s32;
1943 if (TYPE_PRECISION (actual) == 64)
1944 return uns_p ? TYPE_SUFFIX_u64 : TYPE_SUFFIX_s64;
1945 }
1946
1947 error_at (location, "passing %qT to argument %d of %qE, which expects"
1948 " a 32-bit or 64-bit integer type", actual, argno + 1, fndecl);
1949 return NUM_TYPE_SUFFIXES;
1950 }
1951
1952 /* Return arguments ARGNO and ARGNO + 1 to be 64-bit scalar integers
1953 of the same signedness, or be a combination that converts unambiguously
1954 to such a pair. Return the associated type suffix if they are,
1955 otherwise report an error and return NUM_TYPE_SUFFIXES. */
1956 type_suffix_index
1957 function_resolver::infer_64bit_scalar_integer_pair (unsigned int argno)
1958 {
1959 /* Require two scalar integers, with one having 64 bits and the other
1960 one being no bigger. */
1961 tree types[] = { get_argument_type (argno), get_argument_type (argno + 1) };
1962 if (!INTEGRAL_TYPE_P (types[0])
1963 || !INTEGRAL_TYPE_P (types[1])
1964 || MAX (TYPE_PRECISION (types[0]), TYPE_PRECISION (types[1])) != 64)
1965 {
1966 error_at (location, "passing %qT and %qT to arguments %d and %d of %qE,"
1967 " which expects a pair of 64-bit integers", types[0], types[1],
1968 argno + 1, argno + 2, fndecl);
1969 return NUM_TYPE_SUFFIXES;
1970 }
1971
1972 /* Allow signed integers smaller than int64_t to be paired with an int64_t.
1973 Allow unsigned integers smaller than uint64_t to be paired with any
1974 64-bit integer. */
1975 for (int i = 0; i < 2; ++i)
1976 {
1977 if (TYPE_PRECISION (types[i]) != 64)
1978 continue;
1979
1980 if (TYPE_UNSIGNED (types[1 - i]) != TYPE_UNSIGNED (types[i]))
1981 {
1982 if (TYPE_PRECISION (types[1 - i]) == 64)
1983 continue;
1984 if (!TYPE_UNSIGNED (types[1 - i]))
1985 continue;
1986 }
1987 return TYPE_UNSIGNED (types[i]) ? TYPE_SUFFIX_u64 : TYPE_SUFFIX_s64;
1988 }
1989
1990 error_at (location, "passing mismatched integer types %qT and %qT"
1991 " to arguments %d and %d of %qE", types[0], types[1],
1992 argno + 1, argno + 2, fndecl);
1993 return NUM_TYPE_SUFFIXES;
1994 }
1995
1996 /* Require argument ARGNO to be a pointer to a scalar type that has a
1997 corresponding type suffix. Return that type suffix on success,
1998 otherwise report an error and return NUM_TYPE_SUFFIXES.
1999 GATHER_SCATTER_P is true if the function is a gather/scatter
2000 operation. RESTRICTIONS describes any additional restrictions
2001 on the target type. */
2002 type_suffix_index
2003 function_resolver::infer_pointer_type (unsigned int argno,
2004 bool gather_scatter_p,
2005 target_type_restrictions restrictions)
2006 {
2007 tree actual = get_argument_type (argno);
2008 if (actual == error_mark_node)
2009 return NUM_TYPE_SUFFIXES;
2010
2011 if (TREE_CODE (actual) != POINTER_TYPE)
2012 {
2013 error_at (location, "passing %qT to argument %d of %qE, which"
2014 " expects a pointer type", actual, argno + 1, fndecl);
2015 if (VECTOR_TYPE_P (actual) && gather_scatter_p)
2016 inform (location, "an explicit type suffix is needed"
2017 " when using a vector of base addresses");
2018 return NUM_TYPE_SUFFIXES;
2019 }
2020
2021 tree target = TREE_TYPE (actual);
2022 type_suffix_index type = find_type_suffix_for_scalar_type (target);
2023 if (type == NUM_TYPE_SUFFIXES)
2024 {
2025 error_at (location, "passing %qT to argument %d of %qE, but %qT is not"
2026 " a valid SVE element type", actual, argno + 1, fndecl,
2027 build_qualified_type (target, 0));
2028 return NUM_TYPE_SUFFIXES;
2029 }
2030 unsigned int bits = type_suffixes[type].element_bits;
2031 if (restrictions == TARGET_32_64 && bits != 32 && bits != 64)
2032 {
2033 error_at (location, "passing %qT to argument %d of %qE, which"
2034 " expects a pointer to 32-bit or 64-bit elements",
2035 actual, argno + 1, fndecl);
2036 return NUM_TYPE_SUFFIXES;
2037 }
2038 if (displacement_units () == UNITS_elements && bits == 8)
2039 {
2040 error_at (location, "passing %qT to argument %d of %qE, which"
2041 " expects the data to be 16 bits or wider",
2042 actual, argno + 1, fndecl);
2043 inform (location, "use the %<offset%> rather than %<index%> form"
2044 " for 8-bit data");
2045 return NUM_TYPE_SUFFIXES;
2046 }
2047
2048 return type;
2049 }
2050
2051 /* If TYPE is an SVE predicate or vector type, or a tuple of such a type,
2052 return the associated sve_type, otherwise return an invalid sve_type. */
2053 static sve_type
2054 find_sve_type (const_tree type)
2055 {
2056 /* A linear search should be OK here, since the code isn't hot and
2057 the number of types is only small. */
2058 for (unsigned int size_i = 0; size_i < MAX_TUPLE_SIZE; ++size_i)
2059 for (unsigned int suffix_i = 0; suffix_i < NUM_TYPE_SUFFIXES; ++suffix_i)
2060 {
2061 vector_type_index type_i = type_suffixes[suffix_i].vector_type;
2062 tree this_type = acle_vector_types[size_i][type_i];
2063 if (this_type && matches_type_p (this_type, type))
2064 return { type_suffix_index (suffix_i), size_i + 1 };
2065 }
2066
2067 return {};
2068 }
2069
2070 /* Require argument ARGNO to be an SVE type (i.e. something that can be
2071 represented by sve_type). Return the (valid) type if it is, otherwise
2072 report an error and return an invalid type. */
2073 sve_type
2074 function_resolver::infer_sve_type (unsigned int argno)
2075 {
2076 tree actual = get_argument_type (argno);
2077 if (actual == error_mark_node)
2078 return {};
2079
2080 if (sve_type type = find_sve_type (actual))
2081 return type;
2082
2083 if (scalar_argument_p (argno))
2084 error_at (location, "passing %qT to argument %d of %qE, which"
2085 " expects an SVE type rather than a scalar type",
2086 actual, argno + 1, fndecl);
2087 else
2088 error_at (location, "passing %qT to argument %d of %qE, which"
2089 " expects an SVE type",
2090 actual, argno + 1, fndecl);
2091 return {};
2092 }
2093
2094 /* Require argument ARGNO to be a single vector or a tuple of NUM_VECTORS
2095 vectors; NUM_VECTORS is 1 for the former. Return the associated type
2096 on success. Report an error on failure. */
2097 sve_type
2098 function_resolver::infer_vector_or_tuple_type (unsigned int argno,
2099 unsigned int num_vectors)
2100 {
2101 auto type = infer_sve_type (argno);
2102 if (!type)
2103 return type;
2104
2105 if (type.num_vectors == num_vectors)
2106 return type;
2107
2108 report_incorrect_num_vectors (argno, type, num_vectors);
2109 return {};
2110 }
2111
2112 /* Require argument ARGNO to have some form of vector type. Return the
2113 associated type suffix on success, using TYPE_SUFFIX_b for predicates.
2114 Report an error and return NUM_TYPE_SUFFIXES on failure. */
2115 type_suffix_index
2116 function_resolver::infer_vector_type (unsigned int argno)
2117 {
2118 if (auto type = infer_vector_or_tuple_type (argno, 1))
2119 return type.type;
2120 return NUM_TYPE_SUFFIXES;
2121 }
2122
2123 /* Like infer_vector_type, but also require the type to be integral. */
2124 type_suffix_index
2125 function_resolver::infer_integer_vector_type (unsigned int argno)
2126 {
2127 type_suffix_index type = infer_vector_type (argno);
2128 if (type == NUM_TYPE_SUFFIXES)
2129 return type;
2130
2131 if (!type_suffixes[type].integer_p)
2132 {
2133 error_at (location, "passing %qT to argument %d of %qE, which"
2134 " expects a vector of integers", get_argument_type (argno),
2135 argno + 1, fndecl);
2136 return NUM_TYPE_SUFFIXES;
2137 }
2138
2139 return type;
2140 }
2141
2142 /* Require argument ARGNO to have some form of NEON128 vector type. Return the
2143 associated type suffix on success.
2144 Report an error and return NUM_TYPE_SUFFIXES on failure. */
2145 type_suffix_index
2146 function_resolver::infer_neon128_vector_type (unsigned int argno)
2147 {
2148 tree actual = get_argument_type (argno);
2149 if (actual == error_mark_node)
2150 return NUM_TYPE_SUFFIXES;
2151
2152 for (unsigned int suffix_i = 0; suffix_i < NUM_TYPE_SUFFIXES; ++suffix_i)
2153 {
2154 int neon_index = type_suffixes[suffix_i].neon128_type;
2155 if (neon_index != ARM_NEON_H_TYPES_LAST)
2156 {
2157 tree type = aarch64_simd_types_trees[neon_index].itype;
2158 if (type && matches_type_p (type, actual))
2159 return type_suffix_index (suffix_i);
2160 }
2161 }
2162
2163 error_at (location, "passing %qT to argument %d of %qE, which"
2164 " expects a 128 bit NEON vector type", actual, argno + 1, fndecl);
2165 return NUM_TYPE_SUFFIXES;
2166 }
2167
2168
2169 /* Like infer_vector_type, but also require the type to be an unsigned
2170 integer. */
2171 type_suffix_index
2172 function_resolver::infer_unsigned_vector_type (unsigned int argno)
2173 {
2174 type_suffix_index type = infer_vector_type (argno);
2175 if (type == NUM_TYPE_SUFFIXES)
2176 return type;
2177
2178 if (!type_suffixes[type].unsigned_p)
2179 {
2180 error_at (location, "passing %qT to argument %d of %qE, which"
2181 " expects a vector of unsigned integers",
2182 get_argument_type (argno), argno + 1, fndecl);
2183 return NUM_TYPE_SUFFIXES;
2184 }
2185
2186 return type;
2187 }
2188
2189 /* Like infer_vector_type, but also require the element size to be
2190 32 or 64 bits. */
2191 type_suffix_index
2192 function_resolver::infer_sd_vector_type (unsigned int argno)
2193 {
2194 type_suffix_index type = infer_vector_type (argno);
2195 if (type == NUM_TYPE_SUFFIXES)
2196 return type;
2197
2198 unsigned int bits = type_suffixes[type].element_bits;
2199 if (bits != 32 && bits != 64)
2200 {
2201 error_at (location, "passing %qT to argument %d of %qE, which"
2202 " expects a vector of 32-bit or 64-bit elements",
2203 get_argument_type (argno), argno + 1, fndecl);
2204 return NUM_TYPE_SUFFIXES;
2205 }
2206
2207 return type;
2208 }
2209
2210 /* If the function operates on tuples of vectors, require argument ARGNO to be
2211 a tuple with the appropriate number of vectors, otherwise require it to be
2212 a single vector. Return the associated type on success. Report an error
2213 on failure. */
2214 sve_type
2215 function_resolver::infer_tuple_type (unsigned int argno)
2216 {
2217 return infer_vector_or_tuple_type (argno, vectors_per_tuple ());
2218 }
2219
2220 /* PRED_TYPE is the type of a governing predicate argument and DATA_TYPE
2221 is the type of an argument that it predicates. Require the two types
2222 to "agree": svcount_t must be used for multiple vectors and svbool_t
2223 for single vectors.
2224
2225 Return true if they do agree, otherwise report an error and
2226 return false. */
2227 bool function_resolver::
2228 require_matching_predicate_type (vector_type_index pred_type,
2229 sve_type data_type)
2230 {
2231 if (pred_type == VECTOR_TYPE_svbool_t && data_type.num_vectors == 1)
2232 return true;
2233
2234 if (pred_type == VECTOR_TYPE_svcount_t && data_type.num_vectors != 1)
2235 return true;
2236
2237 /* Make sure that FIRST_TYPE itself is sensible before using it
2238 as a basis for an error message. */
2239 if (resolve_to (mode_suffix_id, data_type) == error_mark_node)
2240 return false;
2241
2242 if (data_type.num_vectors > 1)
2243 error_at (location, "operations on multiple vectors must be predicated"
2244 " by %qs rather than %qs", "svcount_t", "svbool_t");
2245 else
2246 error_at (location, "operations on single vectors must be predicated"
2247 " by %qs rather than %qs", "svbool_t", "svcount_t");
2248 return false;
2249 }
2250
2251 /* Require argument ARGNO to be a vector or scalar argument. Return true
2252 if it is, otherwise report an appropriate error. */
2253 bool
2254 function_resolver::require_vector_or_scalar_type (unsigned int argno)
2255 {
2256 tree actual = get_argument_type (argno);
2257 if (actual == error_mark_node)
2258 return false;
2259
2260 if (!scalar_argument_p (argno) && !VECTOR_TYPE_P (actual))
2261 {
2262 error_at (location, "passing %qT to argument %d of %qE, which"
2263 " expects a vector or scalar type", actual, argno + 1, fndecl);
2264 return false;
2265 }
2266
2267 return true;
2268 }
2269
2270 /* Require argument ARGNO to have vector type TYPE, in cases where this
2271 requirement holds for all uses of the function. Return true if the
2272 argument has the right form, otherwise report an appropriate error. */
2273 bool
2274 function_resolver::require_vector_type (unsigned int argno,
2275 vector_type_index type)
2276 {
2277 tree expected = acle_vector_types[0][type];
2278 tree actual = get_argument_type (argno);
2279 if (actual == error_mark_node)
2280 return false;
2281
2282 if (!matches_type_p (expected, actual))
2283 {
2284 error_at (location, "passing %qT to argument %d of %qE, which"
2285 " expects %qT", actual, argno + 1, fndecl, expected);
2286 return false;
2287 }
2288 return true;
2289 }
2290
2291 /* Like require_vector_type, but TYPE is inferred from argument FIRST_ARGNO
2292 rather than being a fixed part of the function signature. This changes
2293 the nature of the error messages. */
2294 bool
2295 function_resolver::require_matching_vector_type (unsigned int argno,
2296 unsigned int first_argno,
2297 sve_type type)
2298 {
2299 sve_type new_type = infer_sve_type (argno);
2300 if (!new_type)
2301 return false;
2302
2303 if (type.num_vectors != new_type.num_vectors)
2304 {
2305 report_mismatched_num_vectors (first_argno, type, argno, new_type);
2306 return false;
2307 }
2308
2309 if (type != new_type)
2310 {
2311 error_at (location, "passing %qT to argument %d of %qE, but"
2312 " argument %d had type %qT",
2313 get_vector_type (new_type), argno + 1, fndecl,
2314 first_argno + 1, get_vector_type (type));
2315 return false;
2316 }
2317 return true;
2318 }
2319
2320 /* Require argument ARGNO to be a vector or tuple type with the following
2321 properties:
2322
2323 - the type class must be the same as FIRST_TYPE's if EXPECTED_TCLASS
2324 is SAME_TYPE_CLASS, otherwise it must be EXPECTED_TCLASS itself.
2325
2326 - the element size must be:
2327
2328 - the same as FIRST_TYPE's if EXPECTED_BITS == SAME_SIZE
2329 - half of FIRST_TYPE's if EXPECTED_BITS == HALF_SIZE
2330 - a quarter of FIRST_TYPE's if EXPECTED_BITS == QUARTER_SIZE
2331 - EXPECTED_BITS itself otherwise
2332
2333 - the number of vectors must be the same as FIRST_TYPE's if
2334 EXPECTED_NUM_VECTORS is zero, otherwise it must be EXPECTED_NUM_VECTORS.
2335
2336 Return true if the argument has the required type, otherwise report
2337 an appropriate error.
2338
2339 FIRST_ARGNO is the first argument that is known to have type FIRST_TYPE.
2340 Usually it comes before ARGNO, but sometimes it is more natural to resolve
2341 arguments out of order.
2342
2343 If the required properties depend on FIRST_TYPE then both FIRST_ARGNO and
2344 ARGNO contribute to the resolution process. If the required properties
2345 are fixed, only FIRST_ARGNO contributes to the resolution process.
2346
2347 This function is a bit of a Swiss army knife. The complication comes
2348 from trying to give good error messages when FIRST_ARGNO and ARGNO are
2349 inconsistent, since either of them might be wrong. */
2350 bool function_resolver::
2351 require_derived_vector_type (unsigned int argno,
2352 unsigned int first_argno,
2353 sve_type first_type,
2354 type_class_index expected_tclass,
2355 unsigned int expected_bits,
2356 unsigned int expected_num_vectors)
2357 {
2358 /* If the type needs to match FIRST_ARGNO exactly, use the preferred
2359 error message for that case. */
2360 if (expected_tclass == SAME_TYPE_CLASS
2361 && expected_bits == SAME_SIZE
2362 && expected_num_vectors == 0)
2363 {
2364 /* There's no need to resolve this case out of order. */
2365 gcc_assert (argno > first_argno);
2366 return require_matching_vector_type (argno, first_argno, first_type);
2367 }
2368
2369 /* Use FIRST_TYPE to get the expected type class and element size. */
2370 auto &first_type_suffix = type_suffixes[first_type.type];
2371 type_class_index orig_expected_tclass = expected_tclass;
2372 if (expected_tclass == NUM_TYPE_CLASSES)
2373 expected_tclass = first_type_suffix.tclass;
2374
2375 unsigned int orig_expected_bits = expected_bits;
2376 if (expected_bits == SAME_SIZE)
2377 expected_bits = first_type_suffix.element_bits;
2378 else if (expected_bits == HALF_SIZE)
2379 expected_bits = first_type_suffix.element_bits / 2;
2380 else if (expected_bits == QUARTER_SIZE)
2381 expected_bits = first_type_suffix.element_bits / 4;
2382
2383 unsigned int orig_expected_num_vectors = expected_num_vectors;
2384 if (expected_num_vectors == 0)
2385 expected_num_vectors = first_type.num_vectors;
2386
2387 /* If the expected type doesn't depend on FIRST_TYPE at all,
2388 just check for the fixed choice of vector type. */
2389 if (expected_tclass == orig_expected_tclass
2390 && expected_bits == orig_expected_bits
2391 && orig_expected_num_vectors == 1)
2392 {
2393 const type_suffix_info &expected_suffix
2394 = type_suffixes[find_type_suffix (expected_tclass, expected_bits)];
2395 return require_vector_type (argno, expected_suffix.vector_type);
2396 }
2397
2398 /* Require the argument to be some form of SVE vector type,
2399 without being specific about the type of vector we want. */
2400 sve_type actual_type = infer_sve_type (argno);
2401 if (!actual_type)
2402 return false;
2403
2404 if (actual_type.num_vectors != expected_num_vectors)
2405 {
2406 if (orig_expected_num_vectors == 0)
2407 report_mismatched_num_vectors (first_argno, first_type,
2408 argno, actual_type);
2409 else
2410 report_incorrect_num_vectors (argno, actual_type,
2411 expected_num_vectors);
2412 return false;
2413 }
2414
2415 if (orig_expected_tclass == SAME_TYPE_CLASS
2416 && orig_expected_bits == SAME_SIZE)
2417 {
2418 if (actual_type.type == first_type.type)
2419 return true;
2420
2421 if (first_type.num_vectors > 1)
2422 error_at (location, "passing %qT to argument %d of %qE, but"
2423 " argument %d was a tuple of %qT",
2424 get_vector_type (actual_type), argno + 1, fndecl,
2425 first_argno + 1, get_vector_type (first_type.type));
2426 else
2427 error_at (location, "passing %qT to argument %d of %qE, but"
2428 " argument %d had type %qT",
2429 get_vector_type (actual_type), argno + 1, fndecl,
2430 first_argno + 1, get_vector_type (first_type));
2431 return false;
2432 }
2433
2434 /* Exit now if we got the right type. */
2435 auto &actual_type_suffix = type_suffixes[actual_type.type];
2436 bool tclass_ok_p = (actual_type_suffix.tclass == expected_tclass);
2437 bool size_ok_p = (actual_type_suffix.element_bits == expected_bits);
2438 if (tclass_ok_p && size_ok_p)
2439 return true;
2440
2441 /* First look for cases in which the actual type contravenes a fixed
2442 size requirement, without having to refer to FIRST_TYPE. */
2443 if (!size_ok_p && expected_bits == orig_expected_bits)
2444 {
2445 if (expected_num_vectors == 1)
2446 error_at (location, "passing %qT to argument %d of %qE, which"
2447 " expects a vector of %d-bit elements",
2448 get_vector_type (actual_type), argno + 1, fndecl,
2449 expected_bits);
2450 else
2451 error_at (location, "passing %qT to argument %d of %qE, which"
2452 " expects vectors of %d-bit elements",
2453 get_vector_type (actual_type), argno + 1, fndecl,
2454 expected_bits);
2455 return false;
2456 }
2457
2458 /* Likewise for a fixed type class requirement. This is only ever
2459 needed for signed and unsigned types, so don't create unnecessary
2460 translation work for other type classes. */
2461 if (!tclass_ok_p && orig_expected_tclass == TYPE_signed)
2462 {
2463 if (expected_num_vectors == 1)
2464 error_at (location, "passing %qT to argument %d of %qE, which"
2465 " expects a vector of signed integers",
2466 get_vector_type (actual_type), argno + 1, fndecl);
2467 else
2468 /* Translation note: could also be written "expects a tuple of
2469 signed integer vectors". */
2470 error_at (location, "passing %qT to argument %d of %qE, which"
2471 " expects vectors of signed integers",
2472 get_vector_type (actual_type), argno + 1, fndecl);
2473 return false;
2474 }
2475 if (!tclass_ok_p && orig_expected_tclass == TYPE_unsigned)
2476 {
2477 if (expected_num_vectors == 1)
2478 error_at (location, "passing %qT to argument %d of %qE, which"
2479 " expects a vector of unsigned integers",
2480 get_vector_type (actual_type), argno + 1, fndecl);
2481 else
2482 /* Translation note: could also be written "expects a tuple of
2483 unsigned integer vectors". */
2484 error_at (location, "passing %qT to argument %d of %qE, which"
2485 " expects vectors of unsigned integers",
2486 get_vector_type (actual_type), argno + 1, fndecl);
2487 return false;
2488 }
2489
2490 /* Make sure that FIRST_TYPE itself is sensible before using it
2491 as a basis for an error message. */
2492 if (resolve_to (mode_suffix_id, first_type) == error_mark_node)
2493 return false;
2494
2495 /* If the arguments have consistent type classes, but a link between
2496 the sizes has been broken, try to describe the error in those terms. */
2497 if (tclass_ok_p && orig_expected_bits == SAME_SIZE)
2498 {
2499 if (argno < first_argno)
2500 {
2501 std::swap (argno, first_argno);
2502 std::swap (actual_type, first_type);
2503 }
2504 error_at (location, "arguments %d and %d of %qE must have the"
2505 " same element size, but the values passed here have type"
2506 " %qT and %qT respectively", first_argno + 1, argno + 1,
2507 fndecl, get_vector_type (first_type),
2508 get_vector_type (actual_type));
2509 return false;
2510 }
2511
2512 /* Likewise in reverse: look for cases in which the sizes are consistent
2513 but a link between the type classes has been broken. */
2514 if (size_ok_p
2515 && orig_expected_tclass == SAME_TYPE_CLASS
2516 && first_type_suffix.integer_p
2517 && actual_type_suffix.integer_p)
2518 {
2519 if (argno < first_argno)
2520 {
2521 std::swap (argno, first_argno);
2522 std::swap (actual_type, first_type);
2523 }
2524 error_at (location, "arguments %d and %d of %qE must have the"
2525 " same signedness, but the values passed here have type"
2526 " %qT and %qT respectively", first_argno + 1, argno + 1,
2527 fndecl, get_vector_type (first_type),
2528 get_vector_type (actual_type));
2529 return false;
2530 }
2531
2532 /* The two arguments are wildly inconsistent. */
2533 type_suffix_index expected_type
2534 = find_type_suffix (expected_tclass, expected_bits);
2535 error_at (location, "passing %qT instead of the expected %qT to argument"
2536 " %d of %qE, after passing %qT to argument %d",
2537 get_vector_type (actual_type), get_vector_type (expected_type),
2538 argno + 1, fndecl, get_argument_type (first_argno),
2539 first_argno + 1);
2540 return false;
2541 }
2542
2543 /* Require argument ARGNO to match argument FIRST_ARGNO, which was inferred
2544 to be a pointer to a scalar element of type TYPE. */
2545 bool
2546 function_resolver::require_matching_pointer_type (unsigned int argno,
2547 unsigned int first_argno,
2548 type_suffix_index type)
2549 {
2550 type_suffix_index new_type = infer_pointer_type (argno);
2551 if (new_type == NUM_TYPE_SUFFIXES)
2552 return false;
2553
2554 if (type != new_type)
2555 {
2556 error_at (location, "passing %qT to argument %d of %qE, but"
2557 " argument %d had type %qT", get_argument_type (argno),
2558 argno + 1, fndecl, first_argno + 1,
2559 get_argument_type (first_argno));
2560 return false;
2561 }
2562 return true;
2563 }
2564
2565 /* Require argument ARGNO to be a (possibly variable) scalar, using EXPECTED
2566 as the name of its expected type. Return true if the argument has the
2567 right form, otherwise report an appropriate error. */
2568 bool
2569 function_resolver::require_scalar_type (unsigned int argno,
2570 const char *expected)
2571 {
2572 if (!scalar_argument_p (argno))
2573 {
2574 if (expected)
2575 error_at (location, "passing %qT to argument %d of %qE, which"
2576 " expects %qs", get_argument_type (argno), argno + 1,
2577 fndecl, expected);
2578 return false;
2579 }
2580 return true;
2581 }
2582
2583 /* Require argument ARGNO to be a nonscalar type, given that it has already
2584 passed require_vector_or_scalar_type. Return true if it is, otherwise
2585 report an error. This is used when two sets of instructions share the
2586 same overloaded function and one accepts scalars while the other
2587 doesn't. */
2588 bool
2589 function_resolver::require_nonscalar_type (unsigned int argno)
2590 {
2591 if (scalar_argument_p (argno))
2592 {
2593 error_at (location, "passing %qT to argument %d of %qE, which"
2594 " does not accept scalars for this combination of arguments",
2595 get_argument_type (argno), argno + 1, fndecl);
2596 return false;
2597 }
2598 return true;
2599 }
2600
2601 /* Require argument ARGNO to be some form of pointer, without being specific
2602 about its target type. Return true if the argument has the right form,
2603 otherwise report an appropriate error. */
2604 bool
2605 function_resolver::require_pointer_type (unsigned int argno)
2606 {
2607 if (!scalar_argument_p (argno))
2608 {
2609 error_at (location, "passing %qT to argument %d of %qE, which"
2610 " expects a scalar pointer", get_argument_type (argno),
2611 argno + 1, fndecl);
2612 return false;
2613 }
2614 return true;
2615 }
2616
2617 /* Argument FIRST_ARGNO is a scalar with type EXPECTED_TYPE, and argument
2618 ARGNO should be consistent with it. Return true if it is, otherwise
2619 report an appropriate error. */
2620 bool function_resolver::
2621 require_matching_integer_scalar_type (unsigned int argno,
2622 unsigned int first_argno,
2623 type_suffix_index expected_type)
2624 {
2625 type_suffix_index actual_type = infer_integer_scalar_type (argno);
2626 if (actual_type == NUM_TYPE_SUFFIXES)
2627 return false;
2628
2629 if (actual_type == expected_type)
2630 return true;
2631
2632 error_at (location, "call to %qE is ambiguous; argument %d has type"
2633 " %qs but argument %d has type %qs", fndecl,
2634 first_argno + 1, get_scalar_type_name (expected_type),
2635 argno + 1, get_scalar_type_name (actual_type));
2636 return false;
2637 }
2638
2639 /* Require argument ARGNO to be a (possibly variable) scalar, expecting it
2640 to have the following properties:
2641
2642 - the type class must be the same as for type suffix 0 if EXPECTED_TCLASS
2643 is SAME_TYPE_CLASS, otherwise it must be EXPECTED_TCLASS itself.
2644
2645 - the element size must be the same as for type suffix 0 if EXPECTED_BITS
2646 is SAME_TYPE_SIZE, otherwise it must be EXPECTED_BITS itself.
2647
2648 Return true if the argument is valid, otherwise report an appropriate error.
2649
2650 Note that we don't check whether the scalar type actually has the required
2651 properties, since that's subject to implicit promotions and conversions.
2652 Instead we just use the expected properties to tune the error message. */
2653 bool function_resolver::
2654 require_derived_scalar_type (unsigned int argno,
2655 type_class_index expected_tclass,
2656 unsigned int expected_bits)
2657 {
2658 gcc_assert (expected_tclass == SAME_TYPE_CLASS
2659 || expected_tclass == TYPE_signed
2660 || expected_tclass == TYPE_unsigned);
2661
2662 /* If the expected type doesn't depend on the type suffix at all,
2663 just check for the fixed choice of scalar type. */
2664 if (expected_tclass != SAME_TYPE_CLASS && expected_bits != SAME_SIZE)
2665 {
2666 type_suffix_index expected_type
2667 = find_type_suffix (expected_tclass, expected_bits);
2668 return require_scalar_type (argno, get_scalar_type_name (expected_type));
2669 }
2670
2671 if (scalar_argument_p (argno))
2672 return true;
2673
2674 if (expected_tclass == SAME_TYPE_CLASS)
2675 /* It doesn't really matter whether the element is expected to be
2676 the same size as type suffix 0. */
2677 error_at (location, "passing %qT to argument %d of %qE, which"
2678 " expects a scalar element", get_argument_type (argno),
2679 argno + 1, fndecl);
2680 else
2681 /* It doesn't seem useful to distinguish between signed and unsigned
2682 scalars here. */
2683 error_at (location, "passing %qT to argument %d of %qE, which"
2684 " expects a scalar integer", get_argument_type (argno),
2685 argno + 1, fndecl);
2686 return false;
2687 }
2688
2689 /* Require argument ARGNO to be suitable for an integer constant expression.
2690 Return true if it is, otherwise report an appropriate error.
2691
2692 function_checker checks whether the argument is actually constant and
2693 has a suitable range. The reason for distinguishing immediate arguments
2694 here is because it provides more consistent error messages than
2695 require_scalar_type would. */
2696 bool
2697 function_resolver::require_integer_immediate (unsigned int argno)
2698 {
2699 if (!scalar_argument_p (argno))
2700 {
2701 report_non_ice (location, fndecl, argno);
2702 return false;
2703 }
2704 return true;
2705 }
2706
2707 /* Require argument ARGNO to be a vector base in a gather-style address.
2708 Return its type on success, otherwise return NUM_VECTOR_TYPES. */
2709 vector_type_index
2710 function_resolver::infer_vector_base_type (unsigned int argno)
2711 {
2712 type_suffix_index type = infer_vector_type (argno);
2713 if (type == NUM_TYPE_SUFFIXES)
2714 return NUM_VECTOR_TYPES;
2715
2716 if (type == TYPE_SUFFIX_u32 || type == TYPE_SUFFIX_u64)
2717 return type_suffixes[type].vector_type;
2718
2719 error_at (location, "passing %qT to argument %d of %qE, which"
2720 " expects %qs or %qs", get_argument_type (argno),
2721 argno + 1, fndecl, "svuint32_t", "svuint64_t");
2722 return NUM_VECTOR_TYPES;
2723 }
2724
2725 /* Require argument ARGNO to be a vector displacement in a gather-style
2726 address. Return its type on success, otherwise return NUM_VECTOR_TYPES. */
2727 vector_type_index
2728 function_resolver::infer_vector_displacement_type (unsigned int argno)
2729 {
2730 type_suffix_index type = infer_integer_vector_type (argno);
2731 if (type == NUM_TYPE_SUFFIXES)
2732 return NUM_VECTOR_TYPES;
2733
2734 if (type_suffixes[type].integer_p
2735 && (type_suffixes[type].element_bits == 32
2736 || type_suffixes[type].element_bits == 64))
2737 return type_suffixes[type].vector_type;
2738
2739 error_at (location, "passing %qT to argument %d of %qE, which"
2740 " expects a vector of 32-bit or 64-bit integers",
2741 get_argument_type (argno), argno + 1, fndecl);
2742 return NUM_VECTOR_TYPES;
2743 }
2744
2745 /* Require argument ARGNO to be a vector displacement in a gather-style
2746 address. There are three possible uses:
2747
2748 - for loading into elements of type TYPE (when LOAD_P is true)
2749 - for storing from elements of type TYPE (when LOAD_P is false)
2750 - for prefetching data (when TYPE is NUM_TYPE_SUFFIXES)
2751
2752 The overloaded function's mode suffix determines the units of the
2753 displacement (bytes for "_offset", elements for "_index").
2754
2755 Return the associated mode on success, otherwise report an error
2756 and return MODE_none. */
2757 mode_suffix_index
2758 function_resolver::resolve_sv_displacement (unsigned int argno,
2759 type_suffix_index type,
2760 bool load_p)
2761 {
2762 if (type == NUM_TYPE_SUFFIXES)
2763 {
2764 /* For prefetches, the base is a void pointer and the displacement
2765 can be any valid offset or index type. */
2766 vector_type_index displacement_vector_type
2767 = infer_vector_displacement_type (argno);
2768 if (displacement_vector_type == NUM_VECTOR_TYPES)
2769 return MODE_none;
2770
2771 mode_suffix_index mode = find_mode_suffix (NUM_VECTOR_TYPES,
2772 displacement_vector_type,
2773 displacement_units ());
2774 gcc_assert (mode != MODE_none);
2775 return mode;
2776 }
2777
2778 auto base_type = shape->vector_base_type (type);
2779 unsigned int required_bits = type_suffixes[base_type].element_bits;
2780 if (required_bits == 32
2781 && displacement_units () == UNITS_elements
2782 && !lookup_form (MODE_s32index, type)
2783 && !lookup_form (MODE_u32index, type))
2784 {
2785 if (lookup_form (MODE_u32base_index, type))
2786 {
2787 if (type_suffix_ids[0] == NUM_TYPE_SUFFIXES)
2788 {
2789 gcc_assert (!load_p);
2790 error_at (location, "when storing %qT, %qE requires a vector"
2791 " base and a scalar index", get_vector_type (type),
2792 fndecl);
2793 }
2794 else
2795 error_at (location, "%qE requires a vector base and a scalar"
2796 " index", fndecl);
2797 }
2798 else
2799 error_at (location, "%qE does not support 32-bit vector type %qT",
2800 fndecl, get_vector_type (type));
2801 return MODE_none;
2802 }
2803
2804 /* Check for some form of vector type, without naming any in particular
2805 as being expected. */
2806 type_suffix_index displacement_type = infer_vector_type (argno);
2807 if (displacement_type == NUM_TYPE_SUFFIXES)
2808 return MODE_none;
2809
2810 /* If the displacement type is consistent with the data vector type,
2811 try to find the associated mode suffix. This will fall through
2812 for non-integral displacement types. */
2813 if (type_suffixes[displacement_type].element_bits == required_bits)
2814 {
2815 vector_type_index displacement_vector_type
2816 = type_suffixes[displacement_type].vector_type;
2817 mode_suffix_index mode = find_mode_suffix (NUM_VECTOR_TYPES,
2818 displacement_vector_type,
2819 displacement_units ());
2820 if (mode != MODE_none)
2821 {
2822 if (mode == MODE_s32offset
2823 && !lookup_form (mode, type)
2824 && lookup_form (MODE_u32offset, type))
2825 {
2826 if (type_suffix_ids[0] == NUM_TYPE_SUFFIXES)
2827 error_at (location, "%qE does not support 32-bit sign-extended"
2828 " offsets", fndecl);
2829 else
2830 error_at (location, "%qE does not support sign-extended"
2831 " offsets", fndecl);
2832 return MODE_none;
2833 }
2834 return mode;
2835 }
2836 }
2837
2838 if (type_suffix_ids[0] == NUM_TYPE_SUFFIXES
2839 && shape->vector_base_type (TYPE_SUFFIX_u32) == TYPE_SUFFIX_u32)
2840 {
2841 /* TYPE has been inferred rather than specified by the user,
2842 so mention it in the error messages. */
2843 if (load_p)
2844 error_at (location, "passing %qT to argument %d of %qE, which when"
2845 " loading %qT expects a vector of %d-bit integers",
2846 get_argument_type (argno), argno + 1, fndecl,
2847 get_vector_type (type), required_bits);
2848 else
2849 error_at (location, "passing %qT to argument %d of %qE, which when"
2850 " storing %qT expects a vector of %d-bit integers",
2851 get_argument_type (argno), argno + 1, fndecl,
2852 get_vector_type (type), required_bits);
2853 }
2854 else
2855 /* TYPE is part of the function name. */
2856 error_at (location, "passing %qT to argument %d of %qE, which"
2857 " expects a vector of %d-bit integers",
2858 get_argument_type (argno), argno + 1, fndecl, required_bits);
2859 return MODE_none;
2860 }
2861
2862 /* Require the arguments starting at ARGNO to form a gather-style address.
2863 There are three possible uses:
2864
2865 - for loading into elements of type TYPE (when LOAD_P is true)
2866 - for storing from elements of type TYPE (when LOAD_P is false)
2867 - for prefetching data (when TYPE is NUM_TYPE_SUFFIXES)
2868
2869 The three possible addresses are:
2870
2871 - a vector base with no displacement
2872 - a vector base and a scalar displacement
2873 - a scalar (pointer) base and a vector displacement
2874
2875 The overloaded function's mode suffix determines whether there is
2876 a displacement, and if so, what units it uses:
2877
2878 - MODE_none: no displacement
2879 - MODE_offset: the displacement is measured in bytes
2880 - MODE_index: the displacement is measured in elements
2881
2882 Return the mode of the non-overloaded function on success, otherwise
2883 report an error and return MODE_none. */
2884 mode_suffix_index
2885 function_resolver::resolve_gather_address (unsigned int argno,
2886 type_suffix_index type,
2887 bool load_p)
2888 {
2889 tree actual = get_argument_type (argno);
2890 if (actual == error_mark_node)
2891 return MODE_none;
2892
2893 if (displacement_units () != UNITS_none)
2894 {
2895 /* Some form of displacement is needed. First handle a scalar
2896 pointer base and a vector displacement. */
2897 if (scalar_argument_p (argno))
2898 /* Don't check the pointer type here, since there's only one valid
2899 choice. Leave that to the frontend. */
2900 return resolve_sv_displacement (argno + 1, type, load_p);
2901
2902 if (!VECTOR_TYPE_P (actual))
2903 {
2904 error_at (location, "passing %qT to argument %d of %qE,"
2905 " which expects a vector or pointer base address",
2906 actual, argno + 1, fndecl);
2907 return MODE_none;
2908 }
2909 }
2910
2911 /* Check for the correct choice of vector base type. */
2912 vector_type_index base_vector_type;
2913 if (type == NUM_TYPE_SUFFIXES)
2914 {
2915 /* Since prefetches have no type suffix, there is a free choice
2916 between 32-bit and 64-bit base addresses. */
2917 base_vector_type = infer_vector_base_type (argno);
2918 if (base_vector_type == NUM_VECTOR_TYPES)
2919 return MODE_none;
2920 }
2921 else
2922 {
2923 /* Check for some form of vector type, without saying which type
2924 we expect. */
2925 type_suffix_index base_type = infer_vector_type (argno);
2926 if (base_type == NUM_TYPE_SUFFIXES)
2927 return MODE_none;
2928
2929 /* Check whether the type is the right one. */
2930 auto required_type = shape->vector_base_type (type);
2931 if (required_type != base_type)
2932 {
2933 error_at (location, "passing %qT to argument %d of %qE,"
2934 " which expects %qT", actual, argno + 1, fndecl,
2935 get_vector_type (required_type));
2936 return MODE_none;
2937 }
2938 base_vector_type = type_suffixes[base_type].vector_type;
2939 }
2940
2941 /* Check the scalar displacement, if any. */
2942 if (displacement_units () != UNITS_none
2943 && !require_scalar_type (argno + 1, "int64_t"))
2944 return MODE_none;
2945
2946 /* Find the appropriate mode suffix. The checks above should have
2947 weeded out all erroneous cases. */
2948 for (unsigned int mode_i = 0; mode_i < ARRAY_SIZE (mode_suffixes); ++mode_i)
2949 {
2950 const mode_suffix_info &mode = mode_suffixes[mode_i];
2951 if (mode.base_vector_type == base_vector_type
2952 && mode.displacement_vector_type == NUM_VECTOR_TYPES
2953 && mode.displacement_units == displacement_units ())
2954 return mode_suffix_index (mode_i);
2955 }
2956
2957 gcc_unreachable ();
2958 }
2959
2960 /* Require arguments ARGNO and ARGNO + 1 to form an ADR-style address,
2961 i.e. one with a vector of base addresses and a vector of displacements.
2962 The overloaded function's mode suffix determines the units of the
2963 displacement (bytes for "_offset", elements for "_index").
2964
2965 Return the associated mode suffix on success, otherwise report
2966 an error and return MODE_none. */
2967 mode_suffix_index
2968 function_resolver::resolve_adr_address (unsigned int argno)
2969 {
2970 vector_type_index base_type = infer_vector_base_type (argno);
2971 if (base_type == NUM_VECTOR_TYPES)
2972 return MODE_none;
2973
2974 vector_type_index displacement_type
2975 = infer_vector_displacement_type (argno + 1);
2976 if (displacement_type == NUM_VECTOR_TYPES)
2977 return MODE_none;
2978
2979 mode_suffix_index mode = find_mode_suffix (base_type, displacement_type,
2980 displacement_units ());
2981 if (mode == MODE_none)
2982 {
2983 if (mode_suffix_id == MODE_offset)
2984 error_at (location, "cannot combine a base of type %qT with"
2985 " an offset of type %qT",
2986 get_argument_type (argno), get_argument_type (argno + 1));
2987 else
2988 error_at (location, "cannot combine a base of type %qT with"
2989 " an index of type %qT",
2990 get_argument_type (argno), get_argument_type (argno + 1));
2991 }
2992 return mode;
2993 }
2994
2995 /* Require the function to have exactly EXPECTED arguments. Return true
2996 if it does, otherwise report an appropriate error. */
2997 bool
2998 function_resolver::check_num_arguments (unsigned int expected)
2999 {
3000 if (m_arglist.length () < expected)
3001 error_at (location, "too few arguments to function %qE", fndecl);
3002 else if (m_arglist.length () > expected)
3003 error_at (location, "too many arguments to function %qE", fndecl);
3004 return m_arglist.length () == expected;
3005 }
3006
3007 /* If the function is predicated, check that the first argument is a
3008 suitable governing predicate. Also check that there are NOPS further
3009 arguments after any governing predicate, but don't check what they are.
3010
3011 Return true on success, otherwise report a suitable error.
3012 When returning true:
3013
3014 - set I to the number of the first unchecked argument.
3015 - set NARGS to the total number of arguments. */
3016 bool
3017 function_resolver::check_gp_argument (unsigned int nops,
3018 unsigned int &i, unsigned int &nargs)
3019 {
3020 gcc_assert (pred != PRED_za_m);
3021 i = 0;
3022 unsigned int nfpm_args = (fpm_mode == FPM_set)? 1:0;
3023 if (pred != PRED_none)
3024 {
3025 /* Unary merge operations should use resolve_unary instead. */
3026 gcc_assert (!shape->has_merge_argument_p (*this, nops));
3027 nargs = nops + nfpm_args + 1;
3028 if (!check_num_arguments (nargs)
3029 || !require_vector_type (i, gp_type_index ()))
3030 return false;
3031 i += 1;
3032 }
3033 else
3034 {
3035 nargs = nops + nfpm_args;
3036 if (!check_num_arguments (nargs))
3037 return false;
3038 }
3039
3040 return true;
3041 }
3042
3043 /* Finish resolving a function whose final argument can be a vector
3044 or a scalar, with the function having an implicit "_n" suffix
3045 in the latter case. This "_n" form might only exist for certain
3046 type suffixes.
3047
3048 ARGNO is the index of the final argument. The inferred type suffix
3049 was obtained from argument FIRST_ARGNO, which has type FIRST_TYPE.
3050 EXPECTED_TCLASS and EXPECTED_BITS describe the expected properties
3051 of the final vector or scalar argument, in the same way as for
3052 require_derived_vector_type. INFERRED_TYPE is the inferred type
3053 suffix itself, or NUM_TYPE_SUFFIXES if it's the same as FIRST_TYPE.
3054
3055 Return the function decl of the resolved function on success,
3056 otherwise report a suitable error and return error_mark_node. */
3057 tree function_resolver::
3058 finish_opt_n_resolution (unsigned int argno, unsigned int first_argno,
3059 type_suffix_index first_type,
3060 type_class_index expected_tclass,
3061 unsigned int expected_bits,
3062 type_suffix_index inferred_type)
3063 {
3064 if (inferred_type == NUM_TYPE_SUFFIXES)
3065 inferred_type = first_type;
3066 tree scalar_form = lookup_form (MODE_n, inferred_type);
3067
3068 /* Allow the final argument to be scalar, if an _n form exists. */
3069 if (scalar_argument_p (argno))
3070 {
3071 if (scalar_form)
3072 return scalar_form;
3073
3074 /* Check the vector form normally. If that succeeds, raise an
3075 error about having no corresponding _n form. */
3076 tree res = resolve_to (mode_suffix_id, inferred_type);
3077 if (res != error_mark_node)
3078 error_at (location, "passing %qT to argument %d of %qE, but its"
3079 " %qT form does not accept scalars",
3080 get_argument_type (argno), argno + 1, fndecl,
3081 get_vector_type (first_type));
3082 return error_mark_node;
3083 }
3084
3085 /* If an _n form does exist, provide a more accurate message than
3086 require_derived_vector_type would for arguments that are neither
3087 vectors nor scalars. */
3088 if (scalar_form && !require_vector_or_scalar_type (argno))
3089 return error_mark_node;
3090
3091 /* Check for the correct vector type. */
3092 if (!require_derived_vector_type (argno, first_argno, first_type,
3093 expected_tclass, expected_bits))
3094 return error_mark_node;
3095
3096 return resolve_to (mode_suffix_id, inferred_type);
3097 }
3098
3099 /* Finish resolving a function whose final argument can be a tuple
3100 or a vector, with the function having an implicit "_single" suffix
3101 in the latter case. This "_single" form might only exist for certain
3102 type suffixes.
3103
3104 ARGNO is the index of the final argument. The inferred type suffix
3105 was obtained from argument FIRST_ARGNO, which has type FIRST_TYPE.
3106 EXPECTED_TCLASS gives the expected type class for the final tuple
3107 or vector.
3108
3109 Return the function decl of the resolved function on success,
3110 otherwise report a suitable error and return error_mark_node. */
3111 tree function_resolver::
3112 finish_opt_single_resolution (unsigned int argno, unsigned int first_argno,
3113 sve_type first_type,
3114 type_class_index expected_tclass)
3115 {
3116 sve_type new_type = infer_sve_type (argno);
3117 if (!new_type)
3118 return error_mark_node;
3119
3120 /* If the type is a tuple, require it to match the group suffix. */
3121 unsigned int num_vectors = vectors_per_tuple ();
3122 if (num_vectors != 1
3123 && new_type.num_vectors != 1
3124 && new_type.num_vectors != num_vectors)
3125 {
3126 report_incorrect_num_vectors (argno, new_type, num_vectors);
3127 return error_mark_node;
3128 }
3129
3130 auto expected_num_vectors = (new_type.num_vectors == 1 ? 1 : 0);
3131 if (!require_derived_vector_type (argno, first_argno, first_type,
3132 expected_tclass, SAME_SIZE,
3133 expected_num_vectors))
3134 return error_mark_node;
3135
3136 if (new_type.num_vectors == 1 && first_type.num_vectors > 1)
3137 {
3138 if (tree single_form = lookup_form (MODE_single, first_type))
3139 return single_form;
3140
3141 if (resolve_to (mode_suffix_id, first_type) != error_mark_node)
3142 error_at (location, "passing %qT to argument %d of %qE, but its"
3143 " %qT form does not accept single vectors",
3144 get_vector_type (new_type), argno + 1, fndecl,
3145 get_vector_type (first_type));
3146 return error_mark_node;
3147 }
3148 return resolve_to (mode_suffix_id, first_type);
3149 }
3150
3151 /* Resolve a (possibly predicated) unary function. If the function uses
3152 merge predication or if TREAT_AS_MERGE_P is true, there is an extra
3153 vector argument before the governing predicate that specifies the
3154 values of inactive elements. This argument has the following
3155 properties:
3156
3157 - the type class must be the same as for active elements if MERGE_TCLASS
3158 is SAME_TYPE_CLASS, otherwise it must be MERGE_TCLASS itself.
3159
3160 - the element size must be the same as for active elements if MERGE_BITS
3161 is SAME_TYPE_SIZE, otherwise it must be MERGE_BITS itself.
3162
3163 Return the function decl of the resolved function on success,
3164 otherwise report a suitable error and return error_mark_node. */
3165 tree
3166 function_resolver::resolve_unary (type_class_index merge_tclass,
3167 unsigned int merge_bits,
3168 bool treat_as_merge_p)
3169 {
3170 type_suffix_index type;
3171 if (pred == PRED_m || treat_as_merge_p)
3172 {
3173 if (!check_num_arguments (3))
3174 return error_mark_node;
3175 if (merge_tclass == SAME_TYPE_CLASS && merge_bits == SAME_SIZE)
3176 {
3177 /* The inactive elements are the same as the active elements,
3178 so we can use normal left-to-right resolution. */
3179 if ((type = infer_vector_type (0)) == NUM_TYPE_SUFFIXES
3180 || !require_vector_type (1, VECTOR_TYPE_svbool_t)
3181 || !require_matching_vector_type (2, 0, type))
3182 return error_mark_node;
3183 }
3184 else
3185 {
3186 /* The inactive element type is a function of the active one,
3187 so resolve the active one first. */
3188 if (!require_vector_type (1, VECTOR_TYPE_svbool_t)
3189 || (type = infer_vector_type (2)) == NUM_TYPE_SUFFIXES
3190 || !require_derived_vector_type (0, 2, type, merge_tclass,
3191 merge_bits))
3192 return error_mark_node;
3193 }
3194 }
3195 else
3196 {
3197 /* We just need to check the predicate (if any) and the single
3198 vector argument. */
3199 unsigned int i, nargs;
3200 if (!check_gp_argument (1, i, nargs)
3201 || (type = infer_vector_type (i)) == NUM_TYPE_SUFFIXES)
3202 return error_mark_node;
3203 }
3204
3205 /* Handle convert-like functions in which the first type suffix is
3206 explicit. */
3207 if (type_suffix_ids[0] != NUM_TYPE_SUFFIXES)
3208 return resolve_to (mode_suffix_id, type_suffix_ids[0], type);
3209
3210 return resolve_to (mode_suffix_id, type);
3211 }
3212
3213 /* Resolve a (possibly predicated) function that takes NOPS like-typed
3214 vector arguments followed by NIMM integer immediates. Return the
3215 function decl of the resolved function on success, otherwise report
3216 a suitable error and return error_mark_node. */
3217 tree
3218 function_resolver::resolve_uniform (unsigned int nops, unsigned int nimm)
3219 {
3220 unsigned int i, nargs;
3221 type_suffix_index type;
3222 if (!check_gp_argument (nops + nimm, i, nargs)
3223 || (type = infer_vector_type (i)) == NUM_TYPE_SUFFIXES)
3224 return error_mark_node;
3225
3226 unsigned int first_arg = i++;
3227 for (; i < nargs - nimm; ++i)
3228 if (!require_matching_vector_type (i, first_arg, type))
3229 return error_mark_node;
3230
3231 for (; i < nargs; ++i)
3232 if (!require_integer_immediate (i))
3233 return error_mark_node;
3234
3235 return resolve_to (mode_suffix_id, type);
3236 }
3237
3238 /* Resolve a (possibly predicated) function that offers a choice between
3239 taking:
3240
3241 - NOPS like-typed vector arguments or
3242 - NOPS - 1 like-typed vector arguments followed by a scalar argument
3243
3244 Return the function decl of the resolved function on success,
3245 otherwise report a suitable error and return error_mark_node. */
3246 tree
3247 function_resolver::resolve_uniform_opt_n (unsigned int nops)
3248 {
3249 unsigned int i, nargs;
3250 type_suffix_index type;
3251 if (!check_gp_argument (nops, i, nargs)
3252 || (type = infer_vector_type (i)) == NUM_TYPE_SUFFIXES)
3253 return error_mark_node;
3254
3255 unsigned int first_arg = i++;
3256 for (; i < nargs - 1; ++i)
3257 if (!require_matching_vector_type (i, first_arg, type))
3258 return error_mark_node;
3259
3260 return finish_opt_n_resolution (i, first_arg, type);
3261 }
3262
3263 /* If the call is erroneous, report an appropriate error and return
3264 error_mark_node. Otherwise, if the function is overloaded, return
3265 the decl of the non-overloaded function. Return NULL_TREE otherwise,
3266 indicating that the call should be processed in the normal way. */
3267 tree
3268 function_resolver::resolve ()
3269 {
3270 return shape->resolve (*this);
3271 }
3272
3273 function_checker::function_checker (location_t location,
3274 const function_instance &instance,
3275 tree fndecl, tree fntype,
3276 unsigned int nargs, tree *args)
3277 : function_call_info (location, instance, fndecl),
3278 m_fntype (fntype), m_nargs (nargs), m_args (args),
3279 m_base_arg (pred != PRED_none && pred != PRED_za_m ? 1 : 0)
3280 {
3281 }
3282
3283 /* Return true if argument ARGNO exists. which it might not for
3284 erroneous calls. It is safe to wave through checks if this
3285 function returns false. */
3286 bool
3287 function_checker::argument_exists_p (unsigned int argno)
3288 {
3289 gcc_assert (argno < (unsigned int) type_num_arguments (m_fntype));
3290 return argno < m_nargs;
3291 }
3292
3293 /* Check that argument ARGNO is an integer constant expression and
3294 store its value in VALUE_OUT if so. The caller should first
3295 check that argument ARGNO exists. */
3296 bool
3297 function_checker::require_immediate (unsigned int argno,
3298 HOST_WIDE_INT &value_out)
3299 {
3300 gcc_assert (argno < m_nargs);
3301 tree arg = m_args[argno];
3302
3303 /* The type and range are unsigned, so read the argument as an
3304 unsigned rather than signed HWI. */
3305 if (!tree_fits_uhwi_p (arg))
3306 {
3307 report_non_ice (location, fndecl, argno);
3308 return false;
3309 }
3310
3311 /* ...but treat VALUE_OUT as signed for error reporting, since printing
3312 -1 is more user-friendly than the maximum uint64_t value. */
3313 value_out = tree_to_uhwi (arg);
3314 return true;
3315 }
3316
3317 /* Check that argument REL_ARGNO is an integer constant expression that
3318 has the value VALUE0 or VALUE1. REL_ARGNO counts from the end of the
3319 predication arguments. */
3320 bool
3321 function_checker::require_immediate_either_or (unsigned int rel_argno,
3322 HOST_WIDE_INT value0,
3323 HOST_WIDE_INT value1)
3324 {
3325 unsigned int argno = m_base_arg + rel_argno;
3326 if (!argument_exists_p (argno))
3327 return true;
3328
3329 HOST_WIDE_INT actual;
3330 if (!require_immediate (argno, actual))
3331 return false;
3332
3333 if (actual != value0 && actual != value1)
3334 {
3335 report_neither_nor (location, fndecl, argno, actual, value0, value1);
3336 return false;
3337 }
3338
3339 return true;
3340 }
3341
3342 /* Check that argument REL_ARGNO is an integer constant expression that has
3343 a valid value for enumeration type TYPE. REL_ARGNO counts from the end
3344 of the predication arguments. */
3345 bool
3346 function_checker::require_immediate_enum (unsigned int rel_argno, tree type)
3347 {
3348 unsigned int argno = m_base_arg + rel_argno;
3349 if (!argument_exists_p (argno))
3350 return true;
3351
3352 HOST_WIDE_INT actual;
3353 if (!require_immediate (argno, actual))
3354 return false;
3355
3356 for (tree entry = TYPE_VALUES (type); entry; entry = TREE_CHAIN (entry))
3357 {
3358 /* The value is an INTEGER_CST for C and a CONST_DECL wrapper
3359 around an INTEGER_CST for C++. */
3360 tree value = TREE_VALUE (entry);
3361 if (TREE_CODE (value) == CONST_DECL)
3362 value = DECL_INITIAL (value);
3363 if (wi::to_widest (value) == actual)
3364 return true;
3365 }
3366
3367 report_not_enum (location, fndecl, argno, actual, type);
3368 return false;
3369 }
3370
3371 /* The intrinsic conceptually divides vector argument REL_VEC_ARGNO into
3372 groups of GROUP_SIZE elements. Return true if argument REL_ARGNO is
3373 a suitable constant index for selecting one of these groups. The
3374 selection happens within a 128-bit quadword, rather than the whole vector.
3375
3376 REL_ARGNO and REL_VEC_ARGNO count from the end of the predication
3377 arguments. */
3378 bool
3379 function_checker::require_immediate_lane_index (unsigned int rel_argno,
3380 unsigned int rel_vec_argno,
3381 unsigned int group_size)
3382 {
3383 unsigned int argno = m_base_arg + rel_argno;
3384 if (!argument_exists_p (argno))
3385 return true;
3386
3387 /* Get the type of the vector argument. tree_argument_type wants a
3388 1-based number, whereas VEC_ARGNO is 0-based. */
3389 unsigned int vec_argno = m_base_arg + rel_vec_argno;
3390 machine_mode mode = TYPE_MODE (type_argument_type (m_fntype, vec_argno + 1));
3391 gcc_assert (VECTOR_MODE_P (mode));
3392 unsigned int nlanes = 128 / (group_size * GET_MODE_UNIT_BITSIZE (mode));
3393 return require_immediate_range (rel_argno, 0, nlanes - 1);
3394 }
3395
3396 /* Check that argument REL_ARGNO is an integer constant expression that
3397 has one of the given values. */
3398 bool
3399 function_checker::require_immediate_one_of (unsigned int rel_argno,
3400 HOST_WIDE_INT value0,
3401 HOST_WIDE_INT value1,
3402 HOST_WIDE_INT value2,
3403 HOST_WIDE_INT value3)
3404 {
3405 unsigned int argno = m_base_arg + rel_argno;
3406 if (!argument_exists_p (argno))
3407 return true;
3408
3409 HOST_WIDE_INT actual;
3410 if (!require_immediate (argno, actual))
3411 return false;
3412
3413 if (actual != value0
3414 && actual != value1
3415 && actual != value2
3416 && actual != value3)
3417 {
3418 report_not_one_of (location, fndecl, argno, actual,
3419 value0, value1, value2, value3);
3420 return false;
3421 }
3422
3423 return true;
3424 }
3425
3426 /* Check that argument REL_ARGNO is an integer constant expression in the
3427 range [MIN, MAX]. REL_ARGNO counts from the end of the predication
3428 arguments. */
3429 bool
3430 function_checker::require_immediate_range (unsigned int rel_argno,
3431 HOST_WIDE_INT min,
3432 HOST_WIDE_INT max)
3433 {
3434 unsigned int argno = m_base_arg + rel_argno;
3435 if (!argument_exists_p (argno))
3436 return true;
3437
3438 /* Required because of the tree_to_uhwi -> HOST_WIDE_INT conversion
3439 in require_immediate. */
3440 gcc_assert (min >= 0 && min <= max);
3441 HOST_WIDE_INT actual;
3442 if (!require_immediate (argno, actual))
3443 return false;
3444
3445 if (!IN_RANGE (actual, min, max))
3446 {
3447 report_out_of_range (location, fndecl, argno, actual, min, max);
3448 return false;
3449 }
3450
3451 return true;
3452 }
3453
3454 /* Perform semantic checks on the call. Return true if the call is valid,
3455 otherwise report a suitable error. */
3456 bool
3457 function_checker::check ()
3458 {
3459 function_args_iterator iter;
3460 tree type;
3461 unsigned int i = 0;
3462 FOREACH_FUNCTION_ARGS (m_fntype, type, iter)
3463 {
3464 if (type == void_type_node || i >= m_nargs)
3465 break;
3466
3467 if (i >= m_base_arg
3468 && TREE_CODE (type) == ENUMERAL_TYPE
3469 && !require_immediate_enum (i - m_base_arg, type))
3470 return false;
3471
3472 i += 1;
3473 }
3474
3475 return shape->check (*this);
3476 }
3477
3478 /* Return true if V is a vector constant and if, for every in-range integer I,
3479 element STEP*I is equal to element 0. */
3480 bool
3481 vector_cst_all_same (tree v, unsigned int step)
3482 {
3483 if (TREE_CODE (v) != VECTOR_CST)
3484 return false;
3485
3486 /* VECTOR_CST_NELTS_PER_PATTERN applies to any multiple of
3487 VECTOR_CST_NPATTERNS. */
3488 unsigned int lcm = least_common_multiple (step, VECTOR_CST_NPATTERNS (v));
3489 unsigned int nelts = lcm * VECTOR_CST_NELTS_PER_PATTERN (v);
3490 tree first_el = VECTOR_CST_ENCODED_ELT (v, 0);
3491 for (unsigned int i = 0; i < nelts; i += step)
3492 if (!operand_equal_p (VECTOR_CST_ELT (v, i), first_el, 0))
3493 return false;
3494
3495 return true;
3496 }
3497
3498 /* Return true if V is a constant predicate that acts as a ptrue when
3499 predicating STEP-byte elements. */
3500 bool
3501 is_ptrue (tree v, unsigned int step)
3502 {
3503 return (TREE_CODE (v) == VECTOR_CST
3504 && TYPE_MODE (TREE_TYPE (v)) == VNx16BImode
3505 && integer_nonzerop (VECTOR_CST_ENCODED_ELT (v, 0))
3506 && vector_cst_all_same (v, step));
3507 }
3508
3509 /* Return true if V is a constant predicate that acts as a pfalse. */
3510 bool
3511 is_pfalse (tree v)
3512 {
3513 return (TREE_CODE (v) == VECTOR_CST
3514 && TYPE_MODE (TREE_TYPE (v)) == VNx16BImode
3515 && integer_zerop (v));
3516 }
3517
3518 gimple_folder::gimple_folder (const function_instance &instance, tree fndecl,
3519 gimple_stmt_iterator *gsi_in, gcall *call_in)
3520 : function_call_info (gimple_location (call_in), instance, fndecl),
3521 gsi (gsi_in), call (call_in), lhs (gimple_call_lhs (call_in))
3522 {
3523 }
3524
3525 /* VALUE might be a vector of type VECTYPE or a single scalar element.
3526 Duplicate it into a vector of type VECTYPE in the latter case, adding any
3527 new statements to STMTS. */
3528 tree
3529 gimple_folder::force_vector (gimple_seq &stmts, tree vectype, tree value)
3530 {
3531 if (!VECTOR_TYPE_P (TREE_TYPE (value)))
3532 value = gimple_build_vector_from_val (&stmts, vectype, value);
3533 return value;
3534 }
3535
3536 /* Convert predicate argument ARGNO so that it has the type appropriate for
3537 an operation on VECTYPE. Add any new statements to STMTS. */
3538 tree
3539 gimple_folder::convert_pred (gimple_seq &stmts, tree vectype,
3540 unsigned int argno)
3541 {
3542 tree pred = gimple_call_arg (call, argno);
3543 if (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (pred)),
3544 TYPE_VECTOR_SUBPARTS (vectype)))
3545 return pred;
3546
3547 return gimple_build (&stmts, VIEW_CONVERT_EXPR,
3548 truth_type_for (vectype), pred);
3549 }
3550
3551 /* Return a pointer to the address in a contiguous load or store,
3552 given that each memory vector has type VECTYPE. Add any new
3553 statements to STMTS. */
3554 tree
3555 gimple_folder::fold_contiguous_base (gimple_seq &stmts, tree vectype)
3556 {
3557 tree base = gimple_call_arg (call, 1);
3558 if (mode_suffix_id == MODE_vnum)
3559 {
3560 tree offset = gimple_call_arg (call, 2);
3561 offset = gimple_convert (&stmts, sizetype, offset);
3562 offset = gimple_build (&stmts, MULT_EXPR, sizetype, offset,
3563 TYPE_SIZE_UNIT (vectype));
3564 base = gimple_build (&stmts, POINTER_PLUS_EXPR, TREE_TYPE (base),
3565 base, offset);
3566 }
3567 return base;
3568 }
3569
3570 /* Return the alignment and TBAA argument to an internal load or store
3571 function like IFN_MASK_LOAD or IFN_MASK_STORE, given that it accesses
3572 memory elements of type TYPE. */
3573 tree
3574 gimple_folder::load_store_cookie (tree type)
3575 {
3576 return build_int_cst (build_pointer_type (type), TYPE_ALIGN (type));
3577 }
3578
3579 /* Fold the call to a call to INSTANCE, with the same arguments. */
3580 gcall *
3581 gimple_folder::redirect_call (const function_instance &instance)
3582 {
3583 registered_function *rfn
3584 = function_table->find_with_hash (instance, instance.hash ());
3585 if (!rfn)
3586 return NULL;
3587
3588 gimple_call_set_fndecl (call, rfn->decl);
3589 gimple_call_set_fntype (call, TREE_TYPE (rfn->decl));
3590 return call;
3591 }
3592
3593 /* Redirect _z and _m calls to _x functions if the predicate is all-true.
3594 This allows us to use unpredicated instructions, where available. */
3595 gimple *
3596 gimple_folder::redirect_pred_x ()
3597 {
3598 if (pred != PRED_z && pred != PRED_m)
3599 return nullptr;
3600
3601 if (gimple_call_num_args (call) < 2)
3602 return nullptr;
3603
3604 tree lhs_type = TREE_TYPE (TREE_TYPE (fndecl));
3605 tree arg0_type = type_argument_type (TREE_TYPE (fndecl), 1);
3606 tree arg1_type = type_argument_type (TREE_TYPE (fndecl), 2);
3607 if (!VECTOR_TYPE_P (lhs_type)
3608 || !VECTOR_TYPE_P (arg0_type)
3609 || !VECTOR_TYPE_P (arg1_type))
3610 return nullptr;
3611
3612 auto lhs_step = element_precision (lhs_type);
3613 auto rhs_step = element_precision (arg1_type);
3614 auto step = MAX (lhs_step, rhs_step);
3615 if (!multiple_p (step, BITS_PER_UNIT)
3616 || !is_ptrue (gimple_call_arg (call, 0), step / BITS_PER_UNIT))
3617 return nullptr;
3618
3619 function_instance instance (*this);
3620 instance.pred = PRED_x;
3621 return redirect_call (instance);
3622 }
3623
3624 /* Fold calls with predicate pfalse:
3625 _m predication: lhs = op1.
3626 _x or _z: lhs = {0, ...}.
3627 Implicit predication that reads from memory: lhs = {0, ...}.
3628 Implicit predication that writes to memory or prefetches: no-op.
3629 Return the new gimple statement on success, else NULL. */
3630 gimple *
3631 gimple_folder::fold_pfalse ()
3632 {
3633 tree gp = gp_value (call);
3634 /* If there isn't a GP then we can't do any folding as the instruction isn't
3635 predicated. */
3636 if (!gp)
3637 return nullptr;
3638
3639 if (pred == PRED_m)
3640 {
3641 tree val = inactive_values (call);
3642 if (is_pfalse (gp))
3643 return fold_call_to (val);
3644 return nullptr;
3645 }
3646 if ((pred == PRED_x || pred == PRED_z) && is_pfalse (gp))
3647 return fold_call_to (build_zero_cst (TREE_TYPE (lhs)));
3648 if (pred == PRED_implicit && is_pfalse (gp))
3649 {
3650 unsigned int flags = call_properties ();
3651 /* Folding to lhs = {0, ...} is not appropriate for intrinsics with
3652 AGGREGATE types as lhs. */
3653 if ((flags & CP_READ_MEMORY)
3654 && !AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
3655 return fold_call_to (build_zero_cst (TREE_TYPE (lhs)));
3656 if (flags & (CP_WRITE_MEMORY | CP_PREFETCH_MEMORY))
3657 return fold_to_stmt_vops (gimple_build_nop ());
3658 }
3659 return nullptr;
3660 }
3661
3662 /* Convert the lhs and all non-boolean vector-type operands to TYPE.
3663 Pass the converted variables to the callback FP, and finally convert the
3664 result back to the original type. Add the necessary conversion statements.
3665 Return the new call. */
3666 gimple *
3667 gimple_folder::convert_and_fold (tree type,
3668 gimple *(*fp) (gimple_folder &,
3669 tree, vec<tree> &))
3670 {
3671 gcc_assert (VECTOR_TYPE_P (type)
3672 && TYPE_MODE (type) != VNx16BImode);
3673 tree old_ty = TREE_TYPE (lhs);
3674 gimple_seq stmts = NULL;
3675 bool convert_lhs_p = !useless_type_conversion_p (type, old_ty);
3676 tree lhs_conv = convert_lhs_p ? create_tmp_var (type) : lhs;
3677 unsigned int num_args = gimple_call_num_args (call);
3678 auto_vec<tree, 16> args_conv;
3679 args_conv.safe_grow (num_args);
3680 for (unsigned int i = 0; i < num_args; ++i)
3681 {
3682 tree op = gimple_call_arg (call, i);
3683 tree op_ty = TREE_TYPE (op);
3684 args_conv[i] =
3685 (VECTOR_TYPE_P (op_ty)
3686 && TYPE_MODE (op_ty) != VNx16BImode
3687 && !useless_type_conversion_p (op_ty, type))
3688 ? gimple_build (&stmts, VIEW_CONVERT_EXPR, type, op) : op;
3689 }
3690
3691 gimple *new_stmt = fp (*this, lhs_conv, args_conv);
3692 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3693 if (convert_lhs_p)
3694 {
3695 tree t = build1 (VIEW_CONVERT_EXPR, old_ty, lhs_conv);
3696 gimple *g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR, t);
3697 gsi_insert_after (gsi, g, GSI_SAME_STMT);
3698 }
3699 return new_stmt;
3700 }
3701
3702 /* Fold the call to constant VAL. */
3703 gimple *
3704 gimple_folder::fold_to_cstu (poly_uint64 val)
3705 {
3706 return gimple_build_assign (lhs, build_int_cstu (TREE_TYPE (lhs), val));
3707 }
3708
3709 /* Fold the call to a PTRUE, taking the element size from type suffix 0. */
3710 gimple *
3711 gimple_folder::fold_to_ptrue ()
3712 {
3713 tree svbool_type = TREE_TYPE (lhs);
3714 tree bool_type = TREE_TYPE (svbool_type);
3715 unsigned int element_bytes = type_suffix (0).element_bytes;
3716
3717 /* The return type is svbool_t for all type suffixes, thus for b8 we
3718 want { 1, 1, 1, 1, ... }, for b16 we want { 1, 0, 1, 0, ... }, etc. */
3719 tree_vector_builder builder (svbool_type, element_bytes, 1);
3720 builder.quick_push (build_all_ones_cst (bool_type));
3721 for (unsigned int i = 1; i < element_bytes; ++i)
3722 builder.quick_push (build_zero_cst (bool_type));
3723 return gimple_build_assign (lhs, builder.build ());
3724 }
3725
3726 /* Fold the call to a PFALSE. */
3727 gimple *
3728 gimple_folder::fold_to_pfalse ()
3729 {
3730 return gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
3731 }
3732
3733 /* Fold an operation to a constant predicate in which the first VL
3734 elements are set and the rest are clear. Take the element size
3735 from type suffix 0. */
3736 gimple *
3737 gimple_folder::fold_to_vl_pred (unsigned int vl)
3738 {
3739 tree vectype = TREE_TYPE (lhs);
3740 tree element_type = TREE_TYPE (vectype);
3741 tree minus_one = build_all_ones_cst (element_type);
3742 tree zero = build_zero_cst (element_type);
3743 unsigned int element_bytes = type_suffix (0).element_bytes;
3744
3745 /* Construct COUNT elements that contain the ptrue followed by
3746 a repeating sequence of COUNT elements. */
3747 unsigned int count = constant_lower_bound (TYPE_VECTOR_SUBPARTS (vectype));
3748 gcc_assert (vl * element_bytes <= count);
3749 tree_vector_builder builder (vectype, count, 2);
3750 for (unsigned int i = 0; i < count * 2; ++i)
3751 {
3752 bool bit = (i & (element_bytes - 1)) == 0 && i < vl * element_bytes;
3753 builder.quick_push (bit ? minus_one : zero);
3754 }
3755 return gimple_build_assign (lhs, builder.build ());
3756 }
3757
3758 /* Try to fold the call to a constant, given that, for integers, the call
3759 is roughly equivalent to binary operation CODE. aarch64_const_binop
3760 handles any differences between CODE and the intrinsic. */
3761 gimple *
3762 gimple_folder::fold_const_binary (enum tree_code code)
3763 {
3764 gcc_assert (gimple_call_num_args (call) == 3);
3765 tree pg = gimple_call_arg (call, 0);
3766 tree op1 = gimple_call_arg (call, 1);
3767 tree op2 = gimple_call_arg (call, 2);
3768
3769 if (type_suffix (0).integer_p
3770 && (pred == PRED_x || is_ptrue (pg, type_suffix (0).element_bytes)))
3771 if (tree res = vector_const_binop (code, op1, op2, aarch64_const_binop))
3772 return gimple_build_assign (lhs, res);
3773
3774 return NULL;
3775 }
3776
3777 /* Fold the active lanes to X and set the inactive lanes according to the
3778 predication. Return the new statement. */
3779 gimple *
3780 gimple_folder::fold_active_lanes_to (tree x)
3781 {
3782 /* If predication is _x or the predicate is ptrue, fold to X. */
3783 if (pred == PRED_x
3784 || is_ptrue (gimple_call_arg (call, 0), type_suffix (0).element_bytes))
3785 return gimple_build_assign (lhs, x);
3786
3787 /* If the predication is _z or _m, calculate a vector that supplies the
3788 values of inactive lanes (the first vector argument for m and a zero
3789 vector from z). */
3790 tree vec_inactive;
3791 if (pred == PRED_z)
3792 vec_inactive = build_zero_cst (TREE_TYPE (lhs));
3793 else
3794 vec_inactive = gimple_call_arg (call, 1);
3795 if (operand_equal_p (x, vec_inactive, 0))
3796 return gimple_build_assign (lhs, x);
3797
3798 gimple_seq stmts = NULL;
3799 tree pred = convert_pred (stmts, vector_type (0), 0);
3800 x = force_vector (stmts, TREE_TYPE (lhs), x);
3801 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3802 return gimple_build_assign (lhs, VEC_COND_EXPR, pred, x, vec_inactive);
3803 }
3804
3805 /* Fold call to assignment statement lhs = t. */
3806 gimple *
3807 gimple_folder::fold_call_to (tree t)
3808 {
3809 if (types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (t)))
3810 return fold_to_stmt_vops (gimple_build_assign (lhs, t));
3811
3812 tree rhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), t);
3813 return fold_to_stmt_vops (gimple_build_assign (lhs, VIEW_CONVERT_EXPR, rhs));
3814 }
3815
3816 /* Fold call to G, incl. adjustments to the virtual operands. */
3817 gimple *
3818 gimple_folder::fold_to_stmt_vops (gimple *g)
3819 {
3820 gimple_seq stmts = NULL;
3821 gimple_seq_add_stmt_without_update (&stmts, g);
3822 gsi_replace_with_seq_vops (gsi, stmts);
3823 return g;
3824 }
3825
3826 /* Try to fold the call. Return the new statement on success and null
3827 on failure. */
3828 gimple *
3829 gimple_folder::fold ()
3830 {
3831 /* Don't fold anything when SVE is disabled; emit an error during
3832 expansion instead. */
3833 if (!TARGET_SVE)
3834 return NULL;
3835
3836 /* Punt if the function has a return type and no result location is
3837 provided. The attributes should allow target-independent code to
3838 remove the calls if appropriate. */
3839 if (!lhs && TREE_TYPE (gimple_call_fntype (call)) != void_type_node)
3840 return NULL;
3841
3842 /* First try some simplifications that are common to many functions. */
3843 if (auto *call = redirect_pred_x ())
3844 return call;
3845 if (auto *call = fold_pfalse ())
3846 return call;
3847
3848 return base->fold (*this);
3849 }
3850
3851 function_expander::function_expander (const function_instance &instance,
3852 tree fndecl, tree call_expr_in,
3853 rtx possible_target_in)
3854 : function_call_info (EXPR_LOCATION (call_expr_in), instance, fndecl),
3855 call_expr (call_expr_in), possible_target (possible_target_in)
3856 {
3857 }
3858
3859 /* Return the handler of direct optab OP for type suffix SUFFIX_I. */
3860 insn_code
3861 function_expander::direct_optab_handler (optab op, unsigned int suffix_i)
3862 {
3863 return ::direct_optab_handler (op, tuple_mode (suffix_i));
3864 }
3865
3866 /* Choose between signed and unsigned direct optabs SIGNED_OP and
3867 UNSIGNED_OP based on the signedness of type suffix SUFFIX_I, then
3868 pick the appropriate optab handler for the mode. Use MODE as the
3869 mode if given, otherwise use the mode of type suffix SUFFIX_I. */
3870 insn_code
3871 function_expander::direct_optab_handler_for_sign (optab signed_op,
3872 optab unsigned_op,
3873 unsigned int suffix_i,
3874 machine_mode mode)
3875 {
3876 if (mode == VOIDmode)
3877 mode = vector_mode (suffix_i);
3878 optab op = type_suffix (suffix_i).unsigned_p ? unsigned_op : signed_op;
3879 return ::direct_optab_handler (op, mode);
3880 }
3881
3882 /* Choose between signed and unsigned convert optabs SIGNED_OP and
3883 UNSIGNED_OP based on the signedness of type suffix SUFFIX_I, then
3884 pick the appropriate optab handler for "converting" from FROM_MODE
3885 to TO_MODE. */
3886 insn_code
3887 function_expander::convert_optab_handler_for_sign (optab signed_op,
3888 optab unsigned_op,
3889 unsigned int suffix_i,
3890 machine_mode to_mode,
3891 machine_mode from_mode)
3892 {
3893 optab op = type_suffix (suffix_i).unsigned_p ? unsigned_op : signed_op;
3894 return ::convert_optab_handler (op, to_mode, from_mode);
3895 }
3896
3897 /* Return true if X overlaps any input. */
3898 bool
3899 function_expander::overlaps_input_p (rtx x)
3900 {
3901 for (unsigned int i = 0; i < args.length (); ++i)
3902 if (reg_overlap_mentioned_p (x, args[i]))
3903 return true;
3904 return false;
3905 }
3906
3907 /* Convert ptr_mode value X to Pmode. */
3908 rtx
3909 function_expander::convert_to_pmode (rtx x)
3910 {
3911 if (ptr_mode == SImode)
3912 x = simplify_gen_unary (ZERO_EXTEND, DImode, x, SImode);
3913 return x;
3914 }
3915
3916 /* Return the base address for a contiguous load or store function.
3917 MEM_MODE is the mode of the addressed memory, BASE_ARGNO is
3918 the index of the base argument, and VNUM_ARGNO is the index of
3919 the vnum offset argument (if any). VL_ISA_MODE is AARCH64_FL_SM_ON
3920 if the vnum argument is a factor of the SME vector length, 0 if it
3921 is a factor of the current prevailing vector length. */
3922 rtx
3923 function_expander::get_contiguous_base (machine_mode mem_mode,
3924 unsigned int base_argno,
3925 unsigned int vnum_argno,
3926 aarch64_feature_flags vl_isa_mode)
3927 {
3928 rtx base = convert_to_pmode (args[base_argno]);
3929 if (mode_suffix_id == MODE_vnum)
3930 {
3931 rtx vnum = args[vnum_argno];
3932 if (vnum != const0_rtx)
3933 {
3934 /* Use the size of the memory mode for extending loads and truncating
3935 stores. Use the size of a full vector for non-extending loads
3936 and non-truncating stores (including svld[234] and svst[234]). */
3937 poly_int64 size = ordered_min (GET_MODE_SIZE (mem_mode),
3938 BYTES_PER_SVE_VECTOR);
3939 rtx offset;
3940 if ((vl_isa_mode & AARCH64_FL_SM_ON)
3941 && !TARGET_STREAMING
3942 && !size.is_constant ())
3943 {
3944 gcc_assert (known_eq (size, BYTES_PER_SVE_VECTOR));
3945 if (CONST_INT_P (vnum) && IN_RANGE (INTVAL (vnum), -32, 31))
3946 offset = aarch64_sme_vq_immediate (Pmode, INTVAL (vnum) * 16,
3947 AARCH64_ISA_MODE);
3948 else
3949 {
3950 offset = aarch64_sme_vq_immediate (Pmode, 16,
3951 AARCH64_ISA_MODE);
3952 offset = simplify_gen_binary (MULT, Pmode, vnum, offset);
3953 }
3954 }
3955 else
3956 {
3957 offset = gen_int_mode (size, Pmode);
3958 offset = simplify_gen_binary (MULT, Pmode, vnum, offset);
3959 }
3960 base = simplify_gen_binary (PLUS, Pmode, base, offset);
3961 }
3962 }
3963 return base;
3964 }
3965
3966 /* For a function that does the equivalent of:
3967
3968 OUTPUT = COND ? FN (INPUTS) : FALLBACK;
3969
3970 return the value of FALLBACK.
3971
3972 MODE is the mode of OUTPUT. NOPS is the number of operands in INPUTS.
3973 MERGE_ARGNO is the argument that provides FALLBACK for _m functions,
3974 or DEFAULT_MERGE_ARGNO if we should apply the usual rules.
3975
3976 ARGNO is the caller's index into args. If the returned value is
3977 argument 0 (as for unary _m operations), increment ARGNO past the
3978 returned argument. */
3979 rtx
3980 function_expander::get_fallback_value (machine_mode mode, unsigned int nops,
3981 unsigned int merge_argno,
3982 unsigned int &argno)
3983 {
3984 if (pred == PRED_z)
3985 return CONST0_RTX (mode);
3986
3987 gcc_assert (pred == PRED_m || pred == PRED_x);
3988 if (merge_argno == DEFAULT_MERGE_ARGNO)
3989 merge_argno = shape->has_merge_argument_p (*this, nops) ? 0 : 1;
3990
3991 if (merge_argno == 0)
3992 return args[argno++];
3993
3994 return args[merge_argno];
3995 }
3996
3997 /* Return a REG rtx that can be used for the result of the function,
3998 using the preferred target if suitable. */
3999 rtx
4000 function_expander::get_reg_target ()
4001 {
4002 machine_mode target_mode = result_mode ();
4003 if (!possible_target || GET_MODE (possible_target) != target_mode)
4004 possible_target = gen_reg_rtx (target_mode);
4005 return possible_target;
4006 }
4007
4008 /* As for get_reg_target, but make sure that the returned REG does not
4009 overlap any inputs. */
4010 rtx
4011 function_expander::get_nonoverlapping_reg_target ()
4012 {
4013 if (possible_target && overlaps_input_p (possible_target))
4014 possible_target = NULL_RTX;
4015 return get_reg_target ();
4016 }
4017
4018 /* Add an output operand to the instruction we're building, which has
4019 code ICODE. Bind the output to the preferred target rtx if possible. */
4020 void
4021 function_expander::add_output_operand (insn_code icode)
4022 {
4023 unsigned int opno = m_ops.length ();
4024 machine_mode mode = insn_data[icode].operand[opno].mode;
4025 m_ops.safe_grow (opno + 1, true);
4026 create_output_operand (&m_ops.last (), possible_target, mode);
4027 }
4028
4029 /* Add an input operand to the instruction we're building, which has
4030 code ICODE. Calculate the value of the operand as follows:
4031
4032 - If the operand is a vector and X is not, broadcast X to fill a
4033 vector of the appropriate mode.
4034
4035 - Otherwise, if the operand is a predicate, coerce X to have the
4036 mode that the instruction expects. In this case X is known to be
4037 VNx16BImode (the mode of svbool_t).
4038
4039 - Otherwise use X directly. The expand machinery checks that X has
4040 the right mode for the instruction. */
4041 void
4042 function_expander::add_input_operand (insn_code icode, rtx x)
4043 {
4044 unsigned int opno = m_ops.length ();
4045 const insn_operand_data &operand = insn_data[icode].operand[opno];
4046 machine_mode mode = operand.mode;
4047 if (mode == VOIDmode)
4048 {
4049 /* The only allowable uses of VOIDmode are:
4050
4051 - the wildcard aarch64_any_register_operand, which is used
4052 to avoid combinatorial explosion in the reinterpret patterns
4053
4054 - pmode_register_operand, which always has mode Pmode. */
4055 if (operand.predicate == aarch64_any_register_operand)
4056 mode = GET_MODE (x);
4057 else if (operand.predicate == pmode_register_operand)
4058 mode = Pmode;
4059 else
4060 gcc_unreachable ();
4061 }
4062 else if (!VECTOR_MODE_P (GET_MODE (x)) && VECTOR_MODE_P (mode))
4063 x = expand_vector_broadcast (mode, x);
4064 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
4065 {
4066 gcc_assert (GET_MODE (x) == VNx16BImode);
4067 x = gen_lowpart (mode, x);
4068 }
4069 m_ops.safe_grow (m_ops.length () + 1, true);
4070 create_input_operand (&m_ops.last (), x, mode);
4071 }
4072
4073 /* Add an integer operand with value X to the instruction. */
4074 void
4075 function_expander::add_integer_operand (poly_int64 x)
4076 {
4077 m_ops.safe_grow (m_ops.length () + 1, true);
4078 create_integer_operand (&m_ops.last (), x);
4079 }
4080
4081 /* Add a memory operand with mode MODE and address ADDR. */
4082 void
4083 function_expander::add_mem_operand (machine_mode mode, rtx addr)
4084 {
4085 /* Exception for OImode for the ld1ro intrinsics.
4086 They act on 256 bit octaword data, and it's just easier to use a scalar
4087 mode to represent that than add a new vector mode solely for the purpose
4088 of this intrinsic. */
4089 gcc_assert (VECTOR_MODE_P (mode) || mode == OImode);
4090 rtx mem = gen_rtx_MEM (mode, memory_address (mode, addr));
4091 /* The memory is only guaranteed to be element-aligned. */
4092 set_mem_align (mem, GET_MODE_ALIGNMENT (GET_MODE_INNER (mode)));
4093 add_fixed_operand (mem);
4094 }
4095
4096 /* Add an address operand with value X. The static operand data says
4097 what mode and form the address must have. */
4098 void
4099 function_expander::add_address_operand (rtx x)
4100 {
4101 m_ops.safe_grow (m_ops.length () + 1, true);
4102 create_address_operand (&m_ops.last (), x);
4103 }
4104
4105 /* Add an operand that must be X. The only way of legitimizing an
4106 invalid X is to reload the address of a MEM. */
4107 void
4108 function_expander::add_fixed_operand (rtx x)
4109 {
4110 m_ops.safe_grow (m_ops.length () + 1, true);
4111 create_fixed_operand (&m_ops.last (), x);
4112 }
4113
4114 /* Generate instruction ICODE, given that its operands have already
4115 been added to M_OPS. Return the value of the first operand. */
4116 rtx
4117 function_expander::generate_insn (insn_code icode)
4118 {
4119 expand_insn (icode, m_ops.length (), m_ops.address ());
4120 return function_returns_void_p () ? const0_rtx : m_ops[0].value;
4121 }
4122
4123 /* Convert the arguments to a gather/scatter function into the
4124 associated md operands. Argument ARGNO is the scalar or vector base and
4125 argument ARGNO + 1 is the scalar or vector displacement (if applicable).
4126 The md pattern expects:
4127
4128 - a scalar base
4129 - a vector displacement
4130
4131 If SCALED_P is true, it also expects:
4132
4133 - a const_int that is 1 if the displacement is zero-extended from 32 bits
4134 - a scaling multiplier (1 for bytes, 2 for .h indices, etc.).
4135
4136 If SCALED_P is false, the displacement is implicitly zero-extended
4137 and the scaling multiplier is implicitly 1. */
4138 void
4139 function_expander::prepare_gather_address_operands (unsigned int argno,
4140 bool scaled_p)
4141 {
4142 machine_mode mem_mode = memory_vector_mode ();
4143 tree vector_type = base_vector_type ();
4144 units_index units = displacement_units ();
4145 int shift_idx = -1;
4146 if (units == UNITS_none)
4147 {
4148 /* Vector base, no displacement. Convert to an integer zero base
4149 and a vector byte offset. */
4150 args.quick_insert (argno, const0_rtx);
4151 units = UNITS_bytes;
4152 }
4153 else if (vector_type)
4154 {
4155 /* Vector base, scalar displacement. Convert to a scalar base and
4156 a vector byte offset. */
4157 std::swap (args[argno], args[argno + 1]);
4158 if (units == UNITS_elements)
4159 shift_idx = argno;
4160 }
4161 else
4162 {
4163 /* Scalar base, vector displacement. This is the order that the md
4164 pattern wants. */
4165 args[argno] = convert_to_pmode (args[argno]);
4166 vector_type = displacement_vector_type ();
4167 if (units == UNITS_elements && !scaled_p)
4168 shift_idx = argno + 1;
4169 }
4170 tree scalar_displacement_type = TREE_TYPE (vector_type);
4171
4172 if (shift_idx >= 0)
4173 {
4174 machine_mode arg_mode = GET_MODE (args[shift_idx]);
4175 if (arg_mode == VOIDmode)
4176 arg_mode = DImode;
4177 unsigned int elt_bytes = GET_MODE_UNIT_SIZE (mem_mode);
4178 rtx shift = gen_int_mode (exact_log2 (elt_bytes), DImode);
4179 args[shift_idx] = simplify_gen_binary (ASHIFT, arg_mode,
4180 args[shift_idx], shift);
4181 units = UNITS_bytes;
4182 }
4183
4184 bool uxtw_p = (TYPE_PRECISION (scalar_displacement_type) == 64
4185 || TYPE_UNSIGNED (scalar_displacement_type));
4186 unsigned int scale = (units == UNITS_bytes
4187 ? 1 : GET_MODE_UNIT_SIZE (mem_mode));
4188
4189 if (scaled_p)
4190 {
4191 args.quick_insert (argno + 2, GEN_INT (uxtw_p));
4192 args.quick_insert (argno + 3, GEN_INT (scale));
4193 }
4194 else
4195 gcc_assert (uxtw_p && scale == 1);
4196 }
4197
4198 /* The final argument is an immediate svprfop value. Add two fake arguments
4199 to represent the rw and locality operands of a PREFETCH rtx. */
4200 void
4201 function_expander::prepare_prefetch_operands ()
4202 {
4203 unsigned int prfop = INTVAL (args.last ());
4204 /* Bit 3 of the prfop selects stores over loads. */
4205 args.quick_push (GEN_INT ((prfop & 8) != 0));
4206 /* Bits 1 and 2 specify the locality; 0-based for svprfop but
4207 1-based for PREFETCH. */
4208 args.quick_push (GEN_INT (((prfop >> 1) & 3) + 1));
4209 }
4210
4211 /* Add a dummy argument to indicate whether predicate argument ARGNO
4212 is all-true when interpreted in mode PRED_MODE. The hint goes
4213 immediately after ARGNO. */
4214 void
4215 function_expander::add_ptrue_hint (unsigned int argno, machine_mode pred_mode)
4216 {
4217 rtx pred = gen_lowpart (pred_mode, args[argno]);
4218 int hint = (pred == CONSTM1_RTX (pred_mode)
4219 ? SVE_KNOWN_PTRUE : SVE_MAYBE_NOT_PTRUE);
4220 args.quick_insert (argno + 1, gen_int_mode (hint, SImode));
4221 }
4222
4223 /* Rotate inputs args[START:END] one position to the left, so that
4224 args[START] becomes args[END - 1]. */
4225 void
4226 function_expander::rotate_inputs_left (unsigned int start, unsigned int end)
4227 {
4228 rtx new_last = args[start];
4229 for (unsigned int i = start; i < end - 1; ++i)
4230 args[i] = args[i + 1];
4231 args[end - 1] = new_last;
4232 }
4233
4234 /* Return true if the negation of argument ARGNO can be folded away,
4235 replacing it with the negated value if so. MODE is the associated
4236 vector mode, but the argument could be a single element. The main
4237 case this handles is constant arguments. */
4238 bool
4239 function_expander::try_negating_argument (unsigned int argno,
4240 machine_mode mode)
4241 {
4242 rtx x = args[argno];
4243 if (!VECTOR_MODE_P (GET_MODE (x)))
4244 mode = GET_MODE_INNER (mode);
4245
4246 x = simplify_unary_operation (NEG, mode, x, mode);
4247 if (!x)
4248 return false;
4249
4250 args[argno] = x;
4251 return true;
4252 }
4253
4254 /* Implement the call using instruction ICODE, with a 1:1 mapping between
4255 arguments and input operands. */
4256 rtx
4257 function_expander::use_exact_insn (insn_code icode)
4258 {
4259 unsigned int nops = insn_data[icode].n_operands;
4260 if (!function_returns_void_p ())
4261 {
4262 add_output_operand (icode);
4263 nops -= 1;
4264 }
4265 for (unsigned int i = 0; i < nops; ++i)
4266 add_input_operand (icode, args[i]);
4267 return generate_insn (icode);
4268 }
4269
4270 /* Implement the call using instruction ICODE, which does not use a
4271 governing predicate. We must therefore drop the GP from an _x call. */
4272 rtx
4273 function_expander::use_unpred_insn (insn_code icode)
4274 {
4275 /* We can't drop the predicate for _z and _m. */
4276 gcc_assert (pred == PRED_x || pred == PRED_none);
4277 /* Discount the output operand. */
4278 unsigned int nops = insn_data[icode].n_operands - 1;
4279 /* Drop the predicate argument in the case of _x predication. */
4280 unsigned int bias = (pred == PRED_x ? 1 : 0);
4281 unsigned int i = 0;
4282
4283 add_output_operand (icode);
4284 for (; i < nops; ++i)
4285 add_input_operand (icode, args[i + bias]);
4286
4287 return generate_insn (icode);
4288 }
4289
4290 /* Implement the call using instruction ICODE, which is a predicated
4291 operation that returns arbitrary values for inactive lanes. */
4292 rtx
4293 function_expander::use_pred_x_insn (insn_code icode)
4294 {
4295 /* At present we never need to handle PRED_none, which would involve
4296 creating a new predicate rather than using one supplied by the user. */
4297 gcc_assert (pred == PRED_x);
4298 /* Discount the output operand. */
4299 unsigned int nops = args.length () - 1;
4300
4301 bool has_float_operand_p = FLOAT_MODE_P (insn_data[icode].operand[0].mode);
4302
4303 /* Add the normal operands. */
4304 add_output_operand (icode);
4305 add_input_operand (icode, args[0]);
4306 for (unsigned int i = 0; i < nops; ++i)
4307 {
4308 add_input_operand (icode, args[i + 1]);
4309 if (FLOAT_MODE_P (GET_MODE (args[i + 1])))
4310 has_float_operand_p = true;
4311 }
4312
4313 if (has_float_operand_p
4314 && insn_data[icode].n_operands > (int) nops + 2)
4315 {
4316 /* Add a flag that indicates whether unpredicated instructions
4317 are allowed. */
4318 rtx pred = m_ops[1].value;
4319 if (flag_trapping_math && pred != CONST1_RTX (GET_MODE (pred)))
4320 add_integer_operand (SVE_STRICT_GP);
4321 else
4322 add_integer_operand (SVE_RELAXED_GP);
4323 }
4324
4325 return generate_insn (icode);
4326 }
4327
4328 /* Implement the call using instruction ICODE, which does the equivalent of:
4329
4330 OUTPUT = COND ? FN (INPUTS) : FALLBACK;
4331
4332 The instruction operands are in the order above: OUTPUT, COND, INPUTS
4333 and FALLBACK. MERGE_ARGNO is the argument that provides FALLBACK for _m
4334 functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
4335 rtx
4336 function_expander::use_cond_insn (insn_code icode, unsigned int merge_argno)
4337 {
4338 /* At present we never need to handle PRED_none, which would involve
4339 creating a new predicate rather than using one supplied by the user. */
4340 gcc_assert (pred != PRED_none);
4341 /* Discount the output, predicate and fallback value. */
4342 unsigned int nops = insn_data[icode].n_operands - 3;
4343 machine_mode mode = insn_data[icode].operand[0].mode;
4344
4345 unsigned int opno = 0;
4346 rtx fallback_arg = get_fallback_value (mode, nops, merge_argno, opno);
4347 rtx pred = args[opno++];
4348
4349 add_output_operand (icode);
4350 add_input_operand (icode, pred);
4351 for (unsigned int i = 0; i < nops; ++i)
4352 add_input_operand (icode, args[opno + i]);
4353 if (fallback_arg == CONST0_RTX (mode)
4354 && insn_operand_matches (icode, m_ops.length (), fallback_arg))
4355 add_fixed_operand (fallback_arg);
4356 else
4357 add_input_operand (icode, fallback_arg);
4358 return generate_insn (icode);
4359 }
4360
4361 /* Implement the call using instruction ICODE, which is a select-like
4362 operation with the following operands:
4363
4364 0: output
4365 1: true value
4366 2: false value
4367 3: predicate
4368
4369 MERGE_ARGNO is the argument that provides the "false" value for _m
4370 functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
4371 rtx
4372 function_expander::use_vcond_mask_insn (insn_code icode,
4373 unsigned int merge_argno)
4374 {
4375 machine_mode mode = vector_mode (0);
4376
4377 unsigned int opno = 0;
4378 rtx false_arg = get_fallback_value (mode, 1, merge_argno, opno);
4379 rtx pred_arg = args[opno++];
4380 rtx true_arg = args[opno++];
4381
4382 add_output_operand (icode);
4383 add_input_operand (icode, true_arg);
4384 add_input_operand (icode, false_arg);
4385 add_input_operand (icode, pred_arg);
4386 return generate_insn (icode);
4387 }
4388
4389 /* Implement the call using instruction ICODE, which loads memory operand 1
4390 into register operand 0 under the control of predicate operand 2.
4391 Extending loads have a further predicate (operand 3) that nominally
4392 controls the extension.
4393 HAS_ELSE is true if the pattern has an additional operand that specifies
4394 the values of inactive lanes. This exists to match the general maskload
4395 interface and is always zero for AArch64. */
4396 rtx
4397 function_expander::use_contiguous_load_insn (insn_code icode, bool has_else)
4398 {
4399 machine_mode mem_mode = memory_vector_mode ();
4400
4401 add_output_operand (icode);
4402 add_mem_operand (mem_mode, get_contiguous_base (mem_mode));
4403 add_input_operand (icode, args[0]);
4404 if (GET_MODE_UNIT_BITSIZE (mem_mode) < type_suffix (0).element_bits)
4405 add_input_operand (icode, CONSTM1_RTX (VNx16BImode));
4406
4407 /* If we have an else operand, add it. */
4408 if (has_else)
4409 add_input_operand (icode, CONST0_RTX (mem_mode));
4410
4411 return generate_insn (icode);
4412 }
4413
4414 /* Implement the call using instruction ICODE, which prefetches from
4415 address operand 1 under the control of predicate operand 0.
4416 Operands 2, 3 and 4 respectively specify the svprfop value,
4417 the PREFETCH rw flag and the PREFETCH locality. */
4418 rtx
4419 function_expander::use_contiguous_prefetch_insn (insn_code icode)
4420 {
4421 add_input_operand (icode, args[0]);
4422 add_address_operand (get_contiguous_base (VNx16QImode));
4423 for (unsigned int i = args.length () - 3; i < args.length (); ++i)
4424 add_input_operand (icode, args[i]);
4425 return generate_insn (icode);
4426 }
4427
4428 /* Implement the call using instruction ICODE, which stores register operand 1
4429 into memory operand 0 under the control of predicate operand 2. */
4430 rtx
4431 function_expander::use_contiguous_store_insn (insn_code icode)
4432 {
4433 machine_mode mem_mode = memory_vector_mode ();
4434
4435 add_mem_operand (mem_mode, get_contiguous_base (mem_mode));
4436 add_input_operand (icode, args.last ());
4437 add_input_operand (icode, args[0]);
4438 return generate_insn (icode);
4439 }
4440
4441 /* Implement the call using one of the following strategies, chosen in order:
4442
4443 (1) "aarch64_pred_<optab><mode>_z" for PRED_z predicate functions
4444
4445 (2) "aarch64_pred_<optab><mode>" for PRED_x functions
4446
4447 (3) a normal unpredicated optab for PRED_none and PRED_x functions,
4448 dropping the predicate in the latter case
4449
4450 (4) an unpredicated "aarch64_sve_<code_optab><mode>" for PRED_none and
4451 PRED_x functions, again dropping the predicate for PRED_x
4452
4453 (5) "cond_<optab><mode>" otherwise
4454
4455 where <optab> corresponds to:
4456
4457 - CODE_FOR_SINT for signed integers
4458 - CODE_FOR_UINT for unsigned integers
4459 - UNSPEC_FOR_COND_FP for predicated floating-point
4460 - UNSPEC_FOR_UNCOND_FP for unpredicated floating-point
4461
4462 and where <code_optab> is like <optab>, but uses CODE_FOR_SINT instead
4463 of UNSPEC_FOR_FP for floating-point values.
4464
4465 MERGE_ARGNO is the argument that provides the values of inactive lanes for
4466 _m functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
4467 rtx
4468 function_expander::map_to_rtx_codes (rtx_code code_for_sint,
4469 rtx_code code_for_uint,
4470 int unspec_for_cond_fp,
4471 int unspec_for_uncond_fp,
4472 unsigned int merge_argno)
4473 {
4474 machine_mode mode = tuple_mode (0);
4475 rtx_code code = (type_suffix (0).unsigned_p ? code_for_uint : code_for_sint);
4476 insn_code icode;
4477
4478 if (mode_suffix_id == MODE_single)
4479 {
4480 gcc_assert (pred == PRED_none);
4481 if (type_suffix (0).integer_p)
4482 icode = code_for_aarch64_sve_single (code, mode);
4483 else
4484 icode = code_for_aarch64_sve_single (unspec_for_uncond_fp, mode);
4485 return use_exact_insn (icode);
4486 }
4487
4488 /* Handle predicate logic operations, which always use _z predication. */
4489 if (type_suffix (0).tclass == TYPE_bool)
4490 {
4491 gcc_assert (pred == PRED_z && code_for_uint == code_for_sint);
4492 return use_exact_insn (code_for_aarch64_pred_z (code, mode));
4493 }
4494
4495 /* First try using UNSPEC_PRED_X patterns for _x predication,
4496 if available. */
4497 if (pred == PRED_x)
4498 {
4499 if (type_suffix (0).integer_p)
4500 icode = maybe_code_for_aarch64_pred (code, mode);
4501 else
4502 icode = maybe_code_for_aarch64_pred (unspec_for_cond_fp, mode);
4503 if (icode != CODE_FOR_nothing)
4504 return use_pred_x_insn (icode);
4505 }
4506
4507 /* Otherwise expand PRED_none and PRED_x operations without a predicate.
4508 Floating-point operations conventionally use the signed rtx code. */
4509 if (pred == PRED_none || pred == PRED_x)
4510 {
4511 if (type_suffix (0).float_p && unspec_for_uncond_fp >= 0)
4512 icode = maybe_code_for_aarch64_sve (unspec_for_uncond_fp, mode);
4513 else
4514 icode = direct_optab_handler (code_to_optab (code), 0);
4515 if (icode == CODE_FOR_nothing)
4516 icode = code_for_aarch64_sve (code, mode);
4517 return use_unpred_insn (icode);
4518 }
4519
4520 /* Don't use cond_*_optabs here, since not all codes have one yet. */
4521 if (type_suffix (0).integer_p)
4522 icode = code_for_cond (code, mode);
4523 else
4524 icode = code_for_cond (unspec_for_cond_fp, mode);
4525 return use_cond_insn (icode, merge_argno);
4526 }
4527
4528 /* Implement the call using one of the following strategies, chosen in order:
4529
4530 (1) "aarch64_pred_<optab><mode>" for PRED_x functions; this is a
4531 predicated pattern
4532
4533 (2) "aarch64_sve_<optab><mode>" for PRED_none and PRED_x functions;
4534 this is an unpredicated pattern
4535
4536 (3) "cond_<optab><mode>" otherwise
4537
4538 where <optab> corresponds to:
4539
4540 - UNSPEC_FOR_SINT for signed integers
4541 - UNSPEC_FOR_UINT for unsigned integers
4542 - UNSPEC_FOR_FP for floating-point values
4543
4544 MERGE_ARGNO is the argument that provides the values of inactive lanes for
4545 _m functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
4546 rtx
4547 function_expander::map_to_unspecs (int unspec_for_sint, int unspec_for_uint,
4548 int unspec_for_fp, unsigned int merge_argno)
4549 {
4550 machine_mode mode = tuple_mode (0);
4551 int unspec = (!type_suffix (0).integer_p ? unspec_for_fp
4552 : type_suffix (0).unsigned_p ? unspec_for_uint
4553 : unspec_for_sint);
4554
4555 if (mode_suffix_id == MODE_single)
4556 {
4557 gcc_assert (pred == PRED_none);
4558 return use_exact_insn (code_for_aarch64_sve_single (unspec, mode));
4559 }
4560
4561 if (pred == PRED_x)
4562 {
4563 insn_code icode = maybe_code_for_aarch64_pred (unspec, mode);
4564 if (icode != CODE_FOR_nothing)
4565 return use_pred_x_insn (icode);
4566 }
4567
4568 if (pred == PRED_none || pred == PRED_x)
4569 {
4570 insn_code icode = maybe_code_for_aarch64_sve (unspec, mode);
4571 if (icode != CODE_FOR_nothing)
4572 return use_unpred_insn (icode);
4573 }
4574
4575 insn_code icode = code_for_cond (unspec, vector_mode (0));
4576 return use_cond_insn (icode, merge_argno);
4577 }
4578
4579 /* Expand the call and return its lhs. */
4580 rtx
4581 function_expander::expand ()
4582 {
4583 unsigned int nargs = call_expr_nargs (call_expr);
4584 args.reserve (nargs);
4585 for (unsigned int i = 0; i < nargs; ++i)
4586 args.quick_push (expand_normal (CALL_EXPR_ARG (call_expr, i)));
4587
4588 if (fpm_mode == FPM_set)
4589 {
4590 /* The last element of these functions is always an fpm_t that must be
4591 written to FPMR before the call to the instruction itself. */
4592 rtx fpm = args.last ();
4593 gcc_assert (CONST_INT_P (fpm) || GET_MODE (fpm) == DImode);
4594 emit_move_insn (gen_rtx_REG (DImode, FPM_REGNUM), fpm);
4595 }
4596 return base->expand (*this);
4597 }
4598
4599 /* Return a structure type that contains a single field of type FIELD_TYPE.
4600 The field is called __val, but that's an internal detail rather than
4601 an exposed part of the API. */
4602 static tree
4603 wrap_type_in_struct (tree field_type)
4604 {
4605 tree field = build_decl (input_location, FIELD_DECL,
4606 get_identifier ("__val"), field_type);
4607 tree struct_type = lang_hooks.types.make_type (RECORD_TYPE);
4608 DECL_FIELD_CONTEXT (field) = struct_type;
4609 TYPE_FIELDS (struct_type) = field;
4610 make_type_sizeless (struct_type);
4611 layout_type (struct_type);
4612 return struct_type;
4613 }
4614
4615 /* Register a built-in TYPE_DECL called NAME for TYPE. This is used/needed
4616 when TYPE is a structure type. */
4617 static void
4618 register_type_decl (tree type, const char *name)
4619 {
4620 tree decl = build_decl (input_location, TYPE_DECL,
4621 get_identifier (name), type);
4622 TYPE_NAME (type) = decl;
4623 TYPE_STUB_DECL (type) = decl;
4624 lang_hooks.decls.pushdecl (decl);
4625 /* ??? Undo the effect of set_underlying_type for C. The C frontend
4626 doesn't recognize DECL as a built-in because (as intended) the decl has
4627 a real location instead of BUILTINS_LOCATION. The frontend therefore
4628 treats the decl like a normal C "typedef struct foo foo;", expecting
4629 the type for tag "struct foo" to have a dummy unnamed TYPE_DECL instead
4630 of the named one we attached above. It then sets DECL_ORIGINAL_TYPE
4631 on the supposedly unnamed decl, creating a circularity that upsets
4632 dwarf2out.
4633
4634 We don't want to follow the normal C model and create "struct foo"
4635 tags for tuple types since (a) the types are supposed to be opaque
4636 and (b) they couldn't be defined as a real struct anyway. Treating
4637 the TYPE_DECLs as "typedef struct foo foo;" without creating
4638 "struct foo" would lead to confusing error messages. */
4639 DECL_ORIGINAL_TYPE (decl) = NULL_TREE;
4640 }
4641
4642 /* Register the built-in SVE ABI types, such as __SVBool_t. */
4643 static void
4644 register_builtin_types ()
4645 {
4646 sve_alignment_switcher switcher;
4647
4648 #define DEF_SVE_TYPE(ACLE_NAME, NCHARS, ABI_NAME, SCALAR_TYPE) \
4649 scalar_types[VECTOR_TYPE_ ## ACLE_NAME] = SCALAR_TYPE;
4650 #include "aarch64-sve-builtins.def"
4651
4652 for (unsigned int i = 0; i < NUM_VECTOR_TYPES; ++i)
4653 {
4654 tree vectype;
4655 unsigned int num_zr = 0, num_pr = 0;
4656 if (vector_type_index (i) == VECTOR_TYPE_svcount_t)
4657 {
4658 vectype = abi_vector_types[VECTOR_TYPE_svbool_t];
4659 vectype = wrap_type_in_struct (vectype);
4660 num_pr = 1;
4661 }
4662 else
4663 {
4664 tree eltype = scalar_types[i];
4665 if (eltype == boolean_type_node)
4666 {
4667 vectype = build_truth_vector_type_for_mode (BYTES_PER_SVE_VECTOR,
4668 VNx16BImode);
4669 num_pr = 1;
4670 /* Leave svbool_t as indivisible for now. We don't yet support
4671 C/C++ operators on predicates. */
4672 TYPE_INDIVISIBLE_P (vectype) = 1;
4673 }
4674 else
4675 {
4676 scalar_mode elmode = SCALAR_TYPE_MODE (eltype);
4677 unsigned int elbytes = GET_MODE_SIZE (elmode);
4678 poly_uint64 nunits = exact_div (BYTES_PER_SVE_VECTOR, elbytes);
4679 machine_mode mode
4680 = aarch64_sve_data_mode (elmode, nunits).require ();
4681 vectype = build_vector_type_for_mode (eltype, mode);
4682 auto size = wi::to_poly_offset (TYPE_SIZE (vectype));
4683 gcc_assert (VECTOR_MODE_P (TYPE_MODE (vectype))
4684 && TYPE_MODE (vectype) == mode
4685 && TYPE_MODE_RAW (vectype) == mode
4686 && TYPE_ALIGN (vectype) == 128
4687 && known_eq (size, BITS_PER_SVE_VECTOR));
4688 num_zr = 1;
4689 TYPE_INDIVISIBLE_P (vectype) = 0;
4690 }
4691 vectype = build_distinct_type_copy (vectype);
4692 gcc_assert (vectype == TYPE_MAIN_VARIANT (vectype));
4693 SET_TYPE_STRUCTURAL_EQUALITY (vectype);
4694 TYPE_ARTIFICIAL (vectype) = 1;
4695 make_type_sizeless (vectype);
4696 }
4697 if (num_pr)
4698 {
4699 auto size = wi::to_poly_offset (TYPE_SIZE (vectype));
4700 gcc_assert (TYPE_MODE (vectype) == VNx16BImode
4701 && TYPE_MODE (vectype) == TYPE_MODE_RAW (vectype)
4702 && TYPE_ALIGN (vectype) == 16
4703 && known_eq (size, BYTES_PER_SVE_VECTOR));
4704 }
4705 add_sve_type_attribute (vectype, num_zr, num_pr,
4706 vector_types[i].mangled_name,
4707 vector_types[i].acle_name);
4708 abi_vector_types[i] = vectype;
4709 if (TREE_CODE (vectype) == RECORD_TYPE)
4710 register_type_decl (vectype, vector_types[i].abi_name);
4711 else
4712 lang_hooks.types.register_builtin_type (vectype,
4713 vector_types[i].abi_name);
4714 }
4715 }
4716
4717 /* Initialize all compiler built-ins related to SVE that should be
4718 defined at start-up. */
4719 void
4720 init_builtins ()
4721 {
4722 aarch64_target_switcher switcher (AARCH64_FL_SVE);
4723 register_builtin_types ();
4724 if (in_lto_p)
4725 {
4726 handle_arm_sve_h (false);
4727 handle_arm_sme_h (false);
4728 handle_arm_neon_sve_bridge_h (false);
4729 }
4730 }
4731
4732 /* Register vector type TYPE under its arm_sve.h name. */
4733 static void
4734 register_vector_type (vector_type_index type)
4735 {
4736 tree vectype = abi_vector_types[type];
4737 tree id = get_identifier (vector_types[type].acle_name);
4738 tree decl = build_decl (input_location, TYPE_DECL, id, vectype);
4739 decl = lang_hooks.decls.pushdecl (decl);
4740
4741 /* Record the new ACLE type if pushdecl succeeded without error. Use
4742 the ABI type otherwise, so that the type we record at least has the
4743 right form, even if it doesn't have the right name. This should give
4744 better error recovery behavior than installing error_mark_node or
4745 installing an incorrect type. */
4746 if (decl
4747 && TREE_CODE (decl) == TYPE_DECL
4748 && TREE_TYPE (decl) != error_mark_node
4749 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == vectype)
4750 vectype = TREE_TYPE (decl);
4751 acle_vector_types[0][type] = vectype;
4752 }
4753
4754 /* Register the tuple type that contains NUM_VECTORS vectors of type TYPE. */
4755 static void
4756 register_tuple_type (unsigned int num_vectors, vector_type_index type)
4757 {
4758 tree vector_type = acle_vector_types[0][type];
4759 bool is_pred = GET_MODE_CLASS (TYPE_MODE (vector_type)) == MODE_VECTOR_BOOL;
4760
4761 /* Work out the structure name. */
4762 char buffer[sizeof ("svbfloat16x4_t")];
4763 const char *vector_type_name = vector_types[type].acle_name;
4764 snprintf (buffer, sizeof (buffer), "%.*sx%d_t",
4765 (int) strlen (vector_type_name) - 2, vector_type_name,
4766 num_vectors);
4767
4768 /* The contents of the type are opaque, so we can define them in any
4769 way that maps to the correct ABI type.
4770
4771 Here we choose to use the same layout as for arm_neon.h, but with
4772 "__val" instead of "val":
4773
4774 struct svfooxN_t { svfoo_t __val[N]; };
4775
4776 (It wouldn't be possible to write that directly in C or C++ for
4777 sizeless types, but that's not a problem for this function.)
4778
4779 Using arrays simplifies the handling of svget and svset for variable
4780 arguments. */
4781 tree array_type = build_array_type_nelts (vector_type, num_vectors);
4782 gcc_assert (VECTOR_MODE_P (TYPE_MODE (array_type))
4783 && TYPE_MODE_RAW (array_type) == TYPE_MODE (array_type)
4784 && TYPE_ALIGN (array_type) == (is_pred ? 16 : 128));
4785
4786 tree tuple_type = wrap_type_in_struct (array_type);
4787 if (is_pred)
4788 add_sve_type_attribute (tuple_type, 0, num_vectors, NULL, buffer);
4789 else
4790 add_sve_type_attribute (tuple_type, num_vectors, 0, NULL, buffer);
4791 gcc_assert (VECTOR_MODE_P (TYPE_MODE (tuple_type))
4792 && TYPE_MODE_RAW (tuple_type) == TYPE_MODE (tuple_type)
4793 && TYPE_ALIGN (tuple_type) == TYPE_ALIGN (array_type));
4794
4795 register_type_decl (tuple_type, buffer);
4796
4797 acle_vector_types[num_vectors - 1][type] = tuple_type;
4798 }
4799
4800 /* Register the svpattern enum. */
4801 static void
4802 register_svpattern ()
4803 {
4804 auto_vec<string_int_pair, 32> values;
4805 #define PUSH(UPPER, LOWER, VALUE) \
4806 values.quick_push (string_int_pair ("SV_" #UPPER, VALUE));
4807 AARCH64_FOR_SVPATTERN (PUSH)
4808 #undef PUSH
4809
4810 acle_svpattern = lang_hooks.types.simulate_enum_decl (input_location,
4811 "svpattern", &values);
4812 }
4813
4814 /* Register the svprfop enum. */
4815 static void
4816 register_svprfop ()
4817 {
4818 auto_vec<string_int_pair, 16> values;
4819 #define PUSH(UPPER, LOWER, VALUE) \
4820 values.quick_push (string_int_pair ("SV_" #UPPER, VALUE));
4821 AARCH64_FOR_SVPRFOP (PUSH)
4822 #undef PUSH
4823
4824 acle_svprfop = lang_hooks.types.simulate_enum_decl (input_location,
4825 "svprfop", &values);
4826 }
4827
4828 /* Implement #pragma GCC aarch64 "arm_sve.h". */
4829 void
4830 handle_arm_sve_h (bool function_nulls_p)
4831 {
4832 if (function_table)
4833 {
4834 error ("duplicate definition of %qs", "arm_sve.h");
4835 return;
4836 }
4837
4838 aarch64_target_switcher switcher (AARCH64_FL_SVE);
4839 sve_alignment_switcher alignment_switcher;
4840
4841 /* Define the vector and tuple types. */
4842 for (unsigned int type_i = 0; type_i < NUM_VECTOR_TYPES; ++type_i)
4843 {
4844 vector_type_index type = vector_type_index (type_i);
4845 register_vector_type (type);
4846 if (type != VECTOR_TYPE_svcount_t)
4847 for (unsigned int count = 2; count <= MAX_TUPLE_SIZE; ++count)
4848 if (type != VECTOR_TYPE_svbool_t || count == 2 || count == 4)
4849 register_tuple_type (count, type);
4850 }
4851
4852 /* Define the enums. */
4853 register_svpattern ();
4854 register_svprfop ();
4855
4856 /* Define the functions. */
4857 function_table = hash_table<registered_function_hasher>::create_ggc (1023);
4858 function_builder builder (arm_sve_handle, function_nulls_p);
4859 for (unsigned int i = 0; i < ARRAY_SIZE (function_groups); ++i)
4860 builder.register_function_group (function_groups[i]);
4861 }
4862
4863 /* Implement #pragma GCC aarch64 "arm_neon_sve_bridge.h". */
4864 void
4865 handle_arm_neon_sve_bridge_h (bool function_nulls_p)
4866 {
4867 if (initial_indexes[arm_sme_handle] == 0)
4868 handle_arm_sme_h (true);
4869
4870 aarch64_target_switcher switcher;
4871
4872 /* Define the functions. */
4873 function_builder builder (arm_neon_sve_handle, function_nulls_p);
4874 for (unsigned int i = 0; i < ARRAY_SIZE (neon_sve_function_groups); ++i)
4875 builder.register_function_group (neon_sve_function_groups[i]);
4876 }
4877
4878 /* Return the function decl with SVE function subcode CODE, or error_mark_node
4879 if no such function exists. */
4880 tree
4881 builtin_decl (unsigned int code, bool)
4882 {
4883 if (code >= vec_safe_length (registered_functions))
4884 return error_mark_node;
4885 return (*registered_functions)[code]->decl;
4886 }
4887
4888 /* Implement #pragma GCC aarch64 "arm_sme.h". */
4889 void
4890 handle_arm_sme_h (bool function_nulls_p)
4891 {
4892 if (!function_table)
4893 {
4894 error ("%qs defined without first defining %qs",
4895 "arm_sme.h", "arm_sve.h");
4896 return;
4897 }
4898
4899 aarch64_target_switcher switcher (AARCH64_FL_SME);
4900
4901 function_builder builder (arm_sme_handle, function_nulls_p);
4902 for (unsigned int i = 0; i < ARRAY_SIZE (sme_function_groups); ++i)
4903 builder.register_function_group (sme_function_groups[i]);
4904 }
4905
4906 /* If we're implementing manual overloading, check whether the SVE
4907 function with subcode CODE is overloaded, and if so attempt to
4908 determine the corresponding non-overloaded function. The call
4909 occurs at location LOCATION and has the arguments given by ARGLIST.
4910
4911 If the call is erroneous, report an appropriate error and return
4912 error_mark_node. Otherwise, if the function is overloaded, return
4913 the decl of the non-overloaded function. Return NULL_TREE otherwise,
4914 indicating that the call should be processed in the normal way. */
4915 tree
4916 resolve_overloaded_builtin (location_t location, unsigned int code,
4917 vec<tree, va_gc> *arglist)
4918 {
4919 if (code >= vec_safe_length (registered_functions))
4920 return NULL_TREE;
4921
4922 registered_function &rfn = *(*registered_functions)[code];
4923 if (rfn.overloaded_p)
4924 return function_resolver (location, rfn.instance, rfn.decl,
4925 *arglist).resolve ();
4926 return NULL_TREE;
4927 }
4928
4929 /* Perform any semantic checks needed for a call to the SVE function
4930 with subcode CODE, such as testing for integer constant expressions.
4931 The call occurs at location LOCATION and has NARGS arguments,
4932 given by ARGS. FNDECL is the original function decl, before
4933 overload resolution.
4934
4935 Return true if the call is valid, otherwise report a suitable error. */
4936 bool
4937 check_builtin_call (location_t location, vec<location_t>, unsigned int code,
4938 tree fndecl, unsigned int nargs, tree *args)
4939 {
4940 const registered_function &rfn = *(*registered_functions)[code];
4941 if (!aarch64_check_required_extensions (location, rfn.decl,
4942 rfn.required_extensions))
4943 return false;
4944 return function_checker (location, rfn.instance, fndecl,
4945 TREE_TYPE (rfn.decl), nargs, args).check ();
4946 }
4947
4948 /* Attempt to fold STMT, given that it's a call to the SVE function
4949 with subcode CODE. Return the new statement on success and null
4950 on failure. Insert any other new statements at GSI. */
4951 gimple *
4952 gimple_fold_builtin (unsigned int code, gimple_stmt_iterator *gsi, gcall *stmt)
4953 {
4954 registered_function &rfn = *(*registered_functions)[code];
4955 return gimple_folder (rfn.instance, rfn.decl, gsi, stmt).fold ();
4956 }
4957
4958 /* Expand a call to the SVE function with subcode CODE. EXP is the call
4959 expression and TARGET is the preferred location for the result.
4960 Return the value of the lhs. */
4961 rtx
4962 expand_builtin (unsigned int code, tree exp, rtx target)
4963 {
4964 registered_function &rfn = *(*registered_functions)[code];
4965 if (!aarch64_check_required_extensions (EXPR_LOCATION (exp), rfn.decl,
4966 rfn.required_extensions))
4967 return target;
4968 return function_expander (rfn.instance, rfn.decl, exp, target).expand ();
4969 }
4970
4971 /* If TYPE is a built-in type defined by the SVE ABI, return the mangled name,
4972 otherwise return NULL. */
4973 const char *
4974 mangle_builtin_type (const_tree type)
4975 {
4976 /* ??? The C++ frontend normally strips qualifiers and attributes before
4977 calling this hook, adding separate mangling for attributes that affect
4978 type identity. Fortunately the type copy will have the same TYPE_NAME
4979 as the original, so we can get the attributes from there. */
4980 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4981 type = TREE_TYPE (TYPE_NAME (type));
4982 if (tree attr = lookup_sve_type_attribute (type))
4983 if (tree id = TREE_VALUE (chain_index (2, TREE_VALUE (attr))))
4984 return IDENTIFIER_POINTER (id);
4985 return NULL;
4986 }
4987
4988 /* Return true if TYPE is a built-in SVE type defined by the ABI or ACLE. */
4989 bool
4990 builtin_type_p (const_tree type)
4991 {
4992 return lookup_sve_type_attribute (type);
4993 }
4994
4995 /* Return true if TYPE is a built-in SVE type defined by the ABI or ACLE.
4996 If so, store the number of constituent SVE vectors in *NUM_ZR and the
4997 number of constituent SVE predicates in *NUM_PR. */
4998 bool
4999 builtin_type_p (const_tree type, unsigned int *num_zr, unsigned int *num_pr)
5000 {
5001 if (tree attr = lookup_sve_type_attribute (type))
5002 {
5003 tree num_zr_node = TREE_VALUE (attr);
5004 tree num_pr_node = TREE_CHAIN (num_zr_node);
5005 *num_zr = tree_to_uhwi (TREE_VALUE (num_zr_node));
5006 *num_pr = tree_to_uhwi (TREE_VALUE (num_pr_node));
5007 return true;
5008 }
5009 return false;
5010 }
5011
5012 /* ATTRS is the attribute list for a sizeless SVE type. Return the
5013 attributes of the associated fixed-length SVE type, taking the
5014 "SVE type" attributes from NEW_SVE_TYPE_ARGS. */
5015 static tree
5016 get_arm_sve_vector_bits_attributes (tree old_attrs, tree new_sve_type_args)
5017 {
5018 tree new_attrs = NULL_TREE;
5019 tree *ptr = &new_attrs;
5020 for (tree attr = old_attrs; attr; attr = TREE_CHAIN (attr))
5021 {
5022 tree name = get_attribute_name (attr);
5023 if (is_attribute_p ("SVE sizeless type", name))
5024 continue;
5025
5026 tree args = TREE_VALUE (attr);
5027 if (is_attribute_p ("SVE type", name))
5028 args = new_sve_type_args;
5029 *ptr = tree_cons (TREE_PURPOSE (attr), args, NULL_TREE);
5030 ptr = &TREE_CHAIN (*ptr);
5031 }
5032 return new_attrs;
5033 }
5034
5035 /* An attribute callback for the "arm_sve_vector_bits" attribute. */
5036 tree
5037 handle_arm_sve_vector_bits_attribute (tree *node, tree, tree args, int,
5038 bool *no_add_attrs)
5039 {
5040 *no_add_attrs = true;
5041
5042 tree type = *node;
5043 tree attr = lookup_sve_type_attribute (type);
5044 if (!attr)
5045 {
5046 error ("%qs applied to non-SVE type %qT", "arm_sve_vector_bits", type);
5047 return NULL_TREE;
5048 }
5049
5050 if (!VECTOR_TYPE_P (type))
5051 {
5052 error ("%qs applied to non-vector type %qT",
5053 "arm_sve_vector_bits", type);
5054 return NULL_TREE;
5055 }
5056
5057 if (!sizeless_type_p (type))
5058 {
5059 error ("%qs applied to type %qT, which already has a size",
5060 "arm_sve_vector_bits", type);
5061 return NULL_TREE;
5062 }
5063
5064 tree size = TREE_VALUE (args);
5065 if (TREE_CODE (size) != INTEGER_CST)
5066 {
5067 error ("%qs requires an integer constant expression",
5068 "arm_sve_vector_bits");
5069 return NULL_TREE;
5070 }
5071
5072 unsigned HOST_WIDE_INT value = tree_to_uhwi (size);
5073 if (maybe_ne (value, BITS_PER_SVE_VECTOR))
5074 {
5075 warning (OPT_Wattributes, "unsupported SVE vector size");
5076 return NULL_TREE;
5077 }
5078
5079 /* Construct a new list of "SVE type" attribute arguments. */
5080 tree new_sve_type_args = copy_list (TREE_VALUE (attr));
5081
5082 /* Mangle the type as an instance of the imaginary template:
5083
5084 __SVE_VLS<typename, unsigned>
5085
5086 where the first parameter is the SVE type and where the second
5087 parameter is the SVE vector length in bits. */
5088 tree mangled_name_node = chain_index (2, new_sve_type_args);
5089 const char *old_mangled_name
5090 = IDENTIFIER_POINTER (TREE_VALUE (mangled_name_node));
5091 char *new_mangled_name
5092 = xasprintf ("9__SVE_VLSI%sLj%dEE", old_mangled_name, (int) value);
5093 TREE_VALUE (mangled_name_node) = get_identifier (new_mangled_name);
5094 free (new_mangled_name);
5095
5096 /* FIXME: The type ought to be a distinct copy in all cases, but
5097 currently that makes the C frontend reject conversions between
5098 svbool_t and its fixed-length variants. Using a type variant
5099 avoids that but means that we treat some ambiguous combinations
5100 as valid. */
5101 tree new_type;
5102 tree base_type = TYPE_MAIN_VARIANT (type);
5103 if (lang_GNU_C () && VECTOR_BOOLEAN_TYPE_P (type))
5104 new_type = build_variant_type_copy (base_type);
5105 else
5106 new_type = build_distinct_type_copy (base_type);
5107
5108 /* Construct a TYPE_DECL for the new type. This serves two purposes:
5109
5110 - It ensures we don't print the original TYPE_DECL in error messages.
5111 Printing the original name would be confusing because there are
5112 situations in which the distinction between the original type and
5113 the new type matters. For example:
5114
5115 __SVInt8_t __attribute__((arm_sve_vector_bits(512))) *a;
5116 __SVInt8_t *b;
5117
5118 a = b;
5119
5120 is invalid in C++, but without this, we'd print both types in
5121 the same way.
5122
5123 - Having a separate TYPE_DECL is necessary to ensure that C++
5124 mangling works correctly. See mangle_builtin_type for details.
5125
5126 The name of the decl is something like:
5127
5128 svint8_t __attribute__((arm_sve_vector_bits(512)))
5129
5130 This is a compromise. It would be more accurate to use something like:
5131
5132 __SVInt8_t __attribute__((arm_sve_vector_bits(512)))
5133
5134 but the <arm_sve.h> name is likely to be more meaningful. */
5135 tree acle_name_node = TREE_CHAIN (mangled_name_node);
5136 const char *old_type_name = IDENTIFIER_POINTER (TREE_VALUE (acle_name_node));
5137 char *new_type_name
5138 = xasprintf ("%s __attribute__((arm_sve_vector_bits(%d)))",
5139 old_type_name, (int) value);
5140 tree decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
5141 get_identifier (new_type_name), new_type);
5142 DECL_ARTIFICIAL (decl) = 1;
5143 TYPE_NAME (new_type) = decl;
5144 free (new_type_name);
5145
5146 /* Allow the GNU vector extensions to be applied to vectors.
5147 The extensions aren't yet defined for packed predicates,
5148 so continue to treat them as abstract entities for now. */
5149 if (!VECTOR_BOOLEAN_TYPE_P (new_type))
5150 TYPE_INDIVISIBLE_P (new_type) = 0;
5151
5152 /* The new type is a normal sized type; it doesn't have the same
5153 restrictions as sizeless types. */
5154 TYPE_ATTRIBUTES (new_type)
5155 = get_arm_sve_vector_bits_attributes (TYPE_ATTRIBUTES (new_type),
5156 new_sve_type_args);
5157
5158 /* Apply the relevant attributes, qualifiers and alignment of TYPE,
5159 if they differ from the original (sizeless) BASE_TYPE. */
5160 if (TYPE_ATTRIBUTES (base_type) != TYPE_ATTRIBUTES (type)
5161 || TYPE_QUALS (base_type) != TYPE_QUALS (type))
5162 {
5163 tree attrs
5164 = get_arm_sve_vector_bits_attributes (TYPE_ATTRIBUTES (type),
5165 new_sve_type_args);
5166 new_type = build_type_attribute_qual_variant (new_type, attrs,
5167 TYPE_QUALS (type));
5168 }
5169 if (TYPE_ALIGN (base_type) != TYPE_ALIGN (type))
5170 new_type = build_aligned_type (new_type, TYPE_ALIGN (type));
5171
5172 *node = new_type;
5173 return NULL_TREE;
5174 }
5175
5176 /* Implement TARGET_VERIFY_TYPE_CONTEXT for SVE types. */
5177 bool
5178 verify_type_context (location_t loc, type_context_kind context,
5179 const_tree type, bool silent_p)
5180 {
5181 const_tree tmp = type;
5182 if (omp_type_context (context) && POINTER_TYPE_P (type))
5183 tmp = strip_pointer_types (tmp);
5184
5185 if (!sizeless_type_p (tmp))
5186 return true;
5187
5188 switch (context)
5189 {
5190 case TCTX_SIZEOF:
5191 case TCTX_STATIC_STORAGE:
5192 if (!silent_p)
5193 error_at (loc, "SVE type %qT does not have a fixed size", type);
5194 return false;
5195
5196 case TCTX_ALIGNOF:
5197 if (!silent_p)
5198 error_at (loc, "SVE type %qT does not have a defined alignment", type);
5199 return false;
5200
5201 case TCTX_THREAD_STORAGE:
5202 if (!silent_p)
5203 error_at (loc, "variables of type %qT cannot have thread-local"
5204 " storage duration", type);
5205 return false;
5206
5207 case TCTX_POINTER_ARITH:
5208 if (!silent_p)
5209 error_at (loc, "arithmetic on pointer to SVE type %qT", type);
5210 return false;
5211
5212 case TCTX_FIELD:
5213 if (silent_p)
5214 ;
5215 else if (lang_GNU_CXX ())
5216 error_at (loc, "member variables cannot have SVE type %qT", type);
5217 else
5218 error_at (loc, "fields cannot have SVE type %qT", type);
5219 return false;
5220
5221 case TCTX_ARRAY_ELEMENT:
5222 if (!silent_p)
5223 error_at (loc, "array elements cannot have SVE type %qT", type);
5224 return false;
5225
5226 case TCTX_ALLOCATION:
5227 if (!silent_p)
5228 error_at (loc, "cannot allocate objects with SVE type %qT", type);
5229 return false;
5230
5231 case TCTX_DEALLOCATION:
5232 if (!silent_p)
5233 error_at (loc, "cannot delete objects with SVE type %qT", type);
5234 return false;
5235
5236 case TCTX_EXCEPTIONS:
5237 if (!silent_p)
5238 error_at (loc, "cannot throw or catch SVE type %qT", type);
5239 return false;
5240
5241 case TCTX_CAPTURE_BY_COPY:
5242 if (!silent_p)
5243 error_at (loc, "capture by copy of SVE type %qT", type);
5244 return false;
5245
5246 case TCTX_OMP_MAP:
5247 if (!silent_p)
5248 error_at (loc, "SVE type %qT not allowed in %<map%> clause", type);
5249 return false;
5250
5251 case TCTX_OMP_MAP_IMP_REF:
5252 if (!silent_p)
5253 error ("cannot reference %qT object types in %<target%> region", type);
5254 return false;
5255
5256 case TCTX_OMP_PRIVATE:
5257 if (!silent_p)
5258 error_at (loc, "SVE type %qT not allowed in"
5259 " %<target%> %<private%> clause", type);
5260 return false;
5261
5262 case TCTX_OMP_FIRSTPRIVATE:
5263 if (!silent_p)
5264 error_at (loc, "SVE type %qT not allowed in"
5265 " %<target%> %<firstprivate%> clause", type);
5266 return false;
5267
5268 case TCTX_OMP_DEVICE_ADDR:
5269 if (!silent_p)
5270 error_at (loc, "SVE type %qT not allowed in"
5271 " %<target%> device clauses", type);
5272 return false;
5273
5274 default:
5275 break;
5276 }
5277 gcc_unreachable ();
5278 }
5279
5280 }
5281
5282 using namespace aarch64_sve;
5283
5284 inline void
5285 gt_ggc_mx (function_instance *)
5286 {
5287 }
5288
5289 inline void
5290 gt_pch_nx (function_instance *)
5291 {
5292 }
5293
5294 inline void
5295 gt_pch_nx (function_instance *, gt_pointer_operator, void *)
5296 {
5297 }
5298
5299 #include "gt-aarch64-sve-builtins.h"