lto.c (uniquify_nodes): Move code to register decls to the loop that computes canonic...
[official-gcc.git] / gcc / lto / lto.c
blob6e49ee77b59cc353d5f98f91db6a6d30040ab4fc
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 "splay-tree.h"
47 #include "params.h"
48 #include "ipa-inline.h"
49 #include "ipa-utils.h"
51 static GTY(()) tree first_personality_decl;
53 /* Returns a hash code for P. */
55 static hashval_t
56 hash_name (const void *p)
58 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
59 return (hashval_t) htab_hash_string (ds->name);
63 /* Returns nonzero if P1 and P2 are equal. */
65 static int
66 eq_name (const void *p1, const void *p2)
68 const struct lto_section_slot *s1 =
69 (const struct lto_section_slot *) p1;
70 const struct lto_section_slot *s2 =
71 (const struct lto_section_slot *) p2;
73 return strcmp (s1->name, s2->name) == 0;
76 /* Free lto_section_slot */
78 static void
79 free_with_string (void *arg)
81 struct lto_section_slot *s = (struct lto_section_slot *)arg;
83 free (CONST_CAST (char *, s->name));
84 free (arg);
87 /* Create section hash table */
89 htab_t
90 lto_obj_create_section_hash_table (void)
92 return htab_create (37, hash_name, eq_name, free_with_string);
95 /* Read the constructors and inits. */
97 static void
98 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
100 size_t len;
101 const char *data = lto_get_section_data (file_data,
102 LTO_section_static_initializer,
103 NULL, &len);
104 lto_input_constructors_and_inits (file_data, data);
105 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
106 data, len);
109 /* Return true when NODE has a clone that is analyzed (i.e. we need
110 to load its body even if the node itself is not needed). */
112 static bool
113 has_analyzed_clone_p (struct cgraph_node *node)
115 struct cgraph_node *orig = node;
116 node = node->clones;
117 if (node)
118 while (node != orig)
120 if (node->analyzed)
121 return true;
122 if (node->clones)
123 node = node->clones;
124 else if (node->next_sibling_clone)
125 node = node->next_sibling_clone;
126 else
128 while (node != orig && !node->next_sibling_clone)
129 node = node->clone_of;
130 if (node != orig)
131 node = node->next_sibling_clone;
134 return false;
137 /* Read the function body for the function associated with NODE. */
139 static void
140 lto_materialize_function (struct cgraph_node *node)
142 tree decl;
143 struct lto_file_decl_data *file_data;
144 const char *data, *name;
145 size_t len;
147 decl = node->decl;
148 /* Read in functions with body (analyzed nodes)
149 and also functions that are needed to produce virtual clones. */
150 if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node))
152 /* Clones and thunks don't need to be read. */
153 if (node->clone_of)
154 return;
156 /* Load the function body only if not operating in WPA mode. In
157 WPA mode, the body of the function is not needed. */
158 if (!flag_wpa)
160 file_data = node->local.lto_file_data;
161 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
163 /* We may have renamed the declaration, e.g., a static function. */
164 name = lto_get_decl_name_mapping (file_data, name);
166 data = lto_get_section_data (file_data, LTO_section_function_body,
167 name, &len);
168 if (!data)
169 fatal_error ("%s: section %s is missing",
170 file_data->file_name,
171 name);
173 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
175 allocate_struct_function (decl, false);
176 announce_function (decl);
177 lto_input_function_body (file_data, decl, data);
178 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
179 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
180 lto_stats.num_function_bodies++;
181 lto_free_section_data (file_data, LTO_section_function_body, name,
182 data, len);
183 ggc_collect ();
187 /* Let the middle end know about the function. */
188 rest_of_decl_compilation (decl, 1, 0);
192 /* Decode the content of memory pointed to by DATA in the in decl
193 state object STATE. DATA_IN points to a data_in structure for
194 decoding. Return the address after the decoded object in the
195 input. */
197 static const uint32_t *
198 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
199 struct lto_in_decl_state *state)
201 uint32_t ix;
202 tree decl;
203 uint32_t i, j;
205 ix = *data++;
206 decl = lto_streamer_cache_get (data_in->reader_cache, ix);
207 if (TREE_CODE (decl) != FUNCTION_DECL)
209 gcc_assert (decl == void_type_node);
210 decl = NULL_TREE;
212 state->fn_decl = decl;
214 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
216 uint32_t size = *data++;
217 tree *decls = ggc_alloc_vec_tree (size);
219 for (j = 0; j < size; j++)
220 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
222 state->streams[i].size = size;
223 state->streams[i].trees = decls;
224 data += size;
227 return data;
230 /* A hashtable of trees that potentially refer to variables or functions
231 that must be replaced with their prevailing variant. */
232 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
233 tree_with_vars;
235 /* Remember that T is a tree that (potentially) refers to a variable
236 or function decl that may be replaced with its prevailing variant. */
237 static void
238 remember_with_vars (tree t)
240 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
243 #define LTO_FIXUP_TREE(tt) \
244 do \
246 if (tt) \
248 if (TYPE_P (tt)) \
249 (tt) = gimple_register_type (tt); \
250 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
251 remember_with_vars (t); \
253 } while (0)
255 static void lto_fixup_types (tree);
257 /* Fix up fields of a tree_typed T. */
259 static void
260 lto_ft_typed (tree t)
262 LTO_FIXUP_TREE (TREE_TYPE (t));
265 /* Fix up fields of a tree_common T. */
267 static void
268 lto_ft_common (tree t)
270 lto_ft_typed (t);
271 LTO_FIXUP_TREE (TREE_CHAIN (t));
274 /* Fix up fields of a decl_minimal T. */
276 static void
277 lto_ft_decl_minimal (tree t)
279 lto_ft_common (t);
280 LTO_FIXUP_TREE (DECL_NAME (t));
281 LTO_FIXUP_TREE (DECL_CONTEXT (t));
284 /* Fix up fields of a decl_common T. */
286 static void
287 lto_ft_decl_common (tree t)
289 lto_ft_decl_minimal (t);
290 LTO_FIXUP_TREE (DECL_SIZE (t));
291 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t));
292 LTO_FIXUP_TREE (DECL_INITIAL (t));
293 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t));
294 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t));
297 /* Fix up fields of a decl_with_vis T. */
299 static void
300 lto_ft_decl_with_vis (tree t)
302 lto_ft_decl_common (t);
304 /* Accessor macro has side-effects, use field-name here. */
305 LTO_FIXUP_TREE (t->decl_with_vis.assembler_name);
306 LTO_FIXUP_TREE (DECL_SECTION_NAME (t));
309 /* Fix up fields of a decl_non_common T. */
311 static void
312 lto_ft_decl_non_common (tree t)
314 lto_ft_decl_with_vis (t);
315 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t));
316 LTO_FIXUP_TREE (DECL_RESULT_FLD (t));
317 LTO_FIXUP_TREE (DECL_VINDEX (t));
320 /* Fix up fields of a decl_non_common T. */
322 static void
323 lto_ft_function (tree t)
325 lto_ft_decl_non_common (t);
326 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t));
329 /* Fix up fields of a field_decl T. */
331 static void
332 lto_ft_field_decl (tree t)
334 lto_ft_decl_common (t);
335 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t));
336 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t));
337 LTO_FIXUP_TREE (DECL_QUALIFIER (t));
338 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t));
339 LTO_FIXUP_TREE (DECL_FCONTEXT (t));
342 /* Fix up fields of a type T. */
344 static void
345 lto_ft_type (tree t)
347 lto_ft_common (t);
348 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t));
349 LTO_FIXUP_TREE (TYPE_SIZE (t));
350 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t));
351 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t));
352 LTO_FIXUP_TREE (TYPE_NAME (t));
354 /* Accessors are for derived node types only. */
355 if (!POINTER_TYPE_P (t))
356 LTO_FIXUP_TREE (TYPE_MINVAL (t));
357 LTO_FIXUP_TREE (TYPE_MAXVAL (t));
359 /* Accessor is for derived node types only. */
360 LTO_FIXUP_TREE (t->type_non_common.binfo);
362 LTO_FIXUP_TREE (TYPE_CONTEXT (t));
365 /* Fix up fields of a BINFO T. */
367 static void
368 lto_ft_binfo (tree t)
370 unsigned HOST_WIDE_INT i, n;
371 tree base, saved_base;
373 lto_ft_common (t);
374 LTO_FIXUP_TREE (BINFO_VTABLE (t));
375 LTO_FIXUP_TREE (BINFO_OFFSET (t));
376 LTO_FIXUP_TREE (BINFO_VIRTUALS (t));
377 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t));
378 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
379 for (i = 0; i < n; i++)
381 saved_base = base = BINFO_BASE_ACCESS (t, i);
382 LTO_FIXUP_TREE (base);
383 if (base != saved_base)
384 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
386 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t));
387 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t));
388 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t));
389 n = BINFO_N_BASE_BINFOS (t);
390 for (i = 0; i < n; i++)
392 saved_base = base = BINFO_BASE_BINFO (t, i);
393 LTO_FIXUP_TREE (base);
394 if (base != saved_base)
395 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
399 /* Fix up fields of a CONSTRUCTOR T. */
401 static void
402 lto_ft_constructor (tree t)
404 unsigned HOST_WIDE_INT idx;
405 constructor_elt *ce;
407 lto_ft_typed (t);
409 for (idx = 0;
410 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
411 idx++)
413 LTO_FIXUP_TREE (ce->index);
414 LTO_FIXUP_TREE (ce->value);
418 /* Fix up fields of an expression tree T. */
420 static void
421 lto_ft_expr (tree t)
423 int i;
424 lto_ft_typed (t);
425 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
426 LTO_FIXUP_TREE (TREE_OPERAND (t, i));
429 /* Given a tree T fixup fields of T by replacing types with their merged
430 variant and other entities by an equal entity from an earlier compilation
431 unit, or an entity being canonical in a different way. This includes
432 for instance integer or string constants. */
434 static void
435 lto_fixup_types (tree t)
437 switch (TREE_CODE (t))
439 case IDENTIFIER_NODE:
440 break;
442 case TREE_LIST:
443 LTO_FIXUP_TREE (TREE_VALUE (t));
444 LTO_FIXUP_TREE (TREE_PURPOSE (t));
445 LTO_FIXUP_TREE (TREE_CHAIN (t));
446 break;
448 case FIELD_DECL:
449 lto_ft_field_decl (t);
450 break;
452 case LABEL_DECL:
453 case CONST_DECL:
454 case PARM_DECL:
455 case RESULT_DECL:
456 case IMPORTED_DECL:
457 lto_ft_decl_common (t);
458 break;
460 case VAR_DECL:
461 lto_ft_decl_with_vis (t);
462 break;
464 case TYPE_DECL:
465 lto_ft_decl_non_common (t);
466 break;
468 case FUNCTION_DECL:
469 lto_ft_function (t);
470 break;
472 case TREE_BINFO:
473 lto_ft_binfo (t);
474 break;
476 case PLACEHOLDER_EXPR:
477 lto_ft_common (t);
478 break;
480 case BLOCK:
481 case TRANSLATION_UNIT_DECL:
482 case OPTIMIZATION_NODE:
483 case TARGET_OPTION_NODE:
484 break;
486 default:
487 if (TYPE_P (t))
488 lto_ft_type (t);
489 else if (TREE_CODE (t) == CONSTRUCTOR)
490 lto_ft_constructor (t);
491 else if (CONSTANT_CLASS_P (t))
492 LTO_FIXUP_TREE (TREE_TYPE (t));
493 else if (EXPR_P (t))
495 lto_ft_expr (t);
497 else
499 remember_with_vars (t);
505 /* Return the resolution for the decl with index INDEX from DATA_IN. */
507 static enum ld_plugin_symbol_resolution
508 get_resolution (struct data_in *data_in, unsigned index)
510 if (data_in->globals_resolution)
512 ld_plugin_symbol_resolution_t ret;
513 /* We can have references to not emitted functions in
514 DECL_FUNCTION_PERSONALITY at least. So we can and have
515 to indeed return LDPR_UNKNOWN in some cases. */
516 if (VEC_length (ld_plugin_symbol_resolution_t,
517 data_in->globals_resolution) <= index)
518 return LDPR_UNKNOWN;
519 ret = VEC_index (ld_plugin_symbol_resolution_t,
520 data_in->globals_resolution,
521 index);
522 return ret;
524 else
525 /* Delay resolution finding until decl merging. */
526 return LDPR_UNKNOWN;
530 /* Register DECL with the global symbol table and change its
531 name if necessary to avoid name clashes for static globals across
532 different files. */
534 static void
535 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl)
537 tree context;
539 /* Variable has file scope, not local. Need to ensure static variables
540 between different files don't clash unexpectedly. */
541 if (!TREE_PUBLIC (decl)
542 && !((context = decl_function_context (decl))
543 && auto_var_in_fn_p (decl, context)))
545 /* ??? We normally pre-mangle names before we serialize them
546 out. Here, in lto1, we do not know the language, and
547 thus cannot do the mangling again. Instead, we just
548 append a suffix to the mangled name. The resulting name,
549 however, is not a properly-formed mangled name, and will
550 confuse any attempt to unmangle it. */
551 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
552 char *label;
554 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
555 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
556 rest_of_decl_compilation (decl, 1, 0);
557 VEC_safe_push (tree, gc, lto_global_var_decls, decl);
560 /* If this variable has already been declared, queue the
561 declaration for merging. */
562 if (TREE_PUBLIC (decl))
564 unsigned ix;
565 if (!lto_streamer_cache_lookup (data_in->reader_cache, decl, &ix))
566 gcc_unreachable ();
567 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
568 data_in->file_data);
573 /* Register DECL with the global symbol table and change its
574 name if necessary to avoid name clashes for static globals across
575 different files. DATA_IN contains descriptors and tables for the
576 file being read. */
578 static void
579 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl)
581 /* Need to ensure static entities between different files
582 don't clash unexpectedly. */
583 if (!TREE_PUBLIC (decl))
585 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
586 may set the assembler name where it was previously empty. */
587 tree old_assembler_name = decl->decl_with_vis.assembler_name;
589 /* FIXME lto: We normally pre-mangle names before we serialize
590 them out. Here, in lto1, we do not know the language, and
591 thus cannot do the mangling again. Instead, we just append a
592 suffix to the mangled name. The resulting name, however, is
593 not a properly-formed mangled name, and will confuse any
594 attempt to unmangle it. */
595 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
596 char *label;
598 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
599 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
601 /* We may arrive here with the old assembler name not set
602 if the function body is not needed, e.g., it has been
603 inlined away and does not appear in the cgraph. */
604 if (old_assembler_name)
606 tree new_assembler_name = DECL_ASSEMBLER_NAME (decl);
608 /* Make the original assembler name available for later use.
609 We may have used it to indicate the section within its
610 object file where the function body may be found.
611 FIXME lto: Find a better way to maintain the function decl
612 to body section mapping so we don't need this hack. */
613 lto_record_renamed_decl (data_in->file_data,
614 IDENTIFIER_POINTER (old_assembler_name),
615 IDENTIFIER_POINTER (new_assembler_name));
617 /* Also register the reverse mapping so that we can find the
618 new name given to an existing assembler name (used when
619 restoring alias pairs in input_constructors_or_inits. */
620 lto_record_renamed_decl (data_in->file_data,
621 IDENTIFIER_POINTER (new_assembler_name),
622 IDENTIFIER_POINTER (old_assembler_name));
626 /* If this variable has already been declared, queue the
627 declaration for merging. */
628 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
630 unsigned ix;
631 if (!lto_streamer_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 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
640 for one compilation unit) go over all trees starting at index FROM until the
641 end of the sequence and replace fields of those trees, and the trees
642 themself with their canonical variants as per gimple_register_type. */
644 static void
645 uniquify_nodes (struct data_in *data_in, unsigned from)
647 struct lto_streamer_cache_d *cache = data_in->reader_cache;
648 unsigned len = VEC_length (tree, cache->nodes);
649 unsigned i;
651 /* Go backwards because children streamed for the first time come
652 as part of their parents, and hence are created after them. */
654 /* First register all the types in the cache. This makes sure to
655 have the original structure in the type cycles when registering
656 them and computing hashes. */
657 for (i = len; i-- > from;)
659 tree t = VEC_index (tree, cache->nodes, i);
660 if (t && TYPE_P (t))
661 gimple_register_type (t);
664 /* Second fixup all trees in the new cache entries. */
665 for (i = len; i-- > from;)
667 tree t = VEC_index (tree, cache->nodes, i);
668 tree oldt = t;
669 if (!t)
670 continue;
672 /* First fixup the fields of T. */
673 lto_fixup_types (t);
675 if (!TYPE_P (t))
676 continue;
678 /* Now try to find a canonical variant of T itself. */
679 t = gimple_register_type (t);
681 if (t == oldt)
683 /* The following re-creates proper variant lists while fixing up
684 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
685 variant list state before fixup is broken. */
686 tree tem, mv;
688 /* Remove us from our main variant list if we are not the
689 variant leader. */
690 if (TYPE_MAIN_VARIANT (t) != t)
692 tem = TYPE_MAIN_VARIANT (t);
693 while (tem && TYPE_NEXT_VARIANT (tem) != t)
694 tem = TYPE_NEXT_VARIANT (tem);
695 if (tem)
696 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
697 TYPE_NEXT_VARIANT (t) = NULL_TREE;
700 /* Query our new main variant. */
701 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
703 /* If we were the variant leader and we get replaced ourselves drop
704 all variants from our list. */
705 if (TYPE_MAIN_VARIANT (t) == t
706 && mv != t)
708 tem = t;
709 while (tem)
711 tree tem2 = TYPE_NEXT_VARIANT (tem);
712 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
713 tem = tem2;
717 /* If we are not our own variant leader link us into our new leaders
718 variant list. */
719 if (mv != t)
721 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
722 TYPE_NEXT_VARIANT (mv) = t;
725 /* Finally adjust our main variant and fix it up. */
726 TYPE_MAIN_VARIANT (t) = mv;
728 /* The following reconstructs the pointer chains
729 of the new pointed-to type if we are a main variant. We do
730 not stream those so they are broken before fixup. */
731 if (TREE_CODE (t) == POINTER_TYPE
732 && TYPE_MAIN_VARIANT (t) == t)
734 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
735 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
737 else if (TREE_CODE (t) == REFERENCE_TYPE
738 && TYPE_MAIN_VARIANT (t) == t)
740 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
741 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
745 else
747 if (RECORD_OR_UNION_TYPE_P (t))
749 tree f1, f2;
750 if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt))
751 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt);
752 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
754 unsigned ix;
755 gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2));
756 if (!lto_streamer_cache_lookup (cache, f2, &ix))
757 gcc_unreachable ();
758 /* If we're going to replace an element which we'd
759 still visit in the next iterations, we wouldn't
760 handle it, so do it here. We do have to handle it
761 even though the field_decl itself will be removed,
762 as it could refer to e.g. integer_cst which we
763 wouldn't reach via any other way, hence they
764 (and their type) would stay uncollected. */
765 /* ??? We should rather make sure to replace all
766 references to f2 with f1. That means handling
767 COMPONENT_REFs and CONSTRUCTOR elements in
768 lto_fixup_types and special-case the field-decl
769 operand handling. */
770 if (ix < i)
771 lto_fixup_types (f2);
772 lto_streamer_cache_insert_at (cache, f1, ix);
776 /* If we found a tree that is equal to oldt replace it in the
777 cache, so that further users (in the various LTO sections)
778 make use of it. */
779 lto_streamer_cache_insert_at (cache, t, i);
783 /* Finally compute the canonical type of all TREE_TYPEs and register
784 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
785 From this point there are no longer any types with
786 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
787 This step requires the TYPE_POINTER_TO lists being present, so
788 make sure it is done last. */
789 for (i = len; i-- > from;)
791 tree t = VEC_index (tree, cache->nodes, i);
792 if (t == NULL_TREE)
793 continue;
795 if (TREE_CODE (t) == VAR_DECL)
796 lto_register_var_decl_in_symtab (data_in, t);
797 else if (TREE_CODE (t) == FUNCTION_DECL && !DECL_BUILT_IN (t))
798 lto_register_function_decl_in_symtab (data_in, t);
799 else if (TYPE_P (t) && !TYPE_CANONICAL (t))
800 TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
805 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
806 RESOLUTIONS is the set of symbols picked by the linker (read from the
807 resolution file when the linker plugin is being used). */
809 static void
810 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
811 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
813 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
814 const int32_t decl_offset = sizeof (struct lto_decl_header);
815 const int32_t main_offset = decl_offset + header->decl_state_size;
816 const int32_t string_offset = main_offset + header->main_size;
817 struct lto_input_block ib_main;
818 struct data_in *data_in;
819 unsigned int i;
820 const uint32_t *data_ptr, *data_end;
821 uint32_t num_decl_states;
823 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
824 header->main_size);
826 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
827 header->string_size, resolutions);
829 /* Read the global declarations and types. */
830 while (ib_main.p < ib_main.len)
832 tree t;
833 unsigned from = VEC_length (tree, data_in->reader_cache->nodes);
834 t = lto_input_tree (&ib_main, data_in);
835 gcc_assert (t && ib_main.p <= ib_main.len);
836 uniquify_nodes (data_in, from);
839 /* Read in lto_in_decl_state objects. */
840 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
841 data_end =
842 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
843 num_decl_states = *data_ptr++;
845 gcc_assert (num_decl_states > 0);
846 decl_data->global_decl_state = lto_new_in_decl_state ();
847 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
848 decl_data->global_decl_state);
850 /* Read in per-function decl states and enter them in hash table. */
851 decl_data->function_decl_states =
852 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
854 for (i = 1; i < num_decl_states; i++)
856 struct lto_in_decl_state *state = lto_new_in_decl_state ();
857 void **slot;
859 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
860 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
861 gcc_assert (*slot == NULL);
862 *slot = state;
865 if (data_ptr != data_end)
866 internal_error ("bytecode stream: garbage at the end of symbols section");
868 /* Set the current decl state to be the global state. */
869 decl_data->current_decl_state = decl_data->global_decl_state;
871 lto_data_in_delete (data_in);
874 /* strtoll is not portable. */
875 int64_t
876 lto_parse_hex (const char *p) {
877 uint64_t ret = 0;
878 for (; *p != '\0'; ++p)
880 char c = *p;
881 unsigned char part;
882 ret <<= 4;
883 if (c >= '0' && c <= '9')
884 part = c - '0';
885 else if (c >= 'a' && c <= 'f')
886 part = c - 'a' + 10;
887 else if (c >= 'A' && c <= 'F')
888 part = c - 'A' + 10;
889 else
890 internal_error ("could not parse hex number");
891 ret |= part;
893 return ret;
896 /* Read resolution for file named FILE_NAME. The resolution is read from
897 RESOLUTION. */
899 static void
900 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
902 /* We require that objects in the resolution file are in the same
903 order as the lto1 command line. */
904 unsigned int name_len;
905 char *obj_name;
906 unsigned int num_symbols;
907 unsigned int i;
908 struct lto_file_decl_data *file_data;
909 unsigned max_index = 0;
910 splay_tree_node nd = NULL;
912 if (!resolution)
913 return;
915 name_len = strlen (file->filename);
916 obj_name = XNEWVEC (char, name_len + 1);
917 fscanf (resolution, " "); /* Read white space. */
919 fread (obj_name, sizeof (char), name_len, resolution);
920 obj_name[name_len] = '\0';
921 if (filename_cmp (obj_name, file->filename) != 0)
922 internal_error ("unexpected file name %s in linker resolution file. "
923 "Expected %s", obj_name, file->filename);
924 if (file->offset != 0)
926 int t;
927 char offset_p[17];
928 int64_t offset;
929 t = fscanf (resolution, "@0x%16s", offset_p);
930 if (t != 1)
931 internal_error ("could not parse file offset");
932 offset = lto_parse_hex (offset_p);
933 if (offset != file->offset)
934 internal_error ("unexpected offset");
937 free (obj_name);
939 fscanf (resolution, "%u", &num_symbols);
941 for (i = 0; i < num_symbols; i++)
943 int t;
944 unsigned index, id;
945 char r_str[27];
946 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
947 unsigned int j;
948 unsigned int lto_resolution_str_len =
949 sizeof (lto_resolution_str) / sizeof (char *);
951 t = fscanf (resolution, "%u %x %26s %*[^\n]\n", &index, &id, r_str);
952 if (t != 3)
953 internal_error ("invalid line in the resolution file");
954 if (index > max_index)
955 max_index = index;
957 for (j = 0; j < lto_resolution_str_len; j++)
959 if (strcmp (lto_resolution_str[j], r_str) == 0)
961 r = (enum ld_plugin_symbol_resolution) j;
962 break;
965 if (j == lto_resolution_str_len)
966 internal_error ("invalid resolution in the resolution file");
968 if (!(nd && nd->key == id))
970 nd = splay_tree_lookup (file_ids, id);
971 if (nd == NULL)
972 internal_error ("resolution sub id %x not in object file", id);
975 file_data = (struct lto_file_decl_data *)nd->value;
976 if (cgraph_dump_file)
977 fprintf (cgraph_dump_file, "Adding resolution %u %u to id %x\n",
978 index, r, file_data->id);
979 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap,
980 file_data->resolutions,
981 max_index + 1);
982 VEC_replace (ld_plugin_symbol_resolution_t,
983 file_data->resolutions, index, r);
987 /* Is the name for a id'ed LTO section? */
989 static int
990 lto_section_with_id (const char *name, unsigned *id)
992 const char *s;
994 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
995 return 0;
996 s = strrchr (name, '.');
997 return s && sscanf (s, ".%x", id) == 1;
1000 /* Create file_data of each sub file id */
1002 static int
1003 create_subid_section_table (void **slot, void *data)
1005 struct lto_section_slot s_slot, *new_slot;
1006 struct lto_section_slot *ls = *(struct lto_section_slot **)slot;
1007 splay_tree file_ids = (splay_tree)data;
1008 unsigned id;
1009 splay_tree_node nd;
1010 void **hash_slot;
1011 char *new_name;
1012 struct lto_file_decl_data *file_data;
1014 if (!lto_section_with_id (ls->name, &id))
1015 return 1;
1017 /* Find hash table of sub module id */
1018 nd = splay_tree_lookup (file_ids, id);
1019 if (nd != NULL)
1021 file_data = (struct lto_file_decl_data *)nd->value;
1023 else
1025 file_data = ggc_alloc_lto_file_decl_data ();
1026 memset(file_data, 0, sizeof (struct lto_file_decl_data));
1027 file_data->id = id;
1028 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1029 splay_tree_insert (file_ids, id, (splay_tree_value)file_data);
1032 /* Copy section into sub module hash table */
1033 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
1034 s_slot.name = new_name;
1035 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
1036 gcc_assert (*hash_slot == NULL);
1038 new_slot = XDUP (struct lto_section_slot, ls);
1039 new_slot->name = new_name;
1040 *hash_slot = new_slot;
1041 return 1;
1044 /* Read declarations and other initializations for a FILE_DATA. */
1046 static void
1047 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
1049 const char *data;
1050 size_t len;
1052 file_data->renaming_hash_table = lto_create_renaming_table ();
1053 file_data->file_name = file->filename;
1054 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
1055 if (data == NULL)
1057 internal_error ("cannot read LTO decls from %s", file_data->file_name);
1058 return;
1060 lto_read_decls (file_data, data, file_data->resolutions);
1061 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
1064 struct lwstate
1066 lto_file *file;
1067 struct lto_file_decl_data **file_data;
1068 int *count;
1071 /* Traverse ids and create a list of file_datas out of it. */
1073 static int lto_create_files_from_ids (splay_tree_node node, void *data)
1075 struct lwstate *lw = (struct lwstate *)data;
1076 struct lto_file_decl_data *file_data = (struct lto_file_decl_data *)node->value;
1078 lto_file_finalize (file_data, lw->file);
1079 if (cgraph_dump_file)
1080 fprintf (cgraph_dump_file, "Creating file %s with sub id %x\n",
1081 file_data->file_name, file_data->id);
1082 file_data->next = *lw->file_data;
1083 *lw->file_data = file_data;
1084 (*lw->count)++;
1085 return 0;
1088 /* Generate a TREE representation for all types and external decls
1089 entities in FILE.
1091 Read all of the globals out of the file. Then read the cgraph
1092 and process the .o index into the cgraph nodes so that it can open
1093 the .o file to load the functions and ipa information. */
1095 static struct lto_file_decl_data *
1096 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
1098 struct lto_file_decl_data *file_data = NULL;
1099 splay_tree file_ids;
1100 htab_t section_hash_table;
1101 struct lwstate state;
1103 section_hash_table = lto_obj_build_section_table (file);
1105 /* Find all sub modules in the object and put their sections into new hash
1106 tables in a splay tree. */
1107 file_ids = splay_tree_new (splay_tree_compare_ints, NULL, NULL);
1108 htab_traverse (section_hash_table, create_subid_section_table, file_ids);
1110 /* Add resolutions to file ids */
1111 lto_resolution_read (file_ids, resolution_file, file);
1113 /* Finalize each lto file for each submodule in the merged object
1114 and create list for returning. */
1115 state.file = file;
1116 state.file_data = &file_data;
1117 state.count = count;
1118 splay_tree_foreach (file_ids, lto_create_files_from_ids, &state);
1120 splay_tree_delete (file_ids);
1121 htab_delete (section_hash_table);
1123 return file_data;
1126 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
1127 #define LTO_MMAP_IO 1
1128 #endif
1130 #if LTO_MMAP_IO
1131 /* Page size of machine is used for mmap and munmap calls. */
1132 static size_t page_mask;
1133 #endif
1135 /* Get the section data of length LEN from FILENAME starting at
1136 OFFSET. The data segment must be freed by the caller when the
1137 caller is finished. Returns NULL if all was not well. */
1139 static char *
1140 lto_read_section_data (struct lto_file_decl_data *file_data,
1141 intptr_t offset, size_t len)
1143 char *result;
1144 static int fd = -1;
1145 static char *fd_name;
1146 #if LTO_MMAP_IO
1147 intptr_t computed_len;
1148 intptr_t computed_offset;
1149 intptr_t diff;
1150 #endif
1152 /* Keep a single-entry file-descriptor cache. The last file we
1153 touched will get closed at exit.
1154 ??? Eventually we want to add a more sophisticated larger cache
1155 or rather fix function body streaming to not stream them in
1156 practically random order. */
1157 if (fd != -1
1158 && filename_cmp (fd_name, file_data->file_name) != 0)
1160 free (fd_name);
1161 close (fd);
1162 fd = -1;
1164 if (fd == -1)
1166 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
1167 if (fd == -1)
1168 return NULL;
1169 fd_name = xstrdup (file_data->file_name);
1172 #if LTO_MMAP_IO
1173 if (!page_mask)
1175 size_t page_size = sysconf (_SC_PAGE_SIZE);
1176 page_mask = ~(page_size - 1);
1179 computed_offset = offset & page_mask;
1180 diff = offset - computed_offset;
1181 computed_len = len + diff;
1183 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
1184 fd, computed_offset);
1185 if (result == MAP_FAILED)
1186 return NULL;
1188 return result + diff;
1189 #else
1190 result = (char *) xmalloc (len);
1191 if (lseek (fd, offset, SEEK_SET) != offset
1192 || read (fd, result, len) != (ssize_t) len)
1194 free (result);
1195 result = NULL;
1197 #ifdef __MINGW32__
1198 /* Native windows doesn't supports delayed unlink on opened file. So
1199 we close file here again. This produces higher I/O load, but at least
1200 it prevents to have dangling file handles preventing unlink. */
1201 free (fd_name);
1202 fd_name = NULL;
1203 close (fd);
1204 fd = -1;
1205 #endif
1206 return result;
1207 #endif
1211 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1212 NAME will be NULL unless the section type is for a function
1213 body. */
1215 static const char *
1216 get_section_data (struct lto_file_decl_data *file_data,
1217 enum lto_section_type section_type,
1218 const char *name,
1219 size_t *len)
1221 htab_t section_hash_table = file_data->section_hash_table;
1222 struct lto_section_slot *f_slot;
1223 struct lto_section_slot s_slot;
1224 const char *section_name = lto_get_section_name (section_type, name, file_data);
1225 char *data = NULL;
1227 *len = 0;
1228 s_slot.name = section_name;
1229 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
1230 if (f_slot)
1232 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
1233 *len = f_slot->len;
1236 free (CONST_CAST (char *, section_name));
1237 return data;
1241 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1242 starts at OFFSET and has LEN bytes. */
1244 static void
1245 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
1246 enum lto_section_type section_type ATTRIBUTE_UNUSED,
1247 const char *name ATTRIBUTE_UNUSED,
1248 const char *offset, size_t len ATTRIBUTE_UNUSED)
1250 #if LTO_MMAP_IO
1251 intptr_t computed_len;
1252 intptr_t computed_offset;
1253 intptr_t diff;
1254 #endif
1256 #if LTO_MMAP_IO
1257 computed_offset = ((intptr_t) offset) & page_mask;
1258 diff = (intptr_t) offset - computed_offset;
1259 computed_len = len + diff;
1261 munmap ((caddr_t) computed_offset, computed_len);
1262 #else
1263 free (CONST_CAST(char *, offset));
1264 #endif
1267 /* Structure describing ltrans partitions. */
1269 struct ltrans_partition_def
1271 cgraph_node_set cgraph_set;
1272 varpool_node_set varpool_set;
1273 const char * name;
1274 int insns;
1277 typedef struct ltrans_partition_def *ltrans_partition;
1278 DEF_VEC_P(ltrans_partition);
1279 DEF_VEC_ALLOC_P(ltrans_partition,heap);
1281 static VEC(ltrans_partition, heap) *ltrans_partitions;
1283 static void add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node);
1284 static void add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode);
1286 /* Create new partition with name NAME. */
1287 static ltrans_partition
1288 new_partition (const char *name)
1290 ltrans_partition part = XCNEW (struct ltrans_partition_def);
1291 part->cgraph_set = cgraph_node_set_new ();
1292 part->varpool_set = varpool_node_set_new ();
1293 part->name = name;
1294 part->insns = 0;
1295 VEC_safe_push (ltrans_partition, heap, ltrans_partitions, part);
1296 return part;
1299 /* Free memory used by ltrans datastructures. */
1300 static void
1301 free_ltrans_partitions (void)
1303 unsigned int idx;
1304 ltrans_partition part;
1305 for (idx = 0; VEC_iterate (ltrans_partition, ltrans_partitions, idx, part); idx++)
1307 free_cgraph_node_set (part->cgraph_set);
1308 free (part);
1310 VEC_free (ltrans_partition, heap, ltrans_partitions);
1313 /* See all references that go to comdat objects and bring them into partition too. */
1314 static void
1315 add_references_to_partition (ltrans_partition part, struct ipa_ref_list *refs)
1317 int i;
1318 struct ipa_ref *ref;
1319 for (i = 0; ipa_ref_list_reference_iterate (refs, i, ref); i++)
1321 if (ref->refered_type == IPA_REF_CGRAPH
1322 && DECL_COMDAT (ipa_ref_node (ref)->decl)
1323 && !cgraph_node_in_set_p (ipa_ref_node (ref), part->cgraph_set))
1324 add_cgraph_node_to_partition (part, ipa_ref_node (ref));
1325 else
1326 if (ref->refered_type == IPA_REF_VARPOOL
1327 && DECL_COMDAT (ipa_ref_varpool_node (ref)->decl)
1328 && !varpool_node_in_set_p (ipa_ref_varpool_node (ref), part->varpool_set))
1329 add_varpool_node_to_partition (part, ipa_ref_varpool_node (ref));
1333 /* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */
1335 static void
1336 add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node)
1338 struct cgraph_edge *e;
1339 cgraph_node_set_iterator csi;
1341 /* If NODE is already there, we have nothing to do. */
1342 csi = cgraph_node_set_find (part->cgraph_set, node);
1343 if (!csi_end_p (csi))
1344 return;
1346 part->insns += inline_summary (node)->self_size;
1348 if (node->aux)
1350 node->in_other_partition = 1;
1351 if (cgraph_dump_file)
1352 fprintf (cgraph_dump_file, "Node %s/%i now used in multiple partitions\n",
1353 cgraph_node_name (node), node->uid);
1355 node->aux = (void *)((size_t)node->aux + 1);
1357 cgraph_node_set_add (part->cgraph_set, node);
1359 /* Thunks always must go along with function they reffer to. */
1360 if (node->thunk.thunk_p)
1361 add_cgraph_node_to_partition (part, node->callees->callee);
1362 for (e = node->callers; e; e = e->next_caller)
1363 if (e->caller->thunk.thunk_p)
1364 add_cgraph_node_to_partition (part, e->caller);
1366 for (e = node->callees; e; e = e->next_callee)
1367 if ((!e->inline_failed || DECL_COMDAT (e->callee->decl))
1368 && !cgraph_node_in_set_p (e->callee, part->cgraph_set))
1369 add_cgraph_node_to_partition (part, e->callee);
1371 add_references_to_partition (part, &node->ref_list);
1373 if (node->same_comdat_group
1374 && !cgraph_node_in_set_p (node->same_comdat_group, part->cgraph_set))
1375 add_cgraph_node_to_partition (part, node->same_comdat_group);
1378 /* Add VNODE to partition as well as comdat references partition PART. */
1380 static void
1381 add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode)
1383 varpool_node_set_iterator vsi;
1385 /* If NODE is already there, we have nothing to do. */
1386 vsi = varpool_node_set_find (part->varpool_set, vnode);
1387 if (!vsi_end_p (vsi))
1388 return;
1390 varpool_node_set_add (part->varpool_set, vnode);
1392 if (vnode->aux)
1394 vnode->in_other_partition = 1;
1395 if (cgraph_dump_file)
1396 fprintf (cgraph_dump_file, "Varpool node %s now used in multiple partitions\n",
1397 varpool_node_name (vnode));
1399 vnode->aux = (void *)((size_t)vnode->aux + 1);
1401 add_references_to_partition (part, &vnode->ref_list);
1403 if (vnode->same_comdat_group
1404 && !varpool_node_in_set_p (vnode->same_comdat_group, part->varpool_set))
1405 add_varpool_node_to_partition (part, vnode->same_comdat_group);
1408 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
1409 and number of varpool nodes is N_VARPOOL_NODES. */
1411 static void
1412 undo_partition (ltrans_partition partition, unsigned int n_cgraph_nodes,
1413 unsigned int n_varpool_nodes)
1415 while (VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes) >
1416 n_cgraph_nodes)
1418 struct cgraph_node *node = VEC_index (cgraph_node_ptr,
1419 partition->cgraph_set->nodes,
1420 n_cgraph_nodes);
1421 partition->insns -= inline_summary (node)->self_size;
1422 cgraph_node_set_remove (partition->cgraph_set, node);
1423 node->aux = (void *)((size_t)node->aux - 1);
1425 while (VEC_length (varpool_node_ptr, partition->varpool_set->nodes) >
1426 n_varpool_nodes)
1428 struct varpool_node *node = VEC_index (varpool_node_ptr,
1429 partition->varpool_set->nodes,
1430 n_varpool_nodes);
1431 varpool_node_set_remove (partition->varpool_set, node);
1432 node->aux = (void *)((size_t)node->aux - 1);
1436 /* Return true if NODE should be partitioned.
1437 This means that partitioning algorithm should put NODE into one of partitions.
1438 This apply to most functions with bodies. Functions that are not partitions
1439 are put into every unit needing them. This is the case of i.e. COMDATs. */
1441 static bool
1442 partition_cgraph_node_p (struct cgraph_node *node)
1444 /* We will get proper partition based on function they are inlined to. */
1445 if (node->global.inlined_to)
1446 return false;
1447 /* Nodes without a body do not need partitioning. */
1448 if (!node->analyzed)
1449 return false;
1450 /* Extern inlines and comdat are always only in partitions they are needed. */
1451 if (DECL_EXTERNAL (node->decl)
1452 || (DECL_COMDAT (node->decl)
1453 && !cgraph_used_from_object_file_p (node)))
1454 return false;
1455 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
1456 return false;
1457 return true;
1460 /* Return true if VNODE should be partitioned.
1461 This means that partitioning algorithm should put VNODE into one of partitions. */
1463 static bool
1464 partition_varpool_node_p (struct varpool_node *vnode)
1466 if (vnode->alias || !vnode->needed)
1467 return false;
1468 /* Constant pool and comdat are always only in partitions they are needed. */
1469 if (DECL_IN_CONSTANT_POOL (vnode->decl)
1470 || (DECL_COMDAT (vnode->decl)
1471 && !vnode->force_output
1472 && !varpool_used_from_object_file_p (vnode)))
1473 return false;
1474 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->decl)))
1475 return false;
1476 return true;
1479 /* Group cgrah nodes by input files. This is used mainly for testing
1480 right now. */
1482 static void
1483 lto_1_to_1_map (void)
1485 struct cgraph_node *node;
1486 struct varpool_node *vnode;
1487 struct lto_file_decl_data *file_data;
1488 struct pointer_map_t *pmap;
1489 ltrans_partition partition;
1490 void **slot;
1491 int npartitions = 0;
1493 timevar_push (TV_WHOPR_WPA);
1495 pmap = pointer_map_create ();
1497 for (node = cgraph_nodes; node; node = node->next)
1499 if (!partition_cgraph_node_p (node))
1500 continue;
1502 file_data = node->local.lto_file_data;
1503 gcc_assert (!node->same_body_alias);
1505 if (file_data)
1507 slot = pointer_map_contains (pmap, file_data);
1508 if (slot)
1509 partition = (ltrans_partition) *slot;
1510 else
1512 partition = new_partition (file_data->file_name);
1513 slot = pointer_map_insert (pmap, file_data);
1514 *slot = partition;
1515 npartitions++;
1518 else if (!file_data
1519 && VEC_length (ltrans_partition, ltrans_partitions))
1520 partition = VEC_index (ltrans_partition, ltrans_partitions, 0);
1521 else
1523 partition = new_partition ("");
1524 slot = pointer_map_insert (pmap, NULL);
1525 *slot = partition;
1526 npartitions++;
1529 if (!node->aux)
1530 add_cgraph_node_to_partition (partition, node);
1533 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1535 if (!partition_varpool_node_p (vnode))
1536 continue;
1537 file_data = vnode->lto_file_data;
1538 slot = pointer_map_contains (pmap, file_data);
1539 if (slot)
1540 partition = (ltrans_partition) *slot;
1541 else
1543 partition = new_partition (file_data->file_name);
1544 slot = pointer_map_insert (pmap, file_data);
1545 *slot = partition;
1546 npartitions++;
1549 if (!vnode->aux)
1550 add_varpool_node_to_partition (partition, vnode);
1552 for (node = cgraph_nodes; node; node = node->next)
1553 node->aux = NULL;
1554 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1555 vnode->aux = NULL;
1557 /* If the cgraph is empty, create one cgraph node set so that there is still
1558 an output file for any variables that need to be exported in a DSO. */
1559 if (!npartitions)
1560 new_partition ("empty");
1562 pointer_map_destroy (pmap);
1564 timevar_pop (TV_WHOPR_WPA);
1566 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
1567 ltrans_partitions);
1571 /* Group cgraph nodes into equally-sized partitions.
1573 The partitioning algorithm is simple: nodes are taken in predefined order.
1574 The order corresponds to the order we want functions to have in the final
1575 output. In the future this will be given by function reordering pass, but
1576 at the moment we use the topological order, which is a good approximation.
1578 The goal is to partition this linear order into intervals (partitions) so
1579 that all the partitions have approximately the same size and the number of
1580 callgraph or IPA reference edges crossing boundaries is minimal.
1582 This is a lot faster (O(n) in size of callgraph) than algorithms doing
1583 priority-based graph clustering that are generally O(n^2) and, since
1584 WHOPR is designed to make things go well across partitions, it leads
1585 to good results.
1587 We compute the expected size of a partition as:
1589 max (total_size / lto_partitions, min_partition_size)
1591 We use dynamic expected size of partition so small programs are partitioned
1592 into enough partitions to allow use of multiple CPUs, while large programs
1593 are not partitioned too much. Creating too many partitions significantly
1594 increases the streaming overhead.
1596 In the future, we would like to bound the maximal size of partitions so as
1597 to prevent the LTRANS stage from consuming too much memory. At the moment,
1598 however, the WPA stage is the most memory intensive for large benchmarks,
1599 since too many types and declarations are read into memory.
1601 The function implements a simple greedy algorithm. Nodes are being added
1602 to the current partition until after 3/4 of the expected partition size is
1603 reached. Past this threshold, we keep track of boundary size (number of
1604 edges going to other partitions) and continue adding functions until after
1605 the current partition has grown to twice the expected partition size. Then
1606 the process is undone to the point where the minimal ratio of boundary size
1607 and in-partition calls was reached. */
1609 static void
1610 lto_balanced_map (void)
1612 int n_nodes = 0;
1613 struct cgraph_node **postorder =
1614 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1615 struct cgraph_node **order = XNEWVEC (struct cgraph_node *, cgraph_max_uid);
1616 int i, postorder_len;
1617 struct cgraph_node *node;
1618 int total_size = 0, best_total_size = 0;
1619 int partition_size;
1620 ltrans_partition partition;
1621 unsigned int last_visited_cgraph_node = 0, last_visited_varpool_node = 0;
1622 struct varpool_node *vnode;
1623 int cost = 0, internal = 0;
1624 int best_n_nodes = 0, best_n_varpool_nodes = 0, best_i = 0, best_cost =
1625 INT_MAX, best_internal = 0;
1626 int npartitions;
1628 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1629 gcc_assert (!vnode->aux);
1630 /* Until we have better ordering facility, use toplogical order.
1631 Include only nodes we will partition and compute estimate of program
1632 size. Note that since nodes that are not partitioned might be put into
1633 multiple partitions, this is just an estimate of real size. This is why
1634 we keep partition_size updated after every partition is finalized. */
1635 postorder_len = ipa_reverse_postorder (postorder);
1636 for (i = 0; i < postorder_len; i++)
1638 node = postorder[i];
1639 if (partition_cgraph_node_p (node))
1641 order[n_nodes++] = node;
1642 total_size += inline_summary (node)->size;
1645 free (postorder);
1647 /* Compute partition size and create the first partition. */
1648 partition_size = total_size / PARAM_VALUE (PARAM_LTO_PARTITIONS);
1649 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1650 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1651 npartitions = 1;
1652 partition = new_partition ("");
1653 if (cgraph_dump_file)
1654 fprintf (cgraph_dump_file, "Total unit size: %i, partition size: %i\n",
1655 total_size, partition_size);
1657 for (i = 0; i < n_nodes; i++)
1659 if (!order[i]->aux)
1660 add_cgraph_node_to_partition (partition, order[i]);
1661 total_size -= inline_summary (order[i])->size;
1663 /* Once we added a new node to the partition, we also want to add
1664 all referenced variables unless they was already added into some
1665 earlier partition.
1666 add_cgraph_node_to_partition adds possibly multiple nodes and
1667 variables that are needed to satisfy needs of ORDER[i].
1668 We remember last visited cgraph and varpool node from last iteration
1669 of outer loop that allows us to process every new addition.
1671 At the same time we compute size of the boundary into COST. Every
1672 callgraph or IPA reference edge leaving the partition contributes into
1673 COST. Every edge inside partition was earlier computed as one leaving
1674 it and thus we need to subtract it from COST. */
1675 while (last_visited_cgraph_node <
1676 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes)
1677 || last_visited_varpool_node < VEC_length (varpool_node_ptr,
1678 partition->varpool_set->
1679 nodes))
1681 struct ipa_ref_list *refs;
1682 int j;
1683 struct ipa_ref *ref;
1684 bool cgraph_p = false;
1686 if (last_visited_cgraph_node <
1687 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes))
1689 struct cgraph_edge *edge;
1691 cgraph_p = true;
1692 node = VEC_index (cgraph_node_ptr, partition->cgraph_set->nodes,
1693 last_visited_cgraph_node);
1694 refs = &node->ref_list;
1696 last_visited_cgraph_node++;
1698 gcc_assert (node->analyzed);
1700 /* Compute boundary cost of callgrpah edges. */
1701 for (edge = node->callees; edge; edge = edge->next_callee)
1702 if (edge->callee->analyzed)
1704 int edge_cost = edge->frequency;
1705 cgraph_node_set_iterator csi;
1707 if (!edge_cost)
1708 edge_cost = 1;
1709 gcc_assert (edge_cost > 0);
1710 csi = cgraph_node_set_find (partition->cgraph_set, edge->callee);
1711 if (!csi_end_p (csi)
1712 && csi.index < last_visited_cgraph_node - 1)
1713 cost -= edge_cost, internal+= edge_cost;
1714 else
1715 cost += edge_cost;
1717 for (edge = node->callers; edge; edge = edge->next_caller)
1719 int edge_cost = edge->frequency;
1720 cgraph_node_set_iterator csi;
1722 gcc_assert (edge->caller->analyzed);
1723 if (!edge_cost)
1724 edge_cost = 1;
1725 gcc_assert (edge_cost > 0);
1726 csi = cgraph_node_set_find (partition->cgraph_set, edge->caller);
1727 if (!csi_end_p (csi)
1728 && csi.index < last_visited_cgraph_node)
1729 cost -= edge_cost;
1730 else
1731 cost += edge_cost;
1734 else
1736 refs =
1737 &VEC_index (varpool_node_ptr, partition->varpool_set->nodes,
1738 last_visited_varpool_node)->ref_list;
1739 last_visited_varpool_node++;
1742 /* Compute boundary cost of IPA REF edges and at the same time look into
1743 variables referenced from current partition and try to add them. */
1744 for (j = 0; ipa_ref_list_reference_iterate (refs, j, ref); j++)
1745 if (ref->refered_type == IPA_REF_VARPOOL)
1747 varpool_node_set_iterator vsi;
1749 vnode = ipa_ref_varpool_node (ref);
1750 if (!vnode->finalized)
1751 continue;
1752 if (!vnode->aux && partition_varpool_node_p (vnode))
1753 add_varpool_node_to_partition (partition, vnode);
1754 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1755 if (!vsi_end_p (vsi)
1756 && vsi.index < last_visited_varpool_node - !cgraph_p)
1757 cost--, internal++;
1758 else
1759 cost++;
1761 else
1763 cgraph_node_set_iterator csi;
1765 node = ipa_ref_node (ref);
1766 if (!node->analyzed)
1767 continue;
1768 csi = cgraph_node_set_find (partition->cgraph_set, node);
1769 if (!csi_end_p (csi)
1770 && csi.index < last_visited_cgraph_node - cgraph_p)
1771 cost--, internal++;
1772 else
1773 cost++;
1775 for (j = 0; ipa_ref_list_refering_iterate (refs, j, ref); j++)
1776 if (ref->refering_type == IPA_REF_VARPOOL)
1778 varpool_node_set_iterator vsi;
1780 vnode = ipa_ref_refering_varpool_node (ref);
1781 gcc_assert (vnode->finalized);
1782 if (!vnode->aux && partition_varpool_node_p (vnode))
1783 add_varpool_node_to_partition (partition, vnode);
1784 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1785 if (!vsi_end_p (vsi)
1786 && vsi.index < last_visited_varpool_node)
1787 cost--;
1788 else
1789 cost++;
1791 else
1793 cgraph_node_set_iterator csi;
1795 node = ipa_ref_refering_node (ref);
1796 gcc_assert (node->analyzed);
1797 csi = cgraph_node_set_find (partition->cgraph_set, node);
1798 if (!csi_end_p (csi)
1799 && csi.index < last_visited_cgraph_node)
1800 cost--;
1801 else
1802 cost++;
1806 /* If the partition is large enough, start looking for smallest boundary cost. */
1807 if (partition->insns < partition_size * 3 / 4
1808 || best_cost == INT_MAX
1809 || ((!cost
1810 || (best_internal * (HOST_WIDE_INT) cost
1811 > (internal * (HOST_WIDE_INT)best_cost)))
1812 && partition->insns < partition_size * 5 / 4))
1814 best_cost = cost;
1815 best_internal = internal;
1816 best_i = i;
1817 best_n_nodes = VEC_length (cgraph_node_ptr,
1818 partition->cgraph_set->nodes);
1819 best_n_varpool_nodes = VEC_length (varpool_node_ptr,
1820 partition->varpool_set->nodes);
1821 best_total_size = total_size;
1823 if (cgraph_dump_file)
1824 fprintf (cgraph_dump_file, "Step %i: added %s/%i, size %i, cost %i/%i best %i/%i, step %i\n", i,
1825 cgraph_node_name (order[i]), order[i]->uid, partition->insns, cost, internal,
1826 best_cost, best_internal, best_i);
1827 /* Partition is too large, unwind into step when best cost was reached and
1828 start new partition. */
1829 if (partition->insns > 2 * partition_size)
1831 if (best_i != i)
1833 if (cgraph_dump_file)
1834 fprintf (cgraph_dump_file, "Unwinding %i insertions to step %i\n",
1835 i - best_i, best_i);
1836 undo_partition (partition, best_n_nodes, best_n_varpool_nodes);
1838 i = best_i;
1839 /* When we are finished, avoid creating empty partition. */
1840 if (i == n_nodes - 1)
1841 break;
1842 partition = new_partition ("");
1843 last_visited_cgraph_node = 0;
1844 last_visited_varpool_node = 0;
1845 total_size = best_total_size;
1846 cost = 0;
1848 if (cgraph_dump_file)
1849 fprintf (cgraph_dump_file, "New partition\n");
1850 best_n_nodes = 0;
1851 best_n_varpool_nodes = 0;
1852 best_cost = INT_MAX;
1854 /* Since the size of partitions is just approximate, update the size after
1855 we finished current one. */
1856 if (npartitions < PARAM_VALUE (PARAM_LTO_PARTITIONS))
1857 partition_size = total_size
1858 / (PARAM_VALUE (PARAM_LTO_PARTITIONS) - npartitions);
1859 else
1860 partition_size = INT_MAX;
1862 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1863 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1864 npartitions ++;
1868 /* Varables that are not reachable from the code go into last partition. */
1869 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1870 if (partition_varpool_node_p (vnode) && !vnode->aux)
1871 add_varpool_node_to_partition (partition, vnode);
1872 free (order);
1875 /* Promote variable VNODE to be static. */
1877 static bool
1878 promote_var (struct varpool_node *vnode)
1880 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
1881 return false;
1882 gcc_assert (flag_wpa);
1883 TREE_PUBLIC (vnode->decl) = 1;
1884 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
1885 DECL_VISIBILITY_SPECIFIED (vnode->decl) = true;
1886 if (cgraph_dump_file)
1887 fprintf (cgraph_dump_file,
1888 "Promoting var as hidden: %s\n", varpool_node_name (vnode));
1889 return true;
1892 /* Promote function NODE to be static. */
1894 static bool
1895 promote_fn (struct cgraph_node *node)
1897 gcc_assert (flag_wpa);
1898 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1899 return false;
1900 TREE_PUBLIC (node->decl) = 1;
1901 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
1902 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
1903 if (node->same_body)
1905 struct cgraph_node *alias;
1906 for (alias = node->same_body;
1907 alias; alias = alias->next)
1909 TREE_PUBLIC (alias->decl) = 1;
1910 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
1911 DECL_VISIBILITY_SPECIFIED (alias->decl) = true;
1914 if (cgraph_dump_file)
1915 fprintf (cgraph_dump_file,
1916 "Promoting function as hidden: %s/%i\n",
1917 cgraph_node_name (node), node->uid);
1918 return true;
1921 /* Find out all static decls that need to be promoted to global because
1922 of cross file sharing. This function must be run in the WPA mode after
1923 all inlinees are added. */
1925 static void
1926 lto_promote_cross_file_statics (void)
1928 struct varpool_node *vnode;
1929 unsigned i, n_sets;
1930 cgraph_node_set set;
1931 varpool_node_set vset;
1932 cgraph_node_set_iterator csi;
1933 varpool_node_set_iterator vsi;
1934 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
1935 struct pointer_set_t *inserted = pointer_set_create ();
1937 gcc_assert (flag_wpa);
1939 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
1940 for (i = 0; i < n_sets; i++)
1942 ltrans_partition part
1943 = VEC_index (ltrans_partition, ltrans_partitions, i);
1944 set = part->cgraph_set;
1945 vset = part->varpool_set;
1947 /* If node has either address taken (and we have no clue from where)
1948 or it is called from other partition, it needs to be globalized. */
1949 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
1951 struct cgraph_node *node = csi_node (csi);
1952 if (node->local.externally_visible)
1953 continue;
1954 if (node->global.inlined_to)
1955 continue;
1956 if ((!DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1957 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
1958 || reachable_from_other_partition_p (node, set)))
1959 promote_fn (node);
1961 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
1963 vnode = vsi_node (vsi);
1964 /* Constant pool references use internal labels and thus can not
1965 be made global. It is sensible to keep those ltrans local to
1966 allow better optimization. */
1967 if (!DECL_IN_CONSTANT_POOL (vnode->decl) && !DECL_COMDAT (vnode->decl)
1968 && !vnode->externally_visible && vnode->analyzed
1969 && referenced_from_other_partition_p (&vnode->ref_list,
1970 set, vset))
1971 promote_var (vnode);
1974 /* We export the initializer of a read-only var into each partition
1975 referencing the var. Folding might take declarations from the
1976 initializer and use them, so everything referenced from the
1977 initializer can be accessed from this partition after folding.
1979 This means that we need to promote all variables and functions
1980 referenced from all initializers of read-only vars referenced
1981 from this partition that are not in this partition. This needs
1982 to be done recursively. */
1983 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1984 if (const_value_known_p (vnode->decl)
1985 && DECL_INITIAL (vnode->decl)
1986 && !varpool_node_in_set_p (vnode, vset)
1987 && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
1988 && !pointer_set_insert (inserted, vnode))
1989 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
1991 while (!VEC_empty (varpool_node_ptr, promoted_initializers))
1993 int i;
1994 struct ipa_ref *ref;
1996 vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
1997 for (i = 0;
1998 ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref);
1999 i++)
2001 if (ref->refered_type == IPA_REF_CGRAPH)
2003 struct cgraph_node *n = ipa_ref_node (ref);
2004 gcc_assert (!n->global.inlined_to);
2005 if (!n->local.externally_visible
2006 && !cgraph_node_in_set_p (n, set))
2007 promote_fn (n);
2009 else
2011 struct varpool_node *v = ipa_ref_varpool_node (ref);
2012 if (varpool_node_in_set_p (v, vset))
2013 continue;
2015 /* Constant pool references use internal labels and thus
2016 cannot be made global. It is sensible to keep those
2017 ltrans local to allow better optimization. */
2018 if (DECL_IN_CONSTANT_POOL (v->decl))
2020 if (!pointer_set_insert (inserted, vnode))
2021 VEC_safe_push (varpool_node_ptr, heap,
2022 promoted_initializers, v);
2024 else if (!v->externally_visible && v->analyzed)
2026 if (promote_var (v)
2027 && DECL_INITIAL (v->decl)
2028 && const_value_known_p (v->decl)
2029 && !pointer_set_insert (inserted, vnode))
2030 VEC_safe_push (varpool_node_ptr, heap,
2031 promoted_initializers, v);
2037 pointer_set_destroy (inserted);
2040 static lto_file *current_lto_file;
2042 /* Helper for qsort; compare partitions and return one with smaller size.
2043 We sort from greatest to smallest so parallel build doesn't stale on the
2044 longest compilation being executed too late. */
2046 static int
2047 cmp_partitions (const void *a, const void *b)
2049 const struct ltrans_partition_def *pa
2050 = *(struct ltrans_partition_def *const *)a;
2051 const struct ltrans_partition_def *pb
2052 = *(struct ltrans_partition_def *const *)b;
2053 return pb->insns - pa->insns;
2056 /* Write all output files in WPA mode and the file with the list of
2057 LTRANS units. */
2059 static void
2060 lto_wpa_write_files (void)
2062 unsigned i, n_sets;
2063 lto_file *file;
2064 cgraph_node_set set;
2065 varpool_node_set vset;
2066 ltrans_partition part;
2067 FILE *ltrans_output_list_stream;
2068 char *temp_filename;
2069 size_t blen;
2071 /* Open the LTRANS output list. */
2072 if (!ltrans_output_list)
2073 fatal_error ("no LTRANS output list filename provided");
2074 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2075 if (ltrans_output_list_stream == NULL)
2076 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2078 timevar_push (TV_WHOPR_WPA);
2080 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
2081 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
2082 part->cgraph_set->nodes);
2084 /* Find out statics that need to be promoted
2085 to globals with hidden visibility because they are accessed from multiple
2086 partitions. */
2087 lto_promote_cross_file_statics ();
2089 timevar_pop (TV_WHOPR_WPA);
2091 timevar_push (TV_WHOPR_WPA_IO);
2093 /* Generate a prefix for the LTRANS unit files. */
2094 blen = strlen (ltrans_output_list);
2095 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2096 strcpy (temp_filename, ltrans_output_list);
2097 if (blen > sizeof (".out")
2098 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2099 ".out") == 0)
2100 temp_filename[blen - sizeof (".out") + 1] = '\0';
2101 blen = strlen (temp_filename);
2103 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
2104 VEC_qsort (ltrans_partition, ltrans_partitions, cmp_partitions);
2105 for (i = 0; i < n_sets; i++)
2107 size_t len;
2108 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
2110 set = part->cgraph_set;
2111 vset = part->varpool_set;
2113 /* Write all the nodes in SET. */
2114 sprintf (temp_filename + blen, "%u.o", i);
2115 file = lto_obj_file_open (temp_filename, true);
2116 if (!file)
2117 fatal_error ("lto_obj_file_open() failed");
2119 if (!quiet_flag)
2120 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2121 if (cgraph_dump_file)
2123 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
2124 part->name, temp_filename, part->insns);
2125 fprintf (cgraph_dump_file, "cgraph nodes:");
2126 dump_cgraph_node_set (cgraph_dump_file, set);
2127 fprintf (cgraph_dump_file, "varpool nodes:");
2128 dump_varpool_node_set (cgraph_dump_file, vset);
2130 gcc_checking_assert (cgraph_node_set_nonempty_p (set)
2131 || varpool_node_set_nonempty_p (vset) || !i);
2133 lto_set_current_out_file (file);
2135 ipa_write_optimization_summaries (set, vset);
2137 lto_set_current_out_file (NULL);
2138 lto_obj_file_close (file);
2140 len = strlen (temp_filename);
2141 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
2142 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2143 fatal_error ("writing to LTRANS output list %s: %m",
2144 ltrans_output_list);
2147 lto_stats.num_output_files += n_sets;
2149 /* Close the LTRANS output list. */
2150 if (fclose (ltrans_output_list_stream))
2151 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2153 free_ltrans_partitions();
2155 timevar_pop (TV_WHOPR_WPA_IO);
2159 /* If TT is a variable or function decl replace it with its
2160 prevailing variant. */
2161 #define LTO_SET_PREVAIL(tt) \
2162 do {\
2163 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
2164 tt = lto_symtab_prevailing_decl (tt); \
2165 } while (0)
2167 /* Ensure that TT isn't a replacable var of function decl. */
2168 #define LTO_NO_PREVAIL(tt) \
2169 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2171 /* Given a tree T replace all fields referring to variables or functions
2172 with their prevailing variant. */
2173 static void
2174 lto_fixup_prevailing_decls (tree t)
2176 enum tree_code code = TREE_CODE (t);
2177 LTO_NO_PREVAIL (TREE_TYPE (t));
2178 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2179 LTO_NO_PREVAIL (TREE_CHAIN (t));
2180 if (DECL_P (t))
2182 LTO_NO_PREVAIL (DECL_NAME (t));
2183 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2184 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2186 LTO_SET_PREVAIL (DECL_SIZE (t));
2187 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2188 LTO_SET_PREVAIL (DECL_INITIAL (t));
2189 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2190 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2192 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2194 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2195 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
2197 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2199 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
2200 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2201 LTO_NO_PREVAIL (DECL_VINDEX (t));
2203 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2204 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2205 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2207 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
2208 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2209 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2210 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2211 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2214 else if (TYPE_P (t))
2216 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2217 LTO_SET_PREVAIL (TYPE_SIZE (t));
2218 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2219 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2220 LTO_NO_PREVAIL (TYPE_NAME (t));
2222 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2223 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2224 LTO_SET_PREVAIL (t->type_non_common.binfo);
2226 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2228 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2229 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2230 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2232 else if (EXPR_P (t))
2234 int i;
2235 LTO_NO_PREVAIL (t->exp.block);
2236 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2237 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2239 else
2241 switch (code)
2243 case TREE_LIST:
2244 LTO_SET_PREVAIL (TREE_VALUE (t));
2245 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2246 break;
2247 default:
2248 gcc_unreachable ();
2252 #undef LTO_SET_PREVAIL
2253 #undef LTO_NO_PREVAIL
2255 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2256 replaces var and function decls with the corresponding prevailing def. */
2258 static void
2259 lto_fixup_state (struct lto_in_decl_state *state)
2261 unsigned i, si;
2262 struct lto_tree_ref_table *table;
2264 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2265 we still need to walk from all DECLs to find the reachable
2266 FUNCTION_DECLs and VAR_DECLs. */
2267 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2269 table = &state->streams[si];
2270 for (i = 0; i < table->size; i++)
2272 tree *tp = table->trees + i;
2273 if (VAR_OR_FUNCTION_DECL_P (*tp))
2274 *tp = lto_symtab_prevailing_decl (*tp);
2279 /* A callback of htab_traverse. Just extracts a state from SLOT
2280 and calls lto_fixup_state. */
2282 static int
2283 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
2285 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
2286 lto_fixup_state (state);
2287 return 1;
2290 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2291 prevailing one. */
2293 static void
2294 lto_fixup_decls (struct lto_file_decl_data **files)
2296 unsigned int i;
2297 htab_iterator hi;
2298 tree t;
2300 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
2301 lto_fixup_prevailing_decls (t);
2303 for (i = 0; files[i]; i++)
2305 struct lto_file_decl_data *file = files[i];
2306 struct lto_in_decl_state *state = file->global_decl_state;
2307 lto_fixup_state (state);
2309 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
2313 /* Read the options saved from each file in the command line. Called
2314 from lang_hooks.post_options which is called by process_options
2315 right before all the options are used to initialize the compiler.
2316 This assumes that decode_options has already run, so the
2317 num_in_fnames and in_fnames are properly set.
2319 Note that this assumes that all the files had been compiled with
2320 the same options, which is not a good assumption. In general,
2321 options ought to be read from all the files in the set and merged.
2322 However, it is still unclear what the merge rules should be. */
2324 void
2325 lto_read_all_file_options (void)
2327 size_t i;
2329 /* Clear any file options currently saved. */
2330 lto_clear_file_options ();
2332 /* Set the hooks to read ELF sections. */
2333 lto_set_in_hooks (NULL, get_section_data, free_section_data);
2334 if (!quiet_flag)
2335 fprintf (stderr, "Reading command line options:");
2337 for (i = 0; i < num_in_fnames; i++)
2339 struct lto_file_decl_data *file_data;
2340 lto_file *file = lto_obj_file_open (in_fnames[i], false);
2341 if (!file)
2342 break;
2343 if (!quiet_flag)
2345 fprintf (stderr, " %s", in_fnames[i]);
2346 fflush (stderr);
2349 file_data = XCNEW (struct lto_file_decl_data);
2350 file_data->file_name = file->filename;
2351 file_data->section_hash_table = lto_obj_build_section_table (file);
2353 lto_read_file_options (file_data);
2355 lto_obj_file_close (file);
2356 htab_delete (file_data->section_hash_table);
2357 free (file_data);
2360 if (!quiet_flag)
2361 fprintf (stderr, "\n");
2363 /* Apply globally the options read from all the files. */
2364 lto_reissue_options ();
2367 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2369 /* Turn file datas for sub files into a single array, so that they look
2370 like separate files for further passes. */
2372 static void
2373 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2375 struct lto_file_decl_data *n, *next;
2376 int i, k;
2378 lto_stats.num_input_files = count;
2379 all_file_decl_data
2380 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
2381 /* Set the hooks so that all of the ipa passes can read in their data. */
2382 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2383 for (i = 0, k = 0; i < last_file_ix; i++)
2385 for (n = orig[i]; n != NULL; n = next)
2387 all_file_decl_data[k++] = n;
2388 next = n->next;
2389 n->next = NULL;
2392 all_file_decl_data[k] = NULL;
2393 gcc_assert (k == count);
2396 /* Input file data before flattening (i.e. splitting them to subfiles to support
2397 incremental linking. */
2398 static int real_file_count;
2399 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2401 /* Read all the symbols from the input files FNAMES. NFILES is the
2402 number of files requested in the command line. Instantiate a
2403 global call graph by aggregating all the sub-graphs found in each
2404 file. */
2406 static void
2407 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2409 unsigned int i, last_file_ix;
2410 FILE *resolution;
2411 struct cgraph_node *node;
2412 int count = 0;
2413 struct lto_file_decl_data **decl_data;
2415 init_cgraph ();
2417 timevar_push (TV_IPA_LTO_DECL_IN);
2419 real_file_decl_data
2420 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
2421 real_file_count = nfiles;
2423 /* Read the resolution file. */
2424 resolution = NULL;
2425 if (resolution_file_name)
2427 int t;
2428 unsigned num_objects;
2430 resolution = fopen (resolution_file_name, "r");
2431 if (resolution == NULL)
2432 fatal_error ("could not open symbol resolution file: %m");
2434 t = fscanf (resolution, "%u", &num_objects);
2435 gcc_assert (t == 1);
2437 /* True, since the plugin splits the archives. */
2438 gcc_assert (num_objects == nfiles);
2441 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
2442 NULL);
2444 if (!quiet_flag)
2445 fprintf (stderr, "Reading object files:");
2447 /* Read all of the object files specified on the command line. */
2448 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2450 struct lto_file_decl_data *file_data = NULL;
2451 if (!quiet_flag)
2453 fprintf (stderr, " %s", fnames[i]);
2454 fflush (stderr);
2457 current_lto_file = lto_obj_file_open (fnames[i], false);
2458 if (!current_lto_file)
2459 break;
2461 file_data = lto_file_read (current_lto_file, resolution, &count);
2462 if (!file_data)
2464 lto_obj_file_close (current_lto_file);
2465 current_lto_file = NULL;
2466 break;
2469 decl_data[last_file_ix++] = file_data;
2471 lto_obj_file_close (current_lto_file);
2472 current_lto_file = NULL;
2473 ggc_collect ();
2476 lto_flatten_files (decl_data, count, last_file_ix);
2477 lto_stats.num_input_files = count;
2478 ggc_free(decl_data);
2479 real_file_decl_data = NULL;
2481 if (resolution_file_name)
2482 fclose (resolution);
2484 /* Set the hooks so that all of the ipa passes can read in their data. */
2485 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2487 timevar_pop (TV_IPA_LTO_DECL_IN);
2489 if (!quiet_flag)
2490 fprintf (stderr, "\nReading the callgraph\n");
2492 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2493 /* Read the callgraph. */
2494 input_cgraph ();
2495 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2497 if (!quiet_flag)
2498 fprintf (stderr, "Merging declarations\n");
2500 timevar_push (TV_IPA_LTO_DECL_MERGE);
2501 /* Merge global decls. */
2502 lto_symtab_merge_decls ();
2504 /* If there were errors during symbol merging bail out, we have no
2505 good way to recover here. */
2506 if (seen_error ())
2507 fatal_error ("errors during merging of translation units");
2509 /* Fixup all decls and types and free the type hash tables. */
2510 lto_fixup_decls (all_file_decl_data);
2511 htab_delete (tree_with_vars);
2512 tree_with_vars = NULL;
2513 free_gimple_type_tables ();
2514 ggc_collect ();
2516 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2517 /* Each pass will set the appropriate timer. */
2519 if (!quiet_flag)
2520 fprintf (stderr, "Reading summaries\n");
2522 /* Read the IPA summary data. */
2523 if (flag_ltrans)
2524 ipa_read_optimization_summaries ();
2525 else
2526 ipa_read_summaries ();
2528 /* Finally merge the cgraph according to the decl merging decisions. */
2529 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2530 if (cgraph_dump_file)
2532 fprintf (cgraph_dump_file, "Before merging:\n");
2533 dump_cgraph (cgraph_dump_file);
2534 dump_varpool (cgraph_dump_file);
2536 lto_symtab_merge_cgraph_nodes ();
2537 ggc_collect ();
2539 if (flag_ltrans)
2540 for (node = cgraph_nodes; node; node = node->next)
2542 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
2543 summaries computed and needs to apply changes. At the moment WHOPR only
2544 supports inlining, so we can push it here by hand. In future we need to stream
2545 this field into ltrans compilation. */
2546 if (node->analyzed)
2547 VEC_safe_push (ipa_opt_pass, heap,
2548 node->ipa_transforms_to_apply,
2549 (ipa_opt_pass)&pass_ipa_inline);
2551 lto_symtab_free ();
2553 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2555 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
2557 /* FIXME lto. This loop needs to be changed to use the pass manager to
2558 call the ipa passes directly. */
2559 if (!seen_error ())
2560 for (i = 0; i < last_file_ix; i++)
2562 struct lto_file_decl_data *file_data = all_file_decl_data [i];
2563 lto_materialize_constructors_and_inits (file_data);
2566 /* Indicate that the cgraph is built and ready. */
2567 cgraph_function_flags_ready = true;
2569 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
2570 ggc_free (all_file_decl_data);
2571 all_file_decl_data = NULL;
2575 /* Materialize all the bodies for all the nodes in the callgraph. */
2577 static void
2578 materialize_cgraph (void)
2580 tree decl;
2581 struct cgraph_node *node;
2582 unsigned i;
2583 timevar_id_t lto_timer;
2585 if (!quiet_flag)
2586 fprintf (stderr,
2587 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2590 /* Now that we have input the cgraph, we need to clear all of the aux
2591 nodes and read the functions if we are not running in WPA mode. */
2592 timevar_push (TV_IPA_LTO_GIMPLE_IN);
2594 for (node = cgraph_nodes; node; node = node->next)
2596 if (node->local.lto_file_data)
2598 lto_materialize_function (node);
2599 lto_stats.num_input_cgraph_nodes++;
2603 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
2605 /* Start the appropriate timer depending on the mode that we are
2606 operating in. */
2607 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2608 : (flag_ltrans) ? TV_WHOPR_LTRANS
2609 : TV_LTO;
2610 timevar_push (lto_timer);
2612 current_function_decl = NULL;
2613 set_cfun (NULL);
2615 /* Inform the middle end about the global variables we have seen. */
2616 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
2617 rest_of_decl_compilation (decl, 1, 0);
2619 if (!quiet_flag)
2620 fprintf (stderr, "\n");
2622 timevar_pop (lto_timer);
2626 /* Perform whole program analysis (WPA) on the callgraph and write out the
2627 optimization plan. */
2629 static void
2630 do_whole_program_analysis (void)
2632 /* Note that since we are in WPA mode, materialize_cgraph will not
2633 actually read in all the function bodies. It only materializes
2634 the decls and cgraph nodes so that analysis can be performed. */
2635 materialize_cgraph ();
2637 /* Reading in the cgraph uses different timers, start timing WPA now. */
2638 timevar_push (TV_WHOPR_WPA);
2640 if (pre_ipa_mem_report)
2642 fprintf (stderr, "Memory consumption before IPA\n");
2643 dump_memory_report (false);
2646 cgraph_function_flags_ready = true;
2648 if (cgraph_dump_file)
2650 dump_cgraph (cgraph_dump_file);
2651 dump_varpool (cgraph_dump_file);
2653 bitmap_obstack_initialize (NULL);
2654 cgraph_state = CGRAPH_STATE_IPA_SSA;
2656 execute_ipa_pass_list (all_regular_ipa_passes);
2658 if (cgraph_dump_file)
2660 fprintf (cgraph_dump_file, "Optimized ");
2661 dump_cgraph (cgraph_dump_file);
2662 dump_varpool (cgraph_dump_file);
2664 verify_cgraph ();
2665 bitmap_obstack_release (NULL);
2667 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
2668 timevar_pop (TV_WHOPR_WPA);
2670 if (flag_lto_partition_1to1)
2671 lto_1_to_1_map ();
2672 else
2673 lto_balanced_map ();
2675 if (!quiet_flag)
2677 fprintf (stderr, "\nStreaming out");
2678 fflush (stderr);
2680 lto_wpa_write_files ();
2681 ggc_collect ();
2682 if (!quiet_flag)
2683 fprintf (stderr, "\n");
2685 if (post_ipa_mem_report)
2687 fprintf (stderr, "Memory consumption after IPA\n");
2688 dump_memory_report (false);
2691 /* Show the LTO report before launching LTRANS. */
2692 if (flag_lto_report)
2693 print_lto_report ();
2697 static GTY(()) tree lto_eh_personality_decl;
2699 /* Return the LTO personality function decl. */
2701 tree
2702 lto_eh_personality (void)
2704 if (!lto_eh_personality_decl)
2706 /* Use the first personality DECL for our personality if we don't
2707 support multiple ones. This ensures that we don't artificially
2708 create the need for them in a single-language program. */
2709 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2710 lto_eh_personality_decl = first_personality_decl;
2711 else
2712 lto_eh_personality_decl = lhd_gcc_personality ();
2715 return lto_eh_personality_decl;
2718 /* Set the process name based on the LTO mode. */
2720 static void
2721 lto_process_name (void)
2723 if (flag_lto)
2724 setproctitle ("lto1-lto");
2725 if (flag_wpa)
2726 setproctitle ("lto1-wpa");
2727 if (flag_ltrans)
2728 setproctitle ("lto1-ltrans");
2732 /* Initialize the LTO front end. */
2734 static void
2735 lto_init (void)
2737 lto_process_name ();
2738 lto_streamer_hooks_init ();
2739 lto_reader_init ();
2740 memset (&lto_stats, 0, sizeof (lto_stats));
2741 bitmap_obstack_initialize (NULL);
2742 gimple_register_cfg_hooks ();
2746 /* Main entry point for the GIMPLE front end. This front end has
2747 three main personalities:
2749 - LTO (-flto). All the object files on the command line are
2750 loaded in memory and processed as a single translation unit.
2751 This is the traditional link-time optimization behavior.
2753 - WPA (-fwpa). Only the callgraph and summary information for
2754 files in the command file are loaded. A single callgraph
2755 (without function bodies) is instantiated for the whole set of
2756 files. IPA passes are only allowed to analyze the call graph
2757 and make transformation decisions. The callgraph is
2758 partitioned, each partition is written to a new object file
2759 together with the transformation decisions.
2761 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2762 summary files from running again. Since WPA computed summary
2763 information and decided what transformations to apply, LTRANS
2764 simply applies them. */
2766 void
2767 lto_main (void)
2769 /* Initialize the LTO front end. */
2770 lto_init ();
2772 /* Read all the symbols and call graph from all the files in the
2773 command line. */
2774 read_cgraph_and_symbols (num_in_fnames, in_fnames);
2776 if (!seen_error ())
2778 /* If WPA is enabled analyze the whole call graph and create an
2779 optimization plan. Otherwise, read in all the function
2780 bodies and continue with optimization. */
2781 if (flag_wpa)
2782 do_whole_program_analysis ();
2783 else
2785 materialize_cgraph ();
2787 /* Let the middle end know that we have read and merged all of
2788 the input files. */
2789 cgraph_optimize ();
2791 /* FIXME lto, if the processes spawned by WPA fail, we miss
2792 the chance to print WPA's report, so WPA will call
2793 print_lto_report before launching LTRANS. If LTRANS was
2794 launched directly by the driver we would not need to do
2795 this. */
2796 if (flag_lto_report)
2797 print_lto_report ();
2802 #include "gt-lto-lto.h"