2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lto / lto.c
blob4bb0aafcb7db1150846b01836d5e678a32ab3be6
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2015 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 "alias.h"
27 #include "tm.h"
28 #include "function.h"
29 #include "bitmap.h"
30 #include "cfghooks.h"
31 #include "basic-block.h"
32 #include "tree.h"
33 #include "gimple.h"
34 #include "hard-reg-set.h"
35 #include "options.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "diagnostic-core.h"
39 #include "cgraph.h"
40 #include "tree-ssa-operands.h"
41 #include "tree-pass.h"
42 #include "langhooks.h"
43 #include "alloc-pool.h"
44 #include "symbol-summary.h"
45 #include "ipa-prop.h"
46 #include "common.h"
47 #include "debug.h"
48 #include "internal-fn.h"
49 #include "lto.h"
50 #include "lto-tree.h"
51 #include "tree-streamer.h"
52 #include "lto-section-names.h"
53 #include "splay-tree.h"
54 #include "lto-partition.h"
55 #include "context.h"
56 #include "pass_manager.h"
57 #include "ipa-inline.h"
58 #include "params.h"
59 #include "ipa-utils.h"
60 #include "gomp-constants.h"
63 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
64 static int lto_parallelism;
66 static GTY(()) tree first_personality_decl;
68 static GTY(()) const unsigned char *lto_mode_identity_table;
70 /* Returns a hash code for P. */
72 static hashval_t
73 hash_name (const void *p)
75 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
76 return (hashval_t) htab_hash_string (ds->name);
80 /* Returns nonzero if P1 and P2 are equal. */
82 static int
83 eq_name (const void *p1, const void *p2)
85 const struct lto_section_slot *s1 =
86 (const struct lto_section_slot *) p1;
87 const struct lto_section_slot *s2 =
88 (const struct lto_section_slot *) p2;
90 return strcmp (s1->name, s2->name) == 0;
93 /* Free lto_section_slot */
95 static void
96 free_with_string (void *arg)
98 struct lto_section_slot *s = (struct lto_section_slot *)arg;
100 free (CONST_CAST (char *, s->name));
101 free (arg);
104 /* Create section hash table */
106 htab_t
107 lto_obj_create_section_hash_table (void)
109 return htab_create (37, hash_name, eq_name, free_with_string);
112 /* Delete an allocated integer KEY in the splay tree. */
114 static void
115 lto_splay_tree_delete_id (splay_tree_key key)
117 free ((void *) key);
120 /* Compare splay tree node ids A and B. */
122 static int
123 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
125 unsigned HOST_WIDE_INT ai;
126 unsigned HOST_WIDE_INT bi;
128 ai = *(unsigned HOST_WIDE_INT *) a;
129 bi = *(unsigned HOST_WIDE_INT *) b;
131 if (ai < bi)
132 return -1;
133 else if (ai > bi)
134 return 1;
135 return 0;
138 /* Look up splay tree node by ID in splay tree T. */
140 static splay_tree_node
141 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
143 return splay_tree_lookup (t, (splay_tree_key) &id);
146 /* Check if KEY has ID. */
148 static bool
149 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
151 return *(unsigned HOST_WIDE_INT *) key == id;
154 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
155 The ID is allocated separately because we need HOST_WIDE_INTs which may
156 be wider than a splay_tree_key. */
158 static void
159 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
160 struct lto_file_decl_data *file_data)
162 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
163 *idp = id;
164 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
167 /* Create a splay tree. */
169 static splay_tree
170 lto_splay_tree_new (void)
172 return splay_tree_new (lto_splay_tree_compare_ids,
173 lto_splay_tree_delete_id,
174 NULL);
177 /* Return true when NODE has a clone that is analyzed (i.e. we need
178 to load its body even if the node itself is not needed). */
180 static bool
181 has_analyzed_clone_p (struct cgraph_node *node)
183 struct cgraph_node *orig = node;
184 node = node->clones;
185 if (node)
186 while (node != orig)
188 if (node->analyzed)
189 return true;
190 if (node->clones)
191 node = node->clones;
192 else if (node->next_sibling_clone)
193 node = node->next_sibling_clone;
194 else
196 while (node != orig && !node->next_sibling_clone)
197 node = node->clone_of;
198 if (node != orig)
199 node = node->next_sibling_clone;
202 return false;
205 /* Read the function body for the function associated with NODE. */
207 static void
208 lto_materialize_function (struct cgraph_node *node)
210 tree decl;
212 decl = node->decl;
213 /* Read in functions with body (analyzed nodes)
214 and also functions that are needed to produce virtual clones. */
215 if ((node->has_gimple_body_p () && node->analyzed)
216 || node->used_as_abstract_origin
217 || has_analyzed_clone_p (node))
219 /* Clones don't need to be read. */
220 if (node->clone_of)
221 return;
222 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
223 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
226 /* Let the middle end know about the function. */
227 rest_of_decl_compilation (decl, 1, 0);
231 /* Decode the content of memory pointed to by DATA in the in decl
232 state object STATE. DATA_IN points to a data_in structure for
233 decoding. Return the address after the decoded object in the
234 input. */
236 static const uint32_t *
237 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
238 struct lto_in_decl_state *state)
240 uint32_t ix;
241 tree decl;
242 uint32_t i, j;
244 ix = *data++;
245 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
246 if (!VAR_OR_FUNCTION_DECL_P (decl))
248 gcc_assert (decl == void_type_node);
249 decl = NULL_TREE;
251 state->fn_decl = decl;
253 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
255 uint32_t size = *data++;
256 vec<tree, va_gc> *decls = NULL;
257 vec_alloc (decls, size);
259 for (j = 0; j < size; j++)
260 vec_safe_push (decls,
261 streamer_tree_cache_get_tree (data_in->reader_cache,
262 data[j]));
264 state->streams[i] = decls;
265 data += size;
268 return data;
272 /* Global canonical type table. */
273 static htab_t gimple_canonical_types;
274 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
275 static unsigned long num_canonical_type_hash_entries;
276 static unsigned long num_canonical_type_hash_queries;
278 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
279 static hashval_t gimple_canonical_type_hash (const void *p);
280 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
282 /* Returning a hash value for gimple type TYPE.
284 The hash value returned is equal for types considered compatible
285 by gimple_canonical_types_compatible_p. */
287 static hashval_t
288 hash_canonical_type (tree type)
290 inchash::hash hstate;
292 /* We compute alias sets only for types that needs them.
293 Be sure we do not recurse to something else as we can not hash incomplete
294 types in a way they would have same hash value as compatible complete
295 types. */
296 gcc_checking_assert (type_with_alias_set_p (type));
298 /* Combine a few common features of types so that types are grouped into
299 smaller sets; when searching for existing matching types to merge,
300 only existing types having the same features as the new type will be
301 checked. */
302 hstate.add_int (tree_code_for_canonical_type_merging (TREE_CODE (type)));
303 hstate.add_int (TYPE_MODE (type));
305 /* Incorporate common features of numerical types. */
306 if (INTEGRAL_TYPE_P (type)
307 || SCALAR_FLOAT_TYPE_P (type)
308 || FIXED_POINT_TYPE_P (type)
309 || TREE_CODE (type) == OFFSET_TYPE
310 || POINTER_TYPE_P (type))
312 hstate.add_int (TYPE_UNSIGNED (type));
313 hstate.add_int (TYPE_PRECISION (type));
316 if (VECTOR_TYPE_P (type))
318 hstate.add_int (TYPE_VECTOR_SUBPARTS (type));
319 hstate.add_int (TYPE_UNSIGNED (type));
322 if (TREE_CODE (type) == COMPLEX_TYPE)
323 hstate.add_int (TYPE_UNSIGNED (type));
325 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
326 interoperable with "signed char". Unless all frontends are revisited to
327 agree on these types, we must ignore the flag completely. */
329 /* Fortran standard define C_PTR type that is compatible with every
330 C pointer. For this reason we need to glob all pointers into one.
331 Still pointers in different address spaces are not compatible. */
332 if (POINTER_TYPE_P (type))
333 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
335 /* For array types hash the domain bounds and the string flag. */
336 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
338 hstate.add_int (TYPE_STRING_FLAG (type));
339 /* OMP lowering can introduce error_mark_node in place of
340 random local decls in types. */
341 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
342 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
343 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
344 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
347 /* Recurse for aggregates with a single element type. */
348 if (TREE_CODE (type) == ARRAY_TYPE
349 || TREE_CODE (type) == COMPLEX_TYPE
350 || TREE_CODE (type) == VECTOR_TYPE)
351 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
353 /* Incorporate function return and argument types. */
354 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
356 unsigned na;
357 tree p;
359 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
361 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
363 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
364 na++;
367 hstate.add_int (na);
370 if (RECORD_OR_UNION_TYPE_P (type))
372 unsigned nf;
373 tree f;
375 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
376 if (TREE_CODE (f) == FIELD_DECL)
378 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
379 nf++;
382 hstate.add_int (nf);
385 return hstate.end();
388 /* Returning a hash value for gimple type TYPE combined with VAL. */
390 static void
391 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
393 hashval_t v;
395 /* All type variants have same TYPE_CANONICAL. */
396 type = TYPE_MAIN_VARIANT (type);
397 /* An already processed type. */
398 if (TYPE_CANONICAL (type))
400 type = TYPE_CANONICAL (type);
401 v = gimple_canonical_type_hash (type);
403 else
405 /* Canonical types should not be able to form SCCs by design, this
406 recursion is just because we do not register canonical types in
407 optimal order. To avoid quadratic behavior also register the
408 type here. */
409 v = hash_canonical_type (type);
410 gimple_register_canonical_type_1 (type, v);
412 hstate.add_int (v);
415 /* Returns the hash for a canonical type P. */
417 static hashval_t
418 gimple_canonical_type_hash (const void *p)
420 num_canonical_type_hash_queries++;
421 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
422 gcc_assert (slot != NULL);
423 return *slot;
428 /* Returns nonzero if P1 and P2 are equal. */
430 static int
431 gimple_canonical_type_eq (const void *p1, const void *p2)
433 const_tree t1 = (const_tree) p1;
434 const_tree t2 = (const_tree) p2;
435 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
436 CONST_CAST_TREE (t2));
439 /* Main worker for gimple_register_canonical_type. */
441 static void
442 gimple_register_canonical_type_1 (tree t, hashval_t hash)
444 void **slot;
446 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
448 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
449 if (*slot)
451 tree new_type = (tree)(*slot);
452 gcc_checking_assert (new_type != t);
453 TYPE_CANONICAL (t) = new_type;
455 else
457 TYPE_CANONICAL (t) = t;
458 *slot = (void *) t;
459 /* Cache the just computed hash value. */
460 num_canonical_type_hash_entries++;
461 bool existed_p = canonical_type_hash_cache->put (t, hash);
462 gcc_assert (!existed_p);
466 /* Register type T in the global type table gimple_types and set
467 TYPE_CANONICAL of T accordingly.
468 This is used by LTO to merge structurally equivalent types for
469 type-based aliasing purposes across different TUs and languages.
471 ??? This merging does not exactly match how the tree.c middle-end
472 functions will assign TYPE_CANONICAL when new types are created
473 during optimization (which at least happens for pointer and array
474 types). */
476 static void
477 gimple_register_canonical_type (tree t)
479 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t))
480 return;
482 /* Canonical types are same among all complete variants. */
483 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
484 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
485 else
487 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t),
488 hash_canonical_type (TYPE_MAIN_VARIANT (t)));
489 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
493 /* Re-compute TYPE_CANONICAL for NODE and related types. */
495 static void
496 lto_register_canonical_types (tree node, bool first_p)
498 if (!node
499 || !TYPE_P (node))
500 return;
502 if (first_p)
503 TYPE_CANONICAL (node) = NULL_TREE;
505 if (POINTER_TYPE_P (node)
506 || TREE_CODE (node) == COMPLEX_TYPE
507 || TREE_CODE (node) == ARRAY_TYPE)
508 lto_register_canonical_types (TREE_TYPE (node), first_p);
510 if (!first_p)
511 gimple_register_canonical_type (node);
515 /* Remember trees that contains references to declarations. */
516 static GTY(()) vec <tree, va_gc> *tree_with_vars;
518 #define CHECK_VAR(tt) \
519 do \
521 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
522 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
523 return true; \
524 } while (0)
526 #define CHECK_NO_VAR(tt) \
527 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
529 /* Check presence of pointers to decls in fields of a tree_typed T. */
531 static inline bool
532 mentions_vars_p_typed (tree t)
534 CHECK_NO_VAR (TREE_TYPE (t));
535 return false;
538 /* Check presence of pointers to decls in fields of a tree_common T. */
540 static inline bool
541 mentions_vars_p_common (tree t)
543 if (mentions_vars_p_typed (t))
544 return true;
545 CHECK_NO_VAR (TREE_CHAIN (t));
546 return false;
549 /* Check presence of pointers to decls in fields of a decl_minimal T. */
551 static inline bool
552 mentions_vars_p_decl_minimal (tree t)
554 if (mentions_vars_p_common (t))
555 return true;
556 CHECK_NO_VAR (DECL_NAME (t));
557 CHECK_VAR (DECL_CONTEXT (t));
558 return false;
561 /* Check presence of pointers to decls in fields of a decl_common T. */
563 static inline bool
564 mentions_vars_p_decl_common (tree t)
566 if (mentions_vars_p_decl_minimal (t))
567 return true;
568 CHECK_VAR (DECL_SIZE (t));
569 CHECK_VAR (DECL_SIZE_UNIT (t));
570 CHECK_VAR (DECL_INITIAL (t));
571 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
572 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
573 return false;
576 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
578 static inline bool
579 mentions_vars_p_decl_with_vis (tree t)
581 if (mentions_vars_p_decl_common (t))
582 return true;
584 /* Accessor macro has side-effects, use field-name here. */
585 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
586 return false;
589 /* Check presence of pointers to decls in fields of a decl_non_common T. */
591 static inline bool
592 mentions_vars_p_decl_non_common (tree t)
594 if (mentions_vars_p_decl_with_vis (t))
595 return true;
596 CHECK_NO_VAR (DECL_RESULT_FLD (t));
597 return false;
600 /* Check presence of pointers to decls in fields of a decl_non_common T. */
602 static bool
603 mentions_vars_p_function (tree t)
605 if (mentions_vars_p_decl_non_common (t))
606 return true;
607 CHECK_NO_VAR (DECL_ARGUMENTS (t));
608 CHECK_NO_VAR (DECL_VINDEX (t));
609 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
610 return false;
613 /* Check presence of pointers to decls in fields of a field_decl T. */
615 static bool
616 mentions_vars_p_field_decl (tree t)
618 if (mentions_vars_p_decl_common (t))
619 return true;
620 CHECK_VAR (DECL_FIELD_OFFSET (t));
621 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
622 CHECK_NO_VAR (DECL_QUALIFIER (t));
623 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
624 CHECK_NO_VAR (DECL_FCONTEXT (t));
625 return false;
628 /* Check presence of pointers to decls in fields of a type T. */
630 static bool
631 mentions_vars_p_type (tree t)
633 if (mentions_vars_p_common (t))
634 return true;
635 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
636 CHECK_VAR (TYPE_SIZE (t));
637 CHECK_VAR (TYPE_SIZE_UNIT (t));
638 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
639 CHECK_NO_VAR (TYPE_NAME (t));
641 CHECK_VAR (TYPE_MINVAL (t));
642 CHECK_VAR (TYPE_MAXVAL (t));
644 /* Accessor is for derived node types only. */
645 CHECK_NO_VAR (t->type_non_common.binfo);
647 CHECK_VAR (TYPE_CONTEXT (t));
648 CHECK_NO_VAR (TYPE_CANONICAL (t));
649 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
650 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
651 return false;
654 /* Check presence of pointers to decls in fields of a BINFO T. */
656 static bool
657 mentions_vars_p_binfo (tree t)
659 unsigned HOST_WIDE_INT i, n;
661 if (mentions_vars_p_common (t))
662 return true;
663 CHECK_VAR (BINFO_VTABLE (t));
664 CHECK_NO_VAR (BINFO_OFFSET (t));
665 CHECK_NO_VAR (BINFO_VIRTUALS (t));
666 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
667 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
668 for (i = 0; i < n; i++)
669 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
670 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
671 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
672 n = BINFO_N_BASE_BINFOS (t);
673 for (i = 0; i < n; i++)
674 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
675 return false;
678 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
680 static bool
681 mentions_vars_p_constructor (tree t)
683 unsigned HOST_WIDE_INT idx;
684 constructor_elt *ce;
686 if (mentions_vars_p_typed (t))
687 return true;
689 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
691 CHECK_NO_VAR (ce->index);
692 CHECK_VAR (ce->value);
694 return false;
697 /* Check presence of pointers to decls in fields of an expression tree T. */
699 static bool
700 mentions_vars_p_expr (tree t)
702 int i;
703 if (mentions_vars_p_typed (t))
704 return true;
705 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
706 CHECK_VAR (TREE_OPERAND (t, i));
707 return false;
710 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
712 static bool
713 mentions_vars_p_omp_clause (tree t)
715 int i;
716 if (mentions_vars_p_common (t))
717 return true;
718 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
719 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
720 return false;
723 /* Check presence of pointers to decls that needs later fixup in T. */
725 static bool
726 mentions_vars_p (tree t)
728 switch (TREE_CODE (t))
730 case IDENTIFIER_NODE:
731 break;
733 case TREE_LIST:
734 CHECK_VAR (TREE_VALUE (t));
735 CHECK_VAR (TREE_PURPOSE (t));
736 CHECK_NO_VAR (TREE_CHAIN (t));
737 break;
739 case FIELD_DECL:
740 return mentions_vars_p_field_decl (t);
742 case LABEL_DECL:
743 case CONST_DECL:
744 case PARM_DECL:
745 case RESULT_DECL:
746 case IMPORTED_DECL:
747 case NAMESPACE_DECL:
748 case NAMELIST_DECL:
749 return mentions_vars_p_decl_common (t);
751 case VAR_DECL:
752 return mentions_vars_p_decl_with_vis (t);
754 case TYPE_DECL:
755 return mentions_vars_p_decl_non_common (t);
757 case FUNCTION_DECL:
758 return mentions_vars_p_function (t);
760 case TREE_BINFO:
761 return mentions_vars_p_binfo (t);
763 case PLACEHOLDER_EXPR:
764 return mentions_vars_p_common (t);
766 case BLOCK:
767 case TRANSLATION_UNIT_DECL:
768 case OPTIMIZATION_NODE:
769 case TARGET_OPTION_NODE:
770 break;
772 case CONSTRUCTOR:
773 return mentions_vars_p_constructor (t);
775 case OMP_CLAUSE:
776 return mentions_vars_p_omp_clause (t);
778 default:
779 if (TYPE_P (t))
781 if (mentions_vars_p_type (t))
782 return true;
784 else if (EXPR_P (t))
786 if (mentions_vars_p_expr (t))
787 return true;
789 else if (CONSTANT_CLASS_P (t))
790 CHECK_NO_VAR (TREE_TYPE (t));
791 else
792 gcc_unreachable ();
794 return false;
798 /* Return the resolution for the decl with index INDEX from DATA_IN. */
800 static enum ld_plugin_symbol_resolution
801 get_resolution (struct data_in *data_in, unsigned index)
803 if (data_in->globals_resolution.exists ())
805 ld_plugin_symbol_resolution_t ret;
806 /* We can have references to not emitted functions in
807 DECL_FUNCTION_PERSONALITY at least. So we can and have
808 to indeed return LDPR_UNKNOWN in some cases. */
809 if (data_in->globals_resolution.length () <= index)
810 return LDPR_UNKNOWN;
811 ret = data_in->globals_resolution[index];
812 return ret;
814 else
815 /* Delay resolution finding until decl merging. */
816 return LDPR_UNKNOWN;
819 /* We need to record resolutions until symbol table is read. */
820 static void
821 register_resolution (struct lto_file_decl_data *file_data, tree decl,
822 enum ld_plugin_symbol_resolution resolution)
824 if (resolution == LDPR_UNKNOWN)
825 return;
826 if (!file_data->resolution_map)
827 file_data->resolution_map
828 = new hash_map<tree, ld_plugin_symbol_resolution>;
829 file_data->resolution_map->put (decl, resolution);
832 /* Register DECL with the global symbol table and change its
833 name if necessary to avoid name clashes for static globals across
834 different files. */
836 static void
837 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
838 unsigned ix)
840 tree context;
842 /* Variable has file scope, not local. */
843 if (!TREE_PUBLIC (decl)
844 && !((context = decl_function_context (decl))
845 && auto_var_in_fn_p (decl, context)))
846 rest_of_decl_compilation (decl, 1, 0);
848 /* If this variable has already been declared, queue the
849 declaration for merging. */
850 if (TREE_PUBLIC (decl))
851 register_resolution (data_in->file_data,
852 decl, get_resolution (data_in, ix));
856 /* Register DECL with the global symbol table and change its
857 name if necessary to avoid name clashes for static globals across
858 different files. DATA_IN contains descriptors and tables for the
859 file being read. */
861 static void
862 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
863 unsigned ix)
865 /* If this variable has already been declared, queue the
866 declaration for merging. */
867 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
868 register_resolution (data_in->file_data,
869 decl, get_resolution (data_in, ix));
873 /* For the type T re-materialize it in the type variant list and
874 the pointer/reference-to chains. */
876 static void
877 lto_fixup_prevailing_type (tree t)
879 /* The following re-creates proper variant lists while fixing up
880 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
881 variant list state before fixup is broken. */
883 /* If we are not our own variant leader link us into our new leaders
884 variant list. */
885 if (TYPE_MAIN_VARIANT (t) != t)
887 tree mv = TYPE_MAIN_VARIANT (t);
888 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
889 TYPE_NEXT_VARIANT (mv) = t;
892 /* The following reconstructs the pointer chains
893 of the new pointed-to type if we are a main variant. We do
894 not stream those so they are broken before fixup. */
895 if (TREE_CODE (t) == POINTER_TYPE
896 && TYPE_MAIN_VARIANT (t) == t)
898 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
899 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
901 else if (TREE_CODE (t) == REFERENCE_TYPE
902 && TYPE_MAIN_VARIANT (t) == t)
904 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
905 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
910 /* We keep prevailing tree SCCs in a hashtable with manual collision
911 handling (in case all hashes compare the same) and keep the colliding
912 entries in the tree_scc->next chain. */
914 struct tree_scc
916 tree_scc *next;
917 /* Hash of the whole SCC. */
918 hashval_t hash;
919 /* Number of trees in the SCC. */
920 unsigned len;
921 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
922 which share the same individual tree hash). */
923 unsigned entry_len;
924 /* The members of the SCC.
925 We only need to remember the first entry node candidate for prevailing
926 SCCs (but of course have access to all entries for SCCs we are
927 processing).
928 ??? For prevailing SCCs we really only need hash and the first
929 entry candidate, but that's too awkward to implement. */
930 tree entries[1];
933 struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
935 static inline hashval_t hash (const tree_scc *);
936 static inline bool equal (const tree_scc *, const tree_scc *);
939 hashval_t
940 tree_scc_hasher::hash (const tree_scc *scc)
942 return scc->hash;
945 bool
946 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
948 if (scc1->hash != scc2->hash
949 || scc1->len != scc2->len
950 || scc1->entry_len != scc2->entry_len)
951 return false;
952 return true;
955 static hash_table<tree_scc_hasher> *tree_scc_hash;
956 static struct obstack tree_scc_hash_obstack;
958 static unsigned long num_merged_types;
959 static unsigned long num_prevailing_types;
960 static unsigned long num_type_scc_trees;
961 static unsigned long total_scc_size;
962 static unsigned long num_sccs_read;
963 static unsigned long total_scc_size_merged;
964 static unsigned long num_sccs_merged;
965 static unsigned long num_scc_compares;
966 static unsigned long num_scc_compare_collisions;
969 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
970 recursing through in-SCC tree edges. Returns true if the SCCs entered
971 through T1 and T2 are equal and fills in *MAP with the pairs of
972 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
974 static bool
975 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
977 enum tree_code code;
979 /* Mark already visited nodes. */
980 TREE_ASM_WRITTEN (t2) = 1;
982 /* Push the pair onto map. */
983 (*map)[0] = t1;
984 (*map)[1] = t2;
985 *map = *map + 2;
987 /* Compare value-fields. */
988 #define compare_values(X) \
989 do { \
990 if (X(t1) != X(t2)) \
991 return false; \
992 } while (0)
994 compare_values (TREE_CODE);
995 code = TREE_CODE (t1);
997 if (!TYPE_P (t1))
999 compare_values (TREE_SIDE_EFFECTS);
1000 compare_values (TREE_CONSTANT);
1001 compare_values (TREE_READONLY);
1002 compare_values (TREE_PUBLIC);
1004 compare_values (TREE_ADDRESSABLE);
1005 compare_values (TREE_THIS_VOLATILE);
1006 if (DECL_P (t1))
1007 compare_values (DECL_UNSIGNED);
1008 else if (TYPE_P (t1))
1009 compare_values (TYPE_UNSIGNED);
1010 if (TYPE_P (t1))
1011 compare_values (TYPE_ARTIFICIAL);
1012 else
1013 compare_values (TREE_NO_WARNING);
1014 compare_values (TREE_NOTHROW);
1015 compare_values (TREE_STATIC);
1016 if (code != TREE_BINFO)
1017 compare_values (TREE_PRIVATE);
1018 compare_values (TREE_PROTECTED);
1019 compare_values (TREE_DEPRECATED);
1020 if (TYPE_P (t1))
1022 compare_values (TYPE_SATURATING);
1023 compare_values (TYPE_ADDR_SPACE);
1025 else if (code == SSA_NAME)
1026 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1028 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1030 if (!wi::eq_p (t1, t2))
1031 return false;
1034 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1036 /* ??? No suitable compare routine available. */
1037 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1038 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1039 if (r1.cl != r2.cl
1040 || r1.decimal != r2.decimal
1041 || r1.sign != r2.sign
1042 || r1.signalling != r2.signalling
1043 || r1.canonical != r2.canonical
1044 || r1.uexp != r2.uexp)
1045 return false;
1046 for (unsigned i = 0; i < SIGSZ; ++i)
1047 if (r1.sig[i] != r2.sig[i])
1048 return false;
1051 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1052 if (!fixed_compare (EQ_EXPR,
1053 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1054 return false;
1057 /* We want to compare locations up to the point where it makes
1058 a difference for streaming - thus whether the decl is builtin or not. */
1059 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1060 compare_values (streamer_handle_as_builtin_p);
1062 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1064 compare_values (DECL_MODE);
1065 compare_values (DECL_NONLOCAL);
1066 compare_values (DECL_VIRTUAL_P);
1067 compare_values (DECL_IGNORED_P);
1068 compare_values (DECL_ABSTRACT_P);
1069 compare_values (DECL_ARTIFICIAL);
1070 compare_values (DECL_USER_ALIGN);
1071 compare_values (DECL_PRESERVE_P);
1072 compare_values (DECL_EXTERNAL);
1073 compare_values (DECL_GIMPLE_REG_P);
1074 compare_values (DECL_ALIGN);
1075 if (code == LABEL_DECL)
1077 compare_values (EH_LANDING_PAD_NR);
1078 compare_values (LABEL_DECL_UID);
1080 else if (code == FIELD_DECL)
1082 compare_values (DECL_PACKED);
1083 compare_values (DECL_NONADDRESSABLE_P);
1084 compare_values (DECL_OFFSET_ALIGN);
1086 else if (code == VAR_DECL)
1088 compare_values (DECL_HAS_DEBUG_EXPR_P);
1089 compare_values (DECL_NONLOCAL_FRAME);
1091 if (code == RESULT_DECL
1092 || code == PARM_DECL
1093 || code == VAR_DECL)
1095 compare_values (DECL_BY_REFERENCE);
1096 if (code == VAR_DECL
1097 || code == PARM_DECL)
1098 compare_values (DECL_HAS_VALUE_EXPR_P);
1102 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1103 compare_values (DECL_REGISTER);
1105 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1107 compare_values (DECL_COMMON);
1108 compare_values (DECL_DLLIMPORT_P);
1109 compare_values (DECL_WEAK);
1110 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1111 compare_values (DECL_COMDAT);
1112 compare_values (DECL_VISIBILITY);
1113 compare_values (DECL_VISIBILITY_SPECIFIED);
1114 if (code == VAR_DECL)
1116 compare_values (DECL_HARD_REGISTER);
1117 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1118 compare_values (DECL_IN_CONSTANT_POOL);
1122 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1124 compare_values (DECL_BUILT_IN_CLASS);
1125 compare_values (DECL_STATIC_CONSTRUCTOR);
1126 compare_values (DECL_STATIC_DESTRUCTOR);
1127 compare_values (DECL_UNINLINABLE);
1128 compare_values (DECL_POSSIBLY_INLINED);
1129 compare_values (DECL_IS_NOVOPS);
1130 compare_values (DECL_IS_RETURNS_TWICE);
1131 compare_values (DECL_IS_MALLOC);
1132 compare_values (DECL_IS_OPERATOR_NEW);
1133 compare_values (DECL_DECLARED_INLINE_P);
1134 compare_values (DECL_STATIC_CHAIN);
1135 compare_values (DECL_NO_INLINE_WARNING_P);
1136 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1137 compare_values (DECL_NO_LIMIT_STACK);
1138 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1139 compare_values (DECL_PURE_P);
1140 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1141 compare_values (DECL_FINAL_P);
1142 compare_values (DECL_CXX_CONSTRUCTOR_P);
1143 compare_values (DECL_CXX_DESTRUCTOR_P);
1144 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1145 compare_values (DECL_FUNCTION_CODE);
1148 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1150 compare_values (TYPE_MODE);
1151 compare_values (TYPE_STRING_FLAG);
1152 compare_values (TYPE_NEEDS_CONSTRUCTING);
1153 if (RECORD_OR_UNION_TYPE_P (t1))
1155 compare_values (TYPE_TRANSPARENT_AGGR);
1156 compare_values (TYPE_FINAL_P);
1158 else if (code == ARRAY_TYPE)
1159 compare_values (TYPE_NONALIASED_COMPONENT);
1160 compare_values (TYPE_PACKED);
1161 compare_values (TYPE_RESTRICT);
1162 compare_values (TYPE_USER_ALIGN);
1163 compare_values (TYPE_READONLY);
1164 compare_values (TYPE_PRECISION);
1165 compare_values (TYPE_ALIGN);
1166 compare_values (TYPE_ALIAS_SET);
1169 /* We don't want to compare locations, so there is nothing do compare
1170 for TS_EXP. */
1172 /* BLOCKs are function local and we don't merge anything there, so
1173 simply refuse to merge. */
1174 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1175 return false;
1177 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1178 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1179 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1180 return false;
1182 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1183 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1184 return false;
1186 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1187 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1188 sizeof (struct cl_optimization)) != 0)
1189 return false;
1191 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1192 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1193 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1194 return false;
1196 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1197 compare_values (CONSTRUCTOR_NELTS);
1199 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1200 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1201 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1202 IDENTIFIER_LENGTH (t1)) != 0)
1203 return false;
1205 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1206 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1207 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1208 TREE_STRING_LENGTH (t1)) != 0)
1209 return false;
1211 if (code == OMP_CLAUSE)
1213 compare_values (OMP_CLAUSE_CODE);
1214 switch (OMP_CLAUSE_CODE (t1))
1216 case OMP_CLAUSE_DEFAULT:
1217 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1218 break;
1219 case OMP_CLAUSE_SCHEDULE:
1220 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1221 break;
1222 case OMP_CLAUSE_DEPEND:
1223 compare_values (OMP_CLAUSE_DEPEND_KIND);
1224 break;
1225 case OMP_CLAUSE_MAP:
1226 compare_values (OMP_CLAUSE_MAP_KIND);
1227 break;
1228 case OMP_CLAUSE_PROC_BIND:
1229 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1230 break;
1231 case OMP_CLAUSE_REDUCTION:
1232 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1233 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1234 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1235 break;
1236 default:
1237 break;
1241 #undef compare_values
1244 /* Compare pointer fields. */
1246 /* Recurse. Search & Replaced from DFS_write_tree_body.
1247 Folding the early checks into the compare_tree_edges recursion
1248 macro makes debugging way quicker as you are able to break on
1249 compare_tree_sccs_1 and simply finish until a call returns false
1250 to spot the SCC members with the difference. */
1251 #define compare_tree_edges(E1, E2) \
1252 do { \
1253 tree t1_ = (E1), t2_ = (E2); \
1254 if (t1_ != t2_ \
1255 && (!t1_ || !t2_ \
1256 || !TREE_VISITED (t2_) \
1257 || (!TREE_ASM_WRITTEN (t2_) \
1258 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1259 return false; \
1260 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1261 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1262 } while (0)
1264 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1266 if (code != IDENTIFIER_NODE)
1267 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1270 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1272 unsigned i;
1273 /* Note that the number of elements for EXPR has already been emitted
1274 in EXPR's header (see streamer_write_tree_header). */
1275 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1276 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1279 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1281 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1282 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1285 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1287 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1288 /* ??? Global decls from different TUs have non-matching
1289 TRANSLATION_UNIT_DECLs. Only consider a small set of
1290 decls equivalent, we should not end up merging others. */
1291 if ((code == TYPE_DECL
1292 || code == NAMESPACE_DECL
1293 || code == IMPORTED_DECL
1294 || code == CONST_DECL
1295 || (VAR_OR_FUNCTION_DECL_P (t1)
1296 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1297 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1299 else
1300 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1303 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1305 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1306 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1307 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1308 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1), DECL_ABSTRACT_ORIGIN (t2));
1309 if ((code == VAR_DECL
1310 || code == PARM_DECL)
1311 && DECL_HAS_VALUE_EXPR_P (t1))
1312 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1313 if (code == VAR_DECL
1314 && DECL_HAS_DEBUG_EXPR_P (t1))
1315 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1316 /* LTO specific edges. */
1317 if (code != FUNCTION_DECL
1318 && code != TRANSLATION_UNIT_DECL)
1319 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1322 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1324 if (code == FUNCTION_DECL)
1326 tree a1, a2;
1327 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1328 a1 || a2;
1329 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1330 compare_tree_edges (a1, a2);
1331 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1333 else if (code == TYPE_DECL)
1334 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1337 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1339 /* Make sure we don't inadvertently set the assembler name. */
1340 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1341 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1342 DECL_ASSEMBLER_NAME (t2));
1345 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1347 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1348 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1349 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1350 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1351 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1352 DECL_FIELD_BIT_OFFSET (t2));
1353 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1356 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1358 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1359 DECL_FUNCTION_PERSONALITY (t2));
1360 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1361 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1362 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1363 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1364 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1367 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1369 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1370 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1371 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1372 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1373 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1374 reconstructed during fixup. */
1375 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1376 during fixup. */
1377 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1378 /* ??? Global types from different TUs have non-matching
1379 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1380 equal. */
1381 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1383 else
1384 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1385 /* TYPE_CANONICAL is re-computed during type merging, so do not
1386 compare it here. */
1387 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1390 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1392 if (code == ENUMERAL_TYPE)
1393 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1394 else if (code == ARRAY_TYPE)
1395 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1396 else if (RECORD_OR_UNION_TYPE_P (t1))
1398 tree f1, f2;
1399 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1400 f1 || f2;
1401 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1402 compare_tree_edges (f1, f2);
1403 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1405 else if (code == FUNCTION_TYPE
1406 || code == METHOD_TYPE)
1407 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1408 if (!POINTER_TYPE_P (t1))
1409 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1410 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1413 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1415 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1416 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1417 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1420 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1421 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1422 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1424 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1426 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1427 compare_tree_edges (TREE_OPERAND (t1, i),
1428 TREE_OPERAND (t2, i));
1430 /* BLOCKs are function local and we don't merge anything there. */
1431 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1432 return false;
1435 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1437 unsigned i;
1438 tree t;
1439 /* Lengths have already been compared above. */
1440 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1441 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1442 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1443 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1444 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1445 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1446 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1447 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1448 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1451 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1453 unsigned i;
1454 tree index, value;
1455 /* Lengths have already been compared above. */
1456 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1458 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1459 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1463 if (code == OMP_CLAUSE)
1465 int i;
1467 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1468 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1469 OMP_CLAUSE_OPERAND (t2, i));
1470 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1473 #undef compare_tree_edges
1475 return true;
1478 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1479 out MAP if they are equal. */
1481 static bool
1482 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1483 tree *map)
1485 /* Assume SCC entry hashes are sorted after their cardinality. Which
1486 means we can simply take the first n-tuple of equal hashes
1487 (which is recorded as entry_len) and do n SCC entry candidate
1488 comparisons. */
1489 for (unsigned i = 0; i < pscc->entry_len; ++i)
1491 tree *mapp = map;
1492 num_scc_compare_collisions++;
1493 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1495 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1496 on the scc as all trees will be freed. */
1497 return true;
1499 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1500 the SCC prevails. */
1501 for (unsigned j = 0; j < scc->len; ++j)
1502 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1505 return false;
1508 /* QSort sort function to sort a map of two pointers after the 2nd
1509 pointer. */
1511 static int
1512 cmp_tree (const void *p1_, const void *p2_)
1514 tree *p1 = (tree *)(const_cast<void *>(p1_));
1515 tree *p2 = (tree *)(const_cast<void *>(p2_));
1516 if (p1[1] == p2[1])
1517 return 0;
1518 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1521 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1522 hash value SCC_HASH with an already recorded SCC. Return true if
1523 that was successful, otherwise return false. */
1525 static bool
1526 unify_scc (struct data_in *data_in, unsigned from,
1527 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1529 bool unified_p = false;
1530 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1531 tree_scc *scc
1532 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1533 scc->next = NULL;
1534 scc->hash = scc_hash;
1535 scc->len = len;
1536 scc->entry_len = scc_entry_len;
1537 for (unsigned i = 0; i < len; ++i)
1539 tree t = streamer_tree_cache_get_tree (cache, from + i);
1540 scc->entries[i] = t;
1541 /* Do not merge SCCs with local entities inside them. Also do
1542 not merge TRANSLATION_UNIT_DECLs. */
1543 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1544 || (VAR_OR_FUNCTION_DECL_P (t)
1545 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1546 || TREE_CODE (t) == LABEL_DECL)
1548 /* Avoid doing any work for these cases and do not worry to
1549 record the SCCs for further merging. */
1550 return false;
1554 /* Look for the list of candidate SCCs to compare against. */
1555 tree_scc **slot;
1556 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1557 if (*slot)
1559 /* Try unifying against each candidate. */
1560 num_scc_compares++;
1562 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1563 outside of the scc when following tree edges. Make sure
1564 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1565 to track whether we visited the SCC member during the compare.
1566 We cannot use TREE_VISITED on the pscc members as the extended
1567 scc and pscc can overlap. */
1568 for (unsigned i = 0; i < scc->len; ++i)
1570 TREE_VISITED (scc->entries[i]) = 1;
1571 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1574 tree *map = XALLOCAVEC (tree, 2 * len);
1575 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1577 if (!compare_tree_sccs (pscc, scc, map))
1578 continue;
1580 /* Found an equal SCC. */
1581 unified_p = true;
1582 num_scc_compare_collisions--;
1583 num_sccs_merged++;
1584 total_scc_size_merged += len;
1586 #ifdef ENABLE_CHECKING
1587 for (unsigned i = 0; i < len; ++i)
1589 tree t = map[2*i+1];
1590 enum tree_code code = TREE_CODE (t);
1591 /* IDENTIFIER_NODEs should be singletons and are merged by the
1592 streamer. The others should be singletons, too, and we
1593 should not merge them in any way. */
1594 gcc_assert (code != TRANSLATION_UNIT_DECL
1595 && code != IDENTIFIER_NODE
1596 && !streamer_handle_as_builtin_p (t));
1598 #endif
1600 /* Fixup the streamer cache with the prevailing nodes according
1601 to the tree node mapping computed by compare_tree_sccs. */
1602 if (len == 1)
1603 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1604 else
1606 tree *map2 = XALLOCAVEC (tree, 2 * len);
1607 for (unsigned i = 0; i < len; ++i)
1609 map2[i*2] = (tree)(uintptr_t)(from + i);
1610 map2[i*2+1] = scc->entries[i];
1612 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1613 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1614 for (unsigned i = 0; i < len; ++i)
1615 streamer_tree_cache_replace_tree (cache, map[2*i],
1616 (uintptr_t)map2[2*i]);
1619 /* Free the tree nodes from the read SCC. */
1620 data_in->location_cache.revert_location_cache ();
1621 for (unsigned i = 0; i < len; ++i)
1623 enum tree_code code;
1624 if (TYPE_P (scc->entries[i]))
1625 num_merged_types++;
1626 code = TREE_CODE (scc->entries[i]);
1627 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1628 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1629 ggc_free (scc->entries[i]);
1632 break;
1635 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1636 if (!unified_p)
1637 for (unsigned i = 0; i < scc->len; ++i)
1638 TREE_VISITED (scc->entries[i]) = 0;
1641 /* If we didn't unify it to any candidate duplicate the relevant
1642 pieces to permanent storage and link it into the chain. */
1643 if (!unified_p)
1645 tree_scc *pscc
1646 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1647 memcpy (pscc, scc, sizeof (tree_scc));
1648 pscc->next = (*slot);
1649 *slot = pscc;
1651 return unified_p;
1655 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1656 RESOLUTIONS is the set of symbols picked by the linker (read from the
1657 resolution file when the linker plugin is being used). */
1659 static void
1660 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1661 vec<ld_plugin_symbol_resolution_t> resolutions)
1663 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1664 const int decl_offset = sizeof (struct lto_decl_header);
1665 const int main_offset = decl_offset + header->decl_state_size;
1666 const int string_offset = main_offset + header->main_size;
1667 struct data_in *data_in;
1668 unsigned int i;
1669 const uint32_t *data_ptr, *data_end;
1670 uint32_t num_decl_states;
1672 lto_input_block ib_main ((const char *) data + main_offset,
1673 header->main_size, decl_data->mode_table);
1675 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1676 header->string_size, resolutions);
1678 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1679 internal types that should not be merged. */
1681 /* Read the global declarations and types. */
1682 while (ib_main.p < ib_main.len)
1684 tree t;
1685 unsigned from = data_in->reader_cache->nodes.length ();
1686 /* Read and uniquify SCCs as in the input stream. */
1687 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1688 if (tag == LTO_tree_scc)
1690 unsigned len_;
1691 unsigned scc_entry_len;
1692 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1693 &scc_entry_len);
1694 unsigned len = data_in->reader_cache->nodes.length () - from;
1695 gcc_assert (len == len_);
1697 total_scc_size += len;
1698 num_sccs_read++;
1700 /* We have the special case of size-1 SCCs that are pre-merged
1701 by means of identifier and string sharing for example.
1702 ??? Maybe we should avoid streaming those as SCCs. */
1703 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1704 from);
1705 if (len == 1
1706 && (TREE_CODE (first) == IDENTIFIER_NODE
1707 || TREE_CODE (first) == INTEGER_CST
1708 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1709 || streamer_handle_as_builtin_p (first)))
1710 continue;
1712 /* Try to unify the SCC with already existing ones. */
1713 if (!flag_ltrans
1714 && unify_scc (data_in, from,
1715 len, scc_entry_len, scc_hash))
1716 continue;
1718 /* Tree merging failed, mark entries in location cache as
1719 permanent. */
1720 data_in->location_cache.accept_location_cache ();
1722 bool seen_type = false;
1723 for (unsigned i = 0; i < len; ++i)
1725 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1726 from + i);
1727 /* Reconstruct the type variant and pointer-to/reference-to
1728 chains. */
1729 if (TYPE_P (t))
1731 seen_type = true;
1732 num_prevailing_types++;
1733 lto_fixup_prevailing_type (t);
1735 /* Compute the canonical type of all types.
1736 ??? Should be able to assert that !TYPE_CANONICAL. */
1737 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1739 gimple_register_canonical_type (t);
1740 if (odr_type_p (t))
1741 register_odr_type (t);
1743 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1744 type which is also member of this SCC. */
1745 if (TREE_CODE (t) == INTEGER_CST
1746 && !TREE_OVERFLOW (t))
1747 cache_integer_cst (t);
1748 /* Register TYPE_DECLs with the debuginfo machinery. */
1749 if (!flag_wpa
1750 && TREE_CODE (t) == TYPE_DECL)
1752 /* Dwarf2out needs location information.
1753 TODO: Moving this out of the streamer loop may noticealy
1754 improve ltrans linemap memory use. */
1755 data_in->location_cache.apply_location_cache ();
1756 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1758 if (!flag_ltrans)
1760 /* Register variables and functions with the
1761 symbol table. */
1762 if (TREE_CODE (t) == VAR_DECL)
1763 lto_register_var_decl_in_symtab (data_in, t, from + i);
1764 else if (TREE_CODE (t) == FUNCTION_DECL
1765 && !DECL_BUILT_IN (t))
1766 lto_register_function_decl_in_symtab (data_in, t, from + i);
1767 /* Scan the tree for references to global functions or
1768 variables and record those for later fixup. */
1769 if (mentions_vars_p (t))
1770 vec_safe_push (tree_with_vars, t);
1773 if (seen_type)
1774 num_type_scc_trees += len;
1776 else
1778 /* Pickle stray references. */
1779 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1780 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1783 data_in->location_cache.apply_location_cache ();
1785 /* Read in lto_in_decl_state objects. */
1786 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1787 data_end =
1788 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1789 num_decl_states = *data_ptr++;
1791 gcc_assert (num_decl_states > 0);
1792 decl_data->global_decl_state = lto_new_in_decl_state ();
1793 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1794 decl_data->global_decl_state);
1796 /* Read in per-function decl states and enter them in hash table. */
1797 decl_data->function_decl_states =
1798 hash_table<decl_state_hasher>::create_ggc (37);
1800 for (i = 1; i < num_decl_states; i++)
1802 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1804 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1805 lto_in_decl_state **slot
1806 = decl_data->function_decl_states->find_slot (state, INSERT);
1807 gcc_assert (*slot == NULL);
1808 *slot = state;
1811 if (data_ptr != data_end)
1812 internal_error ("bytecode stream: garbage at the end of symbols section");
1814 /* Set the current decl state to be the global state. */
1815 decl_data->current_decl_state = decl_data->global_decl_state;
1817 lto_data_in_delete (data_in);
1820 /* Custom version of strtoll, which is not portable. */
1822 static int64_t
1823 lto_parse_hex (const char *p)
1825 int64_t ret = 0;
1827 for (; *p != '\0'; ++p)
1829 char c = *p;
1830 unsigned char part;
1831 ret <<= 4;
1832 if (c >= '0' && c <= '9')
1833 part = c - '0';
1834 else if (c >= 'a' && c <= 'f')
1835 part = c - 'a' + 10;
1836 else if (c >= 'A' && c <= 'F')
1837 part = c - 'A' + 10;
1838 else
1839 internal_error ("could not parse hex number");
1840 ret |= part;
1843 return ret;
1846 /* Read resolution for file named FILE_NAME. The resolution is read from
1847 RESOLUTION. */
1849 static void
1850 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1852 /* We require that objects in the resolution file are in the same
1853 order as the lto1 command line. */
1854 unsigned int name_len;
1855 char *obj_name;
1856 unsigned int num_symbols;
1857 unsigned int i;
1858 struct lto_file_decl_data *file_data;
1859 splay_tree_node nd = NULL;
1861 if (!resolution)
1862 return;
1864 name_len = strlen (file->filename);
1865 obj_name = XNEWVEC (char, name_len + 1);
1866 fscanf (resolution, " "); /* Read white space. */
1868 fread (obj_name, sizeof (char), name_len, resolution);
1869 obj_name[name_len] = '\0';
1870 if (filename_cmp (obj_name, file->filename) != 0)
1871 internal_error ("unexpected file name %s in linker resolution file. "
1872 "Expected %s", obj_name, file->filename);
1873 if (file->offset != 0)
1875 int t;
1876 char offset_p[17];
1877 int64_t offset;
1878 t = fscanf (resolution, "@0x%16s", offset_p);
1879 if (t != 1)
1880 internal_error ("could not parse file offset");
1881 offset = lto_parse_hex (offset_p);
1882 if (offset != file->offset)
1883 internal_error ("unexpected offset");
1886 free (obj_name);
1888 fscanf (resolution, "%u", &num_symbols);
1890 for (i = 0; i < num_symbols; i++)
1892 int t;
1893 unsigned index;
1894 unsigned HOST_WIDE_INT id;
1895 char r_str[27];
1896 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1897 unsigned int j;
1898 unsigned int lto_resolution_str_len =
1899 sizeof (lto_resolution_str) / sizeof (char *);
1900 res_pair rp;
1902 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1903 &index, &id, r_str);
1904 if (t != 3)
1905 internal_error ("invalid line in the resolution file");
1907 for (j = 0; j < lto_resolution_str_len; j++)
1909 if (strcmp (lto_resolution_str[j], r_str) == 0)
1911 r = (enum ld_plugin_symbol_resolution) j;
1912 break;
1915 if (j == lto_resolution_str_len)
1916 internal_error ("invalid resolution in the resolution file");
1918 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1920 nd = lto_splay_tree_lookup (file_ids, id);
1921 if (nd == NULL)
1922 internal_error ("resolution sub id %wx not in object file", id);
1925 file_data = (struct lto_file_decl_data *)nd->value;
1926 /* The indexes are very sparse. To save memory save them in a compact
1927 format that is only unpacked later when the subfile is processed. */
1928 rp.res = r;
1929 rp.index = index;
1930 file_data->respairs.safe_push (rp);
1931 if (file_data->max_index < index)
1932 file_data->max_index = index;
1936 /* List of file_decl_datas */
1937 struct file_data_list
1939 struct lto_file_decl_data *first, *last;
1942 /* Is the name for a id'ed LTO section? */
1944 static int
1945 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1947 const char *s;
1949 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
1950 return 0;
1951 s = strrchr (name, '.');
1952 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1955 /* Create file_data of each sub file id */
1957 static int
1958 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1959 struct file_data_list *list)
1961 struct lto_section_slot s_slot, *new_slot;
1962 unsigned HOST_WIDE_INT id;
1963 splay_tree_node nd;
1964 void **hash_slot;
1965 char *new_name;
1966 struct lto_file_decl_data *file_data;
1968 if (!lto_section_with_id (ls->name, &id))
1969 return 1;
1971 /* Find hash table of sub module id */
1972 nd = lto_splay_tree_lookup (file_ids, id);
1973 if (nd != NULL)
1975 file_data = (struct lto_file_decl_data *)nd->value;
1977 else
1979 file_data = ggc_alloc<lto_file_decl_data> ();
1980 memset(file_data, 0, sizeof (struct lto_file_decl_data));
1981 file_data->id = id;
1982 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1983 lto_splay_tree_insert (file_ids, id, file_data);
1985 /* Maintain list in linker order */
1986 if (!list->first)
1987 list->first = file_data;
1988 if (list->last)
1989 list->last->next = file_data;
1990 list->last = file_data;
1993 /* Copy section into sub module hash table */
1994 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
1995 s_slot.name = new_name;
1996 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
1997 gcc_assert (*hash_slot == NULL);
1999 new_slot = XDUP (struct lto_section_slot, ls);
2000 new_slot->name = new_name;
2001 *hash_slot = new_slot;
2002 return 1;
2005 /* Read declarations and other initializations for a FILE_DATA. */
2007 static void
2008 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2010 const char *data;
2011 size_t len;
2012 vec<ld_plugin_symbol_resolution_t>
2013 resolutions = vNULL;
2014 int i;
2015 res_pair *rp;
2017 /* Create vector for fast access of resolution. We do this lazily
2018 to save memory. */
2019 resolutions.safe_grow_cleared (file_data->max_index + 1);
2020 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2021 resolutions[rp->index] = rp->res;
2022 file_data->respairs.release ();
2024 file_data->renaming_hash_table = lto_create_renaming_table ();
2025 file_data->file_name = file->filename;
2026 #ifdef ACCEL_COMPILER
2027 lto_input_mode_table (file_data);
2028 #else
2029 file_data->mode_table = lto_mode_identity_table;
2030 #endif
2031 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2032 if (data == NULL)
2034 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2035 return;
2037 /* Frees resolutions */
2038 lto_read_decls (file_data, data, resolutions);
2039 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2042 /* Finalize FILE_DATA in FILE and increase COUNT. */
2044 static int
2045 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2046 int *count)
2048 lto_file_finalize (file_data, file);
2049 if (symtab->dump_file)
2050 fprintf (symtab->dump_file,
2051 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2052 file_data->file_name, file_data->id);
2053 (*count)++;
2054 return 0;
2057 /* Generate a TREE representation for all types and external decls
2058 entities in FILE.
2060 Read all of the globals out of the file. Then read the cgraph
2061 and process the .o index into the cgraph nodes so that it can open
2062 the .o file to load the functions and ipa information. */
2064 static struct lto_file_decl_data *
2065 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2067 struct lto_file_decl_data *file_data = NULL;
2068 splay_tree file_ids;
2069 htab_t section_hash_table;
2070 struct lto_section_slot *section;
2071 struct file_data_list file_list;
2072 struct lto_section_list section_list;
2074 memset (&section_list, 0, sizeof (struct lto_section_list));
2075 section_hash_table = lto_obj_build_section_table (file, &section_list);
2077 /* Find all sub modules in the object and put their sections into new hash
2078 tables in a splay tree. */
2079 file_ids = lto_splay_tree_new ();
2080 memset (&file_list, 0, sizeof (struct file_data_list));
2081 for (section = section_list.first; section != NULL; section = section->next)
2082 create_subid_section_table (section, file_ids, &file_list);
2084 /* Add resolutions to file ids */
2085 lto_resolution_read (file_ids, resolution_file, file);
2087 /* Finalize each lto file for each submodule in the merged object */
2088 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2089 lto_create_files_from_ids (file, file_data, count);
2091 splay_tree_delete (file_ids);
2092 htab_delete (section_hash_table);
2094 return file_list.first;
2097 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2098 #define LTO_MMAP_IO 1
2099 #endif
2101 #if LTO_MMAP_IO
2102 /* Page size of machine is used for mmap and munmap calls. */
2103 static size_t page_mask;
2104 #endif
2106 /* Get the section data of length LEN from FILENAME starting at
2107 OFFSET. The data segment must be freed by the caller when the
2108 caller is finished. Returns NULL if all was not well. */
2110 static char *
2111 lto_read_section_data (struct lto_file_decl_data *file_data,
2112 intptr_t offset, size_t len)
2114 char *result;
2115 static int fd = -1;
2116 static char *fd_name;
2117 #if LTO_MMAP_IO
2118 intptr_t computed_len;
2119 intptr_t computed_offset;
2120 intptr_t diff;
2121 #endif
2123 /* Keep a single-entry file-descriptor cache. The last file we
2124 touched will get closed at exit.
2125 ??? Eventually we want to add a more sophisticated larger cache
2126 or rather fix function body streaming to not stream them in
2127 practically random order. */
2128 if (fd != -1
2129 && filename_cmp (fd_name, file_data->file_name) != 0)
2131 free (fd_name);
2132 close (fd);
2133 fd = -1;
2135 if (fd == -1)
2137 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2138 if (fd == -1)
2140 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2141 return NULL;
2143 fd_name = xstrdup (file_data->file_name);
2146 #if LTO_MMAP_IO
2147 if (!page_mask)
2149 size_t page_size = sysconf (_SC_PAGE_SIZE);
2150 page_mask = ~(page_size - 1);
2153 computed_offset = offset & page_mask;
2154 diff = offset - computed_offset;
2155 computed_len = len + diff;
2157 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2158 fd, computed_offset);
2159 if (result == MAP_FAILED)
2161 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2162 return NULL;
2165 return result + diff;
2166 #else
2167 result = (char *) xmalloc (len);
2168 if (lseek (fd, offset, SEEK_SET) != offset
2169 || read (fd, result, len) != (ssize_t) len)
2171 free (result);
2172 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2173 result = NULL;
2175 #ifdef __MINGW32__
2176 /* Native windows doesn't supports delayed unlink on opened file. So
2177 we close file here again. This produces higher I/O load, but at least
2178 it prevents to have dangling file handles preventing unlink. */
2179 free (fd_name);
2180 fd_name = NULL;
2181 close (fd);
2182 fd = -1;
2183 #endif
2184 return result;
2185 #endif
2189 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2190 NAME will be NULL unless the section type is for a function
2191 body. */
2193 static const char *
2194 get_section_data (struct lto_file_decl_data *file_data,
2195 enum lto_section_type section_type,
2196 const char *name,
2197 size_t *len)
2199 htab_t section_hash_table = file_data->section_hash_table;
2200 struct lto_section_slot *f_slot;
2201 struct lto_section_slot s_slot;
2202 const char *section_name = lto_get_section_name (section_type, name, file_data);
2203 char *data = NULL;
2205 *len = 0;
2206 s_slot.name = section_name;
2207 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2208 if (f_slot)
2210 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2211 *len = f_slot->len;
2214 free (CONST_CAST (char *, section_name));
2215 return data;
2219 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2220 starts at OFFSET and has LEN bytes. */
2222 static void
2223 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2224 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2225 const char *name ATTRIBUTE_UNUSED,
2226 const char *offset, size_t len ATTRIBUTE_UNUSED)
2228 #if LTO_MMAP_IO
2229 intptr_t computed_len;
2230 intptr_t computed_offset;
2231 intptr_t diff;
2232 #endif
2234 #if LTO_MMAP_IO
2235 computed_offset = ((intptr_t) offset) & page_mask;
2236 diff = (intptr_t) offset - computed_offset;
2237 computed_len = len + diff;
2239 munmap ((caddr_t) computed_offset, computed_len);
2240 #else
2241 free (CONST_CAST(char *, offset));
2242 #endif
2245 static lto_file *current_lto_file;
2247 /* Helper for qsort; compare partitions and return one with smaller size.
2248 We sort from greatest to smallest so parallel build doesn't stale on the
2249 longest compilation being executed too late. */
2251 static int
2252 cmp_partitions_size (const void *a, const void *b)
2254 const struct ltrans_partition_def *pa
2255 = *(struct ltrans_partition_def *const *)a;
2256 const struct ltrans_partition_def *pb
2257 = *(struct ltrans_partition_def *const *)b;
2258 return pb->insns - pa->insns;
2261 /* Helper for qsort; compare partitions and return one with smaller order. */
2263 static int
2264 cmp_partitions_order (const void *a, const void *b)
2266 const struct ltrans_partition_def *pa
2267 = *(struct ltrans_partition_def *const *)a;
2268 const struct ltrans_partition_def *pb
2269 = *(struct ltrans_partition_def *const *)b;
2270 int ordera = -1, orderb = -1;
2272 if (lto_symtab_encoder_size (pa->encoder))
2273 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2274 if (lto_symtab_encoder_size (pb->encoder))
2275 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2276 return orderb - ordera;
2279 /* Actually stream out ENCODER into TEMP_FILENAME. */
2281 static void
2282 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2284 lto_file *file = lto_obj_file_open (temp_filename, true);
2285 if (!file)
2286 fatal_error (input_location, "lto_obj_file_open() failed");
2287 lto_set_current_out_file (file);
2289 ipa_write_optimization_summaries (encoder);
2291 lto_set_current_out_file (NULL);
2292 lto_obj_file_close (file);
2293 free (file);
2296 /* Wait for forked process and signal errors. */
2297 #ifdef HAVE_WORKING_FORK
2298 static void
2299 wait_for_child ()
2301 int status;
2304 #ifndef WCONTINUED
2305 #define WCONTINUED 0
2306 #endif
2307 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2308 if (w == -1)
2309 fatal_error (input_location, "waitpid failed");
2311 if (WIFEXITED (status) && WEXITSTATUS (status))
2312 fatal_error (input_location, "streaming subprocess failed");
2313 else if (WIFSIGNALED (status))
2314 fatal_error (input_location,
2315 "streaming subprocess was killed by signal");
2317 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2319 #endif
2321 /* Stream out ENCODER into TEMP_FILENAME
2322 Fork if that seems to help. */
2324 static void
2325 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2326 bool ARG_UNUSED (last))
2328 #ifdef HAVE_WORKING_FORK
2329 static int nruns;
2331 if (lto_parallelism <= 1)
2333 do_stream_out (temp_filename, encoder);
2334 return;
2337 /* Do not run more than LTO_PARALLELISM streamings
2338 FIXME: we ignore limits on jobserver. */
2339 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2341 wait_for_child ();
2342 nruns --;
2344 /* If this is not the last parallel partition, execute new
2345 streaming process. */
2346 if (!last)
2348 pid_t cpid = fork ();
2350 if (!cpid)
2352 setproctitle ("lto1-wpa-streaming");
2353 do_stream_out (temp_filename, encoder);
2354 exit (0);
2356 /* Fork failed; lets do the job ourseleves. */
2357 else if (cpid == -1)
2358 do_stream_out (temp_filename, encoder);
2359 else
2360 nruns++;
2362 /* Last partition; stream it and wait for all children to die. */
2363 else
2365 int i;
2366 do_stream_out (temp_filename, encoder);
2367 for (i = 0; i < nruns; i++)
2368 wait_for_child ();
2370 asm_nodes_output = true;
2371 #else
2372 do_stream_out (temp_filename, encoder);
2373 #endif
2376 /* Write all output files in WPA mode and the file with the list of
2377 LTRANS units. */
2379 static void
2380 lto_wpa_write_files (void)
2382 unsigned i, n_sets;
2383 ltrans_partition part;
2384 FILE *ltrans_output_list_stream;
2385 char *temp_filename;
2386 vec <char *>temp_filenames = vNULL;
2387 size_t blen;
2389 /* Open the LTRANS output list. */
2390 if (!ltrans_output_list)
2391 fatal_error (input_location, "no LTRANS output list filename provided");
2393 timevar_push (TV_WHOPR_WPA);
2395 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2396 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2398 timevar_pop (TV_WHOPR_WPA);
2400 timevar_push (TV_WHOPR_WPA_IO);
2402 /* Generate a prefix for the LTRANS unit files. */
2403 blen = strlen (ltrans_output_list);
2404 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2405 strcpy (temp_filename, ltrans_output_list);
2406 if (blen > sizeof (".out")
2407 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2408 ".out") == 0)
2409 temp_filename[blen - sizeof (".out") + 1] = '\0';
2410 blen = strlen (temp_filename);
2412 n_sets = ltrans_partitions.length ();
2414 /* Sort partitions by size so small ones are compiled last.
2415 FIXME: Even when not reordering we may want to output one list for parallel make
2416 and other for final link command. */
2418 if (!flag_profile_reorder_functions || !flag_profile_use)
2419 ltrans_partitions.qsort (flag_toplevel_reorder
2420 ? cmp_partitions_size
2421 : cmp_partitions_order);
2423 for (i = 0; i < n_sets; i++)
2425 ltrans_partition part = ltrans_partitions[i];
2427 /* Write all the nodes in SET. */
2428 sprintf (temp_filename + blen, "%u.o", i);
2430 if (!quiet_flag)
2431 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2432 if (symtab->dump_file)
2434 lto_symtab_encoder_iterator lsei;
2436 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2437 part->name, temp_filename, part->insns);
2438 fprintf (symtab->dump_file, " Symbols in partition: ");
2439 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2440 lsei_next_in_partition (&lsei))
2442 symtab_node *node = lsei_node (lsei);
2443 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2445 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2446 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2447 lsei_next (&lsei))
2449 symtab_node *node = lsei_node (lsei);
2450 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2452 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2453 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2454 if (cnode
2455 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2456 fprintf (symtab->dump_file, "(body included)");
2457 else
2459 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2460 if (vnode
2461 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2462 fprintf (symtab->dump_file, "(initializer included)");
2466 fprintf (symtab->dump_file, "\n");
2468 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2470 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2472 part->encoder = NULL;
2474 temp_filenames.safe_push (xstrdup (temp_filename));
2476 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2477 if (ltrans_output_list_stream == NULL)
2478 fatal_error (input_location,
2479 "opening LTRANS output list %s: %m", ltrans_output_list);
2480 for (i = 0; i < n_sets; i++)
2482 unsigned int len = strlen (temp_filenames[i]);
2483 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2484 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2485 fatal_error (input_location, "writing to LTRANS output list %s: %m",
2486 ltrans_output_list);
2487 free (temp_filenames[i]);
2489 temp_filenames.release();
2491 lto_stats.num_output_files += n_sets;
2493 /* Close the LTRANS output list. */
2494 if (fclose (ltrans_output_list_stream))
2495 fatal_error (input_location,
2496 "closing LTRANS output list %s: %m", ltrans_output_list);
2498 free_ltrans_partitions();
2499 free (temp_filename);
2501 timevar_pop (TV_WHOPR_WPA_IO);
2505 /* If TT is a variable or function decl replace it with its
2506 prevailing variant. */
2507 #define LTO_SET_PREVAIL(tt) \
2508 do {\
2509 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2510 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2512 tt = lto_symtab_prevailing_decl (tt); \
2513 fixed = true; \
2515 } while (0)
2517 /* Ensure that TT isn't a replacable var of function decl. */
2518 #define LTO_NO_PREVAIL(tt) \
2519 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2521 /* Given a tree T replace all fields referring to variables or functions
2522 with their prevailing variant. */
2523 static void
2524 lto_fixup_prevailing_decls (tree t)
2526 enum tree_code code = TREE_CODE (t);
2527 bool fixed = false;
2529 gcc_checking_assert (code != TREE_BINFO);
2530 LTO_NO_PREVAIL (TREE_TYPE (t));
2531 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2532 LTO_NO_PREVAIL (TREE_CHAIN (t));
2533 if (DECL_P (t))
2535 LTO_NO_PREVAIL (DECL_NAME (t));
2536 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2537 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2539 LTO_SET_PREVAIL (DECL_SIZE (t));
2540 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2541 LTO_SET_PREVAIL (DECL_INITIAL (t));
2542 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2543 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2545 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2547 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2549 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2551 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2553 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2555 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2556 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2557 LTO_NO_PREVAIL (DECL_VINDEX (t));
2559 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2561 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2562 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2563 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2564 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2565 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2568 else if (TYPE_P (t))
2570 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2571 LTO_SET_PREVAIL (TYPE_SIZE (t));
2572 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2573 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2574 LTO_NO_PREVAIL (TYPE_NAME (t));
2576 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2577 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2578 LTO_NO_PREVAIL (t->type_non_common.binfo);
2580 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2582 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2583 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2584 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2586 else if (EXPR_P (t))
2588 int i;
2589 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2590 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2592 else if (TREE_CODE (t) == CONSTRUCTOR)
2594 unsigned i;
2595 tree val;
2596 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2597 LTO_SET_PREVAIL (val);
2599 else
2601 switch (code)
2603 case TREE_LIST:
2604 LTO_SET_PREVAIL (TREE_VALUE (t));
2605 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2606 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2607 break;
2608 default:
2609 gcc_unreachable ();
2612 /* If we fixed nothing, then we missed something seen by
2613 mentions_vars_p. */
2614 gcc_checking_assert (fixed);
2616 #undef LTO_SET_PREVAIL
2617 #undef LTO_NO_PREVAIL
2619 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2620 replaces var and function decls with the corresponding prevailing def. */
2622 static void
2623 lto_fixup_state (struct lto_in_decl_state *state)
2625 unsigned i, si;
2627 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2628 we still need to walk from all DECLs to find the reachable
2629 FUNCTION_DECLs and VAR_DECLs. */
2630 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2632 vec<tree, va_gc> *trees = state->streams[si];
2633 for (i = 0; i < vec_safe_length (trees); i++)
2635 tree t = (*trees)[i];
2636 #ifdef ENABLE_CHECKING
2637 if (TYPE_P (t))
2638 verify_type (t);
2639 #endif
2640 if (VAR_OR_FUNCTION_DECL_P (t)
2641 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2642 (*trees)[i] = lto_symtab_prevailing_decl (t);
2647 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2648 prevailing one. */
2650 static void
2651 lto_fixup_decls (struct lto_file_decl_data **files)
2653 unsigned int i;
2654 tree t;
2656 if (tree_with_vars)
2657 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2658 lto_fixup_prevailing_decls (t);
2660 for (i = 0; files[i]; i++)
2662 struct lto_file_decl_data *file = files[i];
2663 struct lto_in_decl_state *state = file->global_decl_state;
2664 lto_fixup_state (state);
2666 hash_table<decl_state_hasher>::iterator iter;
2667 lto_in_decl_state *elt;
2668 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2669 lto_in_decl_state *, iter)
2670 lto_fixup_state (elt);
2674 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2676 /* Turn file datas for sub files into a single array, so that they look
2677 like separate files for further passes. */
2679 static void
2680 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2682 struct lto_file_decl_data *n, *next;
2683 int i, k;
2685 lto_stats.num_input_files = count;
2686 all_file_decl_data
2687 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2688 /* Set the hooks so that all of the ipa passes can read in their data. */
2689 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2690 for (i = 0, k = 0; i < last_file_ix; i++)
2692 for (n = orig[i]; n != NULL; n = next)
2694 all_file_decl_data[k++] = n;
2695 next = n->next;
2696 n->next = NULL;
2699 all_file_decl_data[k] = NULL;
2700 gcc_assert (k == count);
2703 /* Input file data before flattening (i.e. splitting them to subfiles to support
2704 incremental linking. */
2705 static int real_file_count;
2706 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2708 static void print_lto_report_1 (void);
2710 /* Read all the symbols from the input files FNAMES. NFILES is the
2711 number of files requested in the command line. Instantiate a
2712 global call graph by aggregating all the sub-graphs found in each
2713 file. */
2715 static void
2716 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2718 unsigned int i, last_file_ix;
2719 FILE *resolution;
2720 int count = 0;
2721 struct lto_file_decl_data **decl_data;
2722 symtab_node *snode;
2724 symtab->initialize ();
2726 timevar_push (TV_IPA_LTO_DECL_IN);
2728 #ifdef ACCEL_COMPILER
2729 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2730 lto_stream_offload_p = true;
2731 #endif
2733 real_file_decl_data
2734 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2735 real_file_count = nfiles;
2737 /* Read the resolution file. */
2738 resolution = NULL;
2739 if (resolution_file_name)
2741 int t;
2742 unsigned num_objects;
2744 resolution = fopen (resolution_file_name, "r");
2745 if (resolution == NULL)
2746 fatal_error (input_location,
2747 "could not open symbol resolution file: %m");
2749 t = fscanf (resolution, "%u", &num_objects);
2750 gcc_assert (t == 1);
2752 /* True, since the plugin splits the archives. */
2753 gcc_assert (num_objects == nfiles);
2755 symtab->state = LTO_STREAMING;
2757 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2758 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2759 gimple_canonical_type_eq, NULL);
2760 gcc_obstack_init (&tree_scc_hash_obstack);
2761 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2763 /* Register the common node types with the canonical type machinery so
2764 we properly share alias-sets across languages and TUs. Do not
2765 expose the common nodes as type merge target - those that should be
2766 are already exposed so by pre-loading the LTO streamer caches.
2767 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2768 for (i = 0; i < itk_none; ++i)
2769 lto_register_canonical_types (integer_types[i], true);
2770 for (i = 0; i < stk_type_kind_last; ++i)
2771 lto_register_canonical_types (sizetype_tab[i], true);
2772 for (i = 0; i < TI_MAX; ++i)
2773 lto_register_canonical_types (global_trees[i], true);
2774 for (i = 0; i < itk_none; ++i)
2775 lto_register_canonical_types (integer_types[i], false);
2776 for (i = 0; i < stk_type_kind_last; ++i)
2777 lto_register_canonical_types (sizetype_tab[i], false);
2778 for (i = 0; i < TI_MAX; ++i)
2779 lto_register_canonical_types (global_trees[i], false);
2781 if (!quiet_flag)
2782 fprintf (stderr, "Reading object files:");
2784 /* Read all of the object files specified on the command line. */
2785 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2787 struct lto_file_decl_data *file_data = NULL;
2788 if (!quiet_flag)
2790 fprintf (stderr, " %s", fnames[i]);
2791 fflush (stderr);
2794 current_lto_file = lto_obj_file_open (fnames[i], false);
2795 if (!current_lto_file)
2796 break;
2798 file_data = lto_file_read (current_lto_file, resolution, &count);
2799 if (!file_data)
2801 lto_obj_file_close (current_lto_file);
2802 free (current_lto_file);
2803 current_lto_file = NULL;
2804 break;
2807 decl_data[last_file_ix++] = file_data;
2809 lto_obj_file_close (current_lto_file);
2810 free (current_lto_file);
2811 current_lto_file = NULL;
2814 lto_flatten_files (decl_data, count, last_file_ix);
2815 lto_stats.num_input_files = count;
2816 ggc_free(decl_data);
2817 real_file_decl_data = NULL;
2819 if (resolution_file_name)
2820 fclose (resolution);
2822 /* Show the LTO report before launching LTRANS. */
2823 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2824 print_lto_report_1 ();
2826 /* Free gimple type merging datastructures. */
2827 delete tree_scc_hash;
2828 tree_scc_hash = NULL;
2829 obstack_free (&tree_scc_hash_obstack, NULL);
2830 htab_delete (gimple_canonical_types);
2831 gimple_canonical_types = NULL;
2832 delete canonical_type_hash_cache;
2833 canonical_type_hash_cache = NULL;
2835 /* At this stage we know that majority of GGC memory is reachable.
2836 Growing the limits prevents unnecesary invocation of GGC. */
2837 ggc_grow ();
2838 ggc_collect ();
2840 /* Set the hooks so that all of the ipa passes can read in their data. */
2841 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2843 timevar_pop (TV_IPA_LTO_DECL_IN);
2845 if (!quiet_flag)
2846 fprintf (stderr, "\nReading the callgraph\n");
2848 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2849 /* Read the symtab. */
2850 input_symtab ();
2852 input_offload_tables ();
2854 /* Store resolutions into the symbol table. */
2856 ld_plugin_symbol_resolution_t *res;
2857 FOR_EACH_SYMBOL (snode)
2858 if (snode->real_symbol_p ()
2859 && snode->lto_file_data
2860 && snode->lto_file_data->resolution_map
2861 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
2862 snode->resolution = *res;
2863 for (i = 0; all_file_decl_data[i]; i++)
2864 if (all_file_decl_data[i]->resolution_map)
2866 delete all_file_decl_data[i]->resolution_map;
2867 all_file_decl_data[i]->resolution_map = NULL;
2870 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2872 if (!quiet_flag)
2873 fprintf (stderr, "Merging declarations\n");
2875 timevar_push (TV_IPA_LTO_DECL_MERGE);
2876 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2877 need to care about resolving symbols again, we only need to replace
2878 duplicated declarations read from the callgraph and from function
2879 sections. */
2880 if (!flag_ltrans)
2882 lto_symtab_merge_decls ();
2884 /* If there were errors during symbol merging bail out, we have no
2885 good way to recover here. */
2886 if (seen_error ())
2887 fatal_error (input_location,
2888 "errors during merging of translation units");
2890 /* Fixup all decls. */
2891 lto_fixup_decls (all_file_decl_data);
2893 if (tree_with_vars)
2894 ggc_free (tree_with_vars);
2895 tree_with_vars = NULL;
2896 ggc_collect ();
2898 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2899 /* Each pass will set the appropriate timer. */
2901 if (!quiet_flag)
2902 fprintf (stderr, "Reading summaries\n");
2904 /* Read the IPA summary data. */
2905 if (flag_ltrans)
2906 ipa_read_optimization_summaries ();
2907 else
2908 ipa_read_summaries ();
2910 for (i = 0; all_file_decl_data[i]; i++)
2912 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2913 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2914 all_file_decl_data[i]->symtab_node_encoder = NULL;
2915 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
2916 all_file_decl_data[i]->global_decl_state = NULL;
2917 all_file_decl_data[i]->current_decl_state = NULL;
2920 /* Finally merge the cgraph according to the decl merging decisions. */
2921 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2922 if (symtab->dump_file)
2924 fprintf (symtab->dump_file, "Before merging:\n");
2925 symtab_node::dump_table (symtab->dump_file);
2927 if (!flag_ltrans)
2929 lto_symtab_merge_symbols ();
2930 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2931 we are still having duplicated comdat groups containing local statics.
2932 We could also just remove them while merging. */
2933 symtab->remove_unreachable_nodes (dump_file);
2935 ggc_collect ();
2936 symtab->state = IPA_SSA;
2937 /* FIXME: Technically all node removals happening here are useless, because
2938 WPA should not stream them. */
2939 if (flag_ltrans)
2940 symtab->remove_unreachable_nodes (dump_file);
2942 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2944 /* Indicate that the cgraph is built and ready. */
2945 symtab->function_flags_ready = true;
2947 ggc_free (all_file_decl_data);
2948 all_file_decl_data = NULL;
2952 /* Materialize all the bodies for all the nodes in the callgraph. */
2954 static void
2955 materialize_cgraph (void)
2957 struct cgraph_node *node;
2958 timevar_id_t lto_timer;
2960 if (!quiet_flag)
2961 fprintf (stderr,
2962 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2965 FOR_EACH_FUNCTION (node)
2967 if (node->lto_file_data)
2969 lto_materialize_function (node);
2970 lto_stats.num_input_cgraph_nodes++;
2975 /* Start the appropriate timer depending on the mode that we are
2976 operating in. */
2977 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2978 : (flag_ltrans) ? TV_WHOPR_LTRANS
2979 : TV_LTO;
2980 timevar_push (lto_timer);
2982 current_function_decl = NULL;
2983 set_cfun (NULL);
2985 if (!quiet_flag)
2986 fprintf (stderr, "\n");
2988 timevar_pop (lto_timer);
2992 /* Show various memory usage statistics related to LTO. */
2993 static void
2994 print_lto_report_1 (void)
2996 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
2997 fprintf (stderr, "%s statistics\n", pfx);
2999 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3000 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3001 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3002 if (flag_wpa && tree_scc_hash)
3004 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3005 "collision ratio: %f\n", pfx,
3006 (long) tree_scc_hash->size (),
3007 (long) tree_scc_hash->elements (),
3008 tree_scc_hash->collisions ());
3009 hash_table<tree_scc_hasher>::iterator hiter;
3010 tree_scc *scc, *max_scc = NULL;
3011 unsigned max_length = 0;
3012 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3014 unsigned length = 0;
3015 tree_scc *s = scc;
3016 for (; s; s = s->next)
3017 length++;
3018 if (length > max_length)
3020 max_length = length;
3021 max_scc = scc;
3024 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3025 pfx, max_length, max_scc->len);
3026 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3027 num_scc_compares, num_scc_compare_collisions,
3028 num_scc_compare_collisions / (double) num_scc_compares);
3029 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3030 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3031 total_scc_size_merged);
3032 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3033 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3034 pfx, num_prevailing_types, num_type_scc_trees);
3035 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3036 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3037 (long) htab_size (gimple_canonical_types),
3038 (long) htab_elements (gimple_canonical_types),
3039 (long) gimple_canonical_types->searches,
3040 (long) gimple_canonical_types->collisions,
3041 htab_collisions (gimple_canonical_types));
3042 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3043 "%lu elements, %ld searches\n", pfx,
3044 num_canonical_type_hash_entries,
3045 num_canonical_type_hash_queries);
3048 print_lto_report (pfx);
3051 /* Perform whole program analysis (WPA) on the callgraph and write out the
3052 optimization plan. */
3054 static void
3055 do_whole_program_analysis (void)
3057 symtab_node *node;
3059 lto_parallelism = 1;
3061 /* TODO: jobserver communicatoin is not supported, yet. */
3062 if (!strcmp (flag_wpa, "jobserver"))
3063 lto_parallelism = -1;
3064 else
3066 lto_parallelism = atoi (flag_wpa);
3067 if (lto_parallelism <= 0)
3068 lto_parallelism = 0;
3071 timevar_start (TV_PHASE_OPT_GEN);
3073 /* Note that since we are in WPA mode, materialize_cgraph will not
3074 actually read in all the function bodies. It only materializes
3075 the decls and cgraph nodes so that analysis can be performed. */
3076 materialize_cgraph ();
3078 /* Reading in the cgraph uses different timers, start timing WPA now. */
3079 timevar_push (TV_WHOPR_WPA);
3081 if (pre_ipa_mem_report)
3083 fprintf (stderr, "Memory consumption before IPA\n");
3084 dump_memory_report (false);
3087 symtab->function_flags_ready = true;
3089 if (symtab->dump_file)
3090 symtab_node::dump_table (symtab->dump_file);
3091 bitmap_obstack_initialize (NULL);
3092 symtab->state = IPA_SSA;
3094 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3096 if (symtab->dump_file)
3098 fprintf (symtab->dump_file, "Optimized ");
3099 symtab_node::dump_table (symtab->dump_file);
3101 #ifdef ENABLE_CHECKING
3102 symtab_node::verify_symtab_nodes ();
3103 #endif
3104 bitmap_obstack_release (NULL);
3106 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3107 timevar_pop (TV_WHOPR_WPA);
3109 timevar_push (TV_WHOPR_PARTITIONING);
3110 if (flag_lto_partition == LTO_PARTITION_1TO1)
3111 lto_1_to_1_map ();
3112 else if (flag_lto_partition == LTO_PARTITION_MAX)
3113 lto_max_map ();
3114 else if (flag_lto_partition == LTO_PARTITION_ONE)
3115 lto_balanced_map (1);
3116 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3117 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3118 else
3119 gcc_unreachable ();
3121 /* Inline summaries are needed for balanced partitioning. Free them now so
3122 the memory can be used for streamer caches. */
3123 inline_free_summary ();
3125 /* AUX pointers are used by partitioning code to bookkeep number of
3126 partitions symbol is in. This is no longer needed. */
3127 FOR_EACH_SYMBOL (node)
3128 node->aux = NULL;
3130 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3132 /* Find out statics that need to be promoted
3133 to globals with hidden visibility because they are accessed from multiple
3134 partitions. */
3135 lto_promote_cross_file_statics ();
3136 timevar_pop (TV_WHOPR_PARTITIONING);
3138 timevar_stop (TV_PHASE_OPT_GEN);
3140 /* Collect a last time - in lto_wpa_write_files we may end up forking
3141 with the idea that this doesn't increase memory usage. So we
3142 absoultely do not want to collect after that. */
3143 ggc_collect ();
3145 timevar_start (TV_PHASE_STREAM_OUT);
3146 if (!quiet_flag)
3148 fprintf (stderr, "\nStreaming out");
3149 fflush (stderr);
3151 lto_wpa_write_files ();
3152 if (!quiet_flag)
3153 fprintf (stderr, "\n");
3154 timevar_stop (TV_PHASE_STREAM_OUT);
3156 if (post_ipa_mem_report)
3158 fprintf (stderr, "Memory consumption after IPA\n");
3159 dump_memory_report (false);
3162 /* Show the LTO report before launching LTRANS. */
3163 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3164 print_lto_report_1 ();
3165 if (mem_report_wpa)
3166 dump_memory_report (true);
3170 static GTY(()) tree lto_eh_personality_decl;
3172 /* Return the LTO personality function decl. */
3174 tree
3175 lto_eh_personality (void)
3177 if (!lto_eh_personality_decl)
3179 /* Use the first personality DECL for our personality if we don't
3180 support multiple ones. This ensures that we don't artificially
3181 create the need for them in a single-language program. */
3182 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3183 lto_eh_personality_decl = first_personality_decl;
3184 else
3185 lto_eh_personality_decl = lhd_gcc_personality ();
3188 return lto_eh_personality_decl;
3191 /* Set the process name based on the LTO mode. */
3193 static void
3194 lto_process_name (void)
3196 if (flag_lto)
3197 setproctitle ("lto1-lto");
3198 if (flag_wpa)
3199 setproctitle ("lto1-wpa");
3200 if (flag_ltrans)
3201 setproctitle ("lto1-ltrans");
3205 /* Initialize the LTO front end. */
3207 static void
3208 lto_init (void)
3210 lto_process_name ();
3211 lto_streamer_hooks_init ();
3212 lto_reader_init ();
3213 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3214 memset (&lto_stats, 0, sizeof (lto_stats));
3215 bitmap_obstack_initialize (NULL);
3216 gimple_register_cfg_hooks ();
3217 #ifndef ACCEL_COMPILER
3218 unsigned char *table
3219 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3220 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3221 table[m] = m;
3222 lto_mode_identity_table = table;
3223 #endif
3227 /* Main entry point for the GIMPLE front end. This front end has
3228 three main personalities:
3230 - LTO (-flto). All the object files on the command line are
3231 loaded in memory and processed as a single translation unit.
3232 This is the traditional link-time optimization behavior.
3234 - WPA (-fwpa). Only the callgraph and summary information for
3235 files in the command file are loaded. A single callgraph
3236 (without function bodies) is instantiated for the whole set of
3237 files. IPA passes are only allowed to analyze the call graph
3238 and make transformation decisions. The callgraph is
3239 partitioned, each partition is written to a new object file
3240 together with the transformation decisions.
3242 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3243 summary files from running again. Since WPA computed summary
3244 information and decided what transformations to apply, LTRANS
3245 simply applies them. */
3247 void
3248 lto_main (void)
3250 /* LTO is called as a front end, even though it is not a front end.
3251 Because it is called as a front end, TV_PHASE_PARSING and
3252 TV_PARSE_GLOBAL are active, and we need to turn them off while
3253 doing LTO. Later we turn them back on so they are active up in
3254 toplev.c. */
3255 timevar_pop (TV_PARSE_GLOBAL);
3256 timevar_stop (TV_PHASE_PARSING);
3258 timevar_start (TV_PHASE_SETUP);
3260 /* Initialize the LTO front end. */
3261 lto_init ();
3263 timevar_stop (TV_PHASE_SETUP);
3264 timevar_start (TV_PHASE_STREAM_IN);
3266 /* Read all the symbols and call graph from all the files in the
3267 command line. */
3268 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3270 timevar_stop (TV_PHASE_STREAM_IN);
3272 if (!seen_error ())
3274 /* If WPA is enabled analyze the whole call graph and create an
3275 optimization plan. Otherwise, read in all the function
3276 bodies and continue with optimization. */
3277 if (flag_wpa)
3278 do_whole_program_analysis ();
3279 else
3281 timevar_start (TV_PHASE_OPT_GEN);
3283 materialize_cgraph ();
3284 if (!flag_ltrans)
3285 lto_promote_statics_nonwpa ();
3287 /* Let the middle end know that we have read and merged all of
3288 the input files. */
3289 symtab->compile ();
3291 timevar_stop (TV_PHASE_OPT_GEN);
3293 /* FIXME lto, if the processes spawned by WPA fail, we miss
3294 the chance to print WPA's report, so WPA will call
3295 print_lto_report before launching LTRANS. If LTRANS was
3296 launched directly by the driver we would not need to do
3297 this. */
3298 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3299 print_lto_report_1 ();
3303 /* Here we make LTO pretend to be a parser. */
3304 timevar_start (TV_PHASE_PARSING);
3305 timevar_push (TV_PARSE_GLOBAL);
3308 #include "gt-lto-lto.h"