* config/i386/gnu-user.h (TARGET_CAN_SPLIT_STACK): Move from here ...
[official-gcc.git] / gcc / lto / lto.c
blob324538a0cb8b355352a38b1723a9c5d575b63bce
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2014 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 "tree.h"
27 #include "stor-layout.h"
28 #include "diagnostic-core.h"
29 #include "tm.h"
30 #include "predict.h"
31 #include "basic-block.h"
32 #include "hash-map.h"
33 #include "is-a.h"
34 #include "plugin-api.h"
35 #include "vec.h"
36 #include "hashtab.h"
37 #include "hash-set.h"
38 #include "machmode.h"
39 #include "hard-reg-set.h"
40 #include "input.h"
41 #include "function.h"
42 #include "ipa-ref.h"
43 #include "cgraph.h"
44 #include "tree-ssa-operands.h"
45 #include "tree-pass.h"
46 #include "langhooks.h"
47 #include "bitmap.h"
48 #include "inchash.h"
49 #include "alloc-pool.h"
50 #include "ipa-prop.h"
51 #include "common.h"
52 #include "debug.h"
53 #include "tree-ssa-alias.h"
54 #include "internal-fn.h"
55 #include "gimple-expr.h"
56 #include "gimple.h"
57 #include "lto.h"
58 #include "lto-tree.h"
59 #include "lto-streamer.h"
60 #include "lto-section-names.h"
61 #include "tree-streamer.h"
62 #include "splay-tree.h"
63 #include "lto-partition.h"
64 #include "data-streamer.h"
65 #include "context.h"
66 #include "pass_manager.h"
67 #include "ipa-inline.h"
68 #include "params.h"
69 #include "ipa-utils.h"
72 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
73 static int lto_parallelism;
75 static GTY(()) tree first_personality_decl;
77 /* Returns a hash code for P. */
79 static hashval_t
80 hash_name (const void *p)
82 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
83 return (hashval_t) htab_hash_string (ds->name);
87 /* Returns nonzero if P1 and P2 are equal. */
89 static int
90 eq_name (const void *p1, const void *p2)
92 const struct lto_section_slot *s1 =
93 (const struct lto_section_slot *) p1;
94 const struct lto_section_slot *s2 =
95 (const struct lto_section_slot *) p2;
97 return strcmp (s1->name, s2->name) == 0;
100 /* Free lto_section_slot */
102 static void
103 free_with_string (void *arg)
105 struct lto_section_slot *s = (struct lto_section_slot *)arg;
107 free (CONST_CAST (char *, s->name));
108 free (arg);
111 /* Create section hash table */
113 htab_t
114 lto_obj_create_section_hash_table (void)
116 return htab_create (37, hash_name, eq_name, free_with_string);
119 /* Delete an allocated integer KEY in the splay tree. */
121 static void
122 lto_splay_tree_delete_id (splay_tree_key key)
124 free ((void *) key);
127 /* Compare splay tree node ids A and B. */
129 static int
130 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
132 unsigned HOST_WIDE_INT ai;
133 unsigned HOST_WIDE_INT bi;
135 ai = *(unsigned HOST_WIDE_INT *) a;
136 bi = *(unsigned HOST_WIDE_INT *) b;
138 if (ai < bi)
139 return -1;
140 else if (ai > bi)
141 return 1;
142 return 0;
145 /* Look up splay tree node by ID in splay tree T. */
147 static splay_tree_node
148 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
150 return splay_tree_lookup (t, (splay_tree_key) &id);
153 /* Check if KEY has ID. */
155 static bool
156 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
158 return *(unsigned HOST_WIDE_INT *) key == id;
161 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
162 The ID is allocated separately because we need HOST_WIDE_INTs which may
163 be wider than a splay_tree_key. */
165 static void
166 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
167 struct lto_file_decl_data *file_data)
169 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
170 *idp = id;
171 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
174 /* Create a splay tree. */
176 static splay_tree
177 lto_splay_tree_new (void)
179 return splay_tree_new (lto_splay_tree_compare_ids,
180 lto_splay_tree_delete_id,
181 NULL);
184 /* Return true when NODE has a clone that is analyzed (i.e. we need
185 to load its body even if the node itself is not needed). */
187 static bool
188 has_analyzed_clone_p (struct cgraph_node *node)
190 struct cgraph_node *orig = node;
191 node = node->clones;
192 if (node)
193 while (node != orig)
195 if (node->analyzed)
196 return true;
197 if (node->clones)
198 node = node->clones;
199 else if (node->next_sibling_clone)
200 node = node->next_sibling_clone;
201 else
203 while (node != orig && !node->next_sibling_clone)
204 node = node->clone_of;
205 if (node != orig)
206 node = node->next_sibling_clone;
209 return false;
212 /* Read the function body for the function associated with NODE. */
214 static void
215 lto_materialize_function (struct cgraph_node *node)
217 tree decl;
219 decl = node->decl;
220 /* Read in functions with body (analyzed nodes)
221 and also functions that are needed to produce virtual clones. */
222 if ((node->has_gimple_body_p () && node->analyzed)
223 || node->used_as_abstract_origin
224 || has_analyzed_clone_p (node))
226 /* Clones don't need to be read. */
227 if (node->clone_of)
228 return;
229 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
230 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
233 /* Let the middle end know about the function. */
234 rest_of_decl_compilation (decl, 1, 0);
238 /* Decode the content of memory pointed to by DATA in the in decl
239 state object STATE. DATA_IN points to a data_in structure for
240 decoding. Return the address after the decoded object in the
241 input. */
243 static const uint32_t *
244 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
245 struct lto_in_decl_state *state)
247 uint32_t ix;
248 tree decl;
249 uint32_t i, j;
251 ix = *data++;
252 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
253 if (!VAR_OR_FUNCTION_DECL_P (decl))
255 gcc_assert (decl == void_type_node);
256 decl = NULL_TREE;
258 state->fn_decl = decl;
260 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
262 uint32_t size = *data++;
263 vec<tree, va_gc> *decls = NULL;
264 vec_alloc (decls, size);
266 for (j = 0; j < size; j++)
267 vec_safe_push (decls,
268 streamer_tree_cache_get_tree (data_in->reader_cache,
269 data[j]));
271 state->streams[i] = decls;
272 data += size;
275 return data;
279 /* Global canonical type table. */
280 static htab_t gimple_canonical_types;
281 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
282 static unsigned long num_canonical_type_hash_entries;
283 static unsigned long num_canonical_type_hash_queries;
285 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
286 static hashval_t gimple_canonical_type_hash (const void *p);
287 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
289 /* Returning a hash value for gimple type TYPE.
291 The hash value returned is equal for types considered compatible
292 by gimple_canonical_types_compatible_p. */
294 static hashval_t
295 hash_canonical_type (tree type)
297 inchash::hash hstate;
299 /* Combine a few common features of types so that types are grouped into
300 smaller sets; when searching for existing matching types to merge,
301 only existing types having the same features as the new type will be
302 checked. */
303 hstate.add_int (TREE_CODE (type));
304 hstate.add_int (TYPE_MODE (type));
306 /* Incorporate common features of numerical types. */
307 if (INTEGRAL_TYPE_P (type)
308 || SCALAR_FLOAT_TYPE_P (type)
309 || FIXED_POINT_TYPE_P (type)
310 || TREE_CODE (type) == OFFSET_TYPE
311 || POINTER_TYPE_P (type))
313 hstate.add_int (TYPE_UNSIGNED (type));
314 hstate.add_int (TYPE_PRECISION (type));
317 if (VECTOR_TYPE_P (type))
319 hstate.add_int (TYPE_VECTOR_SUBPARTS (type));
320 hstate.add_int (TYPE_UNSIGNED (type));
323 if (TREE_CODE (type) == COMPLEX_TYPE)
324 hstate.add_int (TYPE_UNSIGNED (type));
326 /* For pointer and reference types, fold in information about the type
327 pointed to but do not recurse to the pointed-to type. */
328 if (POINTER_TYPE_P (type))
330 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
331 hstate.add_int (TREE_CODE (TREE_TYPE (type)));
334 /* For integer types hash only the string flag. */
335 if (TREE_CODE (type) == INTEGER_TYPE)
336 hstate.add_int (TYPE_STRING_FLAG (type));
338 /* For array types hash the domain bounds and the string flag. */
339 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
341 hstate.add_int (TYPE_STRING_FLAG (type));
342 /* OMP lowering can introduce error_mark_node in place of
343 random local decls in types. */
344 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
345 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
346 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
347 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
350 /* Recurse for aggregates with a single element type. */
351 if (TREE_CODE (type) == ARRAY_TYPE
352 || TREE_CODE (type) == COMPLEX_TYPE
353 || TREE_CODE (type) == VECTOR_TYPE)
354 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
356 /* Incorporate function return and argument types. */
357 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
359 unsigned na;
360 tree p;
362 /* For method types also incorporate their parent class. */
363 if (TREE_CODE (type) == METHOD_TYPE)
364 iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type), hstate);
366 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
368 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
370 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
371 na++;
374 hstate.add_int (na);
377 if (RECORD_OR_UNION_TYPE_P (type))
379 unsigned nf;
380 tree f;
382 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
383 if (TREE_CODE (f) == FIELD_DECL)
385 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
386 nf++;
389 hstate.add_int (nf);
392 return hstate.end();
395 /* Returning a hash value for gimple type TYPE combined with VAL. */
397 static void
398 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
400 hashval_t v;
401 /* An already processed type. */
402 if (TYPE_CANONICAL (type))
404 type = TYPE_CANONICAL (type);
405 v = gimple_canonical_type_hash (type);
407 else
409 /* Canonical types should not be able to form SCCs by design, this
410 recursion is just because we do not register canonical types in
411 optimal order. To avoid quadratic behavior also register the
412 type here. */
413 v = hash_canonical_type (type);
414 gimple_register_canonical_type_1 (type, v);
416 hstate.add_int (v);
419 /* Returns the hash for a canonical type P. */
421 static hashval_t
422 gimple_canonical_type_hash (const void *p)
424 num_canonical_type_hash_queries++;
425 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
426 gcc_assert (slot != NULL);
427 return *slot;
431 /* The TYPE_CANONICAL merging machinery. It should closely resemble
432 the middle-end types_compatible_p function. It needs to avoid
433 claiming types are different for types that should be treated
434 the same with respect to TBAA. Canonical types are also used
435 for IL consistency checks via the useless_type_conversion_p
436 predicate which does not handle all type kinds itself but falls
437 back to pointer-comparison of TYPE_CANONICAL for aggregates
438 for example. */
440 /* Return true iff T1 and T2 are structurally identical for what
441 TBAA is concerned. */
443 static bool
444 gimple_canonical_types_compatible_p (tree t1, tree t2)
446 /* Before starting to set up the SCC machinery handle simple cases. */
448 /* Check first for the obvious case of pointer identity. */
449 if (t1 == t2)
450 return true;
452 /* Check that we have two types to compare. */
453 if (t1 == NULL_TREE || t2 == NULL_TREE)
454 return false;
456 /* If the types have been previously registered and found equal
457 they still are. */
458 if (TYPE_CANONICAL (t1)
459 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
460 return true;
462 /* Can't be the same type if the types don't have the same code. */
463 if (TREE_CODE (t1) != TREE_CODE (t2))
464 return false;
466 /* Qualifiers do not matter for canonical type comparison purposes. */
468 /* Void types and nullptr types are always the same. */
469 if (TREE_CODE (t1) == VOID_TYPE
470 || TREE_CODE (t1) == NULLPTR_TYPE)
471 return true;
473 /* Can't be the same type if they have different mode. */
474 if (TYPE_MODE (t1) != TYPE_MODE (t2))
475 return false;
477 /* Non-aggregate types can be handled cheaply. */
478 if (INTEGRAL_TYPE_P (t1)
479 || SCALAR_FLOAT_TYPE_P (t1)
480 || FIXED_POINT_TYPE_P (t1)
481 || TREE_CODE (t1) == VECTOR_TYPE
482 || TREE_CODE (t1) == COMPLEX_TYPE
483 || TREE_CODE (t1) == OFFSET_TYPE
484 || POINTER_TYPE_P (t1))
486 /* Can't be the same type if they have different sign or precision. */
487 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
488 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
489 return false;
491 if (TREE_CODE (t1) == INTEGER_TYPE
492 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
493 return false;
495 /* For canonical type comparisons we do not want to build SCCs
496 so we cannot compare pointed-to types. But we can, for now,
497 require the same pointed-to type kind and match what
498 useless_type_conversion_p would do. */
499 if (POINTER_TYPE_P (t1))
501 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
502 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
503 return false;
505 if (TREE_CODE (TREE_TYPE (t1)) != TREE_CODE (TREE_TYPE (t2)))
506 return false;
509 /* Tail-recurse to components. */
510 if (TREE_CODE (t1) == VECTOR_TYPE
511 || TREE_CODE (t1) == COMPLEX_TYPE)
512 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
513 TREE_TYPE (t2));
515 return true;
518 /* Do type-specific comparisons. */
519 switch (TREE_CODE (t1))
521 case ARRAY_TYPE:
522 /* Array types are the same if the element types are the same and
523 the number of elements are the same. */
524 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
525 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
526 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
527 return false;
528 else
530 tree i1 = TYPE_DOMAIN (t1);
531 tree i2 = TYPE_DOMAIN (t2);
533 /* For an incomplete external array, the type domain can be
534 NULL_TREE. Check this condition also. */
535 if (i1 == NULL_TREE && i2 == NULL_TREE)
536 return true;
537 else if (i1 == NULL_TREE || i2 == NULL_TREE)
538 return false;
539 else
541 tree min1 = TYPE_MIN_VALUE (i1);
542 tree min2 = TYPE_MIN_VALUE (i2);
543 tree max1 = TYPE_MAX_VALUE (i1);
544 tree max2 = TYPE_MAX_VALUE (i2);
546 /* The minimum/maximum values have to be the same. */
547 if ((min1 == min2
548 || (min1 && min2
549 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
550 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
551 || operand_equal_p (min1, min2, 0))))
552 && (max1 == max2
553 || (max1 && max2
554 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
555 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
556 || operand_equal_p (max1, max2, 0)))))
557 return true;
558 else
559 return false;
563 case METHOD_TYPE:
564 case FUNCTION_TYPE:
565 /* Function types are the same if the return type and arguments types
566 are the same. */
567 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
568 return false;
570 if (!comp_type_attributes (t1, t2))
571 return false;
573 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
574 return true;
575 else
577 tree parms1, parms2;
579 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
580 parms1 && parms2;
581 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
583 if (!gimple_canonical_types_compatible_p
584 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
585 return false;
588 if (parms1 || parms2)
589 return false;
591 return true;
594 case RECORD_TYPE:
595 case UNION_TYPE:
596 case QUAL_UNION_TYPE:
598 tree f1, f2;
600 /* For aggregate types, all the fields must be the same. */
601 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
602 f1 || f2;
603 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
605 /* Skip non-fields. */
606 while (f1 && TREE_CODE (f1) != FIELD_DECL)
607 f1 = TREE_CHAIN (f1);
608 while (f2 && TREE_CODE (f2) != FIELD_DECL)
609 f2 = TREE_CHAIN (f2);
610 if (!f1 || !f2)
611 break;
612 /* The fields must have the same name, offset and type. */
613 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
614 || !gimple_compare_field_offset (f1, f2)
615 || !gimple_canonical_types_compatible_p
616 (TREE_TYPE (f1), TREE_TYPE (f2)))
617 return false;
620 /* If one aggregate has more fields than the other, they
621 are not the same. */
622 if (f1 || f2)
623 return false;
625 return true;
628 default:
629 gcc_unreachable ();
634 /* Returns nonzero if P1 and P2 are equal. */
636 static int
637 gimple_canonical_type_eq (const void *p1, const void *p2)
639 const_tree t1 = (const_tree) p1;
640 const_tree t2 = (const_tree) p2;
641 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
642 CONST_CAST_TREE (t2));
645 /* Main worker for gimple_register_canonical_type. */
647 static void
648 gimple_register_canonical_type_1 (tree t, hashval_t hash)
650 void **slot;
652 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
654 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
655 if (*slot)
657 tree new_type = (tree)(*slot);
658 gcc_checking_assert (new_type != t);
659 TYPE_CANONICAL (t) = new_type;
661 else
663 TYPE_CANONICAL (t) = t;
664 *slot = (void *) t;
665 /* Cache the just computed hash value. */
666 num_canonical_type_hash_entries++;
667 bool existed_p = canonical_type_hash_cache->put (t, hash);
668 gcc_assert (!existed_p);
672 /* Register type T in the global type table gimple_types and set
673 TYPE_CANONICAL of T accordingly.
674 This is used by LTO to merge structurally equivalent types for
675 type-based aliasing purposes across different TUs and languages.
677 ??? This merging does not exactly match how the tree.c middle-end
678 functions will assign TYPE_CANONICAL when new types are created
679 during optimization (which at least happens for pointer and array
680 types). */
682 static void
683 gimple_register_canonical_type (tree t)
685 if (TYPE_CANONICAL (t))
686 return;
688 gimple_register_canonical_type_1 (t, hash_canonical_type (t));
691 /* Re-compute TYPE_CANONICAL for NODE and related types. */
693 static void
694 lto_register_canonical_types (tree node, bool first_p)
696 if (!node
697 || !TYPE_P (node))
698 return;
700 if (first_p)
701 TYPE_CANONICAL (node) = NULL_TREE;
703 if (POINTER_TYPE_P (node)
704 || TREE_CODE (node) == COMPLEX_TYPE
705 || TREE_CODE (node) == ARRAY_TYPE)
706 lto_register_canonical_types (TREE_TYPE (node), first_p);
708 if (!first_p)
709 gimple_register_canonical_type (node);
713 /* Remember trees that contains references to declarations. */
714 static GTY(()) vec <tree, va_gc> *tree_with_vars;
716 #define CHECK_VAR(tt) \
717 do \
719 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
720 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
721 return true; \
722 } while (0)
724 #define CHECK_NO_VAR(tt) \
725 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
727 /* Check presence of pointers to decls in fields of a tree_typed T. */
729 static inline bool
730 mentions_vars_p_typed (tree t)
732 CHECK_NO_VAR (TREE_TYPE (t));
733 return false;
736 /* Check presence of pointers to decls in fields of a tree_common T. */
738 static inline bool
739 mentions_vars_p_common (tree t)
741 if (mentions_vars_p_typed (t))
742 return true;
743 CHECK_NO_VAR (TREE_CHAIN (t));
744 return false;
747 /* Check presence of pointers to decls in fields of a decl_minimal T. */
749 static inline bool
750 mentions_vars_p_decl_minimal (tree t)
752 if (mentions_vars_p_common (t))
753 return true;
754 CHECK_NO_VAR (DECL_NAME (t));
755 CHECK_VAR (DECL_CONTEXT (t));
756 return false;
759 /* Check presence of pointers to decls in fields of a decl_common T. */
761 static inline bool
762 mentions_vars_p_decl_common (tree t)
764 if (mentions_vars_p_decl_minimal (t))
765 return true;
766 CHECK_VAR (DECL_SIZE (t));
767 CHECK_VAR (DECL_SIZE_UNIT (t));
768 CHECK_VAR (DECL_INITIAL (t));
769 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
770 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
771 return false;
774 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
776 static inline bool
777 mentions_vars_p_decl_with_vis (tree t)
779 if (mentions_vars_p_decl_common (t))
780 return true;
782 /* Accessor macro has side-effects, use field-name here. */
783 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
784 return false;
787 /* Check presence of pointers to decls in fields of a decl_non_common T. */
789 static inline bool
790 mentions_vars_p_decl_non_common (tree t)
792 if (mentions_vars_p_decl_with_vis (t))
793 return true;
794 CHECK_NO_VAR (DECL_RESULT_FLD (t));
795 return false;
798 /* Check presence of pointers to decls in fields of a decl_non_common T. */
800 static bool
801 mentions_vars_p_function (tree t)
803 if (mentions_vars_p_decl_non_common (t))
804 return true;
805 CHECK_NO_VAR (DECL_ARGUMENTS (t));
806 CHECK_NO_VAR (DECL_VINDEX (t));
807 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
808 return false;
811 /* Check presence of pointers to decls in fields of a field_decl T. */
813 static bool
814 mentions_vars_p_field_decl (tree t)
816 if (mentions_vars_p_decl_common (t))
817 return true;
818 CHECK_VAR (DECL_FIELD_OFFSET (t));
819 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
820 CHECK_NO_VAR (DECL_QUALIFIER (t));
821 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
822 CHECK_NO_VAR (DECL_FCONTEXT (t));
823 return false;
826 /* Check presence of pointers to decls in fields of a type T. */
828 static bool
829 mentions_vars_p_type (tree t)
831 if (mentions_vars_p_common (t))
832 return true;
833 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
834 CHECK_VAR (TYPE_SIZE (t));
835 CHECK_VAR (TYPE_SIZE_UNIT (t));
836 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
837 CHECK_NO_VAR (TYPE_NAME (t));
839 CHECK_VAR (TYPE_MINVAL (t));
840 CHECK_VAR (TYPE_MAXVAL (t));
842 /* Accessor is for derived node types only. */
843 CHECK_NO_VAR (t->type_non_common.binfo);
845 CHECK_VAR (TYPE_CONTEXT (t));
846 CHECK_NO_VAR (TYPE_CANONICAL (t));
847 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
848 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
849 return false;
852 /* Check presence of pointers to decls in fields of a BINFO T. */
854 static bool
855 mentions_vars_p_binfo (tree t)
857 unsigned HOST_WIDE_INT i, n;
859 if (mentions_vars_p_common (t))
860 return true;
861 CHECK_VAR (BINFO_VTABLE (t));
862 CHECK_NO_VAR (BINFO_OFFSET (t));
863 CHECK_NO_VAR (BINFO_VIRTUALS (t));
864 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
865 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
866 for (i = 0; i < n; i++)
867 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
868 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
869 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
870 n = BINFO_N_BASE_BINFOS (t);
871 for (i = 0; i < n; i++)
872 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
873 return false;
876 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
878 static bool
879 mentions_vars_p_constructor (tree t)
881 unsigned HOST_WIDE_INT idx;
882 constructor_elt *ce;
884 if (mentions_vars_p_typed (t))
885 return true;
887 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
889 CHECK_NO_VAR (ce->index);
890 CHECK_VAR (ce->value);
892 return false;
895 /* Check presence of pointers to decls in fields of an expression tree T. */
897 static bool
898 mentions_vars_p_expr (tree t)
900 int i;
901 if (mentions_vars_p_typed (t))
902 return true;
903 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
904 CHECK_VAR (TREE_OPERAND (t, i));
905 return false;
908 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
910 static bool
911 mentions_vars_p_omp_clause (tree t)
913 int i;
914 if (mentions_vars_p_common (t))
915 return true;
916 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
917 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
918 return false;
921 /* Check presence of pointers to decls that needs later fixup in T. */
923 static bool
924 mentions_vars_p (tree t)
926 switch (TREE_CODE (t))
928 case IDENTIFIER_NODE:
929 break;
931 case TREE_LIST:
932 CHECK_VAR (TREE_VALUE (t));
933 CHECK_VAR (TREE_PURPOSE (t));
934 CHECK_NO_VAR (TREE_CHAIN (t));
935 break;
937 case FIELD_DECL:
938 return mentions_vars_p_field_decl (t);
940 case LABEL_DECL:
941 case CONST_DECL:
942 case PARM_DECL:
943 case RESULT_DECL:
944 case IMPORTED_DECL:
945 case NAMESPACE_DECL:
946 case NAMELIST_DECL:
947 return mentions_vars_p_decl_common (t);
949 case VAR_DECL:
950 return mentions_vars_p_decl_with_vis (t);
952 case TYPE_DECL:
953 return mentions_vars_p_decl_non_common (t);
955 case FUNCTION_DECL:
956 return mentions_vars_p_function (t);
958 case TREE_BINFO:
959 return mentions_vars_p_binfo (t);
961 case PLACEHOLDER_EXPR:
962 return mentions_vars_p_common (t);
964 case BLOCK:
965 case TRANSLATION_UNIT_DECL:
966 case OPTIMIZATION_NODE:
967 case TARGET_OPTION_NODE:
968 break;
970 case CONSTRUCTOR:
971 return mentions_vars_p_constructor (t);
973 case OMP_CLAUSE:
974 return mentions_vars_p_omp_clause (t);
976 default:
977 if (TYPE_P (t))
979 if (mentions_vars_p_type (t))
980 return true;
982 else if (EXPR_P (t))
984 if (mentions_vars_p_expr (t))
985 return true;
987 else if (CONSTANT_CLASS_P (t))
988 CHECK_NO_VAR (TREE_TYPE (t));
989 else
990 gcc_unreachable ();
992 return false;
996 /* Return the resolution for the decl with index INDEX from DATA_IN. */
998 static enum ld_plugin_symbol_resolution
999 get_resolution (struct data_in *data_in, unsigned index)
1001 if (data_in->globals_resolution.exists ())
1003 ld_plugin_symbol_resolution_t ret;
1004 /* We can have references to not emitted functions in
1005 DECL_FUNCTION_PERSONALITY at least. So we can and have
1006 to indeed return LDPR_UNKNOWN in some cases. */
1007 if (data_in->globals_resolution.length () <= index)
1008 return LDPR_UNKNOWN;
1009 ret = data_in->globals_resolution[index];
1010 return ret;
1012 else
1013 /* Delay resolution finding until decl merging. */
1014 return LDPR_UNKNOWN;
1017 /* We need to record resolutions until symbol table is read. */
1018 static void
1019 register_resolution (struct lto_file_decl_data *file_data, tree decl,
1020 enum ld_plugin_symbol_resolution resolution)
1022 if (resolution == LDPR_UNKNOWN)
1023 return;
1024 if (!file_data->resolution_map)
1025 file_data->resolution_map
1026 = new hash_map<tree, ld_plugin_symbol_resolution>;
1027 file_data->resolution_map->put (decl, resolution);
1030 /* Register DECL with the global symbol table and change its
1031 name if necessary to avoid name clashes for static globals across
1032 different files. */
1034 static void
1035 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
1036 unsigned ix)
1038 tree context;
1040 /* Variable has file scope, not local. */
1041 if (!TREE_PUBLIC (decl)
1042 && !((context = decl_function_context (decl))
1043 && auto_var_in_fn_p (decl, context)))
1044 rest_of_decl_compilation (decl, 1, 0);
1046 /* If this variable has already been declared, queue the
1047 declaration for merging. */
1048 if (TREE_PUBLIC (decl))
1049 register_resolution (data_in->file_data,
1050 decl, get_resolution (data_in, ix));
1054 /* Register DECL with the global symbol table and change its
1055 name if necessary to avoid name clashes for static globals across
1056 different files. DATA_IN contains descriptors and tables for the
1057 file being read. */
1059 static void
1060 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
1061 unsigned ix)
1063 /* If this variable has already been declared, queue the
1064 declaration for merging. */
1065 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
1066 register_resolution (data_in->file_data,
1067 decl, get_resolution (data_in, ix));
1071 /* For the type T re-materialize it in the type variant list and
1072 the pointer/reference-to chains. */
1074 static void
1075 lto_fixup_prevailing_type (tree t)
1077 /* The following re-creates proper variant lists while fixing up
1078 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1079 variant list state before fixup is broken. */
1081 /* If we are not our own variant leader link us into our new leaders
1082 variant list. */
1083 if (TYPE_MAIN_VARIANT (t) != t)
1085 tree mv = TYPE_MAIN_VARIANT (t);
1086 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1087 TYPE_NEXT_VARIANT (mv) = t;
1090 /* The following reconstructs the pointer chains
1091 of the new pointed-to type if we are a main variant. We do
1092 not stream those so they are broken before fixup. */
1093 if (TREE_CODE (t) == POINTER_TYPE
1094 && TYPE_MAIN_VARIANT (t) == t)
1096 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1097 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1099 else if (TREE_CODE (t) == REFERENCE_TYPE
1100 && TYPE_MAIN_VARIANT (t) == t)
1102 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1103 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1108 /* We keep prevailing tree SCCs in a hashtable with manual collision
1109 handling (in case all hashes compare the same) and keep the colliding
1110 entries in the tree_scc->next chain. */
1112 struct tree_scc
1114 tree_scc *next;
1115 /* Hash of the whole SCC. */
1116 hashval_t hash;
1117 /* Number of trees in the SCC. */
1118 unsigned len;
1119 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1120 which share the same individual tree hash). */
1121 unsigned entry_len;
1122 /* The members of the SCC.
1123 We only need to remember the first entry node candidate for prevailing
1124 SCCs (but of course have access to all entries for SCCs we are
1125 processing).
1126 ??? For prevailing SCCs we really only need hash and the first
1127 entry candidate, but that's too awkward to implement. */
1128 tree entries[1];
1131 struct tree_scc_hasher : typed_noop_remove <tree_scc>
1133 typedef tree_scc value_type;
1134 typedef tree_scc compare_type;
1135 static inline hashval_t hash (const value_type *);
1136 static inline bool equal (const value_type *, const compare_type *);
1139 hashval_t
1140 tree_scc_hasher::hash (const value_type *scc)
1142 return scc->hash;
1145 bool
1146 tree_scc_hasher::equal (const value_type *scc1, const compare_type *scc2)
1148 if (scc1->hash != scc2->hash
1149 || scc1->len != scc2->len
1150 || scc1->entry_len != scc2->entry_len)
1151 return false;
1152 return true;
1155 static hash_table<tree_scc_hasher> *tree_scc_hash;
1156 static struct obstack tree_scc_hash_obstack;
1158 static unsigned long num_merged_types;
1159 static unsigned long num_prevailing_types;
1160 static unsigned long num_type_scc_trees;
1161 static unsigned long total_scc_size;
1162 static unsigned long num_sccs_read;
1163 static unsigned long total_scc_size_merged;
1164 static unsigned long num_sccs_merged;
1165 static unsigned long num_scc_compares;
1166 static unsigned long num_scc_compare_collisions;
1169 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1170 recursing through in-SCC tree edges. Returns true if the SCCs entered
1171 through T1 and T2 are equal and fills in *MAP with the pairs of
1172 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1174 static bool
1175 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1177 enum tree_code code;
1179 /* Mark already visited nodes. */
1180 TREE_ASM_WRITTEN (t2) = 1;
1182 /* Push the pair onto map. */
1183 (*map)[0] = t1;
1184 (*map)[1] = t2;
1185 *map = *map + 2;
1187 /* Compare value-fields. */
1188 #define compare_values(X) \
1189 do { \
1190 if (X(t1) != X(t2)) \
1191 return false; \
1192 } while (0)
1194 compare_values (TREE_CODE);
1195 code = TREE_CODE (t1);
1197 if (!TYPE_P (t1))
1199 compare_values (TREE_SIDE_EFFECTS);
1200 compare_values (TREE_CONSTANT);
1201 compare_values (TREE_READONLY);
1202 compare_values (TREE_PUBLIC);
1204 compare_values (TREE_ADDRESSABLE);
1205 compare_values (TREE_THIS_VOLATILE);
1206 if (DECL_P (t1))
1207 compare_values (DECL_UNSIGNED);
1208 else if (TYPE_P (t1))
1209 compare_values (TYPE_UNSIGNED);
1210 if (TYPE_P (t1))
1211 compare_values (TYPE_ARTIFICIAL);
1212 else
1213 compare_values (TREE_NO_WARNING);
1214 compare_values (TREE_NOTHROW);
1215 compare_values (TREE_STATIC);
1216 if (code != TREE_BINFO)
1217 compare_values (TREE_PRIVATE);
1218 compare_values (TREE_PROTECTED);
1219 compare_values (TREE_DEPRECATED);
1220 if (TYPE_P (t1))
1222 compare_values (TYPE_SATURATING);
1223 compare_values (TYPE_ADDR_SPACE);
1225 else if (code == SSA_NAME)
1226 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1228 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1230 if (!wi::eq_p (t1, t2))
1231 return false;
1234 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1236 /* ??? No suitable compare routine available. */
1237 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1238 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1239 if (r1.cl != r2.cl
1240 || r1.decimal != r2.decimal
1241 || r1.sign != r2.sign
1242 || r1.signalling != r2.signalling
1243 || r1.canonical != r2.canonical
1244 || r1.uexp != r2.uexp)
1245 return false;
1246 for (unsigned i = 0; i < SIGSZ; ++i)
1247 if (r1.sig[i] != r2.sig[i])
1248 return false;
1251 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1252 if (!fixed_compare (EQ_EXPR,
1253 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1254 return false;
1257 /* We don't want to compare locations, so there is nothing do compare
1258 for TS_DECL_MINIMAL. */
1260 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1262 compare_values (DECL_MODE);
1263 compare_values (DECL_NONLOCAL);
1264 compare_values (DECL_VIRTUAL_P);
1265 compare_values (DECL_IGNORED_P);
1266 compare_values (DECL_ABSTRACT_P);
1267 compare_values (DECL_ARTIFICIAL);
1268 compare_values (DECL_USER_ALIGN);
1269 compare_values (DECL_PRESERVE_P);
1270 compare_values (DECL_EXTERNAL);
1271 compare_values (DECL_GIMPLE_REG_P);
1272 compare_values (DECL_ALIGN);
1273 if (code == LABEL_DECL)
1275 compare_values (EH_LANDING_PAD_NR);
1276 compare_values (LABEL_DECL_UID);
1278 else if (code == FIELD_DECL)
1280 compare_values (DECL_PACKED);
1281 compare_values (DECL_NONADDRESSABLE_P);
1282 compare_values (DECL_OFFSET_ALIGN);
1284 else if (code == VAR_DECL)
1286 compare_values (DECL_HAS_DEBUG_EXPR_P);
1287 compare_values (DECL_NONLOCAL_FRAME);
1289 if (code == RESULT_DECL
1290 || code == PARM_DECL
1291 || code == VAR_DECL)
1293 compare_values (DECL_BY_REFERENCE);
1294 if (code == VAR_DECL
1295 || code == PARM_DECL)
1296 compare_values (DECL_HAS_VALUE_EXPR_P);
1300 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1301 compare_values (DECL_REGISTER);
1303 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1305 compare_values (DECL_COMMON);
1306 compare_values (DECL_DLLIMPORT_P);
1307 compare_values (DECL_WEAK);
1308 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1309 compare_values (DECL_COMDAT);
1310 compare_values (DECL_VISIBILITY);
1311 compare_values (DECL_VISIBILITY_SPECIFIED);
1312 if (code == VAR_DECL)
1314 compare_values (DECL_HARD_REGISTER);
1315 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1316 compare_values (DECL_IN_CONSTANT_POOL);
1320 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1322 compare_values (DECL_BUILT_IN_CLASS);
1323 compare_values (DECL_STATIC_CONSTRUCTOR);
1324 compare_values (DECL_STATIC_DESTRUCTOR);
1325 compare_values (DECL_UNINLINABLE);
1326 compare_values (DECL_POSSIBLY_INLINED);
1327 compare_values (DECL_IS_NOVOPS);
1328 compare_values (DECL_IS_RETURNS_TWICE);
1329 compare_values (DECL_IS_MALLOC);
1330 compare_values (DECL_IS_OPERATOR_NEW);
1331 compare_values (DECL_DECLARED_INLINE_P);
1332 compare_values (DECL_STATIC_CHAIN);
1333 compare_values (DECL_NO_INLINE_WARNING_P);
1334 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1335 compare_values (DECL_NO_LIMIT_STACK);
1336 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1337 compare_values (DECL_PURE_P);
1338 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1339 compare_values (DECL_FINAL_P);
1340 compare_values (DECL_CXX_CONSTRUCTOR_P);
1341 compare_values (DECL_CXX_DESTRUCTOR_P);
1342 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1343 compare_values (DECL_FUNCTION_CODE);
1346 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1348 compare_values (TYPE_MODE);
1349 compare_values (TYPE_STRING_FLAG);
1350 compare_values (TYPE_NO_FORCE_BLK);
1351 compare_values (TYPE_NEEDS_CONSTRUCTING);
1352 if (RECORD_OR_UNION_TYPE_P (t1))
1354 compare_values (TYPE_TRANSPARENT_AGGR);
1355 compare_values (TYPE_FINAL_P);
1357 else if (code == ARRAY_TYPE)
1358 compare_values (TYPE_NONALIASED_COMPONENT);
1359 compare_values (TYPE_PACKED);
1360 compare_values (TYPE_RESTRICT);
1361 compare_values (TYPE_USER_ALIGN);
1362 compare_values (TYPE_READONLY);
1363 compare_values (TYPE_PRECISION);
1364 compare_values (TYPE_ALIGN);
1365 compare_values (TYPE_ALIAS_SET);
1368 /* We don't want to compare locations, so there is nothing do compare
1369 for TS_EXP. */
1371 /* BLOCKs are function local and we don't merge anything there, so
1372 simply refuse to merge. */
1373 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1374 return false;
1376 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1377 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1378 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1379 return false;
1381 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1382 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1383 return false;
1385 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1386 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1387 sizeof (struct cl_optimization)) != 0)
1388 return false;
1390 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1391 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1392 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1393 return false;
1395 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1396 compare_values (CONSTRUCTOR_NELTS);
1398 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1399 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1400 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1401 IDENTIFIER_LENGTH (t1)) != 0)
1402 return false;
1404 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1405 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1406 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1407 TREE_STRING_LENGTH (t1)) != 0)
1408 return false;
1410 if (code == OMP_CLAUSE)
1412 compare_values (OMP_CLAUSE_CODE);
1413 switch (OMP_CLAUSE_CODE (t1))
1415 case OMP_CLAUSE_DEFAULT:
1416 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1417 break;
1418 case OMP_CLAUSE_SCHEDULE:
1419 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1420 break;
1421 case OMP_CLAUSE_DEPEND:
1422 compare_values (OMP_CLAUSE_DEPEND_KIND);
1423 break;
1424 case OMP_CLAUSE_MAP:
1425 compare_values (OMP_CLAUSE_MAP_KIND);
1426 break;
1427 case OMP_CLAUSE_PROC_BIND:
1428 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1429 break;
1430 case OMP_CLAUSE_REDUCTION:
1431 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1432 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1433 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1434 break;
1435 default:
1436 break;
1440 #undef compare_values
1443 /* Compare pointer fields. */
1445 /* Recurse. Search & Replaced from DFS_write_tree_body.
1446 Folding the early checks into the compare_tree_edges recursion
1447 macro makes debugging way quicker as you are able to break on
1448 compare_tree_sccs_1 and simply finish until a call returns false
1449 to spot the SCC members with the difference. */
1450 #define compare_tree_edges(E1, E2) \
1451 do { \
1452 tree t1_ = (E1), t2_ = (E2); \
1453 if (t1_ != t2_ \
1454 && (!t1_ || !t2_ \
1455 || !TREE_VISITED (t2_) \
1456 || (!TREE_ASM_WRITTEN (t2_) \
1457 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1458 return false; \
1459 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1460 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1461 } while (0)
1463 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1465 if (code != IDENTIFIER_NODE)
1466 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1469 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1471 unsigned i;
1472 /* Note that the number of elements for EXPR has already been emitted
1473 in EXPR's header (see streamer_write_tree_header). */
1474 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1475 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1478 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1480 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1481 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1484 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1486 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1487 /* ??? Global decls from different TUs have non-matching
1488 TRANSLATION_UNIT_DECLs. Only consider a small set of
1489 decls equivalent, we should not end up merging others. */
1490 if ((code == TYPE_DECL
1491 || code == NAMESPACE_DECL
1492 || code == IMPORTED_DECL
1493 || code == CONST_DECL
1494 || (VAR_OR_FUNCTION_DECL_P (t1)
1495 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1496 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1498 else
1499 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1502 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1504 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1505 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1506 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1507 if ((code == VAR_DECL
1508 || code == PARM_DECL)
1509 && DECL_HAS_VALUE_EXPR_P (t1))
1510 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1511 if (code == VAR_DECL
1512 && DECL_HAS_DEBUG_EXPR_P (t1))
1513 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1514 /* LTO specific edges. */
1515 if (code != FUNCTION_DECL
1516 && code != TRANSLATION_UNIT_DECL)
1517 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1520 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1522 if (code == FUNCTION_DECL)
1524 tree a1, a2;
1525 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1526 a1 || a2;
1527 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1528 compare_tree_edges (a1, a2);
1529 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1531 else if (code == TYPE_DECL)
1532 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1535 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1537 /* Make sure we don't inadvertently set the assembler name. */
1538 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1539 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1540 DECL_ASSEMBLER_NAME (t2));
1543 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1545 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1546 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1547 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1548 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1549 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1550 DECL_FIELD_BIT_OFFSET (t2));
1551 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1554 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1556 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1557 DECL_FUNCTION_PERSONALITY (t2));
1558 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1559 /* DECL_FUNCTION_SPECIFIC_TARGET is not yet created. We compare
1560 the attribute list instead. */
1561 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1562 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1565 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1567 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1568 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1569 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1570 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1571 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1572 reconstructed during fixup. */
1573 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1574 during fixup. */
1575 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1576 /* ??? Global types from different TUs have non-matching
1577 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1578 equal. */
1579 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1581 else
1582 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1583 /* TYPE_CANONICAL is re-computed during type merging, so do not
1584 compare it here. */
1585 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1588 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1590 if (code == ENUMERAL_TYPE)
1591 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1592 else if (code == ARRAY_TYPE)
1593 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1594 else if (RECORD_OR_UNION_TYPE_P (t1))
1596 tree f1, f2;
1597 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1598 f1 || f2;
1599 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1600 compare_tree_edges (f1, f2);
1601 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1603 else if (code == FUNCTION_TYPE
1604 || code == METHOD_TYPE)
1605 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1606 if (!POINTER_TYPE_P (t1))
1607 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1608 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1611 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1613 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1614 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1615 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1618 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1619 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1620 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1622 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1624 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1625 compare_tree_edges (TREE_OPERAND (t1, i),
1626 TREE_OPERAND (t2, i));
1628 /* BLOCKs are function local and we don't merge anything there. */
1629 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1630 return false;
1633 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1635 unsigned i;
1636 tree t;
1637 /* Lengths have already been compared above. */
1638 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1639 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1640 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1641 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1642 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1643 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1644 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1645 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1646 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1649 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1651 unsigned i;
1652 tree index, value;
1653 /* Lengths have already been compared above. */
1654 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1656 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1657 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1661 if (code == OMP_CLAUSE)
1663 int i;
1665 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1666 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1667 OMP_CLAUSE_OPERAND (t2, i));
1668 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1671 #undef compare_tree_edges
1673 return true;
1676 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1677 out MAP if they are equal. */
1679 static bool
1680 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1681 tree *map)
1683 /* Assume SCC entry hashes are sorted after their cardinality. Which
1684 means we can simply take the first n-tuple of equal hashes
1685 (which is recorded as entry_len) and do n SCC entry candidate
1686 comparisons. */
1687 for (unsigned i = 0; i < pscc->entry_len; ++i)
1689 tree *mapp = map;
1690 num_scc_compare_collisions++;
1691 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1693 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1694 on the scc as all trees will be freed. */
1695 return true;
1697 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1698 the SCC prevails. */
1699 for (unsigned j = 0; j < scc->len; ++j)
1700 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1703 return false;
1706 /* QSort sort function to sort a map of two pointers after the 2nd
1707 pointer. */
1709 static int
1710 cmp_tree (const void *p1_, const void *p2_)
1712 tree *p1 = (tree *)(const_cast<void *>(p1_));
1713 tree *p2 = (tree *)(const_cast<void *>(p2_));
1714 if (p1[1] == p2[1])
1715 return 0;
1716 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1719 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1720 hash value SCC_HASH with an already recorded SCC. Return true if
1721 that was successful, otherwise return false. */
1723 static bool
1724 unify_scc (struct streamer_tree_cache_d *cache, unsigned from,
1725 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1727 bool unified_p = false;
1728 tree_scc *scc
1729 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1730 scc->next = NULL;
1731 scc->hash = scc_hash;
1732 scc->len = len;
1733 scc->entry_len = scc_entry_len;
1734 for (unsigned i = 0; i < len; ++i)
1736 tree t = streamer_tree_cache_get_tree (cache, from + i);
1737 scc->entries[i] = t;
1738 /* Do not merge SCCs with local entities inside them. Also do
1739 not merge TRANSLATION_UNIT_DECLs. */
1740 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1741 || (VAR_OR_FUNCTION_DECL_P (t)
1742 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1743 || TREE_CODE (t) == LABEL_DECL)
1745 /* Avoid doing any work for these cases and do not worry to
1746 record the SCCs for further merging. */
1747 return false;
1751 /* Look for the list of candidate SCCs to compare against. */
1752 tree_scc **slot;
1753 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1754 if (*slot)
1756 /* Try unifying against each candidate. */
1757 num_scc_compares++;
1759 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1760 outside of the scc when following tree edges. Make sure
1761 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1762 to track whether we visited the SCC member during the compare.
1763 We cannot use TREE_VISITED on the pscc members as the extended
1764 scc and pscc can overlap. */
1765 for (unsigned i = 0; i < scc->len; ++i)
1767 TREE_VISITED (scc->entries[i]) = 1;
1768 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1771 tree *map = XALLOCAVEC (tree, 2 * len);
1772 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1774 if (!compare_tree_sccs (pscc, scc, map))
1775 continue;
1777 /* Found an equal SCC. */
1778 unified_p = true;
1779 num_scc_compare_collisions--;
1780 num_sccs_merged++;
1781 total_scc_size_merged += len;
1783 #ifdef ENABLE_CHECKING
1784 for (unsigned i = 0; i < len; ++i)
1786 tree t = map[2*i+1];
1787 enum tree_code code = TREE_CODE (t);
1788 /* IDENTIFIER_NODEs should be singletons and are merged by the
1789 streamer. The others should be singletons, too, and we
1790 should not merge them in any way. */
1791 gcc_assert (code != TRANSLATION_UNIT_DECL
1792 && code != IDENTIFIER_NODE
1793 && !streamer_handle_as_builtin_p (t));
1795 #endif
1797 /* Fixup the streamer cache with the prevailing nodes according
1798 to the tree node mapping computed by compare_tree_sccs. */
1799 if (len == 1)
1800 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1801 else
1803 tree *map2 = XALLOCAVEC (tree, 2 * len);
1804 for (unsigned i = 0; i < len; ++i)
1806 map2[i*2] = (tree)(uintptr_t)(from + i);
1807 map2[i*2+1] = scc->entries[i];
1809 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1810 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1811 for (unsigned i = 0; i < len; ++i)
1812 streamer_tree_cache_replace_tree (cache, map[2*i],
1813 (uintptr_t)map2[2*i]);
1816 /* Free the tree nodes from the read SCC. */
1817 for (unsigned i = 0; i < len; ++i)
1819 enum tree_code code;
1820 if (TYPE_P (scc->entries[i]))
1821 num_merged_types++;
1822 code = TREE_CODE (scc->entries[i]);
1823 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1824 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1825 ggc_free (scc->entries[i]);
1828 break;
1831 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1832 if (!unified_p)
1833 for (unsigned i = 0; i < scc->len; ++i)
1834 TREE_VISITED (scc->entries[i]) = 0;
1837 /* If we didn't unify it to any candidate duplicate the relevant
1838 pieces to permanent storage and link it into the chain. */
1839 if (!unified_p)
1841 tree_scc *pscc
1842 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1843 memcpy (pscc, scc, sizeof (tree_scc));
1844 pscc->next = (*slot);
1845 *slot = pscc;
1847 return unified_p;
1851 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1852 RESOLUTIONS is the set of symbols picked by the linker (read from the
1853 resolution file when the linker plugin is being used). */
1855 static void
1856 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1857 vec<ld_plugin_symbol_resolution_t> resolutions)
1859 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1860 const int decl_offset = sizeof (struct lto_decl_header);
1861 const int main_offset = decl_offset + header->decl_state_size;
1862 const int string_offset = main_offset + header->main_size;
1863 struct data_in *data_in;
1864 unsigned int i;
1865 const uint32_t *data_ptr, *data_end;
1866 uint32_t num_decl_states;
1868 lto_input_block ib_main ((const char *) data + main_offset,
1869 header->main_size);
1871 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1872 header->string_size, resolutions);
1874 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1875 internal types that should not be merged. */
1877 /* Read the global declarations and types. */
1878 while (ib_main.p < ib_main.len)
1880 tree t;
1881 unsigned from = data_in->reader_cache->nodes.length ();
1882 /* Read and uniquify SCCs as in the input stream. */
1883 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1884 if (tag == LTO_tree_scc)
1886 unsigned len_;
1887 unsigned scc_entry_len;
1888 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1889 &scc_entry_len);
1890 unsigned len = data_in->reader_cache->nodes.length () - from;
1891 gcc_assert (len == len_);
1893 total_scc_size += len;
1894 num_sccs_read++;
1896 /* We have the special case of size-1 SCCs that are pre-merged
1897 by means of identifier and string sharing for example.
1898 ??? Maybe we should avoid streaming those as SCCs. */
1899 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1900 from);
1901 if (len == 1
1902 && (TREE_CODE (first) == IDENTIFIER_NODE
1903 || TREE_CODE (first) == INTEGER_CST
1904 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1905 || streamer_handle_as_builtin_p (first)))
1906 continue;
1908 /* Try to unify the SCC with already existing ones. */
1909 if (!flag_ltrans
1910 && unify_scc (data_in->reader_cache, from,
1911 len, scc_entry_len, scc_hash))
1912 continue;
1914 bool seen_type = false;
1915 for (unsigned i = 0; i < len; ++i)
1917 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1918 from + i);
1919 /* Reconstruct the type variant and pointer-to/reference-to
1920 chains. */
1921 if (TYPE_P (t))
1923 seen_type = true;
1924 num_prevailing_types++;
1925 lto_fixup_prevailing_type (t);
1927 /* Compute the canonical type of all types.
1928 ??? Should be able to assert that !TYPE_CANONICAL. */
1929 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1931 gimple_register_canonical_type (t);
1932 if (odr_type_p (t))
1933 register_odr_type (t);
1935 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1936 type which is also member of this SCC. */
1937 if (TREE_CODE (t) == INTEGER_CST
1938 && !TREE_OVERFLOW (t))
1939 cache_integer_cst (t);
1940 /* Register TYPE_DECLs with the debuginfo machinery. */
1941 if (!flag_wpa
1942 && TREE_CODE (t) == TYPE_DECL)
1943 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1944 if (!flag_ltrans)
1946 /* Register variables and functions with the
1947 symbol table. */
1948 if (TREE_CODE (t) == VAR_DECL)
1949 lto_register_var_decl_in_symtab (data_in, t, from + i);
1950 else if (TREE_CODE (t) == FUNCTION_DECL
1951 && !DECL_BUILT_IN (t))
1952 lto_register_function_decl_in_symtab (data_in, t, from + i);
1953 /* Scan the tree for references to global functions or
1954 variables and record those for later fixup. */
1955 if (mentions_vars_p (t))
1956 vec_safe_push (tree_with_vars, t);
1959 if (seen_type)
1960 num_type_scc_trees += len;
1962 else
1964 /* Pickle stray references. */
1965 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1966 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1970 /* Read in lto_in_decl_state objects. */
1971 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1972 data_end =
1973 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1974 num_decl_states = *data_ptr++;
1976 gcc_assert (num_decl_states > 0);
1977 decl_data->global_decl_state = lto_new_in_decl_state ();
1978 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1979 decl_data->global_decl_state);
1981 /* Read in per-function decl states and enter them in hash table. */
1982 decl_data->function_decl_states =
1983 hash_table<decl_state_hasher>::create_ggc (37);
1985 for (i = 1; i < num_decl_states; i++)
1987 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1989 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1990 lto_in_decl_state **slot
1991 = decl_data->function_decl_states->find_slot (state, INSERT);
1992 gcc_assert (*slot == NULL);
1993 *slot = state;
1996 if (data_ptr != data_end)
1997 internal_error ("bytecode stream: garbage at the end of symbols section");
1999 /* Set the current decl state to be the global state. */
2000 decl_data->current_decl_state = decl_data->global_decl_state;
2002 lto_data_in_delete (data_in);
2005 /* Custom version of strtoll, which is not portable. */
2007 static int64_t
2008 lto_parse_hex (const char *p)
2010 int64_t ret = 0;
2012 for (; *p != '\0'; ++p)
2014 char c = *p;
2015 unsigned char part;
2016 ret <<= 4;
2017 if (c >= '0' && c <= '9')
2018 part = c - '0';
2019 else if (c >= 'a' && c <= 'f')
2020 part = c - 'a' + 10;
2021 else if (c >= 'A' && c <= 'F')
2022 part = c - 'A' + 10;
2023 else
2024 internal_error ("could not parse hex number");
2025 ret |= part;
2028 return ret;
2031 /* Read resolution for file named FILE_NAME. The resolution is read from
2032 RESOLUTION. */
2034 static void
2035 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2037 /* We require that objects in the resolution file are in the same
2038 order as the lto1 command line. */
2039 unsigned int name_len;
2040 char *obj_name;
2041 unsigned int num_symbols;
2042 unsigned int i;
2043 struct lto_file_decl_data *file_data;
2044 splay_tree_node nd = NULL;
2046 if (!resolution)
2047 return;
2049 name_len = strlen (file->filename);
2050 obj_name = XNEWVEC (char, name_len + 1);
2051 fscanf (resolution, " "); /* Read white space. */
2053 fread (obj_name, sizeof (char), name_len, resolution);
2054 obj_name[name_len] = '\0';
2055 if (filename_cmp (obj_name, file->filename) != 0)
2056 internal_error ("unexpected file name %s in linker resolution file. "
2057 "Expected %s", obj_name, file->filename);
2058 if (file->offset != 0)
2060 int t;
2061 char offset_p[17];
2062 int64_t offset;
2063 t = fscanf (resolution, "@0x%16s", offset_p);
2064 if (t != 1)
2065 internal_error ("could not parse file offset");
2066 offset = lto_parse_hex (offset_p);
2067 if (offset != file->offset)
2068 internal_error ("unexpected offset");
2071 free (obj_name);
2073 fscanf (resolution, "%u", &num_symbols);
2075 for (i = 0; i < num_symbols; i++)
2077 int t;
2078 unsigned index;
2079 unsigned HOST_WIDE_INT id;
2080 char r_str[27];
2081 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2082 unsigned int j;
2083 unsigned int lto_resolution_str_len =
2084 sizeof (lto_resolution_str) / sizeof (char *);
2085 res_pair rp;
2087 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
2088 &index, &id, r_str);
2089 if (t != 3)
2090 internal_error ("invalid line in the resolution file");
2092 for (j = 0; j < lto_resolution_str_len; j++)
2094 if (strcmp (lto_resolution_str[j], r_str) == 0)
2096 r = (enum ld_plugin_symbol_resolution) j;
2097 break;
2100 if (j == lto_resolution_str_len)
2101 internal_error ("invalid resolution in the resolution file");
2103 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2105 nd = lto_splay_tree_lookup (file_ids, id);
2106 if (nd == NULL)
2107 internal_error ("resolution sub id %wx not in object file", id);
2110 file_data = (struct lto_file_decl_data *)nd->value;
2111 /* The indexes are very sparse. To save memory save them in a compact
2112 format that is only unpacked later when the subfile is processed. */
2113 rp.res = r;
2114 rp.index = index;
2115 file_data->respairs.safe_push (rp);
2116 if (file_data->max_index < index)
2117 file_data->max_index = index;
2121 /* List of file_decl_datas */
2122 struct file_data_list
2124 struct lto_file_decl_data *first, *last;
2127 /* Is the name for a id'ed LTO section? */
2129 static int
2130 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2132 const char *s;
2134 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
2135 return 0;
2136 s = strrchr (name, '.');
2137 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2140 /* Create file_data of each sub file id */
2142 static int
2143 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2144 struct file_data_list *list)
2146 struct lto_section_slot s_slot, *new_slot;
2147 unsigned HOST_WIDE_INT id;
2148 splay_tree_node nd;
2149 void **hash_slot;
2150 char *new_name;
2151 struct lto_file_decl_data *file_data;
2153 if (!lto_section_with_id (ls->name, &id))
2154 return 1;
2156 /* Find hash table of sub module id */
2157 nd = lto_splay_tree_lookup (file_ids, id);
2158 if (nd != NULL)
2160 file_data = (struct lto_file_decl_data *)nd->value;
2162 else
2164 file_data = ggc_alloc<lto_file_decl_data> ();
2165 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2166 file_data->id = id;
2167 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
2168 lto_splay_tree_insert (file_ids, id, file_data);
2170 /* Maintain list in linker order */
2171 if (!list->first)
2172 list->first = file_data;
2173 if (list->last)
2174 list->last->next = file_data;
2175 list->last = file_data;
2178 /* Copy section into sub module hash table */
2179 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2180 s_slot.name = new_name;
2181 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2182 gcc_assert (*hash_slot == NULL);
2184 new_slot = XDUP (struct lto_section_slot, ls);
2185 new_slot->name = new_name;
2186 *hash_slot = new_slot;
2187 return 1;
2190 /* Read declarations and other initializations for a FILE_DATA. */
2192 static void
2193 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2195 const char *data;
2196 size_t len;
2197 vec<ld_plugin_symbol_resolution_t>
2198 resolutions = vNULL;
2199 int i;
2200 res_pair *rp;
2202 /* Create vector for fast access of resolution. We do this lazily
2203 to save memory. */
2204 resolutions.safe_grow_cleared (file_data->max_index + 1);
2205 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2206 resolutions[rp->index] = rp->res;
2207 file_data->respairs.release ();
2209 file_data->renaming_hash_table = lto_create_renaming_table ();
2210 file_data->file_name = file->filename;
2211 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2212 if (data == NULL)
2214 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2215 return;
2217 /* Frees resolutions */
2218 lto_read_decls (file_data, data, resolutions);
2219 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2222 /* Finalize FILE_DATA in FILE and increase COUNT. */
2224 static int
2225 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2226 int *count)
2228 lto_file_finalize (file_data, file);
2229 if (symtab->dump_file)
2230 fprintf (symtab->dump_file,
2231 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2232 file_data->file_name, file_data->id);
2233 (*count)++;
2234 return 0;
2237 /* Generate a TREE representation for all types and external decls
2238 entities in FILE.
2240 Read all of the globals out of the file. Then read the cgraph
2241 and process the .o index into the cgraph nodes so that it can open
2242 the .o file to load the functions and ipa information. */
2244 static struct lto_file_decl_data *
2245 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2247 struct lto_file_decl_data *file_data = NULL;
2248 splay_tree file_ids;
2249 htab_t section_hash_table;
2250 struct lto_section_slot *section;
2251 struct file_data_list file_list;
2252 struct lto_section_list section_list;
2254 memset (&section_list, 0, sizeof (struct lto_section_list));
2255 section_hash_table = lto_obj_build_section_table (file, &section_list);
2257 /* Find all sub modules in the object and put their sections into new hash
2258 tables in a splay tree. */
2259 file_ids = lto_splay_tree_new ();
2260 memset (&file_list, 0, sizeof (struct file_data_list));
2261 for (section = section_list.first; section != NULL; section = section->next)
2262 create_subid_section_table (section, file_ids, &file_list);
2264 /* Add resolutions to file ids */
2265 lto_resolution_read (file_ids, resolution_file, file);
2267 /* Finalize each lto file for each submodule in the merged object */
2268 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2269 lto_create_files_from_ids (file, file_data, count);
2271 splay_tree_delete (file_ids);
2272 htab_delete (section_hash_table);
2274 return file_list.first;
2277 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2278 #define LTO_MMAP_IO 1
2279 #endif
2281 #if LTO_MMAP_IO
2282 /* Page size of machine is used for mmap and munmap calls. */
2283 static size_t page_mask;
2284 #endif
2286 /* Get the section data of length LEN from FILENAME starting at
2287 OFFSET. The data segment must be freed by the caller when the
2288 caller is finished. Returns NULL if all was not well. */
2290 static char *
2291 lto_read_section_data (struct lto_file_decl_data *file_data,
2292 intptr_t offset, size_t len)
2294 char *result;
2295 static int fd = -1;
2296 static char *fd_name;
2297 #if LTO_MMAP_IO
2298 intptr_t computed_len;
2299 intptr_t computed_offset;
2300 intptr_t diff;
2301 #endif
2303 /* Keep a single-entry file-descriptor cache. The last file we
2304 touched will get closed at exit.
2305 ??? Eventually we want to add a more sophisticated larger cache
2306 or rather fix function body streaming to not stream them in
2307 practically random order. */
2308 if (fd != -1
2309 && filename_cmp (fd_name, file_data->file_name) != 0)
2311 free (fd_name);
2312 close (fd);
2313 fd = -1;
2315 if (fd == -1)
2317 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2318 if (fd == -1)
2320 fatal_error ("Cannot open %s", file_data->file_name);
2321 return NULL;
2323 fd_name = xstrdup (file_data->file_name);
2326 #if LTO_MMAP_IO
2327 if (!page_mask)
2329 size_t page_size = sysconf (_SC_PAGE_SIZE);
2330 page_mask = ~(page_size - 1);
2333 computed_offset = offset & page_mask;
2334 diff = offset - computed_offset;
2335 computed_len = len + diff;
2337 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2338 fd, computed_offset);
2339 if (result == MAP_FAILED)
2341 fatal_error ("Cannot map %s", file_data->file_name);
2342 return NULL;
2345 return result + diff;
2346 #else
2347 result = (char *) xmalloc (len);
2348 if (lseek (fd, offset, SEEK_SET) != offset
2349 || read (fd, result, len) != (ssize_t) len)
2351 free (result);
2352 fatal_error ("Cannot read %s", file_data->file_name);
2353 result = NULL;
2355 #ifdef __MINGW32__
2356 /* Native windows doesn't supports delayed unlink on opened file. So
2357 we close file here again. This produces higher I/O load, but at least
2358 it prevents to have dangling file handles preventing unlink. */
2359 free (fd_name);
2360 fd_name = NULL;
2361 close (fd);
2362 fd = -1;
2363 #endif
2364 return result;
2365 #endif
2369 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2370 NAME will be NULL unless the section type is for a function
2371 body. */
2373 static const char *
2374 get_section_data (struct lto_file_decl_data *file_data,
2375 enum lto_section_type section_type,
2376 const char *name,
2377 size_t *len)
2379 htab_t section_hash_table = file_data->section_hash_table;
2380 struct lto_section_slot *f_slot;
2381 struct lto_section_slot s_slot;
2382 const char *section_name = lto_get_section_name (section_type, name, file_data);
2383 char *data = NULL;
2385 *len = 0;
2386 s_slot.name = section_name;
2387 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2388 if (f_slot)
2390 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2391 *len = f_slot->len;
2394 free (CONST_CAST (char *, section_name));
2395 return data;
2399 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2400 starts at OFFSET and has LEN bytes. */
2402 static void
2403 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2404 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2405 const char *name ATTRIBUTE_UNUSED,
2406 const char *offset, size_t len ATTRIBUTE_UNUSED)
2408 #if LTO_MMAP_IO
2409 intptr_t computed_len;
2410 intptr_t computed_offset;
2411 intptr_t diff;
2412 #endif
2414 #if LTO_MMAP_IO
2415 computed_offset = ((intptr_t) offset) & page_mask;
2416 diff = (intptr_t) offset - computed_offset;
2417 computed_len = len + diff;
2419 munmap ((caddr_t) computed_offset, computed_len);
2420 #else
2421 free (CONST_CAST(char *, offset));
2422 #endif
2425 static lto_file *current_lto_file;
2427 /* Helper for qsort; compare partitions and return one with smaller size.
2428 We sort from greatest to smallest so parallel build doesn't stale on the
2429 longest compilation being executed too late. */
2431 static int
2432 cmp_partitions_size (const void *a, const void *b)
2434 const struct ltrans_partition_def *pa
2435 = *(struct ltrans_partition_def *const *)a;
2436 const struct ltrans_partition_def *pb
2437 = *(struct ltrans_partition_def *const *)b;
2438 return pb->insns - pa->insns;
2441 /* Helper for qsort; compare partitions and return one with smaller order. */
2443 static int
2444 cmp_partitions_order (const void *a, const void *b)
2446 const struct ltrans_partition_def *pa
2447 = *(struct ltrans_partition_def *const *)a;
2448 const struct ltrans_partition_def *pb
2449 = *(struct ltrans_partition_def *const *)b;
2450 int ordera = -1, orderb = -1;
2452 if (lto_symtab_encoder_size (pa->encoder))
2453 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2454 if (lto_symtab_encoder_size (pb->encoder))
2455 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2456 return orderb - ordera;
2459 /* Actually stream out ENCODER into TEMP_FILENAME. */
2461 static void
2462 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2464 lto_file *file = lto_obj_file_open (temp_filename, true);
2465 if (!file)
2466 fatal_error ("lto_obj_file_open() failed");
2467 lto_set_current_out_file (file);
2469 ipa_write_optimization_summaries (encoder);
2471 lto_set_current_out_file (NULL);
2472 lto_obj_file_close (file);
2473 free (file);
2476 /* Wait for forked process and signal errors. */
2477 #ifdef HAVE_WORKING_FORK
2478 static void
2479 wait_for_child ()
2481 int status;
2484 #ifndef WCONTINUED
2485 #define WCONTINUED 0
2486 #endif
2487 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2488 if (w == -1)
2489 fatal_error ("waitpid failed");
2491 if (WIFEXITED (status) && WEXITSTATUS (status))
2492 fatal_error ("streaming subprocess failed");
2493 else if (WIFSIGNALED (status))
2494 fatal_error ("streaming subprocess was killed by signal");
2496 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2498 #endif
2500 /* Stream out ENCODER into TEMP_FILENAME
2501 Fork if that seems to help. */
2503 static void
2504 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2505 bool ARG_UNUSED (last))
2507 #ifdef HAVE_WORKING_FORK
2508 static int nruns;
2510 if (lto_parallelism <= 1)
2512 do_stream_out (temp_filename, encoder);
2513 return;
2516 /* Do not run more than LTO_PARALLELISM streamings
2517 FIXME: we ignore limits on jobserver. */
2518 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2520 wait_for_child ();
2521 nruns --;
2523 /* If this is not the last parallel partition, execute new
2524 streaming process. */
2525 if (!last)
2527 pid_t cpid = fork ();
2529 if (!cpid)
2531 setproctitle ("lto1-wpa-streaming");
2532 do_stream_out (temp_filename, encoder);
2533 exit (0);
2535 /* Fork failed; lets do the job ourseleves. */
2536 else if (cpid == -1)
2537 do_stream_out (temp_filename, encoder);
2538 else
2539 nruns++;
2541 /* Last partition; stream it and wait for all children to die. */
2542 else
2544 int i;
2545 do_stream_out (temp_filename, encoder);
2546 for (i = 0; i < nruns; i++)
2547 wait_for_child ();
2549 asm_nodes_output = true;
2550 #else
2551 do_stream_out (temp_filename, encoder);
2552 #endif
2555 /* Write all output files in WPA mode and the file with the list of
2556 LTRANS units. */
2558 static void
2559 lto_wpa_write_files (void)
2561 unsigned i, n_sets;
2562 ltrans_partition part;
2563 FILE *ltrans_output_list_stream;
2564 char *temp_filename;
2565 vec <char *>temp_filenames = vNULL;
2566 size_t blen;
2568 /* Open the LTRANS output list. */
2569 if (!ltrans_output_list)
2570 fatal_error ("no LTRANS output list filename provided");
2572 timevar_push (TV_WHOPR_WPA);
2574 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2575 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2577 timevar_pop (TV_WHOPR_WPA);
2579 timevar_push (TV_WHOPR_WPA_IO);
2581 /* Generate a prefix for the LTRANS unit files. */
2582 blen = strlen (ltrans_output_list);
2583 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2584 strcpy (temp_filename, ltrans_output_list);
2585 if (blen > sizeof (".out")
2586 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2587 ".out") == 0)
2588 temp_filename[blen - sizeof (".out") + 1] = '\0';
2589 blen = strlen (temp_filename);
2591 n_sets = ltrans_partitions.length ();
2593 /* Sort partitions by size so small ones are compiled last.
2594 FIXME: Even when not reordering we may want to output one list for parallel make
2595 and other for final link command. */
2597 if (!flag_profile_reorder_functions || !flag_profile_use)
2598 ltrans_partitions.qsort (flag_toplevel_reorder
2599 ? cmp_partitions_size
2600 : cmp_partitions_order);
2602 for (i = 0; i < n_sets; i++)
2604 ltrans_partition part = ltrans_partitions[i];
2606 /* Write all the nodes in SET. */
2607 sprintf (temp_filename + blen, "%u.o", i);
2609 if (!quiet_flag)
2610 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2611 if (symtab->dump_file)
2613 lto_symtab_encoder_iterator lsei;
2615 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2616 part->name, temp_filename, part->insns);
2617 fprintf (symtab->dump_file, " Symbols in partition: ");
2618 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2619 lsei_next_in_partition (&lsei))
2621 symtab_node *node = lsei_node (lsei);
2622 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2624 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2625 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2626 lsei_next (&lsei))
2628 symtab_node *node = lsei_node (lsei);
2629 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2631 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2632 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2633 if (cnode
2634 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2635 fprintf (symtab->dump_file, "(body included)");
2636 else
2638 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2639 if (vnode
2640 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2641 fprintf (symtab->dump_file, "(initializer included)");
2645 fprintf (symtab->dump_file, "\n");
2647 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2649 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2651 part->encoder = NULL;
2653 temp_filenames.safe_push (xstrdup (temp_filename));
2655 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2656 if (ltrans_output_list_stream == NULL)
2657 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2658 for (i = 0; i < n_sets; i++)
2660 unsigned int len = strlen (temp_filenames[i]);
2661 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2662 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2663 fatal_error ("writing to LTRANS output list %s: %m",
2664 ltrans_output_list);
2665 free (temp_filenames[i]);
2667 temp_filenames.release();
2669 lto_stats.num_output_files += n_sets;
2671 /* Close the LTRANS output list. */
2672 if (fclose (ltrans_output_list_stream))
2673 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2675 free_ltrans_partitions();
2676 free (temp_filename);
2678 timevar_pop (TV_WHOPR_WPA_IO);
2682 /* If TT is a variable or function decl replace it with its
2683 prevailing variant. */
2684 #define LTO_SET_PREVAIL(tt) \
2685 do {\
2686 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2687 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2689 tt = lto_symtab_prevailing_decl (tt); \
2690 fixed = true; \
2692 } while (0)
2694 /* Ensure that TT isn't a replacable var of function decl. */
2695 #define LTO_NO_PREVAIL(tt) \
2696 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2698 /* Given a tree T replace all fields referring to variables or functions
2699 with their prevailing variant. */
2700 static void
2701 lto_fixup_prevailing_decls (tree t)
2703 enum tree_code code = TREE_CODE (t);
2704 bool fixed = false;
2706 gcc_checking_assert (code != TREE_BINFO);
2707 LTO_NO_PREVAIL (TREE_TYPE (t));
2708 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2709 LTO_NO_PREVAIL (TREE_CHAIN (t));
2710 if (DECL_P (t))
2712 LTO_NO_PREVAIL (DECL_NAME (t));
2713 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2714 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2716 LTO_SET_PREVAIL (DECL_SIZE (t));
2717 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2718 LTO_SET_PREVAIL (DECL_INITIAL (t));
2719 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2720 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2722 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2724 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2726 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2728 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2730 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2732 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2733 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2734 LTO_NO_PREVAIL (DECL_VINDEX (t));
2736 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2738 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2739 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2740 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2741 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2742 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2745 else if (TYPE_P (t))
2747 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2748 LTO_SET_PREVAIL (TYPE_SIZE (t));
2749 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2750 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2751 LTO_NO_PREVAIL (TYPE_NAME (t));
2753 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2754 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2755 LTO_NO_PREVAIL (t->type_non_common.binfo);
2757 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2759 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2760 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2761 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2763 else if (EXPR_P (t))
2765 int i;
2766 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2767 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2769 else if (TREE_CODE (t) == CONSTRUCTOR)
2771 unsigned i;
2772 tree val;
2773 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2774 LTO_SET_PREVAIL (val);
2776 else
2778 switch (code)
2780 case TREE_LIST:
2781 LTO_SET_PREVAIL (TREE_VALUE (t));
2782 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2783 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2784 break;
2785 default:
2786 gcc_unreachable ();
2789 /* If we fixed nothing, then we missed something seen by
2790 mentions_vars_p. */
2791 gcc_checking_assert (fixed);
2793 #undef LTO_SET_PREVAIL
2794 #undef LTO_NO_PREVAIL
2796 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2797 replaces var and function decls with the corresponding prevailing def. */
2799 static void
2800 lto_fixup_state (struct lto_in_decl_state *state)
2802 unsigned i, si;
2804 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2805 we still need to walk from all DECLs to find the reachable
2806 FUNCTION_DECLs and VAR_DECLs. */
2807 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2809 vec<tree, va_gc> *trees = state->streams[si];
2810 for (i = 0; i < vec_safe_length (trees); i++)
2812 tree t = (*trees)[i];
2813 if (VAR_OR_FUNCTION_DECL_P (t)
2814 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2815 (*trees)[i] = lto_symtab_prevailing_decl (t);
2820 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2821 prevailing one. */
2823 static void
2824 lto_fixup_decls (struct lto_file_decl_data **files)
2826 unsigned int i;
2827 tree t;
2829 if (tree_with_vars)
2830 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2831 lto_fixup_prevailing_decls (t);
2833 for (i = 0; files[i]; i++)
2835 struct lto_file_decl_data *file = files[i];
2836 struct lto_in_decl_state *state = file->global_decl_state;
2837 lto_fixup_state (state);
2839 hash_table<decl_state_hasher>::iterator iter;
2840 lto_in_decl_state *elt;
2841 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2842 lto_in_decl_state *, iter)
2843 lto_fixup_state (elt);
2847 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2849 /* Turn file datas for sub files into a single array, so that they look
2850 like separate files for further passes. */
2852 static void
2853 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2855 struct lto_file_decl_data *n, *next;
2856 int i, k;
2858 lto_stats.num_input_files = count;
2859 all_file_decl_data
2860 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2861 /* Set the hooks so that all of the ipa passes can read in their data. */
2862 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2863 for (i = 0, k = 0; i < last_file_ix; i++)
2865 for (n = orig[i]; n != NULL; n = next)
2867 all_file_decl_data[k++] = n;
2868 next = n->next;
2869 n->next = NULL;
2872 all_file_decl_data[k] = NULL;
2873 gcc_assert (k == count);
2876 /* Input file data before flattening (i.e. splitting them to subfiles to support
2877 incremental linking. */
2878 static int real_file_count;
2879 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2881 static void print_lto_report_1 (void);
2883 /* Read all the symbols from the input files FNAMES. NFILES is the
2884 number of files requested in the command line. Instantiate a
2885 global call graph by aggregating all the sub-graphs found in each
2886 file. */
2888 static void
2889 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2891 unsigned int i, last_file_ix;
2892 FILE *resolution;
2893 int count = 0;
2894 struct lto_file_decl_data **decl_data;
2895 symtab_node *snode;
2897 symtab->initialize ();
2899 timevar_push (TV_IPA_LTO_DECL_IN);
2901 #ifdef ACCEL_COMPILER
2902 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2903 #endif
2905 real_file_decl_data
2906 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2907 real_file_count = nfiles;
2909 /* Read the resolution file. */
2910 resolution = NULL;
2911 if (resolution_file_name)
2913 int t;
2914 unsigned num_objects;
2916 resolution = fopen (resolution_file_name, "r");
2917 if (resolution == NULL)
2918 fatal_error ("could not open symbol resolution file: %m");
2920 t = fscanf (resolution, "%u", &num_objects);
2921 gcc_assert (t == 1);
2923 /* True, since the plugin splits the archives. */
2924 gcc_assert (num_objects == nfiles);
2926 symtab->state = LTO_STREAMING;
2928 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2929 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2930 gimple_canonical_type_eq, NULL);
2931 gcc_obstack_init (&tree_scc_hash_obstack);
2932 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2934 /* Register the common node types with the canonical type machinery so
2935 we properly share alias-sets across languages and TUs. Do not
2936 expose the common nodes as type merge target - those that should be
2937 are already exposed so by pre-loading the LTO streamer caches.
2938 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2939 for (i = 0; i < itk_none; ++i)
2940 lto_register_canonical_types (integer_types[i], true);
2941 for (i = 0; i < stk_type_kind_last; ++i)
2942 lto_register_canonical_types (sizetype_tab[i], true);
2943 for (i = 0; i < TI_MAX; ++i)
2944 lto_register_canonical_types (global_trees[i], true);
2945 for (i = 0; i < itk_none; ++i)
2946 lto_register_canonical_types (integer_types[i], false);
2947 for (i = 0; i < stk_type_kind_last; ++i)
2948 lto_register_canonical_types (sizetype_tab[i], false);
2949 for (i = 0; i < TI_MAX; ++i)
2950 lto_register_canonical_types (global_trees[i], false);
2952 if (!quiet_flag)
2953 fprintf (stderr, "Reading object files:");
2955 /* Read all of the object files specified on the command line. */
2956 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2958 struct lto_file_decl_data *file_data = NULL;
2959 if (!quiet_flag)
2961 fprintf (stderr, " %s", fnames[i]);
2962 fflush (stderr);
2965 current_lto_file = lto_obj_file_open (fnames[i], false);
2966 if (!current_lto_file)
2967 break;
2969 file_data = lto_file_read (current_lto_file, resolution, &count);
2970 if (!file_data)
2972 lto_obj_file_close (current_lto_file);
2973 free (current_lto_file);
2974 current_lto_file = NULL;
2975 break;
2978 decl_data[last_file_ix++] = file_data;
2980 lto_obj_file_close (current_lto_file);
2981 free (current_lto_file);
2982 current_lto_file = NULL;
2985 lto_flatten_files (decl_data, count, last_file_ix);
2986 lto_stats.num_input_files = count;
2987 ggc_free(decl_data);
2988 real_file_decl_data = NULL;
2990 if (resolution_file_name)
2991 fclose (resolution);
2993 /* Show the LTO report before launching LTRANS. */
2994 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2995 print_lto_report_1 ();
2997 /* Free gimple type merging datastructures. */
2998 delete tree_scc_hash;
2999 tree_scc_hash = NULL;
3000 obstack_free (&tree_scc_hash_obstack, NULL);
3001 htab_delete (gimple_canonical_types);
3002 gimple_canonical_types = NULL;
3003 delete canonical_type_hash_cache;
3004 canonical_type_hash_cache = NULL;
3006 /* At this stage we know that majority of GGC memory is reachable.
3007 Growing the limits prevents unnecesary invocation of GGC. */
3008 ggc_grow ();
3009 ggc_collect ();
3011 /* Set the hooks so that all of the ipa passes can read in their data. */
3012 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
3014 timevar_pop (TV_IPA_LTO_DECL_IN);
3016 if (!quiet_flag)
3017 fprintf (stderr, "\nReading the callgraph\n");
3019 timevar_push (TV_IPA_LTO_CGRAPH_IO);
3020 /* Read the symtab. */
3021 input_symtab ();
3023 input_offload_tables ();
3025 /* Store resolutions into the symbol table. */
3027 ld_plugin_symbol_resolution_t *res;
3028 FOR_EACH_SYMBOL (snode)
3029 if (snode->real_symbol_p ()
3030 && snode->lto_file_data
3031 && snode->lto_file_data->resolution_map
3032 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
3033 snode->resolution = *res;
3034 for (i = 0; all_file_decl_data[i]; i++)
3035 if (all_file_decl_data[i]->resolution_map)
3037 delete all_file_decl_data[i]->resolution_map;
3038 all_file_decl_data[i]->resolution_map = NULL;
3041 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
3043 if (!quiet_flag)
3044 fprintf (stderr, "Merging declarations\n");
3046 timevar_push (TV_IPA_LTO_DECL_MERGE);
3047 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
3048 need to care about resolving symbols again, we only need to replace
3049 duplicated declarations read from the callgraph and from function
3050 sections. */
3051 if (!flag_ltrans)
3053 lto_symtab_merge_decls ();
3055 /* If there were errors during symbol merging bail out, we have no
3056 good way to recover here. */
3057 if (seen_error ())
3058 fatal_error ("errors during merging of translation units");
3060 /* Fixup all decls. */
3061 lto_fixup_decls (all_file_decl_data);
3063 if (tree_with_vars)
3064 ggc_free (tree_with_vars);
3065 tree_with_vars = NULL;
3066 ggc_collect ();
3068 timevar_pop (TV_IPA_LTO_DECL_MERGE);
3069 /* Each pass will set the appropriate timer. */
3071 if (!quiet_flag)
3072 fprintf (stderr, "Reading summaries\n");
3074 /* Read the IPA summary data. */
3075 if (flag_ltrans)
3076 ipa_read_optimization_summaries ();
3077 else
3078 ipa_read_summaries ();
3080 for (i = 0; all_file_decl_data[i]; i++)
3082 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
3083 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
3084 all_file_decl_data[i]->symtab_node_encoder = NULL;
3085 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
3086 all_file_decl_data[i]->global_decl_state = NULL;
3087 all_file_decl_data[i]->current_decl_state = NULL;
3090 /* Finally merge the cgraph according to the decl merging decisions. */
3091 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
3092 if (symtab->dump_file)
3094 fprintf (symtab->dump_file, "Before merging:\n");
3095 symtab_node::dump_table (symtab->dump_file);
3097 lto_symtab_merge_symbols ();
3098 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
3099 we are still having duplicated comdat groups containing local statics.
3100 We could also just remove them while merging. */
3101 symtab->remove_unreachable_nodes (dump_file);
3102 ggc_collect ();
3103 symtab->state = IPA_SSA;
3105 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
3107 /* Indicate that the cgraph is built and ready. */
3108 symtab->function_flags_ready = true;
3110 ggc_free (all_file_decl_data);
3111 all_file_decl_data = NULL;
3115 /* Materialize all the bodies for all the nodes in the callgraph. */
3117 static void
3118 materialize_cgraph (void)
3120 struct cgraph_node *node;
3121 timevar_id_t lto_timer;
3123 if (!quiet_flag)
3124 fprintf (stderr,
3125 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
3128 FOR_EACH_FUNCTION (node)
3130 if (node->lto_file_data)
3132 lto_materialize_function (node);
3133 lto_stats.num_input_cgraph_nodes++;
3138 /* Start the appropriate timer depending on the mode that we are
3139 operating in. */
3140 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
3141 : (flag_ltrans) ? TV_WHOPR_LTRANS
3142 : TV_LTO;
3143 timevar_push (lto_timer);
3145 current_function_decl = NULL;
3146 set_cfun (NULL);
3148 if (!quiet_flag)
3149 fprintf (stderr, "\n");
3151 timevar_pop (lto_timer);
3155 /* Show various memory usage statistics related to LTO. */
3156 static void
3157 print_lto_report_1 (void)
3159 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3160 fprintf (stderr, "%s statistics\n", pfx);
3162 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3163 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3164 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3165 if (flag_wpa && tree_scc_hash)
3167 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3168 "collision ratio: %f\n", pfx,
3169 (long) tree_scc_hash->size (),
3170 (long) tree_scc_hash->elements (),
3171 tree_scc_hash->collisions ());
3172 hash_table<tree_scc_hasher>::iterator hiter;
3173 tree_scc *scc, *max_scc = NULL;
3174 unsigned max_length = 0;
3175 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3177 unsigned length = 0;
3178 tree_scc *s = scc;
3179 for (; s; s = s->next)
3180 length++;
3181 if (length > max_length)
3183 max_length = length;
3184 max_scc = scc;
3187 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3188 pfx, max_length, max_scc->len);
3189 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3190 num_scc_compares, num_scc_compare_collisions,
3191 num_scc_compare_collisions / (double) num_scc_compares);
3192 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3193 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3194 total_scc_size_merged);
3195 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3196 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3197 pfx, num_prevailing_types, num_type_scc_trees);
3198 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3199 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3200 (long) htab_size (gimple_canonical_types),
3201 (long) htab_elements (gimple_canonical_types),
3202 (long) gimple_canonical_types->searches,
3203 (long) gimple_canonical_types->collisions,
3204 htab_collisions (gimple_canonical_types));
3205 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3206 "%lu elements, %ld searches\n", pfx,
3207 num_canonical_type_hash_entries,
3208 num_canonical_type_hash_queries);
3211 print_lto_report (pfx);
3214 /* Perform whole program analysis (WPA) on the callgraph and write out the
3215 optimization plan. */
3217 static void
3218 do_whole_program_analysis (void)
3220 symtab_node *node;
3222 lto_parallelism = 1;
3224 /* TODO: jobserver communicatoin is not supported, yet. */
3225 if (!strcmp (flag_wpa, "jobserver"))
3226 lto_parallelism = -1;
3227 else
3229 lto_parallelism = atoi (flag_wpa);
3230 if (lto_parallelism <= 0)
3231 lto_parallelism = 0;
3234 timevar_start (TV_PHASE_OPT_GEN);
3236 /* Note that since we are in WPA mode, materialize_cgraph will not
3237 actually read in all the function bodies. It only materializes
3238 the decls and cgraph nodes so that analysis can be performed. */
3239 materialize_cgraph ();
3241 /* Reading in the cgraph uses different timers, start timing WPA now. */
3242 timevar_push (TV_WHOPR_WPA);
3244 if (pre_ipa_mem_report)
3246 fprintf (stderr, "Memory consumption before IPA\n");
3247 dump_memory_report (false);
3250 symtab->function_flags_ready = true;
3252 if (symtab->dump_file)
3253 symtab_node::dump_table (symtab->dump_file);
3254 bitmap_obstack_initialize (NULL);
3255 symtab->state = IPA_SSA;
3257 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3259 if (symtab->dump_file)
3261 fprintf (symtab->dump_file, "Optimized ");
3262 symtab_node::dump_table (symtab->dump_file);
3264 #ifdef ENABLE_CHECKING
3265 symtab_node::verify_symtab_nodes ();
3266 #endif
3267 bitmap_obstack_release (NULL);
3269 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3270 timevar_pop (TV_WHOPR_WPA);
3272 timevar_push (TV_WHOPR_PARTITIONING);
3273 if (flag_lto_partition == LTO_PARTITION_1TO1)
3274 lto_1_to_1_map ();
3275 else if (flag_lto_partition == LTO_PARTITION_MAX)
3276 lto_max_map ();
3277 else if (flag_lto_partition == LTO_PARTITION_ONE)
3278 lto_balanced_map (1);
3279 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3280 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3281 else
3282 gcc_unreachable ();
3284 /* Inline summaries are needed for balanced partitioning. Free them now so
3285 the memory can be used for streamer caches. */
3286 inline_free_summary ();
3288 /* AUX pointers are used by partitioning code to bookkeep number of
3289 partitions symbol is in. This is no longer needed. */
3290 FOR_EACH_SYMBOL (node)
3291 node->aux = NULL;
3293 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3295 /* Find out statics that need to be promoted
3296 to globals with hidden visibility because they are accessed from multiple
3297 partitions. */
3298 lto_promote_cross_file_statics ();
3299 timevar_pop (TV_WHOPR_PARTITIONING);
3301 timevar_stop (TV_PHASE_OPT_GEN);
3303 /* Collect a last time - in lto_wpa_write_files we may end up forking
3304 with the idea that this doesn't increase memory usage. So we
3305 absoultely do not want to collect after that. */
3306 ggc_collect ();
3308 timevar_start (TV_PHASE_STREAM_OUT);
3309 if (!quiet_flag)
3311 fprintf (stderr, "\nStreaming out");
3312 fflush (stderr);
3314 lto_wpa_write_files ();
3315 if (!quiet_flag)
3316 fprintf (stderr, "\n");
3317 timevar_stop (TV_PHASE_STREAM_OUT);
3319 if (post_ipa_mem_report)
3321 fprintf (stderr, "Memory consumption after IPA\n");
3322 dump_memory_report (false);
3325 /* Show the LTO report before launching LTRANS. */
3326 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3327 print_lto_report_1 ();
3328 if (mem_report_wpa)
3329 dump_memory_report (true);
3333 static GTY(()) tree lto_eh_personality_decl;
3335 /* Return the LTO personality function decl. */
3337 tree
3338 lto_eh_personality (void)
3340 if (!lto_eh_personality_decl)
3342 /* Use the first personality DECL for our personality if we don't
3343 support multiple ones. This ensures that we don't artificially
3344 create the need for them in a single-language program. */
3345 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3346 lto_eh_personality_decl = first_personality_decl;
3347 else
3348 lto_eh_personality_decl = lhd_gcc_personality ();
3351 return lto_eh_personality_decl;
3354 /* Set the process name based on the LTO mode. */
3356 static void
3357 lto_process_name (void)
3359 if (flag_lto)
3360 setproctitle ("lto1-lto");
3361 if (flag_wpa)
3362 setproctitle ("lto1-wpa");
3363 if (flag_ltrans)
3364 setproctitle ("lto1-ltrans");
3368 /* Initialize the LTO front end. */
3370 static void
3371 lto_init (void)
3373 lto_process_name ();
3374 lto_streamer_hooks_init ();
3375 lto_reader_init ();
3376 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3377 memset (&lto_stats, 0, sizeof (lto_stats));
3378 bitmap_obstack_initialize (NULL);
3379 gimple_register_cfg_hooks ();
3383 /* Main entry point for the GIMPLE front end. This front end has
3384 three main personalities:
3386 - LTO (-flto). All the object files on the command line are
3387 loaded in memory and processed as a single translation unit.
3388 This is the traditional link-time optimization behavior.
3390 - WPA (-fwpa). Only the callgraph and summary information for
3391 files in the command file are loaded. A single callgraph
3392 (without function bodies) is instantiated for the whole set of
3393 files. IPA passes are only allowed to analyze the call graph
3394 and make transformation decisions. The callgraph is
3395 partitioned, each partition is written to a new object file
3396 together with the transformation decisions.
3398 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3399 summary files from running again. Since WPA computed summary
3400 information and decided what transformations to apply, LTRANS
3401 simply applies them. */
3403 void
3404 lto_main (void)
3406 /* LTO is called as a front end, even though it is not a front end.
3407 Because it is called as a front end, TV_PHASE_PARSING and
3408 TV_PARSE_GLOBAL are active, and we need to turn them off while
3409 doing LTO. Later we turn them back on so they are active up in
3410 toplev.c. */
3411 timevar_pop (TV_PARSE_GLOBAL);
3412 timevar_stop (TV_PHASE_PARSING);
3414 timevar_start (TV_PHASE_SETUP);
3416 /* Initialize the LTO front end. */
3417 lto_init ();
3419 timevar_stop (TV_PHASE_SETUP);
3420 timevar_start (TV_PHASE_STREAM_IN);
3422 /* Read all the symbols and call graph from all the files in the
3423 command line. */
3424 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3426 timevar_stop (TV_PHASE_STREAM_IN);
3428 if (!seen_error ())
3430 /* If WPA is enabled analyze the whole call graph and create an
3431 optimization plan. Otherwise, read in all the function
3432 bodies and continue with optimization. */
3433 if (flag_wpa)
3434 do_whole_program_analysis ();
3435 else
3437 timevar_start (TV_PHASE_OPT_GEN);
3439 materialize_cgraph ();
3440 if (!flag_ltrans)
3441 lto_promote_statics_nonwpa ();
3443 /* Let the middle end know that we have read and merged all of
3444 the input files. */
3445 symtab->compile ();
3447 timevar_stop (TV_PHASE_OPT_GEN);
3449 /* FIXME lto, if the processes spawned by WPA fail, we miss
3450 the chance to print WPA's report, so WPA will call
3451 print_lto_report before launching LTRANS. If LTRANS was
3452 launched directly by the driver we would not need to do
3453 this. */
3454 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3455 print_lto_report_1 ();
3459 /* Here we make LTO pretend to be a parser. */
3460 timevar_start (TV_PHASE_PARSING);
3461 timevar_push (TV_PARSE_GLOBAL);
3464 #include "gt-lto-lto.h"