1 /* Top-level LTO routines.
2 Copyright 2009, 2010 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "diagnostic-core.h"
29 #include "libiberty.h"
32 #include "tree-ssa-operands.h"
33 #include "tree-pass.h"
34 #include "langhooks.h"
37 #include "pointer-set.h"
45 #include "lto-streamer.h"
47 /* This needs to be included after config.h. Otherwise, _GNU_SOURCE will not
48 be defined in time to set __USE_GNU in the system headers, and strsignal
49 will not be declared. */
54 /* Handle opening elf files on hosts, such as Windows, that may use
55 text file handling that will break binary access. */
61 static GTY(()) tree first_personality_decl;
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. */
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 (decl);
118 lto_input_function_body (file_data, decl, data);
119 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
120 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
121 lto_stats.num_function_bodies++;
124 lto_free_section_data (file_data, LTO_section_function_body, name,
130 DECL_EXTERNAL (decl) = 1;
132 /* Let the middle end know about the function. */
133 rest_of_decl_compilation (decl, 1, 0);
137 /* Decode the content of memory pointed to by DATA in the the
138 in decl state object STATE. DATA_IN points to a data_in structure for
139 decoding. Return the address after the decoded object in the input. */
141 static const uint32_t *
142 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
143 struct lto_in_decl_state *state)
150 decl = lto_streamer_cache_get (data_in->reader_cache, (int) ix);
151 if (TREE_CODE (decl) != FUNCTION_DECL)
153 gcc_assert (decl == void_type_node);
156 state->fn_decl = decl;
158 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
160 uint32_t size = *data++;
161 tree *decls = ggc_alloc_vec_tree (size);
163 for (j = 0; j < size; j++)
165 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
167 /* Register every type in the global type table. If the
168 type existed already, use the existing type. */
169 if (TYPE_P (decls[j]))
170 decls[j] = gimple_register_type (decls[j]);
173 state->streams[i].size = size;
174 state->streams[i].trees = decls;
182 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
183 RESOLUTIONS is the set of symbols picked by the linker (read from the
184 resolution file when the linker plugin is being used). */
187 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
188 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
190 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
191 const int32_t decl_offset = sizeof (struct lto_decl_header);
192 const int32_t main_offset = decl_offset + header->decl_state_size;
193 const int32_t string_offset = main_offset + header->main_size;
194 struct lto_input_block ib_main;
195 struct data_in *data_in;
197 const uint32_t *data_ptr, *data_end;
198 uint32_t num_decl_states;
200 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
203 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
204 header->string_size, resolutions);
206 /* Read the global declarations and types. */
207 while (ib_main.p < ib_main.len)
209 tree t = lto_input_tree (&ib_main, data_in);
210 gcc_assert (t && ib_main.p <= ib_main.len);
213 /* Read in lto_in_decl_state objects. */
214 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
216 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
217 num_decl_states = *data_ptr++;
219 gcc_assert (num_decl_states > 0);
220 decl_data->global_decl_state = lto_new_in_decl_state ();
221 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
222 decl_data->global_decl_state);
224 /* Read in per-function decl states and enter them in hash table. */
225 decl_data->function_decl_states =
226 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
228 for (i = 1; i < num_decl_states; i++)
230 struct lto_in_decl_state *state = lto_new_in_decl_state ();
233 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
234 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
235 gcc_assert (*slot == NULL);
239 if (data_ptr != data_end)
240 internal_error ("bytecode stream: garbage at the end of symbols section");
242 /* Set the current decl state to be the global state. */
243 decl_data->current_decl_state = decl_data->global_decl_state;
245 lto_data_in_delete (data_in);
248 /* strtoll is not portable. */
250 lto_parse_hex (const char *p) {
252 for (; *p != '\0'; ++p)
257 if (c >= '0' && c <= '9')
259 else if (c >= 'a' && c <= 'f')
261 else if (c >= 'A' && c <= 'F')
264 internal_error ("could not parse hex number");
270 /* Read resolution for file named FILE_NAME. The resolution is read from
271 RESOLUTION. An array with the symbol resolution is returned. The array
272 size is written to SIZE. */
274 static VEC(ld_plugin_symbol_resolution_t,heap) *
275 lto_resolution_read (FILE *resolution, lto_file *file)
277 /* We require that objects in the resolution file are in the same
278 order as the lto1 command line. */
279 unsigned int name_len;
281 unsigned int num_symbols;
283 VEC(ld_plugin_symbol_resolution_t,heap) *ret = NULL;
284 unsigned max_index = 0;
289 name_len = strlen (file->filename);
290 obj_name = XNEWVEC (char, name_len + 1);
291 fscanf (resolution, " "); /* Read white space. */
293 fread (obj_name, sizeof (char), name_len, resolution);
294 obj_name[name_len] = '\0';
295 if (strcmp (obj_name, file->filename) != 0)
296 internal_error ("unexpected file name %s in linker resolution file. "
297 "Expected %s", obj_name, file->filename);
298 if (file->offset != 0)
303 t = fscanf (resolution, "@0x%16s", offset_p);
305 internal_error ("could not parse file offset");
306 offset = lto_parse_hex (offset_p);
307 if (offset != file->offset)
308 internal_error ("unexpected offset");
313 fscanf (resolution, "%u", &num_symbols);
315 for (i = 0; i < num_symbols; i++)
320 enum ld_plugin_symbol_resolution r;
322 unsigned int lto_resolution_str_len =
323 sizeof (lto_resolution_str) / sizeof (char *);
325 t = fscanf (resolution, "%u %26s %*[^\n]\n", &index, r_str);
327 internal_error ("Invalid line in the resolution file.");
328 if (index > max_index)
331 for (j = 0; j < lto_resolution_str_len; j++)
333 if (strcmp (lto_resolution_str[j], r_str) == 0)
335 r = (enum ld_plugin_symbol_resolution) j;
339 if (j == lto_resolution_str_len)
340 internal_error ("Invalid resolution in the resolution file.");
342 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap, ret,
344 VEC_replace (ld_plugin_symbol_resolution_t, ret, index, r);
350 /* Generate a TREE representation for all types and external decls
353 Read all of the globals out of the file. Then read the cgraph
354 and process the .o index into the cgraph nodes so that it can open
355 the .o file to load the functions and ipa information. */
357 static struct lto_file_decl_data *
358 lto_file_read (lto_file *file, FILE *resolution_file)
360 struct lto_file_decl_data *file_data;
363 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions;
365 resolutions = lto_resolution_read (resolution_file, file);
367 file_data = ggc_alloc_lto_file_decl_data ();
368 file_data->file_name = file->filename;
369 file_data->section_hash_table = lto_obj_build_section_table (file);
370 file_data->renaming_hash_table = lto_create_renaming_table ();
372 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
373 lto_read_decls (file_data, data, resolutions);
374 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
379 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
380 #define LTO_MMAP_IO 1
384 /* Page size of machine is used for mmap and munmap calls. */
385 static size_t page_mask;
388 /* Get the section data of length LEN from FILENAME starting at
389 OFFSET. The data segment must be freed by the caller when the
390 caller is finished. Returns NULL if all was not well. */
393 lto_read_section_data (struct lto_file_decl_data *file_data,
394 intptr_t offset, size_t len)
398 static char *fd_name;
400 intptr_t computed_len;
401 intptr_t computed_offset;
405 /* Keep a single-entry file-descriptor cache. The last file we
406 touched will get closed at exit.
407 ??? Eventually we want to add a more sophisticated larger cache
408 or rather fix function body streaming to not stream them in
409 practically random order. */
411 && strcmp (fd_name, file_data->file_name) != 0)
419 fd_name = xstrdup (file_data->file_name);
420 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
428 size_t page_size = sysconf (_SC_PAGE_SIZE);
429 page_mask = ~(page_size - 1);
432 computed_offset = offset & page_mask;
433 diff = offset - computed_offset;
434 computed_len = len + diff;
436 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
437 fd, computed_offset);
438 if (result == MAP_FAILED)
441 return result + diff;
443 result = (char *) xmalloc (len);
444 if (lseek (fd, offset, SEEK_SET) != offset
445 || read (fd, result, len) != (ssize_t) len)
456 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
457 NAME will be NULL unless the section type is for a function
461 get_section_data (struct lto_file_decl_data *file_data,
462 enum lto_section_type section_type,
466 htab_t section_hash_table = file_data->section_hash_table;
467 struct lto_section_slot *f_slot;
468 struct lto_section_slot s_slot;
469 const char *section_name = lto_get_section_name (section_type, name);
473 s_slot.name = section_name;
474 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
477 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
481 free (CONST_CAST (char *, section_name));
486 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
487 starts at OFFSET and has LEN bytes. */
490 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
491 enum lto_section_type section_type ATTRIBUTE_UNUSED,
492 const char *name ATTRIBUTE_UNUSED,
493 const char *offset, size_t len ATTRIBUTE_UNUSED)
496 intptr_t computed_len;
497 intptr_t computed_offset;
502 computed_offset = ((intptr_t) offset) & page_mask;
503 diff = (intptr_t) offset - computed_offset;
504 computed_len = len + diff;
506 munmap ((caddr_t) computed_offset, computed_len);
508 free (CONST_CAST(char *, offset));
512 /* Structure describing ltrans partitions. */
514 struct GTY (()) ltrans_partition_def
516 cgraph_node_set cgraph_set;
517 varpool_node_set varpool_set;
518 const char * GTY ((skip)) name;
522 typedef struct ltrans_partition_def *ltrans_partition;
523 DEF_VEC_P(ltrans_partition);
524 DEF_VEC_ALLOC_P(ltrans_partition,gc);
526 static GTY (()) VEC(ltrans_partition, gc) *ltrans_partitions;
528 /* Create new partition with name NAME. */
529 static ltrans_partition
530 new_partition (const char *name)
532 ltrans_partition part = ggc_alloc_ltrans_partition_def ();
533 part->cgraph_set = cgraph_node_set_new ();
534 part->varpool_set = varpool_node_set_new ();
537 VEC_safe_push (ltrans_partition, gc, ltrans_partitions, part);
541 /* Add NODE to partition as well as the inline callees into partition PART. */
544 add_cgraph_node_to_partition (ltrans_partition part, struct cgraph_node *node)
546 struct cgraph_edge *e;
547 part->insns += node->local.inline_summary.self_size;
548 cgraph_node_set_add (part->cgraph_set, node);
549 for (e = node->callees; e; e = e->next_callee)
550 if (!e->inline_failed)
551 add_cgraph_node_to_partition (part, e->callee);
554 /* Group cgrah nodes by input files. This is used mainly for testing
558 lto_1_to_1_map (void)
560 struct cgraph_node *node;
561 struct varpool_node *vnode;
562 struct lto_file_decl_data *file_data;
563 struct pointer_map_t *pmap;
564 ltrans_partition partition;
568 timevar_push (TV_WHOPR_WPA);
570 pmap = pointer_map_create ();
572 for (node = cgraph_nodes; node; node = node->next)
574 /* We will get proper partition based on function they are inlined to. */
575 if (node->global.inlined_to)
577 /* Nodes without a body do not need partitioning. */
581 file_data = node->local.lto_file_data;
582 gcc_assert (!node->same_body_alias && file_data);
584 slot = pointer_map_contains (pmap, file_data);
586 partition = (ltrans_partition) *slot;
589 partition = new_partition (file_data->file_name);
590 slot = pointer_map_insert (pmap, file_data);
595 add_cgraph_node_to_partition (partition, node);
598 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
600 if (vnode->alias || !vnode->needed)
602 file_data = vnode->lto_file_data;
603 slot = pointer_map_contains (pmap, file_data);
605 partition = (ltrans_partition) *slot;
608 partition = new_partition (file_data->file_name);
609 slot = pointer_map_insert (pmap, file_data);
614 varpool_node_set_add (partition->varpool_set, vnode);
617 /* If the cgraph is empty, create one cgraph node set so that there is still
618 an output file for any variables that need to be exported in a DSO. */
620 new_partition ("empty");
622 pointer_map_destroy (pmap);
624 timevar_pop (TV_WHOPR_WPA);
626 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
630 /* Promote variable VNODE to be static. */
633 promote_var (struct varpool_node *vnode)
635 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
637 gcc_assert (flag_wpa);
638 TREE_PUBLIC (vnode->decl) = 1;
639 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
640 if (cgraph_dump_file)
641 fprintf (cgraph_dump_file,
642 "Promoting var as hidden: %s\n", varpool_node_name (vnode));
646 /* Promote function NODE to be static. */
649 promote_fn (struct cgraph_node *node)
651 gcc_assert (flag_wpa);
652 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
654 TREE_PUBLIC (node->decl) = 1;
655 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
658 struct cgraph_node *alias;
659 for (alias = node->same_body;
660 alias; alias = alias->next)
662 TREE_PUBLIC (alias->decl) = 1;
663 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
666 if (cgraph_dump_file)
667 fprintf (cgraph_dump_file,
668 "Promoting function as hidden: %s/%i\n",
669 cgraph_node_name (node), node->uid);
673 /* Find out all static decls that need to be promoted to global because
674 of cross file sharing. This function must be run in the WPA mode after
675 all inlinees are added. */
678 lto_promote_cross_file_statics (void)
680 struct varpool_node *vnode;
683 varpool_node_set vset;
684 cgraph_node_set_iterator csi;
685 varpool_node_set_iterator vsi;
686 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
687 struct pointer_set_t *inserted = pointer_set_create ();
689 gcc_assert (flag_wpa);
691 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
692 for (i = 0; i < n_sets; i++)
694 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
695 set = part->cgraph_set;
696 vset = part->varpool_set;
698 /* If node has either address taken (and we have no clue from where)
699 or it is called from other partition, it needs to be globalized. */
700 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
702 struct cgraph_node *node = csi_node (csi);
703 if (node->local.externally_visible)
705 if (node->global.inlined_to)
707 if (!DECL_EXTERNAL (node->decl)
708 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
709 || reachable_from_other_partition_p (node, set)))
712 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
714 vnode = vsi_node (vsi);
715 /* Constant pool references use internal labels and thus can not
716 be made global. It is sensible to keep those ltrans local to
717 allow better optimization. */
718 if (!DECL_IN_CONSTANT_POOL (vnode->decl)
719 && !vnode->externally_visible && vnode->analyzed
720 && referenced_from_other_partition_p (&vnode->ref_list,
725 /* We export initializers of read-only var into each partition
726 referencing it. Folding might take declarations from the
727 initializers and use it; so everything referenced from the
728 initializers needs can be accessed from this partition after
731 This means that we need to promote all variables and functions
732 referenced from all initializers from readonly vars referenced
733 from this partition that are not in this partition.
734 This needs to be done recursively. */
735 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
736 if ((TREE_READONLY (vnode->decl) || DECL_IN_CONSTANT_POOL (vnode->decl))
737 && DECL_INITIAL (vnode->decl)
738 && !varpool_node_in_set_p (vnode, vset)
739 && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
740 && !pointer_set_insert (inserted, vnode))
741 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
742 while (!VEC_empty (varpool_node_ptr, promoted_initializers))
747 vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
748 for (i = 0; ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref); i++)
750 if (ref->refered_type == IPA_REF_CGRAPH)
752 struct cgraph_node *n = ipa_ref_node (ref);
753 gcc_assert (!n->global.inlined_to);
754 if (!n->local.externally_visible
755 && !cgraph_node_in_set_p (n, set))
760 struct varpool_node *v = ipa_ref_varpool_node (ref);
761 if (varpool_node_in_set_p (v, vset))
763 /* Constant pool references use internal labels and thus can not
764 be made global. It is sensible to keep those ltrans local to
765 allow better optimization. */
766 if (DECL_IN_CONSTANT_POOL (v->decl))
768 if (!pointer_set_insert (inserted, vnode))
769 VEC_safe_push (varpool_node_ptr, heap,
770 promoted_initializers, v);
772 else if (!DECL_IN_CONSTANT_POOL (v->decl)
773 && !v->externally_visible && v->analyzed)
776 && DECL_INITIAL (v->decl) && TREE_READONLY (v->decl)
777 && !pointer_set_insert (inserted, vnode))
778 VEC_safe_push (varpool_node_ptr, heap,
779 promoted_initializers, v);
785 pointer_set_destroy (inserted);
788 static lto_file *current_lto_file;
790 /* Helper for qsort; compare partitions and return one with smaller size.
791 We sort from greatest to smallest so parallel build doesn't stale on the
792 longest compilation being executed too late. */
795 cmp_partitions (const void *a, const void *b)
797 const struct ltrans_partition_def *pa
798 = *(struct ltrans_partition_def *const *)a;
799 const struct ltrans_partition_def *pb
800 = *(struct ltrans_partition_def *const *)b;
801 return pb->insns - pa->insns;
804 /* Write all output files in WPA mode and the file with the list of
808 lto_wpa_write_files (void)
813 varpool_node_set vset;
814 ltrans_partition part;
815 FILE *ltrans_output_list_stream;
819 /* Open the LTRANS output list. */
820 if (!ltrans_output_list)
821 fatal_error ("no LTRANS output list filename provided");
822 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
823 if (ltrans_output_list_stream == NULL)
824 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
826 timevar_push (TV_WHOPR_WPA);
828 /* Include all inlined functions and determine what sets need to be
829 compiled by LTRANS. After this loop, only those sets that
830 contain callgraph nodes from more than one file will need to be
831 compiled by LTRANS. */
832 for (i = 0; VEC_iterate (ltrans_partition, ltrans_partitions, i, part); i++)
833 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
834 part->cgraph_set->nodes);
836 /* After adding all inlinees, find out statics that need to be promoted
837 to globals because of cross-file inlining. */
838 lto_promote_cross_file_statics ();
840 timevar_pop (TV_WHOPR_WPA);
842 timevar_push (TV_WHOPR_WPA_IO);
844 /* Generate a prefix for the LTRANS unit files. */
845 blen = strlen (ltrans_output_list);
846 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
847 strcpy (temp_filename, ltrans_output_list);
848 if (blen > sizeof (".out")
849 && strcmp (temp_filename + blen - sizeof (".out") + 1,
851 temp_filename[blen - sizeof (".out") + 1] = '\0';
852 blen = strlen (temp_filename);
854 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
855 qsort (VEC_address (ltrans_partition, ltrans_partitions), n_sets,
856 sizeof (ltrans_partition), cmp_partitions);
857 for (i = 0; i < n_sets; i++)
860 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
862 set = part->cgraph_set;
863 vset = part->varpool_set;
865 /* Write all the nodes in SET. */
866 sprintf (temp_filename + blen, "%u.o", i);
867 file = lto_obj_file_open (temp_filename, true);
869 fatal_error ("lto_obj_file_open() failed");
872 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
873 if (cgraph_dump_file)
875 fprintf (cgraph_dump_file, "Writting partition %s to file %s, %i insns\n",
876 part->name, temp_filename, part->insns);
877 fprintf (cgraph_dump_file, "cgraph nodes:");
878 dump_cgraph_node_set (cgraph_dump_file, set);
879 fprintf (cgraph_dump_file, "varpool nodes:");
880 dump_varpool_node_set (cgraph_dump_file, vset);
882 gcc_assert (cgraph_node_set_nonempty_p (set)
883 || varpool_node_set_nonempty_p (vset) || !i);
885 lto_set_current_out_file (file);
887 ipa_write_optimization_summaries (set, vset);
889 lto_set_current_out_file (NULL);
890 lto_obj_file_close (file);
892 len = strlen (temp_filename);
893 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
894 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
895 fatal_error ("writing to LTRANS output list %s: %m",
899 lto_stats.num_output_files += n_sets;
901 /* Close the LTRANS output list. */
902 if (fclose (ltrans_output_list_stream))
903 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
905 timevar_pop (TV_WHOPR_WPA_IO);
910 struct pointer_set_t *seen;
913 #define LTO_FIXUP_SUBTREE(t) \
915 walk_tree (&(t), lto_fixup_tree, data, NULL); \
918 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
922 (t) = gimple_register_type (t); \
923 walk_tree (&(t), lto_fixup_tree, data, NULL); \
927 static tree lto_fixup_tree (tree *, int *, void *);
929 /* Return true if T does not need to be fixed up recursively. */
935 || CONSTANT_CLASS_P (t)
936 || TREE_CODE (t) == IDENTIFIER_NODE);
939 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
942 lto_fixup_common (tree t, void *data)
944 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
945 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
946 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
947 First remove us from any pointer list we are on. */
948 if (TREE_CODE (t) == POINTER_TYPE)
950 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
951 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
954 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
955 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
956 tem = TYPE_NEXT_PTR_TO (tem);
958 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
960 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
962 else if (TREE_CODE (t) == REFERENCE_TYPE)
964 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
965 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
968 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
969 while (tem && TYPE_NEXT_REF_TO (tem) != t)
970 tem = TYPE_NEXT_REF_TO (tem);
972 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
974 TYPE_NEXT_REF_TO (t) = NULL_TREE;
977 /* Fixup our type. */
978 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
980 /* Second put us on the list of pointers of the new pointed-to type
981 if we are a main variant. This is done in lto_fixup_type after
982 fixing up our main variant. */
984 /* This is not very efficient because we cannot do tail-recursion with
985 a long chain of trees. */
986 LTO_FIXUP_SUBTREE (TREE_CHAIN (t));
989 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
992 lto_fixup_decl_minimal (tree t, void *data)
994 lto_fixup_common (t, data);
995 LTO_FIXUP_SUBTREE (DECL_NAME (t));
996 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t));
999 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1002 lto_fixup_decl_common (tree t, void *data)
1004 lto_fixup_decl_minimal (t, data);
1005 LTO_FIXUP_SUBTREE (DECL_SIZE (t));
1006 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t));
1007 LTO_FIXUP_SUBTREE (DECL_INITIAL (t));
1008 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t));
1009 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t));
1012 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1015 lto_fixup_decl_with_vis (tree t, void *data)
1017 lto_fixup_decl_common (t, data);
1019 /* Accessor macro has side-effects, use field-name here. */
1020 LTO_FIXUP_SUBTREE (t->decl_with_vis.assembler_name);
1022 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t)));
1025 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1028 lto_fixup_decl_non_common (tree t, void *data)
1030 lto_fixup_decl_with_vis (t, data);
1031 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t));
1032 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t));
1033 LTO_FIXUP_SUBTREE (DECL_VINDEX (t));
1035 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1036 gcc_assert (no_fixup_p (t->decl_non_common.saved_tree));
1039 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1042 lto_fixup_function (tree t, void *data)
1044 lto_fixup_decl_non_common (t, data);
1045 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t));
1048 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1051 lto_fixup_field_decl (tree t, void *data)
1053 lto_fixup_decl_common (t, data);
1054 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t));
1055 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t));
1056 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t));
1057 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t)));
1058 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t));
1061 /* Fix up fields of a type T. DATA points to fix-up states. */
1064 lto_fixup_type (tree t, void *data)
1068 lto_fixup_common (t, data);
1069 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t));
1070 LTO_FIXUP_SUBTREE (TYPE_SIZE (t));
1071 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t));
1072 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t));
1073 LTO_FIXUP_SUBTREE (TYPE_NAME (t));
1075 /* Accessors are for derived node types only. */
1076 if (!POINTER_TYPE_P (t))
1077 LTO_FIXUP_SUBTREE (t->type.minval);
1078 LTO_FIXUP_SUBTREE (t->type.maxval);
1080 /* Accessor is for derived node types only. */
1081 LTO_FIXUP_SUBTREE (t->type.binfo);
1083 if (TYPE_CONTEXT (t))
1085 if (TYPE_P (TYPE_CONTEXT (t)))
1086 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1088 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1090 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CANONICAL (t));
1092 /* The following re-creates proper variant lists while fixing up
1093 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1094 variant list state before fixup is broken. */
1096 /* Remove us from our main variant list if we are not the variant leader. */
1097 if (TYPE_MAIN_VARIANT (t) != t)
1099 tem = TYPE_MAIN_VARIANT (t);
1100 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1101 tem = TYPE_NEXT_VARIANT (tem);
1103 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1104 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1107 /* Query our new main variant. */
1108 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1110 /* If we were the variant leader and we get replaced ourselves drop
1111 all variants from our list. */
1112 if (TYPE_MAIN_VARIANT (t) == t
1118 tree tem2 = TYPE_NEXT_VARIANT (tem);
1119 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1124 /* If we are not our own variant leader link us into our new leaders
1128 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1129 TYPE_NEXT_VARIANT (mv) = t;
1132 /* Finally adjust our main variant and fix it up. */
1133 TYPE_MAIN_VARIANT (t) = mv;
1134 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1136 /* As the second step of reconstructing the pointer chains put us
1137 on the list of pointers of the new pointed-to type
1138 if we are a main variant. See lto_fixup_common for the first step. */
1139 if (TREE_CODE (t) == POINTER_TYPE
1140 && TYPE_MAIN_VARIANT (t) == t)
1142 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1143 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1145 else if (TREE_CODE (t) == REFERENCE_TYPE
1146 && TYPE_MAIN_VARIANT (t) == t)
1148 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1149 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1153 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1156 lto_fixup_binfo (tree t, void *data)
1158 unsigned HOST_WIDE_INT i, n;
1159 tree base, saved_base;
1161 lto_fixup_common (t, data);
1162 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1163 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1164 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1165 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1166 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1167 for (i = 0; i < n; i++)
1169 saved_base = base = BINFO_BASE_ACCESS (t, i);
1170 LTO_FIXUP_SUBTREE (base);
1171 if (base != saved_base)
1172 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1174 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1175 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1176 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1177 n = BINFO_N_BASE_BINFOS (t);
1178 for (i = 0; i < n; i++)
1180 saved_base = base = BINFO_BASE_BINFO (t, i);
1181 LTO_FIXUP_SUBTREE (base);
1182 if (base != saved_base)
1183 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1187 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1190 lto_fixup_constructor (tree t, void *data)
1192 unsigned HOST_WIDE_INT idx;
1193 constructor_elt *ce;
1195 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1198 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1201 LTO_FIXUP_SUBTREE (ce->index);
1202 LTO_FIXUP_SUBTREE (ce->value);
1206 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1207 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1208 DATA is a pointer set to record visited nodes. */
1211 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1214 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1219 if (!t || pointer_set_contains (fixup_data->seen, t))
1222 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1224 prevailing = lto_symtab_prevailing_decl (t);
1226 if (t != prevailing)
1228 /* Also replace t with prevailing defintion. We don't want to
1229 insert the other defintion in the seen set as we want to
1230 replace all instances of it. */
1235 else if (TYPE_P (t))
1237 /* Replace t with the prevailing type. We don't want to insert the
1238 other type in the seen set as we want to replace all instances of it. */
1239 t = gimple_register_type (t);
1243 if (pointer_set_insert (fixup_data->seen, t))
1246 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1247 Hence we do special processing here for those kind of nodes. */
1248 switch (TREE_CODE (t))
1251 lto_fixup_field_decl (t, data);
1259 lto_fixup_decl_common (t, data);
1263 lto_fixup_decl_with_vis (t, data);
1267 lto_fixup_decl_non_common (t, data);
1271 lto_fixup_function (t, data);
1275 lto_fixup_binfo (t, data);
1280 lto_fixup_type (t, data);
1281 else if (TREE_CODE (t) == CONSTRUCTOR)
1282 lto_fixup_constructor (t, data);
1283 else if (CONSTANT_CLASS_P (t))
1284 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1285 else if (EXPR_P (t))
1287 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1288 lto_fixup_common (t, data);
1289 LTO_FIXUP_SUBTREE (t->exp.block);
1294 /* Let walk_tree handle sub-trees. */
1302 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1303 replaces var and function decls with the corresponding prevailing def and
1304 records the old decl in the free-list in DATA. We also record visted nodes
1305 in the seen-set in DATA to avoid multiple visit for nodes that need not
1309 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1312 struct lto_tree_ref_table *table;
1314 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1315 we still need to walk from all DECLs to find the reachable
1316 FUNCTION_DECLs and VAR_DECLs. */
1317 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1319 table = &state->streams[si];
1320 for (i = 0; i < table->size; i++)
1321 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1325 /* A callback of htab_traverse. Just extract a state from SLOT and the
1326 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1329 lto_fixup_state_aux (void **slot, void *aux)
1331 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1332 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1336 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1340 lto_fixup_decls (struct lto_file_decl_data **files)
1344 struct pointer_set_t *seen = pointer_set_create ();
1345 lto_fixup_data_t data;
1348 for (i = 0; files[i]; i++)
1350 struct lto_file_decl_data *file = files[i];
1351 struct lto_in_decl_state *state = file->global_decl_state;
1352 lto_fixup_state (state, &data);
1354 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1357 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1359 tree saved_decl = decl;
1360 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1361 if (decl != saved_decl)
1362 VEC_replace (tree, lto_global_var_decls, i, decl);
1365 pointer_set_destroy (seen);
1368 /* Read the options saved from each file in the command line. Called
1369 from lang_hooks.post_options which is called by process_options
1370 right before all the options are used to initialize the compiler.
1371 This assumes that decode_options has already run, so the
1372 num_in_fnames and in_fnames are properly set.
1374 Note that this assumes that all the files had been compiled with
1375 the same options, which is not a good assumption. In general,
1376 options ought to be read from all the files in the set and merged.
1377 However, it is still unclear what the merge rules should be. */
1380 lto_read_all_file_options (void)
1384 /* Clear any file options currently saved. */
1385 lto_clear_file_options ();
1387 /* Set the hooks to read ELF sections. */
1388 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1390 fprintf (stderr, "Reading command line options:");
1392 for (i = 0; i < num_in_fnames; i++)
1394 struct lto_file_decl_data *file_data;
1395 lto_file *file = lto_obj_file_open (in_fnames[i], false);
1400 fprintf (stderr, " %s", in_fnames[i]);
1404 file_data = XCNEW (struct lto_file_decl_data);
1405 file_data->file_name = file->filename;
1406 file_data->section_hash_table = lto_obj_build_section_table (file);
1408 lto_read_file_options (file_data);
1410 lto_obj_file_close (file);
1411 htab_delete (file_data->section_hash_table);
1415 /* Apply globally the options read from all the files. */
1416 lto_reissue_options ();
1419 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1421 /* Read all the symbols from the input files FNAMES. NFILES is the
1422 number of files requested in the command line. Instantiate a
1423 global call graph by aggregating all the sub-graphs found in each
1427 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1429 unsigned int i, last_file_ix;
1431 struct cgraph_node *node;
1433 lto_stats.num_input_files = nfiles;
1435 timevar_push (TV_IPA_LTO_DECL_IO);
1437 /* Set the hooks so that all of the ipa passes can read in their data. */
1439 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
1440 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1442 /* Read the resolution file. */
1444 if (resolution_file_name)
1447 unsigned num_objects;
1449 resolution = fopen (resolution_file_name, "r");
1450 if (resolution == NULL)
1451 fatal_error ("could not open symbol resolution file: %m");
1453 t = fscanf (resolution, "%u", &num_objects);
1454 gcc_assert (t == 1);
1456 /* True, since the plugin splits the archives. */
1457 gcc_assert (num_objects == nfiles);
1461 fprintf (stderr, "Reading object files:");
1463 /* Read all of the object files specified on the command line. */
1464 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1466 struct lto_file_decl_data *file_data = NULL;
1469 fprintf (stderr, " %s", fnames[i]);
1473 current_lto_file = lto_obj_file_open (fnames[i], false);
1474 if (!current_lto_file)
1477 file_data = lto_file_read (current_lto_file, resolution);
1481 all_file_decl_data[last_file_ix++] = file_data;
1483 lto_obj_file_close (current_lto_file);
1484 current_lto_file = NULL;
1485 /* ??? We'd want but can't ggc_collect () here as the type merging
1486 code in gimple.c uses hashtables that are not ggc aware. */
1489 if (resolution_file_name)
1490 fclose (resolution);
1492 all_file_decl_data[last_file_ix] = NULL;
1494 /* Set the hooks so that all of the ipa passes can read in their data. */
1495 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1497 timevar_pop (TV_IPA_LTO_DECL_IO);
1500 fprintf (stderr, "\nReading the callgraph\n");
1502 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1503 /* Read the callgraph. */
1505 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1508 fprintf (stderr, "Merging declarations\n");
1510 timevar_push (TV_IPA_LTO_DECL_MERGE);
1511 /* Merge global decls. */
1512 lto_symtab_merge_decls ();
1514 /* Fixup all decls and types and free the type hash tables. */
1515 lto_fixup_decls (all_file_decl_data);
1516 free_gimple_type_tables ();
1519 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1520 /* Each pass will set the appropriate timer. */
1523 fprintf (stderr, "Reading summaries\n");
1525 /* Read the IPA summary data. */
1527 ipa_read_optimization_summaries ();
1529 ipa_read_summaries ();
1531 /* Finally merge the cgraph according to the decl merging decisions. */
1532 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1533 lto_symtab_merge_cgraph_nodes ();
1537 for (node = cgraph_nodes; node; node = node->next)
1539 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1540 summaries computed and needs to apply changes. At the moment WHOPR only
1541 supports inlining, so we can push it here by hand. In future we need to stream
1542 this field into ltrans compilation. */
1544 VEC_safe_push (ipa_opt_pass, heap,
1545 node->ipa_transforms_to_apply,
1546 (ipa_opt_pass)&pass_ipa_inline);
1550 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1552 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1554 /* FIXME lto. This loop needs to be changed to use the pass manager to
1555 call the ipa passes directly. */
1557 for (i = 0; i < last_file_ix; i++)
1559 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1560 lto_materialize_constructors_and_inits (file_data);
1563 /* Indicate that the cgraph is built and ready. */
1564 cgraph_function_flags_ready = true;
1566 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1567 ggc_free (all_file_decl_data);
1568 all_file_decl_data = NULL;
1572 /* Materialize all the bodies for all the nodes in the callgraph. */
1575 materialize_cgraph (void)
1578 struct cgraph_node *node;
1580 timevar_id_t lto_timer;
1584 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1587 /* Now that we have input the cgraph, we need to clear all of the aux
1588 nodes and read the functions if we are not running in WPA mode. */
1589 timevar_push (TV_IPA_LTO_GIMPLE_IO);
1591 for (node = cgraph_nodes; node; node = node->next)
1593 /* Some cgraph nodes get created on the fly, and they don't need
1594 to be materialized. For instance, nodes for nested functions
1595 where the parent function was not streamed out or builtin
1596 functions. Additionally, builtin functions should not be
1597 materialized and may, in fact, cause confusion because there
1598 may be a regular function in the file whose assembler name
1599 matches that of the function.
1600 See gcc.c-torture/execute/20030125-1.c and
1601 gcc.c-torture/execute/921215-1.c. */
1602 if (node->local.lto_file_data
1603 && !DECL_IS_BUILTIN (node->decl))
1605 lto_materialize_function (node);
1606 lto_stats.num_input_cgraph_nodes++;
1610 timevar_pop (TV_IPA_LTO_GIMPLE_IO);
1612 /* Start the appropriate timer depending on the mode that we are
1614 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1615 : (flag_ltrans) ? TV_WHOPR_LTRANS
1617 timevar_push (lto_timer);
1619 current_function_decl = NULL;
1622 /* Inform the middle end about the global variables we have seen. */
1623 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1624 rest_of_decl_compilation (decl, 1, 0);
1627 fprintf (stderr, "\n");
1629 timevar_pop (lto_timer);
1633 /* Perform whole program analysis (WPA) on the callgraph and write out the
1634 optimization plan. */
1637 do_whole_program_analysis (void)
1639 /* Note that since we are in WPA mode, materialize_cgraph will not
1640 actually read in all the function bodies. It only materializes
1641 the decls and cgraph nodes so that analysis can be performed. */
1642 materialize_cgraph ();
1644 /* Reading in the cgraph uses different timers, start timing WPA now. */
1645 timevar_push (TV_WHOPR_WPA);
1647 if (pre_ipa_mem_report)
1649 fprintf (stderr, "Memory consumption before IPA\n");
1650 dump_memory_report (false);
1653 if (cgraph_dump_file)
1655 dump_cgraph (cgraph_dump_file);
1656 dump_varpool (cgraph_dump_file);
1659 cgraph_function_flags_ready = true;
1660 bitmap_obstack_initialize (NULL);
1661 ipa_register_cgraph_hooks ();
1662 cgraph_state = CGRAPH_STATE_IPA_SSA;
1664 execute_ipa_pass_list (all_regular_ipa_passes);
1666 if (cgraph_dump_file)
1668 fprintf (cgraph_dump_file, "Optimized ");
1669 dump_cgraph (cgraph_dump_file);
1670 dump_varpool (cgraph_dump_file);
1673 bitmap_obstack_release (NULL);
1675 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1676 timevar_pop (TV_WHOPR_WPA);
1682 fprintf (stderr, "\nStreaming out");
1685 lto_wpa_write_files ();
1688 fprintf (stderr, "\n");
1690 if (post_ipa_mem_report)
1692 fprintf (stderr, "Memory consumption after IPA\n");
1693 dump_memory_report (false);
1696 /* Show the LTO report before launching LTRANS. */
1697 if (flag_lto_report)
1698 print_lto_report ();
1702 static GTY(()) tree lto_eh_personality_decl;
1704 /* Return the LTO personality function decl. */
1707 lto_eh_personality (void)
1709 if (!lto_eh_personality_decl)
1711 /* Use the first personality DECL for our personality if we don't
1712 support multiple ones. This ensures that we don't artificially
1713 create the need for them in a single-language program. */
1714 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
1715 lto_eh_personality_decl = first_personality_decl;
1717 lto_eh_personality_decl = lhd_gcc_personality ();
1720 return lto_eh_personality_decl;
1724 /* Main entry point for the GIMPLE front end. This front end has
1725 three main personalities:
1727 - LTO (-flto). All the object files on the command line are
1728 loaded in memory and processed as a single translation unit.
1729 This is the traditional link-time optimization behavior.
1731 - WPA (-fwpa). Only the callgraph and summary information for
1732 files in the command file are loaded. A single callgraph
1733 (without function bodies) is instantiated for the whole set of
1734 files. IPA passes are only allowed to analyze the call graph
1735 and make transformation decisions. The callgraph is
1736 partitioned, each partition is written to a new object file
1737 together with the transformation decisions.
1739 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
1740 summary files from running again. Since WPA computed summary
1741 information and decided what transformations to apply, LTRANS
1742 simply applies them. */
1745 lto_main (int debug_p ATTRIBUTE_UNUSED)
1749 /* Read all the symbols and call graph from all the files in the
1751 read_cgraph_and_symbols (num_in_fnames, in_fnames);
1755 /* If WPA is enabled analyze the whole call graph and create an
1756 optimization plan. Otherwise, read in all the function
1757 bodies and continue with optimization. */
1759 do_whole_program_analysis ();
1762 materialize_cgraph ();
1764 /* Let the middle end know that we have read and merged all of
1768 /* FIXME lto, if the processes spawned by WPA fail, we miss
1769 the chance to print WPA's report, so WPA will call
1770 print_lto_report before launching LTRANS. If LTRANS was
1771 launched directly by the driver we would not need to do
1773 if (flag_lto_report)
1774 print_lto_report ();
1779 #include "gt-lto-lto.h"