PR lto/81004
[official-gcc.git] / gcc / lto / lto.c
blob0ac8b1ef4911cff6a0b036ffdfdde9658d513e09
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2018 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 "tm.h"
25 #include "function.h"
26 #include "bitmap.h"
27 #include "basic-block.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "alloc-pool.h"
32 #include "tree-pass.h"
33 #include "tree-streamer.h"
34 #include "cgraph.h"
35 #include "opts.h"
36 #include "toplev.h"
37 #include "stor-layout.h"
38 #include "symbol-summary.h"
39 #include "tree-vrp.h"
40 #include "ipa-prop.h"
41 #include "common.h"
42 #include "debug.h"
43 #include "lto.h"
44 #include "lto-section-names.h"
45 #include "splay-tree.h"
46 #include "lto-partition.h"
47 #include "context.h"
48 #include "pass_manager.h"
49 #include "ipa-fnsummary.h"
50 #include "params.h"
51 #include "ipa-utils.h"
52 #include "gomp-constants.h"
53 #include "lto-symtab.h"
54 #include "stringpool.h"
55 #include "fold-const.h"
56 #include "attribs.h"
57 #include "builtins.h"
60 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
61 static int lto_parallelism;
63 static GTY(()) tree first_personality_decl;
65 static GTY(()) const unsigned char *lto_mode_identity_table;
67 /* Returns a hash code for P. */
69 static hashval_t
70 hash_name (const void *p)
72 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
73 return (hashval_t) htab_hash_string (ds->name);
77 /* Returns nonzero if P1 and P2 are equal. */
79 static int
80 eq_name (const void *p1, const void *p2)
82 const struct lto_section_slot *s1 =
83 (const struct lto_section_slot *) p1;
84 const struct lto_section_slot *s2 =
85 (const struct lto_section_slot *) p2;
87 return strcmp (s1->name, s2->name) == 0;
90 /* Free lto_section_slot */
92 static void
93 free_with_string (void *arg)
95 struct lto_section_slot *s = (struct lto_section_slot *)arg;
97 free (CONST_CAST (char *, s->name));
98 free (arg);
101 /* Create section hash table */
103 htab_t
104 lto_obj_create_section_hash_table (void)
106 return htab_create (37, hash_name, eq_name, free_with_string);
109 /* Delete an allocated integer KEY in the splay tree. */
111 static void
112 lto_splay_tree_delete_id (splay_tree_key key)
114 free ((void *) key);
117 /* Compare splay tree node ids A and B. */
119 static int
120 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
122 unsigned HOST_WIDE_INT ai;
123 unsigned HOST_WIDE_INT bi;
125 ai = *(unsigned HOST_WIDE_INT *) a;
126 bi = *(unsigned HOST_WIDE_INT *) b;
128 if (ai < bi)
129 return -1;
130 else if (ai > bi)
131 return 1;
132 return 0;
135 /* Look up splay tree node by ID in splay tree T. */
137 static splay_tree_node
138 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
140 return splay_tree_lookup (t, (splay_tree_key) &id);
143 /* Check if KEY has ID. */
145 static bool
146 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
148 return *(unsigned HOST_WIDE_INT *) key == id;
151 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
152 The ID is allocated separately because we need HOST_WIDE_INTs which may
153 be wider than a splay_tree_key. */
155 static void
156 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
157 struct lto_file_decl_data *file_data)
159 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
160 *idp = id;
161 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
164 /* Create a splay tree. */
166 static splay_tree
167 lto_splay_tree_new (void)
169 return splay_tree_new (lto_splay_tree_compare_ids,
170 lto_splay_tree_delete_id,
171 NULL);
174 /* Return true when NODE has a clone that is analyzed (i.e. we need
175 to load its body even if the node itself is not needed). */
177 static bool
178 has_analyzed_clone_p (struct cgraph_node *node)
180 struct cgraph_node *orig = node;
181 node = node->clones;
182 if (node)
183 while (node != orig)
185 if (node->analyzed)
186 return true;
187 if (node->clones)
188 node = node->clones;
189 else if (node->next_sibling_clone)
190 node = node->next_sibling_clone;
191 else
193 while (node != orig && !node->next_sibling_clone)
194 node = node->clone_of;
195 if (node != orig)
196 node = node->next_sibling_clone;
199 return false;
202 /* Read the function body for the function associated with NODE. */
204 static void
205 lto_materialize_function (struct cgraph_node *node)
207 tree decl;
209 decl = node->decl;
210 /* Read in functions with body (analyzed nodes)
211 and also functions that are needed to produce virtual clones. */
212 if ((node->has_gimple_body_p () && node->analyzed)
213 || node->used_as_abstract_origin
214 || has_analyzed_clone_p (node))
216 /* Clones don't need to be read. */
217 if (node->clone_of)
218 return;
219 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
220 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
223 /* Let the middle end know about the function. */
224 rest_of_decl_compilation (decl, 1, 0);
228 /* Decode the content of memory pointed to by DATA in the in decl
229 state object STATE. DATA_IN points to a data_in structure for
230 decoding. Return the address after the decoded object in the
231 input. */
233 static const uint32_t *
234 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
235 struct lto_in_decl_state *state)
237 uint32_t ix;
238 tree decl;
239 uint32_t i, j;
241 ix = *data++;
242 state->compressed = ix & 1;
243 ix /= 2;
244 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
245 if (!VAR_OR_FUNCTION_DECL_P (decl))
247 gcc_assert (decl == void_type_node);
248 decl = NULL_TREE;
250 state->fn_decl = decl;
252 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
254 uint32_t size = *data++;
255 vec<tree, va_gc> *decls = NULL;
256 vec_alloc (decls, size);
258 for (j = 0; j < size; j++)
259 vec_safe_push (decls,
260 streamer_tree_cache_get_tree (data_in->reader_cache,
261 data[j]));
263 state->streams[i] = decls;
264 data += size;
267 return data;
271 /* Global canonical type table. */
272 static htab_t gimple_canonical_types;
273 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
274 static unsigned long num_canonical_type_hash_entries;
275 static unsigned long num_canonical_type_hash_queries;
277 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
278 static hashval_t gimple_canonical_type_hash (const void *p);
279 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
281 /* Returning a hash value for gimple type TYPE.
283 The hash value returned is equal for types considered compatible
284 by gimple_canonical_types_compatible_p. */
286 static hashval_t
287 hash_canonical_type (tree type)
289 inchash::hash hstate;
290 enum tree_code code;
292 /* We compute alias sets only for types that needs them.
293 Be sure we do not recurse to something else as we can not hash incomplete
294 types in a way they would have same hash value as compatible complete
295 types. */
296 gcc_checking_assert (type_with_alias_set_p (type));
298 /* Combine a few common features of types so that types are grouped into
299 smaller sets; when searching for existing matching types to merge,
300 only existing types having the same features as the new type will be
301 checked. */
302 code = tree_code_for_canonical_type_merging (TREE_CODE (type));
303 hstate.add_int (code);
304 hstate.add_int (TYPE_MODE (type));
306 /* Incorporate common features of numerical types. */
307 if (INTEGRAL_TYPE_P (type)
308 || SCALAR_FLOAT_TYPE_P (type)
309 || FIXED_POINT_TYPE_P (type)
310 || TREE_CODE (type) == OFFSET_TYPE
311 || POINTER_TYPE_P (type))
313 hstate.add_int (TYPE_PRECISION (type));
314 if (!type_with_interoperable_signedness (type))
315 hstate.add_int (TYPE_UNSIGNED (type));
318 if (VECTOR_TYPE_P (type))
320 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
321 hstate.add_int (TYPE_UNSIGNED (type));
324 if (TREE_CODE (type) == COMPLEX_TYPE)
325 hstate.add_int (TYPE_UNSIGNED (type));
327 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
328 interoperable with "signed char". Unless all frontends are revisited to
329 agree on these types, we must ignore the flag completely. */
331 /* Fortran standard define C_PTR type that is compatible with every
332 C pointer. For this reason we need to glob all pointers into one.
333 Still pointers in different address spaces are not compatible. */
334 if (POINTER_TYPE_P (type))
335 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
337 /* For array types hash the domain bounds and the string flag. */
338 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
340 hstate.add_int (TYPE_STRING_FLAG (type));
341 /* OMP lowering can introduce error_mark_node in place of
342 random local decls in types. */
343 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
344 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
345 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
346 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
349 /* Recurse for aggregates with a single element type. */
350 if (TREE_CODE (type) == ARRAY_TYPE
351 || TREE_CODE (type) == COMPLEX_TYPE
352 || TREE_CODE (type) == VECTOR_TYPE)
353 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
355 /* Incorporate function return and argument types. */
356 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
358 unsigned na;
359 tree p;
361 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
363 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
365 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
366 na++;
369 hstate.add_int (na);
372 if (RECORD_OR_UNION_TYPE_P (type))
374 unsigned nf;
375 tree f;
377 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
378 if (TREE_CODE (f) == FIELD_DECL
379 && (! DECL_SIZE (f)
380 || ! integer_zerop (DECL_SIZE (f))))
382 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
383 nf++;
386 hstate.add_int (nf);
389 return hstate.end();
392 /* Returning a hash value for gimple type TYPE combined with VAL. */
394 static void
395 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
397 hashval_t v;
399 /* All type variants have same TYPE_CANONICAL. */
400 type = TYPE_MAIN_VARIANT (type);
402 if (!canonical_type_used_p (type))
403 v = hash_canonical_type (type);
404 /* An already processed type. */
405 else if (TYPE_CANONICAL (type))
407 type = TYPE_CANONICAL (type);
408 v = gimple_canonical_type_hash (type);
410 else
412 /* Canonical types should not be able to form SCCs by design, this
413 recursion is just because we do not register canonical types in
414 optimal order. To avoid quadratic behavior also register the
415 type here. */
416 v = hash_canonical_type (type);
417 gimple_register_canonical_type_1 (type, v);
419 hstate.add_int (v);
422 /* Returns the hash for a canonical type P. */
424 static hashval_t
425 gimple_canonical_type_hash (const void *p)
427 num_canonical_type_hash_queries++;
428 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
429 gcc_assert (slot != NULL);
430 return *slot;
435 /* Returns nonzero if P1 and P2 are equal. */
437 static int
438 gimple_canonical_type_eq (const void *p1, const void *p2)
440 const_tree t1 = (const_tree) p1;
441 const_tree t2 = (const_tree) p2;
442 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
443 CONST_CAST_TREE (t2));
446 /* Main worker for gimple_register_canonical_type. */
448 static void
449 gimple_register_canonical_type_1 (tree t, hashval_t hash)
451 void **slot;
453 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t)
454 && type_with_alias_set_p (t)
455 && canonical_type_used_p (t));
457 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
458 if (*slot)
460 tree new_type = (tree)(*slot);
461 gcc_checking_assert (new_type != t);
462 TYPE_CANONICAL (t) = new_type;
464 else
466 TYPE_CANONICAL (t) = t;
467 *slot = (void *) t;
468 /* Cache the just computed hash value. */
469 num_canonical_type_hash_entries++;
470 bool existed_p = canonical_type_hash_cache->put (t, hash);
471 gcc_assert (!existed_p);
475 /* Register type T in the global type table gimple_types and set
476 TYPE_CANONICAL of T accordingly.
477 This is used by LTO to merge structurally equivalent types for
478 type-based aliasing purposes across different TUs and languages.
480 ??? This merging does not exactly match how the tree.c middle-end
481 functions will assign TYPE_CANONICAL when new types are created
482 during optimization (which at least happens for pointer and array
483 types). */
485 static void
486 gimple_register_canonical_type (tree t)
488 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t)
489 || !canonical_type_used_p (t))
490 return;
492 /* Canonical types are same among all complete variants. */
493 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
494 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
495 else
497 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t),
498 hash_canonical_type (TYPE_MAIN_VARIANT (t)));
499 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
503 /* Re-compute TYPE_CANONICAL for NODE and related types. */
505 static void
506 lto_register_canonical_types (tree node, bool first_p)
508 if (!node
509 || !TYPE_P (node))
510 return;
512 if (first_p)
513 TYPE_CANONICAL (node) = NULL_TREE;
515 if (POINTER_TYPE_P (node)
516 || TREE_CODE (node) == COMPLEX_TYPE
517 || TREE_CODE (node) == ARRAY_TYPE)
518 lto_register_canonical_types (TREE_TYPE (node), first_p);
520 if (!first_p)
521 gimple_register_canonical_type (node);
525 /* Remember trees that contains references to declarations. */
526 static GTY(()) vec <tree, va_gc> *tree_with_vars;
528 #define CHECK_VAR(tt) \
529 do \
531 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
532 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
533 return true; \
534 } while (0)
536 #define CHECK_NO_VAR(tt) \
537 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
539 /* Check presence of pointers to decls in fields of a tree_typed T. */
541 static inline bool
542 mentions_vars_p_typed (tree t)
544 CHECK_NO_VAR (TREE_TYPE (t));
545 return false;
548 /* Check presence of pointers to decls in fields of a tree_common T. */
550 static inline bool
551 mentions_vars_p_common (tree t)
553 if (mentions_vars_p_typed (t))
554 return true;
555 CHECK_NO_VAR (TREE_CHAIN (t));
556 return false;
559 /* Check presence of pointers to decls in fields of a decl_minimal T. */
561 static inline bool
562 mentions_vars_p_decl_minimal (tree t)
564 if (mentions_vars_p_common (t))
565 return true;
566 CHECK_NO_VAR (DECL_NAME (t));
567 CHECK_VAR (DECL_CONTEXT (t));
568 return false;
571 /* Check presence of pointers to decls in fields of a decl_common T. */
573 static inline bool
574 mentions_vars_p_decl_common (tree t)
576 if (mentions_vars_p_decl_minimal (t))
577 return true;
578 CHECK_VAR (DECL_SIZE (t));
579 CHECK_VAR (DECL_SIZE_UNIT (t));
580 CHECK_VAR (DECL_INITIAL (t));
581 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
582 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
583 return false;
586 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
588 static inline bool
589 mentions_vars_p_decl_with_vis (tree t)
591 if (mentions_vars_p_decl_common (t))
592 return true;
594 /* Accessor macro has side-effects, use field-name here. */
595 CHECK_NO_VAR (DECL_ASSEMBLER_NAME_RAW (t));
596 return false;
599 /* Check presence of pointers to decls in fields of a decl_non_common T. */
601 static inline bool
602 mentions_vars_p_decl_non_common (tree t)
604 if (mentions_vars_p_decl_with_vis (t))
605 return true;
606 CHECK_NO_VAR (DECL_RESULT_FLD (t));
607 return false;
610 /* Check presence of pointers to decls in fields of a decl_non_common T. */
612 static bool
613 mentions_vars_p_function (tree t)
615 if (mentions_vars_p_decl_non_common (t))
616 return true;
617 CHECK_NO_VAR (DECL_ARGUMENTS (t));
618 CHECK_NO_VAR (DECL_VINDEX (t));
619 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
620 return false;
623 /* Check presence of pointers to decls in fields of a field_decl T. */
625 static bool
626 mentions_vars_p_field_decl (tree t)
628 if (mentions_vars_p_decl_common (t))
629 return true;
630 CHECK_VAR (DECL_FIELD_OFFSET (t));
631 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
632 CHECK_NO_VAR (DECL_QUALIFIER (t));
633 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
634 CHECK_NO_VAR (DECL_FCONTEXT (t));
635 return false;
638 /* Check presence of pointers to decls in fields of a type T. */
640 static bool
641 mentions_vars_p_type (tree t)
643 if (mentions_vars_p_common (t))
644 return true;
645 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
646 CHECK_VAR (TYPE_SIZE (t));
647 CHECK_VAR (TYPE_SIZE_UNIT (t));
648 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
649 CHECK_NO_VAR (TYPE_NAME (t));
651 CHECK_VAR (TYPE_MIN_VALUE_RAW (t));
652 CHECK_VAR (TYPE_MAX_VALUE_RAW (t));
654 /* Accessor is for derived node types only. */
655 CHECK_NO_VAR (TYPE_LANG_SLOT_1 (t));
657 CHECK_VAR (TYPE_CONTEXT (t));
658 CHECK_NO_VAR (TYPE_CANONICAL (t));
659 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
660 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
661 return false;
664 /* Check presence of pointers to decls in fields of a BINFO T. */
666 static bool
667 mentions_vars_p_binfo (tree t)
669 unsigned HOST_WIDE_INT i, n;
671 if (mentions_vars_p_common (t))
672 return true;
673 CHECK_VAR (BINFO_VTABLE (t));
674 CHECK_NO_VAR (BINFO_OFFSET (t));
675 CHECK_NO_VAR (BINFO_VIRTUALS (t));
676 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
677 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
678 for (i = 0; i < n; i++)
679 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
680 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
681 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
682 n = BINFO_N_BASE_BINFOS (t);
683 for (i = 0; i < n; i++)
684 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
685 return false;
688 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
690 static bool
691 mentions_vars_p_constructor (tree t)
693 unsigned HOST_WIDE_INT idx;
694 constructor_elt *ce;
696 if (mentions_vars_p_typed (t))
697 return true;
699 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
701 CHECK_NO_VAR (ce->index);
702 CHECK_VAR (ce->value);
704 return false;
707 /* Check presence of pointers to decls in fields of an expression tree T. */
709 static bool
710 mentions_vars_p_expr (tree t)
712 int i;
713 if (mentions_vars_p_typed (t))
714 return true;
715 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
716 CHECK_VAR (TREE_OPERAND (t, i));
717 return false;
720 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
722 static bool
723 mentions_vars_p_omp_clause (tree t)
725 int i;
726 if (mentions_vars_p_common (t))
727 return true;
728 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
729 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
730 return false;
733 /* Check presence of pointers to decls that needs later fixup in T. */
735 static bool
736 mentions_vars_p (tree t)
738 switch (TREE_CODE (t))
740 case IDENTIFIER_NODE:
741 break;
743 case TREE_LIST:
744 CHECK_VAR (TREE_VALUE (t));
745 CHECK_VAR (TREE_PURPOSE (t));
746 CHECK_NO_VAR (TREE_CHAIN (t));
747 break;
749 case FIELD_DECL:
750 return mentions_vars_p_field_decl (t);
752 case LABEL_DECL:
753 case CONST_DECL:
754 case PARM_DECL:
755 case RESULT_DECL:
756 case IMPORTED_DECL:
757 case NAMESPACE_DECL:
758 case NAMELIST_DECL:
759 return mentions_vars_p_decl_common (t);
761 case VAR_DECL:
762 return mentions_vars_p_decl_with_vis (t);
764 case TYPE_DECL:
765 return mentions_vars_p_decl_non_common (t);
767 case FUNCTION_DECL:
768 return mentions_vars_p_function (t);
770 case TREE_BINFO:
771 return mentions_vars_p_binfo (t);
773 case PLACEHOLDER_EXPR:
774 return mentions_vars_p_common (t);
776 case BLOCK:
777 case TRANSLATION_UNIT_DECL:
778 case OPTIMIZATION_NODE:
779 case TARGET_OPTION_NODE:
780 break;
782 case CONSTRUCTOR:
783 return mentions_vars_p_constructor (t);
785 case OMP_CLAUSE:
786 return mentions_vars_p_omp_clause (t);
788 default:
789 if (TYPE_P (t))
791 if (mentions_vars_p_type (t))
792 return true;
794 else if (EXPR_P (t))
796 if (mentions_vars_p_expr (t))
797 return true;
799 else if (CONSTANT_CLASS_P (t))
800 CHECK_NO_VAR (TREE_TYPE (t));
801 else
802 gcc_unreachable ();
804 return false;
808 /* Return the resolution for the decl with index INDEX from DATA_IN. */
810 static enum ld_plugin_symbol_resolution
811 get_resolution (struct data_in *data_in, unsigned index)
813 if (data_in->globals_resolution.exists ())
815 ld_plugin_symbol_resolution_t ret;
816 /* We can have references to not emitted functions in
817 DECL_FUNCTION_PERSONALITY at least. So we can and have
818 to indeed return LDPR_UNKNOWN in some cases. */
819 if (data_in->globals_resolution.length () <= index)
820 return LDPR_UNKNOWN;
821 ret = data_in->globals_resolution[index];
822 return ret;
824 else
825 /* Delay resolution finding until decl merging. */
826 return LDPR_UNKNOWN;
829 /* We need to record resolutions until symbol table is read. */
830 static void
831 register_resolution (struct lto_file_decl_data *file_data, tree decl,
832 enum ld_plugin_symbol_resolution resolution)
834 bool existed;
835 if (resolution == LDPR_UNKNOWN)
836 return;
837 if (!file_data->resolution_map)
838 file_data->resolution_map
839 = new hash_map<tree, ld_plugin_symbol_resolution>;
840 ld_plugin_symbol_resolution_t &res
841 = file_data->resolution_map->get_or_insert (decl, &existed);
842 gcc_assert (!existed || res == resolution);
843 if (!existed
844 || resolution == LDPR_PREVAILING_DEF_IRONLY
845 || resolution == LDPR_PREVAILING_DEF
846 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
847 res = resolution;
850 /* Register DECL with the global symbol table and change its
851 name if necessary to avoid name clashes for static globals across
852 different files. */
854 static void
855 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
856 unsigned ix)
858 tree context;
860 /* Variable has file scope, not local. */
861 if (!TREE_PUBLIC (decl)
862 && !((context = decl_function_context (decl))
863 && auto_var_in_fn_p (decl, context)))
864 rest_of_decl_compilation (decl, 1, 0);
866 /* If this variable has already been declared, queue the
867 declaration for merging. */
868 if (TREE_PUBLIC (decl))
869 register_resolution (data_in->file_data,
870 decl, get_resolution (data_in, ix));
874 /* Register DECL with the global symbol table and change its
875 name if necessary to avoid name clashes for static globals across
876 different files. DATA_IN contains descriptors and tables for the
877 file being read. */
879 static void
880 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
881 unsigned ix)
883 /* If this variable has already been declared, queue the
884 declaration for merging. */
885 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
886 register_resolution (data_in->file_data,
887 decl, get_resolution (data_in, ix));
890 /* Check if T is a decl and needs register its resolution info. */
892 static void
893 lto_maybe_register_decl (struct data_in *data_in, tree t, unsigned ix)
895 if (TREE_CODE (t) == VAR_DECL)
896 lto_register_var_decl_in_symtab (data_in, t, ix);
897 else if (TREE_CODE (t) == FUNCTION_DECL
898 && !DECL_BUILT_IN (t))
899 lto_register_function_decl_in_symtab (data_in, t, ix);
903 /* For the type T re-materialize it in the type variant list and
904 the pointer/reference-to chains. */
906 static void
907 lto_fixup_prevailing_type (tree t)
909 /* The following re-creates proper variant lists while fixing up
910 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
911 variant list state before fixup is broken. */
913 /* If we are not our own variant leader link us into our new leaders
914 variant list. */
915 if (TYPE_MAIN_VARIANT (t) != t)
917 tree mv = TYPE_MAIN_VARIANT (t);
918 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
919 TYPE_NEXT_VARIANT (mv) = t;
922 /* The following reconstructs the pointer chains
923 of the new pointed-to type if we are a main variant. We do
924 not stream those so they are broken before fixup. */
925 if (TREE_CODE (t) == POINTER_TYPE
926 && TYPE_MAIN_VARIANT (t) == t)
928 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
929 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
931 else if (TREE_CODE (t) == REFERENCE_TYPE
932 && TYPE_MAIN_VARIANT (t) == t)
934 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
935 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
940 /* We keep prevailing tree SCCs in a hashtable with manual collision
941 handling (in case all hashes compare the same) and keep the colliding
942 entries in the tree_scc->next chain. */
944 struct tree_scc
946 tree_scc *next;
947 /* Hash of the whole SCC. */
948 hashval_t hash;
949 /* Number of trees in the SCC. */
950 unsigned len;
951 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
952 which share the same individual tree hash). */
953 unsigned entry_len;
954 /* The members of the SCC.
955 We only need to remember the first entry node candidate for prevailing
956 SCCs (but of course have access to all entries for SCCs we are
957 processing).
958 ??? For prevailing SCCs we really only need hash and the first
959 entry candidate, but that's too awkward to implement. */
960 tree entries[1];
963 struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
965 static inline hashval_t hash (const tree_scc *);
966 static inline bool equal (const tree_scc *, const tree_scc *);
969 hashval_t
970 tree_scc_hasher::hash (const tree_scc *scc)
972 return scc->hash;
975 bool
976 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
978 if (scc1->hash != scc2->hash
979 || scc1->len != scc2->len
980 || scc1->entry_len != scc2->entry_len)
981 return false;
982 return true;
985 static hash_table<tree_scc_hasher> *tree_scc_hash;
986 static struct obstack tree_scc_hash_obstack;
988 static unsigned long num_merged_types;
989 static unsigned long num_prevailing_types;
990 static unsigned long num_type_scc_trees;
991 static unsigned long total_scc_size;
992 static unsigned long num_sccs_read;
993 static unsigned long total_scc_size_merged;
994 static unsigned long num_sccs_merged;
995 static unsigned long num_scc_compares;
996 static unsigned long num_scc_compare_collisions;
999 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1000 recursing through in-SCC tree edges. Returns true if the SCCs entered
1001 through T1 and T2 are equal and fills in *MAP with the pairs of
1002 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1004 static bool
1005 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1007 enum tree_code code;
1009 /* Mark already visited nodes. */
1010 TREE_ASM_WRITTEN (t2) = 1;
1012 /* Push the pair onto map. */
1013 (*map)[0] = t1;
1014 (*map)[1] = t2;
1015 *map = *map + 2;
1017 /* Compare value-fields. */
1018 #define compare_values(X) \
1019 do { \
1020 if (X(t1) != X(t2)) \
1021 return false; \
1022 } while (0)
1024 compare_values (TREE_CODE);
1025 code = TREE_CODE (t1);
1027 if (!TYPE_P (t1))
1029 compare_values (TREE_SIDE_EFFECTS);
1030 compare_values (TREE_CONSTANT);
1031 compare_values (TREE_READONLY);
1032 compare_values (TREE_PUBLIC);
1034 compare_values (TREE_ADDRESSABLE);
1035 compare_values (TREE_THIS_VOLATILE);
1036 if (DECL_P (t1))
1037 compare_values (DECL_UNSIGNED);
1038 else if (TYPE_P (t1))
1039 compare_values (TYPE_UNSIGNED);
1040 if (TYPE_P (t1))
1041 compare_values (TYPE_ARTIFICIAL);
1042 else
1043 compare_values (TREE_NO_WARNING);
1044 compare_values (TREE_NOTHROW);
1045 compare_values (TREE_STATIC);
1046 if (code != TREE_BINFO)
1047 compare_values (TREE_PRIVATE);
1048 compare_values (TREE_PROTECTED);
1049 compare_values (TREE_DEPRECATED);
1050 if (TYPE_P (t1))
1052 if (AGGREGATE_TYPE_P (t1))
1053 compare_values (TYPE_REVERSE_STORAGE_ORDER);
1054 else
1055 compare_values (TYPE_SATURATING);
1056 compare_values (TYPE_ADDR_SPACE);
1058 else if (code == SSA_NAME)
1059 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1061 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1063 if (wi::to_wide (t1) != wi::to_wide (t2))
1064 return false;
1067 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1069 /* ??? No suitable compare routine available. */
1070 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1071 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1072 if (r1.cl != r2.cl
1073 || r1.decimal != r2.decimal
1074 || r1.sign != r2.sign
1075 || r1.signalling != r2.signalling
1076 || r1.canonical != r2.canonical
1077 || r1.uexp != r2.uexp)
1078 return false;
1079 for (unsigned i = 0; i < SIGSZ; ++i)
1080 if (r1.sig[i] != r2.sig[i])
1081 return false;
1084 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1085 if (!fixed_compare (EQ_EXPR,
1086 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1087 return false;
1089 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1091 compare_values (VECTOR_CST_LOG2_NPATTERNS);
1092 compare_values (VECTOR_CST_NELTS_PER_PATTERN);
1095 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1097 compare_values (DECL_MODE);
1098 compare_values (DECL_NONLOCAL);
1099 compare_values (DECL_VIRTUAL_P);
1100 compare_values (DECL_IGNORED_P);
1101 compare_values (DECL_ABSTRACT_P);
1102 compare_values (DECL_ARTIFICIAL);
1103 compare_values (DECL_USER_ALIGN);
1104 compare_values (DECL_PRESERVE_P);
1105 compare_values (DECL_EXTERNAL);
1106 compare_values (DECL_GIMPLE_REG_P);
1107 compare_values (DECL_ALIGN);
1108 if (code == LABEL_DECL)
1110 compare_values (EH_LANDING_PAD_NR);
1111 compare_values (LABEL_DECL_UID);
1113 else if (code == FIELD_DECL)
1115 compare_values (DECL_PACKED);
1116 compare_values (DECL_NONADDRESSABLE_P);
1117 compare_values (DECL_PADDING_P);
1118 compare_values (DECL_OFFSET_ALIGN);
1120 else if (code == VAR_DECL)
1122 compare_values (DECL_HAS_DEBUG_EXPR_P);
1123 compare_values (DECL_NONLOCAL_FRAME);
1125 if (code == RESULT_DECL
1126 || code == PARM_DECL
1127 || code == VAR_DECL)
1129 compare_values (DECL_BY_REFERENCE);
1130 if (code == VAR_DECL
1131 || code == PARM_DECL)
1132 compare_values (DECL_HAS_VALUE_EXPR_P);
1136 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1137 compare_values (DECL_REGISTER);
1139 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1141 compare_values (DECL_COMMON);
1142 compare_values (DECL_DLLIMPORT_P);
1143 compare_values (DECL_WEAK);
1144 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1145 compare_values (DECL_COMDAT);
1146 compare_values (DECL_VISIBILITY);
1147 compare_values (DECL_VISIBILITY_SPECIFIED);
1148 if (code == VAR_DECL)
1150 compare_values (DECL_HARD_REGISTER);
1151 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1152 compare_values (DECL_IN_CONSTANT_POOL);
1156 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1158 compare_values (DECL_BUILT_IN_CLASS);
1159 compare_values (DECL_STATIC_CONSTRUCTOR);
1160 compare_values (DECL_STATIC_DESTRUCTOR);
1161 compare_values (DECL_UNINLINABLE);
1162 compare_values (DECL_POSSIBLY_INLINED);
1163 compare_values (DECL_IS_NOVOPS);
1164 compare_values (DECL_IS_RETURNS_TWICE);
1165 compare_values (DECL_IS_MALLOC);
1166 compare_values (DECL_IS_OPERATOR_NEW);
1167 compare_values (DECL_DECLARED_INLINE_P);
1168 compare_values (DECL_STATIC_CHAIN);
1169 compare_values (DECL_NO_INLINE_WARNING_P);
1170 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1171 compare_values (DECL_NO_LIMIT_STACK);
1172 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1173 compare_values (DECL_PURE_P);
1174 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1175 compare_values (DECL_FINAL_P);
1176 compare_values (DECL_CXX_CONSTRUCTOR_P);
1177 compare_values (DECL_CXX_DESTRUCTOR_P);
1178 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1179 compare_values (DECL_FUNCTION_CODE);
1182 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1184 compare_values (TYPE_MODE);
1185 compare_values (TYPE_STRING_FLAG);
1186 compare_values (TYPE_NEEDS_CONSTRUCTING);
1187 if (RECORD_OR_UNION_TYPE_P (t1))
1189 compare_values (TYPE_TRANSPARENT_AGGR);
1190 compare_values (TYPE_FINAL_P);
1192 else if (code == ARRAY_TYPE)
1193 compare_values (TYPE_NONALIASED_COMPONENT);
1194 if (AGGREGATE_TYPE_P (t1))
1195 compare_values (TYPE_TYPELESS_STORAGE);
1196 compare_values (TYPE_EMPTY_P);
1197 compare_values (TYPE_PACKED);
1198 compare_values (TYPE_RESTRICT);
1199 compare_values (TYPE_USER_ALIGN);
1200 compare_values (TYPE_READONLY);
1201 compare_values (TYPE_PRECISION);
1202 compare_values (TYPE_ALIGN);
1203 /* Do not compare TYPE_ALIAS_SET. Doing so introduce ordering issues
1204 with calls to get_alias_set which may initialize it for streamed
1205 in types. */
1208 /* We don't want to compare locations, so there is nothing do compare
1209 for TS_EXP. */
1211 /* BLOCKs are function local and we don't merge anything there, so
1212 simply refuse to merge. */
1213 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1214 return false;
1216 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1217 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1218 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1219 return false;
1221 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1222 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1223 return false;
1225 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1226 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1227 sizeof (struct cl_optimization)) != 0)
1228 return false;
1230 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1231 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1232 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1233 return false;
1235 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1236 compare_values (CONSTRUCTOR_NELTS);
1238 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1239 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1240 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1241 IDENTIFIER_LENGTH (t1)) != 0)
1242 return false;
1244 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1245 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1246 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1247 TREE_STRING_LENGTH (t1)) != 0)
1248 return false;
1250 if (code == OMP_CLAUSE)
1252 compare_values (OMP_CLAUSE_CODE);
1253 switch (OMP_CLAUSE_CODE (t1))
1255 case OMP_CLAUSE_DEFAULT:
1256 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1257 break;
1258 case OMP_CLAUSE_SCHEDULE:
1259 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1260 break;
1261 case OMP_CLAUSE_DEPEND:
1262 compare_values (OMP_CLAUSE_DEPEND_KIND);
1263 break;
1264 case OMP_CLAUSE_MAP:
1265 compare_values (OMP_CLAUSE_MAP_KIND);
1266 break;
1267 case OMP_CLAUSE_PROC_BIND:
1268 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1269 break;
1270 case OMP_CLAUSE_REDUCTION:
1271 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1272 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1273 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1274 break;
1275 default:
1276 break;
1280 #undef compare_values
1283 /* Compare pointer fields. */
1285 /* Recurse. Search & Replaced from DFS_write_tree_body.
1286 Folding the early checks into the compare_tree_edges recursion
1287 macro makes debugging way quicker as you are able to break on
1288 compare_tree_sccs_1 and simply finish until a call returns false
1289 to spot the SCC members with the difference. */
1290 #define compare_tree_edges(E1, E2) \
1291 do { \
1292 tree t1_ = (E1), t2_ = (E2); \
1293 if (t1_ != t2_ \
1294 && (!t1_ || !t2_ \
1295 || !TREE_VISITED (t2_) \
1296 || (!TREE_ASM_WRITTEN (t2_) \
1297 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1298 return false; \
1299 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1300 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1301 } while (0)
1303 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1305 if (code != IDENTIFIER_NODE)
1306 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1309 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1311 /* Note that the number of elements for EXPR has already been emitted
1312 in EXPR's header (see streamer_write_tree_header). */
1313 unsigned int count = vector_cst_encoded_nelts (t1);
1314 for (unsigned int i = 0; i < count; ++i)
1315 compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
1316 VECTOR_CST_ENCODED_ELT (t2, i));
1319 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1321 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1322 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1325 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1327 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1328 /* ??? Global decls from different TUs have non-matching
1329 TRANSLATION_UNIT_DECLs. Only consider a small set of
1330 decls equivalent, we should not end up merging others. */
1331 if ((code == TYPE_DECL
1332 || code == NAMESPACE_DECL
1333 || code == IMPORTED_DECL
1334 || code == CONST_DECL
1335 || (VAR_OR_FUNCTION_DECL_P (t1)
1336 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1337 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1339 else
1340 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1343 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1345 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1346 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1347 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1348 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1), DECL_ABSTRACT_ORIGIN (t2));
1349 if ((code == VAR_DECL
1350 || code == PARM_DECL)
1351 && DECL_HAS_VALUE_EXPR_P (t1))
1352 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1353 if (code == VAR_DECL
1354 && DECL_HAS_DEBUG_EXPR_P (t1))
1355 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1356 /* LTO specific edges. */
1357 if (code != FUNCTION_DECL
1358 && code != TRANSLATION_UNIT_DECL)
1359 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1362 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1364 if (code == FUNCTION_DECL)
1366 tree a1, a2;
1367 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1368 a1 || a2;
1369 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1370 compare_tree_edges (a1, a2);
1371 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1373 else if (code == TYPE_DECL)
1374 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1377 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1379 /* Make sure we don't inadvertently set the assembler name. */
1380 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1381 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1382 DECL_ASSEMBLER_NAME (t2));
1385 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1387 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1388 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1389 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1390 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1391 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1392 DECL_FIELD_BIT_OFFSET (t2));
1393 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1396 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1398 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1399 DECL_FUNCTION_PERSONALITY (t2));
1400 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1401 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1402 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1403 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1404 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1407 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1409 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1410 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1411 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1412 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1413 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1414 reconstructed during fixup. */
1415 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1416 during fixup. */
1417 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1418 /* ??? Global types from different TUs have non-matching
1419 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1420 equal. */
1421 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1423 else
1424 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1425 /* TYPE_CANONICAL is re-computed during type merging, so do not
1426 compare it here. */
1427 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1430 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1432 if (code == ENUMERAL_TYPE)
1433 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1434 else if (code == ARRAY_TYPE)
1435 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1436 else if (RECORD_OR_UNION_TYPE_P (t1))
1438 tree f1, f2;
1439 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1440 f1 || f2;
1441 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1442 compare_tree_edges (f1, f2);
1444 else if (code == FUNCTION_TYPE
1445 || code == METHOD_TYPE)
1446 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1448 if (!POINTER_TYPE_P (t1))
1449 compare_tree_edges (TYPE_MIN_VALUE_RAW (t1), TYPE_MIN_VALUE_RAW (t2));
1450 compare_tree_edges (TYPE_MAX_VALUE_RAW (t1), TYPE_MAX_VALUE_RAW (t2));
1453 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1455 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1456 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1457 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1460 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1461 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1462 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1464 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1466 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1467 compare_tree_edges (TREE_OPERAND (t1, i),
1468 TREE_OPERAND (t2, i));
1470 /* BLOCKs are function local and we don't merge anything there. */
1471 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1472 return false;
1475 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1477 unsigned i;
1478 tree t;
1479 /* Lengths have already been compared above. */
1480 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1481 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1482 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1483 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1484 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1485 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1486 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1487 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1488 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1491 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1493 unsigned i;
1494 tree index, value;
1495 /* Lengths have already been compared above. */
1496 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1498 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1499 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1503 if (code == OMP_CLAUSE)
1505 int i;
1507 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1508 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1509 OMP_CLAUSE_OPERAND (t2, i));
1510 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1513 #undef compare_tree_edges
1515 return true;
1518 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1519 out MAP if they are equal. */
1521 static bool
1522 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1523 tree *map)
1525 /* Assume SCC entry hashes are sorted after their cardinality. Which
1526 means we can simply take the first n-tuple of equal hashes
1527 (which is recorded as entry_len) and do n SCC entry candidate
1528 comparisons. */
1529 for (unsigned i = 0; i < pscc->entry_len; ++i)
1531 tree *mapp = map;
1532 num_scc_compare_collisions++;
1533 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1535 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1536 on the scc as all trees will be freed. */
1537 return true;
1539 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1540 the SCC prevails. */
1541 for (unsigned j = 0; j < scc->len; ++j)
1542 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1545 return false;
1548 /* QSort sort function to sort a map of two pointers after the 2nd
1549 pointer. */
1551 static int
1552 cmp_tree (const void *p1_, const void *p2_)
1554 tree *p1 = (tree *)(const_cast<void *>(p1_));
1555 tree *p2 = (tree *)(const_cast<void *>(p2_));
1556 if (p1[1] == p2[1])
1557 return 0;
1558 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1561 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1562 hash value SCC_HASH with an already recorded SCC. Return true if
1563 that was successful, otherwise return false. */
1565 static bool
1566 unify_scc (struct data_in *data_in, unsigned from,
1567 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1569 bool unified_p = false;
1570 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1571 tree_scc *scc
1572 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1573 scc->next = NULL;
1574 scc->hash = scc_hash;
1575 scc->len = len;
1576 scc->entry_len = scc_entry_len;
1577 for (unsigned i = 0; i < len; ++i)
1579 tree t = streamer_tree_cache_get_tree (cache, from + i);
1580 scc->entries[i] = t;
1581 /* Do not merge SCCs with local entities inside them. Also do
1582 not merge TRANSLATION_UNIT_DECLs. */
1583 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1584 || (VAR_OR_FUNCTION_DECL_P (t)
1585 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1586 || TREE_CODE (t) == LABEL_DECL)
1588 /* Avoid doing any work for these cases and do not worry to
1589 record the SCCs for further merging. */
1590 return false;
1594 /* Look for the list of candidate SCCs to compare against. */
1595 tree_scc **slot;
1596 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1597 if (*slot)
1599 /* Try unifying against each candidate. */
1600 num_scc_compares++;
1602 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1603 outside of the scc when following tree edges. Make sure
1604 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1605 to track whether we visited the SCC member during the compare.
1606 We cannot use TREE_VISITED on the pscc members as the extended
1607 scc and pscc can overlap. */
1608 for (unsigned i = 0; i < scc->len; ++i)
1610 TREE_VISITED (scc->entries[i]) = 1;
1611 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1614 tree *map = XALLOCAVEC (tree, 2 * len);
1615 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1617 if (!compare_tree_sccs (pscc, scc, map))
1618 continue;
1620 /* Found an equal SCC. */
1621 unified_p = true;
1622 num_scc_compare_collisions--;
1623 num_sccs_merged++;
1624 total_scc_size_merged += len;
1626 if (flag_checking)
1627 for (unsigned i = 0; i < len; ++i)
1629 tree t = map[2*i+1];
1630 enum tree_code code = TREE_CODE (t);
1631 /* IDENTIFIER_NODEs should be singletons and are merged by the
1632 streamer. The others should be singletons, too, and we
1633 should not merge them in any way. */
1634 gcc_assert (code != TRANSLATION_UNIT_DECL
1635 && code != IDENTIFIER_NODE);
1638 /* Fixup the streamer cache with the prevailing nodes according
1639 to the tree node mapping computed by compare_tree_sccs. */
1640 if (len == 1)
1642 lto_maybe_register_decl (data_in, pscc->entries[0], from);
1643 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1645 else
1647 tree *map2 = XALLOCAVEC (tree, 2 * len);
1648 for (unsigned i = 0; i < len; ++i)
1650 map2[i*2] = (tree)(uintptr_t)(from + i);
1651 map2[i*2+1] = scc->entries[i];
1652 lto_maybe_register_decl (data_in, scc->entries[i], from + i);
1654 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1655 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1656 for (unsigned i = 0; i < len; ++i)
1657 streamer_tree_cache_replace_tree (cache, map[2*i],
1658 (uintptr_t)map2[2*i]);
1661 /* Free the tree nodes from the read SCC. */
1662 data_in->location_cache.revert_location_cache ();
1663 for (unsigned i = 0; i < len; ++i)
1665 if (TYPE_P (scc->entries[i]))
1666 num_merged_types++;
1667 free_node (scc->entries[i]);
1670 /* Drop DIE references. */
1671 dref_queue.truncate (0);
1673 break;
1676 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1677 if (!unified_p)
1678 for (unsigned i = 0; i < scc->len; ++i)
1679 TREE_VISITED (scc->entries[i]) = 0;
1682 /* If we didn't unify it to any candidate duplicate the relevant
1683 pieces to permanent storage and link it into the chain. */
1684 if (!unified_p)
1686 tree_scc *pscc
1687 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1688 memcpy (pscc, scc, sizeof (tree_scc));
1689 pscc->next = (*slot);
1690 *slot = pscc;
1692 return unified_p;
1696 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1697 RESOLUTIONS is the set of symbols picked by the linker (read from the
1698 resolution file when the linker plugin is being used). */
1700 static void
1701 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1702 vec<ld_plugin_symbol_resolution_t> resolutions)
1704 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1705 const int decl_offset = sizeof (struct lto_decl_header);
1706 const int main_offset = decl_offset + header->decl_state_size;
1707 const int string_offset = main_offset + header->main_size;
1708 struct data_in *data_in;
1709 unsigned int i;
1710 const uint32_t *data_ptr, *data_end;
1711 uint32_t num_decl_states;
1713 lto_input_block ib_main ((const char *) data + main_offset,
1714 header->main_size, decl_data->mode_table);
1716 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1717 header->string_size, resolutions);
1719 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1720 internal types that should not be merged. */
1722 /* Read the global declarations and types. */
1723 while (ib_main.p < ib_main.len)
1725 tree t;
1726 unsigned from = data_in->reader_cache->nodes.length ();
1727 /* Read and uniquify SCCs as in the input stream. */
1728 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1729 if (tag == LTO_tree_scc)
1731 unsigned len_;
1732 unsigned scc_entry_len;
1733 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1734 &scc_entry_len);
1735 unsigned len = data_in->reader_cache->nodes.length () - from;
1736 gcc_assert (len == len_);
1738 total_scc_size += len;
1739 num_sccs_read++;
1741 /* We have the special case of size-1 SCCs that are pre-merged
1742 by means of identifier and string sharing for example.
1743 ??? Maybe we should avoid streaming those as SCCs. */
1744 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1745 from);
1746 if (len == 1
1747 && (TREE_CODE (first) == IDENTIFIER_NODE
1748 || TREE_CODE (first) == INTEGER_CST))
1749 continue;
1751 /* Try to unify the SCC with already existing ones. */
1752 if (!flag_ltrans
1753 && unify_scc (data_in, from,
1754 len, scc_entry_len, scc_hash))
1755 continue;
1757 /* Tree merging failed, mark entries in location cache as
1758 permanent. */
1759 data_in->location_cache.accept_location_cache ();
1761 bool seen_type = false;
1762 for (unsigned i = 0; i < len; ++i)
1764 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1765 from + i);
1766 /* Reconstruct the type variant and pointer-to/reference-to
1767 chains. */
1768 if (TYPE_P (t))
1770 seen_type = true;
1771 num_prevailing_types++;
1772 lto_fixup_prevailing_type (t);
1774 /* Compute the canonical type of all types.
1775 ??? Should be able to assert that !TYPE_CANONICAL. */
1776 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1778 gimple_register_canonical_type (t);
1779 if (odr_type_p (t))
1780 register_odr_type (t);
1782 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1783 type which is also member of this SCC. */
1784 if (TREE_CODE (t) == INTEGER_CST
1785 && !TREE_OVERFLOW (t))
1786 cache_integer_cst (t);
1787 if (!flag_ltrans)
1789 lto_maybe_register_decl (data_in, t, from + i);
1790 /* Scan the tree for references to global functions or
1791 variables and record those for later fixup. */
1792 if (mentions_vars_p (t))
1793 vec_safe_push (tree_with_vars, t);
1797 /* Register DECLs with the debuginfo machinery. */
1798 while (!dref_queue.is_empty ())
1800 dref_entry e = dref_queue.pop ();
1801 debug_hooks->register_external_die (e.decl, e.sym, e.off);
1804 if (seen_type)
1805 num_type_scc_trees += len;
1807 else
1809 /* Pickle stray references. */
1810 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1811 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1814 data_in->location_cache.apply_location_cache ();
1816 /* Read in lto_in_decl_state objects. */
1817 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1818 data_end =
1819 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1820 num_decl_states = *data_ptr++;
1822 gcc_assert (num_decl_states > 0);
1823 decl_data->global_decl_state = lto_new_in_decl_state ();
1824 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1825 decl_data->global_decl_state);
1827 /* Read in per-function decl states and enter them in hash table. */
1828 decl_data->function_decl_states =
1829 hash_table<decl_state_hasher>::create_ggc (37);
1831 for (i = 1; i < num_decl_states; i++)
1833 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1835 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1836 lto_in_decl_state **slot
1837 = decl_data->function_decl_states->find_slot (state, INSERT);
1838 gcc_assert (*slot == NULL);
1839 *slot = state;
1842 if (data_ptr != data_end)
1843 internal_error ("bytecode stream: garbage at the end of symbols section");
1845 /* Set the current decl state to be the global state. */
1846 decl_data->current_decl_state = decl_data->global_decl_state;
1848 lto_data_in_delete (data_in);
1851 /* Custom version of strtoll, which is not portable. */
1853 static int64_t
1854 lto_parse_hex (const char *p)
1856 int64_t ret = 0;
1858 for (; *p != '\0'; ++p)
1860 char c = *p;
1861 unsigned char part;
1862 ret <<= 4;
1863 if (c >= '0' && c <= '9')
1864 part = c - '0';
1865 else if (c >= 'a' && c <= 'f')
1866 part = c - 'a' + 10;
1867 else if (c >= 'A' && c <= 'F')
1868 part = c - 'A' + 10;
1869 else
1870 internal_error ("could not parse hex number");
1871 ret |= part;
1874 return ret;
1877 /* Read resolution for file named FILE_NAME. The resolution is read from
1878 RESOLUTION. */
1880 static void
1881 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1883 /* We require that objects in the resolution file are in the same
1884 order as the lto1 command line. */
1885 unsigned int name_len;
1886 char *obj_name;
1887 unsigned int num_symbols;
1888 unsigned int i;
1889 struct lto_file_decl_data *file_data;
1890 splay_tree_node nd = NULL;
1892 if (!resolution)
1893 return;
1895 name_len = strlen (file->filename);
1896 obj_name = XNEWVEC (char, name_len + 1);
1897 fscanf (resolution, " "); /* Read white space. */
1899 fread (obj_name, sizeof (char), name_len, resolution);
1900 obj_name[name_len] = '\0';
1901 if (filename_cmp (obj_name, file->filename) != 0)
1902 internal_error ("unexpected file name %s in linker resolution file. "
1903 "Expected %s", obj_name, file->filename);
1904 if (file->offset != 0)
1906 int t;
1907 char offset_p[17];
1908 int64_t offset;
1909 t = fscanf (resolution, "@0x%16s", offset_p);
1910 if (t != 1)
1911 internal_error ("could not parse file offset");
1912 offset = lto_parse_hex (offset_p);
1913 if (offset != file->offset)
1914 internal_error ("unexpected offset");
1917 free (obj_name);
1919 fscanf (resolution, "%u", &num_symbols);
1921 for (i = 0; i < num_symbols; i++)
1923 int t;
1924 unsigned index;
1925 unsigned HOST_WIDE_INT id;
1926 char r_str[27];
1927 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1928 unsigned int j;
1929 unsigned int lto_resolution_str_len =
1930 sizeof (lto_resolution_str) / sizeof (char *);
1931 res_pair rp;
1933 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1934 &index, &id, r_str);
1935 if (t != 3)
1936 internal_error ("invalid line in the resolution file");
1938 for (j = 0; j < lto_resolution_str_len; j++)
1940 if (strcmp (lto_resolution_str[j], r_str) == 0)
1942 r = (enum ld_plugin_symbol_resolution) j;
1943 break;
1946 if (j == lto_resolution_str_len)
1947 internal_error ("invalid resolution in the resolution file");
1949 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1951 nd = lto_splay_tree_lookup (file_ids, id);
1952 if (nd == NULL)
1953 internal_error ("resolution sub id %wx not in object file", id);
1956 file_data = (struct lto_file_decl_data *)nd->value;
1957 /* The indexes are very sparse. To save memory save them in a compact
1958 format that is only unpacked later when the subfile is processed. */
1959 rp.res = r;
1960 rp.index = index;
1961 file_data->respairs.safe_push (rp);
1962 if (file_data->max_index < index)
1963 file_data->max_index = index;
1967 /* List of file_decl_datas */
1968 struct file_data_list
1970 struct lto_file_decl_data *first, *last;
1973 /* Is the name for a id'ed LTO section? */
1975 static int
1976 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1978 const char *s;
1980 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
1981 return 0;
1982 s = strrchr (name, '.');
1983 if (!s)
1984 return 0;
1985 /* If the section is not suffixed with an ID return. */
1986 if ((size_t)(s - name) == strlen (section_name_prefix))
1987 return 0;
1988 return sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1991 /* Create file_data of each sub file id */
1993 static int
1994 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1995 struct file_data_list *list)
1997 struct lto_section_slot s_slot, *new_slot;
1998 unsigned HOST_WIDE_INT id;
1999 splay_tree_node nd;
2000 void **hash_slot;
2001 char *new_name;
2002 struct lto_file_decl_data *file_data;
2004 if (!lto_section_with_id (ls->name, &id))
2005 return 1;
2007 /* Find hash table of sub module id */
2008 nd = lto_splay_tree_lookup (file_ids, id);
2009 if (nd != NULL)
2011 file_data = (struct lto_file_decl_data *)nd->value;
2013 else
2015 file_data = ggc_alloc<lto_file_decl_data> ();
2016 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2017 file_data->id = id;
2018 file_data->section_hash_table = lto_obj_create_section_hash_table ();
2019 lto_splay_tree_insert (file_ids, id, file_data);
2021 /* Maintain list in linker order */
2022 if (!list->first)
2023 list->first = file_data;
2024 if (list->last)
2025 list->last->next = file_data;
2026 list->last = file_data;
2029 /* Copy section into sub module hash table */
2030 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2031 s_slot.name = new_name;
2032 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2033 gcc_assert (*hash_slot == NULL);
2035 new_slot = XDUP (struct lto_section_slot, ls);
2036 new_slot->name = new_name;
2037 *hash_slot = new_slot;
2038 return 1;
2041 /* Read declarations and other initializations for a FILE_DATA. */
2043 static void
2044 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2046 const char *data;
2047 size_t len;
2048 vec<ld_plugin_symbol_resolution_t>
2049 resolutions = vNULL;
2050 int i;
2051 res_pair *rp;
2053 /* Create vector for fast access of resolution. We do this lazily
2054 to save memory. */
2055 resolutions.safe_grow_cleared (file_data->max_index + 1);
2056 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2057 resolutions[rp->index] = rp->res;
2058 file_data->respairs.release ();
2060 file_data->renaming_hash_table = lto_create_renaming_table ();
2061 file_data->file_name = file->filename;
2062 #ifdef ACCEL_COMPILER
2063 lto_input_mode_table (file_data);
2064 #else
2065 file_data->mode_table = lto_mode_identity_table;
2066 #endif
2067 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2068 if (data == NULL)
2070 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2071 return;
2073 /* Frees resolutions */
2074 lto_read_decls (file_data, data, resolutions);
2075 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2078 /* Finalize FILE_DATA in FILE and increase COUNT. */
2080 static int
2081 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2082 int *count)
2084 lto_file_finalize (file_data, file);
2085 if (symtab->dump_file)
2086 fprintf (symtab->dump_file,
2087 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2088 file_data->file_name, file_data->id);
2089 (*count)++;
2090 return 0;
2093 /* Generate a TREE representation for all types and external decls
2094 entities in FILE.
2096 Read all of the globals out of the file. Then read the cgraph
2097 and process the .o index into the cgraph nodes so that it can open
2098 the .o file to load the functions and ipa information. */
2100 static struct lto_file_decl_data *
2101 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2103 struct lto_file_decl_data *file_data = NULL;
2104 splay_tree file_ids;
2105 htab_t section_hash_table;
2106 struct lto_section_slot *section;
2107 struct file_data_list file_list;
2108 struct lto_section_list section_list;
2110 memset (&section_list, 0, sizeof (struct lto_section_list));
2111 section_hash_table = lto_obj_build_section_table (file, &section_list);
2113 /* Find all sub modules in the object and put their sections into new hash
2114 tables in a splay tree. */
2115 file_ids = lto_splay_tree_new ();
2116 memset (&file_list, 0, sizeof (struct file_data_list));
2117 for (section = section_list.first; section != NULL; section = section->next)
2118 create_subid_section_table (section, file_ids, &file_list);
2120 /* Add resolutions to file ids */
2121 lto_resolution_read (file_ids, resolution_file, file);
2123 /* Finalize each lto file for each submodule in the merged object */
2124 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2125 lto_create_files_from_ids (file, file_data, count);
2127 splay_tree_delete (file_ids);
2128 htab_delete (section_hash_table);
2130 return file_list.first;
2133 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2134 #define LTO_MMAP_IO 1
2135 #endif
2137 #if LTO_MMAP_IO
2138 /* Page size of machine is used for mmap and munmap calls. */
2139 static size_t page_mask;
2140 #endif
2142 /* Get the section data of length LEN from FILENAME starting at
2143 OFFSET. The data segment must be freed by the caller when the
2144 caller is finished. Returns NULL if all was not well. */
2146 static char *
2147 lto_read_section_data (struct lto_file_decl_data *file_data,
2148 intptr_t offset, size_t len)
2150 char *result;
2151 static int fd = -1;
2152 static char *fd_name;
2153 #if LTO_MMAP_IO
2154 intptr_t computed_len;
2155 intptr_t computed_offset;
2156 intptr_t diff;
2157 #endif
2159 /* Keep a single-entry file-descriptor cache. The last file we
2160 touched will get closed at exit.
2161 ??? Eventually we want to add a more sophisticated larger cache
2162 or rather fix function body streaming to not stream them in
2163 practically random order. */
2164 if (fd != -1
2165 && filename_cmp (fd_name, file_data->file_name) != 0)
2167 free (fd_name);
2168 close (fd);
2169 fd = -1;
2171 if (fd == -1)
2173 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2174 if (fd == -1)
2176 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2177 return NULL;
2179 fd_name = xstrdup (file_data->file_name);
2182 #if LTO_MMAP_IO
2183 if (!page_mask)
2185 size_t page_size = sysconf (_SC_PAGE_SIZE);
2186 page_mask = ~(page_size - 1);
2189 computed_offset = offset & page_mask;
2190 diff = offset - computed_offset;
2191 computed_len = len + diff;
2193 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2194 fd, computed_offset);
2195 if (result == MAP_FAILED)
2197 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2198 return NULL;
2201 return result + diff;
2202 #else
2203 result = (char *) xmalloc (len);
2204 if (lseek (fd, offset, SEEK_SET) != offset
2205 || read (fd, result, len) != (ssize_t) len)
2207 free (result);
2208 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2209 result = NULL;
2211 #ifdef __MINGW32__
2212 /* Native windows doesn't supports delayed unlink on opened file. So
2213 we close file here again. This produces higher I/O load, but at least
2214 it prevents to have dangling file handles preventing unlink. */
2215 free (fd_name);
2216 fd_name = NULL;
2217 close (fd);
2218 fd = -1;
2219 #endif
2220 return result;
2221 #endif
2225 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2226 NAME will be NULL unless the section type is for a function
2227 body. */
2229 static const char *
2230 get_section_data (struct lto_file_decl_data *file_data,
2231 enum lto_section_type section_type,
2232 const char *name,
2233 size_t *len)
2235 htab_t section_hash_table = file_data->section_hash_table;
2236 struct lto_section_slot *f_slot;
2237 struct lto_section_slot s_slot;
2238 const char *section_name = lto_get_section_name (section_type, name, file_data);
2239 char *data = NULL;
2241 *len = 0;
2242 s_slot.name = section_name;
2243 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2244 if (f_slot)
2246 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2247 *len = f_slot->len;
2250 free (CONST_CAST (char *, section_name));
2251 return data;
2255 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2256 starts at OFFSET and has LEN bytes. */
2258 static void
2259 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2260 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2261 const char *name ATTRIBUTE_UNUSED,
2262 const char *offset, size_t len ATTRIBUTE_UNUSED)
2264 #if LTO_MMAP_IO
2265 intptr_t computed_len;
2266 intptr_t computed_offset;
2267 intptr_t diff;
2268 #endif
2270 #if LTO_MMAP_IO
2271 computed_offset = ((intptr_t) offset) & page_mask;
2272 diff = (intptr_t) offset - computed_offset;
2273 computed_len = len + diff;
2275 munmap ((caddr_t) computed_offset, computed_len);
2276 #else
2277 free (CONST_CAST(char *, offset));
2278 #endif
2281 static lto_file *current_lto_file;
2283 /* Helper for qsort; compare partitions and return one with smaller size.
2284 We sort from greatest to smallest so parallel build doesn't stale on the
2285 longest compilation being executed too late. */
2287 static int
2288 cmp_partitions_size (const void *a, const void *b)
2290 const struct ltrans_partition_def *pa
2291 = *(struct ltrans_partition_def *const *)a;
2292 const struct ltrans_partition_def *pb
2293 = *(struct ltrans_partition_def *const *)b;
2294 return pb->insns - pa->insns;
2297 /* Helper for qsort; compare partitions and return one with smaller order. */
2299 static int
2300 cmp_partitions_order (const void *a, const void *b)
2302 const struct ltrans_partition_def *pa
2303 = *(struct ltrans_partition_def *const *)a;
2304 const struct ltrans_partition_def *pb
2305 = *(struct ltrans_partition_def *const *)b;
2306 int ordera = -1, orderb = -1;
2308 if (lto_symtab_encoder_size (pa->encoder))
2309 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2310 if (lto_symtab_encoder_size (pb->encoder))
2311 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2312 return orderb - ordera;
2315 /* Actually stream out ENCODER into TEMP_FILENAME. */
2317 static void
2318 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2320 lto_file *file = lto_obj_file_open (temp_filename, true);
2321 if (!file)
2322 fatal_error (input_location, "lto_obj_file_open() failed");
2323 lto_set_current_out_file (file);
2325 ipa_write_optimization_summaries (encoder);
2327 free (CONST_CAST (char *, file->filename));
2329 lto_set_current_out_file (NULL);
2330 lto_obj_file_close (file);
2331 free (file);
2334 /* Wait for forked process and signal errors. */
2335 #ifdef HAVE_WORKING_FORK
2336 static void
2337 wait_for_child ()
2339 int status;
2342 #ifndef WCONTINUED
2343 #define WCONTINUED 0
2344 #endif
2345 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2346 if (w == -1)
2347 fatal_error (input_location, "waitpid failed");
2349 if (WIFEXITED (status) && WEXITSTATUS (status))
2350 fatal_error (input_location, "streaming subprocess failed");
2351 else if (WIFSIGNALED (status))
2352 fatal_error (input_location,
2353 "streaming subprocess was killed by signal");
2355 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2357 #endif
2359 /* Stream out ENCODER into TEMP_FILENAME
2360 Fork if that seems to help. */
2362 static void
2363 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2364 bool ARG_UNUSED (last))
2366 #ifdef HAVE_WORKING_FORK
2367 static int nruns;
2369 if (lto_parallelism <= 1)
2371 do_stream_out (temp_filename, encoder);
2372 return;
2375 /* Do not run more than LTO_PARALLELISM streamings
2376 FIXME: we ignore limits on jobserver. */
2377 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2379 wait_for_child ();
2380 nruns --;
2382 /* If this is not the last parallel partition, execute new
2383 streaming process. */
2384 if (!last)
2386 pid_t cpid = fork ();
2388 if (!cpid)
2390 setproctitle ("lto1-wpa-streaming");
2391 do_stream_out (temp_filename, encoder);
2392 exit (0);
2394 /* Fork failed; lets do the job ourseleves. */
2395 else if (cpid == -1)
2396 do_stream_out (temp_filename, encoder);
2397 else
2398 nruns++;
2400 /* Last partition; stream it and wait for all children to die. */
2401 else
2403 int i;
2404 do_stream_out (temp_filename, encoder);
2405 for (i = 0; i < nruns; i++)
2406 wait_for_child ();
2408 asm_nodes_output = true;
2409 #else
2410 do_stream_out (temp_filename, encoder);
2411 #endif
2414 /* Write all output files in WPA mode and the file with the list of
2415 LTRANS units. */
2417 static void
2418 lto_wpa_write_files (void)
2420 unsigned i, n_sets;
2421 ltrans_partition part;
2422 FILE *ltrans_output_list_stream;
2423 char *temp_filename;
2424 vec <char *>temp_filenames = vNULL;
2425 size_t blen;
2427 /* Open the LTRANS output list. */
2428 if (!ltrans_output_list)
2429 fatal_error (input_location, "no LTRANS output list filename provided");
2431 timevar_push (TV_WHOPR_WPA);
2433 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2434 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2436 timevar_pop (TV_WHOPR_WPA);
2438 timevar_push (TV_WHOPR_WPA_IO);
2440 /* Generate a prefix for the LTRANS unit files. */
2441 blen = strlen (ltrans_output_list);
2442 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2443 strcpy (temp_filename, ltrans_output_list);
2444 if (blen > sizeof (".out")
2445 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2446 ".out") == 0)
2447 temp_filename[blen - sizeof (".out") + 1] = '\0';
2448 blen = strlen (temp_filename);
2450 n_sets = ltrans_partitions.length ();
2452 /* Sort partitions by size so small ones are compiled last.
2453 FIXME: Even when not reordering we may want to output one list for parallel make
2454 and other for final link command. */
2456 if (!flag_profile_reorder_functions || !flag_profile_use)
2457 ltrans_partitions.qsort (flag_toplevel_reorder
2458 ? cmp_partitions_size
2459 : cmp_partitions_order);
2461 for (i = 0; i < n_sets; i++)
2463 ltrans_partition part = ltrans_partitions[i];
2465 /* Write all the nodes in SET. */
2466 sprintf (temp_filename + blen, "%u.o", i);
2468 if (!quiet_flag)
2469 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2470 if (symtab->dump_file)
2472 lto_symtab_encoder_iterator lsei;
2474 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2475 part->name, temp_filename, part->insns);
2476 fprintf (symtab->dump_file, " Symbols in partition: ");
2477 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2478 lsei_next_in_partition (&lsei))
2480 symtab_node *node = lsei_node (lsei);
2481 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2483 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2484 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2485 lsei_next (&lsei))
2487 symtab_node *node = lsei_node (lsei);
2488 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2490 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2491 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2492 if (cnode
2493 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2494 fprintf (symtab->dump_file, "(body included)");
2495 else
2497 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2498 if (vnode
2499 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2500 fprintf (symtab->dump_file, "(initializer included)");
2504 fprintf (symtab->dump_file, "\n");
2506 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2508 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2510 part->encoder = NULL;
2512 temp_filenames.safe_push (xstrdup (temp_filename));
2514 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2515 if (ltrans_output_list_stream == NULL)
2516 fatal_error (input_location,
2517 "opening LTRANS output list %s: %m", ltrans_output_list);
2518 for (i = 0; i < n_sets; i++)
2520 unsigned int len = strlen (temp_filenames[i]);
2521 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2522 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2523 fatal_error (input_location, "writing to LTRANS output list %s: %m",
2524 ltrans_output_list);
2525 free (temp_filenames[i]);
2527 temp_filenames.release();
2529 lto_stats.num_output_files += n_sets;
2531 /* Close the LTRANS output list. */
2532 if (fclose (ltrans_output_list_stream))
2533 fatal_error (input_location,
2534 "closing LTRANS output list %s: %m", ltrans_output_list);
2536 free_ltrans_partitions();
2537 free (temp_filename);
2539 timevar_pop (TV_WHOPR_WPA_IO);
2543 /* If TT is a variable or function decl replace it with its
2544 prevailing variant. */
2545 #define LTO_SET_PREVAIL(tt) \
2546 do {\
2547 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2548 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2550 tt = lto_symtab_prevailing_decl (tt); \
2551 fixed = true; \
2553 } while (0)
2555 /* Ensure that TT isn't a replacable var of function decl. */
2556 #define LTO_NO_PREVAIL(tt) \
2557 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2559 /* Given a tree T replace all fields referring to variables or functions
2560 with their prevailing variant. */
2561 static void
2562 lto_fixup_prevailing_decls (tree t)
2564 enum tree_code code = TREE_CODE (t);
2565 bool fixed = false;
2567 gcc_checking_assert (code != TREE_BINFO);
2568 LTO_NO_PREVAIL (TREE_TYPE (t));
2569 if (CODE_CONTAINS_STRUCT (code, TS_COMMON)
2570 /* lto_symtab_prevail_decl use TREE_CHAIN to link to the prevailing decl.
2571 in the case T is a prevailed declaration we would ICE here. */
2572 && !VAR_OR_FUNCTION_DECL_P (t))
2573 LTO_NO_PREVAIL (TREE_CHAIN (t));
2574 if (DECL_P (t))
2576 LTO_NO_PREVAIL (DECL_NAME (t));
2577 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2578 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2580 LTO_SET_PREVAIL (DECL_SIZE (t));
2581 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2582 LTO_SET_PREVAIL (DECL_INITIAL (t));
2583 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2584 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2586 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2588 LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t));
2590 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2592 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2594 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2596 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2597 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2598 LTO_NO_PREVAIL (DECL_VINDEX (t));
2600 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2602 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2603 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2604 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2605 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2606 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2609 else if (TYPE_P (t))
2611 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2612 LTO_SET_PREVAIL (TYPE_SIZE (t));
2613 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2614 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2615 LTO_NO_PREVAIL (TYPE_NAME (t));
2617 LTO_SET_PREVAIL (TYPE_MIN_VALUE_RAW (t));
2618 LTO_SET_PREVAIL (TYPE_MAX_VALUE_RAW (t));
2619 LTO_NO_PREVAIL (TYPE_LANG_SLOT_1 (t));
2621 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2623 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2624 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2625 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2627 else if (EXPR_P (t))
2629 int i;
2630 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2631 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2633 else if (TREE_CODE (t) == CONSTRUCTOR)
2635 unsigned i;
2636 tree val;
2637 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2638 LTO_SET_PREVAIL (val);
2640 else
2642 switch (code)
2644 case TREE_LIST:
2645 LTO_SET_PREVAIL (TREE_VALUE (t));
2646 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2647 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2648 break;
2649 default:
2650 gcc_unreachable ();
2653 /* If we fixed nothing, then we missed something seen by
2654 mentions_vars_p. */
2655 gcc_checking_assert (fixed);
2657 #undef LTO_SET_PREVAIL
2658 #undef LTO_NO_PREVAIL
2660 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2661 replaces var and function decls with the corresponding prevailing def. */
2663 static void
2664 lto_fixup_state (struct lto_in_decl_state *state)
2666 unsigned i, si;
2668 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2669 we still need to walk from all DECLs to find the reachable
2670 FUNCTION_DECLs and VAR_DECLs. */
2671 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2673 vec<tree, va_gc> *trees = state->streams[si];
2674 for (i = 0; i < vec_safe_length (trees); i++)
2676 tree t = (*trees)[i];
2677 if (flag_checking && TYPE_P (t))
2678 verify_type (t);
2679 if (VAR_OR_FUNCTION_DECL_P (t)
2680 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2681 (*trees)[i] = lto_symtab_prevailing_decl (t);
2686 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2687 prevailing one. */
2689 static void
2690 lto_fixup_decls (struct lto_file_decl_data **files)
2692 unsigned int i;
2693 tree t;
2695 if (tree_with_vars)
2696 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2697 lto_fixup_prevailing_decls (t);
2699 for (i = 0; files[i]; i++)
2701 struct lto_file_decl_data *file = files[i];
2702 struct lto_in_decl_state *state = file->global_decl_state;
2703 lto_fixup_state (state);
2705 hash_table<decl_state_hasher>::iterator iter;
2706 lto_in_decl_state *elt;
2707 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2708 lto_in_decl_state *, iter)
2709 lto_fixup_state (elt);
2713 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2715 /* Turn file datas for sub files into a single array, so that they look
2716 like separate files for further passes. */
2718 static void
2719 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2721 struct lto_file_decl_data *n, *next;
2722 int i, k;
2724 lto_stats.num_input_files = count;
2725 all_file_decl_data
2726 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2727 /* Set the hooks so that all of the ipa passes can read in their data. */
2728 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2729 for (i = 0, k = 0; i < last_file_ix; i++)
2731 for (n = orig[i]; n != NULL; n = next)
2733 all_file_decl_data[k++] = n;
2734 next = n->next;
2735 n->next = NULL;
2738 all_file_decl_data[k] = NULL;
2739 gcc_assert (k == count);
2742 /* Input file data before flattening (i.e. splitting them to subfiles to support
2743 incremental linking. */
2744 static int real_file_count;
2745 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2747 static void print_lto_report_1 (void);
2749 /* Read all the symbols from the input files FNAMES. NFILES is the
2750 number of files requested in the command line. Instantiate a
2751 global call graph by aggregating all the sub-graphs found in each
2752 file. */
2754 static void
2755 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2757 unsigned int i, last_file_ix;
2758 FILE *resolution;
2759 int count = 0;
2760 struct lto_file_decl_data **decl_data;
2761 symtab_node *snode;
2763 symtab->initialize ();
2765 timevar_push (TV_IPA_LTO_DECL_IN);
2767 #ifdef ACCEL_COMPILER
2768 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2769 lto_stream_offload_p = true;
2770 #endif
2772 real_file_decl_data
2773 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2774 real_file_count = nfiles;
2776 /* Read the resolution file. */
2777 resolution = NULL;
2778 if (resolution_file_name)
2780 int t;
2781 unsigned num_objects;
2783 resolution = fopen (resolution_file_name, "r");
2784 if (resolution == NULL)
2785 fatal_error (input_location,
2786 "could not open symbol resolution file: %m");
2788 t = fscanf (resolution, "%u", &num_objects);
2789 gcc_assert (t == 1);
2791 /* True, since the plugin splits the archives. */
2792 gcc_assert (num_objects == nfiles);
2794 symtab->state = LTO_STREAMING;
2796 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2797 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2798 gimple_canonical_type_eq, NULL);
2799 gcc_obstack_init (&tree_scc_hash_obstack);
2800 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2802 /* Register the common node types with the canonical type machinery so
2803 we properly share alias-sets across languages and TUs. Do not
2804 expose the common nodes as type merge target - those that should be
2805 are already exposed so by pre-loading the LTO streamer caches.
2806 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2807 for (i = 0; i < itk_none; ++i)
2808 lto_register_canonical_types (integer_types[i], true);
2809 for (i = 0; i < stk_type_kind_last; ++i)
2810 lto_register_canonical_types (sizetype_tab[i], true);
2811 for (i = 0; i < TI_MAX; ++i)
2812 lto_register_canonical_types (global_trees[i], true);
2813 for (i = 0; i < itk_none; ++i)
2814 lto_register_canonical_types (integer_types[i], false);
2815 for (i = 0; i < stk_type_kind_last; ++i)
2816 lto_register_canonical_types (sizetype_tab[i], false);
2817 for (i = 0; i < TI_MAX; ++i)
2818 lto_register_canonical_types (global_trees[i], false);
2820 if (!quiet_flag)
2821 fprintf (stderr, "Reading object files:");
2823 /* Read all of the object files specified on the command line. */
2824 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2826 struct lto_file_decl_data *file_data = NULL;
2827 if (!quiet_flag)
2829 fprintf (stderr, " %s", fnames[i]);
2830 fflush (stderr);
2833 current_lto_file = lto_obj_file_open (fnames[i], false);
2834 if (!current_lto_file)
2835 break;
2837 file_data = lto_file_read (current_lto_file, resolution, &count);
2838 if (!file_data)
2840 lto_obj_file_close (current_lto_file);
2841 free (current_lto_file);
2842 current_lto_file = NULL;
2843 break;
2846 decl_data[last_file_ix++] = file_data;
2848 lto_obj_file_close (current_lto_file);
2849 free (current_lto_file);
2850 current_lto_file = NULL;
2853 lto_flatten_files (decl_data, count, last_file_ix);
2854 lto_stats.num_input_files = count;
2855 ggc_free(decl_data);
2856 real_file_decl_data = NULL;
2858 if (resolution_file_name)
2859 fclose (resolution);
2861 /* Show the LTO report before launching LTRANS. */
2862 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2863 print_lto_report_1 ();
2865 /* Free gimple type merging datastructures. */
2866 delete tree_scc_hash;
2867 tree_scc_hash = NULL;
2868 obstack_free (&tree_scc_hash_obstack, NULL);
2869 htab_delete (gimple_canonical_types);
2870 gimple_canonical_types = NULL;
2871 delete canonical_type_hash_cache;
2872 canonical_type_hash_cache = NULL;
2874 /* At this stage we know that majority of GGC memory is reachable.
2875 Growing the limits prevents unnecesary invocation of GGC. */
2876 ggc_grow ();
2877 ggc_collect ();
2879 /* Set the hooks so that all of the ipa passes can read in their data. */
2880 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2882 timevar_pop (TV_IPA_LTO_DECL_IN);
2884 if (!quiet_flag)
2885 fprintf (stderr, "\nReading the callgraph\n");
2887 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2888 /* Read the symtab. */
2889 input_symtab ();
2891 input_offload_tables (!flag_ltrans);
2893 /* Store resolutions into the symbol table. */
2895 FOR_EACH_SYMBOL (snode)
2896 if (snode->externally_visible && snode->real_symbol_p ()
2897 && snode->lto_file_data && snode->lto_file_data->resolution_map
2898 && !is_builtin_fn (snode->decl)
2899 && !(VAR_P (snode->decl) && DECL_HARD_REGISTER (snode->decl)))
2901 ld_plugin_symbol_resolution_t *res;
2903 res = snode->lto_file_data->resolution_map->get (snode->decl);
2904 if (!res || *res == LDPR_UNKNOWN)
2905 fatal_error (input_location, "missing resolution data for %s",
2906 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (snode->decl)));
2907 else
2908 snode->resolution = *res;
2910 for (i = 0; all_file_decl_data[i]; i++)
2911 if (all_file_decl_data[i]->resolution_map)
2913 delete all_file_decl_data[i]->resolution_map;
2914 all_file_decl_data[i]->resolution_map = NULL;
2917 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2919 if (!quiet_flag)
2920 fprintf (stderr, "Merging declarations\n");
2922 timevar_push (TV_IPA_LTO_DECL_MERGE);
2923 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2924 need to care about resolving symbols again, we only need to replace
2925 duplicated declarations read from the callgraph and from function
2926 sections. */
2927 if (!flag_ltrans)
2929 lto_symtab_merge_decls ();
2931 /* If there were errors during symbol merging bail out, we have no
2932 good way to recover here. */
2933 if (seen_error ())
2934 fatal_error (input_location,
2935 "errors during merging of translation units");
2937 /* Fixup all decls. */
2938 lto_fixup_decls (all_file_decl_data);
2940 if (tree_with_vars)
2941 ggc_free (tree_with_vars);
2942 tree_with_vars = NULL;
2943 ggc_collect ();
2945 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2946 /* Each pass will set the appropriate timer. */
2948 if (!quiet_flag)
2949 fprintf (stderr, "Reading summaries\n");
2951 /* Read the IPA summary data. */
2952 if (flag_ltrans)
2953 ipa_read_optimization_summaries ();
2954 else
2955 ipa_read_summaries ();
2957 for (i = 0; all_file_decl_data[i]; i++)
2959 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2960 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2961 all_file_decl_data[i]->symtab_node_encoder = NULL;
2962 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
2963 all_file_decl_data[i]->global_decl_state = NULL;
2964 all_file_decl_data[i]->current_decl_state = NULL;
2967 /* Finally merge the cgraph according to the decl merging decisions. */
2968 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2969 if (symtab->dump_file)
2971 fprintf (symtab->dump_file, "Before merging:\n");
2972 symtab->dump (symtab->dump_file);
2974 if (!flag_ltrans)
2976 lto_symtab_merge_symbols ();
2977 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2978 we are still having duplicated comdat groups containing local statics.
2979 We could also just remove them while merging. */
2980 symtab->remove_unreachable_nodes (dump_file);
2982 ggc_collect ();
2983 symtab->state = IPA_SSA;
2984 /* FIXME: Technically all node removals happening here are useless, because
2985 WPA should not stream them. */
2986 if (flag_ltrans)
2987 symtab->remove_unreachable_nodes (dump_file);
2989 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2991 /* Indicate that the cgraph is built and ready. */
2992 symtab->function_flags_ready = true;
2994 ggc_free (all_file_decl_data);
2995 all_file_decl_data = NULL;
2999 /* Materialize all the bodies for all the nodes in the callgraph. */
3001 static void
3002 materialize_cgraph (void)
3004 struct cgraph_node *node;
3005 timevar_id_t lto_timer;
3007 if (!quiet_flag)
3008 fprintf (stderr,
3009 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
3012 FOR_EACH_FUNCTION (node)
3014 if (node->lto_file_data)
3016 lto_materialize_function (node);
3017 lto_stats.num_input_cgraph_nodes++;
3022 /* Start the appropriate timer depending on the mode that we are
3023 operating in. */
3024 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
3025 : (flag_ltrans) ? TV_WHOPR_LTRANS
3026 : TV_LTO;
3027 timevar_push (lto_timer);
3029 current_function_decl = NULL;
3030 set_cfun (NULL);
3032 if (!quiet_flag)
3033 fprintf (stderr, "\n");
3035 timevar_pop (lto_timer);
3039 /* Show various memory usage statistics related to LTO. */
3040 static void
3041 print_lto_report_1 (void)
3043 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3044 fprintf (stderr, "%s statistics\n", pfx);
3046 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3047 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3048 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3049 if (flag_wpa && tree_scc_hash)
3051 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3052 "collision ratio: %f\n", pfx,
3053 (long) tree_scc_hash->size (),
3054 (long) tree_scc_hash->elements (),
3055 tree_scc_hash->collisions ());
3056 hash_table<tree_scc_hasher>::iterator hiter;
3057 tree_scc *scc, *max_scc = NULL;
3058 unsigned max_length = 0;
3059 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3061 unsigned length = 0;
3062 tree_scc *s = scc;
3063 for (; s; s = s->next)
3064 length++;
3065 if (length > max_length)
3067 max_length = length;
3068 max_scc = scc;
3071 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3072 pfx, max_length, max_scc->len);
3073 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3074 num_scc_compares, num_scc_compare_collisions,
3075 num_scc_compare_collisions / (double) num_scc_compares);
3076 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3077 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3078 total_scc_size_merged);
3079 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3080 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3081 pfx, num_prevailing_types, num_type_scc_trees);
3082 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3083 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3084 (long) htab_size (gimple_canonical_types),
3085 (long) htab_elements (gimple_canonical_types),
3086 (long) gimple_canonical_types->searches,
3087 (long) gimple_canonical_types->collisions,
3088 htab_collisions (gimple_canonical_types));
3089 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3090 "%lu elements, %ld searches\n", pfx,
3091 num_canonical_type_hash_entries,
3092 num_canonical_type_hash_queries);
3095 print_lto_report (pfx);
3098 /* Perform whole program analysis (WPA) on the callgraph and write out the
3099 optimization plan. */
3101 static void
3102 do_whole_program_analysis (void)
3104 symtab_node *node;
3106 lto_parallelism = 1;
3108 /* TODO: jobserver communicatoin is not supported, yet. */
3109 if (!strcmp (flag_wpa, "jobserver"))
3110 lto_parallelism = -1;
3111 else
3113 lto_parallelism = atoi (flag_wpa);
3114 if (lto_parallelism <= 0)
3115 lto_parallelism = 0;
3118 timevar_start (TV_PHASE_OPT_GEN);
3120 /* Note that since we are in WPA mode, materialize_cgraph will not
3121 actually read in all the function bodies. It only materializes
3122 the decls and cgraph nodes so that analysis can be performed. */
3123 materialize_cgraph ();
3125 /* Reading in the cgraph uses different timers, start timing WPA now. */
3126 timevar_push (TV_WHOPR_WPA);
3128 if (pre_ipa_mem_report)
3130 fprintf (stderr, "Memory consumption before IPA\n");
3131 dump_memory_report (false);
3134 symtab->function_flags_ready = true;
3136 if (symtab->dump_file)
3137 symtab->dump (symtab->dump_file);
3138 bitmap_obstack_initialize (NULL);
3139 symtab->state = IPA_SSA;
3141 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3143 /* When WPA analysis raises errors, do not bother to output anything. */
3144 if (seen_error ())
3145 return;
3147 if (symtab->dump_file)
3149 fprintf (symtab->dump_file, "Optimized ");
3150 symtab->dump (symtab->dump_file);
3153 symtab_node::checking_verify_symtab_nodes ();
3154 bitmap_obstack_release (NULL);
3156 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3157 timevar_pop (TV_WHOPR_WPA);
3159 timevar_push (TV_WHOPR_PARTITIONING);
3160 if (flag_lto_partition == LTO_PARTITION_1TO1)
3161 lto_1_to_1_map ();
3162 else if (flag_lto_partition == LTO_PARTITION_MAX)
3163 lto_max_map ();
3164 else if (flag_lto_partition == LTO_PARTITION_ONE)
3165 lto_balanced_map (1, INT_MAX);
3166 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3167 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS),
3168 PARAM_VALUE (MAX_PARTITION_SIZE));
3169 else
3170 gcc_unreachable ();
3172 /* Inline summaries are needed for balanced partitioning. Free them now so
3173 the memory can be used for streamer caches. */
3174 ipa_free_fn_summary ();
3176 /* AUX pointers are used by partitioning code to bookkeep number of
3177 partitions symbol is in. This is no longer needed. */
3178 FOR_EACH_SYMBOL (node)
3179 node->aux = NULL;
3181 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3183 /* Find out statics that need to be promoted
3184 to globals with hidden visibility because they are accessed from multiple
3185 partitions. */
3186 lto_promote_cross_file_statics ();
3187 timevar_pop (TV_WHOPR_PARTITIONING);
3189 timevar_stop (TV_PHASE_OPT_GEN);
3191 /* Collect a last time - in lto_wpa_write_files we may end up forking
3192 with the idea that this doesn't increase memory usage. So we
3193 absoultely do not want to collect after that. */
3194 ggc_collect ();
3196 timevar_start (TV_PHASE_STREAM_OUT);
3197 if (!quiet_flag)
3199 fprintf (stderr, "\nStreaming out");
3200 fflush (stderr);
3202 lto_wpa_write_files ();
3203 if (!quiet_flag)
3204 fprintf (stderr, "\n");
3205 timevar_stop (TV_PHASE_STREAM_OUT);
3207 if (post_ipa_mem_report)
3209 fprintf (stderr, "Memory consumption after IPA\n");
3210 dump_memory_report (false);
3213 /* Show the LTO report before launching LTRANS. */
3214 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3215 print_lto_report_1 ();
3216 if (mem_report_wpa)
3217 dump_memory_report (true);
3221 static GTY(()) tree lto_eh_personality_decl;
3223 /* Return the LTO personality function decl. */
3225 tree
3226 lto_eh_personality (void)
3228 if (!lto_eh_personality_decl)
3230 /* Use the first personality DECL for our personality if we don't
3231 support multiple ones. This ensures that we don't artificially
3232 create the need for them in a single-language program. */
3233 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3234 lto_eh_personality_decl = first_personality_decl;
3235 else
3236 lto_eh_personality_decl = lhd_gcc_personality ();
3239 return lto_eh_personality_decl;
3242 /* Set the process name based on the LTO mode. */
3244 static void
3245 lto_process_name (void)
3247 if (flag_lto)
3248 setproctitle ("lto1-lto");
3249 if (flag_wpa)
3250 setproctitle ("lto1-wpa");
3251 if (flag_ltrans)
3252 setproctitle ("lto1-ltrans");
3256 /* Initialize the LTO front end. */
3258 static void
3259 lto_init (void)
3261 lto_process_name ();
3262 lto_streamer_hooks_init ();
3263 lto_reader_init ();
3264 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3265 memset (&lto_stats, 0, sizeof (lto_stats));
3266 bitmap_obstack_initialize (NULL);
3267 gimple_register_cfg_hooks ();
3268 #ifndef ACCEL_COMPILER
3269 unsigned char *table
3270 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3271 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3272 table[m] = m;
3273 lto_mode_identity_table = table;
3274 #endif
3277 /* Create artificial pointers for "omp declare target link" vars. */
3279 static void
3280 offload_handle_link_vars (void)
3282 #ifdef ACCEL_COMPILER
3283 varpool_node *var;
3284 FOR_EACH_VARIABLE (var)
3285 if (lookup_attribute ("omp declare target link",
3286 DECL_ATTRIBUTES (var->decl)))
3288 tree type = build_pointer_type (TREE_TYPE (var->decl));
3289 tree link_ptr_var = make_node (VAR_DECL);
3290 TREE_TYPE (link_ptr_var) = type;
3291 TREE_USED (link_ptr_var) = 1;
3292 TREE_STATIC (link_ptr_var) = 1;
3293 SET_DECL_MODE (link_ptr_var, TYPE_MODE (type));
3294 DECL_SIZE (link_ptr_var) = TYPE_SIZE (type);
3295 DECL_SIZE_UNIT (link_ptr_var) = TYPE_SIZE_UNIT (type);
3296 DECL_ARTIFICIAL (link_ptr_var) = 1;
3297 tree var_name = DECL_ASSEMBLER_NAME (var->decl);
3298 char *new_name
3299 = ACONCAT ((IDENTIFIER_POINTER (var_name), "_linkptr", NULL));
3300 DECL_NAME (link_ptr_var) = get_identifier (new_name);
3301 SET_DECL_ASSEMBLER_NAME (link_ptr_var, DECL_NAME (link_ptr_var));
3302 SET_DECL_VALUE_EXPR (var->decl, build_simple_mem_ref (link_ptr_var));
3303 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
3305 #endif
3309 /* Main entry point for the GIMPLE front end. This front end has
3310 three main personalities:
3312 - LTO (-flto). All the object files on the command line are
3313 loaded in memory and processed as a single translation unit.
3314 This is the traditional link-time optimization behavior.
3316 - WPA (-fwpa). Only the callgraph and summary information for
3317 files in the command file are loaded. A single callgraph
3318 (without function bodies) is instantiated for the whole set of
3319 files. IPA passes are only allowed to analyze the call graph
3320 and make transformation decisions. The callgraph is
3321 partitioned, each partition is written to a new object file
3322 together with the transformation decisions.
3324 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3325 summary files from running again. Since WPA computed summary
3326 information and decided what transformations to apply, LTRANS
3327 simply applies them. */
3329 void
3330 lto_main (void)
3332 /* LTO is called as a front end, even though it is not a front end.
3333 Because it is called as a front end, TV_PHASE_PARSING and
3334 TV_PARSE_GLOBAL are active, and we need to turn them off while
3335 doing LTO. Later we turn them back on so they are active up in
3336 toplev.c. */
3337 timevar_pop (TV_PARSE_GLOBAL);
3338 timevar_stop (TV_PHASE_PARSING);
3340 timevar_start (TV_PHASE_SETUP);
3342 /* Initialize the LTO front end. */
3343 lto_init ();
3345 timevar_stop (TV_PHASE_SETUP);
3346 timevar_start (TV_PHASE_STREAM_IN);
3348 /* Read all the symbols and call graph from all the files in the
3349 command line. */
3350 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3352 timevar_stop (TV_PHASE_STREAM_IN);
3354 if (!seen_error ())
3356 offload_handle_link_vars ();
3358 /* If WPA is enabled analyze the whole call graph and create an
3359 optimization plan. Otherwise, read in all the function
3360 bodies and continue with optimization. */
3361 if (flag_wpa)
3362 do_whole_program_analysis ();
3363 else
3365 timevar_start (TV_PHASE_OPT_GEN);
3367 materialize_cgraph ();
3368 if (!flag_ltrans)
3369 lto_promote_statics_nonwpa ();
3371 /* Annotate the CU DIE and mark the early debug phase as finished. */
3372 debug_hooks->early_finish ("<artificial>");
3374 /* Let the middle end know that we have read and merged all of
3375 the input files. */
3376 symtab->compile ();
3378 timevar_stop (TV_PHASE_OPT_GEN);
3380 /* FIXME lto, if the processes spawned by WPA fail, we miss
3381 the chance to print WPA's report, so WPA will call
3382 print_lto_report before launching LTRANS. If LTRANS was
3383 launched directly by the driver we would not need to do
3384 this. */
3385 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3386 print_lto_report_1 ();
3390 /* Here we make LTO pretend to be a parser. */
3391 timevar_start (TV_PHASE_PARSING);
3392 timevar_push (TV_PARSE_GLOBAL);
3395 #include "gt-lto-lto.h"