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