1 /* Top-level LTO routines.
2 Copyright (C) 2009-2016 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-inline.h"
51 #include "ipa-utils.h"
52 #include "gomp-constants.h"
53 #include "lto-symtab.h"
54 #include "stringpool.h"
55 #include "fold-const.h"
58 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
59 static int lto_parallelism
;
61 static GTY(()) tree first_personality_decl
;
63 static GTY(()) const unsigned char *lto_mode_identity_table
;
65 /* Returns a hash code for P. */
68 hash_name (const void *p
)
70 const struct lto_section_slot
*ds
= (const struct lto_section_slot
*) p
;
71 return (hashval_t
) htab_hash_string (ds
->name
);
75 /* Returns nonzero if P1 and P2 are equal. */
78 eq_name (const void *p1
, const void *p2
)
80 const struct lto_section_slot
*s1
=
81 (const struct lto_section_slot
*) p1
;
82 const struct lto_section_slot
*s2
=
83 (const struct lto_section_slot
*) p2
;
85 return strcmp (s1
->name
, s2
->name
) == 0;
88 /* Free lto_section_slot */
91 free_with_string (void *arg
)
93 struct lto_section_slot
*s
= (struct lto_section_slot
*)arg
;
95 free (CONST_CAST (char *, s
->name
));
99 /* Create section hash table */
102 lto_obj_create_section_hash_table (void)
104 return htab_create (37, hash_name
, eq_name
, free_with_string
);
107 /* Delete an allocated integer KEY in the splay tree. */
110 lto_splay_tree_delete_id (splay_tree_key key
)
115 /* Compare splay tree node ids A and B. */
118 lto_splay_tree_compare_ids (splay_tree_key a
, splay_tree_key b
)
120 unsigned HOST_WIDE_INT ai
;
121 unsigned HOST_WIDE_INT bi
;
123 ai
= *(unsigned HOST_WIDE_INT
*) a
;
124 bi
= *(unsigned HOST_WIDE_INT
*) b
;
133 /* Look up splay tree node by ID in splay tree T. */
135 static splay_tree_node
136 lto_splay_tree_lookup (splay_tree t
, unsigned HOST_WIDE_INT id
)
138 return splay_tree_lookup (t
, (splay_tree_key
) &id
);
141 /* Check if KEY has ID. */
144 lto_splay_tree_id_equal_p (splay_tree_key key
, unsigned HOST_WIDE_INT id
)
146 return *(unsigned HOST_WIDE_INT
*) key
== id
;
149 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
150 The ID is allocated separately because we need HOST_WIDE_INTs which may
151 be wider than a splay_tree_key. */
154 lto_splay_tree_insert (splay_tree t
, unsigned HOST_WIDE_INT id
,
155 struct lto_file_decl_data
*file_data
)
157 unsigned HOST_WIDE_INT
*idp
= XCNEW (unsigned HOST_WIDE_INT
);
159 splay_tree_insert (t
, (splay_tree_key
) idp
, (splay_tree_value
) file_data
);
162 /* Create a splay tree. */
165 lto_splay_tree_new (void)
167 return splay_tree_new (lto_splay_tree_compare_ids
,
168 lto_splay_tree_delete_id
,
172 /* Return true when NODE has a clone that is analyzed (i.e. we need
173 to load its body even if the node itself is not needed). */
176 has_analyzed_clone_p (struct cgraph_node
*node
)
178 struct cgraph_node
*orig
= node
;
187 else if (node
->next_sibling_clone
)
188 node
= node
->next_sibling_clone
;
191 while (node
!= orig
&& !node
->next_sibling_clone
)
192 node
= node
->clone_of
;
194 node
= node
->next_sibling_clone
;
200 /* Read the function body for the function associated with NODE. */
203 lto_materialize_function (struct cgraph_node
*node
)
208 /* Read in functions with body (analyzed nodes)
209 and also functions that are needed to produce virtual clones. */
210 if ((node
->has_gimple_body_p () && node
->analyzed
)
211 || node
->used_as_abstract_origin
212 || has_analyzed_clone_p (node
))
214 /* Clones don't need to be read. */
217 if (DECL_FUNCTION_PERSONALITY (decl
) && !first_personality_decl
)
218 first_personality_decl
= DECL_FUNCTION_PERSONALITY (decl
);
221 /* Let the middle end know about the function. */
222 rest_of_decl_compilation (decl
, 1, 0);
226 /* Decode the content of memory pointed to by DATA in the in decl
227 state object STATE. DATA_IN points to a data_in structure for
228 decoding. Return the address after the decoded object in the
231 static const uint32_t *
232 lto_read_in_decl_state (struct data_in
*data_in
, const uint32_t *data
,
233 struct lto_in_decl_state
*state
)
240 state
->compressed
= ix
& 1;
242 decl
= streamer_tree_cache_get_tree (data_in
->reader_cache
, ix
);
243 if (!VAR_OR_FUNCTION_DECL_P (decl
))
245 gcc_assert (decl
== void_type_node
);
248 state
->fn_decl
= decl
;
250 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
252 uint32_t size
= *data
++;
253 vec
<tree
, va_gc
> *decls
= NULL
;
254 vec_alloc (decls
, size
);
256 for (j
= 0; j
< size
; j
++)
257 vec_safe_push (decls
,
258 streamer_tree_cache_get_tree (data_in
->reader_cache
,
261 state
->streams
[i
] = decls
;
269 /* Global canonical type table. */
270 static htab_t gimple_canonical_types
;
271 static hash_map
<const_tree
, hashval_t
> *canonical_type_hash_cache
;
272 static unsigned long num_canonical_type_hash_entries
;
273 static unsigned long num_canonical_type_hash_queries
;
275 static void iterative_hash_canonical_type (tree type
, inchash::hash
&hstate
);
276 static hashval_t
gimple_canonical_type_hash (const void *p
);
277 static void gimple_register_canonical_type_1 (tree t
, hashval_t hash
);
279 /* Returning a hash value for gimple type TYPE.
281 The hash value returned is equal for types considered compatible
282 by gimple_canonical_types_compatible_p. */
285 hash_canonical_type (tree type
)
287 inchash::hash hstate
;
290 /* We compute alias sets only for types that needs them.
291 Be sure we do not recurse to something else as we can not hash incomplete
292 types in a way they would have same hash value as compatible complete
294 gcc_checking_assert (type_with_alias_set_p (type
));
296 /* Combine a few common features of types so that types are grouped into
297 smaller sets; when searching for existing matching types to merge,
298 only existing types having the same features as the new type will be
300 code
= tree_code_for_canonical_type_merging (TREE_CODE (type
));
301 hstate
.add_int (code
);
302 hstate
.add_int (TYPE_MODE (type
));
304 /* Incorporate common features of numerical types. */
305 if (INTEGRAL_TYPE_P (type
)
306 || SCALAR_FLOAT_TYPE_P (type
)
307 || FIXED_POINT_TYPE_P (type
)
308 || TREE_CODE (type
) == OFFSET_TYPE
309 || POINTER_TYPE_P (type
))
311 hstate
.add_int (TYPE_PRECISION (type
));
312 if (!type_with_interoperable_signedness (type
))
313 hstate
.add_int (TYPE_UNSIGNED (type
));
316 if (VECTOR_TYPE_P (type
))
318 hstate
.add_int (TYPE_VECTOR_SUBPARTS (type
));
319 hstate
.add_int (TYPE_UNSIGNED (type
));
322 if (TREE_CODE (type
) == COMPLEX_TYPE
)
323 hstate
.add_int (TYPE_UNSIGNED (type
));
325 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
326 interoperable with "signed char". Unless all frontends are revisited to
327 agree on these types, we must ignore the flag completely. */
329 /* Fortran standard define C_PTR type that is compatible with every
330 C pointer. For this reason we need to glob all pointers into one.
331 Still pointers in different address spaces are not compatible. */
332 if (POINTER_TYPE_P (type
))
333 hstate
.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type
)));
335 /* For array types hash the domain bounds and the string flag. */
336 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
338 hstate
.add_int (TYPE_STRING_FLAG (type
));
339 /* OMP lowering can introduce error_mark_node in place of
340 random local decls in types. */
341 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type
)) != error_mark_node
)
342 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type
)), hstate
);
343 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) != error_mark_node
)
344 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)), hstate
);
347 /* Recurse for aggregates with a single element type. */
348 if (TREE_CODE (type
) == ARRAY_TYPE
349 || TREE_CODE (type
) == COMPLEX_TYPE
350 || TREE_CODE (type
) == VECTOR_TYPE
)
351 iterative_hash_canonical_type (TREE_TYPE (type
), hstate
);
353 /* Incorporate function return and argument types. */
354 if (TREE_CODE (type
) == FUNCTION_TYPE
|| TREE_CODE (type
) == METHOD_TYPE
)
359 iterative_hash_canonical_type (TREE_TYPE (type
), hstate
);
361 for (p
= TYPE_ARG_TYPES (type
), na
= 0; p
; p
= TREE_CHAIN (p
))
363 iterative_hash_canonical_type (TREE_VALUE (p
), hstate
);
370 if (RECORD_OR_UNION_TYPE_P (type
))
375 for (f
= TYPE_FIELDS (type
), nf
= 0; f
; f
= TREE_CHAIN (f
))
376 if (TREE_CODE (f
) == FIELD_DECL
)
378 iterative_hash_canonical_type (TREE_TYPE (f
), hstate
);
388 /* Returning a hash value for gimple type TYPE combined with VAL. */
391 iterative_hash_canonical_type (tree type
, inchash::hash
&hstate
)
395 /* All type variants have same TYPE_CANONICAL. */
396 type
= TYPE_MAIN_VARIANT (type
);
398 if (!canonical_type_used_p (type
))
399 v
= hash_canonical_type (type
);
400 /* An already processed type. */
401 else if (TYPE_CANONICAL (type
))
403 type
= TYPE_CANONICAL (type
);
404 v
= gimple_canonical_type_hash (type
);
408 /* Canonical types should not be able to form SCCs by design, this
409 recursion is just because we do not register canonical types in
410 optimal order. To avoid quadratic behavior also register the
412 v
= hash_canonical_type (type
);
413 gimple_register_canonical_type_1 (type
, v
);
418 /* Returns the hash for a canonical type P. */
421 gimple_canonical_type_hash (const void *p
)
423 num_canonical_type_hash_queries
++;
424 hashval_t
*slot
= canonical_type_hash_cache
->get ((const_tree
) p
);
425 gcc_assert (slot
!= NULL
);
431 /* Returns nonzero if P1 and P2 are equal. */
434 gimple_canonical_type_eq (const void *p1
, const void *p2
)
436 const_tree t1
= (const_tree
) p1
;
437 const_tree t2
= (const_tree
) p2
;
438 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1
),
439 CONST_CAST_TREE (t2
));
442 /* Main worker for gimple_register_canonical_type. */
445 gimple_register_canonical_type_1 (tree t
, hashval_t hash
)
449 gcc_checking_assert (TYPE_P (t
) && !TYPE_CANONICAL (t
)
450 && type_with_alias_set_p (t
)
451 && canonical_type_used_p (t
));
453 slot
= htab_find_slot_with_hash (gimple_canonical_types
, t
, hash
, INSERT
);
456 tree new_type
= (tree
)(*slot
);
457 gcc_checking_assert (new_type
!= t
);
458 TYPE_CANONICAL (t
) = new_type
;
462 TYPE_CANONICAL (t
) = t
;
464 /* Cache the just computed hash value. */
465 num_canonical_type_hash_entries
++;
466 bool existed_p
= canonical_type_hash_cache
->put (t
, hash
);
467 gcc_assert (!existed_p
);
471 /* Register type T in the global type table gimple_types and set
472 TYPE_CANONICAL of T accordingly.
473 This is used by LTO to merge structurally equivalent types for
474 type-based aliasing purposes across different TUs and languages.
476 ??? This merging does not exactly match how the tree.c middle-end
477 functions will assign TYPE_CANONICAL when new types are created
478 during optimization (which at least happens for pointer and array
482 gimple_register_canonical_type (tree t
)
484 if (TYPE_CANONICAL (t
) || !type_with_alias_set_p (t
)
485 || !canonical_type_used_p (t
))
488 /* Canonical types are same among all complete variants. */
489 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
)))
490 TYPE_CANONICAL (t
) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
));
493 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t
),
494 hash_canonical_type (TYPE_MAIN_VARIANT (t
)));
495 TYPE_CANONICAL (t
) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
));
499 /* Re-compute TYPE_CANONICAL for NODE and related types. */
502 lto_register_canonical_types (tree node
, bool first_p
)
509 TYPE_CANONICAL (node
) = NULL_TREE
;
511 if (POINTER_TYPE_P (node
)
512 || TREE_CODE (node
) == COMPLEX_TYPE
513 || TREE_CODE (node
) == ARRAY_TYPE
)
514 lto_register_canonical_types (TREE_TYPE (node
), first_p
);
517 gimple_register_canonical_type (node
);
521 /* Remember trees that contains references to declarations. */
522 static GTY(()) vec
<tree
, va_gc
> *tree_with_vars
;
524 #define CHECK_VAR(tt) \
527 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
528 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
532 #define CHECK_NO_VAR(tt) \
533 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
535 /* Check presence of pointers to decls in fields of a tree_typed T. */
538 mentions_vars_p_typed (tree t
)
540 CHECK_NO_VAR (TREE_TYPE (t
));
544 /* Check presence of pointers to decls in fields of a tree_common T. */
547 mentions_vars_p_common (tree t
)
549 if (mentions_vars_p_typed (t
))
551 CHECK_NO_VAR (TREE_CHAIN (t
));
555 /* Check presence of pointers to decls in fields of a decl_minimal T. */
558 mentions_vars_p_decl_minimal (tree t
)
560 if (mentions_vars_p_common (t
))
562 CHECK_NO_VAR (DECL_NAME (t
));
563 CHECK_VAR (DECL_CONTEXT (t
));
567 /* Check presence of pointers to decls in fields of a decl_common T. */
570 mentions_vars_p_decl_common (tree t
)
572 if (mentions_vars_p_decl_minimal (t
))
574 CHECK_VAR (DECL_SIZE (t
));
575 CHECK_VAR (DECL_SIZE_UNIT (t
));
576 CHECK_VAR (DECL_INITIAL (t
));
577 CHECK_NO_VAR (DECL_ATTRIBUTES (t
));
578 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t
));
582 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
585 mentions_vars_p_decl_with_vis (tree t
)
587 if (mentions_vars_p_decl_common (t
))
590 /* Accessor macro has side-effects, use field-name here. */
591 CHECK_NO_VAR (t
->decl_with_vis
.assembler_name
);
595 /* Check presence of pointers to decls in fields of a decl_non_common T. */
598 mentions_vars_p_decl_non_common (tree t
)
600 if (mentions_vars_p_decl_with_vis (t
))
602 CHECK_NO_VAR (DECL_RESULT_FLD (t
));
606 /* Check presence of pointers to decls in fields of a decl_non_common T. */
609 mentions_vars_p_function (tree t
)
611 if (mentions_vars_p_decl_non_common (t
))
613 CHECK_NO_VAR (DECL_ARGUMENTS (t
));
614 CHECK_NO_VAR (DECL_VINDEX (t
));
615 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t
));
619 /* Check presence of pointers to decls in fields of a field_decl T. */
622 mentions_vars_p_field_decl (tree t
)
624 if (mentions_vars_p_decl_common (t
))
626 CHECK_VAR (DECL_FIELD_OFFSET (t
));
627 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t
));
628 CHECK_NO_VAR (DECL_QUALIFIER (t
));
629 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t
));
630 CHECK_NO_VAR (DECL_FCONTEXT (t
));
634 /* Check presence of pointers to decls in fields of a type T. */
637 mentions_vars_p_type (tree t
)
639 if (mentions_vars_p_common (t
))
641 CHECK_NO_VAR (TYPE_CACHED_VALUES (t
));
642 CHECK_VAR (TYPE_SIZE (t
));
643 CHECK_VAR (TYPE_SIZE_UNIT (t
));
644 CHECK_NO_VAR (TYPE_ATTRIBUTES (t
));
645 CHECK_NO_VAR (TYPE_NAME (t
));
647 CHECK_VAR (TYPE_MINVAL (t
));
648 CHECK_VAR (TYPE_MAXVAL (t
));
650 /* Accessor is for derived node types only. */
651 CHECK_NO_VAR (t
->type_non_common
.binfo
);
653 CHECK_VAR (TYPE_CONTEXT (t
));
654 CHECK_NO_VAR (TYPE_CANONICAL (t
));
655 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t
));
656 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t
));
660 /* Check presence of pointers to decls in fields of a BINFO T. */
663 mentions_vars_p_binfo (tree t
)
665 unsigned HOST_WIDE_INT i
, n
;
667 if (mentions_vars_p_common (t
))
669 CHECK_VAR (BINFO_VTABLE (t
));
670 CHECK_NO_VAR (BINFO_OFFSET (t
));
671 CHECK_NO_VAR (BINFO_VIRTUALS (t
));
672 CHECK_NO_VAR (BINFO_VPTR_FIELD (t
));
673 n
= vec_safe_length (BINFO_BASE_ACCESSES (t
));
674 for (i
= 0; i
< n
; i
++)
675 CHECK_NO_VAR (BINFO_BASE_ACCESS (t
, i
));
676 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
677 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
678 n
= BINFO_N_BASE_BINFOS (t
);
679 for (i
= 0; i
< n
; i
++)
680 CHECK_NO_VAR (BINFO_BASE_BINFO (t
, i
));
684 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
687 mentions_vars_p_constructor (tree t
)
689 unsigned HOST_WIDE_INT idx
;
692 if (mentions_vars_p_typed (t
))
695 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
697 CHECK_NO_VAR (ce
->index
);
698 CHECK_VAR (ce
->value
);
703 /* Check presence of pointers to decls in fields of an expression tree T. */
706 mentions_vars_p_expr (tree t
)
709 if (mentions_vars_p_typed (t
))
711 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
712 CHECK_VAR (TREE_OPERAND (t
, i
));
716 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
719 mentions_vars_p_omp_clause (tree t
)
722 if (mentions_vars_p_common (t
))
724 for (i
= omp_clause_num_ops
[OMP_CLAUSE_CODE (t
)] - 1; i
>= 0; --i
)
725 CHECK_VAR (OMP_CLAUSE_OPERAND (t
, i
));
729 /* Check presence of pointers to decls that needs later fixup in T. */
732 mentions_vars_p (tree t
)
734 switch (TREE_CODE (t
))
736 case IDENTIFIER_NODE
:
740 CHECK_VAR (TREE_VALUE (t
));
741 CHECK_VAR (TREE_PURPOSE (t
));
742 CHECK_NO_VAR (TREE_CHAIN (t
));
746 return mentions_vars_p_field_decl (t
);
755 return mentions_vars_p_decl_common (t
);
758 return mentions_vars_p_decl_with_vis (t
);
761 return mentions_vars_p_decl_non_common (t
);
764 return mentions_vars_p_function (t
);
767 return mentions_vars_p_binfo (t
);
769 case PLACEHOLDER_EXPR
:
770 return mentions_vars_p_common (t
);
773 case TRANSLATION_UNIT_DECL
:
774 case OPTIMIZATION_NODE
:
775 case TARGET_OPTION_NODE
:
779 return mentions_vars_p_constructor (t
);
782 return mentions_vars_p_omp_clause (t
);
787 if (mentions_vars_p_type (t
))
792 if (mentions_vars_p_expr (t
))
795 else if (CONSTANT_CLASS_P (t
))
796 CHECK_NO_VAR (TREE_TYPE (t
));
804 /* Return the resolution for the decl with index INDEX from DATA_IN. */
806 static enum ld_plugin_symbol_resolution
807 get_resolution (struct data_in
*data_in
, unsigned index
)
809 if (data_in
->globals_resolution
.exists ())
811 ld_plugin_symbol_resolution_t ret
;
812 /* We can have references to not emitted functions in
813 DECL_FUNCTION_PERSONALITY at least. So we can and have
814 to indeed return LDPR_UNKNOWN in some cases. */
815 if (data_in
->globals_resolution
.length () <= index
)
817 ret
= data_in
->globals_resolution
[index
];
821 /* Delay resolution finding until decl merging. */
825 /* We need to record resolutions until symbol table is read. */
827 register_resolution (struct lto_file_decl_data
*file_data
, tree decl
,
828 enum ld_plugin_symbol_resolution resolution
)
830 if (resolution
== LDPR_UNKNOWN
)
832 if (!file_data
->resolution_map
)
833 file_data
->resolution_map
834 = new hash_map
<tree
, ld_plugin_symbol_resolution
>;
835 file_data
->resolution_map
->put (decl
, resolution
);
838 /* Register DECL with the global symbol table and change its
839 name if necessary to avoid name clashes for static globals across
843 lto_register_var_decl_in_symtab (struct data_in
*data_in
, tree decl
,
848 /* Variable has file scope, not local. */
849 if (!TREE_PUBLIC (decl
)
850 && !((context
= decl_function_context (decl
))
851 && auto_var_in_fn_p (decl
, context
)))
852 rest_of_decl_compilation (decl
, 1, 0);
854 /* If this variable has already been declared, queue the
855 declaration for merging. */
856 if (TREE_PUBLIC (decl
))
857 register_resolution (data_in
->file_data
,
858 decl
, get_resolution (data_in
, ix
));
862 /* Register DECL with the global symbol table and change its
863 name if necessary to avoid name clashes for static globals across
864 different files. DATA_IN contains descriptors and tables for the
868 lto_register_function_decl_in_symtab (struct data_in
*data_in
, tree decl
,
871 /* If this variable has already been declared, queue the
872 declaration for merging. */
873 if (TREE_PUBLIC (decl
) && !DECL_ABSTRACT_P (decl
))
874 register_resolution (data_in
->file_data
,
875 decl
, get_resolution (data_in
, ix
));
879 /* For the type T re-materialize it in the type variant list and
880 the pointer/reference-to chains. */
883 lto_fixup_prevailing_type (tree t
)
885 /* The following re-creates proper variant lists while fixing up
886 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
887 variant list state before fixup is broken. */
889 /* If we are not our own variant leader link us into our new leaders
891 if (TYPE_MAIN_VARIANT (t
) != t
)
893 tree mv
= TYPE_MAIN_VARIANT (t
);
894 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
895 TYPE_NEXT_VARIANT (mv
) = t
;
898 /* The following reconstructs the pointer chains
899 of the new pointed-to type if we are a main variant. We do
900 not stream those so they are broken before fixup. */
901 if (TREE_CODE (t
) == POINTER_TYPE
902 && TYPE_MAIN_VARIANT (t
) == t
)
904 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
905 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
907 else if (TREE_CODE (t
) == REFERENCE_TYPE
908 && TYPE_MAIN_VARIANT (t
) == t
)
910 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
911 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
916 /* We keep prevailing tree SCCs in a hashtable with manual collision
917 handling (in case all hashes compare the same) and keep the colliding
918 entries in the tree_scc->next chain. */
923 /* Hash of the whole SCC. */
925 /* Number of trees in the SCC. */
927 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
928 which share the same individual tree hash). */
930 /* The members of the SCC.
931 We only need to remember the first entry node candidate for prevailing
932 SCCs (but of course have access to all entries for SCCs we are
934 ??? For prevailing SCCs we really only need hash and the first
935 entry candidate, but that's too awkward to implement. */
939 struct tree_scc_hasher
: nofree_ptr_hash
<tree_scc
>
941 static inline hashval_t
hash (const tree_scc
*);
942 static inline bool equal (const tree_scc
*, const tree_scc
*);
946 tree_scc_hasher::hash (const tree_scc
*scc
)
952 tree_scc_hasher::equal (const tree_scc
*scc1
, const tree_scc
*scc2
)
954 if (scc1
->hash
!= scc2
->hash
955 || scc1
->len
!= scc2
->len
956 || scc1
->entry_len
!= scc2
->entry_len
)
961 static hash_table
<tree_scc_hasher
> *tree_scc_hash
;
962 static struct obstack tree_scc_hash_obstack
;
964 static unsigned long num_merged_types
;
965 static unsigned long num_prevailing_types
;
966 static unsigned long num_type_scc_trees
;
967 static unsigned long total_scc_size
;
968 static unsigned long num_sccs_read
;
969 static unsigned long total_scc_size_merged
;
970 static unsigned long num_sccs_merged
;
971 static unsigned long num_scc_compares
;
972 static unsigned long num_scc_compare_collisions
;
975 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
976 recursing through in-SCC tree edges. Returns true if the SCCs entered
977 through T1 and T2 are equal and fills in *MAP with the pairs of
978 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
981 compare_tree_sccs_1 (tree t1
, tree t2
, tree
**map
)
985 /* Mark already visited nodes. */
986 TREE_ASM_WRITTEN (t2
) = 1;
988 /* Push the pair onto map. */
993 /* Compare value-fields. */
994 #define compare_values(X) \
996 if (X(t1) != X(t2)) \
1000 compare_values (TREE_CODE
);
1001 code
= TREE_CODE (t1
);
1005 compare_values (TREE_SIDE_EFFECTS
);
1006 compare_values (TREE_CONSTANT
);
1007 compare_values (TREE_READONLY
);
1008 compare_values (TREE_PUBLIC
);
1010 compare_values (TREE_ADDRESSABLE
);
1011 compare_values (TREE_THIS_VOLATILE
);
1013 compare_values (DECL_UNSIGNED
);
1014 else if (TYPE_P (t1
))
1015 compare_values (TYPE_UNSIGNED
);
1017 compare_values (TYPE_ARTIFICIAL
);
1019 compare_values (TREE_NO_WARNING
);
1020 compare_values (TREE_NOTHROW
);
1021 compare_values (TREE_STATIC
);
1022 if (code
!= TREE_BINFO
)
1023 compare_values (TREE_PRIVATE
);
1024 compare_values (TREE_PROTECTED
);
1025 compare_values (TREE_DEPRECATED
);
1028 if (AGGREGATE_TYPE_P (t1
))
1029 compare_values (TYPE_REVERSE_STORAGE_ORDER
);
1031 compare_values (TYPE_SATURATING
);
1032 compare_values (TYPE_ADDR_SPACE
);
1034 else if (code
== SSA_NAME
)
1035 compare_values (SSA_NAME_IS_DEFAULT_DEF
);
1037 if (CODE_CONTAINS_STRUCT (code
, TS_INT_CST
))
1039 if (!wi::eq_p (t1
, t2
))
1043 if (CODE_CONTAINS_STRUCT (code
, TS_REAL_CST
))
1045 /* ??? No suitable compare routine available. */
1046 REAL_VALUE_TYPE r1
= TREE_REAL_CST (t1
);
1047 REAL_VALUE_TYPE r2
= TREE_REAL_CST (t2
);
1049 || r1
.decimal
!= r2
.decimal
1050 || r1
.sign
!= r2
.sign
1051 || r1
.signalling
!= r2
.signalling
1052 || r1
.canonical
!= r2
.canonical
1053 || r1
.uexp
!= r2
.uexp
)
1055 for (unsigned i
= 0; i
< SIGSZ
; ++i
)
1056 if (r1
.sig
[i
] != r2
.sig
[i
])
1060 if (CODE_CONTAINS_STRUCT (code
, TS_FIXED_CST
))
1061 if (!fixed_compare (EQ_EXPR
,
1062 TREE_FIXED_CST_PTR (t1
), TREE_FIXED_CST_PTR (t2
)))
1065 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1067 compare_values (DECL_MODE
);
1068 compare_values (DECL_NONLOCAL
);
1069 compare_values (DECL_VIRTUAL_P
);
1070 compare_values (DECL_IGNORED_P
);
1071 compare_values (DECL_ABSTRACT_P
);
1072 compare_values (DECL_ARTIFICIAL
);
1073 compare_values (DECL_USER_ALIGN
);
1074 compare_values (DECL_PRESERVE_P
);
1075 compare_values (DECL_EXTERNAL
);
1076 compare_values (DECL_GIMPLE_REG_P
);
1077 compare_values (DECL_ALIGN
);
1078 if (code
== LABEL_DECL
)
1080 compare_values (EH_LANDING_PAD_NR
);
1081 compare_values (LABEL_DECL_UID
);
1083 else if (code
== FIELD_DECL
)
1085 compare_values (DECL_PACKED
);
1086 compare_values (DECL_NONADDRESSABLE_P
);
1087 compare_values (DECL_OFFSET_ALIGN
);
1089 else if (code
== VAR_DECL
)
1091 compare_values (DECL_HAS_DEBUG_EXPR_P
);
1092 compare_values (DECL_NONLOCAL_FRAME
);
1094 if (code
== RESULT_DECL
1095 || code
== PARM_DECL
1096 || code
== VAR_DECL
)
1098 compare_values (DECL_BY_REFERENCE
);
1099 if (code
== VAR_DECL
1100 || code
== PARM_DECL
)
1101 compare_values (DECL_HAS_VALUE_EXPR_P
);
1105 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WRTL
))
1106 compare_values (DECL_REGISTER
);
1108 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1110 compare_values (DECL_COMMON
);
1111 compare_values (DECL_DLLIMPORT_P
);
1112 compare_values (DECL_WEAK
);
1113 compare_values (DECL_SEEN_IN_BIND_EXPR_P
);
1114 compare_values (DECL_COMDAT
);
1115 compare_values (DECL_VISIBILITY
);
1116 compare_values (DECL_VISIBILITY_SPECIFIED
);
1117 if (code
== VAR_DECL
)
1119 compare_values (DECL_HARD_REGISTER
);
1120 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1121 compare_values (DECL_IN_CONSTANT_POOL
);
1125 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1127 compare_values (DECL_BUILT_IN_CLASS
);
1128 compare_values (DECL_STATIC_CONSTRUCTOR
);
1129 compare_values (DECL_STATIC_DESTRUCTOR
);
1130 compare_values (DECL_UNINLINABLE
);
1131 compare_values (DECL_POSSIBLY_INLINED
);
1132 compare_values (DECL_IS_NOVOPS
);
1133 compare_values (DECL_IS_RETURNS_TWICE
);
1134 compare_values (DECL_IS_MALLOC
);
1135 compare_values (DECL_IS_OPERATOR_NEW
);
1136 compare_values (DECL_DECLARED_INLINE_P
);
1137 compare_values (DECL_STATIC_CHAIN
);
1138 compare_values (DECL_NO_INLINE_WARNING_P
);
1139 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT
);
1140 compare_values (DECL_NO_LIMIT_STACK
);
1141 compare_values (DECL_DISREGARD_INLINE_LIMITS
);
1142 compare_values (DECL_PURE_P
);
1143 compare_values (DECL_LOOPING_CONST_OR_PURE_P
);
1144 compare_values (DECL_FINAL_P
);
1145 compare_values (DECL_CXX_CONSTRUCTOR_P
);
1146 compare_values (DECL_CXX_DESTRUCTOR_P
);
1147 if (DECL_BUILT_IN_CLASS (t1
) != NOT_BUILT_IN
)
1148 compare_values (DECL_FUNCTION_CODE
);
1151 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1153 compare_values (TYPE_MODE
);
1154 compare_values (TYPE_STRING_FLAG
);
1155 compare_values (TYPE_NEEDS_CONSTRUCTING
);
1156 if (RECORD_OR_UNION_TYPE_P (t1
))
1158 compare_values (TYPE_TRANSPARENT_AGGR
);
1159 compare_values (TYPE_FINAL_P
);
1161 else if (code
== ARRAY_TYPE
)
1162 compare_values (TYPE_NONALIASED_COMPONENT
);
1163 compare_values (TYPE_PACKED
);
1164 compare_values (TYPE_RESTRICT
);
1165 compare_values (TYPE_USER_ALIGN
);
1166 compare_values (TYPE_READONLY
);
1167 compare_values (TYPE_PRECISION
);
1168 compare_values (TYPE_ALIGN
);
1169 /* Do not compare TYPE_ALIAS_SET. Doing so introduce ordering issues
1170 with calls to get_alias_set which may initialize it for streamed
1174 /* We don't want to compare locations, so there is nothing do compare
1177 /* BLOCKs are function local and we don't merge anything there, so
1178 simply refuse to merge. */
1179 if (CODE_CONTAINS_STRUCT (code
, TS_BLOCK
))
1182 if (CODE_CONTAINS_STRUCT (code
, TS_TRANSLATION_UNIT_DECL
))
1183 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1
),
1184 TRANSLATION_UNIT_LANGUAGE (t2
)) != 0)
1187 if (CODE_CONTAINS_STRUCT (code
, TS_TARGET_OPTION
))
1188 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1
), TREE_TARGET_OPTION (t2
)))
1191 if (CODE_CONTAINS_STRUCT (code
, TS_OPTIMIZATION
))
1192 if (memcmp (TREE_OPTIMIZATION (t1
), TREE_OPTIMIZATION (t2
),
1193 sizeof (struct cl_optimization
)) != 0)
1196 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1197 if (vec_safe_length (BINFO_BASE_ACCESSES (t1
))
1198 != vec_safe_length (BINFO_BASE_ACCESSES (t2
)))
1201 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1202 compare_values (CONSTRUCTOR_NELTS
);
1204 if (CODE_CONTAINS_STRUCT (code
, TS_IDENTIFIER
))
1205 if (IDENTIFIER_LENGTH (t1
) != IDENTIFIER_LENGTH (t2
)
1206 || memcmp (IDENTIFIER_POINTER (t1
), IDENTIFIER_POINTER (t2
),
1207 IDENTIFIER_LENGTH (t1
)) != 0)
1210 if (CODE_CONTAINS_STRUCT (code
, TS_STRING
))
1211 if (TREE_STRING_LENGTH (t1
) != TREE_STRING_LENGTH (t2
)
1212 || memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
1213 TREE_STRING_LENGTH (t1
)) != 0)
1216 if (code
== OMP_CLAUSE
)
1218 compare_values (OMP_CLAUSE_CODE
);
1219 switch (OMP_CLAUSE_CODE (t1
))
1221 case OMP_CLAUSE_DEFAULT
:
1222 compare_values (OMP_CLAUSE_DEFAULT_KIND
);
1224 case OMP_CLAUSE_SCHEDULE
:
1225 compare_values (OMP_CLAUSE_SCHEDULE_KIND
);
1227 case OMP_CLAUSE_DEPEND
:
1228 compare_values (OMP_CLAUSE_DEPEND_KIND
);
1230 case OMP_CLAUSE_MAP
:
1231 compare_values (OMP_CLAUSE_MAP_KIND
);
1233 case OMP_CLAUSE_PROC_BIND
:
1234 compare_values (OMP_CLAUSE_PROC_BIND_KIND
);
1236 case OMP_CLAUSE_REDUCTION
:
1237 compare_values (OMP_CLAUSE_REDUCTION_CODE
);
1238 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT
);
1239 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE
);
1246 #undef compare_values
1249 /* Compare pointer fields. */
1251 /* Recurse. Search & Replaced from DFS_write_tree_body.
1252 Folding the early checks into the compare_tree_edges recursion
1253 macro makes debugging way quicker as you are able to break on
1254 compare_tree_sccs_1 and simply finish until a call returns false
1255 to spot the SCC members with the difference. */
1256 #define compare_tree_edges(E1, E2) \
1258 tree t1_ = (E1), t2_ = (E2); \
1261 || !TREE_VISITED (t2_) \
1262 || (!TREE_ASM_WRITTEN (t2_) \
1263 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1265 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1266 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1269 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
1271 if (code
!= IDENTIFIER_NODE
)
1272 compare_tree_edges (TREE_TYPE (t1
), TREE_TYPE (t2
));
1275 if (CODE_CONTAINS_STRUCT (code
, TS_VECTOR
))
1278 /* Note that the number of elements for EXPR has already been emitted
1279 in EXPR's header (see streamer_write_tree_header). */
1280 for (i
= 0; i
< VECTOR_CST_NELTS (t1
); ++i
)
1281 compare_tree_edges (VECTOR_CST_ELT (t1
, i
), VECTOR_CST_ELT (t2
, i
));
1284 if (CODE_CONTAINS_STRUCT (code
, TS_COMPLEX
))
1286 compare_tree_edges (TREE_REALPART (t1
), TREE_REALPART (t2
));
1287 compare_tree_edges (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
1290 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_MINIMAL
))
1292 compare_tree_edges (DECL_NAME (t1
), DECL_NAME (t2
));
1293 /* ??? Global decls from different TUs have non-matching
1294 TRANSLATION_UNIT_DECLs. Only consider a small set of
1295 decls equivalent, we should not end up merging others. */
1296 if ((code
== TYPE_DECL
1297 || code
== NAMESPACE_DECL
1298 || code
== IMPORTED_DECL
1299 || code
== CONST_DECL
1300 || (VAR_OR_FUNCTION_DECL_P (t1
)
1301 && (TREE_PUBLIC (t1
) || DECL_EXTERNAL (t1
))))
1302 && DECL_FILE_SCOPE_P (t1
) && DECL_FILE_SCOPE_P (t2
))
1305 compare_tree_edges (DECL_CONTEXT (t1
), DECL_CONTEXT (t2
));
1308 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1310 compare_tree_edges (DECL_SIZE (t1
), DECL_SIZE (t2
));
1311 compare_tree_edges (DECL_SIZE_UNIT (t1
), DECL_SIZE_UNIT (t2
));
1312 compare_tree_edges (DECL_ATTRIBUTES (t1
), DECL_ATTRIBUTES (t2
));
1313 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1
), DECL_ABSTRACT_ORIGIN (t2
));
1314 if ((code
== VAR_DECL
1315 || code
== PARM_DECL
)
1316 && DECL_HAS_VALUE_EXPR_P (t1
))
1317 compare_tree_edges (DECL_VALUE_EXPR (t1
), DECL_VALUE_EXPR (t2
));
1318 if (code
== VAR_DECL
1319 && DECL_HAS_DEBUG_EXPR_P (t1
))
1320 compare_tree_edges (DECL_DEBUG_EXPR (t1
), DECL_DEBUG_EXPR (t2
));
1321 /* LTO specific edges. */
1322 if (code
!= FUNCTION_DECL
1323 && code
!= TRANSLATION_UNIT_DECL
)
1324 compare_tree_edges (DECL_INITIAL (t1
), DECL_INITIAL (t2
));
1327 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
1329 if (code
== FUNCTION_DECL
)
1332 for (a1
= DECL_ARGUMENTS (t1
), a2
= DECL_ARGUMENTS (t2
);
1334 a1
= TREE_CHAIN (a1
), a2
= TREE_CHAIN (a2
))
1335 compare_tree_edges (a1
, a2
);
1336 compare_tree_edges (DECL_RESULT (t1
), DECL_RESULT (t2
));
1338 else if (code
== TYPE_DECL
)
1339 compare_tree_edges (DECL_ORIGINAL_TYPE (t1
), DECL_ORIGINAL_TYPE (t2
));
1342 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1344 /* Make sure we don't inadvertently set the assembler name. */
1345 if (DECL_ASSEMBLER_NAME_SET_P (t1
))
1346 compare_tree_edges (DECL_ASSEMBLER_NAME (t1
),
1347 DECL_ASSEMBLER_NAME (t2
));
1350 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
1352 compare_tree_edges (DECL_FIELD_OFFSET (t1
), DECL_FIELD_OFFSET (t2
));
1353 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1
), DECL_BIT_FIELD_TYPE (t2
));
1354 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1
),
1355 DECL_BIT_FIELD_REPRESENTATIVE (t2
));
1356 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1
),
1357 DECL_FIELD_BIT_OFFSET (t2
));
1358 compare_tree_edges (DECL_FCONTEXT (t1
), DECL_FCONTEXT (t2
));
1361 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1363 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1
),
1364 DECL_FUNCTION_PERSONALITY (t2
));
1365 compare_tree_edges (DECL_VINDEX (t1
), DECL_VINDEX (t2
));
1366 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1
),
1367 DECL_FUNCTION_SPECIFIC_TARGET (t2
));
1368 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1
),
1369 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2
));
1372 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1374 compare_tree_edges (TYPE_SIZE (t1
), TYPE_SIZE (t2
));
1375 compare_tree_edges (TYPE_SIZE_UNIT (t1
), TYPE_SIZE_UNIT (t2
));
1376 compare_tree_edges (TYPE_ATTRIBUTES (t1
), TYPE_ATTRIBUTES (t2
));
1377 compare_tree_edges (TYPE_NAME (t1
), TYPE_NAME (t2
));
1378 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1379 reconstructed during fixup. */
1380 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1382 compare_tree_edges (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
));
1383 /* ??? Global types from different TUs have non-matching
1384 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1386 if (TYPE_FILE_SCOPE_P (t1
) && TYPE_FILE_SCOPE_P (t2
))
1389 compare_tree_edges (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1390 /* TYPE_CANONICAL is re-computed during type merging, so do not
1392 compare_tree_edges (TYPE_STUB_DECL (t1
), TYPE_STUB_DECL (t2
));
1395 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_NON_COMMON
))
1397 if (code
== ENUMERAL_TYPE
)
1398 compare_tree_edges (TYPE_VALUES (t1
), TYPE_VALUES (t2
));
1399 else if (code
== ARRAY_TYPE
)
1400 compare_tree_edges (TYPE_DOMAIN (t1
), TYPE_DOMAIN (t2
));
1401 else if (RECORD_OR_UNION_TYPE_P (t1
))
1404 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1406 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1407 compare_tree_edges (f1
, f2
);
1408 compare_tree_edges (TYPE_BINFO (t1
), TYPE_BINFO (t2
));
1410 else if (code
== FUNCTION_TYPE
1411 || code
== METHOD_TYPE
)
1412 compare_tree_edges (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
));
1413 if (!POINTER_TYPE_P (t1
))
1414 compare_tree_edges (TYPE_MINVAL (t1
), TYPE_MINVAL (t2
));
1415 compare_tree_edges (TYPE_MAXVAL (t1
), TYPE_MAXVAL (t2
));
1418 if (CODE_CONTAINS_STRUCT (code
, TS_LIST
))
1420 compare_tree_edges (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1421 compare_tree_edges (TREE_VALUE (t1
), TREE_VALUE (t2
));
1422 compare_tree_edges (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1425 if (CODE_CONTAINS_STRUCT (code
, TS_VEC
))
1426 for (int i
= 0; i
< TREE_VEC_LENGTH (t1
); i
++)
1427 compare_tree_edges (TREE_VEC_ELT (t1
, i
), TREE_VEC_ELT (t2
, i
));
1429 if (CODE_CONTAINS_STRUCT (code
, TS_EXP
))
1431 for (int i
= 0; i
< TREE_OPERAND_LENGTH (t1
); i
++)
1432 compare_tree_edges (TREE_OPERAND (t1
, i
),
1433 TREE_OPERAND (t2
, i
));
1435 /* BLOCKs are function local and we don't merge anything there. */
1436 if (TREE_BLOCK (t1
) || TREE_BLOCK (t2
))
1440 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1444 /* Lengths have already been compared above. */
1445 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1
), i
, t
)
1446 compare_tree_edges (t
, BINFO_BASE_BINFO (t2
, i
));
1447 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1
), i
, t
)
1448 compare_tree_edges (t
, BINFO_BASE_ACCESS (t2
, i
));
1449 compare_tree_edges (BINFO_OFFSET (t1
), BINFO_OFFSET (t2
));
1450 compare_tree_edges (BINFO_VTABLE (t1
), BINFO_VTABLE (t2
));
1451 compare_tree_edges (BINFO_VPTR_FIELD (t1
), BINFO_VPTR_FIELD (t2
));
1452 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1453 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1456 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1460 /* Lengths have already been compared above. */
1461 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, index
, value
)
1463 compare_tree_edges (index
, CONSTRUCTOR_ELT (t2
, i
)->index
);
1464 compare_tree_edges (value
, CONSTRUCTOR_ELT (t2
, i
)->value
);
1468 if (code
== OMP_CLAUSE
)
1472 for (i
= 0; i
< omp_clause_num_ops
[OMP_CLAUSE_CODE (t1
)]; i
++)
1473 compare_tree_edges (OMP_CLAUSE_OPERAND (t1
, i
),
1474 OMP_CLAUSE_OPERAND (t2
, i
));
1475 compare_tree_edges (OMP_CLAUSE_CHAIN (t1
), OMP_CLAUSE_CHAIN (t2
));
1478 #undef compare_tree_edges
1483 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1484 out MAP if they are equal. */
1487 compare_tree_sccs (tree_scc
*pscc
, tree_scc
*scc
,
1490 /* Assume SCC entry hashes are sorted after their cardinality. Which
1491 means we can simply take the first n-tuple of equal hashes
1492 (which is recorded as entry_len) and do n SCC entry candidate
1494 for (unsigned i
= 0; i
< pscc
->entry_len
; ++i
)
1497 num_scc_compare_collisions
++;
1498 if (compare_tree_sccs_1 (pscc
->entries
[0], scc
->entries
[i
], &mapp
))
1500 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1501 on the scc as all trees will be freed. */
1504 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1505 the SCC prevails. */
1506 for (unsigned j
= 0; j
< scc
->len
; ++j
)
1507 TREE_ASM_WRITTEN (scc
->entries
[j
]) = 0;
1513 /* QSort sort function to sort a map of two pointers after the 2nd
1517 cmp_tree (const void *p1_
, const void *p2_
)
1519 tree
*p1
= (tree
*)(const_cast<void *>(p1_
));
1520 tree
*p2
= (tree
*)(const_cast<void *>(p2_
));
1523 return ((uintptr_t)p1
[1] < (uintptr_t)p2
[1]) ? -1 : 1;
1526 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1527 hash value SCC_HASH with an already recorded SCC. Return true if
1528 that was successful, otherwise return false. */
1531 unify_scc (struct data_in
*data_in
, unsigned from
,
1532 unsigned len
, unsigned scc_entry_len
, hashval_t scc_hash
)
1534 bool unified_p
= false;
1535 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
1537 = (tree_scc
*) alloca (sizeof (tree_scc
) + (len
- 1) * sizeof (tree
));
1539 scc
->hash
= scc_hash
;
1541 scc
->entry_len
= scc_entry_len
;
1542 for (unsigned i
= 0; i
< len
; ++i
)
1544 tree t
= streamer_tree_cache_get_tree (cache
, from
+ i
);
1545 scc
->entries
[i
] = t
;
1546 /* Do not merge SCCs with local entities inside them. Also do
1547 not merge TRANSLATION_UNIT_DECLs. */
1548 if (TREE_CODE (t
) == TRANSLATION_UNIT_DECL
1549 || (VAR_OR_FUNCTION_DECL_P (t
)
1550 && !(TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
1551 || TREE_CODE (t
) == LABEL_DECL
)
1553 /* Avoid doing any work for these cases and do not worry to
1554 record the SCCs for further merging. */
1559 /* Look for the list of candidate SCCs to compare against. */
1561 slot
= tree_scc_hash
->find_slot_with_hash (scc
, scc_hash
, INSERT
);
1564 /* Try unifying against each candidate. */
1567 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1568 outside of the scc when following tree edges. Make sure
1569 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1570 to track whether we visited the SCC member during the compare.
1571 We cannot use TREE_VISITED on the pscc members as the extended
1572 scc and pscc can overlap. */
1573 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1575 TREE_VISITED (scc
->entries
[i
]) = 1;
1576 gcc_checking_assert (!TREE_ASM_WRITTEN (scc
->entries
[i
]));
1579 tree
*map
= XALLOCAVEC (tree
, 2 * len
);
1580 for (tree_scc
*pscc
= *slot
; pscc
; pscc
= pscc
->next
)
1582 if (!compare_tree_sccs (pscc
, scc
, map
))
1585 /* Found an equal SCC. */
1587 num_scc_compare_collisions
--;
1589 total_scc_size_merged
+= len
;
1592 for (unsigned i
= 0; i
< len
; ++i
)
1594 tree t
= map
[2*i
+1];
1595 enum tree_code code
= TREE_CODE (t
);
1596 /* IDENTIFIER_NODEs should be singletons and are merged by the
1597 streamer. The others should be singletons, too, and we
1598 should not merge them in any way. */
1599 gcc_assert (code
!= TRANSLATION_UNIT_DECL
1600 && code
!= IDENTIFIER_NODE
);
1603 /* Fixup the streamer cache with the prevailing nodes according
1604 to the tree node mapping computed by compare_tree_sccs. */
1606 streamer_tree_cache_replace_tree (cache
, pscc
->entries
[0], from
);
1609 tree
*map2
= XALLOCAVEC (tree
, 2 * len
);
1610 for (unsigned i
= 0; i
< len
; ++i
)
1612 map2
[i
*2] = (tree
)(uintptr_t)(from
+ i
);
1613 map2
[i
*2+1] = scc
->entries
[i
];
1615 qsort (map2
, len
, 2 * sizeof (tree
), cmp_tree
);
1616 qsort (map
, len
, 2 * sizeof (tree
), cmp_tree
);
1617 for (unsigned i
= 0; i
< len
; ++i
)
1618 streamer_tree_cache_replace_tree (cache
, map
[2*i
],
1619 (uintptr_t)map2
[2*i
]);
1622 /* Free the tree nodes from the read SCC. */
1623 data_in
->location_cache
.revert_location_cache ();
1624 for (unsigned i
= 0; i
< len
; ++i
)
1626 if (TYPE_P (scc
->entries
[i
]))
1628 free_node (scc
->entries
[i
]);
1634 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1636 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1637 TREE_VISITED (scc
->entries
[i
]) = 0;
1640 /* If we didn't unify it to any candidate duplicate the relevant
1641 pieces to permanent storage and link it into the chain. */
1645 = XOBNEWVAR (&tree_scc_hash_obstack
, tree_scc
, sizeof (tree_scc
));
1646 memcpy (pscc
, scc
, sizeof (tree_scc
));
1647 pscc
->next
= (*slot
);
1654 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1655 RESOLUTIONS is the set of symbols picked by the linker (read from the
1656 resolution file when the linker plugin is being used). */
1659 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
1660 vec
<ld_plugin_symbol_resolution_t
> resolutions
)
1662 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
1663 const int decl_offset
= sizeof (struct lto_decl_header
);
1664 const int main_offset
= decl_offset
+ header
->decl_state_size
;
1665 const int string_offset
= main_offset
+ header
->main_size
;
1666 struct data_in
*data_in
;
1668 const uint32_t *data_ptr
, *data_end
;
1669 uint32_t num_decl_states
;
1671 lto_input_block
ib_main ((const char *) data
+ main_offset
,
1672 header
->main_size
, decl_data
->mode_table
);
1674 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
1675 header
->string_size
, resolutions
);
1677 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1678 internal types that should not be merged. */
1680 /* Read the global declarations and types. */
1681 while (ib_main
.p
< ib_main
.len
)
1684 unsigned from
= data_in
->reader_cache
->nodes
.length ();
1685 /* Read and uniquify SCCs as in the input stream. */
1686 enum LTO_tags tag
= streamer_read_record_start (&ib_main
);
1687 if (tag
== LTO_tree_scc
)
1690 unsigned scc_entry_len
;
1691 hashval_t scc_hash
= lto_input_scc (&ib_main
, data_in
, &len_
,
1693 unsigned len
= data_in
->reader_cache
->nodes
.length () - from
;
1694 gcc_assert (len
== len_
);
1696 total_scc_size
+= len
;
1699 /* We have the special case of size-1 SCCs that are pre-merged
1700 by means of identifier and string sharing for example.
1701 ??? Maybe we should avoid streaming those as SCCs. */
1702 tree first
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1705 && (TREE_CODE (first
) == IDENTIFIER_NODE
1706 || TREE_CODE (first
) == INTEGER_CST
1707 || TREE_CODE (first
) == TRANSLATION_UNIT_DECL
))
1710 /* Try to unify the SCC with already existing ones. */
1712 && unify_scc (data_in
, from
,
1713 len
, scc_entry_len
, scc_hash
))
1716 /* Tree merging failed, mark entries in location cache as
1718 data_in
->location_cache
.accept_location_cache ();
1720 bool seen_type
= false;
1721 for (unsigned i
= 0; i
< len
; ++i
)
1723 tree t
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1725 /* Reconstruct the type variant and pointer-to/reference-to
1730 num_prevailing_types
++;
1731 lto_fixup_prevailing_type (t
);
1733 /* Compute the canonical type of all types.
1734 ??? Should be able to assert that !TYPE_CANONICAL. */
1735 if (TYPE_P (t
) && !TYPE_CANONICAL (t
))
1737 gimple_register_canonical_type (t
);
1739 register_odr_type (t
);
1741 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1742 type which is also member of this SCC. */
1743 if (TREE_CODE (t
) == INTEGER_CST
1744 && !TREE_OVERFLOW (t
))
1745 cache_integer_cst (t
);
1746 /* Register TYPE_DECLs with the debuginfo machinery. */
1748 && TREE_CODE (t
) == TYPE_DECL
)
1750 /* Dwarf2out needs location information.
1751 TODO: Moving this out of the streamer loop may noticealy
1752 improve ltrans linemap memory use. */
1753 data_in
->location_cache
.apply_location_cache ();
1754 debug_hooks
->type_decl (t
, !DECL_FILE_SCOPE_P (t
));
1758 /* Register variables and functions with the
1760 if (TREE_CODE (t
) == VAR_DECL
)
1761 lto_register_var_decl_in_symtab (data_in
, t
, from
+ i
);
1762 else if (TREE_CODE (t
) == FUNCTION_DECL
1763 && !DECL_BUILT_IN (t
))
1764 lto_register_function_decl_in_symtab (data_in
, t
, from
+ i
);
1765 /* Scan the tree for references to global functions or
1766 variables and record those for later fixup. */
1767 if (mentions_vars_p (t
))
1768 vec_safe_push (tree_with_vars
, t
);
1772 num_type_scc_trees
+= len
;
1776 /* Pickle stray references. */
1777 t
= lto_input_tree_1 (&ib_main
, data_in
, tag
, 0);
1778 gcc_assert (t
&& data_in
->reader_cache
->nodes
.length () == from
);
1781 data_in
->location_cache
.apply_location_cache ();
1783 /* Read in lto_in_decl_state objects. */
1784 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
1786 (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
1787 num_decl_states
= *data_ptr
++;
1789 gcc_assert (num_decl_states
> 0);
1790 decl_data
->global_decl_state
= lto_new_in_decl_state ();
1791 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
1792 decl_data
->global_decl_state
);
1794 /* Read in per-function decl states and enter them in hash table. */
1795 decl_data
->function_decl_states
=
1796 hash_table
<decl_state_hasher
>::create_ggc (37);
1798 for (i
= 1; i
< num_decl_states
; i
++)
1800 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
1802 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
1803 lto_in_decl_state
**slot
1804 = decl_data
->function_decl_states
->find_slot (state
, INSERT
);
1805 gcc_assert (*slot
== NULL
);
1809 if (data_ptr
!= data_end
)
1810 internal_error ("bytecode stream: garbage at the end of symbols section");
1812 /* Set the current decl state to be the global state. */
1813 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
1815 lto_data_in_delete (data_in
);
1818 /* Custom version of strtoll, which is not portable. */
1821 lto_parse_hex (const char *p
)
1825 for (; *p
!= '\0'; ++p
)
1830 if (c
>= '0' && c
<= '9')
1832 else if (c
>= 'a' && c
<= 'f')
1833 part
= c
- 'a' + 10;
1834 else if (c
>= 'A' && c
<= 'F')
1835 part
= c
- 'A' + 10;
1837 internal_error ("could not parse hex number");
1844 /* Read resolution for file named FILE_NAME. The resolution is read from
1848 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
1850 /* We require that objects in the resolution file are in the same
1851 order as the lto1 command line. */
1852 unsigned int name_len
;
1854 unsigned int num_symbols
;
1856 struct lto_file_decl_data
*file_data
;
1857 splay_tree_node nd
= NULL
;
1862 name_len
= strlen (file
->filename
);
1863 obj_name
= XNEWVEC (char, name_len
+ 1);
1864 fscanf (resolution
, " "); /* Read white space. */
1866 fread (obj_name
, sizeof (char), name_len
, resolution
);
1867 obj_name
[name_len
] = '\0';
1868 if (filename_cmp (obj_name
, file
->filename
) != 0)
1869 internal_error ("unexpected file name %s in linker resolution file. "
1870 "Expected %s", obj_name
, file
->filename
);
1871 if (file
->offset
!= 0)
1876 t
= fscanf (resolution
, "@0x%16s", offset_p
);
1878 internal_error ("could not parse file offset");
1879 offset
= lto_parse_hex (offset_p
);
1880 if (offset
!= file
->offset
)
1881 internal_error ("unexpected offset");
1886 fscanf (resolution
, "%u", &num_symbols
);
1888 for (i
= 0; i
< num_symbols
; i
++)
1892 unsigned HOST_WIDE_INT id
;
1894 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
1896 unsigned int lto_resolution_str_len
=
1897 sizeof (lto_resolution_str
) / sizeof (char *);
1900 t
= fscanf (resolution
, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
" %26s %*[^\n]\n",
1901 &index
, &id
, r_str
);
1903 internal_error ("invalid line in the resolution file");
1905 for (j
= 0; j
< lto_resolution_str_len
; j
++)
1907 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
1909 r
= (enum ld_plugin_symbol_resolution
) j
;
1913 if (j
== lto_resolution_str_len
)
1914 internal_error ("invalid resolution in the resolution file");
1916 if (!(nd
&& lto_splay_tree_id_equal_p (nd
->key
, id
)))
1918 nd
= lto_splay_tree_lookup (file_ids
, id
);
1920 internal_error ("resolution sub id %wx not in object file", id
);
1923 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1924 /* The indexes are very sparse. To save memory save them in a compact
1925 format that is only unpacked later when the subfile is processed. */
1928 file_data
->respairs
.safe_push (rp
);
1929 if (file_data
->max_index
< index
)
1930 file_data
->max_index
= index
;
1934 /* List of file_decl_datas */
1935 struct file_data_list
1937 struct lto_file_decl_data
*first
, *last
;
1940 /* Is the name for a id'ed LTO section? */
1943 lto_section_with_id (const char *name
, unsigned HOST_WIDE_INT
*id
)
1947 if (strncmp (name
, section_name_prefix
, strlen (section_name_prefix
)))
1949 s
= strrchr (name
, '.');
1950 return s
&& sscanf (s
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, id
) == 1;
1953 /* Create file_data of each sub file id */
1956 create_subid_section_table (struct lto_section_slot
*ls
, splay_tree file_ids
,
1957 struct file_data_list
*list
)
1959 struct lto_section_slot s_slot
, *new_slot
;
1960 unsigned HOST_WIDE_INT id
;
1964 struct lto_file_decl_data
*file_data
;
1966 if (!lto_section_with_id (ls
->name
, &id
))
1969 /* Find hash table of sub module id */
1970 nd
= lto_splay_tree_lookup (file_ids
, id
);
1973 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1977 file_data
= ggc_alloc
<lto_file_decl_data
> ();
1978 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
1980 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();;
1981 lto_splay_tree_insert (file_ids
, id
, file_data
);
1983 /* Maintain list in linker order */
1985 list
->first
= file_data
;
1987 list
->last
->next
= file_data
;
1988 list
->last
= file_data
;
1991 /* Copy section into sub module hash table */
1992 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
1993 s_slot
.name
= new_name
;
1994 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
1995 gcc_assert (*hash_slot
== NULL
);
1997 new_slot
= XDUP (struct lto_section_slot
, ls
);
1998 new_slot
->name
= new_name
;
1999 *hash_slot
= new_slot
;
2003 /* Read declarations and other initializations for a FILE_DATA. */
2006 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
)
2010 vec
<ld_plugin_symbol_resolution_t
>
2011 resolutions
= vNULL
;
2015 /* Create vector for fast access of resolution. We do this lazily
2017 resolutions
.safe_grow_cleared (file_data
->max_index
+ 1);
2018 for (i
= 0; file_data
->respairs
.iterate (i
, &rp
); i
++)
2019 resolutions
[rp
->index
] = rp
->res
;
2020 file_data
->respairs
.release ();
2022 file_data
->renaming_hash_table
= lto_create_renaming_table ();
2023 file_data
->file_name
= file
->filename
;
2024 #ifdef ACCEL_COMPILER
2025 lto_input_mode_table (file_data
);
2027 file_data
->mode_table
= lto_mode_identity_table
;
2029 data
= lto_get_section_data (file_data
, LTO_section_decls
, NULL
, &len
);
2032 internal_error ("cannot read LTO decls from %s", file_data
->file_name
);
2035 /* Frees resolutions */
2036 lto_read_decls (file_data
, data
, resolutions
);
2037 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
2040 /* Finalize FILE_DATA in FILE and increase COUNT. */
2043 lto_create_files_from_ids (lto_file
*file
, struct lto_file_decl_data
*file_data
,
2046 lto_file_finalize (file_data
, file
);
2047 if (symtab
->dump_file
)
2048 fprintf (symtab
->dump_file
,
2049 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX
"\n",
2050 file_data
->file_name
, file_data
->id
);
2055 /* Generate a TREE representation for all types and external decls
2058 Read all of the globals out of the file. Then read the cgraph
2059 and process the .o index into the cgraph nodes so that it can open
2060 the .o file to load the functions and ipa information. */
2062 static struct lto_file_decl_data
*
2063 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
2065 struct lto_file_decl_data
*file_data
= NULL
;
2066 splay_tree file_ids
;
2067 htab_t section_hash_table
;
2068 struct lto_section_slot
*section
;
2069 struct file_data_list file_list
;
2070 struct lto_section_list section_list
;
2072 memset (§ion_list
, 0, sizeof (struct lto_section_list
));
2073 section_hash_table
= lto_obj_build_section_table (file
, §ion_list
);
2075 /* Find all sub modules in the object and put their sections into new hash
2076 tables in a splay tree. */
2077 file_ids
= lto_splay_tree_new ();
2078 memset (&file_list
, 0, sizeof (struct file_data_list
));
2079 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
2080 create_subid_section_table (section
, file_ids
, &file_list
);
2082 /* Add resolutions to file ids */
2083 lto_resolution_read (file_ids
, resolution_file
, file
);
2085 /* Finalize each lto file for each submodule in the merged object */
2086 for (file_data
= file_list
.first
; file_data
!= NULL
; file_data
= file_data
->next
)
2087 lto_create_files_from_ids (file
, file_data
, count
);
2089 splay_tree_delete (file_ids
);
2090 htab_delete (section_hash_table
);
2092 return file_list
.first
;
2095 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2096 #define LTO_MMAP_IO 1
2100 /* Page size of machine is used for mmap and munmap calls. */
2101 static size_t page_mask
;
2104 /* Get the section data of length LEN from FILENAME starting at
2105 OFFSET. The data segment must be freed by the caller when the
2106 caller is finished. Returns NULL if all was not well. */
2109 lto_read_section_data (struct lto_file_decl_data
*file_data
,
2110 intptr_t offset
, size_t len
)
2114 static char *fd_name
;
2116 intptr_t computed_len
;
2117 intptr_t computed_offset
;
2121 /* Keep a single-entry file-descriptor cache. The last file we
2122 touched will get closed at exit.
2123 ??? Eventually we want to add a more sophisticated larger cache
2124 or rather fix function body streaming to not stream them in
2125 practically random order. */
2127 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
2135 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
2138 fatal_error (input_location
, "Cannot open %s", file_data
->file_name
);
2141 fd_name
= xstrdup (file_data
->file_name
);
2147 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
2148 page_mask
= ~(page_size
- 1);
2151 computed_offset
= offset
& page_mask
;
2152 diff
= offset
- computed_offset
;
2153 computed_len
= len
+ diff
;
2155 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
2156 fd
, computed_offset
);
2157 if (result
== MAP_FAILED
)
2159 fatal_error (input_location
, "Cannot map %s", file_data
->file_name
);
2163 return result
+ diff
;
2165 result
= (char *) xmalloc (len
);
2166 if (lseek (fd
, offset
, SEEK_SET
) != offset
2167 || read (fd
, result
, len
) != (ssize_t
) len
)
2170 fatal_error (input_location
, "Cannot read %s", file_data
->file_name
);
2174 /* Native windows doesn't supports delayed unlink on opened file. So
2175 we close file here again. This produces higher I/O load, but at least
2176 it prevents to have dangling file handles preventing unlink. */
2187 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2188 NAME will be NULL unless the section type is for a function
2192 get_section_data (struct lto_file_decl_data
*file_data
,
2193 enum lto_section_type section_type
,
2197 htab_t section_hash_table
= file_data
->section_hash_table
;
2198 struct lto_section_slot
*f_slot
;
2199 struct lto_section_slot s_slot
;
2200 const char *section_name
= lto_get_section_name (section_type
, name
, file_data
);
2204 s_slot
.name
= section_name
;
2205 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
2208 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
2212 free (CONST_CAST (char *, section_name
));
2217 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2218 starts at OFFSET and has LEN bytes. */
2221 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
2222 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
2223 const char *name ATTRIBUTE_UNUSED
,
2224 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
2227 intptr_t computed_len
;
2228 intptr_t computed_offset
;
2233 computed_offset
= ((intptr_t) offset
) & page_mask
;
2234 diff
= (intptr_t) offset
- computed_offset
;
2235 computed_len
= len
+ diff
;
2237 munmap ((caddr_t
) computed_offset
, computed_len
);
2239 free (CONST_CAST(char *, offset
));
2243 static lto_file
*current_lto_file
;
2245 /* Helper for qsort; compare partitions and return one with smaller size.
2246 We sort from greatest to smallest so parallel build doesn't stale on the
2247 longest compilation being executed too late. */
2250 cmp_partitions_size (const void *a
, const void *b
)
2252 const struct ltrans_partition_def
*pa
2253 = *(struct ltrans_partition_def
*const *)a
;
2254 const struct ltrans_partition_def
*pb
2255 = *(struct ltrans_partition_def
*const *)b
;
2256 return pb
->insns
- pa
->insns
;
2259 /* Helper for qsort; compare partitions and return one with smaller order. */
2262 cmp_partitions_order (const void *a
, const void *b
)
2264 const struct ltrans_partition_def
*pa
2265 = *(struct ltrans_partition_def
*const *)a
;
2266 const struct ltrans_partition_def
*pb
2267 = *(struct ltrans_partition_def
*const *)b
;
2268 int ordera
= -1, orderb
= -1;
2270 if (lto_symtab_encoder_size (pa
->encoder
))
2271 ordera
= lto_symtab_encoder_deref (pa
->encoder
, 0)->order
;
2272 if (lto_symtab_encoder_size (pb
->encoder
))
2273 orderb
= lto_symtab_encoder_deref (pb
->encoder
, 0)->order
;
2274 return orderb
- ordera
;
2277 /* Actually stream out ENCODER into TEMP_FILENAME. */
2280 do_stream_out (char *temp_filename
, lto_symtab_encoder_t encoder
)
2282 lto_file
*file
= lto_obj_file_open (temp_filename
, true);
2284 fatal_error (input_location
, "lto_obj_file_open() failed");
2285 lto_set_current_out_file (file
);
2287 ipa_write_optimization_summaries (encoder
);
2289 lto_set_current_out_file (NULL
);
2290 lto_obj_file_close (file
);
2294 /* Wait for forked process and signal errors. */
2295 #ifdef HAVE_WORKING_FORK
2303 #define WCONTINUED 0
2305 int w
= waitpid (0, &status
, WUNTRACED
| WCONTINUED
);
2307 fatal_error (input_location
, "waitpid failed");
2309 if (WIFEXITED (status
) && WEXITSTATUS (status
))
2310 fatal_error (input_location
, "streaming subprocess failed");
2311 else if (WIFSIGNALED (status
))
2312 fatal_error (input_location
,
2313 "streaming subprocess was killed by signal");
2315 while (!WIFEXITED (status
) && !WIFSIGNALED (status
));
2319 /* Stream out ENCODER into TEMP_FILENAME
2320 Fork if that seems to help. */
2323 stream_out (char *temp_filename
, lto_symtab_encoder_t encoder
,
2324 bool ARG_UNUSED (last
))
2326 #ifdef HAVE_WORKING_FORK
2329 if (lto_parallelism
<= 1)
2331 do_stream_out (temp_filename
, encoder
);
2335 /* Do not run more than LTO_PARALLELISM streamings
2336 FIXME: we ignore limits on jobserver. */
2337 if (lto_parallelism
> 0 && nruns
>= lto_parallelism
)
2342 /* If this is not the last parallel partition, execute new
2343 streaming process. */
2346 pid_t cpid
= fork ();
2350 setproctitle ("lto1-wpa-streaming");
2351 do_stream_out (temp_filename
, encoder
);
2354 /* Fork failed; lets do the job ourseleves. */
2355 else if (cpid
== -1)
2356 do_stream_out (temp_filename
, encoder
);
2360 /* Last partition; stream it and wait for all children to die. */
2364 do_stream_out (temp_filename
, encoder
);
2365 for (i
= 0; i
< nruns
; i
++)
2368 asm_nodes_output
= true;
2370 do_stream_out (temp_filename
, encoder
);
2374 /* Write all output files in WPA mode and the file with the list of
2378 lto_wpa_write_files (void)
2381 ltrans_partition part
;
2382 FILE *ltrans_output_list_stream
;
2383 char *temp_filename
;
2384 vec
<char *>temp_filenames
= vNULL
;
2387 /* Open the LTRANS output list. */
2388 if (!ltrans_output_list
)
2389 fatal_error (input_location
, "no LTRANS output list filename provided");
2391 timevar_push (TV_WHOPR_WPA
);
2393 FOR_EACH_VEC_ELT (ltrans_partitions
, i
, part
)
2394 lto_stats
.num_output_symtab_nodes
+= lto_symtab_encoder_size (part
->encoder
);
2396 timevar_pop (TV_WHOPR_WPA
);
2398 timevar_push (TV_WHOPR_WPA_IO
);
2400 /* Generate a prefix for the LTRANS unit files. */
2401 blen
= strlen (ltrans_output_list
);
2402 temp_filename
= (char *) xmalloc (blen
+ sizeof ("2147483648.o"));
2403 strcpy (temp_filename
, ltrans_output_list
);
2404 if (blen
> sizeof (".out")
2405 && strcmp (temp_filename
+ blen
- sizeof (".out") + 1,
2407 temp_filename
[blen
- sizeof (".out") + 1] = '\0';
2408 blen
= strlen (temp_filename
);
2410 n_sets
= ltrans_partitions
.length ();
2412 /* Sort partitions by size so small ones are compiled last.
2413 FIXME: Even when not reordering we may want to output one list for parallel make
2414 and other for final link command. */
2416 if (!flag_profile_reorder_functions
|| !flag_profile_use
)
2417 ltrans_partitions
.qsort (flag_toplevel_reorder
2418 ? cmp_partitions_size
2419 : cmp_partitions_order
);
2421 for (i
= 0; i
< n_sets
; i
++)
2423 ltrans_partition part
= ltrans_partitions
[i
];
2425 /* Write all the nodes in SET. */
2426 sprintf (temp_filename
+ blen
, "%u.o", i
);
2429 fprintf (stderr
, " %s (%s %i insns)", temp_filename
, part
->name
, part
->insns
);
2430 if (symtab
->dump_file
)
2432 lto_symtab_encoder_iterator lsei
;
2434 fprintf (symtab
->dump_file
, "Writing partition %s to file %s, %i insns\n",
2435 part
->name
, temp_filename
, part
->insns
);
2436 fprintf (symtab
->dump_file
, " Symbols in partition: ");
2437 for (lsei
= lsei_start_in_partition (part
->encoder
); !lsei_end_p (lsei
);
2438 lsei_next_in_partition (&lsei
))
2440 symtab_node
*node
= lsei_node (lsei
);
2441 fprintf (symtab
->dump_file
, "%s ", node
->asm_name ());
2443 fprintf (symtab
->dump_file
, "\n Symbols in boundary: ");
2444 for (lsei
= lsei_start (part
->encoder
); !lsei_end_p (lsei
);
2447 symtab_node
*node
= lsei_node (lsei
);
2448 if (!lto_symtab_encoder_in_partition_p (part
->encoder
, node
))
2450 fprintf (symtab
->dump_file
, "%s ", node
->asm_name ());
2451 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
2453 && lto_symtab_encoder_encode_body_p (part
->encoder
, cnode
))
2454 fprintf (symtab
->dump_file
, "(body included)");
2457 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
2459 && lto_symtab_encoder_encode_initializer_p (part
->encoder
, vnode
))
2460 fprintf (symtab
->dump_file
, "(initializer included)");
2464 fprintf (symtab
->dump_file
, "\n");
2466 gcc_checking_assert (lto_symtab_encoder_size (part
->encoder
) || !i
);
2468 stream_out (temp_filename
, part
->encoder
, i
== n_sets
- 1);
2470 part
->encoder
= NULL
;
2472 temp_filenames
.safe_push (xstrdup (temp_filename
));
2474 ltrans_output_list_stream
= fopen (ltrans_output_list
, "w");
2475 if (ltrans_output_list_stream
== NULL
)
2476 fatal_error (input_location
,
2477 "opening LTRANS output list %s: %m", ltrans_output_list
);
2478 for (i
= 0; i
< n_sets
; i
++)
2480 unsigned int len
= strlen (temp_filenames
[i
]);
2481 if (fwrite (temp_filenames
[i
], 1, len
, ltrans_output_list_stream
) < len
2482 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
2483 fatal_error (input_location
, "writing to LTRANS output list %s: %m",
2484 ltrans_output_list
);
2485 free (temp_filenames
[i
]);
2487 temp_filenames
.release();
2489 lto_stats
.num_output_files
+= n_sets
;
2491 /* Close the LTRANS output list. */
2492 if (fclose (ltrans_output_list_stream
))
2493 fatal_error (input_location
,
2494 "closing LTRANS output list %s: %m", ltrans_output_list
);
2496 free_ltrans_partitions();
2497 free (temp_filename
);
2499 timevar_pop (TV_WHOPR_WPA_IO
);
2503 /* If TT is a variable or function decl replace it with its
2504 prevailing variant. */
2505 #define LTO_SET_PREVAIL(tt) \
2507 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2508 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2510 tt = lto_symtab_prevailing_decl (tt); \
2515 /* Ensure that TT isn't a replacable var of function decl. */
2516 #define LTO_NO_PREVAIL(tt) \
2517 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2519 /* Given a tree T replace all fields referring to variables or functions
2520 with their prevailing variant. */
2522 lto_fixup_prevailing_decls (tree t
)
2524 enum tree_code code
= TREE_CODE (t
);
2527 gcc_checking_assert (code
!= TREE_BINFO
);
2528 LTO_NO_PREVAIL (TREE_TYPE (t
));
2529 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
)
2530 /* lto_symtab_prevail_decl use TREE_CHAIN to link to the prevailing decl.
2531 in the case T is a prevailed declaration we would ICE here. */
2532 && !VAR_OR_FUNCTION_DECL_P (t
))
2533 LTO_NO_PREVAIL (TREE_CHAIN (t
));
2536 LTO_NO_PREVAIL (DECL_NAME (t
));
2537 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
2538 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
2540 LTO_SET_PREVAIL (DECL_SIZE (t
));
2541 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
2542 LTO_SET_PREVAIL (DECL_INITIAL (t
));
2543 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
2544 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
2546 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
2548 LTO_NO_PREVAIL (t
->decl_with_vis
.assembler_name
);
2550 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
2552 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
2554 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
2556 LTO_NO_PREVAIL (DECL_ARGUMENTS (t
));
2557 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
2558 LTO_NO_PREVAIL (DECL_VINDEX (t
));
2560 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
2562 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t
));
2563 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
2564 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
2565 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
2566 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
2569 else if (TYPE_P (t
))
2571 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
2572 LTO_SET_PREVAIL (TYPE_SIZE (t
));
2573 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
2574 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
2575 LTO_NO_PREVAIL (TYPE_NAME (t
));
2577 LTO_SET_PREVAIL (TYPE_MINVAL (t
));
2578 LTO_SET_PREVAIL (TYPE_MAXVAL (t
));
2579 LTO_NO_PREVAIL (t
->type_non_common
.binfo
);
2581 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
2583 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
2584 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
2585 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
2587 else if (EXPR_P (t
))
2590 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
2591 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
2593 else if (TREE_CODE (t
) == CONSTRUCTOR
)
2597 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
2598 LTO_SET_PREVAIL (val
);
2605 LTO_SET_PREVAIL (TREE_VALUE (t
));
2606 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
2607 LTO_NO_PREVAIL (TREE_PURPOSE (t
));
2613 /* If we fixed nothing, then we missed something seen by
2615 gcc_checking_assert (fixed
);
2617 #undef LTO_SET_PREVAIL
2618 #undef LTO_NO_PREVAIL
2620 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2621 replaces var and function decls with the corresponding prevailing def. */
2624 lto_fixup_state (struct lto_in_decl_state
*state
)
2628 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2629 we still need to walk from all DECLs to find the reachable
2630 FUNCTION_DECLs and VAR_DECLs. */
2631 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
2633 vec
<tree
, va_gc
> *trees
= state
->streams
[si
];
2634 for (i
= 0; i
< vec_safe_length (trees
); i
++)
2636 tree t
= (*trees
)[i
];
2637 if (flag_checking
&& TYPE_P (t
))
2639 if (VAR_OR_FUNCTION_DECL_P (t
)
2640 && (TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
2641 (*trees
)[i
] = lto_symtab_prevailing_decl (t
);
2646 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2650 lto_fixup_decls (struct lto_file_decl_data
**files
)
2656 FOR_EACH_VEC_ELT ((*tree_with_vars
), i
, t
)
2657 lto_fixup_prevailing_decls (t
);
2659 for (i
= 0; files
[i
]; i
++)
2661 struct lto_file_decl_data
*file
= files
[i
];
2662 struct lto_in_decl_state
*state
= file
->global_decl_state
;
2663 lto_fixup_state (state
);
2665 hash_table
<decl_state_hasher
>::iterator iter
;
2666 lto_in_decl_state
*elt
;
2667 FOR_EACH_HASH_TABLE_ELEMENT (*file
->function_decl_states
, elt
,
2668 lto_in_decl_state
*, iter
)
2669 lto_fixup_state (elt
);
2673 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
2675 /* Turn file datas for sub files into a single array, so that they look
2676 like separate files for further passes. */
2679 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
, int last_file_ix
)
2681 struct lto_file_decl_data
*n
, *next
;
2684 lto_stats
.num_input_files
= count
;
2686 = ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (count
+ 1);
2687 /* Set the hooks so that all of the ipa passes can read in their data. */
2688 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2689 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
2691 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
2693 all_file_decl_data
[k
++] = n
;
2698 all_file_decl_data
[k
] = NULL
;
2699 gcc_assert (k
== count
);
2702 /* Input file data before flattening (i.e. splitting them to subfiles to support
2703 incremental linking. */
2704 static int real_file_count
;
2705 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
2707 static void print_lto_report_1 (void);
2709 /* Read all the symbols from the input files FNAMES. NFILES is the
2710 number of files requested in the command line. Instantiate a
2711 global call graph by aggregating all the sub-graphs found in each
2715 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
2717 unsigned int i
, last_file_ix
;
2720 struct lto_file_decl_data
**decl_data
;
2723 symtab
->initialize ();
2725 timevar_push (TV_IPA_LTO_DECL_IN
);
2727 #ifdef ACCEL_COMPILER
2728 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2729 lto_stream_offload_p
= true;
2733 = decl_data
= ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (nfiles
+ 1);
2734 real_file_count
= nfiles
;
2736 /* Read the resolution file. */
2738 if (resolution_file_name
)
2741 unsigned num_objects
;
2743 resolution
= fopen (resolution_file_name
, "r");
2744 if (resolution
== NULL
)
2745 fatal_error (input_location
,
2746 "could not open symbol resolution file: %m");
2748 t
= fscanf (resolution
, "%u", &num_objects
);
2749 gcc_assert (t
== 1);
2751 /* True, since the plugin splits the archives. */
2752 gcc_assert (num_objects
== nfiles
);
2754 symtab
->state
= LTO_STREAMING
;
2756 canonical_type_hash_cache
= new hash_map
<const_tree
, hashval_t
> (251);
2757 gimple_canonical_types
= htab_create (16381, gimple_canonical_type_hash
,
2758 gimple_canonical_type_eq
, NULL
);
2759 gcc_obstack_init (&tree_scc_hash_obstack
);
2760 tree_scc_hash
= new hash_table
<tree_scc_hasher
> (4096);
2762 /* Register the common node types with the canonical type machinery so
2763 we properly share alias-sets across languages and TUs. Do not
2764 expose the common nodes as type merge target - those that should be
2765 are already exposed so by pre-loading the LTO streamer caches.
2766 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2767 for (i
= 0; i
< itk_none
; ++i
)
2768 lto_register_canonical_types (integer_types
[i
], true);
2769 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2770 lto_register_canonical_types (sizetype_tab
[i
], true);
2771 for (i
= 0; i
< TI_MAX
; ++i
)
2772 lto_register_canonical_types (global_trees
[i
], true);
2773 for (i
= 0; i
< itk_none
; ++i
)
2774 lto_register_canonical_types (integer_types
[i
], false);
2775 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2776 lto_register_canonical_types (sizetype_tab
[i
], false);
2777 for (i
= 0; i
< TI_MAX
; ++i
)
2778 lto_register_canonical_types (global_trees
[i
], false);
2781 fprintf (stderr
, "Reading object files:");
2783 /* Read all of the object files specified on the command line. */
2784 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
2786 struct lto_file_decl_data
*file_data
= NULL
;
2789 fprintf (stderr
, " %s", fnames
[i
]);
2793 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
2794 if (!current_lto_file
)
2797 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
2800 lto_obj_file_close (current_lto_file
);
2801 free (current_lto_file
);
2802 current_lto_file
= NULL
;
2806 decl_data
[last_file_ix
++] = file_data
;
2808 lto_obj_file_close (current_lto_file
);
2809 free (current_lto_file
);
2810 current_lto_file
= NULL
;
2813 lto_flatten_files (decl_data
, count
, last_file_ix
);
2814 lto_stats
.num_input_files
= count
;
2815 ggc_free(decl_data
);
2816 real_file_decl_data
= NULL
;
2818 if (resolution_file_name
)
2819 fclose (resolution
);
2821 /* Show the LTO report before launching LTRANS. */
2822 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
2823 print_lto_report_1 ();
2825 /* Free gimple type merging datastructures. */
2826 delete tree_scc_hash
;
2827 tree_scc_hash
= NULL
;
2828 obstack_free (&tree_scc_hash_obstack
, NULL
);
2829 htab_delete (gimple_canonical_types
);
2830 gimple_canonical_types
= NULL
;
2831 delete canonical_type_hash_cache
;
2832 canonical_type_hash_cache
= NULL
;
2834 /* At this stage we know that majority of GGC memory is reachable.
2835 Growing the limits prevents unnecesary invocation of GGC. */
2839 /* Set the hooks so that all of the ipa passes can read in their data. */
2840 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2842 timevar_pop (TV_IPA_LTO_DECL_IN
);
2845 fprintf (stderr
, "\nReading the callgraph\n");
2847 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
2848 /* Read the symtab. */
2851 input_offload_tables (!flag_ltrans
);
2853 /* Store resolutions into the symbol table. */
2855 ld_plugin_symbol_resolution_t
*res
;
2856 FOR_EACH_SYMBOL (snode
)
2857 if (snode
->real_symbol_p ()
2858 && snode
->lto_file_data
2859 && snode
->lto_file_data
->resolution_map
2860 && (res
= snode
->lto_file_data
->resolution_map
->get (snode
->decl
)))
2861 snode
->resolution
= *res
;
2862 for (i
= 0; all_file_decl_data
[i
]; i
++)
2863 if (all_file_decl_data
[i
]->resolution_map
)
2865 delete all_file_decl_data
[i
]->resolution_map
;
2866 all_file_decl_data
[i
]->resolution_map
= NULL
;
2869 timevar_pop (TV_IPA_LTO_CGRAPH_IO
);
2872 fprintf (stderr
, "Merging declarations\n");
2874 timevar_push (TV_IPA_LTO_DECL_MERGE
);
2875 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2876 need to care about resolving symbols again, we only need to replace
2877 duplicated declarations read from the callgraph and from function
2881 lto_symtab_merge_decls ();
2883 /* If there were errors during symbol merging bail out, we have no
2884 good way to recover here. */
2886 fatal_error (input_location
,
2887 "errors during merging of translation units");
2889 /* Fixup all decls. */
2890 lto_fixup_decls (all_file_decl_data
);
2893 ggc_free (tree_with_vars
);
2894 tree_with_vars
= NULL
;
2897 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
2898 /* Each pass will set the appropriate timer. */
2901 fprintf (stderr
, "Reading summaries\n");
2903 /* Read the IPA summary data. */
2905 ipa_read_optimization_summaries ();
2907 ipa_read_summaries ();
2909 for (i
= 0; all_file_decl_data
[i
]; i
++)
2911 gcc_assert (all_file_decl_data
[i
]->symtab_node_encoder
);
2912 lto_symtab_encoder_delete (all_file_decl_data
[i
]->symtab_node_encoder
);
2913 all_file_decl_data
[i
]->symtab_node_encoder
= NULL
;
2914 lto_free_function_in_decl_state (all_file_decl_data
[i
]->global_decl_state
);
2915 all_file_decl_data
[i
]->global_decl_state
= NULL
;
2916 all_file_decl_data
[i
]->current_decl_state
= NULL
;
2919 /* Finally merge the cgraph according to the decl merging decisions. */
2920 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
2921 if (symtab
->dump_file
)
2923 fprintf (symtab
->dump_file
, "Before merging:\n");
2924 symtab_node::dump_table (symtab
->dump_file
);
2928 lto_symtab_merge_symbols ();
2929 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2930 we are still having duplicated comdat groups containing local statics.
2931 We could also just remove them while merging. */
2932 symtab
->remove_unreachable_nodes (dump_file
);
2935 symtab
->state
= IPA_SSA
;
2936 /* FIXME: Technically all node removals happening here are useless, because
2937 WPA should not stream them. */
2939 symtab
->remove_unreachable_nodes (dump_file
);
2941 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
2943 /* Indicate that the cgraph is built and ready. */
2944 symtab
->function_flags_ready
= true;
2946 ggc_free (all_file_decl_data
);
2947 all_file_decl_data
= NULL
;
2951 /* Materialize all the bodies for all the nodes in the callgraph. */
2954 materialize_cgraph (void)
2956 struct cgraph_node
*node
;
2957 timevar_id_t lto_timer
;
2961 flag_wpa
? "Materializing decls:" : "Reading function bodies:");
2964 FOR_EACH_FUNCTION (node
)
2966 if (node
->lto_file_data
)
2968 lto_materialize_function (node
);
2969 lto_stats
.num_input_cgraph_nodes
++;
2974 /* Start the appropriate timer depending on the mode that we are
2976 lto_timer
= (flag_wpa
) ? TV_WHOPR_WPA
2977 : (flag_ltrans
) ? TV_WHOPR_LTRANS
2979 timevar_push (lto_timer
);
2981 current_function_decl
= NULL
;
2985 fprintf (stderr
, "\n");
2987 timevar_pop (lto_timer
);
2991 /* Show various memory usage statistics related to LTO. */
2993 print_lto_report_1 (void)
2995 const char *pfx
= (flag_lto
) ? "LTO" : (flag_wpa
) ? "WPA" : "LTRANS";
2996 fprintf (stderr
, "%s statistics\n", pfx
);
2998 fprintf (stderr
, "[%s] read %lu SCCs of average size %f\n",
2999 pfx
, num_sccs_read
, total_scc_size
/ (double)num_sccs_read
);
3000 fprintf (stderr
, "[%s] %lu tree bodies read in total\n", pfx
, total_scc_size
);
3001 if (flag_wpa
&& tree_scc_hash
)
3003 fprintf (stderr
, "[%s] tree SCC table: size %ld, %ld elements, "
3004 "collision ratio: %f\n", pfx
,
3005 (long) tree_scc_hash
->size (),
3006 (long) tree_scc_hash
->elements (),
3007 tree_scc_hash
->collisions ());
3008 hash_table
<tree_scc_hasher
>::iterator hiter
;
3009 tree_scc
*scc
, *max_scc
= NULL
;
3010 unsigned max_length
= 0;
3011 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash
, scc
, x
, hiter
)
3013 unsigned length
= 0;
3015 for (; s
; s
= s
->next
)
3017 if (length
> max_length
)
3019 max_length
= length
;
3023 fprintf (stderr
, "[%s] tree SCC max chain length %u (size %u)\n",
3024 pfx
, max_length
, max_scc
->len
);
3025 fprintf (stderr
, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx
,
3026 num_scc_compares
, num_scc_compare_collisions
,
3027 num_scc_compare_collisions
/ (double) num_scc_compares
);
3028 fprintf (stderr
, "[%s] Merged %lu SCCs\n", pfx
, num_sccs_merged
);
3029 fprintf (stderr
, "[%s] Merged %lu tree bodies\n", pfx
,
3030 total_scc_size_merged
);
3031 fprintf (stderr
, "[%s] Merged %lu types\n", pfx
, num_merged_types
);
3032 fprintf (stderr
, "[%s] %lu types prevailed (%lu associated trees)\n",
3033 pfx
, num_prevailing_types
, num_type_scc_trees
);
3034 fprintf (stderr
, "[%s] GIMPLE canonical type table: size %ld, "
3035 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx
,
3036 (long) htab_size (gimple_canonical_types
),
3037 (long) htab_elements (gimple_canonical_types
),
3038 (long) gimple_canonical_types
->searches
,
3039 (long) gimple_canonical_types
->collisions
,
3040 htab_collisions (gimple_canonical_types
));
3041 fprintf (stderr
, "[%s] GIMPLE canonical type pointer-map: "
3042 "%lu elements, %ld searches\n", pfx
,
3043 num_canonical_type_hash_entries
,
3044 num_canonical_type_hash_queries
);
3047 print_lto_report (pfx
);
3050 /* Perform whole program analysis (WPA) on the callgraph and write out the
3051 optimization plan. */
3054 do_whole_program_analysis (void)
3058 lto_parallelism
= 1;
3060 /* TODO: jobserver communicatoin is not supported, yet. */
3061 if (!strcmp (flag_wpa
, "jobserver"))
3062 lto_parallelism
= -1;
3065 lto_parallelism
= atoi (flag_wpa
);
3066 if (lto_parallelism
<= 0)
3067 lto_parallelism
= 0;
3070 timevar_start (TV_PHASE_OPT_GEN
);
3072 /* Note that since we are in WPA mode, materialize_cgraph will not
3073 actually read in all the function bodies. It only materializes
3074 the decls and cgraph nodes so that analysis can be performed. */
3075 materialize_cgraph ();
3077 /* Reading in the cgraph uses different timers, start timing WPA now. */
3078 timevar_push (TV_WHOPR_WPA
);
3080 if (pre_ipa_mem_report
)
3082 fprintf (stderr
, "Memory consumption before IPA\n");
3083 dump_memory_report (false);
3086 symtab
->function_flags_ready
= true;
3088 if (symtab
->dump_file
)
3089 symtab_node::dump_table (symtab
->dump_file
);
3090 bitmap_obstack_initialize (NULL
);
3091 symtab
->state
= IPA_SSA
;
3093 execute_ipa_pass_list (g
->get_passes ()->all_regular_ipa_passes
);
3095 /* When WPA analysis raises errors, do not bother to output anything. */
3099 if (symtab
->dump_file
)
3101 fprintf (symtab
->dump_file
, "Optimized ");
3102 symtab_node::dump_table (symtab
->dump_file
);
3105 symtab_node::checking_verify_symtab_nodes ();
3106 bitmap_obstack_release (NULL
);
3108 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3109 timevar_pop (TV_WHOPR_WPA
);
3111 timevar_push (TV_WHOPR_PARTITIONING
);
3112 if (flag_lto_partition
== LTO_PARTITION_1TO1
)
3114 else if (flag_lto_partition
== LTO_PARTITION_MAX
)
3116 else if (flag_lto_partition
== LTO_PARTITION_ONE
)
3117 lto_balanced_map (1, INT_MAX
);
3118 else if (flag_lto_partition
== LTO_PARTITION_BALANCED
)
3119 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS
),
3120 PARAM_VALUE (MAX_PARTITION_SIZE
));
3124 /* Inline summaries are needed for balanced partitioning. Free them now so
3125 the memory can be used for streamer caches. */
3126 inline_free_summary ();
3128 /* AUX pointers are used by partitioning code to bookkeep number of
3129 partitions symbol is in. This is no longer needed. */
3130 FOR_EACH_SYMBOL (node
)
3133 lto_stats
.num_cgraph_partitions
+= ltrans_partitions
.length ();
3135 /* Find out statics that need to be promoted
3136 to globals with hidden visibility because they are accessed from multiple
3138 lto_promote_cross_file_statics ();
3139 timevar_pop (TV_WHOPR_PARTITIONING
);
3141 timevar_stop (TV_PHASE_OPT_GEN
);
3143 /* Collect a last time - in lto_wpa_write_files we may end up forking
3144 with the idea that this doesn't increase memory usage. So we
3145 absoultely do not want to collect after that. */
3148 timevar_start (TV_PHASE_STREAM_OUT
);
3151 fprintf (stderr
, "\nStreaming out");
3154 lto_wpa_write_files ();
3156 fprintf (stderr
, "\n");
3157 timevar_stop (TV_PHASE_STREAM_OUT
);
3159 if (post_ipa_mem_report
)
3161 fprintf (stderr
, "Memory consumption after IPA\n");
3162 dump_memory_report (false);
3165 /* Show the LTO report before launching LTRANS. */
3166 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
3167 print_lto_report_1 ();
3169 dump_memory_report (true);
3173 static GTY(()) tree lto_eh_personality_decl
;
3175 /* Return the LTO personality function decl. */
3178 lto_eh_personality (void)
3180 if (!lto_eh_personality_decl
)
3182 /* Use the first personality DECL for our personality if we don't
3183 support multiple ones. This ensures that we don't artificially
3184 create the need for them in a single-language program. */
3185 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
3186 lto_eh_personality_decl
= first_personality_decl
;
3188 lto_eh_personality_decl
= lhd_gcc_personality ();
3191 return lto_eh_personality_decl
;
3194 /* Set the process name based on the LTO mode. */
3197 lto_process_name (void)
3200 setproctitle ("lto1-lto");
3202 setproctitle ("lto1-wpa");
3204 setproctitle ("lto1-ltrans");
3208 /* Initialize the LTO front end. */
3213 lto_process_name ();
3214 lto_streamer_hooks_init ();
3216 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
3217 memset (<o_stats
, 0, sizeof (lto_stats
));
3218 bitmap_obstack_initialize (NULL
);
3219 gimple_register_cfg_hooks ();
3220 #ifndef ACCEL_COMPILER
3221 unsigned char *table
3222 = ggc_vec_alloc
<unsigned char> (MAX_MACHINE_MODE
);
3223 for (int m
= 0; m
< MAX_MACHINE_MODE
; m
++)
3225 lto_mode_identity_table
= table
;
3229 /* Create artificial pointers for "omp declare target link" vars. */
3232 offload_handle_link_vars (void)
3234 #ifdef ACCEL_COMPILER
3236 FOR_EACH_VARIABLE (var
)
3237 if (lookup_attribute ("omp declare target link",
3238 DECL_ATTRIBUTES (var
->decl
)))
3240 tree type
= build_pointer_type (TREE_TYPE (var
->decl
));
3241 tree link_ptr_var
= make_node (VAR_DECL
);
3242 TREE_TYPE (link_ptr_var
) = type
;
3243 TREE_USED (link_ptr_var
) = 1;
3244 TREE_STATIC (link_ptr_var
) = 1;
3245 DECL_MODE (link_ptr_var
) = TYPE_MODE (type
);
3246 DECL_SIZE (link_ptr_var
) = TYPE_SIZE (type
);
3247 DECL_SIZE_UNIT (link_ptr_var
) = TYPE_SIZE_UNIT (type
);
3248 DECL_ARTIFICIAL (link_ptr_var
) = 1;
3249 tree var_name
= DECL_ASSEMBLER_NAME (var
->decl
);
3251 = ACONCAT ((IDENTIFIER_POINTER (var_name
), "_linkptr", NULL
));
3252 DECL_NAME (link_ptr_var
) = get_identifier (new_name
);
3253 SET_DECL_ASSEMBLER_NAME (link_ptr_var
, DECL_NAME (link_ptr_var
));
3254 SET_DECL_VALUE_EXPR (var
->decl
, build_simple_mem_ref (link_ptr_var
));
3255 DECL_HAS_VALUE_EXPR_P (var
->decl
) = 1;
3261 /* Main entry point for the GIMPLE front end. This front end has
3262 three main personalities:
3264 - LTO (-flto). All the object files on the command line are
3265 loaded in memory and processed as a single translation unit.
3266 This is the traditional link-time optimization behavior.
3268 - WPA (-fwpa). Only the callgraph and summary information for
3269 files in the command file are loaded. A single callgraph
3270 (without function bodies) is instantiated for the whole set of
3271 files. IPA passes are only allowed to analyze the call graph
3272 and make transformation decisions. The callgraph is
3273 partitioned, each partition is written to a new object file
3274 together with the transformation decisions.
3276 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3277 summary files from running again. Since WPA computed summary
3278 information and decided what transformations to apply, LTRANS
3279 simply applies them. */
3284 /* LTO is called as a front end, even though it is not a front end.
3285 Because it is called as a front end, TV_PHASE_PARSING and
3286 TV_PARSE_GLOBAL are active, and we need to turn them off while
3287 doing LTO. Later we turn them back on so they are active up in
3289 timevar_pop (TV_PARSE_GLOBAL
);
3290 timevar_stop (TV_PHASE_PARSING
);
3292 timevar_start (TV_PHASE_SETUP
);
3294 /* Initialize the LTO front end. */
3297 timevar_stop (TV_PHASE_SETUP
);
3298 timevar_start (TV_PHASE_STREAM_IN
);
3300 /* Read all the symbols and call graph from all the files in the
3302 read_cgraph_and_symbols (num_in_fnames
, in_fnames
);
3304 timevar_stop (TV_PHASE_STREAM_IN
);
3308 offload_handle_link_vars ();
3310 /* If WPA is enabled analyze the whole call graph and create an
3311 optimization plan. Otherwise, read in all the function
3312 bodies and continue with optimization. */
3314 do_whole_program_analysis ();
3317 timevar_start (TV_PHASE_OPT_GEN
);
3319 materialize_cgraph ();
3321 lto_promote_statics_nonwpa ();
3323 /* Annotate the CU DIE and mark the early debug phase as finished. */
3324 debug_hooks
->early_finish ("<artificial>");
3326 /* Let the middle end know that we have read and merged all of
3330 timevar_stop (TV_PHASE_OPT_GEN
);
3332 /* FIXME lto, if the processes spawned by WPA fail, we miss
3333 the chance to print WPA's report, so WPA will call
3334 print_lto_report before launching LTRANS. If LTRANS was
3335 launched directly by the driver we would not need to do
3337 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
3338 print_lto_report_1 ();
3342 /* Here we make LTO pretend to be a parser. */
3343 timevar_start (TV_PHASE_PARSING
);
3344 timevar_push (TV_PARSE_GLOBAL
);
3347 #include "gt-lto-lto.h"