OSDN Git Service

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