1 /* Top-level LTO routines.
2 Copyright (C) 2009-2013 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 "tree-flow.h"
28 #include "diagnostic-core.h"
32 #include "tree-ssa-operands.h"
33 #include "tree-pass.h"
34 #include "langhooks.h"
37 #include "pointer-set.h"
44 #include "lto-streamer.h"
45 #include "tree-streamer.h"
46 #include "splay-tree.h"
47 #include "lto-partition.h"
48 #include "data-streamer.h"
50 #include "pass_manager.h"
52 /* Vector to keep track of external variables we've seen so far. */
53 vec
<tree
, va_gc
> *lto_global_var_decls
;
55 static GTY(()) tree first_personality_decl
;
57 /* Returns a hash code for P. */
60 hash_name (const void *p
)
62 const struct lto_section_slot
*ds
= (const struct lto_section_slot
*) p
;
63 return (hashval_t
) htab_hash_string (ds
->name
);
67 /* Returns nonzero if P1 and P2 are equal. */
70 eq_name (const void *p1
, const void *p2
)
72 const struct lto_section_slot
*s1
=
73 (const struct lto_section_slot
*) p1
;
74 const struct lto_section_slot
*s2
=
75 (const struct lto_section_slot
*) p2
;
77 return strcmp (s1
->name
, s2
->name
) == 0;
80 /* Free lto_section_slot */
83 free_with_string (void *arg
)
85 struct lto_section_slot
*s
= (struct lto_section_slot
*)arg
;
87 free (CONST_CAST (char *, s
->name
));
91 /* Create section hash table */
94 lto_obj_create_section_hash_table (void)
96 return htab_create (37, hash_name
, eq_name
, free_with_string
);
99 /* Delete an allocated integer KEY in the splay tree. */
102 lto_splay_tree_delete_id (splay_tree_key key
)
107 /* Compare splay tree node ids A and B. */
110 lto_splay_tree_compare_ids (splay_tree_key a
, splay_tree_key b
)
112 unsigned HOST_WIDE_INT ai
;
113 unsigned HOST_WIDE_INT bi
;
115 ai
= *(unsigned HOST_WIDE_INT
*) a
;
116 bi
= *(unsigned HOST_WIDE_INT
*) b
;
125 /* Look up splay tree node by ID in splay tree T. */
127 static splay_tree_node
128 lto_splay_tree_lookup (splay_tree t
, unsigned HOST_WIDE_INT id
)
130 return splay_tree_lookup (t
, (splay_tree_key
) &id
);
133 /* Check if KEY has ID. */
136 lto_splay_tree_id_equal_p (splay_tree_key key
, unsigned HOST_WIDE_INT id
)
138 return *(unsigned HOST_WIDE_INT
*) key
== id
;
141 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
142 The ID is allocated separately because we need HOST_WIDE_INTs which may
143 be wider than a splay_tree_key. */
146 lto_splay_tree_insert (splay_tree t
, unsigned HOST_WIDE_INT id
,
147 struct lto_file_decl_data
*file_data
)
149 unsigned HOST_WIDE_INT
*idp
= XCNEW (unsigned HOST_WIDE_INT
);
151 splay_tree_insert (t
, (splay_tree_key
) idp
, (splay_tree_value
) file_data
);
154 /* Create a splay tree. */
157 lto_splay_tree_new (void)
159 return splay_tree_new (lto_splay_tree_compare_ids
,
160 lto_splay_tree_delete_id
,
164 /* Return true when NODE has a clone that is analyzed (i.e. we need
165 to load its body even if the node itself is not needed). */
168 has_analyzed_clone_p (struct cgraph_node
*node
)
170 struct cgraph_node
*orig
= node
;
175 if (node
->symbol
.analyzed
)
179 else if (node
->next_sibling_clone
)
180 node
= node
->next_sibling_clone
;
183 while (node
!= orig
&& !node
->next_sibling_clone
)
184 node
= node
->clone_of
;
186 node
= node
->next_sibling_clone
;
192 /* Read the function body for the function associated with NODE. */
195 lto_materialize_function (struct cgraph_node
*node
)
199 decl
= node
->symbol
.decl
;
200 /* Read in functions with body (analyzed nodes)
201 and also functions that are needed to produce virtual clones. */
202 if ((cgraph_function_with_gimple_body_p (node
) && node
->symbol
.analyzed
)
203 || node
->used_as_abstract_origin
204 || has_analyzed_clone_p (node
))
206 /* Clones don't need to be read. */
209 if (DECL_FUNCTION_PERSONALITY (decl
) && !first_personality_decl
)
210 first_personality_decl
= DECL_FUNCTION_PERSONALITY (decl
);
213 /* Let the middle end know about the function. */
214 rest_of_decl_compilation (decl
, 1, 0);
218 /* Decode the content of memory pointed to by DATA in the in decl
219 state object STATE. DATA_IN points to a data_in structure for
220 decoding. Return the address after the decoded object in the
223 static const uint32_t *
224 lto_read_in_decl_state (struct data_in
*data_in
, const uint32_t *data
,
225 struct lto_in_decl_state
*state
)
232 decl
= streamer_tree_cache_get_tree (data_in
->reader_cache
, ix
);
233 if (TREE_CODE (decl
) != FUNCTION_DECL
)
235 gcc_assert (decl
== void_type_node
);
238 state
->fn_decl
= decl
;
240 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
242 uint32_t size
= *data
++;
243 tree
*decls
= ggc_alloc_vec_tree (size
);
245 for (j
= 0; j
< size
; j
++)
246 decls
[j
] = streamer_tree_cache_get_tree (data_in
->reader_cache
, data
[j
]);
248 state
->streams
[i
].size
= size
;
249 state
->streams
[i
].trees
= decls
;
257 /* Global canonical type table. */
258 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node
)))
259 htab_t gimple_canonical_types
;
260 static GTY((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map
)))
261 htab_t canonical_type_hash_cache
;
263 /* Returning a hash value for gimple type TYPE combined with VAL.
265 The hash value returned is equal for types considered compatible
266 by gimple_canonical_types_compatible_p. */
269 iterative_hash_canonical_type (tree type
, hashval_t val
)
273 struct tree_int_map
*mp
, m
;
276 if ((slot
= htab_find_slot (canonical_type_hash_cache
, &m
, NO_INSERT
)))
277 return iterative_hash_hashval_t (((struct tree_int_map
*) *slot
)->to
, val
);
279 /* Combine a few common features of types so that types are grouped into
280 smaller sets; when searching for existing matching types to merge,
281 only existing types having the same features as the new type will be
283 v
= iterative_hash_hashval_t (TREE_CODE (type
), 0);
284 v
= iterative_hash_hashval_t (TREE_ADDRESSABLE (type
), v
);
285 v
= iterative_hash_hashval_t (TYPE_ALIGN (type
), v
);
286 v
= iterative_hash_hashval_t (TYPE_MODE (type
), v
);
288 /* Incorporate common features of numerical types. */
289 if (INTEGRAL_TYPE_P (type
)
290 || SCALAR_FLOAT_TYPE_P (type
)
291 || FIXED_POINT_TYPE_P (type
)
292 || TREE_CODE (type
) == OFFSET_TYPE
293 || POINTER_TYPE_P (type
))
295 v
= iterative_hash_hashval_t (TYPE_PRECISION (type
), v
);
296 v
= iterative_hash_hashval_t (TYPE_UNSIGNED (type
), v
);
299 if (VECTOR_TYPE_P (type
))
301 v
= iterative_hash_hashval_t (TYPE_VECTOR_SUBPARTS (type
), v
);
302 v
= iterative_hash_hashval_t (TYPE_UNSIGNED (type
), v
);
305 if (TREE_CODE (type
) == COMPLEX_TYPE
)
306 v
= iterative_hash_hashval_t (TYPE_UNSIGNED (type
), v
);
308 /* For pointer and reference types, fold in information about the type
309 pointed to but do not recurse to the pointed-to type. */
310 if (POINTER_TYPE_P (type
))
312 v
= iterative_hash_hashval_t (TYPE_REF_CAN_ALIAS_ALL (type
), v
);
313 v
= iterative_hash_hashval_t (TYPE_ADDR_SPACE (TREE_TYPE (type
)), v
);
314 v
= iterative_hash_hashval_t (TYPE_RESTRICT (type
), v
);
315 v
= iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type
)), v
);
318 /* For integer types hash only the string flag. */
319 if (TREE_CODE (type
) == INTEGER_TYPE
)
320 v
= iterative_hash_hashval_t (TYPE_STRING_FLAG (type
), v
);
322 /* For array types hash the domain bounds and the string flag. */
323 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
325 v
= iterative_hash_hashval_t (TYPE_STRING_FLAG (type
), v
);
326 /* OMP lowering can introduce error_mark_node in place of
327 random local decls in types. */
328 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type
)) != error_mark_node
)
329 v
= iterative_hash_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type
)), v
);
330 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) != error_mark_node
)
331 v
= iterative_hash_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)), v
);
334 /* Recurse for aggregates with a single element type. */
335 if (TREE_CODE (type
) == ARRAY_TYPE
336 || TREE_CODE (type
) == COMPLEX_TYPE
337 || TREE_CODE (type
) == VECTOR_TYPE
)
338 v
= iterative_hash_canonical_type (TREE_TYPE (type
), v
);
340 /* Incorporate function return and argument types. */
341 if (TREE_CODE (type
) == FUNCTION_TYPE
|| TREE_CODE (type
) == METHOD_TYPE
)
346 /* For method types also incorporate their parent class. */
347 if (TREE_CODE (type
) == METHOD_TYPE
)
348 v
= iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type
), v
);
350 v
= iterative_hash_canonical_type (TREE_TYPE (type
), v
);
352 for (p
= TYPE_ARG_TYPES (type
), na
= 0; p
; p
= TREE_CHAIN (p
))
354 v
= iterative_hash_canonical_type (TREE_VALUE (p
), v
);
358 v
= iterative_hash_hashval_t (na
, v
);
361 if (RECORD_OR_UNION_TYPE_P (type
))
366 for (f
= TYPE_FIELDS (type
), nf
= 0; f
; f
= TREE_CHAIN (f
))
367 if (TREE_CODE (f
) == FIELD_DECL
)
369 v
= iterative_hash_canonical_type (TREE_TYPE (f
), v
);
373 v
= iterative_hash_hashval_t (nf
, v
);
376 /* Cache the just computed hash value. */
377 mp
= ggc_alloc_cleared_tree_int_map ();
378 mp
->base
.from
= type
;
380 /* As we recurse the hashtable may expand between looking up the
381 cached value (and not finding one) and here, so we have to
382 re-lookup the slot. */
383 slot
= htab_find_slot (canonical_type_hash_cache
, &m
, INSERT
);
386 return iterative_hash_hashval_t (v
, val
);
390 gimple_canonical_type_hash (const void *p
)
392 return iterative_hash_canonical_type (CONST_CAST_TREE ((const_tree
) p
), 0);
396 /* The TYPE_CANONICAL merging machinery. It should closely resemble
397 the middle-end types_compatible_p function. It needs to avoid
398 claiming types are different for types that should be treated
399 the same with respect to TBAA. Canonical types are also used
400 for IL consistency checks via the useless_type_conversion_p
401 predicate which does not handle all type kinds itself but falls
402 back to pointer-comparison of TYPE_CANONICAL for aggregates
405 /* Return true iff T1 and T2 are structurally identical for what
406 TBAA is concerned. */
409 gimple_canonical_types_compatible_p (tree t1
, tree t2
)
411 /* Before starting to set up the SCC machinery handle simple cases. */
413 /* Check first for the obvious case of pointer identity. */
417 /* Check that we have two types to compare. */
418 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
421 /* If the types have been previously registered and found equal
423 if (TYPE_CANONICAL (t1
)
424 && TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
427 /* Can't be the same type if the types don't have the same code. */
428 if (TREE_CODE (t1
) != TREE_CODE (t2
))
431 if (TREE_ADDRESSABLE (t1
) != TREE_ADDRESSABLE (t2
))
434 /* Qualifiers do not matter for canonical type comparison purposes. */
436 /* Void types and nullptr types are always the same. */
437 if (TREE_CODE (t1
) == VOID_TYPE
438 || TREE_CODE (t1
) == NULLPTR_TYPE
)
441 /* Can't be the same type if they have different alignment, or mode. */
442 if (TYPE_ALIGN (t1
) != TYPE_ALIGN (t2
)
443 || TYPE_MODE (t1
) != TYPE_MODE (t2
))
446 /* Non-aggregate types can be handled cheaply. */
447 if (INTEGRAL_TYPE_P (t1
)
448 || SCALAR_FLOAT_TYPE_P (t1
)
449 || FIXED_POINT_TYPE_P (t1
)
450 || TREE_CODE (t1
) == VECTOR_TYPE
451 || TREE_CODE (t1
) == COMPLEX_TYPE
452 || TREE_CODE (t1
) == OFFSET_TYPE
453 || POINTER_TYPE_P (t1
))
455 /* Can't be the same type if they have different sign or precision. */
456 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
)
457 || TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
460 if (TREE_CODE (t1
) == INTEGER_TYPE
461 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
464 /* For canonical type comparisons we do not want to build SCCs
465 so we cannot compare pointed-to types. But we can, for now,
466 require the same pointed-to type kind and match what
467 useless_type_conversion_p would do. */
468 if (POINTER_TYPE_P (t1
))
470 /* If the two pointers have different ref-all attributes,
471 they can't be the same type. */
472 if (TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
475 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
476 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
479 if (TYPE_RESTRICT (t1
) != TYPE_RESTRICT (t2
))
482 if (TREE_CODE (TREE_TYPE (t1
)) != TREE_CODE (TREE_TYPE (t2
)))
486 /* Tail-recurse to components. */
487 if (TREE_CODE (t1
) == VECTOR_TYPE
488 || TREE_CODE (t1
) == COMPLEX_TYPE
)
489 return gimple_canonical_types_compatible_p (TREE_TYPE (t1
),
495 /* Do type-specific comparisons. */
496 switch (TREE_CODE (t1
))
499 /* Array types are the same if the element types are the same and
500 the number of elements are the same. */
501 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
502 || TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
)
503 || TYPE_NONALIASED_COMPONENT (t1
) != TYPE_NONALIASED_COMPONENT (t2
))
507 tree i1
= TYPE_DOMAIN (t1
);
508 tree i2
= TYPE_DOMAIN (t2
);
510 /* For an incomplete external array, the type domain can be
511 NULL_TREE. Check this condition also. */
512 if (i1
== NULL_TREE
&& i2
== NULL_TREE
)
514 else if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
518 tree min1
= TYPE_MIN_VALUE (i1
);
519 tree min2
= TYPE_MIN_VALUE (i2
);
520 tree max1
= TYPE_MAX_VALUE (i1
);
521 tree max2
= TYPE_MAX_VALUE (i2
);
523 /* The minimum/maximum values have to be the same. */
526 && ((TREE_CODE (min1
) == PLACEHOLDER_EXPR
527 && TREE_CODE (min2
) == PLACEHOLDER_EXPR
)
528 || operand_equal_p (min1
, min2
, 0))))
531 && ((TREE_CODE (max1
) == PLACEHOLDER_EXPR
532 && TREE_CODE (max2
) == PLACEHOLDER_EXPR
)
533 || operand_equal_p (max1
, max2
, 0)))))
542 /* Function types are the same if the return type and arguments types
544 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
547 if (!comp_type_attributes (t1
, t2
))
550 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
))
556 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
558 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
560 if (!gimple_canonical_types_compatible_p
561 (TREE_VALUE (parms1
), TREE_VALUE (parms2
)))
565 if (parms1
|| parms2
)
573 case QUAL_UNION_TYPE
:
577 /* For aggregate types, all the fields must be the same. */
578 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
580 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
582 /* Skip non-fields. */
583 while (f1
&& TREE_CODE (f1
) != FIELD_DECL
)
584 f1
= TREE_CHAIN (f1
);
585 while (f2
&& TREE_CODE (f2
) != FIELD_DECL
)
586 f2
= TREE_CHAIN (f2
);
589 /* The fields must have the same name, offset and type. */
590 if (DECL_NONADDRESSABLE_P (f1
) != DECL_NONADDRESSABLE_P (f2
)
591 || !gimple_compare_field_offset (f1
, f2
)
592 || !gimple_canonical_types_compatible_p
593 (TREE_TYPE (f1
), TREE_TYPE (f2
)))
597 /* If one aggregate has more fields than the other, they
611 /* Returns nonzero if P1 and P2 are equal. */
614 gimple_canonical_type_eq (const void *p1
, const void *p2
)
616 const_tree t1
= (const_tree
) p1
;
617 const_tree t2
= (const_tree
) p2
;
618 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1
),
619 CONST_CAST_TREE (t2
));
622 /* Register type T in the global type table gimple_types.
623 If another type T', compatible with T, already existed in
624 gimple_types then return T', otherwise return T. This is used by
625 LTO to merge identical types read from different TUs.
627 ??? This merging does not exactly match how the tree.c middle-end
628 functions will assign TYPE_CANONICAL when new types are created
629 during optimization (which at least happens for pointer and array
633 gimple_register_canonical_type (tree t
)
637 gcc_assert (TYPE_P (t
));
639 if (TYPE_CANONICAL (t
))
640 return TYPE_CANONICAL (t
);
642 slot
= htab_find_slot (gimple_canonical_types
, t
, INSERT
);
644 && *(tree
*)slot
!= t
)
646 tree new_type
= (tree
) *((tree
*) slot
);
648 TYPE_CANONICAL (t
) = new_type
;
653 TYPE_CANONICAL (t
) = t
;
660 /* Re-compute TYPE_CANONICAL for NODE and related types. */
663 lto_register_canonical_types (tree node
)
669 TYPE_CANONICAL (node
) = NULL_TREE
;
670 TYPE_CANONICAL (node
) = gimple_register_canonical_type (node
);
672 if (POINTER_TYPE_P (node
)
673 || TREE_CODE (node
) == COMPLEX_TYPE
674 || TREE_CODE (node
) == ARRAY_TYPE
)
675 lto_register_canonical_types (TREE_TYPE (node
));
679 /* Remember trees that contains references to declarations. */
680 static GTY(()) vec
<tree
, va_gc
> *tree_with_vars
;
682 #define CHECK_VAR(tt) \
685 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
686 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
690 #define CHECK_NO_VAR(tt) \
691 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
693 /* Check presence of pointers to decls in fields of a tree_typed T. */
696 mentions_vars_p_typed (tree t
)
698 CHECK_NO_VAR (TREE_TYPE (t
));
702 /* Check presence of pointers to decls in fields of a tree_common T. */
705 mentions_vars_p_common (tree t
)
707 if (mentions_vars_p_typed (t
))
709 CHECK_NO_VAR (TREE_CHAIN (t
));
713 /* Check presence of pointers to decls in fields of a decl_minimal T. */
716 mentions_vars_p_decl_minimal (tree t
)
718 if (mentions_vars_p_common (t
))
720 CHECK_NO_VAR (DECL_NAME (t
));
721 CHECK_VAR (DECL_CONTEXT (t
));
725 /* Check presence of pointers to decls in fields of a decl_common T. */
728 mentions_vars_p_decl_common (tree t
)
730 if (mentions_vars_p_decl_minimal (t
))
732 CHECK_VAR (DECL_SIZE (t
));
733 CHECK_VAR (DECL_SIZE_UNIT (t
));
734 CHECK_VAR (DECL_INITIAL (t
));
735 CHECK_NO_VAR (DECL_ATTRIBUTES (t
));
736 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t
));
740 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
743 mentions_vars_p_decl_with_vis (tree t
)
745 if (mentions_vars_p_decl_common (t
))
748 /* Accessor macro has side-effects, use field-name here. */
749 CHECK_NO_VAR (t
->decl_with_vis
.assembler_name
);
750 CHECK_NO_VAR (DECL_SECTION_NAME (t
));
754 /* Check presence of pointers to decls in fields of a decl_non_common T. */
757 mentions_vars_p_decl_non_common (tree t
)
759 if (mentions_vars_p_decl_with_vis (t
))
761 CHECK_NO_VAR (DECL_ARGUMENT_FLD (t
));
762 CHECK_NO_VAR (DECL_RESULT_FLD (t
));
763 CHECK_NO_VAR (DECL_VINDEX (t
));
767 /* Check presence of pointers to decls in fields of a decl_non_common T. */
770 mentions_vars_p_function (tree t
)
772 if (mentions_vars_p_decl_non_common (t
))
774 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t
));
778 /* Check presence of pointers to decls in fields of a field_decl T. */
781 mentions_vars_p_field_decl (tree t
)
783 if (mentions_vars_p_decl_common (t
))
785 CHECK_VAR (DECL_FIELD_OFFSET (t
));
786 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t
));
787 CHECK_NO_VAR (DECL_QUALIFIER (t
));
788 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t
));
789 CHECK_NO_VAR (DECL_FCONTEXT (t
));
793 /* Check presence of pointers to decls in fields of a type T. */
796 mentions_vars_p_type (tree t
)
798 if (mentions_vars_p_common (t
))
800 CHECK_NO_VAR (TYPE_CACHED_VALUES (t
));
801 CHECK_VAR (TYPE_SIZE (t
));
802 CHECK_VAR (TYPE_SIZE_UNIT (t
));
803 CHECK_NO_VAR (TYPE_ATTRIBUTES (t
));
804 CHECK_NO_VAR (TYPE_NAME (t
));
806 CHECK_VAR (TYPE_MINVAL (t
));
807 CHECK_VAR (TYPE_MAXVAL (t
));
809 /* Accessor is for derived node types only. */
810 CHECK_NO_VAR (t
->type_non_common
.binfo
);
812 CHECK_VAR (TYPE_CONTEXT (t
));
813 CHECK_NO_VAR (TYPE_CANONICAL (t
));
814 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t
));
815 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t
));
819 /* Check presence of pointers to decls in fields of a BINFO T. */
822 mentions_vars_p_binfo (tree t
)
824 unsigned HOST_WIDE_INT i
, n
;
826 if (mentions_vars_p_common (t
))
828 CHECK_VAR (BINFO_VTABLE (t
));
829 CHECK_NO_VAR (BINFO_OFFSET (t
));
830 CHECK_NO_VAR (BINFO_VIRTUALS (t
));
831 CHECK_NO_VAR (BINFO_VPTR_FIELD (t
));
832 n
= vec_safe_length (BINFO_BASE_ACCESSES (t
));
833 for (i
= 0; i
< n
; i
++)
834 CHECK_NO_VAR (BINFO_BASE_ACCESS (t
, i
));
835 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
836 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
837 n
= BINFO_N_BASE_BINFOS (t
);
838 for (i
= 0; i
< n
; i
++)
839 CHECK_NO_VAR (BINFO_BASE_BINFO (t
, i
));
843 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
846 mentions_vars_p_constructor (tree t
)
848 unsigned HOST_WIDE_INT idx
;
851 if (mentions_vars_p_typed (t
))
854 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
856 CHECK_NO_VAR (ce
->index
);
857 CHECK_VAR (ce
->value
);
862 /* Check presence of pointers to decls in fields of an expression tree T. */
865 mentions_vars_p_expr (tree t
)
868 if (mentions_vars_p_typed (t
))
870 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
871 CHECK_VAR (TREE_OPERAND (t
, i
));
875 /* Check presence of pointers to decls that needs later fixup in T. */
878 mentions_vars_p (tree t
)
880 switch (TREE_CODE (t
))
882 case IDENTIFIER_NODE
:
886 CHECK_VAR (TREE_VALUE (t
));
887 CHECK_VAR (TREE_PURPOSE (t
));
888 CHECK_NO_VAR (TREE_CHAIN (t
));
892 return mentions_vars_p_field_decl (t
);
901 return mentions_vars_p_decl_common (t
);
905 return mentions_vars_p_decl_with_vis (t
);
909 return mentions_vars_p_decl_non_common (t
);
913 return mentions_vars_p_function (t
);
917 return mentions_vars_p_binfo (t
);
920 case PLACEHOLDER_EXPR
:
921 return mentions_vars_p_common (t
);
925 case TRANSLATION_UNIT_DECL
:
926 case OPTIMIZATION_NODE
:
927 case TARGET_OPTION_NODE
:
931 return mentions_vars_p_constructor (t
);
937 if (mentions_vars_p_type (t
))
942 if (mentions_vars_p_expr (t
))
945 else if (CONSTANT_CLASS_P (t
))
946 CHECK_NO_VAR (TREE_TYPE (t
));
954 /* Return the resolution for the decl with index INDEX from DATA_IN. */
956 static enum ld_plugin_symbol_resolution
957 get_resolution (struct data_in
*data_in
, unsigned index
)
959 if (data_in
->globals_resolution
.exists ())
961 ld_plugin_symbol_resolution_t ret
;
962 /* We can have references to not emitted functions in
963 DECL_FUNCTION_PERSONALITY at least. So we can and have
964 to indeed return LDPR_UNKNOWN in some cases. */
965 if (data_in
->globals_resolution
.length () <= index
)
967 ret
= data_in
->globals_resolution
[index
];
971 /* Delay resolution finding until decl merging. */
975 /* We need to record resolutions until symbol table is read. */
977 register_resolution (struct lto_file_decl_data
*file_data
, tree decl
,
978 enum ld_plugin_symbol_resolution resolution
)
980 if (resolution
== LDPR_UNKNOWN
)
982 if (!file_data
->resolution_map
)
983 file_data
->resolution_map
= pointer_map_create ();
984 *pointer_map_insert (file_data
->resolution_map
, decl
) = (void *)(size_t)resolution
;
987 /* Register DECL with the global symbol table and change its
988 name if necessary to avoid name clashes for static globals across
992 lto_register_var_decl_in_symtab (struct data_in
*data_in
, tree decl
,
997 /* Variable has file scope, not local. */
998 if (!TREE_PUBLIC (decl
)
999 && !((context
= decl_function_context (decl
))
1000 && auto_var_in_fn_p (decl
, context
)))
1001 rest_of_decl_compilation (decl
, 1, 0);
1003 /* If this variable has already been declared, queue the
1004 declaration for merging. */
1005 if (TREE_PUBLIC (decl
))
1006 register_resolution (data_in
->file_data
,
1007 decl
, get_resolution (data_in
, ix
));
1011 /* Register DECL with the global symbol table and change its
1012 name if necessary to avoid name clashes for static globals across
1013 different files. DATA_IN contains descriptors and tables for the
1017 lto_register_function_decl_in_symtab (struct data_in
*data_in
, tree decl
,
1020 /* If this variable has already been declared, queue the
1021 declaration for merging. */
1022 if (TREE_PUBLIC (decl
) && !DECL_ABSTRACT (decl
))
1023 register_resolution (data_in
->file_data
,
1024 decl
, get_resolution (data_in
, ix
));
1028 /* For the type T re-materialize it in the type variant list and
1029 the pointer/reference-to chains. */
1032 lto_fixup_prevailing_type (tree t
)
1034 /* The following re-creates proper variant lists while fixing up
1035 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1036 variant list state before fixup is broken. */
1038 /* If we are not our own variant leader link us into our new leaders
1040 if (TYPE_MAIN_VARIANT (t
) != t
)
1042 tree mv
= TYPE_MAIN_VARIANT (t
);
1043 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
1044 TYPE_NEXT_VARIANT (mv
) = t
;
1047 /* The following reconstructs the pointer chains
1048 of the new pointed-to type if we are a main variant. We do
1049 not stream those so they are broken before fixup. */
1050 if (TREE_CODE (t
) == POINTER_TYPE
1051 && TYPE_MAIN_VARIANT (t
) == t
)
1053 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
1054 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
1056 else if (TREE_CODE (t
) == REFERENCE_TYPE
1057 && TYPE_MAIN_VARIANT (t
) == t
)
1059 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
1060 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
1065 /* We keep prevailing tree SCCs in a hashtable with manual collision
1066 handling (in case all hashes compare the same) and keep the colliding
1067 entries in the tree_scc->next chain. */
1072 /* Hash of the whole SCC. */
1074 /* Number of trees in the SCC. */
1076 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1077 which share the same individual tree hash). */
1079 /* The members of the SCC.
1080 We only need to remember the first entry node candidate for prevailing
1081 SCCs (but of course have access to all entries for SCCs we are
1083 ??? For prevailing SCCs we really only need hash and the first
1084 entry candidate, but that's too awkward to implement. */
1088 struct tree_scc_hasher
: typed_noop_remove
<tree_scc
>
1090 typedef tree_scc value_type
;
1091 typedef tree_scc compare_type
;
1092 static inline hashval_t
hash (const value_type
*);
1093 static inline bool equal (const value_type
*, const compare_type
*);
1097 tree_scc_hasher::hash (const value_type
*scc
)
1103 tree_scc_hasher::equal (const value_type
*scc1
, const compare_type
*scc2
)
1105 if (scc1
->hash
!= scc2
->hash
1106 || scc1
->len
!= scc2
->len
1107 || scc1
->entry_len
!= scc2
->entry_len
)
1112 static hash_table
<tree_scc_hasher
> tree_scc_hash
;
1113 static struct obstack tree_scc_hash_obstack
;
1115 static unsigned long num_merged_types
;
1116 static unsigned long num_prevailing_types
;
1117 static unsigned long num_type_scc_trees
;
1118 static unsigned long total_scc_size
;
1119 static unsigned long num_sccs_read
;
1120 static unsigned long total_scc_size_merged
;
1121 static unsigned long num_sccs_merged
;
1122 static unsigned long num_scc_compares
;
1123 static unsigned long num_scc_compare_collisions
;
1126 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1127 recursing through in-SCC tree edges. Returns true if the SCCs entered
1128 through T1 and T2 are equal and fills in *MAP with the pairs of
1129 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1132 compare_tree_sccs_1 (tree t1
, tree t2
, tree
**map
)
1134 enum tree_code code
;
1136 /* Mark already visited nodes. */
1137 TREE_ASM_WRITTEN (t2
) = 1;
1139 /* Push the pair onto map. */
1144 /* Compare value-fields. */
1145 #define compare_values(X) \
1147 if (X(t1) != X(t2)) \
1151 compare_values (TREE_CODE
);
1152 code
= TREE_CODE (t1
);
1156 compare_values (TREE_SIDE_EFFECTS
);
1157 compare_values (TREE_CONSTANT
);
1158 compare_values (TREE_READONLY
);
1159 compare_values (TREE_PUBLIC
);
1161 compare_values (TREE_ADDRESSABLE
);
1162 compare_values (TREE_THIS_VOLATILE
);
1164 compare_values (DECL_UNSIGNED
);
1165 else if (TYPE_P (t1
))
1166 compare_values (TYPE_UNSIGNED
);
1168 compare_values (TYPE_ARTIFICIAL
);
1170 compare_values (TREE_NO_WARNING
);
1171 compare_values (TREE_NOTHROW
);
1172 compare_values (TREE_STATIC
);
1173 if (code
!= TREE_BINFO
)
1174 compare_values (TREE_PRIVATE
);
1175 compare_values (TREE_PROTECTED
);
1176 compare_values (TREE_DEPRECATED
);
1179 compare_values (TYPE_SATURATING
);
1180 compare_values (TYPE_ADDR_SPACE
);
1182 else if (code
== SSA_NAME
)
1183 compare_values (SSA_NAME_IS_DEFAULT_DEF
);
1185 if (CODE_CONTAINS_STRUCT (code
, TS_INT_CST
))
1187 compare_values (TREE_INT_CST_LOW
);
1188 compare_values (TREE_INT_CST_HIGH
);
1191 if (CODE_CONTAINS_STRUCT (code
, TS_REAL_CST
))
1193 /* ??? No suitable compare routine available. */
1194 REAL_VALUE_TYPE r1
= TREE_REAL_CST (t1
);
1195 REAL_VALUE_TYPE r2
= TREE_REAL_CST (t2
);
1197 || r1
.decimal
!= r2
.decimal
1198 || r1
.sign
!= r2
.sign
1199 || r1
.signalling
!= r2
.signalling
1200 || r1
.canonical
!= r2
.canonical
1201 || r1
.uexp
!= r2
.uexp
)
1203 for (unsigned i
= 0; i
< SIGSZ
; ++i
)
1204 if (r1
.sig
[i
] != r2
.sig
[i
])
1208 if (CODE_CONTAINS_STRUCT (code
, TS_FIXED_CST
))
1209 if (!fixed_compare (EQ_EXPR
,
1210 TREE_FIXED_CST_PTR (t1
), TREE_FIXED_CST_PTR (t2
)))
1214 /* We don't want to compare locations, so there is nothing do compare
1215 for TS_DECL_MINIMAL. */
1217 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1219 compare_values (DECL_MODE
);
1220 compare_values (DECL_NONLOCAL
);
1221 compare_values (DECL_VIRTUAL_P
);
1222 compare_values (DECL_IGNORED_P
);
1223 compare_values (DECL_ABSTRACT
);
1224 compare_values (DECL_ARTIFICIAL
);
1225 compare_values (DECL_USER_ALIGN
);
1226 compare_values (DECL_PRESERVE_P
);
1227 compare_values (DECL_EXTERNAL
);
1228 compare_values (DECL_GIMPLE_REG_P
);
1229 compare_values (DECL_ALIGN
);
1230 if (code
== LABEL_DECL
)
1232 compare_values (EH_LANDING_PAD_NR
);
1233 compare_values (LABEL_DECL_UID
);
1235 else if (code
== FIELD_DECL
)
1237 compare_values (DECL_PACKED
);
1238 compare_values (DECL_NONADDRESSABLE_P
);
1239 compare_values (DECL_OFFSET_ALIGN
);
1241 else if (code
== VAR_DECL
)
1243 compare_values (DECL_HAS_DEBUG_EXPR_P
);
1244 compare_values (DECL_NONLOCAL_FRAME
);
1246 if (code
== RESULT_DECL
1247 || code
== PARM_DECL
1248 || code
== VAR_DECL
)
1250 compare_values (DECL_BY_REFERENCE
);
1251 if (code
== VAR_DECL
1252 || code
== PARM_DECL
)
1253 compare_values (DECL_HAS_VALUE_EXPR_P
);
1257 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WRTL
))
1258 compare_values (DECL_REGISTER
);
1260 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1262 compare_values (DECL_COMMON
);
1263 compare_values (DECL_DLLIMPORT_P
);
1264 compare_values (DECL_WEAK
);
1265 compare_values (DECL_SEEN_IN_BIND_EXPR_P
);
1266 compare_values (DECL_COMDAT
);
1267 compare_values (DECL_VISIBILITY
);
1268 compare_values (DECL_VISIBILITY_SPECIFIED
);
1269 if (code
== VAR_DECL
)
1271 compare_values (DECL_HARD_REGISTER
);
1272 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1273 compare_values (DECL_IN_CONSTANT_POOL
);
1274 compare_values (DECL_TLS_MODEL
);
1276 if (VAR_OR_FUNCTION_DECL_P (t1
))
1277 compare_values (DECL_INIT_PRIORITY
);
1280 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1282 compare_values (DECL_BUILT_IN_CLASS
);
1283 compare_values (DECL_STATIC_CONSTRUCTOR
);
1284 compare_values (DECL_STATIC_DESTRUCTOR
);
1285 compare_values (DECL_UNINLINABLE
);
1286 compare_values (DECL_POSSIBLY_INLINED
);
1287 compare_values (DECL_IS_NOVOPS
);
1288 compare_values (DECL_IS_RETURNS_TWICE
);
1289 compare_values (DECL_IS_MALLOC
);
1290 compare_values (DECL_IS_OPERATOR_NEW
);
1291 compare_values (DECL_DECLARED_INLINE_P
);
1292 compare_values (DECL_STATIC_CHAIN
);
1293 compare_values (DECL_NO_INLINE_WARNING_P
);
1294 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT
);
1295 compare_values (DECL_NO_LIMIT_STACK
);
1296 compare_values (DECL_DISREGARD_INLINE_LIMITS
);
1297 compare_values (DECL_PURE_P
);
1298 compare_values (DECL_LOOPING_CONST_OR_PURE_P
);
1299 compare_values (DECL_FINAL_P
);
1300 compare_values (DECL_CXX_CONSTRUCTOR_P
);
1301 compare_values (DECL_CXX_DESTRUCTOR_P
);
1302 if (DECL_BUILT_IN_CLASS (t1
) != NOT_BUILT_IN
)
1303 compare_values (DECL_FUNCTION_CODE
);
1304 if (DECL_STATIC_DESTRUCTOR (t1
))
1305 compare_values (DECL_FINI_PRIORITY
);
1308 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1310 compare_values (TYPE_MODE
);
1311 compare_values (TYPE_STRING_FLAG
);
1312 compare_values (TYPE_NO_FORCE_BLK
);
1313 compare_values (TYPE_NEEDS_CONSTRUCTING
);
1314 if (RECORD_OR_UNION_TYPE_P (t1
))
1316 compare_values (TYPE_TRANSPARENT_AGGR
);
1317 compare_values (TYPE_FINAL_P
);
1319 else if (code
== ARRAY_TYPE
)
1320 compare_values (TYPE_NONALIASED_COMPONENT
);
1321 compare_values (TYPE_PACKED
);
1322 compare_values (TYPE_RESTRICT
);
1323 compare_values (TYPE_USER_ALIGN
);
1324 compare_values (TYPE_READONLY
);
1325 compare_values (TYPE_PRECISION
);
1326 compare_values (TYPE_ALIGN
);
1327 compare_values (TYPE_ALIAS_SET
);
1330 /* We don't want to compare locations, so there is nothing do compare
1333 /* BLOCKs are function local and we don't merge anything there, so
1334 simply refuse to merge. */
1335 if (CODE_CONTAINS_STRUCT (code
, TS_BLOCK
))
1338 if (CODE_CONTAINS_STRUCT (code
, TS_TRANSLATION_UNIT_DECL
))
1339 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1
),
1340 TRANSLATION_UNIT_LANGUAGE (t2
)) != 0)
1343 if (CODE_CONTAINS_STRUCT (code
, TS_TARGET_OPTION
))
1344 if (memcmp (TREE_TARGET_OPTION (t1
), TREE_TARGET_OPTION (t2
),
1345 sizeof (struct cl_target_option
)) != 0)
1348 if (CODE_CONTAINS_STRUCT (code
, TS_OPTIMIZATION
))
1349 if (memcmp (TREE_OPTIMIZATION (t1
), TREE_OPTIMIZATION (t2
),
1350 sizeof (struct cl_optimization
)) != 0)
1353 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1354 if (vec_safe_length (BINFO_BASE_ACCESSES (t1
))
1355 != vec_safe_length (BINFO_BASE_ACCESSES (t2
)))
1358 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1359 compare_values (CONSTRUCTOR_NELTS
);
1361 if (CODE_CONTAINS_STRUCT (code
, TS_IDENTIFIER
))
1362 if (IDENTIFIER_LENGTH (t1
) != IDENTIFIER_LENGTH (t2
)
1363 || memcmp (IDENTIFIER_POINTER (t1
), IDENTIFIER_POINTER (t2
),
1364 IDENTIFIER_LENGTH (t1
)) != 0)
1367 if (CODE_CONTAINS_STRUCT (code
, TS_STRING
))
1368 if (TREE_STRING_LENGTH (t1
) != TREE_STRING_LENGTH (t2
)
1369 || memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
1370 TREE_STRING_LENGTH (t1
)) != 0)
1373 #undef compare_values
1376 /* Compare pointer fields. */
1378 /* Recurse. Search & Replaced from DFS_write_tree_body.
1379 Folding the early checks into the compare_tree_edges recursion
1380 macro makes debugging way quicker as you are able to break on
1381 compare_tree_sccs_1 and simply finish until a call returns false
1382 to spot the SCC members with the difference. */
1383 #define compare_tree_edges(E1, E2) \
1385 tree t1_ = (E1), t2_ = (E2); \
1388 || !TREE_VISITED (t2_) \
1389 || (!TREE_ASM_WRITTEN (t2_) \
1390 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1392 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1393 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1396 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
1398 if (code
!= IDENTIFIER_NODE
)
1399 compare_tree_edges (TREE_TYPE (t1
), TREE_TYPE (t2
));
1402 if (CODE_CONTAINS_STRUCT (code
, TS_VECTOR
))
1405 /* Note that the number of elements for EXPR has already been emitted
1406 in EXPR's header (see streamer_write_tree_header). */
1407 for (i
= 0; i
< VECTOR_CST_NELTS (t1
); ++i
)
1408 compare_tree_edges (VECTOR_CST_ELT (t1
, i
), VECTOR_CST_ELT (t2
, i
));
1411 if (CODE_CONTAINS_STRUCT (code
, TS_COMPLEX
))
1413 compare_tree_edges (TREE_REALPART (t1
), TREE_REALPART (t2
));
1414 compare_tree_edges (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
1417 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_MINIMAL
))
1419 compare_tree_edges (DECL_NAME (t1
), DECL_NAME (t2
));
1420 /* ??? Global decls from different TUs have non-matching
1421 TRANSLATION_UNIT_DECLs. Only consider a small set of
1422 decls equivalent, we should not end up merging others. */
1423 if ((code
== TYPE_DECL
1424 || code
== NAMESPACE_DECL
1425 || code
== IMPORTED_DECL
1426 || code
== CONST_DECL
1427 || (VAR_OR_FUNCTION_DECL_P (t1
)
1428 && (TREE_PUBLIC (t1
) || DECL_EXTERNAL (t1
))))
1429 && DECL_FILE_SCOPE_P (t1
) && DECL_FILE_SCOPE_P (t2
))
1432 compare_tree_edges (DECL_CONTEXT (t1
), DECL_CONTEXT (t2
));
1435 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1437 compare_tree_edges (DECL_SIZE (t1
), DECL_SIZE (t2
));
1438 compare_tree_edges (DECL_SIZE_UNIT (t1
), DECL_SIZE_UNIT (t2
));
1439 compare_tree_edges (DECL_ATTRIBUTES (t1
), DECL_ATTRIBUTES (t2
));
1440 if ((code
== VAR_DECL
1441 || code
== PARM_DECL
)
1442 && DECL_HAS_VALUE_EXPR_P (t1
))
1443 compare_tree_edges (DECL_VALUE_EXPR (t1
), DECL_VALUE_EXPR (t2
));
1444 if (code
== VAR_DECL
1445 && DECL_HAS_DEBUG_EXPR_P (t1
))
1446 compare_tree_edges (DECL_DEBUG_EXPR (t1
), DECL_DEBUG_EXPR (t2
));
1447 /* LTO specific edges. */
1448 if (code
!= FUNCTION_DECL
1449 && code
!= TRANSLATION_UNIT_DECL
)
1450 compare_tree_edges (DECL_INITIAL (t1
), DECL_INITIAL (t2
));
1453 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
1455 if (code
== FUNCTION_DECL
)
1458 for (a1
= DECL_ARGUMENTS (t1
), a2
= DECL_ARGUMENTS (t2
);
1460 a1
= TREE_CHAIN (a1
), a2
= TREE_CHAIN (a2
))
1461 compare_tree_edges (a1
, a2
);
1462 compare_tree_edges (DECL_RESULT (t1
), DECL_RESULT (t2
));
1464 else if (code
== TYPE_DECL
)
1465 compare_tree_edges (DECL_ORIGINAL_TYPE (t1
), DECL_ORIGINAL_TYPE (t2
));
1466 compare_tree_edges (DECL_VINDEX (t1
), DECL_VINDEX (t2
));
1469 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1471 /* Make sure we don't inadvertently set the assembler name. */
1472 if (DECL_ASSEMBLER_NAME_SET_P (t1
))
1473 compare_tree_edges (DECL_ASSEMBLER_NAME (t1
),
1474 DECL_ASSEMBLER_NAME (t2
));
1475 compare_tree_edges (DECL_SECTION_NAME (t1
), DECL_SECTION_NAME (t2
));
1476 compare_tree_edges (DECL_COMDAT_GROUP (t1
), DECL_COMDAT_GROUP (t2
));
1479 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
1481 compare_tree_edges (DECL_FIELD_OFFSET (t1
), DECL_FIELD_OFFSET (t2
));
1482 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1
), DECL_BIT_FIELD_TYPE (t2
));
1483 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1
),
1484 DECL_BIT_FIELD_REPRESENTATIVE (t2
));
1485 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1
),
1486 DECL_FIELD_BIT_OFFSET (t2
));
1487 compare_tree_edges (DECL_FCONTEXT (t1
), DECL_FCONTEXT (t2
));
1490 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1492 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1
),
1493 DECL_FUNCTION_PERSONALITY (t2
));
1494 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1
),
1495 DECL_FUNCTION_SPECIFIC_TARGET (t2
));
1496 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1
),
1497 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2
));
1500 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_COMMON
))
1502 compare_tree_edges (TYPE_SIZE (t1
), TYPE_SIZE (t2
));
1503 compare_tree_edges (TYPE_SIZE_UNIT (t1
), TYPE_SIZE_UNIT (t2
));
1504 compare_tree_edges (TYPE_ATTRIBUTES (t1
), TYPE_ATTRIBUTES (t2
));
1505 compare_tree_edges (TYPE_NAME (t1
), TYPE_NAME (t2
));
1506 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1507 reconstructed during fixup. */
1508 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1510 compare_tree_edges (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
));
1511 /* ??? Global types from different TUs have non-matching
1512 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1514 if (TYPE_FILE_SCOPE_P (t1
) && TYPE_FILE_SCOPE_P (t2
))
1517 compare_tree_edges (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
));
1518 /* TYPE_CANONICAL is re-computed during type merging, so do not
1520 compare_tree_edges (TYPE_STUB_DECL (t1
), TYPE_STUB_DECL (t2
));
1523 if (CODE_CONTAINS_STRUCT (code
, TS_TYPE_NON_COMMON
))
1525 if (code
== ENUMERAL_TYPE
)
1526 compare_tree_edges (TYPE_VALUES (t1
), TYPE_VALUES (t2
));
1527 else if (code
== ARRAY_TYPE
)
1528 compare_tree_edges (TYPE_DOMAIN (t1
), TYPE_DOMAIN (t2
));
1529 else if (RECORD_OR_UNION_TYPE_P (t1
))
1532 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1534 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1535 compare_tree_edges (f1
, f2
);
1536 compare_tree_edges (TYPE_BINFO (t1
), TYPE_BINFO (t2
));
1538 else if (code
== FUNCTION_TYPE
1539 || code
== METHOD_TYPE
)
1540 compare_tree_edges (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
));
1541 if (!POINTER_TYPE_P (t1
))
1542 compare_tree_edges (TYPE_MINVAL (t1
), TYPE_MINVAL (t2
));
1543 compare_tree_edges (TYPE_MAXVAL (t1
), TYPE_MAXVAL (t2
));
1546 if (CODE_CONTAINS_STRUCT (code
, TS_LIST
))
1548 compare_tree_edges (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
));
1549 compare_tree_edges (TREE_VALUE (t1
), TREE_VALUE (t2
));
1550 compare_tree_edges (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1553 if (CODE_CONTAINS_STRUCT (code
, TS_VEC
))
1554 for (int i
= 0; i
< TREE_VEC_LENGTH (t1
); i
++)
1555 compare_tree_edges (TREE_VEC_ELT (t1
, i
), TREE_VEC_ELT (t2
, i
));
1557 if (CODE_CONTAINS_STRUCT (code
, TS_EXP
))
1559 for (int i
= 0; i
< TREE_OPERAND_LENGTH (t1
); i
++)
1560 compare_tree_edges (TREE_OPERAND (t1
, i
),
1561 TREE_OPERAND (t2
, i
));
1563 /* BLOCKs are function local and we don't merge anything there. */
1564 if (TREE_BLOCK (t1
) || TREE_BLOCK (t2
))
1568 if (CODE_CONTAINS_STRUCT (code
, TS_BINFO
))
1572 /* Lengths have already been compared above. */
1573 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1
), i
, t
)
1574 compare_tree_edges (t
, BINFO_BASE_BINFO (t2
, i
));
1575 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1
), i
, t
)
1576 compare_tree_edges (t
, BINFO_BASE_ACCESS (t2
, i
));
1577 compare_tree_edges (BINFO_OFFSET (t1
), BINFO_OFFSET (t2
));
1578 compare_tree_edges (BINFO_VTABLE (t1
), BINFO_VTABLE (t2
));
1579 compare_tree_edges (BINFO_VPTR_FIELD (t1
), BINFO_VPTR_FIELD (t2
));
1580 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1581 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1584 if (CODE_CONTAINS_STRUCT (code
, TS_CONSTRUCTOR
))
1588 /* Lengths have already been compared above. */
1589 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, index
, value
)
1591 compare_tree_edges (index
, CONSTRUCTOR_ELT (t2
, i
)->index
);
1592 compare_tree_edges (value
, CONSTRUCTOR_ELT (t2
, i
)->value
);
1596 #undef compare_tree_edges
1601 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1602 out MAP if they are equal. */
1605 compare_tree_sccs (tree_scc
*pscc
, tree_scc
*scc
,
1608 /* Assume SCC entry hashes are sorted after their cardinality. Which
1609 means we can simply take the first n-tuple of equal hashes
1610 (which is recorded as entry_len) and do n SCC entry candidate
1612 for (unsigned i
= 0; i
< pscc
->entry_len
; ++i
)
1615 num_scc_compare_collisions
++;
1616 if (compare_tree_sccs_1 (pscc
->entries
[0], scc
->entries
[i
], &mapp
))
1618 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1619 on the scc as all trees will be freed. */
1622 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1623 the SCC prevails. */
1624 for (unsigned j
= 0; j
< scc
->len
; ++j
)
1625 TREE_ASM_WRITTEN (scc
->entries
[j
]) = 0;
1631 /* QSort sort function to sort a map of two pointers after the 2nd
1635 cmp_tree (const void *p1_
, const void *p2_
)
1637 tree
*p1
= (tree
*)(const_cast<void *>(p1_
));
1638 tree
*p2
= (tree
*)(const_cast<void *>(p2_
));
1641 return ((uintptr_t)p1
[1] < (uintptr_t)p2
[1]) ? -1 : 1;
1644 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1645 hash value SCC_HASH with an already recorded SCC. Return true if
1646 that was successful, otherwise return false. */
1649 unify_scc (struct streamer_tree_cache_d
*cache
, unsigned from
,
1650 unsigned len
, unsigned scc_entry_len
, hashval_t scc_hash
)
1652 bool unified_p
= false;
1654 = (tree_scc
*) alloca (sizeof (tree_scc
) + (len
- 1) * sizeof (tree
));
1656 scc
->hash
= scc_hash
;
1658 scc
->entry_len
= scc_entry_len
;
1659 for (unsigned i
= 0; i
< len
; ++i
)
1661 tree t
= streamer_tree_cache_get_tree (cache
, from
+ i
);
1662 scc
->entries
[i
] = t
;
1663 /* Do not merge SCCs with local entities inside them. Also do
1664 not merge TRANSLATION_UNIT_DECLs. */
1665 if (TREE_CODE (t
) == TRANSLATION_UNIT_DECL
1666 || (VAR_OR_FUNCTION_DECL_P (t
)
1667 && !(TREE_PUBLIC (t
) || DECL_EXTERNAL (t
)))
1668 || TREE_CODE (t
) == LABEL_DECL
)
1670 /* Avoid doing any work for these cases and do not worry to
1671 record the SCCs for further merging. */
1676 /* Look for the list of candidate SCCs to compare against. */
1678 slot
= tree_scc_hash
.find_slot_with_hash (scc
, scc_hash
, INSERT
);
1681 /* Try unifying against each candidate. */
1684 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1685 outside of the scc when following tree edges. Make sure
1686 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1687 to track whether we visited the SCC member during the compare.
1688 We cannot use TREE_VISITED on the pscc members as the extended
1689 scc and pscc can overlap. */
1690 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1692 TREE_VISITED (scc
->entries
[i
]) = 1;
1693 gcc_checking_assert (!TREE_ASM_WRITTEN (scc
->entries
[i
]));
1696 tree
*map
= XALLOCAVEC (tree
, 2 * len
);
1697 for (tree_scc
*pscc
= *slot
; pscc
; pscc
= pscc
->next
)
1699 if (!compare_tree_sccs (pscc
, scc
, map
))
1702 /* Found an equal SCC. */
1704 num_scc_compare_collisions
--;
1706 total_scc_size_merged
+= len
;
1708 #ifdef ENABLE_CHECKING
1709 for (unsigned i
= 0; i
< len
; ++i
)
1711 tree t
= map
[2*i
+1];
1712 enum tree_code code
= TREE_CODE (t
);
1713 /* IDENTIFIER_NODEs should be singletons and are merged by the
1714 streamer. The others should be singletons, too, and we
1715 should not merge them in any way. */
1716 gcc_assert (code
!= TRANSLATION_UNIT_DECL
1717 && code
!= IDENTIFIER_NODE
1718 && !streamer_handle_as_builtin_p (t
));
1722 /* Fixup the streamer cache with the prevailing nodes according
1723 to the tree node mapping computed by compare_tree_sccs. */
1725 streamer_tree_cache_replace_tree (cache
, pscc
->entries
[0], from
);
1728 tree
*map2
= XALLOCAVEC (tree
, 2 * len
);
1729 for (unsigned i
= 0; i
< len
; ++i
)
1731 map2
[i
*2] = (tree
)(uintptr_t)(from
+ i
);
1732 map2
[i
*2+1] = scc
->entries
[i
];
1734 qsort (map2
, len
, 2 * sizeof (tree
), cmp_tree
);
1735 qsort (map
, len
, 2 * sizeof (tree
), cmp_tree
);
1736 for (unsigned i
= 0; i
< len
; ++i
)
1737 streamer_tree_cache_replace_tree (cache
, map
[2*i
],
1738 (uintptr_t)map2
[2*i
]);
1741 /* Free the tree nodes from the read SCC. */
1742 for (unsigned i
= 0; i
< len
; ++i
)
1744 if (TYPE_P (scc
->entries
[i
]))
1746 ggc_free (scc
->entries
[i
]);
1752 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1754 for (unsigned i
= 0; i
< scc
->len
; ++i
)
1755 TREE_VISITED (scc
->entries
[i
]) = 0;
1758 /* If we didn't unify it to any candidate duplicate the relevant
1759 pieces to permanent storage and link it into the chain. */
1763 = XOBNEWVAR (&tree_scc_hash_obstack
, tree_scc
, sizeof (tree_scc
));
1764 memcpy (pscc
, scc
, sizeof (tree_scc
));
1765 pscc
->next
= (*slot
);
1772 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1773 RESOLUTIONS is the set of symbols picked by the linker (read from the
1774 resolution file when the linker plugin is being used). */
1777 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
1778 vec
<ld_plugin_symbol_resolution_t
> resolutions
)
1780 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
1781 const int decl_offset
= sizeof (struct lto_decl_header
);
1782 const int main_offset
= decl_offset
+ header
->decl_state_size
;
1783 const int string_offset
= main_offset
+ header
->main_size
;
1784 struct lto_input_block ib_main
;
1785 struct data_in
*data_in
;
1787 const uint32_t *data_ptr
, *data_end
;
1788 uint32_t num_decl_states
;
1790 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1793 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
1794 header
->string_size
, resolutions
);
1796 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1797 internal types that should not be merged. */
1799 /* Read the global declarations and types. */
1800 while (ib_main
.p
< ib_main
.len
)
1803 unsigned from
= data_in
->reader_cache
->nodes
.length ();
1804 /* Read and uniquify SCCs as in the input stream. */
1805 enum LTO_tags tag
= streamer_read_record_start (&ib_main
);
1806 if (tag
== LTO_tree_scc
)
1809 unsigned scc_entry_len
;
1810 hashval_t scc_hash
= lto_input_scc (&ib_main
, data_in
, &len_
,
1812 unsigned len
= data_in
->reader_cache
->nodes
.length () - from
;
1813 gcc_assert (len
== len_
);
1815 total_scc_size
+= len
;
1818 /* We have the special case of size-1 SCCs that are pre-merged
1819 by means of identifier and string sharing for example.
1820 ??? Maybe we should avoid streaming those as SCCs. */
1821 tree first
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1824 && (TREE_CODE (first
) == IDENTIFIER_NODE
1825 || TREE_CODE (first
) == INTEGER_CST
1826 || TREE_CODE (first
) == TRANSLATION_UNIT_DECL
1827 || streamer_handle_as_builtin_p (first
)))
1830 /* Try to unify the SCC with already existing ones. */
1832 && unify_scc (data_in
->reader_cache
, from
,
1833 len
, scc_entry_len
, scc_hash
))
1836 /* Do remaining fixup tasks for prevailing nodes. */
1837 bool seen_type
= false;
1838 for (unsigned i
= 0; i
< len
; ++i
)
1840 tree t
= streamer_tree_cache_get_tree (data_in
->reader_cache
,
1842 /* Reconstruct the type variant and pointer-to/reference-to
1847 num_prevailing_types
++;
1848 lto_fixup_prevailing_type (t
);
1850 /* Compute the canonical type of all types.
1851 ??? Should be able to assert that !TYPE_CANONICAL. */
1852 if (TYPE_P (t
) && !TYPE_CANONICAL (t
))
1853 TYPE_CANONICAL (t
) = gimple_register_canonical_type (t
);
1854 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1855 type which is also member of this SCC. */
1856 if (TREE_CODE (t
) == INTEGER_CST
1857 && !TREE_OVERFLOW (t
))
1858 cache_integer_cst (t
);
1859 /* Register TYPE_DECLs with the debuginfo machinery. */
1861 && TREE_CODE (t
) == TYPE_DECL
)
1862 debug_hooks
->type_decl (t
, !DECL_FILE_SCOPE_P (t
));
1865 /* Register variables and functions with the
1867 if (TREE_CODE (t
) == VAR_DECL
)
1868 lto_register_var_decl_in_symtab (data_in
, t
, from
+ i
);
1869 else if (TREE_CODE (t
) == FUNCTION_DECL
1870 && !DECL_BUILT_IN (t
))
1871 lto_register_function_decl_in_symtab (data_in
, t
, from
+ i
);
1872 /* Scan the tree for references to global functions or
1873 variables and record those for later fixup. */
1874 if (mentions_vars_p (t
))
1875 vec_safe_push (tree_with_vars
, t
);
1879 num_type_scc_trees
+= len
;
1883 /* Pickle stray references. */
1884 t
= lto_input_tree_1 (&ib_main
, data_in
, tag
, 0);
1885 gcc_assert (t
&& data_in
->reader_cache
->nodes
.length () == from
);
1889 /* Read in lto_in_decl_state objects. */
1890 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
1892 (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
1893 num_decl_states
= *data_ptr
++;
1895 gcc_assert (num_decl_states
> 0);
1896 decl_data
->global_decl_state
= lto_new_in_decl_state ();
1897 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
1898 decl_data
->global_decl_state
);
1900 /* Read in per-function decl states and enter them in hash table. */
1901 decl_data
->function_decl_states
=
1902 htab_create_ggc (37, lto_hash_in_decl_state
, lto_eq_in_decl_state
, NULL
);
1904 for (i
= 1; i
< num_decl_states
; i
++)
1906 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
1909 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
1910 slot
= htab_find_slot (decl_data
->function_decl_states
, state
, INSERT
);
1911 gcc_assert (*slot
== NULL
);
1915 if (data_ptr
!= data_end
)
1916 internal_error ("bytecode stream: garbage at the end of symbols section");
1918 /* Set the current decl state to be the global state. */
1919 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
1921 lto_data_in_delete (data_in
);
1924 /* Custom version of strtoll, which is not portable. */
1926 static HOST_WIDEST_INT
1927 lto_parse_hex (const char *p
)
1929 HOST_WIDEST_INT ret
= 0;
1931 for (; *p
!= '\0'; ++p
)
1936 if (c
>= '0' && c
<= '9')
1938 else if (c
>= 'a' && c
<= 'f')
1939 part
= c
- 'a' + 10;
1940 else if (c
>= 'A' && c
<= 'F')
1941 part
= c
- 'A' + 10;
1943 internal_error ("could not parse hex number");
1950 /* Read resolution for file named FILE_NAME. The resolution is read from
1954 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
1956 /* We require that objects in the resolution file are in the same
1957 order as the lto1 command line. */
1958 unsigned int name_len
;
1960 unsigned int num_symbols
;
1962 struct lto_file_decl_data
*file_data
;
1963 splay_tree_node nd
= NULL
;
1968 name_len
= strlen (file
->filename
);
1969 obj_name
= XNEWVEC (char, name_len
+ 1);
1970 fscanf (resolution
, " "); /* Read white space. */
1972 fread (obj_name
, sizeof (char), name_len
, resolution
);
1973 obj_name
[name_len
] = '\0';
1974 if (filename_cmp (obj_name
, file
->filename
) != 0)
1975 internal_error ("unexpected file name %s in linker resolution file. "
1976 "Expected %s", obj_name
, file
->filename
);
1977 if (file
->offset
!= 0)
1981 HOST_WIDEST_INT offset
;
1982 t
= fscanf (resolution
, "@0x%16s", offset_p
);
1984 internal_error ("could not parse file offset");
1985 offset
= lto_parse_hex (offset_p
);
1986 if (offset
!= file
->offset
)
1987 internal_error ("unexpected offset");
1992 fscanf (resolution
, "%u", &num_symbols
);
1994 for (i
= 0; i
< num_symbols
; i
++)
1998 unsigned HOST_WIDE_INT id
;
2000 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
2002 unsigned int lto_resolution_str_len
=
2003 sizeof (lto_resolution_str
) / sizeof (char *);
2006 t
= fscanf (resolution
, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
" %26s %*[^\n]\n",
2007 &index
, &id
, r_str
);
2009 internal_error ("invalid line in the resolution file");
2011 for (j
= 0; j
< lto_resolution_str_len
; j
++)
2013 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
2015 r
= (enum ld_plugin_symbol_resolution
) j
;
2019 if (j
== lto_resolution_str_len
)
2020 internal_error ("invalid resolution in the resolution file");
2022 if (!(nd
&& lto_splay_tree_id_equal_p (nd
->key
, id
)))
2024 nd
= lto_splay_tree_lookup (file_ids
, id
);
2026 internal_error ("resolution sub id %wx not in object file", id
);
2029 file_data
= (struct lto_file_decl_data
*)nd
->value
;
2030 /* The indexes are very sparse. To save memory save them in a compact
2031 format that is only unpacked later when the subfile is processed. */
2034 file_data
->respairs
.safe_push (rp
);
2035 if (file_data
->max_index
< index
)
2036 file_data
->max_index
= index
;
2040 /* List of file_decl_datas */
2041 struct file_data_list
2043 struct lto_file_decl_data
*first
, *last
;
2046 /* Is the name for a id'ed LTO section? */
2049 lto_section_with_id (const char *name
, unsigned HOST_WIDE_INT
*id
)
2053 if (strncmp (name
, LTO_SECTION_NAME_PREFIX
, strlen (LTO_SECTION_NAME_PREFIX
)))
2055 s
= strrchr (name
, '.');
2056 return s
&& sscanf (s
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, id
) == 1;
2059 /* Create file_data of each sub file id */
2062 create_subid_section_table (struct lto_section_slot
*ls
, splay_tree file_ids
,
2063 struct file_data_list
*list
)
2065 struct lto_section_slot s_slot
, *new_slot
;
2066 unsigned HOST_WIDE_INT id
;
2070 struct lto_file_decl_data
*file_data
;
2072 if (!lto_section_with_id (ls
->name
, &id
))
2075 /* Find hash table of sub module id */
2076 nd
= lto_splay_tree_lookup (file_ids
, id
);
2079 file_data
= (struct lto_file_decl_data
*)nd
->value
;
2083 file_data
= ggc_alloc_lto_file_decl_data ();
2084 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
2086 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();;
2087 lto_splay_tree_insert (file_ids
, id
, file_data
);
2089 /* Maintain list in linker order */
2091 list
->first
= file_data
;
2093 list
->last
->next
= file_data
;
2094 list
->last
= file_data
;
2097 /* Copy section into sub module hash table */
2098 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
2099 s_slot
.name
= new_name
;
2100 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
2101 gcc_assert (*hash_slot
== NULL
);
2103 new_slot
= XDUP (struct lto_section_slot
, ls
);
2104 new_slot
->name
= new_name
;
2105 *hash_slot
= new_slot
;
2109 /* Read declarations and other initializations for a FILE_DATA. */
2112 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
)
2116 vec
<ld_plugin_symbol_resolution_t
>
2117 resolutions
= vNULL
;
2121 /* Create vector for fast access of resolution. We do this lazily
2123 resolutions
.safe_grow_cleared (file_data
->max_index
+ 1);
2124 for (i
= 0; file_data
->respairs
.iterate (i
, &rp
); i
++)
2125 resolutions
[rp
->index
] = rp
->res
;
2126 file_data
->respairs
.release ();
2128 file_data
->renaming_hash_table
= lto_create_renaming_table ();
2129 file_data
->file_name
= file
->filename
;
2130 data
= lto_get_section_data (file_data
, LTO_section_decls
, NULL
, &len
);
2133 internal_error ("cannot read LTO decls from %s", file_data
->file_name
);
2136 /* Frees resolutions */
2137 lto_read_decls (file_data
, data
, resolutions
);
2138 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
2141 /* Finalize FILE_DATA in FILE and increase COUNT. */
2144 lto_create_files_from_ids (lto_file
*file
, struct lto_file_decl_data
*file_data
,
2147 lto_file_finalize (file_data
, file
);
2148 if (cgraph_dump_file
)
2149 fprintf (cgraph_dump_file
, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX
"\n",
2150 file_data
->file_name
, file_data
->id
);
2155 /* Generate a TREE representation for all types and external decls
2158 Read all of the globals out of the file. Then read the cgraph
2159 and process the .o index into the cgraph nodes so that it can open
2160 the .o file to load the functions and ipa information. */
2162 static struct lto_file_decl_data
*
2163 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
2165 struct lto_file_decl_data
*file_data
= NULL
;
2166 splay_tree file_ids
;
2167 htab_t section_hash_table
;
2168 struct lto_section_slot
*section
;
2169 struct file_data_list file_list
;
2170 struct lto_section_list section_list
;
2172 memset (§ion_list
, 0, sizeof (struct lto_section_list
));
2173 section_hash_table
= lto_obj_build_section_table (file
, §ion_list
);
2175 /* Find all sub modules in the object and put their sections into new hash
2176 tables in a splay tree. */
2177 file_ids
= lto_splay_tree_new ();
2178 memset (&file_list
, 0, sizeof (struct file_data_list
));
2179 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
2180 create_subid_section_table (section
, file_ids
, &file_list
);
2182 /* Add resolutions to file ids */
2183 lto_resolution_read (file_ids
, resolution_file
, file
);
2185 /* Finalize each lto file for each submodule in the merged object */
2186 for (file_data
= file_list
.first
; file_data
!= NULL
; file_data
= file_data
->next
)
2187 lto_create_files_from_ids (file
, file_data
, count
);
2189 splay_tree_delete (file_ids
);
2190 htab_delete (section_hash_table
);
2192 return file_list
.first
;
2195 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2196 #define LTO_MMAP_IO 1
2200 /* Page size of machine is used for mmap and munmap calls. */
2201 static size_t page_mask
;
2204 /* Get the section data of length LEN from FILENAME starting at
2205 OFFSET. The data segment must be freed by the caller when the
2206 caller is finished. Returns NULL if all was not well. */
2209 lto_read_section_data (struct lto_file_decl_data
*file_data
,
2210 intptr_t offset
, size_t len
)
2214 static char *fd_name
;
2216 intptr_t computed_len
;
2217 intptr_t computed_offset
;
2221 /* Keep a single-entry file-descriptor cache. The last file we
2222 touched will get closed at exit.
2223 ??? Eventually we want to add a more sophisticated larger cache
2224 or rather fix function body streaming to not stream them in
2225 practically random order. */
2227 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
2235 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
2238 fatal_error ("Cannot open %s", file_data
->file_name
);
2241 fd_name
= xstrdup (file_data
->file_name
);
2247 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
2248 page_mask
= ~(page_size
- 1);
2251 computed_offset
= offset
& page_mask
;
2252 diff
= offset
- computed_offset
;
2253 computed_len
= len
+ diff
;
2255 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
2256 fd
, computed_offset
);
2257 if (result
== MAP_FAILED
)
2259 fatal_error ("Cannot map %s", file_data
->file_name
);
2263 return result
+ diff
;
2265 result
= (char *) xmalloc (len
);
2266 if (lseek (fd
, offset
, SEEK_SET
) != offset
2267 || read (fd
, result
, len
) != (ssize_t
) len
)
2270 fatal_error ("Cannot read %s", file_data
->file_name
);
2274 /* Native windows doesn't supports delayed unlink on opened file. So
2275 we close file here again. This produces higher I/O load, but at least
2276 it prevents to have dangling file handles preventing unlink. */
2287 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2288 NAME will be NULL unless the section type is for a function
2292 get_section_data (struct lto_file_decl_data
*file_data
,
2293 enum lto_section_type section_type
,
2297 htab_t section_hash_table
= file_data
->section_hash_table
;
2298 struct lto_section_slot
*f_slot
;
2299 struct lto_section_slot s_slot
;
2300 const char *section_name
= lto_get_section_name (section_type
, name
, file_data
);
2304 s_slot
.name
= section_name
;
2305 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
2308 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
2312 free (CONST_CAST (char *, section_name
));
2317 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2318 starts at OFFSET and has LEN bytes. */
2321 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
2322 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
2323 const char *name ATTRIBUTE_UNUSED
,
2324 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
2327 intptr_t computed_len
;
2328 intptr_t computed_offset
;
2333 computed_offset
= ((intptr_t) offset
) & page_mask
;
2334 diff
= (intptr_t) offset
- computed_offset
;
2335 computed_len
= len
+ diff
;
2337 munmap ((caddr_t
) computed_offset
, computed_len
);
2339 free (CONST_CAST(char *, offset
));
2343 static lto_file
*current_lto_file
;
2345 /* Helper for qsort; compare partitions and return one with smaller size.
2346 We sort from greatest to smallest so parallel build doesn't stale on the
2347 longest compilation being executed too late. */
2350 cmp_partitions_size (const void *a
, const void *b
)
2352 const struct ltrans_partition_def
*pa
2353 = *(struct ltrans_partition_def
*const *)a
;
2354 const struct ltrans_partition_def
*pb
2355 = *(struct ltrans_partition_def
*const *)b
;
2356 return pb
->insns
- pa
->insns
;
2359 /* Helper for qsort; compare partitions and return one with smaller order. */
2362 cmp_partitions_order (const void *a
, const void *b
)
2364 const struct ltrans_partition_def
*pa
2365 = *(struct ltrans_partition_def
*const *)a
;
2366 const struct ltrans_partition_def
*pb
2367 = *(struct ltrans_partition_def
*const *)b
;
2368 int ordera
= -1, orderb
= -1;
2370 if (lto_symtab_encoder_size (pa
->encoder
))
2371 ordera
= lto_symtab_encoder_deref (pa
->encoder
, 0)->symbol
.order
;
2372 if (lto_symtab_encoder_size (pb
->encoder
))
2373 orderb
= lto_symtab_encoder_deref (pb
->encoder
, 0)->symbol
.order
;
2374 return orderb
- ordera
;
2377 /* Write all output files in WPA mode and the file with the list of
2381 lto_wpa_write_files (void)
2385 ltrans_partition part
;
2386 FILE *ltrans_output_list_stream
;
2387 char *temp_filename
;
2390 /* Open the LTRANS output list. */
2391 if (!ltrans_output_list
)
2392 fatal_error ("no LTRANS output list filename provided");
2393 ltrans_output_list_stream
= fopen (ltrans_output_list
, "w");
2394 if (ltrans_output_list_stream
== NULL
)
2395 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list
);
2397 timevar_push (TV_WHOPR_WPA
);
2399 FOR_EACH_VEC_ELT (ltrans_partitions
, i
, part
)
2400 lto_stats
.num_output_symtab_nodes
+= lto_symtab_encoder_size (part
->encoder
);
2402 /* Find out statics that need to be promoted
2403 to globals with hidden visibility because they are accessed from multiple
2405 lto_promote_cross_file_statics ();
2407 timevar_pop (TV_WHOPR_WPA
);
2409 timevar_push (TV_WHOPR_WPA_IO
);
2411 /* Generate a prefix for the LTRANS unit files. */
2412 blen
= strlen (ltrans_output_list
);
2413 temp_filename
= (char *) xmalloc (blen
+ sizeof ("2147483648.o"));
2414 strcpy (temp_filename
, ltrans_output_list
);
2415 if (blen
> sizeof (".out")
2416 && strcmp (temp_filename
+ blen
- sizeof (".out") + 1,
2418 temp_filename
[blen
- sizeof (".out") + 1] = '\0';
2419 blen
= strlen (temp_filename
);
2421 n_sets
= ltrans_partitions
.length ();
2423 /* Sort partitions by size so small ones are compiled last.
2424 FIXME: Even when not reordering we may want to output one list for parallel make
2425 and other for final link command. */
2426 ltrans_partitions
.qsort (flag_toplevel_reorder
2427 ? cmp_partitions_size
2428 : cmp_partitions_order
);
2429 for (i
= 0; i
< n_sets
; i
++)
2432 ltrans_partition part
= ltrans_partitions
[i
];
2434 /* Write all the nodes in SET. */
2435 sprintf (temp_filename
+ blen
, "%u.o", i
);
2436 file
= lto_obj_file_open (temp_filename
, true);
2438 fatal_error ("lto_obj_file_open() failed");
2441 fprintf (stderr
, " %s (%s %i insns)", temp_filename
, part
->name
, part
->insns
);
2442 if (cgraph_dump_file
)
2444 lto_symtab_encoder_iterator lsei
;
2446 fprintf (cgraph_dump_file
, "Writing partition %s to file %s, %i insns\n",
2447 part
->name
, temp_filename
, part
->insns
);
2448 fprintf (cgraph_dump_file
, " Symbols in partition: ");
2449 for (lsei
= lsei_start_in_partition (part
->encoder
); !lsei_end_p (lsei
);
2450 lsei_next_in_partition (&lsei
))
2452 symtab_node node
= lsei_node (lsei
);
2453 fprintf (cgraph_dump_file
, "%s ", symtab_node_asm_name (node
));
2455 fprintf (cgraph_dump_file
, "\n Symbols in boundary: ");
2456 for (lsei
= lsei_start (part
->encoder
); !lsei_end_p (lsei
);
2459 symtab_node node
= lsei_node (lsei
);
2460 if (!lto_symtab_encoder_in_partition_p (part
->encoder
, node
))
2462 fprintf (cgraph_dump_file
, "%s ", symtab_node_asm_name (node
));
2463 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
2465 && lto_symtab_encoder_encode_body_p (part
->encoder
, cnode
))
2466 fprintf (cgraph_dump_file
, "(body included)");
2469 varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
);
2471 && lto_symtab_encoder_encode_initializer_p (part
->encoder
, vnode
))
2472 fprintf (cgraph_dump_file
, "(initializer included)");
2476 fprintf (cgraph_dump_file
, "\n");
2478 gcc_checking_assert (lto_symtab_encoder_size (part
->encoder
) || !i
);
2480 lto_set_current_out_file (file
);
2482 ipa_write_optimization_summaries (part
->encoder
);
2484 lto_set_current_out_file (NULL
);
2485 lto_obj_file_close (file
);
2487 part
->encoder
= NULL
;
2489 len
= strlen (temp_filename
);
2490 if (fwrite (temp_filename
, 1, len
, ltrans_output_list_stream
) < len
2491 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
2492 fatal_error ("writing to LTRANS output list %s: %m",
2493 ltrans_output_list
);
2496 lto_stats
.num_output_files
+= n_sets
;
2498 /* Close the LTRANS output list. */
2499 if (fclose (ltrans_output_list_stream
))
2500 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list
);
2502 free_ltrans_partitions();
2503 free (temp_filename
);
2505 timevar_pop (TV_WHOPR_WPA_IO
);
2509 /* If TT is a variable or function decl replace it with its
2510 prevailing variant. */
2511 #define LTO_SET_PREVAIL(tt) \
2513 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2514 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2516 tt = lto_symtab_prevailing_decl (tt); \
2521 /* Ensure that TT isn't a replacable var of function decl. */
2522 #define LTO_NO_PREVAIL(tt) \
2523 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2525 /* Given a tree T replace all fields referring to variables or functions
2526 with their prevailing variant. */
2528 lto_fixup_prevailing_decls (tree t
)
2530 enum tree_code code
= TREE_CODE (t
);
2533 gcc_checking_assert (code
!= CONSTRUCTOR
&& code
!= TREE_BINFO
);
2534 LTO_NO_PREVAIL (TREE_TYPE (t
));
2535 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
2536 LTO_NO_PREVAIL (TREE_CHAIN (t
));
2539 LTO_NO_PREVAIL (DECL_NAME (t
));
2540 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
2541 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
2543 LTO_SET_PREVAIL (DECL_SIZE (t
));
2544 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
2545 LTO_SET_PREVAIL (DECL_INITIAL (t
));
2546 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
2547 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
2549 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
2551 LTO_NO_PREVAIL (t
->decl_with_vis
.assembler_name
);
2552 LTO_NO_PREVAIL (DECL_SECTION_NAME (t
));
2554 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
2556 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t
));
2557 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
2558 LTO_NO_PREVAIL (DECL_VINDEX (t
));
2560 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
2561 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
2562 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
2564 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t
));
2565 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
2566 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
2567 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
2568 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
2571 else if (TYPE_P (t
))
2573 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
2574 LTO_SET_PREVAIL (TYPE_SIZE (t
));
2575 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
2576 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
2577 LTO_NO_PREVAIL (TYPE_NAME (t
));
2579 LTO_SET_PREVAIL (TYPE_MINVAL (t
));
2580 LTO_SET_PREVAIL (TYPE_MAXVAL (t
));
2581 LTO_NO_PREVAIL (t
->type_non_common
.binfo
);
2583 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
2585 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
2586 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
2587 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
2589 else if (EXPR_P (t
))
2592 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
2593 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
2600 LTO_SET_PREVAIL (TREE_VALUE (t
));
2601 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
2602 LTO_NO_PREVAIL (TREE_PURPOSE (t
));
2608 /* If we fixed nothing, then we missed something seen by
2610 gcc_checking_assert (fixed
);
2612 #undef LTO_SET_PREVAIL
2613 #undef LTO_NO_PREVAIL
2615 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2616 replaces var and function decls with the corresponding prevailing def. */
2619 lto_fixup_state (struct lto_in_decl_state
*state
)
2622 struct lto_tree_ref_table
*table
;
2624 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2625 we still need to walk from all DECLs to find the reachable
2626 FUNCTION_DECLs and VAR_DECLs. */
2627 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
2629 table
= &state
->streams
[si
];
2630 for (i
= 0; i
< table
->size
; i
++)
2632 tree
*tp
= table
->trees
+ i
;
2633 if (VAR_OR_FUNCTION_DECL_P (*tp
)
2634 && (TREE_PUBLIC (*tp
) || DECL_EXTERNAL (*tp
)))
2635 *tp
= lto_symtab_prevailing_decl (*tp
);
2640 /* A callback of htab_traverse. Just extracts a state from SLOT
2641 and calls lto_fixup_state. */
2644 lto_fixup_state_aux (void **slot
, void *aux ATTRIBUTE_UNUSED
)
2646 struct lto_in_decl_state
*state
= (struct lto_in_decl_state
*) *slot
;
2647 lto_fixup_state (state
);
2651 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2655 lto_fixup_decls (struct lto_file_decl_data
**files
)
2661 FOR_EACH_VEC_ELT ((*tree_with_vars
), i
, t
)
2662 lto_fixup_prevailing_decls (t
);
2664 for (i
= 0; files
[i
]; i
++)
2666 struct lto_file_decl_data
*file
= files
[i
];
2667 struct lto_in_decl_state
*state
= file
->global_decl_state
;
2668 lto_fixup_state (state
);
2670 htab_traverse (file
->function_decl_states
, lto_fixup_state_aux
, NULL
);
2674 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
2676 /* Turn file datas for sub files into a single array, so that they look
2677 like separate files for further passes. */
2680 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
, int last_file_ix
)
2682 struct lto_file_decl_data
*n
, *next
;
2685 lto_stats
.num_input_files
= count
;
2687 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count
+ 1);
2688 /* Set the hooks so that all of the ipa passes can read in their data. */
2689 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2690 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
2692 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
2694 all_file_decl_data
[k
++] = n
;
2699 all_file_decl_data
[k
] = NULL
;
2700 gcc_assert (k
== count
);
2703 /* Input file data before flattening (i.e. splitting them to subfiles to support
2704 incremental linking. */
2705 static int real_file_count
;
2706 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
2708 static void print_lto_report_1 (void);
2710 /* Read all the symbols from the input files FNAMES. NFILES is the
2711 number of files requested in the command line. Instantiate a
2712 global call graph by aggregating all the sub-graphs found in each
2716 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
2718 unsigned int i
, last_file_ix
;
2721 struct lto_file_decl_data
**decl_data
;
2727 timevar_push (TV_IPA_LTO_DECL_IN
);
2730 = decl_data
= ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles
+ 1);
2731 real_file_count
= nfiles
;
2733 /* Read the resolution file. */
2735 if (resolution_file_name
)
2738 unsigned num_objects
;
2740 resolution
= fopen (resolution_file_name
, "r");
2741 if (resolution
== NULL
)
2742 fatal_error ("could not open symbol resolution file: %m");
2744 t
= fscanf (resolution
, "%u", &num_objects
);
2745 gcc_assert (t
== 1);
2747 /* True, since the plugin splits the archives. */
2748 gcc_assert (num_objects
== nfiles
);
2750 cgraph_state
= CGRAPH_LTO_STREAMING
;
2752 canonical_type_hash_cache
= htab_create_ggc (512, tree_int_map_hash
,
2753 tree_int_map_eq
, NULL
);
2754 gimple_canonical_types
= htab_create_ggc (16381, gimple_canonical_type_hash
,
2755 gimple_canonical_type_eq
, 0);
2756 gcc_obstack_init (&tree_scc_hash_obstack
);
2757 tree_scc_hash
.create (4096);
2759 /* Register the common node types with the canonical type machinery so
2760 we properly share alias-sets across languages and TUs. Do not
2761 expose the common nodes as type merge target - those that should be
2762 are already exposed so by pre-loading the LTO streamer caches. */
2763 for (i
= 0; i
< itk_none
; ++i
)
2764 lto_register_canonical_types (integer_types
[i
]);
2765 /* The sizetypes are not used to access data so we do not need to
2766 do anything about them. */
2767 for (i
= 0; i
< TI_MAX
; ++i
)
2768 lto_register_canonical_types (global_trees
[i
]);
2771 fprintf (stderr
, "Reading object files:");
2773 /* Read all of the object files specified on the command line. */
2774 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
2776 struct lto_file_decl_data
*file_data
= NULL
;
2779 fprintf (stderr
, " %s", fnames
[i
]);
2783 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
2784 if (!current_lto_file
)
2787 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
2790 lto_obj_file_close (current_lto_file
);
2791 free (current_lto_file
);
2792 current_lto_file
= NULL
;
2796 decl_data
[last_file_ix
++] = file_data
;
2798 lto_obj_file_close (current_lto_file
);
2799 free (current_lto_file
);
2800 current_lto_file
= NULL
;
2803 lto_flatten_files (decl_data
, count
, last_file_ix
);
2804 lto_stats
.num_input_files
= count
;
2805 ggc_free(decl_data
);
2806 real_file_decl_data
= NULL
;
2808 if (resolution_file_name
)
2809 fclose (resolution
);
2811 /* Show the LTO report before launching LTRANS. */
2812 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
2813 print_lto_report_1 ();
2815 /* Free gimple type merging datastructures. */
2816 tree_scc_hash
.dispose ();
2817 obstack_free (&tree_scc_hash_obstack
, NULL
);
2818 htab_delete (gimple_canonical_types
);
2819 gimple_canonical_types
= NULL
;
2820 htab_delete (canonical_type_hash_cache
);
2821 canonical_type_hash_cache
= NULL
;
2824 /* Set the hooks so that all of the ipa passes can read in their data. */
2825 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2827 timevar_pop (TV_IPA_LTO_DECL_IN
);
2830 fprintf (stderr
, "\nReading the callgraph\n");
2832 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
2833 /* Read the symtab. */
2836 /* Store resolutions into the symbol table. */
2838 FOR_EACH_SYMBOL (snode
)
2839 if (symtab_real_symbol_p (snode
)
2840 && snode
->symbol
.lto_file_data
2841 && snode
->symbol
.lto_file_data
->resolution_map
2842 && (res
= pointer_map_contains (snode
->symbol
.lto_file_data
->resolution_map
,
2843 snode
->symbol
.decl
)))
2844 snode
->symbol
.resolution
2845 = (enum ld_plugin_symbol_resolution
)(size_t)*res
;
2846 for (i
= 0; all_file_decl_data
[i
]; i
++)
2847 if (all_file_decl_data
[i
]->resolution_map
)
2849 pointer_map_destroy (all_file_decl_data
[i
]->resolution_map
);
2850 all_file_decl_data
[i
]->resolution_map
= NULL
;
2853 timevar_pop (TV_IPA_LTO_CGRAPH_IO
);
2856 fprintf (stderr
, "Merging declarations\n");
2858 timevar_push (TV_IPA_LTO_DECL_MERGE
);
2859 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2860 need to care about resolving symbols again, we only need to replace
2861 duplicated declarations read from the callgraph and from function
2865 lto_symtab_merge_decls ();
2867 /* If there were errors during symbol merging bail out, we have no
2868 good way to recover here. */
2870 fatal_error ("errors during merging of translation units");
2872 /* Fixup all decls. */
2873 lto_fixup_decls (all_file_decl_data
);
2876 ggc_free (tree_with_vars
);
2877 tree_with_vars
= NULL
;
2880 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
2881 /* Each pass will set the appropriate timer. */
2884 fprintf (stderr
, "Reading summaries\n");
2886 /* Read the IPA summary data. */
2888 ipa_read_optimization_summaries ();
2890 ipa_read_summaries ();
2892 for (i
= 0; all_file_decl_data
[i
]; i
++)
2894 gcc_assert (all_file_decl_data
[i
]->symtab_node_encoder
);
2895 lto_symtab_encoder_delete (all_file_decl_data
[i
]->symtab_node_encoder
);
2896 all_file_decl_data
[i
]->symtab_node_encoder
= NULL
;
2897 lto_free_function_in_decl_state (all_file_decl_data
[i
]->global_decl_state
);
2898 all_file_decl_data
[i
]->global_decl_state
= NULL
;
2899 all_file_decl_data
[i
]->current_decl_state
= NULL
;
2902 /* Finally merge the cgraph according to the decl merging decisions. */
2903 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
2904 if (cgraph_dump_file
)
2906 fprintf (cgraph_dump_file
, "Before merging:\n");
2907 dump_symtab (cgraph_dump_file
);
2909 lto_symtab_merge_symbols ();
2911 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
2913 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
2915 timevar_push (TV_IPA_LTO_DECL_INIT_IO
);
2917 /* Indicate that the cgraph is built and ready. */
2918 cgraph_function_flags_ready
= true;
2920 timevar_pop (TV_IPA_LTO_DECL_INIT_IO
);
2921 ggc_free (all_file_decl_data
);
2922 all_file_decl_data
= NULL
;
2926 /* Materialize all the bodies for all the nodes in the callgraph. */
2929 materialize_cgraph (void)
2932 struct cgraph_node
*node
;
2934 timevar_id_t lto_timer
;
2938 flag_wpa
? "Materializing decls:" : "Reading function bodies:");
2940 /* Now that we have input the cgraph, we need to clear all of the aux
2941 nodes and read the functions if we are not running in WPA mode. */
2942 timevar_push (TV_IPA_LTO_GIMPLE_IN
);
2944 FOR_EACH_FUNCTION (node
)
2946 if (node
->symbol
.lto_file_data
)
2948 lto_materialize_function (node
);
2949 lto_stats
.num_input_cgraph_nodes
++;
2953 timevar_pop (TV_IPA_LTO_GIMPLE_IN
);
2955 /* Start the appropriate timer depending on the mode that we are
2957 lto_timer
= (flag_wpa
) ? TV_WHOPR_WPA
2958 : (flag_ltrans
) ? TV_WHOPR_LTRANS
2960 timevar_push (lto_timer
);
2962 current_function_decl
= NULL
;
2965 /* Inform the middle end about the global variables we have seen. */
2966 FOR_EACH_VEC_ELT (*lto_global_var_decls
, i
, decl
)
2967 rest_of_decl_compilation (decl
, 1, 0);
2970 fprintf (stderr
, "\n");
2972 timevar_pop (lto_timer
);
2976 /* Show various memory usage statistics related to LTO. */
2978 print_lto_report_1 (void)
2980 const char *pfx
= (flag_lto
) ? "LTO" : (flag_wpa
) ? "WPA" : "LTRANS";
2981 fprintf (stderr
, "%s statistics\n", pfx
);
2983 fprintf (stderr
, "[%s] read %lu SCCs of average size %f\n",
2984 pfx
, num_sccs_read
, total_scc_size
/ (double)num_sccs_read
);
2985 fprintf (stderr
, "[%s] %lu tree bodies read in total\n", pfx
, total_scc_size
);
2986 if (flag_wpa
&& tree_scc_hash
.is_created ())
2988 fprintf (stderr
, "[%s] tree SCC table: size %ld, %ld elements, "
2989 "collision ratio: %f\n", pfx
,
2990 (long) tree_scc_hash
.size (),
2991 (long) tree_scc_hash
.elements (),
2992 tree_scc_hash
.collisions ());
2993 hash_table
<tree_scc_hasher
>::iterator hiter
;
2994 tree_scc
*scc
, *max_scc
= NULL
;
2995 unsigned max_length
= 0;
2996 FOR_EACH_HASH_TABLE_ELEMENT (tree_scc_hash
, scc
, x
, hiter
)
2998 unsigned length
= 0;
3000 for (; s
; s
= s
->next
)
3002 if (length
> max_length
)
3004 max_length
= length
;
3008 fprintf (stderr
, "[%s] tree SCC max chain length %u (size %u)\n",
3009 pfx
, max_length
, max_scc
->len
);
3010 fprintf (stderr
, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx
,
3011 num_scc_compares
, num_scc_compare_collisions
,
3012 num_scc_compare_collisions
/ (double) num_scc_compares
);
3013 fprintf (stderr
, "[%s] Merged %lu SCCs\n", pfx
, num_sccs_merged
);
3014 fprintf (stderr
, "[%s] Merged %lu tree bodies\n", pfx
,
3015 total_scc_size_merged
);
3016 fprintf (stderr
, "[%s] Merged %lu types\n", pfx
, num_merged_types
);
3017 fprintf (stderr
, "[%s] %lu types prevailed (%lu associated trees)\n",
3018 pfx
, num_prevailing_types
, num_type_scc_trees
);
3019 fprintf (stderr
, "[%s] GIMPLE canonical type table: size %ld, "
3020 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx
,
3021 (long) htab_size (gimple_canonical_types
),
3022 (long) htab_elements (gimple_canonical_types
),
3023 (long) gimple_canonical_types
->searches
,
3024 (long) gimple_canonical_types
->collisions
,
3025 htab_collisions (gimple_canonical_types
));
3026 fprintf (stderr
, "[%s] GIMPLE canonical type hash table: size %ld, "
3027 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx
,
3028 (long) htab_size (canonical_type_hash_cache
),
3029 (long) htab_elements (canonical_type_hash_cache
),
3030 (long) canonical_type_hash_cache
->searches
,
3031 (long) canonical_type_hash_cache
->collisions
,
3032 htab_collisions (canonical_type_hash_cache
));
3035 print_lto_report (pfx
);
3038 /* Perform whole program analysis (WPA) on the callgraph and write out the
3039 optimization plan. */
3042 do_whole_program_analysis (void)
3046 timevar_start (TV_PHASE_OPT_GEN
);
3048 /* Note that since we are in WPA mode, materialize_cgraph will not
3049 actually read in all the function bodies. It only materializes
3050 the decls and cgraph nodes so that analysis can be performed. */
3051 materialize_cgraph ();
3053 /* Reading in the cgraph uses different timers, start timing WPA now. */
3054 timevar_push (TV_WHOPR_WPA
);
3056 if (pre_ipa_mem_report
)
3058 fprintf (stderr
, "Memory consumption before IPA\n");
3059 dump_memory_report (false);
3062 cgraph_function_flags_ready
= true;
3064 if (cgraph_dump_file
)
3065 dump_symtab (cgraph_dump_file
);
3066 bitmap_obstack_initialize (NULL
);
3067 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
3069 execute_ipa_pass_list (g
->get_passes ()->all_regular_ipa_passes
);
3070 symtab_remove_unreachable_nodes (false, dump_file
);
3072 if (cgraph_dump_file
)
3074 fprintf (cgraph_dump_file
, "Optimized ");
3075 dump_symtab (cgraph_dump_file
);
3077 #ifdef ENABLE_CHECKING
3080 bitmap_obstack_release (NULL
);
3082 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3083 timevar_pop (TV_WHOPR_WPA
);
3085 timevar_push (TV_WHOPR_PARTITIONING
);
3086 if (flag_lto_partition_1to1
)
3088 else if (flag_lto_partition_max
)
3091 lto_balanced_map ();
3093 /* AUX pointers are used by partitioning code to bookkeep number of
3094 partitions symbol is in. This is no longer needed. */
3095 FOR_EACH_SYMBOL (node
)
3096 node
->symbol
.aux
= NULL
;
3098 lto_stats
.num_cgraph_partitions
+= ltrans_partitions
.length ();
3099 timevar_pop (TV_WHOPR_PARTITIONING
);
3101 timevar_stop (TV_PHASE_OPT_GEN
);
3102 timevar_start (TV_PHASE_STREAM_OUT
);
3106 fprintf (stderr
, "\nStreaming out");
3109 lto_wpa_write_files ();
3111 fprintf (stderr
, "\n");
3113 timevar_stop (TV_PHASE_STREAM_OUT
);
3116 if (post_ipa_mem_report
)
3118 fprintf (stderr
, "Memory consumption after IPA\n");
3119 dump_memory_report (false);
3122 /* Show the LTO report before launching LTRANS. */
3123 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
3124 print_lto_report_1 ();
3126 dump_memory_report (true);
3130 static GTY(()) tree lto_eh_personality_decl
;
3132 /* Return the LTO personality function decl. */
3135 lto_eh_personality (void)
3137 if (!lto_eh_personality_decl
)
3139 /* Use the first personality DECL for our personality if we don't
3140 support multiple ones. This ensures that we don't artificially
3141 create the need for them in a single-language program. */
3142 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
3143 lto_eh_personality_decl
= first_personality_decl
;
3145 lto_eh_personality_decl
= lhd_gcc_personality ();
3148 return lto_eh_personality_decl
;
3151 /* Set the process name based on the LTO mode. */
3154 lto_process_name (void)
3157 setproctitle ("lto1-lto");
3159 setproctitle ("lto1-wpa");
3161 setproctitle ("lto1-ltrans");
3165 /* Initialize the LTO front end. */
3170 lto_process_name ();
3171 lto_streamer_hooks_init ();
3173 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
3174 memset (<o_stats
, 0, sizeof (lto_stats
));
3175 bitmap_obstack_initialize (NULL
);
3176 gimple_register_cfg_hooks ();
3180 /* Main entry point for the GIMPLE front end. This front end has
3181 three main personalities:
3183 - LTO (-flto). All the object files on the command line are
3184 loaded in memory and processed as a single translation unit.
3185 This is the traditional link-time optimization behavior.
3187 - WPA (-fwpa). Only the callgraph and summary information for
3188 files in the command file are loaded. A single callgraph
3189 (without function bodies) is instantiated for the whole set of
3190 files. IPA passes are only allowed to analyze the call graph
3191 and make transformation decisions. The callgraph is
3192 partitioned, each partition is written to a new object file
3193 together with the transformation decisions.
3195 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3196 summary files from running again. Since WPA computed summary
3197 information and decided what transformations to apply, LTRANS
3198 simply applies them. */
3203 /* LTO is called as a front end, even though it is not a front end.
3204 Because it is called as a front end, TV_PHASE_PARSING and
3205 TV_PARSE_GLOBAL are active, and we need to turn them off while
3206 doing LTO. Later we turn them back on so they are active up in
3208 timevar_pop (TV_PARSE_GLOBAL
);
3209 timevar_stop (TV_PHASE_PARSING
);
3211 timevar_start (TV_PHASE_SETUP
);
3213 /* Initialize the LTO front end. */
3216 timevar_stop (TV_PHASE_SETUP
);
3217 timevar_start (TV_PHASE_STREAM_IN
);
3219 /* Read all the symbols and call graph from all the files in the
3221 read_cgraph_and_symbols (num_in_fnames
, in_fnames
);
3223 timevar_stop (TV_PHASE_STREAM_IN
);
3227 /* If WPA is enabled analyze the whole call graph and create an
3228 optimization plan. Otherwise, read in all the function
3229 bodies and continue with optimization. */
3231 do_whole_program_analysis ();
3234 struct varpool_node
*vnode
;
3236 timevar_start (TV_PHASE_OPT_GEN
);
3238 materialize_cgraph ();
3240 lto_promote_statics_nonwpa ();
3242 /* Let the middle end know that we have read and merged all of
3246 timevar_stop (TV_PHASE_OPT_GEN
);
3248 /* FIXME lto, if the processes spawned by WPA fail, we miss
3249 the chance to print WPA's report, so WPA will call
3250 print_lto_report before launching LTRANS. If LTRANS was
3251 launched directly by the driver we would not need to do
3253 if (flag_lto_report
|| (flag_wpa
&& flag_lto_report_wpa
))
3254 print_lto_report_1 ();
3256 /* Record the global variables. */
3257 FOR_EACH_DEFINED_VARIABLE (vnode
)
3258 vec_safe_push (lto_global_var_decls
, vnode
->symbol
.decl
);
3262 /* Here we make LTO pretend to be a parser. */
3263 timevar_start (TV_PHASE_PARSING
);
3264 timevar_push (TV_PARSE_GLOBAL
);
3267 #include "gt-lto-lto.h"