]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto/lto.c
lto-symtab.c (lto_symtab_free): New function.
[thirdparty/gcc.git] / gcc / lto / lto.c
1 /* Top-level LTO routines.
2 Copyright 2009, 2010 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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 "timevar.h"
41 #include "gimple.h"
42 #include "lto.h"
43 #include "lto-tree.h"
44 #include "lto-streamer.h"
45
46 /* This needs to be included after config.h. Otherwise, _GNU_SOURCE will not
47 be defined in time to set __USE_GNU in the system headers, and strsignal
48 will not be declared. */
49 #if HAVE_MMAP_FILE
50 #include <sys/mman.h>
51 #endif
52
53 /* Handle opening elf files on hosts, such as Windows, that may use
54 text file handling that will break binary access. */
55
56 #ifndef O_BINARY
57 # define O_BINARY 0
58 #endif
59
60
61 DEF_VEC_P(bitmap);
62 DEF_VEC_ALLOC_P(bitmap,heap);
63
64 /* Read the constructors and inits. */
65
66 static void
67 lto_materialize_constructors_and_inits (struct lto_file_decl_data * file_data)
68 {
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);
76 }
77
78 /* Read the function body for the function associated with NODE if possible. */
79
80 static void
81 lto_materialize_function (struct cgraph_node *node)
82 {
83 tree decl;
84 struct lto_file_decl_data *file_data;
85 const char *data, *name;
86 size_t len;
87 tree step;
88
89 /* Ignore clone nodes. Read the body only from the original one.
90 We may find clone nodes during LTRANS after WPA has made inlining
91 decisions. */
92 if (node->clone_of)
93 return;
94
95 decl = node->decl;
96 file_data = node->local.lto_file_data;
97 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
98
99 /* We may have renamed the declaration, e.g., a static function. */
100 name = lto_get_decl_name_mapping (file_data, name);
101
102 data = lto_get_section_data (file_data, LTO_section_function_body,
103 name, &len);
104 if (data)
105 {
106 struct function *fn;
107
108 gcc_assert (!DECL_IS_BUILTIN (decl));
109
110 /* This function has a definition. */
111 TREE_STATIC (decl) = 1;
112
113 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
114 allocate_struct_function (decl, false);
115
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)
119 {
120 lto_input_function_body (file_data, decl, data);
121 lto_stats.num_function_bodies++;
122 }
123
124 fn = DECL_STRUCT_FUNCTION (decl);
125 lto_free_section_data (file_data, LTO_section_function_body, name,
126 data, len);
127
128 /* Look for initializers of constant variables and private
129 statics. */
130 for (step = fn->local_decls; step; step = TREE_CHAIN (step))
131 {
132 tree decl = TREE_VALUE (step);
133 if (TREE_CODE (decl) == VAR_DECL
134 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
135 && flag_unit_at_a_time)
136 varpool_finalize_decl (decl);
137 }
138 }
139 else
140 DECL_EXTERNAL (decl) = 1;
141
142 /* Let the middle end know about the function. */
143 rest_of_decl_compilation (decl, 1, 0);
144 if (cgraph_node (decl)->needed)
145 cgraph_mark_reachable_node (cgraph_node (decl));
146 }
147
148
149 /* Decode the content of memory pointed to by DATA in the the
150 in decl state object STATE. DATA_IN points to a data_in structure for
151 decoding. Return the address after the decoded object in the input. */
152
153 static const uint32_t *
154 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
155 struct lto_in_decl_state *state)
156 {
157 uint32_t ix;
158 tree decl;
159 uint32_t i, j;
160
161 ix = *data++;
162 decl = lto_streamer_cache_get (data_in->reader_cache, (int) ix);
163 if (TREE_CODE (decl) != FUNCTION_DECL)
164 {
165 gcc_assert (decl == void_type_node);
166 decl = NULL_TREE;
167 }
168 state->fn_decl = decl;
169
170 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
171 {
172 uint32_t size = *data++;
173 tree *decls = GGC_NEWVEC (tree, size);
174
175 for (j = 0; j < size; j++)
176 {
177 decls[j] = lto_streamer_cache_get (data_in->reader_cache, data[j]);
178
179 /* Register every type in the global type table. If the
180 type existed already, use the existing type. */
181 if (TYPE_P (decls[j]))
182 decls[j] = gimple_register_type (decls[j]);
183 }
184
185 state->streams[i].size = size;
186 state->streams[i].trees = decls;
187 data += size;
188 }
189
190 return data;
191 }
192
193
194 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
195 RESOLUTIONS is the set of symbols picked by the linker (read from the
196 resolution file when the linker plugin is being used). */
197
198 static void
199 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
200 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
201 {
202 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
203 const int32_t decl_offset = sizeof (struct lto_decl_header);
204 const int32_t main_offset = decl_offset + header->decl_state_size;
205 const int32_t string_offset = main_offset + header->main_size;
206 struct lto_input_block ib_main;
207 struct data_in *data_in;
208 unsigned int i;
209 const uint32_t *data_ptr, *data_end;
210 uint32_t num_decl_states;
211
212 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
213 header->main_size);
214
215 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
216 header->string_size, resolutions);
217
218 /* Read the global declarations and types. */
219 while (ib_main.p < ib_main.len)
220 {
221 tree t = lto_input_tree (&ib_main, data_in);
222 gcc_assert (t && ib_main.p <= ib_main.len);
223 }
224
225 /* Read in lto_in_decl_state objects. */
226 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
227 data_end =
228 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
229 num_decl_states = *data_ptr++;
230
231 gcc_assert (num_decl_states > 0);
232 decl_data->global_decl_state = lto_new_in_decl_state ();
233 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
234 decl_data->global_decl_state);
235
236 /* Read in per-function decl states and enter them in hash table. */
237 decl_data->function_decl_states =
238 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
239
240 for (i = 1; i < num_decl_states; i++)
241 {
242 struct lto_in_decl_state *state = lto_new_in_decl_state ();
243 void **slot;
244
245 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
246 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
247 gcc_assert (*slot == NULL);
248 *slot = state;
249 }
250
251 if (data_ptr != data_end)
252 internal_error ("bytecode stream: garbage at the end of symbols section");
253
254 /* Set the current decl state to be the global state. */
255 decl_data->current_decl_state = decl_data->global_decl_state;
256
257 lto_data_in_delete (data_in);
258 }
259
260 /* strtoll is not portable. */
261 int64_t
262 lto_parse_hex (const char *p) {
263 uint64_t ret = 0;
264 for (; *p != '\0'; ++p)
265 {
266 char c = *p;
267 unsigned char part;
268 ret <<= 4;
269 if (c >= '0' && c <= '9')
270 part = c - '0';
271 else if (c >= 'a' && c <= 'f')
272 part = c - 'a' + 10;
273 else if (c >= 'A' && c <= 'F')
274 part = c - 'A' + 10;
275 else
276 internal_error ("could not parse hex number");
277 ret |= part;
278 }
279 return ret;
280 }
281
282 /* Read resolution for file named FILE_NAME. The resolution is read from
283 RESOLUTION. An array with the symbol resolution is returned. The array
284 size is written to SIZE. */
285
286 static VEC(ld_plugin_symbol_resolution_t,heap) *
287 lto_resolution_read (FILE *resolution, lto_file *file)
288 {
289 /* We require that objects in the resolution file are in the same
290 order as the lto1 command line. */
291 unsigned int name_len;
292 char *obj_name;
293 unsigned int num_symbols;
294 unsigned int i;
295 VEC(ld_plugin_symbol_resolution_t,heap) *ret = NULL;
296 unsigned max_index = 0;
297
298 if (!resolution)
299 return NULL;
300
301 name_len = strlen (file->filename);
302 obj_name = XNEWVEC (char, name_len + 1);
303 fscanf (resolution, " "); /* Read white space. */
304
305 fread (obj_name, sizeof (char), name_len, resolution);
306 obj_name[name_len] = '\0';
307 if (strcmp (obj_name, file->filename) != 0)
308 internal_error ("unexpected file name %s in linker resolution file. "
309 "Expected %s", obj_name, file->filename);
310 if (file->offset != 0)
311 {
312 int t;
313 char offset_p[17];
314 int64_t offset;
315 t = fscanf (resolution, "@0x%16s", offset_p);
316 if (t != 1)
317 internal_error ("could not parse file offset");
318 offset = lto_parse_hex (offset_p);
319 if (offset != file->offset)
320 internal_error ("unexpected offset");
321 }
322
323 free (obj_name);
324
325 fscanf (resolution, "%u", &num_symbols);
326
327 for (i = 0; i < num_symbols; i++)
328 {
329 int t;
330 unsigned index;
331 char r_str[27];
332 enum ld_plugin_symbol_resolution r;
333 unsigned int j;
334 unsigned int lto_resolution_str_len =
335 sizeof (lto_resolution_str) / sizeof (char *);
336
337 t = fscanf (resolution, "%u %26s %*[^\n]\n", &index, r_str);
338 if (t != 2)
339 internal_error ("Invalid line in the resolution file.");
340 if (index > max_index)
341 max_index = index;
342
343 for (j = 0; j < lto_resolution_str_len; j++)
344 {
345 if (strcmp (lto_resolution_str[j], r_str) == 0)
346 {
347 r = (enum ld_plugin_symbol_resolution) j;
348 break;
349 }
350 }
351 if (j == lto_resolution_str_len)
352 internal_error ("Invalid resolution in the resolution file.");
353
354 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap, ret,
355 max_index + 1);
356 VEC_replace (ld_plugin_symbol_resolution_t, ret, index, r);
357 }
358
359 return ret;
360 }
361
362 /* Generate a TREE representation for all types and external decls
363 entities in FILE.
364
365 Read all of the globals out of the file. Then read the cgraph
366 and process the .o index into the cgraph nodes so that it can open
367 the .o file to load the functions and ipa information. */
368
369 static struct lto_file_decl_data *
370 lto_file_read (lto_file *file, FILE *resolution_file)
371 {
372 struct lto_file_decl_data *file_data;
373 const char *data;
374 size_t len;
375 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions;
376
377 resolutions = lto_resolution_read (resolution_file, file);
378
379 file_data = GGC_NEW (struct lto_file_decl_data);
380 file_data->file_name = file->filename;
381 file_data->section_hash_table = lto_obj_build_section_table (file);
382 file_data->renaming_hash_table = lto_create_renaming_table ();
383
384 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
385 lto_read_decls (file_data, data, resolutions);
386 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
387
388 return file_data;
389 }
390
391 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
392 #define LTO_MMAP_IO 1
393 #endif
394
395 #if LTO_MMAP_IO
396 /* Page size of machine is used for mmap and munmap calls. */
397 static size_t page_mask;
398 #endif
399
400 /* Get the section data of length LEN from FILENAME starting at
401 OFFSET. The data segment must be freed by the caller when the
402 caller is finished. Returns NULL if all was not well. */
403
404 static char *
405 lto_read_section_data (struct lto_file_decl_data *file_data,
406 intptr_t offset, size_t len)
407 {
408 char *result;
409 static int fd = -1;
410 static char *fd_name;
411 #if LTO_MMAP_IO
412 intptr_t computed_len;
413 intptr_t computed_offset;
414 intptr_t diff;
415 #endif
416
417 /* Keep a single-entry file-descriptor cache. The last file we
418 touched will get closed at exit.
419 ??? Eventually we want to add a more sophisticated larger cache
420 or rather fix function body streaming to not stream them in
421 practically random order. */
422 if (fd != -1
423 && strcmp (fd_name, file_data->file_name) != 0)
424 {
425 free (fd_name);
426 close (fd);
427 fd = -1;
428 }
429 if (fd == -1)
430 {
431 fd_name = xstrdup (file_data->file_name);
432 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
433 if (fd == -1)
434 return NULL;
435 }
436
437 #if LTO_MMAP_IO
438 if (!page_mask)
439 {
440 size_t page_size = sysconf (_SC_PAGE_SIZE);
441 page_mask = ~(page_size - 1);
442 }
443
444 computed_offset = offset & page_mask;
445 diff = offset - computed_offset;
446 computed_len = len + diff;
447
448 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
449 fd, computed_offset);
450 if (result == MAP_FAILED)
451 return NULL;
452
453 return result + diff;
454 #else
455 result = (char *) xmalloc (len);
456 if (lseek (fd, offset, SEEK_SET) != offset
457 || read (fd, result, len) != (ssize_t) len)
458 {
459 free (result);
460 return NULL;
461 }
462
463 return result;
464 #endif
465 }
466
467
468 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
469 NAME will be NULL unless the section type is for a function
470 body. */
471
472 static const char *
473 get_section_data (struct lto_file_decl_data *file_data,
474 enum lto_section_type section_type,
475 const char *name,
476 size_t *len)
477 {
478 htab_t section_hash_table = file_data->section_hash_table;
479 struct lto_section_slot *f_slot;
480 struct lto_section_slot s_slot;
481 const char *section_name = lto_get_section_name (section_type, name);
482 char *data = NULL;
483
484 *len = 0;
485 s_slot.name = section_name;
486 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
487 if (f_slot)
488 {
489 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
490 *len = f_slot->len;
491 }
492
493 free (CONST_CAST (char *, section_name));
494 return data;
495 }
496
497
498 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
499 starts at OFFSET and has LEN bytes. */
500
501 static void
502 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
503 enum lto_section_type section_type ATTRIBUTE_UNUSED,
504 const char *name ATTRIBUTE_UNUSED,
505 const char *offset, size_t len ATTRIBUTE_UNUSED)
506 {
507 #if LTO_MMAP_IO
508 intptr_t computed_len;
509 intptr_t computed_offset;
510 intptr_t diff;
511 #endif
512
513 #if LTO_MMAP_IO
514 computed_offset = ((intptr_t) offset) & page_mask;
515 diff = (intptr_t) offset - computed_offset;
516 computed_len = len + diff;
517
518 munmap ((caddr_t) computed_offset, computed_len);
519 #else
520 free (CONST_CAST(char *, offset));
521 #endif
522 }
523
524 /* Vector of all cgraph node sets. */
525 static GTY (()) VEC(cgraph_node_set, gc) *lto_cgraph_node_sets;
526 static GTY (()) VEC(varpool_node_set, gc) *lto_varpool_node_sets;
527
528
529 /* Group cgrah nodes by input files. This is used mainly for testing
530 right now. */
531
532 static void
533 lto_1_to_1_map (void)
534 {
535 struct cgraph_node *node;
536 struct varpool_node *vnode;
537 struct lto_file_decl_data *file_data;
538 struct pointer_map_t *pmap;
539 struct pointer_map_t *vpmap;
540 cgraph_node_set set;
541 varpool_node_set vset;
542 void **slot;
543
544 timevar_push (TV_WHOPR_WPA);
545
546 lto_cgraph_node_sets = VEC_alloc (cgraph_node_set, gc, 1);
547 lto_varpool_node_sets = VEC_alloc (varpool_node_set, gc, 1);
548
549 pmap = pointer_map_create ();
550 vpmap = pointer_map_create ();
551
552 for (node = cgraph_nodes; node; node = node->next)
553 {
554 /* We will get proper partition based on function they are inlined to or
555 cloned from. */
556 if (node->global.inlined_to || node->clone_of)
557 continue;
558 /* Nodes without a body do not need partitioning. */
559 if (!node->analyzed)
560 continue;
561
562 file_data = node->local.lto_file_data;
563 gcc_assert (!node->same_body_alias && file_data);
564
565 slot = pointer_map_contains (pmap, file_data);
566 if (slot)
567 set = (cgraph_node_set) *slot;
568 else
569 {
570 set = cgraph_node_set_new ();
571 slot = pointer_map_insert (pmap, file_data);
572 *slot = set;
573 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
574 vset = varpool_node_set_new ();
575 slot = pointer_map_insert (vpmap, file_data);
576 *slot = vset;
577 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
578 }
579
580 cgraph_node_set_add (set, node);
581 }
582
583 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
584 {
585 if (vnode->alias || !vnode->needed)
586 continue;
587 slot = pointer_map_contains (vpmap, file_data);
588 if (slot)
589 vset = (varpool_node_set) *slot;
590 else
591 {
592 set = cgraph_node_set_new ();
593 slot = pointer_map_insert (pmap, file_data);
594 *slot = set;
595 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
596 vset = varpool_node_set_new ();
597 slot = pointer_map_insert (vpmap, file_data);
598 *slot = vset;
599 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
600 }
601
602 varpool_node_set_add (vset, vnode);
603 }
604
605 /* If the cgraph is empty, create one cgraph node set so that there is still
606 an output file for any variables that need to be exported in a DSO. */
607 if (!lto_cgraph_node_sets)
608 {
609 set = cgraph_node_set_new ();
610 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
611 vset = varpool_node_set_new ();
612 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
613 }
614
615 pointer_map_destroy (pmap);
616 pointer_map_destroy (vpmap);
617
618 timevar_pop (TV_WHOPR_WPA);
619
620 lto_stats.num_cgraph_partitions += VEC_length (cgraph_node_set,
621 lto_cgraph_node_sets);
622 }
623
624
625 /* Add inlined clone NODE and its master clone to SET, if NODE itself has
626 inlined callees, recursively add the callees. */
627
628 static void
629 lto_add_inline_clones (cgraph_node_set set, struct cgraph_node *node,
630 bitmap original_decls)
631 {
632 struct cgraph_node *callee;
633 struct cgraph_edge *edge;
634
635 cgraph_node_set_add (set, node);
636
637 /* Check to see if NODE has any inlined callee. */
638 for (edge = node->callees; edge != NULL; edge = edge->next_callee)
639 {
640 callee = edge->callee;
641 if (callee->global.inlined_to != NULL)
642 lto_add_inline_clones (set, callee, original_decls);
643 }
644 }
645
646 /* Compute the transitive closure of inlining of SET based on the
647 information in the callgraph. Returns a bitmap of decls that have
648 been inlined into SET indexed by UID. */
649
650 static void
651 lto_add_all_inlinees (cgraph_node_set set)
652 {
653 cgraph_node_set_iterator csi;
654 struct cgraph_node *node;
655 bitmap original_nodes = lto_bitmap_alloc ();
656 bitmap original_decls = lto_bitmap_alloc ();
657 bool changed;
658
659 /* We are going to iterate SET while adding to it, mark all original
660 nodes so that we only add node inlined to original nodes. */
661 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
662 {
663 bitmap_set_bit (original_nodes, csi_node (csi)->uid);
664 bitmap_set_bit (original_decls, DECL_UID (csi_node (csi)->decl));
665 }
666
667 /* Some of the original nodes might not be needed anymore.
668 Remove them. */
669 do
670 {
671 changed = false;
672 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
673 {
674 struct cgraph_node *inlined_to;
675 node = csi_node (csi);
676
677 /* NODE was not inlined. We still need it. */
678 if (!node->global.inlined_to)
679 continue;
680
681 inlined_to = node->global.inlined_to;
682
683 /* NODE should have only one caller. */
684 gcc_assert (!node->callers->next_caller);
685
686 if (!bitmap_bit_p (original_nodes, inlined_to->uid))
687 {
688 bitmap_clear_bit (original_nodes, node->uid);
689 cgraph_node_set_remove (set, node);
690 changed = true;
691 }
692 }
693 }
694 while (changed);
695
696 /* Transitively add to SET all the inline clones for every node that
697 has been inlined. */
698 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
699 {
700 node = csi_node (csi);
701 if (bitmap_bit_p (original_nodes, node->uid))
702 lto_add_inline_clones (set, node, original_decls);
703 }
704
705 lto_bitmap_free (original_nodes);
706 lto_bitmap_free (original_decls);
707 }
708
709 /* Find out all static decls that need to be promoted to global because
710 of cross file sharing. This function must be run in the WPA mode after
711 all inlinees are added. */
712
713 static void
714 lto_promote_cross_file_statics (void)
715 {
716 struct varpool_node *vnode;
717 unsigned i, n_sets;
718 cgraph_node_set set;
719 varpool_node_set vset;
720 cgraph_node_set_iterator csi;
721 varpool_node_set_iterator vsi;
722
723 gcc_assert (flag_wpa);
724
725 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
726 for (i = 0; i < n_sets; i++)
727 {
728 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
729 vset = VEC_index (varpool_node_set, lto_varpool_node_sets, i);
730
731 /* If node has either address taken (and we have no clue from where)
732 or it is called from other partition, it needs to be globalized. */
733 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
734 {
735 struct cgraph_node *node = csi_node (csi);
736 if (node->local.externally_visible)
737 continue;
738 if (node->clone_of || node->global.inlined_to)
739 continue;
740 if (!DECL_EXTERNAL (node->decl)
741 && (referenced_from_other_partition_p (&node->ref_list, set, vset)
742 || reachable_from_other_partition_p (node, set)))
743 {
744 gcc_assert (flag_wpa);
745 TREE_PUBLIC (node->decl) = 1;
746 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
747 if (node->same_body)
748 {
749 struct cgraph_node *alias;
750 for (alias = node->same_body;
751 alias; alias = alias->next)
752 {
753 TREE_PUBLIC (alias->decl) = 1;
754 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
755 }
756 }
757 }
758 }
759 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
760 {
761 vnode = vsi_node (vsi);
762 /* Constant pool references use internal labels and thus can not
763 be made global. It is sensible to keep those ltrans local to
764 allow better optimization. */
765 if (!DECL_IN_CONSTANT_POOL (vnode->decl)
766 && !vnode->externally_visible && vnode->analyzed
767 && referenced_from_other_partition_p (&vnode->ref_list, set, vset))
768 {
769 gcc_assert (flag_wpa);
770 TREE_PUBLIC (vnode->decl) = 1;
771 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
772 }
773 }
774
775 }
776 }
777
778
779 /* Given a file name FNAME, return a string with FNAME prefixed with '*'. */
780
781 static char *
782 prefix_name_with_star (const char *fname)
783 {
784 char *star_fname;
785 size_t len;
786
787 len = strlen (fname) + 1 + 1;
788 star_fname = XNEWVEC (char, len);
789 snprintf (star_fname, len, "*%s", fname);
790
791 return star_fname;
792 }
793
794
795 /* Return a copy of FNAME without the .o extension. */
796
797 static char *
798 strip_extension (const char *fname)
799 {
800 char *s = XNEWVEC (char, strlen (fname) - 2 + 1);
801 gcc_assert (strstr (fname, ".o"));
802 snprintf (s, strlen (fname) - 2 + 1, "%s", fname);
803
804 return s;
805 }
806
807
808 /* Return a file name associated with cgraph node set SET. This may
809 be a new temporary file name if SET needs to be processed by
810 LTRANS, or the original file name if all the nodes in SET belong to
811 the same input file. */
812
813 static char *
814 get_filename_for_set (cgraph_node_set set)
815 {
816 char *fname = NULL;
817 static const size_t max_fname_len = 100;
818
819 /* Create a new temporary file to store SET. To facilitate
820 debugging, use file names from SET as part of the new
821 temporary file name. */
822 cgraph_node_set_iterator si;
823 struct pointer_set_t *pset = pointer_set_create ();
824 for (si = csi_start (set); !csi_end_p (si); csi_next (&si))
825 {
826 struct cgraph_node *n = csi_node (si);
827 const char *node_fname;
828 char *f;
829
830 /* Don't use the same file name more than once. */
831 if (pointer_set_insert (pset, n->local.lto_file_data))
832 continue;
833
834 /* The first file name found in SET determines the output
835 directory. For the remaining files, we use their
836 base names. */
837 node_fname = n->local.lto_file_data->file_name;
838 if (fname == NULL)
839 {
840 fname = strip_extension (node_fname);
841 continue;
842 }
843
844 f = strip_extension (lbasename (node_fname));
845
846 /* If the new name causes an excessively long file name,
847 make the last component "___" to indicate overflow. */
848 if (strlen (fname) + strlen (f) > max_fname_len - 3)
849 {
850 fname = reconcat (fname, fname, "___", NULL);
851 break;
852 }
853 else
854 {
855 fname = reconcat (fname, fname, "_", f, NULL);
856 free (f);
857 }
858 }
859
860 pointer_set_destroy (pset);
861
862 if (!fname)
863 {
864 /* Since SET does not need to be processed by LTRANS, use
865 the original file name and mark it with a '*' prefix so that
866 lto_execute_ltrans knows not to process it. */
867 cgraph_node_set_iterator si = csi_start (set);
868 struct cgraph_node *first = csi_node (si);
869 fname = prefix_name_with_star (first->local.lto_file_data->file_name);
870 }
871 else
872 {
873 /* Add the extension .wpa.o to indicate that this file has been
874 produced by WPA. */
875 fname = reconcat (fname, fname, ".wpa.o", NULL);
876 gcc_assert (fname);
877 }
878
879 return fname;
880 }
881
882 static lto_file *current_lto_file;
883
884
885 /* Write all output files in WPA mode. Returns a NULL-terminated array of
886 output file names. */
887
888 static char **
889 lto_wpa_write_files (void)
890 {
891 char **output_files;
892 unsigned i, n_sets, last_out_file_ix, num_out_files;
893 lto_file *file;
894 cgraph_node_set set;
895 varpool_node_set vset;
896
897 timevar_push (TV_WHOPR_WPA);
898
899 /* Include all inlined functions and determine what sets need to be
900 compiled by LTRANS. After this loop, only those sets that
901 contain callgraph nodes from more than one file will need to be
902 compiled by LTRANS. */
903 for (i = 0; VEC_iterate (cgraph_node_set, lto_cgraph_node_sets, i, set); i++)
904 {
905 lto_add_all_inlinees (set);
906 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
907 set->nodes);
908 }
909
910 /* After adding all inlinees, find out statics that need to be promoted
911 to globals because of cross-file inlining. */
912 lto_promote_cross_file_statics ();
913
914 timevar_pop (TV_WHOPR_WPA);
915
916 timevar_push (TV_WHOPR_WPA_IO);
917
918 /* The number of output files depends on the number of input files
919 and how many callgraph node sets we create. Reserve enough space
920 for the maximum of these two. */
921 num_out_files = MAX (VEC_length (cgraph_node_set, lto_cgraph_node_sets),
922 num_in_fnames);
923 output_files = XNEWVEC (char *, num_out_files + 1);
924
925 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
926 for (i = 0; i < n_sets; i++)
927 {
928 char *temp_filename;
929
930 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
931 vset = VEC_index (varpool_node_set, lto_varpool_node_sets, i);
932 temp_filename = get_filename_for_set (set);
933 output_files[i] = temp_filename;
934
935 if (cgraph_node_set_nonempty_p (set) || varpool_node_set_nonempty_p (vset))
936 {
937 /* Write all the nodes in SET to TEMP_FILENAME. */
938 file = lto_obj_file_open (temp_filename, true);
939 if (!file)
940 fatal_error ("lto_obj_file_open() failed");
941
942 if (!quiet_flag)
943 fprintf (stderr, " %s", temp_filename);
944
945 lto_set_current_out_file (file);
946
947 ipa_write_optimization_summaries (set, vset);
948
949 lto_set_current_out_file (NULL);
950 lto_obj_file_close (file);
951 }
952 }
953
954 last_out_file_ix = n_sets;
955
956 lto_stats.num_output_files += n_sets;
957
958 output_files[last_out_file_ix] = NULL;
959
960 timevar_pop (TV_WHOPR_WPA_IO);
961
962 return output_files;
963 }
964
965 /* Perform local transformations (LTRANS) on the files in the NULL-terminated
966 FILES array. These should have been written previously by
967 lto_wpa_write_files (). Transformations are performed via executing
968 COLLECT_GCC for reach file. */
969
970 static void
971 lto_write_ltrans_list (char *const *files)
972 {
973 FILE *ltrans_output_list_stream = NULL;
974 unsigned i;
975
976 /* Open the LTRANS output list. */
977 if (!ltrans_output_list)
978 error ("no LTRANS output filename provided");
979
980 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
981 if (ltrans_output_list_stream == NULL)
982 error ("opening LTRANS output list %s: %m", ltrans_output_list);
983
984 for (i = 0; files[i]; ++i)
985 {
986 size_t len;
987
988 len = strlen (files[i]);
989 if (fwrite (files[i], 1, len, ltrans_output_list_stream) < len
990 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
991 error ("writing to LTRANS output list %s: %m",
992 ltrans_output_list);
993 }
994
995 /* Close the LTRANS output list. */
996 if (fclose (ltrans_output_list_stream))
997 error ("closing LTRANS output list %s: %m", ltrans_output_list);
998 }
999
1000
1001 typedef struct {
1002 struct pointer_set_t *seen;
1003 } lto_fixup_data_t;
1004
1005 #define LTO_FIXUP_SUBTREE(t) \
1006 do \
1007 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1008 while (0)
1009
1010 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
1011 do \
1012 { \
1013 if (t) \
1014 (t) = gimple_register_type (t); \
1015 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1016 } \
1017 while (0)
1018
1019 static tree lto_fixup_tree (tree *, int *, void *);
1020
1021 /* Return true if T does not need to be fixed up recursively. */
1022
1023 static inline bool
1024 no_fixup_p (tree t)
1025 {
1026 return (t == NULL
1027 || CONSTANT_CLASS_P (t)
1028 || TREE_CODE (t) == IDENTIFIER_NODE);
1029 }
1030
1031 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
1032
1033 static void
1034 lto_fixup_common (tree t, void *data)
1035 {
1036 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
1037 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
1038 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
1039 First remove us from any pointer list we are on. */
1040 if (TREE_CODE (t) == POINTER_TYPE)
1041 {
1042 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
1043 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
1044 else
1045 {
1046 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
1047 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
1048 tem = TYPE_NEXT_PTR_TO (tem);
1049 if (tem)
1050 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
1051 }
1052 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
1053 }
1054 else if (TREE_CODE (t) == REFERENCE_TYPE)
1055 {
1056 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
1057 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
1058 else
1059 {
1060 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
1061 while (tem && TYPE_NEXT_REF_TO (tem) != t)
1062 tem = TYPE_NEXT_REF_TO (tem);
1063 if (tem)
1064 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
1065 }
1066 TYPE_NEXT_REF_TO (t) = NULL_TREE;
1067 }
1068
1069 /* Fixup our type. */
1070 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1071
1072 /* Second put us on the list of pointers of the new pointed-to type
1073 if we are a main variant. This is done in lto_fixup_type after
1074 fixing up our main variant. */
1075
1076 /* This is not very efficient because we cannot do tail-recursion with
1077 a long chain of trees. */
1078 LTO_FIXUP_SUBTREE (TREE_CHAIN (t));
1079 }
1080
1081 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
1082
1083 static void
1084 lto_fixup_decl_minimal (tree t, void *data)
1085 {
1086 lto_fixup_common (t, data);
1087 LTO_FIXUP_SUBTREE (DECL_NAME (t));
1088 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t));
1089 }
1090
1091 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1092
1093 static void
1094 lto_fixup_decl_common (tree t, void *data)
1095 {
1096 lto_fixup_decl_minimal (t, data);
1097 LTO_FIXUP_SUBTREE (DECL_SIZE (t));
1098 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t));
1099 LTO_FIXUP_SUBTREE (DECL_INITIAL (t));
1100 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t));
1101 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t));
1102 }
1103
1104 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1105
1106 static void
1107 lto_fixup_decl_with_vis (tree t, void *data)
1108 {
1109 lto_fixup_decl_common (t, data);
1110
1111 /* Accessor macro has side-effects, use field-name here. */
1112 LTO_FIXUP_SUBTREE (t->decl_with_vis.assembler_name);
1113
1114 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t)));
1115 }
1116
1117 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1118
1119 static void
1120 lto_fixup_decl_non_common (tree t, void *data)
1121 {
1122 lto_fixup_decl_with_vis (t, data);
1123 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t));
1124 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t));
1125 LTO_FIXUP_SUBTREE (DECL_VINDEX (t));
1126
1127 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1128 gcc_assert (no_fixup_p (t->decl_non_common.saved_tree));
1129 }
1130
1131 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1132
1133 static void
1134 lto_fixup_function (tree t, void *data)
1135 {
1136 lto_fixup_decl_non_common (t, data);
1137 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t));
1138 }
1139
1140 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1141
1142 static void
1143 lto_fixup_field_decl (tree t, void *data)
1144 {
1145 lto_fixup_decl_common (t, data);
1146 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t));
1147 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t));
1148 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t));
1149 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t)));
1150 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t));
1151 }
1152
1153 /* Fix up fields of a type T. DATA points to fix-up states. */
1154
1155 static void
1156 lto_fixup_type (tree t, void *data)
1157 {
1158 tree tem, mv;
1159
1160 lto_fixup_common (t, data);
1161 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t));
1162 LTO_FIXUP_SUBTREE (TYPE_SIZE (t));
1163 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t));
1164 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t));
1165 LTO_FIXUP_SUBTREE (TYPE_NAME (t));
1166
1167 /* Accessors are for derived node types only. */
1168 if (!POINTER_TYPE_P (t))
1169 LTO_FIXUP_SUBTREE (t->type.minval);
1170 LTO_FIXUP_SUBTREE (t->type.maxval);
1171
1172 /* Accessor is for derived node types only. */
1173 LTO_FIXUP_SUBTREE (t->type.binfo);
1174
1175 if (TYPE_CONTEXT (t))
1176 {
1177 if (TYPE_P (TYPE_CONTEXT (t)))
1178 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1179 else
1180 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1181 }
1182 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CANONICAL (t));
1183
1184 /* The following re-creates proper variant lists while fixing up
1185 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1186 variant list state before fixup is broken. */
1187
1188 /* Remove us from our main variant list if we are not the variant leader. */
1189 if (TYPE_MAIN_VARIANT (t) != t)
1190 {
1191 tem = TYPE_MAIN_VARIANT (t);
1192 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1193 tem = TYPE_NEXT_VARIANT (tem);
1194 if (tem)
1195 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1196 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1197 }
1198
1199 /* Query our new main variant. */
1200 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1201
1202 /* If we were the variant leader and we get replaced ourselves drop
1203 all variants from our list. */
1204 if (TYPE_MAIN_VARIANT (t) == t
1205 && mv != t)
1206 {
1207 tem = t;
1208 while (tem)
1209 {
1210 tree tem2 = TYPE_NEXT_VARIANT (tem);
1211 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1212 tem = tem2;
1213 }
1214 }
1215
1216 /* If we are not our own variant leader link us into our new leaders
1217 variant list. */
1218 if (mv != t)
1219 {
1220 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1221 TYPE_NEXT_VARIANT (mv) = t;
1222 }
1223
1224 /* Finally adjust our main variant and fix it up. */
1225 TYPE_MAIN_VARIANT (t) = mv;
1226 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1227
1228 /* As the second step of reconstructing the pointer chains put us
1229 on the list of pointers of the new pointed-to type
1230 if we are a main variant. See lto_fixup_common for the first step. */
1231 if (TREE_CODE (t) == POINTER_TYPE
1232 && TYPE_MAIN_VARIANT (t) == t)
1233 {
1234 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1235 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1236 }
1237 else if (TREE_CODE (t) == REFERENCE_TYPE
1238 && TYPE_MAIN_VARIANT (t) == t)
1239 {
1240 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1241 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1242 }
1243 }
1244
1245 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1246
1247 static void
1248 lto_fixup_binfo (tree t, void *data)
1249 {
1250 unsigned HOST_WIDE_INT i, n;
1251 tree base, saved_base;
1252
1253 lto_fixup_common (t, data);
1254 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1255 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1256 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1257 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1258 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1259 for (i = 0; i < n; i++)
1260 {
1261 saved_base = base = BINFO_BASE_ACCESS (t, i);
1262 LTO_FIXUP_SUBTREE (base);
1263 if (base != saved_base)
1264 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1265 }
1266 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1267 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1268 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1269 n = BINFO_N_BASE_BINFOS (t);
1270 for (i = 0; i < n; i++)
1271 {
1272 saved_base = base = BINFO_BASE_BINFO (t, i);
1273 LTO_FIXUP_SUBTREE (base);
1274 if (base != saved_base)
1275 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1276 }
1277 }
1278
1279 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1280
1281 static void
1282 lto_fixup_constructor (tree t, void *data)
1283 {
1284 unsigned HOST_WIDE_INT idx;
1285 constructor_elt *ce;
1286
1287 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1288
1289 for (idx = 0;
1290 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1291 idx++)
1292 {
1293 LTO_FIXUP_SUBTREE (ce->index);
1294 LTO_FIXUP_SUBTREE (ce->value);
1295 }
1296 }
1297
1298 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1299 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1300 DATA is a pointer set to record visited nodes. */
1301
1302 static tree
1303 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1304 {
1305 tree t;
1306 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1307 tree prevailing;
1308
1309 t = *tp;
1310 *walk_subtrees = 0;
1311 if (!t || pointer_set_contains (fixup_data->seen, t))
1312 return NULL;
1313
1314 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1315 {
1316 prevailing = lto_symtab_prevailing_decl (t);
1317
1318 if (t != prevailing)
1319 {
1320 /* Also replace t with prevailing defintion. We don't want to
1321 insert the other defintion in the seen set as we want to
1322 replace all instances of it. */
1323 *tp = prevailing;
1324 t = prevailing;
1325 }
1326 }
1327 else if (TYPE_P (t))
1328 {
1329 /* Replace t with the prevailing type. We don't want to insert the
1330 other type in the seen set as we want to replace all instances of it. */
1331 t = gimple_register_type (t);
1332 *tp = t;
1333 }
1334
1335 if (pointer_set_insert (fixup_data->seen, t))
1336 return NULL;
1337
1338 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1339 Hence we do special processing here for those kind of nodes. */
1340 switch (TREE_CODE (t))
1341 {
1342 case FIELD_DECL:
1343 lto_fixup_field_decl (t, data);
1344 break;
1345
1346 case LABEL_DECL:
1347 case CONST_DECL:
1348 case PARM_DECL:
1349 case RESULT_DECL:
1350 case IMPORTED_DECL:
1351 lto_fixup_decl_common (t, data);
1352 break;
1353
1354 case VAR_DECL:
1355 lto_fixup_decl_with_vis (t, data);
1356 break;
1357
1358 case TYPE_DECL:
1359 lto_fixup_decl_non_common (t, data);
1360 break;
1361
1362 case FUNCTION_DECL:
1363 lto_fixup_function (t, data);
1364 break;
1365
1366 case TREE_BINFO:
1367 lto_fixup_binfo (t, data);
1368 break;
1369
1370 default:
1371 if (TYPE_P (t))
1372 lto_fixup_type (t, data);
1373 else if (TREE_CODE (t) == CONSTRUCTOR)
1374 lto_fixup_constructor (t, data);
1375 else if (CONSTANT_CLASS_P (t))
1376 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1377 else if (EXPR_P (t))
1378 {
1379 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1380 lto_fixup_common (t, data);
1381 LTO_FIXUP_SUBTREE (t->exp.block);
1382 *walk_subtrees = 1;
1383 }
1384 else
1385 {
1386 /* Let walk_tree handle sub-trees. */
1387 *walk_subtrees = 1;
1388 }
1389 }
1390
1391 return NULL;
1392 }
1393
1394 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1395 replaces var and function decls with the corresponding prevailing def and
1396 records the old decl in the free-list in DATA. We also record visted nodes
1397 in the seen-set in DATA to avoid multiple visit for nodes that need not
1398 to be replaced. */
1399
1400 static void
1401 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1402 {
1403 unsigned i, si;
1404 struct lto_tree_ref_table *table;
1405
1406 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1407 we still need to walk from all DECLs to find the reachable
1408 FUNCTION_DECLs and VAR_DECLs. */
1409 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1410 {
1411 table = &state->streams[si];
1412 for (i = 0; i < table->size; i++)
1413 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1414 }
1415 }
1416
1417 /* A callback of htab_traverse. Just extract a state from SLOT and the
1418 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1419
1420 static int
1421 lto_fixup_state_aux (void **slot, void *aux)
1422 {
1423 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1424 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1425 return 1;
1426 }
1427
1428 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1429 prevailing one. */
1430
1431 static void
1432 lto_fixup_decls (struct lto_file_decl_data **files)
1433 {
1434 unsigned int i;
1435 tree decl;
1436 struct pointer_set_t *seen = pointer_set_create ();
1437 lto_fixup_data_t data;
1438
1439 data.seen = seen;
1440 for (i = 0; files[i]; i++)
1441 {
1442 struct lto_file_decl_data *file = files[i];
1443 struct lto_in_decl_state *state = file->global_decl_state;
1444 lto_fixup_state (state, &data);
1445
1446 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1447 }
1448
1449 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1450 {
1451 tree saved_decl = decl;
1452 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1453 if (decl != saved_decl)
1454 VEC_replace (tree, lto_global_var_decls, i, decl);
1455 }
1456
1457 VEC_free (tree, gc, lto_global_var_decls);
1458 lto_global_var_decls = NULL;
1459 pointer_set_destroy (seen);
1460 }
1461
1462 /* Read the options saved from each file in the command line. Called
1463 from lang_hooks.post_options which is called by process_options
1464 right before all the options are used to initialize the compiler.
1465 This assumes that decode_options has already run, so the
1466 num_in_fnames and in_fnames are properly set.
1467
1468 Note that this assumes that all the files had been compiled with
1469 the same options, which is not a good assumption. In general,
1470 options ought to be read from all the files in the set and merged.
1471 However, it is still unclear what the merge rules should be. */
1472
1473 void
1474 lto_read_all_file_options (void)
1475 {
1476 size_t i;
1477
1478 /* Clear any file options currently saved. */
1479 lto_clear_file_options ();
1480
1481 /* Set the hooks to read ELF sections. */
1482 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1483
1484 for (i = 0; i < num_in_fnames; i++)
1485 {
1486 struct lto_file_decl_data *file_data;
1487 lto_file *file = lto_obj_file_open (in_fnames[i], false);
1488 if (!file)
1489 break;
1490
1491 file_data = XCNEW (struct lto_file_decl_data);
1492 file_data->file_name = file->filename;
1493 file_data->section_hash_table = lto_obj_build_section_table (file);
1494
1495 lto_read_file_options (file_data);
1496
1497 lto_obj_file_close (file);
1498 htab_delete (file_data->section_hash_table);
1499 free (file_data);
1500 }
1501
1502 /* Apply globally the options read from all the files. */
1503 lto_reissue_options ();
1504 }
1505
1506 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1507
1508 /* Read all the symbols from the input files FNAMES. NFILES is the
1509 number of files requested in the command line. Instantiate a
1510 global call graph by aggregating all the sub-graphs found in each
1511 file. */
1512
1513 static void
1514 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1515 {
1516 unsigned int i, last_file_ix;
1517 FILE *resolution;
1518 struct cgraph_node *node;
1519
1520 lto_stats.num_input_files = nfiles;
1521
1522 timevar_push (TV_IPA_LTO_DECL_IO);
1523
1524 /* Set the hooks so that all of the ipa passes can read in their data. */
1525 all_file_decl_data = GGC_CNEWVEC (struct lto_file_decl_data *, nfiles + 1);
1526 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1527
1528 /* Read the resolution file. */
1529 resolution = NULL;
1530 if (resolution_file_name)
1531 {
1532 int t;
1533 unsigned num_objects;
1534
1535 resolution = fopen (resolution_file_name, "r");
1536 if (resolution == NULL)
1537 fatal_error ("could not open symbol resolution file: %s",
1538 xstrerror (errno));
1539
1540 t = fscanf (resolution, "%u", &num_objects);
1541 gcc_assert (t == 1);
1542
1543 /* True, since the plugin splits the archives. */
1544 gcc_assert (num_objects == nfiles);
1545 }
1546
1547 if (!quiet_flag)
1548 fprintf (stderr, "Reading object files:");
1549
1550 /* Read all of the object files specified on the command line. */
1551 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1552 {
1553 struct lto_file_decl_data *file_data = NULL;
1554 if (!quiet_flag)
1555 {
1556 fprintf (stderr, " %s", fnames[i]);
1557 fflush (stderr);
1558 }
1559
1560 current_lto_file = lto_obj_file_open (fnames[i], false);
1561 if (!current_lto_file)
1562 break;
1563
1564 file_data = lto_file_read (current_lto_file, resolution);
1565 if (!file_data)
1566 break;
1567
1568 all_file_decl_data[last_file_ix++] = file_data;
1569
1570 lto_obj_file_close (current_lto_file);
1571 current_lto_file = NULL;
1572 ggc_collect ();
1573 }
1574
1575 if (resolution_file_name)
1576 fclose (resolution);
1577
1578 all_file_decl_data[last_file_ix] = NULL;
1579
1580 /* Set the hooks so that all of the ipa passes can read in their data. */
1581 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1582
1583 timevar_pop (TV_IPA_LTO_DECL_IO);
1584
1585 if (!quiet_flag)
1586 fprintf (stderr, "\nReading the callgraph\n");
1587
1588 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1589 /* Read the callgraph. */
1590 input_cgraph ();
1591 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1592
1593 if (!quiet_flag)
1594 fprintf (stderr, "Merging declarations\n");
1595
1596 timevar_push (TV_IPA_LTO_DECL_MERGE);
1597 /* Merge global decls. */
1598 lto_symtab_merge_decls ();
1599
1600 /* Fixup all decls and types and free the type hash tables. */
1601 lto_fixup_decls (all_file_decl_data);
1602 free_gimple_type_tables ();
1603 ggc_collect ();
1604
1605 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1606 /* Each pass will set the appropriate timer. */
1607
1608 if (!quiet_flag)
1609 fprintf (stderr, "Reading summaries\n");
1610
1611 /* Read the IPA summary data. */
1612 if (flag_ltrans)
1613 ipa_read_optimization_summaries ();
1614 else
1615 ipa_read_summaries ();
1616
1617 /* Finally merge the cgraph according to the decl merging decisions. */
1618 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1619 lto_symtab_merge_cgraph_nodes ();
1620 ggc_collect ();
1621
1622 if (flag_ltrans)
1623 for (node = cgraph_nodes; node; node = node->next)
1624 {
1625 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1626 summaries computed and needs to apply changes. At the moment WHOPR only
1627 supports inlining, so we can push it here by hand. In future we need to stream
1628 this field into ltrans compilation. */
1629 if (node->analyzed)
1630 VEC_safe_push (ipa_opt_pass, heap,
1631 node->ipa_transforms_to_apply,
1632 (ipa_opt_pass)&pass_ipa_inline);
1633 }
1634 lto_symtab_free ();
1635
1636 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1637
1638 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1639
1640 /* FIXME lto. This loop needs to be changed to use the pass manager to
1641 call the ipa passes directly. */
1642 if (!errorcount)
1643 for (i = 0; i < last_file_ix; i++)
1644 {
1645 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1646 lto_materialize_constructors_and_inits (file_data);
1647 }
1648
1649 /* Indicate that the cgraph is built and ready. */
1650 cgraph_function_flags_ready = true;
1651
1652 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1653 ggc_free (all_file_decl_data);
1654 all_file_decl_data = NULL;
1655 }
1656
1657
1658 /* Materialize all the bodies for all the nodes in the callgraph. */
1659
1660 static void
1661 materialize_cgraph (void)
1662 {
1663 tree decl;
1664 struct cgraph_node *node;
1665 unsigned i;
1666 timevar_id_t lto_timer;
1667
1668 if (!quiet_flag)
1669 fprintf (stderr,
1670 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1671
1672
1673 /* Now that we have input the cgraph, we need to clear all of the aux
1674 nodes and read the functions if we are not running in WPA mode. */
1675 timevar_push (TV_IPA_LTO_GIMPLE_IO);
1676
1677 for (node = cgraph_nodes; node; node = node->next)
1678 {
1679 /* Some cgraph nodes get created on the fly, and they don't need
1680 to be materialized. For instance, nodes for nested functions
1681 where the parent function was not streamed out or builtin
1682 functions. Additionally, builtin functions should not be
1683 materialized and may, in fact, cause confusion because there
1684 may be a regular function in the file whose assembler name
1685 matches that of the function.
1686 See gcc.c-torture/execute/20030125-1.c and
1687 gcc.c-torture/execute/921215-1.c. */
1688 if (node->local.lto_file_data
1689 && !DECL_IS_BUILTIN (node->decl))
1690 {
1691 announce_function (node->decl);
1692 lto_materialize_function (node);
1693 lto_stats.num_input_cgraph_nodes++;
1694 }
1695 }
1696
1697 timevar_pop (TV_IPA_LTO_GIMPLE_IO);
1698
1699 /* Start the appropriate timer depending on the mode that we are
1700 operating in. */
1701 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1702 : (flag_ltrans) ? TV_WHOPR_LTRANS
1703 : TV_LTO;
1704 timevar_push (lto_timer);
1705
1706 current_function_decl = NULL;
1707 set_cfun (NULL);
1708
1709 /* Inform the middle end about the global variables we have seen. */
1710 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1711 rest_of_decl_compilation (decl, 1, 0);
1712
1713 if (!quiet_flag)
1714 fprintf (stderr, "\n");
1715
1716 timevar_pop (lto_timer);
1717 }
1718
1719
1720 /* Perform whole program analysis (WPA) on the callgraph and write out the
1721 optimization plan. */
1722
1723 static void
1724 do_whole_program_analysis (void)
1725 {
1726 char **output_files;
1727
1728 /* Note that since we are in WPA mode, materialize_cgraph will not
1729 actually read in all the function bodies. It only materializes
1730 the decls and cgraph nodes so that analysis can be performed. */
1731 materialize_cgraph ();
1732
1733 /* Reading in the cgraph uses different timers, start timing WPA now. */
1734 timevar_push (TV_WHOPR_WPA);
1735
1736 if (pre_ipa_mem_report)
1737 {
1738 fprintf (stderr, "Memory consumption before IPA\n");
1739 dump_memory_report (false);
1740 }
1741
1742 cgraph_function_flags_ready = true;
1743 bitmap_obstack_initialize (NULL);
1744 ipa_register_cgraph_hooks ();
1745 cgraph_state = CGRAPH_STATE_IPA_SSA;
1746
1747 execute_ipa_pass_list (all_regular_ipa_passes);
1748
1749 verify_cgraph ();
1750 bitmap_obstack_release (NULL);
1751
1752 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1753 timevar_pop (TV_WHOPR_WPA);
1754
1755 lto_1_to_1_map ();
1756
1757 if (!quiet_flag)
1758 {
1759 fprintf (stderr, "\nStreaming out");
1760 fflush (stderr);
1761 }
1762 output_files = lto_wpa_write_files ();
1763 ggc_collect ();
1764 if (!quiet_flag)
1765 fprintf (stderr, "\n");
1766
1767 if (post_ipa_mem_report)
1768 {
1769 fprintf (stderr, "Memory consumption after IPA\n");
1770 dump_memory_report (false);
1771 }
1772
1773 /* Show the LTO report before launching LTRANS. */
1774 if (flag_lto_report)
1775 print_lto_report ();
1776
1777 lto_write_ltrans_list (output_files);
1778
1779 XDELETEVEC (output_files);
1780 }
1781
1782
1783 /* Main entry point for the GIMPLE front end. This front end has
1784 three main personalities:
1785
1786 - LTO (-flto). All the object files on the command line are
1787 loaded in memory and processed as a single translation unit.
1788 This is the traditional link-time optimization behavior.
1789
1790 - WPA (-fwpa). Only the callgraph and summary information for
1791 files in the command file are loaded. A single callgraph
1792 (without function bodies) is instantiated for the whole set of
1793 files. IPA passes are only allowed to analyze the call graph
1794 and make transformation decisions. The callgraph is
1795 partitioned, each partition is written to a new object file
1796 together with the transformation decisions.
1797
1798 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
1799 summary files from running again. Since WPA computed summary
1800 information and decided what transformations to apply, LTRANS
1801 simply applies them. */
1802
1803 void
1804 lto_main (int debug_p ATTRIBUTE_UNUSED)
1805 {
1806 lto_init_reader ();
1807
1808 /* Read all the symbols and call graph from all the files in the
1809 command line. */
1810 read_cgraph_and_symbols (num_in_fnames, in_fnames);
1811
1812 if (!errorcount)
1813 {
1814 /* If WPA is enabled analyze the whole call graph and create an
1815 optimization plan. Otherwise, read in all the function
1816 bodies and continue with optimization. */
1817 if (flag_wpa)
1818 do_whole_program_analysis ();
1819 else
1820 {
1821 materialize_cgraph ();
1822
1823 /* Let the middle end know that we have read and merged all of
1824 the input files. */
1825 cgraph_optimize ();
1826
1827 /* FIXME lto, if the processes spawned by WPA fail, we miss
1828 the chance to print WPA's report, so WPA will call
1829 print_lto_report before launching LTRANS. If LTRANS was
1830 launched directly by the driver we would not need to do
1831 this. */
1832 if (flag_lto_report)
1833 print_lto_report ();
1834 }
1835 }
1836 }
1837
1838 #include "gt-lto-lto.h"