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