]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lto/lto-common.cc
Change references of .c files to .cc files
[thirdparty/gcc.git] / gcc / lto / lto-common.cc
CommitLineData
a79420f9 1/* Top-level LTO routines.
7adcbafe 2 Copyright (C) 2009-2022 Free Software Foundation, Inc.
a79420f9
ML
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"
a79420f9
ML
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"
a0276c00 58#include "tree-pretty-print.h"
03d90a20 59#include "print-tree.h"
a0276c00
JH
60
61/* True when no new types are going to be streamd from the global stream. */
62
63static bool type_streaming_finished = false;
a79420f9
ML
64
65GTY(()) tree first_personality_decl;
66
67GTY(()) const unsigned char *lto_mode_identity_table;
68
69/* Returns a hash code for P. */
70
71static hashval_t
72hash_name (const void *p)
73{
74 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
75 return (hashval_t) htab_hash_string (ds->name);
76}
77
78
79/* Returns nonzero if P1 and P2 are equal. */
80
81static int
82eq_name (const void *p1, const void *p2)
83{
ee7a003f
ML
84 const struct lto_section_slot *s1
85 = (const struct lto_section_slot *) p1;
86 const struct lto_section_slot *s2
87 = (const struct lto_section_slot *) p2;
a79420f9
ML
88
89 return strcmp (s1->name, s2->name) == 0;
90}
91
ee7a003f 92/* Free lto_section_slot. */
a79420f9
ML
93
94static void
95free_with_string (void *arg)
96{
97 struct lto_section_slot *s = (struct lto_section_slot *)arg;
98
99 free (CONST_CAST (char *, s->name));
100 free (arg);
101}
102
ee7a003f 103/* Create section hash table. */
a79420f9 104
ee7a003f 105htab_t
a79420f9
ML
106lto_obj_create_section_hash_table (void)
107{
108 return htab_create (37, hash_name, eq_name, free_with_string);
109}
110
111/* Delete an allocated integer KEY in the splay tree. */
112
113static void
114lto_splay_tree_delete_id (splay_tree_key key)
115{
116 free ((void *) key);
117}
118
119/* Compare splay tree node ids A and B. */
120
121static int
122lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
123{
124 unsigned HOST_WIDE_INT ai;
125 unsigned HOST_WIDE_INT bi;
126
127 ai = *(unsigned HOST_WIDE_INT *) a;
128 bi = *(unsigned HOST_WIDE_INT *) b;
129
130 if (ai < bi)
131 return -1;
132 else if (ai > bi)
133 return 1;
134 return 0;
135}
136
137/* Look up splay tree node by ID in splay tree T. */
138
139static splay_tree_node
140lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
141{
142 return splay_tree_lookup (t, (splay_tree_key) &id);
143}
144
145/* Check if KEY has ID. */
146
147static bool
148lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
149{
150 return *(unsigned HOST_WIDE_INT *) key == id;
151}
152
ee7a003f 153/* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
a79420f9 154 The ID is allocated separately because we need HOST_WIDE_INTs which may
ee7a003f 155 be wider than a splay_tree_key. */
a79420f9
ML
156
157static void
158lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
159 struct lto_file_decl_data *file_data)
160{
161 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
162 *idp = id;
163 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
164}
165
166/* Create a splay tree. */
167
168static splay_tree
169lto_splay_tree_new (void)
170{
171 return splay_tree_new (lto_splay_tree_compare_ids,
ee7a003f 172 lto_splay_tree_delete_id,
a79420f9
ML
173 NULL);
174}
175
176/* Decode the content of memory pointed to by DATA in the in decl
ee7a003f
ML
177 state object STATE. DATA_IN points to a data_in structure for
178 decoding. Return the address after the decoded object in the
a79420f9
ML
179 input. */
180
181static const uint32_t *
99b1c316 182lto_read_in_decl_state (class data_in *data_in, const uint32_t *data,
a79420f9
ML
183 struct lto_in_decl_state *state)
184{
185 uint32_t ix;
186 tree decl;
187 uint32_t i, j;
188
189 ix = *data++;
190 state->compressed = ix & 1;
191 ix /= 2;
192 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
193 if (!VAR_OR_FUNCTION_DECL_P (decl))
194 {
195 gcc_assert (decl == void_type_node);
196 decl = NULL_TREE;
197 }
198 state->fn_decl = decl;
199
200 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
201 {
202 uint32_t size = *data++;
203 vec<tree, va_gc> *decls = NULL;
204 vec_alloc (decls, size);
205
206 for (j = 0; j < size; j++)
207 vec_safe_push (decls,
208 streamer_tree_cache_get_tree (data_in->reader_cache,
209 data[j]));
210
211 state->streams[i] = decls;
212 data += size;
213 }
214
215 return data;
216}
217
218
219/* Global canonical type table. */
220static htab_t gimple_canonical_types;
221static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
222static unsigned long num_canonical_type_hash_entries;
223static unsigned long num_canonical_type_hash_queries;
224
a0276c00
JH
225/* Types postponed for registration to the canonical type table.
226 During streaming we postpone all TYPE_CXX_ODR_P types so we can alter
227 decide whether there is conflict with non-ODR type or not. */
228static GTY(()) vec<tree, va_gc> *types_to_register = NULL;
229
a79420f9
ML
230static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
231static hashval_t gimple_canonical_type_hash (const void *p);
a0276c00 232static hashval_t gimple_register_canonical_type_1 (tree t, hashval_t hash);
a79420f9
ML
233
234/* Returning a hash value for gimple type TYPE.
235
236 The hash value returned is equal for types considered compatible
237 by gimple_canonical_types_compatible_p. */
238
239static hashval_t
240hash_canonical_type (tree type)
241{
242 inchash::hash hstate;
243 enum tree_code code;
244
245 /* We compute alias sets only for types that needs them.
246 Be sure we do not recurse to something else as we cannot hash incomplete
247 types in a way they would have same hash value as compatible complete
248 types. */
249 gcc_checking_assert (type_with_alias_set_p (type));
250
251 /* Combine a few common features of types so that types are grouped into
252 smaller sets; when searching for existing matching types to merge,
253 only existing types having the same features as the new type will be
254 checked. */
255 code = tree_code_for_canonical_type_merging (TREE_CODE (type));
256 hstate.add_int (code);
257 hstate.add_int (TYPE_MODE (type));
258
259 /* Incorporate common features of numerical types. */
260 if (INTEGRAL_TYPE_P (type)
261 || SCALAR_FLOAT_TYPE_P (type)
262 || FIXED_POINT_TYPE_P (type)
263 || TREE_CODE (type) == OFFSET_TYPE
264 || POINTER_TYPE_P (type))
265 {
266 hstate.add_int (TYPE_PRECISION (type));
267 if (!type_with_interoperable_signedness (type))
ee7a003f 268 hstate.add_int (TYPE_UNSIGNED (type));
a79420f9
ML
269 }
270
271 if (VECTOR_TYPE_P (type))
272 {
273 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
274 hstate.add_int (TYPE_UNSIGNED (type));
275 }
276
277 if (TREE_CODE (type) == COMPLEX_TYPE)
278 hstate.add_int (TYPE_UNSIGNED (type));
279
280 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
281 interoperable with "signed char". Unless all frontends are revisited to
282 agree on these types, we must ignore the flag completely. */
283
284 /* Fortran standard define C_PTR type that is compatible with every
285 C pointer. For this reason we need to glob all pointers into one.
286 Still pointers in different address spaces are not compatible. */
287 if (POINTER_TYPE_P (type))
288 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
289
290 /* For array types hash the domain bounds and the string flag. */
291 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
292 {
293 hstate.add_int (TYPE_STRING_FLAG (type));
294 /* OMP lowering can introduce error_mark_node in place of
295 random local decls in types. */
296 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
297 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
298 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
299 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
300 }
301
302 /* Recurse for aggregates with a single element type. */
303 if (TREE_CODE (type) == ARRAY_TYPE
304 || TREE_CODE (type) == COMPLEX_TYPE
305 || TREE_CODE (type) == VECTOR_TYPE)
306 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
307
308 /* Incorporate function return and argument types. */
309 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
310 {
311 unsigned na;
312 tree p;
313
314 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
315
316 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
317 {
318 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
319 na++;
320 }
321
322 hstate.add_int (na);
323 }
324
325 if (RECORD_OR_UNION_TYPE_P (type))
326 {
327 unsigned nf;
328 tree f;
329
330 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
331 if (TREE_CODE (f) == FIELD_DECL
332 && (! DECL_SIZE (f)
333 || ! integer_zerop (DECL_SIZE (f))))
334 {
335 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
336 nf++;
337 }
338
339 hstate.add_int (nf);
340 }
341
342 return hstate.end();
343}
344
345/* Returning a hash value for gimple type TYPE combined with VAL. */
346
347static void
348iterative_hash_canonical_type (tree type, inchash::hash &hstate)
349{
350 hashval_t v;
351
352 /* All type variants have same TYPE_CANONICAL. */
353 type = TYPE_MAIN_VARIANT (type);
354
355 if (!canonical_type_used_p (type))
356 v = hash_canonical_type (type);
357 /* An already processed type. */
358 else if (TYPE_CANONICAL (type))
359 {
360 type = TYPE_CANONICAL (type);
361 v = gimple_canonical_type_hash (type);
362 }
363 else
364 {
365 /* Canonical types should not be able to form SCCs by design, this
366 recursion is just because we do not register canonical types in
367 optimal order. To avoid quadratic behavior also register the
368 type here. */
369 v = hash_canonical_type (type);
a0276c00 370 v = gimple_register_canonical_type_1 (type, v);
a79420f9 371 }
a0276c00 372 hstate.merge_hash (v);
a79420f9
ML
373}
374
375/* Returns the hash for a canonical type P. */
376
377static hashval_t
378gimple_canonical_type_hash (const void *p)
379{
380 num_canonical_type_hash_queries++;
381 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
382 gcc_assert (slot != NULL);
383 return *slot;
384}
385
386
387
388/* Returns nonzero if P1 and P2 are equal. */
389
390static int
391gimple_canonical_type_eq (const void *p1, const void *p2)
392{
393 const_tree t1 = (const_tree) p1;
394 const_tree t2 = (const_tree) p2;
395 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
396 CONST_CAST_TREE (t2));
397}
398
399/* Main worker for gimple_register_canonical_type. */
400
a0276c00 401static hashval_t
a79420f9
ML
402gimple_register_canonical_type_1 (tree t, hashval_t hash)
403{
404 void **slot;
405
406 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t)
407 && type_with_alias_set_p (t)
408 && canonical_type_used_p (t));
409
a0276c00
JH
410 /* ODR types for which there is no ODR violation and we did not record
411 structurally equivalent non-ODR type can be treated as unique by their
412 name.
413
414 hash passed to gimple_register_canonical_type_1 is a structural hash
415 that we can use to lookup structurally equivalent non-ODR type.
416 In case we decide to treat type as unique ODR type we recompute hash based
417 on name and let TBAA machinery know about our decision. */
18dd2956
JH
418 if (RECORD_OR_UNION_TYPE_P (t) && odr_type_p (t)
419 && TYPE_CXX_ODR_P (t) && !odr_type_violation_reported_p (t))
a0276c00 420 {
b01659aa
JH
421 /* Anonymous namespace types never conflict with non-C++ types. */
422 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
423 slot = NULL;
424 else
425 {
426 /* Here we rely on fact that all non-ODR types was inserted into
427 canonical type hash and thus we can safely detect conflicts between
428 ODR types and interoperable non-ODR types. */
429 gcc_checking_assert (type_streaming_finished
430 && TYPE_MAIN_VARIANT (t) == t);
431 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash,
432 NO_INSERT);
433 }
a0276c00
JH
434 if (slot && !TYPE_CXX_ODR_P (*(tree *)slot))
435 {
436 tree nonodr = *(tree *)slot;
18dd2956 437 gcc_checking_assert (!flag_ltrans);
a0276c00
JH
438 if (symtab->dump_file)
439 {
440 fprintf (symtab->dump_file,
441 "ODR and non-ODR type conflict: ");
442 print_generic_expr (symtab->dump_file, t);
443 fprintf (symtab->dump_file, " and ");
444 print_generic_expr (symtab->dump_file, nonodr);
445 fprintf (symtab->dump_file, " mangled:%s\n",
446 IDENTIFIER_POINTER
447 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
448 }
449 /* Set canonical for T and all other ODR equivalent duplicates
450 including incomplete structures. */
451 set_type_canonical_for_odr_type (t, nonodr);
452 }
453 else
454 {
455 tree prevail = prevailing_odr_type (t);
456
457 if (symtab->dump_file)
458 {
459 fprintf (symtab->dump_file,
460 "New canonical ODR type: ");
461 print_generic_expr (symtab->dump_file, t);
462 fprintf (symtab->dump_file, " mangled:%s\n",
463 IDENTIFIER_POINTER
464 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
465 }
466 /* Set canonical for T and all other ODR equivalent duplicates
467 including incomplete structures. */
468 set_type_canonical_for_odr_type (t, prevail);
469 enable_odr_based_tbaa (t);
470 if (!type_in_anonymous_namespace_p (t))
471 hash = htab_hash_string (IDENTIFIER_POINTER
472 (DECL_ASSEMBLER_NAME
473 (TYPE_NAME (t))));
474 else
475 hash = TYPE_UID (t);
476
477 /* All variants of t now have TYPE_CANONICAL set to prevail.
478 Update canonical type hash cache accordingly. */
479 num_canonical_type_hash_entries++;
480 bool existed_p = canonical_type_hash_cache->put (prevail, hash);
481 gcc_checking_assert (!existed_p);
482 }
483 return hash;
484 }
485
a79420f9
ML
486 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
487 if (*slot)
488 {
489 tree new_type = (tree)(*slot);
490 gcc_checking_assert (new_type != t);
491 TYPE_CANONICAL (t) = new_type;
492 }
493 else
494 {
495 TYPE_CANONICAL (t) = t;
496 *slot = (void *) t;
497 /* Cache the just computed hash value. */
498 num_canonical_type_hash_entries++;
499 bool existed_p = canonical_type_hash_cache->put (t, hash);
500 gcc_assert (!existed_p);
501 }
a0276c00 502 return hash;
a79420f9
ML
503}
504
505/* Register type T in the global type table gimple_types and set
506 TYPE_CANONICAL of T accordingly.
507 This is used by LTO to merge structurally equivalent types for
508 type-based aliasing purposes across different TUs and languages.
509
e53b6e56 510 ??? This merging does not exactly match how the tree.cc middle-end
a79420f9
ML
511 functions will assign TYPE_CANONICAL when new types are created
512 during optimization (which at least happens for pointer and array
513 types). */
514
515static void
516gimple_register_canonical_type (tree t)
517{
518 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t)
519 || !canonical_type_used_p (t))
520 return;
521
522 /* Canonical types are same among all complete variants. */
523 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
524 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
525 else
526 {
ee7a003f
ML
527 hashval_t h = hash_canonical_type (TYPE_MAIN_VARIANT (t));
528 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t), h);
a79420f9
ML
529 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
530 }
531}
532
533/* Re-compute TYPE_CANONICAL for NODE and related types. */
534
535static void
536lto_register_canonical_types (tree node, bool first_p)
537{
538 if (!node
539 || !TYPE_P (node))
540 return;
541
542 if (first_p)
543 TYPE_CANONICAL (node) = NULL_TREE;
544
545 if (POINTER_TYPE_P (node)
546 || TREE_CODE (node) == COMPLEX_TYPE
547 || TREE_CODE (node) == ARRAY_TYPE)
548 lto_register_canonical_types (TREE_TYPE (node), first_p);
549
ee7a003f 550 if (!first_p)
a79420f9
ML
551 gimple_register_canonical_type (node);
552}
553
a0276c00
JH
554/* Finish canonical type calculation: after all units has been streamed in we
555 can check if given ODR type structurally conflicts with a non-ODR type. In
556 the first case we set type canonical according to the canonical type hash.
557 In the second case we use type names. */
558
559static void
560lto_register_canonical_types_for_odr_types ()
561{
562 tree t;
563 unsigned int i;
564
565 if (!types_to_register)
566 return;
567
568 type_streaming_finished = true;
569
570 /* Be sure that no types derived from ODR types was
571 not inserted into the hash table. */
572 if (flag_checking)
573 FOR_EACH_VEC_ELT (*types_to_register, i, t)
574 gcc_assert (!TYPE_CANONICAL (t));
575
576 /* Register all remaining types. */
577 FOR_EACH_VEC_ELT (*types_to_register, i, t)
606a9a8c
JH
578 {
579 /* For pre-streamed types like va-arg it is possible that main variant
580 is !CXX_ODR_P while the variant (which is streamed) is.
581 Copy CXX_ODR_P to make type verifier happy. This is safe because
582 in canonical type calculation we only consider main variants.
583 However we can not change this flag before streaming is finished
584 to not affect tree merging. */
585 TYPE_CXX_ODR_P (t) = TYPE_CXX_ODR_P (TYPE_MAIN_VARIANT (t));
586 if (!TYPE_CANONICAL (t))
587 gimple_register_canonical_type (t);
588 }
a0276c00
JH
589}
590
a79420f9
ML
591
592/* Remember trees that contains references to declarations. */
593vec <tree, va_gc> *tree_with_vars;
594
595#define CHECK_VAR(tt) \
596 do \
597 { \
598 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
599 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
600 return true; \
601 } while (0)
602
603#define CHECK_NO_VAR(tt) \
604 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
605
606/* Check presence of pointers to decls in fields of a tree_typed T. */
607
608static inline bool
609mentions_vars_p_typed (tree t)
610{
611 CHECK_NO_VAR (TREE_TYPE (t));
612 return false;
613}
614
615/* Check presence of pointers to decls in fields of a tree_common T. */
616
617static inline bool
618mentions_vars_p_common (tree t)
619{
620 if (mentions_vars_p_typed (t))
621 return true;
622 CHECK_NO_VAR (TREE_CHAIN (t));
623 return false;
624}
625
626/* Check presence of pointers to decls in fields of a decl_minimal T. */
627
628static inline bool
629mentions_vars_p_decl_minimal (tree t)
630{
631 if (mentions_vars_p_common (t))
632 return true;
633 CHECK_NO_VAR (DECL_NAME (t));
634 CHECK_VAR (DECL_CONTEXT (t));
635 return false;
636}
637
638/* Check presence of pointers to decls in fields of a decl_common T. */
639
640static inline bool
641mentions_vars_p_decl_common (tree t)
642{
643 if (mentions_vars_p_decl_minimal (t))
644 return true;
645 CHECK_VAR (DECL_SIZE (t));
646 CHECK_VAR (DECL_SIZE_UNIT (t));
647 CHECK_VAR (DECL_INITIAL (t));
648 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
649 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
650 return false;
651}
652
653/* Check presence of pointers to decls in fields of a decl_with_vis T. */
654
655static inline bool
656mentions_vars_p_decl_with_vis (tree t)
657{
658 if (mentions_vars_p_decl_common (t))
659 return true;
660
ee7a003f 661 /* Accessor macro has side-effects, use field-name here. */
a79420f9
ML
662 CHECK_NO_VAR (DECL_ASSEMBLER_NAME_RAW (t));
663 return false;
664}
665
666/* Check presence of pointers to decls in fields of a decl_non_common T. */
667
668static inline bool
669mentions_vars_p_decl_non_common (tree t)
670{
671 if (mentions_vars_p_decl_with_vis (t))
672 return true;
673 CHECK_NO_VAR (DECL_RESULT_FLD (t));
674 return false;
675}
676
677/* Check presence of pointers to decls in fields of a decl_non_common T. */
678
679static bool
680mentions_vars_p_function (tree t)
681{
682 if (mentions_vars_p_decl_non_common (t))
683 return true;
684 CHECK_NO_VAR (DECL_ARGUMENTS (t));
685 CHECK_NO_VAR (DECL_VINDEX (t));
686 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
687 return false;
688}
689
690/* Check presence of pointers to decls in fields of a field_decl T. */
691
692static bool
693mentions_vars_p_field_decl (tree t)
694{
695 if (mentions_vars_p_decl_common (t))
696 return true;
697 CHECK_VAR (DECL_FIELD_OFFSET (t));
698 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
699 CHECK_NO_VAR (DECL_QUALIFIER (t));
700 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
701 CHECK_NO_VAR (DECL_FCONTEXT (t));
702 return false;
703}
704
705/* Check presence of pointers to decls in fields of a type T. */
706
707static bool
708mentions_vars_p_type (tree t)
709{
710 if (mentions_vars_p_common (t))
711 return true;
712 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
713 CHECK_VAR (TYPE_SIZE (t));
714 CHECK_VAR (TYPE_SIZE_UNIT (t));
715 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
716 CHECK_NO_VAR (TYPE_NAME (t));
717
718 CHECK_VAR (TYPE_MIN_VALUE_RAW (t));
719 CHECK_VAR (TYPE_MAX_VALUE_RAW (t));
720
ee7a003f 721 /* Accessor is for derived node types only. */
a79420f9
ML
722 CHECK_NO_VAR (TYPE_LANG_SLOT_1 (t));
723
724 CHECK_VAR (TYPE_CONTEXT (t));
725 CHECK_NO_VAR (TYPE_CANONICAL (t));
726 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
727 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
728 return false;
729}
730
731/* Check presence of pointers to decls in fields of a BINFO T. */
732
733static bool
734mentions_vars_p_binfo (tree t)
735{
736 unsigned HOST_WIDE_INT i, n;
737
738 if (mentions_vars_p_common (t))
739 return true;
740 CHECK_VAR (BINFO_VTABLE (t));
741 CHECK_NO_VAR (BINFO_OFFSET (t));
742 CHECK_NO_VAR (BINFO_VIRTUALS (t));
743 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
744 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
745 for (i = 0; i < n; i++)
746 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
747 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
748 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
749 n = BINFO_N_BASE_BINFOS (t);
750 for (i = 0; i < n; i++)
751 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
752 return false;
753}
754
755/* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
756
757static bool
758mentions_vars_p_constructor (tree t)
759{
760 unsigned HOST_WIDE_INT idx;
761 constructor_elt *ce;
762
763 if (mentions_vars_p_typed (t))
764 return true;
765
766 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
767 {
768 CHECK_NO_VAR (ce->index);
769 CHECK_VAR (ce->value);
770 }
771 return false;
772}
773
774/* Check presence of pointers to decls in fields of an expression tree T. */
775
776static bool
777mentions_vars_p_expr (tree t)
778{
779 int i;
780 if (mentions_vars_p_typed (t))
781 return true;
782 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
783 CHECK_VAR (TREE_OPERAND (t, i));
784 return false;
785}
786
787/* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
788
789static bool
790mentions_vars_p_omp_clause (tree t)
791{
792 int i;
793 if (mentions_vars_p_common (t))
794 return true;
795 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
796 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
797 return false;
798}
799
800/* Check presence of pointers to decls that needs later fixup in T. */
801
802static bool
803mentions_vars_p (tree t)
804{
805 switch (TREE_CODE (t))
806 {
807 case IDENTIFIER_NODE:
808 break;
809
810 case TREE_LIST:
811 CHECK_VAR (TREE_VALUE (t));
812 CHECK_VAR (TREE_PURPOSE (t));
813 CHECK_NO_VAR (TREE_CHAIN (t));
814 break;
815
816 case FIELD_DECL:
817 return mentions_vars_p_field_decl (t);
818
819 case LABEL_DECL:
820 case CONST_DECL:
821 case PARM_DECL:
822 case RESULT_DECL:
823 case IMPORTED_DECL:
824 case NAMESPACE_DECL:
825 case NAMELIST_DECL:
826 return mentions_vars_p_decl_common (t);
827
828 case VAR_DECL:
829 return mentions_vars_p_decl_with_vis (t);
830
831 case TYPE_DECL:
832 return mentions_vars_p_decl_non_common (t);
833
834 case FUNCTION_DECL:
835 return mentions_vars_p_function (t);
836
837 case TREE_BINFO:
838 return mentions_vars_p_binfo (t);
839
840 case PLACEHOLDER_EXPR:
841 return mentions_vars_p_common (t);
842
843 case BLOCK:
844 case TRANSLATION_UNIT_DECL:
845 case OPTIMIZATION_NODE:
846 case TARGET_OPTION_NODE:
847 break;
848
849 case CONSTRUCTOR:
850 return mentions_vars_p_constructor (t);
851
852 case OMP_CLAUSE:
853 return mentions_vars_p_omp_clause (t);
854
855 default:
856 if (TYPE_P (t))
857 {
858 if (mentions_vars_p_type (t))
859 return true;
860 }
861 else if (EXPR_P (t))
862 {
863 if (mentions_vars_p_expr (t))
864 return true;
865 }
866 else if (CONSTANT_CLASS_P (t))
867 CHECK_NO_VAR (TREE_TYPE (t));
868 else
869 gcc_unreachable ();
870 }
871 return false;
872}
873
874
ee7a003f 875/* Return the resolution for the decl with index INDEX from DATA_IN. */
a79420f9
ML
876
877static enum ld_plugin_symbol_resolution
99b1c316 878get_resolution (class data_in *data_in, unsigned index)
a79420f9
ML
879{
880 if (data_in->globals_resolution.exists ())
881 {
882 ld_plugin_symbol_resolution_t ret;
883 /* We can have references to not emitted functions in
884 DECL_FUNCTION_PERSONALITY at least. So we can and have
ee7a003f 885 to indeed return LDPR_UNKNOWN in some cases. */
a79420f9
ML
886 if (data_in->globals_resolution.length () <= index)
887 return LDPR_UNKNOWN;
888 ret = data_in->globals_resolution[index];
889 return ret;
890 }
891 else
892 /* Delay resolution finding until decl merging. */
893 return LDPR_UNKNOWN;
894}
895
896/* We need to record resolutions until symbol table is read. */
897static void
898register_resolution (struct lto_file_decl_data *file_data, tree decl,
899 enum ld_plugin_symbol_resolution resolution)
900{
901 bool existed;
902 if (resolution == LDPR_UNKNOWN)
903 return;
904 if (!file_data->resolution_map)
905 file_data->resolution_map
906 = new hash_map<tree, ld_plugin_symbol_resolution>;
907 ld_plugin_symbol_resolution_t &res
908 = file_data->resolution_map->get_or_insert (decl, &existed);
909 if (!existed
910 || resolution == LDPR_PREVAILING_DEF_IRONLY
911 || resolution == LDPR_PREVAILING_DEF
912 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
913 res = resolution;
914}
915
916/* Register DECL with the global symbol table and change its
917 name if necessary to avoid name clashes for static globals across
918 different files. */
919
920static void
99b1c316 921lto_register_var_decl_in_symtab (class data_in *data_in, tree decl,
a79420f9
ML
922 unsigned ix)
923{
924 tree context;
925
926 /* Variable has file scope, not local. */
927 if (!TREE_PUBLIC (decl)
928 && !((context = decl_function_context (decl))
929 && auto_var_in_fn_p (decl, context)))
930 rest_of_decl_compilation (decl, 1, 0);
931
932 /* If this variable has already been declared, queue the
933 declaration for merging. */
934 if (TREE_PUBLIC (decl))
935 register_resolution (data_in->file_data,
936 decl, get_resolution (data_in, ix));
937}
938
939
940/* Register DECL with the global symbol table and change its
941 name if necessary to avoid name clashes for static globals across
942 different files. DATA_IN contains descriptors and tables for the
943 file being read. */
944
945static void
99b1c316 946lto_register_function_decl_in_symtab (class data_in *data_in, tree decl,
a79420f9
ML
947 unsigned ix)
948{
949 /* If this variable has already been declared, queue the
950 declaration for merging. */
951 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
952 register_resolution (data_in->file_data,
953 decl, get_resolution (data_in, ix));
954}
955
956/* Check if T is a decl and needs register its resolution info. */
957
958static void
99b1c316 959lto_maybe_register_decl (class data_in *data_in, tree t, unsigned ix)
a79420f9
ML
960{
961 if (TREE_CODE (t) == VAR_DECL)
962 lto_register_var_decl_in_symtab (data_in, t, ix);
963 else if (TREE_CODE (t) == FUNCTION_DECL
964 && !fndecl_built_in_p (t))
965 lto_register_function_decl_in_symtab (data_in, t, ix);
966}
967
968
969/* For the type T re-materialize it in the type variant list and
970 the pointer/reference-to chains. */
971
972static void
973lto_fixup_prevailing_type (tree t)
974{
975 /* The following re-creates proper variant lists while fixing up
976 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
977 variant list state before fixup is broken. */
978
979 /* If we are not our own variant leader link us into our new leaders
980 variant list. */
981 if (TYPE_MAIN_VARIANT (t) != t)
982 {
983 tree mv = TYPE_MAIN_VARIANT (t);
984 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
985 TYPE_NEXT_VARIANT (mv) = t;
986 }
987
988 /* The following reconstructs the pointer chains
989 of the new pointed-to type if we are a main variant. We do
990 not stream those so they are broken before fixup. */
991 if (TREE_CODE (t) == POINTER_TYPE
992 && TYPE_MAIN_VARIANT (t) == t)
993 {
994 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
995 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
996 }
997 else if (TREE_CODE (t) == REFERENCE_TYPE
998 && TYPE_MAIN_VARIANT (t) == t)
999 {
1000 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1001 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1002 }
1003}
1004
1005
1006/* We keep prevailing tree SCCs in a hashtable with manual collision
1007 handling (in case all hashes compare the same) and keep the colliding
1008 entries in the tree_scc->next chain. */
1009
1010struct tree_scc
1011{
1012 tree_scc *next;
1013 /* Hash of the whole SCC. */
1014 hashval_t hash;
1015 /* Number of trees in the SCC. */
1016 unsigned len;
1017 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1018 which share the same individual tree hash). */
1019 unsigned entry_len;
1020 /* The members of the SCC.
1021 We only need to remember the first entry node candidate for prevailing
1022 SCCs (but of course have access to all entries for SCCs we are
1023 processing).
1024 ??? For prevailing SCCs we really only need hash and the first
1025 entry candidate, but that's too awkward to implement. */
1026 tree entries[1];
1027};
1028
1029struct tree_scc_hasher : nofree_ptr_hash <tree_scc>
1030{
1031 static inline hashval_t hash (const tree_scc *);
1032 static inline bool equal (const tree_scc *, const tree_scc *);
1033};
1034
1035hashval_t
1036tree_scc_hasher::hash (const tree_scc *scc)
1037{
1038 return scc->hash;
1039}
1040
1041bool
1042tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
1043{
1044 if (scc1->hash != scc2->hash
1045 || scc1->len != scc2->len
1046 || scc1->entry_len != scc2->entry_len)
1047 return false;
1048 return true;
1049}
1050
1051static hash_table<tree_scc_hasher> *tree_scc_hash;
1052static struct obstack tree_scc_hash_obstack;
1053
1054static unsigned long num_merged_types;
1055static unsigned long num_prevailing_types;
1056static unsigned long num_type_scc_trees;
1057static unsigned long total_scc_size;
1058static unsigned long num_sccs_read;
03d90a20 1059static unsigned long num_unshared_trees_read;
a79420f9
ML
1060static unsigned long total_scc_size_merged;
1061static unsigned long num_sccs_merged;
1062static unsigned long num_scc_compares;
1063static unsigned long num_scc_compare_collisions;
1064
1065
1066/* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1067 recursing through in-SCC tree edges. Returns true if the SCCs entered
1068 through T1 and T2 are equal and fills in *MAP with the pairs of
1069 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1070
1071static bool
1072compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1073{
1074 enum tree_code code;
1075
1076 /* Mark already visited nodes. */
1077 TREE_ASM_WRITTEN (t2) = 1;
1078
1079 /* Push the pair onto map. */
1080 (*map)[0] = t1;
1081 (*map)[1] = t2;
1082 *map = *map + 2;
1083
1084 /* Compare value-fields. */
1085#define compare_values(X) \
1086 do { \
1087 if (X(t1) != X(t2)) \
1088 return false; \
1089 } while (0)
1090
1091 compare_values (TREE_CODE);
1092 code = TREE_CODE (t1);
1093
03d90a20
JH
1094 /* If we end up comparing translation unit decls we either forgot to mark
1095 some SCC as local or we compare too much. */
1096 gcc_checking_assert (code != TRANSLATION_UNIT_DECL);
1097
a79420f9
ML
1098 if (!TYPE_P (t1))
1099 {
1100 compare_values (TREE_SIDE_EFFECTS);
1101 compare_values (TREE_CONSTANT);
1102 compare_values (TREE_READONLY);
1103 compare_values (TREE_PUBLIC);
1104 }
1105 compare_values (TREE_ADDRESSABLE);
1106 compare_values (TREE_THIS_VOLATILE);
1107 if (DECL_P (t1))
1108 compare_values (DECL_UNSIGNED);
1109 else if (TYPE_P (t1))
1110 compare_values (TYPE_UNSIGNED);
1111 if (TYPE_P (t1))
1112 compare_values (TYPE_ARTIFICIAL);
1113 else
1114 compare_values (TREE_NO_WARNING);
1115 compare_values (TREE_NOTHROW);
1116 compare_values (TREE_STATIC);
1117 if (code != TREE_BINFO)
1118 compare_values (TREE_PRIVATE);
1119 compare_values (TREE_PROTECTED);
1120 compare_values (TREE_DEPRECATED);
1121 if (TYPE_P (t1))
1122 {
1123 if (AGGREGATE_TYPE_P (t1))
1124 compare_values (TYPE_REVERSE_STORAGE_ORDER);
1125 else
1126 compare_values (TYPE_SATURATING);
1127 compare_values (TYPE_ADDR_SPACE);
1128 }
1129 else if (code == SSA_NAME)
1130 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1131
1132 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1133 {
1134 if (wi::to_wide (t1) != wi::to_wide (t2))
1135 return false;
1136 }
1137
1138 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1139 {
1140 /* ??? No suitable compare routine available. */
1141 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1142 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1143 if (r1.cl != r2.cl
1144 || r1.decimal != r2.decimal
1145 || r1.sign != r2.sign
1146 || r1.signalling != r2.signalling
1147 || r1.canonical != r2.canonical
1148 || r1.uexp != r2.uexp)
1149 return false;
1150 for (unsigned i = 0; i < SIGSZ; ++i)
1151 if (r1.sig[i] != r2.sig[i])
1152 return false;
1153 }
1154
1155 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1156 if (!fixed_compare (EQ_EXPR,
1157 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1158 return false;
1159
1160 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1161 {
1162 compare_values (VECTOR_CST_LOG2_NPATTERNS);
1163 compare_values (VECTOR_CST_NELTS_PER_PATTERN);
1164 }
1165
1166 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1167 {
1168 compare_values (DECL_MODE);
1169 compare_values (DECL_NONLOCAL);
1170 compare_values (DECL_VIRTUAL_P);
1171 compare_values (DECL_IGNORED_P);
1172 compare_values (DECL_ABSTRACT_P);
1173 compare_values (DECL_ARTIFICIAL);
1174 compare_values (DECL_USER_ALIGN);
1175 compare_values (DECL_PRESERVE_P);
1176 compare_values (DECL_EXTERNAL);
eb72dc66 1177 compare_values (DECL_NOT_GIMPLE_REG_P);
a79420f9
ML
1178 compare_values (DECL_ALIGN);
1179 if (code == LABEL_DECL)
1180 {
1181 compare_values (EH_LANDING_PAD_NR);
1182 compare_values (LABEL_DECL_UID);
1183 }
1184 else if (code == FIELD_DECL)
1185 {
1186 compare_values (DECL_PACKED);
1187 compare_values (DECL_NONADDRESSABLE_P);
1188 compare_values (DECL_PADDING_P);
575ac27f 1189 compare_values (DECL_FIELD_ABI_IGNORED);
e902136b 1190 compare_values (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD);
a79420f9
ML
1191 compare_values (DECL_OFFSET_ALIGN);
1192 }
1193 else if (code == VAR_DECL)
1194 {
1195 compare_values (DECL_HAS_DEBUG_EXPR_P);
1196 compare_values (DECL_NONLOCAL_FRAME);
1197 }
1198 if (code == RESULT_DECL
1199 || code == PARM_DECL
1200 || code == VAR_DECL)
1201 {
1202 compare_values (DECL_BY_REFERENCE);
1203 if (code == VAR_DECL
1204 || code == PARM_DECL)
1205 compare_values (DECL_HAS_VALUE_EXPR_P);
1206 }
1207 }
1208
1209 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1210 compare_values (DECL_REGISTER);
1211
1212 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1213 {
1214 compare_values (DECL_COMMON);
1215 compare_values (DECL_DLLIMPORT_P);
1216 compare_values (DECL_WEAK);
1217 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1218 compare_values (DECL_COMDAT);
1219 compare_values (DECL_VISIBILITY);
1220 compare_values (DECL_VISIBILITY_SPECIFIED);
1221 if (code == VAR_DECL)
1222 {
1223 compare_values (DECL_HARD_REGISTER);
ee7a003f 1224 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
a79420f9
ML
1225 compare_values (DECL_IN_CONSTANT_POOL);
1226 }
1227 }
1228
1229 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1230 {
1231 compare_values (DECL_BUILT_IN_CLASS);
1232 compare_values (DECL_STATIC_CONSTRUCTOR);
1233 compare_values (DECL_STATIC_DESTRUCTOR);
1234 compare_values (DECL_UNINLINABLE);
1235 compare_values (DECL_POSSIBLY_INLINED);
1236 compare_values (DECL_IS_NOVOPS);
1237 compare_values (DECL_IS_RETURNS_TWICE);
1238 compare_values (DECL_IS_MALLOC);
21d7bba2 1239 compare_values (FUNCTION_DECL_DECL_TYPE);
a79420f9
ML
1240 compare_values (DECL_DECLARED_INLINE_P);
1241 compare_values (DECL_STATIC_CHAIN);
1242 compare_values (DECL_NO_INLINE_WARNING_P);
1243 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1244 compare_values (DECL_NO_LIMIT_STACK);
1245 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1246 compare_values (DECL_PURE_P);
1247 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
70df40ca 1248 compare_values (DECL_IS_REPLACEABLE_OPERATOR);
a79420f9
ML
1249 compare_values (DECL_FINAL_P);
1250 compare_values (DECL_CXX_CONSTRUCTOR_P);
1251 compare_values (DECL_CXX_DESTRUCTOR_P);
1252 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
4d732405 1253 compare_values (DECL_UNCHECKED_FUNCTION_CODE);
a79420f9
ML
1254 }
1255
1256 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1257 {
1258 compare_values (TYPE_MODE);
a79420f9
ML
1259 compare_values (TYPE_NEEDS_CONSTRUCTING);
1260 if (RECORD_OR_UNION_TYPE_P (t1))
1261 {
1262 compare_values (TYPE_TRANSPARENT_AGGR);
1263 compare_values (TYPE_FINAL_P);
f4af4019 1264 compare_values (TYPE_CXX_ODR_P);
a79420f9
ML
1265 }
1266 else if (code == ARRAY_TYPE)
1267 compare_values (TYPE_NONALIASED_COMPONENT);
f4af4019
JH
1268 if (code == ARRAY_TYPE || code == INTEGER_TYPE)
1269 compare_values (TYPE_STRING_FLAG);
a79420f9
ML
1270 if (AGGREGATE_TYPE_P (t1))
1271 compare_values (TYPE_TYPELESS_STORAGE);
1272 compare_values (TYPE_EMPTY_P);
1273 compare_values (TYPE_PACKED);
1274 compare_values (TYPE_RESTRICT);
1275 compare_values (TYPE_USER_ALIGN);
1276 compare_values (TYPE_READONLY);
1277 compare_values (TYPE_PRECISION);
1278 compare_values (TYPE_ALIGN);
1279 /* Do not compare TYPE_ALIAS_SET. Doing so introduce ordering issues
ee7a003f
ML
1280 with calls to get_alias_set which may initialize it for streamed
1281 in types. */
a79420f9
ML
1282 }
1283
1284 /* We don't want to compare locations, so there is nothing do compare
1285 for TS_EXP. */
1286
1287 /* BLOCKs are function local and we don't merge anything there, so
1288 simply refuse to merge. */
1289 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1290 return false;
1291
1292 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1293 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1294 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1295 return false;
1296
1297 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1298 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1299 return false;
1300
1301 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1302 if (!cl_optimization_option_eq (TREE_OPTIMIZATION (t1),
1303 TREE_OPTIMIZATION (t2)))
1304 return false;
1305
1306 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1307 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1308 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1309 return false;
1310
1311 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1312 compare_values (CONSTRUCTOR_NELTS);
1313
1314 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1315 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1316 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1317 IDENTIFIER_LENGTH (t1)) != 0)
1318 return false;
1319
1320 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1321 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1322 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1323 TREE_STRING_LENGTH (t1)) != 0)
1324 return false;
1325
1326 if (code == OMP_CLAUSE)
1327 {
1328 compare_values (OMP_CLAUSE_CODE);
1329 switch (OMP_CLAUSE_CODE (t1))
1330 {
1331 case OMP_CLAUSE_DEFAULT:
1332 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1333 break;
1334 case OMP_CLAUSE_SCHEDULE:
1335 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1336 break;
1337 case OMP_CLAUSE_DEPEND:
1338 compare_values (OMP_CLAUSE_DEPEND_KIND);
1339 break;
1340 case OMP_CLAUSE_MAP:
1341 compare_values (OMP_CLAUSE_MAP_KIND);
1342 break;
1343 case OMP_CLAUSE_PROC_BIND:
1344 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1345 break;
1346 case OMP_CLAUSE_REDUCTION:
1347 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1348 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1349 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1350 break;
1351 default:
1352 break;
1353 }
1354 }
1355
1356#undef compare_values
1357
1358
1359 /* Compare pointer fields. */
1360
1361 /* Recurse. Search & Replaced from DFS_write_tree_body.
1362 Folding the early checks into the compare_tree_edges recursion
1363 macro makes debugging way quicker as you are able to break on
1364 compare_tree_sccs_1 and simply finish until a call returns false
1365 to spot the SCC members with the difference. */
1366#define compare_tree_edges(E1, E2) \
1367 do { \
1368 tree t1_ = (E1), t2_ = (E2); \
1369 if (t1_ != t2_ \
1370 && (!t1_ || !t2_ \
1371 || !TREE_VISITED (t2_) \
1372 || (!TREE_ASM_WRITTEN (t2_) \
1373 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1374 return false; \
1375 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1376 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1377 } while (0)
1378
1379 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1380 {
1381 if (code != IDENTIFIER_NODE)
1382 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1383 }
1384
1385 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1386 {
1387 /* Note that the number of elements for EXPR has already been emitted
1388 in EXPR's header (see streamer_write_tree_header). */
1389 unsigned int count = vector_cst_encoded_nelts (t1);
1390 for (unsigned int i = 0; i < count; ++i)
1391 compare_tree_edges (VECTOR_CST_ENCODED_ELT (t1, i),
1392 VECTOR_CST_ENCODED_ELT (t2, i));
1393 }
1394
1395 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1396 {
1397 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1398 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1399 }
1400
1401 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1402 {
1403 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1404 /* ??? Global decls from different TUs have non-matching
1405 TRANSLATION_UNIT_DECLs. Only consider a small set of
1406 decls equivalent, we should not end up merging others. */
1407 if ((code == TYPE_DECL
1408 || code == NAMESPACE_DECL
1409 || code == IMPORTED_DECL
1410 || code == CONST_DECL
1411 || (VAR_OR_FUNCTION_DECL_P (t1)
1412 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1413 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1414 ;
1415 else
1416 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1417 }
1418
1419 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1420 {
1421 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1422 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1423 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1424 compare_tree_edges (DECL_ABSTRACT_ORIGIN (t1), DECL_ABSTRACT_ORIGIN (t2));
1425 if ((code == VAR_DECL
1426 || code == PARM_DECL)
1427 && DECL_HAS_VALUE_EXPR_P (t1))
1428 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1429 if (code == VAR_DECL
1430 && DECL_HAS_DEBUG_EXPR_P (t1))
1431 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1432 /* LTO specific edges. */
1433 if (code != FUNCTION_DECL
1434 && code != TRANSLATION_UNIT_DECL)
1435 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1436 }
1437
1438 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1439 {
1440 if (code == FUNCTION_DECL)
1441 {
1442 tree a1, a2;
1443 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1444 a1 || a2;
1445 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1446 compare_tree_edges (a1, a2);
1447 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1448 }
1449 else if (code == TYPE_DECL)
1450 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1451 }
1452
1453 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1454 {
1455 /* Make sure we don't inadvertently set the assembler name. */
1456 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1457 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1458 DECL_ASSEMBLER_NAME (t2));
1459 }
1460
1461 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1462 {
1463 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1464 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1465 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1466 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1467 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1468 DECL_FIELD_BIT_OFFSET (t2));
1469 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1470 }
1471
1472 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1473 {
1474 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1475 DECL_FUNCTION_PERSONALITY (t2));
1476 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1477 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1478 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1479 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1480 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1481 }
1482
1483 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1484 {
1485 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1486 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1487 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1488 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1489 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1490 reconstructed during fixup. */
1491 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1492 during fixup. */
1493 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1494 /* ??? Global types from different TUs have non-matching
1495 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1496 equal. */
1497 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1498 ;
1499 else
1500 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1501 /* TYPE_CANONICAL is re-computed during type merging, so do not
1502 compare it here. */
1503 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1504 }
1505
1506 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1507 {
3fb68f2e 1508 if (code == ARRAY_TYPE)
a79420f9
ML
1509 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1510 else if (RECORD_OR_UNION_TYPE_P (t1))
1511 {
1512 tree f1, f2;
1513 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1514 f1 || f2;
1515 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1516 compare_tree_edges (f1, f2);
1517 }
1518 else if (code == FUNCTION_TYPE
1519 || code == METHOD_TYPE)
1520 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1521
1522 if (!POINTER_TYPE_P (t1))
1523 compare_tree_edges (TYPE_MIN_VALUE_RAW (t1), TYPE_MIN_VALUE_RAW (t2));
1524 compare_tree_edges (TYPE_MAX_VALUE_RAW (t1), TYPE_MAX_VALUE_RAW (t2));
1525 }
1526
1527 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1528 {
1529 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1530 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1531 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1532 }
1533
1534 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1535 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1536 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1537
1538 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1539 {
1540 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1541 compare_tree_edges (TREE_OPERAND (t1, i),
1542 TREE_OPERAND (t2, i));
1543
1544 /* BLOCKs are function local and we don't merge anything there. */
1545 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1546 return false;
1547 }
1548
1549 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1550 {
1551 unsigned i;
1552 tree t;
1553 /* Lengths have already been compared above. */
1554 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1555 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1556 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1557 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1558 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1559 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1560 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1561 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1562 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1563 }
1564
1565 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1566 {
1567 unsigned i;
1568 tree index, value;
1569 /* Lengths have already been compared above. */
1570 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1571 {
1572 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1573 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1574 }
1575 }
1576
1577 if (code == OMP_CLAUSE)
1578 {
1579 int i;
1580
1581 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1582 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1583 OMP_CLAUSE_OPERAND (t2, i));
1584 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1585 }
1586
1587#undef compare_tree_edges
1588
1589 return true;
1590}
1591
1592/* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1593 out MAP if they are equal. */
1594
1595static bool
1596compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1597 tree *map)
1598{
1599 /* Assume SCC entry hashes are sorted after their cardinality. Which
1600 means we can simply take the first n-tuple of equal hashes
1601 (which is recorded as entry_len) and do n SCC entry candidate
1602 comparisons. */
1603 for (unsigned i = 0; i < pscc->entry_len; ++i)
1604 {
1605 tree *mapp = map;
1606 num_scc_compare_collisions++;
1607 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1608 {
1609 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1610 on the scc as all trees will be freed. */
1611 return true;
1612 }
1613 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
ee7a003f 1614 the SCC prevails. */
a79420f9
ML
1615 for (unsigned j = 0; j < scc->len; ++j)
1616 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1617 }
1618
1619 return false;
1620}
1621
1622/* QSort sort function to sort a map of two pointers after the 2nd
1623 pointer. */
1624
1625static int
1626cmp_tree (const void *p1_, const void *p2_)
1627{
1628 tree *p1 = (tree *)(const_cast<void *>(p1_));
1629 tree *p2 = (tree *)(const_cast<void *>(p2_));
1630 if (p1[1] == p2[1])
1631 return 0;
1632 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1633}
1634
03d90a20
JH
1635/* New scc of size 1 containing T was streamed in from DATA_IN and not merged.
1636 Register it to reader cache at index FROM. */
1637
1638static void
1639process_dref (class data_in *data_in, tree t, unsigned from)
1640{
1641 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1642 /* If we got a debug reference queued, see if the prevailing
1643 tree has a debug reference and if not, register the one
1644 for the tree we are about to throw away. */
1645 if (dref_queue.length () == 1)
1646 {
1647 dref_entry e = dref_queue.pop ();
1648 gcc_assert (e.decl
1649 == streamer_tree_cache_get_tree (cache, from));
1650 const char *sym;
1651 unsigned HOST_WIDE_INT off;
1652 if (!debug_hooks->die_ref_for_decl (t, &sym, &off))
1653 debug_hooks->register_external_die (t, e.sym, e.off);
1654 }
1655}
1656
a79420f9
ML
1657/* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1658 hash value SCC_HASH with an already recorded SCC. Return true if
1659 that was successful, otherwise return false. */
1660
1661static bool
99b1c316 1662unify_scc (class data_in *data_in, unsigned from,
a79420f9
ML
1663 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1664{
1665 bool unified_p = false;
1666 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1667 tree_scc *scc
1668 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1669 scc->next = NULL;
1670 scc->hash = scc_hash;
1671 scc->len = len;
1672 scc->entry_len = scc_entry_len;
1673 for (unsigned i = 0; i < len; ++i)
1674 {
1675 tree t = streamer_tree_cache_get_tree (cache, from + i);
1676 scc->entries[i] = t;
03d90a20
JH
1677 /* These types should be streamed as unshared. */
1678 gcc_checking_assert
1679 (!(TREE_CODE (t) == TRANSLATION_UNIT_DECL
1680 || (VAR_OR_FUNCTION_DECL_P (t)
1681 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1682 || TREE_CODE (t) == LABEL_DECL
1683 || (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAME (t))
1684 || (TYPE_P (t)
1685 && type_with_linkage_p (TYPE_MAIN_VARIANT (t))
1686 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t)))));
a79420f9
ML
1687 }
1688
1689 /* Look for the list of candidate SCCs to compare against. */
1690 tree_scc **slot;
1691 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1692 if (*slot)
1693 {
1694 /* Try unifying against each candidate. */
1695 num_scc_compares++;
1696
1697 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1698 outside of the scc when following tree edges. Make sure
1699 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1700 to track whether we visited the SCC member during the compare.
1701 We cannot use TREE_VISITED on the pscc members as the extended
1702 scc and pscc can overlap. */
1703 for (unsigned i = 0; i < scc->len; ++i)
1704 {
1705 TREE_VISITED (scc->entries[i]) = 1;
1706 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1707 }
1708
1709 tree *map = XALLOCAVEC (tree, 2 * len);
1710 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1711 {
1712 if (!compare_tree_sccs (pscc, scc, map))
1713 continue;
1714
1715 /* Found an equal SCC. */
1716 unified_p = true;
1717 num_scc_compare_collisions--;
1718 num_sccs_merged++;
1719 total_scc_size_merged += len;
1720
1721 if (flag_checking)
1722 for (unsigned i = 0; i < len; ++i)
1723 {
1724 tree t = map[2*i+1];
1725 enum tree_code code = TREE_CODE (t);
1726 /* IDENTIFIER_NODEs should be singletons and are merged by the
1727 streamer. The others should be singletons, too, and we
1728 should not merge them in any way. */
1729 gcc_assert (code != TRANSLATION_UNIT_DECL
1730 && code != IDENTIFIER_NODE);
1731 }
1732
1733 /* Fixup the streamer cache with the prevailing nodes according
1734 to the tree node mapping computed by compare_tree_sccs. */
1735 if (len == 1)
1736 {
03d90a20 1737 process_dref (data_in, pscc->entries[0], from);
a79420f9
ML
1738 lto_maybe_register_decl (data_in, pscc->entries[0], from);
1739 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1740 }
1741 else
1742 {
1743 tree *map2 = XALLOCAVEC (tree, 2 * len);
1744 for (unsigned i = 0; i < len; ++i)
1745 {
1746 map2[i*2] = (tree)(uintptr_t)(from + i);
1747 map2[i*2+1] = scc->entries[i];
1748 }
1749 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1750 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1751 for (unsigned i = 0; i < len; ++i)
1752 {
1753 lto_maybe_register_decl (data_in, map[2*i],
1754 (uintptr_t)map2[2*i]);
1755 streamer_tree_cache_replace_tree (cache, map[2*i],
1756 (uintptr_t)map2[2*i]);
1757 }
1758 }
1759
1760 /* Free the tree nodes from the read SCC. */
1761 data_in->location_cache.revert_location_cache ();
1762 for (unsigned i = 0; i < len; ++i)
1763 {
1764 if (TYPE_P (scc->entries[i]))
1765 num_merged_types++;
1766 free_node (scc->entries[i]);
1767 }
1768
1769 /* Drop DIE references.
1770 ??? Do as in the size-one SCC case which involves sorting
1771 the queue. */
1772 dref_queue.truncate (0);
1773
1774 break;
1775 }
1776
1777 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1778 if (!unified_p)
1779 for (unsigned i = 0; i < scc->len; ++i)
1780 TREE_VISITED (scc->entries[i]) = 0;
1781 }
1782
1783 /* If we didn't unify it to any candidate duplicate the relevant
1784 pieces to permanent storage and link it into the chain. */
1785 if (!unified_p)
1786 {
1787 tree_scc *pscc
1788 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1789 memcpy (pscc, scc, sizeof (tree_scc));
1790 pscc->next = (*slot);
1791 *slot = pscc;
1792 }
1793 return unified_p;
1794}
1795
03d90a20 1796typedef int_hash<unsigned, 0, UINT_MAX> code_id_hash;
a79420f9 1797
03d90a20
JH
1798/* Do registering necessary once new tree fully streamed in (including all
1799 trees it reffers to). */
1800
1801static void
1802process_new_tree (tree t, hash_map <code_id_hash, unsigned> *hm,
1803 unsigned index, unsigned *total, class data_in *data_in)
1804{
1805 /* Reconstruct the type variant and pointer-to/reference-to
1806 chains. */
1807 if (TYPE_P (t))
1808 {
1809 /* Map the tree types to their frequencies. */
1810 if (flag_lto_dump_type_stats)
1811 {
1812 unsigned key = (unsigned) TREE_CODE (t);
1813 unsigned *countp = hm->get (key);
1814 hm->put (key, countp ? (*countp) + 1 : 1);
1815 (*total)++;
1816 }
1817
1818 num_prevailing_types++;
1819 lto_fixup_prevailing_type (t);
1820
1821 /* Compute the canonical type of all non-ODR types.
1822 Delay ODR types for the end of merging process - the canonical
1823 type for those can be computed using the (unique) name however
1824 we want to do this only if units in other languages do not
1825 contain structurally equivalent type.
1826
1827 Because SCC components are streamed in random (hash) order
1828 we may have encountered the type before while registering
1829 type canonical of a derived type in the same SCC. */
1830 if (!TYPE_CANONICAL (t))
1831 {
1832 if (!RECORD_OR_UNION_TYPE_P (t)
1833 || !TYPE_CXX_ODR_P (t))
1834 gimple_register_canonical_type (t);
1835 else if (COMPLETE_TYPE_P (t))
1836 vec_safe_push (types_to_register, t);
1837 }
1838 if (TYPE_MAIN_VARIANT (t) == t && odr_type_p (t))
1839 register_odr_type (t);
1840 }
1841 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1842 type which is also member of this SCC. */
1843 if (TREE_CODE (t) == INTEGER_CST
1844 && !TREE_OVERFLOW (t))
1845 cache_integer_cst (t);
1846 if (!flag_ltrans)
1847 {
1848 lto_maybe_register_decl (data_in, t, index);
1849 /* Scan the tree for references to global functions or
1850 variables and record those for later fixup. */
1851 if (mentions_vars_p (t))
1852 vec_safe_push (tree_with_vars, t);
1853 }
1854}
a0276c00 1855
a79420f9
ML
1856/* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1857 RESOLUTIONS is the set of symbols picked by the linker (read from the
1858 resolution file when the linker plugin is being used). */
1859
1860static void
1861lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1862 vec<ld_plugin_symbol_resolution_t> resolutions)
1863{
1864 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1865 const int decl_offset = sizeof (struct lto_decl_header);
1866 const int main_offset = decl_offset + header->decl_state_size;
1867 const int string_offset = main_offset + header->main_size;
99b1c316 1868 class data_in *data_in;
a79420f9
ML
1869 unsigned int i;
1870 const uint32_t *data_ptr, *data_end;
1871 uint32_t num_decl_states;
1872
1873 lto_input_block ib_main ((const char *) data + main_offset,
1874 header->main_size, decl_data->mode_table);
1875
1876 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1877 header->string_size, resolutions);
1878
1879 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1880 internal types that should not be merged. */
1881
66d62d9f
HK
1882 hash_map <code_id_hash, unsigned> hm;
1883 unsigned total = 0;
1884
a79420f9
ML
1885 /* Read the global declarations and types. */
1886 while (ib_main.p < ib_main.len)
1887 {
1888 tree t;
1889 unsigned from = data_in->reader_cache->nodes.length ();
1890 /* Read and uniquify SCCs as in the input stream. */
1891 enum LTO_tags tag = streamer_read_record_start (&ib_main);
03d90a20 1892 if (tag == LTO_tree_scc || tag == LTO_trees)
a79420f9
ML
1893 {
1894 unsigned len_;
1895 unsigned scc_entry_len;
03d90a20
JH
1896
1897 /* Because we stream in SCC order we know that all unshared trees
1898 are now fully streamed. Process them. */
a79420f9 1899 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
03d90a20
JH
1900 &scc_entry_len,
1901 tag == LTO_tree_scc);
a79420f9
ML
1902 unsigned len = data_in->reader_cache->nodes.length () - from;
1903 gcc_assert (len == len_);
1904
03d90a20
JH
1905 if (tag == LTO_tree_scc)
1906 {
1907 total_scc_size += len;
1908 num_sccs_read++;
1909 }
1910 else
1911 num_unshared_trees_read += len;
a79420f9
ML
1912
1913 /* We have the special case of size-1 SCCs that are pre-merged
1914 by means of identifier and string sharing for example.
1915 ??? Maybe we should avoid streaming those as SCCs. */
1916 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1917 from);
03d90a20
JH
1918 /* Identifier and integers are shared specially, they should never
1919 go by the tree merging path. */
1920 gcc_checking_assert ((TREE_CODE (first) != IDENTIFIER_NODE
1921 && (TREE_CODE (first) != INTEGER_CST
1922 || TREE_OVERFLOW (first)))
1923 || len != 1);
a79420f9
ML
1924
1925 /* Try to unify the SCC with already existing ones. */
03d90a20 1926 if (!flag_ltrans && tag != LTO_trees
a79420f9
ML
1927 && unify_scc (data_in, from,
1928 len, scc_entry_len, scc_hash))
1929 continue;
1930
1931 /* Tree merging failed, mark entries in location cache as
1932 permanent. */
1933 data_in->location_cache.accept_location_cache ();
1934
1935 bool seen_type = false;
1936 for (unsigned i = 0; i < len; ++i)
1937 {
1938 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1939 from + i);
03d90a20 1940 process_new_tree (t, &hm, from + i, &total, data_in);
a79420f9 1941 if (TYPE_P (t))
03d90a20 1942 seen_type = true;
a79420f9
ML
1943 }
1944
1945 /* Register DECLs with the debuginfo machinery. */
1946 while (!dref_queue.is_empty ())
1947 {
1948 dref_entry e = dref_queue.pop ();
1949 debug_hooks->register_external_die (e.decl, e.sym, e.off);
1950 }
1951
1952 if (seen_type)
1953 num_type_scc_trees += len;
1954 }
1955 else
1956 {
a79420f9 1957 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
bcb63eb2
JH
1958 gcc_assert (data_in->reader_cache->nodes.length () == from + 1);
1959 num_unshared_trees_read++;
1960 data_in->location_cache.accept_location_cache ();
1961 process_dref (data_in, t, from);
1962 if (TREE_CODE (t) == IDENTIFIER_NODE
1963 || (TREE_CODE (t) == INTEGER_CST
1964 && !TREE_OVERFLOW (t)))
1965 ;
1966 else
03d90a20 1967 {
bcb63eb2
JH
1968 lto_maybe_register_decl (data_in, t, from);
1969 process_new_tree (t, &hm, from, &total, data_in);
03d90a20 1970 }
a79420f9
ML
1971 }
1972 }
66d62d9f
HK
1973
1974 /* Dump type statistics. */
1975 if (flag_lto_dump_type_stats)
1976 {
1977 fprintf (stdout, " Type Frequency Percentage\n\n");
1978 for (hash_map<code_id_hash, unsigned>::iterator itr = hm.begin ();
1979 itr != hm.end ();
1980 ++itr)
1981 {
1982 std::pair<unsigned, unsigned> p = *itr;
1983 enum tree_code code = (enum tree_code) p.first;
1984 fprintf (stdout, "%14s %6d %12.2f\n", get_tree_code_name (code),
1985 p.second, float (p.second)/total*100);
1986 }
1987 }
1988
a79420f9
ML
1989 data_in->location_cache.apply_location_cache ();
1990
1991 /* Read in lto_in_decl_state objects. */
ee7a003f
ML
1992 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1993 data_end
1994 = (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
a79420f9 1995 num_decl_states = *data_ptr++;
ee7a003f 1996
a79420f9
ML
1997 gcc_assert (num_decl_states > 0);
1998 decl_data->global_decl_state = lto_new_in_decl_state ();
1999 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
2000 decl_data->global_decl_state);
2001
2002 /* Read in per-function decl states and enter them in hash table. */
ee7a003f
ML
2003 decl_data->function_decl_states
2004 = hash_table<decl_state_hasher>::create_ggc (37);
a79420f9
ML
2005
2006 for (i = 1; i < num_decl_states; i++)
2007 {
2008 struct lto_in_decl_state *state = lto_new_in_decl_state ();
2009
2010 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
2011 lto_in_decl_state **slot
2012 = decl_data->function_decl_states->find_slot (state, INSERT);
2013 gcc_assert (*slot == NULL);
2014 *slot = state;
2015 }
2016
2017 if (data_ptr != data_end)
2018 internal_error ("bytecode stream: garbage at the end of symbols section");
2019
ee7a003f 2020 /* Set the current decl state to be the global state. */
a79420f9
ML
2021 decl_data->current_decl_state = decl_data->global_decl_state;
2022
2023 lto_data_in_delete (data_in);
2024}
2025
2026/* Custom version of strtoll, which is not portable. */
2027
2028static int64_t
2029lto_parse_hex (const char *p)
2030{
2031 int64_t ret = 0;
2032
2033 for (; *p != '\0'; ++p)
2034 {
2035 char c = *p;
2036 unsigned char part;
2037 ret <<= 4;
2038 if (c >= '0' && c <= '9')
ee7a003f 2039 part = c - '0';
a79420f9 2040 else if (c >= 'a' && c <= 'f')
ee7a003f 2041 part = c - 'a' + 10;
a79420f9 2042 else if (c >= 'A' && c <= 'F')
ee7a003f 2043 part = c - 'A' + 10;
a79420f9 2044 else
ee7a003f 2045 internal_error ("could not parse hex number");
a79420f9
ML
2046 ret |= part;
2047 }
2048
2049 return ret;
2050}
2051
ee7a003f
ML
2052/* Read resolution for file named FILE_NAME. The resolution is read from
2053 RESOLUTION. */
a79420f9
ML
2054
2055static void
2056lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2057{
2058 /* We require that objects in the resolution file are in the same
ee7a003f 2059 order as the lto1 command line. */
a79420f9
ML
2060 unsigned int name_len;
2061 char *obj_name;
2062 unsigned int num_symbols;
2063 unsigned int i;
2064 struct lto_file_decl_data *file_data;
ee7a003f 2065 splay_tree_node nd = NULL;
a79420f9
ML
2066
2067 if (!resolution)
2068 return;
2069
2070 name_len = strlen (file->filename);
2071 obj_name = XNEWVEC (char, name_len + 1);
ee7a003f 2072 fscanf (resolution, " "); /* Read white space. */
a79420f9
ML
2073
2074 fread (obj_name, sizeof (char), name_len, resolution);
2075 obj_name[name_len] = '\0';
2076 if (filename_cmp (obj_name, file->filename) != 0)
2077 internal_error ("unexpected file name %s in linker resolution file. "
2078 "Expected %s", obj_name, file->filename);
2079 if (file->offset != 0)
2080 {
2081 int t;
2082 char offset_p[17];
2083 int64_t offset;
2084 t = fscanf (resolution, "@0x%16s", offset_p);
2085 if (t != 1)
ee7a003f 2086 internal_error ("could not parse file offset");
a79420f9
ML
2087 offset = lto_parse_hex (offset_p);
2088 if (offset != file->offset)
ee7a003f 2089 internal_error ("unexpected offset");
a79420f9
ML
2090 }
2091
2092 free (obj_name);
2093
2094 fscanf (resolution, "%u", &num_symbols);
2095
2096 for (i = 0; i < num_symbols; i++)
2097 {
2098 int t;
2099 unsigned index;
2100 unsigned HOST_WIDE_INT id;
2101 char r_str[27];
2102 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2103 unsigned int j;
ee7a003f
ML
2104 unsigned int lto_resolution_str_len
2105 = sizeof (lto_resolution_str) / sizeof (char *);
a79420f9
ML
2106 res_pair rp;
2107
ee7a003f
ML
2108 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
2109 " %26s %*[^\n]\n", &index, &id, r_str);
a79420f9 2110 if (t != 3)
ee7a003f 2111 internal_error ("invalid line in the resolution file");
a79420f9
ML
2112
2113 for (j = 0; j < lto_resolution_str_len; j++)
2114 {
2115 if (strcmp (lto_resolution_str[j], r_str) == 0)
2116 {
2117 r = (enum ld_plugin_symbol_resolution) j;
2118 break;
2119 }
2120 }
2121 if (j == lto_resolution_str_len)
2122 internal_error ("invalid resolution in the resolution file");
2123
2124 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2125 {
2126 nd = lto_splay_tree_lookup (file_ids, id);
2127 if (nd == NULL)
2128 internal_error ("resolution sub id %wx not in object file", id);
2129 }
2130
2131 file_data = (struct lto_file_decl_data *)nd->value;
ee7a003f
ML
2132 /* The indexes are very sparse. To save memory save them in a compact
2133 format that is only unpacked later when the subfile is processed. */
a79420f9
ML
2134 rp.res = r;
2135 rp.index = index;
2136 file_data->respairs.safe_push (rp);
2137 if (file_data->max_index < index)
ee7a003f 2138 file_data->max_index = index;
a79420f9
ML
2139 }
2140}
2141
ee7a003f 2142/* List of file_decl_datas. */
a79420f9 2143struct file_data_list
ee7a003f
ML
2144{
2145 struct lto_file_decl_data *first, *last;
2146};
a79420f9
ML
2147
2148/* Is the name for a id'ed LTO section? */
2149
ee7a003f 2150static int
a79420f9
ML
2151lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2152{
2153 const char *s;
2154
2155 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
2156 return 0;
2157 s = strrchr (name, '.');
2158 if (!s)
2159 return 0;
2160 /* If the section is not suffixed with an ID return. */
2161 if ((size_t)(s - name) == strlen (section_name_prefix))
2162 return 0;
2163 return sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2164}
2165
ee7a003f 2166/* Create file_data of each sub file id. */
a79420f9 2167
ee7a003f 2168static int
a79420f9 2169create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
ee7a003f 2170 struct file_data_list *list)
a79420f9
ML
2171{
2172 struct lto_section_slot s_slot, *new_slot;
2173 unsigned HOST_WIDE_INT id;
2174 splay_tree_node nd;
2175 void **hash_slot;
2176 char *new_name;
2177 struct lto_file_decl_data *file_data;
2178
2179 if (!lto_section_with_id (ls->name, &id))
2180 return 1;
ee7a003f
ML
2181
2182 /* Find hash table of sub module id. */
a79420f9
ML
2183 nd = lto_splay_tree_lookup (file_ids, id);
2184 if (nd != NULL)
2185 {
2186 file_data = (struct lto_file_decl_data *)nd->value;
2187 }
2188 else
2189 {
2190 file_data = ggc_alloc<lto_file_decl_data> ();
2191 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2192 file_data->id = id;
2193 file_data->section_hash_table = lto_obj_create_section_hash_table ();
2194 lto_splay_tree_insert (file_ids, id, file_data);
2195
ee7a003f 2196 /* Maintain list in linker order. */
a79420f9 2197 if (!list->first)
ee7a003f 2198 list->first = file_data;
a79420f9 2199 if (list->last)
ee7a003f
ML
2200 list->last->next = file_data;
2201
a79420f9
ML
2202 list->last = file_data;
2203 }
2204
ee7a003f 2205 /* Copy section into sub module hash table. */
a79420f9
ML
2206 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2207 s_slot.name = new_name;
2208 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2209 gcc_assert (*hash_slot == NULL);
2210
2211 new_slot = XDUP (struct lto_section_slot, ls);
2212 new_slot->name = new_name;
2213 *hash_slot = new_slot;
2214 return 1;
2215}
2216
ee7a003f 2217/* Read declarations and other initializations for a FILE_DATA. */
a79420f9
ML
2218
2219static void
ebad2eee
JH
2220lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file,
2221 int order)
a79420f9
ML
2222{
2223 const char *data;
2224 size_t len;
2225 vec<ld_plugin_symbol_resolution_t>
2226 resolutions = vNULL;
2227 int i;
2228 res_pair *rp;
2229
ee7a003f
ML
2230 /* Create vector for fast access of resolution. We do this lazily
2231 to save memory. */
cb3874dc 2232 resolutions.safe_grow_cleared (file_data->max_index + 1, true);
a79420f9
ML
2233 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2234 resolutions[rp->index] = rp->res;
2235 file_data->respairs.release ();
2236
2237 file_data->renaming_hash_table = lto_create_renaming_table ();
2238 file_data->file_name = file->filename;
ebad2eee 2239 file_data->order = order;
88614dfa
ML
2240
2241 /* Read and verify LTO section. */
3c56d8d8 2242 data = lto_get_summary_section_data (file_data, LTO_section_lto, &len);
88614dfa
ML
2243 if (data == NULL)
2244 {
2245 fatal_error (input_location, "bytecode stream in file %qs generated "
2246 "with GCC compiler older than 10.0", file_data->file_name);
2247 return;
2248 }
2249
70980ea2 2250 memcpy (&file_data->lto_section_header, data, sizeof (lto_section));
88614dfa
ML
2251 lto_check_version (file_data->lto_section_header.major_version,
2252 file_data->lto_section_header.minor_version,
2253 file_data->file_name);
2254
b835645c
ML
2255#ifdef ACCEL_COMPILER
2256 lto_input_mode_table (file_data);
2257#else
2258 file_data->mode_table = lto_mode_identity_table;
2259#endif
2260
3c56d8d8 2261 data = lto_get_summary_section_data (file_data, LTO_section_decls, &len);
a79420f9
ML
2262 if (data == NULL)
2263 {
0ecf545c
MS
2264 internal_error ("cannot read %<LTO_section_decls%> from %s",
2265 file_data->file_name);
a79420f9
ML
2266 return;
2267 }
ee7a003f 2268 /* Frees resolutions. */
a79420f9
ML
2269 lto_read_decls (file_data, data, resolutions);
2270 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2271}
2272
ee7a003f 2273/* Finalize FILE_DATA in FILE and increase COUNT. */
a79420f9 2274
ee7a003f 2275static int
a79420f9 2276lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
ebad2eee 2277 int *count, int order)
a79420f9 2278{
ebad2eee 2279 lto_file_finalize (file_data, file, order);
a79420f9
ML
2280 if (symtab->dump_file)
2281 fprintf (symtab->dump_file,
2282 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2283 file_data->file_name, file_data->id);
2284 (*count)++;
2285 return 0;
2286}
2287
2288/* Generate a TREE representation for all types and external decls
ee7a003f 2289 entities in FILE.
a79420f9
ML
2290
2291 Read all of the globals out of the file. Then read the cgraph
2292 and process the .o index into the cgraph nodes so that it can open
ee7a003f 2293 the .o file to load the functions and ipa information. */
a79420f9
ML
2294
2295static struct lto_file_decl_data *
2296lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2297{
2298 struct lto_file_decl_data *file_data = NULL;
2299 splay_tree file_ids;
2300 htab_t section_hash_table;
2301 struct lto_section_slot *section;
2302 struct file_data_list file_list;
2303 struct lto_section_list section_list;
ee7a003f
ML
2304
2305 memset (&section_list, 0, sizeof (struct lto_section_list));
a79420f9
ML
2306 section_hash_table = lto_obj_build_section_table (file, &section_list);
2307
66d62d9f
HK
2308 /* Dump the details of LTO objects. */
2309 if (flag_lto_dump_objects)
2310 {
2311 int i=0;
2312 fprintf (stdout, "\n LTO Object Name: %s\n", file->filename);
2313 fprintf (stdout, "\nNo. Offset Size Section Name\n\n");
2314 for (section = section_list.first; section != NULL; section = section->next)
03de2955
RO
2315 fprintf (stdout, "%2d %8" PRId64 " %8" PRIu64 " %s\n",
2316 ++i, (int64_t) section->start, (uint64_t) section->len,
2317 section->name);
66d62d9f
HK
2318 }
2319
a79420f9 2320 /* Find all sub modules in the object and put their sections into new hash
ee7a003f 2321 tables in a splay tree. */
a79420f9
ML
2322 file_ids = lto_splay_tree_new ();
2323 memset (&file_list, 0, sizeof (struct file_data_list));
2324 for (section = section_list.first; section != NULL; section = section->next)
2325 create_subid_section_table (section, file_ids, &file_list);
2326
ee7a003f 2327 /* Add resolutions to file ids. */
a79420f9
ML
2328 lto_resolution_read (file_ids, resolution_file, file);
2329
ee7a003f 2330 /* Finalize each lto file for each submodule in the merged object. */
ebad2eee 2331 int order = 0;
ee7a003f
ML
2332 for (file_data = file_list.first; file_data != NULL;
2333 file_data = file_data->next)
ebad2eee 2334 lto_create_files_from_ids (file, file_data, count, order++);
ee7a003f 2335
a79420f9
ML
2336 splay_tree_delete (file_ids);
2337 htab_delete (section_hash_table);
2338
2339 return file_list.first;
2340}
2341
2342#if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2343#define LTO_MMAP_IO 1
2344#endif
2345
2346#if LTO_MMAP_IO
2347/* Page size of machine is used for mmap and munmap calls. */
2348static size_t page_mask;
2349#endif
2350
2351/* Get the section data of length LEN from FILENAME starting at
2352 OFFSET. The data segment must be freed by the caller when the
2353 caller is finished. Returns NULL if all was not well. */
2354
2355static char *
2356lto_read_section_data (struct lto_file_decl_data *file_data,
2357 intptr_t offset, size_t len)
2358{
2359 char *result;
2360 static int fd = -1;
2361 static char *fd_name;
2362#if LTO_MMAP_IO
2363 intptr_t computed_len;
2364 intptr_t computed_offset;
2365 intptr_t diff;
2366#endif
2367
2368 /* Keep a single-entry file-descriptor cache. The last file we
2369 touched will get closed at exit.
2370 ??? Eventually we want to add a more sophisticated larger cache
2371 or rather fix function body streaming to not stream them in
2372 practically random order. */
2373 if (fd != -1
2374 && filename_cmp (fd_name, file_data->file_name) != 0)
2375 {
2376 free (fd_name);
2377 close (fd);
2378 fd = -1;
2379 }
2380 if (fd == -1)
2381 {
2382 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2383 if (fd == -1)
ee7a003f 2384 {
a79420f9
ML
2385 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2386 return NULL;
ee7a003f 2387 }
a79420f9
ML
2388 fd_name = xstrdup (file_data->file_name);
2389 }
2390
2391#if LTO_MMAP_IO
2392 if (!page_mask)
2393 {
2394 size_t page_size = sysconf (_SC_PAGE_SIZE);
2395 page_mask = ~(page_size - 1);
2396 }
2397
2398 computed_offset = offset & page_mask;
2399 diff = offset - computed_offset;
2400 computed_len = len + diff;
2401
2402 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2403 fd, computed_offset);
2404 if (result == MAP_FAILED)
2405 {
2406 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2407 return NULL;
2408 }
2409
2410 return result + diff;
2411#else
2412 result = (char *) xmalloc (len);
2413 if (lseek (fd, offset, SEEK_SET) != offset
2414 || read (fd, result, len) != (ssize_t) len)
2415 {
2416 free (result);
2417 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2418 result = NULL;
2419 }
2420#ifdef __MINGW32__
ee7a003f
ML
2421 /* Native windows doesn't supports delayed unlink on opened file. So
2422 we close file here again. This produces higher I/O load, but at least
a79420f9
ML
2423 it prevents to have dangling file handles preventing unlink. */
2424 free (fd_name);
2425 fd_name = NULL;
2426 close (fd);
2427 fd = -1;
2428#endif
2429 return result;
2430#endif
ee7a003f 2431}
a79420f9
ML
2432
2433
2434/* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2435 NAME will be NULL unless the section type is for a function
2436 body. */
2437
2438static const char *
2439get_section_data (struct lto_file_decl_data *file_data,
3c56d8d8
ML
2440 enum lto_section_type section_type,
2441 const char *name, int order,
2442 size_t *len)
a79420f9
ML
2443{
2444 htab_t section_hash_table = file_data->section_hash_table;
2445 struct lto_section_slot *f_slot;
2446 struct lto_section_slot s_slot;
ee7a003f 2447 const char *section_name = lto_get_section_name (section_type, name,
3c56d8d8 2448 order, file_data);
a79420f9
ML
2449 char *data = NULL;
2450
2451 *len = 0;
2452 s_slot.name = section_name;
2453 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2454 if (f_slot)
2455 {
2456 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2457 *len = f_slot->len;
2458 }
2459
2460 free (CONST_CAST (char *, section_name));
2461 return data;
2462}
2463
2464
2465/* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2466 starts at OFFSET and has LEN bytes. */
2467
2468static void
2469free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2470 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2471 const char *name ATTRIBUTE_UNUSED,
2472 const char *offset, size_t len ATTRIBUTE_UNUSED)
2473{
2474#if LTO_MMAP_IO
2475 intptr_t computed_len;
2476 intptr_t computed_offset;
2477 intptr_t diff;
2478#endif
2479
2480#if LTO_MMAP_IO
2481 computed_offset = ((intptr_t) offset) & page_mask;
2482 diff = (intptr_t) offset - computed_offset;
2483 computed_len = len + diff;
2484
2485 munmap ((caddr_t) computed_offset, computed_len);
2486#else
2487 free (CONST_CAST(char *, offset));
2488#endif
2489}
2490
2491static lto_file *current_lto_file;
2492
2493/* If TT is a variable or function decl replace it with its
2494 prevailing variant. */
2495#define LTO_SET_PREVAIL(tt) \
2496 do {\
2497 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2498 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2499 { \
ee7a003f 2500 tt = lto_symtab_prevailing_decl (tt); \
a79420f9
ML
2501 fixed = true; \
2502 } \
2503 } while (0)
2504
2505/* Ensure that TT isn't a replacable var of function decl. */
2506#define LTO_NO_PREVAIL(tt) \
2507 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2508
2509/* Given a tree T replace all fields referring to variables or functions
2510 with their prevailing variant. */
2511static void
2512lto_fixup_prevailing_decls (tree t)
2513{
2514 enum tree_code code = TREE_CODE (t);
2515 bool fixed = false;
2516
2517 gcc_checking_assert (code != TREE_BINFO);
2518 LTO_NO_PREVAIL (TREE_TYPE (t));
2519 if (CODE_CONTAINS_STRUCT (code, TS_COMMON)
2520 /* lto_symtab_prevail_decl use TREE_CHAIN to link to the prevailing decl.
ee7a003f 2521 in the case T is a prevailed declaration we would ICE here. */
a79420f9
ML
2522 && !VAR_OR_FUNCTION_DECL_P (t))
2523 LTO_NO_PREVAIL (TREE_CHAIN (t));
2524 if (DECL_P (t))
2525 {
2526 LTO_NO_PREVAIL (DECL_NAME (t));
2527 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2528 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2529 {
2530 LTO_SET_PREVAIL (DECL_SIZE (t));
2531 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2532 LTO_SET_PREVAIL (DECL_INITIAL (t));
2533 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2534 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2535 }
2536 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2537 {
2538 LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t));
2539 }
2540 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2541 {
2542 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2543 }
2544 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2545 {
2546 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2547 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2548 LTO_NO_PREVAIL (DECL_VINDEX (t));
2549 }
2550 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2551 {
2552 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2553 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2554 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2555 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2556 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2557 }
2558 }
2559 else if (TYPE_P (t))
2560 {
2561 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2562 LTO_SET_PREVAIL (TYPE_SIZE (t));
2563 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2564 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2565 LTO_NO_PREVAIL (TYPE_NAME (t));
2566
2567 LTO_SET_PREVAIL (TYPE_MIN_VALUE_RAW (t));
2568 LTO_SET_PREVAIL (TYPE_MAX_VALUE_RAW (t));
2569 LTO_NO_PREVAIL (TYPE_LANG_SLOT_1 (t));
2570
2571 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2572
2573 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2574 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2575 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2576 }
2577 else if (EXPR_P (t))
2578 {
2579 int i;
2580 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2581 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2582 }
2583 else if (TREE_CODE (t) == CONSTRUCTOR)
2584 {
2585 unsigned i;
2586 tree val;
2587 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2588 LTO_SET_PREVAIL (val);
2589 }
2590 else
2591 {
2592 switch (code)
2593 {
2594 case TREE_LIST:
2595 LTO_SET_PREVAIL (TREE_VALUE (t));
2596 LTO_SET_PREVAIL (TREE_PURPOSE (t));
a79420f9
ML
2597 break;
2598 default:
2599 gcc_unreachable ();
2600 }
2601 }
2602 /* If we fixed nothing, then we missed something seen by
2603 mentions_vars_p. */
2604 gcc_checking_assert (fixed);
2605}
2606#undef LTO_SET_PREVAIL
2607#undef LTO_NO_PREVAIL
2608
ee7a003f 2609/* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
a79420f9
ML
2610 replaces var and function decls with the corresponding prevailing def. */
2611
2612static void
2613lto_fixup_state (struct lto_in_decl_state *state)
2614{
2615 unsigned i, si;
2616
2617 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2618 we still need to walk from all DECLs to find the reachable
2619 FUNCTION_DECLs and VAR_DECLs. */
2620 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2621 {
2622 vec<tree, va_gc> *trees = state->streams[si];
2623 for (i = 0; i < vec_safe_length (trees); i++)
2624 {
2625 tree t = (*trees)[i];
2626 if (flag_checking && TYPE_P (t))
2627 verify_type (t);
2628 if (VAR_OR_FUNCTION_DECL_P (t)
2629 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2630 (*trees)[i] = lto_symtab_prevailing_decl (t);
2631 }
2632 }
2633}
2634
ee7a003f 2635/* Fix the decls from all FILES. Replaces each decl with the corresponding
a79420f9
ML
2636 prevailing one. */
2637
2638static void
2639lto_fixup_decls (struct lto_file_decl_data **files)
2640{
2641 unsigned int i;
2642 tree t;
2643
2644 if (tree_with_vars)
2645 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2646 lto_fixup_prevailing_decls (t);
2647
2648 for (i = 0; files[i]; i++)
2649 {
2650 struct lto_file_decl_data *file = files[i];
2651 struct lto_in_decl_state *state = file->global_decl_state;
2652 lto_fixup_state (state);
2653
2654 hash_table<decl_state_hasher>::iterator iter;
2655 lto_in_decl_state *elt;
2656 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2657 lto_in_decl_state *, iter)
2658 lto_fixup_state (elt);
2659 }
2660}
2661
2662static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2663
2664/* Turn file datas for sub files into a single array, so that they look
ee7a003f 2665 like separate files for further passes. */
a79420f9
ML
2666
2667static void
ee7a003f
ML
2668lto_flatten_files (struct lto_file_decl_data **orig, int count,
2669 int last_file_ix)
a79420f9
ML
2670{
2671 struct lto_file_decl_data *n, *next;
2672 int i, k;
2673
2674 lto_stats.num_input_files = count;
2675 all_file_decl_data
2676 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2677 /* Set the hooks so that all of the ipa passes can read in their data. */
2678 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
ee7a003f 2679 for (i = 0, k = 0; i < last_file_ix; i++)
a79420f9
ML
2680 {
2681 for (n = orig[i]; n != NULL; n = next)
2682 {
2683 all_file_decl_data[k++] = n;
2684 next = n->next;
2685 n->next = NULL;
2686 }
2687 }
2688 all_file_decl_data[k] = NULL;
2689 gcc_assert (k == count);
2690}
2691
2692/* Input file data before flattening (i.e. splitting them to subfiles to support
2693 incremental linking. */
2694static int real_file_count;
2695static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2696
2697/* Read all the symbols from the input files FNAMES. NFILES is the
2698 number of files requested in the command line. Instantiate a
2699 global call graph by aggregating all the sub-graphs found in each
2700 file. */
2701
2702void
2703read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2704{
2705 unsigned int i, last_file_ix;
2706 FILE *resolution;
2707 int count = 0;
2708 struct lto_file_decl_data **decl_data;
2709 symtab_node *snode;
2710
2711 symtab->initialize ();
2712
2713 timevar_push (TV_IPA_LTO_DECL_IN);
2714
2715#ifdef ACCEL_COMPILER
2716 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2717 lto_stream_offload_p = true;
2718#endif
2719
2720 real_file_decl_data
2721 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2722 real_file_count = nfiles;
2723
2724 /* Read the resolution file. */
2725 resolution = NULL;
2726 if (resolution_file_name)
2727 {
2728 int t;
2729 unsigned num_objects;
2730
2731 resolution = fopen (resolution_file_name, "r");
2732 if (resolution == NULL)
2733 fatal_error (input_location,
2734 "could not open symbol resolution file: %m");
2735
2736 t = fscanf (resolution, "%u", &num_objects);
2737 gcc_assert (t == 1);
2738
2739 /* True, since the plugin splits the archives. */
2740 gcc_assert (num_objects == nfiles);
2741 }
2742 symtab->state = LTO_STREAMING;
2743
2744 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2745 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2746 gimple_canonical_type_eq, NULL);
2747 gcc_obstack_init (&tree_scc_hash_obstack);
2748 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2749
2750 /* Register the common node types with the canonical type machinery so
2751 we properly share alias-sets across languages and TUs. Do not
2752 expose the common nodes as type merge target - those that should be
2753 are already exposed so by pre-loading the LTO streamer caches.
2754 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2755 for (i = 0; i < itk_none; ++i)
2756 lto_register_canonical_types (integer_types[i], true);
2757 for (i = 0; i < stk_type_kind_last; ++i)
2758 lto_register_canonical_types (sizetype_tab[i], true);
2759 for (i = 0; i < TI_MAX; ++i)
2760 lto_register_canonical_types (global_trees[i], true);
2761 for (i = 0; i < itk_none; ++i)
2762 lto_register_canonical_types (integer_types[i], false);
2763 for (i = 0; i < stk_type_kind_last; ++i)
2764 lto_register_canonical_types (sizetype_tab[i], false);
2765 for (i = 0; i < TI_MAX; ++i)
2766 lto_register_canonical_types (global_trees[i], false);
2767
2768 if (!quiet_flag)
2769 fprintf (stderr, "Reading object files:");
2770
2771 /* Read all of the object files specified on the command line. */
2772 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2773 {
2774 struct lto_file_decl_data *file_data = NULL;
2775 if (!quiet_flag)
2776 {
2777 fprintf (stderr, " %s", fnames[i]);
2778 fflush (stderr);
2779 }
2780
2781 current_lto_file = lto_obj_file_open (fnames[i], false);
2782 if (!current_lto_file)
2783 break;
2784
2785 file_data = lto_file_read (current_lto_file, resolution, &count);
2786 if (!file_data)
2787 {
2788 lto_obj_file_close (current_lto_file);
2789 free (current_lto_file);
2790 current_lto_file = NULL;
2791 break;
2792 }
2793
2794 decl_data[last_file_ix++] = file_data;
2795
2796 lto_obj_file_close (current_lto_file);
2797 free (current_lto_file);
2798 current_lto_file = NULL;
2799 }
2800
2801 lto_flatten_files (decl_data, count, last_file_ix);
2802 lto_stats.num_input_files = count;
2803 ggc_free(decl_data);
2804 real_file_decl_data = NULL;
2805
a0276c00
JH
2806 lto_register_canonical_types_for_odr_types ();
2807
a79420f9
ML
2808 if (resolution_file_name)
2809 fclose (resolution);
2810
2811 /* Show the LTO report before launching LTRANS. */
2812 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2813 print_lto_report_1 ();
2814
2815 /* Free gimple type merging datastructures. */
2816 delete tree_scc_hash;
2817 tree_scc_hash = NULL;
2818 obstack_free (&tree_scc_hash_obstack, NULL);
2819 htab_delete (gimple_canonical_types);
2820 gimple_canonical_types = NULL;
2821 delete canonical_type_hash_cache;
2822 canonical_type_hash_cache = NULL;
2823
ee7a003f 2824 /* At this stage we know that majority of GGC memory is reachable.
a79420f9
ML
2825 Growing the limits prevents unnecesary invocation of GGC. */
2826 ggc_grow ();
c9ef0409 2827 report_heap_memory_use ();
a79420f9
ML
2828
2829 /* Set the hooks so that all of the ipa passes can read in their data. */
2830 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2831
2832 timevar_pop (TV_IPA_LTO_DECL_IN);
2833
2834 if (!quiet_flag)
c9ef0409 2835 fprintf (stderr, "\nReading the symbol table:");
a79420f9
ML
2836
2837 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2838 /* Read the symtab. */
2839 input_symtab ();
2840
2841 input_offload_tables (!flag_ltrans);
2842
2843 /* Store resolutions into the symbol table. */
2844
2845 FOR_EACH_SYMBOL (snode)
2846 if (snode->externally_visible && snode->real_symbol_p ()
2847 && snode->lto_file_data && snode->lto_file_data->resolution_map
2848 && !(TREE_CODE (snode->decl) == FUNCTION_DECL
2849 && fndecl_built_in_p (snode->decl))
2850 && !(VAR_P (snode->decl) && DECL_HARD_REGISTER (snode->decl)))
2851 {
2852 ld_plugin_symbol_resolution_t *res;
2853
2854 res = snode->lto_file_data->resolution_map->get (snode->decl);
2855 if (!res || *res == LDPR_UNKNOWN)
2856 {
2857 if (snode->output_to_lto_symbol_table_p ())
2858 fatal_error (input_location, "missing resolution data for %s",
ee7a003f 2859 IDENTIFIER_POINTER
a79420f9
ML
2860 (DECL_ASSEMBLER_NAME (snode->decl)));
2861 }
40ebe1fc
JH
2862 /* Symbol versions are always used externally, but linker does not
2863 report that correctly.
2864 This is binutils PR25924. */
2865 else if (snode->symver && *res == LDPR_PREVAILING_DEF_IRONLY)
2866 snode->resolution = LDPR_PREVAILING_DEF_IRONLY_EXP;
a79420f9 2867 else
ee7a003f 2868 snode->resolution = *res;
a79420f9
ML
2869 }
2870 for (i = 0; all_file_decl_data[i]; i++)
2871 if (all_file_decl_data[i]->resolution_map)
2872 {
ee7a003f
ML
2873 delete all_file_decl_data[i]->resolution_map;
2874 all_file_decl_data[i]->resolution_map = NULL;
a79420f9 2875 }
ee7a003f 2876
a79420f9
ML
2877 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2878
2879 if (!quiet_flag)
c9ef0409 2880 fprintf (stderr, "\nMerging declarations:");
a79420f9
ML
2881
2882 timevar_push (TV_IPA_LTO_DECL_MERGE);
2883 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2884 need to care about resolving symbols again, we only need to replace
2885 duplicated declarations read from the callgraph and from function
2886 sections. */
2887 if (!flag_ltrans)
2888 {
2889 lto_symtab_merge_decls ();
2890
2891 /* If there were errors during symbol merging bail out, we have no
2892 good way to recover here. */
2893 if (seen_error ())
2894 fatal_error (input_location,
2895 "errors during merging of translation units");
2896
2897 /* Fixup all decls. */
2898 lto_fixup_decls (all_file_decl_data);
2899 }
2900 if (tree_with_vars)
2901 ggc_free (tree_with_vars);
2902 tree_with_vars = NULL;
7cc47709
JH
2903 /* During WPA we want to prevent ggc collecting by default. Grow limits
2904 until after the IPA summaries are streamed in. Basically all IPA memory
2905 is explcitly managed by ggc_free and ggc collect is not useful.
2906 Exception are the merged declarations. */
2907 ggc_grow ();
c9ef0409 2908 report_heap_memory_use ();
a79420f9
ML
2909
2910 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2911 /* Each pass will set the appropriate timer. */
2912
2913 if (!quiet_flag)
c9ef0409 2914 fprintf (stderr, "\nReading summaries:");
a79420f9
ML
2915
2916 /* Read the IPA summary data. */
2917 if (flag_ltrans)
2918 ipa_read_optimization_summaries ();
2919 else
2920 ipa_read_summaries ();
2921
7cc47709
JH
2922 ggc_grow ();
2923
a79420f9
ML
2924 for (i = 0; all_file_decl_data[i]; i++)
2925 {
2926 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2927 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2928 all_file_decl_data[i]->symtab_node_encoder = NULL;
ee7a003f
ML
2929 lto_in_decl_state *global_decl_state
2930 = all_file_decl_data[i]->global_decl_state;
2931 lto_free_function_in_decl_state (global_decl_state);
a79420f9 2932 all_file_decl_data[i]->global_decl_state = NULL;
ee7a003f 2933 all_file_decl_data[i]->current_decl_state = NULL;
a79420f9
ML
2934 }
2935
2936 if (!flag_ltrans)
2937 {
2938 /* Finally merge the cgraph according to the decl merging decisions. */
2939 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2940
c9ef0409
JH
2941 if (!quiet_flag)
2942 fprintf (stderr, "\nMerging symbols:");
2943
a79420f9
ML
2944 gcc_assert (!dump_file);
2945 dump_file = dump_begin (lto_link_dump_id, NULL);
2946
2947 if (dump_file)
2948 {
2949 fprintf (dump_file, "Before merging:\n");
2950 symtab->dump (dump_file);
2951 }
2952 lto_symtab_merge_symbols ();
2953 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2954 we are still having duplicated comdat groups containing local statics.
2955 We could also just remove them while merging. */
2956 symtab->remove_unreachable_nodes (dump_file);
2957 ggc_collect ();
c9ef0409 2958 report_heap_memory_use ();
a79420f9
ML
2959
2960 if (dump_file)
ee7a003f 2961 dump_end (lto_link_dump_id, dump_file);
a79420f9
ML
2962 dump_file = NULL;
2963 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2964 }
2965 symtab->state = IPA_SSA;
2966 /* All node removals happening here are useless, because
2967 WPA should not stream them. Still always perform remove_unreachable_nodes
2968 because we may reshape clone tree, get rid of dead masters of inline
2969 clones and remove symbol entries for read-only variables we keep around
2970 only to be able to constant fold them. */
2971 if (flag_ltrans)
2972 {
2973 if (symtab->dump_file)
2974 symtab->dump (symtab->dump_file);
2975 symtab->remove_unreachable_nodes (symtab->dump_file);
2976 }
2977
2978 /* Indicate that the cgraph is built and ready. */
2979 symtab->function_flags_ready = true;
2980
2981 ggc_free (all_file_decl_data);
2982 all_file_decl_data = NULL;
2983}
2984
2985
2986
2987/* Show various memory usage statistics related to LTO. */
2988void
2989print_lto_report_1 (void)
2990{
2991 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
2992 fprintf (stderr, "%s statistics\n", pfx);
2993
03d90a20
JH
2994 fprintf (stderr, "[%s] read %lu unshared trees\n",
2995 pfx, num_unshared_trees_read);
2996 fprintf (stderr, "[%s] read %lu mergeable SCCs of average size %f\n",
a79420f9 2997 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
03d90a20
JH
2998 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx,
2999 total_scc_size + num_unshared_trees_read);
3000 if (flag_wpa && tree_scc_hash && num_sccs_read)
a79420f9
ML
3001 {
3002 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3003 "collision ratio: %f\n", pfx,
3004 (long) tree_scc_hash->size (),
3005 (long) tree_scc_hash->elements (),
3006 tree_scc_hash->collisions ());
3007 hash_table<tree_scc_hasher>::iterator hiter;
3008 tree_scc *scc, *max_scc = NULL;
3009 unsigned max_length = 0;
3010 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3011 {
3012 unsigned length = 0;
3013 tree_scc *s = scc;
3014 for (; s; s = s->next)
3015 length++;
3016 if (length > max_length)
3017 {
3018 max_length = length;
3019 max_scc = scc;
3020 }
3021 }
3022 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3023 pfx, max_length, max_scc->len);
3024 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3025 num_scc_compares, num_scc_compare_collisions,
3026 num_scc_compare_collisions / (double) num_scc_compares);
3027 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3028 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3029 total_scc_size_merged);
3030 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3031 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3032 pfx, num_prevailing_types, num_type_scc_trees);
3033 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3034 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3035 (long) htab_size (gimple_canonical_types),
3036 (long) htab_elements (gimple_canonical_types),
3037 (long) gimple_canonical_types->searches,
3038 (long) gimple_canonical_types->collisions,
3039 htab_collisions (gimple_canonical_types));
3040 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3041 "%lu elements, %ld searches\n", pfx,
3042 num_canonical_type_hash_entries,
3043 num_canonical_type_hash_queries);
3044 }
3045
3046 print_lto_report (pfx);
3047}
3048
3049GTY(()) tree lto_eh_personality_decl;
3050
3051/* Return the LTO personality function decl. */
3052
3053tree
3054lto_eh_personality (void)
3055{
3056 if (!lto_eh_personality_decl)
3057 {
3058 /* Use the first personality DECL for our personality if we don't
3059 support multiple ones. This ensures that we don't artificially
3060 create the need for them in a single-language program. */
3061 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3062 lto_eh_personality_decl = first_personality_decl;
3063 else
3064 lto_eh_personality_decl = lhd_gcc_personality ();
3065 }
3066
3067 return lto_eh_personality_decl;
3068}
3069
ee7a003f 3070/* Set the process name based on the LTO mode. */
a79420f9 3071
ee7a003f 3072static void
a79420f9
ML
3073lto_process_name (void)
3074{
3075 if (flag_lto)
3076 setproctitle (flag_incremental_link == INCREMENTAL_LINK_LTO
3077 ? "lto1-inclink" : "lto1-lto");
3078 if (flag_wpa)
3079 setproctitle ("lto1-wpa");
3080 if (flag_ltrans)
3081 setproctitle ("lto1-ltrans");
3082}
3083
3084
3085/* Initialize the LTO front end. */
3086
3087void
3088lto_fe_init (void)
3089{
3090 lto_process_name ();
3091 lto_streamer_hooks_init ();
3092 lto_reader_init ();
3093 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3094 memset (&lto_stats, 0, sizeof (lto_stats));
3095 bitmap_obstack_initialize (NULL);
3096 gimple_register_cfg_hooks ();
3097#ifndef ACCEL_COMPILER
3098 unsigned char *table
3099 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3100 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3101 table[m] = m;
3102 lto_mode_identity_table = table;
3103#endif
3104}
3105
3106#include "gt-lto-lto-common.h"