OSDN Git Service

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