1 /* Top-level LTO routines.
2 Copyright 2009, 2010 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "diagnostic.h"
29 #include "libiberty.h"
32 #include "tree-ssa-operands.h"
33 #include "tree-pass.h"
34 #include "langhooks.h"
37 #include "pointer-set.h"
44 #include "lto-streamer.h"
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. */
53 /* Handle opening elf files on hosts, such as Windows, that may use
54 text file handling that will break binary access. */
62 DEF_VEC_ALLOC_P(bitmap
,heap
);
64 /* Read the constructors and inits. */
67 lto_materialize_constructors_and_inits (struct lto_file_decl_data
* file_data
)
70 const char *data
= lto_get_section_data (file_data
,
71 LTO_section_static_initializer
,
73 lto_input_constructors_and_inits (file_data
, data
);
74 lto_free_section_data (file_data
, LTO_section_static_initializer
, NULL
,
78 /* Read the function body for the function associated with NODE if possible. */
81 lto_materialize_function (struct cgraph_node
*node
)
84 struct lto_file_decl_data
*file_data
;
85 const char *data
, *name
;
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
96 file_data
= node
->local
.lto_file_data
;
97 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
99 /* We may have renamed the declaration, e.g., a static function. */
100 name
= lto_get_decl_name_mapping (file_data
, name
);
102 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
108 gcc_assert (!DECL_IS_BUILTIN (decl
));
110 /* This function has a definition. */
111 TREE_STATIC (decl
) = 1;
113 gcc_assert (DECL_STRUCT_FUNCTION (decl
) == NULL
);
114 allocate_struct_function (decl
, false);
116 /* Load the function body only if not operating in WPA mode. In
117 WPA mode, the body of the function is not needed. */
120 lto_input_function_body (file_data
, decl
, data
);
121 lto_stats
.num_function_bodies
++;
124 fn
= DECL_STRUCT_FUNCTION (decl
);
125 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
128 /* Look for initializers of constant variables and private
130 for (step
= fn
->local_decls
; step
; step
= TREE_CHAIN (step
))
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
);
140 DECL_EXTERNAL (decl
) = 1;
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
));
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. */
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
)
162 decl
= lto_streamer_cache_get (data_in
->reader_cache
, (int) ix
);
163 if (TREE_CODE (decl
) != FUNCTION_DECL
)
165 gcc_assert (decl
== void_type_node
);
168 state
->fn_decl
= decl
;
170 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
172 uint32_t size
= *data
++;
173 tree
*decls
= (tree
*) xcalloc (size
, sizeof (tree
));
175 for (j
= 0; j
< size
; j
++)
177 decls
[j
] = lto_streamer_cache_get (data_in
->reader_cache
, data
[j
]);
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
]);
185 state
->streams
[i
].size
= size
;
186 state
->streams
[i
].trees
= decls
;
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). */
199 lto_read_decls (struct lto_file_decl_data
*decl_data
, const void *data
,
200 VEC(ld_plugin_symbol_resolution_t
,heap
) *resolutions
)
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
;
209 const uint32_t *data_ptr
, *data_end
;
210 uint32_t num_decl_states
;
212 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
215 data_in
= lto_data_in_create (decl_data
, (const char *) data
+ string_offset
,
216 header
->string_size
, resolutions
);
218 /* Read the global declarations and types. */
219 while (ib_main
.p
< ib_main
.len
)
221 tree t
= lto_input_tree (&ib_main
, data_in
);
222 gcc_assert (t
&& ib_main
.p
<= ib_main
.len
);
225 /* Read in lto_in_decl_state objects. */
226 data_ptr
= (const uint32_t *) ((const char*) data
+ decl_offset
);
228 (const uint32_t *) ((const char*) data_ptr
+ header
->decl_state_size
);
229 num_decl_states
= *data_ptr
++;
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
);
236 /* Read in per-function decl states and enter them in hash table. */
237 decl_data
->function_decl_states
=
238 htab_create (37, lto_hash_in_decl_state
, lto_eq_in_decl_state
, free
);
240 for (i
= 1; i
< num_decl_states
; i
++)
242 struct lto_in_decl_state
*state
= lto_new_in_decl_state ();
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
);
251 if (data_ptr
!= data_end
)
252 internal_error ("bytecode stream: garbage at the end of symbols section");
254 /* Set the current decl state to be the global state. */
255 decl_data
->current_decl_state
= decl_data
->global_decl_state
;
257 lto_data_in_delete (data_in
);
260 /* strtoll is not portable. */
262 lto_parse_hex (const char *p
) {
264 for (; *p
!= '\0'; ++p
)
269 if (c
>= '0' && c
<= '9')
271 else if (c
>= 'a' && c
<= 'f')
273 else if (c
>= 'A' && c
<= 'F')
276 internal_error ("could not parse hex number");
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. */
286 static VEC(ld_plugin_symbol_resolution_t
,heap
) *
287 lto_resolution_read (FILE *resolution
, lto_file
*file
)
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
;
293 unsigned int num_symbols
;
295 VEC(ld_plugin_symbol_resolution_t
,heap
) *ret
= NULL
;
296 unsigned max_index
= 0;
301 name_len
= strlen (file
->filename
);
302 obj_name
= XNEWVEC (char, name_len
+ 1);
303 fscanf (resolution
, " "); /* Read white space. */
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)
315 t
= fscanf (resolution
, "@0x%16s", offset_p
);
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");
325 fscanf (resolution
, "%u", &num_symbols
);
327 for (i
= 0; i
< num_symbols
; i
++)
332 enum ld_plugin_symbol_resolution r
;
334 unsigned int lto_resolution_str_len
=
335 sizeof (lto_resolution_str
) / sizeof (char *);
337 t
= fscanf (resolution
, "%u %26s %*[^\n]\n", &index
, r_str
);
339 internal_error ("Invalid line in the resolution file.");
340 if (index
> max_index
)
343 for (j
= 0; j
< lto_resolution_str_len
; j
++)
345 if (strcmp (lto_resolution_str
[j
], r_str
) == 0)
347 r
= (enum ld_plugin_symbol_resolution
) j
;
351 if (j
== lto_resolution_str_len
)
352 internal_error ("Invalid resolution in the resolution file.");
354 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t
, heap
, ret
,
356 VEC_replace (ld_plugin_symbol_resolution_t
, ret
, index
, r
);
362 /* Generate a TREE representation for all types and external decls
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. */
369 static struct lto_file_decl_data
*
370 lto_file_read (lto_file
*file
, FILE *resolution_file
)
372 struct lto_file_decl_data
*file_data
;
375 VEC(ld_plugin_symbol_resolution_t
,heap
) *resolutions
;
377 resolutions
= lto_resolution_read (resolution_file
, file
);
379 file_data
= XCNEW (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 ();
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
);
391 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
392 #define LTO_MMAP_IO 1
396 /* Page size of machine is used for mmap and munmap calls. */
397 static size_t page_mask
;
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. */
405 lto_read_section_data (struct lto_file_decl_data
*file_data
,
406 intptr_t offset
, size_t len
)
410 static char *fd_name
;
412 intptr_t computed_len
;
413 intptr_t computed_offset
;
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. */
423 && strcmp (fd_name
, file_data
->file_name
) != 0)
431 fd_name
= xstrdup (file_data
->file_name
);
432 fd
= open (file_data
->file_name
, O_RDONLY
|O_BINARY
);
440 size_t page_size
= sysconf (_SC_PAGE_SIZE
);
441 page_mask
= ~(page_size
- 1);
444 computed_offset
= offset
& page_mask
;
445 diff
= offset
- computed_offset
;
446 computed_len
= len
+ diff
;
448 result
= (char *) mmap (NULL
, computed_len
, PROT_READ
, MAP_PRIVATE
,
449 fd
, computed_offset
);
450 if (result
== MAP_FAILED
)
453 return result
+ diff
;
455 result
= (char *) xmalloc (len
);
456 if (lseek (fd
, offset
, SEEK_SET
) != offset
457 || read (fd
, result
, len
) != (ssize_t
) len
)
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
473 get_section_data (struct lto_file_decl_data
*file_data
,
474 enum lto_section_type section_type
,
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
);
485 s_slot
.name
= section_name
;
486 f_slot
= (struct lto_section_slot
*) htab_find (section_hash_table
, &s_slot
);
489 data
= lto_read_section_data (file_data
, f_slot
->start
, f_slot
->len
);
493 free (CONST_CAST (char *, section_name
));
498 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
499 starts at OFFSET and has LEN bytes. */
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
)
508 intptr_t computed_len
;
509 intptr_t computed_offset
;
514 computed_offset
= ((intptr_t) offset
) & page_mask
;
515 diff
= (intptr_t) offset
- computed_offset
;
516 computed_len
= len
+ diff
;
518 munmap ((caddr_t
) computed_offset
, computed_len
);
520 free (CONST_CAST(char *, offset
));
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
;
529 /* Group cgrah nodes by input files. This is used mainly for testing
533 lto_1_to_1_map (void)
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
;
541 varpool_node_set vset
;
544 timevar_push (TV_WHOPR_WPA
);
546 lto_cgraph_node_sets
= VEC_alloc (cgraph_node_set
, gc
, 1);
547 lto_varpool_node_sets
= VEC_alloc (varpool_node_set
, gc
, 1);
549 pmap
= pointer_map_create ();
550 vpmap
= pointer_map_create ();
552 for (node
= cgraph_nodes
; node
; node
= node
->next
)
554 /* We will get proper partition based on function they are inlined to or
556 if (node
->global
.inlined_to
|| node
->clone_of
)
558 /* Nodes without a body do not need partitioning. */
559 if (!node
->analyzed
|| node
->same_body_alias
)
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
)
567 slot
= pointer_map_contains (pmap
, file_data
);
569 set
= (cgraph_node_set
) *slot
;
572 set
= cgraph_node_set_new ();
573 slot
= pointer_map_insert (pmap
, file_data
);
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
);
579 VEC_safe_push (varpool_node_set
, gc
, lto_varpool_node_sets
, vset
);
582 cgraph_node_set_add (set
, node
);
585 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
589 slot
= pointer_map_contains (vpmap
, file_data
);
591 vset
= (varpool_node_set
) *slot
;
594 set
= cgraph_node_set_new ();
595 slot
= pointer_map_insert (pmap
, file_data
);
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
);
601 VEC_safe_push (varpool_node_set
, gc
, lto_varpool_node_sets
, vset
);
604 varpool_node_set_add (vset
, vnode
);
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
)
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
);
617 pointer_map_destroy (pmap
);
618 pointer_map_destroy (vpmap
);
620 timevar_pop (TV_WHOPR_WPA
);
622 lto_stats
.num_cgraph_partitions
+= VEC_length (cgraph_node_set
,
623 lto_cgraph_node_sets
);
627 /* Add inlined clone NODE and its master clone to SET, if NODE itself has
628 inlined callees, recursively add the callees. */
631 lto_add_inline_clones (cgraph_node_set set
, struct cgraph_node
*node
,
632 bitmap original_decls
)
634 struct cgraph_node
*callee
;
635 struct cgraph_edge
*edge
;
637 cgraph_node_set_add (set
, node
);
639 /* Check to see if NODE has any inlined callee. */
640 for (edge
= node
->callees
; edge
!= NULL
; edge
= edge
->next_callee
)
642 callee
= edge
->callee
;
643 if (callee
->global
.inlined_to
!= NULL
)
644 lto_add_inline_clones (set
, callee
, original_decls
);
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. */
653 lto_add_all_inlinees (cgraph_node_set set
)
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 ();
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
))
665 bitmap_set_bit (original_nodes
, csi_node (csi
)->uid
);
666 bitmap_set_bit (original_decls
, DECL_UID (csi_node (csi
)->decl
));
669 /* Some of the original nodes might not be needed anymore.
674 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
676 struct cgraph_node
*inlined_to
;
677 node
= csi_node (csi
);
679 /* NODE was not inlined. We still need it. */
680 if (!node
->global
.inlined_to
)
683 inlined_to
= node
->global
.inlined_to
;
685 /* NODE should have only one caller. */
686 gcc_assert (!node
->callers
->next_caller
);
688 if (!bitmap_bit_p (original_nodes
, inlined_to
->uid
))
690 bitmap_clear_bit (original_nodes
, node
->uid
);
691 cgraph_node_set_remove (set
, node
);
698 /* Transitively add to SET all the inline clones for every node that
700 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
702 node
= csi_node (csi
);
703 if (bitmap_bit_p (original_nodes
, node
->uid
))
704 lto_add_inline_clones (set
, node
, original_decls
);
707 lto_bitmap_free (original_nodes
);
708 lto_bitmap_free (original_decls
);
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. */
716 lto_promote_cross_file_statics (void)
718 struct varpool_node
*vnode
;
721 cgraph_node_set_iterator csi
;
723 gcc_assert (flag_wpa
);
725 /* At moment we make no attempt to figure out who is refering the variables,
726 so all must become global. */
727 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
728 if (!vnode
->externally_visible
&& vnode
->analyzed
)
730 TREE_PUBLIC (vnode
->decl
) = 1;
731 DECL_VISIBILITY (vnode
->decl
) = VISIBILITY_HIDDEN
;
733 n_sets
= VEC_length (cgraph_node_set
, lto_cgraph_node_sets
);
734 for (i
= 0; i
< n_sets
; i
++)
736 set
= VEC_index (cgraph_node_set
, lto_cgraph_node_sets
, i
);
738 /* If node has either address taken (and we have no clue from where)
739 or it is called from other partition, it needs to be globalized. */
740 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
742 struct cgraph_node
*node
= csi_node (csi
);
743 bool globalize
= node
->address_taken
|| node
->local
.vtable_method
;
744 struct cgraph_edge
*e
;
745 if (node
->local
.externally_visible
)
747 for (e
= node
->callers
; e
&& !globalize
; e
= e
->next_caller
)
749 struct cgraph_node
*caller
= e
->caller
;
750 if (caller
->global
.inlined_to
)
751 caller
= caller
->global
.inlined_to
;
752 if (!cgraph_node_in_set_p (caller
, set
))
757 TREE_PUBLIC (node
->decl
) = 1;
758 DECL_VISIBILITY (node
->decl
) = VISIBILITY_HIDDEN
;
761 struct cgraph_node
*alias
;
762 for (alias
= node
->same_body
;
763 alias
; alias
= alias
->next
)
765 TREE_PUBLIC (alias
->decl
) = 1;
766 DECL_VISIBILITY (alias
->decl
) = VISIBILITY_HIDDEN
;
776 /* Given a file name FNAME, return a string with FNAME prefixed with '*'. */
779 prefix_name_with_star (const char *fname
)
784 len
= strlen (fname
) + 1 + 1;
785 star_fname
= XNEWVEC (char, len
);
786 snprintf (star_fname
, len
, "*%s", fname
);
792 /* Return a copy of FNAME without the .o extension. */
795 strip_extension (const char *fname
)
797 char *s
= XNEWVEC (char, strlen (fname
) - 2 + 1);
798 gcc_assert (strstr (fname
, ".o"));
799 snprintf (s
, strlen (fname
) - 2 + 1, "%s", fname
);
805 /* Return a file name associated with cgraph node set SET. This may
806 be a new temporary file name if SET needs to be processed by
807 LTRANS, or the original file name if all the nodes in SET belong to
808 the same input file. */
811 get_filename_for_set (cgraph_node_set set
)
814 static const size_t max_fname_len
= 100;
816 if (cgraph_node_set_needs_ltrans_p (set
))
818 /* Create a new temporary file to store SET. To facilitate
819 debugging, use file names from SET as part of the new
820 temporary file name. */
821 cgraph_node_set_iterator si
;
822 struct pointer_set_t
*pset
= pointer_set_create ();
823 for (si
= csi_start (set
); !csi_end_p (si
); csi_next (&si
))
825 struct cgraph_node
*n
= csi_node (si
);
826 const char *node_fname
;
829 /* Don't use the same file name more than once. */
830 if (pointer_set_insert (pset
, n
->local
.lto_file_data
))
833 /* The first file name found in SET determines the output
834 directory. For the remaining files, we use their
836 node_fname
= n
->local
.lto_file_data
->file_name
;
839 fname
= strip_extension (node_fname
);
843 f
= strip_extension (lbasename (node_fname
));
845 /* If the new name causes an excessively long file name,
846 make the last component "___" to indicate overflow. */
847 if (strlen (fname
) + strlen (f
) > max_fname_len
- 3)
849 fname
= reconcat (fname
, fname
, "___", NULL
);
854 fname
= reconcat (fname
, fname
, "_", f
, NULL
);
859 pointer_set_destroy (pset
);
861 /* Add the extension .wpa.o to indicate that this file has been
863 fname
= reconcat (fname
, fname
, ".wpa.o", NULL
);
868 /* Since SET does not need to be processed by LTRANS, use
869 the original file name and mark it with a '*' prefix so that
870 lto_execute_ltrans knows not to process it. */
871 cgraph_node_set_iterator si
= csi_start (set
);
872 struct cgraph_node
*first
= csi_node (si
);
873 fname
= prefix_name_with_star (first
->local
.lto_file_data
->file_name
);
879 static lto_file
*current_lto_file
;
882 /* Write all output files in WPA mode. Returns a NULL-terminated array of
883 output file names. */
886 lto_wpa_write_files (void)
889 unsigned i
, n_sets
, last_out_file_ix
, num_out_files
;
892 varpool_node_set vset
;
894 timevar_push (TV_WHOPR_WPA
);
896 /* Include all inlined functions and determine what sets need to be
897 compiled by LTRANS. After this loop, only those sets that
898 contain callgraph nodes from more than one file will need to be
899 compiled by LTRANS. */
900 for (i
= 0; VEC_iterate (cgraph_node_set
, lto_cgraph_node_sets
, i
, set
); i
++)
902 lto_add_all_inlinees (set
);
903 lto_stats
.num_output_cgraph_nodes
+= VEC_length (cgraph_node_ptr
,
907 /* After adding all inlinees, find out statics that need to be promoted
908 to globals because of cross-file inlining. */
909 lto_promote_cross_file_statics ();
911 timevar_pop (TV_WHOPR_WPA
);
913 timevar_push (TV_WHOPR_WPA_IO
);
915 /* The number of output files depends on the number of input files
916 and how many callgraph node sets we create. Reserve enough space
917 for the maximum of these two. */
918 num_out_files
= MAX (VEC_length (cgraph_node_set
, lto_cgraph_node_sets
),
920 output_files
= XNEWVEC (char *, num_out_files
+ 1);
922 n_sets
= VEC_length (cgraph_node_set
, lto_cgraph_node_sets
);
923 for (i
= 0; i
< n_sets
; i
++)
927 set
= VEC_index (cgraph_node_set
, lto_cgraph_node_sets
, i
);
928 vset
= VEC_index (varpool_node_set
, lto_varpool_node_sets
, i
);
929 temp_filename
= get_filename_for_set (set
);
930 output_files
[i
] = temp_filename
;
932 if (cgraph_node_set_needs_ltrans_p (set
))
934 /* Write all the nodes in SET to TEMP_FILENAME. */
935 file
= lto_obj_file_open (temp_filename
, true);
937 fatal_error ("lto_obj_file_open() failed");
939 lto_set_current_out_file (file
);
941 ipa_write_optimization_summaries (set
, vset
);
943 lto_set_current_out_file (NULL
);
944 lto_obj_file_close (file
);
948 last_out_file_ix
= n_sets
;
950 lto_stats
.num_output_files
+= n_sets
;
952 output_files
[last_out_file_ix
] = NULL
;
954 timevar_pop (TV_WHOPR_WPA_IO
);
959 /* Template of LTRANS dumpbase suffix. */
960 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
962 /* Perform local transformations (LTRANS) on the files in the NULL-terminated
963 FILES array. These should have been written previously by
964 lto_wpa_write_files (). Transformations are performed via executing
965 COLLECT_GCC for reach file. */
968 lto_execute_ltrans (char *const *files
)
971 const char *collect_gcc_options
, *collect_gcc
;
972 struct obstack env_obstack
;
974 const char **argv_ptr
;
979 FILE *ltrans_output_list_stream
= NULL
;
980 bool seen_dumpbase
= false;
981 char *dumpbase_suffix
= NULL
;
983 timevar_push (TV_WHOPR_WPA_LTRANS_EXEC
);
985 /* Get the driver and options. */
986 collect_gcc
= getenv ("COLLECT_GCC");
988 fatal_error ("environment variable COLLECT_GCC must be set");
990 /* Set the CFLAGS environment variable. */
991 collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
992 if (!collect_gcc_options
)
993 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
995 /* Count arguments. */
997 for (j
= 0; collect_gcc_options
[j
] != '\0'; ++j
)
998 if (collect_gcc_options
[j
] == '\'')
1002 fatal_error ("malformed COLLECT_GCC_OPTIONS");
1004 /* Initalize the arguments for the LTRANS driver. */
1005 argv
= XNEWVEC (const char *, 8 + i
/ 2);
1007 *argv_ptr
++ = collect_gcc
;
1008 *argv_ptr
++ = "-xlto";
1009 for (j
= 0; collect_gcc_options
[j
] != '\0'; ++j
)
1010 if (collect_gcc_options
[j
] == '\'')
1016 while (collect_gcc_options
[j
] != '\'')
1018 obstack_init (&env_obstack
);
1019 obstack_grow (&env_obstack
, &collect_gcc_options
[i
], j
- i
);
1021 obstack_grow (&env_obstack
, DUMPBASE_SUFFIX
,
1022 sizeof (DUMPBASE_SUFFIX
));
1024 obstack_1grow (&env_obstack
, 0);
1025 option
= XOBFINISH (&env_obstack
, char *);
1028 dumpbase_suffix
= option
+ 7 + j
- i
;
1029 seen_dumpbase
= false;
1032 /* LTRANS does not need -fwpa nor -fltrans-*. */
1033 if (strncmp (option
, "-fwpa", 5) != 0
1034 && strncmp (option
, "-fltrans-", 9) != 0)
1036 if (strncmp (option
, "-dumpbase", 9) == 0)
1037 seen_dumpbase
= true;
1038 *argv_ptr
++ = option
;
1041 *argv_ptr
++ = "-fltrans";
1043 /* Open the LTRANS output list. */
1044 if (ltrans_output_list
)
1046 ltrans_output_list_stream
= fopen (ltrans_output_list
, "w");
1047 if (ltrans_output_list_stream
== NULL
)
1048 error ("opening LTRANS output list %s: %m", ltrans_output_list
);
1051 for (i
= 0; files
[i
]; ++i
)
1055 /* If the file is prefixed with a '*', it means that we do not
1056 need to re-compile it with LTRANS because it has not been
1057 modified by WPA. Skip it from the command line to
1058 lto_execute_ltrans, but add it to ltrans_output_list_stream
1059 so it is linked after we are done. */
1060 if (files
[i
][0] == '*')
1062 size_t len
= strlen (files
[i
]) - 1;
1063 if (ltrans_output_list_stream
)
1064 if (fwrite (&files
[i
][1], 1, len
, ltrans_output_list_stream
) < len
1065 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
1066 error ("writing to LTRANS output list %s: %m",
1067 ltrans_output_list
);
1073 /* Otherwise, add FILES[I] to lto_execute_ltrans command line
1074 and add the resulting file to LTRANS output list. */
1076 /* Replace the .o suffix with a .ltrans.o suffix and write
1077 the resulting name to the LTRANS output list. */
1078 obstack_init (&env_obstack
);
1079 obstack_grow (&env_obstack
, files
[i
], strlen (files
[i
]) - 2);
1080 obstack_grow (&env_obstack
, ".ltrans.o", sizeof (".ltrans.o"));
1081 output_name
= XOBFINISH (&env_obstack
, char *);
1082 if (ltrans_output_list_stream
)
1084 len
= strlen (output_name
);
1086 if (fwrite (output_name
, 1, len
, ltrans_output_list_stream
) < len
1087 || fwrite ("\n", 1, 1, ltrans_output_list_stream
) < 1)
1088 error ("writing to LTRANS output list %s: %m",
1089 ltrans_output_list
);
1093 argv_ptr
[1] = output_name
;
1094 argv_ptr
[2] = files
[i
];
1097 /* Append a sequence number to -dumpbase for LTRANS. */
1098 if (dumpbase_suffix
)
1099 snprintf (dumpbase_suffix
, sizeof (DUMPBASE_SUFFIX
) - 7,
1100 "%lu", (unsigned long) i
);
1102 /* Execute the driver. */
1103 pex
= pex_init (0, "lto1", NULL
);
1105 fatal_error ("pex_init failed: %s", xstrerror (errno
));
1107 errmsg
= pex_run (pex
, PEX_LAST
| PEX_SEARCH
, argv
[0],
1108 CONST_CAST (char **, argv
), NULL
, NULL
, &err
);
1110 fatal_error ("%s: %s", errmsg
, xstrerror (err
));
1112 if (!pex_get_status (pex
, 1, &status
))
1113 fatal_error ("can't get program status: %s", xstrerror (errno
));
1117 if (WIFSIGNALED (status
))
1119 int sig
= WTERMSIG (status
);
1120 fatal_error ("%s terminated with signal %d [%s]%s",
1121 argv
[0], sig
, strsignal (sig
),
1122 WCOREDUMP (status
) ? ", core dumped" : "");
1125 fatal_error ("%s terminated with status %d", argv
[0], status
);
1132 /* Close the LTRANS output list. */
1133 if (ltrans_output_list_stream
&& fclose (ltrans_output_list_stream
))
1134 error ("closing LTRANS output list %s: %m", ltrans_output_list
);
1136 obstack_free (&env_obstack
, NULL
);
1139 timevar_pop (TV_WHOPR_WPA_LTRANS_EXEC
);
1144 struct pointer_set_t
*seen
;
1147 #define LTO_FIXUP_SUBTREE(t) \
1149 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1152 #define LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE(t) \
1156 (t) = gimple_register_type (t); \
1157 walk_tree (&(t), lto_fixup_tree, data, NULL); \
1161 static tree
lto_fixup_tree (tree
*, int *, void *);
1163 /* Return true if T does not need to be fixed up recursively. */
1169 || CONSTANT_CLASS_P (t
)
1170 || TREE_CODE (t
) == IDENTIFIER_NODE
);
1173 /* Fix up fields of a tree_common T. DATA points to fix-up states. */
1176 lto_fixup_common (tree t
, void *data
)
1178 /* The following re-creates the TYPE_REFERENCE_TO and TYPE_POINTER_TO
1179 lists. We do not stream TYPE_REFERENCE_TO, TYPE_POINTER_TO or
1180 TYPE_NEXT_PTR_TO and TYPE_NEXT_REF_TO.
1181 First remove us from any pointer list we are on. */
1182 if (TREE_CODE (t
) == POINTER_TYPE
)
1184 if (TYPE_POINTER_TO (TREE_TYPE (t
)) == t
)
1185 TYPE_POINTER_TO (TREE_TYPE (t
)) = TYPE_NEXT_PTR_TO (t
);
1188 tree tem
= TYPE_POINTER_TO (TREE_TYPE (t
));
1189 while (tem
&& TYPE_NEXT_PTR_TO (tem
) != t
)
1190 tem
= TYPE_NEXT_PTR_TO (tem
);
1192 TYPE_NEXT_PTR_TO (tem
) = TYPE_NEXT_PTR_TO (t
);
1194 TYPE_NEXT_PTR_TO (t
) = NULL_TREE
;
1196 else if (TREE_CODE (t
) == REFERENCE_TYPE
)
1198 if (TYPE_REFERENCE_TO (TREE_TYPE (t
)) == t
)
1199 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = TYPE_NEXT_REF_TO (t
);
1202 tree tem
= TYPE_REFERENCE_TO (TREE_TYPE (t
));
1203 while (tem
&& TYPE_NEXT_REF_TO (tem
) != t
)
1204 tem
= TYPE_NEXT_REF_TO (tem
);
1206 TYPE_NEXT_REF_TO (tem
) = TYPE_NEXT_REF_TO (t
);
1208 TYPE_NEXT_REF_TO (t
) = NULL_TREE
;
1211 /* Fixup our type. */
1212 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t
));
1214 /* Second put us on the list of pointers of the new pointed-to type
1215 if we are a main variant. This is done in lto_fixup_type after
1216 fixing up our main variant. */
1218 /* This is not very efficient because we cannot do tail-recursion with
1219 a long chain of trees. */
1220 LTO_FIXUP_SUBTREE (TREE_CHAIN (t
));
1223 /* Fix up fields of a decl_minimal T. DATA points to fix-up states. */
1226 lto_fixup_decl_minimal (tree t
, void *data
)
1228 lto_fixup_common (t
, data
);
1229 LTO_FIXUP_SUBTREE (DECL_NAME (t
));
1230 LTO_FIXUP_SUBTREE (DECL_CONTEXT (t
));
1233 /* Fix up fields of a decl_common T. DATA points to fix-up states. */
1236 lto_fixup_decl_common (tree t
, void *data
)
1238 lto_fixup_decl_minimal (t
, data
);
1239 LTO_FIXUP_SUBTREE (DECL_SIZE (t
));
1240 LTO_FIXUP_SUBTREE (DECL_SIZE_UNIT (t
));
1241 LTO_FIXUP_SUBTREE (DECL_INITIAL (t
));
1242 LTO_FIXUP_SUBTREE (DECL_ATTRIBUTES (t
));
1243 LTO_FIXUP_SUBTREE (DECL_ABSTRACT_ORIGIN (t
));
1246 /* Fix up fields of a decl_with_vis T. DATA points to fix-up states. */
1249 lto_fixup_decl_with_vis (tree t
, void *data
)
1251 lto_fixup_decl_common (t
, data
);
1253 /* Accessor macro has side-effects, use field-name here. */
1254 LTO_FIXUP_SUBTREE (t
->decl_with_vis
.assembler_name
);
1256 gcc_assert (no_fixup_p (DECL_SECTION_NAME (t
)));
1259 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1262 lto_fixup_decl_non_common (tree t
, void *data
)
1264 lto_fixup_decl_with_vis (t
, data
);
1265 LTO_FIXUP_SUBTREE (DECL_ARGUMENT_FLD (t
));
1266 LTO_FIXUP_SUBTREE (DECL_RESULT_FLD (t
));
1267 LTO_FIXUP_SUBTREE (DECL_VINDEX (t
));
1269 /* SAVED_TREE should not cleared by now. Also no accessor for base type. */
1270 gcc_assert (no_fixup_p (t
->decl_non_common
.saved_tree
));
1273 /* Fix up fields of a decl_non_common T. DATA points to fix-up states. */
1276 lto_fixup_function (tree t
, void *data
)
1278 lto_fixup_decl_non_common (t
, data
);
1279 LTO_FIXUP_SUBTREE (DECL_FUNCTION_PERSONALITY (t
));
1282 /* Fix up fields of a field_decl T. DATA points to fix-up states. */
1285 lto_fixup_field_decl (tree t
, void *data
)
1287 lto_fixup_decl_common (t
, data
);
1288 LTO_FIXUP_SUBTREE (DECL_FIELD_OFFSET (t
));
1289 LTO_FIXUP_SUBTREE (DECL_BIT_FIELD_TYPE (t
));
1290 LTO_FIXUP_SUBTREE (DECL_QUALIFIER (t
));
1291 gcc_assert (no_fixup_p (DECL_FIELD_BIT_OFFSET (t
)));
1292 LTO_FIXUP_SUBTREE (DECL_FCONTEXT (t
));
1295 /* Fix up fields of a type T. DATA points to fix-up states. */
1298 lto_fixup_type (tree t
, void *data
)
1302 lto_fixup_common (t
, data
);
1303 LTO_FIXUP_SUBTREE (TYPE_CACHED_VALUES (t
));
1304 LTO_FIXUP_SUBTREE (TYPE_SIZE (t
));
1305 LTO_FIXUP_SUBTREE (TYPE_SIZE_UNIT (t
));
1306 LTO_FIXUP_SUBTREE (TYPE_ATTRIBUTES (t
));
1307 LTO_FIXUP_SUBTREE (TYPE_NAME (t
));
1309 /* Accessors are for derived node types only. */
1310 if (!POINTER_TYPE_P (t
))
1311 LTO_FIXUP_SUBTREE (t
->type
.minval
);
1312 LTO_FIXUP_SUBTREE (t
->type
.maxval
);
1314 /* Accessor is for derived node types only. */
1315 LTO_FIXUP_SUBTREE (t
->type
.binfo
);
1317 if (TYPE_CONTEXT (t
))
1319 if (TYPE_P (TYPE_CONTEXT (t
)))
1320 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CONTEXT (t
));
1322 LTO_FIXUP_SUBTREE (TYPE_CONTEXT (t
));
1324 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TYPE_CANONICAL (t
));
1326 /* The following re-creates proper variant lists while fixing up
1327 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1328 variant list state before fixup is broken. */
1330 /* Remove us from our main variant list if we are not the variant leader. */
1331 if (TYPE_MAIN_VARIANT (t
) != t
)
1333 tem
= TYPE_MAIN_VARIANT (t
);
1334 while (tem
&& TYPE_NEXT_VARIANT (tem
) != t
)
1335 tem
= TYPE_NEXT_VARIANT (tem
);
1337 TYPE_NEXT_VARIANT (tem
) = TYPE_NEXT_VARIANT (t
);
1338 TYPE_NEXT_VARIANT (t
) = NULL_TREE
;
1341 /* Query our new main variant. */
1342 mv
= gimple_register_type (TYPE_MAIN_VARIANT (t
));
1344 /* If we were the variant leader and we get replaced ourselves drop
1345 all variants from our list. */
1346 if (TYPE_MAIN_VARIANT (t
) == t
1352 tree tem2
= TYPE_NEXT_VARIANT (tem
);
1353 TYPE_NEXT_VARIANT (tem
) = NULL_TREE
;
1358 /* If we are not our own variant leader link us into our new leaders
1362 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (mv
);
1363 TYPE_NEXT_VARIANT (mv
) = t
;
1366 /* Finally adjust our main variant and fix it up. */
1367 TYPE_MAIN_VARIANT (t
) = mv
;
1368 LTO_FIXUP_SUBTREE (TYPE_MAIN_VARIANT (t
));
1370 /* As the second step of reconstructing the pointer chains put us
1371 on the list of pointers of the new pointed-to type
1372 if we are a main variant. See lto_fixup_common for the first step. */
1373 if (TREE_CODE (t
) == POINTER_TYPE
1374 && TYPE_MAIN_VARIANT (t
) == t
)
1376 TYPE_NEXT_PTR_TO (t
) = TYPE_POINTER_TO (TREE_TYPE (t
));
1377 TYPE_POINTER_TO (TREE_TYPE (t
)) = t
;
1379 else if (TREE_CODE (t
) == REFERENCE_TYPE
1380 && TYPE_MAIN_VARIANT (t
) == t
)
1382 TYPE_NEXT_REF_TO (t
) = TYPE_REFERENCE_TO (TREE_TYPE (t
));
1383 TYPE_REFERENCE_TO (TREE_TYPE (t
)) = t
;
1387 /* Fix up fields of a BINFO T. DATA points to fix-up states. */
1390 lto_fixup_binfo (tree t
, void *data
)
1392 unsigned HOST_WIDE_INT i
, n
;
1393 tree base
, saved_base
;
1395 lto_fixup_common (t
, data
);
1396 gcc_assert (no_fixup_p (BINFO_OFFSET (t
)));
1397 LTO_FIXUP_SUBTREE (BINFO_VTABLE (t
));
1398 LTO_FIXUP_SUBTREE (BINFO_VIRTUALS (t
));
1399 LTO_FIXUP_SUBTREE (BINFO_VPTR_FIELD (t
));
1400 n
= VEC_length (tree
, BINFO_BASE_ACCESSES (t
));
1401 for (i
= 0; i
< n
; i
++)
1403 saved_base
= base
= BINFO_BASE_ACCESS (t
, i
);
1404 LTO_FIXUP_SUBTREE (base
);
1405 if (base
!= saved_base
)
1406 VEC_replace (tree
, BINFO_BASE_ACCESSES (t
), i
, base
);
1408 LTO_FIXUP_SUBTREE (BINFO_INHERITANCE_CHAIN (t
));
1409 LTO_FIXUP_SUBTREE (BINFO_SUBVTT_INDEX (t
));
1410 LTO_FIXUP_SUBTREE (BINFO_VPTR_INDEX (t
));
1411 n
= BINFO_N_BASE_BINFOS (t
);
1412 for (i
= 0; i
< n
; i
++)
1414 saved_base
= base
= BINFO_BASE_BINFO (t
, i
);
1415 LTO_FIXUP_SUBTREE (base
);
1416 if (base
!= saved_base
)
1417 VEC_replace (tree
, BINFO_BASE_BINFOS (t
), i
, base
);
1421 /* Fix up fields of a CONSTRUCTOR T. DATA points to fix-up states. */
1424 lto_fixup_constructor (tree t
, void *data
)
1426 unsigned HOST_WIDE_INT idx
;
1427 constructor_elt
*ce
;
1429 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t
));
1432 VEC_iterate(constructor_elt
, CONSTRUCTOR_ELTS (t
), idx
, ce
);
1435 LTO_FIXUP_SUBTREE (ce
->index
);
1436 LTO_FIXUP_SUBTREE (ce
->value
);
1440 /* A walk_tree callback used by lto_fixup_state. TP is the pointer to the
1441 current tree. WALK_SUBTREES indicates if the subtrees will be walked.
1442 DATA is a pointer set to record visited nodes. */
1445 lto_fixup_tree (tree
*tp
, int *walk_subtrees
, void *data
)
1448 lto_fixup_data_t
*fixup_data
= (lto_fixup_data_t
*) data
;
1453 if (pointer_set_contains (fixup_data
->seen
, t
))
1456 if (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == FUNCTION_DECL
)
1458 prevailing
= lto_symtab_prevailing_decl (t
);
1460 if (t
!= prevailing
)
1462 /* Also replace t with prevailing defintion. We don't want to
1463 insert the other defintion in the seen set as we want to
1464 replace all instances of it. */
1469 else if (TYPE_P (t
))
1471 /* Replace t with the prevailing type. We don't want to insert the
1472 other type in the seen set as we want to replace all instances of it. */
1473 t
= gimple_register_type (t
);
1477 if (pointer_set_insert (fixup_data
->seen
, t
))
1480 /* walk_tree does not visit all reachable nodes that need to be fixed up.
1481 Hence we do special processing here for those kind of nodes. */
1482 switch (TREE_CODE (t
))
1485 lto_fixup_field_decl (t
, data
);
1493 lto_fixup_decl_common (t
, data
);
1497 lto_fixup_decl_with_vis (t
, data
);
1501 lto_fixup_decl_non_common (t
, data
);
1505 lto_fixup_function (t
, data
);
1509 lto_fixup_binfo (t
, data
);
1514 lto_fixup_type (t
, data
);
1515 else if (TREE_CODE (t
) == CONSTRUCTOR
)
1516 lto_fixup_constructor (t
, data
);
1517 else if (CONSTANT_CLASS_P (t
))
1518 LTO_REGISTER_TYPE_AND_FIXUP_SUBTREE (TREE_TYPE (t
));
1519 else if (EXPR_P (t
))
1521 /* walk_tree only handles TREE_OPERANDs. Do the rest here. */
1522 lto_fixup_common (t
, data
);
1523 LTO_FIXUP_SUBTREE (t
->exp
.block
);
1528 /* Let walk_tree handle sub-trees. */
1536 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
1537 replaces var and function decls with the corresponding prevailing def and
1538 records the old decl in the free-list in DATA. We also record visted nodes
1539 in the seen-set in DATA to avoid multiple visit for nodes that need not
1543 lto_fixup_state (struct lto_in_decl_state
*state
, lto_fixup_data_t
*data
)
1546 struct lto_tree_ref_table
*table
;
1548 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
1549 we still need to walk from all DECLs to find the reachable
1550 FUNCTION_DECLs and VAR_DECLs. */
1551 for (si
= 0; si
< LTO_N_DECL_STREAMS
; si
++)
1553 table
= &state
->streams
[si
];
1554 for (i
= 0; i
< table
->size
; i
++)
1555 walk_tree (table
->trees
+ i
, lto_fixup_tree
, data
, NULL
);
1559 /* A callback of htab_traverse. Just extract a state from SLOT and the
1560 lto_fixup_data_t object from AUX and calls lto_fixup_state. */
1563 lto_fixup_state_aux (void **slot
, void *aux
)
1565 struct lto_in_decl_state
*state
= (struct lto_in_decl_state
*) *slot
;
1566 lto_fixup_state (state
, (lto_fixup_data_t
*) aux
);
1570 /* Fix the decls from all FILES. Replaces each decl with the corresponding
1574 lto_fixup_decls (struct lto_file_decl_data
**files
)
1578 struct pointer_set_t
*seen
= pointer_set_create ();
1579 lto_fixup_data_t data
;
1582 for (i
= 0; files
[i
]; i
++)
1584 struct lto_file_decl_data
*file
= files
[i
];
1585 struct lto_in_decl_state
*state
= file
->global_decl_state
;
1586 lto_fixup_state (state
, &data
);
1588 htab_traverse (file
->function_decl_states
, lto_fixup_state_aux
, &data
);
1591 for (i
= 0; VEC_iterate (tree
, lto_global_var_decls
, i
, decl
); i
++)
1593 tree saved_decl
= decl
;
1594 walk_tree (&decl
, lto_fixup_tree
, &data
, NULL
);
1595 if (decl
!= saved_decl
)
1596 VEC_replace (tree
, lto_global_var_decls
, i
, decl
);
1599 pointer_set_destroy (seen
);
1602 /* Unlink a temporary LTRANS file unless requested otherwise. */
1605 lto_maybe_unlink (const char *file
)
1607 if (!getenv ("WPA_SAVE_LTRANS"))
1609 if (unlink_if_ordinary (file
))
1610 error ("deleting LTRANS input file %s: %m", file
);
1613 fprintf (stderr
, "[Leaving LTRANS input file %s]\n", file
);
1616 /* Read the options saved from each file in the command line. Called
1617 from lang_hooks.post_options which is called by process_options
1618 right before all the options are used to initialize the compiler.
1619 This assumes that decode_options has already run, so the
1620 num_in_fnames and in_fnames are properly set.
1622 Note that this assumes that all the files had been compiled with
1623 the same options, which is not a good assumption. In general,
1624 options ought to be read from all the files in the set and merged.
1625 However, it is still unclear what the merge rules should be. */
1628 lto_read_all_file_options (void)
1632 /* Clear any file options currently saved. */
1633 lto_clear_file_options ();
1635 /* Set the hooks to read ELF sections. */
1636 lto_set_in_hooks (NULL
, get_section_data
, free_section_data
);
1638 for (i
= 0; i
< num_in_fnames
; i
++)
1640 struct lto_file_decl_data
*file_data
;
1641 lto_file
*file
= lto_obj_file_open (in_fnames
[i
], false);
1645 file_data
= XCNEW (struct lto_file_decl_data
);
1646 file_data
->file_name
= file
->filename
;
1647 file_data
->section_hash_table
= lto_obj_build_section_table (file
);
1649 lto_read_file_options (file_data
);
1651 lto_obj_file_close (file
);
1652 htab_delete (file_data
->section_hash_table
);
1656 /* Apply globally the options read from all the files. */
1657 lto_reissue_options ();
1661 /* Read all the symbols from the input files FNAMES. NFILES is the
1662 number of files requested in the command line. Instantiate a
1663 global call graph by aggregating all the sub-graphs found in each
1667 read_cgraph_and_symbols (unsigned nfiles
, const char **fnames
)
1669 unsigned int i
, last_file_ix
;
1670 struct lto_file_decl_data
**all_file_decl_data
;
1672 struct cgraph_node
*node
;
1674 lto_stats
.num_input_files
= nfiles
;
1676 timevar_push (TV_IPA_LTO_DECL_IO
);
1678 /* Set the hooks so that all of the ipa passes can read in their data. */
1679 all_file_decl_data
= XNEWVEC (struct lto_file_decl_data
*, nfiles
+ 1);
1680 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
1682 /* Read the resolution file. */
1684 if (resolution_file_name
)
1687 unsigned num_objects
;
1689 resolution
= fopen (resolution_file_name
, "r");
1690 if (resolution
== NULL
)
1691 fatal_error ("could not open symbol resolution file: %s",
1694 t
= fscanf (resolution
, "%u", &num_objects
);
1695 gcc_assert (t
== 1);
1697 /* True, since the plugin splits the archives. */
1698 gcc_assert (num_objects
== nfiles
);
1702 fprintf (stderr
, "Reading object files:");
1704 /* Read all of the object files specified on the command line. */
1705 for (i
= 0, last_file_ix
= 0; i
< nfiles
; ++i
)
1707 struct lto_file_decl_data
*file_data
= NULL
;
1710 fprintf (stderr
, " %s", fnames
[i
]);
1714 current_lto_file
= lto_obj_file_open (fnames
[i
], false);
1715 if (!current_lto_file
)
1718 file_data
= lto_file_read (current_lto_file
, resolution
);
1722 all_file_decl_data
[last_file_ix
++] = file_data
;
1724 lto_obj_file_close (current_lto_file
);
1725 current_lto_file
= NULL
;
1728 if (resolution_file_name
)
1729 fclose (resolution
);
1731 all_file_decl_data
[last_file_ix
] = NULL
;
1733 /* Set the hooks so that all of the ipa passes can read in their data. */
1734 lto_set_in_hooks (all_file_decl_data
, get_section_data
, free_section_data
);
1736 /* Each pass will set the appropriate timer. */
1737 timevar_pop (TV_IPA_LTO_DECL_IO
);
1740 fprintf (stderr
, "\nReading the callgraph\n");
1742 /* Read the callgraph. */
1746 fprintf (stderr
, "Merging declarations\n");
1748 /* Merge global decls. */
1749 lto_symtab_merge_decls ();
1751 /* Fixup all decls and types and free the type hash tables. */
1752 lto_fixup_decls (all_file_decl_data
);
1753 free_gimple_type_tables ();
1756 fprintf (stderr
, "Reading summaries\n");
1758 /* Read the IPA summary data. */
1760 ipa_read_optimization_summaries ();
1762 ipa_read_summaries ();
1764 /* Finally merge the cgraph according to the decl merging decisions. */
1765 lto_symtab_merge_cgraph_nodes ();
1768 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1770 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
1771 summaries computed and needs to apply changes. At the moment WHOPR only
1772 supports inlining, so we can push it here by hand. In future we need to stream
1773 this field into ltrans compilation. */
1775 VEC_safe_push (ipa_opt_pass
, heap
,
1776 node
->ipa_transforms_to_apply
,
1777 (ipa_opt_pass
)&pass_ipa_inline
);
1780 timevar_push (TV_IPA_LTO_DECL_IO
);
1782 /* FIXME lto. This loop needs to be changed to use the pass manager to
1783 call the ipa passes directly. */
1785 for (i
= 0; i
< last_file_ix
; i
++)
1787 struct lto_file_decl_data
*file_data
= all_file_decl_data
[i
];
1788 lto_materialize_constructors_and_inits (file_data
);
1791 /* Indicate that the cgraph is built and ready. */
1792 cgraph_function_flags_ready
= true;
1794 timevar_pop (TV_IPA_LTO_DECL_IO
);
1798 /* Materialize all the bodies for all the nodes in the callgraph. */
1801 materialize_cgraph (void)
1804 struct cgraph_node
*node
;
1806 timevar_id_t lto_timer
;
1810 flag_wpa
? "Materializing decls:" : "Reading function bodies:");
1813 /* Now that we have input the cgraph, we need to clear all of the aux
1814 nodes and read the functions if we are not running in WPA mode. */
1815 timevar_push (TV_IPA_LTO_GIMPLE_IO
);
1817 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1819 /* Some cgraph nodes get created on the fly, and they don't need
1820 to be materialized. For instance, nodes for nested functions
1821 where the parent function was not streamed out or builtin
1822 functions. Additionally, builtin functions should not be
1823 materialized and may, in fact, cause confusion because there
1824 may be a regular function in the file whose assembler name
1825 matches that of the function.
1826 See gcc.c-torture/execute/20030125-1.c and
1827 gcc.c-torture/execute/921215-1.c. */
1828 if (node
->local
.lto_file_data
1829 && !DECL_IS_BUILTIN (node
->decl
))
1831 announce_function (node
->decl
);
1832 lto_materialize_function (node
);
1833 lto_stats
.num_input_cgraph_nodes
++;
1837 timevar_pop (TV_IPA_LTO_GIMPLE_IO
);
1839 /* Start the appropriate timer depending on the mode that we are
1841 lto_timer
= (flag_wpa
) ? TV_WHOPR_WPA
1842 : (flag_ltrans
) ? TV_WHOPR_LTRANS
1844 timevar_push (lto_timer
);
1846 current_function_decl
= NULL
;
1849 /* Inform the middle end about the global variables we have seen. */
1850 for (i
= 0; VEC_iterate (tree
, lto_global_var_decls
, i
, decl
); i
++)
1851 rest_of_decl_compilation (decl
, 1, 0);
1854 fprintf (stderr
, "\n");
1856 timevar_pop (lto_timer
);
1860 /* Perform whole program analysis (WPA) on the callgraph and write out the
1861 optimization plan. */
1864 do_whole_program_analysis (void)
1866 char **output_files
;
1869 /* Note that since we are in WPA mode, materialize_cgraph will not
1870 actually read in all the function bodies. It only materializes
1871 the decls and cgraph nodes so that analysis can be performed. */
1872 materialize_cgraph ();
1874 /* Reading in the cgraph uses different timers, start timing WPA now. */
1875 timevar_push (TV_WHOPR_WPA
);
1877 cgraph_function_flags_ready
= true;
1878 bitmap_obstack_initialize (NULL
);
1879 ipa_register_cgraph_hooks ();
1880 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
1882 execute_ipa_pass_list (all_regular_ipa_passes
);
1885 bitmap_obstack_release (NULL
);
1887 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
1888 timevar_pop (TV_WHOPR_WPA
);
1894 fprintf (stderr
, "\nStreaming out");
1897 output_files
= lto_wpa_write_files ();
1899 fprintf (stderr
, "\n");
1901 /* Show the LTO report before launching LTRANS. */
1902 if (flag_lto_report
)
1903 print_lto_report ();
1905 lto_execute_ltrans (output_files
);
1907 for (i
= 0; output_files
[i
]; ++i
)
1909 if (output_files
[i
][0] != '*')
1910 lto_maybe_unlink (output_files
[i
]);
1912 free (output_files
[i
]);
1915 XDELETEVEC (output_files
);
1919 /* Main entry point for the GIMPLE front end. This front end has
1920 three main personalities:
1922 - LTO (-flto). All the object files on the command line are
1923 loaded in memory and processed as a single translation unit.
1924 This is the traditional link-time optimization behavior.
1926 - WPA (-fwpa). Only the callgraph and summary information for
1927 files in the command file are loaded. A single callgraph
1928 (without function bodies) is instantiated for the whole set of
1929 files. IPA passes are only allowed to analyze the call graph
1930 and make transformation decisions. The callgraph is
1931 partitioned, each partition is written to a new object file
1932 together with the transformation decisions.
1934 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
1935 summary files from running again. Since WPA computed summary
1936 information and decided what transformations to apply, LTRANS
1937 simply applies them. */
1940 lto_main (int debug_p ATTRIBUTE_UNUSED
)
1944 /* Read all the symbols and call graph from all the files in the
1946 read_cgraph_and_symbols (num_in_fnames
, in_fnames
);
1950 /* If WPA is enabled analyze the whole call graph and create an
1951 optimization plan. Otherwise, read in all the function
1952 bodies and continue with optimization. */
1954 do_whole_program_analysis ();
1957 materialize_cgraph ();
1959 /* Let the middle end know that we have read and merged all of
1963 /* FIXME lto, if the processes spawned by WPA fail, we miss
1964 the chance to print WPA's report, so WPA will call
1965 print_lto_report before launching LTRANS. If LTRANS was
1966 launched directly by the driver we would not need to do
1968 if (flag_lto_report
)
1969 print_lto_report ();
1974 #include "gt-lto-lto.h"