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