* tree-if-conv.c: Fix various typos in comments.
[official-gcc.git] / gcc / lto / lto.c
blob7ec4c08a39ee0d5cb6f482fac9ebe09eb47f39a2
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 "tree-streamer.h"
52 #include "lto-section-names.h"
53 #include "splay-tree.h"
54 #include "lto-partition.h"
55 #include "context.h"
56 #include "pass_manager.h"
57 #include "ipa-inline.h"
58 #include "params.h"
59 #include "ipa-utils.h"
60 #include "gomp-constants.h"
63 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
64 static int lto_parallelism;
66 static GTY(()) tree first_personality_decl;
68 static GTY(()) const unsigned char *lto_mode_identity_table;
70 /* Returns a hash code for P. */
72 static hashval_t
73 hash_name (const void *p)
75 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
76 return (hashval_t) htab_hash_string (ds->name);
80 /* Returns nonzero if P1 and P2 are equal. */
82 static int
83 eq_name (const void *p1, const void *p2)
85 const struct lto_section_slot *s1 =
86 (const struct lto_section_slot *) p1;
87 const struct lto_section_slot *s2 =
88 (const struct lto_section_slot *) p2;
90 return strcmp (s1->name, s2->name) == 0;
93 /* Free lto_section_slot */
95 static void
96 free_with_string (void *arg)
98 struct lto_section_slot *s = (struct lto_section_slot *)arg;
100 free (CONST_CAST (char *, s->name));
101 free (arg);
104 /* Create section hash table */
106 htab_t
107 lto_obj_create_section_hash_table (void)
109 return htab_create (37, hash_name, eq_name, free_with_string);
112 /* Delete an allocated integer KEY in the splay tree. */
114 static void
115 lto_splay_tree_delete_id (splay_tree_key key)
117 free ((void *) key);
120 /* Compare splay tree node ids A and B. */
122 static int
123 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
125 unsigned HOST_WIDE_INT ai;
126 unsigned HOST_WIDE_INT bi;
128 ai = *(unsigned HOST_WIDE_INT *) a;
129 bi = *(unsigned HOST_WIDE_INT *) b;
131 if (ai < bi)
132 return -1;
133 else if (ai > bi)
134 return 1;
135 return 0;
138 /* Look up splay tree node by ID in splay tree T. */
140 static splay_tree_node
141 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
143 return splay_tree_lookup (t, (splay_tree_key) &id);
146 /* Check if KEY has ID. */
148 static bool
149 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
151 return *(unsigned HOST_WIDE_INT *) key == id;
154 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
155 The ID is allocated separately because we need HOST_WIDE_INTs which may
156 be wider than a splay_tree_key. */
158 static void
159 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
160 struct lto_file_decl_data *file_data)
162 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
163 *idp = id;
164 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
167 /* Create a splay tree. */
169 static splay_tree
170 lto_splay_tree_new (void)
172 return splay_tree_new (lto_splay_tree_compare_ids,
173 lto_splay_tree_delete_id,
174 NULL);
177 /* Return true when NODE has a clone that is analyzed (i.e. we need
178 to load its body even if the node itself is not needed). */
180 static bool
181 has_analyzed_clone_p (struct cgraph_node *node)
183 struct cgraph_node *orig = node;
184 node = node->clones;
185 if (node)
186 while (node != orig)
188 if (node->analyzed)
189 return true;
190 if (node->clones)
191 node = node->clones;
192 else if (node->next_sibling_clone)
193 node = node->next_sibling_clone;
194 else
196 while (node != orig && !node->next_sibling_clone)
197 node = node->clone_of;
198 if (node != orig)
199 node = node->next_sibling_clone;
202 return false;
205 /* Read the function body for the function associated with NODE. */
207 static void
208 lto_materialize_function (struct cgraph_node *node)
210 tree decl;
212 decl = node->decl;
213 /* Read in functions with body (analyzed nodes)
214 and also functions that are needed to produce virtual clones. */
215 if ((node->has_gimple_body_p () && node->analyzed)
216 || node->used_as_abstract_origin
217 || has_analyzed_clone_p (node))
219 /* Clones don't need to be read. */
220 if (node->clone_of)
221 return;
222 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
223 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
226 /* Let the middle end know about the function. */
227 rest_of_decl_compilation (decl, 1, 0);
231 /* Decode the content of memory pointed to by DATA in the in decl
232 state object STATE. DATA_IN points to a data_in structure for
233 decoding. Return the address after the decoded object in the
234 input. */
236 static const uint32_t *
237 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
238 struct lto_in_decl_state *state)
240 uint32_t ix;
241 tree decl;
242 uint32_t i, j;
244 ix = *data++;
245 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
246 if (!VAR_OR_FUNCTION_DECL_P (decl))
248 gcc_assert (decl == void_type_node);
249 decl = NULL_TREE;
251 state->fn_decl = decl;
253 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
255 uint32_t size = *data++;
256 vec<tree, va_gc> *decls = NULL;
257 vec_alloc (decls, size);
259 for (j = 0; j < size; j++)
260 vec_safe_push (decls,
261 streamer_tree_cache_get_tree (data_in->reader_cache,
262 data[j]));
264 state->streams[i] = decls;
265 data += size;
268 return data;
272 /* Global canonical type table. */
273 static htab_t gimple_canonical_types;
274 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
275 static unsigned long num_canonical_type_hash_entries;
276 static unsigned long num_canonical_type_hash_queries;
278 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
279 static hashval_t gimple_canonical_type_hash (const void *p);
280 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
282 /* Returning a hash value for gimple type TYPE.
284 The hash value returned is equal for types considered compatible
285 by gimple_canonical_types_compatible_p. */
287 static hashval_t
288 hash_canonical_type (tree type)
290 inchash::hash hstate;
292 /* We compute alias sets only for types that needs them.
293 Be sure we do not recurse to something else as we can not hash incomplete
294 types in a way they would have same hash value as compatible complete
295 types. */
296 gcc_checking_assert (type_with_alias_set_p (type));
298 /* Combine a few common features of types so that types are grouped into
299 smaller sets; when searching for existing matching types to merge,
300 only existing types having the same features as the new type will be
301 checked. */
302 hstate.add_int (tree_code_for_canonical_type_merging (TREE_CODE (type)));
303 hstate.add_int (TYPE_MODE (type));
305 /* Incorporate common features of numerical types. */
306 if (INTEGRAL_TYPE_P (type)
307 || SCALAR_FLOAT_TYPE_P (type)
308 || FIXED_POINT_TYPE_P (type)
309 || TREE_CODE (type) == OFFSET_TYPE
310 || POINTER_TYPE_P (type))
312 hstate.add_int (TYPE_UNSIGNED (type));
313 hstate.add_int (TYPE_PRECISION (type));
316 if (VECTOR_TYPE_P (type))
318 hstate.add_int (TYPE_VECTOR_SUBPARTS (type));
319 hstate.add_int (TYPE_UNSIGNED (type));
322 if (TREE_CODE (type) == COMPLEX_TYPE)
323 hstate.add_int (TYPE_UNSIGNED (type));
325 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
326 interoperable with "signed char". Unless all frontends are revisited to
327 agree on these types, we must ignore the flag completely. */
329 /* Fortran standard define C_PTR type that is compatible with every
330 C pointer. For this reason we need to glob all pointers into one.
331 Still pointers in different address spaces are not compatible. */
332 if (POINTER_TYPE_P (type))
333 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
335 /* For array types hash the domain bounds and the string flag. */
336 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
338 hstate.add_int (TYPE_STRING_FLAG (type));
339 /* OMP lowering can introduce error_mark_node in place of
340 random local decls in types. */
341 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
342 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
343 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
344 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
347 /* Recurse for aggregates with a single element type. */
348 if (TREE_CODE (type) == ARRAY_TYPE
349 || TREE_CODE (type) == COMPLEX_TYPE
350 || TREE_CODE (type) == VECTOR_TYPE)
351 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
353 /* Incorporate function return and argument types. */
354 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
356 unsigned na;
357 tree p;
359 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
361 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
363 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
364 na++;
367 hstate.add_int (na);
370 if (RECORD_OR_UNION_TYPE_P (type))
372 unsigned nf;
373 tree f;
375 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
376 if (TREE_CODE (f) == FIELD_DECL)
378 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
379 nf++;
382 hstate.add_int (nf);
385 return hstate.end();
388 /* Returning a hash value for gimple type TYPE combined with VAL. */
390 static void
391 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
393 hashval_t v;
395 /* All type variants have same TYPE_CANONICAL. */
396 type = TYPE_MAIN_VARIANT (type);
397 /* An already processed type. */
398 if (TYPE_CANONICAL (type))
400 type = TYPE_CANONICAL (type);
401 v = gimple_canonical_type_hash (type);
403 else
405 /* Canonical types should not be able to form SCCs by design, this
406 recursion is just because we do not register canonical types in
407 optimal order. To avoid quadratic behavior also register the
408 type here. */
409 v = hash_canonical_type (type);
410 gimple_register_canonical_type_1 (type, v);
412 hstate.add_int (v);
415 /* Returns the hash for a canonical type P. */
417 static hashval_t
418 gimple_canonical_type_hash (const void *p)
420 num_canonical_type_hash_queries++;
421 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
422 gcc_assert (slot != NULL);
423 return *slot;
428 /* Returns nonzero if P1 and P2 are equal. */
430 static int
431 gimple_canonical_type_eq (const void *p1, const void *p2)
433 const_tree t1 = (const_tree) p1;
434 const_tree t2 = (const_tree) p2;
435 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
436 CONST_CAST_TREE (t2));
439 /* Main worker for gimple_register_canonical_type. */
441 static void
442 gimple_register_canonical_type_1 (tree t, hashval_t hash)
444 void **slot;
446 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
448 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
449 if (*slot)
451 tree new_type = (tree)(*slot);
452 gcc_checking_assert (new_type != t);
453 TYPE_CANONICAL (t) = new_type;
455 else
457 TYPE_CANONICAL (t) = t;
458 *slot = (void *) t;
459 /* Cache the just computed hash value. */
460 num_canonical_type_hash_entries++;
461 bool existed_p = canonical_type_hash_cache->put (t, hash);
462 gcc_assert (!existed_p);
466 /* Register type T in the global type table gimple_types and set
467 TYPE_CANONICAL of T accordingly.
468 This is used by LTO to merge structurally equivalent types for
469 type-based aliasing purposes across different TUs and languages.
471 ??? This merging does not exactly match how the tree.c middle-end
472 functions will assign TYPE_CANONICAL when new types are created
473 during optimization (which at least happens for pointer and array
474 types). */
476 static void
477 gimple_register_canonical_type (tree t)
479 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t))
480 return;
482 /* Canonical types are same among all complete variants. */
483 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
484 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
485 else
487 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t),
488 hash_canonical_type (TYPE_MAIN_VARIANT (t)));
489 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
493 /* Re-compute TYPE_CANONICAL for NODE and related types. */
495 static void
496 lto_register_canonical_types (tree node, bool first_p)
498 if (!node
499 || !TYPE_P (node))
500 return;
502 if (first_p)
503 TYPE_CANONICAL (node) = NULL_TREE;
505 if (POINTER_TYPE_P (node)
506 || TREE_CODE (node) == COMPLEX_TYPE
507 || TREE_CODE (node) == ARRAY_TYPE)
508 lto_register_canonical_types (TREE_TYPE (node), first_p);
510 if (!first_p)
511 gimple_register_canonical_type (node);
515 /* Remember trees that contains references to declarations. */
516 static GTY(()) vec <tree, va_gc> *tree_with_vars;
518 #define CHECK_VAR(tt) \
519 do \
521 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
522 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
523 return true; \
524 } while (0)
526 #define CHECK_NO_VAR(tt) \
527 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
529 /* Check presence of pointers to decls in fields of a tree_typed T. */
531 static inline bool
532 mentions_vars_p_typed (tree t)
534 CHECK_NO_VAR (TREE_TYPE (t));
535 return false;
538 /* Check presence of pointers to decls in fields of a tree_common T. */
540 static inline bool
541 mentions_vars_p_common (tree t)
543 if (mentions_vars_p_typed (t))
544 return true;
545 CHECK_NO_VAR (TREE_CHAIN (t));
546 return false;
549 /* Check presence of pointers to decls in fields of a decl_minimal T. */
551 static inline bool
552 mentions_vars_p_decl_minimal (tree t)
554 if (mentions_vars_p_common (t))
555 return true;
556 CHECK_NO_VAR (DECL_NAME (t));
557 CHECK_VAR (DECL_CONTEXT (t));
558 return false;
561 /* Check presence of pointers to decls in fields of a decl_common T. */
563 static inline bool
564 mentions_vars_p_decl_common (tree t)
566 if (mentions_vars_p_decl_minimal (t))
567 return true;
568 CHECK_VAR (DECL_SIZE (t));
569 CHECK_VAR (DECL_SIZE_UNIT (t));
570 CHECK_VAR (DECL_INITIAL (t));
571 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
572 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
573 return false;
576 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
578 static inline bool
579 mentions_vars_p_decl_with_vis (tree t)
581 if (mentions_vars_p_decl_common (t))
582 return true;
584 /* Accessor macro has side-effects, use field-name here. */
585 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
586 return false;
589 /* Check presence of pointers to decls in fields of a decl_non_common T. */
591 static inline bool
592 mentions_vars_p_decl_non_common (tree t)
594 if (mentions_vars_p_decl_with_vis (t))
595 return true;
596 CHECK_NO_VAR (DECL_RESULT_FLD (t));
597 return false;
600 /* Check presence of pointers to decls in fields of a decl_non_common T. */
602 static bool
603 mentions_vars_p_function (tree t)
605 if (mentions_vars_p_decl_non_common (t))
606 return true;
607 CHECK_NO_VAR (DECL_ARGUMENTS (t));
608 CHECK_NO_VAR (DECL_VINDEX (t));
609 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
610 return false;
613 /* Check presence of pointers to decls in fields of a field_decl T. */
615 static bool
616 mentions_vars_p_field_decl (tree t)
618 if (mentions_vars_p_decl_common (t))
619 return true;
620 CHECK_VAR (DECL_FIELD_OFFSET (t));
621 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
622 CHECK_NO_VAR (DECL_QUALIFIER (t));
623 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
624 CHECK_NO_VAR (DECL_FCONTEXT (t));
625 return false;
628 /* Check presence of pointers to decls in fields of a type T. */
630 static bool
631 mentions_vars_p_type (tree t)
633 if (mentions_vars_p_common (t))
634 return true;
635 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
636 CHECK_VAR (TYPE_SIZE (t));
637 CHECK_VAR (TYPE_SIZE_UNIT (t));
638 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
639 CHECK_NO_VAR (TYPE_NAME (t));
641 CHECK_VAR (TYPE_MINVAL (t));
642 CHECK_VAR (TYPE_MAXVAL (t));
644 /* Accessor is for derived node types only. */
645 CHECK_NO_VAR (t->type_non_common.binfo);
647 CHECK_VAR (TYPE_CONTEXT (t));
648 CHECK_NO_VAR (TYPE_CANONICAL (t));
649 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
650 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
651 return false;
654 /* Check presence of pointers to decls in fields of a BINFO T. */
656 static bool
657 mentions_vars_p_binfo (tree t)
659 unsigned HOST_WIDE_INT i, n;
661 if (mentions_vars_p_common (t))
662 return true;
663 CHECK_VAR (BINFO_VTABLE (t));
664 CHECK_NO_VAR (BINFO_OFFSET (t));
665 CHECK_NO_VAR (BINFO_VIRTUALS (t));
666 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
667 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
668 for (i = 0; i < n; i++)
669 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
670 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
671 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
672 n = BINFO_N_BASE_BINFOS (t);
673 for (i = 0; i < n; i++)
674 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
675 return false;
678 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
680 static bool
681 mentions_vars_p_constructor (tree t)
683 unsigned HOST_WIDE_INT idx;
684 constructor_elt *ce;
686 if (mentions_vars_p_typed (t))
687 return true;
689 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
691 CHECK_NO_VAR (ce->index);
692 CHECK_VAR (ce->value);
694 return false;
697 /* Check presence of pointers to decls in fields of an expression tree T. */
699 static bool
700 mentions_vars_p_expr (tree t)
702 int i;
703 if (mentions_vars_p_typed (t))
704 return true;
705 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
706 CHECK_VAR (TREE_OPERAND (t, i));
707 return false;
710 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
712 static bool
713 mentions_vars_p_omp_clause (tree t)
715 int i;
716 if (mentions_vars_p_common (t))
717 return true;
718 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
719 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
720 return false;
723 /* Check presence of pointers to decls that needs later fixup in T. */
725 static bool
726 mentions_vars_p (tree t)
728 switch (TREE_CODE (t))
730 case IDENTIFIER_NODE:
731 break;
733 case TREE_LIST:
734 CHECK_VAR (TREE_VALUE (t));
735 CHECK_VAR (TREE_PURPOSE (t));
736 CHECK_NO_VAR (TREE_CHAIN (t));
737 break;
739 case FIELD_DECL:
740 return mentions_vars_p_field_decl (t);
742 case LABEL_DECL:
743 case CONST_DECL:
744 case PARM_DECL:
745 case RESULT_DECL:
746 case IMPORTED_DECL:
747 case NAMESPACE_DECL:
748 case NAMELIST_DECL:
749 return mentions_vars_p_decl_common (t);
751 case VAR_DECL:
752 return mentions_vars_p_decl_with_vis (t);
754 case TYPE_DECL:
755 return mentions_vars_p_decl_non_common (t);
757 case FUNCTION_DECL:
758 return mentions_vars_p_function (t);
760 case TREE_BINFO:
761 return mentions_vars_p_binfo (t);
763 case PLACEHOLDER_EXPR:
764 return mentions_vars_p_common (t);
766 case BLOCK:
767 case TRANSLATION_UNIT_DECL:
768 case OPTIMIZATION_NODE:
769 case TARGET_OPTION_NODE:
770 break;
772 case CONSTRUCTOR:
773 return mentions_vars_p_constructor (t);
775 case OMP_CLAUSE:
776 return mentions_vars_p_omp_clause (t);
778 default:
779 if (TYPE_P (t))
781 if (mentions_vars_p_type (t))
782 return true;
784 else if (EXPR_P (t))
786 if (mentions_vars_p_expr (t))
787 return true;
789 else if (CONSTANT_CLASS_P (t))
790 CHECK_NO_VAR (TREE_TYPE (t));
791 else
792 gcc_unreachable ();
794 return false;
798 /* Return the resolution for the decl with index INDEX from DATA_IN. */
800 static enum ld_plugin_symbol_resolution
801 get_resolution (struct data_in *data_in, unsigned index)
803 if (data_in->globals_resolution.exists ())
805 ld_plugin_symbol_resolution_t ret;
806 /* We can have references to not emitted functions in
807 DECL_FUNCTION_PERSONALITY at least. So we can and have
808 to indeed return LDPR_UNKNOWN in some cases. */
809 if (data_in->globals_resolution.length () <= index)
810 return LDPR_UNKNOWN;
811 ret = data_in->globals_resolution[index];
812 return ret;
814 else
815 /* Delay resolution finding until decl merging. */
816 return LDPR_UNKNOWN;
819 /* We need to record resolutions until symbol table is read. */
820 static void
821 register_resolution (struct lto_file_decl_data *file_data, tree decl,
822 enum ld_plugin_symbol_resolution resolution)
824 if (resolution == LDPR_UNKNOWN)
825 return;
826 if (!file_data->resolution_map)
827 file_data->resolution_map
828 = new hash_map<tree, ld_plugin_symbol_resolution>;
829 file_data->resolution_map->put (decl, resolution);
832 /* Register DECL with the global symbol table and change its
833 name if necessary to avoid name clashes for static globals across
834 different files. */
836 static void
837 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
838 unsigned ix)
840 tree context;
842 /* Variable has file scope, not local. */
843 if (!TREE_PUBLIC (decl)
844 && !((context = decl_function_context (decl))
845 && auto_var_in_fn_p (decl, context)))
846 rest_of_decl_compilation (decl, 1, 0);
848 /* If this variable has already been declared, queue the
849 declaration for merging. */
850 if (TREE_PUBLIC (decl))
851 register_resolution (data_in->file_data,
852 decl, get_resolution (data_in, ix));
856 /* Register DECL with the global symbol table and change its
857 name if necessary to avoid name clashes for static globals across
858 different files. DATA_IN contains descriptors and tables for the
859 file being read. */
861 static void
862 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
863 unsigned ix)
865 /* If this variable has already been declared, queue the
866 declaration for merging. */
867 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
868 register_resolution (data_in->file_data,
869 decl, get_resolution (data_in, ix));
873 /* For the type T re-materialize it in the type variant list and
874 the pointer/reference-to chains. */
876 static void
877 lto_fixup_prevailing_type (tree t)
879 /* The following re-creates proper variant lists while fixing up
880 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
881 variant list state before fixup is broken. */
883 /* If we are not our own variant leader link us into our new leaders
884 variant list. */
885 if (TYPE_MAIN_VARIANT (t) != t)
887 tree mv = TYPE_MAIN_VARIANT (t);
888 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
889 TYPE_NEXT_VARIANT (mv) = t;
892 /* The following reconstructs the pointer chains
893 of the new pointed-to type if we are a main variant. We do
894 not stream those so they are broken before fixup. */
895 if (TREE_CODE (t) == POINTER_TYPE
896 && TYPE_MAIN_VARIANT (t) == t)
898 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
899 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
901 else if (TREE_CODE (t) == REFERENCE_TYPE
902 && TYPE_MAIN_VARIANT (t) == t)
904 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
905 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
910 /* We keep prevailing tree SCCs in a hashtable with manual collision
911 handling (in case all hashes compare the same) and keep the colliding
912 entries in the tree_scc->next chain. */
914 struct tree_scc
916 tree_scc *next;
917 /* Hash of the whole SCC. */
918 hashval_t hash;
919 /* Number of trees in the SCC. */
920 unsigned len;
921 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
922 which share the same individual tree hash). */
923 unsigned entry_len;
924 /* The members of the SCC.
925 We only need to remember the first entry node candidate for prevailing
926 SCCs (but of course have access to all entries for SCCs we are
927 processing).
928 ??? For prevailing SCCs we really only need hash and the first
929 entry candidate, but that's too awkward to implement. */
930 tree entries[1];
933 struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
935 static inline hashval_t hash (const tree_scc *);
936 static inline bool equal (const tree_scc *, const tree_scc *);
939 hashval_t
940 tree_scc_hasher::hash (const tree_scc *scc)
942 return scc->hash;
945 bool
946 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
948 if (scc1->hash != scc2->hash
949 || scc1->len != scc2->len
950 || scc1->entry_len != scc2->entry_len)
951 return false;
952 return true;
955 static hash_table<tree_scc_hasher> *tree_scc_hash;
956 static struct obstack tree_scc_hash_obstack;
958 static unsigned long num_merged_types;
959 static unsigned long num_prevailing_types;
960 static unsigned long num_type_scc_trees;
961 static unsigned long total_scc_size;
962 static unsigned long num_sccs_read;
963 static unsigned long total_scc_size_merged;
964 static unsigned long num_sccs_merged;
965 static unsigned long num_scc_compares;
966 static unsigned long num_scc_compare_collisions;
969 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
970 recursing through in-SCC tree edges. Returns true if the SCCs entered
971 through T1 and T2 are equal and fills in *MAP with the pairs of
972 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
974 static bool
975 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
977 enum tree_code code;
979 /* Mark already visited nodes. */
980 TREE_ASM_WRITTEN (t2) = 1;
982 /* Push the pair onto map. */
983 (*map)[0] = t1;
984 (*map)[1] = t2;
985 *map = *map + 2;
987 /* Compare value-fields. */
988 #define compare_values(X) \
989 do { \
990 if (X(t1) != X(t2)) \
991 return false; \
992 } while (0)
994 compare_values (TREE_CODE);
995 code = TREE_CODE (t1);
997 if (!TYPE_P (t1))
999 compare_values (TREE_SIDE_EFFECTS);
1000 compare_values (TREE_CONSTANT);
1001 compare_values (TREE_READONLY);
1002 compare_values (TREE_PUBLIC);
1004 compare_values (TREE_ADDRESSABLE);
1005 compare_values (TREE_THIS_VOLATILE);
1006 if (DECL_P (t1))
1007 compare_values (DECL_UNSIGNED);
1008 else if (TYPE_P (t1))
1009 compare_values (TYPE_UNSIGNED);
1010 if (TYPE_P (t1))
1011 compare_values (TYPE_ARTIFICIAL);
1012 else
1013 compare_values (TREE_NO_WARNING);
1014 compare_values (TREE_NOTHROW);
1015 compare_values (TREE_STATIC);
1016 if (code != TREE_BINFO)
1017 compare_values (TREE_PRIVATE);
1018 compare_values (TREE_PROTECTED);
1019 compare_values (TREE_DEPRECATED);
1020 if (TYPE_P (t1))
1022 compare_values (TYPE_SATURATING);
1023 compare_values (TYPE_ADDR_SPACE);
1025 else if (code == SSA_NAME)
1026 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1028 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1030 if (!wi::eq_p (t1, t2))
1031 return false;
1034 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1036 /* ??? No suitable compare routine available. */
1037 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1038 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1039 if (r1.cl != r2.cl
1040 || r1.decimal != r2.decimal
1041 || r1.sign != r2.sign
1042 || r1.signalling != r2.signalling
1043 || r1.canonical != r2.canonical
1044 || r1.uexp != r2.uexp)
1045 return false;
1046 for (unsigned i = 0; i < SIGSZ; ++i)
1047 if (r1.sig[i] != r2.sig[i])
1048 return false;
1051 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1052 if (!fixed_compare (EQ_EXPR,
1053 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1054 return false;
1057 /* We don't want to compare locations, so there is nothing do compare
1058 for TS_DECL_MINIMAL. */
1060 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1062 compare_values (DECL_MODE);
1063 compare_values (DECL_NONLOCAL);
1064 compare_values (DECL_VIRTUAL_P);
1065 compare_values (DECL_IGNORED_P);
1066 compare_values (DECL_ABSTRACT_P);
1067 compare_values (DECL_ARTIFICIAL);
1068 compare_values (DECL_USER_ALIGN);
1069 compare_values (DECL_PRESERVE_P);
1070 compare_values (DECL_EXTERNAL);
1071 compare_values (DECL_GIMPLE_REG_P);
1072 compare_values (DECL_ALIGN);
1073 if (code == LABEL_DECL)
1075 compare_values (EH_LANDING_PAD_NR);
1076 compare_values (LABEL_DECL_UID);
1078 else if (code == FIELD_DECL)
1080 compare_values (DECL_PACKED);
1081 compare_values (DECL_NONADDRESSABLE_P);
1082 compare_values (DECL_OFFSET_ALIGN);
1084 else if (code == VAR_DECL)
1086 compare_values (DECL_HAS_DEBUG_EXPR_P);
1087 compare_values (DECL_NONLOCAL_FRAME);
1089 if (code == RESULT_DECL
1090 || code == PARM_DECL
1091 || code == VAR_DECL)
1093 compare_values (DECL_BY_REFERENCE);
1094 if (code == VAR_DECL
1095 || code == PARM_DECL)
1096 compare_values (DECL_HAS_VALUE_EXPR_P);
1100 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1101 compare_values (DECL_REGISTER);
1103 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1105 compare_values (DECL_COMMON);
1106 compare_values (DECL_DLLIMPORT_P);
1107 compare_values (DECL_WEAK);
1108 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1109 compare_values (DECL_COMDAT);
1110 compare_values (DECL_VISIBILITY);
1111 compare_values (DECL_VISIBILITY_SPECIFIED);
1112 if (code == VAR_DECL)
1114 compare_values (DECL_HARD_REGISTER);
1115 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1116 compare_values (DECL_IN_CONSTANT_POOL);
1120 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1122 compare_values (DECL_BUILT_IN_CLASS);
1123 compare_values (DECL_STATIC_CONSTRUCTOR);
1124 compare_values (DECL_STATIC_DESTRUCTOR);
1125 compare_values (DECL_UNINLINABLE);
1126 compare_values (DECL_POSSIBLY_INLINED);
1127 compare_values (DECL_IS_NOVOPS);
1128 compare_values (DECL_IS_RETURNS_TWICE);
1129 compare_values (DECL_IS_MALLOC);
1130 compare_values (DECL_IS_OPERATOR_NEW);
1131 compare_values (DECL_DECLARED_INLINE_P);
1132 compare_values (DECL_STATIC_CHAIN);
1133 compare_values (DECL_NO_INLINE_WARNING_P);
1134 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1135 compare_values (DECL_NO_LIMIT_STACK);
1136 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1137 compare_values (DECL_PURE_P);
1138 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1139 compare_values (DECL_FINAL_P);
1140 compare_values (DECL_CXX_CONSTRUCTOR_P);
1141 compare_values (DECL_CXX_DESTRUCTOR_P);
1142 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1143 compare_values (DECL_FUNCTION_CODE);
1146 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1148 compare_values (TYPE_MODE);
1149 compare_values (TYPE_STRING_FLAG);
1150 compare_values (TYPE_NEEDS_CONSTRUCTING);
1151 if (RECORD_OR_UNION_TYPE_P (t1))
1153 compare_values (TYPE_TRANSPARENT_AGGR);
1154 compare_values (TYPE_FINAL_P);
1156 else if (code == ARRAY_TYPE)
1157 compare_values (TYPE_NONALIASED_COMPONENT);
1158 compare_values (TYPE_PACKED);
1159 compare_values (TYPE_RESTRICT);
1160 compare_values (TYPE_USER_ALIGN);
1161 compare_values (TYPE_READONLY);
1162 compare_values (TYPE_PRECISION);
1163 compare_values (TYPE_ALIGN);
1164 compare_values (TYPE_ALIAS_SET);
1167 /* We don't want to compare locations, so there is nothing do compare
1168 for TS_EXP. */
1170 /* BLOCKs are function local and we don't merge anything there, so
1171 simply refuse to merge. */
1172 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1173 return false;
1175 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1176 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1177 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1178 return false;
1180 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1181 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1182 return false;
1184 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1185 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1186 sizeof (struct cl_optimization)) != 0)
1187 return false;
1189 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1190 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1191 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1192 return false;
1194 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1195 compare_values (CONSTRUCTOR_NELTS);
1197 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1198 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1199 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1200 IDENTIFIER_LENGTH (t1)) != 0)
1201 return false;
1203 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1204 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1205 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1206 TREE_STRING_LENGTH (t1)) != 0)
1207 return false;
1209 if (code == OMP_CLAUSE)
1211 compare_values (OMP_CLAUSE_CODE);
1212 switch (OMP_CLAUSE_CODE (t1))
1214 case OMP_CLAUSE_DEFAULT:
1215 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1216 break;
1217 case OMP_CLAUSE_SCHEDULE:
1218 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1219 break;
1220 case OMP_CLAUSE_DEPEND:
1221 compare_values (OMP_CLAUSE_DEPEND_KIND);
1222 break;
1223 case OMP_CLAUSE_MAP:
1224 compare_values (OMP_CLAUSE_MAP_KIND);
1225 break;
1226 case OMP_CLAUSE_PROC_BIND:
1227 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1228 break;
1229 case OMP_CLAUSE_REDUCTION:
1230 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1231 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1232 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1233 break;
1234 default:
1235 break;
1239 #undef compare_values
1242 /* Compare pointer fields. */
1244 /* Recurse. Search & Replaced from DFS_write_tree_body.
1245 Folding the early checks into the compare_tree_edges recursion
1246 macro makes debugging way quicker as you are able to break on
1247 compare_tree_sccs_1 and simply finish until a call returns false
1248 to spot the SCC members with the difference. */
1249 #define compare_tree_edges(E1, E2) \
1250 do { \
1251 tree t1_ = (E1), t2_ = (E2); \
1252 if (t1_ != t2_ \
1253 && (!t1_ || !t2_ \
1254 || !TREE_VISITED (t2_) \
1255 || (!TREE_ASM_WRITTEN (t2_) \
1256 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1257 return false; \
1258 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1259 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1260 } while (0)
1262 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1264 if (code != IDENTIFIER_NODE)
1265 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1268 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1270 unsigned i;
1271 /* Note that the number of elements for EXPR has already been emitted
1272 in EXPR's header (see streamer_write_tree_header). */
1273 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1274 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1277 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1279 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1280 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1283 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1285 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1286 /* ??? Global decls from different TUs have non-matching
1287 TRANSLATION_UNIT_DECLs. Only consider a small set of
1288 decls equivalent, we should not end up merging others. */
1289 if ((code == TYPE_DECL
1290 || code == NAMESPACE_DECL
1291 || code == IMPORTED_DECL
1292 || code == CONST_DECL
1293 || (VAR_OR_FUNCTION_DECL_P (t1)
1294 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1295 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1297 else
1298 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1301 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1303 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1304 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1305 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1306 if ((code == VAR_DECL
1307 || code == PARM_DECL)
1308 && DECL_HAS_VALUE_EXPR_P (t1))
1309 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1310 if (code == VAR_DECL
1311 && DECL_HAS_DEBUG_EXPR_P (t1))
1312 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1313 /* LTO specific edges. */
1314 if (code != FUNCTION_DECL
1315 && code != TRANSLATION_UNIT_DECL)
1316 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1319 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1321 if (code == FUNCTION_DECL)
1323 tree a1, a2;
1324 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1325 a1 || a2;
1326 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1327 compare_tree_edges (a1, a2);
1328 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1330 else if (code == TYPE_DECL)
1331 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1334 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1336 /* Make sure we don't inadvertently set the assembler name. */
1337 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1338 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1339 DECL_ASSEMBLER_NAME (t2));
1342 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1344 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1345 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1346 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1347 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1348 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1349 DECL_FIELD_BIT_OFFSET (t2));
1350 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1353 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1355 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1356 DECL_FUNCTION_PERSONALITY (t2));
1357 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1358 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1359 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1360 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1361 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1364 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1366 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1367 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1368 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1369 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1370 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1371 reconstructed during fixup. */
1372 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1373 during fixup. */
1374 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1375 /* ??? Global types from different TUs have non-matching
1376 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1377 equal. */
1378 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1380 else
1381 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1382 /* TYPE_CANONICAL is re-computed during type merging, so do not
1383 compare it here. */
1384 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1387 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1389 if (code == ENUMERAL_TYPE)
1390 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1391 else if (code == ARRAY_TYPE)
1392 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1393 else if (RECORD_OR_UNION_TYPE_P (t1))
1395 tree f1, f2;
1396 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1397 f1 || f2;
1398 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1399 compare_tree_edges (f1, f2);
1400 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1402 else if (code == FUNCTION_TYPE
1403 || code == METHOD_TYPE)
1404 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1405 if (!POINTER_TYPE_P (t1))
1406 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1407 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1410 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1412 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1413 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1414 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1417 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1418 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1419 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1421 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1423 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1424 compare_tree_edges (TREE_OPERAND (t1, i),
1425 TREE_OPERAND (t2, i));
1427 /* BLOCKs are function local and we don't merge anything there. */
1428 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1429 return false;
1432 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1434 unsigned i;
1435 tree t;
1436 /* Lengths have already been compared above. */
1437 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1438 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1439 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1440 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1441 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1442 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1443 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1444 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1445 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1448 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1450 unsigned i;
1451 tree index, value;
1452 /* Lengths have already been compared above. */
1453 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1455 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1456 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1460 if (code == OMP_CLAUSE)
1462 int i;
1464 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1465 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1466 OMP_CLAUSE_OPERAND (t2, i));
1467 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1470 #undef compare_tree_edges
1472 return true;
1475 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1476 out MAP if they are equal. */
1478 static bool
1479 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1480 tree *map)
1482 /* Assume SCC entry hashes are sorted after their cardinality. Which
1483 means we can simply take the first n-tuple of equal hashes
1484 (which is recorded as entry_len) and do n SCC entry candidate
1485 comparisons. */
1486 for (unsigned i = 0; i < pscc->entry_len; ++i)
1488 tree *mapp = map;
1489 num_scc_compare_collisions++;
1490 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1492 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1493 on the scc as all trees will be freed. */
1494 return true;
1496 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1497 the SCC prevails. */
1498 for (unsigned j = 0; j < scc->len; ++j)
1499 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1502 return false;
1505 /* QSort sort function to sort a map of two pointers after the 2nd
1506 pointer. */
1508 static int
1509 cmp_tree (const void *p1_, const void *p2_)
1511 tree *p1 = (tree *)(const_cast<void *>(p1_));
1512 tree *p2 = (tree *)(const_cast<void *>(p2_));
1513 if (p1[1] == p2[1])
1514 return 0;
1515 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1518 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1519 hash value SCC_HASH with an already recorded SCC. Return true if
1520 that was successful, otherwise return false. */
1522 static bool
1523 unify_scc (struct data_in *data_in, unsigned from,
1524 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1526 bool unified_p = false;
1527 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1528 tree_scc *scc
1529 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1530 scc->next = NULL;
1531 scc->hash = scc_hash;
1532 scc->len = len;
1533 scc->entry_len = scc_entry_len;
1534 for (unsigned i = 0; i < len; ++i)
1536 tree t = streamer_tree_cache_get_tree (cache, from + i);
1537 scc->entries[i] = t;
1538 /* Do not merge SCCs with local entities inside them. Also do
1539 not merge TRANSLATION_UNIT_DECLs. */
1540 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1541 || (VAR_OR_FUNCTION_DECL_P (t)
1542 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1543 || TREE_CODE (t) == LABEL_DECL)
1545 /* Avoid doing any work for these cases and do not worry to
1546 record the SCCs for further merging. */
1547 return false;
1551 /* Look for the list of candidate SCCs to compare against. */
1552 tree_scc **slot;
1553 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1554 if (*slot)
1556 /* Try unifying against each candidate. */
1557 num_scc_compares++;
1559 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1560 outside of the scc when following tree edges. Make sure
1561 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1562 to track whether we visited the SCC member during the compare.
1563 We cannot use TREE_VISITED on the pscc members as the extended
1564 scc and pscc can overlap. */
1565 for (unsigned i = 0; i < scc->len; ++i)
1567 TREE_VISITED (scc->entries[i]) = 1;
1568 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1571 tree *map = XALLOCAVEC (tree, 2 * len);
1572 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1574 if (!compare_tree_sccs (pscc, scc, map))
1575 continue;
1577 /* Found an equal SCC. */
1578 unified_p = true;
1579 num_scc_compare_collisions--;
1580 num_sccs_merged++;
1581 total_scc_size_merged += len;
1583 #ifdef ENABLE_CHECKING
1584 for (unsigned i = 0; i < len; ++i)
1586 tree t = map[2*i+1];
1587 enum tree_code code = TREE_CODE (t);
1588 /* IDENTIFIER_NODEs should be singletons and are merged by the
1589 streamer. The others should be singletons, too, and we
1590 should not merge them in any way. */
1591 gcc_assert (code != TRANSLATION_UNIT_DECL
1592 && code != IDENTIFIER_NODE
1593 && !streamer_handle_as_builtin_p (t));
1595 #endif
1597 /* Fixup the streamer cache with the prevailing nodes according
1598 to the tree node mapping computed by compare_tree_sccs. */
1599 if (len == 1)
1600 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1601 else
1603 tree *map2 = XALLOCAVEC (tree, 2 * len);
1604 for (unsigned i = 0; i < len; ++i)
1606 map2[i*2] = (tree)(uintptr_t)(from + i);
1607 map2[i*2+1] = scc->entries[i];
1609 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1610 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1611 for (unsigned i = 0; i < len; ++i)
1612 streamer_tree_cache_replace_tree (cache, map[2*i],
1613 (uintptr_t)map2[2*i]);
1616 /* Free the tree nodes from the read SCC. */
1617 data_in->location_cache.revert_location_cache ();
1618 for (unsigned i = 0; i < len; ++i)
1620 enum tree_code code;
1621 if (TYPE_P (scc->entries[i]))
1622 num_merged_types++;
1623 code = TREE_CODE (scc->entries[i]);
1624 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1625 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1626 ggc_free (scc->entries[i]);
1629 break;
1632 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1633 if (!unified_p)
1634 for (unsigned i = 0; i < scc->len; ++i)
1635 TREE_VISITED (scc->entries[i]) = 0;
1638 /* If we didn't unify it to any candidate duplicate the relevant
1639 pieces to permanent storage and link it into the chain. */
1640 if (!unified_p)
1642 tree_scc *pscc
1643 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1644 memcpy (pscc, scc, sizeof (tree_scc));
1645 pscc->next = (*slot);
1646 *slot = pscc;
1648 return unified_p;
1652 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1653 RESOLUTIONS is the set of symbols picked by the linker (read from the
1654 resolution file when the linker plugin is being used). */
1656 static void
1657 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1658 vec<ld_plugin_symbol_resolution_t> resolutions)
1660 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1661 const int decl_offset = sizeof (struct lto_decl_header);
1662 const int main_offset = decl_offset + header->decl_state_size;
1663 const int string_offset = main_offset + header->main_size;
1664 struct data_in *data_in;
1665 unsigned int i;
1666 const uint32_t *data_ptr, *data_end;
1667 uint32_t num_decl_states;
1669 lto_input_block ib_main ((const char *) data + main_offset,
1670 header->main_size, decl_data->mode_table);
1672 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1673 header->string_size, resolutions);
1675 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1676 internal types that should not be merged. */
1678 /* Read the global declarations and types. */
1679 while (ib_main.p < ib_main.len)
1681 tree t;
1682 unsigned from = data_in->reader_cache->nodes.length ();
1683 /* Read and uniquify SCCs as in the input stream. */
1684 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1685 if (tag == LTO_tree_scc)
1687 unsigned len_;
1688 unsigned scc_entry_len;
1689 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1690 &scc_entry_len);
1691 unsigned len = data_in->reader_cache->nodes.length () - from;
1692 gcc_assert (len == len_);
1694 total_scc_size += len;
1695 num_sccs_read++;
1697 /* We have the special case of size-1 SCCs that are pre-merged
1698 by means of identifier and string sharing for example.
1699 ??? Maybe we should avoid streaming those as SCCs. */
1700 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1701 from);
1702 if (len == 1
1703 && (TREE_CODE (first) == IDENTIFIER_NODE
1704 || TREE_CODE (first) == INTEGER_CST
1705 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1706 || streamer_handle_as_builtin_p (first)))
1707 continue;
1709 /* Try to unify the SCC with already existing ones. */
1710 if (!flag_ltrans
1711 && unify_scc (data_in, from,
1712 len, scc_entry_len, scc_hash))
1713 continue;
1715 /* Tree merging failed, mark entries in location cache as
1716 permanent. */
1717 data_in->location_cache.accept_location_cache ();
1719 bool seen_type = false;
1720 for (unsigned i = 0; i < len; ++i)
1722 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1723 from + i);
1724 /* Reconstruct the type variant and pointer-to/reference-to
1725 chains. */
1726 if (TYPE_P (t))
1728 seen_type = true;
1729 num_prevailing_types++;
1730 lto_fixup_prevailing_type (t);
1732 /* Compute the canonical type of all types.
1733 ??? Should be able to assert that !TYPE_CANONICAL. */
1734 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1736 gimple_register_canonical_type (t);
1737 if (odr_type_p (t))
1738 register_odr_type (t);
1740 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1741 type which is also member of this SCC. */
1742 if (TREE_CODE (t) == INTEGER_CST
1743 && !TREE_OVERFLOW (t))
1744 cache_integer_cst (t);
1745 /* Register TYPE_DECLs with the debuginfo machinery. */
1746 if (!flag_wpa
1747 && TREE_CODE (t) == TYPE_DECL)
1749 /* Dwarf2out needs location information.
1750 TODO: Moving this out of the streamer loop may noticealy
1751 improve ltrans linemap memory use. */
1752 data_in->location_cache.apply_location_cache ();
1753 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1755 if (!flag_ltrans)
1757 /* Register variables and functions with the
1758 symbol table. */
1759 if (TREE_CODE (t) == VAR_DECL)
1760 lto_register_var_decl_in_symtab (data_in, t, from + i);
1761 else if (TREE_CODE (t) == FUNCTION_DECL
1762 && !DECL_BUILT_IN (t))
1763 lto_register_function_decl_in_symtab (data_in, t, from + i);
1764 /* Scan the tree for references to global functions or
1765 variables and record those for later fixup. */
1766 if (mentions_vars_p (t))
1767 vec_safe_push (tree_with_vars, t);
1770 if (seen_type)
1771 num_type_scc_trees += len;
1773 else
1775 /* Pickle stray references. */
1776 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1777 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1780 data_in->location_cache.apply_location_cache ();
1782 /* Read in lto_in_decl_state objects. */
1783 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1784 data_end =
1785 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1786 num_decl_states = *data_ptr++;
1788 gcc_assert (num_decl_states > 0);
1789 decl_data->global_decl_state = lto_new_in_decl_state ();
1790 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1791 decl_data->global_decl_state);
1793 /* Read in per-function decl states and enter them in hash table. */
1794 decl_data->function_decl_states =
1795 hash_table<decl_state_hasher>::create_ggc (37);
1797 for (i = 1; i < num_decl_states; i++)
1799 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1801 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1802 lto_in_decl_state **slot
1803 = decl_data->function_decl_states->find_slot (state, INSERT);
1804 gcc_assert (*slot == NULL);
1805 *slot = state;
1808 if (data_ptr != data_end)
1809 internal_error ("bytecode stream: garbage at the end of symbols section");
1811 /* Set the current decl state to be the global state. */
1812 decl_data->current_decl_state = decl_data->global_decl_state;
1814 lto_data_in_delete (data_in);
1817 /* Custom version of strtoll, which is not portable. */
1819 static int64_t
1820 lto_parse_hex (const char *p)
1822 int64_t ret = 0;
1824 for (; *p != '\0'; ++p)
1826 char c = *p;
1827 unsigned char part;
1828 ret <<= 4;
1829 if (c >= '0' && c <= '9')
1830 part = c - '0';
1831 else if (c >= 'a' && c <= 'f')
1832 part = c - 'a' + 10;
1833 else if (c >= 'A' && c <= 'F')
1834 part = c - 'A' + 10;
1835 else
1836 internal_error ("could not parse hex number");
1837 ret |= part;
1840 return ret;
1843 /* Read resolution for file named FILE_NAME. The resolution is read from
1844 RESOLUTION. */
1846 static void
1847 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1849 /* We require that objects in the resolution file are in the same
1850 order as the lto1 command line. */
1851 unsigned int name_len;
1852 char *obj_name;
1853 unsigned int num_symbols;
1854 unsigned int i;
1855 struct lto_file_decl_data *file_data;
1856 splay_tree_node nd = NULL;
1858 if (!resolution)
1859 return;
1861 name_len = strlen (file->filename);
1862 obj_name = XNEWVEC (char, name_len + 1);
1863 fscanf (resolution, " "); /* Read white space. */
1865 fread (obj_name, sizeof (char), name_len, resolution);
1866 obj_name[name_len] = '\0';
1867 if (filename_cmp (obj_name, file->filename) != 0)
1868 internal_error ("unexpected file name %s in linker resolution file. "
1869 "Expected %s", obj_name, file->filename);
1870 if (file->offset != 0)
1872 int t;
1873 char offset_p[17];
1874 int64_t offset;
1875 t = fscanf (resolution, "@0x%16s", offset_p);
1876 if (t != 1)
1877 internal_error ("could not parse file offset");
1878 offset = lto_parse_hex (offset_p);
1879 if (offset != file->offset)
1880 internal_error ("unexpected offset");
1883 free (obj_name);
1885 fscanf (resolution, "%u", &num_symbols);
1887 for (i = 0; i < num_symbols; i++)
1889 int t;
1890 unsigned index;
1891 unsigned HOST_WIDE_INT id;
1892 char r_str[27];
1893 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1894 unsigned int j;
1895 unsigned int lto_resolution_str_len =
1896 sizeof (lto_resolution_str) / sizeof (char *);
1897 res_pair rp;
1899 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1900 &index, &id, r_str);
1901 if (t != 3)
1902 internal_error ("invalid line in the resolution file");
1904 for (j = 0; j < lto_resolution_str_len; j++)
1906 if (strcmp (lto_resolution_str[j], r_str) == 0)
1908 r = (enum ld_plugin_symbol_resolution) j;
1909 break;
1912 if (j == lto_resolution_str_len)
1913 internal_error ("invalid resolution in the resolution file");
1915 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1917 nd = lto_splay_tree_lookup (file_ids, id);
1918 if (nd == NULL)
1919 internal_error ("resolution sub id %wx not in object file", id);
1922 file_data = (struct lto_file_decl_data *)nd->value;
1923 /* The indexes are very sparse. To save memory save them in a compact
1924 format that is only unpacked later when the subfile is processed. */
1925 rp.res = r;
1926 rp.index = index;
1927 file_data->respairs.safe_push (rp);
1928 if (file_data->max_index < index)
1929 file_data->max_index = index;
1933 /* List of file_decl_datas */
1934 struct file_data_list
1936 struct lto_file_decl_data *first, *last;
1939 /* Is the name for a id'ed LTO section? */
1941 static int
1942 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1944 const char *s;
1946 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
1947 return 0;
1948 s = strrchr (name, '.');
1949 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1952 /* Create file_data of each sub file id */
1954 static int
1955 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1956 struct file_data_list *list)
1958 struct lto_section_slot s_slot, *new_slot;
1959 unsigned HOST_WIDE_INT id;
1960 splay_tree_node nd;
1961 void **hash_slot;
1962 char *new_name;
1963 struct lto_file_decl_data *file_data;
1965 if (!lto_section_with_id (ls->name, &id))
1966 return 1;
1968 /* Find hash table of sub module id */
1969 nd = lto_splay_tree_lookup (file_ids, id);
1970 if (nd != NULL)
1972 file_data = (struct lto_file_decl_data *)nd->value;
1974 else
1976 file_data = ggc_alloc<lto_file_decl_data> ();
1977 memset(file_data, 0, sizeof (struct lto_file_decl_data));
1978 file_data->id = id;
1979 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1980 lto_splay_tree_insert (file_ids, id, file_data);
1982 /* Maintain list in linker order */
1983 if (!list->first)
1984 list->first = file_data;
1985 if (list->last)
1986 list->last->next = file_data;
1987 list->last = file_data;
1990 /* Copy section into sub module hash table */
1991 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
1992 s_slot.name = new_name;
1993 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
1994 gcc_assert (*hash_slot == NULL);
1996 new_slot = XDUP (struct lto_section_slot, ls);
1997 new_slot->name = new_name;
1998 *hash_slot = new_slot;
1999 return 1;
2002 /* Read declarations and other initializations for a FILE_DATA. */
2004 static void
2005 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2007 const char *data;
2008 size_t len;
2009 vec<ld_plugin_symbol_resolution_t>
2010 resolutions = vNULL;
2011 int i;
2012 res_pair *rp;
2014 /* Create vector for fast access of resolution. We do this lazily
2015 to save memory. */
2016 resolutions.safe_grow_cleared (file_data->max_index + 1);
2017 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2018 resolutions[rp->index] = rp->res;
2019 file_data->respairs.release ();
2021 file_data->renaming_hash_table = lto_create_renaming_table ();
2022 file_data->file_name = file->filename;
2023 #ifdef ACCEL_COMPILER
2024 lto_input_mode_table (file_data);
2025 #else
2026 file_data->mode_table = lto_mode_identity_table;
2027 #endif
2028 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2029 if (data == NULL)
2031 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2032 return;
2034 /* Frees resolutions */
2035 lto_read_decls (file_data, data, resolutions);
2036 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2039 /* Finalize FILE_DATA in FILE and increase COUNT. */
2041 static int
2042 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2043 int *count)
2045 lto_file_finalize (file_data, file);
2046 if (symtab->dump_file)
2047 fprintf (symtab->dump_file,
2048 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2049 file_data->file_name, file_data->id);
2050 (*count)++;
2051 return 0;
2054 /* Generate a TREE representation for all types and external decls
2055 entities in FILE.
2057 Read all of the globals out of the file. Then read the cgraph
2058 and process the .o index into the cgraph nodes so that it can open
2059 the .o file to load the functions and ipa information. */
2061 static struct lto_file_decl_data *
2062 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2064 struct lto_file_decl_data *file_data = NULL;
2065 splay_tree file_ids;
2066 htab_t section_hash_table;
2067 struct lto_section_slot *section;
2068 struct file_data_list file_list;
2069 struct lto_section_list section_list;
2071 memset (&section_list, 0, sizeof (struct lto_section_list));
2072 section_hash_table = lto_obj_build_section_table (file, &section_list);
2074 /* Find all sub modules in the object and put their sections into new hash
2075 tables in a splay tree. */
2076 file_ids = lto_splay_tree_new ();
2077 memset (&file_list, 0, sizeof (struct file_data_list));
2078 for (section = section_list.first; section != NULL; section = section->next)
2079 create_subid_section_table (section, file_ids, &file_list);
2081 /* Add resolutions to file ids */
2082 lto_resolution_read (file_ids, resolution_file, file);
2084 /* Finalize each lto file for each submodule in the merged object */
2085 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2086 lto_create_files_from_ids (file, file_data, count);
2088 splay_tree_delete (file_ids);
2089 htab_delete (section_hash_table);
2091 return file_list.first;
2094 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2095 #define LTO_MMAP_IO 1
2096 #endif
2098 #if LTO_MMAP_IO
2099 /* Page size of machine is used for mmap and munmap calls. */
2100 static size_t page_mask;
2101 #endif
2103 /* Get the section data of length LEN from FILENAME starting at
2104 OFFSET. The data segment must be freed by the caller when the
2105 caller is finished. Returns NULL if all was not well. */
2107 static char *
2108 lto_read_section_data (struct lto_file_decl_data *file_data,
2109 intptr_t offset, size_t len)
2111 char *result;
2112 static int fd = -1;
2113 static char *fd_name;
2114 #if LTO_MMAP_IO
2115 intptr_t computed_len;
2116 intptr_t computed_offset;
2117 intptr_t diff;
2118 #endif
2120 /* Keep a single-entry file-descriptor cache. The last file we
2121 touched will get closed at exit.
2122 ??? Eventually we want to add a more sophisticated larger cache
2123 or rather fix function body streaming to not stream them in
2124 practically random order. */
2125 if (fd != -1
2126 && filename_cmp (fd_name, file_data->file_name) != 0)
2128 free (fd_name);
2129 close (fd);
2130 fd = -1;
2132 if (fd == -1)
2134 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2135 if (fd == -1)
2137 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2138 return NULL;
2140 fd_name = xstrdup (file_data->file_name);
2143 #if LTO_MMAP_IO
2144 if (!page_mask)
2146 size_t page_size = sysconf (_SC_PAGE_SIZE);
2147 page_mask = ~(page_size - 1);
2150 computed_offset = offset & page_mask;
2151 diff = offset - computed_offset;
2152 computed_len = len + diff;
2154 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2155 fd, computed_offset);
2156 if (result == MAP_FAILED)
2158 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2159 return NULL;
2162 return result + diff;
2163 #else
2164 result = (char *) xmalloc (len);
2165 if (lseek (fd, offset, SEEK_SET) != offset
2166 || read (fd, result, len) != (ssize_t) len)
2168 free (result);
2169 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2170 result = NULL;
2172 #ifdef __MINGW32__
2173 /* Native windows doesn't supports delayed unlink on opened file. So
2174 we close file here again. This produces higher I/O load, but at least
2175 it prevents to have dangling file handles preventing unlink. */
2176 free (fd_name);
2177 fd_name = NULL;
2178 close (fd);
2179 fd = -1;
2180 #endif
2181 return result;
2182 #endif
2186 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2187 NAME will be NULL unless the section type is for a function
2188 body. */
2190 static const char *
2191 get_section_data (struct lto_file_decl_data *file_data,
2192 enum lto_section_type section_type,
2193 const char *name,
2194 size_t *len)
2196 htab_t section_hash_table = file_data->section_hash_table;
2197 struct lto_section_slot *f_slot;
2198 struct lto_section_slot s_slot;
2199 const char *section_name = lto_get_section_name (section_type, name, file_data);
2200 char *data = NULL;
2202 *len = 0;
2203 s_slot.name = section_name;
2204 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2205 if (f_slot)
2207 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2208 *len = f_slot->len;
2211 free (CONST_CAST (char *, section_name));
2212 return data;
2216 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2217 starts at OFFSET and has LEN bytes. */
2219 static void
2220 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2221 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2222 const char *name ATTRIBUTE_UNUSED,
2223 const char *offset, size_t len ATTRIBUTE_UNUSED)
2225 #if LTO_MMAP_IO
2226 intptr_t computed_len;
2227 intptr_t computed_offset;
2228 intptr_t diff;
2229 #endif
2231 #if LTO_MMAP_IO
2232 computed_offset = ((intptr_t) offset) & page_mask;
2233 diff = (intptr_t) offset - computed_offset;
2234 computed_len = len + diff;
2236 munmap ((caddr_t) computed_offset, computed_len);
2237 #else
2238 free (CONST_CAST(char *, offset));
2239 #endif
2242 static lto_file *current_lto_file;
2244 /* Helper for qsort; compare partitions and return one with smaller size.
2245 We sort from greatest to smallest so parallel build doesn't stale on the
2246 longest compilation being executed too late. */
2248 static int
2249 cmp_partitions_size (const void *a, const void *b)
2251 const struct ltrans_partition_def *pa
2252 = *(struct ltrans_partition_def *const *)a;
2253 const struct ltrans_partition_def *pb
2254 = *(struct ltrans_partition_def *const *)b;
2255 return pb->insns - pa->insns;
2258 /* Helper for qsort; compare partitions and return one with smaller order. */
2260 static int
2261 cmp_partitions_order (const void *a, const void *b)
2263 const struct ltrans_partition_def *pa
2264 = *(struct ltrans_partition_def *const *)a;
2265 const struct ltrans_partition_def *pb
2266 = *(struct ltrans_partition_def *const *)b;
2267 int ordera = -1, orderb = -1;
2269 if (lto_symtab_encoder_size (pa->encoder))
2270 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2271 if (lto_symtab_encoder_size (pb->encoder))
2272 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2273 return orderb - ordera;
2276 /* Actually stream out ENCODER into TEMP_FILENAME. */
2278 static void
2279 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2281 lto_file *file = lto_obj_file_open (temp_filename, true);
2282 if (!file)
2283 fatal_error (input_location, "lto_obj_file_open() failed");
2284 lto_set_current_out_file (file);
2286 ipa_write_optimization_summaries (encoder);
2288 lto_set_current_out_file (NULL);
2289 lto_obj_file_close (file);
2290 free (file);
2293 /* Wait for forked process and signal errors. */
2294 #ifdef HAVE_WORKING_FORK
2295 static void
2296 wait_for_child ()
2298 int status;
2301 #ifndef WCONTINUED
2302 #define WCONTINUED 0
2303 #endif
2304 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2305 if (w == -1)
2306 fatal_error (input_location, "waitpid failed");
2308 if (WIFEXITED (status) && WEXITSTATUS (status))
2309 fatal_error (input_location, "streaming subprocess failed");
2310 else if (WIFSIGNALED (status))
2311 fatal_error (input_location,
2312 "streaming subprocess was killed by signal");
2314 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2316 #endif
2318 /* Stream out ENCODER into TEMP_FILENAME
2319 Fork if that seems to help. */
2321 static void
2322 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2323 bool ARG_UNUSED (last))
2325 #ifdef HAVE_WORKING_FORK
2326 static int nruns;
2328 if (lto_parallelism <= 1)
2330 do_stream_out (temp_filename, encoder);
2331 return;
2334 /* Do not run more than LTO_PARALLELISM streamings
2335 FIXME: we ignore limits on jobserver. */
2336 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2338 wait_for_child ();
2339 nruns --;
2341 /* If this is not the last parallel partition, execute new
2342 streaming process. */
2343 if (!last)
2345 pid_t cpid = fork ();
2347 if (!cpid)
2349 setproctitle ("lto1-wpa-streaming");
2350 do_stream_out (temp_filename, encoder);
2351 exit (0);
2353 /* Fork failed; lets do the job ourseleves. */
2354 else if (cpid == -1)
2355 do_stream_out (temp_filename, encoder);
2356 else
2357 nruns++;
2359 /* Last partition; stream it and wait for all children to die. */
2360 else
2362 int i;
2363 do_stream_out (temp_filename, encoder);
2364 for (i = 0; i < nruns; i++)
2365 wait_for_child ();
2367 asm_nodes_output = true;
2368 #else
2369 do_stream_out (temp_filename, encoder);
2370 #endif
2373 /* Write all output files in WPA mode and the file with the list of
2374 LTRANS units. */
2376 static void
2377 lto_wpa_write_files (void)
2379 unsigned i, n_sets;
2380 ltrans_partition part;
2381 FILE *ltrans_output_list_stream;
2382 char *temp_filename;
2383 vec <char *>temp_filenames = vNULL;
2384 size_t blen;
2386 /* Open the LTRANS output list. */
2387 if (!ltrans_output_list)
2388 fatal_error (input_location, "no LTRANS output list filename provided");
2390 timevar_push (TV_WHOPR_WPA);
2392 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2393 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2395 timevar_pop (TV_WHOPR_WPA);
2397 timevar_push (TV_WHOPR_WPA_IO);
2399 /* Generate a prefix for the LTRANS unit files. */
2400 blen = strlen (ltrans_output_list);
2401 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2402 strcpy (temp_filename, ltrans_output_list);
2403 if (blen > sizeof (".out")
2404 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2405 ".out") == 0)
2406 temp_filename[blen - sizeof (".out") + 1] = '\0';
2407 blen = strlen (temp_filename);
2409 n_sets = ltrans_partitions.length ();
2411 /* Sort partitions by size so small ones are compiled last.
2412 FIXME: Even when not reordering we may want to output one list for parallel make
2413 and other for final link command. */
2415 if (!flag_profile_reorder_functions || !flag_profile_use)
2416 ltrans_partitions.qsort (flag_toplevel_reorder
2417 ? cmp_partitions_size
2418 : cmp_partitions_order);
2420 for (i = 0; i < n_sets; i++)
2422 ltrans_partition part = ltrans_partitions[i];
2424 /* Write all the nodes in SET. */
2425 sprintf (temp_filename + blen, "%u.o", i);
2427 if (!quiet_flag)
2428 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2429 if (symtab->dump_file)
2431 lto_symtab_encoder_iterator lsei;
2433 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2434 part->name, temp_filename, part->insns);
2435 fprintf (symtab->dump_file, " Symbols in partition: ");
2436 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2437 lsei_next_in_partition (&lsei))
2439 symtab_node *node = lsei_node (lsei);
2440 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2442 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2443 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2444 lsei_next (&lsei))
2446 symtab_node *node = lsei_node (lsei);
2447 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2449 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2450 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2451 if (cnode
2452 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2453 fprintf (symtab->dump_file, "(body included)");
2454 else
2456 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2457 if (vnode
2458 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2459 fprintf (symtab->dump_file, "(initializer included)");
2463 fprintf (symtab->dump_file, "\n");
2465 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2467 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2469 part->encoder = NULL;
2471 temp_filenames.safe_push (xstrdup (temp_filename));
2473 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2474 if (ltrans_output_list_stream == NULL)
2475 fatal_error (input_location,
2476 "opening LTRANS output list %s: %m", ltrans_output_list);
2477 for (i = 0; i < n_sets; i++)
2479 unsigned int len = strlen (temp_filenames[i]);
2480 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2481 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2482 fatal_error (input_location, "writing to LTRANS output list %s: %m",
2483 ltrans_output_list);
2484 free (temp_filenames[i]);
2486 temp_filenames.release();
2488 lto_stats.num_output_files += n_sets;
2490 /* Close the LTRANS output list. */
2491 if (fclose (ltrans_output_list_stream))
2492 fatal_error (input_location,
2493 "closing LTRANS output list %s: %m", ltrans_output_list);
2495 free_ltrans_partitions();
2496 free (temp_filename);
2498 timevar_pop (TV_WHOPR_WPA_IO);
2502 /* If TT is a variable or function decl replace it with its
2503 prevailing variant. */
2504 #define LTO_SET_PREVAIL(tt) \
2505 do {\
2506 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2507 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2509 tt = lto_symtab_prevailing_decl (tt); \
2510 fixed = true; \
2512 } while (0)
2514 /* Ensure that TT isn't a replacable var of function decl. */
2515 #define LTO_NO_PREVAIL(tt) \
2516 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2518 /* Given a tree T replace all fields referring to variables or functions
2519 with their prevailing variant. */
2520 static void
2521 lto_fixup_prevailing_decls (tree t)
2523 enum tree_code code = TREE_CODE (t);
2524 bool fixed = false;
2526 gcc_checking_assert (code != TREE_BINFO);
2527 LTO_NO_PREVAIL (TREE_TYPE (t));
2528 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2529 LTO_NO_PREVAIL (TREE_CHAIN (t));
2530 if (DECL_P (t))
2532 LTO_NO_PREVAIL (DECL_NAME (t));
2533 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2534 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2536 LTO_SET_PREVAIL (DECL_SIZE (t));
2537 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2538 LTO_SET_PREVAIL (DECL_INITIAL (t));
2539 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2540 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2542 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2544 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2546 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2548 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2550 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2552 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2553 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2554 LTO_NO_PREVAIL (DECL_VINDEX (t));
2556 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2558 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2559 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2560 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2561 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2562 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2565 else if (TYPE_P (t))
2567 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2568 LTO_SET_PREVAIL (TYPE_SIZE (t));
2569 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2570 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2571 LTO_NO_PREVAIL (TYPE_NAME (t));
2573 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2574 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2575 LTO_NO_PREVAIL (t->type_non_common.binfo);
2577 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2579 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2580 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2581 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2583 else if (EXPR_P (t))
2585 int i;
2586 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2587 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2589 else if (TREE_CODE (t) == CONSTRUCTOR)
2591 unsigned i;
2592 tree val;
2593 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2594 LTO_SET_PREVAIL (val);
2596 else
2598 switch (code)
2600 case TREE_LIST:
2601 LTO_SET_PREVAIL (TREE_VALUE (t));
2602 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2603 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2604 break;
2605 default:
2606 gcc_unreachable ();
2609 /* If we fixed nothing, then we missed something seen by
2610 mentions_vars_p. */
2611 gcc_checking_assert (fixed);
2613 #undef LTO_SET_PREVAIL
2614 #undef LTO_NO_PREVAIL
2616 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2617 replaces var and function decls with the corresponding prevailing def. */
2619 static void
2620 lto_fixup_state (struct lto_in_decl_state *state)
2622 unsigned i, si;
2624 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2625 we still need to walk from all DECLs to find the reachable
2626 FUNCTION_DECLs and VAR_DECLs. */
2627 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2629 vec<tree, va_gc> *trees = state->streams[si];
2630 for (i = 0; i < vec_safe_length (trees); i++)
2632 tree t = (*trees)[i];
2633 #ifdef ENABLE_CHECKING
2634 if (TYPE_P (t))
2635 verify_type (t);
2636 #endif
2637 if (VAR_OR_FUNCTION_DECL_P (t)
2638 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2639 (*trees)[i] = lto_symtab_prevailing_decl (t);
2644 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2645 prevailing one. */
2647 static void
2648 lto_fixup_decls (struct lto_file_decl_data **files)
2650 unsigned int i;
2651 tree t;
2653 if (tree_with_vars)
2654 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2655 lto_fixup_prevailing_decls (t);
2657 for (i = 0; files[i]; i++)
2659 struct lto_file_decl_data *file = files[i];
2660 struct lto_in_decl_state *state = file->global_decl_state;
2661 lto_fixup_state (state);
2663 hash_table<decl_state_hasher>::iterator iter;
2664 lto_in_decl_state *elt;
2665 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2666 lto_in_decl_state *, iter)
2667 lto_fixup_state (elt);
2671 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2673 /* Turn file datas for sub files into a single array, so that they look
2674 like separate files for further passes. */
2676 static void
2677 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2679 struct lto_file_decl_data *n, *next;
2680 int i, k;
2682 lto_stats.num_input_files = count;
2683 all_file_decl_data
2684 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2685 /* Set the hooks so that all of the ipa passes can read in their data. */
2686 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2687 for (i = 0, k = 0; i < last_file_ix; i++)
2689 for (n = orig[i]; n != NULL; n = next)
2691 all_file_decl_data[k++] = n;
2692 next = n->next;
2693 n->next = NULL;
2696 all_file_decl_data[k] = NULL;
2697 gcc_assert (k == count);
2700 /* Input file data before flattening (i.e. splitting them to subfiles to support
2701 incremental linking. */
2702 static int real_file_count;
2703 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2705 static void print_lto_report_1 (void);
2707 /* Read all the symbols from the input files FNAMES. NFILES is the
2708 number of files requested in the command line. Instantiate a
2709 global call graph by aggregating all the sub-graphs found in each
2710 file. */
2712 static void
2713 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2715 unsigned int i, last_file_ix;
2716 FILE *resolution;
2717 int count = 0;
2718 struct lto_file_decl_data **decl_data;
2719 symtab_node *snode;
2721 symtab->initialize ();
2723 timevar_push (TV_IPA_LTO_DECL_IN);
2725 #ifdef ACCEL_COMPILER
2726 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2727 lto_stream_offload_p = true;
2728 #endif
2730 real_file_decl_data
2731 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2732 real_file_count = nfiles;
2734 /* Read the resolution file. */
2735 resolution = NULL;
2736 if (resolution_file_name)
2738 int t;
2739 unsigned num_objects;
2741 resolution = fopen (resolution_file_name, "r");
2742 if (resolution == NULL)
2743 fatal_error (input_location,
2744 "could not open symbol resolution file: %m");
2746 t = fscanf (resolution, "%u", &num_objects);
2747 gcc_assert (t == 1);
2749 /* True, since the plugin splits the archives. */
2750 gcc_assert (num_objects == nfiles);
2752 symtab->state = LTO_STREAMING;
2754 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2755 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2756 gimple_canonical_type_eq, NULL);
2757 gcc_obstack_init (&tree_scc_hash_obstack);
2758 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2760 /* Register the common node types with the canonical type machinery so
2761 we properly share alias-sets across languages and TUs. Do not
2762 expose the common nodes as type merge target - those that should be
2763 are already exposed so by pre-loading the LTO streamer caches.
2764 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2765 for (i = 0; i < itk_none; ++i)
2766 lto_register_canonical_types (integer_types[i], true);
2767 for (i = 0; i < stk_type_kind_last; ++i)
2768 lto_register_canonical_types (sizetype_tab[i], true);
2769 for (i = 0; i < TI_MAX; ++i)
2770 lto_register_canonical_types (global_trees[i], true);
2771 for (i = 0; i < itk_none; ++i)
2772 lto_register_canonical_types (integer_types[i], false);
2773 for (i = 0; i < stk_type_kind_last; ++i)
2774 lto_register_canonical_types (sizetype_tab[i], false);
2775 for (i = 0; i < TI_MAX; ++i)
2776 lto_register_canonical_types (global_trees[i], false);
2778 if (!quiet_flag)
2779 fprintf (stderr, "Reading object files:");
2781 /* Read all of the object files specified on the command line. */
2782 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2784 struct lto_file_decl_data *file_data = NULL;
2785 if (!quiet_flag)
2787 fprintf (stderr, " %s", fnames[i]);
2788 fflush (stderr);
2791 current_lto_file = lto_obj_file_open (fnames[i], false);
2792 if (!current_lto_file)
2793 break;
2795 file_data = lto_file_read (current_lto_file, resolution, &count);
2796 if (!file_data)
2798 lto_obj_file_close (current_lto_file);
2799 free (current_lto_file);
2800 current_lto_file = NULL;
2801 break;
2804 decl_data[last_file_ix++] = file_data;
2806 lto_obj_file_close (current_lto_file);
2807 free (current_lto_file);
2808 current_lto_file = NULL;
2811 lto_flatten_files (decl_data, count, last_file_ix);
2812 lto_stats.num_input_files = count;
2813 ggc_free(decl_data);
2814 real_file_decl_data = NULL;
2816 if (resolution_file_name)
2817 fclose (resolution);
2819 /* Show the LTO report before launching LTRANS. */
2820 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2821 print_lto_report_1 ();
2823 /* Free gimple type merging datastructures. */
2824 delete tree_scc_hash;
2825 tree_scc_hash = NULL;
2826 obstack_free (&tree_scc_hash_obstack, NULL);
2827 htab_delete (gimple_canonical_types);
2828 gimple_canonical_types = NULL;
2829 delete canonical_type_hash_cache;
2830 canonical_type_hash_cache = NULL;
2832 /* At this stage we know that majority of GGC memory is reachable.
2833 Growing the limits prevents unnecesary invocation of GGC. */
2834 ggc_grow ();
2835 ggc_collect ();
2837 /* Set the hooks so that all of the ipa passes can read in their data. */
2838 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2840 timevar_pop (TV_IPA_LTO_DECL_IN);
2842 if (!quiet_flag)
2843 fprintf (stderr, "\nReading the callgraph\n");
2845 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2846 /* Read the symtab. */
2847 input_symtab ();
2849 input_offload_tables ();
2851 /* Store resolutions into the symbol table. */
2853 ld_plugin_symbol_resolution_t *res;
2854 FOR_EACH_SYMBOL (snode)
2855 if (snode->real_symbol_p ()
2856 && snode->lto_file_data
2857 && snode->lto_file_data->resolution_map
2858 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
2859 snode->resolution = *res;
2860 for (i = 0; all_file_decl_data[i]; i++)
2861 if (all_file_decl_data[i]->resolution_map)
2863 delete all_file_decl_data[i]->resolution_map;
2864 all_file_decl_data[i]->resolution_map = NULL;
2867 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2869 if (!quiet_flag)
2870 fprintf (stderr, "Merging declarations\n");
2872 timevar_push (TV_IPA_LTO_DECL_MERGE);
2873 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2874 need to care about resolving symbols again, we only need to replace
2875 duplicated declarations read from the callgraph and from function
2876 sections. */
2877 if (!flag_ltrans)
2879 lto_symtab_merge_decls ();
2881 /* If there were errors during symbol merging bail out, we have no
2882 good way to recover here. */
2883 if (seen_error ())
2884 fatal_error (input_location,
2885 "errors during merging of translation units");
2887 /* Fixup all decls. */
2888 lto_fixup_decls (all_file_decl_data);
2890 if (tree_with_vars)
2891 ggc_free (tree_with_vars);
2892 tree_with_vars = NULL;
2893 ggc_collect ();
2895 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2896 /* Each pass will set the appropriate timer. */
2898 if (!quiet_flag)
2899 fprintf (stderr, "Reading summaries\n");
2901 /* Read the IPA summary data. */
2902 if (flag_ltrans)
2903 ipa_read_optimization_summaries ();
2904 else
2905 ipa_read_summaries ();
2907 for (i = 0; all_file_decl_data[i]; i++)
2909 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2910 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2911 all_file_decl_data[i]->symtab_node_encoder = NULL;
2912 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
2913 all_file_decl_data[i]->global_decl_state = NULL;
2914 all_file_decl_data[i]->current_decl_state = NULL;
2917 /* Finally merge the cgraph according to the decl merging decisions. */
2918 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2919 if (symtab->dump_file)
2921 fprintf (symtab->dump_file, "Before merging:\n");
2922 symtab_node::dump_table (symtab->dump_file);
2924 if (!flag_ltrans)
2926 lto_symtab_merge_symbols ();
2927 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2928 we are still having duplicated comdat groups containing local statics.
2929 We could also just remove them while merging. */
2930 symtab->remove_unreachable_nodes (dump_file);
2932 ggc_collect ();
2933 symtab->state = IPA_SSA;
2934 /* FIXME: Technically all node removals happening here are useless, because
2935 WPA should not stream them. */
2936 if (flag_ltrans)
2937 symtab->remove_unreachable_nodes (dump_file);
2939 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2941 /* Indicate that the cgraph is built and ready. */
2942 symtab->function_flags_ready = true;
2944 ggc_free (all_file_decl_data);
2945 all_file_decl_data = NULL;
2949 /* Materialize all the bodies for all the nodes in the callgraph. */
2951 static void
2952 materialize_cgraph (void)
2954 struct cgraph_node *node;
2955 timevar_id_t lto_timer;
2957 if (!quiet_flag)
2958 fprintf (stderr,
2959 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2962 FOR_EACH_FUNCTION (node)
2964 if (node->lto_file_data)
2966 lto_materialize_function (node);
2967 lto_stats.num_input_cgraph_nodes++;
2972 /* Start the appropriate timer depending on the mode that we are
2973 operating in. */
2974 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2975 : (flag_ltrans) ? TV_WHOPR_LTRANS
2976 : TV_LTO;
2977 timevar_push (lto_timer);
2979 current_function_decl = NULL;
2980 set_cfun (NULL);
2982 if (!quiet_flag)
2983 fprintf (stderr, "\n");
2985 timevar_pop (lto_timer);
2989 /* Show various memory usage statistics related to LTO. */
2990 static void
2991 print_lto_report_1 (void)
2993 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
2994 fprintf (stderr, "%s statistics\n", pfx);
2996 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
2997 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
2998 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
2999 if (flag_wpa && tree_scc_hash)
3001 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3002 "collision ratio: %f\n", pfx,
3003 (long) tree_scc_hash->size (),
3004 (long) tree_scc_hash->elements (),
3005 tree_scc_hash->collisions ());
3006 hash_table<tree_scc_hasher>::iterator hiter;
3007 tree_scc *scc, *max_scc = NULL;
3008 unsigned max_length = 0;
3009 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3011 unsigned length = 0;
3012 tree_scc *s = scc;
3013 for (; s; s = s->next)
3014 length++;
3015 if (length > max_length)
3017 max_length = length;
3018 max_scc = scc;
3021 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3022 pfx, max_length, max_scc->len);
3023 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3024 num_scc_compares, num_scc_compare_collisions,
3025 num_scc_compare_collisions / (double) num_scc_compares);
3026 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3027 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3028 total_scc_size_merged);
3029 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3030 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3031 pfx, num_prevailing_types, num_type_scc_trees);
3032 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3033 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3034 (long) htab_size (gimple_canonical_types),
3035 (long) htab_elements (gimple_canonical_types),
3036 (long) gimple_canonical_types->searches,
3037 (long) gimple_canonical_types->collisions,
3038 htab_collisions (gimple_canonical_types));
3039 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3040 "%lu elements, %ld searches\n", pfx,
3041 num_canonical_type_hash_entries,
3042 num_canonical_type_hash_queries);
3045 print_lto_report (pfx);
3048 /* Perform whole program analysis (WPA) on the callgraph and write out the
3049 optimization plan. */
3051 static void
3052 do_whole_program_analysis (void)
3054 symtab_node *node;
3056 lto_parallelism = 1;
3058 /* TODO: jobserver communicatoin is not supported, yet. */
3059 if (!strcmp (flag_wpa, "jobserver"))
3060 lto_parallelism = -1;
3061 else
3063 lto_parallelism = atoi (flag_wpa);
3064 if (lto_parallelism <= 0)
3065 lto_parallelism = 0;
3068 timevar_start (TV_PHASE_OPT_GEN);
3070 /* Note that since we are in WPA mode, materialize_cgraph will not
3071 actually read in all the function bodies. It only materializes
3072 the decls and cgraph nodes so that analysis can be performed. */
3073 materialize_cgraph ();
3075 /* Reading in the cgraph uses different timers, start timing WPA now. */
3076 timevar_push (TV_WHOPR_WPA);
3078 if (pre_ipa_mem_report)
3080 fprintf (stderr, "Memory consumption before IPA\n");
3081 dump_memory_report (false);
3084 symtab->function_flags_ready = true;
3086 if (symtab->dump_file)
3087 symtab_node::dump_table (symtab->dump_file);
3088 bitmap_obstack_initialize (NULL);
3089 symtab->state = IPA_SSA;
3091 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3093 if (symtab->dump_file)
3095 fprintf (symtab->dump_file, "Optimized ");
3096 symtab_node::dump_table (symtab->dump_file);
3098 #ifdef ENABLE_CHECKING
3099 symtab_node::verify_symtab_nodes ();
3100 #endif
3101 bitmap_obstack_release (NULL);
3103 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3104 timevar_pop (TV_WHOPR_WPA);
3106 timevar_push (TV_WHOPR_PARTITIONING);
3107 if (flag_lto_partition == LTO_PARTITION_1TO1)
3108 lto_1_to_1_map ();
3109 else if (flag_lto_partition == LTO_PARTITION_MAX)
3110 lto_max_map ();
3111 else if (flag_lto_partition == LTO_PARTITION_ONE)
3112 lto_balanced_map (1);
3113 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3114 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3115 else
3116 gcc_unreachable ();
3118 /* Inline summaries are needed for balanced partitioning. Free them now so
3119 the memory can be used for streamer caches. */
3120 inline_free_summary ();
3122 /* AUX pointers are used by partitioning code to bookkeep number of
3123 partitions symbol is in. This is no longer needed. */
3124 FOR_EACH_SYMBOL (node)
3125 node->aux = NULL;
3127 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3129 /* Find out statics that need to be promoted
3130 to globals with hidden visibility because they are accessed from multiple
3131 partitions. */
3132 lto_promote_cross_file_statics ();
3133 timevar_pop (TV_WHOPR_PARTITIONING);
3135 timevar_stop (TV_PHASE_OPT_GEN);
3137 /* Collect a last time - in lto_wpa_write_files we may end up forking
3138 with the idea that this doesn't increase memory usage. So we
3139 absoultely do not want to collect after that. */
3140 ggc_collect ();
3142 timevar_start (TV_PHASE_STREAM_OUT);
3143 if (!quiet_flag)
3145 fprintf (stderr, "\nStreaming out");
3146 fflush (stderr);
3148 lto_wpa_write_files ();
3149 if (!quiet_flag)
3150 fprintf (stderr, "\n");
3151 timevar_stop (TV_PHASE_STREAM_OUT);
3153 if (post_ipa_mem_report)
3155 fprintf (stderr, "Memory consumption after IPA\n");
3156 dump_memory_report (false);
3159 /* Show the LTO report before launching LTRANS. */
3160 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3161 print_lto_report_1 ();
3162 if (mem_report_wpa)
3163 dump_memory_report (true);
3167 static GTY(()) tree lto_eh_personality_decl;
3169 /* Return the LTO personality function decl. */
3171 tree
3172 lto_eh_personality (void)
3174 if (!lto_eh_personality_decl)
3176 /* Use the first personality DECL for our personality if we don't
3177 support multiple ones. This ensures that we don't artificially
3178 create the need for them in a single-language program. */
3179 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3180 lto_eh_personality_decl = first_personality_decl;
3181 else
3182 lto_eh_personality_decl = lhd_gcc_personality ();
3185 return lto_eh_personality_decl;
3188 /* Set the process name based on the LTO mode. */
3190 static void
3191 lto_process_name (void)
3193 if (flag_lto)
3194 setproctitle ("lto1-lto");
3195 if (flag_wpa)
3196 setproctitle ("lto1-wpa");
3197 if (flag_ltrans)
3198 setproctitle ("lto1-ltrans");
3202 /* Initialize the LTO front end. */
3204 static void
3205 lto_init (void)
3207 lto_process_name ();
3208 lto_streamer_hooks_init ();
3209 lto_reader_init ();
3210 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3211 memset (&lto_stats, 0, sizeof (lto_stats));
3212 bitmap_obstack_initialize (NULL);
3213 gimple_register_cfg_hooks ();
3214 #ifndef ACCEL_COMPILER
3215 unsigned char *table
3216 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3217 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3218 table[m] = m;
3219 lto_mode_identity_table = table;
3220 #endif
3224 /* Main entry point for the GIMPLE front end. This front end has
3225 three main personalities:
3227 - LTO (-flto). All the object files on the command line are
3228 loaded in memory and processed as a single translation unit.
3229 This is the traditional link-time optimization behavior.
3231 - WPA (-fwpa). Only the callgraph and summary information for
3232 files in the command file are loaded. A single callgraph
3233 (without function bodies) is instantiated for the whole set of
3234 files. IPA passes are only allowed to analyze the call graph
3235 and make transformation decisions. The callgraph is
3236 partitioned, each partition is written to a new object file
3237 together with the transformation decisions.
3239 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3240 summary files from running again. Since WPA computed summary
3241 information and decided what transformations to apply, LTRANS
3242 simply applies them. */
3244 void
3245 lto_main (void)
3247 /* LTO is called as a front end, even though it is not a front end.
3248 Because it is called as a front end, TV_PHASE_PARSING and
3249 TV_PARSE_GLOBAL are active, and we need to turn them off while
3250 doing LTO. Later we turn them back on so they are active up in
3251 toplev.c. */
3252 timevar_pop (TV_PARSE_GLOBAL);
3253 timevar_stop (TV_PHASE_PARSING);
3255 timevar_start (TV_PHASE_SETUP);
3257 /* Initialize the LTO front end. */
3258 lto_init ();
3260 timevar_stop (TV_PHASE_SETUP);
3261 timevar_start (TV_PHASE_STREAM_IN);
3263 /* Read all the symbols and call graph from all the files in the
3264 command line. */
3265 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3267 timevar_stop (TV_PHASE_STREAM_IN);
3269 if (!seen_error ())
3271 /* If WPA is enabled analyze the whole call graph and create an
3272 optimization plan. Otherwise, read in all the function
3273 bodies and continue with optimization. */
3274 if (flag_wpa)
3275 do_whole_program_analysis ();
3276 else
3278 timevar_start (TV_PHASE_OPT_GEN);
3280 materialize_cgraph ();
3281 if (!flag_ltrans)
3282 lto_promote_statics_nonwpa ();
3284 /* Let the middle end know that we have read and merged all of
3285 the input files. */
3286 symtab->compile ();
3288 timevar_stop (TV_PHASE_OPT_GEN);
3290 /* FIXME lto, if the processes spawned by WPA fail, we miss
3291 the chance to print WPA's report, so WPA will call
3292 print_lto_report before launching LTRANS. If LTRANS was
3293 launched directly by the driver we would not need to do
3294 this. */
3295 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3296 print_lto_report_1 ();
3300 /* Here we make LTO pretend to be a parser. */
3301 timevar_start (TV_PHASE_PARSING);
3302 timevar_push (TV_PARSE_GLOBAL);
3305 #include "gt-lto-lto.h"