/cp
[official-gcc.git] / gcc / lto / lto.c
blob2d80ffec61a897b3fd8a8070141e53326fa43dda
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "toplev.h"
26 #include "alias.h"
27 #include "tm.h"
28 #include "function.h"
29 #include "bitmap.h"
30 #include "cfghooks.h"
31 #include "basic-block.h"
32 #include "tree.h"
33 #include "gimple.h"
34 #include "hard-reg-set.h"
35 #include "options.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "diagnostic-core.h"
39 #include "cgraph.h"
40 #include "tree-ssa-operands.h"
41 #include "tree-pass.h"
42 #include "langhooks.h"
43 #include "alloc-pool.h"
44 #include "symbol-summary.h"
45 #include "ipa-prop.h"
46 #include "common.h"
47 #include "debug.h"
48 #include "internal-fn.h"
49 #include "lto.h"
50 #include "lto-tree.h"
51 #include "lto-streamer.h"
52 #include "lto-section-names.h"
53 #include "tree-streamer.h"
54 #include "splay-tree.h"
55 #include "lto-partition.h"
56 #include "data-streamer.h"
57 #include "context.h"
58 #include "pass_manager.h"
59 #include "ipa-inline.h"
60 #include "params.h"
61 #include "ipa-utils.h"
62 #include "gomp-constants.h"
65 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
66 static int lto_parallelism;
68 static GTY(()) tree first_personality_decl;
70 static GTY(()) const unsigned char *lto_mode_identity_table;
72 /* Returns a hash code for P. */
74 static hashval_t
75 hash_name (const void *p)
77 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
78 return (hashval_t) htab_hash_string (ds->name);
82 /* Returns nonzero if P1 and P2 are equal. */
84 static int
85 eq_name (const void *p1, const void *p2)
87 const struct lto_section_slot *s1 =
88 (const struct lto_section_slot *) p1;
89 const struct lto_section_slot *s2 =
90 (const struct lto_section_slot *) p2;
92 return strcmp (s1->name, s2->name) == 0;
95 /* Free lto_section_slot */
97 static void
98 free_with_string (void *arg)
100 struct lto_section_slot *s = (struct lto_section_slot *)arg;
102 free (CONST_CAST (char *, s->name));
103 free (arg);
106 /* Create section hash table */
108 htab_t
109 lto_obj_create_section_hash_table (void)
111 return htab_create (37, hash_name, eq_name, free_with_string);
114 /* Delete an allocated integer KEY in the splay tree. */
116 static void
117 lto_splay_tree_delete_id (splay_tree_key key)
119 free ((void *) key);
122 /* Compare splay tree node ids A and B. */
124 static int
125 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
127 unsigned HOST_WIDE_INT ai;
128 unsigned HOST_WIDE_INT bi;
130 ai = *(unsigned HOST_WIDE_INT *) a;
131 bi = *(unsigned HOST_WIDE_INT *) b;
133 if (ai < bi)
134 return -1;
135 else if (ai > bi)
136 return 1;
137 return 0;
140 /* Look up splay tree node by ID in splay tree T. */
142 static splay_tree_node
143 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
145 return splay_tree_lookup (t, (splay_tree_key) &id);
148 /* Check if KEY has ID. */
150 static bool
151 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
153 return *(unsigned HOST_WIDE_INT *) key == id;
156 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
157 The ID is allocated separately because we need HOST_WIDE_INTs which may
158 be wider than a splay_tree_key. */
160 static void
161 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
162 struct lto_file_decl_data *file_data)
164 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
165 *idp = id;
166 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
169 /* Create a splay tree. */
171 static splay_tree
172 lto_splay_tree_new (void)
174 return splay_tree_new (lto_splay_tree_compare_ids,
175 lto_splay_tree_delete_id,
176 NULL);
179 /* Return true when NODE has a clone that is analyzed (i.e. we need
180 to load its body even if the node itself is not needed). */
182 static bool
183 has_analyzed_clone_p (struct cgraph_node *node)
185 struct cgraph_node *orig = node;
186 node = node->clones;
187 if (node)
188 while (node != orig)
190 if (node->analyzed)
191 return true;
192 if (node->clones)
193 node = node->clones;
194 else if (node->next_sibling_clone)
195 node = node->next_sibling_clone;
196 else
198 while (node != orig && !node->next_sibling_clone)
199 node = node->clone_of;
200 if (node != orig)
201 node = node->next_sibling_clone;
204 return false;
207 /* Read the function body for the function associated with NODE. */
209 static void
210 lto_materialize_function (struct cgraph_node *node)
212 tree decl;
214 decl = node->decl;
215 /* Read in functions with body (analyzed nodes)
216 and also functions that are needed to produce virtual clones. */
217 if ((node->has_gimple_body_p () && node->analyzed)
218 || node->used_as_abstract_origin
219 || has_analyzed_clone_p (node))
221 /* Clones don't need to be read. */
222 if (node->clone_of)
223 return;
224 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
225 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
228 /* Let the middle end know about the function. */
229 rest_of_decl_compilation (decl, 1, 0);
233 /* Decode the content of memory pointed to by DATA in the in decl
234 state object STATE. DATA_IN points to a data_in structure for
235 decoding. Return the address after the decoded object in the
236 input. */
238 static const uint32_t *
239 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
240 struct lto_in_decl_state *state)
242 uint32_t ix;
243 tree decl;
244 uint32_t i, j;
246 ix = *data++;
247 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
248 if (!VAR_OR_FUNCTION_DECL_P (decl))
250 gcc_assert (decl == void_type_node);
251 decl = NULL_TREE;
253 state->fn_decl = decl;
255 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
257 uint32_t size = *data++;
258 vec<tree, va_gc> *decls = NULL;
259 vec_alloc (decls, size);
261 for (j = 0; j < size; j++)
262 vec_safe_push (decls,
263 streamer_tree_cache_get_tree (data_in->reader_cache,
264 data[j]));
266 state->streams[i] = decls;
267 data += size;
270 return data;
274 /* Global canonical type table. */
275 static htab_t gimple_canonical_types;
276 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
277 static unsigned long num_canonical_type_hash_entries;
278 static unsigned long num_canonical_type_hash_queries;
280 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
281 static hashval_t gimple_canonical_type_hash (const void *p);
282 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
284 /* Returning a hash value for gimple type TYPE.
286 The hash value returned is equal for types considered compatible
287 by gimple_canonical_types_compatible_p. */
289 static hashval_t
290 hash_canonical_type (tree type)
292 inchash::hash hstate;
294 /* We compute alias sets only for types that needs them.
295 Be sure we do not recurse to something else as we can not hash incomplete
296 types in a way they would have same hash value as compatible complete
297 types. */
298 gcc_checking_assert (type_with_alias_set_p (type));
300 /* Combine a few common features of types so that types are grouped into
301 smaller sets; when searching for existing matching types to merge,
302 only existing types having the same features as the new type will be
303 checked. */
304 hstate.add_int (tree_code_for_canonical_type_merging (TREE_CODE (type)));
305 hstate.add_int (TYPE_MODE (type));
307 /* Incorporate common features of numerical types. */
308 if (INTEGRAL_TYPE_P (type)
309 || SCALAR_FLOAT_TYPE_P (type)
310 || FIXED_POINT_TYPE_P (type)
311 || TREE_CODE (type) == OFFSET_TYPE
312 || POINTER_TYPE_P (type))
314 hstate.add_int (TYPE_UNSIGNED (type));
315 hstate.add_int (TYPE_PRECISION (type));
318 if (VECTOR_TYPE_P (type))
320 hstate.add_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)
380 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
381 nf++;
384 hstate.add_int (nf);
387 return hstate.end();
390 /* Returning a hash value for gimple type TYPE combined with VAL. */
392 static void
393 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
395 hashval_t v;
397 /* All type variants have same TYPE_CANONICAL. */
398 type = TYPE_MAIN_VARIANT (type);
399 /* An already processed type. */
400 if (TYPE_CANONICAL (type))
402 type = TYPE_CANONICAL (type);
403 v = gimple_canonical_type_hash (type);
405 else
407 /* Canonical types should not be able to form SCCs by design, this
408 recursion is just because we do not register canonical types in
409 optimal order. To avoid quadratic behavior also register the
410 type here. */
411 v = hash_canonical_type (type);
412 gimple_register_canonical_type_1 (type, v);
414 hstate.add_int (v);
417 /* Returns the hash for a canonical type P. */
419 static hashval_t
420 gimple_canonical_type_hash (const void *p)
422 num_canonical_type_hash_queries++;
423 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
424 gcc_assert (slot != NULL);
425 return *slot;
430 /* Returns nonzero if P1 and P2 are equal. */
432 static int
433 gimple_canonical_type_eq (const void *p1, const void *p2)
435 const_tree t1 = (const_tree) p1;
436 const_tree t2 = (const_tree) p2;
437 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
438 CONST_CAST_TREE (t2));
441 /* Main worker for gimple_register_canonical_type. */
443 static void
444 gimple_register_canonical_type_1 (tree t, hashval_t hash)
446 void **slot;
448 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
450 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
451 if (*slot)
453 tree new_type = (tree)(*slot);
454 gcc_checking_assert (new_type != t);
455 TYPE_CANONICAL (t) = new_type;
457 else
459 TYPE_CANONICAL (t) = t;
460 *slot = (void *) t;
461 /* Cache the just computed hash value. */
462 num_canonical_type_hash_entries++;
463 bool existed_p = canonical_type_hash_cache->put (t, hash);
464 gcc_assert (!existed_p);
468 /* Register type T in the global type table gimple_types and set
469 TYPE_CANONICAL of T accordingly.
470 This is used by LTO to merge structurally equivalent types for
471 type-based aliasing purposes across different TUs and languages.
473 ??? This merging does not exactly match how the tree.c middle-end
474 functions will assign TYPE_CANONICAL when new types are created
475 during optimization (which at least happens for pointer and array
476 types). */
478 static void
479 gimple_register_canonical_type (tree t)
481 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t))
482 return;
484 /* Canonical types are same among all complete variants. */
485 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
486 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
487 else
489 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t),
490 hash_canonical_type (TYPE_MAIN_VARIANT (t)));
491 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
495 /* Re-compute TYPE_CANONICAL for NODE and related types. */
497 static void
498 lto_register_canonical_types (tree node, bool first_p)
500 if (!node
501 || !TYPE_P (node))
502 return;
504 if (first_p)
505 TYPE_CANONICAL (node) = NULL_TREE;
507 if (POINTER_TYPE_P (node)
508 || TREE_CODE (node) == COMPLEX_TYPE
509 || TREE_CODE (node) == ARRAY_TYPE)
510 lto_register_canonical_types (TREE_TYPE (node), first_p);
512 if (!first_p)
513 gimple_register_canonical_type (node);
517 /* Remember trees that contains references to declarations. */
518 static GTY(()) vec <tree, va_gc> *tree_with_vars;
520 #define CHECK_VAR(tt) \
521 do \
523 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
524 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
525 return true; \
526 } while (0)
528 #define CHECK_NO_VAR(tt) \
529 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
531 /* Check presence of pointers to decls in fields of a tree_typed T. */
533 static inline bool
534 mentions_vars_p_typed (tree t)
536 CHECK_NO_VAR (TREE_TYPE (t));
537 return false;
540 /* Check presence of pointers to decls in fields of a tree_common T. */
542 static inline bool
543 mentions_vars_p_common (tree t)
545 if (mentions_vars_p_typed (t))
546 return true;
547 CHECK_NO_VAR (TREE_CHAIN (t));
548 return false;
551 /* Check presence of pointers to decls in fields of a decl_minimal T. */
553 static inline bool
554 mentions_vars_p_decl_minimal (tree t)
556 if (mentions_vars_p_common (t))
557 return true;
558 CHECK_NO_VAR (DECL_NAME (t));
559 CHECK_VAR (DECL_CONTEXT (t));
560 return false;
563 /* Check presence of pointers to decls in fields of a decl_common T. */
565 static inline bool
566 mentions_vars_p_decl_common (tree t)
568 if (mentions_vars_p_decl_minimal (t))
569 return true;
570 CHECK_VAR (DECL_SIZE (t));
571 CHECK_VAR (DECL_SIZE_UNIT (t));
572 CHECK_VAR (DECL_INITIAL (t));
573 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
574 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
575 return false;
578 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
580 static inline bool
581 mentions_vars_p_decl_with_vis (tree t)
583 if (mentions_vars_p_decl_common (t))
584 return true;
586 /* Accessor macro has side-effects, use field-name here. */
587 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
588 return false;
591 /* Check presence of pointers to decls in fields of a decl_non_common T. */
593 static inline bool
594 mentions_vars_p_decl_non_common (tree t)
596 if (mentions_vars_p_decl_with_vis (t))
597 return true;
598 CHECK_NO_VAR (DECL_RESULT_FLD (t));
599 return false;
602 /* Check presence of pointers to decls in fields of a decl_non_common T. */
604 static bool
605 mentions_vars_p_function (tree t)
607 if (mentions_vars_p_decl_non_common (t))
608 return true;
609 CHECK_NO_VAR (DECL_ARGUMENTS (t));
610 CHECK_NO_VAR (DECL_VINDEX (t));
611 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
612 return false;
615 /* Check presence of pointers to decls in fields of a field_decl T. */
617 static bool
618 mentions_vars_p_field_decl (tree t)
620 if (mentions_vars_p_decl_common (t))
621 return true;
622 CHECK_VAR (DECL_FIELD_OFFSET (t));
623 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
624 CHECK_NO_VAR (DECL_QUALIFIER (t));
625 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
626 CHECK_NO_VAR (DECL_FCONTEXT (t));
627 return false;
630 /* Check presence of pointers to decls in fields of a type T. */
632 static bool
633 mentions_vars_p_type (tree t)
635 if (mentions_vars_p_common (t))
636 return true;
637 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
638 CHECK_VAR (TYPE_SIZE (t));
639 CHECK_VAR (TYPE_SIZE_UNIT (t));
640 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
641 CHECK_NO_VAR (TYPE_NAME (t));
643 CHECK_VAR (TYPE_MINVAL (t));
644 CHECK_VAR (TYPE_MAXVAL (t));
646 /* Accessor is for derived node types only. */
647 CHECK_NO_VAR (t->type_non_common.binfo);
649 CHECK_VAR (TYPE_CONTEXT (t));
650 CHECK_NO_VAR (TYPE_CANONICAL (t));
651 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
652 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
653 return false;
656 /* Check presence of pointers to decls in fields of a BINFO T. */
658 static bool
659 mentions_vars_p_binfo (tree t)
661 unsigned HOST_WIDE_INT i, n;
663 if (mentions_vars_p_common (t))
664 return true;
665 CHECK_VAR (BINFO_VTABLE (t));
666 CHECK_NO_VAR (BINFO_OFFSET (t));
667 CHECK_NO_VAR (BINFO_VIRTUALS (t));
668 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
669 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
670 for (i = 0; i < n; i++)
671 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
672 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
673 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
674 n = BINFO_N_BASE_BINFOS (t);
675 for (i = 0; i < n; i++)
676 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
677 return false;
680 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
682 static bool
683 mentions_vars_p_constructor (tree t)
685 unsigned HOST_WIDE_INT idx;
686 constructor_elt *ce;
688 if (mentions_vars_p_typed (t))
689 return true;
691 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
693 CHECK_NO_VAR (ce->index);
694 CHECK_VAR (ce->value);
696 return false;
699 /* Check presence of pointers to decls in fields of an expression tree T. */
701 static bool
702 mentions_vars_p_expr (tree t)
704 int i;
705 if (mentions_vars_p_typed (t))
706 return true;
707 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
708 CHECK_VAR (TREE_OPERAND (t, i));
709 return false;
712 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
714 static bool
715 mentions_vars_p_omp_clause (tree t)
717 int i;
718 if (mentions_vars_p_common (t))
719 return true;
720 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
721 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
722 return false;
725 /* Check presence of pointers to decls that needs later fixup in T. */
727 static bool
728 mentions_vars_p (tree t)
730 switch (TREE_CODE (t))
732 case IDENTIFIER_NODE:
733 break;
735 case TREE_LIST:
736 CHECK_VAR (TREE_VALUE (t));
737 CHECK_VAR (TREE_PURPOSE (t));
738 CHECK_NO_VAR (TREE_CHAIN (t));
739 break;
741 case FIELD_DECL:
742 return mentions_vars_p_field_decl (t);
744 case LABEL_DECL:
745 case CONST_DECL:
746 case PARM_DECL:
747 case RESULT_DECL:
748 case IMPORTED_DECL:
749 case NAMESPACE_DECL:
750 case NAMELIST_DECL:
751 return mentions_vars_p_decl_common (t);
753 case VAR_DECL:
754 return mentions_vars_p_decl_with_vis (t);
756 case TYPE_DECL:
757 return mentions_vars_p_decl_non_common (t);
759 case FUNCTION_DECL:
760 return mentions_vars_p_function (t);
762 case TREE_BINFO:
763 return mentions_vars_p_binfo (t);
765 case PLACEHOLDER_EXPR:
766 return mentions_vars_p_common (t);
768 case BLOCK:
769 case TRANSLATION_UNIT_DECL:
770 case OPTIMIZATION_NODE:
771 case TARGET_OPTION_NODE:
772 break;
774 case CONSTRUCTOR:
775 return mentions_vars_p_constructor (t);
777 case OMP_CLAUSE:
778 return mentions_vars_p_omp_clause (t);
780 default:
781 if (TYPE_P (t))
783 if (mentions_vars_p_type (t))
784 return true;
786 else if (EXPR_P (t))
788 if (mentions_vars_p_expr (t))
789 return true;
791 else if (CONSTANT_CLASS_P (t))
792 CHECK_NO_VAR (TREE_TYPE (t));
793 else
794 gcc_unreachable ();
796 return false;
800 /* Return the resolution for the decl with index INDEX from DATA_IN. */
802 static enum ld_plugin_symbol_resolution
803 get_resolution (struct data_in *data_in, unsigned index)
805 if (data_in->globals_resolution.exists ())
807 ld_plugin_symbol_resolution_t ret;
808 /* We can have references to not emitted functions in
809 DECL_FUNCTION_PERSONALITY at least. So we can and have
810 to indeed return LDPR_UNKNOWN in some cases. */
811 if (data_in->globals_resolution.length () <= index)
812 return LDPR_UNKNOWN;
813 ret = data_in->globals_resolution[index];
814 return ret;
816 else
817 /* Delay resolution finding until decl merging. */
818 return LDPR_UNKNOWN;
821 /* We need to record resolutions until symbol table is read. */
822 static void
823 register_resolution (struct lto_file_decl_data *file_data, tree decl,
824 enum ld_plugin_symbol_resolution resolution)
826 if (resolution == LDPR_UNKNOWN)
827 return;
828 if (!file_data->resolution_map)
829 file_data->resolution_map
830 = new hash_map<tree, ld_plugin_symbol_resolution>;
831 file_data->resolution_map->put (decl, resolution);
834 /* Register DECL with the global symbol table and change its
835 name if necessary to avoid name clashes for static globals across
836 different files. */
838 static void
839 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
840 unsigned ix)
842 tree context;
844 /* Variable has file scope, not local. */
845 if (!TREE_PUBLIC (decl)
846 && !((context = decl_function_context (decl))
847 && auto_var_in_fn_p (decl, context)))
848 rest_of_decl_compilation (decl, 1, 0);
850 /* If this variable has already been declared, queue the
851 declaration for merging. */
852 if (TREE_PUBLIC (decl))
853 register_resolution (data_in->file_data,
854 decl, get_resolution (data_in, ix));
858 /* Register DECL with the global symbol table and change its
859 name if necessary to avoid name clashes for static globals across
860 different files. DATA_IN contains descriptors and tables for the
861 file being read. */
863 static void
864 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
865 unsigned ix)
867 /* If this variable has already been declared, queue the
868 declaration for merging. */
869 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
870 register_resolution (data_in->file_data,
871 decl, get_resolution (data_in, ix));
875 /* For the type T re-materialize it in the type variant list and
876 the pointer/reference-to chains. */
878 static void
879 lto_fixup_prevailing_type (tree t)
881 /* The following re-creates proper variant lists while fixing up
882 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
883 variant list state before fixup is broken. */
885 /* If we are not our own variant leader link us into our new leaders
886 variant list. */
887 if (TYPE_MAIN_VARIANT (t) != t)
889 tree mv = TYPE_MAIN_VARIANT (t);
890 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
891 TYPE_NEXT_VARIANT (mv) = t;
894 /* The following reconstructs the pointer chains
895 of the new pointed-to type if we are a main variant. We do
896 not stream those so they are broken before fixup. */
897 if (TREE_CODE (t) == POINTER_TYPE
898 && TYPE_MAIN_VARIANT (t) == t)
900 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
901 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
903 else if (TREE_CODE (t) == REFERENCE_TYPE
904 && TYPE_MAIN_VARIANT (t) == t)
906 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
907 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
912 /* We keep prevailing tree SCCs in a hashtable with manual collision
913 handling (in case all hashes compare the same) and keep the colliding
914 entries in the tree_scc->next chain. */
916 struct tree_scc
918 tree_scc *next;
919 /* Hash of the whole SCC. */
920 hashval_t hash;
921 /* Number of trees in the SCC. */
922 unsigned len;
923 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
924 which share the same individual tree hash). */
925 unsigned entry_len;
926 /* The members of the SCC.
927 We only need to remember the first entry node candidate for prevailing
928 SCCs (but of course have access to all entries for SCCs we are
929 processing).
930 ??? For prevailing SCCs we really only need hash and the first
931 entry candidate, but that's too awkward to implement. */
932 tree entries[1];
935 struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
937 static inline hashval_t hash (const tree_scc *);
938 static inline bool equal (const tree_scc *, const tree_scc *);
941 hashval_t
942 tree_scc_hasher::hash (const tree_scc *scc)
944 return scc->hash;
947 bool
948 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
950 if (scc1->hash != scc2->hash
951 || scc1->len != scc2->len
952 || scc1->entry_len != scc2->entry_len)
953 return false;
954 return true;
957 static hash_table<tree_scc_hasher> *tree_scc_hash;
958 static struct obstack tree_scc_hash_obstack;
960 static unsigned long num_merged_types;
961 static unsigned long num_prevailing_types;
962 static unsigned long num_type_scc_trees;
963 static unsigned long total_scc_size;
964 static unsigned long num_sccs_read;
965 static unsigned long total_scc_size_merged;
966 static unsigned long num_sccs_merged;
967 static unsigned long num_scc_compares;
968 static unsigned long num_scc_compare_collisions;
971 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
972 recursing through in-SCC tree edges. Returns true if the SCCs entered
973 through T1 and T2 are equal and fills in *MAP with the pairs of
974 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
976 static bool
977 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
979 enum tree_code code;
981 /* Mark already visited nodes. */
982 TREE_ASM_WRITTEN (t2) = 1;
984 /* Push the pair onto map. */
985 (*map)[0] = t1;
986 (*map)[1] = t2;
987 *map = *map + 2;
989 /* Compare value-fields. */
990 #define compare_values(X) \
991 do { \
992 if (X(t1) != X(t2)) \
993 return false; \
994 } while (0)
996 compare_values (TREE_CODE);
997 code = TREE_CODE (t1);
999 if (!TYPE_P (t1))
1001 compare_values (TREE_SIDE_EFFECTS);
1002 compare_values (TREE_CONSTANT);
1003 compare_values (TREE_READONLY);
1004 compare_values (TREE_PUBLIC);
1006 compare_values (TREE_ADDRESSABLE);
1007 compare_values (TREE_THIS_VOLATILE);
1008 if (DECL_P (t1))
1009 compare_values (DECL_UNSIGNED);
1010 else if (TYPE_P (t1))
1011 compare_values (TYPE_UNSIGNED);
1012 if (TYPE_P (t1))
1013 compare_values (TYPE_ARTIFICIAL);
1014 else
1015 compare_values (TREE_NO_WARNING);
1016 compare_values (TREE_NOTHROW);
1017 compare_values (TREE_STATIC);
1018 if (code != TREE_BINFO)
1019 compare_values (TREE_PRIVATE);
1020 compare_values (TREE_PROTECTED);
1021 compare_values (TREE_DEPRECATED);
1022 if (TYPE_P (t1))
1024 compare_values (TYPE_SATURATING);
1025 compare_values (TYPE_ADDR_SPACE);
1027 else if (code == SSA_NAME)
1028 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1030 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1032 if (!wi::eq_p (t1, t2))
1033 return false;
1036 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1038 /* ??? No suitable compare routine available. */
1039 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1040 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1041 if (r1.cl != r2.cl
1042 || r1.decimal != r2.decimal
1043 || r1.sign != r2.sign
1044 || r1.signalling != r2.signalling
1045 || r1.canonical != r2.canonical
1046 || r1.uexp != r2.uexp)
1047 return false;
1048 for (unsigned i = 0; i < SIGSZ; ++i)
1049 if (r1.sig[i] != r2.sig[i])
1050 return false;
1053 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1054 if (!fixed_compare (EQ_EXPR,
1055 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1056 return false;
1059 /* We don't want to compare locations, so there is nothing do compare
1060 for TS_DECL_MINIMAL. */
1062 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1064 compare_values (DECL_MODE);
1065 compare_values (DECL_NONLOCAL);
1066 compare_values (DECL_VIRTUAL_P);
1067 compare_values (DECL_IGNORED_P);
1068 compare_values (DECL_ABSTRACT_P);
1069 compare_values (DECL_ARTIFICIAL);
1070 compare_values (DECL_USER_ALIGN);
1071 compare_values (DECL_PRESERVE_P);
1072 compare_values (DECL_EXTERNAL);
1073 compare_values (DECL_GIMPLE_REG_P);
1074 compare_values (DECL_ALIGN);
1075 if (code == LABEL_DECL)
1077 compare_values (EH_LANDING_PAD_NR);
1078 compare_values (LABEL_DECL_UID);
1080 else if (code == FIELD_DECL)
1082 compare_values (DECL_PACKED);
1083 compare_values (DECL_NONADDRESSABLE_P);
1084 compare_values (DECL_OFFSET_ALIGN);
1086 else if (code == VAR_DECL)
1088 compare_values (DECL_HAS_DEBUG_EXPR_P);
1089 compare_values (DECL_NONLOCAL_FRAME);
1091 if (code == RESULT_DECL
1092 || code == PARM_DECL
1093 || code == VAR_DECL)
1095 compare_values (DECL_BY_REFERENCE);
1096 if (code == VAR_DECL
1097 || code == PARM_DECL)
1098 compare_values (DECL_HAS_VALUE_EXPR_P);
1102 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1103 compare_values (DECL_REGISTER);
1105 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1107 compare_values (DECL_COMMON);
1108 compare_values (DECL_DLLIMPORT_P);
1109 compare_values (DECL_WEAK);
1110 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1111 compare_values (DECL_COMDAT);
1112 compare_values (DECL_VISIBILITY);
1113 compare_values (DECL_VISIBILITY_SPECIFIED);
1114 if (code == VAR_DECL)
1116 compare_values (DECL_HARD_REGISTER);
1117 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1118 compare_values (DECL_IN_CONSTANT_POOL);
1122 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1124 compare_values (DECL_BUILT_IN_CLASS);
1125 compare_values (DECL_STATIC_CONSTRUCTOR);
1126 compare_values (DECL_STATIC_DESTRUCTOR);
1127 compare_values (DECL_UNINLINABLE);
1128 compare_values (DECL_POSSIBLY_INLINED);
1129 compare_values (DECL_IS_NOVOPS);
1130 compare_values (DECL_IS_RETURNS_TWICE);
1131 compare_values (DECL_IS_MALLOC);
1132 compare_values (DECL_IS_OPERATOR_NEW);
1133 compare_values (DECL_DECLARED_INLINE_P);
1134 compare_values (DECL_STATIC_CHAIN);
1135 compare_values (DECL_NO_INLINE_WARNING_P);
1136 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1137 compare_values (DECL_NO_LIMIT_STACK);
1138 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1139 compare_values (DECL_PURE_P);
1140 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1141 compare_values (DECL_FINAL_P);
1142 compare_values (DECL_CXX_CONSTRUCTOR_P);
1143 compare_values (DECL_CXX_DESTRUCTOR_P);
1144 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1145 compare_values (DECL_FUNCTION_CODE);
1148 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1150 compare_values (TYPE_MODE);
1151 compare_values (TYPE_STRING_FLAG);
1152 compare_values (TYPE_NEEDS_CONSTRUCTING);
1153 if (RECORD_OR_UNION_TYPE_P (t1))
1155 compare_values (TYPE_TRANSPARENT_AGGR);
1156 compare_values (TYPE_FINAL_P);
1158 else if (code == ARRAY_TYPE)
1159 compare_values (TYPE_NONALIASED_COMPONENT);
1160 compare_values (TYPE_PACKED);
1161 compare_values (TYPE_RESTRICT);
1162 compare_values (TYPE_USER_ALIGN);
1163 compare_values (TYPE_READONLY);
1164 compare_values (TYPE_PRECISION);
1165 compare_values (TYPE_ALIGN);
1166 compare_values (TYPE_ALIAS_SET);
1169 /* We don't want to compare locations, so there is nothing do compare
1170 for TS_EXP. */
1172 /* BLOCKs are function local and we don't merge anything there, so
1173 simply refuse to merge. */
1174 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1175 return false;
1177 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1178 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1179 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1180 return false;
1182 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1183 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1184 return false;
1186 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1187 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1188 sizeof (struct cl_optimization)) != 0)
1189 return false;
1191 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1192 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1193 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1194 return false;
1196 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1197 compare_values (CONSTRUCTOR_NELTS);
1199 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1200 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1201 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1202 IDENTIFIER_LENGTH (t1)) != 0)
1203 return false;
1205 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1206 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1207 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1208 TREE_STRING_LENGTH (t1)) != 0)
1209 return false;
1211 if (code == OMP_CLAUSE)
1213 compare_values (OMP_CLAUSE_CODE);
1214 switch (OMP_CLAUSE_CODE (t1))
1216 case OMP_CLAUSE_DEFAULT:
1217 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1218 break;
1219 case OMP_CLAUSE_SCHEDULE:
1220 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1221 break;
1222 case OMP_CLAUSE_DEPEND:
1223 compare_values (OMP_CLAUSE_DEPEND_KIND);
1224 break;
1225 case OMP_CLAUSE_MAP:
1226 compare_values (OMP_CLAUSE_MAP_KIND);
1227 break;
1228 case OMP_CLAUSE_PROC_BIND:
1229 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1230 break;
1231 case OMP_CLAUSE_REDUCTION:
1232 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1233 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1234 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1235 break;
1236 default:
1237 break;
1241 #undef compare_values
1244 /* Compare pointer fields. */
1246 /* Recurse. Search & Replaced from DFS_write_tree_body.
1247 Folding the early checks into the compare_tree_edges recursion
1248 macro makes debugging way quicker as you are able to break on
1249 compare_tree_sccs_1 and simply finish until a call returns false
1250 to spot the SCC members with the difference. */
1251 #define compare_tree_edges(E1, E2) \
1252 do { \
1253 tree t1_ = (E1), t2_ = (E2); \
1254 if (t1_ != t2_ \
1255 && (!t1_ || !t2_ \
1256 || !TREE_VISITED (t2_) \
1257 || (!TREE_ASM_WRITTEN (t2_) \
1258 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1259 return false; \
1260 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1261 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1262 } while (0)
1264 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1266 if (code != IDENTIFIER_NODE)
1267 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1270 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1272 unsigned i;
1273 /* Note that the number of elements for EXPR has already been emitted
1274 in EXPR's header (see streamer_write_tree_header). */
1275 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1276 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1279 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1281 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1282 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1285 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1287 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1288 /* ??? Global decls from different TUs have non-matching
1289 TRANSLATION_UNIT_DECLs. Only consider a small set of
1290 decls equivalent, we should not end up merging others. */
1291 if ((code == TYPE_DECL
1292 || code == NAMESPACE_DECL
1293 || code == IMPORTED_DECL
1294 || code == CONST_DECL
1295 || (VAR_OR_FUNCTION_DECL_P (t1)
1296 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1297 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1299 else
1300 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1303 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1305 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1306 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1307 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1308 if ((code == VAR_DECL
1309 || code == PARM_DECL)
1310 && DECL_HAS_VALUE_EXPR_P (t1))
1311 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1312 if (code == VAR_DECL
1313 && DECL_HAS_DEBUG_EXPR_P (t1))
1314 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1315 /* LTO specific edges. */
1316 if (code != FUNCTION_DECL
1317 && code != TRANSLATION_UNIT_DECL)
1318 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1321 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1323 if (code == FUNCTION_DECL)
1325 tree a1, a2;
1326 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1327 a1 || a2;
1328 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1329 compare_tree_edges (a1, a2);
1330 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1332 else if (code == TYPE_DECL)
1333 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1336 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1338 /* Make sure we don't inadvertently set the assembler name. */
1339 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1340 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1341 DECL_ASSEMBLER_NAME (t2));
1344 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1346 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1347 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1348 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1349 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1350 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1351 DECL_FIELD_BIT_OFFSET (t2));
1352 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1355 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1357 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1358 DECL_FUNCTION_PERSONALITY (t2));
1359 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1360 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1361 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1362 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1363 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1366 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1368 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1369 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1370 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1371 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1372 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1373 reconstructed during fixup. */
1374 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1375 during fixup. */
1376 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1377 /* ??? Global types from different TUs have non-matching
1378 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1379 equal. */
1380 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1382 else
1383 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1384 /* TYPE_CANONICAL is re-computed during type merging, so do not
1385 compare it here. */
1386 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1389 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1391 if (code == ENUMERAL_TYPE)
1392 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1393 else if (code == ARRAY_TYPE)
1394 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1395 else if (RECORD_OR_UNION_TYPE_P (t1))
1397 tree f1, f2;
1398 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1399 f1 || f2;
1400 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1401 compare_tree_edges (f1, f2);
1402 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1404 else if (code == FUNCTION_TYPE
1405 || code == METHOD_TYPE)
1406 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1407 if (!POINTER_TYPE_P (t1))
1408 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1409 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1412 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1414 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1415 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1416 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1419 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1420 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1421 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1423 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1425 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1426 compare_tree_edges (TREE_OPERAND (t1, i),
1427 TREE_OPERAND (t2, i));
1429 /* BLOCKs are function local and we don't merge anything there. */
1430 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1431 return false;
1434 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1436 unsigned i;
1437 tree t;
1438 /* Lengths have already been compared above. */
1439 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1440 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1441 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1442 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1443 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1444 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1445 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1446 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1447 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1450 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1452 unsigned i;
1453 tree index, value;
1454 /* Lengths have already been compared above. */
1455 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1457 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1458 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1462 if (code == OMP_CLAUSE)
1464 int i;
1466 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1467 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1468 OMP_CLAUSE_OPERAND (t2, i));
1469 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1472 #undef compare_tree_edges
1474 return true;
1477 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1478 out MAP if they are equal. */
1480 static bool
1481 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1482 tree *map)
1484 /* Assume SCC entry hashes are sorted after their cardinality. Which
1485 means we can simply take the first n-tuple of equal hashes
1486 (which is recorded as entry_len) and do n SCC entry candidate
1487 comparisons. */
1488 for (unsigned i = 0; i < pscc->entry_len; ++i)
1490 tree *mapp = map;
1491 num_scc_compare_collisions++;
1492 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1494 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1495 on the scc as all trees will be freed. */
1496 return true;
1498 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1499 the SCC prevails. */
1500 for (unsigned j = 0; j < scc->len; ++j)
1501 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1504 return false;
1507 /* QSort sort function to sort a map of two pointers after the 2nd
1508 pointer. */
1510 static int
1511 cmp_tree (const void *p1_, const void *p2_)
1513 tree *p1 = (tree *)(const_cast<void *>(p1_));
1514 tree *p2 = (tree *)(const_cast<void *>(p2_));
1515 if (p1[1] == p2[1])
1516 return 0;
1517 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1520 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1521 hash value SCC_HASH with an already recorded SCC. Return true if
1522 that was successful, otherwise return false. */
1524 static bool
1525 unify_scc (struct data_in *data_in, unsigned from,
1526 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1528 bool unified_p = false;
1529 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1530 tree_scc *scc
1531 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1532 scc->next = NULL;
1533 scc->hash = scc_hash;
1534 scc->len = len;
1535 scc->entry_len = scc_entry_len;
1536 for (unsigned i = 0; i < len; ++i)
1538 tree t = streamer_tree_cache_get_tree (cache, from + i);
1539 scc->entries[i] = t;
1540 /* Do not merge SCCs with local entities inside them. Also do
1541 not merge TRANSLATION_UNIT_DECLs. */
1542 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1543 || (VAR_OR_FUNCTION_DECL_P (t)
1544 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1545 || TREE_CODE (t) == LABEL_DECL)
1547 /* Avoid doing any work for these cases and do not worry to
1548 record the SCCs for further merging. */
1549 return false;
1553 /* Look for the list of candidate SCCs to compare against. */
1554 tree_scc **slot;
1555 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1556 if (*slot)
1558 /* Try unifying against each candidate. */
1559 num_scc_compares++;
1561 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1562 outside of the scc when following tree edges. Make sure
1563 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1564 to track whether we visited the SCC member during the compare.
1565 We cannot use TREE_VISITED on the pscc members as the extended
1566 scc and pscc can overlap. */
1567 for (unsigned i = 0; i < scc->len; ++i)
1569 TREE_VISITED (scc->entries[i]) = 1;
1570 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1573 tree *map = XALLOCAVEC (tree, 2 * len);
1574 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1576 if (!compare_tree_sccs (pscc, scc, map))
1577 continue;
1579 /* Found an equal SCC. */
1580 unified_p = true;
1581 num_scc_compare_collisions--;
1582 num_sccs_merged++;
1583 total_scc_size_merged += len;
1585 #ifdef ENABLE_CHECKING
1586 for (unsigned i = 0; i < len; ++i)
1588 tree t = map[2*i+1];
1589 enum tree_code code = TREE_CODE (t);
1590 /* IDENTIFIER_NODEs should be singletons and are merged by the
1591 streamer. The others should be singletons, too, and we
1592 should not merge them in any way. */
1593 gcc_assert (code != TRANSLATION_UNIT_DECL
1594 && code != IDENTIFIER_NODE
1595 && !streamer_handle_as_builtin_p (t));
1597 #endif
1599 /* Fixup the streamer cache with the prevailing nodes according
1600 to the tree node mapping computed by compare_tree_sccs. */
1601 if (len == 1)
1602 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1603 else
1605 tree *map2 = XALLOCAVEC (tree, 2 * len);
1606 for (unsigned i = 0; i < len; ++i)
1608 map2[i*2] = (tree)(uintptr_t)(from + i);
1609 map2[i*2+1] = scc->entries[i];
1611 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1612 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1613 for (unsigned i = 0; i < len; ++i)
1614 streamer_tree_cache_replace_tree (cache, map[2*i],
1615 (uintptr_t)map2[2*i]);
1618 /* Free the tree nodes from the read SCC. */
1619 data_in->location_cache.revert_location_cache ();
1620 for (unsigned i = 0; i < len; ++i)
1622 enum tree_code code;
1623 if (TYPE_P (scc->entries[i]))
1624 num_merged_types++;
1625 code = TREE_CODE (scc->entries[i]);
1626 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1627 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1628 ggc_free (scc->entries[i]);
1631 break;
1634 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1635 if (!unified_p)
1636 for (unsigned i = 0; i < scc->len; ++i)
1637 TREE_VISITED (scc->entries[i]) = 0;
1640 /* If we didn't unify it to any candidate duplicate the relevant
1641 pieces to permanent storage and link it into the chain. */
1642 if (!unified_p)
1644 tree_scc *pscc
1645 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1646 memcpy (pscc, scc, sizeof (tree_scc));
1647 pscc->next = (*slot);
1648 *slot = pscc;
1650 return unified_p;
1654 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1655 RESOLUTIONS is the set of symbols picked by the linker (read from the
1656 resolution file when the linker plugin is being used). */
1658 static void
1659 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1660 vec<ld_plugin_symbol_resolution_t> resolutions)
1662 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1663 const int decl_offset = sizeof (struct lto_decl_header);
1664 const int main_offset = decl_offset + header->decl_state_size;
1665 const int string_offset = main_offset + header->main_size;
1666 struct data_in *data_in;
1667 unsigned int i;
1668 const uint32_t *data_ptr, *data_end;
1669 uint32_t num_decl_states;
1671 lto_input_block ib_main ((const char *) data + main_offset,
1672 header->main_size, decl_data->mode_table);
1674 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1675 header->string_size, resolutions);
1677 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1678 internal types that should not be merged. */
1680 /* Read the global declarations and types. */
1681 while (ib_main.p < ib_main.len)
1683 tree t;
1684 unsigned from = data_in->reader_cache->nodes.length ();
1685 /* Read and uniquify SCCs as in the input stream. */
1686 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1687 if (tag == LTO_tree_scc)
1689 unsigned len_;
1690 unsigned scc_entry_len;
1691 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1692 &scc_entry_len);
1693 unsigned len = data_in->reader_cache->nodes.length () - from;
1694 gcc_assert (len == len_);
1696 total_scc_size += len;
1697 num_sccs_read++;
1699 /* We have the special case of size-1 SCCs that are pre-merged
1700 by means of identifier and string sharing for example.
1701 ??? Maybe we should avoid streaming those as SCCs. */
1702 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1703 from);
1704 if (len == 1
1705 && (TREE_CODE (first) == IDENTIFIER_NODE
1706 || TREE_CODE (first) == INTEGER_CST
1707 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1708 || streamer_handle_as_builtin_p (first)))
1709 continue;
1711 /* Try to unify the SCC with already existing ones. */
1712 if (!flag_ltrans
1713 && unify_scc (data_in, from,
1714 len, scc_entry_len, scc_hash))
1715 continue;
1717 /* Tree merging failed, mark entries in location cache as
1718 permanent. */
1719 data_in->location_cache.accept_location_cache ();
1721 bool seen_type = false;
1722 for (unsigned i = 0; i < len; ++i)
1724 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1725 from + i);
1726 /* Reconstruct the type variant and pointer-to/reference-to
1727 chains. */
1728 if (TYPE_P (t))
1730 seen_type = true;
1731 num_prevailing_types++;
1732 lto_fixup_prevailing_type (t);
1734 /* Compute the canonical type of all types.
1735 ??? Should be able to assert that !TYPE_CANONICAL. */
1736 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1738 gimple_register_canonical_type (t);
1739 if (odr_type_p (t))
1740 register_odr_type (t);
1742 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1743 type which is also member of this SCC. */
1744 if (TREE_CODE (t) == INTEGER_CST
1745 && !TREE_OVERFLOW (t))
1746 cache_integer_cst (t);
1747 /* Register TYPE_DECLs with the debuginfo machinery. */
1748 if (!flag_wpa
1749 && TREE_CODE (t) == TYPE_DECL)
1751 /* Dwarf2out needs location information.
1752 TODO: Moving this out of the streamer loop may noticealy
1753 improve ltrans linemap memory use. */
1754 data_in->location_cache.apply_location_cache ();
1755 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1757 if (!flag_ltrans)
1759 /* Register variables and functions with the
1760 symbol table. */
1761 if (TREE_CODE (t) == VAR_DECL)
1762 lto_register_var_decl_in_symtab (data_in, t, from + i);
1763 else if (TREE_CODE (t) == FUNCTION_DECL
1764 && !DECL_BUILT_IN (t))
1765 lto_register_function_decl_in_symtab (data_in, t, from + i);
1766 /* Scan the tree for references to global functions or
1767 variables and record those for later fixup. */
1768 if (mentions_vars_p (t))
1769 vec_safe_push (tree_with_vars, t);
1772 if (seen_type)
1773 num_type_scc_trees += len;
1775 else
1777 /* Pickle stray references. */
1778 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1779 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1782 data_in->location_cache.apply_location_cache ();
1784 /* Read in lto_in_decl_state objects. */
1785 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1786 data_end =
1787 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1788 num_decl_states = *data_ptr++;
1790 gcc_assert (num_decl_states > 0);
1791 decl_data->global_decl_state = lto_new_in_decl_state ();
1792 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1793 decl_data->global_decl_state);
1795 /* Read in per-function decl states and enter them in hash table. */
1796 decl_data->function_decl_states =
1797 hash_table<decl_state_hasher>::create_ggc (37);
1799 for (i = 1; i < num_decl_states; i++)
1801 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1803 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1804 lto_in_decl_state **slot
1805 = decl_data->function_decl_states->find_slot (state, INSERT);
1806 gcc_assert (*slot == NULL);
1807 *slot = state;
1810 if (data_ptr != data_end)
1811 internal_error ("bytecode stream: garbage at the end of symbols section");
1813 /* Set the current decl state to be the global state. */
1814 decl_data->current_decl_state = decl_data->global_decl_state;
1816 lto_data_in_delete (data_in);
1819 /* Custom version of strtoll, which is not portable. */
1821 static int64_t
1822 lto_parse_hex (const char *p)
1824 int64_t ret = 0;
1826 for (; *p != '\0'; ++p)
1828 char c = *p;
1829 unsigned char part;
1830 ret <<= 4;
1831 if (c >= '0' && c <= '9')
1832 part = c - '0';
1833 else if (c >= 'a' && c <= 'f')
1834 part = c - 'a' + 10;
1835 else if (c >= 'A' && c <= 'F')
1836 part = c - 'A' + 10;
1837 else
1838 internal_error ("could not parse hex number");
1839 ret |= part;
1842 return ret;
1845 /* Read resolution for file named FILE_NAME. The resolution is read from
1846 RESOLUTION. */
1848 static void
1849 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1851 /* We require that objects in the resolution file are in the same
1852 order as the lto1 command line. */
1853 unsigned int name_len;
1854 char *obj_name;
1855 unsigned int num_symbols;
1856 unsigned int i;
1857 struct lto_file_decl_data *file_data;
1858 splay_tree_node nd = NULL;
1860 if (!resolution)
1861 return;
1863 name_len = strlen (file->filename);
1864 obj_name = XNEWVEC (char, name_len + 1);
1865 fscanf (resolution, " "); /* Read white space. */
1867 fread (obj_name, sizeof (char), name_len, resolution);
1868 obj_name[name_len] = '\0';
1869 if (filename_cmp (obj_name, file->filename) != 0)
1870 internal_error ("unexpected file name %s in linker resolution file. "
1871 "Expected %s", obj_name, file->filename);
1872 if (file->offset != 0)
1874 int t;
1875 char offset_p[17];
1876 int64_t offset;
1877 t = fscanf (resolution, "@0x%16s", offset_p);
1878 if (t != 1)
1879 internal_error ("could not parse file offset");
1880 offset = lto_parse_hex (offset_p);
1881 if (offset != file->offset)
1882 internal_error ("unexpected offset");
1885 free (obj_name);
1887 fscanf (resolution, "%u", &num_symbols);
1889 for (i = 0; i < num_symbols; i++)
1891 int t;
1892 unsigned index;
1893 unsigned HOST_WIDE_INT id;
1894 char r_str[27];
1895 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1896 unsigned int j;
1897 unsigned int lto_resolution_str_len =
1898 sizeof (lto_resolution_str) / sizeof (char *);
1899 res_pair rp;
1901 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1902 &index, &id, r_str);
1903 if (t != 3)
1904 internal_error ("invalid line in the resolution file");
1906 for (j = 0; j < lto_resolution_str_len; j++)
1908 if (strcmp (lto_resolution_str[j], r_str) == 0)
1910 r = (enum ld_plugin_symbol_resolution) j;
1911 break;
1914 if (j == lto_resolution_str_len)
1915 internal_error ("invalid resolution in the resolution file");
1917 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1919 nd = lto_splay_tree_lookup (file_ids, id);
1920 if (nd == NULL)
1921 internal_error ("resolution sub id %wx not in object file", id);
1924 file_data = (struct lto_file_decl_data *)nd->value;
1925 /* The indexes are very sparse. To save memory save them in a compact
1926 format that is only unpacked later when the subfile is processed. */
1927 rp.res = r;
1928 rp.index = index;
1929 file_data->respairs.safe_push (rp);
1930 if (file_data->max_index < index)
1931 file_data->max_index = index;
1935 /* List of file_decl_datas */
1936 struct file_data_list
1938 struct lto_file_decl_data *first, *last;
1941 /* Is the name for a id'ed LTO section? */
1943 static int
1944 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1946 const char *s;
1948 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
1949 return 0;
1950 s = strrchr (name, '.');
1951 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1954 /* Create file_data of each sub file id */
1956 static int
1957 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1958 struct file_data_list *list)
1960 struct lto_section_slot s_slot, *new_slot;
1961 unsigned HOST_WIDE_INT id;
1962 splay_tree_node nd;
1963 void **hash_slot;
1964 char *new_name;
1965 struct lto_file_decl_data *file_data;
1967 if (!lto_section_with_id (ls->name, &id))
1968 return 1;
1970 /* Find hash table of sub module id */
1971 nd = lto_splay_tree_lookup (file_ids, id);
1972 if (nd != NULL)
1974 file_data = (struct lto_file_decl_data *)nd->value;
1976 else
1978 file_data = ggc_alloc<lto_file_decl_data> ();
1979 memset(file_data, 0, sizeof (struct lto_file_decl_data));
1980 file_data->id = id;
1981 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1982 lto_splay_tree_insert (file_ids, id, file_data);
1984 /* Maintain list in linker order */
1985 if (!list->first)
1986 list->first = file_data;
1987 if (list->last)
1988 list->last->next = file_data;
1989 list->last = file_data;
1992 /* Copy section into sub module hash table */
1993 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
1994 s_slot.name = new_name;
1995 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
1996 gcc_assert (*hash_slot == NULL);
1998 new_slot = XDUP (struct lto_section_slot, ls);
1999 new_slot->name = new_name;
2000 *hash_slot = new_slot;
2001 return 1;
2004 /* Read declarations and other initializations for a FILE_DATA. */
2006 static void
2007 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2009 const char *data;
2010 size_t len;
2011 vec<ld_plugin_symbol_resolution_t>
2012 resolutions = vNULL;
2013 int i;
2014 res_pair *rp;
2016 /* Create vector for fast access of resolution. We do this lazily
2017 to save memory. */
2018 resolutions.safe_grow_cleared (file_data->max_index + 1);
2019 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2020 resolutions[rp->index] = rp->res;
2021 file_data->respairs.release ();
2023 file_data->renaming_hash_table = lto_create_renaming_table ();
2024 file_data->file_name = file->filename;
2025 #ifdef ACCEL_COMPILER
2026 lto_input_mode_table (file_data);
2027 #else
2028 file_data->mode_table = lto_mode_identity_table;
2029 #endif
2030 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2031 if (data == NULL)
2033 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2034 return;
2036 /* Frees resolutions */
2037 lto_read_decls (file_data, data, resolutions);
2038 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2041 /* Finalize FILE_DATA in FILE and increase COUNT. */
2043 static int
2044 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2045 int *count)
2047 lto_file_finalize (file_data, file);
2048 if (symtab->dump_file)
2049 fprintf (symtab->dump_file,
2050 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2051 file_data->file_name, file_data->id);
2052 (*count)++;
2053 return 0;
2056 /* Generate a TREE representation for all types and external decls
2057 entities in FILE.
2059 Read all of the globals out of the file. Then read the cgraph
2060 and process the .o index into the cgraph nodes so that it can open
2061 the .o file to load the functions and ipa information. */
2063 static struct lto_file_decl_data *
2064 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2066 struct lto_file_decl_data *file_data = NULL;
2067 splay_tree file_ids;
2068 htab_t section_hash_table;
2069 struct lto_section_slot *section;
2070 struct file_data_list file_list;
2071 struct lto_section_list section_list;
2073 memset (&section_list, 0, sizeof (struct lto_section_list));
2074 section_hash_table = lto_obj_build_section_table (file, &section_list);
2076 /* Find all sub modules in the object and put their sections into new hash
2077 tables in a splay tree. */
2078 file_ids = lto_splay_tree_new ();
2079 memset (&file_list, 0, sizeof (struct file_data_list));
2080 for (section = section_list.first; section != NULL; section = section->next)
2081 create_subid_section_table (section, file_ids, &file_list);
2083 /* Add resolutions to file ids */
2084 lto_resolution_read (file_ids, resolution_file, file);
2086 /* Finalize each lto file for each submodule in the merged object */
2087 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2088 lto_create_files_from_ids (file, file_data, count);
2090 splay_tree_delete (file_ids);
2091 htab_delete (section_hash_table);
2093 return file_list.first;
2096 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2097 #define LTO_MMAP_IO 1
2098 #endif
2100 #if LTO_MMAP_IO
2101 /* Page size of machine is used for mmap and munmap calls. */
2102 static size_t page_mask;
2103 #endif
2105 /* Get the section data of length LEN from FILENAME starting at
2106 OFFSET. The data segment must be freed by the caller when the
2107 caller is finished. Returns NULL if all was not well. */
2109 static char *
2110 lto_read_section_data (struct lto_file_decl_data *file_data,
2111 intptr_t offset, size_t len)
2113 char *result;
2114 static int fd = -1;
2115 static char *fd_name;
2116 #if LTO_MMAP_IO
2117 intptr_t computed_len;
2118 intptr_t computed_offset;
2119 intptr_t diff;
2120 #endif
2122 /* Keep a single-entry file-descriptor cache. The last file we
2123 touched will get closed at exit.
2124 ??? Eventually we want to add a more sophisticated larger cache
2125 or rather fix function body streaming to not stream them in
2126 practically random order. */
2127 if (fd != -1
2128 && filename_cmp (fd_name, file_data->file_name) != 0)
2130 free (fd_name);
2131 close (fd);
2132 fd = -1;
2134 if (fd == -1)
2136 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2137 if (fd == -1)
2139 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2140 return NULL;
2142 fd_name = xstrdup (file_data->file_name);
2145 #if LTO_MMAP_IO
2146 if (!page_mask)
2148 size_t page_size = sysconf (_SC_PAGE_SIZE);
2149 page_mask = ~(page_size - 1);
2152 computed_offset = offset & page_mask;
2153 diff = offset - computed_offset;
2154 computed_len = len + diff;
2156 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2157 fd, computed_offset);
2158 if (result == MAP_FAILED)
2160 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2161 return NULL;
2164 return result + diff;
2165 #else
2166 result = (char *) xmalloc (len);
2167 if (lseek (fd, offset, SEEK_SET) != offset
2168 || read (fd, result, len) != (ssize_t) len)
2170 free (result);
2171 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2172 result = NULL;
2174 #ifdef __MINGW32__
2175 /* Native windows doesn't supports delayed unlink on opened file. So
2176 we close file here again. This produces higher I/O load, but at least
2177 it prevents to have dangling file handles preventing unlink. */
2178 free (fd_name);
2179 fd_name = NULL;
2180 close (fd);
2181 fd = -1;
2182 #endif
2183 return result;
2184 #endif
2188 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2189 NAME will be NULL unless the section type is for a function
2190 body. */
2192 static const char *
2193 get_section_data (struct lto_file_decl_data *file_data,
2194 enum lto_section_type section_type,
2195 const char *name,
2196 size_t *len)
2198 htab_t section_hash_table = file_data->section_hash_table;
2199 struct lto_section_slot *f_slot;
2200 struct lto_section_slot s_slot;
2201 const char *section_name = lto_get_section_name (section_type, name, file_data);
2202 char *data = NULL;
2204 *len = 0;
2205 s_slot.name = section_name;
2206 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2207 if (f_slot)
2209 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2210 *len = f_slot->len;
2213 free (CONST_CAST (char *, section_name));
2214 return data;
2218 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2219 starts at OFFSET and has LEN bytes. */
2221 static void
2222 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2223 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2224 const char *name ATTRIBUTE_UNUSED,
2225 const char *offset, size_t len ATTRIBUTE_UNUSED)
2227 #if LTO_MMAP_IO
2228 intptr_t computed_len;
2229 intptr_t computed_offset;
2230 intptr_t diff;
2231 #endif
2233 #if LTO_MMAP_IO
2234 computed_offset = ((intptr_t) offset) & page_mask;
2235 diff = (intptr_t) offset - computed_offset;
2236 computed_len = len + diff;
2238 munmap ((caddr_t) computed_offset, computed_len);
2239 #else
2240 free (CONST_CAST(char *, offset));
2241 #endif
2244 static lto_file *current_lto_file;
2246 /* Helper for qsort; compare partitions and return one with smaller size.
2247 We sort from greatest to smallest so parallel build doesn't stale on the
2248 longest compilation being executed too late. */
2250 static int
2251 cmp_partitions_size (const void *a, const void *b)
2253 const struct ltrans_partition_def *pa
2254 = *(struct ltrans_partition_def *const *)a;
2255 const struct ltrans_partition_def *pb
2256 = *(struct ltrans_partition_def *const *)b;
2257 return pb->insns - pa->insns;
2260 /* Helper for qsort; compare partitions and return one with smaller order. */
2262 static int
2263 cmp_partitions_order (const void *a, const void *b)
2265 const struct ltrans_partition_def *pa
2266 = *(struct ltrans_partition_def *const *)a;
2267 const struct ltrans_partition_def *pb
2268 = *(struct ltrans_partition_def *const *)b;
2269 int ordera = -1, orderb = -1;
2271 if (lto_symtab_encoder_size (pa->encoder))
2272 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2273 if (lto_symtab_encoder_size (pb->encoder))
2274 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2275 return orderb - ordera;
2278 /* Actually stream out ENCODER into TEMP_FILENAME. */
2280 static void
2281 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2283 lto_file *file = lto_obj_file_open (temp_filename, true);
2284 if (!file)
2285 fatal_error (input_location, "lto_obj_file_open() failed");
2286 lto_set_current_out_file (file);
2288 ipa_write_optimization_summaries (encoder);
2290 lto_set_current_out_file (NULL);
2291 lto_obj_file_close (file);
2292 free (file);
2295 /* Wait for forked process and signal errors. */
2296 #ifdef HAVE_WORKING_FORK
2297 static void
2298 wait_for_child ()
2300 int status;
2303 #ifndef WCONTINUED
2304 #define WCONTINUED 0
2305 #endif
2306 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2307 if (w == -1)
2308 fatal_error (input_location, "waitpid failed");
2310 if (WIFEXITED (status) && WEXITSTATUS (status))
2311 fatal_error (input_location, "streaming subprocess failed");
2312 else if (WIFSIGNALED (status))
2313 fatal_error (input_location,
2314 "streaming subprocess was killed by signal");
2316 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2318 #endif
2320 /* Stream out ENCODER into TEMP_FILENAME
2321 Fork if that seems to help. */
2323 static void
2324 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2325 bool ARG_UNUSED (last))
2327 #ifdef HAVE_WORKING_FORK
2328 static int nruns;
2330 if (lto_parallelism <= 1)
2332 do_stream_out (temp_filename, encoder);
2333 return;
2336 /* Do not run more than LTO_PARALLELISM streamings
2337 FIXME: we ignore limits on jobserver. */
2338 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2340 wait_for_child ();
2341 nruns --;
2343 /* If this is not the last parallel partition, execute new
2344 streaming process. */
2345 if (!last)
2347 pid_t cpid = fork ();
2349 if (!cpid)
2351 setproctitle ("lto1-wpa-streaming");
2352 do_stream_out (temp_filename, encoder);
2353 exit (0);
2355 /* Fork failed; lets do the job ourseleves. */
2356 else if (cpid == -1)
2357 do_stream_out (temp_filename, encoder);
2358 else
2359 nruns++;
2361 /* Last partition; stream it and wait for all children to die. */
2362 else
2364 int i;
2365 do_stream_out (temp_filename, encoder);
2366 for (i = 0; i < nruns; i++)
2367 wait_for_child ();
2369 asm_nodes_output = true;
2370 #else
2371 do_stream_out (temp_filename, encoder);
2372 #endif
2375 /* Write all output files in WPA mode and the file with the list of
2376 LTRANS units. */
2378 static void
2379 lto_wpa_write_files (void)
2381 unsigned i, n_sets;
2382 ltrans_partition part;
2383 FILE *ltrans_output_list_stream;
2384 char *temp_filename;
2385 vec <char *>temp_filenames = vNULL;
2386 size_t blen;
2388 /* Open the LTRANS output list. */
2389 if (!ltrans_output_list)
2390 fatal_error (input_location, "no LTRANS output list filename provided");
2392 timevar_push (TV_WHOPR_WPA);
2394 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2395 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2397 timevar_pop (TV_WHOPR_WPA);
2399 timevar_push (TV_WHOPR_WPA_IO);
2401 /* Generate a prefix for the LTRANS unit files. */
2402 blen = strlen (ltrans_output_list);
2403 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2404 strcpy (temp_filename, ltrans_output_list);
2405 if (blen > sizeof (".out")
2406 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2407 ".out") == 0)
2408 temp_filename[blen - sizeof (".out") + 1] = '\0';
2409 blen = strlen (temp_filename);
2411 n_sets = ltrans_partitions.length ();
2413 /* Sort partitions by size so small ones are compiled last.
2414 FIXME: Even when not reordering we may want to output one list for parallel make
2415 and other for final link command. */
2417 if (!flag_profile_reorder_functions || !flag_profile_use)
2418 ltrans_partitions.qsort (flag_toplevel_reorder
2419 ? cmp_partitions_size
2420 : cmp_partitions_order);
2422 for (i = 0; i < n_sets; i++)
2424 ltrans_partition part = ltrans_partitions[i];
2426 /* Write all the nodes in SET. */
2427 sprintf (temp_filename + blen, "%u.o", i);
2429 if (!quiet_flag)
2430 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2431 if (symtab->dump_file)
2433 lto_symtab_encoder_iterator lsei;
2435 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2436 part->name, temp_filename, part->insns);
2437 fprintf (symtab->dump_file, " Symbols in partition: ");
2438 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2439 lsei_next_in_partition (&lsei))
2441 symtab_node *node = lsei_node (lsei);
2442 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2444 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2445 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2446 lsei_next (&lsei))
2448 symtab_node *node = lsei_node (lsei);
2449 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2451 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2452 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2453 if (cnode
2454 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2455 fprintf (symtab->dump_file, "(body included)");
2456 else
2458 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2459 if (vnode
2460 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2461 fprintf (symtab->dump_file, "(initializer included)");
2465 fprintf (symtab->dump_file, "\n");
2467 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2469 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2471 part->encoder = NULL;
2473 temp_filenames.safe_push (xstrdup (temp_filename));
2475 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2476 if (ltrans_output_list_stream == NULL)
2477 fatal_error (input_location,
2478 "opening LTRANS output list %s: %m", ltrans_output_list);
2479 for (i = 0; i < n_sets; i++)
2481 unsigned int len = strlen (temp_filenames[i]);
2482 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2483 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2484 fatal_error (input_location, "writing to LTRANS output list %s: %m",
2485 ltrans_output_list);
2486 free (temp_filenames[i]);
2488 temp_filenames.release();
2490 lto_stats.num_output_files += n_sets;
2492 /* Close the LTRANS output list. */
2493 if (fclose (ltrans_output_list_stream))
2494 fatal_error (input_location,
2495 "closing LTRANS output list %s: %m", ltrans_output_list);
2497 free_ltrans_partitions();
2498 free (temp_filename);
2500 timevar_pop (TV_WHOPR_WPA_IO);
2504 /* If TT is a variable or function decl replace it with its
2505 prevailing variant. */
2506 #define LTO_SET_PREVAIL(tt) \
2507 do {\
2508 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2509 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2511 tt = lto_symtab_prevailing_decl (tt); \
2512 fixed = true; \
2514 } while (0)
2516 /* Ensure that TT isn't a replacable var of function decl. */
2517 #define LTO_NO_PREVAIL(tt) \
2518 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2520 /* Given a tree T replace all fields referring to variables or functions
2521 with their prevailing variant. */
2522 static void
2523 lto_fixup_prevailing_decls (tree t)
2525 enum tree_code code = TREE_CODE (t);
2526 bool fixed = false;
2528 gcc_checking_assert (code != TREE_BINFO);
2529 LTO_NO_PREVAIL (TREE_TYPE (t));
2530 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2531 LTO_NO_PREVAIL (TREE_CHAIN (t));
2532 if (DECL_P (t))
2534 LTO_NO_PREVAIL (DECL_NAME (t));
2535 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2536 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2538 LTO_SET_PREVAIL (DECL_SIZE (t));
2539 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2540 LTO_SET_PREVAIL (DECL_INITIAL (t));
2541 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2542 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2544 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2546 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2548 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2550 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2552 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2554 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2555 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2556 LTO_NO_PREVAIL (DECL_VINDEX (t));
2558 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2560 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2561 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2562 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2563 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2564 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2567 else if (TYPE_P (t))
2569 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2570 LTO_SET_PREVAIL (TYPE_SIZE (t));
2571 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2572 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2573 LTO_NO_PREVAIL (TYPE_NAME (t));
2575 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2576 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2577 LTO_NO_PREVAIL (t->type_non_common.binfo);
2579 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2581 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2582 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2583 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2585 else if (EXPR_P (t))
2587 int i;
2588 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2589 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2591 else if (TREE_CODE (t) == CONSTRUCTOR)
2593 unsigned i;
2594 tree val;
2595 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2596 LTO_SET_PREVAIL (val);
2598 else
2600 switch (code)
2602 case TREE_LIST:
2603 LTO_SET_PREVAIL (TREE_VALUE (t));
2604 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2605 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2606 break;
2607 default:
2608 gcc_unreachable ();
2611 /* If we fixed nothing, then we missed something seen by
2612 mentions_vars_p. */
2613 gcc_checking_assert (fixed);
2615 #undef LTO_SET_PREVAIL
2616 #undef LTO_NO_PREVAIL
2618 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2619 replaces var and function decls with the corresponding prevailing def. */
2621 static void
2622 lto_fixup_state (struct lto_in_decl_state *state)
2624 unsigned i, si;
2626 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2627 we still need to walk from all DECLs to find the reachable
2628 FUNCTION_DECLs and VAR_DECLs. */
2629 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2631 vec<tree, va_gc> *trees = state->streams[si];
2632 for (i = 0; i < vec_safe_length (trees); i++)
2634 tree t = (*trees)[i];
2635 #ifdef ENABLE_CHECKING
2636 if (TYPE_P (t))
2637 verify_type (t);
2638 #endif
2639 if (VAR_OR_FUNCTION_DECL_P (t)
2640 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2641 (*trees)[i] = lto_symtab_prevailing_decl (t);
2646 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2647 prevailing one. */
2649 static void
2650 lto_fixup_decls (struct lto_file_decl_data **files)
2652 unsigned int i;
2653 tree t;
2655 if (tree_with_vars)
2656 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2657 lto_fixup_prevailing_decls (t);
2659 for (i = 0; files[i]; i++)
2661 struct lto_file_decl_data *file = files[i];
2662 struct lto_in_decl_state *state = file->global_decl_state;
2663 lto_fixup_state (state);
2665 hash_table<decl_state_hasher>::iterator iter;
2666 lto_in_decl_state *elt;
2667 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2668 lto_in_decl_state *, iter)
2669 lto_fixup_state (elt);
2673 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2675 /* Turn file datas for sub files into a single array, so that they look
2676 like separate files for further passes. */
2678 static void
2679 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2681 struct lto_file_decl_data *n, *next;
2682 int i, k;
2684 lto_stats.num_input_files = count;
2685 all_file_decl_data
2686 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2687 /* Set the hooks so that all of the ipa passes can read in their data. */
2688 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2689 for (i = 0, k = 0; i < last_file_ix; i++)
2691 for (n = orig[i]; n != NULL; n = next)
2693 all_file_decl_data[k++] = n;
2694 next = n->next;
2695 n->next = NULL;
2698 all_file_decl_data[k] = NULL;
2699 gcc_assert (k == count);
2702 /* Input file data before flattening (i.e. splitting them to subfiles to support
2703 incremental linking. */
2704 static int real_file_count;
2705 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2707 static void print_lto_report_1 (void);
2709 /* Read all the symbols from the input files FNAMES. NFILES is the
2710 number of files requested in the command line. Instantiate a
2711 global call graph by aggregating all the sub-graphs found in each
2712 file. */
2714 static void
2715 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2717 unsigned int i, last_file_ix;
2718 FILE *resolution;
2719 int count = 0;
2720 struct lto_file_decl_data **decl_data;
2721 symtab_node *snode;
2723 symtab->initialize ();
2725 timevar_push (TV_IPA_LTO_DECL_IN);
2727 #ifdef ACCEL_COMPILER
2728 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2729 lto_stream_offload_p = true;
2730 #endif
2732 real_file_decl_data
2733 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2734 real_file_count = nfiles;
2736 /* Read the resolution file. */
2737 resolution = NULL;
2738 if (resolution_file_name)
2740 int t;
2741 unsigned num_objects;
2743 resolution = fopen (resolution_file_name, "r");
2744 if (resolution == NULL)
2745 fatal_error (input_location,
2746 "could not open symbol resolution file: %m");
2748 t = fscanf (resolution, "%u", &num_objects);
2749 gcc_assert (t == 1);
2751 /* True, since the plugin splits the archives. */
2752 gcc_assert (num_objects == nfiles);
2754 symtab->state = LTO_STREAMING;
2756 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2757 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2758 gimple_canonical_type_eq, NULL);
2759 gcc_obstack_init (&tree_scc_hash_obstack);
2760 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2762 /* Register the common node types with the canonical type machinery so
2763 we properly share alias-sets across languages and TUs. Do not
2764 expose the common nodes as type merge target - those that should be
2765 are already exposed so by pre-loading the LTO streamer caches.
2766 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2767 for (i = 0; i < itk_none; ++i)
2768 lto_register_canonical_types (integer_types[i], true);
2769 for (i = 0; i < stk_type_kind_last; ++i)
2770 lto_register_canonical_types (sizetype_tab[i], true);
2771 for (i = 0; i < TI_MAX; ++i)
2772 lto_register_canonical_types (global_trees[i], true);
2773 for (i = 0; i < itk_none; ++i)
2774 lto_register_canonical_types (integer_types[i], false);
2775 for (i = 0; i < stk_type_kind_last; ++i)
2776 lto_register_canonical_types (sizetype_tab[i], false);
2777 for (i = 0; i < TI_MAX; ++i)
2778 lto_register_canonical_types (global_trees[i], false);
2780 if (!quiet_flag)
2781 fprintf (stderr, "Reading object files:");
2783 /* Read all of the object files specified on the command line. */
2784 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2786 struct lto_file_decl_data *file_data = NULL;
2787 if (!quiet_flag)
2789 fprintf (stderr, " %s", fnames[i]);
2790 fflush (stderr);
2793 current_lto_file = lto_obj_file_open (fnames[i], false);
2794 if (!current_lto_file)
2795 break;
2797 file_data = lto_file_read (current_lto_file, resolution, &count);
2798 if (!file_data)
2800 lto_obj_file_close (current_lto_file);
2801 free (current_lto_file);
2802 current_lto_file = NULL;
2803 break;
2806 decl_data[last_file_ix++] = file_data;
2808 lto_obj_file_close (current_lto_file);
2809 free (current_lto_file);
2810 current_lto_file = NULL;
2813 lto_flatten_files (decl_data, count, last_file_ix);
2814 lto_stats.num_input_files = count;
2815 ggc_free(decl_data);
2816 real_file_decl_data = NULL;
2818 if (resolution_file_name)
2819 fclose (resolution);
2821 /* Show the LTO report before launching LTRANS. */
2822 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2823 print_lto_report_1 ();
2825 /* Free gimple type merging datastructures. */
2826 delete tree_scc_hash;
2827 tree_scc_hash = NULL;
2828 obstack_free (&tree_scc_hash_obstack, NULL);
2829 htab_delete (gimple_canonical_types);
2830 gimple_canonical_types = NULL;
2831 delete canonical_type_hash_cache;
2832 canonical_type_hash_cache = NULL;
2834 /* At this stage we know that majority of GGC memory is reachable.
2835 Growing the limits prevents unnecesary invocation of GGC. */
2836 ggc_grow ();
2837 ggc_collect ();
2839 /* Set the hooks so that all of the ipa passes can read in their data. */
2840 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2842 timevar_pop (TV_IPA_LTO_DECL_IN);
2844 if (!quiet_flag)
2845 fprintf (stderr, "\nReading the callgraph\n");
2847 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2848 /* Read the symtab. */
2849 input_symtab ();
2851 input_offload_tables ();
2853 /* Store resolutions into the symbol table. */
2855 ld_plugin_symbol_resolution_t *res;
2856 FOR_EACH_SYMBOL (snode)
2857 if (snode->real_symbol_p ()
2858 && snode->lto_file_data
2859 && snode->lto_file_data->resolution_map
2860 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
2861 snode->resolution = *res;
2862 for (i = 0; all_file_decl_data[i]; i++)
2863 if (all_file_decl_data[i]->resolution_map)
2865 delete all_file_decl_data[i]->resolution_map;
2866 all_file_decl_data[i]->resolution_map = NULL;
2869 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2871 if (!quiet_flag)
2872 fprintf (stderr, "Merging declarations\n");
2874 timevar_push (TV_IPA_LTO_DECL_MERGE);
2875 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2876 need to care about resolving symbols again, we only need to replace
2877 duplicated declarations read from the callgraph and from function
2878 sections. */
2879 if (!flag_ltrans)
2881 lto_symtab_merge_decls ();
2883 /* If there were errors during symbol merging bail out, we have no
2884 good way to recover here. */
2885 if (seen_error ())
2886 fatal_error (input_location,
2887 "errors during merging of translation units");
2889 /* Fixup all decls. */
2890 lto_fixup_decls (all_file_decl_data);
2892 if (tree_with_vars)
2893 ggc_free (tree_with_vars);
2894 tree_with_vars = NULL;
2895 ggc_collect ();
2897 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2898 /* Each pass will set the appropriate timer. */
2900 if (!quiet_flag)
2901 fprintf (stderr, "Reading summaries\n");
2903 /* Read the IPA summary data. */
2904 if (flag_ltrans)
2905 ipa_read_optimization_summaries ();
2906 else
2907 ipa_read_summaries ();
2909 for (i = 0; all_file_decl_data[i]; i++)
2911 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2912 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2913 all_file_decl_data[i]->symtab_node_encoder = NULL;
2914 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
2915 all_file_decl_data[i]->global_decl_state = NULL;
2916 all_file_decl_data[i]->current_decl_state = NULL;
2919 /* Finally merge the cgraph according to the decl merging decisions. */
2920 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2921 if (symtab->dump_file)
2923 fprintf (symtab->dump_file, "Before merging:\n");
2924 symtab_node::dump_table (symtab->dump_file);
2926 if (!flag_ltrans)
2928 lto_symtab_merge_symbols ();
2929 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2930 we are still having duplicated comdat groups containing local statics.
2931 We could also just remove them while merging. */
2932 symtab->remove_unreachable_nodes (dump_file);
2934 ggc_collect ();
2935 symtab->state = IPA_SSA;
2936 /* FIXME: Technically all node removals happening here are useless, because
2937 WPA should not stream them. */
2938 if (flag_ltrans)
2939 symtab->remove_unreachable_nodes (dump_file);
2941 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2943 /* Indicate that the cgraph is built and ready. */
2944 symtab->function_flags_ready = true;
2946 ggc_free (all_file_decl_data);
2947 all_file_decl_data = NULL;
2951 /* Materialize all the bodies for all the nodes in the callgraph. */
2953 static void
2954 materialize_cgraph (void)
2956 struct cgraph_node *node;
2957 timevar_id_t lto_timer;
2959 if (!quiet_flag)
2960 fprintf (stderr,
2961 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2964 FOR_EACH_FUNCTION (node)
2966 if (node->lto_file_data)
2968 lto_materialize_function (node);
2969 lto_stats.num_input_cgraph_nodes++;
2974 /* Start the appropriate timer depending on the mode that we are
2975 operating in. */
2976 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2977 : (flag_ltrans) ? TV_WHOPR_LTRANS
2978 : TV_LTO;
2979 timevar_push (lto_timer);
2981 current_function_decl = NULL;
2982 set_cfun (NULL);
2984 if (!quiet_flag)
2985 fprintf (stderr, "\n");
2987 timevar_pop (lto_timer);
2991 /* Show various memory usage statistics related to LTO. */
2992 static void
2993 print_lto_report_1 (void)
2995 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
2996 fprintf (stderr, "%s statistics\n", pfx);
2998 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
2999 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3000 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3001 if (flag_wpa && tree_scc_hash)
3003 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3004 "collision ratio: %f\n", pfx,
3005 (long) tree_scc_hash->size (),
3006 (long) tree_scc_hash->elements (),
3007 tree_scc_hash->collisions ());
3008 hash_table<tree_scc_hasher>::iterator hiter;
3009 tree_scc *scc, *max_scc = NULL;
3010 unsigned max_length = 0;
3011 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3013 unsigned length = 0;
3014 tree_scc *s = scc;
3015 for (; s; s = s->next)
3016 length++;
3017 if (length > max_length)
3019 max_length = length;
3020 max_scc = scc;
3023 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3024 pfx, max_length, max_scc->len);
3025 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3026 num_scc_compares, num_scc_compare_collisions,
3027 num_scc_compare_collisions / (double) num_scc_compares);
3028 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3029 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3030 total_scc_size_merged);
3031 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3032 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3033 pfx, num_prevailing_types, num_type_scc_trees);
3034 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3035 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3036 (long) htab_size (gimple_canonical_types),
3037 (long) htab_elements (gimple_canonical_types),
3038 (long) gimple_canonical_types->searches,
3039 (long) gimple_canonical_types->collisions,
3040 htab_collisions (gimple_canonical_types));
3041 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3042 "%lu elements, %ld searches\n", pfx,
3043 num_canonical_type_hash_entries,
3044 num_canonical_type_hash_queries);
3047 print_lto_report (pfx);
3050 /* Perform whole program analysis (WPA) on the callgraph and write out the
3051 optimization plan. */
3053 static void
3054 do_whole_program_analysis (void)
3056 symtab_node *node;
3058 lto_parallelism = 1;
3060 /* TODO: jobserver communicatoin is not supported, yet. */
3061 if (!strcmp (flag_wpa, "jobserver"))
3062 lto_parallelism = -1;
3063 else
3065 lto_parallelism = atoi (flag_wpa);
3066 if (lto_parallelism <= 0)
3067 lto_parallelism = 0;
3070 timevar_start (TV_PHASE_OPT_GEN);
3072 /* Note that since we are in WPA mode, materialize_cgraph will not
3073 actually read in all the function bodies. It only materializes
3074 the decls and cgraph nodes so that analysis can be performed. */
3075 materialize_cgraph ();
3077 /* Reading in the cgraph uses different timers, start timing WPA now. */
3078 timevar_push (TV_WHOPR_WPA);
3080 if (pre_ipa_mem_report)
3082 fprintf (stderr, "Memory consumption before IPA\n");
3083 dump_memory_report (false);
3086 symtab->function_flags_ready = true;
3088 if (symtab->dump_file)
3089 symtab_node::dump_table (symtab->dump_file);
3090 bitmap_obstack_initialize (NULL);
3091 symtab->state = IPA_SSA;
3093 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3095 if (symtab->dump_file)
3097 fprintf (symtab->dump_file, "Optimized ");
3098 symtab_node::dump_table (symtab->dump_file);
3100 #ifdef ENABLE_CHECKING
3101 symtab_node::verify_symtab_nodes ();
3102 #endif
3103 bitmap_obstack_release (NULL);
3105 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3106 timevar_pop (TV_WHOPR_WPA);
3108 timevar_push (TV_WHOPR_PARTITIONING);
3109 if (flag_lto_partition == LTO_PARTITION_1TO1)
3110 lto_1_to_1_map ();
3111 else if (flag_lto_partition == LTO_PARTITION_MAX)
3112 lto_max_map ();
3113 else if (flag_lto_partition == LTO_PARTITION_ONE)
3114 lto_balanced_map (1);
3115 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3116 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3117 else
3118 gcc_unreachable ();
3120 /* Inline summaries are needed for balanced partitioning. Free them now so
3121 the memory can be used for streamer caches. */
3122 inline_free_summary ();
3124 /* AUX pointers are used by partitioning code to bookkeep number of
3125 partitions symbol is in. This is no longer needed. */
3126 FOR_EACH_SYMBOL (node)
3127 node->aux = NULL;
3129 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3131 /* Find out statics that need to be promoted
3132 to globals with hidden visibility because they are accessed from multiple
3133 partitions. */
3134 lto_promote_cross_file_statics ();
3135 timevar_pop (TV_WHOPR_PARTITIONING);
3137 timevar_stop (TV_PHASE_OPT_GEN);
3139 /* Collect a last time - in lto_wpa_write_files we may end up forking
3140 with the idea that this doesn't increase memory usage. So we
3141 absoultely do not want to collect after that. */
3142 ggc_collect ();
3144 timevar_start (TV_PHASE_STREAM_OUT);
3145 if (!quiet_flag)
3147 fprintf (stderr, "\nStreaming out");
3148 fflush (stderr);
3150 lto_wpa_write_files ();
3151 if (!quiet_flag)
3152 fprintf (stderr, "\n");
3153 timevar_stop (TV_PHASE_STREAM_OUT);
3155 if (post_ipa_mem_report)
3157 fprintf (stderr, "Memory consumption after IPA\n");
3158 dump_memory_report (false);
3161 /* Show the LTO report before launching LTRANS. */
3162 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3163 print_lto_report_1 ();
3164 if (mem_report_wpa)
3165 dump_memory_report (true);
3169 static GTY(()) tree lto_eh_personality_decl;
3171 /* Return the LTO personality function decl. */
3173 tree
3174 lto_eh_personality (void)
3176 if (!lto_eh_personality_decl)
3178 /* Use the first personality DECL for our personality if we don't
3179 support multiple ones. This ensures that we don't artificially
3180 create the need for them in a single-language program. */
3181 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3182 lto_eh_personality_decl = first_personality_decl;
3183 else
3184 lto_eh_personality_decl = lhd_gcc_personality ();
3187 return lto_eh_personality_decl;
3190 /* Set the process name based on the LTO mode. */
3192 static void
3193 lto_process_name (void)
3195 if (flag_lto)
3196 setproctitle ("lto1-lto");
3197 if (flag_wpa)
3198 setproctitle ("lto1-wpa");
3199 if (flag_ltrans)
3200 setproctitle ("lto1-ltrans");
3204 /* Initialize the LTO front end. */
3206 static void
3207 lto_init (void)
3209 lto_process_name ();
3210 lto_streamer_hooks_init ();
3211 lto_reader_init ();
3212 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3213 memset (&lto_stats, 0, sizeof (lto_stats));
3214 bitmap_obstack_initialize (NULL);
3215 gimple_register_cfg_hooks ();
3216 #ifndef ACCEL_COMPILER
3217 unsigned char *table
3218 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3219 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3220 table[m] = m;
3221 lto_mode_identity_table = table;
3222 #endif
3226 /* Main entry point for the GIMPLE front end. This front end has
3227 three main personalities:
3229 - LTO (-flto). All the object files on the command line are
3230 loaded in memory and processed as a single translation unit.
3231 This is the traditional link-time optimization behavior.
3233 - WPA (-fwpa). Only the callgraph and summary information for
3234 files in the command file are loaded. A single callgraph
3235 (without function bodies) is instantiated for the whole set of
3236 files. IPA passes are only allowed to analyze the call graph
3237 and make transformation decisions. The callgraph is
3238 partitioned, each partition is written to a new object file
3239 together with the transformation decisions.
3241 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3242 summary files from running again. Since WPA computed summary
3243 information and decided what transformations to apply, LTRANS
3244 simply applies them. */
3246 void
3247 lto_main (void)
3249 /* LTO is called as a front end, even though it is not a front end.
3250 Because it is called as a front end, TV_PHASE_PARSING and
3251 TV_PARSE_GLOBAL are active, and we need to turn them off while
3252 doing LTO. Later we turn them back on so they are active up in
3253 toplev.c. */
3254 timevar_pop (TV_PARSE_GLOBAL);
3255 timevar_stop (TV_PHASE_PARSING);
3257 timevar_start (TV_PHASE_SETUP);
3259 /* Initialize the LTO front end. */
3260 lto_init ();
3262 timevar_stop (TV_PHASE_SETUP);
3263 timevar_start (TV_PHASE_STREAM_IN);
3265 /* Read all the symbols and call graph from all the files in the
3266 command line. */
3267 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3269 timevar_stop (TV_PHASE_STREAM_IN);
3271 if (!seen_error ())
3273 /* If WPA is enabled analyze the whole call graph and create an
3274 optimization plan. Otherwise, read in all the function
3275 bodies and continue with optimization. */
3276 if (flag_wpa)
3277 do_whole_program_analysis ();
3278 else
3280 timevar_start (TV_PHASE_OPT_GEN);
3282 materialize_cgraph ();
3283 if (!flag_ltrans)
3284 lto_promote_statics_nonwpa ();
3286 /* Let the middle end know that we have read and merged all of
3287 the input files. */
3288 symtab->compile ();
3290 timevar_stop (TV_PHASE_OPT_GEN);
3292 /* FIXME lto, if the processes spawned by WPA fail, we miss
3293 the chance to print WPA's report, so WPA will call
3294 print_lto_report before launching LTRANS. If LTRANS was
3295 launched directly by the driver we would not need to do
3296 this. */
3297 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3298 print_lto_report_1 ();
3302 /* Here we make LTO pretend to be a parser. */
3303 timevar_start (TV_PHASE_PARSING);
3304 timevar_push (TV_PARSE_GLOBAL);
3307 #include "gt-lto-lto.h"