[Ada] Further cleanup in inlining machinery
[official-gcc.git] / gcc / lto / lto-common.c
blob9a17933d094b212cd28242cb8e5289d8f8c6217f
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2019 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 "tm.h"
25 #include "function.h"
26 #include "bitmap.h"
27 #include "basic-block.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "alloc-pool.h"
32 #include "tree-pass.h"
33 #include "tree-streamer.h"
34 #include "cgraph.h"
35 #include "opts.h"
36 #include "toplev.h"
37 #include "stor-layout.h"
38 #include "symbol-summary.h"
39 #include "tree-vrp.h"
40 #include "ipa-prop.h"
41 #include "common.h"
42 #include "debug.h"
43 #include "lto.h"
44 #include "lto-section-names.h"
45 #include "splay-tree.h"
46 #include "lto-partition.h"
47 #include "context.h"
48 #include "pass_manager.h"
49 #include "ipa-fnsummary.h"
50 #include "params.h"
51 #include "ipa-utils.h"
52 #include "gomp-constants.h"
53 #include "lto-symtab.h"
54 #include "stringpool.h"
55 #include "fold-const.h"
56 #include "attribs.h"
57 #include "builtins.h"
58 #include "lto-common.h"
59 #include "tree-pretty-print.h"
61 /* True when no new types are going to be streamd from the global stream. */
63 static bool type_streaming_finished = false;
65 GTY(()) tree first_personality_decl;
67 GTY(()) const unsigned char *lto_mode_identity_table;
69 /* Returns a hash code for P. */
71 static hashval_t
72 hash_name (const void *p)
74 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
75 return (hashval_t) htab_hash_string (ds->name);
79 /* Returns nonzero if P1 and P2 are equal. */
81 static int
82 eq_name (const void *p1, const void *p2)
84 const struct lto_section_slot *s1
85 = (const struct lto_section_slot *) p1;
86 const struct lto_section_slot *s2
87 = (const struct lto_section_slot *) p2;
89 return strcmp (s1->name, s2->name) == 0;
92 /* Free lto_section_slot. */
94 static void
95 free_with_string (void *arg)
97 struct lto_section_slot *s = (struct lto_section_slot *)arg;
99 free (CONST_CAST (char *, s->name));
100 free (arg);
103 /* Create section hash table. */
105 htab_t
106 lto_obj_create_section_hash_table (void)
108 return htab_create (37, hash_name, eq_name, free_with_string);
111 /* Delete an allocated integer KEY in the splay tree. */
113 static void
114 lto_splay_tree_delete_id (splay_tree_key key)
116 free ((void *) key);
119 /* Compare splay tree node ids A and B. */
121 static int
122 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
124 unsigned HOST_WIDE_INT ai;
125 unsigned HOST_WIDE_INT bi;
127 ai = *(unsigned HOST_WIDE_INT *) a;
128 bi = *(unsigned HOST_WIDE_INT *) b;
130 if (ai < bi)
131 return -1;
132 else if (ai > bi)
133 return 1;
134 return 0;
137 /* Look up splay tree node by ID in splay tree T. */
139 static splay_tree_node
140 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
142 return splay_tree_lookup (t, (splay_tree_key) &id);
145 /* Check if KEY has ID. */
147 static bool
148 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
150 return *(unsigned HOST_WIDE_INT *) key == id;
153 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
154 The ID is allocated separately because we need HOST_WIDE_INTs which may
155 be wider than a splay_tree_key. */
157 static void
158 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
159 struct lto_file_decl_data *file_data)
161 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
162 *idp = id;
163 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
166 /* Create a splay tree. */
168 static splay_tree
169 lto_splay_tree_new (void)
171 return splay_tree_new (lto_splay_tree_compare_ids,
172 lto_splay_tree_delete_id,
173 NULL);
176 /* Decode the content of memory pointed to by DATA in the in decl
177 state object STATE. DATA_IN points to a data_in structure for
178 decoding. Return the address after the decoded object in the
179 input. */
181 static const uint32_t *
182 lto_read_in_decl_state (class data_in *data_in, const uint32_t *data,
183 struct lto_in_decl_state *state)
185 uint32_t ix;
186 tree decl;
187 uint32_t i, j;
189 ix = *data++;
190 state->compressed = ix & 1;
191 ix /= 2;
192 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
193 if (!VAR_OR_FUNCTION_DECL_P (decl))
195 gcc_assert (decl == void_type_node);
196 decl = NULL_TREE;
198 state->fn_decl = decl;
200 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
202 uint32_t size = *data++;
203 vec<tree, va_gc> *decls = NULL;
204 vec_alloc (decls, size);
206 for (j = 0; j < size; j++)
207 vec_safe_push (decls,
208 streamer_tree_cache_get_tree (data_in->reader_cache,
209 data[j]));
211 state->streams[i] = decls;
212 data += size;
215 return data;
219 /* Global canonical type table. */
220 static htab_t gimple_canonical_types;
221 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
222 static unsigned long num_canonical_type_hash_entries;
223 static unsigned long num_canonical_type_hash_queries;
225 /* Types postponed for registration to the canonical type table.
226 During streaming we postpone all TYPE_CXX_ODR_P types so we can alter
227 decide whether there is conflict with non-ODR type or not. */
228 static GTY(()) vec<tree, va_gc> *types_to_register = NULL;
230 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
231 static hashval_t gimple_canonical_type_hash (const void *p);
232 static hashval_t gimple_register_canonical_type_1 (tree t, hashval_t hash);
234 /* Returning a hash value for gimple type TYPE.
236 The hash value returned is equal for types considered compatible
237 by gimple_canonical_types_compatible_p. */
239 static hashval_t
240 hash_canonical_type (tree type)
242 inchash::hash hstate;
243 enum tree_code code;
245 /* We compute alias sets only for types that needs them.
246 Be sure we do not recurse to something else as we cannot hash incomplete
247 types in a way they would have same hash value as compatible complete
248 types. */
249 gcc_checking_assert (type_with_alias_set_p (type));
251 /* Combine a few common features of types so that types are grouped into
252 smaller sets; when searching for existing matching types to merge,
253 only existing types having the same features as the new type will be
254 checked. */
255 code = tree_code_for_canonical_type_merging (TREE_CODE (type));
256 hstate.add_int (code);
257 hstate.add_int (TYPE_MODE (type));
259 /* Incorporate common features of numerical types. */
260 if (INTEGRAL_TYPE_P (type)
261 || SCALAR_FLOAT_TYPE_P (type)
262 || FIXED_POINT_TYPE_P (type)
263 || TREE_CODE (type) == OFFSET_TYPE
264 || POINTER_TYPE_P (type))
266 hstate.add_int (TYPE_PRECISION (type));
267 if (!type_with_interoperable_signedness (type))
268 hstate.add_int (TYPE_UNSIGNED (type));
271 if (VECTOR_TYPE_P (type))
273 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
274 hstate.add_int (TYPE_UNSIGNED (type));
277 if (TREE_CODE (type) == COMPLEX_TYPE)
278 hstate.add_int (TYPE_UNSIGNED (type));
280 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
281 interoperable with "signed char". Unless all frontends are revisited to
282 agree on these types, we must ignore the flag completely. */
284 /* Fortran standard define C_PTR type that is compatible with every
285 C pointer. For this reason we need to glob all pointers into one.
286 Still pointers in different address spaces are not compatible. */
287 if (POINTER_TYPE_P (type))
288 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
290 /* For array types hash the domain bounds and the string flag. */
291 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
293 hstate.add_int (TYPE_STRING_FLAG (type));
294 /* OMP lowering can introduce error_mark_node in place of
295 random local decls in types. */
296 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
297 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
298 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
299 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
302 /* Recurse for aggregates with a single element type. */
303 if (TREE_CODE (type) == ARRAY_TYPE
304 || TREE_CODE (type) == COMPLEX_TYPE
305 || TREE_CODE (type) == VECTOR_TYPE)
306 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
308 /* Incorporate function return and argument types. */
309 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
311 unsigned na;
312 tree p;
314 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
316 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
318 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
319 na++;
322 hstate.add_int (na);
325 if (RECORD_OR_UNION_TYPE_P (type))
327 unsigned nf;
328 tree f;
330 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
331 if (TREE_CODE (f) == FIELD_DECL
332 && (! DECL_SIZE (f)
333 || ! integer_zerop (DECL_SIZE (f))))
335 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
336 nf++;
339 hstate.add_int (nf);
342 return hstate.end();
345 /* Returning a hash value for gimple type TYPE combined with VAL. */
347 static void
348 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
350 hashval_t v;
352 /* All type variants have same TYPE_CANONICAL. */
353 type = TYPE_MAIN_VARIANT (type);
355 if (!canonical_type_used_p (type))
356 v = hash_canonical_type (type);
357 /* An already processed type. */
358 else if (TYPE_CANONICAL (type))
360 type = TYPE_CANONICAL (type);
361 v = gimple_canonical_type_hash (type);
363 else
365 /* Canonical types should not be able to form SCCs by design, this
366 recursion is just because we do not register canonical types in
367 optimal order. To avoid quadratic behavior also register the
368 type here. */
369 v = hash_canonical_type (type);
370 v = gimple_register_canonical_type_1 (type, v);
372 hstate.merge_hash (v);
375 /* Returns the hash for a canonical type P. */
377 static hashval_t
378 gimple_canonical_type_hash (const void *p)
380 num_canonical_type_hash_queries++;
381 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
382 gcc_assert (slot != NULL);
383 return *slot;
388 /* Returns nonzero if P1 and P2 are equal. */
390 static int
391 gimple_canonical_type_eq (const void *p1, const void *p2)
393 const_tree t1 = (const_tree) p1;
394 const_tree t2 = (const_tree) p2;
395 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
396 CONST_CAST_TREE (t2));
399 /* Main worker for gimple_register_canonical_type. */
401 static hashval_t
402 gimple_register_canonical_type_1 (tree t, hashval_t hash)
404 void **slot;
406 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t)
407 && type_with_alias_set_p (t)
408 && canonical_type_used_p (t));
410 /* ODR types for which there is no ODR violation and we did not record
411 structurally equivalent non-ODR type can be treated as unique by their
412 name.
414 hash passed to gimple_register_canonical_type_1 is a structural hash
415 that we can use to lookup structurally equivalent non-ODR type.
416 In case we decide to treat type as unique ODR type we recompute hash based
417 on name and let TBAA machinery know about our decision. */
418 if (RECORD_OR_UNION_TYPE_P (t)
419 && odr_type_p (t) && !odr_type_violation_reported_p (t))
421 /* Anonymous namespace types never conflict with non-C++ types. */
422 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
423 slot = NULL;
424 else
426 /* Here we rely on fact that all non-ODR types was inserted into
427 canonical type hash and thus we can safely detect conflicts between
428 ODR types and interoperable non-ODR types. */
429 gcc_checking_assert (type_streaming_finished
430 && TYPE_MAIN_VARIANT (t) == t);
431 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash,
432 NO_INSERT);
434 if (slot && !TYPE_CXX_ODR_P (*(tree *)slot))
436 tree nonodr = *(tree *)slot;
437 if (symtab->dump_file)
439 fprintf (symtab->dump_file,
440 "ODR and non-ODR type conflict: ");
441 print_generic_expr (symtab->dump_file, t);
442 fprintf (symtab->dump_file, " and ");
443 print_generic_expr (symtab->dump_file, nonodr);
444 fprintf (symtab->dump_file, " mangled:%s\n",
445 IDENTIFIER_POINTER
446 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
448 /* Set canonical for T and all other ODR equivalent duplicates
449 including incomplete structures. */
450 set_type_canonical_for_odr_type (t, nonodr);
452 else
454 tree prevail = prevailing_odr_type (t);
456 if (symtab->dump_file)
458 fprintf (symtab->dump_file,
459 "New canonical ODR type: ");
460 print_generic_expr (symtab->dump_file, t);
461 fprintf (symtab->dump_file, " mangled:%s\n",
462 IDENTIFIER_POINTER
463 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
465 /* Set canonical for T and all other ODR equivalent duplicates
466 including incomplete structures. */
467 set_type_canonical_for_odr_type (t, prevail);
468 enable_odr_based_tbaa (t);
469 if (!type_in_anonymous_namespace_p (t))
470 hash = htab_hash_string (IDENTIFIER_POINTER
471 (DECL_ASSEMBLER_NAME
472 (TYPE_NAME (t))));
473 else
474 hash = TYPE_UID (t);
476 /* All variants of t now have TYPE_CANONICAL set to prevail.
477 Update canonical type hash cache accordingly. */
478 num_canonical_type_hash_entries++;
479 bool existed_p = canonical_type_hash_cache->put (prevail, hash);
480 gcc_checking_assert (!existed_p);
482 return hash;
485 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
486 if (*slot)
488 tree new_type = (tree)(*slot);
489 gcc_checking_assert (new_type != t);
490 TYPE_CANONICAL (t) = new_type;
492 else
494 TYPE_CANONICAL (t) = t;
495 *slot = (void *) t;
496 /* Cache the just computed hash value. */
497 num_canonical_type_hash_entries++;
498 bool existed_p = canonical_type_hash_cache->put (t, hash);
499 gcc_assert (!existed_p);
501 return hash;
504 /* Register type T in the global type table gimple_types and set
505 TYPE_CANONICAL of T accordingly.
506 This is used by LTO to merge structurally equivalent types for
507 type-based aliasing purposes across different TUs and languages.
509 ??? This merging does not exactly match how the tree.c middle-end
510 functions will assign TYPE_CANONICAL when new types are created
511 during optimization (which at least happens for pointer and array
512 types). */
514 static void
515 gimple_register_canonical_type (tree t)
517 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t)
518 || !canonical_type_used_p (t))
519 return;
521 /* Canonical types are same among all complete variants. */
522 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
523 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
524 else
526 hashval_t h = hash_canonical_type (TYPE_MAIN_VARIANT (t));
527 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t), h);
528 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
532 /* Re-compute TYPE_CANONICAL for NODE and related types. */
534 static void
535 lto_register_canonical_types (tree node, bool first_p)
537 if (!node
538 || !TYPE_P (node))
539 return;
541 if (first_p)
542 TYPE_CANONICAL (node) = NULL_TREE;
544 if (POINTER_TYPE_P (node)
545 || TREE_CODE (node) == COMPLEX_TYPE
546 || TREE_CODE (node) == ARRAY_TYPE)
547 lto_register_canonical_types (TREE_TYPE (node), first_p);
549 if (!first_p)
550 gimple_register_canonical_type (node);
553 /* Finish canonical type calculation: after all units has been streamed in we
554 can check if given ODR type structurally conflicts with a non-ODR type. In
555 the first case we set type canonical according to the canonical type hash.
556 In the second case we use type names. */
558 static void
559 lto_register_canonical_types_for_odr_types ()
561 tree t;
562 unsigned int i;
564 if (!types_to_register)
565 return;
567 type_streaming_finished = true;
569 /* Be sure that no types derived from ODR types was
570 not inserted into the hash table. */
571 if (flag_checking)
572 FOR_EACH_VEC_ELT (*types_to_register, i, t)
573 gcc_assert (!TYPE_CANONICAL (t));
575 /* Register all remaining types. */
576 FOR_EACH_VEC_ELT (*types_to_register, i, t)
578 /* For pre-streamed types like va-arg it is possible that main variant
579 is !CXX_ODR_P while the variant (which is streamed) is.
580 Copy CXX_ODR_P to make type verifier happy. This is safe because
581 in canonical type calculation we only consider main variants.
582 However we can not change this flag before streaming is finished
583 to not affect tree merging. */
584 TYPE_CXX_ODR_P (t) = TYPE_CXX_ODR_P (TYPE_MAIN_VARIANT (t));
585 if (!TYPE_CANONICAL (t))
586 gimple_register_canonical_type (t);
591 /* Remember trees that contains references to declarations. */
592 vec <tree, va_gc> *tree_with_vars;
594 #define CHECK_VAR(tt) \
595 do \
597 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
598 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
599 return true; \
600 } while (0)
602 #define CHECK_NO_VAR(tt) \
603 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
605 /* Check presence of pointers to decls in fields of a tree_typed T. */
607 static inline bool
608 mentions_vars_p_typed (tree t)
610 CHECK_NO_VAR (TREE_TYPE (t));
611 return false;
614 /* Check presence of pointers to decls in fields of a tree_common T. */
616 static inline bool
617 mentions_vars_p_common (tree t)
619 if (mentions_vars_p_typed (t))
620 return true;
621 CHECK_NO_VAR (TREE_CHAIN (t));
622 return false;
625 /* Check presence of pointers to decls in fields of a decl_minimal T. */
627 static inline bool
628 mentions_vars_p_decl_minimal (tree t)
630 if (mentions_vars_p_common (t))
631 return true;
632 CHECK_NO_VAR (DECL_NAME (t));
633 CHECK_VAR (DECL_CONTEXT (t));
634 return false;
637 /* Check presence of pointers to decls in fields of a decl_common T. */
639 static inline bool
640 mentions_vars_p_decl_common (tree t)
642 if (mentions_vars_p_decl_minimal (t))
643 return true;
644 CHECK_VAR (DECL_SIZE (t));
645 CHECK_VAR (DECL_SIZE_UNIT (t));
646 CHECK_VAR (DECL_INITIAL (t));
647 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
648 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
649 return false;
652 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
654 static inline bool
655 mentions_vars_p_decl_with_vis (tree t)
657 if (mentions_vars_p_decl_common (t))
658 return true;
660 /* Accessor macro has side-effects, use field-name here. */
661 CHECK_NO_VAR (DECL_ASSEMBLER_NAME_RAW (t));
662 return false;
665 /* Check presence of pointers to decls in fields of a decl_non_common T. */
667 static inline bool
668 mentions_vars_p_decl_non_common (tree t)
670 if (mentions_vars_p_decl_with_vis (t))
671 return true;
672 CHECK_NO_VAR (DECL_RESULT_FLD (t));
673 return false;
676 /* Check presence of pointers to decls in fields of a decl_non_common T. */
678 static bool
679 mentions_vars_p_function (tree t)
681 if (mentions_vars_p_decl_non_common (t))
682 return true;
683 CHECK_NO_VAR (DECL_ARGUMENTS (t));
684 CHECK_NO_VAR (DECL_VINDEX (t));
685 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
686 return false;
689 /* Check presence of pointers to decls in fields of a field_decl T. */
691 static bool
692 mentions_vars_p_field_decl (tree t)
694 if (mentions_vars_p_decl_common (t))
695 return true;
696 CHECK_VAR (DECL_FIELD_OFFSET (t));
697 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
698 CHECK_NO_VAR (DECL_QUALIFIER (t));
699 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
700 CHECK_NO_VAR (DECL_FCONTEXT (t));
701 return false;
704 /* Check presence of pointers to decls in fields of a type T. */
706 static bool
707 mentions_vars_p_type (tree t)
709 if (mentions_vars_p_common (t))
710 return true;
711 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
712 CHECK_VAR (TYPE_SIZE (t));
713 CHECK_VAR (TYPE_SIZE_UNIT (t));
714 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
715 CHECK_NO_VAR (TYPE_NAME (t));
717 CHECK_VAR (TYPE_MIN_VALUE_RAW (t));
718 CHECK_VAR (TYPE_MAX_VALUE_RAW (t));
720 /* Accessor is for derived node types only. */
721 CHECK_NO_VAR (TYPE_LANG_SLOT_1 (t));
723 CHECK_VAR (TYPE_CONTEXT (t));
724 CHECK_NO_VAR (TYPE_CANONICAL (t));
725 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
726 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
727 return false;
730 /* Check presence of pointers to decls in fields of a BINFO T. */
732 static bool
733 mentions_vars_p_binfo (tree t)
735 unsigned HOST_WIDE_INT i, n;
737 if (mentions_vars_p_common (t))
738 return true;
739 CHECK_VAR (BINFO_VTABLE (t));
740 CHECK_NO_VAR (BINFO_OFFSET (t));
741 CHECK_NO_VAR (BINFO_VIRTUALS (t));
742 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
743 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
744 for (i = 0; i < n; i++)
745 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
746 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
747 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
748 n = BINFO_N_BASE_BINFOS (t);
749 for (i = 0; i < n; i++)
750 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
751 return false;
754 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
756 static bool
757 mentions_vars_p_constructor (tree t)
759 unsigned HOST_WIDE_INT idx;
760 constructor_elt *ce;
762 if (mentions_vars_p_typed (t))
763 return true;
765 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
767 CHECK_NO_VAR (ce->index);
768 CHECK_VAR (ce->value);
770 return false;
773 /* Check presence of pointers to decls in fields of an expression tree T. */
775 static bool
776 mentions_vars_p_expr (tree t)
778 int i;
779 if (mentions_vars_p_typed (t))
780 return true;
781 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
782 CHECK_VAR (TREE_OPERAND (t, i));
783 return false;
786 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
788 static bool
789 mentions_vars_p_omp_clause (tree t)
791 int i;
792 if (mentions_vars_p_common (t))
793 return true;
794 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
795 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
796 return false;
799 /* Check presence of pointers to decls that needs later fixup in T. */
801 static bool
802 mentions_vars_p (tree t)
804 switch (TREE_CODE (t))
806 case IDENTIFIER_NODE:
807 break;
809 case TREE_LIST:
810 CHECK_VAR (TREE_VALUE (t));
811 CHECK_VAR (TREE_PURPOSE (t));
812 CHECK_NO_VAR (TREE_CHAIN (t));
813 break;
815 case FIELD_DECL:
816 return mentions_vars_p_field_decl (t);
818 case LABEL_DECL:
819 case CONST_DECL:
820 case PARM_DECL:
821 case RESULT_DECL:
822 case IMPORTED_DECL:
823 case NAMESPACE_DECL:
824 case NAMELIST_DECL:
825 return mentions_vars_p_decl_common (t);
827 case VAR_DECL:
828 return mentions_vars_p_decl_with_vis (t);
830 case TYPE_DECL:
831 return mentions_vars_p_decl_non_common (t);
833 case FUNCTION_DECL:
834 return mentions_vars_p_function (t);
836 case TREE_BINFO:
837 return mentions_vars_p_binfo (t);
839 case PLACEHOLDER_EXPR:
840 return mentions_vars_p_common (t);
842 case BLOCK:
843 case TRANSLATION_UNIT_DECL:
844 case OPTIMIZATION_NODE:
845 case TARGET_OPTION_NODE:
846 break;
848 case CONSTRUCTOR:
849 return mentions_vars_p_constructor (t);
851 case OMP_CLAUSE:
852 return mentions_vars_p_omp_clause (t);
854 default:
855 if (TYPE_P (t))
857 if (mentions_vars_p_type (t))
858 return true;
860 else if (EXPR_P (t))
862 if (mentions_vars_p_expr (t))
863 return true;
865 else if (CONSTANT_CLASS_P (t))
866 CHECK_NO_VAR (TREE_TYPE (t));
867 else
868 gcc_unreachable ();
870 return false;
874 /* Return the resolution for the decl with index INDEX from DATA_IN. */
876 static enum ld_plugin_symbol_resolution
877 get_resolution (class data_in *data_in, unsigned index)
879 if (data_in->globals_resolution.exists ())
881 ld_plugin_symbol_resolution_t ret;
882 /* We can have references to not emitted functions in
883 DECL_FUNCTION_PERSONALITY at least. So we can and have
884 to indeed return LDPR_UNKNOWN in some cases. */
885 if (data_in->globals_resolution.length () <= index)
886 return LDPR_UNKNOWN;
887 ret = data_in->globals_resolution[index];
888 return ret;
890 else
891 /* Delay resolution finding until decl merging. */
892 return LDPR_UNKNOWN;
895 /* We need to record resolutions until symbol table is read. */
896 static void
897 register_resolution (struct lto_file_decl_data *file_data, tree decl,
898 enum ld_plugin_symbol_resolution resolution)
900 bool existed;
901 if (resolution == LDPR_UNKNOWN)
902 return;
903 if (!file_data->resolution_map)
904 file_data->resolution_map
905 = new hash_map<tree, ld_plugin_symbol_resolution>;
906 ld_plugin_symbol_resolution_t &res
907 = file_data->resolution_map->get_or_insert (decl, &existed);
908 if (!existed
909 || resolution == LDPR_PREVAILING_DEF_IRONLY
910 || resolution == LDPR_PREVAILING_DEF
911 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
912 res = resolution;
915 /* Register DECL with the global symbol table and change its
916 name if necessary to avoid name clashes for static globals across
917 different files. */
919 static void
920 lto_register_var_decl_in_symtab (class data_in *data_in, tree decl,
921 unsigned ix)
923 tree context;
925 /* Variable has file scope, not local. */
926 if (!TREE_PUBLIC (decl)
927 && !((context = decl_function_context (decl))
928 && auto_var_in_fn_p (decl, context)))
929 rest_of_decl_compilation (decl, 1, 0);
931 /* If this variable has already been declared, queue the
932 declaration for merging. */
933 if (TREE_PUBLIC (decl))
934 register_resolution (data_in->file_data,
935 decl, get_resolution (data_in, ix));
939 /* Register DECL with the global symbol table and change its
940 name if necessary to avoid name clashes for static globals across
941 different files. DATA_IN contains descriptors and tables for the
942 file being read. */
944 static void
945 lto_register_function_decl_in_symtab (class data_in *data_in, tree decl,
946 unsigned ix)
948 /* If this variable has already been declared, queue the
949 declaration for merging. */
950 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
951 register_resolution (data_in->file_data,
952 decl, get_resolution (data_in, ix));
955 /* Check if T is a decl and needs register its resolution info. */
957 static void
958 lto_maybe_register_decl (class data_in *data_in, tree t, unsigned ix)
960 if (TREE_CODE (t) == VAR_DECL)
961 lto_register_var_decl_in_symtab (data_in, t, ix);
962 else if (TREE_CODE (t) == FUNCTION_DECL
963 && !fndecl_built_in_p (t))
964 lto_register_function_decl_in_symtab (data_in, t, ix);
968 /* For the type T re-materialize it in the type variant list and
969 the pointer/reference-to chains. */
971 static void
972 lto_fixup_prevailing_type (tree t)
974 /* The following re-creates proper variant lists while fixing up
975 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
976 variant list state before fixup is broken. */
978 /* If we are not our own variant leader link us into our new leaders
979 variant list. */
980 if (TYPE_MAIN_VARIANT (t) != t)
982 tree mv = TYPE_MAIN_VARIANT (t);
983 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
984 TYPE_NEXT_VARIANT (mv) = t;
987 /* The following reconstructs the pointer chains
988 of the new pointed-to type if we are a main variant. We do
989 not stream those so they are broken before fixup. */
990 if (TREE_CODE (t) == POINTER_TYPE
991 && TYPE_MAIN_VARIANT (t) == t)
993 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
994 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
996 else if (TREE_CODE (t) == REFERENCE_TYPE
997 && TYPE_MAIN_VARIANT (t) == t)
999 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1000 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1005 /* We keep prevailing tree SCCs in a hashtable with manual collision
1006 handling (in case all hashes compare the same) and keep the colliding
1007 entries in the tree_scc->next chain. */
1009 struct tree_scc
1011 tree_scc *next;
1012 /* Hash of the whole SCC. */
1013 hashval_t hash;
1014 /* Number of trees in the SCC. */
1015 unsigned len;
1016 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1017 which share the same individual tree hash). */
1018 unsigned entry_len;
1019 /* The members of the SCC.
1020 We only need to remember the first entry node candidate for prevailing
1021 SCCs (but of course have access to all entries for SCCs we are
1022 processing).
1023 ??? For prevailing SCCs we really only need hash and the first
1024 entry candidate, but that's too awkward to implement. */
1025 tree entries[1];
1028 struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
1030 static inline hashval_t hash (const tree_scc *);
1031 static inline bool equal (const tree_scc *, const tree_scc *);
1034 hashval_t
1035 tree_scc_hasher::hash (const tree_scc *scc)
1037 return scc->hash;
1040 bool
1041 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
1043 if (scc1->hash != scc2->hash
1044 || scc1->len != scc2->len
1045 || scc1->entry_len != scc2->entry_len)
1046 return false;
1047 return true;
1050 static hash_table<tree_scc_hasher> *tree_scc_hash;
1051 static struct obstack tree_scc_hash_obstack;
1053 static unsigned long num_merged_types;
1054 static unsigned long num_prevailing_types;
1055 static unsigned long num_type_scc_trees;
1056 static unsigned long total_scc_size;
1057 static unsigned long num_sccs_read;
1058 static unsigned long total_scc_size_merged;
1059 static unsigned long num_sccs_merged;
1060 static unsigned long num_scc_compares;
1061 static unsigned long num_scc_compare_collisions;
1064 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1065 recursing through in-SCC tree edges. Returns true if the SCCs entered
1066 through T1 and T2 are equal and fills in *MAP with the pairs of
1067 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1069 static bool
1070 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1072 enum tree_code code;
1074 /* Mark already visited nodes. */
1075 TREE_ASM_WRITTEN (t2) = 1;
1077 /* Push the pair onto map. */
1078 (*map)[0] = t1;
1079 (*map)[1] = t2;
1080 *map = *map + 2;
1082 /* Compare value-fields. */
1083 #define compare_values(X) \
1084 do { \
1085 if (X(t1) != X(t2)) \
1086 return false; \
1087 } while (0)
1089 compare_values (TREE_CODE);
1090 code = TREE_CODE (t1);
1092 if (!TYPE_P (t1))
1094 compare_values (TREE_SIDE_EFFECTS);
1095 compare_values (TREE_CONSTANT);
1096 compare_values (TREE_READONLY);
1097 compare_values (TREE_PUBLIC);
1099 compare_values (TREE_ADDRESSABLE);
1100 compare_values (TREE_THIS_VOLATILE);
1101 if (DECL_P (t1))
1102 compare_values (DECL_UNSIGNED);
1103 else if (TYPE_P (t1))
1104 compare_values (TYPE_UNSIGNED);
1105 if (TYPE_P (t1))
1106 compare_values (TYPE_ARTIFICIAL);
1107 else
1108 compare_values (TREE_NO_WARNING);
1109 compare_values (TREE_NOTHROW);
1110 compare_values (TREE_STATIC);
1111 if (code != TREE_BINFO)
1112 compare_values (TREE_PRIVATE);
1113 compare_values (TREE_PROTECTED);
1114 compare_values (TREE_DEPRECATED);
1115 if (TYPE_P (t1))
1117 if (AGGREGATE_TYPE_P (t1))
1118 compare_values (TYPE_REVERSE_STORAGE_ORDER);
1119 else
1120 compare_values (TYPE_SATURATING);
1121 compare_values (TYPE_ADDR_SPACE);
1123 else if (code == SSA_NAME)
1124 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1126 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1128 if (wi::to_wide (t1) != wi::to_wide (t2))
1129 return false;
1132 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1134 /* ??? No suitable compare routine available. */
1135 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1136 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1137 if (r1.cl != r2.cl
1138 || r1.decimal != r2.decimal
1139 || r1.sign != r2.sign
1140 || r1.signalling != r2.signalling
1141 || r1.canonical != r2.canonical
1142 || r1.uexp != r2.uexp)
1143 return false;
1144 for (unsigned i = 0; i < SIGSZ; ++i)
1145 if (r1.sig[i] != r2.sig[i])
1146 return false;
1149 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1150 if (!fixed_compare (EQ_EXPR,
1151 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1152 return false;
1154 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1156 compare_values (VECTOR_CST_LOG2_NPATTERNS);
1157 compare_values (VECTOR_CST_NELTS_PER_PATTERN);
1160 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1162 compare_values (DECL_MODE);
1163 compare_values (DECL_NONLOCAL);
1164 compare_values (DECL_VIRTUAL_P);
1165 compare_values (DECL_IGNORED_P);
1166 compare_values (DECL_ABSTRACT_P);
1167 compare_values (DECL_ARTIFICIAL);
1168 compare_values (DECL_USER_ALIGN);
1169 compare_values (DECL_PRESERVE_P);
1170 compare_values (DECL_EXTERNAL);
1171 compare_values (DECL_GIMPLE_REG_P);
1172 compare_values (DECL_ALIGN);
1173 if (code == LABEL_DECL)
1175 compare_values (EH_LANDING_PAD_NR);
1176 compare_values (LABEL_DECL_UID);
1178 else if (code == FIELD_DECL)
1180 compare_values (DECL_PACKED);
1181 compare_values (DECL_NONADDRESSABLE_P);
1182 compare_values (DECL_PADDING_P);
1183 compare_values (DECL_OFFSET_ALIGN);
1185 else if (code == VAR_DECL)
1187 compare_values (DECL_HAS_DEBUG_EXPR_P);
1188 compare_values (DECL_NONLOCAL_FRAME);
1190 if (code == RESULT_DECL
1191 || code == PARM_DECL
1192 || code == VAR_DECL)
1194 compare_values (DECL_BY_REFERENCE);
1195 if (code == VAR_DECL
1196 || code == PARM_DECL)
1197 compare_values (DECL_HAS_VALUE_EXPR_P);
1201 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1202 compare_values (DECL_REGISTER);
1204 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1206 compare_values (DECL_COMMON);
1207 compare_values (DECL_DLLIMPORT_P);
1208 compare_values (DECL_WEAK);
1209 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1210 compare_values (DECL_COMDAT);
1211 compare_values (DECL_VISIBILITY);
1212 compare_values (DECL_VISIBILITY_SPECIFIED);
1213 if (code == VAR_DECL)
1215 compare_values (DECL_HARD_REGISTER);
1216 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1217 compare_values (DECL_IN_CONSTANT_POOL);
1221 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1223 compare_values (DECL_BUILT_IN_CLASS);
1224 compare_values (DECL_STATIC_CONSTRUCTOR);
1225 compare_values (DECL_STATIC_DESTRUCTOR);
1226 compare_values (DECL_UNINLINABLE);
1227 compare_values (DECL_POSSIBLY_INLINED);
1228 compare_values (DECL_IS_NOVOPS);
1229 compare_values (DECL_IS_RETURNS_TWICE);
1230 compare_values (DECL_IS_MALLOC);
1231 compare_values (DECL_IS_OPERATOR_NEW_P);
1232 compare_values (DECL_DECLARED_INLINE_P);
1233 compare_values (DECL_STATIC_CHAIN);
1234 compare_values (DECL_NO_INLINE_WARNING_P);
1235 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1236 compare_values (DECL_NO_LIMIT_STACK);
1237 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1238 compare_values (DECL_PURE_P);
1239 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1240 compare_values (DECL_FINAL_P);
1241 compare_values (DECL_CXX_CONSTRUCTOR_P);
1242 compare_values (DECL_CXX_DESTRUCTOR_P);
1243 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1244 compare_values (DECL_UNCHECKED_FUNCTION_CODE);
1247 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1249 compare_values (TYPE_MODE);
1250 compare_values (TYPE_NEEDS_CONSTRUCTING);
1251 if (RECORD_OR_UNION_TYPE_P (t1))
1253 compare_values (TYPE_TRANSPARENT_AGGR);
1254 compare_values (TYPE_FINAL_P);
1255 compare_values (TYPE_CXX_ODR_P);
1257 else if (code == ARRAY_TYPE)
1258 compare_values (TYPE_NONALIASED_COMPONENT);
1259 if (code == ARRAY_TYPE || code == INTEGER_TYPE)
1260 compare_values (TYPE_STRING_FLAG);
1261 if (AGGREGATE_TYPE_P (t1))
1262 compare_values (TYPE_TYPELESS_STORAGE);
1263 compare_values (TYPE_EMPTY_P);
1264 compare_values (TYPE_PACKED);
1265 compare_values (TYPE_RESTRICT);
1266 compare_values (TYPE_USER_ALIGN);
1267 compare_values (TYPE_READONLY);
1268 compare_values (TYPE_PRECISION);
1269 compare_values (TYPE_ALIGN);
1270 /* Do not compare TYPE_ALIAS_SET. Doing so introduce ordering issues
1271 with calls to get_alias_set which may initialize it for streamed
1272 in types. */
1275 /* We don't want to compare locations, so there is nothing do compare
1276 for TS_EXP. */
1278 /* BLOCKs are function local and we don't merge anything there, so
1279 simply refuse to merge. */
1280 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1281 return false;
1283 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1284 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1285 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1286 return false;
1288 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1289 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1290 return false;
1292 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1293 if (!cl_optimization_option_eq (TREE_OPTIMIZATION (t1),
1294 TREE_OPTIMIZATION (t2)))
1295 return false;
1297 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1298 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1299 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1300 return false;
1302 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1303 compare_values (CONSTRUCTOR_NELTS);
1305 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1306 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1307 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1308 IDENTIFIER_LENGTH (t1)) != 0)
1309 return false;
1311 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1312 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1313 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1314 TREE_STRING_LENGTH (t1)) != 0)
1315 return false;
1317 if (code == OMP_CLAUSE)
1319 compare_values (OMP_CLAUSE_CODE);
1320 switch (OMP_CLAUSE_CODE (t1))
1322 case OMP_CLAUSE_DEFAULT:
1323 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1324 break;
1325 case OMP_CLAUSE_SCHEDULE:
1326 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1327 break;
1328 case OMP_CLAUSE_DEPEND:
1329 compare_values (OMP_CLAUSE_DEPEND_KIND);
1330 break;
1331 case OMP_CLAUSE_MAP:
1332 compare_values (OMP_CLAUSE_MAP_KIND);
1333 break;
1334 case OMP_CLAUSE_PROC_BIND:
1335 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1336 break;
1337 case OMP_CLAUSE_REDUCTION:
1338 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1339 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1340 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1341 break;
1342 default:
1343 break;
1347 #undef compare_values
1350 /* Compare pointer fields. */
1352 /* Recurse. Search & Replaced from DFS_write_tree_body.
1353 Folding the early checks into the compare_tree_edges recursion
1354 macro makes debugging way quicker as you are able to break on
1355 compare_tree_sccs_1 and simply finish until a call returns false
1356 to spot the SCC members with the difference. */
1357 #define compare_tree_edges(E1, E2) \
1358 do { \
1359 tree t1_ = (E1), t2_ = (E2); \
1360 if (t1_ != t2_ \
1361 && (!t1_ || !t2_ \
1362 || !TREE_VISITED (t2_) \
1363 || (!TREE_ASM_WRITTEN (t2_) \
1364 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1365 return false; \
1366 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1367 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1368 } while (0)
1370 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1372 if (code != IDENTIFIER_NODE)
1373 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1376 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1378 /* Note that the number of elements for EXPR has already been emitted
1379 in EXPR's header (see streamer_write_tree_header). */
1380 unsigned int count = vector_cst_encoded_nelts (t1);
1381 for (unsigned int i = 0; i < count; ++i)
1382 compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
1383 VECTOR_CST_ENCODED_ELT (t2, i));
1386 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1388 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1389 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1392 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1394 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1395 /* ??? Global decls from different TUs have non-matching
1396 TRANSLATION_UNIT_DECLs. Only consider a small set of
1397 decls equivalent, we should not end up merging others. */
1398 if ((code == TYPE_DECL
1399 || code == NAMESPACE_DECL
1400 || code == IMPORTED_DECL
1401 || code == CONST_DECL
1402 || (VAR_OR_FUNCTION_DECL_P (t1)
1403 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1404 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1406 else
1407 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1410 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1412 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1413 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1414 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1415 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1), DECL_ABSTRACT_ORIGIN (t2));
1416 if ((code == VAR_DECL
1417 || code == PARM_DECL)
1418 && DECL_HAS_VALUE_EXPR_P (t1))
1419 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1420 if (code == VAR_DECL
1421 && DECL_HAS_DEBUG_EXPR_P (t1))
1422 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1423 /* LTO specific edges. */
1424 if (code != FUNCTION_DECL
1425 && code != TRANSLATION_UNIT_DECL)
1426 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1429 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1431 if (code == FUNCTION_DECL)
1433 tree a1, a2;
1434 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1435 a1 || a2;
1436 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1437 compare_tree_edges (a1, a2);
1438 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1440 else if (code == TYPE_DECL)
1441 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1444 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1446 /* Make sure we don't inadvertently set the assembler name. */
1447 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1448 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1449 DECL_ASSEMBLER_NAME (t2));
1452 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1454 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1455 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1456 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1457 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1458 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1459 DECL_FIELD_BIT_OFFSET (t2));
1460 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1463 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1465 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1466 DECL_FUNCTION_PERSONALITY (t2));
1467 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1468 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1469 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1470 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1471 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1474 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1476 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1477 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1478 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1479 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1480 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1481 reconstructed during fixup. */
1482 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1483 during fixup. */
1484 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1485 /* ??? Global types from different TUs have non-matching
1486 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1487 equal. */
1488 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1490 else
1491 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1492 /* TYPE_CANONICAL is re-computed during type merging, so do not
1493 compare it here. */
1494 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1497 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1499 if (code == ENUMERAL_TYPE)
1500 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1501 else if (code == ARRAY_TYPE)
1502 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1503 else if (RECORD_OR_UNION_TYPE_P (t1))
1505 tree f1, f2;
1506 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1507 f1 || f2;
1508 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1509 compare_tree_edges (f1, f2);
1511 else if (code == FUNCTION_TYPE
1512 || code == METHOD_TYPE)
1513 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1515 if (!POINTER_TYPE_P (t1))
1516 compare_tree_edges (TYPE_MIN_VALUE_RAW (t1), TYPE_MIN_VALUE_RAW (t2));
1517 compare_tree_edges (TYPE_MAX_VALUE_RAW (t1), TYPE_MAX_VALUE_RAW (t2));
1520 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1522 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1523 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1524 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1527 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1528 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1529 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1531 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1533 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1534 compare_tree_edges (TREE_OPERAND (t1, i),
1535 TREE_OPERAND (t2, i));
1537 /* BLOCKs are function local and we don't merge anything there. */
1538 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1539 return false;
1542 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1544 unsigned i;
1545 tree t;
1546 /* Lengths have already been compared above. */
1547 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1548 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1549 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1550 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1551 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1552 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1553 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1554 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1555 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1558 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1560 unsigned i;
1561 tree index, value;
1562 /* Lengths have already been compared above. */
1563 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1565 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1566 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1570 if (code == OMP_CLAUSE)
1572 int i;
1574 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1575 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1576 OMP_CLAUSE_OPERAND (t2, i));
1577 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1580 #undef compare_tree_edges
1582 return true;
1585 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1586 out MAP if they are equal. */
1588 static bool
1589 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1590 tree *map)
1592 /* Assume SCC entry hashes are sorted after their cardinality. Which
1593 means we can simply take the first n-tuple of equal hashes
1594 (which is recorded as entry_len) and do n SCC entry candidate
1595 comparisons. */
1596 for (unsigned i = 0; i < pscc->entry_len; ++i)
1598 tree *mapp = map;
1599 num_scc_compare_collisions++;
1600 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1602 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1603 on the scc as all trees will be freed. */
1604 return true;
1606 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1607 the SCC prevails. */
1608 for (unsigned j = 0; j < scc->len; ++j)
1609 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1612 return false;
1615 /* QSort sort function to sort a map of two pointers after the 2nd
1616 pointer. */
1618 static int
1619 cmp_tree (const void *p1_, const void *p2_)
1621 tree *p1 = (tree *)(const_cast<void *>(p1_));
1622 tree *p2 = (tree *)(const_cast<void *>(p2_));
1623 if (p1[1] == p2[1])
1624 return 0;
1625 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1628 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1629 hash value SCC_HASH with an already recorded SCC. Return true if
1630 that was successful, otherwise return false. */
1632 static bool
1633 unify_scc (class data_in *data_in, unsigned from,
1634 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1636 bool unified_p = false;
1637 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1638 tree_scc *scc
1639 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1640 scc->next = NULL;
1641 scc->hash = scc_hash;
1642 scc->len = len;
1643 scc->entry_len = scc_entry_len;
1644 for (unsigned i = 0; i < len; ++i)
1646 tree t = streamer_tree_cache_get_tree (cache, from + i);
1647 scc->entries[i] = t;
1648 /* Do not merge SCCs with local entities inside them. Also do
1649 not merge TRANSLATION_UNIT_DECLs and anonymous namespace types. */
1650 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1651 || (VAR_OR_FUNCTION_DECL_P (t)
1652 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1653 || TREE_CODE (t) == LABEL_DECL
1654 || (TYPE_P (t)
1655 && type_with_linkage_p (TYPE_MAIN_VARIANT (t))
1656 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t))))
1658 /* Avoid doing any work for these cases and do not worry to
1659 record the SCCs for further merging. */
1660 return false;
1664 /* Look for the list of candidate SCCs to compare against. */
1665 tree_scc **slot;
1666 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1667 if (*slot)
1669 /* Try unifying against each candidate. */
1670 num_scc_compares++;
1672 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1673 outside of the scc when following tree edges. Make sure
1674 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1675 to track whether we visited the SCC member during the compare.
1676 We cannot use TREE_VISITED on the pscc members as the extended
1677 scc and pscc can overlap. */
1678 for (unsigned i = 0; i < scc->len; ++i)
1680 TREE_VISITED (scc->entries[i]) = 1;
1681 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1684 tree *map = XALLOCAVEC (tree, 2 * len);
1685 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1687 if (!compare_tree_sccs (pscc, scc, map))
1688 continue;
1690 /* Found an equal SCC. */
1691 unified_p = true;
1692 num_scc_compare_collisions--;
1693 num_sccs_merged++;
1694 total_scc_size_merged += len;
1696 if (flag_checking)
1697 for (unsigned i = 0; i < len; ++i)
1699 tree t = map[2*i+1];
1700 enum tree_code code = TREE_CODE (t);
1701 /* IDENTIFIER_NODEs should be singletons and are merged by the
1702 streamer. The others should be singletons, too, and we
1703 should not merge them in any way. */
1704 gcc_assert (code != TRANSLATION_UNIT_DECL
1705 && code != IDENTIFIER_NODE);
1708 /* Fixup the streamer cache with the prevailing nodes according
1709 to the tree node mapping computed by compare_tree_sccs. */
1710 if (len == 1)
1712 /* If we got a debug reference queued, see if the prevailing
1713 tree has a debug reference and if not, register the one
1714 for the tree we are about to throw away. */
1715 if (dref_queue.length () == 1)
1717 dref_entry e = dref_queue.pop ();
1718 gcc_assert (e.decl
1719 == streamer_tree_cache_get_tree (cache, from));
1720 const char *sym;
1721 unsigned HOST_WIDE_INT off;
1722 if (!debug_hooks->die_ref_for_decl (pscc->entries[0], &sym,
1723 &off))
1724 debug_hooks->register_external_die (pscc->entries[0],
1725 e.sym, e.off);
1727 lto_maybe_register_decl (data_in, pscc->entries[0], from);
1728 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1730 else
1732 tree *map2 = XALLOCAVEC (tree, 2 * len);
1733 for (unsigned i = 0; i < len; ++i)
1735 map2[i*2] = (tree)(uintptr_t)(from + i);
1736 map2[i*2+1] = scc->entries[i];
1738 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1739 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1740 for (unsigned i = 0; i < len; ++i)
1742 lto_maybe_register_decl (data_in, map[2*i],
1743 (uintptr_t)map2[2*i]);
1744 streamer_tree_cache_replace_tree (cache, map[2*i],
1745 (uintptr_t)map2[2*i]);
1749 /* Free the tree nodes from the read SCC. */
1750 data_in->location_cache.revert_location_cache ();
1751 for (unsigned i = 0; i < len; ++i)
1753 if (TYPE_P (scc->entries[i]))
1754 num_merged_types++;
1755 free_node (scc->entries[i]);
1758 /* Drop DIE references.
1759 ??? Do as in the size-one SCC case which involves sorting
1760 the queue. */
1761 dref_queue.truncate (0);
1763 break;
1766 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1767 if (!unified_p)
1768 for (unsigned i = 0; i < scc->len; ++i)
1769 TREE_VISITED (scc->entries[i]) = 0;
1772 /* If we didn't unify it to any candidate duplicate the relevant
1773 pieces to permanent storage and link it into the chain. */
1774 if (!unified_p)
1776 tree_scc *pscc
1777 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1778 memcpy (pscc, scc, sizeof (tree_scc));
1779 pscc->next = (*slot);
1780 *slot = pscc;
1782 return unified_p;
1787 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1788 RESOLUTIONS is the set of symbols picked by the linker (read from the
1789 resolution file when the linker plugin is being used). */
1791 static void
1792 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1793 vec<ld_plugin_symbol_resolution_t> resolutions)
1795 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1796 const int decl_offset = sizeof (struct lto_decl_header);
1797 const int main_offset = decl_offset + header->decl_state_size;
1798 const int string_offset = main_offset + header->main_size;
1799 class data_in *data_in;
1800 unsigned int i;
1801 const uint32_t *data_ptr, *data_end;
1802 uint32_t num_decl_states;
1804 lto_input_block ib_main ((const char *) data + main_offset,
1805 header->main_size, decl_data->mode_table);
1807 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1808 header->string_size, resolutions);
1810 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1811 internal types that should not be merged. */
1813 typedef int_hash<unsigned, 0, UINT_MAX> code_id_hash;
1814 hash_map <code_id_hash, unsigned> hm;
1815 unsigned total = 0;
1817 /* Read the global declarations and types. */
1818 while (ib_main.p < ib_main.len)
1820 tree t;
1821 unsigned from = data_in->reader_cache->nodes.length ();
1822 /* Read and uniquify SCCs as in the input stream. */
1823 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1824 if (tag == LTO_tree_scc)
1826 unsigned len_;
1827 unsigned scc_entry_len;
1828 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1829 &scc_entry_len);
1830 unsigned len = data_in->reader_cache->nodes.length () - from;
1831 gcc_assert (len == len_);
1833 total_scc_size += len;
1834 num_sccs_read++;
1836 /* We have the special case of size-1 SCCs that are pre-merged
1837 by means of identifier and string sharing for example.
1838 ??? Maybe we should avoid streaming those as SCCs. */
1839 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1840 from);
1841 if (len == 1
1842 && (TREE_CODE (first) == IDENTIFIER_NODE
1843 || (TREE_CODE (first) == INTEGER_CST
1844 && !TREE_OVERFLOW (first))))
1845 continue;
1847 /* Try to unify the SCC with already existing ones. */
1848 if (!flag_ltrans
1849 && unify_scc (data_in, from,
1850 len, scc_entry_len, scc_hash))
1851 continue;
1853 /* Tree merging failed, mark entries in location cache as
1854 permanent. */
1855 data_in->location_cache.accept_location_cache ();
1857 bool seen_type = false;
1858 for (unsigned i = 0; i < len; ++i)
1860 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1861 from + i);
1862 /* Reconstruct the type variant and pointer-to/reference-to
1863 chains. */
1864 if (TYPE_P (t))
1866 /* Map the tree types to their frequencies. */
1867 if (flag_lto_dump_type_stats)
1869 unsigned key = (unsigned) TREE_CODE (t);
1870 unsigned *countp = hm.get (key);
1871 hm.put (key, countp ? (*countp) + 1 : 1);
1872 total++;
1875 seen_type = true;
1876 num_prevailing_types++;
1877 lto_fixup_prevailing_type (t);
1879 /* Compute the canonical type of all non-ODR types.
1880 Delay ODR types for the end of merging process - the canonical
1881 type for those can be computed using the (unique) name however
1882 we want to do this only if units in other languages do not
1883 contain structurally equivalent type.
1885 Because SCC components are streamed in random (hash) order
1886 we may have encountered the type before while registering
1887 type canonical of a derived type in the same SCC. */
1888 if (!TYPE_CANONICAL (t))
1890 if (!RECORD_OR_UNION_TYPE_P (t)
1891 || !TYPE_CXX_ODR_P (t))
1892 gimple_register_canonical_type (t);
1893 else if (COMPLETE_TYPE_P (t))
1894 vec_safe_push (types_to_register, t);
1896 if (TYPE_MAIN_VARIANT (t) == t && odr_type_p (t))
1897 register_odr_type (t);
1899 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1900 type which is also member of this SCC. */
1901 if (TREE_CODE (t) == INTEGER_CST
1902 && !TREE_OVERFLOW (t))
1903 cache_integer_cst (t);
1904 if (!flag_ltrans)
1906 lto_maybe_register_decl (data_in, t, from + i);
1907 /* Scan the tree for references to global functions or
1908 variables and record those for later fixup. */
1909 if (mentions_vars_p (t))
1910 vec_safe_push (tree_with_vars, t);
1914 /* Register DECLs with the debuginfo machinery. */
1915 while (!dref_queue.is_empty ())
1917 dref_entry e = dref_queue.pop ();
1918 debug_hooks->register_external_die (e.decl, e.sym, e.off);
1921 if (seen_type)
1922 num_type_scc_trees += len;
1924 else
1926 /* Pickle stray references. */
1927 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1928 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1932 /* Dump type statistics. */
1933 if (flag_lto_dump_type_stats)
1935 fprintf (stdout, " Type Frequency Percentage\n\n");
1936 for (hash_map<code_id_hash, unsigned>::iterator itr = hm.begin ();
1937 itr != hm.end ();
1938 ++itr)
1940 std::pair<unsigned, unsigned> p = *itr;
1941 enum tree_code code = (enum tree_code) p.first;
1942 fprintf (stdout, "%14s %6d %12.2f\n", get_tree_code_name (code),
1943 p.second, float (p.second)/total*100);
1947 data_in->location_cache.apply_location_cache ();
1949 /* Read in lto_in_decl_state objects. */
1950 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1951 data_end
1952 = (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1953 num_decl_states = *data_ptr++;
1955 gcc_assert (num_decl_states > 0);
1956 decl_data->global_decl_state = lto_new_in_decl_state ();
1957 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1958 decl_data->global_decl_state);
1960 /* Read in per-function decl states and enter them in hash table. */
1961 decl_data->function_decl_states
1962 = hash_table<decl_state_hasher>::create_ggc (37);
1964 for (i = 1; i < num_decl_states; i++)
1966 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1968 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1969 lto_in_decl_state **slot
1970 = decl_data->function_decl_states->find_slot (state, INSERT);
1971 gcc_assert (*slot == NULL);
1972 *slot = state;
1975 if (data_ptr != data_end)
1976 internal_error ("bytecode stream: garbage at the end of symbols section");
1978 /* Set the current decl state to be the global state. */
1979 decl_data->current_decl_state = decl_data->global_decl_state;
1981 lto_data_in_delete (data_in);
1984 /* Custom version of strtoll, which is not portable. */
1986 static int64_t
1987 lto_parse_hex (const char *p)
1989 int64_t ret = 0;
1991 for (; *p != '\0'; ++p)
1993 char c = *p;
1994 unsigned char part;
1995 ret <<= 4;
1996 if (c >= '0' && c <= '9')
1997 part = c - '0';
1998 else if (c >= 'a' && c <= 'f')
1999 part = c - 'a' + 10;
2000 else if (c >= 'A' && c <= 'F')
2001 part = c - 'A' + 10;
2002 else
2003 internal_error ("could not parse hex number");
2004 ret |= part;
2007 return ret;
2010 /* Read resolution for file named FILE_NAME. The resolution is read from
2011 RESOLUTION. */
2013 static void
2014 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2016 /* We require that objects in the resolution file are in the same
2017 order as the lto1 command line. */
2018 unsigned int name_len;
2019 char *obj_name;
2020 unsigned int num_symbols;
2021 unsigned int i;
2022 struct lto_file_decl_data *file_data;
2023 splay_tree_node nd = NULL;
2025 if (!resolution)
2026 return;
2028 name_len = strlen (file->filename);
2029 obj_name = XNEWVEC (char, name_len + 1);
2030 fscanf (resolution, " "); /* Read white space. */
2032 fread (obj_name, sizeof (char), name_len, resolution);
2033 obj_name[name_len] = '\0';
2034 if (filename_cmp (obj_name, file->filename) != 0)
2035 internal_error ("unexpected file name %s in linker resolution file. "
2036 "Expected %s", obj_name, file->filename);
2037 if (file->offset != 0)
2039 int t;
2040 char offset_p[17];
2041 int64_t offset;
2042 t = fscanf (resolution, "@0x%16s", offset_p);
2043 if (t != 1)
2044 internal_error ("could not parse file offset");
2045 offset = lto_parse_hex (offset_p);
2046 if (offset != file->offset)
2047 internal_error ("unexpected offset");
2050 free (obj_name);
2052 fscanf (resolution, "%u", &num_symbols);
2054 for (i = 0; i < num_symbols; i++)
2056 int t;
2057 unsigned index;
2058 unsigned HOST_WIDE_INT id;
2059 char r_str[27];
2060 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2061 unsigned int j;
2062 unsigned int lto_resolution_str_len
2063 = sizeof (lto_resolution_str) / sizeof (char *);
2064 res_pair rp;
2066 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
2067 " %26s %*[^\n]\n", &index, &id, r_str);
2068 if (t != 3)
2069 internal_error ("invalid line in the resolution file");
2071 for (j = 0; j < lto_resolution_str_len; j++)
2073 if (strcmp (lto_resolution_str[j], r_str) == 0)
2075 r = (enum ld_plugin_symbol_resolution) j;
2076 break;
2079 if (j == lto_resolution_str_len)
2080 internal_error ("invalid resolution in the resolution file");
2082 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2084 nd = lto_splay_tree_lookup (file_ids, id);
2085 if (nd == NULL)
2086 internal_error ("resolution sub id %wx not in object file", id);
2089 file_data = (struct lto_file_decl_data *)nd->value;
2090 /* The indexes are very sparse. To save memory save them in a compact
2091 format that is only unpacked later when the subfile is processed. */
2092 rp.res = r;
2093 rp.index = index;
2094 file_data->respairs.safe_push (rp);
2095 if (file_data->max_index < index)
2096 file_data->max_index = index;
2100 /* List of file_decl_datas. */
2101 struct file_data_list
2103 struct lto_file_decl_data *first, *last;
2106 /* Is the name for a id'ed LTO section? */
2108 static int
2109 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2111 const char *s;
2113 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
2114 return 0;
2115 s = strrchr (name, '.');
2116 if (!s)
2117 return 0;
2118 /* If the section is not suffixed with an ID return. */
2119 if ((size_t)(s - name) == strlen (section_name_prefix))
2120 return 0;
2121 return sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2124 /* Create file_data of each sub file id. */
2126 static int
2127 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2128 struct file_data_list *list)
2130 struct lto_section_slot s_slot, *new_slot;
2131 unsigned HOST_WIDE_INT id;
2132 splay_tree_node nd;
2133 void **hash_slot;
2134 char *new_name;
2135 struct lto_file_decl_data *file_data;
2137 if (!lto_section_with_id (ls->name, &id))
2138 return 1;
2140 /* Find hash table of sub module id. */
2141 nd = lto_splay_tree_lookup (file_ids, id);
2142 if (nd != NULL)
2144 file_data = (struct lto_file_decl_data *)nd->value;
2146 else
2148 file_data = ggc_alloc<lto_file_decl_data> ();
2149 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2150 file_data->id = id;
2151 file_data->section_hash_table = lto_obj_create_section_hash_table ();
2152 lto_splay_tree_insert (file_ids, id, file_data);
2154 /* Maintain list in linker order. */
2155 if (!list->first)
2156 list->first = file_data;
2157 if (list->last)
2158 list->last->next = file_data;
2160 list->last = file_data;
2163 /* Copy section into sub module hash table. */
2164 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2165 s_slot.name = new_name;
2166 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2167 gcc_assert (*hash_slot == NULL);
2169 new_slot = XDUP (struct lto_section_slot, ls);
2170 new_slot->name = new_name;
2171 *hash_slot = new_slot;
2172 return 1;
2175 /* Read declarations and other initializations for a FILE_DATA. */
2177 static void
2178 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2180 const char *data;
2181 size_t len;
2182 vec<ld_plugin_symbol_resolution_t>
2183 resolutions = vNULL;
2184 int i;
2185 res_pair *rp;
2187 /* Create vector for fast access of resolution. We do this lazily
2188 to save memory. */
2189 resolutions.safe_grow_cleared (file_data->max_index + 1);
2190 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2191 resolutions[rp->index] = rp->res;
2192 file_data->respairs.release ();
2194 file_data->renaming_hash_table = lto_create_renaming_table ();
2195 file_data->file_name = file->filename;
2196 #ifdef ACCEL_COMPILER
2197 lto_input_mode_table (file_data);
2198 #else
2199 file_data->mode_table = lto_mode_identity_table;
2200 #endif
2202 /* Read and verify LTO section. */
2203 data = lto_get_section_data (file_data, LTO_section_lto, NULL, &len, false);
2204 if (data == NULL)
2206 fatal_error (input_location, "bytecode stream in file %qs generated "
2207 "with GCC compiler older than 10.0", file_data->file_name);
2208 return;
2211 memcpy (&file_data->lto_section_header, data, sizeof (lto_section));
2212 lto_check_version (file_data->lto_section_header.major_version,
2213 file_data->lto_section_header.minor_version,
2214 file_data->file_name);
2216 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2217 if (data == NULL)
2219 internal_error ("cannot read %<LTO_section_decls%> from %s",
2220 file_data->file_name);
2221 return;
2223 /* Frees resolutions. */
2224 lto_read_decls (file_data, data, resolutions);
2225 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2228 /* Finalize FILE_DATA in FILE and increase COUNT. */
2230 static int
2231 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2232 int *count)
2234 lto_file_finalize (file_data, file);
2235 if (symtab->dump_file)
2236 fprintf (symtab->dump_file,
2237 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2238 file_data->file_name, file_data->id);
2239 (*count)++;
2240 return 0;
2243 /* Generate a TREE representation for all types and external decls
2244 entities in FILE.
2246 Read all of the globals out of the file. Then read the cgraph
2247 and process the .o index into the cgraph nodes so that it can open
2248 the .o file to load the functions and ipa information. */
2250 static struct lto_file_decl_data *
2251 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2253 struct lto_file_decl_data *file_data = NULL;
2254 splay_tree file_ids;
2255 htab_t section_hash_table;
2256 struct lto_section_slot *section;
2257 struct file_data_list file_list;
2258 struct lto_section_list section_list;
2260 memset (&section_list, 0, sizeof (struct lto_section_list));
2261 section_hash_table = lto_obj_build_section_table (file, &section_list);
2263 /* Dump the details of LTO objects. */
2264 if (flag_lto_dump_objects)
2266 int i=0;
2267 fprintf (stdout, "\n LTO Object Name: %s\n", file->filename);
2268 fprintf (stdout, "\nNo. Offset Size Section Name\n\n");
2269 for (section = section_list.first; section != NULL; section = section->next)
2270 fprintf (stdout, "%2d %8" PRId64 " %8" PRIu64 " %s\n",
2271 ++i, (int64_t) section->start, (uint64_t) section->len,
2272 section->name);
2275 /* Find all sub modules in the object and put their sections into new hash
2276 tables in a splay tree. */
2277 file_ids = lto_splay_tree_new ();
2278 memset (&file_list, 0, sizeof (struct file_data_list));
2279 for (section = section_list.first; section != NULL; section = section->next)
2280 create_subid_section_table (section, file_ids, &file_list);
2282 /* Add resolutions to file ids. */
2283 lto_resolution_read (file_ids, resolution_file, file);
2285 /* Finalize each lto file for each submodule in the merged object. */
2286 for (file_data = file_list.first; file_data != NULL;
2287 file_data = file_data->next)
2288 lto_create_files_from_ids (file, file_data, count);
2290 splay_tree_delete (file_ids);
2291 htab_delete (section_hash_table);
2293 return file_list.first;
2296 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2297 #define LTO_MMAP_IO 1
2298 #endif
2300 #if LTO_MMAP_IO
2301 /* Page size of machine is used for mmap and munmap calls. */
2302 static size_t page_mask;
2303 #endif
2305 /* Get the section data of length LEN from FILENAME starting at
2306 OFFSET. The data segment must be freed by the caller when the
2307 caller is finished. Returns NULL if all was not well. */
2309 static char *
2310 lto_read_section_data (struct lto_file_decl_data *file_data,
2311 intptr_t offset, size_t len)
2313 char *result;
2314 static int fd = -1;
2315 static char *fd_name;
2316 #if LTO_MMAP_IO
2317 intptr_t computed_len;
2318 intptr_t computed_offset;
2319 intptr_t diff;
2320 #endif
2322 /* Keep a single-entry file-descriptor cache. The last file we
2323 touched will get closed at exit.
2324 ??? Eventually we want to add a more sophisticated larger cache
2325 or rather fix function body streaming to not stream them in
2326 practically random order. */
2327 if (fd != -1
2328 && filename_cmp (fd_name, file_data->file_name) != 0)
2330 free (fd_name);
2331 close (fd);
2332 fd = -1;
2334 if (fd == -1)
2336 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2337 if (fd == -1)
2339 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2340 return NULL;
2342 fd_name = xstrdup (file_data->file_name);
2345 #if LTO_MMAP_IO
2346 if (!page_mask)
2348 size_t page_size = sysconf (_SC_PAGE_SIZE);
2349 page_mask = ~(page_size - 1);
2352 computed_offset = offset & page_mask;
2353 diff = offset - computed_offset;
2354 computed_len = len + diff;
2356 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2357 fd, computed_offset);
2358 if (result == MAP_FAILED)
2360 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2361 return NULL;
2364 return result + diff;
2365 #else
2366 result = (char *) xmalloc (len);
2367 if (lseek (fd, offset, SEEK_SET) != offset
2368 || read (fd, result, len) != (ssize_t) len)
2370 free (result);
2371 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2372 result = NULL;
2374 #ifdef __MINGW32__
2375 /* Native windows doesn't supports delayed unlink on opened file. So
2376 we close file here again. This produces higher I/O load, but at least
2377 it prevents to have dangling file handles preventing unlink. */
2378 free (fd_name);
2379 fd_name = NULL;
2380 close (fd);
2381 fd = -1;
2382 #endif
2383 return result;
2384 #endif
2388 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2389 NAME will be NULL unless the section type is for a function
2390 body. */
2392 static const char *
2393 get_section_data (struct lto_file_decl_data *file_data,
2394 enum lto_section_type section_type,
2395 const char *name,
2396 size_t *len)
2398 htab_t section_hash_table = file_data->section_hash_table;
2399 struct lto_section_slot *f_slot;
2400 struct lto_section_slot s_slot;
2401 const char *section_name = lto_get_section_name (section_type, name,
2402 file_data);
2403 char *data = NULL;
2405 *len = 0;
2406 s_slot.name = section_name;
2407 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2408 if (f_slot)
2410 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2411 *len = f_slot->len;
2414 free (CONST_CAST (char *, section_name));
2415 return data;
2419 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2420 starts at OFFSET and has LEN bytes. */
2422 static void
2423 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2424 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2425 const char *name ATTRIBUTE_UNUSED,
2426 const char *offset, size_t len ATTRIBUTE_UNUSED)
2428 #if LTO_MMAP_IO
2429 intptr_t computed_len;
2430 intptr_t computed_offset;
2431 intptr_t diff;
2432 #endif
2434 #if LTO_MMAP_IO
2435 computed_offset = ((intptr_t) offset) & page_mask;
2436 diff = (intptr_t) offset - computed_offset;
2437 computed_len = len + diff;
2439 munmap ((caddr_t) computed_offset, computed_len);
2440 #else
2441 free (CONST_CAST(char *, offset));
2442 #endif
2445 static lto_file *current_lto_file;
2447 /* If TT is a variable or function decl replace it with its
2448 prevailing variant. */
2449 #define LTO_SET_PREVAIL(tt) \
2450 do {\
2451 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2452 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2454 tt = lto_symtab_prevailing_decl (tt); \
2455 fixed = true; \
2457 } while (0)
2459 /* Ensure that TT isn't a replacable var of function decl. */
2460 #define LTO_NO_PREVAIL(tt) \
2461 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2463 /* Given a tree T replace all fields referring to variables or functions
2464 with their prevailing variant. */
2465 static void
2466 lto_fixup_prevailing_decls (tree t)
2468 enum tree_code code = TREE_CODE (t);
2469 bool fixed = false;
2471 gcc_checking_assert (code != TREE_BINFO);
2472 LTO_NO_PREVAIL (TREE_TYPE (t));
2473 if (CODE_CONTAINS_STRUCT (code, TS_COMMON)
2474 /* lto_symtab_prevail_decl use TREE_CHAIN to link to the prevailing decl.
2475 in the case T is a prevailed declaration we would ICE here. */
2476 && !VAR_OR_FUNCTION_DECL_P (t))
2477 LTO_NO_PREVAIL (TREE_CHAIN (t));
2478 if (DECL_P (t))
2480 LTO_NO_PREVAIL (DECL_NAME (t));
2481 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2482 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2484 LTO_SET_PREVAIL (DECL_SIZE (t));
2485 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2486 LTO_SET_PREVAIL (DECL_INITIAL (t));
2487 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2488 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2490 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2492 LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t));
2494 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2496 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2498 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2500 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2501 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2502 LTO_NO_PREVAIL (DECL_VINDEX (t));
2504 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2506 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2507 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2508 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2509 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2510 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2513 else if (TYPE_P (t))
2515 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2516 LTO_SET_PREVAIL (TYPE_SIZE (t));
2517 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2518 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2519 LTO_NO_PREVAIL (TYPE_NAME (t));
2521 LTO_SET_PREVAIL (TYPE_MIN_VALUE_RAW (t));
2522 LTO_SET_PREVAIL (TYPE_MAX_VALUE_RAW (t));
2523 LTO_NO_PREVAIL (TYPE_LANG_SLOT_1 (t));
2525 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2527 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2528 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2529 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2531 else if (EXPR_P (t))
2533 int i;
2534 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2535 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2537 else if (TREE_CODE (t) == CONSTRUCTOR)
2539 unsigned i;
2540 tree val;
2541 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2542 LTO_SET_PREVAIL (val);
2544 else
2546 switch (code)
2548 case TREE_LIST:
2549 LTO_SET_PREVAIL (TREE_VALUE (t));
2550 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2551 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2552 break;
2553 default:
2554 gcc_unreachable ();
2557 /* If we fixed nothing, then we missed something seen by
2558 mentions_vars_p. */
2559 gcc_checking_assert (fixed);
2561 #undef LTO_SET_PREVAIL
2562 #undef LTO_NO_PREVAIL
2564 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2565 replaces var and function decls with the corresponding prevailing def. */
2567 static void
2568 lto_fixup_state (struct lto_in_decl_state *state)
2570 unsigned i, si;
2572 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2573 we still need to walk from all DECLs to find the reachable
2574 FUNCTION_DECLs and VAR_DECLs. */
2575 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2577 vec<tree, va_gc> *trees = state->streams[si];
2578 for (i = 0; i < vec_safe_length (trees); i++)
2580 tree t = (*trees)[i];
2581 if (flag_checking && TYPE_P (t))
2582 verify_type (t);
2583 if (VAR_OR_FUNCTION_DECL_P (t)
2584 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2585 (*trees)[i] = lto_symtab_prevailing_decl (t);
2590 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2591 prevailing one. */
2593 static void
2594 lto_fixup_decls (struct lto_file_decl_data **files)
2596 unsigned int i;
2597 tree t;
2599 if (tree_with_vars)
2600 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2601 lto_fixup_prevailing_decls (t);
2603 for (i = 0; files[i]; i++)
2605 struct lto_file_decl_data *file = files[i];
2606 struct lto_in_decl_state *state = file->global_decl_state;
2607 lto_fixup_state (state);
2609 hash_table<decl_state_hasher>::iterator iter;
2610 lto_in_decl_state *elt;
2611 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2612 lto_in_decl_state *, iter)
2613 lto_fixup_state (elt);
2617 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2619 /* Turn file datas for sub files into a single array, so that they look
2620 like separate files for further passes. */
2622 static void
2623 lto_flatten_files (struct lto_file_decl_data **orig, int count,
2624 int last_file_ix)
2626 struct lto_file_decl_data *n, *next;
2627 int i, k;
2629 lto_stats.num_input_files = count;
2630 all_file_decl_data
2631 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2632 /* Set the hooks so that all of the ipa passes can read in their data. */
2633 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2634 for (i = 0, k = 0; i < last_file_ix; i++)
2636 for (n = orig[i]; n != NULL; n = next)
2638 all_file_decl_data[k++] = n;
2639 next = n->next;
2640 n->next = NULL;
2643 all_file_decl_data[k] = NULL;
2644 gcc_assert (k == count);
2647 /* Input file data before flattening (i.e. splitting them to subfiles to support
2648 incremental linking. */
2649 static int real_file_count;
2650 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2652 /* Read all the symbols from the input files FNAMES. NFILES is the
2653 number of files requested in the command line. Instantiate a
2654 global call graph by aggregating all the sub-graphs found in each
2655 file. */
2657 void
2658 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2660 unsigned int i, last_file_ix;
2661 FILE *resolution;
2662 int count = 0;
2663 struct lto_file_decl_data **decl_data;
2664 symtab_node *snode;
2666 symtab->initialize ();
2668 timevar_push (TV_IPA_LTO_DECL_IN);
2670 #ifdef ACCEL_COMPILER
2671 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2672 lto_stream_offload_p = true;
2673 #endif
2675 real_file_decl_data
2676 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2677 real_file_count = nfiles;
2679 /* Read the resolution file. */
2680 resolution = NULL;
2681 if (resolution_file_name)
2683 int t;
2684 unsigned num_objects;
2686 resolution = fopen (resolution_file_name, "r");
2687 if (resolution == NULL)
2688 fatal_error (input_location,
2689 "could not open symbol resolution file: %m");
2691 t = fscanf (resolution, "%u", &num_objects);
2692 gcc_assert (t == 1);
2694 /* True, since the plugin splits the archives. */
2695 gcc_assert (num_objects == nfiles);
2697 symtab->state = LTO_STREAMING;
2699 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2700 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2701 gimple_canonical_type_eq, NULL);
2702 gcc_obstack_init (&tree_scc_hash_obstack);
2703 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2705 /* Register the common node types with the canonical type machinery so
2706 we properly share alias-sets across languages and TUs. Do not
2707 expose the common nodes as type merge target - those that should be
2708 are already exposed so by pre-loading the LTO streamer caches.
2709 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2710 for (i = 0; i < itk_none; ++i)
2711 lto_register_canonical_types (integer_types[i], true);
2712 for (i = 0; i < stk_type_kind_last; ++i)
2713 lto_register_canonical_types (sizetype_tab[i], true);
2714 for (i = 0; i < TI_MAX; ++i)
2715 lto_register_canonical_types (global_trees[i], true);
2716 for (i = 0; i < itk_none; ++i)
2717 lto_register_canonical_types (integer_types[i], false);
2718 for (i = 0; i < stk_type_kind_last; ++i)
2719 lto_register_canonical_types (sizetype_tab[i], false);
2720 for (i = 0; i < TI_MAX; ++i)
2721 lto_register_canonical_types (global_trees[i], false);
2723 if (!quiet_flag)
2724 fprintf (stderr, "Reading object files:");
2726 /* Read all of the object files specified on the command line. */
2727 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2729 struct lto_file_decl_data *file_data = NULL;
2730 if (!quiet_flag)
2732 fprintf (stderr, " %s", fnames[i]);
2733 fflush (stderr);
2736 current_lto_file = lto_obj_file_open (fnames[i], false);
2737 if (!current_lto_file)
2738 break;
2740 file_data = lto_file_read (current_lto_file, resolution, &count);
2741 if (!file_data)
2743 lto_obj_file_close (current_lto_file);
2744 free (current_lto_file);
2745 current_lto_file = NULL;
2746 break;
2749 decl_data[last_file_ix++] = file_data;
2751 lto_obj_file_close (current_lto_file);
2752 free (current_lto_file);
2753 current_lto_file = NULL;
2756 lto_flatten_files (decl_data, count, last_file_ix);
2757 lto_stats.num_input_files = count;
2758 ggc_free(decl_data);
2759 real_file_decl_data = NULL;
2761 lto_register_canonical_types_for_odr_types ();
2763 if (resolution_file_name)
2764 fclose (resolution);
2766 /* Show the LTO report before launching LTRANS. */
2767 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2768 print_lto_report_1 ();
2770 /* Free gimple type merging datastructures. */
2771 delete tree_scc_hash;
2772 tree_scc_hash = NULL;
2773 obstack_free (&tree_scc_hash_obstack, NULL);
2774 htab_delete (gimple_canonical_types);
2775 gimple_canonical_types = NULL;
2776 delete canonical_type_hash_cache;
2777 canonical_type_hash_cache = NULL;
2779 /* At this stage we know that majority of GGC memory is reachable.
2780 Growing the limits prevents unnecesary invocation of GGC. */
2781 ggc_grow ();
2782 ggc_collect ();
2784 /* Set the hooks so that all of the ipa passes can read in their data. */
2785 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2787 timevar_pop (TV_IPA_LTO_DECL_IN);
2789 if (!quiet_flag)
2790 fprintf (stderr, "\nReading the callgraph\n");
2792 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2793 /* Read the symtab. */
2794 input_symtab ();
2796 input_offload_tables (!flag_ltrans);
2798 /* Store resolutions into the symbol table. */
2800 FOR_EACH_SYMBOL (snode)
2801 if (snode->externally_visible && snode->real_symbol_p ()
2802 && snode->lto_file_data && snode->lto_file_data->resolution_map
2803 && !(TREE_CODE (snode->decl) == FUNCTION_DECL
2804 && fndecl_built_in_p (snode->decl))
2805 && !(VAR_P (snode->decl) && DECL_HARD_REGISTER (snode->decl)))
2807 ld_plugin_symbol_resolution_t *res;
2809 res = snode->lto_file_data->resolution_map->get (snode->decl);
2810 if (!res || *res == LDPR_UNKNOWN)
2812 if (snode->output_to_lto_symbol_table_p ())
2813 fatal_error (input_location, "missing resolution data for %s",
2814 IDENTIFIER_POINTER
2815 (DECL_ASSEMBLER_NAME (snode->decl)));
2817 else
2818 snode->resolution = *res;
2820 for (i = 0; all_file_decl_data[i]; i++)
2821 if (all_file_decl_data[i]->resolution_map)
2823 delete all_file_decl_data[i]->resolution_map;
2824 all_file_decl_data[i]->resolution_map = NULL;
2827 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2829 if (!quiet_flag)
2830 fprintf (stderr, "Merging declarations\n");
2832 timevar_push (TV_IPA_LTO_DECL_MERGE);
2833 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2834 need to care about resolving symbols again, we only need to replace
2835 duplicated declarations read from the callgraph and from function
2836 sections. */
2837 if (!flag_ltrans)
2839 lto_symtab_merge_decls ();
2841 /* If there were errors during symbol merging bail out, we have no
2842 good way to recover here. */
2843 if (seen_error ())
2844 fatal_error (input_location,
2845 "errors during merging of translation units");
2847 /* Fixup all decls. */
2848 lto_fixup_decls (all_file_decl_data);
2850 if (tree_with_vars)
2851 ggc_free (tree_with_vars);
2852 tree_with_vars = NULL;
2853 ggc_collect ();
2855 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2856 /* Each pass will set the appropriate timer. */
2858 if (!quiet_flag)
2859 fprintf (stderr, "Reading summaries\n");
2861 /* Read the IPA summary data. */
2862 if (flag_ltrans)
2863 ipa_read_optimization_summaries ();
2864 else
2865 ipa_read_summaries ();
2867 for (i = 0; all_file_decl_data[i]; i++)
2869 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2870 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2871 all_file_decl_data[i]->symtab_node_encoder = NULL;
2872 lto_in_decl_state *global_decl_state
2873 = all_file_decl_data[i]->global_decl_state;
2874 lto_free_function_in_decl_state (global_decl_state);
2875 all_file_decl_data[i]->global_decl_state = NULL;
2876 all_file_decl_data[i]->current_decl_state = NULL;
2879 if (!flag_ltrans)
2881 /* Finally merge the cgraph according to the decl merging decisions. */
2882 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2884 gcc_assert (!dump_file);
2885 dump_file = dump_begin (lto_link_dump_id, NULL);
2887 if (dump_file)
2889 fprintf (dump_file, "Before merging:\n");
2890 symtab->dump (dump_file);
2892 lto_symtab_merge_symbols ();
2893 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2894 we are still having duplicated comdat groups containing local statics.
2895 We could also just remove them while merging. */
2896 symtab->remove_unreachable_nodes (dump_file);
2897 ggc_collect ();
2899 if (dump_file)
2900 dump_end (lto_link_dump_id, dump_file);
2901 dump_file = NULL;
2902 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2904 symtab->state = IPA_SSA;
2905 /* All node removals happening here are useless, because
2906 WPA should not stream them. Still always perform remove_unreachable_nodes
2907 because we may reshape clone tree, get rid of dead masters of inline
2908 clones and remove symbol entries for read-only variables we keep around
2909 only to be able to constant fold them. */
2910 if (flag_ltrans)
2912 if (symtab->dump_file)
2913 symtab->dump (symtab->dump_file);
2914 symtab->remove_unreachable_nodes (symtab->dump_file);
2917 /* Indicate that the cgraph is built and ready. */
2918 symtab->function_flags_ready = true;
2920 ggc_free (all_file_decl_data);
2921 all_file_decl_data = NULL;
2926 /* Show various memory usage statistics related to LTO. */
2927 void
2928 print_lto_report_1 (void)
2930 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
2931 fprintf (stderr, "%s statistics\n", pfx);
2933 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
2934 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
2935 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
2936 if (flag_wpa && tree_scc_hash)
2938 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
2939 "collision ratio: %f\n", pfx,
2940 (long) tree_scc_hash->size (),
2941 (long) tree_scc_hash->elements (),
2942 tree_scc_hash->collisions ());
2943 hash_table<tree_scc_hasher>::iterator hiter;
2944 tree_scc *scc, *max_scc = NULL;
2945 unsigned max_length = 0;
2946 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
2948 unsigned length = 0;
2949 tree_scc *s = scc;
2950 for (; s; s = s->next)
2951 length++;
2952 if (length > max_length)
2954 max_length = length;
2955 max_scc = scc;
2958 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
2959 pfx, max_length, max_scc->len);
2960 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
2961 num_scc_compares, num_scc_compare_collisions,
2962 num_scc_compare_collisions / (double) num_scc_compares);
2963 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
2964 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
2965 total_scc_size_merged);
2966 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
2967 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
2968 pfx, num_prevailing_types, num_type_scc_trees);
2969 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
2970 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
2971 (long) htab_size (gimple_canonical_types),
2972 (long) htab_elements (gimple_canonical_types),
2973 (long) gimple_canonical_types->searches,
2974 (long) gimple_canonical_types->collisions,
2975 htab_collisions (gimple_canonical_types));
2976 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
2977 "%lu elements, %ld searches\n", pfx,
2978 num_canonical_type_hash_entries,
2979 num_canonical_type_hash_queries);
2982 print_lto_report (pfx);
2985 GTY(()) tree lto_eh_personality_decl;
2987 /* Return the LTO personality function decl. */
2989 tree
2990 lto_eh_personality (void)
2992 if (!lto_eh_personality_decl)
2994 /* Use the first personality DECL for our personality if we don't
2995 support multiple ones. This ensures that we don't artificially
2996 create the need for them in a single-language program. */
2997 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2998 lto_eh_personality_decl = first_personality_decl;
2999 else
3000 lto_eh_personality_decl = lhd_gcc_personality ();
3003 return lto_eh_personality_decl;
3006 /* Set the process name based on the LTO mode. */
3008 static void
3009 lto_process_name (void)
3011 if (flag_lto)
3012 setproctitle (flag_incremental_link == INCREMENTAL_LINK_LTO
3013 ? "lto1-inclink" : "lto1-lto");
3014 if (flag_wpa)
3015 setproctitle ("lto1-wpa");
3016 if (flag_ltrans)
3017 setproctitle ("lto1-ltrans");
3021 /* Initialize the LTO front end. */
3023 void
3024 lto_fe_init (void)
3026 lto_process_name ();
3027 lto_streamer_hooks_init ();
3028 lto_reader_init ();
3029 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3030 memset (&lto_stats, 0, sizeof (lto_stats));
3031 bitmap_obstack_initialize (NULL);
3032 gimple_register_cfg_hooks ();
3033 #ifndef ACCEL_COMPILER
3034 unsigned char *table
3035 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3036 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3037 table[m] = m;
3038 lto_mode_identity_table = table;
3039 #endif
3042 #include "gt-lto-lto-common.h"