1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014-2015 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"
25 #include "stringpool.h"
27 #include "jit-common.h"
28 #include "jit-builtins.h"
29 #include "jit-recording.h"
30 #include "jit-playback.h"
36 const char *const prefix
= "__builtin_";
37 const size_t prefix_len
= strlen (prefix
);
39 /* Create "builtin_data", a const table of the data within builtins.def. */
43 enum built_in_class fnclass
;
44 enum jit_builtin_type type
;
47 enum built_in_attribute attr
;
50 const char *get_asm_name () const
52 if (both_p
&& fallback_p
)
53 return name
+ prefix_len
;
59 #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
60 NONANSI_P, ATTRS, IMPLICIT, COND) \
61 {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
62 static const struct builtin_data builtin_data
[] =
64 #include "builtins.def"
68 /* Helper function for find_builtin_by_name. */
71 matches_builtin (const char *in_name
,
72 const struct builtin_data
& bd
)
78 fprintf (stderr
, "seen builtin: %s\n", bd
.name
);
80 if (0 == strcmp (bd
.name
, in_name
))
87 /* Then the macros in builtins.def gave a "__builtin_"
88 prefix to bd.name, but we should also recognize the form
89 without the prefix. */
90 gcc_assert (0 == strncmp (bd
.name
, prefix
, prefix_len
));
92 fprintf (stderr
, "testing without prefix as: %s\n",
93 bd
.name
+ prefix_len
);
94 if (0 == strcmp (bd
.name
+ prefix_len
, in_name
))
103 /* Locate the built-in function that matches name IN_NAME,
104 writing the result to OUT_ID and returning true if found,
105 or returning false if not found. */
108 find_builtin_by_name (const char *in_name
,
109 enum built_in_function
*out_id
)
111 /* Locate builtin. This currently works by performing repeated
112 strcmp against every possible candidate, which is likely to
115 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
117 for (unsigned int i
= 1;
118 i
< sizeof (builtin_data
) / sizeof (builtin_data
[0]);
121 const struct builtin_data
& bd
= builtin_data
[i
];
122 if (matches_builtin (in_name
, bd
))
125 *out_id
= static_cast<enum built_in_function
> (i
);
134 // class builtins_manager
136 /* Constructor for gcc::jit::builtins_manager. */
138 builtins_manager::builtins_manager (recording::context
*ctxt
)
141 memset (m_types
, 0, sizeof (m_types
));
142 memset (m_builtin_functions
, 0, sizeof (m_builtin_functions
));
143 memset (m_attributes
, 0, sizeof (m_attributes
));
146 /* Locate a builtin function by name.
147 Create a recording::function of the appropriate type, reusing them
148 if they've already been seen. */
150 recording::function
*
151 builtins_manager::get_builtin_function (const char *name
)
153 enum built_in_function builtin_id
;
154 if (!find_builtin_by_name (name
, &builtin_id
))
156 m_ctxt
->add_error (NULL
, "builtin \"%s\" not found", name
);
160 return get_builtin_function_by_id (builtin_id
);
163 /* Locate a builtin function by id.
164 Create a recording::function of the appropriate type, reusing them
165 if they've already been seen. */
167 recording::function
*
168 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id
)
170 gcc_assert (builtin_id
>= 0);
171 gcc_assert (builtin_id
< END_BUILTINS
);
173 /* Lazily build the functions, caching them so that repeated calls for
174 the same id on a context give back the same object. */
175 if (!m_builtin_functions
[builtin_id
])
177 recording::function
*fn
= make_builtin_function (builtin_id
);
180 m_builtin_functions
[builtin_id
] = fn
;
185 return m_builtin_functions
[builtin_id
];
188 /* Create the recording::function for a given builtin function, by ID. */
190 recording::function
*
191 builtins_manager::make_builtin_function (enum built_in_function builtin_id
)
193 const struct builtin_data
& bd
= builtin_data
[builtin_id
];
194 enum jit_builtin_type type_id
= bd
.type
;
195 recording::type
*t
= get_type (type_id
);
198 recording::function_type
*func_type
= t
->as_a_function_type ();
202 vec
<recording::type
*> param_types
= func_type
->get_param_types ();
203 recording::param
**params
= new recording::param
*[param_types
.length ()];
206 recording::type
*param_type
;
207 FOR_EACH_VEC_ELT (param_types
, i
, param_type
)
210 snprintf (buf
, 16, "arg%d", i
);
211 params
[i
] = m_ctxt
->new_param (NULL
,
215 const char *asm_name
= bd
.get_asm_name ();
216 recording::function
*result
=
217 new recording::function (m_ctxt
,
219 GCC_JIT_FUNCTION_IMPORTED
, // FIXME
220 func_type
->get_return_type (),
221 m_ctxt
->new_string (asm_name
),
222 param_types
.length (),
224 func_type
->is_variadic (),
228 /* PR/64020 - If the client code is using builtin cos or sin,
229 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
230 to optimize them to use __builtin_cexpi; for this,
231 BUILT_IN_CEXPI needs to exist.
233 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
235 if (builtin_id
== BUILT_IN_COS
|| builtin_id
== BUILT_IN_SIN
)
236 (void)get_builtin_function_by_id (BUILT_IN_CEXPI
);
238 /* builtins.c:expand_builtin_cexpi can optimize the various
239 CEXP builtins to SINCOS builtins, and hence we may require
240 SINCOS builtins latter.
242 Ensure the appropriate SINCOS builtin exists. */
243 if (builtin_id
== BUILT_IN_CEXPIF
)
244 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF
);
245 else if (builtin_id
== BUILT_IN_CEXPI
)
246 (void)get_builtin_function_by_id (BUILT_IN_SINCOS
);
247 else if (builtin_id
== BUILT_IN_CEXPIL
)
248 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL
);
253 /* Get the recording::type for a given type of builtin function,
254 by ID, creating it if it doesn't already exist. */
257 builtins_manager::get_type (enum jit_builtin_type type_id
)
259 if (!m_types
[type_id
])
260 m_types
[type_id
] = make_type (type_id
);
261 return m_types
[type_id
];
264 /* Create the recording::type for a given type of builtin function. */
267 builtins_manager::make_type (enum jit_builtin_type type_id
)
269 /* Use builtin-types.def to construct a switch statement, with each
270 case deferring to one of the methods below:
271 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
272 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
274 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
275 "is_variadic" argument.
276 - DEF_POINTER_TYPE is handled by make_ptr_type.
277 That should handle everything, but just in case we also suppy a
278 gcc_unreachable default clause. */
281 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
282 case ENUM: return make_primitive_type (ENUM);
283 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
284 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
285 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
286 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
287 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
288 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
289 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
290 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
291 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
292 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, \
294 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
295 case ENUM: return make_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, \
297 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
299 case ENUM: return make_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, \
301 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
303 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, \
304 ARG4, ARG5, ARG6, ARG7);
305 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
307 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, \
308 ARG4, ARG5, ARG6, ARG7, ARG8);
309 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
310 ARG6, ARG7, ARG8, ARG9) \
311 case ENUM: return make_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, \
312 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9);
313 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
314 ARG6, ARG7, ARG8, ARG9, ARG10) \
315 case ENUM: return make_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, \
316 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
318 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
319 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
320 case ENUM: return make_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, \
321 ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, \
323 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
324 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
325 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
326 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
327 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
328 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
329 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
330 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
331 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
332 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, \
334 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
335 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, \
337 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
339 case ENUM: return make_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, \
341 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
343 case ENUM: return make_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, \
344 ARG4, ARG5, ARG6, ARG7);
345 #define DEF_POINTER_TYPE(ENUM, TYPE) \
346 case ENUM: return make_ptr_type (ENUM, TYPE);
348 #include "builtin-types.def"
350 #undef DEF_PRIMITIVE_TYPE
351 #undef DEF_FUNCTION_TYPE_0
352 #undef DEF_FUNCTION_TYPE_1
353 #undef DEF_FUNCTION_TYPE_2
354 #undef DEF_FUNCTION_TYPE_3
355 #undef DEF_FUNCTION_TYPE_4
356 #undef DEF_FUNCTION_TYPE_5
357 #undef DEF_FUNCTION_TYPE_6
358 #undef DEF_FUNCTION_TYPE_7
359 #undef DEF_FUNCTION_TYPE_8
360 #undef DEF_FUNCTION_TYPE_9
361 #undef DEF_FUNCTION_TYPE_10
362 #undef DEF_FUNCTION_TYPE_11
363 #undef DEF_FUNCTION_TYPE_VAR_0
364 #undef DEF_FUNCTION_TYPE_VAR_1
365 #undef DEF_FUNCTION_TYPE_VAR_2
366 #undef DEF_FUNCTION_TYPE_VAR_3
367 #undef DEF_FUNCTION_TYPE_VAR_4
368 #undef DEF_FUNCTION_TYPE_VAR_5
369 #undef DEF_FUNCTION_TYPE_VAR_6
370 #undef DEF_FUNCTION_TYPE_VAR_7
371 #undef DEF_POINTER_TYPE
378 /* Create the recording::type for a given primitive type within the
381 Only some types are currently supported. */
384 builtins_manager::make_primitive_type (enum jit_builtin_type type_id
)
389 // only some of these types are implemented so far:
390 m_ctxt
->add_error (NULL
,
391 "unimplemented primitive type for builtin: %d", type_id
);
394 case BT_VOID
: return m_ctxt
->get_type (GCC_JIT_TYPE_VOID
);
395 case BT_BOOL
: return m_ctxt
->get_type (GCC_JIT_TYPE_BOOL
);
396 case BT_INT
: return m_ctxt
->get_type (GCC_JIT_TYPE_INT
);
397 case BT_UINT
: return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_INT
);
398 case BT_LONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG
);
399 case BT_ULONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_LONG
);
400 case BT_LONGLONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG_LONG
);
402 return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG
);
407 case BT_UINT16
: return m_ctxt
->get_int_type (2, false);
408 case BT_UINT32
: return m_ctxt
->get_int_type (4, false);
409 case BT_UINT64
: return m_ctxt
->get_int_type (8, false);
411 // case BT_UNWINDWORD:
412 case BT_FLOAT
: return m_ctxt
->get_type (GCC_JIT_TYPE_FLOAT
);
413 case BT_DOUBLE
: return m_ctxt
->get_type (GCC_JIT_TYPE_DOUBLE
);
414 case BT_LONGDOUBLE
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG_DOUBLE
);
415 case BT_COMPLEX_FLOAT
:
416 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT
);
417 case BT_COMPLEX_DOUBLE
:
418 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE
);
419 case BT_COMPLEX_LONGDOUBLE
:
420 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE
);
421 case BT_PTR
: return m_ctxt
->get_type (GCC_JIT_TYPE_VOID_PTR
);
422 case BT_FILEPTR
: return m_ctxt
->get_type (GCC_JIT_TYPE_FILE_PTR
);
424 // case BT_VOLATILE_PTR:
425 // case BT_CONST_VOLATILE_PTR:
428 // case BT_FLOAT_PTR:
430 return m_ctxt
->get_type (GCC_JIT_TYPE_DOUBLE
)->get_pointer ();
431 // case BT_CONST_DOUBLE_PTR:
432 // case BT_LONGDOUBLE_PTR:
438 case BT_CONST_STRING
: return m_ctxt
->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR
);
441 // case BT_DFLOAT128:
442 // case BT_DFLOAT32_PTR:
443 // case BT_DFLOAT64_PTR:
444 // case BT_DFLOAT128_PTR:
445 // case BT_VALIST_REF:
446 // case BT_VALIST_ARG:
455 /* Create the recording::function_type for a given function type
458 recording::function_type
*
459 builtins_manager::make_fn_type (enum jit_builtin_type
,
460 enum jit_builtin_type return_type_id
,
466 recording::type
**param_types
= new recording::type
*[num_args
];
467 recording::type
*return_type
= NULL
;
468 recording::function_type
*result
= NULL
;
470 va_start (list
, num_args
);
471 for (i
= 0; i
< num_args
; ++i
)
473 enum jit_builtin_type arg_type_id
=
474 (enum jit_builtin_type
) va_arg (list
, int);
475 param_types
[i
] = get_type (arg_type_id
);
481 return_type
= get_type (return_type_id
);
485 result
= m_ctxt
->new_function_type (return_type
,
491 delete[] param_types
;
495 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
498 builtins_manager::make_ptr_type (enum jit_builtin_type
,
499 enum jit_builtin_type other_type_id
)
501 recording::type
*base_type
= get_type (other_type_id
);
502 return base_type
->get_pointer ();
505 /* Playback support. */
507 /* A builtins_manager is associated with a recording::context
508 and might be reused for multiple compiles on various
509 playback::contexts, perhaps with different options.
511 Purge any playback state. Currently this is just the table of
515 builtins_manager::finish_playback (void)
517 memset (m_attributes
, 0, sizeof (m_attributes
));
520 /* Get the enum built_in_class for BUILTIN_ID. */
523 builtins_manager::get_class (enum built_in_function builtin_id
)
525 return builtin_data
[builtin_id
].fnclass
;
528 /* Is BUILTIN_ID implicit? */
531 builtins_manager::implicit_p (enum built_in_function builtin_id
)
533 return builtin_data
[builtin_id
].implicit_p
;
536 /* Get any attributes (in tree form) for the function declaration
539 These are created on-demand, and cached within the m_attributes
540 array, until finish_playback. */
543 builtins_manager::get_attrs_tree (enum built_in_function builtin_id
)
545 enum built_in_attribute attr
= builtin_data
[builtin_id
].attr
;
546 return get_attrs_tree (attr
);
549 /* As above, but for an enum built_in_attribute. */
552 builtins_manager::get_attrs_tree (enum built_in_attribute attr
)
554 gcc_assert (attr
< ATTR_LAST
);
555 if (!m_attributes
[attr
])
556 m_attributes
[attr
] = make_attrs_tree (attr
);
557 return m_attributes
[attr
];
560 /* Handle a cache-miss within the m_attributes array by
561 generating the attributes for enum built_in_attribute
565 builtins_manager::make_attrs_tree (enum built_in_attribute attr
)
569 /* Generate cases from builtin-attrs.def. */
570 #define DEF_ATTR_NULL_TREE(ENUM) \
571 case ENUM: return NULL_TREE;
572 #define DEF_ATTR_INT(ENUM, VALUE) \
573 case ENUM: return build_int_cst (integer_type_node, VALUE);
574 #define DEF_ATTR_STRING(ENUM, VALUE) \
575 case ENUM: return build_string (strlen (VALUE), VALUE);
576 #define DEF_ATTR_IDENT(ENUM, STRING) \
577 case ENUM: return get_identifier (STRING);
578 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
579 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
580 get_attrs_tree (VALUE), \
581 get_attrs_tree (CHAIN));
582 #include "builtin-attrs.def"
583 #undef DEF_ATTR_NULL_TREE
585 #undef DEF_ATTR_IDENT
586 #undef DEF_ATTR_TREE_LIST
589 /* We somehow got a value not covered by the autogenerated