1 /* Top-level LTO routines.
2 Copyright 2009, 2010, 2011 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"
49 #include "ipa-inline.h"
50 #include "ipa-utils.h"
52 static GTY(()) tree first_personality_decl
;
54 /* Returns a hash code for P. */
57 hash_name (const void *p
)
59 const struct lto_section_slot
*ds
= (const struct lto_section_slot
*) p
;
60 return (hashval_t
) htab_hash_string (ds
->name
);
64 /* Returns nonzero if P1 and P2 are equal. */
67 eq_name (const void *p1
, const void *p2
)
69 const struct lto_section_slot
*s1
=
70 (const struct lto_section_slot
*) p1
;
71 const struct lto_section_slot
*s2
=
72 (const struct lto_section_slot
*) p2
;
74 return strcmp (s1
->name
, s2
->name
) == 0;
77 /* Free lto_section_slot */
80 free_with_string (void *arg
)
82 struct lto_section_slot
*s
= (struct lto_section_slot
*)arg
;
84 free (CONST_CAST (char *, s
->name
));
88 /* Create section hash table */
91 lto_obj_create_section_hash_table (void)
93 return htab_create (37, hash_name
, eq_name
, free_with_string
);
96 /* Read the constructors and inits. */
99 lto_materialize_constructors_and_inits (struct lto_file_decl_data
* file_data
)
102 const char *data
= lto_get_section_data (file_data
,
103 LTO_section_static_initializer
,
105 lto_input_constructors_and_inits (file_data
, data
);
106 lto_free_section_data (file_data
, LTO_section_static_initializer
, NULL
,
110 /* Return true when NODE has a clone that is analyzed (i.e. we need
111 to load its body even if the node itself is not needed). */
114 has_analyzed_clone_p (struct cgraph_node
*node
)
116 struct cgraph_node
*orig
= node
;
125 else if (node
->next_sibling_clone
)
126 node
= node
->next_sibling_clone
;
129 while (node
!= orig
&& !node
->next_sibling_clone
)
130 node
= node
->clone_of
;
132 node
= node
->next_sibling_clone
;
138 /* Read the function body for the function associated with NODE. */
141 lto_materialize_function (struct cgraph_node
*node
)
144 struct lto_file_decl_data
*file_data
;
145 const char *data
, *name
;
149 /* Read in functions with body (analyzed nodes)
150 and also functions that are needed to produce virtual clones. */
151 if (cgraph_function_with_gimple_body_p (node
) || has_analyzed_clone_p (node
))
153 /* Clones and thunks don't need to be read. */
157 /* Load the function body only if not operating in WPA mode. In
158 WPA mode, the body of the function is not needed. */
161 file_data
= node
->local
.lto_file_data
;
162 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
164 /* We may have renamed the declaration, e.g., a static function. */
165 name
= lto_get_decl_name_mapping (file_data
, name
);
167 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
170 fatal_error ("%s: section %s is missing",
171 file_data
->file_name
,
174 gcc_assert (DECL_STRUCT_FUNCTION (decl
) == NULL
);
176 allocate_struct_function (decl
, false);
177 announce_function (decl
);
178 lto_input_function_body (file_data
, decl
, data
);
179 if (DECL_FUNCTION_PERSONALITY (decl
) && !first_personality_decl
)
180 first_personality_decl
= DECL_FUNCTION_PERSONALITY (decl
);
181 lto_stats
.num_function_bodies
++;
182 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
188 /* Let the middle end know about the function. */
189 rest_of_decl_compilation (decl
, 1, 0);
193 /* Decode the content of memory pointed to by DATA in the in decl
194 state object STATE. DATA_IN points to a data_in structure for
195 decoding. Return the address after the decoded object in the
198 static const uint32_t *
199 lto_read_in_decl_state (struct data_in
*data_in
, const uint32_t *data
,
200 struct lto_in_decl_state
*state
)
207 decl
= streamer_tree_cache_get (data_in
->reader_cache
, ix
);
208 if (TREE_CODE (decl
) != FUNCTION_DECL
)
210 gcc_assert (decl
== void_type_node
);
213 state
->fn_decl
= decl
;
215 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
217 uint32_t size
= *data
++;
218 tree
*decls
= ggc_alloc_vec_tree (size
);
220 for (j
= 0; j
< size
; j
++)
221 decls
[j
] = streamer_tree_cache_get (data_in
->reader_cache
, data
[j
]);
223 state
->streams
[i
].size
= size
;
224 state
->streams
[i
].trees
= decls
;
231 /* A hashtable of trees that potentially refer to variables or functions
232 that must be replaced with their prevailing variant. */
233 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node
))) htab_t
236 /* Remember that T is a tree that (potentially) refers to a variable
237 or function decl that may be replaced with its prevailing variant. */
239 remember_with_vars (tree t
)
241 *(tree
*) htab_find_slot (tree_with_vars
, t
, INSERT
) = t
;
244 #define LTO_FIXUP_TREE(tt) \
250 (tt) = gimple_register_type (tt); \
251 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
252 remember_with_vars (t); \
256 static void lto_fixup_types (tree
);
258 /* Fix up fields of a tree_typed T. */
261 lto_ft_typed (tree t
)
263 LTO_FIXUP_TREE (TREE_TYPE (t
));
266 /* Fix up fields of a tree_common T. */
269 lto_ft_common (tree t
)
272 LTO_FIXUP_TREE (TREE_CHAIN (t
));
275 /* Fix up fields of a decl_minimal T. */
278 lto_ft_decl_minimal (tree t
)
281 LTO_FIXUP_TREE (DECL_NAME (t
));
282 LTO_FIXUP_TREE (DECL_CONTEXT (t
));
285 /* Fix up fields of a decl_common T. */
288 lto_ft_decl_common (tree t
)
290 lto_ft_decl_minimal (t
);
291 LTO_FIXUP_TREE (DECL_SIZE (t
));
292 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t
));
293 LTO_FIXUP_TREE (DECL_INITIAL (t
));
294 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t
));
295 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t
));
298 /* Fix up fields of a decl_with_vis T. */
301 lto_ft_decl_with_vis (tree t
)
303 lto_ft_decl_common (t
);
305 /* Accessor macro has side-effects, use field-name here. */
306 LTO_FIXUP_TREE (t
->decl_with_vis
.assembler_name
);
307 LTO_FIXUP_TREE (DECL_SECTION_NAME (t
));
310 /* Fix up fields of a decl_non_common T. */
313 lto_ft_decl_non_common (tree t
)
315 lto_ft_decl_with_vis (t
);
316 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t
));
317 LTO_FIXUP_TREE (DECL_RESULT_FLD (t
));
318 LTO_FIXUP_TREE (DECL_VINDEX (t
));
321 /* Fix up fields of a decl_non_common T. */
324 lto_ft_function (tree t
)
326 lto_ft_decl_non_common (t
);
327 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t
));
330 /* Fix up fields of a field_decl T. */
333 lto_ft_field_decl (tree t
)
335 lto_ft_decl_common (t
);
336 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t
));
337 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t
));
338 LTO_FIXUP_TREE (DECL_QUALIFIER (t
));
339 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t
));
340 LTO_FIXUP_TREE (DECL_FCONTEXT (t
));
343 /* Fix up fields of a type T. */
349 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t
));
350 LTO_FIXUP_TREE (TYPE_SIZE (t
));
351 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t
));
352 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t
));
353 LTO_FIXUP_TREE (TYPE_NAME (t
));
355 /* Accessors are for derived node types only. */
356 if (!POINTER_TYPE_P (t
))
357 LTO_FIXUP_TREE (TYPE_MINVAL (t
));
358 LTO_FIXUP_TREE (TYPE_MAXVAL (t
));
360 /* Accessor is for derived node types only. */
361 LTO_FIXUP_TREE (t
->type_non_common
.binfo
);
363 LTO_FIXUP_TREE (TYPE_CONTEXT (t
));
366 /* Fix up fields of a BINFO T. */
369 lto_ft_binfo (tree t
)
371 unsigned HOST_WIDE_INT i
, n
;
372 tree base
, saved_base
;
375 LTO_FIXUP_TREE (BINFO_VTABLE (t
));
376 LTO_FIXUP_TREE (BINFO_OFFSET (t
));
377 LTO_FIXUP_TREE (BINFO_VIRTUALS (t
));
378 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t
));
379 n
= VEC_length (tree
, BINFO_BASE_ACCESSES (t
));
380 for (i
= 0; i
< n
; i
++)
382 saved_base
= base
= BINFO_BASE_ACCESS (t
, i
);
383 LTO_FIXUP_TREE (base
);
384 if (base
!= saved_base
)
385 VEC_replace (tree
, BINFO_BASE_ACCESSES (t
), i
, base
);
387 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t
));
388 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t
));
389 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t
));
390 n
= BINFO_N_BASE_BINFOS (t
);
391 for (i
= 0; i
< n
; i
++)
393 saved_base
= base
= BINFO_BASE_BINFO (t
, i
);
394 LTO_FIXUP_TREE (base
);
395 if (base
!= saved_base
)
396 VEC_replace (tree
, BINFO_BASE_BINFOS (t
), i
, base
);
400 /* Fix up fields of a CONSTRUCTOR T. */
403 lto_ft_constructor (tree t
)
405 unsigned HOST_WIDE_INT idx
;
411 VEC_iterate(constructor_elt
, CONSTRUCTOR_ELTS (t
), idx
, ce
);
414 LTO_FIXUP_TREE (ce
->index
);
415 LTO_FIXUP_TREE (ce
->value
);
419 /* Fix up fields of an expression tree T. */
426 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
427 LTO_FIXUP_TREE (TREE_OPERAND (t
, i
));
430 /* Given a tree T fixup fields of T by replacing types with their merged
431 variant and other entities by an equal entity from an earlier compilation
432 unit, or an entity being canonical in a different way. This includes
433 for instance integer or string constants. */
436 lto_fixup_types (tree t
)
438 switch (TREE_CODE (t
))
440 case IDENTIFIER_NODE
:
444 LTO_FIXUP_TREE (TREE_VALUE (t
));
445 LTO_FIXUP_TREE (TREE_PURPOSE (t
));
446 LTO_FIXUP_TREE (TREE_CHAIN (t
));
450 lto_ft_field_decl (t
);
458 lto_ft_decl_common (t
);
462 lto_ft_decl_with_vis (t
);
466 lto_ft_decl_non_common (t
);
477 case PLACEHOLDER_EXPR
:
482 case TRANSLATION_UNIT_DECL
:
483 case OPTIMIZATION_NODE
:
484 case TARGET_OPTION_NODE
:
490 else if (TREE_CODE (t
) == CONSTRUCTOR
)
491 lto_ft_constructor (t
);
492 else if (CONSTANT_CLASS_P (t
))
493 LTO_FIXUP_TREE (TREE_TYPE (t
));
500 remember_with_vars (t
);
506 /* Return the resolution for the decl with index INDEX from DATA_IN. */
508 static enum ld_plugin_symbol_resolution
509 get_resolution (struct data_in
*data_in
, unsigned index
)
511 if (data_in
->globals_resolution
)
513 ld_plugin_symbol_resolution_t ret
;
514 /* We can have references to not emitted functions in
515 DECL_FUNCTION_PERSONALITY at least. So we can and have
516 to indeed return LDPR_UNKNOWN in some cases. */
517 if (VEC_length (ld_plugin_symbol_resolution_t
,
518 data_in
->globals_resolution
) <= index
)
520 ret
= VEC_index (ld_plugin_symbol_resolution_t
,
521 data_in
->globals_resolution
,
526 /* Delay resolution finding until decl merging. */
531 /* Register DECL with the global symbol table and change its
532 name if necessary to avoid name clashes for static globals across
536 lto_register_var_decl_in_symtab (struct data_in
*data_in
, tree decl
)
540 /* Variable has file scope, not local. Need to ensure static variables
541 between different files don't clash unexpectedly. */
542 if (!TREE_PUBLIC (decl
)
543 && !((context
= decl_function_context (decl
))
544 && auto_var_in_fn_p (decl
, context
)))
546 /* ??? We normally pre-mangle names before we serialize them
547 out. Here, in lto1, we do not know the language, and
548 thus cannot do the mangling again. Instead, we just
549 append a suffix to the mangled name. The resulting name,
550 however, is not a properly-formed mangled name, and will
551 confuse any attempt to unmangle it. */
552 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
555 ASM_FORMAT_PRIVATE_NAME (label
, name
, DECL_UID (decl
));
556 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (label
));
557 rest_of_decl_compilation (decl
, 1, 0);
558 VEC_safe_push (tree
, gc
, lto_global_var_decls
, decl
);
561 /* If this variable has already been declared, queue the
562 declaration for merging. */
563 if (TREE_PUBLIC (decl
))
566 if (!streamer_tree_cache_lookup (data_in
->reader_cache
, decl
, &ix
))
568 lto_symtab_register_decl (decl
, get_resolution (data_in
, ix
),
574 /* Register DECL with the global symbol table and change its
575 name if necessary to avoid name clashes for static globals across
576 different files. DATA_IN contains descriptors and tables for the
580 lto_register_function_decl_in_symtab (struct data_in
*data_in
, tree decl
)
582 /* Need to ensure static entities between different files
583 don't clash unexpectedly. */
584 if (!TREE_PUBLIC (decl
))
586 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
587 may set the assembler name where it was previously empty. */
588 tree old_assembler_name
= decl
->decl_with_vis
.assembler_name
;
590 /* FIXME lto: We normally pre-mangle names before we serialize
591 them out. Here, in lto1, we do not know the language, and
592 thus cannot do the mangling again. Instead, we just append a
593 suffix to the mangled name. The resulting name, however, is
594 not a properly-formed mangled name, and will confuse any
595 attempt to unmangle it. */
596 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
599 ASM_FORMAT_PRIVATE_NAME (label
, name
, DECL_UID (decl
));
600 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (label
));
602 /* We may arrive here with the old assembler name not set
603 if the function body is not needed, e.g., it has been
604 inlined away and does not appear in the cgraph. */
605 if (old_assembler_name
)
607 tree new_assembler_name
= DECL_ASSEMBLER_NAME (decl
);
609 /* Make the original assembler name available for later use.
610 We may have used it to indicate the section within its
611 object file where the function body may be found.
612 FIXME lto: Find a better way to maintain the function decl
613 to body section mapping so we don't need this hack. */
614 lto_record_renamed_decl (data_in
->file_data
,
615 IDENTIFIER_POINTER (old_assembler_name
),
616 IDENTIFIER_POINTER (new_assembler_name
));
618 /* Also register the reverse mapping so that we can find the
619 new name given to an existing assembler name (used when
620 restoring alias pairs in input_constructors_or_inits. */
621 lto_record_renamed_decl (data_in
->file_data
,
622 IDENTIFIER_POINTER (new_assembler_name
),
623 IDENTIFIER_POINTER (old_assembler_name
));
627 /* If this variable has already been declared, queue the
628 declaration for merging. */
629 if (TREE_PUBLIC (decl
) && !DECL_ABSTRACT (decl
))
632 if (!streamer_tree_cache_lookup (data_in
->reader_cache
, decl
, &ix
))
634 lto_symtab_register_decl (decl
, get_resolution (data_in
, ix
),
640 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
641 for one compilation unit) go over all trees starting at index FROM until the
642 end of the sequence and replace fields of those trees, and the trees
643 themself with their canonical variants as per gimple_register_type. */
646 uniquify_nodes (struct data_in
*data_in
, unsigned from
)
648 struct streamer_tree_cache_d
*cache
= data_in
->reader_cache
;
649 unsigned len
= VEC_length (tree
, cache
->nodes
);
652 /* Go backwards because children streamed for the first time come
653 as part of their parents, and hence are created after them. */
655 /* First register all the types in the cache. This makes sure to
656 have the original structure in the type cycles when registering
657 them and computing hashes. */
658 for (i
= len
; i
-- > from
;)
660 tree t
= VEC_index (tree
, cache
->nodes
, i
);
662 gimple_register_type (t
);
665 /* Second fixup all trees in the new cache entries. */
666 for (i
= len
; i
-- > from
;)
668 tree t
= VEC_index (tree
, cache
->nodes
, i
);
673 /* First fixup the fields of T. */
679 /* Now try to find a canonical variant of T itself. */
680 t
= gimple_register_type (t
);
684 /* The following re-creates proper variant lists while fixing up
685 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
686 variant list state before fixup is broken. */
689 /* Remove us from our main variant list if we are not the
691 if (TYPE_MAIN_VARIANT (t
) != t
)
693 tem
= TYPE_MAIN_VARIANT (t
);
694 while (tem
&& TYPE_NEXT_VARIANT (tem
) != t
)
695 tem
= TYPE_NEXT_VARIANT (tem
);
697 TYPE_NEXT_VARIANT (tem
) = TYPE_NEXT_VARIANT (t
);
698 TYPE_NEXT_VARIANT (t
) = NULL_TREE
;
701 /* Query our new main variant. */
702 mv
= gimple_register_type (TYPE_MAIN_VARIANT (t
));
704 /* If we were the variant leader and we get replaced ourselves drop
705 all variants from our list. */
706 if (TYPE_MAIN_VARIANT (t
) == t
712 tree tem2
= TYPE_NEXT_VARIANT (tem
);
713 TYPE_NEXT_VARIANT (tem
) = NULL_TREE
;
718 /* If we are not our own variant leader link us into our new leaders
722 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
723 TYPE_NEXT_VARIANT (mv
) = t
;
724 if (RECORD_OR_UNION_TYPE_P (t
))
725 TYPE_BINFO (t
) = TYPE_BINFO (mv
);
728 /* Finally adjust our main variant and fix it up. */
729 TYPE_MAIN_VARIANT (t
) = mv
;
731 /* The following reconstructs the pointer chains
732 of the new pointed-to type if we are a main variant. We do
733 not stream those so they are broken before fixup. */
734 if (TREE_CODE (t
) == POINTER_TYPE
735 && TYPE_MAIN_VARIANT (t
) == t
)
737 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
738 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
740 else if (TREE_CODE (t
) == REFERENCE_TYPE
741 && TYPE_MAIN_VARIANT (t
) == t
)
743 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
744 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
750 if (RECORD_OR_UNION_TYPE_P (t
))
753 if (TYPE_FIELDS (t
) != TYPE_FIELDS (oldt
))
754 for (f1
= TYPE_FIELDS (t
), f2
= TYPE_FIELDS (oldt
);
755 f1
&& f2
; f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
758 gcc_assert (f1
!= f2
&& DECL_NAME (f1
) == DECL_NAME (f2
));
759 if (!streamer_tree_cache_lookup (cache
, f2
, &ix
))
761 /* If we're going to replace an element which we'd
762 still visit in the next iterations, we wouldn't
763 handle it, so do it here. We do have to handle it
764 even though the field_decl itself will be removed,
765 as it could refer to e.g. integer_cst which we
766 wouldn't reach via any other way, hence they
767 (and their type) would stay uncollected. */
768 /* ??? We should rather make sure to replace all
769 references to f2 with f1. That means handling
770 COMPONENT_REFs and CONSTRUCTOR elements in
771 lto_fixup_types and special-case the field-decl
774 lto_fixup_types (f2
);
775 streamer_tree_cache_insert_at (cache
, f1
, ix
);
779 /* If we found a tree that is equal to oldt replace it in the
780 cache, so that further users (in the various LTO sections)
782 streamer_tree_cache_insert_at (cache
, t
, i
);
786 /* Finally compute the canonical type of all TREE_TYPEs and register
787 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
788 From this point there are no longer any types with
789 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
790 This step requires the TYPE_POINTER_TO lists being present, so
791 make sure it is done last. */
792 for (i
= len
; i
-- > from
;)
794 tree t
= VEC_index (tree
, cache
->nodes
, i
);
798 if (TREE_CODE (t
) == VAR_DECL
)
799 lto_register_var_decl_in_symtab (data_in
, t
);
800 else if (TREE_CODE (t
) == FUNCTION_DECL
&& !DECL_BUILT_IN (t
))
801 lto_register_function_decl_in_symtab (data_in
, t
);
802 else if (TYPE_P (t
) && !TYPE_CANONICAL (t
))
803 TYPE_CANONICAL (t
) = gimple_register_canonical_type (t
);
808 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
809 RESOLUTIONS is the set of symbols picked by the linker (read from the
810 resolution file when the linker plugin is being used). */
813 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
814 VEC(ld_plugin_symbol_resolution_t
,heap
) *resolutions
)
816 const struct lto_decl_header
*header
= (const struct lto_decl_header
*) data
;
817 const int32_t decl_offset
= sizeof (struct lto_decl_header
);
818 const int32_t main_offset
= decl_offset
+ header
->decl_state_size
;
819 const int32_t string_offset
= main_offset
+ header
->main_size
;
820 struct lto_input_block ib_main
;
821 struct data_in
*data_in
;
823 const uint32_t *data_ptr
, *data_end
;
824 uint32_t num_decl_states
;
826 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
829 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
830 header
->string_size
, resolutions
);
832 /* Read the global declarations and types. */
833 while (ib_main
.p
< ib_main
.len
)
836 unsigned from
= VEC_length (tree
, data_in
->reader_cache
->nodes
);
837 t
= stream_read_tree (&ib_main
, data_in
);
838 gcc_assert (t
&& ib_main
.p
<= ib_main
.len
);
839 uniquify_nodes (data_in
, from
);
842 /* Read in lto_in_decl_state objects. */
843 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
845 (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
846 num_decl_states
= *data_ptr
++;
848 gcc_assert (num_decl_states
> 0);
849 decl_data
->global_decl_state
= lto_new_in_decl_state ();
850 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
,
851 decl_data
->global_decl_state
);
853 /* Read in per-function decl states and enter them in hash table. */
854 decl_data
->function_decl_states
=
855 htab_create_ggc (37, lto_hash_in_decl_state
, lto_eq_in_decl_state
, NULL
);
857 for (i
= 1; i
< num_decl_states
; i
++)
859 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
862 data_ptr
= lto_read_in_decl_state (data_in
, data_ptr
, state
);
863 slot
= htab_find_slot (decl_data
->function_decl_states
, state
, INSERT
);
864 gcc_assert (*slot
== NULL
);
868 if (data_ptr
!= data_end
)
869 internal_error ("bytecode stream: garbage at the end of symbols section");
871 /* Set the current decl state to be the global state. */
872 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
874 lto_data_in_delete (data_in
);
877 /* strtoll is not portable. */
879 lto_parse_hex (const char *p
) {
881 for (; *p
!= '\0'; ++p
)
886 if (c
>= '0' && c
<= '9')
888 else if (c
>= 'a' && c
<= 'f')
890 else if (c
>= 'A' && c
<= 'F')
893 internal_error ("could not parse hex number");
899 /* Read resolution for file named FILE_NAME. The resolution is read from
903 lto_resolution_read (splay_tree file_ids
, FILE *resolution
, lto_file
*file
)
905 /* We require that objects in the resolution file are in the same
906 order as the lto1 command line. */
907 unsigned int name_len
;
909 unsigned int num_symbols
;
911 struct lto_file_decl_data
*file_data
;
912 unsigned max_index
= 0;
913 splay_tree_node nd
= NULL
;
918 name_len
= strlen (file
->filename
);
919 obj_name
= XNEWVEC (char, name_len
+ 1);
920 fscanf (resolution
, " "); /* Read white space. */
922 fread (obj_name
, sizeof (char), name_len
, resolution
);
923 obj_name
[name_len
] = '\0';
924 if (filename_cmp (obj_name
, file
->filename
) != 0)
925 internal_error ("unexpected file name %s in linker resolution file. "
926 "Expected %s", obj_name
, file
->filename
);
927 if (file
->offset
!= 0)
932 t
= fscanf (resolution
, "@0x%16s", offset_p
);
934 internal_error ("could not parse file offset");
935 offset
= lto_parse_hex (offset_p
);
936 if (offset
!= file
->offset
)
937 internal_error ("unexpected offset");
942 fscanf (resolution
, "%u", &num_symbols
);
944 for (i
= 0; i
< num_symbols
; i
++)
949 enum ld_plugin_symbol_resolution r
= (enum ld_plugin_symbol_resolution
) 0;
951 unsigned int lto_resolution_str_len
=
952 sizeof (lto_resolution_str
) / sizeof (char *);
954 t
= fscanf (resolution
, "%u %x %26s %*[^\n]\n", &index
, &id
, r_str
);
956 internal_error ("invalid line in the resolution file");
957 if (index
> max_index
)
960 for (j
= 0; j
< lto_resolution_str_len
; j
++)
962 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
964 r
= (enum ld_plugin_symbol_resolution
) j
;
968 if (j
== lto_resolution_str_len
)
969 internal_error ("invalid resolution in the resolution file");
971 if (!(nd
&& nd
->key
== id
))
973 nd
= splay_tree_lookup (file_ids
, id
);
975 internal_error ("resolution sub id %x not in object file", id
);
978 file_data
= (struct lto_file_decl_data
*)nd
->value
;
979 if (cgraph_dump_file
)
980 fprintf (cgraph_dump_file
, "Adding resolution %u %u to id %x\n",
981 index
, r
, file_data
->id
);
982 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t
, heap
,
983 file_data
->resolutions
,
985 VEC_replace (ld_plugin_symbol_resolution_t
,
986 file_data
->resolutions
, index
, r
);
990 /* Is the name for a id'ed LTO section? */
993 lto_section_with_id (const char *name
, unsigned *id
)
997 if (strncmp (name
, LTO_SECTION_NAME_PREFIX
, strlen (LTO_SECTION_NAME_PREFIX
)))
999 s
= strrchr (name
, '.');
1000 return s
&& sscanf (s
, ".%x", id
) == 1;
1003 /* Create file_data of each sub file id */
1006 create_subid_section_table (void **slot
, void *data
)
1008 struct lto_section_slot s_slot
, *new_slot
;
1009 struct lto_section_slot
*ls
= *(struct lto_section_slot
**)slot
;
1010 splay_tree file_ids
= (splay_tree
)data
;
1015 struct lto_file_decl_data
*file_data
;
1017 if (!lto_section_with_id (ls
->name
, &id
))
1020 /* Find hash table of sub module id */
1021 nd
= splay_tree_lookup (file_ids
, id
);
1024 file_data
= (struct lto_file_decl_data
*)nd
->value
;
1028 file_data
= ggc_alloc_lto_file_decl_data ();
1029 memset(file_data
, 0, sizeof (struct lto_file_decl_data
));
1031 file_data
->section_hash_table
= lto_obj_create_section_hash_table ();;
1032 splay_tree_insert (file_ids
, id
, (splay_tree_value
)file_data
);
1035 /* Copy section into sub module hash table */
1036 new_name
= XDUPVEC (char, ls
->name
, strlen (ls
->name
) + 1);
1037 s_slot
.name
= new_name
;
1038 hash_slot
= htab_find_slot (file_data
->section_hash_table
, &s_slot
, INSERT
);
1039 gcc_assert (*hash_slot
== NULL
);
1041 new_slot
= XDUP (struct lto_section_slot
, ls
);
1042 new_slot
->name
= new_name
;
1043 *hash_slot
= new_slot
;
1047 /* Read declarations and other initializations for a FILE_DATA. */
1050 lto_file_finalize (struct lto_file_decl_data
*file_data
, lto_file
*file
)
1055 file_data
->renaming_hash_table
= lto_create_renaming_table ();
1056 file_data
->file_name
= file
->filename
;
1057 data
= lto_get_section_data (file_data
, LTO_section_decls
, NULL
, &len
);
1060 internal_error ("cannot read LTO decls from %s", file_data
->file_name
);
1063 lto_read_decls (file_data
, data
, file_data
->resolutions
);
1064 lto_free_section_data (file_data
, LTO_section_decls
, NULL
, data
, len
);
1070 struct lto_file_decl_data
**file_data
;
1074 /* Traverse ids and create a list of file_datas out of it. */
1076 static int lto_create_files_from_ids (splay_tree_node node
, void *data
)
1078 struct lwstate
*lw
= (struct lwstate
*)data
;
1079 struct lto_file_decl_data
*file_data
= (struct lto_file_decl_data
*)node
->value
;
1081 lto_file_finalize (file_data
, lw
->file
);
1082 if (cgraph_dump_file
)
1083 fprintf (cgraph_dump_file
, "Creating file %s with sub id %x\n",
1084 file_data
->file_name
, file_data
->id
);
1085 file_data
->next
= *lw
->file_data
;
1086 *lw
->file_data
= file_data
;
1091 /* Generate a TREE representation for all types and external decls
1094 Read all of the globals out of the file. Then read the cgraph
1095 and process the .o index into the cgraph nodes so that it can open
1096 the .o file to load the functions and ipa information. */
1098 static struct lto_file_decl_data
*
1099 lto_file_read (lto_file
*file
, FILE *resolution_file
, int *count
)
1101 struct lto_file_decl_data
*file_data
= NULL
;
1102 splay_tree file_ids
;
1103 htab_t section_hash_table
;
1104 struct lwstate state
;
1106 section_hash_table
= lto_obj_build_section_table (file
);
1108 /* Find all sub modules in the object and put their sections into new hash
1109 tables in a splay tree. */
1110 file_ids
= splay_tree_new (splay_tree_compare_ints
, NULL
, NULL
);
1111 htab_traverse (section_hash_table
, create_subid_section_table
, file_ids
);
1113 /* Add resolutions to file ids */
1114 lto_resolution_read (file_ids
, resolution_file
, file
);
1116 /* Finalize each lto file for each submodule in the merged object
1117 and create list for returning. */
1119 state
.file_data
= &file_data
;
1120 state
.count
= count
;
1121 splay_tree_foreach (file_ids
, lto_create_files_from_ids
, &state
);
1123 splay_tree_delete (file_ids
);
1124 htab_delete (section_hash_table
);
1129 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
1130 #define LTO_MMAP_IO 1
1134 /* Page size of machine is used for mmap and munmap calls. */
1135 static size_t page_mask
;
1138 /* Get the section data of length LEN from FILENAME starting at
1139 OFFSET. The data segment must be freed by the caller when the
1140 caller is finished. Returns NULL if all was not well. */
1143 lto_read_section_data (struct lto_file_decl_data
*file_data
,
1144 intptr_t offset
, size_t len
)
1148 static char *fd_name
;
1150 intptr_t computed_len
;
1151 intptr_t computed_offset
;
1155 /* Keep a single-entry file-descriptor cache. The last file we
1156 touched will get closed at exit.
1157 ??? Eventually we want to add a more sophisticated larger cache
1158 or rather fix function body streaming to not stream them in
1159 practically random order. */
1161 && filename_cmp (fd_name
, file_data
->file_name
) != 0)
1169 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
1172 fd_name
= xstrdup (file_data
->file_name
);
1178 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
1179 page_mask
= ~(page_size
- 1);
1182 computed_offset
= offset
& page_mask
;
1183 diff
= offset
- computed_offset
;
1184 computed_len
= len
+ diff
;
1186 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
1187 fd
, computed_offset
);
1188 if (result
== MAP_FAILED
)
1191 return result
+ diff
;
1193 result
= (char *) xmalloc (len
);
1194 if (lseek (fd
, offset
, SEEK_SET
) != offset
1195 || read (fd
, result
, len
) != (ssize_t
) len
)
1201 /* Native windows doesn't supports delayed unlink on opened file. So
1202 we close file here again. This produces higher I/O load, but at least
1203 it prevents to have dangling file handles preventing unlink. */
1214 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1215 NAME will be NULL unless the section type is for a function
1219 get_section_data (struct lto_file_decl_data
*file_data
,
1220 enum lto_section_type section_type
,
1224 htab_t section_hash_table
= file_data
->section_hash_table
;
1225 struct lto_section_slot
*f_slot
;
1226 struct lto_section_slot s_slot
;
1227 const char *section_name
= lto_get_section_name (section_type
, name
, file_data
);
1231 s_slot
.name
= section_name
;
1232 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
1235 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
1239 free (CONST_CAST (char *, section_name
));
1244 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1245 starts at OFFSET and has LEN bytes. */
1248 free_section_data (struct lto_file_decl_data
*file_data ATTRIBUTE_UNUSED
,
1249 enum lto_section_type section_type ATTRIBUTE_UNUSED
,
1250 const char *name ATTRIBUTE_UNUSED
,
1251 const char *offset
, size_t len ATTRIBUTE_UNUSED
)
1254 intptr_t computed_len
;
1255 intptr_t computed_offset
;
1260 computed_offset
= ((intptr_t) offset
) & page_mask
;
1261 diff
= (intptr_t) offset
- computed_offset
;
1262 computed_len
= len
+ diff
;
1264 munmap ((caddr_t
) computed_offset
, computed_len
);
1266 free (CONST_CAST(char *, offset
));
1270 /* Structure describing ltrans partitions. */
1272 struct ltrans_partition_def
1274 cgraph_node_set cgraph_set
;
1275 varpool_node_set varpool_set
;
1280 typedef struct ltrans_partition_def
*ltrans_partition
;
1281 DEF_VEC_P(ltrans_partition
);
1282 DEF_VEC_ALLOC_P(ltrans_partition
,heap
);
1284 static VEC(ltrans_partition
, heap
) *ltrans_partitions
;
1286 static void add_cgraph_node_to_partition (ltrans_partition part
, struct cgraph_node
*node
);
1287 static void add_varpool_node_to_partition (ltrans_partition part
, struct varpool_node
*vnode
);
1289 /* Create new partition with name NAME. */
1290 static ltrans_partition
1291 new_partition (const char *name
)
1293 ltrans_partition part
= XCNEW (struct ltrans_partition_def
);
1294 part
->cgraph_set
= cgraph_node_set_new ();
1295 part
->varpool_set
= varpool_node_set_new ();
1298 VEC_safe_push (ltrans_partition
, heap
, ltrans_partitions
, part
);
1302 /* Free memory used by ltrans datastructures. */
1304 free_ltrans_partitions (void)
1307 ltrans_partition part
;
1308 for (idx
= 0; VEC_iterate (ltrans_partition
, ltrans_partitions
, idx
, part
); idx
++)
1310 free_cgraph_node_set (part
->cgraph_set
);
1313 VEC_free (ltrans_partition
, heap
, ltrans_partitions
);
1316 /* See all references that go to comdat objects and bring them into partition too. */
1318 add_references_to_partition (ltrans_partition part
, struct ipa_ref_list
*refs
)
1321 struct ipa_ref
*ref
;
1322 for (i
= 0; ipa_ref_list_reference_iterate (refs
, i
, ref
); i
++)
1324 if (ref
->refered_type
== IPA_REF_CGRAPH
1325 && DECL_COMDAT (cgraph_function_node (ipa_ref_node (ref
), NULL
)->decl
)
1326 && !cgraph_node_in_set_p (ipa_ref_node (ref
), part
->cgraph_set
))
1327 add_cgraph_node_to_partition (part
, ipa_ref_node (ref
));
1329 if (ref
->refered_type
== IPA_REF_VARPOOL
1330 && DECL_COMDAT (ipa_ref_varpool_node (ref
)->decl
)
1331 && !varpool_node_in_set_p (ipa_ref_varpool_node (ref
), part
->varpool_set
))
1332 add_varpool_node_to_partition (part
, ipa_ref_varpool_node (ref
));
1336 /* Worker for add_cgraph_node_to_partition. */
1339 add_cgraph_node_to_partition_1 (struct cgraph_node
*node
, void *data
)
1341 ltrans_partition part
= (ltrans_partition
) data
;
1343 /* non-COMDAT aliases of COMDAT functions needs to be output just once. */
1344 if (!DECL_COMDAT (node
->decl
)
1345 && !node
->global
.inlined_to
1348 gcc_assert (node
->thunk
.thunk_p
|| node
->alias
);
1354 node
->in_other_partition
= 1;
1355 if (cgraph_dump_file
)
1356 fprintf (cgraph_dump_file
, "Node %s/%i now used in multiple partitions\n",
1357 cgraph_node_name (node
), node
->uid
);
1359 node
->aux
= (void *)((size_t)node
->aux
+ 1);
1360 cgraph_node_set_add (part
->cgraph_set
, node
);
1364 /* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */
1367 add_cgraph_node_to_partition (ltrans_partition part
, struct cgraph_node
*node
)
1369 struct cgraph_edge
*e
;
1370 cgraph_node_set_iterator csi
;
1371 struct cgraph_node
*n
;
1373 /* We always decide on functions, not associated thunks and aliases. */
1374 node
= cgraph_function_node (node
, NULL
);
1376 /* If NODE is already there, we have nothing to do. */
1377 csi
= cgraph_node_set_find (part
->cgraph_set
, node
);
1378 if (!csi_end_p (csi
))
1381 cgraph_for_node_thunks_and_aliases (node
, add_cgraph_node_to_partition_1
, part
, true);
1383 part
->insns
+= inline_summary (node
)->self_size
;
1386 cgraph_node_set_add (part
->cgraph_set
, node
);
1388 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1389 if ((!e
->inline_failed
1390 || DECL_COMDAT (cgraph_function_node (e
->callee
, NULL
)->decl
))
1391 && !cgraph_node_in_set_p (e
->callee
, part
->cgraph_set
))
1392 add_cgraph_node_to_partition (part
, e
->callee
);
1394 add_references_to_partition (part
, &node
->ref_list
);
1396 if (node
->same_comdat_group
)
1397 for (n
= node
->same_comdat_group
; n
!= node
; n
= n
->same_comdat_group
)
1398 add_cgraph_node_to_partition (part
, n
);
1401 /* Add VNODE to partition as well as comdat references partition PART. */
1404 add_varpool_node_to_partition (ltrans_partition part
, struct varpool_node
*vnode
)
1406 varpool_node_set_iterator vsi
;
1408 /* If NODE is already there, we have nothing to do. */
1409 vsi
= varpool_node_set_find (part
->varpool_set
, vnode
);
1410 if (!vsi_end_p (vsi
))
1413 varpool_node_set_add (part
->varpool_set
, vnode
);
1417 vnode
->in_other_partition
= 1;
1418 if (cgraph_dump_file
)
1419 fprintf (cgraph_dump_file
, "Varpool node %s now used in multiple partitions\n",
1420 varpool_node_name (vnode
));
1422 vnode
->aux
= (void *)((size_t)vnode
->aux
+ 1);
1424 add_references_to_partition (part
, &vnode
->ref_list
);
1426 if (vnode
->same_comdat_group
1427 && !varpool_node_in_set_p (vnode
->same_comdat_group
, part
->varpool_set
))
1428 add_varpool_node_to_partition (part
, vnode
->same_comdat_group
);
1431 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
1432 and number of varpool nodes is N_VARPOOL_NODES. */
1435 undo_partition (ltrans_partition partition
, unsigned int n_cgraph_nodes
,
1436 unsigned int n_varpool_nodes
)
1438 while (VEC_length (cgraph_node_ptr
, partition
->cgraph_set
->nodes
) >
1441 struct cgraph_node
*node
= VEC_index (cgraph_node_ptr
,
1442 partition
->cgraph_set
->nodes
,
1444 partition
->insns
-= inline_summary (node
)->self_size
;
1445 cgraph_node_set_remove (partition
->cgraph_set
, node
);
1446 node
->aux
= (void *)((size_t)node
->aux
- 1);
1448 while (VEC_length (varpool_node_ptr
, partition
->varpool_set
->nodes
) >
1451 struct varpool_node
*node
= VEC_index (varpool_node_ptr
,
1452 partition
->varpool_set
->nodes
,
1454 varpool_node_set_remove (partition
->varpool_set
, node
);
1455 node
->aux
= (void *)((size_t)node
->aux
- 1);
1459 /* Return true if NODE should be partitioned.
1460 This means that partitioning algorithm should put NODE into one of partitions.
1461 This apply to most functions with bodies. Functions that are not partitions
1462 are put into every unit needing them. This is the case of i.e. COMDATs. */
1465 partition_cgraph_node_p (struct cgraph_node
*node
)
1467 /* We will get proper partition based on function they are inlined to. */
1468 if (node
->global
.inlined_to
)
1470 /* Nodes without a body do not need partitioning. */
1471 if (!node
->analyzed
)
1473 /* Extern inlines and comdat are always only in partitions they are needed. */
1474 if (DECL_EXTERNAL (node
->decl
)
1475 || (DECL_COMDAT (node
->decl
)
1476 && !cgraph_used_from_object_file_p (node
)))
1478 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
1483 /* Return true if VNODE should be partitioned.
1484 This means that partitioning algorithm should put VNODE into one of partitions. */
1487 partition_varpool_node_p (struct varpool_node
*vnode
)
1489 if (vnode
->alias
|| !vnode
->needed
)
1491 /* Constant pool and comdat are always only in partitions they are needed. */
1492 if (DECL_IN_CONSTANT_POOL (vnode
->decl
)
1493 || (DECL_COMDAT (vnode
->decl
)
1494 && !vnode
->force_output
1495 && !varpool_used_from_object_file_p (vnode
)))
1497 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode
->decl
)))
1502 /* Group cgrah nodes by input files. This is used mainly for testing
1506 lto_1_to_1_map (void)
1508 struct cgraph_node
*node
;
1509 struct varpool_node
*vnode
;
1510 struct lto_file_decl_data
*file_data
;
1511 struct pointer_map_t
*pmap
;
1512 ltrans_partition partition
;
1514 int npartitions
= 0;
1516 timevar_push (TV_WHOPR_WPA
);
1518 pmap
= pointer_map_create ();
1520 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1522 if (!partition_cgraph_node_p (node
)
1526 file_data
= node
->local
.lto_file_data
;
1530 slot
= pointer_map_contains (pmap
, file_data
);
1532 partition
= (ltrans_partition
) *slot
;
1535 partition
= new_partition (file_data
->file_name
);
1536 slot
= pointer_map_insert (pmap
, file_data
);
1542 && VEC_length (ltrans_partition
, ltrans_partitions
))
1543 partition
= VEC_index (ltrans_partition
, ltrans_partitions
, 0);
1546 partition
= new_partition ("");
1547 slot
= pointer_map_insert (pmap
, NULL
);
1552 add_cgraph_node_to_partition (partition
, node
);
1555 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
1557 if (!partition_varpool_node_p (vnode
)
1560 file_data
= vnode
->lto_file_data
;
1561 slot
= pointer_map_contains (pmap
, file_data
);
1563 partition
= (ltrans_partition
) *slot
;
1566 partition
= new_partition (file_data
->file_name
);
1567 slot
= pointer_map_insert (pmap
, file_data
);
1572 add_varpool_node_to_partition (partition
, vnode
);
1574 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1576 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
1579 /* If the cgraph is empty, create one cgraph node set so that there is still
1580 an output file for any variables that need to be exported in a DSO. */
1582 new_partition ("empty");
1584 pointer_map_destroy (pmap
);
1586 timevar_pop (TV_WHOPR_WPA
);
1588 lto_stats
.num_cgraph_partitions
+= VEC_length (ltrans_partition
,
1593 /* Group cgraph nodes into equally-sized partitions.
1595 The partitioning algorithm is simple: nodes are taken in predefined order.
1596 The order corresponds to the order we want functions to have in the final
1597 output. In the future this will be given by function reordering pass, but
1598 at the moment we use the topological order, which is a good approximation.
1600 The goal is to partition this linear order into intervals (partitions) so
1601 that all the partitions have approximately the same size and the number of
1602 callgraph or IPA reference edges crossing boundaries is minimal.
1604 This is a lot faster (O(n) in size of callgraph) than algorithms doing
1605 priority-based graph clustering that are generally O(n^2) and, since
1606 WHOPR is designed to make things go well across partitions, it leads
1609 We compute the expected size of a partition as:
1611 max (total_size / lto_partitions, min_partition_size)
1613 We use dynamic expected size of partition so small programs are partitioned
1614 into enough partitions to allow use of multiple CPUs, while large programs
1615 are not partitioned too much. Creating too many partitions significantly
1616 increases the streaming overhead.
1618 In the future, we would like to bound the maximal size of partitions so as
1619 to prevent the LTRANS stage from consuming too much memory. At the moment,
1620 however, the WPA stage is the most memory intensive for large benchmarks,
1621 since too many types and declarations are read into memory.
1623 The function implements a simple greedy algorithm. Nodes are being added
1624 to the current partition until after 3/4 of the expected partition size is
1625 reached. Past this threshold, we keep track of boundary size (number of
1626 edges going to other partitions) and continue adding functions until after
1627 the current partition has grown to twice the expected partition size. Then
1628 the process is undone to the point where the minimal ratio of boundary size
1629 and in-partition calls was reached. */
1632 lto_balanced_map (void)
1635 struct cgraph_node
**postorder
=
1636 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1637 struct cgraph_node
**order
= XNEWVEC (struct cgraph_node
*, cgraph_max_uid
);
1638 int i
, postorder_len
;
1639 struct cgraph_node
*node
;
1640 int total_size
= 0, best_total_size
= 0;
1642 ltrans_partition partition
;
1643 unsigned int last_visited_cgraph_node
= 0, last_visited_varpool_node
= 0;
1644 struct varpool_node
*vnode
;
1645 int cost
= 0, internal
= 0;
1646 int best_n_nodes
= 0, best_n_varpool_nodes
= 0, best_i
= 0, best_cost
=
1647 INT_MAX
, best_internal
= 0;
1650 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
1651 gcc_assert (!vnode
->aux
);
1652 /* Until we have better ordering facility, use toplogical order.
1653 Include only nodes we will partition and compute estimate of program
1654 size. Note that since nodes that are not partitioned might be put into
1655 multiple partitions, this is just an estimate of real size. This is why
1656 we keep partition_size updated after every partition is finalized. */
1657 postorder_len
= ipa_reverse_postorder (postorder
);
1658 for (i
= 0; i
< postorder_len
; i
++)
1660 node
= postorder
[i
];
1661 if (partition_cgraph_node_p (node
))
1663 order
[n_nodes
++] = node
;
1664 total_size
+= inline_summary (node
)->size
;
1669 /* Compute partition size and create the first partition. */
1670 partition_size
= total_size
/ PARAM_VALUE (PARAM_LTO_PARTITIONS
);
1671 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
1672 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
1674 partition
= new_partition ("");
1675 if (cgraph_dump_file
)
1676 fprintf (cgraph_dump_file
, "Total unit size: %i, partition size: %i\n",
1677 total_size
, partition_size
);
1679 for (i
= 0; i
< n_nodes
; i
++)
1683 add_cgraph_node_to_partition (partition
, order
[i
]);
1684 total_size
-= inline_summary (order
[i
])->size
;
1686 /* Once we added a new node to the partition, we also want to add
1687 all referenced variables unless they was already added into some
1689 add_cgraph_node_to_partition adds possibly multiple nodes and
1690 variables that are needed to satisfy needs of ORDER[i].
1691 We remember last visited cgraph and varpool node from last iteration
1692 of outer loop that allows us to process every new addition.
1694 At the same time we compute size of the boundary into COST. Every
1695 callgraph or IPA reference edge leaving the partition contributes into
1696 COST. Every edge inside partition was earlier computed as one leaving
1697 it and thus we need to subtract it from COST. */
1698 while (last_visited_cgraph_node
<
1699 VEC_length (cgraph_node_ptr
, partition
->cgraph_set
->nodes
)
1700 || last_visited_varpool_node
< VEC_length (varpool_node_ptr
,
1701 partition
->varpool_set
->
1704 struct ipa_ref_list
*refs
;
1706 struct ipa_ref
*ref
;
1707 bool cgraph_p
= false;
1709 if (last_visited_cgraph_node
<
1710 VEC_length (cgraph_node_ptr
, partition
->cgraph_set
->nodes
))
1712 struct cgraph_edge
*edge
;
1715 node
= VEC_index (cgraph_node_ptr
, partition
->cgraph_set
->nodes
,
1716 last_visited_cgraph_node
);
1717 refs
= &node
->ref_list
;
1719 last_visited_cgraph_node
++;
1721 gcc_assert (node
->analyzed
);
1723 /* Compute boundary cost of callgrpah edges. */
1724 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1725 if (edge
->callee
->analyzed
)
1727 int edge_cost
= edge
->frequency
;
1728 cgraph_node_set_iterator csi
;
1732 gcc_assert (edge_cost
> 0);
1733 csi
= cgraph_node_set_find (partition
->cgraph_set
, edge
->callee
);
1734 if (!csi_end_p (csi
)
1735 && csi
.index
< last_visited_cgraph_node
- 1)
1736 cost
-= edge_cost
, internal
+= edge_cost
;
1740 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
1742 int edge_cost
= edge
->frequency
;
1743 cgraph_node_set_iterator csi
;
1745 gcc_assert (edge
->caller
->analyzed
);
1748 gcc_assert (edge_cost
> 0);
1749 csi
= cgraph_node_set_find (partition
->cgraph_set
, edge
->caller
);
1750 if (!csi_end_p (csi
)
1751 && csi
.index
< last_visited_cgraph_node
)
1760 &VEC_index (varpool_node_ptr
, partition
->varpool_set
->nodes
,
1761 last_visited_varpool_node
)->ref_list
;
1762 last_visited_varpool_node
++;
1765 /* Compute boundary cost of IPA REF edges and at the same time look into
1766 variables referenced from current partition and try to add them. */
1767 for (j
= 0; ipa_ref_list_reference_iterate (refs
, j
, ref
); j
++)
1768 if (ref
->refered_type
== IPA_REF_VARPOOL
)
1770 varpool_node_set_iterator vsi
;
1772 vnode
= ipa_ref_varpool_node (ref
);
1773 if (!vnode
->finalized
)
1775 if (!vnode
->aux
&& partition_varpool_node_p (vnode
))
1776 add_varpool_node_to_partition (partition
, vnode
);
1777 vsi
= varpool_node_set_find (partition
->varpool_set
, vnode
);
1778 if (!vsi_end_p (vsi
)
1779 && vsi
.index
< last_visited_varpool_node
- !cgraph_p
)
1786 cgraph_node_set_iterator csi
;
1788 node
= ipa_ref_node (ref
);
1789 if (!node
->analyzed
)
1791 csi
= cgraph_node_set_find (partition
->cgraph_set
, node
);
1792 if (!csi_end_p (csi
)
1793 && csi
.index
< last_visited_cgraph_node
- cgraph_p
)
1798 for (j
= 0; ipa_ref_list_refering_iterate (refs
, j
, ref
); j
++)
1799 if (ref
->refering_type
== IPA_REF_VARPOOL
)
1801 varpool_node_set_iterator vsi
;
1803 vnode
= ipa_ref_refering_varpool_node (ref
);
1804 gcc_assert (vnode
->finalized
);
1805 if (!vnode
->aux
&& partition_varpool_node_p (vnode
))
1806 add_varpool_node_to_partition (partition
, vnode
);
1807 vsi
= varpool_node_set_find (partition
->varpool_set
, vnode
);
1808 if (!vsi_end_p (vsi
)
1809 && vsi
.index
< last_visited_varpool_node
)
1816 cgraph_node_set_iterator csi
;
1818 node
= ipa_ref_refering_node (ref
);
1819 gcc_assert (node
->analyzed
);
1820 csi
= cgraph_node_set_find (partition
->cgraph_set
, node
);
1821 if (!csi_end_p (csi
)
1822 && csi
.index
< last_visited_cgraph_node
)
1829 /* If the partition is large enough, start looking for smallest boundary cost. */
1830 if (partition
->insns
< partition_size
* 3 / 4
1831 || best_cost
== INT_MAX
1833 || (best_internal
* (HOST_WIDE_INT
) cost
1834 > (internal
* (HOST_WIDE_INT
)best_cost
)))
1835 && partition
->insns
< partition_size
* 5 / 4))
1838 best_internal
= internal
;
1840 best_n_nodes
= VEC_length (cgraph_node_ptr
,
1841 partition
->cgraph_set
->nodes
);
1842 best_n_varpool_nodes
= VEC_length (varpool_node_ptr
,
1843 partition
->varpool_set
->nodes
);
1844 best_total_size
= total_size
;
1846 if (cgraph_dump_file
)
1847 fprintf (cgraph_dump_file
, "Step %i: added %s/%i, size %i, cost %i/%i best %i/%i, step %i\n", i
,
1848 cgraph_node_name (order
[i
]), order
[i
]->uid
, partition
->insns
, cost
, internal
,
1849 best_cost
, best_internal
, best_i
);
1850 /* Partition is too large, unwind into step when best cost was reached and
1851 start new partition. */
1852 if (partition
->insns
> 2 * partition_size
)
1856 if (cgraph_dump_file
)
1857 fprintf (cgraph_dump_file
, "Unwinding %i insertions to step %i\n",
1858 i
- best_i
, best_i
);
1859 undo_partition (partition
, best_n_nodes
, best_n_varpool_nodes
);
1862 /* When we are finished, avoid creating empty partition. */
1863 while (i
< n_nodes
- 1 && order
[i
+ 1]->aux
)
1865 if (i
== n_nodes
- 1)
1867 partition
= new_partition ("");
1868 last_visited_cgraph_node
= 0;
1869 last_visited_varpool_node
= 0;
1870 total_size
= best_total_size
;
1873 if (cgraph_dump_file
)
1874 fprintf (cgraph_dump_file
, "New partition\n");
1876 best_n_varpool_nodes
= 0;
1877 best_cost
= INT_MAX
;
1879 /* Since the size of partitions is just approximate, update the size after
1880 we finished current one. */
1881 if (npartitions
< PARAM_VALUE (PARAM_LTO_PARTITIONS
))
1882 partition_size
= total_size
1883 / (PARAM_VALUE (PARAM_LTO_PARTITIONS
) - npartitions
);
1885 partition_size
= INT_MAX
;
1887 if (partition_size
< PARAM_VALUE (MIN_PARTITION_SIZE
))
1888 partition_size
= PARAM_VALUE (MIN_PARTITION_SIZE
);
1893 /* Varables that are not reachable from the code go into last partition. */
1894 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
1895 if (partition_varpool_node_p (vnode
) && !vnode
->aux
)
1896 add_varpool_node_to_partition (partition
, vnode
);
1900 /* Promote variable VNODE to be static. */
1903 promote_var (struct varpool_node
*vnode
)
1905 if (TREE_PUBLIC (vnode
->decl
) || DECL_EXTERNAL (vnode
->decl
))
1907 gcc_assert (flag_wpa
);
1908 TREE_PUBLIC (vnode
->decl
) = 1;
1909 DECL_VISIBILITY (vnode
->decl
) = VISIBILITY_HIDDEN
;
1910 DECL_VISIBILITY_SPECIFIED (vnode
->decl
) = true;
1911 if (cgraph_dump_file
)
1912 fprintf (cgraph_dump_file
,
1913 "Promoting var as hidden: %s\n", varpool_node_name (vnode
));
1917 /* Promote function NODE to be static. */
1920 promote_fn (struct cgraph_node
*node
)
1922 gcc_assert (flag_wpa
);
1923 if (TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
))
1925 TREE_PUBLIC (node
->decl
) = 1;
1926 DECL_VISIBILITY (node
->decl
) = VISIBILITY_HIDDEN
;
1927 DECL_VISIBILITY_SPECIFIED (node
->decl
) = true;
1928 if (cgraph_dump_file
)
1929 fprintf (cgraph_dump_file
,
1930 "Promoting function as hidden: %s/%i\n",
1931 cgraph_node_name (node
), node
->uid
);
1935 /* Find out all static decls that need to be promoted to global because
1936 of cross file sharing. This function must be run in the WPA mode after
1937 all inlinees are added. */
1940 lto_promote_cross_file_statics (void)
1942 struct varpool_node
*vnode
;
1944 cgraph_node_set set
;
1945 varpool_node_set vset
;
1946 cgraph_node_set_iterator csi
;
1947 varpool_node_set_iterator vsi
;
1948 VEC(varpool_node_ptr
, heap
) *promoted_initializers
= NULL
;
1949 struct pointer_set_t
*inserted
= pointer_set_create ();
1951 gcc_assert (flag_wpa
);
1953 n_sets
= VEC_length (ltrans_partition
, ltrans_partitions
);
1954 for (i
= 0; i
< n_sets
; i
++)
1956 ltrans_partition part
1957 = VEC_index (ltrans_partition
, ltrans_partitions
, i
);
1958 set
= part
->cgraph_set
;
1959 vset
= part
->varpool_set
;
1961 /* If node called or referred to from other partition, it needs to be
1963 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
1965 struct cgraph_node
*node
= csi_node (csi
);
1966 if (node
->local
.externally_visible
)
1968 if (node
->global
.inlined_to
)
1970 if ((!DECL_EXTERNAL (node
->decl
) && !DECL_COMDAT (node
->decl
))
1971 && (referenced_from_other_partition_p (&node
->ref_list
, set
, vset
)
1972 || reachable_from_other_partition_p (node
, set
)))
1975 for (vsi
= vsi_start (vset
); !vsi_end_p (vsi
); vsi_next (&vsi
))
1977 vnode
= vsi_node (vsi
);
1978 /* Constant pool references use internal labels and thus can not
1979 be made global. It is sensible to keep those ltrans local to
1980 allow better optimization. */
1981 if (!DECL_IN_CONSTANT_POOL (vnode
->decl
) && !DECL_COMDAT (vnode
->decl
)
1982 && !vnode
->externally_visible
&& vnode
->analyzed
1983 && referenced_from_other_partition_p (&vnode
->ref_list
,
1985 promote_var (vnode
);
1988 /* We export the initializer of a read-only var into each partition
1989 referencing the var. Folding might take declarations from the
1990 initializer and use them, so everything referenced from the
1991 initializer can be accessed from this partition after folding.
1993 This means that we need to promote all variables and functions
1994 referenced from all initializers of read-only vars referenced
1995 from this partition that are not in this partition. This needs
1996 to be done recursively. */
1997 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
1998 if (const_value_known_p (vnode
->decl
)
1999 && DECL_INITIAL (vnode
->decl
)
2000 && !varpool_node_in_set_p (vnode
, vset
)
2001 && referenced_from_this_partition_p (&vnode
->ref_list
, set
, vset
)
2002 && !pointer_set_insert (inserted
, vnode
))
2003 VEC_safe_push (varpool_node_ptr
, heap
, promoted_initializers
, vnode
);
2005 while (!VEC_empty (varpool_node_ptr
, promoted_initializers
))
2008 struct ipa_ref
*ref
;
2010 vnode
= VEC_pop (varpool_node_ptr
, promoted_initializers
);
2012 ipa_ref_list_reference_iterate (&vnode
->ref_list
, i
, ref
);
2015 if (ref
->refered_type
== IPA_REF_CGRAPH
)
2017 struct cgraph_node
*n
= ipa_ref_node (ref
);
2018 gcc_assert (!n
->global
.inlined_to
);
2019 if (!n
->local
.externally_visible
2020 && !cgraph_node_in_set_p (n
, set
))
2025 struct varpool_node
*v
= ipa_ref_varpool_node (ref
);
2026 if (varpool_node_in_set_p (v
, vset
))
2029 /* Constant pool references use internal labels and thus
2030 cannot be made global. It is sensible to keep those
2031 ltrans local to allow better optimization. */
2032 if (DECL_IN_CONSTANT_POOL (v
->decl
))
2034 if (!pointer_set_insert (inserted
, vnode
))
2035 VEC_safe_push (varpool_node_ptr
, heap
,
2036 promoted_initializers
, v
);
2038 else if (!v
->externally_visible
&& v
->analyzed
)
2041 && DECL_INITIAL (v
->decl
)
2042 && const_value_known_p (v
->decl
)
2043 && !pointer_set_insert (inserted
, vnode
))
2044 VEC_safe_push (varpool_node_ptr
, heap
,
2045 promoted_initializers
, v
);
2051 pointer_set_destroy (inserted
);
2054 static lto_file
*current_lto_file
;
2056 /* Helper for qsort; compare partitions and return one with smaller size.
2057 We sort from greatest to smallest so parallel build doesn't stale on the
2058 longest compilation being executed too late. */
2061 cmp_partitions (const void *a
, const void *b
)
2063 const struct ltrans_partition_def
*pa
2064 = *(struct ltrans_partition_def
*const *)a
;
2065 const struct ltrans_partition_def
*pb
2066 = *(struct ltrans_partition_def
*const *)b
;
2067 return pb
->insns
- pa
->insns
;
2070 /* Write all output files in WPA mode and the file with the list of
2074 lto_wpa_write_files (void)
2078 cgraph_node_set set
;
2079 varpool_node_set vset
;
2080 ltrans_partition part
;
2081 FILE *ltrans_output_list_stream
;
2082 char *temp_filename
;
2085 /* Open the LTRANS output list. */
2086 if (!ltrans_output_list
)
2087 fatal_error ("no LTRANS output list filename provided");
2088 ltrans_output_list_stream
= fopen (ltrans_output_list
, "w");
2089 if (ltrans_output_list_stream
== NULL
)
2090 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list
);
2092 timevar_push (TV_WHOPR_WPA
);
2094 FOR_EACH_VEC_ELT (ltrans_partition
, ltrans_partitions
, i
, part
)
2095 lto_stats
.num_output_cgraph_nodes
+= VEC_length (cgraph_node_ptr
,
2096 part
->cgraph_set
->nodes
);
2098 /* Find out statics that need to be promoted
2099 to globals with hidden visibility because they are accessed from multiple
2101 lto_promote_cross_file_statics ();
2103 timevar_pop (TV_WHOPR_WPA
);
2105 timevar_push (TV_WHOPR_WPA_IO
);
2107 /* Generate a prefix for the LTRANS unit files. */
2108 blen
= strlen (ltrans_output_list
);
2109 temp_filename
= (char *) xmalloc (blen
+ sizeof ("2147483648.o"));
2110 strcpy (temp_filename
, ltrans_output_list
);
2111 if (blen
> sizeof (".out")
2112 && strcmp (temp_filename
+ blen
- sizeof (".out") + 1,
2114 temp_filename
[blen
- sizeof (".out") + 1] = '\0';
2115 blen
= strlen (temp_filename
);
2117 n_sets
= VEC_length (ltrans_partition
, ltrans_partitions
);
2118 VEC_qsort (ltrans_partition
, ltrans_partitions
, cmp_partitions
);
2119 for (i
= 0; i
< n_sets
; i
++)
2122 ltrans_partition part
= VEC_index (ltrans_partition
, ltrans_partitions
, i
);
2124 set
= part
->cgraph_set
;
2125 vset
= part
->varpool_set
;
2127 /* Write all the nodes in SET. */
2128 sprintf (temp_filename
+ blen
, "%u.o", i
);
2129 file
= lto_obj_file_open (temp_filename
, true);
2131 fatal_error ("lto_obj_file_open() failed");
2134 fprintf (stderr
, " %s (%s %i insns)", temp_filename
, part
->name
, part
->insns
);
2135 if (cgraph_dump_file
)
2137 fprintf (cgraph_dump_file
, "Writing partition %s to file %s, %i insns\n",
2138 part
->name
, temp_filename
, part
->insns
);
2139 fprintf (cgraph_dump_file
, "cgraph nodes:");
2140 dump_cgraph_node_set (cgraph_dump_file
, set
);
2141 fprintf (cgraph_dump_file
, "varpool nodes:");
2142 dump_varpool_node_set (cgraph_dump_file
, vset
);
2144 gcc_checking_assert (cgraph_node_set_nonempty_p (set
)
2145 || varpool_node_set_nonempty_p (vset
) || !i
);
2147 lto_set_current_out_file (file
);
2149 ipa_write_optimization_summaries (set
, vset
);
2151 lto_set_current_out_file (NULL
);
2152 lto_obj_file_close (file
);
2154 len
= strlen (temp_filename
);
2155 if (fwrite (temp_filename
, 1, len
, ltrans_output_list_stream
) < len
2156 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
2157 fatal_error ("writing to LTRANS output list %s: %m",
2158 ltrans_output_list
);
2161 lto_stats
.num_output_files
+= n_sets
;
2163 /* Close the LTRANS output list. */
2164 if (fclose (ltrans_output_list_stream
))
2165 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list
);
2167 free_ltrans_partitions();
2169 timevar_pop (TV_WHOPR_WPA_IO
);
2173 /* If TT is a variable or function decl replace it with its
2174 prevailing variant. */
2175 #define LTO_SET_PREVAIL(tt) \
2177 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
2178 tt = lto_symtab_prevailing_decl (tt); \
2181 /* Ensure that TT isn't a replacable var of function decl. */
2182 #define LTO_NO_PREVAIL(tt) \
2183 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2185 /* Given a tree T replace all fields referring to variables or functions
2186 with their prevailing variant. */
2188 lto_fixup_prevailing_decls (tree t
)
2190 enum tree_code code
= TREE_CODE (t
);
2191 LTO_NO_PREVAIL (TREE_TYPE (t
));
2192 if (CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
2193 LTO_NO_PREVAIL (TREE_CHAIN (t
));
2196 LTO_NO_PREVAIL (DECL_NAME (t
));
2197 LTO_SET_PREVAIL (DECL_CONTEXT (t
));
2198 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_COMMON
))
2200 LTO_SET_PREVAIL (DECL_SIZE (t
));
2201 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t
));
2202 LTO_SET_PREVAIL (DECL_INITIAL (t
));
2203 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t
));
2204 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t
));
2206 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_WITH_VIS
))
2208 LTO_NO_PREVAIL (t
->decl_with_vis
.assembler_name
);
2209 LTO_NO_PREVAIL (DECL_SECTION_NAME (t
));
2211 if (CODE_CONTAINS_STRUCT (code
, TS_DECL_NON_COMMON
))
2213 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t
));
2214 LTO_NO_PREVAIL (DECL_RESULT_FLD (t
));
2215 LTO_NO_PREVAIL (DECL_VINDEX (t
));
2217 if (CODE_CONTAINS_STRUCT (code
, TS_FUNCTION_DECL
))
2218 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t
));
2219 if (CODE_CONTAINS_STRUCT (code
, TS_FIELD_DECL
))
2221 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t
));
2222 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t
));
2223 LTO_NO_PREVAIL (DECL_QUALIFIER (t
));
2224 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t
));
2225 LTO_NO_PREVAIL (DECL_FCONTEXT (t
));
2228 else if (TYPE_P (t
))
2230 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t
));
2231 LTO_SET_PREVAIL (TYPE_SIZE (t
));
2232 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t
));
2233 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t
));
2234 LTO_NO_PREVAIL (TYPE_NAME (t
));
2236 LTO_SET_PREVAIL (TYPE_MINVAL (t
));
2237 LTO_SET_PREVAIL (TYPE_MAXVAL (t
));
2238 LTO_SET_PREVAIL (t
->type_non_common
.binfo
);
2240 LTO_SET_PREVAIL (TYPE_CONTEXT (t
));
2242 LTO_NO_PREVAIL (TYPE_CANONICAL (t
));
2243 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t
));
2244 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t
));
2246 else if (EXPR_P (t
))
2249 LTO_NO_PREVAIL (t
->exp
.block
);
2250 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
2251 LTO_SET_PREVAIL (TREE_OPERAND (t
, i
));
2258 LTO_SET_PREVAIL (TREE_VALUE (t
));
2259 LTO_SET_PREVAIL (TREE_PURPOSE (t
));
2266 #undef LTO_SET_PREVAIL
2267 #undef LTO_NO_PREVAIL
2269 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2270 replaces var and function decls with the corresponding prevailing def. */
2273 lto_fixup_state (struct lto_in_decl_state
*state
)
2276 struct lto_tree_ref_table
*table
;
2278 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2279 we still need to walk from all DECLs to find the reachable
2280 FUNCTION_DECLs and VAR_DECLs. */
2281 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
2283 table
= &state
->streams
[si
];
2284 for (i
= 0; i
< table
->size
; i
++)
2286 tree
*tp
= table
->trees
+ i
;
2287 if (VAR_OR_FUNCTION_DECL_P (*tp
))
2288 *tp
= lto_symtab_prevailing_decl (*tp
);
2293 /* A callback of htab_traverse. Just extracts a state from SLOT
2294 and calls lto_fixup_state. */
2297 lto_fixup_state_aux (void **slot
, void *aux ATTRIBUTE_UNUSED
)
2299 struct lto_in_decl_state
*state
= (struct lto_in_decl_state
*) *slot
;
2300 lto_fixup_state (state
);
2304 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2308 lto_fixup_decls (struct lto_file_decl_data
**files
)
2314 FOR_EACH_HTAB_ELEMENT (tree_with_vars
, t
, tree
, hi
)
2315 lto_fixup_prevailing_decls (t
);
2317 for (i
= 0; files
[i
]; i
++)
2319 struct lto_file_decl_data
*file
= files
[i
];
2320 struct lto_in_decl_state
*state
= file
->global_decl_state
;
2321 lto_fixup_state (state
);
2323 htab_traverse (file
->function_decl_states
, lto_fixup_state_aux
, NULL
);
2327 /* Read the options saved from each file in the command line. Called
2328 from lang_hooks.post_options which is called by process_options
2329 right before all the options are used to initialize the compiler.
2330 This assumes that decode_options has already run, so the
2331 num_in_fnames and in_fnames are properly set.
2333 Note that this assumes that all the files had been compiled with
2334 the same options, which is not a good assumption. In general,
2335 options ought to be read from all the files in the set and merged.
2336 However, it is still unclear what the merge rules should be. */
2339 lto_read_all_file_options (void)
2343 /* Clear any file options currently saved. */
2344 lto_clear_file_options ();
2346 /* Set the hooks to read ELF sections. */
2347 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
2349 fprintf (stderr
, "Reading command line options:");
2351 for (i
= 0; i
< num_in_fnames
; i
++)
2353 struct lto_file_decl_data
*file_data
;
2354 lto_file
*file
= lto_obj_file_open (in_fnames
[i
], false);
2359 fprintf (stderr
, " %s", in_fnames
[i
]);
2363 file_data
= XCNEW (struct lto_file_decl_data
);
2364 file_data
->file_name
= file
->filename
;
2365 file_data
->section_hash_table
= lto_obj_build_section_table (file
);
2367 lto_read_file_options (file_data
);
2369 lto_obj_file_close (file
);
2370 htab_delete (file_data
->section_hash_table
);
2375 fprintf (stderr
, "\n");
2377 /* Apply globally the options read from all the files. */
2378 lto_reissue_options ();
2381 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data
**all_file_decl_data
;
2383 /* Turn file datas for sub files into a single array, so that they look
2384 like separate files for further passes. */
2387 lto_flatten_files (struct lto_file_decl_data
**orig
, int count
, int last_file_ix
)
2389 struct lto_file_decl_data
*n
, *next
;
2392 lto_stats
.num_input_files
= count
;
2394 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count
+ 1);
2395 /* Set the hooks so that all of the ipa passes can read in their data. */
2396 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2397 for (i
= 0, k
= 0; i
< last_file_ix
; i
++)
2399 for (n
= orig
[i
]; n
!= NULL
; n
= next
)
2401 all_file_decl_data
[k
++] = n
;
2406 all_file_decl_data
[k
] = NULL
;
2407 gcc_assert (k
== count
);
2410 /* Input file data before flattening (i.e. splitting them to subfiles to support
2411 incremental linking. */
2412 static int real_file_count
;
2413 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data
**real_file_decl_data
;
2415 /* Read all the symbols from the input files FNAMES. NFILES is the
2416 number of files requested in the command line. Instantiate a
2417 global call graph by aggregating all the sub-graphs found in each
2421 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
2423 unsigned int i
, last_file_ix
;
2425 struct cgraph_node
*node
;
2427 struct lto_file_decl_data
**decl_data
;
2431 timevar_push (TV_IPA_LTO_DECL_IN
);
2434 = decl_data
= ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles
+ 1);
2435 real_file_count
= nfiles
;
2437 /* Read the resolution file. */
2439 if (resolution_file_name
)
2442 unsigned num_objects
;
2444 resolution
= fopen (resolution_file_name
, "r");
2445 if (resolution
== NULL
)
2446 fatal_error ("could not open symbol resolution file: %m");
2448 t
= fscanf (resolution
, "%u", &num_objects
);
2449 gcc_assert (t
== 1);
2451 /* True, since the plugin splits the archives. */
2452 gcc_assert (num_objects
== nfiles
);
2455 tree_with_vars
= htab_create_ggc (101, htab_hash_pointer
, htab_eq_pointer
,
2459 fprintf (stderr
, "Reading object files:");
2461 /* Read all of the object files specified on the command line. */
2462 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
2464 struct lto_file_decl_data
*file_data
= NULL
;
2467 fprintf (stderr
, " %s", fnames
[i
]);
2471 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
2472 if (!current_lto_file
)
2475 file_data
= lto_file_read (current_lto_file
, resolution
, &count
);
2478 lto_obj_file_close (current_lto_file
);
2479 current_lto_file
= NULL
;
2483 decl_data
[last_file_ix
++] = file_data
;
2485 lto_obj_file_close (current_lto_file
);
2486 current_lto_file
= NULL
;
2490 lto_flatten_files (decl_data
, count
, last_file_ix
);
2491 lto_stats
.num_input_files
= count
;
2492 ggc_free(decl_data
);
2493 real_file_decl_data
= NULL
;
2495 if (resolution_file_name
)
2496 fclose (resolution
);
2498 /* Set the hooks so that all of the ipa passes can read in their data. */
2499 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
2501 timevar_pop (TV_IPA_LTO_DECL_IN
);
2504 fprintf (stderr
, "\nReading the callgraph\n");
2506 timevar_push (TV_IPA_LTO_CGRAPH_IO
);
2507 /* Read the callgraph. */
2509 timevar_pop (TV_IPA_LTO_CGRAPH_IO
);
2512 fprintf (stderr
, "Merging declarations\n");
2514 timevar_push (TV_IPA_LTO_DECL_MERGE
);
2515 /* Merge global decls. */
2516 lto_symtab_merge_decls ();
2518 /* If there were errors during symbol merging bail out, we have no
2519 good way to recover here. */
2521 fatal_error ("errors during merging of translation units");
2523 /* Fixup all decls and types and free the type hash tables. */
2524 lto_fixup_decls (all_file_decl_data
);
2525 htab_delete (tree_with_vars
);
2526 tree_with_vars
= NULL
;
2527 free_gimple_type_tables ();
2530 timevar_pop (TV_IPA_LTO_DECL_MERGE
);
2531 /* Each pass will set the appropriate timer. */
2534 fprintf (stderr
, "Reading summaries\n");
2536 /* Read the IPA summary data. */
2538 ipa_read_optimization_summaries ();
2540 ipa_read_summaries ();
2542 /* Finally merge the cgraph according to the decl merging decisions. */
2543 timevar_push (TV_IPA_LTO_CGRAPH_MERGE
);
2544 if (cgraph_dump_file
)
2546 fprintf (cgraph_dump_file
, "Before merging:\n");
2547 dump_cgraph (cgraph_dump_file
);
2548 dump_varpool (cgraph_dump_file
);
2550 lto_symtab_merge_cgraph_nodes ();
2554 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2556 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
2557 summaries computed and needs to apply changes. At the moment WHOPR only
2558 supports inlining, so we can push it here by hand. In future we need to stream
2559 this field into ltrans compilation. */
2561 VEC_safe_push (ipa_opt_pass
, heap
,
2562 node
->ipa_transforms_to_apply
,
2563 (ipa_opt_pass
)&pass_ipa_inline
);
2567 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE
);
2569 timevar_push (TV_IPA_LTO_DECL_INIT_IO
);
2571 /* FIXME lto. This loop needs to be changed to use the pass manager to
2572 call the ipa passes directly. */
2574 for (i
= 0; i
< last_file_ix
; i
++)
2576 struct lto_file_decl_data
*file_data
= all_file_decl_data
[i
];
2577 lto_materialize_constructors_and_inits (file_data
);
2580 /* Indicate that the cgraph is built and ready. */
2581 cgraph_function_flags_ready
= true;
2583 timevar_pop (TV_IPA_LTO_DECL_INIT_IO
);
2584 ggc_free (all_file_decl_data
);
2585 all_file_decl_data
= NULL
;
2589 /* Materialize all the bodies for all the nodes in the callgraph. */
2592 materialize_cgraph (void)
2595 struct cgraph_node
*node
;
2597 timevar_id_t lto_timer
;
2601 flag_wpa
? "Materializing decls:" : "Reading function bodies:");
2604 /* Now that we have input the cgraph, we need to clear all of the aux
2605 nodes and read the functions if we are not running in WPA mode. */
2606 timevar_push (TV_IPA_LTO_GIMPLE_IN
);
2608 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2610 if (node
->local
.lto_file_data
)
2612 lto_materialize_function (node
);
2613 lto_stats
.num_input_cgraph_nodes
++;
2617 timevar_pop (TV_IPA_LTO_GIMPLE_IN
);
2619 /* Start the appropriate timer depending on the mode that we are
2621 lto_timer
= (flag_wpa
) ? TV_WHOPR_WPA
2622 : (flag_ltrans
) ? TV_WHOPR_LTRANS
2624 timevar_push (lto_timer
);
2626 current_function_decl
= NULL
;
2629 /* Inform the middle end about the global variables we have seen. */
2630 FOR_EACH_VEC_ELT (tree
, lto_global_var_decls
, i
, decl
)
2631 rest_of_decl_compilation (decl
, 1, 0);
2634 fprintf (stderr
, "\n");
2636 timevar_pop (lto_timer
);
2640 /* Perform whole program analysis (WPA) on the callgraph and write out the
2641 optimization plan. */
2644 do_whole_program_analysis (void)
2646 /* Note that since we are in WPA mode, materialize_cgraph will not
2647 actually read in all the function bodies. It only materializes
2648 the decls and cgraph nodes so that analysis can be performed. */
2649 materialize_cgraph ();
2651 /* Reading in the cgraph uses different timers, start timing WPA now. */
2652 timevar_push (TV_WHOPR_WPA
);
2654 if (pre_ipa_mem_report
)
2656 fprintf (stderr
, "Memory consumption before IPA\n");
2657 dump_memory_report (false);
2660 cgraph_function_flags_ready
= true;
2662 if (cgraph_dump_file
)
2664 dump_cgraph (cgraph_dump_file
);
2665 dump_varpool (cgraph_dump_file
);
2667 bitmap_obstack_initialize (NULL
);
2668 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
2670 execute_ipa_pass_list (all_regular_ipa_passes
);
2672 if (cgraph_dump_file
)
2674 fprintf (cgraph_dump_file
, "Optimized ");
2675 dump_cgraph (cgraph_dump_file
);
2676 dump_varpool (cgraph_dump_file
);
2679 bitmap_obstack_release (NULL
);
2681 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
2682 timevar_pop (TV_WHOPR_WPA
);
2684 if (flag_lto_partition_1to1
)
2687 lto_balanced_map ();
2691 fprintf (stderr
, "\nStreaming out");
2694 lto_wpa_write_files ();
2697 fprintf (stderr
, "\n");
2699 if (post_ipa_mem_report
)
2701 fprintf (stderr
, "Memory consumption after IPA\n");
2702 dump_memory_report (false);
2705 /* Show the LTO report before launching LTRANS. */
2706 if (flag_lto_report
)
2707 print_lto_report ();
2711 static GTY(()) tree lto_eh_personality_decl
;
2713 /* Return the LTO personality function decl. */
2716 lto_eh_personality (void)
2718 if (!lto_eh_personality_decl
)
2720 /* Use the first personality DECL for our personality if we don't
2721 support multiple ones. This ensures that we don't artificially
2722 create the need for them in a single-language program. */
2723 if (first_personality_decl
&& !dwarf2out_do_cfi_asm ())
2724 lto_eh_personality_decl
= first_personality_decl
;
2726 lto_eh_personality_decl
= lhd_gcc_personality ();
2729 return lto_eh_personality_decl
;
2732 /* Set the process name based on the LTO mode. */
2735 lto_process_name (void)
2738 setproctitle ("lto1-lto");
2740 setproctitle ("lto1-wpa");
2742 setproctitle ("lto1-ltrans");
2746 /* Initialize the LTO front end. */
2751 lto_process_name ();
2752 lto_streamer_hooks_init ();
2754 memset (<o_stats
, 0, sizeof (lto_stats
));
2755 bitmap_obstack_initialize (NULL
);
2756 gimple_register_cfg_hooks ();
2760 /* Main entry point for the GIMPLE front end. This front end has
2761 three main personalities:
2763 - LTO (-flto). All the object files on the command line are
2764 loaded in memory and processed as a single translation unit.
2765 This is the traditional link-time optimization behavior.
2767 - WPA (-fwpa). Only the callgraph and summary information for
2768 files in the command file are loaded. A single callgraph
2769 (without function bodies) is instantiated for the whole set of
2770 files. IPA passes are only allowed to analyze the call graph
2771 and make transformation decisions. The callgraph is
2772 partitioned, each partition is written to a new object file
2773 together with the transformation decisions.
2775 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2776 summary files from running again. Since WPA computed summary
2777 information and decided what transformations to apply, LTRANS
2778 simply applies them. */
2783 /* Initialize the LTO front end. */
2786 /* Read all the symbols and call graph from all the files in the
2788 read_cgraph_and_symbols (num_in_fnames
, in_fnames
);
2792 /* If WPA is enabled analyze the whole call graph and create an
2793 optimization plan. Otherwise, read in all the function
2794 bodies and continue with optimization. */
2796 do_whole_program_analysis ();
2799 materialize_cgraph ();
2801 /* Let the middle end know that we have read and merged all of
2805 /* FIXME lto, if the processes spawned by WPA fail, we miss
2806 the chance to print WPA's report, so WPA will call
2807 print_lto_report before launching LTRANS. If LTRANS was
2808 launched directly by the driver we would not need to do
2810 if (flag_lto_report
)
2811 print_lto_report ();
2816 #include "gt-lto-lto.h"