libgomp: Use pthread mutexes in the nvptx plugin.
[official-gcc.git] / gcc / lto / lto.c
blob96e5fd18ac134fb67c05c51818f2a24991bf82a5
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "toplev.h"
26 #include "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "real.h"
37 #include "fixed-value.h"
38 #include "tree.h"
39 #include "fold-const.h"
40 #include "stor-layout.h"
41 #include "diagnostic-core.h"
42 #include "tm.h"
43 #include "predict.h"
44 #include "basic-block.h"
45 #include "hash-map.h"
46 #include "is-a.h"
47 #include "plugin-api.h"
48 #include "hard-reg-set.h"
49 #include "input.h"
50 #include "function.h"
51 #include "ipa-ref.h"
52 #include "cgraph.h"
53 #include "tree-ssa-operands.h"
54 #include "tree-pass.h"
55 #include "langhooks.h"
56 #include "bitmap.h"
57 #include "inchash.h"
58 #include "alloc-pool.h"
59 #include "symbol-summary.h"
60 #include "ipa-prop.h"
61 #include "common.h"
62 #include "debug.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-expr.h"
66 #include "gimple.h"
67 #include "lto.h"
68 #include "lto-tree.h"
69 #include "lto-streamer.h"
70 #include "lto-section-names.h"
71 #include "tree-streamer.h"
72 #include "splay-tree.h"
73 #include "lto-partition.h"
74 #include "data-streamer.h"
75 #include "context.h"
76 #include "pass_manager.h"
77 #include "ipa-inline.h"
78 #include "params.h"
79 #include "ipa-utils.h"
82 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver. */
83 static int lto_parallelism;
85 static GTY(()) tree first_personality_decl;
87 /* Returns a hash code for P. */
89 static hashval_t
90 hash_name (const void *p)
92 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
93 return (hashval_t) htab_hash_string (ds->name);
97 /* Returns nonzero if P1 and P2 are equal. */
99 static int
100 eq_name (const void *p1, const void *p2)
102 const struct lto_section_slot *s1 =
103 (const struct lto_section_slot *) p1;
104 const struct lto_section_slot *s2 =
105 (const struct lto_section_slot *) p2;
107 return strcmp (s1->name, s2->name) == 0;
110 /* Free lto_section_slot */
112 static void
113 free_with_string (void *arg)
115 struct lto_section_slot *s = (struct lto_section_slot *)arg;
117 free (CONST_CAST (char *, s->name));
118 free (arg);
121 /* Create section hash table */
123 htab_t
124 lto_obj_create_section_hash_table (void)
126 return htab_create (37, hash_name, eq_name, free_with_string);
129 /* Delete an allocated integer KEY in the splay tree. */
131 static void
132 lto_splay_tree_delete_id (splay_tree_key key)
134 free ((void *) key);
137 /* Compare splay tree node ids A and B. */
139 static int
140 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
142 unsigned HOST_WIDE_INT ai;
143 unsigned HOST_WIDE_INT bi;
145 ai = *(unsigned HOST_WIDE_INT *) a;
146 bi = *(unsigned HOST_WIDE_INT *) b;
148 if (ai < bi)
149 return -1;
150 else if (ai > bi)
151 return 1;
152 return 0;
155 /* Look up splay tree node by ID in splay tree T. */
157 static splay_tree_node
158 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
160 return splay_tree_lookup (t, (splay_tree_key) &id);
163 /* Check if KEY has ID. */
165 static bool
166 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
168 return *(unsigned HOST_WIDE_INT *) key == id;
171 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
172 The ID is allocated separately because we need HOST_WIDE_INTs which may
173 be wider than a splay_tree_key. */
175 static void
176 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
177 struct lto_file_decl_data *file_data)
179 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
180 *idp = id;
181 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
184 /* Create a splay tree. */
186 static splay_tree
187 lto_splay_tree_new (void)
189 return splay_tree_new (lto_splay_tree_compare_ids,
190 lto_splay_tree_delete_id,
191 NULL);
194 /* Return true when NODE has a clone that is analyzed (i.e. we need
195 to load its body even if the node itself is not needed). */
197 static bool
198 has_analyzed_clone_p (struct cgraph_node *node)
200 struct cgraph_node *orig = node;
201 node = node->clones;
202 if (node)
203 while (node != orig)
205 if (node->analyzed)
206 return true;
207 if (node->clones)
208 node = node->clones;
209 else if (node->next_sibling_clone)
210 node = node->next_sibling_clone;
211 else
213 while (node != orig && !node->next_sibling_clone)
214 node = node->clone_of;
215 if (node != orig)
216 node = node->next_sibling_clone;
219 return false;
222 /* Read the function body for the function associated with NODE. */
224 static void
225 lto_materialize_function (struct cgraph_node *node)
227 tree decl;
229 decl = node->decl;
230 /* Read in functions with body (analyzed nodes)
231 and also functions that are needed to produce virtual clones. */
232 if ((node->has_gimple_body_p () && node->analyzed)
233 || node->used_as_abstract_origin
234 || has_analyzed_clone_p (node))
236 /* Clones don't need to be read. */
237 if (node->clone_of)
238 return;
239 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
240 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
243 /* Let the middle end know about the function. */
244 rest_of_decl_compilation (decl, 1, 0);
248 /* Decode the content of memory pointed to by DATA in the in decl
249 state object STATE. DATA_IN points to a data_in structure for
250 decoding. Return the address after the decoded object in the
251 input. */
253 static const uint32_t *
254 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
255 struct lto_in_decl_state *state)
257 uint32_t ix;
258 tree decl;
259 uint32_t i, j;
261 ix = *data++;
262 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
263 if (!VAR_OR_FUNCTION_DECL_P (decl))
265 gcc_assert (decl == void_type_node);
266 decl = NULL_TREE;
268 state->fn_decl = decl;
270 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
272 uint32_t size = *data++;
273 vec<tree, va_gc> *decls = NULL;
274 vec_alloc (decls, size);
276 for (j = 0; j < size; j++)
277 vec_safe_push (decls,
278 streamer_tree_cache_get_tree (data_in->reader_cache,
279 data[j]));
281 state->streams[i] = decls;
282 data += size;
285 return data;
289 /* Global canonical type table. */
290 static htab_t gimple_canonical_types;
291 static hash_map<const_tree, hashval_t> *canonical_type_hash_cache;
292 static unsigned long num_canonical_type_hash_entries;
293 static unsigned long num_canonical_type_hash_queries;
295 static void iterative_hash_canonical_type (tree type, inchash::hash &hstate);
296 static hashval_t gimple_canonical_type_hash (const void *p);
297 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
299 /* Returning a hash value for gimple type TYPE.
301 The hash value returned is equal for types considered compatible
302 by gimple_canonical_types_compatible_p. */
304 static hashval_t
305 hash_canonical_type (tree type)
307 inchash::hash hstate;
309 /* Combine a few common features of types so that types are grouped into
310 smaller sets; when searching for existing matching types to merge,
311 only existing types having the same features as the new type will be
312 checked. */
313 hstate.add_int (TREE_CODE (type));
314 hstate.add_int (TYPE_MODE (type));
316 /* Incorporate common features of numerical types. */
317 if (INTEGRAL_TYPE_P (type)
318 || SCALAR_FLOAT_TYPE_P (type)
319 || FIXED_POINT_TYPE_P (type)
320 || TREE_CODE (type) == OFFSET_TYPE
321 || POINTER_TYPE_P (type))
323 hstate.add_int (TYPE_UNSIGNED (type));
324 hstate.add_int (TYPE_PRECISION (type));
327 if (VECTOR_TYPE_P (type))
329 hstate.add_int (TYPE_VECTOR_SUBPARTS (type));
330 hstate.add_int (TYPE_UNSIGNED (type));
333 if (TREE_CODE (type) == COMPLEX_TYPE)
334 hstate.add_int (TYPE_UNSIGNED (type));
336 /* For pointer and reference types, fold in information about the type
337 pointed to but do not recurse to the pointed-to type. */
338 if (POINTER_TYPE_P (type))
340 hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
341 hstate.add_int (TREE_CODE (TREE_TYPE (type)));
344 /* For integer types hash only the string flag. */
345 if (TREE_CODE (type) == INTEGER_TYPE)
346 hstate.add_int (TYPE_STRING_FLAG (type));
348 /* For array types hash the domain bounds and the string flag. */
349 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
351 hstate.add_int (TYPE_STRING_FLAG (type));
352 /* OMP lowering can introduce error_mark_node in place of
353 random local decls in types. */
354 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
355 inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
356 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
357 inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
360 /* Recurse for aggregates with a single element type. */
361 if (TREE_CODE (type) == ARRAY_TYPE
362 || TREE_CODE (type) == COMPLEX_TYPE
363 || TREE_CODE (type) == VECTOR_TYPE)
364 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
366 /* Incorporate function return and argument types. */
367 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
369 unsigned na;
370 tree p;
372 /* For method types also incorporate their parent class. */
373 if (TREE_CODE (type) == METHOD_TYPE)
374 iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type), hstate);
376 iterative_hash_canonical_type (TREE_TYPE (type), hstate);
378 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
380 iterative_hash_canonical_type (TREE_VALUE (p), hstate);
381 na++;
384 hstate.add_int (na);
387 if (RECORD_OR_UNION_TYPE_P (type))
389 unsigned nf;
390 tree f;
392 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
393 if (TREE_CODE (f) == FIELD_DECL)
395 iterative_hash_canonical_type (TREE_TYPE (f), hstate);
396 nf++;
399 hstate.add_int (nf);
402 return hstate.end();
405 /* Returning a hash value for gimple type TYPE combined with VAL. */
407 static void
408 iterative_hash_canonical_type (tree type, inchash::hash &hstate)
410 hashval_t v;
411 /* An already processed type. */
412 if (TYPE_CANONICAL (type))
414 type = TYPE_CANONICAL (type);
415 v = gimple_canonical_type_hash (type);
417 else
419 /* Canonical types should not be able to form SCCs by design, this
420 recursion is just because we do not register canonical types in
421 optimal order. To avoid quadratic behavior also register the
422 type here. */
423 v = hash_canonical_type (type);
424 gimple_register_canonical_type_1 (type, v);
426 hstate.add_int (v);
429 /* Returns the hash for a canonical type P. */
431 static hashval_t
432 gimple_canonical_type_hash (const void *p)
434 num_canonical_type_hash_queries++;
435 hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
436 gcc_assert (slot != NULL);
437 return *slot;
441 /* The TYPE_CANONICAL merging machinery. It should closely resemble
442 the middle-end types_compatible_p function. It needs to avoid
443 claiming types are different for types that should be treated
444 the same with respect to TBAA. Canonical types are also used
445 for IL consistency checks via the useless_type_conversion_p
446 predicate which does not handle all type kinds itself but falls
447 back to pointer-comparison of TYPE_CANONICAL for aggregates
448 for example. */
450 /* Return true iff T1 and T2 are structurally identical for what
451 TBAA is concerned. */
453 static bool
454 gimple_canonical_types_compatible_p (tree t1, tree t2)
456 /* Before starting to set up the SCC machinery handle simple cases. */
458 /* Check first for the obvious case of pointer identity. */
459 if (t1 == t2)
460 return true;
462 /* Check that we have two types to compare. */
463 if (t1 == NULL_TREE || t2 == NULL_TREE)
464 return false;
466 /* If the types have been previously registered and found equal
467 they still are. */
468 if (TYPE_CANONICAL (t1)
469 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
470 return true;
472 /* Can't be the same type if the types don't have the same code. */
473 if (TREE_CODE (t1) != TREE_CODE (t2))
474 return false;
476 /* Qualifiers do not matter for canonical type comparison purposes. */
478 /* Void types and nullptr types are always the same. */
479 if (TREE_CODE (t1) == VOID_TYPE
480 || TREE_CODE (t1) == NULLPTR_TYPE)
481 return true;
483 /* Can't be the same type if they have different mode. */
484 if (TYPE_MODE (t1) != TYPE_MODE (t2))
485 return false;
487 /* Non-aggregate types can be handled cheaply. */
488 if (INTEGRAL_TYPE_P (t1)
489 || SCALAR_FLOAT_TYPE_P (t1)
490 || FIXED_POINT_TYPE_P (t1)
491 || TREE_CODE (t1) == VECTOR_TYPE
492 || TREE_CODE (t1) == COMPLEX_TYPE
493 || TREE_CODE (t1) == OFFSET_TYPE
494 || POINTER_TYPE_P (t1))
496 /* Can't be the same type if they have different sign or precision. */
497 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
498 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
499 return false;
501 if (TREE_CODE (t1) == INTEGER_TYPE
502 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
503 return false;
505 /* For canonical type comparisons we do not want to build SCCs
506 so we cannot compare pointed-to types. But we can, for now,
507 require the same pointed-to type kind and match what
508 useless_type_conversion_p would do. */
509 if (POINTER_TYPE_P (t1))
511 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
512 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
513 return false;
515 if (TREE_CODE (TREE_TYPE (t1)) != TREE_CODE (TREE_TYPE (t2)))
516 return false;
519 /* Tail-recurse to components. */
520 if (TREE_CODE (t1) == VECTOR_TYPE
521 || TREE_CODE (t1) == COMPLEX_TYPE)
522 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
523 TREE_TYPE (t2));
525 return true;
528 /* Do type-specific comparisons. */
529 switch (TREE_CODE (t1))
531 case ARRAY_TYPE:
532 /* Array types are the same if the element types are the same and
533 the number of elements are the same. */
534 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
535 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
536 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
537 return false;
538 else
540 tree i1 = TYPE_DOMAIN (t1);
541 tree i2 = TYPE_DOMAIN (t2);
543 /* For an incomplete external array, the type domain can be
544 NULL_TREE. Check this condition also. */
545 if (i1 == NULL_TREE && i2 == NULL_TREE)
546 return true;
547 else if (i1 == NULL_TREE || i2 == NULL_TREE)
548 return false;
549 else
551 tree min1 = TYPE_MIN_VALUE (i1);
552 tree min2 = TYPE_MIN_VALUE (i2);
553 tree max1 = TYPE_MAX_VALUE (i1);
554 tree max2 = TYPE_MAX_VALUE (i2);
556 /* The minimum/maximum values have to be the same. */
557 if ((min1 == min2
558 || (min1 && min2
559 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
560 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
561 || operand_equal_p (min1, min2, 0))))
562 && (max1 == max2
563 || (max1 && max2
564 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
565 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
566 || operand_equal_p (max1, max2, 0)))))
567 return true;
568 else
569 return false;
573 case METHOD_TYPE:
574 case FUNCTION_TYPE:
575 /* Function types are the same if the return type and arguments types
576 are the same. */
577 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
578 return false;
580 if (!comp_type_attributes (t1, t2))
581 return false;
583 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
584 return true;
585 else
587 tree parms1, parms2;
589 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
590 parms1 && parms2;
591 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
593 if (!gimple_canonical_types_compatible_p
594 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
595 return false;
598 if (parms1 || parms2)
599 return false;
601 return true;
604 case RECORD_TYPE:
605 case UNION_TYPE:
606 case QUAL_UNION_TYPE:
608 tree f1, f2;
610 /* For aggregate types, all the fields must be the same. */
611 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
612 f1 || f2;
613 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
615 /* Skip non-fields. */
616 while (f1 && TREE_CODE (f1) != FIELD_DECL)
617 f1 = TREE_CHAIN (f1);
618 while (f2 && TREE_CODE (f2) != FIELD_DECL)
619 f2 = TREE_CHAIN (f2);
620 if (!f1 || !f2)
621 break;
622 /* The fields must have the same name, offset and type. */
623 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
624 || !gimple_compare_field_offset (f1, f2)
625 || !gimple_canonical_types_compatible_p
626 (TREE_TYPE (f1), TREE_TYPE (f2)))
627 return false;
630 /* If one aggregate has more fields than the other, they
631 are not the same. */
632 if (f1 || f2)
633 return false;
635 return true;
638 default:
639 gcc_unreachable ();
644 /* Returns nonzero if P1 and P2 are equal. */
646 static int
647 gimple_canonical_type_eq (const void *p1, const void *p2)
649 const_tree t1 = (const_tree) p1;
650 const_tree t2 = (const_tree) p2;
651 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
652 CONST_CAST_TREE (t2));
655 /* Main worker for gimple_register_canonical_type. */
657 static void
658 gimple_register_canonical_type_1 (tree t, hashval_t hash)
660 void **slot;
662 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
664 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
665 if (*slot)
667 tree new_type = (tree)(*slot);
668 gcc_checking_assert (new_type != t);
669 TYPE_CANONICAL (t) = new_type;
671 else
673 TYPE_CANONICAL (t) = t;
674 *slot = (void *) t;
675 /* Cache the just computed hash value. */
676 num_canonical_type_hash_entries++;
677 bool existed_p = canonical_type_hash_cache->put (t, hash);
678 gcc_assert (!existed_p);
682 /* Register type T in the global type table gimple_types and set
683 TYPE_CANONICAL of T accordingly.
684 This is used by LTO to merge structurally equivalent types for
685 type-based aliasing purposes across different TUs and languages.
687 ??? This merging does not exactly match how the tree.c middle-end
688 functions will assign TYPE_CANONICAL when new types are created
689 during optimization (which at least happens for pointer and array
690 types). */
692 static void
693 gimple_register_canonical_type (tree t)
695 if (TYPE_CANONICAL (t))
696 return;
698 gimple_register_canonical_type_1 (t, hash_canonical_type (t));
701 /* Re-compute TYPE_CANONICAL for NODE and related types. */
703 static void
704 lto_register_canonical_types (tree node, bool first_p)
706 if (!node
707 || !TYPE_P (node))
708 return;
710 if (first_p)
711 TYPE_CANONICAL (node) = NULL_TREE;
713 if (POINTER_TYPE_P (node)
714 || TREE_CODE (node) == COMPLEX_TYPE
715 || TREE_CODE (node) == ARRAY_TYPE)
716 lto_register_canonical_types (TREE_TYPE (node), first_p);
718 if (!first_p)
719 gimple_register_canonical_type (node);
723 /* Remember trees that contains references to declarations. */
724 static GTY(()) vec <tree, va_gc> *tree_with_vars;
726 #define CHECK_VAR(tt) \
727 do \
729 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
730 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
731 return true; \
732 } while (0)
734 #define CHECK_NO_VAR(tt) \
735 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
737 /* Check presence of pointers to decls in fields of a tree_typed T. */
739 static inline bool
740 mentions_vars_p_typed (tree t)
742 CHECK_NO_VAR (TREE_TYPE (t));
743 return false;
746 /* Check presence of pointers to decls in fields of a tree_common T. */
748 static inline bool
749 mentions_vars_p_common (tree t)
751 if (mentions_vars_p_typed (t))
752 return true;
753 CHECK_NO_VAR (TREE_CHAIN (t));
754 return false;
757 /* Check presence of pointers to decls in fields of a decl_minimal T. */
759 static inline bool
760 mentions_vars_p_decl_minimal (tree t)
762 if (mentions_vars_p_common (t))
763 return true;
764 CHECK_NO_VAR (DECL_NAME (t));
765 CHECK_VAR (DECL_CONTEXT (t));
766 return false;
769 /* Check presence of pointers to decls in fields of a decl_common T. */
771 static inline bool
772 mentions_vars_p_decl_common (tree t)
774 if (mentions_vars_p_decl_minimal (t))
775 return true;
776 CHECK_VAR (DECL_SIZE (t));
777 CHECK_VAR (DECL_SIZE_UNIT (t));
778 CHECK_VAR (DECL_INITIAL (t));
779 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
780 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
781 return false;
784 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
786 static inline bool
787 mentions_vars_p_decl_with_vis (tree t)
789 if (mentions_vars_p_decl_common (t))
790 return true;
792 /* Accessor macro has side-effects, use field-name here. */
793 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
794 return false;
797 /* Check presence of pointers to decls in fields of a decl_non_common T. */
799 static inline bool
800 mentions_vars_p_decl_non_common (tree t)
802 if (mentions_vars_p_decl_with_vis (t))
803 return true;
804 CHECK_NO_VAR (DECL_RESULT_FLD (t));
805 return false;
808 /* Check presence of pointers to decls in fields of a decl_non_common T. */
810 static bool
811 mentions_vars_p_function (tree t)
813 if (mentions_vars_p_decl_non_common (t))
814 return true;
815 CHECK_NO_VAR (DECL_ARGUMENTS (t));
816 CHECK_NO_VAR (DECL_VINDEX (t));
817 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
818 return false;
821 /* Check presence of pointers to decls in fields of a field_decl T. */
823 static bool
824 mentions_vars_p_field_decl (tree t)
826 if (mentions_vars_p_decl_common (t))
827 return true;
828 CHECK_VAR (DECL_FIELD_OFFSET (t));
829 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
830 CHECK_NO_VAR (DECL_QUALIFIER (t));
831 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
832 CHECK_NO_VAR (DECL_FCONTEXT (t));
833 return false;
836 /* Check presence of pointers to decls in fields of a type T. */
838 static bool
839 mentions_vars_p_type (tree t)
841 if (mentions_vars_p_common (t))
842 return true;
843 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
844 CHECK_VAR (TYPE_SIZE (t));
845 CHECK_VAR (TYPE_SIZE_UNIT (t));
846 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
847 CHECK_NO_VAR (TYPE_NAME (t));
849 CHECK_VAR (TYPE_MINVAL (t));
850 CHECK_VAR (TYPE_MAXVAL (t));
852 /* Accessor is for derived node types only. */
853 CHECK_NO_VAR (t->type_non_common.binfo);
855 CHECK_VAR (TYPE_CONTEXT (t));
856 CHECK_NO_VAR (TYPE_CANONICAL (t));
857 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
858 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
859 return false;
862 /* Check presence of pointers to decls in fields of a BINFO T. */
864 static bool
865 mentions_vars_p_binfo (tree t)
867 unsigned HOST_WIDE_INT i, n;
869 if (mentions_vars_p_common (t))
870 return true;
871 CHECK_VAR (BINFO_VTABLE (t));
872 CHECK_NO_VAR (BINFO_OFFSET (t));
873 CHECK_NO_VAR (BINFO_VIRTUALS (t));
874 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
875 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
876 for (i = 0; i < n; i++)
877 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
878 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
879 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
880 n = BINFO_N_BASE_BINFOS (t);
881 for (i = 0; i < n; i++)
882 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
883 return false;
886 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
888 static bool
889 mentions_vars_p_constructor (tree t)
891 unsigned HOST_WIDE_INT idx;
892 constructor_elt *ce;
894 if (mentions_vars_p_typed (t))
895 return true;
897 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
899 CHECK_NO_VAR (ce->index);
900 CHECK_VAR (ce->value);
902 return false;
905 /* Check presence of pointers to decls in fields of an expression tree T. */
907 static bool
908 mentions_vars_p_expr (tree t)
910 int i;
911 if (mentions_vars_p_typed (t))
912 return true;
913 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
914 CHECK_VAR (TREE_OPERAND (t, i));
915 return false;
918 /* Check presence of pointers to decls in fields of an OMP_CLAUSE T. */
920 static bool
921 mentions_vars_p_omp_clause (tree t)
923 int i;
924 if (mentions_vars_p_common (t))
925 return true;
926 for (i = omp_clause_num_ops[OMP_CLAUSE_CODE (t)] - 1; i >= 0; --i)
927 CHECK_VAR (OMP_CLAUSE_OPERAND (t, i));
928 return false;
931 /* Check presence of pointers to decls that needs later fixup in T. */
933 static bool
934 mentions_vars_p (tree t)
936 switch (TREE_CODE (t))
938 case IDENTIFIER_NODE:
939 break;
941 case TREE_LIST:
942 CHECK_VAR (TREE_VALUE (t));
943 CHECK_VAR (TREE_PURPOSE (t));
944 CHECK_NO_VAR (TREE_CHAIN (t));
945 break;
947 case FIELD_DECL:
948 return mentions_vars_p_field_decl (t);
950 case LABEL_DECL:
951 case CONST_DECL:
952 case PARM_DECL:
953 case RESULT_DECL:
954 case IMPORTED_DECL:
955 case NAMESPACE_DECL:
956 case NAMELIST_DECL:
957 return mentions_vars_p_decl_common (t);
959 case VAR_DECL:
960 return mentions_vars_p_decl_with_vis (t);
962 case TYPE_DECL:
963 return mentions_vars_p_decl_non_common (t);
965 case FUNCTION_DECL:
966 return mentions_vars_p_function (t);
968 case TREE_BINFO:
969 return mentions_vars_p_binfo (t);
971 case PLACEHOLDER_EXPR:
972 return mentions_vars_p_common (t);
974 case BLOCK:
975 case TRANSLATION_UNIT_DECL:
976 case OPTIMIZATION_NODE:
977 case TARGET_OPTION_NODE:
978 break;
980 case CONSTRUCTOR:
981 return mentions_vars_p_constructor (t);
983 case OMP_CLAUSE:
984 return mentions_vars_p_omp_clause (t);
986 default:
987 if (TYPE_P (t))
989 if (mentions_vars_p_type (t))
990 return true;
992 else if (EXPR_P (t))
994 if (mentions_vars_p_expr (t))
995 return true;
997 else if (CONSTANT_CLASS_P (t))
998 CHECK_NO_VAR (TREE_TYPE (t));
999 else
1000 gcc_unreachable ();
1002 return false;
1006 /* Return the resolution for the decl with index INDEX from DATA_IN. */
1008 static enum ld_plugin_symbol_resolution
1009 get_resolution (struct data_in *data_in, unsigned index)
1011 if (data_in->globals_resolution.exists ())
1013 ld_plugin_symbol_resolution_t ret;
1014 /* We can have references to not emitted functions in
1015 DECL_FUNCTION_PERSONALITY at least. So we can and have
1016 to indeed return LDPR_UNKNOWN in some cases. */
1017 if (data_in->globals_resolution.length () <= index)
1018 return LDPR_UNKNOWN;
1019 ret = data_in->globals_resolution[index];
1020 return ret;
1022 else
1023 /* Delay resolution finding until decl merging. */
1024 return LDPR_UNKNOWN;
1027 /* We need to record resolutions until symbol table is read. */
1028 static void
1029 register_resolution (struct lto_file_decl_data *file_data, tree decl,
1030 enum ld_plugin_symbol_resolution resolution)
1032 if (resolution == LDPR_UNKNOWN)
1033 return;
1034 if (!file_data->resolution_map)
1035 file_data->resolution_map
1036 = new hash_map<tree, ld_plugin_symbol_resolution>;
1037 file_data->resolution_map->put (decl, resolution);
1040 /* Register DECL with the global symbol table and change its
1041 name if necessary to avoid name clashes for static globals across
1042 different files. */
1044 static void
1045 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
1046 unsigned ix)
1048 tree context;
1050 /* Variable has file scope, not local. */
1051 if (!TREE_PUBLIC (decl)
1052 && !((context = decl_function_context (decl))
1053 && auto_var_in_fn_p (decl, context)))
1054 rest_of_decl_compilation (decl, 1, 0);
1056 /* If this variable has already been declared, queue the
1057 declaration for merging. */
1058 if (TREE_PUBLIC (decl))
1059 register_resolution (data_in->file_data,
1060 decl, get_resolution (data_in, ix));
1064 /* Register DECL with the global symbol table and change its
1065 name if necessary to avoid name clashes for static globals across
1066 different files. DATA_IN contains descriptors and tables for the
1067 file being read. */
1069 static void
1070 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
1071 unsigned ix)
1073 /* If this variable has already been declared, queue the
1074 declaration for merging. */
1075 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
1076 register_resolution (data_in->file_data,
1077 decl, get_resolution (data_in, ix));
1081 /* For the type T re-materialize it in the type variant list and
1082 the pointer/reference-to chains. */
1084 static void
1085 lto_fixup_prevailing_type (tree t)
1087 /* The following re-creates proper variant lists while fixing up
1088 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1089 variant list state before fixup is broken. */
1091 /* If we are not our own variant leader link us into our new leaders
1092 variant list. */
1093 if (TYPE_MAIN_VARIANT (t) != t)
1095 tree mv = TYPE_MAIN_VARIANT (t);
1096 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1097 TYPE_NEXT_VARIANT (mv) = t;
1100 /* The following reconstructs the pointer chains
1101 of the new pointed-to type if we are a main variant. We do
1102 not stream those so they are broken before fixup. */
1103 if (TREE_CODE (t) == POINTER_TYPE
1104 && TYPE_MAIN_VARIANT (t) == t)
1106 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1107 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1109 else if (TREE_CODE (t) == REFERENCE_TYPE
1110 && TYPE_MAIN_VARIANT (t) == t)
1112 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1113 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1118 /* We keep prevailing tree SCCs in a hashtable with manual collision
1119 handling (in case all hashes compare the same) and keep the colliding
1120 entries in the tree_scc->next chain. */
1122 struct tree_scc
1124 tree_scc *next;
1125 /* Hash of the whole SCC. */
1126 hashval_t hash;
1127 /* Number of trees in the SCC. */
1128 unsigned len;
1129 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1130 which share the same individual tree hash). */
1131 unsigned entry_len;
1132 /* The members of the SCC.
1133 We only need to remember the first entry node candidate for prevailing
1134 SCCs (but of course have access to all entries for SCCs we are
1135 processing).
1136 ??? For prevailing SCCs we really only need hash and the first
1137 entry candidate, but that's too awkward to implement. */
1138 tree entries[1];
1141 struct tree_scc_hasher : typed_noop_remove <tree_scc>
1143 typedef tree_scc value_type;
1144 typedef tree_scc compare_type;
1145 static inline hashval_t hash (const value_type *);
1146 static inline bool equal (const value_type *, const compare_type *);
1149 hashval_t
1150 tree_scc_hasher::hash (const value_type *scc)
1152 return scc->hash;
1155 bool
1156 tree_scc_hasher::equal (const value_type *scc1, const compare_type *scc2)
1158 if (scc1->hash != scc2->hash
1159 || scc1->len != scc2->len
1160 || scc1->entry_len != scc2->entry_len)
1161 return false;
1162 return true;
1165 static hash_table<tree_scc_hasher> *tree_scc_hash;
1166 static struct obstack tree_scc_hash_obstack;
1168 static unsigned long num_merged_types;
1169 static unsigned long num_prevailing_types;
1170 static unsigned long num_type_scc_trees;
1171 static unsigned long total_scc_size;
1172 static unsigned long num_sccs_read;
1173 static unsigned long total_scc_size_merged;
1174 static unsigned long num_sccs_merged;
1175 static unsigned long num_scc_compares;
1176 static unsigned long num_scc_compare_collisions;
1179 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1180 recursing through in-SCC tree edges. Returns true if the SCCs entered
1181 through T1 and T2 are equal and fills in *MAP with the pairs of
1182 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1184 static bool
1185 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1187 enum tree_code code;
1189 /* Mark already visited nodes. */
1190 TREE_ASM_WRITTEN (t2) = 1;
1192 /* Push the pair onto map. */
1193 (*map)[0] = t1;
1194 (*map)[1] = t2;
1195 *map = *map + 2;
1197 /* Compare value-fields. */
1198 #define compare_values(X) \
1199 do { \
1200 if (X(t1) != X(t2)) \
1201 return false; \
1202 } while (0)
1204 compare_values (TREE_CODE);
1205 code = TREE_CODE (t1);
1207 if (!TYPE_P (t1))
1209 compare_values (TREE_SIDE_EFFECTS);
1210 compare_values (TREE_CONSTANT);
1211 compare_values (TREE_READONLY);
1212 compare_values (TREE_PUBLIC);
1214 compare_values (TREE_ADDRESSABLE);
1215 compare_values (TREE_THIS_VOLATILE);
1216 if (DECL_P (t1))
1217 compare_values (DECL_UNSIGNED);
1218 else if (TYPE_P (t1))
1219 compare_values (TYPE_UNSIGNED);
1220 if (TYPE_P (t1))
1221 compare_values (TYPE_ARTIFICIAL);
1222 else
1223 compare_values (TREE_NO_WARNING);
1224 compare_values (TREE_NOTHROW);
1225 compare_values (TREE_STATIC);
1226 if (code != TREE_BINFO)
1227 compare_values (TREE_PRIVATE);
1228 compare_values (TREE_PROTECTED);
1229 compare_values (TREE_DEPRECATED);
1230 if (TYPE_P (t1))
1232 compare_values (TYPE_SATURATING);
1233 compare_values (TYPE_ADDR_SPACE);
1235 else if (code == SSA_NAME)
1236 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1238 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1240 if (!wi::eq_p (t1, t2))
1241 return false;
1244 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1246 /* ??? No suitable compare routine available. */
1247 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1248 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1249 if (r1.cl != r2.cl
1250 || r1.decimal != r2.decimal
1251 || r1.sign != r2.sign
1252 || r1.signalling != r2.signalling
1253 || r1.canonical != r2.canonical
1254 || r1.uexp != r2.uexp)
1255 return false;
1256 for (unsigned i = 0; i < SIGSZ; ++i)
1257 if (r1.sig[i] != r2.sig[i])
1258 return false;
1261 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1262 if (!fixed_compare (EQ_EXPR,
1263 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1264 return false;
1267 /* We don't want to compare locations, so there is nothing do compare
1268 for TS_DECL_MINIMAL. */
1270 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1272 compare_values (DECL_MODE);
1273 compare_values (DECL_NONLOCAL);
1274 compare_values (DECL_VIRTUAL_P);
1275 compare_values (DECL_IGNORED_P);
1276 compare_values (DECL_ABSTRACT_P);
1277 compare_values (DECL_ARTIFICIAL);
1278 compare_values (DECL_USER_ALIGN);
1279 compare_values (DECL_PRESERVE_P);
1280 compare_values (DECL_EXTERNAL);
1281 compare_values (DECL_GIMPLE_REG_P);
1282 compare_values (DECL_ALIGN);
1283 if (code == LABEL_DECL)
1285 compare_values (EH_LANDING_PAD_NR);
1286 compare_values (LABEL_DECL_UID);
1288 else if (code == FIELD_DECL)
1290 compare_values (DECL_PACKED);
1291 compare_values (DECL_NONADDRESSABLE_P);
1292 compare_values (DECL_OFFSET_ALIGN);
1294 else if (code == VAR_DECL)
1296 compare_values (DECL_HAS_DEBUG_EXPR_P);
1297 compare_values (DECL_NONLOCAL_FRAME);
1299 if (code == RESULT_DECL
1300 || code == PARM_DECL
1301 || code == VAR_DECL)
1303 compare_values (DECL_BY_REFERENCE);
1304 if (code == VAR_DECL
1305 || code == PARM_DECL)
1306 compare_values (DECL_HAS_VALUE_EXPR_P);
1310 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1311 compare_values (DECL_REGISTER);
1313 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1315 compare_values (DECL_COMMON);
1316 compare_values (DECL_DLLIMPORT_P);
1317 compare_values (DECL_WEAK);
1318 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1319 compare_values (DECL_COMDAT);
1320 compare_values (DECL_VISIBILITY);
1321 compare_values (DECL_VISIBILITY_SPECIFIED);
1322 if (code == VAR_DECL)
1324 compare_values (DECL_HARD_REGISTER);
1325 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1326 compare_values (DECL_IN_CONSTANT_POOL);
1330 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1332 compare_values (DECL_BUILT_IN_CLASS);
1333 compare_values (DECL_STATIC_CONSTRUCTOR);
1334 compare_values (DECL_STATIC_DESTRUCTOR);
1335 compare_values (DECL_UNINLINABLE);
1336 compare_values (DECL_POSSIBLY_INLINED);
1337 compare_values (DECL_IS_NOVOPS);
1338 compare_values (DECL_IS_RETURNS_TWICE);
1339 compare_values (DECL_IS_MALLOC);
1340 compare_values (DECL_IS_OPERATOR_NEW);
1341 compare_values (DECL_DECLARED_INLINE_P);
1342 compare_values (DECL_STATIC_CHAIN);
1343 compare_values (DECL_NO_INLINE_WARNING_P);
1344 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1345 compare_values (DECL_NO_LIMIT_STACK);
1346 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1347 compare_values (DECL_PURE_P);
1348 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1349 compare_values (DECL_FINAL_P);
1350 compare_values (DECL_CXX_CONSTRUCTOR_P);
1351 compare_values (DECL_CXX_DESTRUCTOR_P);
1352 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1353 compare_values (DECL_FUNCTION_CODE);
1356 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1358 compare_values (TYPE_MODE);
1359 compare_values (TYPE_STRING_FLAG);
1360 compare_values (TYPE_NO_FORCE_BLK);
1361 compare_values (TYPE_NEEDS_CONSTRUCTING);
1362 if (RECORD_OR_UNION_TYPE_P (t1))
1364 compare_values (TYPE_TRANSPARENT_AGGR);
1365 compare_values (TYPE_FINAL_P);
1367 else if (code == ARRAY_TYPE)
1368 compare_values (TYPE_NONALIASED_COMPONENT);
1369 compare_values (TYPE_PACKED);
1370 compare_values (TYPE_RESTRICT);
1371 compare_values (TYPE_USER_ALIGN);
1372 compare_values (TYPE_READONLY);
1373 compare_values (TYPE_PRECISION);
1374 compare_values (TYPE_ALIGN);
1375 compare_values (TYPE_ALIAS_SET);
1378 /* We don't want to compare locations, so there is nothing do compare
1379 for TS_EXP. */
1381 /* BLOCKs are function local and we don't merge anything there, so
1382 simply refuse to merge. */
1383 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1384 return false;
1386 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1387 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1388 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1389 return false;
1391 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1392 if (!cl_target_option_eq (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2)))
1393 return false;
1395 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1396 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1397 sizeof (struct cl_optimization)) != 0)
1398 return false;
1400 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1401 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1402 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1403 return false;
1405 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1406 compare_values (CONSTRUCTOR_NELTS);
1408 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1409 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1410 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1411 IDENTIFIER_LENGTH (t1)) != 0)
1412 return false;
1414 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1415 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1416 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1417 TREE_STRING_LENGTH (t1)) != 0)
1418 return false;
1420 if (code == OMP_CLAUSE)
1422 compare_values (OMP_CLAUSE_CODE);
1423 switch (OMP_CLAUSE_CODE (t1))
1425 case OMP_CLAUSE_DEFAULT:
1426 compare_values (OMP_CLAUSE_DEFAULT_KIND);
1427 break;
1428 case OMP_CLAUSE_SCHEDULE:
1429 compare_values (OMP_CLAUSE_SCHEDULE_KIND);
1430 break;
1431 case OMP_CLAUSE_DEPEND:
1432 compare_values (OMP_CLAUSE_DEPEND_KIND);
1433 break;
1434 case OMP_CLAUSE_MAP:
1435 compare_values (OMP_CLAUSE_MAP_KIND);
1436 break;
1437 case OMP_CLAUSE_PROC_BIND:
1438 compare_values (OMP_CLAUSE_PROC_BIND_KIND);
1439 break;
1440 case OMP_CLAUSE_REDUCTION:
1441 compare_values (OMP_CLAUSE_REDUCTION_CODE);
1442 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_INIT);
1443 compare_values (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE);
1444 break;
1445 default:
1446 break;
1450 #undef compare_values
1453 /* Compare pointer fields. */
1455 /* Recurse. Search & Replaced from DFS_write_tree_body.
1456 Folding the early checks into the compare_tree_edges recursion
1457 macro makes debugging way quicker as you are able to break on
1458 compare_tree_sccs_1 and simply finish until a call returns false
1459 to spot the SCC members with the difference. */
1460 #define compare_tree_edges(E1, E2) \
1461 do { \
1462 tree t1_ = (E1), t2_ = (E2); \
1463 if (t1_ != t2_ \
1464 && (!t1_ || !t2_ \
1465 || !TREE_VISITED (t2_) \
1466 || (!TREE_ASM_WRITTEN (t2_) \
1467 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1468 return false; \
1469 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1470 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1471 } while (0)
1473 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1475 if (code != IDENTIFIER_NODE)
1476 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1479 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1481 unsigned i;
1482 /* Note that the number of elements for EXPR has already been emitted
1483 in EXPR's header (see streamer_write_tree_header). */
1484 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1485 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1488 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1490 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1491 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1494 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1496 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1497 /* ??? Global decls from different TUs have non-matching
1498 TRANSLATION_UNIT_DECLs. Only consider a small set of
1499 decls equivalent, we should not end up merging others. */
1500 if ((code == TYPE_DECL
1501 || code == NAMESPACE_DECL
1502 || code == IMPORTED_DECL
1503 || code == CONST_DECL
1504 || (VAR_OR_FUNCTION_DECL_P (t1)
1505 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1506 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1508 else
1509 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1512 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1514 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1515 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1516 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1517 if ((code == VAR_DECL
1518 || code == PARM_DECL)
1519 && DECL_HAS_VALUE_EXPR_P (t1))
1520 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1521 if (code == VAR_DECL
1522 && DECL_HAS_DEBUG_EXPR_P (t1))
1523 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1524 /* LTO specific edges. */
1525 if (code != FUNCTION_DECL
1526 && code != TRANSLATION_UNIT_DECL)
1527 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1530 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1532 if (code == FUNCTION_DECL)
1534 tree a1, a2;
1535 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1536 a1 || a2;
1537 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1538 compare_tree_edges (a1, a2);
1539 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1541 else if (code == TYPE_DECL)
1542 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1545 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1547 /* Make sure we don't inadvertently set the assembler name. */
1548 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1549 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1550 DECL_ASSEMBLER_NAME (t2));
1553 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1555 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1556 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1557 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1558 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1559 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1560 DECL_FIELD_BIT_OFFSET (t2));
1561 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1564 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1566 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1567 DECL_FUNCTION_PERSONALITY (t2));
1568 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1569 /* DECL_FUNCTION_SPECIFIC_TARGET is not yet created. We compare
1570 the attribute list instead. */
1571 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1572 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1575 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1577 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1578 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1579 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1580 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1581 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1582 reconstructed during fixup. */
1583 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1584 during fixup. */
1585 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1586 /* ??? Global types from different TUs have non-matching
1587 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1588 equal. */
1589 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1591 else
1592 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1593 /* TYPE_CANONICAL is re-computed during type merging, so do not
1594 compare it here. */
1595 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1598 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1600 if (code == ENUMERAL_TYPE)
1601 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1602 else if (code == ARRAY_TYPE)
1603 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1604 else if (RECORD_OR_UNION_TYPE_P (t1))
1606 tree f1, f2;
1607 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1608 f1 || f2;
1609 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1610 compare_tree_edges (f1, f2);
1611 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1613 else if (code == FUNCTION_TYPE
1614 || code == METHOD_TYPE)
1615 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1616 if (!POINTER_TYPE_P (t1))
1617 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1618 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1621 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1623 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1624 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1625 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1628 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1629 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1630 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1632 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1634 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1635 compare_tree_edges (TREE_OPERAND (t1, i),
1636 TREE_OPERAND (t2, i));
1638 /* BLOCKs are function local and we don't merge anything there. */
1639 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1640 return false;
1643 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1645 unsigned i;
1646 tree t;
1647 /* Lengths have already been compared above. */
1648 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1649 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1650 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1651 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1652 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1653 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1654 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1655 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1656 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1659 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1661 unsigned i;
1662 tree index, value;
1663 /* Lengths have already been compared above. */
1664 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1666 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1667 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1671 if (code == OMP_CLAUSE)
1673 int i;
1675 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t1)]; i++)
1676 compare_tree_edges (OMP_CLAUSE_OPERAND (t1, i),
1677 OMP_CLAUSE_OPERAND (t2, i));
1678 compare_tree_edges (OMP_CLAUSE_CHAIN (t1), OMP_CLAUSE_CHAIN (t2));
1681 #undef compare_tree_edges
1683 return true;
1686 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1687 out MAP if they are equal. */
1689 static bool
1690 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1691 tree *map)
1693 /* Assume SCC entry hashes are sorted after their cardinality. Which
1694 means we can simply take the first n-tuple of equal hashes
1695 (which is recorded as entry_len) and do n SCC entry candidate
1696 comparisons. */
1697 for (unsigned i = 0; i < pscc->entry_len; ++i)
1699 tree *mapp = map;
1700 num_scc_compare_collisions++;
1701 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1703 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1704 on the scc as all trees will be freed. */
1705 return true;
1707 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1708 the SCC prevails. */
1709 for (unsigned j = 0; j < scc->len; ++j)
1710 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1713 return false;
1716 /* QSort sort function to sort a map of two pointers after the 2nd
1717 pointer. */
1719 static int
1720 cmp_tree (const void *p1_, const void *p2_)
1722 tree *p1 = (tree *)(const_cast<void *>(p1_));
1723 tree *p2 = (tree *)(const_cast<void *>(p2_));
1724 if (p1[1] == p2[1])
1725 return 0;
1726 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1729 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1730 hash value SCC_HASH with an already recorded SCC. Return true if
1731 that was successful, otherwise return false. */
1733 static bool
1734 unify_scc (struct streamer_tree_cache_d *cache, unsigned from,
1735 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1737 bool unified_p = false;
1738 tree_scc *scc
1739 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1740 scc->next = NULL;
1741 scc->hash = scc_hash;
1742 scc->len = len;
1743 scc->entry_len = scc_entry_len;
1744 for (unsigned i = 0; i < len; ++i)
1746 tree t = streamer_tree_cache_get_tree (cache, from + i);
1747 scc->entries[i] = t;
1748 /* Do not merge SCCs with local entities inside them. Also do
1749 not merge TRANSLATION_UNIT_DECLs. */
1750 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1751 || (VAR_OR_FUNCTION_DECL_P (t)
1752 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1753 || TREE_CODE (t) == LABEL_DECL)
1755 /* Avoid doing any work for these cases and do not worry to
1756 record the SCCs for further merging. */
1757 return false;
1761 /* Look for the list of candidate SCCs to compare against. */
1762 tree_scc **slot;
1763 slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
1764 if (*slot)
1766 /* Try unifying against each candidate. */
1767 num_scc_compares++;
1769 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1770 outside of the scc when following tree edges. Make sure
1771 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1772 to track whether we visited the SCC member during the compare.
1773 We cannot use TREE_VISITED on the pscc members as the extended
1774 scc and pscc can overlap. */
1775 for (unsigned i = 0; i < scc->len; ++i)
1777 TREE_VISITED (scc->entries[i]) = 1;
1778 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1781 tree *map = XALLOCAVEC (tree, 2 * len);
1782 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1784 if (!compare_tree_sccs (pscc, scc, map))
1785 continue;
1787 /* Found an equal SCC. */
1788 unified_p = true;
1789 num_scc_compare_collisions--;
1790 num_sccs_merged++;
1791 total_scc_size_merged += len;
1793 #ifdef ENABLE_CHECKING
1794 for (unsigned i = 0; i < len; ++i)
1796 tree t = map[2*i+1];
1797 enum tree_code code = TREE_CODE (t);
1798 /* IDENTIFIER_NODEs should be singletons and are merged by the
1799 streamer. The others should be singletons, too, and we
1800 should not merge them in any way. */
1801 gcc_assert (code != TRANSLATION_UNIT_DECL
1802 && code != IDENTIFIER_NODE
1803 && !streamer_handle_as_builtin_p (t));
1805 #endif
1807 /* Fixup the streamer cache with the prevailing nodes according
1808 to the tree node mapping computed by compare_tree_sccs. */
1809 if (len == 1)
1810 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1811 else
1813 tree *map2 = XALLOCAVEC (tree, 2 * len);
1814 for (unsigned i = 0; i < len; ++i)
1816 map2[i*2] = (tree)(uintptr_t)(from + i);
1817 map2[i*2+1] = scc->entries[i];
1819 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1820 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1821 for (unsigned i = 0; i < len; ++i)
1822 streamer_tree_cache_replace_tree (cache, map[2*i],
1823 (uintptr_t)map2[2*i]);
1826 /* Free the tree nodes from the read SCC. */
1827 for (unsigned i = 0; i < len; ++i)
1829 enum tree_code code;
1830 if (TYPE_P (scc->entries[i]))
1831 num_merged_types++;
1832 code = TREE_CODE (scc->entries[i]);
1833 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1834 vec_free (CONSTRUCTOR_ELTS (scc->entries[i]));
1835 ggc_free (scc->entries[i]);
1838 break;
1841 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1842 if (!unified_p)
1843 for (unsigned i = 0; i < scc->len; ++i)
1844 TREE_VISITED (scc->entries[i]) = 0;
1847 /* If we didn't unify it to any candidate duplicate the relevant
1848 pieces to permanent storage and link it into the chain. */
1849 if (!unified_p)
1851 tree_scc *pscc
1852 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1853 memcpy (pscc, scc, sizeof (tree_scc));
1854 pscc->next = (*slot);
1855 *slot = pscc;
1857 return unified_p;
1861 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1862 RESOLUTIONS is the set of symbols picked by the linker (read from the
1863 resolution file when the linker plugin is being used). */
1865 static void
1866 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1867 vec<ld_plugin_symbol_resolution_t> resolutions)
1869 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1870 const int decl_offset = sizeof (struct lto_decl_header);
1871 const int main_offset = decl_offset + header->decl_state_size;
1872 const int string_offset = main_offset + header->main_size;
1873 struct data_in *data_in;
1874 unsigned int i;
1875 const uint32_t *data_ptr, *data_end;
1876 uint32_t num_decl_states;
1878 lto_input_block ib_main ((const char *) data + main_offset,
1879 header->main_size);
1881 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1882 header->string_size, resolutions);
1884 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1885 internal types that should not be merged. */
1887 /* Read the global declarations and types. */
1888 while (ib_main.p < ib_main.len)
1890 tree t;
1891 unsigned from = data_in->reader_cache->nodes.length ();
1892 /* Read and uniquify SCCs as in the input stream. */
1893 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1894 if (tag == LTO_tree_scc)
1896 unsigned len_;
1897 unsigned scc_entry_len;
1898 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1899 &scc_entry_len);
1900 unsigned len = data_in->reader_cache->nodes.length () - from;
1901 gcc_assert (len == len_);
1903 total_scc_size += len;
1904 num_sccs_read++;
1906 /* We have the special case of size-1 SCCs that are pre-merged
1907 by means of identifier and string sharing for example.
1908 ??? Maybe we should avoid streaming those as SCCs. */
1909 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1910 from);
1911 if (len == 1
1912 && (TREE_CODE (first) == IDENTIFIER_NODE
1913 || TREE_CODE (first) == INTEGER_CST
1914 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1915 || streamer_handle_as_builtin_p (first)))
1916 continue;
1918 /* Try to unify the SCC with already existing ones. */
1919 if (!flag_ltrans
1920 && unify_scc (data_in->reader_cache, from,
1921 len, scc_entry_len, scc_hash))
1922 continue;
1924 bool seen_type = false;
1925 for (unsigned i = 0; i < len; ++i)
1927 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1928 from + i);
1929 /* Reconstruct the type variant and pointer-to/reference-to
1930 chains. */
1931 if (TYPE_P (t))
1933 seen_type = true;
1934 num_prevailing_types++;
1935 lto_fixup_prevailing_type (t);
1937 /* Compute the canonical type of all types.
1938 ??? Should be able to assert that !TYPE_CANONICAL. */
1939 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1941 gimple_register_canonical_type (t);
1942 if (odr_type_p (t))
1943 register_odr_type (t);
1945 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1946 type which is also member of this SCC. */
1947 if (TREE_CODE (t) == INTEGER_CST
1948 && !TREE_OVERFLOW (t))
1949 cache_integer_cst (t);
1950 /* Register TYPE_DECLs with the debuginfo machinery. */
1951 if (!flag_wpa
1952 && TREE_CODE (t) == TYPE_DECL)
1953 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1954 if (!flag_ltrans)
1956 /* Register variables and functions with the
1957 symbol table. */
1958 if (TREE_CODE (t) == VAR_DECL)
1959 lto_register_var_decl_in_symtab (data_in, t, from + i);
1960 else if (TREE_CODE (t) == FUNCTION_DECL
1961 && !DECL_BUILT_IN (t))
1962 lto_register_function_decl_in_symtab (data_in, t, from + i);
1963 /* Scan the tree for references to global functions or
1964 variables and record those for later fixup. */
1965 if (mentions_vars_p (t))
1966 vec_safe_push (tree_with_vars, t);
1969 if (seen_type)
1970 num_type_scc_trees += len;
1972 else
1974 /* Pickle stray references. */
1975 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1976 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1980 /* Read in lto_in_decl_state objects. */
1981 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1982 data_end =
1983 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1984 num_decl_states = *data_ptr++;
1986 gcc_assert (num_decl_states > 0);
1987 decl_data->global_decl_state = lto_new_in_decl_state ();
1988 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1989 decl_data->global_decl_state);
1991 /* Read in per-function decl states and enter them in hash table. */
1992 decl_data->function_decl_states =
1993 hash_table<decl_state_hasher>::create_ggc (37);
1995 for (i = 1; i < num_decl_states; i++)
1997 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1999 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
2000 lto_in_decl_state **slot
2001 = decl_data->function_decl_states->find_slot (state, INSERT);
2002 gcc_assert (*slot == NULL);
2003 *slot = state;
2006 if (data_ptr != data_end)
2007 internal_error ("bytecode stream: garbage at the end of symbols section");
2009 /* Set the current decl state to be the global state. */
2010 decl_data->current_decl_state = decl_data->global_decl_state;
2012 lto_data_in_delete (data_in);
2015 /* Custom version of strtoll, which is not portable. */
2017 static int64_t
2018 lto_parse_hex (const char *p)
2020 int64_t ret = 0;
2022 for (; *p != '\0'; ++p)
2024 char c = *p;
2025 unsigned char part;
2026 ret <<= 4;
2027 if (c >= '0' && c <= '9')
2028 part = c - '0';
2029 else if (c >= 'a' && c <= 'f')
2030 part = c - 'a' + 10;
2031 else if (c >= 'A' && c <= 'F')
2032 part = c - 'A' + 10;
2033 else
2034 internal_error ("could not parse hex number");
2035 ret |= part;
2038 return ret;
2041 /* Read resolution for file named FILE_NAME. The resolution is read from
2042 RESOLUTION. */
2044 static void
2045 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2047 /* We require that objects in the resolution file are in the same
2048 order as the lto1 command line. */
2049 unsigned int name_len;
2050 char *obj_name;
2051 unsigned int num_symbols;
2052 unsigned int i;
2053 struct lto_file_decl_data *file_data;
2054 splay_tree_node nd = NULL;
2056 if (!resolution)
2057 return;
2059 name_len = strlen (file->filename);
2060 obj_name = XNEWVEC (char, name_len + 1);
2061 fscanf (resolution, " "); /* Read white space. */
2063 fread (obj_name, sizeof (char), name_len, resolution);
2064 obj_name[name_len] = '\0';
2065 if (filename_cmp (obj_name, file->filename) != 0)
2066 internal_error ("unexpected file name %s in linker resolution file. "
2067 "Expected %s", obj_name, file->filename);
2068 if (file->offset != 0)
2070 int t;
2071 char offset_p[17];
2072 int64_t offset;
2073 t = fscanf (resolution, "@0x%16s", offset_p);
2074 if (t != 1)
2075 internal_error ("could not parse file offset");
2076 offset = lto_parse_hex (offset_p);
2077 if (offset != file->offset)
2078 internal_error ("unexpected offset");
2081 free (obj_name);
2083 fscanf (resolution, "%u", &num_symbols);
2085 for (i = 0; i < num_symbols; i++)
2087 int t;
2088 unsigned index;
2089 unsigned HOST_WIDE_INT id;
2090 char r_str[27];
2091 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2092 unsigned int j;
2093 unsigned int lto_resolution_str_len =
2094 sizeof (lto_resolution_str) / sizeof (char *);
2095 res_pair rp;
2097 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
2098 &index, &id, r_str);
2099 if (t != 3)
2100 internal_error ("invalid line in the resolution file");
2102 for (j = 0; j < lto_resolution_str_len; j++)
2104 if (strcmp (lto_resolution_str[j], r_str) == 0)
2106 r = (enum ld_plugin_symbol_resolution) j;
2107 break;
2110 if (j == lto_resolution_str_len)
2111 internal_error ("invalid resolution in the resolution file");
2113 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2115 nd = lto_splay_tree_lookup (file_ids, id);
2116 if (nd == NULL)
2117 internal_error ("resolution sub id %wx not in object file", id);
2120 file_data = (struct lto_file_decl_data *)nd->value;
2121 /* The indexes are very sparse. To save memory save them in a compact
2122 format that is only unpacked later when the subfile is processed. */
2123 rp.res = r;
2124 rp.index = index;
2125 file_data->respairs.safe_push (rp);
2126 if (file_data->max_index < index)
2127 file_data->max_index = index;
2131 /* List of file_decl_datas */
2132 struct file_data_list
2134 struct lto_file_decl_data *first, *last;
2137 /* Is the name for a id'ed LTO section? */
2139 static int
2140 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2142 const char *s;
2144 if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
2145 return 0;
2146 s = strrchr (name, '.');
2147 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2150 /* Create file_data of each sub file id */
2152 static int
2153 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2154 struct file_data_list *list)
2156 struct lto_section_slot s_slot, *new_slot;
2157 unsigned HOST_WIDE_INT id;
2158 splay_tree_node nd;
2159 void **hash_slot;
2160 char *new_name;
2161 struct lto_file_decl_data *file_data;
2163 if (!lto_section_with_id (ls->name, &id))
2164 return 1;
2166 /* Find hash table of sub module id */
2167 nd = lto_splay_tree_lookup (file_ids, id);
2168 if (nd != NULL)
2170 file_data = (struct lto_file_decl_data *)nd->value;
2172 else
2174 file_data = ggc_alloc<lto_file_decl_data> ();
2175 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2176 file_data->id = id;
2177 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
2178 lto_splay_tree_insert (file_ids, id, file_data);
2180 /* Maintain list in linker order */
2181 if (!list->first)
2182 list->first = file_data;
2183 if (list->last)
2184 list->last->next = file_data;
2185 list->last = file_data;
2188 /* Copy section into sub module hash table */
2189 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2190 s_slot.name = new_name;
2191 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2192 gcc_assert (*hash_slot == NULL);
2194 new_slot = XDUP (struct lto_section_slot, ls);
2195 new_slot->name = new_name;
2196 *hash_slot = new_slot;
2197 return 1;
2200 /* Read declarations and other initializations for a FILE_DATA. */
2202 static void
2203 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2205 const char *data;
2206 size_t len;
2207 vec<ld_plugin_symbol_resolution_t>
2208 resolutions = vNULL;
2209 int i;
2210 res_pair *rp;
2212 /* Create vector for fast access of resolution. We do this lazily
2213 to save memory. */
2214 resolutions.safe_grow_cleared (file_data->max_index + 1);
2215 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2216 resolutions[rp->index] = rp->res;
2217 file_data->respairs.release ();
2219 file_data->renaming_hash_table = lto_create_renaming_table ();
2220 file_data->file_name = file->filename;
2221 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2222 if (data == NULL)
2224 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2225 return;
2227 /* Frees resolutions */
2228 lto_read_decls (file_data, data, resolutions);
2229 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2232 /* Finalize FILE_DATA in FILE and increase COUNT. */
2234 static int
2235 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2236 int *count)
2238 lto_file_finalize (file_data, file);
2239 if (symtab->dump_file)
2240 fprintf (symtab->dump_file,
2241 "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2242 file_data->file_name, file_data->id);
2243 (*count)++;
2244 return 0;
2247 /* Generate a TREE representation for all types and external decls
2248 entities in FILE.
2250 Read all of the globals out of the file. Then read the cgraph
2251 and process the .o index into the cgraph nodes so that it can open
2252 the .o file to load the functions and ipa information. */
2254 static struct lto_file_decl_data *
2255 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2257 struct lto_file_decl_data *file_data = NULL;
2258 splay_tree file_ids;
2259 htab_t section_hash_table;
2260 struct lto_section_slot *section;
2261 struct file_data_list file_list;
2262 struct lto_section_list section_list;
2264 memset (&section_list, 0, sizeof (struct lto_section_list));
2265 section_hash_table = lto_obj_build_section_table (file, &section_list);
2267 /* Find all sub modules in the object and put their sections into new hash
2268 tables in a splay tree. */
2269 file_ids = lto_splay_tree_new ();
2270 memset (&file_list, 0, sizeof (struct file_data_list));
2271 for (section = section_list.first; section != NULL; section = section->next)
2272 create_subid_section_table (section, file_ids, &file_list);
2274 /* Add resolutions to file ids */
2275 lto_resolution_read (file_ids, resolution_file, file);
2277 /* Finalize each lto file for each submodule in the merged object */
2278 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2279 lto_create_files_from_ids (file, file_data, count);
2281 splay_tree_delete (file_ids);
2282 htab_delete (section_hash_table);
2284 return file_list.first;
2287 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2288 #define LTO_MMAP_IO 1
2289 #endif
2291 #if LTO_MMAP_IO
2292 /* Page size of machine is used for mmap and munmap calls. */
2293 static size_t page_mask;
2294 #endif
2296 /* Get the section data of length LEN from FILENAME starting at
2297 OFFSET. The data segment must be freed by the caller when the
2298 caller is finished. Returns NULL if all was not well. */
2300 static char *
2301 lto_read_section_data (struct lto_file_decl_data *file_data,
2302 intptr_t offset, size_t len)
2304 char *result;
2305 static int fd = -1;
2306 static char *fd_name;
2307 #if LTO_MMAP_IO
2308 intptr_t computed_len;
2309 intptr_t computed_offset;
2310 intptr_t diff;
2311 #endif
2313 /* Keep a single-entry file-descriptor cache. The last file we
2314 touched will get closed at exit.
2315 ??? Eventually we want to add a more sophisticated larger cache
2316 or rather fix function body streaming to not stream them in
2317 practically random order. */
2318 if (fd != -1
2319 && filename_cmp (fd_name, file_data->file_name) != 0)
2321 free (fd_name);
2322 close (fd);
2323 fd = -1;
2325 if (fd == -1)
2327 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2328 if (fd == -1)
2330 fatal_error ("Cannot open %s", file_data->file_name);
2331 return NULL;
2333 fd_name = xstrdup (file_data->file_name);
2336 #if LTO_MMAP_IO
2337 if (!page_mask)
2339 size_t page_size = sysconf (_SC_PAGE_SIZE);
2340 page_mask = ~(page_size - 1);
2343 computed_offset = offset & page_mask;
2344 diff = offset - computed_offset;
2345 computed_len = len + diff;
2347 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2348 fd, computed_offset);
2349 if (result == MAP_FAILED)
2351 fatal_error ("Cannot map %s", file_data->file_name);
2352 return NULL;
2355 return result + diff;
2356 #else
2357 result = (char *) xmalloc (len);
2358 if (lseek (fd, offset, SEEK_SET) != offset
2359 || read (fd, result, len) != (ssize_t) len)
2361 free (result);
2362 fatal_error ("Cannot read %s", file_data->file_name);
2363 result = NULL;
2365 #ifdef __MINGW32__
2366 /* Native windows doesn't supports delayed unlink on opened file. So
2367 we close file here again. This produces higher I/O load, but at least
2368 it prevents to have dangling file handles preventing unlink. */
2369 free (fd_name);
2370 fd_name = NULL;
2371 close (fd);
2372 fd = -1;
2373 #endif
2374 return result;
2375 #endif
2379 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2380 NAME will be NULL unless the section type is for a function
2381 body. */
2383 static const char *
2384 get_section_data (struct lto_file_decl_data *file_data,
2385 enum lto_section_type section_type,
2386 const char *name,
2387 size_t *len)
2389 htab_t section_hash_table = file_data->section_hash_table;
2390 struct lto_section_slot *f_slot;
2391 struct lto_section_slot s_slot;
2392 const char *section_name = lto_get_section_name (section_type, name, file_data);
2393 char *data = NULL;
2395 *len = 0;
2396 s_slot.name = section_name;
2397 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2398 if (f_slot)
2400 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2401 *len = f_slot->len;
2404 free (CONST_CAST (char *, section_name));
2405 return data;
2409 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2410 starts at OFFSET and has LEN bytes. */
2412 static void
2413 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2414 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2415 const char *name ATTRIBUTE_UNUSED,
2416 const char *offset, size_t len ATTRIBUTE_UNUSED)
2418 #if LTO_MMAP_IO
2419 intptr_t computed_len;
2420 intptr_t computed_offset;
2421 intptr_t diff;
2422 #endif
2424 #if LTO_MMAP_IO
2425 computed_offset = ((intptr_t) offset) & page_mask;
2426 diff = (intptr_t) offset - computed_offset;
2427 computed_len = len + diff;
2429 munmap ((caddr_t) computed_offset, computed_len);
2430 #else
2431 free (CONST_CAST(char *, offset));
2432 #endif
2435 static lto_file *current_lto_file;
2437 /* Helper for qsort; compare partitions and return one with smaller size.
2438 We sort from greatest to smallest so parallel build doesn't stale on the
2439 longest compilation being executed too late. */
2441 static int
2442 cmp_partitions_size (const void *a, const void *b)
2444 const struct ltrans_partition_def *pa
2445 = *(struct ltrans_partition_def *const *)a;
2446 const struct ltrans_partition_def *pb
2447 = *(struct ltrans_partition_def *const *)b;
2448 return pb->insns - pa->insns;
2451 /* Helper for qsort; compare partitions and return one with smaller order. */
2453 static int
2454 cmp_partitions_order (const void *a, const void *b)
2456 const struct ltrans_partition_def *pa
2457 = *(struct ltrans_partition_def *const *)a;
2458 const struct ltrans_partition_def *pb
2459 = *(struct ltrans_partition_def *const *)b;
2460 int ordera = -1, orderb = -1;
2462 if (lto_symtab_encoder_size (pa->encoder))
2463 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
2464 if (lto_symtab_encoder_size (pb->encoder))
2465 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
2466 return orderb - ordera;
2469 /* Actually stream out ENCODER into TEMP_FILENAME. */
2471 static void
2472 do_stream_out (char *temp_filename, lto_symtab_encoder_t encoder)
2474 lto_file *file = lto_obj_file_open (temp_filename, true);
2475 if (!file)
2476 fatal_error ("lto_obj_file_open() failed");
2477 lto_set_current_out_file (file);
2479 ipa_write_optimization_summaries (encoder);
2481 lto_set_current_out_file (NULL);
2482 lto_obj_file_close (file);
2483 free (file);
2486 /* Wait for forked process and signal errors. */
2487 #ifdef HAVE_WORKING_FORK
2488 static void
2489 wait_for_child ()
2491 int status;
2494 #ifndef WCONTINUED
2495 #define WCONTINUED 0
2496 #endif
2497 int w = waitpid (0, &status, WUNTRACED | WCONTINUED);
2498 if (w == -1)
2499 fatal_error ("waitpid failed");
2501 if (WIFEXITED (status) && WEXITSTATUS (status))
2502 fatal_error ("streaming subprocess failed");
2503 else if (WIFSIGNALED (status))
2504 fatal_error ("streaming subprocess was killed by signal");
2506 while (!WIFEXITED (status) && !WIFSIGNALED (status));
2508 #endif
2510 /* Stream out ENCODER into TEMP_FILENAME
2511 Fork if that seems to help. */
2513 static void
2514 stream_out (char *temp_filename, lto_symtab_encoder_t encoder,
2515 bool ARG_UNUSED (last))
2517 #ifdef HAVE_WORKING_FORK
2518 static int nruns;
2520 if (lto_parallelism <= 1)
2522 do_stream_out (temp_filename, encoder);
2523 return;
2526 /* Do not run more than LTO_PARALLELISM streamings
2527 FIXME: we ignore limits on jobserver. */
2528 if (lto_parallelism > 0 && nruns >= lto_parallelism)
2530 wait_for_child ();
2531 nruns --;
2533 /* If this is not the last parallel partition, execute new
2534 streaming process. */
2535 if (!last)
2537 pid_t cpid = fork ();
2539 if (!cpid)
2541 setproctitle ("lto1-wpa-streaming");
2542 do_stream_out (temp_filename, encoder);
2543 exit (0);
2545 /* Fork failed; lets do the job ourseleves. */
2546 else if (cpid == -1)
2547 do_stream_out (temp_filename, encoder);
2548 else
2549 nruns++;
2551 /* Last partition; stream it and wait for all children to die. */
2552 else
2554 int i;
2555 do_stream_out (temp_filename, encoder);
2556 for (i = 0; i < nruns; i++)
2557 wait_for_child ();
2559 asm_nodes_output = true;
2560 #else
2561 do_stream_out (temp_filename, encoder);
2562 #endif
2565 /* Write all output files in WPA mode and the file with the list of
2566 LTRANS units. */
2568 static void
2569 lto_wpa_write_files (void)
2571 unsigned i, n_sets;
2572 ltrans_partition part;
2573 FILE *ltrans_output_list_stream;
2574 char *temp_filename;
2575 vec <char *>temp_filenames = vNULL;
2576 size_t blen;
2578 /* Open the LTRANS output list. */
2579 if (!ltrans_output_list)
2580 fatal_error ("no LTRANS output list filename provided");
2582 timevar_push (TV_WHOPR_WPA);
2584 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2585 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2587 timevar_pop (TV_WHOPR_WPA);
2589 timevar_push (TV_WHOPR_WPA_IO);
2591 /* Generate a prefix for the LTRANS unit files. */
2592 blen = strlen (ltrans_output_list);
2593 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2594 strcpy (temp_filename, ltrans_output_list);
2595 if (blen > sizeof (".out")
2596 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2597 ".out") == 0)
2598 temp_filename[blen - sizeof (".out") + 1] = '\0';
2599 blen = strlen (temp_filename);
2601 n_sets = ltrans_partitions.length ();
2603 /* Sort partitions by size so small ones are compiled last.
2604 FIXME: Even when not reordering we may want to output one list for parallel make
2605 and other for final link command. */
2607 if (!flag_profile_reorder_functions || !flag_profile_use)
2608 ltrans_partitions.qsort (flag_toplevel_reorder
2609 ? cmp_partitions_size
2610 : cmp_partitions_order);
2612 for (i = 0; i < n_sets; i++)
2614 ltrans_partition part = ltrans_partitions[i];
2616 /* Write all the nodes in SET. */
2617 sprintf (temp_filename + blen, "%u.o", i);
2619 if (!quiet_flag)
2620 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2621 if (symtab->dump_file)
2623 lto_symtab_encoder_iterator lsei;
2625 fprintf (symtab->dump_file, "Writing partition %s to file %s, %i insns\n",
2626 part->name, temp_filename, part->insns);
2627 fprintf (symtab->dump_file, " Symbols in partition: ");
2628 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2629 lsei_next_in_partition (&lsei))
2631 symtab_node *node = lsei_node (lsei);
2632 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2634 fprintf (symtab->dump_file, "\n Symbols in boundary: ");
2635 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2636 lsei_next (&lsei))
2638 symtab_node *node = lsei_node (lsei);
2639 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2641 fprintf (symtab->dump_file, "%s ", node->asm_name ());
2642 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2643 if (cnode
2644 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2645 fprintf (symtab->dump_file, "(body included)");
2646 else
2648 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2649 if (vnode
2650 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2651 fprintf (symtab->dump_file, "(initializer included)");
2655 fprintf (symtab->dump_file, "\n");
2657 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2659 stream_out (temp_filename, part->encoder, i == n_sets - 1);
2661 part->encoder = NULL;
2663 temp_filenames.safe_push (xstrdup (temp_filename));
2665 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2666 if (ltrans_output_list_stream == NULL)
2667 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2668 for (i = 0; i < n_sets; i++)
2670 unsigned int len = strlen (temp_filenames[i]);
2671 if (fwrite (temp_filenames[i], 1, len, ltrans_output_list_stream) < len
2672 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2673 fatal_error ("writing to LTRANS output list %s: %m",
2674 ltrans_output_list);
2675 free (temp_filenames[i]);
2677 temp_filenames.release();
2679 lto_stats.num_output_files += n_sets;
2681 /* Close the LTRANS output list. */
2682 if (fclose (ltrans_output_list_stream))
2683 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2685 free_ltrans_partitions();
2686 free (temp_filename);
2688 timevar_pop (TV_WHOPR_WPA_IO);
2692 /* If TT is a variable or function decl replace it with its
2693 prevailing variant. */
2694 #define LTO_SET_PREVAIL(tt) \
2695 do {\
2696 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2697 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2699 tt = lto_symtab_prevailing_decl (tt); \
2700 fixed = true; \
2702 } while (0)
2704 /* Ensure that TT isn't a replacable var of function decl. */
2705 #define LTO_NO_PREVAIL(tt) \
2706 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2708 /* Given a tree T replace all fields referring to variables or functions
2709 with their prevailing variant. */
2710 static void
2711 lto_fixup_prevailing_decls (tree t)
2713 enum tree_code code = TREE_CODE (t);
2714 bool fixed = false;
2716 gcc_checking_assert (code != TREE_BINFO);
2717 LTO_NO_PREVAIL (TREE_TYPE (t));
2718 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2719 LTO_NO_PREVAIL (TREE_CHAIN (t));
2720 if (DECL_P (t))
2722 LTO_NO_PREVAIL (DECL_NAME (t));
2723 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2724 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2726 LTO_SET_PREVAIL (DECL_SIZE (t));
2727 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2728 LTO_SET_PREVAIL (DECL_INITIAL (t));
2729 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2730 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2732 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2734 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2736 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2738 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2740 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2742 LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
2743 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2744 LTO_NO_PREVAIL (DECL_VINDEX (t));
2746 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2748 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2749 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2750 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2751 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2752 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2755 else if (TYPE_P (t))
2757 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2758 LTO_SET_PREVAIL (TYPE_SIZE (t));
2759 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2760 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2761 LTO_NO_PREVAIL (TYPE_NAME (t));
2763 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2764 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2765 LTO_NO_PREVAIL (t->type_non_common.binfo);
2767 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2769 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2770 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2771 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2773 else if (EXPR_P (t))
2775 int i;
2776 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2777 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2779 else if (TREE_CODE (t) == CONSTRUCTOR)
2781 unsigned i;
2782 tree val;
2783 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
2784 LTO_SET_PREVAIL (val);
2786 else
2788 switch (code)
2790 case TREE_LIST:
2791 LTO_SET_PREVAIL (TREE_VALUE (t));
2792 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2793 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2794 break;
2795 default:
2796 gcc_unreachable ();
2799 /* If we fixed nothing, then we missed something seen by
2800 mentions_vars_p. */
2801 gcc_checking_assert (fixed);
2803 #undef LTO_SET_PREVAIL
2804 #undef LTO_NO_PREVAIL
2806 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2807 replaces var and function decls with the corresponding prevailing def. */
2809 static void
2810 lto_fixup_state (struct lto_in_decl_state *state)
2812 unsigned i, si;
2814 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2815 we still need to walk from all DECLs to find the reachable
2816 FUNCTION_DECLs and VAR_DECLs. */
2817 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2819 vec<tree, va_gc> *trees = state->streams[si];
2820 for (i = 0; i < vec_safe_length (trees); i++)
2822 tree t = (*trees)[i];
2823 if (VAR_OR_FUNCTION_DECL_P (t)
2824 && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2825 (*trees)[i] = lto_symtab_prevailing_decl (t);
2830 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2831 prevailing one. */
2833 static void
2834 lto_fixup_decls (struct lto_file_decl_data **files)
2836 unsigned int i;
2837 tree t;
2839 if (tree_with_vars)
2840 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2841 lto_fixup_prevailing_decls (t);
2843 for (i = 0; files[i]; i++)
2845 struct lto_file_decl_data *file = files[i];
2846 struct lto_in_decl_state *state = file->global_decl_state;
2847 lto_fixup_state (state);
2849 hash_table<decl_state_hasher>::iterator iter;
2850 lto_in_decl_state *elt;
2851 FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
2852 lto_in_decl_state *, iter)
2853 lto_fixup_state (elt);
2857 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2859 /* Turn file datas for sub files into a single array, so that they look
2860 like separate files for further passes. */
2862 static void
2863 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2865 struct lto_file_decl_data *n, *next;
2866 int i, k;
2868 lto_stats.num_input_files = count;
2869 all_file_decl_data
2870 = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (count + 1);
2871 /* Set the hooks so that all of the ipa passes can read in their data. */
2872 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2873 for (i = 0, k = 0; i < last_file_ix; i++)
2875 for (n = orig[i]; n != NULL; n = next)
2877 all_file_decl_data[k++] = n;
2878 next = n->next;
2879 n->next = NULL;
2882 all_file_decl_data[k] = NULL;
2883 gcc_assert (k == count);
2886 /* Input file data before flattening (i.e. splitting them to subfiles to support
2887 incremental linking. */
2888 static int real_file_count;
2889 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2891 static void print_lto_report_1 (void);
2893 /* Read all the symbols from the input files FNAMES. NFILES is the
2894 number of files requested in the command line. Instantiate a
2895 global call graph by aggregating all the sub-graphs found in each
2896 file. */
2898 static void
2899 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2901 unsigned int i, last_file_ix;
2902 FILE *resolution;
2903 int count = 0;
2904 struct lto_file_decl_data **decl_data;
2905 symtab_node *snode;
2907 symtab->initialize ();
2909 timevar_push (TV_IPA_LTO_DECL_IN);
2911 #ifdef ACCEL_COMPILER
2912 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2913 lto_stream_offload_p = true;
2914 #endif
2916 real_file_decl_data
2917 = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
2918 real_file_count = nfiles;
2920 /* Read the resolution file. */
2921 resolution = NULL;
2922 if (resolution_file_name)
2924 int t;
2925 unsigned num_objects;
2927 resolution = fopen (resolution_file_name, "r");
2928 if (resolution == NULL)
2929 fatal_error ("could not open symbol resolution file: %m");
2931 t = fscanf (resolution, "%u", &num_objects);
2932 gcc_assert (t == 1);
2934 /* True, since the plugin splits the archives. */
2935 gcc_assert (num_objects == nfiles);
2937 symtab->state = LTO_STREAMING;
2939 canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
2940 gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
2941 gimple_canonical_type_eq, NULL);
2942 gcc_obstack_init (&tree_scc_hash_obstack);
2943 tree_scc_hash = new hash_table<tree_scc_hasher> (4096);
2945 /* Register the common node types with the canonical type machinery so
2946 we properly share alias-sets across languages and TUs. Do not
2947 expose the common nodes as type merge target - those that should be
2948 are already exposed so by pre-loading the LTO streamer caches.
2949 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2950 for (i = 0; i < itk_none; ++i)
2951 lto_register_canonical_types (integer_types[i], true);
2952 for (i = 0; i < stk_type_kind_last; ++i)
2953 lto_register_canonical_types (sizetype_tab[i], true);
2954 for (i = 0; i < TI_MAX; ++i)
2955 lto_register_canonical_types (global_trees[i], true);
2956 for (i = 0; i < itk_none; ++i)
2957 lto_register_canonical_types (integer_types[i], false);
2958 for (i = 0; i < stk_type_kind_last; ++i)
2959 lto_register_canonical_types (sizetype_tab[i], false);
2960 for (i = 0; i < TI_MAX; ++i)
2961 lto_register_canonical_types (global_trees[i], false);
2963 if (!quiet_flag)
2964 fprintf (stderr, "Reading object files:");
2966 /* Read all of the object files specified on the command line. */
2967 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2969 struct lto_file_decl_data *file_data = NULL;
2970 if (!quiet_flag)
2972 fprintf (stderr, " %s", fnames[i]);
2973 fflush (stderr);
2976 current_lto_file = lto_obj_file_open (fnames[i], false);
2977 if (!current_lto_file)
2978 break;
2980 file_data = lto_file_read (current_lto_file, resolution, &count);
2981 if (!file_data)
2983 lto_obj_file_close (current_lto_file);
2984 free (current_lto_file);
2985 current_lto_file = NULL;
2986 break;
2989 decl_data[last_file_ix++] = file_data;
2991 lto_obj_file_close (current_lto_file);
2992 free (current_lto_file);
2993 current_lto_file = NULL;
2996 lto_flatten_files (decl_data, count, last_file_ix);
2997 lto_stats.num_input_files = count;
2998 ggc_free(decl_data);
2999 real_file_decl_data = NULL;
3001 if (resolution_file_name)
3002 fclose (resolution);
3004 /* Show the LTO report before launching LTRANS. */
3005 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3006 print_lto_report_1 ();
3008 /* Free gimple type merging datastructures. */
3009 delete tree_scc_hash;
3010 tree_scc_hash = NULL;
3011 obstack_free (&tree_scc_hash_obstack, NULL);
3012 htab_delete (gimple_canonical_types);
3013 gimple_canonical_types = NULL;
3014 delete canonical_type_hash_cache;
3015 canonical_type_hash_cache = NULL;
3017 /* At this stage we know that majority of GGC memory is reachable.
3018 Growing the limits prevents unnecesary invocation of GGC. */
3019 ggc_grow ();
3020 ggc_collect ();
3022 /* Set the hooks so that all of the ipa passes can read in their data. */
3023 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
3025 timevar_pop (TV_IPA_LTO_DECL_IN);
3027 if (!quiet_flag)
3028 fprintf (stderr, "\nReading the callgraph\n");
3030 timevar_push (TV_IPA_LTO_CGRAPH_IO);
3031 /* Read the symtab. */
3032 input_symtab ();
3034 input_offload_tables ();
3036 /* Store resolutions into the symbol table. */
3038 ld_plugin_symbol_resolution_t *res;
3039 FOR_EACH_SYMBOL (snode)
3040 if (snode->real_symbol_p ()
3041 && snode->lto_file_data
3042 && snode->lto_file_data->resolution_map
3043 && (res = snode->lto_file_data->resolution_map->get (snode->decl)))
3044 snode->resolution = *res;
3045 for (i = 0; all_file_decl_data[i]; i++)
3046 if (all_file_decl_data[i]->resolution_map)
3048 delete all_file_decl_data[i]->resolution_map;
3049 all_file_decl_data[i]->resolution_map = NULL;
3052 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
3054 if (!quiet_flag)
3055 fprintf (stderr, "Merging declarations\n");
3057 timevar_push (TV_IPA_LTO_DECL_MERGE);
3058 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
3059 need to care about resolving symbols again, we only need to replace
3060 duplicated declarations read from the callgraph and from function
3061 sections. */
3062 if (!flag_ltrans)
3064 lto_symtab_merge_decls ();
3066 /* If there were errors during symbol merging bail out, we have no
3067 good way to recover here. */
3068 if (seen_error ())
3069 fatal_error ("errors during merging of translation units");
3071 /* Fixup all decls. */
3072 lto_fixup_decls (all_file_decl_data);
3074 if (tree_with_vars)
3075 ggc_free (tree_with_vars);
3076 tree_with_vars = NULL;
3077 ggc_collect ();
3079 timevar_pop (TV_IPA_LTO_DECL_MERGE);
3080 /* Each pass will set the appropriate timer. */
3082 if (!quiet_flag)
3083 fprintf (stderr, "Reading summaries\n");
3085 /* Read the IPA summary data. */
3086 if (flag_ltrans)
3087 ipa_read_optimization_summaries ();
3088 else
3089 ipa_read_summaries ();
3091 for (i = 0; all_file_decl_data[i]; i++)
3093 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
3094 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
3095 all_file_decl_data[i]->symtab_node_encoder = NULL;
3096 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
3097 all_file_decl_data[i]->global_decl_state = NULL;
3098 all_file_decl_data[i]->current_decl_state = NULL;
3101 /* Finally merge the cgraph according to the decl merging decisions. */
3102 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
3103 if (symtab->dump_file)
3105 fprintf (symtab->dump_file, "Before merging:\n");
3106 symtab_node::dump_table (symtab->dump_file);
3108 lto_symtab_merge_symbols ();
3109 /* Removal of unreachable symbols is needed to make verify_symtab to pass;
3110 we are still having duplicated comdat groups containing local statics.
3111 We could also just remove them while merging. */
3112 symtab->remove_unreachable_nodes (dump_file);
3113 ggc_collect ();
3114 symtab->state = IPA_SSA;
3116 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
3118 /* Indicate that the cgraph is built and ready. */
3119 symtab->function_flags_ready = true;
3121 ggc_free (all_file_decl_data);
3122 all_file_decl_data = NULL;
3126 /* Materialize all the bodies for all the nodes in the callgraph. */
3128 static void
3129 materialize_cgraph (void)
3131 struct cgraph_node *node;
3132 timevar_id_t lto_timer;
3134 if (!quiet_flag)
3135 fprintf (stderr,
3136 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
3139 FOR_EACH_FUNCTION (node)
3141 if (node->lto_file_data)
3143 lto_materialize_function (node);
3144 lto_stats.num_input_cgraph_nodes++;
3149 /* Start the appropriate timer depending on the mode that we are
3150 operating in. */
3151 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
3152 : (flag_ltrans) ? TV_WHOPR_LTRANS
3153 : TV_LTO;
3154 timevar_push (lto_timer);
3156 current_function_decl = NULL;
3157 set_cfun (NULL);
3159 if (!quiet_flag)
3160 fprintf (stderr, "\n");
3162 timevar_pop (lto_timer);
3166 /* Show various memory usage statistics related to LTO. */
3167 static void
3168 print_lto_report_1 (void)
3170 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3171 fprintf (stderr, "%s statistics\n", pfx);
3173 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3174 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3175 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3176 if (flag_wpa && tree_scc_hash)
3178 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3179 "collision ratio: %f\n", pfx,
3180 (long) tree_scc_hash->size (),
3181 (long) tree_scc_hash->elements (),
3182 tree_scc_hash->collisions ());
3183 hash_table<tree_scc_hasher>::iterator hiter;
3184 tree_scc *scc, *max_scc = NULL;
3185 unsigned max_length = 0;
3186 FOR_EACH_HASH_TABLE_ELEMENT (*tree_scc_hash, scc, x, hiter)
3188 unsigned length = 0;
3189 tree_scc *s = scc;
3190 for (; s; s = s->next)
3191 length++;
3192 if (length > max_length)
3194 max_length = length;
3195 max_scc = scc;
3198 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3199 pfx, max_length, max_scc->len);
3200 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3201 num_scc_compares, num_scc_compare_collisions,
3202 num_scc_compare_collisions / (double) num_scc_compares);
3203 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3204 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3205 total_scc_size_merged);
3206 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3207 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3208 pfx, num_prevailing_types, num_type_scc_trees);
3209 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3210 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3211 (long) htab_size (gimple_canonical_types),
3212 (long) htab_elements (gimple_canonical_types),
3213 (long) gimple_canonical_types->searches,
3214 (long) gimple_canonical_types->collisions,
3215 htab_collisions (gimple_canonical_types));
3216 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3217 "%lu elements, %ld searches\n", pfx,
3218 num_canonical_type_hash_entries,
3219 num_canonical_type_hash_queries);
3222 print_lto_report (pfx);
3225 /* Perform whole program analysis (WPA) on the callgraph and write out the
3226 optimization plan. */
3228 static void
3229 do_whole_program_analysis (void)
3231 symtab_node *node;
3233 lto_parallelism = 1;
3235 /* TODO: jobserver communicatoin is not supported, yet. */
3236 if (!strcmp (flag_wpa, "jobserver"))
3237 lto_parallelism = -1;
3238 else
3240 lto_parallelism = atoi (flag_wpa);
3241 if (lto_parallelism <= 0)
3242 lto_parallelism = 0;
3245 timevar_start (TV_PHASE_OPT_GEN);
3247 /* Note that since we are in WPA mode, materialize_cgraph will not
3248 actually read in all the function bodies. It only materializes
3249 the decls and cgraph nodes so that analysis can be performed. */
3250 materialize_cgraph ();
3252 /* Reading in the cgraph uses different timers, start timing WPA now. */
3253 timevar_push (TV_WHOPR_WPA);
3255 if (pre_ipa_mem_report)
3257 fprintf (stderr, "Memory consumption before IPA\n");
3258 dump_memory_report (false);
3261 symtab->function_flags_ready = true;
3263 if (symtab->dump_file)
3264 symtab_node::dump_table (symtab->dump_file);
3265 bitmap_obstack_initialize (NULL);
3266 symtab->state = IPA_SSA;
3268 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3270 if (symtab->dump_file)
3272 fprintf (symtab->dump_file, "Optimized ");
3273 symtab_node::dump_table (symtab->dump_file);
3275 #ifdef ENABLE_CHECKING
3276 symtab_node::verify_symtab_nodes ();
3277 #endif
3278 bitmap_obstack_release (NULL);
3280 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3281 timevar_pop (TV_WHOPR_WPA);
3283 timevar_push (TV_WHOPR_PARTITIONING);
3284 if (flag_lto_partition == LTO_PARTITION_1TO1)
3285 lto_1_to_1_map ();
3286 else if (flag_lto_partition == LTO_PARTITION_MAX)
3287 lto_max_map ();
3288 else if (flag_lto_partition == LTO_PARTITION_ONE)
3289 lto_balanced_map (1);
3290 else if (flag_lto_partition == LTO_PARTITION_BALANCED)
3291 lto_balanced_map (PARAM_VALUE (PARAM_LTO_PARTITIONS));
3292 else
3293 gcc_unreachable ();
3295 /* Inline summaries are needed for balanced partitioning. Free them now so
3296 the memory can be used for streamer caches. */
3297 inline_free_summary ();
3299 /* AUX pointers are used by partitioning code to bookkeep number of
3300 partitions symbol is in. This is no longer needed. */
3301 FOR_EACH_SYMBOL (node)
3302 node->aux = NULL;
3304 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3306 /* Find out statics that need to be promoted
3307 to globals with hidden visibility because they are accessed from multiple
3308 partitions. */
3309 lto_promote_cross_file_statics ();
3310 timevar_pop (TV_WHOPR_PARTITIONING);
3312 timevar_stop (TV_PHASE_OPT_GEN);
3314 /* Collect a last time - in lto_wpa_write_files we may end up forking
3315 with the idea that this doesn't increase memory usage. So we
3316 absoultely do not want to collect after that. */
3317 ggc_collect ();
3319 timevar_start (TV_PHASE_STREAM_OUT);
3320 if (!quiet_flag)
3322 fprintf (stderr, "\nStreaming out");
3323 fflush (stderr);
3325 lto_wpa_write_files ();
3326 if (!quiet_flag)
3327 fprintf (stderr, "\n");
3328 timevar_stop (TV_PHASE_STREAM_OUT);
3330 if (post_ipa_mem_report)
3332 fprintf (stderr, "Memory consumption after IPA\n");
3333 dump_memory_report (false);
3336 /* Show the LTO report before launching LTRANS. */
3337 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3338 print_lto_report_1 ();
3339 if (mem_report_wpa)
3340 dump_memory_report (true);
3344 static GTY(()) tree lto_eh_personality_decl;
3346 /* Return the LTO personality function decl. */
3348 tree
3349 lto_eh_personality (void)
3351 if (!lto_eh_personality_decl)
3353 /* Use the first personality DECL for our personality if we don't
3354 support multiple ones. This ensures that we don't artificially
3355 create the need for them in a single-language program. */
3356 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3357 lto_eh_personality_decl = first_personality_decl;
3358 else
3359 lto_eh_personality_decl = lhd_gcc_personality ();
3362 return lto_eh_personality_decl;
3365 /* Set the process name based on the LTO mode. */
3367 static void
3368 lto_process_name (void)
3370 if (flag_lto)
3371 setproctitle ("lto1-lto");
3372 if (flag_wpa)
3373 setproctitle ("lto1-wpa");
3374 if (flag_ltrans)
3375 setproctitle ("lto1-ltrans");
3379 /* Initialize the LTO front end. */
3381 static void
3382 lto_init (void)
3384 lto_process_name ();
3385 lto_streamer_hooks_init ();
3386 lto_reader_init ();
3387 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3388 memset (&lto_stats, 0, sizeof (lto_stats));
3389 bitmap_obstack_initialize (NULL);
3390 gimple_register_cfg_hooks ();
3394 /* Main entry point for the GIMPLE front end. This front end has
3395 three main personalities:
3397 - LTO (-flto). All the object files on the command line are
3398 loaded in memory and processed as a single translation unit.
3399 This is the traditional link-time optimization behavior.
3401 - WPA (-fwpa). Only the callgraph and summary information for
3402 files in the command file are loaded. A single callgraph
3403 (without function bodies) is instantiated for the whole set of
3404 files. IPA passes are only allowed to analyze the call graph
3405 and make transformation decisions. The callgraph is
3406 partitioned, each partition is written to a new object file
3407 together with the transformation decisions.
3409 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3410 summary files from running again. Since WPA computed summary
3411 information and decided what transformations to apply, LTRANS
3412 simply applies them. */
3414 void
3415 lto_main (void)
3417 /* LTO is called as a front end, even though it is not a front end.
3418 Because it is called as a front end, TV_PHASE_PARSING and
3419 TV_PARSE_GLOBAL are active, and we need to turn them off while
3420 doing LTO. Later we turn them back on so they are active up in
3421 toplev.c. */
3422 timevar_pop (TV_PARSE_GLOBAL);
3423 timevar_stop (TV_PHASE_PARSING);
3425 timevar_start (TV_PHASE_SETUP);
3427 /* Initialize the LTO front end. */
3428 lto_init ();
3430 timevar_stop (TV_PHASE_SETUP);
3431 timevar_start (TV_PHASE_STREAM_IN);
3433 /* Read all the symbols and call graph from all the files in the
3434 command line. */
3435 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3437 timevar_stop (TV_PHASE_STREAM_IN);
3439 if (!seen_error ())
3441 /* If WPA is enabled analyze the whole call graph and create an
3442 optimization plan. Otherwise, read in all the function
3443 bodies and continue with optimization. */
3444 if (flag_wpa)
3445 do_whole_program_analysis ();
3446 else
3448 timevar_start (TV_PHASE_OPT_GEN);
3450 materialize_cgraph ();
3451 if (!flag_ltrans)
3452 lto_promote_statics_nonwpa ();
3454 /* Let the middle end know that we have read and merged all of
3455 the input files. */
3456 symtab->compile ();
3458 timevar_stop (TV_PHASE_OPT_GEN);
3460 /* FIXME lto, if the processes spawned by WPA fail, we miss
3461 the chance to print WPA's report, so WPA will call
3462 print_lto_report before launching LTRANS. If LTRANS was
3463 launched directly by the driver we would not need to do
3464 this. */
3465 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3466 print_lto_report_1 ();
3470 /* Here we make LTO pretend to be a parser. */
3471 timevar_start (TV_PHASE_PARSING);
3472 timevar_push (TV_PARSE_GLOBAL);
3475 #include "gt-lto-lto.h"