OSDN Git Service

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