1/* Top-level LTO routines.
2 Copyright (C) 2009-2023 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
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"
60
61/* True when no new types are going to be streamd from the global stream. */
62
63static bool type_streaming_finished = false;
64
65GTY(()) tree first_personality_decl;
66
67/* Returns a hash code for P. */
68
69static hashval_t
70hash_name (const void *p)
71{
72 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
73 return (hashval_t) htab_hash_string (ds->name);
74}
75
76
77/* Returns nonzero if P1 and P2 are equal. */
78
79static int
80eq_name (const void *p1, const void *p2)
81{
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;
86
87 return strcmp (s1: s1->name, s2: s2->name) == 0;
88}
89
90/* Free lto_section_slot. */
91
92static void
93free_with_string (void *arg)
94{
95 struct lto_section_slot *s = (struct lto_section_slot *)arg;
96
97 free (CONST_CAST (char *, s->name));
98 free (ptr: arg);
99}
100
101/* Create section hash table. */
102
103htab_t
104lto_obj_create_section_hash_table (void)
105{
106 return htab_create (37, hash_name, eq_name, free_with_string);
107}
108
109/* Delete an allocated integer KEY in the splay tree. */
110
111static void
112lto_splay_tree_delete_id (splay_tree_key key)
113{
114 free (ptr: (void *) key);
115}
116
117/* Compare splay tree node ids A and B. */
118
119static int
120lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
121{
122 unsigned HOST_WIDE_INT ai;
123 unsigned HOST_WIDE_INT bi;
124
125 ai = *(unsigned HOST_WIDE_INT *) a;
126 bi = *(unsigned HOST_WIDE_INT *) b;
127
128 if (ai < bi)
129 return -1;
130 else if (ai > bi)
131 return 1;
132 return 0;
133}
134
135/* Look up splay tree node by ID in splay tree T. */
136
137static splay_tree_node
138lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
139{
140 return splay_tree_lookup (t, (splay_tree_key) &id);
141}
142
143/* Check if KEY has ID. */
144
145static bool
146lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
147{
148 return *(unsigned HOST_WIDE_INT *) key == id;
149}
150
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. */
154
155static void
156lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
157 struct lto_file_decl_data *file_data)
158{
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);
162}
163
164/* Create a splay tree. */
165
166static splay_tree
167lto_splay_tree_new (void)
168{
169 return splay_tree_new (lto_splay_tree_compare_ids,
170 lto_splay_tree_delete_id,
171 NULL);
172}
173
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. */
178
179static const uint32_t *
180lto_read_in_decl_state (class data_in *data_in, const uint32_t *data,
181 struct lto_in_decl_state *state)
182{
183 uint32_t ix;
184 tree decl;
185 uint32_t i, j;
186
187 ix = *data++;
188 state->compressed = ix & 1;
189 ix /= 2;
190 decl = streamer_tree_cache_get_tree (cache: data_in->reader_cache, ix);
191 if (!VAR_OR_FUNCTION_DECL_P (decl))
192 {
193 gcc_assert (decl == void_type_node);
194 decl = NULL_TREE;
195 }
196 state->fn_decl = decl;
197
198 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
199 {
200 uint32_t size = *data++;
201 vec<tree, va_gc> *decls = NULL;
202 vec_alloc (v&: decls, nelems: size);
203
204 for (j = 0; j < size; j++)
205 vec_safe_push (v&: decls,
206 obj: streamer_tree_cache_get_tree (cache: data_in->reader_cache,
207 ix: data[j]));
208
209 state->streams[i] = decls;
210 data += size;
211 }
212
213 return data;
214}
215
216
217/* Global canonical type table. */
218static htab_t gimple_canonical_types;
219static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
220static unsigned long num_canonical_type_hash_entries;
221static unsigned long num_canonical_type_hash_queries;
222
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. */
226static GTY(()) vec<tree, va_gc> *types_to_register = NULL;
227
228static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
229static hashval_t gimple_canonical_type_hash (const void *p);
230static hashval_t gimple_register_canonical_type_1 (tree t, hashval_t hash);
231
232/* Returning a hash value for gimple type TYPE.
233
234 The hash value returned is equal for types considered compatible
235 by gimple_canonical_types_compatible_p. */
236
237static hashval_t
238hash_canonical_type (tree type)
239{
240 inchash::hash hstate;
241 enum tree_code code;
242
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));
248
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 (v: code);
255 hstate.add_int (TYPE_MODE (type));
256
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))
263 {
264 hstate.add_int (TYPE_PRECISION (type));
265 if (!type_with_interoperable_signedness (type))
266 hstate.add_int (TYPE_UNSIGNED (type));
267 }
268
269 if (VECTOR_TYPE_P (type))
270 {
271 hstate.add_poly_int (v: TYPE_VECTOR_SUBPARTS (node: type));
272 hstate.add_int (TYPE_UNSIGNED (type));
273 }
274
275 if (TREE_CODE (type) == COMPLEX_TYPE)
276 hstate.add_int (TYPE_UNSIGNED (type));
277
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. */
281
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)));
287
288 /* For array types hash the domain bounds and the string flag. */
289 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
290 {
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);
298 }
299
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);
305
306 /* Incorporate function return and argument types. */
307 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
308 {
309 unsigned na;
310 tree p;
311
312 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
313
314 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
315 {
316 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
317 na++;
318 }
319
320 hstate.add_int (v: na);
321 }
322
323 if (RECORD_OR_UNION_TYPE_P (type))
324 {
325 unsigned nf;
326 tree f;
327
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))))
332 {
333 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
334 nf++;
335 }
336
337 hstate.add_int (v: nf);
338 }
339
340 return hstate.end();
341}
342
343/* Returning a hash value for gimple type TYPE combined with VAL. */
344
345static void
346iterative_hash_canonical_type (tree type, inchash::hash &hstate)
347{
348 hashval_t v;
349
350 /* All type variants have same TYPE_CANONICAL. */
351 type = TYPE_MAIN_VARIANT (type);
352
353 if (!canonical_type_used_p (t: type))
354 v = hash_canonical_type (type);
355 /* An already processed type. */
356 else if (TYPE_CANONICAL (type))
357 {
358 type = TYPE_CANONICAL (type);
359 v = gimple_canonical_type_hash (p: type);
360 }
361 else
362 {
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 (t: type, hash: v);
369 }
370 hstate.merge_hash (other: v);
371}
372
373/* Returns the hash for a canonical type P. */
374
375static hashval_t
376gimple_canonical_type_hash (const void *p)
377{
378 num_canonical_type_hash_queries++;
379 hashval_t *slot = canonical_type_hash_cache->get (k: (const_tree) p);
380 gcc_assert (slot != NULL);
381 return *slot;
382}
383
384
385
386/* Returns nonzero if P1 and P2 are equal. */
387
388static int
389gimple_canonical_type_eq (const void *p1, const void *p2)
390{
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));
395}
396
397/* Main worker for gimple_register_canonical_type. */
398
399static hashval_t
400gimple_register_canonical_type_1 (tree t, hashval_t hash)
401{
402 void **slot;
403
404 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t)
405 && type_with_alias_set_p (t)
406 && canonical_type_used_p (t));
407
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.
411
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 (type: t))
418 {
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
423 {
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);
431 }
432 if (slot && !TYPE_CXX_ODR_P (*(tree *)slot))
433 {
434 tree nonodr = *(tree *)slot;
435 gcc_checking_assert (!flag_ltrans);
436 if (symtab->dump_file)
437 {
438 fprintf (stream: symtab->dump_file,
439 format: "ODR and non-ODR type conflict: ");
440 print_generic_expr (symtab->dump_file, t);
441 fprintf (stream: symtab->dump_file, format: " and ");
442 print_generic_expr (symtab->dump_file, nonodr);
443 fprintf (stream: symtab->dump_file, format: " mangled:%s\n",
444 IDENTIFIER_POINTER
445 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
446 }
447 /* Set canonical for T and all other ODR equivalent duplicates
448 including incomplete structures. */
449 set_type_canonical_for_odr_type (type: t, canonical: nonodr);
450 }
451 else
452 {
453 tree prevail = prevailing_odr_type (type: t);
454
455 if (symtab->dump_file)
456 {
457 fprintf (stream: symtab->dump_file,
458 format: "New canonical ODR type: ");
459 print_generic_expr (symtab->dump_file, t);
460 fprintf (stream: symtab->dump_file, format: " mangled:%s\n",
461 IDENTIFIER_POINTER
462 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
463 }
464 /* Set canonical for T and all other ODR equivalent duplicates
465 including incomplete structures. */
466 set_type_canonical_for_odr_type (type: t, canonical: prevail);
467 enable_odr_based_tbaa (type: 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);
474
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 (k: prevail, v: hash);
479 gcc_checking_assert (!existed_p);
480 }
481 return hash;
482 }
483
484 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
485 if (*slot)
486 {
487 tree new_type = (tree)(*slot);
488 gcc_checking_assert (new_type != t);
489 TYPE_CANONICAL (t) = new_type;
490 }
491 else
492 {
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 (k: t, v: hash);
498 gcc_assert (!existed_p);
499 }
500 return hash;
501}
502
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.
507
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). */
512
513static void
514gimple_register_canonical_type (tree t)
515{
516 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t)
517 || !canonical_type_used_p (t))
518 return;
519
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
524 {
525 hashval_t h = hash_canonical_type (TYPE_MAIN_VARIANT (t));
526 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t), hash: h);
527 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
528 }
529}
530
531/* Re-compute TYPE_CANONICAL for NODE and related types. */
532
533static void
534lto_register_canonical_types (tree node, bool first_p)
535{
536 if (!node
537 || !TYPE_P (node))
538 return;
539
540 if (first_p)
541 TYPE_CANONICAL (node) = NULL_TREE;
542
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);
547
548 if (!first_p)
549 gimple_register_canonical_type (t: node);
550}
551
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. */
556
557static void
558lto_register_canonical_types_for_odr_types ()
559{
560 tree t;
561 unsigned int i;
562
563 if (!types_to_register)
564 return;
565
566 type_streaming_finished = true;
567
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));
573
574 /* Register all remaining types. */
575 FOR_EACH_VEC_ELT (*types_to_register, i, t)
576 {
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);
586 }
587}
588
589
590/* Remember trees that contains references to declarations. */
591vec <tree, va_gc> *tree_with_vars;
592
593#define CHECK_VAR(tt) \
594 do \
595 { \
596 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
597 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
598 return true; \
599 } while (0)
600
601#define CHECK_NO_VAR(tt) \
602 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
603
604/* Check presence of pointers to decls in fields of a tree_typed T. */
605
606static inline bool
607mentions_vars_p_typed (tree t)
608{
609 CHECK_NO_VAR (TREE_TYPE (t));
610 return false;
611}
612
613/* Check presence of pointers to decls in fields of a tree_common T. */
614
615static inline bool
616mentions_vars_p_common (tree t)
617{
618 if (mentions_vars_p_typed (t))
619 return true;
620 CHECK_NO_VAR (TREE_CHAIN (t));
621 return false;
622}
623
624/* Check presence of pointers to decls in fields of a decl_minimal T. */
625
626static inline bool
627mentions_vars_p_decl_minimal (tree t)
628{
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;
634}
635
636/* Check presence of pointers to decls in fields of a decl_common T. */
637
638static inline bool
639mentions_vars_p_decl_common (tree t)
640{
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;
649}
650
651/* Check presence of pointers to decls in fields of a decl_with_vis T. */
652
653static inline bool
654mentions_vars_p_decl_with_vis (tree t)
655{
656 if (mentions_vars_p_decl_common (t))
657 return true;
658
659 /* Accessor macro has side-effects, use field-name here. */
660 CHECK_NO_VAR (DECL_ASSEMBLER_NAME_RAW (t));
661 return false;
662}
663
664/* Check presence of pointers to decls in fields of a decl_non_common T. */
665
666static inline bool
667mentions_vars_p_decl_non_common (tree t)
668{
669 if (mentions_vars_p_decl_with_vis (t))
670 return true;
671 CHECK_NO_VAR (DECL_RESULT_FLD (t));
672 return false;
673}
674
675/* Check presence of pointers to decls in fields of a decl_non_common T. */
676
677static bool
678mentions_vars_p_function (tree t)
679{
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;
686}
687
688/* Check presence of pointers to decls in fields of a field_decl T. */
689
690static bool
691mentions_vars_p_field_decl (tree t)
692{
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;
701}
702
703/* Check presence of pointers to decls in fields of a type T. */
704
705static bool
706mentions_vars_p_type (tree t)
707{
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));
715
716 CHECK_VAR (TYPE_MIN_VALUE_RAW (t));
717 CHECK_VAR (TYPE_MAX_VALUE_RAW (t));
718
719 /* Accessor is for derived node types only. */
720 CHECK_NO_VAR (TYPE_LANG_SLOT_1 (t));
721
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;
727}
728
729/* Check presence of pointers to decls in fields of a BINFO T. */
730
731static bool
732mentions_vars_p_binfo (tree t)
733{
734 unsigned HOST_WIDE_INT i, n;
735
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;
751}
752
753/* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
754
755static bool
756mentions_vars_p_constructor (tree t)
757{
758 unsigned HOST_WIDE_INT idx;
759 constructor_elt *ce;
760
761 if (mentions_vars_p_typed (t))
762 return true;
763
764 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), ix: idx, ptr: &ce); idx++)
765 {
766 CHECK_NO_VAR (ce->index);
767 CHECK_VAR (ce->value);
768 }
769 return false;
770}
771
772/* Check presence of pointers to decls in fields of an expression tree T. */
773
774static bool
775mentions_vars_p_expr (tree t)
776{
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;
783}
784
785/* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
786
787static bool
788mentions_vars_p_omp_clause (tree t)
789{
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;
796}
797
798/* Check presence of pointers to decls that needs later fixup in T. */
799
800static bool
801mentions_vars_p (tree t)
802{
803 switch (TREE_CODE (t))
804 {
805 case IDENTIFIER_NODE:
806 break;
807
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;
813
814 case FIELD_DECL:
815 return mentions_vars_p_field_decl (t);
816
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);
825
826 case VAR_DECL:
827 return mentions_vars_p_decl_with_vis (t);
828
829 case TYPE_DECL:
830 return mentions_vars_p_decl_non_common (t);
831
832 case FUNCTION_DECL:
833 return mentions_vars_p_function (t);
834
835 case TREE_BINFO:
836 return mentions_vars_p_binfo (t);
837
838 case PLACEHOLDER_EXPR:
839 return mentions_vars_p_common (t);
840
841 case BLOCK:
842 case TRANSLATION_UNIT_DECL:
843 case OPTIMIZATION_NODE:
844 case TARGET_OPTION_NODE:
845 break;
846
847 case CONSTRUCTOR:
848 return mentions_vars_p_constructor (t);
849
850 case OMP_CLAUSE:
851 return mentions_vars_p_omp_clause (t);
852
853 default:
854 if (TYPE_P (t))
855 {
856 if (mentions_vars_p_type (t))
857 return true;
858 }
859 else if (EXPR_P (t))
860 {
861 if (mentions_vars_p_expr (t))
862 return true;
863 }
864 else if (CONSTANT_CLASS_P (t))
865 CHECK_NO_VAR (TREE_TYPE (t));
866 else
867 gcc_unreachable ();
868 }
869 return false;
870}
871
872
873/* Return the resolution for the decl with index INDEX from DATA_IN. */
874
875static enum ld_plugin_symbol_resolution
876get_resolution (class data_in *data_in, unsigned index)
877{
878 if (data_in->globals_resolution.exists ())
879 {
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;
888 }
889 else
890 /* Delay resolution finding until decl merging. */
891 return LDPR_UNKNOWN;
892}
893
894/* We need to record resolutions until symbol table is read. */
895static void
896register_resolution (struct lto_file_decl_data *file_data, tree decl,
897 enum ld_plugin_symbol_resolution resolution)
898{
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 (k: decl, existed: &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;
912}
913
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. */
917
918static void
919lto_register_var_decl_in_symtab (class data_in *data_in, tree decl,
920 unsigned ix)
921{
922 tree context;
923
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);
929
930 /* If this variable has already been declared, queue the
931 declaration for merging. */
932 if (TREE_PUBLIC (decl))
933 register_resolution (file_data: data_in->file_data,
934 decl, resolution: get_resolution (data_in, index: ix));
935}
936
937
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. */
942
943static void
944lto_register_function_decl_in_symtab (class data_in *data_in, tree decl,
945 unsigned ix)
946{
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 (file_data: data_in->file_data,
951 decl, resolution: get_resolution (data_in, index: ix));
952}
953
954/* Check if T is a decl and needs register its resolution info. */
955
956static void
957lto_maybe_register_decl (class data_in *data_in, tree t, unsigned ix)
958{
959 if (VAR_P (t))
960 lto_register_var_decl_in_symtab (data_in, decl: t, ix);
961 else if (TREE_CODE (t) == FUNCTION_DECL
962 && !fndecl_built_in_p (node: t))
963 lto_register_function_decl_in_symtab (data_in, decl: t, ix);
964}
965
966
967/* For the type T re-materialize it in the type variant list and
968 the pointer/reference-to chains. */
969
970static void
971lto_fixup_prevailing_type (tree t)
972{
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. */
976
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)
980 {
981 tree mv = TYPE_MAIN_VARIANT (t);
982 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
983 TYPE_NEXT_VARIANT (mv) = t;
984 }
985 else if (!TYPE_ATTRIBUTES (t))
986 {
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)
995 {
996 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
997 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
998 }
999 else if (TREE_CODE (t) == REFERENCE_TYPE && !TYPE_REF_IS_RVALUE (t))
1000 {
1001 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1002 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1003 }
1004 }
1005}
1006
1007
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. */
1011
1012struct tree_scc
1013{
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];
1029};
1030
1031struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
1032{
1033 static inline hashval_t hash (const tree_scc *);
1034 static inline bool equal (const tree_scc *, const tree_scc *);
1035};
1036
1037hashval_t
1038tree_scc_hasher::hash (const tree_scc *scc)
1039{
1040 return scc->hash;
1041}
1042
1043bool
1044tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
1045{
1046 if (scc1->hash != scc2->hash
1047 || scc1->len != scc2->len
1048 || scc1->entry_len != scc2->entry_len)
1049 return false;
1050 return true;
1051}
1052
1053static hash_table<tree_scc_hasher> *tree_scc_hash;
1054static struct obstack tree_scc_hash_obstack;
1055
1056static unsigned long num_merged_types;
1057static unsigned long num_prevailing_types;
1058static unsigned long num_type_scc_trees;
1059static unsigned long total_scc_size;
1060static unsigned long num_sccs_read;
1061static unsigned long num_unshared_trees_read;
1062static unsigned long total_scc_size_merged;
1063static unsigned long num_sccs_merged;
1064static unsigned long num_scc_compares;
1065static unsigned long num_scc_compare_collisions;
1066
1067
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. */
1072
1073static bool
1074compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1075{
1076 enum tree_code code;
1077
1078 /* Mark already visited nodes. */
1079 TREE_ASM_WRITTEN (t2) = 1;
1080
1081 /* Push the pair onto map. */
1082 (*map)[0] = t1;
1083 (*map)[1] = t2;
1084 *map = *map + 2;
1085
1086 /* Compare value-fields. */
1087#define compare_values(X) \
1088 do { \
1089 if (X(t1) != X(t2)) \
1090 return false; \
1091 } while (0)
1092
1093 compare_values (TREE_CODE);
1094 code = TREE_CODE (t1);
1095
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);
1099
1100 if (!TYPE_P (t1))
1101 {
1102 compare_values (TREE_SIDE_EFFECTS);
1103 compare_values (TREE_CONSTANT);
1104 compare_values (TREE_READONLY);
1105 compare_values (TREE_PUBLIC);
1106 }
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))
1124 {
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);
1130 }
1131 else if (code == SSA_NAME)
1132 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1133
1134 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1135 {
1136 if (wi::to_wide (t: t1) != wi::to_wide (t: t2))
1137 return false;
1138 }
1139
1140 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1141 {
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;
1155 }
1156
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;
1161
1162 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1163 {
1164 compare_values (VECTOR_CST_LOG2_NPATTERNS);
1165 compare_values (VECTOR_CST_NELTS_PER_PATTERN);
1166 }
1167
1168 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1169 {
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)
1182 {
1183 compare_values (EH_LANDING_PAD_NR);
1184 compare_values (LABEL_DECL_UID);
1185 }
1186 else if (code == FIELD_DECL)
1187 {
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);
1195 }
1196 else if (code == VAR_DECL)
1197 {
1198 compare_values (DECL_HAS_DEBUG_EXPR_P);
1199 compare_values (DECL_NONLOCAL_FRAME);
1200 }
1201 if (code == RESULT_DECL
1202 || code == PARM_DECL
1203 || code == VAR_DECL)
1204 {
1205 compare_values (DECL_BY_REFERENCE);
1206 if (code == VAR_DECL
1207 || code == PARM_DECL)
1208 compare_values (DECL_HAS_VALUE_EXPR_P);
1209 }
1210 }
1211
1212 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1213 compare_values (DECL_REGISTER);
1214
1215 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1216 {
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)
1225 {
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);
1229 }
1230 }
1231
1232 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1233 {
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);
1257 }
1258
1259 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1260 {
1261 compare_values (TYPE_MODE);
1262 compare_values (TYPE_NEEDS_CONSTRUCTING);
1263 if (RECORD_OR_UNION_TYPE_P (t1))
1264 {
1265 compare_values (TYPE_TRANSPARENT_AGGR);
1266 compare_values (TYPE_FINAL_P);
1267 compare_values (TYPE_CXX_ODR_P);
1268 }
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. */
1289 }
1290
1291 /* We don't want to compare locations, so there is nothing do compare
1292 for TS_EXP. */
1293
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;
1298
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;
1303
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;
1307
1308 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1309 if (!cl_optimization_option_eq (TREE_OPTIMIZATION (t1),
1310 TREE_OPTIMIZATION (t2)))
1311 return false;
1312
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;
1317
1318 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1319 {
1320 compare_values (CLOBBER_KIND);
1321 compare_values (CONSTRUCTOR_NELTS);
1322 }
1323
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;
1329
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;
1335
1336 if (code == OMP_CLAUSE)
1337 {
1338 compare_values (OMP_CLAUSE_CODE);
1339 switch (OMP_CLAUSE_CODE (t1))
1340 {
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;
1363 }
1364 }
1365
1366#undef compare_values
1367
1368
1369 /* Compare pointer fields. */
1370
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)
1388
1389 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1390 {
1391 if (code != IDENTIFIER_NODE)
1392 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1393 }
1394
1395 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1396 {
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 (t: 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));
1403 }
1404
1405 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1406 {
1407 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1408 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1409 }
1410
1411 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1412 {
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))
1424 ;
1425 else
1426 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1427 }
1428
1429 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1430 {
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));
1446 }
1447
1448 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1449 {
1450 if (code == FUNCTION_DECL)
1451 {
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));
1458 }
1459 else if (code == TYPE_DECL)
1460 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1461 }
1462
1463 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1464 {
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));
1469 }
1470
1471 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1472 {
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));
1480 }
1481
1482 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1483 {
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));
1491 }
1492
1493 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1494 {
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))
1508 ;
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));
1514 }
1515
1516 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1517 {
1518 if (code == ARRAY_TYPE)
1519 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1520 else if (RECORD_OR_UNION_TYPE_P (t1))
1521 {
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);
1527 }
1528 else if (code == FUNCTION_TYPE
1529 || code == METHOD_TYPE)
1530 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1531
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));
1535 }
1536
1537 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1538 {
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));
1542 }
1543
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));
1547
1548 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1549 {
1550 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1551 compare_tree_edges (TREE_OPERAND (t1, i),
1552 TREE_OPERAND (t2, i));
1553
1554 /* BLOCKs are function local and we don't merge anything there. */
1555 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1556 return false;
1557 }
1558
1559 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1560 {
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. */
1573 }
1574
1575 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1576 {
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)
1581 {
1582 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1583 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1584 }
1585 }
1586
1587 if (code == OMP_CLAUSE)
1588 {
1589 int i;
1590
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));
1595 }
1596
1597#undef compare_tree_edges
1598
1599 return true;
1600}
1601
1602/* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1603 out MAP if they are equal. */
1604
1605static bool
1606compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1607 tree *map)
1608{
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)
1614 {
1615 tree *mapp = map;
1616 num_scc_compare_collisions++;
1617 if (compare_tree_sccs_1 (t1: pscc->entries[0], t2: scc->entries[i], map: &mapp))
1618 {
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;
1622 }
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;
1627 }
1628
1629 return false;
1630}
1631
1632/* QSort sort function to sort a map of two pointers after the 2nd
1633 pointer. */
1634
1635static int
1636cmp_tree (const void *p1_, const void *p2_)
1637{
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;
1643}
1644
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. */
1647
1648static void
1649process_dref (class data_in *data_in, tree t, unsigned from)
1650{
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)
1656 {
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);
1664 }
1665}
1666
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. */
1670
1671static bool
1672unify_scc (class data_in *data_in, unsigned from,
1673 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1674{
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)
1684 {
1685 tree t = streamer_tree_cache_get_tree (cache, ix: 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)))));
1697 }
1698
1699 /* Look for the list of candidate SCCs to compare against. */
1700 tree_scc **slot;
1701 slot = tree_scc_hash->find_slot_with_hash (comparable: scc, hash: scc_hash, insert: INSERT);
1702 if (*slot)
1703 {
1704 /* Try unifying against each candidate. */
1705 num_scc_compares++;
1706
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)
1714 {
1715 TREE_VISITED (scc->entries[i]) = 1;
1716 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1717 }
1718
1719 tree *map = XALLOCAVEC (tree, 2 * len);
1720 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1721 {
1722 if (!compare_tree_sccs (pscc, scc, map))
1723 continue;
1724
1725 /* Found an equal SCC. */
1726 unified_p = true;
1727 num_scc_compare_collisions--;
1728 num_sccs_merged++;
1729 total_scc_size_merged += len;
1730
1731 if (flag_checking)
1732 for (unsigned i = 0; i < len; ++i)
1733 {
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);
1741 }
1742
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)
1746 {
1747 process_dref (data_in, t: pscc->entries[0], from);
1748 lto_maybe_register_decl (data_in, t: pscc->entries[0], ix: from);
1749 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1750 }
1751 else
1752 {
1753 tree *map2 = XALLOCAVEC (tree, 2 * len);
1754 for (unsigned i = 0; i < len; ++i)
1755 {
1756 map2[i*2] = (tree)(uintptr_t)(from + i);
1757 map2[i*2+1] = scc->entries[i];
1758 }
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)
1762 {
1763 lto_maybe_register_decl (data_in, t: map[2*i],
1764 ix: (uintptr_t)map2[2*i]);
1765 streamer_tree_cache_replace_tree (cache, map[2*i],
1766 (uintptr_t)map2[2*i]);
1767 }
1768 }
1769
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)
1773 {
1774 if (TYPE_P (scc->entries[i]))
1775 num_merged_types++;
1776 free_node (scc->entries[i]);
1777 }
1778
1779 /* Drop DIE references.
1780 ??? Do as in the size-one SCC case which involves sorting
1781 the queue. */
1782 dref_queue.truncate (size: 0);
1783
1784 break;
1785 }
1786
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;
1791 }
1792
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)
1796 {
1797 tree_scc *pscc
1798 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1799 memcpy (dest: pscc, src: scc, n: sizeof (tree_scc));
1800 pscc->next = (*slot);
1801 *slot = pscc;
1802 }
1803 return unified_p;
1804}
1805
1806typedef int_hash<unsigned, 0, UINT_MAX> code_id_hash;
1807
1808/* Do registering necessary once new tree fully streamed in (including all
1809 trees it reffers to). */
1810
1811static void
1812process_new_tree (tree t, hash_map <code_id_hash, unsigned> *hm,
1813 unsigned index, unsigned *total, class data_in *data_in)
1814{
1815 /* Reconstruct the type variant and pointer-to/reference-to
1816 chains. */
1817 if (TYPE_P (t))
1818 {
1819 /* Map the tree types to their frequencies. */
1820 if (flag_lto_dump_type_stats)
1821 {
1822 unsigned key = (unsigned) TREE_CODE (t);
1823 unsigned *countp = hm->get (k: key);
1824 hm->put (k: key, v: countp ? (*countp) + 1 : 1);
1825 (*total)++;
1826 }
1827
1828 num_prevailing_types++;
1829 lto_fixup_prevailing_type (t);
1830
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.
1836
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))
1841 {
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 (v&: types_to_register, obj: t);
1847 }
1848 if (TYPE_MAIN_VARIANT (t) == t && odr_type_p (t))
1849 register_odr_type (t);
1850 }
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)
1857 {
1858 lto_maybe_register_decl (data_in, t, ix: 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 (v&: tree_with_vars, obj: t);
1863 }
1864}
1865
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). */
1869
1870static void
1871lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1872 vec<ld_plugin_symbol_resolution_t> resolutions)
1873{
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;
1882
1883 lto_input_block ib_main ((const char *) data + main_offset,
1884 header->main_size, decl_data);
1885
1886 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1887 header->string_size, resolutions);
1888
1889 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1890 internal types that should not be merged. */
1891
1892 hash_map <code_id_hash, unsigned> hm;
1893 unsigned total = 0;
1894
1895 /* Read the global declarations and types. */
1896 while (ib_main.p < ib_main.len)
1897 {
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: &ib_main);
1902 if (tag == LTO_tree_scc || tag == LTO_trees)
1903 {
1904 unsigned len_;
1905 unsigned scc_entry_len;
1906
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_);
1914
1915 if (tag == LTO_tree_scc)
1916 {
1917 total_scc_size += len;
1918 num_sccs_read++;
1919 }
1920 else
1921 num_unshared_trees_read += len;
1922
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 (cache: data_in->reader_cache,
1927 ix: 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);
1934
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;
1940
1941 /* Tree merging failed, mark entries in location cache as
1942 permanent. */
1943 data_in->location_cache.accept_location_cache ();
1944
1945 bool seen_type = false;
1946 for (unsigned i = 0; i < len; ++i)
1947 {
1948 tree t = streamer_tree_cache_get_tree (cache: data_in->reader_cache,
1949 ix: from + i);
1950 process_new_tree (t, hm: &hm, index: from + i, total: &total, data_in);
1951 if (TYPE_P (t))
1952 seen_type = true;
1953 }
1954
1955 /* Register DECLs with the debuginfo machinery. */
1956 while (!dref_queue.is_empty ())
1957 {
1958 dref_entry e = dref_queue.pop ();
1959 debug_hooks->register_external_die (e.decl, e.sym, e.off);
1960 }
1961
1962 if (seen_type)
1963 num_type_scc_trees += len;
1964 }
1965 else
1966 {
1967 t = lto_input_tree_1 (&ib_main, data_in, tag, hash: 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)))
1975 ;
1976 else
1977 {
1978 lto_maybe_register_decl (data_in, t, ix: from);
1979 process_new_tree (t, hm: &hm, index: from, total: &total, data_in);
1980 }
1981 }
1982 }
1983
1984 /* Dump type statistics. */
1985 if (flag_lto_dump_type_stats)
1986 {
1987 fprintf (stdout, format: " Type Frequency Percentage\n\n");
1988 for (hash_map<code_id_hash, unsigned>::iterator itr = hm.begin ();
1989 itr != hm.end ();
1990 ++itr)
1991 {
1992 std::pair<unsigned, unsigned> p = *itr;
1993 enum tree_code code = (enum tree_code) p.first;
1994 fprintf (stdout, format: "%14s %6d %12.2f\n", get_tree_code_name (code),
1995 p.second, float (p.second)/total*100);
1996 }
1997 }
1998
1999 data_in->location_cache.apply_location_cache ();
2000
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++;
2006
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: data_ptr,
2010 state: decl_data->global_decl_state);
2011
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 (n: 37);
2015
2016 for (i = 1; i < num_decl_states; i++)
2017 {
2018 struct lto_in_decl_state *state = lto_new_in_decl_state ();
2019
2020 data_ptr = lto_read_in_decl_state (data_in, data: data_ptr, state);
2021 lto_in_decl_state **slot
2022 = decl_data->function_decl_states->find_slot (value: state, insert: INSERT);
2023 gcc_assert (*slot == NULL);
2024 *slot = state;
2025 }
2026
2027 if (data_ptr != data_end)
2028 internal_error ("bytecode stream: garbage at the end of symbols section");
2029
2030 /* Set the current decl state to be the global state. */
2031 decl_data->current_decl_state = decl_data->global_decl_state;
2032
2033 lto_data_in_delete (data_in);
2034}
2035
2036/* Custom version of strtoll, which is not portable. */
2037
2038static int64_t
2039lto_parse_hex (const char *p)
2040{
2041 int64_t ret = 0;
2042
2043 for (; *p != '\0'; ++p)
2044 {
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;
2057 }
2058
2059 return ret;
2060}
2061
2062/* Read resolution for file named FILE_NAME. The resolution is read from
2063 RESOLUTION. */
2064
2065static void
2066lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2067{
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;
2076
2077 if (!resolution)
2078 return;
2079
2080 name_len = strlen (s: file->filename);
2081 obj_name = XNEWVEC (char, name_len + 1);
2082 fscanf (stream: resolution, format: " "); /* Read white space. */
2083
2084 fread (ptr: obj_name, size: sizeof (char), n: name_len, stream: resolution);
2085 obj_name[name_len] = '\0';
2086 if (filename_cmp (s1: obj_name, s2: 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)
2090 {
2091 int t;
2092 char offset_p[17];
2093 int64_t offset;
2094 t = fscanf (stream: resolution, format: "@0x%16s", offset_p);
2095 if (t != 1)
2096 internal_error ("could not parse file offset");
2097 offset = lto_parse_hex (p: offset_p);
2098 if (offset != file->offset)
2099 internal_error ("unexpected offset");
2100 }
2101
2102 free (ptr: obj_name);
2103
2104 fscanf (stream: resolution, format: "%u", &num_symbols);
2105
2106 for (i = 0; i < num_symbols; i++)
2107 {
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;
2116
2117 t = fscanf (stream: resolution, format: "%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");
2121
2122 for (j = 0; j < lto_resolution_str_len; j++)
2123 {
2124 if (strcmp (s1: lto_resolution_str[j], s2: r_str) == 0)
2125 {
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;
2139 }
2140 }
2141 if (j == lto_resolution_str_len)
2142 internal_error ("invalid resolution in the resolution file");
2143
2144 if (!(nd && lto_splay_tree_id_equal_p (key: nd->key, id)))
2145 {
2146 nd = lto_splay_tree_lookup (t: file_ids, id);
2147 if (nd == NULL)
2148 internal_error ("resolution sub id %wx not in object file", id);
2149 }
2150
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 (obj: rp);
2157 if (file_data->max_index < index)
2158 file_data->max_index = index;
2159 }
2160}
2161
2162/* List of file_decl_datas. */
2163struct file_data_list
2164{
2165 struct lto_file_decl_data *first, *last;
2166};
2167
2168/* Is the name for a id'ed LTO section? */
2169
2170static int
2171lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2172{
2173 const char *s;
2174
2175 if (strncmp (s1: name, s2: section_name_prefix, n: strlen (s: section_name_prefix)))
2176 return 0;
2177 s = strrchr (s: name, c: '.');
2178 if (!s)
2179 return 0;
2180 /* If the section is not suffixed with an ID return. */
2181 if ((size_t)(s - name) == strlen (s: section_name_prefix))
2182 return 0;
2183 return sscanf (s: s, format: "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2184}
2185
2186/* Create file_data of each sub file id. */
2187
2188static int
2189create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2190 struct file_data_list *list)
2191{
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;
2198
2199 if (!lto_section_with_id (name: ls->name, id: &id))
2200 return 1;
2201
2202 /* Find hash table of sub module id. */
2203 nd = lto_splay_tree_lookup (t: file_ids, id);
2204 if (nd != NULL)
2205 {
2206 file_data = (struct lto_file_decl_data *)nd->value;
2207 }
2208 else
2209 {
2210 file_data = ggc_alloc<lto_file_decl_data> ();
2211 memset(s: file_data, c: 0, n: 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 (t: file_ids, id, file_data);
2215
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;
2221
2222 list->last = file_data;
2223 }
2224
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);
2230
2231 new_slot = XDUP (struct lto_section_slot, ls);
2232 new_slot->name = new_name;
2233 *hash_slot = new_slot;
2234 return 1;
2235}
2236
2237/* Read declarations and other initializations for a FILE_DATA. */
2238
2239static void
2240lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file,
2241 int order)
2242{
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;
2249
2250 /* Create vector for fast access of resolution. We do this lazily
2251 to save memory. */
2252 resolutions.safe_grow_cleared (len: file_data->max_index + 1, exact: true);
2253 for (i = 0; file_data->respairs.iterate (ix: i, ptr: &rp); i++)
2254 resolutions[rp->index] = rp->res;
2255 file_data->respairs.release ();
2256
2257 file_data->renaming_hash_table = lto_create_renaming_table ();
2258 file_data->file_name = file->filename;
2259 file_data->order = order;
2260
2261 /* Read and verify LTO section. */
2262 data = lto_get_summary_section_data (file_data, LTO_section_lto, &len);
2263 if (data == NULL)
2264 {
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;
2268 }
2269
2270 memcpy (dest: &file_data->lto_section_header, src: data, n: 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);
2274
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 (x: MAX_MACHINE_MODE);
2280#endif
2281
2282 data = lto_get_summary_section_data (file_data, LTO_section_decls, &len);
2283 if (data == NULL)
2284 {
2285 internal_error ("cannot read %<LTO_section_decls%> from %s",
2286 file_data->file_name);
2287 return;
2288 }
2289 /* Frees resolutions. */
2290 lto_read_decls (decl_data: file_data, data, resolutions);
2291 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2292}
2293
2294/* Finalize FILE_DATA in FILE and increase COUNT. */
2295
2296static int
2297lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2298 int *count, int order)
2299{
2300 lto_file_finalize (file_data, file, order);
2301 if (symtab->dump_file)
2302 fprintf (stream: symtab->dump_file,
2303 format: "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;
2307}
2308
2309/* Generate a TREE representation for all types and external decls
2310 entities in FILE.
2311
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. */
2315
2316static struct lto_file_decl_data *
2317lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2318{
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;
2325
2326 memset (s: &section_list, c: 0, n: sizeof (struct lto_section_list));
2327 section_hash_table = lto_obj_build_section_table (file, list: &section_list);
2328
2329 /* Dump the details of LTO objects. */
2330 if (flag_lto_dump_objects)
2331 {
2332 int i=0;
2333 fprintf (stdout, format: "\n LTO Object Name: %s\n", file->filename);
2334 fprintf (stdout, format: "\nNo. Offset Size Section Name\n\n");
2335 for (section = section_list.first; section != NULL; section = section->next)
2336 fprintf (stdout, format: "%2d %8" PRId64 " %8" PRIu64 " %s\n",
2337 ++i, (int64_t) section->start, (uint64_t) section->len,
2338 section->name);
2339 }
2340
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 (s: &file_list, c: 0, n: sizeof (struct file_data_list));
2345 for (section = section_list.first; section != NULL; section = section->next)
2346 create_subid_section_table (ls: section, file_ids, list: &file_list);
2347
2348 /* Add resolutions to file ids. */
2349 lto_resolution_read (file_ids, resolution: resolution_file, file);
2350
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: order++);
2356
2357 splay_tree_delete (file_ids);
2358 htab_delete (section_hash_table);
2359
2360 return file_list.first;
2361}
2362
2363#if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2364#define LTO_MMAP_IO 1
2365#endif
2366
2367#if LTO_MMAP_IO
2368/* Page size of machine is used for mmap and munmap calls. */
2369static size_t page_mask;
2370#endif
2371
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. */
2375
2376static char *
2377lto_read_section_data (struct lto_file_decl_data *file_data,
2378 intptr_t offset, size_t len)
2379{
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
2388
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 (s1: fd_name, s2: file_data->file_name) != 0)
2396 {
2397 free (ptr: fd_name);
2398 close (fd: fd);
2399 fd = -1;
2400 }
2401 if (fd == -1)
2402 {
2403 fd = open (file: file_data->file_name, O_RDONLY|O_BINARY);
2404 if (fd == -1)
2405 {
2406 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2407 return NULL;
2408 }
2409 fd_name = xstrdup (file_data->file_name);
2410 }
2411
2412#if LTO_MMAP_IO
2413 if (!page_mask)
2414 {
2415 size_t page_size = sysconf (_SC_PAGE_SIZE);
2416 page_mask = ~(page_size - 1);
2417 }
2418
2419 computed_offset = offset & page_mask;
2420 diff = offset - computed_offset;
2421 computed_len = len + diff;
2422
2423 result = (char *) mmap (NULL, len: computed_len, PROT_READ, MAP_PRIVATE,
2424 fd: fd, offset: computed_offset);
2425 if (result == MAP_FAILED)
2426 {
2427 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2428 return NULL;
2429 }
2430
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)
2436 {
2437 free (result);
2438 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2439 result = NULL;
2440 }
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
2452}
2453
2454
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. */
2458
2459static const char *
2460get_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)
2464{
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;
2471
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)
2476 {
2477 data = lto_read_section_data (file_data, offset: f_slot->start, len: f_slot->len);
2478 *len = f_slot->len;
2479 }
2480
2481 free (CONST_CAST (char *, section_name));
2482 return data;
2483}
2484
2485
2486/* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2487 starts at OFFSET and has LEN bytes. */
2488
2489static void
2490free_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)
2494{
2495#if LTO_MMAP_IO
2496 intptr_t computed_len;
2497 intptr_t computed_offset;
2498 intptr_t diff;
2499#endif
2500
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;
2505
2506 munmap (addr: (caddr_t) computed_offset, len: computed_len);
2507#else
2508 free (CONST_CAST(char *, offset));
2509#endif
2510}
2511
2512static lto_file *current_lto_file;
2513
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))) \
2520 { \
2521 tt = lto_symtab_prevailing_decl (tt); \
2522 fixed = true; \
2523 } \
2524 } while (0)
2525
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))
2529
2530/* Given a tree T replace all fields referring to variables or functions
2531 with their prevailing variant. */
2532static void
2533lto_fixup_prevailing_decls (tree t)
2534{
2535 enum tree_code code = TREE_CODE (t);
2536 bool fixed = false;
2537
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))
2546 {
2547 LTO_NO_PREVAIL (DECL_NAME (t));
2548 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2549 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2550 {
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));
2556 }
2557 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2558 {
2559 LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t));
2560 }
2561 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2562 {
2563 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2564 }
2565 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2566 {
2567 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2568 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2569 LTO_NO_PREVAIL (DECL_VINDEX (t));
2570 }
2571 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2572 {
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));
2578 }
2579 }
2580 else if (TYPE_P (t))
2581 {
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));
2587
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));
2591
2592 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2593
2594 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2595 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2596 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2597 }
2598 else if (EXPR_P (t))
2599 {
2600 int i;
2601 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2602 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2603 }
2604 else if (TREE_CODE (t) == CONSTRUCTOR)
2605 {
2606 unsigned i;
2607 tree val;
2608 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2609 LTO_SET_PREVAIL (val);
2610 }
2611 else
2612 {
2613 switch (code)
2614 {
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 ();
2621 }
2622 }
2623 /* If we fixed nothing, then we missed something seen by
2624 mentions_vars_p. */
2625 gcc_checking_assert (fixed);
2626}
2627#undef LTO_SET_PREVAIL
2628#undef LTO_NO_PREVAIL
2629
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. */
2632
2633static void
2634lto_fixup_state (struct lto_in_decl_state *state)
2635{
2636 unsigned i, si;
2637
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++)
2642 {
2643 vec<tree, va_gc> *trees = state->streams[si];
2644 for (i = 0; i < vec_safe_length (v: trees); i++)
2645 {
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 (decl: t);
2652 }
2653 }
2654}
2655
2656/* Fix the decls from all FILES. Replaces each decl with the corresponding
2657 prevailing one. */
2658
2659static void
2660lto_fixup_decls (struct lto_file_decl_data **files)
2661{
2662 unsigned int i;
2663 tree t;
2664
2665 if (tree_with_vars)
2666 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2667 lto_fixup_prevailing_decls (t);
2668
2669 for (i = 0; files[i]; i++)
2670 {
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);
2674
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 (state: elt);
2680 }
2681}
2682
2683static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2684
2685/* Turn file datas for sub files into a single array, so that they look
2686 like separate files for further passes. */
2687
2688static void
2689lto_flatten_files (struct lto_file_decl_data **orig, int count,
2690 int last_file_ix)
2691{
2692 struct lto_file_decl_data *n, *next;
2693 int i, k;
2694
2695 lto_stats.num_input_files = count;
2696 all_file_decl_data
2697 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (c: 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++)
2701 {
2702 for (n = orig[i]; n != NULL; n = next)
2703 {
2704 all_file_decl_data[k++] = n;
2705 next = n->next;
2706 n->next = NULL;
2707 }
2708 }
2709 all_file_decl_data[k] = NULL;
2710 gcc_assert (k == count);
2711}
2712
2713/* Input file data before flattening (i.e. splitting them to subfiles to support
2714 incremental linking. */
2715static int real_file_count;
2716static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2717
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. */
2722
2723void
2724read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2725{
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;
2732
2733 symtab->initialize ();
2734
2735 timevar_push (tv: TV_IPA_LTO_DECL_IN);
2736
2737#ifdef ACCEL_COMPILER
2738 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2739 lto_stream_offload_p = true;
2740#endif
2741
2742 real_file_decl_data
2743 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (c: nfiles + 1);
2744 real_file_count = nfiles;
2745
2746 /* Read the resolution file. */
2747 resolution = NULL;
2748 if (resolution_file_name)
2749 {
2750 int t;
2751
2752 resolution = fopen (filename: resolution_file_name, modes: "r");
2753 if (resolution == NULL)
2754 fatal_error (input_location,
2755 "could not open symbol resolution file: %m");
2756
2757 t = fscanf (stream: resolution, format: "%u", &resolution_objects);
2758 gcc_assert (t == 1);
2759 }
2760 symtab->state = LTO_STREAMING;
2761
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);
2767
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 (node: integer_types[i], first_p: true);
2775 for (i = 0; i < stk_type_kind_last; ++i)
2776 lto_register_canonical_types (node: sizetype_tab[i], first_p: true);
2777 for (i = 0; i < TI_MAX; ++i)
2778 lto_register_canonical_types (node: global_trees[i], first_p: true);
2779 for (i = 0; i < itk_none; ++i)
2780 lto_register_canonical_types (node: integer_types[i], first_p: false);
2781 for (i = 0; i < stk_type_kind_last; ++i)
2782 lto_register_canonical_types (node: sizetype_tab[i], first_p: false);
2783 for (i = 0; i < TI_MAX; ++i)
2784 lto_register_canonical_types (node: global_trees[i], first_p: false);
2785
2786 if (!quiet_flag)
2787 fprintf (stderr, format: "Reading object files:");
2788
2789 /* Read all of the object files specified on the command line. */
2790 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2791 {
2792 struct lto_file_decl_data *file_data = NULL;
2793 if (!quiet_flag)
2794 {
2795 fprintf (stderr, format: " %s", fnames[i]);
2796 fflush (stderr);
2797 }
2798
2799 current_lto_file = lto_obj_file_open (filename: fnames[i], writable: false);
2800 if (!current_lto_file)
2801 break;
2802
2803 file_data = lto_file_read (file: current_lto_file, resolution_file: resolution, count: &count);
2804 if (!file_data)
2805 {
2806 lto_obj_file_close (file: current_lto_file);
2807 free (ptr: current_lto_file);
2808 current_lto_file = NULL;
2809 break;
2810 }
2811
2812 decl_data[last_file_ix++] = file_data;
2813
2814 lto_obj_file_close (file: current_lto_file);
2815 free (ptr: current_lto_file);
2816 current_lto_file = NULL;
2817 }
2818
2819 lto_flatten_files (orig: decl_data, count, last_file_ix);
2820 lto_stats.num_input_files = count;
2821 ggc_free(decl_data);
2822 real_file_decl_data = NULL;
2823
2824 lto_register_canonical_types_for_odr_types ();
2825
2826 if (resolution_file_name)
2827 {
2828 /* True, since the plugin splits the archives. */
2829 gcc_assert (resolution_objects == nfiles);
2830 fclose (stream: resolution);
2831 }
2832
2833 /* Show the LTO report before launching LTRANS. */
2834 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2835 print_lto_report_1 ();
2836
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;
2845
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 ();
2850
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);
2853
2854 timevar_pop (tv: TV_IPA_LTO_DECL_IN);
2855
2856 if (!quiet_flag)
2857 fprintf (stderr, format: "\nReading the symbol table:");
2858
2859 timevar_push (tv: TV_IPA_LTO_CGRAPH_IO);
2860 /* Read the symtab. */
2861 input_symtab ();
2862
2863 input_offload_tables (!flag_ltrans);
2864
2865 /* Store resolutions into the symbol table. */
2866
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 (node: snode->decl))
2872 && !(VAR_P (snode->decl) && DECL_HARD_REGISTER (snode->decl)))
2873 {
2874 ld_plugin_symbol_resolution_t *res;
2875
2876 res = snode->lto_file_data->resolution_map->get (k: snode->decl);
2877 if (!res || *res == LDPR_UNKNOWN)
2878 {
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)));
2883 }
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;
2891 }
2892 for (i = 0; all_file_decl_data[i]; i++)
2893 if (all_file_decl_data[i]->resolution_map)
2894 {
2895 delete all_file_decl_data[i]->resolution_map;
2896 all_file_decl_data[i]->resolution_map = NULL;
2897 }
2898
2899 timevar_pop (tv: TV_IPA_LTO_CGRAPH_IO);
2900
2901 if (!quiet_flag)
2902 fprintf (stderr, format: "\nMerging declarations:");
2903
2904 timevar_push (tv: 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)
2910 {
2911 lto_symtab_merge_decls ();
2912
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");
2918
2919 /* Fixup all decls. */
2920 lto_fixup_decls (files: all_file_decl_data);
2921 }
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 ();
2931
2932 timevar_pop (tv: TV_IPA_LTO_DECL_MERGE);
2933 /* Each pass will set the appropriate timer. */
2934
2935 if (!quiet_flag)
2936 fprintf (stderr, format: "\nReading summaries:");
2937
2938 /* Read the IPA summary data. */
2939 if (flag_ltrans)
2940 ipa_read_optimization_summaries ();
2941 else
2942 ipa_read_summaries ();
2943
2944 ggc_grow ();
2945
2946 for (i = 0; all_file_decl_data[i]; i++)
2947 {
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;
2956 }
2957
2958 if (!flag_ltrans)
2959 {
2960 /* Finally merge the cgraph according to the decl merging decisions. */
2961 timevar_push (tv: TV_IPA_LTO_CGRAPH_MERGE);
2962
2963 if (!quiet_flag)
2964 fprintf (stderr, format: "\nMerging symbols:");
2965
2966 gcc_assert (!dump_file);
2967 dump_file = dump_begin (lto_link_dump_id, NULL);
2968
2969 if (dump_file)
2970 {
2971 fprintf (stream: dump_file, format: "Before merging:\n");
2972 symtab->dump (f: dump_file);
2973 }
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 (file: dump_file);
2979 ggc_collect ();
2980 report_heap_memory_use ();
2981
2982 if (dump_file)
2983 dump_end (lto_link_dump_id, dump_file);
2984 dump_file = NULL;
2985 timevar_pop (tv: TV_IPA_LTO_CGRAPH_MERGE);
2986 }
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)
2994 {
2995 if (symtab->dump_file)
2996 symtab->dump (f: symtab->dump_file);
2997 symtab->remove_unreachable_nodes (file: symtab->dump_file);
2998 }
2999
3000 /* Indicate that the cgraph is built and ready. */
3001 symtab->function_flags_ready = true;
3002
3003 ggc_free (all_file_decl_data);
3004 all_file_decl_data = NULL;
3005}
3006
3007
3008
3009/* Show various memory usage statistics related to LTO. */
3010void
3011print_lto_report_1 (void)
3012{
3013 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3014 fprintf (stderr, format: "%s statistics\n", pfx);
3015
3016 fprintf (stderr, format: "[%s] read %lu unshared trees\n",
3017 pfx, num_unshared_trees_read);
3018 fprintf (stderr, format: "[%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, format: "[%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)
3023 {
3024 fprintf (stderr, format: "[%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)
3033 {
3034 unsigned length = 0;
3035 tree_scc *s = scc;
3036 for (; s; s = s->next)
3037 length++;
3038 if (length > max_length)
3039 {
3040 max_length = length;
3041 max_scc = scc;
3042 }
3043 }
3044 fprintf (stderr, format: "[%s] tree SCC max chain length %u (size %u)\n",
3045 pfx, max_length, max_scc->len);
3046 fprintf (stderr, format: "[%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, format: "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3050 fprintf (stderr, format: "[%s] Merged %lu tree bodies\n", pfx,
3051 total_scc_size_merged);
3052 fprintf (stderr, format: "[%s] Merged %lu types\n", pfx, num_merged_types);
3053 fprintf (stderr, format: "[%s] %lu types prevailed (%lu associated trees)\n",
3054 pfx, num_prevailing_types, num_type_scc_trees);
3055 fprintf (stderr, format: "[%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, format: "[%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);
3066 }
3067
3068 print_lto_report (pfx);
3069}
3070
3071GTY(()) tree lto_eh_personality_decl;
3072
3073/* Return the LTO personality function decl. */
3074
3075tree
3076lto_eh_personality (void)
3077{
3078 if (!lto_eh_personality_decl)
3079 {
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 ();
3087 }
3088
3089 return lto_eh_personality_decl;
3090}
3091
3092/* Set the process name based on the LTO mode. */
3093
3094static void
3095lto_process_name (void)
3096{
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");
3104}
3105
3106
3107/* Initialize the LTO front end. */
3108
3109void
3110lto_fe_init (void)
3111{
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 (s: &lto_stats, c: 0, n: sizeof (lto_stats));
3117 bitmap_obstack_initialize (NULL);
3118 gimple_register_cfg_hooks ();
3119}
3120
3121#include "gt-lto-lto-common.h"
3122

source code of gcc/lto/lto-common.cc