Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / lto / lto-lang.c
blob974e3d7328bdcf8ed1c4c0e893634513b75d8826
1 /* Language-dependent hooks for LTO.
2 Copyright 2009 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "flags.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "target.h"
28 #include "langhooks.h"
29 #include "langhooks-def.h"
30 #include "debug.h"
31 #include "lto-tree.h"
32 #include "lto.h"
33 #include "tree-inline.h"
34 #include "gimple.h"
35 #include "toplev.h"
37 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
38 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
39 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
40 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
41 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
42 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
43 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
44 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
45 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
46 static tree handle_format_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_format_arg_attribute (tree *, tree, tree, int, bool *);
49 /* Table of machine-independent attributes supported in GIMPLE. */
50 const struct attribute_spec lto_attribute_table[] =
52 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
53 { "noreturn", 0, 0, true, false, false,
54 handle_noreturn_attribute },
55 /* The same comments as for noreturn attributes apply to const ones. */
56 { "const", 0, 0, true, false, false,
57 handle_const_attribute },
58 { "malloc", 0, 0, true, false, false,
59 handle_malloc_attribute },
60 { "pure", 0, 0, true, false, false,
61 handle_pure_attribute },
62 { "no vops", 0, 0, true, false, false,
63 handle_novops_attribute },
64 { "nonnull", 0, -1, false, true, true,
65 handle_nonnull_attribute },
66 { "nothrow", 0, 0, true, false, false,
67 handle_nothrow_attribute },
68 { "sentinel", 0, 1, false, true, true,
69 handle_sentinel_attribute },
70 { "type generic", 0, 0, false, true, true,
71 handle_type_generic_attribute },
72 { NULL, 0, 0, false, false, false, NULL }
75 /* Give the specifications for the format attributes, used by C and all
76 descendants. */
78 const struct attribute_spec lto_format_attribute_table[] =
80 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
81 { "format", 3, 3, false, true, true,
82 handle_format_attribute },
83 { "format_arg", 1, 1, false, true, true,
84 handle_format_arg_attribute },
85 { NULL, 0, 0, false, false, false, NULL }
88 enum built_in_attribute
90 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
91 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
92 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
93 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
94 #include "builtin-attrs.def"
95 #undef DEF_ATTR_NULL_TREE
96 #undef DEF_ATTR_INT
97 #undef DEF_ATTR_IDENT
98 #undef DEF_ATTR_TREE_LIST
99 ATTR_LAST
102 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
104 /* Builtin types. */
106 enum lto_builtin_type
108 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
109 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
110 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
111 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
112 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
113 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
114 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
115 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
116 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
117 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
118 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
119 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
120 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
121 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
122 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
123 NAME,
124 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
125 #include "builtin-types.def"
126 #undef DEF_PRIMITIVE_TYPE
127 #undef DEF_FUNCTION_TYPE_0
128 #undef DEF_FUNCTION_TYPE_1
129 #undef DEF_FUNCTION_TYPE_2
130 #undef DEF_FUNCTION_TYPE_3
131 #undef DEF_FUNCTION_TYPE_4
132 #undef DEF_FUNCTION_TYPE_5
133 #undef DEF_FUNCTION_TYPE_6
134 #undef DEF_FUNCTION_TYPE_7
135 #undef DEF_FUNCTION_TYPE_VAR_0
136 #undef DEF_FUNCTION_TYPE_VAR_1
137 #undef DEF_FUNCTION_TYPE_VAR_2
138 #undef DEF_FUNCTION_TYPE_VAR_3
139 #undef DEF_FUNCTION_TYPE_VAR_4
140 #undef DEF_FUNCTION_TYPE_VAR_5
141 #undef DEF_POINTER_TYPE
142 BT_LAST
145 typedef enum lto_builtin_type builtin_type;
147 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
149 static GTY(()) tree string_type_node;
150 static GTY(()) tree const_string_type_node;
151 static GTY(()) tree wint_type_node;
152 static GTY(()) tree intmax_type_node;
153 static GTY(()) tree uintmax_type_node;
154 static GTY(()) tree signed_size_type_node;
156 /* Flags needed to process builtins.def. */
157 int flag_no_builtin;
158 int flag_no_nonansi_builtin;
159 int flag_isoc94;
160 int flag_isoc99;
162 /* Attribute handlers. */
164 /* Handle a "noreturn" attribute; arguments as in
165 struct attribute_spec.handler. */
167 static tree
168 handle_noreturn_attribute (tree *node, tree ARG_UNUSED (name),
169 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
170 bool * ARG_UNUSED (no_add_attrs))
172 tree type = TREE_TYPE (*node);
174 if (TREE_CODE (*node) == FUNCTION_DECL)
175 TREE_THIS_VOLATILE (*node) = 1;
176 else if (TREE_CODE (type) == POINTER_TYPE
177 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
178 TREE_TYPE (*node)
179 = build_pointer_type
180 (build_type_variant (TREE_TYPE (type),
181 TYPE_READONLY (TREE_TYPE (type)), 1));
182 else
183 gcc_unreachable ();
185 return NULL_TREE;
189 /* Handle a "const" attribute; arguments as in
190 struct attribute_spec.handler. */
192 static tree
193 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
194 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
195 bool * ARG_UNUSED (no_add_attrs))
197 tree type = TREE_TYPE (*node);
199 /* See FIXME comment on noreturn in c_common_attribute_table. */
200 if (TREE_CODE (*node) == FUNCTION_DECL)
201 TREE_READONLY (*node) = 1;
202 else if (TREE_CODE (type) == POINTER_TYPE
203 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
204 TREE_TYPE (*node)
205 = build_pointer_type
206 (build_type_variant (TREE_TYPE (type), 1,
207 TREE_THIS_VOLATILE (TREE_TYPE (type))));
208 else
209 gcc_unreachable ();
211 return NULL_TREE;
215 /* Handle a "malloc" attribute; arguments as in
216 struct attribute_spec.handler. */
218 static tree
219 handle_malloc_attribute (tree *node, tree ARG_UNUSED (name),
220 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
221 bool * ARG_UNUSED (no_add_attrs))
223 if (TREE_CODE (*node) == FUNCTION_DECL
224 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
225 DECL_IS_MALLOC (*node) = 1;
226 else
227 gcc_unreachable ();
229 return NULL_TREE;
233 /* Handle a "pure" attribute; arguments as in
234 struct attribute_spec.handler. */
236 static tree
237 handle_pure_attribute (tree *node, tree ARG_UNUSED (name),
238 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
239 bool * ARG_UNUSED (no_add_attrs))
241 if (TREE_CODE (*node) == FUNCTION_DECL)
242 DECL_PURE_P (*node) = 1;
243 else
244 gcc_unreachable ();
246 return NULL_TREE;
250 /* Handle a "no vops" attribute; arguments as in
251 struct attribute_spec.handler. */
253 static tree
254 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
255 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
256 bool *ARG_UNUSED (no_add_attrs))
258 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
259 DECL_IS_NOVOPS (*node) = 1;
260 return NULL_TREE;
264 /* Helper for nonnull attribute handling; fetch the operand number
265 from the attribute argument list. */
267 static bool
268 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
270 /* Verify the arg number is a constant. */
271 if (TREE_CODE (arg_num_expr) != INTEGER_CST
272 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
273 return false;
275 *valp = TREE_INT_CST_LOW (arg_num_expr);
276 return true;
279 /* Handle the "nonnull" attribute. */
281 static tree
282 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
283 tree args, int ARG_UNUSED (flags),
284 bool * ARG_UNUSED (no_add_attrs))
286 tree type = *node;
288 /* If no arguments are specified, all pointer arguments should be
289 non-null. Verify a full prototype is given so that the arguments
290 will have the correct types when we actually check them later. */
291 if (!args)
293 gcc_assert (TYPE_ARG_TYPES (type));
294 return NULL_TREE;
297 /* Argument list specified. Verify that each argument number references
298 a pointer argument. */
299 for (; args; args = TREE_CHAIN (args))
301 tree argument;
302 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
304 if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
305 gcc_unreachable ();
307 argument = TYPE_ARG_TYPES (type);
308 if (argument)
310 for (ck_num = 1; ; ck_num++)
312 if (!argument || ck_num == arg_num)
313 break;
314 argument = TREE_CHAIN (argument);
317 gcc_assert (argument
318 && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
322 return NULL_TREE;
326 /* Handle a "nothrow" attribute; arguments as in
327 struct attribute_spec.handler. */
329 static tree
330 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
331 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
332 bool * ARG_UNUSED (no_add_attrs))
334 if (TREE_CODE (*node) == FUNCTION_DECL)
335 TREE_NOTHROW (*node) = 1;
336 else
337 gcc_unreachable ();
339 return NULL_TREE;
343 /* Handle a "sentinel" attribute. */
345 static tree
346 handle_sentinel_attribute (tree *node, tree ARG_UNUSED (name), tree args,
347 int ARG_UNUSED (flags),
348 bool * ARG_UNUSED (no_add_attrs))
350 tree params = TYPE_ARG_TYPES (*node);
351 gcc_assert (params);
353 while (TREE_CHAIN (params))
354 params = TREE_CHAIN (params);
356 gcc_assert (!VOID_TYPE_P (TREE_VALUE (params)));
358 if (args)
360 tree position = TREE_VALUE (args);
361 gcc_assert (TREE_CODE (position) == INTEGER_CST);
362 if (tree_int_cst_lt (position, integer_zero_node))
363 gcc_unreachable ();
366 return NULL_TREE;
369 /* Handle a "type_generic" attribute. */
371 static tree
372 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
373 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
374 bool * ARG_UNUSED (no_add_attrs))
376 tree params;
378 /* Ensure we have a function type. */
379 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
381 params = TYPE_ARG_TYPES (*node);
382 while (params && ! VOID_TYPE_P (TREE_VALUE (params)))
383 params = TREE_CHAIN (params);
385 /* Ensure we have a variadic function. */
386 gcc_assert (!params);
388 return NULL_TREE;
391 /* Handle a "format" attribute; arguments as in
392 struct attribute_spec.handler. */
394 static tree
395 handle_format_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
396 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
397 bool *no_add_attrs)
399 *no_add_attrs = true;
400 return NULL_TREE;
404 /* Handle a "format_arg" attribute; arguments as in
405 struct attribute_spec.handler. */
407 tree
408 handle_format_arg_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
409 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
410 bool *no_add_attrs)
412 *no_add_attrs = true;
413 return NULL_TREE;
417 /* Cribbed from c-common.c. */
419 static void
420 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
422 tree args = NULL, t;
423 va_list list;
424 int i;
426 va_start (list, n);
427 for (i = 0; i < n; ++i)
429 builtin_type a = (builtin_type) va_arg (list, int);
430 t = builtin_types[a];
431 if (t == error_mark_node)
432 goto egress;
433 args = tree_cons (NULL_TREE, t, args);
435 va_end (list);
437 args = nreverse (args);
438 if (!var)
439 args = chainon (args, void_list_node);
441 t = builtin_types[ret];
442 if (t == error_mark_node)
443 goto egress;
444 t = build_function_type (t, args);
446 egress:
447 builtin_types[def] = t;
450 /* Used to help initialize the builtin-types.def table. When a type of
451 the correct size doesn't exist, use error_mark_node instead of NULL.
452 The later results in segfaults even when a decl using the type doesn't
453 get invoked. */
455 static tree
456 builtin_type_for_size (int size, bool unsignedp)
458 tree type = lang_hooks.types.type_for_size (size, unsignedp);
459 return type ? type : error_mark_node;
462 /* Support for DEF_BUILTIN. */
464 static void
465 def_builtin_1 (enum built_in_function fncode, const char *name,
466 enum built_in_class fnclass, tree fntype, tree libtype,
467 bool both_p, bool fallback_p, bool nonansi_p,
468 tree fnattrs, bool implicit_p)
470 tree decl;
471 const char *libname;
473 if (fntype == error_mark_node)
474 return;
476 libname = name + strlen ("__builtin_");
477 decl = add_builtin_function (name, fntype, fncode, fnclass,
478 (fallback_p ? libname : NULL),
479 fnattrs);
481 if (both_p
482 && !flag_no_builtin
483 && !(nonansi_p && flag_no_nonansi_builtin))
484 add_builtin_function (libname, libtype, fncode, fnclass,
485 NULL, fnattrs);
487 built_in_decls[(int) fncode] = decl;
488 if (implicit_p)
489 implicit_built_in_decls[(int) fncode] = decl;
493 /* Initialize the attribute table for all the supported builtins. */
495 static void
496 lto_init_attributes (void)
498 /* Fill in the built_in_attributes array. */
499 #define DEF_ATTR_NULL_TREE(ENUM) \
500 built_in_attributes[(int) ENUM] = NULL_TREE;
501 #define DEF_ATTR_INT(ENUM, VALUE) \
502 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
503 #define DEF_ATTR_IDENT(ENUM, STRING) \
504 built_in_attributes[(int) ENUM] = get_identifier (STRING);
505 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
506 built_in_attributes[(int) ENUM] \
507 = tree_cons (built_in_attributes[(int) PURPOSE], \
508 built_in_attributes[(int) VALUE], \
509 built_in_attributes[(int) CHAIN]);
510 #include "builtin-attrs.def"
511 #undef DEF_ATTR_NULL_TREE
512 #undef DEF_ATTR_INT
513 #undef DEF_ATTR_IDENT
514 #undef DEF_ATTR_TREE_LIST
517 /* Create builtin types and functions. VA_LIST_REF_TYPE_NODE and
518 VA_LIST_ARG_TYPE_NODE are used in builtin-types.def. */
520 static void
521 lto_define_builtins (tree va_list_ref_type_node ATTRIBUTE_UNUSED,
522 tree va_list_arg_type_node ATTRIBUTE_UNUSED)
524 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
525 builtin_types[ENUM] = VALUE;
526 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
527 def_fn_type (ENUM, RETURN, 0, 0);
528 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
529 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
530 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
531 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
532 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
533 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
534 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
535 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
536 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
537 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
538 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
539 ARG6) \
540 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
541 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
542 ARG6, ARG7) \
543 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
544 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
545 def_fn_type (ENUM, RETURN, 1, 0);
546 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
547 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
548 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
549 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
550 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
551 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
552 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
553 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
554 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
555 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
556 #define DEF_POINTER_TYPE(ENUM, TYPE) \
557 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
559 #include "builtin-types.def"
561 #undef DEF_PRIMITIVE_TYPE
562 #undef DEF_FUNCTION_TYPE_1
563 #undef DEF_FUNCTION_TYPE_2
564 #undef DEF_FUNCTION_TYPE_3
565 #undef DEF_FUNCTION_TYPE_4
566 #undef DEF_FUNCTION_TYPE_5
567 #undef DEF_FUNCTION_TYPE_6
568 #undef DEF_FUNCTION_TYPE_VAR_0
569 #undef DEF_FUNCTION_TYPE_VAR_1
570 #undef DEF_FUNCTION_TYPE_VAR_2
571 #undef DEF_FUNCTION_TYPE_VAR_3
572 #undef DEF_FUNCTION_TYPE_VAR_4
573 #undef DEF_FUNCTION_TYPE_VAR_5
574 #undef DEF_POINTER_TYPE
575 builtin_types[(int) BT_LAST] = NULL_TREE;
577 lto_init_attributes ();
579 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P,\
580 NONANSI_P, ATTRS, IMPLICIT, COND) \
581 if (NAME && COND) \
582 def_builtin_1 (ENUM, NAME, CLASS, builtin_types[(int) TYPE], \
583 builtin_types[(int) LIBTYPE], BOTH_P, FALLBACK_P, \
584 NONANSI_P, built_in_attributes[(int) ATTRS], IMPLICIT);
585 #include "builtins.def"
586 #undef DEF_BUILTIN
589 static GTY(()) tree registered_builtin_types;
591 /* A chain of builtin functions that we need to recognize. We will
592 assume that all other function names we see will be defined by the
593 user's program. */
594 static GTY(()) tree registered_builtin_fndecls;
596 /* Language hooks. */
598 static unsigned int
599 lto_init_options (unsigned int argc ATTRIBUTE_UNUSED,
600 const char **argv ATTRIBUTE_UNUSED)
602 /* By default, C99-like requirements for complex multiply and divide.
603 ??? Until the complex method is encoded in the IL this is the only
604 safe choice. This will pessimize Fortran code with LTO unless
605 people specify a complex method manually or use -ffast-math. */
606 flag_complex_method = 2;
608 return CL_LTO;
611 /* Handle command-line option SCODE. If the option takes an argument, it is
612 stored in ARG, which is otherwise NULL. VALUE holds either a numerical
613 argument or a binary value indicating whether the positive or negative form
614 of the option was supplied. */
616 const char *resolution_file_name;
617 static int
618 lto_handle_option (size_t scode, const char *arg,
619 int value ATTRIBUTE_UNUSED, int kind ATTRIBUTE_UNUSED)
621 enum opt_code code = (enum opt_code) scode;
622 int result = 1;
624 switch (code)
626 case OPT_fresolution_:
627 resolution_file_name = arg;
628 result = 1;
629 break;
631 case OPT_Wabi:
632 warn_psabi = value;
633 break;
635 case OPT_fsigned_char:
636 flag_signed_char = value;
637 break;
639 case OPT_funsigned_char:
640 flag_signed_char = !value;
641 break;
643 default:
644 break;
647 return result;
650 /* Perform post-option processing. Does additional initialization based on
651 command-line options. PFILENAME is the main input filename. Returns false
652 to enable subsequent back-end initialization. */
654 static bool
655 lto_post_options (const char **pfilename ATTRIBUTE_UNUSED)
657 /* -fltrans and -fwpa are mutually exclusive. Check for that here. */
658 if (flag_wpa && flag_ltrans)
659 error ("-fwpa and -fltrans are mutually exclusive");
661 if (flag_ltrans)
663 flag_generate_lto = 0;
665 /* During LTRANS, we are not looking at the whole program, only
666 a subset of the whole callgraph. */
667 flag_whole_program = 0;
670 if (flag_wpa)
671 flag_generate_lto = 1;
673 /* Excess precision other than "fast" requires front-end
674 support. */
675 flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
677 lto_read_all_file_options ();
679 /* Initialize the compiler back end. */
680 return false;
683 /* Return an integer type with PRECISION bits of precision,
684 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
686 static tree
687 lto_type_for_size (unsigned precision, int unsignedp)
689 if (precision == TYPE_PRECISION (integer_type_node))
690 return unsignedp ? unsigned_type_node : integer_type_node;
692 if (precision == TYPE_PRECISION (signed_char_type_node))
693 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
695 if (precision == TYPE_PRECISION (short_integer_type_node))
696 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
698 if (precision == TYPE_PRECISION (long_integer_type_node))
699 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
701 if (precision == TYPE_PRECISION (long_long_integer_type_node))
702 return unsignedp
703 ? long_long_unsigned_type_node
704 : long_long_integer_type_node;
706 if (precision <= TYPE_PRECISION (intQI_type_node))
707 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
709 if (precision <= TYPE_PRECISION (intHI_type_node))
710 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
712 if (precision <= TYPE_PRECISION (intSI_type_node))
713 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
715 if (precision <= TYPE_PRECISION (intDI_type_node))
716 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
718 if (precision <= TYPE_PRECISION (intTI_type_node))
719 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
721 return NULL_TREE;
725 /* Return a data type that has machine mode MODE.
726 If the mode is an integer,
727 then UNSIGNEDP selects between signed and unsigned types.
728 If the mode is a fixed-point mode,
729 then UNSIGNEDP selects between saturating and nonsaturating types. */
731 static tree
732 lto_type_for_mode (enum machine_mode mode, int unsigned_p)
734 tree t;
736 if (mode == TYPE_MODE (integer_type_node))
737 return unsigned_p ? unsigned_type_node : integer_type_node;
739 if (mode == TYPE_MODE (signed_char_type_node))
740 return unsigned_p ? unsigned_char_type_node : signed_char_type_node;
742 if (mode == TYPE_MODE (short_integer_type_node))
743 return unsigned_p ? short_unsigned_type_node : short_integer_type_node;
745 if (mode == TYPE_MODE (long_integer_type_node))
746 return unsigned_p ? long_unsigned_type_node : long_integer_type_node;
748 if (mode == TYPE_MODE (long_long_integer_type_node))
749 return unsigned_p ? long_long_unsigned_type_node : long_long_integer_type_node;
751 if (mode == QImode)
752 return unsigned_p ? unsigned_intQI_type_node : intQI_type_node;
754 if (mode == HImode)
755 return unsigned_p ? unsigned_intHI_type_node : intHI_type_node;
757 if (mode == SImode)
758 return unsigned_p ? unsigned_intSI_type_node : intSI_type_node;
760 if (mode == DImode)
761 return unsigned_p ? unsigned_intDI_type_node : intDI_type_node;
763 #if HOST_BITS_PER_WIDE_INT >= 64
764 if (mode == TYPE_MODE (intTI_type_node))
765 return unsigned_p ? unsigned_intTI_type_node : intTI_type_node;
766 #endif
768 if (mode == TYPE_MODE (float_type_node))
769 return float_type_node;
771 if (mode == TYPE_MODE (double_type_node))
772 return double_type_node;
774 if (mode == TYPE_MODE (long_double_type_node))
775 return long_double_type_node;
777 if (mode == TYPE_MODE (void_type_node))
778 return void_type_node;
780 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
781 return (unsigned_p
782 ? make_unsigned_type (GET_MODE_PRECISION (mode))
783 : make_signed_type (GET_MODE_PRECISION (mode)));
785 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
786 return (unsigned_p
787 ? make_unsigned_type (GET_MODE_PRECISION (mode))
788 : make_signed_type (GET_MODE_PRECISION (mode)));
790 if (COMPLEX_MODE_P (mode))
792 enum machine_mode inner_mode;
793 tree inner_type;
795 if (mode == TYPE_MODE (complex_float_type_node))
796 return complex_float_type_node;
797 if (mode == TYPE_MODE (complex_double_type_node))
798 return complex_double_type_node;
799 if (mode == TYPE_MODE (complex_long_double_type_node))
800 return complex_long_double_type_node;
802 if (mode == TYPE_MODE (complex_integer_type_node) && !unsigned_p)
803 return complex_integer_type_node;
805 inner_mode = GET_MODE_INNER (mode);
806 inner_type = lto_type_for_mode (inner_mode, unsigned_p);
807 if (inner_type != NULL_TREE)
808 return build_complex_type (inner_type);
810 else if (VECTOR_MODE_P (mode))
812 enum machine_mode inner_mode = GET_MODE_INNER (mode);
813 tree inner_type = lto_type_for_mode (inner_mode, unsigned_p);
814 if (inner_type != NULL_TREE)
815 return build_vector_type_for_mode (inner_type, mode);
818 if (mode == TYPE_MODE (dfloat32_type_node))
819 return dfloat32_type_node;
820 if (mode == TYPE_MODE (dfloat64_type_node))
821 return dfloat64_type_node;
822 if (mode == TYPE_MODE (dfloat128_type_node))
823 return dfloat128_type_node;
825 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
827 if (mode == TYPE_MODE (short_fract_type_node))
828 return unsigned_p ? sat_short_fract_type_node : short_fract_type_node;
829 if (mode == TYPE_MODE (fract_type_node))
830 return unsigned_p ? sat_fract_type_node : fract_type_node;
831 if (mode == TYPE_MODE (long_fract_type_node))
832 return unsigned_p ? sat_long_fract_type_node : long_fract_type_node;
833 if (mode == TYPE_MODE (long_long_fract_type_node))
834 return unsigned_p ? sat_long_long_fract_type_node
835 : long_long_fract_type_node;
837 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
838 return unsigned_p ? sat_unsigned_short_fract_type_node
839 : unsigned_short_fract_type_node;
840 if (mode == TYPE_MODE (unsigned_fract_type_node))
841 return unsigned_p ? sat_unsigned_fract_type_node
842 : unsigned_fract_type_node;
843 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
844 return unsigned_p ? sat_unsigned_long_fract_type_node
845 : unsigned_long_fract_type_node;
846 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
847 return unsigned_p ? sat_unsigned_long_long_fract_type_node
848 : unsigned_long_long_fract_type_node;
850 if (mode == TYPE_MODE (short_accum_type_node))
851 return unsigned_p ? sat_short_accum_type_node : short_accum_type_node;
852 if (mode == TYPE_MODE (accum_type_node))
853 return unsigned_p ? sat_accum_type_node : accum_type_node;
854 if (mode == TYPE_MODE (long_accum_type_node))
855 return unsigned_p ? sat_long_accum_type_node : long_accum_type_node;
856 if (mode == TYPE_MODE (long_long_accum_type_node))
857 return unsigned_p ? sat_long_long_accum_type_node
858 : long_long_accum_type_node;
860 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
861 return unsigned_p ? sat_unsigned_short_accum_type_node
862 : unsigned_short_accum_type_node;
863 if (mode == TYPE_MODE (unsigned_accum_type_node))
864 return unsigned_p ? sat_unsigned_accum_type_node
865 : unsigned_accum_type_node;
866 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
867 return unsigned_p ? sat_unsigned_long_accum_type_node
868 : unsigned_long_accum_type_node;
869 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
870 return unsigned_p ? sat_unsigned_long_long_accum_type_node
871 : unsigned_long_long_accum_type_node;
873 if (mode == QQmode)
874 return unsigned_p ? sat_qq_type_node : qq_type_node;
875 if (mode == HQmode)
876 return unsigned_p ? sat_hq_type_node : hq_type_node;
877 if (mode == SQmode)
878 return unsigned_p ? sat_sq_type_node : sq_type_node;
879 if (mode == DQmode)
880 return unsigned_p ? sat_dq_type_node : dq_type_node;
881 if (mode == TQmode)
882 return unsigned_p ? sat_tq_type_node : tq_type_node;
884 if (mode == UQQmode)
885 return unsigned_p ? sat_uqq_type_node : uqq_type_node;
886 if (mode == UHQmode)
887 return unsigned_p ? sat_uhq_type_node : uhq_type_node;
888 if (mode == USQmode)
889 return unsigned_p ? sat_usq_type_node : usq_type_node;
890 if (mode == UDQmode)
891 return unsigned_p ? sat_udq_type_node : udq_type_node;
892 if (mode == UTQmode)
893 return unsigned_p ? sat_utq_type_node : utq_type_node;
895 if (mode == HAmode)
896 return unsigned_p ? sat_ha_type_node : ha_type_node;
897 if (mode == SAmode)
898 return unsigned_p ? sat_sa_type_node : sa_type_node;
899 if (mode == DAmode)
900 return unsigned_p ? sat_da_type_node : da_type_node;
901 if (mode == TAmode)
902 return unsigned_p ? sat_ta_type_node : ta_type_node;
904 if (mode == UHAmode)
905 return unsigned_p ? sat_uha_type_node : uha_type_node;
906 if (mode == USAmode)
907 return unsigned_p ? sat_usa_type_node : usa_type_node;
908 if (mode == UDAmode)
909 return unsigned_p ? sat_uda_type_node : uda_type_node;
910 if (mode == UTAmode)
911 return unsigned_p ? sat_uta_type_node : uta_type_node;
914 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
915 if (TYPE_MODE (TREE_VALUE (t)) == mode)
916 return TREE_VALUE (t);
918 return NULL_TREE;
921 static int
922 lto_global_bindings_p (void)
924 return cfun == NULL;
927 static void
928 lto_set_decl_assembler_name (tree decl)
930 /* This is almost the same as lhd_set_decl_assembler_name, except that
931 we need to uniquify file-scope names, even if they are not
932 TREE_PUBLIC, to avoid conflicts between individual files. */
933 tree id;
935 if (TREE_PUBLIC (decl))
936 id = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
937 else
939 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
940 char *label;
942 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
943 id = get_identifier (label);
946 SET_DECL_ASSEMBLER_NAME (decl, id);
949 static tree
950 lto_pushdecl (tree t ATTRIBUTE_UNUSED)
952 /* Do nothing, since we get all information from DWARF and LTO
953 sections. */
954 return NULL_TREE;
957 static tree
958 lto_getdecls (void)
960 return registered_builtin_fndecls;
963 static void
964 lto_write_globals (void)
966 tree *vec = VEC_address (tree, lto_global_var_decls);
967 int len = VEC_length (tree, lto_global_var_decls);
968 wrapup_global_declarations (vec, len);
969 emit_debug_global_declarations (vec, len);
970 VEC_free (tree, gc, lto_global_var_decls);
973 static tree
974 lto_builtin_function (tree decl)
976 /* Record it. */
977 TREE_CHAIN (decl) = registered_builtin_fndecls;
978 registered_builtin_fndecls = decl;
980 return decl;
983 static void
984 lto_register_builtin_type (tree type, const char *name)
986 tree decl;
988 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier (name), type);
989 DECL_ARTIFICIAL (decl) = 1;
990 if (!TYPE_NAME (type))
991 TYPE_NAME (type) = decl;
993 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
996 /* Build nodes that would have be created by the C front-end; necessary
997 for including builtin-types.def and ultimately builtins.def. */
999 static void
1000 lto_build_c_type_nodes (void)
1002 gcc_assert (void_type_node);
1004 void_list_node = build_tree_list (NULL_TREE, void_type_node);
1005 string_type_node = build_pointer_type (char_type_node);
1006 const_string_type_node
1007 = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
1009 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
1011 intmax_type_node = integer_type_node;
1012 uintmax_type_node = unsigned_type_node;
1013 signed_size_type_node = integer_type_node;
1015 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
1017 intmax_type_node = long_integer_type_node;
1018 uintmax_type_node = long_unsigned_type_node;
1019 signed_size_type_node = long_integer_type_node;
1021 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
1023 intmax_type_node = long_long_integer_type_node;
1024 uintmax_type_node = long_long_unsigned_type_node;
1025 signed_size_type_node = long_long_integer_type_node;
1027 else
1028 gcc_unreachable ();
1030 wint_type_node = unsigned_type_node;
1031 pid_type_node = integer_type_node;
1035 /* Perform LTO-specific initialization. */
1037 static bool
1038 lto_init (void)
1040 /* We need to generate LTO if running in WPA mode. */
1041 flag_generate_lto = flag_wpa;
1043 /* Initialize libcpp line maps for gcc_assert to work. */
1044 linemap_add (line_table, LC_RENAME, 0, NULL, 0);
1045 linemap_add (line_table, LC_RENAME, 0, NULL, 0);
1047 /* Create the basic integer types. */
1048 build_common_tree_nodes (flag_signed_char);
1050 /* Share char_type_node with whatever would be the default for the target.
1051 char_type_node will be used for internal types such as
1052 va_list_type_node but will not be present in the lto stream. */
1053 /* ??? This breaks the more common case of consistent but non-standard
1054 setting of flag_signed_char, so share according to flag_signed_char.
1055 See PR42528. */
1056 char_type_node
1057 = flag_signed_char ? signed_char_type_node : unsigned_char_type_node;
1059 /* Tell the middle end what type to use for the size of objects. */
1060 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
1062 set_sizetype (unsigned_type_node);
1063 size_type_node = unsigned_type_node;
1065 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
1067 set_sizetype (long_unsigned_type_node);
1068 size_type_node = long_unsigned_type_node;
1070 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
1072 set_sizetype (long_long_unsigned_type_node);
1073 size_type_node = long_long_unsigned_type_node;
1075 else
1076 gcc_unreachable ();
1078 /* The global tree for the main identifier is filled in by
1079 language-specific front-end initialization that is not run in the
1080 LTO back-end. It appears that all languages that perform such
1081 initialization currently do so in the same way, so we do it here. */
1082 if (main_identifier_node == NULL_TREE)
1083 main_identifier_node = get_identifier ("main");
1085 /* In the C++ front-end, fileptr_type_node is defined as a variant
1086 copy of of ptr_type_node, rather than ptr_node itself. The
1087 distinction should only be relevant to the front-end, so we
1088 always use the C definition here in lto1. */
1089 gcc_assert (fileptr_type_node == ptr_type_node);
1091 ptrdiff_type_node = integer_type_node;
1093 /* Create other basic types. */
1094 build_common_tree_nodes_2 (/*short_double=*/false);
1095 lto_build_c_type_nodes ();
1096 gcc_assert (va_list_type_node);
1098 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
1100 tree x = build_pointer_type (TREE_TYPE (va_list_type_node));
1101 lto_define_builtins (x, x);
1103 else
1105 lto_define_builtins (va_list_type_node,
1106 build_reference_type (va_list_type_node));
1109 targetm.init_builtins ();
1110 build_common_builtin_nodes ();
1112 /* Initialize LTO-specific data structures. */
1113 lto_global_var_decls = VEC_alloc (tree, gc, 256);
1114 in_lto_p = true;
1116 return true;
1119 /* Initialize tree structures required by the LTO front end. */
1121 static void lto_init_ts (void)
1123 tree_contains_struct[NAMESPACE_DECL][TS_DECL_MINIMAL] = 1;
1126 #undef LANG_HOOKS_NAME
1127 #define LANG_HOOKS_NAME "GNU GIMPLE"
1128 #undef LANG_HOOKS_INIT_OPTIONS
1129 #define LANG_HOOKS_INIT_OPTIONS lto_init_options
1130 #undef LANG_HOOKS_HANDLE_OPTION
1131 #define LANG_HOOKS_HANDLE_OPTION lto_handle_option
1132 #undef LANG_HOOKS_POST_OPTIONS
1133 #define LANG_HOOKS_POST_OPTIONS lto_post_options
1134 #undef LANG_HOOKS_GET_ALIAS_SET
1135 #define LANG_HOOKS_GET_ALIAS_SET gimple_get_alias_set
1136 #undef LANG_HOOKS_TYPE_FOR_MODE
1137 #define LANG_HOOKS_TYPE_FOR_MODE lto_type_for_mode
1138 #undef LANG_HOOKS_TYPE_FOR_SIZE
1139 #define LANG_HOOKS_TYPE_FOR_SIZE lto_type_for_size
1140 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
1141 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME lto_set_decl_assembler_name
1142 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
1143 #define LANG_HOOKS_GLOBAL_BINDINGS_P lto_global_bindings_p
1144 #undef LANG_HOOKS_PUSHDECL
1145 #define LANG_HOOKS_PUSHDECL lto_pushdecl
1146 #undef LANG_HOOKS_GETDECLS
1147 #define LANG_HOOKS_GETDECLS lto_getdecls
1148 #undef LANG_HOOKS_WRITE_GLOBALS
1149 #define LANG_HOOKS_WRITE_GLOBALS lto_write_globals
1150 #undef LANG_HOOKS_REGISTER_BUILTIN_TYPE
1151 #define LANG_HOOKS_REGISTER_BUILTIN_TYPE lto_register_builtin_type
1152 #undef LANG_HOOKS_BUILTIN_FUNCTION
1153 #define LANG_HOOKS_BUILTIN_FUNCTION lto_builtin_function
1154 #undef LANG_HOOKS_INIT
1155 #define LANG_HOOKS_INIT lto_init
1156 #undef LANG_HOOKS_PARSE_FILE
1157 #define LANG_HOOKS_PARSE_FILE lto_main
1158 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
1159 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION tree_rest_of_compilation
1160 #undef LANG_HOOKS_REDUCE_BIT_FIELD_OPERATIONS
1161 #define LANG_HOOKS_REDUCE_BIT_FIELD_OPERATIONS true
1162 #undef LANG_HOOKS_TYPES_COMPATIBLE_P
1163 #define LANG_HOOKS_TYPES_COMPATIBLE_P NULL
1164 #undef LANG_HOOKS_EH_PERSONALITY
1165 #define LANG_HOOKS_EH_PERSONALITY lto_eh_personality
1167 /* Attribute hooks. */
1168 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
1169 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE lto_attribute_table
1170 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
1171 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE lto_format_attribute_table
1173 #undef LANG_HOOKS_BEGIN_SECTION
1174 #define LANG_HOOKS_BEGIN_SECTION lto_obj_begin_section
1175 #undef LANG_HOOKS_APPEND_DATA
1176 #define LANG_HOOKS_APPEND_DATA lto_obj_append_data
1177 #undef LANG_HOOKS_END_SECTION
1178 #define LANG_HOOKS_END_SECTION lto_obj_end_section
1180 #undef LANG_HOOKS_INIT_TS
1181 #define LANG_HOOKS_INIT_TS lto_init_ts
1183 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
1185 /* Language hooks that are not part of lang_hooks. */
1187 tree
1188 convert (tree type ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
1190 gcc_unreachable ();
1193 /* Tree walking support. */
1195 static enum lto_tree_node_structure_enum
1196 lto_tree_node_structure (union lang_tree_node *t ATTRIBUTE_UNUSED)
1198 return TS_LTO_GENERIC;
1201 #include "ggc.h"
1202 #include "gtype-lto.h"
1203 #include "gt-lto-lto-lang.h"