OSDN Git Service

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