2013-10-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lto / lto.c
blobd9c36dcc5d68628121bb552a1cce36541759c969
1 /* Top-level LTO routines.
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "toplev.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "diagnostic-core.h"
29 #include "tm.h"
30 #include "cgraph.h"
31 #include "ggc.h"
32 #include "tree-ssa-operands.h"
33 #include "tree-pass.h"
34 #include "langhooks.h"
35 #include "vec.h"
36 #include "bitmap.h"
37 #include "pointer-set.h"
38 #include "ipa-prop.h"
39 #include "common.h"
40 #include "debug.h"
41 #include "gimple.h"
42 #include "lto.h"
43 #include "lto-tree.h"
44 #include "lto-streamer.h"
45 #include "tree-streamer.h"
46 #include "splay-tree.h"
47 #include "lto-partition.h"
48 #include "data-streamer.h"
49 #include "context.h"
50 #include "pass_manager.h"
52 /* Vector to keep track of external variables we've seen so far. */
53 vec<tree, va_gc> *lto_global_var_decls;
55 static GTY(()) tree first_personality_decl;
57 /* Returns a hash code for P. */
59 static hashval_t
60 hash_name (const void *p)
62 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
63 return (hashval_t) htab_hash_string (ds->name);
67 /* Returns nonzero if P1 and P2 are equal. */
69 static int
70 eq_name (const void *p1, const void *p2)
72 const struct lto_section_slot *s1 =
73 (const struct lto_section_slot *) p1;
74 const struct lto_section_slot *s2 =
75 (const struct lto_section_slot *) p2;
77 return strcmp (s1->name, s2->name) == 0;
80 /* Free lto_section_slot */
82 static void
83 free_with_string (void *arg)
85 struct lto_section_slot *s = (struct lto_section_slot *)arg;
87 free (CONST_CAST (char *, s->name));
88 free (arg);
91 /* Create section hash table */
93 htab_t
94 lto_obj_create_section_hash_table (void)
96 return htab_create (37, hash_name, eq_name, free_with_string);
99 /* Delete an allocated integer KEY in the splay tree. */
101 static void
102 lto_splay_tree_delete_id (splay_tree_key key)
104 free ((void *) key);
107 /* Compare splay tree node ids A and B. */
109 static int
110 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
112 unsigned HOST_WIDE_INT ai;
113 unsigned HOST_WIDE_INT bi;
115 ai = *(unsigned HOST_WIDE_INT *) a;
116 bi = *(unsigned HOST_WIDE_INT *) b;
118 if (ai < bi)
119 return -1;
120 else if (ai > bi)
121 return 1;
122 return 0;
125 /* Look up splay tree node by ID in splay tree T. */
127 static splay_tree_node
128 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
130 return splay_tree_lookup (t, (splay_tree_key) &id);
133 /* Check if KEY has ID. */
135 static bool
136 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
138 return *(unsigned HOST_WIDE_INT *) key == id;
141 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
142 The ID is allocated separately because we need HOST_WIDE_INTs which may
143 be wider than a splay_tree_key. */
145 static void
146 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
147 struct lto_file_decl_data *file_data)
149 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
150 *idp = id;
151 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
154 /* Create a splay tree. */
156 static splay_tree
157 lto_splay_tree_new (void)
159 return splay_tree_new (lto_splay_tree_compare_ids,
160 lto_splay_tree_delete_id,
161 NULL);
164 /* Return true when NODE has a clone that is analyzed (i.e. we need
165 to load its body even if the node itself is not needed). */
167 static bool
168 has_analyzed_clone_p (struct cgraph_node *node)
170 struct cgraph_node *orig = node;
171 node = node->clones;
172 if (node)
173 while (node != orig)
175 if (node->symbol.analyzed)
176 return true;
177 if (node->clones)
178 node = node->clones;
179 else if (node->next_sibling_clone)
180 node = node->next_sibling_clone;
181 else
183 while (node != orig && !node->next_sibling_clone)
184 node = node->clone_of;
185 if (node != orig)
186 node = node->next_sibling_clone;
189 return false;
192 /* Read the function body for the function associated with NODE. */
194 static void
195 lto_materialize_function (struct cgraph_node *node)
197 tree decl;
199 decl = node->symbol.decl;
200 /* Read in functions with body (analyzed nodes)
201 and also functions that are needed to produce virtual clones. */
202 if ((cgraph_function_with_gimple_body_p (node) && node->symbol.analyzed)
203 || node->used_as_abstract_origin
204 || has_analyzed_clone_p (node))
206 /* Clones don't need to be read. */
207 if (node->clone_of)
208 return;
209 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
210 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
213 /* Let the middle end know about the function. */
214 rest_of_decl_compilation (decl, 1, 0);
218 /* Decode the content of memory pointed to by DATA in the in decl
219 state object STATE. DATA_IN points to a data_in structure for
220 decoding. Return the address after the decoded object in the
221 input. */
223 static const uint32_t *
224 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
225 struct lto_in_decl_state *state)
227 uint32_t ix;
228 tree decl;
229 uint32_t i, j;
231 ix = *data++;
232 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
233 if (TREE_CODE (decl) != FUNCTION_DECL)
235 gcc_assert (decl == void_type_node);
236 decl = NULL_TREE;
238 state->fn_decl = decl;
240 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
242 uint32_t size = *data++;
243 tree *decls = ggc_alloc_vec_tree (size);
245 for (j = 0; j < size; j++)
246 decls[j] = streamer_tree_cache_get_tree (data_in->reader_cache, data[j]);
248 state->streams[i].size = size;
249 state->streams[i].trees = decls;
250 data += size;
253 return data;
257 /* Global canonical type table. */
258 static htab_t gimple_canonical_types;
259 static pointer_map <hashval_t> *canonical_type_hash_cache;
260 static unsigned long num_canonical_type_hash_entries;
261 static unsigned long num_canonical_type_hash_queries;
263 static hashval_t iterative_hash_canonical_type (tree type, hashval_t val);
264 static hashval_t gimple_canonical_type_hash (const void *p);
265 static void gimple_register_canonical_type_1 (tree t, hashval_t hash);
267 /* Returning a hash value for gimple type TYPE.
269 The hash value returned is equal for types considered compatible
270 by gimple_canonical_types_compatible_p. */
272 static hashval_t
273 hash_canonical_type (tree type)
275 hashval_t v;
277 /* Combine a few common features of types so that types are grouped into
278 smaller sets; when searching for existing matching types to merge,
279 only existing types having the same features as the new type will be
280 checked. */
281 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
282 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
283 v = iterative_hash_hashval_t (TYPE_ALIGN (type), v);
284 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
286 /* Incorporate common features of numerical types. */
287 if (INTEGRAL_TYPE_P (type)
288 || SCALAR_FLOAT_TYPE_P (type)
289 || FIXED_POINT_TYPE_P (type)
290 || TREE_CODE (type) == OFFSET_TYPE
291 || POINTER_TYPE_P (type))
293 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
294 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
297 if (VECTOR_TYPE_P (type))
299 v = iterative_hash_hashval_t (TYPE_VECTOR_SUBPARTS (type), v);
300 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
303 if (TREE_CODE (type) == COMPLEX_TYPE)
304 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
306 /* For pointer and reference types, fold in information about the type
307 pointed to but do not recurse to the pointed-to type. */
308 if (POINTER_TYPE_P (type))
310 v = iterative_hash_hashval_t (TYPE_REF_CAN_ALIAS_ALL (type), v);
311 v = iterative_hash_hashval_t (TYPE_ADDR_SPACE (TREE_TYPE (type)), v);
312 v = iterative_hash_hashval_t (TYPE_RESTRICT (type), v);
313 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
316 /* For integer types hash only the string flag. */
317 if (TREE_CODE (type) == INTEGER_TYPE)
318 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
320 /* For array types hash the domain bounds and the string flag. */
321 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
323 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
324 /* OMP lowering can introduce error_mark_node in place of
325 random local decls in types. */
326 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
327 v = iterative_hash_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), v);
328 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
329 v = iterative_hash_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), v);
332 /* Recurse for aggregates with a single element type. */
333 if (TREE_CODE (type) == ARRAY_TYPE
334 || TREE_CODE (type) == COMPLEX_TYPE
335 || TREE_CODE (type) == VECTOR_TYPE)
336 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
338 /* Incorporate function return and argument types. */
339 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
341 unsigned na;
342 tree p;
344 /* For method types also incorporate their parent class. */
345 if (TREE_CODE (type) == METHOD_TYPE)
346 v = iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type), v);
348 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
350 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
352 v = iterative_hash_canonical_type (TREE_VALUE (p), v);
353 na++;
356 v = iterative_hash_hashval_t (na, v);
359 if (RECORD_OR_UNION_TYPE_P (type))
361 unsigned nf;
362 tree f;
364 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
365 if (TREE_CODE (f) == FIELD_DECL)
367 v = iterative_hash_canonical_type (TREE_TYPE (f), v);
368 nf++;
371 v = iterative_hash_hashval_t (nf, v);
374 return v;
377 /* Returning a hash value for gimple type TYPE combined with VAL. */
379 static hashval_t
380 iterative_hash_canonical_type (tree type, hashval_t val)
382 hashval_t v;
383 /* An already processed type. */
384 if (TYPE_CANONICAL (type))
386 type = TYPE_CANONICAL (type);
387 v = gimple_canonical_type_hash (type);
389 else
391 /* Canonical types should not be able to form SCCs by design, this
392 recursion is just because we do not register canonical types in
393 optimal order. To avoid quadratic behavior also register the
394 type here. */
395 v = hash_canonical_type (type);
396 gimple_register_canonical_type_1 (type, v);
398 return iterative_hash_hashval_t (v, val);
401 /* Returns the hash for a canonical type P. */
403 static hashval_t
404 gimple_canonical_type_hash (const void *p)
406 num_canonical_type_hash_queries++;
407 hashval_t *slot
408 = canonical_type_hash_cache->contains (CONST_CAST_TREE ((const_tree) p));
409 gcc_assert (slot != NULL);
410 return *slot;
414 /* The TYPE_CANONICAL merging machinery. It should closely resemble
415 the middle-end types_compatible_p function. It needs to avoid
416 claiming types are different for types that should be treated
417 the same with respect to TBAA. Canonical types are also used
418 for IL consistency checks via the useless_type_conversion_p
419 predicate which does not handle all type kinds itself but falls
420 back to pointer-comparison of TYPE_CANONICAL for aggregates
421 for example. */
423 /* Return true iff T1 and T2 are structurally identical for what
424 TBAA is concerned. */
426 static bool
427 gimple_canonical_types_compatible_p (tree t1, tree t2)
429 /* Before starting to set up the SCC machinery handle simple cases. */
431 /* Check first for the obvious case of pointer identity. */
432 if (t1 == t2)
433 return true;
435 /* Check that we have two types to compare. */
436 if (t1 == NULL_TREE || t2 == NULL_TREE)
437 return false;
439 /* If the types have been previously registered and found equal
440 they still are. */
441 if (TYPE_CANONICAL (t1)
442 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
443 return true;
445 /* Can't be the same type if the types don't have the same code. */
446 if (TREE_CODE (t1) != TREE_CODE (t2))
447 return false;
449 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
450 return false;
452 /* Qualifiers do not matter for canonical type comparison purposes. */
454 /* Void types and nullptr types are always the same. */
455 if (TREE_CODE (t1) == VOID_TYPE
456 || TREE_CODE (t1) == NULLPTR_TYPE)
457 return true;
459 /* Can't be the same type if they have different alignment, or mode. */
460 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
461 || TYPE_MODE (t1) != TYPE_MODE (t2))
462 return false;
464 /* Non-aggregate types can be handled cheaply. */
465 if (INTEGRAL_TYPE_P (t1)
466 || SCALAR_FLOAT_TYPE_P (t1)
467 || FIXED_POINT_TYPE_P (t1)
468 || TREE_CODE (t1) == VECTOR_TYPE
469 || TREE_CODE (t1) == COMPLEX_TYPE
470 || TREE_CODE (t1) == OFFSET_TYPE
471 || POINTER_TYPE_P (t1))
473 /* Can't be the same type if they have different sign or precision. */
474 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
475 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
476 return false;
478 if (TREE_CODE (t1) == INTEGER_TYPE
479 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
480 return false;
482 /* For canonical type comparisons we do not want to build SCCs
483 so we cannot compare pointed-to types. But we can, for now,
484 require the same pointed-to type kind and match what
485 useless_type_conversion_p would do. */
486 if (POINTER_TYPE_P (t1))
488 /* If the two pointers have different ref-all attributes,
489 they can't be the same type. */
490 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
491 return false;
493 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
494 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
495 return false;
497 if (TYPE_RESTRICT (t1) != TYPE_RESTRICT (t2))
498 return false;
500 if (TREE_CODE (TREE_TYPE (t1)) != TREE_CODE (TREE_TYPE (t2)))
501 return false;
504 /* Tail-recurse to components. */
505 if (TREE_CODE (t1) == VECTOR_TYPE
506 || TREE_CODE (t1) == COMPLEX_TYPE)
507 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
508 TREE_TYPE (t2));
510 return true;
513 /* Do type-specific comparisons. */
514 switch (TREE_CODE (t1))
516 case ARRAY_TYPE:
517 /* Array types are the same if the element types are the same and
518 the number of elements are the same. */
519 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
520 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
521 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
522 return false;
523 else
525 tree i1 = TYPE_DOMAIN (t1);
526 tree i2 = TYPE_DOMAIN (t2);
528 /* For an incomplete external array, the type domain can be
529 NULL_TREE. Check this condition also. */
530 if (i1 == NULL_TREE && i2 == NULL_TREE)
531 return true;
532 else if (i1 == NULL_TREE || i2 == NULL_TREE)
533 return false;
534 else
536 tree min1 = TYPE_MIN_VALUE (i1);
537 tree min2 = TYPE_MIN_VALUE (i2);
538 tree max1 = TYPE_MAX_VALUE (i1);
539 tree max2 = TYPE_MAX_VALUE (i2);
541 /* The minimum/maximum values have to be the same. */
542 if ((min1 == min2
543 || (min1 && min2
544 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
545 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
546 || operand_equal_p (min1, min2, 0))))
547 && (max1 == max2
548 || (max1 && max2
549 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
550 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
551 || operand_equal_p (max1, max2, 0)))))
552 return true;
553 else
554 return false;
558 case METHOD_TYPE:
559 case FUNCTION_TYPE:
560 /* Function types are the same if the return type and arguments types
561 are the same. */
562 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
563 return false;
565 if (!comp_type_attributes (t1, t2))
566 return false;
568 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
569 return true;
570 else
572 tree parms1, parms2;
574 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
575 parms1 && parms2;
576 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
578 if (!gimple_canonical_types_compatible_p
579 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
580 return false;
583 if (parms1 || parms2)
584 return false;
586 return true;
589 case RECORD_TYPE:
590 case UNION_TYPE:
591 case QUAL_UNION_TYPE:
593 tree f1, f2;
595 /* For aggregate types, all the fields must be the same. */
596 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
597 f1 || f2;
598 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
600 /* Skip non-fields. */
601 while (f1 && TREE_CODE (f1) != FIELD_DECL)
602 f1 = TREE_CHAIN (f1);
603 while (f2 && TREE_CODE (f2) != FIELD_DECL)
604 f2 = TREE_CHAIN (f2);
605 if (!f1 || !f2)
606 break;
607 /* The fields must have the same name, offset and type. */
608 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
609 || !gimple_compare_field_offset (f1, f2)
610 || !gimple_canonical_types_compatible_p
611 (TREE_TYPE (f1), TREE_TYPE (f2)))
612 return false;
615 /* If one aggregate has more fields than the other, they
616 are not the same. */
617 if (f1 || f2)
618 return false;
620 return true;
623 default:
624 gcc_unreachable ();
629 /* Returns nonzero if P1 and P2 are equal. */
631 static int
632 gimple_canonical_type_eq (const void *p1, const void *p2)
634 const_tree t1 = (const_tree) p1;
635 const_tree t2 = (const_tree) p2;
636 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
637 CONST_CAST_TREE (t2));
640 /* Main worker for gimple_register_canonical_type. */
642 static void
643 gimple_register_canonical_type_1 (tree t, hashval_t hash)
645 void **slot;
647 gcc_checking_assert (TYPE_P (t) && !TYPE_CANONICAL (t));
649 slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
650 if (*slot)
652 tree new_type = (tree)(*slot);
653 gcc_checking_assert (new_type != t);
654 TYPE_CANONICAL (t) = new_type;
656 else
658 TYPE_CANONICAL (t) = t;
659 *slot = (void *) t;
660 /* Cache the just computed hash value. */
661 num_canonical_type_hash_entries++;
662 bool existed_p;
663 hashval_t *hslot = canonical_type_hash_cache->insert (t, &existed_p);
664 gcc_assert (!existed_p);
665 *hslot = hash;
669 /* Register type T in the global type table gimple_types and set
670 TYPE_CANONICAL of T accordingly.
671 This is used by LTO to merge structurally equivalent types for
672 type-based aliasing purposes across different TUs and languages.
674 ??? This merging does not exactly match how the tree.c middle-end
675 functions will assign TYPE_CANONICAL when new types are created
676 during optimization (which at least happens for pointer and array
677 types). */
679 static void
680 gimple_register_canonical_type (tree t)
682 if (TYPE_CANONICAL (t))
683 return;
685 gimple_register_canonical_type_1 (t, hash_canonical_type (t));
688 /* Re-compute TYPE_CANONICAL for NODE and related types. */
690 static void
691 lto_register_canonical_types (tree node, bool first_p)
693 if (!node
694 || !TYPE_P (node))
695 return;
697 if (first_p)
698 TYPE_CANONICAL (node) = NULL_TREE;
700 if (POINTER_TYPE_P (node)
701 || TREE_CODE (node) == COMPLEX_TYPE
702 || TREE_CODE (node) == ARRAY_TYPE)
703 lto_register_canonical_types (TREE_TYPE (node), first_p);
705 if (!first_p)
706 gimple_register_canonical_type (node);
710 /* Remember trees that contains references to declarations. */
711 static GTY(()) vec <tree, va_gc> *tree_with_vars;
713 #define CHECK_VAR(tt) \
714 do \
716 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
717 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
718 return true; \
719 } while (0)
721 #define CHECK_NO_VAR(tt) \
722 gcc_checking_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
724 /* Check presence of pointers to decls in fields of a tree_typed T. */
726 static inline bool
727 mentions_vars_p_typed (tree t)
729 CHECK_NO_VAR (TREE_TYPE (t));
730 return false;
733 /* Check presence of pointers to decls in fields of a tree_common T. */
735 static inline bool
736 mentions_vars_p_common (tree t)
738 if (mentions_vars_p_typed (t))
739 return true;
740 CHECK_NO_VAR (TREE_CHAIN (t));
741 return false;
744 /* Check presence of pointers to decls in fields of a decl_minimal T. */
746 static inline bool
747 mentions_vars_p_decl_minimal (tree t)
749 if (mentions_vars_p_common (t))
750 return true;
751 CHECK_NO_VAR (DECL_NAME (t));
752 CHECK_VAR (DECL_CONTEXT (t));
753 return false;
756 /* Check presence of pointers to decls in fields of a decl_common T. */
758 static inline bool
759 mentions_vars_p_decl_common (tree t)
761 if (mentions_vars_p_decl_minimal (t))
762 return true;
763 CHECK_VAR (DECL_SIZE (t));
764 CHECK_VAR (DECL_SIZE_UNIT (t));
765 CHECK_VAR (DECL_INITIAL (t));
766 CHECK_NO_VAR (DECL_ATTRIBUTES (t));
767 CHECK_VAR (DECL_ABSTRACT_ORIGIN (t));
768 return false;
771 /* Check presence of pointers to decls in fields of a decl_with_vis T. */
773 static inline bool
774 mentions_vars_p_decl_with_vis (tree t)
776 if (mentions_vars_p_decl_common (t))
777 return true;
779 /* Accessor macro has side-effects, use field-name here. */
780 CHECK_NO_VAR (t->decl_with_vis.assembler_name);
781 CHECK_NO_VAR (DECL_SECTION_NAME (t));
782 return false;
785 /* Check presence of pointers to decls in fields of a decl_non_common T. */
787 static inline bool
788 mentions_vars_p_decl_non_common (tree t)
790 if (mentions_vars_p_decl_with_vis (t))
791 return true;
792 CHECK_NO_VAR (DECL_ARGUMENT_FLD (t));
793 CHECK_NO_VAR (DECL_RESULT_FLD (t));
794 CHECK_NO_VAR (DECL_VINDEX (t));
795 return false;
798 /* Check presence of pointers to decls in fields of a decl_non_common T. */
800 static bool
801 mentions_vars_p_function (tree t)
803 if (mentions_vars_p_decl_non_common (t))
804 return true;
805 CHECK_VAR (DECL_FUNCTION_PERSONALITY (t));
806 return false;
809 /* Check presence of pointers to decls in fields of a field_decl T. */
811 static bool
812 mentions_vars_p_field_decl (tree t)
814 if (mentions_vars_p_decl_common (t))
815 return true;
816 CHECK_VAR (DECL_FIELD_OFFSET (t));
817 CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
818 CHECK_NO_VAR (DECL_QUALIFIER (t));
819 CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
820 CHECK_NO_VAR (DECL_FCONTEXT (t));
821 return false;
824 /* Check presence of pointers to decls in fields of a type T. */
826 static bool
827 mentions_vars_p_type (tree t)
829 if (mentions_vars_p_common (t))
830 return true;
831 CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
832 CHECK_VAR (TYPE_SIZE (t));
833 CHECK_VAR (TYPE_SIZE_UNIT (t));
834 CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
835 CHECK_NO_VAR (TYPE_NAME (t));
837 CHECK_VAR (TYPE_MINVAL (t));
838 CHECK_VAR (TYPE_MAXVAL (t));
840 /* Accessor is for derived node types only. */
841 CHECK_NO_VAR (t->type_non_common.binfo);
843 CHECK_VAR (TYPE_CONTEXT (t));
844 CHECK_NO_VAR (TYPE_CANONICAL (t));
845 CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
846 CHECK_NO_VAR (TYPE_NEXT_VARIANT (t));
847 return false;
850 /* Check presence of pointers to decls in fields of a BINFO T. */
852 static bool
853 mentions_vars_p_binfo (tree t)
855 unsigned HOST_WIDE_INT i, n;
857 if (mentions_vars_p_common (t))
858 return true;
859 CHECK_VAR (BINFO_VTABLE (t));
860 CHECK_NO_VAR (BINFO_OFFSET (t));
861 CHECK_NO_VAR (BINFO_VIRTUALS (t));
862 CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
863 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
864 for (i = 0; i < n; i++)
865 CHECK_NO_VAR (BINFO_BASE_ACCESS (t, i));
866 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
867 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
868 n = BINFO_N_BASE_BINFOS (t);
869 for (i = 0; i < n; i++)
870 CHECK_NO_VAR (BINFO_BASE_BINFO (t, i));
871 return false;
874 /* Check presence of pointers to decls in fields of a CONSTRUCTOR T. */
876 static bool
877 mentions_vars_p_constructor (tree t)
879 unsigned HOST_WIDE_INT idx;
880 constructor_elt *ce;
882 if (mentions_vars_p_typed (t))
883 return true;
885 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
887 CHECK_NO_VAR (ce->index);
888 CHECK_VAR (ce->value);
890 return false;
893 /* Check presence of pointers to decls in fields of an expression tree T. */
895 static bool
896 mentions_vars_p_expr (tree t)
898 int i;
899 if (mentions_vars_p_typed (t))
900 return true;
901 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
902 CHECK_VAR (TREE_OPERAND (t, i));
903 return false;
906 /* Check presence of pointers to decls that needs later fixup in T. */
908 static bool
909 mentions_vars_p (tree t)
911 switch (TREE_CODE (t))
913 case IDENTIFIER_NODE:
914 break;
916 case TREE_LIST:
917 CHECK_VAR (TREE_VALUE (t));
918 CHECK_VAR (TREE_PURPOSE (t));
919 CHECK_NO_VAR (TREE_CHAIN (t));
920 break;
922 case FIELD_DECL:
923 return mentions_vars_p_field_decl (t);
924 break;
926 case LABEL_DECL:
927 case CONST_DECL:
928 case PARM_DECL:
929 case RESULT_DECL:
930 case IMPORTED_DECL:
931 case NAMESPACE_DECL:
932 return mentions_vars_p_decl_common (t);
933 break;
935 case VAR_DECL:
936 return mentions_vars_p_decl_with_vis (t);
937 break;
939 case TYPE_DECL:
940 return mentions_vars_p_decl_non_common (t);
941 break;
943 case FUNCTION_DECL:
944 return mentions_vars_p_function (t);
945 break;
947 case TREE_BINFO:
948 return mentions_vars_p_binfo (t);
949 break;
951 case PLACEHOLDER_EXPR:
952 return mentions_vars_p_common (t);
953 break;
955 case BLOCK:
956 case TRANSLATION_UNIT_DECL:
957 case OPTIMIZATION_NODE:
958 case TARGET_OPTION_NODE:
959 break;
961 case CONSTRUCTOR:
962 return mentions_vars_p_constructor (t);
963 break;
965 default:
966 if (TYPE_P (t))
968 if (mentions_vars_p_type (t))
969 return true;
971 else if (EXPR_P (t))
973 if (mentions_vars_p_expr (t))
974 return true;
976 else if (CONSTANT_CLASS_P (t))
977 CHECK_NO_VAR (TREE_TYPE (t));
978 else
979 gcc_unreachable ();
981 return false;
985 /* Return the resolution for the decl with index INDEX from DATA_IN. */
987 static enum ld_plugin_symbol_resolution
988 get_resolution (struct data_in *data_in, unsigned index)
990 if (data_in->globals_resolution.exists ())
992 ld_plugin_symbol_resolution_t ret;
993 /* We can have references to not emitted functions in
994 DECL_FUNCTION_PERSONALITY at least. So we can and have
995 to indeed return LDPR_UNKNOWN in some cases. */
996 if (data_in->globals_resolution.length () <= index)
997 return LDPR_UNKNOWN;
998 ret = data_in->globals_resolution[index];
999 return ret;
1001 else
1002 /* Delay resolution finding until decl merging. */
1003 return LDPR_UNKNOWN;
1006 /* We need to record resolutions until symbol table is read. */
1007 static void
1008 register_resolution (struct lto_file_decl_data *file_data, tree decl,
1009 enum ld_plugin_symbol_resolution resolution)
1011 if (resolution == LDPR_UNKNOWN)
1012 return;
1013 if (!file_data->resolution_map)
1014 file_data->resolution_map = pointer_map_create ();
1015 *pointer_map_insert (file_data->resolution_map, decl) = (void *)(size_t)resolution;
1018 /* Register DECL with the global symbol table and change its
1019 name if necessary to avoid name clashes for static globals across
1020 different files. */
1022 static void
1023 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
1024 unsigned ix)
1026 tree context;
1028 /* Variable has file scope, not local. */
1029 if (!TREE_PUBLIC (decl)
1030 && !((context = decl_function_context (decl))
1031 && auto_var_in_fn_p (decl, context)))
1032 rest_of_decl_compilation (decl, 1, 0);
1034 /* If this variable has already been declared, queue the
1035 declaration for merging. */
1036 if (TREE_PUBLIC (decl))
1037 register_resolution (data_in->file_data,
1038 decl, get_resolution (data_in, ix));
1042 /* Register DECL with the global symbol table and change its
1043 name if necessary to avoid name clashes for static globals across
1044 different files. DATA_IN contains descriptors and tables for the
1045 file being read. */
1047 static void
1048 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
1049 unsigned ix)
1051 /* If this variable has already been declared, queue the
1052 declaration for merging. */
1053 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
1054 register_resolution (data_in->file_data,
1055 decl, get_resolution (data_in, ix));
1059 /* For the type T re-materialize it in the type variant list and
1060 the pointer/reference-to chains. */
1062 static void
1063 lto_fixup_prevailing_type (tree t)
1065 /* The following re-creates proper variant lists while fixing up
1066 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1067 variant list state before fixup is broken. */
1069 /* If we are not our own variant leader link us into our new leaders
1070 variant list. */
1071 if (TYPE_MAIN_VARIANT (t) != t)
1073 tree mv = TYPE_MAIN_VARIANT (t);
1074 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1075 TYPE_NEXT_VARIANT (mv) = t;
1078 /* The following reconstructs the pointer chains
1079 of the new pointed-to type if we are a main variant. We do
1080 not stream those so they are broken before fixup. */
1081 if (TREE_CODE (t) == POINTER_TYPE
1082 && TYPE_MAIN_VARIANT (t) == t)
1084 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1085 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1087 else if (TREE_CODE (t) == REFERENCE_TYPE
1088 && TYPE_MAIN_VARIANT (t) == t)
1090 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1091 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1096 /* We keep prevailing tree SCCs in a hashtable with manual collision
1097 handling (in case all hashes compare the same) and keep the colliding
1098 entries in the tree_scc->next chain. */
1100 struct tree_scc
1102 tree_scc *next;
1103 /* Hash of the whole SCC. */
1104 hashval_t hash;
1105 /* Number of trees in the SCC. */
1106 unsigned len;
1107 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1108 which share the same individual tree hash). */
1109 unsigned entry_len;
1110 /* The members of the SCC.
1111 We only need to remember the first entry node candidate for prevailing
1112 SCCs (but of course have access to all entries for SCCs we are
1113 processing).
1114 ??? For prevailing SCCs we really only need hash and the first
1115 entry candidate, but that's too awkward to implement. */
1116 tree entries[1];
1119 struct tree_scc_hasher : typed_noop_remove <tree_scc>
1121 typedef tree_scc value_type;
1122 typedef tree_scc compare_type;
1123 static inline hashval_t hash (const value_type *);
1124 static inline bool equal (const value_type *, const compare_type *);
1127 hashval_t
1128 tree_scc_hasher::hash (const value_type *scc)
1130 return scc->hash;
1133 bool
1134 tree_scc_hasher::equal (const value_type *scc1, const compare_type *scc2)
1136 if (scc1->hash != scc2->hash
1137 || scc1->len != scc2->len
1138 || scc1->entry_len != scc2->entry_len)
1139 return false;
1140 return true;
1143 static hash_table <tree_scc_hasher> tree_scc_hash;
1144 static struct obstack tree_scc_hash_obstack;
1146 static unsigned long num_merged_types;
1147 static unsigned long num_prevailing_types;
1148 static unsigned long num_type_scc_trees;
1149 static unsigned long total_scc_size;
1150 static unsigned long num_sccs_read;
1151 static unsigned long total_scc_size_merged;
1152 static unsigned long num_sccs_merged;
1153 static unsigned long num_scc_compares;
1154 static unsigned long num_scc_compare_collisions;
1157 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1158 recursing through in-SCC tree edges. Returns true if the SCCs entered
1159 through T1 and T2 are equal and fills in *MAP with the pairs of
1160 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1162 static bool
1163 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1165 enum tree_code code;
1167 /* Mark already visited nodes. */
1168 TREE_ASM_WRITTEN (t2) = 1;
1170 /* Push the pair onto map. */
1171 (*map)[0] = t1;
1172 (*map)[1] = t2;
1173 *map = *map + 2;
1175 /* Compare value-fields. */
1176 #define compare_values(X) \
1177 do { \
1178 if (X(t1) != X(t2)) \
1179 return false; \
1180 } while (0)
1182 compare_values (TREE_CODE);
1183 code = TREE_CODE (t1);
1185 if (!TYPE_P (t1))
1187 compare_values (TREE_SIDE_EFFECTS);
1188 compare_values (TREE_CONSTANT);
1189 compare_values (TREE_READONLY);
1190 compare_values (TREE_PUBLIC);
1192 compare_values (TREE_ADDRESSABLE);
1193 compare_values (TREE_THIS_VOLATILE);
1194 if (DECL_P (t1))
1195 compare_values (DECL_UNSIGNED);
1196 else if (TYPE_P (t1))
1197 compare_values (TYPE_UNSIGNED);
1198 if (TYPE_P (t1))
1199 compare_values (TYPE_ARTIFICIAL);
1200 else
1201 compare_values (TREE_NO_WARNING);
1202 compare_values (TREE_NOTHROW);
1203 compare_values (TREE_STATIC);
1204 if (code != TREE_BINFO)
1205 compare_values (TREE_PRIVATE);
1206 compare_values (TREE_PROTECTED);
1207 compare_values (TREE_DEPRECATED);
1208 if (TYPE_P (t1))
1210 compare_values (TYPE_SATURATING);
1211 compare_values (TYPE_ADDR_SPACE);
1213 else if (code == SSA_NAME)
1214 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1216 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1218 compare_values (TREE_INT_CST_LOW);
1219 compare_values (TREE_INT_CST_HIGH);
1222 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1224 /* ??? No suitable compare routine available. */
1225 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1226 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1227 if (r1.cl != r2.cl
1228 || r1.decimal != r2.decimal
1229 || r1.sign != r2.sign
1230 || r1.signalling != r2.signalling
1231 || r1.canonical != r2.canonical
1232 || r1.uexp != r2.uexp)
1233 return false;
1234 for (unsigned i = 0; i < SIGSZ; ++i)
1235 if (r1.sig[i] != r2.sig[i])
1236 return false;
1239 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1240 if (!fixed_compare (EQ_EXPR,
1241 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1242 return false;
1245 /* We don't want to compare locations, so there is nothing do compare
1246 for TS_DECL_MINIMAL. */
1248 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1250 compare_values (DECL_MODE);
1251 compare_values (DECL_NONLOCAL);
1252 compare_values (DECL_VIRTUAL_P);
1253 compare_values (DECL_IGNORED_P);
1254 compare_values (DECL_ABSTRACT);
1255 compare_values (DECL_ARTIFICIAL);
1256 compare_values (DECL_USER_ALIGN);
1257 compare_values (DECL_PRESERVE_P);
1258 compare_values (DECL_EXTERNAL);
1259 compare_values (DECL_GIMPLE_REG_P);
1260 compare_values (DECL_ALIGN);
1261 if (code == LABEL_DECL)
1263 compare_values (EH_LANDING_PAD_NR);
1264 compare_values (LABEL_DECL_UID);
1266 else if (code == FIELD_DECL)
1268 compare_values (DECL_PACKED);
1269 compare_values (DECL_NONADDRESSABLE_P);
1270 compare_values (DECL_OFFSET_ALIGN);
1272 else if (code == VAR_DECL)
1274 compare_values (DECL_HAS_DEBUG_EXPR_P);
1275 compare_values (DECL_NONLOCAL_FRAME);
1277 if (code == RESULT_DECL
1278 || code == PARM_DECL
1279 || code == VAR_DECL)
1281 compare_values (DECL_BY_REFERENCE);
1282 if (code == VAR_DECL
1283 || code == PARM_DECL)
1284 compare_values (DECL_HAS_VALUE_EXPR_P);
1288 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1289 compare_values (DECL_REGISTER);
1291 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1293 compare_values (DECL_COMMON);
1294 compare_values (DECL_DLLIMPORT_P);
1295 compare_values (DECL_WEAK);
1296 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1297 compare_values (DECL_COMDAT);
1298 compare_values (DECL_VISIBILITY);
1299 compare_values (DECL_VISIBILITY_SPECIFIED);
1300 if (code == VAR_DECL)
1302 compare_values (DECL_HARD_REGISTER);
1303 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1304 compare_values (DECL_IN_CONSTANT_POOL);
1305 compare_values (DECL_TLS_MODEL);
1307 if (VAR_OR_FUNCTION_DECL_P (t1))
1308 compare_values (DECL_INIT_PRIORITY);
1311 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1313 compare_values (DECL_BUILT_IN_CLASS);
1314 compare_values (DECL_STATIC_CONSTRUCTOR);
1315 compare_values (DECL_STATIC_DESTRUCTOR);
1316 compare_values (DECL_UNINLINABLE);
1317 compare_values (DECL_POSSIBLY_INLINED);
1318 compare_values (DECL_IS_NOVOPS);
1319 compare_values (DECL_IS_RETURNS_TWICE);
1320 compare_values (DECL_IS_MALLOC);
1321 compare_values (DECL_IS_OPERATOR_NEW);
1322 compare_values (DECL_DECLARED_INLINE_P);
1323 compare_values (DECL_STATIC_CHAIN);
1324 compare_values (DECL_NO_INLINE_WARNING_P);
1325 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1326 compare_values (DECL_NO_LIMIT_STACK);
1327 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1328 compare_values (DECL_PURE_P);
1329 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1330 compare_values (DECL_FINAL_P);
1331 compare_values (DECL_CXX_CONSTRUCTOR_P);
1332 compare_values (DECL_CXX_DESTRUCTOR_P);
1333 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1334 compare_values (DECL_FUNCTION_CODE);
1335 if (DECL_STATIC_DESTRUCTOR (t1))
1336 compare_values (DECL_FINI_PRIORITY);
1339 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1341 compare_values (TYPE_MODE);
1342 compare_values (TYPE_STRING_FLAG);
1343 compare_values (TYPE_NO_FORCE_BLK);
1344 compare_values (TYPE_NEEDS_CONSTRUCTING);
1345 if (RECORD_OR_UNION_TYPE_P (t1))
1347 compare_values (TYPE_TRANSPARENT_AGGR);
1348 compare_values (TYPE_FINAL_P);
1350 else if (code == ARRAY_TYPE)
1351 compare_values (TYPE_NONALIASED_COMPONENT);
1352 compare_values (TYPE_PACKED);
1353 compare_values (TYPE_RESTRICT);
1354 compare_values (TYPE_USER_ALIGN);
1355 compare_values (TYPE_READONLY);
1356 compare_values (TYPE_PRECISION);
1357 compare_values (TYPE_ALIGN);
1358 compare_values (TYPE_ALIAS_SET);
1361 /* We don't want to compare locations, so there is nothing do compare
1362 for TS_EXP. */
1364 /* BLOCKs are function local and we don't merge anything there, so
1365 simply refuse to merge. */
1366 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1367 return false;
1369 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1370 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1371 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1372 return false;
1374 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1375 if (memcmp (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2),
1376 sizeof (struct cl_target_option)) != 0)
1377 return false;
1379 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1380 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1381 sizeof (struct cl_optimization)) != 0)
1382 return false;
1384 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1385 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1386 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1387 return false;
1389 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1390 compare_values (CONSTRUCTOR_NELTS);
1392 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1393 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1394 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1395 IDENTIFIER_LENGTH (t1)) != 0)
1396 return false;
1398 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1399 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1400 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1401 TREE_STRING_LENGTH (t1)) != 0)
1402 return false;
1404 #undef compare_values
1407 /* Compare pointer fields. */
1409 /* Recurse. Search & Replaced from DFS_write_tree_body.
1410 Folding the early checks into the compare_tree_edges recursion
1411 macro makes debugging way quicker as you are able to break on
1412 compare_tree_sccs_1 and simply finish until a call returns false
1413 to spot the SCC members with the difference. */
1414 #define compare_tree_edges(E1, E2) \
1415 do { \
1416 tree t1_ = (E1), t2_ = (E2); \
1417 if (t1_ != t2_ \
1418 && (!t1_ || !t2_ \
1419 || !TREE_VISITED (t2_) \
1420 || (!TREE_ASM_WRITTEN (t2_) \
1421 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
1422 return false; \
1423 /* Only non-NULL trees outside of the SCC may compare equal. */ \
1424 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
1425 } while (0)
1427 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1429 if (code != IDENTIFIER_NODE)
1430 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
1433 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1435 unsigned i;
1436 /* Note that the number of elements for EXPR has already been emitted
1437 in EXPR's header (see streamer_write_tree_header). */
1438 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
1439 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
1442 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1444 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
1445 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1448 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1450 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
1451 /* ??? Global decls from different TUs have non-matching
1452 TRANSLATION_UNIT_DECLs. Only consider a small set of
1453 decls equivalent, we should not end up merging others. */
1454 if ((code == TYPE_DECL
1455 || code == NAMESPACE_DECL
1456 || code == IMPORTED_DECL
1457 || code == CONST_DECL
1458 || (VAR_OR_FUNCTION_DECL_P (t1)
1459 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
1460 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
1462 else
1463 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
1466 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1468 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
1469 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
1470 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
1471 if ((code == VAR_DECL
1472 || code == PARM_DECL)
1473 && DECL_HAS_VALUE_EXPR_P (t1))
1474 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
1475 if (code == VAR_DECL
1476 && DECL_HAS_DEBUG_EXPR_P (t1))
1477 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
1478 /* LTO specific edges. */
1479 if (code != FUNCTION_DECL
1480 && code != TRANSLATION_UNIT_DECL)
1481 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
1484 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1486 if (code == FUNCTION_DECL)
1488 tree a1, a2;
1489 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
1490 a1 || a2;
1491 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
1492 compare_tree_edges (a1, a2);
1493 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
1495 else if (code == TYPE_DECL)
1496 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
1497 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
1500 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1502 /* Make sure we don't inadvertently set the assembler name. */
1503 if (DECL_ASSEMBLER_NAME_SET_P (t1))
1504 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
1505 DECL_ASSEMBLER_NAME (t2));
1506 compare_tree_edges (DECL_SECTION_NAME (t1), DECL_SECTION_NAME (t2));
1507 compare_tree_edges (DECL_COMDAT_GROUP (t1), DECL_COMDAT_GROUP (t2));
1510 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1512 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
1513 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
1514 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
1515 DECL_BIT_FIELD_REPRESENTATIVE (t2));
1516 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
1517 DECL_FIELD_BIT_OFFSET (t2));
1518 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
1521 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1523 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
1524 DECL_FUNCTION_PERSONALITY (t2));
1525 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
1526 DECL_FUNCTION_SPECIFIC_TARGET (t2));
1527 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
1528 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
1531 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1533 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
1534 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
1535 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
1536 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
1537 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1538 reconstructed during fixup. */
1539 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
1540 during fixup. */
1541 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
1542 /* ??? Global types from different TUs have non-matching
1543 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
1544 equal. */
1545 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
1547 else
1548 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1549 /* TYPE_CANONICAL is re-computed during type merging, so do not
1550 compare it here. */
1551 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
1554 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1556 if (code == ENUMERAL_TYPE)
1557 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
1558 else if (code == ARRAY_TYPE)
1559 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
1560 else if (RECORD_OR_UNION_TYPE_P (t1))
1562 tree f1, f2;
1563 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1564 f1 || f2;
1565 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1566 compare_tree_edges (f1, f2);
1567 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
1569 else if (code == FUNCTION_TYPE
1570 || code == METHOD_TYPE)
1571 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
1572 if (!POINTER_TYPE_P (t1))
1573 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
1574 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
1577 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1579 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1580 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
1581 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
1584 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1585 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
1586 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
1588 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1590 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
1591 compare_tree_edges (TREE_OPERAND (t1, i),
1592 TREE_OPERAND (t2, i));
1594 /* BLOCKs are function local and we don't merge anything there. */
1595 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
1596 return false;
1599 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1601 unsigned i;
1602 tree t;
1603 /* Lengths have already been compared above. */
1604 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
1605 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
1606 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
1607 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
1608 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
1609 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
1610 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
1611 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1612 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1615 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1617 unsigned i;
1618 tree index, value;
1619 /* Lengths have already been compared above. */
1620 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
1622 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
1623 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
1627 #undef compare_tree_edges
1629 return true;
1632 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
1633 out MAP if they are equal. */
1635 static bool
1636 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
1637 tree *map)
1639 /* Assume SCC entry hashes are sorted after their cardinality. Which
1640 means we can simply take the first n-tuple of equal hashes
1641 (which is recorded as entry_len) and do n SCC entry candidate
1642 comparisons. */
1643 for (unsigned i = 0; i < pscc->entry_len; ++i)
1645 tree *mapp = map;
1646 num_scc_compare_collisions++;
1647 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
1649 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
1650 on the scc as all trees will be freed. */
1651 return true;
1653 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
1654 the SCC prevails. */
1655 for (unsigned j = 0; j < scc->len; ++j)
1656 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
1659 return false;
1662 /* QSort sort function to sort a map of two pointers after the 2nd
1663 pointer. */
1665 static int
1666 cmp_tree (const void *p1_, const void *p2_)
1668 tree *p1 = (tree *)(const_cast<void *>(p1_));
1669 tree *p2 = (tree *)(const_cast<void *>(p2_));
1670 if (p1[1] == p2[1])
1671 return 0;
1672 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
1675 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
1676 hash value SCC_HASH with an already recorded SCC. Return true if
1677 that was successful, otherwise return false. */
1679 static bool
1680 unify_scc (struct streamer_tree_cache_d *cache, unsigned from,
1681 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
1683 bool unified_p = false;
1684 tree_scc *scc
1685 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
1686 scc->next = NULL;
1687 scc->hash = scc_hash;
1688 scc->len = len;
1689 scc->entry_len = scc_entry_len;
1690 for (unsigned i = 0; i < len; ++i)
1692 tree t = streamer_tree_cache_get_tree (cache, from + i);
1693 scc->entries[i] = t;
1694 /* Do not merge SCCs with local entities inside them. Also do
1695 not merge TRANSLATION_UNIT_DECLs. */
1696 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
1697 || (VAR_OR_FUNCTION_DECL_P (t)
1698 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
1699 || TREE_CODE (t) == LABEL_DECL)
1701 /* Avoid doing any work for these cases and do not worry to
1702 record the SCCs for further merging. */
1703 return false;
1707 /* Look for the list of candidate SCCs to compare against. */
1708 tree_scc **slot;
1709 slot = tree_scc_hash.find_slot_with_hash (scc, scc_hash, INSERT);
1710 if (*slot)
1712 /* Try unifying against each candidate. */
1713 num_scc_compares++;
1715 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
1716 outside of the scc when following tree edges. Make sure
1717 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
1718 to track whether we visited the SCC member during the compare.
1719 We cannot use TREE_VISITED on the pscc members as the extended
1720 scc and pscc can overlap. */
1721 for (unsigned i = 0; i < scc->len; ++i)
1723 TREE_VISITED (scc->entries[i]) = 1;
1724 gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
1727 tree *map = XALLOCAVEC (tree, 2 * len);
1728 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
1730 if (!compare_tree_sccs (pscc, scc, map))
1731 continue;
1733 /* Found an equal SCC. */
1734 unified_p = true;
1735 num_scc_compare_collisions--;
1736 num_sccs_merged++;
1737 total_scc_size_merged += len;
1739 #ifdef ENABLE_CHECKING
1740 for (unsigned i = 0; i < len; ++i)
1742 tree t = map[2*i+1];
1743 enum tree_code code = TREE_CODE (t);
1744 /* IDENTIFIER_NODEs should be singletons and are merged by the
1745 streamer. The others should be singletons, too, and we
1746 should not merge them in any way. */
1747 gcc_assert (code != TRANSLATION_UNIT_DECL
1748 && code != IDENTIFIER_NODE
1749 && !streamer_handle_as_builtin_p (t));
1751 #endif
1753 /* Fixup the streamer cache with the prevailing nodes according
1754 to the tree node mapping computed by compare_tree_sccs. */
1755 if (len == 1)
1756 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
1757 else
1759 tree *map2 = XALLOCAVEC (tree, 2 * len);
1760 for (unsigned i = 0; i < len; ++i)
1762 map2[i*2] = (tree)(uintptr_t)(from + i);
1763 map2[i*2+1] = scc->entries[i];
1765 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
1766 qsort (map, len, 2 * sizeof (tree), cmp_tree);
1767 for (unsigned i = 0; i < len; ++i)
1768 streamer_tree_cache_replace_tree (cache, map[2*i],
1769 (uintptr_t)map2[2*i]);
1772 /* Free the tree nodes from the read SCC. */
1773 for (unsigned i = 0; i < len; ++i)
1775 if (TYPE_P (scc->entries[i]))
1776 num_merged_types++;
1777 ggc_free (scc->entries[i]);
1780 break;
1783 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
1784 if (!unified_p)
1785 for (unsigned i = 0; i < scc->len; ++i)
1786 TREE_VISITED (scc->entries[i]) = 0;
1789 /* If we didn't unify it to any candidate duplicate the relevant
1790 pieces to permanent storage and link it into the chain. */
1791 if (!unified_p)
1793 tree_scc *pscc
1794 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
1795 memcpy (pscc, scc, sizeof (tree_scc));
1796 pscc->next = (*slot);
1797 *slot = pscc;
1799 return unified_p;
1803 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
1804 RESOLUTIONS is the set of symbols picked by the linker (read from the
1805 resolution file when the linker plugin is being used). */
1807 static void
1808 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
1809 vec<ld_plugin_symbol_resolution_t> resolutions)
1811 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
1812 const int decl_offset = sizeof (struct lto_decl_header);
1813 const int main_offset = decl_offset + header->decl_state_size;
1814 const int string_offset = main_offset + header->main_size;
1815 struct lto_input_block ib_main;
1816 struct data_in *data_in;
1817 unsigned int i;
1818 const uint32_t *data_ptr, *data_end;
1819 uint32_t num_decl_states;
1821 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1822 header->main_size);
1824 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
1825 header->string_size, resolutions);
1827 /* We do not uniquify the pre-loaded cache entries, those are middle-end
1828 internal types that should not be merged. */
1830 /* Read the global declarations and types. */
1831 while (ib_main.p < ib_main.len)
1833 tree t;
1834 unsigned from = data_in->reader_cache->nodes.length ();
1835 /* Read and uniquify SCCs as in the input stream. */
1836 enum LTO_tags tag = streamer_read_record_start (&ib_main);
1837 if (tag == LTO_tree_scc)
1839 unsigned len_;
1840 unsigned scc_entry_len;
1841 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
1842 &scc_entry_len);
1843 unsigned len = data_in->reader_cache->nodes.length () - from;
1844 gcc_assert (len == len_);
1846 total_scc_size += len;
1847 num_sccs_read++;
1849 /* We have the special case of size-1 SCCs that are pre-merged
1850 by means of identifier and string sharing for example.
1851 ??? Maybe we should avoid streaming those as SCCs. */
1852 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
1853 from);
1854 if (len == 1
1855 && (TREE_CODE (first) == IDENTIFIER_NODE
1856 || TREE_CODE (first) == INTEGER_CST
1857 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
1858 || streamer_handle_as_builtin_p (first)))
1859 continue;
1861 /* Try to unify the SCC with already existing ones. */
1862 if (!flag_ltrans
1863 && unify_scc (data_in->reader_cache, from,
1864 len, scc_entry_len, scc_hash))
1865 continue;
1867 /* Do remaining fixup tasks for prevailing nodes. */
1868 bool seen_type = false;
1869 for (unsigned i = 0; i < len; ++i)
1871 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
1872 from + i);
1873 /* Reconstruct the type variant and pointer-to/reference-to
1874 chains. */
1875 if (TYPE_P (t))
1877 seen_type = true;
1878 num_prevailing_types++;
1879 lto_fixup_prevailing_type (t);
1881 /* Compute the canonical type of all types.
1882 ??? Should be able to assert that !TYPE_CANONICAL. */
1883 if (TYPE_P (t) && !TYPE_CANONICAL (t))
1884 gimple_register_canonical_type (t);
1885 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
1886 type which is also member of this SCC. */
1887 if (TREE_CODE (t) == INTEGER_CST
1888 && !TREE_OVERFLOW (t))
1889 cache_integer_cst (t);
1890 /* Register TYPE_DECLs with the debuginfo machinery. */
1891 if (!flag_wpa
1892 && TREE_CODE (t) == TYPE_DECL)
1893 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
1894 if (!flag_ltrans)
1896 /* Register variables and functions with the
1897 symbol table. */
1898 if (TREE_CODE (t) == VAR_DECL)
1899 lto_register_var_decl_in_symtab (data_in, t, from + i);
1900 else if (TREE_CODE (t) == FUNCTION_DECL
1901 && !DECL_BUILT_IN (t))
1902 lto_register_function_decl_in_symtab (data_in, t, from + i);
1903 /* Scan the tree for references to global functions or
1904 variables and record those for later fixup. */
1905 if (mentions_vars_p (t))
1906 vec_safe_push (tree_with_vars, t);
1909 if (seen_type)
1910 num_type_scc_trees += len;
1912 else
1914 /* Pickle stray references. */
1915 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
1916 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
1920 /* Read in lto_in_decl_state objects. */
1921 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
1922 data_end =
1923 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
1924 num_decl_states = *data_ptr++;
1926 gcc_assert (num_decl_states > 0);
1927 decl_data->global_decl_state = lto_new_in_decl_state ();
1928 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
1929 decl_data->global_decl_state);
1931 /* Read in per-function decl states and enter them in hash table. */
1932 decl_data->function_decl_states =
1933 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
1935 for (i = 1; i < num_decl_states; i++)
1937 struct lto_in_decl_state *state = lto_new_in_decl_state ();
1938 void **slot;
1940 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
1941 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
1942 gcc_assert (*slot == NULL);
1943 *slot = state;
1946 if (data_ptr != data_end)
1947 internal_error ("bytecode stream: garbage at the end of symbols section");
1949 /* Set the current decl state to be the global state. */
1950 decl_data->current_decl_state = decl_data->global_decl_state;
1952 lto_data_in_delete (data_in);
1955 /* Custom version of strtoll, which is not portable. */
1957 static HOST_WIDEST_INT
1958 lto_parse_hex (const char *p)
1960 HOST_WIDEST_INT ret = 0;
1962 for (; *p != '\0'; ++p)
1964 char c = *p;
1965 unsigned char part;
1966 ret <<= 4;
1967 if (c >= '0' && c <= '9')
1968 part = c - '0';
1969 else if (c >= 'a' && c <= 'f')
1970 part = c - 'a' + 10;
1971 else if (c >= 'A' && c <= 'F')
1972 part = c - 'A' + 10;
1973 else
1974 internal_error ("could not parse hex number");
1975 ret |= part;
1978 return ret;
1981 /* Read resolution for file named FILE_NAME. The resolution is read from
1982 RESOLUTION. */
1984 static void
1985 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
1987 /* We require that objects in the resolution file are in the same
1988 order as the lto1 command line. */
1989 unsigned int name_len;
1990 char *obj_name;
1991 unsigned int num_symbols;
1992 unsigned int i;
1993 struct lto_file_decl_data *file_data;
1994 splay_tree_node nd = NULL;
1996 if (!resolution)
1997 return;
1999 name_len = strlen (file->filename);
2000 obj_name = XNEWVEC (char, name_len + 1);
2001 fscanf (resolution, " "); /* Read white space. */
2003 fread (obj_name, sizeof (char), name_len, resolution);
2004 obj_name[name_len] = '\0';
2005 if (filename_cmp (obj_name, file->filename) != 0)
2006 internal_error ("unexpected file name %s in linker resolution file. "
2007 "Expected %s", obj_name, file->filename);
2008 if (file->offset != 0)
2010 int t;
2011 char offset_p[17];
2012 HOST_WIDEST_INT offset;
2013 t = fscanf (resolution, "@0x%16s", offset_p);
2014 if (t != 1)
2015 internal_error ("could not parse file offset");
2016 offset = lto_parse_hex (offset_p);
2017 if (offset != file->offset)
2018 internal_error ("unexpected offset");
2021 free (obj_name);
2023 fscanf (resolution, "%u", &num_symbols);
2025 for (i = 0; i < num_symbols; i++)
2027 int t;
2028 unsigned index;
2029 unsigned HOST_WIDE_INT id;
2030 char r_str[27];
2031 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2032 unsigned int j;
2033 unsigned int lto_resolution_str_len =
2034 sizeof (lto_resolution_str) / sizeof (char *);
2035 res_pair rp;
2037 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
2038 &index, &id, r_str);
2039 if (t != 3)
2040 internal_error ("invalid line in the resolution file");
2042 for (j = 0; j < lto_resolution_str_len; j++)
2044 if (strcmp (lto_resolution_str[j], r_str) == 0)
2046 r = (enum ld_plugin_symbol_resolution) j;
2047 break;
2050 if (j == lto_resolution_str_len)
2051 internal_error ("invalid resolution in the resolution file");
2053 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2055 nd = lto_splay_tree_lookup (file_ids, id);
2056 if (nd == NULL)
2057 internal_error ("resolution sub id %wx not in object file", id);
2060 file_data = (struct lto_file_decl_data *)nd->value;
2061 /* The indexes are very sparse. To save memory save them in a compact
2062 format that is only unpacked later when the subfile is processed. */
2063 rp.res = r;
2064 rp.index = index;
2065 file_data->respairs.safe_push (rp);
2066 if (file_data->max_index < index)
2067 file_data->max_index = index;
2071 /* List of file_decl_datas */
2072 struct file_data_list
2074 struct lto_file_decl_data *first, *last;
2077 /* Is the name for a id'ed LTO section? */
2079 static int
2080 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2082 const char *s;
2084 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
2085 return 0;
2086 s = strrchr (name, '.');
2087 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2090 /* Create file_data of each sub file id */
2092 static int
2093 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2094 struct file_data_list *list)
2096 struct lto_section_slot s_slot, *new_slot;
2097 unsigned HOST_WIDE_INT id;
2098 splay_tree_node nd;
2099 void **hash_slot;
2100 char *new_name;
2101 struct lto_file_decl_data *file_data;
2103 if (!lto_section_with_id (ls->name, &id))
2104 return 1;
2106 /* Find hash table of sub module id */
2107 nd = lto_splay_tree_lookup (file_ids, id);
2108 if (nd != NULL)
2110 file_data = (struct lto_file_decl_data *)nd->value;
2112 else
2114 file_data = ggc_alloc_lto_file_decl_data ();
2115 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2116 file_data->id = id;
2117 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
2118 lto_splay_tree_insert (file_ids, id, file_data);
2120 /* Maintain list in linker order */
2121 if (!list->first)
2122 list->first = file_data;
2123 if (list->last)
2124 list->last->next = file_data;
2125 list->last = file_data;
2128 /* Copy section into sub module hash table */
2129 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2130 s_slot.name = new_name;
2131 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2132 gcc_assert (*hash_slot == NULL);
2134 new_slot = XDUP (struct lto_section_slot, ls);
2135 new_slot->name = new_name;
2136 *hash_slot = new_slot;
2137 return 1;
2140 /* Read declarations and other initializations for a FILE_DATA. */
2142 static void
2143 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2145 const char *data;
2146 size_t len;
2147 vec<ld_plugin_symbol_resolution_t>
2148 resolutions = vNULL;
2149 int i;
2150 res_pair *rp;
2152 /* Create vector for fast access of resolution. We do this lazily
2153 to save memory. */
2154 resolutions.safe_grow_cleared (file_data->max_index + 1);
2155 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2156 resolutions[rp->index] = rp->res;
2157 file_data->respairs.release ();
2159 file_data->renaming_hash_table = lto_create_renaming_table ();
2160 file_data->file_name = file->filename;
2161 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2162 if (data == NULL)
2164 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2165 return;
2167 /* Frees resolutions */
2168 lto_read_decls (file_data, data, resolutions);
2169 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2172 /* Finalize FILE_DATA in FILE and increase COUNT. */
2174 static int
2175 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2176 int *count)
2178 lto_file_finalize (file_data, file);
2179 if (cgraph_dump_file)
2180 fprintf (cgraph_dump_file, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2181 file_data->file_name, file_data->id);
2182 (*count)++;
2183 return 0;
2186 /* Generate a TREE representation for all types and external decls
2187 entities in FILE.
2189 Read all of the globals out of the file. Then read the cgraph
2190 and process the .o index into the cgraph nodes so that it can open
2191 the .o file to load the functions and ipa information. */
2193 static struct lto_file_decl_data *
2194 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2196 struct lto_file_decl_data *file_data = NULL;
2197 splay_tree file_ids;
2198 htab_t section_hash_table;
2199 struct lto_section_slot *section;
2200 struct file_data_list file_list;
2201 struct lto_section_list section_list;
2203 memset (&section_list, 0, sizeof (struct lto_section_list));
2204 section_hash_table = lto_obj_build_section_table (file, &section_list);
2206 /* Find all sub modules in the object and put their sections into new hash
2207 tables in a splay tree. */
2208 file_ids = lto_splay_tree_new ();
2209 memset (&file_list, 0, sizeof (struct file_data_list));
2210 for (section = section_list.first; section != NULL; section = section->next)
2211 create_subid_section_table (section, file_ids, &file_list);
2213 /* Add resolutions to file ids */
2214 lto_resolution_read (file_ids, resolution_file, file);
2216 /* Finalize each lto file for each submodule in the merged object */
2217 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2218 lto_create_files_from_ids (file, file_data, count);
2220 splay_tree_delete (file_ids);
2221 htab_delete (section_hash_table);
2223 return file_list.first;
2226 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2227 #define LTO_MMAP_IO 1
2228 #endif
2230 #if LTO_MMAP_IO
2231 /* Page size of machine is used for mmap and munmap calls. */
2232 static size_t page_mask;
2233 #endif
2235 /* Get the section data of length LEN from FILENAME starting at
2236 OFFSET. The data segment must be freed by the caller when the
2237 caller is finished. Returns NULL if all was not well. */
2239 static char *
2240 lto_read_section_data (struct lto_file_decl_data *file_data,
2241 intptr_t offset, size_t len)
2243 char *result;
2244 static int fd = -1;
2245 static char *fd_name;
2246 #if LTO_MMAP_IO
2247 intptr_t computed_len;
2248 intptr_t computed_offset;
2249 intptr_t diff;
2250 #endif
2252 /* Keep a single-entry file-descriptor cache. The last file we
2253 touched will get closed at exit.
2254 ??? Eventually we want to add a more sophisticated larger cache
2255 or rather fix function body streaming to not stream them in
2256 practically random order. */
2257 if (fd != -1
2258 && filename_cmp (fd_name, file_data->file_name) != 0)
2260 free (fd_name);
2261 close (fd);
2262 fd = -1;
2264 if (fd == -1)
2266 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2267 if (fd == -1)
2269 fatal_error ("Cannot open %s", file_data->file_name);
2270 return NULL;
2272 fd_name = xstrdup (file_data->file_name);
2275 #if LTO_MMAP_IO
2276 if (!page_mask)
2278 size_t page_size = sysconf (_SC_PAGE_SIZE);
2279 page_mask = ~(page_size - 1);
2282 computed_offset = offset & page_mask;
2283 diff = offset - computed_offset;
2284 computed_len = len + diff;
2286 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2287 fd, computed_offset);
2288 if (result == MAP_FAILED)
2290 fatal_error ("Cannot map %s", file_data->file_name);
2291 return NULL;
2294 return result + diff;
2295 #else
2296 result = (char *) xmalloc (len);
2297 if (lseek (fd, offset, SEEK_SET) != offset
2298 || read (fd, result, len) != (ssize_t) len)
2300 free (result);
2301 fatal_error ("Cannot read %s", file_data->file_name);
2302 result = NULL;
2304 #ifdef __MINGW32__
2305 /* Native windows doesn't supports delayed unlink on opened file. So
2306 we close file here again. This produces higher I/O load, but at least
2307 it prevents to have dangling file handles preventing unlink. */
2308 free (fd_name);
2309 fd_name = NULL;
2310 close (fd);
2311 fd = -1;
2312 #endif
2313 return result;
2314 #endif
2318 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2319 NAME will be NULL unless the section type is for a function
2320 body. */
2322 static const char *
2323 get_section_data (struct lto_file_decl_data *file_data,
2324 enum lto_section_type section_type,
2325 const char *name,
2326 size_t *len)
2328 htab_t section_hash_table = file_data->section_hash_table;
2329 struct lto_section_slot *f_slot;
2330 struct lto_section_slot s_slot;
2331 const char *section_name = lto_get_section_name (section_type, name, file_data);
2332 char *data = NULL;
2334 *len = 0;
2335 s_slot.name = section_name;
2336 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2337 if (f_slot)
2339 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2340 *len = f_slot->len;
2343 free (CONST_CAST (char *, section_name));
2344 return data;
2348 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2349 starts at OFFSET and has LEN bytes. */
2351 static void
2352 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2353 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2354 const char *name ATTRIBUTE_UNUSED,
2355 const char *offset, size_t len ATTRIBUTE_UNUSED)
2357 #if LTO_MMAP_IO
2358 intptr_t computed_len;
2359 intptr_t computed_offset;
2360 intptr_t diff;
2361 #endif
2363 #if LTO_MMAP_IO
2364 computed_offset = ((intptr_t) offset) & page_mask;
2365 diff = (intptr_t) offset - computed_offset;
2366 computed_len = len + diff;
2368 munmap ((caddr_t) computed_offset, computed_len);
2369 #else
2370 free (CONST_CAST(char *, offset));
2371 #endif
2374 static lto_file *current_lto_file;
2376 /* Helper for qsort; compare partitions and return one with smaller size.
2377 We sort from greatest to smallest so parallel build doesn't stale on the
2378 longest compilation being executed too late. */
2380 static int
2381 cmp_partitions_size (const void *a, const void *b)
2383 const struct ltrans_partition_def *pa
2384 = *(struct ltrans_partition_def *const *)a;
2385 const struct ltrans_partition_def *pb
2386 = *(struct ltrans_partition_def *const *)b;
2387 return pb->insns - pa->insns;
2390 /* Helper for qsort; compare partitions and return one with smaller order. */
2392 static int
2393 cmp_partitions_order (const void *a, const void *b)
2395 const struct ltrans_partition_def *pa
2396 = *(struct ltrans_partition_def *const *)a;
2397 const struct ltrans_partition_def *pb
2398 = *(struct ltrans_partition_def *const *)b;
2399 int ordera = -1, orderb = -1;
2401 if (lto_symtab_encoder_size (pa->encoder))
2402 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->symbol.order;
2403 if (lto_symtab_encoder_size (pb->encoder))
2404 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->symbol.order;
2405 return orderb - ordera;
2408 /* Write all output files in WPA mode and the file with the list of
2409 LTRANS units. */
2411 static void
2412 lto_wpa_write_files (void)
2414 unsigned i, n_sets;
2415 lto_file *file;
2416 ltrans_partition part;
2417 FILE *ltrans_output_list_stream;
2418 char *temp_filename;
2419 size_t blen;
2421 /* Open the LTRANS output list. */
2422 if (!ltrans_output_list)
2423 fatal_error ("no LTRANS output list filename provided");
2424 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2425 if (ltrans_output_list_stream == NULL)
2426 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2428 timevar_push (TV_WHOPR_WPA);
2430 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
2431 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2433 /* Find out statics that need to be promoted
2434 to globals with hidden visibility because they are accessed from multiple
2435 partitions. */
2436 lto_promote_cross_file_statics ();
2438 timevar_pop (TV_WHOPR_WPA);
2440 timevar_push (TV_WHOPR_WPA_IO);
2442 /* Generate a prefix for the LTRANS unit files. */
2443 blen = strlen (ltrans_output_list);
2444 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2445 strcpy (temp_filename, ltrans_output_list);
2446 if (blen > sizeof (".out")
2447 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2448 ".out") == 0)
2449 temp_filename[blen - sizeof (".out") + 1] = '\0';
2450 blen = strlen (temp_filename);
2452 n_sets = ltrans_partitions.length ();
2454 /* Sort partitions by size so small ones are compiled last.
2455 FIXME: Even when not reordering we may want to output one list for parallel make
2456 and other for final link command. */
2457 ltrans_partitions.qsort (flag_toplevel_reorder
2458 ? cmp_partitions_size
2459 : cmp_partitions_order);
2460 for (i = 0; i < n_sets; i++)
2462 size_t len;
2463 ltrans_partition part = ltrans_partitions[i];
2465 /* Write all the nodes in SET. */
2466 sprintf (temp_filename + blen, "%u.o", i);
2467 file = lto_obj_file_open (temp_filename, true);
2468 if (!file)
2469 fatal_error ("lto_obj_file_open() failed");
2471 if (!quiet_flag)
2472 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2473 if (cgraph_dump_file)
2475 lto_symtab_encoder_iterator lsei;
2477 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
2478 part->name, temp_filename, part->insns);
2479 fprintf (cgraph_dump_file, " Symbols in partition: ");
2480 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2481 lsei_next_in_partition (&lsei))
2483 symtab_node node = lsei_node (lsei);
2484 fprintf (cgraph_dump_file, "%s ", symtab_node_asm_name (node));
2486 fprintf (cgraph_dump_file, "\n Symbols in boundary: ");
2487 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2488 lsei_next (&lsei))
2490 symtab_node node = lsei_node (lsei);
2491 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2493 fprintf (cgraph_dump_file, "%s ", symtab_node_asm_name (node));
2494 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
2495 if (cnode
2496 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
2497 fprintf (cgraph_dump_file, "(body included)");
2498 else
2500 varpool_node *vnode = dyn_cast <varpool_node> (node);
2501 if (vnode
2502 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
2503 fprintf (cgraph_dump_file, "(initializer included)");
2507 fprintf (cgraph_dump_file, "\n");
2509 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2511 lto_set_current_out_file (file);
2513 ipa_write_optimization_summaries (part->encoder);
2515 lto_set_current_out_file (NULL);
2516 lto_obj_file_close (file);
2517 free (file);
2518 part->encoder = NULL;
2520 len = strlen (temp_filename);
2521 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
2522 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2523 fatal_error ("writing to LTRANS output list %s: %m",
2524 ltrans_output_list);
2527 lto_stats.num_output_files += n_sets;
2529 /* Close the LTRANS output list. */
2530 if (fclose (ltrans_output_list_stream))
2531 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2533 free_ltrans_partitions();
2534 free (temp_filename);
2536 timevar_pop (TV_WHOPR_WPA_IO);
2540 /* If TT is a variable or function decl replace it with its
2541 prevailing variant. */
2542 #define LTO_SET_PREVAIL(tt) \
2543 do {\
2544 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt) \
2545 && (TREE_PUBLIC (tt) || DECL_EXTERNAL (tt))) \
2547 tt = lto_symtab_prevailing_decl (tt); \
2548 fixed = true; \
2550 } while (0)
2552 /* Ensure that TT isn't a replacable var of function decl. */
2553 #define LTO_NO_PREVAIL(tt) \
2554 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2556 /* Given a tree T replace all fields referring to variables or functions
2557 with their prevailing variant. */
2558 static void
2559 lto_fixup_prevailing_decls (tree t)
2561 enum tree_code code = TREE_CODE (t);
2562 bool fixed = false;
2564 gcc_checking_assert (code != CONSTRUCTOR && code != TREE_BINFO);
2565 LTO_NO_PREVAIL (TREE_TYPE (t));
2566 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2567 LTO_NO_PREVAIL (TREE_CHAIN (t));
2568 if (DECL_P (t))
2570 LTO_NO_PREVAIL (DECL_NAME (t));
2571 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2572 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2574 LTO_SET_PREVAIL (DECL_SIZE (t));
2575 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2576 LTO_SET_PREVAIL (DECL_INITIAL (t));
2577 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2578 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2580 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2582 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2583 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
2585 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2587 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
2588 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2589 LTO_NO_PREVAIL (DECL_VINDEX (t));
2591 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2592 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2593 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2595 LTO_SET_PREVAIL (DECL_FIELD_OFFSET (t));
2596 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2597 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2598 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2599 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2602 else if (TYPE_P (t))
2604 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2605 LTO_SET_PREVAIL (TYPE_SIZE (t));
2606 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2607 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2608 LTO_NO_PREVAIL (TYPE_NAME (t));
2610 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2611 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2612 LTO_NO_PREVAIL (t->type_non_common.binfo);
2614 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2616 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2617 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2618 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2620 else if (EXPR_P (t))
2622 int i;
2623 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2624 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2626 else
2628 switch (code)
2630 case TREE_LIST:
2631 LTO_SET_PREVAIL (TREE_VALUE (t));
2632 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2633 LTO_NO_PREVAIL (TREE_PURPOSE (t));
2634 break;
2635 default:
2636 gcc_unreachable ();
2639 /* If we fixed nothing, then we missed something seen by
2640 mentions_vars_p. */
2641 gcc_checking_assert (fixed);
2643 #undef LTO_SET_PREVAIL
2644 #undef LTO_NO_PREVAIL
2646 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2647 replaces var and function decls with the corresponding prevailing def. */
2649 static void
2650 lto_fixup_state (struct lto_in_decl_state *state)
2652 unsigned i, si;
2653 struct lto_tree_ref_table *table;
2655 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2656 we still need to walk from all DECLs to find the reachable
2657 FUNCTION_DECLs and VAR_DECLs. */
2658 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2660 table = &state->streams[si];
2661 for (i = 0; i < table->size; i++)
2663 tree *tp = table->trees + i;
2664 if (VAR_OR_FUNCTION_DECL_P (*tp)
2665 && (TREE_PUBLIC (*tp) || DECL_EXTERNAL (*tp)))
2666 *tp = lto_symtab_prevailing_decl (*tp);
2671 /* A callback of htab_traverse. Just extracts a state from SLOT
2672 and calls lto_fixup_state. */
2674 static int
2675 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
2677 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
2678 lto_fixup_state (state);
2679 return 1;
2682 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2683 prevailing one. */
2685 static void
2686 lto_fixup_decls (struct lto_file_decl_data **files)
2688 unsigned int i;
2689 tree t;
2691 if (tree_with_vars)
2692 FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
2693 lto_fixup_prevailing_decls (t);
2695 for (i = 0; files[i]; i++)
2697 struct lto_file_decl_data *file = files[i];
2698 struct lto_in_decl_state *state = file->global_decl_state;
2699 lto_fixup_state (state);
2701 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
2705 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2707 /* Turn file datas for sub files into a single array, so that they look
2708 like separate files for further passes. */
2710 static void
2711 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2713 struct lto_file_decl_data *n, *next;
2714 int i, k;
2716 lto_stats.num_input_files = count;
2717 all_file_decl_data
2718 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
2719 /* Set the hooks so that all of the ipa passes can read in their data. */
2720 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2721 for (i = 0, k = 0; i < last_file_ix; i++)
2723 for (n = orig[i]; n != NULL; n = next)
2725 all_file_decl_data[k++] = n;
2726 next = n->next;
2727 n->next = NULL;
2730 all_file_decl_data[k] = NULL;
2731 gcc_assert (k == count);
2734 /* Input file data before flattening (i.e. splitting them to subfiles to support
2735 incremental linking. */
2736 static int real_file_count;
2737 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2739 static void print_lto_report_1 (void);
2741 /* Read all the symbols from the input files FNAMES. NFILES is the
2742 number of files requested in the command line. Instantiate a
2743 global call graph by aggregating all the sub-graphs found in each
2744 file. */
2746 static void
2747 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2749 unsigned int i, last_file_ix;
2750 FILE *resolution;
2751 int count = 0;
2752 struct lto_file_decl_data **decl_data;
2753 void **res;
2754 symtab_node snode;
2756 init_cgraph ();
2758 timevar_push (TV_IPA_LTO_DECL_IN);
2760 real_file_decl_data
2761 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
2762 real_file_count = nfiles;
2764 /* Read the resolution file. */
2765 resolution = NULL;
2766 if (resolution_file_name)
2768 int t;
2769 unsigned num_objects;
2771 resolution = fopen (resolution_file_name, "r");
2772 if (resolution == NULL)
2773 fatal_error ("could not open symbol resolution file: %m");
2775 t = fscanf (resolution, "%u", &num_objects);
2776 gcc_assert (t == 1);
2778 /* True, since the plugin splits the archives. */
2779 gcc_assert (num_objects == nfiles);
2781 cgraph_state = CGRAPH_LTO_STREAMING;
2783 canonical_type_hash_cache = new pointer_map <hashval_t>;
2784 gimple_canonical_types = htab_create_ggc (16381, gimple_canonical_type_hash,
2785 gimple_canonical_type_eq, 0);
2786 gcc_obstack_init (&tree_scc_hash_obstack);
2787 tree_scc_hash.create (4096);
2789 /* Register the common node types with the canonical type machinery so
2790 we properly share alias-sets across languages and TUs. Do not
2791 expose the common nodes as type merge target - those that should be
2792 are already exposed so by pre-loading the LTO streamer caches.
2793 Do two passes - first clear TYPE_CANONICAL and then re-compute it. */
2794 for (i = 0; i < itk_none; ++i)
2795 lto_register_canonical_types (integer_types[i], true);
2796 for (i = 0; i < stk_type_kind_last; ++i)
2797 lto_register_canonical_types (sizetype_tab[i], true);
2798 for (i = 0; i < TI_MAX; ++i)
2799 lto_register_canonical_types (global_trees[i], true);
2800 for (i = 0; i < itk_none; ++i)
2801 lto_register_canonical_types (integer_types[i], false);
2802 for (i = 0; i < stk_type_kind_last; ++i)
2803 lto_register_canonical_types (sizetype_tab[i], false);
2804 for (i = 0; i < TI_MAX; ++i)
2805 lto_register_canonical_types (global_trees[i], false);
2807 if (!quiet_flag)
2808 fprintf (stderr, "Reading object files:");
2810 /* Read all of the object files specified on the command line. */
2811 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2813 struct lto_file_decl_data *file_data = NULL;
2814 if (!quiet_flag)
2816 fprintf (stderr, " %s", fnames[i]);
2817 fflush (stderr);
2820 current_lto_file = lto_obj_file_open (fnames[i], false);
2821 if (!current_lto_file)
2822 break;
2824 file_data = lto_file_read (current_lto_file, resolution, &count);
2825 if (!file_data)
2827 lto_obj_file_close (current_lto_file);
2828 free (current_lto_file);
2829 current_lto_file = NULL;
2830 break;
2833 decl_data[last_file_ix++] = file_data;
2835 lto_obj_file_close (current_lto_file);
2836 free (current_lto_file);
2837 current_lto_file = NULL;
2840 lto_flatten_files (decl_data, count, last_file_ix);
2841 lto_stats.num_input_files = count;
2842 ggc_free(decl_data);
2843 real_file_decl_data = NULL;
2845 if (resolution_file_name)
2846 fclose (resolution);
2848 /* Show the LTO report before launching LTRANS. */
2849 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
2850 print_lto_report_1 ();
2852 /* Free gimple type merging datastructures. */
2853 tree_scc_hash.dispose ();
2854 obstack_free (&tree_scc_hash_obstack, NULL);
2855 htab_delete (gimple_canonical_types);
2856 gimple_canonical_types = NULL;
2857 delete canonical_type_hash_cache;
2858 canonical_type_hash_cache = NULL;
2859 ggc_collect ();
2861 /* Set the hooks so that all of the ipa passes can read in their data. */
2862 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2864 timevar_pop (TV_IPA_LTO_DECL_IN);
2866 if (!quiet_flag)
2867 fprintf (stderr, "\nReading the callgraph\n");
2869 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2870 /* Read the symtab. */
2871 input_symtab ();
2873 /* Store resolutions into the symbol table. */
2875 FOR_EACH_SYMBOL (snode)
2876 if (symtab_real_symbol_p (snode)
2877 && snode->symbol.lto_file_data
2878 && snode->symbol.lto_file_data->resolution_map
2879 && (res = pointer_map_contains (snode->symbol.lto_file_data->resolution_map,
2880 snode->symbol.decl)))
2881 snode->symbol.resolution
2882 = (enum ld_plugin_symbol_resolution)(size_t)*res;
2883 for (i = 0; all_file_decl_data[i]; i++)
2884 if (all_file_decl_data[i]->resolution_map)
2886 pointer_map_destroy (all_file_decl_data[i]->resolution_map);
2887 all_file_decl_data[i]->resolution_map = NULL;
2890 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2892 if (!quiet_flag)
2893 fprintf (stderr, "Merging declarations\n");
2895 timevar_push (TV_IPA_LTO_DECL_MERGE);
2896 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
2897 need to care about resolving symbols again, we only need to replace
2898 duplicated declarations read from the callgraph and from function
2899 sections. */
2900 if (!flag_ltrans)
2902 lto_symtab_merge_decls ();
2904 /* If there were errors during symbol merging bail out, we have no
2905 good way to recover here. */
2906 if (seen_error ())
2907 fatal_error ("errors during merging of translation units");
2909 /* Fixup all decls. */
2910 lto_fixup_decls (all_file_decl_data);
2912 if (tree_with_vars)
2913 ggc_free (tree_with_vars);
2914 tree_with_vars = NULL;
2915 ggc_collect ();
2917 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2918 /* Each pass will set the appropriate timer. */
2920 if (!quiet_flag)
2921 fprintf (stderr, "Reading summaries\n");
2923 /* Read the IPA summary data. */
2924 if (flag_ltrans)
2925 ipa_read_optimization_summaries ();
2926 else
2927 ipa_read_summaries ();
2929 for (i = 0; all_file_decl_data[i]; i++)
2931 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
2932 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
2933 all_file_decl_data[i]->symtab_node_encoder = NULL;
2934 lto_free_function_in_decl_state (all_file_decl_data[i]->global_decl_state);
2935 all_file_decl_data[i]->global_decl_state = NULL;
2936 all_file_decl_data[i]->current_decl_state = NULL;
2939 /* Finally merge the cgraph according to the decl merging decisions. */
2940 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2941 if (cgraph_dump_file)
2943 fprintf (cgraph_dump_file, "Before merging:\n");
2944 dump_symtab (cgraph_dump_file);
2946 lto_symtab_merge_symbols ();
2947 ggc_collect ();
2948 cgraph_state = CGRAPH_STATE_IPA_SSA;
2950 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
2952 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
2954 /* Indicate that the cgraph is built and ready. */
2955 cgraph_function_flags_ready = true;
2957 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
2958 ggc_free (all_file_decl_data);
2959 all_file_decl_data = NULL;
2963 /* Materialize all the bodies for all the nodes in the callgraph. */
2965 static void
2966 materialize_cgraph (void)
2968 tree decl;
2969 struct cgraph_node *node;
2970 unsigned i;
2971 timevar_id_t lto_timer;
2973 if (!quiet_flag)
2974 fprintf (stderr,
2975 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
2977 /* Now that we have input the cgraph, we need to clear all of the aux
2978 nodes and read the functions if we are not running in WPA mode. */
2979 timevar_push (TV_IPA_LTO_GIMPLE_IN);
2981 FOR_EACH_FUNCTION (node)
2983 if (node->symbol.lto_file_data)
2985 lto_materialize_function (node);
2986 lto_stats.num_input_cgraph_nodes++;
2990 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
2992 /* Start the appropriate timer depending on the mode that we are
2993 operating in. */
2994 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
2995 : (flag_ltrans) ? TV_WHOPR_LTRANS
2996 : TV_LTO;
2997 timevar_push (lto_timer);
2999 current_function_decl = NULL;
3000 set_cfun (NULL);
3002 /* Inform the middle end about the global variables we have seen. */
3003 FOR_EACH_VEC_ELT (*lto_global_var_decls, i, decl)
3004 rest_of_decl_compilation (decl, 1, 0);
3006 if (!quiet_flag)
3007 fprintf (stderr, "\n");
3009 timevar_pop (lto_timer);
3013 /* Show various memory usage statistics related to LTO. */
3014 static void
3015 print_lto_report_1 (void)
3017 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3018 fprintf (stderr, "%s statistics\n", pfx);
3020 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3021 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3022 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3023 if (flag_wpa && tree_scc_hash.is_created ())
3025 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3026 "collision ratio: %f\n", pfx,
3027 (long) tree_scc_hash.size (),
3028 (long) tree_scc_hash.elements (),
3029 tree_scc_hash.collisions ());
3030 hash_table<tree_scc_hasher>::iterator hiter;
3031 tree_scc *scc, *max_scc = NULL;
3032 unsigned max_length = 0;
3033 FOR_EACH_HASH_TABLE_ELEMENT (tree_scc_hash, scc, x, hiter)
3035 unsigned length = 0;
3036 tree_scc *s = scc;
3037 for (; s; s = s->next)
3038 length++;
3039 if (length > max_length)
3041 max_length = length;
3042 max_scc = scc;
3045 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3046 pfx, max_length, max_scc->len);
3047 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3048 num_scc_compares, num_scc_compare_collisions,
3049 num_scc_compare_collisions / (double) num_scc_compares);
3050 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3051 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3052 total_scc_size_merged);
3053 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3054 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3055 pfx, num_prevailing_types, num_type_scc_trees);
3056 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3057 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
3058 (long) htab_size (gimple_canonical_types),
3059 (long) htab_elements (gimple_canonical_types),
3060 (long) gimple_canonical_types->searches,
3061 (long) gimple_canonical_types->collisions,
3062 htab_collisions (gimple_canonical_types));
3063 fprintf (stderr, "[%s] GIMPLE canonical type pointer-map: "
3064 "%lu elements, %ld searches\n", pfx,
3065 num_canonical_type_hash_entries,
3066 num_canonical_type_hash_queries);
3069 print_lto_report (pfx);
3072 /* Perform whole program analysis (WPA) on the callgraph and write out the
3073 optimization plan. */
3075 static void
3076 do_whole_program_analysis (void)
3078 symtab_node node;
3080 timevar_start (TV_PHASE_OPT_GEN);
3082 /* Note that since we are in WPA mode, materialize_cgraph will not
3083 actually read in all the function bodies. It only materializes
3084 the decls and cgraph nodes so that analysis can be performed. */
3085 materialize_cgraph ();
3087 /* Reading in the cgraph uses different timers, start timing WPA now. */
3088 timevar_push (TV_WHOPR_WPA);
3090 if (pre_ipa_mem_report)
3092 fprintf (stderr, "Memory consumption before IPA\n");
3093 dump_memory_report (false);
3096 cgraph_function_flags_ready = true;
3098 if (cgraph_dump_file)
3099 dump_symtab (cgraph_dump_file);
3100 bitmap_obstack_initialize (NULL);
3101 cgraph_state = CGRAPH_STATE_IPA_SSA;
3103 execute_ipa_pass_list (g->get_passes ()->all_regular_ipa_passes);
3104 symtab_remove_unreachable_nodes (false, dump_file);
3106 if (cgraph_dump_file)
3108 fprintf (cgraph_dump_file, "Optimized ");
3109 dump_symtab (cgraph_dump_file);
3111 #ifdef ENABLE_CHECKING
3112 verify_cgraph ();
3113 #endif
3114 bitmap_obstack_release (NULL);
3116 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3117 timevar_pop (TV_WHOPR_WPA);
3119 timevar_push (TV_WHOPR_PARTITIONING);
3120 if (flag_lto_partition_1to1)
3121 lto_1_to_1_map ();
3122 else if (flag_lto_partition_max)
3123 lto_max_map ();
3124 else
3125 lto_balanced_map ();
3127 /* AUX pointers are used by partitioning code to bookkeep number of
3128 partitions symbol is in. This is no longer needed. */
3129 FOR_EACH_SYMBOL (node)
3130 node->symbol.aux = NULL;
3132 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3133 timevar_pop (TV_WHOPR_PARTITIONING);
3135 timevar_stop (TV_PHASE_OPT_GEN);
3136 timevar_start (TV_PHASE_STREAM_OUT);
3138 if (!quiet_flag)
3140 fprintf (stderr, "\nStreaming out");
3141 fflush (stderr);
3143 lto_wpa_write_files ();
3144 if (!quiet_flag)
3145 fprintf (stderr, "\n");
3147 timevar_stop (TV_PHASE_STREAM_OUT);
3149 ggc_collect ();
3150 if (post_ipa_mem_report)
3152 fprintf (stderr, "Memory consumption after IPA\n");
3153 dump_memory_report (false);
3156 /* Show the LTO report before launching LTRANS. */
3157 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3158 print_lto_report_1 ();
3159 if (mem_report_wpa)
3160 dump_memory_report (true);
3164 static GTY(()) tree lto_eh_personality_decl;
3166 /* Return the LTO personality function decl. */
3168 tree
3169 lto_eh_personality (void)
3171 if (!lto_eh_personality_decl)
3173 /* Use the first personality DECL for our personality if we don't
3174 support multiple ones. This ensures that we don't artificially
3175 create the need for them in a single-language program. */
3176 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3177 lto_eh_personality_decl = first_personality_decl;
3178 else
3179 lto_eh_personality_decl = lhd_gcc_personality ();
3182 return lto_eh_personality_decl;
3185 /* Set the process name based on the LTO mode. */
3187 static void
3188 lto_process_name (void)
3190 if (flag_lto)
3191 setproctitle ("lto1-lto");
3192 if (flag_wpa)
3193 setproctitle ("lto1-wpa");
3194 if (flag_ltrans)
3195 setproctitle ("lto1-ltrans");
3199 /* Initialize the LTO front end. */
3201 static void
3202 lto_init (void)
3204 lto_process_name ();
3205 lto_streamer_hooks_init ();
3206 lto_reader_init ();
3207 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3208 memset (&lto_stats, 0, sizeof (lto_stats));
3209 bitmap_obstack_initialize (NULL);
3210 gimple_register_cfg_hooks ();
3214 /* Main entry point for the GIMPLE front end. This front end has
3215 three main personalities:
3217 - LTO (-flto). All the object files on the command line are
3218 loaded in memory and processed as a single translation unit.
3219 This is the traditional link-time optimization behavior.
3221 - WPA (-fwpa). Only the callgraph and summary information for
3222 files in the command file are loaded. A single callgraph
3223 (without function bodies) is instantiated for the whole set of
3224 files. IPA passes are only allowed to analyze the call graph
3225 and make transformation decisions. The callgraph is
3226 partitioned, each partition is written to a new object file
3227 together with the transformation decisions.
3229 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3230 summary files from running again. Since WPA computed summary
3231 information and decided what transformations to apply, LTRANS
3232 simply applies them. */
3234 void
3235 lto_main (void)
3237 /* LTO is called as a front end, even though it is not a front end.
3238 Because it is called as a front end, TV_PHASE_PARSING and
3239 TV_PARSE_GLOBAL are active, and we need to turn them off while
3240 doing LTO. Later we turn them back on so they are active up in
3241 toplev.c. */
3242 timevar_pop (TV_PARSE_GLOBAL);
3243 timevar_stop (TV_PHASE_PARSING);
3245 timevar_start (TV_PHASE_SETUP);
3247 /* Initialize the LTO front end. */
3248 lto_init ();
3250 timevar_stop (TV_PHASE_SETUP);
3251 timevar_start (TV_PHASE_STREAM_IN);
3253 /* Read all the symbols and call graph from all the files in the
3254 command line. */
3255 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3257 timevar_stop (TV_PHASE_STREAM_IN);
3259 if (!seen_error ())
3261 /* If WPA is enabled analyze the whole call graph and create an
3262 optimization plan. Otherwise, read in all the function
3263 bodies and continue with optimization. */
3264 if (flag_wpa)
3265 do_whole_program_analysis ();
3266 else
3268 struct varpool_node *vnode;
3270 timevar_start (TV_PHASE_OPT_GEN);
3272 materialize_cgraph ();
3273 if (!flag_ltrans)
3274 lto_promote_statics_nonwpa ();
3276 /* Let the middle end know that we have read and merged all of
3277 the input files. */
3278 compile ();
3280 timevar_stop (TV_PHASE_OPT_GEN);
3282 /* FIXME lto, if the processes spawned by WPA fail, we miss
3283 the chance to print WPA's report, so WPA will call
3284 print_lto_report before launching LTRANS. If LTRANS was
3285 launched directly by the driver we would not need to do
3286 this. */
3287 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3288 print_lto_report_1 ();
3290 /* Record the global variables. */
3291 FOR_EACH_DEFINED_VARIABLE (vnode)
3292 vec_safe_push (lto_global_var_decls, vnode->symbol.decl);
3296 /* Here we make LTO pretend to be a parser. */
3297 timevar_start (TV_PHASE_PARSING);
3298 timevar_push (TV_PARSE_GLOBAL);
3301 #include "gt-lto-lto.h"