1 /* Top-level LTO routines.
2 Copyright (C) 2009-2022 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
) && odr_type_p (t
)
419 && TYPE_CXX_ODR_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 gcc_checking_assert (!flag_ltrans
);
438 if (symtab
->dump_file
)
440 fprintf (symtab
->dump_file
,
441 "ODR and non-ODR type conflict: ");
442 print_generic_expr (symtab
->dump_file
, t
);
443 fprintf (symtab
->dump_file
, " and ");
444 print_generic_expr (symtab
->dump_file
, nonodr
);
445 fprintf (symtab
->dump_file
, " mangled:%s\n",
447 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
))));
449 /* Set canonical for T and all other ODR equivalent duplicates
450 including incomplete structures. */
451 set_type_canonical_for_odr_type (t
, nonodr
);
455 tree prevail
= prevailing_odr_type (t
);
457 if (symtab
->dump_file
)
459 fprintf (symtab
->dump_file
,
460 "New canonical ODR type: ");
461 print_generic_expr (symtab
->dump_file
, t
);
462 fprintf (symtab
->dump_file
, " mangled:%s\n",
464 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
))));
466 /* Set canonical for T and all other ODR equivalent duplicates
467 including incomplete structures. */
468 set_type_canonical_for_odr_type (t
, prevail
);
469 enable_odr_based_tbaa (t
);
470 if (!type_in_anonymous_namespace_p (t
))
471 hash
= htab_hash_string (IDENTIFIER_POINTER
477 /* All variants of t now have TYPE_CANONICAL set to prevail.
478 Update canonical type hash cache accordingly. */
479 num_canonical_type_hash_entries
++;
480 bool existed_p
= canonical_type_hash_cache
->put (prevail
, hash
);
481 gcc_checking_assert (!existed_p
);
486 slot
= htab_find_slot_with_hash (gimple_canonical_types
, t
, hash
, INSERT
);
489 tree new_type
= (tree
)(*slot
);
490 gcc_checking_assert (new_type
!= t
);
491 TYPE_CANONICAL (t
) = new_type
;
495 TYPE_CANONICAL (t
) = t
;
497 /* Cache the just computed hash value. */
498 num_canonical_type_hash_entries
++;
499 bool existed_p
= canonical_type_hash_cache
->put (t
, hash
);
500 gcc_assert (!existed_p
);
505 /* Register type T in the global type table gimple_types and set
506 TYPE_CANONICAL of T accordingly.
507 This is used by LTO to merge structurally equivalent types for
508 type-based aliasing purposes across different TUs and languages.
510 ??? This merging does not exactly match how the tree.cc middle-end
511 functions will assign TYPE_CANONICAL when new types are created
512 during optimization (which at least happens for pointer and array
516 gimple_register_canonical_type (tree t
)
518 if (TYPE_CANONICAL (t
) || !type_with_alias_set_p (t
)
519 || !canonical_type_used_p (t
))
522 /* Canonical types are same among all complete variants. */
523 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
)))
524 TYPE_CANONICAL (t
) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
));
527 hashval_t h
= hash_canonical_type (TYPE_MAIN_VARIANT (t
));
528 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t
), h
);
529 TYPE_CANONICAL (t
) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
));
533 /* Re-compute TYPE_CANONICAL for NODE and related types. */
536 lto_register_canonical_types (tree node
, bool first_p
)
543 TYPE_CANONICAL (node
) = NULL_TREE
;
545 if (POINTER_TYPE_P (node
)
546 || TREE_CODE (node
) == COMPLEX_TYPE
547 || TREE_CODE (node
) == ARRAY_TYPE
)
548 lto_register_canonical_types (TREE_TYPE (node
), first_p
);
551 gimple_register_canonical_type (node
);
554 /* Finish canonical type calculation: after all units has been streamed in we
555 can check if given ODR type structurally conflicts with a non-ODR type. In
556 the first case we set type canonical according to the canonical type hash.
557 In the second case we use type names. */
560 lto_register_canonical_types_for_odr_types ()
565 if (!types_to_register
)
568 type_streaming_finished
= true;
570 /* Be sure that no types derived from ODR types was
571 not inserted into the hash table. */
573 FOR_EACH_VEC_ELT (*types_to_register
, i
, t
)
574 gcc_assert (!TYPE_CANONICAL (t
));
576 /* Register all remaining types. */
577 FOR_EACH_VEC_ELT (*types_to_register
, i
, t
)
579 /* For pre-streamed types like va-arg it is possible that main variant
580 is !CXX_ODR_P while the variant (which is streamed) is.
581 Copy CXX_ODR_P to make type verifier happy. This is safe because
582 in canonical type calculation we only consider main variants.
583 However we can not change this flag before streaming is finished
584 to not affect tree merging. */
585 TYPE_CXX_ODR_P (t
) = TYPE_CXX_ODR_P (TYPE_MAIN_VARIANT (t
));
586 if (!TYPE_CANONICAL (t
))
587 gimple_register_canonical_type (t
);
592 /* Remember trees that contains references to declarations. */
593 vec
<tree
, va_gc
> *tree_with_vars
;
595 #define CHECK_VAR(tt) \
598 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
599 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
603 #define CHECK_NO_VAR(tt) \
604 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
606 /* Check presence of pointers to decls in fields of a tree_typed T. */
609 mentions_vars_p_typed (tree t
)
611 CHECK_NO_VAR (TREE_TYPE (t
));
615 /* Check presence of pointers to decls in fields of a tree_common T. */
618 mentions_vars_p_common (tree t
)
620 if (mentions_vars_p_typed (t
))
622 CHECK_NO_VAR (TREE_CHAIN (t
));
626 /* Check presence of pointers to decls in fields of a decl_minimal T. */
629 mentions_vars_p_decl_minimal (tree t
)
631 if (mentions_vars_p_common (t
))
633 CHECK_NO_VAR (DECL_NAME (t
));
634 CHECK_VAR (DECL_CONTEXT (t
));
638 /* Check presence of pointers to decls in fields of a decl_common T. */
641 mentions_vars_p_decl_common (tree t
)
643 if (mentions_vars_p_decl_minimal (t
))
645 CHECK_VAR (DECL_SIZE (t
));
646 CHECK_VAR (DECL_SIZE_UNIT (t
));
647 CHECK_VAR (DECL_INITIAL (t
));
648 CHECK_NO_VAR (DECL_ATTRIBUTES (t
));
649 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t
));
653 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
656 mentions_vars_p_decl_with_vis (tree t
)
658 if (mentions_vars_p_decl_common (t
))
661 /* Accessor macro has side-effects, use field-name here. */
662 CHECK_NO_VAR (DECL_ASSEMBLER_NAME_RAW (t
));
666 /* Check presence of pointers to decls in fields of a decl_non_common T. */
669 mentions_vars_p_decl_non_common (tree t
)
671 if (mentions_vars_p_decl_with_vis (t
))
673 CHECK_NO_VAR (DECL_RESULT_FLD (t
));
677 /* Check presence of pointers to decls in fields of a decl_non_common T. */
680 mentions_vars_p_function (tree t
)
682 if (mentions_vars_p_decl_non_common (t
))
684 CHECK_NO_VAR (DECL_ARGUMENTS (t
));
685 CHECK_NO_VAR (DECL_VINDEX (t
));
686 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t
));
690 /* Check presence of pointers to decls in fields of a field_decl T. */
693 mentions_vars_p_field_decl (tree t
)
695 if (mentions_vars_p_decl_common (t
))
697 CHECK_VAR (DECL_FIELD_OFFSET (t
));
698 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t
));
699 CHECK_NO_VAR (DECL_QUALIFIER (t
));
700 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t
));
701 CHECK_NO_VAR (DECL_FCONTEXT (t
));
705 /* Check presence of pointers to decls in fields of a type T. */
708 mentions_vars_p_type (tree t
)
710 if (mentions_vars_p_common (t
))
712 CHECK_NO_VAR (TYPE_CACHED_VALUES (t
));
713 CHECK_VAR (TYPE_SIZE (t
));
714 CHECK_VAR (TYPE_SIZE_UNIT (t
));
715 CHECK_NO_VAR (TYPE_ATTRIBUTES (t
));
716 CHECK_NO_VAR (TYPE_NAME (t
));
718 CHECK_VAR (TYPE_MIN_VALUE_RAW (t
));
719 CHECK_VAR (TYPE_MAX_VALUE_RAW (t
));
721 /* Accessor is for derived node types only. */
722 CHECK_NO_VAR (TYPE_LANG_SLOT_1 (t
));
724 CHECK_VAR (TYPE_CONTEXT (t
));
725 CHECK_NO_VAR (TYPE_CANONICAL (t
));
726 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t
));
727 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t
));
731 /* Check presence of pointers to decls in fields of a BINFO T. */
734 mentions_vars_p_binfo (tree t
)
736 unsigned HOST_WIDE_INT i
, n
;
738 if (mentions_vars_p_common (t
))
740 CHECK_VAR (BINFO_VTABLE (t
));
741 CHECK_NO_VAR (BINFO_OFFSET (t
));
742 CHECK_NO_VAR (BINFO_VIRTUALS (t
));
743 CHECK_NO_VAR (BINFO_VPTR_FIELD (t
));
744 n
= vec_safe_length (BINFO_BASE_ACCESSES (t
));
745 for (i
= 0; i
< n
; i
++)
746 CHECK_NO_VAR (BINFO_BASE_ACCESS (t
, i
));
747 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
748 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
749 n
= BINFO_N_BASE_BINFOS (t
);
750 for (i
= 0; i
< n
; i
++)
751 CHECK_NO_VAR (BINFO_BASE_BINFO (t
, i
));
755 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
758 mentions_vars_p_constructor (tree t
)
760 unsigned HOST_WIDE_INT idx
;
763 if (mentions_vars_p_typed (t
))
766 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
768 CHECK_NO_VAR (ce
->index
);
769 CHECK_VAR (ce
->value
);
774 /* Check presence of pointers to decls in fields of an expression tree T. */
777 mentions_vars_p_expr (tree t
)
780 if (mentions_vars_p_typed (t
))
782 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
783 CHECK_VAR (TREE_OPERAND (t
, i
));
787 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
790 mentions_vars_p_omp_clause (tree t
)
793 if (mentions_vars_p_common (t
))
795 for (i
= omp_clause_num_ops
[OMP_CLAUSE_CODE (t
)] - 1; i
>= 0; --i
)
796 CHECK_VAR (OMP_CLAUSE_OPERAND (t
, i
));
800 /* Check presence of pointers to decls that needs later fixup in T. */
803 mentions_vars_p (tree t
)
805 switch (TREE_CODE (t
))
807 case IDENTIFIER_NODE
:
811 CHECK_VAR (TREE_VALUE (t
));
812 CHECK_VAR (TREE_PURPOSE (t
));
813 CHECK_NO_VAR (TREE_CHAIN (t
));
817 return mentions_vars_p_field_decl (t
);
826 return mentions_vars_p_decl_common (t
);
829 return mentions_vars_p_decl_with_vis (t
);
832 return mentions_vars_p_decl_non_common (t
);
835 return mentions_vars_p_function (t
);
838 return mentions_vars_p_binfo (t
);
840 case PLACEHOLDER_EXPR
:
841 return mentions_vars_p_common (t
);
844 case TRANSLATION_UNIT_DECL
:
845 case OPTIMIZATION_NODE
:
846 case TARGET_OPTION_NODE
:
850 return mentions_vars_p_constructor (t
);
853 return mentions_vars_p_omp_clause (t
);
858 if (mentions_vars_p_type (t
))
863 if (mentions_vars_p_expr (t
))
866 else if (CONSTANT_CLASS_P (t
))
867 CHECK_NO_VAR (TREE_TYPE (t
));
875 /* Return the resolution for the decl with index INDEX from DATA_IN. */
877 static enum ld_plugin_symbol_resolution
878 get_resolution (class data_in
*data_in
, unsigned index
)
880 if (data_in
->globals_resolution
.exists ())
882 ld_plugin_symbol_resolution_t ret
;
883 /* We can have references to not emitted functions in
884 DECL_FUNCTION_PERSONALITY at least. So we can and have
885 to indeed return LDPR_UNKNOWN in some cases. */
886 if (data_in
->globals_resolution
.length () <= index
)
888 ret
= data_in
->globals_resolution
[index
];
892 /* Delay resolution finding until decl merging. */
896 /* We need to record resolutions until symbol table is read. */
898 register_resolution (struct lto_file_decl_data
*file_data
, tree decl
,
899 enum ld_plugin_symbol_resolution resolution
)
902 if (resolution
== LDPR_UNKNOWN
)
904 if (!file_data
->resolution_map
)
905 file_data
->resolution_map
906 = new hash_map
<tree
, ld_plugin_symbol_resolution
>;
907 ld_plugin_symbol_resolution_t
&res
908 = file_data
->resolution_map
->get_or_insert (decl
, &existed
);
910 || resolution
== LDPR_PREVAILING_DEF_IRONLY
911 || resolution
== LDPR_PREVAILING_DEF
912 || resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
916 /* Register DECL with the global symbol table and change its
917 name if necessary to avoid name clashes for static globals across
921 lto_register_var_decl_in_symtab (class data_in
*data_in
, tree decl
,
926 /* Variable has file scope, not local. */
927 if (!TREE_PUBLIC (decl
)
928 && !((context
= decl_function_context (decl
))
929 && auto_var_in_fn_p (decl
, context
)))
930 rest_of_decl_compilation (decl
, 1, 0);
932 /* If this variable has already been declared, queue the
933 declaration for merging. */
934 if (TREE_PUBLIC (decl
))
935 register_resolution (data_in
->file_data
,
936 decl
, get_resolution (data_in
, ix
));
940 /* Register DECL with the global symbol table and change its
941 name if necessary to avoid name clashes for static globals across
942 different files. DATA_IN contains descriptors and tables for the
946 lto_register_function_decl_in_symtab (class data_in
*data_in
, tree decl
,
949 /* If this variable has already been declared, queue the
950 declaration for merging. */
951 if (TREE_PUBLIC (decl
) && !DECL_ABSTRACT_P (decl
))
952 register_resolution (data_in
->file_data
,
953 decl
, get_resolution (data_in
, ix
));
956 /* Check if T is a decl and needs register its resolution info. */
959 lto_maybe_register_decl (class data_in
*data_in
, tree t
, unsigned ix
)
961 if (TREE_CODE (t
) == VAR_DECL
)
962 lto_register_var_decl_in_symtab (data_in
, t
, ix
);
963 else if (TREE_CODE (t
) == FUNCTION_DECL
964 && !fndecl_built_in_p (t
))
965 lto_register_function_decl_in_symtab (data_in
, t
, ix
);
969 /* For the type T re-materialize it in the type variant list and
970 the pointer/reference-to chains. */
973 lto_fixup_prevailing_type (tree t
)
975 /* The following re-creates proper variant lists while fixing up
976 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
977 variant list state before fixup is broken. */
979 /* If we are not our own variant leader link us into our new leaders
981 if (TYPE_MAIN_VARIANT (t
) != t
)
983 tree mv
= TYPE_MAIN_VARIANT (t
);
984 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
985 TYPE_NEXT_VARIANT (mv
) = t
;
988 /* The following reconstructs the pointer chains
989 of the new pointed-to type if we are a main variant. We do
990 not stream those so they are broken before fixup. */
991 if (TREE_CODE (t
) == POINTER_TYPE
992 && TYPE_MAIN_VARIANT (t
) == t
)
994 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
995 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
997 else if (TREE_CODE (t
) == REFERENCE_TYPE
998 && TYPE_MAIN_VARIANT (t
) == t
)
1000 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
1001 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
1006 /* We keep prevailing tree SCCs in a hashtable with manual collision
1007 handling (in case all hashes compare the same) and keep the colliding
1008 entries in the tree_scc->next chain. */
1013 /* Hash of the whole SCC. */
1015 /* Number of trees in the SCC. */
1017 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1018 which share the same individual tree hash). */
1020 /* The members of the SCC.
1021 We only need to remember the first entry node candidate for prevailing
1022 SCCs (but of course have access to all entries for SCCs we are
1024 ??? For prevailing SCCs we really only need hash and the first
1025 entry candidate, but that's too awkward to implement. */
1029 struct tree_scc_hasher
: nofree_ptr_hash
<tree_scc
>
1031 static inline hashval_t
hash (const tree_scc
*);
1032 static inline bool equal (const tree_scc
*, const tree_scc
*);
1036 tree_scc_hasher::hash (const tree_scc
*scc
)
1042 tree_scc_hasher::equal (const tree_scc
*scc1
, const tree_scc
*scc2
)
1044 if (scc1
->hash
!= scc2
->hash
1045 || scc1
->len
!= scc2
->len
1046 || scc1
->entry_len
!= scc2
->entry_len
)
1051 static hash_table
<tree_scc_hasher
> *tree_scc_hash
;
1052 static struct obstack tree_scc_hash_obstack
;
1054 static unsigned long num_merged_types
;
1055 static unsigned long num_prevailing_types
;
1056 static unsigned long num_type_scc_trees
;
1057 static unsigned long total_scc_size
;
1058 static unsigned long num_sccs_read
;
1059 static unsigned long num_unshared_trees_read
;
1060 static unsigned long total_scc_size_merged
;
1061 static unsigned long num_sccs_merged
;
1062 static unsigned long num_scc_compares
;
1063 static unsigned long num_scc_compare_collisions
;
1066 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1067 recursing through in-SCC tree edges. Returns true if the SCCs entered
1068 through T1 and T2 are equal and fills in *MAP with the pairs of
1069 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1072 compare_tree_sccs_1 (tree t1
, tree t2
, tree
**map
)
1074 enum tree_code code
;
1076 /* Mark already visited nodes. */
1077 TREE_ASM_WRITTEN (t2
) = 1;
1079 /* Push the pair onto map. */
1084 /* Compare value-fields. */
1085 #define compare_values(X) \
1087 if (X(t1) != X(t2)) \
1091 compare_values (TREE_CODE
);
1092 code
= TREE_CODE (t1
);
1094 /* If we end up comparing translation unit decls we either forgot to mark
1095 some SCC as local or we compare too much. */
1096 gcc_checking_assert (code
!= TRANSLATION_UNIT_DECL
);
1100 compare_values (TREE_SIDE_EFFECTS
);
1101 compare_values (TREE_CONSTANT
);
1102 compare_values (TREE_READONLY
);
1103 compare_values (TREE_PUBLIC
);
1105 compare_values (TREE_ADDRESSABLE
);
1106 compare_values (TREE_THIS_VOLATILE
);
1108 compare_values (DECL_UNSIGNED
);
1109 else if (TYPE_P (t1
))
1110 compare_values (TYPE_UNSIGNED
);
1112 compare_values (TYPE_ARTIFICIAL
);
1114 compare_values (TREE_NO_WARNING
);
1115 compare_values (TREE_NOTHROW
);
1116 compare_values (TREE_STATIC
);
1117 if (code
!= TREE_BINFO
)
1118 compare_values (TREE_PRIVATE
);
1119 compare_values (TREE_PROTECTED
);
1120 compare_values (TREE_DEPRECATED
);
1123 if (AGGREGATE_TYPE_P (t1
))
1124 compare_values (TYPE_REVERSE_STORAGE_ORDER
);
1126 compare_values (TYPE_SATURATING
);
1127 compare_values (TYPE_ADDR_SPACE
);
1129 else if (code
== SSA_NAME
)
1130 compare_values (SSA_NAME_IS_DEFAULT_DEF
);
1132 if (CODE_CONTAINS_STRUCT (code
, TS_INT_CST
))
1134 if (wi::to_wide (t1
) != wi::to_wide (t2
))
1138 if (CODE_CONTAINS_STRUCT (code
, TS_REAL_CST
))
1140 /* ??? No suitable compare routine available. */
1141 REAL_VALUE_TYPE r1
= TREE_REAL_CST (t1
);
1142 REAL_VALUE_TYPE r2
= TREE_REAL_CST (t2
);
1144 || r1
.decimal
!= r2
.decimal
1145 || r1
.sign
!= r2
.sign
1146 || r1
.signalling
!= r2
.signalling
1147 || r1
.canonical
!= r2
.canonical
1148 || r1
.uexp
!= r2
.uexp
)
1150 for (unsigned i
= 0; i
< SIGSZ
; ++i
)
1151 if (r1
.sig
[i
] != r2
.sig
[i
])
1155 if (CODE_CONTAINS_STRUCT (code
, TS_FIXED_CST
))
1156 if (!fixed_compare (EQ_EXPR
,
1157 TREE_FIXED_CST_PTR (t1
), TREE_FIXED_CST_PTR (t2
)))
1160 if (CODE_CONTAINS_STRUCT (code
, TS_VECTOR
))
1162 compare_values (VECTOR_CST_LOG2_NPATTERNS
);
1163 compare_values (VECTOR_CST_NELTS_PER_PATTERN
);
1166 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1168 compare_values (DECL_MODE
);
1169 compare_values (DECL_NONLOCAL
);
1170 compare_values (DECL_VIRTUAL_P
);
1171 compare_values (DECL_IGNORED_P
);
1172 compare_values (DECL_ABSTRACT_P
);
1173 compare_values (DECL_ARTIFICIAL
);
1174 compare_values (DECL_USER_ALIGN
);
1175 compare_values (DECL_PRESERVE_P
);
1176 compare_values (DECL_EXTERNAL
);
1177 compare_values (DECL_NOT_GIMPLE_REG_P
);
1178 compare_values (DECL_ALIGN
);
1179 if (code
== LABEL_DECL
)
1181 compare_values (EH_LANDING_PAD_NR
);
1182 compare_values (LABEL_DECL_UID
);
1184 else if (code
== FIELD_DECL
)
1186 compare_values (DECL_PACKED
);
1187 compare_values (DECL_NONADDRESSABLE_P
);
1188 compare_values (DECL_PADDING_P
);
1189 compare_values (DECL_FIELD_ABI_IGNORED
);
1190 compare_values (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD
);
1191 compare_values (DECL_OFFSET_ALIGN
);
1193 else if (code
== VAR_DECL
)
1195 compare_values (DECL_HAS_DEBUG_EXPR_P
);
1196 compare_values (DECL_NONLOCAL_FRAME
);
1198 if (code
== RESULT_DECL
1199 || code
== PARM_DECL
1200 || code
== VAR_DECL
)
1202 compare_values (DECL_BY_REFERENCE
);
1203 if (code
== VAR_DECL
1204 || code
== PARM_DECL
)
1205 compare_values (DECL_HAS_VALUE_EXPR_P
);
1209 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WRTL
))
1210 compare_values (DECL_REGISTER
);
1212 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1214 compare_values (DECL_COMMON
);
1215 compare_values (DECL_DLLIMPORT_P
);
1216 compare_values (DECL_WEAK
);
1217 compare_values (DECL_SEEN_IN_BIND_EXPR_P
);
1218 compare_values (DECL_COMDAT
);
1219 compare_values (DECL_VISIBILITY
);
1220 compare_values (DECL_VISIBILITY_SPECIFIED
);
1221 if (code
== VAR_DECL
)
1223 compare_values (DECL_HARD_REGISTER
);
1224 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1225 compare_values (DECL_IN_CONSTANT_POOL
);
1229 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1231 compare_values (DECL_BUILT_IN_CLASS
);
1232 compare_values (DECL_STATIC_CONSTRUCTOR
);
1233 compare_values (DECL_STATIC_DESTRUCTOR
);
1234 compare_values (DECL_UNINLINABLE
);
1235 compare_values (DECL_POSSIBLY_INLINED
);
1236 compare_values (DECL_IS_NOVOPS
);
1237 compare_values (DECL_IS_RETURNS_TWICE
);
1238 compare_values (DECL_IS_MALLOC
);
1239 compare_values (FUNCTION_DECL_DECL_TYPE
);
1240 compare_values (DECL_DECLARED_INLINE_P
);
1241 compare_values (DECL_STATIC_CHAIN
);
1242 compare_values (DECL_NO_INLINE_WARNING_P
);
1243 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT
);
1244 compare_values (DECL_NO_LIMIT_STACK
);
1245 compare_values (DECL_DISREGARD_INLINE_LIMITS
);
1246 compare_values (DECL_PURE_P
);
1247 compare_values (DECL_LOOPING_CONST_OR_PURE_P
);
1248 compare_values (DECL_IS_REPLACEABLE_OPERATOR
);
1249 compare_values (DECL_FINAL_P
);
1250 compare_values (DECL_CXX_CONSTRUCTOR_P
);
1251 compare_values (DECL_CXX_DESTRUCTOR_P
);
1252 if (DECL_BUILT_IN_CLASS (t1
) != NOT_BUILT_IN
)
1253 compare_values (DECL_UNCHECKED_FUNCTION_CODE
);
1256 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1258 compare_values (TYPE_MODE
);
1259 compare_values (TYPE_NEEDS_CONSTRUCTING
);
1260 if (RECORD_OR_UNION_TYPE_P (t1
))
1262 compare_values (TYPE_TRANSPARENT_AGGR
);
1263 compare_values (TYPE_FINAL_P
);
1264 compare_values (TYPE_CXX_ODR_P
);
1266 else if (code
== ARRAY_TYPE
)
1267 compare_values (TYPE_NONALIASED_COMPONENT
);
1268 if (code
== ARRAY_TYPE
|| code
== INTEGER_TYPE
)
1269 compare_values (TYPE_STRING_FLAG
);
1270 if (AGGREGATE_TYPE_P (t1
))
1271 compare_values (TYPE_TYPELESS_STORAGE
);
1272 compare_values (TYPE_EMPTY_P
);
1273 compare_values (TYPE_NO_NAMED_ARGS_STDARG_P
);
1274 compare_values (TYPE_PACKED
);
1275 compare_values (TYPE_RESTRICT
);
1276 compare_values (TYPE_USER_ALIGN
);
1277 compare_values (TYPE_READONLY
);
1278 compare_values (TYPE_PRECISION
);
1279 compare_values (TYPE_ALIGN
);
1280 /* Do not compare TYPE_ALIAS_SET. Doing so introduce ordering issues
1281 with calls to get_alias_set which may initialize it for streamed
1285 /* We don't want to compare locations, so there is nothing do compare
1288 /* BLOCKs are function local and we don't merge anything there, so
1289 simply refuse to merge. */
1290 if (CODE_CONTAINS_STRUCT (code
, TS_BLOCK
))
1293 if (CODE_CONTAINS_STRUCT (code
, TS_TRANSLATION_UNIT_DECL
))
1294 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1
),
1295 TRANSLATION_UNIT_LANGUAGE (t2
)) != 0)
1298 if (CODE_CONTAINS_STRUCT (code
, TS_TARGET_OPTION
))
1299 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1
), TREE_TARGET_OPTION (t2
)))
1302 if (CODE_CONTAINS_STRUCT (code
, TS_OPTIMIZATION
))
1303 if (!cl_optimization_option_eq (TREE_OPTIMIZATION (t1
),
1304 TREE_OPTIMIZATION (t2
)))
1307 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1308 if (vec_safe_length (BINFO_BASE_ACCESSES (t1
))
1309 != vec_safe_length (BINFO_BASE_ACCESSES (t2
)))
1312 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1314 compare_values (CLOBBER_KIND
);
1315 compare_values (CONSTRUCTOR_NELTS
);
1318 if (CODE_CONTAINS_STRUCT (code
, TS_IDENTIFIER
))
1319 if (IDENTIFIER_LENGTH (t1
) != IDENTIFIER_LENGTH (t2
)
1320 || memcmp (IDENTIFIER_POINTER (t1
), IDENTIFIER_POINTER (t2
),
1321 IDENTIFIER_LENGTH (t1
)) != 0)
1324 if (CODE_CONTAINS_STRUCT (code
, TS_STRING
))
1325 if (TREE_STRING_LENGTH (t1
) != TREE_STRING_LENGTH (t2
)
1326 || memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
1327 TREE_STRING_LENGTH (t1
)) != 0)
1330 if (code
== OMP_CLAUSE
)
1332 compare_values (OMP_CLAUSE_CODE
);
1333 switch (OMP_CLAUSE_CODE (t1
))
1335 case OMP_CLAUSE_DEFAULT
:
1336 compare_values (OMP_CLAUSE_DEFAULT_KIND
);
1338 case OMP_CLAUSE_SCHEDULE
:
1339 compare_values (OMP_CLAUSE_SCHEDULE_KIND
);
1341 case OMP_CLAUSE_DEPEND
:
1342 compare_values (OMP_CLAUSE_DEPEND_KIND
);
1344 case OMP_CLAUSE_MAP
:
1345 compare_values (OMP_CLAUSE_MAP_KIND
);
1347 case OMP_CLAUSE_PROC_BIND
:
1348 compare_values (OMP_CLAUSE_PROC_BIND_KIND
);
1350 case OMP_CLAUSE_REDUCTION
:
1351 compare_values (OMP_CLAUSE_REDUCTION_CODE
);
1352 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT
);
1353 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE
);
1360 #undef compare_values
1363 /* Compare pointer fields. */
1365 /* Recurse. Search & Replaced from DFS_write_tree_body.
1366 Folding the early checks into the compare_tree_edges recursion
1367 macro makes debugging way quicker as you are able to break on
1368 compare_tree_sccs_1 and simply finish until a call returns false
1369 to spot the SCC members with the difference. */
1370 #define compare_tree_edges(E1, E2) \
1372 tree t1_ = (E1), t2_ = (E2); \
1375 || !TREE_VISITED (t2_) \
1376 || (!TREE_ASM_WRITTEN (t2_) \
1377 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1379 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1380 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1383 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
1385 if (code
!= IDENTIFIER_NODE
)
1386 compare_tree_edges (TREE_TYPE (t1
), TREE_TYPE (t2
));
1389 if (CODE_CONTAINS_STRUCT (code
, TS_VECTOR
))
1391 /* Note that the number of elements for EXPR has already been emitted
1392 in EXPR's header (see streamer_write_tree_header). */
1393 unsigned int count
= vector_cst_encoded_nelts (t1
);
1394 for (unsigned int i
= 0; i
< count
; ++i
)
1395 compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1
, i
),
1396 VECTOR_CST_ENCODED_ELT (t2
, i
));
1399 if (CODE_CONTAINS_STRUCT (code
, TS_COMPLEX
))
1401 compare_tree_edges (TREE_REALPART (t1
), TREE_REALPART (t2
));
1402 compare_tree_edges (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
1405 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_MINIMAL
))
1407 compare_tree_edges (DECL_NAME (t1
), DECL_NAME (t2
));
1408 /* ??? Global decls from different TUs have non-matching
1409 TRANSLATION_UNIT_DECLs. Only consider a small set of
1410 decls equivalent, we should not end up merging others. */
1411 if ((code
== TYPE_DECL
1412 || code
== NAMESPACE_DECL
1413 || code
== IMPORTED_DECL
1414 || code
== CONST_DECL
1415 || (VAR_OR_FUNCTION_DECL_P (t1
)
1416 && (TREE_PUBLIC (t1
) || DECL_EXTERNAL (t1
))))
1417 && DECL_FILE_SCOPE_P (t1
) && DECL_FILE_SCOPE_P (t2
))
1420 compare_tree_edges (DECL_CONTEXT (t1
), DECL_CONTEXT (t2
));
1423 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1425 compare_tree_edges (DECL_SIZE (t1
), DECL_SIZE (t2
));
1426 compare_tree_edges (DECL_SIZE_UNIT (t1
), DECL_SIZE_UNIT (t2
));
1427 compare_tree_edges (DECL_ATTRIBUTES (t1
), DECL_ATTRIBUTES (t2
));
1428 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1
), DECL_ABSTRACT_ORIGIN (t2
));
1429 if ((code
== VAR_DECL
1430 || code
== PARM_DECL
)
1431 && DECL_HAS_VALUE_EXPR_P (t1
))
1432 compare_tree_edges (DECL_VALUE_EXPR (t1
), DECL_VALUE_EXPR (t2
));
1433 if (code
== VAR_DECL
1434 && DECL_HAS_DEBUG_EXPR_P (t1
))
1435 compare_tree_edges (DECL_DEBUG_EXPR (t1
), DECL_DEBUG_EXPR (t2
));
1436 /* LTO specific edges. */
1437 if (code
!= FUNCTION_DECL
1438 && code
!= TRANSLATION_UNIT_DECL
)
1439 compare_tree_edges (DECL_INITIAL (t1
), DECL_INITIAL (t2
));
1442 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
1444 if (code
== FUNCTION_DECL
)
1447 for (a1
= DECL_ARGUMENTS (t1
), a2
= DECL_ARGUMENTS (t2
);
1449 a1
= TREE_CHAIN (a1
), a2
= TREE_CHAIN (a2
))
1450 compare_tree_edges (a1
, a2
);
1451 compare_tree_edges (DECL_RESULT (t1
), DECL_RESULT (t2
));
1453 else if (code
== TYPE_DECL
)
1454 compare_tree_edges (DECL_ORIGINAL_TYPE (t1
), DECL_ORIGINAL_TYPE (t2
));
1457 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1459 /* Make sure we don't inadvertently set the assembler name. */
1460 if (DECL_ASSEMBLER_NAME_SET_P (t1
))
1461 compare_tree_edges (DECL_ASSEMBLER_NAME (t1
),
1462 DECL_ASSEMBLER_NAME (t2
));
1465 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
1467 compare_tree_edges (DECL_FIELD_OFFSET (t1
), DECL_FIELD_OFFSET (t2
));
1468 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1
), DECL_BIT_FIELD_TYPE (t2
));
1469 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1
),
1470 DECL_BIT_FIELD_REPRESENTATIVE (t2
));
1471 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1
),
1472 DECL_FIELD_BIT_OFFSET (t2
));
1473 compare_tree_edges (DECL_FCONTEXT (t1
), DECL_FCONTEXT (t2
));
1476 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1478 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1
),
1479 DECL_FUNCTION_PERSONALITY (t2
));
1480 compare_tree_edges (DECL_VINDEX (t1
), DECL_VINDEX (t2
));
1481 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1
),
1482 DECL_FUNCTION_SPECIFIC_TARGET (t2
));
1483 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1
),
1484 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2
));
1487 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1489 compare_tree_edges (TYPE_SIZE (t1
), TYPE_SIZE (t2
));
1490 compare_tree_edges (TYPE_SIZE_UNIT (t1
), TYPE_SIZE_UNIT (t2
));
1491 compare_tree_edges (TYPE_ATTRIBUTES (t1
), TYPE_ATTRIBUTES (t2
));
1492 compare_tree_edges (TYPE_NAME (t1
), TYPE_NAME (t2
));
1493 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1494 reconstructed during fixup. */
1495 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1497 compare_tree_edges (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
));
1498 /* ??? Global types from different TUs have non-matching
1499 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1501 if (TYPE_FILE_SCOPE_P (t1
) && TYPE_FILE_SCOPE_P (t2
))
1504 compare_tree_edges (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1505 /* TYPE_CANONICAL is re-computed during type merging, so do not
1507 compare_tree_edges (TYPE_STUB_DECL (t1
), TYPE_STUB_DECL (t2
));
1510 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_NON_COMMON
))
1512 if (code
== ARRAY_TYPE
)
1513 compare_tree_edges (TYPE_DOMAIN (t1
), TYPE_DOMAIN (t2
));
1514 else if (RECORD_OR_UNION_TYPE_P (t1
))
1517 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1519 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1520 compare_tree_edges (f1
, f2
);
1522 else if (code
== FUNCTION_TYPE
1523 || code
== METHOD_TYPE
)
1524 compare_tree_edges (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
));
1526 if (!POINTER_TYPE_P (t1
))
1527 compare_tree_edges (TYPE_MIN_VALUE_RAW (t1
), TYPE_MIN_VALUE_RAW (t2
));
1528 compare_tree_edges (TYPE_MAX_VALUE_RAW (t1
), TYPE_MAX_VALUE_RAW (t2
));
1531 if (CODE_CONTAINS_STRUCT (code
, TS_LIST
))
1533 compare_tree_edges (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1534 compare_tree_edges (TREE_VALUE (t1
), TREE_VALUE (t2
));
1535 compare_tree_edges (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1538 if (CODE_CONTAINS_STRUCT (code
, TS_VEC
))
1539 for (int i
= 0; i
< TREE_VEC_LENGTH (t1
); i
++)
1540 compare_tree_edges (TREE_VEC_ELT (t1
, i
), TREE_VEC_ELT (t2
, i
));
1542 if (CODE_CONTAINS_STRUCT (code
, TS_EXP
))
1544 for (int i
= 0; i
< TREE_OPERAND_LENGTH (t1
); i
++)
1545 compare_tree_edges (TREE_OPERAND (t1
, i
),
1546 TREE_OPERAND (t2
, i
));
1548 /* BLOCKs are function local and we don't merge anything there. */
1549 if (TREE_BLOCK (t1
) || TREE_BLOCK (t2
))
1553 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1557 /* Lengths have already been compared above. */
1558 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1
), i
, t
)
1559 compare_tree_edges (t
, BINFO_BASE_BINFO (t2
, i
));
1560 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1
), i
, t
)
1561 compare_tree_edges (t
, BINFO_BASE_ACCESS (t2
, i
));
1562 compare_tree_edges (BINFO_OFFSET (t1
), BINFO_OFFSET (t2
));
1563 compare_tree_edges (BINFO_VTABLE (t1
), BINFO_VTABLE (t2
));
1564 compare_tree_edges (BINFO_VPTR_FIELD (t1
), BINFO_VPTR_FIELD (t2
));
1565 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1566 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1569 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1573 /* Lengths have already been compared above. */
1574 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, index
, value
)
1576 compare_tree_edges (index
, CONSTRUCTOR_ELT (t2
, i
)->index
);
1577 compare_tree_edges (value
, CONSTRUCTOR_ELT (t2
, i
)->value
);
1581 if (code
== OMP_CLAUSE
)
1585 for (i
= 0; i
< omp_clause_num_ops
[OMP_CLAUSE_CODE (t1
)]; i
++)
1586 compare_tree_edges (OMP_CLAUSE_OPERAND (t1
, i
),
1587 OMP_CLAUSE_OPERAND (t2
, i
));
1588 compare_tree_edges (OMP_CLAUSE_CHAIN (t1
), OMP_CLAUSE_CHAIN (t2
));
1591 #undef compare_tree_edges
1596 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1597 out MAP if they are equal. */
1600 compare_tree_sccs (tree_scc
*pscc
, tree_scc
*scc
,
1603 /* Assume SCC entry hashes are sorted after their cardinality. Which
1604 means we can simply take the first n-tuple of equal hashes
1605 (which is recorded as entry_len) and do n SCC entry candidate
1607 for (unsigned i
= 0; i
< pscc
->entry_len
; ++i
)
1610 num_scc_compare_collisions
++;
1611 if (compare_tree_sccs_1 (pscc
->entries
[0], scc
->entries
[i
], &mapp
))
1613 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1614 on the scc as all trees will be freed. */
1617 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1618 the SCC prevails. */
1619 for (unsigned j
= 0; j
< scc
->len
; ++j
)
1620 TREE_ASM_WRITTEN (scc
->entries
[j
]) = 0;
1626 /* QSort sort function to sort a map of two pointers after the 2nd
1630 cmp_tree (const void *p1_
, const void *p2_
)
1632 tree
*p1
= (tree
*)(const_cast<void *>(p1_
));
1633 tree
*p2
= (tree
*)(const_cast<void *>(p2_
));
1636 return ((uintptr_t)p1
[1] < (uintptr_t)p2
[1]) ? -1 : 1;
1639 /* New scc of size 1 containing T was streamed in from DATA_IN and not merged.
1640 Register it to reader cache at index FROM. */
1643 process_dref (class data_in
*data_in
, tree t
, unsigned from
)
1645 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
1646 /* If we got a debug reference queued, see if the prevailing
1647 tree has a debug reference and if not, register the one
1648 for the tree we are about to throw away. */
1649 if (dref_queue
.length () == 1)
1651 dref_entry e
= dref_queue
.pop ();
1653 == streamer_tree_cache_get_tree (cache
, from
));
1655 unsigned HOST_WIDE_INT off
;
1656 if (!debug_hooks
->die_ref_for_decl (t
, &sym
, &off
))
1657 debug_hooks
->register_external_die (t
, e
.sym
, e
.off
);
1661 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1662 hash value SCC_HASH with an already recorded SCC. Return true if
1663 that was successful, otherwise return false. */
1666 unify_scc (class data_in
*data_in
, unsigned from
,
1667 unsigned len
, unsigned scc_entry_len
, hashval_t scc_hash
)
1669 bool unified_p
= false;
1670 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
1672 = (tree_scc
*) alloca (sizeof (tree_scc
) + (len
- 1) * sizeof (tree
));
1674 scc
->hash
= scc_hash
;
1676 scc
->entry_len
= scc_entry_len
;
1677 for (unsigned i
= 0; i
< len
; ++i
)
1679 tree t
= streamer_tree_cache_get_tree (cache
, from
+ i
);
1680 scc
->entries
[i
] = t
;
1681 /* These types should be streamed as unshared. */
1683 (!(TREE_CODE (t
) == TRANSLATION_UNIT_DECL
1684 || (VAR_OR_FUNCTION_DECL_P (t
)
1685 && !(TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
1686 || TREE_CODE (t
) == LABEL_DECL
1687 || (TREE_CODE (t
) == NAMESPACE_DECL
&& !DECL_NAME (t
))
1689 && type_with_linkage_p (TYPE_MAIN_VARIANT (t
))
1690 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t
)))));
1693 /* Look for the list of candidate SCCs to compare against. */
1695 slot
= tree_scc_hash
->find_slot_with_hash (scc
, scc_hash
, INSERT
);
1698 /* Try unifying against each candidate. */
1701 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1702 outside of the scc when following tree edges. Make sure
1703 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1704 to track whether we visited the SCC member during the compare.
1705 We cannot use TREE_VISITED on the pscc members as the extended
1706 scc and pscc can overlap. */
1707 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1709 TREE_VISITED (scc
->entries
[i
]) = 1;
1710 gcc_checking_assert (!TREE_ASM_WRITTEN (scc
->entries
[i
]));
1713 tree
*map
= XALLOCAVEC (tree
, 2 * len
);
1714 for (tree_scc
*pscc
= *slot
; pscc
; pscc
= pscc
->next
)
1716 if (!compare_tree_sccs (pscc
, scc
, map
))
1719 /* Found an equal SCC. */
1721 num_scc_compare_collisions
--;
1723 total_scc_size_merged
+= len
;
1726 for (unsigned i
= 0; i
< len
; ++i
)
1728 tree t
= map
[2*i
+1];
1729 enum tree_code code
= TREE_CODE (t
);
1730 /* IDENTIFIER_NODEs should be singletons and are merged by the
1731 streamer. The others should be singletons, too, and we
1732 should not merge them in any way. */
1733 gcc_assert (code
!= TRANSLATION_UNIT_DECL
1734 && code
!= IDENTIFIER_NODE
);
1737 /* Fixup the streamer cache with the prevailing nodes according
1738 to the tree node mapping computed by compare_tree_sccs. */
1741 process_dref (data_in
, pscc
->entries
[0], from
);
1742 lto_maybe_register_decl (data_in
, pscc
->entries
[0], from
);
1743 streamer_tree_cache_replace_tree (cache
, pscc
->entries
[0], from
);
1747 tree
*map2
= XALLOCAVEC (tree
, 2 * len
);
1748 for (unsigned i
= 0; i
< len
; ++i
)
1750 map2
[i
*2] = (tree
)(uintptr_t)(from
+ i
);
1751 map2
[i
*2+1] = scc
->entries
[i
];
1753 qsort (map2
, len
, 2 * sizeof (tree
), cmp_tree
);
1754 qsort (map
, len
, 2 * sizeof (tree
), cmp_tree
);
1755 for (unsigned i
= 0; i
< len
; ++i
)
1757 lto_maybe_register_decl (data_in
, map
[2*i
],
1758 (uintptr_t)map2
[2*i
]);
1759 streamer_tree_cache_replace_tree (cache
, map
[2*i
],
1760 (uintptr_t)map2
[2*i
]);
1764 /* Free the tree nodes from the read SCC. */
1765 data_in
->location_cache
.revert_location_cache ();
1766 for (unsigned i
= 0; i
< len
; ++i
)
1768 if (TYPE_P (scc
->entries
[i
]))
1770 free_node (scc
->entries
[i
]);
1773 /* Drop DIE references.
1774 ??? Do as in the size-one SCC case which involves sorting
1776 dref_queue
.truncate (0);
1781 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1783 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1784 TREE_VISITED (scc
->entries
[i
]) = 0;
1787 /* If we didn't unify it to any candidate duplicate the relevant
1788 pieces to permanent storage and link it into the chain. */
1792 = XOBNEWVAR (&tree_scc_hash_obstack
, tree_scc
, sizeof (tree_scc
));
1793 memcpy (pscc
, scc
, sizeof (tree_scc
));
1794 pscc
->next
= (*slot
);
1800 typedef int_hash
<unsigned, 0, UINT_MAX
> code_id_hash
;
1802 /* Do registering necessary once new tree fully streamed in (including all
1803 trees it reffers to). */
1806 process_new_tree (tree t
, hash_map
<code_id_hash
, unsigned> *hm
,
1807 unsigned index
, unsigned *total
, class data_in
*data_in
)
1809 /* Reconstruct the type variant and pointer-to/reference-to
1813 /* Map the tree types to their frequencies. */
1814 if (flag_lto_dump_type_stats
)
1816 unsigned key
= (unsigned) TREE_CODE (t
);
1817 unsigned *countp
= hm
->get (key
);
1818 hm
->put (key
, countp
? (*countp
) + 1 : 1);
1822 num_prevailing_types
++;
1823 lto_fixup_prevailing_type (t
);
1825 /* Compute the canonical type of all non-ODR types.
1826 Delay ODR types for the end of merging process - the canonical
1827 type for those can be computed using the (unique) name however
1828 we want to do this only if units in other languages do not
1829 contain structurally equivalent type.
1831 Because SCC components are streamed in random (hash) order
1832 we may have encountered the type before while registering
1833 type canonical of a derived type in the same SCC. */
1834 if (!TYPE_CANONICAL (t
))
1836 if (!RECORD_OR_UNION_TYPE_P (t
)
1837 || !TYPE_CXX_ODR_P (t
))
1838 gimple_register_canonical_type (t
);
1839 else if (COMPLETE_TYPE_P (t
))
1840 vec_safe_push (types_to_register
, t
);
1842 if (TYPE_MAIN_VARIANT (t
) == t
&& odr_type_p (t
))
1843 register_odr_type (t
);
1845 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1846 type which is also member of this SCC. */
1847 if (TREE_CODE (t
) == INTEGER_CST
1848 && !TREE_OVERFLOW (t
))
1849 cache_integer_cst (t
);
1852 lto_maybe_register_decl (data_in
, t
, index
);
1853 /* Scan the tree for references to global functions or
1854 variables and record those for later fixup. */
1855 if (mentions_vars_p (t
))
1856 vec_safe_push (tree_with_vars
, t
);
1860 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1861 RESOLUTIONS is the set of symbols picked by the linker (read from the
1862 resolution file when the linker plugin is being used). */
1865 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
1866 vec
<ld_plugin_symbol_resolution_t
> resolutions
)
1868 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
1869 const int decl_offset
= sizeof (struct lto_decl_header
);
1870 const int main_offset
= decl_offset
+ header
->decl_state_size
;
1871 const int string_offset
= main_offset
+ header
->main_size
;
1872 class data_in
*data_in
;
1874 const uint32_t *data_ptr
, *data_end
;
1875 uint32_t num_decl_states
;
1877 lto_input_block
ib_main ((const char *) data
+ main_offset
,
1878 header
->main_size
, decl_data
->mode_table
);
1880 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
1881 header
->string_size
, resolutions
);
1883 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1884 internal types that should not be merged. */
1886 hash_map
<code_id_hash
, unsigned> hm
;
1889 /* Read the global declarations and types. */
1890 while (ib_main
.p
< ib_main
.len
)
1893 unsigned from
= data_in
->reader_cache
->nodes
.length ();
1894 /* Read and uniquify SCCs as in the input stream. */
1895 enum LTO_tags tag
= streamer_read_record_start (&ib_main
);
1896 if (tag
== LTO_tree_scc
|| tag
== LTO_trees
)
1899 unsigned scc_entry_len
;
1901 /* Because we stream in SCC order we know that all unshared trees
1902 are now fully streamed. Process them. */
1903 hashval_t scc_hash
= lto_input_scc (&ib_main
, data_in
, &len_
,
1905 tag
== LTO_tree_scc
);
1906 unsigned len
= data_in
->reader_cache
->nodes
.length () - from
;
1907 gcc_assert (len
== len_
);
1909 if (tag
== LTO_tree_scc
)
1911 total_scc_size
+= len
;
1915 num_unshared_trees_read
+= len
;
1917 /* We have the special case of size-1 SCCs that are pre-merged
1918 by means of identifier and string sharing for example.
1919 ??? Maybe we should avoid streaming those as SCCs. */
1920 tree first
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1922 /* Identifier and integers are shared specially, they should never
1923 go by the tree merging path. */
1924 gcc_checking_assert ((TREE_CODE (first
) != IDENTIFIER_NODE
1925 && (TREE_CODE (first
) != INTEGER_CST
1926 || TREE_OVERFLOW (first
)))
1929 /* Try to unify the SCC with already existing ones. */
1930 if (!flag_ltrans
&& tag
!= LTO_trees
1931 && unify_scc (data_in
, from
,
1932 len
, scc_entry_len
, scc_hash
))
1935 /* Tree merging failed, mark entries in location cache as
1937 data_in
->location_cache
.accept_location_cache ();
1939 bool seen_type
= false;
1940 for (unsigned i
= 0; i
< len
; ++i
)
1942 tree t
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1944 process_new_tree (t
, &hm
, from
+ i
, &total
, data_in
);
1949 /* Register DECLs with the debuginfo machinery. */
1950 while (!dref_queue
.is_empty ())
1952 dref_entry e
= dref_queue
.pop ();
1953 debug_hooks
->register_external_die (e
.decl
, e
.sym
, e
.off
);
1957 num_type_scc_trees
+= len
;
1961 t
= lto_input_tree_1 (&ib_main
, data_in
, tag
, 0);
1962 gcc_assert (data_in
->reader_cache
->nodes
.length () == from
+ 1);
1963 num_unshared_trees_read
++;
1964 data_in
->location_cache
.accept_location_cache ();
1965 process_dref (data_in
, t
, from
);
1966 if (TREE_CODE (t
) == IDENTIFIER_NODE
1967 || (TREE_CODE (t
) == INTEGER_CST
1968 && !TREE_OVERFLOW (t
)))
1972 lto_maybe_register_decl (data_in
, t
, from
);
1973 process_new_tree (t
, &hm
, from
, &total
, data_in
);
1978 /* Dump type statistics. */
1979 if (flag_lto_dump_type_stats
)
1981 fprintf (stdout
, " Type Frequency Percentage\n\n");
1982 for (hash_map
<code_id_hash
, unsigned>::iterator itr
= hm
.begin ();
1986 std::pair
<unsigned, unsigned> p
= *itr
;
1987 enum tree_code code
= (enum tree_code
) p
.first
;
1988 fprintf (stdout
, "%14s %6d %12.2f\n", get_tree_code_name (code
),
1989 p
.second
, float (p
.second
)/total
*100);
1993 data_in
->location_cache
.apply_location_cache ();
1995 /* Read in lto_in_decl_state objects. */
1996 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
1998 = (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
1999 num_decl_states
= *data_ptr
++;
2001 gcc_assert (num_decl_states
> 0);
2002 decl_data
->global_decl_state
= lto_new_in_decl_state ();
2003 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
2004 decl_data
->global_decl_state
);
2006 /* Read in per-function decl states and enter them in hash table. */
2007 decl_data
->function_decl_states
2008 = hash_table
<decl_state_hasher
>::create_ggc (37);
2010 for (i
= 1; i
< num_decl_states
; i
++)
2012 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
2014 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
2015 lto_in_decl_state
**slot
2016 = decl_data
->function_decl_states
->find_slot (state
, INSERT
);
2017 gcc_assert (*slot
== NULL
);
2021 if (data_ptr
!= data_end
)
2022 internal_error ("bytecode stream: garbage at the end of symbols section");
2024 /* Set the current decl state to be the global state. */
2025 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
2027 lto_data_in_delete (data_in
);
2030 /* Custom version of strtoll, which is not portable. */
2033 lto_parse_hex (const char *p
)
2037 for (; *p
!= '\0'; ++p
)
2042 if (c
>= '0' && c
<= '9')
2044 else if (c
>= 'a' && c
<= 'f')
2045 part
= c
- 'a' + 10;
2046 else if (c
>= 'A' && c
<= 'F')
2047 part
= c
- 'A' + 10;
2049 internal_error ("could not parse hex number");
2056 /* Read resolution for file named FILE_NAME. The resolution is read from
2060 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
2062 /* We require that objects in the resolution file are in the same
2063 order as the lto1 command line. */
2064 unsigned int name_len
;
2066 unsigned int num_symbols
;
2068 struct lto_file_decl_data
*file_data
;
2069 splay_tree_node nd
= NULL
;
2074 name_len
= strlen (file
->filename
);
2075 obj_name
= XNEWVEC (char, name_len
+ 1);
2076 fscanf (resolution
, " "); /* Read white space. */
2078 fread (obj_name
, sizeof (char), name_len
, resolution
);
2079 obj_name
[name_len
] = '\0';
2080 if (filename_cmp (obj_name
, file
->filename
) != 0)
2081 internal_error ("unexpected file name %s in linker resolution file. "
2082 "Expected %s", obj_name
, file
->filename
);
2083 if (file
->offset
!= 0)
2088 t
= fscanf (resolution
, "@0x%16s", offset_p
);
2090 internal_error ("could not parse file offset");
2091 offset
= lto_parse_hex (offset_p
);
2092 if (offset
!= file
->offset
)
2093 internal_error ("unexpected offset");
2098 fscanf (resolution
, "%u", &num_symbols
);
2100 for (i
= 0; i
< num_symbols
; i
++)
2104 unsigned HOST_WIDE_INT id
;
2106 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
2108 unsigned int lto_resolution_str_len
= ARRAY_SIZE (lto_resolution_str
);
2111 t
= fscanf (resolution
, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
2112 " %26s %*[^\n]\n", &index
, &id
, r_str
);
2114 internal_error ("invalid line in the resolution file");
2116 for (j
= 0; j
< lto_resolution_str_len
; j
++)
2118 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
2120 r
= (enum ld_plugin_symbol_resolution
) j
;
2121 /* Incremental linking together with -fwhole-program may seem
2122 somewhat contradictionary (as the point of incremental linking
2123 is to allow re-linking with more symbols later) but it is
2124 used to build LTO kernel. We want to hide all symbols that
2125 are not explicitely marked as exported and thus turn
2126 LDPR_PREVAILING_DEF_IRONLY_EXP
2127 to LDPR_PREVAILING_DEF_IRONLY. */
2128 if (flag_whole_program
2129 && flag_incremental_link
== INCREMENTAL_LINK_NOLTO
2130 && r
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
2131 r
= LDPR_PREVAILING_DEF_IRONLY
;
2135 if (j
== lto_resolution_str_len
)
2136 internal_error ("invalid resolution in the resolution file");
2138 if (!(nd
&& lto_splay_tree_id_equal_p (nd
->key
, id
)))
2140 nd
= lto_splay_tree_lookup (file_ids
, id
);
2142 internal_error ("resolution sub id %wx not in object file", id
);
2145 file_data
= (struct lto_file_decl_data
*)nd
->value
;
2146 /* The indexes are very sparse. To save memory save them in a compact
2147 format that is only unpacked later when the subfile is processed. */
2150 file_data
->respairs
.safe_push (rp
);
2151 if (file_data
->max_index
< index
)
2152 file_data
->max_index
= index
;
2156 /* List of file_decl_datas. */
2157 struct file_data_list
2159 struct lto_file_decl_data
*first
, *last
;
2162 /* Is the name for a id'ed LTO section? */
2165 lto_section_with_id (const char *name
, unsigned HOST_WIDE_INT
*id
)
2169 if (strncmp (name
, section_name_prefix
, strlen (section_name_prefix
)))
2171 s
= strrchr (name
, '.');
2174 /* If the section is not suffixed with an ID return. */
2175 if ((size_t)(s
- name
) == strlen (section_name_prefix
))
2177 return sscanf (s
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, id
) == 1;
2180 /* Create file_data of each sub file id. */
2183 create_subid_section_table (struct lto_section_slot
*ls
, splay_tree file_ids
,
2184 struct file_data_list
*list
)
2186 struct lto_section_slot s_slot
, *new_slot
;
2187 unsigned HOST_WIDE_INT id
;
2191 struct lto_file_decl_data
*file_data
;
2193 if (!lto_section_with_id (ls
->name
, &id
))
2196 /* Find hash table of sub module id. */
2197 nd
= lto_splay_tree_lookup (file_ids
, id
);
2200 file_data
= (struct lto_file_decl_data
*)nd
->value
;
2204 file_data
= ggc_alloc
<lto_file_decl_data
> ();
2205 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
2207 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();
2208 lto_splay_tree_insert (file_ids
, id
, file_data
);
2210 /* Maintain list in linker order. */
2212 list
->first
= file_data
;
2214 list
->last
->next
= file_data
;
2216 list
->last
= file_data
;
2219 /* Copy section into sub module hash table. */
2220 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
2221 s_slot
.name
= new_name
;
2222 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
2223 gcc_assert (*hash_slot
== NULL
);
2225 new_slot
= XDUP (struct lto_section_slot
, ls
);
2226 new_slot
->name
= new_name
;
2227 *hash_slot
= new_slot
;
2231 /* Read declarations and other initializations for a FILE_DATA. */
2234 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
,
2239 vec
<ld_plugin_symbol_resolution_t
>
2240 resolutions
= vNULL
;
2244 /* Create vector for fast access of resolution. We do this lazily
2246 resolutions
.safe_grow_cleared (file_data
->max_index
+ 1, true);
2247 for (i
= 0; file_data
->respairs
.iterate (i
, &rp
); i
++)
2248 resolutions
[rp
->index
] = rp
->res
;
2249 file_data
->respairs
.release ();
2251 file_data
->renaming_hash_table
= lto_create_renaming_table ();
2252 file_data
->file_name
= file
->filename
;
2253 file_data
->order
= order
;
2255 /* Read and verify LTO section. */
2256 data
= lto_get_summary_section_data (file_data
, LTO_section_lto
, &len
);
2259 fatal_error (input_location
, "bytecode stream in file %qs generated "
2260 "with GCC compiler older than 10.0", file_data
->file_name
);
2264 memcpy (&file_data
->lto_section_header
, data
, sizeof (lto_section
));
2265 lto_check_version (file_data
->lto_section_header
.major_version
,
2266 file_data
->lto_section_header
.minor_version
,
2267 file_data
->file_name
);
2269 #ifdef ACCEL_COMPILER
2270 lto_input_mode_table (file_data
);
2272 file_data
->mode_table
= lto_mode_identity_table
;
2275 data
= lto_get_summary_section_data (file_data
, LTO_section_decls
, &len
);
2278 internal_error ("cannot read %<LTO_section_decls%> from %s",
2279 file_data
->file_name
);
2282 /* Frees resolutions. */
2283 lto_read_decls (file_data
, data
, resolutions
);
2284 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
2287 /* Finalize FILE_DATA in FILE and increase COUNT. */
2290 lto_create_files_from_ids (lto_file
*file
, struct lto_file_decl_data
*file_data
,
2291 int *count
, int order
)
2293 lto_file_finalize (file_data
, file
, order
);
2294 if (symtab
->dump_file
)
2295 fprintf (symtab
->dump_file
,
2296 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX
"\n",
2297 file_data
->file_name
, file_data
->id
);
2302 /* Generate a TREE representation for all types and external decls
2305 Read all of the globals out of the file. Then read the cgraph
2306 and process the .o index into the cgraph nodes so that it can open
2307 the .o file to load the functions and ipa information. */
2309 static struct lto_file_decl_data
*
2310 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
2312 struct lto_file_decl_data
*file_data
= NULL
;
2313 splay_tree file_ids
;
2314 htab_t section_hash_table
;
2315 struct lto_section_slot
*section
;
2316 struct file_data_list file_list
;
2317 struct lto_section_list section_list
;
2319 memset (§ion_list
, 0, sizeof (struct lto_section_list
));
2320 section_hash_table
= lto_obj_build_section_table (file
, §ion_list
);
2322 /* Dump the details of LTO objects. */
2323 if (flag_lto_dump_objects
)
2326 fprintf (stdout
, "\n LTO Object Name: %s\n", file
->filename
);
2327 fprintf (stdout
, "\nNo. Offset Size Section Name\n\n");
2328 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
2329 fprintf (stdout
, "%2d %8" PRId64
" %8" PRIu64
" %s\n",
2330 ++i
, (int64_t) section
->start
, (uint64_t) section
->len
,
2334 /* Find all sub modules in the object and put their sections into new hash
2335 tables in a splay tree. */
2336 file_ids
= lto_splay_tree_new ();
2337 memset (&file_list
, 0, sizeof (struct file_data_list
));
2338 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
2339 create_subid_section_table (section
, file_ids
, &file_list
);
2341 /* Add resolutions to file ids. */
2342 lto_resolution_read (file_ids
, resolution_file
, file
);
2344 /* Finalize each lto file for each submodule in the merged object. */
2346 for (file_data
= file_list
.first
; file_data
!= NULL
;
2347 file_data
= file_data
->next
)
2348 lto_create_files_from_ids (file
, file_data
, count
, order
++);
2350 splay_tree_delete (file_ids
);
2351 htab_delete (section_hash_table
);
2353 return file_list
.first
;
2356 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2357 #define LTO_MMAP_IO 1
2361 /* Page size of machine is used for mmap and munmap calls. */
2362 static size_t page_mask
;
2365 /* Get the section data of length LEN from FILENAME starting at
2366 OFFSET. The data segment must be freed by the caller when the
2367 caller is finished. Returns NULL if all was not well. */
2370 lto_read_section_data (struct lto_file_decl_data
*file_data
,
2371 intptr_t offset
, size_t len
)
2375 static char *fd_name
;
2377 intptr_t computed_len
;
2378 intptr_t computed_offset
;
2382 /* Keep a single-entry file-descriptor cache. The last file we
2383 touched will get closed at exit.
2384 ??? Eventually we want to add a more sophisticated larger cache
2385 or rather fix function body streaming to not stream them in
2386 practically random order. */
2388 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
2396 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
2399 fatal_error (input_location
, "Cannot open %s", file_data
->file_name
);
2402 fd_name
= xstrdup (file_data
->file_name
);
2408 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
2409 page_mask
= ~(page_size
- 1);
2412 computed_offset
= offset
& page_mask
;
2413 diff
= offset
- computed_offset
;
2414 computed_len
= len
+ diff
;
2416 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
2417 fd
, computed_offset
);
2418 if (result
== MAP_FAILED
)
2420 fatal_error (input_location
, "Cannot map %s", file_data
->file_name
);
2424 return result
+ diff
;
2426 result
= (char *) xmalloc (len
);
2427 if (lseek (fd
, offset
, SEEK_SET
) != offset
2428 || read (fd
, result
, len
) != (ssize_t
) len
)
2431 fatal_error (input_location
, "Cannot read %s", file_data
->file_name
);
2435 /* Native windows doesn't supports delayed unlink on opened file. So
2436 we close file here again. This produces higher I/O load, but at least
2437 it prevents to have dangling file handles preventing unlink. */
2448 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2449 NAME will be NULL unless the section type is for a function
2453 get_section_data (struct lto_file_decl_data
*file_data
,
2454 enum lto_section_type section_type
,
2455 const char *name
, int order
,
2458 htab_t section_hash_table
= file_data
->section_hash_table
;
2459 struct lto_section_slot
*f_slot
;
2460 struct lto_section_slot s_slot
;
2461 const char *section_name
= lto_get_section_name (section_type
, name
,
2466 s_slot
.name
= section_name
;
2467 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
2470 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
2474 free (CONST_CAST (char *, section_name
));
2479 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2480 starts at OFFSET and has LEN bytes. */
2483 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
2484 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
2485 const char *name ATTRIBUTE_UNUSED
,
2486 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
2489 intptr_t computed_len
;
2490 intptr_t computed_offset
;
2495 computed_offset
= ((intptr_t) offset
) & page_mask
;
2496 diff
= (intptr_t) offset
- computed_offset
;
2497 computed_len
= len
+ diff
;
2499 munmap ((caddr_t
) computed_offset
, computed_len
);
2501 free (CONST_CAST(char *, offset
));
2505 static lto_file
*current_lto_file
;
2507 /* If TT is a variable or function decl replace it with its
2508 prevailing variant. */
2509 #define LTO_SET_PREVAIL(tt) \
2511 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2512 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2514 tt = lto_symtab_prevailing_decl (tt); \
2519 /* Ensure that TT isn't a replacable var of function decl. */
2520 #define LTO_NO_PREVAIL(tt) \
2521 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2523 /* Given a tree T replace all fields referring to variables or functions
2524 with their prevailing variant. */
2526 lto_fixup_prevailing_decls (tree t
)
2528 enum tree_code code
= TREE_CODE (t
);
2531 gcc_checking_assert (code
!= TREE_BINFO
);
2532 LTO_NO_PREVAIL (TREE_TYPE (t
));
2533 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
)
2534 /* lto_symtab_prevail_decl use TREE_CHAIN to link to the prevailing decl.
2535 in the case T is a prevailed declaration we would ICE here. */
2536 && !VAR_OR_FUNCTION_DECL_P (t
))
2537 LTO_NO_PREVAIL (TREE_CHAIN (t
));
2540 LTO_NO_PREVAIL (DECL_NAME (t
));
2541 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
2542 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
2544 LTO_SET_PREVAIL (DECL_SIZE (t
));
2545 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
2546 LTO_SET_PREVAIL (DECL_INITIAL (t
));
2547 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
2548 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
2550 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
2552 LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t
));
2554 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
2556 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
2558 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
2560 LTO_NO_PREVAIL (DECL_ARGUMENTS (t
));
2561 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
2562 LTO_NO_PREVAIL (DECL_VINDEX (t
));
2564 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
2566 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t
));
2567 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
2568 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
2569 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
2570 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
2573 else if (TYPE_P (t
))
2575 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
2576 LTO_SET_PREVAIL (TYPE_SIZE (t
));
2577 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
2578 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
2579 LTO_NO_PREVAIL (TYPE_NAME (t
));
2581 LTO_SET_PREVAIL (TYPE_MIN_VALUE_RAW (t
));
2582 LTO_SET_PREVAIL (TYPE_MAX_VALUE_RAW (t
));
2583 LTO_NO_PREVAIL (TYPE_LANG_SLOT_1 (t
));
2585 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
2587 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
2588 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
2589 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
2591 else if (EXPR_P (t
))
2594 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
2595 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
2597 else if (TREE_CODE (t
) == CONSTRUCTOR
)
2601 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
2602 LTO_SET_PREVAIL (val
);
2609 LTO_SET_PREVAIL (TREE_VALUE (t
));
2610 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
2616 /* If we fixed nothing, then we missed something seen by
2618 gcc_checking_assert (fixed
);
2620 #undef LTO_SET_PREVAIL
2621 #undef LTO_NO_PREVAIL
2623 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2624 replaces var and function decls with the corresponding prevailing def. */
2627 lto_fixup_state (struct lto_in_decl_state
*state
)
2631 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2632 we still need to walk from all DECLs to find the reachable
2633 FUNCTION_DECLs and VAR_DECLs. */
2634 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
2636 vec
<tree
, va_gc
> *trees
= state
->streams
[si
];
2637 for (i
= 0; i
< vec_safe_length (trees
); i
++)
2639 tree t
= (*trees
)[i
];
2640 if (flag_checking
&& TYPE_P (t
))
2642 if (VAR_OR_FUNCTION_DECL_P (t
)
2643 && (TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
2644 (*trees
)[i
] = lto_symtab_prevailing_decl (t
);
2649 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2653 lto_fixup_decls (struct lto_file_decl_data
**files
)
2659 FOR_EACH_VEC_ELT ((*tree_with_vars
), i
, t
)
2660 lto_fixup_prevailing_decls (t
);
2662 for (i
= 0; files
[i
]; i
++)
2664 struct lto_file_decl_data
*file
= files
[i
];
2665 struct lto_in_decl_state
*state
= file
->global_decl_state
;
2666 lto_fixup_state (state
);
2668 hash_table
<decl_state_hasher
>::iterator iter
;
2669 lto_in_decl_state
*elt
;
2670 FOR_EACH_HASH_TABLE_ELEMENT (*file
->function_decl_states
, elt
,
2671 lto_in_decl_state
*, iter
)
2672 lto_fixup_state (elt
);
2676 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
2678 /* Turn file datas for sub files into a single array, so that they look
2679 like separate files for further passes. */
2682 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
,
2685 struct lto_file_decl_data
*n
, *next
;
2688 lto_stats
.num_input_files
= count
;
2690 = ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (count
+ 1);
2691 /* Set the hooks so that all of the ipa passes can read in their data. */
2692 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2693 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
2695 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
2697 all_file_decl_data
[k
++] = n
;
2702 all_file_decl_data
[k
] = NULL
;
2703 gcc_assert (k
== count
);
2706 /* Input file data before flattening (i.e. splitting them to subfiles to support
2707 incremental linking. */
2708 static int real_file_count
;
2709 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
2711 /* Read all the symbols from the input files FNAMES. NFILES is the
2712 number of files requested in the command line. Instantiate a
2713 global call graph by aggregating all the sub-graphs found in each
2717 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
2719 unsigned int i
, last_file_ix
;
2721 unsigned resolution_objects
= 0;
2723 struct lto_file_decl_data
**decl_data
;
2726 symtab
->initialize ();
2728 timevar_push (TV_IPA_LTO_DECL_IN
);
2730 #ifdef ACCEL_COMPILER
2731 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2732 lto_stream_offload_p
= true;
2736 = decl_data
= ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (nfiles
+ 1);
2737 real_file_count
= nfiles
;
2739 /* Read the resolution file. */
2741 if (resolution_file_name
)
2745 resolution
= fopen (resolution_file_name
, "r");
2746 if (resolution
== NULL
)
2747 fatal_error (input_location
,
2748 "could not open symbol resolution file: %m");
2750 t
= fscanf (resolution
, "%u", &resolution_objects
);
2751 gcc_assert (t
== 1);
2753 symtab
->state
= LTO_STREAMING
;
2755 canonical_type_hash_cache
= new hash_map
<const_tree
, hashval_t
> (251);
2756 gimple_canonical_types
= htab_create (16381, gimple_canonical_type_hash
,
2757 gimple_canonical_type_eq
, NULL
);
2758 gcc_obstack_init (&tree_scc_hash_obstack
);
2759 tree_scc_hash
= new hash_table
<tree_scc_hasher
> (4096);
2761 /* Register the common node types with the canonical type machinery so
2762 we properly share alias-sets across languages and TUs. Do not
2763 expose the common nodes as type merge target - those that should be
2764 are already exposed so by pre-loading the LTO streamer caches.
2765 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2766 for (i
= 0; i
< itk_none
; ++i
)
2767 lto_register_canonical_types (integer_types
[i
], true);
2768 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2769 lto_register_canonical_types (sizetype_tab
[i
], true);
2770 for (i
= 0; i
< TI_MAX
; ++i
)
2771 lto_register_canonical_types (global_trees
[i
], true);
2772 for (i
= 0; i
< itk_none
; ++i
)
2773 lto_register_canonical_types (integer_types
[i
], false);
2774 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2775 lto_register_canonical_types (sizetype_tab
[i
], false);
2776 for (i
= 0; i
< TI_MAX
; ++i
)
2777 lto_register_canonical_types (global_trees
[i
], false);
2780 fprintf (stderr
, "Reading object files:");
2782 /* Read all of the object files specified on the command line. */
2783 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
2785 struct lto_file_decl_data
*file_data
= NULL
;
2788 fprintf (stderr
, " %s", fnames
[i
]);
2792 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
2793 if (!current_lto_file
)
2796 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
2799 lto_obj_file_close (current_lto_file
);
2800 free (current_lto_file
);
2801 current_lto_file
= NULL
;
2805 decl_data
[last_file_ix
++] = file_data
;
2807 lto_obj_file_close (current_lto_file
);
2808 free (current_lto_file
);
2809 current_lto_file
= NULL
;
2812 lto_flatten_files (decl_data
, count
, last_file_ix
);
2813 lto_stats
.num_input_files
= count
;
2814 ggc_free(decl_data
);
2815 real_file_decl_data
= NULL
;
2817 lto_register_canonical_types_for_odr_types ();
2819 if (resolution_file_name
)
2821 /* True, since the plugin splits the archives. */
2822 gcc_assert (resolution_objects
== nfiles
);
2823 fclose (resolution
);
2826 /* Show the LTO report before launching LTRANS. */
2827 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
2828 print_lto_report_1 ();
2830 /* Free gimple type merging datastructures. */
2831 delete tree_scc_hash
;
2832 tree_scc_hash
= NULL
;
2833 obstack_free (&tree_scc_hash_obstack
, NULL
);
2834 htab_delete (gimple_canonical_types
);
2835 gimple_canonical_types
= NULL
;
2836 delete canonical_type_hash_cache
;
2837 canonical_type_hash_cache
= NULL
;
2839 /* At this stage we know that majority of GGC memory is reachable.
2840 Growing the limits prevents unnecesary invocation of GGC. */
2842 report_heap_memory_use ();
2844 /* Set the hooks so that all of the ipa passes can read in their data. */
2845 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2847 timevar_pop (TV_IPA_LTO_DECL_IN
);
2850 fprintf (stderr
, "\nReading the symbol table:");
2852 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
2853 /* Read the symtab. */
2856 input_offload_tables (!flag_ltrans
);
2858 /* Store resolutions into the symbol table. */
2860 FOR_EACH_SYMBOL (snode
)
2861 if (snode
->externally_visible
&& snode
->real_symbol_p ()
2862 && snode
->lto_file_data
&& snode
->lto_file_data
->resolution_map
2863 && !(TREE_CODE (snode
->decl
) == FUNCTION_DECL
2864 && fndecl_built_in_p (snode
->decl
))
2865 && !(VAR_P (snode
->decl
) && DECL_HARD_REGISTER (snode
->decl
)))
2867 ld_plugin_symbol_resolution_t
*res
;
2869 res
= snode
->lto_file_data
->resolution_map
->get (snode
->decl
);
2870 if (!res
|| *res
== LDPR_UNKNOWN
)
2872 if (snode
->output_to_lto_symbol_table_p ())
2873 fatal_error (input_location
, "missing resolution data for %s",
2875 (DECL_ASSEMBLER_NAME (snode
->decl
)));
2877 /* Symbol versions are always used externally, but linker does not
2878 report that correctly.
2879 This is binutils PR25924. */
2880 else if (snode
->symver
&& *res
== LDPR_PREVAILING_DEF_IRONLY
)
2881 snode
->resolution
= LDPR_PREVAILING_DEF_IRONLY_EXP
;
2883 snode
->resolution
= *res
;
2885 for (i
= 0; all_file_decl_data
[i
]; i
++)
2886 if (all_file_decl_data
[i
]->resolution_map
)
2888 delete all_file_decl_data
[i
]->resolution_map
;
2889 all_file_decl_data
[i
]->resolution_map
= NULL
;
2892 timevar_pop (TV_IPA_LTO_CGRAPH_IO
);
2895 fprintf (stderr
, "\nMerging declarations:");
2897 timevar_push (TV_IPA_LTO_DECL_MERGE
);
2898 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2899 need to care about resolving symbols again, we only need to replace
2900 duplicated declarations read from the callgraph and from function
2904 lto_symtab_merge_decls ();
2906 /* If there were errors during symbol merging bail out, we have no
2907 good way to recover here. */
2909 fatal_error (input_location
,
2910 "errors during merging of translation units");
2912 /* Fixup all decls. */
2913 lto_fixup_decls (all_file_decl_data
);
2916 ggc_free (tree_with_vars
);
2917 tree_with_vars
= NULL
;
2918 /* During WPA we want to prevent ggc collecting by default. Grow limits
2919 until after the IPA summaries are streamed in. Basically all IPA memory
2920 is explcitly managed by ggc_free and ggc collect is not useful.
2921 Exception are the merged declarations. */
2923 report_heap_memory_use ();
2925 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
2926 /* Each pass will set the appropriate timer. */
2929 fprintf (stderr
, "\nReading summaries:");
2931 /* Read the IPA summary data. */
2933 ipa_read_optimization_summaries ();
2935 ipa_read_summaries ();
2939 for (i
= 0; all_file_decl_data
[i
]; i
++)
2941 gcc_assert (all_file_decl_data
[i
]->symtab_node_encoder
);
2942 lto_symtab_encoder_delete (all_file_decl_data
[i
]->symtab_node_encoder
);
2943 all_file_decl_data
[i
]->symtab_node_encoder
= NULL
;
2944 lto_in_decl_state
*global_decl_state
2945 = all_file_decl_data
[i
]->global_decl_state
;
2946 lto_free_function_in_decl_state (global_decl_state
);
2947 all_file_decl_data
[i
]->global_decl_state
= NULL
;
2948 all_file_decl_data
[i
]->current_decl_state
= NULL
;
2953 /* Finally merge the cgraph according to the decl merging decisions. */
2954 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
2957 fprintf (stderr
, "\nMerging symbols:");
2959 gcc_assert (!dump_file
);
2960 dump_file
= dump_begin (lto_link_dump_id
, NULL
);
2964 fprintf (dump_file
, "Before merging:\n");
2965 symtab
->dump (dump_file
);
2967 lto_symtab_merge_symbols ();
2968 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2969 we are still having duplicated comdat groups containing local statics.
2970 We could also just remove them while merging. */
2971 symtab
->remove_unreachable_nodes (dump_file
);
2973 report_heap_memory_use ();
2976 dump_end (lto_link_dump_id
, dump_file
);
2978 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
2980 symtab
->state
= IPA_SSA
;
2981 /* All node removals happening here are useless, because
2982 WPA should not stream them. Still always perform remove_unreachable_nodes
2983 because we may reshape clone tree, get rid of dead masters of inline
2984 clones and remove symbol entries for read-only variables we keep around
2985 only to be able to constant fold them. */
2988 if (symtab
->dump_file
)
2989 symtab
->dump (symtab
->dump_file
);
2990 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2993 /* Indicate that the cgraph is built and ready. */
2994 symtab
->function_flags_ready
= true;
2996 ggc_free (all_file_decl_data
);
2997 all_file_decl_data
= NULL
;
3002 /* Show various memory usage statistics related to LTO. */
3004 print_lto_report_1 (void)
3006 const char *pfx
= (flag_lto
) ? "LTO" : (flag_wpa
) ? "WPA" : "LTRANS";
3007 fprintf (stderr
, "%s statistics\n", pfx
);
3009 fprintf (stderr
, "[%s] read %lu unshared trees\n",
3010 pfx
, num_unshared_trees_read
);
3011 fprintf (stderr
, "[%s] read %lu mergeable SCCs of average size %f\n",
3012 pfx
, num_sccs_read
, total_scc_size
/ (double)num_sccs_read
);
3013 fprintf (stderr
, "[%s] %lu tree bodies read in total\n", pfx
,
3014 total_scc_size
+ num_unshared_trees_read
);
3015 if (flag_wpa
&& tree_scc_hash
&& num_sccs_read
)
3017 fprintf (stderr
, "[%s] tree SCC table: size %ld, %ld elements, "
3018 "collision ratio: %f\n", pfx
,
3019 (long) tree_scc_hash
->size (),
3020 (long) tree_scc_hash
->elements (),
3021 tree_scc_hash
->collisions ());
3022 hash_table
<tree_scc_hasher
>::iterator hiter
;
3023 tree_scc
*scc
, *max_scc
= NULL
;
3024 unsigned max_length
= 0;
3025 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash
, scc
, x
, hiter
)
3027 unsigned length
= 0;
3029 for (; s
; s
= s
->next
)
3031 if (length
> max_length
)
3033 max_length
= length
;
3037 fprintf (stderr
, "[%s] tree SCC max chain length %u (size %u)\n",
3038 pfx
, max_length
, max_scc
->len
);
3039 fprintf (stderr
, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx
,
3040 num_scc_compares
, num_scc_compare_collisions
,
3041 num_scc_compare_collisions
/ (double) num_scc_compares
);
3042 fprintf (stderr
, "[%s] Merged %lu SCCs\n", pfx
, num_sccs_merged
);
3043 fprintf (stderr
, "[%s] Merged %lu tree bodies\n", pfx
,
3044 total_scc_size_merged
);
3045 fprintf (stderr
, "[%s] Merged %lu types\n", pfx
, num_merged_types
);
3046 fprintf (stderr
, "[%s] %lu types prevailed (%lu associated trees)\n",
3047 pfx
, num_prevailing_types
, num_type_scc_trees
);
3048 fprintf (stderr
, "[%s] GIMPLE canonical type table: size %ld, "
3049 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx
,
3050 (long) htab_size (gimple_canonical_types
),
3051 (long) htab_elements (gimple_canonical_types
),
3052 (long) gimple_canonical_types
->searches
,
3053 (long) gimple_canonical_types
->collisions
,
3054 htab_collisions (gimple_canonical_types
));
3055 fprintf (stderr
, "[%s] GIMPLE canonical type pointer-map: "
3056 "%lu elements, %ld searches\n", pfx
,
3057 num_canonical_type_hash_entries
,
3058 num_canonical_type_hash_queries
);
3061 print_lto_report (pfx
);
3064 GTY(()) tree lto_eh_personality_decl
;
3066 /* Return the LTO personality function decl. */
3069 lto_eh_personality (void)
3071 if (!lto_eh_personality_decl
)
3073 /* Use the first personality DECL for our personality if we don't
3074 support multiple ones. This ensures that we don't artificially
3075 create the need for them in a single-language program. */
3076 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
3077 lto_eh_personality_decl
= first_personality_decl
;
3079 lto_eh_personality_decl
= lhd_gcc_personality ();
3082 return lto_eh_personality_decl
;
3085 /* Set the process name based on the LTO mode. */
3088 lto_process_name (void)
3091 setproctitle (flag_incremental_link
== INCREMENTAL_LINK_LTO
3092 ? "lto1-inclink" : "lto1-lto");
3094 setproctitle ("lto1-wpa");
3096 setproctitle ("lto1-ltrans");
3100 /* Initialize the LTO front end. */
3105 lto_process_name ();
3106 lto_streamer_hooks_init ();
3108 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
3109 memset (<o_stats
, 0, sizeof (lto_stats
));
3110 bitmap_obstack_initialize (NULL
);
3111 gimple_register_cfg_hooks ();
3112 #ifndef ACCEL_COMPILER
3113 unsigned char *table
3114 = ggc_vec_alloc
<unsigned char> (MAX_MACHINE_MODE
);
3115 for (int m
= 0; m
< MAX_MACHINE_MODE
; m
++)
3117 lto_mode_identity_table
= table
;
3121 #include "gt-lto-lto-common.h"