1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2017 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
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
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/>. */
22 #include "coretypes.h"
24 #include "jit-playback.h"
25 #include "stringpool.h"
27 #include "jit-builtins.h"
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. */
40 enum built_in_class fnclass
;
41 enum jit_builtin_type type
;
44 enum built_in_attribute attr
;
47 const char *get_asm_name () const
49 if (both_p
&& fallback_p
)
50 return name
+ prefix_len
;
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. */
67 matches_builtin (const char *in_name
,
68 const struct builtin_data
& bd
)
72 /* Ignore entries with a NULL name. */
77 fprintf (stderr
, "seen builtin: %s\n", bd
.name
);
79 if (0 == strcmp (bd
.name
, in_name
))
86 /* Then the macros in builtins.def gave a "__builtin_"
87 prefix to bd.name, but we should also recognize the form
88 without the prefix. */
89 gcc_assert (0 == strncmp (bd
.name
, prefix
, prefix_len
));
91 fprintf (stderr
, "testing without prefix as: %s\n",
92 bd
.name
+ prefix_len
);
93 if (0 == strcmp (bd
.name
+ prefix_len
, in_name
))
102 /* Locate the built-in function that matches name IN_NAME,
103 writing the result to OUT_ID and returning true if found,
104 or returning false if not found. */
107 find_builtin_by_name (const char *in_name
,
108 enum built_in_function
*out_id
)
110 /* Locate builtin. This currently works by performing repeated
111 strcmp against every possible candidate, which is likely to
114 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
116 for (unsigned int i
= 1;
117 i
< sizeof (builtin_data
) / sizeof (builtin_data
[0]);
120 const struct builtin_data
& bd
= builtin_data
[i
];
121 if (matches_builtin (in_name
, bd
))
124 *out_id
= static_cast<enum built_in_function
> (i
);
133 // class builtins_manager
135 /* Constructor for gcc::jit::builtins_manager. */
137 builtins_manager::builtins_manager (recording::context
*ctxt
)
140 memset (m_types
, 0, sizeof (m_types
));
141 memset (m_builtin_functions
, 0, sizeof (m_builtin_functions
));
142 memset (m_attributes
, 0, sizeof (m_attributes
));
145 /* Locate a builtin function by name.
146 Create a recording::function of the appropriate type, reusing them
147 if they've already been seen. */
149 recording::function
*
150 builtins_manager::get_builtin_function (const char *name
)
152 enum built_in_function builtin_id
;
153 if (!find_builtin_by_name (name
, &builtin_id
))
155 m_ctxt
->add_error (NULL
, "builtin \"%s\" not found", name
);
159 return get_builtin_function_by_id (builtin_id
);
162 /* Locate a builtin function by id.
163 Create a recording::function of the appropriate type, reusing them
164 if they've already been seen. */
166 recording::function
*
167 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id
)
169 gcc_assert (builtin_id
>= 0);
170 gcc_assert (builtin_id
< END_BUILTINS
);
172 /* Lazily build the functions, caching them so that repeated calls for
173 the same id on a context give back the same object. */
174 if (!m_builtin_functions
[builtin_id
])
176 recording::function
*fn
= make_builtin_function (builtin_id
);
179 m_builtin_functions
[builtin_id
] = fn
;
184 return m_builtin_functions
[builtin_id
];
187 /* Create the recording::function for a given builtin function, by ID. */
189 recording::function
*
190 builtins_manager::make_builtin_function (enum built_in_function builtin_id
)
192 const struct builtin_data
& bd
= builtin_data
[builtin_id
];
193 enum jit_builtin_type type_id
= bd
.type
;
194 recording::type
*t
= get_type (type_id
);
197 recording::function_type
*func_type
= t
->as_a_function_type ();
201 vec
<recording::type
*> param_types
= func_type
->get_param_types ();
202 recording::param
**params
= new recording::param
*[param_types
.length ()];
205 recording::type
*param_type
;
206 FOR_EACH_VEC_ELT (param_types
, i
, param_type
)
209 snprintf (buf
, 16, "arg%d", i
);
210 params
[i
] = m_ctxt
->new_param (NULL
,
214 const char *asm_name
= bd
.get_asm_name ();
215 recording::function
*result
=
216 new recording::function (m_ctxt
,
218 GCC_JIT_FUNCTION_IMPORTED
, // FIXME
219 func_type
->get_return_type (),
220 m_ctxt
->new_string (asm_name
),
221 param_types
.length (),
223 func_type
->is_variadic (),
227 /* PR/64020 - If the client code is using builtin cos or sin,
228 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
229 to optimize them to use __builtin_cexpi; for this,
230 BUILT_IN_CEXPI needs to exist.
232 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
234 if (builtin_id
== BUILT_IN_COS
|| builtin_id
== BUILT_IN_SIN
)
235 (void)get_builtin_function_by_id (BUILT_IN_CEXPI
);
237 /* builtins.c:expand_builtin_cexpi can optimize the various
238 CEXP builtins to SINCOS builtins, and hence we may require
239 SINCOS builtins latter.
241 Ensure the appropriate SINCOS builtin exists. */
242 if (builtin_id
== BUILT_IN_CEXPIF
)
243 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF
);
244 else if (builtin_id
== BUILT_IN_CEXPI
)
245 (void)get_builtin_function_by_id (BUILT_IN_SINCOS
);
246 else if (builtin_id
== BUILT_IN_CEXPIL
)
247 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL
);
252 /* Get the recording::type for a given type of builtin function,
253 by ID, creating it if it doesn't already exist. */
256 builtins_manager::get_type (enum jit_builtin_type type_id
)
258 if (!m_types
[type_id
])
259 m_types
[type_id
] = make_type (type_id
);
260 return m_types
[type_id
];
263 /* Create the recording::type for a given type of builtin function. */
266 builtins_manager::make_type (enum jit_builtin_type type_id
)
268 /* Use builtin-types.def to construct a switch statement, with each
269 case deferring to one of the methods below:
270 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
271 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
273 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
274 "is_variadic" argument.
275 - DEF_POINTER_TYPE is handled by make_ptr_type.
276 That should handle everything, but just in case we also suppy a
277 gcc_unreachable default clause. */
280 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
281 case ENUM: return make_primitive_type (ENUM);
282 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
283 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
284 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
285 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
286 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
287 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
288 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
289 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
290 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
291 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
293 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
294 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
296 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
298 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
300 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
302 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
303 ARG4, ARG5, ARG6, ARG7);
304 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
306 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
307 ARG4, ARG5, ARG6, ARG7, ARG8);
308 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
309 ARG6, ARG7, ARG8, ARG9) \
310 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
311 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
312 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
313 ARG6, ARG7, ARG8, ARG9, ARG10) \
314 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
315 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
317 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
318 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
319 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
320 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
322 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
323 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
324 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
325 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
326 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
327 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
328 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
329 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
330 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
331 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
333 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
334 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
336 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
338 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
340 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
342 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
343 ARG4, ARG5, ARG6, ARG7);
344 #define DEF_POINTER_TYPE(ENUM, TYPE) \
345 case ENUM: return make_ptr_type (ENUM, TYPE);
347 #include "builtin-types.def"
349 #undef DEF_PRIMITIVE_TYPE
350 #undef DEF_FUNCTION_TYPE_0
351 #undef DEF_FUNCTION_TYPE_1
352 #undef DEF_FUNCTION_TYPE_2
353 #undef DEF_FUNCTION_TYPE_3
354 #undef DEF_FUNCTION_TYPE_4
355 #undef DEF_FUNCTION_TYPE_5
356 #undef DEF_FUNCTION_TYPE_6
357 #undef DEF_FUNCTION_TYPE_7
358 #undef DEF_FUNCTION_TYPE_8
359 #undef DEF_FUNCTION_TYPE_9
360 #undef DEF_FUNCTION_TYPE_10
361 #undef DEF_FUNCTION_TYPE_11
362 #undef DEF_FUNCTION_TYPE_VAR_0
363 #undef DEF_FUNCTION_TYPE_VAR_1
364 #undef DEF_FUNCTION_TYPE_VAR_2
365 #undef DEF_FUNCTION_TYPE_VAR_3
366 #undef DEF_FUNCTION_TYPE_VAR_4
367 #undef DEF_FUNCTION_TYPE_VAR_5
368 #undef DEF_FUNCTION_TYPE_VAR_6
369 #undef DEF_FUNCTION_TYPE_VAR_7
370 #undef DEF_POINTER_TYPE
377 /* Create the recording::type for a given primitive type within the
380 Only some types are currently supported. */
383 builtins_manager::make_primitive_type (enum jit_builtin_type type_id
)
388 // only some of these types are implemented so far:
389 m_ctxt
->add_error (NULL
,
390 "unimplemented primitive type for builtin: %d", type_id
);
393 case BT_VOID
: return m_ctxt
->get_type (GCC_JIT_TYPE_VOID
);
394 case BT_BOOL
: return m_ctxt
->get_type (GCC_JIT_TYPE_BOOL
);
395 case BT_INT
: return m_ctxt
->get_type (GCC_JIT_TYPE_INT
);
396 case BT_UINT
: return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_INT
);
397 case BT_LONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG
);
398 case BT_ULONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_LONG
);
399 case BT_LONGLONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG_LONG
);
401 return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG
);
406 case BT_UINT16
: return m_ctxt
->get_int_type (2, false);
407 case BT_UINT32
: return m_ctxt
->get_int_type (4, false);
408 case BT_UINT64
: return m_ctxt
->get_int_type (8, false);
410 // case BT_UNWINDWORD:
411 case BT_FLOAT
: return m_ctxt
->get_type (GCC_JIT_TYPE_FLOAT
);
412 case BT_DOUBLE
: return m_ctxt
->get_type (GCC_JIT_TYPE_DOUBLE
);
413 case BT_LONGDOUBLE
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG_DOUBLE
);
414 case BT_COMPLEX_FLOAT
:
415 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT
);
416 case BT_COMPLEX_DOUBLE
:
417 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE
);
418 case BT_COMPLEX_LONGDOUBLE
:
419 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE
);
420 case BT_PTR
: return m_ctxt
->get_type (GCC_JIT_TYPE_VOID_PTR
);
421 case BT_FILEPTR
: return m_ctxt
->get_type (GCC_JIT_TYPE_FILE_PTR
);
423 // case BT_VOLATILE_PTR:
424 // case BT_CONST_VOLATILE_PTR:
427 // case BT_FLOAT_PTR:
429 return m_ctxt
->get_type (GCC_JIT_TYPE_DOUBLE
)->get_pointer ();
430 // case BT_CONST_DOUBLE_PTR:
431 // case BT_LONGDOUBLE_PTR:
437 case BT_CONST_STRING
: return m_ctxt
->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR
);
440 // case BT_DFLOAT128:
441 // case BT_DFLOAT32_PTR:
442 // case BT_DFLOAT64_PTR:
443 // case BT_DFLOAT128_PTR:
444 // case BT_VALIST_REF:
445 // case BT_VALIST_ARG:
454 /* Create the recording::function_type for a given function type
457 recording::function_type
*
458 builtins_manager::make_fn_type (enum jit_builtin_type
,
459 enum jit_builtin_type return_type_id
,
465 recording::type
**param_types
= new recording::type
*[num_args
];
466 recording::type
*return_type
= NULL
;
467 recording::function_type
*result
= NULL
;
469 va_start (list
, num_args
);
470 for (i
= 0; i
< num_args
; ++i
)
472 enum jit_builtin_type arg_type_id
=
473 (enum jit_builtin_type
) va_arg (list
, int);
474 param_types
[i
] = get_type (arg_type_id
);
480 return_type
= get_type (return_type_id
);
484 result
= m_ctxt
->new_function_type (return_type
,
490 delete[] param_types
;
494 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
497 builtins_manager::make_ptr_type (enum jit_builtin_type
,
498 enum jit_builtin_type other_type_id
)
500 recording::type
*base_type
= get_type (other_type_id
);
501 return base_type
->get_pointer ();
504 /* Playback support. */
506 /* A builtins_manager is associated with a recording::context
507 and might be reused for multiple compiles on various
508 playback::contexts, perhaps with different options.
510 Purge any playback state. Currently this is just the table of
514 builtins_manager::finish_playback (void)
516 memset (m_attributes
, 0, sizeof (m_attributes
));
519 /* Get the enum built_in_class for BUILTIN_ID. */
522 builtins_manager::get_class (enum built_in_function builtin_id
)
524 return builtin_data
[builtin_id
].fnclass
;
527 /* Is BUILTIN_ID implicit? */
530 builtins_manager::implicit_p (enum built_in_function builtin_id
)
532 return builtin_data
[builtin_id
].implicit_p
;
535 /* Get any attributes (in tree form) for the function declaration
538 These are created on-demand, and cached within the m_attributes
539 array, until finish_playback. */
542 builtins_manager::get_attrs_tree (enum built_in_function builtin_id
)
544 enum built_in_attribute attr
= builtin_data
[builtin_id
].attr
;
545 return get_attrs_tree (attr
);
548 /* As above, but for an enum built_in_attribute. */
551 builtins_manager::get_attrs_tree (enum built_in_attribute attr
)
553 gcc_assert (attr
< ATTR_LAST
);
554 if (!m_attributes
[attr
])
555 m_attributes
[attr
] = make_attrs_tree (attr
);
556 return m_attributes
[attr
];
559 /* Handle a cache-miss within the m_attributes array by
560 generating the attributes for enum built_in_attribute
564 builtins_manager::make_attrs_tree (enum built_in_attribute attr
)
568 /* Generate cases from builtin-attrs.def. */
569 #define DEF_ATTR_NULL_TREE(ENUM) \
570 case ENUM: return NULL_TREE;
571 #define DEF_ATTR_INT(ENUM, VALUE) \
572 case ENUM: return build_int_cst (integer_type_node, VALUE);
573 #define DEF_ATTR_STRING(ENUM, VALUE) \
574 case ENUM: return build_string (strlen (VALUE), VALUE);
575 #define DEF_ATTR_IDENT(ENUM, STRING) \
576 case ENUM: return get_identifier (STRING);
577 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
578 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
579 get_attrs_tree (VALUE), \
580 get_attrs_tree (CHAIN));
581 #include "builtin-attrs.def"
582 #undef DEF_ATTR_NULL_TREE
584 #undef DEF_ATTR_IDENT
585 #undef DEF_ATTR_TREE_LIST
588 /* We somehow got a value not covered by the autogenerated