OSDN Git Service

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