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