* gcc-interface/lang-specs.h (TARGET_VXWORKS_RTP): Simplify and add
[official-gcc.git] / gcc / lto / lto.c
blob169b025eda5fb5a6f6b40e572bca82a98423e4d0
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;
416 /* An already processed type. */
417 if (TYPE_CANONICAL (type))
419 type = TYPE_CANONICAL (type);
420 v = gimple_canonical_type_hash (type);
422 else
424 /* Canonical types should not be able to form SCCs by design, this
425 recursion is just because we do not register canonical types in
426 optimal order. To avoid quadratic behavior also register the
427 type here. */
428 v = hash_canonical_type (type);
429 gimple_register_canonical_type_1 (type, v);
431 hstate.add_int (v);
434 /* Returns the hash for a canonical type P. */
436 static hashval_t
437 gimple_canonical_type_hash (const void *p)
439 num_canonical_type_hash_queries++;
440 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
441 gcc_assert (slot != NULL);
442 return *slot;
447 /* Returns nonzero if P1 and P2 are equal. */
449 static int
450 gimple_canonical_type_eq (const void *p1, const void *p2)
452 const_tree t1 = (const_tree) p1;
453 const_tree t2 = (const_tree) p2;
454 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
455 CONST_CAST_TREE (t2));
458 /* Main worker for gimple_register_canonical_type. */
460 static void
461 gimple_register_canonical_type_1 (tree t, hashval_t hash)
463 void **slot;
465 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
467 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
468 if (*slot)
470 tree new_type = (tree)(*slot);
471 gcc_checking_assert (new_type != t);
472 TYPE_CANONICAL (t) = new_type;
474 else
476 TYPE_CANONICAL (t) = t;
477 *slot = (void *) t;
478 /* Cache the just computed hash value. */
479 num_canonical_type_hash_entries++;
480 bool existed_p = canonical_type_hash_cache->put (t, hash);
481 gcc_assert (!existed_p);
485 /* Register type T in the global type table gimple_types and set
486 TYPE_CANONICAL of T accordingly.
487 This is used by LTO to merge structurally equivalent types for
488 type-based aliasing purposes across different TUs and languages.
490 ??? This merging does not exactly match how the tree.c middle-end
491 functions will assign TYPE_CANONICAL when new types are created
492 during optimization (which at least happens for pointer and array
493 types). */
495 static void
496 gimple_register_canonical_type (tree t)
498 if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t))
499 return;
501 gimple_register_canonical_type_1 (t, hash_canonical_type (t));
504 /* Re-compute TYPE_CANONICAL for NODE and related types. */
506 static void
507 lto_register_canonical_types (tree node, bool first_p)
509 if (!node
510 || !TYPE_P (node))
511 return;
513 if (first_p)
514 TYPE_CANONICAL (node) = NULL_TREE;
516 if (POINTER_TYPE_P (node)
517 || TREE_CODE (node) == COMPLEX_TYPE
518 || TREE_CODE (node) == ARRAY_TYPE)
519 lto_register_canonical_types (TREE_TYPE (node), first_p);
521 if (!first_p)
522 gimple_register_canonical_type (node);
526 /* Remember trees that contains references to declarations. */
527 static GTY(()) vec <tree, va_gc> *tree_with_vars;
529 #define CHECK_VAR(tt) \
530 do \
532 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
533 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
534 return true; \
535 } while (0)
537 #define CHECK_NO_VAR(tt) \
538 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
540 /* Check presence of pointers to decls in fields of a tree_typed T. */
542 static inline bool
543 mentions_vars_p_typed (tree t)
545 CHECK_NO_VAR (TREE_TYPE (t));
546 return false;
549 /* Check presence of pointers to decls in fields of a tree_common T. */
551 static inline bool
552 mentions_vars_p_common (tree t)
554 if (mentions_vars_p_typed (t))
555 return true;
556 CHECK_NO_VAR (TREE_CHAIN (t));
557 return false;
560 /* Check presence of pointers to decls in fields of a decl_minimal T. */
562 static inline bool
563 mentions_vars_p_decl_minimal (tree t)
565 if (mentions_vars_p_common (t))
566 return true;
567 CHECK_NO_VAR (DECL_NAME (t));
568 CHECK_VAR (DECL_CONTEXT (t));
569 return false;
572 /* Check presence of pointers to decls in fields of a decl_common T. */
574 static inline bool
575 mentions_vars_p_decl_common (tree t)
577 if (mentions_vars_p_decl_minimal (t))
578 return true;
579 CHECK_VAR (DECL_SIZE (t));
580 CHECK_VAR (DECL_SIZE_UNIT (t));
581 CHECK_VAR (DECL_INITIAL (t));
582 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
583 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
584 return false;
587 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
589 static inline bool
590 mentions_vars_p_decl_with_vis (tree t)
592 if (mentions_vars_p_decl_common (t))
593 return true;
595 /* Accessor macro has side-effects, use field-name here. */
596 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
597 return false;
600 /* Check presence of pointers to decls in fields of a decl_non_common T. */
602 static inline bool
603 mentions_vars_p_decl_non_common (tree t)
605 if (mentions_vars_p_decl_with_vis (t))
606 return true;
607 CHECK_NO_VAR (DECL_RESULT_FLD (t));
608 return false;
611 /* Check presence of pointers to decls in fields of a decl_non_common T. */
613 static bool
614 mentions_vars_p_function (tree t)
616 if (mentions_vars_p_decl_non_common (t))
617 return true;
618 CHECK_NO_VAR (DECL_ARGUMENTS (t));
619 CHECK_NO_VAR (DECL_VINDEX (t));
620 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
621 return false;
624 /* Check presence of pointers to decls in fields of a field_decl T. */
626 static bool
627 mentions_vars_p_field_decl (tree t)
629 if (mentions_vars_p_decl_common (t))
630 return true;
631 CHECK_VAR (DECL_FIELD_OFFSET (t));
632 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
633 CHECK_NO_VAR (DECL_QUALIFIER (t));
634 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
635 CHECK_NO_VAR (DECL_FCONTEXT (t));
636 return false;
639 /* Check presence of pointers to decls in fields of a type T. */
641 static bool
642 mentions_vars_p_type (tree t)
644 if (mentions_vars_p_common (t))
645 return true;
646 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
647 CHECK_VAR (TYPE_SIZE (t));
648 CHECK_VAR (TYPE_SIZE_UNIT (t));
649 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
650 CHECK_NO_VAR (TYPE_NAME (t));
652 CHECK_VAR (TYPE_MINVAL (t));
653 CHECK_VAR (TYPE_MAXVAL (t));
655 /* Accessor is for derived node types only. */
656 CHECK_NO_VAR (t->type_non_common.binfo);
658 CHECK_VAR (TYPE_CONTEXT (t));
659 CHECK_NO_VAR (TYPE_CANONICAL (t));
660 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
661 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
662 return false;
665 /* Check presence of pointers to decls in fields of a BINFO T. */
667 static bool
668 mentions_vars_p_binfo (tree t)
670 unsigned HOST_WIDE_INT i, n;
672 if (mentions_vars_p_common (t))
673 return true;
674 CHECK_VAR (BINFO_VTABLE (t));
675 CHECK_NO_VAR (BINFO_OFFSET (t));
676 CHECK_NO_VAR (BINFO_VIRTUALS (t));
677 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
678 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
679 for (i = 0; i < n; i++)
680 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
681 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
682 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
683 n = BINFO_N_BASE_BINFOS (t);
684 for (i = 0; i < n; i++)
685 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
686 return false;
689 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
691 static bool
692 mentions_vars_p_constructor (tree t)
694 unsigned HOST_WIDE_INT idx;
695 constructor_elt *ce;
697 if (mentions_vars_p_typed (t))
698 return true;
700 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
702 CHECK_NO_VAR (ce->index);
703 CHECK_VAR (ce->value);
705 return false;
708 /* Check presence of pointers to decls in fields of an expression tree T. */
710 static bool
711 mentions_vars_p_expr (tree t)
713 int i;
714 if (mentions_vars_p_typed (t))
715 return true;
716 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
717 CHECK_VAR (TREE_OPERAND (t, i));
718 return false;
721 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
723 static bool
724 mentions_vars_p_omp_clause (tree t)
726 int i;
727 if (mentions_vars_p_common (t))
728 return true;
729 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
730 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
731 return false;
734 /* Check presence of pointers to decls that needs later fixup in T. */
736 static bool
737 mentions_vars_p (tree t)
739 switch (TREE_CODE (t))
741 case IDENTIFIER_NODE:
742 break;
744 case TREE_LIST:
745 CHECK_VAR (TREE_VALUE (t));
746 CHECK_VAR (TREE_PURPOSE (t));
747 CHECK_NO_VAR (TREE_CHAIN (t));
748 break;
750 case FIELD_DECL:
751 return mentions_vars_p_field_decl (t);
753 case LABEL_DECL:
754 case CONST_DECL:
755 case PARM_DECL:
756 case RESULT_DECL:
757 case IMPORTED_DECL:
758 case NAMESPACE_DECL:
759 case NAMELIST_DECL:
760 return mentions_vars_p_decl_common (t);
762 case VAR_DECL:
763 return mentions_vars_p_decl_with_vis (t);
765 case TYPE_DECL:
766 return mentions_vars_p_decl_non_common (t);
768 case FUNCTION_DECL:
769 return mentions_vars_p_function (t);
771 case TREE_BINFO:
772 return mentions_vars_p_binfo (t);
774 case PLACEHOLDER_EXPR:
775 return mentions_vars_p_common (t);
777 case BLOCK:
778 case TRANSLATION_UNIT_DECL:
779 case OPTIMIZATION_NODE:
780 case TARGET_OPTION_NODE:
781 break;
783 case CONSTRUCTOR:
784 return mentions_vars_p_constructor (t);
786 case OMP_CLAUSE:
787 return mentions_vars_p_omp_clause (t);
789 default:
790 if (TYPE_P (t))
792 if (mentions_vars_p_type (t))
793 return true;
795 else if (EXPR_P (t))
797 if (mentions_vars_p_expr (t))
798 return true;
800 else if (CONSTANT_CLASS_P (t))
801 CHECK_NO_VAR (TREE_TYPE (t));
802 else
803 gcc_unreachable ();
805 return false;
809 /* Return the resolution for the decl with index INDEX from DATA_IN. */
811 static enum ld_plugin_symbol_resolution
812 get_resolution (struct data_in *data_in, unsigned index)
814 if (data_in->globals_resolution.exists ())
816 ld_plugin_symbol_resolution_t ret;
817 /* We can have references to not emitted functions in
818 DECL_FUNCTION_PERSONALITY at least. So we can and have
819 to indeed return LDPR_UNKNOWN in some cases. */
820 if (data_in->globals_resolution.length () <= index)
821 return LDPR_UNKNOWN;
822 ret = data_in->globals_resolution[index];
823 return ret;
825 else
826 /* Delay resolution finding until decl merging. */
827 return LDPR_UNKNOWN;
830 /* We need to record resolutions until symbol table is read. */
831 static void
832 register_resolution (struct lto_file_decl_data *file_data, tree decl,
833 enum ld_plugin_symbol_resolution resolution)
835 if (resolution == LDPR_UNKNOWN)
836 return;
837 if (!file_data->resolution_map)
838 file_data->resolution_map
839 = new hash_map<tree, ld_plugin_symbol_resolution>;
840 file_data->resolution_map->put (decl, resolution);
843 /* Register DECL with the global symbol table and change its
844 name if necessary to avoid name clashes for static globals across
845 different files. */
847 static void
848 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
849 unsigned ix)
851 tree context;
853 /* Variable has file scope, not local. */
854 if (!TREE_PUBLIC (decl)
855 && !((context = decl_function_context (decl))
856 && auto_var_in_fn_p (decl, context)))
857 rest_of_decl_compilation (decl, 1, 0);
859 /* If this variable has already been declared, queue the
860 declaration for merging. */
861 if (TREE_PUBLIC (decl))
862 register_resolution (data_in->file_data,
863 decl, get_resolution (data_in, ix));
867 /* Register DECL with the global symbol table and change its
868 name if necessary to avoid name clashes for static globals across
869 different files. DATA_IN contains descriptors and tables for the
870 file being read. */
872 static void
873 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
874 unsigned ix)
876 /* If this variable has already been declared, queue the
877 declaration for merging. */
878 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
879 register_resolution (data_in->file_data,
880 decl, get_resolution (data_in, ix));
884 /* For the type T re-materialize it in the type variant list and
885 the pointer/reference-to chains. */
887 static void
888 lto_fixup_prevailing_type (tree t)
890 /* The following re-creates proper variant lists while fixing up
891 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
892 variant list state before fixup is broken. */
894 /* If we are not our own variant leader link us into our new leaders
895 variant list. */
896 if (TYPE_MAIN_VARIANT (t) != t)
898 tree mv = TYPE_MAIN_VARIANT (t);
899 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
900 TYPE_NEXT_VARIANT (mv) = t;
903 /* The following reconstructs the pointer chains
904 of the new pointed-to type if we are a main variant. We do
905 not stream those so they are broken before fixup. */
906 if (TREE_CODE (t) == POINTER_TYPE
907 && TYPE_MAIN_VARIANT (t) == t)
909 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
910 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
912 else if (TREE_CODE (t) == REFERENCE_TYPE
913 && TYPE_MAIN_VARIANT (t) == t)
915 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
916 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
921 /* We keep prevailing tree SCCs in a hashtable with manual collision
922 handling (in case all hashes compare the same) and keep the colliding
923 entries in the tree_scc->next chain. */
925 struct tree_scc
927 tree_scc *next;
928 /* Hash of the whole SCC. */
929 hashval_t hash;
930 /* Number of trees in the SCC. */
931 unsigned len;
932 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
933 which share the same individual tree hash). */
934 unsigned entry_len;
935 /* The members of the SCC.
936 We only need to remember the first entry node candidate for prevailing
937 SCCs (but of course have access to all entries for SCCs we are
938 processing).
939 ??? For prevailing SCCs we really only need hash and the first
940 entry candidate, but that's too awkward to implement. */
941 tree entries[1];
944 struct tree_scc_hasher : typed_noop_remove <tree_scc>
946 typedef tree_scc *value_type;
947 typedef tree_scc *compare_type;
948 static inline hashval_t hash (const tree_scc *);
949 static inline bool equal (const tree_scc *, const tree_scc *);
952 hashval_t
953 tree_scc_hasher::hash (const tree_scc *scc)
955 return scc->hash;
958 bool
959 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
961 if (scc1->hash != scc2->hash
962 || scc1->len != scc2->len
963 || scc1->entry_len != scc2->entry_len)
964 return false;
965 return true;
968 static hash_table<tree_scc_hasher> *tree_scc_hash;
969 static struct obstack tree_scc_hash_obstack;
971 static unsigned long num_merged_types;
972 static unsigned long num_prevailing_types;
973 static unsigned long num_type_scc_trees;
974 static unsigned long total_scc_size;
975 static unsigned long num_sccs_read;
976 static unsigned long total_scc_size_merged;
977 static unsigned long num_sccs_merged;
978 static unsigned long num_scc_compares;
979 static unsigned long num_scc_compare_collisions;
982 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
983 recursing through in-SCC tree edges. Returns true if the SCCs entered
984 through T1 and T2 are equal and fills in *MAP with the pairs of
985 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
987 static bool
988 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
990 enum tree_code code;
992 /* Mark already visited nodes. */
993 TREE_ASM_WRITTEN (t2) = 1;
995 /* Push the pair onto map. */
996 (*map)[0] = t1;
997 (*map)[1] = t2;
998 *map = *map + 2;
1000 /* Compare value-fields. */
1001 #define compare_values(X) \
1002 do { \
1003 if (X(t1) != X(t2)) \
1004 return false; \
1005 } while (0)
1007 compare_values (TREE_CODE);
1008 code = TREE_CODE (t1);
1010 if (!TYPE_P (t1))
1012 compare_values (TREE_SIDE_EFFECTS);
1013 compare_values (TREE_CONSTANT);
1014 compare_values (TREE_READONLY);
1015 compare_values (TREE_PUBLIC);
1017 compare_values (TREE_ADDRESSABLE);
1018 compare_values (TREE_THIS_VOLATILE);
1019 if (DECL_P (t1))
1020 compare_values (DECL_UNSIGNED);
1021 else if (TYPE_P (t1))
1022 compare_values (TYPE_UNSIGNED);
1023 if (TYPE_P (t1))
1024 compare_values (TYPE_ARTIFICIAL);
1025 else
1026 compare_values (TREE_NO_WARNING);
1027 compare_values (TREE_NOTHROW);
1028 compare_values (TREE_STATIC);
1029 if (code != TREE_BINFO)
1030 compare_values (TREE_PRIVATE);
1031 compare_values (TREE_PROTECTED);
1032 compare_values (TREE_DEPRECATED);
1033 if (TYPE_P (t1))
1035 compare_values (TYPE_SATURATING);
1036 compare_values (TYPE_ADDR_SPACE);
1038 else if (code == SSA_NAME)
1039 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1041 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1043 if (!wi::eq_p (t1, t2))
1044 return false;
1047 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1049 /* ??? No suitable compare routine available. */
1050 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1051 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1052 if (r1.cl != r2.cl
1053 || r1.decimal != r2.decimal
1054 || r1.sign != r2.sign
1055 || r1.signalling != r2.signalling
1056 || r1.canonical != r2.canonical
1057 || r1.uexp != r2.uexp)
1058 return false;
1059 for (unsigned i = 0; i < SIGSZ; ++i)
1060 if (r1.sig[i] != r2.sig[i])
1061 return false;
1064 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1065 if (!fixed_compare (EQ_EXPR,
1066 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1067 return false;
1070 /* We don't want to compare locations, so there is nothing do compare
1071 for TS_DECL_MINIMAL. */
1073 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1075 compare_values (DECL_MODE);
1076 compare_values (DECL_NONLOCAL);
1077 compare_values (DECL_VIRTUAL_P);
1078 compare_values (DECL_IGNORED_P);
1079 compare_values (DECL_ABSTRACT_P);
1080 compare_values (DECL_ARTIFICIAL);
1081 compare_values (DECL_USER_ALIGN);
1082 compare_values (DECL_PRESERVE_P);
1083 compare_values (DECL_EXTERNAL);
1084 compare_values (DECL_GIMPLE_REG_P);
1085 compare_values (DECL_ALIGN);
1086 if (code == LABEL_DECL)
1088 compare_values (EH_LANDING_PAD_NR);
1089 compare_values (LABEL_DECL_UID);
1091 else if (code == FIELD_DECL)
1093 compare_values (DECL_PACKED);
1094 compare_values (DECL_NONADDRESSABLE_P);
1095 compare_values (DECL_OFFSET_ALIGN);
1097 else if (code == VAR_DECL)
1099 compare_values (DECL_HAS_DEBUG_EXPR_P);
1100 compare_values (DECL_NONLOCAL_FRAME);
1102 if (code == RESULT_DECL
1103 || code == PARM_DECL
1104 || code == VAR_DECL)
1106 compare_values (DECL_BY_REFERENCE);
1107 if (code == VAR_DECL
1108 || code == PARM_DECL)
1109 compare_values (DECL_HAS_VALUE_EXPR_P);
1113 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1114 compare_values (DECL_REGISTER);
1116 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1118 compare_values (DECL_COMMON);
1119 compare_values (DECL_DLLIMPORT_P);
1120 compare_values (DECL_WEAK);
1121 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1122 compare_values (DECL_COMDAT);
1123 compare_values (DECL_VISIBILITY);
1124 compare_values (DECL_VISIBILITY_SPECIFIED);
1125 if (code == VAR_DECL)
1127 compare_values (DECL_HARD_REGISTER);
1128 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1129 compare_values (DECL_IN_CONSTANT_POOL);
1133 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1135 compare_values (DECL_BUILT_IN_CLASS);
1136 compare_values (DECL_STATIC_CONSTRUCTOR);
1137 compare_values (DECL_STATIC_DESTRUCTOR);
1138 compare_values (DECL_UNINLINABLE);
1139 compare_values (DECL_POSSIBLY_INLINED);
1140 compare_values (DECL_IS_NOVOPS);
1141 compare_values (DECL_IS_RETURNS_TWICE);
1142 compare_values (DECL_IS_MALLOC);
1143 compare_values (DECL_IS_OPERATOR_NEW);
1144 compare_values (DECL_DECLARED_INLINE_P);
1145 compare_values (DECL_STATIC_CHAIN);
1146 compare_values (DECL_NO_INLINE_WARNING_P);
1147 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1148 compare_values (DECL_NO_LIMIT_STACK);
1149 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1150 compare_values (DECL_PURE_P);
1151 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1152 compare_values (DECL_FINAL_P);
1153 compare_values (DECL_CXX_CONSTRUCTOR_P);
1154 compare_values (DECL_CXX_DESTRUCTOR_P);
1155 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1156 compare_values (DECL_FUNCTION_CODE);
1159 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1161 compare_values (TYPE_MODE);
1162 compare_values (TYPE_STRING_FLAG);
1163 compare_values (TYPE_NO_FORCE_BLK);
1164 compare_values (TYPE_NEEDS_CONSTRUCTING);
1165 if (RECORD_OR_UNION_TYPE_P (t1))
1167 compare_values (TYPE_TRANSPARENT_AGGR);
1168 compare_values (TYPE_FINAL_P);
1170 else if (code == ARRAY_TYPE)
1171 compare_values (TYPE_NONALIASED_COMPONENT);
1172 compare_values (TYPE_PACKED);
1173 compare_values (TYPE_RESTRICT);
1174 compare_values (TYPE_USER_ALIGN);
1175 compare_values (TYPE_READONLY);
1176 compare_values (TYPE_PRECISION);
1177 compare_values (TYPE_ALIGN);
1178 compare_values (TYPE_ALIAS_SET);
1181 /* We don't want to compare locations, so there is nothing do compare
1182 for TS_EXP. */
1184 /* BLOCKs are function local and we don't merge anything there, so
1185 simply refuse to merge. */
1186 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1187 return false;
1189 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1190 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1191 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1192 return false;
1194 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1195 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1196 return false;
1198 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1199 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1200 sizeof (struct cl_optimization)) != 0)
1201 return false;
1203 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1204 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1205 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1206 return false;
1208 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1209 compare_values (CONSTRUCTOR_NELTS);
1211 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1212 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1213 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1214 IDENTIFIER_LENGTH (t1)) != 0)
1215 return false;
1217 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1218 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1219 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1220 TREE_STRING_LENGTH (t1)) != 0)
1221 return false;
1223 if (code == OMP_CLAUSE)
1225 compare_values (OMP_CLAUSE_CODE);
1226 switch (OMP_CLAUSE_CODE (t1))
1228 case OMP_CLAUSE_DEFAULT:
1229 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1230 break;
1231 case OMP_CLAUSE_SCHEDULE:
1232 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1233 break;
1234 case OMP_CLAUSE_DEPEND:
1235 compare_values (OMP_CLAUSE_DEPEND_KIND);
1236 break;
1237 case OMP_CLAUSE_MAP:
1238 compare_values (OMP_CLAUSE_MAP_KIND);
1239 break;
1240 case OMP_CLAUSE_PROC_BIND:
1241 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1242 break;
1243 case OMP_CLAUSE_REDUCTION:
1244 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1245 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1246 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1247 break;
1248 default:
1249 break;
1253 #undef compare_values
1256 /* Compare pointer fields. */
1258 /* Recurse. Search & Replaced from DFS_write_tree_body.
1259 Folding the early checks into the compare_tree_edges recursion
1260 macro makes debugging way quicker as you are able to break on
1261 compare_tree_sccs_1 and simply finish until a call returns false
1262 to spot the SCC members with the difference. */
1263 #define compare_tree_edges(E1, E2) \
1264 do { \
1265 tree t1_ = (E1), t2_ = (E2); \
1266 if (t1_ != t2_ \
1267 && (!t1_ || !t2_ \
1268 || !TREE_VISITED (t2_) \
1269 || (!TREE_ASM_WRITTEN (t2_) \
1270 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1271 return false; \
1272 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1273 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1274 } while (0)
1276 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1278 if (code != IDENTIFIER_NODE)
1279 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1282 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1284 unsigned i;
1285 /* Note that the number of elements for EXPR has already been emitted
1286 in EXPR's header (see streamer_write_tree_header). */
1287 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1288 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1291 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1293 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1294 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1297 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1299 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1300 /* ??? Global decls from different TUs have non-matching
1301 TRANSLATION_UNIT_DECLs. Only consider a small set of
1302 decls equivalent, we should not end up merging others. */
1303 if ((code == TYPE_DECL
1304 || code == NAMESPACE_DECL
1305 || code == IMPORTED_DECL
1306 || code == CONST_DECL
1307 || (VAR_OR_FUNCTION_DECL_P (t1)
1308 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1309 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1311 else
1312 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1315 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1317 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1318 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1319 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1320 if ((code == VAR_DECL
1321 || code == PARM_DECL)
1322 && DECL_HAS_VALUE_EXPR_P (t1))
1323 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1324 if (code == VAR_DECL
1325 && DECL_HAS_DEBUG_EXPR_P (t1))
1326 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1327 /* LTO specific edges. */
1328 if (code != FUNCTION_DECL
1329 && code != TRANSLATION_UNIT_DECL)
1330 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1333 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1335 if (code == FUNCTION_DECL)
1337 tree a1, a2;
1338 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1339 a1 || a2;
1340 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1341 compare_tree_edges (a1, a2);
1342 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1344 else if (code == TYPE_DECL)
1345 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1348 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1350 /* Make sure we don't inadvertently set the assembler name. */
1351 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1352 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1353 DECL_ASSEMBLER_NAME (t2));
1356 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1358 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1359 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1360 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1361 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1362 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1363 DECL_FIELD_BIT_OFFSET (t2));
1364 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1367 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1369 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1370 DECL_FUNCTION_PERSONALITY (t2));
1371 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1372 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1373 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1374 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1375 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1378 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1380 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1381 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1382 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1383 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1384 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1385 reconstructed during fixup. */
1386 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1387 during fixup. */
1388 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1389 /* ??? Global types from different TUs have non-matching
1390 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1391 equal. */
1392 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1394 else
1395 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1396 /* TYPE_CANONICAL is re-computed during type merging, so do not
1397 compare it here. */
1398 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1401 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1403 if (code == ENUMERAL_TYPE)
1404 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1405 else if (code == ARRAY_TYPE)
1406 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1407 else if (RECORD_OR_UNION_TYPE_P (t1))
1409 tree f1, f2;
1410 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1411 f1 || f2;
1412 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1413 compare_tree_edges (f1, f2);
1414 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1416 else if (code == FUNCTION_TYPE
1417 || code == METHOD_TYPE)
1418 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1419 if (!POINTER_TYPE_P (t1))
1420 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1421 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1424 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1426 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1427 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1428 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1431 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1432 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1433 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1435 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1437 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1438 compare_tree_edges (TREE_OPERAND (t1, i),
1439 TREE_OPERAND (t2, i));
1441 /* BLOCKs are function local and we don't merge anything there. */
1442 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1443 return false;
1446 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1448 unsigned i;
1449 tree t;
1450 /* Lengths have already been compared above. */
1451 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1452 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1453 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1454 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1455 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1456 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1457 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1458 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1459 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1462 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1464 unsigned i;
1465 tree index, value;
1466 /* Lengths have already been compared above. */
1467 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1469 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1470 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1474 if (code == OMP_CLAUSE)
1476 int i;
1478 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1479 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1480 OMP_CLAUSE_OPERAND (t2, i));
1481 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1484 #undef compare_tree_edges
1486 return true;
1489 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1490 out MAP if they are equal. */
1492 static bool
1493 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1494 tree *map)
1496 /* Assume SCC entry hashes are sorted after their cardinality. Which
1497 means we can simply take the first n-tuple of equal hashes
1498 (which is recorded as entry_len) and do n SCC entry candidate
1499 comparisons. */
1500 for (unsigned i = 0; i < pscc->entry_len; ++i)
1502 tree *mapp = map;
1503 num_scc_compare_collisions++;
1504 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1506 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1507 on the scc as all trees will be freed. */
1508 return true;
1510 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1511 the SCC prevails. */
1512 for (unsigned j = 0; j < scc->len; ++j)
1513 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1516 return false;
1519 /* QSort sort function to sort a map of two pointers after the 2nd
1520 pointer. */
1522 static int
1523 cmp_tree (const void *p1_, const void *p2_)
1525 tree *p1 = (tree *)(const_cast<void *>(p1_));
1526 tree *p2 = (tree *)(const_cast<void *>(p2_));
1527 if (p1[1] == p2[1])
1528 return 0;
1529 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1532 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1533 hash value SCC_HASH with an already recorded SCC. Return true if
1534 that was successful, otherwise return false. */
1536 static bool
1537 unify_scc (struct data_in *data_in, unsigned from,
1538 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1540 bool unified_p = false;
1541 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1542 tree_scc *scc
1543 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1544 scc->next = NULL;
1545 scc->hash = scc_hash;
1546 scc->len = len;
1547 scc->entry_len = scc_entry_len;
1548 for (unsigned i = 0; i < len; ++i)
1550 tree t = streamer_tree_cache_get_tree (cache, from + i);
1551 scc->entries[i] = t;
1552 /* Do not merge SCCs with local entities inside them. Also do
1553 not merge TRANSLATION_UNIT_DECLs. */
1554 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1555 || (VAR_OR_FUNCTION_DECL_P (t)
1556 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1557 || TREE_CODE (t) == LABEL_DECL)
1559 /* Avoid doing any work for these cases and do not worry to
1560 record the SCCs for further merging. */
1561 return false;
1565 /* Look for the list of candidate SCCs to compare against. */
1566 tree_scc **slot;
1567 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1568 if (*slot)
1570 /* Try unifying against each candidate. */
1571 num_scc_compares++;
1573 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1574 outside of the scc when following tree edges. Make sure
1575 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1576 to track whether we visited the SCC member during the compare.
1577 We cannot use TREE_VISITED on the pscc members as the extended
1578 scc and pscc can overlap. */
1579 for (unsigned i = 0; i < scc->len; ++i)
1581 TREE_VISITED (scc->entries[i]) = 1;
1582 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1585 tree *map = XALLOCAVEC (tree, 2 * len);
1586 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1588 if (!compare_tree_sccs (pscc, scc, map))
1589 continue;
1591 /* Found an equal SCC. */
1592 unified_p = true;
1593 num_scc_compare_collisions--;
1594 num_sccs_merged++;
1595 total_scc_size_merged += len;
1597 #ifdef ENABLE_CHECKING
1598 for (unsigned i = 0; i < len; ++i)
1600 tree t = map[2*i+1];
1601 enum tree_code code = TREE_CODE (t);
1602 /* IDENTIFIER_NODEs should be singletons and are merged by the
1603 streamer. The others should be singletons, too, and we
1604 should not merge them in any way. */
1605 gcc_assert (code != TRANSLATION_UNIT_DECL
1606 && code != IDENTIFIER_NODE
1607 && !streamer_handle_as_builtin_p (t));
1609 #endif
1611 /* Fixup the streamer cache with the prevailing nodes according
1612 to the tree node mapping computed by compare_tree_sccs. */
1613 if (len == 1)
1614 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1615 else
1617 tree *map2 = XALLOCAVEC (tree, 2 * len);
1618 for (unsigned i = 0; i < len; ++i)
1620 map2[i*2] = (tree)(uintptr_t)(from + i);
1621 map2[i*2+1] = scc->entries[i];
1623 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1624 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1625 for (unsigned i = 0; i < len; ++i)
1626 streamer_tree_cache_replace_tree (cache, map[2*i],
1627 (uintptr_t)map2[2*i]);
1630 /* Free the tree nodes from the read SCC. */
1631 data_in->location_cache.revert_location_cache ();
1632 for (unsigned i = 0; i < len; ++i)
1634 enum tree_code code;
1635 if (TYPE_P (scc->entries[i]))
1636 num_merged_types++;
1637 code = TREE_CODE (scc->entries[i]);
1638 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1639 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1640 ggc_free (scc->entries[i]);
1643 break;
1646 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1647 if (!unified_p)
1648 for (unsigned i = 0; i < scc->len; ++i)
1649 TREE_VISITED (scc->entries[i]) = 0;
1652 /* If we didn't unify it to any candidate duplicate the relevant
1653 pieces to permanent storage and link it into the chain. */
1654 if (!unified_p)
1656 tree_scc *pscc
1657 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1658 memcpy (pscc, scc, sizeof (tree_scc));
1659 pscc->next = (*slot);
1660 *slot = pscc;
1662 return unified_p;
1666 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1667 RESOLUTIONS is the set of symbols picked by the linker (read from the
1668 resolution file when the linker plugin is being used). */
1670 static void
1671 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1672 vec<ld_plugin_symbol_resolution_t> resolutions)
1674 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1675 const int decl_offset = sizeof (struct lto_decl_header);
1676 const int main_offset = decl_offset + header->decl_state_size;
1677 const int string_offset = main_offset + header->main_size;
1678 struct data_in *data_in;
1679 unsigned int i;
1680 const uint32_t *data_ptr, *data_end;
1681 uint32_t num_decl_states;
1683 lto_input_block ib_main ((const char *) data + main_offset,
1684 header->main_size, decl_data->mode_table);
1686 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1687 header->string_size, resolutions);
1689 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1690 internal types that should not be merged. */
1692 /* Read the global declarations and types. */
1693 while (ib_main.p < ib_main.len)
1695 tree t;
1696 unsigned from = data_in->reader_cache->nodes.length ();
1697 /* Read and uniquify SCCs as in the input stream. */
1698 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1699 if (tag == LTO_tree_scc)
1701 unsigned len_;
1702 unsigned scc_entry_len;
1703 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1704 &scc_entry_len);
1705 unsigned len = data_in->reader_cache->nodes.length () - from;
1706 gcc_assert (len == len_);
1708 total_scc_size += len;
1709 num_sccs_read++;
1711 /* We have the special case of size-1 SCCs that are pre-merged
1712 by means of identifier and string sharing for example.
1713 ??? Maybe we should avoid streaming those as SCCs. */
1714 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1715 from);
1716 if (len == 1
1717 && (TREE_CODE (first) == IDENTIFIER_NODE
1718 || TREE_CODE (first) == INTEGER_CST
1719 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1720 || streamer_handle_as_builtin_p (first)))
1721 continue;
1723 /* Try to unify the SCC with already existing ones. */
1724 if (!flag_ltrans
1725 && unify_scc (data_in, from,
1726 len, scc_entry_len, scc_hash))
1727 continue;
1729 /* Tree merging failed, mark entries in location cache as
1730 permanent. */
1731 data_in->location_cache.accept_location_cache ();
1733 bool seen_type = false;
1734 for (unsigned i = 0; i < len; ++i)
1736 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1737 from + i);
1738 /* Reconstruct the type variant and pointer-to/reference-to
1739 chains. */
1740 if (TYPE_P (t))
1742 seen_type = true;
1743 num_prevailing_types++;
1744 lto_fixup_prevailing_type (t);
1746 /* Compute the canonical type of all types.
1747 ??? Should be able to assert that !TYPE_CANONICAL. */
1748 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1750 gimple_register_canonical_type (t);
1751 if (odr_type_p (t))
1752 register_odr_type (t);
1754 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1755 type which is also member of this SCC. */
1756 if (TREE_CODE (t) == INTEGER_CST
1757 && !TREE_OVERFLOW (t))
1758 cache_integer_cst (t);
1759 /* Register TYPE_DECLs with the debuginfo machinery. */
1760 if (!flag_wpa
1761 && TREE_CODE (t) == TYPE_DECL)
1763 /* Dwarf2out needs location information.
1764 TODO: Moving this out of the streamer loop may noticealy
1765 improve ltrans linemap memory use. */
1766 data_in->location_cache.apply_location_cache ();
1767 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1769 if (!flag_ltrans)
1771 /* Register variables and functions with the
1772 symbol table. */
1773 if (TREE_CODE (t) == VAR_DECL)
1774 lto_register_var_decl_in_symtab (data_in, t, from + i);
1775 else if (TREE_CODE (t) == FUNCTION_DECL
1776 && !DECL_BUILT_IN (t))
1777 lto_register_function_decl_in_symtab (data_in, t, from + i);
1778 /* Scan the tree for references to global functions or
1779 variables and record those for later fixup. */
1780 if (mentions_vars_p (t))
1781 vec_safe_push (tree_with_vars, t);
1784 if (seen_type)
1785 num_type_scc_trees += len;
1787 else
1789 /* Pickle stray references. */
1790 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1791 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1794 data_in->location_cache.apply_location_cache ();
1796 /* Read in lto_in_decl_state objects. */
1797 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1798 data_end =
1799 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1800 num_decl_states = *data_ptr++;
1802 gcc_assert (num_decl_states > 0);
1803 decl_data->global_decl_state = lto_new_in_decl_state ();
1804 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1805 decl_data->global_decl_state);
1807 /* Read in per-function decl states and enter them in hash table. */
1808 decl_data->function_decl_states =
1809 hash_table<decl_state_hasher>::create_ggc (37);
1811 for (i = 1; i < num_decl_states; i++)
1813 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1815 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1816 lto_in_decl_state **slot
1817 = decl_data->function_decl_states->find_slot (state, INSERT);
1818 gcc_assert (*slot == NULL);
1819 *slot = state;
1822 if (data_ptr != data_end)
1823 internal_error ("bytecode stream: garbage at the end of symbols section");
1825 /* Set the current decl state to be the global state. */
1826 decl_data->current_decl_state = decl_data->global_decl_state;
1828 lto_data_in_delete (data_in);
1831 /* Custom version of strtoll, which is not portable. */
1833 static int64_t
1834 lto_parse_hex (const char *p)
1836 int64_t ret = 0;
1838 for (; *p != '\0'; ++p)
1840 char c = *p;
1841 unsigned char part;
1842 ret <<= 4;
1843 if (c >= '0' && c <= '9')
1844 part = c - '0';
1845 else if (c >= 'a' && c <= 'f')
1846 part = c - 'a' + 10;
1847 else if (c >= 'A' && c <= 'F')
1848 part = c - 'A' + 10;
1849 else
1850 internal_error ("could not parse hex number");
1851 ret |= part;
1854 return ret;
1857 /* Read resolution for file named FILE_NAME. The resolution is read from
1858 RESOLUTION. */
1860 static void
1861 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1863 /* We require that objects in the resolution file are in the same
1864 order as the lto1 command line. */
1865 unsigned int name_len;
1866 char *obj_name;
1867 unsigned int num_symbols;
1868 unsigned int i;
1869 struct lto_file_decl_data *file_data;
1870 splay_tree_node nd = NULL;
1872 if (!resolution)
1873 return;
1875 name_len = strlen (file->filename);
1876 obj_name = XNEWVEC (char, name_len + 1);
1877 fscanf (resolution, " "); /* Read white space. */
1879 fread (obj_name, sizeof (char), name_len, resolution);
1880 obj_name[name_len] = '\0';
1881 if (filename_cmp (obj_name, file->filename) != 0)
1882 internal_error ("unexpected file name %s in linker resolution file. "
1883 "Expected %s", obj_name, file->filename);
1884 if (file->offset != 0)
1886 int t;
1887 char offset_p[17];
1888 int64_t offset;
1889 t = fscanf (resolution, "@0x%16s", offset_p);
1890 if (t != 1)
1891 internal_error ("could not parse file offset");
1892 offset = lto_parse_hex (offset_p);
1893 if (offset != file->offset)
1894 internal_error ("unexpected offset");
1897 free (obj_name);
1899 fscanf (resolution, "%u", &num_symbols);
1901 for (i = 0; i < num_symbols; i++)
1903 int t;
1904 unsigned index;
1905 unsigned HOST_WIDE_INT id;
1906 char r_str[27];
1907 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
1908 unsigned int j;
1909 unsigned int lto_resolution_str_len =
1910 sizeof (lto_resolution_str) / sizeof (char *);
1911 res_pair rp;
1913 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
1914 &index, &id, r_str);
1915 if (t != 3)
1916 internal_error ("invalid line in the resolution file");
1918 for (j = 0; j < lto_resolution_str_len; j++)
1920 if (strcmp (lto_resolution_str[j], r_str) == 0)
1922 r = (enum ld_plugin_symbol_resolution) j;
1923 break;
1926 if (j == lto_resolution_str_len)
1927 internal_error ("invalid resolution in the resolution file");
1929 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
1931 nd = lto_splay_tree_lookup (file_ids, id);
1932 if (nd == NULL)
1933 internal_error ("resolution sub id %wx not in object file", id);
1936 file_data = (struct lto_file_decl_data *)nd->value;
1937 /* The indexes are very sparse. To save memory save them in a compact
1938 format that is only unpacked later when the subfile is processed. */
1939 rp.res = r;
1940 rp.index = index;
1941 file_data->respairs.safe_push (rp);
1942 if (file_data->max_index < index)
1943 file_data->max_index = index;
1947 /* List of file_decl_datas */
1948 struct file_data_list
1950 struct lto_file_decl_data *first, *last;
1953 /* Is the name for a id'ed LTO section? */
1955 static int
1956 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
1958 const char *s;
1960 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
1961 return 0;
1962 s = strrchr (name, '.');
1963 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
1966 /* Create file_data of each sub file id */
1968 static int
1969 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
1970 struct file_data_list *list)
1972 struct lto_section_slot s_slot, *new_slot;
1973 unsigned HOST_WIDE_INT id;
1974 splay_tree_node nd;
1975 void **hash_slot;
1976 char *new_name;
1977 struct lto_file_decl_data *file_data;
1979 if (!lto_section_with_id (ls->name, &id))
1980 return 1;
1982 /* Find hash table of sub module id */
1983 nd = lto_splay_tree_lookup (file_ids, id);
1984 if (nd != NULL)
1986 file_data = (struct lto_file_decl_data *)nd->value;
1988 else
1990 file_data = ggc_alloc<lto_file_decl_data> ();
1991 memset(file_data, 0, sizeof (struct lto_file_decl_data));
1992 file_data->id = id;
1993 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
1994 lto_splay_tree_insert (file_ids, id, file_data);
1996 /* Maintain list in linker order */
1997 if (!list->first)
1998 list->first = file_data;
1999 if (list->last)
2000 list->last->next = file_data;
2001 list->last = file_data;
2004 /* Copy section into sub module hash table */
2005 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2006 s_slot.name = new_name;
2007 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2008 gcc_assert (*hash_slot == NULL);
2010 new_slot = XDUP (struct lto_section_slot, ls);
2011 new_slot->name = new_name;
2012 *hash_slot = new_slot;
2013 return 1;
2016 /* Read declarations and other initializations for a FILE_DATA. */
2018 static void
2019 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2021 const char *data;
2022 size_t len;
2023 vec<ld_plugin_symbol_resolution_t>
2024 resolutions = vNULL;
2025 int i;
2026 res_pair *rp;
2028 /* Create vector for fast access of resolution. We do this lazily
2029 to save memory. */
2030 resolutions.safe_grow_cleared (file_data->max_index + 1);
2031 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2032 resolutions[rp->index] = rp->res;
2033 file_data->respairs.release ();
2035 file_data->renaming_hash_table = lto_create_renaming_table ();
2036 file_data->file_name = file->filename;
2037 #ifdef ACCEL_COMPILER
2038 lto_input_mode_table (file_data);
2039 #else
2040 file_data->mode_table = lto_mode_identity_table;
2041 #endif
2042 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2043 if (data == NULL)
2045 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2046 return;
2048 /* Frees resolutions */
2049 lto_read_decls (file_data, data, resolutions);
2050 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2053 /* Finalize FILE_DATA in FILE and increase COUNT. */
2055 static int
2056 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2057 int *count)
2059 lto_file_finalize (file_data, file);
2060 if (symtab->dump_file)
2061 fprintf (symtab->dump_file,
2062 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2063 file_data->file_name, file_data->id);
2064 (*count)++;
2065 return 0;
2068 /* Generate a TREE representation for all types and external decls
2069 entities in FILE.
2071 Read all of the globals out of the file. Then read the cgraph
2072 and process the .o index into the cgraph nodes so that it can open
2073 the .o file to load the functions and ipa information. */
2075 static struct lto_file_decl_data *
2076 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2078 struct lto_file_decl_data *file_data = NULL;
2079 splay_tree file_ids;
2080 htab_t section_hash_table;
2081 struct lto_section_slot *section;
2082 struct file_data_list file_list;
2083 struct lto_section_list section_list;
2085 memset (&section_list, 0, sizeof (struct lto_section_list));
2086 section_hash_table = lto_obj_build_section_table (file, &section_list);
2088 /* Find all sub modules in the object and put their sections into new hash
2089 tables in a splay tree. */
2090 file_ids = lto_splay_tree_new ();
2091 memset (&file_list, 0, sizeof (struct file_data_list));
2092 for (section = section_list.first; section != NULL; section = section->next)
2093 create_subid_section_table (section, file_ids, &file_list);
2095 /* Add resolutions to file ids */
2096 lto_resolution_read (file_ids, resolution_file, file);
2098 /* Finalize each lto file for each submodule in the merged object */
2099 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2100 lto_create_files_from_ids (file, file_data, count);
2102 splay_tree_delete (file_ids);
2103 htab_delete (section_hash_table);
2105 return file_list.first;
2108 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2109 #define LTO_MMAP_IO 1
2110 #endif
2112 #if LTO_MMAP_IO
2113 /* Page size of machine is used for mmap and munmap calls. */
2114 static size_t page_mask;
2115 #endif
2117 /* Get the section data of length LEN from FILENAME starting at
2118 OFFSET. The data segment must be freed by the caller when the
2119 caller is finished. Returns NULL if all was not well. */
2121 static char *
2122 lto_read_section_data (struct lto_file_decl_data *file_data,
2123 intptr_t offset, size_t len)
2125 char *result;
2126 static int fd = -1;
2127 static char *fd_name;
2128 #if LTO_MMAP_IO
2129 intptr_t computed_len;
2130 intptr_t computed_offset;
2131 intptr_t diff;
2132 #endif
2134 /* Keep a single-entry file-descriptor cache. The last file we
2135 touched will get closed at exit.
2136 ??? Eventually we want to add a more sophisticated larger cache
2137 or rather fix function body streaming to not stream them in
2138 practically random order. */
2139 if (fd != -1
2140 && filename_cmp (fd_name, file_data->file_name) != 0)
2142 free (fd_name);
2143 close (fd);
2144 fd = -1;
2146 if (fd == -1)
2148 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2149 if (fd == -1)
2151 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2152 return NULL;
2154 fd_name = xstrdup (file_data->file_name);
2157 #if LTO_MMAP_IO
2158 if (!page_mask)
2160 size_t page_size = sysconf (_SC_PAGE_SIZE);
2161 page_mask = ~(page_size - 1);
2164 computed_offset = offset & page_mask;
2165 diff = offset - computed_offset;
2166 computed_len = len + diff;
2168 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2169 fd, computed_offset);
2170 if (result == MAP_FAILED)
2172 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2173 return NULL;
2176 return result + diff;
2177 #else
2178 result = (char *) xmalloc (len);
2179 if (lseek (fd, offset, SEEK_SET) != offset
2180 || read (fd, result, len) != (ssize_t) len)
2182 free (result);
2183 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2184 result = NULL;
2186 #ifdef __MINGW32__
2187 /* Native windows doesn't supports delayed unlink on opened file. So
2188 we close file here again. This produces higher I/O load, but at least
2189 it prevents to have dangling file handles preventing unlink. */
2190 free (fd_name);
2191 fd_name = NULL;
2192 close (fd);
2193 fd = -1;
2194 #endif
2195 return result;
2196 #endif
2200 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2201 NAME will be NULL unless the section type is for a function
2202 body. */
2204 static const char *
2205 get_section_data (struct lto_file_decl_data *file_data,
2206 enum lto_section_type section_type,
2207 const char *name,
2208 size_t *len)
2210 htab_t section_hash_table = file_data->section_hash_table;
2211 struct lto_section_slot *f_slot;
2212 struct lto_section_slot s_slot;
2213 const char *section_name = lto_get_section_name (section_type, name, file_data);
2214 char *data = NULL;
2216 *len = 0;
2217 s_slot.name = section_name;
2218 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2219 if (f_slot)
2221 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2222 *len = f_slot->len;
2225 free (CONST_CAST (char *, section_name));
2226 return data;
2230 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2231 starts at OFFSET and has LEN bytes. */
2233 static void
2234 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2235 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2236 const char *name ATTRIBUTE_UNUSED,
2237 const char *offset, size_t len ATTRIBUTE_UNUSED)
2239 #if LTO_MMAP_IO
2240 intptr_t computed_len;
2241 intptr_t computed_offset;
2242 intptr_t diff;
2243 #endif
2245 #if LTO_MMAP_IO
2246 computed_offset = ((intptr_t) offset) & page_mask;
2247 diff = (intptr_t) offset - computed_offset;
2248 computed_len = len + diff;
2250 munmap ((caddr_t) computed_offset, computed_len);
2251 #else
2252 free (CONST_CAST(char *, offset));
2253 #endif
2256 static lto_file *current_lto_file;
2258 /* Helper for qsort; compare partitions and return one with smaller size.
2259 We sort from greatest to smallest so parallel build doesn't stale on the
2260 longest compilation being executed too late. */
2262 static int
2263 cmp_partitions_size (const void *a, const void *b)
2265 const struct ltrans_partition_def *pa
2266 = *(struct ltrans_partition_def *const *)a;
2267 const struct ltrans_partition_def *pb
2268 = *(struct ltrans_partition_def *const *)b;
2269 return pb->insns - pa->insns;
2272 /* Helper for qsort; compare partitions and return one with smaller order. */
2274 static int
2275 cmp_partitions_order (const void *a, const void *b)
2277 const struct ltrans_partition_def *pa
2278 = *(struct ltrans_partition_def *const *)a;
2279 const struct ltrans_partition_def *pb
2280 = *(struct ltrans_partition_def *const *)b;
2281 int ordera = -1, orderb = -1;
2283 if (lto_symtab_encoder_size (pa->encoder))
2284 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2285 if (lto_symtab_encoder_size (pb->encoder))
2286 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2287 return orderb - ordera;
2290 /* Actually stream out ENCODER into TEMP_FILENAME. */
2292 static void
2293 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2295 lto_file *file = lto_obj_file_open (temp_filename, true);
2296 if (!file)
2297 fatal_error (input_location, "lto_obj_file_open() failed");
2298 lto_set_current_out_file (file);
2300 ipa_write_optimization_summaries (encoder);
2302 lto_set_current_out_file (NULL);
2303 lto_obj_file_close (file);
2304 free (file);
2307 /* Wait for forked process and signal errors. */
2308 #ifdef HAVE_WORKING_FORK
2309 static void
2310 wait_for_child ()
2312 int status;
2315 #ifndef WCONTINUED
2316 #define WCONTINUED 0
2317 #endif
2318 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2319 if (w == -1)
2320 fatal_error (input_location, "waitpid failed");
2322 if (WIFEXITED (status) && WEXITSTATUS (status))
2323 fatal_error (input_location, "streaming subprocess failed");
2324 else if (WIFSIGNALED (status))
2325 fatal_error (input_location,
2326 "streaming subprocess was killed by signal");
2328 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2330 #endif
2332 /* Stream out ENCODER into TEMP_FILENAME
2333 Fork if that seems to help. */
2335 static void
2336 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2337 bool ARG_UNUSED (last))
2339 #ifdef HAVE_WORKING_FORK
2340 static int nruns;
2342 if (lto_parallelism <= 1)
2344 do_stream_out (temp_filename, encoder);
2345 return;
2348 /* Do not run more than LTO_PARALLELISM streamings
2349 FIXME: we ignore limits on jobserver. */
2350 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2352 wait_for_child ();
2353 nruns --;
2355 /* If this is not the last parallel partition, execute new
2356 streaming process. */
2357 if (!last)
2359 pid_t cpid = fork ();
2361 if (!cpid)
2363 setproctitle ("lto1-wpa-streaming");
2364 do_stream_out (temp_filename, encoder);
2365 exit (0);
2367 /* Fork failed; lets do the job ourseleves. */
2368 else if (cpid == -1)
2369 do_stream_out (temp_filename, encoder);
2370 else
2371 nruns++;
2373 /* Last partition; stream it and wait for all children to die. */
2374 else
2376 int i;
2377 do_stream_out (temp_filename, encoder);
2378 for (i = 0; i < nruns; i++)
2379 wait_for_child ();
2381 asm_nodes_output = true;
2382 #else
2383 do_stream_out (temp_filename, encoder);
2384 #endif
2387 /* Write all output files in WPA mode and the file with the list of
2388 LTRANS units. */
2390 static void
2391 lto_wpa_write_files (void)
2393 unsigned i, n_sets;
2394 ltrans_partition part;
2395 FILE *ltrans_output_list_stream;
2396 char *temp_filename;
2397 vec <char *>temp_filenames = vNULL;
2398 size_t blen;
2400 /* Open the LTRANS output list. */
2401 if (!ltrans_output_list)
2402 fatal_error (input_location, "no LTRANS output list filename provided");
2404 timevar_push (TV_WHOPR_WPA);
2406 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2407 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2409 timevar_pop (TV_WHOPR_WPA);
2411 timevar_push (TV_WHOPR_WPA_IO);
2413 /* Generate a prefix for the LTRANS unit files. */
2414 blen = strlen (ltrans_output_list);
2415 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2416 strcpy (temp_filename, ltrans_output_list);
2417 if (blen > sizeof (".out")
2418 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2419 ".out") == 0)
2420 temp_filename[blen - sizeof (".out") + 1] = '\0';
2421 blen = strlen (temp_filename);
2423 n_sets = ltrans_partitions.length ();
2425 /* Sort partitions by size so small ones are compiled last.
2426 FIXME: Even when not reordering we may want to output one list for parallel make
2427 and other for final link command. */
2429 if (!flag_profile_reorder_functions || !flag_profile_use)
2430 ltrans_partitions.qsort (flag_toplevel_reorder
2431 ? cmp_partitions_size
2432 : cmp_partitions_order);
2434 for (i = 0; i < n_sets; i++)
2436 ltrans_partition part = ltrans_partitions[i];
2438 /* Write all the nodes in SET. */
2439 sprintf (temp_filename + blen, "%u.o", i);
2441 if (!quiet_flag)
2442 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2443 if (symtab->dump_file)
2445 lto_symtab_encoder_iterator lsei;
2447 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2448 part->name, temp_filename, part->insns);
2449 fprintf (symtab->dump_file, " Symbols in partition: ");
2450 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2451 lsei_next_in_partition (&lsei))
2453 symtab_node *node = lsei_node (lsei);
2454 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2456 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2457 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2458 lsei_next (&lsei))
2460 symtab_node *node = lsei_node (lsei);
2461 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2463 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2464 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2465 if (cnode
2466 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2467 fprintf (symtab->dump_file, "(body included)");
2468 else
2470 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2471 if (vnode
2472 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2473 fprintf (symtab->dump_file, "(initializer included)");
2477 fprintf (symtab->dump_file, "\n");
2479 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2481 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2483 part->encoder = NULL;
2485 temp_filenames.safe_push (xstrdup (temp_filename));
2487 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2488 if (ltrans_output_list_stream == NULL)
2489 fatal_error (input_location,
2490 "opening LTRANS output list %s: %m", ltrans_output_list);
2491 for (i = 0; i < n_sets; i++)
2493 unsigned int len = strlen (temp_filenames[i]);
2494 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2495 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2496 fatal_error (input_location, "writing to LTRANS output list %s: %m",
2497 ltrans_output_list);
2498 free (temp_filenames[i]);
2500 temp_filenames.release();
2502 lto_stats.num_output_files += n_sets;
2504 /* Close the LTRANS output list. */
2505 if (fclose (ltrans_output_list_stream))
2506 fatal_error (input_location,
2507 "closing LTRANS output list %s: %m", ltrans_output_list);
2509 free_ltrans_partitions();
2510 free (temp_filename);
2512 timevar_pop (TV_WHOPR_WPA_IO);
2516 /* If TT is a variable or function decl replace it with its
2517 prevailing variant. */
2518 #define LTO_SET_PREVAIL(tt) \
2519 do {\
2520 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2521 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2523 tt = lto_symtab_prevailing_decl (tt); \
2524 fixed = true; \
2526 } while (0)
2528 /* Ensure that TT isn't a replacable var of function decl. */
2529 #define LTO_NO_PREVAIL(tt) \
2530 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2532 /* Given a tree T replace all fields referring to variables or functions
2533 with their prevailing variant. */
2534 static void
2535 lto_fixup_prevailing_decls (tree t)
2537 enum tree_code code = TREE_CODE (t);
2538 bool fixed = false;
2540 gcc_checking_assert (code != TREE_BINFO);
2541 LTO_NO_PREVAIL (TREE_TYPE (t));
2542 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2543 LTO_NO_PREVAIL (TREE_CHAIN (t));
2544 if (DECL_P (t))
2546 LTO_NO_PREVAIL (DECL_NAME (t));
2547 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2548 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2550 LTO_SET_PREVAIL (DECL_SIZE (t));
2551 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2552 LTO_SET_PREVAIL (DECL_INITIAL (t));
2553 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2554 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2556 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2558 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2560 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2562 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2564 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2566 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2567 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2568 LTO_NO_PREVAIL (DECL_VINDEX (t));
2570 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2572 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2573 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2574 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2575 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2576 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2579 else if (TYPE_P (t))
2581 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2582 LTO_SET_PREVAIL (TYPE_SIZE (t));
2583 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2584 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2585 LTO_NO_PREVAIL (TYPE_NAME (t));
2587 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2588 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2589 LTO_NO_PREVAIL (t->type_non_common.binfo);
2591 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2593 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2594 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2595 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2597 else if (EXPR_P (t))
2599 int i;
2600 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2601 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2603 else if (TREE_CODE (t) == CONSTRUCTOR)
2605 unsigned i;
2606 tree val;
2607 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2608 LTO_SET_PREVAIL (val);
2610 else
2612 switch (code)
2614 case TREE_LIST:
2615 LTO_SET_PREVAIL (TREE_VALUE (t));
2616 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2617 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2618 break;
2619 default:
2620 gcc_unreachable ();
2623 /* If we fixed nothing, then we missed something seen by
2624 mentions_vars_p. */
2625 gcc_checking_assert (fixed);
2627 #undef LTO_SET_PREVAIL
2628 #undef LTO_NO_PREVAIL
2630 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2631 replaces var and function decls with the corresponding prevailing def. */
2633 static void
2634 lto_fixup_state (struct lto_in_decl_state *state)
2636 unsigned i, si;
2638 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2639 we still need to walk from all DECLs to find the reachable
2640 FUNCTION_DECLs and VAR_DECLs. */
2641 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2643 vec<tree, va_gc> *trees = state->streams[si];
2644 for (i = 0; i < vec_safe_length (trees); i++)
2646 tree t = (*trees)[i];
2647 #ifdef ENABLE_CHECKING
2648 if (TYPE_P (t))
2649 verify_type (t);
2650 #endif
2651 if (VAR_OR_FUNCTION_DECL_P (t)
2652 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2653 (*trees)[i] = lto_symtab_prevailing_decl (t);
2658 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2659 prevailing one. */
2661 static void
2662 lto_fixup_decls (struct lto_file_decl_data **files)
2664 unsigned int i;
2665 tree t;
2667 if (tree_with_vars)
2668 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2669 lto_fixup_prevailing_decls (t);
2671 for (i = 0; files[i]; i++)
2673 struct lto_file_decl_data *file = files[i];
2674 struct lto_in_decl_state *state = file->global_decl_state;
2675 lto_fixup_state (state);
2677 hash_table<decl_state_hasher>::iterator iter;
2678 lto_in_decl_state *elt;
2679 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2680 lto_in_decl_state *, iter)
2681 lto_fixup_state (elt);
2685 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2687 /* Turn file datas for sub files into a single array, so that they look
2688 like separate files for further passes. */
2690 static void
2691 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2693 struct lto_file_decl_data *n, *next;
2694 int i, k;
2696 lto_stats.num_input_files = count;
2697 all_file_decl_data
2698 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2699 /* Set the hooks so that all of the ipa passes can read in their data. */
2700 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2701 for (i = 0, k = 0; i < last_file_ix; i++)
2703 for (n = orig[i]; n != NULL; n = next)
2705 all_file_decl_data[k++] = n;
2706 next = n->next;
2707 n->next = NULL;
2710 all_file_decl_data[k] = NULL;
2711 gcc_assert (k == count);
2714 /* Input file data before flattening (i.e. splitting them to subfiles to support
2715 incremental linking. */
2716 static int real_file_count;
2717 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2719 static void print_lto_report_1 (void);
2721 /* Read all the symbols from the input files FNAMES. NFILES is the
2722 number of files requested in the command line. Instantiate a
2723 global call graph by aggregating all the sub-graphs found in each
2724 file. */
2726 static void
2727 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2729 unsigned int i, last_file_ix;
2730 FILE *resolution;
2731 int count = 0;
2732 struct lto_file_decl_data **decl_data;
2733 symtab_node *snode;
2735 symtab->initialize ();
2737 timevar_push (TV_IPA_LTO_DECL_IN);
2739 #ifdef ACCEL_COMPILER
2740 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2741 lto_stream_offload_p = true;
2742 #endif
2744 real_file_decl_data
2745 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2746 real_file_count = nfiles;
2748 /* Read the resolution file. */
2749 resolution = NULL;
2750 if (resolution_file_name)
2752 int t;
2753 unsigned num_objects;
2755 resolution = fopen (resolution_file_name, "r");
2756 if (resolution == NULL)
2757 fatal_error (input_location,
2758 "could not open symbol resolution file: %m");
2760 t = fscanf (resolution, "%u", &num_objects);
2761 gcc_assert (t == 1);
2763 /* True, since the plugin splits the archives. */
2764 gcc_assert (num_objects == nfiles);
2766 symtab->state = LTO_STREAMING;
2768 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2769 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2770 gimple_canonical_type_eq, NULL);
2771 gcc_obstack_init (&tree_scc_hash_obstack);
2772 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2774 /* Register the common node types with the canonical type machinery so
2775 we properly share alias-sets across languages and TUs. Do not
2776 expose the common nodes as type merge target - those that should be
2777 are already exposed so by pre-loading the LTO streamer caches.
2778 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2779 for (i = 0; i < itk_none; ++i)
2780 lto_register_canonical_types (integer_types[i], true);
2781 for (i = 0; i < stk_type_kind_last; ++i)
2782 lto_register_canonical_types (sizetype_tab[i], true);
2783 for (i = 0; i < TI_MAX; ++i)
2784 lto_register_canonical_types (global_trees[i], true);
2785 for (i = 0; i < itk_none; ++i)
2786 lto_register_canonical_types (integer_types[i], false);
2787 for (i = 0; i < stk_type_kind_last; ++i)
2788 lto_register_canonical_types (sizetype_tab[i], false);
2789 for (i = 0; i < TI_MAX; ++i)
2790 lto_register_canonical_types (global_trees[i], false);
2792 if (!quiet_flag)
2793 fprintf (stderr, "Reading object files:");
2795 /* Read all of the object files specified on the command line. */
2796 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2798 struct lto_file_decl_data *file_data = NULL;
2799 if (!quiet_flag)
2801 fprintf (stderr, " %s", fnames[i]);
2802 fflush (stderr);
2805 current_lto_file = lto_obj_file_open (fnames[i], false);
2806 if (!current_lto_file)
2807 break;
2809 file_data = lto_file_read (current_lto_file, resolution, &count);
2810 if (!file_data)
2812 lto_obj_file_close (current_lto_file);
2813 free (current_lto_file);
2814 current_lto_file = NULL;
2815 break;
2818 decl_data[last_file_ix++] = file_data;
2820 lto_obj_file_close (current_lto_file);
2821 free (current_lto_file);
2822 current_lto_file = NULL;
2825 lto_flatten_files (decl_data, count, last_file_ix);
2826 lto_stats.num_input_files = count;
2827 ggc_free(decl_data);
2828 real_file_decl_data = NULL;
2830 if (resolution_file_name)
2831 fclose (resolution);
2833 /* Show the LTO report before launching LTRANS. */
2834 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2835 print_lto_report_1 ();
2837 /* Free gimple type merging datastructures. */
2838 delete tree_scc_hash;
2839 tree_scc_hash = NULL;
2840 obstack_free (&tree_scc_hash_obstack, NULL);
2841 htab_delete (gimple_canonical_types);
2842 gimple_canonical_types = NULL;
2843 delete canonical_type_hash_cache;
2844 canonical_type_hash_cache = NULL;
2846 /* At this stage we know that majority of GGC memory is reachable.
2847 Growing the limits prevents unnecesary invocation of GGC. */
2848 ggc_grow ();
2849 ggc_collect ();
2851 /* Set the hooks so that all of the ipa passes can read in their data. */
2852 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2854 timevar_pop (TV_IPA_LTO_DECL_IN);
2856 if (!quiet_flag)
2857 fprintf (stderr, "\nReading the callgraph\n");
2859 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2860 /* Read the symtab. */
2861 input_symtab ();
2863 input_offload_tables ();
2865 /* Store resolutions into the symbol table. */
2867 ld_plugin_symbol_resolution_t *res;
2868 FOR_EACH_SYMBOL (snode)
2869 if (snode->real_symbol_p ()
2870 && snode->lto_file_data
2871 && snode->lto_file_data->resolution_map
2872 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
2873 snode->resolution = *res;
2874 for (i = 0; all_file_decl_data[i]; i++)
2875 if (all_file_decl_data[i]->resolution_map)
2877 delete all_file_decl_data[i]->resolution_map;
2878 all_file_decl_data[i]->resolution_map = NULL;
2881 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2883 if (!quiet_flag)
2884 fprintf (stderr, "Merging declarations\n");
2886 timevar_push (TV_IPA_LTO_DECL_MERGE);
2887 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2888 need to care about resolving symbols again, we only need to replace
2889 duplicated declarations read from the callgraph and from function
2890 sections. */
2891 if (!flag_ltrans)
2893 lto_symtab_merge_decls ();
2895 /* If there were errors during symbol merging bail out, we have no
2896 good way to recover here. */
2897 if (seen_error ())
2898 fatal_error (input_location,
2899 "errors during merging of translation units");
2901 /* Fixup all decls. */
2902 lto_fixup_decls (all_file_decl_data);
2904 if (tree_with_vars)
2905 ggc_free (tree_with_vars);
2906 tree_with_vars = NULL;
2907 ggc_collect ();
2909 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2910 /* Each pass will set the appropriate timer. */
2912 if (!quiet_flag)
2913 fprintf (stderr, "Reading summaries\n");
2915 /* Read the IPA summary data. */
2916 if (flag_ltrans)
2917 ipa_read_optimization_summaries ();
2918 else
2919 ipa_read_summaries ();
2921 for (i = 0; all_file_decl_data[i]; i++)
2923 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2924 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2925 all_file_decl_data[i]->symtab_node_encoder = NULL;
2926 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
2927 all_file_decl_data[i]->global_decl_state = NULL;
2928 all_file_decl_data[i]->current_decl_state = NULL;
2931 /* Finally merge the cgraph according to the decl merging decisions. */
2932 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2933 if (symtab->dump_file)
2935 fprintf (symtab->dump_file, "Before merging:\n");
2936 symtab_node::dump_table (symtab->dump_file);
2938 if (!flag_ltrans)
2940 lto_symtab_merge_symbols ();
2941 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
2942 we are still having duplicated comdat groups containing local statics.
2943 We could also just remove them while merging. */
2944 symtab->remove_unreachable_nodes (dump_file);
2946 ggc_collect ();
2947 symtab->state = IPA_SSA;
2948 /* FIXME: Technically all node removals happening here are useless, because
2949 WPA should not stream them. */
2950 if (flag_ltrans)
2951 symtab->remove_unreachable_nodes (dump_file);
2953 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2955 /* Indicate that the cgraph is built and ready. */
2956 symtab->function_flags_ready = true;
2958 ggc_free (all_file_decl_data);
2959 all_file_decl_data = NULL;
2963 /* Materialize all the bodies for all the nodes in the callgraph. */
2965 static void
2966 materialize_cgraph (void)
2968 struct cgraph_node *node;
2969 timevar_id_t lto_timer;
2971 if (!quiet_flag)
2972 fprintf (stderr,
2973 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2976 FOR_EACH_FUNCTION (node)
2978 if (node->lto_file_data)
2980 lto_materialize_function (node);
2981 lto_stats.num_input_cgraph_nodes++;
2986 /* Start the appropriate timer depending on the mode that we are
2987 operating in. */
2988 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2989 : (flag_ltrans) ? TV_WHOPR_LTRANS
2990 : TV_LTO;
2991 timevar_push (lto_timer);
2993 current_function_decl = NULL;
2994 set_cfun (NULL);
2996 if (!quiet_flag)
2997 fprintf (stderr, "\n");
2999 timevar_pop (lto_timer);
3003 /* Show various memory usage statistics related to LTO. */
3004 static void
3005 print_lto_report_1 (void)
3007 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3008 fprintf (stderr, "%s statistics\n", pfx);
3010 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3011 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3012 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3013 if (flag_wpa && tree_scc_hash)
3015 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3016 "collision ratio: %f\n", pfx,
3017 (long) tree_scc_hash->size (),
3018 (long) tree_scc_hash->elements (),
3019 tree_scc_hash->collisions ());
3020 hash_table<tree_scc_hasher>::iterator hiter;
3021 tree_scc *scc, *max_scc = NULL;
3022 unsigned max_length = 0;
3023 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3025 unsigned length = 0;
3026 tree_scc *s = scc;
3027 for (; s; s = s->next)
3028 length++;
3029 if (length > max_length)
3031 max_length = length;
3032 max_scc = scc;
3035 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3036 pfx, max_length, max_scc->len);
3037 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3038 num_scc_compares, num_scc_compare_collisions,
3039 num_scc_compare_collisions / (double) num_scc_compares);
3040 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3041 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3042 total_scc_size_merged);
3043 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3044 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3045 pfx, num_prevailing_types, num_type_scc_trees);
3046 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3047 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3048 (long) htab_size (gimple_canonical_types),
3049 (long) htab_elements (gimple_canonical_types),
3050 (long) gimple_canonical_types->searches,
3051 (long) gimple_canonical_types->collisions,
3052 htab_collisions (gimple_canonical_types));
3053 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3054 "%lu elements, %ld searches\n", pfx,
3055 num_canonical_type_hash_entries,
3056 num_canonical_type_hash_queries);
3059 print_lto_report (pfx);
3062 /* Perform whole program analysis (WPA) on the callgraph and write out the
3063 optimization plan. */
3065 static void
3066 do_whole_program_analysis (void)
3068 symtab_node *node;
3070 lto_parallelism = 1;
3072 /* TODO: jobserver communicatoin is not supported, yet. */
3073 if (!strcmp (flag_wpa, "jobserver"))
3074 lto_parallelism = -1;
3075 else
3077 lto_parallelism = atoi (flag_wpa);
3078 if (lto_parallelism <= 0)
3079 lto_parallelism = 0;
3082 timevar_start (TV_PHASE_OPT_GEN);
3084 /* Note that since we are in WPA mode, materialize_cgraph will not
3085 actually read in all the function bodies. It only materializes
3086 the decls and cgraph nodes so that analysis can be performed. */
3087 materialize_cgraph ();
3089 /* Reading in the cgraph uses different timers, start timing WPA now. */
3090 timevar_push (TV_WHOPR_WPA);
3092 if (pre_ipa_mem_report)
3094 fprintf (stderr, "Memory consumption before IPA\n");
3095 dump_memory_report (false);
3098 symtab->function_flags_ready = true;
3100 if (symtab->dump_file)
3101 symtab_node::dump_table (symtab->dump_file);
3102 bitmap_obstack_initialize (NULL);
3103 symtab->state = IPA_SSA;
3105 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3107 if (symtab->dump_file)
3109 fprintf (symtab->dump_file, "Optimized ");
3110 symtab_node::dump_table (symtab->dump_file);
3112 #ifdef ENABLE_CHECKING
3113 symtab_node::verify_symtab_nodes ();
3114 #endif
3115 bitmap_obstack_release (NULL);
3117 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3118 timevar_pop (TV_WHOPR_WPA);
3120 timevar_push (TV_WHOPR_PARTITIONING);
3121 if (flag_lto_partition == LTO_PARTITION_1TO1)
3122 lto_1_to_1_map ();
3123 else if (flag_lto_partition == LTO_PARTITION_MAX)
3124 lto_max_map ();
3125 else if (flag_lto_partition == LTO_PARTITION_ONE)
3126 lto_balanced_map (1);
3127 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3128 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3129 else
3130 gcc_unreachable ();
3132 /* Inline summaries are needed for balanced partitioning. Free them now so
3133 the memory can be used for streamer caches. */
3134 inline_free_summary ();
3136 /* AUX pointers are used by partitioning code to bookkeep number of
3137 partitions symbol is in. This is no longer needed. */
3138 FOR_EACH_SYMBOL (node)
3139 node->aux = NULL;
3141 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3143 /* Find out statics that need to be promoted
3144 to globals with hidden visibility because they are accessed from multiple
3145 partitions. */
3146 lto_promote_cross_file_statics ();
3147 timevar_pop (TV_WHOPR_PARTITIONING);
3149 timevar_stop (TV_PHASE_OPT_GEN);
3151 /* Collect a last time - in lto_wpa_write_files we may end up forking
3152 with the idea that this doesn't increase memory usage. So we
3153 absoultely do not want to collect after that. */
3154 ggc_collect ();
3156 timevar_start (TV_PHASE_STREAM_OUT);
3157 if (!quiet_flag)
3159 fprintf (stderr, "\nStreaming out");
3160 fflush (stderr);
3162 lto_wpa_write_files ();
3163 if (!quiet_flag)
3164 fprintf (stderr, "\n");
3165 timevar_stop (TV_PHASE_STREAM_OUT);
3167 if (post_ipa_mem_report)
3169 fprintf (stderr, "Memory consumption after IPA\n");
3170 dump_memory_report (false);
3173 /* Show the LTO report before launching LTRANS. */
3174 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3175 print_lto_report_1 ();
3176 if (mem_report_wpa)
3177 dump_memory_report (true);
3181 static GTY(()) tree lto_eh_personality_decl;
3183 /* Return the LTO personality function decl. */
3185 tree
3186 lto_eh_personality (void)
3188 if (!lto_eh_personality_decl)
3190 /* Use the first personality DECL for our personality if we don't
3191 support multiple ones. This ensures that we don't artificially
3192 create the need for them in a single-language program. */
3193 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3194 lto_eh_personality_decl = first_personality_decl;
3195 else
3196 lto_eh_personality_decl = lhd_gcc_personality ();
3199 return lto_eh_personality_decl;
3202 /* Set the process name based on the LTO mode. */
3204 static void
3205 lto_process_name (void)
3207 if (flag_lto)
3208 setproctitle ("lto1-lto");
3209 if (flag_wpa)
3210 setproctitle ("lto1-wpa");
3211 if (flag_ltrans)
3212 setproctitle ("lto1-ltrans");
3216 /* Initialize the LTO front end. */
3218 static void
3219 lto_init (void)
3221 lto_process_name ();
3222 lto_streamer_hooks_init ();
3223 lto_reader_init ();
3224 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3225 memset (&lto_stats, 0, sizeof (lto_stats));
3226 bitmap_obstack_initialize (NULL);
3227 gimple_register_cfg_hooks ();
3228 #ifndef ACCEL_COMPILER
3229 unsigned char *table
3230 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3231 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3232 table[m] = m;
3233 lto_mode_identity_table = table;
3234 #endif
3238 /* Main entry point for the GIMPLE front end. This front end has
3239 three main personalities:
3241 - LTO (-flto). All the object files on the command line are
3242 loaded in memory and processed as a single translation unit.
3243 This is the traditional link-time optimization behavior.
3245 - WPA (-fwpa). Only the callgraph and summary information for
3246 files in the command file are loaded. A single callgraph
3247 (without function bodies) is instantiated for the whole set of
3248 files. IPA passes are only allowed to analyze the call graph
3249 and make transformation decisions. The callgraph is
3250 partitioned, each partition is written to a new object file
3251 together with the transformation decisions.
3253 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3254 summary files from running again. Since WPA computed summary
3255 information and decided what transformations to apply, LTRANS
3256 simply applies them. */
3258 void
3259 lto_main (void)
3261 /* LTO is called as a front end, even though it is not a front end.
3262 Because it is called as a front end, TV_PHASE_PARSING and
3263 TV_PARSE_GLOBAL are active, and we need to turn them off while
3264 doing LTO. Later we turn them back on so they are active up in
3265 toplev.c. */
3266 timevar_pop (TV_PARSE_GLOBAL);
3267 timevar_stop (TV_PHASE_PARSING);
3269 timevar_start (TV_PHASE_SETUP);
3271 /* Initialize the LTO front end. */
3272 lto_init ();
3274 timevar_stop (TV_PHASE_SETUP);
3275 timevar_start (TV_PHASE_STREAM_IN);
3277 /* Read all the symbols and call graph from all the files in the
3278 command line. */
3279 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3281 timevar_stop (TV_PHASE_STREAM_IN);
3283 if (!seen_error ())
3285 /* If WPA is enabled analyze the whole call graph and create an
3286 optimization plan. Otherwise, read in all the function
3287 bodies and continue with optimization. */
3288 if (flag_wpa)
3289 do_whole_program_analysis ();
3290 else
3292 timevar_start (TV_PHASE_OPT_GEN);
3294 materialize_cgraph ();
3295 if (!flag_ltrans)
3296 lto_promote_statics_nonwpa ();
3298 /* Let the middle end know that we have read and merged all of
3299 the input files. */
3300 symtab->compile ();
3302 timevar_stop (TV_PHASE_OPT_GEN);
3304 /* FIXME lto, if the processes spawned by WPA fail, we miss
3305 the chance to print WPA's report, so WPA will call
3306 print_lto_report before launching LTRANS. If LTRANS was
3307 launched directly by the driver we would not need to do
3308 this. */
3309 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3310 print_lto_report_1 ();
3314 /* Here we make LTO pretend to be a parser. */
3315 timevar_start (TV_PHASE_PARSING);
3316 timevar_push (TV_PARSE_GLOBAL);
3319 #include "gt-lto-lto.h"