PR fortran/38718
[official-gcc.git] / gcc / lto / lto.c
blob3b35604af8d64a7ff0fa8878c822362a8c1650a9
1 /* Top-level LTO routines.
2 Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "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 "params.h"
49 #include "ipa-inline.h"
50 #include "ipa-utils.h"
52 static GTY(()) tree first_personality_decl;
54 /* Returns a hash code for P. */
56 static hashval_t
57 hash_name (const void *p)
59 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
60 return (hashval_t) htab_hash_string (ds->name);
64 /* Returns nonzero if P1 and P2 are equal. */
66 static int
67 eq_name (const void *p1, const void *p2)
69 const struct lto_section_slot *s1 =
70 (const struct lto_section_slot *) p1;
71 const struct lto_section_slot *s2 =
72 (const struct lto_section_slot *) p2;
74 return strcmp (s1->name, s2->name) == 0;
77 /* Free lto_section_slot */
79 static void
80 free_with_string (void *arg)
82 struct lto_section_slot *s = (struct lto_section_slot *)arg;
84 free (CONST_CAST (char *, s->name));
85 free (arg);
88 /* Create section hash table */
90 htab_t
91 lto_obj_create_section_hash_table (void)
93 return htab_create (37, hash_name, eq_name, free_with_string);
96 /* Delete an allocated integer KEY in the splay tree. */
98 static void
99 lto_splay_tree_delete_id (splay_tree_key key)
101 free ((void *) key);
104 /* Compare splay tree node ids A and B. */
106 static int
107 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
109 unsigned HOST_WIDE_INT ai;
110 unsigned HOST_WIDE_INT bi;
112 ai = *(unsigned HOST_WIDE_INT *) a;
113 bi = *(unsigned HOST_WIDE_INT *) b;
115 if (ai < bi)
116 return -1;
117 else if (ai > bi)
118 return 1;
119 return 0;
122 /* Look up splay tree node by ID in splay tree T. */
124 static splay_tree_node
125 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
127 return splay_tree_lookup (t, (splay_tree_key) &id);
130 /* Check if KEY has ID. */
132 static bool
133 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
135 return *(unsigned HOST_WIDE_INT *) key == id;
138 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
139 The ID is allocated separately because we need HOST_WIDE_INTs which may
140 be wider than a splay_tree_key. */
142 static void
143 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
144 struct lto_file_decl_data *file_data)
146 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
147 *idp = id;
148 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
151 /* Create a splay tree. */
153 static splay_tree
154 lto_splay_tree_new (void)
156 return splay_tree_new (lto_splay_tree_compare_ids,
157 lto_splay_tree_delete_id,
158 NULL);
161 /* Read the constructors and inits. */
163 static void
164 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
166 size_t len;
167 const char *data = lto_get_section_data (file_data,
168 LTO_section_static_initializer,
169 NULL, &len);
170 lto_input_constructors_and_inits (file_data, data);
171 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
172 data, len);
175 /* Return true when NODE has a clone that is analyzed (i.e. we need
176 to load its body even if the node itself is not needed). */
178 static bool
179 has_analyzed_clone_p (struct cgraph_node *node)
181 struct cgraph_node *orig = node;
182 node = node->clones;
183 if (node)
184 while (node != orig)
186 if (node->analyzed)
187 return true;
188 if (node->clones)
189 node = node->clones;
190 else if (node->next_sibling_clone)
191 node = node->next_sibling_clone;
192 else
194 while (node != orig && !node->next_sibling_clone)
195 node = node->clone_of;
196 if (node != orig)
197 node = node->next_sibling_clone;
200 return false;
203 /* Read the function body for the function associated with NODE. */
205 static void
206 lto_materialize_function (struct cgraph_node *node)
208 tree decl;
209 struct lto_file_decl_data *file_data;
210 const char *data, *name;
211 size_t len;
213 decl = node->decl;
214 /* Read in functions with body (analyzed nodes)
215 and also functions that are needed to produce virtual clones. */
216 if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node))
218 /* Clones and thunks don't need to be read. */
219 if (node->clone_of)
220 return;
222 /* Load the function body only if not operating in WPA mode. In
223 WPA mode, the body of the function is not needed. */
224 if (!flag_wpa)
226 file_data = node->local.lto_file_data;
227 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
229 /* We may have renamed the declaration, e.g., a static function. */
230 name = lto_get_decl_name_mapping (file_data, name);
232 data = lto_get_section_data (file_data, LTO_section_function_body,
233 name, &len);
234 if (!data)
235 fatal_error ("%s: section %s is missing",
236 file_data->file_name,
237 name);
239 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
241 allocate_struct_function (decl, false);
242 announce_function (decl);
243 lto_input_function_body (file_data, decl, data);
244 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
245 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
246 lto_stats.num_function_bodies++;
247 lto_free_section_data (file_data, LTO_section_function_body, name,
248 data, len);
249 ggc_collect ();
253 /* Let the middle end know about the function. */
254 rest_of_decl_compilation (decl, 1, 0);
258 /* Decode the content of memory pointed to by DATA in the in decl
259 state object STATE. DATA_IN points to a data_in structure for
260 decoding. Return the address after the decoded object in the
261 input. */
263 static const uint32_t *
264 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
265 struct lto_in_decl_state *state)
267 uint32_t ix;
268 tree decl;
269 uint32_t i, j;
271 ix = *data++;
272 decl = streamer_tree_cache_get (data_in->reader_cache, ix);
273 if (TREE_CODE (decl) != FUNCTION_DECL)
275 gcc_assert (decl == void_type_node);
276 decl = NULL_TREE;
278 state->fn_decl = decl;
280 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
282 uint32_t size = *data++;
283 tree *decls = ggc_alloc_vec_tree (size);
285 for (j = 0; j < size; j++)
286 decls[j] = streamer_tree_cache_get (data_in->reader_cache, data[j]);
288 state->streams[i].size = size;
289 state->streams[i].trees = decls;
290 data += size;
293 return data;
296 /* A hashtable of trees that potentially refer to variables or functions
297 that must be replaced with their prevailing variant. */
298 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
299 tree_with_vars;
301 /* Remember that T is a tree that (potentially) refers to a variable
302 or function decl that may be replaced with its prevailing variant. */
303 static void
304 remember_with_vars (tree t)
306 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
309 #define LTO_FIXUP_TREE(tt) \
310 do \
312 if (tt) \
314 if (TYPE_P (tt)) \
315 (tt) = gimple_register_type (tt); \
316 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
317 remember_with_vars (t); \
319 } while (0)
321 static void lto_fixup_types (tree);
323 /* Fix up fields of a tree_typed T. */
325 static void
326 lto_ft_typed (tree t)
328 LTO_FIXUP_TREE (TREE_TYPE (t));
331 /* Fix up fields of a tree_common T. */
333 static void
334 lto_ft_common (tree t)
336 lto_ft_typed (t);
337 LTO_FIXUP_TREE (TREE_CHAIN (t));
340 /* Fix up fields of a decl_minimal T. */
342 static void
343 lto_ft_decl_minimal (tree t)
345 lto_ft_common (t);
346 LTO_FIXUP_TREE (DECL_NAME (t));
347 LTO_FIXUP_TREE (DECL_CONTEXT (t));
350 /* Fix up fields of a decl_common T. */
352 static void
353 lto_ft_decl_common (tree t)
355 lto_ft_decl_minimal (t);
356 LTO_FIXUP_TREE (DECL_SIZE (t));
357 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t));
358 LTO_FIXUP_TREE (DECL_INITIAL (t));
359 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t));
360 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t));
363 /* Fix up fields of a decl_with_vis T. */
365 static void
366 lto_ft_decl_with_vis (tree t)
368 lto_ft_decl_common (t);
370 /* Accessor macro has side-effects, use field-name here. */
371 LTO_FIXUP_TREE (t->decl_with_vis.assembler_name);
372 LTO_FIXUP_TREE (DECL_SECTION_NAME (t));
375 /* Fix up fields of a decl_non_common T. */
377 static void
378 lto_ft_decl_non_common (tree t)
380 lto_ft_decl_with_vis (t);
381 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t));
382 LTO_FIXUP_TREE (DECL_RESULT_FLD (t));
383 LTO_FIXUP_TREE (DECL_VINDEX (t));
386 /* Fix up fields of a decl_non_common T. */
388 static void
389 lto_ft_function (tree t)
391 lto_ft_decl_non_common (t);
392 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t));
395 /* Fix up fields of a field_decl T. */
397 static void
398 lto_ft_field_decl (tree t)
400 lto_ft_decl_common (t);
401 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t));
402 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t));
403 LTO_FIXUP_TREE (DECL_QUALIFIER (t));
404 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t));
405 LTO_FIXUP_TREE (DECL_FCONTEXT (t));
408 /* Fix up fields of a type T. */
410 static void
411 lto_ft_type (tree t)
413 lto_ft_common (t);
414 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t));
415 LTO_FIXUP_TREE (TYPE_SIZE (t));
416 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t));
417 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t));
418 LTO_FIXUP_TREE (TYPE_NAME (t));
420 /* Accessors are for derived node types only. */
421 if (!POINTER_TYPE_P (t))
422 LTO_FIXUP_TREE (TYPE_MINVAL (t));
423 LTO_FIXUP_TREE (TYPE_MAXVAL (t));
425 /* Accessor is for derived node types only. */
426 LTO_FIXUP_TREE (t->type_non_common.binfo);
428 LTO_FIXUP_TREE (TYPE_CONTEXT (t));
431 /* Fix up fields of a BINFO T. */
433 static void
434 lto_ft_binfo (tree t)
436 unsigned HOST_WIDE_INT i, n;
437 tree base, saved_base;
439 lto_ft_common (t);
440 LTO_FIXUP_TREE (BINFO_VTABLE (t));
441 LTO_FIXUP_TREE (BINFO_OFFSET (t));
442 LTO_FIXUP_TREE (BINFO_VIRTUALS (t));
443 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t));
444 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
445 for (i = 0; i < n; i++)
447 saved_base = base = BINFO_BASE_ACCESS (t, i);
448 LTO_FIXUP_TREE (base);
449 if (base != saved_base)
450 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
452 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t));
453 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t));
454 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t));
455 n = BINFO_N_BASE_BINFOS (t);
456 for (i = 0; i < n; i++)
458 saved_base = base = BINFO_BASE_BINFO (t, i);
459 LTO_FIXUP_TREE (base);
460 if (base != saved_base)
461 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
465 /* Fix up fields of a CONSTRUCTOR T. */
467 static void
468 lto_ft_constructor (tree t)
470 unsigned HOST_WIDE_INT idx;
471 constructor_elt *ce;
473 lto_ft_typed (t);
475 for (idx = 0;
476 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
477 idx++)
479 LTO_FIXUP_TREE (ce->index);
480 LTO_FIXUP_TREE (ce->value);
484 /* Fix up fields of an expression tree T. */
486 static void
487 lto_ft_expr (tree t)
489 int i;
490 lto_ft_typed (t);
491 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
492 LTO_FIXUP_TREE (TREE_OPERAND (t, i));
495 /* Given a tree T fixup fields of T by replacing types with their merged
496 variant and other entities by an equal entity from an earlier compilation
497 unit, or an entity being canonical in a different way. This includes
498 for instance integer or string constants. */
500 static void
501 lto_fixup_types (tree t)
503 switch (TREE_CODE (t))
505 case IDENTIFIER_NODE:
506 break;
508 case TREE_LIST:
509 LTO_FIXUP_TREE (TREE_VALUE (t));
510 LTO_FIXUP_TREE (TREE_PURPOSE (t));
511 LTO_FIXUP_TREE (TREE_CHAIN (t));
512 break;
514 case FIELD_DECL:
515 lto_ft_field_decl (t);
516 break;
518 case LABEL_DECL:
519 case CONST_DECL:
520 case PARM_DECL:
521 case RESULT_DECL:
522 case IMPORTED_DECL:
523 lto_ft_decl_common (t);
524 break;
526 case VAR_DECL:
527 lto_ft_decl_with_vis (t);
528 break;
530 case TYPE_DECL:
531 lto_ft_decl_non_common (t);
532 break;
534 case FUNCTION_DECL:
535 lto_ft_function (t);
536 break;
538 case TREE_BINFO:
539 lto_ft_binfo (t);
540 break;
542 case PLACEHOLDER_EXPR:
543 lto_ft_common (t);
544 break;
546 case BLOCK:
547 case TRANSLATION_UNIT_DECL:
548 case OPTIMIZATION_NODE:
549 case TARGET_OPTION_NODE:
550 break;
552 default:
553 if (TYPE_P (t))
554 lto_ft_type (t);
555 else if (TREE_CODE (t) == CONSTRUCTOR)
556 lto_ft_constructor (t);
557 else if (CONSTANT_CLASS_P (t))
558 LTO_FIXUP_TREE (TREE_TYPE (t));
559 else if (EXPR_P (t))
561 lto_ft_expr (t);
563 else
565 remember_with_vars (t);
571 /* Return the resolution for the decl with index INDEX from DATA_IN. */
573 static enum ld_plugin_symbol_resolution
574 get_resolution (struct data_in *data_in, unsigned index)
576 if (data_in->globals_resolution)
578 ld_plugin_symbol_resolution_t ret;
579 /* We can have references to not emitted functions in
580 DECL_FUNCTION_PERSONALITY at least. So we can and have
581 to indeed return LDPR_UNKNOWN in some cases. */
582 if (VEC_length (ld_plugin_symbol_resolution_t,
583 data_in->globals_resolution) <= index)
584 return LDPR_UNKNOWN;
585 ret = VEC_index (ld_plugin_symbol_resolution_t,
586 data_in->globals_resolution,
587 index);
588 return ret;
590 else
591 /* Delay resolution finding until decl merging. */
592 return LDPR_UNKNOWN;
596 /* Register DECL with the global symbol table and change its
597 name if necessary to avoid name clashes for static globals across
598 different files. */
600 static void
601 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl)
603 tree context;
605 /* Variable has file scope, not local. Need to ensure static variables
606 between different files don't clash unexpectedly. */
607 if (!TREE_PUBLIC (decl)
608 && !((context = decl_function_context (decl))
609 && auto_var_in_fn_p (decl, context)))
611 /* ??? We normally pre-mangle names before we serialize them
612 out. Here, in lto1, we do not know the language, and
613 thus cannot do the mangling again. Instead, we just
614 append a suffix to the mangled name. The resulting name,
615 however, is not a properly-formed mangled name, and will
616 confuse any attempt to unmangle it. */
617 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
618 char *label;
620 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
621 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
622 rest_of_decl_compilation (decl, 1, 0);
623 VEC_safe_push (tree, gc, lto_global_var_decls, decl);
626 /* If this variable has already been declared, queue the
627 declaration for merging. */
628 if (TREE_PUBLIC (decl))
630 unsigned ix;
631 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
632 gcc_unreachable ();
633 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
634 data_in->file_data);
639 /* Register DECL with the global symbol table and change its
640 name if necessary to avoid name clashes for static globals across
641 different files. DATA_IN contains descriptors and tables for the
642 file being read. */
644 static void
645 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl)
647 /* Need to ensure static entities between different files
648 don't clash unexpectedly. */
649 if (!TREE_PUBLIC (decl))
651 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
652 may set the assembler name where it was previously empty. */
653 tree old_assembler_name = decl->decl_with_vis.assembler_name;
655 /* FIXME lto: We normally pre-mangle names before we serialize
656 them out. Here, in lto1, we do not know the language, and
657 thus cannot do the mangling again. Instead, we just append a
658 suffix to the mangled name. The resulting name, however, is
659 not a properly-formed mangled name, and will confuse any
660 attempt to unmangle it. */
661 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
662 char *label;
664 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
665 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
667 /* We may arrive here with the old assembler name not set
668 if the function body is not needed, e.g., it has been
669 inlined away and does not appear in the cgraph. */
670 if (old_assembler_name)
672 tree new_assembler_name = DECL_ASSEMBLER_NAME (decl);
674 /* Make the original assembler name available for later use.
675 We may have used it to indicate the section within its
676 object file where the function body may be found.
677 FIXME lto: Find a better way to maintain the function decl
678 to body section mapping so we don't need this hack. */
679 lto_record_renamed_decl (data_in->file_data,
680 IDENTIFIER_POINTER (old_assembler_name),
681 IDENTIFIER_POINTER (new_assembler_name));
683 /* Also register the reverse mapping so that we can find the
684 new name given to an existing assembler name (used when
685 restoring alias pairs in input_constructors_or_inits. */
686 lto_record_renamed_decl (data_in->file_data,
687 IDENTIFIER_POINTER (new_assembler_name),
688 IDENTIFIER_POINTER (old_assembler_name));
692 /* If this variable has already been declared, queue the
693 declaration for merging. */
694 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
696 unsigned ix;
697 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
698 gcc_unreachable ();
699 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
700 data_in->file_data);
705 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
706 for one compilation unit) go over all trees starting at index FROM until the
707 end of the sequence and replace fields of those trees, and the trees
708 themself with their canonical variants as per gimple_register_type. */
710 static void
711 uniquify_nodes (struct data_in *data_in, unsigned from)
713 struct streamer_tree_cache_d *cache = data_in->reader_cache;
714 unsigned len = VEC_length (tree, cache->nodes);
715 unsigned i;
717 /* Go backwards because children streamed for the first time come
718 as part of their parents, and hence are created after them. */
720 /* First register all the types in the cache. This makes sure to
721 have the original structure in the type cycles when registering
722 them and computing hashes. */
723 for (i = len; i-- > from;)
725 tree t = VEC_index (tree, cache->nodes, i);
726 if (t && TYPE_P (t))
727 gimple_register_type (t);
730 /* Second fixup all trees in the new cache entries. */
731 for (i = len; i-- > from;)
733 tree t = VEC_index (tree, cache->nodes, i);
734 tree oldt = t;
735 if (!t)
736 continue;
738 /* First fixup the fields of T. */
739 lto_fixup_types (t);
741 if (!TYPE_P (t))
742 continue;
744 /* Now try to find a canonical variant of T itself. */
745 t = gimple_register_type (t);
747 if (t == oldt)
749 /* The following re-creates proper variant lists while fixing up
750 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
751 variant list state before fixup is broken. */
752 tree tem, mv;
754 /* Remove us from our main variant list if we are not the
755 variant leader. */
756 if (TYPE_MAIN_VARIANT (t) != t)
758 tem = TYPE_MAIN_VARIANT (t);
759 while (tem && TYPE_NEXT_VARIANT (tem) != t)
760 tem = TYPE_NEXT_VARIANT (tem);
761 if (tem)
762 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
763 TYPE_NEXT_VARIANT (t) = NULL_TREE;
766 /* Query our new main variant. */
767 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
769 /* If we were the variant leader and we get replaced ourselves drop
770 all variants from our list. */
771 if (TYPE_MAIN_VARIANT (t) == t
772 && mv != t)
774 tem = t;
775 while (tem)
777 tree tem2 = TYPE_NEXT_VARIANT (tem);
778 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
779 tem = tem2;
783 /* If we are not our own variant leader link us into our new leaders
784 variant list. */
785 if (mv != t)
787 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
788 TYPE_NEXT_VARIANT (mv) = t;
789 if (RECORD_OR_UNION_TYPE_P (t))
790 TYPE_BINFO (t) = TYPE_BINFO (mv);
793 /* Finally adjust our main variant and fix it up. */
794 TYPE_MAIN_VARIANT (t) = mv;
796 /* The following reconstructs the pointer chains
797 of the new pointed-to type if we are a main variant. We do
798 not stream those so they are broken before fixup. */
799 if (TREE_CODE (t) == POINTER_TYPE
800 && TYPE_MAIN_VARIANT (t) == t)
802 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
803 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
805 else if (TREE_CODE (t) == REFERENCE_TYPE
806 && TYPE_MAIN_VARIANT (t) == t)
808 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
809 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
813 else
815 if (RECORD_OR_UNION_TYPE_P (t))
817 tree f1, f2;
818 if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt))
819 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt);
820 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
822 unsigned ix;
823 gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2));
824 if (!streamer_tree_cache_lookup (cache, f2, &ix))
825 gcc_unreachable ();
826 /* If we're going to replace an element which we'd
827 still visit in the next iterations, we wouldn't
828 handle it, so do it here. We do have to handle it
829 even though the field_decl itself will be removed,
830 as it could refer to e.g. integer_cst which we
831 wouldn't reach via any other way, hence they
832 (and their type) would stay uncollected. */
833 /* ??? We should rather make sure to replace all
834 references to f2 with f1. That means handling
835 COMPONENT_REFs and CONSTRUCTOR elements in
836 lto_fixup_types and special-case the field-decl
837 operand handling. */
838 if (ix < i)
839 lto_fixup_types (f2);
840 streamer_tree_cache_insert_at (cache, f1, ix);
844 /* If we found a tree that is equal to oldt replace it in the
845 cache, so that further users (in the various LTO sections)
846 make use of it. */
847 streamer_tree_cache_insert_at (cache, t, i);
851 /* Finally compute the canonical type of all TREE_TYPEs and register
852 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
853 From this point there are no longer any types with
854 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
855 This step requires the TYPE_POINTER_TO lists being present, so
856 make sure it is done last. */
857 for (i = len; i-- > from;)
859 tree t = VEC_index (tree, cache->nodes, i);
860 if (t == NULL_TREE)
861 continue;
863 if (TREE_CODE (t) == VAR_DECL)
864 lto_register_var_decl_in_symtab (data_in, t);
865 else if (TREE_CODE (t) == FUNCTION_DECL && !DECL_BUILT_IN (t))
866 lto_register_function_decl_in_symtab (data_in, t);
867 else if (TYPE_P (t) && !TYPE_CANONICAL (t))
868 TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
873 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
874 RESOLUTIONS is the set of symbols picked by the linker (read from the
875 resolution file when the linker plugin is being used). */
877 static void
878 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
879 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
881 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
882 const int32_t decl_offset = sizeof (struct lto_decl_header);
883 const int32_t main_offset = decl_offset + header->decl_state_size;
884 const int32_t string_offset = main_offset + header->main_size;
885 struct lto_input_block ib_main;
886 struct data_in *data_in;
887 unsigned int i;
888 const uint32_t *data_ptr, *data_end;
889 uint32_t num_decl_states;
891 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
892 header->main_size);
894 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
895 header->string_size, resolutions);
897 /* Read the global declarations and types. */
898 while (ib_main.p < ib_main.len)
900 tree t;
901 unsigned from = VEC_length (tree, data_in->reader_cache->nodes);
902 t = stream_read_tree (&ib_main, data_in);
903 gcc_assert (t && ib_main.p <= ib_main.len);
904 uniquify_nodes (data_in, from);
907 /* Read in lto_in_decl_state objects. */
908 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
909 data_end =
910 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
911 num_decl_states = *data_ptr++;
913 gcc_assert (num_decl_states > 0);
914 decl_data->global_decl_state = lto_new_in_decl_state ();
915 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
916 decl_data->global_decl_state);
918 /* Read in per-function decl states and enter them in hash table. */
919 decl_data->function_decl_states =
920 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
922 for (i = 1; i < num_decl_states; i++)
924 struct lto_in_decl_state *state = lto_new_in_decl_state ();
925 void **slot;
927 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
928 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
929 gcc_assert (*slot == NULL);
930 *slot = state;
933 if (data_ptr != data_end)
934 internal_error ("bytecode stream: garbage at the end of symbols section");
936 /* Set the current decl state to be the global state. */
937 decl_data->current_decl_state = decl_data->global_decl_state;
939 lto_data_in_delete (data_in);
942 /* strtoll is not portable. */
943 int64_t
944 lto_parse_hex (const char *p) {
945 uint64_t ret = 0;
946 for (; *p != '\0'; ++p)
948 char c = *p;
949 unsigned char part;
950 ret <<= 4;
951 if (c >= '0' && c <= '9')
952 part = c - '0';
953 else if (c >= 'a' && c <= 'f')
954 part = c - 'a' + 10;
955 else if (c >= 'A' && c <= 'F')
956 part = c - 'A' + 10;
957 else
958 internal_error ("could not parse hex number");
959 ret |= part;
961 return ret;
964 /* Read resolution for file named FILE_NAME. The resolution is read from
965 RESOLUTION. */
967 static void
968 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
970 /* We require that objects in the resolution file are in the same
971 order as the lto1 command line. */
972 unsigned int name_len;
973 char *obj_name;
974 unsigned int num_symbols;
975 unsigned int i;
976 struct lto_file_decl_data *file_data;
977 unsigned max_index = 0;
978 splay_tree_node nd = NULL;
980 if (!resolution)
981 return;
983 name_len = strlen (file->filename);
984 obj_name = XNEWVEC (char, name_len + 1);
985 fscanf (resolution, " "); /* Read white space. */
987 fread (obj_name, sizeof (char), name_len, resolution);
988 obj_name[name_len] = '\0';
989 if (filename_cmp (obj_name, file->filename) != 0)
990 internal_error ("unexpected file name %s in linker resolution file. "
991 "Expected %s", obj_name, file->filename);
992 if (file->offset != 0)
994 int t;
995 char offset_p[17];
996 int64_t offset;
997 t = fscanf (resolution, "@0x%16s", offset_p);
998 if (t != 1)
999 internal_error ("could not parse file offset");
1000 offset = lto_parse_hex (offset_p);
1001 if (offset != file->offset)
1002 internal_error ("unexpected offset");
1005 free (obj_name);
1007 fscanf (resolution, "%u", &num_symbols);
1009 for (i = 0; i < num_symbols; i++)
1011 int t;
1012 unsigned index;
1013 unsigned HOST_WIDE_INT id;
1014 char r_str[27];
1015 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1016 unsigned int j;
1017 unsigned int lto_resolution_str_len =
1018 sizeof (lto_resolution_str) / sizeof (char *);
1020 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1021 &index, &id, r_str);
1022 if (t != 3)
1023 internal_error ("invalid line in the resolution file");
1024 if (index > max_index)
1025 max_index = index;
1027 for (j = 0; j < lto_resolution_str_len; j++)
1029 if (strcmp (lto_resolution_str[j], r_str) == 0)
1031 r = (enum ld_plugin_symbol_resolution) j;
1032 break;
1035 if (j == lto_resolution_str_len)
1036 internal_error ("invalid resolution in the resolution file");
1038 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1040 nd = lto_splay_tree_lookup (file_ids, id);
1041 if (nd == NULL)
1042 internal_error ("resolution sub id " HOST_WIDE_INT_PRINT_HEX_PURE
1043 " not in object file", id);
1046 file_data = (struct lto_file_decl_data *)nd->value;
1047 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap,
1048 file_data->resolutions,
1049 max_index + 1);
1050 VEC_replace (ld_plugin_symbol_resolution_t,
1051 file_data->resolutions, index, r);
1055 /* List of file_decl_datas */
1056 struct file_data_list
1058 struct lto_file_decl_data *first, *last;
1061 /* Is the name for a id'ed LTO section? */
1063 static int
1064 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1066 const char *s;
1068 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
1069 return 0;
1070 s = strrchr (name, '.');
1071 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1074 /* Create file_data of each sub file id */
1076 static int
1077 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1078 struct file_data_list *list)
1080 struct lto_section_slot s_slot, *new_slot;
1081 unsigned HOST_WIDE_INT id;
1082 splay_tree_node nd;
1083 void **hash_slot;
1084 char *new_name;
1085 struct lto_file_decl_data *file_data;
1087 if (!lto_section_with_id (ls->name, &id))
1088 return 1;
1090 /* Find hash table of sub module id */
1091 nd = lto_splay_tree_lookup (file_ids, id);
1092 if (nd != NULL)
1094 file_data = (struct lto_file_decl_data *)nd->value;
1096 else
1098 file_data = ggc_alloc_lto_file_decl_data ();
1099 memset(file_data, 0, sizeof (struct lto_file_decl_data));
1100 file_data->id = id;
1101 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1102 lto_splay_tree_insert (file_ids, id, file_data);
1104 /* Maintain list in linker order */
1105 if (!list->first)
1106 list->first = file_data;
1107 if (list->last)
1108 list->last->next = file_data;
1109 list->last = file_data;
1112 /* Copy section into sub module hash table */
1113 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
1114 s_slot.name = new_name;
1115 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
1116 gcc_assert (*hash_slot == NULL);
1118 new_slot = XDUP (struct lto_section_slot, ls);
1119 new_slot->name = new_name;
1120 *hash_slot = new_slot;
1121 return 1;
1124 /* Read declarations and other initializations for a FILE_DATA. */
1126 static void
1127 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
1129 const char *data;
1130 size_t len;
1132 file_data->renaming_hash_table = lto_create_renaming_table ();
1133 file_data->file_name = file->filename;
1134 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
1135 if (data == NULL)
1137 internal_error ("cannot read LTO decls from %s", file_data->file_name);
1138 return;
1140 lto_read_decls (file_data, data, file_data->resolutions);
1141 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
1144 /* Finalize FILE_DATA in FILE and increase COUNT. */
1146 static int
1147 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
1148 int *count)
1150 lto_file_finalize (file_data, file);
1151 if (cgraph_dump_file)
1152 fprintf (cgraph_dump_file, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
1153 file_data->file_name, file_data->id);
1154 (*count)++;
1155 return 0;
1158 /* Generate a TREE representation for all types and external decls
1159 entities in FILE.
1161 Read all of the globals out of the file. Then read the cgraph
1162 and process the .o index into the cgraph nodes so that it can open
1163 the .o file to load the functions and ipa information. */
1165 static struct lto_file_decl_data *
1166 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
1168 struct lto_file_decl_data *file_data = NULL;
1169 splay_tree file_ids;
1170 htab_t section_hash_table;
1171 struct lto_section_slot *section;
1172 struct file_data_list file_list;
1173 struct lto_section_list section_list;
1175 memset (&section_list, 0, sizeof (struct lto_section_list));
1176 section_hash_table = lto_obj_build_section_table (file, &section_list);
1178 /* Find all sub modules in the object and put their sections into new hash
1179 tables in a splay tree. */
1180 file_ids = lto_splay_tree_new ();
1181 memset (&file_list, 0, sizeof (struct file_data_list));
1182 for (section = section_list.first; section != NULL; section = section->next)
1183 create_subid_section_table (section, file_ids, &file_list);
1185 /* Add resolutions to file ids */
1186 lto_resolution_read (file_ids, resolution_file, file);
1188 /* Finalize each lto file for each submodule in the merged object */
1189 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
1190 lto_create_files_from_ids (file, file_data, count);
1192 splay_tree_delete (file_ids);
1193 htab_delete (section_hash_table);
1195 return file_list.first;
1198 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
1199 #define LTO_MMAP_IO 1
1200 #endif
1202 #if LTO_MMAP_IO
1203 /* Page size of machine is used for mmap and munmap calls. */
1204 static size_t page_mask;
1205 #endif
1207 /* Get the section data of length LEN from FILENAME starting at
1208 OFFSET. The data segment must be freed by the caller when the
1209 caller is finished. Returns NULL if all was not well. */
1211 static char *
1212 lto_read_section_data (struct lto_file_decl_data *file_data,
1213 intptr_t offset, size_t len)
1215 char *result;
1216 static int fd = -1;
1217 static char *fd_name;
1218 #if LTO_MMAP_IO
1219 intptr_t computed_len;
1220 intptr_t computed_offset;
1221 intptr_t diff;
1222 #endif
1224 /* Keep a single-entry file-descriptor cache. The last file we
1225 touched will get closed at exit.
1226 ??? Eventually we want to add a more sophisticated larger cache
1227 or rather fix function body streaming to not stream them in
1228 practically random order. */
1229 if (fd != -1
1230 && filename_cmp (fd_name, file_data->file_name) != 0)
1232 free (fd_name);
1233 close (fd);
1234 fd = -1;
1236 if (fd == -1)
1238 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
1239 if (fd == -1)
1241 fatal_error ("Cannot open %s", file_data->file_name);
1242 return NULL;
1244 fd_name = xstrdup (file_data->file_name);
1247 #if LTO_MMAP_IO
1248 if (!page_mask)
1250 size_t page_size = sysconf (_SC_PAGE_SIZE);
1251 page_mask = ~(page_size - 1);
1254 computed_offset = offset & page_mask;
1255 diff = offset - computed_offset;
1256 computed_len = len + diff;
1258 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
1259 fd, computed_offset);
1260 if (result == MAP_FAILED)
1262 fatal_error ("Cannot map %s", file_data->file_name);
1263 return NULL;
1266 return result + diff;
1267 #else
1268 result = (char *) xmalloc (len);
1269 if (lseek (fd, offset, SEEK_SET) != offset
1270 || read (fd, result, len) != (ssize_t) len)
1272 free (result);
1273 fatal_error ("Cannot read %s", file_data->file_name);
1274 result = NULL;
1276 #ifdef __MINGW32__
1277 /* Native windows doesn't supports delayed unlink on opened file. So
1278 we close file here again. This produces higher I/O load, but at least
1279 it prevents to have dangling file handles preventing unlink. */
1280 free (fd_name);
1281 fd_name = NULL;
1282 close (fd);
1283 fd = -1;
1284 #endif
1285 return result;
1286 #endif
1290 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1291 NAME will be NULL unless the section type is for a function
1292 body. */
1294 static const char *
1295 get_section_data (struct lto_file_decl_data *file_data,
1296 enum lto_section_type section_type,
1297 const char *name,
1298 size_t *len)
1300 htab_t section_hash_table = file_data->section_hash_table;
1301 struct lto_section_slot *f_slot;
1302 struct lto_section_slot s_slot;
1303 const char *section_name = lto_get_section_name (section_type, name, file_data);
1304 char *data = NULL;
1306 *len = 0;
1307 s_slot.name = section_name;
1308 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
1309 if (f_slot)
1311 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
1312 *len = f_slot->len;
1315 free (CONST_CAST (char *, section_name));
1316 return data;
1320 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1321 starts at OFFSET and has LEN bytes. */
1323 static void
1324 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
1325 enum lto_section_type section_type ATTRIBUTE_UNUSED,
1326 const char *name ATTRIBUTE_UNUSED,
1327 const char *offset, size_t len ATTRIBUTE_UNUSED)
1329 #if LTO_MMAP_IO
1330 intptr_t computed_len;
1331 intptr_t computed_offset;
1332 intptr_t diff;
1333 #endif
1335 #if LTO_MMAP_IO
1336 computed_offset = ((intptr_t) offset) & page_mask;
1337 diff = (intptr_t) offset - computed_offset;
1338 computed_len = len + diff;
1340 munmap ((caddr_t) computed_offset, computed_len);
1341 #else
1342 free (CONST_CAST(char *, offset));
1343 #endif
1346 /* Structure describing ltrans partitions. */
1348 struct ltrans_partition_def
1350 cgraph_node_set cgraph_set;
1351 varpool_node_set varpool_set;
1352 const char * name;
1353 int insns;
1356 typedef struct ltrans_partition_def *ltrans_partition;
1357 DEF_VEC_P(ltrans_partition);
1358 DEF_VEC_ALLOC_P(ltrans_partition,heap);
1360 static VEC(ltrans_partition, heap) *ltrans_partitions;
1362 static void add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node);
1363 static void add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode);
1365 /* Create new partition with name NAME. */
1366 static ltrans_partition
1367 new_partition (const char *name)
1369 ltrans_partition part = XCNEW (struct ltrans_partition_def);
1370 part->cgraph_set = cgraph_node_set_new ();
1371 part->varpool_set = varpool_node_set_new ();
1372 part->name = name;
1373 part->insns = 0;
1374 VEC_safe_push (ltrans_partition, heap, ltrans_partitions, part);
1375 return part;
1378 /* Free memory used by ltrans datastructures. */
1379 static void
1380 free_ltrans_partitions (void)
1382 unsigned int idx;
1383 ltrans_partition part;
1384 for (idx = 0; VEC_iterate (ltrans_partition, ltrans_partitions, idx, part); idx++)
1386 free_cgraph_node_set (part->cgraph_set);
1387 free (part);
1389 VEC_free (ltrans_partition, heap, ltrans_partitions);
1392 /* See all references that go to comdat objects and bring them into partition too. */
1393 static void
1394 add_references_to_partition (ltrans_partition part, struct ipa_ref_list *refs)
1396 int i;
1397 struct ipa_ref *ref;
1398 for (i = 0; ipa_ref_list_reference_iterate (refs, i, ref); i++)
1400 if (ref->refered_type == IPA_REF_CGRAPH
1401 && DECL_COMDAT (cgraph_function_node (ipa_ref_node (ref), NULL)->decl)
1402 && !cgraph_node_in_set_p (ipa_ref_node (ref), part->cgraph_set))
1403 add_cgraph_node_to_partition (part, ipa_ref_node (ref));
1404 else
1405 if (ref->refered_type == IPA_REF_VARPOOL
1406 && DECL_COMDAT (ipa_ref_varpool_node (ref)->decl)
1407 && !varpool_node_in_set_p (ipa_ref_varpool_node (ref), part->varpool_set))
1408 add_varpool_node_to_partition (part, ipa_ref_varpool_node (ref));
1412 /* Worker for add_cgraph_node_to_partition. */
1414 static bool
1415 add_cgraph_node_to_partition_1 (struct cgraph_node *node, void *data)
1417 ltrans_partition part = (ltrans_partition) data;
1419 /* non-COMDAT aliases of COMDAT functions needs to be output just once. */
1420 if (!DECL_COMDAT (node->decl)
1421 && !node->global.inlined_to
1422 && node->aux)
1424 gcc_assert (node->thunk.thunk_p || node->alias);
1425 return false;
1428 if (node->aux)
1430 node->in_other_partition = 1;
1431 if (cgraph_dump_file)
1432 fprintf (cgraph_dump_file, "Node %s/%i now used in multiple partitions\n",
1433 cgraph_node_name (node), node->uid);
1435 node->aux = (void *)((size_t)node->aux + 1);
1436 cgraph_node_set_add (part->cgraph_set, node);
1437 return false;
1440 /* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */
1442 static void
1443 add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node)
1445 struct cgraph_edge *e;
1446 cgraph_node_set_iterator csi;
1447 struct cgraph_node *n;
1449 /* We always decide on functions, not associated thunks and aliases. */
1450 node = cgraph_function_node (node, NULL);
1452 /* If NODE is already there, we have nothing to do. */
1453 csi = cgraph_node_set_find (part->cgraph_set, node);
1454 if (!csi_end_p (csi))
1455 return;
1457 cgraph_for_node_thunks_and_aliases (node, add_cgraph_node_to_partition_1, part, true);
1459 part->insns += inline_summary (node)->self_size;
1462 cgraph_node_set_add (part->cgraph_set, node);
1464 for (e = node->callees; e; e = e->next_callee)
1465 if ((!e->inline_failed
1466 || DECL_COMDAT (cgraph_function_node (e->callee, NULL)->decl))
1467 && !cgraph_node_in_set_p (e->callee, part->cgraph_set))
1468 add_cgraph_node_to_partition (part, e->callee);
1470 add_references_to_partition (part, &node->ref_list);
1472 if (node->same_comdat_group)
1473 for (n = node->same_comdat_group; n != node; n = n->same_comdat_group)
1474 add_cgraph_node_to_partition (part, n);
1477 /* Add VNODE to partition as well as comdat references partition PART. */
1479 static void
1480 add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode)
1482 varpool_node_set_iterator vsi;
1484 vnode = varpool_variable_node (vnode, NULL);
1486 /* If NODE is already there, we have nothing to do. */
1487 vsi = varpool_node_set_find (part->varpool_set, vnode);
1488 if (!vsi_end_p (vsi))
1489 return;
1491 varpool_node_set_add (part->varpool_set, vnode);
1493 if (vnode->aux)
1495 vnode->in_other_partition = 1;
1496 if (cgraph_dump_file)
1497 fprintf (cgraph_dump_file, "Varpool node %s now used in multiple partitions\n",
1498 varpool_node_name (vnode));
1500 vnode->aux = (void *)((size_t)vnode->aux + 1);
1502 add_references_to_partition (part, &vnode->ref_list);
1504 if (vnode->same_comdat_group
1505 && !varpool_node_in_set_p (vnode->same_comdat_group, part->varpool_set))
1506 add_varpool_node_to_partition (part, vnode->same_comdat_group);
1509 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
1510 and number of varpool nodes is N_VARPOOL_NODES. */
1512 static void
1513 undo_partition (ltrans_partition partition, unsigned int n_cgraph_nodes,
1514 unsigned int n_varpool_nodes)
1516 while (VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes) >
1517 n_cgraph_nodes)
1519 struct cgraph_node *node = VEC_index (cgraph_node_ptr,
1520 partition->cgraph_set->nodes,
1521 n_cgraph_nodes);
1522 partition->insns -= inline_summary (node)->self_size;
1523 cgraph_node_set_remove (partition->cgraph_set, node);
1524 node->aux = (void *)((size_t)node->aux - 1);
1526 while (VEC_length (varpool_node_ptr, partition->varpool_set->nodes) >
1527 n_varpool_nodes)
1529 struct varpool_node *node = VEC_index (varpool_node_ptr,
1530 partition->varpool_set->nodes,
1531 n_varpool_nodes);
1532 varpool_node_set_remove (partition->varpool_set, node);
1533 node->aux = (void *)((size_t)node->aux - 1);
1537 /* Return true if NODE should be partitioned.
1538 This means that partitioning algorithm should put NODE into one of partitions.
1539 This apply to most functions with bodies. Functions that are not partitions
1540 are put into every unit needing them. This is the case of i.e. COMDATs. */
1542 static bool
1543 partition_cgraph_node_p (struct cgraph_node *node)
1545 /* We will get proper partition based on function they are inlined to. */
1546 if (node->global.inlined_to)
1547 return false;
1548 /* Nodes without a body do not need partitioning. */
1549 if (!node->analyzed)
1550 return false;
1551 /* Extern inlines and comdat are always only in partitions they are needed. */
1552 if (DECL_EXTERNAL (node->decl)
1553 || (DECL_COMDAT (node->decl)
1554 && !cgraph_used_from_object_file_p (node)))
1555 return false;
1556 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
1557 return false;
1558 return true;
1561 /* Return true if VNODE should be partitioned.
1562 This means that partitioning algorithm should put VNODE into one of partitions. */
1564 static bool
1565 partition_varpool_node_p (struct varpool_node *vnode)
1567 if (vnode->alias || !vnode->needed)
1568 return false;
1569 /* Constant pool and comdat are always only in partitions they are needed. */
1570 if (DECL_IN_CONSTANT_POOL (vnode->decl)
1571 || (DECL_COMDAT (vnode->decl)
1572 && !vnode->force_output
1573 && !varpool_used_from_object_file_p (vnode)))
1574 return false;
1575 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->decl)))
1576 return false;
1577 return true;
1580 /* Group cgrah nodes by input files. This is used mainly for testing
1581 right now. */
1583 static void
1584 lto_1_to_1_map (void)
1586 struct cgraph_node *node;
1587 struct varpool_node *vnode;
1588 struct lto_file_decl_data *file_data;
1589 struct pointer_map_t *pmap;
1590 ltrans_partition partition;
1591 void **slot;
1592 int npartitions = 0;
1594 timevar_push (TV_WHOPR_WPA);
1596 pmap = pointer_map_create ();
1598 for (node = cgraph_nodes; node; node = node->next)
1600 if (!partition_cgraph_node_p (node)
1601 || node->aux)
1602 continue;
1604 file_data = node->local.lto_file_data;
1606 if (file_data)
1608 slot = pointer_map_contains (pmap, file_data);
1609 if (slot)
1610 partition = (ltrans_partition) *slot;
1611 else
1613 partition = new_partition (file_data->file_name);
1614 slot = pointer_map_insert (pmap, file_data);
1615 *slot = partition;
1616 npartitions++;
1619 else if (!file_data
1620 && VEC_length (ltrans_partition, ltrans_partitions))
1621 partition = VEC_index (ltrans_partition, ltrans_partitions, 0);
1622 else
1624 partition = new_partition ("");
1625 slot = pointer_map_insert (pmap, NULL);
1626 *slot = partition;
1627 npartitions++;
1630 add_cgraph_node_to_partition (partition, node);
1633 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1635 if (!partition_varpool_node_p (vnode)
1636 || vnode->aux)
1637 continue;
1638 file_data = vnode->lto_file_data;
1639 slot = pointer_map_contains (pmap, file_data);
1640 if (slot)
1641 partition = (ltrans_partition) *slot;
1642 else
1644 partition = new_partition (file_data->file_name);
1645 slot = pointer_map_insert (pmap, file_data);
1646 *slot = partition;
1647 npartitions++;
1650 add_varpool_node_to_partition (partition, vnode);
1652 for (node = cgraph_nodes; node; node = node->next)
1653 node->aux = NULL;
1654 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1655 vnode->aux = NULL;
1657 /* If the cgraph is empty, create one cgraph node set so that there is still
1658 an output file for any variables that need to be exported in a DSO. */
1659 if (!npartitions)
1660 new_partition ("empty");
1662 pointer_map_destroy (pmap);
1664 timevar_pop (TV_WHOPR_WPA);
1666 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
1667 ltrans_partitions);
1670 /* Helper function for qsort; sort nodes by order. */
1671 static int
1672 node_cmp (const void *pa, const void *pb)
1674 const struct cgraph_node *a = *(const struct cgraph_node * const *) pa;
1675 const struct cgraph_node *b = *(const struct cgraph_node * const *) pb;
1676 return b->order - a->order;
1679 /* Helper function for qsort; sort nodes by order. */
1680 static int
1681 varpool_node_cmp (const void *pa, const void *pb)
1683 const struct varpool_node *a = *(const struct varpool_node * const *) pa;
1684 const struct varpool_node *b = *(const struct varpool_node * const *) pb;
1685 return b->order - a->order;
1688 /* Group cgraph nodes into equally-sized partitions.
1690 The partitioning algorithm is simple: nodes are taken in predefined order.
1691 The order corresponds to the order we want functions to have in the final
1692 output. In the future this will be given by function reordering pass, but
1693 at the moment we use the topological order, which is a good approximation.
1695 The goal is to partition this linear order into intervals (partitions) so
1696 that all the partitions have approximately the same size and the number of
1697 callgraph or IPA reference edges crossing boundaries is minimal.
1699 This is a lot faster (O(n) in size of callgraph) than algorithms doing
1700 priority-based graph clustering that are generally O(n^2) and, since
1701 WHOPR is designed to make things go well across partitions, it leads
1702 to good results.
1704 We compute the expected size of a partition as:
1706 max (total_size / lto_partitions, min_partition_size)
1708 We use dynamic expected size of partition so small programs are partitioned
1709 into enough partitions to allow use of multiple CPUs, while large programs
1710 are not partitioned too much. Creating too many partitions significantly
1711 increases the streaming overhead.
1713 In the future, we would like to bound the maximal size of partitions so as
1714 to prevent the LTRANS stage from consuming too much memory. At the moment,
1715 however, the WPA stage is the most memory intensive for large benchmarks,
1716 since too many types and declarations are read into memory.
1718 The function implements a simple greedy algorithm. Nodes are being added
1719 to the current partition until after 3/4 of the expected partition size is
1720 reached. Past this threshold, we keep track of boundary size (number of
1721 edges going to other partitions) and continue adding functions until after
1722 the current partition has grown to twice the expected partition size. Then
1723 the process is undone to the point where the minimal ratio of boundary size
1724 and in-partition calls was reached. */
1726 static void
1727 lto_balanced_map (void)
1729 int n_nodes = 0;
1730 int n_varpool_nodes = 0, varpool_pos = 0;
1731 struct cgraph_node **postorder =
1732 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1733 struct cgraph_node **order = XNEWVEC (struct cgraph_node *, cgraph_max_uid);
1734 struct varpool_node **varpool_order = NULL;
1735 int i, postorder_len;
1736 struct cgraph_node *node;
1737 int total_size = 0, best_total_size = 0;
1738 int partition_size;
1739 ltrans_partition partition;
1740 unsigned int last_visited_cgraph_node = 0, last_visited_varpool_node = 0;
1741 struct varpool_node *vnode;
1742 int cost = 0, internal = 0;
1743 int best_n_nodes = 0, best_n_varpool_nodes = 0, best_i = 0, best_cost =
1744 INT_MAX, best_internal = 0;
1745 int npartitions;
1746 int current_order = -1;
1748 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1749 gcc_assert (!vnode->aux);
1750 /* Until we have better ordering facility, use toplogical order.
1751 Include only nodes we will partition and compute estimate of program
1752 size. Note that since nodes that are not partitioned might be put into
1753 multiple partitions, this is just an estimate of real size. This is why
1754 we keep partition_size updated after every partition is finalized. */
1755 postorder_len = ipa_reverse_postorder (postorder);
1757 for (i = 0; i < postorder_len; i++)
1759 node = postorder[i];
1760 if (partition_cgraph_node_p (node))
1762 order[n_nodes++] = node;
1763 total_size += inline_summary (node)->size;
1766 free (postorder);
1768 if (!flag_toplevel_reorder)
1770 qsort (order, n_nodes, sizeof (struct cgraph_node *), node_cmp);
1772 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1773 if (partition_varpool_node_p (vnode))
1774 n_varpool_nodes++;
1775 varpool_order = XNEWVEC (struct varpool_node *, n_varpool_nodes);
1777 n_varpool_nodes = 0;
1778 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1779 if (partition_varpool_node_p (vnode))
1780 varpool_order[n_varpool_nodes++] = vnode;
1781 qsort (varpool_order, n_varpool_nodes, sizeof (struct varpool_node *),
1782 varpool_node_cmp);
1785 /* Compute partition size and create the first partition. */
1786 partition_size = total_size / PARAM_VALUE (PARAM_LTO_PARTITIONS);
1787 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1788 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1789 npartitions = 1;
1790 partition = new_partition ("");
1791 if (cgraph_dump_file)
1792 fprintf (cgraph_dump_file, "Total unit size: %i, partition size: %i\n",
1793 total_size, partition_size);
1795 for (i = 0; i < n_nodes; i++)
1797 if (order[i]->aux)
1798 continue;
1800 current_order = order[i]->order;
1802 if (!flag_toplevel_reorder)
1803 while (varpool_pos < n_varpool_nodes && varpool_order[varpool_pos]->order < current_order)
1805 if (!varpool_order[varpool_pos]->aux)
1806 add_varpool_node_to_partition (partition, varpool_order[varpool_pos]);
1807 varpool_pos++;
1810 add_cgraph_node_to_partition (partition, order[i]);
1811 total_size -= inline_summary (order[i])->size;
1814 /* Once we added a new node to the partition, we also want to add
1815 all referenced variables unless they was already added into some
1816 earlier partition.
1817 add_cgraph_node_to_partition adds possibly multiple nodes and
1818 variables that are needed to satisfy needs of ORDER[i].
1819 We remember last visited cgraph and varpool node from last iteration
1820 of outer loop that allows us to process every new addition.
1822 At the same time we compute size of the boundary into COST. Every
1823 callgraph or IPA reference edge leaving the partition contributes into
1824 COST. Every edge inside partition was earlier computed as one leaving
1825 it and thus we need to subtract it from COST. */
1826 while (last_visited_cgraph_node <
1827 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes)
1828 || last_visited_varpool_node < VEC_length (varpool_node_ptr,
1829 partition->varpool_set->
1830 nodes))
1832 struct ipa_ref_list *refs;
1833 int j;
1834 struct ipa_ref *ref;
1835 bool cgraph_p = false;
1837 if (last_visited_cgraph_node <
1838 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes))
1840 struct cgraph_edge *edge;
1842 cgraph_p = true;
1843 node = VEC_index (cgraph_node_ptr, partition->cgraph_set->nodes,
1844 last_visited_cgraph_node);
1845 refs = &node->ref_list;
1847 last_visited_cgraph_node++;
1849 gcc_assert (node->analyzed);
1851 /* Compute boundary cost of callgraph edges. */
1852 for (edge = node->callees; edge; edge = edge->next_callee)
1853 if (edge->callee->analyzed)
1855 int edge_cost = edge->frequency;
1856 cgraph_node_set_iterator csi;
1858 if (!edge_cost)
1859 edge_cost = 1;
1860 gcc_assert (edge_cost > 0);
1861 csi = cgraph_node_set_find (partition->cgraph_set, edge->callee);
1862 if (!csi_end_p (csi)
1863 && csi.index < last_visited_cgraph_node - 1)
1864 cost -= edge_cost, internal+= edge_cost;
1865 else
1866 cost += edge_cost;
1868 for (edge = node->callers; edge; edge = edge->next_caller)
1870 int edge_cost = edge->frequency;
1871 cgraph_node_set_iterator csi;
1873 gcc_assert (edge->caller->analyzed);
1874 if (!edge_cost)
1875 edge_cost = 1;
1876 gcc_assert (edge_cost > 0);
1877 csi = cgraph_node_set_find (partition->cgraph_set, edge->caller);
1878 if (!csi_end_p (csi)
1879 && csi.index < last_visited_cgraph_node)
1880 cost -= edge_cost;
1881 else
1882 cost += edge_cost;
1885 else
1887 refs =
1888 &VEC_index (varpool_node_ptr, partition->varpool_set->nodes,
1889 last_visited_varpool_node)->ref_list;
1890 last_visited_varpool_node++;
1893 /* Compute boundary cost of IPA REF edges and at the same time look into
1894 variables referenced from current partition and try to add them. */
1895 for (j = 0; ipa_ref_list_reference_iterate (refs, j, ref); j++)
1896 if (ref->refered_type == IPA_REF_VARPOOL)
1898 varpool_node_set_iterator vsi;
1900 vnode = ipa_ref_varpool_node (ref);
1901 if (!vnode->finalized)
1902 continue;
1903 if (!vnode->aux && flag_toplevel_reorder
1904 && partition_varpool_node_p (vnode))
1905 add_varpool_node_to_partition (partition, vnode);
1906 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1907 if (!vsi_end_p (vsi)
1908 && vsi.index < last_visited_varpool_node - !cgraph_p)
1909 cost--, internal++;
1910 else
1911 cost++;
1913 else
1915 cgraph_node_set_iterator csi;
1917 node = ipa_ref_node (ref);
1918 if (!node->analyzed)
1919 continue;
1920 csi = cgraph_node_set_find (partition->cgraph_set, node);
1921 if (!csi_end_p (csi)
1922 && csi.index < last_visited_cgraph_node - cgraph_p)
1923 cost--, internal++;
1924 else
1925 cost++;
1927 for (j = 0; ipa_ref_list_refering_iterate (refs, j, ref); j++)
1928 if (ref->refering_type == IPA_REF_VARPOOL)
1930 varpool_node_set_iterator vsi;
1932 vnode = ipa_ref_refering_varpool_node (ref);
1933 gcc_assert (vnode->finalized);
1934 if (!vnode->aux && flag_toplevel_reorder
1935 && partition_varpool_node_p (vnode))
1936 add_varpool_node_to_partition (partition, vnode);
1937 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1938 if (!vsi_end_p (vsi)
1939 && vsi.index < last_visited_varpool_node)
1940 cost--;
1941 else
1942 cost++;
1944 else
1946 cgraph_node_set_iterator csi;
1948 node = ipa_ref_refering_node (ref);
1949 gcc_assert (node->analyzed);
1950 csi = cgraph_node_set_find (partition->cgraph_set, node);
1951 if (!csi_end_p (csi)
1952 && csi.index < last_visited_cgraph_node)
1953 cost--;
1954 else
1955 cost++;
1959 /* If the partition is large enough, start looking for smallest boundary cost. */
1960 if (partition->insns < partition_size * 3 / 4
1961 || best_cost == INT_MAX
1962 || ((!cost
1963 || (best_internal * (HOST_WIDE_INT) cost
1964 > (internal * (HOST_WIDE_INT)best_cost)))
1965 && partition->insns < partition_size * 5 / 4))
1967 best_cost = cost;
1968 best_internal = internal;
1969 best_i = i;
1970 best_n_nodes = VEC_length (cgraph_node_ptr,
1971 partition->cgraph_set->nodes);
1972 best_n_varpool_nodes = VEC_length (varpool_node_ptr,
1973 partition->varpool_set->nodes);
1974 best_total_size = total_size;
1976 if (cgraph_dump_file)
1977 fprintf (cgraph_dump_file, "Step %i: added %s/%i, size %i, cost %i/%i best %i/%i, step %i\n", i,
1978 cgraph_node_name (order[i]), order[i]->uid, partition->insns, cost, internal,
1979 best_cost, best_internal, best_i);
1980 /* Partition is too large, unwind into step when best cost was reached and
1981 start new partition. */
1982 if (partition->insns > 2 * partition_size)
1984 if (best_i != i)
1986 if (cgraph_dump_file)
1987 fprintf (cgraph_dump_file, "Unwinding %i insertions to step %i\n",
1988 i - best_i, best_i);
1989 undo_partition (partition, best_n_nodes, best_n_varpool_nodes);
1991 i = best_i;
1992 /* When we are finished, avoid creating empty partition. */
1993 while (i < n_nodes - 1 && order[i + 1]->aux)
1994 i++;
1995 if (i == n_nodes - 1)
1996 break;
1997 partition = new_partition ("");
1998 last_visited_cgraph_node = 0;
1999 last_visited_varpool_node = 0;
2000 total_size = best_total_size;
2001 cost = 0;
2003 if (cgraph_dump_file)
2004 fprintf (cgraph_dump_file, "New partition\n");
2005 best_n_nodes = 0;
2006 best_n_varpool_nodes = 0;
2007 best_cost = INT_MAX;
2009 /* Since the size of partitions is just approximate, update the size after
2010 we finished current one. */
2011 if (npartitions < PARAM_VALUE (PARAM_LTO_PARTITIONS))
2012 partition_size = total_size
2013 / (PARAM_VALUE (PARAM_LTO_PARTITIONS) - npartitions);
2014 else
2015 partition_size = INT_MAX;
2017 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
2018 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
2019 npartitions ++;
2023 /* Varables that are not reachable from the code go into last partition. */
2024 if (flag_toplevel_reorder)
2026 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
2027 if (partition_varpool_node_p (vnode) && !vnode->aux)
2028 add_varpool_node_to_partition (partition, vnode);
2030 else
2032 while (varpool_pos < n_varpool_nodes)
2034 if (!varpool_order[varpool_pos]->aux)
2035 add_varpool_node_to_partition (partition, varpool_order[varpool_pos]);
2036 varpool_pos++;
2038 free (varpool_order);
2040 free (order);
2043 /* Promote variable VNODE to be static. */
2045 static bool
2046 promote_var (struct varpool_node *vnode)
2048 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
2049 return false;
2050 gcc_assert (flag_wpa);
2051 TREE_PUBLIC (vnode->decl) = 1;
2052 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
2053 DECL_VISIBILITY_SPECIFIED (vnode->decl) = true;
2054 if (cgraph_dump_file)
2055 fprintf (cgraph_dump_file,
2056 "Promoting var as hidden: %s\n", varpool_node_name (vnode));
2057 return true;
2060 /* Promote function NODE to be static. */
2062 static bool
2063 promote_fn (struct cgraph_node *node)
2065 gcc_assert (flag_wpa);
2066 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2067 return false;
2068 TREE_PUBLIC (node->decl) = 1;
2069 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
2070 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
2071 if (cgraph_dump_file)
2072 fprintf (cgraph_dump_file,
2073 "Promoting function as hidden: %s/%i\n",
2074 cgraph_node_name (node), node->uid);
2075 return true;
2078 /* Find out all static decls that need to be promoted to global because
2079 of cross file sharing. This function must be run in the WPA mode after
2080 all inlinees are added. */
2082 static void
2083 lto_promote_cross_file_statics (void)
2085 struct varpool_node *vnode;
2086 unsigned i, n_sets;
2087 cgraph_node_set set;
2088 varpool_node_set vset;
2089 cgraph_node_set_iterator csi;
2090 varpool_node_set_iterator vsi;
2091 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
2092 struct pointer_set_t *inserted = pointer_set_create ();
2094 gcc_assert (flag_wpa);
2096 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
2097 for (i = 0; i < n_sets; i++)
2099 ltrans_partition part
2100 = VEC_index (ltrans_partition, ltrans_partitions, i);
2101 set = part->cgraph_set;
2102 vset = part->varpool_set;
2104 /* If node called or referred to from other partition, it needs to be
2105 globalized. */
2106 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2108 struct cgraph_node *node = csi_node (csi);
2109 if (node->local.externally_visible)
2110 continue;
2111 if (node->global.inlined_to)
2112 continue;
2113 if ((!DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
2114 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
2115 || reachable_from_other_partition_p (node, set)))
2116 promote_fn (node);
2118 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
2120 vnode = vsi_node (vsi);
2121 /* Constant pool references use internal labels and thus can not
2122 be made global. It is sensible to keep those ltrans local to
2123 allow better optimization. */
2124 if (!DECL_IN_CONSTANT_POOL (vnode->decl) && !DECL_COMDAT (vnode->decl)
2125 && !vnode->externally_visible && vnode->analyzed
2126 && referenced_from_other_partition_p (&vnode->ref_list,
2127 set, vset))
2128 promote_var (vnode);
2131 /* We export the initializer of a read-only var into each partition
2132 referencing the var. Folding might take declarations from the
2133 initializer and use them, so everything referenced from the
2134 initializer can be accessed from this partition after folding.
2136 This means that we need to promote all variables and functions
2137 referenced from all initializers of read-only vars referenced
2138 from this partition that are not in this partition. This needs
2139 to be done recursively. */
2140 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
2141 if (const_value_known_p (vnode->decl)
2142 && DECL_INITIAL (vnode->decl)
2143 && !varpool_node_in_set_p (vnode, vset)
2144 && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
2145 && !pointer_set_insert (inserted, vnode))
2146 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
2148 while (!VEC_empty (varpool_node_ptr, promoted_initializers))
2150 int i;
2151 struct ipa_ref *ref;
2153 vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
2154 for (i = 0;
2155 ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref);
2156 i++)
2158 if (ref->refered_type == IPA_REF_CGRAPH)
2160 struct cgraph_node *n = ipa_ref_node (ref);
2161 gcc_assert (!n->global.inlined_to);
2162 if (!n->local.externally_visible
2163 && !cgraph_node_in_set_p (n, set))
2164 promote_fn (n);
2166 else
2168 struct varpool_node *v = ipa_ref_varpool_node (ref);
2169 if (varpool_node_in_set_p (v, vset))
2170 continue;
2172 /* Constant pool references use internal labels and thus
2173 cannot be made global. It is sensible to keep those
2174 ltrans local to allow better optimization. */
2175 if (DECL_IN_CONSTANT_POOL (v->decl))
2177 if (!pointer_set_insert (inserted, vnode))
2178 VEC_safe_push (varpool_node_ptr, heap,
2179 promoted_initializers, v);
2181 else if (!v->externally_visible && v->analyzed)
2183 if (promote_var (v)
2184 && DECL_INITIAL (v->decl)
2185 && const_value_known_p (v->decl)
2186 && !pointer_set_insert (inserted, vnode))
2187 VEC_safe_push (varpool_node_ptr, heap,
2188 promoted_initializers, v);
2194 pointer_set_destroy (inserted);
2197 static lto_file *current_lto_file;
2199 /* Helper for qsort; compare partitions and return one with smaller size.
2200 We sort from greatest to smallest so parallel build doesn't stale on the
2201 longest compilation being executed too late. */
2203 static int
2204 cmp_partitions_size (const void *a, const void *b)
2206 const struct ltrans_partition_def *pa
2207 = *(struct ltrans_partition_def *const *)a;
2208 const struct ltrans_partition_def *pb
2209 = *(struct ltrans_partition_def *const *)b;
2210 return pb->insns - pa->insns;
2213 /* Helper for qsort; compare partitions and return one with smaller order. */
2215 static int
2216 cmp_partitions_order (const void *a, const void *b)
2218 const struct ltrans_partition_def *pa
2219 = *(struct ltrans_partition_def *const *)a;
2220 const struct ltrans_partition_def *pb
2221 = *(struct ltrans_partition_def *const *)b;
2222 int ordera = -1, orderb = -1;
2224 if (VEC_length (cgraph_node_ptr, pa->cgraph_set->nodes))
2225 ordera = VEC_index (cgraph_node_ptr, pa->cgraph_set->nodes, 0)->order;
2226 else if (VEC_length (varpool_node_ptr, pa->varpool_set->nodes))
2227 ordera = VEC_index (varpool_node_ptr, pa->varpool_set->nodes, 0)->order;
2228 if (VEC_length (cgraph_node_ptr, pb->cgraph_set->nodes))
2229 orderb = VEC_index (cgraph_node_ptr, pb->cgraph_set->nodes, 0)->order;
2230 else if (VEC_length (varpool_node_ptr, pb->varpool_set->nodes))
2231 orderb = VEC_index (varpool_node_ptr, pb->varpool_set->nodes, 0)->order;
2232 return orderb - ordera;
2235 /* Write all output files in WPA mode and the file with the list of
2236 LTRANS units. */
2238 static void
2239 lto_wpa_write_files (void)
2241 unsigned i, n_sets;
2242 lto_file *file;
2243 cgraph_node_set set;
2244 varpool_node_set vset;
2245 ltrans_partition part;
2246 FILE *ltrans_output_list_stream;
2247 char *temp_filename;
2248 size_t blen;
2250 /* Open the LTRANS output list. */
2251 if (!ltrans_output_list)
2252 fatal_error ("no LTRANS output list filename provided");
2253 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2254 if (ltrans_output_list_stream == NULL)
2255 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2257 timevar_push (TV_WHOPR_WPA);
2259 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
2260 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
2261 part->cgraph_set->nodes);
2263 /* Find out statics that need to be promoted
2264 to globals with hidden visibility because they are accessed from multiple
2265 partitions. */
2266 lto_promote_cross_file_statics ();
2268 timevar_pop (TV_WHOPR_WPA);
2270 timevar_push (TV_WHOPR_WPA_IO);
2272 /* Generate a prefix for the LTRANS unit files. */
2273 blen = strlen (ltrans_output_list);
2274 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2275 strcpy (temp_filename, ltrans_output_list);
2276 if (blen > sizeof (".out")
2277 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2278 ".out") == 0)
2279 temp_filename[blen - sizeof (".out") + 1] = '\0';
2280 blen = strlen (temp_filename);
2282 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
2284 /* Sort partitions by size so small ones are compiled last.
2285 FIXME: Even when not reordering we may want to output one list for parallel make
2286 and other for final link command. */
2287 VEC_qsort (ltrans_partition, ltrans_partitions,
2288 flag_toplevel_reorder ? cmp_partitions_size : cmp_partitions_order);
2289 for (i = 0; i < n_sets; i++)
2291 size_t len;
2292 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
2294 set = part->cgraph_set;
2295 vset = part->varpool_set;
2297 /* Write all the nodes in SET. */
2298 sprintf (temp_filename + blen, "%u.o", i);
2299 file = lto_obj_file_open (temp_filename, true);
2300 if (!file)
2301 fatal_error ("lto_obj_file_open() failed");
2303 if (!quiet_flag)
2304 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2305 if (cgraph_dump_file)
2307 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
2308 part->name, temp_filename, part->insns);
2309 fprintf (cgraph_dump_file, "cgraph nodes:");
2310 dump_cgraph_node_set (cgraph_dump_file, set);
2311 fprintf (cgraph_dump_file, "varpool nodes:");
2312 dump_varpool_node_set (cgraph_dump_file, vset);
2314 gcc_checking_assert (cgraph_node_set_nonempty_p (set)
2315 || varpool_node_set_nonempty_p (vset) || !i);
2317 lto_set_current_out_file (file);
2319 ipa_write_optimization_summaries (set, vset);
2321 lto_set_current_out_file (NULL);
2322 lto_obj_file_close (file);
2324 len = strlen (temp_filename);
2325 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
2326 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2327 fatal_error ("writing to LTRANS output list %s: %m",
2328 ltrans_output_list);
2331 lto_stats.num_output_files += n_sets;
2333 /* Close the LTRANS output list. */
2334 if (fclose (ltrans_output_list_stream))
2335 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2337 free_ltrans_partitions();
2339 timevar_pop (TV_WHOPR_WPA_IO);
2343 /* If TT is a variable or function decl replace it with its
2344 prevailing variant. */
2345 #define LTO_SET_PREVAIL(tt) \
2346 do {\
2347 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
2348 tt = lto_symtab_prevailing_decl (tt); \
2349 } while (0)
2351 /* Ensure that TT isn't a replacable var of function decl. */
2352 #define LTO_NO_PREVAIL(tt) \
2353 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2355 /* Given a tree T replace all fields referring to variables or functions
2356 with their prevailing variant. */
2357 static void
2358 lto_fixup_prevailing_decls (tree t)
2360 enum tree_code code = TREE_CODE (t);
2361 LTO_NO_PREVAIL (TREE_TYPE (t));
2362 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2363 LTO_NO_PREVAIL (TREE_CHAIN (t));
2364 if (DECL_P (t))
2366 LTO_NO_PREVAIL (DECL_NAME (t));
2367 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2368 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2370 LTO_SET_PREVAIL (DECL_SIZE (t));
2371 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2372 LTO_SET_PREVAIL (DECL_INITIAL (t));
2373 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2374 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2376 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2378 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2379 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
2381 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2383 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
2384 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2385 LTO_NO_PREVAIL (DECL_VINDEX (t));
2387 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2388 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2389 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2391 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
2392 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2393 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2394 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2395 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2398 else if (TYPE_P (t))
2400 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2401 LTO_SET_PREVAIL (TYPE_SIZE (t));
2402 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2403 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2404 LTO_NO_PREVAIL (TYPE_NAME (t));
2406 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2407 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2408 LTO_SET_PREVAIL (t->type_non_common.binfo);
2410 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2412 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2413 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2414 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2416 else if (EXPR_P (t))
2418 int i;
2419 LTO_NO_PREVAIL (t->exp.block);
2420 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2421 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2423 else
2425 switch (code)
2427 case TREE_LIST:
2428 LTO_SET_PREVAIL (TREE_VALUE (t));
2429 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2430 break;
2431 default:
2432 gcc_unreachable ();
2436 #undef LTO_SET_PREVAIL
2437 #undef LTO_NO_PREVAIL
2439 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2440 replaces var and function decls with the corresponding prevailing def. */
2442 static void
2443 lto_fixup_state (struct lto_in_decl_state *state)
2445 unsigned i, si;
2446 struct lto_tree_ref_table *table;
2448 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2449 we still need to walk from all DECLs to find the reachable
2450 FUNCTION_DECLs and VAR_DECLs. */
2451 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2453 table = &state->streams[si];
2454 for (i = 0; i < table->size; i++)
2456 tree *tp = table->trees + i;
2457 if (VAR_OR_FUNCTION_DECL_P (*tp))
2458 *tp = lto_symtab_prevailing_decl (*tp);
2463 /* A callback of htab_traverse. Just extracts a state from SLOT
2464 and calls lto_fixup_state. */
2466 static int
2467 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
2469 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
2470 lto_fixup_state (state);
2471 return 1;
2474 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2475 prevailing one. */
2477 static void
2478 lto_fixup_decls (struct lto_file_decl_data **files)
2480 unsigned int i;
2481 htab_iterator hi;
2482 tree t;
2484 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
2485 lto_fixup_prevailing_decls (t);
2487 for (i = 0; files[i]; i++)
2489 struct lto_file_decl_data *file = files[i];
2490 struct lto_in_decl_state *state = file->global_decl_state;
2491 lto_fixup_state (state);
2493 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
2497 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2499 /* Turn file datas for sub files into a single array, so that they look
2500 like separate files for further passes. */
2502 static void
2503 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2505 struct lto_file_decl_data *n, *next;
2506 int i, k;
2508 lto_stats.num_input_files = count;
2509 all_file_decl_data
2510 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
2511 /* Set the hooks so that all of the ipa passes can read in their data. */
2512 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2513 for (i = 0, k = 0; i < last_file_ix; i++)
2515 for (n = orig[i]; n != NULL; n = next)
2517 all_file_decl_data[k++] = n;
2518 next = n->next;
2519 n->next = NULL;
2522 all_file_decl_data[k] = NULL;
2523 gcc_assert (k == count);
2526 /* Input file data before flattening (i.e. splitting them to subfiles to support
2527 incremental linking. */
2528 static int real_file_count;
2529 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2531 /* Read all the symbols from the input files FNAMES. NFILES is the
2532 number of files requested in the command line. Instantiate a
2533 global call graph by aggregating all the sub-graphs found in each
2534 file. */
2536 static void
2537 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2539 unsigned int i, last_file_ix;
2540 FILE *resolution;
2541 struct cgraph_node *node;
2542 int count = 0;
2543 struct lto_file_decl_data **decl_data;
2545 init_cgraph ();
2547 timevar_push (TV_IPA_LTO_DECL_IN);
2549 real_file_decl_data
2550 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
2551 real_file_count = nfiles;
2553 /* Read the resolution file. */
2554 resolution = NULL;
2555 if (resolution_file_name)
2557 int t;
2558 unsigned num_objects;
2560 resolution = fopen (resolution_file_name, "r");
2561 if (resolution == NULL)
2562 fatal_error ("could not open symbol resolution file: %m");
2564 t = fscanf (resolution, "%u", &num_objects);
2565 gcc_assert (t == 1);
2567 /* True, since the plugin splits the archives. */
2568 gcc_assert (num_objects == nfiles);
2571 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
2572 NULL);
2574 if (!quiet_flag)
2575 fprintf (stderr, "Reading object files:");
2577 /* Read all of the object files specified on the command line. */
2578 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2580 struct lto_file_decl_data *file_data = NULL;
2581 if (!quiet_flag)
2583 fprintf (stderr, " %s", fnames[i]);
2584 fflush (stderr);
2587 current_lto_file = lto_obj_file_open (fnames[i], false);
2588 if (!current_lto_file)
2589 break;
2591 file_data = lto_file_read (current_lto_file, resolution, &count);
2592 if (!file_data)
2594 lto_obj_file_close (current_lto_file);
2595 current_lto_file = NULL;
2596 break;
2599 decl_data[last_file_ix++] = file_data;
2601 lto_obj_file_close (current_lto_file);
2602 current_lto_file = NULL;
2603 ggc_collect ();
2606 lto_flatten_files (decl_data, count, last_file_ix);
2607 lto_stats.num_input_files = count;
2608 ggc_free(decl_data);
2609 real_file_decl_data = NULL;
2611 if (resolution_file_name)
2612 fclose (resolution);
2614 /* Set the hooks so that all of the ipa passes can read in their data. */
2615 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2617 timevar_pop (TV_IPA_LTO_DECL_IN);
2619 if (!quiet_flag)
2620 fprintf (stderr, "\nReading the callgraph\n");
2622 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2623 /* Read the callgraph. */
2624 input_cgraph ();
2625 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2627 if (!quiet_flag)
2628 fprintf (stderr, "Merging declarations\n");
2630 timevar_push (TV_IPA_LTO_DECL_MERGE);
2631 /* Merge global decls. */
2632 lto_symtab_merge_decls ();
2634 /* If there were errors during symbol merging bail out, we have no
2635 good way to recover here. */
2636 if (seen_error ())
2637 fatal_error ("errors during merging of translation units");
2639 /* Fixup all decls and types and free the type hash tables. */
2640 lto_fixup_decls (all_file_decl_data);
2641 htab_delete (tree_with_vars);
2642 tree_with_vars = NULL;
2643 free_gimple_type_tables ();
2644 ggc_collect ();
2646 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2647 /* Each pass will set the appropriate timer. */
2649 if (!quiet_flag)
2650 fprintf (stderr, "Reading summaries\n");
2652 /* Read the IPA summary data. */
2653 if (flag_ltrans)
2654 ipa_read_optimization_summaries ();
2655 else
2656 ipa_read_summaries ();
2658 /* Finally merge the cgraph according to the decl merging decisions. */
2659 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2660 if (cgraph_dump_file)
2662 fprintf (cgraph_dump_file, "Before merging:\n");
2663 dump_cgraph (cgraph_dump_file);
2664 dump_varpool (cgraph_dump_file);
2666 lto_symtab_merge_cgraph_nodes ();
2667 ggc_collect ();
2669 if (flag_ltrans)
2670 for (node = cgraph_nodes; node; node = node->next)
2672 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
2673 summaries computed and needs to apply changes. At the moment WHOPR only
2674 supports inlining, so we can push it here by hand. In future we need to stream
2675 this field into ltrans compilation. */
2676 if (node->analyzed)
2677 VEC_safe_push (ipa_opt_pass, heap,
2678 node->ipa_transforms_to_apply,
2679 (ipa_opt_pass)&pass_ipa_inline);
2681 lto_symtab_free ();
2683 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2685 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
2687 /* FIXME lto. This loop needs to be changed to use the pass manager to
2688 call the ipa passes directly. */
2689 if (!seen_error ())
2690 for (i = 0; i < last_file_ix; i++)
2692 struct lto_file_decl_data *file_data = all_file_decl_data [i];
2693 lto_materialize_constructors_and_inits (file_data);
2696 /* Indicate that the cgraph is built and ready. */
2697 cgraph_function_flags_ready = true;
2699 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
2700 ggc_free (all_file_decl_data);
2701 all_file_decl_data = NULL;
2705 /* Materialize all the bodies for all the nodes in the callgraph. */
2707 static void
2708 materialize_cgraph (void)
2710 tree decl;
2711 struct cgraph_node *node;
2712 unsigned i;
2713 timevar_id_t lto_timer;
2715 if (!quiet_flag)
2716 fprintf (stderr,
2717 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2720 /* Now that we have input the cgraph, we need to clear all of the aux
2721 nodes and read the functions if we are not running in WPA mode. */
2722 timevar_push (TV_IPA_LTO_GIMPLE_IN);
2724 for (node = cgraph_nodes; node; node = node->next)
2726 if (node->local.lto_file_data)
2728 lto_materialize_function (node);
2729 lto_stats.num_input_cgraph_nodes++;
2733 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
2735 /* Start the appropriate timer depending on the mode that we are
2736 operating in. */
2737 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2738 : (flag_ltrans) ? TV_WHOPR_LTRANS
2739 : TV_LTO;
2740 timevar_push (lto_timer);
2742 current_function_decl = NULL;
2743 set_cfun (NULL);
2745 /* Inform the middle end about the global variables we have seen. */
2746 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
2747 rest_of_decl_compilation (decl, 1, 0);
2749 if (!quiet_flag)
2750 fprintf (stderr, "\n");
2752 timevar_pop (lto_timer);
2756 /* Perform whole program analysis (WPA) on the callgraph and write out the
2757 optimization plan. */
2759 static void
2760 do_whole_program_analysis (void)
2762 /* Note that since we are in WPA mode, materialize_cgraph will not
2763 actually read in all the function bodies. It only materializes
2764 the decls and cgraph nodes so that analysis can be performed. */
2765 materialize_cgraph ();
2767 /* Reading in the cgraph uses different timers, start timing WPA now. */
2768 timevar_push (TV_WHOPR_WPA);
2770 if (pre_ipa_mem_report)
2772 fprintf (stderr, "Memory consumption before IPA\n");
2773 dump_memory_report (false);
2776 cgraph_function_flags_ready = true;
2778 if (cgraph_dump_file)
2780 dump_cgraph (cgraph_dump_file);
2781 dump_varpool (cgraph_dump_file);
2783 bitmap_obstack_initialize (NULL);
2784 cgraph_state = CGRAPH_STATE_IPA_SSA;
2786 execute_ipa_pass_list (all_regular_ipa_passes);
2788 if (cgraph_dump_file)
2790 fprintf (cgraph_dump_file, "Optimized ");
2791 dump_cgraph (cgraph_dump_file);
2792 dump_varpool (cgraph_dump_file);
2794 verify_cgraph ();
2795 bitmap_obstack_release (NULL);
2797 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
2798 timevar_pop (TV_WHOPR_WPA);
2800 if (flag_lto_partition_1to1)
2801 lto_1_to_1_map ();
2802 else
2803 lto_balanced_map ();
2805 if (!quiet_flag)
2807 fprintf (stderr, "\nStreaming out");
2808 fflush (stderr);
2810 lto_wpa_write_files ();
2811 ggc_collect ();
2812 if (!quiet_flag)
2813 fprintf (stderr, "\n");
2815 if (post_ipa_mem_report)
2817 fprintf (stderr, "Memory consumption after IPA\n");
2818 dump_memory_report (false);
2821 /* Show the LTO report before launching LTRANS. */
2822 if (flag_lto_report)
2823 print_lto_report ();
2827 static GTY(()) tree lto_eh_personality_decl;
2829 /* Return the LTO personality function decl. */
2831 tree
2832 lto_eh_personality (void)
2834 if (!lto_eh_personality_decl)
2836 /* Use the first personality DECL for our personality if we don't
2837 support multiple ones. This ensures that we don't artificially
2838 create the need for them in a single-language program. */
2839 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2840 lto_eh_personality_decl = first_personality_decl;
2841 else
2842 lto_eh_personality_decl = lhd_gcc_personality ();
2845 return lto_eh_personality_decl;
2848 /* Set the process name based on the LTO mode. */
2850 static void
2851 lto_process_name (void)
2853 if (flag_lto)
2854 setproctitle ("lto1-lto");
2855 if (flag_wpa)
2856 setproctitle ("lto1-wpa");
2857 if (flag_ltrans)
2858 setproctitle ("lto1-ltrans");
2862 /* Initialize the LTO front end. */
2864 static void
2865 lto_init (void)
2867 lto_process_name ();
2868 lto_streamer_hooks_init ();
2869 lto_reader_init ();
2870 lto_set_in_hooks (NULL, get_section_data, free_section_data);
2871 memset (&lto_stats, 0, sizeof (lto_stats));
2872 bitmap_obstack_initialize (NULL);
2873 gimple_register_cfg_hooks ();
2877 /* Main entry point for the GIMPLE front end. This front end has
2878 three main personalities:
2880 - LTO (-flto). All the object files on the command line are
2881 loaded in memory and processed as a single translation unit.
2882 This is the traditional link-time optimization behavior.
2884 - WPA (-fwpa). Only the callgraph and summary information for
2885 files in the command file are loaded. A single callgraph
2886 (without function bodies) is instantiated for the whole set of
2887 files. IPA passes are only allowed to analyze the call graph
2888 and make transformation decisions. The callgraph is
2889 partitioned, each partition is written to a new object file
2890 together with the transformation decisions.
2892 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2893 summary files from running again. Since WPA computed summary
2894 information and decided what transformations to apply, LTRANS
2895 simply applies them. */
2897 void
2898 lto_main (void)
2900 /* Initialize the LTO front end. */
2901 lto_init ();
2903 /* Read all the symbols and call graph from all the files in the
2904 command line. */
2905 read_cgraph_and_symbols (num_in_fnames, in_fnames);
2907 if (!seen_error ())
2909 /* If WPA is enabled analyze the whole call graph and create an
2910 optimization plan. Otherwise, read in all the function
2911 bodies and continue with optimization. */
2912 if (flag_wpa)
2913 do_whole_program_analysis ();
2914 else
2916 materialize_cgraph ();
2918 /* Let the middle end know that we have read and merged all of
2919 the input files. */
2920 cgraph_optimize ();
2922 /* FIXME lto, if the processes spawned by WPA fail, we miss
2923 the chance to print WPA's report, so WPA will call
2924 print_lto_report before launching LTRANS. If LTRANS was
2925 launched directly by the driver we would not need to do
2926 this. */
2927 if (flag_lto_report)
2928 print_lto_report ();
2933 #include "gt-lto-lto.h"