* lto.c (iterative_hash_canonical_type,
[official-gcc.git] / gcc / lto / lto.c
blob75774a1060024585db71c58bb1ad6199dcd42a4c
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "toplev.h"
26 #include "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "real.h"
37 #include "fixed-value.h"
38 #include "tree.h"
39 #include "fold-const.h"
40 #include "stor-layout.h"
41 #include "diagnostic-core.h"
42 #include "tm.h"
43 #include "predict.h"
44 #include "basic-block.h"
45 #include "hash-map.h"
46 #include "is-a.h"
47 #include "plugin-api.h"
48 #include "hard-reg-set.h"
49 #include "input.h"
50 #include "function.h"
51 #include "ipa-ref.h"
52 #include "cgraph.h"
53 #include "tree-ssa-operands.h"
54 #include "tree-pass.h"
55 #include "langhooks.h"
56 #include "bitmap.h"
57 #include "inchash.h"
58 #include "alloc-pool.h"
59 #include "symbol-summary.h"
60 #include "ipa-prop.h"
61 #include "common.h"
62 #include "debug.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-expr.h"
66 #include "gimple.h"
67 #include "lto.h"
68 #include "lto-tree.h"
69 #include "lto-streamer.h"
70 #include "lto-section-names.h"
71 #include "tree-streamer.h"
72 #include "splay-tree.h"
73 #include "lto-partition.h"
74 #include "data-streamer.h"
75 #include "context.h"
76 #include "pass_manager.h"
77 #include "ipa-inline.h"
78 #include "params.h"
79 #include "ipa-utils.h"
80 #include "gomp-constants.h"
83 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
84 static int lto_parallelism;
86 static GTY(()) tree first_personality_decl;
88 static GTY(()) const unsigned char *lto_mode_identity_table;
90 /* Returns a hash code for P. */
92 static hashval_t
93 hash_name (const void *p)
95 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
96 return (hashval_t) htab_hash_string (ds->name);
100 /* Returns nonzero if P1 and P2 are equal. */
102 static int
103 eq_name (const void *p1, const void *p2)
105 const struct lto_section_slot *s1 =
106 (const struct lto_section_slot *) p1;
107 const struct lto_section_slot *s2 =
108 (const struct lto_section_slot *) p2;
110 return strcmp (s1->name, s2->name) == 0;
113 /* Free lto_section_slot */
115 static void
116 free_with_string (void *arg)
118 struct lto_section_slot *s = (struct lto_section_slot *)arg;
120 free (CONST_CAST (char *, s->name));
121 free (arg);
124 /* Create section hash table */
126 htab_t
127 lto_obj_create_section_hash_table (void)
129 return htab_create (37, hash_name, eq_name, free_with_string);
132 /* Delete an allocated integer KEY in the splay tree. */
134 static void
135 lto_splay_tree_delete_id (splay_tree_key key)
137 free ((void *) key);
140 /* Compare splay tree node ids A and B. */
142 static int
143 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
145 unsigned HOST_WIDE_INT ai;
146 unsigned HOST_WIDE_INT bi;
148 ai = *(unsigned HOST_WIDE_INT *) a;
149 bi = *(unsigned HOST_WIDE_INT *) b;
151 if (ai < bi)
152 return -1;
153 else if (ai > bi)
154 return 1;
155 return 0;
158 /* Look up splay tree node by ID in splay tree T. */
160 static splay_tree_node
161 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
163 return splay_tree_lookup (t, (splay_tree_key) &id);
166 /* Check if KEY has ID. */
168 static bool
169 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
171 return *(unsigned HOST_WIDE_INT *) key == id;
174 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
175 The ID is allocated separately because we need HOST_WIDE_INTs which may
176 be wider than a splay_tree_key. */
178 static void
179 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
180 struct lto_file_decl_data *file_data)
182 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
183 *idp = id;
184 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
187 /* Create a splay tree. */
189 static splay_tree
190 lto_splay_tree_new (void)
192 return splay_tree_new (lto_splay_tree_compare_ids,
193 lto_splay_tree_delete_id,
194 NULL);
197 /* Return true when NODE has a clone that is analyzed (i.e. we need
198 to load its body even if the node itself is not needed). */
200 static bool
201 has_analyzed_clone_p (struct cgraph_node *node)
203 struct cgraph_node *orig = node;
204 node = node->clones;
205 if (node)
206 while (node != orig)
208 if (node->analyzed)
209 return true;
210 if (node->clones)
211 node = node->clones;
212 else if (node->next_sibling_clone)
213 node = node->next_sibling_clone;
214 else
216 while (node != orig && !node->next_sibling_clone)
217 node = node->clone_of;
218 if (node != orig)
219 node = node->next_sibling_clone;
222 return false;
225 /* Read the function body for the function associated with NODE. */
227 static void
228 lto_materialize_function (struct cgraph_node *node)
230 tree decl;
232 decl = node->decl;
233 /* Read in functions with body (analyzed nodes)
234 and also functions that are needed to produce virtual clones. */
235 if ((node->has_gimple_body_p () && node->analyzed)
236 || node->used_as_abstract_origin
237 || has_analyzed_clone_p (node))
239 /* Clones don't need to be read. */
240 if (node->clone_of)
241 return;
242 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
243 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
246 /* Let the middle end know about the function. */
247 rest_of_decl_compilation (decl, 1, 0);
251 /* Decode the content of memory pointed to by DATA in the in decl
252 state object STATE. DATA_IN points to a data_in structure for
253 decoding. Return the address after the decoded object in the
254 input. */
256 static const uint32_t *
257 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
258 struct lto_in_decl_state *state)
260 uint32_t ix;
261 tree decl;
262 uint32_t i, j;
264 ix = *data++;
265 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
266 if (!VAR_OR_FUNCTION_DECL_P (decl))
268 gcc_assert (decl == void_type_node);
269 decl = NULL_TREE;
271 state->fn_decl = decl;
273 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
275 uint32_t size = *data++;
276 vec<tree, va_gc> *decls = NULL;
277 vec_alloc (decls, size);
279 for (j = 0; j < size; j++)
280 vec_safe_push (decls,
281 streamer_tree_cache_get_tree (data_in->reader_cache,
282 data[j]));
284 state->streams[i] = decls;
285 data += size;
288 return data;
292 /* Global canonical type table. */
293 static htab_t gimple_canonical_types;
294 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
295 static unsigned long num_canonical_type_hash_entries;
296 static unsigned long num_canonical_type_hash_queries;
298 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
299 static hashval_t gimple_canonical_type_hash (const void *p);
300 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
302 /* Returning a hash value for gimple type TYPE.
304 The hash value returned is equal for types considered compatible
305 by gimple_canonical_types_compatible_p. */
307 static hashval_t
308 hash_canonical_type (tree type)
310 inchash::hash hstate;
312 /* We compute alias sets only for types that needs them.
313 Be sure we do not recurse to something else as we can not hash incomplete
314 types in a way they would have same hash value as compatible complete
315 types. */
316 gcc_checking_assert (type_with_alias_set_p (type));
318 /* Combine a few common features of types so that types are grouped into
319 smaller sets; when searching for existing matching types to merge,
320 only existing types having the same features as the new type will be
321 checked. */
322 hstate.add_int (TREE_CODE (type));
323 hstate.add_int (TYPE_MODE (type));
325 /* Incorporate common features of numerical types. */
326 if (INTEGRAL_TYPE_P (type)
327 || SCALAR_FLOAT_TYPE_P (type)
328 || FIXED_POINT_TYPE_P (type)
329 || TREE_CODE (type) == OFFSET_TYPE
330 || POINTER_TYPE_P (type))
332 hstate.add_int (TYPE_UNSIGNED (type));
333 hstate.add_int (TYPE_PRECISION (type));
336 if (VECTOR_TYPE_P (type))
338 hstate.add_int (TYPE_VECTOR_SUBPARTS (type));
339 hstate.add_int (TYPE_UNSIGNED (type));
342 if (TREE_CODE (type) == COMPLEX_TYPE)
343 hstate.add_int (TYPE_UNSIGNED (type));
345 /* For pointer and reference types, fold in information about the type
346 pointed to but do not recurse to the pointed-to type. */
347 if (POINTER_TYPE_P (type))
349 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
350 hstate.add_int (TREE_CODE (TREE_TYPE (type)));
353 /* For integer types hash only the string flag. */
354 if (TREE_CODE (type) == INTEGER_TYPE)
355 hstate.add_int (TYPE_STRING_FLAG (type));
357 /* For array types hash the domain bounds and the string flag. */
358 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
360 hstate.add_int (TYPE_STRING_FLAG (type));
361 /* OMP lowering can introduce error_mark_node in place of
362 random local decls in types. */
363 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
364 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
365 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
366 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
369 /* Recurse for aggregates with a single element type. */
370 if (TREE_CODE (type) == ARRAY_TYPE
371 || TREE_CODE (type) == COMPLEX_TYPE
372 || TREE_CODE (type) == VECTOR_TYPE)
373 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
375 /* Incorporate function return and argument types. */
376 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
378 unsigned na;
379 tree p;
381 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
383 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
385 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
386 na++;
389 hstate.add_int (na);
392 if (RECORD_OR_UNION_TYPE_P (type))
394 unsigned nf;
395 tree f;
397 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
398 if (TREE_CODE (f) == FIELD_DECL)
400 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
401 nf++;
404 hstate.add_int (nf);
407 return hstate.end();
410 /* Returning a hash value for gimple type TYPE combined with VAL. */
412 static void
413 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
415 hashval_t v;
417 /* All type variants have same TYPE_CANONICAL. */
418 type = TYPE_MAIN_VARIANT (type);
419 /* An already processed type. */
420 if (TYPE_CANONICAL (type))
422 type = TYPE_CANONICAL (type);
423 v = gimple_canonical_type_hash (type);
425 else
427 /* Canonical types should not be able to form SCCs by design, this
428 recursion is just because we do not register canonical types in
429 optimal order. To avoid quadratic behavior also register the
430 type here. */
431 v = hash_canonical_type (type);
432 gimple_register_canonical_type_1 (type, v);
434 hstate.add_int (v);
437 /* Returns the hash for a canonical type P. */
439 static hashval_t
440 gimple_canonical_type_hash (const void *p)
442 num_canonical_type_hash_queries++;
443 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
444 gcc_assert (slot != NULL);
445 return *slot;
450 /* Returns nonzero if P1 and P2 are equal. */
452 static int
453 gimple_canonical_type_eq (const void *p1, const void *p2)
455 const_tree t1 = (const_tree) p1;
456 const_tree t2 = (const_tree) p2;
457 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
458 CONST_CAST_TREE (t2));
461 /* Main worker for gimple_register_canonical_type. */
463 static void
464 gimple_register_canonical_type_1 (tree t, hashval_t hash)
466 void **slot;
468 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
470 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
471 if (*slot)
473 tree new_type = (tree)(*slot);
474 gcc_checking_assert (new_type != t);
475 TYPE_CANONICAL (t) = new_type;
477 else
479 TYPE_CANONICAL (t) = t;
480 *slot = (void *) t;
481 /* Cache the just computed hash value. */
482 num_canonical_type_hash_entries++;
483 bool existed_p = canonical_type_hash_cache->put (t, hash);
484 gcc_assert (!existed_p);
488 /* Register type T in the global type table gimple_types and set
489 TYPE_CANONICAL of T accordingly.
490 This is used by LTO to merge structurally equivalent types for
491 type-based aliasing purposes across different TUs and languages.
493 ??? This merging does not exactly match how the tree.c middle-end
494 functions will assign TYPE_CANONICAL when new types are created
495 during optimization (which at least happens for pointer and array
496 types). */
498 static void
499 gimple_register_canonical_type (tree t)
501 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t))
502 return;
504 /* Canonical types are same among all complete variants. */
505 if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
506 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
507 else
509 gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t),
510 hash_canonical_type (TYPE_MAIN_VARIANT (t)));
511 TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
515 /* Re-compute TYPE_CANONICAL for NODE and related types. */
517 static void
518 lto_register_canonical_types (tree node, bool first_p)
520 if (!node
521 || !TYPE_P (node))
522 return;
524 if (first_p)
525 TYPE_CANONICAL (node) = NULL_TREE;
527 if (POINTER_TYPE_P (node)
528 || TREE_CODE (node) == COMPLEX_TYPE
529 || TREE_CODE (node) == ARRAY_TYPE)
530 lto_register_canonical_types (TREE_TYPE (node), first_p);
532 if (!first_p)
533 gimple_register_canonical_type (node);
537 /* Remember trees that contains references to declarations. */
538 static GTY(()) vec <tree, va_gc> *tree_with_vars;
540 #define CHECK_VAR(tt) \
541 do \
543 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
544 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
545 return true; \
546 } while (0)
548 #define CHECK_NO_VAR(tt) \
549 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
551 /* Check presence of pointers to decls in fields of a tree_typed T. */
553 static inline bool
554 mentions_vars_p_typed (tree t)
556 CHECK_NO_VAR (TREE_TYPE (t));
557 return false;
560 /* Check presence of pointers to decls in fields of a tree_common T. */
562 static inline bool
563 mentions_vars_p_common (tree t)
565 if (mentions_vars_p_typed (t))
566 return true;
567 CHECK_NO_VAR (TREE_CHAIN (t));
568 return false;
571 /* Check presence of pointers to decls in fields of a decl_minimal T. */
573 static inline bool
574 mentions_vars_p_decl_minimal (tree t)
576 if (mentions_vars_p_common (t))
577 return true;
578 CHECK_NO_VAR (DECL_NAME (t));
579 CHECK_VAR (DECL_CONTEXT (t));
580 return false;
583 /* Check presence of pointers to decls in fields of a decl_common T. */
585 static inline bool
586 mentions_vars_p_decl_common (tree t)
588 if (mentions_vars_p_decl_minimal (t))
589 return true;
590 CHECK_VAR (DECL_SIZE (t));
591 CHECK_VAR (DECL_SIZE_UNIT (t));
592 CHECK_VAR (DECL_INITIAL (t));
593 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
594 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
595 return false;
598 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
600 static inline bool
601 mentions_vars_p_decl_with_vis (tree t)
603 if (mentions_vars_p_decl_common (t))
604 return true;
606 /* Accessor macro has side-effects, use field-name here. */
607 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
608 return false;
611 /* Check presence of pointers to decls in fields of a decl_non_common T. */
613 static inline bool
614 mentions_vars_p_decl_non_common (tree t)
616 if (mentions_vars_p_decl_with_vis (t))
617 return true;
618 CHECK_NO_VAR (DECL_RESULT_FLD (t));
619 return false;
622 /* Check presence of pointers to decls in fields of a decl_non_common T. */
624 static bool
625 mentions_vars_p_function (tree t)
627 if (mentions_vars_p_decl_non_common (t))
628 return true;
629 CHECK_NO_VAR (DECL_ARGUMENTS (t));
630 CHECK_NO_VAR (DECL_VINDEX (t));
631 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
632 return false;
635 /* Check presence of pointers to decls in fields of a field_decl T. */
637 static bool
638 mentions_vars_p_field_decl (tree t)
640 if (mentions_vars_p_decl_common (t))
641 return true;
642 CHECK_VAR (DECL_FIELD_OFFSET (t));
643 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
644 CHECK_NO_VAR (DECL_QUALIFIER (t));
645 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
646 CHECK_NO_VAR (DECL_FCONTEXT (t));
647 return false;
650 /* Check presence of pointers to decls in fields of a type T. */
652 static bool
653 mentions_vars_p_type (tree t)
655 if (mentions_vars_p_common (t))
656 return true;
657 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
658 CHECK_VAR (TYPE_SIZE (t));
659 CHECK_VAR (TYPE_SIZE_UNIT (t));
660 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
661 CHECK_NO_VAR (TYPE_NAME (t));
663 CHECK_VAR (TYPE_MINVAL (t));
664 CHECK_VAR (TYPE_MAXVAL (t));
666 /* Accessor is for derived node types only. */
667 CHECK_NO_VAR (t->type_non_common.binfo);
669 CHECK_VAR (TYPE_CONTEXT (t));
670 CHECK_NO_VAR (TYPE_CANONICAL (t));
671 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
672 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
673 return false;
676 /* Check presence of pointers to decls in fields of a BINFO T. */
678 static bool
679 mentions_vars_p_binfo (tree t)
681 unsigned HOST_WIDE_INT i, n;
683 if (mentions_vars_p_common (t))
684 return true;
685 CHECK_VAR (BINFO_VTABLE (t));
686 CHECK_NO_VAR (BINFO_OFFSET (t));
687 CHECK_NO_VAR (BINFO_VIRTUALS (t));
688 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
689 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
690 for (i = 0; i < n; i++)
691 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
692 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
693 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
694 n = BINFO_N_BASE_BINFOS (t);
695 for (i = 0; i < n; i++)
696 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
697 return false;
700 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
702 static bool
703 mentions_vars_p_constructor (tree t)
705 unsigned HOST_WIDE_INT idx;
706 constructor_elt *ce;
708 if (mentions_vars_p_typed (t))
709 return true;
711 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
713 CHECK_NO_VAR (ce->index);
714 CHECK_VAR (ce->value);
716 return false;
719 /* Check presence of pointers to decls in fields of an expression tree T. */
721 static bool
722 mentions_vars_p_expr (tree t)
724 int i;
725 if (mentions_vars_p_typed (t))
726 return true;
727 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
728 CHECK_VAR (TREE_OPERAND (t, i));
729 return false;
732 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
734 static bool
735 mentions_vars_p_omp_clause (tree t)
737 int i;
738 if (mentions_vars_p_common (t))
739 return true;
740 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
741 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
742 return false;
745 /* Check presence of pointers to decls that needs later fixup in T. */
747 static bool
748 mentions_vars_p (tree t)
750 switch (TREE_CODE (t))
752 case IDENTIFIER_NODE:
753 break;
755 case TREE_LIST:
756 CHECK_VAR (TREE_VALUE (t));
757 CHECK_VAR (TREE_PURPOSE (t));
758 CHECK_NO_VAR (TREE_CHAIN (t));
759 break;
761 case FIELD_DECL:
762 return mentions_vars_p_field_decl (t);
764 case LABEL_DECL:
765 case CONST_DECL:
766 case PARM_DECL:
767 case RESULT_DECL:
768 case IMPORTED_DECL:
769 case NAMESPACE_DECL:
770 case NAMELIST_DECL:
771 return mentions_vars_p_decl_common (t);
773 case VAR_DECL:
774 return mentions_vars_p_decl_with_vis (t);
776 case TYPE_DECL:
777 return mentions_vars_p_decl_non_common (t);
779 case FUNCTION_DECL:
780 return mentions_vars_p_function (t);
782 case TREE_BINFO:
783 return mentions_vars_p_binfo (t);
785 case PLACEHOLDER_EXPR:
786 return mentions_vars_p_common (t);
788 case BLOCK:
789 case TRANSLATION_UNIT_DECL:
790 case OPTIMIZATION_NODE:
791 case TARGET_OPTION_NODE:
792 break;
794 case CONSTRUCTOR:
795 return mentions_vars_p_constructor (t);
797 case OMP_CLAUSE:
798 return mentions_vars_p_omp_clause (t);
800 default:
801 if (TYPE_P (t))
803 if (mentions_vars_p_type (t))
804 return true;
806 else if (EXPR_P (t))
808 if (mentions_vars_p_expr (t))
809 return true;
811 else if (CONSTANT_CLASS_P (t))
812 CHECK_NO_VAR (TREE_TYPE (t));
813 else
814 gcc_unreachable ();
816 return false;
820 /* Return the resolution for the decl with index INDEX from DATA_IN. */
822 static enum ld_plugin_symbol_resolution
823 get_resolution (struct data_in *data_in, unsigned index)
825 if (data_in->globals_resolution.exists ())
827 ld_plugin_symbol_resolution_t ret;
828 /* We can have references to not emitted functions in
829 DECL_FUNCTION_PERSONALITY at least. So we can and have
830 to indeed return LDPR_UNKNOWN in some cases. */
831 if (data_in->globals_resolution.length () <= index)
832 return LDPR_UNKNOWN;
833 ret = data_in->globals_resolution[index];
834 return ret;
836 else
837 /* Delay resolution finding until decl merging. */
838 return LDPR_UNKNOWN;
841 /* We need to record resolutions until symbol table is read. */
842 static void
843 register_resolution (struct lto_file_decl_data *file_data, tree decl,
844 enum ld_plugin_symbol_resolution resolution)
846 if (resolution == LDPR_UNKNOWN)
847 return;
848 if (!file_data->resolution_map)
849 file_data->resolution_map
850 = new hash_map<tree, ld_plugin_symbol_resolution>;
851 file_data->resolution_map->put (decl, resolution);
854 /* Register DECL with the global symbol table and change its
855 name if necessary to avoid name clashes for static globals across
856 different files. */
858 static void
859 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
860 unsigned ix)
862 tree context;
864 /* Variable has file scope, not local. */
865 if (!TREE_PUBLIC (decl)
866 && !((context = decl_function_context (decl))
867 && auto_var_in_fn_p (decl, context)))
868 rest_of_decl_compilation (decl, 1, 0);
870 /* If this variable has already been declared, queue the
871 declaration for merging. */
872 if (TREE_PUBLIC (decl))
873 register_resolution (data_in->file_data,
874 decl, get_resolution (data_in, ix));
878 /* Register DECL with the global symbol table and change its
879 name if necessary to avoid name clashes for static globals across
880 different files. DATA_IN contains descriptors and tables for the
881 file being read. */
883 static void
884 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
885 unsigned ix)
887 /* If this variable has already been declared, queue the
888 declaration for merging. */
889 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
890 register_resolution (data_in->file_data,
891 decl, get_resolution (data_in, ix));
895 /* For the type T re-materialize it in the type variant list and
896 the pointer/reference-to chains. */
898 static void
899 lto_fixup_prevailing_type (tree t)
901 /* The following re-creates proper variant lists while fixing up
902 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
903 variant list state before fixup is broken. */
905 /* If we are not our own variant leader link us into our new leaders
906 variant list. */
907 if (TYPE_MAIN_VARIANT (t) != t)
909 tree mv = TYPE_MAIN_VARIANT (t);
910 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
911 TYPE_NEXT_VARIANT (mv) = t;
914 /* The following reconstructs the pointer chains
915 of the new pointed-to type if we are a main variant. We do
916 not stream those so they are broken before fixup. */
917 if (TREE_CODE (t) == POINTER_TYPE
918 && TYPE_MAIN_VARIANT (t) == t)
920 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
921 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
923 else if (TREE_CODE (t) == REFERENCE_TYPE
924 && TYPE_MAIN_VARIANT (t) == t)
926 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
927 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
932 /* We keep prevailing tree SCCs in a hashtable with manual collision
933 handling (in case all hashes compare the same) and keep the colliding
934 entries in the tree_scc->next chain. */
936 struct tree_scc
938 tree_scc *next;
939 /* Hash of the whole SCC. */
940 hashval_t hash;
941 /* Number of trees in the SCC. */
942 unsigned len;
943 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
944 which share the same individual tree hash). */
945 unsigned entry_len;
946 /* The members of the SCC.
947 We only need to remember the first entry node candidate for prevailing
948 SCCs (but of course have access to all entries for SCCs we are
949 processing).
950 ??? For prevailing SCCs we really only need hash and the first
951 entry candidate, but that's too awkward to implement. */
952 tree entries[1];
955 struct tree_scc_hasher : typed_noop_remove <tree_scc>
957 typedef tree_scc *value_type;
958 typedef tree_scc *compare_type;
959 static inline hashval_t hash (const tree_scc *);
960 static inline bool equal (const tree_scc *, const tree_scc *);
963 hashval_t
964 tree_scc_hasher::hash (const tree_scc *scc)
966 return scc->hash;
969 bool
970 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
972 if (scc1->hash != scc2->hash
973 || scc1->len != scc2->len
974 || scc1->entry_len != scc2->entry_len)
975 return false;
976 return true;
979 static hash_table<tree_scc_hasher> *tree_scc_hash;
980 static struct obstack tree_scc_hash_obstack;
982 static unsigned long num_merged_types;
983 static unsigned long num_prevailing_types;
984 static unsigned long num_type_scc_trees;
985 static unsigned long total_scc_size;
986 static unsigned long num_sccs_read;
987 static unsigned long total_scc_size_merged;
988 static unsigned long num_sccs_merged;
989 static unsigned long num_scc_compares;
990 static unsigned long num_scc_compare_collisions;
993 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
994 recursing through in-SCC tree edges. Returns true if the SCCs entered
995 through T1 and T2 are equal and fills in *MAP with the pairs of
996 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
998 static bool
999 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1001 enum tree_code code;
1003 /* Mark already visited nodes. */
1004 TREE_ASM_WRITTEN (t2) = 1;
1006 /* Push the pair onto map. */
1007 (*map)[0] = t1;
1008 (*map)[1] = t2;
1009 *map = *map + 2;
1011 /* Compare value-fields. */
1012 #define compare_values(X) \
1013 do { \
1014 if (X(t1) != X(t2)) \
1015 return false; \
1016 } while (0)
1018 compare_values (TREE_CODE);
1019 code = TREE_CODE (t1);
1021 if (!TYPE_P (t1))
1023 compare_values (TREE_SIDE_EFFECTS);
1024 compare_values (TREE_CONSTANT);
1025 compare_values (TREE_READONLY);
1026 compare_values (TREE_PUBLIC);
1028 compare_values (TREE_ADDRESSABLE);
1029 compare_values (TREE_THIS_VOLATILE);
1030 if (DECL_P (t1))
1031 compare_values (DECL_UNSIGNED);
1032 else if (TYPE_P (t1))
1033 compare_values (TYPE_UNSIGNED);
1034 if (TYPE_P (t1))
1035 compare_values (TYPE_ARTIFICIAL);
1036 else
1037 compare_values (TREE_NO_WARNING);
1038 compare_values (TREE_NOTHROW);
1039 compare_values (TREE_STATIC);
1040 if (code != TREE_BINFO)
1041 compare_values (TREE_PRIVATE);
1042 compare_values (TREE_PROTECTED);
1043 compare_values (TREE_DEPRECATED);
1044 if (TYPE_P (t1))
1046 compare_values (TYPE_SATURATING);
1047 compare_values (TYPE_ADDR_SPACE);
1049 else if (code == SSA_NAME)
1050 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1052 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1054 if (!wi::eq_p (t1, t2))
1055 return false;
1058 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1060 /* ??? No suitable compare routine available. */
1061 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1062 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1063 if (r1.cl != r2.cl
1064 || r1.decimal != r2.decimal
1065 || r1.sign != r2.sign
1066 || r1.signalling != r2.signalling
1067 || r1.canonical != r2.canonical
1068 || r1.uexp != r2.uexp)
1069 return false;
1070 for (unsigned i = 0; i < SIGSZ; ++i)
1071 if (r1.sig[i] != r2.sig[i])
1072 return false;
1075 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1076 if (!fixed_compare (EQ_EXPR,
1077 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1078 return false;
1081 /* We don't want to compare locations, so there is nothing do compare
1082 for TS_DECL_MINIMAL. */
1084 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1086 compare_values (DECL_MODE);
1087 compare_values (DECL_NONLOCAL);
1088 compare_values (DECL_VIRTUAL_P);
1089 compare_values (DECL_IGNORED_P);
1090 compare_values (DECL_ABSTRACT_P);
1091 compare_values (DECL_ARTIFICIAL);
1092 compare_values (DECL_USER_ALIGN);
1093 compare_values (DECL_PRESERVE_P);
1094 compare_values (DECL_EXTERNAL);
1095 compare_values (DECL_GIMPLE_REG_P);
1096 compare_values (DECL_ALIGN);
1097 if (code == LABEL_DECL)
1099 compare_values (EH_LANDING_PAD_NR);
1100 compare_values (LABEL_DECL_UID);
1102 else if (code == FIELD_DECL)
1104 compare_values (DECL_PACKED);
1105 compare_values (DECL_NONADDRESSABLE_P);
1106 compare_values (DECL_OFFSET_ALIGN);
1108 else if (code == VAR_DECL)
1110 compare_values (DECL_HAS_DEBUG_EXPR_P);
1111 compare_values (DECL_NONLOCAL_FRAME);
1113 if (code == RESULT_DECL
1114 || code == PARM_DECL
1115 || code == VAR_DECL)
1117 compare_values (DECL_BY_REFERENCE);
1118 if (code == VAR_DECL
1119 || code == PARM_DECL)
1120 compare_values (DECL_HAS_VALUE_EXPR_P);
1124 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1125 compare_values (DECL_REGISTER);
1127 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1129 compare_values (DECL_COMMON);
1130 compare_values (DECL_DLLIMPORT_P);
1131 compare_values (DECL_WEAK);
1132 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1133 compare_values (DECL_COMDAT);
1134 compare_values (DECL_VISIBILITY);
1135 compare_values (DECL_VISIBILITY_SPECIFIED);
1136 if (code == VAR_DECL)
1138 compare_values (DECL_HARD_REGISTER);
1139 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1140 compare_values (DECL_IN_CONSTANT_POOL);
1144 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1146 compare_values (DECL_BUILT_IN_CLASS);
1147 compare_values (DECL_STATIC_CONSTRUCTOR);
1148 compare_values (DECL_STATIC_DESTRUCTOR);
1149 compare_values (DECL_UNINLINABLE);
1150 compare_values (DECL_POSSIBLY_INLINED);
1151 compare_values (DECL_IS_NOVOPS);
1152 compare_values (DECL_IS_RETURNS_TWICE);
1153 compare_values (DECL_IS_MALLOC);
1154 compare_values (DECL_IS_OPERATOR_NEW);
1155 compare_values (DECL_DECLARED_INLINE_P);
1156 compare_values (DECL_STATIC_CHAIN);
1157 compare_values (DECL_NO_INLINE_WARNING_P);
1158 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1159 compare_values (DECL_NO_LIMIT_STACK);
1160 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1161 compare_values (DECL_PURE_P);
1162 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1163 compare_values (DECL_FINAL_P);
1164 compare_values (DECL_CXX_CONSTRUCTOR_P);
1165 compare_values (DECL_CXX_DESTRUCTOR_P);
1166 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1167 compare_values (DECL_FUNCTION_CODE);
1170 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1172 compare_values (TYPE_MODE);
1173 compare_values (TYPE_STRING_FLAG);
1174 compare_values (TYPE_NO_FORCE_BLK);
1175 compare_values (TYPE_NEEDS_CONSTRUCTING);
1176 if (RECORD_OR_UNION_TYPE_P (t1))
1178 compare_values (TYPE_TRANSPARENT_AGGR);
1179 compare_values (TYPE_FINAL_P);
1181 else if (code == ARRAY_TYPE)
1182 compare_values (TYPE_NONALIASED_COMPONENT);
1183 compare_values (TYPE_PACKED);
1184 compare_values (TYPE_RESTRICT);
1185 compare_values (TYPE_USER_ALIGN);
1186 compare_values (TYPE_READONLY);
1187 compare_values (TYPE_PRECISION);
1188 compare_values (TYPE_ALIGN);
1189 compare_values (TYPE_ALIAS_SET);
1192 /* We don't want to compare locations, so there is nothing do compare
1193 for TS_EXP. */
1195 /* BLOCKs are function local and we don't merge anything there, so
1196 simply refuse to merge. */
1197 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1198 return false;
1200 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1201 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1202 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1203 return false;
1205 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1206 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1207 return false;
1209 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1210 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1211 sizeof (struct cl_optimization)) != 0)
1212 return false;
1214 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1215 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1216 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1217 return false;
1219 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1220 compare_values (CONSTRUCTOR_NELTS);
1222 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1223 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1224 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1225 IDENTIFIER_LENGTH (t1)) != 0)
1226 return false;
1228 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1229 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1230 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1231 TREE_STRING_LENGTH (t1)) != 0)
1232 return false;
1234 if (code == OMP_CLAUSE)
1236 compare_values (OMP_CLAUSE_CODE);
1237 switch (OMP_CLAUSE_CODE (t1))
1239 case OMP_CLAUSE_DEFAULT:
1240 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1241 break;
1242 case OMP_CLAUSE_SCHEDULE:
1243 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1244 break;
1245 case OMP_CLAUSE_DEPEND:
1246 compare_values (OMP_CLAUSE_DEPEND_KIND);
1247 break;
1248 case OMP_CLAUSE_MAP:
1249 compare_values (OMP_CLAUSE_MAP_KIND);
1250 break;
1251 case OMP_CLAUSE_PROC_BIND:
1252 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1253 break;
1254 case OMP_CLAUSE_REDUCTION:
1255 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1256 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1257 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1258 break;
1259 default:
1260 break;
1264 #undef compare_values
1267 /* Compare pointer fields. */
1269 /* Recurse. Search & Replaced from DFS_write_tree_body.
1270 Folding the early checks into the compare_tree_edges recursion
1271 macro makes debugging way quicker as you are able to break on
1272 compare_tree_sccs_1 and simply finish until a call returns false
1273 to spot the SCC members with the difference. */
1274 #define compare_tree_edges(E1, E2) \
1275 do { \
1276 tree t1_ = (E1), t2_ = (E2); \
1277 if (t1_ != t2_ \
1278 && (!t1_ || !t2_ \
1279 || !TREE_VISITED (t2_) \
1280 || (!TREE_ASM_WRITTEN (t2_) \
1281 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1282 return false; \
1283 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1284 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1285 } while (0)
1287 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1289 if (code != IDENTIFIER_NODE)
1290 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1293 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1295 unsigned i;
1296 /* Note that the number of elements for EXPR has already been emitted
1297 in EXPR's header (see streamer_write_tree_header). */
1298 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1299 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1302 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1304 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1305 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1308 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1310 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1311 /* ??? Global decls from different TUs have non-matching
1312 TRANSLATION_UNIT_DECLs. Only consider a small set of
1313 decls equivalent, we should not end up merging others. */
1314 if ((code == TYPE_DECL
1315 || code == NAMESPACE_DECL
1316 || code == IMPORTED_DECL
1317 || code == CONST_DECL
1318 || (VAR_OR_FUNCTION_DECL_P (t1)
1319 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1320 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1322 else
1323 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1326 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1328 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1329 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1330 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1331 if ((code == VAR_DECL
1332 || code == PARM_DECL)
1333 && DECL_HAS_VALUE_EXPR_P (t1))
1334 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1335 if (code == VAR_DECL
1336 && DECL_HAS_DEBUG_EXPR_P (t1))
1337 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1338 /* LTO specific edges. */
1339 if (code != FUNCTION_DECL
1340 && code != TRANSLATION_UNIT_DECL)
1341 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1344 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1346 if (code == FUNCTION_DECL)
1348 tree a1, a2;
1349 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1350 a1 || a2;
1351 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1352 compare_tree_edges (a1, a2);
1353 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1355 else if (code == TYPE_DECL)
1356 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1359 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1361 /* Make sure we don't inadvertently set the assembler name. */
1362 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1363 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1364 DECL_ASSEMBLER_NAME (t2));
1367 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1369 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1370 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1371 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1372 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1373 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1374 DECL_FIELD_BIT_OFFSET (t2));
1375 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1378 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1380 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1381 DECL_FUNCTION_PERSONALITY (t2));
1382 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1383 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1384 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1385 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1386 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1389 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1391 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1392 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1393 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1394 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1395 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1396 reconstructed during fixup. */
1397 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1398 during fixup. */
1399 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1400 /* ??? Global types from different TUs have non-matching
1401 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1402 equal. */
1403 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1405 else
1406 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1407 /* TYPE_CANONICAL is re-computed during type merging, so do not
1408 compare it here. */
1409 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1412 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1414 if (code == ENUMERAL_TYPE)
1415 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1416 else if (code == ARRAY_TYPE)
1417 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1418 else if (RECORD_OR_UNION_TYPE_P (t1))
1420 tree f1, f2;
1421 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1422 f1 || f2;
1423 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1424 compare_tree_edges (f1, f2);
1425 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1427 else if (code == FUNCTION_TYPE
1428 || code == METHOD_TYPE)
1429 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1430 if (!POINTER_TYPE_P (t1))
1431 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1432 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1435 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1437 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1438 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1439 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1442 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1443 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1444 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1446 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1448 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1449 compare_tree_edges (TREE_OPERAND (t1, i),
1450 TREE_OPERAND (t2, i));
1452 /* BLOCKs are function local and we don't merge anything there. */
1453 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1454 return false;
1457 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1459 unsigned i;
1460 tree t;
1461 /* Lengths have already been compared above. */
1462 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1463 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1464 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1465 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1466 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1467 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1468 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1469 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1470 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1473 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1475 unsigned i;
1476 tree index, value;
1477 /* Lengths have already been compared above. */
1478 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1480 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1481 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1485 if (code == OMP_CLAUSE)
1487 int i;
1489 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1490 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1491 OMP_CLAUSE_OPERAND (t2, i));
1492 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1495 #undef compare_tree_edges
1497 return true;
1500 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1501 out MAP if they are equal. */
1503 static bool
1504 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1505 tree *map)
1507 /* Assume SCC entry hashes are sorted after their cardinality. Which
1508 means we can simply take the first n-tuple of equal hashes
1509 (which is recorded as entry_len) and do n SCC entry candidate
1510 comparisons. */
1511 for (unsigned i = 0; i < pscc->entry_len; ++i)
1513 tree *mapp = map;
1514 num_scc_compare_collisions++;
1515 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1517 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1518 on the scc as all trees will be freed. */
1519 return true;
1521 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1522 the SCC prevails. */
1523 for (unsigned j = 0; j < scc->len; ++j)
1524 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1527 return false;
1530 /* QSort sort function to sort a map of two pointers after the 2nd
1531 pointer. */
1533 static int
1534 cmp_tree (const void *p1_, const void *p2_)
1536 tree *p1 = (tree *)(const_cast<void *>(p1_));
1537 tree *p2 = (tree *)(const_cast<void *>(p2_));
1538 if (p1[1] == p2[1])
1539 return 0;
1540 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1543 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1544 hash value SCC_HASH with an already recorded SCC. Return true if
1545 that was successful, otherwise return false. */
1547 static bool
1548 unify_scc (struct data_in *data_in, unsigned from,
1549 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1551 bool unified_p = false;
1552 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1553 tree_scc *scc
1554 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1555 scc->next = NULL;
1556 scc->hash = scc_hash;
1557 scc->len = len;
1558 scc->entry_len = scc_entry_len;
1559 for (unsigned i = 0; i < len; ++i)
1561 tree t = streamer_tree_cache_get_tree (cache, from + i);
1562 scc->entries[i] = t;
1563 /* Do not merge SCCs with local entities inside them. Also do
1564 not merge TRANSLATION_UNIT_DECLs. */
1565 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1566 || (VAR_OR_FUNCTION_DECL_P (t)
1567 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1568 || TREE_CODE (t) == LABEL_DECL)
1570 /* Avoid doing any work for these cases and do not worry to
1571 record the SCCs for further merging. */
1572 return false;
1576 /* Look for the list of candidate SCCs to compare against. */
1577 tree_scc **slot;
1578 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1579 if (*slot)
1581 /* Try unifying against each candidate. */
1582 num_scc_compares++;
1584 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1585 outside of the scc when following tree edges. Make sure
1586 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1587 to track whether we visited the SCC member during the compare.
1588 We cannot use TREE_VISITED on the pscc members as the extended
1589 scc and pscc can overlap. */
1590 for (unsigned i = 0; i < scc->len; ++i)
1592 TREE_VISITED (scc->entries[i]) = 1;
1593 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1596 tree *map = XALLOCAVEC (tree, 2 * len);
1597 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1599 if (!compare_tree_sccs (pscc, scc, map))
1600 continue;
1602 /* Found an equal SCC. */
1603 unified_p = true;
1604 num_scc_compare_collisions--;
1605 num_sccs_merged++;
1606 total_scc_size_merged += len;
1608 #ifdef ENABLE_CHECKING
1609 for (unsigned i = 0; i < len; ++i)
1611 tree t = map[2*i+1];
1612 enum tree_code code = TREE_CODE (t);
1613 /* IDENTIFIER_NODEs should be singletons and are merged by the
1614 streamer. The others should be singletons, too, and we
1615 should not merge them in any way. */
1616 gcc_assert (code != TRANSLATION_UNIT_DECL
1617 && code != IDENTIFIER_NODE
1618 && !streamer_handle_as_builtin_p (t));
1620 #endif
1622 /* Fixup the streamer cache with the prevailing nodes according
1623 to the tree node mapping computed by compare_tree_sccs. */
1624 if (len == 1)
1625 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1626 else
1628 tree *map2 = XALLOCAVEC (tree, 2 * len);
1629 for (unsigned i = 0; i < len; ++i)
1631 map2[i*2] = (tree)(uintptr_t)(from + i);
1632 map2[i*2+1] = scc->entries[i];
1634 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1635 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1636 for (unsigned i = 0; i < len; ++i)
1637 streamer_tree_cache_replace_tree (cache, map[2*i],
1638 (uintptr_t)map2[2*i]);
1641 /* Free the tree nodes from the read SCC. */
1642 data_in->location_cache.revert_location_cache ();
1643 for (unsigned i = 0; i < len; ++i)
1645 enum tree_code code;
1646 if (TYPE_P (scc->entries[i]))
1647 num_merged_types++;
1648 code = TREE_CODE (scc->entries[i]);
1649 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1650 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1651 ggc_free (scc->entries[i]);
1654 break;
1657 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1658 if (!unified_p)
1659 for (unsigned i = 0; i < scc->len; ++i)
1660 TREE_VISITED (scc->entries[i]) = 0;
1663 /* If we didn't unify it to any candidate duplicate the relevant
1664 pieces to permanent storage and link it into the chain. */
1665 if (!unified_p)
1667 tree_scc *pscc
1668 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1669 memcpy (pscc, scc, sizeof (tree_scc));
1670 pscc->next = (*slot);
1671 *slot = pscc;
1673 return unified_p;
1677 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1678 RESOLUTIONS is the set of symbols picked by the linker (read from the
1679 resolution file when the linker plugin is being used). */
1681 static void
1682 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1683 vec<ld_plugin_symbol_resolution_t> resolutions)
1685 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1686 const int decl_offset = sizeof (struct lto_decl_header);
1687 const int main_offset = decl_offset + header->decl_state_size;
1688 const int string_offset = main_offset + header->main_size;
1689 struct data_in *data_in;
1690 unsigned int i;
1691 const uint32_t *data_ptr, *data_end;
1692 uint32_t num_decl_states;
1694 lto_input_block ib_main ((const char *) data + main_offset,
1695 header->main_size, decl_data->mode_table);
1697 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1698 header->string_size, resolutions);
1700 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1701 internal types that should not be merged. */
1703 /* Read the global declarations and types. */
1704 while (ib_main.p < ib_main.len)
1706 tree t;
1707 unsigned from = data_in->reader_cache->nodes.length ();
1708 /* Read and uniquify SCCs as in the input stream. */
1709 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1710 if (tag == LTO_tree_scc)
1712 unsigned len_;
1713 unsigned scc_entry_len;
1714 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1715 &scc_entry_len);
1716 unsigned len = data_in->reader_cache->nodes.length () - from;
1717 gcc_assert (len == len_);
1719 total_scc_size += len;
1720 num_sccs_read++;
1722 /* We have the special case of size-1 SCCs that are pre-merged
1723 by means of identifier and string sharing for example.
1724 ??? Maybe we should avoid streaming those as SCCs. */
1725 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1726 from);
1727 if (len == 1
1728 && (TREE_CODE (first) == IDENTIFIER_NODE
1729 || TREE_CODE (first) == INTEGER_CST
1730 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1731 || streamer_handle_as_builtin_p (first)))
1732 continue;
1734 /* Try to unify the SCC with already existing ones. */
1735 if (!flag_ltrans
1736 && unify_scc (data_in, from,
1737 len, scc_entry_len, scc_hash))
1738 continue;
1740 /* Tree merging failed, mark entries in location cache as
1741 permanent. */
1742 data_in->location_cache.accept_location_cache ();
1744 bool seen_type = false;
1745 for (unsigned i = 0; i < len; ++i)
1747 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1748 from + i);
1749 /* Reconstruct the type variant and pointer-to/reference-to
1750 chains. */
1751 if (TYPE_P (t))
1753 seen_type = true;
1754 num_prevailing_types++;
1755 lto_fixup_prevailing_type (t);
1757 /* Compute the canonical type of all types.
1758 ??? Should be able to assert that !TYPE_CANONICAL. */
1759 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1761 gimple_register_canonical_type (t);
1762 if (odr_type_p (t))
1763 register_odr_type (t);
1765 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1766 type which is also member of this SCC. */
1767 if (TREE_CODE (t) == INTEGER_CST
1768 && !TREE_OVERFLOW (t))
1769 cache_integer_cst (t);
1770 /* Register TYPE_DECLs with the debuginfo machinery. */
1771 if (!flag_wpa
1772 && TREE_CODE (t) == TYPE_DECL)
1774 /* Dwarf2out needs location information.
1775 TODO: Moving this out of the streamer loop may noticealy
1776 improve ltrans linemap memory use. */
1777 data_in->location_cache.apply_location_cache ();
1778 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1780 if (!flag_ltrans)
1782 /* Register variables and functions with the
1783 symbol table. */
1784 if (TREE_CODE (t) == VAR_DECL)
1785 lto_register_var_decl_in_symtab (data_in, t, from + i);
1786 else if (TREE_CODE (t) == FUNCTION_DECL
1787 && !DECL_BUILT_IN (t))
1788 lto_register_function_decl_in_symtab (data_in, t, from + i);
1789 /* Scan the tree for references to global functions or
1790 variables and record those for later fixup. */
1791 if (mentions_vars_p (t))
1792 vec_safe_push (tree_with_vars, t);
1795 if (seen_type)
1796 num_type_scc_trees += len;
1798 else
1800 /* Pickle stray references. */
1801 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1802 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1805 data_in->location_cache.apply_location_cache ();
1807 /* Read in lto_in_decl_state objects. */
1808 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1809 data_end =
1810 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1811 num_decl_states = *data_ptr++;
1813 gcc_assert (num_decl_states > 0);
1814 decl_data->global_decl_state = lto_new_in_decl_state ();
1815 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1816 decl_data->global_decl_state);
1818 /* Read in per-function decl states and enter them in hash table. */
1819 decl_data->function_decl_states =
1820 hash_table<decl_state_hasher>::create_ggc (37);
1822 for (i = 1; i < num_decl_states; i++)
1824 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1826 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1827 lto_in_decl_state **slot
1828 = decl_data->function_decl_states->find_slot (state, INSERT);
1829 gcc_assert (*slot == NULL);
1830 *slot = state;
1833 if (data_ptr != data_end)
1834 internal_error ("bytecode stream: garbage at the end of symbols section");
1836 /* Set the current decl state to be the global state. */
1837 decl_data->current_decl_state = decl_data->global_decl_state;
1839 lto_data_in_delete (data_in);
1842 /* Custom version of strtoll, which is not portable. */
1844 static int64_t
1845 lto_parse_hex (const char *p)
1847 int64_t ret = 0;
1849 for (; *p != '\0'; ++p)
1851 char c = *p;
1852 unsigned char part;
1853 ret <<= 4;
1854 if (c >= '0' && c <= '9')
1855 part = c - '0';
1856 else if (c >= 'a' && c <= 'f')
1857 part = c - 'a' + 10;
1858 else if (c >= 'A' && c <= 'F')
1859 part = c - 'A' + 10;
1860 else
1861 internal_error ("could not parse hex number");
1862 ret |= part;
1865 return ret;
1868 /* Read resolution for file named FILE_NAME. The resolution is read from
1869 RESOLUTION. */
1871 static void
1872 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1874 /* We require that objects in the resolution file are in the same
1875 order as the lto1 command line. */
1876 unsigned int name_len;
1877 char *obj_name;
1878 unsigned int num_symbols;
1879 unsigned int i;
1880 struct lto_file_decl_data *file_data;
1881 splay_tree_node nd = NULL;
1883 if (!resolution)
1884 return;
1886 name_len = strlen (file->filename);
1887 obj_name = XNEWVEC (char, name_len + 1);
1888 fscanf (resolution, " "); /* Read white space. */
1890 fread (obj_name, sizeof (char), name_len, resolution);
1891 obj_name[name_len] = '\0';
1892 if (filename_cmp (obj_name, file->filename) != 0)
1893 internal_error ("unexpected file name %s in linker resolution file. "
1894 "Expected %s", obj_name, file->filename);
1895 if (file->offset != 0)
1897 int t;
1898 char offset_p[17];
1899 int64_t offset;
1900 t = fscanf (resolution, "@0x%16s", offset_p);
1901 if (t != 1)
1902 internal_error ("could not parse file offset");
1903 offset = lto_parse_hex (offset_p);
1904 if (offset != file->offset)
1905 internal_error ("unexpected offset");
1908 free (obj_name);
1910 fscanf (resolution, "%u", &num_symbols);
1912 for (i = 0; i < num_symbols; i++)
1914 int t;
1915 unsigned index;
1916 unsigned HOST_WIDE_INT id;
1917 char r_str[27];
1918 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1919 unsigned int j;
1920 unsigned int lto_resolution_str_len =
1921 sizeof (lto_resolution_str) / sizeof (char *);
1922 res_pair rp;
1924 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1925 &index, &id, r_str);
1926 if (t != 3)
1927 internal_error ("invalid line in the resolution file");
1929 for (j = 0; j < lto_resolution_str_len; j++)
1931 if (strcmp (lto_resolution_str[j], r_str) == 0)
1933 r = (enum ld_plugin_symbol_resolution) j;
1934 break;
1937 if (j == lto_resolution_str_len)
1938 internal_error ("invalid resolution in the resolution file");
1940 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1942 nd = lto_splay_tree_lookup (file_ids, id);
1943 if (nd == NULL)
1944 internal_error ("resolution sub id %wx not in object file", id);
1947 file_data = (struct lto_file_decl_data *)nd->value;
1948 /* The indexes are very sparse. To save memory save them in a compact
1949 format that is only unpacked later when the subfile is processed. */
1950 rp.res = r;
1951 rp.index = index;
1952 file_data->respairs.safe_push (rp);
1953 if (file_data->max_index < index)
1954 file_data->max_index = index;
1958 /* List of file_decl_datas */
1959 struct file_data_list
1961 struct lto_file_decl_data *first, *last;
1964 /* Is the name for a id'ed LTO section? */
1966 static int
1967 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1969 const char *s;
1971 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
1972 return 0;
1973 s = strrchr (name, '.');
1974 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1977 /* Create file_data of each sub file id */
1979 static int
1980 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1981 struct file_data_list *list)
1983 struct lto_section_slot s_slot, *new_slot;
1984 unsigned HOST_WIDE_INT id;
1985 splay_tree_node nd;
1986 void **hash_slot;
1987 char *new_name;
1988 struct lto_file_decl_data *file_data;
1990 if (!lto_section_with_id (ls->name, &id))
1991 return 1;
1993 /* Find hash table of sub module id */
1994 nd = lto_splay_tree_lookup (file_ids, id);
1995 if (nd != NULL)
1997 file_data = (struct lto_file_decl_data *)nd->value;
1999 else
2001 file_data = ggc_alloc<lto_file_decl_data> ();
2002 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2003 file_data->id = id;
2004 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
2005 lto_splay_tree_insert (file_ids, id, file_data);
2007 /* Maintain list in linker order */
2008 if (!list->first)
2009 list->first = file_data;
2010 if (list->last)
2011 list->last->next = file_data;
2012 list->last = file_data;
2015 /* Copy section into sub module hash table */
2016 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2017 s_slot.name = new_name;
2018 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2019 gcc_assert (*hash_slot == NULL);
2021 new_slot = XDUP (struct lto_section_slot, ls);
2022 new_slot->name = new_name;
2023 *hash_slot = new_slot;
2024 return 1;
2027 /* Read declarations and other initializations for a FILE_DATA. */
2029 static void
2030 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2032 const char *data;
2033 size_t len;
2034 vec<ld_plugin_symbol_resolution_t>
2035 resolutions = vNULL;
2036 int i;
2037 res_pair *rp;
2039 /* Create vector for fast access of resolution. We do this lazily
2040 to save memory. */
2041 resolutions.safe_grow_cleared (file_data->max_index + 1);
2042 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2043 resolutions[rp->index] = rp->res;
2044 file_data->respairs.release ();
2046 file_data->renaming_hash_table = lto_create_renaming_table ();
2047 file_data->file_name = file->filename;
2048 #ifdef ACCEL_COMPILER
2049 lto_input_mode_table (file_data);
2050 #else
2051 file_data->mode_table = lto_mode_identity_table;
2052 #endif
2053 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2054 if (data == NULL)
2056 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2057 return;
2059 /* Frees resolutions */
2060 lto_read_decls (file_data, data, resolutions);
2061 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2064 /* Finalize FILE_DATA in FILE and increase COUNT. */
2066 static int
2067 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2068 int *count)
2070 lto_file_finalize (file_data, file);
2071 if (symtab->dump_file)
2072 fprintf (symtab->dump_file,
2073 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2074 file_data->file_name, file_data->id);
2075 (*count)++;
2076 return 0;
2079 /* Generate a TREE representation for all types and external decls
2080 entities in FILE.
2082 Read all of the globals out of the file. Then read the cgraph
2083 and process the .o index into the cgraph nodes so that it can open
2084 the .o file to load the functions and ipa information. */
2086 static struct lto_file_decl_data *
2087 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2089 struct lto_file_decl_data *file_data = NULL;
2090 splay_tree file_ids;
2091 htab_t section_hash_table;
2092 struct lto_section_slot *section;
2093 struct file_data_list file_list;
2094 struct lto_section_list section_list;
2096 memset (&section_list, 0, sizeof (struct lto_section_list));
2097 section_hash_table = lto_obj_build_section_table (file, &section_list);
2099 /* Find all sub modules in the object and put their sections into new hash
2100 tables in a splay tree. */
2101 file_ids = lto_splay_tree_new ();
2102 memset (&file_list, 0, sizeof (struct file_data_list));
2103 for (section = section_list.first; section != NULL; section = section->next)
2104 create_subid_section_table (section, file_ids, &file_list);
2106 /* Add resolutions to file ids */
2107 lto_resolution_read (file_ids, resolution_file, file);
2109 /* Finalize each lto file for each submodule in the merged object */
2110 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2111 lto_create_files_from_ids (file, file_data, count);
2113 splay_tree_delete (file_ids);
2114 htab_delete (section_hash_table);
2116 return file_list.first;
2119 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2120 #define LTO_MMAP_IO 1
2121 #endif
2123 #if LTO_MMAP_IO
2124 /* Page size of machine is used for mmap and munmap calls. */
2125 static size_t page_mask;
2126 #endif
2128 /* Get the section data of length LEN from FILENAME starting at
2129 OFFSET. The data segment must be freed by the caller when the
2130 caller is finished. Returns NULL if all was not well. */
2132 static char *
2133 lto_read_section_data (struct lto_file_decl_data *file_data,
2134 intptr_t offset, size_t len)
2136 char *result;
2137 static int fd = -1;
2138 static char *fd_name;
2139 #if LTO_MMAP_IO
2140 intptr_t computed_len;
2141 intptr_t computed_offset;
2142 intptr_t diff;
2143 #endif
2145 /* Keep a single-entry file-descriptor cache. The last file we
2146 touched will get closed at exit.
2147 ??? Eventually we want to add a more sophisticated larger cache
2148 or rather fix function body streaming to not stream them in
2149 practically random order. */
2150 if (fd != -1
2151 && filename_cmp (fd_name, file_data->file_name) != 0)
2153 free (fd_name);
2154 close (fd);
2155 fd = -1;
2157 if (fd == -1)
2159 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2160 if (fd == -1)
2162 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2163 return NULL;
2165 fd_name = xstrdup (file_data->file_name);
2168 #if LTO_MMAP_IO
2169 if (!page_mask)
2171 size_t page_size = sysconf (_SC_PAGE_SIZE);
2172 page_mask = ~(page_size - 1);
2175 computed_offset = offset & page_mask;
2176 diff = offset - computed_offset;
2177 computed_len = len + diff;
2179 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2180 fd, computed_offset);
2181 if (result == MAP_FAILED)
2183 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2184 return NULL;
2187 return result + diff;
2188 #else
2189 result = (char *) xmalloc (len);
2190 if (lseek (fd, offset, SEEK_SET) != offset
2191 || read (fd, result, len) != (ssize_t) len)
2193 free (result);
2194 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2195 result = NULL;
2197 #ifdef __MINGW32__
2198 /* Native windows doesn't supports delayed unlink on opened file. So
2199 we close file here again. This produces higher I/O load, but at least
2200 it prevents to have dangling file handles preventing unlink. */
2201 free (fd_name);
2202 fd_name = NULL;
2203 close (fd);
2204 fd = -1;
2205 #endif
2206 return result;
2207 #endif
2211 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2212 NAME will be NULL unless the section type is for a function
2213 body. */
2215 static const char *
2216 get_section_data (struct lto_file_decl_data *file_data,
2217 enum lto_section_type section_type,
2218 const char *name,
2219 size_t *len)
2221 htab_t section_hash_table = file_data->section_hash_table;
2222 struct lto_section_slot *f_slot;
2223 struct lto_section_slot s_slot;
2224 const char *section_name = lto_get_section_name (section_type, name, file_data);
2225 char *data = NULL;
2227 *len = 0;
2228 s_slot.name = section_name;
2229 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2230 if (f_slot)
2232 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2233 *len = f_slot->len;
2236 free (CONST_CAST (char *, section_name));
2237 return data;
2241 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2242 starts at OFFSET and has LEN bytes. */
2244 static void
2245 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2246 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2247 const char *name ATTRIBUTE_UNUSED,
2248 const char *offset, size_t len ATTRIBUTE_UNUSED)
2250 #if LTO_MMAP_IO
2251 intptr_t computed_len;
2252 intptr_t computed_offset;
2253 intptr_t diff;
2254 #endif
2256 #if LTO_MMAP_IO
2257 computed_offset = ((intptr_t) offset) & page_mask;
2258 diff = (intptr_t) offset - computed_offset;
2259 computed_len = len + diff;
2261 munmap ((caddr_t) computed_offset, computed_len);
2262 #else
2263 free (CONST_CAST(char *, offset));
2264 #endif
2267 static lto_file *current_lto_file;
2269 /* Helper for qsort; compare partitions and return one with smaller size.
2270 We sort from greatest to smallest so parallel build doesn't stale on the
2271 longest compilation being executed too late. */
2273 static int
2274 cmp_partitions_size (const void *a, const void *b)
2276 const struct ltrans_partition_def *pa
2277 = *(struct ltrans_partition_def *const *)a;
2278 const struct ltrans_partition_def *pb
2279 = *(struct ltrans_partition_def *const *)b;
2280 return pb->insns - pa->insns;
2283 /* Helper for qsort; compare partitions and return one with smaller order. */
2285 static int
2286 cmp_partitions_order (const void *a, const void *b)
2288 const struct ltrans_partition_def *pa
2289 = *(struct ltrans_partition_def *const *)a;
2290 const struct ltrans_partition_def *pb
2291 = *(struct ltrans_partition_def *const *)b;
2292 int ordera = -1, orderb = -1;
2294 if (lto_symtab_encoder_size (pa->encoder))
2295 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2296 if (lto_symtab_encoder_size (pb->encoder))
2297 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2298 return orderb - ordera;
2301 /* Actually stream out ENCODER into TEMP_FILENAME. */
2303 static void
2304 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2306 lto_file *file = lto_obj_file_open (temp_filename, true);
2307 if (!file)
2308 fatal_error (input_location, "lto_obj_file_open() failed");
2309 lto_set_current_out_file (file);
2311 ipa_write_optimization_summaries (encoder);
2313 lto_set_current_out_file (NULL);
2314 lto_obj_file_close (file);
2315 free (file);
2318 /* Wait for forked process and signal errors. */
2319 #ifdef HAVE_WORKING_FORK
2320 static void
2321 wait_for_child ()
2323 int status;
2326 #ifndef WCONTINUED
2327 #define WCONTINUED 0
2328 #endif
2329 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2330 if (w == -1)
2331 fatal_error (input_location, "waitpid failed");
2333 if (WIFEXITED (status) && WEXITSTATUS (status))
2334 fatal_error (input_location, "streaming subprocess failed");
2335 else if (WIFSIGNALED (status))
2336 fatal_error (input_location,
2337 "streaming subprocess was killed by signal");
2339 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2341 #endif
2343 /* Stream out ENCODER into TEMP_FILENAME
2344 Fork if that seems to help. */
2346 static void
2347 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2348 bool ARG_UNUSED (last))
2350 #ifdef HAVE_WORKING_FORK
2351 static int nruns;
2353 if (lto_parallelism <= 1)
2355 do_stream_out (temp_filename, encoder);
2356 return;
2359 /* Do not run more than LTO_PARALLELISM streamings
2360 FIXME: we ignore limits on jobserver. */
2361 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2363 wait_for_child ();
2364 nruns --;
2366 /* If this is not the last parallel partition, execute new
2367 streaming process. */
2368 if (!last)
2370 pid_t cpid = fork ();
2372 if (!cpid)
2374 setproctitle ("lto1-wpa-streaming");
2375 do_stream_out (temp_filename, encoder);
2376 exit (0);
2378 /* Fork failed; lets do the job ourseleves. */
2379 else if (cpid == -1)
2380 do_stream_out (temp_filename, encoder);
2381 else
2382 nruns++;
2384 /* Last partition; stream it and wait for all children to die. */
2385 else
2387 int i;
2388 do_stream_out (temp_filename, encoder);
2389 for (i = 0; i < nruns; i++)
2390 wait_for_child ();
2392 asm_nodes_output = true;
2393 #else
2394 do_stream_out (temp_filename, encoder);
2395 #endif
2398 /* Write all output files in WPA mode and the file with the list of
2399 LTRANS units. */
2401 static void
2402 lto_wpa_write_files (void)
2404 unsigned i, n_sets;
2405 ltrans_partition part;
2406 FILE *ltrans_output_list_stream;
2407 char *temp_filename;
2408 vec <char *>temp_filenames = vNULL;
2409 size_t blen;
2411 /* Open the LTRANS output list. */
2412 if (!ltrans_output_list)
2413 fatal_error (input_location, "no LTRANS output list filename provided");
2415 timevar_push (TV_WHOPR_WPA);
2417 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2418 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2420 timevar_pop (TV_WHOPR_WPA);
2422 timevar_push (TV_WHOPR_WPA_IO);
2424 /* Generate a prefix for the LTRANS unit files. */
2425 blen = strlen (ltrans_output_list);
2426 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2427 strcpy (temp_filename, ltrans_output_list);
2428 if (blen > sizeof (".out")
2429 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2430 ".out") == 0)
2431 temp_filename[blen - sizeof (".out") + 1] = '\0';
2432 blen = strlen (temp_filename);
2434 n_sets = ltrans_partitions.length ();
2436 /* Sort partitions by size so small ones are compiled last.
2437 FIXME: Even when not reordering we may want to output one list for parallel make
2438 and other for final link command. */
2440 if (!flag_profile_reorder_functions || !flag_profile_use)
2441 ltrans_partitions.qsort (flag_toplevel_reorder
2442 ? cmp_partitions_size
2443 : cmp_partitions_order);
2445 for (i = 0; i < n_sets; i++)
2447 ltrans_partition part = ltrans_partitions[i];
2449 /* Write all the nodes in SET. */
2450 sprintf (temp_filename + blen, "%u.o", i);
2452 if (!quiet_flag)
2453 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2454 if (symtab->dump_file)
2456 lto_symtab_encoder_iterator lsei;
2458 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2459 part->name, temp_filename, part->insns);
2460 fprintf (symtab->dump_file, " Symbols in partition: ");
2461 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2462 lsei_next_in_partition (&lsei))
2464 symtab_node *node = lsei_node (lsei);
2465 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2467 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2468 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2469 lsei_next (&lsei))
2471 symtab_node *node = lsei_node (lsei);
2472 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2474 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2475 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2476 if (cnode
2477 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2478 fprintf (symtab->dump_file, "(body included)");
2479 else
2481 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2482 if (vnode
2483 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2484 fprintf (symtab->dump_file, "(initializer included)");
2488 fprintf (symtab->dump_file, "\n");
2490 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2492 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2494 part->encoder = NULL;
2496 temp_filenames.safe_push (xstrdup (temp_filename));
2498 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2499 if (ltrans_output_list_stream == NULL)
2500 fatal_error (input_location,
2501 "opening LTRANS output list %s: %m", ltrans_output_list);
2502 for (i = 0; i < n_sets; i++)
2504 unsigned int len = strlen (temp_filenames[i]);
2505 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2506 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2507 fatal_error (input_location, "writing to LTRANS output list %s: %m",
2508 ltrans_output_list);
2509 free (temp_filenames[i]);
2511 temp_filenames.release();
2513 lto_stats.num_output_files += n_sets;
2515 /* Close the LTRANS output list. */
2516 if (fclose (ltrans_output_list_stream))
2517 fatal_error (input_location,
2518 "closing LTRANS output list %s: %m", ltrans_output_list);
2520 free_ltrans_partitions();
2521 free (temp_filename);
2523 timevar_pop (TV_WHOPR_WPA_IO);
2527 /* If TT is a variable or function decl replace it with its
2528 prevailing variant. */
2529 #define LTO_SET_PREVAIL(tt) \
2530 do {\
2531 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2532 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2534 tt = lto_symtab_prevailing_decl (tt); \
2535 fixed = true; \
2537 } while (0)
2539 /* Ensure that TT isn't a replacable var of function decl. */
2540 #define LTO_NO_PREVAIL(tt) \
2541 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2543 /* Given a tree T replace all fields referring to variables or functions
2544 with their prevailing variant. */
2545 static void
2546 lto_fixup_prevailing_decls (tree t)
2548 enum tree_code code = TREE_CODE (t);
2549 bool fixed = false;
2551 gcc_checking_assert (code != TREE_BINFO);
2552 LTO_NO_PREVAIL (TREE_TYPE (t));
2553 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2554 LTO_NO_PREVAIL (TREE_CHAIN (t));
2555 if (DECL_P (t))
2557 LTO_NO_PREVAIL (DECL_NAME (t));
2558 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2559 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2561 LTO_SET_PREVAIL (DECL_SIZE (t));
2562 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2563 LTO_SET_PREVAIL (DECL_INITIAL (t));
2564 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2565 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2567 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2569 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2571 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2573 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2575 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2577 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2578 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2579 LTO_NO_PREVAIL (DECL_VINDEX (t));
2581 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2583 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2584 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2585 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2586 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2587 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2590 else if (TYPE_P (t))
2592 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2593 LTO_SET_PREVAIL (TYPE_SIZE (t));
2594 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2595 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2596 LTO_NO_PREVAIL (TYPE_NAME (t));
2598 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2599 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2600 LTO_NO_PREVAIL (t->type_non_common.binfo);
2602 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2604 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2605 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2606 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2608 else if (EXPR_P (t))
2610 int i;
2611 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2612 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2614 else if (TREE_CODE (t) == CONSTRUCTOR)
2616 unsigned i;
2617 tree val;
2618 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2619 LTO_SET_PREVAIL (val);
2621 else
2623 switch (code)
2625 case TREE_LIST:
2626 LTO_SET_PREVAIL (TREE_VALUE (t));
2627 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2628 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2629 break;
2630 default:
2631 gcc_unreachable ();
2634 /* If we fixed nothing, then we missed something seen by
2635 mentions_vars_p. */
2636 gcc_checking_assert (fixed);
2638 #undef LTO_SET_PREVAIL
2639 #undef LTO_NO_PREVAIL
2641 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2642 replaces var and function decls with the corresponding prevailing def. */
2644 static void
2645 lto_fixup_state (struct lto_in_decl_state *state)
2647 unsigned i, si;
2649 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2650 we still need to walk from all DECLs to find the reachable
2651 FUNCTION_DECLs and VAR_DECLs. */
2652 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2654 vec<tree, va_gc> *trees = state->streams[si];
2655 for (i = 0; i < vec_safe_length (trees); i++)
2657 tree t = (*trees)[i];
2658 #ifdef ENABLE_CHECKING
2659 if (TYPE_P (t))
2660 verify_type (t);
2661 #endif
2662 if (VAR_OR_FUNCTION_DECL_P (t)
2663 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2664 (*trees)[i] = lto_symtab_prevailing_decl (t);
2669 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2670 prevailing one. */
2672 static void
2673 lto_fixup_decls (struct lto_file_decl_data **files)
2675 unsigned int i;
2676 tree t;
2678 if (tree_with_vars)
2679 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2680 lto_fixup_prevailing_decls (t);
2682 for (i = 0; files[i]; i++)
2684 struct lto_file_decl_data *file = files[i];
2685 struct lto_in_decl_state *state = file->global_decl_state;
2686 lto_fixup_state (state);
2688 hash_table<decl_state_hasher>::iterator iter;
2689 lto_in_decl_state *elt;
2690 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2691 lto_in_decl_state *, iter)
2692 lto_fixup_state (elt);
2696 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2698 /* Turn file datas for sub files into a single array, so that they look
2699 like separate files for further passes. */
2701 static void
2702 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2704 struct lto_file_decl_data *n, *next;
2705 int i, k;
2707 lto_stats.num_input_files = count;
2708 all_file_decl_data
2709 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2710 /* Set the hooks so that all of the ipa passes can read in their data. */
2711 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2712 for (i = 0, k = 0; i < last_file_ix; i++)
2714 for (n = orig[i]; n != NULL; n = next)
2716 all_file_decl_data[k++] = n;
2717 next = n->next;
2718 n->next = NULL;
2721 all_file_decl_data[k] = NULL;
2722 gcc_assert (k == count);
2725 /* Input file data before flattening (i.e. splitting them to subfiles to support
2726 incremental linking. */
2727 static int real_file_count;
2728 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2730 static void print_lto_report_1 (void);
2732 /* Read all the symbols from the input files FNAMES. NFILES is the
2733 number of files requested in the command line. Instantiate a
2734 global call graph by aggregating all the sub-graphs found in each
2735 file. */
2737 static void
2738 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2740 unsigned int i, last_file_ix;
2741 FILE *resolution;
2742 int count = 0;
2743 struct lto_file_decl_data **decl_data;
2744 symtab_node *snode;
2746 symtab->initialize ();
2748 timevar_push (TV_IPA_LTO_DECL_IN);
2750 #ifdef ACCEL_COMPILER
2751 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2752 lto_stream_offload_p = true;
2753 #endif
2755 real_file_decl_data
2756 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2757 real_file_count = nfiles;
2759 /* Read the resolution file. */
2760 resolution = NULL;
2761 if (resolution_file_name)
2763 int t;
2764 unsigned num_objects;
2766 resolution = fopen (resolution_file_name, "r");
2767 if (resolution == NULL)
2768 fatal_error (input_location,
2769 "could not open symbol resolution file: %m");
2771 t = fscanf (resolution, "%u", &num_objects);
2772 gcc_assert (t == 1);
2774 /* True, since the plugin splits the archives. */
2775 gcc_assert (num_objects == nfiles);
2777 symtab->state = LTO_STREAMING;
2779 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2780 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2781 gimple_canonical_type_eq, NULL);
2782 gcc_obstack_init (&tree_scc_hash_obstack);
2783 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2785 /* Register the common node types with the canonical type machinery so
2786 we properly share alias-sets across languages and TUs. Do not
2787 expose the common nodes as type merge target - those that should be
2788 are already exposed so by pre-loading the LTO streamer caches.
2789 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2790 for (i = 0; i < itk_none; ++i)
2791 lto_register_canonical_types (integer_types[i], true);
2792 for (i = 0; i < stk_type_kind_last; ++i)
2793 lto_register_canonical_types (sizetype_tab[i], true);
2794 for (i = 0; i < TI_MAX; ++i)
2795 lto_register_canonical_types (global_trees[i], true);
2796 for (i = 0; i < itk_none; ++i)
2797 lto_register_canonical_types (integer_types[i], false);
2798 for (i = 0; i < stk_type_kind_last; ++i)
2799 lto_register_canonical_types (sizetype_tab[i], false);
2800 for (i = 0; i < TI_MAX; ++i)
2801 lto_register_canonical_types (global_trees[i], false);
2803 if (!quiet_flag)
2804 fprintf (stderr, "Reading object files:");
2806 /* Read all of the object files specified on the command line. */
2807 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2809 struct lto_file_decl_data *file_data = NULL;
2810 if (!quiet_flag)
2812 fprintf (stderr, " %s", fnames[i]);
2813 fflush (stderr);
2816 current_lto_file = lto_obj_file_open (fnames[i], false);
2817 if (!current_lto_file)
2818 break;
2820 file_data = lto_file_read (current_lto_file, resolution, &count);
2821 if (!file_data)
2823 lto_obj_file_close (current_lto_file);
2824 free (current_lto_file);
2825 current_lto_file = NULL;
2826 break;
2829 decl_data[last_file_ix++] = file_data;
2831 lto_obj_file_close (current_lto_file);
2832 free (current_lto_file);
2833 current_lto_file = NULL;
2836 lto_flatten_files (decl_data, count, last_file_ix);
2837 lto_stats.num_input_files = count;
2838 ggc_free(decl_data);
2839 real_file_decl_data = NULL;
2841 if (resolution_file_name)
2842 fclose (resolution);
2844 /* Show the LTO report before launching LTRANS. */
2845 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2846 print_lto_report_1 ();
2848 /* Free gimple type merging datastructures. */
2849 delete tree_scc_hash;
2850 tree_scc_hash = NULL;
2851 obstack_free (&tree_scc_hash_obstack, NULL);
2852 htab_delete (gimple_canonical_types);
2853 gimple_canonical_types = NULL;
2854 delete canonical_type_hash_cache;
2855 canonical_type_hash_cache = NULL;
2857 /* At this stage we know that majority of GGC memory is reachable.
2858 Growing the limits prevents unnecesary invocation of GGC. */
2859 ggc_grow ();
2860 ggc_collect ();
2862 /* Set the hooks so that all of the ipa passes can read in their data. */
2863 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2865 timevar_pop (TV_IPA_LTO_DECL_IN);
2867 if (!quiet_flag)
2868 fprintf (stderr, "\nReading the callgraph\n");
2870 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2871 /* Read the symtab. */
2872 input_symtab ();
2874 input_offload_tables ();
2876 /* Store resolutions into the symbol table. */
2878 ld_plugin_symbol_resolution_t *res;
2879 FOR_EACH_SYMBOL (snode)
2880 if (snode->real_symbol_p ()
2881 && snode->lto_file_data
2882 && snode->lto_file_data->resolution_map
2883 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
2884 snode->resolution = *res;
2885 for (i = 0; all_file_decl_data[i]; i++)
2886 if (all_file_decl_data[i]->resolution_map)
2888 delete all_file_decl_data[i]->resolution_map;
2889 all_file_decl_data[i]->resolution_map = NULL;
2892 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2894 if (!quiet_flag)
2895 fprintf (stderr, "Merging declarations\n");
2897 timevar_push (TV_IPA_LTO_DECL_MERGE);
2898 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2899 need to care about resolving symbols again, we only need to replace
2900 duplicated declarations read from the callgraph and from function
2901 sections. */
2902 if (!flag_ltrans)
2904 lto_symtab_merge_decls ();
2906 /* If there were errors during symbol merging bail out, we have no
2907 good way to recover here. */
2908 if (seen_error ())
2909 fatal_error (input_location,
2910 "errors during merging of translation units");
2912 /* Fixup all decls. */
2913 lto_fixup_decls (all_file_decl_data);
2915 if (tree_with_vars)
2916 ggc_free (tree_with_vars);
2917 tree_with_vars = NULL;
2918 ggc_collect ();
2920 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2921 /* Each pass will set the appropriate timer. */
2923 if (!quiet_flag)
2924 fprintf (stderr, "Reading summaries\n");
2926 /* Read the IPA summary data. */
2927 if (flag_ltrans)
2928 ipa_read_optimization_summaries ();
2929 else
2930 ipa_read_summaries ();
2932 for (i = 0; all_file_decl_data[i]; i++)
2934 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2935 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2936 all_file_decl_data[i]->symtab_node_encoder = NULL;
2937 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
2938 all_file_decl_data[i]->global_decl_state = NULL;
2939 all_file_decl_data[i]->current_decl_state = NULL;
2942 /* Finally merge the cgraph according to the decl merging decisions. */
2943 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2944 if (symtab->dump_file)
2946 fprintf (symtab->dump_file, "Before merging:\n");
2947 symtab_node::dump_table (symtab->dump_file);
2949 if (!flag_ltrans)
2951 lto_symtab_merge_symbols ();
2952 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2953 we are still having duplicated comdat groups containing local statics.
2954 We could also just remove them while merging. */
2955 symtab->remove_unreachable_nodes (dump_file);
2957 ggc_collect ();
2958 symtab->state = IPA_SSA;
2959 /* FIXME: Technically all node removals happening here are useless, because
2960 WPA should not stream them. */
2961 if (flag_ltrans)
2962 symtab->remove_unreachable_nodes (dump_file);
2964 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2966 /* Indicate that the cgraph is built and ready. */
2967 symtab->function_flags_ready = true;
2969 ggc_free (all_file_decl_data);
2970 all_file_decl_data = NULL;
2974 /* Materialize all the bodies for all the nodes in the callgraph. */
2976 static void
2977 materialize_cgraph (void)
2979 struct cgraph_node *node;
2980 timevar_id_t lto_timer;
2982 if (!quiet_flag)
2983 fprintf (stderr,
2984 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2987 FOR_EACH_FUNCTION (node)
2989 if (node->lto_file_data)
2991 lto_materialize_function (node);
2992 lto_stats.num_input_cgraph_nodes++;
2997 /* Start the appropriate timer depending on the mode that we are
2998 operating in. */
2999 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
3000 : (flag_ltrans) ? TV_WHOPR_LTRANS
3001 : TV_LTO;
3002 timevar_push (lto_timer);
3004 current_function_decl = NULL;
3005 set_cfun (NULL);
3007 if (!quiet_flag)
3008 fprintf (stderr, "\n");
3010 timevar_pop (lto_timer);
3014 /* Show various memory usage statistics related to LTO. */
3015 static void
3016 print_lto_report_1 (void)
3018 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3019 fprintf (stderr, "%s statistics\n", pfx);
3021 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3022 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3023 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3024 if (flag_wpa && tree_scc_hash)
3026 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3027 "collision ratio: %f\n", pfx,
3028 (long) tree_scc_hash->size (),
3029 (long) tree_scc_hash->elements (),
3030 tree_scc_hash->collisions ());
3031 hash_table<tree_scc_hasher>::iterator hiter;
3032 tree_scc *scc, *max_scc = NULL;
3033 unsigned max_length = 0;
3034 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3036 unsigned length = 0;
3037 tree_scc *s = scc;
3038 for (; s; s = s->next)
3039 length++;
3040 if (length > max_length)
3042 max_length = length;
3043 max_scc = scc;
3046 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3047 pfx, max_length, max_scc->len);
3048 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3049 num_scc_compares, num_scc_compare_collisions,
3050 num_scc_compare_collisions / (double) num_scc_compares);
3051 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3052 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3053 total_scc_size_merged);
3054 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3055 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3056 pfx, num_prevailing_types, num_type_scc_trees);
3057 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3058 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3059 (long) htab_size (gimple_canonical_types),
3060 (long) htab_elements (gimple_canonical_types),
3061 (long) gimple_canonical_types->searches,
3062 (long) gimple_canonical_types->collisions,
3063 htab_collisions (gimple_canonical_types));
3064 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3065 "%lu elements, %ld searches\n", pfx,
3066 num_canonical_type_hash_entries,
3067 num_canonical_type_hash_queries);
3070 print_lto_report (pfx);
3073 /* Perform whole program analysis (WPA) on the callgraph and write out the
3074 optimization plan. */
3076 static void
3077 do_whole_program_analysis (void)
3079 symtab_node *node;
3081 lto_parallelism = 1;
3083 /* TODO: jobserver communicatoin is not supported, yet. */
3084 if (!strcmp (flag_wpa, "jobserver"))
3085 lto_parallelism = -1;
3086 else
3088 lto_parallelism = atoi (flag_wpa);
3089 if (lto_parallelism <= 0)
3090 lto_parallelism = 0;
3093 timevar_start (TV_PHASE_OPT_GEN);
3095 /* Note that since we are in WPA mode, materialize_cgraph will not
3096 actually read in all the function bodies. It only materializes
3097 the decls and cgraph nodes so that analysis can be performed. */
3098 materialize_cgraph ();
3100 /* Reading in the cgraph uses different timers, start timing WPA now. */
3101 timevar_push (TV_WHOPR_WPA);
3103 if (pre_ipa_mem_report)
3105 fprintf (stderr, "Memory consumption before IPA\n");
3106 dump_memory_report (false);
3109 symtab->function_flags_ready = true;
3111 if (symtab->dump_file)
3112 symtab_node::dump_table (symtab->dump_file);
3113 bitmap_obstack_initialize (NULL);
3114 symtab->state = IPA_SSA;
3116 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3118 if (symtab->dump_file)
3120 fprintf (symtab->dump_file, "Optimized ");
3121 symtab_node::dump_table (symtab->dump_file);
3123 #ifdef ENABLE_CHECKING
3124 symtab_node::verify_symtab_nodes ();
3125 #endif
3126 bitmap_obstack_release (NULL);
3128 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3129 timevar_pop (TV_WHOPR_WPA);
3131 timevar_push (TV_WHOPR_PARTITIONING);
3132 if (flag_lto_partition == LTO_PARTITION_1TO1)
3133 lto_1_to_1_map ();
3134 else if (flag_lto_partition == LTO_PARTITION_MAX)
3135 lto_max_map ();
3136 else if (flag_lto_partition == LTO_PARTITION_ONE)
3137 lto_balanced_map (1);
3138 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3139 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3140 else
3141 gcc_unreachable ();
3143 /* Inline summaries are needed for balanced partitioning. Free them now so
3144 the memory can be used for streamer caches. */
3145 inline_free_summary ();
3147 /* AUX pointers are used by partitioning code to bookkeep number of
3148 partitions symbol is in. This is no longer needed. */
3149 FOR_EACH_SYMBOL (node)
3150 node->aux = NULL;
3152 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3154 /* Find out statics that need to be promoted
3155 to globals with hidden visibility because they are accessed from multiple
3156 partitions. */
3157 lto_promote_cross_file_statics ();
3158 timevar_pop (TV_WHOPR_PARTITIONING);
3160 timevar_stop (TV_PHASE_OPT_GEN);
3162 /* Collect a last time - in lto_wpa_write_files we may end up forking
3163 with the idea that this doesn't increase memory usage. So we
3164 absoultely do not want to collect after that. */
3165 ggc_collect ();
3167 timevar_start (TV_PHASE_STREAM_OUT);
3168 if (!quiet_flag)
3170 fprintf (stderr, "\nStreaming out");
3171 fflush (stderr);
3173 lto_wpa_write_files ();
3174 if (!quiet_flag)
3175 fprintf (stderr, "\n");
3176 timevar_stop (TV_PHASE_STREAM_OUT);
3178 if (post_ipa_mem_report)
3180 fprintf (stderr, "Memory consumption after IPA\n");
3181 dump_memory_report (false);
3184 /* Show the LTO report before launching LTRANS. */
3185 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3186 print_lto_report_1 ();
3187 if (mem_report_wpa)
3188 dump_memory_report (true);
3192 static GTY(()) tree lto_eh_personality_decl;
3194 /* Return the LTO personality function decl. */
3196 tree
3197 lto_eh_personality (void)
3199 if (!lto_eh_personality_decl)
3201 /* Use the first personality DECL for our personality if we don't
3202 support multiple ones. This ensures that we don't artificially
3203 create the need for them in a single-language program. */
3204 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3205 lto_eh_personality_decl = first_personality_decl;
3206 else
3207 lto_eh_personality_decl = lhd_gcc_personality ();
3210 return lto_eh_personality_decl;
3213 /* Set the process name based on the LTO mode. */
3215 static void
3216 lto_process_name (void)
3218 if (flag_lto)
3219 setproctitle ("lto1-lto");
3220 if (flag_wpa)
3221 setproctitle ("lto1-wpa");
3222 if (flag_ltrans)
3223 setproctitle ("lto1-ltrans");
3227 /* Initialize the LTO front end. */
3229 static void
3230 lto_init (void)
3232 lto_process_name ();
3233 lto_streamer_hooks_init ();
3234 lto_reader_init ();
3235 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3236 memset (&lto_stats, 0, sizeof (lto_stats));
3237 bitmap_obstack_initialize (NULL);
3238 gimple_register_cfg_hooks ();
3239 #ifndef ACCEL_COMPILER
3240 unsigned char *table
3241 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3242 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3243 table[m] = m;
3244 lto_mode_identity_table = table;
3245 #endif
3249 /* Main entry point for the GIMPLE front end. This front end has
3250 three main personalities:
3252 - LTO (-flto). All the object files on the command line are
3253 loaded in memory and processed as a single translation unit.
3254 This is the traditional link-time optimization behavior.
3256 - WPA (-fwpa). Only the callgraph and summary information for
3257 files in the command file are loaded. A single callgraph
3258 (without function bodies) is instantiated for the whole set of
3259 files. IPA passes are only allowed to analyze the call graph
3260 and make transformation decisions. The callgraph is
3261 partitioned, each partition is written to a new object file
3262 together with the transformation decisions.
3264 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3265 summary files from running again. Since WPA computed summary
3266 information and decided what transformations to apply, LTRANS
3267 simply applies them. */
3269 void
3270 lto_main (void)
3272 /* LTO is called as a front end, even though it is not a front end.
3273 Because it is called as a front end, TV_PHASE_PARSING and
3274 TV_PARSE_GLOBAL are active, and we need to turn them off while
3275 doing LTO. Later we turn them back on so they are active up in
3276 toplev.c. */
3277 timevar_pop (TV_PARSE_GLOBAL);
3278 timevar_stop (TV_PHASE_PARSING);
3280 timevar_start (TV_PHASE_SETUP);
3282 /* Initialize the LTO front end. */
3283 lto_init ();
3285 timevar_stop (TV_PHASE_SETUP);
3286 timevar_start (TV_PHASE_STREAM_IN);
3288 /* Read all the symbols and call graph from all the files in the
3289 command line. */
3290 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3292 timevar_stop (TV_PHASE_STREAM_IN);
3294 if (!seen_error ())
3296 /* If WPA is enabled analyze the whole call graph and create an
3297 optimization plan. Otherwise, read in all the function
3298 bodies and continue with optimization. */
3299 if (flag_wpa)
3300 do_whole_program_analysis ();
3301 else
3303 timevar_start (TV_PHASE_OPT_GEN);
3305 materialize_cgraph ();
3306 if (!flag_ltrans)
3307 lto_promote_statics_nonwpa ();
3309 /* Let the middle end know that we have read and merged all of
3310 the input files. */
3311 symtab->compile ();
3313 timevar_stop (TV_PHASE_OPT_GEN);
3315 /* FIXME lto, if the processes spawned by WPA fail, we miss
3316 the chance to print WPA's report, so WPA will call
3317 print_lto_report before launching LTRANS. If LTRANS was
3318 launched directly by the driver we would not need to do
3319 this. */
3320 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3321 print_lto_report_1 ();
3325 /* Here we make LTO pretend to be a parser. */
3326 timevar_start (TV_PHASE_PARSING);
3327 timevar_push (TV_PARSE_GLOBAL);
3330 #include "gt-lto-lto.h"