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