[PATCH v4 1/3] RISC-V: Add support for XCVelw extension in CV32E40P
[official-gcc.git] / gcc / lto / lto-common.cc
blobad6e7fde5cee7ad8372b5825e7aa2b16c21e9ece
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2023 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "function.h"
26 #include "bitmap.h"
27 #include "basic-block.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "alloc-pool.h"
32 #include "tree-pass.h"
33 #include "tree-streamer.h"
34 #include "cgraph.h"
35 #include "opts.h"
36 #include "toplev.h"
37 #include "stor-layout.h"
38 #include "symbol-summary.h"
39 #include "tree-vrp.h"
40 #include "ipa-prop.h"
41 #include "common.h"
42 #include "debug.h"
43 #include "lto.h"
44 #include "lto-section-names.h"
45 #include "splay-tree.h"
46 #include "lto-partition.h"
47 #include "context.h"
48 #include "pass_manager.h"
49 #include "ipa-fnsummary.h"
50 #include "ipa-utils.h"
51 #include "gomp-constants.h"
52 #include "lto-symtab.h"
53 #include "stringpool.h"
54 #include "fold-const.h"
55 #include "attribs.h"
56 #include "builtins.h"
57 #include "lto-common.h"
58 #include "tree-pretty-print.h"
59 #include "print-tree.h"
61 /* True when no new types are going to be streamd from the global stream. */
63 static bool type_streaming_finished = false;
65 GTY(()) tree first_personality_decl;
67 /* Returns a hash code for P. */
69 static hashval_t
70 hash_name (const void *p)
72 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
73 return (hashval_t) htab_hash_string (ds->name);
77 /* Returns nonzero if P1 and P2 are equal. */
79 static int
80 eq_name (const void *p1, const void *p2)
82 const struct lto_section_slot *s1
83 = (const struct lto_section_slot *) p1;
84 const struct lto_section_slot *s2
85 = (const struct lto_section_slot *) p2;
87 return strcmp (s1->name, s2->name) == 0;
90 /* Free lto_section_slot. */
92 static void
93 free_with_string (void *arg)
95 struct lto_section_slot *s = (struct lto_section_slot *)arg;
97 free (CONST_CAST (char *, s->name));
98 free (arg);
101 /* Create section hash table. */
103 htab_t
104 lto_obj_create_section_hash_table (void)
106 return htab_create (37, hash_name, eq_name, free_with_string);
109 /* Delete an allocated integer KEY in the splay tree. */
111 static void
112 lto_splay_tree_delete_id (splay_tree_key key)
114 free ((void *) key);
117 /* Compare splay tree node ids A and B. */
119 static int
120 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
122 unsigned HOST_WIDE_INT ai;
123 unsigned HOST_WIDE_INT bi;
125 ai = *(unsigned HOST_WIDE_INT *) a;
126 bi = *(unsigned HOST_WIDE_INT *) b;
128 if (ai < bi)
129 return -1;
130 else if (ai > bi)
131 return 1;
132 return 0;
135 /* Look up splay tree node by ID in splay tree T. */
137 static splay_tree_node
138 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
140 return splay_tree_lookup (t, (splay_tree_key) &id);
143 /* Check if KEY has ID. */
145 static bool
146 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
148 return *(unsigned HOST_WIDE_INT *) key == id;
151 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
152 The ID is allocated separately because we need HOST_WIDE_INTs which may
153 be wider than a splay_tree_key. */
155 static void
156 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
157 struct lto_file_decl_data *file_data)
159 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
160 *idp = id;
161 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
164 /* Create a splay tree. */
166 static splay_tree
167 lto_splay_tree_new (void)
169 return splay_tree_new (lto_splay_tree_compare_ids,
170 lto_splay_tree_delete_id,
171 NULL);
174 /* Decode the content of memory pointed to by DATA in the in decl
175 state object STATE. DATA_IN points to a data_in structure for
176 decoding. Return the address after the decoded object in the
177 input. */
179 static const uint32_t *
180 lto_read_in_decl_state (class data_in *data_in, const uint32_t *data,
181 struct lto_in_decl_state *state)
183 uint32_t ix;
184 tree decl;
185 uint32_t i, j;
187 ix = *data++;
188 state->compressed = ix & 1;
189 ix /= 2;
190 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
191 if (!VAR_OR_FUNCTION_DECL_P (decl))
193 gcc_assert (decl == void_type_node);
194 decl = NULL_TREE;
196 state->fn_decl = decl;
198 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
200 uint32_t size = *data++;
201 vec<tree, va_gc> *decls = NULL;
202 vec_alloc (decls, size);
204 for (j = 0; j < size; j++)
205 vec_safe_push (decls,
206 streamer_tree_cache_get_tree (data_in->reader_cache,
207 data[j]));
209 state->streams[i] = decls;
210 data += size;
213 return data;
217 /* Global canonical type table. */
218 static htab_t gimple_canonical_types;
219 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
220 static unsigned long num_canonical_type_hash_entries;
221 static unsigned long num_canonical_type_hash_queries;
223 /* Types postponed for registration to the canonical type table.
224 During streaming we postpone all TYPE_CXX_ODR_P types so we can alter
225 decide whether there is conflict with non-ODR type or not. */
226 static GTY(()) vec<tree, va_gc> *types_to_register = NULL;
228 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
229 static hashval_t gimple_canonical_type_hash (const void *p);
230 static hashval_t gimple_register_canonical_type_1 (tree t, hashval_t hash);
232 /* Returning a hash value for gimple type TYPE.
234 The hash value returned is equal for types considered compatible
235 by gimple_canonical_types_compatible_p. */
237 static hashval_t
238 hash_canonical_type (tree type)
240 inchash::hash hstate;
241 enum tree_code code;
243 /* We compute alias sets only for types that needs them.
244 Be sure we do not recurse to something else as we cannot hash incomplete
245 types in a way they would have same hash value as compatible complete
246 types. */
247 gcc_checking_assert (type_with_alias_set_p (type));
249 /* Combine a few common features of types so that types are grouped into
250 smaller sets; when searching for existing matching types to merge,
251 only existing types having the same features as the new type will be
252 checked. */
253 code = tree_code_for_canonical_type_merging (TREE_CODE (type));
254 hstate.add_int (code);
255 hstate.add_int (TYPE_MODE (type));
257 /* Incorporate common features of numerical types. */
258 if (INTEGRAL_TYPE_P (type)
259 || SCALAR_FLOAT_TYPE_P (type)
260 || FIXED_POINT_TYPE_P (type)
261 || TREE_CODE (type) == OFFSET_TYPE
262 || POINTER_TYPE_P (type))
264 hstate.add_int (TYPE_PRECISION (type));
265 if (!type_with_interoperable_signedness (type))
266 hstate.add_int (TYPE_UNSIGNED (type));
269 if (VECTOR_TYPE_P (type))
271 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
272 hstate.add_int (TYPE_UNSIGNED (type));
275 if (TREE_CODE (type) == COMPLEX_TYPE)
276 hstate.add_int (TYPE_UNSIGNED (type));
278 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
279 interoperable with "signed char". Unless all frontends are revisited to
280 agree on these types, we must ignore the flag completely. */
282 /* Fortran standard define C_PTR type that is compatible with every
283 C pointer. For this reason we need to glob all pointers into one.
284 Still pointers in different address spaces are not compatible. */
285 if (POINTER_TYPE_P (type))
286 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
288 /* For array types hash the domain bounds and the string flag. */
289 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
291 hstate.add_int (TYPE_STRING_FLAG (type));
292 /* OMP lowering can introduce error_mark_node in place of
293 random local decls in types. */
294 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
295 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
296 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
297 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
300 /* Recurse for aggregates with a single element type. */
301 if (TREE_CODE (type) == ARRAY_TYPE
302 || TREE_CODE (type) == COMPLEX_TYPE
303 || TREE_CODE (type) == VECTOR_TYPE)
304 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
306 /* Incorporate function return and argument types. */
307 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
309 unsigned na;
310 tree p;
312 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
314 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
316 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
317 na++;
320 hstate.add_int (na);
323 if (RECORD_OR_UNION_TYPE_P (type))
325 unsigned nf;
326 tree f;
328 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
329 if (TREE_CODE (f) == FIELD_DECL
330 && (! DECL_SIZE (f)
331 || ! integer_zerop (DECL_SIZE (f))))
333 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
334 nf++;
337 hstate.add_int (nf);
340 return hstate.end();
343 /* Returning a hash value for gimple type TYPE combined with VAL. */
345 static void
346 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
348 hashval_t v;
350 /* All type variants have same TYPE_CANONICAL. */
351 type = TYPE_MAIN_VARIANT (type);
353 if (!canonical_type_used_p (type))
354 v = hash_canonical_type (type);
355 /* An already processed type. */
356 else if (TYPE_CANONICAL (type))
358 type = TYPE_CANONICAL (type);
359 v = gimple_canonical_type_hash (type);
361 else
363 /* Canonical types should not be able to form SCCs by design, this
364 recursion is just because we do not register canonical types in
365 optimal order. To avoid quadratic behavior also register the
366 type here. */
367 v = hash_canonical_type (type);
368 v = gimple_register_canonical_type_1 (type, v);
370 hstate.merge_hash (v);
373 /* Returns the hash for a canonical type P. */
375 static hashval_t
376 gimple_canonical_type_hash (const void *p)
378 num_canonical_type_hash_queries++;
379 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
380 gcc_assert (slot != NULL);
381 return *slot;
386 /* Returns nonzero if P1 and P2 are equal. */
388 static int
389 gimple_canonical_type_eq (const void *p1, const void *p2)
391 const_tree t1 = (const_tree) p1;
392 const_tree t2 = (const_tree) p2;
393 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
394 CONST_CAST_TREE (t2));
397 /* Main worker for gimple_register_canonical_type. */
399 static hashval_t
400 gimple_register_canonical_type_1 (tree t, hashval_t hash)
402 void **slot;
404 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t)
405 && type_with_alias_set_p (t)
406 && canonical_type_used_p (t));
408 /* ODR types for which there is no ODR violation and we did not record
409 structurally equivalent non-ODR type can be treated as unique by their
410 name.
412 hash passed to gimple_register_canonical_type_1 is a structural hash
413 that we can use to lookup structurally equivalent non-ODR type.
414 In case we decide to treat type as unique ODR type we recompute hash based
415 on name and let TBAA machinery know about our decision. */
416 if (RECORD_OR_UNION_TYPE_P (t) && odr_type_p (t)
417 && TYPE_CXX_ODR_P (t) && !odr_type_violation_reported_p (t))
419 /* Anonymous namespace types never conflict with non-C++ types. */
420 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
421 slot = NULL;
422 else
424 /* Here we rely on fact that all non-ODR types was inserted into
425 canonical type hash and thus we can safely detect conflicts between
426 ODR types and interoperable non-ODR types. */
427 gcc_checking_assert (type_streaming_finished
428 && TYPE_MAIN_VARIANT (t) == t);
429 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash,
430 NO_INSERT);
432 if (slot && !TYPE_CXX_ODR_P (*(tree *)slot))
434 tree nonodr = *(tree *)slot;
435 gcc_checking_assert (!flag_ltrans);
436 if (symtab->dump_file)
438 fprintf (symtab->dump_file,
439 "ODR and non-ODR type conflict: ");
440 print_generic_expr (symtab->dump_file, t);
441 fprintf (symtab->dump_file, " and ");
442 print_generic_expr (symtab->dump_file, nonodr);
443 fprintf (symtab->dump_file, " mangled:%s\n",
444 IDENTIFIER_POINTER
445 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
447 /* Set canonical for T and all other ODR equivalent duplicates
448 including incomplete structures. */
449 set_type_canonical_for_odr_type (t, nonodr);
451 else
453 tree prevail = prevailing_odr_type (t);
455 if (symtab->dump_file)
457 fprintf (symtab->dump_file,
458 "New canonical ODR type: ");
459 print_generic_expr (symtab->dump_file, t);
460 fprintf (symtab->dump_file, " mangled:%s\n",
461 IDENTIFIER_POINTER
462 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
464 /* Set canonical for T and all other ODR equivalent duplicates
465 including incomplete structures. */
466 set_type_canonical_for_odr_type (t, prevail);
467 enable_odr_based_tbaa (t);
468 if (!type_in_anonymous_namespace_p (t))
469 hash = htab_hash_string (IDENTIFIER_POINTER
470 (DECL_ASSEMBLER_NAME
471 (TYPE_NAME (t))));
472 else
473 hash = TYPE_UID (t);
475 /* All variants of t now have TYPE_CANONICAL set to prevail.
476 Update canonical type hash cache accordingly. */
477 num_canonical_type_hash_entries++;
478 bool existed_p = canonical_type_hash_cache->put (prevail, hash);
479 gcc_checking_assert (!existed_p);
481 return hash;
484 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
485 if (*slot)
487 tree new_type = (tree)(*slot);
488 gcc_checking_assert (new_type != t);
489 TYPE_CANONICAL (t) = new_type;
491 else
493 TYPE_CANONICAL (t) = t;
494 *slot = (void *) t;
495 /* Cache the just computed hash value. */
496 num_canonical_type_hash_entries++;
497 bool existed_p = canonical_type_hash_cache->put (t, hash);
498 gcc_assert (!existed_p);
500 return hash;
503 /* Register type T in the global type table gimple_types and set
504 TYPE_CANONICAL of T accordingly.
505 This is used by LTO to merge structurally equivalent types for
506 type-based aliasing purposes across different TUs and languages.
508 ??? This merging does not exactly match how the tree.cc middle-end
509 functions will assign TYPE_CANONICAL when new types are created
510 during optimization (which at least happens for pointer and array
511 types). */
513 static void
514 gimple_register_canonical_type (tree t)
516 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t)
517 || !canonical_type_used_p (t))
518 return;
520 /* Canonical types are same among all complete variants. */
521 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
522 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
523 else
525 hashval_t h = hash_canonical_type (TYPE_MAIN_VARIANT (t));
526 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t), h);
527 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
531 /* Re-compute TYPE_CANONICAL for NODE and related types. */
533 static void
534 lto_register_canonical_types (tree node, bool first_p)
536 if (!node
537 || !TYPE_P (node))
538 return;
540 if (first_p)
541 TYPE_CANONICAL (node) = NULL_TREE;
543 if (POINTER_TYPE_P (node)
544 || TREE_CODE (node) == COMPLEX_TYPE
545 || TREE_CODE (node) == ARRAY_TYPE)
546 lto_register_canonical_types (TREE_TYPE (node), first_p);
548 if (!first_p)
549 gimple_register_canonical_type (node);
552 /* Finish canonical type calculation: after all units has been streamed in we
553 can check if given ODR type structurally conflicts with a non-ODR type. In
554 the first case we set type canonical according to the canonical type hash.
555 In the second case we use type names. */
557 static void
558 lto_register_canonical_types_for_odr_types ()
560 tree t;
561 unsigned int i;
563 if (!types_to_register)
564 return;
566 type_streaming_finished = true;
568 /* Be sure that no types derived from ODR types was
569 not inserted into the hash table. */
570 if (flag_checking)
571 FOR_EACH_VEC_ELT (*types_to_register, i, t)
572 gcc_assert (!TYPE_CANONICAL (t));
574 /* Register all remaining types. */
575 FOR_EACH_VEC_ELT (*types_to_register, i, t)
577 /* For pre-streamed types like va-arg it is possible that main variant
578 is !CXX_ODR_P while the variant (which is streamed) is.
579 Copy CXX_ODR_P to make type verifier happy. This is safe because
580 in canonical type calculation we only consider main variants.
581 However we can not change this flag before streaming is finished
582 to not affect tree merging. */
583 TYPE_CXX_ODR_P (t) = TYPE_CXX_ODR_P (TYPE_MAIN_VARIANT (t));
584 if (!TYPE_CANONICAL (t))
585 gimple_register_canonical_type (t);
590 /* Remember trees that contains references to declarations. */
591 vec <tree, va_gc> *tree_with_vars;
593 #define CHECK_VAR(tt) \
594 do \
596 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
597 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
598 return true; \
599 } while (0)
601 #define CHECK_NO_VAR(tt) \
602 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
604 /* Check presence of pointers to decls in fields of a tree_typed T. */
606 static inline bool
607 mentions_vars_p_typed (tree t)
609 CHECK_NO_VAR (TREE_TYPE (t));
610 return false;
613 /* Check presence of pointers to decls in fields of a tree_common T. */
615 static inline bool
616 mentions_vars_p_common (tree t)
618 if (mentions_vars_p_typed (t))
619 return true;
620 CHECK_NO_VAR (TREE_CHAIN (t));
621 return false;
624 /* Check presence of pointers to decls in fields of a decl_minimal T. */
626 static inline bool
627 mentions_vars_p_decl_minimal (tree t)
629 if (mentions_vars_p_common (t))
630 return true;
631 CHECK_NO_VAR (DECL_NAME (t));
632 CHECK_VAR (DECL_CONTEXT (t));
633 return false;
636 /* Check presence of pointers to decls in fields of a decl_common T. */
638 static inline bool
639 mentions_vars_p_decl_common (tree t)
641 if (mentions_vars_p_decl_minimal (t))
642 return true;
643 CHECK_VAR (DECL_SIZE (t));
644 CHECK_VAR (DECL_SIZE_UNIT (t));
645 CHECK_VAR (DECL_INITIAL (t));
646 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
647 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
648 return false;
651 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
653 static inline bool
654 mentions_vars_p_decl_with_vis (tree t)
656 if (mentions_vars_p_decl_common (t))
657 return true;
659 /* Accessor macro has side-effects, use field-name here. */
660 CHECK_NO_VAR (DECL_ASSEMBLER_NAME_RAW (t));
661 return false;
664 /* Check presence of pointers to decls in fields of a decl_non_common T. */
666 static inline bool
667 mentions_vars_p_decl_non_common (tree t)
669 if (mentions_vars_p_decl_with_vis (t))
670 return true;
671 CHECK_NO_VAR (DECL_RESULT_FLD (t));
672 return false;
675 /* Check presence of pointers to decls in fields of a decl_non_common T. */
677 static bool
678 mentions_vars_p_function (tree t)
680 if (mentions_vars_p_decl_non_common (t))
681 return true;
682 CHECK_NO_VAR (DECL_ARGUMENTS (t));
683 CHECK_NO_VAR (DECL_VINDEX (t));
684 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
685 return false;
688 /* Check presence of pointers to decls in fields of a field_decl T. */
690 static bool
691 mentions_vars_p_field_decl (tree t)
693 if (mentions_vars_p_decl_common (t))
694 return true;
695 CHECK_VAR (DECL_FIELD_OFFSET (t));
696 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
697 CHECK_NO_VAR (DECL_QUALIFIER (t));
698 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
699 CHECK_NO_VAR (DECL_FCONTEXT (t));
700 return false;
703 /* Check presence of pointers to decls in fields of a type T. */
705 static bool
706 mentions_vars_p_type (tree t)
708 if (mentions_vars_p_common (t))
709 return true;
710 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
711 CHECK_VAR (TYPE_SIZE (t));
712 CHECK_VAR (TYPE_SIZE_UNIT (t));
713 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
714 CHECK_NO_VAR (TYPE_NAME (t));
716 CHECK_VAR (TYPE_MIN_VALUE_RAW (t));
717 CHECK_VAR (TYPE_MAX_VALUE_RAW (t));
719 /* Accessor is for derived node types only. */
720 CHECK_NO_VAR (TYPE_LANG_SLOT_1 (t));
722 CHECK_VAR (TYPE_CONTEXT (t));
723 CHECK_NO_VAR (TYPE_CANONICAL (t));
724 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
725 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
726 return false;
729 /* Check presence of pointers to decls in fields of a BINFO T. */
731 static bool
732 mentions_vars_p_binfo (tree t)
734 unsigned HOST_WIDE_INT i, n;
736 if (mentions_vars_p_common (t))
737 return true;
738 CHECK_VAR (BINFO_VTABLE (t));
739 CHECK_NO_VAR (BINFO_OFFSET (t));
740 CHECK_NO_VAR (BINFO_VIRTUALS (t));
741 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
742 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
743 for (i = 0; i < n; i++)
744 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
745 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
746 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
747 n = BINFO_N_BASE_BINFOS (t);
748 for (i = 0; i < n; i++)
749 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
750 return false;
753 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
755 static bool
756 mentions_vars_p_constructor (tree t)
758 unsigned HOST_WIDE_INT idx;
759 constructor_elt *ce;
761 if (mentions_vars_p_typed (t))
762 return true;
764 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
766 CHECK_NO_VAR (ce->index);
767 CHECK_VAR (ce->value);
769 return false;
772 /* Check presence of pointers to decls in fields of an expression tree T. */
774 static bool
775 mentions_vars_p_expr (tree t)
777 int i;
778 if (mentions_vars_p_typed (t))
779 return true;
780 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
781 CHECK_VAR (TREE_OPERAND (t, i));
782 return false;
785 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
787 static bool
788 mentions_vars_p_omp_clause (tree t)
790 int i;
791 if (mentions_vars_p_common (t))
792 return true;
793 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
794 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
795 return false;
798 /* Check presence of pointers to decls that needs later fixup in T. */
800 static bool
801 mentions_vars_p (tree t)
803 switch (TREE_CODE (t))
805 case IDENTIFIER_NODE:
806 break;
808 case TREE_LIST:
809 CHECK_VAR (TREE_VALUE (t));
810 CHECK_VAR (TREE_PURPOSE (t));
811 CHECK_NO_VAR (TREE_CHAIN (t));
812 break;
814 case FIELD_DECL:
815 return mentions_vars_p_field_decl (t);
817 case LABEL_DECL:
818 case CONST_DECL:
819 case PARM_DECL:
820 case RESULT_DECL:
821 case IMPORTED_DECL:
822 case NAMESPACE_DECL:
823 case NAMELIST_DECL:
824 return mentions_vars_p_decl_common (t);
826 case VAR_DECL:
827 return mentions_vars_p_decl_with_vis (t);
829 case TYPE_DECL:
830 return mentions_vars_p_decl_non_common (t);
832 case FUNCTION_DECL:
833 return mentions_vars_p_function (t);
835 case TREE_BINFO:
836 return mentions_vars_p_binfo (t);
838 case PLACEHOLDER_EXPR:
839 return mentions_vars_p_common (t);
841 case BLOCK:
842 case TRANSLATION_UNIT_DECL:
843 case OPTIMIZATION_NODE:
844 case TARGET_OPTION_NODE:
845 break;
847 case CONSTRUCTOR:
848 return mentions_vars_p_constructor (t);
850 case OMP_CLAUSE:
851 return mentions_vars_p_omp_clause (t);
853 default:
854 if (TYPE_P (t))
856 if (mentions_vars_p_type (t))
857 return true;
859 else if (EXPR_P (t))
861 if (mentions_vars_p_expr (t))
862 return true;
864 else if (CONSTANT_CLASS_P (t))
865 CHECK_NO_VAR (TREE_TYPE (t));
866 else
867 gcc_unreachable ();
869 return false;
873 /* Return the resolution for the decl with index INDEX from DATA_IN. */
875 static enum ld_plugin_symbol_resolution
876 get_resolution (class data_in *data_in, unsigned index)
878 if (data_in->globals_resolution.exists ())
880 ld_plugin_symbol_resolution_t ret;
881 /* We can have references to not emitted functions in
882 DECL_FUNCTION_PERSONALITY at least. So we can and have
883 to indeed return LDPR_UNKNOWN in some cases. */
884 if (data_in->globals_resolution.length () <= index)
885 return LDPR_UNKNOWN;
886 ret = data_in->globals_resolution[index];
887 return ret;
889 else
890 /* Delay resolution finding until decl merging. */
891 return LDPR_UNKNOWN;
894 /* We need to record resolutions until symbol table is read. */
895 static void
896 register_resolution (struct lto_file_decl_data *file_data, tree decl,
897 enum ld_plugin_symbol_resolution resolution)
899 bool existed;
900 if (resolution == LDPR_UNKNOWN)
901 return;
902 if (!file_data->resolution_map)
903 file_data->resolution_map
904 = new hash_map<tree, ld_plugin_symbol_resolution>;
905 ld_plugin_symbol_resolution_t &res
906 = file_data->resolution_map->get_or_insert (decl, &existed);
907 if (!existed
908 || resolution == LDPR_PREVAILING_DEF_IRONLY
909 || resolution == LDPR_PREVAILING_DEF
910 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
911 res = resolution;
914 /* Register DECL with the global symbol table and change its
915 name if necessary to avoid name clashes for static globals across
916 different files. */
918 static void
919 lto_register_var_decl_in_symtab (class data_in *data_in, tree decl,
920 unsigned ix)
922 tree context;
924 /* Variable has file scope, not local. */
925 if (!TREE_PUBLIC (decl)
926 && !((context = decl_function_context (decl))
927 && auto_var_in_fn_p (decl, context)))
928 rest_of_decl_compilation (decl, 1, 0);
930 /* If this variable has already been declared, queue the
931 declaration for merging. */
932 if (TREE_PUBLIC (decl))
933 register_resolution (data_in->file_data,
934 decl, get_resolution (data_in, ix));
938 /* Register DECL with the global symbol table and change its
939 name if necessary to avoid name clashes for static globals across
940 different files. DATA_IN contains descriptors and tables for the
941 file being read. */
943 static void
944 lto_register_function_decl_in_symtab (class data_in *data_in, tree decl,
945 unsigned ix)
947 /* If this variable has already been declared, queue the
948 declaration for merging. */
949 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
950 register_resolution (data_in->file_data,
951 decl, get_resolution (data_in, ix));
954 /* Check if T is a decl and needs register its resolution info. */
956 static void
957 lto_maybe_register_decl (class data_in *data_in, tree t, unsigned ix)
959 if (VAR_P (t))
960 lto_register_var_decl_in_symtab (data_in, t, ix);
961 else if (TREE_CODE (t) == FUNCTION_DECL
962 && !fndecl_built_in_p (t))
963 lto_register_function_decl_in_symtab (data_in, t, ix);
967 /* For the type T re-materialize it in the type variant list and
968 the pointer/reference-to chains. */
970 static void
971 lto_fixup_prevailing_type (tree t)
973 /* The following re-creates proper variant lists while fixing up
974 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
975 variant list state before fixup is broken. */
977 /* If we are not our own variant leader link us into our new leaders
978 variant list. */
979 if (TYPE_MAIN_VARIANT (t) != t)
981 tree mv = TYPE_MAIN_VARIANT (t);
982 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
983 TYPE_NEXT_VARIANT (mv) = t;
985 else if (!TYPE_ATTRIBUTES (t))
987 /* The following reconstructs the pointer chains
988 of the new pointed-to type if we are a main variant. We do
989 not stream those so they are broken before fixup.
990 Don't add it if despite being main variant it has
991 attributes (then it was created with build_distinct_type_copy).
992 Similarly don't add TYPE_REF_IS_RVALUE REFERENCE_TYPEs.
993 Don't add it if there is something in the chain already. */
994 if (TREE_CODE (t) == POINTER_TYPE)
996 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
997 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
999 else if (TREE_CODE (t) == REFERENCE_TYPE && !TYPE_REF_IS_RVALUE (t))
1001 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1002 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1008 /* We keep prevailing tree SCCs in a hashtable with manual collision
1009 handling (in case all hashes compare the same) and keep the colliding
1010 entries in the tree_scc->next chain. */
1012 struct tree_scc
1014 tree_scc *next;
1015 /* Hash of the whole SCC. */
1016 hashval_t hash;
1017 /* Number of trees in the SCC. */
1018 unsigned len;
1019 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1020 which share the same individual tree hash). */
1021 unsigned entry_len;
1022 /* The members of the SCC.
1023 We only need to remember the first entry node candidate for prevailing
1024 SCCs (but of course have access to all entries for SCCs we are
1025 processing).
1026 ??? For prevailing SCCs we really only need hash and the first
1027 entry candidate, but that's too awkward to implement. */
1028 tree entries[1];
1031 struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
1033 static inline hashval_t hash (const tree_scc *);
1034 static inline bool equal (const tree_scc *, const tree_scc *);
1037 hashval_t
1038 tree_scc_hasher::hash (const tree_scc *scc)
1040 return scc->hash;
1043 bool
1044 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
1046 if (scc1->hash != scc2->hash
1047 || scc1->len != scc2->len
1048 || scc1->entry_len != scc2->entry_len)
1049 return false;
1050 return true;
1053 static hash_table<tree_scc_hasher> *tree_scc_hash;
1054 static struct obstack tree_scc_hash_obstack;
1056 static unsigned long num_merged_types;
1057 static unsigned long num_prevailing_types;
1058 static unsigned long num_type_scc_trees;
1059 static unsigned long total_scc_size;
1060 static unsigned long num_sccs_read;
1061 static unsigned long num_unshared_trees_read;
1062 static unsigned long total_scc_size_merged;
1063 static unsigned long num_sccs_merged;
1064 static unsigned long num_scc_compares;
1065 static unsigned long num_scc_compare_collisions;
1068 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1069 recursing through in-SCC tree edges. Returns true if the SCCs entered
1070 through T1 and T2 are equal and fills in *MAP with the pairs of
1071 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1073 static bool
1074 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1076 enum tree_code code;
1078 /* Mark already visited nodes. */
1079 TREE_ASM_WRITTEN (t2) = 1;
1081 /* Push the pair onto map. */
1082 (*map)[0] = t1;
1083 (*map)[1] = t2;
1084 *map = *map + 2;
1086 /* Compare value-fields. */
1087 #define compare_values(X) \
1088 do { \
1089 if (X(t1) != X(t2)) \
1090 return false; \
1091 } while (0)
1093 compare_values (TREE_CODE);
1094 code = TREE_CODE (t1);
1096 /* If we end up comparing translation unit decls we either forgot to mark
1097 some SCC as local or we compare too much. */
1098 gcc_checking_assert (code != TRANSLATION_UNIT_DECL);
1100 if (!TYPE_P (t1))
1102 compare_values (TREE_SIDE_EFFECTS);
1103 compare_values (TREE_CONSTANT);
1104 compare_values (TREE_READONLY);
1105 compare_values (TREE_PUBLIC);
1107 compare_values (TREE_ADDRESSABLE);
1108 compare_values (TREE_THIS_VOLATILE);
1109 if (DECL_P (t1))
1110 compare_values (DECL_UNSIGNED);
1111 else if (TYPE_P (t1))
1112 compare_values (TYPE_UNSIGNED);
1113 if (TYPE_P (t1))
1114 compare_values (TYPE_ARTIFICIAL);
1115 else
1116 compare_values (TREE_NO_WARNING);
1117 compare_values (TREE_NOTHROW);
1118 compare_values (TREE_STATIC);
1119 if (code != TREE_BINFO)
1120 compare_values (TREE_PRIVATE);
1121 compare_values (TREE_PROTECTED);
1122 compare_values (TREE_DEPRECATED);
1123 if (TYPE_P (t1))
1125 if (AGGREGATE_TYPE_P (t1))
1126 compare_values (TYPE_REVERSE_STORAGE_ORDER);
1127 else
1128 compare_values (TYPE_SATURATING);
1129 compare_values (TYPE_ADDR_SPACE);
1131 else if (code == SSA_NAME)
1132 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1134 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1136 if (wi::to_wide (t1) != wi::to_wide (t2))
1137 return false;
1140 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1142 /* ??? No suitable compare routine available. */
1143 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1144 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1145 if (r1.cl != r2.cl
1146 || r1.decimal != r2.decimal
1147 || r1.sign != r2.sign
1148 || r1.signalling != r2.signalling
1149 || r1.canonical != r2.canonical
1150 || r1.uexp != r2.uexp)
1151 return false;
1152 for (unsigned i = 0; i < SIGSZ; ++i)
1153 if (r1.sig[i] != r2.sig[i])
1154 return false;
1157 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1158 if (!fixed_compare (EQ_EXPR,
1159 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1160 return false;
1162 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1164 compare_values (VECTOR_CST_LOG2_NPATTERNS);
1165 compare_values (VECTOR_CST_NELTS_PER_PATTERN);
1168 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1170 compare_values (DECL_MODE);
1171 compare_values (DECL_NONLOCAL);
1172 compare_values (DECL_VIRTUAL_P);
1173 compare_values (DECL_IGNORED_P);
1174 compare_values (DECL_ABSTRACT_P);
1175 compare_values (DECL_ARTIFICIAL);
1176 compare_values (DECL_USER_ALIGN);
1177 compare_values (DECL_PRESERVE_P);
1178 compare_values (DECL_EXTERNAL);
1179 compare_values (DECL_NOT_GIMPLE_REG_P);
1180 compare_values (DECL_ALIGN);
1181 if (code == LABEL_DECL)
1183 compare_values (EH_LANDING_PAD_NR);
1184 compare_values (LABEL_DECL_UID);
1186 else if (code == FIELD_DECL)
1188 compare_values (DECL_PACKED);
1189 compare_values (DECL_NONADDRESSABLE_P);
1190 compare_values (DECL_PADDING_P);
1191 compare_values (DECL_FIELD_ABI_IGNORED);
1192 compare_values (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD);
1193 compare_values (DECL_OFFSET_ALIGN);
1194 compare_values (DECL_NOT_FLEXARRAY);
1196 else if (code == VAR_DECL)
1198 compare_values (DECL_HAS_DEBUG_EXPR_P);
1199 compare_values (DECL_NONLOCAL_FRAME);
1201 if (code == RESULT_DECL
1202 || code == PARM_DECL
1203 || code == VAR_DECL)
1205 compare_values (DECL_BY_REFERENCE);
1206 if (code == VAR_DECL
1207 || code == PARM_DECL)
1208 compare_values (DECL_HAS_VALUE_EXPR_P);
1212 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1213 compare_values (DECL_REGISTER);
1215 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1217 compare_values (DECL_COMMON);
1218 compare_values (DECL_DLLIMPORT_P);
1219 compare_values (DECL_WEAK);
1220 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1221 compare_values (DECL_COMDAT);
1222 compare_values (DECL_VISIBILITY);
1223 compare_values (DECL_VISIBILITY_SPECIFIED);
1224 if (code == VAR_DECL)
1226 compare_values (DECL_HARD_REGISTER);
1227 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1228 compare_values (DECL_IN_CONSTANT_POOL);
1232 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1234 compare_values (DECL_BUILT_IN_CLASS);
1235 compare_values (DECL_STATIC_CONSTRUCTOR);
1236 compare_values (DECL_STATIC_DESTRUCTOR);
1237 compare_values (DECL_UNINLINABLE);
1238 compare_values (DECL_POSSIBLY_INLINED);
1239 compare_values (DECL_IS_NOVOPS);
1240 compare_values (DECL_IS_RETURNS_TWICE);
1241 compare_values (DECL_IS_MALLOC);
1242 compare_values (FUNCTION_DECL_DECL_TYPE);
1243 compare_values (DECL_DECLARED_INLINE_P);
1244 compare_values (DECL_STATIC_CHAIN);
1245 compare_values (DECL_NO_INLINE_WARNING_P);
1246 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1247 compare_values (DECL_NO_LIMIT_STACK);
1248 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1249 compare_values (DECL_PURE_P);
1250 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1251 compare_values (DECL_IS_REPLACEABLE_OPERATOR);
1252 compare_values (DECL_FINAL_P);
1253 compare_values (DECL_CXX_CONSTRUCTOR_P);
1254 compare_values (DECL_CXX_DESTRUCTOR_P);
1255 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1256 compare_values (DECL_UNCHECKED_FUNCTION_CODE);
1259 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1261 compare_values (TYPE_MODE);
1262 compare_values (TYPE_NEEDS_CONSTRUCTING);
1263 if (RECORD_OR_UNION_TYPE_P (t1))
1265 compare_values (TYPE_TRANSPARENT_AGGR);
1266 compare_values (TYPE_FINAL_P);
1267 compare_values (TYPE_CXX_ODR_P);
1269 else if (code == ARRAY_TYPE)
1270 compare_values (TYPE_NONALIASED_COMPONENT);
1271 if (code == ARRAY_TYPE || code == INTEGER_TYPE)
1272 compare_values (TYPE_STRING_FLAG);
1273 if (AGGREGATE_TYPE_P (t1))
1274 compare_values (TYPE_TYPELESS_STORAGE);
1275 compare_values (TYPE_EMPTY_P);
1276 if (FUNC_OR_METHOD_TYPE_P (t1))
1277 compare_values (TYPE_NO_NAMED_ARGS_STDARG_P);
1278 if (RECORD_OR_UNION_TYPE_P (t1))
1279 compare_values (TYPE_INCLUDES_FLEXARRAY);
1280 compare_values (TYPE_PACKED);
1281 compare_values (TYPE_RESTRICT);
1282 compare_values (TYPE_USER_ALIGN);
1283 compare_values (TYPE_READONLY);
1284 compare_values (TYPE_PRECISION_RAW);
1285 compare_values (TYPE_ALIGN);
1286 /* Do not compare TYPE_ALIAS_SET. Doing so introduce ordering issues
1287 with calls to get_alias_set which may initialize it for streamed
1288 in types. */
1291 /* We don't want to compare locations, so there is nothing do compare
1292 for TS_EXP. */
1294 /* BLOCKs are function local and we don't merge anything there, so
1295 simply refuse to merge. */
1296 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1297 return false;
1299 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1300 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1301 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1302 return false;
1304 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1305 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1306 return false;
1308 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1309 if (!cl_optimization_option_eq (TREE_OPTIMIZATION (t1),
1310 TREE_OPTIMIZATION (t2)))
1311 return false;
1313 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1314 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1315 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1316 return false;
1318 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1320 compare_values (CLOBBER_KIND);
1321 compare_values (CONSTRUCTOR_NELTS);
1324 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1325 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1326 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1327 IDENTIFIER_LENGTH (t1)) != 0)
1328 return false;
1330 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1331 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1332 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1333 TREE_STRING_LENGTH (t1)) != 0)
1334 return false;
1336 if (code == OMP_CLAUSE)
1338 compare_values (OMP_CLAUSE_CODE);
1339 switch (OMP_CLAUSE_CODE (t1))
1341 case OMP_CLAUSE_DEFAULT:
1342 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1343 break;
1344 case OMP_CLAUSE_SCHEDULE:
1345 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1346 break;
1347 case OMP_CLAUSE_DEPEND:
1348 compare_values (OMP_CLAUSE_DEPEND_KIND);
1349 break;
1350 case OMP_CLAUSE_MAP:
1351 compare_values (OMP_CLAUSE_MAP_KIND);
1352 break;
1353 case OMP_CLAUSE_PROC_BIND:
1354 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1355 break;
1356 case OMP_CLAUSE_REDUCTION:
1357 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1358 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1359 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1360 break;
1361 default:
1362 break;
1366 #undef compare_values
1369 /* Compare pointer fields. */
1371 /* Recurse. Search & Replaced from DFS_write_tree_body.
1372 Folding the early checks into the compare_tree_edges recursion
1373 macro makes debugging way quicker as you are able to break on
1374 compare_tree_sccs_1 and simply finish until a call returns false
1375 to spot the SCC members with the difference. */
1376 #define compare_tree_edges(E1, E2) \
1377 do { \
1378 tree t1_ = (E1), t2_ = (E2); \
1379 if (t1_ != t2_ \
1380 && (!t1_ || !t2_ \
1381 || !TREE_VISITED (t2_) \
1382 || (!TREE_ASM_WRITTEN (t2_) \
1383 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1384 return false; \
1385 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1386 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1387 } while (0)
1389 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1391 if (code != IDENTIFIER_NODE)
1392 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1395 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1397 /* Note that the number of elements for EXPR has already been emitted
1398 in EXPR's header (see streamer_write_tree_header). */
1399 unsigned int count = vector_cst_encoded_nelts (t1);
1400 for (unsigned int i = 0; i < count; ++i)
1401 compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
1402 VECTOR_CST_ENCODED_ELT (t2, i));
1405 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1407 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1408 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1411 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1413 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1414 /* ??? Global decls from different TUs have non-matching
1415 TRANSLATION_UNIT_DECLs. Only consider a small set of
1416 decls equivalent, we should not end up merging others. */
1417 if ((code == TYPE_DECL
1418 || code == NAMESPACE_DECL
1419 || code == IMPORTED_DECL
1420 || code == CONST_DECL
1421 || (VAR_OR_FUNCTION_DECL_P (t1)
1422 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1423 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1425 else
1426 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1429 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1431 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1432 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1433 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1434 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1), DECL_ABSTRACT_ORIGIN (t2));
1435 if ((code == VAR_DECL
1436 || code == PARM_DECL)
1437 && DECL_HAS_VALUE_EXPR_P (t1))
1438 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1439 if (code == VAR_DECL
1440 && DECL_HAS_DEBUG_EXPR_P (t1))
1441 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1442 /* LTO specific edges. */
1443 if (code != FUNCTION_DECL
1444 && code != TRANSLATION_UNIT_DECL)
1445 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1448 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1450 if (code == FUNCTION_DECL)
1452 tree a1, a2;
1453 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1454 a1 || a2;
1455 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1456 compare_tree_edges (a1, a2);
1457 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1459 else if (code == TYPE_DECL)
1460 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1463 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1465 /* Make sure we don't inadvertently set the assembler name. */
1466 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1467 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1468 DECL_ASSEMBLER_NAME (t2));
1471 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1473 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1474 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1475 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1476 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1477 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1478 DECL_FIELD_BIT_OFFSET (t2));
1479 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1482 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1484 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1485 DECL_FUNCTION_PERSONALITY (t2));
1486 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1487 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1488 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1489 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1490 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1493 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1495 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1496 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1497 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1498 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1499 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1500 reconstructed during fixup. */
1501 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1502 during fixup. */
1503 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1504 /* ??? Global types from different TUs have non-matching
1505 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1506 equal. */
1507 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1509 else
1510 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1511 /* TYPE_CANONICAL is re-computed during type merging, so do not
1512 compare it here. */
1513 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1516 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1518 if (code == ARRAY_TYPE)
1519 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1520 else if (RECORD_OR_UNION_TYPE_P (t1))
1522 tree f1, f2;
1523 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1524 f1 || f2;
1525 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1526 compare_tree_edges (f1, f2);
1528 else if (code == FUNCTION_TYPE
1529 || code == METHOD_TYPE)
1530 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1532 if (!POINTER_TYPE_P (t1))
1533 compare_tree_edges (TYPE_MIN_VALUE_RAW (t1), TYPE_MIN_VALUE_RAW (t2));
1534 compare_tree_edges (TYPE_MAX_VALUE_RAW (t1), TYPE_MAX_VALUE_RAW (t2));
1537 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1539 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1540 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1541 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1544 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1545 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1546 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1548 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1550 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1551 compare_tree_edges (TREE_OPERAND (t1, i),
1552 TREE_OPERAND (t2, i));
1554 /* BLOCKs are function local and we don't merge anything there. */
1555 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1556 return false;
1559 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1561 unsigned i;
1562 tree t;
1563 /* Lengths have already been compared above. */
1564 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1565 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1566 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1567 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1568 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1569 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1570 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1571 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1572 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1575 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1577 unsigned i;
1578 tree index, value;
1579 /* Lengths have already been compared above. */
1580 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1582 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1583 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1587 if (code == OMP_CLAUSE)
1589 int i;
1591 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1592 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1593 OMP_CLAUSE_OPERAND (t2, i));
1594 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1597 #undef compare_tree_edges
1599 return true;
1602 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1603 out MAP if they are equal. */
1605 static bool
1606 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1607 tree *map)
1609 /* Assume SCC entry hashes are sorted after their cardinality. Which
1610 means we can simply take the first n-tuple of equal hashes
1611 (which is recorded as entry_len) and do n SCC entry candidate
1612 comparisons. */
1613 for (unsigned i = 0; i < pscc->entry_len; ++i)
1615 tree *mapp = map;
1616 num_scc_compare_collisions++;
1617 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1619 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1620 on the scc as all trees will be freed. */
1621 return true;
1623 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1624 the SCC prevails. */
1625 for (unsigned j = 0; j < scc->len; ++j)
1626 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1629 return false;
1632 /* QSort sort function to sort a map of two pointers after the 2nd
1633 pointer. */
1635 static int
1636 cmp_tree (const void *p1_, const void *p2_)
1638 tree *p1 = (tree *)(const_cast<void *>(p1_));
1639 tree *p2 = (tree *)(const_cast<void *>(p2_));
1640 if (p1[1] == p2[1])
1641 return 0;
1642 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1645 /* New scc of size 1 containing T was streamed in from DATA_IN and not merged.
1646 Register it to reader cache at index FROM. */
1648 static void
1649 process_dref (class data_in *data_in, tree t, unsigned from)
1651 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1652 /* If we got a debug reference queued, see if the prevailing
1653 tree has a debug reference and if not, register the one
1654 for the tree we are about to throw away. */
1655 if (dref_queue.length () == 1)
1657 dref_entry e = dref_queue.pop ();
1658 gcc_assert (e.decl
1659 == streamer_tree_cache_get_tree (cache, from));
1660 const char *sym;
1661 unsigned HOST_WIDE_INT off;
1662 if (!debug_hooks->die_ref_for_decl (t, &sym, &off))
1663 debug_hooks->register_external_die (t, e.sym, e.off);
1667 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1668 hash value SCC_HASH with an already recorded SCC. Return true if
1669 that was successful, otherwise return false. */
1671 static bool
1672 unify_scc (class data_in *data_in, unsigned from,
1673 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1675 bool unified_p = false;
1676 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1677 tree_scc *scc
1678 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1679 scc->next = NULL;
1680 scc->hash = scc_hash;
1681 scc->len = len;
1682 scc->entry_len = scc_entry_len;
1683 for (unsigned i = 0; i < len; ++i)
1685 tree t = streamer_tree_cache_get_tree (cache, from + i);
1686 scc->entries[i] = t;
1687 /* These types should be streamed as unshared. */
1688 gcc_checking_assert
1689 (!(TREE_CODE (t) == TRANSLATION_UNIT_DECL
1690 || (VAR_OR_FUNCTION_DECL_P (t)
1691 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1692 || TREE_CODE (t) == LABEL_DECL
1693 || (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAME (t))
1694 || (TYPE_P (t)
1695 && type_with_linkage_p (TYPE_MAIN_VARIANT (t))
1696 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t)))));
1699 /* Look for the list of candidate SCCs to compare against. */
1700 tree_scc **slot;
1701 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1702 if (*slot)
1704 /* Try unifying against each candidate. */
1705 num_scc_compares++;
1707 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1708 outside of the scc when following tree edges. Make sure
1709 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1710 to track whether we visited the SCC member during the compare.
1711 We cannot use TREE_VISITED on the pscc members as the extended
1712 scc and pscc can overlap. */
1713 for (unsigned i = 0; i < scc->len; ++i)
1715 TREE_VISITED (scc->entries[i]) = 1;
1716 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1719 tree *map = XALLOCAVEC (tree, 2 * len);
1720 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1722 if (!compare_tree_sccs (pscc, scc, map))
1723 continue;
1725 /* Found an equal SCC. */
1726 unified_p = true;
1727 num_scc_compare_collisions--;
1728 num_sccs_merged++;
1729 total_scc_size_merged += len;
1731 if (flag_checking)
1732 for (unsigned i = 0; i < len; ++i)
1734 tree t = map[2*i+1];
1735 enum tree_code code = TREE_CODE (t);
1736 /* IDENTIFIER_NODEs should be singletons and are merged by the
1737 streamer. The others should be singletons, too, and we
1738 should not merge them in any way. */
1739 gcc_assert (code != TRANSLATION_UNIT_DECL
1740 && code != IDENTIFIER_NODE);
1743 /* Fixup the streamer cache with the prevailing nodes according
1744 to the tree node mapping computed by compare_tree_sccs. */
1745 if (len == 1)
1747 process_dref (data_in, pscc->entries[0], from);
1748 lto_maybe_register_decl (data_in, pscc->entries[0], from);
1749 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1751 else
1753 tree *map2 = XALLOCAVEC (tree, 2 * len);
1754 for (unsigned i = 0; i < len; ++i)
1756 map2[i*2] = (tree)(uintptr_t)(from + i);
1757 map2[i*2+1] = scc->entries[i];
1759 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1760 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1761 for (unsigned i = 0; i < len; ++i)
1763 lto_maybe_register_decl (data_in, map[2*i],
1764 (uintptr_t)map2[2*i]);
1765 streamer_tree_cache_replace_tree (cache, map[2*i],
1766 (uintptr_t)map2[2*i]);
1770 /* Free the tree nodes from the read SCC. */
1771 data_in->location_cache.revert_location_cache ();
1772 for (unsigned i = 0; i < len; ++i)
1774 if (TYPE_P (scc->entries[i]))
1775 num_merged_types++;
1776 free_node (scc->entries[i]);
1779 /* Drop DIE references.
1780 ??? Do as in the size-one SCC case which involves sorting
1781 the queue. */
1782 dref_queue.truncate (0);
1784 break;
1787 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1788 if (!unified_p)
1789 for (unsigned i = 0; i < scc->len; ++i)
1790 TREE_VISITED (scc->entries[i]) = 0;
1793 /* If we didn't unify it to any candidate duplicate the relevant
1794 pieces to permanent storage and link it into the chain. */
1795 if (!unified_p)
1797 tree_scc *pscc
1798 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1799 memcpy (pscc, scc, sizeof (tree_scc));
1800 pscc->next = (*slot);
1801 *slot = pscc;
1803 return unified_p;
1806 typedef int_hash<unsigned, 0, UINT_MAX> code_id_hash;
1808 /* Do registering necessary once new tree fully streamed in (including all
1809 trees it reffers to). */
1811 static void
1812 process_new_tree (tree t, hash_map <code_id_hash, unsigned> *hm,
1813 unsigned index, unsigned *total, class data_in *data_in)
1815 /* Reconstruct the type variant and pointer-to/reference-to
1816 chains. */
1817 if (TYPE_P (t))
1819 /* Map the tree types to their frequencies. */
1820 if (flag_lto_dump_type_stats)
1822 unsigned key = (unsigned) TREE_CODE (t);
1823 unsigned *countp = hm->get (key);
1824 hm->put (key, countp ? (*countp) + 1 : 1);
1825 (*total)++;
1828 num_prevailing_types++;
1829 lto_fixup_prevailing_type (t);
1831 /* Compute the canonical type of all non-ODR types.
1832 Delay ODR types for the end of merging process - the canonical
1833 type for those can be computed using the (unique) name however
1834 we want to do this only if units in other languages do not
1835 contain structurally equivalent type.
1837 Because SCC components are streamed in random (hash) order
1838 we may have encountered the type before while registering
1839 type canonical of a derived type in the same SCC. */
1840 if (!TYPE_CANONICAL (t))
1842 if (!RECORD_OR_UNION_TYPE_P (t)
1843 || !TYPE_CXX_ODR_P (t))
1844 gimple_register_canonical_type (t);
1845 else if (COMPLETE_TYPE_P (t))
1846 vec_safe_push (types_to_register, t);
1848 if (TYPE_MAIN_VARIANT (t) == t && odr_type_p (t))
1849 register_odr_type (t);
1851 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1852 type which is also member of this SCC. */
1853 if (TREE_CODE (t) == INTEGER_CST
1854 && !TREE_OVERFLOW (t))
1855 cache_integer_cst (t);
1856 if (!flag_ltrans)
1858 lto_maybe_register_decl (data_in, t, index);
1859 /* Scan the tree for references to global functions or
1860 variables and record those for later fixup. */
1861 if (mentions_vars_p (t))
1862 vec_safe_push (tree_with_vars, t);
1866 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1867 RESOLUTIONS is the set of symbols picked by the linker (read from the
1868 resolution file when the linker plugin is being used). */
1870 static void
1871 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1872 vec<ld_plugin_symbol_resolution_t> resolutions)
1874 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1875 const int decl_offset = sizeof (struct lto_decl_header);
1876 const int main_offset = decl_offset + header->decl_state_size;
1877 const int string_offset = main_offset + header->main_size;
1878 class data_in *data_in;
1879 unsigned int i;
1880 const uint32_t *data_ptr, *data_end;
1881 uint32_t num_decl_states;
1883 lto_input_block ib_main ((const char *) data + main_offset,
1884 header->main_size, decl_data);
1886 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1887 header->string_size, resolutions);
1889 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1890 internal types that should not be merged. */
1892 hash_map <code_id_hash, unsigned> hm;
1893 unsigned total = 0;
1895 /* Read the global declarations and types. */
1896 while (ib_main.p < ib_main.len)
1898 tree t;
1899 unsigned from = data_in->reader_cache->nodes.length ();
1900 /* Read and uniquify SCCs as in the input stream. */
1901 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1902 if (tag == LTO_tree_scc || tag == LTO_trees)
1904 unsigned len_;
1905 unsigned scc_entry_len;
1907 /* Because we stream in SCC order we know that all unshared trees
1908 are now fully streamed. Process them. */
1909 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1910 &scc_entry_len,
1911 tag == LTO_tree_scc);
1912 unsigned len = data_in->reader_cache->nodes.length () - from;
1913 gcc_assert (len == len_);
1915 if (tag == LTO_tree_scc)
1917 total_scc_size += len;
1918 num_sccs_read++;
1920 else
1921 num_unshared_trees_read += len;
1923 /* We have the special case of size-1 SCCs that are pre-merged
1924 by means of identifier and string sharing for example.
1925 ??? Maybe we should avoid streaming those as SCCs. */
1926 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1927 from);
1928 /* Identifier and integers are shared specially, they should never
1929 go by the tree merging path. */
1930 gcc_checking_assert ((TREE_CODE (first) != IDENTIFIER_NODE
1931 && (TREE_CODE (first) != INTEGER_CST
1932 || TREE_OVERFLOW (first)))
1933 || len != 1);
1935 /* Try to unify the SCC with already existing ones. */
1936 if (!flag_ltrans && tag != LTO_trees
1937 && unify_scc (data_in, from,
1938 len, scc_entry_len, scc_hash))
1939 continue;
1941 /* Tree merging failed, mark entries in location cache as
1942 permanent. */
1943 data_in->location_cache.accept_location_cache ();
1945 bool seen_type = false;
1946 for (unsigned i = 0; i < len; ++i)
1948 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1949 from + i);
1950 process_new_tree (t, &hm, from + i, &total, data_in);
1951 if (TYPE_P (t))
1952 seen_type = true;
1955 /* Register DECLs with the debuginfo machinery. */
1956 while (!dref_queue.is_empty ())
1958 dref_entry e = dref_queue.pop ();
1959 debug_hooks->register_external_die (e.decl, e.sym, e.off);
1962 if (seen_type)
1963 num_type_scc_trees += len;
1965 else
1967 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1968 gcc_assert (data_in->reader_cache->nodes.length () == from + 1);
1969 num_unshared_trees_read++;
1970 data_in->location_cache.accept_location_cache ();
1971 process_dref (data_in, t, from);
1972 if (TREE_CODE (t) == IDENTIFIER_NODE
1973 || (TREE_CODE (t) == INTEGER_CST
1974 && !TREE_OVERFLOW (t)))
1976 else
1978 lto_maybe_register_decl (data_in, t, from);
1979 process_new_tree (t, &hm, from, &total, data_in);
1984 /* Dump type statistics. */
1985 if (flag_lto_dump_type_stats)
1987 fprintf (stdout, " Type Frequency Percentage\n\n");
1988 for (hash_map<code_id_hash, unsigned>::iterator itr = hm.begin ();
1989 itr != hm.end ();
1990 ++itr)
1992 std::pair<unsigned, unsigned> p = *itr;
1993 enum tree_code code = (enum tree_code) p.first;
1994 fprintf (stdout, "%14s %6d %12.2f\n", get_tree_code_name (code),
1995 p.second, float (p.second)/total*100);
1999 data_in->location_cache.apply_location_cache ();
2001 /* Read in lto_in_decl_state objects. */
2002 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
2003 data_end
2004 = (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
2005 num_decl_states = *data_ptr++;
2007 gcc_assert (num_decl_states > 0);
2008 decl_data->global_decl_state = lto_new_in_decl_state ();
2009 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
2010 decl_data->global_decl_state);
2012 /* Read in per-function decl states and enter them in hash table. */
2013 decl_data->function_decl_states
2014 = hash_table<decl_state_hasher>::create_ggc (37);
2016 for (i = 1; i < num_decl_states; i++)
2018 struct lto_in_decl_state *state = lto_new_in_decl_state ();
2020 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
2021 lto_in_decl_state **slot
2022 = decl_data->function_decl_states->find_slot (state, INSERT);
2023 gcc_assert (*slot == NULL);
2024 *slot = state;
2027 if (data_ptr != data_end)
2028 internal_error ("bytecode stream: garbage at the end of symbols section");
2030 /* Set the current decl state to be the global state. */
2031 decl_data->current_decl_state = decl_data->global_decl_state;
2033 lto_data_in_delete (data_in);
2036 /* Custom version of strtoll, which is not portable. */
2038 static int64_t
2039 lto_parse_hex (const char *p)
2041 int64_t ret = 0;
2043 for (; *p != '\0'; ++p)
2045 char c = *p;
2046 unsigned char part;
2047 ret <<= 4;
2048 if (c >= '0' && c <= '9')
2049 part = c - '0';
2050 else if (c >= 'a' && c <= 'f')
2051 part = c - 'a' + 10;
2052 else if (c >= 'A' && c <= 'F')
2053 part = c - 'A' + 10;
2054 else
2055 internal_error ("could not parse hex number");
2056 ret |= part;
2059 return ret;
2062 /* Read resolution for file named FILE_NAME. The resolution is read from
2063 RESOLUTION. */
2065 static void
2066 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2068 /* We require that objects in the resolution file are in the same
2069 order as the lto1 command line. */
2070 unsigned int name_len;
2071 char *obj_name;
2072 unsigned int num_symbols;
2073 unsigned int i;
2074 struct lto_file_decl_data *file_data;
2075 splay_tree_node nd = NULL;
2077 if (!resolution)
2078 return;
2080 name_len = strlen (file->filename);
2081 obj_name = XNEWVEC (char, name_len + 1);
2082 fscanf (resolution, " "); /* Read white space. */
2084 fread (obj_name, sizeof (char), name_len, resolution);
2085 obj_name[name_len] = '\0';
2086 if (filename_cmp (obj_name, file->filename) != 0)
2087 internal_error ("unexpected file name %s in linker resolution file. "
2088 "Expected %s", obj_name, file->filename);
2089 if (file->offset != 0)
2091 int t;
2092 char offset_p[17];
2093 int64_t offset;
2094 t = fscanf (resolution, "@0x%16s", offset_p);
2095 if (t != 1)
2096 internal_error ("could not parse file offset");
2097 offset = lto_parse_hex (offset_p);
2098 if (offset != file->offset)
2099 internal_error ("unexpected offset");
2102 free (obj_name);
2104 fscanf (resolution, "%u", &num_symbols);
2106 for (i = 0; i < num_symbols; i++)
2108 int t;
2109 unsigned index;
2110 unsigned HOST_WIDE_INT id;
2111 char r_str[27];
2112 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2113 unsigned int j;
2114 unsigned int lto_resolution_str_len = ARRAY_SIZE (lto_resolution_str);
2115 res_pair rp;
2117 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
2118 " %26s %*[^\n]\n", &index, &id, r_str);
2119 if (t != 3)
2120 internal_error ("invalid line in the resolution file");
2122 for (j = 0; j < lto_resolution_str_len; j++)
2124 if (strcmp (lto_resolution_str[j], r_str) == 0)
2126 r = (enum ld_plugin_symbol_resolution) j;
2127 /* Incremental linking together with -fwhole-program may seem
2128 somewhat contradictionary (as the point of incremental linking
2129 is to allow re-linking with more symbols later) but it is
2130 used to build LTO kernel. We want to hide all symbols that
2131 are not explicitely marked as exported and thus turn
2132 LDPR_PREVAILING_DEF_IRONLY_EXP
2133 to LDPR_PREVAILING_DEF_IRONLY. */
2134 if (flag_whole_program
2135 && flag_incremental_link == INCREMENTAL_LINK_NOLTO
2136 && r == LDPR_PREVAILING_DEF_IRONLY_EXP)
2137 r = LDPR_PREVAILING_DEF_IRONLY;
2138 break;
2141 if (j == lto_resolution_str_len)
2142 internal_error ("invalid resolution in the resolution file");
2144 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2146 nd = lto_splay_tree_lookup (file_ids, id);
2147 if (nd == NULL)
2148 internal_error ("resolution sub id %wx not in object file", id);
2151 file_data = (struct lto_file_decl_data *)nd->value;
2152 /* The indexes are very sparse. To save memory save them in a compact
2153 format that is only unpacked later when the subfile is processed. */
2154 rp.res = r;
2155 rp.index = index;
2156 file_data->respairs.safe_push (rp);
2157 if (file_data->max_index < index)
2158 file_data->max_index = index;
2162 /* List of file_decl_datas. */
2163 struct file_data_list
2165 struct lto_file_decl_data *first, *last;
2168 /* Is the name for a id'ed LTO section? */
2170 static int
2171 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2173 const char *s;
2175 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
2176 return 0;
2177 s = strrchr (name, '.');
2178 if (!s)
2179 return 0;
2180 /* If the section is not suffixed with an ID return. */
2181 if ((size_t)(s - name) == strlen (section_name_prefix))
2182 return 0;
2183 return sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2186 /* Create file_data of each sub file id. */
2188 static int
2189 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2190 struct file_data_list *list)
2192 struct lto_section_slot s_slot, *new_slot;
2193 unsigned HOST_WIDE_INT id;
2194 splay_tree_node nd;
2195 void **hash_slot;
2196 char *new_name;
2197 struct lto_file_decl_data *file_data;
2199 if (!lto_section_with_id (ls->name, &id))
2200 return 1;
2202 /* Find hash table of sub module id. */
2203 nd = lto_splay_tree_lookup (file_ids, id);
2204 if (nd != NULL)
2206 file_data = (struct lto_file_decl_data *)nd->value;
2208 else
2210 file_data = ggc_alloc<lto_file_decl_data> ();
2211 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2212 file_data->id = id;
2213 file_data->section_hash_table = lto_obj_create_section_hash_table ();
2214 lto_splay_tree_insert (file_ids, id, file_data);
2216 /* Maintain list in linker order. */
2217 if (!list->first)
2218 list->first = file_data;
2219 if (list->last)
2220 list->last->next = file_data;
2222 list->last = file_data;
2225 /* Copy section into sub module hash table. */
2226 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2227 s_slot.name = new_name;
2228 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2229 gcc_assert (*hash_slot == NULL);
2231 new_slot = XDUP (struct lto_section_slot, ls);
2232 new_slot->name = new_name;
2233 *hash_slot = new_slot;
2234 return 1;
2237 /* Read declarations and other initializations for a FILE_DATA. */
2239 static void
2240 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file,
2241 int order)
2243 const char *data;
2244 size_t len;
2245 vec<ld_plugin_symbol_resolution_t>
2246 resolutions = vNULL;
2247 int i;
2248 res_pair *rp;
2250 /* Create vector for fast access of resolution. We do this lazily
2251 to save memory. */
2252 resolutions.safe_grow_cleared (file_data->max_index + 1, true);
2253 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2254 resolutions[rp->index] = rp->res;
2255 file_data->respairs.release ();
2257 file_data->renaming_hash_table = lto_create_renaming_table ();
2258 file_data->file_name = file->filename;
2259 file_data->order = order;
2261 /* Read and verify LTO section. */
2262 data = lto_get_summary_section_data (file_data, LTO_section_lto, &len);
2263 if (data == NULL)
2265 fatal_error (input_location, "bytecode stream in file %qs generated "
2266 "with GCC compiler older than 10.0", file_data->file_name);
2267 return;
2270 memcpy (&file_data->lto_section_header, data, sizeof (lto_section));
2271 lto_check_version (file_data->lto_section_header.major_version,
2272 file_data->lto_section_header.minor_version,
2273 file_data->file_name);
2275 #ifdef ACCEL_COMPILER
2276 lto_input_mode_table (file_data);
2277 #else
2278 file_data->mode_table = NULL;
2279 file_data->mode_bits = ceil_log2 (MAX_MACHINE_MODE);
2280 #endif
2282 data = lto_get_summary_section_data (file_data, LTO_section_decls, &len);
2283 if (data == NULL)
2285 internal_error ("cannot read %<LTO_section_decls%> from %s",
2286 file_data->file_name);
2287 return;
2289 /* Frees resolutions. */
2290 lto_read_decls (file_data, data, resolutions);
2291 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2294 /* Finalize FILE_DATA in FILE and increase COUNT. */
2296 static int
2297 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2298 int *count, int order)
2300 lto_file_finalize (file_data, file, order);
2301 if (symtab->dump_file)
2302 fprintf (symtab->dump_file,
2303 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2304 file_data->file_name, file_data->id);
2305 (*count)++;
2306 return 0;
2309 /* Generate a TREE representation for all types and external decls
2310 entities in FILE.
2312 Read all of the globals out of the file. Then read the cgraph
2313 and process the .o index into the cgraph nodes so that it can open
2314 the .o file to load the functions and ipa information. */
2316 static struct lto_file_decl_data *
2317 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2319 struct lto_file_decl_data *file_data = NULL;
2320 splay_tree file_ids;
2321 htab_t section_hash_table;
2322 struct lto_section_slot *section;
2323 struct file_data_list file_list;
2324 struct lto_section_list section_list;
2326 memset (&section_list, 0, sizeof (struct lto_section_list));
2327 section_hash_table = lto_obj_build_section_table (file, &section_list);
2329 /* Dump the details of LTO objects. */
2330 if (flag_lto_dump_objects)
2332 int i=0;
2333 fprintf (stdout, "\n LTO Object Name: %s\n", file->filename);
2334 fprintf (stdout, "\nNo. Offset Size Section Name\n\n");
2335 for (section = section_list.first; section != NULL; section = section->next)
2336 fprintf (stdout, "%2d %8" PRId64 " %8" PRIu64 " %s\n",
2337 ++i, (int64_t) section->start, (uint64_t) section->len,
2338 section->name);
2341 /* Find all sub modules in the object and put their sections into new hash
2342 tables in a splay tree. */
2343 file_ids = lto_splay_tree_new ();
2344 memset (&file_list, 0, sizeof (struct file_data_list));
2345 for (section = section_list.first; section != NULL; section = section->next)
2346 create_subid_section_table (section, file_ids, &file_list);
2348 /* Add resolutions to file ids. */
2349 lto_resolution_read (file_ids, resolution_file, file);
2351 /* Finalize each lto file for each submodule in the merged object. */
2352 int order = 0;
2353 for (file_data = file_list.first; file_data != NULL;
2354 file_data = file_data->next)
2355 lto_create_files_from_ids (file, file_data, count, order++);
2357 splay_tree_delete (file_ids);
2358 htab_delete (section_hash_table);
2360 return file_list.first;
2363 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2364 #define LTO_MMAP_IO 1
2365 #endif
2367 #if LTO_MMAP_IO
2368 /* Page size of machine is used for mmap and munmap calls. */
2369 static size_t page_mask;
2370 #endif
2372 /* Get the section data of length LEN from FILENAME starting at
2373 OFFSET. The data segment must be freed by the caller when the
2374 caller is finished. Returns NULL if all was not well. */
2376 static char *
2377 lto_read_section_data (struct lto_file_decl_data *file_data,
2378 intptr_t offset, size_t len)
2380 char *result;
2381 static int fd = -1;
2382 static char *fd_name;
2383 #if LTO_MMAP_IO
2384 intptr_t computed_len;
2385 intptr_t computed_offset;
2386 intptr_t diff;
2387 #endif
2389 /* Keep a single-entry file-descriptor cache. The last file we
2390 touched will get closed at exit.
2391 ??? Eventually we want to add a more sophisticated larger cache
2392 or rather fix function body streaming to not stream them in
2393 practically random order. */
2394 if (fd != -1
2395 && filename_cmp (fd_name, file_data->file_name) != 0)
2397 free (fd_name);
2398 close (fd);
2399 fd = -1;
2401 if (fd == -1)
2403 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2404 if (fd == -1)
2406 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2407 return NULL;
2409 fd_name = xstrdup (file_data->file_name);
2412 #if LTO_MMAP_IO
2413 if (!page_mask)
2415 size_t page_size = sysconf (_SC_PAGE_SIZE);
2416 page_mask = ~(page_size - 1);
2419 computed_offset = offset & page_mask;
2420 diff = offset - computed_offset;
2421 computed_len = len + diff;
2423 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2424 fd, computed_offset);
2425 if (result == MAP_FAILED)
2427 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2428 return NULL;
2431 return result + diff;
2432 #else
2433 result = (char *) xmalloc (len);
2434 if (lseek (fd, offset, SEEK_SET) != offset
2435 || read (fd, result, len) != (ssize_t) len)
2437 free (result);
2438 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2439 result = NULL;
2441 #ifdef __MINGW32__
2442 /* Native windows doesn't supports delayed unlink on opened file. So
2443 we close file here again. This produces higher I/O load, but at least
2444 it prevents to have dangling file handles preventing unlink. */
2445 free (fd_name);
2446 fd_name = NULL;
2447 close (fd);
2448 fd = -1;
2449 #endif
2450 return result;
2451 #endif
2455 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2456 NAME will be NULL unless the section type is for a function
2457 body. */
2459 static const char *
2460 get_section_data (struct lto_file_decl_data *file_data,
2461 enum lto_section_type section_type,
2462 const char *name, int order,
2463 size_t *len)
2465 htab_t section_hash_table = file_data->section_hash_table;
2466 struct lto_section_slot *f_slot;
2467 struct lto_section_slot s_slot;
2468 const char *section_name = lto_get_section_name (section_type, name,
2469 order, file_data);
2470 char *data = NULL;
2472 *len = 0;
2473 s_slot.name = section_name;
2474 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2475 if (f_slot)
2477 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2478 *len = f_slot->len;
2481 free (CONST_CAST (char *, section_name));
2482 return data;
2486 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2487 starts at OFFSET and has LEN bytes. */
2489 static void
2490 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2491 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2492 const char *name ATTRIBUTE_UNUSED,
2493 const char *offset, size_t len ATTRIBUTE_UNUSED)
2495 #if LTO_MMAP_IO
2496 intptr_t computed_len;
2497 intptr_t computed_offset;
2498 intptr_t diff;
2499 #endif
2501 #if LTO_MMAP_IO
2502 computed_offset = ((intptr_t) offset) & page_mask;
2503 diff = (intptr_t) offset - computed_offset;
2504 computed_len = len + diff;
2506 munmap ((caddr_t) computed_offset, computed_len);
2507 #else
2508 free (CONST_CAST(char *, offset));
2509 #endif
2512 static lto_file *current_lto_file;
2514 /* If TT is a variable or function decl replace it with its
2515 prevailing variant. */
2516 #define LTO_SET_PREVAIL(tt) \
2517 do {\
2518 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2519 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2521 tt = lto_symtab_prevailing_decl (tt); \
2522 fixed = true; \
2524 } while (0)
2526 /* Ensure that TT isn't a replacable var of function decl. */
2527 #define LTO_NO_PREVAIL(tt) \
2528 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2530 /* Given a tree T replace all fields referring to variables or functions
2531 with their prevailing variant. */
2532 static void
2533 lto_fixup_prevailing_decls (tree t)
2535 enum tree_code code = TREE_CODE (t);
2536 bool fixed = false;
2538 gcc_checking_assert (code != TREE_BINFO);
2539 LTO_NO_PREVAIL (TREE_TYPE (t));
2540 if (CODE_CONTAINS_STRUCT (code, TS_COMMON)
2541 /* lto_symtab_prevail_decl use TREE_CHAIN to link to the prevailing decl.
2542 in the case T is a prevailed declaration we would ICE here. */
2543 && !VAR_OR_FUNCTION_DECL_P (t))
2544 LTO_NO_PREVAIL (TREE_CHAIN (t));
2545 if (DECL_P (t))
2547 LTO_NO_PREVAIL (DECL_NAME (t));
2548 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2549 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2551 LTO_SET_PREVAIL (DECL_SIZE (t));
2552 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2553 LTO_SET_PREVAIL (DECL_INITIAL (t));
2554 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2555 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2557 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2559 LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t));
2561 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2563 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2565 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2567 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2568 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2569 LTO_NO_PREVAIL (DECL_VINDEX (t));
2571 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2573 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2574 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2575 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2576 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2577 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2580 else if (TYPE_P (t))
2582 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2583 LTO_SET_PREVAIL (TYPE_SIZE (t));
2584 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2585 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2586 LTO_NO_PREVAIL (TYPE_NAME (t));
2588 LTO_SET_PREVAIL (TYPE_MIN_VALUE_RAW (t));
2589 LTO_SET_PREVAIL (TYPE_MAX_VALUE_RAW (t));
2590 LTO_NO_PREVAIL (TYPE_LANG_SLOT_1 (t));
2592 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2594 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2595 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2596 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2598 else if (EXPR_P (t))
2600 int i;
2601 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2602 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2604 else if (TREE_CODE (t) == CONSTRUCTOR)
2606 unsigned i;
2607 tree val;
2608 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2609 LTO_SET_PREVAIL (val);
2611 else
2613 switch (code)
2615 case TREE_LIST:
2616 LTO_SET_PREVAIL (TREE_VALUE (t));
2617 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2618 break;
2619 default:
2620 gcc_unreachable ();
2623 /* If we fixed nothing, then we missed something seen by
2624 mentions_vars_p. */
2625 gcc_checking_assert (fixed);
2627 #undef LTO_SET_PREVAIL
2628 #undef LTO_NO_PREVAIL
2630 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2631 replaces var and function decls with the corresponding prevailing def. */
2633 static void
2634 lto_fixup_state (struct lto_in_decl_state *state)
2636 unsigned i, si;
2638 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2639 we still need to walk from all DECLs to find the reachable
2640 FUNCTION_DECLs and VAR_DECLs. */
2641 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2643 vec<tree, va_gc> *trees = state->streams[si];
2644 for (i = 0; i < vec_safe_length (trees); i++)
2646 tree t = (*trees)[i];
2647 if (flag_checking && TYPE_P (t))
2648 verify_type (t);
2649 if (VAR_OR_FUNCTION_DECL_P (t)
2650 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2651 (*trees)[i] = lto_symtab_prevailing_decl (t);
2656 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2657 prevailing one. */
2659 static void
2660 lto_fixup_decls (struct lto_file_decl_data **files)
2662 unsigned int i;
2663 tree t;
2665 if (tree_with_vars)
2666 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2667 lto_fixup_prevailing_decls (t);
2669 for (i = 0; files[i]; i++)
2671 struct lto_file_decl_data *file = files[i];
2672 struct lto_in_decl_state *state = file->global_decl_state;
2673 lto_fixup_state (state);
2675 hash_table<decl_state_hasher>::iterator iter;
2676 lto_in_decl_state *elt;
2677 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2678 lto_in_decl_state *, iter)
2679 lto_fixup_state (elt);
2683 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2685 /* Turn file datas for sub files into a single array, so that they look
2686 like separate files for further passes. */
2688 static void
2689 lto_flatten_files (struct lto_file_decl_data **orig, int count,
2690 int last_file_ix)
2692 struct lto_file_decl_data *n, *next;
2693 int i, k;
2695 lto_stats.num_input_files = count;
2696 all_file_decl_data
2697 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2698 /* Set the hooks so that all of the ipa passes can read in their data. */
2699 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2700 for (i = 0, k = 0; i < last_file_ix; i++)
2702 for (n = orig[i]; n != NULL; n = next)
2704 all_file_decl_data[k++] = n;
2705 next = n->next;
2706 n->next = NULL;
2709 all_file_decl_data[k] = NULL;
2710 gcc_assert (k == count);
2713 /* Input file data before flattening (i.e. splitting them to subfiles to support
2714 incremental linking. */
2715 static int real_file_count;
2716 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2718 /* Read all the symbols from the input files FNAMES. NFILES is the
2719 number of files requested in the command line. Instantiate a
2720 global call graph by aggregating all the sub-graphs found in each
2721 file. */
2723 void
2724 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2726 unsigned int i, last_file_ix;
2727 FILE *resolution;
2728 unsigned resolution_objects = 0;
2729 int count = 0;
2730 struct lto_file_decl_data **decl_data;
2731 symtab_node *snode;
2733 symtab->initialize ();
2735 timevar_push (TV_IPA_LTO_DECL_IN);
2737 #ifdef ACCEL_COMPILER
2738 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2739 lto_stream_offload_p = true;
2740 #endif
2742 real_file_decl_data
2743 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2744 real_file_count = nfiles;
2746 /* Read the resolution file. */
2747 resolution = NULL;
2748 if (resolution_file_name)
2750 int t;
2752 resolution = fopen (resolution_file_name, "r");
2753 if (resolution == NULL)
2754 fatal_error (input_location,
2755 "could not open symbol resolution file: %m");
2757 t = fscanf (resolution, "%u", &resolution_objects);
2758 gcc_assert (t == 1);
2760 symtab->state = LTO_STREAMING;
2762 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2763 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2764 gimple_canonical_type_eq, NULL);
2765 gcc_obstack_init (&tree_scc_hash_obstack);
2766 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2768 /* Register the common node types with the canonical type machinery so
2769 we properly share alias-sets across languages and TUs. Do not
2770 expose the common nodes as type merge target - those that should be
2771 are already exposed so by pre-loading the LTO streamer caches.
2772 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2773 for (i = 0; i < itk_none; ++i)
2774 lto_register_canonical_types (integer_types[i], true);
2775 for (i = 0; i < stk_type_kind_last; ++i)
2776 lto_register_canonical_types (sizetype_tab[i], true);
2777 for (i = 0; i < TI_MAX; ++i)
2778 lto_register_canonical_types (global_trees[i], true);
2779 for (i = 0; i < itk_none; ++i)
2780 lto_register_canonical_types (integer_types[i], false);
2781 for (i = 0; i < stk_type_kind_last; ++i)
2782 lto_register_canonical_types (sizetype_tab[i], false);
2783 for (i = 0; i < TI_MAX; ++i)
2784 lto_register_canonical_types (global_trees[i], false);
2786 if (!quiet_flag)
2787 fprintf (stderr, "Reading object files:");
2789 /* Read all of the object files specified on the command line. */
2790 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2792 struct lto_file_decl_data *file_data = NULL;
2793 if (!quiet_flag)
2795 fprintf (stderr, " %s", fnames[i]);
2796 fflush (stderr);
2799 current_lto_file = lto_obj_file_open (fnames[i], false);
2800 if (!current_lto_file)
2801 break;
2803 file_data = lto_file_read (current_lto_file, resolution, &count);
2804 if (!file_data)
2806 lto_obj_file_close (current_lto_file);
2807 free (current_lto_file);
2808 current_lto_file = NULL;
2809 break;
2812 decl_data[last_file_ix++] = file_data;
2814 lto_obj_file_close (current_lto_file);
2815 free (current_lto_file);
2816 current_lto_file = NULL;
2819 lto_flatten_files (decl_data, count, last_file_ix);
2820 lto_stats.num_input_files = count;
2821 ggc_free(decl_data);
2822 real_file_decl_data = NULL;
2824 lto_register_canonical_types_for_odr_types ();
2826 if (resolution_file_name)
2828 /* True, since the plugin splits the archives. */
2829 gcc_assert (resolution_objects == nfiles);
2830 fclose (resolution);
2833 /* Show the LTO report before launching LTRANS. */
2834 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2835 print_lto_report_1 ();
2837 /* Free gimple type merging datastructures. */
2838 delete tree_scc_hash;
2839 tree_scc_hash = NULL;
2840 obstack_free (&tree_scc_hash_obstack, NULL);
2841 htab_delete (gimple_canonical_types);
2842 gimple_canonical_types = NULL;
2843 delete canonical_type_hash_cache;
2844 canonical_type_hash_cache = NULL;
2846 /* At this stage we know that majority of GGC memory is reachable.
2847 Growing the limits prevents unnecesary invocation of GGC. */
2848 ggc_grow ();
2849 report_heap_memory_use ();
2851 /* Set the hooks so that all of the ipa passes can read in their data. */
2852 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2854 timevar_pop (TV_IPA_LTO_DECL_IN);
2856 if (!quiet_flag)
2857 fprintf (stderr, "\nReading the symbol table:");
2859 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2860 /* Read the symtab. */
2861 input_symtab ();
2863 input_offload_tables (!flag_ltrans);
2865 /* Store resolutions into the symbol table. */
2867 FOR_EACH_SYMBOL (snode)
2868 if (snode->externally_visible && snode->real_symbol_p ()
2869 && snode->lto_file_data && snode->lto_file_data->resolution_map
2870 && !(TREE_CODE (snode->decl) == FUNCTION_DECL
2871 && fndecl_built_in_p (snode->decl))
2872 && !(VAR_P (snode->decl) && DECL_HARD_REGISTER (snode->decl)))
2874 ld_plugin_symbol_resolution_t *res;
2876 res = snode->lto_file_data->resolution_map->get (snode->decl);
2877 if (!res || *res == LDPR_UNKNOWN)
2879 if (snode->output_to_lto_symbol_table_p ())
2880 fatal_error (input_location, "missing resolution data for %s",
2881 IDENTIFIER_POINTER
2882 (DECL_ASSEMBLER_NAME (snode->decl)));
2884 /* Symbol versions are always used externally, but linker does not
2885 report that correctly.
2886 This is binutils PR25924. */
2887 else if (snode->symver && *res == LDPR_PREVAILING_DEF_IRONLY)
2888 snode->resolution = LDPR_PREVAILING_DEF_IRONLY_EXP;
2889 else
2890 snode->resolution = *res;
2892 for (i = 0; all_file_decl_data[i]; i++)
2893 if (all_file_decl_data[i]->resolution_map)
2895 delete all_file_decl_data[i]->resolution_map;
2896 all_file_decl_data[i]->resolution_map = NULL;
2899 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2901 if (!quiet_flag)
2902 fprintf (stderr, "\nMerging declarations:");
2904 timevar_push (TV_IPA_LTO_DECL_MERGE);
2905 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2906 need to care about resolving symbols again, we only need to replace
2907 duplicated declarations read from the callgraph and from function
2908 sections. */
2909 if (!flag_ltrans)
2911 lto_symtab_merge_decls ();
2913 /* If there were errors during symbol merging bail out, we have no
2914 good way to recover here. */
2915 if (seen_error ())
2916 fatal_error (input_location,
2917 "errors during merging of translation units");
2919 /* Fixup all decls. */
2920 lto_fixup_decls (all_file_decl_data);
2922 if (tree_with_vars)
2923 ggc_free (tree_with_vars);
2924 tree_with_vars = NULL;
2925 /* During WPA we want to prevent ggc collecting by default. Grow limits
2926 until after the IPA summaries are streamed in. Basically all IPA memory
2927 is explcitly managed by ggc_free and ggc collect is not useful.
2928 Exception are the merged declarations. */
2929 ggc_grow ();
2930 report_heap_memory_use ();
2932 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2933 /* Each pass will set the appropriate timer. */
2935 if (!quiet_flag)
2936 fprintf (stderr, "\nReading summaries:");
2938 /* Read the IPA summary data. */
2939 if (flag_ltrans)
2940 ipa_read_optimization_summaries ();
2941 else
2942 ipa_read_summaries ();
2944 ggc_grow ();
2946 for (i = 0; all_file_decl_data[i]; i++)
2948 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2949 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2950 all_file_decl_data[i]->symtab_node_encoder = NULL;
2951 lto_in_decl_state *global_decl_state
2952 = all_file_decl_data[i]->global_decl_state;
2953 lto_free_function_in_decl_state (global_decl_state);
2954 all_file_decl_data[i]->global_decl_state = NULL;
2955 all_file_decl_data[i]->current_decl_state = NULL;
2958 if (!flag_ltrans)
2960 /* Finally merge the cgraph according to the decl merging decisions. */
2961 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2963 if (!quiet_flag)
2964 fprintf (stderr, "\nMerging symbols:");
2966 gcc_assert (!dump_file);
2967 dump_file = dump_begin (lto_link_dump_id, NULL);
2969 if (dump_file)
2971 fprintf (dump_file, "Before merging:\n");
2972 symtab->dump (dump_file);
2974 lto_symtab_merge_symbols ();
2975 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2976 we are still having duplicated comdat groups containing local statics.
2977 We could also just remove them while merging. */
2978 symtab->remove_unreachable_nodes (dump_file);
2979 ggc_collect ();
2980 report_heap_memory_use ();
2982 if (dump_file)
2983 dump_end (lto_link_dump_id, dump_file);
2984 dump_file = NULL;
2985 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2987 symtab->state = IPA_SSA;
2988 /* All node removals happening here are useless, because
2989 WPA should not stream them. Still always perform remove_unreachable_nodes
2990 because we may reshape clone tree, get rid of dead masters of inline
2991 clones and remove symbol entries for read-only variables we keep around
2992 only to be able to constant fold them. */
2993 if (flag_ltrans)
2995 if (symtab->dump_file)
2996 symtab->dump (symtab->dump_file);
2997 symtab->remove_unreachable_nodes (symtab->dump_file);
3000 /* Indicate that the cgraph is built and ready. */
3001 symtab->function_flags_ready = true;
3003 ggc_free (all_file_decl_data);
3004 all_file_decl_data = NULL;
3009 /* Show various memory usage statistics related to LTO. */
3010 void
3011 print_lto_report_1 (void)
3013 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3014 fprintf (stderr, "%s statistics\n", pfx);
3016 fprintf (stderr, "[%s] read %lu unshared trees\n",
3017 pfx, num_unshared_trees_read);
3018 fprintf (stderr, "[%s] read %lu mergeable SCCs of average size %f\n",
3019 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3020 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx,
3021 total_scc_size + num_unshared_trees_read);
3022 if (flag_wpa && tree_scc_hash && num_sccs_read)
3024 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3025 "collision ratio: %f\n", pfx,
3026 (long) tree_scc_hash->size (),
3027 (long) tree_scc_hash->elements (),
3028 tree_scc_hash->collisions ());
3029 hash_table<tree_scc_hasher>::iterator hiter;
3030 tree_scc *scc, *max_scc = NULL;
3031 unsigned max_length = 0;
3032 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3034 unsigned length = 0;
3035 tree_scc *s = scc;
3036 for (; s; s = s->next)
3037 length++;
3038 if (length > max_length)
3040 max_length = length;
3041 max_scc = scc;
3044 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3045 pfx, max_length, max_scc->len);
3046 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3047 num_scc_compares, num_scc_compare_collisions,
3048 num_scc_compare_collisions / (double) num_scc_compares);
3049 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3050 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3051 total_scc_size_merged);
3052 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3053 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3054 pfx, num_prevailing_types, num_type_scc_trees);
3055 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3056 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3057 (long) htab_size (gimple_canonical_types),
3058 (long) htab_elements (gimple_canonical_types),
3059 (long) gimple_canonical_types->searches,
3060 (long) gimple_canonical_types->collisions,
3061 htab_collisions (gimple_canonical_types));
3062 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3063 "%lu elements, %ld searches\n", pfx,
3064 num_canonical_type_hash_entries,
3065 num_canonical_type_hash_queries);
3068 print_lto_report (pfx);
3071 GTY(()) tree lto_eh_personality_decl;
3073 /* Return the LTO personality function decl. */
3075 tree
3076 lto_eh_personality (void)
3078 if (!lto_eh_personality_decl)
3080 /* Use the first personality DECL for our personality if we don't
3081 support multiple ones. This ensures that we don't artificially
3082 create the need for them in a single-language program. */
3083 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3084 lto_eh_personality_decl = first_personality_decl;
3085 else
3086 lto_eh_personality_decl = lhd_gcc_personality ();
3089 return lto_eh_personality_decl;
3092 /* Set the process name based on the LTO mode. */
3094 static void
3095 lto_process_name (void)
3097 if (flag_lto)
3098 setproctitle (flag_incremental_link == INCREMENTAL_LINK_LTO
3099 ? "lto1-inclink" : "lto1-lto");
3100 if (flag_wpa)
3101 setproctitle ("lto1-wpa");
3102 if (flag_ltrans)
3103 setproctitle ("lto1-ltrans");
3107 /* Initialize the LTO front end. */
3109 void
3110 lto_fe_init (void)
3112 lto_process_name ();
3113 lto_streamer_hooks_init ();
3114 lto_reader_init ();
3115 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3116 memset (&lto_stats, 0, sizeof (lto_stats));
3117 bitmap_obstack_initialize (NULL);
3118 gimple_register_cfg_hooks ();
3121 #include "gt-lto-lto-common.h"