1 /* Top-level LTO routines.
2 Copyright 2009, 2010 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 "diagnostic-core.h"
29 #include "libiberty.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 /* This needs to be included after config.h. Otherwise, _GNU_SOURCE will not
49 be defined in time to set __USE_GNU in the system headers, and strsignal
50 will not be declared. */
55 /* Handle opening elf files on hosts, such as Windows, that may use
56 text file handling that will break binary access. */
62 static GTY(()) tree first_personality_decl;
64 /* Returns a hash code for P. */
67 hash_name (const void *p)
69 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
70 return (hashval_t) htab_hash_string (ds->name);
74 /* Returns nonzero if P1 and P2 are equal. */
77 eq_name (const void *p1, const void *p2)
79 const struct lto_section_slot *s1 =
80 (const struct lto_section_slot *) p1;
81 const struct lto_section_slot *s2 =
82 (const struct lto_section_slot *) p2;
84 return strcmp (s1->name, s2->name) == 0;
87 /* Free lto_section_slot */
90 free_with_string (void *arg)
92 struct lto_section_slot *s = (struct lto_section_slot *)arg;
94 free (CONST_CAST (char *, s->name));
98 /* Create section hash table */
101 lto_obj_create_section_hash_table (void)
103 return htab_create (37, hash_name, eq_name, free_with_string);
106 /* Read the constructors and inits. */
109 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
112 const char *data = lto_get_section_data (file_data,
113 LTO_section_static_initializer,
115 lto_input_constructors_and_inits (file_data, data);
116 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
120 /* Return true when NODE has a clone that is analyzed (i.e. we need
121 to load its body even if the node itself is not needed). */
124 has_analyzed_clone_p (struct cgraph_node *node)
126 struct cgraph_node *orig = node;
135 else if (node->next_sibling_clone)
136 node = node->next_sibling_clone;
139 while (node != orig && !node->next_sibling_clone)
140 node = node->clone_of;
142 node = node->next_sibling_clone;
148 /* Read the function body for the function associated with NODE. */
151 lto_materialize_function (struct cgraph_node *node)
154 struct lto_file_decl_data *file_data;
155 const char *data, *name;
159 /* Read in functions with body (analyzed nodes)
160 and also functions that are needed to produce virtual clones. */
161 if (node->analyzed || has_analyzed_clone_p (node))
163 /* Clones don't need to be read. */
166 file_data = node->local.lto_file_data;
167 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
169 /* We may have renamed the declaration, e.g., a static function. */
170 name = lto_get_decl_name_mapping (file_data, name);
172 data = lto_get_section_data (file_data, LTO_section_function_body,
175 fatal_error ("%s: section %s is missing",
176 file_data->file_name,
179 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
181 /* Load the function body only if not operating in WPA mode. In
182 WPA mode, the body of the function is not needed. */
185 allocate_struct_function (decl, false);
186 announce_function (decl);
187 lto_input_function_body (file_data, decl, data);
188 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
189 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
190 lto_stats.num_function_bodies++;
193 lto_free_section_data (file_data, LTO_section_function_body, name,
199 /* Let the middle end know about the function. */
200 rest_of_decl_compilation (decl, 1, 0);
204 /* Decode the content of memory pointed to by DATA in the the
205 in decl state object STATE. DATA_IN points to a data_in structure for
206 decoding. Return the address after the decoded object in the input. */
208 static const uint32_t *
209 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
210 struct lto_in_decl_state *state)
217 decl = lto_streamer_cache_get (data_in->reader_cache, (int) ix);
218 if (TREE_CODE (decl) != FUNCTION_DECL)
220 gcc_assert (decl == void_type_node);
223 state->fn_decl = decl;
225 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
227 uint32_t size = *data++;
228 tree *decls = ggc_alloc_vec_tree (size);
230 for (j = 0; j < size; j++)
232 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
234 /* Register every type in the global type table. If the
235 type existed already, use the existing type. */
236 if (TYPE_P (decls[j]))
237 decls[j] = gimple_register_type (decls[j]);
240 state->streams[i].size = size;
241 state->streams[i].trees = decls;
249 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
250 RESOLUTIONS is the set of symbols picked by the linker (read from the
251 resolution file when the linker plugin is being used). */
254 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
255 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
257 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
258 const int32_t decl_offset = sizeof (struct lto_decl_header);
259 const int32_t main_offset = decl_offset + header->decl_state_size;
260 const int32_t string_offset = main_offset + header->main_size;
261 struct lto_input_block ib_main;
262 struct data_in *data_in;
264 const uint32_t *data_ptr, *data_end;
265 uint32_t num_decl_states;
267 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
270 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
271 header->string_size, resolutions);
273 /* Read the global declarations and types. */
274 while (ib_main.p < ib_main.len)
276 tree t = lto_input_tree (&ib_main, data_in);
277 gcc_assert (t && ib_main.p <= ib_main.len);
280 /* Read in lto_in_decl_state objects. */
281 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
283 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
284 num_decl_states = *data_ptr++;
286 gcc_assert (num_decl_states > 0);
287 decl_data->global_decl_state = lto_new_in_decl_state ();
288 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
289 decl_data->global_decl_state);
291 /* Read in per-function decl states and enter them in hash table. */
292 decl_data->function_decl_states =
293 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
295 for (i = 1; i < num_decl_states; i++)
297 struct lto_in_decl_state *state = lto_new_in_decl_state ();
300 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
301 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
302 gcc_assert (*slot == NULL);
306 if (data_ptr != data_end)
307 internal_error ("bytecode stream: garbage at the end of symbols section");
309 /* Set the current decl state to be the global state. */
310 decl_data->current_decl_state = decl_data->global_decl_state;
312 lto_data_in_delete (data_in);
315 /* strtoll is not portable. */
317 lto_parse_hex (const char *p) {
319 for (; *p != '\0'; ++p)
324 if (c >= '0' && c <= '9')
326 else if (c >= 'a' && c <= 'f')
328 else if (c >= 'A' && c <= 'F')
331 internal_error ("could not parse hex number");
337 /* Read resolution for file named FILE_NAME. The resolution is read from
341 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
343 /* We require that objects in the resolution file are in the same
344 order as the lto1 command line. */
345 unsigned int name_len;
347 unsigned int num_symbols;
349 struct lto_file_decl_data *file_data;
350 unsigned max_index = 0;
351 splay_tree_node nd = NULL;
356 name_len = strlen (file->filename);
357 obj_name = XNEWVEC (char, name_len + 1);
358 fscanf (resolution, " "); /* Read white space. */
360 fread (obj_name, sizeof (char), name_len, resolution);
361 obj_name[name_len] = '\0';
362 if (strcmp (obj_name, file->filename) != 0)
363 internal_error ("unexpected file name %s in linker resolution file. "
364 "Expected %s", obj_name, file->filename);
365 if (file->offset != 0)
370 t = fscanf (resolution, "@0x%16s", offset_p);
372 internal_error ("could not parse file offset");
373 offset = lto_parse_hex (offset_p);
374 if (offset != file->offset)
375 internal_error ("unexpected offset");
380 fscanf (resolution, "%u", &num_symbols);
382 for (i = 0; i < num_symbols; i++)
387 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
389 unsigned int lto_resolution_str_len =
390 sizeof (lto_resolution_str) / sizeof (char *);
392 t = fscanf (resolution, "%u %x %26s %*[^\n]\n", &index, &id, r_str);
394 internal_error ("Invalid line in the resolution file.");
395 if (index > max_index)
398 for (j = 0; j < lto_resolution_str_len; j++)
400 if (strcmp (lto_resolution_str[j], r_str) == 0)
402 r = (enum ld_plugin_symbol_resolution) j;
406 if (j == lto_resolution_str_len)
407 internal_error ("Invalid resolution in the resolution file.");
409 if (!(nd && nd->key == id))
411 nd = splay_tree_lookup (file_ids, id);
413 internal_error ("Resolution sub id %x not in object file", id);
416 file_data = (struct lto_file_decl_data *)nd->value;
417 if (cgraph_dump_file)
418 fprintf (cgraph_dump_file, "Adding resolution %u %u to id %x\n",
419 index, r, file_data->id);
420 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap,
421 file_data->resolutions,
423 VEC_replace (ld_plugin_symbol_resolution_t,
424 file_data->resolutions, index, r);
428 /* Is the name for a id'ed LTO section? */
431 lto_section_with_id (const char *name, unsigned *id)
435 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
437 s = strrchr (name, '.');
438 return s && sscanf (s, ".%x", id) == 1;
441 /* Create file_data of each sub file id */
444 create_subid_section_table (void **slot, void *data)
446 struct lto_section_slot s_slot, *new_slot;
447 struct lto_section_slot *ls = *(struct lto_section_slot **)slot;
448 splay_tree file_ids = (splay_tree)data;
453 struct lto_file_decl_data *file_data;
455 if (!lto_section_with_id (ls->name, &id))
458 /* Find hash table of sub module id */
459 nd = splay_tree_lookup (file_ids, id);
462 file_data = (struct lto_file_decl_data *)nd->value;
466 file_data = ggc_alloc_lto_file_decl_data ();
467 memset(file_data, 0, sizeof (struct lto_file_decl_data));
469 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
470 splay_tree_insert (file_ids, id, (splay_tree_value)file_data);
473 /* Copy section into sub module hash table */
474 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
475 s_slot.name = new_name;
476 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
477 gcc_assert (*hash_slot == NULL);
479 new_slot = XDUP (struct lto_section_slot, ls);
480 new_slot->name = new_name;
481 *hash_slot = new_slot;
485 /* Read declarations and other initializations for a FILE_DATA. */
488 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
493 file_data->renaming_hash_table = lto_create_renaming_table ();
494 file_data->file_name = file->filename;
495 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
496 gcc_assert (data != NULL);
497 lto_read_decls (file_data, data, file_data->resolutions);
498 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
504 struct lto_file_decl_data **file_data;
508 /* Traverse ids and create a list of file_datas out of it. */
510 static int lto_create_files_from_ids (splay_tree_node node, void *data)
512 struct lwstate *lw = (struct lwstate *)data;
513 struct lto_file_decl_data *file_data = (struct lto_file_decl_data *)node->value;
515 lto_file_finalize (file_data, lw->file);
516 if (cgraph_dump_file)
517 fprintf (cgraph_dump_file, "Creating file %s with sub id %x\n",
518 file_data->file_name, file_data->id);
519 file_data->next = *lw->file_data;
520 *lw->file_data = file_data;
525 /* Generate a TREE representation for all types and external decls
528 Read all of the globals out of the file. Then read the cgraph
529 and process the .o index into the cgraph nodes so that it can open
530 the .o file to load the functions and ipa information. */
532 static struct lto_file_decl_data *
533 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
535 struct lto_file_decl_data *file_data = NULL;
537 htab_t section_hash_table;
538 struct lwstate state;
540 section_hash_table = lto_obj_build_section_table (file);
542 /* Find all sub modules in the object and put their sections into new hash
543 tables in a splay tree. */
544 file_ids = splay_tree_new (splay_tree_compare_ints, NULL, NULL);
545 htab_traverse (section_hash_table, create_subid_section_table, file_ids);
547 /* Add resolutions to file ids */
548 lto_resolution_read (file_ids, resolution_file, file);
550 /* Finalize each lto file for each submodule in the merged object
551 and create list for returning. */
553 state.file_data = &file_data;
555 splay_tree_foreach (file_ids, lto_create_files_from_ids, &state);
557 splay_tree_delete (file_ids);
558 htab_delete (section_hash_table);
563 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
564 #define LTO_MMAP_IO 1
568 /* Page size of machine is used for mmap and munmap calls. */
569 static size_t page_mask;
572 /* Get the section data of length LEN from FILENAME starting at
573 OFFSET. The data segment must be freed by the caller when the
574 caller is finished. Returns NULL if all was not well. */
577 lto_read_section_data (struct lto_file_decl_data *file_data,
578 intptr_t offset, size_t len)
582 static char *fd_name;
584 intptr_t computed_len;
585 intptr_t computed_offset;
589 /* Keep a single-entry file-descriptor cache. The last file we
590 touched will get closed at exit.
591 ??? Eventually we want to add a more sophisticated larger cache
592 or rather fix function body streaming to not stream them in
593 practically random order. */
595 && strcmp (fd_name, file_data->file_name) != 0)
603 fd_name = xstrdup (file_data->file_name);
604 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
612 size_t page_size = sysconf (_SC_PAGE_SIZE);
613 page_mask = ~(page_size - 1);
616 computed_offset = offset & page_mask;
617 diff = offset - computed_offset;
618 computed_len = len + diff;
620 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
621 fd, computed_offset);
622 if (result == MAP_FAILED)
625 return result + diff;
627 result = (char *) xmalloc (len);
628 if (lseek (fd, offset, SEEK_SET) != offset
629 || read (fd, result, len) != (ssize_t) len)
640 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
641 NAME will be NULL unless the section type is for a function
645 get_section_data (struct lto_file_decl_data *file_data,
646 enum lto_section_type section_type,
650 htab_t section_hash_table = file_data->section_hash_table;
651 struct lto_section_slot *f_slot;
652 struct lto_section_slot s_slot;
653 const char *section_name = lto_get_section_name (section_type, name, file_data);
657 s_slot.name = section_name;
658 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
661 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
665 free (CONST_CAST (char *, section_name));
670 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
671 starts at OFFSET and has LEN bytes. */
674 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
675 enum lto_section_type section_type ATTRIBUTE_UNUSED,
676 const char *name ATTRIBUTE_UNUSED,
677 const char *offset, size_t len ATTRIBUTE_UNUSED)
680 intptr_t computed_len;
681 intptr_t computed_offset;
686 computed_offset = ((intptr_t) offset) & page_mask;
687 diff = (intptr_t) offset - computed_offset;
688 computed_len = len + diff;
690 munmap ((caddr_t) computed_offset, computed_len);
692 free (CONST_CAST(char *, offset));
696 /* Structure describing ltrans partitions. */
698 struct GTY (()) ltrans_partition_def
700 cgraph_node_set cgraph_set;
701 varpool_node_set varpool_set;
702 const char * GTY ((skip)) name;
706 typedef struct ltrans_partition_def *ltrans_partition;
707 DEF_VEC_P(ltrans_partition);
708 DEF_VEC_ALLOC_P(ltrans_partition,gc);
710 static GTY (()) VEC(ltrans_partition, gc) *ltrans_partitions;
712 static void add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node);
713 static void add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode);
715 /* Create new partition with name NAME. */
716 static ltrans_partition
717 new_partition (const char *name)
719 ltrans_partition part = ggc_alloc_ltrans_partition_def ();
720 part->cgraph_set = cgraph_node_set_new ();
721 part->varpool_set = varpool_node_set_new ();
724 VEC_safe_push (ltrans_partition, gc, ltrans_partitions, part);
728 /* See all references that go to comdat objects and bring them into partition too. */
730 add_references_to_partition (ltrans_partition part, struct ipa_ref_list *refs)
734 for (i = 0; ipa_ref_list_reference_iterate (refs, i, ref); i++)
736 if (ref->refered_type == IPA_REF_CGRAPH
737 && DECL_COMDAT (ipa_ref_node (ref)->decl)
738 && !cgraph_node_in_set_p (ipa_ref_node (ref), part->cgraph_set))
739 add_cgraph_node_to_partition (part, ipa_ref_node (ref));
741 if (ref->refered_type == IPA_REF_VARPOOL
742 && DECL_COMDAT (ipa_ref_varpool_node (ref)->decl)
743 && !varpool_node_in_set_p (ipa_ref_varpool_node (ref), part->varpool_set))
744 add_varpool_node_to_partition (part, ipa_ref_varpool_node (ref));
748 /* Add NODE to partition as well as the inline callees and referred comdats into partition PART. */
751 add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node)
753 struct cgraph_edge *e;
755 part->insns += node->local.inline_summary.self_size;
759 gcc_assert (node->aux != part);
760 node->in_other_partition = 1;
765 cgraph_node_set_add (part->cgraph_set, node);
767 for (e = node->callees; e; e = e->next_callee)
768 if ((!e->inline_failed || DECL_COMDAT (e->callee->decl))
769 && !cgraph_node_in_set_p (e->callee, part->cgraph_set))
770 add_cgraph_node_to_partition (part, e->callee);
772 add_references_to_partition (part, &node->ref_list);
774 if (node->same_comdat_group
775 && !cgraph_node_in_set_p (node->same_comdat_group, part->cgraph_set))
776 add_cgraph_node_to_partition (part, node->same_comdat_group);
779 /* Add VNODE to partition as well as comdat references partition PART. */
782 add_varpool_node_to_partition (ltrans_partition part, struct varpool_node *vnode)
784 varpool_node_set_add (part->varpool_set, vnode);
788 gcc_assert (vnode->aux != part);
789 vnode->in_other_partition = 1;
794 add_references_to_partition (part, &vnode->ref_list);
796 if (vnode->same_comdat_group
797 && !varpool_node_in_set_p (vnode->same_comdat_group, part->varpool_set))
798 add_varpool_node_to_partition (part, vnode->same_comdat_group);
801 /* Group cgrah nodes by input files. This is used mainly for testing
805 lto_1_to_1_map (void)
807 struct cgraph_node *node;
808 struct varpool_node *vnode;
809 struct lto_file_decl_data *file_data;
810 struct pointer_map_t *pmap;
811 ltrans_partition partition;
815 timevar_push (TV_WHOPR_WPA);
817 pmap = pointer_map_create ();
819 for (node = cgraph_nodes; node; node = node->next)
821 /* We will get proper partition based on function they are inlined to. */
822 if (node->global.inlined_to)
824 /* Nodes without a body do not need partitioning. */
827 /* Extern inlines and comdat are always only in partitions they are needed. */
828 if (DECL_EXTERNAL (node->decl)
829 || DECL_COMDAT (node->decl))
832 file_data = node->local.lto_file_data;
833 gcc_assert (!node->same_body_alias);
837 slot = pointer_map_contains (pmap, file_data);
839 partition = (ltrans_partition) *slot;
842 partition = new_partition (file_data->file_name);
843 slot = pointer_map_insert (pmap, file_data);
849 && VEC_length (ltrans_partition, ltrans_partitions))
850 partition = VEC_index (ltrans_partition, ltrans_partitions, 0);
853 partition = new_partition ("");
854 slot = pointer_map_insert (pmap, NULL);
859 add_cgraph_node_to_partition (partition, node);
862 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
864 if (vnode->alias || !vnode->needed)
866 /* Constant pool and comdat are always only in partitions they are needed. */
867 if (DECL_IN_CONSTANT_POOL (vnode->decl)
868 || DECL_COMDAT (vnode->decl))
870 file_data = vnode->lto_file_data;
871 slot = pointer_map_contains (pmap, file_data);
873 partition = (ltrans_partition) *slot;
876 partition = new_partition (file_data->file_name);
877 slot = pointer_map_insert (pmap, file_data);
882 add_varpool_node_to_partition (partition, vnode);
884 for (node = cgraph_nodes; node; node = node->next)
886 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
889 /* If the cgraph is empty, create one cgraph node set so that there is still
890 an output file for any variables that need to be exported in a DSO. */
892 new_partition ("empty");
894 pointer_map_destroy (pmap);
896 timevar_pop (TV_WHOPR_WPA);
898 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
902 /* Promote variable VNODE to be static. */
905 promote_var (struct varpool_node *vnode)
907 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
909 gcc_assert (flag_wpa);
910 TREE_PUBLIC (vnode->decl) = 1;
911 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
912 DECL_VISIBILITY_SPECIFIED (vnode->decl) = true;
913 if (cgraph_dump_file)
914 fprintf (cgraph_dump_file,
915 "Promoting var as hidden: %s\n", varpool_node_name (vnode));
919 /* Promote function NODE to be static. */
922 promote_fn (struct cgraph_node *node)
924 gcc_assert (flag_wpa);
925 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
927 TREE_PUBLIC (node->decl) = 1;
928 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
929 DECL_VISIBILITY_SPECIFIED (node->decl) = true;
932 struct cgraph_node *alias;
933 for (alias = node->same_body;
934 alias; alias = alias->next)
936 TREE_PUBLIC (alias->decl) = 1;
937 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
938 DECL_VISIBILITY_SPECIFIED (alias->decl) = true;
941 if (cgraph_dump_file)
942 fprintf (cgraph_dump_file,
943 "Promoting function as hidden: %s/%i\n",
944 cgraph_node_name (node), node->uid);
948 /* Find out all static decls that need to be promoted to global because
949 of cross file sharing. This function must be run in the WPA mode after
950 all inlinees are added. */
953 lto_promote_cross_file_statics (void)
955 struct varpool_node *vnode;
958 varpool_node_set vset;
959 cgraph_node_set_iterator csi;
960 varpool_node_set_iterator vsi;
961 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
962 struct pointer_set_t *inserted = pointer_set_create ();
964 gcc_assert (flag_wpa);
966 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
967 for (i = 0; i < n_sets; i++)
969 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
970 set = part->cgraph_set;
971 vset = part->varpool_set;
973 /* If node has either address taken (and we have no clue from where)
974 or it is called from other partition, it needs to be globalized. */
975 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
977 struct cgraph_node *node = csi_node (csi);
978 if (node->local.externally_visible)
980 if (node->global.inlined_to)
982 if ((!DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
983 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
984 || reachable_from_other_partition_p (node, set)))
987 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
989 vnode = vsi_node (vsi);
990 /* Constant pool references use internal labels and thus can not
991 be made global. It is sensible to keep those ltrans local to
992 allow better optimization. */
993 if (!DECL_IN_CONSTANT_POOL (vnode->decl) && !DECL_COMDAT (vnode->decl)
994 && !vnode->externally_visible && vnode->analyzed
995 && referenced_from_other_partition_p (&vnode->ref_list,
1000 /* We export initializers of read-only var into each partition
1001 referencing it. Folding might take declarations from the
1002 initializers and use it; so everything referenced from the
1003 initializers needs can be accessed from this partition after
1006 This means that we need to promote all variables and functions
1007 referenced from all initializers from readonly vars referenced
1008 from this partition that are not in this partition.
1009 This needs to be done recursively. */
1010 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1011 if (const_value_known_p (vnode->decl)
1012 && DECL_INITIAL (vnode->decl)
1013 && !varpool_node_in_set_p (vnode, vset)
1014 && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
1015 && !pointer_set_insert (inserted, vnode))
1016 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
1017 while (!VEC_empty (varpool_node_ptr, promoted_initializers))
1020 struct ipa_ref *ref;
1022 vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
1023 for (i = 0; ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref); i++)
1025 if (ref->refered_type == IPA_REF_CGRAPH)
1027 struct cgraph_node *n = ipa_ref_node (ref);
1028 gcc_assert (!n->global.inlined_to);
1029 if (!n->local.externally_visible
1030 && !cgraph_node_in_set_p (n, set))
1035 struct varpool_node *v = ipa_ref_varpool_node (ref);
1036 if (varpool_node_in_set_p (v, vset))
1038 /* Constant pool references use internal labels and thus can not
1039 be made global. It is sensible to keep those ltrans local to
1040 allow better optimization. */
1041 if (DECL_IN_CONSTANT_POOL (v->decl))
1043 if (!pointer_set_insert (inserted, vnode))
1044 VEC_safe_push (varpool_node_ptr, heap,
1045 promoted_initializers, v);
1047 else if (!DECL_IN_CONSTANT_POOL (v->decl)
1048 && !v->externally_visible && v->analyzed)
1051 && DECL_INITIAL (v->decl)
1052 && const_value_known_p (v->decl)
1053 && !pointer_set_insert (inserted, vnode))
1054 VEC_safe_push (varpool_node_ptr, heap,
1055 promoted_initializers, v);
1061 pointer_set_destroy (inserted);
1064 static lto_file *current_lto_file;
1066 /* Helper for qsort; compare partitions and return one with smaller size.
1067 We sort from greatest to smallest so parallel build doesn't stale on the
1068 longest compilation being executed too late. */
1071 cmp_partitions (const void *a, const void *b)
1073 const struct ltrans_partition_def *pa
1074 = *(struct ltrans_partition_def *const *)a;
1075 const struct ltrans_partition_def *pb
1076 = *(struct ltrans_partition_def *const *)b;
1077 return pb->insns - pa->insns;
1080 /* Write all output files in WPA mode and the file with the list of
1084 lto_wpa_write_files (void)
1088 cgraph_node_set set;
1089 varpool_node_set vset;
1090 ltrans_partition part;
1091 FILE *ltrans_output_list_stream;
1092 char *temp_filename;
1095 /* Open the LTRANS output list. */
1096 if (!ltrans_output_list)
1097 fatal_error ("no LTRANS output list filename provided");
1098 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
1099 if (ltrans_output_list_stream == NULL)
1100 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
1102 timevar_push (TV_WHOPR_WPA);
1104 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
1105 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
1106 part->cgraph_set->nodes);
1108 /* Find out statics that need to be promoted
1109 to globals with hidden visibility because they are accessed from multiple
1111 lto_promote_cross_file_statics ();
1113 timevar_pop (TV_WHOPR_WPA);
1115 timevar_push (TV_WHOPR_WPA_IO);
1117 /* Generate a prefix for the LTRANS unit files. */
1118 blen = strlen (ltrans_output_list);
1119 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
1120 strcpy (temp_filename, ltrans_output_list);
1121 if (blen > sizeof (".out")
1122 && strcmp (temp_filename + blen - sizeof (".out") + 1,
1124 temp_filename[blen - sizeof (".out") + 1] = '\0';
1125 blen = strlen (temp_filename);
1127 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
1128 qsort (VEC_address (ltrans_partition, ltrans_partitions), n_sets,
1129 sizeof (ltrans_partition), cmp_partitions);
1130 for (i = 0; i < n_sets; i++)
1133 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
1135 set = part->cgraph_set;
1136 vset = part->varpool_set;
1138 /* Write all the nodes in SET. */
1139 sprintf (temp_filename + blen, "%u.o", i);
1140 file = lto_obj_file_open (temp_filename, true);
1142 fatal_error ("lto_obj_file_open() failed");
1145 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
1146 if (cgraph_dump_file)
1148 fprintf (cgraph_dump_file, "Writting partition %s to file %s, %i insns\n",
1149 part->name, temp_filename, part->insns);
1150 fprintf (cgraph_dump_file, "cgraph nodes:");
1151 dump_cgraph_node_set (cgraph_dump_file, set);
1152 fprintf (cgraph_dump_file, "varpool nodes:");
1153 dump_varpool_node_set (cgraph_dump_file, vset);
1155 gcc_assert (cgraph_node_set_nonempty_p (set)
1156 || varpool_node_set_nonempty_p (vset) || !i);
1158 lto_set_current_out_file (file);
1160 ipa_write_optimization_summaries (set, vset);
1162 lto_set_current_out_file (NULL);
1163 lto_obj_file_close (file);
1165 len = strlen (temp_filename);
1166 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
1167 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1168 fatal_error ("writing to LTRANS output list %s: %m",
1169 ltrans_output_list);
1172 lto_stats.num_output_files += n_sets;
1174 /* Close the LTRANS output list. */
1175 if (fclose (ltrans_output_list_stream))
1176 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
1178 timevar_pop (TV_WHOPR_WPA_IO);
1183 struct pointer_set_t *seen;
1186 #define LTO_FIXUP_SUBTREE(t) \
1188 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1191 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
1195 (t) = gimple_register_type (t); \
1196 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1200 static tree lto_fixup_tree (tree *, int *, void *);
1202 /* Return true if T does not need to be fixed up recursively. */
1208 || CONSTANT_CLASS_P (t)
1209 || TREE_CODE (t) == IDENTIFIER_NODE);
1212 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
1215 lto_fixup_common (tree t, void *data)
1217 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
1218 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
1219 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
1220 First remove us from any pointer list we are on. */
1221 if (TREE_CODE (t) == POINTER_TYPE)
1223 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
1224 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
1227 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
1228 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
1229 tem = TYPE_NEXT_PTR_TO (tem);
1231 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
1233 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
1235 else if (TREE_CODE (t) == REFERENCE_TYPE)
1237 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
1238 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
1241 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
1242 while (tem && TYPE_NEXT_REF_TO (tem) != t)
1243 tem = TYPE_NEXT_REF_TO (tem);
1245 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
1247 TYPE_NEXT_REF_TO (t) = NULL_TREE;
1250 /* Fixup our type. */
1251 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1253 /* Second put us on the list of pointers of the new pointed-to type
1254 if we are a main variant. This is done in lto_fixup_type after
1255 fixing up our main variant. */
1257 /* This is not very efficient because we cannot do tail-recursion with
1258 a long chain of trees. */
1259 LTO_FIXUP_SUBTREE (TREE_CHAIN (t));
1262 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
1265 lto_fixup_decl_minimal (tree t, void *data)
1267 lto_fixup_common (t, data);
1268 LTO_FIXUP_SUBTREE (DECL_NAME (t));
1269 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t));
1272 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1275 lto_fixup_decl_common (tree t, void *data)
1277 lto_fixup_decl_minimal (t, data);
1278 LTO_FIXUP_SUBTREE (DECL_SIZE (t));
1279 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t));
1280 LTO_FIXUP_SUBTREE (DECL_INITIAL (t));
1281 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t));
1282 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t));
1285 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1288 lto_fixup_decl_with_vis (tree t, void *data)
1290 lto_fixup_decl_common (t, data);
1292 /* Accessor macro has side-effects, use field-name here. */
1293 LTO_FIXUP_SUBTREE (t->decl_with_vis.assembler_name);
1295 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t)));
1298 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1301 lto_fixup_decl_non_common (tree t, void *data)
1303 lto_fixup_decl_with_vis (t, data);
1304 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t));
1305 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t));
1306 LTO_FIXUP_SUBTREE (DECL_VINDEX (t));
1308 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1309 gcc_assert (no_fixup_p (t->decl_non_common.saved_tree));
1312 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1315 lto_fixup_function (tree t, void *data)
1317 lto_fixup_decl_non_common (t, data);
1318 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t));
1321 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1324 lto_fixup_field_decl (tree t, void *data)
1326 lto_fixup_decl_common (t, data);
1327 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t));
1328 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t));
1329 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t));
1330 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t)));
1331 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t));
1334 /* Fix up fields of a type T. DATA points to fix-up states. */
1337 lto_fixup_type (tree t, void *data)
1341 lto_fixup_common (t, data);
1342 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t));
1343 LTO_FIXUP_SUBTREE (TYPE_SIZE (t));
1344 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t));
1345 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t));
1346 LTO_FIXUP_SUBTREE (TYPE_NAME (t));
1348 /* Accessors are for derived node types only. */
1349 if (!POINTER_TYPE_P (t))
1350 LTO_FIXUP_SUBTREE (t->type.minval);
1351 LTO_FIXUP_SUBTREE (t->type.maxval);
1353 /* Accessor is for derived node types only. */
1354 LTO_FIXUP_SUBTREE (t->type.binfo);
1356 if (TYPE_CONTEXT (t))
1358 if (TYPE_P (TYPE_CONTEXT (t)))
1359 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1361 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1364 /* TYPE_CANONICAL does not need to be fixed up, instead it should
1365 always point to ourselves at this time as we never fixup
1366 non-canonical ones. */
1367 gcc_assert (TYPE_CANONICAL (t) == t);
1369 /* The following re-creates proper variant lists while fixing up
1370 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1371 variant list state before fixup is broken. */
1373 /* Remove us from our main variant list if we are not the variant leader. */
1374 if (TYPE_MAIN_VARIANT (t) != t)
1376 tem = TYPE_MAIN_VARIANT (t);
1377 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1378 tem = TYPE_NEXT_VARIANT (tem);
1380 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1381 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1384 /* Query our new main variant. */
1385 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1387 /* If we were the variant leader and we get replaced ourselves drop
1388 all variants from our list. */
1389 if (TYPE_MAIN_VARIANT (t) == t
1395 tree tem2 = TYPE_NEXT_VARIANT (tem);
1396 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1401 /* If we are not our own variant leader link us into our new leaders
1405 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1406 TYPE_NEXT_VARIANT (mv) = t;
1409 /* Finally adjust our main variant and fix it up. */
1410 TYPE_MAIN_VARIANT (t) = mv;
1411 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1413 /* As the second step of reconstructing the pointer chains put us
1414 on the list of pointers of the new pointed-to type
1415 if we are a main variant. See lto_fixup_common for the first step. */
1416 if (TREE_CODE (t) == POINTER_TYPE
1417 && TYPE_MAIN_VARIANT (t) == t)
1419 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1420 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1422 else if (TREE_CODE (t) == REFERENCE_TYPE
1423 && TYPE_MAIN_VARIANT (t) == t)
1425 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1426 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1430 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1433 lto_fixup_binfo (tree t, void *data)
1435 unsigned HOST_WIDE_INT i, n;
1436 tree base, saved_base;
1438 lto_fixup_common (t, data);
1439 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1440 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1441 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1442 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1443 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1444 for (i = 0; i < n; i++)
1446 saved_base = base = BINFO_BASE_ACCESS (t, i);
1447 LTO_FIXUP_SUBTREE (base);
1448 if (base != saved_base)
1449 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1451 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1452 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1453 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1454 n = BINFO_N_BASE_BINFOS (t);
1455 for (i = 0; i < n; i++)
1457 saved_base = base = BINFO_BASE_BINFO (t, i);
1458 LTO_FIXUP_SUBTREE (base);
1459 if (base != saved_base)
1460 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1464 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1467 lto_fixup_constructor (tree t, void *data)
1469 unsigned HOST_WIDE_INT idx;
1470 constructor_elt *ce;
1472 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1475 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1478 LTO_FIXUP_SUBTREE (ce->index);
1479 LTO_FIXUP_SUBTREE (ce->value);
1483 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1484 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1485 DATA is a pointer set to record visited nodes. */
1488 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1491 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1496 if (!t || pointer_set_contains (fixup_data->seen, t))
1499 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1501 prevailing = lto_symtab_prevailing_decl (t);
1503 if (t != prevailing)
1505 /* Also replace t with prevailing defintion. We don't want to
1506 insert the other defintion in the seen set as we want to
1507 replace all instances of it. */
1512 else if (TYPE_P (t))
1514 /* Replace t with the prevailing type. We don't want to insert the
1515 other type in the seen set as we want to replace all instances of it. */
1516 t = gimple_register_type (t);
1520 if (pointer_set_insert (fixup_data->seen, t))
1523 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1524 Hence we do special processing here for those kind of nodes. */
1525 switch (TREE_CODE (t))
1528 lto_fixup_field_decl (t, data);
1536 lto_fixup_decl_common (t, data);
1540 lto_fixup_decl_with_vis (t, data);
1544 lto_fixup_decl_non_common (t, data);
1548 lto_fixup_function (t, data);
1552 lto_fixup_binfo (t, data);
1557 lto_fixup_type (t, data);
1558 else if (TREE_CODE (t) == CONSTRUCTOR)
1559 lto_fixup_constructor (t, data);
1560 else if (CONSTANT_CLASS_P (t))
1561 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1562 else if (EXPR_P (t))
1564 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1565 lto_fixup_common (t, data);
1566 LTO_FIXUP_SUBTREE (t->exp.block);
1571 /* Let walk_tree handle sub-trees. */
1579 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1580 replaces var and function decls with the corresponding prevailing def and
1581 records the old decl in the free-list in DATA. We also record visted nodes
1582 in the seen-set in DATA to avoid multiple visit for nodes that need not
1586 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1589 struct lto_tree_ref_table *table;
1591 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1592 we still need to walk from all DECLs to find the reachable
1593 FUNCTION_DECLs and VAR_DECLs. */
1594 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1596 table = &state->streams[si];
1597 for (i = 0; i < table->size; i++)
1598 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1602 /* A callback of htab_traverse. Just extract a state from SLOT and the
1603 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1606 lto_fixup_state_aux (void **slot, void *aux)
1608 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1609 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1613 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1617 lto_fixup_decls (struct lto_file_decl_data **files)
1621 struct pointer_set_t *seen = pointer_set_create ();
1622 lto_fixup_data_t data;
1625 for (i = 0; files[i]; i++)
1627 struct lto_file_decl_data *file = files[i];
1628 struct lto_in_decl_state *state = file->global_decl_state;
1629 lto_fixup_state (state, &data);
1631 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1634 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
1636 tree saved_decl = decl;
1637 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1638 if (decl != saved_decl)
1639 VEC_replace (tree, lto_global_var_decls, i, decl);
1642 pointer_set_destroy (seen);
1645 /* Read the options saved from each file in the command line. Called
1646 from lang_hooks.post_options which is called by process_options
1647 right before all the options are used to initialize the compiler.
1648 This assumes that decode_options has already run, so the
1649 num_in_fnames and in_fnames are properly set.
1651 Note that this assumes that all the files had been compiled with
1652 the same options, which is not a good assumption. In general,
1653 options ought to be read from all the files in the set and merged.
1654 However, it is still unclear what the merge rules should be. */
1657 lto_read_all_file_options (void)
1661 /* Clear any file options currently saved. */
1662 lto_clear_file_options ();
1664 /* Set the hooks to read ELF sections. */
1665 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1667 fprintf (stderr, "Reading command line options:");
1669 for (i = 0; i < num_in_fnames; i++)
1671 struct lto_file_decl_data *file_data;
1672 lto_file *file = lto_obj_file_open (in_fnames[i], false);
1677 fprintf (stderr, " %s", in_fnames[i]);
1681 file_data = XCNEW (struct lto_file_decl_data);
1682 file_data->file_name = file->filename;
1683 file_data->section_hash_table = lto_obj_build_section_table (file);
1685 lto_read_file_options (file_data);
1687 lto_obj_file_close (file);
1688 htab_delete (file_data->section_hash_table);
1693 fprintf (stderr, "\n");
1695 /* Apply globally the options read from all the files. */
1696 lto_reissue_options ();
1699 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1701 /* Turn file datas for sub files into a single array, so that they look
1702 like separate files for further passes. */
1705 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
1707 struct lto_file_decl_data *n, *next;
1710 lto_stats.num_input_files = count;
1712 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
1713 /* Set the hooks so that all of the ipa passes can read in their data. */
1714 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1715 for (i = 0, k = 0; i < last_file_ix; i++)
1717 for (n = orig[i]; n != NULL; n = next)
1719 all_file_decl_data[k++] = n;
1724 all_file_decl_data[k] = NULL;
1725 gcc_assert (k == count);
1728 /* Input file data before flattening (i.e. splitting them to subfiles to support
1729 incremental linking. */
1730 static int real_file_count;
1731 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
1733 /* Read all the symbols from the input files FNAMES. NFILES is the
1734 number of files requested in the command line. Instantiate a
1735 global call graph by aggregating all the sub-graphs found in each
1739 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1741 unsigned int i, last_file_ix;
1743 struct cgraph_node *node;
1745 struct lto_file_decl_data **decl_data;
1749 timevar_push (TV_IPA_LTO_DECL_IN);
1752 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
1753 real_file_count = nfiles;
1755 /* Read the resolution file. */
1757 if (resolution_file_name)
1760 unsigned num_objects;
1762 resolution = fopen (resolution_file_name, "r");
1763 if (resolution == NULL)
1764 fatal_error ("could not open symbol resolution file: %m");
1766 t = fscanf (resolution, "%u", &num_objects);
1767 gcc_assert (t == 1);
1769 /* True, since the plugin splits the archives. */
1770 gcc_assert (num_objects == nfiles);
1774 fprintf (stderr, "Reading object files:");
1776 /* Read all of the object files specified on the command line. */
1777 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1779 struct lto_file_decl_data *file_data = NULL;
1782 fprintf (stderr, " %s", fnames[i]);
1786 current_lto_file = lto_obj_file_open (fnames[i], false);
1787 if (!current_lto_file)
1790 file_data = lto_file_read (current_lto_file, resolution, &count);
1794 decl_data[last_file_ix++] = file_data;
1796 lto_obj_file_close (current_lto_file);
1797 current_lto_file = NULL;
1801 lto_flatten_files (decl_data, count, last_file_ix);
1802 lto_stats.num_input_files = count;
1803 ggc_free(decl_data);
1804 real_file_decl_data = NULL;
1806 if (resolution_file_name)
1807 fclose (resolution);
1809 /* Set the hooks so that all of the ipa passes can read in their data. */
1810 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1812 timevar_pop (TV_IPA_LTO_DECL_IN);
1815 fprintf (stderr, "\nReading the callgraph\n");
1817 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1818 /* Read the callgraph. */
1820 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1823 fprintf (stderr, "Merging declarations\n");
1825 timevar_push (TV_IPA_LTO_DECL_MERGE);
1826 /* Merge global decls. */
1827 lto_symtab_merge_decls ();
1829 /* Fixup all decls and types and free the type hash tables. */
1830 lto_fixup_decls (all_file_decl_data);
1831 free_gimple_type_tables ();
1834 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1835 /* Each pass will set the appropriate timer. */
1838 fprintf (stderr, "Reading summaries\n");
1840 /* Read the IPA summary data. */
1842 ipa_read_optimization_summaries ();
1844 ipa_read_summaries ();
1846 /* Finally merge the cgraph according to the decl merging decisions. */
1847 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1848 if (cgraph_dump_file)
1850 fprintf (cgraph_dump_file, "Before merging:\n");
1851 dump_cgraph (cgraph_dump_file);
1852 dump_varpool (cgraph_dump_file);
1854 lto_symtab_merge_cgraph_nodes ();
1858 for (node = cgraph_nodes; node; node = node->next)
1860 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1861 summaries computed and needs to apply changes. At the moment WHOPR only
1862 supports inlining, so we can push it here by hand. In future we need to stream
1863 this field into ltrans compilation. */
1865 VEC_safe_push (ipa_opt_pass, heap,
1866 node->ipa_transforms_to_apply,
1867 (ipa_opt_pass)&pass_ipa_inline);
1871 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1873 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1875 /* FIXME lto. This loop needs to be changed to use the pass manager to
1876 call the ipa passes directly. */
1878 for (i = 0; i < last_file_ix; i++)
1880 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1881 lto_materialize_constructors_and_inits (file_data);
1884 /* Indicate that the cgraph is built and ready. */
1885 cgraph_function_flags_ready = true;
1887 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1888 ggc_free (all_file_decl_data);
1889 all_file_decl_data = NULL;
1893 /* Materialize all the bodies for all the nodes in the callgraph. */
1896 materialize_cgraph (void)
1899 struct cgraph_node *node;
1901 timevar_id_t lto_timer;
1905 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1908 /* Now that we have input the cgraph, we need to clear all of the aux
1909 nodes and read the functions if we are not running in WPA mode. */
1910 timevar_push (TV_IPA_LTO_GIMPLE_IN);
1912 for (node = cgraph_nodes; node; node = node->next)
1914 if (node->local.lto_file_data)
1916 lto_materialize_function (node);
1917 lto_stats.num_input_cgraph_nodes++;
1921 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
1923 /* Start the appropriate timer depending on the mode that we are
1925 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1926 : (flag_ltrans) ? TV_WHOPR_LTRANS
1928 timevar_push (lto_timer);
1930 current_function_decl = NULL;
1933 /* Inform the middle end about the global variables we have seen. */
1934 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
1935 rest_of_decl_compilation (decl, 1, 0);
1938 fprintf (stderr, "\n");
1940 timevar_pop (lto_timer);
1944 /* Perform whole program analysis (WPA) on the callgraph and write out the
1945 optimization plan. */
1948 do_whole_program_analysis (void)
1950 /* Note that since we are in WPA mode, materialize_cgraph will not
1951 actually read in all the function bodies. It only materializes
1952 the decls and cgraph nodes so that analysis can be performed. */
1953 materialize_cgraph ();
1955 /* Reading in the cgraph uses different timers, start timing WPA now. */
1956 timevar_push (TV_WHOPR_WPA);
1958 if (pre_ipa_mem_report)
1960 fprintf (stderr, "Memory consumption before IPA\n");
1961 dump_memory_report (false);
1964 if (cgraph_dump_file)
1966 dump_cgraph (cgraph_dump_file);
1967 dump_varpool (cgraph_dump_file);
1970 cgraph_function_flags_ready = true;
1971 bitmap_obstack_initialize (NULL);
1972 ipa_register_cgraph_hooks ();
1973 cgraph_state = CGRAPH_STATE_IPA_SSA;
1975 execute_ipa_pass_list (all_regular_ipa_passes);
1977 if (cgraph_dump_file)
1979 fprintf (cgraph_dump_file, "Optimized ");
1980 dump_cgraph (cgraph_dump_file);
1981 dump_varpool (cgraph_dump_file);
1984 bitmap_obstack_release (NULL);
1986 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1987 timevar_pop (TV_WHOPR_WPA);
1993 fprintf (stderr, "\nStreaming out");
1996 lto_wpa_write_files ();
1999 fprintf (stderr, "\n");
2001 if (post_ipa_mem_report)
2003 fprintf (stderr, "Memory consumption after IPA\n");
2004 dump_memory_report (false);
2007 /* Show the LTO report before launching LTRANS. */
2008 if (flag_lto_report)
2009 print_lto_report ();
2013 static GTY(()) tree lto_eh_personality_decl;
2015 /* Return the LTO personality function decl. */
2018 lto_eh_personality (void)
2020 if (!lto_eh_personality_decl)
2022 /* Use the first personality DECL for our personality if we don't
2023 support multiple ones. This ensures that we don't artificially
2024 create the need for them in a single-language program. */
2025 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
2026 lto_eh_personality_decl = first_personality_decl;
2028 lto_eh_personality_decl = lhd_gcc_personality ();
2031 return lto_eh_personality_decl;
2035 /* Main entry point for the GIMPLE front end. This front end has
2036 three main personalities:
2038 - LTO (-flto). All the object files on the command line are
2039 loaded in memory and processed as a single translation unit.
2040 This is the traditional link-time optimization behavior.
2042 - WPA (-fwpa). Only the callgraph and summary information for
2043 files in the command file are loaded. A single callgraph
2044 (without function bodies) is instantiated for the whole set of
2045 files. IPA passes are only allowed to analyze the call graph
2046 and make transformation decisions. The callgraph is
2047 partitioned, each partition is written to a new object file
2048 together with the transformation decisions.
2050 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2051 summary files from running again. Since WPA computed summary
2052 information and decided what transformations to apply, LTRANS
2053 simply applies them. */
2056 lto_main (int debug_p ATTRIBUTE_UNUSED)
2060 /* Read all the symbols and call graph from all the files in the
2062 read_cgraph_and_symbols (num_in_fnames, in_fnames);
2066 /* If WPA is enabled analyze the whole call graph and create an
2067 optimization plan. Otherwise, read in all the function
2068 bodies and continue with optimization. */
2070 do_whole_program_analysis ();
2073 materialize_cgraph ();
2075 /* Let the middle end know that we have read and merged all of
2079 /* FIXME lto, if the processes spawned by WPA fail, we miss
2080 the chance to print WPA's report, so WPA will call
2081 print_lto_report before launching LTRANS. If LTRANS was
2082 launched directly by the driver we would not need to do
2084 if (flag_lto_report)
2085 print_lto_report ();
2090 #include "gt-lto-lto.h"