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