cgraphbuild.c (build_cgraph_edges): Do not finalize vars with VALUE_EXPR.
[gcc.git] / gcc / lto / lto.c
1 /* Top-level LTO routines.
2 Copyright 2009, 2010, 2011, 2012 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 "opts.h"
25 #include "toplev.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "diagnostic-core.h"
29 #include "tm.h"
30 #include "cgraph.h"
31 #include "ggc.h"
32 #include "tree-ssa-operands.h"
33 #include "tree-pass.h"
34 #include "langhooks.h"
35 #include "vec.h"
36 #include "bitmap.h"
37 #include "pointer-set.h"
38 #include "ipa-prop.h"
39 #include "common.h"
40 #include "debug.h"
41 #include "timevar.h"
42 #include "gimple.h"
43 #include "lto.h"
44 #include "lto-tree.h"
45 #include "lto-streamer.h"
46 #include "tree-streamer.h"
47 #include "splay-tree.h"
48 #include "lto-partition.h"
49
50 static GTY(()) tree first_personality_decl;
51
52 /* Returns a hash code for P. */
53
54 static hashval_t
55 hash_name (const void *p)
56 {
57 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
58 return (hashval_t) htab_hash_string (ds->name);
59 }
60
61
62 /* Returns nonzero if P1 and P2 are equal. */
63
64 static int
65 eq_name (const void *p1, const void *p2)
66 {
67 const struct lto_section_slot *s1 =
68 (const struct lto_section_slot *) p1;
69 const struct lto_section_slot *s2 =
70 (const struct lto_section_slot *) p2;
71
72 return strcmp (s1->name, s2->name) == 0;
73 }
74
75 /* Free lto_section_slot */
76
77 static void
78 free_with_string (void *arg)
79 {
80 struct lto_section_slot *s = (struct lto_section_slot *)arg;
81
82 free (CONST_CAST (char *, s->name));
83 free (arg);
84 }
85
86 /* Create section hash table */
87
88 htab_t
89 lto_obj_create_section_hash_table (void)
90 {
91 return htab_create (37, hash_name, eq_name, free_with_string);
92 }
93
94 /* Delete an allocated integer KEY in the splay tree. */
95
96 static void
97 lto_splay_tree_delete_id (splay_tree_key key)
98 {
99 free ((void *) key);
100 }
101
102 /* Compare splay tree node ids A and B. */
103
104 static int
105 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
106 {
107 unsigned HOST_WIDE_INT ai;
108 unsigned HOST_WIDE_INT bi;
109
110 ai = *(unsigned HOST_WIDE_INT *) a;
111 bi = *(unsigned HOST_WIDE_INT *) b;
112
113 if (ai < bi)
114 return -1;
115 else if (ai > bi)
116 return 1;
117 return 0;
118 }
119
120 /* Look up splay tree node by ID in splay tree T. */
121
122 static splay_tree_node
123 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
124 {
125 return splay_tree_lookup (t, (splay_tree_key) &id);
126 }
127
128 /* Check if KEY has ID. */
129
130 static bool
131 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
132 {
133 return *(unsigned HOST_WIDE_INT *) key == id;
134 }
135
136 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
137 The ID is allocated separately because we need HOST_WIDE_INTs which may
138 be wider than a splay_tree_key. */
139
140 static void
141 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
142 struct lto_file_decl_data *file_data)
143 {
144 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
145 *idp = id;
146 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
147 }
148
149 /* Create a splay tree. */
150
151 static splay_tree
152 lto_splay_tree_new (void)
153 {
154 return splay_tree_new (lto_splay_tree_compare_ids,
155 lto_splay_tree_delete_id,
156 NULL);
157 }
158
159 /* Read the constructors and inits. */
160
161 static void
162 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
163 {
164 size_t len;
165 const char *data = lto_get_section_data (file_data,
166 LTO_section_static_initializer,
167 NULL, &len);
168 lto_input_constructors_and_inits (file_data, data);
169 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
170 data, len);
171 }
172
173 /* Return true when NODE has a clone that is analyzed (i.e. we need
174 to load its body even if the node itself is not needed). */
175
176 static bool
177 has_analyzed_clone_p (struct cgraph_node *node)
178 {
179 struct cgraph_node *orig = node;
180 node = node->clones;
181 if (node)
182 while (node != orig)
183 {
184 if (node->analyzed)
185 return true;
186 if (node->clones)
187 node = node->clones;
188 else if (node->next_sibling_clone)
189 node = node->next_sibling_clone;
190 else
191 {
192 while (node != orig && !node->next_sibling_clone)
193 node = node->clone_of;
194 if (node != orig)
195 node = node->next_sibling_clone;
196 }
197 }
198 return false;
199 }
200
201 /* Read the function body for the function associated with NODE. */
202
203 static void
204 lto_materialize_function (struct cgraph_node *node)
205 {
206 tree decl;
207 struct lto_file_decl_data *file_data;
208 const char *data, *name;
209 size_t len;
210
211 decl = node->symbol.decl;
212 /* Read in functions with body (analyzed nodes)
213 and also functions that are needed to produce virtual clones. */
214 if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node))
215 {
216 /* Clones and thunks don't need to be read. */
217 if (node->clone_of)
218 return;
219
220 /* Load the function body only if not operating in WPA mode. In
221 WPA mode, the body of the function is not needed. */
222 if (!flag_wpa)
223 {
224 file_data = node->symbol.lto_file_data;
225 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
226
227 /* We may have renamed the declaration, e.g., a static function. */
228 name = lto_get_decl_name_mapping (file_data, name);
229
230 data = lto_get_section_data (file_data, LTO_section_function_body,
231 name, &len);
232 if (!data)
233 fatal_error ("%s: section %s is missing",
234 file_data->file_name,
235 name);
236
237 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
238
239 allocate_struct_function (decl, false);
240 announce_function (decl);
241 lto_input_function_body (file_data, decl, data);
242 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
243 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
244 lto_stats.num_function_bodies++;
245 lto_free_section_data (file_data, LTO_section_function_body, name,
246 data, len);
247 ggc_collect ();
248 }
249 }
250
251 /* Let the middle end know about the function. */
252 rest_of_decl_compilation (decl, 1, 0);
253 }
254
255
256 /* Decode the content of memory pointed to by DATA in the in decl
257 state object STATE. DATA_IN points to a data_in structure for
258 decoding. Return the address after the decoded object in the
259 input. */
260
261 static const uint32_t *
262 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
263 struct lto_in_decl_state *state)
264 {
265 uint32_t ix;
266 tree decl;
267 uint32_t i, j;
268
269 ix = *data++;
270 decl = streamer_tree_cache_get (data_in->reader_cache, ix);
271 if (TREE_CODE (decl) != FUNCTION_DECL)
272 {
273 gcc_assert (decl == void_type_node);
274 decl = NULL_TREE;
275 }
276 state->fn_decl = decl;
277
278 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
279 {
280 uint32_t size = *data++;
281 tree *decls = ggc_alloc_vec_tree (size);
282
283 for (j = 0; j < size; j++)
284 decls[j] = streamer_tree_cache_get (data_in->reader_cache, data[j]);
285
286 state->streams[i].size = size;
287 state->streams[i].trees = decls;
288 data += size;
289 }
290
291 return data;
292 }
293
294 /* A hashtable of trees that potentially refer to variables or functions
295 that must be replaced with their prevailing variant. */
296 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
297 tree_with_vars;
298
299 /* Remember that T is a tree that (potentially) refers to a variable
300 or function decl that may be replaced with its prevailing variant. */
301 static void
302 remember_with_vars (tree t)
303 {
304 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
305 }
306
307 #define GIMPLE_REGISTER_TYPE(tt) \
308 (TREE_VISITED (tt) ? gimple_register_type (tt) : tt)
309
310 #define LTO_FIXUP_TREE(tt) \
311 do \
312 { \
313 if (tt) \
314 { \
315 if (TYPE_P (tt)) \
316 (tt) = GIMPLE_REGISTER_TYPE (tt); \
317 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
318 remember_with_vars (t); \
319 } \
320 } while (0)
321
322 static void lto_fixup_types (tree);
323
324 /* Fix up fields of a tree_typed T. */
325
326 static void
327 lto_ft_typed (tree t)
328 {
329 LTO_FIXUP_TREE (TREE_TYPE (t));
330 }
331
332 /* Fix up fields of a tree_common T. */
333
334 static void
335 lto_ft_common (tree t)
336 {
337 lto_ft_typed (t);
338 LTO_FIXUP_TREE (TREE_CHAIN (t));
339 }
340
341 /* Fix up fields of a decl_minimal T. */
342
343 static void
344 lto_ft_decl_minimal (tree t)
345 {
346 lto_ft_common (t);
347 LTO_FIXUP_TREE (DECL_NAME (t));
348 LTO_FIXUP_TREE (DECL_CONTEXT (t));
349 }
350
351 /* Fix up fields of a decl_common T. */
352
353 static void
354 lto_ft_decl_common (tree t)
355 {
356 lto_ft_decl_minimal (t);
357 LTO_FIXUP_TREE (DECL_SIZE (t));
358 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t));
359 LTO_FIXUP_TREE (DECL_INITIAL (t));
360 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t));
361 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t));
362 }
363
364 /* Fix up fields of a decl_with_vis T. */
365
366 static void
367 lto_ft_decl_with_vis (tree t)
368 {
369 lto_ft_decl_common (t);
370
371 /* Accessor macro has side-effects, use field-name here. */
372 LTO_FIXUP_TREE (t->decl_with_vis.assembler_name);
373 LTO_FIXUP_TREE (DECL_SECTION_NAME (t));
374 }
375
376 /* Fix up fields of a decl_non_common T. */
377
378 static void
379 lto_ft_decl_non_common (tree t)
380 {
381 lto_ft_decl_with_vis (t);
382 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t));
383 LTO_FIXUP_TREE (DECL_RESULT_FLD (t));
384 LTO_FIXUP_TREE (DECL_VINDEX (t));
385 /* The C frontends may create exact duplicates for DECL_ORIGINAL_TYPE
386 like for 'typedef enum foo foo'. We have no way of avoiding to
387 merge them and dwarf2out.c cannot deal with this,
388 so fix this up by clearing DECL_ORIGINAL_TYPE in this case. */
389 if (TREE_CODE (t) == TYPE_DECL
390 && DECL_ORIGINAL_TYPE (t) == TREE_TYPE (t))
391 DECL_ORIGINAL_TYPE (t) = NULL_TREE;
392 }
393
394 /* Fix up fields of a decl_non_common T. */
395
396 static void
397 lto_ft_function (tree t)
398 {
399 lto_ft_decl_non_common (t);
400 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t));
401 }
402
403 /* Fix up fields of a field_decl T. */
404
405 static void
406 lto_ft_field_decl (tree t)
407 {
408 lto_ft_decl_common (t);
409 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t));
410 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t));
411 LTO_FIXUP_TREE (DECL_QUALIFIER (t));
412 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t));
413 LTO_FIXUP_TREE (DECL_FCONTEXT (t));
414 }
415
416 /* Fix up fields of a type T. */
417
418 static void
419 lto_ft_type (tree t)
420 {
421 lto_ft_common (t);
422 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t));
423 LTO_FIXUP_TREE (TYPE_SIZE (t));
424 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t));
425 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t));
426 LTO_FIXUP_TREE (TYPE_NAME (t));
427
428 /* Accessors are for derived node types only. */
429 if (!POINTER_TYPE_P (t))
430 LTO_FIXUP_TREE (TYPE_MINVAL (t));
431 LTO_FIXUP_TREE (TYPE_MAXVAL (t));
432
433 /* Accessor is for derived node types only. */
434 LTO_FIXUP_TREE (t->type_non_common.binfo);
435
436 LTO_FIXUP_TREE (TYPE_CONTEXT (t));
437 }
438
439 /* Fix up fields of a BINFO T. */
440
441 static void
442 lto_ft_binfo (tree t)
443 {
444 unsigned HOST_WIDE_INT i, n;
445 tree base, saved_base;
446
447 lto_ft_common (t);
448 LTO_FIXUP_TREE (BINFO_VTABLE (t));
449 LTO_FIXUP_TREE (BINFO_OFFSET (t));
450 LTO_FIXUP_TREE (BINFO_VIRTUALS (t));
451 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t));
452 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
453 for (i = 0; i < n; i++)
454 {
455 saved_base = base = BINFO_BASE_ACCESS (t, i);
456 LTO_FIXUP_TREE (base);
457 if (base != saved_base)
458 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
459 }
460 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t));
461 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t));
462 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t));
463 n = BINFO_N_BASE_BINFOS (t);
464 for (i = 0; i < n; i++)
465 {
466 saved_base = base = BINFO_BASE_BINFO (t, i);
467 LTO_FIXUP_TREE (base);
468 if (base != saved_base)
469 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
470 }
471 }
472
473 /* Fix up fields of a CONSTRUCTOR T. */
474
475 static void
476 lto_ft_constructor (tree t)
477 {
478 unsigned HOST_WIDE_INT idx;
479 constructor_elt *ce;
480
481 lto_ft_typed (t);
482
483 for (idx = 0;
484 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
485 idx++)
486 {
487 LTO_FIXUP_TREE (ce->index);
488 LTO_FIXUP_TREE (ce->value);
489 }
490 }
491
492 /* Fix up fields of an expression tree T. */
493
494 static void
495 lto_ft_expr (tree t)
496 {
497 int i;
498 lto_ft_typed (t);
499 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
500 LTO_FIXUP_TREE (TREE_OPERAND (t, i));
501 }
502
503 /* Given a tree T fixup fields of T by replacing types with their merged
504 variant and other entities by an equal entity from an earlier compilation
505 unit, or an entity being canonical in a different way. This includes
506 for instance integer or string constants. */
507
508 static void
509 lto_fixup_types (tree t)
510 {
511 switch (TREE_CODE (t))
512 {
513 case IDENTIFIER_NODE:
514 break;
515
516 case TREE_LIST:
517 LTO_FIXUP_TREE (TREE_VALUE (t));
518 LTO_FIXUP_TREE (TREE_PURPOSE (t));
519 LTO_FIXUP_TREE (TREE_CHAIN (t));
520 break;
521
522 case FIELD_DECL:
523 lto_ft_field_decl (t);
524 break;
525
526 case LABEL_DECL:
527 case CONST_DECL:
528 case PARM_DECL:
529 case RESULT_DECL:
530 case IMPORTED_DECL:
531 lto_ft_decl_common (t);
532 break;
533
534 case VAR_DECL:
535 lto_ft_decl_with_vis (t);
536 break;
537
538 case TYPE_DECL:
539 lto_ft_decl_non_common (t);
540 break;
541
542 case FUNCTION_DECL:
543 lto_ft_function (t);
544 break;
545
546 case TREE_BINFO:
547 lto_ft_binfo (t);
548 break;
549
550 case PLACEHOLDER_EXPR:
551 lto_ft_common (t);
552 break;
553
554 case BLOCK:
555 case TRANSLATION_UNIT_DECL:
556 case OPTIMIZATION_NODE:
557 case TARGET_OPTION_NODE:
558 break;
559
560 default:
561 if (TYPE_P (t))
562 lto_ft_type (t);
563 else if (TREE_CODE (t) == CONSTRUCTOR)
564 lto_ft_constructor (t);
565 else if (CONSTANT_CLASS_P (t))
566 LTO_FIXUP_TREE (TREE_TYPE (t));
567 else if (EXPR_P (t))
568 {
569 lto_ft_expr (t);
570 }
571 else
572 {
573 remember_with_vars (t);
574 }
575 }
576 }
577
578
579 /* Return the resolution for the decl with index INDEX from DATA_IN. */
580
581 static enum ld_plugin_symbol_resolution
582 get_resolution (struct data_in *data_in, unsigned index)
583 {
584 if (data_in->globals_resolution)
585 {
586 ld_plugin_symbol_resolution_t ret;
587 /* We can have references to not emitted functions in
588 DECL_FUNCTION_PERSONALITY at least. So we can and have
589 to indeed return LDPR_UNKNOWN in some cases. */
590 if (VEC_length (ld_plugin_symbol_resolution_t,
591 data_in->globals_resolution) <= index)
592 return LDPR_UNKNOWN;
593 ret = VEC_index (ld_plugin_symbol_resolution_t,
594 data_in->globals_resolution,
595 index);
596 return ret;
597 }
598 else
599 /* Delay resolution finding until decl merging. */
600 return LDPR_UNKNOWN;
601 }
602
603
604 /* Register DECL with the global symbol table and change its
605 name if necessary to avoid name clashes for static globals across
606 different files. */
607
608 static void
609 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl)
610 {
611 tree context;
612
613 /* Variable has file scope, not local. Need to ensure static variables
614 between different files don't clash unexpectedly. */
615 if (!TREE_PUBLIC (decl)
616 && !((context = decl_function_context (decl))
617 && auto_var_in_fn_p (decl, context)))
618 {
619 /* ??? We normally pre-mangle names before we serialize them
620 out. Here, in lto1, we do not know the language, and
621 thus cannot do the mangling again. Instead, we just
622 append a suffix to the mangled name. The resulting name,
623 however, is not a properly-formed mangled name, and will
624 confuse any attempt to unmangle it. */
625 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
626 char *label;
627
628 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
629 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
630 rest_of_decl_compilation (decl, 1, 0);
631 VEC_safe_push (tree, gc, lto_global_var_decls, decl);
632 }
633
634 /* If this variable has already been declared, queue the
635 declaration for merging. */
636 if (TREE_PUBLIC (decl))
637 {
638 unsigned ix;
639 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
640 gcc_unreachable ();
641 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
642 data_in->file_data);
643 }
644 }
645
646
647 /* Register DECL with the global symbol table and change its
648 name if necessary to avoid name clashes for static globals across
649 different files. DATA_IN contains descriptors and tables for the
650 file being read. */
651
652 static void
653 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl)
654 {
655 /* Need to ensure static entities between different files
656 don't clash unexpectedly. */
657 if (!TREE_PUBLIC (decl))
658 {
659 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
660 may set the assembler name where it was previously empty. */
661 tree old_assembler_name = decl->decl_with_vis.assembler_name;
662
663 /* FIXME lto: We normally pre-mangle names before we serialize
664 them out. Here, in lto1, we do not know the language, and
665 thus cannot do the mangling again. Instead, we just append a
666 suffix to the mangled name. The resulting name, however, is
667 not a properly-formed mangled name, and will confuse any
668 attempt to unmangle it. */
669 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
670 char *label;
671
672 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
673 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
674
675 /* We may arrive here with the old assembler name not set
676 if the function body is not needed, e.g., it has been
677 inlined away and does not appear in the cgraph. */
678 if (old_assembler_name)
679 {
680 tree new_assembler_name = DECL_ASSEMBLER_NAME (decl);
681
682 /* Make the original assembler name available for later use.
683 We may have used it to indicate the section within its
684 object file where the function body may be found.
685 FIXME lto: Find a better way to maintain the function decl
686 to body section mapping so we don't need this hack. */
687 lto_record_renamed_decl (data_in->file_data,
688 IDENTIFIER_POINTER (old_assembler_name),
689 IDENTIFIER_POINTER (new_assembler_name));
690 }
691 }
692
693 /* If this variable has already been declared, queue the
694 declaration for merging. */
695 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
696 {
697 unsigned ix;
698 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
699 gcc_unreachable ();
700 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
701 data_in->file_data);
702 }
703 }
704
705
706 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
707 for one compilation unit) go over all trees starting at index FROM until the
708 end of the sequence and replace fields of those trees, and the trees
709 themself with their canonical variants as per gimple_register_type. */
710
711 static void
712 uniquify_nodes (struct data_in *data_in, unsigned from)
713 {
714 struct streamer_tree_cache_d *cache = data_in->reader_cache;
715 unsigned len = VEC_length (tree, cache->nodes);
716 unsigned i;
717
718 /* Go backwards because children streamed for the first time come
719 as part of their parents, and hence are created after them. */
720
721 /* First register all the types in the cache. This makes sure to
722 have the original structure in the type cycles when registering
723 them and computing hashes. */
724 for (i = len; i-- > from;)
725 {
726 tree t = VEC_index (tree, cache->nodes, i);
727 if (t && TYPE_P (t))
728 {
729 tree newt = gimple_register_type (t);
730 /* Mark non-prevailing types so we fix them up. No need
731 to reset that flag afterwards - nothing that refers
732 to those types is left and they are collected. */
733 if (newt != t)
734 TREE_VISITED (t) = 1;
735 }
736 }
737
738 /* Second fixup all trees in the new cache entries. */
739 for (i = len; i-- > from;)
740 {
741 tree t = VEC_index (tree, cache->nodes, i);
742 tree oldt = t;
743 if (!t)
744 continue;
745
746 /* First fixup the fields of T. */
747 lto_fixup_types (t);
748
749 if (!TYPE_P (t))
750 continue;
751
752 /* Now try to find a canonical variant of T itself. */
753 t = GIMPLE_REGISTER_TYPE (t);
754
755 if (t == oldt)
756 {
757 /* The following re-creates proper variant lists while fixing up
758 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
759 variant list state before fixup is broken. */
760 tree tem, mv;
761
762 /* Remove us from our main variant list if we are not the
763 variant leader. */
764 if (TYPE_MAIN_VARIANT (t) != t)
765 {
766 tem = TYPE_MAIN_VARIANT (t);
767 while (tem && TYPE_NEXT_VARIANT (tem) != t)
768 tem = TYPE_NEXT_VARIANT (tem);
769 if (tem)
770 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
771 TYPE_NEXT_VARIANT (t) = NULL_TREE;
772 }
773
774 /* Query our new main variant. */
775 mv = GIMPLE_REGISTER_TYPE (TYPE_MAIN_VARIANT (t));
776
777 /* If we were the variant leader and we get replaced ourselves drop
778 all variants from our list. */
779 if (TYPE_MAIN_VARIANT (t) == t
780 && mv != t)
781 {
782 tem = t;
783 while (tem)
784 {
785 tree tem2 = TYPE_NEXT_VARIANT (tem);
786 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
787 tem = tem2;
788 }
789 }
790
791 /* If we are not our own variant leader link us into our new leaders
792 variant list. */
793 if (mv != t)
794 {
795 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
796 TYPE_NEXT_VARIANT (mv) = t;
797 if (RECORD_OR_UNION_TYPE_P (t))
798 TYPE_BINFO (t) = TYPE_BINFO (mv);
799 /* Preserve the invariant that type variants share their
800 TYPE_FIELDS. */
801 if (RECORD_OR_UNION_TYPE_P (t)
802 && TYPE_FIELDS (mv) != TYPE_FIELDS (t))
803 {
804 tree f1, f2;
805 for (f1 = TYPE_FIELDS (mv), f2 = TYPE_FIELDS (t);
806 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
807 {
808 unsigned ix;
809 gcc_assert (f1 != f2
810 && DECL_NAME (f1) == DECL_NAME (f2));
811 if (!streamer_tree_cache_lookup (cache, f2, &ix))
812 gcc_unreachable ();
813 /* If we're going to replace an element which we'd
814 still visit in the next iterations, we wouldn't
815 handle it, so do it here. We do have to handle it
816 even though the field_decl itself will be removed,
817 as it could refer to e.g. integer_cst which we
818 wouldn't reach via any other way, hence they
819 (and their type) would stay uncollected. */
820 /* ??? We should rather make sure to replace all
821 references to f2 with f1. That means handling
822 COMPONENT_REFs and CONSTRUCTOR elements in
823 lto_fixup_types and special-case the field-decl
824 operand handling. */
825 /* ??? Not sure the above is all relevant in this
826 path canonicalizing TYPE_FIELDS to that of the
827 main variant. */
828 if (ix < i)
829 lto_fixup_types (f2);
830 streamer_tree_cache_insert_at (cache, f1, ix);
831 }
832 TYPE_FIELDS (t) = TYPE_FIELDS (mv);
833 }
834 }
835
836 /* Finally adjust our main variant and fix it up. */
837 TYPE_MAIN_VARIANT (t) = mv;
838
839 /* The following reconstructs the pointer chains
840 of the new pointed-to type if we are a main variant. We do
841 not stream those so they are broken before fixup. */
842 if (TREE_CODE (t) == POINTER_TYPE
843 && TYPE_MAIN_VARIANT (t) == t)
844 {
845 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
846 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
847 }
848 else if (TREE_CODE (t) == REFERENCE_TYPE
849 && TYPE_MAIN_VARIANT (t) == t)
850 {
851 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
852 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
853 }
854 }
855
856 else
857 {
858 if (RECORD_OR_UNION_TYPE_P (t))
859 {
860 tree f1, f2;
861 if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt))
862 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt);
863 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
864 {
865 unsigned ix;
866 gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2));
867 if (!streamer_tree_cache_lookup (cache, f2, &ix))
868 gcc_unreachable ();
869 /* If we're going to replace an element which we'd
870 still visit in the next iterations, we wouldn't
871 handle it, so do it here. We do have to handle it
872 even though the field_decl itself will be removed,
873 as it could refer to e.g. integer_cst which we
874 wouldn't reach via any other way, hence they
875 (and their type) would stay uncollected. */
876 /* ??? We should rather make sure to replace all
877 references to f2 with f1. That means handling
878 COMPONENT_REFs and CONSTRUCTOR elements in
879 lto_fixup_types and special-case the field-decl
880 operand handling. */
881 if (ix < i)
882 lto_fixup_types (f2);
883 streamer_tree_cache_insert_at (cache, f1, ix);
884 }
885 }
886
887 /* If we found a tree that is equal to oldt replace it in the
888 cache, so that further users (in the various LTO sections)
889 make use of it. */
890 streamer_tree_cache_insert_at (cache, t, i);
891 }
892 }
893
894 /* Finally compute the canonical type of all TREE_TYPEs and register
895 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
896 From this point there are no longer any types with
897 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
898 This step requires the TYPE_POINTER_TO lists being present, so
899 make sure it is done last. */
900 for (i = len; i-- > from;)
901 {
902 tree t = VEC_index (tree, cache->nodes, i);
903 if (t == NULL_TREE)
904 continue;
905
906 if (TREE_CODE (t) == VAR_DECL)
907 lto_register_var_decl_in_symtab (data_in, t);
908 else if (TREE_CODE (t) == FUNCTION_DECL && !DECL_BUILT_IN (t))
909 lto_register_function_decl_in_symtab (data_in, t);
910 else if (!flag_wpa
911 && TREE_CODE (t) == TYPE_DECL)
912 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
913 else if (TYPE_P (t) && !TYPE_CANONICAL (t))
914 TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
915 }
916 }
917
918
919 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
920 RESOLUTIONS is the set of symbols picked by the linker (read from the
921 resolution file when the linker plugin is being used). */
922
923 static void
924 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
925 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
926 {
927 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
928 const int decl_offset = sizeof (struct lto_decl_header);
929 const int main_offset = decl_offset + header->decl_state_size;
930 const int string_offset = main_offset + header->main_size;
931 struct lto_input_block ib_main;
932 struct data_in *data_in;
933 unsigned int i;
934 const uint32_t *data_ptr, *data_end;
935 uint32_t num_decl_states;
936
937 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
938 header->main_size);
939
940 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
941 header->string_size, resolutions);
942
943 /* We do not uniquify the pre-loaded cache entries, those are middle-end
944 internal types that should not be merged. */
945
946 /* Read the global declarations and types. */
947 while (ib_main.p < ib_main.len)
948 {
949 tree t;
950 unsigned from = VEC_length (tree, data_in->reader_cache->nodes);
951 t = stream_read_tree (&ib_main, data_in);
952 gcc_assert (t && ib_main.p <= ib_main.len);
953 uniquify_nodes (data_in, from);
954 }
955
956 /* Read in lto_in_decl_state objects. */
957 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
958 data_end =
959 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
960 num_decl_states = *data_ptr++;
961
962 gcc_assert (num_decl_states > 0);
963 decl_data->global_decl_state = lto_new_in_decl_state ();
964 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
965 decl_data->global_decl_state);
966
967 /* Read in per-function decl states and enter them in hash table. */
968 decl_data->function_decl_states =
969 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
970
971 for (i = 1; i < num_decl_states; i++)
972 {
973 struct lto_in_decl_state *state = lto_new_in_decl_state ();
974 void **slot;
975
976 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
977 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
978 gcc_assert (*slot == NULL);
979 *slot = state;
980 }
981
982 if (data_ptr != data_end)
983 internal_error ("bytecode stream: garbage at the end of symbols section");
984
985 /* Set the current decl state to be the global state. */
986 decl_data->current_decl_state = decl_data->global_decl_state;
987
988 lto_data_in_delete (data_in);
989 }
990
991 /* Custom version of strtoll, which is not portable. */
992
993 static HOST_WIDEST_INT
994 lto_parse_hex (const char *p)
995 {
996 HOST_WIDEST_INT ret = 0;
997
998 for (; *p != '\0'; ++p)
999 {
1000 char c = *p;
1001 unsigned char part;
1002 ret <<= 4;
1003 if (c >= '0' && c <= '9')
1004 part = c - '0';
1005 else if (c >= 'a' && c <= 'f')
1006 part = c - 'a' + 10;
1007 else if (c >= 'A' && c <= 'F')
1008 part = c - 'A' + 10;
1009 else
1010 internal_error ("could not parse hex number");
1011 ret |= part;
1012 }
1013
1014 return ret;
1015 }
1016
1017 /* Read resolution for file named FILE_NAME. The resolution is read from
1018 RESOLUTION. */
1019
1020 static void
1021 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1022 {
1023 /* We require that objects in the resolution file are in the same
1024 order as the lto1 command line. */
1025 unsigned int name_len;
1026 char *obj_name;
1027 unsigned int num_symbols;
1028 unsigned int i;
1029 struct lto_file_decl_data *file_data;
1030 unsigned max_index = 0;
1031 splay_tree_node nd = NULL;
1032
1033 if (!resolution)
1034 return;
1035
1036 name_len = strlen (file->filename);
1037 obj_name = XNEWVEC (char, name_len + 1);
1038 fscanf (resolution, " "); /* Read white space. */
1039
1040 fread (obj_name, sizeof (char), name_len, resolution);
1041 obj_name[name_len] = '\0';
1042 if (filename_cmp (obj_name, file->filename) != 0)
1043 internal_error ("unexpected file name %s in linker resolution file. "
1044 "Expected %s", obj_name, file->filename);
1045 if (file->offset != 0)
1046 {
1047 int t;
1048 char offset_p[17];
1049 HOST_WIDEST_INT offset;
1050 t = fscanf (resolution, "@0x%16s", offset_p);
1051 if (t != 1)
1052 internal_error ("could not parse file offset");
1053 offset = lto_parse_hex (offset_p);
1054 if (offset != file->offset)
1055 internal_error ("unexpected offset");
1056 }
1057
1058 free (obj_name);
1059
1060 fscanf (resolution, "%u", &num_symbols);
1061
1062 for (i = 0; i < num_symbols; i++)
1063 {
1064 int t;
1065 unsigned index;
1066 unsigned HOST_WIDE_INT id;
1067 char r_str[27];
1068 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1069 unsigned int j;
1070 unsigned int lto_resolution_str_len =
1071 sizeof (lto_resolution_str) / sizeof (char *);
1072
1073 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1074 &index, &id, r_str);
1075 if (t != 3)
1076 internal_error ("invalid line in the resolution file");
1077 if (index > max_index)
1078 max_index = index;
1079
1080 for (j = 0; j < lto_resolution_str_len; j++)
1081 {
1082 if (strcmp (lto_resolution_str[j], r_str) == 0)
1083 {
1084 r = (enum ld_plugin_symbol_resolution) j;
1085 break;
1086 }
1087 }
1088 if (j == lto_resolution_str_len)
1089 internal_error ("invalid resolution in the resolution file");
1090
1091 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1092 {
1093 nd = lto_splay_tree_lookup (file_ids, id);
1094 if (nd == NULL)
1095 internal_error ("resolution sub id " HOST_WIDE_INT_PRINT_HEX_PURE
1096 " not in object file", id);
1097 }
1098
1099 file_data = (struct lto_file_decl_data *)nd->value;
1100 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap,
1101 file_data->resolutions,
1102 max_index + 1);
1103 VEC_replace (ld_plugin_symbol_resolution_t,
1104 file_data->resolutions, index, r);
1105 }
1106 }
1107
1108 /* List of file_decl_datas */
1109 struct file_data_list
1110 {
1111 struct lto_file_decl_data *first, *last;
1112 };
1113
1114 /* Is the name for a id'ed LTO section? */
1115
1116 static int
1117 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1118 {
1119 const char *s;
1120
1121 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
1122 return 0;
1123 s = strrchr (name, '.');
1124 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1125 }
1126
1127 /* Create file_data of each sub file id */
1128
1129 static int
1130 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1131 struct file_data_list *list)
1132 {
1133 struct lto_section_slot s_slot, *new_slot;
1134 unsigned HOST_WIDE_INT id;
1135 splay_tree_node nd;
1136 void **hash_slot;
1137 char *new_name;
1138 struct lto_file_decl_data *file_data;
1139
1140 if (!lto_section_with_id (ls->name, &id))
1141 return 1;
1142
1143 /* Find hash table of sub module id */
1144 nd = lto_splay_tree_lookup (file_ids, id);
1145 if (nd != NULL)
1146 {
1147 file_data = (struct lto_file_decl_data *)nd->value;
1148 }
1149 else
1150 {
1151 file_data = ggc_alloc_lto_file_decl_data ();
1152 memset(file_data, 0, sizeof (struct lto_file_decl_data));
1153 file_data->id = id;
1154 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1155 lto_splay_tree_insert (file_ids, id, file_data);
1156
1157 /* Maintain list in linker order */
1158 if (!list->first)
1159 list->first = file_data;
1160 if (list->last)
1161 list->last->next = file_data;
1162 list->last = file_data;
1163 }
1164
1165 /* Copy section into sub module hash table */
1166 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
1167 s_slot.name = new_name;
1168 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
1169 gcc_assert (*hash_slot == NULL);
1170
1171 new_slot = XDUP (struct lto_section_slot, ls);
1172 new_slot->name = new_name;
1173 *hash_slot = new_slot;
1174 return 1;
1175 }
1176
1177 /* Read declarations and other initializations for a FILE_DATA. */
1178
1179 static void
1180 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
1181 {
1182 const char *data;
1183 size_t len;
1184
1185 file_data->renaming_hash_table = lto_create_renaming_table ();
1186 file_data->file_name = file->filename;
1187 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
1188 if (data == NULL)
1189 {
1190 internal_error ("cannot read LTO decls from %s", file_data->file_name);
1191 return;
1192 }
1193 lto_read_decls (file_data, data, file_data->resolutions);
1194 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
1195 }
1196
1197 /* Finalize FILE_DATA in FILE and increase COUNT. */
1198
1199 static int
1200 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
1201 int *count)
1202 {
1203 lto_file_finalize (file_data, file);
1204 if (cgraph_dump_file)
1205 fprintf (cgraph_dump_file, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
1206 file_data->file_name, file_data->id);
1207 (*count)++;
1208 return 0;
1209 }
1210
1211 /* Generate a TREE representation for all types and external decls
1212 entities in FILE.
1213
1214 Read all of the globals out of the file. Then read the cgraph
1215 and process the .o index into the cgraph nodes so that it can open
1216 the .o file to load the functions and ipa information. */
1217
1218 static struct lto_file_decl_data *
1219 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
1220 {
1221 struct lto_file_decl_data *file_data = NULL;
1222 splay_tree file_ids;
1223 htab_t section_hash_table;
1224 struct lto_section_slot *section;
1225 struct file_data_list file_list;
1226 struct lto_section_list section_list;
1227
1228 memset (&section_list, 0, sizeof (struct lto_section_list));
1229 section_hash_table = lto_obj_build_section_table (file, &section_list);
1230
1231 /* Find all sub modules in the object and put their sections into new hash
1232 tables in a splay tree. */
1233 file_ids = lto_splay_tree_new ();
1234 memset (&file_list, 0, sizeof (struct file_data_list));
1235 for (section = section_list.first; section != NULL; section = section->next)
1236 create_subid_section_table (section, file_ids, &file_list);
1237
1238 /* Add resolutions to file ids */
1239 lto_resolution_read (file_ids, resolution_file, file);
1240
1241 /* Finalize each lto file for each submodule in the merged object */
1242 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
1243 lto_create_files_from_ids (file, file_data, count);
1244
1245 splay_tree_delete (file_ids);
1246 htab_delete (section_hash_table);
1247
1248 return file_list.first;
1249 }
1250
1251 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
1252 #define LTO_MMAP_IO 1
1253 #endif
1254
1255 #if LTO_MMAP_IO
1256 /* Page size of machine is used for mmap and munmap calls. */
1257 static size_t page_mask;
1258 #endif
1259
1260 /* Get the section data of length LEN from FILENAME starting at
1261 OFFSET. The data segment must be freed by the caller when the
1262 caller is finished. Returns NULL if all was not well. */
1263
1264 static char *
1265 lto_read_section_data (struct lto_file_decl_data *file_data,
1266 intptr_t offset, size_t len)
1267 {
1268 char *result;
1269 static int fd = -1;
1270 static char *fd_name;
1271 #if LTO_MMAP_IO
1272 intptr_t computed_len;
1273 intptr_t computed_offset;
1274 intptr_t diff;
1275 #endif
1276
1277 /* Keep a single-entry file-descriptor cache. The last file we
1278 touched will get closed at exit.
1279 ??? Eventually we want to add a more sophisticated larger cache
1280 or rather fix function body streaming to not stream them in
1281 practically random order. */
1282 if (fd != -1
1283 && filename_cmp (fd_name, file_data->file_name) != 0)
1284 {
1285 free (fd_name);
1286 close (fd);
1287 fd = -1;
1288 }
1289 if (fd == -1)
1290 {
1291 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
1292 if (fd == -1)
1293 {
1294 fatal_error ("Cannot open %s", file_data->file_name);
1295 return NULL;
1296 }
1297 fd_name = xstrdup (file_data->file_name);
1298 }
1299
1300 #if LTO_MMAP_IO
1301 if (!page_mask)
1302 {
1303 size_t page_size = sysconf (_SC_PAGE_SIZE);
1304 page_mask = ~(page_size - 1);
1305 }
1306
1307 computed_offset = offset & page_mask;
1308 diff = offset - computed_offset;
1309 computed_len = len + diff;
1310
1311 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
1312 fd, computed_offset);
1313 if (result == MAP_FAILED)
1314 {
1315 fatal_error ("Cannot map %s", file_data->file_name);
1316 return NULL;
1317 }
1318
1319 return result + diff;
1320 #else
1321 result = (char *) xmalloc (len);
1322 if (lseek (fd, offset, SEEK_SET) != offset
1323 || read (fd, result, len) != (ssize_t) len)
1324 {
1325 free (result);
1326 fatal_error ("Cannot read %s", file_data->file_name);
1327 result = NULL;
1328 }
1329 #ifdef __MINGW32__
1330 /* Native windows doesn't supports delayed unlink on opened file. So
1331 we close file here again. This produces higher I/O load, but at least
1332 it prevents to have dangling file handles preventing unlink. */
1333 free (fd_name);
1334 fd_name = NULL;
1335 close (fd);
1336 fd = -1;
1337 #endif
1338 return result;
1339 #endif
1340 }
1341
1342
1343 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1344 NAME will be NULL unless the section type is for a function
1345 body. */
1346
1347 static const char *
1348 get_section_data (struct lto_file_decl_data *file_data,
1349 enum lto_section_type section_type,
1350 const char *name,
1351 size_t *len)
1352 {
1353 htab_t section_hash_table = file_data->section_hash_table;
1354 struct lto_section_slot *f_slot;
1355 struct lto_section_slot s_slot;
1356 const char *section_name = lto_get_section_name (section_type, name, file_data);
1357 char *data = NULL;
1358
1359 *len = 0;
1360 s_slot.name = section_name;
1361 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
1362 if (f_slot)
1363 {
1364 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
1365 *len = f_slot->len;
1366 }
1367
1368 free (CONST_CAST (char *, section_name));
1369 return data;
1370 }
1371
1372
1373 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1374 starts at OFFSET and has LEN bytes. */
1375
1376 static void
1377 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
1378 enum lto_section_type section_type ATTRIBUTE_UNUSED,
1379 const char *name ATTRIBUTE_UNUSED,
1380 const char *offset, size_t len ATTRIBUTE_UNUSED)
1381 {
1382 #if LTO_MMAP_IO
1383 intptr_t computed_len;
1384 intptr_t computed_offset;
1385 intptr_t diff;
1386 #endif
1387
1388 #if LTO_MMAP_IO
1389 computed_offset = ((intptr_t) offset) & page_mask;
1390 diff = (intptr_t) offset - computed_offset;
1391 computed_len = len + diff;
1392
1393 munmap ((caddr_t) computed_offset, computed_len);
1394 #else
1395 free (CONST_CAST(char *, offset));
1396 #endif
1397 }
1398
1399 static lto_file *current_lto_file;
1400
1401 /* Helper for qsort; compare partitions and return one with smaller size.
1402 We sort from greatest to smallest so parallel build doesn't stale on the
1403 longest compilation being executed too late. */
1404
1405 static int
1406 cmp_partitions_size (const void *a, const void *b)
1407 {
1408 const struct ltrans_partition_def *pa
1409 = *(struct ltrans_partition_def *const *)a;
1410 const struct ltrans_partition_def *pb
1411 = *(struct ltrans_partition_def *const *)b;
1412 return pb->insns - pa->insns;
1413 }
1414
1415 /* Helper for qsort; compare partitions and return one with smaller order. */
1416
1417 static int
1418 cmp_partitions_order (const void *a, const void *b)
1419 {
1420 const struct ltrans_partition_def *pa
1421 = *(struct ltrans_partition_def *const *)a;
1422 const struct ltrans_partition_def *pb
1423 = *(struct ltrans_partition_def *const *)b;
1424 int ordera = -1, orderb = -1;
1425
1426 if (VEC_length (cgraph_node_ptr, pa->cgraph_set->nodes))
1427 ordera = VEC_index (cgraph_node_ptr, pa->cgraph_set->nodes, 0)->symbol.order;
1428 else if (VEC_length (varpool_node_ptr, pa->varpool_set->nodes))
1429 ordera = VEC_index (varpool_node_ptr, pa->varpool_set->nodes, 0)->symbol.order;
1430 if (VEC_length (cgraph_node_ptr, pb->cgraph_set->nodes))
1431 orderb = VEC_index (cgraph_node_ptr, pb->cgraph_set->nodes, 0)->symbol.order;
1432 else if (VEC_length (varpool_node_ptr, pb->varpool_set->nodes))
1433 orderb = VEC_index (varpool_node_ptr, pb->varpool_set->nodes, 0)->symbol.order;
1434 return orderb - ordera;
1435 }
1436
1437 /* Write all output files in WPA mode and the file with the list of
1438 LTRANS units. */
1439
1440 static void
1441 lto_wpa_write_files (void)
1442 {
1443 unsigned i, n_sets;
1444 lto_file *file;
1445 cgraph_node_set set;
1446 varpool_node_set vset;
1447 ltrans_partition part;
1448 FILE *ltrans_output_list_stream;
1449 char *temp_filename;
1450 size_t blen;
1451
1452 /* Open the LTRANS output list. */
1453 if (!ltrans_output_list)
1454 fatal_error ("no LTRANS output list filename provided");
1455 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
1456 if (ltrans_output_list_stream == NULL)
1457 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
1458
1459 timevar_push (TV_WHOPR_WPA);
1460
1461 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
1462 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
1463 part->cgraph_set->nodes);
1464
1465 /* Find out statics that need to be promoted
1466 to globals with hidden visibility because they are accessed from multiple
1467 partitions. */
1468 lto_promote_cross_file_statics ();
1469
1470 timevar_pop (TV_WHOPR_WPA);
1471
1472 timevar_push (TV_WHOPR_WPA_IO);
1473
1474 /* Generate a prefix for the LTRANS unit files. */
1475 blen = strlen (ltrans_output_list);
1476 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
1477 strcpy (temp_filename, ltrans_output_list);
1478 if (blen > sizeof (".out")
1479 && strcmp (temp_filename + blen - sizeof (".out") + 1,
1480 ".out") == 0)
1481 temp_filename[blen - sizeof (".out") + 1] = '\0';
1482 blen = strlen (temp_filename);
1483
1484 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
1485
1486 /* Sort partitions by size so small ones are compiled last.
1487 FIXME: Even when not reordering we may want to output one list for parallel make
1488 and other for final link command. */
1489 VEC_qsort (ltrans_partition, ltrans_partitions,
1490 flag_toplevel_reorder ? cmp_partitions_size : cmp_partitions_order);
1491 for (i = 0; i < n_sets; i++)
1492 {
1493 size_t len;
1494 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
1495
1496 set = part->cgraph_set;
1497 vset = part->varpool_set;
1498
1499 /* Write all the nodes in SET. */
1500 sprintf (temp_filename + blen, "%u.o", i);
1501 file = lto_obj_file_open (temp_filename, true);
1502 if (!file)
1503 fatal_error ("lto_obj_file_open() failed");
1504
1505 if (!quiet_flag)
1506 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
1507 if (cgraph_dump_file)
1508 {
1509 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
1510 part->name, temp_filename, part->insns);
1511 fprintf (cgraph_dump_file, "cgraph nodes:");
1512 dump_cgraph_node_set (cgraph_dump_file, set);
1513 fprintf (cgraph_dump_file, "varpool nodes:");
1514 dump_varpool_node_set (cgraph_dump_file, vset);
1515 }
1516 gcc_checking_assert (cgraph_node_set_nonempty_p (set)
1517 || varpool_node_set_nonempty_p (vset) || !i);
1518
1519 lto_set_current_out_file (file);
1520
1521 ipa_write_optimization_summaries (set, vset);
1522
1523 lto_set_current_out_file (NULL);
1524 lto_obj_file_close (file);
1525
1526 len = strlen (temp_filename);
1527 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
1528 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1529 fatal_error ("writing to LTRANS output list %s: %m",
1530 ltrans_output_list);
1531 }
1532
1533 lto_stats.num_output_files += n_sets;
1534
1535 /* Close the LTRANS output list. */
1536 if (fclose (ltrans_output_list_stream))
1537 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
1538
1539 free_ltrans_partitions();
1540
1541 timevar_pop (TV_WHOPR_WPA_IO);
1542 }
1543
1544
1545 /* If TT is a variable or function decl replace it with its
1546 prevailing variant. */
1547 #define LTO_SET_PREVAIL(tt) \
1548 do {\
1549 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
1550 tt = lto_symtab_prevailing_decl (tt); \
1551 } while (0)
1552
1553 /* Ensure that TT isn't a replacable var of function decl. */
1554 #define LTO_NO_PREVAIL(tt) \
1555 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
1556
1557 /* Given a tree T replace all fields referring to variables or functions
1558 with their prevailing variant. */
1559 static void
1560 lto_fixup_prevailing_decls (tree t)
1561 {
1562 enum tree_code code = TREE_CODE (t);
1563 LTO_NO_PREVAIL (TREE_TYPE (t));
1564 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1565 LTO_NO_PREVAIL (TREE_CHAIN (t));
1566 if (DECL_P (t))
1567 {
1568 LTO_NO_PREVAIL (DECL_NAME (t));
1569 LTO_SET_PREVAIL (DECL_CONTEXT (t));
1570 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1571 {
1572 LTO_SET_PREVAIL (DECL_SIZE (t));
1573 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
1574 LTO_SET_PREVAIL (DECL_INITIAL (t));
1575 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
1576 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
1577 }
1578 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1579 {
1580 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
1581 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
1582 }
1583 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1584 {
1585 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
1586 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
1587 LTO_NO_PREVAIL (DECL_VINDEX (t));
1588 }
1589 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1590 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
1591 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1592 {
1593 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
1594 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
1595 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
1596 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
1597 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
1598 }
1599 }
1600 else if (TYPE_P (t))
1601 {
1602 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
1603 LTO_SET_PREVAIL (TYPE_SIZE (t));
1604 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
1605 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
1606 LTO_NO_PREVAIL (TYPE_NAME (t));
1607
1608 LTO_SET_PREVAIL (TYPE_MINVAL (t));
1609 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
1610 LTO_SET_PREVAIL (t->type_non_common.binfo);
1611
1612 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
1613
1614 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
1615 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
1616 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
1617 }
1618 else if (EXPR_P (t))
1619 {
1620 int i;
1621 LTO_NO_PREVAIL (t->exp.block);
1622 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
1623 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
1624 }
1625 else
1626 {
1627 switch (code)
1628 {
1629 case TREE_LIST:
1630 LTO_SET_PREVAIL (TREE_VALUE (t));
1631 LTO_SET_PREVAIL (TREE_PURPOSE (t));
1632 break;
1633 default:
1634 gcc_unreachable ();
1635 }
1636 }
1637 }
1638 #undef LTO_SET_PREVAIL
1639 #undef LTO_NO_PREVAIL
1640
1641 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1642 replaces var and function decls with the corresponding prevailing def. */
1643
1644 static void
1645 lto_fixup_state (struct lto_in_decl_state *state)
1646 {
1647 unsigned i, si;
1648 struct lto_tree_ref_table *table;
1649
1650 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1651 we still need to walk from all DECLs to find the reachable
1652 FUNCTION_DECLs and VAR_DECLs. */
1653 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1654 {
1655 table = &state->streams[si];
1656 for (i = 0; i < table->size; i++)
1657 {
1658 tree *tp = table->trees + i;
1659 if (VAR_OR_FUNCTION_DECL_P (*tp))
1660 *tp = lto_symtab_prevailing_decl (*tp);
1661 }
1662 }
1663 }
1664
1665 /* A callback of htab_traverse. Just extracts a state from SLOT
1666 and calls lto_fixup_state. */
1667
1668 static int
1669 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
1670 {
1671 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1672 lto_fixup_state (state);
1673 return 1;
1674 }
1675
1676 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1677 prevailing one. */
1678
1679 static void
1680 lto_fixup_decls (struct lto_file_decl_data **files)
1681 {
1682 unsigned int i;
1683 htab_iterator hi;
1684 tree t;
1685
1686 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
1687 lto_fixup_prevailing_decls (t);
1688
1689 for (i = 0; files[i]; i++)
1690 {
1691 struct lto_file_decl_data *file = files[i];
1692 struct lto_in_decl_state *state = file->global_decl_state;
1693 lto_fixup_state (state);
1694
1695 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
1696 }
1697 }
1698
1699 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1700
1701 /* Turn file datas for sub files into a single array, so that they look
1702 like separate files for further passes. */
1703
1704 static void
1705 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
1706 {
1707 struct lto_file_decl_data *n, *next;
1708 int i, k;
1709
1710 lto_stats.num_input_files = count;
1711 all_file_decl_data
1712 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
1713 /* Set the hooks so that all of the ipa passes can read in their data. */
1714 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1715 for (i = 0, k = 0; i < last_file_ix; i++)
1716 {
1717 for (n = orig[i]; n != NULL; n = next)
1718 {
1719 all_file_decl_data[k++] = n;
1720 next = n->next;
1721 n->next = NULL;
1722 }
1723 }
1724 all_file_decl_data[k] = NULL;
1725 gcc_assert (k == count);
1726 }
1727
1728 /* Input file data before flattening (i.e. splitting them to subfiles to support
1729 incremental linking. */
1730 static int real_file_count;
1731 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
1732
1733 /* Read all the symbols from the input files FNAMES. NFILES is the
1734 number of files requested in the command line. Instantiate a
1735 global call graph by aggregating all the sub-graphs found in each
1736 file. */
1737
1738 static void
1739 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1740 {
1741 unsigned int i, last_file_ix;
1742 FILE *resolution;
1743 struct cgraph_node *node;
1744 int count = 0;
1745 struct lto_file_decl_data **decl_data;
1746
1747 init_cgraph ();
1748
1749 timevar_push (TV_IPA_LTO_DECL_IN);
1750
1751 real_file_decl_data
1752 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
1753 real_file_count = nfiles;
1754
1755 /* Read the resolution file. */
1756 resolution = NULL;
1757 if (resolution_file_name)
1758 {
1759 int t;
1760 unsigned num_objects;
1761
1762 resolution = fopen (resolution_file_name, "r");
1763 if (resolution == NULL)
1764 fatal_error ("could not open symbol resolution file: %m");
1765
1766 t = fscanf (resolution, "%u", &num_objects);
1767 gcc_assert (t == 1);
1768
1769 /* True, since the plugin splits the archives. */
1770 gcc_assert (num_objects == nfiles);
1771 }
1772
1773 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
1774 NULL);
1775
1776 if (!quiet_flag)
1777 fprintf (stderr, "Reading object files:");
1778
1779 /* Read all of the object files specified on the command line. */
1780 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1781 {
1782 struct lto_file_decl_data *file_data = NULL;
1783 if (!quiet_flag)
1784 {
1785 fprintf (stderr, " %s", fnames[i]);
1786 fflush (stderr);
1787 }
1788
1789 current_lto_file = lto_obj_file_open (fnames[i], false);
1790 if (!current_lto_file)
1791 break;
1792
1793 file_data = lto_file_read (current_lto_file, resolution, &count);
1794 if (!file_data)
1795 {
1796 lto_obj_file_close (current_lto_file);
1797 current_lto_file = NULL;
1798 break;
1799 }
1800
1801 decl_data[last_file_ix++] = file_data;
1802
1803 lto_obj_file_close (current_lto_file);
1804 current_lto_file = NULL;
1805 ggc_collect ();
1806 }
1807
1808 lto_flatten_files (decl_data, count, last_file_ix);
1809 lto_stats.num_input_files = count;
1810 ggc_free(decl_data);
1811 real_file_decl_data = NULL;
1812
1813 if (resolution_file_name)
1814 fclose (resolution);
1815
1816 /* Set the hooks so that all of the ipa passes can read in their data. */
1817 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1818
1819 timevar_pop (TV_IPA_LTO_DECL_IN);
1820
1821 if (!quiet_flag)
1822 fprintf (stderr, "\nReading the callgraph\n");
1823
1824 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1825 /* Read the callgraph. */
1826 input_cgraph ();
1827 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1828
1829 if (!quiet_flag)
1830 fprintf (stderr, "Merging declarations\n");
1831
1832 timevar_push (TV_IPA_LTO_DECL_MERGE);
1833 /* Merge global decls. */
1834 lto_symtab_merge_decls ();
1835
1836 /* If there were errors during symbol merging bail out, we have no
1837 good way to recover here. */
1838 if (seen_error ())
1839 fatal_error ("errors during merging of translation units");
1840
1841 /* Fixup all decls and types and free the type hash tables. */
1842 lto_fixup_decls (all_file_decl_data);
1843 htab_delete (tree_with_vars);
1844 tree_with_vars = NULL;
1845 free_gimple_type_tables ();
1846 ggc_collect ();
1847
1848 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1849 /* Each pass will set the appropriate timer. */
1850
1851 if (!quiet_flag)
1852 fprintf (stderr, "Reading summaries\n");
1853
1854 /* Read the IPA summary data. */
1855 if (flag_ltrans)
1856 ipa_read_optimization_summaries ();
1857 else
1858 ipa_read_summaries ();
1859
1860 /* Finally merge the cgraph according to the decl merging decisions. */
1861 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1862 if (cgraph_dump_file)
1863 {
1864 fprintf (cgraph_dump_file, "Before merging:\n");
1865 dump_cgraph (cgraph_dump_file);
1866 dump_varpool (cgraph_dump_file);
1867 }
1868 lto_symtab_merge_cgraph_nodes ();
1869 ggc_collect ();
1870
1871 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1872 summaries computed and needs to apply changes. At the moment WHOPR only
1873 supports inlining, so we can push it here by hand. In future we need to stream
1874 this field into ltrans compilation. */
1875 if (flag_ltrans)
1876 FOR_EACH_DEFINED_FUNCTION (node)
1877 VEC_safe_push (ipa_opt_pass, heap,
1878 node->ipa_transforms_to_apply,
1879 (ipa_opt_pass)&pass_ipa_inline);
1880 lto_symtab_free ();
1881
1882 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1883
1884 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1885
1886 /* FIXME lto. This loop needs to be changed to use the pass manager to
1887 call the ipa passes directly. */
1888 if (!seen_error ())
1889 for (i = 0; i < last_file_ix; i++)
1890 {
1891 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1892 lto_materialize_constructors_and_inits (file_data);
1893 }
1894
1895 /* Indicate that the cgraph is built and ready. */
1896 cgraph_function_flags_ready = true;
1897
1898 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1899 ggc_free (all_file_decl_data);
1900 all_file_decl_data = NULL;
1901 }
1902
1903
1904 /* Materialize all the bodies for all the nodes in the callgraph. */
1905
1906 static void
1907 materialize_cgraph (void)
1908 {
1909 tree decl;
1910 struct cgraph_node *node;
1911 unsigned i;
1912 timevar_id_t lto_timer;
1913
1914 if (!quiet_flag)
1915 fprintf (stderr,
1916 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1917
1918
1919 /* Now that we have input the cgraph, we need to clear all of the aux
1920 nodes and read the functions if we are not running in WPA mode. */
1921 timevar_push (TV_IPA_LTO_GIMPLE_IN);
1922
1923 FOR_EACH_FUNCTION (node)
1924 {
1925 if (node->symbol.lto_file_data)
1926 {
1927 lto_materialize_function (node);
1928 lto_stats.num_input_cgraph_nodes++;
1929 }
1930 }
1931
1932 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
1933
1934 /* Start the appropriate timer depending on the mode that we are
1935 operating in. */
1936 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1937 : (flag_ltrans) ? TV_WHOPR_LTRANS
1938 : TV_LTO;
1939 timevar_push (lto_timer);
1940
1941 current_function_decl = NULL;
1942 set_cfun (NULL);
1943
1944 /* Inform the middle end about the global variables we have seen. */
1945 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
1946 rest_of_decl_compilation (decl, 1, 0);
1947
1948 if (!quiet_flag)
1949 fprintf (stderr, "\n");
1950
1951 timevar_pop (lto_timer);
1952 }
1953
1954
1955 /* Perform whole program analysis (WPA) on the callgraph and write out the
1956 optimization plan. */
1957
1958 static void
1959 do_whole_program_analysis (void)
1960 {
1961 timevar_start (TV_PHASE_CGRAPH);
1962 /* Note that since we are in WPA mode, materialize_cgraph will not
1963 actually read in all the function bodies. It only materializes
1964 the decls and cgraph nodes so that analysis can be performed. */
1965 materialize_cgraph ();
1966
1967 /* Reading in the cgraph uses different timers, start timing WPA now. */
1968 timevar_push (TV_WHOPR_WPA);
1969
1970 if (pre_ipa_mem_report)
1971 {
1972 fprintf (stderr, "Memory consumption before IPA\n");
1973 dump_memory_report (false);
1974 }
1975
1976 cgraph_function_flags_ready = true;
1977
1978 if (cgraph_dump_file)
1979 {
1980 dump_cgraph (cgraph_dump_file);
1981 dump_varpool (cgraph_dump_file);
1982 }
1983 bitmap_obstack_initialize (NULL);
1984 cgraph_state = CGRAPH_STATE_IPA_SSA;
1985
1986 execute_ipa_pass_list (all_regular_ipa_passes);
1987
1988 if (cgraph_dump_file)
1989 {
1990 fprintf (cgraph_dump_file, "Optimized ");
1991 dump_cgraph (cgraph_dump_file);
1992 dump_varpool (cgraph_dump_file);
1993 }
1994 verify_cgraph ();
1995 bitmap_obstack_release (NULL);
1996
1997 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1998 timevar_pop (TV_WHOPR_WPA);
1999
2000 if (flag_lto_partition_1to1)
2001 lto_1_to_1_map ();
2002 else
2003 lto_balanced_map ();
2004
2005 if (!quiet_flag)
2006 {
2007 fprintf (stderr, "\nStreaming out");
2008 fflush (stderr);
2009 }
2010 lto_wpa_write_files ();
2011 ggc_collect ();
2012 if (!quiet_flag)
2013 fprintf (stderr, "\n");
2014
2015 if (post_ipa_mem_report)
2016 {
2017 fprintf (stderr, "Memory consumption after IPA\n");
2018 dump_memory_report (false);
2019 }
2020
2021 timevar_stop (TV_PHASE_CGRAPH);
2022 /* Show the LTO report before launching LTRANS. */
2023 if (flag_lto_report)
2024 print_lto_report ();
2025 }
2026
2027
2028 static GTY(()) tree lto_eh_personality_decl;
2029
2030 /* Return the LTO personality function decl. */
2031
2032 tree
2033 lto_eh_personality (void)
2034 {
2035 if (!lto_eh_personality_decl)
2036 {
2037 /* Use the first personality DECL for our personality if we don't
2038 support multiple ones. This ensures that we don't artificially
2039 create the need for them in a single-language program. */
2040 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2041 lto_eh_personality_decl = first_personality_decl;
2042 else
2043 lto_eh_personality_decl = lhd_gcc_personality ();
2044 }
2045
2046 return lto_eh_personality_decl;
2047 }
2048
2049 /* Set the process name based on the LTO mode. */
2050
2051 static void
2052 lto_process_name (void)
2053 {
2054 if (flag_lto)
2055 setproctitle ("lto1-lto");
2056 if (flag_wpa)
2057 setproctitle ("lto1-wpa");
2058 if (flag_ltrans)
2059 setproctitle ("lto1-ltrans");
2060 }
2061
2062
2063 /* Initialize the LTO front end. */
2064
2065 static void
2066 lto_init (void)
2067 {
2068 lto_process_name ();
2069 lto_streamer_hooks_init ();
2070 lto_reader_init ();
2071 lto_set_in_hooks (NULL, get_section_data, free_section_data);
2072 memset (&lto_stats, 0, sizeof (lto_stats));
2073 bitmap_obstack_initialize (NULL);
2074 gimple_register_cfg_hooks ();
2075 }
2076
2077
2078 /* Main entry point for the GIMPLE front end. This front end has
2079 three main personalities:
2080
2081 - LTO (-flto). All the object files on the command line are
2082 loaded in memory and processed as a single translation unit.
2083 This is the traditional link-time optimization behavior.
2084
2085 - WPA (-fwpa). Only the callgraph and summary information for
2086 files in the command file are loaded. A single callgraph
2087 (without function bodies) is instantiated for the whole set of
2088 files. IPA passes are only allowed to analyze the call graph
2089 and make transformation decisions. The callgraph is
2090 partitioned, each partition is written to a new object file
2091 together with the transformation decisions.
2092
2093 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2094 summary files from running again. Since WPA computed summary
2095 information and decided what transformations to apply, LTRANS
2096 simply applies them. */
2097
2098 void
2099 lto_main (void)
2100 {
2101 /* Initialize the LTO front end. */
2102 lto_init ();
2103
2104 /* Read all the symbols and call graph from all the files in the
2105 command line. */
2106 read_cgraph_and_symbols (num_in_fnames, in_fnames);
2107
2108 if (!seen_error ())
2109 {
2110 /* If WPA is enabled analyze the whole call graph and create an
2111 optimization plan. Otherwise, read in all the function
2112 bodies and continue with optimization. */
2113 if (flag_wpa)
2114 do_whole_program_analysis ();
2115 else
2116 {
2117 materialize_cgraph ();
2118
2119 /* Let the middle end know that we have read and merged all of
2120 the input files. */
2121 timevar_start (TV_PHASE_CGRAPH);
2122 compile ();
2123 timevar_stop (TV_PHASE_CGRAPH);
2124
2125 /* FIXME lto, if the processes spawned by WPA fail, we miss
2126 the chance to print WPA's report, so WPA will call
2127 print_lto_report before launching LTRANS. If LTRANS was
2128 launched directly by the driver we would not need to do
2129 this. */
2130 if (flag_lto_report)
2131 print_lto_report ();
2132 }
2133 }
2134 }
2135
2136 #include "gt-lto-lto.h"