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;
88 /* Ignore clone nodes. Read the body only from the original one.
89 We may find clone nodes during LTRANS after WPA has made inlining
95 file_data = node->local.lto_file_data;
96 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
98 /* We may have renamed the declaration, e.g., a static function. */
99 name = lto_get_decl_name_mapping (file_data, name);
101 data = lto_get_section_data (file_data, LTO_section_function_body,
105 gcc_assert (!DECL_IS_BUILTIN (decl));
107 /* This function has a definition. */
108 TREE_STATIC (decl) = 1;
110 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
112 /* Load the function body only if not operating in WPA mode. In
113 WPA mode, the body of the function is not needed. */
116 allocate_struct_function (decl, false);
117 announce_function (node->decl);
118 lto_input_function_body (file_data, decl, data);
119 lto_stats.num_function_bodies++;
122 lto_free_section_data (file_data, LTO_section_function_body, name,
128 DECL_EXTERNAL (decl) = 1;
130 /* Let the middle end know about the function. */
131 rest_of_decl_compilation (decl, 1, 0);
135 /* Decode the content of memory pointed to by DATA in the the
136 in decl state object STATE. DATA_IN points to a data_in structure for
137 decoding. Return the address after the decoded object in the input. */
139 static const uint32_t *
140 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
141 struct lto_in_decl_state *state)
148 decl = lto_streamer_cache_get (data_in->reader_cache, (int) ix);
149 if (TREE_CODE (decl) != FUNCTION_DECL)
151 gcc_assert (decl == void_type_node);
154 state->fn_decl = decl;
156 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
158 uint32_t size = *data++;
159 tree *decls = GGC_NEWVEC (tree, size);
161 for (j = 0; j < size; j++)
163 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
165 /* Register every type in the global type table. If the
166 type existed already, use the existing type. */
167 if (TYPE_P (decls[j]))
168 decls[j] = gimple_register_type (decls[j]);
171 state->streams[i].size = size;
172 state->streams[i].trees = decls;
180 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
181 RESOLUTIONS is the set of symbols picked by the linker (read from the
182 resolution file when the linker plugin is being used). */
185 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
186 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
188 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
189 const int32_t decl_offset = sizeof (struct lto_decl_header);
190 const int32_t main_offset = decl_offset + header->decl_state_size;
191 const int32_t string_offset = main_offset + header->main_size;
192 struct lto_input_block ib_main;
193 struct data_in *data_in;
195 const uint32_t *data_ptr, *data_end;
196 uint32_t num_decl_states;
198 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
201 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
202 header->string_size, resolutions);
204 /* Read the global declarations and types. */
205 while (ib_main.p < ib_main.len)
207 tree t = lto_input_tree (&ib_main, data_in);
208 gcc_assert (t && ib_main.p <= ib_main.len);
211 /* Read in lto_in_decl_state objects. */
212 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
214 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
215 num_decl_states = *data_ptr++;
217 gcc_assert (num_decl_states > 0);
218 decl_data->global_decl_state = lto_new_in_decl_state ();
219 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
220 decl_data->global_decl_state);
222 /* Read in per-function decl states and enter them in hash table. */
223 decl_data->function_decl_states =
224 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
226 for (i = 1; i < num_decl_states; i++)
228 struct lto_in_decl_state *state = lto_new_in_decl_state ();
231 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
232 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
233 gcc_assert (*slot == NULL);
237 if (data_ptr != data_end)
238 internal_error ("bytecode stream: garbage at the end of symbols section");
240 /* Set the current decl state to be the global state. */
241 decl_data->current_decl_state = decl_data->global_decl_state;
243 lto_data_in_delete (data_in);
246 /* strtoll is not portable. */
248 lto_parse_hex (const char *p) {
250 for (; *p != '\0'; ++p)
255 if (c >= '0' && c <= '9')
257 else if (c >= 'a' && c <= 'f')
259 else if (c >= 'A' && c <= 'F')
262 internal_error ("could not parse hex number");
268 /* Read resolution for file named FILE_NAME. The resolution is read from
269 RESOLUTION. An array with the symbol resolution is returned. The array
270 size is written to SIZE. */
272 static VEC(ld_plugin_symbol_resolution_t,heap) *
273 lto_resolution_read (FILE *resolution, lto_file *file)
275 /* We require that objects in the resolution file are in the same
276 order as the lto1 command line. */
277 unsigned int name_len;
279 unsigned int num_symbols;
281 VEC(ld_plugin_symbol_resolution_t,heap) *ret = NULL;
282 unsigned max_index = 0;
287 name_len = strlen (file->filename);
288 obj_name = XNEWVEC (char, name_len + 1);
289 fscanf (resolution, " "); /* Read white space. */
291 fread (obj_name, sizeof (char), name_len, resolution);
292 obj_name[name_len] = '\0';
293 if (strcmp (obj_name, file->filename) != 0)
294 internal_error ("unexpected file name %s in linker resolution file. "
295 "Expected %s", obj_name, file->filename);
296 if (file->offset != 0)
301 t = fscanf (resolution, "@0x%16s", offset_p);
303 internal_error ("could not parse file offset");
304 offset = lto_parse_hex (offset_p);
305 if (offset != file->offset)
306 internal_error ("unexpected offset");
311 fscanf (resolution, "%u", &num_symbols);
313 for (i = 0; i < num_symbols; i++)
318 enum ld_plugin_symbol_resolution r;
320 unsigned int lto_resolution_str_len =
321 sizeof (lto_resolution_str) / sizeof (char *);
323 t = fscanf (resolution, "%u %26s %*[^\n]\n", &index, r_str);
325 internal_error ("Invalid line in the resolution file.");
326 if (index > max_index)
329 for (j = 0; j < lto_resolution_str_len; j++)
331 if (strcmp (lto_resolution_str[j], r_str) == 0)
333 r = (enum ld_plugin_symbol_resolution) j;
337 if (j == lto_resolution_str_len)
338 internal_error ("Invalid resolution in the resolution file.");
340 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap, ret,
342 VEC_replace (ld_plugin_symbol_resolution_t, ret, index, r);
348 /* Generate a TREE representation for all types and external decls
351 Read all of the globals out of the file. Then read the cgraph
352 and process the .o index into the cgraph nodes so that it can open
353 the .o file to load the functions and ipa information. */
355 static struct lto_file_decl_data *
356 lto_file_read (lto_file *file, FILE *resolution_file)
358 struct lto_file_decl_data *file_data;
361 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions;
363 resolutions = lto_resolution_read (resolution_file, file);
365 file_data = GGC_NEW (struct lto_file_decl_data);
366 file_data->file_name = file->filename;
367 file_data->section_hash_table = lto_obj_build_section_table (file);
368 file_data->renaming_hash_table = lto_create_renaming_table ();
370 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
371 lto_read_decls (file_data, data, resolutions);
372 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
377 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
378 #define LTO_MMAP_IO 1
382 /* Page size of machine is used for mmap and munmap calls. */
383 static size_t page_mask;
386 /* Get the section data of length LEN from FILENAME starting at
387 OFFSET. The data segment must be freed by the caller when the
388 caller is finished. Returns NULL if all was not well. */
391 lto_read_section_data (struct lto_file_decl_data *file_data,
392 intptr_t offset, size_t len)
396 static char *fd_name;
398 intptr_t computed_len;
399 intptr_t computed_offset;
403 /* Keep a single-entry file-descriptor cache. The last file we
404 touched will get closed at exit.
405 ??? Eventually we want to add a more sophisticated larger cache
406 or rather fix function body streaming to not stream them in
407 practically random order. */
409 && strcmp (fd_name, file_data->file_name) != 0)
417 fd_name = xstrdup (file_data->file_name);
418 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
426 size_t page_size = sysconf (_SC_PAGE_SIZE);
427 page_mask = ~(page_size - 1);
430 computed_offset = offset & page_mask;
431 diff = offset - computed_offset;
432 computed_len = len + diff;
434 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
435 fd, computed_offset);
436 if (result == MAP_FAILED)
439 return result + diff;
441 result = (char *) xmalloc (len);
442 if (lseek (fd, offset, SEEK_SET) != offset
443 || read (fd, result, len) != (ssize_t) len)
454 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
455 NAME will be NULL unless the section type is for a function
459 get_section_data (struct lto_file_decl_data *file_data,
460 enum lto_section_type section_type,
464 htab_t section_hash_table = file_data->section_hash_table;
465 struct lto_section_slot *f_slot;
466 struct lto_section_slot s_slot;
467 const char *section_name = lto_get_section_name (section_type, name);
471 s_slot.name = section_name;
472 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
475 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
479 free (CONST_CAST (char *, section_name));
484 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
485 starts at OFFSET and has LEN bytes. */
488 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
489 enum lto_section_type section_type ATTRIBUTE_UNUSED,
490 const char *name ATTRIBUTE_UNUSED,
491 const char *offset, size_t len ATTRIBUTE_UNUSED)
494 intptr_t computed_len;
495 intptr_t computed_offset;
500 computed_offset = ((intptr_t) offset) & page_mask;
501 diff = (intptr_t) offset - computed_offset;
502 computed_len = len + diff;
504 munmap ((caddr_t) computed_offset, computed_len);
506 free (CONST_CAST(char *, offset));
510 /* Vector of all cgraph node sets. */
511 static GTY (()) VEC(cgraph_node_set, gc) *lto_cgraph_node_sets;
512 static GTY (()) VEC(varpool_node_set, gc) *lto_varpool_node_sets;
515 /* Group cgrah nodes by input files. This is used mainly for testing
519 lto_1_to_1_map (void)
521 struct cgraph_node *node;
522 struct varpool_node *vnode;
523 struct lto_file_decl_data *file_data;
524 struct pointer_map_t *pmap;
525 struct pointer_map_t *vpmap;
527 varpool_node_set vset;
530 timevar_push (TV_WHOPR_WPA);
532 lto_cgraph_node_sets = VEC_alloc (cgraph_node_set, gc, 1);
533 lto_varpool_node_sets = VEC_alloc (varpool_node_set, gc, 1);
535 pmap = pointer_map_create ();
536 vpmap = pointer_map_create ();
538 for (node = cgraph_nodes; node; node = node->next)
540 /* We will get proper partition based on function they are inlined to or
542 if (node->global.inlined_to || node->clone_of)
544 /* Nodes without a body do not need partitioning. */
548 file_data = node->local.lto_file_data;
549 gcc_assert (!node->same_body_alias && file_data);
551 slot = pointer_map_contains (pmap, file_data);
553 set = (cgraph_node_set) *slot;
556 set = cgraph_node_set_new ();
557 slot = pointer_map_insert (pmap, file_data);
559 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
560 vset = varpool_node_set_new ();
561 slot = pointer_map_insert (vpmap, file_data);
563 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
566 cgraph_node_set_add (set, node);
569 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
571 if (vnode->alias || !vnode->needed)
573 slot = pointer_map_contains (vpmap, file_data);
575 vset = (varpool_node_set) *slot;
578 set = cgraph_node_set_new ();
579 slot = pointer_map_insert (pmap, file_data);
581 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
582 vset = varpool_node_set_new ();
583 slot = pointer_map_insert (vpmap, file_data);
585 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
588 varpool_node_set_add (vset, vnode);
591 /* If the cgraph is empty, create one cgraph node set so that there is still
592 an output file for any variables that need to be exported in a DSO. */
593 if (!lto_cgraph_node_sets)
595 set = cgraph_node_set_new ();
596 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
597 vset = varpool_node_set_new ();
598 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
601 pointer_map_destroy (pmap);
602 pointer_map_destroy (vpmap);
604 timevar_pop (TV_WHOPR_WPA);
606 lto_stats.num_cgraph_partitions += VEC_length (cgraph_node_set,
607 lto_cgraph_node_sets);
611 /* Add inlined clone NODE and its master clone to SET, if NODE itself has
612 inlined callees, recursively add the callees. */
615 lto_add_inline_clones (cgraph_node_set set, struct cgraph_node *node,
616 bitmap original_decls)
618 struct cgraph_node *callee;
619 struct cgraph_edge *edge;
621 cgraph_node_set_add (set, node);
623 /* Check to see if NODE has any inlined callee. */
624 for (edge = node->callees; edge != NULL; edge = edge->next_callee)
626 callee = edge->callee;
627 if (callee->global.inlined_to != NULL)
628 lto_add_inline_clones (set, callee, original_decls);
632 /* Compute the transitive closure of inlining of SET based on the
633 information in the callgraph. Returns a bitmap of decls that have
634 been inlined into SET indexed by UID. */
637 lto_add_all_inlinees (cgraph_node_set set)
639 cgraph_node_set_iterator csi;
640 struct cgraph_node *node;
641 bitmap original_nodes = lto_bitmap_alloc ();
642 bitmap original_decls = lto_bitmap_alloc ();
645 /* We are going to iterate SET while adding to it, mark all original
646 nodes so that we only add node inlined to original nodes. */
647 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
649 bitmap_set_bit (original_nodes, csi_node (csi)->uid);
650 bitmap_set_bit (original_decls, DECL_UID (csi_node (csi)->decl));
653 /* Some of the original nodes might not be needed anymore.
658 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
660 struct cgraph_node *inlined_to;
661 node = csi_node (csi);
663 /* NODE was not inlined. We still need it. */
664 if (!node->global.inlined_to)
667 inlined_to = node->global.inlined_to;
669 /* NODE should have only one caller. */
670 gcc_assert (!node->callers->next_caller);
672 if (!bitmap_bit_p (original_nodes, inlined_to->uid))
674 bitmap_clear_bit (original_nodes, node->uid);
675 cgraph_node_set_remove (set, node);
682 /* Transitively add to SET all the inline clones for every node that
684 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
686 node = csi_node (csi);
687 if (bitmap_bit_p (original_nodes, node->uid))
688 lto_add_inline_clones (set, node, original_decls);
691 lto_bitmap_free (original_nodes);
692 lto_bitmap_free (original_decls);
695 /* Find out all static decls that need to be promoted to global because
696 of cross file sharing. This function must be run in the WPA mode after
697 all inlinees are added. */
700 lto_promote_cross_file_statics (void)
702 struct varpool_node *vnode;
705 varpool_node_set vset;
706 cgraph_node_set_iterator csi;
707 varpool_node_set_iterator vsi;
709 gcc_assert (flag_wpa);
711 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
712 for (i = 0; i < n_sets; i++)
714 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
715 vset = VEC_index (varpool_node_set, lto_varpool_node_sets, i);
717 /* If node has either address taken (and we have no clue from where)
718 or it is called from other partition, it needs to be globalized. */
719 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
721 struct cgraph_node *node = csi_node (csi);
722 if (node->local.externally_visible)
724 if (node->clone_of || node->global.inlined_to)
726 if (!DECL_EXTERNAL (node->decl)
727 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
728 || reachable_from_other_partition_p (node, set)))
730 gcc_assert (flag_wpa);
731 TREE_PUBLIC (node->decl) = 1;
732 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
735 struct cgraph_node *alias;
736 for (alias = node->same_body;
737 alias; alias = alias->next)
739 TREE_PUBLIC (alias->decl) = 1;
740 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
745 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
747 vnode = vsi_node (vsi);
748 /* Constant pool references use internal labels and thus can not
749 be made global. It is sensible to keep those ltrans local to
750 allow better optimization. */
751 if (!DECL_IN_CONSTANT_POOL (vnode->decl)
752 && !vnode->externally_visible && vnode->analyzed
753 && referenced_from_other_partition_p (&vnode->ref_list, set, vset))
755 gcc_assert (flag_wpa);
756 TREE_PUBLIC (vnode->decl) = 1;
757 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
765 /* Given a file name FNAME, return a string with FNAME prefixed with '*'. */
768 prefix_name_with_star (const char *fname)
773 len = strlen (fname) + 1 + 1;
774 star_fname = XNEWVEC (char, len);
775 snprintf (star_fname, len, "*%s", fname);
781 /* Return a copy of FNAME without the .o extension. */
784 strip_extension (const char *fname)
786 char *s = XNEWVEC (char, strlen (fname) - 2 + 1);
787 gcc_assert (strstr (fname, ".o"));
788 snprintf (s, strlen (fname) - 2 + 1, "%s", fname);
794 /* Return a file name associated with cgraph node set SET. This may
795 be a new temporary file name if SET needs to be processed by
796 LTRANS, or the original file name if all the nodes in SET belong to
797 the same input file. */
800 get_filename_for_set (cgraph_node_set set)
803 static const size_t max_fname_len = 100;
805 /* Create a new temporary file to store SET. To facilitate
806 debugging, use file names from SET as part of the new
807 temporary file name. */
808 cgraph_node_set_iterator si;
809 struct pointer_set_t *pset = pointer_set_create ();
810 for (si = csi_start (set); !csi_end_p (si); csi_next (&si))
812 struct cgraph_node *n = csi_node (si);
813 const char *node_fname;
816 /* Don't use the same file name more than once. */
817 if (pointer_set_insert (pset, n->local.lto_file_data))
820 /* The first file name found in SET determines the output
821 directory. For the remaining files, we use their
823 node_fname = n->local.lto_file_data->file_name;
826 fname = strip_extension (node_fname);
830 f = strip_extension (lbasename (node_fname));
832 /* If the new name causes an excessively long file name,
833 make the last component "___" to indicate overflow. */
834 if (strlen (fname) + strlen (f) > max_fname_len - 3)
836 fname = reconcat (fname, fname, "___", NULL);
841 fname = reconcat (fname, fname, "_", f, NULL);
846 pointer_set_destroy (pset);
850 /* Since SET does not need to be processed by LTRANS, use
851 the original file name and mark it with a '*' prefix so that
852 lto_execute_ltrans knows not to process it. */
853 cgraph_node_set_iterator si = csi_start (set);
854 struct cgraph_node *first = csi_node (si);
855 fname = prefix_name_with_star (first->local.lto_file_data->file_name);
859 /* Add the extension .wpa.o to indicate that this file has been
861 fname = reconcat (fname, fname, ".wpa.o", NULL);
868 static lto_file *current_lto_file;
871 /* Write all output files in WPA mode. Returns a NULL-terminated array of
872 output file names. */
875 lto_wpa_write_files (void)
878 unsigned i, n_sets, last_out_file_ix, num_out_files;
881 varpool_node_set vset;
883 timevar_push (TV_WHOPR_WPA);
885 /* Include all inlined functions and determine what sets need to be
886 compiled by LTRANS. After this loop, only those sets that
887 contain callgraph nodes from more than one file will need to be
888 compiled by LTRANS. */
889 for (i = 0; VEC_iterate (cgraph_node_set, lto_cgraph_node_sets, i, set); i++)
891 lto_add_all_inlinees (set);
892 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
896 /* After adding all inlinees, find out statics that need to be promoted
897 to globals because of cross-file inlining. */
898 lto_promote_cross_file_statics ();
900 timevar_pop (TV_WHOPR_WPA);
902 timevar_push (TV_WHOPR_WPA_IO);
904 /* The number of output files depends on the number of input files
905 and how many callgraph node sets we create. Reserve enough space
906 for the maximum of these two. */
907 num_out_files = MAX (VEC_length (cgraph_node_set, lto_cgraph_node_sets),
909 output_files = XNEWVEC (char *, num_out_files + 1);
911 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
912 for (i = 0; i < n_sets; i++)
916 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
917 vset = VEC_index (varpool_node_set, lto_varpool_node_sets, i);
918 temp_filename = get_filename_for_set (set);
919 output_files[i] = temp_filename;
921 if (cgraph_node_set_nonempty_p (set) || varpool_node_set_nonempty_p (vset))
923 /* Write all the nodes in SET to TEMP_FILENAME. */
924 file = lto_obj_file_open (temp_filename, true);
926 fatal_error ("lto_obj_file_open() failed");
929 fprintf (stderr, " %s", temp_filename);
931 lto_set_current_out_file (file);
933 ipa_write_optimization_summaries (set, vset);
935 lto_set_current_out_file (NULL);
936 lto_obj_file_close (file);
940 last_out_file_ix = n_sets;
942 lto_stats.num_output_files += n_sets;
944 output_files[last_out_file_ix] = NULL;
946 timevar_pop (TV_WHOPR_WPA_IO);
951 /* Perform local transformations (LTRANS) on the files in the NULL-terminated
952 FILES array. These should have been written previously by
953 lto_wpa_write_files (). Transformations are performed via executing
954 COLLECT_GCC for reach file. */
957 lto_write_ltrans_list (char *const *files)
959 FILE *ltrans_output_list_stream = NULL;
962 /* Open the LTRANS output list. */
963 if (!ltrans_output_list)
964 error ("no LTRANS output filename provided");
966 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
967 if (ltrans_output_list_stream == NULL)
968 error ("opening LTRANS output list %s: %m", ltrans_output_list);
970 for (i = 0; files[i]; ++i)
974 len = strlen (files[i]);
975 if (fwrite (files[i], 1, len, ltrans_output_list_stream) < len
976 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
977 error ("writing to LTRANS output list %s: %m",
981 /* Close the LTRANS output list. */
982 if (fclose (ltrans_output_list_stream))
983 error ("closing LTRANS output list %s: %m", ltrans_output_list);
988 struct pointer_set_t *seen;
991 #define LTO_FIXUP_SUBTREE(t) \
993 walk_tree (&(t), lto_fixup_tree, data, NULL); \
996 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
1000 (t) = gimple_register_type (t); \
1001 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1005 static tree lto_fixup_tree (tree *, int *, void *);
1007 /* Return true if T does not need to be fixed up recursively. */
1013 || CONSTANT_CLASS_P (t)
1014 || TREE_CODE (t) == IDENTIFIER_NODE);
1017 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
1020 lto_fixup_common (tree t, void *data)
1022 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
1023 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
1024 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
1025 First remove us from any pointer list we are on. */
1026 if (TREE_CODE (t) == POINTER_TYPE)
1028 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
1029 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
1032 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
1033 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
1034 tem = TYPE_NEXT_PTR_TO (tem);
1036 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
1038 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
1040 else if (TREE_CODE (t) == REFERENCE_TYPE)
1042 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
1043 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
1046 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
1047 while (tem && TYPE_NEXT_REF_TO (tem) != t)
1048 tem = TYPE_NEXT_REF_TO (tem);
1050 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
1052 TYPE_NEXT_REF_TO (t) = NULL_TREE;
1055 /* Fixup our type. */
1056 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1058 /* Second put us on the list of pointers of the new pointed-to type
1059 if we are a main variant. This is done in lto_fixup_type after
1060 fixing up our main variant. */
1062 /* This is not very efficient because we cannot do tail-recursion with
1063 a long chain of trees. */
1064 LTO_FIXUP_SUBTREE (TREE_CHAIN (t));
1067 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
1070 lto_fixup_decl_minimal (tree t, void *data)
1072 lto_fixup_common (t, data);
1073 LTO_FIXUP_SUBTREE (DECL_NAME (t));
1074 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t));
1077 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1080 lto_fixup_decl_common (tree t, void *data)
1082 lto_fixup_decl_minimal (t, data);
1083 LTO_FIXUP_SUBTREE (DECL_SIZE (t));
1084 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t));
1085 LTO_FIXUP_SUBTREE (DECL_INITIAL (t));
1086 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t));
1087 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t));
1090 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1093 lto_fixup_decl_with_vis (tree t, void *data)
1095 lto_fixup_decl_common (t, data);
1097 /* Accessor macro has side-effects, use field-name here. */
1098 LTO_FIXUP_SUBTREE (t->decl_with_vis.assembler_name);
1100 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t)));
1103 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1106 lto_fixup_decl_non_common (tree t, void *data)
1108 lto_fixup_decl_with_vis (t, data);
1109 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t));
1110 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t));
1111 LTO_FIXUP_SUBTREE (DECL_VINDEX (t));
1113 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1114 gcc_assert (no_fixup_p (t->decl_non_common.saved_tree));
1117 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1120 lto_fixup_function (tree t, void *data)
1122 lto_fixup_decl_non_common (t, data);
1123 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t));
1126 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1129 lto_fixup_field_decl (tree t, void *data)
1131 lto_fixup_decl_common (t, data);
1132 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t));
1133 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t));
1134 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t));
1135 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t)));
1136 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t));
1139 /* Fix up fields of a type T. DATA points to fix-up states. */
1142 lto_fixup_type (tree t, void *data)
1146 lto_fixup_common (t, data);
1147 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t));
1148 LTO_FIXUP_SUBTREE (TYPE_SIZE (t));
1149 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t));
1150 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t));
1151 LTO_FIXUP_SUBTREE (TYPE_NAME (t));
1153 /* Accessors are for derived node types only. */
1154 if (!POINTER_TYPE_P (t))
1155 LTO_FIXUP_SUBTREE (t->type.minval);
1156 LTO_FIXUP_SUBTREE (t->type.maxval);
1158 /* Accessor is for derived node types only. */
1159 LTO_FIXUP_SUBTREE (t->type.binfo);
1161 if (TYPE_CONTEXT (t))
1163 if (TYPE_P (TYPE_CONTEXT (t)))
1164 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1166 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1168 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CANONICAL (t));
1170 /* The following re-creates proper variant lists while fixing up
1171 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1172 variant list state before fixup is broken. */
1174 /* Remove us from our main variant list if we are not the variant leader. */
1175 if (TYPE_MAIN_VARIANT (t) != t)
1177 tem = TYPE_MAIN_VARIANT (t);
1178 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1179 tem = TYPE_NEXT_VARIANT (tem);
1181 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1182 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1185 /* Query our new main variant. */
1186 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1188 /* If we were the variant leader and we get replaced ourselves drop
1189 all variants from our list. */
1190 if (TYPE_MAIN_VARIANT (t) == t
1196 tree tem2 = TYPE_NEXT_VARIANT (tem);
1197 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1202 /* If we are not our own variant leader link us into our new leaders
1206 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1207 TYPE_NEXT_VARIANT (mv) = t;
1210 /* Finally adjust our main variant and fix it up. */
1211 TYPE_MAIN_VARIANT (t) = mv;
1212 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1214 /* As the second step of reconstructing the pointer chains put us
1215 on the list of pointers of the new pointed-to type
1216 if we are a main variant. See lto_fixup_common for the first step. */
1217 if (TREE_CODE (t) == POINTER_TYPE
1218 && TYPE_MAIN_VARIANT (t) == t)
1220 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1221 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1223 else if (TREE_CODE (t) == REFERENCE_TYPE
1224 && TYPE_MAIN_VARIANT (t) == t)
1226 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1227 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1231 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1234 lto_fixup_binfo (tree t, void *data)
1236 unsigned HOST_WIDE_INT i, n;
1237 tree base, saved_base;
1239 lto_fixup_common (t, data);
1240 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1241 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1242 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1243 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1244 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1245 for (i = 0; i < n; i++)
1247 saved_base = base = BINFO_BASE_ACCESS (t, i);
1248 LTO_FIXUP_SUBTREE (base);
1249 if (base != saved_base)
1250 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1252 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1253 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1254 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1255 n = BINFO_N_BASE_BINFOS (t);
1256 for (i = 0; i < n; i++)
1258 saved_base = base = BINFO_BASE_BINFO (t, i);
1259 LTO_FIXUP_SUBTREE (base);
1260 if (base != saved_base)
1261 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1265 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1268 lto_fixup_constructor (tree t, void *data)
1270 unsigned HOST_WIDE_INT idx;
1271 constructor_elt *ce;
1273 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1276 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1279 LTO_FIXUP_SUBTREE (ce->index);
1280 LTO_FIXUP_SUBTREE (ce->value);
1284 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1285 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1286 DATA is a pointer set to record visited nodes. */
1289 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1292 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1297 if (!t || pointer_set_contains (fixup_data->seen, t))
1300 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1302 prevailing = lto_symtab_prevailing_decl (t);
1304 if (t != prevailing)
1306 /* Also replace t with prevailing defintion. We don't want to
1307 insert the other defintion in the seen set as we want to
1308 replace all instances of it. */
1313 else if (TYPE_P (t))
1315 /* Replace t with the prevailing type. We don't want to insert the
1316 other type in the seen set as we want to replace all instances of it. */
1317 t = gimple_register_type (t);
1321 if (pointer_set_insert (fixup_data->seen, t))
1324 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1325 Hence we do special processing here for those kind of nodes. */
1326 switch (TREE_CODE (t))
1329 lto_fixup_field_decl (t, data);
1337 lto_fixup_decl_common (t, data);
1341 lto_fixup_decl_with_vis (t, data);
1345 lto_fixup_decl_non_common (t, data);
1349 lto_fixup_function (t, data);
1353 lto_fixup_binfo (t, data);
1358 lto_fixup_type (t, data);
1359 else if (TREE_CODE (t) == CONSTRUCTOR)
1360 lto_fixup_constructor (t, data);
1361 else if (CONSTANT_CLASS_P (t))
1362 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1363 else if (EXPR_P (t))
1365 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1366 lto_fixup_common (t, data);
1367 LTO_FIXUP_SUBTREE (t->exp.block);
1372 /* Let walk_tree handle sub-trees. */
1380 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1381 replaces var and function decls with the corresponding prevailing def and
1382 records the old decl in the free-list in DATA. We also record visted nodes
1383 in the seen-set in DATA to avoid multiple visit for nodes that need not
1387 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1390 struct lto_tree_ref_table *table;
1392 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1393 we still need to walk from all DECLs to find the reachable
1394 FUNCTION_DECLs and VAR_DECLs. */
1395 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1397 table = &state->streams[si];
1398 for (i = 0; i < table->size; i++)
1399 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1403 /* A callback of htab_traverse. Just extract a state from SLOT and the
1404 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1407 lto_fixup_state_aux (void **slot, void *aux)
1409 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1410 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1414 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1418 lto_fixup_decls (struct lto_file_decl_data **files)
1422 struct pointer_set_t *seen = pointer_set_create ();
1423 lto_fixup_data_t data;
1426 for (i = 0; files[i]; i++)
1428 struct lto_file_decl_data *file = files[i];
1429 struct lto_in_decl_state *state = file->global_decl_state;
1430 lto_fixup_state (state, &data);
1432 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1435 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1437 tree saved_decl = decl;
1438 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1439 if (decl != saved_decl)
1440 VEC_replace (tree, lto_global_var_decls, i, decl);
1443 pointer_set_destroy (seen);
1446 /* Read the options saved from each file in the command line. Called
1447 from lang_hooks.post_options which is called by process_options
1448 right before all the options are used to initialize the compiler.
1449 This assumes that decode_options has already run, so the
1450 num_in_fnames and in_fnames are properly set.
1452 Note that this assumes that all the files had been compiled with
1453 the same options, which is not a good assumption. In general,
1454 options ought to be read from all the files in the set and merged.
1455 However, it is still unclear what the merge rules should be. */
1458 lto_read_all_file_options (void)
1462 /* Clear any file options currently saved. */
1463 lto_clear_file_options ();
1465 /* Set the hooks to read ELF sections. */
1466 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1468 for (i = 0; i < num_in_fnames; i++)
1470 struct lto_file_decl_data *file_data;
1471 lto_file *file = lto_obj_file_open (in_fnames[i], false);
1475 file_data = XCNEW (struct lto_file_decl_data);
1476 file_data->file_name = file->filename;
1477 file_data->section_hash_table = lto_obj_build_section_table (file);
1479 lto_read_file_options (file_data);
1481 lto_obj_file_close (file);
1482 htab_delete (file_data->section_hash_table);
1486 /* Apply globally the options read from all the files. */
1487 lto_reissue_options ();
1490 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1492 /* Read all the symbols from the input files FNAMES. NFILES is the
1493 number of files requested in the command line. Instantiate a
1494 global call graph by aggregating all the sub-graphs found in each
1498 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1500 unsigned int i, last_file_ix;
1502 struct cgraph_node *node;
1504 lto_stats.num_input_files = nfiles;
1506 timevar_push (TV_IPA_LTO_DECL_IO);
1508 /* Set the hooks so that all of the ipa passes can read in their data. */
1509 all_file_decl_data = GGC_CNEWVEC (struct lto_file_decl_data *, nfiles + 1);
1510 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1512 /* Read the resolution file. */
1514 if (resolution_file_name)
1517 unsigned num_objects;
1519 resolution = fopen (resolution_file_name, "r");
1520 if (resolution == NULL)
1521 fatal_error ("could not open symbol resolution file: %s",
1524 t = fscanf (resolution, "%u", &num_objects);
1525 gcc_assert (t == 1);
1527 /* True, since the plugin splits the archives. */
1528 gcc_assert (num_objects == nfiles);
1532 fprintf (stderr, "Reading object files:");
1534 /* Read all of the object files specified on the command line. */
1535 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1537 struct lto_file_decl_data *file_data = NULL;
1540 fprintf (stderr, " %s", fnames[i]);
1544 current_lto_file = lto_obj_file_open (fnames[i], false);
1545 if (!current_lto_file)
1548 file_data = lto_file_read (current_lto_file, resolution);
1552 all_file_decl_data[last_file_ix++] = file_data;
1554 lto_obj_file_close (current_lto_file);
1555 current_lto_file = NULL;
1559 if (resolution_file_name)
1560 fclose (resolution);
1562 all_file_decl_data[last_file_ix] = NULL;
1564 /* Set the hooks so that all of the ipa passes can read in their data. */
1565 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1567 timevar_pop (TV_IPA_LTO_DECL_IO);
1570 fprintf (stderr, "\nReading the callgraph\n");
1572 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1573 /* Read the callgraph. */
1575 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1578 fprintf (stderr, "Merging declarations\n");
1580 timevar_push (TV_IPA_LTO_DECL_MERGE);
1581 /* Merge global decls. */
1582 lto_symtab_merge_decls ();
1584 /* Fixup all decls and types and free the type hash tables. */
1585 lto_fixup_decls (all_file_decl_data);
1586 free_gimple_type_tables ();
1589 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1590 /* Each pass will set the appropriate timer. */
1593 fprintf (stderr, "Reading summaries\n");
1595 /* Read the IPA summary data. */
1597 ipa_read_optimization_summaries ();
1599 ipa_read_summaries ();
1601 /* Finally merge the cgraph according to the decl merging decisions. */
1602 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1603 lto_symtab_merge_cgraph_nodes ();
1607 for (node = cgraph_nodes; node; node = node->next)
1609 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1610 summaries computed and needs to apply changes. At the moment WHOPR only
1611 supports inlining, so we can push it here by hand. In future we need to stream
1612 this field into ltrans compilation. */
1614 VEC_safe_push (ipa_opt_pass, heap,
1615 node->ipa_transforms_to_apply,
1616 (ipa_opt_pass)&pass_ipa_inline);
1620 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1622 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1624 /* FIXME lto. This loop needs to be changed to use the pass manager to
1625 call the ipa passes directly. */
1627 for (i = 0; i < last_file_ix; i++)
1629 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1630 lto_materialize_constructors_and_inits (file_data);
1633 /* Indicate that the cgraph is built and ready. */
1634 cgraph_function_flags_ready = true;
1636 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1637 ggc_free (all_file_decl_data);
1638 all_file_decl_data = NULL;
1642 /* Materialize all the bodies for all the nodes in the callgraph. */
1645 materialize_cgraph (void)
1648 struct cgraph_node *node;
1650 timevar_id_t lto_timer;
1654 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1657 /* Now that we have input the cgraph, we need to clear all of the aux
1658 nodes and read the functions if we are not running in WPA mode. */
1659 timevar_push (TV_IPA_LTO_GIMPLE_IO);
1661 for (node = cgraph_nodes; node; node = node->next)
1663 /* Some cgraph nodes get created on the fly, and they don't need
1664 to be materialized. For instance, nodes for nested functions
1665 where the parent function was not streamed out or builtin
1666 functions. Additionally, builtin functions should not be
1667 materialized and may, in fact, cause confusion because there
1668 may be a regular function in the file whose assembler name
1669 matches that of the function.
1670 See gcc.c-torture/execute/20030125-1.c and
1671 gcc.c-torture/execute/921215-1.c. */
1672 if (node->local.lto_file_data
1673 && !DECL_IS_BUILTIN (node->decl))
1675 lto_materialize_function (node);
1676 lto_stats.num_input_cgraph_nodes++;
1680 timevar_pop (TV_IPA_LTO_GIMPLE_IO);
1682 /* Start the appropriate timer depending on the mode that we are
1684 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1685 : (flag_ltrans) ? TV_WHOPR_LTRANS
1687 timevar_push (lto_timer);
1689 current_function_decl = NULL;
1692 /* Inform the middle end about the global variables we have seen. */
1693 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1694 rest_of_decl_compilation (decl, 1, 0);
1697 fprintf (stderr, "\n");
1699 timevar_pop (lto_timer);
1703 /* Perform whole program analysis (WPA) on the callgraph and write out the
1704 optimization plan. */
1707 do_whole_program_analysis (void)
1709 char **output_files;
1711 /* Note that since we are in WPA mode, materialize_cgraph will not
1712 actually read in all the function bodies. It only materializes
1713 the decls and cgraph nodes so that analysis can be performed. */
1714 materialize_cgraph ();
1716 /* Reading in the cgraph uses different timers, start timing WPA now. */
1717 timevar_push (TV_WHOPR_WPA);
1719 if (pre_ipa_mem_report)
1721 fprintf (stderr, "Memory consumption before IPA\n");
1722 dump_memory_report (false);
1725 cgraph_function_flags_ready = true;
1726 bitmap_obstack_initialize (NULL);
1727 ipa_register_cgraph_hooks ();
1728 cgraph_state = CGRAPH_STATE_IPA_SSA;
1730 execute_ipa_pass_list (all_regular_ipa_passes);
1733 bitmap_obstack_release (NULL);
1735 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1736 timevar_pop (TV_WHOPR_WPA);
1742 fprintf (stderr, "\nStreaming out");
1745 output_files = lto_wpa_write_files ();
1748 fprintf (stderr, "\n");
1750 if (post_ipa_mem_report)
1752 fprintf (stderr, "Memory consumption after IPA\n");
1753 dump_memory_report (false);
1756 /* Show the LTO report before launching LTRANS. */
1757 if (flag_lto_report)
1758 print_lto_report ();
1760 lto_write_ltrans_list (output_files);
1762 XDELETEVEC (output_files);
1766 /* Main entry point for the GIMPLE front end. This front end has
1767 three main personalities:
1769 - LTO (-flto). All the object files on the command line are
1770 loaded in memory and processed as a single translation unit.
1771 This is the traditional link-time optimization behavior.
1773 - WPA (-fwpa). Only the callgraph and summary information for
1774 files in the command file are loaded. A single callgraph
1775 (without function bodies) is instantiated for the whole set of
1776 files. IPA passes are only allowed to analyze the call graph
1777 and make transformation decisions. The callgraph is
1778 partitioned, each partition is written to a new object file
1779 together with the transformation decisions.
1781 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
1782 summary files from running again. Since WPA computed summary
1783 information and decided what transformations to apply, LTRANS
1784 simply applies them. */
1787 lto_main (int debug_p ATTRIBUTE_UNUSED)
1791 /* Read all the symbols and call graph from all the files in the
1793 read_cgraph_and_symbols (num_in_fnames, in_fnames);
1797 /* If WPA is enabled analyze the whole call graph and create an
1798 optimization plan. Otherwise, read in all the function
1799 bodies and continue with optimization. */
1801 do_whole_program_analysis ();
1804 materialize_cgraph ();
1806 /* Let the middle end know that we have read and merged all of
1810 /* FIXME lto, if the processes spawned by WPA fail, we miss
1811 the chance to print WPA's report, so WPA will call
1812 print_lto_report before launching LTRANS. If LTRANS was
1813 launched directly by the driver we would not need to do
1815 if (flag_lto_report)
1816 print_lto_report ();
1821 #include "gt-lto-lto.h"