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 "splay-tree.h"
48 #include "ipa-inline.h"
49 #include "ipa-utils.h"
51 static GTY(()) tree first_personality_decl;
53 /* Returns a hash code for P. */
56 hash_name (const void *p)
58 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
59 return (hashval_t) htab_hash_string (ds->name);
63 /* Returns nonzero if P1 and P2 are equal. */
66 eq_name (const void *p1, const void *p2)
68 const struct lto_section_slot *s1 =
69 (const struct lto_section_slot *) p1;
70 const struct lto_section_slot *s2 =
71 (const struct lto_section_slot *) p2;
73 return strcmp (s1->name, s2->name) == 0;
76 /* Free lto_section_slot */
79 free_with_string (void *arg)
81 struct lto_section_slot *s = (struct lto_section_slot *)arg;
83 free (CONST_CAST (char *, s->name));
87 /* Create section hash table */
90 lto_obj_create_section_hash_table (void)
92 return htab_create (37, hash_name, eq_name, free_with_string);
95 /* Read the constructors and inits. */
98 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
101 const char *data = lto_get_section_data (file_data,
102 LTO_section_static_initializer,
104 lto_input_constructors_and_inits (file_data, data);
105 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
109 /* Return true when NODE has a clone that is analyzed (i.e. we need
110 to load its body even if the node itself is not needed). */
113 has_analyzed_clone_p (struct cgraph_node *node)
115 struct cgraph_node *orig = node;
124 else if (node->next_sibling_clone)
125 node = node->next_sibling_clone;
128 while (node != orig && !node->next_sibling_clone)
129 node = node->clone_of;
131 node = node->next_sibling_clone;
137 /* Read the function body for the function associated with NODE. */
140 lto_materialize_function (struct cgraph_node *node)
143 struct lto_file_decl_data *file_data;
144 const char *data, *name;
148 /* Read in functions with body (analyzed nodes)
149 and also functions that are needed to produce virtual clones. */
150 if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node))
152 /* Clones and thunks don't need to be read. */
156 /* Load the function body only if not operating in WPA mode. In
157 WPA mode, the body of the function is not needed. */
160 file_data = node->local.lto_file_data;
161 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
163 /* We may have renamed the declaration, e.g., a static function. */
164 name = lto_get_decl_name_mapping (file_data, name);
166 data = lto_get_section_data (file_data, LTO_section_function_body,
169 fatal_error ("%s: section %s is missing",
170 file_data->file_name,
173 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
175 allocate_struct_function (decl, false);
176 announce_function (decl);
177 lto_input_function_body (file_data, decl, data);
178 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
179 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
180 lto_stats.num_function_bodies++;
181 lto_free_section_data (file_data, LTO_section_function_body, name,
187 /* Let the middle end know about the function. */
188 rest_of_decl_compilation (decl, 1, 0);
192 /* Decode the content of memory pointed to by DATA in the in decl
193 state object STATE. DATA_IN points to a data_in structure for
194 decoding. Return the address after the decoded object in the
197 static const uint32_t *
198 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
199 struct lto_in_decl_state *state)
206 decl = lto_streamer_cache_get (data_in->reader_cache, ix);
207 if (TREE_CODE (decl) != FUNCTION_DECL)
209 gcc_assert (decl == void_type_node);
212 state->fn_decl = decl;
214 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
216 uint32_t size = *data++;
217 tree *decls = ggc_alloc_vec_tree (size);
219 for (j = 0; j < size; j++)
220 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
222 state->streams[i].size = size;
223 state->streams[i].trees = decls;
230 /* A hashtable of trees that potentially refer to variables or functions
231 that must be replaced with their prevailing variant. */
232 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
235 /* Remember that T is a tree that (potentially) refers to a variable
236 or function decl that may be replaced with its prevailing variant. */
238 remember_with_vars (tree t)
240 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
243 #define LTO_FIXUP_TREE(tt) \
249 (tt) = gimple_register_type (tt); \
250 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
251 remember_with_vars (t); \
255 static void lto_fixup_types (tree);
257 /* Fix up fields of a tree_common T. */
260 lto_ft_common (tree t)
262 /* Fixup our type. */
263 LTO_FIXUP_TREE (TREE_TYPE (t));
265 LTO_FIXUP_TREE (TREE_CHAIN (t));
268 /* Fix up fields of a decl_minimal T. */
271 lto_ft_decl_minimal (tree t)
274 LTO_FIXUP_TREE (DECL_NAME (t));
275 LTO_FIXUP_TREE (DECL_CONTEXT (t));
278 /* Fix up fields of a decl_common T. */
281 lto_ft_decl_common (tree t)
283 lto_ft_decl_minimal (t);
284 LTO_FIXUP_TREE (DECL_SIZE (t));
285 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t));
286 LTO_FIXUP_TREE (DECL_INITIAL (t));
287 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t));
288 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t));
291 /* Fix up fields of a decl_with_vis T. */
294 lto_ft_decl_with_vis (tree t)
296 lto_ft_decl_common (t);
298 /* Accessor macro has side-effects, use field-name here. */
299 LTO_FIXUP_TREE (t->decl_with_vis.assembler_name);
300 LTO_FIXUP_TREE (DECL_SECTION_NAME (t));
303 /* Fix up fields of a decl_non_common T. */
306 lto_ft_decl_non_common (tree t)
308 lto_ft_decl_with_vis (t);
309 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t));
310 LTO_FIXUP_TREE (DECL_RESULT_FLD (t));
311 LTO_FIXUP_TREE (DECL_VINDEX (t));
314 /* Fix up fields of a decl_non_common T. */
317 lto_ft_function (tree t)
319 lto_ft_decl_non_common (t);
320 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t));
323 /* Fix up fields of a field_decl T. */
326 lto_ft_field_decl (tree t)
328 lto_ft_decl_common (t);
329 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t));
330 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t));
331 LTO_FIXUP_TREE (DECL_QUALIFIER (t));
332 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t));
333 LTO_FIXUP_TREE (DECL_FCONTEXT (t));
336 /* Fix up fields of a type T. */
342 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t));
343 LTO_FIXUP_TREE (TYPE_SIZE (t));
344 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t));
345 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t));
346 LTO_FIXUP_TREE (TYPE_NAME (t));
348 /* Accessors are for derived node types only. */
349 if (!POINTER_TYPE_P (t))
350 LTO_FIXUP_TREE (TYPE_MINVAL (t));
351 LTO_FIXUP_TREE (TYPE_MAXVAL (t));
353 /* Accessor is for derived node types only. */
354 LTO_FIXUP_TREE (t->type_non_common.binfo);
356 LTO_FIXUP_TREE (TYPE_CONTEXT (t));
359 /* Fix up fields of a BINFO T. */
362 lto_ft_binfo (tree t)
364 unsigned HOST_WIDE_INT i, n;
365 tree base, saved_base;
368 LTO_FIXUP_TREE (BINFO_VTABLE (t));
369 LTO_FIXUP_TREE (BINFO_OFFSET (t));
370 LTO_FIXUP_TREE (BINFO_VIRTUALS (t));
371 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t));
372 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
373 for (i = 0; i < n; i++)
375 saved_base = base = BINFO_BASE_ACCESS (t, i);
376 LTO_FIXUP_TREE (base);
377 if (base != saved_base)
378 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
380 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t));
381 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t));
382 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t));
383 n = BINFO_N_BASE_BINFOS (t);
384 for (i = 0; i < n; i++)
386 saved_base = base = BINFO_BASE_BINFO (t, i);
387 LTO_FIXUP_TREE (base);
388 if (base != saved_base)
389 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
393 /* Fix up fields of a CONSTRUCTOR T. */
396 lto_ft_constructor (tree t)
398 unsigned HOST_WIDE_INT idx;
401 LTO_FIXUP_TREE (TREE_TYPE (t));
404 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
407 LTO_FIXUP_TREE (ce->index);
408 LTO_FIXUP_TREE (ce->value);
412 /* Fix up fields of an expression tree T. */
419 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
420 LTO_FIXUP_TREE (TREE_OPERAND (t, i));
423 /* Given a tree T fixup fields of T by replacing types with their merged
424 variant and other entities by an equal entity from an earlier compilation
425 unit, or an entity being canonical in a different way. This includes
426 for instance integer or string constants. */
429 lto_fixup_types (tree t)
431 switch (TREE_CODE (t))
433 case IDENTIFIER_NODE:
437 LTO_FIXUP_TREE (TREE_VALUE (t));
438 LTO_FIXUP_TREE (TREE_PURPOSE (t));
439 LTO_FIXUP_TREE (TREE_CHAIN (t));
443 lto_ft_field_decl (t);
451 lto_ft_decl_common (t);
455 lto_ft_decl_with_vis (t);
459 lto_ft_decl_non_common (t);
470 case PLACEHOLDER_EXPR:
475 case TRANSLATION_UNIT_DECL:
476 case OPTIMIZATION_NODE:
477 case TARGET_OPTION_NODE:
483 else if (TREE_CODE (t) == CONSTRUCTOR)
484 lto_ft_constructor (t);
485 else if (CONSTANT_CLASS_P (t))
486 LTO_FIXUP_TREE (TREE_TYPE (t));
493 remember_with_vars (t);
498 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
499 for one compilation unit) go over all trees starting at index FROM until the
500 end of the sequence and replace fields of those trees, and the trees
501 themself with their canonical variants as per gimple_register_type. */
504 uniquify_nodes (struct data_in *data_in, unsigned from)
506 struct lto_streamer_cache_d *cache = data_in->reader_cache;
507 unsigned len = VEC_length (tree, cache->nodes);
510 /* Go backwards because childs streamed for the first time come
511 as part of their parents, and hence are created after them. */
513 /* First register all types in the cache.
514 This makes sure to have the original structure in the type cycles
515 when registering them and computing hashes. */
516 for (i = len; i-- > from;)
518 tree t = VEC_index (tree, cache->nodes, i);
523 gimple_register_type (t);
526 /* Second fixup all trees in the new cache entries. */
527 for (i = len; i-- > from;)
529 tree t = VEC_index (tree, cache->nodes, i);
534 /* First fixup the fields of T. */
540 /* Now try to find a canonical variant of T itself. */
541 t = gimple_register_type (t);
545 /* The following re-creates proper variant lists while fixing up
546 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
547 variant list state before fixup is broken. */
550 /* Remove us from our main variant list if we are not the
552 if (TYPE_MAIN_VARIANT (t) != t)
554 tem = TYPE_MAIN_VARIANT (t);
555 while (tem && TYPE_NEXT_VARIANT (tem) != t)
556 tem = TYPE_NEXT_VARIANT (tem);
558 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
559 TYPE_NEXT_VARIANT (t) = NULL_TREE;
562 /* Query our new main variant. */
563 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
565 /* If we were the variant leader and we get replaced ourselves drop
566 all variants from our list. */
567 if (TYPE_MAIN_VARIANT (t) == t
573 tree tem2 = TYPE_NEXT_VARIANT (tem);
574 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
579 /* If we are not our own variant leader link us into our new leaders
583 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
584 TYPE_NEXT_VARIANT (mv) = t;
587 /* Finally adjust our main variant and fix it up. */
588 TYPE_MAIN_VARIANT (t) = mv;
590 /* The following reconstructs the pointer chains
591 of the new pointed-to type if we are a main variant. We do
592 not stream those so they are broken before fixup. */
593 if (TREE_CODE (t) == POINTER_TYPE
594 && TYPE_MAIN_VARIANT (t) == t)
596 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
597 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
599 else if (TREE_CODE (t) == REFERENCE_TYPE
600 && TYPE_MAIN_VARIANT (t) == t)
602 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
603 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
607 else if (RECORD_OR_UNION_TYPE_P (t))
610 if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt))
611 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt);
612 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
615 gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2));
616 if (!lto_streamer_cache_lookup (cache, f2, &ix))
618 /* If we're going to replace an element which we'd
619 still visit in the next iterations, we wouldn't
620 handle it, so do it here. We do have to handle it
621 even though the field_decl itself will be removed,
622 as it could refer to e.g. integer_cst which we
623 wouldn't reach via any other way, hence they
624 (and their type) would stay uncollected. */
625 /* ??? We should rather make sure to replace all
626 references to f2 with f1. That means handling
627 COMPONENT_REFs and CONSTRUCTOR elements in
628 lto_fixup_types and special-case the field-decl
631 lto_fixup_types (f2);
632 lto_streamer_cache_insert_at (cache, f1, ix);
635 /* If we found a tree that is equal to oldt replace it in the
636 cache, so that further users (in the various LTO sections)
638 lto_streamer_cache_insert_at (cache, t, i);
642 /* Finally compute the canonical type of t. From this point
643 there are no longer any types with TYPE_STRUCTURAL_EQUALITY_P
644 and its type-based alias problems. This step requires the
645 TYPE_POINTER_TO lists being present, so make sure it is done
647 for (i = len; i-- > from;)
649 tree t = VEC_index (tree, cache->nodes, i);
654 if (!TYPE_CANONICAL (t))
655 TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
659 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
660 RESOLUTIONS is the set of symbols picked by the linker (read from the
661 resolution file when the linker plugin is being used). */
664 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
665 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
667 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
668 const int32_t decl_offset = sizeof (struct lto_decl_header);
669 const int32_t main_offset = decl_offset + header->decl_state_size;
670 const int32_t string_offset = main_offset + header->main_size;
671 struct lto_input_block ib_main;
672 struct data_in *data_in;
674 const uint32_t *data_ptr, *data_end;
675 uint32_t num_decl_states;
677 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
680 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
681 header->string_size, resolutions);
683 /* Read the global declarations and types. */
684 while (ib_main.p < ib_main.len)
687 unsigned from = VEC_length (tree, data_in->reader_cache->nodes);
688 t = lto_input_tree (&ib_main, data_in);
689 gcc_assert (t && ib_main.p <= ib_main.len);
690 uniquify_nodes (data_in, from);
693 /* Read in lto_in_decl_state objects. */
694 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
696 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
697 num_decl_states = *data_ptr++;
699 gcc_assert (num_decl_states > 0);
700 decl_data->global_decl_state = lto_new_in_decl_state ();
701 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
702 decl_data->global_decl_state);
704 /* Read in per-function decl states and enter them in hash table. */
705 decl_data->function_decl_states =
706 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
708 for (i = 1; i < num_decl_states; i++)
710 struct lto_in_decl_state *state = lto_new_in_decl_state ();
713 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
714 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
715 gcc_assert (*slot == NULL);
719 if (data_ptr != data_end)
720 internal_error ("bytecode stream: garbage at the end of symbols section");
722 /* Set the current decl state to be the global state. */
723 decl_data->current_decl_state = decl_data->global_decl_state;
725 lto_data_in_delete (data_in);
728 /* strtoll is not portable. */
730 lto_parse_hex (const char *p) {
732 for (; *p != '\0'; ++p)
737 if (c >= '0' && c <= '9')
739 else if (c >= 'a' && c <= 'f')
741 else if (c >= 'A' && c <= 'F')
744 internal_error ("could not parse hex number");
750 /* Read resolution for file named FILE_NAME. The resolution is read from
754 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
756 /* We require that objects in the resolution file are in the same
757 order as the lto1 command line. */
758 unsigned int name_len;
760 unsigned int num_symbols;
762 struct lto_file_decl_data *file_data;
763 unsigned max_index = 0;
764 splay_tree_node nd = NULL;
769 name_len = strlen (file->filename);
770 obj_name = XNEWVEC (char, name_len + 1);
771 fscanf (resolution, " "); /* Read white space. */
773 fread (obj_name, sizeof (char), name_len, resolution);
774 obj_name[name_len] = '\0';
775 if (filename_cmp (obj_name, file->filename) != 0)
776 internal_error ("unexpected file name %s in linker resolution file. "
777 "Expected %s", obj_name, file->filename);
778 if (file->offset != 0)
783 t = fscanf (resolution, "@0x%16s", offset_p);
785 internal_error ("could not parse file offset");
786 offset = lto_parse_hex (offset_p);
787 if (offset != file->offset)
788 internal_error ("unexpected offset");
793 fscanf (resolution, "%u", &num_symbols);
795 for (i = 0; i < num_symbols; i++)
800 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
802 unsigned int lto_resolution_str_len =
803 sizeof (lto_resolution_str) / sizeof (char *);
805 t = fscanf (resolution, "%u %x %26s %*[^\n]\n", &index, &id, r_str);
807 internal_error ("invalid line in the resolution file");
808 if (index > max_index)
811 for (j = 0; j < lto_resolution_str_len; j++)
813 if (strcmp (lto_resolution_str[j], r_str) == 0)
815 r = (enum ld_plugin_symbol_resolution) j;
819 if (j == lto_resolution_str_len)
820 internal_error ("invalid resolution in the resolution file");
822 if (!(nd && nd->key == id))
824 nd = splay_tree_lookup (file_ids, id);
826 internal_error ("resolution sub id %x not in object file", id);
829 file_data = (struct lto_file_decl_data *)nd->value;
830 if (cgraph_dump_file)
831 fprintf (cgraph_dump_file, "Adding resolution %u %u to id %x\n",
832 index, r, file_data->id);
833 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap,
834 file_data->resolutions,
836 VEC_replace (ld_plugin_symbol_resolution_t,
837 file_data->resolutions, index, r);
841 /* Is the name for a id'ed LTO section? */
844 lto_section_with_id (const char *name, unsigned *id)
848 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
850 s = strrchr (name, '.');
851 return s && sscanf (s, ".%x", id) == 1;
854 /* Create file_data of each sub file id */
857 create_subid_section_table (void **slot, void *data)
859 struct lto_section_slot s_slot, *new_slot;
860 struct lto_section_slot *ls = *(struct lto_section_slot **)slot;
861 splay_tree file_ids = (splay_tree)data;
866 struct lto_file_decl_data *file_data;
868 if (!lto_section_with_id (ls->name, &id))
871 /* Find hash table of sub module id */
872 nd = splay_tree_lookup (file_ids, id);
875 file_data = (struct lto_file_decl_data *)nd->value;
879 file_data = ggc_alloc_lto_file_decl_data ();
880 memset(file_data, 0, sizeof (struct lto_file_decl_data));
882 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
883 splay_tree_insert (file_ids, id, (splay_tree_value)file_data);
886 /* Copy section into sub module hash table */
887 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
888 s_slot.name = new_name;
889 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
890 gcc_assert (*hash_slot == NULL);
892 new_slot = XDUP (struct lto_section_slot, ls);
893 new_slot->name = new_name;
894 *hash_slot = new_slot;
898 /* Read declarations and other initializations for a FILE_DATA. */
901 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
906 file_data->renaming_hash_table = lto_create_renaming_table ();
907 file_data->file_name = file->filename;
908 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
911 internal_error ("cannot read LTO decls from %s", file_data->file_name);
914 lto_read_decls (file_data, data, file_data->resolutions);
915 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
921 struct lto_file_decl_data **file_data;
925 /* Traverse ids and create a list of file_datas out of it. */
927 static int lto_create_files_from_ids (splay_tree_node node, void *data)
929 struct lwstate *lw = (struct lwstate *)data;
930 struct lto_file_decl_data *file_data = (struct lto_file_decl_data *)node->value;
932 lto_file_finalize (file_data, lw->file);
933 if (cgraph_dump_file)
934 fprintf (cgraph_dump_file, "Creating file %s with sub id %x\n",
935 file_data->file_name, file_data->id);
936 file_data->next = *lw->file_data;
937 *lw->file_data = file_data;
942 /* Generate a TREE representation for all types and external decls
945 Read all of the globals out of the file. Then read the cgraph
946 and process the .o index into the cgraph nodes so that it can open
947 the .o file to load the functions and ipa information. */
949 static struct lto_file_decl_data *
950 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
952 struct lto_file_decl_data *file_data = NULL;
954 htab_t section_hash_table;
955 struct lwstate state;
957 section_hash_table = lto_obj_build_section_table (file);
959 /* Find all sub modules in the object and put their sections into new hash
960 tables in a splay tree. */
961 file_ids = splay_tree_new (splay_tree_compare_ints, NULL, NULL);
962 htab_traverse (section_hash_table, create_subid_section_table, file_ids);
964 /* Add resolutions to file ids */
965 lto_resolution_read (file_ids, resolution_file, file);
967 /* Finalize each lto file for each submodule in the merged object
968 and create list for returning. */
970 state.file_data = &file_data;
972 splay_tree_foreach (file_ids, lto_create_files_from_ids, &state);
974 splay_tree_delete (file_ids);
975 htab_delete (section_hash_table);
980 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
981 #define LTO_MMAP_IO 1
985 /* Page size of machine is used for mmap and munmap calls. */
986 static size_t page_mask;
989 /* Get the section data of length LEN from FILENAME starting at
990 OFFSET. The data segment must be freed by the caller when the
991 caller is finished. Returns NULL if all was not well. */
994 lto_read_section_data (struct lto_file_decl_data *file_data,
995 intptr_t offset, size_t len)
999 static char *fd_name;
1001 intptr_t computed_len;
1002 intptr_t computed_offset;
1006 /* Keep a single-entry file-descriptor cache. The last file we
1007 touched will get closed at exit.
1008 ??? Eventually we want to add a more sophisticated larger cache
1009 or rather fix function body streaming to not stream them in
1010 practically random order. */
1012 && filename_cmp (fd_name, file_data->file_name) != 0)
1020 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
1023 fd_name = xstrdup (file_data->file_name);
1029 size_t page_size = sysconf (_SC_PAGE_SIZE);
1030 page_mask = ~(page_size - 1);
1033 computed_offset = offset & page_mask;
1034 diff = offset - computed_offset;
1035 computed_len = len + diff;
1037 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
1038 fd, computed_offset);
1039 if (result == MAP_FAILED)
1042 return result + diff;
1044 result = (char *) xmalloc (len);
1045 if (lseek (fd, offset, SEEK_SET) != offset
1046 || read (fd, result, len) != (ssize_t) len)
1052 /* Native windows doesn't supports delayed unlink on opened file. So
1053 we close file here again. This produces higher I/O load, but at least
1054 it prevents to have dangling file handles preventing unlink. */
1065 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
1066 NAME will be NULL unless the section type is for a function
1070 get_section_data (struct lto_file_decl_data *file_data,
1071 enum lto_section_type section_type,
1075 htab_t section_hash_table = file_data->section_hash_table;
1076 struct lto_section_slot *f_slot;
1077 struct lto_section_slot s_slot;
1078 const char *section_name = lto_get_section_name (section_type, name, file_data);
1082 s_slot.name = section_name;
1083 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
1086 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
1090 free (CONST_CAST (char *, section_name));
1095 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
1096 starts at OFFSET and has LEN bytes. */
1099 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
1100 enum lto_section_type section_type ATTRIBUTE_UNUSED,
1101 const char *name ATTRIBUTE_UNUSED,
1102 const char *offset, size_t len ATTRIBUTE_UNUSED)
1105 intptr_t computed_len;
1106 intptr_t computed_offset;
1111 computed_offset = ((intptr_t) offset) & page_mask;
1112 diff = (intptr_t) offset - computed_offset;
1113 computed_len = len + diff;
1115 munmap ((caddr_t) computed_offset, computed_len);
1117 free (CONST_CAST(char *, offset));
1121 /* Structure describing ltrans partitions. */
1123 struct ltrans_partition_def
1125 cgraph_node_set cgraph_set;
1126 varpool_node_set varpool_set;
1131 typedef struct ltrans_partition_def *ltrans_partition;
1132 DEF_VEC_P(ltrans_partition);
1133 DEF_VEC_ALLOC_P(ltrans_partition,heap);
1135 static VEC(ltrans_partition, heap) *ltrans_partitions;
1137 static void add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node);
1138 static void add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode);
1140 /* Create new partition with name NAME. */
1141 static ltrans_partition
1142 new_partition (const char *name)
1144 ltrans_partition part = XCNEW (struct ltrans_partition_def);
1145 part->cgraph_set = cgraph_node_set_new ();
1146 part->varpool_set = varpool_node_set_new ();
1149 VEC_safe_push (ltrans_partition, heap, ltrans_partitions, part);
1153 /* Free memory used by ltrans datastructures. */
1155 free_ltrans_partitions (void)
1158 ltrans_partition part;
1159 for (idx = 0; VEC_iterate (ltrans_partition, ltrans_partitions, idx, part); idx++)
1161 free_cgraph_node_set (part->cgraph_set);
1164 VEC_free (ltrans_partition, heap, ltrans_partitions);
1167 /* See all references that go to comdat objects and bring them into partition too. */
1169 add_references_to_partition (ltrans_partition part, struct ipa_ref_list *refs)
1172 struct ipa_ref *ref;
1173 for (i = 0; ipa_ref_list_reference_iterate (refs, i, ref); i++)
1175 if (ref->refered_type == IPA_REF_CGRAPH
1176 && DECL_COMDAT (ipa_ref_node (ref)->decl)
1177 && !cgraph_node_in_set_p (ipa_ref_node (ref), part->cgraph_set))
1178 add_cgraph_node_to_partition (part, ipa_ref_node (ref));
1180 if (ref->refered_type == IPA_REF_VARPOOL
1181 && DECL_COMDAT (ipa_ref_varpool_node (ref)->decl)
1182 && !varpool_node_in_set_p (ipa_ref_varpool_node (ref), part->varpool_set))
1183 add_varpool_node_to_partition (part, ipa_ref_varpool_node (ref));
1187 /* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */
1190 add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node)
1192 struct cgraph_edge *e;
1193 cgraph_node_set_iterator csi;
1195 /* If NODE is already there, we have nothing to do. */
1196 csi = cgraph_node_set_find (part->cgraph_set, node);
1197 if (!csi_end_p (csi))
1200 part->insns += inline_summary (node)->self_size;
1204 node->in_other_partition = 1;
1205 if (cgraph_dump_file)
1206 fprintf (cgraph_dump_file, "Node %s/%i now used in multiple partitions\n",
1207 cgraph_node_name (node), node->uid);
1209 node->aux = (void *)((size_t)node->aux + 1);
1211 cgraph_node_set_add (part->cgraph_set, node);
1213 /* Thunks always must go along with function they reffer to. */
1214 if (node->thunk.thunk_p)
1215 add_cgraph_node_to_partition (part, node->callees->callee);
1216 for (e = node->callers; e; e = e->next_caller)
1217 if (e->caller->thunk.thunk_p)
1218 add_cgraph_node_to_partition (part, e->caller);
1220 for (e = node->callees; e; e = e->next_callee)
1221 if ((!e->inline_failed || DECL_COMDAT (e->callee->decl))
1222 && !cgraph_node_in_set_p (e->callee, part->cgraph_set))
1223 add_cgraph_node_to_partition (part, e->callee);
1225 add_references_to_partition (part, &node->ref_list);
1227 if (node->same_comdat_group
1228 && !cgraph_node_in_set_p (node->same_comdat_group, part->cgraph_set))
1229 add_cgraph_node_to_partition (part, node->same_comdat_group);
1232 /* Add VNODE to partition as well as comdat references partition PART. */
1235 add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode)
1237 varpool_node_set_iterator vsi;
1239 /* If NODE is already there, we have nothing to do. */
1240 vsi = varpool_node_set_find (part->varpool_set, vnode);
1241 if (!vsi_end_p (vsi))
1244 varpool_node_set_add (part->varpool_set, vnode);
1248 vnode->in_other_partition = 1;
1249 if (cgraph_dump_file)
1250 fprintf (cgraph_dump_file, "Varpool node %s now used in multiple partitions\n",
1251 varpool_node_name (vnode));
1253 vnode->aux = (void *)((size_t)vnode->aux + 1);
1255 add_references_to_partition (part, &vnode->ref_list);
1257 if (vnode->same_comdat_group
1258 && !varpool_node_in_set_p (vnode->same_comdat_group, part->varpool_set))
1259 add_varpool_node_to_partition (part, vnode->same_comdat_group);
1262 /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
1263 and number of varpool nodes is N_VARPOOL_NODES. */
1266 undo_partition (ltrans_partition partition, unsigned int n_cgraph_nodes,
1267 unsigned int n_varpool_nodes)
1269 while (VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes) >
1272 struct cgraph_node *node = VEC_index (cgraph_node_ptr,
1273 partition->cgraph_set->nodes,
1275 partition->insns -= inline_summary (node)->self_size;
1276 cgraph_node_set_remove (partition->cgraph_set, node);
1277 node->aux = (void *)((size_t)node->aux - 1);
1279 while (VEC_length (varpool_node_ptr, partition->varpool_set->nodes) >
1282 struct varpool_node *node = VEC_index (varpool_node_ptr,
1283 partition->varpool_set->nodes,
1285 varpool_node_set_remove (partition->varpool_set, node);
1286 node->aux = (void *)((size_t)node->aux - 1);
1290 /* Return true if NODE should be partitioned.
1291 This means that partitioning algorithm should put NODE into one of partitions.
1292 This apply to most functions with bodies. Functions that are not partitions
1293 are put into every unit needing them. This is the case of i.e. COMDATs. */
1296 partition_cgraph_node_p (struct cgraph_node *node)
1298 /* We will get proper partition based on function they are inlined to. */
1299 if (node->global.inlined_to)
1301 /* Nodes without a body do not need partitioning. */
1302 if (!node->analyzed)
1304 /* Extern inlines and comdat are always only in partitions they are needed. */
1305 if (DECL_EXTERNAL (node->decl)
1306 || (DECL_COMDAT (node->decl)
1307 && !cgraph_used_from_object_file_p (node)))
1309 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
1314 /* Return true if VNODE should be partitioned.
1315 This means that partitioning algorithm should put VNODE into one of partitions. */
1318 partition_varpool_node_p (struct varpool_node *vnode)
1320 if (vnode->alias || !vnode->needed)
1322 /* Constant pool and comdat are always only in partitions they are needed. */
1323 if (DECL_IN_CONSTANT_POOL (vnode->decl)
1324 || (DECL_COMDAT (vnode->decl)
1325 && !vnode->force_output
1326 && !varpool_used_from_object_file_p (vnode)))
1328 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->decl)))
1333 /* Group cgrah nodes by input files. This is used mainly for testing
1337 lto_1_to_1_map (void)
1339 struct cgraph_node *node;
1340 struct varpool_node *vnode;
1341 struct lto_file_decl_data *file_data;
1342 struct pointer_map_t *pmap;
1343 ltrans_partition partition;
1345 int npartitions = 0;
1347 timevar_push (TV_WHOPR_WPA);
1349 pmap = pointer_map_create ();
1351 for (node = cgraph_nodes; node; node = node->next)
1353 if (!partition_cgraph_node_p (node))
1356 file_data = node->local.lto_file_data;
1357 gcc_assert (!node->same_body_alias);
1361 slot = pointer_map_contains (pmap, file_data);
1363 partition = (ltrans_partition) *slot;
1366 partition = new_partition (file_data->file_name);
1367 slot = pointer_map_insert (pmap, file_data);
1373 && VEC_length (ltrans_partition, ltrans_partitions))
1374 partition = VEC_index (ltrans_partition, ltrans_partitions, 0);
1377 partition = new_partition ("");
1378 slot = pointer_map_insert (pmap, NULL);
1384 add_cgraph_node_to_partition (partition, node);
1387 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1389 if (!partition_varpool_node_p (vnode))
1391 file_data = vnode->lto_file_data;
1392 slot = pointer_map_contains (pmap, file_data);
1394 partition = (ltrans_partition) *slot;
1397 partition = new_partition (file_data->file_name);
1398 slot = pointer_map_insert (pmap, file_data);
1404 add_varpool_node_to_partition (partition, vnode);
1406 for (node = cgraph_nodes; node; node = node->next)
1408 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1411 /* If the cgraph is empty, create one cgraph node set so that there is still
1412 an output file for any variables that need to be exported in a DSO. */
1414 new_partition ("empty");
1416 pointer_map_destroy (pmap);
1418 timevar_pop (TV_WHOPR_WPA);
1420 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
1425 /* Group cgraph nodes into equally-sized partitions.
1427 The partitioning algorithm is simple: nodes are taken in predefined order.
1428 The order corresponds to the order we want functions to have in the final
1429 output. In the future this will be given by function reordering pass, but
1430 at the moment we use the topological order, which is a good approximation.
1432 The goal is to partition this linear order into intervals (partitions) so
1433 that all the partitions have approximately the same size and the number of
1434 callgraph or IPA reference edges crossing boundaries is minimal.
1436 This is a lot faster (O(n) in size of callgraph) than algorithms doing
1437 priority-based graph clustering that are generally O(n^2) and, since
1438 WHOPR is designed to make things go well across partitions, it leads
1441 We compute the expected size of a partition as:
1443 max (total_size / lto_partitions, min_partition_size)
1445 We use dynamic expected size of partition so small programs are partitioned
1446 into enough partitions to allow use of multiple CPUs, while large programs
1447 are not partitioned too much. Creating too many partitions significantly
1448 increases the streaming overhead.
1450 In the future, we would like to bound the maximal size of partitions so as
1451 to prevent the LTRANS stage from consuming too much memory. At the moment,
1452 however, the WPA stage is the most memory intensive for large benchmarks,
1453 since too many types and declarations are read into memory.
1455 The function implements a simple greedy algorithm. Nodes are being added
1456 to the current partition until after 3/4 of the expected partition size is
1457 reached. Past this threshold, we keep track of boundary size (number of
1458 edges going to other partitions) and continue adding functions until after
1459 the current partition has grown to twice the expected partition size. Then
1460 the process is undone to the point where the minimal ratio of boundary size
1461 and in-partition calls was reached. */
1464 lto_balanced_map (void)
1467 struct cgraph_node **postorder =
1468 XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1469 struct cgraph_node **order = XNEWVEC (struct cgraph_node *, cgraph_max_uid);
1470 int i, postorder_len;
1471 struct cgraph_node *node;
1472 int total_size = 0, best_total_size = 0;
1474 ltrans_partition partition;
1475 unsigned int last_visited_cgraph_node = 0, last_visited_varpool_node = 0;
1476 struct varpool_node *vnode;
1477 int cost = 0, internal = 0;
1478 int best_n_nodes = 0, best_n_varpool_nodes = 0, best_i = 0, best_cost =
1479 INT_MAX, best_internal = 0;
1482 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1483 gcc_assert (!vnode->aux);
1484 /* Until we have better ordering facility, use toplogical order.
1485 Include only nodes we will partition and compute estimate of program
1486 size. Note that since nodes that are not partitioned might be put into
1487 multiple partitions, this is just an estimate of real size. This is why
1488 we keep partition_size updated after every partition is finalized. */
1489 postorder_len = ipa_reverse_postorder (postorder);
1490 for (i = 0; i < postorder_len; i++)
1492 node = postorder[i];
1493 if (partition_cgraph_node_p (node))
1495 order[n_nodes++] = node;
1496 total_size += inline_summary (node)->size;
1501 /* Compute partition size and create the first partition. */
1502 partition_size = total_size / PARAM_VALUE (PARAM_LTO_PARTITIONS);
1503 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1504 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1506 partition = new_partition ("");
1507 if (cgraph_dump_file)
1508 fprintf (cgraph_dump_file, "Total unit size: %i, partition size: %i\n",
1509 total_size, partition_size);
1511 for (i = 0; i < n_nodes; i++)
1514 add_cgraph_node_to_partition (partition, order[i]);
1515 total_size -= inline_summary (order[i])->size;
1517 /* Once we added a new node to the partition, we also want to add
1518 all referenced variables unless they was already added into some
1520 add_cgraph_node_to_partition adds possibly multiple nodes and
1521 variables that are needed to satisfy needs of ORDER[i].
1522 We remember last visited cgraph and varpool node from last iteration
1523 of outer loop that allows us to process every new addition.
1525 At the same time we compute size of the boundary into COST. Every
1526 callgraph or IPA reference edge leaving the partition contributes into
1527 COST. Every edge inside partition was earlier computed as one leaving
1528 it and thus we need to subtract it from COST. */
1529 while (last_visited_cgraph_node <
1530 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes)
1531 || last_visited_varpool_node < VEC_length (varpool_node_ptr,
1532 partition->varpool_set->
1535 struct ipa_ref_list *refs;
1537 struct ipa_ref *ref;
1538 bool cgraph_p = false;
1540 if (last_visited_cgraph_node <
1541 VEC_length (cgraph_node_ptr, partition->cgraph_set->nodes))
1543 struct cgraph_edge *edge;
1546 node = VEC_index (cgraph_node_ptr, partition->cgraph_set->nodes,
1547 last_visited_cgraph_node);
1548 refs = &node->ref_list;
1550 last_visited_cgraph_node++;
1552 gcc_assert (node->analyzed);
1554 /* Compute boundary cost of callgrpah edges. */
1555 for (edge = node->callees; edge; edge = edge->next_callee)
1556 if (edge->callee->analyzed)
1558 int edge_cost = edge->frequency;
1559 cgraph_node_set_iterator csi;
1563 gcc_assert (edge_cost > 0);
1564 csi = cgraph_node_set_find (partition->cgraph_set, edge->callee);
1565 if (!csi_end_p (csi)
1566 && csi.index < last_visited_cgraph_node - 1)
1567 cost -= edge_cost, internal+= edge_cost;
1571 for (edge = node->callers; edge; edge = edge->next_caller)
1573 int edge_cost = edge->frequency;
1574 cgraph_node_set_iterator csi;
1576 gcc_assert (edge->caller->analyzed);
1579 gcc_assert (edge_cost > 0);
1580 csi = cgraph_node_set_find (partition->cgraph_set, edge->caller);
1581 if (!csi_end_p (csi)
1582 && csi.index < last_visited_cgraph_node)
1591 &VEC_index (varpool_node_ptr, partition->varpool_set->nodes,
1592 last_visited_varpool_node)->ref_list;
1593 last_visited_varpool_node++;
1596 /* Compute boundary cost of IPA REF edges and at the same time look into
1597 variables referenced from current partition and try to add them. */
1598 for (j = 0; ipa_ref_list_reference_iterate (refs, j, ref); j++)
1599 if (ref->refered_type == IPA_REF_VARPOOL)
1601 varpool_node_set_iterator vsi;
1603 vnode = ipa_ref_varpool_node (ref);
1604 if (!vnode->finalized)
1606 if (!vnode->aux && partition_varpool_node_p (vnode))
1607 add_varpool_node_to_partition (partition, vnode);
1608 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1609 if (!vsi_end_p (vsi)
1610 && vsi.index < last_visited_varpool_node - !cgraph_p)
1617 cgraph_node_set_iterator csi;
1619 node = ipa_ref_node (ref);
1620 if (!node->analyzed)
1622 csi = cgraph_node_set_find (partition->cgraph_set, node);
1623 if (!csi_end_p (csi)
1624 && csi.index < last_visited_cgraph_node - cgraph_p)
1629 for (j = 0; ipa_ref_list_refering_iterate (refs, j, ref); j++)
1630 if (ref->refering_type == IPA_REF_VARPOOL)
1632 varpool_node_set_iterator vsi;
1634 vnode = ipa_ref_refering_varpool_node (ref);
1635 gcc_assert (vnode->finalized);
1636 if (!vnode->aux && partition_varpool_node_p (vnode))
1637 add_varpool_node_to_partition (partition, vnode);
1638 vsi = varpool_node_set_find (partition->varpool_set, vnode);
1639 if (!vsi_end_p (vsi)
1640 && vsi.index < last_visited_varpool_node)
1647 cgraph_node_set_iterator csi;
1649 node = ipa_ref_refering_node (ref);
1650 gcc_assert (node->analyzed);
1651 csi = cgraph_node_set_find (partition->cgraph_set, node);
1652 if (!csi_end_p (csi)
1653 && csi.index < last_visited_cgraph_node)
1660 /* If the partition is large enough, start looking for smallest boundary cost. */
1661 if (partition->insns < partition_size * 3 / 4
1662 || best_cost == INT_MAX
1664 || (best_internal * (HOST_WIDE_INT) cost
1665 > (internal * (HOST_WIDE_INT)best_cost)))
1666 && partition->insns < partition_size * 5 / 4))
1669 best_internal = internal;
1671 best_n_nodes = VEC_length (cgraph_node_ptr,
1672 partition->cgraph_set->nodes);
1673 best_n_varpool_nodes = VEC_length (varpool_node_ptr,
1674 partition->varpool_set->nodes);
1675 best_total_size = total_size;
1677 if (cgraph_dump_file)
1678 fprintf (cgraph_dump_file, "Step %i: added %s/%i, size %i, cost %i/%i best %i/%i, step %i\n", i,
1679 cgraph_node_name (order[i]), order[i]->uid, partition->insns, cost, internal,
1680 best_cost, best_internal, best_i);
1681 /* Partition is too large, unwind into step when best cost was reached and
1682 start new partition. */
1683 if (partition->insns > 2 * partition_size)
1687 if (cgraph_dump_file)
1688 fprintf (cgraph_dump_file, "Unwinding %i insertions to step %i\n",
1689 i - best_i, best_i);
1690 undo_partition (partition, best_n_nodes, best_n_varpool_nodes);
1693 /* When we are finished, avoid creating empty partition. */
1694 if (i == n_nodes - 1)
1696 partition = new_partition ("");
1697 last_visited_cgraph_node = 0;
1698 last_visited_varpool_node = 0;
1699 total_size = best_total_size;
1702 if (cgraph_dump_file)
1703 fprintf (cgraph_dump_file, "New partition\n");
1705 best_n_varpool_nodes = 0;
1706 best_cost = INT_MAX;
1708 /* Since the size of partitions is just approximate, update the size after
1709 we finished current one. */
1710 if (npartitions < PARAM_VALUE (PARAM_LTO_PARTITIONS))
1711 partition_size = total_size
1712 / (PARAM_VALUE (PARAM_LTO_PARTITIONS) - npartitions);
1714 partition_size = INT_MAX;
1716 if (partition_size < PARAM_VALUE (MIN_PARTITION_SIZE))
1717 partition_size = PARAM_VALUE (MIN_PARTITION_SIZE);
1722 /* Varables that are not reachable from the code go into last partition. */
1723 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1724 if (partition_varpool_node_p (vnode) && !vnode->aux)
1725 add_varpool_node_to_partition (partition, vnode);
1729 /* Promote variable VNODE to be static. */
1732 promote_var (struct varpool_node *vnode)
1734 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
1736 gcc_assert (flag_wpa);
1737 TREE_PUBLIC (vnode->decl) = 1;
1738 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
1739 DECL_VISIBILITY_SPECIFIED (vnode->decl) = true;
1740 if (cgraph_dump_file)
1741 fprintf (cgraph_dump_file,
1742 "Promoting var as hidden: %s\n", varpool_node_name (vnode));
1746 /* Promote function NODE to be static. */
1749 promote_fn (struct cgraph_node *node)
1751 gcc_assert (flag_wpa);
1752 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
1754 TREE_PUBLIC (node->decl) = 1;
1755 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
1756 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
1757 if (node->same_body)
1759 struct cgraph_node *alias;
1760 for (alias = node->same_body;
1761 alias; alias = alias->next)
1763 TREE_PUBLIC (alias->decl) = 1;
1764 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
1765 DECL_VISIBILITY_SPECIFIED (alias->decl) = true;
1768 if (cgraph_dump_file)
1769 fprintf (cgraph_dump_file,
1770 "Promoting function as hidden: %s/%i\n",
1771 cgraph_node_name (node), node->uid);
1775 /* Find out all static decls that need to be promoted to global because
1776 of cross file sharing. This function must be run in the WPA mode after
1777 all inlinees are added. */
1780 lto_promote_cross_file_statics (void)
1782 struct varpool_node *vnode;
1784 cgraph_node_set set;
1785 varpool_node_set vset;
1786 cgraph_node_set_iterator csi;
1787 varpool_node_set_iterator vsi;
1788 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
1789 struct pointer_set_t *inserted = pointer_set_create ();
1791 gcc_assert (flag_wpa);
1793 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
1794 for (i = 0; i < n_sets; i++)
1796 ltrans_partition part
1797 = VEC_index (ltrans_partition, ltrans_partitions, i);
1798 set = part->cgraph_set;
1799 vset = part->varpool_set;
1801 /* If node has either address taken (and we have no clue from where)
1802 or it is called from other partition, it needs to be globalized. */
1803 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
1805 struct cgraph_node *node = csi_node (csi);
1806 if (node->local.externally_visible)
1808 if (node->global.inlined_to)
1810 if ((!DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1811 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
1812 || reachable_from_other_partition_p (node, set)))
1815 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
1817 vnode = vsi_node (vsi);
1818 /* Constant pool references use internal labels and thus can not
1819 be made global. It is sensible to keep those ltrans local to
1820 allow better optimization. */
1821 if (!DECL_IN_CONSTANT_POOL (vnode->decl) && !DECL_COMDAT (vnode->decl)
1822 && !vnode->externally_visible && vnode->analyzed
1823 && referenced_from_other_partition_p (&vnode->ref_list,
1825 promote_var (vnode);
1828 /* We export the initializer of a read-only var into each partition
1829 referencing the var. Folding might take declarations from the
1830 initializer and use them, so everything referenced from the
1831 initializer can be accessed from this partition after folding.
1833 This means that we need to promote all variables and functions
1834 referenced from all initializers of read-only vars referenced
1835 from this partition that are not in this partition. This needs
1836 to be done recursively. */
1837 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1838 if (const_value_known_p (vnode->decl)
1839 && DECL_INITIAL (vnode->decl)
1840 && !varpool_node_in_set_p (vnode, vset)
1841 && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
1842 && !pointer_set_insert (inserted, vnode))
1843 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
1845 while (!VEC_empty (varpool_node_ptr, promoted_initializers))
1848 struct ipa_ref *ref;
1850 vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
1852 ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref);
1855 if (ref->refered_type == IPA_REF_CGRAPH)
1857 struct cgraph_node *n = ipa_ref_node (ref);
1858 gcc_assert (!n->global.inlined_to);
1859 if (!n->local.externally_visible
1860 && !cgraph_node_in_set_p (n, set))
1865 struct varpool_node *v = ipa_ref_varpool_node (ref);
1866 if (varpool_node_in_set_p (v, vset))
1869 /* Constant pool references use internal labels and thus
1870 cannot be made global. It is sensible to keep those
1871 ltrans local to allow better optimization. */
1872 if (DECL_IN_CONSTANT_POOL (v->decl))
1874 if (!pointer_set_insert (inserted, vnode))
1875 VEC_safe_push (varpool_node_ptr, heap,
1876 promoted_initializers, v);
1878 else if (!v->externally_visible && v->analyzed)
1881 && DECL_INITIAL (v->decl)
1882 && const_value_known_p (v->decl)
1883 && !pointer_set_insert (inserted, vnode))
1884 VEC_safe_push (varpool_node_ptr, heap,
1885 promoted_initializers, v);
1891 pointer_set_destroy (inserted);
1894 static lto_file *current_lto_file;
1896 /* Helper for qsort; compare partitions and return one with smaller size.
1897 We sort from greatest to smallest so parallel build doesn't stale on the
1898 longest compilation being executed too late. */
1901 cmp_partitions (const void *a, const void *b)
1903 const struct ltrans_partition_def *pa
1904 = *(struct ltrans_partition_def *const *)a;
1905 const struct ltrans_partition_def *pb
1906 = *(struct ltrans_partition_def *const *)b;
1907 return pb->insns - pa->insns;
1910 /* Write all output files in WPA mode and the file with the list of
1914 lto_wpa_write_files (void)
1918 cgraph_node_set set;
1919 varpool_node_set vset;
1920 ltrans_partition part;
1921 FILE *ltrans_output_list_stream;
1922 char *temp_filename;
1925 /* Open the LTRANS output list. */
1926 if (!ltrans_output_list)
1927 fatal_error ("no LTRANS output list filename provided");
1928 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
1929 if (ltrans_output_list_stream == NULL)
1930 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
1932 timevar_push (TV_WHOPR_WPA);
1934 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
1935 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
1936 part->cgraph_set->nodes);
1938 /* Find out statics that need to be promoted
1939 to globals with hidden visibility because they are accessed from multiple
1941 lto_promote_cross_file_statics ();
1943 timevar_pop (TV_WHOPR_WPA);
1945 timevar_push (TV_WHOPR_WPA_IO);
1947 /* Generate a prefix for the LTRANS unit files. */
1948 blen = strlen (ltrans_output_list);
1949 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
1950 strcpy (temp_filename, ltrans_output_list);
1951 if (blen > sizeof (".out")
1952 && strcmp (temp_filename + blen - sizeof (".out") + 1,
1954 temp_filename[blen - sizeof (".out") + 1] = '\0';
1955 blen = strlen (temp_filename);
1957 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
1958 VEC_qsort (ltrans_partition, ltrans_partitions, cmp_partitions);
1959 for (i = 0; i < n_sets; i++)
1962 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
1964 set = part->cgraph_set;
1965 vset = part->varpool_set;
1967 /* Write all the nodes in SET. */
1968 sprintf (temp_filename + blen, "%u.o", i);
1969 file = lto_obj_file_open (temp_filename, true);
1971 fatal_error ("lto_obj_file_open() failed");
1974 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
1975 if (cgraph_dump_file)
1977 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
1978 part->name, temp_filename, part->insns);
1979 fprintf (cgraph_dump_file, "cgraph nodes:");
1980 dump_cgraph_node_set (cgraph_dump_file, set);
1981 fprintf (cgraph_dump_file, "varpool nodes:");
1982 dump_varpool_node_set (cgraph_dump_file, vset);
1984 gcc_checking_assert (cgraph_node_set_nonempty_p (set)
1985 || varpool_node_set_nonempty_p (vset) || !i);
1987 lto_set_current_out_file (file);
1989 ipa_write_optimization_summaries (set, vset);
1991 lto_set_current_out_file (NULL);
1992 lto_obj_file_close (file);
1994 len = strlen (temp_filename);
1995 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
1996 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1997 fatal_error ("writing to LTRANS output list %s: %m",
1998 ltrans_output_list);
2001 lto_stats.num_output_files += n_sets;
2003 /* Close the LTRANS output list. */
2004 if (fclose (ltrans_output_list_stream))
2005 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2007 free_ltrans_partitions();
2009 timevar_pop (TV_WHOPR_WPA_IO);
2013 /* If TT is a variable or function decl replace it with its
2014 prevailing variant. */
2015 #define LTO_SET_PREVAIL(tt) \
2017 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
2018 tt = lto_symtab_prevailing_decl (tt); \
2021 /* Ensure that TT isn't a replacable var of function decl. */
2022 #define LTO_NO_PREVAIL(tt) \
2023 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2025 /* Given a tree T replace all fields referring to variables or functions
2026 with their prevailing variant. */
2028 lto_fixup_prevailing_decls (tree t)
2030 enum tree_code code = TREE_CODE (t);
2031 LTO_NO_PREVAIL (TREE_TYPE (t));
2032 LTO_NO_PREVAIL (TREE_CHAIN (t));
2035 LTO_NO_PREVAIL (DECL_NAME (t));
2036 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2037 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2039 LTO_SET_PREVAIL (DECL_SIZE (t));
2040 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2041 LTO_SET_PREVAIL (DECL_INITIAL (t));
2042 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2043 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2045 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2047 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2048 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
2050 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2052 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
2053 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2054 LTO_NO_PREVAIL (DECL_VINDEX (t));
2056 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2057 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2058 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2060 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
2061 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2062 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2063 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2064 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2067 else if (TYPE_P (t))
2069 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2070 LTO_SET_PREVAIL (TYPE_SIZE (t));
2071 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2072 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2073 LTO_NO_PREVAIL (TYPE_NAME (t));
2075 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2076 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2077 LTO_SET_PREVAIL (t->type_non_common.binfo);
2079 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2081 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2082 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2083 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2085 else if (EXPR_P (t))
2088 LTO_NO_PREVAIL (t->exp.block);
2089 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2090 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2097 LTO_SET_PREVAIL (TREE_VALUE (t));
2098 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2105 #undef LTO_SET_PREVAIL
2106 #undef LTO_NO_PREVAIL
2108 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2109 replaces var and function decls with the corresponding prevailing def. */
2112 lto_fixup_state (struct lto_in_decl_state *state)
2115 struct lto_tree_ref_table *table;
2117 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2118 we still need to walk from all DECLs to find the reachable
2119 FUNCTION_DECLs and VAR_DECLs. */
2120 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2122 table = &state->streams[si];
2123 for (i = 0; i < table->size; i++)
2125 tree *tp = table->trees + i;
2126 if (VAR_OR_FUNCTION_DECL_P (*tp))
2127 *tp = lto_symtab_prevailing_decl (*tp);
2132 /* A callback of htab_traverse. Just extracts a state from SLOT
2133 and calls lto_fixup_state. */
2136 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
2138 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
2139 lto_fixup_state (state);
2143 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2147 lto_fixup_decls (struct lto_file_decl_data **files)
2153 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
2154 lto_fixup_prevailing_decls (t);
2156 for (i = 0; files[i]; i++)
2158 struct lto_file_decl_data *file = files[i];
2159 struct lto_in_decl_state *state = file->global_decl_state;
2160 lto_fixup_state (state);
2162 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
2166 /* Read the options saved from each file in the command line. Called
2167 from lang_hooks.post_options which is called by process_options
2168 right before all the options are used to initialize the compiler.
2169 This assumes that decode_options has already run, so the
2170 num_in_fnames and in_fnames are properly set.
2172 Note that this assumes that all the files had been compiled with
2173 the same options, which is not a good assumption. In general,
2174 options ought to be read from all the files in the set and merged.
2175 However, it is still unclear what the merge rules should be. */
2178 lto_read_all_file_options (void)
2182 /* Clear any file options currently saved. */
2183 lto_clear_file_options ();
2185 /* Set the hooks to read ELF sections. */
2186 lto_set_in_hooks (NULL, get_section_data, free_section_data);
2188 fprintf (stderr, "Reading command line options:");
2190 for (i = 0; i < num_in_fnames; i++)
2192 struct lto_file_decl_data *file_data;
2193 lto_file *file = lto_obj_file_open (in_fnames[i], false);
2198 fprintf (stderr, " %s", in_fnames[i]);
2202 file_data = XCNEW (struct lto_file_decl_data);
2203 file_data->file_name = file->filename;
2204 file_data->section_hash_table = lto_obj_build_section_table (file);
2206 lto_read_file_options (file_data);
2208 lto_obj_file_close (file);
2209 htab_delete (file_data->section_hash_table);
2214 fprintf (stderr, "\n");
2216 /* Apply globally the options read from all the files. */
2217 lto_reissue_options ();
2220 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2222 /* Turn file datas for sub files into a single array, so that they look
2223 like separate files for further passes. */
2226 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2228 struct lto_file_decl_data *n, *next;
2231 lto_stats.num_input_files = count;
2233 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
2234 /* Set the hooks so that all of the ipa passes can read in their data. */
2235 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2236 for (i = 0, k = 0; i < last_file_ix; i++)
2238 for (n = orig[i]; n != NULL; n = next)
2240 all_file_decl_data[k++] = n;
2245 all_file_decl_data[k] = NULL;
2246 gcc_assert (k == count);
2249 /* Input file data before flattening (i.e. splitting them to subfiles to support
2250 incremental linking. */
2251 static int real_file_count;
2252 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2254 /* Read all the symbols from the input files FNAMES. NFILES is the
2255 number of files requested in the command line. Instantiate a
2256 global call graph by aggregating all the sub-graphs found in each
2260 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2262 unsigned int i, last_file_ix;
2264 struct cgraph_node *node;
2266 struct lto_file_decl_data **decl_data;
2270 timevar_push (TV_IPA_LTO_DECL_IN);
2273 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
2274 real_file_count = nfiles;
2276 /* Read the resolution file. */
2278 if (resolution_file_name)
2281 unsigned num_objects;
2283 resolution = fopen (resolution_file_name, "r");
2284 if (resolution == NULL)
2285 fatal_error ("could not open symbol resolution file: %m");
2287 t = fscanf (resolution, "%u", &num_objects);
2288 gcc_assert (t == 1);
2290 /* True, since the plugin splits the archives. */
2291 gcc_assert (num_objects == nfiles);
2294 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
2298 fprintf (stderr, "Reading object files:");
2300 /* Read all of the object files specified on the command line. */
2301 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2303 struct lto_file_decl_data *file_data = NULL;
2306 fprintf (stderr, " %s", fnames[i]);
2310 current_lto_file = lto_obj_file_open (fnames[i], false);
2311 if (!current_lto_file)
2314 file_data = lto_file_read (current_lto_file, resolution, &count);
2317 lto_obj_file_close (current_lto_file);
2318 current_lto_file = NULL;
2322 decl_data[last_file_ix++] = file_data;
2324 lto_obj_file_close (current_lto_file);
2325 current_lto_file = NULL;
2329 lto_flatten_files (decl_data, count, last_file_ix);
2330 lto_stats.num_input_files = count;
2331 ggc_free(decl_data);
2332 real_file_decl_data = NULL;
2334 if (resolution_file_name)
2335 fclose (resolution);
2337 /* Set the hooks so that all of the ipa passes can read in their data. */
2338 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2340 timevar_pop (TV_IPA_LTO_DECL_IN);
2343 fprintf (stderr, "\nReading the callgraph\n");
2345 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2346 /* Read the callgraph. */
2348 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2351 fprintf (stderr, "Merging declarations\n");
2353 timevar_push (TV_IPA_LTO_DECL_MERGE);
2354 /* Merge global decls. */
2355 lto_symtab_merge_decls ();
2357 /* If there were errors during symbol merging bail out, we have no
2358 good way to recover here. */
2360 fatal_error ("errors during merging of translation units");
2362 /* Fixup all decls and types and free the type hash tables. */
2363 lto_fixup_decls (all_file_decl_data);
2364 htab_delete (tree_with_vars);
2365 tree_with_vars = NULL;
2366 free_gimple_type_tables ();
2369 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2370 /* Each pass will set the appropriate timer. */
2373 fprintf (stderr, "Reading summaries\n");
2375 /* Read the IPA summary data. */
2377 ipa_read_optimization_summaries ();
2379 ipa_read_summaries ();
2381 /* Finally merge the cgraph according to the decl merging decisions. */
2382 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2383 if (cgraph_dump_file)
2385 fprintf (cgraph_dump_file, "Before merging:\n");
2386 dump_cgraph (cgraph_dump_file);
2387 dump_varpool (cgraph_dump_file);
2389 lto_symtab_merge_cgraph_nodes ();
2393 for (node = cgraph_nodes; node; node = node->next)
2395 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
2396 summaries computed and needs to apply changes. At the moment WHOPR only
2397 supports inlining, so we can push it here by hand. In future we need to stream
2398 this field into ltrans compilation. */
2400 VEC_safe_push (ipa_opt_pass, heap,
2401 node->ipa_transforms_to_apply,
2402 (ipa_opt_pass)&pass_ipa_inline);
2406 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2408 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
2410 /* FIXME lto. This loop needs to be changed to use the pass manager to
2411 call the ipa passes directly. */
2413 for (i = 0; i < last_file_ix; i++)
2415 struct lto_file_decl_data *file_data = all_file_decl_data [i];
2416 lto_materialize_constructors_and_inits (file_data);
2419 /* Indicate that the cgraph is built and ready. */
2420 cgraph_function_flags_ready = true;
2422 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
2423 ggc_free (all_file_decl_data);
2424 all_file_decl_data = NULL;
2428 /* Materialize all the bodies for all the nodes in the callgraph. */
2431 materialize_cgraph (void)
2434 struct cgraph_node *node;
2436 timevar_id_t lto_timer;
2440 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2443 /* Now that we have input the cgraph, we need to clear all of the aux
2444 nodes and read the functions if we are not running in WPA mode. */
2445 timevar_push (TV_IPA_LTO_GIMPLE_IN);
2447 for (node = cgraph_nodes; node; node = node->next)
2449 if (node->local.lto_file_data)
2451 lto_materialize_function (node);
2452 lto_stats.num_input_cgraph_nodes++;
2456 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
2458 /* Start the appropriate timer depending on the mode that we are
2460 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2461 : (flag_ltrans) ? TV_WHOPR_LTRANS
2463 timevar_push (lto_timer);
2465 current_function_decl = NULL;
2468 /* Inform the middle end about the global variables we have seen. */
2469 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
2470 rest_of_decl_compilation (decl, 1, 0);
2473 fprintf (stderr, "\n");
2475 timevar_pop (lto_timer);
2479 /* Perform whole program analysis (WPA) on the callgraph and write out the
2480 optimization plan. */
2483 do_whole_program_analysis (void)
2485 /* Note that since we are in WPA mode, materialize_cgraph will not
2486 actually read in all the function bodies. It only materializes
2487 the decls and cgraph nodes so that analysis can be performed. */
2488 materialize_cgraph ();
2490 /* Reading in the cgraph uses different timers, start timing WPA now. */
2491 timevar_push (TV_WHOPR_WPA);
2493 if (pre_ipa_mem_report)
2495 fprintf (stderr, "Memory consumption before IPA\n");
2496 dump_memory_report (false);
2499 cgraph_function_flags_ready = true;
2501 if (cgraph_dump_file)
2503 dump_cgraph (cgraph_dump_file);
2504 dump_varpool (cgraph_dump_file);
2506 bitmap_obstack_initialize (NULL);
2507 cgraph_state = CGRAPH_STATE_IPA_SSA;
2509 execute_ipa_pass_list (all_regular_ipa_passes);
2511 if (cgraph_dump_file)
2513 fprintf (cgraph_dump_file, "Optimized ");
2514 dump_cgraph (cgraph_dump_file);
2515 dump_varpool (cgraph_dump_file);
2518 bitmap_obstack_release (NULL);
2520 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
2521 timevar_pop (TV_WHOPR_WPA);
2523 if (flag_lto_partition_1to1)
2526 lto_balanced_map ();
2530 fprintf (stderr, "\nStreaming out");
2533 lto_wpa_write_files ();
2536 fprintf (stderr, "\n");
2538 if (post_ipa_mem_report)
2540 fprintf (stderr, "Memory consumption after IPA\n");
2541 dump_memory_report (false);
2544 /* Show the LTO report before launching LTRANS. */
2545 if (flag_lto_report)
2546 print_lto_report ();
2550 static GTY(()) tree lto_eh_personality_decl;
2552 /* Return the LTO personality function decl. */
2555 lto_eh_personality (void)
2557 if (!lto_eh_personality_decl)
2559 /* Use the first personality DECL for our personality if we don't
2560 support multiple ones. This ensures that we don't artificially
2561 create the need for them in a single-language program. */
2562 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2563 lto_eh_personality_decl = first_personality_decl;
2565 lto_eh_personality_decl = lhd_gcc_personality ();
2568 return lto_eh_personality_decl;
2571 /* Set the process name based on the LTO mode. */
2574 lto_process_name (void)
2577 setproctitle ("lto1-lto");
2579 setproctitle ("lto1-wpa");
2581 setproctitle ("lto1-ltrans");
2584 /* Main entry point for the GIMPLE front end. This front end has
2585 three main personalities:
2587 - LTO (-flto). All the object files on the command line are
2588 loaded in memory and processed as a single translation unit.
2589 This is the traditional link-time optimization behavior.
2591 - WPA (-fwpa). Only the callgraph and summary information for
2592 files in the command file are loaded. A single callgraph
2593 (without function bodies) is instantiated for the whole set of
2594 files. IPA passes are only allowed to analyze the call graph
2595 and make transformation decisions. The callgraph is
2596 partitioned, each partition is written to a new object file
2597 together with the transformation decisions.
2599 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2600 summary files from running again. Since WPA computed summary
2601 information and decided what transformations to apply, LTRANS
2602 simply applies them. */
2607 lto_process_name ();
2611 /* Read all the symbols and call graph from all the files in the
2613 read_cgraph_and_symbols (num_in_fnames, in_fnames);
2617 /* If WPA is enabled analyze the whole call graph and create an
2618 optimization plan. Otherwise, read in all the function
2619 bodies and continue with optimization. */
2621 do_whole_program_analysis ();
2624 materialize_cgraph ();
2626 /* Let the middle end know that we have read and merged all of
2630 /* FIXME lto, if the processes spawned by WPA fail, we miss
2631 the chance to print WPA's report, so WPA will call
2632 print_lto_report before launching LTRANS. If LTRANS was
2633 launched directly by the driver we would not need to do
2635 if (flag_lto_report)
2636 print_lto_report ();
2641 #include "gt-lto-lto.h"