Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / lto / lto.c
blobd969a1042d7803f9271e8c2f123f813c41640cfa
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
10 version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "toplev.h"
26 #include "tree.h"
27 #include "diagnostic-core.h"
28 #include "tm.h"
29 #include "libiberty.h"
30 #include "cgraph.h"
31 #include "ggc.h"
32 #include "tree-ssa-operands.h"
33 #include "tree-pass.h"
34 #include "langhooks.h"
35 #include "vec.h"
36 #include "bitmap.h"
37 #include "pointer-set.h"
38 #include "ipa-prop.h"
39 #include "common.h"
40 #include "debug.h"
41 #include "timevar.h"
42 #include "gimple.h"
43 #include "lto.h"
44 #include "lto-tree.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. */
50 #if HAVE_MMAP_FILE
51 #include <sys/mman.h>
52 #endif
54 /* Handle opening elf files on hosts, such as Windows, that may use
55 text file handling that will break binary access. */
57 #ifndef O_BINARY
58 # define O_BINARY 0
59 #endif
61 static GTY(()) tree first_personality_decl;
64 /* Read the constructors and inits. */
66 static void
67 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
69 size_t len;
70 const char *data = lto_get_section_data (file_data,
71 LTO_section_static_initializer,
72 NULL, &len);
73 lto_input_constructors_and_inits (file_data, data);
74 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
75 data, len);
78 /* Read the function body for the function associated with NODE. */
80 static void
81 lto_materialize_function (struct cgraph_node *node)
83 tree decl;
84 struct lto_file_decl_data *file_data;
85 const char *data, *name;
86 size_t len;
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
90 decisions. */
91 if (node->clone_of)
92 return;
94 decl = node->decl;
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,
102 name, &len);
103 if (data)
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. */
114 if (!flag_wpa)
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,
125 data, len);
126 if (!flag_wpa)
127 ggc_collect ();
129 else
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)
145 uint32_t ix;
146 tree decl;
147 uint32_t i, j;
149 ix = *data++;
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);
154 decl = NULL_TREE;
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;
175 data += size;
178 return data;
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). */
186 static void
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;
196 unsigned int i;
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,
201 header->main_size);
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);
215 data_end =
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 ();
231 void **slot;
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);
236 *slot = state;
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. */
249 int64_t
250 lto_parse_hex (const char *p) {
251 uint64_t ret = 0;
252 for (; *p != '\0'; ++p)
254 char c = *p;
255 unsigned char part;
256 ret <<= 4;
257 if (c >= '0' && c <= '9')
258 part = c - '0';
259 else if (c >= 'a' && c <= 'f')
260 part = c - 'a' + 10;
261 else if (c >= 'A' && c <= 'F')
262 part = c - 'A' + 10;
263 else
264 internal_error ("could not parse hex number");
265 ret |= part;
267 return ret;
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;
280 char *obj_name;
281 unsigned int num_symbols;
282 unsigned int i;
283 VEC(ld_plugin_symbol_resolution_t,heap) *ret = NULL;
284 unsigned max_index = 0;
286 if (!resolution)
287 return NULL;
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)
300 int t;
301 char offset_p[17];
302 int64_t offset;
303 t = fscanf (resolution, "@0x%16s", offset_p);
304 if (t != 1)
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");
311 free (obj_name);
313 fscanf (resolution, "%u", &num_symbols);
315 for (i = 0; i < num_symbols; i++)
317 int t;
318 unsigned index;
319 char r_str[27];
320 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
321 unsigned int j;
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);
326 if (t != 2)
327 internal_error ("Invalid line in the resolution file.");
328 if (index > max_index)
329 max_index = 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;
336 break;
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,
343 max_index + 1);
344 VEC_replace (ld_plugin_symbol_resolution_t, ret, index, r);
347 return ret;
350 /* Generate a TREE representation for all types and external decls
351 entities in FILE.
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;
361 const char *data;
362 size_t len;
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);
376 return file_data;
379 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
380 #define LTO_MMAP_IO 1
381 #endif
383 #if LTO_MMAP_IO
384 /* Page size of machine is used for mmap and munmap calls. */
385 static size_t page_mask;
386 #endif
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. */
392 static char *
393 lto_read_section_data (struct lto_file_decl_data *file_data,
394 intptr_t offset, size_t len)
396 char *result;
397 static int fd = -1;
398 static char *fd_name;
399 #if LTO_MMAP_IO
400 intptr_t computed_len;
401 intptr_t computed_offset;
402 intptr_t diff;
403 #endif
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. */
410 if (fd != -1
411 && strcmp (fd_name, file_data->file_name) != 0)
413 free (fd_name);
414 close (fd);
415 fd = -1;
417 if (fd == -1)
419 fd_name = xstrdup (file_data->file_name);
420 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
421 if (fd == -1)
422 return NULL;
425 #if LTO_MMAP_IO
426 if (!page_mask)
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)
439 return NULL;
441 return result + diff;
442 #else
443 result = (char *) xmalloc (len);
444 if (lseek (fd, offset, SEEK_SET) != offset
445 || read (fd, result, len) != (ssize_t) len)
447 free (result);
448 return NULL;
451 return result;
452 #endif
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
458 body. */
460 static const char *
461 get_section_data (struct lto_file_decl_data *file_data,
462 enum lto_section_type section_type,
463 const char *name,
464 size_t *len)
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);
470 char *data = NULL;
472 *len = 0;
473 s_slot.name = section_name;
474 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
475 if (f_slot)
477 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
478 *len = f_slot->len;
481 free (CONST_CAST (char *, section_name));
482 return data;
486 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
487 starts at OFFSET and has LEN bytes. */
489 static void
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)
495 #if LTO_MMAP_IO
496 intptr_t computed_len;
497 intptr_t computed_offset;
498 intptr_t diff;
499 #endif
501 #if LTO_MMAP_IO
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);
507 #else
508 free (CONST_CAST(char *, offset));
509 #endif
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;
519 int insns;
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 ();
535 part->name = name;
536 part->insns = 0;
537 VEC_safe_push (ltrans_partition, gc, ltrans_partitions, part);
538 return part;
541 /* Add NODE to partition as well as the inline callees into partition PART. */
543 static void
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
555 right now. */
557 static void
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;
565 void **slot;
566 int npartitions = 0;
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)
576 continue;
577 /* Nodes without a body do not need partitioning. */
578 if (!node->analyzed)
579 continue;
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);
585 if (slot)
586 partition = (ltrans_partition) *slot;
587 else
589 partition = new_partition (file_data->file_name);
590 slot = pointer_map_insert (pmap, file_data);
591 *slot = partition;
592 npartitions++;
595 add_cgraph_node_to_partition (partition, node);
598 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
600 if (vnode->alias || !vnode->needed)
601 continue;
602 file_data = vnode->lto_file_data;
603 slot = pointer_map_contains (pmap, file_data);
604 if (slot)
605 partition = (ltrans_partition) *slot;
606 else
608 partition = new_partition (file_data->file_name);
609 slot = pointer_map_insert (pmap, file_data);
610 *slot = partition;
611 npartitions++;
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. */
619 if (!npartitions)
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,
627 ltrans_partitions);
630 /* Promote variable VNODE to be static. */
632 static bool
633 promote_var (struct varpool_node *vnode)
635 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
636 return false;
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));
643 return true;
646 /* Promote function NODE to be static. */
648 static bool
649 promote_fn (struct cgraph_node *node)
651 gcc_assert (flag_wpa);
652 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
653 return false;
654 TREE_PUBLIC (node->decl) = 1;
655 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
656 if (node->same_body)
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);
670 return true;
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. */
677 static void
678 lto_promote_cross_file_statics (void)
680 struct varpool_node *vnode;
681 unsigned i, n_sets;
682 cgraph_node_set set;
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)
704 continue;
705 if (node->global.inlined_to)
706 continue;
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)))
710 promote_fn (node);
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,
721 set, vset))
722 promote_var (vnode);
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
729 folding.
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))
744 int i;
745 struct ipa_ref *ref;
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))
756 promote_fn (n);
758 else
760 struct varpool_node *v = ipa_ref_varpool_node (ref);
761 if (varpool_node_in_set_p (v, vset))
762 continue;
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)
775 if (promote_var (v)
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. */
794 static int
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
805 LTRANS units. */
807 static void
808 lto_wpa_write_files (void)
810 unsigned i, n_sets;
811 lto_file *file;
812 cgraph_node_set set;
813 varpool_node_set vset;
814 ltrans_partition part;
815 FILE *ltrans_output_list_stream;
816 char *temp_filename;
817 size_t blen;
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,
850 ".out") == 0)
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++)
859 size_t len;
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);
868 if (!file)
869 fatal_error ("lto_obj_file_open() failed");
871 if (!quiet_flag)
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",
896 ltrans_output_list);
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);
909 typedef struct {
910 struct pointer_set_t *seen;
911 } lto_fixup_data_t;
913 #define LTO_FIXUP_SUBTREE(t) \
914 do \
915 walk_tree (&(t), lto_fixup_tree, data, NULL); \
916 while (0)
918 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
919 do \
921 if (t) \
922 (t) = gimple_register_type (t); \
923 walk_tree (&(t), lto_fixup_tree, data, NULL); \
925 while (0)
927 static tree lto_fixup_tree (tree *, int *, void *);
929 /* Return true if T does not need to be fixed up recursively. */
931 static inline bool
932 no_fixup_p (tree t)
934 return (t == NULL
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. */
941 static void
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);
952 else
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);
957 if (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);
966 else
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);
971 if (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. */
991 static void
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. */
1001 static void
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. */
1014 static void
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. */
1027 static void
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. */
1041 static void
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. */
1050 static void
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. */
1063 static void
1064 lto_fixup_type (tree t, void *data)
1066 tree tem, mv;
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));
1087 else
1088 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1091 /* TYPE_CANONICAL does not need to be fixed up, instead it should
1092 always point to ourselves at this time as we never fixup
1093 non-canonical ones. */
1094 gcc_assert (TYPE_CANONICAL (t) == t);
1096 /* The following re-creates proper variant lists while fixing up
1097 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1098 variant list state before fixup is broken. */
1100 /* Remove us from our main variant list if we are not the variant leader. */
1101 if (TYPE_MAIN_VARIANT (t) != t)
1103 tem = TYPE_MAIN_VARIANT (t);
1104 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1105 tem = TYPE_NEXT_VARIANT (tem);
1106 if (tem)
1107 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1108 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1111 /* Query our new main variant. */
1112 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1114 /* If we were the variant leader and we get replaced ourselves drop
1115 all variants from our list. */
1116 if (TYPE_MAIN_VARIANT (t) == t
1117 && mv != t)
1119 tem = t;
1120 while (tem)
1122 tree tem2 = TYPE_NEXT_VARIANT (tem);
1123 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1124 tem = tem2;
1128 /* If we are not our own variant leader link us into our new leaders
1129 variant list. */
1130 if (mv != t)
1132 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1133 TYPE_NEXT_VARIANT (mv) = t;
1136 /* Finally adjust our main variant and fix it up. */
1137 TYPE_MAIN_VARIANT (t) = mv;
1138 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1140 /* As the second step of reconstructing the pointer chains put us
1141 on the list of pointers of the new pointed-to type
1142 if we are a main variant. See lto_fixup_common for the first step. */
1143 if (TREE_CODE (t) == POINTER_TYPE
1144 && TYPE_MAIN_VARIANT (t) == t)
1146 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1147 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1149 else if (TREE_CODE (t) == REFERENCE_TYPE
1150 && TYPE_MAIN_VARIANT (t) == t)
1152 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1153 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1157 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1159 static void
1160 lto_fixup_binfo (tree t, void *data)
1162 unsigned HOST_WIDE_INT i, n;
1163 tree base, saved_base;
1165 lto_fixup_common (t, data);
1166 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1167 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1168 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1169 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1170 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1171 for (i = 0; i < n; i++)
1173 saved_base = base = BINFO_BASE_ACCESS (t, i);
1174 LTO_FIXUP_SUBTREE (base);
1175 if (base != saved_base)
1176 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1178 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1179 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1180 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1181 n = BINFO_N_BASE_BINFOS (t);
1182 for (i = 0; i < n; i++)
1184 saved_base = base = BINFO_BASE_BINFO (t, i);
1185 LTO_FIXUP_SUBTREE (base);
1186 if (base != saved_base)
1187 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1191 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1193 static void
1194 lto_fixup_constructor (tree t, void *data)
1196 unsigned HOST_WIDE_INT idx;
1197 constructor_elt *ce;
1199 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1201 for (idx = 0;
1202 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1203 idx++)
1205 LTO_FIXUP_SUBTREE (ce->index);
1206 LTO_FIXUP_SUBTREE (ce->value);
1210 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1211 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1212 DATA is a pointer set to record visited nodes. */
1214 static tree
1215 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1217 tree t;
1218 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1219 tree prevailing;
1221 t = *tp;
1222 *walk_subtrees = 0;
1223 if (!t || pointer_set_contains (fixup_data->seen, t))
1224 return NULL;
1226 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1228 prevailing = lto_symtab_prevailing_decl (t);
1230 if (t != prevailing)
1232 /* Also replace t with prevailing defintion. We don't want to
1233 insert the other defintion in the seen set as we want to
1234 replace all instances of it. */
1235 *tp = prevailing;
1236 t = prevailing;
1239 else if (TYPE_P (t))
1241 /* Replace t with the prevailing type. We don't want to insert the
1242 other type in the seen set as we want to replace all instances of it. */
1243 t = gimple_register_type (t);
1244 *tp = t;
1247 if (pointer_set_insert (fixup_data->seen, t))
1248 return NULL;
1250 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1251 Hence we do special processing here for those kind of nodes. */
1252 switch (TREE_CODE (t))
1254 case FIELD_DECL:
1255 lto_fixup_field_decl (t, data);
1256 break;
1258 case LABEL_DECL:
1259 case CONST_DECL:
1260 case PARM_DECL:
1261 case RESULT_DECL:
1262 case IMPORTED_DECL:
1263 lto_fixup_decl_common (t, data);
1264 break;
1266 case VAR_DECL:
1267 lto_fixup_decl_with_vis (t, data);
1268 break;
1270 case TYPE_DECL:
1271 lto_fixup_decl_non_common (t, data);
1272 break;
1274 case FUNCTION_DECL:
1275 lto_fixup_function (t, data);
1276 break;
1278 case TREE_BINFO:
1279 lto_fixup_binfo (t, data);
1280 break;
1282 default:
1283 if (TYPE_P (t))
1284 lto_fixup_type (t, data);
1285 else if (TREE_CODE (t) == CONSTRUCTOR)
1286 lto_fixup_constructor (t, data);
1287 else if (CONSTANT_CLASS_P (t))
1288 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1289 else if (EXPR_P (t))
1291 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1292 lto_fixup_common (t, data);
1293 LTO_FIXUP_SUBTREE (t->exp.block);
1294 *walk_subtrees = 1;
1296 else
1298 /* Let walk_tree handle sub-trees. */
1299 *walk_subtrees = 1;
1303 return NULL;
1306 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1307 replaces var and function decls with the corresponding prevailing def and
1308 records the old decl in the free-list in DATA. We also record visted nodes
1309 in the seen-set in DATA to avoid multiple visit for nodes that need not
1310 to be replaced. */
1312 static void
1313 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1315 unsigned i, si;
1316 struct lto_tree_ref_table *table;
1318 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1319 we still need to walk from all DECLs to find the reachable
1320 FUNCTION_DECLs and VAR_DECLs. */
1321 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1323 table = &state->streams[si];
1324 for (i = 0; i < table->size; i++)
1325 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1329 /* A callback of htab_traverse. Just extract a state from SLOT and the
1330 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1332 static int
1333 lto_fixup_state_aux (void **slot, void *aux)
1335 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1336 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1337 return 1;
1340 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1341 prevailing one. */
1343 static void
1344 lto_fixup_decls (struct lto_file_decl_data **files)
1346 unsigned int i;
1347 tree decl;
1348 struct pointer_set_t *seen = pointer_set_create ();
1349 lto_fixup_data_t data;
1351 data.seen = seen;
1352 for (i = 0; files[i]; i++)
1354 struct lto_file_decl_data *file = files[i];
1355 struct lto_in_decl_state *state = file->global_decl_state;
1356 lto_fixup_state (state, &data);
1358 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1361 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1363 tree saved_decl = decl;
1364 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1365 if (decl != saved_decl)
1366 VEC_replace (tree, lto_global_var_decls, i, decl);
1369 pointer_set_destroy (seen);
1372 /* Read the options saved from each file in the command line. Called
1373 from lang_hooks.post_options which is called by process_options
1374 right before all the options are used to initialize the compiler.
1375 This assumes that decode_options has already run, so the
1376 num_in_fnames and in_fnames are properly set.
1378 Note that this assumes that all the files had been compiled with
1379 the same options, which is not a good assumption. In general,
1380 options ought to be read from all the files in the set and merged.
1381 However, it is still unclear what the merge rules should be. */
1383 void
1384 lto_read_all_file_options (void)
1386 size_t i;
1388 /* Clear any file options currently saved. */
1389 lto_clear_file_options ();
1391 /* Set the hooks to read ELF sections. */
1392 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1393 if (!quiet_flag)
1394 fprintf (stderr, "Reading command line options:");
1396 for (i = 0; i < num_in_fnames; i++)
1398 struct lto_file_decl_data *file_data;
1399 lto_file *file = lto_obj_file_open (in_fnames[i], false);
1400 if (!file)
1401 break;
1402 if (!quiet_flag)
1404 fprintf (stderr, " %s", in_fnames[i]);
1405 fflush (stderr);
1408 file_data = XCNEW (struct lto_file_decl_data);
1409 file_data->file_name = file->filename;
1410 file_data->section_hash_table = lto_obj_build_section_table (file);
1412 lto_read_file_options (file_data);
1414 lto_obj_file_close (file);
1415 htab_delete (file_data->section_hash_table);
1416 free (file_data);
1419 /* Apply globally the options read from all the files. */
1420 lto_reissue_options ();
1423 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1425 /* Read all the symbols from the input files FNAMES. NFILES is the
1426 number of files requested in the command line. Instantiate a
1427 global call graph by aggregating all the sub-graphs found in each
1428 file. */
1430 static void
1431 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1433 unsigned int i, last_file_ix;
1434 FILE *resolution;
1435 struct cgraph_node *node;
1437 lto_stats.num_input_files = nfiles;
1439 timevar_push (TV_IPA_LTO_DECL_IO);
1441 /* Set the hooks so that all of the ipa passes can read in their data. */
1442 all_file_decl_data
1443 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
1444 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1446 /* Read the resolution file. */
1447 resolution = NULL;
1448 if (resolution_file_name)
1450 int t;
1451 unsigned num_objects;
1453 resolution = fopen (resolution_file_name, "r");
1454 if (resolution == NULL)
1455 fatal_error ("could not open symbol resolution file: %m");
1457 t = fscanf (resolution, "%u", &num_objects);
1458 gcc_assert (t == 1);
1460 /* True, since the plugin splits the archives. */
1461 gcc_assert (num_objects == nfiles);
1464 if (!quiet_flag)
1465 fprintf (stderr, "Reading object files:");
1467 /* Read all of the object files specified on the command line. */
1468 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1470 struct lto_file_decl_data *file_data = NULL;
1471 if (!quiet_flag)
1473 fprintf (stderr, " %s", fnames[i]);
1474 fflush (stderr);
1477 current_lto_file = lto_obj_file_open (fnames[i], false);
1478 if (!current_lto_file)
1479 break;
1481 file_data = lto_file_read (current_lto_file, resolution);
1482 if (!file_data)
1483 break;
1485 all_file_decl_data[last_file_ix++] = file_data;
1487 lto_obj_file_close (current_lto_file);
1488 current_lto_file = NULL;
1489 /* ??? We'd want but can't ggc_collect () here as the type merging
1490 code in gimple.c uses hashtables that are not ggc aware. */
1493 if (resolution_file_name)
1494 fclose (resolution);
1496 all_file_decl_data[last_file_ix] = NULL;
1498 /* Set the hooks so that all of the ipa passes can read in their data. */
1499 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1501 timevar_pop (TV_IPA_LTO_DECL_IO);
1503 if (!quiet_flag)
1504 fprintf (stderr, "\nReading the callgraph\n");
1506 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1507 /* Read the callgraph. */
1508 input_cgraph ();
1509 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1511 if (!quiet_flag)
1512 fprintf (stderr, "Merging declarations\n");
1514 timevar_push (TV_IPA_LTO_DECL_MERGE);
1515 /* Merge global decls. */
1516 lto_symtab_merge_decls ();
1518 /* Fixup all decls and types and free the type hash tables. */
1519 lto_fixup_decls (all_file_decl_data);
1520 free_gimple_type_tables ();
1521 ggc_collect ();
1523 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1524 /* Each pass will set the appropriate timer. */
1526 if (!quiet_flag)
1527 fprintf (stderr, "Reading summaries\n");
1529 /* Read the IPA summary data. */
1530 if (flag_ltrans)
1531 ipa_read_optimization_summaries ();
1532 else
1533 ipa_read_summaries ();
1535 /* Finally merge the cgraph according to the decl merging decisions. */
1536 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1537 lto_symtab_merge_cgraph_nodes ();
1538 ggc_collect ();
1540 if (flag_ltrans)
1541 for (node = cgraph_nodes; node; node = node->next)
1543 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1544 summaries computed and needs to apply changes. At the moment WHOPR only
1545 supports inlining, so we can push it here by hand. In future we need to stream
1546 this field into ltrans compilation. */
1547 if (node->analyzed)
1548 VEC_safe_push (ipa_opt_pass, heap,
1549 node->ipa_transforms_to_apply,
1550 (ipa_opt_pass)&pass_ipa_inline);
1552 lto_symtab_free ();
1554 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1556 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1558 /* FIXME lto. This loop needs to be changed to use the pass manager to
1559 call the ipa passes directly. */
1560 if (!seen_error ())
1561 for (i = 0; i < last_file_ix; i++)
1563 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1564 lto_materialize_constructors_and_inits (file_data);
1567 /* Indicate that the cgraph is built and ready. */
1568 cgraph_function_flags_ready = true;
1570 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1571 ggc_free (all_file_decl_data);
1572 all_file_decl_data = NULL;
1576 /* Materialize all the bodies for all the nodes in the callgraph. */
1578 static void
1579 materialize_cgraph (void)
1581 tree decl;
1582 struct cgraph_node *node;
1583 unsigned i;
1584 timevar_id_t lto_timer;
1586 if (!quiet_flag)
1587 fprintf (stderr,
1588 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1591 /* Now that we have input the cgraph, we need to clear all of the aux
1592 nodes and read the functions if we are not running in WPA mode. */
1593 timevar_push (TV_IPA_LTO_GIMPLE_IO);
1595 for (node = cgraph_nodes; node; node = node->next)
1597 /* Some cgraph nodes get created on the fly, and they don't need
1598 to be materialized. For instance, nodes for nested functions
1599 where the parent function was not streamed out or builtin
1600 functions. Additionally, builtin functions should not be
1601 materialized and may, in fact, cause confusion because there
1602 may be a regular function in the file whose assembler name
1603 matches that of the function.
1604 See gcc.c-torture/execute/20030125-1.c and
1605 gcc.c-torture/execute/921215-1.c. */
1606 if (node->local.lto_file_data
1607 && !DECL_IS_BUILTIN (node->decl))
1609 lto_materialize_function (node);
1610 lto_stats.num_input_cgraph_nodes++;
1614 timevar_pop (TV_IPA_LTO_GIMPLE_IO);
1616 /* Start the appropriate timer depending on the mode that we are
1617 operating in. */
1618 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1619 : (flag_ltrans) ? TV_WHOPR_LTRANS
1620 : TV_LTO;
1621 timevar_push (lto_timer);
1623 current_function_decl = NULL;
1624 set_cfun (NULL);
1626 /* Inform the middle end about the global variables we have seen. */
1627 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1628 rest_of_decl_compilation (decl, 1, 0);
1630 if (!quiet_flag)
1631 fprintf (stderr, "\n");
1633 timevar_pop (lto_timer);
1637 /* Perform whole program analysis (WPA) on the callgraph and write out the
1638 optimization plan. */
1640 static void
1641 do_whole_program_analysis (void)
1643 /* Note that since we are in WPA mode, materialize_cgraph will not
1644 actually read in all the function bodies. It only materializes
1645 the decls and cgraph nodes so that analysis can be performed. */
1646 materialize_cgraph ();
1648 /* Reading in the cgraph uses different timers, start timing WPA now. */
1649 timevar_push (TV_WHOPR_WPA);
1651 if (pre_ipa_mem_report)
1653 fprintf (stderr, "Memory consumption before IPA\n");
1654 dump_memory_report (false);
1657 if (cgraph_dump_file)
1659 dump_cgraph (cgraph_dump_file);
1660 dump_varpool (cgraph_dump_file);
1663 cgraph_function_flags_ready = true;
1664 bitmap_obstack_initialize (NULL);
1665 ipa_register_cgraph_hooks ();
1666 cgraph_state = CGRAPH_STATE_IPA_SSA;
1668 execute_ipa_pass_list (all_regular_ipa_passes);
1670 if (cgraph_dump_file)
1672 fprintf (cgraph_dump_file, "Optimized ");
1673 dump_cgraph (cgraph_dump_file);
1674 dump_varpool (cgraph_dump_file);
1676 verify_cgraph ();
1677 bitmap_obstack_release (NULL);
1679 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1680 timevar_pop (TV_WHOPR_WPA);
1682 lto_1_to_1_map ();
1684 if (!quiet_flag)
1686 fprintf (stderr, "\nStreaming out");
1687 fflush (stderr);
1689 lto_wpa_write_files ();
1690 ggc_collect ();
1691 if (!quiet_flag)
1692 fprintf (stderr, "\n");
1694 if (post_ipa_mem_report)
1696 fprintf (stderr, "Memory consumption after IPA\n");
1697 dump_memory_report (false);
1700 /* Show the LTO report before launching LTRANS. */
1701 if (flag_lto_report)
1702 print_lto_report ();
1706 static GTY(()) tree lto_eh_personality_decl;
1708 /* Return the LTO personality function decl. */
1710 tree
1711 lto_eh_personality (void)
1713 if (!lto_eh_personality_decl)
1715 /* Use the first personality DECL for our personality if we don't
1716 support multiple ones. This ensures that we don't artificially
1717 create the need for them in a single-language program. */
1718 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
1719 lto_eh_personality_decl = first_personality_decl;
1720 else
1721 lto_eh_personality_decl = lhd_gcc_personality ();
1724 return lto_eh_personality_decl;
1728 /* Main entry point for the GIMPLE front end. This front end has
1729 three main personalities:
1731 - LTO (-flto). All the object files on the command line are
1732 loaded in memory and processed as a single translation unit.
1733 This is the traditional link-time optimization behavior.
1735 - WPA (-fwpa). Only the callgraph and summary information for
1736 files in the command file are loaded. A single callgraph
1737 (without function bodies) is instantiated for the whole set of
1738 files. IPA passes are only allowed to analyze the call graph
1739 and make transformation decisions. The callgraph is
1740 partitioned, each partition is written to a new object file
1741 together with the transformation decisions.
1743 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
1744 summary files from running again. Since WPA computed summary
1745 information and decided what transformations to apply, LTRANS
1746 simply applies them. */
1748 void
1749 lto_main (int debug_p ATTRIBUTE_UNUSED)
1751 lto_init_reader ();
1753 /* Read all the symbols and call graph from all the files in the
1754 command line. */
1755 read_cgraph_and_symbols (num_in_fnames, in_fnames);
1757 if (!seen_error ())
1759 /* If WPA is enabled analyze the whole call graph and create an
1760 optimization plan. Otherwise, read in all the function
1761 bodies and continue with optimization. */
1762 if (flag_wpa)
1763 do_whole_program_analysis ();
1764 else
1766 materialize_cgraph ();
1768 /* Let the middle end know that we have read and merged all of
1769 the input files. */
1770 cgraph_optimize ();
1772 /* FIXME lto, if the processes spawned by WPA fail, we miss
1773 the chance to print WPA's report, so WPA will call
1774 print_lto_report before launching LTRANS. If LTRANS was
1775 launched directly by the driver we would not need to do
1776 this. */
1777 if (flag_lto_report)
1778 print_lto_report ();
1783 #include "gt-lto-lto.h"