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.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"
44 #include "lto-streamer.h"
46 /* This needs to be included after config.h. Otherwise, _GNU_SOURCE will not
47 be defined in time to set __USE_GNU in the system headers, and strsignal
48 will not be declared. */
53 /* Handle opening elf files on hosts, such as Windows, that may use
54 text file handling that will break binary access. */
62 DEF_VEC_ALLOC_P(bitmap,heap);
64 /* Read the constructors and inits. */
67 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
70 const char *data = lto_get_section_data (file_data,
71 LTO_section_static_initializer,
73 lto_input_constructors_and_inits (file_data, data);
74 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
78 /* Read the function body for the function associated with NODE if possible. */
81 lto_materialize_function (struct cgraph_node *node)
84 struct lto_file_decl_data *file_data;
85 const char *data, *name;
89 /* Ignore clone nodes. Read the body only from the original one.
90 We may find clone nodes during LTRANS after WPA has made inlining
96 file_data = node->local.lto_file_data;
97 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
99 /* We may have renamed the declaration, e.g., a static function. */
100 name = lto_get_decl_name_mapping (file_data, name);
102 data = lto_get_section_data (file_data, LTO_section_function_body,
108 gcc_assert (!DECL_IS_BUILTIN (decl));
110 /* This function has a definition. */
111 TREE_STATIC (decl) = 1;
113 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
114 allocate_struct_function (decl, false);
116 /* Load the function body only if not operating in WPA mode. In
117 WPA mode, the body of the function is not needed. */
120 lto_input_function_body (file_data, decl, data);
121 lto_stats.num_function_bodies++;
124 fn = DECL_STRUCT_FUNCTION (decl);
125 lto_free_section_data (file_data, LTO_section_function_body, name,
128 /* Look for initializers of constant variables and private
130 for (step = fn->local_decls; step; step = TREE_CHAIN (step))
132 tree decl = TREE_VALUE (step);
133 if (TREE_CODE (decl) == VAR_DECL
134 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
135 && flag_unit_at_a_time)
136 varpool_finalize_decl (decl);
140 DECL_EXTERNAL (decl) = 1;
142 /* Let the middle end know about the function. */
143 rest_of_decl_compilation (decl, 1, 0);
144 if (cgraph_node (decl)->needed)
145 cgraph_mark_reachable_node (cgraph_node (decl));
149 /* Decode the content of memory pointed to by DATA in the the
150 in decl state object STATE. DATA_IN points to a data_in structure for
151 decoding. Return the address after the decoded object in the input. */
153 static const uint32_t *
154 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
155 struct lto_in_decl_state *state)
162 decl = lto_streamer_cache_get (data_in->reader_cache, (int) ix);
163 if (TREE_CODE (decl) != FUNCTION_DECL)
165 gcc_assert (decl == void_type_node);
168 state->fn_decl = decl;
170 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
172 uint32_t size = *data++;
173 tree *decls = (tree *) xcalloc (size, sizeof (tree));
175 for (j = 0; j < size; j++)
177 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
179 /* Register every type in the global type table. If the
180 type existed already, use the existing type. */
181 if (TYPE_P (decls[j]))
182 decls[j] = gimple_register_type (decls[j]);
185 state->streams[i].size = size;
186 state->streams[i].trees = decls;
194 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
195 RESOLUTIONS is the set of symbols picked by the linker (read from the
196 resolution file when the linker plugin is being used). */
199 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
200 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
202 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
203 const int32_t decl_offset = sizeof (struct lto_decl_header);
204 const int32_t main_offset = decl_offset + header->decl_state_size;
205 const int32_t string_offset = main_offset + header->main_size;
206 struct lto_input_block ib_main;
207 struct data_in *data_in;
209 const uint32_t *data_ptr, *data_end;
210 uint32_t num_decl_states;
212 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
215 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
216 header->string_size, resolutions);
218 /* Read the global declarations and types. */
219 while (ib_main.p < ib_main.len)
221 tree t = lto_input_tree (&ib_main, data_in);
222 gcc_assert (t && ib_main.p <= ib_main.len);
225 /* Read in lto_in_decl_state objects. */
226 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
228 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
229 num_decl_states = *data_ptr++;
231 gcc_assert (num_decl_states > 0);
232 decl_data->global_decl_state = lto_new_in_decl_state ();
233 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
234 decl_data->global_decl_state);
236 /* Read in per-function decl states and enter them in hash table. */
237 decl_data->function_decl_states =
238 htab_create (37, lto_hash_in_decl_state, lto_eq_in_decl_state, free);
240 for (i = 1; i < num_decl_states; i++)
242 struct lto_in_decl_state *state = lto_new_in_decl_state ();
245 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
246 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
247 gcc_assert (*slot == NULL);
251 if (data_ptr != data_end)
252 internal_error ("bytecode stream: garbage at the end of symbols section");
254 /* Set the current decl state to be the global state. */
255 decl_data->current_decl_state = decl_data->global_decl_state;
257 lto_data_in_delete (data_in);
260 /* strtoll is not portable. */
262 lto_parse_hex (const char *p) {
264 for (; *p != '\0'; ++p)
269 if (c >= '0' && c <= '9')
271 else if (c >= 'a' && c <= 'f')
273 else if (c >= 'A' && c <= 'F')
276 internal_error ("could not parse hex number");
282 /* Read resolution for file named FILE_NAME. The resolution is read from
283 RESOLUTION. An array with the symbol resolution is returned. The array
284 size is written to SIZE. */
286 static VEC(ld_plugin_symbol_resolution_t,heap) *
287 lto_resolution_read (FILE *resolution, lto_file *file)
289 /* We require that objects in the resolution file are in the same
290 order as the lto1 command line. */
291 unsigned int name_len;
293 unsigned int num_symbols;
295 VEC(ld_plugin_symbol_resolution_t,heap) *ret = NULL;
296 unsigned max_index = 0;
301 name_len = strlen (file->filename);
302 obj_name = XNEWVEC (char, name_len + 1);
303 fscanf (resolution, " "); /* Read white space. */
305 fread (obj_name, sizeof (char), name_len, resolution);
306 obj_name[name_len] = '\0';
307 if (strcmp (obj_name, file->filename) != 0)
308 internal_error ("unexpected file name %s in linker resolution file. "
309 "Expected %s", obj_name, file->filename);
310 if (file->offset != 0)
315 t = fscanf (resolution, "@0x%16s", offset_p);
317 internal_error ("could not parse file offset");
318 offset = lto_parse_hex (offset_p);
319 if (offset != file->offset)
320 internal_error ("unexpected offset");
325 fscanf (resolution, "%u", &num_symbols);
327 for (i = 0; i < num_symbols; i++)
332 enum ld_plugin_symbol_resolution r;
334 unsigned int lto_resolution_str_len =
335 sizeof (lto_resolution_str) / sizeof (char *);
337 t = fscanf (resolution, "%u %26s %*[^\n]\n", &index, r_str);
339 internal_error ("Invalid line in the resolution file.");
340 if (index > max_index)
343 for (j = 0; j < lto_resolution_str_len; j++)
345 if (strcmp (lto_resolution_str[j], r_str) == 0)
347 r = (enum ld_plugin_symbol_resolution) j;
351 if (j == lto_resolution_str_len)
352 internal_error ("Invalid resolution in the resolution file.");
354 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap, ret,
356 VEC_replace (ld_plugin_symbol_resolution_t, ret, index, r);
362 /* Generate a TREE representation for all types and external decls
365 Read all of the globals out of the file. Then read the cgraph
366 and process the .o index into the cgraph nodes so that it can open
367 the .o file to load the functions and ipa information. */
369 static struct lto_file_decl_data *
370 lto_file_read (lto_file *file, FILE *resolution_file)
372 struct lto_file_decl_data *file_data;
375 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions;
377 resolutions = lto_resolution_read (resolution_file, file);
379 file_data = XCNEW (struct lto_file_decl_data);
380 file_data->file_name = file->filename;
381 file_data->section_hash_table = lto_elf_build_section_table (file);
382 file_data->renaming_hash_table = lto_create_renaming_table ();
384 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
385 lto_read_decls (file_data, data, resolutions);
386 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
391 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
392 #define LTO_MMAP_IO 1
396 /* Page size of machine is used for mmap and munmap calls. */
397 static size_t page_mask;
400 /* Get the section data of length LEN from FILENAME starting at
401 OFFSET. The data segment must be freed by the caller when the
402 caller is finished. Returns NULL if all was not well. */
405 lto_read_section_data (struct lto_file_decl_data *file_data,
406 intptr_t offset, size_t len)
410 static char *fd_name;
412 intptr_t computed_len;
413 intptr_t computed_offset;
417 /* Keep a single-entry file-descriptor cache. The last file we
418 touched will get closed at exit.
419 ??? Eventually we want to add a more sophisticated larger cache
420 or rather fix function body streaming to not stream them in
421 practically random order. */
423 && strcmp (fd_name, file_data->file_name) != 0)
431 fd_name = xstrdup (file_data->file_name);
432 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
440 size_t page_size = sysconf (_SC_PAGE_SIZE);
441 page_mask = ~(page_size - 1);
444 computed_offset = offset & page_mask;
445 diff = offset - computed_offset;
446 computed_len = len + diff;
448 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
449 fd, computed_offset);
450 if (result == MAP_FAILED)
453 return result + diff;
455 result = (char *) xmalloc (len);
456 if (lseek (fd, offset, SEEK_SET) != offset
457 || read (fd, result, len) != (ssize_t) len)
468 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
469 NAME will be NULL unless the section type is for a function
473 get_section_data (struct lto_file_decl_data *file_data,
474 enum lto_section_type section_type,
478 htab_t section_hash_table = file_data->section_hash_table;
479 struct lto_section_slot *f_slot;
480 struct lto_section_slot s_slot;
481 const char *section_name = lto_get_section_name (section_type, name);
485 s_slot.name = section_name;
486 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
489 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
493 free (CONST_CAST (char *, section_name));
498 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
499 starts at OFFSET and has LEN bytes. */
502 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
503 enum lto_section_type section_type ATTRIBUTE_UNUSED,
504 const char *name ATTRIBUTE_UNUSED,
505 const char *offset, size_t len ATTRIBUTE_UNUSED)
508 intptr_t computed_len;
509 intptr_t computed_offset;
514 computed_offset = ((intptr_t) offset) & page_mask;
515 diff = (intptr_t) offset - computed_offset;
516 computed_len = len + diff;
518 munmap ((caddr_t) computed_offset, computed_len);
520 free (CONST_CAST(char *, offset));
524 /* Vector of all cgraph node sets. */
525 static GTY (()) VEC(cgraph_node_set, gc) *lto_cgraph_node_sets;
528 /* Group cgrah nodes by input files. This is used mainly for testing
532 lto_1_to_1_map (void)
534 struct cgraph_node *node;
535 struct lto_file_decl_data *file_data;
536 struct pointer_map_t *pmap;
540 timevar_push (TV_WHOPR_WPA);
542 lto_cgraph_node_sets = VEC_alloc (cgraph_node_set, gc, 1);
544 /* If the cgraph is empty, create one cgraph node set so that there is still
545 an output file for any variables that need to be exported in a DSO. */
548 set = cgraph_node_set_new ();
549 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
553 pmap = pointer_map_create ();
555 for (node = cgraph_nodes; node; node = node->next)
557 /* We only need to partition the nodes that we read from the
558 gimple bytecode files. */
559 file_data = node->local.lto_file_data;
560 if (file_data == NULL)
563 slot = pointer_map_contains (pmap, file_data);
565 set = (cgraph_node_set) *slot;
568 set = cgraph_node_set_new ();
569 slot = pointer_map_insert (pmap, file_data);
571 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
574 cgraph_node_set_add (set, node);
577 pointer_map_destroy (pmap);
580 timevar_pop (TV_WHOPR_WPA);
582 lto_stats.num_cgraph_partitions += VEC_length (cgraph_node_set,
583 lto_cgraph_node_sets);
587 /* Add inlined clone NODE and its master clone to SET, if NODE itself has
588 inlined callees, recursively add the callees. */
591 lto_add_inline_clones (cgraph_node_set set, struct cgraph_node *node,
592 bitmap original_decls, bitmap inlined_decls)
594 struct cgraph_node *callee;
595 struct cgraph_edge *edge;
597 cgraph_node_set_add (set, node);
599 if (!bitmap_bit_p (original_decls, DECL_UID (node->decl)))
600 bitmap_set_bit (inlined_decls, DECL_UID (node->decl));
602 /* Check to see if NODE has any inlined callee. */
603 for (edge = node->callees; edge != NULL; edge = edge->next_callee)
605 callee = edge->callee;
606 if (callee->global.inlined_to != NULL)
607 lto_add_inline_clones (set, callee, original_decls, inlined_decls);
611 /* Compute the transitive closure of inlining of SET based on the
612 information in the callgraph. Returns a bitmap of decls that have
613 been inlined into SET indexed by UID. */
616 lto_add_all_inlinees (cgraph_node_set set)
618 cgraph_node_set_iterator csi;
619 struct cgraph_node *node;
620 bitmap original_nodes = lto_bitmap_alloc ();
621 bitmap original_decls = lto_bitmap_alloc ();
622 bitmap inlined_decls = lto_bitmap_alloc ();
625 /* We are going to iterate SET while adding to it, mark all original
626 nodes so that we only add node inlined to original nodes. */
627 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
629 bitmap_set_bit (original_nodes, csi_node (csi)->uid);
630 bitmap_set_bit (original_decls, DECL_UID (csi_node (csi)->decl));
633 /* Some of the original nodes might not be needed anymore.
638 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
640 struct cgraph_node *inlined_to;
641 node = csi_node (csi);
643 /* NODE was not inlined. We still need it. */
644 if (!node->global.inlined_to)
647 inlined_to = node->global.inlined_to;
649 /* NODE should have only one caller. */
650 gcc_assert (!node->callers->next_caller);
652 if (!bitmap_bit_p (original_nodes, inlined_to->uid))
654 bitmap_clear_bit (original_nodes, node->uid);
655 cgraph_node_set_remove (set, node);
662 /* Transitively add to SET all the inline clones for every node that
664 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
666 node = csi_node (csi);
667 if (bitmap_bit_p (original_nodes, node->uid))
668 lto_add_inline_clones (set, node, original_decls, inlined_decls);
671 lto_bitmap_free (original_nodes);
672 lto_bitmap_free (original_decls);
674 return inlined_decls;
677 /* Owing to inlining, we may need to promote a file-scope variable
678 to a global variable. Consider this case:
691 extern void foo (void);
699 If WPA inlines FOO inside BAR, then the static variable VAR needs to
700 be promoted to global because BAR and VAR may be in different LTRANS
703 /* This struct keeps track of states used in globalization. */
707 /* Current cgraph node set. */
710 /* Function DECLs of cgraph nodes seen. */
711 bitmap seen_node_decls;
713 /* Use in walk_tree to avoid multiple visits of a node. */
714 struct pointer_set_t *visited;
716 /* static vars in this set. */
717 bitmap static_vars_in_set;
719 /* static vars in all previous set. */
720 bitmap all_static_vars;
722 /* all vars in all previous set. */
724 } globalize_context_t;
726 /* Callback for walk_tree. Examine the tree pointer to by TP and see if
727 if its a file-scope static variable of function that need to be turned
731 globalize_cross_file_statics (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
734 globalize_context_t *context = (globalize_context_t *) data;
740 /* The logic for globalization of VAR_DECLs and FUNCTION_DECLs are
741 different. For functions, we can simply look at the cgraph node sets
742 to tell if there are references to static functions outside the set.
743 The cgraph node sets do not keep track of vars, we need to traverse
744 the trees to determine what vars need to be globalized. */
745 if (TREE_CODE (t) == VAR_DECL)
747 if (!TREE_PUBLIC (t))
749 /* This file-scope static variable is reachable from more
750 that one set. Make it global but with hidden visibility
751 so that we do not export it in dynamic linking. */
752 if (bitmap_bit_p (context->all_static_vars, DECL_UID (t)))
755 DECL_VISIBILITY (t) = VISIBILITY_HIDDEN;
757 bitmap_set_bit (context->static_vars_in_set, DECL_UID (t));
759 bitmap_set_bit (context->all_vars, DECL_UID (t));
760 walk_tree (&DECL_INITIAL (t), globalize_cross_file_statics, context,
763 else if (TREE_CODE (t) == FUNCTION_DECL && !TREE_PUBLIC (t))
765 if (!cgraph_node_in_set_p (cgraph_node (t), context->set))
767 /* This file-scope static function is reachable from a set
768 which does not contain the function DECL. Make it global
769 but with hidden visibility. */
771 DECL_VISIBILITY (t) = VISIBILITY_HIDDEN;
778 /* Helper of lto_scan_statics_in_cgraph_node below. Scan TABLE for
779 static decls that may be used in more than one LTRANS file.
780 CONTEXT is a globalize_context_t for storing scanning states. */
783 lto_scan_statics_in_ref_table (struct lto_tree_ref_table *table,
784 globalize_context_t *context)
788 for (i = 0; i < table->size; i++)
789 walk_tree (&table->trees[i], globalize_cross_file_statics, context,
793 /* Promote file-scope decl reachable from NODE if necessary to global.
794 CONTEXT is a globalize_context_t storing scanning states. */
797 lto_scan_statics_in_cgraph_node (struct cgraph_node *node,
798 globalize_context_t *context)
800 struct lto_in_decl_state *state;
802 /* Do nothing if NODE has no function body. */
806 /* Return if the DECL of nodes has been visited before. */
807 if (bitmap_bit_p (context->seen_node_decls, DECL_UID (node->decl)))
810 bitmap_set_bit (context->seen_node_decls, DECL_UID (node->decl));
812 state = lto_get_function_in_decl_state (node->local.lto_file_data,
816 lto_scan_statics_in_ref_table (&state->streams[LTO_DECL_STREAM_VAR_DECL],
818 lto_scan_statics_in_ref_table (&state->streams[LTO_DECL_STREAM_FN_DECL],
822 /* Scan all global variables that we have not yet seen so far. CONTEXT
823 is a globalize_context_t storing scanning states. */
826 lto_scan_statics_in_remaining_global_vars (globalize_context_t *context)
828 tree var, var_context;
829 struct varpool_node *vnode;
831 FOR_EACH_STATIC_VARIABLE (vnode)
834 var_context = DECL_CONTEXT (var);
835 if (TREE_STATIC (var)
837 && (!var_context || TREE_CODE (var_context) != FUNCTION_DECL)
838 && !bitmap_bit_p (context->all_vars, DECL_UID (var)))
839 walk_tree (&var, globalize_cross_file_statics, context,
844 /* Find out all static decls that need to be promoted to global because
845 of cross file sharing. This function must be run in the WPA mode after
846 all inlinees are added. */
849 lto_promote_cross_file_statics (void)
853 cgraph_node_set_iterator csi;
854 globalize_context_t context;
856 memset (&context, 0, sizeof (context));
857 context.all_vars = lto_bitmap_alloc ();
858 context.all_static_vars = lto_bitmap_alloc ();
860 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
861 for (i = 0; i < n_sets; i++)
863 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
865 context.visited = pointer_set_create ();
866 context.static_vars_in_set = lto_bitmap_alloc ();
867 context.seen_node_decls = lto_bitmap_alloc ();
869 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
870 lto_scan_statics_in_cgraph_node (csi_node (csi), &context);
873 lto_scan_statics_in_remaining_global_vars (&context);
875 bitmap_ior_into (context.all_static_vars, context.static_vars_in_set);
877 pointer_set_destroy (context.visited);
878 lto_bitmap_free (context.static_vars_in_set);
879 lto_bitmap_free (context.seen_node_decls);
882 lto_bitmap_free (context.all_vars);
883 lto_bitmap_free (context.all_static_vars);
887 /* Given a file name FNAME, return a string with FNAME prefixed with '*'. */
890 prefix_name_with_star (const char *fname)
895 len = strlen (fname) + 1 + 1;
896 star_fname = XNEWVEC (char, len);
897 snprintf (star_fname, len, "*%s", fname);
903 /* Return a copy of FNAME without the .o extension. */
906 strip_extension (const char *fname)
908 char *s = XNEWVEC (char, strlen (fname) - 2 + 1);
909 gcc_assert (strstr (fname, ".o"));
910 snprintf (s, strlen (fname) - 2 + 1, "%s", fname);
916 /* Return a file name associated with cgraph node set SET. This may
917 be a new temporary file name if SET needs to be processed by
918 LTRANS, or the original file name if all the nodes in SET belong to
919 the same input file. */
922 get_filename_for_set (cgraph_node_set set)
925 static const size_t max_fname_len = 100;
927 if (cgraph_node_set_needs_ltrans_p (set))
929 /* Create a new temporary file to store SET. To facilitate
930 debugging, use file names from SET as part of the new
931 temporary file name. */
932 cgraph_node_set_iterator si;
933 struct pointer_set_t *pset = pointer_set_create ();
934 for (si = csi_start (set); !csi_end_p (si); csi_next (&si))
936 struct cgraph_node *n = csi_node (si);
937 const char *node_fname;
940 /* Don't use the same file name more than once. */
941 if (pointer_set_insert (pset, n->local.lto_file_data))
944 /* The first file name found in SET determines the output
945 directory. For the remaining files, we use their
947 node_fname = n->local.lto_file_data->file_name;
950 fname = strip_extension (node_fname);
954 f = strip_extension (lbasename (node_fname));
956 /* If the new name causes an excessively long file name,
957 make the last component "___" to indicate overflow. */
958 if (strlen (fname) + strlen (f) > max_fname_len - 3)
960 fname = reconcat (fname, fname, "___", NULL);
965 fname = reconcat (fname, fname, "_", f, NULL);
970 pointer_set_destroy (pset);
972 /* Add the extension .wpa.o to indicate that this file has been
974 fname = reconcat (fname, fname, ".wpa.o", NULL);
979 /* Since SET does not need to be processed by LTRANS, use
980 the original file name and mark it with a '*' prefix so that
981 lto_execute_ltrans knows not to process it. */
982 cgraph_node_set_iterator si = csi_start (set);
983 struct cgraph_node *first = csi_node (si);
984 fname = prefix_name_with_star (first->local.lto_file_data->file_name);
990 static lto_file *current_lto_file;
993 /* Write all output files in WPA mode. Returns a NULL-terminated array of
994 output file names. */
997 lto_wpa_write_files (void)
1000 unsigned i, n_sets, last_out_file_ix, num_out_files;
1002 cgraph_node_set set;
1004 VEC(bitmap,heap) *inlined_decls = NULL;
1006 timevar_push (TV_WHOPR_WPA);
1008 /* Include all inlined functions and determine what sets need to be
1009 compiled by LTRANS. After this loop, only those sets that
1010 contain callgraph nodes from more than one file will need to be
1011 compiled by LTRANS. */
1012 for (i = 0; VEC_iterate (cgraph_node_set, lto_cgraph_node_sets, i, set); i++)
1014 decls = lto_add_all_inlinees (set);
1015 VEC_safe_push (bitmap, heap, inlined_decls, decls);
1016 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
1020 /* After adding all inlinees, find out statics that need to be promoted
1021 to globals because of cross-file inlining. */
1022 lto_promote_cross_file_statics ();
1024 timevar_pop (TV_WHOPR_WPA);
1026 timevar_push (TV_WHOPR_WPA_IO);
1028 /* The number of output files depends on the number of input files
1029 and how many callgraph node sets we create. Reserve enough space
1030 for the maximum of these two. */
1031 num_out_files = MAX (VEC_length (cgraph_node_set, lto_cgraph_node_sets),
1033 output_files = XNEWVEC (char *, num_out_files + 1);
1035 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
1036 for (i = 0; i < n_sets; i++)
1038 char *temp_filename;
1040 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
1041 temp_filename = get_filename_for_set (set);
1042 output_files[i] = temp_filename;
1044 if (cgraph_node_set_needs_ltrans_p (set))
1046 /* Write all the nodes in SET to TEMP_FILENAME. */
1047 file = lto_elf_file_open (temp_filename, true);
1049 fatal_error ("lto_elf_file_open() failed");
1051 lto_set_current_out_file (file);
1052 lto_new_extern_inline_states ();
1054 decls = VEC_index (bitmap, inlined_decls, i);
1055 lto_force_functions_extern_inline (decls);
1057 ipa_write_summaries_of_cgraph_node_set (set);
1058 lto_delete_extern_inline_states ();
1060 lto_set_current_out_file (NULL);
1061 lto_elf_file_close (file);
1065 last_out_file_ix = n_sets;
1067 lto_stats.num_output_files += n_sets;
1069 output_files[last_out_file_ix] = NULL;
1071 for (i = 0; VEC_iterate (bitmap, inlined_decls, i, decls); i++)
1072 lto_bitmap_free (decls);
1073 VEC_free (bitmap, heap, inlined_decls);
1075 timevar_pop (TV_WHOPR_WPA_IO);
1077 return output_files;
1080 /* Template of LTRANS dumpbase suffix. */
1081 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
1083 /* Perform local transformations (LTRANS) on the files in the NULL-terminated
1084 FILES array. These should have been written previously by
1085 lto_wpa_write_files (). Transformations are performed via executing
1086 COLLECT_GCC for reach file. */
1089 lto_execute_ltrans (char *const *files)
1091 struct pex_obj *pex;
1092 const char *collect_gcc_options, *collect_gcc;
1093 struct obstack env_obstack;
1095 const char **argv_ptr;
1100 FILE *ltrans_output_list_stream = NULL;
1101 bool seen_dumpbase = false;
1102 char *dumpbase_suffix = NULL;
1104 timevar_push (TV_WHOPR_WPA_LTRANS_EXEC);
1106 /* Get the driver and options. */
1107 collect_gcc = getenv ("COLLECT_GCC");
1109 fatal_error ("environment variable COLLECT_GCC must be set");
1111 /* Set the CFLAGS environment variable. */
1112 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1113 if (!collect_gcc_options)
1114 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
1116 /* Count arguments. */
1118 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
1119 if (collect_gcc_options[j] == '\'')
1123 fatal_error ("malformed COLLECT_GCC_OPTIONS");
1125 /* Initalize the arguments for the LTRANS driver. */
1126 argv = XNEWVEC (const char *, 8 + i / 2);
1128 *argv_ptr++ = collect_gcc;
1129 *argv_ptr++ = "-xlto";
1130 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
1131 if (collect_gcc_options[j] == '\'')
1137 while (collect_gcc_options[j] != '\'')
1139 obstack_init (&env_obstack);
1140 obstack_grow (&env_obstack, &collect_gcc_options[i], j - i);
1142 obstack_grow (&env_obstack, DUMPBASE_SUFFIX,
1143 sizeof (DUMPBASE_SUFFIX));
1145 obstack_1grow (&env_obstack, 0);
1146 option = XOBFINISH (&env_obstack, char *);
1149 dumpbase_suffix = option + 7 + j - i;
1150 seen_dumpbase = false;
1153 /* LTRANS does not need -fwpa nor -fltrans-*. */
1154 if (strncmp (option, "-fwpa", 5) != 0
1155 && strncmp (option, "-fltrans-", 9) != 0)
1157 if (strncmp (option, "-dumpbase", 9) == 0)
1158 seen_dumpbase = true;
1159 *argv_ptr++ = option;
1162 *argv_ptr++ = "-fltrans";
1164 /* Open the LTRANS output list. */
1165 if (ltrans_output_list)
1167 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
1168 if (ltrans_output_list_stream == NULL)
1169 error ("opening LTRANS output list %s: %m", ltrans_output_list);
1172 for (i = 0; files[i]; ++i)
1176 /* If the file is prefixed with a '*', it means that we do not
1177 need to re-compile it with LTRANS because it has not been
1178 modified by WPA. Skip it from the command line to
1179 lto_execute_ltrans, but add it to ltrans_output_list_stream
1180 so it is linked after we are done. */
1181 if (files[i][0] == '*')
1183 size_t len = strlen (files[i]) - 1;
1184 if (ltrans_output_list_stream)
1185 if (fwrite (&files[i][1], 1, len, ltrans_output_list_stream) < len
1186 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1187 error ("writing to LTRANS output list %s: %m",
1188 ltrans_output_list);
1194 /* Otherwise, add FILES[I] to lto_execute_ltrans command line
1195 and add the resulting file to LTRANS output list. */
1197 /* Replace the .o suffix with a .ltrans.o suffix and write
1198 the resulting name to the LTRANS output list. */
1199 obstack_init (&env_obstack);
1200 obstack_grow (&env_obstack, files[i], strlen (files[i]) - 2);
1201 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1202 output_name = XOBFINISH (&env_obstack, char *);
1203 if (ltrans_output_list_stream)
1205 len = strlen (output_name);
1207 if (fwrite (output_name, 1, len, ltrans_output_list_stream) < len
1208 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1209 error ("writing to LTRANS output list %s: %m",
1210 ltrans_output_list);
1214 argv_ptr[1] = output_name;
1215 argv_ptr[2] = files[i];
1218 /* Append a sequence number to -dumpbase for LTRANS. */
1219 if (dumpbase_suffix)
1220 snprintf (dumpbase_suffix, sizeof (DUMPBASE_SUFFIX) - 7,
1221 "%lu", (unsigned long) i);
1223 /* Execute the driver. */
1224 pex = pex_init (0, "lto1", NULL);
1226 fatal_error ("pex_init failed: %s", xstrerror (errno));
1228 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0],
1229 CONST_CAST (char **, argv), NULL, NULL, &err);
1231 fatal_error ("%s: %s", errmsg, xstrerror (err));
1233 if (!pex_get_status (pex, 1, &status))
1234 fatal_error ("can't get program status: %s", xstrerror (errno));
1238 if (WIFSIGNALED (status))
1240 int sig = WTERMSIG (status);
1241 fatal_error ("%s terminated with signal %d [%s]%s",
1242 argv[0], sig, strsignal (sig),
1243 WCOREDUMP (status) ? ", core dumped" : "");
1246 fatal_error ("%s terminated with status %d", argv[0], status);
1253 /* Close the LTRANS output list. */
1254 if (ltrans_output_list_stream && fclose (ltrans_output_list_stream))
1255 error ("closing LTRANS output list %s: %m", ltrans_output_list);
1257 obstack_free (&env_obstack, NULL);
1260 timevar_pop (TV_WHOPR_WPA_LTRANS_EXEC);
1265 struct pointer_set_t *seen;
1268 #define LTO_FIXUP_SUBTREE(t) \
1270 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1273 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
1277 (t) = gimple_register_type (t); \
1278 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1282 static tree lto_fixup_tree (tree *, int *, void *);
1284 /* Return true if T does not need to be fixed up recursively. */
1290 || CONSTANT_CLASS_P (t)
1291 || TREE_CODE (t) == IDENTIFIER_NODE);
1294 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
1297 lto_fixup_common (tree t, void *data)
1299 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
1300 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
1301 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
1302 First remove us from any pointer list we are on. */
1303 if (TREE_CODE (t) == POINTER_TYPE)
1305 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
1306 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
1309 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
1310 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
1311 tem = TYPE_NEXT_PTR_TO (tem);
1313 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
1315 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
1317 else if (TREE_CODE (t) == REFERENCE_TYPE)
1319 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
1320 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
1323 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
1324 while (tem && TYPE_NEXT_REF_TO (tem) != t)
1325 tem = TYPE_NEXT_REF_TO (tem);
1327 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
1329 TYPE_NEXT_REF_TO (t) = NULL_TREE;
1332 /* Fixup our type. */
1333 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1335 /* Second put us on the list of pointers of the new pointed-to type
1336 if we are a main variant. This is done in lto_fixup_type after
1337 fixing up our main variant. */
1339 /* This is not very efficient because we cannot do tail-recursion with
1340 a long chain of trees. */
1341 LTO_FIXUP_SUBTREE (TREE_CHAIN (t));
1344 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
1347 lto_fixup_decl_minimal (tree t, void *data)
1349 lto_fixup_common (t, data);
1350 LTO_FIXUP_SUBTREE (DECL_NAME (t));
1351 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t));
1354 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1357 lto_fixup_decl_common (tree t, void *data)
1359 lto_fixup_decl_minimal (t, data);
1360 LTO_FIXUP_SUBTREE (DECL_SIZE (t));
1361 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t));
1362 LTO_FIXUP_SUBTREE (DECL_INITIAL (t));
1363 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t));
1364 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t));
1367 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1370 lto_fixup_decl_with_vis (tree t, void *data)
1372 lto_fixup_decl_common (t, data);
1374 /* Accessor macro has side-effects, use field-name here. */
1375 LTO_FIXUP_SUBTREE (t->decl_with_vis.assembler_name);
1377 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t)));
1380 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1383 lto_fixup_decl_non_common (tree t, void *data)
1385 lto_fixup_decl_with_vis (t, data);
1386 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t));
1387 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t));
1388 LTO_FIXUP_SUBTREE (DECL_VINDEX (t));
1390 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1391 gcc_assert (no_fixup_p (t->decl_non_common.saved_tree));
1394 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1397 lto_fixup_function (tree t, void *data)
1399 lto_fixup_decl_non_common (t, data);
1400 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t));
1403 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1406 lto_fixup_field_decl (tree t, void *data)
1408 lto_fixup_decl_common (t, data);
1409 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t));
1410 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t));
1411 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t));
1412 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t)));
1413 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t));
1416 /* Fix up fields of a type T. DATA points to fix-up states. */
1419 lto_fixup_type (tree t, void *data)
1423 lto_fixup_common (t, data);
1424 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t));
1425 LTO_FIXUP_SUBTREE (TYPE_SIZE (t));
1426 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t));
1427 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t));
1428 LTO_FIXUP_SUBTREE (TYPE_NAME (t));
1430 /* Accessors are for derived node types only. */
1431 if (!POINTER_TYPE_P (t))
1432 LTO_FIXUP_SUBTREE (t->type.minval);
1433 LTO_FIXUP_SUBTREE (t->type.maxval);
1435 /* Accessor is for derived node types only. */
1436 LTO_FIXUP_SUBTREE (t->type.binfo);
1438 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1439 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CANONICAL (t));
1441 /* The following re-creates proper variant lists while fixing up
1442 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1443 variant list state before fixup is broken. */
1445 /* Remove us from our main variant list if we are not the variant leader. */
1446 if (TYPE_MAIN_VARIANT (t) != t)
1448 tem = TYPE_MAIN_VARIANT (t);
1449 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1450 tem = TYPE_NEXT_VARIANT (tem);
1452 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1453 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1456 /* Query our new main variant. */
1457 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1459 /* If we were the variant leader and we get replaced ourselves drop
1460 all variants from our list. */
1461 if (TYPE_MAIN_VARIANT (t) == t
1467 tree tem2 = TYPE_NEXT_VARIANT (tem);
1468 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1473 /* If we are not our own variant leader link us into our new leaders
1477 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1478 TYPE_NEXT_VARIANT (mv) = t;
1481 /* Finally adjust our main variant and fix it up. */
1482 TYPE_MAIN_VARIANT (t) = mv;
1483 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1485 /* As the second step of reconstructing the pointer chains put us
1486 on the list of pointers of the new pointed-to type
1487 if we are a main variant. See lto_fixup_common for the first step. */
1488 if (TREE_CODE (t) == POINTER_TYPE
1489 && TYPE_MAIN_VARIANT (t) == t)
1491 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1492 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1494 else if (TREE_CODE (t) == REFERENCE_TYPE
1495 && TYPE_MAIN_VARIANT (t) == t)
1497 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1498 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1502 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1505 lto_fixup_binfo (tree t, void *data)
1507 unsigned HOST_WIDE_INT i, n;
1508 tree base, saved_base;
1510 lto_fixup_common (t, data);
1511 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1512 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1513 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1514 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1515 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1516 for (i = 0; i < n; i++)
1518 saved_base = base = BINFO_BASE_ACCESS (t, i);
1519 LTO_FIXUP_SUBTREE (base);
1520 if (base != saved_base)
1521 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1523 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1524 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1525 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1526 n = BINFO_N_BASE_BINFOS (t);
1527 for (i = 0; i < n; i++)
1529 saved_base = base = BINFO_BASE_BINFO (t, i);
1530 LTO_FIXUP_SUBTREE (base);
1531 if (base != saved_base)
1532 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1536 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1539 lto_fixup_constructor (tree t, void *data)
1541 unsigned HOST_WIDE_INT idx;
1542 constructor_elt *ce;
1544 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1547 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1550 LTO_FIXUP_SUBTREE (ce->index);
1551 LTO_FIXUP_SUBTREE (ce->value);
1555 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1556 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1557 DATA is a pointer set to record visited nodes. */
1560 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1563 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1568 if (pointer_set_contains (fixup_data->seen, t))
1571 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1573 prevailing = lto_symtab_prevailing_decl (t);
1575 if (t != prevailing)
1577 if (TREE_CODE (t) == FUNCTION_DECL
1578 && TREE_NOTHROW (prevailing) != TREE_NOTHROW (t))
1580 /* If the prevailing definition does not throw but the
1581 declaration (T) was considered throwing, then we
1582 simply add PREVAILING to the list of throwing
1583 functions. However, if the opposite is true, then
1584 the call to PREVAILING was generated assuming that
1585 the function didn't throw, which means that CFG
1586 cleanup may have removed surrounding try/catch
1589 Note that we currently accept these cases even when
1590 they occur within a single file. It's certainly a
1591 user error, but we silently allow the compiler to
1592 remove surrounding try/catch regions. Perhaps we
1593 could emit a warning here, instead of silently
1594 accepting the conflicting declaration. */
1595 if (TREE_NOTHROW (prevailing))
1596 lto_mark_nothrow_fndecl (prevailing);
1599 /* Also replace t with prevailing defintion. We don't want to
1600 insert the other defintion in the seen set as we want to
1601 replace all instances of it. */
1606 else if (TYPE_P (t))
1608 /* Replace t with the prevailing type. We don't want to insert the
1609 other type in the seen set as we want to replace all instances of it. */
1610 t = gimple_register_type (t);
1614 if (pointer_set_insert (fixup_data->seen, t))
1617 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1618 Hence we do special processing here for those kind of nodes. */
1619 switch (TREE_CODE (t))
1622 lto_fixup_field_decl (t, data);
1630 lto_fixup_decl_common (t, data);
1634 lto_fixup_decl_with_vis (t, data);
1638 lto_fixup_decl_non_common (t, data);
1642 lto_fixup_function (t, data);
1646 lto_fixup_binfo (t, data);
1651 lto_fixup_type (t, data);
1652 else if (TREE_CODE (t) == CONSTRUCTOR)
1653 lto_fixup_constructor (t, data);
1654 else if (CONSTANT_CLASS_P (t))
1655 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1656 else if (EXPR_P (t))
1658 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1659 lto_fixup_common (t, data);
1660 LTO_FIXUP_SUBTREE (t->exp.block);
1665 /* Let walk_tree handle sub-trees. */
1673 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1674 replaces var and function decls with the corresponding prevailing def and
1675 records the old decl in the free-list in DATA. We also record visted nodes
1676 in the seen-set in DATA to avoid multiple visit for nodes that need not
1680 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1683 struct lto_tree_ref_table *table;
1685 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1686 we still need to walk from all DECLs to find the reachable
1687 FUNCTION_DECLs and VAR_DECLs. */
1688 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1690 table = &state->streams[si];
1691 for (i = 0; i < table->size; i++)
1692 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1696 /* A callback of htab_traverse. Just extract a state from SLOT and the
1697 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1700 lto_fixup_state_aux (void **slot, void *aux)
1702 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1703 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1707 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1711 lto_fixup_decls (struct lto_file_decl_data **files)
1715 struct pointer_set_t *seen = pointer_set_create ();
1716 lto_fixup_data_t data;
1719 for (i = 0; files[i]; i++)
1721 struct lto_file_decl_data *file = files[i];
1722 struct lto_in_decl_state *state = file->global_decl_state;
1723 lto_fixup_state (state, &data);
1725 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1728 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1730 tree saved_decl = decl;
1731 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1732 if (decl != saved_decl)
1733 VEC_replace (tree, lto_global_var_decls, i, decl);
1736 pointer_set_destroy (seen);
1739 /* Unlink a temporary LTRANS file unless requested otherwise. */
1742 lto_maybe_unlink (const char *file)
1744 if (!getenv ("WPA_SAVE_LTRANS"))
1746 if (unlink_if_ordinary (file))
1747 error ("deleting LTRANS input file %s: %m", file);
1750 fprintf (stderr, "[Leaving LTRANS input file %s]\n", file);
1753 /* Read the options saved from each file in the command line. Called
1754 from lang_hooks.post_options which is called by process_options
1755 right before all the options are used to initialize the compiler.
1756 This assumes that decode_options has already run, so the
1757 num_in_fnames and in_fnames are properly set.
1759 Note that this assumes that all the files had been compiled with
1760 the same options, which is not a good assumption. In general,
1761 options ought to be read from all the files in the set and merged.
1762 However, it is still unclear what the merge rules should be. */
1765 lto_read_all_file_options (void)
1769 /* Clear any file options currently saved. */
1770 lto_clear_file_options ();
1772 /* Set the hooks to read ELF sections. */
1773 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1775 for (i = 0; i < num_in_fnames; i++)
1777 struct lto_file_decl_data *file_data;
1778 lto_file *file = lto_elf_file_open (in_fnames[i], false);
1782 file_data = XCNEW (struct lto_file_decl_data);
1783 file_data->file_name = file->filename;
1784 file_data->section_hash_table = lto_elf_build_section_table (file);
1786 lto_read_file_options (file_data);
1788 lto_elf_file_close (file);
1789 htab_delete (file_data->section_hash_table);
1793 /* Apply globally the options read from all the files. */
1794 lto_reissue_options ();
1798 /* Read all the symbols from the input files FNAMES. NFILES is the
1799 number of files requested in the command line. Instantiate a
1800 global call graph by aggregating all the sub-graphs found in each
1804 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1806 unsigned int i, last_file_ix;
1807 struct lto_file_decl_data **all_file_decl_data;
1809 struct cgraph_node *node;
1811 lto_stats.num_input_files = nfiles;
1813 timevar_push (TV_IPA_LTO_DECL_IO);
1815 /* Set the hooks so that all of the ipa passes can read in their data. */
1816 all_file_decl_data = XNEWVEC (struct lto_file_decl_data *, nfiles + 1);
1817 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1819 /* Read the resolution file. */
1821 if (resolution_file_name)
1824 unsigned num_objects;
1826 resolution = fopen (resolution_file_name, "r");
1827 if (resolution == NULL)
1828 fatal_error ("could not open symbol resolution file: %s",
1831 t = fscanf (resolution, "%u", &num_objects);
1832 gcc_assert (t == 1);
1834 /* True, since the plugin splits the archives. */
1835 gcc_assert (num_objects == nfiles);
1838 /* Read all of the object files specified on the command line. */
1839 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1841 struct lto_file_decl_data *file_data = NULL;
1843 current_lto_file = lto_elf_file_open (fnames[i], false);
1844 if (!current_lto_file)
1847 file_data = lto_file_read (current_lto_file, resolution);
1851 all_file_decl_data[last_file_ix++] = file_data;
1853 lto_elf_file_close (current_lto_file);
1854 current_lto_file = NULL;
1857 if (resolution_file_name)
1858 fclose (resolution);
1860 all_file_decl_data[last_file_ix] = NULL;
1862 /* Set the hooks so that all of the ipa passes can read in their data. */
1863 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1865 /* Each pass will set the appropriate timer. */
1866 timevar_pop (TV_IPA_LTO_DECL_IO);
1868 /* Read the callgraph. */
1871 /* Merge global decls. */
1872 lto_symtab_merge_decls ();
1874 /* Fixup all decls and types and free the type hash tables. */
1875 lto_fixup_decls (all_file_decl_data);
1876 free_gimple_type_tables ();
1878 /* Read the IPA summary data. */
1879 ipa_read_summaries ();
1881 /* Finally merge the cgraph according to the decl merging decisions. */
1882 lto_symtab_merge_cgraph_nodes ();
1884 /* Mark cgraph nodes needed in the merged cgraph
1885 This normally happens in whole-program pass, but for
1886 ltrans the pass was already run at WPA phase.
1888 FIXME: This is not valid way to do so; nodes can be needed
1889 for non-obvious reasons. We should stream the flags from WPA
1892 for (node = cgraph_nodes; node; node = node->next)
1894 if (!node->global.inlined_to
1895 && cgraph_decide_is_function_needed (node, node->decl))
1896 cgraph_mark_needed_node (node);
1897 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1898 summaries computed and needs to apply changes. At the moment WHOPR only
1899 supports inlining, so we can push it here by hand. In future we need to stream
1900 this field into ltrans compilation. */
1902 VEC_safe_push (ipa_opt_pass, heap,
1903 node->ipa_transforms_to_apply,
1904 (ipa_opt_pass)&pass_ipa_inline);
1907 timevar_push (TV_IPA_LTO_DECL_IO);
1909 /* FIXME lto. This loop needs to be changed to use the pass manager to
1910 call the ipa passes directly. */
1912 for (i = 0; i < last_file_ix; i++)
1914 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1915 lto_materialize_constructors_and_inits (file_data);
1918 /* Indicate that the cgraph is built and ready. */
1919 cgraph_function_flags_ready = true;
1921 timevar_pop (TV_IPA_LTO_DECL_IO);
1925 /* Materialize all the bodies for all the nodes in the callgraph. */
1928 materialize_cgraph (void)
1931 struct cgraph_node *node;
1933 timevar_id_t lto_timer;
1935 /* Now that we have input the cgraph, we need to clear all of the aux
1936 nodes and read the functions if we are not running in WPA mode. */
1937 timevar_push (TV_IPA_LTO_GIMPLE_IO);
1939 for (node = cgraph_nodes; node; node = node->next)
1941 /* Some cgraph nodes get created on the fly, and they don't need
1942 to be materialized. For instance, nodes for nested functions
1943 where the parent function was not streamed out or builtin
1944 functions. Additionally, builtin functions should not be
1945 materialized and may, in fact, cause confusion because there
1946 may be a regular function in the file whose assembler name
1947 matches that of the function.
1948 See gcc.c-torture/execute/20030125-1.c and
1949 gcc.c-torture/execute/921215-1.c. */
1950 if (node->local.lto_file_data
1951 && !DECL_IS_BUILTIN (node->decl))
1953 lto_materialize_function (node);
1954 lto_stats.num_input_cgraph_nodes++;
1958 timevar_pop (TV_IPA_LTO_GIMPLE_IO);
1960 /* Start the appropriate timer depending on the mode that we are
1962 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1963 : (flag_ltrans) ? TV_WHOPR_LTRANS
1965 timevar_push (lto_timer);
1967 current_function_decl = NULL;
1970 /* Inform the middle end about the global variables we have seen. */
1971 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1972 rest_of_decl_compilation (decl, 1, 0);
1974 /* Fix up any calls to DECLs that have become not exception throwing. */
1975 lto_fixup_nothrow_decls ();
1977 timevar_pop (lto_timer);
1981 /* Perform whole program analysis (WPA) on the callgraph and write out the
1982 optimization plan. */
1985 do_whole_program_analysis (void)
1987 char **output_files;
1989 struct cgraph_node *node;
1993 /* Note that since we are in WPA mode, materialize_cgraph will not
1994 actually read in all the function bodies. It only materializes
1995 the decls and cgraph nodes so that analysis can be performed. */
1996 materialize_cgraph ();
1998 /* Reading in the cgraph uses different timers, start timing WPA now. */
1999 timevar_push (TV_WHOPR_WPA);
2001 /* FIXME lto. Hack. We should use the IPA passes. There are a
2002 number of issues with this now. 1. There is no convenient way to
2003 do this. 2. Some passes may depend on properties that requires
2004 the function bodies to compute. */
2005 cgraph_function_flags_ready = true;
2006 bitmap_obstack_initialize (NULL);
2007 ipa_register_cgraph_hooks ();
2009 /* Reset inlining information before running IPA inliner. */
2010 for (node = cgraph_nodes; node; node = node->next)
2011 reset_inline_failed (node);
2013 /* FIXME lto. We should not call this function directly. */
2014 pass_ipa_inline.pass.execute ();
2017 bitmap_obstack_release (NULL);
2019 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
2020 timevar_pop (TV_WHOPR_WPA);
2022 output_files = lto_wpa_write_files ();
2024 /* Show the LTO report before launching LTRANS. */
2025 if (flag_lto_report)
2026 print_lto_report ();
2028 lto_execute_ltrans (output_files);
2030 for (i = 0; output_files[i]; ++i)
2032 if (output_files[i][0] != '*')
2033 lto_maybe_unlink (output_files[i]);
2035 free (output_files[i]);
2038 XDELETEVEC (output_files);
2042 /* Main entry point for the GIMPLE front end. This front end has
2043 three main personalities:
2045 - LTO (-flto). All the object files on the command line are
2046 loaded in memory and processed as a single translation unit.
2047 This is the traditional link-time optimization behavior.
2049 - WPA (-fwpa). Only the callgraph and summary information for
2050 files in the command file are loaded. A single callgraph
2051 (without function bodies) is instantiated for the whole set of
2052 files. IPA passes are only allowed to analyze the call graph
2053 and make transformation decisions. The callgraph is
2054 partitioned, each partition is written to a new object file
2055 together with the transformation decisions.
2057 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
2058 summary files from running again. Since WPA computed summary
2059 information and decided what transformations to apply, LTRANS
2060 simply applies them. */
2063 lto_main (int debug_p ATTRIBUTE_UNUSED)
2067 /* Read all the symbols and call graph from all the files in the
2069 read_cgraph_and_symbols (num_in_fnames, in_fnames);
2073 /* If WPA is enabled analyze the whole call graph and create an
2074 optimization plan. Otherwise, read in all the function
2075 bodies and continue with optimization. */
2077 do_whole_program_analysis ();
2080 materialize_cgraph ();
2082 /* Let the middle end know that we have read and merged all of
2086 /* FIXME lto, if the processes spawned by WPA fail, we miss
2087 the chance to print WPA's report, so WPA will call
2088 print_lto_report before launching LTRANS. If LTRANS was
2089 launched directly by the driver we would not need to do
2091 if (flag_lto_report)
2092 print_lto_report ();
2097 #include "gt-lto-lto.h"