1 /* Top-level LTO routines.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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"
29 #include "double-int.h"
37 #include "fixed-value.h"
39 #include "fold-const.h"
40 #include "stor-layout.h"
41 #include "diagnostic-core.h"
44 #include "basic-block.h"
47 #include "plugin-api.h"
48 #include "hard-reg-set.h"
53 #include "tree-ssa-operands.h"
54 #include "tree-pass.h"
55 #include "langhooks.h"
58 #include "alloc-pool.h"
59 #include "symbol-summary.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-expr.h"
69 #include "lto-streamer.h"
70 #include "lto-section-names.h"
71 #include "tree-streamer.h"
72 #include "splay-tree.h"
73 #include "lto-partition.h"
74 #include "data-streamer.h"
76 #include "pass_manager.h"
77 #include "ipa-inline.h"
79 #include "ipa-utils.h"
80 #include "gomp-constants.h"
83 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
84 static int lto_parallelism
;
86 static GTY(()) tree first_personality_decl
;
88 static GTY(()) const unsigned char *lto_mode_identity_table
;
90 /* Returns a hash code for P. */
93 hash_name (const void *p
)
95 const struct lto_section_slot
*ds
= (const struct lto_section_slot
*) p
;
96 return (hashval_t
) htab_hash_string (ds
->name
);
100 /* Returns nonzero if P1 and P2 are equal. */
103 eq_name (const void *p1
, const void *p2
)
105 const struct lto_section_slot
*s1
=
106 (const struct lto_section_slot
*) p1
;
107 const struct lto_section_slot
*s2
=
108 (const struct lto_section_slot
*) p2
;
110 return strcmp (s1
->name
, s2
->name
) == 0;
113 /* Free lto_section_slot */
116 free_with_string (void *arg
)
118 struct lto_section_slot
*s
= (struct lto_section_slot
*)arg
;
120 free (CONST_CAST (char *, s
->name
));
124 /* Create section hash table */
127 lto_obj_create_section_hash_table (void)
129 return htab_create (37, hash_name
, eq_name
, free_with_string
);
132 /* Delete an allocated integer KEY in the splay tree. */
135 lto_splay_tree_delete_id (splay_tree_key key
)
140 /* Compare splay tree node ids A and B. */
143 lto_splay_tree_compare_ids (splay_tree_key a
, splay_tree_key b
)
145 unsigned HOST_WIDE_INT ai
;
146 unsigned HOST_WIDE_INT bi
;
148 ai
= *(unsigned HOST_WIDE_INT
*) a
;
149 bi
= *(unsigned HOST_WIDE_INT
*) b
;
158 /* Look up splay tree node by ID in splay tree T. */
160 static splay_tree_node
161 lto_splay_tree_lookup (splay_tree t
, unsigned HOST_WIDE_INT id
)
163 return splay_tree_lookup (t
, (splay_tree_key
) &id
);
166 /* Check if KEY has ID. */
169 lto_splay_tree_id_equal_p (splay_tree_key key
, unsigned HOST_WIDE_INT id
)
171 return *(unsigned HOST_WIDE_INT
*) key
== id
;
174 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
175 The ID is allocated separately because we need HOST_WIDE_INTs which may
176 be wider than a splay_tree_key. */
179 lto_splay_tree_insert (splay_tree t
, unsigned HOST_WIDE_INT id
,
180 struct lto_file_decl_data
*file_data
)
182 unsigned HOST_WIDE_INT
*idp
= XCNEW (unsigned HOST_WIDE_INT
);
184 splay_tree_insert (t
, (splay_tree_key
) idp
, (splay_tree_value
) file_data
);
187 /* Create a splay tree. */
190 lto_splay_tree_new (void)
192 return splay_tree_new (lto_splay_tree_compare_ids
,
193 lto_splay_tree_delete_id
,
197 /* Return true when NODE has a clone that is analyzed (i.e. we need
198 to load its body even if the node itself is not needed). */
201 has_analyzed_clone_p (struct cgraph_node
*node
)
203 struct cgraph_node
*orig
= node
;
212 else if (node
->next_sibling_clone
)
213 node
= node
->next_sibling_clone
;
216 while (node
!= orig
&& !node
->next_sibling_clone
)
217 node
= node
->clone_of
;
219 node
= node
->next_sibling_clone
;
225 /* Read the function body for the function associated with NODE. */
228 lto_materialize_function (struct cgraph_node
*node
)
233 /* Read in functions with body (analyzed nodes)
234 and also functions that are needed to produce virtual clones. */
235 if ((node
->has_gimple_body_p () && node
->analyzed
)
236 || node
->used_as_abstract_origin
237 || has_analyzed_clone_p (node
))
239 /* Clones don't need to be read. */
242 if (DECL_FUNCTION_PERSONALITY (decl
) && !first_personality_decl
)
243 first_personality_decl
= DECL_FUNCTION_PERSONALITY (decl
);
246 /* Let the middle end know about the function. */
247 rest_of_decl_compilation (decl
, 1, 0);
251 /* Decode the content of memory pointed to by DATA in the in decl
252 state object STATE. DATA_IN points to a data_in structure for
253 decoding. Return the address after the decoded object in the
256 static const uint32_t *
257 lto_read_in_decl_state (struct data_in
*data_in
, const uint32_t *data
,
258 struct lto_in_decl_state
*state
)
265 decl
= streamer_tree_cache_get_tree (data_in
->reader_cache
, ix
);
266 if (!VAR_OR_FUNCTION_DECL_P (decl
))
268 gcc_assert (decl
== void_type_node
);
271 state
->fn_decl
= decl
;
273 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
275 uint32_t size
= *data
++;
276 vec
<tree
, va_gc
> *decls
= NULL
;
277 vec_alloc (decls
, size
);
279 for (j
= 0; j
< size
; j
++)
280 vec_safe_push (decls
,
281 streamer_tree_cache_get_tree (data_in
->reader_cache
,
284 state
->streams
[i
] = decls
;
292 /* Global canonical type table. */
293 static htab_t gimple_canonical_types
;
294 static hash_map
<const_tree
, hashval_t
> *canonical_type_hash_cache
;
295 static unsigned long num_canonical_type_hash_entries
;
296 static unsigned long num_canonical_type_hash_queries
;
298 static void iterative_hash_canonical_type (tree type
, inchash::hash
&hstate
);
299 static hashval_t
gimple_canonical_type_hash (const void *p
);
300 static void gimple_register_canonical_type_1 (tree t
, hashval_t hash
);
302 /* Returning a hash value for gimple type TYPE.
304 The hash value returned is equal for types considered compatible
305 by gimple_canonical_types_compatible_p. */
308 hash_canonical_type (tree type
)
310 inchash::hash hstate
;
312 /* We compute alias sets only for types that needs them.
313 Be sure we do not recurse to something else as we can not hash incomplete
314 types in a way they would have same hash value as compatible complete
316 gcc_checking_assert (type_with_alias_set_p (type
));
318 /* Combine a few common features of types so that types are grouped into
319 smaller sets; when searching for existing matching types to merge,
320 only existing types having the same features as the new type will be
322 hstate
.add_int (TREE_CODE (type
));
323 hstate
.add_int (TYPE_MODE (type
));
325 /* Incorporate common features of numerical types. */
326 if (INTEGRAL_TYPE_P (type
)
327 || SCALAR_FLOAT_TYPE_P (type
)
328 || FIXED_POINT_TYPE_P (type
)
329 || TREE_CODE (type
) == OFFSET_TYPE
330 || POINTER_TYPE_P (type
))
332 hstate
.add_int (TYPE_UNSIGNED (type
));
333 hstate
.add_int (TYPE_PRECISION (type
));
336 if (VECTOR_TYPE_P (type
))
338 hstate
.add_int (TYPE_VECTOR_SUBPARTS (type
));
339 hstate
.add_int (TYPE_UNSIGNED (type
));
342 if (TREE_CODE (type
) == COMPLEX_TYPE
)
343 hstate
.add_int (TYPE_UNSIGNED (type
));
345 /* For pointer and reference types, fold in information about the type
346 pointed to but do not recurse to the pointed-to type. */
347 if (POINTER_TYPE_P (type
))
349 hstate
.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type
)));
350 hstate
.add_int (TREE_CODE (TREE_TYPE (type
)));
353 /* For integer types hash only the string flag. */
354 if (TREE_CODE (type
) == INTEGER_TYPE
)
355 hstate
.add_int (TYPE_STRING_FLAG (type
));
357 /* For array types hash the domain bounds and the string flag. */
358 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
360 hstate
.add_int (TYPE_STRING_FLAG (type
));
361 /* OMP lowering can introduce error_mark_node in place of
362 random local decls in types. */
363 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type
)) != error_mark_node
)
364 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type
)), hstate
);
365 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) != error_mark_node
)
366 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)), hstate
);
369 /* Recurse for aggregates with a single element type. */
370 if (TREE_CODE (type
) == ARRAY_TYPE
371 || TREE_CODE (type
) == COMPLEX_TYPE
372 || TREE_CODE (type
) == VECTOR_TYPE
)
373 iterative_hash_canonical_type (TREE_TYPE (type
), hstate
);
375 /* Incorporate function return and argument types. */
376 if (TREE_CODE (type
) == FUNCTION_TYPE
|| TREE_CODE (type
) == METHOD_TYPE
)
381 iterative_hash_canonical_type (TREE_TYPE (type
), hstate
);
383 for (p
= TYPE_ARG_TYPES (type
), na
= 0; p
; p
= TREE_CHAIN (p
))
385 iterative_hash_canonical_type (TREE_VALUE (p
), hstate
);
392 if (RECORD_OR_UNION_TYPE_P (type
))
397 for (f
= TYPE_FIELDS (type
), nf
= 0; f
; f
= TREE_CHAIN (f
))
398 if (TREE_CODE (f
) == FIELD_DECL
)
400 iterative_hash_canonical_type (TREE_TYPE (f
), hstate
);
410 /* Returning a hash value for gimple type TYPE combined with VAL. */
413 iterative_hash_canonical_type (tree type
, inchash::hash
&hstate
)
417 /* All type variants have same TYPE_CANONICAL. */
418 type
= TYPE_MAIN_VARIANT (type
);
419 /* An already processed type. */
420 if (TYPE_CANONICAL (type
))
422 type
= TYPE_CANONICAL (type
);
423 v
= gimple_canonical_type_hash (type
);
427 /* Canonical types should not be able to form SCCs by design, this
428 recursion is just because we do not register canonical types in
429 optimal order. To avoid quadratic behavior also register the
431 v
= hash_canonical_type (type
);
432 gimple_register_canonical_type_1 (type
, v
);
437 /* Returns the hash for a canonical type P. */
440 gimple_canonical_type_hash (const void *p
)
442 num_canonical_type_hash_queries
++;
443 hashval_t
*slot
= canonical_type_hash_cache
->get ((const_tree
) p
);
444 gcc_assert (slot
!= NULL
);
450 /* Returns nonzero if P1 and P2 are equal. */
453 gimple_canonical_type_eq (const void *p1
, const void *p2
)
455 const_tree t1
= (const_tree
) p1
;
456 const_tree t2
= (const_tree
) p2
;
457 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1
),
458 CONST_CAST_TREE (t2
));
461 /* Main worker for gimple_register_canonical_type. */
464 gimple_register_canonical_type_1 (tree t
, hashval_t hash
)
468 gcc_checking_assert (TYPE_P (t
) && !TYPE_CANONICAL (t
));
470 slot
= htab_find_slot_with_hash (gimple_canonical_types
, t
, hash
, INSERT
);
473 tree new_type
= (tree
)(*slot
);
474 gcc_checking_assert (new_type
!= t
);
475 TYPE_CANONICAL (t
) = new_type
;
479 TYPE_CANONICAL (t
) = t
;
481 /* Cache the just computed hash value. */
482 num_canonical_type_hash_entries
++;
483 bool existed_p
= canonical_type_hash_cache
->put (t
, hash
);
484 gcc_assert (!existed_p
);
488 /* Register type T in the global type table gimple_types and set
489 TYPE_CANONICAL of T accordingly.
490 This is used by LTO to merge structurally equivalent types for
491 type-based aliasing purposes across different TUs and languages.
493 ??? This merging does not exactly match how the tree.c middle-end
494 functions will assign TYPE_CANONICAL when new types are created
495 during optimization (which at least happens for pointer and array
499 gimple_register_canonical_type (tree t
)
501 if (TYPE_CANONICAL (t
) || !type_with_alias_set_p (t
))
504 /* Canonical types are same among all complete variants. */
505 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
)))
506 TYPE_CANONICAL (t
) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
));
509 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t
),
510 hash_canonical_type (TYPE_MAIN_VARIANT (t
)));
511 TYPE_CANONICAL (t
) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t
));
515 /* Re-compute TYPE_CANONICAL for NODE and related types. */
518 lto_register_canonical_types (tree node
, bool first_p
)
525 TYPE_CANONICAL (node
) = NULL_TREE
;
527 if (POINTER_TYPE_P (node
)
528 || TREE_CODE (node
) == COMPLEX_TYPE
529 || TREE_CODE (node
) == ARRAY_TYPE
)
530 lto_register_canonical_types (TREE_TYPE (node
), first_p
);
533 gimple_register_canonical_type (node
);
537 /* Remember trees that contains references to declarations. */
538 static GTY(()) vec
<tree
, va_gc
> *tree_with_vars
;
540 #define CHECK_VAR(tt) \
543 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
544 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
548 #define CHECK_NO_VAR(tt) \
549 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
551 /* Check presence of pointers to decls in fields of a tree_typed T. */
554 mentions_vars_p_typed (tree t
)
556 CHECK_NO_VAR (TREE_TYPE (t
));
560 /* Check presence of pointers to decls in fields of a tree_common T. */
563 mentions_vars_p_common (tree t
)
565 if (mentions_vars_p_typed (t
))
567 CHECK_NO_VAR (TREE_CHAIN (t
));
571 /* Check presence of pointers to decls in fields of a decl_minimal T. */
574 mentions_vars_p_decl_minimal (tree t
)
576 if (mentions_vars_p_common (t
))
578 CHECK_NO_VAR (DECL_NAME (t
));
579 CHECK_VAR (DECL_CONTEXT (t
));
583 /* Check presence of pointers to decls in fields of a decl_common T. */
586 mentions_vars_p_decl_common (tree t
)
588 if (mentions_vars_p_decl_minimal (t
))
590 CHECK_VAR (DECL_SIZE (t
));
591 CHECK_VAR (DECL_SIZE_UNIT (t
));
592 CHECK_VAR (DECL_INITIAL (t
));
593 CHECK_NO_VAR (DECL_ATTRIBUTES (t
));
594 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t
));
598 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
601 mentions_vars_p_decl_with_vis (tree t
)
603 if (mentions_vars_p_decl_common (t
))
606 /* Accessor macro has side-effects, use field-name here. */
607 CHECK_NO_VAR (t
->decl_with_vis
.assembler_name
);
611 /* Check presence of pointers to decls in fields of a decl_non_common T. */
614 mentions_vars_p_decl_non_common (tree t
)
616 if (mentions_vars_p_decl_with_vis (t
))
618 CHECK_NO_VAR (DECL_RESULT_FLD (t
));
622 /* Check presence of pointers to decls in fields of a decl_non_common T. */
625 mentions_vars_p_function (tree t
)
627 if (mentions_vars_p_decl_non_common (t
))
629 CHECK_NO_VAR (DECL_ARGUMENTS (t
));
630 CHECK_NO_VAR (DECL_VINDEX (t
));
631 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t
));
635 /* Check presence of pointers to decls in fields of a field_decl T. */
638 mentions_vars_p_field_decl (tree t
)
640 if (mentions_vars_p_decl_common (t
))
642 CHECK_VAR (DECL_FIELD_OFFSET (t
));
643 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t
));
644 CHECK_NO_VAR (DECL_QUALIFIER (t
));
645 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t
));
646 CHECK_NO_VAR (DECL_FCONTEXT (t
));
650 /* Check presence of pointers to decls in fields of a type T. */
653 mentions_vars_p_type (tree t
)
655 if (mentions_vars_p_common (t
))
657 CHECK_NO_VAR (TYPE_CACHED_VALUES (t
));
658 CHECK_VAR (TYPE_SIZE (t
));
659 CHECK_VAR (TYPE_SIZE_UNIT (t
));
660 CHECK_NO_VAR (TYPE_ATTRIBUTES (t
));
661 CHECK_NO_VAR (TYPE_NAME (t
));
663 CHECK_VAR (TYPE_MINVAL (t
));
664 CHECK_VAR (TYPE_MAXVAL (t
));
666 /* Accessor is for derived node types only. */
667 CHECK_NO_VAR (t
->type_non_common
.binfo
);
669 CHECK_VAR (TYPE_CONTEXT (t
));
670 CHECK_NO_VAR (TYPE_CANONICAL (t
));
671 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t
));
672 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t
));
676 /* Check presence of pointers to decls in fields of a BINFO T. */
679 mentions_vars_p_binfo (tree t
)
681 unsigned HOST_WIDE_INT i
, n
;
683 if (mentions_vars_p_common (t
))
685 CHECK_VAR (BINFO_VTABLE (t
));
686 CHECK_NO_VAR (BINFO_OFFSET (t
));
687 CHECK_NO_VAR (BINFO_VIRTUALS (t
));
688 CHECK_NO_VAR (BINFO_VPTR_FIELD (t
));
689 n
= vec_safe_length (BINFO_BASE_ACCESSES (t
));
690 for (i
= 0; i
< n
; i
++)
691 CHECK_NO_VAR (BINFO_BASE_ACCESS (t
, i
));
692 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
693 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
694 n
= BINFO_N_BASE_BINFOS (t
);
695 for (i
= 0; i
< n
; i
++)
696 CHECK_NO_VAR (BINFO_BASE_BINFO (t
, i
));
700 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
703 mentions_vars_p_constructor (tree t
)
705 unsigned HOST_WIDE_INT idx
;
708 if (mentions_vars_p_typed (t
))
711 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
713 CHECK_NO_VAR (ce
->index
);
714 CHECK_VAR (ce
->value
);
719 /* Check presence of pointers to decls in fields of an expression tree T. */
722 mentions_vars_p_expr (tree t
)
725 if (mentions_vars_p_typed (t
))
727 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
728 CHECK_VAR (TREE_OPERAND (t
, i
));
732 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
735 mentions_vars_p_omp_clause (tree t
)
738 if (mentions_vars_p_common (t
))
740 for (i
= omp_clause_num_ops
[OMP_CLAUSE_CODE (t
)] - 1; i
>= 0; --i
)
741 CHECK_VAR (OMP_CLAUSE_OPERAND (t
, i
));
745 /* Check presence of pointers to decls that needs later fixup in T. */
748 mentions_vars_p (tree t
)
750 switch (TREE_CODE (t
))
752 case IDENTIFIER_NODE
:
756 CHECK_VAR (TREE_VALUE (t
));
757 CHECK_VAR (TREE_PURPOSE (t
));
758 CHECK_NO_VAR (TREE_CHAIN (t
));
762 return mentions_vars_p_field_decl (t
);
771 return mentions_vars_p_decl_common (t
);
774 return mentions_vars_p_decl_with_vis (t
);
777 return mentions_vars_p_decl_non_common (t
);
780 return mentions_vars_p_function (t
);
783 return mentions_vars_p_binfo (t
);
785 case PLACEHOLDER_EXPR
:
786 return mentions_vars_p_common (t
);
789 case TRANSLATION_UNIT_DECL
:
790 case OPTIMIZATION_NODE
:
791 case TARGET_OPTION_NODE
:
795 return mentions_vars_p_constructor (t
);
798 return mentions_vars_p_omp_clause (t
);
803 if (mentions_vars_p_type (t
))
808 if (mentions_vars_p_expr (t
))
811 else if (CONSTANT_CLASS_P (t
))
812 CHECK_NO_VAR (TREE_TYPE (t
));
820 /* Return the resolution for the decl with index INDEX from DATA_IN. */
822 static enum ld_plugin_symbol_resolution
823 get_resolution (struct data_in
*data_in
, unsigned index
)
825 if (data_in
->globals_resolution
.exists ())
827 ld_plugin_symbol_resolution_t ret
;
828 /* We can have references to not emitted functions in
829 DECL_FUNCTION_PERSONALITY at least. So we can and have
830 to indeed return LDPR_UNKNOWN in some cases. */
831 if (data_in
->globals_resolution
.length () <= index
)
833 ret
= data_in
->globals_resolution
[index
];
837 /* Delay resolution finding until decl merging. */
841 /* We need to record resolutions until symbol table is read. */
843 register_resolution (struct lto_file_decl_data
*file_data
, tree decl
,
844 enum ld_plugin_symbol_resolution resolution
)
846 if (resolution
== LDPR_UNKNOWN
)
848 if (!file_data
->resolution_map
)
849 file_data
->resolution_map
850 = new hash_map
<tree
, ld_plugin_symbol_resolution
>;
851 file_data
->resolution_map
->put (decl
, resolution
);
854 /* Register DECL with the global symbol table and change its
855 name if necessary to avoid name clashes for static globals across
859 lto_register_var_decl_in_symtab (struct data_in
*data_in
, tree decl
,
864 /* Variable has file scope, not local. */
865 if (!TREE_PUBLIC (decl
)
866 && !((context
= decl_function_context (decl
))
867 && auto_var_in_fn_p (decl
, context
)))
868 rest_of_decl_compilation (decl
, 1, 0);
870 /* If this variable has already been declared, queue the
871 declaration for merging. */
872 if (TREE_PUBLIC (decl
))
873 register_resolution (data_in
->file_data
,
874 decl
, get_resolution (data_in
, ix
));
878 /* Register DECL with the global symbol table and change its
879 name if necessary to avoid name clashes for static globals across
880 different files. DATA_IN contains descriptors and tables for the
884 lto_register_function_decl_in_symtab (struct data_in
*data_in
, tree decl
,
887 /* If this variable has already been declared, queue the
888 declaration for merging. */
889 if (TREE_PUBLIC (decl
) && !DECL_ABSTRACT_P (decl
))
890 register_resolution (data_in
->file_data
,
891 decl
, get_resolution (data_in
, ix
));
895 /* For the type T re-materialize it in the type variant list and
896 the pointer/reference-to chains. */
899 lto_fixup_prevailing_type (tree t
)
901 /* The following re-creates proper variant lists while fixing up
902 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
903 variant list state before fixup is broken. */
905 /* If we are not our own variant leader link us into our new leaders
907 if (TYPE_MAIN_VARIANT (t
) != t
)
909 tree mv
= TYPE_MAIN_VARIANT (t
);
910 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
911 TYPE_NEXT_VARIANT (mv
) = t
;
914 /* The following reconstructs the pointer chains
915 of the new pointed-to type if we are a main variant. We do
916 not stream those so they are broken before fixup. */
917 if (TREE_CODE (t
) == POINTER_TYPE
918 && TYPE_MAIN_VARIANT (t
) == t
)
920 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
921 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
923 else if (TREE_CODE (t
) == REFERENCE_TYPE
924 && TYPE_MAIN_VARIANT (t
) == t
)
926 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
927 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
932 /* We keep prevailing tree SCCs in a hashtable with manual collision
933 handling (in case all hashes compare the same) and keep the colliding
934 entries in the tree_scc->next chain. */
939 /* Hash of the whole SCC. */
941 /* Number of trees in the SCC. */
943 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
944 which share the same individual tree hash). */
946 /* The members of the SCC.
947 We only need to remember the first entry node candidate for prevailing
948 SCCs (but of course have access to all entries for SCCs we are
950 ??? For prevailing SCCs we really only need hash and the first
951 entry candidate, but that's too awkward to implement. */
955 struct tree_scc_hasher
: typed_noop_remove
<tree_scc
>
957 typedef tree_scc
*value_type
;
958 typedef tree_scc
*compare_type
;
959 static inline hashval_t
hash (const tree_scc
*);
960 static inline bool equal (const tree_scc
*, const tree_scc
*);
964 tree_scc_hasher::hash (const tree_scc
*scc
)
970 tree_scc_hasher::equal (const tree_scc
*scc1
, const tree_scc
*scc2
)
972 if (scc1
->hash
!= scc2
->hash
973 || scc1
->len
!= scc2
->len
974 || scc1
->entry_len
!= scc2
->entry_len
)
979 static hash_table
<tree_scc_hasher
> *tree_scc_hash
;
980 static struct obstack tree_scc_hash_obstack
;
982 static unsigned long num_merged_types
;
983 static unsigned long num_prevailing_types
;
984 static unsigned long num_type_scc_trees
;
985 static unsigned long total_scc_size
;
986 static unsigned long num_sccs_read
;
987 static unsigned long total_scc_size_merged
;
988 static unsigned long num_sccs_merged
;
989 static unsigned long num_scc_compares
;
990 static unsigned long num_scc_compare_collisions
;
993 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
994 recursing through in-SCC tree edges. Returns true if the SCCs entered
995 through T1 and T2 are equal and fills in *MAP with the pairs of
996 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
999 compare_tree_sccs_1 (tree t1
, tree t2
, tree
**map
)
1001 enum tree_code code
;
1003 /* Mark already visited nodes. */
1004 TREE_ASM_WRITTEN (t2
) = 1;
1006 /* Push the pair onto map. */
1011 /* Compare value-fields. */
1012 #define compare_values(X) \
1014 if (X(t1) != X(t2)) \
1018 compare_values (TREE_CODE
);
1019 code
= TREE_CODE (t1
);
1023 compare_values (TREE_SIDE_EFFECTS
);
1024 compare_values (TREE_CONSTANT
);
1025 compare_values (TREE_READONLY
);
1026 compare_values (TREE_PUBLIC
);
1028 compare_values (TREE_ADDRESSABLE
);
1029 compare_values (TREE_THIS_VOLATILE
);
1031 compare_values (DECL_UNSIGNED
);
1032 else if (TYPE_P (t1
))
1033 compare_values (TYPE_UNSIGNED
);
1035 compare_values (TYPE_ARTIFICIAL
);
1037 compare_values (TREE_NO_WARNING
);
1038 compare_values (TREE_NOTHROW
);
1039 compare_values (TREE_STATIC
);
1040 if (code
!= TREE_BINFO
)
1041 compare_values (TREE_PRIVATE
);
1042 compare_values (TREE_PROTECTED
);
1043 compare_values (TREE_DEPRECATED
);
1046 compare_values (TYPE_SATURATING
);
1047 compare_values (TYPE_ADDR_SPACE
);
1049 else if (code
== SSA_NAME
)
1050 compare_values (SSA_NAME_IS_DEFAULT_DEF
);
1052 if (CODE_CONTAINS_STRUCT (code
, TS_INT_CST
))
1054 if (!wi::eq_p (t1
, t2
))
1058 if (CODE_CONTAINS_STRUCT (code
, TS_REAL_CST
))
1060 /* ??? No suitable compare routine available. */
1061 REAL_VALUE_TYPE r1
= TREE_REAL_CST (t1
);
1062 REAL_VALUE_TYPE r2
= TREE_REAL_CST (t2
);
1064 || r1
.decimal
!= r2
.decimal
1065 || r1
.sign
!= r2
.sign
1066 || r1
.signalling
!= r2
.signalling
1067 || r1
.canonical
!= r2
.canonical
1068 || r1
.uexp
!= r2
.uexp
)
1070 for (unsigned i
= 0; i
< SIGSZ
; ++i
)
1071 if (r1
.sig
[i
] != r2
.sig
[i
])
1075 if (CODE_CONTAINS_STRUCT (code
, TS_FIXED_CST
))
1076 if (!fixed_compare (EQ_EXPR
,
1077 TREE_FIXED_CST_PTR (t1
), TREE_FIXED_CST_PTR (t2
)))
1081 /* We don't want to compare locations, so there is nothing do compare
1082 for TS_DECL_MINIMAL. */
1084 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1086 compare_values (DECL_MODE
);
1087 compare_values (DECL_NONLOCAL
);
1088 compare_values (DECL_VIRTUAL_P
);
1089 compare_values (DECL_IGNORED_P
);
1090 compare_values (DECL_ABSTRACT_P
);
1091 compare_values (DECL_ARTIFICIAL
);
1092 compare_values (DECL_USER_ALIGN
);
1093 compare_values (DECL_PRESERVE_P
);
1094 compare_values (DECL_EXTERNAL
);
1095 compare_values (DECL_GIMPLE_REG_P
);
1096 compare_values (DECL_ALIGN
);
1097 if (code
== LABEL_DECL
)
1099 compare_values (EH_LANDING_PAD_NR
);
1100 compare_values (LABEL_DECL_UID
);
1102 else if (code
== FIELD_DECL
)
1104 compare_values (DECL_PACKED
);
1105 compare_values (DECL_NONADDRESSABLE_P
);
1106 compare_values (DECL_OFFSET_ALIGN
);
1108 else if (code
== VAR_DECL
)
1110 compare_values (DECL_HAS_DEBUG_EXPR_P
);
1111 compare_values (DECL_NONLOCAL_FRAME
);
1113 if (code
== RESULT_DECL
1114 || code
== PARM_DECL
1115 || code
== VAR_DECL
)
1117 compare_values (DECL_BY_REFERENCE
);
1118 if (code
== VAR_DECL
1119 || code
== PARM_DECL
)
1120 compare_values (DECL_HAS_VALUE_EXPR_P
);
1124 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WRTL
))
1125 compare_values (DECL_REGISTER
);
1127 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1129 compare_values (DECL_COMMON
);
1130 compare_values (DECL_DLLIMPORT_P
);
1131 compare_values (DECL_WEAK
);
1132 compare_values (DECL_SEEN_IN_BIND_EXPR_P
);
1133 compare_values (DECL_COMDAT
);
1134 compare_values (DECL_VISIBILITY
);
1135 compare_values (DECL_VISIBILITY_SPECIFIED
);
1136 if (code
== VAR_DECL
)
1138 compare_values (DECL_HARD_REGISTER
);
1139 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1140 compare_values (DECL_IN_CONSTANT_POOL
);
1144 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1146 compare_values (DECL_BUILT_IN_CLASS
);
1147 compare_values (DECL_STATIC_CONSTRUCTOR
);
1148 compare_values (DECL_STATIC_DESTRUCTOR
);
1149 compare_values (DECL_UNINLINABLE
);
1150 compare_values (DECL_POSSIBLY_INLINED
);
1151 compare_values (DECL_IS_NOVOPS
);
1152 compare_values (DECL_IS_RETURNS_TWICE
);
1153 compare_values (DECL_IS_MALLOC
);
1154 compare_values (DECL_IS_OPERATOR_NEW
);
1155 compare_values (DECL_DECLARED_INLINE_P
);
1156 compare_values (DECL_STATIC_CHAIN
);
1157 compare_values (DECL_NO_INLINE_WARNING_P
);
1158 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT
);
1159 compare_values (DECL_NO_LIMIT_STACK
);
1160 compare_values (DECL_DISREGARD_INLINE_LIMITS
);
1161 compare_values (DECL_PURE_P
);
1162 compare_values (DECL_LOOPING_CONST_OR_PURE_P
);
1163 compare_values (DECL_FINAL_P
);
1164 compare_values (DECL_CXX_CONSTRUCTOR_P
);
1165 compare_values (DECL_CXX_DESTRUCTOR_P
);
1166 if (DECL_BUILT_IN_CLASS (t1
) != NOT_BUILT_IN
)
1167 compare_values (DECL_FUNCTION_CODE
);
1170 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1172 compare_values (TYPE_MODE
);
1173 compare_values (TYPE_STRING_FLAG
);
1174 compare_values (TYPE_NO_FORCE_BLK
);
1175 compare_values (TYPE_NEEDS_CONSTRUCTING
);
1176 if (RECORD_OR_UNION_TYPE_P (t1
))
1178 compare_values (TYPE_TRANSPARENT_AGGR
);
1179 compare_values (TYPE_FINAL_P
);
1181 else if (code
== ARRAY_TYPE
)
1182 compare_values (TYPE_NONALIASED_COMPONENT
);
1183 compare_values (TYPE_PACKED
);
1184 compare_values (TYPE_RESTRICT
);
1185 compare_values (TYPE_USER_ALIGN
);
1186 compare_values (TYPE_READONLY
);
1187 compare_values (TYPE_PRECISION
);
1188 compare_values (TYPE_ALIGN
);
1189 compare_values (TYPE_ALIAS_SET
);
1192 /* We don't want to compare locations, so there is nothing do compare
1195 /* BLOCKs are function local and we don't merge anything there, so
1196 simply refuse to merge. */
1197 if (CODE_CONTAINS_STRUCT (code
, TS_BLOCK
))
1200 if (CODE_CONTAINS_STRUCT (code
, TS_TRANSLATION_UNIT_DECL
))
1201 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1
),
1202 TRANSLATION_UNIT_LANGUAGE (t2
)) != 0)
1205 if (CODE_CONTAINS_STRUCT (code
, TS_TARGET_OPTION
))
1206 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1
), TREE_TARGET_OPTION (t2
)))
1209 if (CODE_CONTAINS_STRUCT (code
, TS_OPTIMIZATION
))
1210 if (memcmp (TREE_OPTIMIZATION (t1
), TREE_OPTIMIZATION (t2
),
1211 sizeof (struct cl_optimization
)) != 0)
1214 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1215 if (vec_safe_length (BINFO_BASE_ACCESSES (t1
))
1216 != vec_safe_length (BINFO_BASE_ACCESSES (t2
)))
1219 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1220 compare_values (CONSTRUCTOR_NELTS
);
1222 if (CODE_CONTAINS_STRUCT (code
, TS_IDENTIFIER
))
1223 if (IDENTIFIER_LENGTH (t1
) != IDENTIFIER_LENGTH (t2
)
1224 || memcmp (IDENTIFIER_POINTER (t1
), IDENTIFIER_POINTER (t2
),
1225 IDENTIFIER_LENGTH (t1
)) != 0)
1228 if (CODE_CONTAINS_STRUCT (code
, TS_STRING
))
1229 if (TREE_STRING_LENGTH (t1
) != TREE_STRING_LENGTH (t2
)
1230 || memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
1231 TREE_STRING_LENGTH (t1
)) != 0)
1234 if (code
== OMP_CLAUSE
)
1236 compare_values (OMP_CLAUSE_CODE
);
1237 switch (OMP_CLAUSE_CODE (t1
))
1239 case OMP_CLAUSE_DEFAULT
:
1240 compare_values (OMP_CLAUSE_DEFAULT_KIND
);
1242 case OMP_CLAUSE_SCHEDULE
:
1243 compare_values (OMP_CLAUSE_SCHEDULE_KIND
);
1245 case OMP_CLAUSE_DEPEND
:
1246 compare_values (OMP_CLAUSE_DEPEND_KIND
);
1248 case OMP_CLAUSE_MAP
:
1249 compare_values (OMP_CLAUSE_MAP_KIND
);
1251 case OMP_CLAUSE_PROC_BIND
:
1252 compare_values (OMP_CLAUSE_PROC_BIND_KIND
);
1254 case OMP_CLAUSE_REDUCTION
:
1255 compare_values (OMP_CLAUSE_REDUCTION_CODE
);
1256 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT
);
1257 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE
);
1264 #undef compare_values
1267 /* Compare pointer fields. */
1269 /* Recurse. Search & Replaced from DFS_write_tree_body.
1270 Folding the early checks into the compare_tree_edges recursion
1271 macro makes debugging way quicker as you are able to break on
1272 compare_tree_sccs_1 and simply finish until a call returns false
1273 to spot the SCC members with the difference. */
1274 #define compare_tree_edges(E1, E2) \
1276 tree t1_ = (E1), t2_ = (E2); \
1279 || !TREE_VISITED (t2_) \
1280 || (!TREE_ASM_WRITTEN (t2_) \
1281 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1283 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1284 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1287 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
1289 if (code
!= IDENTIFIER_NODE
)
1290 compare_tree_edges (TREE_TYPE (t1
), TREE_TYPE (t2
));
1293 if (CODE_CONTAINS_STRUCT (code
, TS_VECTOR
))
1296 /* Note that the number of elements for EXPR has already been emitted
1297 in EXPR's header (see streamer_write_tree_header). */
1298 for (i
= 0; i
< VECTOR_CST_NELTS (t1
); ++i
)
1299 compare_tree_edges (VECTOR_CST_ELT (t1
, i
), VECTOR_CST_ELT (t2
, i
));
1302 if (CODE_CONTAINS_STRUCT (code
, TS_COMPLEX
))
1304 compare_tree_edges (TREE_REALPART (t1
), TREE_REALPART (t2
));
1305 compare_tree_edges (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
1308 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_MINIMAL
))
1310 compare_tree_edges (DECL_NAME (t1
), DECL_NAME (t2
));
1311 /* ??? Global decls from different TUs have non-matching
1312 TRANSLATION_UNIT_DECLs. Only consider a small set of
1313 decls equivalent, we should not end up merging others. */
1314 if ((code
== TYPE_DECL
1315 || code
== NAMESPACE_DECL
1316 || code
== IMPORTED_DECL
1317 || code
== CONST_DECL
1318 || (VAR_OR_FUNCTION_DECL_P (t1
)
1319 && (TREE_PUBLIC (t1
) || DECL_EXTERNAL (t1
))))
1320 && DECL_FILE_SCOPE_P (t1
) && DECL_FILE_SCOPE_P (t2
))
1323 compare_tree_edges (DECL_CONTEXT (t1
), DECL_CONTEXT (t2
));
1326 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1328 compare_tree_edges (DECL_SIZE (t1
), DECL_SIZE (t2
));
1329 compare_tree_edges (DECL_SIZE_UNIT (t1
), DECL_SIZE_UNIT (t2
));
1330 compare_tree_edges (DECL_ATTRIBUTES (t1
), DECL_ATTRIBUTES (t2
));
1331 if ((code
== VAR_DECL
1332 || code
== PARM_DECL
)
1333 && DECL_HAS_VALUE_EXPR_P (t1
))
1334 compare_tree_edges (DECL_VALUE_EXPR (t1
), DECL_VALUE_EXPR (t2
));
1335 if (code
== VAR_DECL
1336 && DECL_HAS_DEBUG_EXPR_P (t1
))
1337 compare_tree_edges (DECL_DEBUG_EXPR (t1
), DECL_DEBUG_EXPR (t2
));
1338 /* LTO specific edges. */
1339 if (code
!= FUNCTION_DECL
1340 && code
!= TRANSLATION_UNIT_DECL
)
1341 compare_tree_edges (DECL_INITIAL (t1
), DECL_INITIAL (t2
));
1344 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
1346 if (code
== FUNCTION_DECL
)
1349 for (a1
= DECL_ARGUMENTS (t1
), a2
= DECL_ARGUMENTS (t2
);
1351 a1
= TREE_CHAIN (a1
), a2
= TREE_CHAIN (a2
))
1352 compare_tree_edges (a1
, a2
);
1353 compare_tree_edges (DECL_RESULT (t1
), DECL_RESULT (t2
));
1355 else if (code
== TYPE_DECL
)
1356 compare_tree_edges (DECL_ORIGINAL_TYPE (t1
), DECL_ORIGINAL_TYPE (t2
));
1359 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1361 /* Make sure we don't inadvertently set the assembler name. */
1362 if (DECL_ASSEMBLER_NAME_SET_P (t1
))
1363 compare_tree_edges (DECL_ASSEMBLER_NAME (t1
),
1364 DECL_ASSEMBLER_NAME (t2
));
1367 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
1369 compare_tree_edges (DECL_FIELD_OFFSET (t1
), DECL_FIELD_OFFSET (t2
));
1370 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1
), DECL_BIT_FIELD_TYPE (t2
));
1371 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1
),
1372 DECL_BIT_FIELD_REPRESENTATIVE (t2
));
1373 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1
),
1374 DECL_FIELD_BIT_OFFSET (t2
));
1375 compare_tree_edges (DECL_FCONTEXT (t1
), DECL_FCONTEXT (t2
));
1378 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1380 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1
),
1381 DECL_FUNCTION_PERSONALITY (t2
));
1382 compare_tree_edges (DECL_VINDEX (t1
), DECL_VINDEX (t2
));
1383 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1
),
1384 DECL_FUNCTION_SPECIFIC_TARGET (t2
));
1385 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1
),
1386 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2
));
1389 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1391 compare_tree_edges (TYPE_SIZE (t1
), TYPE_SIZE (t2
));
1392 compare_tree_edges (TYPE_SIZE_UNIT (t1
), TYPE_SIZE_UNIT (t2
));
1393 compare_tree_edges (TYPE_ATTRIBUTES (t1
), TYPE_ATTRIBUTES (t2
));
1394 compare_tree_edges (TYPE_NAME (t1
), TYPE_NAME (t2
));
1395 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1396 reconstructed during fixup. */
1397 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1399 compare_tree_edges (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
));
1400 /* ??? Global types from different TUs have non-matching
1401 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1403 if (TYPE_FILE_SCOPE_P (t1
) && TYPE_FILE_SCOPE_P (t2
))
1406 compare_tree_edges (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1407 /* TYPE_CANONICAL is re-computed during type merging, so do not
1409 compare_tree_edges (TYPE_STUB_DECL (t1
), TYPE_STUB_DECL (t2
));
1412 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_NON_COMMON
))
1414 if (code
== ENUMERAL_TYPE
)
1415 compare_tree_edges (TYPE_VALUES (t1
), TYPE_VALUES (t2
));
1416 else if (code
== ARRAY_TYPE
)
1417 compare_tree_edges (TYPE_DOMAIN (t1
), TYPE_DOMAIN (t2
));
1418 else if (RECORD_OR_UNION_TYPE_P (t1
))
1421 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1423 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1424 compare_tree_edges (f1
, f2
);
1425 compare_tree_edges (TYPE_BINFO (t1
), TYPE_BINFO (t2
));
1427 else if (code
== FUNCTION_TYPE
1428 || code
== METHOD_TYPE
)
1429 compare_tree_edges (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
));
1430 if (!POINTER_TYPE_P (t1
))
1431 compare_tree_edges (TYPE_MINVAL (t1
), TYPE_MINVAL (t2
));
1432 compare_tree_edges (TYPE_MAXVAL (t1
), TYPE_MAXVAL (t2
));
1435 if (CODE_CONTAINS_STRUCT (code
, TS_LIST
))
1437 compare_tree_edges (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1438 compare_tree_edges (TREE_VALUE (t1
), TREE_VALUE (t2
));
1439 compare_tree_edges (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1442 if (CODE_CONTAINS_STRUCT (code
, TS_VEC
))
1443 for (int i
= 0; i
< TREE_VEC_LENGTH (t1
); i
++)
1444 compare_tree_edges (TREE_VEC_ELT (t1
, i
), TREE_VEC_ELT (t2
, i
));
1446 if (CODE_CONTAINS_STRUCT (code
, TS_EXP
))
1448 for (int i
= 0; i
< TREE_OPERAND_LENGTH (t1
); i
++)
1449 compare_tree_edges (TREE_OPERAND (t1
, i
),
1450 TREE_OPERAND (t2
, i
));
1452 /* BLOCKs are function local and we don't merge anything there. */
1453 if (TREE_BLOCK (t1
) || TREE_BLOCK (t2
))
1457 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1461 /* Lengths have already been compared above. */
1462 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1
), i
, t
)
1463 compare_tree_edges (t
, BINFO_BASE_BINFO (t2
, i
));
1464 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1
), i
, t
)
1465 compare_tree_edges (t
, BINFO_BASE_ACCESS (t2
, i
));
1466 compare_tree_edges (BINFO_OFFSET (t1
), BINFO_OFFSET (t2
));
1467 compare_tree_edges (BINFO_VTABLE (t1
), BINFO_VTABLE (t2
));
1468 compare_tree_edges (BINFO_VPTR_FIELD (t1
), BINFO_VPTR_FIELD (t2
));
1469 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1470 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1473 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1477 /* Lengths have already been compared above. */
1478 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, index
, value
)
1480 compare_tree_edges (index
, CONSTRUCTOR_ELT (t2
, i
)->index
);
1481 compare_tree_edges (value
, CONSTRUCTOR_ELT (t2
, i
)->value
);
1485 if (code
== OMP_CLAUSE
)
1489 for (i
= 0; i
< omp_clause_num_ops
[OMP_CLAUSE_CODE (t1
)]; i
++)
1490 compare_tree_edges (OMP_CLAUSE_OPERAND (t1
, i
),
1491 OMP_CLAUSE_OPERAND (t2
, i
));
1492 compare_tree_edges (OMP_CLAUSE_CHAIN (t1
), OMP_CLAUSE_CHAIN (t2
));
1495 #undef compare_tree_edges
1500 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1501 out MAP if they are equal. */
1504 compare_tree_sccs (tree_scc
*pscc
, tree_scc
*scc
,
1507 /* Assume SCC entry hashes are sorted after their cardinality. Which
1508 means we can simply take the first n-tuple of equal hashes
1509 (which is recorded as entry_len) and do n SCC entry candidate
1511 for (unsigned i
= 0; i
< pscc
->entry_len
; ++i
)
1514 num_scc_compare_collisions
++;
1515 if (compare_tree_sccs_1 (pscc
->entries
[0], scc
->entries
[i
], &mapp
))
1517 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1518 on the scc as all trees will be freed. */
1521 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1522 the SCC prevails. */
1523 for (unsigned j
= 0; j
< scc
->len
; ++j
)
1524 TREE_ASM_WRITTEN (scc
->entries
[j
]) = 0;
1530 /* QSort sort function to sort a map of two pointers after the 2nd
1534 cmp_tree (const void *p1_
, const void *p2_
)
1536 tree
*p1
= (tree
*)(const_cast<void *>(p1_
));
1537 tree
*p2
= (tree
*)(const_cast<void *>(p2_
));
1540 return ((uintptr_t)p1
[1] < (uintptr_t)p2
[1]) ? -1 : 1;
1543 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1544 hash value SCC_HASH with an already recorded SCC. Return true if
1545 that was successful, otherwise return false. */
1548 unify_scc (struct data_in
*data_in
, unsigned from
,
1549 unsigned len
, unsigned scc_entry_len
, hashval_t scc_hash
)
1551 bool unified_p
= false;
1552 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
1554 = (tree_scc
*) alloca (sizeof (tree_scc
) + (len
- 1) * sizeof (tree
));
1556 scc
->hash
= scc_hash
;
1558 scc
->entry_len
= scc_entry_len
;
1559 for (unsigned i
= 0; i
< len
; ++i
)
1561 tree t
= streamer_tree_cache_get_tree (cache
, from
+ i
);
1562 scc
->entries
[i
] = t
;
1563 /* Do not merge SCCs with local entities inside them. Also do
1564 not merge TRANSLATION_UNIT_DECLs. */
1565 if (TREE_CODE (t
) == TRANSLATION_UNIT_DECL
1566 || (VAR_OR_FUNCTION_DECL_P (t
)
1567 && !(TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
1568 || TREE_CODE (t
) == LABEL_DECL
)
1570 /* Avoid doing any work for these cases and do not worry to
1571 record the SCCs for further merging. */
1576 /* Look for the list of candidate SCCs to compare against. */
1578 slot
= tree_scc_hash
->find_slot_with_hash (scc
, scc_hash
, INSERT
);
1581 /* Try unifying against each candidate. */
1584 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1585 outside of the scc when following tree edges. Make sure
1586 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1587 to track whether we visited the SCC member during the compare.
1588 We cannot use TREE_VISITED on the pscc members as the extended
1589 scc and pscc can overlap. */
1590 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1592 TREE_VISITED (scc
->entries
[i
]) = 1;
1593 gcc_checking_assert (!TREE_ASM_WRITTEN (scc
->entries
[i
]));
1596 tree
*map
= XALLOCAVEC (tree
, 2 * len
);
1597 for (tree_scc
*pscc
= *slot
; pscc
; pscc
= pscc
->next
)
1599 if (!compare_tree_sccs (pscc
, scc
, map
))
1602 /* Found an equal SCC. */
1604 num_scc_compare_collisions
--;
1606 total_scc_size_merged
+= len
;
1608 #ifdef ENABLE_CHECKING
1609 for (unsigned i
= 0; i
< len
; ++i
)
1611 tree t
= map
[2*i
+1];
1612 enum tree_code code
= TREE_CODE (t
);
1613 /* IDENTIFIER_NODEs should be singletons and are merged by the
1614 streamer. The others should be singletons, too, and we
1615 should not merge them in any way. */
1616 gcc_assert (code
!= TRANSLATION_UNIT_DECL
1617 && code
!= IDENTIFIER_NODE
1618 && !streamer_handle_as_builtin_p (t
));
1622 /* Fixup the streamer cache with the prevailing nodes according
1623 to the tree node mapping computed by compare_tree_sccs. */
1625 streamer_tree_cache_replace_tree (cache
, pscc
->entries
[0], from
);
1628 tree
*map2
= XALLOCAVEC (tree
, 2 * len
);
1629 for (unsigned i
= 0; i
< len
; ++i
)
1631 map2
[i
*2] = (tree
)(uintptr_t)(from
+ i
);
1632 map2
[i
*2+1] = scc
->entries
[i
];
1634 qsort (map2
, len
, 2 * sizeof (tree
), cmp_tree
);
1635 qsort (map
, len
, 2 * sizeof (tree
), cmp_tree
);
1636 for (unsigned i
= 0; i
< len
; ++i
)
1637 streamer_tree_cache_replace_tree (cache
, map
[2*i
],
1638 (uintptr_t)map2
[2*i
]);
1641 /* Free the tree nodes from the read SCC. */
1642 data_in
->location_cache
.revert_location_cache ();
1643 for (unsigned i
= 0; i
< len
; ++i
)
1645 enum tree_code code
;
1646 if (TYPE_P (scc
->entries
[i
]))
1648 code
= TREE_CODE (scc
->entries
[i
]);
1649 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1650 vec_free (CONSTRUCTOR_ELTS (scc
->entries
[i
]));
1651 ggc_free (scc
->entries
[i
]);
1657 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1659 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1660 TREE_VISITED (scc
->entries
[i
]) = 0;
1663 /* If we didn't unify it to any candidate duplicate the relevant
1664 pieces to permanent storage and link it into the chain. */
1668 = XOBNEWVAR (&tree_scc_hash_obstack
, tree_scc
, sizeof (tree_scc
));
1669 memcpy (pscc
, scc
, sizeof (tree_scc
));
1670 pscc
->next
= (*slot
);
1677 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1678 RESOLUTIONS is the set of symbols picked by the linker (read from the
1679 resolution file when the linker plugin is being used). */
1682 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
1683 vec
<ld_plugin_symbol_resolution_t
> resolutions
)
1685 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
1686 const int decl_offset
= sizeof (struct lto_decl_header
);
1687 const int main_offset
= decl_offset
+ header
->decl_state_size
;
1688 const int string_offset
= main_offset
+ header
->main_size
;
1689 struct data_in
*data_in
;
1691 const uint32_t *data_ptr
, *data_end
;
1692 uint32_t num_decl_states
;
1694 lto_input_block
ib_main ((const char *) data
+ main_offset
,
1695 header
->main_size
, decl_data
->mode_table
);
1697 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
1698 header
->string_size
, resolutions
);
1700 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1701 internal types that should not be merged. */
1703 /* Read the global declarations and types. */
1704 while (ib_main
.p
< ib_main
.len
)
1707 unsigned from
= data_in
->reader_cache
->nodes
.length ();
1708 /* Read and uniquify SCCs as in the input stream. */
1709 enum LTO_tags tag
= streamer_read_record_start (&ib_main
);
1710 if (tag
== LTO_tree_scc
)
1713 unsigned scc_entry_len
;
1714 hashval_t scc_hash
= lto_input_scc (&ib_main
, data_in
, &len_
,
1716 unsigned len
= data_in
->reader_cache
->nodes
.length () - from
;
1717 gcc_assert (len
== len_
);
1719 total_scc_size
+= len
;
1722 /* We have the special case of size-1 SCCs that are pre-merged
1723 by means of identifier and string sharing for example.
1724 ??? Maybe we should avoid streaming those as SCCs. */
1725 tree first
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1728 && (TREE_CODE (first
) == IDENTIFIER_NODE
1729 || TREE_CODE (first
) == INTEGER_CST
1730 || TREE_CODE (first
) == TRANSLATION_UNIT_DECL
1731 || streamer_handle_as_builtin_p (first
)))
1734 /* Try to unify the SCC with already existing ones. */
1736 && unify_scc (data_in
, from
,
1737 len
, scc_entry_len
, scc_hash
))
1740 /* Tree merging failed, mark entries in location cache as
1742 data_in
->location_cache
.accept_location_cache ();
1744 bool seen_type
= false;
1745 for (unsigned i
= 0; i
< len
; ++i
)
1747 tree t
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1749 /* Reconstruct the type variant and pointer-to/reference-to
1754 num_prevailing_types
++;
1755 lto_fixup_prevailing_type (t
);
1757 /* Compute the canonical type of all types.
1758 ??? Should be able to assert that !TYPE_CANONICAL. */
1759 if (TYPE_P (t
) && !TYPE_CANONICAL (t
))
1761 gimple_register_canonical_type (t
);
1763 register_odr_type (t
);
1765 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1766 type which is also member of this SCC. */
1767 if (TREE_CODE (t
) == INTEGER_CST
1768 && !TREE_OVERFLOW (t
))
1769 cache_integer_cst (t
);
1770 /* Register TYPE_DECLs with the debuginfo machinery. */
1772 && TREE_CODE (t
) == TYPE_DECL
)
1774 /* Dwarf2out needs location information.
1775 TODO: Moving this out of the streamer loop may noticealy
1776 improve ltrans linemap memory use. */
1777 data_in
->location_cache
.apply_location_cache ();
1778 debug_hooks
->type_decl (t
, !DECL_FILE_SCOPE_P (t
));
1782 /* Register variables and functions with the
1784 if (TREE_CODE (t
) == VAR_DECL
)
1785 lto_register_var_decl_in_symtab (data_in
, t
, from
+ i
);
1786 else if (TREE_CODE (t
) == FUNCTION_DECL
1787 && !DECL_BUILT_IN (t
))
1788 lto_register_function_decl_in_symtab (data_in
, t
, from
+ i
);
1789 /* Scan the tree for references to global functions or
1790 variables and record those for later fixup. */
1791 if (mentions_vars_p (t
))
1792 vec_safe_push (tree_with_vars
, t
);
1796 num_type_scc_trees
+= len
;
1800 /* Pickle stray references. */
1801 t
= lto_input_tree_1 (&ib_main
, data_in
, tag
, 0);
1802 gcc_assert (t
&& data_in
->reader_cache
->nodes
.length () == from
);
1805 data_in
->location_cache
.apply_location_cache ();
1807 /* Read in lto_in_decl_state objects. */
1808 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
1810 (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
1811 num_decl_states
= *data_ptr
++;
1813 gcc_assert (num_decl_states
> 0);
1814 decl_data
->global_decl_state
= lto_new_in_decl_state ();
1815 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
1816 decl_data
->global_decl_state
);
1818 /* Read in per-function decl states and enter them in hash table. */
1819 decl_data
->function_decl_states
=
1820 hash_table
<decl_state_hasher
>::create_ggc (37);
1822 for (i
= 1; i
< num_decl_states
; i
++)
1824 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
1826 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
1827 lto_in_decl_state
**slot
1828 = decl_data
->function_decl_states
->find_slot (state
, INSERT
);
1829 gcc_assert (*slot
== NULL
);
1833 if (data_ptr
!= data_end
)
1834 internal_error ("bytecode stream: garbage at the end of symbols section");
1836 /* Set the current decl state to be the global state. */
1837 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
1839 lto_data_in_delete (data_in
);
1842 /* Custom version of strtoll, which is not portable. */
1845 lto_parse_hex (const char *p
)
1849 for (; *p
!= '\0'; ++p
)
1854 if (c
>= '0' && c
<= '9')
1856 else if (c
>= 'a' && c
<= 'f')
1857 part
= c
- 'a' + 10;
1858 else if (c
>= 'A' && c
<= 'F')
1859 part
= c
- 'A' + 10;
1861 internal_error ("could not parse hex number");
1868 /* Read resolution for file named FILE_NAME. The resolution is read from
1872 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
1874 /* We require that objects in the resolution file are in the same
1875 order as the lto1 command line. */
1876 unsigned int name_len
;
1878 unsigned int num_symbols
;
1880 struct lto_file_decl_data
*file_data
;
1881 splay_tree_node nd
= NULL
;
1886 name_len
= strlen (file
->filename
);
1887 obj_name
= XNEWVEC (char, name_len
+ 1);
1888 fscanf (resolution
, " "); /* Read white space. */
1890 fread (obj_name
, sizeof (char), name_len
, resolution
);
1891 obj_name
[name_len
] = '\0';
1892 if (filename_cmp (obj_name
, file
->filename
) != 0)
1893 internal_error ("unexpected file name %s in linker resolution file. "
1894 "Expected %s", obj_name
, file
->filename
);
1895 if (file
->offset
!= 0)
1900 t
= fscanf (resolution
, "@0x%16s", offset_p
);
1902 internal_error ("could not parse file offset");
1903 offset
= lto_parse_hex (offset_p
);
1904 if (offset
!= file
->offset
)
1905 internal_error ("unexpected offset");
1910 fscanf (resolution
, "%u", &num_symbols
);
1912 for (i
= 0; i
< num_symbols
; i
++)
1916 unsigned HOST_WIDE_INT id
;
1918 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
1920 unsigned int lto_resolution_str_len
=
1921 sizeof (lto_resolution_str
) / sizeof (char *);
1924 t
= fscanf (resolution
, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
" %26s %*[^\n]\n",
1925 &index
, &id
, r_str
);
1927 internal_error ("invalid line in the resolution file");
1929 for (j
= 0; j
< lto_resolution_str_len
; j
++)
1931 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
1933 r
= (enum ld_plugin_symbol_resolution
) j
;
1937 if (j
== lto_resolution_str_len
)
1938 internal_error ("invalid resolution in the resolution file");
1940 if (!(nd
&& lto_splay_tree_id_equal_p (nd
->key
, id
)))
1942 nd
= lto_splay_tree_lookup (file_ids
, id
);
1944 internal_error ("resolution sub id %wx not in object file", id
);
1947 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1948 /* The indexes are very sparse. To save memory save them in a compact
1949 format that is only unpacked later when the subfile is processed. */
1952 file_data
->respairs
.safe_push (rp
);
1953 if (file_data
->max_index
< index
)
1954 file_data
->max_index
= index
;
1958 /* List of file_decl_datas */
1959 struct file_data_list
1961 struct lto_file_decl_data
*first
, *last
;
1964 /* Is the name for a id'ed LTO section? */
1967 lto_section_with_id (const char *name
, unsigned HOST_WIDE_INT
*id
)
1971 if (strncmp (name
, section_name_prefix
, strlen (section_name_prefix
)))
1973 s
= strrchr (name
, '.');
1974 return s
&& sscanf (s
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, id
) == 1;
1977 /* Create file_data of each sub file id */
1980 create_subid_section_table (struct lto_section_slot
*ls
, splay_tree file_ids
,
1981 struct file_data_list
*list
)
1983 struct lto_section_slot s_slot
, *new_slot
;
1984 unsigned HOST_WIDE_INT id
;
1988 struct lto_file_decl_data
*file_data
;
1990 if (!lto_section_with_id (ls
->name
, &id
))
1993 /* Find hash table of sub module id */
1994 nd
= lto_splay_tree_lookup (file_ids
, id
);
1997 file_data
= (struct lto_file_decl_data
*)nd
->value
;
2001 file_data
= ggc_alloc
<lto_file_decl_data
> ();
2002 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
2004 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();;
2005 lto_splay_tree_insert (file_ids
, id
, file_data
);
2007 /* Maintain list in linker order */
2009 list
->first
= file_data
;
2011 list
->last
->next
= file_data
;
2012 list
->last
= file_data
;
2015 /* Copy section into sub module hash table */
2016 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
2017 s_slot
.name
= new_name
;
2018 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
2019 gcc_assert (*hash_slot
== NULL
);
2021 new_slot
= XDUP (struct lto_section_slot
, ls
);
2022 new_slot
->name
= new_name
;
2023 *hash_slot
= new_slot
;
2027 /* Read declarations and other initializations for a FILE_DATA. */
2030 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
)
2034 vec
<ld_plugin_symbol_resolution_t
>
2035 resolutions
= vNULL
;
2039 /* Create vector for fast access of resolution. We do this lazily
2041 resolutions
.safe_grow_cleared (file_data
->max_index
+ 1);
2042 for (i
= 0; file_data
->respairs
.iterate (i
, &rp
); i
++)
2043 resolutions
[rp
->index
] = rp
->res
;
2044 file_data
->respairs
.release ();
2046 file_data
->renaming_hash_table
= lto_create_renaming_table ();
2047 file_data
->file_name
= file
->filename
;
2048 #ifdef ACCEL_COMPILER
2049 lto_input_mode_table (file_data
);
2051 file_data
->mode_table
= lto_mode_identity_table
;
2053 data
= lto_get_section_data (file_data
, LTO_section_decls
, NULL
, &len
);
2056 internal_error ("cannot read LTO decls from %s", file_data
->file_name
);
2059 /* Frees resolutions */
2060 lto_read_decls (file_data
, data
, resolutions
);
2061 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
2064 /* Finalize FILE_DATA in FILE and increase COUNT. */
2067 lto_create_files_from_ids (lto_file
*file
, struct lto_file_decl_data
*file_data
,
2070 lto_file_finalize (file_data
, file
);
2071 if (symtab
->dump_file
)
2072 fprintf (symtab
->dump_file
,
2073 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX
"\n",
2074 file_data
->file_name
, file_data
->id
);
2079 /* Generate a TREE representation for all types and external decls
2082 Read all of the globals out of the file. Then read the cgraph
2083 and process the .o index into the cgraph nodes so that it can open
2084 the .o file to load the functions and ipa information. */
2086 static struct lto_file_decl_data
*
2087 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
2089 struct lto_file_decl_data
*file_data
= NULL
;
2090 splay_tree file_ids
;
2091 htab_t section_hash_table
;
2092 struct lto_section_slot
*section
;
2093 struct file_data_list file_list
;
2094 struct lto_section_list section_list
;
2096 memset (§ion_list
, 0, sizeof (struct lto_section_list
));
2097 section_hash_table
= lto_obj_build_section_table (file
, §ion_list
);
2099 /* Find all sub modules in the object and put their sections into new hash
2100 tables in a splay tree. */
2101 file_ids
= lto_splay_tree_new ();
2102 memset (&file_list
, 0, sizeof (struct file_data_list
));
2103 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
2104 create_subid_section_table (section
, file_ids
, &file_list
);
2106 /* Add resolutions to file ids */
2107 lto_resolution_read (file_ids
, resolution_file
, file
);
2109 /* Finalize each lto file for each submodule in the merged object */
2110 for (file_data
= file_list
.first
; file_data
!= NULL
; file_data
= file_data
->next
)
2111 lto_create_files_from_ids (file
, file_data
, count
);
2113 splay_tree_delete (file_ids
);
2114 htab_delete (section_hash_table
);
2116 return file_list
.first
;
2119 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2120 #define LTO_MMAP_IO 1
2124 /* Page size of machine is used for mmap and munmap calls. */
2125 static size_t page_mask
;
2128 /* Get the section data of length LEN from FILENAME starting at
2129 OFFSET. The data segment must be freed by the caller when the
2130 caller is finished. Returns NULL if all was not well. */
2133 lto_read_section_data (struct lto_file_decl_data
*file_data
,
2134 intptr_t offset
, size_t len
)
2138 static char *fd_name
;
2140 intptr_t computed_len
;
2141 intptr_t computed_offset
;
2145 /* Keep a single-entry file-descriptor cache. The last file we
2146 touched will get closed at exit.
2147 ??? Eventually we want to add a more sophisticated larger cache
2148 or rather fix function body streaming to not stream them in
2149 practically random order. */
2151 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
2159 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
2162 fatal_error (input_location
, "Cannot open %s", file_data
->file_name
);
2165 fd_name
= xstrdup (file_data
->file_name
);
2171 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
2172 page_mask
= ~(page_size
- 1);
2175 computed_offset
= offset
& page_mask
;
2176 diff
= offset
- computed_offset
;
2177 computed_len
= len
+ diff
;
2179 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
2180 fd
, computed_offset
);
2181 if (result
== MAP_FAILED
)
2183 fatal_error (input_location
, "Cannot map %s", file_data
->file_name
);
2187 return result
+ diff
;
2189 result
= (char *) xmalloc (len
);
2190 if (lseek (fd
, offset
, SEEK_SET
) != offset
2191 || read (fd
, result
, len
) != (ssize_t
) len
)
2194 fatal_error (input_location
, "Cannot read %s", file_data
->file_name
);
2198 /* Native windows doesn't supports delayed unlink on opened file. So
2199 we close file here again. This produces higher I/O load, but at least
2200 it prevents to have dangling file handles preventing unlink. */
2211 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2212 NAME will be NULL unless the section type is for a function
2216 get_section_data (struct lto_file_decl_data
*file_data
,
2217 enum lto_section_type section_type
,
2221 htab_t section_hash_table
= file_data
->section_hash_table
;
2222 struct lto_section_slot
*f_slot
;
2223 struct lto_section_slot s_slot
;
2224 const char *section_name
= lto_get_section_name (section_type
, name
, file_data
);
2228 s_slot
.name
= section_name
;
2229 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
2232 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
2236 free (CONST_CAST (char *, section_name
));
2241 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2242 starts at OFFSET and has LEN bytes. */
2245 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
2246 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
2247 const char *name ATTRIBUTE_UNUSED
,
2248 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
2251 intptr_t computed_len
;
2252 intptr_t computed_offset
;
2257 computed_offset
= ((intptr_t) offset
) & page_mask
;
2258 diff
= (intptr_t) offset
- computed_offset
;
2259 computed_len
= len
+ diff
;
2261 munmap ((caddr_t
) computed_offset
, computed_len
);
2263 free (CONST_CAST(char *, offset
));
2267 static lto_file
*current_lto_file
;
2269 /* Helper for qsort; compare partitions and return one with smaller size.
2270 We sort from greatest to smallest so parallel build doesn't stale on the
2271 longest compilation being executed too late. */
2274 cmp_partitions_size (const void *a
, const void *b
)
2276 const struct ltrans_partition_def
*pa
2277 = *(struct ltrans_partition_def
*const *)a
;
2278 const struct ltrans_partition_def
*pb
2279 = *(struct ltrans_partition_def
*const *)b
;
2280 return pb
->insns
- pa
->insns
;
2283 /* Helper for qsort; compare partitions and return one with smaller order. */
2286 cmp_partitions_order (const void *a
, const void *b
)
2288 const struct ltrans_partition_def
*pa
2289 = *(struct ltrans_partition_def
*const *)a
;
2290 const struct ltrans_partition_def
*pb
2291 = *(struct ltrans_partition_def
*const *)b
;
2292 int ordera
= -1, orderb
= -1;
2294 if (lto_symtab_encoder_size (pa
->encoder
))
2295 ordera
= lto_symtab_encoder_deref (pa
->encoder
, 0)->order
;
2296 if (lto_symtab_encoder_size (pb
->encoder
))
2297 orderb
= lto_symtab_encoder_deref (pb
->encoder
, 0)->order
;
2298 return orderb
- ordera
;
2301 /* Actually stream out ENCODER into TEMP_FILENAME. */
2304 do_stream_out (char *temp_filename
, lto_symtab_encoder_t encoder
)
2306 lto_file
*file
= lto_obj_file_open (temp_filename
, true);
2308 fatal_error (input_location
, "lto_obj_file_open() failed");
2309 lto_set_current_out_file (file
);
2311 ipa_write_optimization_summaries (encoder
);
2313 lto_set_current_out_file (NULL
);
2314 lto_obj_file_close (file
);
2318 /* Wait for forked process and signal errors. */
2319 #ifdef HAVE_WORKING_FORK
2327 #define WCONTINUED 0
2329 int w
= waitpid (0, &status
, WUNTRACED
| WCONTINUED
);
2331 fatal_error (input_location
, "waitpid failed");
2333 if (WIFEXITED (status
) && WEXITSTATUS (status
))
2334 fatal_error (input_location
, "streaming subprocess failed");
2335 else if (WIFSIGNALED (status
))
2336 fatal_error (input_location
,
2337 "streaming subprocess was killed by signal");
2339 while (!WIFEXITED (status
) && !WIFSIGNALED (status
));
2343 /* Stream out ENCODER into TEMP_FILENAME
2344 Fork if that seems to help. */
2347 stream_out (char *temp_filename
, lto_symtab_encoder_t encoder
,
2348 bool ARG_UNUSED (last
))
2350 #ifdef HAVE_WORKING_FORK
2353 if (lto_parallelism
<= 1)
2355 do_stream_out (temp_filename
, encoder
);
2359 /* Do not run more than LTO_PARALLELISM streamings
2360 FIXME: we ignore limits on jobserver. */
2361 if (lto_parallelism
> 0 && nruns
>= lto_parallelism
)
2366 /* If this is not the last parallel partition, execute new
2367 streaming process. */
2370 pid_t cpid
= fork ();
2374 setproctitle ("lto1-wpa-streaming");
2375 do_stream_out (temp_filename
, encoder
);
2378 /* Fork failed; lets do the job ourseleves. */
2379 else if (cpid
== -1)
2380 do_stream_out (temp_filename
, encoder
);
2384 /* Last partition; stream it and wait for all children to die. */
2388 do_stream_out (temp_filename
, encoder
);
2389 for (i
= 0; i
< nruns
; i
++)
2392 asm_nodes_output
= true;
2394 do_stream_out (temp_filename
, encoder
);
2398 /* Write all output files in WPA mode and the file with the list of
2402 lto_wpa_write_files (void)
2405 ltrans_partition part
;
2406 FILE *ltrans_output_list_stream
;
2407 char *temp_filename
;
2408 vec
<char *>temp_filenames
= vNULL
;
2411 /* Open the LTRANS output list. */
2412 if (!ltrans_output_list
)
2413 fatal_error (input_location
, "no LTRANS output list filename provided");
2415 timevar_push (TV_WHOPR_WPA
);
2417 FOR_EACH_VEC_ELT (ltrans_partitions
, i
, part
)
2418 lto_stats
.num_output_symtab_nodes
+= lto_symtab_encoder_size (part
->encoder
);
2420 timevar_pop (TV_WHOPR_WPA
);
2422 timevar_push (TV_WHOPR_WPA_IO
);
2424 /* Generate a prefix for the LTRANS unit files. */
2425 blen
= strlen (ltrans_output_list
);
2426 temp_filename
= (char *) xmalloc (blen
+ sizeof ("2147483648.o"));
2427 strcpy (temp_filename
, ltrans_output_list
);
2428 if (blen
> sizeof (".out")
2429 && strcmp (temp_filename
+ blen
- sizeof (".out") + 1,
2431 temp_filename
[blen
- sizeof (".out") + 1] = '\0';
2432 blen
= strlen (temp_filename
);
2434 n_sets
= ltrans_partitions
.length ();
2436 /* Sort partitions by size so small ones are compiled last.
2437 FIXME: Even when not reordering we may want to output one list for parallel make
2438 and other for final link command. */
2440 if (!flag_profile_reorder_functions
|| !flag_profile_use
)
2441 ltrans_partitions
.qsort (flag_toplevel_reorder
2442 ? cmp_partitions_size
2443 : cmp_partitions_order
);
2445 for (i
= 0; i
< n_sets
; i
++)
2447 ltrans_partition part
= ltrans_partitions
[i
];
2449 /* Write all the nodes in SET. */
2450 sprintf (temp_filename
+ blen
, "%u.o", i
);
2453 fprintf (stderr
, " %s (%s %i insns)", temp_filename
, part
->name
, part
->insns
);
2454 if (symtab
->dump_file
)
2456 lto_symtab_encoder_iterator lsei
;
2458 fprintf (symtab
->dump_file
, "Writing partition %s to file %s, %i insns\n",
2459 part
->name
, temp_filename
, part
->insns
);
2460 fprintf (symtab
->dump_file
, " Symbols in partition: ");
2461 for (lsei
= lsei_start_in_partition (part
->encoder
); !lsei_end_p (lsei
);
2462 lsei_next_in_partition (&lsei
))
2464 symtab_node
*node
= lsei_node (lsei
);
2465 fprintf (symtab
->dump_file
, "%s ", node
->asm_name ());
2467 fprintf (symtab
->dump_file
, "\n Symbols in boundary: ");
2468 for (lsei
= lsei_start (part
->encoder
); !lsei_end_p (lsei
);
2471 symtab_node
*node
= lsei_node (lsei
);
2472 if (!lto_symtab_encoder_in_partition_p (part
->encoder
, node
))
2474 fprintf (symtab
->dump_file
, "%s ", node
->asm_name ());
2475 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
2477 && lto_symtab_encoder_encode_body_p (part
->encoder
, cnode
))
2478 fprintf (symtab
->dump_file
, "(body included)");
2481 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
2483 && lto_symtab_encoder_encode_initializer_p (part
->encoder
, vnode
))
2484 fprintf (symtab
->dump_file
, "(initializer included)");
2488 fprintf (symtab
->dump_file
, "\n");
2490 gcc_checking_assert (lto_symtab_encoder_size (part
->encoder
) || !i
);
2492 stream_out (temp_filename
, part
->encoder
, i
== n_sets
- 1);
2494 part
->encoder
= NULL
;
2496 temp_filenames
.safe_push (xstrdup (temp_filename
));
2498 ltrans_output_list_stream
= fopen (ltrans_output_list
, "w");
2499 if (ltrans_output_list_stream
== NULL
)
2500 fatal_error (input_location
,
2501 "opening LTRANS output list %s: %m", ltrans_output_list
);
2502 for (i
= 0; i
< n_sets
; i
++)
2504 unsigned int len
= strlen (temp_filenames
[i
]);
2505 if (fwrite (temp_filenames
[i
], 1, len
, ltrans_output_list_stream
) < len
2506 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
2507 fatal_error (input_location
, "writing to LTRANS output list %s: %m",
2508 ltrans_output_list
);
2509 free (temp_filenames
[i
]);
2511 temp_filenames
.release();
2513 lto_stats
.num_output_files
+= n_sets
;
2515 /* Close the LTRANS output list. */
2516 if (fclose (ltrans_output_list_stream
))
2517 fatal_error (input_location
,
2518 "closing LTRANS output list %s: %m", ltrans_output_list
);
2520 free_ltrans_partitions();
2521 free (temp_filename
);
2523 timevar_pop (TV_WHOPR_WPA_IO
);
2527 /* If TT is a variable or function decl replace it with its
2528 prevailing variant. */
2529 #define LTO_SET_PREVAIL(tt) \
2531 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2532 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2534 tt = lto_symtab_prevailing_decl (tt); \
2539 /* Ensure that TT isn't a replacable var of function decl. */
2540 #define LTO_NO_PREVAIL(tt) \
2541 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2543 /* Given a tree T replace all fields referring to variables or functions
2544 with their prevailing variant. */
2546 lto_fixup_prevailing_decls (tree t
)
2548 enum tree_code code
= TREE_CODE (t
);
2551 gcc_checking_assert (code
!= TREE_BINFO
);
2552 LTO_NO_PREVAIL (TREE_TYPE (t
));
2553 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
2554 LTO_NO_PREVAIL (TREE_CHAIN (t
));
2557 LTO_NO_PREVAIL (DECL_NAME (t
));
2558 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
2559 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
2561 LTO_SET_PREVAIL (DECL_SIZE (t
));
2562 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
2563 LTO_SET_PREVAIL (DECL_INITIAL (t
));
2564 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
2565 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
2567 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
2569 LTO_NO_PREVAIL (t
->decl_with_vis
.assembler_name
);
2571 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
2573 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
2575 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
2577 LTO_NO_PREVAIL (DECL_ARGUMENTS (t
));
2578 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
2579 LTO_NO_PREVAIL (DECL_VINDEX (t
));
2581 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
2583 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t
));
2584 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
2585 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
2586 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
2587 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
2590 else if (TYPE_P (t
))
2592 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
2593 LTO_SET_PREVAIL (TYPE_SIZE (t
));
2594 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
2595 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
2596 LTO_NO_PREVAIL (TYPE_NAME (t
));
2598 LTO_SET_PREVAIL (TYPE_MINVAL (t
));
2599 LTO_SET_PREVAIL (TYPE_MAXVAL (t
));
2600 LTO_NO_PREVAIL (t
->type_non_common
.binfo
);
2602 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
2604 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
2605 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
2606 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
2608 else if (EXPR_P (t
))
2611 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
2612 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
2614 else if (TREE_CODE (t
) == CONSTRUCTOR
)
2618 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
2619 LTO_SET_PREVAIL (val
);
2626 LTO_SET_PREVAIL (TREE_VALUE (t
));
2627 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
2628 LTO_NO_PREVAIL (TREE_PURPOSE (t
));
2634 /* If we fixed nothing, then we missed something seen by
2636 gcc_checking_assert (fixed
);
2638 #undef LTO_SET_PREVAIL
2639 #undef LTO_NO_PREVAIL
2641 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2642 replaces var and function decls with the corresponding prevailing def. */
2645 lto_fixup_state (struct lto_in_decl_state
*state
)
2649 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2650 we still need to walk from all DECLs to find the reachable
2651 FUNCTION_DECLs and VAR_DECLs. */
2652 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
2654 vec
<tree
, va_gc
> *trees
= state
->streams
[si
];
2655 for (i
= 0; i
< vec_safe_length (trees
); i
++)
2657 tree t
= (*trees
)[i
];
2658 #ifdef ENABLE_CHECKING
2662 if (VAR_OR_FUNCTION_DECL_P (t
)
2663 && (TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
2664 (*trees
)[i
] = lto_symtab_prevailing_decl (t
);
2669 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2673 lto_fixup_decls (struct lto_file_decl_data
**files
)
2679 FOR_EACH_VEC_ELT ((*tree_with_vars
), i
, t
)
2680 lto_fixup_prevailing_decls (t
);
2682 for (i
= 0; files
[i
]; i
++)
2684 struct lto_file_decl_data
*file
= files
[i
];
2685 struct lto_in_decl_state
*state
= file
->global_decl_state
;
2686 lto_fixup_state (state
);
2688 hash_table
<decl_state_hasher
>::iterator iter
;
2689 lto_in_decl_state
*elt
;
2690 FOR_EACH_HASH_TABLE_ELEMENT (*file
->function_decl_states
, elt
,
2691 lto_in_decl_state
*, iter
)
2692 lto_fixup_state (elt
);
2696 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
2698 /* Turn file datas for sub files into a single array, so that they look
2699 like separate files for further passes. */
2702 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
, int last_file_ix
)
2704 struct lto_file_decl_data
*n
, *next
;
2707 lto_stats
.num_input_files
= count
;
2709 = ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (count
+ 1);
2710 /* Set the hooks so that all of the ipa passes can read in their data. */
2711 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2712 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
2714 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
2716 all_file_decl_data
[k
++] = n
;
2721 all_file_decl_data
[k
] = NULL
;
2722 gcc_assert (k
== count
);
2725 /* Input file data before flattening (i.e. splitting them to subfiles to support
2726 incremental linking. */
2727 static int real_file_count
;
2728 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
2730 static void print_lto_report_1 (void);
2732 /* Read all the symbols from the input files FNAMES. NFILES is the
2733 number of files requested in the command line. Instantiate a
2734 global call graph by aggregating all the sub-graphs found in each
2738 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
2740 unsigned int i
, last_file_ix
;
2743 struct lto_file_decl_data
**decl_data
;
2746 symtab
->initialize ();
2748 timevar_push (TV_IPA_LTO_DECL_IN
);
2750 #ifdef ACCEL_COMPILER
2751 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2752 lto_stream_offload_p
= true;
2756 = decl_data
= ggc_cleared_vec_alloc
<lto_file_decl_data_ptr
> (nfiles
+ 1);
2757 real_file_count
= nfiles
;
2759 /* Read the resolution file. */
2761 if (resolution_file_name
)
2764 unsigned num_objects
;
2766 resolution
= fopen (resolution_file_name
, "r");
2767 if (resolution
== NULL
)
2768 fatal_error (input_location
,
2769 "could not open symbol resolution file: %m");
2771 t
= fscanf (resolution
, "%u", &num_objects
);
2772 gcc_assert (t
== 1);
2774 /* True, since the plugin splits the archives. */
2775 gcc_assert (num_objects
== nfiles
);
2777 symtab
->state
= LTO_STREAMING
;
2779 canonical_type_hash_cache
= new hash_map
<const_tree
, hashval_t
> (251);
2780 gimple_canonical_types
= htab_create (16381, gimple_canonical_type_hash
,
2781 gimple_canonical_type_eq
, NULL
);
2782 gcc_obstack_init (&tree_scc_hash_obstack
);
2783 tree_scc_hash
= new hash_table
<tree_scc_hasher
> (4096);
2785 /* Register the common node types with the canonical type machinery so
2786 we properly share alias-sets across languages and TUs. Do not
2787 expose the common nodes as type merge target - those that should be
2788 are already exposed so by pre-loading the LTO streamer caches.
2789 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2790 for (i
= 0; i
< itk_none
; ++i
)
2791 lto_register_canonical_types (integer_types
[i
], true);
2792 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2793 lto_register_canonical_types (sizetype_tab
[i
], true);
2794 for (i
= 0; i
< TI_MAX
; ++i
)
2795 lto_register_canonical_types (global_trees
[i
], true);
2796 for (i
= 0; i
< itk_none
; ++i
)
2797 lto_register_canonical_types (integer_types
[i
], false);
2798 for (i
= 0; i
< stk_type_kind_last
; ++i
)
2799 lto_register_canonical_types (sizetype_tab
[i
], false);
2800 for (i
= 0; i
< TI_MAX
; ++i
)
2801 lto_register_canonical_types (global_trees
[i
], false);
2804 fprintf (stderr
, "Reading object files:");
2806 /* Read all of the object files specified on the command line. */
2807 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
2809 struct lto_file_decl_data
*file_data
= NULL
;
2812 fprintf (stderr
, " %s", fnames
[i
]);
2816 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
2817 if (!current_lto_file
)
2820 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
2823 lto_obj_file_close (current_lto_file
);
2824 free (current_lto_file
);
2825 current_lto_file
= NULL
;
2829 decl_data
[last_file_ix
++] = file_data
;
2831 lto_obj_file_close (current_lto_file
);
2832 free (current_lto_file
);
2833 current_lto_file
= NULL
;
2836 lto_flatten_files (decl_data
, count
, last_file_ix
);
2837 lto_stats
.num_input_files
= count
;
2838 ggc_free(decl_data
);
2839 real_file_decl_data
= NULL
;
2841 if (resolution_file_name
)
2842 fclose (resolution
);
2844 /* Show the LTO report before launching LTRANS. */
2845 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
2846 print_lto_report_1 ();
2848 /* Free gimple type merging datastructures. */
2849 delete tree_scc_hash
;
2850 tree_scc_hash
= NULL
;
2851 obstack_free (&tree_scc_hash_obstack
, NULL
);
2852 htab_delete (gimple_canonical_types
);
2853 gimple_canonical_types
= NULL
;
2854 delete canonical_type_hash_cache
;
2855 canonical_type_hash_cache
= NULL
;
2857 /* At this stage we know that majority of GGC memory is reachable.
2858 Growing the limits prevents unnecesary invocation of GGC. */
2862 /* Set the hooks so that all of the ipa passes can read in their data. */
2863 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2865 timevar_pop (TV_IPA_LTO_DECL_IN
);
2868 fprintf (stderr
, "\nReading the callgraph\n");
2870 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
2871 /* Read the symtab. */
2874 input_offload_tables ();
2876 /* Store resolutions into the symbol table. */
2878 ld_plugin_symbol_resolution_t
*res
;
2879 FOR_EACH_SYMBOL (snode
)
2880 if (snode
->real_symbol_p ()
2881 && snode
->lto_file_data
2882 && snode
->lto_file_data
->resolution_map
2883 && (res
= snode
->lto_file_data
->resolution_map
->get (snode
->decl
)))
2884 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
, "Merging declarations\n");
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
;
2920 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
2921 /* Each pass will set the appropriate timer. */
2924 fprintf (stderr
, "Reading summaries\n");
2926 /* Read the IPA summary data. */
2928 ipa_read_optimization_summaries ();
2930 ipa_read_summaries ();
2932 for (i
= 0; all_file_decl_data
[i
]; i
++)
2934 gcc_assert (all_file_decl_data
[i
]->symtab_node_encoder
);
2935 lto_symtab_encoder_delete (all_file_decl_data
[i
]->symtab_node_encoder
);
2936 all_file_decl_data
[i
]->symtab_node_encoder
= NULL
;
2937 lto_free_function_in_decl_state (all_file_decl_data
[i
]->global_decl_state
);
2938 all_file_decl_data
[i
]->global_decl_state
= NULL
;
2939 all_file_decl_data
[i
]->current_decl_state
= NULL
;
2942 /* Finally merge the cgraph according to the decl merging decisions. */
2943 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
2944 if (symtab
->dump_file
)
2946 fprintf (symtab
->dump_file
, "Before merging:\n");
2947 symtab_node::dump_table (symtab
->dump_file
);
2951 lto_symtab_merge_symbols ();
2952 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2953 we are still having duplicated comdat groups containing local statics.
2954 We could also just remove them while merging. */
2955 symtab
->remove_unreachable_nodes (dump_file
);
2958 symtab
->state
= IPA_SSA
;
2959 /* FIXME: Technically all node removals happening here are useless, because
2960 WPA should not stream them. */
2962 symtab
->remove_unreachable_nodes (dump_file
);
2964 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
2966 /* Indicate that the cgraph is built and ready. */
2967 symtab
->function_flags_ready
= true;
2969 ggc_free (all_file_decl_data
);
2970 all_file_decl_data
= NULL
;
2974 /* Materialize all the bodies for all the nodes in the callgraph. */
2977 materialize_cgraph (void)
2979 struct cgraph_node
*node
;
2980 timevar_id_t lto_timer
;
2984 flag_wpa
? "Materializing decls:" : "Reading function bodies:");
2987 FOR_EACH_FUNCTION (node
)
2989 if (node
->lto_file_data
)
2991 lto_materialize_function (node
);
2992 lto_stats
.num_input_cgraph_nodes
++;
2997 /* Start the appropriate timer depending on the mode that we are
2999 lto_timer
= (flag_wpa
) ? TV_WHOPR_WPA
3000 : (flag_ltrans
) ? TV_WHOPR_LTRANS
3002 timevar_push (lto_timer
);
3004 current_function_decl
= NULL
;
3008 fprintf (stderr
, "\n");
3010 timevar_pop (lto_timer
);
3014 /* Show various memory usage statistics related to LTO. */
3016 print_lto_report_1 (void)
3018 const char *pfx
= (flag_lto
) ? "LTO" : (flag_wpa
) ? "WPA" : "LTRANS";
3019 fprintf (stderr
, "%s statistics\n", pfx
);
3021 fprintf (stderr
, "[%s] read %lu SCCs of average size %f\n",
3022 pfx
, num_sccs_read
, total_scc_size
/ (double)num_sccs_read
);
3023 fprintf (stderr
, "[%s] %lu tree bodies read in total\n", pfx
, total_scc_size
);
3024 if (flag_wpa
&& tree_scc_hash
)
3026 fprintf (stderr
, "[%s] tree SCC table: size %ld, %ld elements, "
3027 "collision ratio: %f\n", pfx
,
3028 (long) tree_scc_hash
->size (),
3029 (long) tree_scc_hash
->elements (),
3030 tree_scc_hash
->collisions ());
3031 hash_table
<tree_scc_hasher
>::iterator hiter
;
3032 tree_scc
*scc
, *max_scc
= NULL
;
3033 unsigned max_length
= 0;
3034 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash
, scc
, x
, hiter
)
3036 unsigned length
= 0;
3038 for (; s
; s
= s
->next
)
3040 if (length
> max_length
)
3042 max_length
= length
;
3046 fprintf (stderr
, "[%s] tree SCC max chain length %u (size %u)\n",
3047 pfx
, max_length
, max_scc
->len
);
3048 fprintf (stderr
, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx
,
3049 num_scc_compares
, num_scc_compare_collisions
,
3050 num_scc_compare_collisions
/ (double) num_scc_compares
);
3051 fprintf (stderr
, "[%s] Merged %lu SCCs\n", pfx
, num_sccs_merged
);
3052 fprintf (stderr
, "[%s] Merged %lu tree bodies\n", pfx
,
3053 total_scc_size_merged
);
3054 fprintf (stderr
, "[%s] Merged %lu types\n", pfx
, num_merged_types
);
3055 fprintf (stderr
, "[%s] %lu types prevailed (%lu associated trees)\n",
3056 pfx
, num_prevailing_types
, num_type_scc_trees
);
3057 fprintf (stderr
, "[%s] GIMPLE canonical type table: size %ld, "
3058 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx
,
3059 (long) htab_size (gimple_canonical_types
),
3060 (long) htab_elements (gimple_canonical_types
),
3061 (long) gimple_canonical_types
->searches
,
3062 (long) gimple_canonical_types
->collisions
,
3063 htab_collisions (gimple_canonical_types
));
3064 fprintf (stderr
, "[%s] GIMPLE canonical type pointer-map: "
3065 "%lu elements, %ld searches\n", pfx
,
3066 num_canonical_type_hash_entries
,
3067 num_canonical_type_hash_queries
);
3070 print_lto_report (pfx
);
3073 /* Perform whole program analysis (WPA) on the callgraph and write out the
3074 optimization plan. */
3077 do_whole_program_analysis (void)
3081 lto_parallelism
= 1;
3083 /* TODO: jobserver communicatoin is not supported, yet. */
3084 if (!strcmp (flag_wpa
, "jobserver"))
3085 lto_parallelism
= -1;
3088 lto_parallelism
= atoi (flag_wpa
);
3089 if (lto_parallelism
<= 0)
3090 lto_parallelism
= 0;
3093 timevar_start (TV_PHASE_OPT_GEN
);
3095 /* Note that since we are in WPA mode, materialize_cgraph will not
3096 actually read in all the function bodies. It only materializes
3097 the decls and cgraph nodes so that analysis can be performed. */
3098 materialize_cgraph ();
3100 /* Reading in the cgraph uses different timers, start timing WPA now. */
3101 timevar_push (TV_WHOPR_WPA
);
3103 if (pre_ipa_mem_report
)
3105 fprintf (stderr
, "Memory consumption before IPA\n");
3106 dump_memory_report (false);
3109 symtab
->function_flags_ready
= true;
3111 if (symtab
->dump_file
)
3112 symtab_node::dump_table (symtab
->dump_file
);
3113 bitmap_obstack_initialize (NULL
);
3114 symtab
->state
= IPA_SSA
;
3116 execute_ipa_pass_list (g
->get_passes ()->all_regular_ipa_passes
);
3118 if (symtab
->dump_file
)
3120 fprintf (symtab
->dump_file
, "Optimized ");
3121 symtab_node::dump_table (symtab
->dump_file
);
3123 #ifdef ENABLE_CHECKING
3124 symtab_node::verify_symtab_nodes ();
3126 bitmap_obstack_release (NULL
);
3128 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3129 timevar_pop (TV_WHOPR_WPA
);
3131 timevar_push (TV_WHOPR_PARTITIONING
);
3132 if (flag_lto_partition
== LTO_PARTITION_1TO1
)
3134 else if (flag_lto_partition
== LTO_PARTITION_MAX
)
3136 else if (flag_lto_partition
== LTO_PARTITION_ONE
)
3137 lto_balanced_map (1);
3138 else if (flag_lto_partition
== LTO_PARTITION_BALANCED
)
3139 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS
));
3143 /* Inline summaries are needed for balanced partitioning. Free them now so
3144 the memory can be used for streamer caches. */
3145 inline_free_summary ();
3147 /* AUX pointers are used by partitioning code to bookkeep number of
3148 partitions symbol is in. This is no longer needed. */
3149 FOR_EACH_SYMBOL (node
)
3152 lto_stats
.num_cgraph_partitions
+= ltrans_partitions
.length ();
3154 /* Find out statics that need to be promoted
3155 to globals with hidden visibility because they are accessed from multiple
3157 lto_promote_cross_file_statics ();
3158 timevar_pop (TV_WHOPR_PARTITIONING
);
3160 timevar_stop (TV_PHASE_OPT_GEN
);
3162 /* Collect a last time - in lto_wpa_write_files we may end up forking
3163 with the idea that this doesn't increase memory usage. So we
3164 absoultely do not want to collect after that. */
3167 timevar_start (TV_PHASE_STREAM_OUT
);
3170 fprintf (stderr
, "\nStreaming out");
3173 lto_wpa_write_files ();
3175 fprintf (stderr
, "\n");
3176 timevar_stop (TV_PHASE_STREAM_OUT
);
3178 if (post_ipa_mem_report
)
3180 fprintf (stderr
, "Memory consumption after IPA\n");
3181 dump_memory_report (false);
3184 /* Show the LTO report before launching LTRANS. */
3185 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
3186 print_lto_report_1 ();
3188 dump_memory_report (true);
3192 static GTY(()) tree lto_eh_personality_decl
;
3194 /* Return the LTO personality function decl. */
3197 lto_eh_personality (void)
3199 if (!lto_eh_personality_decl
)
3201 /* Use the first personality DECL for our personality if we don't
3202 support multiple ones. This ensures that we don't artificially
3203 create the need for them in a single-language program. */
3204 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
3205 lto_eh_personality_decl
= first_personality_decl
;
3207 lto_eh_personality_decl
= lhd_gcc_personality ();
3210 return lto_eh_personality_decl
;
3213 /* Set the process name based on the LTO mode. */
3216 lto_process_name (void)
3219 setproctitle ("lto1-lto");
3221 setproctitle ("lto1-wpa");
3223 setproctitle ("lto1-ltrans");
3227 /* Initialize the LTO front end. */
3232 lto_process_name ();
3233 lto_streamer_hooks_init ();
3235 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
3236 memset (<o_stats
, 0, sizeof (lto_stats
));
3237 bitmap_obstack_initialize (NULL
);
3238 gimple_register_cfg_hooks ();
3239 #ifndef ACCEL_COMPILER
3240 unsigned char *table
3241 = ggc_vec_alloc
<unsigned char> (MAX_MACHINE_MODE
);
3242 for (int m
= 0; m
< MAX_MACHINE_MODE
; m
++)
3244 lto_mode_identity_table
= table
;
3249 /* Main entry point for the GIMPLE front end. This front end has
3250 three main personalities:
3252 - LTO (-flto). All the object files on the command line are
3253 loaded in memory and processed as a single translation unit.
3254 This is the traditional link-time optimization behavior.
3256 - WPA (-fwpa). Only the callgraph and summary information for
3257 files in the command file are loaded. A single callgraph
3258 (without function bodies) is instantiated for the whole set of
3259 files. IPA passes are only allowed to analyze the call graph
3260 and make transformation decisions. The callgraph is
3261 partitioned, each partition is written to a new object file
3262 together with the transformation decisions.
3264 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3265 summary files from running again. Since WPA computed summary
3266 information and decided what transformations to apply, LTRANS
3267 simply applies them. */
3272 /* LTO is called as a front end, even though it is not a front end.
3273 Because it is called as a front end, TV_PHASE_PARSING and
3274 TV_PARSE_GLOBAL are active, and we need to turn them off while
3275 doing LTO. Later we turn them back on so they are active up in
3277 timevar_pop (TV_PARSE_GLOBAL
);
3278 timevar_stop (TV_PHASE_PARSING
);
3280 timevar_start (TV_PHASE_SETUP
);
3282 /* Initialize the LTO front end. */
3285 timevar_stop (TV_PHASE_SETUP
);
3286 timevar_start (TV_PHASE_STREAM_IN
);
3288 /* Read all the symbols and call graph from all the files in the
3290 read_cgraph_and_symbols (num_in_fnames
, in_fnames
);
3292 timevar_stop (TV_PHASE_STREAM_IN
);
3296 /* If WPA is enabled analyze the whole call graph and create an
3297 optimization plan. Otherwise, read in all the function
3298 bodies and continue with optimization. */
3300 do_whole_program_analysis ();
3303 timevar_start (TV_PHASE_OPT_GEN
);
3305 materialize_cgraph ();
3307 lto_promote_statics_nonwpa ();
3309 /* Let the middle end know that we have read and merged all of
3313 timevar_stop (TV_PHASE_OPT_GEN
);
3315 /* FIXME lto, if the processes spawned by WPA fail, we miss
3316 the chance to print WPA's report, so WPA will call
3317 print_lto_report before launching LTRANS. If LTRANS was
3318 launched directly by the driver we would not need to do
3320 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
3321 print_lto_report_1 ();
3325 /* Here we make LTO pretend to be a parser. */
3326 timevar_start (TV_PHASE_PARSING
);
3327 timevar_push (TV_PARSE_GLOBAL
);
3330 #include "gt-lto-lto.h"