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"
45 #include "lto-streamer.h"
46 #include "tree-streamer.h"
47 #include "splay-tree.h"
48 #include "lto-partition.h"
50 static GTY(()) tree first_personality_decl
;
52 /* Returns a hash code for P. */
55 hash_name (const void *p
)
57 const struct lto_section_slot
*ds
= (const struct lto_section_slot
*) p
;
58 return (hashval_t
) htab_hash_string (ds
->name
);
62 /* Returns nonzero if P1 and P2 are equal. */
65 eq_name (const void *p1
, const void *p2
)
67 const struct lto_section_slot
*s1
=
68 (const struct lto_section_slot
*) p1
;
69 const struct lto_section_slot
*s2
=
70 (const struct lto_section_slot
*) p2
;
72 return strcmp (s1
->name
, s2
->name
) == 0;
75 /* Free lto_section_slot */
78 free_with_string (void *arg
)
80 struct lto_section_slot
*s
= (struct lto_section_slot
*)arg
;
82 free (CONST_CAST (char *, s
->name
));
86 /* Create section hash table */
89 lto_obj_create_section_hash_table (void)
91 return htab_create (37, hash_name
, eq_name
, free_with_string
);
94 /* Delete an allocated integer KEY in the splay tree. */
97 lto_splay_tree_delete_id (splay_tree_key key
)
102 /* Compare splay tree node ids A and B. */
105 lto_splay_tree_compare_ids (splay_tree_key a
, splay_tree_key b
)
107 unsigned HOST_WIDE_INT ai
;
108 unsigned HOST_WIDE_INT bi
;
110 ai
= *(unsigned HOST_WIDE_INT
*) a
;
111 bi
= *(unsigned HOST_WIDE_INT
*) b
;
120 /* Look up splay tree node by ID in splay tree T. */
122 static splay_tree_node
123 lto_splay_tree_lookup (splay_tree t
, unsigned HOST_WIDE_INT id
)
125 return splay_tree_lookup (t
, (splay_tree_key
) &id
);
128 /* Check if KEY has ID. */
131 lto_splay_tree_id_equal_p (splay_tree_key key
, unsigned HOST_WIDE_INT id
)
133 return *(unsigned HOST_WIDE_INT
*) key
== id
;
136 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
137 The ID is allocated separately because we need HOST_WIDE_INTs which may
138 be wider than a splay_tree_key. */
141 lto_splay_tree_insert (splay_tree t
, unsigned HOST_WIDE_INT id
,
142 struct lto_file_decl_data
*file_data
)
144 unsigned HOST_WIDE_INT
*idp
= XCNEW (unsigned HOST_WIDE_INT
);
146 splay_tree_insert (t
, (splay_tree_key
) idp
, (splay_tree_value
) file_data
);
149 /* Create a splay tree. */
152 lto_splay_tree_new (void)
154 return splay_tree_new (lto_splay_tree_compare_ids
,
155 lto_splay_tree_delete_id
,
159 /* Read the constructors and inits. */
162 lto_materialize_constructors_and_inits (struct lto_file_decl_data
* file_data
)
165 const char *data
= lto_get_section_data (file_data
,
166 LTO_section_static_initializer
,
168 lto_input_constructors_and_inits (file_data
, data
);
169 lto_free_section_data (file_data
, LTO_section_static_initializer
, NULL
,
173 /* Return true when NODE has a clone that is analyzed (i.e. we need
174 to load its body even if the node itself is not needed). */
177 has_analyzed_clone_p (struct cgraph_node
*node
)
179 struct cgraph_node
*orig
= node
;
188 else if (node
->next_sibling_clone
)
189 node
= node
->next_sibling_clone
;
192 while (node
!= orig
&& !node
->next_sibling_clone
)
193 node
= node
->clone_of
;
195 node
= node
->next_sibling_clone
;
201 /* Read the function body for the function associated with NODE. */
204 lto_materialize_function (struct cgraph_node
*node
)
207 struct lto_file_decl_data
*file_data
;
208 const char *data
, *name
;
211 decl
= node
->symbol
.decl
;
212 /* Read in functions with body (analyzed nodes)
213 and also functions that are needed to produce virtual clones. */
214 if (cgraph_function_with_gimple_body_p (node
) || has_analyzed_clone_p (node
))
216 /* Clones and thunks don't need to be read. */
220 /* Load the function body only if not operating in WPA mode. In
221 WPA mode, the body of the function is not needed. */
224 file_data
= node
->symbol
.lto_file_data
;
225 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
227 /* We may have renamed the declaration, e.g., a static function. */
228 name
= lto_get_decl_name_mapping (file_data
, name
);
230 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
233 fatal_error ("%s: section %s is missing",
234 file_data
->file_name
,
237 gcc_assert (DECL_STRUCT_FUNCTION (decl
) == NULL
);
239 allocate_struct_function (decl
, false);
240 announce_function (decl
);
241 lto_input_function_body (file_data
, decl
, data
);
242 if (DECL_FUNCTION_PERSONALITY (decl
) && !first_personality_decl
)
243 first_personality_decl
= DECL_FUNCTION_PERSONALITY (decl
);
244 lto_stats
.num_function_bodies
++;
245 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
251 /* Let the middle end know about the function. */
252 rest_of_decl_compilation (decl
, 1, 0);
256 /* Decode the content of memory pointed to by DATA in the in decl
257 state object STATE. DATA_IN points to a data_in structure for
258 decoding. Return the address after the decoded object in the
261 static const uint32_t *
262 lto_read_in_decl_state (struct data_in
*data_in
, const uint32_t *data
,
263 struct lto_in_decl_state
*state
)
270 decl
= streamer_tree_cache_get (data_in
->reader_cache
, ix
);
271 if (TREE_CODE (decl
) != FUNCTION_DECL
)
273 gcc_assert (decl
== void_type_node
);
276 state
->fn_decl
= decl
;
278 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
280 uint32_t size
= *data
++;
281 tree
*decls
= ggc_alloc_vec_tree (size
);
283 for (j
= 0; j
< size
; j
++)
284 decls
[j
] = streamer_tree_cache_get (data_in
->reader_cache
, data
[j
]);
286 state
->streams
[i
].size
= size
;
287 state
->streams
[i
].trees
= decls
;
294 /* A hashtable of trees that potentially refer to variables or functions
295 that must be replaced with their prevailing variant. */
296 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node
))) htab_t
299 /* Remember that T is a tree that (potentially) refers to a variable
300 or function decl that may be replaced with its prevailing variant. */
302 remember_with_vars (tree t
)
304 *(tree
*) htab_find_slot (tree_with_vars
, t
, INSERT
) = t
;
307 #define GIMPLE_REGISTER_TYPE(tt) \
308 (TREE_VISITED (tt) ? gimple_register_type (tt) : tt)
310 #define LTO_FIXUP_TREE(tt) \
316 (tt) = GIMPLE_REGISTER_TYPE (tt); \
317 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
318 remember_with_vars (t); \
322 static void lto_fixup_types (tree
);
324 /* Fix up fields of a tree_typed T. */
327 lto_ft_typed (tree t
)
329 LTO_FIXUP_TREE (TREE_TYPE (t
));
332 /* Fix up fields of a tree_common T. */
335 lto_ft_common (tree t
)
338 LTO_FIXUP_TREE (TREE_CHAIN (t
));
341 /* Fix up fields of a decl_minimal T. */
344 lto_ft_decl_minimal (tree t
)
347 LTO_FIXUP_TREE (DECL_NAME (t
));
348 LTO_FIXUP_TREE (DECL_CONTEXT (t
));
351 /* Fix up fields of a decl_common T. */
354 lto_ft_decl_common (tree t
)
356 lto_ft_decl_minimal (t
);
357 LTO_FIXUP_TREE (DECL_SIZE (t
));
358 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t
));
359 LTO_FIXUP_TREE (DECL_INITIAL (t
));
360 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t
));
361 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t
));
364 /* Fix up fields of a decl_with_vis T. */
367 lto_ft_decl_with_vis (tree t
)
369 lto_ft_decl_common (t
);
371 /* Accessor macro has side-effects, use field-name here. */
372 LTO_FIXUP_TREE (t
->decl_with_vis
.assembler_name
);
373 LTO_FIXUP_TREE (DECL_SECTION_NAME (t
));
376 /* Fix up fields of a decl_non_common T. */
379 lto_ft_decl_non_common (tree t
)
381 lto_ft_decl_with_vis (t
);
382 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t
));
383 LTO_FIXUP_TREE (DECL_RESULT_FLD (t
));
384 LTO_FIXUP_TREE (DECL_VINDEX (t
));
385 /* The C frontends may create exact duplicates for DECL_ORIGINAL_TYPE
386 like for 'typedef enum foo foo'. We have no way of avoiding to
387 merge them and dwarf2out.c cannot deal with this,
388 so fix this up by clearing DECL_ORIGINAL_TYPE in this case. */
389 if (TREE_CODE (t
) == TYPE_DECL
390 && DECL_ORIGINAL_TYPE (t
) == TREE_TYPE (t
))
391 DECL_ORIGINAL_TYPE (t
) = NULL_TREE
;
394 /* Fix up fields of a decl_non_common T. */
397 lto_ft_function (tree t
)
399 lto_ft_decl_non_common (t
);
400 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t
));
403 /* Fix up fields of a field_decl T. */
406 lto_ft_field_decl (tree t
)
408 lto_ft_decl_common (t
);
409 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t
));
410 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t
));
411 LTO_FIXUP_TREE (DECL_QUALIFIER (t
));
412 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t
));
413 LTO_FIXUP_TREE (DECL_FCONTEXT (t
));
416 /* Fix up fields of a type T. */
422 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t
));
423 LTO_FIXUP_TREE (TYPE_SIZE (t
));
424 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t
));
425 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t
));
426 LTO_FIXUP_TREE (TYPE_NAME (t
));
428 /* Accessors are for derived node types only. */
429 if (!POINTER_TYPE_P (t
))
430 LTO_FIXUP_TREE (TYPE_MINVAL (t
));
431 LTO_FIXUP_TREE (TYPE_MAXVAL (t
));
433 /* Accessor is for derived node types only. */
434 LTO_FIXUP_TREE (t
->type_non_common
.binfo
);
436 LTO_FIXUP_TREE (TYPE_CONTEXT (t
));
439 /* Fix up fields of a BINFO T. */
442 lto_ft_binfo (tree t
)
444 unsigned HOST_WIDE_INT i
, n
;
445 tree base
, saved_base
;
448 LTO_FIXUP_TREE (BINFO_VTABLE (t
));
449 LTO_FIXUP_TREE (BINFO_OFFSET (t
));
450 LTO_FIXUP_TREE (BINFO_VIRTUALS (t
));
451 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t
));
452 n
= VEC_length (tree
, BINFO_BASE_ACCESSES (t
));
453 for (i
= 0; i
< n
; i
++)
455 saved_base
= base
= BINFO_BASE_ACCESS (t
, i
);
456 LTO_FIXUP_TREE (base
);
457 if (base
!= saved_base
)
458 VEC_replace (tree
, BINFO_BASE_ACCESSES (t
), i
, base
);
460 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t
));
461 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t
));
462 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t
));
463 n
= BINFO_N_BASE_BINFOS (t
);
464 for (i
= 0; i
< n
; i
++)
466 saved_base
= base
= BINFO_BASE_BINFO (t
, i
);
467 LTO_FIXUP_TREE (base
);
468 if (base
!= saved_base
)
469 VEC_replace (tree
, BINFO_BASE_BINFOS (t
), i
, base
);
473 /* Fix up fields of a CONSTRUCTOR T. */
476 lto_ft_constructor (tree t
)
478 unsigned HOST_WIDE_INT idx
;
484 VEC_iterate(constructor_elt
, CONSTRUCTOR_ELTS (t
), idx
, ce
);
487 LTO_FIXUP_TREE (ce
->index
);
488 LTO_FIXUP_TREE (ce
->value
);
492 /* Fix up fields of an expression tree T. */
499 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
500 LTO_FIXUP_TREE (TREE_OPERAND (t
, i
));
503 /* Given a tree T fixup fields of T by replacing types with their merged
504 variant and other entities by an equal entity from an earlier compilation
505 unit, or an entity being canonical in a different way. This includes
506 for instance integer or string constants. */
509 lto_fixup_types (tree t
)
511 switch (TREE_CODE (t
))
513 case IDENTIFIER_NODE
:
517 LTO_FIXUP_TREE (TREE_VALUE (t
));
518 LTO_FIXUP_TREE (TREE_PURPOSE (t
));
519 LTO_FIXUP_TREE (TREE_CHAIN (t
));
523 lto_ft_field_decl (t
);
531 lto_ft_decl_common (t
);
535 lto_ft_decl_with_vis (t
);
539 lto_ft_decl_non_common (t
);
550 case PLACEHOLDER_EXPR
:
555 case TRANSLATION_UNIT_DECL
:
556 case OPTIMIZATION_NODE
:
557 case TARGET_OPTION_NODE
:
563 else if (TREE_CODE (t
) == CONSTRUCTOR
)
564 lto_ft_constructor (t
);
565 else if (CONSTANT_CLASS_P (t
))
566 LTO_FIXUP_TREE (TREE_TYPE (t
));
573 remember_with_vars (t
);
579 /* Return the resolution for the decl with index INDEX from DATA_IN. */
581 static enum ld_plugin_symbol_resolution
582 get_resolution (struct data_in
*data_in
, unsigned index
)
584 if (data_in
->globals_resolution
)
586 ld_plugin_symbol_resolution_t ret
;
587 /* We can have references to not emitted functions in
588 DECL_FUNCTION_PERSONALITY at least. So we can and have
589 to indeed return LDPR_UNKNOWN in some cases. */
590 if (VEC_length (ld_plugin_symbol_resolution_t
,
591 data_in
->globals_resolution
) <= index
)
593 ret
= VEC_index (ld_plugin_symbol_resolution_t
,
594 data_in
->globals_resolution
,
599 /* Delay resolution finding until decl merging. */
604 /* Register DECL with the global symbol table and change its
605 name if necessary to avoid name clashes for static globals across
609 lto_register_var_decl_in_symtab (struct data_in
*data_in
, tree decl
)
613 /* Variable has file scope, not local. Need to ensure static variables
614 between different files don't clash unexpectedly. */
615 if (!TREE_PUBLIC (decl
)
616 && !((context
= decl_function_context (decl
))
617 && auto_var_in_fn_p (decl
, context
)))
619 /* ??? We normally pre-mangle names before we serialize them
620 out. Here, in lto1, we do not know the language, and
621 thus cannot do the mangling again. Instead, we just
622 append a suffix to the mangled name. The resulting name,
623 however, is not a properly-formed mangled name, and will
624 confuse any attempt to unmangle it. */
625 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
628 ASM_FORMAT_PRIVATE_NAME (label
, name
, DECL_UID (decl
));
629 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (label
));
630 rest_of_decl_compilation (decl
, 1, 0);
631 VEC_safe_push (tree
, gc
, lto_global_var_decls
, decl
);
634 /* If this variable has already been declared, queue the
635 declaration for merging. */
636 if (TREE_PUBLIC (decl
))
639 if (!streamer_tree_cache_lookup (data_in
->reader_cache
, decl
, &ix
))
641 lto_symtab_register_decl (decl
, get_resolution (data_in
, ix
),
647 /* Register DECL with the global symbol table and change its
648 name if necessary to avoid name clashes for static globals across
649 different files. DATA_IN contains descriptors and tables for the
653 lto_register_function_decl_in_symtab (struct data_in
*data_in
, tree decl
)
655 /* Need to ensure static entities between different files
656 don't clash unexpectedly. */
657 if (!TREE_PUBLIC (decl
))
659 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
660 may set the assembler name where it was previously empty. */
661 tree old_assembler_name
= decl
->decl_with_vis
.assembler_name
;
663 /* FIXME lto: We normally pre-mangle names before we serialize
664 them out. Here, in lto1, we do not know the language, and
665 thus cannot do the mangling again. Instead, we just append a
666 suffix to the mangled name. The resulting name, however, is
667 not a properly-formed mangled name, and will confuse any
668 attempt to unmangle it. */
669 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
672 ASM_FORMAT_PRIVATE_NAME (label
, name
, DECL_UID (decl
));
673 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (label
));
675 /* We may arrive here with the old assembler name not set
676 if the function body is not needed, e.g., it has been
677 inlined away and does not appear in the cgraph. */
678 if (old_assembler_name
)
680 tree new_assembler_name
= DECL_ASSEMBLER_NAME (decl
);
682 /* Make the original assembler name available for later use.
683 We may have used it to indicate the section within its
684 object file where the function body may be found.
685 FIXME lto: Find a better way to maintain the function decl
686 to body section mapping so we don't need this hack. */
687 lto_record_renamed_decl (data_in
->file_data
,
688 IDENTIFIER_POINTER (old_assembler_name
),
689 IDENTIFIER_POINTER (new_assembler_name
));
693 /* If this variable has already been declared, queue the
694 declaration for merging. */
695 if (TREE_PUBLIC (decl
) && !DECL_ABSTRACT (decl
))
698 if (!streamer_tree_cache_lookup (data_in
->reader_cache
, decl
, &ix
))
700 lto_symtab_register_decl (decl
, get_resolution (data_in
, ix
),
706 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
707 for one compilation unit) go over all trees starting at index FROM until the
708 end of the sequence and replace fields of those trees, and the trees
709 themself with their canonical variants as per gimple_register_type. */
712 uniquify_nodes (struct data_in
*data_in
, unsigned from
)
714 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
715 unsigned len
= VEC_length (tree
, cache
->nodes
);
718 /* Go backwards because children streamed for the first time come
719 as part of their parents, and hence are created after them. */
721 /* First register all the types in the cache. This makes sure to
722 have the original structure in the type cycles when registering
723 them and computing hashes. */
724 for (i
= len
; i
-- > from
;)
726 tree t
= VEC_index (tree
, cache
->nodes
, i
);
729 tree newt
= gimple_register_type (t
);
730 /* Mark non-prevailing types so we fix them up. No need
731 to reset that flag afterwards - nothing that refers
732 to those types is left and they are collected. */
734 TREE_VISITED (t
) = 1;
738 /* Second fixup all trees in the new cache entries. */
739 for (i
= len
; i
-- > from
;)
741 tree t
= VEC_index (tree
, cache
->nodes
, i
);
746 /* First fixup the fields of T. */
752 /* Now try to find a canonical variant of T itself. */
753 t
= GIMPLE_REGISTER_TYPE (t
);
757 /* The following re-creates proper variant lists while fixing up
758 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
759 variant list state before fixup is broken. */
762 /* Remove us from our main variant list if we are not the
764 if (TYPE_MAIN_VARIANT (t
) != t
)
766 tem
= TYPE_MAIN_VARIANT (t
);
767 while (tem
&& TYPE_NEXT_VARIANT (tem
) != t
)
768 tem
= TYPE_NEXT_VARIANT (tem
);
770 TYPE_NEXT_VARIANT (tem
) = TYPE_NEXT_VARIANT (t
);
771 TYPE_NEXT_VARIANT (t
) = NULL_TREE
;
774 /* Query our new main variant. */
775 mv
= GIMPLE_REGISTER_TYPE (TYPE_MAIN_VARIANT (t
));
777 /* If we were the variant leader and we get replaced ourselves drop
778 all variants from our list. */
779 if (TYPE_MAIN_VARIANT (t
) == t
785 tree tem2
= TYPE_NEXT_VARIANT (tem
);
786 TYPE_NEXT_VARIANT (tem
) = NULL_TREE
;
791 /* If we are not our own variant leader link us into our new leaders
795 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
796 TYPE_NEXT_VARIANT (mv
) = t
;
797 if (RECORD_OR_UNION_TYPE_P (t
))
798 TYPE_BINFO (t
) = TYPE_BINFO (mv
);
799 /* Preserve the invariant that type variants share their
801 if (RECORD_OR_UNION_TYPE_P (t
)
802 && TYPE_FIELDS (mv
) != TYPE_FIELDS (t
))
805 for (f1
= TYPE_FIELDS (mv
), f2
= TYPE_FIELDS (t
);
806 f1
&& f2
; f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
810 && DECL_NAME (f1
) == DECL_NAME (f2
));
811 if (!streamer_tree_cache_lookup (cache
, f2
, &ix
))
813 /* If we're going to replace an element which we'd
814 still visit in the next iterations, we wouldn't
815 handle it, so do it here. We do have to handle it
816 even though the field_decl itself will be removed,
817 as it could refer to e.g. integer_cst which we
818 wouldn't reach via any other way, hence they
819 (and their type) would stay uncollected. */
820 /* ??? We should rather make sure to replace all
821 references to f2 with f1. That means handling
822 COMPONENT_REFs and CONSTRUCTOR elements in
823 lto_fixup_types and special-case the field-decl
825 /* ??? Not sure the above is all relevant in this
826 path canonicalizing TYPE_FIELDS to that of the
829 lto_fixup_types (f2
);
830 streamer_tree_cache_insert_at (cache
, f1
, ix
);
832 TYPE_FIELDS (t
) = TYPE_FIELDS (mv
);
836 /* Finally adjust our main variant and fix it up. */
837 TYPE_MAIN_VARIANT (t
) = mv
;
839 /* The following reconstructs the pointer chains
840 of the new pointed-to type if we are a main variant. We do
841 not stream those so they are broken before fixup. */
842 if (TREE_CODE (t
) == POINTER_TYPE
843 && TYPE_MAIN_VARIANT (t
) == t
)
845 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
846 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
848 else if (TREE_CODE (t
) == REFERENCE_TYPE
849 && TYPE_MAIN_VARIANT (t
) == t
)
851 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
852 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
858 if (RECORD_OR_UNION_TYPE_P (t
))
861 if (TYPE_FIELDS (t
) != TYPE_FIELDS (oldt
))
862 for (f1
= TYPE_FIELDS (t
), f2
= TYPE_FIELDS (oldt
);
863 f1
&& f2
; f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
866 gcc_assert (f1
!= f2
&& DECL_NAME (f1
) == DECL_NAME (f2
));
867 if (!streamer_tree_cache_lookup (cache
, f2
, &ix
))
869 /* If we're going to replace an element which we'd
870 still visit in the next iterations, we wouldn't
871 handle it, so do it here. We do have to handle it
872 even though the field_decl itself will be removed,
873 as it could refer to e.g. integer_cst which we
874 wouldn't reach via any other way, hence they
875 (and their type) would stay uncollected. */
876 /* ??? We should rather make sure to replace all
877 references to f2 with f1. That means handling
878 COMPONENT_REFs and CONSTRUCTOR elements in
879 lto_fixup_types and special-case the field-decl
882 lto_fixup_types (f2
);
883 streamer_tree_cache_insert_at (cache
, f1
, ix
);
887 /* If we found a tree that is equal to oldt replace it in the
888 cache, so that further users (in the various LTO sections)
890 streamer_tree_cache_insert_at (cache
, t
, i
);
894 /* Finally compute the canonical type of all TREE_TYPEs and register
895 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
896 From this point there are no longer any types with
897 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
898 This step requires the TYPE_POINTER_TO lists being present, so
899 make sure it is done last. */
900 for (i
= len
; i
-- > from
;)
902 tree t
= VEC_index (tree
, cache
->nodes
, i
);
906 if (TREE_CODE (t
) == VAR_DECL
)
907 lto_register_var_decl_in_symtab (data_in
, t
);
908 else if (TREE_CODE (t
) == FUNCTION_DECL
&& !DECL_BUILT_IN (t
))
909 lto_register_function_decl_in_symtab (data_in
, t
);
911 && TREE_CODE (t
) == TYPE_DECL
)
912 debug_hooks
->type_decl (t
, !DECL_FILE_SCOPE_P (t
));
913 else if (TYPE_P (t
) && !TYPE_CANONICAL (t
))
914 TYPE_CANONICAL (t
) = gimple_register_canonical_type (t
);
919 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
920 RESOLUTIONS is the set of symbols picked by the linker (read from the
921 resolution file when the linker plugin is being used). */
924 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
925 VEC(ld_plugin_symbol_resolution_t
,heap
) *resolutions
)
927 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
928 const int decl_offset
= sizeof (struct lto_decl_header
);
929 const int main_offset
= decl_offset
+ header
->decl_state_size
;
930 const int string_offset
= main_offset
+ header
->main_size
;
931 struct lto_input_block ib_main
;
932 struct data_in
*data_in
;
934 const uint32_t *data_ptr
, *data_end
;
935 uint32_t num_decl_states
;
937 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
940 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
941 header
->string_size
, resolutions
);
943 /* We do not uniquify the pre-loaded cache entries, those are middle-end
944 internal types that should not be merged. */
946 /* Read the global declarations and types. */
947 while (ib_main
.p
< ib_main
.len
)
950 unsigned from
= VEC_length (tree
, data_in
->reader_cache
->nodes
);
951 t
= stream_read_tree (&ib_main
, data_in
);
952 gcc_assert (t
&& ib_main
.p
<= ib_main
.len
);
953 uniquify_nodes (data_in
, from
);
956 /* Read in lto_in_decl_state objects. */
957 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
959 (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
960 num_decl_states
= *data_ptr
++;
962 gcc_assert (num_decl_states
> 0);
963 decl_data
->global_decl_state
= lto_new_in_decl_state ();
964 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
965 decl_data
->global_decl_state
);
967 /* Read in per-function decl states and enter them in hash table. */
968 decl_data
->function_decl_states
=
969 htab_create_ggc (37, lto_hash_in_decl_state
, lto_eq_in_decl_state
, NULL
);
971 for (i
= 1; i
< num_decl_states
; i
++)
973 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
976 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
977 slot
= htab_find_slot (decl_data
->function_decl_states
, state
, INSERT
);
978 gcc_assert (*slot
== NULL
);
982 if (data_ptr
!= data_end
)
983 internal_error ("bytecode stream: garbage at the end of symbols section");
985 /* Set the current decl state to be the global state. */
986 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
988 lto_data_in_delete (data_in
);
991 /* Custom version of strtoll, which is not portable. */
993 static HOST_WIDEST_INT
994 lto_parse_hex (const char *p
)
996 HOST_WIDEST_INT ret
= 0;
998 for (; *p
!= '\0'; ++p
)
1003 if (c
>= '0' && c
<= '9')
1005 else if (c
>= 'a' && c
<= 'f')
1006 part
= c
- 'a' + 10;
1007 else if (c
>= 'A' && c
<= 'F')
1008 part
= c
- 'A' + 10;
1010 internal_error ("could not parse hex number");
1017 /* Read resolution for file named FILE_NAME. The resolution is read from
1021 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
1023 /* We require that objects in the resolution file are in the same
1024 order as the lto1 command line. */
1025 unsigned int name_len
;
1027 unsigned int num_symbols
;
1029 struct lto_file_decl_data
*file_data
;
1030 unsigned max_index
= 0;
1031 splay_tree_node nd
= NULL
;
1036 name_len
= strlen (file
->filename
);
1037 obj_name
= XNEWVEC (char, name_len
+ 1);
1038 fscanf (resolution
, " "); /* Read white space. */
1040 fread (obj_name
, sizeof (char), name_len
, resolution
);
1041 obj_name
[name_len
] = '\0';
1042 if (filename_cmp (obj_name
, file
->filename
) != 0)
1043 internal_error ("unexpected file name %s in linker resolution file. "
1044 "Expected %s", obj_name
, file
->filename
);
1045 if (file
->offset
!= 0)
1049 HOST_WIDEST_INT offset
;
1050 t
= fscanf (resolution
, "@0x%16s", offset_p
);
1052 internal_error ("could not parse file offset");
1053 offset
= lto_parse_hex (offset_p
);
1054 if (offset
!= file
->offset
)
1055 internal_error ("unexpected offset");
1060 fscanf (resolution
, "%u", &num_symbols
);
1062 for (i
= 0; i
< num_symbols
; i
++)
1066 unsigned HOST_WIDE_INT id
;
1068 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
1070 unsigned int lto_resolution_str_len
=
1071 sizeof (lto_resolution_str
) / sizeof (char *);
1073 t
= fscanf (resolution
, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
" %26s %*[^\n]\n",
1074 &index
, &id
, r_str
);
1076 internal_error ("invalid line in the resolution file");
1077 if (index
> max_index
)
1080 for (j
= 0; j
< lto_resolution_str_len
; j
++)
1082 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
1084 r
= (enum ld_plugin_symbol_resolution
) j
;
1088 if (j
== lto_resolution_str_len
)
1089 internal_error ("invalid resolution in the resolution file");
1091 if (!(nd
&& lto_splay_tree_id_equal_p (nd
->key
, id
)))
1093 nd
= lto_splay_tree_lookup (file_ids
, id
);
1095 internal_error ("resolution sub id " HOST_WIDE_INT_PRINT_HEX_PURE
1096 " not in object file", id
);
1099 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1100 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t
, heap
,
1101 file_data
->resolutions
,
1103 VEC_replace (ld_plugin_symbol_resolution_t
,
1104 file_data
->resolutions
, index
, r
);
1108 /* List of file_decl_datas */
1109 struct file_data_list
1111 struct lto_file_decl_data
*first
, *last
;
1114 /* Is the name for a id'ed LTO section? */
1117 lto_section_with_id (const char *name
, unsigned HOST_WIDE_INT
*id
)
1121 if (strncmp (name
, LTO_SECTION_NAME_PREFIX
, strlen (LTO_SECTION_NAME_PREFIX
)))
1123 s
= strrchr (name
, '.');
1124 return s
&& sscanf (s
, "." HOST_WIDE_INT_PRINT_HEX_PURE
, id
) == 1;
1127 /* Create file_data of each sub file id */
1130 create_subid_section_table (struct lto_section_slot
*ls
, splay_tree file_ids
,
1131 struct file_data_list
*list
)
1133 struct lto_section_slot s_slot
, *new_slot
;
1134 unsigned HOST_WIDE_INT id
;
1138 struct lto_file_decl_data
*file_data
;
1140 if (!lto_section_with_id (ls
->name
, &id
))
1143 /* Find hash table of sub module id */
1144 nd
= lto_splay_tree_lookup (file_ids
, id
);
1147 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1151 file_data
= ggc_alloc_lto_file_decl_data ();
1152 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
1154 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();;
1155 lto_splay_tree_insert (file_ids
, id
, file_data
);
1157 /* Maintain list in linker order */
1159 list
->first
= file_data
;
1161 list
->last
->next
= file_data
;
1162 list
->last
= file_data
;
1165 /* Copy section into sub module hash table */
1166 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
1167 s_slot
.name
= new_name
;
1168 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
1169 gcc_assert (*hash_slot
== NULL
);
1171 new_slot
= XDUP (struct lto_section_slot
, ls
);
1172 new_slot
->name
= new_name
;
1173 *hash_slot
= new_slot
;
1177 /* Read declarations and other initializations for a FILE_DATA. */
1180 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
)
1185 file_data
->renaming_hash_table
= lto_create_renaming_table ();
1186 file_data
->file_name
= file
->filename
;
1187 data
= lto_get_section_data (file_data
, LTO_section_decls
, NULL
, &len
);
1190 internal_error ("cannot read LTO decls from %s", file_data
->file_name
);
1193 lto_read_decls (file_data
, data
, file_data
->resolutions
);
1194 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
1197 /* Finalize FILE_DATA in FILE and increase COUNT. */
1200 lto_create_files_from_ids (lto_file
*file
, struct lto_file_decl_data
*file_data
,
1203 lto_file_finalize (file_data
, file
);
1204 if (cgraph_dump_file
)
1205 fprintf (cgraph_dump_file
, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX
"\n",
1206 file_data
->file_name
, file_data
->id
);
1211 /* Generate a TREE representation for all types and external decls
1214 Read all of the globals out of the file. Then read the cgraph
1215 and process the .o index into the cgraph nodes so that it can open
1216 the .o file to load the functions and ipa information. */
1218 static struct lto_file_decl_data
*
1219 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
1221 struct lto_file_decl_data
*file_data
= NULL
;
1222 splay_tree file_ids
;
1223 htab_t section_hash_table
;
1224 struct lto_section_slot
*section
;
1225 struct file_data_list file_list
;
1226 struct lto_section_list section_list
;
1228 memset (§ion_list
, 0, sizeof (struct lto_section_list
));
1229 section_hash_table
= lto_obj_build_section_table (file
, §ion_list
);
1231 /* Find all sub modules in the object and put their sections into new hash
1232 tables in a splay tree. */
1233 file_ids
= lto_splay_tree_new ();
1234 memset (&file_list
, 0, sizeof (struct file_data_list
));
1235 for (section
= section_list
.first
; section
!= NULL
; section
= section
->next
)
1236 create_subid_section_table (section
, file_ids
, &file_list
);
1238 /* Add resolutions to file ids */
1239 lto_resolution_read (file_ids
, resolution_file
, file
);
1241 /* Finalize each lto file for each submodule in the merged object */
1242 for (file_data
= file_list
.first
; file_data
!= NULL
; file_data
= file_data
->next
)
1243 lto_create_files_from_ids (file
, file_data
, count
);
1245 splay_tree_delete (file_ids
);
1246 htab_delete (section_hash_table
);
1248 return file_list
.first
;
1251 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
1252 #define LTO_MMAP_IO 1
1256 /* Page size of machine is used for mmap and munmap calls. */
1257 static size_t page_mask
;
1260 /* Get the section data of length LEN from FILENAME starting at
1261 OFFSET. The data segment must be freed by the caller when the
1262 caller is finished. Returns NULL if all was not well. */
1265 lto_read_section_data (struct lto_file_decl_data
*file_data
,
1266 intptr_t offset
, size_t len
)
1270 static char *fd_name
;
1272 intptr_t computed_len
;
1273 intptr_t computed_offset
;
1277 /* Keep a single-entry file-descriptor cache. The last file we
1278 touched will get closed at exit.
1279 ??? Eventually we want to add a more sophisticated larger cache
1280 or rather fix function body streaming to not stream them in
1281 practically random order. */
1283 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
1291 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
1294 fatal_error ("Cannot open %s", file_data
->file_name
);
1297 fd_name
= xstrdup (file_data
->file_name
);
1303 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
1304 page_mask
= ~(page_size
- 1);
1307 computed_offset
= offset
& page_mask
;
1308 diff
= offset
- computed_offset
;
1309 computed_len
= len
+ diff
;
1311 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
1312 fd
, computed_offset
);
1313 if (result
== MAP_FAILED
)
1315 fatal_error ("Cannot map %s", file_data
->file_name
);
1319 return result
+ diff
;
1321 result
= (char *) xmalloc (len
);
1322 if (lseek (fd
, offset
, SEEK_SET
) != offset
1323 || read (fd
, result
, len
) != (ssize_t
) len
)
1326 fatal_error ("Cannot read %s", file_data
->file_name
);
1330 /* Native windows doesn't supports delayed unlink on opened file. So
1331 we close file here again. This produces higher I/O load, but at least
1332 it prevents to have dangling file handles preventing unlink. */
1343 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1344 NAME will be NULL unless the section type is for a function
1348 get_section_data (struct lto_file_decl_data
*file_data
,
1349 enum lto_section_type section_type
,
1353 htab_t section_hash_table
= file_data
->section_hash_table
;
1354 struct lto_section_slot
*f_slot
;
1355 struct lto_section_slot s_slot
;
1356 const char *section_name
= lto_get_section_name (section_type
, name
, file_data
);
1360 s_slot
.name
= section_name
;
1361 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
1364 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
1368 free (CONST_CAST (char *, section_name
));
1373 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1374 starts at OFFSET and has LEN bytes. */
1377 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
1378 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
1379 const char *name ATTRIBUTE_UNUSED
,
1380 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
1383 intptr_t computed_len
;
1384 intptr_t computed_offset
;
1389 computed_offset
= ((intptr_t) offset
) & page_mask
;
1390 diff
= (intptr_t) offset
- computed_offset
;
1391 computed_len
= len
+ diff
;
1393 munmap ((caddr_t
) computed_offset
, computed_len
);
1395 free (CONST_CAST(char *, offset
));
1399 static lto_file
*current_lto_file
;
1401 /* Helper for qsort; compare partitions and return one with smaller size.
1402 We sort from greatest to smallest so parallel build doesn't stale on the
1403 longest compilation being executed too late. */
1406 cmp_partitions_size (const void *a
, const void *b
)
1408 const struct ltrans_partition_def
*pa
1409 = *(struct ltrans_partition_def
*const *)a
;
1410 const struct ltrans_partition_def
*pb
1411 = *(struct ltrans_partition_def
*const *)b
;
1412 return pb
->insns
- pa
->insns
;
1415 /* Helper for qsort; compare partitions and return one with smaller order. */
1418 cmp_partitions_order (const void *a
, const void *b
)
1420 const struct ltrans_partition_def
*pa
1421 = *(struct ltrans_partition_def
*const *)a
;
1422 const struct ltrans_partition_def
*pb
1423 = *(struct ltrans_partition_def
*const *)b
;
1424 int ordera
= -1, orderb
= -1;
1426 if (VEC_length (cgraph_node_ptr
, pa
->cgraph_set
->nodes
))
1427 ordera
= VEC_index (cgraph_node_ptr
, pa
->cgraph_set
->nodes
, 0)->symbol
.order
;
1428 else if (VEC_length (varpool_node_ptr
, pa
->varpool_set
->nodes
))
1429 ordera
= VEC_index (varpool_node_ptr
, pa
->varpool_set
->nodes
, 0)->symbol
.order
;
1430 if (VEC_length (cgraph_node_ptr
, pb
->cgraph_set
->nodes
))
1431 orderb
= VEC_index (cgraph_node_ptr
, pb
->cgraph_set
->nodes
, 0)->symbol
.order
;
1432 else if (VEC_length (varpool_node_ptr
, pb
->varpool_set
->nodes
))
1433 orderb
= VEC_index (varpool_node_ptr
, pb
->varpool_set
->nodes
, 0)->symbol
.order
;
1434 return orderb
- ordera
;
1437 /* Write all output files in WPA mode and the file with the list of
1441 lto_wpa_write_files (void)
1445 cgraph_node_set set
;
1446 varpool_node_set vset
;
1447 ltrans_partition part
;
1448 FILE *ltrans_output_list_stream
;
1449 char *temp_filename
;
1452 /* Open the LTRANS output list. */
1453 if (!ltrans_output_list
)
1454 fatal_error ("no LTRANS output list filename provided");
1455 ltrans_output_list_stream
= fopen (ltrans_output_list
, "w");
1456 if (ltrans_output_list_stream
== NULL
)
1457 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list
);
1459 timevar_push (TV_WHOPR_WPA
);
1461 FOR_EACH_VEC_ELT (ltrans_partition
, ltrans_partitions
, i
, part
)
1462 lto_stats
.num_output_cgraph_nodes
+= VEC_length (cgraph_node_ptr
,
1463 part
->cgraph_set
->nodes
);
1465 /* Find out statics that need to be promoted
1466 to globals with hidden visibility because they are accessed from multiple
1468 lto_promote_cross_file_statics ();
1470 timevar_pop (TV_WHOPR_WPA
);
1472 timevar_push (TV_WHOPR_WPA_IO
);
1474 /* Generate a prefix for the LTRANS unit files. */
1475 blen
= strlen (ltrans_output_list
);
1476 temp_filename
= (char *) xmalloc (blen
+ sizeof ("2147483648.o"));
1477 strcpy (temp_filename
, ltrans_output_list
);
1478 if (blen
> sizeof (".out")
1479 && strcmp (temp_filename
+ blen
- sizeof (".out") + 1,
1481 temp_filename
[blen
- sizeof (".out") + 1] = '\0';
1482 blen
= strlen (temp_filename
);
1484 n_sets
= VEC_length (ltrans_partition
, ltrans_partitions
);
1486 /* Sort partitions by size so small ones are compiled last.
1487 FIXME: Even when not reordering we may want to output one list for parallel make
1488 and other for final link command. */
1489 VEC_qsort (ltrans_partition
, ltrans_partitions
,
1490 flag_toplevel_reorder
? cmp_partitions_size
: cmp_partitions_order
);
1491 for (i
= 0; i
< n_sets
; i
++)
1494 ltrans_partition part
= VEC_index (ltrans_partition
, ltrans_partitions
, i
);
1496 set
= part
->cgraph_set
;
1497 vset
= part
->varpool_set
;
1499 /* Write all the nodes in SET. */
1500 sprintf (temp_filename
+ blen
, "%u.o", i
);
1501 file
= lto_obj_file_open (temp_filename
, true);
1503 fatal_error ("lto_obj_file_open() failed");
1506 fprintf (stderr
, " %s (%s %i insns)", temp_filename
, part
->name
, part
->insns
);
1507 if (cgraph_dump_file
)
1509 fprintf (cgraph_dump_file
, "Writing partition %s to file %s, %i insns\n",
1510 part
->name
, temp_filename
, part
->insns
);
1511 fprintf (cgraph_dump_file
, "cgraph nodes:");
1512 dump_cgraph_node_set (cgraph_dump_file
, set
);
1513 fprintf (cgraph_dump_file
, "varpool nodes:");
1514 dump_varpool_node_set (cgraph_dump_file
, vset
);
1516 gcc_checking_assert (cgraph_node_set_nonempty_p (set
)
1517 || varpool_node_set_nonempty_p (vset
) || !i
);
1519 lto_set_current_out_file (file
);
1521 ipa_write_optimization_summaries (set
, vset
);
1523 lto_set_current_out_file (NULL
);
1524 lto_obj_file_close (file
);
1526 len
= strlen (temp_filename
);
1527 if (fwrite (temp_filename
, 1, len
, ltrans_output_list_stream
) < len
1528 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
1529 fatal_error ("writing to LTRANS output list %s: %m",
1530 ltrans_output_list
);
1533 lto_stats
.num_output_files
+= n_sets
;
1535 /* Close the LTRANS output list. */
1536 if (fclose (ltrans_output_list_stream
))
1537 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list
);
1539 free_ltrans_partitions();
1541 timevar_pop (TV_WHOPR_WPA_IO
);
1545 /* If TT is a variable or function decl replace it with its
1546 prevailing variant. */
1547 #define LTO_SET_PREVAIL(tt) \
1549 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
1550 tt = lto_symtab_prevailing_decl (tt); \
1553 /* Ensure that TT isn't a replacable var of function decl. */
1554 #define LTO_NO_PREVAIL(tt) \
1555 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
1557 /* Given a tree T replace all fields referring to variables or functions
1558 with their prevailing variant. */
1560 lto_fixup_prevailing_decls (tree t
)
1562 enum tree_code code
= TREE_CODE (t
);
1563 LTO_NO_PREVAIL (TREE_TYPE (t
));
1564 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
1565 LTO_NO_PREVAIL (TREE_CHAIN (t
));
1568 LTO_NO_PREVAIL (DECL_NAME (t
));
1569 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
1570 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
1572 LTO_SET_PREVAIL (DECL_SIZE (t
));
1573 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
1574 LTO_SET_PREVAIL (DECL_INITIAL (t
));
1575 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
1576 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
1578 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
1580 LTO_NO_PREVAIL (t
->decl_with_vis
.assembler_name
);
1581 LTO_NO_PREVAIL (DECL_SECTION_NAME (t
));
1583 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
1585 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t
));
1586 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
1587 LTO_NO_PREVAIL (DECL_VINDEX (t
));
1589 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
1590 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
1591 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
1593 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t
));
1594 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
1595 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
1596 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
1597 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
1600 else if (TYPE_P (t
))
1602 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
1603 LTO_SET_PREVAIL (TYPE_SIZE (t
));
1604 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
1605 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
1606 LTO_NO_PREVAIL (TYPE_NAME (t
));
1608 LTO_SET_PREVAIL (TYPE_MINVAL (t
));
1609 LTO_SET_PREVAIL (TYPE_MAXVAL (t
));
1610 LTO_SET_PREVAIL (t
->type_non_common
.binfo
);
1612 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
1614 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
1615 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
1616 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
1618 else if (EXPR_P (t
))
1621 LTO_NO_PREVAIL (t
->exp
.block
);
1622 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
1623 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
1630 LTO_SET_PREVAIL (TREE_VALUE (t
));
1631 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
1638 #undef LTO_SET_PREVAIL
1639 #undef LTO_NO_PREVAIL
1641 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1642 replaces var and function decls with the corresponding prevailing def. */
1645 lto_fixup_state (struct lto_in_decl_state
*state
)
1648 struct lto_tree_ref_table
*table
;
1650 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1651 we still need to walk from all DECLs to find the reachable
1652 FUNCTION_DECLs and VAR_DECLs. */
1653 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
1655 table
= &state
->streams
[si
];
1656 for (i
= 0; i
< table
->size
; i
++)
1658 tree
*tp
= table
->trees
+ i
;
1659 if (VAR_OR_FUNCTION_DECL_P (*tp
))
1660 *tp
= lto_symtab_prevailing_decl (*tp
);
1665 /* A callback of htab_traverse. Just extracts a state from SLOT
1666 and calls lto_fixup_state. */
1669 lto_fixup_state_aux (void **slot
, void *aux ATTRIBUTE_UNUSED
)
1671 struct lto_in_decl_state
*state
= (struct lto_in_decl_state
*) *slot
;
1672 lto_fixup_state (state
);
1676 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1680 lto_fixup_decls (struct lto_file_decl_data
**files
)
1686 FOR_EACH_HTAB_ELEMENT (tree_with_vars
, t
, tree
, hi
)
1687 lto_fixup_prevailing_decls (t
);
1689 for (i
= 0; files
[i
]; i
++)
1691 struct lto_file_decl_data
*file
= files
[i
];
1692 struct lto_in_decl_state
*state
= file
->global_decl_state
;
1693 lto_fixup_state (state
);
1695 htab_traverse (file
->function_decl_states
, lto_fixup_state_aux
, NULL
);
1699 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
1701 /* Turn file datas for sub files into a single array, so that they look
1702 like separate files for further passes. */
1705 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
, int last_file_ix
)
1707 struct lto_file_decl_data
*n
, *next
;
1710 lto_stats
.num_input_files
= count
;
1712 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count
+ 1);
1713 /* Set the hooks so that all of the ipa passes can read in their data. */
1714 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
1715 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
1717 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
1719 all_file_decl_data
[k
++] = n
;
1724 all_file_decl_data
[k
] = NULL
;
1725 gcc_assert (k
== count
);
1728 /* Input file data before flattening (i.e. splitting them to subfiles to support
1729 incremental linking. */
1730 static int real_file_count
;
1731 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
1733 /* Read all the symbols from the input files FNAMES. NFILES is the
1734 number of files requested in the command line. Instantiate a
1735 global call graph by aggregating all the sub-graphs found in each
1739 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
1741 unsigned int i
, last_file_ix
;
1743 struct cgraph_node
*node
;
1745 struct lto_file_decl_data
**decl_data
;
1749 timevar_push (TV_IPA_LTO_DECL_IN
);
1752 = decl_data
= ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles
+ 1);
1753 real_file_count
= nfiles
;
1755 /* Read the resolution file. */
1757 if (resolution_file_name
)
1760 unsigned num_objects
;
1762 resolution
= fopen (resolution_file_name
, "r");
1763 if (resolution
== NULL
)
1764 fatal_error ("could not open symbol resolution file: %m");
1766 t
= fscanf (resolution
, "%u", &num_objects
);
1767 gcc_assert (t
== 1);
1769 /* True, since the plugin splits the archives. */
1770 gcc_assert (num_objects
== nfiles
);
1773 tree_with_vars
= htab_create_ggc (101, htab_hash_pointer
, htab_eq_pointer
,
1777 fprintf (stderr
, "Reading object files:");
1779 /* Read all of the object files specified on the command line. */
1780 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
1782 struct lto_file_decl_data
*file_data
= NULL
;
1785 fprintf (stderr
, " %s", fnames
[i
]);
1789 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
1790 if (!current_lto_file
)
1793 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
1796 lto_obj_file_close (current_lto_file
);
1797 current_lto_file
= NULL
;
1801 decl_data
[last_file_ix
++] = file_data
;
1803 lto_obj_file_close (current_lto_file
);
1804 current_lto_file
= NULL
;
1808 lto_flatten_files (decl_data
, count
, last_file_ix
);
1809 lto_stats
.num_input_files
= count
;
1810 ggc_free(decl_data
);
1811 real_file_decl_data
= NULL
;
1813 if (resolution_file_name
)
1814 fclose (resolution
);
1816 /* Set the hooks so that all of the ipa passes can read in their data. */
1817 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
1819 timevar_pop (TV_IPA_LTO_DECL_IN
);
1822 fprintf (stderr
, "\nReading the callgraph\n");
1824 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
1825 /* Read the callgraph. */
1827 timevar_pop (TV_IPA_LTO_CGRAPH_IO
);
1830 fprintf (stderr
, "Merging declarations\n");
1832 timevar_push (TV_IPA_LTO_DECL_MERGE
);
1833 /* Merge global decls. */
1834 lto_symtab_merge_decls ();
1836 /* If there were errors during symbol merging bail out, we have no
1837 good way to recover here. */
1839 fatal_error ("errors during merging of translation units");
1841 /* Fixup all decls and types and free the type hash tables. */
1842 lto_fixup_decls (all_file_decl_data
);
1843 htab_delete (tree_with_vars
);
1844 tree_with_vars
= NULL
;
1845 free_gimple_type_tables ();
1848 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
1849 /* Each pass will set the appropriate timer. */
1852 fprintf (stderr
, "Reading summaries\n");
1854 /* Read the IPA summary data. */
1856 ipa_read_optimization_summaries ();
1858 ipa_read_summaries ();
1860 /* Finally merge the cgraph according to the decl merging decisions. */
1861 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
1862 if (cgraph_dump_file
)
1864 fprintf (cgraph_dump_file
, "Before merging:\n");
1865 dump_cgraph (cgraph_dump_file
);
1866 dump_varpool (cgraph_dump_file
);
1868 lto_symtab_merge_cgraph_nodes ();
1871 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1872 summaries computed and needs to apply changes. At the moment WHOPR only
1873 supports inlining, so we can push it here by hand. In future we need to stream
1874 this field into ltrans compilation. */
1876 FOR_EACH_DEFINED_FUNCTION (node
)
1877 VEC_safe_push (ipa_opt_pass
, heap
,
1878 node
->ipa_transforms_to_apply
,
1879 (ipa_opt_pass
)&pass_ipa_inline
);
1882 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
1884 timevar_push (TV_IPA_LTO_DECL_INIT_IO
);
1886 /* FIXME lto. This loop needs to be changed to use the pass manager to
1887 call the ipa passes directly. */
1889 for (i
= 0; i
< last_file_ix
; i
++)
1891 struct lto_file_decl_data
*file_data
= all_file_decl_data
[i
];
1892 lto_materialize_constructors_and_inits (file_data
);
1895 /* Indicate that the cgraph is built and ready. */
1896 cgraph_function_flags_ready
= true;
1898 timevar_pop (TV_IPA_LTO_DECL_INIT_IO
);
1899 ggc_free (all_file_decl_data
);
1900 all_file_decl_data
= NULL
;
1904 /* Materialize all the bodies for all the nodes in the callgraph. */
1907 materialize_cgraph (void)
1910 struct cgraph_node
*node
;
1912 timevar_id_t lto_timer
;
1916 flag_wpa
? "Materializing decls:" : "Reading function bodies:");
1919 /* Now that we have input the cgraph, we need to clear all of the aux
1920 nodes and read the functions if we are not running in WPA mode. */
1921 timevar_push (TV_IPA_LTO_GIMPLE_IN
);
1923 FOR_EACH_FUNCTION (node
)
1925 if (node
->symbol
.lto_file_data
)
1927 lto_materialize_function (node
);
1928 lto_stats
.num_input_cgraph_nodes
++;
1932 timevar_pop (TV_IPA_LTO_GIMPLE_IN
);
1934 /* Start the appropriate timer depending on the mode that we are
1936 lto_timer
= (flag_wpa
) ? TV_WHOPR_WPA
1937 : (flag_ltrans
) ? TV_WHOPR_LTRANS
1939 timevar_push (lto_timer
);
1941 current_function_decl
= NULL
;
1944 /* Inform the middle end about the global variables we have seen. */
1945 FOR_EACH_VEC_ELT (tree
, lto_global_var_decls
, i
, decl
)
1946 rest_of_decl_compilation (decl
, 1, 0);
1949 fprintf (stderr
, "\n");
1951 timevar_pop (lto_timer
);
1955 /* Perform whole program analysis (WPA) on the callgraph and write out the
1956 optimization plan. */
1959 do_whole_program_analysis (void)
1961 timevar_start (TV_PHASE_CGRAPH
);
1962 /* Note that since we are in WPA mode, materialize_cgraph will not
1963 actually read in all the function bodies. It only materializes
1964 the decls and cgraph nodes so that analysis can be performed. */
1965 materialize_cgraph ();
1967 /* Reading in the cgraph uses different timers, start timing WPA now. */
1968 timevar_push (TV_WHOPR_WPA
);
1970 if (pre_ipa_mem_report
)
1972 fprintf (stderr
, "Memory consumption before IPA\n");
1973 dump_memory_report (false);
1976 cgraph_function_flags_ready
= true;
1978 if (cgraph_dump_file
)
1980 dump_cgraph (cgraph_dump_file
);
1981 dump_varpool (cgraph_dump_file
);
1983 bitmap_obstack_initialize (NULL
);
1984 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
1986 execute_ipa_pass_list (all_regular_ipa_passes
);
1988 if (cgraph_dump_file
)
1990 fprintf (cgraph_dump_file
, "Optimized ");
1991 dump_cgraph (cgraph_dump_file
);
1992 dump_varpool (cgraph_dump_file
);
1995 bitmap_obstack_release (NULL
);
1997 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1998 timevar_pop (TV_WHOPR_WPA
);
2000 if (flag_lto_partition_1to1
)
2003 lto_balanced_map ();
2007 fprintf (stderr
, "\nStreaming out");
2010 lto_wpa_write_files ();
2013 fprintf (stderr
, "\n");
2015 if (post_ipa_mem_report
)
2017 fprintf (stderr
, "Memory consumption after IPA\n");
2018 dump_memory_report (false);
2021 timevar_stop (TV_PHASE_CGRAPH
);
2022 /* Show the LTO report before launching LTRANS. */
2023 if (flag_lto_report
)
2024 print_lto_report ();
2028 static GTY(()) tree lto_eh_personality_decl
;
2030 /* Return the LTO personality function decl. */
2033 lto_eh_personality (void)
2035 if (!lto_eh_personality_decl
)
2037 /* Use the first personality DECL for our personality if we don't
2038 support multiple ones. This ensures that we don't artificially
2039 create the need for them in a single-language program. */
2040 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
2041 lto_eh_personality_decl
= first_personality_decl
;
2043 lto_eh_personality_decl
= lhd_gcc_personality ();
2046 return lto_eh_personality_decl
;
2049 /* Set the process name based on the LTO mode. */
2052 lto_process_name (void)
2055 setproctitle ("lto1-lto");
2057 setproctitle ("lto1-wpa");
2059 setproctitle ("lto1-ltrans");
2063 /* Initialize the LTO front end. */
2068 lto_process_name ();
2069 lto_streamer_hooks_init ();
2071 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
2072 memset (<o_stats
, 0, sizeof (lto_stats
));
2073 bitmap_obstack_initialize (NULL
);
2074 gimple_register_cfg_hooks ();
2078 /* Main entry point for the GIMPLE front end. This front end has
2079 three main personalities:
2081 - LTO (-flto). All the object files on the command line are
2082 loaded in memory and processed as a single translation unit.
2083 This is the traditional link-time optimization behavior.
2085 - WPA (-fwpa). Only the callgraph and summary information for
2086 files in the command file are loaded. A single callgraph
2087 (without function bodies) is instantiated for the whole set of
2088 files. IPA passes are only allowed to analyze the call graph
2089 and make transformation decisions. The callgraph is
2090 partitioned, each partition is written to a new object file
2091 together with the transformation decisions.
2093 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2094 summary files from running again. Since WPA computed summary
2095 information and decided what transformations to apply, LTRANS
2096 simply applies them. */
2101 /* Initialize the LTO front end. */
2104 /* Read all the symbols and call graph from all the files in the
2106 read_cgraph_and_symbols (num_in_fnames
, in_fnames
);
2110 /* If WPA is enabled analyze the whole call graph and create an
2111 optimization plan. Otherwise, read in all the function
2112 bodies and continue with optimization. */
2114 do_whole_program_analysis ();
2117 materialize_cgraph ();
2119 /* Let the middle end know that we have read and merged all of
2121 timevar_start (TV_PHASE_CGRAPH
);
2123 timevar_stop (TV_PHASE_CGRAPH
);
2125 /* FIXME lto, if the processes spawned by WPA fail, we miss
2126 the chance to print WPA's report, so WPA will call
2127 print_lto_report before launching LTRANS. If LTRANS was
2128 launched directly by the driver we would not need to do
2130 if (flag_lto_report
)
2131 print_lto_report ();
2136 #include "gt-lto-lto.h"