PR lto/44230
[official-gcc.git] / gcc / lto / lto.c
blob52ef9edbafdab847ad2465e1e97bc64b3e376620
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.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
62 DEF_VEC_P(bitmap);
63 DEF_VEC_ALLOC_P(bitmap,heap);
65 static GTY(()) tree first_personality_decl;
68 /* Read the constructors and inits. */
70 static void
71 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
73 size_t len;
74 const char *data = lto_get_section_data (file_data,
75 LTO_section_static_initializer,
76 NULL, &len);
77 lto_input_constructors_and_inits (file_data, data);
78 lto_free_section_data (file_data, LTO_section_static_initializer, NULL,
79 data, len);
82 /* Read the function body for the function associated with NODE. */
84 static void
85 lto_materialize_function (struct cgraph_node *node)
87 tree decl;
88 struct lto_file_decl_data *file_data;
89 const char *data, *name;
90 size_t len;
92 /* Ignore clone nodes. Read the body only from the original one.
93 We may find clone nodes during LTRANS after WPA has made inlining
94 decisions. */
95 if (node->clone_of)
96 return;
98 decl = node->decl;
99 file_data = node->local.lto_file_data;
100 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
102 /* We may have renamed the declaration, e.g., a static function. */
103 name = lto_get_decl_name_mapping (file_data, name);
105 data = lto_get_section_data (file_data, LTO_section_function_body,
106 name, &len);
107 if (data)
109 gcc_assert (!DECL_IS_BUILTIN (decl));
111 /* This function has a definition. */
112 TREE_STATIC (decl) = 1;
114 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
116 /* Load the function body only if not operating in WPA mode. In
117 WPA mode, the body of the function is not needed. */
118 if (!flag_wpa)
120 allocate_struct_function (decl, false);
121 announce_function (decl);
122 lto_input_function_body (file_data, decl, data);
123 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
124 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
125 lto_stats.num_function_bodies++;
128 lto_free_section_data (file_data, LTO_section_function_body, name,
129 data, len);
130 if (!flag_wpa)
131 ggc_collect ();
133 else
134 DECL_EXTERNAL (decl) = 1;
136 /* Let the middle end know about the function. */
137 rest_of_decl_compilation (decl, 1, 0);
141 /* Decode the content of memory pointed to by DATA in the the
142 in decl state object STATE. DATA_IN points to a data_in structure for
143 decoding. Return the address after the decoded object in the input. */
145 static const uint32_t *
146 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
147 struct lto_in_decl_state *state)
149 uint32_t ix;
150 tree decl;
151 uint32_t i, j;
153 ix = *data++;
154 decl = lto_streamer_cache_get (data_in->reader_cache, (int) ix);
155 if (TREE_CODE (decl) != FUNCTION_DECL)
157 gcc_assert (decl == void_type_node);
158 decl = NULL_TREE;
160 state->fn_decl = decl;
162 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
164 uint32_t size = *data++;
165 tree *decls = GGC_NEWVEC (tree, size);
167 for (j = 0; j < size; j++)
169 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
171 /* Register every type in the global type table. If the
172 type existed already, use the existing type. */
173 if (TYPE_P (decls[j]))
174 decls[j] = gimple_register_type (decls[j]);
177 state->streams[i].size = size;
178 state->streams[i].trees = decls;
179 data += size;
182 return data;
186 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
187 RESOLUTIONS is the set of symbols picked by the linker (read from the
188 resolution file when the linker plugin is being used). */
190 static void
191 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
192 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
194 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
195 const int32_t decl_offset = sizeof (struct lto_decl_header);
196 const int32_t main_offset = decl_offset + header->decl_state_size;
197 const int32_t string_offset = main_offset + header->main_size;
198 struct lto_input_block ib_main;
199 struct data_in *data_in;
200 unsigned int i;
201 const uint32_t *data_ptr, *data_end;
202 uint32_t num_decl_states;
204 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
205 header->main_size);
207 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
208 header->string_size, resolutions);
210 /* Read the global declarations and types. */
211 while (ib_main.p < ib_main.len)
213 tree t = lto_input_tree (&ib_main, data_in);
214 gcc_assert (t && ib_main.p <= ib_main.len);
217 /* Read in lto_in_decl_state objects. */
218 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
219 data_end =
220 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
221 num_decl_states = *data_ptr++;
223 gcc_assert (num_decl_states > 0);
224 decl_data->global_decl_state = lto_new_in_decl_state ();
225 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
226 decl_data->global_decl_state);
228 /* Read in per-function decl states and enter them in hash table. */
229 decl_data->function_decl_states =
230 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
232 for (i = 1; i < num_decl_states; i++)
234 struct lto_in_decl_state *state = lto_new_in_decl_state ();
235 void **slot;
237 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
238 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
239 gcc_assert (*slot == NULL);
240 *slot = state;
243 if (data_ptr != data_end)
244 internal_error ("bytecode stream: garbage at the end of symbols section");
246 /* Set the current decl state to be the global state. */
247 decl_data->current_decl_state = decl_data->global_decl_state;
249 lto_data_in_delete (data_in);
252 /* strtoll is not portable. */
253 int64_t
254 lto_parse_hex (const char *p) {
255 uint64_t ret = 0;
256 for (; *p != '\0'; ++p)
258 char c = *p;
259 unsigned char part;
260 ret <<= 4;
261 if (c >= '0' && c <= '9')
262 part = c - '0';
263 else if (c >= 'a' && c <= 'f')
264 part = c - 'a' + 10;
265 else if (c >= 'A' && c <= 'F')
266 part = c - 'A' + 10;
267 else
268 internal_error ("could not parse hex number");
269 ret |= part;
271 return ret;
274 /* Read resolution for file named FILE_NAME. The resolution is read from
275 RESOLUTION. An array with the symbol resolution is returned. The array
276 size is written to SIZE. */
278 static VEC(ld_plugin_symbol_resolution_t,heap) *
279 lto_resolution_read (FILE *resolution, lto_file *file)
281 /* We require that objects in the resolution file are in the same
282 order as the lto1 command line. */
283 unsigned int name_len;
284 char *obj_name;
285 unsigned int num_symbols;
286 unsigned int i;
287 VEC(ld_plugin_symbol_resolution_t,heap) *ret = NULL;
288 unsigned max_index = 0;
290 if (!resolution)
291 return NULL;
293 name_len = strlen (file->filename);
294 obj_name = XNEWVEC (char, name_len + 1);
295 fscanf (resolution, " "); /* Read white space. */
297 fread (obj_name, sizeof (char), name_len, resolution);
298 obj_name[name_len] = '\0';
299 if (strcmp (obj_name, file->filename) != 0)
300 internal_error ("unexpected file name %s in linker resolution file. "
301 "Expected %s", obj_name, file->filename);
302 if (file->offset != 0)
304 int t;
305 char offset_p[17];
306 int64_t offset;
307 t = fscanf (resolution, "@0x%16s", offset_p);
308 if (t != 1)
309 internal_error ("could not parse file offset");
310 offset = lto_parse_hex (offset_p);
311 if (offset != file->offset)
312 internal_error ("unexpected offset");
315 free (obj_name);
317 fscanf (resolution, "%u", &num_symbols);
319 for (i = 0; i < num_symbols; i++)
321 int t;
322 unsigned index;
323 char r_str[27];
324 enum ld_plugin_symbol_resolution r;
325 unsigned int j;
326 unsigned int lto_resolution_str_len =
327 sizeof (lto_resolution_str) / sizeof (char *);
329 t = fscanf (resolution, "%u %26s %*[^\n]\n", &index, r_str);
330 if (t != 2)
331 internal_error ("Invalid line in the resolution file.");
332 if (index > max_index)
333 max_index = index;
335 for (j = 0; j < lto_resolution_str_len; j++)
337 if (strcmp (lto_resolution_str[j], r_str) == 0)
339 r = (enum ld_plugin_symbol_resolution) j;
340 break;
343 if (j == lto_resolution_str_len)
344 internal_error ("Invalid resolution in the resolution file.");
346 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap, ret,
347 max_index + 1);
348 VEC_replace (ld_plugin_symbol_resolution_t, ret, index, r);
351 return ret;
354 /* Generate a TREE representation for all types and external decls
355 entities in FILE.
357 Read all of the globals out of the file. Then read the cgraph
358 and process the .o index into the cgraph nodes so that it can open
359 the .o file to load the functions and ipa information. */
361 static struct lto_file_decl_data *
362 lto_file_read (lto_file *file, FILE *resolution_file)
364 struct lto_file_decl_data *file_data;
365 const char *data;
366 size_t len;
367 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions;
369 resolutions = lto_resolution_read (resolution_file, file);
371 file_data = GGC_NEW (struct lto_file_decl_data);
372 file_data->file_name = file->filename;
373 file_data->section_hash_table = lto_obj_build_section_table (file);
374 file_data->renaming_hash_table = lto_create_renaming_table ();
376 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
377 lto_read_decls (file_data, data, resolutions);
378 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
380 return file_data;
383 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
384 #define LTO_MMAP_IO 1
385 #endif
387 #if LTO_MMAP_IO
388 /* Page size of machine is used for mmap and munmap calls. */
389 static size_t page_mask;
390 #endif
392 /* Get the section data of length LEN from FILENAME starting at
393 OFFSET. The data segment must be freed by the caller when the
394 caller is finished. Returns NULL if all was not well. */
396 static char *
397 lto_read_section_data (struct lto_file_decl_data *file_data,
398 intptr_t offset, size_t len)
400 char *result;
401 static int fd = -1;
402 static char *fd_name;
403 #if LTO_MMAP_IO
404 intptr_t computed_len;
405 intptr_t computed_offset;
406 intptr_t diff;
407 #endif
409 /* Keep a single-entry file-descriptor cache. The last file we
410 touched will get closed at exit.
411 ??? Eventually we want to add a more sophisticated larger cache
412 or rather fix function body streaming to not stream them in
413 practically random order. */
414 if (fd != -1
415 && strcmp (fd_name, file_data->file_name) != 0)
417 free (fd_name);
418 close (fd);
419 fd = -1;
421 if (fd == -1)
423 fd_name = xstrdup (file_data->file_name);
424 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
425 if (fd == -1)
426 return NULL;
429 #if LTO_MMAP_IO
430 if (!page_mask)
432 size_t page_size = sysconf (_SC_PAGE_SIZE);
433 page_mask = ~(page_size - 1);
436 computed_offset = offset & page_mask;
437 diff = offset - computed_offset;
438 computed_len = len + diff;
440 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
441 fd, computed_offset);
442 if (result == MAP_FAILED)
443 return NULL;
445 return result + diff;
446 #else
447 result = (char *) xmalloc (len);
448 if (lseek (fd, offset, SEEK_SET) != offset
449 || read (fd, result, len) != (ssize_t) len)
451 free (result);
452 return NULL;
455 return result;
456 #endif
460 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
461 NAME will be NULL unless the section type is for a function
462 body. */
464 static const char *
465 get_section_data (struct lto_file_decl_data *file_data,
466 enum lto_section_type section_type,
467 const char *name,
468 size_t *len)
470 htab_t section_hash_table = file_data->section_hash_table;
471 struct lto_section_slot *f_slot;
472 struct lto_section_slot s_slot;
473 const char *section_name = lto_get_section_name (section_type, name);
474 char *data = NULL;
476 *len = 0;
477 s_slot.name = section_name;
478 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
479 if (f_slot)
481 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
482 *len = f_slot->len;
485 free (CONST_CAST (char *, section_name));
486 return data;
490 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
491 starts at OFFSET and has LEN bytes. */
493 static void
494 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
495 enum lto_section_type section_type ATTRIBUTE_UNUSED,
496 const char *name ATTRIBUTE_UNUSED,
497 const char *offset, size_t len ATTRIBUTE_UNUSED)
499 #if LTO_MMAP_IO
500 intptr_t computed_len;
501 intptr_t computed_offset;
502 intptr_t diff;
503 #endif
505 #if LTO_MMAP_IO
506 computed_offset = ((intptr_t) offset) & page_mask;
507 diff = (intptr_t) offset - computed_offset;
508 computed_len = len + diff;
510 munmap ((caddr_t) computed_offset, computed_len);
511 #else
512 free (CONST_CAST(char *, offset));
513 #endif
516 /* Vector of all cgraph node sets. */
517 static GTY (()) VEC(cgraph_node_set, gc) *lto_cgraph_node_sets;
518 static GTY (()) VEC(varpool_node_set, gc) *lto_varpool_node_sets;
521 /* Group cgrah nodes by input files. This is used mainly for testing
522 right now. */
524 static void
525 lto_1_to_1_map (void)
527 struct cgraph_node *node;
528 struct varpool_node *vnode;
529 struct lto_file_decl_data *file_data;
530 struct pointer_map_t *pmap;
531 struct pointer_map_t *vpmap;
532 cgraph_node_set set;
533 varpool_node_set vset;
534 void **slot;
536 timevar_push (TV_WHOPR_WPA);
538 lto_cgraph_node_sets = VEC_alloc (cgraph_node_set, gc, 1);
539 lto_varpool_node_sets = VEC_alloc (varpool_node_set, gc, 1);
541 pmap = pointer_map_create ();
542 vpmap = pointer_map_create ();
544 for (node = cgraph_nodes; node; node = node->next)
546 /* We will get proper partition based on function they are inlined to. */
547 if (node->global.inlined_to)
548 continue;
549 /* Nodes without a body do not need partitioning. */
550 if (!node->analyzed)
551 continue;
553 file_data = node->local.lto_file_data;
554 gcc_assert (!node->same_body_alias && file_data);
556 slot = pointer_map_contains (pmap, file_data);
557 if (slot)
558 set = (cgraph_node_set) *slot;
559 else
561 set = cgraph_node_set_new ();
562 slot = pointer_map_insert (pmap, file_data);
563 *slot = set;
564 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
565 vset = varpool_node_set_new ();
566 slot = pointer_map_insert (vpmap, file_data);
567 *slot = vset;
568 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
571 cgraph_node_set_add (set, node);
574 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
576 if (vnode->alias || !vnode->needed)
577 continue;
578 slot = pointer_map_contains (vpmap, file_data);
579 if (slot)
580 vset = (varpool_node_set) *slot;
581 else
583 set = cgraph_node_set_new ();
584 slot = pointer_map_insert (pmap, file_data);
585 *slot = set;
586 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
587 vset = varpool_node_set_new ();
588 slot = pointer_map_insert (vpmap, file_data);
589 *slot = vset;
590 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
593 varpool_node_set_add (vset, vnode);
596 /* If the cgraph is empty, create one cgraph node set so that there is still
597 an output file for any variables that need to be exported in a DSO. */
598 if (!lto_cgraph_node_sets)
600 set = cgraph_node_set_new ();
601 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
602 vset = varpool_node_set_new ();
603 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
606 pointer_map_destroy (pmap);
607 pointer_map_destroy (vpmap);
609 timevar_pop (TV_WHOPR_WPA);
611 lto_stats.num_cgraph_partitions += VEC_length (cgraph_node_set,
612 lto_cgraph_node_sets);
616 /* Add inlined clone NODE and its master clone to SET, if NODE itself has
617 inlined callees, recursively add the callees. */
619 static void
620 lto_add_inline_clones (cgraph_node_set set, struct cgraph_node *node,
621 bitmap original_decls)
623 struct cgraph_node *callee;
624 struct cgraph_edge *edge;
626 cgraph_node_set_add (set, node);
628 /* Check to see if NODE has any inlined callee. */
629 for (edge = node->callees; edge != NULL; edge = edge->next_callee)
631 callee = edge->callee;
632 if (callee->global.inlined_to != NULL)
633 lto_add_inline_clones (set, callee, original_decls);
637 /* Compute the transitive closure of inlining of SET based on the
638 information in the callgraph. Returns a bitmap of decls that have
639 been inlined into SET indexed by UID. */
641 static void
642 lto_add_all_inlinees (cgraph_node_set set)
644 cgraph_node_set_iterator csi;
645 struct cgraph_node *node;
646 bitmap original_nodes = lto_bitmap_alloc ();
647 bitmap original_decls = lto_bitmap_alloc ();
648 bool changed;
650 /* We are going to iterate SET while adding to it, mark all original
651 nodes so that we only add node inlined to original nodes. */
652 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
654 bitmap_set_bit (original_nodes, csi_node (csi)->uid);
655 bitmap_set_bit (original_decls, DECL_UID (csi_node (csi)->decl));
658 /* Some of the original nodes might not be needed anymore.
659 Remove them. */
662 changed = false;
663 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
665 struct cgraph_node *inlined_to;
666 node = csi_node (csi);
668 /* NODE was not inlined. We still need it. */
669 if (!node->global.inlined_to)
670 continue;
672 inlined_to = node->global.inlined_to;
674 /* NODE should have only one caller. */
675 gcc_assert (!node->callers->next_caller);
677 if (!bitmap_bit_p (original_nodes, inlined_to->uid))
679 bitmap_clear_bit (original_nodes, node->uid);
680 cgraph_node_set_remove (set, node);
681 changed = true;
685 while (changed);
687 /* Transitively add to SET all the inline clones for every node that
688 has been inlined. */
689 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
691 node = csi_node (csi);
692 if (bitmap_bit_p (original_nodes, node->uid))
693 lto_add_inline_clones (set, node, original_decls);
696 lto_bitmap_free (original_nodes);
697 lto_bitmap_free (original_decls);
700 /* Promote variable VNODE to be static. */
702 static bool
703 promote_var (struct varpool_node *vnode)
705 if (TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
706 return false;
707 gcc_assert (flag_wpa);
708 TREE_PUBLIC (vnode->decl) = 1;
709 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
710 return true;
713 /* Promote function NODE to be static. */
715 static bool
716 promote_fn (struct cgraph_node *node)
718 gcc_assert (flag_wpa);
719 if (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
720 return false;
721 TREE_PUBLIC (node->decl) = 1;
722 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
723 if (node->same_body)
725 struct cgraph_node *alias;
726 for (alias = node->same_body;
727 alias; alias = alias->next)
729 TREE_PUBLIC (alias->decl) = 1;
730 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
733 return true;
736 /* Find out all static decls that need to be promoted to global because
737 of cross file sharing. This function must be run in the WPA mode after
738 all inlinees are added. */
740 static void
741 lto_promote_cross_file_statics (void)
743 struct varpool_node *vnode;
744 unsigned i, n_sets;
745 cgraph_node_set set;
746 varpool_node_set vset;
747 cgraph_node_set_iterator csi;
748 varpool_node_set_iterator vsi;
749 VEC(varpool_node_ptr, heap) *promoted_initializers = NULL;
750 struct pointer_set_t *inserted = pointer_set_create ();
752 gcc_assert (flag_wpa);
754 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
755 for (i = 0; i < n_sets; i++)
757 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
758 vset = VEC_index (varpool_node_set, lto_varpool_node_sets, i);
760 /* If node has either address taken (and we have no clue from where)
761 or it is called from other partition, it needs to be globalized. */
762 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
764 struct cgraph_node *node = csi_node (csi);
765 if (node->local.externally_visible)
766 continue;
767 if (node->global.inlined_to)
768 continue;
769 if (!DECL_EXTERNAL (node->decl)
770 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
771 || reachable_from_other_partition_p (node, set)))
772 promote_fn (node);
774 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
776 vnode = vsi_node (vsi);
777 /* Constant pool references use internal labels and thus can not
778 be made global. It is sensible to keep those ltrans local to
779 allow better optimization. */
780 if (!DECL_IN_CONSTANT_POOL (vnode->decl)
781 && !vnode->externally_visible && vnode->analyzed
782 && referenced_from_other_partition_p (&vnode->ref_list,
783 set, vset))
784 promote_var (vnode);
787 /* We export initializers of read-only var into each partition
788 referencing it. Folding might take declarations from the
789 initializers and use it; so everything referenced from the
790 initializers needs can be accessed from this partition after
791 folding.
793 This means that we need to promote all variables and functions
794 referenced from all initializers from readonly vars referenced
795 from this partition that are not in this partition.
796 This needs to be done recursively. */
797 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
798 if ((TREE_READONLY (vnode->decl) || DECL_IN_CONSTANT_POOL (vnode->decl))
799 && DECL_INITIAL (vnode->decl)
800 && !varpool_node_in_set_p (vnode, vset)
801 && referenced_from_this_partition_p (&vnode->ref_list, set, vset)
802 && !pointer_set_insert (inserted, vnode))
803 VEC_safe_push (varpool_node_ptr, heap, promoted_initializers, vnode);
804 while (!VEC_empty (varpool_node_ptr, promoted_initializers))
806 int i;
807 struct ipa_ref *ref;
809 vnode = VEC_pop (varpool_node_ptr, promoted_initializers);
810 for (i = 0; ipa_ref_list_reference_iterate (&vnode->ref_list, i, ref); i++)
812 if (ref->refered_type == IPA_REF_CGRAPH)
814 struct cgraph_node *n = ipa_ref_node (ref);
815 gcc_assert (!n->global.inlined_to);
816 if (!n->local.externally_visible
817 && !cgraph_node_in_set_p (n, set))
818 promote_fn (n);
820 else
822 struct varpool_node *v = ipa_ref_varpool_node (ref);
823 if (varpool_node_in_set_p (v, vset))
824 continue;
825 /* Constant pool references use internal labels and thus can not
826 be made global. It is sensible to keep those ltrans local to
827 allow better optimization. */
828 if (DECL_IN_CONSTANT_POOL (v->decl))
830 if (!pointer_set_insert (inserted, vnode))
831 VEC_safe_push (varpool_node_ptr, heap,
832 promoted_initializers, v);
834 else if (!DECL_IN_CONSTANT_POOL (v->decl)
835 && !v->externally_visible && v->analyzed)
837 if (promote_var (v)
838 && DECL_INITIAL (v->decl) && TREE_READONLY (v->decl)
839 && !pointer_set_insert (inserted, vnode))
840 VEC_safe_push (varpool_node_ptr, heap,
841 promoted_initializers, v);
847 pointer_set_destroy (inserted);
851 /* Given a file name FNAME, return a string with FNAME prefixed with '*'. */
853 static char *
854 prefix_name_with_star (const char *fname)
856 char *star_fname;
857 size_t len;
859 len = strlen (fname) + 1 + 1;
860 star_fname = XNEWVEC (char, len);
861 snprintf (star_fname, len, "*%s", fname);
863 return star_fname;
867 /* Return a copy of FNAME without the .o extension. */
869 static char *
870 strip_extension (const char *fname)
872 char *s = XNEWVEC (char, strlen (fname) - 2 + 1);
873 gcc_assert (strstr (fname, ".o"));
874 snprintf (s, strlen (fname) - 2 + 1, "%s", fname);
876 return s;
880 /* Return a file name associated with cgraph node set SET. This may
881 be a new temporary file name if SET needs to be processed by
882 LTRANS, or the original file name if all the nodes in SET belong to
883 the same input file. */
885 static char *
886 get_filename_for_set (cgraph_node_set set)
888 char *fname = NULL;
889 static const size_t max_fname_len = 100;
891 /* Create a new temporary file to store SET. To facilitate
892 debugging, use file names from SET as part of the new
893 temporary file name. */
894 cgraph_node_set_iterator si;
895 struct pointer_set_t *pset = pointer_set_create ();
896 for (si = csi_start (set); !csi_end_p (si); csi_next (&si))
898 struct cgraph_node *n = csi_node (si);
899 const char *node_fname;
900 char *f;
902 /* Don't use the same file name more than once. */
903 if (pointer_set_insert (pset, n->local.lto_file_data))
904 continue;
906 /* The first file name found in SET determines the output
907 directory. For the remaining files, we use their
908 base names. */
909 node_fname = n->local.lto_file_data->file_name;
910 if (fname == NULL)
912 fname = strip_extension (node_fname);
913 continue;
916 f = strip_extension (lbasename (node_fname));
918 /* If the new name causes an excessively long file name,
919 make the last component "___" to indicate overflow. */
920 if (strlen (fname) + strlen (f) > max_fname_len - 3)
922 fname = reconcat (fname, fname, "___", NULL);
923 break;
925 else
927 fname = reconcat (fname, fname, "_", f, NULL);
928 free (f);
932 pointer_set_destroy (pset);
934 if (!fname)
936 /* Since SET does not need to be processed by LTRANS, use
937 the original file name and mark it with a '*' prefix so that
938 lto_execute_ltrans knows not to process it. */
939 cgraph_node_set_iterator si = csi_start (set);
940 struct cgraph_node *first = csi_node (si);
941 fname = prefix_name_with_star (first->local.lto_file_data->file_name);
943 else
945 /* Add the extension .wpa.o to indicate that this file has been
946 produced by WPA. */
947 fname = reconcat (fname, fname, ".wpa.o", NULL);
948 gcc_assert (fname);
951 return fname;
954 static lto_file *current_lto_file;
957 /* Write all output files in WPA mode. Returns a NULL-terminated array of
958 output file names. */
960 static char **
961 lto_wpa_write_files (void)
963 char **output_files;
964 unsigned i, n_sets, last_out_file_ix, num_out_files;
965 lto_file *file;
966 cgraph_node_set set;
967 varpool_node_set vset;
969 timevar_push (TV_WHOPR_WPA);
971 /* Include all inlined functions and determine what sets need to be
972 compiled by LTRANS. After this loop, only those sets that
973 contain callgraph nodes from more than one file will need to be
974 compiled by LTRANS. */
975 for (i = 0; VEC_iterate (cgraph_node_set, lto_cgraph_node_sets, i, set); i++)
977 lto_add_all_inlinees (set);
978 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
979 set->nodes);
982 /* After adding all inlinees, find out statics that need to be promoted
983 to globals because of cross-file inlining. */
984 lto_promote_cross_file_statics ();
986 timevar_pop (TV_WHOPR_WPA);
988 timevar_push (TV_WHOPR_WPA_IO);
990 /* The number of output files depends on the number of input files
991 and how many callgraph node sets we create. Reserve enough space
992 for the maximum of these two. */
993 num_out_files = MAX (VEC_length (cgraph_node_set, lto_cgraph_node_sets),
994 num_in_fnames);
995 output_files = XNEWVEC (char *, num_out_files + 1);
997 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
998 for (i = 0; i < n_sets; i++)
1000 char *temp_filename;
1002 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
1003 vset = VEC_index (varpool_node_set, lto_varpool_node_sets, i);
1004 temp_filename = get_filename_for_set (set);
1005 output_files[i] = temp_filename;
1007 if (cgraph_node_set_nonempty_p (set) || varpool_node_set_nonempty_p (vset))
1009 /* Write all the nodes in SET to TEMP_FILENAME. */
1010 file = lto_obj_file_open (temp_filename, true);
1011 if (!file)
1012 fatal_error ("lto_obj_file_open() failed");
1014 if (!quiet_flag)
1015 fprintf (stderr, " %s", temp_filename);
1017 lto_set_current_out_file (file);
1019 ipa_write_optimization_summaries (set, vset);
1021 lto_set_current_out_file (NULL);
1022 lto_obj_file_close (file);
1026 last_out_file_ix = n_sets;
1028 lto_stats.num_output_files += n_sets;
1030 output_files[last_out_file_ix] = NULL;
1032 timevar_pop (TV_WHOPR_WPA_IO);
1034 return output_files;
1037 /* Perform local transformations (LTRANS) on the files in the NULL-terminated
1038 FILES array. These should have been written previously by
1039 lto_wpa_write_files (). Transformations are performed via executing
1040 COLLECT_GCC for reach file. */
1042 static void
1043 lto_write_ltrans_list (char *const *files)
1045 FILE *ltrans_output_list_stream = NULL;
1046 unsigned i;
1048 /* Open the LTRANS output list. */
1049 if (!ltrans_output_list)
1050 error ("no LTRANS output filename provided");
1052 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
1053 if (ltrans_output_list_stream == NULL)
1054 error ("opening LTRANS output list %s: %m", ltrans_output_list);
1056 for (i = 0; files[i]; ++i)
1058 size_t len;
1060 len = strlen (files[i]);
1061 if (fwrite (files[i], 1, len, ltrans_output_list_stream) < len
1062 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1063 error ("writing to LTRANS output list %s: %m",
1064 ltrans_output_list);
1067 /* Close the LTRANS output list. */
1068 if (fclose (ltrans_output_list_stream))
1069 error ("closing LTRANS output list %s: %m", ltrans_output_list);
1073 typedef struct {
1074 struct pointer_set_t *seen;
1075 } lto_fixup_data_t;
1077 #define LTO_FIXUP_SUBTREE(t) \
1078 do \
1079 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1080 while (0)
1082 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
1083 do \
1085 if (t) \
1086 (t) = gimple_register_type (t); \
1087 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1089 while (0)
1091 static tree lto_fixup_tree (tree *, int *, void *);
1093 /* Return true if T does not need to be fixed up recursively. */
1095 static inline bool
1096 no_fixup_p (tree t)
1098 return (t == NULL
1099 || CONSTANT_CLASS_P (t)
1100 || TREE_CODE (t) == IDENTIFIER_NODE);
1103 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
1105 static void
1106 lto_fixup_common (tree t, void *data)
1108 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
1109 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
1110 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
1111 First remove us from any pointer list we are on. */
1112 if (TREE_CODE (t) == POINTER_TYPE)
1114 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
1115 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
1116 else
1118 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
1119 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
1120 tem = TYPE_NEXT_PTR_TO (tem);
1121 if (tem)
1122 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
1124 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
1126 else if (TREE_CODE (t) == REFERENCE_TYPE)
1128 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
1129 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
1130 else
1132 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
1133 while (tem && TYPE_NEXT_REF_TO (tem) != t)
1134 tem = TYPE_NEXT_REF_TO (tem);
1135 if (tem)
1136 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
1138 TYPE_NEXT_REF_TO (t) = NULL_TREE;
1141 /* Fixup our type. */
1142 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1144 /* Second put us on the list of pointers of the new pointed-to type
1145 if we are a main variant. This is done in lto_fixup_type after
1146 fixing up our main variant. */
1148 /* This is not very efficient because we cannot do tail-recursion with
1149 a long chain of trees. */
1150 LTO_FIXUP_SUBTREE (TREE_CHAIN (t));
1153 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
1155 static void
1156 lto_fixup_decl_minimal (tree t, void *data)
1158 lto_fixup_common (t, data);
1159 LTO_FIXUP_SUBTREE (DECL_NAME (t));
1160 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t));
1163 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1165 static void
1166 lto_fixup_decl_common (tree t, void *data)
1168 lto_fixup_decl_minimal (t, data);
1169 LTO_FIXUP_SUBTREE (DECL_SIZE (t));
1170 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t));
1171 LTO_FIXUP_SUBTREE (DECL_INITIAL (t));
1172 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t));
1173 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t));
1176 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1178 static void
1179 lto_fixup_decl_with_vis (tree t, void *data)
1181 lto_fixup_decl_common (t, data);
1183 /* Accessor macro has side-effects, use field-name here. */
1184 LTO_FIXUP_SUBTREE (t->decl_with_vis.assembler_name);
1186 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t)));
1189 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1191 static void
1192 lto_fixup_decl_non_common (tree t, void *data)
1194 lto_fixup_decl_with_vis (t, data);
1195 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t));
1196 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t));
1197 LTO_FIXUP_SUBTREE (DECL_VINDEX (t));
1199 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1200 gcc_assert (no_fixup_p (t->decl_non_common.saved_tree));
1203 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1205 static void
1206 lto_fixup_function (tree t, void *data)
1208 lto_fixup_decl_non_common (t, data);
1209 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t));
1212 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1214 static void
1215 lto_fixup_field_decl (tree t, void *data)
1217 lto_fixup_decl_common (t, data);
1218 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t));
1219 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t));
1220 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t));
1221 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t)));
1222 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t));
1225 /* Fix up fields of a type T. DATA points to fix-up states. */
1227 static void
1228 lto_fixup_type (tree t, void *data)
1230 tree tem, mv;
1232 lto_fixup_common (t, data);
1233 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t));
1234 LTO_FIXUP_SUBTREE (TYPE_SIZE (t));
1235 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t));
1236 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t));
1237 LTO_FIXUP_SUBTREE (TYPE_NAME (t));
1239 /* Accessors are for derived node types only. */
1240 if (!POINTER_TYPE_P (t))
1241 LTO_FIXUP_SUBTREE (t->type.minval);
1242 LTO_FIXUP_SUBTREE (t->type.maxval);
1244 /* Accessor is for derived node types only. */
1245 LTO_FIXUP_SUBTREE (t->type.binfo);
1247 if (TYPE_CONTEXT (t))
1249 if (TYPE_P (TYPE_CONTEXT (t)))
1250 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1251 else
1252 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1254 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CANONICAL (t));
1256 /* The following re-creates proper variant lists while fixing up
1257 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1258 variant list state before fixup is broken. */
1260 /* Remove us from our main variant list if we are not the variant leader. */
1261 if (TYPE_MAIN_VARIANT (t) != t)
1263 tem = TYPE_MAIN_VARIANT (t);
1264 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1265 tem = TYPE_NEXT_VARIANT (tem);
1266 if (tem)
1267 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1268 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1271 /* Query our new main variant. */
1272 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1274 /* If we were the variant leader and we get replaced ourselves drop
1275 all variants from our list. */
1276 if (TYPE_MAIN_VARIANT (t) == t
1277 && mv != t)
1279 tem = t;
1280 while (tem)
1282 tree tem2 = TYPE_NEXT_VARIANT (tem);
1283 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1284 tem = tem2;
1288 /* If we are not our own variant leader link us into our new leaders
1289 variant list. */
1290 if (mv != t)
1292 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1293 TYPE_NEXT_VARIANT (mv) = t;
1296 /* Finally adjust our main variant and fix it up. */
1297 TYPE_MAIN_VARIANT (t) = mv;
1298 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1300 /* As the second step of reconstructing the pointer chains put us
1301 on the list of pointers of the new pointed-to type
1302 if we are a main variant. See lto_fixup_common for the first step. */
1303 if (TREE_CODE (t) == POINTER_TYPE
1304 && TYPE_MAIN_VARIANT (t) == t)
1306 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1307 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1309 else if (TREE_CODE (t) == REFERENCE_TYPE
1310 && TYPE_MAIN_VARIANT (t) == t)
1312 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1313 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1317 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1319 static void
1320 lto_fixup_binfo (tree t, void *data)
1322 unsigned HOST_WIDE_INT i, n;
1323 tree base, saved_base;
1325 lto_fixup_common (t, data);
1326 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1327 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1328 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1329 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1330 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1331 for (i = 0; i < n; i++)
1333 saved_base = base = BINFO_BASE_ACCESS (t, i);
1334 LTO_FIXUP_SUBTREE (base);
1335 if (base != saved_base)
1336 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1338 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1339 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1340 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1341 n = BINFO_N_BASE_BINFOS (t);
1342 for (i = 0; i < n; i++)
1344 saved_base = base = BINFO_BASE_BINFO (t, i);
1345 LTO_FIXUP_SUBTREE (base);
1346 if (base != saved_base)
1347 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1351 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1353 static void
1354 lto_fixup_constructor (tree t, void *data)
1356 unsigned HOST_WIDE_INT idx;
1357 constructor_elt *ce;
1359 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1361 for (idx = 0;
1362 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1363 idx++)
1365 LTO_FIXUP_SUBTREE (ce->index);
1366 LTO_FIXUP_SUBTREE (ce->value);
1370 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1371 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1372 DATA is a pointer set to record visited nodes. */
1374 static tree
1375 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1377 tree t;
1378 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1379 tree prevailing;
1381 t = *tp;
1382 *walk_subtrees = 0;
1383 if (!t || pointer_set_contains (fixup_data->seen, t))
1384 return NULL;
1386 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1388 prevailing = lto_symtab_prevailing_decl (t);
1390 if (t != prevailing)
1392 /* Also replace t with prevailing defintion. We don't want to
1393 insert the other defintion in the seen set as we want to
1394 replace all instances of it. */
1395 *tp = prevailing;
1396 t = prevailing;
1399 else if (TYPE_P (t))
1401 /* Replace t with the prevailing type. We don't want to insert the
1402 other type in the seen set as we want to replace all instances of it. */
1403 t = gimple_register_type (t);
1404 *tp = t;
1407 if (pointer_set_insert (fixup_data->seen, t))
1408 return NULL;
1410 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1411 Hence we do special processing here for those kind of nodes. */
1412 switch (TREE_CODE (t))
1414 case FIELD_DECL:
1415 lto_fixup_field_decl (t, data);
1416 break;
1418 case LABEL_DECL:
1419 case CONST_DECL:
1420 case PARM_DECL:
1421 case RESULT_DECL:
1422 case IMPORTED_DECL:
1423 lto_fixup_decl_common (t, data);
1424 break;
1426 case VAR_DECL:
1427 lto_fixup_decl_with_vis (t, data);
1428 break;
1430 case TYPE_DECL:
1431 lto_fixup_decl_non_common (t, data);
1432 break;
1434 case FUNCTION_DECL:
1435 lto_fixup_function (t, data);
1436 break;
1438 case TREE_BINFO:
1439 lto_fixup_binfo (t, data);
1440 break;
1442 default:
1443 if (TYPE_P (t))
1444 lto_fixup_type (t, data);
1445 else if (TREE_CODE (t) == CONSTRUCTOR)
1446 lto_fixup_constructor (t, data);
1447 else if (CONSTANT_CLASS_P (t))
1448 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1449 else if (EXPR_P (t))
1451 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1452 lto_fixup_common (t, data);
1453 LTO_FIXUP_SUBTREE (t->exp.block);
1454 *walk_subtrees = 1;
1456 else
1458 /* Let walk_tree handle sub-trees. */
1459 *walk_subtrees = 1;
1463 return NULL;
1466 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1467 replaces var and function decls with the corresponding prevailing def and
1468 records the old decl in the free-list in DATA. We also record visted nodes
1469 in the seen-set in DATA to avoid multiple visit for nodes that need not
1470 to be replaced. */
1472 static void
1473 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1475 unsigned i, si;
1476 struct lto_tree_ref_table *table;
1478 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1479 we still need to walk from all DECLs to find the reachable
1480 FUNCTION_DECLs and VAR_DECLs. */
1481 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1483 table = &state->streams[si];
1484 for (i = 0; i < table->size; i++)
1485 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1489 /* A callback of htab_traverse. Just extract a state from SLOT and the
1490 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1492 static int
1493 lto_fixup_state_aux (void **slot, void *aux)
1495 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1496 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1497 return 1;
1500 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1501 prevailing one. */
1503 static void
1504 lto_fixup_decls (struct lto_file_decl_data **files)
1506 unsigned int i;
1507 tree decl;
1508 struct pointer_set_t *seen = pointer_set_create ();
1509 lto_fixup_data_t data;
1511 data.seen = seen;
1512 for (i = 0; files[i]; i++)
1514 struct lto_file_decl_data *file = files[i];
1515 struct lto_in_decl_state *state = file->global_decl_state;
1516 lto_fixup_state (state, &data);
1518 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1521 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1523 tree saved_decl = decl;
1524 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1525 if (decl != saved_decl)
1526 VEC_replace (tree, lto_global_var_decls, i, decl);
1529 pointer_set_destroy (seen);
1532 /* Read the options saved from each file in the command line. Called
1533 from lang_hooks.post_options which is called by process_options
1534 right before all the options are used to initialize the compiler.
1535 This assumes that decode_options has already run, so the
1536 num_in_fnames and in_fnames are properly set.
1538 Note that this assumes that all the files had been compiled with
1539 the same options, which is not a good assumption. In general,
1540 options ought to be read from all the files in the set and merged.
1541 However, it is still unclear what the merge rules should be. */
1543 void
1544 lto_read_all_file_options (void)
1546 size_t i;
1548 /* Clear any file options currently saved. */
1549 lto_clear_file_options ();
1551 /* Set the hooks to read ELF sections. */
1552 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1554 for (i = 0; i < num_in_fnames; i++)
1556 struct lto_file_decl_data *file_data;
1557 lto_file *file = lto_obj_file_open (in_fnames[i], false);
1558 if (!file)
1559 break;
1561 file_data = XCNEW (struct lto_file_decl_data);
1562 file_data->file_name = file->filename;
1563 file_data->section_hash_table = lto_obj_build_section_table (file);
1565 lto_read_file_options (file_data);
1567 lto_obj_file_close (file);
1568 htab_delete (file_data->section_hash_table);
1569 free (file_data);
1572 /* Apply globally the options read from all the files. */
1573 lto_reissue_options ();
1576 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1578 /* Read all the symbols from the input files FNAMES. NFILES is the
1579 number of files requested in the command line. Instantiate a
1580 global call graph by aggregating all the sub-graphs found in each
1581 file. */
1583 static void
1584 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1586 unsigned int i, last_file_ix;
1587 FILE *resolution;
1588 struct cgraph_node *node;
1590 lto_stats.num_input_files = nfiles;
1592 timevar_push (TV_IPA_LTO_DECL_IO);
1594 /* Set the hooks so that all of the ipa passes can read in their data. */
1595 all_file_decl_data = GGC_CNEWVEC (struct lto_file_decl_data *, nfiles + 1);
1596 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1598 /* Read the resolution file. */
1599 resolution = NULL;
1600 if (resolution_file_name)
1602 int t;
1603 unsigned num_objects;
1605 resolution = fopen (resolution_file_name, "r");
1606 if (resolution == NULL)
1607 fatal_error ("could not open symbol resolution file: %s",
1608 xstrerror (errno));
1610 t = fscanf (resolution, "%u", &num_objects);
1611 gcc_assert (t == 1);
1613 /* True, since the plugin splits the archives. */
1614 gcc_assert (num_objects == nfiles);
1617 if (!quiet_flag)
1618 fprintf (stderr, "Reading object files:");
1620 /* Read all of the object files specified on the command line. */
1621 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1623 struct lto_file_decl_data *file_data = NULL;
1624 if (!quiet_flag)
1626 fprintf (stderr, " %s", fnames[i]);
1627 fflush (stderr);
1630 current_lto_file = lto_obj_file_open (fnames[i], false);
1631 if (!current_lto_file)
1632 break;
1634 file_data = lto_file_read (current_lto_file, resolution);
1635 if (!file_data)
1636 break;
1638 all_file_decl_data[last_file_ix++] = file_data;
1640 lto_obj_file_close (current_lto_file);
1641 current_lto_file = NULL;
1642 /* ??? We'd want but can't ggc_collect () here as the type merging
1643 code in gimple.c uses hashtables that are not ggc aware. */
1646 if (resolution_file_name)
1647 fclose (resolution);
1649 all_file_decl_data[last_file_ix] = NULL;
1651 /* Set the hooks so that all of the ipa passes can read in their data. */
1652 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1654 timevar_pop (TV_IPA_LTO_DECL_IO);
1656 if (!quiet_flag)
1657 fprintf (stderr, "\nReading the callgraph\n");
1659 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1660 /* Read the callgraph. */
1661 input_cgraph ();
1662 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1664 if (!quiet_flag)
1665 fprintf (stderr, "Merging declarations\n");
1667 timevar_push (TV_IPA_LTO_DECL_MERGE);
1668 /* Merge global decls. */
1669 lto_symtab_merge_decls ();
1671 /* Fixup all decls and types and free the type hash tables. */
1672 lto_fixup_decls (all_file_decl_data);
1673 free_gimple_type_tables ();
1674 ggc_collect ();
1676 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1677 /* Each pass will set the appropriate timer. */
1679 if (!quiet_flag)
1680 fprintf (stderr, "Reading summaries\n");
1682 /* Read the IPA summary data. */
1683 if (flag_ltrans)
1684 ipa_read_optimization_summaries ();
1685 else
1686 ipa_read_summaries ();
1688 /* Finally merge the cgraph according to the decl merging decisions. */
1689 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1690 lto_symtab_merge_cgraph_nodes ();
1691 ggc_collect ();
1693 if (flag_ltrans)
1694 for (node = cgraph_nodes; node; node = node->next)
1696 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1697 summaries computed and needs to apply changes. At the moment WHOPR only
1698 supports inlining, so we can push it here by hand. In future we need to stream
1699 this field into ltrans compilation. */
1700 if (node->analyzed)
1701 VEC_safe_push (ipa_opt_pass, heap,
1702 node->ipa_transforms_to_apply,
1703 (ipa_opt_pass)&pass_ipa_inline);
1705 lto_symtab_free ();
1707 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1709 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1711 /* FIXME lto. This loop needs to be changed to use the pass manager to
1712 call the ipa passes directly. */
1713 if (!errorcount)
1714 for (i = 0; i < last_file_ix; i++)
1716 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1717 lto_materialize_constructors_and_inits (file_data);
1720 /* Indicate that the cgraph is built and ready. */
1721 cgraph_function_flags_ready = true;
1723 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1724 ggc_free (all_file_decl_data);
1725 all_file_decl_data = NULL;
1729 /* Materialize all the bodies for all the nodes in the callgraph. */
1731 static void
1732 materialize_cgraph (void)
1734 tree decl;
1735 struct cgraph_node *node;
1736 unsigned i;
1737 timevar_id_t lto_timer;
1739 if (!quiet_flag)
1740 fprintf (stderr,
1741 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1744 /* Now that we have input the cgraph, we need to clear all of the aux
1745 nodes and read the functions if we are not running in WPA mode. */
1746 timevar_push (TV_IPA_LTO_GIMPLE_IO);
1748 for (node = cgraph_nodes; node; node = node->next)
1750 /* Some cgraph nodes get created on the fly, and they don't need
1751 to be materialized. For instance, nodes for nested functions
1752 where the parent function was not streamed out or builtin
1753 functions. Additionally, builtin functions should not be
1754 materialized and may, in fact, cause confusion because there
1755 may be a regular function in the file whose assembler name
1756 matches that of the function.
1757 See gcc.c-torture/execute/20030125-1.c and
1758 gcc.c-torture/execute/921215-1.c. */
1759 if (node->local.lto_file_data
1760 && !DECL_IS_BUILTIN (node->decl))
1762 lto_materialize_function (node);
1763 lto_stats.num_input_cgraph_nodes++;
1767 timevar_pop (TV_IPA_LTO_GIMPLE_IO);
1769 /* Start the appropriate timer depending on the mode that we are
1770 operating in. */
1771 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1772 : (flag_ltrans) ? TV_WHOPR_LTRANS
1773 : TV_LTO;
1774 timevar_push (lto_timer);
1776 current_function_decl = NULL;
1777 set_cfun (NULL);
1779 /* Inform the middle end about the global variables we have seen. */
1780 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1781 rest_of_decl_compilation (decl, 1, 0);
1783 if (!quiet_flag)
1784 fprintf (stderr, "\n");
1786 timevar_pop (lto_timer);
1790 /* Perform whole program analysis (WPA) on the callgraph and write out the
1791 optimization plan. */
1793 static void
1794 do_whole_program_analysis (void)
1796 char **output_files;
1798 /* Note that since we are in WPA mode, materialize_cgraph will not
1799 actually read in all the function bodies. It only materializes
1800 the decls and cgraph nodes so that analysis can be performed. */
1801 materialize_cgraph ();
1803 /* Reading in the cgraph uses different timers, start timing WPA now. */
1804 timevar_push (TV_WHOPR_WPA);
1806 if (pre_ipa_mem_report)
1808 fprintf (stderr, "Memory consumption before IPA\n");
1809 dump_memory_report (false);
1812 cgraph_function_flags_ready = true;
1813 bitmap_obstack_initialize (NULL);
1814 ipa_register_cgraph_hooks ();
1815 cgraph_state = CGRAPH_STATE_IPA_SSA;
1817 execute_ipa_pass_list (all_regular_ipa_passes);
1819 verify_cgraph ();
1820 bitmap_obstack_release (NULL);
1822 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1823 timevar_pop (TV_WHOPR_WPA);
1825 lto_1_to_1_map ();
1827 if (!quiet_flag)
1829 fprintf (stderr, "\nStreaming out");
1830 fflush (stderr);
1832 output_files = lto_wpa_write_files ();
1833 ggc_collect ();
1834 if (!quiet_flag)
1835 fprintf (stderr, "\n");
1837 if (post_ipa_mem_report)
1839 fprintf (stderr, "Memory consumption after IPA\n");
1840 dump_memory_report (false);
1843 /* Show the LTO report before launching LTRANS. */
1844 if (flag_lto_report)
1845 print_lto_report ();
1847 lto_write_ltrans_list (output_files);
1849 XDELETEVEC (output_files);
1853 static GTY(()) tree lto_eh_personality_decl;
1855 /* Return the LTO personality function decl. */
1857 tree
1858 lto_eh_personality (void)
1860 if (!lto_eh_personality_decl)
1862 /* Use the first personality DECL for our personality if we don't
1863 support multiple ones. This ensures that we don't artificially
1864 create the need for them in a single-language program. */
1865 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
1866 lto_eh_personality_decl = first_personality_decl;
1867 else
1868 lto_eh_personality_decl = lhd_gcc_personality ();
1871 return lto_eh_personality_decl;
1875 /* Main entry point for the GIMPLE front end. This front end has
1876 three main personalities:
1878 - LTO (-flto). All the object files on the command line are
1879 loaded in memory and processed as a single translation unit.
1880 This is the traditional link-time optimization behavior.
1882 - WPA (-fwpa). Only the callgraph and summary information for
1883 files in the command file are loaded. A single callgraph
1884 (without function bodies) is instantiated for the whole set of
1885 files. IPA passes are only allowed to analyze the call graph
1886 and make transformation decisions. The callgraph is
1887 partitioned, each partition is written to a new object file
1888 together with the transformation decisions.
1890 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
1891 summary files from running again. Since WPA computed summary
1892 information and decided what transformations to apply, LTRANS
1893 simply applies them. */
1895 void
1896 lto_main (int debug_p ATTRIBUTE_UNUSED)
1898 lto_init_reader ();
1900 /* Read all the symbols and call graph from all the files in the
1901 command line. */
1902 read_cgraph_and_symbols (num_in_fnames, in_fnames);
1904 if (!errorcount)
1906 /* If WPA is enabled analyze the whole call graph and create an
1907 optimization plan. Otherwise, read in all the function
1908 bodies and continue with optimization. */
1909 if (flag_wpa)
1910 do_whole_program_analysis ();
1911 else
1913 materialize_cgraph ();
1915 /* Let the middle end know that we have read and merged all of
1916 the input files. */
1917 cgraph_optimize ();
1919 /* FIXME lto, if the processes spawned by WPA fail, we miss
1920 the chance to print WPA's report, so WPA will call
1921 print_lto_report before launching LTRANS. If LTRANS was
1922 launched directly by the driver we would not need to do
1923 this. */
1924 if (flag_lto_report)
1925 print_lto_report ();
1930 #include "gt-lto-lto.h"