Merge from trunk @222673.
[official-gcc.git] / gcc / lto / lto.c
blob5170996575391c1cfc135f17fecd39452e38fa58
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 /* Combine a few common features of types so that types are grouped into
313 smaller sets; when searching for existing matching types to merge,
314 only existing types having the same features as the new type will be
315 checked. */
316 hstate.add_int (TREE_CODE (type));
317 hstate.add_int (TYPE_MODE (type));
319 /* Incorporate common features of numerical types. */
320 if (INTEGRAL_TYPE_P (type)
321 || SCALAR_FLOAT_TYPE_P (type)
322 || FIXED_POINT_TYPE_P (type)
323 || TREE_CODE (type) == OFFSET_TYPE
324 || POINTER_TYPE_P (type))
326 hstate.add_int (TYPE_UNSIGNED (type));
327 hstate.add_int (TYPE_PRECISION (type));
330 if (VECTOR_TYPE_P (type))
332 hstate.add_int (TYPE_VECTOR_SUBPARTS (type));
333 hstate.add_int (TYPE_UNSIGNED (type));
336 if (TREE_CODE (type) == COMPLEX_TYPE)
337 hstate.add_int (TYPE_UNSIGNED (type));
339 /* For pointer and reference types, fold in information about the type
340 pointed to but do not recurse to the pointed-to type. */
341 if (POINTER_TYPE_P (type))
343 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
344 hstate.add_int (TREE_CODE (TREE_TYPE (type)));
347 /* For integer types hash only the string flag. */
348 if (TREE_CODE (type) == INTEGER_TYPE)
349 hstate.add_int (TYPE_STRING_FLAG (type));
351 /* For array types hash the domain bounds and the string flag. */
352 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
354 hstate.add_int (TYPE_STRING_FLAG (type));
355 /* OMP lowering can introduce error_mark_node in place of
356 random local decls in types. */
357 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
358 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
359 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
360 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
363 /* Recurse for aggregates with a single element type. */
364 if (TREE_CODE (type) == ARRAY_TYPE
365 || TREE_CODE (type) == COMPLEX_TYPE
366 || TREE_CODE (type) == VECTOR_TYPE)
367 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
369 /* Incorporate function return and argument types. */
370 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
372 unsigned na;
373 tree p;
375 /* For method types also incorporate their parent class. */
376 if (TREE_CODE (type) == METHOD_TYPE)
377 iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type), hstate);
379 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
381 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
383 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
384 na++;
387 hstate.add_int (na);
390 if (RECORD_OR_UNION_TYPE_P (type))
392 unsigned nf;
393 tree f;
395 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
396 if (TREE_CODE (f) == FIELD_DECL)
398 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
399 nf++;
402 hstate.add_int (nf);
405 return hstate.end();
408 /* Returning a hash value for gimple type TYPE combined with VAL. */
410 static void
411 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
413 hashval_t v;
414 /* An already processed type. */
415 if (TYPE_CANONICAL (type))
417 type = TYPE_CANONICAL (type);
418 v = gimple_canonical_type_hash (type);
420 else
422 /* Canonical types should not be able to form SCCs by design, this
423 recursion is just because we do not register canonical types in
424 optimal order. To avoid quadratic behavior also register the
425 type here. */
426 v = hash_canonical_type (type);
427 gimple_register_canonical_type_1 (type, v);
429 hstate.add_int (v);
432 /* Returns the hash for a canonical type P. */
434 static hashval_t
435 gimple_canonical_type_hash (const void *p)
437 num_canonical_type_hash_queries++;
438 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
439 gcc_assert (slot != NULL);
440 return *slot;
444 /* The TYPE_CANONICAL merging machinery. It should closely resemble
445 the middle-end types_compatible_p function. It needs to avoid
446 claiming types are different for types that should be treated
447 the same with respect to TBAA. Canonical types are also used
448 for IL consistency checks via the useless_type_conversion_p
449 predicate which does not handle all type kinds itself but falls
450 back to pointer-comparison of TYPE_CANONICAL for aggregates
451 for example. */
453 /* Return true iff T1 and T2 are structurally identical for what
454 TBAA is concerned. */
456 static bool
457 gimple_canonical_types_compatible_p (tree t1, tree t2)
459 /* Before starting to set up the SCC machinery handle simple cases. */
461 /* Check first for the obvious case of pointer identity. */
462 if (t1 == t2)
463 return true;
465 /* Check that we have two types to compare. */
466 if (t1 == NULL_TREE || t2 == NULL_TREE)
467 return false;
469 /* If the types have been previously registered and found equal
470 they still are. */
471 if (TYPE_CANONICAL (t1)
472 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
473 return true;
475 /* Can't be the same type if the types don't have the same code. */
476 if (TREE_CODE (t1) != TREE_CODE (t2))
477 return false;
479 /* Qualifiers do not matter for canonical type comparison purposes. */
481 /* Void types and nullptr types are always the same. */
482 if (TREE_CODE (t1) == VOID_TYPE
483 || TREE_CODE (t1) == NULLPTR_TYPE)
484 return true;
486 /* Can't be the same type if they have different mode. */
487 if (TYPE_MODE (t1) != TYPE_MODE (t2))
488 return false;
490 /* Non-aggregate types can be handled cheaply. */
491 if (INTEGRAL_TYPE_P (t1)
492 || SCALAR_FLOAT_TYPE_P (t1)
493 || FIXED_POINT_TYPE_P (t1)
494 || TREE_CODE (t1) == VECTOR_TYPE
495 || TREE_CODE (t1) == COMPLEX_TYPE
496 || TREE_CODE (t1) == OFFSET_TYPE
497 || POINTER_TYPE_P (t1))
499 /* Can't be the same type if they have different sign or precision. */
500 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
501 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
502 return false;
504 if (TREE_CODE (t1) == INTEGER_TYPE
505 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
506 return false;
508 /* For canonical type comparisons we do not want to build SCCs
509 so we cannot compare pointed-to types. But we can, for now,
510 require the same pointed-to type kind and match what
511 useless_type_conversion_p would do. */
512 if (POINTER_TYPE_P (t1))
514 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
515 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
516 return false;
518 if (TREE_CODE (TREE_TYPE (t1)) != TREE_CODE (TREE_TYPE (t2)))
519 return false;
522 /* Tail-recurse to components. */
523 if (TREE_CODE (t1) == VECTOR_TYPE
524 || TREE_CODE (t1) == COMPLEX_TYPE)
525 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
526 TREE_TYPE (t2));
528 return true;
531 /* Do type-specific comparisons. */
532 switch (TREE_CODE (t1))
534 case ARRAY_TYPE:
535 /* Array types are the same if the element types are the same and
536 the number of elements are the same. */
537 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
538 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
539 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
540 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
541 return false;
542 else
544 tree i1 = TYPE_DOMAIN (t1);
545 tree i2 = TYPE_DOMAIN (t2);
547 /* For an incomplete external array, the type domain can be
548 NULL_TREE. Check this condition also. */
549 if (i1 == NULL_TREE && i2 == NULL_TREE)
550 return true;
551 else if (i1 == NULL_TREE || i2 == NULL_TREE)
552 return false;
553 else
555 tree min1 = TYPE_MIN_VALUE (i1);
556 tree min2 = TYPE_MIN_VALUE (i2);
557 tree max1 = TYPE_MAX_VALUE (i1);
558 tree max2 = TYPE_MAX_VALUE (i2);
560 /* The minimum/maximum values have to be the same. */
561 if ((min1 == min2
562 || (min1 && min2
563 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
564 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
565 || operand_equal_p (min1, min2, 0))))
566 && (max1 == max2
567 || (max1 && max2
568 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
569 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
570 || operand_equal_p (max1, max2, 0)))))
571 return true;
572 else
573 return false;
577 case METHOD_TYPE:
578 case FUNCTION_TYPE:
579 /* Function types are the same if the return type and arguments types
580 are the same. */
581 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
582 return false;
584 if (!comp_type_attributes (t1, t2))
585 return false;
587 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
588 return true;
589 else
591 tree parms1, parms2;
593 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
594 parms1 && parms2;
595 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
597 if (!gimple_canonical_types_compatible_p
598 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
599 return false;
602 if (parms1 || parms2)
603 return false;
605 return true;
608 case RECORD_TYPE:
609 case UNION_TYPE:
610 case QUAL_UNION_TYPE:
612 tree f1, f2;
614 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
615 return false;
617 /* For aggregate types, all the fields must be the same. */
618 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
619 f1 || f2;
620 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
622 /* Skip non-fields. */
623 while (f1 && TREE_CODE (f1) != FIELD_DECL)
624 f1 = TREE_CHAIN (f1);
625 while (f2 && TREE_CODE (f2) != FIELD_DECL)
626 f2 = TREE_CHAIN (f2);
627 if (!f1 || !f2)
628 break;
629 /* The fields must have the same name, offset and type. */
630 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
631 || !gimple_compare_field_offset (f1, f2)
632 || !gimple_canonical_types_compatible_p
633 (TREE_TYPE (f1), TREE_TYPE (f2)))
634 return false;
637 /* If one aggregate has more fields than the other, they
638 are not the same. */
639 if (f1 || f2)
640 return false;
642 return true;
645 default:
646 gcc_unreachable ();
651 /* Returns nonzero if P1 and P2 are equal. */
653 static int
654 gimple_canonical_type_eq (const void *p1, const void *p2)
656 const_tree t1 = (const_tree) p1;
657 const_tree t2 = (const_tree) p2;
658 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
659 CONST_CAST_TREE (t2));
662 /* Main worker for gimple_register_canonical_type. */
664 static void
665 gimple_register_canonical_type_1 (tree t, hashval_t hash)
667 void **slot;
669 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
671 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
672 if (*slot)
674 tree new_type = (tree)(*slot);
675 gcc_checking_assert (new_type != t);
676 TYPE_CANONICAL (t) = new_type;
678 else
680 TYPE_CANONICAL (t) = t;
681 *slot = (void *) t;
682 /* Cache the just computed hash value. */
683 num_canonical_type_hash_entries++;
684 bool existed_p = canonical_type_hash_cache->put (t, hash);
685 gcc_assert (!existed_p);
689 /* Register type T in the global type table gimple_types and set
690 TYPE_CANONICAL of T accordingly.
691 This is used by LTO to merge structurally equivalent types for
692 type-based aliasing purposes across different TUs and languages.
694 ??? This merging does not exactly match how the tree.c middle-end
695 functions will assign TYPE_CANONICAL when new types are created
696 during optimization (which at least happens for pointer and array
697 types). */
699 static void
700 gimple_register_canonical_type (tree t)
702 if (TYPE_CANONICAL (t))
703 return;
705 gimple_register_canonical_type_1 (t, hash_canonical_type (t));
708 /* Re-compute TYPE_CANONICAL for NODE and related types. */
710 static void
711 lto_register_canonical_types (tree node, bool first_p)
713 if (!node
714 || !TYPE_P (node))
715 return;
717 if (first_p)
718 TYPE_CANONICAL (node) = NULL_TREE;
720 if (POINTER_TYPE_P (node)
721 || TREE_CODE (node) == COMPLEX_TYPE
722 || TREE_CODE (node) == ARRAY_TYPE)
723 lto_register_canonical_types (TREE_TYPE (node), first_p);
725 if (!first_p)
726 gimple_register_canonical_type (node);
730 /* Remember trees that contains references to declarations. */
731 static GTY(()) vec <tree, va_gc> *tree_with_vars;
733 #define CHECK_VAR(tt) \
734 do \
736 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
737 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
738 return true; \
739 } while (0)
741 #define CHECK_NO_VAR(tt) \
742 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
744 /* Check presence of pointers to decls in fields of a tree_typed T. */
746 static inline bool
747 mentions_vars_p_typed (tree t)
749 CHECK_NO_VAR (TREE_TYPE (t));
750 return false;
753 /* Check presence of pointers to decls in fields of a tree_common T. */
755 static inline bool
756 mentions_vars_p_common (tree t)
758 if (mentions_vars_p_typed (t))
759 return true;
760 CHECK_NO_VAR (TREE_CHAIN (t));
761 return false;
764 /* Check presence of pointers to decls in fields of a decl_minimal T. */
766 static inline bool
767 mentions_vars_p_decl_minimal (tree t)
769 if (mentions_vars_p_common (t))
770 return true;
771 CHECK_NO_VAR (DECL_NAME (t));
772 CHECK_VAR (DECL_CONTEXT (t));
773 return false;
776 /* Check presence of pointers to decls in fields of a decl_common T. */
778 static inline bool
779 mentions_vars_p_decl_common (tree t)
781 if (mentions_vars_p_decl_minimal (t))
782 return true;
783 CHECK_VAR (DECL_SIZE (t));
784 CHECK_VAR (DECL_SIZE_UNIT (t));
785 CHECK_VAR (DECL_INITIAL (t));
786 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
787 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
788 return false;
791 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
793 static inline bool
794 mentions_vars_p_decl_with_vis (tree t)
796 if (mentions_vars_p_decl_common (t))
797 return true;
799 /* Accessor macro has side-effects, use field-name here. */
800 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
801 return false;
804 /* Check presence of pointers to decls in fields of a decl_non_common T. */
806 static inline bool
807 mentions_vars_p_decl_non_common (tree t)
809 if (mentions_vars_p_decl_with_vis (t))
810 return true;
811 CHECK_NO_VAR (DECL_RESULT_FLD (t));
812 return false;
815 /* Check presence of pointers to decls in fields of a decl_non_common T. */
817 static bool
818 mentions_vars_p_function (tree t)
820 if (mentions_vars_p_decl_non_common (t))
821 return true;
822 CHECK_NO_VAR (DECL_ARGUMENTS (t));
823 CHECK_NO_VAR (DECL_VINDEX (t));
824 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
825 return false;
828 /* Check presence of pointers to decls in fields of a field_decl T. */
830 static bool
831 mentions_vars_p_field_decl (tree t)
833 if (mentions_vars_p_decl_common (t))
834 return true;
835 CHECK_VAR (DECL_FIELD_OFFSET (t));
836 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
837 CHECK_NO_VAR (DECL_QUALIFIER (t));
838 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
839 CHECK_NO_VAR (DECL_FCONTEXT (t));
840 return false;
843 /* Check presence of pointers to decls in fields of a type T. */
845 static bool
846 mentions_vars_p_type (tree t)
848 if (mentions_vars_p_common (t))
849 return true;
850 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
851 CHECK_VAR (TYPE_SIZE (t));
852 CHECK_VAR (TYPE_SIZE_UNIT (t));
853 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
854 CHECK_NO_VAR (TYPE_NAME (t));
856 CHECK_VAR (TYPE_MINVAL (t));
857 CHECK_VAR (TYPE_MAXVAL (t));
859 /* Accessor is for derived node types only. */
860 CHECK_NO_VAR (t->type_non_common.binfo);
862 CHECK_VAR (TYPE_CONTEXT (t));
863 CHECK_NO_VAR (TYPE_CANONICAL (t));
864 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
865 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
866 return false;
869 /* Check presence of pointers to decls in fields of a BINFO T. */
871 static bool
872 mentions_vars_p_binfo (tree t)
874 unsigned HOST_WIDE_INT i, n;
876 if (mentions_vars_p_common (t))
877 return true;
878 CHECK_VAR (BINFO_VTABLE (t));
879 CHECK_NO_VAR (BINFO_OFFSET (t));
880 CHECK_NO_VAR (BINFO_VIRTUALS (t));
881 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
882 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
883 for (i = 0; i < n; i++)
884 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
885 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
886 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
887 n = BINFO_N_BASE_BINFOS (t);
888 for (i = 0; i < n; i++)
889 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
890 return false;
893 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
895 static bool
896 mentions_vars_p_constructor (tree t)
898 unsigned HOST_WIDE_INT idx;
899 constructor_elt *ce;
901 if (mentions_vars_p_typed (t))
902 return true;
904 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
906 CHECK_NO_VAR (ce->index);
907 CHECK_VAR (ce->value);
909 return false;
912 /* Check presence of pointers to decls in fields of an expression tree T. */
914 static bool
915 mentions_vars_p_expr (tree t)
917 int i;
918 if (mentions_vars_p_typed (t))
919 return true;
920 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
921 CHECK_VAR (TREE_OPERAND (t, i));
922 return false;
925 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
927 static bool
928 mentions_vars_p_omp_clause (tree t)
930 int i;
931 if (mentions_vars_p_common (t))
932 return true;
933 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
934 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
935 return false;
938 /* Check presence of pointers to decls that needs later fixup in T. */
940 static bool
941 mentions_vars_p (tree t)
943 switch (TREE_CODE (t))
945 case IDENTIFIER_NODE:
946 break;
948 case TREE_LIST:
949 CHECK_VAR (TREE_VALUE (t));
950 CHECK_VAR (TREE_PURPOSE (t));
951 CHECK_NO_VAR (TREE_CHAIN (t));
952 break;
954 case FIELD_DECL:
955 return mentions_vars_p_field_decl (t);
957 case LABEL_DECL:
958 case CONST_DECL:
959 case PARM_DECL:
960 case RESULT_DECL:
961 case IMPORTED_DECL:
962 case NAMESPACE_DECL:
963 case NAMELIST_DECL:
964 return mentions_vars_p_decl_common (t);
966 case VAR_DECL:
967 return mentions_vars_p_decl_with_vis (t);
969 case TYPE_DECL:
970 return mentions_vars_p_decl_non_common (t);
972 case FUNCTION_DECL:
973 return mentions_vars_p_function (t);
975 case TREE_BINFO:
976 return mentions_vars_p_binfo (t);
978 case PLACEHOLDER_EXPR:
979 return mentions_vars_p_common (t);
981 case BLOCK:
982 case TRANSLATION_UNIT_DECL:
983 case OPTIMIZATION_NODE:
984 case TARGET_OPTION_NODE:
985 break;
987 case CONSTRUCTOR:
988 return mentions_vars_p_constructor (t);
990 case OMP_CLAUSE:
991 return mentions_vars_p_omp_clause (t);
993 default:
994 if (TYPE_P (t))
996 if (mentions_vars_p_type (t))
997 return true;
999 else if (EXPR_P (t))
1001 if (mentions_vars_p_expr (t))
1002 return true;
1004 else if (CONSTANT_CLASS_P (t))
1005 CHECK_NO_VAR (TREE_TYPE (t));
1006 else
1007 gcc_unreachable ();
1009 return false;
1013 /* Return the resolution for the decl with index INDEX from DATA_IN. */
1015 static enum ld_plugin_symbol_resolution
1016 get_resolution (struct data_in *data_in, unsigned index)
1018 if (data_in->globals_resolution.exists ())
1020 ld_plugin_symbol_resolution_t ret;
1021 /* We can have references to not emitted functions in
1022 DECL_FUNCTION_PERSONALITY at least. So we can and have
1023 to indeed return LDPR_UNKNOWN in some cases. */
1024 if (data_in->globals_resolution.length () <= index)
1025 return LDPR_UNKNOWN;
1026 ret = data_in->globals_resolution[index];
1027 return ret;
1029 else
1030 /* Delay resolution finding until decl merging. */
1031 return LDPR_UNKNOWN;
1034 /* We need to record resolutions until symbol table is read. */
1035 static void
1036 register_resolution (struct lto_file_decl_data *file_data, tree decl,
1037 enum ld_plugin_symbol_resolution resolution)
1039 if (resolution == LDPR_UNKNOWN)
1040 return;
1041 if (!file_data->resolution_map)
1042 file_data->resolution_map
1043 = new hash_map<tree, ld_plugin_symbol_resolution>;
1044 file_data->resolution_map->put (decl, resolution);
1047 /* Register DECL with the global symbol table and change its
1048 name if necessary to avoid name clashes for static globals across
1049 different files. */
1051 static void
1052 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
1053 unsigned ix)
1055 tree context;
1057 /* Variable has file scope, not local. */
1058 if (!TREE_PUBLIC (decl)
1059 && !((context = decl_function_context (decl))
1060 && auto_var_in_fn_p (decl, context)))
1061 rest_of_decl_compilation (decl, 1, 0);
1063 /* If this variable has already been declared, queue the
1064 declaration for merging. */
1065 if (TREE_PUBLIC (decl))
1066 register_resolution (data_in->file_data,
1067 decl, get_resolution (data_in, ix));
1071 /* Register DECL with the global symbol table and change its
1072 name if necessary to avoid name clashes for static globals across
1073 different files. DATA_IN contains descriptors and tables for the
1074 file being read. */
1076 static void
1077 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
1078 unsigned ix)
1080 /* If this variable has already been declared, queue the
1081 declaration for merging. */
1082 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
1083 register_resolution (data_in->file_data,
1084 decl, get_resolution (data_in, ix));
1088 /* For the type T re-materialize it in the type variant list and
1089 the pointer/reference-to chains. */
1091 static void
1092 lto_fixup_prevailing_type (tree t)
1094 /* The following re-creates proper variant lists while fixing up
1095 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1096 variant list state before fixup is broken. */
1098 /* If we are not our own variant leader link us into our new leaders
1099 variant list. */
1100 if (TYPE_MAIN_VARIANT (t) != t)
1102 tree mv = TYPE_MAIN_VARIANT (t);
1103 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1104 TYPE_NEXT_VARIANT (mv) = t;
1107 /* The following reconstructs the pointer chains
1108 of the new pointed-to type if we are a main variant. We do
1109 not stream those so they are broken before fixup. */
1110 if (TREE_CODE (t) == POINTER_TYPE
1111 && TYPE_MAIN_VARIANT (t) == t)
1113 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1114 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1116 else if (TREE_CODE (t) == REFERENCE_TYPE
1117 && TYPE_MAIN_VARIANT (t) == t)
1119 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1120 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1125 /* We keep prevailing tree SCCs in a hashtable with manual collision
1126 handling (in case all hashes compare the same) and keep the colliding
1127 entries in the tree_scc->next chain. */
1129 struct tree_scc
1131 tree_scc *next;
1132 /* Hash of the whole SCC. */
1133 hashval_t hash;
1134 /* Number of trees in the SCC. */
1135 unsigned len;
1136 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1137 which share the same individual tree hash). */
1138 unsigned entry_len;
1139 /* The members of the SCC.
1140 We only need to remember the first entry node candidate for prevailing
1141 SCCs (but of course have access to all entries for SCCs we are
1142 processing).
1143 ??? For prevailing SCCs we really only need hash and the first
1144 entry candidate, but that's too awkward to implement. */
1145 tree entries[1];
1148 struct tree_scc_hasher : typed_noop_remove <tree_scc>
1150 typedef tree_scc *value_type;
1151 typedef tree_scc *compare_type;
1152 static inline hashval_t hash (const tree_scc *);
1153 static inline bool equal (const tree_scc *, const tree_scc *);
1156 hashval_t
1157 tree_scc_hasher::hash (const tree_scc *scc)
1159 return scc->hash;
1162 bool
1163 tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
1165 if (scc1->hash != scc2->hash
1166 || scc1->len != scc2->len
1167 || scc1->entry_len != scc2->entry_len)
1168 return false;
1169 return true;
1172 static hash_table<tree_scc_hasher> *tree_scc_hash;
1173 static struct obstack tree_scc_hash_obstack;
1175 static unsigned long num_merged_types;
1176 static unsigned long num_prevailing_types;
1177 static unsigned long num_type_scc_trees;
1178 static unsigned long total_scc_size;
1179 static unsigned long num_sccs_read;
1180 static unsigned long total_scc_size_merged;
1181 static unsigned long num_sccs_merged;
1182 static unsigned long num_scc_compares;
1183 static unsigned long num_scc_compare_collisions;
1186 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1187 recursing through in-SCC tree edges. Returns true if the SCCs entered
1188 through T1 and T2 are equal and fills in *MAP with the pairs of
1189 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1191 static bool
1192 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1194 enum tree_code code;
1196 /* Mark already visited nodes. */
1197 TREE_ASM_WRITTEN (t2) = 1;
1199 /* Push the pair onto map. */
1200 (*map)[0] = t1;
1201 (*map)[1] = t2;
1202 *map = *map + 2;
1204 /* Compare value-fields. */
1205 #define compare_values(X) \
1206 do { \
1207 if (X(t1) != X(t2)) \
1208 return false; \
1209 } while (0)
1211 compare_values (TREE_CODE);
1212 code = TREE_CODE (t1);
1214 if (!TYPE_P (t1))
1216 compare_values (TREE_SIDE_EFFECTS);
1217 compare_values (TREE_CONSTANT);
1218 compare_values (TREE_READONLY);
1219 compare_values (TREE_PUBLIC);
1221 compare_values (TREE_ADDRESSABLE);
1222 compare_values (TREE_THIS_VOLATILE);
1223 if (DECL_P (t1))
1224 compare_values (DECL_UNSIGNED);
1225 else if (TYPE_P (t1))
1226 compare_values (TYPE_UNSIGNED);
1227 if (TYPE_P (t1))
1228 compare_values (TYPE_ARTIFICIAL);
1229 else
1230 compare_values (TREE_NO_WARNING);
1231 compare_values (TREE_NOTHROW);
1232 compare_values (TREE_STATIC);
1233 if (code != TREE_BINFO)
1234 compare_values (TREE_PRIVATE);
1235 compare_values (TREE_PROTECTED);
1236 compare_values (TREE_DEPRECATED);
1237 if (TYPE_P (t1))
1239 if (AGGREGATE_TYPE_P (t1))
1240 compare_values (TYPE_REVERSE_STORAGE_ORDER);
1241 else
1242 compare_values (TYPE_SATURATING);
1243 compare_values (TYPE_ADDR_SPACE);
1245 else if (code == SSA_NAME)
1246 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1248 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1250 if (!wi::eq_p (t1, t2))
1251 return false;
1254 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1256 /* ??? No suitable compare routine available. */
1257 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1258 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1259 if (r1.cl != r2.cl
1260 || r1.decimal != r2.decimal
1261 || r1.sign != r2.sign
1262 || r1.signalling != r2.signalling
1263 || r1.canonical != r2.canonical
1264 || r1.uexp != r2.uexp)
1265 return false;
1266 for (unsigned i = 0; i < SIGSZ; ++i)
1267 if (r1.sig[i] != r2.sig[i])
1268 return false;
1271 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1272 if (!fixed_compare (EQ_EXPR,
1273 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1274 return false;
1277 /* We don't want to compare locations, so there is nothing do compare
1278 for TS_DECL_MINIMAL. */
1280 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1282 compare_values (DECL_MODE);
1283 compare_values (DECL_NONLOCAL);
1284 compare_values (DECL_VIRTUAL_P);
1285 compare_values (DECL_IGNORED_P);
1286 compare_values (DECL_ABSTRACT_P);
1287 compare_values (DECL_ARTIFICIAL);
1288 compare_values (DECL_USER_ALIGN);
1289 compare_values (DECL_PRESERVE_P);
1290 compare_values (DECL_EXTERNAL);
1291 compare_values (DECL_GIMPLE_REG_P);
1292 compare_values (DECL_ALIGN);
1293 if (code == LABEL_DECL)
1295 compare_values (EH_LANDING_PAD_NR);
1296 compare_values (LABEL_DECL_UID);
1298 else if (code == FIELD_DECL)
1300 compare_values (DECL_PACKED);
1301 compare_values (DECL_NONADDRESSABLE_P);
1302 compare_values (DECL_OFFSET_ALIGN);
1304 else if (code == VAR_DECL)
1306 compare_values (DECL_HAS_DEBUG_EXPR_P);
1307 compare_values (DECL_NONLOCAL_FRAME);
1309 if (code == RESULT_DECL
1310 || code == PARM_DECL
1311 || code == VAR_DECL)
1313 compare_values (DECL_BY_REFERENCE);
1314 if (code == VAR_DECL
1315 || code == PARM_DECL)
1316 compare_values (DECL_HAS_VALUE_EXPR_P);
1320 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1321 compare_values (DECL_REGISTER);
1323 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1325 compare_values (DECL_COMMON);
1326 compare_values (DECL_DLLIMPORT_P);
1327 compare_values (DECL_WEAK);
1328 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1329 compare_values (DECL_COMDAT);
1330 compare_values (DECL_VISIBILITY);
1331 compare_values (DECL_VISIBILITY_SPECIFIED);
1332 if (code == VAR_DECL)
1334 compare_values (DECL_HARD_REGISTER);
1335 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1336 compare_values (DECL_IN_CONSTANT_POOL);
1340 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1342 compare_values (DECL_BUILT_IN_CLASS);
1343 compare_values (DECL_STATIC_CONSTRUCTOR);
1344 compare_values (DECL_STATIC_DESTRUCTOR);
1345 compare_values (DECL_UNINLINABLE);
1346 compare_values (DECL_POSSIBLY_INLINED);
1347 compare_values (DECL_IS_NOVOPS);
1348 compare_values (DECL_IS_RETURNS_TWICE);
1349 compare_values (DECL_IS_MALLOC);
1350 compare_values (DECL_IS_OPERATOR_NEW);
1351 compare_values (DECL_DECLARED_INLINE_P);
1352 compare_values (DECL_STATIC_CHAIN);
1353 compare_values (DECL_NO_INLINE_WARNING_P);
1354 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1355 compare_values (DECL_NO_LIMIT_STACK);
1356 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1357 compare_values (DECL_PURE_P);
1358 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1359 compare_values (DECL_FINAL_P);
1360 compare_values (DECL_CXX_CONSTRUCTOR_P);
1361 compare_values (DECL_CXX_DESTRUCTOR_P);
1362 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1363 compare_values (DECL_FUNCTION_CODE);
1366 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1368 compare_values (TYPE_MODE);
1369 compare_values (TYPE_STRING_FLAG);
1370 compare_values (TYPE_NO_FORCE_BLK);
1371 compare_values (TYPE_NEEDS_CONSTRUCTING);
1372 if (RECORD_OR_UNION_TYPE_P (t1))
1374 compare_values (TYPE_TRANSPARENT_AGGR);
1375 compare_values (TYPE_FINAL_P);
1377 else if (code == ARRAY_TYPE)
1378 compare_values (TYPE_NONALIASED_COMPONENT);
1379 compare_values (TYPE_PACKED);
1380 compare_values (TYPE_RESTRICT);
1381 compare_values (TYPE_USER_ALIGN);
1382 compare_values (TYPE_READONLY);
1383 compare_values (TYPE_PRECISION);
1384 compare_values (TYPE_ALIGN);
1385 compare_values (TYPE_ALIAS_SET);
1388 /* We don't want to compare locations, so there is nothing do compare
1389 for TS_EXP. */
1391 /* BLOCKs are function local and we don't merge anything there, so
1392 simply refuse to merge. */
1393 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1394 return false;
1396 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1397 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1398 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1399 return false;
1401 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1402 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1403 return false;
1405 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1406 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1407 sizeof (struct cl_optimization)) != 0)
1408 return false;
1410 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1411 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1412 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1413 return false;
1415 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1416 compare_values (CONSTRUCTOR_NELTS);
1418 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1419 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1420 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1421 IDENTIFIER_LENGTH (t1)) != 0)
1422 return false;
1424 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1425 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1426 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1427 TREE_STRING_LENGTH (t1)) != 0)
1428 return false;
1430 if (code == OMP_CLAUSE)
1432 compare_values (OMP_CLAUSE_CODE);
1433 switch (OMP_CLAUSE_CODE (t1))
1435 case OMP_CLAUSE_DEFAULT:
1436 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1437 break;
1438 case OMP_CLAUSE_SCHEDULE:
1439 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1440 break;
1441 case OMP_CLAUSE_DEPEND:
1442 compare_values (OMP_CLAUSE_DEPEND_KIND);
1443 break;
1444 case OMP_CLAUSE_MAP:
1445 compare_values (OMP_CLAUSE_MAP_KIND);
1446 break;
1447 case OMP_CLAUSE_PROC_BIND:
1448 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1449 break;
1450 case OMP_CLAUSE_REDUCTION:
1451 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1452 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1453 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1454 break;
1455 default:
1456 break;
1460 #undef compare_values
1463 /* Compare pointer fields. */
1465 /* Recurse. Search & Replaced from DFS_write_tree_body.
1466 Folding the early checks into the compare_tree_edges recursion
1467 macro makes debugging way quicker as you are able to break on
1468 compare_tree_sccs_1 and simply finish until a call returns false
1469 to spot the SCC members with the difference. */
1470 #define compare_tree_edges(E1, E2) \
1471 do { \
1472 tree t1_ = (E1), t2_ = (E2); \
1473 if (t1_ != t2_ \
1474 && (!t1_ || !t2_ \
1475 || !TREE_VISITED (t2_) \
1476 || (!TREE_ASM_WRITTEN (t2_) \
1477 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1478 return false; \
1479 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1480 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1481 } while (0)
1483 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1485 if (code != IDENTIFIER_NODE)
1486 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1489 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1491 unsigned i;
1492 /* Note that the number of elements for EXPR has already been emitted
1493 in EXPR's header (see streamer_write_tree_header). */
1494 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1495 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1498 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1500 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1501 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1504 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1506 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1507 /* ??? Global decls from different TUs have non-matching
1508 TRANSLATION_UNIT_DECLs. Only consider a small set of
1509 decls equivalent, we should not end up merging others. */
1510 if ((code == TYPE_DECL
1511 || code == NAMESPACE_DECL
1512 || code == IMPORTED_DECL
1513 || code == CONST_DECL
1514 || (VAR_OR_FUNCTION_DECL_P (t1)
1515 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1516 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1518 else
1519 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1522 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1524 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1525 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1526 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1527 if ((code == VAR_DECL
1528 || code == PARM_DECL)
1529 && DECL_HAS_VALUE_EXPR_P (t1))
1530 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1531 if (code == VAR_DECL
1532 && DECL_HAS_DEBUG_EXPR_P (t1))
1533 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1534 /* LTO specific edges. */
1535 if (code != FUNCTION_DECL
1536 && code != TRANSLATION_UNIT_DECL)
1537 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1540 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1542 if (code == FUNCTION_DECL)
1544 tree a1, a2;
1545 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1546 a1 || a2;
1547 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1548 compare_tree_edges (a1, a2);
1549 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1551 else if (code == TYPE_DECL)
1552 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1555 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1557 /* Make sure we don't inadvertently set the assembler name. */
1558 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1559 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1560 DECL_ASSEMBLER_NAME (t2));
1563 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1565 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1566 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1567 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1568 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1569 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1570 DECL_FIELD_BIT_OFFSET (t2));
1571 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1574 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1576 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1577 DECL_FUNCTION_PERSONALITY (t2));
1578 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1579 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1580 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1581 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1582 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1585 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1587 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1588 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1589 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1590 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1591 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1592 reconstructed during fixup. */
1593 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1594 during fixup. */
1595 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1596 /* ??? Global types from different TUs have non-matching
1597 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1598 equal. */
1599 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1601 else
1602 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1603 /* TYPE_CANONICAL is re-computed during type merging, so do not
1604 compare it here. */
1605 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1608 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1610 if (code == ENUMERAL_TYPE)
1611 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1612 else if (code == ARRAY_TYPE)
1613 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1614 else if (RECORD_OR_UNION_TYPE_P (t1))
1616 tree f1, f2;
1617 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1618 f1 || f2;
1619 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1620 compare_tree_edges (f1, f2);
1621 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1623 else if (code == FUNCTION_TYPE
1624 || code == METHOD_TYPE)
1625 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1626 if (!POINTER_TYPE_P (t1))
1627 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1628 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1631 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1633 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1634 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1635 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1638 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1639 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1640 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1642 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1644 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1645 compare_tree_edges (TREE_OPERAND (t1, i),
1646 TREE_OPERAND (t2, i));
1648 /* BLOCKs are function local and we don't merge anything there. */
1649 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1650 return false;
1653 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1655 unsigned i;
1656 tree t;
1657 /* Lengths have already been compared above. */
1658 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1659 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1660 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1661 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1662 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1663 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1664 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1665 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1666 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1669 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1671 unsigned i;
1672 tree index, value;
1673 /* Lengths have already been compared above. */
1674 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1676 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1677 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1681 if (code == OMP_CLAUSE)
1683 int i;
1685 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1686 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1687 OMP_CLAUSE_OPERAND (t2, i));
1688 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1691 #undef compare_tree_edges
1693 return true;
1696 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1697 out MAP if they are equal. */
1699 static bool
1700 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1701 tree *map)
1703 /* Assume SCC entry hashes are sorted after their cardinality. Which
1704 means we can simply take the first n-tuple of equal hashes
1705 (which is recorded as entry_len) and do n SCC entry candidate
1706 comparisons. */
1707 for (unsigned i = 0; i < pscc->entry_len; ++i)
1709 tree *mapp = map;
1710 num_scc_compare_collisions++;
1711 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1713 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1714 on the scc as all trees will be freed. */
1715 return true;
1717 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1718 the SCC prevails. */
1719 for (unsigned j = 0; j < scc->len; ++j)
1720 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1723 return false;
1726 /* QSort sort function to sort a map of two pointers after the 2nd
1727 pointer. */
1729 static int
1730 cmp_tree (const void *p1_, const void *p2_)
1732 tree *p1 = (tree *)(const_cast<void *>(p1_));
1733 tree *p2 = (tree *)(const_cast<void *>(p2_));
1734 if (p1[1] == p2[1])
1735 return 0;
1736 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1739 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1740 hash value SCC_HASH with an already recorded SCC. Return true if
1741 that was successful, otherwise return false. */
1743 static bool
1744 unify_scc (struct data_in *data_in, unsigned from,
1745 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1747 bool unified_p = false;
1748 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1749 tree_scc *scc
1750 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1751 scc->next = NULL;
1752 scc->hash = scc_hash;
1753 scc->len = len;
1754 scc->entry_len = scc_entry_len;
1755 for (unsigned i = 0; i < len; ++i)
1757 tree t = streamer_tree_cache_get_tree (cache, from + i);
1758 scc->entries[i] = t;
1759 /* Do not merge SCCs with local entities inside them. Also do
1760 not merge TRANSLATION_UNIT_DECLs. */
1761 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1762 || (VAR_OR_FUNCTION_DECL_P (t)
1763 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1764 || TREE_CODE (t) == LABEL_DECL)
1766 /* Avoid doing any work for these cases and do not worry to
1767 record the SCCs for further merging. */
1768 return false;
1772 /* Look for the list of candidate SCCs to compare against. */
1773 tree_scc **slot;
1774 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1775 if (*slot)
1777 /* Try unifying against each candidate. */
1778 num_scc_compares++;
1780 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1781 outside of the scc when following tree edges. Make sure
1782 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1783 to track whether we visited the SCC member during the compare.
1784 We cannot use TREE_VISITED on the pscc members as the extended
1785 scc and pscc can overlap. */
1786 for (unsigned i = 0; i < scc->len; ++i)
1788 TREE_VISITED (scc->entries[i]) = 1;
1789 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1792 tree *map = XALLOCAVEC (tree, 2 * len);
1793 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1795 if (!compare_tree_sccs (pscc, scc, map))
1796 continue;
1798 /* Found an equal SCC. */
1799 unified_p = true;
1800 num_scc_compare_collisions--;
1801 num_sccs_merged++;
1802 total_scc_size_merged += len;
1804 #ifdef ENABLE_CHECKING
1805 for (unsigned i = 0; i < len; ++i)
1807 tree t = map[2*i+1];
1808 enum tree_code code = TREE_CODE (t);
1809 /* IDENTIFIER_NODEs should be singletons and are merged by the
1810 streamer. The others should be singletons, too, and we
1811 should not merge them in any way. */
1812 gcc_assert (code != TRANSLATION_UNIT_DECL
1813 && code != IDENTIFIER_NODE
1814 && !streamer_handle_as_builtin_p (t));
1816 #endif
1818 /* Fixup the streamer cache with the prevailing nodes according
1819 to the tree node mapping computed by compare_tree_sccs. */
1820 if (len == 1)
1821 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1822 else
1824 tree *map2 = XALLOCAVEC (tree, 2 * len);
1825 for (unsigned i = 0; i < len; ++i)
1827 map2[i*2] = (tree)(uintptr_t)(from + i);
1828 map2[i*2+1] = scc->entries[i];
1830 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1831 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1832 for (unsigned i = 0; i < len; ++i)
1833 streamer_tree_cache_replace_tree (cache, map[2*i],
1834 (uintptr_t)map2[2*i]);
1837 /* Free the tree nodes from the read SCC. */
1838 data_in->location_cache.revert_location_cache ();
1839 for (unsigned i = 0; i < len; ++i)
1841 enum tree_code code;
1842 if (TYPE_P (scc->entries[i]))
1843 num_merged_types++;
1844 code = TREE_CODE (scc->entries[i]);
1845 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1846 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1847 ggc_free (scc->entries[i]);
1850 break;
1853 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1854 if (!unified_p)
1855 for (unsigned i = 0; i < scc->len; ++i)
1856 TREE_VISITED (scc->entries[i]) = 0;
1859 /* If we didn't unify it to any candidate duplicate the relevant
1860 pieces to permanent storage and link it into the chain. */
1861 if (!unified_p)
1863 tree_scc *pscc
1864 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1865 memcpy (pscc, scc, sizeof (tree_scc));
1866 pscc->next = (*slot);
1867 *slot = pscc;
1869 return unified_p;
1873 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1874 RESOLUTIONS is the set of symbols picked by the linker (read from the
1875 resolution file when the linker plugin is being used). */
1877 static void
1878 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1879 vec<ld_plugin_symbol_resolution_t> resolutions)
1881 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1882 const int decl_offset = sizeof (struct lto_decl_header);
1883 const int main_offset = decl_offset + header->decl_state_size;
1884 const int string_offset = main_offset + header->main_size;
1885 struct data_in *data_in;
1886 unsigned int i;
1887 const uint32_t *data_ptr, *data_end;
1888 uint32_t num_decl_states;
1890 lto_input_block ib_main ((const char *) data + main_offset,
1891 header->main_size, decl_data->mode_table);
1893 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1894 header->string_size, resolutions);
1896 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1897 internal types that should not be merged. */
1899 /* Read the global declarations and types. */
1900 while (ib_main.p < ib_main.len)
1902 tree t;
1903 unsigned from = data_in->reader_cache->nodes.length ();
1904 /* Read and uniquify SCCs as in the input stream. */
1905 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1906 if (tag == LTO_tree_scc)
1908 unsigned len_;
1909 unsigned scc_entry_len;
1910 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1911 &scc_entry_len);
1912 unsigned len = data_in->reader_cache->nodes.length () - from;
1913 gcc_assert (len == len_);
1915 total_scc_size += len;
1916 num_sccs_read++;
1918 /* We have the special case of size-1 SCCs that are pre-merged
1919 by means of identifier and string sharing for example.
1920 ??? Maybe we should avoid streaming those as SCCs. */
1921 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1922 from);
1923 if (len == 1
1924 && (TREE_CODE (first) == IDENTIFIER_NODE
1925 || TREE_CODE (first) == INTEGER_CST
1926 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1927 || streamer_handle_as_builtin_p (first)))
1928 continue;
1930 /* Try to unify the SCC with already existing ones. */
1931 if (!flag_ltrans
1932 && unify_scc (data_in, from,
1933 len, scc_entry_len, scc_hash))
1934 continue;
1936 /* Tree merging failed, mark entries in location cache as
1937 permanent. */
1938 data_in->location_cache.accept_location_cache ();
1940 bool seen_type = false;
1941 for (unsigned i = 0; i < len; ++i)
1943 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1944 from + i);
1945 /* Reconstruct the type variant and pointer-to/reference-to
1946 chains. */
1947 if (TYPE_P (t))
1949 seen_type = true;
1950 num_prevailing_types++;
1951 lto_fixup_prevailing_type (t);
1953 /* Compute the canonical type of all types.
1954 ??? Should be able to assert that !TYPE_CANONICAL. */
1955 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1957 gimple_register_canonical_type (t);
1958 if (odr_type_p (t))
1959 register_odr_type (t);
1961 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1962 type which is also member of this SCC. */
1963 if (TREE_CODE (t) == INTEGER_CST
1964 && !TREE_OVERFLOW (t))
1965 cache_integer_cst (t);
1966 /* Register TYPE_DECLs with the debuginfo machinery. */
1967 if (!flag_wpa
1968 && TREE_CODE (t) == TYPE_DECL)
1970 /* Dwarf2out needs location information.
1971 TODO: Moving this out of the streamer loop may noticealy
1972 improve ltrans linemap memory use. */
1973 data_in->location_cache.apply_location_cache ();
1974 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1976 if (!flag_ltrans)
1978 /* Register variables and functions with the
1979 symbol table. */
1980 if (TREE_CODE (t) == VAR_DECL)
1981 lto_register_var_decl_in_symtab (data_in, t, from + i);
1982 else if (TREE_CODE (t) == FUNCTION_DECL
1983 && !DECL_BUILT_IN (t))
1984 lto_register_function_decl_in_symtab (data_in, t, from + i);
1985 /* Scan the tree for references to global functions or
1986 variables and record those for later fixup. */
1987 if (mentions_vars_p (t))
1988 vec_safe_push (tree_with_vars, t);
1991 if (seen_type)
1992 num_type_scc_trees += len;
1994 else
1996 /* Pickle stray references. */
1997 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1998 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
2001 data_in->location_cache.apply_location_cache ();
2003 /* Read in lto_in_decl_state objects. */
2004 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
2005 data_end =
2006 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
2007 num_decl_states = *data_ptr++;
2009 gcc_assert (num_decl_states > 0);
2010 decl_data->global_decl_state = lto_new_in_decl_state ();
2011 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
2012 decl_data->global_decl_state);
2014 /* Read in per-function decl states and enter them in hash table. */
2015 decl_data->function_decl_states =
2016 hash_table<decl_state_hasher>::create_ggc (37);
2018 for (i = 1; i < num_decl_states; i++)
2020 struct lto_in_decl_state *state = lto_new_in_decl_state ();
2022 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
2023 lto_in_decl_state **slot
2024 = decl_data->function_decl_states->find_slot (state, INSERT);
2025 gcc_assert (*slot == NULL);
2026 *slot = state;
2029 if (data_ptr != data_end)
2030 internal_error ("bytecode stream: garbage at the end of symbols section");
2032 /* Set the current decl state to be the global state. */
2033 decl_data->current_decl_state = decl_data->global_decl_state;
2035 lto_data_in_delete (data_in);
2038 /* Custom version of strtoll, which is not portable. */
2040 static int64_t
2041 lto_parse_hex (const char *p)
2043 int64_t ret = 0;
2045 for (; *p != '\0'; ++p)
2047 char c = *p;
2048 unsigned char part;
2049 ret <<= 4;
2050 if (c >= '0' && c <= '9')
2051 part = c - '0';
2052 else if (c >= 'a' && c <= 'f')
2053 part = c - 'a' + 10;
2054 else if (c >= 'A' && c <= 'F')
2055 part = c - 'A' + 10;
2056 else
2057 internal_error ("could not parse hex number");
2058 ret |= part;
2061 return ret;
2064 /* Read resolution for file named FILE_NAME. The resolution is read from
2065 RESOLUTION. */
2067 static void
2068 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2070 /* We require that objects in the resolution file are in the same
2071 order as the lto1 command line. */
2072 unsigned int name_len;
2073 char *obj_name;
2074 unsigned int num_symbols;
2075 unsigned int i;
2076 struct lto_file_decl_data *file_data;
2077 splay_tree_node nd = NULL;
2079 if (!resolution)
2080 return;
2082 name_len = strlen (file->filename);
2083 obj_name = XNEWVEC (char, name_len + 1);
2084 fscanf (resolution, " "); /* Read white space. */
2086 fread (obj_name, sizeof (char), name_len, resolution);
2087 obj_name[name_len] = '\0';
2088 if (filename_cmp (obj_name, file->filename) != 0)
2089 internal_error ("unexpected file name %s in linker resolution file. "
2090 "Expected %s", obj_name, file->filename);
2091 if (file->offset != 0)
2093 int t;
2094 char offset_p[17];
2095 int64_t offset;
2096 t = fscanf (resolution, "@0x%16s", offset_p);
2097 if (t != 1)
2098 internal_error ("could not parse file offset");
2099 offset = lto_parse_hex (offset_p);
2100 if (offset != file->offset)
2101 internal_error ("unexpected offset");
2104 free (obj_name);
2106 fscanf (resolution, "%u", &num_symbols);
2108 for (i = 0; i < num_symbols; i++)
2110 int t;
2111 unsigned index;
2112 unsigned HOST_WIDE_INT id;
2113 char r_str[27];
2114 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2115 unsigned int j;
2116 unsigned int lto_resolution_str_len =
2117 sizeof (lto_resolution_str) / sizeof (char *);
2118 res_pair rp;
2120 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
2121 &index, &id, r_str);
2122 if (t != 3)
2123 internal_error ("invalid line in the resolution file");
2125 for (j = 0; j < lto_resolution_str_len; j++)
2127 if (strcmp (lto_resolution_str[j], r_str) == 0)
2129 r = (enum ld_plugin_symbol_resolution) j;
2130 break;
2133 if (j == lto_resolution_str_len)
2134 internal_error ("invalid resolution in the resolution file");
2136 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2138 nd = lto_splay_tree_lookup (file_ids, id);
2139 if (nd == NULL)
2140 internal_error ("resolution sub id %wx not in object file", id);
2143 file_data = (struct lto_file_decl_data *)nd->value;
2144 /* The indexes are very sparse. To save memory save them in a compact
2145 format that is only unpacked later when the subfile is processed. */
2146 rp.res = r;
2147 rp.index = index;
2148 file_data->respairs.safe_push (rp);
2149 if (file_data->max_index < index)
2150 file_data->max_index = index;
2154 /* List of file_decl_datas */
2155 struct file_data_list
2157 struct lto_file_decl_data *first, *last;
2160 /* Is the name for a id'ed LTO section? */
2162 static int
2163 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2165 const char *s;
2167 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
2168 return 0;
2169 s = strrchr (name, '.');
2170 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2173 /* Create file_data of each sub file id */
2175 static int
2176 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2177 struct file_data_list *list)
2179 struct lto_section_slot s_slot, *new_slot;
2180 unsigned HOST_WIDE_INT id;
2181 splay_tree_node nd;
2182 void **hash_slot;
2183 char *new_name;
2184 struct lto_file_decl_data *file_data;
2186 if (!lto_section_with_id (ls->name, &id))
2187 return 1;
2189 /* Find hash table of sub module id */
2190 nd = lto_splay_tree_lookup (file_ids, id);
2191 if (nd != NULL)
2193 file_data = (struct lto_file_decl_data *)nd->value;
2195 else
2197 file_data = ggc_alloc<lto_file_decl_data> ();
2198 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2199 file_data->id = id;
2200 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
2201 lto_splay_tree_insert (file_ids, id, file_data);
2203 /* Maintain list in linker order */
2204 if (!list->first)
2205 list->first = file_data;
2206 if (list->last)
2207 list->last->next = file_data;
2208 list->last = file_data;
2211 /* Copy section into sub module hash table */
2212 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2213 s_slot.name = new_name;
2214 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2215 gcc_assert (*hash_slot == NULL);
2217 new_slot = XDUP (struct lto_section_slot, ls);
2218 new_slot->name = new_name;
2219 *hash_slot = new_slot;
2220 return 1;
2223 /* Read declarations and other initializations for a FILE_DATA. */
2225 static void
2226 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2228 const char *data;
2229 size_t len;
2230 vec<ld_plugin_symbol_resolution_t>
2231 resolutions = vNULL;
2232 int i;
2233 res_pair *rp;
2235 /* Create vector for fast access of resolution. We do this lazily
2236 to save memory. */
2237 resolutions.safe_grow_cleared (file_data->max_index + 1);
2238 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2239 resolutions[rp->index] = rp->res;
2240 file_data->respairs.release ();
2242 file_data->renaming_hash_table = lto_create_renaming_table ();
2243 file_data->file_name = file->filename;
2244 #ifdef ACCEL_COMPILER
2245 lto_input_mode_table (file_data);
2246 #else
2247 file_data->mode_table = lto_mode_identity_table;
2248 #endif
2249 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2250 if (data == NULL)
2252 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2253 return;
2255 /* Frees resolutions */
2256 lto_read_decls (file_data, data, resolutions);
2257 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2260 /* Finalize FILE_DATA in FILE and increase COUNT. */
2262 static int
2263 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2264 int *count)
2266 lto_file_finalize (file_data, file);
2267 if (symtab->dump_file)
2268 fprintf (symtab->dump_file,
2269 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2270 file_data->file_name, file_data->id);
2271 (*count)++;
2272 return 0;
2275 /* Generate a TREE representation for all types and external decls
2276 entities in FILE.
2278 Read all of the globals out of the file. Then read the cgraph
2279 and process the .o index into the cgraph nodes so that it can open
2280 the .o file to load the functions and ipa information. */
2282 static struct lto_file_decl_data *
2283 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2285 struct lto_file_decl_data *file_data = NULL;
2286 splay_tree file_ids;
2287 htab_t section_hash_table;
2288 struct lto_section_slot *section;
2289 struct file_data_list file_list;
2290 struct lto_section_list section_list;
2292 memset (&section_list, 0, sizeof (struct lto_section_list));
2293 section_hash_table = lto_obj_build_section_table (file, &section_list);
2295 /* Find all sub modules in the object and put their sections into new hash
2296 tables in a splay tree. */
2297 file_ids = lto_splay_tree_new ();
2298 memset (&file_list, 0, sizeof (struct file_data_list));
2299 for (section = section_list.first; section != NULL; section = section->next)
2300 create_subid_section_table (section, file_ids, &file_list);
2302 /* Add resolutions to file ids */
2303 lto_resolution_read (file_ids, resolution_file, file);
2305 /* Finalize each lto file for each submodule in the merged object */
2306 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2307 lto_create_files_from_ids (file, file_data, count);
2309 splay_tree_delete (file_ids);
2310 htab_delete (section_hash_table);
2312 return file_list.first;
2315 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2316 #define LTO_MMAP_IO 1
2317 #endif
2319 #if LTO_MMAP_IO
2320 /* Page size of machine is used for mmap and munmap calls. */
2321 static size_t page_mask;
2322 #endif
2324 /* Get the section data of length LEN from FILENAME starting at
2325 OFFSET. The data segment must be freed by the caller when the
2326 caller is finished. Returns NULL if all was not well. */
2328 static char *
2329 lto_read_section_data (struct lto_file_decl_data *file_data,
2330 intptr_t offset, size_t len)
2332 char *result;
2333 static int fd = -1;
2334 static char *fd_name;
2335 #if LTO_MMAP_IO
2336 intptr_t computed_len;
2337 intptr_t computed_offset;
2338 intptr_t diff;
2339 #endif
2341 /* Keep a single-entry file-descriptor cache. The last file we
2342 touched will get closed at exit.
2343 ??? Eventually we want to add a more sophisticated larger cache
2344 or rather fix function body streaming to not stream them in
2345 practically random order. */
2346 if (fd != -1
2347 && filename_cmp (fd_name, file_data->file_name) != 0)
2349 free (fd_name);
2350 close (fd);
2351 fd = -1;
2353 if (fd == -1)
2355 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2356 if (fd == -1)
2358 fatal_error (input_location, "Cannot open %s", file_data->file_name);
2359 return NULL;
2361 fd_name = xstrdup (file_data->file_name);
2364 #if LTO_MMAP_IO
2365 if (!page_mask)
2367 size_t page_size = sysconf (_SC_PAGE_SIZE);
2368 page_mask = ~(page_size - 1);
2371 computed_offset = offset & page_mask;
2372 diff = offset - computed_offset;
2373 computed_len = len + diff;
2375 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2376 fd, computed_offset);
2377 if (result == MAP_FAILED)
2379 fatal_error (input_location, "Cannot map %s", file_data->file_name);
2380 return NULL;
2383 return result + diff;
2384 #else
2385 result = (char *) xmalloc (len);
2386 if (lseek (fd, offset, SEEK_SET) != offset
2387 || read (fd, result, len) != (ssize_t) len)
2389 free (result);
2390 fatal_error (input_location, "Cannot read %s", file_data->file_name);
2391 result = NULL;
2393 #ifdef __MINGW32__
2394 /* Native windows doesn't supports delayed unlink on opened file. So
2395 we close file here again. This produces higher I/O load, but at least
2396 it prevents to have dangling file handles preventing unlink. */
2397 free (fd_name);
2398 fd_name = NULL;
2399 close (fd);
2400 fd = -1;
2401 #endif
2402 return result;
2403 #endif
2407 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2408 NAME will be NULL unless the section type is for a function
2409 body. */
2411 static const char *
2412 get_section_data (struct lto_file_decl_data *file_data,
2413 enum lto_section_type section_type,
2414 const char *name,
2415 size_t *len)
2417 htab_t section_hash_table = file_data->section_hash_table;
2418 struct lto_section_slot *f_slot;
2419 struct lto_section_slot s_slot;
2420 const char *section_name = lto_get_section_name (section_type, name, file_data);
2421 char *data = NULL;
2423 *len = 0;
2424 s_slot.name = section_name;
2425 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2426 if (f_slot)
2428 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2429 *len = f_slot->len;
2432 free (CONST_CAST (char *, section_name));
2433 return data;
2437 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2438 starts at OFFSET and has LEN bytes. */
2440 static void
2441 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2442 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2443 const char *name ATTRIBUTE_UNUSED,
2444 const char *offset, size_t len ATTRIBUTE_UNUSED)
2446 #if LTO_MMAP_IO
2447 intptr_t computed_len;
2448 intptr_t computed_offset;
2449 intptr_t diff;
2450 #endif
2452 #if LTO_MMAP_IO
2453 computed_offset = ((intptr_t) offset) & page_mask;
2454 diff = (intptr_t) offset - computed_offset;
2455 computed_len = len + diff;
2457 munmap ((caddr_t) computed_offset, computed_len);
2458 #else
2459 free (CONST_CAST(char *, offset));
2460 #endif
2463 static lto_file *current_lto_file;
2465 /* Helper for qsort; compare partitions and return one with smaller size.
2466 We sort from greatest to smallest so parallel build doesn't stale on the
2467 longest compilation being executed too late. */
2469 static int
2470 cmp_partitions_size (const void *a, const void *b)
2472 const struct ltrans_partition_def *pa
2473 = *(struct ltrans_partition_def *const *)a;
2474 const struct ltrans_partition_def *pb
2475 = *(struct ltrans_partition_def *const *)b;
2476 return pb->insns - pa->insns;
2479 /* Helper for qsort; compare partitions and return one with smaller order. */
2481 static int
2482 cmp_partitions_order (const void *a, const void *b)
2484 const struct ltrans_partition_def *pa
2485 = *(struct ltrans_partition_def *const *)a;
2486 const struct ltrans_partition_def *pb
2487 = *(struct ltrans_partition_def *const *)b;
2488 int ordera = -1, orderb = -1;
2490 if (lto_symtab_encoder_size (pa->encoder))
2491 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2492 if (lto_symtab_encoder_size (pb->encoder))
2493 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2494 return orderb - ordera;
2497 /* Actually stream out ENCODER into TEMP_FILENAME. */
2499 static void
2500 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2502 lto_file *file = lto_obj_file_open (temp_filename, true);
2503 if (!file)
2504 fatal_error (input_location, "lto_obj_file_open() failed");
2505 lto_set_current_out_file (file);
2507 ipa_write_optimization_summaries (encoder);
2509 lto_set_current_out_file (NULL);
2510 lto_obj_file_close (file);
2511 free (file);
2514 /* Wait for forked process and signal errors. */
2515 #ifdef HAVE_WORKING_FORK
2516 static void
2517 wait_for_child ()
2519 int status;
2522 #ifndef WCONTINUED
2523 #define WCONTINUED 0
2524 #endif
2525 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2526 if (w == -1)
2527 fatal_error (input_location, "waitpid failed");
2529 if (WIFEXITED (status) && WEXITSTATUS (status))
2530 fatal_error (input_location, "streaming subprocess failed");
2531 else if (WIFSIGNALED (status))
2532 fatal_error (input_location,
2533 "streaming subprocess was killed by signal");
2535 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2537 #endif
2539 /* Stream out ENCODER into TEMP_FILENAME
2540 Fork if that seems to help. */
2542 static void
2543 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2544 bool ARG_UNUSED (last))
2546 #ifdef HAVE_WORKING_FORK
2547 static int nruns;
2549 if (lto_parallelism <= 1)
2551 do_stream_out (temp_filename, encoder);
2552 return;
2555 /* Do not run more than LTO_PARALLELISM streamings
2556 FIXME: we ignore limits on jobserver. */
2557 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2559 wait_for_child ();
2560 nruns --;
2562 /* If this is not the last parallel partition, execute new
2563 streaming process. */
2564 if (!last)
2566 pid_t cpid = fork ();
2568 if (!cpid)
2570 setproctitle ("lto1-wpa-streaming");
2571 do_stream_out (temp_filename, encoder);
2572 exit (0);
2574 /* Fork failed; lets do the job ourseleves. */
2575 else if (cpid == -1)
2576 do_stream_out (temp_filename, encoder);
2577 else
2578 nruns++;
2580 /* Last partition; stream it and wait for all children to die. */
2581 else
2583 int i;
2584 do_stream_out (temp_filename, encoder);
2585 for (i = 0; i < nruns; i++)
2586 wait_for_child ();
2588 asm_nodes_output = true;
2589 #else
2590 do_stream_out (temp_filename, encoder);
2591 #endif
2594 /* Write all output files in WPA mode and the file with the list of
2595 LTRANS units. */
2597 static void
2598 lto_wpa_write_files (void)
2600 unsigned i, n_sets;
2601 ltrans_partition part;
2602 FILE *ltrans_output_list_stream;
2603 char *temp_filename;
2604 vec <char *>temp_filenames = vNULL;
2605 size_t blen;
2607 /* Open the LTRANS output list. */
2608 if (!ltrans_output_list)
2609 fatal_error (input_location, "no LTRANS output list filename provided");
2611 timevar_push (TV_WHOPR_WPA);
2613 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2614 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2616 timevar_pop (TV_WHOPR_WPA);
2618 timevar_push (TV_WHOPR_WPA_IO);
2620 /* Generate a prefix for the LTRANS unit files. */
2621 blen = strlen (ltrans_output_list);
2622 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2623 strcpy (temp_filename, ltrans_output_list);
2624 if (blen > sizeof (".out")
2625 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2626 ".out") == 0)
2627 temp_filename[blen - sizeof (".out") + 1] = '\0';
2628 blen = strlen (temp_filename);
2630 n_sets = ltrans_partitions.length ();
2632 /* Sort partitions by size so small ones are compiled last.
2633 FIXME: Even when not reordering we may want to output one list for parallel make
2634 and other for final link command. */
2636 if (!flag_profile_reorder_functions || !flag_profile_use)
2637 ltrans_partitions.qsort (flag_toplevel_reorder
2638 ? cmp_partitions_size
2639 : cmp_partitions_order);
2641 for (i = 0; i < n_sets; i++)
2643 ltrans_partition part = ltrans_partitions[i];
2645 /* Write all the nodes in SET. */
2646 sprintf (temp_filename + blen, "%u.o", i);
2648 if (!quiet_flag)
2649 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2650 if (symtab->dump_file)
2652 lto_symtab_encoder_iterator lsei;
2654 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2655 part->name, temp_filename, part->insns);
2656 fprintf (symtab->dump_file, " Symbols in partition: ");
2657 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2658 lsei_next_in_partition (&lsei))
2660 symtab_node *node = lsei_node (lsei);
2661 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2663 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2664 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2665 lsei_next (&lsei))
2667 symtab_node *node = lsei_node (lsei);
2668 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2670 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2671 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2672 if (cnode
2673 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2674 fprintf (symtab->dump_file, "(body included)");
2675 else
2677 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2678 if (vnode
2679 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2680 fprintf (symtab->dump_file, "(initializer included)");
2684 fprintf (symtab->dump_file, "\n");
2686 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2688 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2690 part->encoder = NULL;
2692 temp_filenames.safe_push (xstrdup (temp_filename));
2694 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2695 if (ltrans_output_list_stream == NULL)
2696 fatal_error (input_location,
2697 "opening LTRANS output list %s: %m", ltrans_output_list);
2698 for (i = 0; i < n_sets; i++)
2700 unsigned int len = strlen (temp_filenames[i]);
2701 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2702 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2703 fatal_error (input_location, "writing to LTRANS output list %s: %m",
2704 ltrans_output_list);
2705 free (temp_filenames[i]);
2707 temp_filenames.release();
2709 lto_stats.num_output_files += n_sets;
2711 /* Close the LTRANS output list. */
2712 if (fclose (ltrans_output_list_stream))
2713 fatal_error (input_location,
2714 "closing LTRANS output list %s: %m", ltrans_output_list);
2716 free_ltrans_partitions();
2717 free (temp_filename);
2719 timevar_pop (TV_WHOPR_WPA_IO);
2723 /* If TT is a variable or function decl replace it with its
2724 prevailing variant. */
2725 #define LTO_SET_PREVAIL(tt) \
2726 do {\
2727 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2728 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2730 tt = lto_symtab_prevailing_decl (tt); \
2731 fixed = true; \
2733 } while (0)
2735 /* Ensure that TT isn't a replacable var of function decl. */
2736 #define LTO_NO_PREVAIL(tt) \
2737 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2739 /* Given a tree T replace all fields referring to variables or functions
2740 with their prevailing variant. */
2741 static void
2742 lto_fixup_prevailing_decls (tree t)
2744 enum tree_code code = TREE_CODE (t);
2745 bool fixed = false;
2747 gcc_checking_assert (code != TREE_BINFO);
2748 LTO_NO_PREVAIL (TREE_TYPE (t));
2749 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2750 LTO_NO_PREVAIL (TREE_CHAIN (t));
2751 if (DECL_P (t))
2753 LTO_NO_PREVAIL (DECL_NAME (t));
2754 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2755 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2757 LTO_SET_PREVAIL (DECL_SIZE (t));
2758 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2759 LTO_SET_PREVAIL (DECL_INITIAL (t));
2760 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2761 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2763 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2765 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2767 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2769 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2771 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2773 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2774 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2775 LTO_NO_PREVAIL (DECL_VINDEX (t));
2777 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2779 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2780 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2781 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2782 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2783 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2786 else if (TYPE_P (t))
2788 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2789 LTO_SET_PREVAIL (TYPE_SIZE (t));
2790 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2791 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2792 LTO_NO_PREVAIL (TYPE_NAME (t));
2794 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2795 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2796 LTO_NO_PREVAIL (t->type_non_common.binfo);
2798 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2800 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2801 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2802 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2804 else if (EXPR_P (t))
2806 int i;
2807 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2808 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2810 else if (TREE_CODE (t) == CONSTRUCTOR)
2812 unsigned i;
2813 tree val;
2814 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2815 LTO_SET_PREVAIL (val);
2817 else
2819 switch (code)
2821 case TREE_LIST:
2822 LTO_SET_PREVAIL (TREE_VALUE (t));
2823 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2824 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2825 break;
2826 default:
2827 gcc_unreachable ();
2830 /* If we fixed nothing, then we missed something seen by
2831 mentions_vars_p. */
2832 gcc_checking_assert (fixed);
2834 #undef LTO_SET_PREVAIL
2835 #undef LTO_NO_PREVAIL
2837 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2838 replaces var and function decls with the corresponding prevailing def. */
2840 static void
2841 lto_fixup_state (struct lto_in_decl_state *state)
2843 unsigned i, si;
2845 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2846 we still need to walk from all DECLs to find the reachable
2847 FUNCTION_DECLs and VAR_DECLs. */
2848 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2850 vec<tree, va_gc> *trees = state->streams[si];
2851 for (i = 0; i < vec_safe_length (trees); i++)
2853 tree t = (*trees)[i];
2854 #ifdef ENABLE_CHECKING
2855 if (TYPE_P (t))
2856 verify_type (t);
2857 #endif
2858 if (VAR_OR_FUNCTION_DECL_P (t)
2859 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2860 (*trees)[i] = lto_symtab_prevailing_decl (t);
2865 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2866 prevailing one. */
2868 static void
2869 lto_fixup_decls (struct lto_file_decl_data **files)
2871 unsigned int i;
2872 tree t;
2874 if (tree_with_vars)
2875 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2876 lto_fixup_prevailing_decls (t);
2878 for (i = 0; files[i]; i++)
2880 struct lto_file_decl_data *file = files[i];
2881 struct lto_in_decl_state *state = file->global_decl_state;
2882 lto_fixup_state (state);
2884 hash_table<decl_state_hasher>::iterator iter;
2885 lto_in_decl_state *elt;
2886 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2887 lto_in_decl_state *, iter)
2888 lto_fixup_state (elt);
2892 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2894 /* Turn file datas for sub files into a single array, so that they look
2895 like separate files for further passes. */
2897 static void
2898 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2900 struct lto_file_decl_data *n, *next;
2901 int i, k;
2903 lto_stats.num_input_files = count;
2904 all_file_decl_data
2905 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2906 /* Set the hooks so that all of the ipa passes can read in their data. */
2907 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2908 for (i = 0, k = 0; i < last_file_ix; i++)
2910 for (n = orig[i]; n != NULL; n = next)
2912 all_file_decl_data[k++] = n;
2913 next = n->next;
2914 n->next = NULL;
2917 all_file_decl_data[k] = NULL;
2918 gcc_assert (k == count);
2921 /* Input file data before flattening (i.e. splitting them to subfiles to support
2922 incremental linking. */
2923 static int real_file_count;
2924 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2926 static void print_lto_report_1 (void);
2928 /* Read all the symbols from the input files FNAMES. NFILES is the
2929 number of files requested in the command line. Instantiate a
2930 global call graph by aggregating all the sub-graphs found in each
2931 file. */
2933 static void
2934 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2936 unsigned int i, last_file_ix;
2937 FILE *resolution;
2938 int count = 0;
2939 struct lto_file_decl_data **decl_data;
2940 symtab_node *snode;
2942 symtab->initialize ();
2944 timevar_push (TV_IPA_LTO_DECL_IN);
2946 #ifdef ACCEL_COMPILER
2947 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2948 lto_stream_offload_p = true;
2949 #endif
2951 real_file_decl_data
2952 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2953 real_file_count = nfiles;
2955 /* Read the resolution file. */
2956 resolution = NULL;
2957 if (resolution_file_name)
2959 int t;
2960 unsigned num_objects;
2962 resolution = fopen (resolution_file_name, "r");
2963 if (resolution == NULL)
2964 fatal_error (input_location,
2965 "could not open symbol resolution file: %m");
2967 t = fscanf (resolution, "%u", &num_objects);
2968 gcc_assert (t == 1);
2970 /* True, since the plugin splits the archives. */
2971 gcc_assert (num_objects == nfiles);
2973 symtab->state = LTO_STREAMING;
2975 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2976 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2977 gimple_canonical_type_eq, NULL);
2978 gcc_obstack_init (&tree_scc_hash_obstack);
2979 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2981 /* Register the common node types with the canonical type machinery so
2982 we properly share alias-sets across languages and TUs. Do not
2983 expose the common nodes as type merge target - those that should be
2984 are already exposed so by pre-loading the LTO streamer caches.
2985 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2986 for (i = 0; i < itk_none; ++i)
2987 lto_register_canonical_types (integer_types[i], true);
2988 for (i = 0; i < stk_type_kind_last; ++i)
2989 lto_register_canonical_types (sizetype_tab[i], true);
2990 for (i = 0; i < TI_MAX; ++i)
2991 lto_register_canonical_types (global_trees[i], true);
2992 for (i = 0; i < itk_none; ++i)
2993 lto_register_canonical_types (integer_types[i], false);
2994 for (i = 0; i < stk_type_kind_last; ++i)
2995 lto_register_canonical_types (sizetype_tab[i], false);
2996 for (i = 0; i < TI_MAX; ++i)
2997 lto_register_canonical_types (global_trees[i], false);
2999 if (!quiet_flag)
3000 fprintf (stderr, "Reading object files:");
3002 /* Read all of the object files specified on the command line. */
3003 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
3005 struct lto_file_decl_data *file_data = NULL;
3006 if (!quiet_flag)
3008 fprintf (stderr, " %s", fnames[i]);
3009 fflush (stderr);
3012 current_lto_file = lto_obj_file_open (fnames[i], false);
3013 if (!current_lto_file)
3014 break;
3016 file_data = lto_file_read (current_lto_file, resolution, &count);
3017 if (!file_data)
3019 lto_obj_file_close (current_lto_file);
3020 free (current_lto_file);
3021 current_lto_file = NULL;
3022 break;
3025 decl_data[last_file_ix++] = file_data;
3027 lto_obj_file_close (current_lto_file);
3028 free (current_lto_file);
3029 current_lto_file = NULL;
3032 lto_flatten_files (decl_data, count, last_file_ix);
3033 lto_stats.num_input_files = count;
3034 ggc_free(decl_data);
3035 real_file_decl_data = NULL;
3037 if (resolution_file_name)
3038 fclose (resolution);
3040 /* Show the LTO report before launching LTRANS. */
3041 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3042 print_lto_report_1 ();
3044 /* Free gimple type merging datastructures. */
3045 delete tree_scc_hash;
3046 tree_scc_hash = NULL;
3047 obstack_free (&tree_scc_hash_obstack, NULL);
3048 htab_delete (gimple_canonical_types);
3049 gimple_canonical_types = NULL;
3050 delete canonical_type_hash_cache;
3051 canonical_type_hash_cache = NULL;
3053 /* At this stage we know that majority of GGC memory is reachable.
3054 Growing the limits prevents unnecesary invocation of GGC. */
3055 ggc_grow ();
3056 ggc_collect ();
3058 /* Set the hooks so that all of the ipa passes can read in their data. */
3059 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
3061 timevar_pop (TV_IPA_LTO_DECL_IN);
3063 if (!quiet_flag)
3064 fprintf (stderr, "\nReading the callgraph\n");
3066 timevar_push (TV_IPA_LTO_CGRAPH_IO);
3067 /* Read the symtab. */
3068 input_symtab ();
3070 input_offload_tables ();
3072 /* Store resolutions into the symbol table. */
3074 ld_plugin_symbol_resolution_t *res;
3075 FOR_EACH_SYMBOL (snode)
3076 if (snode->real_symbol_p ()
3077 && snode->lto_file_data
3078 && snode->lto_file_data->resolution_map
3079 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
3080 snode->resolution = *res;
3081 for (i = 0; all_file_decl_data[i]; i++)
3082 if (all_file_decl_data[i]->resolution_map)
3084 delete all_file_decl_data[i]->resolution_map;
3085 all_file_decl_data[i]->resolution_map = NULL;
3088 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
3090 if (!quiet_flag)
3091 fprintf (stderr, "Merging declarations\n");
3093 timevar_push (TV_IPA_LTO_DECL_MERGE);
3094 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
3095 need to care about resolving symbols again, we only need to replace
3096 duplicated declarations read from the callgraph and from function
3097 sections. */
3098 if (!flag_ltrans)
3100 lto_symtab_merge_decls ();
3102 /* If there were errors during symbol merging bail out, we have no
3103 good way to recover here. */
3104 if (seen_error ())
3105 fatal_error (input_location,
3106 "errors during merging of translation units");
3108 /* Fixup all decls. */
3109 lto_fixup_decls (all_file_decl_data);
3111 if (tree_with_vars)
3112 ggc_free (tree_with_vars);
3113 tree_with_vars = NULL;
3114 ggc_collect ();
3116 timevar_pop (TV_IPA_LTO_DECL_MERGE);
3117 /* Each pass will set the appropriate timer. */
3119 if (!quiet_flag)
3120 fprintf (stderr, "Reading summaries\n");
3122 /* Read the IPA summary data. */
3123 if (flag_ltrans)
3124 ipa_read_optimization_summaries ();
3125 else
3126 ipa_read_summaries ();
3128 for (i = 0; all_file_decl_data[i]; i++)
3130 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
3131 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
3132 all_file_decl_data[i]->symtab_node_encoder = NULL;
3133 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
3134 all_file_decl_data[i]->global_decl_state = NULL;
3135 all_file_decl_data[i]->current_decl_state = NULL;
3138 /* Finally merge the cgraph according to the decl merging decisions. */
3139 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
3140 if (symtab->dump_file)
3142 fprintf (symtab->dump_file, "Before merging:\n");
3143 symtab_node::dump_table (symtab->dump_file);
3145 if (!flag_ltrans)
3147 lto_symtab_merge_symbols ();
3148 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
3149 we are still having duplicated comdat groups containing local statics.
3150 We could also just remove them while merging. */
3151 symtab->remove_unreachable_nodes (dump_file);
3153 ggc_collect ();
3154 symtab->state = IPA_SSA;
3155 /* FIXME: Technically all node removals happening here are useless, because
3156 WPA should not stream them. */
3157 if (flag_ltrans)
3158 symtab->remove_unreachable_nodes (dump_file);
3160 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
3162 /* Indicate that the cgraph is built and ready. */
3163 symtab->function_flags_ready = true;
3165 ggc_free (all_file_decl_data);
3166 all_file_decl_data = NULL;
3170 /* Materialize all the bodies for all the nodes in the callgraph. */
3172 static void
3173 materialize_cgraph (void)
3175 struct cgraph_node *node;
3176 timevar_id_t lto_timer;
3178 if (!quiet_flag)
3179 fprintf (stderr,
3180 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
3183 FOR_EACH_FUNCTION (node)
3185 if (node->lto_file_data)
3187 lto_materialize_function (node);
3188 lto_stats.num_input_cgraph_nodes++;
3193 /* Start the appropriate timer depending on the mode that we are
3194 operating in. */
3195 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
3196 : (flag_ltrans) ? TV_WHOPR_LTRANS
3197 : TV_LTO;
3198 timevar_push (lto_timer);
3200 current_function_decl = NULL;
3201 set_cfun (NULL);
3203 if (!quiet_flag)
3204 fprintf (stderr, "\n");
3206 timevar_pop (lto_timer);
3210 /* Show various memory usage statistics related to LTO. */
3211 static void
3212 print_lto_report_1 (void)
3214 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3215 fprintf (stderr, "%s statistics\n", pfx);
3217 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3218 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3219 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3220 if (flag_wpa && tree_scc_hash)
3222 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3223 "collision ratio: %f\n", pfx,
3224 (long) tree_scc_hash->size (),
3225 (long) tree_scc_hash->elements (),
3226 tree_scc_hash->collisions ());
3227 hash_table<tree_scc_hasher>::iterator hiter;
3228 tree_scc *scc, *max_scc = NULL;
3229 unsigned max_length = 0;
3230 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3232 unsigned length = 0;
3233 tree_scc *s = scc;
3234 for (; s; s = s->next)
3235 length++;
3236 if (length > max_length)
3238 max_length = length;
3239 max_scc = scc;
3242 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3243 pfx, max_length, max_scc->len);
3244 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3245 num_scc_compares, num_scc_compare_collisions,
3246 num_scc_compare_collisions / (double) num_scc_compares);
3247 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3248 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3249 total_scc_size_merged);
3250 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3251 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3252 pfx, num_prevailing_types, num_type_scc_trees);
3253 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3254 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3255 (long) htab_size (gimple_canonical_types),
3256 (long) htab_elements (gimple_canonical_types),
3257 (long) gimple_canonical_types->searches,
3258 (long) gimple_canonical_types->collisions,
3259 htab_collisions (gimple_canonical_types));
3260 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3261 "%lu elements, %ld searches\n", pfx,
3262 num_canonical_type_hash_entries,
3263 num_canonical_type_hash_queries);
3266 print_lto_report (pfx);
3269 /* Perform whole program analysis (WPA) on the callgraph and write out the
3270 optimization plan. */
3272 static void
3273 do_whole_program_analysis (void)
3275 symtab_node *node;
3277 lto_parallelism = 1;
3279 /* TODO: jobserver communicatoin is not supported, yet. */
3280 if (!strcmp (flag_wpa, "jobserver"))
3281 lto_parallelism = -1;
3282 else
3284 lto_parallelism = atoi (flag_wpa);
3285 if (lto_parallelism <= 0)
3286 lto_parallelism = 0;
3289 timevar_start (TV_PHASE_OPT_GEN);
3291 /* Note that since we are in WPA mode, materialize_cgraph will not
3292 actually read in all the function bodies. It only materializes
3293 the decls and cgraph nodes so that analysis can be performed. */
3294 materialize_cgraph ();
3296 /* Reading in the cgraph uses different timers, start timing WPA now. */
3297 timevar_push (TV_WHOPR_WPA);
3299 if (pre_ipa_mem_report)
3301 fprintf (stderr, "Memory consumption before IPA\n");
3302 dump_memory_report (false);
3305 symtab->function_flags_ready = true;
3307 if (symtab->dump_file)
3308 symtab_node::dump_table (symtab->dump_file);
3309 bitmap_obstack_initialize (NULL);
3310 symtab->state = IPA_SSA;
3312 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3314 if (symtab->dump_file)
3316 fprintf (symtab->dump_file, "Optimized ");
3317 symtab_node::dump_table (symtab->dump_file);
3319 #ifdef ENABLE_CHECKING
3320 symtab_node::verify_symtab_nodes ();
3321 #endif
3322 bitmap_obstack_release (NULL);
3324 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3325 timevar_pop (TV_WHOPR_WPA);
3327 timevar_push (TV_WHOPR_PARTITIONING);
3328 if (flag_lto_partition == LTO_PARTITION_1TO1)
3329 lto_1_to_1_map ();
3330 else if (flag_lto_partition == LTO_PARTITION_MAX)
3331 lto_max_map ();
3332 else if (flag_lto_partition == LTO_PARTITION_ONE)
3333 lto_balanced_map (1);
3334 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3335 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3336 else
3337 gcc_unreachable ();
3339 /* Inline summaries are needed for balanced partitioning. Free them now so
3340 the memory can be used for streamer caches. */
3341 inline_free_summary ();
3343 /* AUX pointers are used by partitioning code to bookkeep number of
3344 partitions symbol is in. This is no longer needed. */
3345 FOR_EACH_SYMBOL (node)
3346 node->aux = NULL;
3348 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3350 /* Find out statics that need to be promoted
3351 to globals with hidden visibility because they are accessed from multiple
3352 partitions. */
3353 lto_promote_cross_file_statics ();
3354 timevar_pop (TV_WHOPR_PARTITIONING);
3356 timevar_stop (TV_PHASE_OPT_GEN);
3358 /* Collect a last time - in lto_wpa_write_files we may end up forking
3359 with the idea that this doesn't increase memory usage. So we
3360 absoultely do not want to collect after that. */
3361 ggc_collect ();
3363 timevar_start (TV_PHASE_STREAM_OUT);
3364 if (!quiet_flag)
3366 fprintf (stderr, "\nStreaming out");
3367 fflush (stderr);
3369 lto_wpa_write_files ();
3370 if (!quiet_flag)
3371 fprintf (stderr, "\n");
3372 timevar_stop (TV_PHASE_STREAM_OUT);
3374 if (post_ipa_mem_report)
3376 fprintf (stderr, "Memory consumption after IPA\n");
3377 dump_memory_report (false);
3380 /* Show the LTO report before launching LTRANS. */
3381 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3382 print_lto_report_1 ();
3383 if (mem_report_wpa)
3384 dump_memory_report (true);
3388 static GTY(()) tree lto_eh_personality_decl;
3390 /* Return the LTO personality function decl. */
3392 tree
3393 lto_eh_personality (void)
3395 if (!lto_eh_personality_decl)
3397 /* Use the first personality DECL for our personality if we don't
3398 support multiple ones. This ensures that we don't artificially
3399 create the need for them in a single-language program. */
3400 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3401 lto_eh_personality_decl = first_personality_decl;
3402 else
3403 lto_eh_personality_decl = lhd_gcc_personality ();
3406 return lto_eh_personality_decl;
3409 /* Set the process name based on the LTO mode. */
3411 static void
3412 lto_process_name (void)
3414 if (flag_lto)
3415 setproctitle ("lto1-lto");
3416 if (flag_wpa)
3417 setproctitle ("lto1-wpa");
3418 if (flag_ltrans)
3419 setproctitle ("lto1-ltrans");
3423 /* Initialize the LTO front end. */
3425 static void
3426 lto_init (void)
3428 lto_process_name ();
3429 lto_streamer_hooks_init ();
3430 lto_reader_init ();
3431 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3432 memset (&lto_stats, 0, sizeof (lto_stats));
3433 bitmap_obstack_initialize (NULL);
3434 gimple_register_cfg_hooks ();
3435 #ifndef ACCEL_COMPILER
3436 unsigned char *table
3437 = ggc_vec_alloc<unsigned char> (MAX_MACHINE_MODE);
3438 for (int m = 0; m < MAX_MACHINE_MODE; m++)
3439 table[m] = m;
3440 lto_mode_identity_table = table;
3441 #endif
3445 /* Main entry point for the GIMPLE front end. This front end has
3446 three main personalities:
3448 - LTO (-flto). All the object files on the command line are
3449 loaded in memory and processed as a single translation unit.
3450 This is the traditional link-time optimization behavior.
3452 - WPA (-fwpa). Only the callgraph and summary information for
3453 files in the command file are loaded. A single callgraph
3454 (without function bodies) is instantiated for the whole set of
3455 files. IPA passes are only allowed to analyze the call graph
3456 and make transformation decisions. The callgraph is
3457 partitioned, each partition is written to a new object file
3458 together with the transformation decisions.
3460 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3461 summary files from running again. Since WPA computed summary
3462 information and decided what transformations to apply, LTRANS
3463 simply applies them. */
3465 void
3466 lto_main (void)
3468 /* LTO is called as a front end, even though it is not a front end.
3469 Because it is called as a front end, TV_PHASE_PARSING and
3470 TV_PARSE_GLOBAL are active, and we need to turn them off while
3471 doing LTO. Later we turn them back on so they are active up in
3472 toplev.c. */
3473 timevar_pop (TV_PARSE_GLOBAL);
3474 timevar_stop (TV_PHASE_PARSING);
3476 timevar_start (TV_PHASE_SETUP);
3478 /* Initialize the LTO front end. */
3479 lto_init ();
3481 timevar_stop (TV_PHASE_SETUP);
3482 timevar_start (TV_PHASE_STREAM_IN);
3484 /* Read all the symbols and call graph from all the files in the
3485 command line. */
3486 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3488 timevar_stop (TV_PHASE_STREAM_IN);
3490 if (!seen_error ())
3492 /* If WPA is enabled analyze the whole call graph and create an
3493 optimization plan. Otherwise, read in all the function
3494 bodies and continue with optimization. */
3495 if (flag_wpa)
3496 do_whole_program_analysis ();
3497 else
3499 timevar_start (TV_PHASE_OPT_GEN);
3501 materialize_cgraph ();
3502 if (!flag_ltrans)
3503 lto_promote_statics_nonwpa ();
3505 /* Let the middle end know that we have read and merged all of
3506 the input files. */
3507 symtab->compile ();
3509 timevar_stop (TV_PHASE_OPT_GEN);
3511 /* FIXME lto, if the processes spawned by WPA fail, we miss
3512 the chance to print WPA's report, so WPA will call
3513 print_lto_report before launching LTRANS. If LTRANS was
3514 launched directly by the driver we would not need to do
3515 this. */
3516 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3517 print_lto_report_1 ();
3521 /* Here we make LTO pretend to be a parser. */
3522 timevar_start (TV_PHASE_PARSING);
3523 timevar_push (TV_PARSE_GLOBAL);
3526 #include "gt-lto-lto.h"