1 /* Top-level LTO routines.
2 Copyright (C) 2009-2020 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
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
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/>. */
23 #include "coretypes.h"
27 #include "basic-block.h"
31 #include "alloc-pool.h"
32 #include "tree-pass.h"
33 #include "tree-streamer.h"
37 #include "stor-layout.h"
38 #include "symbol-summary.h"
44 #include "lto-section-names.h"
45 #include "splay-tree.h"
46 #include "lto-partition.h"
48 #include "pass_manager.h"
49 #include "ipa-fnsummary.h"
50 #include "ipa-utils.h"
51 #include "gomp-constants.h"
52 #include "lto-symtab.h"
53 #include "stringpool.h"
54 #include "fold-const.h"
57 #include "lto-common.h"
58 #include "tree-pretty-print.h"
59 #include "print-tree.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. */
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. */
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. */
95 free_with_string (void *arg
)
97 struct lto_section_slot
*s
= (struct lto_section_slot
*)arg
;
99 free (CONST_CAST (char *, s
->name
));
103 /* Create section hash table. */
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. */
114 lto_splay_tree_delete_id (splay_tree_key key
)
119 /* Compare splay tree node ids A and B. */
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
;
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. */
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. */
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
);
163 splay_tree_insert (t
, (splay_tree_key
) idp
, (splay_tree_value
) file_data
);
166 /* Create a splay tree. */
169 lto_splay_tree_new (void)
171 return splay_tree_new (lto_splay_tree_compare_ids
,
172 lto_splay_tree_delete_id
,
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
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
)
190 state
->compressed
= ix
& 1;
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
);
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
,
211 state
->streams
[i
] = decls
;
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. */
240 hash_canonical_type (tree type
)
242 inchash::hash hstate
;
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
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
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
)
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
);
325 if (RECORD_OR_UNION_TYPE_P (type
))
330 for (f
= TYPE_FIELDS (type
), nf
= 0; f
; f
= TREE_CHAIN (f
))
331 if (TREE_CODE (f
) == FIELD_DECL
333 || ! integer_zerop (DECL_SIZE (f
))))
335 iterative_hash_canonical_type (TREE_TYPE (f
), hstate
);
345 /* Returning a hash value for gimple type TYPE combined with VAL. */
348 iterative_hash_canonical_type (tree type
, inchash::hash
&hstate
)
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
);
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
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. */
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
);
388 /* Returns nonzero if P1 and P2 are equal. */
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. */
402 gimple_register_canonical_type_1 (tree t
, hashval_t hash
)
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
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
))
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
,
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",
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
);
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",
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
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
);
485 slot
= htab_find_slot_with_hash (gimple_canonical_types
, t
, hash
, INSERT
);
488 tree new_type
= (tree
)(*slot
);
489 gcc_checking_assert (new_type
!= t
);
490 TYPE_CANONICAL (t
) = new_type
;
494 TYPE_CANONICAL (t
) = 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
);
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
515 gimple_register_canonical_type (tree t
)
517 if (TYPE_CANONICAL (t
) || !type_with_alias_set_p (t
)
518 || !canonical_type_used_p (t
))
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
));
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. */
535 lto_register_canonical_types (tree node
, bool 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
);
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. */
559 lto_register_canonical_types_for_odr_types ()
564 if (!types_to_register
)
567 type_streaming_finished
= true;
569 /* Be sure that no types derived from ODR types was
570 not inserted into the hash table. */
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) \
597 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
598 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
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. */
608 mentions_vars_p_typed (tree t
)
610 CHECK_NO_VAR (TREE_TYPE (t
));
614 /* Check presence of pointers to decls in fields of a tree_common T. */
617 mentions_vars_p_common (tree t
)
619 if (mentions_vars_p_typed (t
))
621 CHECK_NO_VAR (TREE_CHAIN (t
));
625 /* Check presence of pointers to decls in fields of a decl_minimal T. */
628 mentions_vars_p_decl_minimal (tree t
)
630 if (mentions_vars_p_common (t
))
632 CHECK_NO_VAR (DECL_NAME (t
));
633 CHECK_VAR (DECL_CONTEXT (t
));
637 /* Check presence of pointers to decls in fields of a decl_common T. */
640 mentions_vars_p_decl_common (tree t
)
642 if (mentions_vars_p_decl_minimal (t
))
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
));
652 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
655 mentions_vars_p_decl_with_vis (tree t
)
657 if (mentions_vars_p_decl_common (t
))
660 /* Accessor macro has side-effects, use field-name here. */
661 CHECK_NO_VAR (DECL_ASSEMBLER_NAME_RAW (t
));
665 /* Check presence of pointers to decls in fields of a decl_non_common T. */
668 mentions_vars_p_decl_non_common (tree t
)
670 if (mentions_vars_p_decl_with_vis (t
))
672 CHECK_NO_VAR (DECL_RESULT_FLD (t
));
676 /* Check presence of pointers to decls in fields of a decl_non_common T. */
679 mentions_vars_p_function (tree t
)
681 if (mentions_vars_p_decl_non_common (t
))
683 CHECK_NO_VAR (DECL_ARGUMENTS (t
));
684 CHECK_NO_VAR (DECL_VINDEX (t
));
685 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t
));
689 /* Check presence of pointers to decls in fields of a field_decl T. */
692 mentions_vars_p_field_decl (tree t
)
694 if (mentions_vars_p_decl_common (t
))
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
));
704 /* Check presence of pointers to decls in fields of a type T. */
707 mentions_vars_p_type (tree t
)
709 if (mentions_vars_p_common (t
))
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
));
730 /* Check presence of pointers to decls in fields of a BINFO T. */
733 mentions_vars_p_binfo (tree t
)
735 unsigned HOST_WIDE_INT i
, n
;
737 if (mentions_vars_p_common (t
))
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
));
754 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
757 mentions_vars_p_constructor (tree t
)
759 unsigned HOST_WIDE_INT idx
;
762 if (mentions_vars_p_typed (t
))
765 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
767 CHECK_NO_VAR (ce
->index
);
768 CHECK_VAR (ce
->value
);
773 /* Check presence of pointers to decls in fields of an expression tree T. */
776 mentions_vars_p_expr (tree t
)
779 if (mentions_vars_p_typed (t
))
781 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
782 CHECK_VAR (TREE_OPERAND (t
, i
));
786 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
789 mentions_vars_p_omp_clause (tree t
)
792 if (mentions_vars_p_common (t
))
794 for (i
= omp_clause_num_ops
[OMP_CLAUSE_CODE (t
)] - 1; i
>= 0; --i
)
795 CHECK_VAR (OMP_CLAUSE_OPERAND (t
, i
));
799 /* Check presence of pointers to decls that needs later fixup in T. */
802 mentions_vars_p (tree t
)
804 switch (TREE_CODE (t
))
806 case IDENTIFIER_NODE
:
810 CHECK_VAR (TREE_VALUE (t
));
811 CHECK_VAR (TREE_PURPOSE (t
));
812 CHECK_NO_VAR (TREE_CHAIN (t
));
816 return mentions_vars_p_field_decl (t
);
825 return mentions_vars_p_decl_common (t
);
828 return mentions_vars_p_decl_with_vis (t
);
831 return mentions_vars_p_decl_non_common (t
);
834 return mentions_vars_p_function (t
);
837 return mentions_vars_p_binfo (t
);
839 case PLACEHOLDER_EXPR
:
840 return mentions_vars_p_common (t
);
843 case TRANSLATION_UNIT_DECL
:
844 case OPTIMIZATION_NODE
:
845 case TARGET_OPTION_NODE
:
849 return mentions_vars_p_constructor (t
);
852 return mentions_vars_p_omp_clause (t
);
857 if (mentions_vars_p_type (t
))
862 if (mentions_vars_p_expr (t
))
865 else if (CONSTANT_CLASS_P (t
))
866 CHECK_NO_VAR (TREE_TYPE (t
));
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
)
887 ret
= data_in
->globals_resolution
[index
];
891 /* Delay resolution finding until decl merging. */
895 /* We need to record resolutions until symbol table is read. */
897 register_resolution (struct lto_file_decl_data
*file_data
, tree decl
,
898 enum ld_plugin_symbol_resolution resolution
)
901 if (resolution
== LDPR_UNKNOWN
)
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
);
909 || resolution
== LDPR_PREVAILING_DEF_IRONLY
910 || resolution
== LDPR_PREVAILING_DEF
911 || resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
915 /* Register DECL with the global symbol table and change its
916 name if necessary to avoid name clashes for static globals across
920 lto_register_var_decl_in_symtab (class data_in
*data_in
, tree decl
,
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
945 lto_register_function_decl_in_symtab (class data_in
*data_in
, tree decl
,
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. */
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. */
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
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. */
1012 /* Hash of the whole SCC. */
1014 /* Number of trees in the SCC. */
1016 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1017 which share the same individual tree hash). */
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
1023 ??? For prevailing SCCs we really only need hash and the first
1024 entry candidate, but that's too awkward to implement. */
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
*);
1035 tree_scc_hasher::hash (const tree_scc
*scc
)
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
)
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 num_unshared_trees_read
;
1059 static unsigned long total_scc_size_merged
;
1060 static unsigned long num_sccs_merged
;
1061 static unsigned long num_scc_compares
;
1062 static unsigned long num_scc_compare_collisions
;
1065 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1066 recursing through in-SCC tree edges. Returns true if the SCCs entered
1067 through T1 and T2 are equal and fills in *MAP with the pairs of
1068 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1071 compare_tree_sccs_1 (tree t1
, tree t2
, tree
**map
)
1073 enum tree_code code
;
1075 /* Mark already visited nodes. */
1076 TREE_ASM_WRITTEN (t2
) = 1;
1078 /* Push the pair onto map. */
1083 /* Compare value-fields. */
1084 #define compare_values(X) \
1086 if (X(t1) != X(t2)) \
1090 compare_values (TREE_CODE
);
1091 code
= TREE_CODE (t1
);
1093 /* If we end up comparing translation unit decls we either forgot to mark
1094 some SCC as local or we compare too much. */
1095 gcc_checking_assert (code
!= TRANSLATION_UNIT_DECL
);
1099 compare_values (TREE_SIDE_EFFECTS
);
1100 compare_values (TREE_CONSTANT
);
1101 compare_values (TREE_READONLY
);
1102 compare_values (TREE_PUBLIC
);
1104 compare_values (TREE_ADDRESSABLE
);
1105 compare_values (TREE_THIS_VOLATILE
);
1107 compare_values (DECL_UNSIGNED
);
1108 else if (TYPE_P (t1
))
1109 compare_values (TYPE_UNSIGNED
);
1111 compare_values (TYPE_ARTIFICIAL
);
1113 compare_values (TREE_NO_WARNING
);
1114 compare_values (TREE_NOTHROW
);
1115 compare_values (TREE_STATIC
);
1116 if (code
!= TREE_BINFO
)
1117 compare_values (TREE_PRIVATE
);
1118 compare_values (TREE_PROTECTED
);
1119 compare_values (TREE_DEPRECATED
);
1122 if (AGGREGATE_TYPE_P (t1
))
1123 compare_values (TYPE_REVERSE_STORAGE_ORDER
);
1125 compare_values (TYPE_SATURATING
);
1126 compare_values (TYPE_ADDR_SPACE
);
1128 else if (code
== SSA_NAME
)
1129 compare_values (SSA_NAME_IS_DEFAULT_DEF
);
1131 if (CODE_CONTAINS_STRUCT (code
, TS_INT_CST
))
1133 if (wi::to_wide (t1
) != wi::to_wide (t2
))
1137 if (CODE_CONTAINS_STRUCT (code
, TS_REAL_CST
))
1139 /* ??? No suitable compare routine available. */
1140 REAL_VALUE_TYPE r1
= TREE_REAL_CST (t1
);
1141 REAL_VALUE_TYPE r2
= TREE_REAL_CST (t2
);
1143 || r1
.decimal
!= r2
.decimal
1144 || r1
.sign
!= r2
.sign
1145 || r1
.signalling
!= r2
.signalling
1146 || r1
.canonical
!= r2
.canonical
1147 || r1
.uexp
!= r2
.uexp
)
1149 for (unsigned i
= 0; i
< SIGSZ
; ++i
)
1150 if (r1
.sig
[i
] != r2
.sig
[i
])
1154 if (CODE_CONTAINS_STRUCT (code
, TS_FIXED_CST
))
1155 if (!fixed_compare (EQ_EXPR
,
1156 TREE_FIXED_CST_PTR (t1
), TREE_FIXED_CST_PTR (t2
)))
1159 if (CODE_CONTAINS_STRUCT (code
, TS_VECTOR
))
1161 compare_values (VECTOR_CST_LOG2_NPATTERNS
);
1162 compare_values (VECTOR_CST_NELTS_PER_PATTERN
);
1165 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1167 compare_values (DECL_MODE
);
1168 compare_values (DECL_NONLOCAL
);
1169 compare_values (DECL_VIRTUAL_P
);
1170 compare_values (DECL_IGNORED_P
);
1171 compare_values (DECL_ABSTRACT_P
);
1172 compare_values (DECL_ARTIFICIAL
);
1173 compare_values (DECL_USER_ALIGN
);
1174 compare_values (DECL_PRESERVE_P
);
1175 compare_values (DECL_EXTERNAL
);
1176 compare_values (DECL_NOT_GIMPLE_REG_P
);
1177 compare_values (DECL_ALIGN
);
1178 if (code
== LABEL_DECL
)
1180 compare_values (EH_LANDING_PAD_NR
);
1181 compare_values (LABEL_DECL_UID
);
1183 else if (code
== FIELD_DECL
)
1185 compare_values (DECL_PACKED
);
1186 compare_values (DECL_NONADDRESSABLE_P
);
1187 compare_values (DECL_PADDING_P
);
1188 compare_values (DECL_FIELD_ABI_IGNORED
);
1189 compare_values (DECL_OFFSET_ALIGN
);
1191 else if (code
== VAR_DECL
)
1193 compare_values (DECL_HAS_DEBUG_EXPR_P
);
1194 compare_values (DECL_NONLOCAL_FRAME
);
1196 if (code
== RESULT_DECL
1197 || code
== PARM_DECL
1198 || code
== VAR_DECL
)
1200 compare_values (DECL_BY_REFERENCE
);
1201 if (code
== VAR_DECL
1202 || code
== PARM_DECL
)
1203 compare_values (DECL_HAS_VALUE_EXPR_P
);
1207 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WRTL
))
1208 compare_values (DECL_REGISTER
);
1210 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1212 compare_values (DECL_COMMON
);
1213 compare_values (DECL_DLLIMPORT_P
);
1214 compare_values (DECL_WEAK
);
1215 compare_values (DECL_SEEN_IN_BIND_EXPR_P
);
1216 compare_values (DECL_COMDAT
);
1217 compare_values (DECL_VISIBILITY
);
1218 compare_values (DECL_VISIBILITY_SPECIFIED
);
1219 if (code
== VAR_DECL
)
1221 compare_values (DECL_HARD_REGISTER
);
1222 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1223 compare_values (DECL_IN_CONSTANT_POOL
);
1227 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1229 compare_values (DECL_BUILT_IN_CLASS
);
1230 compare_values (DECL_STATIC_CONSTRUCTOR
);
1231 compare_values (DECL_STATIC_DESTRUCTOR
);
1232 compare_values (DECL_UNINLINABLE
);
1233 compare_values (DECL_POSSIBLY_INLINED
);
1234 compare_values (DECL_IS_NOVOPS
);
1235 compare_values (DECL_IS_RETURNS_TWICE
);
1236 compare_values (DECL_IS_MALLOC
);
1237 compare_values (DECL_IS_OPERATOR_NEW_P
);
1238 compare_values (DECL_DECLARED_INLINE_P
);
1239 compare_values (DECL_STATIC_CHAIN
);
1240 compare_values (DECL_NO_INLINE_WARNING_P
);
1241 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT
);
1242 compare_values (DECL_NO_LIMIT_STACK
);
1243 compare_values (DECL_DISREGARD_INLINE_LIMITS
);
1244 compare_values (DECL_PURE_P
);
1245 compare_values (DECL_LOOPING_CONST_OR_PURE_P
);
1246 compare_values (DECL_IS_REPLACEABLE_OPERATOR
);
1247 compare_values (DECL_FINAL_P
);
1248 compare_values (DECL_CXX_CONSTRUCTOR_P
);
1249 compare_values (DECL_CXX_DESTRUCTOR_P
);
1250 if (DECL_BUILT_IN_CLASS (t1
) != NOT_BUILT_IN
)
1251 compare_values (DECL_UNCHECKED_FUNCTION_CODE
);
1254 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1256 compare_values (TYPE_MODE
);
1257 compare_values (TYPE_NEEDS_CONSTRUCTING
);
1258 if (RECORD_OR_UNION_TYPE_P (t1
))
1260 compare_values (TYPE_TRANSPARENT_AGGR
);
1261 compare_values (TYPE_FINAL_P
);
1262 compare_values (TYPE_CXX_ODR_P
);
1264 else if (code
== ARRAY_TYPE
)
1265 compare_values (TYPE_NONALIASED_COMPONENT
);
1266 if (code
== ARRAY_TYPE
|| code
== INTEGER_TYPE
)
1267 compare_values (TYPE_STRING_FLAG
);
1268 if (AGGREGATE_TYPE_P (t1
))
1269 compare_values (TYPE_TYPELESS_STORAGE
);
1270 compare_values (TYPE_EMPTY_P
);
1271 compare_values (TYPE_PACKED
);
1272 compare_values (TYPE_RESTRICT
);
1273 compare_values (TYPE_USER_ALIGN
);
1274 compare_values (TYPE_READONLY
);
1275 compare_values (TYPE_PRECISION
);
1276 compare_values (TYPE_ALIGN
);
1277 /* Do not compare TYPE_ALIAS_SET. Doing so introduce ordering issues
1278 with calls to get_alias_set which may initialize it for streamed
1282 /* We don't want to compare locations, so there is nothing do compare
1285 /* BLOCKs are function local and we don't merge anything there, so
1286 simply refuse to merge. */
1287 if (CODE_CONTAINS_STRUCT (code
, TS_BLOCK
))
1290 if (CODE_CONTAINS_STRUCT (code
, TS_TRANSLATION_UNIT_DECL
))
1291 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1
),
1292 TRANSLATION_UNIT_LANGUAGE (t2
)) != 0)
1295 if (CODE_CONTAINS_STRUCT (code
, TS_TARGET_OPTION
))
1296 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1
), TREE_TARGET_OPTION (t2
)))
1299 if (CODE_CONTAINS_STRUCT (code
, TS_OPTIMIZATION
))
1300 if (!cl_optimization_option_eq (TREE_OPTIMIZATION (t1
),
1301 TREE_OPTIMIZATION (t2
)))
1304 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1305 if (vec_safe_length (BINFO_BASE_ACCESSES (t1
))
1306 != vec_safe_length (BINFO_BASE_ACCESSES (t2
)))
1309 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1310 compare_values (CONSTRUCTOR_NELTS
);
1312 if (CODE_CONTAINS_STRUCT (code
, TS_IDENTIFIER
))
1313 if (IDENTIFIER_LENGTH (t1
) != IDENTIFIER_LENGTH (t2
)
1314 || memcmp (IDENTIFIER_POINTER (t1
), IDENTIFIER_POINTER (t2
),
1315 IDENTIFIER_LENGTH (t1
)) != 0)
1318 if (CODE_CONTAINS_STRUCT (code
, TS_STRING
))
1319 if (TREE_STRING_LENGTH (t1
) != TREE_STRING_LENGTH (t2
)
1320 || memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
1321 TREE_STRING_LENGTH (t1
)) != 0)
1324 if (code
== OMP_CLAUSE
)
1326 compare_values (OMP_CLAUSE_CODE
);
1327 switch (OMP_CLAUSE_CODE (t1
))
1329 case OMP_CLAUSE_DEFAULT
:
1330 compare_values (OMP_CLAUSE_DEFAULT_KIND
);
1332 case OMP_CLAUSE_SCHEDULE
:
1333 compare_values (OMP_CLAUSE_SCHEDULE_KIND
);
1335 case OMP_CLAUSE_DEPEND
:
1336 compare_values (OMP_CLAUSE_DEPEND_KIND
);
1338 case OMP_CLAUSE_MAP
:
1339 compare_values (OMP_CLAUSE_MAP_KIND
);
1341 case OMP_CLAUSE_PROC_BIND
:
1342 compare_values (OMP_CLAUSE_PROC_BIND_KIND
);
1344 case OMP_CLAUSE_REDUCTION
:
1345 compare_values (OMP_CLAUSE_REDUCTION_CODE
);
1346 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT
);
1347 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE
);
1354 #undef compare_values
1357 /* Compare pointer fields. */
1359 /* Recurse. Search & Replaced from DFS_write_tree_body.
1360 Folding the early checks into the compare_tree_edges recursion
1361 macro makes debugging way quicker as you are able to break on
1362 compare_tree_sccs_1 and simply finish until a call returns false
1363 to spot the SCC members with the difference. */
1364 #define compare_tree_edges(E1, E2) \
1366 tree t1_ = (E1), t2_ = (E2); \
1369 || !TREE_VISITED (t2_) \
1370 || (!TREE_ASM_WRITTEN (t2_) \
1371 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1373 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1374 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1377 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
1379 if (code
!= IDENTIFIER_NODE
)
1380 compare_tree_edges (TREE_TYPE (t1
), TREE_TYPE (t2
));
1383 if (CODE_CONTAINS_STRUCT (code
, TS_VECTOR
))
1385 /* Note that the number of elements for EXPR has already been emitted
1386 in EXPR's header (see streamer_write_tree_header). */
1387 unsigned int count
= vector_cst_encoded_nelts (t1
);
1388 for (unsigned int i
= 0; i
< count
; ++i
)
1389 compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1
, i
),
1390 VECTOR_CST_ENCODED_ELT (t2
, i
));
1393 if (CODE_CONTAINS_STRUCT (code
, TS_COMPLEX
))
1395 compare_tree_edges (TREE_REALPART (t1
), TREE_REALPART (t2
));
1396 compare_tree_edges (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
1399 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_MINIMAL
))
1401 compare_tree_edges (DECL_NAME (t1
), DECL_NAME (t2
));
1402 /* ??? Global decls from different TUs have non-matching
1403 TRANSLATION_UNIT_DECLs. Only consider a small set of
1404 decls equivalent, we should not end up merging others. */
1405 if ((code
== TYPE_DECL
1406 || code
== NAMESPACE_DECL
1407 || code
== IMPORTED_DECL
1408 || code
== CONST_DECL
1409 || (VAR_OR_FUNCTION_DECL_P (t1
)
1410 && (TREE_PUBLIC (t1
) || DECL_EXTERNAL (t1
))))
1411 && DECL_FILE_SCOPE_P (t1
) && DECL_FILE_SCOPE_P (t2
))
1414 compare_tree_edges (DECL_CONTEXT (t1
), DECL_CONTEXT (t2
));
1417 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1419 compare_tree_edges (DECL_SIZE (t1
), DECL_SIZE (t2
));
1420 compare_tree_edges (DECL_SIZE_UNIT (t1
), DECL_SIZE_UNIT (t2
));
1421 compare_tree_edges (DECL_ATTRIBUTES (t1
), DECL_ATTRIBUTES (t2
));
1422 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1
), DECL_ABSTRACT_ORIGIN (t2
));
1423 if ((code
== VAR_DECL
1424 || code
== PARM_DECL
)
1425 && DECL_HAS_VALUE_EXPR_P (t1
))
1426 compare_tree_edges (DECL_VALUE_EXPR (t1
), DECL_VALUE_EXPR (t2
));
1427 if (code
== VAR_DECL
1428 && DECL_HAS_DEBUG_EXPR_P (t1
))
1429 compare_tree_edges (DECL_DEBUG_EXPR (t1
), DECL_DEBUG_EXPR (t2
));
1430 /* LTO specific edges. */
1431 if (code
!= FUNCTION_DECL
1432 && code
!= TRANSLATION_UNIT_DECL
)
1433 compare_tree_edges (DECL_INITIAL (t1
), DECL_INITIAL (t2
));
1436 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
1438 if (code
== FUNCTION_DECL
)
1441 for (a1
= DECL_ARGUMENTS (t1
), a2
= DECL_ARGUMENTS (t2
);
1443 a1
= TREE_CHAIN (a1
), a2
= TREE_CHAIN (a2
))
1444 compare_tree_edges (a1
, a2
);
1445 compare_tree_edges (DECL_RESULT (t1
), DECL_RESULT (t2
));
1447 else if (code
== TYPE_DECL
)
1448 compare_tree_edges (DECL_ORIGINAL_TYPE (t1
), DECL_ORIGINAL_TYPE (t2
));
1451 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1453 /* Make sure we don't inadvertently set the assembler name. */
1454 if (DECL_ASSEMBLER_NAME_SET_P (t1
))
1455 compare_tree_edges (DECL_ASSEMBLER_NAME (t1
),
1456 DECL_ASSEMBLER_NAME (t2
));
1459 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
1461 compare_tree_edges (DECL_FIELD_OFFSET (t1
), DECL_FIELD_OFFSET (t2
));
1462 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1
), DECL_BIT_FIELD_TYPE (t2
));
1463 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1
),
1464 DECL_BIT_FIELD_REPRESENTATIVE (t2
));
1465 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1
),
1466 DECL_FIELD_BIT_OFFSET (t2
));
1467 compare_tree_edges (DECL_FCONTEXT (t1
), DECL_FCONTEXT (t2
));
1470 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1472 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1
),
1473 DECL_FUNCTION_PERSONALITY (t2
));
1474 compare_tree_edges (DECL_VINDEX (t1
), DECL_VINDEX (t2
));
1475 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1
),
1476 DECL_FUNCTION_SPECIFIC_TARGET (t2
));
1477 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1
),
1478 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2
));
1481 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1483 compare_tree_edges (TYPE_SIZE (t1
), TYPE_SIZE (t2
));
1484 compare_tree_edges (TYPE_SIZE_UNIT (t1
), TYPE_SIZE_UNIT (t2
));
1485 compare_tree_edges (TYPE_ATTRIBUTES (t1
), TYPE_ATTRIBUTES (t2
));
1486 compare_tree_edges (TYPE_NAME (t1
), TYPE_NAME (t2
));
1487 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1488 reconstructed during fixup. */
1489 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1491 compare_tree_edges (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
));
1492 /* ??? Global types from different TUs have non-matching
1493 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1495 if (TYPE_FILE_SCOPE_P (t1
) && TYPE_FILE_SCOPE_P (t2
))
1498 compare_tree_edges (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1499 /* TYPE_CANONICAL is re-computed during type merging, so do not
1501 compare_tree_edges (TYPE_STUB_DECL (t1
), TYPE_STUB_DECL (t2
));
1504 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_NON_COMMON
))
1506 if (code
== ARRAY_TYPE
)
1507 compare_tree_edges (TYPE_DOMAIN (t1
), TYPE_DOMAIN (t2
));
1508 else if (RECORD_OR_UNION_TYPE_P (t1
))
1511 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1513 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1514 compare_tree_edges (f1
, f2
);
1516 else if (code
== FUNCTION_TYPE
1517 || code
== METHOD_TYPE
)
1518 compare_tree_edges (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
));
1520 if (!POINTER_TYPE_P (t1
))
1521 compare_tree_edges (TYPE_MIN_VALUE_RAW (t1
), TYPE_MIN_VALUE_RAW (t2
));
1522 compare_tree_edges (TYPE_MAX_VALUE_RAW (t1
), TYPE_MAX_VALUE_RAW (t2
));
1525 if (CODE_CONTAINS_STRUCT (code
, TS_LIST
))
1527 compare_tree_edges (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1528 compare_tree_edges (TREE_VALUE (t1
), TREE_VALUE (t2
));
1529 compare_tree_edges (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1532 if (CODE_CONTAINS_STRUCT (code
, TS_VEC
))
1533 for (int i
= 0; i
< TREE_VEC_LENGTH (t1
); i
++)
1534 compare_tree_edges (TREE_VEC_ELT (t1
, i
), TREE_VEC_ELT (t2
, i
));
1536 if (CODE_CONTAINS_STRUCT (code
, TS_EXP
))
1538 for (int i
= 0; i
< TREE_OPERAND_LENGTH (t1
); i
++)
1539 compare_tree_edges (TREE_OPERAND (t1
, i
),
1540 TREE_OPERAND (t2
, i
));
1542 /* BLOCKs are function local and we don't merge anything there. */
1543 if (TREE_BLOCK (t1
) || TREE_BLOCK (t2
))
1547 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1551 /* Lengths have already been compared above. */
1552 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1
), i
, t
)
1553 compare_tree_edges (t
, BINFO_BASE_BINFO (t2
, i
));
1554 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1
), i
, t
)
1555 compare_tree_edges (t
, BINFO_BASE_ACCESS (t2
, i
));
1556 compare_tree_edges (BINFO_OFFSET (t1
), BINFO_OFFSET (t2
));
1557 compare_tree_edges (BINFO_VTABLE (t1
), BINFO_VTABLE (t2
));
1558 compare_tree_edges (BINFO_VPTR_FIELD (t1
), BINFO_VPTR_FIELD (t2
));
1559 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1560 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1563 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1567 /* Lengths have already been compared above. */
1568 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, index
, value
)
1570 compare_tree_edges (index
, CONSTRUCTOR_ELT (t2
, i
)->index
);
1571 compare_tree_edges (value
, CONSTRUCTOR_ELT (t2
, i
)->value
);
1575 if (code
== OMP_CLAUSE
)
1579 for (i
= 0; i
< omp_clause_num_ops
[OMP_CLAUSE_CODE (t1
)]; i
++)
1580 compare_tree_edges (OMP_CLAUSE_OPERAND (t1
, i
),
1581 OMP_CLAUSE_OPERAND (t2
, i
));
1582 compare_tree_edges (OMP_CLAUSE_CHAIN (t1
), OMP_CLAUSE_CHAIN (t2
));
1585 #undef compare_tree_edges
1590 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1591 out MAP if they are equal. */
1594 compare_tree_sccs (tree_scc
*pscc
, tree_scc
*scc
,
1597 /* Assume SCC entry hashes are sorted after their cardinality. Which
1598 means we can simply take the first n-tuple of equal hashes
1599 (which is recorded as entry_len) and do n SCC entry candidate
1601 for (unsigned i
= 0; i
< pscc
->entry_len
; ++i
)
1604 num_scc_compare_collisions
++;
1605 if (compare_tree_sccs_1 (pscc
->entries
[0], scc
->entries
[i
], &mapp
))
1607 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1608 on the scc as all trees will be freed. */
1611 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1612 the SCC prevails. */
1613 for (unsigned j
= 0; j
< scc
->len
; ++j
)
1614 TREE_ASM_WRITTEN (scc
->entries
[j
]) = 0;
1620 /* QSort sort function to sort a map of two pointers after the 2nd
1624 cmp_tree (const void *p1_
, const void *p2_
)
1626 tree
*p1
= (tree
*)(const_cast<void *>(p1_
));
1627 tree
*p2
= (tree
*)(const_cast<void *>(p2_
));
1630 return ((uintptr_t)p1
[1] < (uintptr_t)p2
[1]) ? -1 : 1;
1633 /* New scc of size 1 containing T was streamed in from DATA_IN and not merged.
1634 Register it to reader cache at index FROM. */
1637 process_dref (class data_in
*data_in
, tree t
, unsigned from
)
1639 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
1640 /* If we got a debug reference queued, see if the prevailing
1641 tree has a debug reference and if not, register the one
1642 for the tree we are about to throw away. */
1643 if (dref_queue
.length () == 1)
1645 dref_entry e
= dref_queue
.pop ();
1647 == streamer_tree_cache_get_tree (cache
, from
));
1649 unsigned HOST_WIDE_INT off
;
1650 if (!debug_hooks
->die_ref_for_decl (t
, &sym
, &off
))
1651 debug_hooks
->register_external_die (t
, e
.sym
, e
.off
);
1655 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1656 hash value SCC_HASH with an already recorded SCC. Return true if
1657 that was successful, otherwise return false. */
1660 unify_scc (class data_in
*data_in
, unsigned from
,
1661 unsigned len
, unsigned scc_entry_len
, hashval_t scc_hash
)
1663 bool unified_p
= false;
1664 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
1666 = (tree_scc
*) alloca (sizeof (tree_scc
) + (len
- 1) * sizeof (tree
));
1668 scc
->hash
= scc_hash
;
1670 scc
->entry_len
= scc_entry_len
;
1671 for (unsigned i
= 0; i
< len
; ++i
)
1673 tree t
= streamer_tree_cache_get_tree (cache
, from
+ i
);
1674 scc
->entries
[i
] = t
;
1675 /* These types should be streamed as unshared. */
1677 (!(TREE_CODE (t
) == TRANSLATION_UNIT_DECL
1678 || (VAR_OR_FUNCTION_DECL_P (t
)
1679 && !(TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
1680 || TREE_CODE (t
) == LABEL_DECL
1681 || (TREE_CODE (t
) == NAMESPACE_DECL
&& !DECL_NAME (t
))
1683 && type_with_linkage_p (TYPE_MAIN_VARIANT (t
))
1684 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t
)))));
1687 /* Look for the list of candidate SCCs to compare against. */
1689 slot
= tree_scc_hash
->find_slot_with_hash (scc
, scc_hash
, INSERT
);
1692 /* Try unifying against each candidate. */
1695 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1696 outside of the scc when following tree edges. Make sure
1697 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1698 to track whether we visited the SCC member during the compare.
1699 We cannot use TREE_VISITED on the pscc members as the extended
1700 scc and pscc can overlap. */
1701 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1703 TREE_VISITED (scc
->entries
[i
]) = 1;
1704 gcc_checking_assert (!TREE_ASM_WRITTEN (scc
->entries
[i
]));
1707 tree
*map
= XALLOCAVEC (tree
, 2 * len
);
1708 for (tree_scc
*pscc
= *slot
; pscc
; pscc
= pscc
->next
)
1710 if (!compare_tree_sccs (pscc
, scc
, map
))
1713 /* Found an equal SCC. */
1715 num_scc_compare_collisions
--;
1717 total_scc_size_merged
+= len
;
1720 for (unsigned i
= 0; i
< len
; ++i
)
1722 tree t
= map
[2*i
+1];
1723 enum tree_code code
= TREE_CODE (t
);
1724 /* IDENTIFIER_NODEs should be singletons and are merged by the
1725 streamer. The others should be singletons, too, and we
1726 should not merge them in any way. */
1727 gcc_assert (code
!= TRANSLATION_UNIT_DECL
1728 && code
!= IDENTIFIER_NODE
);
1731 /* Fixup the streamer cache with the prevailing nodes according
1732 to the tree node mapping computed by compare_tree_sccs. */
1735 process_dref (data_in
, pscc
->entries
[0], from
);
1736 lto_maybe_register_decl (data_in
, pscc
->entries
[0], from
);
1737 streamer_tree_cache_replace_tree (cache
, pscc
->entries
[0], from
);
1741 tree
*map2
= XALLOCAVEC (tree
, 2 * len
);
1742 for (unsigned i
= 0; i
< len
; ++i
)
1744 map2
[i
*2] = (tree
)(uintptr_t)(from
+ i
);
1745 map2
[i
*2+1] = scc
->entries
[i
];
1747 qsort (map2
, len
, 2 * sizeof (tree
), cmp_tree
);
1748 qsort (map
, len
, 2 * sizeof (tree
), cmp_tree
);
1749 for (unsigned i
= 0; i
< len
; ++i
)
1751 lto_maybe_register_decl (data_in
, map
[2*i
],
1752 (uintptr_t)map2
[2*i
]);
1753 streamer_tree_cache_replace_tree (cache
, map
[2*i
],
1754 (uintptr_t)map2
[2*i
]);
1758 /* Free the tree nodes from the read SCC. */
1759 data_in
->location_cache
.revert_location_cache ();
1760 for (unsigned i
= 0; i
< len
; ++i
)
1762 if (TYPE_P (scc
->entries
[i
]))
1764 free_node (scc
->entries
[i
]);
1767 /* Drop DIE references.
1768 ??? Do as in the size-one SCC case which involves sorting
1770 dref_queue
.truncate (0);
1775 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1777 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1778 TREE_VISITED (scc
->entries
[i
]) = 0;
1781 /* If we didn't unify it to any candidate duplicate the relevant
1782 pieces to permanent storage and link it into the chain. */
1786 = XOBNEWVAR (&tree_scc_hash_obstack
, tree_scc
, sizeof (tree_scc
));
1787 memcpy (pscc
, scc
, sizeof (tree_scc
));
1788 pscc
->next
= (*slot
);
1794 typedef int_hash
<unsigned, 0, UINT_MAX
> code_id_hash
;
1796 /* Do registering necessary once new tree fully streamed in (including all
1797 trees it reffers to). */
1800 process_new_tree (tree t
, hash_map
<code_id_hash
, unsigned> *hm
,
1801 unsigned index
, unsigned *total
, class data_in
*data_in
)
1803 /* Reconstruct the type variant and pointer-to/reference-to
1807 /* Map the tree types to their frequencies. */
1808 if (flag_lto_dump_type_stats
)
1810 unsigned key
= (unsigned) TREE_CODE (t
);
1811 unsigned *countp
= hm
->get (key
);
1812 hm
->put (key
, countp
? (*countp
) + 1 : 1);
1816 num_prevailing_types
++;
1817 lto_fixup_prevailing_type (t
);
1819 /* Compute the canonical type of all non-ODR types.
1820 Delay ODR types for the end of merging process - the canonical
1821 type for those can be computed using the (unique) name however
1822 we want to do this only if units in other languages do not
1823 contain structurally equivalent type.
1825 Because SCC components are streamed in random (hash) order
1826 we may have encountered the type before while registering
1827 type canonical of a derived type in the same SCC. */
1828 if (!TYPE_CANONICAL (t
))
1830 if (!RECORD_OR_UNION_TYPE_P (t
)
1831 || !TYPE_CXX_ODR_P (t
))
1832 gimple_register_canonical_type (t
);
1833 else if (COMPLETE_TYPE_P (t
))
1834 vec_safe_push (types_to_register
, t
);
1836 if (TYPE_MAIN_VARIANT (t
) == t
&& odr_type_p (t
))
1837 register_odr_type (t
);
1839 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1840 type which is also member of this SCC. */
1841 if (TREE_CODE (t
) == INTEGER_CST
1842 && !TREE_OVERFLOW (t
))
1843 cache_integer_cst (t
);
1846 lto_maybe_register_decl (data_in
, t
, index
);
1847 /* Scan the tree for references to global functions or
1848 variables and record those for later fixup. */
1849 if (mentions_vars_p (t
))
1850 vec_safe_push (tree_with_vars
, t
);
1854 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1855 RESOLUTIONS is the set of symbols picked by the linker (read from the
1856 resolution file when the linker plugin is being used). */
1859 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
1860 vec
<ld_plugin_symbol_resolution_t
> resolutions
)
1862 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
1863 const int decl_offset
= sizeof (struct lto_decl_header
);
1864 const int main_offset
= decl_offset
+ header
->decl_state_size
;
1865 const int string_offset
= main_offset
+ header
->main_size
;
1866 class data_in
*data_in
;
1868 const uint32_t *data_ptr
, *data_end
;
1869 uint32_t num_decl_states
;
1871 lto_input_block
ib_main ((const char *) data
+ main_offset
,
1872 header
->main_size
, decl_data
->mode_table
);
1874 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
1875 header
->string_size
, resolutions
);
1877 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1878 internal types that should not be merged. */
1880 hash_map
<code_id_hash
, unsigned> hm
;
1883 /* Read the global declarations and types. */
1884 while (ib_main
.p
< ib_main
.len
)
1887 unsigned from
= data_in
->reader_cache
->nodes
.length ();
1888 /* Read and uniquify SCCs as in the input stream. */
1889 enum LTO_tags tag
= streamer_read_record_start (&ib_main
);
1890 if (tag
== LTO_tree_scc
|| tag
== LTO_trees
)
1893 unsigned scc_entry_len
;
1895 /* Because we stream in SCC order we know that all unshared trees
1896 are now fully streamed. Process them. */
1897 hashval_t scc_hash
= lto_input_scc (&ib_main
, data_in
, &len_
,
1899 tag
== LTO_tree_scc
);
1900 unsigned len
= data_in
->reader_cache
->nodes
.length () - from
;
1901 gcc_assert (len
== len_
);
1903 if (tag
== LTO_tree_scc
)
1905 total_scc_size
+= len
;
1909 num_unshared_trees_read
+= len
;
1911 /* We have the special case of size-1 SCCs that are pre-merged
1912 by means of identifier and string sharing for example.
1913 ??? Maybe we should avoid streaming those as SCCs. */
1914 tree first
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1916 /* Identifier and integers are shared specially, they should never
1917 go by the tree merging path. */
1918 gcc_checking_assert ((TREE_CODE (first
) != IDENTIFIER_NODE
1919 && (TREE_CODE (first
) != INTEGER_CST
1920 || TREE_OVERFLOW (first
)))
1923 /* Try to unify the SCC with already existing ones. */
1924 if (!flag_ltrans
&& tag
!= LTO_trees
1925 && unify_scc (data_in
, from
,
1926 len
, scc_entry_len
, scc_hash
))
1929 /* Tree merging failed, mark entries in location cache as
1931 data_in
->location_cache
.accept_location_cache ();
1933 bool seen_type
= false;
1934 for (unsigned i
= 0; i
< len
; ++i
)
1936 tree t
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1938 process_new_tree (t
, &hm
, from
+ i
, &total
, data_in
);
1943 /* Register DECLs with the debuginfo machinery. */
1944 while (!dref_queue
.is_empty ())
1946 dref_entry e
= dref_queue
.pop ();
1947 debug_hooks
->register_external_die (e
.decl
, e
.sym
, e
.off
);
1951 num_type_scc_trees
+= len
;
1955 t
= lto_input_tree_1 (&ib_main
, data_in
, tag
, 0);
1956 gcc_assert (data_in
->reader_cache
->nodes
.length () == from
+ 1);
1957 num_unshared_trees_read
++;
1958 data_in
->location_cache
.accept_location_cache ();
1959 process_dref (data_in
, t
, from
);
1960 if (TREE_CODE (t
) == IDENTIFIER_NODE
1961 || (TREE_CODE (t
) == INTEGER_CST
1962 && !TREE_OVERFLOW (t
)))
1966 lto_maybe_register_decl (data_in
, t
, from
);
1967 process_new_tree (t
, &hm
, from
, &total
, data_in
);
1972 /* Dump type statistics. */
1973 if (flag_lto_dump_type_stats
)
1975 fprintf (stdout
, " Type Frequency Percentage\n\n");
1976 for (hash_map
<code_id_hash
, unsigned>::iterator itr
= hm
.begin ();
1980 std::pair
<unsigned, unsigned> p
= *itr
;
1981 enum tree_code code
= (enum tree_code
) p
.first
;
1982 fprintf (stdout
, "%14s %6d %12.2f\n", get_tree_code_name (code
),
1983 p
.second
, float (p
.second
)/total
*100);
1987 data_in
->location_cache
.apply_location_cache ();
1989 /* Read in lto_in_decl_state objects. */
1990 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
1992 = (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
1993 num_decl_states
= *data_ptr
++;
1995 gcc_assert (num_decl_states
> 0);
1996 decl_data
->global_decl_state
= lto_new_in_decl_state ();
1997 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
1998 decl_data
->global_decl_state
);
2000 /* Read in per-function decl states and enter them in hash table. */
2001 decl_data
->function_decl_states
2002 = hash_table
<decl_state_hasher
>::create_ggc (37);
2004 for (i
= 1; i
< num_decl_states
; i
++)
2006 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
2008 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
2009 lto_in_decl_state
**slot
2010 = decl_data
->function_decl_states
->find_slot (state
, INSERT
);
2011 gcc_assert (*slot
== NULL
);
2015 if (data_ptr
!= data_end
)
2016 internal_error ("bytecode stream: garbage at the end of symbols section");
2018 /* Set the current decl state to be the global state. */
2019 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
2021 lto_data_in_delete (data_in
);
2024 /* Custom version of strtoll, which is not portable. */
2027 lto_parse_hex (const char *p
)
2031 for (; *p
!= '\0'; ++p
)
2036 if (c
>= '0' && c
<= '9')
2038 else if (c
>= 'a' && c
<= 'f')
2039 part
= c
- 'a' + 10;
2040 else if (c
>= 'A' && c
<= 'F')
2041 part
= c
- 'A' + 10;
2043 internal_error ("could not parse hex number");
2050 /* Read resolution for file named FILE_NAME. The resolution is read from
2054 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
2056 /* We require that objects in the resolution file are in the same
2057 order as the lto1 command line. */
2058 unsigned int name_len
;
2060 unsigned int num_symbols
;
2062 struct lto_file_decl_data
*file_data
;
2063 splay_tree_node nd
= NULL
;
2068 name_len
= strlen (file
->filename
);
2069 obj_name
= XNEWVEC (char, name_len
+ 1);
2070 fscanf (resolution
, " "); /* Read white space. */
2072 fread (obj_name
, sizeof (char), name_len
, resolution
);
2073 obj_name
[name_len
] = '\0';
2074 if (filename_cmp (obj_name
, file
->filename
) != 0)
2075 internal_error ("unexpected file name %s in linker resolution file. "
2076 "Expected %s", obj_name
, file
->filename
);
2077 if (file
->offset
!= 0)
2082 t
= fscanf (resolution
, "@0x%16s", offset_p
);
2084 internal_error ("could not parse file offset");
2085 offset
= lto_parse_hex (offset_p
);
2086 if (offset
!= file
->offset
)
2087 internal_error ("unexpected offset");
2092 fscanf (resolution
, "%u", &num_symbols
);
2094 for (i
= 0; i
< num_symbols
; i
++)
2098 unsigned HOST_WIDE_INT id
;
2100 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
2102 unsigned int lto_resolution_str_len
2103 = sizeof (lto_resolution_str
) / sizeof (char *);
2106 t
= fscanf (resolution
, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
2107 " %26s %*[^\n]\n", &index
, &id
, r_str
);
2109 internal_error ("invalid line in the resolution file");
2111 for (j
= 0; j
< lto_resolution_str_len
; j
++)
2113 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
2115 r
= (enum ld_plugin_symbol_resolution
) j
;
2119 if (j
== lto_resolution_str_len
)
2120 internal_error ("invalid resolution in the resolution file");
2122 if (!(nd
&& lto_splay_tree_id_equal_p (nd
->key
, id
)))
2124 nd
= lto_splay_tree_lookup (file_ids
, id
);
2126 internal_error ("resolution sub id %wx not in object file", id
);
2129 file_data
= (struct lto_file_decl_data
*)nd
->value
;
2130 /* The indexes are very sparse. To save memory save them in a compact
2131 format that is only unpacked later when the subfile is processed. */
2134 file_data
->respairs
.safe_push (rp
);
2135 if (file_data
->max_index
< index
)
2136 file_data
->max_index
= index
;
2140 /* List of file_decl_datas. */
2141 struct file_data_list
2143 struct lto_file_decl_data
*first
, *last
;
2146 /* Is the name for a id'ed LTO section? */
2149 lto_section_with_id (const char *name
, unsigned HOST_WIDE_INT
*id
)
2153 if (strncmp (name
, section_name_prefix
, strlen (section_name_prefix
)))
2155 s
= strrchr (name
, '.');
2158 /* If the section is not suffixed with an ID return. */
2159 if ((size_t)(s
- name
) == strlen (section_name_prefix
))
2161 return sscanf (s
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, id
) == 1;
2164 /* Create file_data of each sub file id. */
2167 create_subid_section_table (struct lto_section_slot
*ls
, splay_tree file_ids
,
2168 struct file_data_list
*list
)
2170 struct lto_section_slot s_slot
, *new_slot
;
2171 unsigned HOST_WIDE_INT id
;
2175 struct lto_file_decl_data
*file_data
;
2177 if (!lto_section_with_id (ls
->name
, &id
))
2180 /* Find hash table of sub module id. */
2181 nd
= lto_splay_tree_lookup (file_ids
, id
);
2184 file_data
= (struct lto_file_decl_data
*)nd
->value
;
2188 file_data
= ggc_alloc
<lto_file_decl_data
> ();
2189 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
2191 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();
2192 lto_splay_tree_insert (file_ids
, id
, file_data
);
2194 /* Maintain list in linker order. */
2196 list
->first
= file_data
;
2198 list
->last
->next
= file_data
;
2200 list
->last
= file_data
;
2203 /* Copy section into sub module hash table. */
2204 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
2205 s_slot
.name
= new_name
;
2206 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
2207 gcc_assert (*hash_slot
== NULL
);
2209 new_slot
= XDUP (struct lto_section_slot
, ls
);
2210 new_slot
->name
= new_name
;
2211 *hash_slot
= new_slot
;
2215 /* Read declarations and other initializations for a FILE_DATA. */
2218 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
,
2223 vec
<ld_plugin_symbol_resolution_t
>
2224 resolutions
= vNULL
;
2228 /* Create vector for fast access of resolution. We do this lazily
2230 resolutions
.safe_grow_cleared (file_data
->max_index
+ 1);
2231 for (i
= 0; file_data
->respairs
.iterate (i
, &rp
); i
++)
2232 resolutions
[rp
->index
] = rp
->res
;
2233 file_data
->respairs
.release ();
2235 file_data
->renaming_hash_table
= lto_create_renaming_table ();
2236 file_data
->file_name
= file
->filename
;
2237 file_data
->order
= order
;
2239 /* Read and verify LTO section. */
2240 data
= lto_get_summary_section_data (file_data
, LTO_section_lto
, &len
);
2243 fatal_error (input_location
, "bytecode stream in file %qs generated "
2244 "with GCC compiler older than 10.0", file_data
->file_name
);
2248 memcpy (&file_data
->lto_section_header
, data
, sizeof (lto_section
));
2249 lto_check_version (file_data
->lto_section_header
.major_version
,
2250 file_data
->lto_section_header
.minor_version
,
2251 file_data
->file_name
);
2253 #ifdef ACCEL_COMPILER
2254 lto_input_mode_table (file_data
);
2256 file_data
->mode_table
= lto_mode_identity_table
;
2259 data
= lto_get_summary_section_data (file_data
, LTO_section_decls
, &len
);
2262 internal_error ("cannot read %<LTO_section_decls%> from %s",
2263 file_data
->file_name
);
2266 /* Frees resolutions. */
2267 lto_read_decls (file_data
, data
, resolutions
);
2268 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
2271 /* Finalize FILE_DATA in FILE and increase COUNT. */
2274 lto_create_files_from_ids (lto_file
*file
, struct lto_file_decl_data
*file_data
,
2275 int *count
, int order
)
2277 lto_file_finalize (file_data
, file
, order
);
2278 if (symtab
->dump_file
)
2279 fprintf (symtab
->dump_file
,
2280 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX
"\n",
2281 file_data
->file_name
, file_data
->id
);
2286 /* Generate a TREE representation for all types and external decls
2289 Read all of the globals out of the file. Then read the cgraph
2290 and process the .o index into the cgraph nodes so that it can open
2291 the .o file to load the functions and ipa information. */
2293 static struct lto_file_decl_data
*
2294 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
2296 struct lto_file_decl_data
*file_data
= NULL
;
2297 splay_tree file_ids
;
2298 htab_t section_hash_table
;
2299 struct lto_section_slot
*section
;
2300 struct file_data_list file_list
;
2301 struct lto_section_list section_list
;
2303 memset (§ion_list
, 0, sizeof (struct lto_section_list
));
2304 section_hash_table
= lto_obj_build_section_table (file
, §ion_list
);
2306 /* Dump the details of LTO objects. */
2307 if (flag_lto_dump_objects
)
2310 fprintf (stdout
, "\n LTO Object Name: %s\n", file
->filename
);
2311 fprintf (stdout
, "\nNo. Offset Size Section Name\n\n");
2312 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
2313 fprintf (stdout
, "%2d %8" PRId64
" %8" PRIu64
" %s\n",
2314 ++i
, (int64_t) section
->start
, (uint64_t) section
->len
,
2318 /* Find all sub modules in the object and put their sections into new hash
2319 tables in a splay tree. */
2320 file_ids
= lto_splay_tree_new ();
2321 memset (&file_list
, 0, sizeof (struct file_data_list
));
2322 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
2323 create_subid_section_table (section
, file_ids
, &file_list
);
2325 /* Add resolutions to file ids. */
2326 lto_resolution_read (file_ids
, resolution_file
, file
);
2328 /* Finalize each lto file for each submodule in the merged object. */
2330 for (file_data
= file_list
.first
; file_data
!= NULL
;
2331 file_data
= file_data
->next
)
2332 lto_create_files_from_ids (file
, file_data
, count
, order
++);
2334 splay_tree_delete (file_ids
);
2335 htab_delete (section_hash_table
);
2337 return file_list
.first
;
2340 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2341 #define LTO_MMAP_IO 1
2345 /* Page size of machine is used for mmap and munmap calls. */
2346 static size_t page_mask
;
2349 /* Get the section data of length LEN from FILENAME starting at
2350 OFFSET. The data segment must be freed by the caller when the
2351 caller is finished. Returns NULL if all was not well. */
2354 lto_read_section_data (struct lto_file_decl_data
*file_data
,
2355 intptr_t offset
, size_t len
)
2359 static char *fd_name
;
2361 intptr_t computed_len
;
2362 intptr_t computed_offset
;
2366 /* Keep a single-entry file-descriptor cache. The last file we
2367 touched will get closed at exit.
2368 ??? Eventually we want to add a more sophisticated larger cache
2369 or rather fix function body streaming to not stream them in
2370 practically random order. */
2372 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
2380 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
2383 fatal_error (input_location
, "Cannot open %s", file_data
->file_name
);
2386 fd_name
= xstrdup (file_data
->file_name
);
2392 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
2393 page_mask
= ~(page_size
- 1);
2396 computed_offset
= offset
& page_mask
;
2397 diff
= offset
- computed_offset
;
2398 computed_len
= len
+ diff
;
2400 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
2401 fd
, computed_offset
);
2402 if (result
== MAP_FAILED
)
2404 fatal_error (input_location
, "Cannot map %s", file_data
->file_name
);
2408 return result
+ diff
;
2410 result
= (char *) xmalloc (len
);
2411 if (lseek (fd
, offset
, SEEK_SET
) != offset
2412 || read (fd
, result
, len
) != (ssize_t
) len
)
2415 fatal_error (input_location
, "Cannot read %s", file_data
->file_name
);
2419 /* Native windows doesn't supports delayed unlink on opened file. So
2420 we close file here again. This produces higher I/O load, but at least
2421 it prevents to have dangling file handles preventing unlink. */
2432 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2433 NAME will be NULL unless the section type is for a function
2437 get_section_data (struct lto_file_decl_data
*file_data
,
2438 enum lto_section_type section_type
,
2439 const char *name
, int order
,
2442 htab_t section_hash_table
= file_data
->section_hash_table
;
2443 struct lto_section_slot
*f_slot
;
2444 struct lto_section_slot s_slot
;
2445 const char *section_name
= lto_get_section_name (section_type
, name
,
2450 s_slot
.name
= section_name
;
2451 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
2454 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
2458 free (CONST_CAST (char *, section_name
));
2463 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2464 starts at OFFSET and has LEN bytes. */
2467 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
2468 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
2469 const char *name ATTRIBUTE_UNUSED
,
2470 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
2473 intptr_t computed_len
;
2474 intptr_t computed_offset
;
2479 computed_offset
= ((intptr_t) offset
) & page_mask
;
2480 diff
= (intptr_t) offset
- computed_offset
;
2481 computed_len
= len
+ diff
;
2483 munmap ((caddr_t
) computed_offset
, computed_len
);
2485 free (CONST_CAST(char *, offset
));
2489 static lto_file
*current_lto_file
;
2491 /* If TT is a variable or function decl replace it with its
2492 prevailing variant. */
2493 #define LTO_SET_PREVAIL(tt) \
2495 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2496 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2498 tt = lto_symtab_prevailing_decl (tt); \
2503 /* Ensure that TT isn't a replacable var of function decl. */
2504 #define LTO_NO_PREVAIL(tt) \
2505 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2507 /* Given a tree T replace all fields referring to variables or functions
2508 with their prevailing variant. */
2510 lto_fixup_prevailing_decls (tree t
)
2512 enum tree_code code
= TREE_CODE (t
);
2515 gcc_checking_assert (code
!= TREE_BINFO
);
2516 LTO_NO_PREVAIL (TREE_TYPE (t
));
2517 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
)
2518 /* lto_symtab_prevail_decl use TREE_CHAIN to link to the prevailing decl.
2519 in the case T is a prevailed declaration we would ICE here. */
2520 && !VAR_OR_FUNCTION_DECL_P (t
))
2521 LTO_NO_PREVAIL (TREE_CHAIN (t
));
2524 LTO_NO_PREVAIL (DECL_NAME (t
));
2525 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
2526 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
2528 LTO_SET_PREVAIL (DECL_SIZE (t
));
2529 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
2530 LTO_SET_PREVAIL (DECL_INITIAL (t
));
2531 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
2532 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
2534 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
2536 LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t
));
2538 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
2540 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
2542 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
2544 LTO_NO_PREVAIL (DECL_ARGUMENTS (t
));
2545 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
2546 LTO_NO_PREVAIL (DECL_VINDEX (t
));
2548 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
2550 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t
));
2551 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
2552 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
2553 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
2554 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
2557 else if (TYPE_P (t
))
2559 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
2560 LTO_SET_PREVAIL (TYPE_SIZE (t
));
2561 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
2562 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
2563 LTO_NO_PREVAIL (TYPE_NAME (t
));
2565 LTO_SET_PREVAIL (TYPE_MIN_VALUE_RAW (t
));
2566 LTO_SET_PREVAIL (TYPE_MAX_VALUE_RAW (t
));
2567 LTO_NO_PREVAIL (TYPE_LANG_SLOT_1 (t
));
2569 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
2571 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
2572 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
2573 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
2575 else if (EXPR_P (t
))
2578 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
2579 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
2581 else if (TREE_CODE (t
) == CONSTRUCTOR
)
2585 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
2586 LTO_SET_PREVAIL (val
);
2593 LTO_SET_PREVAIL (TREE_VALUE (t
));
2594 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
2595 LTO_NO_PREVAIL (TREE_PURPOSE (t
));
2601 /* If we fixed nothing, then we missed something seen by
2603 gcc_checking_assert (fixed
);
2605 #undef LTO_SET_PREVAIL
2606 #undef LTO_NO_PREVAIL
2608 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2609 replaces var and function decls with the corresponding prevailing def. */
2612 lto_fixup_state (struct lto_in_decl_state
*state
)
2616 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2617 we still need to walk from all DECLs to find the reachable
2618 FUNCTION_DECLs and VAR_DECLs. */
2619 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
2621 vec
<tree
, va_gc
> *trees
= state
->streams
[si
];
2622 for (i
= 0; i
< vec_safe_length (trees
); i
++)
2624 tree t
= (*trees
)[i
];
2625 if (flag_checking
&& TYPE_P (t
))
2627 if (VAR_OR_FUNCTION_DECL_P (t
)
2628 && (TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
2629 (*trees
)[i
] = lto_symtab_prevailing_decl (t
);
2634 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2638 lto_fixup_decls (struct lto_file_decl_data
**files
)
2644 FOR_EACH_VEC_ELT ((*tree_with_vars
), i
, t
)
2645 lto_fixup_prevailing_decls (t
);
2647 for (i
= 0; files
[i
]; i
++)
2649 struct lto_file_decl_data
*file
= files
[i
];
2650 struct lto_in_decl_state
*state
= file
->global_decl_state
;
2651 lto_fixup_state (state
);
2653 hash_table
<decl_state_hasher
>::iterator iter
;
2654 lto_in_decl_state
*elt
;
2655 FOR_EACH_HASH_TABLE_ELEMENT (*file
->function_decl_states
, elt
,
2656 lto_in_decl_state
*, iter
)
2657 lto_fixup_state (elt
);
2661 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
2663 /* Turn file datas for sub files into a single array, so that they look
2664 like separate files for further passes. */
2667 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
,
2670 struct lto_file_decl_data
*n
, *next
;
2673 lto_stats
.num_input_files
= count
;
2675 = ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (count
+ 1);
2676 /* Set the hooks so that all of the ipa passes can read in their data. */
2677 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2678 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
2680 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
2682 all_file_decl_data
[k
++] = n
;
2687 all_file_decl_data
[k
] = NULL
;
2688 gcc_assert (k
== count
);
2691 /* Input file data before flattening (i.e. splitting them to subfiles to support
2692 incremental linking. */
2693 static int real_file_count
;
2694 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
2696 /* Read all the symbols from the input files FNAMES. NFILES is the
2697 number of files requested in the command line. Instantiate a
2698 global call graph by aggregating all the sub-graphs found in each
2702 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
2704 unsigned int i
, last_file_ix
;
2707 struct lto_file_decl_data
**decl_data
;
2710 symtab
->initialize ();
2712 timevar_push (TV_IPA_LTO_DECL_IN
);
2714 #ifdef ACCEL_COMPILER
2715 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2716 lto_stream_offload_p
= true;
2720 = decl_data
= ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (nfiles
+ 1);
2721 real_file_count
= nfiles
;
2723 /* Read the resolution file. */
2725 if (resolution_file_name
)
2728 unsigned num_objects
;
2730 resolution
= fopen (resolution_file_name
, "r");
2731 if (resolution
== NULL
)
2732 fatal_error (input_location
,
2733 "could not open symbol resolution file: %m");
2735 t
= fscanf (resolution
, "%u", &num_objects
);
2736 gcc_assert (t
== 1);
2738 /* True, since the plugin splits the archives. */
2739 gcc_assert (num_objects
== nfiles
);
2741 symtab
->state
= LTO_STREAMING
;
2743 canonical_type_hash_cache
= new hash_map
<const_tree
, hashval_t
> (251);
2744 gimple_canonical_types
= htab_create (16381, gimple_canonical_type_hash
,
2745 gimple_canonical_type_eq
, NULL
);
2746 gcc_obstack_init (&tree_scc_hash_obstack
);
2747 tree_scc_hash
= new hash_table
<tree_scc_hasher
> (4096);
2749 /* Register the common node types with the canonical type machinery so
2750 we properly share alias-sets across languages and TUs. Do not
2751 expose the common nodes as type merge target - those that should be
2752 are already exposed so by pre-loading the LTO streamer caches.
2753 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2754 for (i
= 0; i
< itk_none
; ++i
)
2755 lto_register_canonical_types (integer_types
[i
], true);
2756 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2757 lto_register_canonical_types (sizetype_tab
[i
], true);
2758 for (i
= 0; i
< TI_MAX
; ++i
)
2759 lto_register_canonical_types (global_trees
[i
], true);
2760 for (i
= 0; i
< itk_none
; ++i
)
2761 lto_register_canonical_types (integer_types
[i
], false);
2762 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2763 lto_register_canonical_types (sizetype_tab
[i
], false);
2764 for (i
= 0; i
< TI_MAX
; ++i
)
2765 lto_register_canonical_types (global_trees
[i
], false);
2768 fprintf (stderr
, "Reading object files:");
2770 /* Read all of the object files specified on the command line. */
2771 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
2773 struct lto_file_decl_data
*file_data
= NULL
;
2776 fprintf (stderr
, " %s", fnames
[i
]);
2780 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
2781 if (!current_lto_file
)
2784 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
2787 lto_obj_file_close (current_lto_file
);
2788 free (current_lto_file
);
2789 current_lto_file
= NULL
;
2793 decl_data
[last_file_ix
++] = file_data
;
2795 lto_obj_file_close (current_lto_file
);
2796 free (current_lto_file
);
2797 current_lto_file
= NULL
;
2800 lto_flatten_files (decl_data
, count
, last_file_ix
);
2801 lto_stats
.num_input_files
= count
;
2802 ggc_free(decl_data
);
2803 real_file_decl_data
= NULL
;
2805 lto_register_canonical_types_for_odr_types ();
2807 if (resolution_file_name
)
2808 fclose (resolution
);
2810 /* Show the LTO report before launching LTRANS. */
2811 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
2812 print_lto_report_1 ();
2814 /* Free gimple type merging datastructures. */
2815 delete tree_scc_hash
;
2816 tree_scc_hash
= NULL
;
2817 obstack_free (&tree_scc_hash_obstack
, NULL
);
2818 htab_delete (gimple_canonical_types
);
2819 gimple_canonical_types
= NULL
;
2820 delete canonical_type_hash_cache
;
2821 canonical_type_hash_cache
= NULL
;
2823 /* At this stage we know that majority of GGC memory is reachable.
2824 Growing the limits prevents unnecesary invocation of GGC. */
2826 report_heap_memory_use ();
2828 /* Set the hooks so that all of the ipa passes can read in their data. */
2829 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2831 timevar_pop (TV_IPA_LTO_DECL_IN
);
2834 fprintf (stderr
, "\nReading the symbol table:");
2836 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
2837 /* Read the symtab. */
2840 input_offload_tables (!flag_ltrans
);
2842 /* Store resolutions into the symbol table. */
2844 FOR_EACH_SYMBOL (snode
)
2845 if (snode
->externally_visible
&& snode
->real_symbol_p ()
2846 && snode
->lto_file_data
&& snode
->lto_file_data
->resolution_map
2847 && !(TREE_CODE (snode
->decl
) == FUNCTION_DECL
2848 && fndecl_built_in_p (snode
->decl
))
2849 && !(VAR_P (snode
->decl
) && DECL_HARD_REGISTER (snode
->decl
)))
2851 ld_plugin_symbol_resolution_t
*res
;
2853 res
= snode
->lto_file_data
->resolution_map
->get (snode
->decl
);
2854 if (!res
|| *res
== LDPR_UNKNOWN
)
2856 if (snode
->output_to_lto_symbol_table_p ())
2857 fatal_error (input_location
, "missing resolution data for %s",
2859 (DECL_ASSEMBLER_NAME (snode
->decl
)));
2861 /* Symbol versions are always used externally, but linker does not
2862 report that correctly.
2863 This is binutils PR25924. */
2864 else if (snode
->symver
&& *res
== LDPR_PREVAILING_DEF_IRONLY
)
2865 snode
->resolution
= LDPR_PREVAILING_DEF_IRONLY_EXP
;
2867 snode
->resolution
= *res
;
2869 for (i
= 0; all_file_decl_data
[i
]; i
++)
2870 if (all_file_decl_data
[i
]->resolution_map
)
2872 delete all_file_decl_data
[i
]->resolution_map
;
2873 all_file_decl_data
[i
]->resolution_map
= NULL
;
2876 timevar_pop (TV_IPA_LTO_CGRAPH_IO
);
2879 fprintf (stderr
, "\nMerging declarations:");
2881 timevar_push (TV_IPA_LTO_DECL_MERGE
);
2882 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2883 need to care about resolving symbols again, we only need to replace
2884 duplicated declarations read from the callgraph and from function
2888 lto_symtab_merge_decls ();
2890 /* If there were errors during symbol merging bail out, we have no
2891 good way to recover here. */
2893 fatal_error (input_location
,
2894 "errors during merging of translation units");
2896 /* Fixup all decls. */
2897 lto_fixup_decls (all_file_decl_data
);
2900 ggc_free (tree_with_vars
);
2901 tree_with_vars
= NULL
;
2902 /* During WPA we want to prevent ggc collecting by default. Grow limits
2903 until after the IPA summaries are streamed in. Basically all IPA memory
2904 is explcitly managed by ggc_free and ggc collect is not useful.
2905 Exception are the merged declarations. */
2907 report_heap_memory_use ();
2909 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
2910 /* Each pass will set the appropriate timer. */
2913 fprintf (stderr
, "\nReading summaries:");
2915 /* Read the IPA summary data. */
2917 ipa_read_optimization_summaries ();
2919 ipa_read_summaries ();
2923 for (i
= 0; all_file_decl_data
[i
]; i
++)
2925 gcc_assert (all_file_decl_data
[i
]->symtab_node_encoder
);
2926 lto_symtab_encoder_delete (all_file_decl_data
[i
]->symtab_node_encoder
);
2927 all_file_decl_data
[i
]->symtab_node_encoder
= NULL
;
2928 lto_in_decl_state
*global_decl_state
2929 = all_file_decl_data
[i
]->global_decl_state
;
2930 lto_free_function_in_decl_state (global_decl_state
);
2931 all_file_decl_data
[i
]->global_decl_state
= NULL
;
2932 all_file_decl_data
[i
]->current_decl_state
= NULL
;
2937 /* Finally merge the cgraph according to the decl merging decisions. */
2938 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
2941 fprintf (stderr
, "\nMerging symbols:");
2943 gcc_assert (!dump_file
);
2944 dump_file
= dump_begin (lto_link_dump_id
, NULL
);
2948 fprintf (dump_file
, "Before merging:\n");
2949 symtab
->dump (dump_file
);
2951 lto_symtab_merge_symbols ();
2952 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2953 we are still having duplicated comdat groups containing local statics.
2954 We could also just remove them while merging. */
2955 symtab
->remove_unreachable_nodes (dump_file
);
2957 report_heap_memory_use ();
2960 dump_end (lto_link_dump_id
, dump_file
);
2962 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
2964 symtab
->state
= IPA_SSA
;
2965 /* All node removals happening here are useless, because
2966 WPA should not stream them. Still always perform remove_unreachable_nodes
2967 because we may reshape clone tree, get rid of dead masters of inline
2968 clones and remove symbol entries for read-only variables we keep around
2969 only to be able to constant fold them. */
2972 if (symtab
->dump_file
)
2973 symtab
->dump (symtab
->dump_file
);
2974 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2977 /* Indicate that the cgraph is built and ready. */
2978 symtab
->function_flags_ready
= true;
2980 ggc_free (all_file_decl_data
);
2981 all_file_decl_data
= NULL
;
2986 /* Show various memory usage statistics related to LTO. */
2988 print_lto_report_1 (void)
2990 const char *pfx
= (flag_lto
) ? "LTO" : (flag_wpa
) ? "WPA" : "LTRANS";
2991 fprintf (stderr
, "%s statistics\n", pfx
);
2993 fprintf (stderr
, "[%s] read %lu unshared trees\n",
2994 pfx
, num_unshared_trees_read
);
2995 fprintf (stderr
, "[%s] read %lu mergeable SCCs of average size %f\n",
2996 pfx
, num_sccs_read
, total_scc_size
/ (double)num_sccs_read
);
2997 fprintf (stderr
, "[%s] %lu tree bodies read in total\n", pfx
,
2998 total_scc_size
+ num_unshared_trees_read
);
2999 if (flag_wpa
&& tree_scc_hash
&& num_sccs_read
)
3001 fprintf (stderr
, "[%s] tree SCC table: size %ld, %ld elements, "
3002 "collision ratio: %f\n", pfx
,
3003 (long) tree_scc_hash
->size (),
3004 (long) tree_scc_hash
->elements (),
3005 tree_scc_hash
->collisions ());
3006 hash_table
<tree_scc_hasher
>::iterator hiter
;
3007 tree_scc
*scc
, *max_scc
= NULL
;
3008 unsigned max_length
= 0;
3009 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash
, scc
, x
, hiter
)
3011 unsigned length
= 0;
3013 for (; s
; s
= s
->next
)
3015 if (length
> max_length
)
3017 max_length
= length
;
3021 fprintf (stderr
, "[%s] tree SCC max chain length %u (size %u)\n",
3022 pfx
, max_length
, max_scc
->len
);
3023 fprintf (stderr
, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx
,
3024 num_scc_compares
, num_scc_compare_collisions
,
3025 num_scc_compare_collisions
/ (double) num_scc_compares
);
3026 fprintf (stderr
, "[%s] Merged %lu SCCs\n", pfx
, num_sccs_merged
);
3027 fprintf (stderr
, "[%s] Merged %lu tree bodies\n", pfx
,
3028 total_scc_size_merged
);
3029 fprintf (stderr
, "[%s] Merged %lu types\n", pfx
, num_merged_types
);
3030 fprintf (stderr
, "[%s] %lu types prevailed (%lu associated trees)\n",
3031 pfx
, num_prevailing_types
, num_type_scc_trees
);
3032 fprintf (stderr
, "[%s] GIMPLE canonical type table: size %ld, "
3033 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx
,
3034 (long) htab_size (gimple_canonical_types
),
3035 (long) htab_elements (gimple_canonical_types
),
3036 (long) gimple_canonical_types
->searches
,
3037 (long) gimple_canonical_types
->collisions
,
3038 htab_collisions (gimple_canonical_types
));
3039 fprintf (stderr
, "[%s] GIMPLE canonical type pointer-map: "
3040 "%lu elements, %ld searches\n", pfx
,
3041 num_canonical_type_hash_entries
,
3042 num_canonical_type_hash_queries
);
3045 print_lto_report (pfx
);
3048 GTY(()) tree lto_eh_personality_decl
;
3050 /* Return the LTO personality function decl. */
3053 lto_eh_personality (void)
3055 if (!lto_eh_personality_decl
)
3057 /* Use the first personality DECL for our personality if we don't
3058 support multiple ones. This ensures that we don't artificially
3059 create the need for them in a single-language program. */
3060 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
3061 lto_eh_personality_decl
= first_personality_decl
;
3063 lto_eh_personality_decl
= lhd_gcc_personality ();
3066 return lto_eh_personality_decl
;
3069 /* Set the process name based on the LTO mode. */
3072 lto_process_name (void)
3075 setproctitle (flag_incremental_link
== INCREMENTAL_LINK_LTO
3076 ? "lto1-inclink" : "lto1-lto");
3078 setproctitle ("lto1-wpa");
3080 setproctitle ("lto1-ltrans");
3084 /* Initialize the LTO front end. */
3089 lto_process_name ();
3090 lto_streamer_hooks_init ();
3092 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
3093 memset (<o_stats
, 0, sizeof (lto_stats
));
3094 bitmap_obstack_initialize (NULL
);
3095 gimple_register_cfg_hooks ();
3096 #ifndef ACCEL_COMPILER
3097 unsigned char *table
3098 = ggc_vec_alloc
<unsigned char> (MAX_MACHINE_MODE
);
3099 for (int m
= 0; m
< MAX_MACHINE_MODE
; m
++)
3101 lto_mode_identity_table
= table
;
3105 #include "gt-lto-lto-common.h"