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