]> gcc.gnu.org Git - gcc.git/blob - gcc/lto/lto.c
lto-cgraph.c (output_varpool): Forward declare; work on encoder.
[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 || node->same_body_alias)
560 continue;
561 /* We only need to partition the nodes that we read from the
562 gimple bytecode files. */
563 file_data = node->local.lto_file_data;
564 if (file_data == NULL)
565 continue;
566
567 slot = pointer_map_contains (pmap, file_data);
568 if (slot)
569 set = (cgraph_node_set) *slot;
570 else
571 {
572 set = cgraph_node_set_new ();
573 slot = pointer_map_insert (pmap, file_data);
574 *slot = set;
575 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
576 vset = varpool_node_set_new ();
577 slot = pointer_map_insert (vpmap, file_data);
578 *slot = vset;
579 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
580 }
581
582 cgraph_node_set_add (set, node);
583 }
584
585 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
586 {
587 if (vnode->alias || !vnode->needed)
588 continue;
589 slot = pointer_map_contains (vpmap, file_data);
590 if (slot)
591 vset = (varpool_node_set) *slot;
592 else
593 {
594 set = cgraph_node_set_new ();
595 slot = pointer_map_insert (pmap, file_data);
596 *slot = set;
597 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
598 vset = varpool_node_set_new ();
599 slot = pointer_map_insert (vpmap, file_data);
600 *slot = vset;
601 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
602 }
603
604 varpool_node_set_add (vset, vnode);
605 }
606
607 /* If the cgraph is empty, create one cgraph node set so that there is still
608 an output file for any variables that need to be exported in a DSO. */
609 if (!lto_cgraph_node_sets)
610 {
611 set = cgraph_node_set_new ();
612 VEC_safe_push (cgraph_node_set, gc, lto_cgraph_node_sets, set);
613 vset = varpool_node_set_new ();
614 VEC_safe_push (varpool_node_set, gc, lto_varpool_node_sets, vset);
615 }
616
617 pointer_map_destroy (pmap);
618 pointer_map_destroy (vpmap);
619
620 timevar_pop (TV_WHOPR_WPA);
621
622 lto_stats.num_cgraph_partitions += VEC_length (cgraph_node_set,
623 lto_cgraph_node_sets);
624 }
625
626
627 /* Add inlined clone NODE and its master clone to SET, if NODE itself has
628 inlined callees, recursively add the callees. */
629
630 static void
631 lto_add_inline_clones (cgraph_node_set set, struct cgraph_node *node,
632 bitmap original_decls)
633 {
634 struct cgraph_node *callee;
635 struct cgraph_edge *edge;
636
637 cgraph_node_set_add (set, node);
638
639 /* Check to see if NODE has any inlined callee. */
640 for (edge = node->callees; edge != NULL; edge = edge->next_callee)
641 {
642 callee = edge->callee;
643 if (callee->global.inlined_to != NULL)
644 lto_add_inline_clones (set, callee, original_decls);
645 }
646 }
647
648 /* Compute the transitive closure of inlining of SET based on the
649 information in the callgraph. Returns a bitmap of decls that have
650 been inlined into SET indexed by UID. */
651
652 static void
653 lto_add_all_inlinees (cgraph_node_set set)
654 {
655 cgraph_node_set_iterator csi;
656 struct cgraph_node *node;
657 bitmap original_nodes = lto_bitmap_alloc ();
658 bitmap original_decls = lto_bitmap_alloc ();
659 bool changed;
660
661 /* We are going to iterate SET while adding to it, mark all original
662 nodes so that we only add node inlined to original nodes. */
663 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
664 {
665 bitmap_set_bit (original_nodes, csi_node (csi)->uid);
666 bitmap_set_bit (original_decls, DECL_UID (csi_node (csi)->decl));
667 }
668
669 /* Some of the original nodes might not be needed anymore.
670 Remove them. */
671 do
672 {
673 changed = false;
674 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
675 {
676 struct cgraph_node *inlined_to;
677 node = csi_node (csi);
678
679 /* NODE was not inlined. We still need it. */
680 if (!node->global.inlined_to)
681 continue;
682
683 inlined_to = node->global.inlined_to;
684
685 /* NODE should have only one caller. */
686 gcc_assert (!node->callers->next_caller);
687
688 if (!bitmap_bit_p (original_nodes, inlined_to->uid))
689 {
690 bitmap_clear_bit (original_nodes, node->uid);
691 cgraph_node_set_remove (set, node);
692 changed = true;
693 }
694 }
695 }
696 while (changed);
697
698 /* Transitively add to SET all the inline clones for every node that
699 has been inlined. */
700 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
701 {
702 node = csi_node (csi);
703 if (bitmap_bit_p (original_nodes, node->uid))
704 lto_add_inline_clones (set, node, original_decls);
705 }
706
707 lto_bitmap_free (original_nodes);
708 lto_bitmap_free (original_decls);
709 }
710
711 /* Find out all static decls that need to be promoted to global because
712 of cross file sharing. This function must be run in the WPA mode after
713 all inlinees are added. */
714
715 static void
716 lto_promote_cross_file_statics (void)
717 {
718 struct varpool_node *vnode;
719 unsigned i, n_sets;
720 cgraph_node_set set;
721 cgraph_node_set_iterator csi;
722
723 gcc_assert (flag_wpa);
724
725 /* At moment we make no attempt to figure out who is refering the variables,
726 so all must become global.
727
728 Constant pool references use internal labels and thus can not be made global.
729 It is sensible to keep those ltrans local to allow better optimization. */
730 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
731 if (!vnode->externally_visible && vnode->analyzed
732 && !DECL_IN_CONSTANT_POOL (vnode->decl))
733 {
734 TREE_PUBLIC (vnode->decl) = 1;
735 DECL_VISIBILITY (vnode->decl) = VISIBILITY_HIDDEN;
736 }
737 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
738 for (i = 0; i < n_sets; i++)
739 {
740 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
741
742 /* If node has either address taken (and we have no clue from where)
743 or it is called from other partition, it needs to be globalized. */
744 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
745 {
746 struct cgraph_node *node = csi_node (csi);
747 bool globalize = node->address_taken || node->local.vtable_method;
748 struct cgraph_edge *e;
749 if (node->local.externally_visible)
750 continue;
751 for (e = node->callers; e && !globalize; e = e->next_caller)
752 {
753 struct cgraph_node *caller = e->caller;
754 if (caller->global.inlined_to)
755 caller = caller->global.inlined_to;
756 if (!cgraph_node_in_set_p (caller, set))
757 globalize = true;
758 }
759 if (globalize)
760 {
761 TREE_PUBLIC (node->decl) = 1;
762 DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
763 if (node->same_body)
764 {
765 struct cgraph_node *alias;
766 for (alias = node->same_body;
767 alias; alias = alias->next)
768 {
769 TREE_PUBLIC (alias->decl) = 1;
770 DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
771 }
772 }
773 }
774 }
775
776 }
777 }
778
779
780 /* Given a file name FNAME, return a string with FNAME prefixed with '*'. */
781
782 static char *
783 prefix_name_with_star (const char *fname)
784 {
785 char *star_fname;
786 size_t len;
787
788 len = strlen (fname) + 1 + 1;
789 star_fname = XNEWVEC (char, len);
790 snprintf (star_fname, len, "*%s", fname);
791
792 return star_fname;
793 }
794
795
796 /* Return a copy of FNAME without the .o extension. */
797
798 static char *
799 strip_extension (const char *fname)
800 {
801 char *s = XNEWVEC (char, strlen (fname) - 2 + 1);
802 gcc_assert (strstr (fname, ".o"));
803 snprintf (s, strlen (fname) - 2 + 1, "%s", fname);
804
805 return s;
806 }
807
808
809 /* Return a file name associated with cgraph node set SET. This may
810 be a new temporary file name if SET needs to be processed by
811 LTRANS, or the original file name if all the nodes in SET belong to
812 the same input file. */
813
814 static char *
815 get_filename_for_set (cgraph_node_set set)
816 {
817 char *fname = NULL;
818 static const size_t max_fname_len = 100;
819
820 /* Create a new temporary file to store SET. To facilitate
821 debugging, use file names from SET as part of the new
822 temporary file name. */
823 cgraph_node_set_iterator si;
824 struct pointer_set_t *pset = pointer_set_create ();
825 for (si = csi_start (set); !csi_end_p (si); csi_next (&si))
826 {
827 struct cgraph_node *n = csi_node (si);
828 const char *node_fname;
829 char *f;
830
831 /* Don't use the same file name more than once. */
832 if (pointer_set_insert (pset, n->local.lto_file_data))
833 continue;
834
835 /* The first file name found in SET determines the output
836 directory. For the remaining files, we use their
837 base names. */
838 node_fname = n->local.lto_file_data->file_name;
839 if (fname == NULL)
840 {
841 fname = strip_extension (node_fname);
842 continue;
843 }
844
845 f = strip_extension (lbasename (node_fname));
846
847 /* If the new name causes an excessively long file name,
848 make the last component "___" to indicate overflow. */
849 if (strlen (fname) + strlen (f) > max_fname_len - 3)
850 {
851 fname = reconcat (fname, fname, "___", NULL);
852 break;
853 }
854 else
855 {
856 fname = reconcat (fname, fname, "_", f, NULL);
857 free (f);
858 }
859 }
860
861 pointer_set_destroy (pset);
862
863 if (!fname)
864 {
865 /* Since SET does not need to be processed by LTRANS, use
866 the original file name and mark it with a '*' prefix so that
867 lto_execute_ltrans knows not to process it. */
868 cgraph_node_set_iterator si = csi_start (set);
869 struct cgraph_node *first = csi_node (si);
870 fname = prefix_name_with_star (first->local.lto_file_data->file_name);
871 }
872 else
873 {
874 /* Add the extension .wpa.o to indicate that this file has been
875 produced by WPA. */
876 fname = reconcat (fname, fname, ".wpa.o", NULL);
877 gcc_assert (fname);
878 }
879
880 return fname;
881 }
882
883 static lto_file *current_lto_file;
884
885
886 /* Write all output files in WPA mode. Returns a NULL-terminated array of
887 output file names. */
888
889 static char **
890 lto_wpa_write_files (void)
891 {
892 char **output_files;
893 unsigned i, n_sets, last_out_file_ix, num_out_files;
894 lto_file *file;
895 cgraph_node_set set;
896 varpool_node_set vset;
897
898 timevar_push (TV_WHOPR_WPA);
899
900 /* Include all inlined functions and determine what sets need to be
901 compiled by LTRANS. After this loop, only those sets that
902 contain callgraph nodes from more than one file will need to be
903 compiled by LTRANS. */
904 for (i = 0; VEC_iterate (cgraph_node_set, lto_cgraph_node_sets, i, set); i++)
905 {
906 lto_add_all_inlinees (set);
907 lto_stats.num_output_cgraph_nodes += VEC_length (cgraph_node_ptr,
908 set->nodes);
909 }
910
911 /* After adding all inlinees, find out statics that need to be promoted
912 to globals because of cross-file inlining. */
913 lto_promote_cross_file_statics ();
914
915 timevar_pop (TV_WHOPR_WPA);
916
917 timevar_push (TV_WHOPR_WPA_IO);
918
919 /* The number of output files depends on the number of input files
920 and how many callgraph node sets we create. Reserve enough space
921 for the maximum of these two. */
922 num_out_files = MAX (VEC_length (cgraph_node_set, lto_cgraph_node_sets),
923 num_in_fnames);
924 output_files = XNEWVEC (char *, num_out_files + 1);
925
926 n_sets = VEC_length (cgraph_node_set, lto_cgraph_node_sets);
927 for (i = 0; i < n_sets; i++)
928 {
929 char *temp_filename;
930
931 set = VEC_index (cgraph_node_set, lto_cgraph_node_sets, i);
932 vset = VEC_index (varpool_node_set, lto_varpool_node_sets, i);
933 temp_filename = get_filename_for_set (set);
934 output_files[i] = temp_filename;
935
936 if (cgraph_node_set_nonempty_p (set) || varpool_node_set_nonempty_p (vset))
937 {
938 /* Write all the nodes in SET to TEMP_FILENAME. */
939 file = lto_obj_file_open (temp_filename, true);
940 if (!file)
941 fatal_error ("lto_obj_file_open() failed");
942
943 if (!quiet_flag)
944 fprintf (stderr, " %s", temp_filename);
945
946 lto_set_current_out_file (file);
947
948 ipa_write_optimization_summaries (set, vset);
949
950 lto_set_current_out_file (NULL);
951 lto_obj_file_close (file);
952 }
953 }
954
955 last_out_file_ix = n_sets;
956
957 lto_stats.num_output_files += n_sets;
958
959 output_files[last_out_file_ix] = NULL;
960
961 timevar_pop (TV_WHOPR_WPA_IO);
962
963 return output_files;
964 }
965
966 /* Template of LTRANS dumpbase suffix. */
967 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
968
969 /* Perform local transformations (LTRANS) on the files in the NULL-terminated
970 FILES array. These should have been written previously by
971 lto_wpa_write_files (). Transformations are performed via executing
972 COLLECT_GCC for reach file. */
973
974 static void
975 lto_execute_ltrans (char *const *files)
976 {
977 struct pex_obj *pex;
978 const char *collect_gcc_options, *collect_gcc;
979 struct obstack env_obstack;
980 const char **argv;
981 const char **argv_ptr;
982 const char *errmsg;
983 size_t i, j;
984 int err;
985 int status;
986 FILE *ltrans_output_list_stream = NULL;
987 bool seen_dumpbase = false;
988 char *dumpbase_suffix = NULL;
989
990 timevar_push (TV_WHOPR_WPA_LTRANS_EXEC);
991
992 /* Get the driver and options. */
993 collect_gcc = getenv ("COLLECT_GCC");
994 if (!collect_gcc)
995 fatal_error ("environment variable COLLECT_GCC must be set");
996
997 /* Set the CFLAGS environment variable. */
998 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
999 if (!collect_gcc_options)
1000 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
1001
1002 /* Count arguments. */
1003 i = 0;
1004 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
1005 if (collect_gcc_options[j] == '\'')
1006 ++i;
1007
1008 if (i % 2 != 0)
1009 fatal_error ("malformed COLLECT_GCC_OPTIONS");
1010
1011 /* Initalize the arguments for the LTRANS driver. */
1012 argv = XNEWVEC (const char *, 8 + i / 2);
1013 argv_ptr = argv;
1014 *argv_ptr++ = collect_gcc;
1015 *argv_ptr++ = "-xlto";
1016 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
1017 if (collect_gcc_options[j] == '\'')
1018 {
1019 char *option;
1020
1021 ++j;
1022 i = j;
1023 while (collect_gcc_options[j] != '\'')
1024 ++j;
1025 obstack_init (&env_obstack);
1026 obstack_grow (&env_obstack, &collect_gcc_options[i], j - i);
1027 if (seen_dumpbase)
1028 obstack_grow (&env_obstack, DUMPBASE_SUFFIX,
1029 sizeof (DUMPBASE_SUFFIX));
1030 else
1031 obstack_1grow (&env_obstack, 0);
1032 option = XOBFINISH (&env_obstack, char *);
1033 if (seen_dumpbase)
1034 {
1035 dumpbase_suffix = option + 7 + j - i;
1036 seen_dumpbase = false;
1037 }
1038
1039 /* LTRANS does not need -fwpa nor -fltrans-*. */
1040 if (strncmp (option, "-fwpa", 5) != 0
1041 && strncmp (option, "-fltrans-", 9) != 0)
1042 {
1043 if (strncmp (option, "-dumpbase", 9) == 0)
1044 seen_dumpbase = true;
1045 *argv_ptr++ = option;
1046 }
1047 }
1048 *argv_ptr++ = "-fltrans";
1049
1050 /* Open the LTRANS output list. */
1051 if (ltrans_output_list)
1052 {
1053 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
1054 if (ltrans_output_list_stream == NULL)
1055 error ("opening LTRANS output list %s: %m", ltrans_output_list);
1056 }
1057
1058 for (i = 0; files[i]; ++i)
1059 {
1060 size_t len;
1061
1062 /* If the file is prefixed with a '*', it means that we do not
1063 need to re-compile it with LTRANS because it has not been
1064 modified by WPA. Skip it from the command line to
1065 lto_execute_ltrans, but add it to ltrans_output_list_stream
1066 so it is linked after we are done. */
1067 if (files[i][0] == '*')
1068 {
1069 size_t len = strlen (files[i]) - 1;
1070 if (ltrans_output_list_stream)
1071 if (fwrite (&files[i][1], 1, len, ltrans_output_list_stream) < len
1072 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1073 error ("writing to LTRANS output list %s: %m",
1074 ltrans_output_list);
1075 }
1076 else
1077 {
1078 char *output_name;
1079
1080 /* Otherwise, add FILES[I] to lto_execute_ltrans command line
1081 and add the resulting file to LTRANS output list. */
1082
1083 /* Replace the .o suffix with a .ltrans.o suffix and write
1084 the resulting name to the LTRANS output list. */
1085 obstack_init (&env_obstack);
1086 obstack_grow (&env_obstack, files[i], strlen (files[i]) - 2);
1087 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1088 output_name = XOBFINISH (&env_obstack, char *);
1089 if (ltrans_output_list_stream)
1090 {
1091 len = strlen (output_name);
1092
1093 if (fwrite (output_name, 1, len, ltrans_output_list_stream) < len
1094 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
1095 error ("writing to LTRANS output list %s: %m",
1096 ltrans_output_list);
1097 }
1098
1099 argv_ptr[0] = "-o";
1100 argv_ptr[1] = output_name;
1101 argv_ptr[2] = files[i];
1102 argv_ptr[3] = NULL;
1103
1104 /* Append a sequence number to -dumpbase for LTRANS. */
1105 if (dumpbase_suffix)
1106 snprintf (dumpbase_suffix, sizeof (DUMPBASE_SUFFIX) - 7,
1107 "%lu", (unsigned long) i);
1108
1109 /* Execute the driver. */
1110 pex = pex_init (0, "lto1", NULL);
1111 if (pex == NULL)
1112 fatal_error ("pex_init failed: %s", xstrerror (errno));
1113
1114 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0],
1115 CONST_CAST (char **, argv), NULL, NULL, &err);
1116 if (errmsg)
1117 fatal_error ("%s: %s", errmsg, xstrerror (err));
1118
1119 if (!pex_get_status (pex, 1, &status))
1120 fatal_error ("can't get program status: %s", xstrerror (errno));
1121
1122 if (status)
1123 {
1124 if (WIFSIGNALED (status))
1125 {
1126 int sig = WTERMSIG (status);
1127 fatal_error ("%s terminated with signal %d [%s]%s",
1128 argv[0], sig, strsignal (sig),
1129 WCOREDUMP (status) ? ", core dumped" : "");
1130 }
1131 else
1132 fatal_error ("%s terminated with status %d", argv[0], status);
1133 }
1134
1135 pex_free (pex);
1136 }
1137 }
1138
1139 /* Close the LTRANS output list. */
1140 if (ltrans_output_list_stream && fclose (ltrans_output_list_stream))
1141 error ("closing LTRANS output list %s: %m", ltrans_output_list);
1142
1143 obstack_free (&env_obstack, NULL);
1144 free (argv);
1145
1146 timevar_pop (TV_WHOPR_WPA_LTRANS_EXEC);
1147 }
1148
1149
1150 typedef struct {
1151 struct pointer_set_t *seen;
1152 } lto_fixup_data_t;
1153
1154 #define LTO_FIXUP_SUBTREE(t) \
1155 do \
1156 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1157 while (0)
1158
1159 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
1160 do \
1161 { \
1162 if (t) \
1163 (t) = gimple_register_type (t); \
1164 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1165 } \
1166 while (0)
1167
1168 static tree lto_fixup_tree (tree *, int *, void *);
1169
1170 /* Return true if T does not need to be fixed up recursively. */
1171
1172 static inline bool
1173 no_fixup_p (tree t)
1174 {
1175 return (t == NULL
1176 || CONSTANT_CLASS_P (t)
1177 || TREE_CODE (t) == IDENTIFIER_NODE);
1178 }
1179
1180 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
1181
1182 static void
1183 lto_fixup_common (tree t, void *data)
1184 {
1185 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
1186 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
1187 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
1188 First remove us from any pointer list we are on. */
1189 if (TREE_CODE (t) == POINTER_TYPE)
1190 {
1191 if (TYPE_POINTER_TO (TREE_TYPE (t)) == t)
1192 TYPE_POINTER_TO (TREE_TYPE (t)) = TYPE_NEXT_PTR_TO (t);
1193 else
1194 {
1195 tree tem = TYPE_POINTER_TO (TREE_TYPE (t));
1196 while (tem && TYPE_NEXT_PTR_TO (tem) != t)
1197 tem = TYPE_NEXT_PTR_TO (tem);
1198 if (tem)
1199 TYPE_NEXT_PTR_TO (tem) = TYPE_NEXT_PTR_TO (t);
1200 }
1201 TYPE_NEXT_PTR_TO (t) = NULL_TREE;
1202 }
1203 else if (TREE_CODE (t) == REFERENCE_TYPE)
1204 {
1205 if (TYPE_REFERENCE_TO (TREE_TYPE (t)) == t)
1206 TYPE_REFERENCE_TO (TREE_TYPE (t)) = TYPE_NEXT_REF_TO (t);
1207 else
1208 {
1209 tree tem = TYPE_REFERENCE_TO (TREE_TYPE (t));
1210 while (tem && TYPE_NEXT_REF_TO (tem) != t)
1211 tem = TYPE_NEXT_REF_TO (tem);
1212 if (tem)
1213 TYPE_NEXT_REF_TO (tem) = TYPE_NEXT_REF_TO (t);
1214 }
1215 TYPE_NEXT_REF_TO (t) = NULL_TREE;
1216 }
1217
1218 /* Fixup our type. */
1219 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1220
1221 /* Second put us on the list of pointers of the new pointed-to type
1222 if we are a main variant. This is done in lto_fixup_type after
1223 fixing up our main variant. */
1224
1225 /* This is not very efficient because we cannot do tail-recursion with
1226 a long chain of trees. */
1227 LTO_FIXUP_SUBTREE (TREE_CHAIN (t));
1228 }
1229
1230 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
1231
1232 static void
1233 lto_fixup_decl_minimal (tree t, void *data)
1234 {
1235 lto_fixup_common (t, data);
1236 LTO_FIXUP_SUBTREE (DECL_NAME (t));
1237 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t));
1238 }
1239
1240 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1241
1242 static void
1243 lto_fixup_decl_common (tree t, void *data)
1244 {
1245 lto_fixup_decl_minimal (t, data);
1246 LTO_FIXUP_SUBTREE (DECL_SIZE (t));
1247 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t));
1248 LTO_FIXUP_SUBTREE (DECL_INITIAL (t));
1249 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t));
1250 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t));
1251 }
1252
1253 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1254
1255 static void
1256 lto_fixup_decl_with_vis (tree t, void *data)
1257 {
1258 lto_fixup_decl_common (t, data);
1259
1260 /* Accessor macro has side-effects, use field-name here. */
1261 LTO_FIXUP_SUBTREE (t->decl_with_vis.assembler_name);
1262
1263 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t)));
1264 }
1265
1266 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1267
1268 static void
1269 lto_fixup_decl_non_common (tree t, void *data)
1270 {
1271 lto_fixup_decl_with_vis (t, data);
1272 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t));
1273 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t));
1274 LTO_FIXUP_SUBTREE (DECL_VINDEX (t));
1275
1276 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1277 gcc_assert (no_fixup_p (t->decl_non_common.saved_tree));
1278 }
1279
1280 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1281
1282 static void
1283 lto_fixup_function (tree t, void *data)
1284 {
1285 lto_fixup_decl_non_common (t, data);
1286 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t));
1287 }
1288
1289 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1290
1291 static void
1292 lto_fixup_field_decl (tree t, void *data)
1293 {
1294 lto_fixup_decl_common (t, data);
1295 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t));
1296 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t));
1297 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t));
1298 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t)));
1299 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t));
1300 }
1301
1302 /* Fix up fields of a type T. DATA points to fix-up states. */
1303
1304 static void
1305 lto_fixup_type (tree t, void *data)
1306 {
1307 tree tem, mv;
1308
1309 lto_fixup_common (t, data);
1310 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t));
1311 LTO_FIXUP_SUBTREE (TYPE_SIZE (t));
1312 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t));
1313 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t));
1314 LTO_FIXUP_SUBTREE (TYPE_NAME (t));
1315
1316 /* Accessors are for derived node types only. */
1317 if (!POINTER_TYPE_P (t))
1318 LTO_FIXUP_SUBTREE (t->type.minval);
1319 LTO_FIXUP_SUBTREE (t->type.maxval);
1320
1321 /* Accessor is for derived node types only. */
1322 LTO_FIXUP_SUBTREE (t->type.binfo);
1323
1324 if (TYPE_CONTEXT (t))
1325 {
1326 if (TYPE_P (TYPE_CONTEXT (t)))
1327 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1328 else
1329 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t));
1330 }
1331 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CANONICAL (t));
1332
1333 /* The following re-creates proper variant lists while fixing up
1334 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1335 variant list state before fixup is broken. */
1336
1337 /* Remove us from our main variant list if we are not the variant leader. */
1338 if (TYPE_MAIN_VARIANT (t) != t)
1339 {
1340 tem = TYPE_MAIN_VARIANT (t);
1341 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1342 tem = TYPE_NEXT_VARIANT (tem);
1343 if (tem)
1344 TYPE_NEXT_VARIANT (tem) = TYPE_NEXT_VARIANT (t);
1345 TYPE_NEXT_VARIANT (t) = NULL_TREE;
1346 }
1347
1348 /* Query our new main variant. */
1349 mv = gimple_register_type (TYPE_MAIN_VARIANT (t));
1350
1351 /* If we were the variant leader and we get replaced ourselves drop
1352 all variants from our list. */
1353 if (TYPE_MAIN_VARIANT (t) == t
1354 && mv != t)
1355 {
1356 tem = t;
1357 while (tem)
1358 {
1359 tree tem2 = TYPE_NEXT_VARIANT (tem);
1360 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1361 tem = tem2;
1362 }
1363 }
1364
1365 /* If we are not our own variant leader link us into our new leaders
1366 variant list. */
1367 if (mv != t)
1368 {
1369 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1370 TYPE_NEXT_VARIANT (mv) = t;
1371 }
1372
1373 /* Finally adjust our main variant and fix it up. */
1374 TYPE_MAIN_VARIANT (t) = mv;
1375 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t));
1376
1377 /* As the second step of reconstructing the pointer chains put us
1378 on the list of pointers of the new pointed-to type
1379 if we are a main variant. See lto_fixup_common for the first step. */
1380 if (TREE_CODE (t) == POINTER_TYPE
1381 && TYPE_MAIN_VARIANT (t) == t)
1382 {
1383 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1384 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1385 }
1386 else if (TREE_CODE (t) == REFERENCE_TYPE
1387 && TYPE_MAIN_VARIANT (t) == t)
1388 {
1389 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1390 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1391 }
1392 }
1393
1394 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1395
1396 static void
1397 lto_fixup_binfo (tree t, void *data)
1398 {
1399 unsigned HOST_WIDE_INT i, n;
1400 tree base, saved_base;
1401
1402 lto_fixup_common (t, data);
1403 gcc_assert (no_fixup_p (BINFO_OFFSET (t)));
1404 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t));
1405 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t));
1406 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t));
1407 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1408 for (i = 0; i < n; i++)
1409 {
1410 saved_base = base = BINFO_BASE_ACCESS (t, i);
1411 LTO_FIXUP_SUBTREE (base);
1412 if (base != saved_base)
1413 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1414 }
1415 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t));
1416 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t));
1417 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t));
1418 n = BINFO_N_BASE_BINFOS (t);
1419 for (i = 0; i < n; i++)
1420 {
1421 saved_base = base = BINFO_BASE_BINFO (t, i);
1422 LTO_FIXUP_SUBTREE (base);
1423 if (base != saved_base)
1424 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1425 }
1426 }
1427
1428 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1429
1430 static void
1431 lto_fixup_constructor (tree t, void *data)
1432 {
1433 unsigned HOST_WIDE_INT idx;
1434 constructor_elt *ce;
1435
1436 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1437
1438 for (idx = 0;
1439 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1440 idx++)
1441 {
1442 LTO_FIXUP_SUBTREE (ce->index);
1443 LTO_FIXUP_SUBTREE (ce->value);
1444 }
1445 }
1446
1447 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1448 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1449 DATA is a pointer set to record visited nodes. */
1450
1451 static tree
1452 lto_fixup_tree (tree *tp, int *walk_subtrees, void *data)
1453 {
1454 tree t;
1455 lto_fixup_data_t *fixup_data = (lto_fixup_data_t *) data;
1456 tree prevailing;
1457
1458 t = *tp;
1459 *walk_subtrees = 0;
1460 if (pointer_set_contains (fixup_data->seen, t))
1461 return NULL;
1462
1463 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
1464 {
1465 prevailing = lto_symtab_prevailing_decl (t);
1466
1467 if (t != prevailing)
1468 {
1469 /* Also replace t with prevailing defintion. We don't want to
1470 insert the other defintion in the seen set as we want to
1471 replace all instances of it. */
1472 *tp = prevailing;
1473 t = prevailing;
1474 }
1475 }
1476 else if (TYPE_P (t))
1477 {
1478 /* Replace t with the prevailing type. We don't want to insert the
1479 other type in the seen set as we want to replace all instances of it. */
1480 t = gimple_register_type (t);
1481 *tp = t;
1482 }
1483
1484 if (pointer_set_insert (fixup_data->seen, t))
1485 return NULL;
1486
1487 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1488 Hence we do special processing here for those kind of nodes. */
1489 switch (TREE_CODE (t))
1490 {
1491 case FIELD_DECL:
1492 lto_fixup_field_decl (t, data);
1493 break;
1494
1495 case LABEL_DECL:
1496 case CONST_DECL:
1497 case PARM_DECL:
1498 case RESULT_DECL:
1499 case IMPORTED_DECL:
1500 lto_fixup_decl_common (t, data);
1501 break;
1502
1503 case VAR_DECL:
1504 lto_fixup_decl_with_vis (t, data);
1505 break;
1506
1507 case TYPE_DECL:
1508 lto_fixup_decl_non_common (t, data);
1509 break;
1510
1511 case FUNCTION_DECL:
1512 lto_fixup_function (t, data);
1513 break;
1514
1515 case TREE_BINFO:
1516 lto_fixup_binfo (t, data);
1517 break;
1518
1519 default:
1520 if (TYPE_P (t))
1521 lto_fixup_type (t, data);
1522 else if (TREE_CODE (t) == CONSTRUCTOR)
1523 lto_fixup_constructor (t, data);
1524 else if (CONSTANT_CLASS_P (t))
1525 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t));
1526 else if (EXPR_P (t))
1527 {
1528 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1529 lto_fixup_common (t, data);
1530 LTO_FIXUP_SUBTREE (t->exp.block);
1531 *walk_subtrees = 1;
1532 }
1533 else
1534 {
1535 /* Let walk_tree handle sub-trees. */
1536 *walk_subtrees = 1;
1537 }
1538 }
1539
1540 return NULL;
1541 }
1542
1543 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1544 replaces var and function decls with the corresponding prevailing def and
1545 records the old decl in the free-list in DATA. We also record visted nodes
1546 in the seen-set in DATA to avoid multiple visit for nodes that need not
1547 to be replaced. */
1548
1549 static void
1550 lto_fixup_state (struct lto_in_decl_state *state, lto_fixup_data_t *data)
1551 {
1552 unsigned i, si;
1553 struct lto_tree_ref_table *table;
1554
1555 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1556 we still need to walk from all DECLs to find the reachable
1557 FUNCTION_DECLs and VAR_DECLs. */
1558 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
1559 {
1560 table = &state->streams[si];
1561 for (i = 0; i < table->size; i++)
1562 walk_tree (table->trees + i, lto_fixup_tree, data, NULL);
1563 }
1564 }
1565
1566 /* A callback of htab_traverse. Just extract a state from SLOT and the
1567 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1568
1569 static int
1570 lto_fixup_state_aux (void **slot, void *aux)
1571 {
1572 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
1573 lto_fixup_state (state, (lto_fixup_data_t *) aux);
1574 return 1;
1575 }
1576
1577 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1578 prevailing one. */
1579
1580 static void
1581 lto_fixup_decls (struct lto_file_decl_data **files)
1582 {
1583 unsigned int i;
1584 tree decl;
1585 struct pointer_set_t *seen = pointer_set_create ();
1586 lto_fixup_data_t data;
1587
1588 data.seen = seen;
1589 for (i = 0; files[i]; i++)
1590 {
1591 struct lto_file_decl_data *file = files[i];
1592 struct lto_in_decl_state *state = file->global_decl_state;
1593 lto_fixup_state (state, &data);
1594
1595 htab_traverse (file->function_decl_states, lto_fixup_state_aux, &data);
1596 }
1597
1598 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1599 {
1600 tree saved_decl = decl;
1601 walk_tree (&decl, lto_fixup_tree, &data, NULL);
1602 if (decl != saved_decl)
1603 VEC_replace (tree, lto_global_var_decls, i, decl);
1604 }
1605
1606 pointer_set_destroy (seen);
1607 }
1608
1609 /* Unlink a temporary LTRANS file unless requested otherwise. */
1610
1611 static void
1612 lto_maybe_unlink (const char *file)
1613 {
1614 if (!getenv ("WPA_SAVE_LTRANS"))
1615 {
1616 if (unlink_if_ordinary (file))
1617 error ("deleting LTRANS input file %s: %m", file);
1618 }
1619 else
1620 fprintf (stderr, "[Leaving LTRANS input file %s]\n", file);
1621 }
1622
1623 /* Read the options saved from each file in the command line. Called
1624 from lang_hooks.post_options which is called by process_options
1625 right before all the options are used to initialize the compiler.
1626 This assumes that decode_options has already run, so the
1627 num_in_fnames and in_fnames are properly set.
1628
1629 Note that this assumes that all the files had been compiled with
1630 the same options, which is not a good assumption. In general,
1631 options ought to be read from all the files in the set and merged.
1632 However, it is still unclear what the merge rules should be. */
1633
1634 void
1635 lto_read_all_file_options (void)
1636 {
1637 size_t i;
1638
1639 /* Clear any file options currently saved. */
1640 lto_clear_file_options ();
1641
1642 /* Set the hooks to read ELF sections. */
1643 lto_set_in_hooks (NULL, get_section_data, free_section_data);
1644
1645 for (i = 0; i < num_in_fnames; i++)
1646 {
1647 struct lto_file_decl_data *file_data;
1648 lto_file *file = lto_obj_file_open (in_fnames[i], false);
1649 if (!file)
1650 break;
1651
1652 file_data = XCNEW (struct lto_file_decl_data);
1653 file_data->file_name = file->filename;
1654 file_data->section_hash_table = lto_obj_build_section_table (file);
1655
1656 lto_read_file_options (file_data);
1657
1658 lto_obj_file_close (file);
1659 htab_delete (file_data->section_hash_table);
1660 free (file_data);
1661 }
1662
1663 /* Apply globally the options read from all the files. */
1664 lto_reissue_options ();
1665 }
1666
1667 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
1668
1669 /* Read all the symbols from the input files FNAMES. NFILES is the
1670 number of files requested in the command line. Instantiate a
1671 global call graph by aggregating all the sub-graphs found in each
1672 file. */
1673
1674 static void
1675 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
1676 {
1677 unsigned int i, last_file_ix;
1678 FILE *resolution;
1679 struct cgraph_node *node;
1680
1681 lto_stats.num_input_files = nfiles;
1682
1683 timevar_push (TV_IPA_LTO_DECL_IO);
1684
1685 /* Set the hooks so that all of the ipa passes can read in their data. */
1686 all_file_decl_data = GGC_CNEWVEC (struct lto_file_decl_data *, nfiles + 1);
1687 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1688
1689 /* Read the resolution file. */
1690 resolution = NULL;
1691 if (resolution_file_name)
1692 {
1693 int t;
1694 unsigned num_objects;
1695
1696 resolution = fopen (resolution_file_name, "r");
1697 if (resolution == NULL)
1698 fatal_error ("could not open symbol resolution file: %s",
1699 xstrerror (errno));
1700
1701 t = fscanf (resolution, "%u", &num_objects);
1702 gcc_assert (t == 1);
1703
1704 /* True, since the plugin splits the archives. */
1705 gcc_assert (num_objects == nfiles);
1706 }
1707
1708 if (!quiet_flag)
1709 fprintf (stderr, "Reading object files:");
1710
1711 /* Read all of the object files specified on the command line. */
1712 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
1713 {
1714 struct lto_file_decl_data *file_data = NULL;
1715 if (!quiet_flag)
1716 {
1717 fprintf (stderr, " %s", fnames[i]);
1718 fflush (stderr);
1719 }
1720
1721 current_lto_file = lto_obj_file_open (fnames[i], false);
1722 if (!current_lto_file)
1723 break;
1724
1725 file_data = lto_file_read (current_lto_file, resolution);
1726 if (!file_data)
1727 break;
1728
1729 all_file_decl_data[last_file_ix++] = file_data;
1730
1731 lto_obj_file_close (current_lto_file);
1732 current_lto_file = NULL;
1733 ggc_collect ();
1734 }
1735
1736 if (resolution_file_name)
1737 fclose (resolution);
1738
1739 all_file_decl_data[last_file_ix] = NULL;
1740
1741 /* Set the hooks so that all of the ipa passes can read in their data. */
1742 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
1743
1744 timevar_pop (TV_IPA_LTO_DECL_IO);
1745
1746 if (!quiet_flag)
1747 fprintf (stderr, "\nReading the callgraph\n");
1748
1749 timevar_push (TV_IPA_LTO_CGRAPH_IO);
1750 /* Read the callgraph. */
1751 input_cgraph ();
1752 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
1753
1754 if (!quiet_flag)
1755 fprintf (stderr, "Merging declarations\n");
1756
1757 timevar_push (TV_IPA_LTO_DECL_MERGE);
1758 /* Merge global decls. */
1759 lto_symtab_merge_decls ();
1760
1761 /* Fixup all decls and types and free the type hash tables. */
1762 lto_fixup_decls (all_file_decl_data);
1763 free_gimple_type_tables ();
1764 ggc_collect ();
1765
1766 timevar_pop (TV_IPA_LTO_DECL_MERGE);
1767 /* Each pass will set the appropriate timer. */
1768
1769 if (!quiet_flag)
1770 fprintf (stderr, "Reading summaries\n");
1771
1772 /* Read the IPA summary data. */
1773 if (flag_ltrans)
1774 ipa_read_optimization_summaries ();
1775 else
1776 ipa_read_summaries ();
1777
1778 /* Finally merge the cgraph according to the decl merging decisions. */
1779 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
1780 lto_symtab_merge_cgraph_nodes ();
1781 ggc_collect ();
1782
1783 if (flag_ltrans)
1784 for (node = cgraph_nodes; node; node = node->next)
1785 {
1786 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1787 summaries computed and needs to apply changes. At the moment WHOPR only
1788 supports inlining, so we can push it here by hand. In future we need to stream
1789 this field into ltrans compilation. */
1790 if (node->analyzed)
1791 VEC_safe_push (ipa_opt_pass, heap,
1792 node->ipa_transforms_to_apply,
1793 (ipa_opt_pass)&pass_ipa_inline);
1794 }
1795 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
1796
1797 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
1798
1799 /* FIXME lto. This loop needs to be changed to use the pass manager to
1800 call the ipa passes directly. */
1801 if (!errorcount)
1802 for (i = 0; i < last_file_ix; i++)
1803 {
1804 struct lto_file_decl_data *file_data = all_file_decl_data [i];
1805 lto_materialize_constructors_and_inits (file_data);
1806 }
1807
1808 /* Indicate that the cgraph is built and ready. */
1809 cgraph_function_flags_ready = true;
1810
1811 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
1812 ggc_free (all_file_decl_data);
1813 all_file_decl_data = NULL;
1814 }
1815
1816
1817 /* Materialize all the bodies for all the nodes in the callgraph. */
1818
1819 static void
1820 materialize_cgraph (void)
1821 {
1822 tree decl;
1823 struct cgraph_node *node;
1824 unsigned i;
1825 timevar_id_t lto_timer;
1826
1827 if (!quiet_flag)
1828 fprintf (stderr,
1829 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
1830
1831
1832 /* Now that we have input the cgraph, we need to clear all of the aux
1833 nodes and read the functions if we are not running in WPA mode. */
1834 timevar_push (TV_IPA_LTO_GIMPLE_IO);
1835
1836 for (node = cgraph_nodes; node; node = node->next)
1837 {
1838 /* Some cgraph nodes get created on the fly, and they don't need
1839 to be materialized. For instance, nodes for nested functions
1840 where the parent function was not streamed out or builtin
1841 functions. Additionally, builtin functions should not be
1842 materialized and may, in fact, cause confusion because there
1843 may be a regular function in the file whose assembler name
1844 matches that of the function.
1845 See gcc.c-torture/execute/20030125-1.c and
1846 gcc.c-torture/execute/921215-1.c. */
1847 if (node->local.lto_file_data
1848 && !DECL_IS_BUILTIN (node->decl))
1849 {
1850 announce_function (node->decl);
1851 lto_materialize_function (node);
1852 lto_stats.num_input_cgraph_nodes++;
1853 }
1854 }
1855
1856 timevar_pop (TV_IPA_LTO_GIMPLE_IO);
1857
1858 /* Start the appropriate timer depending on the mode that we are
1859 operating in. */
1860 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
1861 : (flag_ltrans) ? TV_WHOPR_LTRANS
1862 : TV_LTO;
1863 timevar_push (lto_timer);
1864
1865 current_function_decl = NULL;
1866 set_cfun (NULL);
1867
1868 /* Inform the middle end about the global variables we have seen. */
1869 for (i = 0; VEC_iterate (tree, lto_global_var_decls, i, decl); i++)
1870 rest_of_decl_compilation (decl, 1, 0);
1871
1872 if (!quiet_flag)
1873 fprintf (stderr, "\n");
1874
1875 timevar_pop (lto_timer);
1876 }
1877
1878
1879 /* Perform whole program analysis (WPA) on the callgraph and write out the
1880 optimization plan. */
1881
1882 static void
1883 do_whole_program_analysis (void)
1884 {
1885 char **output_files;
1886 size_t i;
1887
1888 /* Note that since we are in WPA mode, materialize_cgraph will not
1889 actually read in all the function bodies. It only materializes
1890 the decls and cgraph nodes so that analysis can be performed. */
1891 materialize_cgraph ();
1892
1893 /* Reading in the cgraph uses different timers, start timing WPA now. */
1894 timevar_push (TV_WHOPR_WPA);
1895
1896 if (pre_ipa_mem_report)
1897 {
1898 fprintf (stderr, "Memory consumption before IPA\n");
1899 dump_memory_report (false);
1900 }
1901
1902 cgraph_function_flags_ready = true;
1903 bitmap_obstack_initialize (NULL);
1904 ipa_register_cgraph_hooks ();
1905 cgraph_state = CGRAPH_STATE_IPA_SSA;
1906
1907 execute_ipa_pass_list (all_regular_ipa_passes);
1908
1909 verify_cgraph ();
1910 bitmap_obstack_release (NULL);
1911
1912 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1913 timevar_pop (TV_WHOPR_WPA);
1914
1915 lto_1_to_1_map ();
1916
1917 if (!quiet_flag)
1918 {
1919 fprintf (stderr, "\nStreaming out");
1920 fflush (stderr);
1921 }
1922 output_files = lto_wpa_write_files ();
1923 ggc_collect ();
1924 if (!quiet_flag)
1925 fprintf (stderr, "\n");
1926
1927 if (post_ipa_mem_report)
1928 {
1929 fprintf (stderr, "Memory consumption after IPA\n");
1930 dump_memory_report (false);
1931 }
1932
1933 /* Show the LTO report before launching LTRANS. */
1934 if (flag_lto_report)
1935 print_lto_report ();
1936
1937 lto_execute_ltrans (output_files);
1938
1939 for (i = 0; output_files[i]; ++i)
1940 {
1941 if (output_files[i][0] != '*')
1942 lto_maybe_unlink (output_files[i]);
1943
1944 free (output_files[i]);
1945 }
1946
1947 XDELETEVEC (output_files);
1948 }
1949
1950
1951 /* Main entry point for the GIMPLE front end. This front end has
1952 three main personalities:
1953
1954 - LTO (-flto). All the object files on the command line are
1955 loaded in memory and processed as a single translation unit.
1956 This is the traditional link-time optimization behavior.
1957
1958 - WPA (-fwpa). Only the callgraph and summary information for
1959 files in the command file are loaded. A single callgraph
1960 (without function bodies) is instantiated for the whole set of
1961 files. IPA passes are only allowed to analyze the call graph
1962 and make transformation decisions. The callgraph is
1963 partitioned, each partition is written to a new object file
1964 together with the transformation decisions.
1965
1966 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
1967 summary files from running again. Since WPA computed summary
1968 information and decided what transformations to apply, LTRANS
1969 simply applies them. */
1970
1971 void
1972 lto_main (int debug_p ATTRIBUTE_UNUSED)
1973 {
1974 lto_init_reader ();
1975
1976 /* Read all the symbols and call graph from all the files in the
1977 command line. */
1978 read_cgraph_and_symbols (num_in_fnames, in_fnames);
1979
1980 if (!errorcount)
1981 {
1982 /* If WPA is enabled analyze the whole call graph and create an
1983 optimization plan. Otherwise, read in all the function
1984 bodies and continue with optimization. */
1985 if (flag_wpa)
1986 do_whole_program_analysis ();
1987 else
1988 {
1989 materialize_cgraph ();
1990
1991 /* Let the middle end know that we have read and merged all of
1992 the input files. */
1993 cgraph_optimize ();
1994
1995 /* FIXME lto, if the processes spawned by WPA fail, we miss
1996 the chance to print WPA's report, so WPA will call
1997 print_lto_report before launching LTRANS. If LTRANS was
1998 launched directly by the driver we would not need to do
1999 this. */
2000 if (flag_lto_report)
2001 print_lto_report ();
2002 }
2003 }
2004 }
2005
2006 #include "gt-lto-lto.h"
This page took 0.126186 seconds and 5 git commands to generate.