]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/arm/arm-mve-builtins.cc
arm: Fix MVE intrinsics support with LTO (PR target/110268)
[thirdparty/gcc.git] / gcc / config / arm / arm-mve-builtins.cc
1 /* ACLE support for Arm MVE
2 Copyright (C) 2021-2023 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 "expr.h"
34 #include "basic-block.h"
35 #include "function.h"
36 #include "fold-const.h"
37 #include "gimple.h"
38 #include "gimple-iterator.h"
39 #include "emit-rtl.h"
40 #include "langhooks.h"
41 #include "stringpool.h"
42 #include "attribs.h"
43 #include "diagnostic.h"
44 #include "arm-protos.h"
45 #include "arm-builtins.h"
46 #include "arm-mve-builtins.h"
47 #include "arm-mve-builtins-base.h"
48 #include "arm-mve-builtins-shapes.h"
49
50 namespace arm_mve {
51
52 /* Static information about each single-predicate or single-vector
53 ACLE type. */
54 struct vector_type_info
55 {
56 /* The name of the type as declared by arm_mve.h. */
57 const char *acle_name;
58
59 /* Whether the type requires a floating point abi. */
60 const bool requires_float;
61 };
62
63 /* Describes a function decl. */
64 class GTY(()) registered_function
65 {
66 public:
67 /* The ACLE function that the decl represents. */
68 function_instance instance GTY ((skip));
69
70 /* The decl itself. */
71 tree decl;
72
73 /* Whether the function requires a floating point abi. */
74 bool requires_float;
75
76 /* True if the decl represents an overloaded function that needs to be
77 resolved by function_resolver. */
78 bool overloaded_p;
79 };
80
81 /* Hash traits for registered_function. */
82 struct registered_function_hasher : nofree_ptr_hash <registered_function>
83 {
84 typedef function_instance compare_type;
85
86 static hashval_t hash (value_type);
87 static bool equal (value_type, const compare_type &);
88 };
89
90 /* Flag indicating whether the arm MVE types have been handled. */
91 static bool handle_arm_mve_types_p;
92
93 /* Information about each single-predicate or single-vector type. */
94 static CONSTEXPR const vector_type_info vector_types[] = {
95 #define DEF_MVE_TYPE(ACLE_NAME, SCALAR_TYPE) \
96 { #ACLE_NAME, REQUIRES_FLOAT },
97 #include "arm-mve-builtins.def"
98 };
99
100 /* The function name suffix associated with each predication type. */
101 static const char *const pred_suffixes[NUM_PREDS + 1] = {
102 "",
103 "_m",
104 "_p",
105 "_x",
106 "_z",
107 ""
108 };
109
110 /* Static information about each mode_suffix_index. */
111 CONSTEXPR const mode_suffix_info mode_suffixes[] = {
112 #define VECTOR_TYPE_none NUM_VECTOR_TYPES
113 #define DEF_MVE_MODE(NAME, BASE, DISPLACEMENT, UNITS) \
114 { "_" #NAME, VECTOR_TYPE_##BASE, VECTOR_TYPE_##DISPLACEMENT, UNITS_##UNITS },
115 #include "arm-mve-builtins.def"
116 #undef VECTOR_TYPE_none
117 { "", NUM_VECTOR_TYPES, NUM_VECTOR_TYPES, UNITS_none }
118 };
119
120 /* Static information about each type_suffix_index. */
121 CONSTEXPR const type_suffix_info type_suffixes[NUM_TYPE_SUFFIXES + 1] = {
122 #define DEF_MVE_TYPE_SUFFIX(NAME, ACLE_TYPE, CLASS, BITS, MODE) \
123 { "_" #NAME, \
124 VECTOR_TYPE_##ACLE_TYPE, \
125 TYPE_##CLASS, \
126 BITS, \
127 BITS / BITS_PER_UNIT, \
128 TYPE_##CLASS == TYPE_signed || TYPE_##CLASS == TYPE_unsigned, \
129 TYPE_##CLASS == TYPE_unsigned, \
130 TYPE_##CLASS == TYPE_float, \
131 0, \
132 MODE },
133 #include "arm-mve-builtins.def"
134 { "", NUM_VECTOR_TYPES, TYPE_bool, 0, 0, false, false, false,
135 0, VOIDmode }
136 };
137
138 /* Define a TYPES_<combination> macro for each combination of type
139 suffixes that an ACLE function can have, where <combination> is the
140 name used in DEF_MVE_FUNCTION entries.
141
142 Use S (T) for single type suffix T and D (T1, T2) for a pair of type
143 suffixes T1 and T2. Use commas to separate the suffixes.
144
145 Although the order shouldn't matter, the convention is to sort the
146 suffixes lexicographically after dividing suffixes into a type
147 class ("b", "f", etc.) and a numerical bit count. */
148
149 /* _f16. */
150 #define TYPES_float16(S, D) \
151 S (f16)
152
153 /* _f16 _f32. */
154 #define TYPES_all_float(S, D) \
155 S (f16), S (f32)
156
157 /* _s8 _u8 . */
158 #define TYPES_integer_8(S, D) \
159 S (s8), S (u8)
160
161 /* _s8 _s16
162 _u8 _u16. */
163 #define TYPES_integer_8_16(S, D) \
164 S (s8), S (s16), S (u8), S(u16)
165
166 /* _s16 _s32
167 _u16 _u32. */
168 #define TYPES_integer_16_32(S, D) \
169 S (s16), S (s32), \
170 S (u16), S (u32)
171
172 /* _s16 _s32. */
173 #define TYPES_signed_16_32(S, D) \
174 S (s16), S (s32)
175
176 /* _s8 _s16 _s32. */
177 #define TYPES_all_signed(S, D) \
178 S (s8), S (s16), S (s32)
179
180 /* _u8 _u16 _u32. */
181 #define TYPES_all_unsigned(S, D) \
182 S (u8), S (u16), S (u32)
183
184 /* _s8 _s16 _s32
185 _u8 _u16 _u32. */
186 #define TYPES_all_integer(S, D) \
187 TYPES_all_signed (S, D), TYPES_all_unsigned (S, D)
188
189 /* _s8 _s16 _s32 _s64
190 _u8 _u16 _u32 _u64. */
191 #define TYPES_all_integer_with_64(S, D) \
192 TYPES_all_signed (S, D), S (s64), TYPES_all_unsigned (S, D), S (u64)
193
194 /* s32 _u32. */
195 #define TYPES_integer_32(S, D) \
196 S (s32), S (u32)
197
198 /* s32 . */
199 #define TYPES_signed_32(S, D) \
200 S (s32)
201
202 #define TYPES_reinterpret_signed1(D, A) \
203 D (A, s8), D (A, s16), D (A, s32), D (A, s64)
204
205 #define TYPES_reinterpret_unsigned1(D, A) \
206 D (A, u8), D (A, u16), D (A, u32), D (A, u64)
207
208 #define TYPES_reinterpret_integer(S, D) \
209 TYPES_reinterpret_unsigned1 (D, s8), \
210 D (s8, s16), D (s8, s32), D (s8, s64), \
211 TYPES_reinterpret_unsigned1 (D, s16), \
212 D (s16, s8), D (s16, s32), D (s16, s64), \
213 TYPES_reinterpret_unsigned1 (D, s32), \
214 D (s32, s8), D (s32, s16), D (s32, s64), \
215 TYPES_reinterpret_unsigned1 (D, s64), \
216 D (s64, s8), D (s64, s16), D (s64, s32), \
217 TYPES_reinterpret_signed1 (D, u8), \
218 D (u8, u16), D (u8, u32), D (u8, u64), \
219 TYPES_reinterpret_signed1 (D, u16), \
220 D (u16, u8), D (u16, u32), D (u16, u64), \
221 TYPES_reinterpret_signed1 (D, u32), \
222 D (u32, u8), D (u32, u16), D (u32, u64), \
223 TYPES_reinterpret_signed1 (D, u64), \
224 D (u64, u8), D (u64, u16), D (u64, u32)
225
226 /* { _s8 _s16 _s32 _s64 } x { _s8 _s16 _s32 _s64 }
227 { _u8 _u16 _u32 _u64 } { _u8 _u16 _u32 _u64 }. */
228 #define TYPES_reinterpret_integer1(D, A) \
229 TYPES_reinterpret_signed1 (D, A), \
230 TYPES_reinterpret_unsigned1 (D, A)
231
232 #define TYPES_reinterpret_float1(D, A) \
233 D (A, f16), D (A, f32)
234
235 #define TYPES_reinterpret_float(S, D) \
236 TYPES_reinterpret_float1 (D, s8), \
237 TYPES_reinterpret_float1 (D, s16), \
238 TYPES_reinterpret_float1 (D, s32), \
239 TYPES_reinterpret_float1 (D, s64), \
240 TYPES_reinterpret_float1 (D, u8), \
241 TYPES_reinterpret_float1 (D, u16), \
242 TYPES_reinterpret_float1 (D, u32), \
243 TYPES_reinterpret_float1 (D, u64), \
244 TYPES_reinterpret_integer1 (D, f16), \
245 TYPES_reinterpret_integer1 (D, f32), \
246 D (f16, f32), D (f32, f16)
247
248 /* Describe a pair of type suffixes in which only the first is used. */
249 #define DEF_VECTOR_TYPE(X) { TYPE_SUFFIX_ ## X, NUM_TYPE_SUFFIXES }
250
251 /* Describe a pair of type suffixes in which both are used. */
252 #define DEF_DOUBLE_TYPE(X, Y) { TYPE_SUFFIX_ ## X, TYPE_SUFFIX_ ## Y }
253
254 /* Create an array that can be used in arm-mve-builtins.def to
255 select the type suffixes in TYPES_<NAME>. */
256 #define DEF_MVE_TYPES_ARRAY(NAME) \
257 static const type_suffix_pair types_##NAME[] = { \
258 TYPES_##NAME (DEF_VECTOR_TYPE, DEF_DOUBLE_TYPE), \
259 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES } \
260 }
261
262 /* For functions that don't take any type suffixes. */
263 static const type_suffix_pair types_none[] = {
264 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES },
265 { NUM_TYPE_SUFFIXES, NUM_TYPE_SUFFIXES }
266 };
267
268 DEF_MVE_TYPES_ARRAY (all_integer);
269 DEF_MVE_TYPES_ARRAY (all_integer_with_64);
270 DEF_MVE_TYPES_ARRAY (float16);
271 DEF_MVE_TYPES_ARRAY (all_float);
272 DEF_MVE_TYPES_ARRAY (all_signed);
273 DEF_MVE_TYPES_ARRAY (all_unsigned);
274 DEF_MVE_TYPES_ARRAY (integer_8);
275 DEF_MVE_TYPES_ARRAY (integer_8_16);
276 DEF_MVE_TYPES_ARRAY (integer_16_32);
277 DEF_MVE_TYPES_ARRAY (integer_32);
278 DEF_MVE_TYPES_ARRAY (signed_16_32);
279 DEF_MVE_TYPES_ARRAY (signed_32);
280 DEF_MVE_TYPES_ARRAY (reinterpret_integer);
281 DEF_MVE_TYPES_ARRAY (reinterpret_float);
282
283 /* Used by functions that have no governing predicate. */
284 static const predication_index preds_none[] = { PRED_none, NUM_PREDS };
285
286 /* Used by functions that have the m (merging) predicated form, and in
287 addition have an unpredicated form. */
288 const predication_index preds_m_or_none[] = {
289 PRED_m, PRED_none, NUM_PREDS
290 };
291
292 /* Used by functions that have the mx (merging and "don't care"
293 predicated forms, and in addition have an unpredicated form. */
294 static const predication_index preds_mx_or_none[] = {
295 PRED_m, PRED_x, PRED_none, NUM_PREDS
296 };
297
298 /* Used by functions that have the p predicated form, in addition to
299 an unpredicated form. */
300 static const predication_index preds_p_or_none[] = {
301 PRED_p, PRED_none, NUM_PREDS
302 };
303
304 /* A list of all MVE ACLE functions. */
305 static CONSTEXPR const function_group_info function_groups[] = {
306 #define DEF_MVE_FUNCTION(NAME, SHAPE, TYPES, PREDS) \
307 { #NAME, &functions::NAME, &shapes::SHAPE, types_##TYPES, preds_##PREDS, \
308 REQUIRES_FLOAT },
309 #include "arm-mve-builtins.def"
310 };
311
312 /* The scalar type associated with each vector type. */
313 extern GTY(()) tree scalar_types[NUM_VECTOR_TYPES];
314 tree scalar_types[NUM_VECTOR_TYPES];
315
316 /* The single-predicate and single-vector types, with their built-in
317 "__simd128_..._t" name. Allow an index of NUM_VECTOR_TYPES, which always
318 yields a null tree. */
319 static GTY(()) tree abi_vector_types[NUM_VECTOR_TYPES + 1];
320
321 /* Same, but with the arm_mve.h names. */
322 extern GTY(()) tree acle_vector_types[MAX_TUPLE_SIZE][NUM_VECTOR_TYPES + 1];
323 tree acle_vector_types[MAX_TUPLE_SIZE][NUM_VECTOR_TYPES + 1];
324
325 /* The list of all registered function decls, indexed by code. */
326 static GTY(()) vec<registered_function *, va_gc> *registered_functions;
327
328 /* All registered function decls, hashed on the function_instance
329 that they implement. This is used for looking up implementations of
330 overloaded functions. */
331 static hash_table<registered_function_hasher> *function_table;
332
333 /* True if we've already complained about attempts to use functions
334 when the required extension is disabled. */
335 static bool reported_missing_float_p;
336
337 /* Return the MVE abi type with element of type TYPE. */
338 static tree
339 arm_mve_type_for_scalar_type (tree eltype)
340 {
341 for (unsigned int i = 0; i < __TYPE_FINAL; ++i)
342 if (arm_simd_types[i].eltype == eltype
343 && GET_MODE_SIZE (arm_simd_types[i].mode) == 16)
344 return arm_simd_types[i].itype;
345
346 gcc_unreachable ();
347 }
348
349 /* Register the built-in MVE ABI vector types, such as uint32x4_t. */
350 static void
351 register_builtin_types ()
352 {
353 #define DEF_MVE_TYPE(ACLE_NAME, SCALAR_TYPE) \
354 scalar_types[VECTOR_TYPE_ ## ACLE_NAME] = SCALAR_TYPE;
355 #include "arm-mve-builtins.def"
356 for (unsigned int i = 0; i < NUM_VECTOR_TYPES; ++i)
357 {
358 if (vector_types[i].requires_float && !TARGET_HAVE_MVE_FLOAT)
359 continue;
360 tree eltype = scalar_types[i];
361 tree vectype;
362 if (eltype == boolean_type_node)
363 {
364 vectype = get_typenode_from_name (UINT16_TYPE);
365 gcc_assert (GET_MODE_SIZE (TYPE_MODE (vectype)) == 2);
366 }
367 else
368 {
369 vectype = arm_mve_type_for_scalar_type (eltype);
370 gcc_assert (VECTOR_MODE_P (TYPE_MODE (vectype))
371 && GET_MODE_SIZE (TYPE_MODE (vectype)) == 16);
372 }
373 abi_vector_types[i] = vectype;
374 }
375 }
376
377 /* Register vector type TYPE under its arm_mve.h name. */
378 static void
379 register_vector_type (vector_type_index type)
380 {
381
382 /* If the target does not have the mve.fp extension, but the type requires
383 it, then it needs to be assigned a non-dummy type so that functions
384 with those types in their signature can be registered. This allows for
385 diagnostics about the missing extension, rather than about a missing
386 function definition. */
387 if (vector_types[type].requires_float && !TARGET_HAVE_MVE_FLOAT)
388 {
389 acle_vector_types[0][type] = void_type_node;
390 return;
391 }
392
393 tree vectype = abi_vector_types[type];
394 tree id = get_identifier (vector_types[type].acle_name);
395 tree decl = build_decl (input_location, TYPE_DECL, id, vectype);
396 decl = lang_hooks.decls.pushdecl (decl);
397
398 /* Record the new ACLE type if pushdecl succeeded without error. Use
399 the ABI type otherwise, so that the type we record at least has the
400 right form, even if it doesn't have the right name. This should give
401 better error recovery behavior than installing error_mark_node or
402 installing an incorrect type. */
403 if (decl
404 && TREE_CODE (decl) == TYPE_DECL
405 && TREE_TYPE (decl) != error_mark_node
406 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == vectype)
407 vectype = TREE_TYPE (decl);
408 acle_vector_types[0][type] = vectype;
409 }
410
411 /* Register tuple types of element type TYPE under their arm_mve_types.h
412 names. */
413 static void
414 register_builtin_tuple_types (vector_type_index type)
415 {
416 const vector_type_info* info = &vector_types[type];
417
418 /* If the target does not have the mve.fp extension, but the type requires
419 it, then it needs to be assigned a non-dummy type so that functions
420 with those types in their signature can be registered. This allows for
421 diagnostics about the missing extension, rather than about a missing
422 function definition. */
423 if (scalar_types[type] == boolean_type_node
424 || (info->requires_float && !TARGET_HAVE_MVE_FLOAT))
425 {
426 for (unsigned int num_vectors = 2; num_vectors <= 4; num_vectors += 2)
427 acle_vector_types[num_vectors >> 1][type] = void_type_node;
428 return;
429 }
430
431 const char *vector_type_name = info->acle_name;
432 char buffer[sizeof ("float32x4x2_t")];
433 for (unsigned int num_vectors = 2; num_vectors <= 4; num_vectors += 2)
434 {
435 snprintf (buffer, sizeof (buffer), "%.*sx%d_t",
436 (int) strlen (vector_type_name) - 2, vector_type_name,
437 num_vectors);
438
439 tree vectype = acle_vector_types[0][type];
440 tree arrtype = build_array_type_nelts (vectype, num_vectors);
441 gcc_assert (TYPE_MODE_RAW (arrtype) == TYPE_MODE (arrtype));
442 tree field = build_decl (input_location, FIELD_DECL,
443 get_identifier ("val"), arrtype);
444
445 tree t = lang_hooks.types.simulate_record_decl (input_location, buffer,
446 make_array_slice (&field,
447 1));
448 gcc_assert (TYPE_MODE_RAW (t) == TYPE_MODE (t));
449 acle_vector_types[num_vectors >> 1][type] = TREE_TYPE (t);
450 }
451 }
452
453 /* Implement #pragma GCC arm "arm_mve_types.h". */
454 void
455 handle_arm_mve_types_h ()
456 {
457 if (handle_arm_mve_types_p)
458 {
459 error ("duplicate definition of %qs", "arm_mve_types.h");
460 return;
461 }
462 handle_arm_mve_types_p = true;
463 if (!TARGET_HAVE_MVE)
464 {
465 error ("this definition requires the MVE ISA extension");
466 return;
467 }
468 register_builtin_types ();
469 for (unsigned int type_i = 0; type_i < NUM_VECTOR_TYPES; ++type_i)
470 {
471 vector_type_index type = vector_type_index (type_i);
472 register_vector_type (type);
473 if (type_i != VECTOR_TYPE_mve_pred16_t)
474 register_builtin_tuple_types (type);
475 }
476 }
477
478 /* Implement #pragma GCC arm "arm_mve.h" <bool>. */
479 void
480 handle_arm_mve_h (bool preserve_user_namespace)
481 {
482 if (function_table)
483 {
484 error ("duplicate definition of %qs", "arm_mve.h");
485 return;
486 }
487
488 /* Define MVE functions. */
489 function_table = new hash_table<registered_function_hasher> (1023);
490 function_builder builder;
491 for (unsigned int i = 0; i < ARRAY_SIZE (function_groups); ++i)
492 builder.register_function_group (function_groups[i],
493 preserve_user_namespace);
494 }
495
496 /* Return the function decl with MVE function subcode CODE, or error_mark_node
497 if no such function exists. */
498 tree
499 builtin_decl (unsigned int code)
500 {
501 if (code >= vec_safe_length (registered_functions))
502 return error_mark_node;
503 return (*registered_functions)[code]->decl;
504 }
505
506 /* Return true if CANDIDATE is equivalent to MODEL_TYPE for overloading
507 purposes. */
508 static bool
509 matches_type_p (const_tree model_type, const_tree candidate)
510 {
511 if (VECTOR_TYPE_P (model_type))
512 {
513 if (!VECTOR_TYPE_P (candidate)
514 || maybe_ne (TYPE_VECTOR_SUBPARTS (model_type),
515 TYPE_VECTOR_SUBPARTS (candidate))
516 || TYPE_MODE (model_type) != TYPE_MODE (candidate))
517 return false;
518
519 model_type = TREE_TYPE (model_type);
520 candidate = TREE_TYPE (candidate);
521 }
522 return (candidate != error_mark_node
523 && TYPE_MAIN_VARIANT (model_type) == TYPE_MAIN_VARIANT (candidate));
524 }
525
526 /* Report an error against LOCATION that the user has tried to use
527 a floating point function when the mve.fp extension is disabled. */
528 static void
529 report_missing_float (location_t location, tree fndecl)
530 {
531 /* Avoid reporting a slew of messages for a single oversight. */
532 if (reported_missing_float_p)
533 return;
534
535 error_at (location, "ACLE function %qD requires ISA extension %qs",
536 fndecl, "mve.fp");
537 inform (location, "you can enable mve.fp by using the command-line"
538 " option %<-march%>, or by using the %<target%>"
539 " attribute or pragma");
540 reported_missing_float_p = true;
541 }
542
543 /* Report that LOCATION has a call to FNDECL in which argument ARGNO
544 was not an integer constant expression. ARGNO counts from zero. */
545 static void
546 report_non_ice (location_t location, tree fndecl, unsigned int argno)
547 {
548 error_at (location, "argument %d of %qE must be an integer constant"
549 " expression", argno + 1, fndecl);
550 }
551
552 /* Report that LOCATION has a call to FNDECL in which argument ARGNO has
553 the value ACTUAL, whereas the function requires a value in the range
554 [MIN, MAX]. ARGNO counts from zero. */
555 static void
556 report_out_of_range (location_t location, tree fndecl, unsigned int argno,
557 HOST_WIDE_INT actual, HOST_WIDE_INT min,
558 HOST_WIDE_INT max)
559 {
560 error_at (location, "passing %wd to argument %d of %qE, which expects"
561 " a value in the range [%wd, %wd]", actual, argno + 1, fndecl,
562 min, max);
563 }
564
565 /* Report that LOCATION has a call to FNDECL in which argument ARGNO has
566 the value ACTUAL, whereas the function requires a valid value of
567 enum type ENUMTYPE. ARGNO counts from zero. */
568 static void
569 report_not_enum (location_t location, tree fndecl, unsigned int argno,
570 HOST_WIDE_INT actual, tree enumtype)
571 {
572 error_at (location, "passing %wd to argument %d of %qE, which expects"
573 " a valid %qT value", actual, argno + 1, fndecl, enumtype);
574 }
575
576 /* Checks that the mve.fp extension is enabled, given that REQUIRES_FLOAT
577 indicates whether it is required or not for function FNDECL.
578 Report an error against LOCATION if not. */
579 static bool
580 check_requires_float (location_t location, tree fndecl,
581 bool requires_float)
582 {
583 if (requires_float && !TARGET_HAVE_MVE_FLOAT)
584 {
585 report_missing_float (location, fndecl);
586 return false;
587 }
588
589 return true;
590 }
591
592 /* Return a hash code for a function_instance. */
593 hashval_t
594 function_instance::hash () const
595 {
596 inchash::hash h;
597 /* BASE uniquely determines BASE_NAME, so we don't need to hash both. */
598 h.add_ptr (base);
599 h.add_ptr (shape);
600 h.add_int (mode_suffix_id);
601 h.add_int (type_suffix_ids[0]);
602 h.add_int (type_suffix_ids[1]);
603 h.add_int (pred);
604 return h.end ();
605 }
606
607 /* Return a set of CP_* flags that describe what the function could do,
608 taking the command-line flags into account. */
609 unsigned int
610 function_instance::call_properties () const
611 {
612 unsigned int flags = base->call_properties (*this);
613
614 /* -fno-trapping-math means that we can assume any FP exceptions
615 are not user-visible. */
616 if (!flag_trapping_math)
617 flags &= ~CP_RAISE_FP_EXCEPTIONS;
618
619 return flags;
620 }
621
622 /* Return true if calls to the function could read some form of
623 global state. */
624 bool
625 function_instance::reads_global_state_p () const
626 {
627 unsigned int flags = call_properties ();
628
629 /* Preserve any dependence on rounding mode, flush to zero mode, etc.
630 There is currently no way of turning this off; in particular,
631 -fno-rounding-math (which is the default) means that we should make
632 the usual assumptions about rounding mode, which for intrinsics means
633 acting as the instructions do. */
634 if (flags & CP_READ_FPCR)
635 return true;
636
637 return false;
638 }
639
640 /* Return true if calls to the function could modify some form of
641 global state. */
642 bool
643 function_instance::modifies_global_state_p () const
644 {
645 unsigned int flags = call_properties ();
646
647 /* Preserve any exception state written back to the FPCR,
648 unless -fno-trapping-math says this is unnecessary. */
649 if (flags & CP_RAISE_FP_EXCEPTIONS)
650 return true;
651
652 /* Handle direct modifications of global state. */
653 return flags & CP_WRITE_MEMORY;
654 }
655
656 /* Return true if calls to the function could raise a signal. */
657 bool
658 function_instance::could_trap_p () const
659 {
660 unsigned int flags = call_properties ();
661
662 /* Handle functions that could raise SIGFPE. */
663 if (flags & CP_RAISE_FP_EXCEPTIONS)
664 return true;
665
666 /* Handle functions that could raise SIGBUS or SIGSEGV. */
667 if (flags & (CP_READ_MEMORY | CP_WRITE_MEMORY))
668 return true;
669
670 return false;
671 }
672
673 /* Return true if the function has an implicit "inactive" argument.
674 This is the case of most _m predicated functions, but not all.
675 The list will be updated as needed. */
676 bool
677 function_instance::has_inactive_argument () const
678 {
679 if (pred != PRED_m)
680 return false;
681
682 if (mode_suffix_id == MODE_r
683 || base == functions::vcmpeqq
684 || base == functions::vcmpneq
685 || base == functions::vcmpgeq
686 || base == functions::vcmpgtq
687 || base == functions::vcmpleq
688 || base == functions::vcmpltq
689 || base == functions::vcmpcsq
690 || base == functions::vcmphiq
691 || base == functions::vfmaq
692 || base == functions::vfmasq
693 || base == functions::vfmsq
694 || base == functions::vmaxaq
695 || base == functions::vmaxnmaq
696 || base == functions::vminaq
697 || base == functions::vminnmaq
698 || base == functions::vmlaq
699 || base == functions::vmlasq
700 || base == functions::vmovnbq
701 || base == functions::vmovntq
702 || base == functions::vqmovnbq
703 || base == functions::vqmovntq
704 || base == functions::vqmovunbq
705 || base == functions::vqmovuntq
706 || (base == functions::vorrq && mode_suffix_id == MODE_n)
707 || base == functions::vqdmladhq
708 || base == functions::vqdmladhxq
709 || base == functions::vqdmlahq
710 || base == functions::vqdmlashq
711 || base == functions::vqdmlsdhq
712 || base == functions::vqdmlsdhxq
713 || base == functions::vqrdmladhq
714 || base == functions::vqrdmladhxq
715 || base == functions::vqrdmlahq
716 || base == functions::vqrdmlashq
717 || base == functions::vqrdmlsdhq
718 || base == functions::vqrdmlsdhxq
719 || (base == functions::vqrshlq && mode_suffix_id == MODE_n)
720 || base == functions::vqrshrnbq
721 || base == functions::vqrshrntq
722 || base == functions::vqrshrunbq
723 || base == functions::vqrshruntq
724 || base == functions::vqshrnbq
725 || base == functions::vqshrntq
726 || base == functions::vqshrunbq
727 || base == functions::vqshruntq
728 || (base == functions::vrshlq && mode_suffix_id == MODE_n)
729 || base == functions::vrshrnbq
730 || base == functions::vrshrntq
731 || base == functions::vshrnbq
732 || base == functions::vshrntq
733 || base == functions::vsliq
734 || base == functions::vsriq)
735 return false;
736
737 return true;
738 }
739
740 inline hashval_t
741 registered_function_hasher::hash (value_type value)
742 {
743 return value->instance.hash ();
744 }
745
746 inline bool
747 registered_function_hasher::equal (value_type value, const compare_type &key)
748 {
749 return value->instance == key;
750 }
751
752 function_builder::function_builder ()
753 {
754 m_overload_type = build_function_type (void_type_node, void_list_node);
755 m_direct_overloads = lang_GNU_CXX ();
756 gcc_obstack_init (&m_string_obstack);
757 }
758
759 function_builder::~function_builder ()
760 {
761 obstack_free (&m_string_obstack, NULL);
762 }
763
764 /* Add NAME to the end of the function name being built. */
765 void
766 function_builder::append_name (const char *name)
767 {
768 obstack_grow (&m_string_obstack, name, strlen (name));
769 }
770
771 /* Zero-terminate and complete the function name being built. */
772 char *
773 function_builder::finish_name ()
774 {
775 obstack_1grow (&m_string_obstack, 0);
776 return (char *) obstack_finish (&m_string_obstack);
777 }
778
779 /* Return the overloaded or full function name for INSTANCE, with optional
780 prefix; PRESERVE_USER_NAMESPACE selects the prefix, and OVERLOADED_P
781 selects which the overloaded or full function name. Allocate the string on
782 m_string_obstack; the caller must use obstack_free to free it after use. */
783 char *
784 function_builder::get_name (const function_instance &instance,
785 bool preserve_user_namespace,
786 bool overloaded_p)
787 {
788 if (preserve_user_namespace)
789 append_name ("__arm_");
790 append_name (instance.base_name);
791 append_name (pred_suffixes[instance.pred]);
792 if (!overloaded_p
793 || instance.shape->explicit_mode_suffix_p (instance.pred,
794 instance.mode_suffix_id))
795 append_name (instance.mode_suffix ().string);
796 for (unsigned int i = 0; i < 2; ++i)
797 if (!overloaded_p
798 || instance.shape->explicit_type_suffix_p (i, instance.pred,
799 instance.mode_suffix_id))
800 append_name (instance.type_suffix (i).string);
801 return finish_name ();
802 }
803
804 /* Add attribute NAME to ATTRS. */
805 static tree
806 add_attribute (const char *name, tree attrs)
807 {
808 return tree_cons (get_identifier (name), NULL_TREE, attrs);
809 }
810
811 /* Return the appropriate function attributes for INSTANCE. */
812 tree
813 function_builder::get_attributes (const function_instance &instance)
814 {
815 tree attrs = NULL_TREE;
816
817 if (!instance.modifies_global_state_p ())
818 {
819 if (instance.reads_global_state_p ())
820 attrs = add_attribute ("pure", attrs);
821 else
822 attrs = add_attribute ("const", attrs);
823 }
824
825 if (!flag_non_call_exceptions || !instance.could_trap_p ())
826 attrs = add_attribute ("nothrow", attrs);
827
828 return add_attribute ("leaf", attrs);
829 }
830
831 /* Add a function called NAME with type FNTYPE and attributes ATTRS.
832 INSTANCE describes what the function does and OVERLOADED_P indicates
833 whether it is overloaded. REQUIRES_FLOAT indicates whether the function
834 requires the mve.fp extension. */
835 registered_function &
836 function_builder::add_function (const function_instance &instance,
837 const char *name, tree fntype, tree attrs,
838 bool requires_float,
839 bool overloaded_p,
840 bool placeholder_p)
841 {
842 unsigned int code = vec_safe_length (registered_functions);
843 code = (code << ARM_BUILTIN_SHIFT) | ARM_BUILTIN_MVE;
844
845 /* We need to be able to generate placeholders to ensure that we have a
846 consistent numbering scheme for function codes between the C and C++
847 frontends, so that everything ties up in LTO.
848
849 Currently, tree-streamer-in.cc:unpack_ts_function_decl_value_fields
850 validates that tree nodes returned by TARGET_BUILTIN_DECL are non-NULL and
851 some node other than error_mark_node. This is a holdover from when builtin
852 decls were streamed by code rather than by value.
853
854 Ultimately, we should be able to remove this validation of BUILT_IN_MD
855 nodes and remove the target hook. For now, however, we need to appease the
856 validation and return a non-NULL, non-error_mark_node node, so we
857 arbitrarily choose integer_zero_node. */
858 tree decl = placeholder_p
859 ? integer_zero_node
860 : simulate_builtin_function_decl (input_location, name, fntype,
861 code, NULL, attrs);
862 registered_function &rfn = *ggc_alloc <registered_function> ();
863 rfn.instance = instance;
864 rfn.decl = decl;
865 rfn.requires_float = requires_float;
866 rfn.overloaded_p = overloaded_p;
867 vec_safe_push (registered_functions, &rfn);
868
869 return rfn;
870 }
871
872 /* Add a built-in function for INSTANCE, with the argument types given
873 by ARGUMENT_TYPES and the return type given by RETURN_TYPE.
874 REQUIRES_FLOAT indicates whether the function requires the mve.fp extension,
875 and PRESERVE_USER_NAMESPACE indicates whether the function should also be
876 registered under its non-prefixed name. */
877 void
878 function_builder::add_unique_function (const function_instance &instance,
879 tree return_type,
880 vec<tree> &argument_types,
881 bool preserve_user_namespace,
882 bool requires_float,
883 bool force_direct_overloads)
884 {
885 /* Add the function under its full (unique) name with prefix. */
886 char *name = get_name (instance, true, false);
887 tree fntype = build_function_type_array (return_type,
888 argument_types.length (),
889 argument_types.address ());
890 tree attrs = get_attributes (instance);
891 registered_function &rfn = add_function (instance, name, fntype, attrs,
892 requires_float, false, false);
893
894 /* Enter the function into the hash table. */
895 hashval_t hash = instance.hash ();
896 registered_function **rfn_slot
897 = function_table->find_slot_with_hash (instance, hash, INSERT);
898 gcc_assert (!*rfn_slot);
899 *rfn_slot = &rfn;
900
901 /* Also add the non-prefixed non-overloaded function, as placeholder
902 if the user namespace does not need to be preserved. */
903 char *noprefix_name = get_name (instance, false, false);
904 attrs = get_attributes (instance);
905 add_function (instance, noprefix_name, fntype, attrs, requires_float,
906 false, preserve_user_namespace);
907
908 /* Also add the function under its overloaded alias, if we want
909 a separate decl for each instance of an overloaded function. */
910 char *overload_name = get_name (instance, true, true);
911 if (strcmp (name, overload_name) != 0)
912 {
913 /* Attribute lists shouldn't be shared. */
914 attrs = get_attributes (instance);
915 bool placeholder_p = !(m_direct_overloads || force_direct_overloads);
916 add_function (instance, overload_name, fntype, attrs,
917 requires_float, false, placeholder_p);
918
919 /* Also add the non-prefixed overloaded function, as placeholder
920 if the user namespace does not need to be preserved. */
921 char *noprefix_overload_name = get_name (instance, false, true);
922 attrs = get_attributes (instance);
923 add_function (instance, noprefix_overload_name, fntype, attrs,
924 requires_float, false, preserve_user_namespace || placeholder_p);
925 }
926
927 obstack_free (&m_string_obstack, name);
928 }
929
930 /* Add one function decl for INSTANCE, to be used with manual overload
931 resolution. REQUIRES_FLOAT indicates whether the function requires the
932 mve.fp extension.
933
934 For simplicity, partition functions by instance and required extensions,
935 and check whether the required extensions are available as part of resolving
936 the function to the relevant unique function. */
937 void
938 function_builder::add_overloaded_function (const function_instance &instance,
939 bool preserve_user_namespace,
940 bool requires_float)
941 {
942 char *name = get_name (instance, true, true);
943 if (registered_function **map_value = m_overload_names.get (name))
944 {
945 gcc_assert ((*map_value)->instance == instance);
946 obstack_free (&m_string_obstack, name);
947 }
948 else
949 {
950 registered_function &rfn
951 = add_function (instance, name, m_overload_type, NULL_TREE,
952 requires_float, true, m_direct_overloads);
953 m_overload_names.put (name, &rfn);
954
955 /* Also add the non-prefixed function, as placeholder if the
956 user namespace does not need to be preserved. */
957 char *noprefix_name = get_name (instance, false, true);
958 registered_function &noprefix_rfn
959 = add_function (instance, noprefix_name, m_overload_type,
960 NULL_TREE, requires_float, true,
961 preserve_user_namespace || m_direct_overloads);
962 m_overload_names.put (noprefix_name, &noprefix_rfn);
963 }
964 }
965
966 /* If we are using manual overload resolution, add one function decl
967 for each overloaded function in GROUP. Take the function base name
968 from GROUP and the mode from MODE. */
969 void
970 function_builder::add_overloaded_functions (const function_group_info &group,
971 mode_suffix_index mode,
972 bool preserve_user_namespace)
973 {
974 for (unsigned int pi = 0; group.preds[pi] != NUM_PREDS; ++pi)
975 {
976 unsigned int explicit_type0
977 = (*group.shape)->explicit_type_suffix_p (0, group.preds[pi], mode);
978 unsigned int explicit_type1
979 = (*group.shape)->explicit_type_suffix_p (1, group.preds[pi], mode);
980
981 if ((*group.shape)->skip_overload_p (group.preds[pi], mode))
982 continue;
983
984 if (!explicit_type0 && !explicit_type1)
985 {
986 /* Deal with the common case in which there is one overloaded
987 function for all type combinations. */
988 function_instance instance (group.base_name, *group.base,
989 *group.shape, mode, types_none[0],
990 group.preds[pi]);
991 add_overloaded_function (instance, preserve_user_namespace,
992 group.requires_float);
993 }
994 else
995 for (unsigned int ti = 0; group.types[ti][0] != NUM_TYPE_SUFFIXES;
996 ++ti)
997 {
998 /* Stub out the types that are determined by overload
999 resolution. */
1000 type_suffix_pair types = {
1001 explicit_type0 ? group.types[ti][0] : NUM_TYPE_SUFFIXES,
1002 explicit_type1 ? group.types[ti][1] : NUM_TYPE_SUFFIXES
1003 };
1004 function_instance instance (group.base_name, *group.base,
1005 *group.shape, mode, types,
1006 group.preds[pi]);
1007 add_overloaded_function (instance, preserve_user_namespace,
1008 group.requires_float);
1009 }
1010 }
1011 }
1012
1013 /* Register all the functions in GROUP. */
1014 void
1015 function_builder::register_function_group (const function_group_info &group,
1016 bool preserve_user_namespace)
1017 {
1018 (*group.shape)->build (*this, group, preserve_user_namespace);
1019 }
1020
1021 function_call_info::function_call_info (location_t location_in,
1022 const function_instance &instance_in,
1023 tree fndecl_in)
1024 : function_instance (instance_in), location (location_in), fndecl (fndecl_in)
1025 {
1026 }
1027
1028 function_resolver::function_resolver (location_t location,
1029 const function_instance &instance,
1030 tree fndecl, vec<tree, va_gc> &arglist)
1031 : function_call_info (location, instance, fndecl), m_arglist (arglist)
1032 {
1033 }
1034
1035 /* Return the vector type associated with type suffix TYPE. */
1036 tree
1037 function_resolver::get_vector_type (type_suffix_index type)
1038 {
1039 return acle_vector_types[0][type_suffixes[type].vector_type];
1040 }
1041
1042 /* Return the <stdint.h> name associated with TYPE. Using the <stdint.h>
1043 name should be more user-friendly than the underlying canonical type,
1044 since it makes the signedness and bitwidth explicit. */
1045 const char *
1046 function_resolver::get_scalar_type_name (type_suffix_index type)
1047 {
1048 return vector_types[type_suffixes[type].vector_type].acle_name + 2;
1049 }
1050
1051 /* Return the type of argument I, or error_mark_node if it isn't
1052 well-formed. */
1053 tree
1054 function_resolver::get_argument_type (unsigned int i)
1055 {
1056 tree arg = m_arglist[i];
1057 return arg == error_mark_node ? arg : TREE_TYPE (arg);
1058 }
1059
1060 /* Return true if argument I is some form of scalar value. */
1061 bool
1062 function_resolver::scalar_argument_p (unsigned int i)
1063 {
1064 tree type = get_argument_type (i);
1065 return (INTEGRAL_TYPE_P (type)
1066 /* Allow pointer types, leaving the frontend to warn where
1067 necessary. */
1068 || POINTER_TYPE_P (type)
1069 || SCALAR_FLOAT_TYPE_P (type));
1070 }
1071
1072 /* Report that the function has no form that takes type suffix TYPE.
1073 Return error_mark_node. */
1074 tree
1075 function_resolver::report_no_such_form (type_suffix_index type)
1076 {
1077 error_at (location, "%qE has no form that takes %qT arguments",
1078 fndecl, get_vector_type (type));
1079 return error_mark_node;
1080 }
1081
1082 /* Silently check whether there is an instance of the function with the
1083 mode suffix given by MODE and the type suffixes given by TYPE0 and TYPE1.
1084 Return its function decl if so, otherwise return null. */
1085 tree
1086 function_resolver::lookup_form (mode_suffix_index mode,
1087 type_suffix_index type0,
1088 type_suffix_index type1)
1089 {
1090 type_suffix_pair types = { type0, type1 };
1091 function_instance instance (base_name, base, shape, mode, types, pred);
1092 registered_function *rfn
1093 = function_table->find_with_hash (instance, instance.hash ());
1094 return rfn ? rfn->decl : NULL_TREE;
1095 }
1096
1097 /* Resolve the function to one with the mode suffix given by MODE and the
1098 type suffixes given by TYPE0 and TYPE1. Return its function decl on
1099 success, otherwise report an error and return error_mark_node. */
1100 tree
1101 function_resolver::resolve_to (mode_suffix_index mode,
1102 type_suffix_index type0,
1103 type_suffix_index type1)
1104 {
1105 tree res = lookup_form (mode, type0, type1);
1106 if (!res)
1107 {
1108 if (type1 == NUM_TYPE_SUFFIXES)
1109 return report_no_such_form (type0);
1110 if (type0 == type_suffix_ids[0])
1111 return report_no_such_form (type1);
1112 /* To be filled in when we have other cases. */
1113 gcc_unreachable ();
1114 }
1115 return res;
1116 }
1117
1118 /* Require argument ARGNO to be a single vector or a tuple of NUM_VECTORS
1119 vectors; NUM_VECTORS is 1 for the former. Return the associated type
1120 suffix on success, using TYPE_SUFFIX_b for predicates. Report an error
1121 and return NUM_TYPE_SUFFIXES on failure. */
1122 type_suffix_index
1123 function_resolver::infer_vector_or_tuple_type (unsigned int argno,
1124 unsigned int num_vectors)
1125 {
1126 tree actual = get_argument_type (argno);
1127 if (actual == error_mark_node)
1128 return NUM_TYPE_SUFFIXES;
1129
1130 /* A linear search should be OK here, since the code isn't hot and
1131 the number of types is only small. */
1132 for (unsigned int size_i = 0; size_i < MAX_TUPLE_SIZE; ++size_i)
1133 for (unsigned int suffix_i = 0; suffix_i < NUM_TYPE_SUFFIXES; ++suffix_i)
1134 {
1135 vector_type_index type_i = type_suffixes[suffix_i].vector_type;
1136 tree type = acle_vector_types[size_i][type_i];
1137 if (type && matches_type_p (type, actual))
1138 {
1139 if (size_i + 1 == num_vectors)
1140 return type_suffix_index (suffix_i);
1141
1142 if (num_vectors == 1)
1143 error_at (location, "passing %qT to argument %d of %qE, which"
1144 " expects a single MVE vector rather than a tuple",
1145 actual, argno + 1, fndecl);
1146 else if (size_i == 0 && type_i != VECTOR_TYPE_mve_pred16_t)
1147 /* num_vectors is always != 1, so the singular isn't needed. */
1148 error_n (location, num_vectors, "%qT%d%qE%d",
1149 "passing single vector %qT to argument %d"
1150 " of %qE, which expects a tuple of %d vectors",
1151 actual, argno + 1, fndecl, num_vectors);
1152 else
1153 /* num_vectors is always != 1, so the singular isn't needed. */
1154 error_n (location, num_vectors, "%qT%d%qE%d",
1155 "passing %qT to argument %d of %qE, which"
1156 " expects a tuple of %d vectors", actual, argno + 1,
1157 fndecl, num_vectors);
1158 return NUM_TYPE_SUFFIXES;
1159 }
1160 }
1161
1162 if (num_vectors == 1)
1163 error_at (location, "passing %qT to argument %d of %qE, which"
1164 " expects an MVE vector type", actual, argno + 1, fndecl);
1165 else
1166 error_at (location, "passing %qT to argument %d of %qE, which"
1167 " expects an MVE tuple type", actual, argno + 1, fndecl);
1168 return NUM_TYPE_SUFFIXES;
1169 }
1170
1171 /* Require argument ARGNO to have some form of vector type. Return the
1172 associated type suffix on success, using TYPE_SUFFIX_b for predicates.
1173 Report an error and return NUM_TYPE_SUFFIXES on failure. */
1174 type_suffix_index
1175 function_resolver::infer_vector_type (unsigned int argno)
1176 {
1177 return infer_vector_or_tuple_type (argno, 1);
1178 }
1179
1180 /* Require argument ARGNO to be a vector or scalar argument. Return true
1181 if it is, otherwise report an appropriate error. */
1182 bool
1183 function_resolver::require_vector_or_scalar_type (unsigned int argno)
1184 {
1185 tree actual = get_argument_type (argno);
1186 if (actual == error_mark_node)
1187 return false;
1188
1189 if (!scalar_argument_p (argno) && !VECTOR_TYPE_P (actual))
1190 {
1191 error_at (location, "passing %qT to argument %d of %qE, which"
1192 " expects a vector or scalar type", actual, argno + 1, fndecl);
1193 return false;
1194 }
1195
1196 return true;
1197 }
1198
1199 /* Require argument ARGNO to have vector type TYPE, in cases where this
1200 requirement holds for all uses of the function. Return true if the
1201 argument has the right form, otherwise report an appropriate error. */
1202 bool
1203 function_resolver::require_vector_type (unsigned int argno,
1204 vector_type_index type)
1205 {
1206 tree expected = acle_vector_types[0][type];
1207 tree actual = get_argument_type (argno);
1208 if (actual == error_mark_node)
1209 return false;
1210
1211 if (!matches_type_p (expected, actual))
1212 {
1213 error_at (location, "passing %qT to argument %d of %qE, which"
1214 " expects %qT", actual, argno + 1, fndecl, expected);
1215 return false;
1216 }
1217 return true;
1218 }
1219
1220 /* Like require_vector_type, but TYPE is inferred from previous arguments
1221 rather than being a fixed part of the function signature. This changes
1222 the nature of the error messages. */
1223 bool
1224 function_resolver::require_matching_vector_type (unsigned int argno,
1225 type_suffix_index type)
1226 {
1227 type_suffix_index new_type = infer_vector_type (argno);
1228 if (new_type == NUM_TYPE_SUFFIXES)
1229 return false;
1230
1231 if (type != new_type)
1232 {
1233 error_at (location, "passing %qT to argument %d of %qE, but"
1234 " previous arguments had type %qT",
1235 get_vector_type (new_type), argno + 1, fndecl,
1236 get_vector_type (type));
1237 return false;
1238 }
1239 return true;
1240 }
1241
1242 /* Require argument ARGNO to be a vector type with the following properties:
1243
1244 - the type class must be the same as FIRST_TYPE's if EXPECTED_TCLASS
1245 is SAME_TYPE_CLASS, otherwise it must be EXPECTED_TCLASS itself.
1246
1247 - the element size must be:
1248
1249 - the same as FIRST_TYPE's if EXPECTED_BITS == SAME_SIZE
1250 - half of FIRST_TYPE's if EXPECTED_BITS == HALF_SIZE
1251 - a quarter of FIRST_TYPE's if EXPECTED_BITS == QUARTER_SIZE
1252 - EXPECTED_BITS itself otherwise
1253
1254 Return true if the argument has the required type, otherwise report
1255 an appropriate error.
1256
1257 FIRST_ARGNO is the first argument that is known to have type FIRST_TYPE.
1258 Usually it comes before ARGNO, but sometimes it is more natural to resolve
1259 arguments out of order.
1260
1261 If the required properties depend on FIRST_TYPE then both FIRST_ARGNO and
1262 ARGNO contribute to the resolution process. If the required properties
1263 are fixed, only FIRST_ARGNO contributes to the resolution process.
1264
1265 This function is a bit of a Swiss army knife. The complication comes
1266 from trying to give good error messages when FIRST_ARGNO and ARGNO are
1267 inconsistent, since either of them might be wrong. */
1268 bool function_resolver::
1269 require_derived_vector_type (unsigned int argno,
1270 unsigned int first_argno,
1271 type_suffix_index first_type,
1272 type_class_index expected_tclass,
1273 unsigned int expected_bits)
1274 {
1275 /* If the type needs to match FIRST_ARGNO exactly, use the preferred
1276 error message for that case. The VECTOR_TYPE_P test excludes tuple
1277 types, which we handle below instead. */
1278 bool both_vectors_p = VECTOR_TYPE_P (get_argument_type (first_argno));
1279 if (both_vectors_p
1280 && expected_tclass == SAME_TYPE_CLASS
1281 && expected_bits == SAME_SIZE)
1282 {
1283 /* There's no need to resolve this case out of order. */
1284 gcc_assert (argno > first_argno);
1285 return require_matching_vector_type (argno, first_type);
1286 }
1287
1288 /* Use FIRST_TYPE to get the expected type class and element size. */
1289 type_class_index orig_expected_tclass = expected_tclass;
1290 if (expected_tclass == NUM_TYPE_CLASSES)
1291 expected_tclass = type_suffixes[first_type].tclass;
1292
1293 unsigned int orig_expected_bits = expected_bits;
1294 if (expected_bits == SAME_SIZE)
1295 expected_bits = type_suffixes[first_type].element_bits;
1296 else if (expected_bits == HALF_SIZE)
1297 expected_bits = type_suffixes[first_type].element_bits / 2;
1298 else if (expected_bits == QUARTER_SIZE)
1299 expected_bits = type_suffixes[first_type].element_bits / 4;
1300
1301 /* If the expected type doesn't depend on FIRST_TYPE at all,
1302 just check for the fixed choice of vector type. */
1303 if (expected_tclass == orig_expected_tclass
1304 && expected_bits == orig_expected_bits)
1305 {
1306 const type_suffix_info &expected_suffix
1307 = type_suffixes[find_type_suffix (expected_tclass, expected_bits)];
1308 return require_vector_type (argno, expected_suffix.vector_type);
1309 }
1310
1311 /* Require the argument to be some form of MVE vector type,
1312 without being specific about the type of vector we want. */
1313 type_suffix_index actual_type = infer_vector_type (argno);
1314 if (actual_type == NUM_TYPE_SUFFIXES)
1315 return false;
1316
1317 /* Exit now if we got the right type. */
1318 bool tclass_ok_p = (type_suffixes[actual_type].tclass == expected_tclass);
1319 bool size_ok_p = (type_suffixes[actual_type].element_bits == expected_bits);
1320 if (tclass_ok_p && size_ok_p)
1321 return true;
1322
1323 /* First look for cases in which the actual type contravenes a fixed
1324 size requirement, without having to refer to FIRST_TYPE. */
1325 if (!size_ok_p && expected_bits == orig_expected_bits)
1326 {
1327 error_at (location, "passing %qT to argument %d of %qE, which"
1328 " expects a vector of %d-bit elements",
1329 get_vector_type (actual_type), argno + 1, fndecl,
1330 expected_bits);
1331 return false;
1332 }
1333
1334 /* Likewise for a fixed type class requirement. This is only ever
1335 needed for signed and unsigned types, so don't create unnecessary
1336 translation work for other type classes. */
1337 if (!tclass_ok_p && orig_expected_tclass == TYPE_signed)
1338 {
1339 error_at (location, "passing %qT to argument %d of %qE, which"
1340 " expects a vector of signed integers",
1341 get_vector_type (actual_type), argno + 1, fndecl);
1342 return false;
1343 }
1344 if (!tclass_ok_p && orig_expected_tclass == TYPE_unsigned)
1345 {
1346 error_at (location, "passing %qT to argument %d of %qE, which"
1347 " expects a vector of unsigned integers",
1348 get_vector_type (actual_type), argno + 1, fndecl);
1349 return false;
1350 }
1351
1352 /* Make sure that FIRST_TYPE itself is sensible before using it
1353 as a basis for an error message. */
1354 if (resolve_to (mode_suffix_id, first_type) == error_mark_node)
1355 return false;
1356
1357 /* If the arguments have consistent type classes, but a link between
1358 the sizes has been broken, try to describe the error in those terms. */
1359 if (both_vectors_p && tclass_ok_p && orig_expected_bits == SAME_SIZE)
1360 {
1361 if (argno < first_argno)
1362 {
1363 std::swap (argno, first_argno);
1364 std::swap (actual_type, first_type);
1365 }
1366 error_at (location, "arguments %d and %d of %qE must have the"
1367 " same element size, but the values passed here have type"
1368 " %qT and %qT respectively", first_argno + 1, argno + 1,
1369 fndecl, get_vector_type (first_type),
1370 get_vector_type (actual_type));
1371 return false;
1372 }
1373
1374 /* Likewise in reverse: look for cases in which the sizes are consistent
1375 but a link between the type classes has been broken. */
1376 if (both_vectors_p
1377 && size_ok_p
1378 && orig_expected_tclass == SAME_TYPE_CLASS
1379 && type_suffixes[first_type].integer_p
1380 && type_suffixes[actual_type].integer_p)
1381 {
1382 if (argno < first_argno)
1383 {
1384 std::swap (argno, first_argno);
1385 std::swap (actual_type, first_type);
1386 }
1387 error_at (location, "arguments %d and %d of %qE must have the"
1388 " same signedness, but the values passed here have type"
1389 " %qT and %qT respectively", first_argno + 1, argno + 1,
1390 fndecl, get_vector_type (first_type),
1391 get_vector_type (actual_type));
1392 return false;
1393 }
1394
1395 /* The two arguments are wildly inconsistent. */
1396 type_suffix_index expected_type
1397 = find_type_suffix (expected_tclass, expected_bits);
1398 error_at (location, "passing %qT instead of the expected %qT to argument"
1399 " %d of %qE, after passing %qT to argument %d",
1400 get_vector_type (actual_type), get_vector_type (expected_type),
1401 argno + 1, fndecl, get_argument_type (first_argno),
1402 first_argno + 1);
1403 return false;
1404 }
1405
1406 /* Require argument ARGNO to be a (possibly variable) scalar, expecting it
1407 to have the following properties:
1408
1409 - the type class must be the same as for type suffix 0 if EXPECTED_TCLASS
1410 is SAME_TYPE_CLASS, otherwise it must be EXPECTED_TCLASS itself.
1411
1412 - the element size must be the same as for type suffix 0 if EXPECTED_BITS
1413 is SAME_TYPE_SIZE, otherwise it must be EXPECTED_BITS itself.
1414
1415 Return true if the argument is valid, otherwise report an appropriate error.
1416
1417 Note that we don't check whether the scalar type actually has the required
1418 properties, since that's subject to implicit promotions and conversions.
1419 Instead we just use the expected properties to tune the error message. */
1420 bool function_resolver::
1421 require_derived_scalar_type (unsigned int argno,
1422 type_class_index expected_tclass,
1423 unsigned int expected_bits)
1424 {
1425 gcc_assert (expected_tclass == SAME_TYPE_CLASS
1426 || expected_tclass == TYPE_signed
1427 || expected_tclass == TYPE_unsigned);
1428
1429 /* If the expected type doesn't depend on the type suffix at all,
1430 just check for the fixed choice of scalar type. */
1431 if (expected_tclass != SAME_TYPE_CLASS && expected_bits != SAME_SIZE)
1432 {
1433 type_suffix_index expected_type
1434 = find_type_suffix (expected_tclass, expected_bits);
1435 return require_scalar_type (argno, get_scalar_type_name (expected_type));
1436 }
1437
1438 if (scalar_argument_p (argno))
1439 return true;
1440
1441 if (expected_tclass == SAME_TYPE_CLASS)
1442 /* It doesn't really matter whether the element is expected to be
1443 the same size as type suffix 0. */
1444 error_at (location, "passing %qT to argument %d of %qE, which"
1445 " expects a scalar element", get_argument_type (argno),
1446 argno + 1, fndecl);
1447 else
1448 /* It doesn't seem useful to distinguish between signed and unsigned
1449 scalars here. */
1450 error_at (location, "passing %qT to argument %d of %qE, which"
1451 " expects a scalar integer", get_argument_type (argno),
1452 argno + 1, fndecl);
1453 return false;
1454 }
1455
1456 /* Require argument ARGNO to be suitable for an integer constant expression.
1457 Return true if it is, otherwise report an appropriate error.
1458
1459 function_checker checks whether the argument is actually constant and
1460 has a suitable range. The reason for distinguishing immediate arguments
1461 here is because it provides more consistent error messages than
1462 require_scalar_type would. */
1463 bool
1464 function_resolver::require_integer_immediate (unsigned int argno)
1465 {
1466 if (!scalar_argument_p (argno))
1467 {
1468 report_non_ice (location, fndecl, argno);
1469 return false;
1470 }
1471 return true;
1472 }
1473
1474 /* Require argument ARGNO to be a (possibly variable) scalar, using EXPECTED
1475 as the name of its expected type. Return true if the argument has the
1476 right form, otherwise report an appropriate error. */
1477 bool
1478 function_resolver::require_scalar_type (unsigned int argno,
1479 const char *expected)
1480 {
1481 if (!scalar_argument_p (argno))
1482 {
1483 error_at (location, "passing %qT to argument %d of %qE, which"
1484 " expects %qs", get_argument_type (argno), argno + 1,
1485 fndecl, expected);
1486 return false;
1487 }
1488 return true;
1489 }
1490
1491 /* Require the function to have exactly EXPECTED arguments. Return true
1492 if it does, otherwise report an appropriate error. */
1493 bool
1494 function_resolver::check_num_arguments (unsigned int expected)
1495 {
1496 if (m_arglist.length () < expected)
1497 error_at (location, "too few arguments to function %qE", fndecl);
1498 else if (m_arglist.length () > expected)
1499 error_at (location, "too many arguments to function %qE", fndecl);
1500 return m_arglist.length () == expected;
1501 }
1502
1503 /* If the function is predicated, check that the last argument is a
1504 suitable predicate. Also check that there are NOPS further
1505 arguments before any predicate, but don't check what they are.
1506
1507 Return true on success, otherwise report a suitable error.
1508 When returning true:
1509
1510 - set I to the number of the last unchecked argument.
1511 - set NARGS to the total number of arguments. */
1512 bool
1513 function_resolver::check_gp_argument (unsigned int nops,
1514 unsigned int &i, unsigned int &nargs)
1515 {
1516 i = nops - 1;
1517 if (pred != PRED_none)
1518 {
1519 switch (pred)
1520 {
1521 case PRED_m:
1522 /* Add first inactive argument if needed, and final predicate. */
1523 if (has_inactive_argument ())
1524 nargs = nops + 2;
1525 else
1526 nargs = nops + 1;
1527 break;
1528
1529 case PRED_p:
1530 case PRED_x:
1531 /* Add final predicate. */
1532 nargs = nops + 1;
1533 break;
1534
1535 default:
1536 gcc_unreachable ();
1537 }
1538
1539 if (!check_num_arguments (nargs)
1540 || !require_vector_type (nargs - 1, VECTOR_TYPE_mve_pred16_t))
1541 return false;
1542
1543 i = nargs - 2;
1544 }
1545 else
1546 {
1547 nargs = nops;
1548 if (!check_num_arguments (nargs))
1549 return false;
1550 }
1551
1552 return true;
1553 }
1554
1555 /* Finish resolving a function whose final argument can be a vector
1556 or a scalar, with the function having an implicit "_n" suffix
1557 in the latter case. This "_n" form might only exist for certain
1558 type suffixes.
1559
1560 ARGNO is the index of the final argument. The inferred type suffix
1561 was obtained from argument FIRST_ARGNO, which has type FIRST_TYPE.
1562 EXPECTED_TCLASS and EXPECTED_BITS describe the expected properties
1563 of the final vector or scalar argument, in the same way as for
1564 require_derived_vector_type. INFERRED_TYPE is the inferred type
1565 suffix itself, or NUM_TYPE_SUFFIXES if it's the same as FIRST_TYPE.
1566
1567 Return the function decl of the resolved function on success,
1568 otherwise report a suitable error and return error_mark_node. */
1569 tree function_resolver::
1570 finish_opt_n_resolution (unsigned int argno, unsigned int first_argno,
1571 type_suffix_index first_type,
1572 type_class_index expected_tclass,
1573 unsigned int expected_bits,
1574 type_suffix_index inferred_type)
1575 {
1576 if (inferred_type == NUM_TYPE_SUFFIXES)
1577 inferred_type = first_type;
1578 mode_suffix_index scalar_mode = MODE_n;
1579 if (mode_suffix_id == MODE_r)
1580 scalar_mode = MODE_r;
1581 tree scalar_form = lookup_form (scalar_mode, inferred_type);
1582
1583 /* Allow the final argument to be scalar, if an _n form exists. */
1584 if (scalar_argument_p (argno))
1585 {
1586 if (scalar_form)
1587 return scalar_form;
1588
1589 /* Check the vector form normally. If that succeeds, raise an
1590 error about having no corresponding _n form. */
1591 tree res = resolve_to (mode_suffix_id, inferred_type);
1592 if (res != error_mark_node)
1593 error_at (location, "passing %qT to argument %d of %qE, but its"
1594 " %qT form does not accept scalars",
1595 get_argument_type (argno), argno + 1, fndecl,
1596 get_vector_type (first_type));
1597 return error_mark_node;
1598 }
1599
1600 /* If an _n form does exist, provide a more accurate message than
1601 require_derived_vector_type would for arguments that are neither
1602 vectors nor scalars. */
1603 if (scalar_form && !require_vector_or_scalar_type (argno))
1604 return error_mark_node;
1605
1606 /* Check for the correct vector type. */
1607 if (!require_derived_vector_type (argno, first_argno, first_type,
1608 expected_tclass, expected_bits))
1609 return error_mark_node;
1610
1611 return resolve_to (mode_suffix_id, inferred_type);
1612 }
1613
1614 /* Resolve a (possibly predicated) unary function. If the function uses
1615 merge predication or if TREAT_AS_MERGE_P is true, there is an extra
1616 vector argument before the governing predicate that specifies the
1617 values of inactive elements. This argument has the following
1618 properties:
1619
1620 - the type class must be the same as for active elements if MERGE_TCLASS
1621 is SAME_TYPE_CLASS, otherwise it must be MERGE_TCLASS itself.
1622
1623 - the element size must be the same as for active elements if MERGE_BITS
1624 is SAME_TYPE_SIZE, otherwise it must be MERGE_BITS itself.
1625
1626 Return the function decl of the resolved function on success,
1627 otherwise report a suitable error and return error_mark_node. */
1628 tree
1629 function_resolver::resolve_unary (type_class_index merge_tclass,
1630 unsigned int merge_bits,
1631 bool treat_as_merge_p)
1632 {
1633 type_suffix_index type;
1634 if (pred == PRED_m || treat_as_merge_p)
1635 {
1636 if (!check_num_arguments (3))
1637 return error_mark_node;
1638 if (merge_tclass == SAME_TYPE_CLASS && merge_bits == SAME_SIZE)
1639 {
1640 /* The inactive elements are the same as the active elements,
1641 so we can use normal left-to-right resolution. */
1642 if ((type = infer_vector_type (0)) == NUM_TYPE_SUFFIXES
1643 /* Predicates are the last argument. */
1644 || !require_vector_type (2 , VECTOR_TYPE_mve_pred16_t)
1645 || !require_matching_vector_type (1 , type))
1646 return error_mark_node;
1647 }
1648 else
1649 {
1650 /* The inactive element type is a function of the active one,
1651 so resolve the active one first. */
1652 if (!require_vector_type (1, VECTOR_TYPE_mve_pred16_t)
1653 || (type = infer_vector_type (2)) == NUM_TYPE_SUFFIXES
1654 || !require_derived_vector_type (0, 2, type, merge_tclass,
1655 merge_bits))
1656 return error_mark_node;
1657 }
1658 }
1659 else
1660 {
1661 /* We just need to check the predicate (if any) and the single
1662 vector argument. */
1663 unsigned int i, nargs;
1664 if (!check_gp_argument (1, i, nargs)
1665 || (type = infer_vector_type (i)) == NUM_TYPE_SUFFIXES)
1666 return error_mark_node;
1667 }
1668
1669 /* Handle convert-like functions in which the first type suffix is
1670 explicit. */
1671 if (type_suffix_ids[0] != NUM_TYPE_SUFFIXES)
1672 return resolve_to (mode_suffix_id, type_suffix_ids[0], type);
1673
1674 return resolve_to (mode_suffix_id, type);
1675 }
1676
1677 /* Resolve a (possibly predicated) unary function taking a scalar
1678 argument (_n suffix). If the function uses merge predication,
1679 there is an extra vector argument in the first position, and the
1680 final governing predicate that specifies the values of inactive
1681 elements.
1682
1683 Return the function decl of the resolved function on success,
1684 otherwise report a suitable error and return error_mark_node. */
1685 tree
1686 function_resolver::resolve_unary_n ()
1687 {
1688 type_suffix_index type;
1689
1690 /* Currently only support overrides for _m (vdupq). */
1691 if (pred != PRED_m)
1692 return error_mark_node;
1693
1694 if (pred == PRED_m)
1695 {
1696 if (!check_num_arguments (3))
1697 return error_mark_node;
1698
1699 /* The inactive elements are the same as the active elements,
1700 so we can use normal left-to-right resolution. */
1701 if ((type = infer_vector_type (0)) == NUM_TYPE_SUFFIXES
1702 /* Predicates are the last argument. */
1703 || !require_vector_type (2 , VECTOR_TYPE_mve_pred16_t))
1704 return error_mark_node;
1705 }
1706
1707 /* Make sure the argument is scalar. */
1708 tree scalar_form = lookup_form (MODE_n, type);
1709
1710 if (scalar_argument_p (1) && scalar_form)
1711 return scalar_form;
1712
1713 return error_mark_node;
1714 }
1715
1716 /* Resolve a (possibly predicated) function that takes NOPS like-typed
1717 vector arguments followed by NIMM integer immediates. Return the
1718 function decl of the resolved function on success, otherwise report
1719 a suitable error and return error_mark_node. */
1720 tree
1721 function_resolver::resolve_uniform (unsigned int nops, unsigned int nimm)
1722 {
1723 unsigned int i, nargs;
1724 type_suffix_index type;
1725 if (!check_gp_argument (nops + nimm, i, nargs)
1726 || (type = infer_vector_type (0 )) == NUM_TYPE_SUFFIXES)
1727 return error_mark_node;
1728
1729 unsigned int last_arg = i + 1 - nimm;
1730 for (i = 0; i < last_arg; i++)
1731 if (!require_matching_vector_type (i, type))
1732 return error_mark_node;
1733
1734 for (i = last_arg; i < nargs; ++i)
1735 if (!require_integer_immediate (i))
1736 return error_mark_node;
1737
1738 return resolve_to (mode_suffix_id, type);
1739 }
1740
1741 /* Resolve a (possibly predicated) function that offers a choice between
1742 taking:
1743
1744 - NOPS like-typed vector arguments or
1745 - NOPS - 1 like-typed vector arguments followed by a scalar argument
1746
1747 Return the function decl of the resolved function on success,
1748 otherwise report a suitable error and return error_mark_node. */
1749 tree
1750 function_resolver::resolve_uniform_opt_n (unsigned int nops)
1751 {
1752 unsigned int i, nargs;
1753 type_suffix_index type;
1754 if (!check_gp_argument (nops, i, nargs)
1755 /* Unary operators should use resolve_unary, so using i - 1 is
1756 safe. */
1757 || (type = infer_vector_type (i - 1)) == NUM_TYPE_SUFFIXES)
1758 return error_mark_node;
1759
1760 /* Skip last argument, may be scalar. */
1761 unsigned int last_arg = i;
1762 for (i = 0; i < last_arg; i++)
1763 if (!require_matching_vector_type (i, type))
1764 return error_mark_node;
1765
1766 return finish_opt_n_resolution (last_arg, 0, type);
1767 }
1768
1769 /* If the call is erroneous, report an appropriate error and return
1770 error_mark_node. Otherwise, if the function is overloaded, return
1771 the decl of the non-overloaded function. Return NULL_TREE otherwise,
1772 indicating that the call should be processed in the normal way. */
1773 tree
1774 function_resolver::resolve ()
1775 {
1776 return shape->resolve (*this);
1777 }
1778
1779 function_checker::function_checker (location_t location,
1780 const function_instance &instance,
1781 tree fndecl, tree fntype,
1782 unsigned int nargs, tree *args)
1783 : function_call_info (location, instance, fndecl),
1784 m_fntype (fntype), m_nargs (nargs), m_args (args)
1785 {
1786 if (instance.has_inactive_argument ())
1787 m_base_arg = 1;
1788 else
1789 m_base_arg = 0;
1790 }
1791
1792 /* Return true if argument ARGNO exists. which it might not for
1793 erroneous calls. It is safe to wave through checks if this
1794 function returns false. */
1795 bool
1796 function_checker::argument_exists_p (unsigned int argno)
1797 {
1798 gcc_assert (argno < (unsigned int) type_num_arguments (m_fntype));
1799 return argno < m_nargs;
1800 }
1801
1802 /* Check that argument ARGNO is an integer constant expression and
1803 store its value in VALUE_OUT if so. The caller should first
1804 check that argument ARGNO exists. */
1805 bool
1806 function_checker::require_immediate (unsigned int argno,
1807 HOST_WIDE_INT &value_out)
1808 {
1809 gcc_assert (argno < m_nargs);
1810 tree arg = m_args[argno];
1811
1812 /* The type and range are unsigned, so read the argument as an
1813 unsigned rather than signed HWI. */
1814 if (!tree_fits_uhwi_p (arg))
1815 {
1816 report_non_ice (location, fndecl, argno);
1817 return false;
1818 }
1819
1820 /* ...but treat VALUE_OUT as signed for error reporting, since printing
1821 -1 is more user-friendly than the maximum uint64_t value. */
1822 value_out = tree_to_uhwi (arg);
1823 return true;
1824 }
1825
1826 /* Check that argument REL_ARGNO is an integer constant expression that has
1827 a valid value for enumeration type TYPE. REL_ARGNO counts from the end
1828 of the predication arguments. */
1829 bool
1830 function_checker::require_immediate_enum (unsigned int rel_argno, tree type)
1831 {
1832 unsigned int argno = m_base_arg + rel_argno;
1833 if (!argument_exists_p (argno))
1834 return true;
1835
1836 HOST_WIDE_INT actual;
1837 if (!require_immediate (argno, actual))
1838 return false;
1839
1840 for (tree entry = TYPE_VALUES (type); entry; entry = TREE_CHAIN (entry))
1841 {
1842 /* The value is an INTEGER_CST for C and a CONST_DECL wrapper
1843 around an INTEGER_CST for C++. */
1844 tree value = TREE_VALUE (entry);
1845 if (TREE_CODE (value) == CONST_DECL)
1846 value = DECL_INITIAL (value);
1847 if (wi::to_widest (value) == actual)
1848 return true;
1849 }
1850
1851 report_not_enum (location, fndecl, argno, actual, type);
1852 return false;
1853 }
1854
1855 /* Check that argument REL_ARGNO is an integer constant expression in the
1856 range [MIN, MAX]. REL_ARGNO counts from the end of the predication
1857 arguments. */
1858 bool
1859 function_checker::require_immediate_range (unsigned int rel_argno,
1860 HOST_WIDE_INT min,
1861 HOST_WIDE_INT max)
1862 {
1863 unsigned int argno = m_base_arg + rel_argno;
1864 if (!argument_exists_p (argno))
1865 return true;
1866
1867 /* Required because of the tree_to_uhwi -> HOST_WIDE_INT conversion
1868 in require_immediate. */
1869 gcc_assert (min >= 0 && min <= max);
1870 HOST_WIDE_INT actual;
1871 if (!require_immediate (argno, actual))
1872 return false;
1873
1874 if (!IN_RANGE (actual, min, max))
1875 {
1876 report_out_of_range (location, fndecl, argno, actual, min, max);
1877 return false;
1878 }
1879
1880 return true;
1881 }
1882
1883 /* Perform semantic checks on the call. Return true if the call is valid,
1884 otherwise report a suitable error. */
1885 bool
1886 function_checker::check ()
1887 {
1888 function_args_iterator iter;
1889 tree type;
1890 unsigned int i = 0;
1891 FOREACH_FUNCTION_ARGS (m_fntype, type, iter)
1892 {
1893 if (type == void_type_node || i >= m_nargs)
1894 break;
1895
1896 if (i >= m_base_arg
1897 && TREE_CODE (type) == ENUMERAL_TYPE
1898 && !require_immediate_enum (i - m_base_arg, type))
1899 return false;
1900
1901 i += 1;
1902 }
1903
1904 return shape->check (*this);
1905 }
1906
1907 gimple_folder::gimple_folder (const function_instance &instance, tree fndecl,
1908 gcall *call_in)
1909 : function_call_info (gimple_location (call_in), instance, fndecl),
1910 call (call_in), lhs (gimple_call_lhs (call_in))
1911 {
1912 }
1913
1914 /* Try to fold the call. Return the new statement on success and null
1915 on failure. */
1916 gimple *
1917 gimple_folder::fold ()
1918 {
1919 /* Don't fold anything when MVE is disabled; emit an error during
1920 expansion instead. */
1921 if (!TARGET_HAVE_MVE)
1922 return NULL;
1923
1924 /* Punt if the function has a return type and no result location is
1925 provided. The attributes should allow target-independent code to
1926 remove the calls if appropriate. */
1927 if (!lhs && TREE_TYPE (gimple_call_fntype (call)) != void_type_node)
1928 return NULL;
1929
1930 return base->fold (*this);
1931 }
1932
1933 function_expander::function_expander (const function_instance &instance,
1934 tree fndecl, tree call_expr_in,
1935 rtx possible_target_in)
1936 : function_call_info (EXPR_LOCATION (call_expr_in), instance, fndecl),
1937 call_expr (call_expr_in), possible_target (possible_target_in)
1938 {
1939 }
1940
1941 /* Return the handler of direct optab OP for type suffix SUFFIX_I. */
1942 insn_code
1943 function_expander::direct_optab_handler (optab op, unsigned int suffix_i)
1944 {
1945 return ::direct_optab_handler (op, vector_mode (suffix_i));
1946 }
1947
1948 /* For a function that does the equivalent of:
1949
1950 OUTPUT = COND ? FN (INPUTS) : FALLBACK;
1951
1952 return the value of FALLBACK.
1953
1954 MODE is the mode of OUTPUT.
1955 MERGE_ARGNO is the argument that provides FALLBACK for _m functions,
1956 or DEFAULT_MERGE_ARGNO if we should apply the usual rules.
1957
1958 ARGNO is the caller's index into args. If the returned value is
1959 argument 0 (as for unary _m operations), increment ARGNO past the
1960 returned argument. */
1961 rtx
1962 function_expander::get_fallback_value (machine_mode mode,
1963 unsigned int merge_argno,
1964 unsigned int &argno)
1965 {
1966 if (pred == PRED_z)
1967 return CONST0_RTX (mode);
1968
1969 gcc_assert (pred == PRED_m || pred == PRED_x);
1970
1971 if (merge_argno == 0)
1972 return args[argno++];
1973
1974 return args[merge_argno];
1975 }
1976
1977 /* Return a REG rtx that can be used for the result of the function,
1978 using the preferred target if suitable. */
1979 rtx
1980 function_expander::get_reg_target ()
1981 {
1982 machine_mode target_mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (fndecl)));
1983 if (!possible_target || GET_MODE (possible_target) != target_mode)
1984 possible_target = gen_reg_rtx (target_mode);
1985 return possible_target;
1986 }
1987
1988 /* Add an output operand to the instruction we're building, which has
1989 code ICODE. Bind the output to the preferred target rtx if possible. */
1990 void
1991 function_expander::add_output_operand (insn_code icode)
1992 {
1993 unsigned int opno = m_ops.length ();
1994 machine_mode mode = insn_data[icode].operand[opno].mode;
1995 m_ops.safe_grow (opno + 1, true);
1996 create_output_operand (&m_ops.last (), possible_target, mode);
1997 }
1998
1999 /* Add an input operand to the instruction we're building, which has
2000 code ICODE. Calculate the value of the operand as follows:
2001
2002 - If the operand is a predicate, coerce X to have the
2003 mode that the instruction expects.
2004
2005 - Otherwise use X directly. The expand machinery checks that X has
2006 the right mode for the instruction. */
2007 void
2008 function_expander::add_input_operand (insn_code icode, rtx x)
2009 {
2010 unsigned int opno = m_ops.length ();
2011 const insn_operand_data &operand = insn_data[icode].operand[opno];
2012 machine_mode mode = operand.mode;
2013 if (mode == VOIDmode)
2014 {
2015 /* The only allowable use of VOIDmode is the wildcard
2016 arm_any_register_operand, which is used to avoid
2017 combinatorial explosion in the reinterpret patterns. */
2018 gcc_assert (operand.predicate == arm_any_register_operand);
2019 mode = GET_MODE (x);
2020 }
2021 else if (VALID_MVE_PRED_MODE (mode))
2022 x = gen_lowpart (mode, x);
2023
2024 m_ops.safe_grow (m_ops.length () + 1, true);
2025 create_input_operand (&m_ops.last (), x, mode);
2026 }
2027
2028 /* Add an integer operand with value X to the instruction. */
2029 void
2030 function_expander::add_integer_operand (HOST_WIDE_INT x)
2031 {
2032 m_ops.safe_grow (m_ops.length () + 1, true);
2033 create_integer_operand (&m_ops.last (), x);
2034 }
2035
2036 /* Generate instruction ICODE, given that its operands have already
2037 been added to M_OPS. Return the value of the first operand. */
2038 rtx
2039 function_expander::generate_insn (insn_code icode)
2040 {
2041 expand_insn (icode, m_ops.length (), m_ops.address ());
2042 return function_returns_void_p () ? const0_rtx : m_ops[0].value;
2043 }
2044
2045 /* Implement the call using instruction ICODE, with a 1:1 mapping between
2046 arguments and input operands. */
2047 rtx
2048 function_expander::use_exact_insn (insn_code icode)
2049 {
2050 unsigned int nops = insn_data[icode].n_operands;
2051 if (!function_returns_void_p ())
2052 {
2053 add_output_operand (icode);
2054 nops -= 1;
2055 }
2056 for (unsigned int i = 0; i < nops; ++i)
2057 add_input_operand (icode, args[i]);
2058 return generate_insn (icode);
2059 }
2060
2061 /* Implement the call using instruction ICODE, which does not use a
2062 predicate. */
2063 rtx
2064 function_expander::use_unpred_insn (insn_code icode)
2065 {
2066 gcc_assert (pred == PRED_none);
2067 /* Discount the output operand. */
2068 unsigned int nops = insn_data[icode].n_operands - 1;
2069 unsigned int i = 0;
2070
2071 add_output_operand (icode);
2072 for (; i < nops; ++i)
2073 add_input_operand (icode, args[i]);
2074
2075 return generate_insn (icode);
2076 }
2077
2078 /* Implement the call using instruction ICODE, which is a predicated
2079 operation that returns arbitrary values for inactive lanes. */
2080 rtx
2081 function_expander::use_pred_x_insn (insn_code icode)
2082 {
2083 gcc_assert (pred == PRED_x);
2084 unsigned int nops = args.length ();
2085
2086 add_output_operand (icode);
2087 /* Use first operand as arbitrary inactive input. */
2088 add_input_operand (icode, possible_target);
2089 emit_clobber (possible_target);
2090 /* Copy remaining arguments, including the final predicate. */
2091 for (unsigned int i = 0; i < nops; ++i)
2092 add_input_operand (icode, args[i]);
2093
2094 return generate_insn (icode);
2095 }
2096
2097 /* Implement the call using instruction ICODE, which does the equivalent of:
2098
2099 OUTPUT = COND ? FN (INPUTS) : FALLBACK;
2100
2101 The instruction operands are in the order above: OUTPUT, COND, INPUTS
2102 and FALLBACK. MERGE_ARGNO is the argument that provides FALLBACK for _m
2103 functions, or DEFAULT_MERGE_ARGNO if we should apply the usual rules. */
2104 rtx
2105 function_expander::use_cond_insn (insn_code icode, unsigned int merge_argno)
2106 {
2107 /* At present we never need to handle PRED_none, which would involve
2108 creating a new predicate rather than using one supplied by the user. */
2109 gcc_assert (pred != PRED_none);
2110 /* For MVE, we only handle PRED_m at present. */
2111 gcc_assert (pred == PRED_m);
2112
2113 /* Discount the output, predicate and fallback value. */
2114 unsigned int nops = insn_data[icode].n_operands - 3;
2115 machine_mode mode = insn_data[icode].operand[0].mode;
2116
2117 unsigned int opno = 0;
2118 rtx fallback_arg = NULL_RTX;
2119 fallback_arg = get_fallback_value (mode, merge_argno, opno);
2120 rtx pred_arg = args[nops + 1];
2121
2122 add_output_operand (icode);
2123 add_input_operand (icode, fallback_arg);
2124 for (unsigned int i = 0; i < nops; ++i)
2125 add_input_operand (icode, args[opno + i]);
2126 add_input_operand (icode, pred_arg);
2127 return generate_insn (icode);
2128 }
2129
2130 /* Implement the call using a normal unpredicated optab for PRED_none.
2131
2132 <optab> corresponds to:
2133
2134 - CODE_FOR_SINT for signed integers
2135 - CODE_FOR_UINT for unsigned integers
2136 - CODE_FOR_FP for floating-point values */
2137 rtx
2138 function_expander::map_to_rtx_codes (rtx_code code_for_sint,
2139 rtx_code code_for_uint,
2140 rtx_code code_for_fp)
2141 {
2142 gcc_assert (pred == PRED_none);
2143 rtx_code code = type_suffix (0).integer_p ?
2144 (type_suffix (0).unsigned_p ? code_for_uint : code_for_sint)
2145 : code_for_fp;
2146 insn_code icode = direct_optab_handler (code_to_optab (code), 0);
2147 if (icode == CODE_FOR_nothing)
2148 gcc_unreachable ();
2149
2150 return use_unpred_insn (icode);
2151 }
2152
2153 /* Expand the call and return its lhs. */
2154 rtx
2155 function_expander::expand ()
2156 {
2157 unsigned int nargs = call_expr_nargs (call_expr);
2158 args.reserve (nargs);
2159 for (unsigned int i = 0; i < nargs; ++i)
2160 args.quick_push (expand_normal (CALL_EXPR_ARG (call_expr, i)));
2161
2162 return base->expand (*this);
2163 }
2164
2165 /* If we're implementing manual overloading, check whether the MVE
2166 function with subcode CODE is overloaded, and if so attempt to
2167 determine the corresponding non-overloaded function. The call
2168 occurs at location LOCATION and has the arguments given by ARGLIST.
2169
2170 If the call is erroneous, report an appropriate error and return
2171 error_mark_node. Otherwise, if the function is overloaded, return
2172 the decl of the non-overloaded function. Return NULL_TREE otherwise,
2173 indicating that the call should be processed in the normal way. */
2174 tree
2175 resolve_overloaded_builtin (location_t location, unsigned int code,
2176 vec<tree, va_gc> *arglist)
2177 {
2178 if (code >= vec_safe_length (registered_functions))
2179 return NULL_TREE;
2180
2181 registered_function &rfn = *(*registered_functions)[code];
2182 if (rfn.overloaded_p)
2183 return function_resolver (location, rfn.instance, rfn.decl,
2184 *arglist).resolve ();
2185 return NULL_TREE;
2186 }
2187
2188 /* Perform any semantic checks needed for a call to the MVE function
2189 with subcode CODE, such as testing for integer constant expressions.
2190 The call occurs at location LOCATION and has NARGS arguments,
2191 given by ARGS. FNDECL is the original function decl, before
2192 overload resolution.
2193
2194 Return true if the call is valid, otherwise report a suitable error. */
2195 bool
2196 check_builtin_call (location_t location, vec<location_t>, unsigned int code,
2197 tree fndecl, unsigned int nargs, tree *args)
2198 {
2199 const registered_function &rfn = *(*registered_functions)[code];
2200 if (!check_requires_float (location, rfn.decl, rfn.requires_float))
2201 return false;
2202
2203 return function_checker (location, rfn.instance, fndecl,
2204 TREE_TYPE (rfn.decl), nargs, args).check ();
2205 }
2206
2207 /* Attempt to fold STMT, given that it's a call to the MVE function
2208 with subcode CODE. Return the new statement on success and null
2209 on failure. Insert any other new statements at GSI. */
2210 gimple *
2211 gimple_fold_builtin (unsigned int code, gcall *stmt)
2212 {
2213 registered_function &rfn = *(*registered_functions)[code];
2214 return gimple_folder (rfn.instance, rfn.decl, stmt).fold ();
2215 }
2216
2217 /* Expand a call to the MVE function with subcode CODE. EXP is the call
2218 expression and TARGET is the preferred location for the result.
2219 Return the value of the lhs. */
2220 rtx
2221 expand_builtin (unsigned int code, tree exp, rtx target)
2222 {
2223 registered_function &rfn = *(*registered_functions)[code];
2224 if (!check_requires_float (EXPR_LOCATION (exp), rfn.decl,
2225 rfn.requires_float))
2226 return target;
2227 return function_expander (rfn.instance, rfn.decl, exp, target).expand ();
2228 }
2229
2230 } /* end namespace arm_mve */
2231
2232 using namespace arm_mve;
2233
2234 inline void
2235 gt_ggc_mx (function_instance *)
2236 {
2237 }
2238
2239 inline void
2240 gt_pch_nx (function_instance *)
2241 {
2242 }
2243
2244 inline void
2245 gt_pch_nx (function_instance *, gt_pointer_operator, void *)
2246 {
2247 }
2248
2249 #include "gt-arm-mve-builtins.h"