1 /* Top-level LTO routines.
2 Copyright 2009, 2010, 2011, 2012 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"
49 static GTY(()) tree first_personality_decl
;
51 /* Returns a hash code for P. */
54 hash_name (const void *p
)
56 const struct lto_section_slot
*ds
= (const struct lto_section_slot
*) p
;
57 return (hashval_t
) htab_hash_string (ds
->name
);
61 /* Returns nonzero if P1 and P2 are equal. */
64 eq_name (const void *p1
, const void *p2
)
66 const struct lto_section_slot
*s1
=
67 (const struct lto_section_slot
*) p1
;
68 const struct lto_section_slot
*s2
=
69 (const struct lto_section_slot
*) p2
;
71 return strcmp (s1
->name
, s2
->name
) == 0;
74 /* Free lto_section_slot */
77 free_with_string (void *arg
)
79 struct lto_section_slot
*s
= (struct lto_section_slot
*)arg
;
81 free (CONST_CAST (char *, s
->name
));
85 /* Create section hash table */
88 lto_obj_create_section_hash_table (void)
90 return htab_create (37, hash_name
, eq_name
, free_with_string
);
93 /* Delete an allocated integer KEY in the splay tree. */
96 lto_splay_tree_delete_id (splay_tree_key key
)
101 /* Compare splay tree node ids A and B. */
104 lto_splay_tree_compare_ids (splay_tree_key a
, splay_tree_key b
)
106 unsigned HOST_WIDE_INT ai
;
107 unsigned HOST_WIDE_INT bi
;
109 ai
= *(unsigned HOST_WIDE_INT
*) a
;
110 bi
= *(unsigned HOST_WIDE_INT
*) b
;
119 /* Look up splay tree node by ID in splay tree T. */
121 static splay_tree_node
122 lto_splay_tree_lookup (splay_tree t
, unsigned HOST_WIDE_INT id
)
124 return splay_tree_lookup (t
, (splay_tree_key
) &id
);
127 /* Check if KEY has ID. */
130 lto_splay_tree_id_equal_p (splay_tree_key key
, unsigned HOST_WIDE_INT id
)
132 return *(unsigned HOST_WIDE_INT
*) key
== id
;
135 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
136 The ID is allocated separately because we need HOST_WIDE_INTs which may
137 be wider than a splay_tree_key. */
140 lto_splay_tree_insert (splay_tree t
, unsigned HOST_WIDE_INT id
,
141 struct lto_file_decl_data
*file_data
)
143 unsigned HOST_WIDE_INT
*idp
= XCNEW (unsigned HOST_WIDE_INT
);
145 splay_tree_insert (t
, (splay_tree_key
) idp
, (splay_tree_value
) file_data
);
148 /* Create a splay tree. */
151 lto_splay_tree_new (void)
153 return splay_tree_new (lto_splay_tree_compare_ids
,
154 lto_splay_tree_delete_id
,
158 /* Return true when NODE has a clone that is analyzed (i.e. we need
159 to load its body even if the node itself is not needed). */
162 has_analyzed_clone_p (struct cgraph_node
*node
)
164 struct cgraph_node
*orig
= node
;
173 else if (node
->next_sibling_clone
)
174 node
= node
->next_sibling_clone
;
177 while (node
!= orig
&& !node
->next_sibling_clone
)
178 node
= node
->clone_of
;
180 node
= node
->next_sibling_clone
;
186 /* Read the function body for the function associated with NODE. */
189 lto_materialize_function (struct cgraph_node
*node
)
192 struct lto_file_decl_data
*file_data
;
193 const char *data
, *name
;
196 decl
= node
->symbol
.decl
;
197 /* Read in functions with body (analyzed nodes)
198 and also functions that are needed to produce virtual clones. */
199 if (cgraph_function_with_gimple_body_p (node
) || has_analyzed_clone_p (node
))
201 /* Clones and thunks don't need to be read. */
205 /* Load the function body only if not operating in WPA mode. In
206 WPA mode, the body of the function is not needed. */
209 file_data
= node
->symbol
.lto_file_data
;
210 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
212 /* We may have renamed the declaration, e.g., a static function. */
213 name
= lto_get_decl_name_mapping (file_data
, name
);
215 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
218 fatal_error ("%s: section %s is missing",
219 file_data
->file_name
,
222 gcc_assert (DECL_STRUCT_FUNCTION (decl
) == NULL
);
224 allocate_struct_function (decl
, false);
225 announce_function (decl
);
226 lto_input_function_body (file_data
, decl
, data
);
227 if (DECL_FUNCTION_PERSONALITY (decl
) && !first_personality_decl
)
228 first_personality_decl
= DECL_FUNCTION_PERSONALITY (decl
);
229 lto_stats
.num_function_bodies
++;
230 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
236 /* Let the middle end know about the function. */
237 rest_of_decl_compilation (decl
, 1, 0);
241 /* Decode the content of memory pointed to by DATA in the in decl
242 state object STATE. DATA_IN points to a data_in structure for
243 decoding. Return the address after the decoded object in the
246 static const uint32_t *
247 lto_read_in_decl_state (struct data_in
*data_in
, const uint32_t *data
,
248 struct lto_in_decl_state
*state
)
255 decl
= streamer_tree_cache_get (data_in
->reader_cache
, ix
);
256 if (TREE_CODE (decl
) != FUNCTION_DECL
)
258 gcc_assert (decl
== void_type_node
);
261 state
->fn_decl
= decl
;
263 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
265 uint32_t size
= *data
++;
266 tree
*decls
= ggc_alloc_vec_tree (size
);
268 for (j
= 0; j
< size
; j
++)
269 decls
[j
] = streamer_tree_cache_get (data_in
->reader_cache
, data
[j
]);
271 state
->streams
[i
].size
= size
;
272 state
->streams
[i
].trees
= decls
;
279 /* A hashtable of trees that potentially refer to variables or functions
280 that must be replaced with their prevailing variant. */
281 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node
))) htab_t
284 /* Remember that T is a tree that (potentially) refers to a variable
285 or function decl that may be replaced with its prevailing variant. */
287 remember_with_vars (tree t
)
289 *(tree
*) htab_find_slot (tree_with_vars
, t
, INSERT
) = t
;
292 #define GIMPLE_REGISTER_TYPE(tt) \
293 (TREE_VISITED (tt) ? gimple_register_type (tt) : tt)
295 #define LTO_FIXUP_TREE(tt) \
301 (tt) = GIMPLE_REGISTER_TYPE (tt); \
302 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
303 remember_with_vars (t); \
307 static void lto_fixup_types (tree
);
309 /* Fix up fields of a tree_typed T. */
312 lto_ft_typed (tree t
)
314 LTO_FIXUP_TREE (TREE_TYPE (t
));
317 /* Fix up fields of a tree_common T. */
320 lto_ft_common (tree t
)
323 LTO_FIXUP_TREE (TREE_CHAIN (t
));
326 /* Fix up fields of a decl_minimal T. */
329 lto_ft_decl_minimal (tree t
)
332 LTO_FIXUP_TREE (DECL_NAME (t
));
333 LTO_FIXUP_TREE (DECL_CONTEXT (t
));
336 /* Fix up fields of a decl_common T. */
339 lto_ft_decl_common (tree t
)
341 lto_ft_decl_minimal (t
);
342 LTO_FIXUP_TREE (DECL_SIZE (t
));
343 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t
));
344 LTO_FIXUP_TREE (DECL_INITIAL (t
));
345 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t
));
346 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t
));
349 /* Fix up fields of a decl_with_vis T. */
352 lto_ft_decl_with_vis (tree t
)
354 lto_ft_decl_common (t
);
356 /* Accessor macro has side-effects, use field-name here. */
357 LTO_FIXUP_TREE (t
->decl_with_vis
.assembler_name
);
358 LTO_FIXUP_TREE (DECL_SECTION_NAME (t
));
361 /* Fix up fields of a decl_non_common T. */
364 lto_ft_decl_non_common (tree t
)
366 lto_ft_decl_with_vis (t
);
367 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t
));
368 LTO_FIXUP_TREE (DECL_RESULT_FLD (t
));
369 LTO_FIXUP_TREE (DECL_VINDEX (t
));
370 /* The C frontends may create exact duplicates for DECL_ORIGINAL_TYPE
371 like for 'typedef enum foo foo'. We have no way of avoiding to
372 merge them and dwarf2out.c cannot deal with this,
373 so fix this up by clearing DECL_ORIGINAL_TYPE in this case. */
374 if (TREE_CODE (t
) == TYPE_DECL
375 && DECL_ORIGINAL_TYPE (t
) == TREE_TYPE (t
))
376 DECL_ORIGINAL_TYPE (t
) = NULL_TREE
;
379 /* Fix up fields of a decl_non_common T. */
382 lto_ft_function (tree t
)
384 lto_ft_decl_non_common (t
);
385 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t
));
388 /* Fix up fields of a field_decl T. */
391 lto_ft_field_decl (tree t
)
393 lto_ft_decl_common (t
);
394 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t
));
395 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t
));
396 LTO_FIXUP_TREE (DECL_QUALIFIER (t
));
397 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t
));
398 LTO_FIXUP_TREE (DECL_FCONTEXT (t
));
401 /* Fix up fields of a type T. */
407 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t
));
408 LTO_FIXUP_TREE (TYPE_SIZE (t
));
409 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t
));
410 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t
));
411 LTO_FIXUP_TREE (TYPE_NAME (t
));
413 /* Accessors are for derived node types only. */
414 if (!POINTER_TYPE_P (t
))
415 LTO_FIXUP_TREE (TYPE_MINVAL (t
));
416 LTO_FIXUP_TREE (TYPE_MAXVAL (t
));
418 /* Accessor is for derived node types only. */
419 LTO_FIXUP_TREE (t
->type_non_common
.binfo
);
421 LTO_FIXUP_TREE (TYPE_CONTEXT (t
));
424 /* Fix up fields of a BINFO T. */
427 lto_ft_binfo (tree t
)
429 unsigned HOST_WIDE_INT i
, n
;
430 tree base
, saved_base
;
433 LTO_FIXUP_TREE (BINFO_VTABLE (t
));
434 LTO_FIXUP_TREE (BINFO_OFFSET (t
));
435 LTO_FIXUP_TREE (BINFO_VIRTUALS (t
));
436 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t
));
437 n
= VEC_length (tree
, BINFO_BASE_ACCESSES (t
));
438 for (i
= 0; i
< n
; i
++)
440 saved_base
= base
= BINFO_BASE_ACCESS (t
, i
);
441 LTO_FIXUP_TREE (base
);
442 if (base
!= saved_base
)
443 VEC_replace (tree
, BINFO_BASE_ACCESSES (t
), i
, base
);
445 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t
));
446 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t
));
447 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t
));
448 n
= BINFO_N_BASE_BINFOS (t
);
449 for (i
= 0; i
< n
; i
++)
451 saved_base
= base
= BINFO_BASE_BINFO (t
, i
);
452 LTO_FIXUP_TREE (base
);
453 if (base
!= saved_base
)
454 VEC_replace (tree
, BINFO_BASE_BINFOS (t
), i
, base
);
458 /* Fix up fields of a CONSTRUCTOR T. */
461 lto_ft_constructor (tree t
)
463 unsigned HOST_WIDE_INT idx
;
469 VEC_iterate(constructor_elt
, CONSTRUCTOR_ELTS (t
), idx
, ce
);
472 LTO_FIXUP_TREE (ce
->index
);
473 LTO_FIXUP_TREE (ce
->value
);
477 /* Fix up fields of an expression tree T. */
484 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
485 LTO_FIXUP_TREE (TREE_OPERAND (t
, i
));
488 /* Given a tree T fixup fields of T by replacing types with their merged
489 variant and other entities by an equal entity from an earlier compilation
490 unit, or an entity being canonical in a different way. This includes
491 for instance integer or string constants. */
494 lto_fixup_types (tree t
)
496 switch (TREE_CODE (t
))
498 case IDENTIFIER_NODE
:
502 LTO_FIXUP_TREE (TREE_VALUE (t
));
503 LTO_FIXUP_TREE (TREE_PURPOSE (t
));
504 LTO_FIXUP_TREE (TREE_CHAIN (t
));
508 lto_ft_field_decl (t
);
516 lto_ft_decl_common (t
);
520 lto_ft_decl_with_vis (t
);
524 lto_ft_decl_non_common (t
);
535 case PLACEHOLDER_EXPR
:
540 case TRANSLATION_UNIT_DECL
:
541 case OPTIMIZATION_NODE
:
542 case TARGET_OPTION_NODE
:
548 else if (TREE_CODE (t
) == CONSTRUCTOR
)
549 lto_ft_constructor (t
);
550 else if (CONSTANT_CLASS_P (t
))
551 LTO_FIXUP_TREE (TREE_TYPE (t
));
558 remember_with_vars (t
);
564 /* Return the resolution for the decl with index INDEX from DATA_IN. */
566 static enum ld_plugin_symbol_resolution
567 get_resolution (struct data_in
*data_in
, unsigned index
)
569 if (data_in
->globals_resolution
)
571 ld_plugin_symbol_resolution_t ret
;
572 /* We can have references to not emitted functions in
573 DECL_FUNCTION_PERSONALITY at least. So we can and have
574 to indeed return LDPR_UNKNOWN in some cases. */
575 if (VEC_length (ld_plugin_symbol_resolution_t
,
576 data_in
->globals_resolution
) <= index
)
578 ret
= VEC_index (ld_plugin_symbol_resolution_t
,
579 data_in
->globals_resolution
,
584 /* Delay resolution finding until decl merging. */
589 /* Register DECL with the global symbol table and change its
590 name if necessary to avoid name clashes for static globals across
594 lto_register_var_decl_in_symtab (struct data_in
*data_in
, tree decl
)
598 /* Variable has file scope, not local. Need to ensure static variables
599 between different files don't clash unexpectedly. */
600 if (!TREE_PUBLIC (decl
)
601 && !((context
= decl_function_context (decl
))
602 && auto_var_in_fn_p (decl
, context
)))
604 /* ??? We normally pre-mangle names before we serialize them
605 out. Here, in lto1, we do not know the language, and
606 thus cannot do the mangling again. Instead, we just
607 append a suffix to the mangled name. The resulting name,
608 however, is not a properly-formed mangled name, and will
609 confuse any attempt to unmangle it. */
610 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
613 ASM_FORMAT_PRIVATE_NAME (label
, name
, DECL_UID (decl
));
614 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (label
));
615 rest_of_decl_compilation (decl
, 1, 0);
616 VEC_safe_push (tree
, gc
, lto_global_var_decls
, decl
);
619 /* If this variable has already been declared, queue the
620 declaration for merging. */
621 if (TREE_PUBLIC (decl
))
624 if (!streamer_tree_cache_lookup (data_in
->reader_cache
, decl
, &ix
))
626 lto_symtab_register_decl (decl
, get_resolution (data_in
, ix
),
632 /* Register DECL with the global symbol table and change its
633 name if necessary to avoid name clashes for static globals across
634 different files. DATA_IN contains descriptors and tables for the
638 lto_register_function_decl_in_symtab (struct data_in
*data_in
, tree decl
)
640 /* Need to ensure static entities between different files
641 don't clash unexpectedly. */
642 if (!TREE_PUBLIC (decl
))
644 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
645 may set the assembler name where it was previously empty. */
646 tree old_assembler_name
= decl
->decl_with_vis
.assembler_name
;
648 /* FIXME lto: We normally pre-mangle names before we serialize
649 them out. Here, in lto1, we do not know the language, and
650 thus cannot do the mangling again. Instead, we just append a
651 suffix to the mangled name. The resulting name, however, is
652 not a properly-formed mangled name, and will confuse any
653 attempt to unmangle it. */
654 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
657 ASM_FORMAT_PRIVATE_NAME (label
, name
, DECL_UID (decl
));
658 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (label
));
660 /* We may arrive here with the old assembler name not set
661 if the function body is not needed, e.g., it has been
662 inlined away and does not appear in the cgraph. */
663 if (old_assembler_name
)
665 tree new_assembler_name
= DECL_ASSEMBLER_NAME (decl
);
667 /* Make the original assembler name available for later use.
668 We may have used it to indicate the section within its
669 object file where the function body may be found.
670 FIXME lto: Find a better way to maintain the function decl
671 to body section mapping so we don't need this hack. */
672 lto_record_renamed_decl (data_in
->file_data
,
673 IDENTIFIER_POINTER (old_assembler_name
),
674 IDENTIFIER_POINTER (new_assembler_name
));
678 /* If this variable has already been declared, queue the
679 declaration for merging. */
680 if (TREE_PUBLIC (decl
) && !DECL_ABSTRACT (decl
))
683 if (!streamer_tree_cache_lookup (data_in
->reader_cache
, decl
, &ix
))
685 lto_symtab_register_decl (decl
, get_resolution (data_in
, ix
),
691 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
692 for one compilation unit) go over all trees starting at index FROM until the
693 end of the sequence and replace fields of those trees, and the trees
694 themself with their canonical variants as per gimple_register_type. */
697 uniquify_nodes (struct data_in
*data_in
, unsigned from
)
699 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
700 unsigned len
= VEC_length (tree
, cache
->nodes
);
703 /* Go backwards because children streamed for the first time come
704 as part of their parents, and hence are created after them. */
706 /* First register all the types in the cache. This makes sure to
707 have the original structure in the type cycles when registering
708 them and computing hashes. */
709 for (i
= len
; i
-- > from
;)
711 tree t
= VEC_index (tree
, cache
->nodes
, i
);
714 tree newt
= gimple_register_type (t
);
715 /* Mark non-prevailing types so we fix them up. No need
716 to reset that flag afterwards - nothing that refers
717 to those types is left and they are collected. */
719 TREE_VISITED (t
) = 1;
723 /* Second fixup all trees in the new cache entries. */
724 for (i
= len
; i
-- > from
;)
726 tree t
= VEC_index (tree
, cache
->nodes
, i
);
731 /* First fixup the fields of T. */
737 /* Now try to find a canonical variant of T itself. */
738 t
= GIMPLE_REGISTER_TYPE (t
);
742 /* The following re-creates proper variant lists while fixing up
743 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
744 variant list state before fixup is broken. */
747 /* Remove us from our main variant list if we are not the
749 if (TYPE_MAIN_VARIANT (t
) != t
)
751 tem
= TYPE_MAIN_VARIANT (t
);
752 while (tem
&& TYPE_NEXT_VARIANT (tem
) != t
)
753 tem
= TYPE_NEXT_VARIANT (tem
);
755 TYPE_NEXT_VARIANT (tem
) = TYPE_NEXT_VARIANT (t
);
756 TYPE_NEXT_VARIANT (t
) = NULL_TREE
;
759 /* Query our new main variant. */
760 mv
= GIMPLE_REGISTER_TYPE (TYPE_MAIN_VARIANT (t
));
762 /* If we were the variant leader and we get replaced ourselves drop
763 all variants from our list. */
764 if (TYPE_MAIN_VARIANT (t
) == t
770 tree tem2
= TYPE_NEXT_VARIANT (tem
);
771 TYPE_NEXT_VARIANT (tem
) = NULL_TREE
;
776 /* If we are not our own variant leader link us into our new leaders
780 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
781 TYPE_NEXT_VARIANT (mv
) = t
;
782 if (RECORD_OR_UNION_TYPE_P (t
))
783 TYPE_BINFO (t
) = TYPE_BINFO (mv
);
784 /* Preserve the invariant that type variants share their
786 if (RECORD_OR_UNION_TYPE_P (t
)
787 && TYPE_FIELDS (mv
) != TYPE_FIELDS (t
))
790 for (f1
= TYPE_FIELDS (mv
), f2
= TYPE_FIELDS (t
);
791 f1
&& f2
; f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
795 && DECL_NAME (f1
) == DECL_NAME (f2
));
796 if (!streamer_tree_cache_lookup (cache
, f2
, &ix
))
798 /* If we're going to replace an element which we'd
799 still visit in the next iterations, we wouldn't
800 handle it, so do it here. We do have to handle it
801 even though the field_decl itself will be removed,
802 as it could refer to e.g. integer_cst which we
803 wouldn't reach via any other way, hence they
804 (and their type) would stay uncollected. */
805 /* ??? We should rather make sure to replace all
806 references to f2 with f1. That means handling
807 COMPONENT_REFs and CONSTRUCTOR elements in
808 lto_fixup_types and special-case the field-decl
810 /* ??? Not sure the above is all relevant in this
811 path canonicalizing TYPE_FIELDS to that of the
814 lto_fixup_types (f2
);
815 streamer_tree_cache_insert_at (cache
, f1
, ix
);
817 TYPE_FIELDS (t
) = TYPE_FIELDS (mv
);
821 /* Finally adjust our main variant and fix it up. */
822 TYPE_MAIN_VARIANT (t
) = mv
;
824 /* The following reconstructs the pointer chains
825 of the new pointed-to type if we are a main variant. We do
826 not stream those so they are broken before fixup. */
827 if (TREE_CODE (t
) == POINTER_TYPE
828 && TYPE_MAIN_VARIANT (t
) == t
)
830 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
831 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
833 else if (TREE_CODE (t
) == REFERENCE_TYPE
834 && TYPE_MAIN_VARIANT (t
) == t
)
836 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
837 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
843 if (RECORD_OR_UNION_TYPE_P (t
))
846 if (TYPE_FIELDS (t
) != TYPE_FIELDS (oldt
))
847 for (f1
= TYPE_FIELDS (t
), f2
= TYPE_FIELDS (oldt
);
848 f1
&& f2
; f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
851 gcc_assert (f1
!= f2
&& DECL_NAME (f1
) == DECL_NAME (f2
));
852 if (!streamer_tree_cache_lookup (cache
, f2
, &ix
))
854 /* If we're going to replace an element which we'd
855 still visit in the next iterations, we wouldn't
856 handle it, so do it here. We do have to handle it
857 even though the field_decl itself will be removed,
858 as it could refer to e.g. integer_cst which we
859 wouldn't reach via any other way, hence they
860 (and their type) would stay uncollected. */
861 /* ??? We should rather make sure to replace all
862 references to f2 with f1. That means handling
863 COMPONENT_REFs and CONSTRUCTOR elements in
864 lto_fixup_types and special-case the field-decl
867 lto_fixup_types (f2
);
868 streamer_tree_cache_insert_at (cache
, f1
, ix
);
872 /* If we found a tree that is equal to oldt replace it in the
873 cache, so that further users (in the various LTO sections)
875 streamer_tree_cache_insert_at (cache
, t
, i
);
879 /* Finally compute the canonical type of all TREE_TYPEs and register
880 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
881 From this point there are no longer any types with
882 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
883 This step requires the TYPE_POINTER_TO lists being present, so
884 make sure it is done last. */
885 for (i
= len
; i
-- > from
;)
887 tree t
= VEC_index (tree
, cache
->nodes
, i
);
891 if (TREE_CODE (t
) == VAR_DECL
)
892 lto_register_var_decl_in_symtab (data_in
, t
);
893 else if (TREE_CODE (t
) == FUNCTION_DECL
&& !DECL_BUILT_IN (t
))
894 lto_register_function_decl_in_symtab (data_in
, t
);
896 && TREE_CODE (t
) == TYPE_DECL
)
897 debug_hooks
->type_decl (t
, !DECL_FILE_SCOPE_P (t
));
898 else if (TYPE_P (t
) && !TYPE_CANONICAL (t
))
899 TYPE_CANONICAL (t
) = gimple_register_canonical_type (t
);
904 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
905 RESOLUTIONS is the set of symbols picked by the linker (read from the
906 resolution file when the linker plugin is being used). */
909 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
910 VEC(ld_plugin_symbol_resolution_t
,heap
) *resolutions
)
912 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
913 const int decl_offset
= sizeof (struct lto_decl_header
);
914 const int main_offset
= decl_offset
+ header
->decl_state_size
;
915 const int string_offset
= main_offset
+ header
->main_size
;
916 struct lto_input_block ib_main
;
917 struct data_in
*data_in
;
919 const uint32_t *data_ptr
, *data_end
;
920 uint32_t num_decl_states
;
922 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
925 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
926 header
->string_size
, resolutions
);
928 /* We do not uniquify the pre-loaded cache entries, those are middle-end
929 internal types that should not be merged. */
931 /* Read the global declarations and types. */
932 while (ib_main
.p
< ib_main
.len
)
935 unsigned from
= VEC_length (tree
, data_in
->reader_cache
->nodes
);
936 t
= stream_read_tree (&ib_main
, data_in
);
937 gcc_assert (t
&& ib_main
.p
<= ib_main
.len
);
938 uniquify_nodes (data_in
, from
);
941 /* Read in lto_in_decl_state objects. */
942 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
944 (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
945 num_decl_states
= *data_ptr
++;
947 gcc_assert (num_decl_states
> 0);
948 decl_data
->global_decl_state
= lto_new_in_decl_state ();
949 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
950 decl_data
->global_decl_state
);
952 /* Read in per-function decl states and enter them in hash table. */
953 decl_data
->function_decl_states
=
954 htab_create_ggc (37, lto_hash_in_decl_state
, lto_eq_in_decl_state
, NULL
);
956 for (i
= 1; i
< num_decl_states
; i
++)
958 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
961 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
962 slot
= htab_find_slot (decl_data
->function_decl_states
, state
, INSERT
);
963 gcc_assert (*slot
== NULL
);
967 if (data_ptr
!= data_end
)
968 internal_error ("bytecode stream: garbage at the end of symbols section");
970 /* Set the current decl state to be the global state. */
971 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
973 lto_data_in_delete (data_in
);
976 /* Custom version of strtoll, which is not portable. */
978 static HOST_WIDEST_INT
979 lto_parse_hex (const char *p
)
981 HOST_WIDEST_INT ret
= 0;
983 for (; *p
!= '\0'; ++p
)
988 if (c
>= '0' && c
<= '9')
990 else if (c
>= 'a' && c
<= 'f')
992 else if (c
>= 'A' && c
<= 'F')
995 internal_error ("could not parse hex number");
1002 /* Read resolution for file named FILE_NAME. The resolution is read from
1006 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
1008 /* We require that objects in the resolution file are in the same
1009 order as the lto1 command line. */
1010 unsigned int name_len
;
1012 unsigned int num_symbols
;
1014 struct lto_file_decl_data
*file_data
;
1015 splay_tree_node nd
= NULL
;
1020 name_len
= strlen (file
->filename
);
1021 obj_name
= XNEWVEC (char, name_len
+ 1);
1022 fscanf (resolution
, " "); /* Read white space. */
1024 fread (obj_name
, sizeof (char), name_len
, resolution
);
1025 obj_name
[name_len
] = '\0';
1026 if (filename_cmp (obj_name
, file
->filename
) != 0)
1027 internal_error ("unexpected file name %s in linker resolution file. "
1028 "Expected %s", obj_name
, file
->filename
);
1029 if (file
->offset
!= 0)
1033 HOST_WIDEST_INT offset
;
1034 t
= fscanf (resolution
, "@0x%16s", offset_p
);
1036 internal_error ("could not parse file offset");
1037 offset
= lto_parse_hex (offset_p
);
1038 if (offset
!= file
->offset
)
1039 internal_error ("unexpected offset");
1044 fscanf (resolution
, "%u", &num_symbols
);
1046 for (i
= 0; i
< num_symbols
; i
++)
1050 unsigned HOST_WIDE_INT id
;
1052 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
1054 unsigned int lto_resolution_str_len
=
1055 sizeof (lto_resolution_str
) / sizeof (char *);
1058 t
= fscanf (resolution
, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
" %26s %*[^\n]\n",
1059 &index
, &id
, r_str
);
1061 internal_error ("invalid line in the resolution file");
1063 for (j
= 0; j
< lto_resolution_str_len
; j
++)
1065 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
1067 r
= (enum ld_plugin_symbol_resolution
) j
;
1071 if (j
== lto_resolution_str_len
)
1072 internal_error ("invalid resolution in the resolution file");
1074 if (!(nd
&& lto_splay_tree_id_equal_p (nd
->key
, id
)))
1076 nd
= lto_splay_tree_lookup (file_ids
, id
);
1078 internal_error ("resolution sub id " HOST_WIDE_INT_PRINT_HEX_PURE
1079 " not in object file", id
);
1082 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1083 /* The indexes are very sparse. To save memory save them in a compact
1084 format that is only unpacked later when the subfile is processed. */
1087 VEC_safe_push (res_pair
, heap
, file_data
->respairs
, rp
);
1088 if (file_data
->max_index
< index
)
1089 file_data
->max_index
= index
;
1093 /* List of file_decl_datas */
1094 struct file_data_list
1096 struct lto_file_decl_data
*first
, *last
;
1099 /* Is the name for a id'ed LTO section? */
1102 lto_section_with_id (const char *name
, unsigned HOST_WIDE_INT
*id
)
1106 if (strncmp (name
, LTO_SECTION_NAME_PREFIX
, strlen (LTO_SECTION_NAME_PREFIX
)))
1108 s
= strrchr (name
, '.');
1109 return s
&& sscanf (s
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, id
) == 1;
1112 /* Create file_data of each sub file id */
1115 create_subid_section_table (struct lto_section_slot
*ls
, splay_tree file_ids
,
1116 struct file_data_list
*list
)
1118 struct lto_section_slot s_slot
, *new_slot
;
1119 unsigned HOST_WIDE_INT id
;
1123 struct lto_file_decl_data
*file_data
;
1125 if (!lto_section_with_id (ls
->name
, &id
))
1128 /* Find hash table of sub module id */
1129 nd
= lto_splay_tree_lookup (file_ids
, id
);
1132 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1136 file_data
= ggc_alloc_lto_file_decl_data ();
1137 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
1139 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();;
1140 lto_splay_tree_insert (file_ids
, id
, file_data
);
1142 /* Maintain list in linker order */
1144 list
->first
= file_data
;
1146 list
->last
->next
= file_data
;
1147 list
->last
= file_data
;
1150 /* Copy section into sub module hash table */
1151 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
1152 s_slot
.name
= new_name
;
1153 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
1154 gcc_assert (*hash_slot
== NULL
);
1156 new_slot
= XDUP (struct lto_section_slot
, ls
);
1157 new_slot
->name
= new_name
;
1158 *hash_slot
= new_slot
;
1162 /* Read declarations and other initializations for a FILE_DATA. */
1165 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
)
1169 VEC(ld_plugin_symbol_resolution_t
,heap
) *resolutions
= NULL
;
1173 /* Create vector for fast access of resolution. We do this lazily
1175 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t
, heap
,
1177 file_data
->max_index
+ 1);
1178 for (i
= 0; VEC_iterate (res_pair
, file_data
->respairs
, i
, rp
); i
++)
1179 VEC_replace (ld_plugin_symbol_resolution_t
, resolutions
, rp
->index
, rp
->res
);
1180 VEC_free (res_pair
, heap
, file_data
->respairs
);
1182 file_data
->renaming_hash_table
= lto_create_renaming_table ();
1183 file_data
->file_name
= file
->filename
;
1184 data
= lto_get_section_data (file_data
, LTO_section_decls
, NULL
, &len
);
1187 internal_error ("cannot read LTO decls from %s", file_data
->file_name
);
1190 /* Frees resolutions */
1191 lto_read_decls (file_data
, data
, resolutions
);
1192 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
1195 /* Finalize FILE_DATA in FILE and increase COUNT. */
1198 lto_create_files_from_ids (lto_file
*file
, struct lto_file_decl_data
*file_data
,
1201 lto_file_finalize (file_data
, file
);
1202 if (cgraph_dump_file
)
1203 fprintf (cgraph_dump_file
, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX
"\n",
1204 file_data
->file_name
, file_data
->id
);
1209 /* Generate a TREE representation for all types and external decls
1212 Read all of the globals out of the file. Then read the cgraph
1213 and process the .o index into the cgraph nodes so that it can open
1214 the .o file to load the functions and ipa information. */
1216 static struct lto_file_decl_data
*
1217 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
1219 struct lto_file_decl_data
*file_data
= NULL
;
1220 splay_tree file_ids
;
1221 htab_t section_hash_table
;
1222 struct lto_section_slot
*section
;
1223 struct file_data_list file_list
;
1224 struct lto_section_list section_list
;
1226 memset (§ion_list
, 0, sizeof (struct lto_section_list
));
1227 section_hash_table
= lto_obj_build_section_table (file
, §ion_list
);
1229 /* Find all sub modules in the object and put their sections into new hash
1230 tables in a splay tree. */
1231 file_ids
= lto_splay_tree_new ();
1232 memset (&file_list
, 0, sizeof (struct file_data_list
));
1233 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
1234 create_subid_section_table (section
, file_ids
, &file_list
);
1236 /* Add resolutions to file ids */
1237 lto_resolution_read (file_ids
, resolution_file
, file
);
1239 /* Finalize each lto file for each submodule in the merged object */
1240 for (file_data
= file_list
.first
; file_data
!= NULL
; file_data
= file_data
->next
)
1241 lto_create_files_from_ids (file
, file_data
, count
);
1243 splay_tree_delete (file_ids
);
1244 htab_delete (section_hash_table
);
1246 return file_list
.first
;
1249 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
1250 #define LTO_MMAP_IO 1
1254 /* Page size of machine is used for mmap and munmap calls. */
1255 static size_t page_mask
;
1258 /* Get the section data of length LEN from FILENAME starting at
1259 OFFSET. The data segment must be freed by the caller when the
1260 caller is finished. Returns NULL if all was not well. */
1263 lto_read_section_data (struct lto_file_decl_data
*file_data
,
1264 intptr_t offset
, size_t len
)
1268 static char *fd_name
;
1270 intptr_t computed_len
;
1271 intptr_t computed_offset
;
1275 /* Keep a single-entry file-descriptor cache. The last file we
1276 touched will get closed at exit.
1277 ??? Eventually we want to add a more sophisticated larger cache
1278 or rather fix function body streaming to not stream them in
1279 practically random order. */
1281 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
1289 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
1292 fatal_error ("Cannot open %s", file_data
->file_name
);
1295 fd_name
= xstrdup (file_data
->file_name
);
1301 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
1302 page_mask
= ~(page_size
- 1);
1305 computed_offset
= offset
& page_mask
;
1306 diff
= offset
- computed_offset
;
1307 computed_len
= len
+ diff
;
1309 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
1310 fd
, computed_offset
);
1311 if (result
== MAP_FAILED
)
1313 fatal_error ("Cannot map %s", file_data
->file_name
);
1317 return result
+ diff
;
1319 result
= (char *) xmalloc (len
);
1320 if (lseek (fd
, offset
, SEEK_SET
) != offset
1321 || read (fd
, result
, len
) != (ssize_t
) len
)
1324 fatal_error ("Cannot read %s", file_data
->file_name
);
1328 /* Native windows doesn't supports delayed unlink on opened file. So
1329 we close file here again. This produces higher I/O load, but at least
1330 it prevents to have dangling file handles preventing unlink. */
1341 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1342 NAME will be NULL unless the section type is for a function
1346 get_section_data (struct lto_file_decl_data
*file_data
,
1347 enum lto_section_type section_type
,
1351 htab_t section_hash_table
= file_data
->section_hash_table
;
1352 struct lto_section_slot
*f_slot
;
1353 struct lto_section_slot s_slot
;
1354 const char *section_name
= lto_get_section_name (section_type
, name
, file_data
);
1358 s_slot
.name
= section_name
;
1359 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
1362 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
1366 free (CONST_CAST (char *, section_name
));
1371 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1372 starts at OFFSET and has LEN bytes. */
1375 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
1376 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
1377 const char *name ATTRIBUTE_UNUSED
,
1378 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
1381 intptr_t computed_len
;
1382 intptr_t computed_offset
;
1387 computed_offset
= ((intptr_t) offset
) & page_mask
;
1388 diff
= (intptr_t) offset
- computed_offset
;
1389 computed_len
= len
+ diff
;
1391 munmap ((caddr_t
) computed_offset
, computed_len
);
1393 free (CONST_CAST(char *, offset
));
1397 static lto_file
*current_lto_file
;
1399 /* Helper for qsort; compare partitions and return one with smaller size.
1400 We sort from greatest to smallest so parallel build doesn't stale on the
1401 longest compilation being executed too late. */
1404 cmp_partitions_size (const void *a
, const void *b
)
1406 const struct ltrans_partition_def
*pa
1407 = *(struct ltrans_partition_def
*const *)a
;
1408 const struct ltrans_partition_def
*pb
1409 = *(struct ltrans_partition_def
*const *)b
;
1410 return pb
->insns
- pa
->insns
;
1413 /* Helper for qsort; compare partitions and return one with smaller order. */
1416 cmp_partitions_order (const void *a
, const void *b
)
1418 const struct ltrans_partition_def
*pa
1419 = *(struct ltrans_partition_def
*const *)a
;
1420 const struct ltrans_partition_def
*pb
1421 = *(struct ltrans_partition_def
*const *)b
;
1422 int ordera
= -1, orderb
= -1;
1424 if (VEC_length (cgraph_node_ptr
, pa
->cgraph_set
->nodes
))
1425 ordera
= VEC_index (cgraph_node_ptr
, pa
->cgraph_set
->nodes
, 0)->symbol
.order
;
1426 else if (VEC_length (varpool_node_ptr
, pa
->varpool_set
->nodes
))
1427 ordera
= VEC_index (varpool_node_ptr
, pa
->varpool_set
->nodes
, 0)->symbol
.order
;
1428 if (VEC_length (cgraph_node_ptr
, pb
->cgraph_set
->nodes
))
1429 orderb
= VEC_index (cgraph_node_ptr
, pb
->cgraph_set
->nodes
, 0)->symbol
.order
;
1430 else if (VEC_length (varpool_node_ptr
, pb
->varpool_set
->nodes
))
1431 orderb
= VEC_index (varpool_node_ptr
, pb
->varpool_set
->nodes
, 0)->symbol
.order
;
1432 return orderb
- ordera
;
1435 /* Write all output files in WPA mode and the file with the list of
1439 lto_wpa_write_files (void)
1443 cgraph_node_set set
;
1444 varpool_node_set vset
;
1445 ltrans_partition part
;
1446 FILE *ltrans_output_list_stream
;
1447 char *temp_filename
;
1450 /* Open the LTRANS output list. */
1451 if (!ltrans_output_list
)
1452 fatal_error ("no LTRANS output list filename provided");
1453 ltrans_output_list_stream
= fopen (ltrans_output_list
, "w");
1454 if (ltrans_output_list_stream
== NULL
)
1455 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list
);
1457 timevar_push (TV_WHOPR_WPA
);
1459 FOR_EACH_VEC_ELT (ltrans_partition
, ltrans_partitions
, i
, part
)
1460 lto_stats
.num_output_cgraph_nodes
+= VEC_length (cgraph_node_ptr
,
1461 part
->cgraph_set
->nodes
);
1463 /* Find out statics that need to be promoted
1464 to globals with hidden visibility because they are accessed from multiple
1466 lto_promote_cross_file_statics ();
1468 timevar_pop (TV_WHOPR_WPA
);
1470 timevar_push (TV_WHOPR_WPA_IO
);
1472 /* Generate a prefix for the LTRANS unit files. */
1473 blen
= strlen (ltrans_output_list
);
1474 temp_filename
= (char *) xmalloc (blen
+ sizeof ("2147483648.o"));
1475 strcpy (temp_filename
, ltrans_output_list
);
1476 if (blen
> sizeof (".out")
1477 && strcmp (temp_filename
+ blen
- sizeof (".out") + 1,
1479 temp_filename
[blen
- sizeof (".out") + 1] = '\0';
1480 blen
= strlen (temp_filename
);
1482 n_sets
= VEC_length (ltrans_partition
, ltrans_partitions
);
1484 /* Sort partitions by size so small ones are compiled last.
1485 FIXME: Even when not reordering we may want to output one list for parallel make
1486 and other for final link command. */
1487 VEC_qsort (ltrans_partition
, ltrans_partitions
,
1488 flag_toplevel_reorder
? cmp_partitions_size
: cmp_partitions_order
);
1489 for (i
= 0; i
< n_sets
; i
++)
1492 ltrans_partition part
= VEC_index (ltrans_partition
, ltrans_partitions
, i
);
1494 set
= part
->cgraph_set
;
1495 vset
= part
->varpool_set
;
1497 /* Write all the nodes in SET. */
1498 sprintf (temp_filename
+ blen
, "%u.o", i
);
1499 file
= lto_obj_file_open (temp_filename
, true);
1501 fatal_error ("lto_obj_file_open() failed");
1504 fprintf (stderr
, " %s (%s %i insns)", temp_filename
, part
->name
, part
->insns
);
1505 if (cgraph_dump_file
)
1507 fprintf (cgraph_dump_file
, "Writing partition %s to file %s, %i insns\n",
1508 part
->name
, temp_filename
, part
->insns
);
1509 fprintf (cgraph_dump_file
, "cgraph nodes:");
1510 dump_cgraph_node_set (cgraph_dump_file
, set
);
1511 fprintf (cgraph_dump_file
, "varpool nodes:");
1512 dump_varpool_node_set (cgraph_dump_file
, vset
);
1514 gcc_checking_assert (cgraph_node_set_nonempty_p (set
)
1515 || varpool_node_set_nonempty_p (vset
) || !i
);
1517 lto_set_current_out_file (file
);
1519 ipa_write_optimization_summaries (set
, vset
);
1521 lto_set_current_out_file (NULL
);
1522 lto_obj_file_close (file
);
1524 len
= strlen (temp_filename
);
1525 if (fwrite (temp_filename
, 1, len
, ltrans_output_list_stream
) < len
1526 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
1527 fatal_error ("writing to LTRANS output list %s: %m",
1528 ltrans_output_list
);
1531 lto_stats
.num_output_files
+= n_sets
;
1533 /* Close the LTRANS output list. */
1534 if (fclose (ltrans_output_list_stream
))
1535 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list
);
1537 free_ltrans_partitions();
1539 timevar_pop (TV_WHOPR_WPA_IO
);
1543 /* If TT is a variable or function decl replace it with its
1544 prevailing variant. */
1545 #define LTO_SET_PREVAIL(tt) \
1547 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
1548 tt = lto_symtab_prevailing_decl (tt); \
1551 /* Ensure that TT isn't a replacable var of function decl. */
1552 #define LTO_NO_PREVAIL(tt) \
1553 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
1555 /* Given a tree T replace all fields referring to variables or functions
1556 with their prevailing variant. */
1558 lto_fixup_prevailing_decls (tree t
)
1560 enum tree_code code
= TREE_CODE (t
);
1561 LTO_NO_PREVAIL (TREE_TYPE (t
));
1562 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
1563 LTO_NO_PREVAIL (TREE_CHAIN (t
));
1566 LTO_NO_PREVAIL (DECL_NAME (t
));
1567 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
1568 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1570 LTO_SET_PREVAIL (DECL_SIZE (t
));
1571 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
1572 LTO_SET_PREVAIL (DECL_INITIAL (t
));
1573 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
1574 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
1576 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1578 LTO_NO_PREVAIL (t
->decl_with_vis
.assembler_name
);
1579 LTO_NO_PREVAIL (DECL_SECTION_NAME (t
));
1581 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
1583 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t
));
1584 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
1585 LTO_NO_PREVAIL (DECL_VINDEX (t
));
1587 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1588 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
1589 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
1591 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t
));
1592 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
1593 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
1594 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
1595 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
1598 else if (TYPE_P (t
))
1600 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
1601 LTO_SET_PREVAIL (TYPE_SIZE (t
));
1602 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
1603 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
1604 LTO_NO_PREVAIL (TYPE_NAME (t
));
1606 LTO_SET_PREVAIL (TYPE_MINVAL (t
));
1607 LTO_SET_PREVAIL (TYPE_MAXVAL (t
));
1608 LTO_SET_PREVAIL (t
->type_non_common
.binfo
);
1610 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
1612 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
1613 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
1614 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
1616 else if (EXPR_P (t
))
1619 LTO_NO_PREVAIL (t
->exp
.block
);
1620 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
1621 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
1628 LTO_SET_PREVAIL (TREE_VALUE (t
));
1629 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
1636 #undef LTO_SET_PREVAIL
1637 #undef LTO_NO_PREVAIL
1639 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1640 replaces var and function decls with the corresponding prevailing def. */
1643 lto_fixup_state (struct lto_in_decl_state
*state
)
1646 struct lto_tree_ref_table
*table
;
1648 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1649 we still need to walk from all DECLs to find the reachable
1650 FUNCTION_DECLs and VAR_DECLs. */
1651 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
1653 table
= &state
->streams
[si
];
1654 for (i
= 0; i
< table
->size
; i
++)
1656 tree
*tp
= table
->trees
+ i
;
1657 if (VAR_OR_FUNCTION_DECL_P (*tp
))
1658 *tp
= lto_symtab_prevailing_decl (*tp
);
1663 /* A callback of htab_traverse. Just extracts a state from SLOT
1664 and calls lto_fixup_state. */
1667 lto_fixup_state_aux (void **slot
, void *aux ATTRIBUTE_UNUSED
)
1669 struct lto_in_decl_state
*state
= (struct lto_in_decl_state
*) *slot
;
1670 lto_fixup_state (state
);
1674 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1678 lto_fixup_decls (struct lto_file_decl_data
**files
)
1684 FOR_EACH_HTAB_ELEMENT (tree_with_vars
, t
, tree
, hi
)
1685 lto_fixup_prevailing_decls (t
);
1687 for (i
= 0; files
[i
]; i
++)
1689 struct lto_file_decl_data
*file
= files
[i
];
1690 struct lto_in_decl_state
*state
= file
->global_decl_state
;
1691 lto_fixup_state (state
);
1693 htab_traverse (file
->function_decl_states
, lto_fixup_state_aux
, NULL
);
1697 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
1699 /* Turn file datas for sub files into a single array, so that they look
1700 like separate files for further passes. */
1703 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
, int last_file_ix
)
1705 struct lto_file_decl_data
*n
, *next
;
1708 lto_stats
.num_input_files
= count
;
1710 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count
+ 1);
1711 /* Set the hooks so that all of the ipa passes can read in their data. */
1712 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
1713 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
1715 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
1717 all_file_decl_data
[k
++] = n
;
1722 all_file_decl_data
[k
] = NULL
;
1723 gcc_assert (k
== count
);
1726 /* Input file data before flattening (i.e. splitting them to subfiles to support
1727 incremental linking. */
1728 static int real_file_count
;
1729 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
1731 /* Read all the symbols from the input files FNAMES. NFILES is the
1732 number of files requested in the command line. Instantiate a
1733 global call graph by aggregating all the sub-graphs found in each
1737 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
1739 unsigned int i
, last_file_ix
;
1741 struct cgraph_node
*node
;
1743 struct lto_file_decl_data
**decl_data
;
1747 timevar_push (TV_IPA_LTO_DECL_IN
);
1750 = decl_data
= ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles
+ 1);
1751 real_file_count
= nfiles
;
1753 /* Read the resolution file. */
1755 if (resolution_file_name
)
1758 unsigned num_objects
;
1760 resolution
= fopen (resolution_file_name
, "r");
1761 if (resolution
== NULL
)
1762 fatal_error ("could not open symbol resolution file: %m");
1764 t
= fscanf (resolution
, "%u", &num_objects
);
1765 gcc_assert (t
== 1);
1767 /* True, since the plugin splits the archives. */
1768 gcc_assert (num_objects
== nfiles
);
1771 tree_with_vars
= htab_create_ggc (101, htab_hash_pointer
, htab_eq_pointer
,
1775 fprintf (stderr
, "Reading object files:");
1777 /* Read all of the object files specified on the command line. */
1778 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
1780 struct lto_file_decl_data
*file_data
= NULL
;
1783 fprintf (stderr
, " %s", fnames
[i
]);
1787 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
1788 if (!current_lto_file
)
1791 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
1794 lto_obj_file_close (current_lto_file
);
1795 current_lto_file
= NULL
;
1799 decl_data
[last_file_ix
++] = file_data
;
1801 lto_obj_file_close (current_lto_file
);
1802 current_lto_file
= NULL
;
1806 lto_flatten_files (decl_data
, count
, last_file_ix
);
1807 lto_stats
.num_input_files
= count
;
1808 ggc_free(decl_data
);
1809 real_file_decl_data
= NULL
;
1811 if (resolution_file_name
)
1812 fclose (resolution
);
1814 /* Set the hooks so that all of the ipa passes can read in their data. */
1815 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
1817 timevar_pop (TV_IPA_LTO_DECL_IN
);
1820 fprintf (stderr
, "\nReading the callgraph\n");
1822 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
1823 /* Read the symtab. */
1825 timevar_pop (TV_IPA_LTO_CGRAPH_IO
);
1828 fprintf (stderr
, "Merging declarations\n");
1830 timevar_push (TV_IPA_LTO_DECL_MERGE
);
1831 /* Merge global decls. */
1832 lto_symtab_merge_decls ();
1834 /* If there were errors during symbol merging bail out, we have no
1835 good way to recover here. */
1837 fatal_error ("errors during merging of translation units");
1839 /* Fixup all decls and types and free the type hash tables. */
1840 lto_fixup_decls (all_file_decl_data
);
1841 htab_delete (tree_with_vars
);
1842 tree_with_vars
= NULL
;
1843 free_gimple_type_tables ();
1846 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
1847 /* Each pass will set the appropriate timer. */
1850 fprintf (stderr
, "Reading summaries\n");
1852 /* Read the IPA summary data. */
1854 ipa_read_optimization_summaries ();
1856 ipa_read_summaries ();
1858 /* Finally merge the cgraph according to the decl merging decisions. */
1859 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
1860 if (cgraph_dump_file
)
1862 fprintf (cgraph_dump_file
, "Before merging:\n");
1863 dump_cgraph (cgraph_dump_file
);
1864 dump_varpool (cgraph_dump_file
);
1866 lto_symtab_merge_cgraph_nodes ();
1869 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1870 summaries computed and needs to apply changes. At the moment WHOPR only
1871 supports inlining, so we can push it here by hand. In future we need to stream
1872 this field into ltrans compilation. */
1874 FOR_EACH_DEFINED_FUNCTION (node
)
1875 VEC_safe_push (ipa_opt_pass
, heap
,
1876 node
->ipa_transforms_to_apply
,
1877 (ipa_opt_pass
)&pass_ipa_inline
);
1880 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
1882 timevar_push (TV_IPA_LTO_DECL_INIT_IO
);
1884 /* Indicate that the cgraph is built and ready. */
1885 cgraph_function_flags_ready
= true;
1887 timevar_pop (TV_IPA_LTO_DECL_INIT_IO
);
1888 ggc_free (all_file_decl_data
);
1889 all_file_decl_data
= NULL
;
1893 /* Materialize all the bodies for all the nodes in the callgraph. */
1896 materialize_cgraph (void)
1899 struct cgraph_node
*node
;
1901 timevar_id_t lto_timer
;
1905 flag_wpa
? "Materializing decls:" : "Reading function bodies:");
1908 /* Now that we have input the cgraph, we need to clear all of the aux
1909 nodes and read the functions if we are not running in WPA mode. */
1910 timevar_push (TV_IPA_LTO_GIMPLE_IN
);
1912 FOR_EACH_FUNCTION (node
)
1914 if (node
->symbol
.lto_file_data
)
1916 lto_materialize_function (node
);
1917 lto_stats
.num_input_cgraph_nodes
++;
1921 timevar_pop (TV_IPA_LTO_GIMPLE_IN
);
1923 /* Start the appropriate timer depending on the mode that we are
1925 lto_timer
= (flag_wpa
) ? TV_WHOPR_WPA
1926 : (flag_ltrans
) ? TV_WHOPR_LTRANS
1928 timevar_push (lto_timer
);
1930 current_function_decl
= NULL
;
1933 /* Inform the middle end about the global variables we have seen. */
1934 FOR_EACH_VEC_ELT (tree
, lto_global_var_decls
, i
, decl
)
1935 rest_of_decl_compilation (decl
, 1, 0);
1938 fprintf (stderr
, "\n");
1940 timevar_pop (lto_timer
);
1944 /* Perform whole program analysis (WPA) on the callgraph and write out the
1945 optimization plan. */
1948 do_whole_program_analysis (void)
1950 timevar_start (TV_PHASE_OPT_GEN
);
1952 /* Note that since we are in WPA mode, materialize_cgraph will not
1953 actually read in all the function bodies. It only materializes
1954 the decls and cgraph nodes so that analysis can be performed. */
1955 materialize_cgraph ();
1957 /* Reading in the cgraph uses different timers, start timing WPA now. */
1958 timevar_push (TV_WHOPR_WPA
);
1960 if (pre_ipa_mem_report
)
1962 fprintf (stderr
, "Memory consumption before IPA\n");
1963 dump_memory_report (false);
1966 cgraph_function_flags_ready
= true;
1968 if (cgraph_dump_file
)
1970 dump_cgraph (cgraph_dump_file
);
1971 dump_varpool (cgraph_dump_file
);
1973 bitmap_obstack_initialize (NULL
);
1974 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
1976 execute_ipa_pass_list (all_regular_ipa_passes
);
1978 if (cgraph_dump_file
)
1980 fprintf (cgraph_dump_file
, "Optimized ");
1981 dump_cgraph (cgraph_dump_file
);
1982 dump_varpool (cgraph_dump_file
);
1985 bitmap_obstack_release (NULL
);
1987 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1988 timevar_pop (TV_WHOPR_WPA
);
1990 if (flag_lto_partition_1to1
)
1993 lto_balanced_map ();
1995 timevar_stop (TV_PHASE_OPT_GEN
);
1996 timevar_start (TV_PHASE_STREAM_OUT
);
2000 fprintf (stderr
, "\nStreaming out");
2003 lto_wpa_write_files ();
2005 fprintf (stderr
, "\n");
2007 timevar_stop (TV_PHASE_STREAM_OUT
);
2010 if (post_ipa_mem_report
)
2012 fprintf (stderr
, "Memory consumption after IPA\n");
2013 dump_memory_report (false);
2016 /* Show the LTO report before launching LTRANS. */
2017 if (flag_lto_report
)
2018 print_lto_report ();
2024 static GTY(()) tree lto_eh_personality_decl
;
2026 /* Return the LTO personality function decl. */
2029 lto_eh_personality (void)
2031 if (!lto_eh_personality_decl
)
2033 /* Use the first personality DECL for our personality if we don't
2034 support multiple ones. This ensures that we don't artificially
2035 create the need for them in a single-language program. */
2036 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
2037 lto_eh_personality_decl
= first_personality_decl
;
2039 lto_eh_personality_decl
= lhd_gcc_personality ();
2042 return lto_eh_personality_decl
;
2045 /* Set the process name based on the LTO mode. */
2048 lto_process_name (void)
2051 setproctitle ("lto1-lto");
2053 setproctitle ("lto1-wpa");
2055 setproctitle ("lto1-ltrans");
2059 /* Initialize the LTO front end. */
2064 lto_process_name ();
2065 lto_streamer_hooks_init ();
2067 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
2068 memset (<o_stats
, 0, sizeof (lto_stats
));
2069 bitmap_obstack_initialize (NULL
);
2070 gimple_register_cfg_hooks ();
2074 /* Main entry point for the GIMPLE front end. This front end has
2075 three main personalities:
2077 - LTO (-flto). All the object files on the command line are
2078 loaded in memory and processed as a single translation unit.
2079 This is the traditional link-time optimization behavior.
2081 - WPA (-fwpa). Only the callgraph and summary information for
2082 files in the command file are loaded. A single callgraph
2083 (without function bodies) is instantiated for the whole set of
2084 files. IPA passes are only allowed to analyze the call graph
2085 and make transformation decisions. The callgraph is
2086 partitioned, each partition is written to a new object file
2087 together with the transformation decisions.
2089 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2090 summary files from running again. Since WPA computed summary
2091 information and decided what transformations to apply, LTRANS
2092 simply applies them. */
2097 /* LTO is called as a front end, even though it is not a front end.
2098 Because it is called as a front end, TV_PHASE_PARSING and
2099 TV_PARSE_GLOBAL are active, and we need to turn them off while
2100 doing LTO. Later we turn them back on so they are active up in
2102 timevar_pop (TV_PARSE_GLOBAL
);
2103 timevar_stop (TV_PHASE_PARSING
);
2105 timevar_start (TV_PHASE_SETUP
);
2107 /* Initialize the LTO front end. */
2110 timevar_stop (TV_PHASE_SETUP
);
2111 timevar_start (TV_PHASE_STREAM_IN
);
2113 /* Read all the symbols and call graph from all the files in the
2115 read_cgraph_and_symbols (num_in_fnames
, in_fnames
);
2117 timevar_stop (TV_PHASE_STREAM_IN
);
2121 /* If WPA is enabled analyze the whole call graph and create an
2122 optimization plan. Otherwise, read in all the function
2123 bodies and continue with optimization. */
2125 do_whole_program_analysis ();
2128 timevar_start (TV_PHASE_OPT_GEN
);
2130 materialize_cgraph ();
2132 /* Let the middle end know that we have read and merged all of
2136 timevar_stop (TV_PHASE_OPT_GEN
);
2138 /* FIXME lto, if the processes spawned by WPA fail, we miss
2139 the chance to print WPA's report, so WPA will call
2140 print_lto_report before launching LTRANS. If LTRANS was
2141 launched directly by the driver we would not need to do
2143 if (flag_lto_report
)
2144 print_lto_report ();
2148 /* Here we make LTO pretend to be a parser. */
2149 timevar_start (TV_PHASE_PARSING
);
2150 timevar_push (TV_PARSE_GLOBAL
);
2153 #include "gt-lto-lto.h"