OSDN Git Service

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