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