Change random seeds to 64bit and drop re-crcing
[official-gcc.git] / gcc / lto / lto.c
blob77eb1a151884ec27f56016fe69ba471578c46880
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 /* Read the constructors and inits. */
98 static void
99 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
101 size_t len;
102 const char *data = lto_get_section_data (file_data,
103 LTO_section_static_initializer,
104 NULL, &len);
105 lto_input_constructors_and_inits (file_data, data);
106 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
107 data, len);
110 /* Return true when NODE has a clone that is analyzed (i.e. we need
111 to load its body even if the node itself is not needed). */
113 static bool
114 has_analyzed_clone_p (struct cgraph_node *node)
116 struct cgraph_node *orig = node;
117 node = node->clones;
118 if (node)
119 while (node != orig)
121 if (node->analyzed)
122 return true;
123 if (node->clones)
124 node = node->clones;
125 else if (node->next_sibling_clone)
126 node = node->next_sibling_clone;
127 else
129 while (node != orig && !node->next_sibling_clone)
130 node = node->clone_of;
131 if (node != orig)
132 node = node->next_sibling_clone;
135 return false;
138 /* Read the function body for the function associated with NODE. */
140 static void
141 lto_materialize_function (struct cgraph_node *node)
143 tree decl;
144 struct lto_file_decl_data *file_data;
145 const char *data, *name;
146 size_t len;
148 decl = node->decl;
149 /* Read in functions with body (analyzed nodes)
150 and also functions that are needed to produce virtual clones. */
151 if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node))
153 /* Clones and thunks don't need to be read. */
154 if (node->clone_of)
155 return;
157 /* Load the function body only if not operating in WPA mode. In
158 WPA mode, the body of the function is not needed. */
159 if (!flag_wpa)
161 file_data = node->local.lto_file_data;
162 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
164 /* We may have renamed the declaration, e.g., a static function. */
165 name = lto_get_decl_name_mapping (file_data, name);
167 data = lto_get_section_data (file_data, LTO_section_function_body,
168 name, &len);
169 if (!data)
170 fatal_error ("%s: section %s is missing",
171 file_data->file_name,
172 name);
174 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
176 allocate_struct_function (decl, false);
177 announce_function (decl);
178 lto_input_function_body (file_data, decl, data);
179 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
180 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
181 lto_stats.num_function_bodies++;
182 lto_free_section_data (file_data, LTO_section_function_body, name,
183 data, len);
184 ggc_collect ();
188 /* Let the middle end know about the function. */
189 rest_of_decl_compilation (decl, 1, 0);
193 /* Decode the content of memory pointed to by DATA in the in decl
194 state object STATE. DATA_IN points to a data_in structure for
195 decoding. Return the address after the decoded object in the
196 input. */
198 static const uint32_t *
199 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
200 struct lto_in_decl_state *state)
202 uint32_t ix;
203 tree decl;
204 uint32_t i, j;
206 ix = *data++;
207 decl = streamer_tree_cache_get (data_in->reader_cache, ix);
208 if (TREE_CODE (decl) != FUNCTION_DECL)
210 gcc_assert (decl == void_type_node);
211 decl = NULL_TREE;
213 state->fn_decl = decl;
215 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
217 uint32_t size = *data++;
218 tree *decls = ggc_alloc_vec_tree (size);
220 for (j = 0; j < size; j++)
221 decls[j] = streamer_tree_cache_get (data_in->reader_cache, data[j]);
223 state->streams[i].size = size;
224 state->streams[i].trees = decls;
225 data += size;
228 return data;
231 /* A hashtable of trees that potentially refer to variables or functions
232 that must be replaced with their prevailing variant. */
233 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
234 tree_with_vars;
236 /* Remember that T is a tree that (potentially) refers to a variable
237 or function decl that may be replaced with its prevailing variant. */
238 static void
239 remember_with_vars (tree t)
241 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
244 #define LTO_FIXUP_TREE(tt) \
245 do \
247 if (tt) \
249 if (TYPE_P (tt)) \
250 (tt) = gimple_register_type (tt); \
251 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
252 remember_with_vars (t); \
254 } while (0)
256 static void lto_fixup_types (tree);
258 /* Fix up fields of a tree_typed T. */
260 static void
261 lto_ft_typed (tree t)
263 LTO_FIXUP_TREE (TREE_TYPE (t));
266 /* Fix up fields of a tree_common T. */
268 static void
269 lto_ft_common (tree t)
271 lto_ft_typed (t);
272 LTO_FIXUP_TREE (TREE_CHAIN (t));
275 /* Fix up fields of a decl_minimal T. */
277 static void
278 lto_ft_decl_minimal (tree t)
280 lto_ft_common (t);
281 LTO_FIXUP_TREE (DECL_NAME (t));
282 LTO_FIXUP_TREE (DECL_CONTEXT (t));
285 /* Fix up fields of a decl_common T. */
287 static void
288 lto_ft_decl_common (tree t)
290 lto_ft_decl_minimal (t);
291 LTO_FIXUP_TREE (DECL_SIZE (t));
292 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t));
293 LTO_FIXUP_TREE (DECL_INITIAL (t));
294 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t));
295 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t));
298 /* Fix up fields of a decl_with_vis T. */
300 static void
301 lto_ft_decl_with_vis (tree t)
303 lto_ft_decl_common (t);
305 /* Accessor macro has side-effects, use field-name here. */
306 LTO_FIXUP_TREE (t->decl_with_vis.assembler_name);
307 LTO_FIXUP_TREE (DECL_SECTION_NAME (t));
310 /* Fix up fields of a decl_non_common T. */
312 static void
313 lto_ft_decl_non_common (tree t)
315 lto_ft_decl_with_vis (t);
316 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t));
317 LTO_FIXUP_TREE (DECL_RESULT_FLD (t));
318 LTO_FIXUP_TREE (DECL_VINDEX (t));
321 /* Fix up fields of a decl_non_common T. */
323 static void
324 lto_ft_function (tree t)
326 lto_ft_decl_non_common (t);
327 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t));
330 /* Fix up fields of a field_decl T. */
332 static void
333 lto_ft_field_decl (tree t)
335 lto_ft_decl_common (t);
336 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t));
337 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t));
338 LTO_FIXUP_TREE (DECL_QUALIFIER (t));
339 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t));
340 LTO_FIXUP_TREE (DECL_FCONTEXT (t));
343 /* Fix up fields of a type T. */
345 static void
346 lto_ft_type (tree t)
348 lto_ft_common (t);
349 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t));
350 LTO_FIXUP_TREE (TYPE_SIZE (t));
351 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t));
352 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t));
353 LTO_FIXUP_TREE (TYPE_NAME (t));
355 /* Accessors are for derived node types only. */
356 if (!POINTER_TYPE_P (t))
357 LTO_FIXUP_TREE (TYPE_MINVAL (t));
358 LTO_FIXUP_TREE (TYPE_MAXVAL (t));
360 /* Accessor is for derived node types only. */
361 LTO_FIXUP_TREE (t->type_non_common.binfo);
363 LTO_FIXUP_TREE (TYPE_CONTEXT (t));
366 /* Fix up fields of a BINFO T. */
368 static void
369 lto_ft_binfo (tree t)
371 unsigned HOST_WIDE_INT i, n;
372 tree base, saved_base;
374 lto_ft_common (t);
375 LTO_FIXUP_TREE (BINFO_VTABLE (t));
376 LTO_FIXUP_TREE (BINFO_OFFSET (t));
377 LTO_FIXUP_TREE (BINFO_VIRTUALS (t));
378 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t));
379 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
380 for (i = 0; i < n; i++)
382 saved_base = base = BINFO_BASE_ACCESS (t, i);
383 LTO_FIXUP_TREE (base);
384 if (base != saved_base)
385 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
387 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t));
388 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t));
389 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t));
390 n = BINFO_N_BASE_BINFOS (t);
391 for (i = 0; i < n; i++)
393 saved_base = base = BINFO_BASE_BINFO (t, i);
394 LTO_FIXUP_TREE (base);
395 if (base != saved_base)
396 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
400 /* Fix up fields of a CONSTRUCTOR T. */
402 static void
403 lto_ft_constructor (tree t)
405 unsigned HOST_WIDE_INT idx;
406 constructor_elt *ce;
408 lto_ft_typed (t);
410 for (idx = 0;
411 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
412 idx++)
414 LTO_FIXUP_TREE (ce->index);
415 LTO_FIXUP_TREE (ce->value);
419 /* Fix up fields of an expression tree T. */
421 static void
422 lto_ft_expr (tree t)
424 int i;
425 lto_ft_typed (t);
426 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
427 LTO_FIXUP_TREE (TREE_OPERAND (t, i));
430 /* Given a tree T fixup fields of T by replacing types with their merged
431 variant and other entities by an equal entity from an earlier compilation
432 unit, or an entity being canonical in a different way. This includes
433 for instance integer or string constants. */
435 static void
436 lto_fixup_types (tree t)
438 switch (TREE_CODE (t))
440 case IDENTIFIER_NODE:
441 break;
443 case TREE_LIST:
444 LTO_FIXUP_TREE (TREE_VALUE (t));
445 LTO_FIXUP_TREE (TREE_PURPOSE (t));
446 LTO_FIXUP_TREE (TREE_CHAIN (t));
447 break;
449 case FIELD_DECL:
450 lto_ft_field_decl (t);
451 break;
453 case LABEL_DECL:
454 case CONST_DECL:
455 case PARM_DECL:
456 case RESULT_DECL:
457 case IMPORTED_DECL:
458 lto_ft_decl_common (t);
459 break;
461 case VAR_DECL:
462 lto_ft_decl_with_vis (t);
463 break;
465 case TYPE_DECL:
466 lto_ft_decl_non_common (t);
467 break;
469 case FUNCTION_DECL:
470 lto_ft_function (t);
471 break;
473 case TREE_BINFO:
474 lto_ft_binfo (t);
475 break;
477 case PLACEHOLDER_EXPR:
478 lto_ft_common (t);
479 break;
481 case BLOCK:
482 case TRANSLATION_UNIT_DECL:
483 case OPTIMIZATION_NODE:
484 case TARGET_OPTION_NODE:
485 break;
487 default:
488 if (TYPE_P (t))
489 lto_ft_type (t);
490 else if (TREE_CODE (t) == CONSTRUCTOR)
491 lto_ft_constructor (t);
492 else if (CONSTANT_CLASS_P (t))
493 LTO_FIXUP_TREE (TREE_TYPE (t));
494 else if (EXPR_P (t))
496 lto_ft_expr (t);
498 else
500 remember_with_vars (t);
506 /* Return the resolution for the decl with index INDEX from DATA_IN. */
508 static enum ld_plugin_symbol_resolution
509 get_resolution (struct data_in *data_in, unsigned index)
511 if (data_in->globals_resolution)
513 ld_plugin_symbol_resolution_t ret;
514 /* We can have references to not emitted functions in
515 DECL_FUNCTION_PERSONALITY at least. So we can and have
516 to indeed return LDPR_UNKNOWN in some cases. */
517 if (VEC_length (ld_plugin_symbol_resolution_t,
518 data_in->globals_resolution) <= index)
519 return LDPR_UNKNOWN;
520 ret = VEC_index (ld_plugin_symbol_resolution_t,
521 data_in->globals_resolution,
522 index);
523 return ret;
525 else
526 /* Delay resolution finding until decl merging. */
527 return LDPR_UNKNOWN;
531 /* Register DECL with the global symbol table and change its
532 name if necessary to avoid name clashes for static globals across
533 different files. */
535 static void
536 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl)
538 tree context;
540 /* Variable has file scope, not local. Need to ensure static variables
541 between different files don't clash unexpectedly. */
542 if (!TREE_PUBLIC (decl)
543 && !((context = decl_function_context (decl))
544 && auto_var_in_fn_p (decl, context)))
546 /* ??? We normally pre-mangle names before we serialize them
547 out. Here, in lto1, we do not know the language, and
548 thus cannot do the mangling again. Instead, we just
549 append a suffix to the mangled name. The resulting name,
550 however, is not a properly-formed mangled name, and will
551 confuse any attempt to unmangle it. */
552 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
553 char *label;
555 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
556 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
557 rest_of_decl_compilation (decl, 1, 0);
558 VEC_safe_push (tree, gc, lto_global_var_decls, decl);
561 /* If this variable has already been declared, queue the
562 declaration for merging. */
563 if (TREE_PUBLIC (decl))
565 unsigned ix;
566 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
567 gcc_unreachable ();
568 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
569 data_in->file_data);
574 /* Register DECL with the global symbol table and change its
575 name if necessary to avoid name clashes for static globals across
576 different files. DATA_IN contains descriptors and tables for the
577 file being read. */
579 static void
580 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl)
582 /* Need to ensure static entities between different files
583 don't clash unexpectedly. */
584 if (!TREE_PUBLIC (decl))
586 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
587 may set the assembler name where it was previously empty. */
588 tree old_assembler_name = decl->decl_with_vis.assembler_name;
590 /* FIXME lto: We normally pre-mangle names before we serialize
591 them out. Here, in lto1, we do not know the language, and
592 thus cannot do the mangling again. Instead, we just append a
593 suffix to the mangled name. The resulting name, however, is
594 not a properly-formed mangled name, and will confuse any
595 attempt to unmangle it. */
596 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
597 char *label;
599 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
600 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
602 /* We may arrive here with the old assembler name not set
603 if the function body is not needed, e.g., it has been
604 inlined away and does not appear in the cgraph. */
605 if (old_assembler_name)
607 tree new_assembler_name = DECL_ASSEMBLER_NAME (decl);
609 /* Make the original assembler name available for later use.
610 We may have used it to indicate the section within its
611 object file where the function body may be found.
612 FIXME lto: Find a better way to maintain the function decl
613 to body section mapping so we don't need this hack. */
614 lto_record_renamed_decl (data_in->file_data,
615 IDENTIFIER_POINTER (old_assembler_name),
616 IDENTIFIER_POINTER (new_assembler_name));
618 /* Also register the reverse mapping so that we can find the
619 new name given to an existing assembler name (used when
620 restoring alias pairs in input_constructors_or_inits. */
621 lto_record_renamed_decl (data_in->file_data,
622 IDENTIFIER_POINTER (new_assembler_name),
623 IDENTIFIER_POINTER (old_assembler_name));
627 /* If this variable has already been declared, queue the
628 declaration for merging. */
629 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
631 unsigned ix;
632 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
633 gcc_unreachable ();
634 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
635 data_in->file_data);
640 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
641 for one compilation unit) go over all trees starting at index FROM until the
642 end of the sequence and replace fields of those trees, and the trees
643 themself with their canonical variants as per gimple_register_type. */
645 static void
646 uniquify_nodes (struct data_in *data_in, unsigned from)
648 struct streamer_tree_cache_d *cache = data_in->reader_cache;
649 unsigned len = VEC_length (tree, cache->nodes);
650 unsigned i;
652 /* Go backwards because children streamed for the first time come
653 as part of their parents, and hence are created after them. */
655 /* First register all the types in the cache. This makes sure to
656 have the original structure in the type cycles when registering
657 them and computing hashes. */
658 for (i = len; i-- > from;)
660 tree t = VEC_index (tree, cache->nodes, i);
661 if (t && TYPE_P (t))
662 gimple_register_type (t);
665 /* Second fixup all trees in the new cache entries. */
666 for (i = len; i-- > from;)
668 tree t = VEC_index (tree, cache->nodes, i);
669 tree oldt = t;
670 if (!t)
671 continue;
673 /* First fixup the fields of T. */
674 lto_fixup_types (t);
676 if (!TYPE_P (t))
677 continue;
679 /* Now try to find a canonical variant of T itself. */
680 t = gimple_register_type (t);
682 if (t == oldt)
684 /* The following re-creates proper variant lists while fixing up
685 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
686 variant list state before fixup is broken. */
687 tree tem, mv;
689 /* Remove us from our main variant list if we are not the
690 variant leader. */
691 if (TYPE_MAIN_VARIANT (t) != t)
693 tem = TYPE_MAIN_VARIANT (t);
694 while (tem && TYPE_NEXT_VARIANT (tem) != t)
695 tem = TYPE_NEXT_VARIANT (tem);
696 if (tem)
697 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
698 TYPE_NEXT_VARIANT (t) = NULL_TREE;
701 /* Query our new main variant. */
702 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
704 /* If we were the variant leader and we get replaced ourselves drop
705 all variants from our list. */
706 if (TYPE_MAIN_VARIANT (t) == t
707 && mv != t)
709 tem = t;
710 while (tem)
712 tree tem2 = TYPE_NEXT_VARIANT (tem);
713 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
714 tem = tem2;
718 /* If we are not our own variant leader link us into our new leaders
719 variant list. */
720 if (mv != t)
722 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
723 TYPE_NEXT_VARIANT (mv) = t;
724 if (RECORD_OR_UNION_TYPE_P (t))
725 TYPE_BINFO (t) = TYPE_BINFO (mv);
728 /* Finally adjust our main variant and fix it up. */
729 TYPE_MAIN_VARIANT (t) = mv;
731 /* The following reconstructs the pointer chains
732 of the new pointed-to type if we are a main variant. We do
733 not stream those so they are broken before fixup. */
734 if (TREE_CODE (t) == POINTER_TYPE
735 && TYPE_MAIN_VARIANT (t) == t)
737 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
738 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
740 else if (TREE_CODE (t) == REFERENCE_TYPE
741 && TYPE_MAIN_VARIANT (t) == t)
743 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
744 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
748 else
750 if (RECORD_OR_UNION_TYPE_P (t))
752 tree f1, f2;
753 if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt))
754 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt);
755 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
757 unsigned ix;
758 gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2));
759 if (!streamer_tree_cache_lookup (cache, f2, &ix))
760 gcc_unreachable ();
761 /* If we're going to replace an element which we'd
762 still visit in the next iterations, we wouldn't
763 handle it, so do it here. We do have to handle it
764 even though the field_decl itself will be removed,
765 as it could refer to e.g. integer_cst which we
766 wouldn't reach via any other way, hence they
767 (and their type) would stay uncollected. */
768 /* ??? We should rather make sure to replace all
769 references to f2 with f1. That means handling
770 COMPONENT_REFs and CONSTRUCTOR elements in
771 lto_fixup_types and special-case the field-decl
772 operand handling. */
773 if (ix < i)
774 lto_fixup_types (f2);
775 streamer_tree_cache_insert_at (cache, f1, ix);
779 /* If we found a tree that is equal to oldt replace it in the
780 cache, so that further users (in the various LTO sections)
781 make use of it. */
782 streamer_tree_cache_insert_at (cache, t, i);
786 /* Finally compute the canonical type of all TREE_TYPEs and register
787 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
788 From this point there are no longer any types with
789 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
790 This step requires the TYPE_POINTER_TO lists being present, so
791 make sure it is done last. */
792 for (i = len; i-- > from;)
794 tree t = VEC_index (tree, cache->nodes, i);
795 if (t == NULL_TREE)
796 continue;
798 if (TREE_CODE (t) == VAR_DECL)
799 lto_register_var_decl_in_symtab (data_in, t);
800 else if (TREE_CODE (t) == FUNCTION_DECL && !DECL_BUILT_IN (t))
801 lto_register_function_decl_in_symtab (data_in, t);
802 else if (TYPE_P (t) && !TYPE_CANONICAL (t))
803 TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
808 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
809 RESOLUTIONS is the set of symbols picked by the linker (read from the
810 resolution file when the linker plugin is being used). */
812 static void
813 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
814 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
816 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
817 const int32_t decl_offset = sizeof (struct lto_decl_header);
818 const int32_t main_offset = decl_offset + header->decl_state_size;
819 const int32_t string_offset = main_offset + header->main_size;
820 struct lto_input_block ib_main;
821 struct data_in *data_in;
822 unsigned int i;
823 const uint32_t *data_ptr, *data_end;
824 uint32_t num_decl_states;
826 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
827 header->main_size);
829 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
830 header->string_size, resolutions);
832 /* Read the global declarations and types. */
833 while (ib_main.p < ib_main.len)
835 tree t;
836 unsigned from = VEC_length (tree, data_in->reader_cache->nodes);
837 t = stream_read_tree (&ib_main, data_in);
838 gcc_assert (t && ib_main.p <= ib_main.len);
839 uniquify_nodes (data_in, from);
842 /* Read in lto_in_decl_state objects. */
843 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
844 data_end =
845 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
846 num_decl_states = *data_ptr++;
848 gcc_assert (num_decl_states > 0);
849 decl_data->global_decl_state = lto_new_in_decl_state ();
850 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
851 decl_data->global_decl_state);
853 /* Read in per-function decl states and enter them in hash table. */
854 decl_data->function_decl_states =
855 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
857 for (i = 1; i < num_decl_states; i++)
859 struct lto_in_decl_state *state = lto_new_in_decl_state ();
860 void **slot;
862 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
863 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
864 gcc_assert (*slot == NULL);
865 *slot = state;
868 if (data_ptr != data_end)
869 internal_error ("bytecode stream: garbage at the end of symbols section");
871 /* Set the current decl state to be the global state. */
872 decl_data->current_decl_state = decl_data->global_decl_state;
874 lto_data_in_delete (data_in);
877 /* strtoll is not portable. */
878 int64_t
879 lto_parse_hex (const char *p) {
880 uint64_t ret = 0;
881 for (; *p != '\0'; ++p)
883 char c = *p;
884 unsigned char part;
885 ret <<= 4;
886 if (c >= '0' && c <= '9')
887 part = c - '0';
888 else if (c >= 'a' && c <= 'f')
889 part = c - 'a' + 10;
890 else if (c >= 'A' && c <= 'F')
891 part = c - 'A' + 10;
892 else
893 internal_error ("could not parse hex number");
894 ret |= part;
896 return ret;
899 /* Read resolution for file named FILE_NAME. The resolution is read from
900 RESOLUTION. */
902 static void
903 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
905 /* We require that objects in the resolution file are in the same
906 order as the lto1 command line. */
907 unsigned int name_len;
908 char *obj_name;
909 unsigned int num_symbols;
910 unsigned int i;
911 struct lto_file_decl_data *file_data;
912 unsigned max_index = 0;
913 splay_tree_node nd = NULL;
915 if (!resolution)
916 return;
918 name_len = strlen (file->filename);
919 obj_name = XNEWVEC (char, name_len + 1);
920 fscanf (resolution, " "); /* Read white space. */
922 fread (obj_name, sizeof (char), name_len, resolution);
923 obj_name[name_len] = '\0';
924 if (filename_cmp (obj_name, file->filename) != 0)
925 internal_error ("unexpected file name %s in linker resolution file. "
926 "Expected %s", obj_name, file->filename);
927 if (file->offset != 0)
929 int t;
930 char offset_p[17];
931 int64_t offset;
932 t = fscanf (resolution, "@0x%16s", offset_p);
933 if (t != 1)
934 internal_error ("could not parse file offset");
935 offset = lto_parse_hex (offset_p);
936 if (offset != file->offset)
937 internal_error ("unexpected offset");
940 free (obj_name);
942 fscanf (resolution, "%u", &num_symbols);
944 for (i = 0; i < num_symbols; i++)
946 int t;
947 unsigned index, id;
948 char r_str[27];
949 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
950 unsigned int j;
951 unsigned int lto_resolution_str_len =
952 sizeof (lto_resolution_str) / sizeof (char *);
954 t = fscanf (resolution, "%u %x %26s %*[^\n]\n", &index, &id, r_str);
955 if (t != 3)
956 internal_error ("invalid line in the resolution file");
957 if (index > max_index)
958 max_index = index;
960 for (j = 0; j < lto_resolution_str_len; j++)
962 if (strcmp (lto_resolution_str[j], r_str) == 0)
964 r = (enum ld_plugin_symbol_resolution) j;
965 break;
968 if (j == lto_resolution_str_len)
969 internal_error ("invalid resolution in the resolution file");
971 if (!(nd && nd->key == id))
973 nd = splay_tree_lookup (file_ids, id);
974 if (nd == NULL)
975 internal_error ("resolution sub id %x not in object file", id);
978 file_data = (struct lto_file_decl_data *)nd->value;
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 HOST_WIDE_INT *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, "." HOST_WIDE_INT_PRINT_HEX_PURE, 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 HOST_WIDE_INT 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 " HOST_WIDE_INT_PRINT_HEX "\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 (cgraph_function_node (ipa_ref_node (ref), NULL)->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 /* Worker for add_cgraph_node_to_partition. */
1335 static bool
1336 add_cgraph_node_to_partition_1 (struct cgraph_node *node, void *data)
1338 ltrans_partition part = (ltrans_partition) data;
1340 /* non-COMDAT aliases of COMDAT functions needs to be output just once. */
1341 if (!DECL_COMDAT (node->decl)
1342 && !node->global.inlined_to
1343 && node->aux)
1345 gcc_assert (node->thunk.thunk_p || node->alias);
1346 return false;
1349 if (node->aux)
1351 node->in_other_partition = 1;
1352 if (cgraph_dump_file)
1353 fprintf (cgraph_dump_file, "Node %s/%i now used in multiple partitions\n",
1354 cgraph_node_name (node), node->uid);
1356 node->aux = (void *)((size_t)node->aux + 1);
1357 cgraph_node_set_add (part->cgraph_set, node);
1358 return false;
1361 /* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */
1363 static void
1364 add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node)
1366 struct cgraph_edge *e;
1367 cgraph_node_set_iterator csi;
1368 struct cgraph_node *n;
1370 /* We always decide on functions, not associated thunks and aliases. */
1371 node = cgraph_function_node (node, NULL);
1373 /* If NODE is already there, we have nothing to do. */
1374 csi = cgraph_node_set_find (part->cgraph_set, node);
1375 if (!csi_end_p (csi))
1376 return;
1378 cgraph_for_node_thunks_and_aliases (node, add_cgraph_node_to_partition_1, part, true);
1380 part->insns += inline_summary (node)->self_size;
1383 cgraph_node_set_add (part->cgraph_set, node);
1385 for (e = node->callees; e; e = e->next_callee)
1386 if ((!e->inline_failed
1387 || DECL_COMDAT (cgraph_function_node (e->callee, NULL)->decl))
1388 && !cgraph_node_in_set_p (e->callee, part->cgraph_set))
1389 add_cgraph_node_to_partition (part, e->callee);
1391 add_references_to_partition (part, &node->ref_list);
1393 if (node->same_comdat_group)
1394 for (n = node->same_comdat_group; n != node; n = n->same_comdat_group)
1395 add_cgraph_node_to_partition (part, n);
1398 /* Add VNODE to partition as well as comdat references partition PART. */
1400 static void
1401 add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode)
1403 varpool_node_set_iterator vsi;
1405 /* If NODE is already there, we have nothing to do. */
1406 vsi = varpool_node_set_find (part->varpool_set, vnode);
1407 if (!vsi_end_p (vsi))
1408 return;
1410 varpool_node_set_add (part->varpool_set, vnode);
1412 if (vnode->aux)
1414 vnode->in_other_partition = 1;
1415 if (cgraph_dump_file)
1416 fprintf (cgraph_dump_file, "Varpool node %s now used in multiple partitions\n",
1417 varpool_node_name (vnode));
1419 vnode->aux = (void *)((size_t)vnode->aux + 1);
1421 add_references_to_partition (part, &vnode->ref_list);
1423 if (vnode->same_comdat_group
1424 && !varpool_node_in_set_p (vnode->same_comdat_group, part->varpool_set))
1425 add_varpool_node_to_partition (part, vnode->same_comdat_group);
1428 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
1429 and number of varpool nodes is N_VARPOOL_NODES. */
1431 static void
1432 undo_partition (ltrans_partition partition, unsigned int n_cgraph_nodes,
1433 unsigned int n_varpool_nodes)
1435 while (VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes) >
1436 n_cgraph_nodes)
1438 struct cgraph_node *node = VEC_index (cgraph_node_ptr,
1439 partition->cgraph_set->nodes,
1440 n_cgraph_nodes);
1441 partition->insns -= inline_summary (node)->self_size;
1442 cgraph_node_set_remove (partition->cgraph_set, node);
1443 node->aux = (void *)((size_t)node->aux - 1);
1445 while (VEC_length (varpool_node_ptr, partition->varpool_set->nodes) >
1446 n_varpool_nodes)
1448 struct varpool_node *node = VEC_index (varpool_node_ptr,
1449 partition->varpool_set->nodes,
1450 n_varpool_nodes);
1451 varpool_node_set_remove (partition->varpool_set, node);
1452 node->aux = (void *)((size_t)node->aux - 1);
1456 /* Return true if NODE should be partitioned.
1457 This means that partitioning algorithm should put NODE into one of partitions.
1458 This apply to most functions with bodies. Functions that are not partitions
1459 are put into every unit needing them. This is the case of i.e. COMDATs. */
1461 static bool
1462 partition_cgraph_node_p (struct cgraph_node *node)
1464 /* We will get proper partition based on function they are inlined to. */
1465 if (node->global.inlined_to)
1466 return false;
1467 /* Nodes without a body do not need partitioning. */
1468 if (!node->analyzed)
1469 return false;
1470 /* Extern inlines and comdat are always only in partitions they are needed. */
1471 if (DECL_EXTERNAL (node->decl)
1472 || (DECL_COMDAT (node->decl)
1473 && !cgraph_used_from_object_file_p (node)))
1474 return false;
1475 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
1476 return false;
1477 return true;
1480 /* Return true if VNODE should be partitioned.
1481 This means that partitioning algorithm should put VNODE into one of partitions. */
1483 static bool
1484 partition_varpool_node_p (struct varpool_node *vnode)
1486 if (vnode->alias || !vnode->needed)
1487 return false;
1488 /* Constant pool and comdat are always only in partitions they are needed. */
1489 if (DECL_IN_CONSTANT_POOL (vnode->decl)
1490 || (DECL_COMDAT (vnode->decl)
1491 && !vnode->force_output
1492 && !varpool_used_from_object_file_p (vnode)))
1493 return false;
1494 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->decl)))
1495 return false;
1496 return true;
1499 /* Group cgrah nodes by input files. This is used mainly for testing
1500 right now. */
1502 static void
1503 lto_1_to_1_map (void)
1505 struct cgraph_node *node;
1506 struct varpool_node *vnode;
1507 struct lto_file_decl_data *file_data;
1508 struct pointer_map_t *pmap;
1509 ltrans_partition partition;
1510 void **slot;
1511 int npartitions = 0;
1513 timevar_push (TV_WHOPR_WPA);
1515 pmap = pointer_map_create ();
1517 for (node = cgraph_nodes; node; node = node->next)
1519 if (!partition_cgraph_node_p (node)
1520 || node->aux)
1521 continue;
1523 file_data = node->local.lto_file_data;
1525 if (file_data)
1527 slot = pointer_map_contains (pmap, file_data);
1528 if (slot)
1529 partition = (ltrans_partition) *slot;
1530 else
1532 partition = new_partition (file_data->file_name);
1533 slot = pointer_map_insert (pmap, file_data);
1534 *slot = partition;
1535 npartitions++;
1538 else if (!file_data
1539 && VEC_length (ltrans_partition, ltrans_partitions))
1540 partition = VEC_index (ltrans_partition, ltrans_partitions, 0);
1541 else
1543 partition = new_partition ("");
1544 slot = pointer_map_insert (pmap, NULL);
1545 *slot = partition;
1546 npartitions++;
1549 add_cgraph_node_to_partition (partition, node);
1552 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1554 if (!partition_varpool_node_p (vnode)
1555 || vnode->aux)
1556 continue;
1557 file_data = vnode->lto_file_data;
1558 slot = pointer_map_contains (pmap, file_data);
1559 if (slot)
1560 partition = (ltrans_partition) *slot;
1561 else
1563 partition = new_partition (file_data->file_name);
1564 slot = pointer_map_insert (pmap, file_data);
1565 *slot = partition;
1566 npartitions++;
1569 add_varpool_node_to_partition (partition, vnode);
1571 for (node = cgraph_nodes; node; node = node->next)
1572 node->aux = NULL;
1573 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1574 vnode->aux = NULL;
1576 /* If the cgraph is empty, create one cgraph node set so that there is still
1577 an output file for any variables that need to be exported in a DSO. */
1578 if (!npartitions)
1579 new_partition ("empty");
1581 pointer_map_destroy (pmap);
1583 timevar_pop (TV_WHOPR_WPA);
1585 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
1586 ltrans_partitions);
1590 /* Group cgraph nodes into equally-sized partitions.
1592 The partitioning algorithm is simple: nodes are taken in predefined order.
1593 The order corresponds to the order we want functions to have in the final
1594 output. In the future this will be given by function reordering pass, but
1595 at the moment we use the topological order, which is a good approximation.
1597 The goal is to partition this linear order into intervals (partitions) so
1598 that all the partitions have approximately the same size and the number of
1599 callgraph or IPA reference edges crossing boundaries is minimal.
1601 This is a lot faster (O(n) in size of callgraph) than algorithms doing
1602 priority-based graph clustering that are generally O(n^2) and, since
1603 WHOPR is designed to make things go well across partitions, it leads
1604 to good results.
1606 We compute the expected size of a partition as:
1608 max (total_size / lto_partitions, min_partition_size)
1610 We use dynamic expected size of partition so small programs are partitioned
1611 into enough partitions to allow use of multiple CPUs, while large programs
1612 are not partitioned too much. Creating too many partitions significantly
1613 increases the streaming overhead.
1615 In the future, we would like to bound the maximal size of partitions so as
1616 to prevent the LTRANS stage from consuming too much memory. At the moment,
1617 however, the WPA stage is the most memory intensive for large benchmarks,
1618 since too many types and declarations are read into memory.
1620 The function implements a simple greedy algorithm. Nodes are being added
1621 to the current partition until after 3/4 of the expected partition size is
1622 reached. Past this threshold, we keep track of boundary size (number of
1623 edges going to other partitions) and continue adding functions until after
1624 the current partition has grown to twice the expected partition size. Then
1625 the process is undone to the point where the minimal ratio of boundary size
1626 and in-partition calls was reached. */
1628 static void
1629 lto_balanced_map (void)
1631 int n_nodes = 0;
1632 struct cgraph_node **postorder =
1633 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1634 struct cgraph_node **order = XNEWVEC (struct cgraph_node *, cgraph_max_uid);
1635 int i, postorder_len;
1636 struct cgraph_node *node;
1637 int total_size = 0, best_total_size = 0;
1638 int partition_size;
1639 ltrans_partition partition;
1640 unsigned int last_visited_cgraph_node = 0, last_visited_varpool_node = 0;
1641 struct varpool_node *vnode;
1642 int cost = 0, internal = 0;
1643 int best_n_nodes = 0, best_n_varpool_nodes = 0, best_i = 0, best_cost =
1644 INT_MAX, best_internal = 0;
1645 int npartitions;
1647 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1648 gcc_assert (!vnode->aux);
1649 /* Until we have better ordering facility, use toplogical order.
1650 Include only nodes we will partition and compute estimate of program
1651 size. Note that since nodes that are not partitioned might be put into
1652 multiple partitions, this is just an estimate of real size. This is why
1653 we keep partition_size updated after every partition is finalized. */
1654 postorder_len = ipa_reverse_postorder (postorder);
1655 for (i = 0; i < postorder_len; i++)
1657 node = postorder[i];
1658 if (partition_cgraph_node_p (node))
1660 order[n_nodes++] = node;
1661 total_size += inline_summary (node)->size;
1664 free (postorder);
1666 /* Compute partition size and create the first partition. */
1667 partition_size = total_size / PARAM_VALUE (PARAM_LTO_PARTITIONS);
1668 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1669 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1670 npartitions = 1;
1671 partition = new_partition ("");
1672 if (cgraph_dump_file)
1673 fprintf (cgraph_dump_file, "Total unit size: %i, partition size: %i\n",
1674 total_size, partition_size);
1676 for (i = 0; i < n_nodes; i++)
1678 if (order[i]->aux)
1679 continue;
1680 add_cgraph_node_to_partition (partition, order[i]);
1681 total_size -= inline_summary (order[i])->size;
1683 /* Once we added a new node to the partition, we also want to add
1684 all referenced variables unless they was already added into some
1685 earlier partition.
1686 add_cgraph_node_to_partition adds possibly multiple nodes and
1687 variables that are needed to satisfy needs of ORDER[i].
1688 We remember last visited cgraph and varpool node from last iteration
1689 of outer loop that allows us to process every new addition.
1691 At the same time we compute size of the boundary into COST. Every
1692 callgraph or IPA reference edge leaving the partition contributes into
1693 COST. Every edge inside partition was earlier computed as one leaving
1694 it and thus we need to subtract it from COST. */
1695 while (last_visited_cgraph_node <
1696 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes)
1697 || last_visited_varpool_node < VEC_length (varpool_node_ptr,
1698 partition->varpool_set->
1699 nodes))
1701 struct ipa_ref_list *refs;
1702 int j;
1703 struct ipa_ref *ref;
1704 bool cgraph_p = false;
1706 if (last_visited_cgraph_node <
1707 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes))
1709 struct cgraph_edge *edge;
1711 cgraph_p = true;
1712 node = VEC_index (cgraph_node_ptr, partition->cgraph_set->nodes,
1713 last_visited_cgraph_node);
1714 refs = &node->ref_list;
1716 last_visited_cgraph_node++;
1718 gcc_assert (node->analyzed);
1720 /* Compute boundary cost of callgrpah edges. */
1721 for (edge = node->callees; edge; edge = edge->next_callee)
1722 if (edge->callee->analyzed)
1724 int edge_cost = edge->frequency;
1725 cgraph_node_set_iterator csi;
1727 if (!edge_cost)
1728 edge_cost = 1;
1729 gcc_assert (edge_cost > 0);
1730 csi = cgraph_node_set_find (partition->cgraph_set, edge->callee);
1731 if (!csi_end_p (csi)
1732 && csi.index < last_visited_cgraph_node - 1)
1733 cost -= edge_cost, internal+= edge_cost;
1734 else
1735 cost += edge_cost;
1737 for (edge = node->callers; edge; edge = edge->next_caller)
1739 int edge_cost = edge->frequency;
1740 cgraph_node_set_iterator csi;
1742 gcc_assert (edge->caller->analyzed);
1743 if (!edge_cost)
1744 edge_cost = 1;
1745 gcc_assert (edge_cost > 0);
1746 csi = cgraph_node_set_find (partition->cgraph_set, edge->caller);
1747 if (!csi_end_p (csi)
1748 && csi.index < last_visited_cgraph_node)
1749 cost -= edge_cost;
1750 else
1751 cost += edge_cost;
1754 else
1756 refs =
1757 &VEC_index (varpool_node_ptr, partition->varpool_set->nodes,
1758 last_visited_varpool_node)->ref_list;
1759 last_visited_varpool_node++;
1762 /* Compute boundary cost of IPA REF edges and at the same time look into
1763 variables referenced from current partition and try to add them. */
1764 for (j = 0; ipa_ref_list_reference_iterate (refs, j, ref); j++)
1765 if (ref->refered_type == IPA_REF_VARPOOL)
1767 varpool_node_set_iterator vsi;
1769 vnode = ipa_ref_varpool_node (ref);
1770 if (!vnode->finalized)
1771 continue;
1772 if (!vnode->aux && partition_varpool_node_p (vnode))
1773 add_varpool_node_to_partition (partition, vnode);
1774 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1775 if (!vsi_end_p (vsi)
1776 && vsi.index < last_visited_varpool_node - !cgraph_p)
1777 cost--, internal++;
1778 else
1779 cost++;
1781 else
1783 cgraph_node_set_iterator csi;
1785 node = ipa_ref_node (ref);
1786 if (!node->analyzed)
1787 continue;
1788 csi = cgraph_node_set_find (partition->cgraph_set, node);
1789 if (!csi_end_p (csi)
1790 && csi.index < last_visited_cgraph_node - cgraph_p)
1791 cost--, internal++;
1792 else
1793 cost++;
1795 for (j = 0; ipa_ref_list_refering_iterate (refs, j, ref); j++)
1796 if (ref->refering_type == IPA_REF_VARPOOL)
1798 varpool_node_set_iterator vsi;
1800 vnode = ipa_ref_refering_varpool_node (ref);
1801 gcc_assert (vnode->finalized);
1802 if (!vnode->aux && partition_varpool_node_p (vnode))
1803 add_varpool_node_to_partition (partition, vnode);
1804 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1805 if (!vsi_end_p (vsi)
1806 && vsi.index < last_visited_varpool_node)
1807 cost--;
1808 else
1809 cost++;
1811 else
1813 cgraph_node_set_iterator csi;
1815 node = ipa_ref_refering_node (ref);
1816 gcc_assert (node->analyzed);
1817 csi = cgraph_node_set_find (partition->cgraph_set, node);
1818 if (!csi_end_p (csi)
1819 && csi.index < last_visited_cgraph_node)
1820 cost--;
1821 else
1822 cost++;
1826 /* If the partition is large enough, start looking for smallest boundary cost. */
1827 if (partition->insns < partition_size * 3 / 4
1828 || best_cost == INT_MAX
1829 || ((!cost
1830 || (best_internal * (HOST_WIDE_INT) cost
1831 > (internal * (HOST_WIDE_INT)best_cost)))
1832 && partition->insns < partition_size * 5 / 4))
1834 best_cost = cost;
1835 best_internal = internal;
1836 best_i = i;
1837 best_n_nodes = VEC_length (cgraph_node_ptr,
1838 partition->cgraph_set->nodes);
1839 best_n_varpool_nodes = VEC_length (varpool_node_ptr,
1840 partition->varpool_set->nodes);
1841 best_total_size = total_size;
1843 if (cgraph_dump_file)
1844 fprintf (cgraph_dump_file, "Step %i: added %s/%i, size %i, cost %i/%i best %i/%i, step %i\n", i,
1845 cgraph_node_name (order[i]), order[i]->uid, partition->insns, cost, internal,
1846 best_cost, best_internal, best_i);
1847 /* Partition is too large, unwind into step when best cost was reached and
1848 start new partition. */
1849 if (partition->insns > 2 * partition_size)
1851 if (best_i != i)
1853 if (cgraph_dump_file)
1854 fprintf (cgraph_dump_file, "Unwinding %i insertions to step %i\n",
1855 i - best_i, best_i);
1856 undo_partition (partition, best_n_nodes, best_n_varpool_nodes);
1858 i = best_i;
1859 /* When we are finished, avoid creating empty partition. */
1860 while (i < n_nodes - 1 && order[i + 1]->aux)
1861 i++;
1862 if (i == n_nodes - 1)
1863 break;
1864 partition = new_partition ("");
1865 last_visited_cgraph_node = 0;
1866 last_visited_varpool_node = 0;
1867 total_size = best_total_size;
1868 cost = 0;
1870 if (cgraph_dump_file)
1871 fprintf (cgraph_dump_file, "New partition\n");
1872 best_n_nodes = 0;
1873 best_n_varpool_nodes = 0;
1874 best_cost = INT_MAX;
1876 /* Since the size of partitions is just approximate, update the size after
1877 we finished current one. */
1878 if (npartitions < PARAM_VALUE (PARAM_LTO_PARTITIONS))
1879 partition_size = total_size
1880 / (PARAM_VALUE (PARAM_LTO_PARTITIONS) - npartitions);
1881 else
1882 partition_size = INT_MAX;
1884 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1885 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1886 npartitions ++;
1890 /* Varables that are not reachable from the code go into last partition. */
1891 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1892 if (partition_varpool_node_p (vnode) && !vnode->aux)
1893 add_varpool_node_to_partition (partition, vnode);
1894 free (order);
1897 /* Promote variable VNODE to be static. */
1899 static bool
1900 promote_var (struct varpool_node *vnode)
1902 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
1903 return false;
1904 gcc_assert (flag_wpa);
1905 TREE_PUBLIC (vnode->decl) = 1;
1906 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
1907 DECL_VISIBILITY_SPECIFIED (vnode->decl) = true;
1908 if (cgraph_dump_file)
1909 fprintf (cgraph_dump_file,
1910 "Promoting var as hidden: %s\n", varpool_node_name (vnode));
1911 return true;
1914 /* Promote function NODE to be static. */
1916 static bool
1917 promote_fn (struct cgraph_node *node)
1919 gcc_assert (flag_wpa);
1920 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1921 return false;
1922 TREE_PUBLIC (node->decl) = 1;
1923 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
1924 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
1925 if (cgraph_dump_file)
1926 fprintf (cgraph_dump_file,
1927 "Promoting function as hidden: %s/%i\n",
1928 cgraph_node_name (node), node->uid);
1929 return true;
1932 /* Find out all static decls that need to be promoted to global because
1933 of cross file sharing. This function must be run in the WPA mode after
1934 all inlinees are added. */
1936 static void
1937 lto_promote_cross_file_statics (void)
1939 struct varpool_node *vnode;
1940 unsigned i, n_sets;
1941 cgraph_node_set set;
1942 varpool_node_set vset;
1943 cgraph_node_set_iterator csi;
1944 varpool_node_set_iterator vsi;
1945 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
1946 struct pointer_set_t *inserted = pointer_set_create ();
1948 gcc_assert (flag_wpa);
1950 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
1951 for (i = 0; i < n_sets; i++)
1953 ltrans_partition part
1954 = VEC_index (ltrans_partition, ltrans_partitions, i);
1955 set = part->cgraph_set;
1956 vset = part->varpool_set;
1958 /* If node called or referred to from other partition, it needs to be
1959 globalized. */
1960 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
1962 struct cgraph_node *node = csi_node (csi);
1963 if (node->local.externally_visible)
1964 continue;
1965 if (node->global.inlined_to)
1966 continue;
1967 if ((!DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1968 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
1969 || reachable_from_other_partition_p (node, set)))
1970 promote_fn (node);
1972 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
1974 vnode = vsi_node (vsi);
1975 /* Constant pool references use internal labels and thus can not
1976 be made global. It is sensible to keep those ltrans local to
1977 allow better optimization. */
1978 if (!DECL_IN_CONSTANT_POOL (vnode->decl) && !DECL_COMDAT (vnode->decl)
1979 && !vnode->externally_visible && vnode->analyzed
1980 && referenced_from_other_partition_p (&vnode->ref_list,
1981 set, vset))
1982 promote_var (vnode);
1985 /* We export the initializer of a read-only var into each partition
1986 referencing the var. Folding might take declarations from the
1987 initializer and use them, so everything referenced from the
1988 initializer can be accessed from this partition after folding.
1990 This means that we need to promote all variables and functions
1991 referenced from all initializers of read-only vars referenced
1992 from this partition that are not in this partition. This needs
1993 to be done recursively. */
1994 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1995 if (const_value_known_p (vnode->decl)
1996 && DECL_INITIAL (vnode->decl)
1997 && !varpool_node_in_set_p (vnode, vset)
1998 && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
1999 && !pointer_set_insert (inserted, vnode))
2000 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
2002 while (!VEC_empty (varpool_node_ptr, promoted_initializers))
2004 int i;
2005 struct ipa_ref *ref;
2007 vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
2008 for (i = 0;
2009 ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref);
2010 i++)
2012 if (ref->refered_type == IPA_REF_CGRAPH)
2014 struct cgraph_node *n = ipa_ref_node (ref);
2015 gcc_assert (!n->global.inlined_to);
2016 if (!n->local.externally_visible
2017 && !cgraph_node_in_set_p (n, set))
2018 promote_fn (n);
2020 else
2022 struct varpool_node *v = ipa_ref_varpool_node (ref);
2023 if (varpool_node_in_set_p (v, vset))
2024 continue;
2026 /* Constant pool references use internal labels and thus
2027 cannot be made global. It is sensible to keep those
2028 ltrans local to allow better optimization. */
2029 if (DECL_IN_CONSTANT_POOL (v->decl))
2031 if (!pointer_set_insert (inserted, vnode))
2032 VEC_safe_push (varpool_node_ptr, heap,
2033 promoted_initializers, v);
2035 else if (!v->externally_visible && v->analyzed)
2037 if (promote_var (v)
2038 && DECL_INITIAL (v->decl)
2039 && const_value_known_p (v->decl)
2040 && !pointer_set_insert (inserted, vnode))
2041 VEC_safe_push (varpool_node_ptr, heap,
2042 promoted_initializers, v);
2048 pointer_set_destroy (inserted);
2051 static lto_file *current_lto_file;
2053 /* Helper for qsort; compare partitions and return one with smaller size.
2054 We sort from greatest to smallest so parallel build doesn't stale on the
2055 longest compilation being executed too late. */
2057 static int
2058 cmp_partitions (const void *a, const void *b)
2060 const struct ltrans_partition_def *pa
2061 = *(struct ltrans_partition_def *const *)a;
2062 const struct ltrans_partition_def *pb
2063 = *(struct ltrans_partition_def *const *)b;
2064 return pb->insns - pa->insns;
2067 /* Write all output files in WPA mode and the file with the list of
2068 LTRANS units. */
2070 static void
2071 lto_wpa_write_files (void)
2073 unsigned i, n_sets;
2074 lto_file *file;
2075 cgraph_node_set set;
2076 varpool_node_set vset;
2077 ltrans_partition part;
2078 FILE *ltrans_output_list_stream;
2079 char *temp_filename;
2080 size_t blen;
2082 /* Open the LTRANS output list. */
2083 if (!ltrans_output_list)
2084 fatal_error ("no LTRANS output list filename provided");
2085 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2086 if (ltrans_output_list_stream == NULL)
2087 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2089 timevar_push (TV_WHOPR_WPA);
2091 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
2092 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
2093 part->cgraph_set->nodes);
2095 /* Find out statics that need to be promoted
2096 to globals with hidden visibility because they are accessed from multiple
2097 partitions. */
2098 lto_promote_cross_file_statics ();
2100 timevar_pop (TV_WHOPR_WPA);
2102 timevar_push (TV_WHOPR_WPA_IO);
2104 /* Generate a prefix for the LTRANS unit files. */
2105 blen = strlen (ltrans_output_list);
2106 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2107 strcpy (temp_filename, ltrans_output_list);
2108 if (blen > sizeof (".out")
2109 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2110 ".out") == 0)
2111 temp_filename[blen - sizeof (".out") + 1] = '\0';
2112 blen = strlen (temp_filename);
2114 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
2115 VEC_qsort (ltrans_partition, ltrans_partitions, cmp_partitions);
2116 for (i = 0; i < n_sets; i++)
2118 size_t len;
2119 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
2121 set = part->cgraph_set;
2122 vset = part->varpool_set;
2124 /* Write all the nodes in SET. */
2125 sprintf (temp_filename + blen, "%u.o", i);
2126 file = lto_obj_file_open (temp_filename, true);
2127 if (!file)
2128 fatal_error ("lto_obj_file_open() failed");
2130 if (!quiet_flag)
2131 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2132 if (cgraph_dump_file)
2134 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
2135 part->name, temp_filename, part->insns);
2136 fprintf (cgraph_dump_file, "cgraph nodes:");
2137 dump_cgraph_node_set (cgraph_dump_file, set);
2138 fprintf (cgraph_dump_file, "varpool nodes:");
2139 dump_varpool_node_set (cgraph_dump_file, vset);
2141 gcc_checking_assert (cgraph_node_set_nonempty_p (set)
2142 || varpool_node_set_nonempty_p (vset) || !i);
2144 lto_set_current_out_file (file);
2146 ipa_write_optimization_summaries (set, vset);
2148 lto_set_current_out_file (NULL);
2149 lto_obj_file_close (file);
2151 len = strlen (temp_filename);
2152 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
2153 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2154 fatal_error ("writing to LTRANS output list %s: %m",
2155 ltrans_output_list);
2158 lto_stats.num_output_files += n_sets;
2160 /* Close the LTRANS output list. */
2161 if (fclose (ltrans_output_list_stream))
2162 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2164 free_ltrans_partitions();
2166 timevar_pop (TV_WHOPR_WPA_IO);
2170 /* If TT is a variable or function decl replace it with its
2171 prevailing variant. */
2172 #define LTO_SET_PREVAIL(tt) \
2173 do {\
2174 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
2175 tt = lto_symtab_prevailing_decl (tt); \
2176 } while (0)
2178 /* Ensure that TT isn't a replacable var of function decl. */
2179 #define LTO_NO_PREVAIL(tt) \
2180 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2182 /* Given a tree T replace all fields referring to variables or functions
2183 with their prevailing variant. */
2184 static void
2185 lto_fixup_prevailing_decls (tree t)
2187 enum tree_code code = TREE_CODE (t);
2188 LTO_NO_PREVAIL (TREE_TYPE (t));
2189 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2190 LTO_NO_PREVAIL (TREE_CHAIN (t));
2191 if (DECL_P (t))
2193 LTO_NO_PREVAIL (DECL_NAME (t));
2194 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2195 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2197 LTO_SET_PREVAIL (DECL_SIZE (t));
2198 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2199 LTO_SET_PREVAIL (DECL_INITIAL (t));
2200 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2201 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2203 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2205 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2206 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
2208 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2210 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
2211 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2212 LTO_NO_PREVAIL (DECL_VINDEX (t));
2214 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2215 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2216 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2218 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
2219 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2220 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2221 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2222 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2225 else if (TYPE_P (t))
2227 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2228 LTO_SET_PREVAIL (TYPE_SIZE (t));
2229 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2230 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2231 LTO_NO_PREVAIL (TYPE_NAME (t));
2233 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2234 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2235 LTO_SET_PREVAIL (t->type_non_common.binfo);
2237 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2239 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2240 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2241 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2243 else if (EXPR_P (t))
2245 int i;
2246 LTO_NO_PREVAIL (t->exp.block);
2247 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2248 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2250 else
2252 switch (code)
2254 case TREE_LIST:
2255 LTO_SET_PREVAIL (TREE_VALUE (t));
2256 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2257 break;
2258 default:
2259 gcc_unreachable ();
2263 #undef LTO_SET_PREVAIL
2264 #undef LTO_NO_PREVAIL
2266 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2267 replaces var and function decls with the corresponding prevailing def. */
2269 static void
2270 lto_fixup_state (struct lto_in_decl_state *state)
2272 unsigned i, si;
2273 struct lto_tree_ref_table *table;
2275 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2276 we still need to walk from all DECLs to find the reachable
2277 FUNCTION_DECLs and VAR_DECLs. */
2278 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2280 table = &state->streams[si];
2281 for (i = 0; i < table->size; i++)
2283 tree *tp = table->trees + i;
2284 if (VAR_OR_FUNCTION_DECL_P (*tp))
2285 *tp = lto_symtab_prevailing_decl (*tp);
2290 /* A callback of htab_traverse. Just extracts a state from SLOT
2291 and calls lto_fixup_state. */
2293 static int
2294 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
2296 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
2297 lto_fixup_state (state);
2298 return 1;
2301 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2302 prevailing one. */
2304 static void
2305 lto_fixup_decls (struct lto_file_decl_data **files)
2307 unsigned int i;
2308 htab_iterator hi;
2309 tree t;
2311 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
2312 lto_fixup_prevailing_decls (t);
2314 for (i = 0; files[i]; i++)
2316 struct lto_file_decl_data *file = files[i];
2317 struct lto_in_decl_state *state = file->global_decl_state;
2318 lto_fixup_state (state);
2320 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
2324 /* Read the options saved from each file in the command line. Called
2325 from lang_hooks.post_options which is called by process_options
2326 right before all the options are used to initialize the compiler.
2327 This assumes that decode_options has already run, so the
2328 num_in_fnames and in_fnames are properly set.
2330 Note that this assumes that all the files had been compiled with
2331 the same options, which is not a good assumption. In general,
2332 options ought to be read from all the files in the set and merged.
2333 However, it is still unclear what the merge rules should be. */
2335 void
2336 lto_read_all_file_options (void)
2338 size_t i;
2340 /* Clear any file options currently saved. */
2341 lto_clear_file_options ();
2343 /* Set the hooks to read ELF sections. */
2344 lto_set_in_hooks (NULL, get_section_data, free_section_data);
2345 if (!quiet_flag)
2346 fprintf (stderr, "Reading command line options:");
2348 for (i = 0; i < num_in_fnames; i++)
2350 struct lto_file_decl_data *file_data;
2351 lto_file *file = lto_obj_file_open (in_fnames[i], false);
2352 if (!file)
2353 break;
2354 if (!quiet_flag)
2356 fprintf (stderr, " %s", in_fnames[i]);
2357 fflush (stderr);
2360 file_data = XCNEW (struct lto_file_decl_data);
2361 file_data->file_name = file->filename;
2362 file_data->section_hash_table = lto_obj_build_section_table (file);
2364 lto_read_file_options (file_data);
2366 lto_obj_file_close (file);
2367 htab_delete (file_data->section_hash_table);
2368 free (file_data);
2371 if (!quiet_flag)
2372 fprintf (stderr, "\n");
2374 /* Apply globally the options read from all the files. */
2375 lto_reissue_options ();
2378 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2380 /* Turn file datas for sub files into a single array, so that they look
2381 like separate files for further passes. */
2383 static void
2384 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2386 struct lto_file_decl_data *n, *next;
2387 int i, k;
2389 lto_stats.num_input_files = count;
2390 all_file_decl_data
2391 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
2392 /* Set the hooks so that all of the ipa passes can read in their data. */
2393 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2394 for (i = 0, k = 0; i < last_file_ix; i++)
2396 for (n = orig[i]; n != NULL; n = next)
2398 all_file_decl_data[k++] = n;
2399 next = n->next;
2400 n->next = NULL;
2403 all_file_decl_data[k] = NULL;
2404 gcc_assert (k == count);
2407 /* Input file data before flattening (i.e. splitting them to subfiles to support
2408 incremental linking. */
2409 static int real_file_count;
2410 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2412 /* Read all the symbols from the input files FNAMES. NFILES is the
2413 number of files requested in the command line. Instantiate a
2414 global call graph by aggregating all the sub-graphs found in each
2415 file. */
2417 static void
2418 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2420 unsigned int i, last_file_ix;
2421 FILE *resolution;
2422 struct cgraph_node *node;
2423 int count = 0;
2424 struct lto_file_decl_data **decl_data;
2426 init_cgraph ();
2428 timevar_push (TV_IPA_LTO_DECL_IN);
2430 real_file_decl_data
2431 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
2432 real_file_count = nfiles;
2434 /* Read the resolution file. */
2435 resolution = NULL;
2436 if (resolution_file_name)
2438 int t;
2439 unsigned num_objects;
2441 resolution = fopen (resolution_file_name, "r");
2442 if (resolution == NULL)
2443 fatal_error ("could not open symbol resolution file: %m");
2445 t = fscanf (resolution, "%u", &num_objects);
2446 gcc_assert (t == 1);
2448 /* True, since the plugin splits the archives. */
2449 gcc_assert (num_objects == nfiles);
2452 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
2453 NULL);
2455 if (!quiet_flag)
2456 fprintf (stderr, "Reading object files:");
2458 /* Read all of the object files specified on the command line. */
2459 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2461 struct lto_file_decl_data *file_data = NULL;
2462 if (!quiet_flag)
2464 fprintf (stderr, " %s", fnames[i]);
2465 fflush (stderr);
2468 current_lto_file = lto_obj_file_open (fnames[i], false);
2469 if (!current_lto_file)
2470 break;
2472 file_data = lto_file_read (current_lto_file, resolution, &count);
2473 if (!file_data)
2475 lto_obj_file_close (current_lto_file);
2476 current_lto_file = NULL;
2477 break;
2480 decl_data[last_file_ix++] = file_data;
2482 lto_obj_file_close (current_lto_file);
2483 current_lto_file = NULL;
2484 ggc_collect ();
2487 lto_flatten_files (decl_data, count, last_file_ix);
2488 lto_stats.num_input_files = count;
2489 ggc_free(decl_data);
2490 real_file_decl_data = NULL;
2492 if (resolution_file_name)
2493 fclose (resolution);
2495 /* Set the hooks so that all of the ipa passes can read in their data. */
2496 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2498 timevar_pop (TV_IPA_LTO_DECL_IN);
2500 if (!quiet_flag)
2501 fprintf (stderr, "\nReading the callgraph\n");
2503 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2504 /* Read the callgraph. */
2505 input_cgraph ();
2506 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2508 if (!quiet_flag)
2509 fprintf (stderr, "Merging declarations\n");
2511 timevar_push (TV_IPA_LTO_DECL_MERGE);
2512 /* Merge global decls. */
2513 lto_symtab_merge_decls ();
2515 /* If there were errors during symbol merging bail out, we have no
2516 good way to recover here. */
2517 if (seen_error ())
2518 fatal_error ("errors during merging of translation units");
2520 /* Fixup all decls and types and free the type hash tables. */
2521 lto_fixup_decls (all_file_decl_data);
2522 htab_delete (tree_with_vars);
2523 tree_with_vars = NULL;
2524 free_gimple_type_tables ();
2525 ggc_collect ();
2527 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2528 /* Each pass will set the appropriate timer. */
2530 if (!quiet_flag)
2531 fprintf (stderr, "Reading summaries\n");
2533 /* Read the IPA summary data. */
2534 if (flag_ltrans)
2535 ipa_read_optimization_summaries ();
2536 else
2537 ipa_read_summaries ();
2539 /* Finally merge the cgraph according to the decl merging decisions. */
2540 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2541 if (cgraph_dump_file)
2543 fprintf (cgraph_dump_file, "Before merging:\n");
2544 dump_cgraph (cgraph_dump_file);
2545 dump_varpool (cgraph_dump_file);
2547 lto_symtab_merge_cgraph_nodes ();
2548 ggc_collect ();
2550 if (flag_ltrans)
2551 for (node = cgraph_nodes; node; node = node->next)
2553 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
2554 summaries computed and needs to apply changes. At the moment WHOPR only
2555 supports inlining, so we can push it here by hand. In future we need to stream
2556 this field into ltrans compilation. */
2557 if (node->analyzed)
2558 VEC_safe_push (ipa_opt_pass, heap,
2559 node->ipa_transforms_to_apply,
2560 (ipa_opt_pass)&pass_ipa_inline);
2562 lto_symtab_free ();
2564 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2566 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
2568 /* FIXME lto. This loop needs to be changed to use the pass manager to
2569 call the ipa passes directly. */
2570 if (!seen_error ())
2571 for (i = 0; i < last_file_ix; i++)
2573 struct lto_file_decl_data *file_data = all_file_decl_data [i];
2574 lto_materialize_constructors_and_inits (file_data);
2577 /* Indicate that the cgraph is built and ready. */
2578 cgraph_function_flags_ready = true;
2580 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
2581 ggc_free (all_file_decl_data);
2582 all_file_decl_data = NULL;
2586 /* Materialize all the bodies for all the nodes in the callgraph. */
2588 static void
2589 materialize_cgraph (void)
2591 tree decl;
2592 struct cgraph_node *node;
2593 unsigned i;
2594 timevar_id_t lto_timer;
2596 if (!quiet_flag)
2597 fprintf (stderr,
2598 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2601 /* Now that we have input the cgraph, we need to clear all of the aux
2602 nodes and read the functions if we are not running in WPA mode. */
2603 timevar_push (TV_IPA_LTO_GIMPLE_IN);
2605 for (node = cgraph_nodes; node; node = node->next)
2607 if (node->local.lto_file_data)
2609 lto_materialize_function (node);
2610 lto_stats.num_input_cgraph_nodes++;
2614 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
2616 /* Start the appropriate timer depending on the mode that we are
2617 operating in. */
2618 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2619 : (flag_ltrans) ? TV_WHOPR_LTRANS
2620 : TV_LTO;
2621 timevar_push (lto_timer);
2623 current_function_decl = NULL;
2624 set_cfun (NULL);
2626 /* Inform the middle end about the global variables we have seen. */
2627 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
2628 rest_of_decl_compilation (decl, 1, 0);
2630 if (!quiet_flag)
2631 fprintf (stderr, "\n");
2633 timevar_pop (lto_timer);
2637 /* Perform whole program analysis (WPA) on the callgraph and write out the
2638 optimization plan. */
2640 static void
2641 do_whole_program_analysis (void)
2643 /* Note that since we are in WPA mode, materialize_cgraph will not
2644 actually read in all the function bodies. It only materializes
2645 the decls and cgraph nodes so that analysis can be performed. */
2646 materialize_cgraph ();
2648 /* Reading in the cgraph uses different timers, start timing WPA now. */
2649 timevar_push (TV_WHOPR_WPA);
2651 if (pre_ipa_mem_report)
2653 fprintf (stderr, "Memory consumption before IPA\n");
2654 dump_memory_report (false);
2657 cgraph_function_flags_ready = true;
2659 if (cgraph_dump_file)
2661 dump_cgraph (cgraph_dump_file);
2662 dump_varpool (cgraph_dump_file);
2664 bitmap_obstack_initialize (NULL);
2665 cgraph_state = CGRAPH_STATE_IPA_SSA;
2667 execute_ipa_pass_list (all_regular_ipa_passes);
2669 if (cgraph_dump_file)
2671 fprintf (cgraph_dump_file, "Optimized ");
2672 dump_cgraph (cgraph_dump_file);
2673 dump_varpool (cgraph_dump_file);
2675 verify_cgraph ();
2676 bitmap_obstack_release (NULL);
2678 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
2679 timevar_pop (TV_WHOPR_WPA);
2681 if (flag_lto_partition_1to1)
2682 lto_1_to_1_map ();
2683 else
2684 lto_balanced_map ();
2686 if (!quiet_flag)
2688 fprintf (stderr, "\nStreaming out");
2689 fflush (stderr);
2691 lto_wpa_write_files ();
2692 ggc_collect ();
2693 if (!quiet_flag)
2694 fprintf (stderr, "\n");
2696 if (post_ipa_mem_report)
2698 fprintf (stderr, "Memory consumption after IPA\n");
2699 dump_memory_report (false);
2702 /* Show the LTO report before launching LTRANS. */
2703 if (flag_lto_report)
2704 print_lto_report ();
2708 static GTY(()) tree lto_eh_personality_decl;
2710 /* Return the LTO personality function decl. */
2712 tree
2713 lto_eh_personality (void)
2715 if (!lto_eh_personality_decl)
2717 /* Use the first personality DECL for our personality if we don't
2718 support multiple ones. This ensures that we don't artificially
2719 create the need for them in a single-language program. */
2720 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2721 lto_eh_personality_decl = first_personality_decl;
2722 else
2723 lto_eh_personality_decl = lhd_gcc_personality ();
2726 return lto_eh_personality_decl;
2729 /* Set the process name based on the LTO mode. */
2731 static void
2732 lto_process_name (void)
2734 if (flag_lto)
2735 setproctitle ("lto1-lto");
2736 if (flag_wpa)
2737 setproctitle ("lto1-wpa");
2738 if (flag_ltrans)
2739 setproctitle ("lto1-ltrans");
2743 /* Initialize the LTO front end. */
2745 static void
2746 lto_init (void)
2748 lto_process_name ();
2749 lto_streamer_hooks_init ();
2750 lto_reader_init ();
2751 memset (&lto_stats, 0, sizeof (lto_stats));
2752 bitmap_obstack_initialize (NULL);
2753 gimple_register_cfg_hooks ();
2757 /* Main entry point for the GIMPLE front end. This front end has
2758 three main personalities:
2760 - LTO (-flto). All the object files on the command line are
2761 loaded in memory and processed as a single translation unit.
2762 This is the traditional link-time optimization behavior.
2764 - WPA (-fwpa). Only the callgraph and summary information for
2765 files in the command file are loaded. A single callgraph
2766 (without function bodies) is instantiated for the whole set of
2767 files. IPA passes are only allowed to analyze the call graph
2768 and make transformation decisions. The callgraph is
2769 partitioned, each partition is written to a new object file
2770 together with the transformation decisions.
2772 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2773 summary files from running again. Since WPA computed summary
2774 information and decided what transformations to apply, LTRANS
2775 simply applies them. */
2777 void
2778 lto_main (void)
2780 /* Initialize the LTO front end. */
2781 lto_init ();
2783 /* Read all the symbols and call graph from all the files in the
2784 command line. */
2785 read_cgraph_and_symbols (num_in_fnames, in_fnames);
2787 if (!seen_error ())
2789 /* If WPA is enabled analyze the whole call graph and create an
2790 optimization plan. Otherwise, read in all the function
2791 bodies and continue with optimization. */
2792 if (flag_wpa)
2793 do_whole_program_analysis ();
2794 else
2796 materialize_cgraph ();
2798 /* Let the middle end know that we have read and merged all of
2799 the input files. */
2800 cgraph_optimize ();
2802 /* FIXME lto, if the processes spawned by WPA fail, we miss
2803 the chance to print WPA's report, so WPA will call
2804 print_lto_report before launching LTRANS. If LTRANS was
2805 launched directly by the driver we would not need to do
2806 this. */
2807 if (flag_lto_report)
2808 print_lto_report ();
2813 #include "gt-lto-lto.h"