OSDN Git Service

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