[AArch64] Avoid GET_MODE_NUNITS in v8.4 support
[official-gcc.git] / gcc / jit / jit-builtins.c
blob9f5c04b1747c366d4a72f20ecfda14e138a9ae7e
1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "jit-playback.h"
25 #include "stringpool.h"
27 #include "jit-builtins.h"
29 namespace gcc {
31 namespace jit {
33 const char *const prefix = "__builtin_";
34 const size_t prefix_len = strlen (prefix);
36 /* Create "builtin_data", a const table of the data within builtins.def. */
37 struct builtin_data
39 const char *name;
40 enum built_in_class fnclass;
41 enum jit_builtin_type type;
42 bool both_p;
43 bool fallback_p;
44 enum built_in_attribute attr;
45 bool implicit_p;
47 const char *get_asm_name () const
49 if (both_p && fallback_p)
50 return name + prefix_len;
51 else
52 return name;
56 #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
57 NONANSI_P, ATTRS, IMPLICIT, COND) \
58 {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
59 static const struct builtin_data builtin_data[] =
61 #include "builtins.def"
64 /* Helper function for find_builtin_by_name. */
66 static bool
67 matches_builtin (const char *in_name,
68 const struct builtin_data& bd)
70 const bool debug = 0;
72 /* Ignore entries with a NULL name. */
73 if (!bd.name)
74 return false;
76 if (debug)
77 fprintf (stderr, "seen builtin: %s\n", bd.name);
79 if (strcmp (bd.name, in_name) == 0)
80 return true;
82 if (bd.both_p)
84 /* Then the macros in builtins.def gave a "__builtin_"
85 prefix to bd.name, but we should also recognize the form
86 without the prefix. */
87 gcc_assert (strncmp (bd.name, prefix, prefix_len) == 0);
88 if (debug)
89 fprintf (stderr, "testing without prefix as: %s\n",
90 bd.name + prefix_len);
91 if (strcmp (bd.name + prefix_len, in_name) == 0)
92 return true;
95 return false;
98 /* Locate the built-in function that matches name IN_NAME,
99 writing the result to OUT_ID and returning true if found,
100 or returning false if not found. */
102 static bool
103 find_builtin_by_name (const char *in_name,
104 enum built_in_function *out_id)
106 /* Locate builtin. This currently works by performing repeated
107 strcmp against every possible candidate, which is likely to
108 inefficient.
110 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
111 has a NULL name. */
112 for (unsigned int i = 1;
113 i < sizeof (builtin_data) / sizeof (builtin_data[0]);
114 i++)
116 const struct builtin_data& bd = builtin_data[i];
117 if (matches_builtin (in_name, bd))
119 /* Found a match. */
120 *out_id = static_cast<enum built_in_function> (i);
121 return true;
125 /* Not found. */
126 return false;
129 // class builtins_manager
131 /* Constructor for gcc::jit::builtins_manager. */
133 builtins_manager::builtins_manager (recording::context *ctxt)
134 : m_ctxt (ctxt)
136 memset (m_types, 0, sizeof (m_types));
137 memset (m_builtin_functions, 0, sizeof (m_builtin_functions));
138 memset (m_attributes, 0, sizeof (m_attributes));
141 /* Locate a builtin function by name.
142 Create a recording::function of the appropriate type, reusing them
143 if they've already been seen. */
145 recording::function *
146 builtins_manager::get_builtin_function (const char *name)
148 enum built_in_function builtin_id;
149 if (!find_builtin_by_name (name, &builtin_id))
151 m_ctxt->add_error (NULL, "builtin \"%s\" not found", name);
152 return NULL;
155 return get_builtin_function_by_id (builtin_id);
158 /* Locate a builtin function by id.
159 Create a recording::function of the appropriate type, reusing them
160 if they've already been seen. */
162 recording::function *
163 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id)
165 gcc_assert (builtin_id >= 0);
166 gcc_assert (builtin_id < END_BUILTINS);
168 /* Lazily build the functions, caching them so that repeated calls for
169 the same id on a context give back the same object. */
170 if (!m_builtin_functions[builtin_id])
172 recording::function *fn = make_builtin_function (builtin_id);
173 if (fn)
175 m_builtin_functions[builtin_id] = fn;
176 m_ctxt->record (fn);
180 return m_builtin_functions[builtin_id];
183 /* Create the recording::function for a given builtin function, by ID. */
185 recording::function *
186 builtins_manager::make_builtin_function (enum built_in_function builtin_id)
188 const struct builtin_data& bd = builtin_data[builtin_id];
189 enum jit_builtin_type type_id = bd.type;
190 recording::type *t = get_type (type_id);
191 if (!t)
192 return NULL;
193 recording::function_type *func_type = t->as_a_function_type ();
194 if (!func_type)
195 return NULL;
197 vec<recording::type *> param_types = func_type->get_param_types ();
198 recording::param **params = new recording::param *[param_types.length ()];
200 int i;
201 recording::type *param_type;
202 FOR_EACH_VEC_ELT (param_types, i, param_type)
204 char buf[16];
205 snprintf (buf, 16, "arg%d", i);
206 params[i] = m_ctxt->new_param (NULL,
207 param_type,
208 buf);
210 const char *asm_name = bd.get_asm_name ();
211 recording::function *result =
212 new recording::function (m_ctxt,
213 NULL,
214 GCC_JIT_FUNCTION_IMPORTED, // FIXME
215 func_type->get_return_type (),
216 m_ctxt->new_string (asm_name),
217 param_types.length (),
218 params,
219 func_type->is_variadic (),
220 builtin_id);
221 delete[] params;
223 /* PR/64020 - If the client code is using builtin cos or sin,
224 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
225 to optimize them to use __builtin_cexpi; for this,
226 BUILT_IN_CEXPI needs to exist.
228 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
229 built. */
230 if (builtin_id == BUILT_IN_COS || builtin_id == BUILT_IN_SIN)
231 (void)get_builtin_function_by_id (BUILT_IN_CEXPI);
233 /* builtins.c:expand_builtin_cexpi can optimize the various
234 CEXP builtins to SINCOS builtins, and hence we may require
235 SINCOS builtins latter.
237 Ensure the appropriate SINCOS builtin exists. */
238 if (builtin_id == BUILT_IN_CEXPIF)
239 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF);
240 else if (builtin_id == BUILT_IN_CEXPI)
241 (void)get_builtin_function_by_id (BUILT_IN_SINCOS);
242 else if (builtin_id == BUILT_IN_CEXPIL)
243 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL);
245 return result;
248 /* Get the recording::type for a given type of builtin function,
249 by ID, creating it if it doesn't already exist. */
251 recording::type *
252 builtins_manager::get_type (enum jit_builtin_type type_id)
254 if (!m_types[type_id])
255 m_types[type_id] = make_type (type_id);
256 return m_types[type_id];
259 /* Create the recording::type for a given type of builtin function. */
261 recording::type *
262 builtins_manager::make_type (enum jit_builtin_type type_id)
264 /* Use builtin-types.def to construct a switch statement, with each
265 case deferring to one of the methods below:
266 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
267 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
268 to make_fn_type.
269 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
270 "is_variadic" argument.
271 - DEF_POINTER_TYPE is handled by make_ptr_type.
272 That should handle everything, but just in case we also suppy a
273 gcc_unreachable default clause. */
274 switch (type_id)
276 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
277 case ENUM: return make_primitive_type (ENUM);
278 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
279 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
280 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
281 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
282 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
283 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
284 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
285 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
286 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
287 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
288 ARG4);
289 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
290 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
291 ARG4, ARG5);
292 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
293 ARG6) \
294 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
295 ARG4, ARG5, ARG6);
296 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
297 ARG6, ARG7) \
298 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
299 ARG4, ARG5, ARG6, ARG7);
300 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
301 ARG6, ARG7, ARG8) \
302 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
303 ARG4, ARG5, ARG6, ARG7, ARG8);
304 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
305 ARG6, ARG7, ARG8, ARG9) \
306 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
307 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
308 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
309 ARG6, ARG7, ARG8, ARG9, ARG10) \
310 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
311 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
312 ARG10);
313 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
314 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
315 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
316 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
317 ARG10, ARG11);
318 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
319 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
320 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
321 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
322 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
323 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
324 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
325 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
326 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
327 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
328 ARG4);
329 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
330 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
331 ARG4, ARG5);
332 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
333 ARG6) \
334 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
335 ARG4, ARG5, ARG6);
336 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
337 ARG6, ARG7) \
338 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
339 ARG4, ARG5, ARG6, ARG7);
340 #define DEF_POINTER_TYPE(ENUM, TYPE) \
341 case ENUM: return make_ptr_type (ENUM, TYPE);
343 #include "builtin-types.def"
345 #undef DEF_PRIMITIVE_TYPE
346 #undef DEF_FUNCTION_TYPE_0
347 #undef DEF_FUNCTION_TYPE_1
348 #undef DEF_FUNCTION_TYPE_2
349 #undef DEF_FUNCTION_TYPE_3
350 #undef DEF_FUNCTION_TYPE_4
351 #undef DEF_FUNCTION_TYPE_5
352 #undef DEF_FUNCTION_TYPE_6
353 #undef DEF_FUNCTION_TYPE_7
354 #undef DEF_FUNCTION_TYPE_8
355 #undef DEF_FUNCTION_TYPE_9
356 #undef DEF_FUNCTION_TYPE_10
357 #undef DEF_FUNCTION_TYPE_11
358 #undef DEF_FUNCTION_TYPE_VAR_0
359 #undef DEF_FUNCTION_TYPE_VAR_1
360 #undef DEF_FUNCTION_TYPE_VAR_2
361 #undef DEF_FUNCTION_TYPE_VAR_3
362 #undef DEF_FUNCTION_TYPE_VAR_4
363 #undef DEF_FUNCTION_TYPE_VAR_5
364 #undef DEF_FUNCTION_TYPE_VAR_6
365 #undef DEF_FUNCTION_TYPE_VAR_7
366 #undef DEF_POINTER_TYPE
368 default:
369 gcc_unreachable ();
373 /* Create the recording::type for a given primitive type within the
374 builtin system.
376 Only some types are currently supported. */
378 recording::type*
379 builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
381 switch (type_id)
383 default:
384 // only some of these types are implemented so far:
385 m_ctxt->add_error (NULL,
386 "unimplemented primitive type for builtin: %d", type_id);
387 return NULL;
389 case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
390 case BT_BOOL: return m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
391 case BT_INT: return m_ctxt->get_type (GCC_JIT_TYPE_INT);
392 case BT_UINT: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_INT);
393 case BT_LONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG);
394 case BT_ULONG: return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG);
395 case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
396 case BT_ULONGLONG:
397 return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
398 // case BT_INT128:
399 // case BT_UINT128:
400 // case BT_INTMAX:
401 // case BT_UINTMAX:
402 case BT_UINT16: return m_ctxt->get_int_type (2, false);
403 case BT_UINT32: return m_ctxt->get_int_type (4, false);
404 case BT_UINT64: return m_ctxt->get_int_type (8, false);
405 // case BT_WORD:
406 // case BT_UNWINDWORD:
407 case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
408 case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
409 case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
410 case BT_COMPLEX_FLOAT:
411 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
412 case BT_COMPLEX_DOUBLE:
413 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE);
414 case BT_COMPLEX_LONGDOUBLE:
415 return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
416 case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
417 case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
418 // case BT_CONST:
419 // case BT_VOLATILE_PTR:
420 // case BT_CONST_VOLATILE_PTR:
421 // case BT_PTRMODE:
422 // case BT_INT_PTR:
423 // case BT_FLOAT_PTR:
424 case BT_DOUBLE_PTR:
425 return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
426 // case BT_CONST_DOUBLE_PTR:
427 // case BT_LONGDOUBLE_PTR:
428 // case BT_PID:
429 // case BT_SIZE:
430 // case BT_SSIZE:
431 // case BT_WINT:
432 // case BT_STRING:
433 case BT_CONST_STRING: return m_ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR);
434 // case BT_DFLOAT32:
435 // case BT_DFLOAT64:
436 // case BT_DFLOAT128:
437 // case BT_DFLOAT32_PTR:
438 // case BT_DFLOAT64_PTR:
439 // case BT_DFLOAT128_PTR:
440 // case BT_VALIST_REF:
441 // case BT_VALIST_ARG:
442 // case BT_I1:
443 // case BT_I2:
444 // case BT_I4:
445 // case BT_I8:
446 // case BT_I16:
450 /* Create the recording::function_type for a given function type
451 signature. */
453 recording::function_type *
454 builtins_manager::make_fn_type (enum jit_builtin_type,
455 enum jit_builtin_type return_type_id,
456 bool is_variadic,
457 int num_args, ...)
459 va_list list;
460 int i;
461 recording::type **param_types = new recording::type *[num_args];
462 recording::type *return_type = NULL;
463 recording::function_type *result = NULL;
465 va_start (list, num_args);
466 for (i = 0; i < num_args; ++i)
468 enum jit_builtin_type arg_type_id =
469 (enum jit_builtin_type) va_arg (list, int);
470 param_types[i] = get_type (arg_type_id);
471 if (!param_types[i])
472 goto error;
474 va_end (list);
476 return_type = get_type (return_type_id);
477 if (!return_type)
478 goto error;
480 result = m_ctxt->new_function_type (return_type,
481 num_args,
482 param_types,
483 is_variadic);
485 error:
486 delete[] param_types;
487 return result;
490 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
492 recording::type *
493 builtins_manager::make_ptr_type (enum jit_builtin_type,
494 enum jit_builtin_type other_type_id)
496 recording::type *base_type = get_type (other_type_id);
497 return base_type->get_pointer ();
500 /* Playback support. */
502 /* A builtins_manager is associated with a recording::context
503 and might be reused for multiple compiles on various
504 playback::contexts, perhaps with different options.
506 Purge any playback state. Currently this is just the table of
507 attributes. */
509 void
510 builtins_manager::finish_playback (void)
512 memset (m_attributes, 0, sizeof (m_attributes));
515 /* Get the enum built_in_class for BUILTIN_ID. */
517 enum built_in_class
518 builtins_manager::get_class (enum built_in_function builtin_id)
520 return builtin_data[builtin_id].fnclass;
523 /* Is BUILTIN_ID implicit? */
525 bool
526 builtins_manager::implicit_p (enum built_in_function builtin_id)
528 return builtin_data[builtin_id].implicit_p;
531 /* Get any attributes (in tree form) for the function declaration
532 for BUILTIN_ID.
534 These are created on-demand, and cached within the m_attributes
535 array, until finish_playback. */
537 tree
538 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
540 enum built_in_attribute attr = builtin_data[builtin_id].attr;
541 return get_attrs_tree (attr);
544 /* As above, but for an enum built_in_attribute. */
546 tree
547 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
549 gcc_assert (attr < ATTR_LAST);
550 if (!m_attributes [attr])
551 m_attributes [attr] = make_attrs_tree (attr);
552 return m_attributes [attr];
555 /* Handle a cache-miss within the m_attributes array by
556 generating the attributes for enum built_in_attribute
557 in tree form. */
559 tree
560 builtins_manager::make_attrs_tree (enum built_in_attribute attr)
562 switch (attr)
564 /* Generate cases from builtin-attrs.def. */
565 #define DEF_ATTR_NULL_TREE(ENUM) \
566 case ENUM: return NULL_TREE;
567 #define DEF_ATTR_INT(ENUM, VALUE) \
568 case ENUM: return build_int_cst (integer_type_node, VALUE);
569 #define DEF_ATTR_STRING(ENUM, VALUE) \
570 case ENUM: return build_string (strlen (VALUE), VALUE);
571 #define DEF_ATTR_IDENT(ENUM, STRING) \
572 case ENUM: return get_identifier (STRING);
573 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
574 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
575 get_attrs_tree (VALUE), \
576 get_attrs_tree (CHAIN));
577 #include "builtin-attrs.def"
578 #undef DEF_ATTR_NULL_TREE
579 #undef DEF_ATTR_INT
580 #undef DEF_ATTR_IDENT
581 #undef DEF_ATTR_TREE_LIST
583 default:
584 /* We somehow got a value not covered by the autogenerated
585 cases. */
586 gcc_unreachable ();
587 return NULL;
591 } // namespace jit
592 } // namespace gcc