OSDN Git Service

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