re PR c++/80038 (Random segfault using local vectors in Cilk function)
[platform/upstream/gcc.git] / gcc / lto / lto-lang.c
1 /* Language-dependent hooks for LTO.
2    Copyright (C) 2009-2017 Free Software Foundation, Inc.
3    Contributed by CodeSourcery, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "stringpool.h"
30 #include "diagnostic-core.h"
31 #include "stor-layout.h"
32 #include "langhooks.h"
33 #include "langhooks-def.h"
34 #include "debug.h"
35 #include "lto-tree.h"
36 #include "lto.h"
37 #include "cilk.h"
38
39 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
40 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
41 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
42 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
43 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
44 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
45 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
46 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
51 static tree ignore_attribute (tree *, tree, tree, int, bool *);
52
53 static tree handle_format_attribute (tree *, tree, tree, int, bool *);
54 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
55 static tree handle_format_arg_attribute (tree *, tree, tree, int, bool *);
56
57 /* Table of machine-independent attributes supported in GIMPLE.  */
58 const struct attribute_spec lto_attribute_table[] =
59 {
60   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
61        do_diagnostic } */
62   { "noreturn",               0, 0, true,  false, false,
63                               handle_noreturn_attribute, false },
64   { "leaf",                   0, 0, true,  false, false,
65                               handle_leaf_attribute, false },
66   /* The same comments as for noreturn attributes apply to const ones.  */
67   { "const",                  0, 0, true,  false, false,
68                               handle_const_attribute, false },
69   { "malloc",                 0, 0, true,  false, false,
70                               handle_malloc_attribute, false },
71   { "pure",                   0, 0, true,  false, false,
72                               handle_pure_attribute, false },
73   { "no vops",                0, 0, true,  false, false,
74                               handle_novops_attribute, false },
75   { "nonnull",                0, -1, false, true, true,
76                               handle_nonnull_attribute, false },
77   { "nothrow",                0, 0, true,  false, false,
78                               handle_nothrow_attribute, false },
79   { "returns_twice",          0, 0, true,  false, false,
80                               handle_returns_twice_attribute, false },
81   { "sentinel",               0, 1, false, true, true,
82                               handle_sentinel_attribute, false },
83   { "type generic",           0, 0, false, true, true,
84                               handle_type_generic_attribute, false },
85   { "fn spec",                1, 1, false, true, true,
86                               handle_fnspec_attribute, false },
87   { "transaction_pure",       0, 0, false, true, true,
88                               handle_transaction_pure_attribute, false },
89   /* For internal use only.  The leading '*' both prevents its usage in
90      source code and signals that it may be overridden by machine tables.  */
91   { "*tm regparm",            0, 0, false, true, true,
92                               ignore_attribute, false },
93   { NULL,                     0, 0, false, false, false, NULL, false }
94 };
95
96 /* Give the specifications for the format attributes, used by C and all
97    descendants.  */
98
99 const struct attribute_spec lto_format_attribute_table[] =
100 {
101   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
102        affects_type_identity } */
103   { "format",                 3, 3, false, true,  true,
104                               handle_format_attribute, false },
105   { "format_arg",             1, 1, false, true,  true,
106                               handle_format_arg_attribute, false },
107   { NULL,                     0, 0, false, false, false, NULL, false }
108 };
109
110 enum built_in_attribute
111 {
112 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
113 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
114 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
115 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
116 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
117 #include "builtin-attrs.def"
118 #undef DEF_ATTR_NULL_TREE
119 #undef DEF_ATTR_INT
120 #undef DEF_ATTR_STRING
121 #undef DEF_ATTR_IDENT
122 #undef DEF_ATTR_TREE_LIST
123   ATTR_LAST
124 };
125
126 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
127
128 /* Builtin types.  */
129
130 enum lto_builtin_type
131 {
132 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
133 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
134 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
135 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
136 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
137 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
138 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
139 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
140                             ARG6) NAME,
141 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
142                             ARG6, ARG7) NAME,
143 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
144                             ARG6, ARG7, ARG8) NAME,
145 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
146                             ARG6, ARG7, ARG8, ARG9) NAME,
147 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
148                              ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
149 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
150                              ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
151 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
152 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
153 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
154 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
155 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
156 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
157                                 NAME,
158 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
159                                  ARG6) NAME,
160 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
161                                 ARG6, ARG7) NAME,
162 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
163 #include "builtin-types.def"
164 #undef DEF_PRIMITIVE_TYPE
165 #undef DEF_FUNCTION_TYPE_0
166 #undef DEF_FUNCTION_TYPE_1
167 #undef DEF_FUNCTION_TYPE_2
168 #undef DEF_FUNCTION_TYPE_3
169 #undef DEF_FUNCTION_TYPE_4
170 #undef DEF_FUNCTION_TYPE_5
171 #undef DEF_FUNCTION_TYPE_6
172 #undef DEF_FUNCTION_TYPE_7
173 #undef DEF_FUNCTION_TYPE_8
174 #undef DEF_FUNCTION_TYPE_9
175 #undef DEF_FUNCTION_TYPE_10
176 #undef DEF_FUNCTION_TYPE_11
177 #undef DEF_FUNCTION_TYPE_VAR_0
178 #undef DEF_FUNCTION_TYPE_VAR_1
179 #undef DEF_FUNCTION_TYPE_VAR_2
180 #undef DEF_FUNCTION_TYPE_VAR_3
181 #undef DEF_FUNCTION_TYPE_VAR_4
182 #undef DEF_FUNCTION_TYPE_VAR_5
183 #undef DEF_FUNCTION_TYPE_VAR_6
184 #undef DEF_FUNCTION_TYPE_VAR_7
185 #undef DEF_POINTER_TYPE
186   BT_LAST
187 };
188
189 typedef enum lto_builtin_type builtin_type;
190
191 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
192
193 static GTY(()) tree string_type_node;
194 static GTY(()) tree const_string_type_node;
195 static GTY(()) tree wint_type_node;
196 static GTY(()) tree intmax_type_node;
197 static GTY(()) tree uintmax_type_node;
198 static GTY(()) tree signed_size_type_node;
199
200 /* Flags needed to process builtins.def.  */
201 int flag_isoc94;
202 int flag_isoc99;
203 int flag_isoc11;
204
205 /* Attribute handlers.  */
206
207 /* Handle a "noreturn" attribute; arguments as in
208    struct attribute_spec.handler.  */
209
210 static tree
211 handle_noreturn_attribute (tree *node, tree ARG_UNUSED (name),
212                            tree ARG_UNUSED (args), int ARG_UNUSED (flags),
213                            bool * ARG_UNUSED (no_add_attrs))
214 {
215   tree type = TREE_TYPE (*node);
216
217   if (TREE_CODE (*node) == FUNCTION_DECL)
218     TREE_THIS_VOLATILE (*node) = 1;
219   else if (TREE_CODE (type) == POINTER_TYPE
220            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
221     TREE_TYPE (*node)
222       = build_pointer_type
223         (build_type_variant (TREE_TYPE (type),
224                              TYPE_READONLY (TREE_TYPE (type)), 1));
225   else
226     gcc_unreachable ();
227
228   return NULL_TREE;
229 }
230
231 /* Handle a "leaf" attribute; arguments as in
232    struct attribute_spec.handler.  */
233
234 static tree
235 handle_leaf_attribute (tree *node, tree name,
236                        tree ARG_UNUSED (args),
237                        int ARG_UNUSED (flags), bool *no_add_attrs)
238 {
239   if (TREE_CODE (*node) != FUNCTION_DECL)
240     {
241       warning (OPT_Wattributes, "%qE attribute ignored", name);
242       *no_add_attrs = true;
243     }
244   if (!TREE_PUBLIC (*node))
245     {
246       warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
247       *no_add_attrs = true;
248     }
249
250   return NULL_TREE;
251 }
252
253 /* Handle a "const" attribute; arguments as in
254    struct attribute_spec.handler.  */
255
256 static tree
257 handle_const_attribute (tree *node, tree ARG_UNUSED (name),
258                         tree ARG_UNUSED (args), int ARG_UNUSED (flags),
259                         bool * ARG_UNUSED (no_add_attrs))
260 {
261   tree type = TREE_TYPE (*node);
262
263   /* See FIXME comment on noreturn in c_common_attribute_table.  */
264   if (TREE_CODE (*node) == FUNCTION_DECL)
265     TREE_READONLY (*node) = 1;
266   else if (TREE_CODE (type) == POINTER_TYPE
267            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
268     TREE_TYPE (*node)
269       = build_pointer_type
270         (build_type_variant (TREE_TYPE (type), 1,
271                              TREE_THIS_VOLATILE (TREE_TYPE (type))));
272   else
273     gcc_unreachable ();
274
275   return NULL_TREE;
276 }
277
278
279 /* Handle a "malloc" attribute; arguments as in
280    struct attribute_spec.handler.  */
281
282 static tree
283 handle_malloc_attribute (tree *node, tree ARG_UNUSED (name),
284                          tree ARG_UNUSED (args), int ARG_UNUSED (flags),
285                          bool * ARG_UNUSED (no_add_attrs))
286 {
287   if (TREE_CODE (*node) == FUNCTION_DECL
288       && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
289     DECL_IS_MALLOC (*node) = 1;
290   else
291     gcc_unreachable ();
292
293   return NULL_TREE;
294 }
295
296
297 /* Handle a "pure" attribute; arguments as in
298    struct attribute_spec.handler.  */
299
300 static tree
301 handle_pure_attribute (tree *node, tree ARG_UNUSED (name),
302                        tree ARG_UNUSED (args), int ARG_UNUSED (flags),
303                        bool * ARG_UNUSED (no_add_attrs))
304 {
305   if (TREE_CODE (*node) == FUNCTION_DECL)
306     DECL_PURE_P (*node) = 1;
307   else
308     gcc_unreachable ();
309
310   return NULL_TREE;
311 }
312
313
314 /* Handle a "no vops" attribute; arguments as in
315    struct attribute_spec.handler.  */
316
317 static tree
318 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
319                          tree ARG_UNUSED (args), int ARG_UNUSED (flags),
320                          bool *ARG_UNUSED (no_add_attrs))
321 {
322   gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
323   DECL_IS_NOVOPS (*node) = 1;
324   return NULL_TREE;
325 }
326
327
328 /* Helper for nonnull attribute handling; fetch the operand number
329    from the attribute argument list.  */
330
331 static bool
332 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
333 {
334   /* Verify the arg number is a constant.  */
335   if (!tree_fits_uhwi_p (arg_num_expr))
336     return false;
337
338   *valp = TREE_INT_CST_LOW (arg_num_expr);
339   return true;
340 }
341
342 /* Handle the "nonnull" attribute.  */
343
344 static tree
345 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
346                           tree args, int ARG_UNUSED (flags),
347                           bool * ARG_UNUSED (no_add_attrs))
348 {
349   tree type = *node;
350
351   /* If no arguments are specified, all pointer arguments should be
352      non-null.  Verify a full prototype is given so that the arguments
353      will have the correct types when we actually check them later.
354      Avoid diagnosing type-generic built-ins since those have no
355      prototype.  */
356   if (!args)
357     {
358       gcc_assert (prototype_p (type)
359                   || !TYPE_ATTRIBUTES (type)
360                   || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
361
362       return NULL_TREE;
363     }
364
365   /* Argument list specified.  Verify that each argument number references
366      a pointer argument.  */
367   for (; args; args = TREE_CHAIN (args))
368     {
369       tree argument;
370       unsigned HOST_WIDE_INT arg_num = 0, ck_num;
371
372       if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
373         gcc_unreachable ();
374
375       argument = TYPE_ARG_TYPES (type);
376       if (argument)
377         {
378           for (ck_num = 1; ; ck_num++)
379             {
380               if (!argument || ck_num == arg_num)
381                 break;
382               argument = TREE_CHAIN (argument);
383             }
384
385           gcc_assert (argument
386                       && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
387         }
388     }
389
390   return NULL_TREE;
391 }
392
393
394 /* Handle a "nothrow" attribute; arguments as in
395    struct attribute_spec.handler.  */
396
397 static tree
398 handle_nothrow_attribute (tree *node, tree ARG_UNUSED (name),
399                           tree ARG_UNUSED (args), int ARG_UNUSED (flags),
400                           bool * ARG_UNUSED (no_add_attrs))
401 {
402   if (TREE_CODE (*node) == FUNCTION_DECL)
403     TREE_NOTHROW (*node) = 1;
404   else
405     gcc_unreachable ();
406
407   return NULL_TREE;
408 }
409
410
411 /* Handle a "sentinel" attribute.  */
412
413 static tree
414 handle_sentinel_attribute (tree *node, tree ARG_UNUSED (name), tree args,
415                            int ARG_UNUSED (flags),
416                            bool * ARG_UNUSED (no_add_attrs))
417 {
418   gcc_assert (stdarg_p (*node));
419
420   if (args)
421     {
422       tree position = TREE_VALUE (args);
423       gcc_assert (TREE_CODE (position) == INTEGER_CST);
424       if (tree_int_cst_lt (position, integer_zero_node))
425         gcc_unreachable ();
426     }
427
428   return NULL_TREE;
429 }
430
431 /* Handle a "type_generic" attribute.  */
432
433 static tree
434 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
435                                tree ARG_UNUSED (args), int ARG_UNUSED (flags),
436                                bool * ARG_UNUSED (no_add_attrs))
437 {
438   /* Ensure we have a function type.  */
439   gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
440   
441   /* Ensure we have a variadic function.  */
442   gcc_assert (!prototype_p (*node) || stdarg_p (*node));
443
444   return NULL_TREE;
445 }
446
447 /* Handle a "transaction_pure" attribute.  */
448
449 static tree
450 handle_transaction_pure_attribute (tree *node, tree ARG_UNUSED (name),
451                                    tree ARG_UNUSED (args),
452                                    int ARG_UNUSED (flags),
453                                    bool * ARG_UNUSED (no_add_attrs))
454 {
455   /* Ensure we have a function type.  */
456   gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
457
458   return NULL_TREE;
459 }
460
461 /* Handle a "returns_twice" attribute.  */
462
463 static tree
464 handle_returns_twice_attribute (tree *node, tree ARG_UNUSED (name),
465                                 tree ARG_UNUSED (args),
466                                 int ARG_UNUSED (flags),
467                                 bool * ARG_UNUSED (no_add_attrs))
468 {
469   gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
470
471   DECL_IS_RETURNS_TWICE (*node) = 1;
472
473   return NULL_TREE;
474 }
475
476 /* Ignore the given attribute.  Used when this attribute may be usefully
477    overridden by the target, but is not used generically.  */
478
479 static tree
480 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
481                   tree ARG_UNUSED (args), int ARG_UNUSED (flags),
482                   bool *no_add_attrs)
483 {
484   *no_add_attrs = true;
485   return NULL_TREE;
486 }
487
488 /* Handle a "format" attribute; arguments as in
489    struct attribute_spec.handler.  */
490
491 static tree
492 handle_format_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
493                          tree ARG_UNUSED (args), int ARG_UNUSED (flags),
494                          bool *no_add_attrs)
495 {
496   *no_add_attrs = true;
497   return NULL_TREE;
498 }
499
500
501 /* Handle a "format_arg" attribute; arguments as in
502    struct attribute_spec.handler.  */
503
504 tree
505 handle_format_arg_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
506                              tree ARG_UNUSED (args), int ARG_UNUSED (flags),
507                              bool *no_add_attrs)
508 {
509   *no_add_attrs = true;
510   return NULL_TREE;
511 }
512
513
514 /* Handle a "fn spec" attribute; arguments as in
515    struct attribute_spec.handler.  */
516
517 static tree
518 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
519                          tree args, int ARG_UNUSED (flags),
520                          bool *no_add_attrs ATTRIBUTE_UNUSED)
521 {
522   gcc_assert (args
523               && TREE_CODE (TREE_VALUE (args)) == STRING_CST
524               && !TREE_CHAIN (args));
525   return NULL_TREE;
526 }
527
528 /* Cribbed from c-common.c.  */
529
530 static void
531 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
532 {
533   tree t;
534   tree *args = XALLOCAVEC (tree, n);
535   va_list list;
536   int i;
537   bool err = false;
538
539   va_start (list, n);
540   for (i = 0; i < n; ++i)
541     {
542       builtin_type a = (builtin_type) va_arg (list, int);
543       t = builtin_types[a];
544       if (t == error_mark_node)
545         err = true;
546       args[i] = t;
547     }
548   va_end (list);
549
550   t = builtin_types[ret];
551   if (err)
552     t = error_mark_node;
553   if (t == error_mark_node)
554     ;
555   else if (var)
556     t = build_varargs_function_type_array (t, n, args);
557   else
558     t = build_function_type_array (t, n, args);
559
560   builtin_types[def] = t;
561 }
562
563 /* Used to help initialize the builtin-types.def table.  When a type of
564    the correct size doesn't exist, use error_mark_node instead of NULL.
565    The later results in segfaults even when a decl using the type doesn't
566    get invoked.  */
567
568 static tree
569 builtin_type_for_size (int size, bool unsignedp)
570 {
571   tree type = lang_hooks.types.type_for_size (size, unsignedp);
572   return type ? type : error_mark_node;
573 }
574
575 /* Support for DEF_BUILTIN.  */
576
577 static void
578 def_builtin_1 (enum built_in_function fncode, const char *name,
579                enum built_in_class fnclass, tree fntype, tree libtype,
580                bool both_p, bool fallback_p, bool nonansi_p,
581                tree fnattrs, bool implicit_p)
582 {
583   tree decl;
584   const char *libname;
585
586   if (fntype == error_mark_node)
587     return;
588
589   libname = name + strlen ("__builtin_");
590   decl = add_builtin_function (name, fntype, fncode, fnclass,
591                                (fallback_p ? libname : NULL),
592                                fnattrs);
593
594   if (both_p
595       && !flag_no_builtin
596       && !(nonansi_p && flag_no_nonansi_builtin))
597     add_builtin_function (libname, libtype, fncode, fnclass,
598                           NULL, fnattrs);
599
600   set_builtin_decl (fncode, decl, implicit_p);
601 }
602
603
604 /* Initialize the attribute table for all the supported builtins.  */
605
606 static void
607 lto_init_attributes (void)
608 {
609   /* Fill in the built_in_attributes array.  */
610 #define DEF_ATTR_NULL_TREE(ENUM)                                \
611   built_in_attributes[(int) ENUM] = NULL_TREE;
612 #define DEF_ATTR_INT(ENUM, VALUE)                               \
613   built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
614 #define DEF_ATTR_STRING(ENUM, VALUE)                            \
615   built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
616 #define DEF_ATTR_IDENT(ENUM, STRING)                            \
617   built_in_attributes[(int) ENUM] = get_identifier (STRING);
618 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
619   built_in_attributes[(int) ENUM]                       \
620     = tree_cons (built_in_attributes[(int) PURPOSE],    \
621                  built_in_attributes[(int) VALUE],      \
622                  built_in_attributes[(int) CHAIN]);
623 #include "builtin-attrs.def"
624 #undef DEF_ATTR_NULL_TREE
625 #undef DEF_ATTR_INT
626 #undef DEF_ATTR_STRING
627 #undef DEF_ATTR_IDENT
628 #undef DEF_ATTR_TREE_LIST
629 }
630
631 /* Create builtin types and functions.  VA_LIST_REF_TYPE_NODE and
632    VA_LIST_ARG_TYPE_NODE are used in builtin-types.def.  */
633
634 static void
635 lto_define_builtins (tree va_list_ref_type_node ATTRIBUTE_UNUSED,
636                      tree va_list_arg_type_node ATTRIBUTE_UNUSED)
637 {
638 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
639   builtin_types[ENUM] = VALUE;
640 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
641   def_fn_type (ENUM, RETURN, 0, 0);
642 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
643   def_fn_type (ENUM, RETURN, 0, 1, ARG1);
644 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
645   def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
646 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
647   def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
648 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
649   def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
650 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
651   def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
652 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
653                             ARG6)                                       \
654   def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
655 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
656                             ARG6, ARG7)                                 \
657   def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
658 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
659                             ARG6, ARG7, ARG8)                           \
660   def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,  \
661                ARG7, ARG8);
662 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
663                             ARG6, ARG7, ARG8, ARG9)                     \
664   def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,  \
665                ARG7, ARG8, ARG9);
666 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
667                              ARG6, ARG7, ARG8, ARG9, ARG10)              \
668   def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,  \
669                ARG7, ARG8, ARG9, ARG10);
670 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
671                              ARG6, ARG7, ARG8, ARG9, ARG10, ARG11)       \
672   def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6,  \
673                ARG7, ARG8, ARG9, ARG10, ARG11);
674 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
675   def_fn_type (ENUM, RETURN, 1, 0);
676 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
677   def_fn_type (ENUM, RETURN, 1, 1, ARG1);
678 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
679   def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
680 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
681   def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
682 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
683   def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
684 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
685   def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
686 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
687                                  ARG6)  \
688   def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
689 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
690                                 ARG6, ARG7)                             \
691   def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
692 #define DEF_POINTER_TYPE(ENUM, TYPE) \
693   builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
694
695 #include "builtin-types.def"
696
697 #undef DEF_PRIMITIVE_TYPE
698 #undef DEF_FUNCTION_TYPE_0
699 #undef DEF_FUNCTION_TYPE_1
700 #undef DEF_FUNCTION_TYPE_2
701 #undef DEF_FUNCTION_TYPE_3
702 #undef DEF_FUNCTION_TYPE_4
703 #undef DEF_FUNCTION_TYPE_5
704 #undef DEF_FUNCTION_TYPE_6
705 #undef DEF_FUNCTION_TYPE_7
706 #undef DEF_FUNCTION_TYPE_8
707 #undef DEF_FUNCTION_TYPE_9
708 #undef DEF_FUNCTION_TYPE_10
709 #undef DEF_FUNCTION_TYPE_11
710 #undef DEF_FUNCTION_TYPE_VAR_0
711 #undef DEF_FUNCTION_TYPE_VAR_1
712 #undef DEF_FUNCTION_TYPE_VAR_2
713 #undef DEF_FUNCTION_TYPE_VAR_3
714 #undef DEF_FUNCTION_TYPE_VAR_4
715 #undef DEF_FUNCTION_TYPE_VAR_5
716 #undef DEF_FUNCTION_TYPE_VAR_6
717 #undef DEF_FUNCTION_TYPE_VAR_7
718 #undef DEF_POINTER_TYPE
719   builtin_types[(int) BT_LAST] = NULL_TREE;
720
721   lto_init_attributes ();
722
723 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P,\
724                     NONANSI_P, ATTRS, IMPLICIT, COND)                   \
725     if (NAME && COND)                                                   \
726       def_builtin_1 (ENUM, NAME, CLASS, builtin_types[(int) TYPE],      \
727                      builtin_types[(int) LIBTYPE], BOTH_P, FALLBACK_P,  \
728                      NONANSI_P, built_in_attributes[(int) ATTRS], IMPLICIT);
729 #include "builtins.def"
730 }
731
732 static GTY(()) tree registered_builtin_types;
733
734 /* Language hooks.  */
735
736 static unsigned int
737 lto_option_lang_mask (void)
738 {
739   return CL_LTO;
740 }
741
742 static bool
743 lto_complain_wrong_lang_p (const struct cl_option *option ATTRIBUTE_UNUSED)
744 {
745   /* The LTO front end inherits all the options from the first front
746      end that was used.  However, not all the original front end
747      options make sense in LTO.
748
749      A real solution would be to filter this in collect2, but collect2
750      does not have access to all the option attributes to know what to
751      filter.  So, in lto1 we silently accept inherited flags and do
752      nothing about it.  */
753   return false;
754 }
755
756 static void
757 lto_init_options_struct (struct gcc_options *opts)
758 {
759   /* By default, C99-like requirements for complex multiply and divide.
760      ???  Until the complex method is encoded in the IL this is the only
761      safe choice.  This will pessimize Fortran code with LTO unless
762      people specify a complex method manually or use -ffast-math.  */
763   opts->x_flag_complex_method = 2;
764 }
765
766 /* Handle command-line option SCODE.  If the option takes an argument, it is
767    stored in ARG, which is otherwise NULL.  VALUE holds either a numerical
768    argument or a binary value indicating whether the positive or negative form
769    of the option was supplied.  */
770
771 const char *resolution_file_name;
772 static bool
773 lto_handle_option (size_t scode, const char *arg,
774                    int value ATTRIBUTE_UNUSED, int kind ATTRIBUTE_UNUSED,
775                    location_t loc ATTRIBUTE_UNUSED,
776                    const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
777 {
778   enum opt_code code = (enum opt_code) scode;
779   bool result = true;
780
781   switch (code)
782     {
783     case OPT_fresolution_:
784       resolution_file_name = arg;
785       break;
786
787     case OPT_Wabi:
788       warn_psabi = value;
789       break;
790
791     case OPT_fwpa:
792       flag_wpa = value ? "" : NULL;
793       break;
794
795     default:
796       break;
797     }
798
799   return result;
800 }
801
802 /* Perform post-option processing.  Does additional initialization based on
803    command-line options.  PFILENAME is the main input filename.  Returns false
804    to enable subsequent back-end initialization.  */
805
806 static bool
807 lto_post_options (const char **pfilename ATTRIBUTE_UNUSED)
808 {
809   /* -fltrans and -fwpa are mutually exclusive.  Check for that here.  */
810   if (flag_wpa && flag_ltrans)
811     error ("-fwpa and -fltrans are mutually exclusive");
812
813   if (flag_ltrans)
814     {
815       flag_generate_lto = 0;
816
817       /* During LTRANS, we are not looking at the whole program, only
818          a subset of the whole callgraph.  */
819       flag_whole_program = 0;
820     }
821
822   if (flag_wpa)
823     flag_generate_lto = 1;
824
825   /* Initialize the codegen flags according to the output type.  */
826   switch (flag_lto_linker_output)
827     {
828     case LTO_LINKER_OUTPUT_REL: /* .o: incremental link producing LTO IL  */
829       flag_whole_program = 0;
830       flag_incremental_link = 1;
831       break;
832
833     case LTO_LINKER_OUTPUT_DYN: /* .so: PID library */
834       /* On some targets, like i386 it makes sense to build PIC library wihout
835          -fpic for performance reasons.  So no need to adjust flags.  */
836       break;
837
838     case LTO_LINKER_OUTPUT_PIE: /* PIE binary */
839       /* If -fPIC or -fPIE was used at compile time, be sure that
840          flag_pie is 2.  */
841       flag_pie = MAX (flag_pie, flag_pic);
842       flag_pic = flag_pie;
843       break;
844
845     case LTO_LINKER_OUTPUT_EXEC: /* Normal executable */
846       flag_pic = 0;
847       flag_pie = 0;
848       break;
849
850     case LTO_LINKER_OUTPUT_UNKNOWN:
851       break;
852     }
853
854   /* Excess precision other than "fast" requires front-end
855      support.  */
856   flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
857
858   /* When partitioning, we can tear appart STRING_CSTs uses from the same
859      TU into multiple partitions.  Without constant merging the constants
860      might not be equal at runtime.  See PR50199.  */
861   if (!flag_merge_constants)
862     flag_merge_constants = 1;
863
864   /* Initialize the compiler back end.  */
865   return false;
866 }
867
868 /* Return a data type that has machine mode MODE.
869    If the mode is an integer,
870    then UNSIGNEDP selects between signed and unsigned types.
871    If the mode is a fixed-point mode,
872    then UNSIGNEDP selects between saturating and nonsaturating types.  */
873
874 static tree
875 lto_type_for_mode (machine_mode mode, int unsigned_p)
876 {
877   tree t;
878   int i;
879
880   if (mode == TYPE_MODE (integer_type_node))
881     return unsigned_p ? unsigned_type_node : integer_type_node;
882
883   if (mode == TYPE_MODE (signed_char_type_node))
884     return unsigned_p ? unsigned_char_type_node : signed_char_type_node;
885
886   if (mode == TYPE_MODE (short_integer_type_node))
887     return unsigned_p ? short_unsigned_type_node : short_integer_type_node;
888
889   if (mode == TYPE_MODE (long_integer_type_node))
890     return unsigned_p ? long_unsigned_type_node : long_integer_type_node;
891
892   if (mode == TYPE_MODE (long_long_integer_type_node))
893     return unsigned_p ? long_long_unsigned_type_node : long_long_integer_type_node;
894
895   for (i = 0; i < NUM_INT_N_ENTS; i ++)
896     if (int_n_enabled_p[i]
897         && mode == int_n_data[i].m)
898       return (unsigned_p ? int_n_trees[i].unsigned_type
899               : int_n_trees[i].signed_type);
900
901   if (mode == QImode)
902     return unsigned_p ? unsigned_intQI_type_node : intQI_type_node;
903
904   if (mode == HImode)
905     return unsigned_p ? unsigned_intHI_type_node : intHI_type_node;
906
907   if (mode == SImode)
908     return unsigned_p ? unsigned_intSI_type_node : intSI_type_node;
909
910   if (mode == DImode)
911     return unsigned_p ? unsigned_intDI_type_node : intDI_type_node;
912
913 #if HOST_BITS_PER_WIDE_INT >= 64
914   if (mode == TYPE_MODE (intTI_type_node))
915     return unsigned_p ? unsigned_intTI_type_node : intTI_type_node;
916 #endif
917
918   if (mode == TYPE_MODE (float_type_node))
919     return float_type_node;
920
921   if (mode == TYPE_MODE (double_type_node))
922     return double_type_node;
923
924   if (mode == TYPE_MODE (long_double_type_node))
925     return long_double_type_node;
926
927   if (mode == TYPE_MODE (void_type_node))
928     return void_type_node;
929
930   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
931     return (unsigned_p
932             ? make_unsigned_type (GET_MODE_PRECISION (mode))
933             : make_signed_type (GET_MODE_PRECISION (mode)));
934
935   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
936     return (unsigned_p
937             ? make_unsigned_type (GET_MODE_PRECISION (mode))
938             : make_signed_type (GET_MODE_PRECISION (mode)));
939
940   if (COMPLEX_MODE_P (mode))
941     {
942       machine_mode inner_mode;
943       tree inner_type;
944
945       if (mode == TYPE_MODE (complex_float_type_node))
946         return complex_float_type_node;
947       if (mode == TYPE_MODE (complex_double_type_node))
948         return complex_double_type_node;
949       if (mode == TYPE_MODE (complex_long_double_type_node))
950         return complex_long_double_type_node;
951
952       if (mode == TYPE_MODE (complex_integer_type_node) && !unsigned_p)
953         return complex_integer_type_node;
954
955       inner_mode = GET_MODE_INNER (mode);
956       inner_type = lto_type_for_mode (inner_mode, unsigned_p);
957       if (inner_type != NULL_TREE)
958         return build_complex_type (inner_type);
959     }
960   else if (VECTOR_MODE_P (mode))
961     {
962       machine_mode inner_mode = GET_MODE_INNER (mode);
963       tree inner_type = lto_type_for_mode (inner_mode, unsigned_p);
964       if (inner_type != NULL_TREE)
965         return build_vector_type_for_mode (inner_type, mode);
966     }
967
968   if (mode == TYPE_MODE (dfloat32_type_node))
969     return dfloat32_type_node;
970   if (mode == TYPE_MODE (dfloat64_type_node))
971     return dfloat64_type_node;
972   if (mode == TYPE_MODE (dfloat128_type_node))
973     return dfloat128_type_node;
974
975   if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
976     {
977       if (mode == TYPE_MODE (short_fract_type_node))
978         return unsigned_p ? sat_short_fract_type_node : short_fract_type_node;
979       if (mode == TYPE_MODE (fract_type_node))
980         return unsigned_p ? sat_fract_type_node : fract_type_node;
981       if (mode == TYPE_MODE (long_fract_type_node))
982         return unsigned_p ? sat_long_fract_type_node : long_fract_type_node;
983       if (mode == TYPE_MODE (long_long_fract_type_node))
984         return unsigned_p ? sat_long_long_fract_type_node
985                          : long_long_fract_type_node;
986
987       if (mode == TYPE_MODE (unsigned_short_fract_type_node))
988         return unsigned_p ? sat_unsigned_short_fract_type_node
989                          : unsigned_short_fract_type_node;
990       if (mode == TYPE_MODE (unsigned_fract_type_node))
991         return unsigned_p ? sat_unsigned_fract_type_node
992                          : unsigned_fract_type_node;
993       if (mode == TYPE_MODE (unsigned_long_fract_type_node))
994         return unsigned_p ? sat_unsigned_long_fract_type_node
995                          : unsigned_long_fract_type_node;
996       if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
997         return unsigned_p ? sat_unsigned_long_long_fract_type_node
998                          : unsigned_long_long_fract_type_node;
999
1000       if (mode == TYPE_MODE (short_accum_type_node))
1001         return unsigned_p ? sat_short_accum_type_node : short_accum_type_node;
1002       if (mode == TYPE_MODE (accum_type_node))
1003         return unsigned_p ? sat_accum_type_node : accum_type_node;
1004       if (mode == TYPE_MODE (long_accum_type_node))
1005         return unsigned_p ? sat_long_accum_type_node : long_accum_type_node;
1006       if (mode == TYPE_MODE (long_long_accum_type_node))
1007         return unsigned_p ? sat_long_long_accum_type_node
1008                          : long_long_accum_type_node;
1009
1010       if (mode == TYPE_MODE (unsigned_short_accum_type_node))
1011         return unsigned_p ? sat_unsigned_short_accum_type_node
1012                          : unsigned_short_accum_type_node;
1013       if (mode == TYPE_MODE (unsigned_accum_type_node))
1014         return unsigned_p ? sat_unsigned_accum_type_node
1015                          : unsigned_accum_type_node;
1016       if (mode == TYPE_MODE (unsigned_long_accum_type_node))
1017         return unsigned_p ? sat_unsigned_long_accum_type_node
1018                          : unsigned_long_accum_type_node;
1019       if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
1020         return unsigned_p ? sat_unsigned_long_long_accum_type_node
1021                          : unsigned_long_long_accum_type_node;
1022
1023       if (mode == QQmode)
1024         return unsigned_p ? sat_qq_type_node : qq_type_node;
1025       if (mode == HQmode)
1026         return unsigned_p ? sat_hq_type_node : hq_type_node;
1027       if (mode == SQmode)
1028         return unsigned_p ? sat_sq_type_node : sq_type_node;
1029       if (mode == DQmode)
1030         return unsigned_p ? sat_dq_type_node : dq_type_node;
1031       if (mode == TQmode)
1032         return unsigned_p ? sat_tq_type_node : tq_type_node;
1033
1034       if (mode == UQQmode)
1035         return unsigned_p ? sat_uqq_type_node : uqq_type_node;
1036       if (mode == UHQmode)
1037         return unsigned_p ? sat_uhq_type_node : uhq_type_node;
1038       if (mode == USQmode)
1039         return unsigned_p ? sat_usq_type_node : usq_type_node;
1040       if (mode == UDQmode)
1041         return unsigned_p ? sat_udq_type_node : udq_type_node;
1042       if (mode == UTQmode)
1043         return unsigned_p ? sat_utq_type_node : utq_type_node;
1044
1045       if (mode == HAmode)
1046         return unsigned_p ? sat_ha_type_node : ha_type_node;
1047       if (mode == SAmode)
1048         return unsigned_p ? sat_sa_type_node : sa_type_node;
1049       if (mode == DAmode)
1050         return unsigned_p ? sat_da_type_node : da_type_node;
1051       if (mode == TAmode)
1052         return unsigned_p ? sat_ta_type_node : ta_type_node;
1053
1054       if (mode == UHAmode)
1055         return unsigned_p ? sat_uha_type_node : uha_type_node;
1056       if (mode == USAmode)
1057         return unsigned_p ? sat_usa_type_node : usa_type_node;
1058       if (mode == UDAmode)
1059         return unsigned_p ? sat_uda_type_node : uda_type_node;
1060       if (mode == UTAmode)
1061         return unsigned_p ? sat_uta_type_node : uta_type_node;
1062     }
1063
1064   for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
1065     if (TYPE_MODE (TREE_VALUE (t)) == mode)
1066       return TREE_VALUE (t);
1067
1068   return NULL_TREE;
1069 }
1070
1071 /* Return true if we are in the global binding level.  */
1072
1073 static bool
1074 lto_global_bindings_p (void)
1075 {
1076   return cfun == NULL;
1077 }
1078
1079 static void
1080 lto_set_decl_assembler_name (tree decl)
1081 {
1082   /* This is almost the same as lhd_set_decl_assembler_name, except that
1083      we need to uniquify file-scope names, even if they are not
1084      TREE_PUBLIC, to avoid conflicts between individual files.  */
1085   tree id;
1086
1087   if (TREE_PUBLIC (decl))
1088     id = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
1089   else
1090     {
1091       const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1092       char *label;
1093
1094       ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
1095       id = get_identifier (label);
1096     }
1097
1098   SET_DECL_ASSEMBLER_NAME (decl, id);
1099 }
1100
1101 static tree
1102 lto_pushdecl (tree t ATTRIBUTE_UNUSED)
1103 {
1104   /* Do nothing, since we get all information from DWARF and LTO
1105      sections.  */
1106   return NULL_TREE;
1107 }
1108
1109 static tree
1110 lto_getdecls (void)
1111 {
1112   /* We have our own write_globals langhook, hence the getdecls
1113      langhook shouldn't be used, except by dbxout.c, so we can't
1114      just abort here.  */
1115   return NULL_TREE;
1116 }
1117
1118 static tree
1119 lto_builtin_function (tree decl)
1120 {
1121   return decl;
1122 }
1123
1124 static void
1125 lto_register_builtin_type (tree type, const char *name)
1126 {
1127   tree decl;
1128
1129   if (!TYPE_NAME (type))
1130     {
1131       decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
1132                          get_identifier (name), type);
1133       DECL_ARTIFICIAL (decl) = 1;
1134       TYPE_NAME (type) = decl;
1135     }
1136
1137   registered_builtin_types = tree_cons (0, type, registered_builtin_types);
1138 }
1139
1140 /* Build nodes that would have be created by the C front-end; necessary
1141    for including builtin-types.def and ultimately builtins.def.  */
1142
1143 static void
1144 lto_build_c_type_nodes (void)
1145 {
1146   gcc_assert (void_type_node);
1147
1148   void_list_node = build_tree_list (NULL_TREE, void_type_node);
1149   string_type_node = build_pointer_type (char_type_node);
1150   const_string_type_node
1151     = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
1152
1153   if (strcmp (SIZE_TYPE, "unsigned int") == 0)
1154     {
1155       intmax_type_node = integer_type_node;
1156       uintmax_type_node = unsigned_type_node;
1157       signed_size_type_node = integer_type_node;
1158     }
1159   else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
1160     {
1161       intmax_type_node = long_integer_type_node;
1162       uintmax_type_node = long_unsigned_type_node;
1163       signed_size_type_node = long_integer_type_node;
1164     }
1165   else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
1166     {
1167       intmax_type_node = long_long_integer_type_node;
1168       uintmax_type_node = long_long_unsigned_type_node;
1169       signed_size_type_node = long_long_integer_type_node;
1170     }
1171   else
1172     {
1173       int i;
1174
1175       signed_size_type_node = NULL_TREE;
1176       for (i = 0; i < NUM_INT_N_ENTS; i++)
1177         if (int_n_enabled_p[i])
1178           {
1179             char name[50];
1180             sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
1181
1182             if (strcmp (name, SIZE_TYPE) == 0)
1183               {
1184                 intmax_type_node = int_n_trees[i].signed_type;
1185                 uintmax_type_node = int_n_trees[i].unsigned_type;
1186                 signed_size_type_node = int_n_trees[i].signed_type;
1187               }
1188           }
1189       if (signed_size_type_node == NULL_TREE)
1190         gcc_unreachable ();
1191     }
1192
1193   wint_type_node = unsigned_type_node;
1194   pid_type_node = integer_type_node;
1195 }
1196
1197 /* Perform LTO-specific initialization.  */
1198
1199 static bool
1200 lto_init (void)
1201 {
1202   int i;
1203
1204   /* Initialize LTO-specific data structures.  */
1205   in_lto_p = true;
1206
1207   /* We need to generate LTO if running in WPA mode.  */
1208   flag_generate_lto = (flag_wpa != NULL);
1209
1210   /* Create the basic integer types.  */
1211   build_common_tree_nodes (flag_signed_char);
1212
1213   /* The global tree for the main identifier is filled in by
1214      language-specific front-end initialization that is not run in the
1215      LTO back-end.  It appears that all languages that perform such
1216      initialization currently do so in the same way, so we do it here.  */
1217   if (main_identifier_node == NULL_TREE)
1218     main_identifier_node = get_identifier ("main");
1219
1220   /* In the C++ front-end, fileptr_type_node is defined as a variant
1221      copy of ptr_type_node, rather than ptr_node itself.  The
1222      distinction should only be relevant to the front-end, so we
1223      always use the C definition here in lto1.  */
1224   gcc_assert (fileptr_type_node == ptr_type_node);
1225   gcc_assert (TYPE_MAIN_VARIANT (fileptr_type_node) == ptr_type_node);
1226   /* Likewise for const struct tm*.  */
1227   gcc_assert (const_tm_ptr_type_node == const_ptr_type_node);
1228   gcc_assert (TYPE_MAIN_VARIANT (const_tm_ptr_type_node)
1229               == const_ptr_type_node);
1230
1231   lto_build_c_type_nodes ();
1232   gcc_assert (va_list_type_node);
1233
1234   if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
1235     {
1236       tree x = build_pointer_type (TREE_TYPE (va_list_type_node));
1237       lto_define_builtins (x, x);
1238     }
1239   else
1240     {
1241       lto_define_builtins (build_reference_type (va_list_type_node),
1242                            va_list_type_node);
1243     }
1244
1245   if (flag_cilkplus)
1246     cilk_init_builtins ();
1247
1248   targetm.init_builtins ();
1249   build_common_builtin_nodes ();
1250
1251   /* Assign names to the builtin types, otherwise they'll end up
1252      as __unknown__ in debug info.
1253      ???  We simply need to stop pre-seeding the streamer cache.
1254      Below is modeled after from c-common.c:c_common_nodes_and_builtins  */
1255 #define NAME_TYPE(t,n) \
1256   if (t) \
1257     TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL, \
1258                                 get_identifier (n), t)
1259   NAME_TYPE (integer_type_node, "int");
1260   NAME_TYPE (char_type_node, "char");
1261   NAME_TYPE (long_integer_type_node, "long int");
1262   NAME_TYPE (unsigned_type_node, "unsigned int");
1263   NAME_TYPE (long_unsigned_type_node, "long unsigned int");
1264   NAME_TYPE (long_long_integer_type_node, "long long int");
1265   NAME_TYPE (long_long_unsigned_type_node, "long long unsigned int");
1266   NAME_TYPE (short_integer_type_node, "short int");
1267   NAME_TYPE (short_unsigned_type_node, "short unsigned int");
1268   if (signed_char_type_node != char_type_node)
1269     NAME_TYPE (signed_char_type_node, "signed char");
1270   if (unsigned_char_type_node != char_type_node)
1271     NAME_TYPE (unsigned_char_type_node, "unsigned char");
1272   NAME_TYPE (float_type_node, "float");
1273   NAME_TYPE (double_type_node, "double");
1274   NAME_TYPE (long_double_type_node, "long double");
1275   NAME_TYPE (void_type_node, "void");
1276   NAME_TYPE (boolean_type_node, "bool");
1277   NAME_TYPE (complex_float_type_node, "complex float");
1278   NAME_TYPE (complex_double_type_node, "complex double");
1279   NAME_TYPE (complex_long_double_type_node, "complex long double");
1280   for (i = 0; i < NUM_INT_N_ENTS; i++)
1281     if (int_n_enabled_p[i])
1282       {
1283         char name[50];
1284         sprintf (name, "__int%d", int_n_data[i].bitsize);
1285         NAME_TYPE (int_n_trees[i].signed_type, name);
1286       }
1287 #undef NAME_TYPE
1288
1289   return true;
1290 }
1291
1292 /* Initialize tree structures required by the LTO front end.  */
1293
1294 static void lto_init_ts (void)
1295 {
1296   tree_contains_struct[NAMESPACE_DECL][TS_DECL_MINIMAL] = 1;
1297 }
1298
1299 #undef LANG_HOOKS_NAME
1300 #define LANG_HOOKS_NAME "GNU GIMPLE"
1301 #undef LANG_HOOKS_OPTION_LANG_MASK
1302 #define LANG_HOOKS_OPTION_LANG_MASK lto_option_lang_mask
1303 #undef LANG_HOOKS_COMPLAIN_WRONG_LANG_P
1304 #define LANG_HOOKS_COMPLAIN_WRONG_LANG_P lto_complain_wrong_lang_p
1305 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
1306 #define LANG_HOOKS_INIT_OPTIONS_STRUCT lto_init_options_struct
1307 #undef LANG_HOOKS_HANDLE_OPTION
1308 #define LANG_HOOKS_HANDLE_OPTION lto_handle_option
1309 #undef LANG_HOOKS_POST_OPTIONS
1310 #define LANG_HOOKS_POST_OPTIONS lto_post_options
1311 #undef LANG_HOOKS_GET_ALIAS_SET
1312 #define LANG_HOOKS_GET_ALIAS_SET gimple_get_alias_set
1313 #undef LANG_HOOKS_TYPE_FOR_MODE
1314 #define LANG_HOOKS_TYPE_FOR_MODE lto_type_for_mode
1315 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
1316 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME lto_set_decl_assembler_name
1317 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
1318 #define LANG_HOOKS_GLOBAL_BINDINGS_P lto_global_bindings_p
1319 #undef LANG_HOOKS_PUSHDECL
1320 #define LANG_HOOKS_PUSHDECL lto_pushdecl
1321 #undef LANG_HOOKS_GETDECLS
1322 #define LANG_HOOKS_GETDECLS lto_getdecls
1323 #undef LANG_HOOKS_REGISTER_BUILTIN_TYPE
1324 #define LANG_HOOKS_REGISTER_BUILTIN_TYPE lto_register_builtin_type
1325 #undef LANG_HOOKS_BUILTIN_FUNCTION
1326 #define LANG_HOOKS_BUILTIN_FUNCTION lto_builtin_function
1327 #undef LANG_HOOKS_INIT
1328 #define LANG_HOOKS_INIT lto_init
1329 #undef LANG_HOOKS_PARSE_FILE
1330 #define LANG_HOOKS_PARSE_FILE lto_main
1331 #undef LANG_HOOKS_REDUCE_BIT_FIELD_OPERATIONS
1332 #define LANG_HOOKS_REDUCE_BIT_FIELD_OPERATIONS true
1333 #undef LANG_HOOKS_TYPES_COMPATIBLE_P
1334 #define LANG_HOOKS_TYPES_COMPATIBLE_P NULL
1335 #undef LANG_HOOKS_EH_PERSONALITY
1336 #define LANG_HOOKS_EH_PERSONALITY lto_eh_personality
1337
1338 /* Attribute hooks.  */
1339 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
1340 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE lto_attribute_table
1341 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
1342 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE lto_format_attribute_table
1343
1344 #undef LANG_HOOKS_BEGIN_SECTION
1345 #define LANG_HOOKS_BEGIN_SECTION lto_obj_begin_section
1346 #undef LANG_HOOKS_APPEND_DATA
1347 #define LANG_HOOKS_APPEND_DATA lto_obj_append_data
1348 #undef LANG_HOOKS_END_SECTION
1349 #define LANG_HOOKS_END_SECTION lto_obj_end_section
1350
1351 #undef LANG_HOOKS_INIT_TS
1352 #define LANG_HOOKS_INIT_TS lto_init_ts
1353
1354 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
1355
1356 /* Language hooks that are not part of lang_hooks.  */
1357
1358 tree
1359 convert (tree type ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
1360 {
1361   gcc_unreachable ();
1362 }
1363
1364 /* Tree walking support.  */
1365
1366 static enum lto_tree_node_structure_enum
1367 lto_tree_node_structure (union lang_tree_node *t ATTRIBUTE_UNUSED)
1368 {
1369   return TS_LTO_GENERIC;
1370 }
1371
1372 #include "gtype-lto.h"
1373 #include "gt-lto-lto-lang.h"