1 /* jit-builtins.c -- Handling of builtin functions during JIT-compilation.
2 Copyright (C) 2014 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"
26 #include "stringpool.h"
28 #include "jit-common.h"
29 #include "jit-builtins.h"
30 #include "jit-recording.h"
31 #include "jit-playback.h"
37 const char *const prefix
= "__builtin_";
38 const size_t prefix_len
= strlen (prefix
);
40 /* Create "builtin_data", a const table of the data within builtins.def. */
44 enum built_in_class fnclass
;
45 enum jit_builtin_type type
;
48 enum built_in_attribute attr
;
51 const char *get_asm_name () const
53 if (both_p
&& fallback_p
)
54 return name
+ prefix_len
;
60 #define DEF_BUILTIN(X, NAME, CLASS, TYPE, LT, BOTH_P, FALLBACK_P, \
61 NONANSI_P, ATTRS, IMPLICIT, COND) \
62 {NAME, CLASS, TYPE, BOTH_P, FALLBACK_P, ATTRS, IMPLICIT},
63 static const struct builtin_data builtin_data
[] =
65 #include "builtins.def"
69 /* Helper function for find_builtin_by_name. */
72 matches_builtin (const char *in_name
,
73 const struct builtin_data
& bd
)
79 fprintf (stderr
, "seen builtin: %s\n", bd
.name
);
81 if (0 == strcmp (bd
.name
, in_name
))
88 /* Then the macros in builtins.def gave a "__builtin_"
89 prefix to bd.name, but we should also recognize the form
90 without the prefix. */
91 gcc_assert (0 == strncmp (bd
.name
, prefix
, prefix_len
));
93 fprintf (stderr
, "testing without prefix as: %s\n",
94 bd
.name
+ prefix_len
);
95 if (0 == strcmp (bd
.name
+ prefix_len
, in_name
))
104 /* Locate the built-in function that matches name IN_NAME,
105 writing the result to OUT_ID and returning true if found,
106 or returning false if not found. */
109 find_builtin_by_name (const char *in_name
,
110 enum built_in_function
*out_id
)
112 /* Locate builtin. This currently works by performing repeated
113 strcmp against every possible candidate, which is likely to
116 We start at index 1 to skip the initial entry (BUILT_IN_NONE), which
118 for (unsigned int i
= 1;
119 i
< sizeof (builtin_data
) / sizeof (builtin_data
[0]);
122 const struct builtin_data
& bd
= builtin_data
[i
];
123 if (matches_builtin (in_name
, bd
))
126 *out_id
= static_cast<enum built_in_function
> (i
);
135 // class builtins_manager
137 /* Constructor for gcc::jit::builtins_manager. */
139 builtins_manager::builtins_manager (recording::context
*ctxt
)
142 memset (m_types
, 0, sizeof (m_types
));
143 memset (m_builtin_functions
, 0, sizeof (m_builtin_functions
));
144 memset (m_attributes
, 0, sizeof (m_attributes
));
147 /* Locate a builtin function by name.
148 Create a recording::function of the appropriate type, reusing them
149 if they've already been seen. */
151 recording::function
*
152 builtins_manager::get_builtin_function (const char *name
)
154 enum built_in_function builtin_id
;
155 if (!find_builtin_by_name (name
, &builtin_id
))
157 m_ctxt
->add_error (NULL
, "builtin \"%s\" not found", name
);
161 return get_builtin_function_by_id (builtin_id
);
164 /* Locate a builtin function by id.
165 Create a recording::function of the appropriate type, reusing them
166 if they've already been seen. */
168 recording::function
*
169 builtins_manager::get_builtin_function_by_id (enum built_in_function builtin_id
)
171 gcc_assert (builtin_id
>= 0);
172 gcc_assert (builtin_id
< END_BUILTINS
);
174 /* Lazily build the functions, caching them so that repeated calls for
175 the same id on a context give back the same object. */
176 if (!m_builtin_functions
[builtin_id
])
178 recording::function
*fn
= make_builtin_function (builtin_id
);
181 m_builtin_functions
[builtin_id
] = fn
;
186 return m_builtin_functions
[builtin_id
];
189 /* Create the recording::function for a given builtin function, by ID. */
191 recording::function
*
192 builtins_manager::make_builtin_function (enum built_in_function builtin_id
)
194 const struct builtin_data
& bd
= builtin_data
[builtin_id
];
195 enum jit_builtin_type type_id
= bd
.type
;
196 recording::type
*t
= get_type (type_id
);
199 recording::function_type
*func_type
= t
->as_a_function_type ();
203 vec
<recording::type
*> param_types
= func_type
->get_param_types ();
204 recording::param
**params
= new recording::param
*[param_types
.length ()];
207 recording::type
*param_type
;
208 FOR_EACH_VEC_ELT (param_types
, i
, param_type
)
211 snprintf (buf
, 16, "arg%d", i
);
212 params
[i
] = m_ctxt
->new_param (NULL
,
216 const char *asm_name
= bd
.get_asm_name ();
217 recording::function
*result
=
218 new recording::function (m_ctxt
,
220 GCC_JIT_FUNCTION_IMPORTED
, // FIXME
221 func_type
->get_return_type (),
222 m_ctxt
->new_string (asm_name
),
223 param_types
.length (),
225 func_type
->is_variadic (),
229 /* PR/64020 - If the client code is using builtin cos or sin,
230 tree-ssa-math-opt.c's execute_cse_sincos_1 may attempt
231 to optimize them to use __builtin_cexpi; for this,
232 BUILT_IN_CEXPI needs to exist.
234 Hence query the cache for BUILT_IN_CEXPI to ensure it gets
236 if (builtin_id
== BUILT_IN_COS
|| builtin_id
== BUILT_IN_SIN
)
237 (void)get_builtin_function_by_id (BUILT_IN_CEXPI
);
239 /* builtins.c:expand_builtin_cexpi can optimize the various
240 CEXP builtins to SINCOS builtins, and hence we may require
241 SINCOS builtins latter.
243 Ensure the appropriate SINCOS builtin exists. */
244 if (builtin_id
== BUILT_IN_CEXPIF
)
245 (void)get_builtin_function_by_id (BUILT_IN_SINCOSF
);
246 else if (builtin_id
== BUILT_IN_CEXPI
)
247 (void)get_builtin_function_by_id (BUILT_IN_SINCOS
);
248 else if (builtin_id
== BUILT_IN_CEXPIL
)
249 (void)get_builtin_function_by_id (BUILT_IN_SINCOSL
);
254 /* Get the recording::type for a given type of builtin function,
255 by ID, creating it if it doesn't already exist. */
258 builtins_manager::get_type (enum jit_builtin_type type_id
)
260 if (!m_types
[type_id
])
261 m_types
[type_id
] = make_type (type_id
);
262 return m_types
[type_id
];
265 /* Create the recording::type for a given type of builtin function. */
268 builtins_manager::make_type (enum jit_builtin_type type_id
)
270 /* Use builtin-types.def to construct a switch statement, with each
271 case deferring to one of the methods below:
272 - DEF_PRIMITIVE_TYPE is handled as a call to make_primitive_type.
273 - the various DEF_FUNCTION_TYPE_n are handled by variadic calls
275 - similarly for DEF_FUNCTION_TYPE_VAR_n, but setting the
276 "is_variadic" argument.
277 - DEF_POINTER_TYPE is handled by make_ptr_type.
278 That should handle everything, but just in case we also suppy a
279 gcc_unreachable default clause. */
282 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
283 case ENUM: return make_primitive_type (ENUM);
284 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
285 case ENUM: return make_fn_type (ENUM, RETURN, 0, 0);
286 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
287 case ENUM: return make_fn_type (ENUM, RETURN, 0, 1, ARG1);
288 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
289 case ENUM: return make_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
290 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
291 case ENUM: return make_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
292 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
293 case ENUM: return make_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
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, ARG4, ARG5);
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, ARG4, ARG5, ARG6);
299 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
301 case ENUM: return make_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
302 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
304 case ENUM: return make_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
306 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
307 case ENUM: return make_fn_type (ENUM, RETURN, 1, 0);
308 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
309 case ENUM: return make_fn_type (ENUM, RETURN, 1, 1, ARG1);
310 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
311 case ENUM: return make_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
312 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
313 case ENUM: return make_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
314 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
315 case ENUM: return make_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
316 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
317 case ENUM: return make_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
318 #define DEF_POINTER_TYPE(ENUM, TYPE) \
319 case ENUM: return make_ptr_type (ENUM, TYPE);
321 #include "builtin-types.def"
323 #undef DEF_PRIMITIVE_TYPE
324 #undef DEF_FUNCTION_TYPE_0
325 #undef DEF_FUNCTION_TYPE_1
326 #undef DEF_FUNCTION_TYPE_2
327 #undef DEF_FUNCTION_TYPE_3
328 #undef DEF_FUNCTION_TYPE_4
329 #undef DEF_FUNCTION_TYPE_5
330 #undef DEF_FUNCTION_TYPE_6
331 #undef DEF_FUNCTION_TYPE_7
332 #undef DEF_FUNCTION_TYPE_8
333 #undef DEF_FUNCTION_TYPE_VAR_0
334 #undef DEF_FUNCTION_TYPE_VAR_1
335 #undef DEF_FUNCTION_TYPE_VAR_2
336 #undef DEF_FUNCTION_TYPE_VAR_3
337 #undef DEF_FUNCTION_TYPE_VAR_4
338 #undef DEF_FUNCTION_TYPE_VAR_5
339 #undef DEF_POINTER_TYPE
346 /* Create the recording::type for a given primitive type within the
349 Only some types are currently supported. */
352 builtins_manager::make_primitive_type (enum jit_builtin_type type_id
)
357 // only some of these types are implemented so far:
358 m_ctxt
->add_error (NULL
,
359 "unimplemented primitive type for builtin: %d", type_id
);
362 case BT_VOID
: return m_ctxt
->get_type (GCC_JIT_TYPE_VOID
);
363 case BT_BOOL
: return m_ctxt
->get_type (GCC_JIT_TYPE_BOOL
);
364 case BT_INT
: return m_ctxt
->get_type (GCC_JIT_TYPE_INT
);
365 case BT_UINT
: return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_INT
);
366 case BT_LONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG
);
367 case BT_ULONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_LONG
);
368 case BT_LONGLONG
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG_LONG
);
370 return m_ctxt
->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG
);
375 case BT_UINT16
: return m_ctxt
->get_int_type (2, false);
376 case BT_UINT32
: return m_ctxt
->get_int_type (4, false);
377 case BT_UINT64
: return m_ctxt
->get_int_type (8, false);
379 // case BT_UNWINDWORD:
380 case BT_FLOAT
: return m_ctxt
->get_type (GCC_JIT_TYPE_FLOAT
);
381 case BT_DOUBLE
: return m_ctxt
->get_type (GCC_JIT_TYPE_DOUBLE
);
382 case BT_LONGDOUBLE
: return m_ctxt
->get_type (GCC_JIT_TYPE_LONG_DOUBLE
);
383 case BT_COMPLEX_FLOAT
:
384 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT
);
385 case BT_COMPLEX_DOUBLE
:
386 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_DOUBLE
);
387 case BT_COMPLEX_LONGDOUBLE
:
388 return m_ctxt
->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE
);
389 case BT_PTR
: return m_ctxt
->get_type (GCC_JIT_TYPE_VOID_PTR
);
390 case BT_FILEPTR
: return m_ctxt
->get_type (GCC_JIT_TYPE_FILE_PTR
);
392 // case BT_VOLATILE_PTR:
393 // case BT_CONST_VOLATILE_PTR:
396 // case BT_FLOAT_PTR:
398 return m_ctxt
->get_type (GCC_JIT_TYPE_DOUBLE
)->get_pointer ();
399 // case BT_CONST_DOUBLE_PTR:
400 // case BT_LONGDOUBLE_PTR:
406 case BT_CONST_STRING
: return m_ctxt
->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR
);
409 // case BT_DFLOAT128:
410 // case BT_DFLOAT32_PTR:
411 // case BT_DFLOAT64_PTR:
412 // case BT_DFLOAT128_PTR:
413 // case BT_VALIST_REF:
414 // case BT_VALIST_ARG:
423 /* Create the recording::function_type for a given function type
426 recording::function_type
*
427 builtins_manager::make_fn_type (enum jit_builtin_type
,
428 enum jit_builtin_type return_type_id
,
434 recording::type
**param_types
= new recording::type
*[num_args
];
435 recording::type
*return_type
= NULL
;
436 recording::function_type
*result
= NULL
;
438 va_start (list
, num_args
);
439 for (i
= 0; i
< num_args
; ++i
)
441 enum jit_builtin_type arg_type_id
=
442 (enum jit_builtin_type
) va_arg (list
, int);
443 param_types
[i
] = get_type (arg_type_id
);
449 return_type
= get_type (return_type_id
);
453 result
= m_ctxt
->new_function_type (return_type
,
459 delete[] param_types
;
463 /* Handler for DEF_POINTER_TYPE within builtins_manager::make_type. */
466 builtins_manager::make_ptr_type (enum jit_builtin_type
,
467 enum jit_builtin_type other_type_id
)
469 recording::type
*base_type
= get_type (other_type_id
);
470 return base_type
->get_pointer ();
473 /* Playback support. */
475 /* A builtins_manager is associated with a recording::context
476 and might be reused for multiple compiles on various
477 playback::contexts, perhaps with different options.
479 Purge any playback state. Currently this is just the table of
483 builtins_manager::finish_playback (void)
485 memset (m_attributes
, 0, sizeof (m_attributes
));
488 /* Get the enum built_in_class for BUILTIN_ID. */
491 builtins_manager::get_class (enum built_in_function builtin_id
)
493 return builtin_data
[builtin_id
].fnclass
;
496 /* Is BUILTIN_ID implicit? */
499 builtins_manager::implicit_p (enum built_in_function builtin_id
)
501 return builtin_data
[builtin_id
].implicit_p
;
504 /* Get any attributes (in tree form) for the function declaration
507 These are created on-demand, and cached within the m_attributes
508 array, until finish_playback. */
511 builtins_manager::get_attrs_tree (enum built_in_function builtin_id
)
513 enum built_in_attribute attr
= builtin_data
[builtin_id
].attr
;
514 return get_attrs_tree (attr
);
517 /* As above, but for an enum built_in_attribute. */
520 builtins_manager::get_attrs_tree (enum built_in_attribute attr
)
522 gcc_assert (attr
< ATTR_LAST
);
523 if (!m_attributes
[attr
])
524 m_attributes
[attr
] = make_attrs_tree (attr
);
525 return m_attributes
[attr
];
528 /* Handle a cache-miss within the m_attributes array by
529 generating the attributes for enum built_in_attribute
533 builtins_manager::make_attrs_tree (enum built_in_attribute attr
)
537 /* Generate cases from builtin-attrs.def. */
538 #define DEF_ATTR_NULL_TREE(ENUM) \
539 case ENUM: return NULL_TREE;
540 #define DEF_ATTR_INT(ENUM, VALUE) \
541 case ENUM: return build_int_cst (integer_type_node, VALUE);
542 #define DEF_ATTR_STRING(ENUM, VALUE) \
543 case ENUM: return build_string (strlen (VALUE), VALUE);
544 #define DEF_ATTR_IDENT(ENUM, STRING) \
545 case ENUM: return get_identifier (STRING);
546 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
547 case ENUM: return tree_cons (get_attrs_tree (PURPOSE), \
548 get_attrs_tree (VALUE), \
549 get_attrs_tree (CHAIN));
550 #include "builtin-attrs.def"
551 #undef DEF_ATTR_NULL_TREE
553 #undef DEF_ATTR_IDENT
554 #undef DEF_ATTR_TREE_LIST
557 /* We somehow got a value not covered by the autogenerated