* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / gcc / lto / lto.c
blobc0f9328b726e74e3716e75a2c95904126113e0c8
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"
50 static GTY(()) tree first_personality_decl;
52 /* Returns a hash code for P. */
54 static hashval_t
55 hash_name (const void *p)
57 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
58 return (hashval_t) htab_hash_string (ds->name);
62 /* Returns nonzero if P1 and P2 are equal. */
64 static int
65 eq_name (const void *p1, const void *p2)
67 const struct lto_section_slot *s1 =
68 (const struct lto_section_slot *) p1;
69 const struct lto_section_slot *s2 =
70 (const struct lto_section_slot *) p2;
72 return strcmp (s1->name, s2->name) == 0;
75 /* Free lto_section_slot */
77 static void
78 free_with_string (void *arg)
80 struct lto_section_slot *s = (struct lto_section_slot *)arg;
82 free (CONST_CAST (char *, s->name));
83 free (arg);
86 /* Create section hash table */
88 htab_t
89 lto_obj_create_section_hash_table (void)
91 return htab_create (37, hash_name, eq_name, free_with_string);
94 /* Delete an allocated integer KEY in the splay tree. */
96 static void
97 lto_splay_tree_delete_id (splay_tree_key key)
99 free ((void *) key);
102 /* Compare splay tree node ids A and B. */
104 static int
105 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
107 unsigned HOST_WIDE_INT ai;
108 unsigned HOST_WIDE_INT bi;
110 ai = *(unsigned HOST_WIDE_INT *) a;
111 bi = *(unsigned HOST_WIDE_INT *) b;
113 if (ai < bi)
114 return -1;
115 else if (ai > bi)
116 return 1;
117 return 0;
120 /* Look up splay tree node by ID in splay tree T. */
122 static splay_tree_node
123 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
125 return splay_tree_lookup (t, (splay_tree_key) &id);
128 /* Check if KEY has ID. */
130 static bool
131 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
133 return *(unsigned HOST_WIDE_INT *) key == id;
136 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
137 The ID is allocated separately because we need HOST_WIDE_INTs which may
138 be wider than a splay_tree_key. */
140 static void
141 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
142 struct lto_file_decl_data *file_data)
144 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
145 *idp = id;
146 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
149 /* Create a splay tree. */
151 static splay_tree
152 lto_splay_tree_new (void)
154 return splay_tree_new (lto_splay_tree_compare_ids,
155 lto_splay_tree_delete_id,
156 NULL);
159 /* Return true when NODE has a clone that is analyzed (i.e. we need
160 to load its body even if the node itself is not needed). */
162 static bool
163 has_analyzed_clone_p (struct cgraph_node *node)
165 struct cgraph_node *orig = node;
166 node = node->clones;
167 if (node)
168 while (node != orig)
170 if (node->symbol.analyzed)
171 return true;
172 if (node->clones)
173 node = node->clones;
174 else if (node->next_sibling_clone)
175 node = node->next_sibling_clone;
176 else
178 while (node != orig && !node->next_sibling_clone)
179 node = node->clone_of;
180 if (node != orig)
181 node = node->next_sibling_clone;
184 return false;
187 /* Read the function body for the function associated with NODE. */
189 static void
190 lto_materialize_function (struct cgraph_node *node)
192 tree decl;
193 struct lto_file_decl_data *file_data;
194 const char *data, *name;
195 size_t len;
197 decl = node->symbol.decl;
198 /* Read in functions with body (analyzed nodes)
199 and also functions that are needed to produce virtual clones. */
200 if ((cgraph_function_with_gimple_body_p (node) && node->symbol.analyzed)
201 || has_analyzed_clone_p (node))
203 /* Clones don't need to be read. */
204 if (node->clone_of)
205 return;
207 /* Load the function body only if not operating in WPA mode. In
208 WPA mode, the body of the function is not needed. */
209 if (!flag_wpa)
211 file_data = node->symbol.lto_file_data;
212 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
214 /* We may have renamed the declaration, e.g., a static function. */
215 name = lto_get_decl_name_mapping (file_data, name);
217 data = lto_get_section_data (file_data, LTO_section_function_body,
218 name, &len);
219 if (!data)
220 fatal_error ("%s: section %s is missing",
221 file_data->file_name,
222 name);
224 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
226 push_struct_function (decl);
227 announce_function (decl);
228 lto_input_function_body (file_data, decl, data);
229 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
230 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
231 lto_stats.num_function_bodies++;
232 lto_free_section_data (file_data, LTO_section_function_body, name,
233 data, len);
234 pop_cfun ();
235 ggc_collect ();
239 /* Let the middle end know about the function. */
240 rest_of_decl_compilation (decl, 1, 0);
244 /* Decode the content of memory pointed to by DATA in the in decl
245 state object STATE. DATA_IN points to a data_in structure for
246 decoding. Return the address after the decoded object in the
247 input. */
249 static const uint32_t *
250 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
251 struct lto_in_decl_state *state)
253 uint32_t ix;
254 tree decl;
255 uint32_t i, j;
257 ix = *data++;
258 decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
259 if (TREE_CODE (decl) != FUNCTION_DECL)
261 gcc_assert (decl == void_type_node);
262 decl = NULL_TREE;
264 state->fn_decl = decl;
266 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
268 uint32_t size = *data++;
269 tree *decls = ggc_alloc_vec_tree (size);
271 for (j = 0; j < size; j++)
272 decls[j] = streamer_tree_cache_get_tree (data_in->reader_cache, data[j]);
274 state->streams[i].size = size;
275 state->streams[i].trees = decls;
276 data += size;
279 return data;
284 /* ??? Old hashing and merging code follows, we keep it for statistics
285 purposes for now. */
287 /* Global type table. FIXME, it should be possible to re-use some
288 of the type hashing routines in tree.c (type_hash_canon, type_hash_lookup,
289 etc), but those assume that types were built with the various
290 build_*_type routines which is not the case with the streamer. */
291 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node)))
292 htab_t gimple_types;
293 static GTY((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
294 htab_t type_hash_cache;
296 static hashval_t gimple_type_hash (const void *);
298 /* Structure used to maintain a cache of some type pairs compared by
299 gimple_types_compatible_p when comparing aggregate types. There are
300 three possible values for SAME_P:
302 -2: The pair (T1, T2) has just been inserted in the table.
303 0: T1 and T2 are different types.
304 1: T1 and T2 are the same type. */
306 struct type_pair_d
308 unsigned int uid1;
309 unsigned int uid2;
310 signed char same_p;
312 typedef struct type_pair_d *type_pair_t;
314 #define GIMPLE_TYPE_PAIR_SIZE 16381
315 struct type_pair_d *type_pair_cache;
318 /* Lookup the pair of types T1 and T2 in *VISITED_P. Insert a new
319 entry if none existed. */
321 static inline type_pair_t
322 lookup_type_pair (tree t1, tree t2)
324 unsigned int index;
325 unsigned int uid1, uid2;
327 if (TYPE_UID (t1) < TYPE_UID (t2))
329 uid1 = TYPE_UID (t1);
330 uid2 = TYPE_UID (t2);
332 else
334 uid1 = TYPE_UID (t2);
335 uid2 = TYPE_UID (t1);
337 gcc_checking_assert (uid1 != uid2);
339 /* iterative_hash_hashval_t imply an function calls.
340 We know that UIDS are in limited range. */
341 index = ((((unsigned HOST_WIDE_INT)uid1 << HOST_BITS_PER_WIDE_INT / 2) + uid2)
342 % GIMPLE_TYPE_PAIR_SIZE);
343 if (type_pair_cache [index].uid1 == uid1
344 && type_pair_cache [index].uid2 == uid2)
345 return &type_pair_cache[index];
347 type_pair_cache [index].uid1 = uid1;
348 type_pair_cache [index].uid2 = uid2;
349 type_pair_cache [index].same_p = -2;
351 return &type_pair_cache[index];
354 /* Per pointer state for the SCC finding. The on_sccstack flag
355 is not strictly required, it is true when there is no hash value
356 recorded for the type and false otherwise. But querying that
357 is slower. */
359 struct sccs
361 unsigned int dfsnum;
362 unsigned int low;
363 bool on_sccstack;
364 union {
365 hashval_t hash;
366 signed char same_p;
367 } u;
370 static unsigned int next_dfs_num;
371 static unsigned int gtc_next_dfs_num;
373 /* Return true if T1 and T2 have the same name. If FOR_COMPLETION_P is
374 true then if any type has no name return false, otherwise return
375 true if both types have no names. */
377 static bool
378 compare_type_names_p (tree t1, tree t2)
380 tree name1 = TYPE_NAME (t1);
381 tree name2 = TYPE_NAME (t2);
383 if ((name1 != NULL_TREE) != (name2 != NULL_TREE))
384 return false;
386 if (name1 == NULL_TREE)
387 return true;
389 /* Either both should be a TYPE_DECL or both an IDENTIFIER_NODE. */
390 if (TREE_CODE (name1) != TREE_CODE (name2))
391 return false;
393 if (TREE_CODE (name1) == TYPE_DECL)
394 name1 = DECL_NAME (name1);
395 gcc_checking_assert (!name1 || TREE_CODE (name1) == IDENTIFIER_NODE);
397 if (TREE_CODE (name2) == TYPE_DECL)
398 name2 = DECL_NAME (name2);
399 gcc_checking_assert (!name2 || TREE_CODE (name2) == IDENTIFIER_NODE);
401 /* Identifiers can be compared with pointer equality rather
402 than a string comparison. */
403 if (name1 == name2)
404 return true;
406 return false;
409 static bool
410 gimple_types_compatible_p_1 (tree, tree, type_pair_t,
411 vec<type_pair_t> *,
412 struct pointer_map_t *, struct obstack *);
414 /* DFS visit the edge from the callers type pair with state *STATE to
415 the pair T1, T2 while operating in FOR_MERGING_P mode.
416 Update the merging status if it is not part of the SCC containing the
417 callers pair and return it.
418 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
420 static bool
421 gtc_visit (tree t1, tree t2,
422 struct sccs *state,
423 vec<type_pair_t> *sccstack,
424 struct pointer_map_t *sccstate,
425 struct obstack *sccstate_obstack)
427 struct sccs *cstate = NULL;
428 type_pair_t p;
429 void **slot;
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 /* Can't be the same type if the types don't have the same code. */
440 if (TREE_CODE (t1) != TREE_CODE (t2))
441 return false;
443 /* Can't be the same type if they have different CV qualifiers. */
444 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
445 return false;
447 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
448 return false;
450 /* Void types and nullptr types are always the same. */
451 if (TREE_CODE (t1) == VOID_TYPE
452 || TREE_CODE (t1) == NULLPTR_TYPE)
453 return true;
455 /* Can't be the same type if they have different alignment or mode. */
456 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
457 || TYPE_MODE (t1) != TYPE_MODE (t2))
458 return false;
460 /* Do some simple checks before doing three hashtable queries. */
461 if (INTEGRAL_TYPE_P (t1)
462 || SCALAR_FLOAT_TYPE_P (t1)
463 || FIXED_POINT_TYPE_P (t1)
464 || TREE_CODE (t1) == VECTOR_TYPE
465 || TREE_CODE (t1) == COMPLEX_TYPE
466 || TREE_CODE (t1) == OFFSET_TYPE
467 || POINTER_TYPE_P (t1))
469 /* Can't be the same type if they have different sign or precision. */
470 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
471 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
472 return false;
474 if (TREE_CODE (t1) == INTEGER_TYPE
475 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
476 return false;
478 /* That's all we need to check for float and fixed-point types. */
479 if (SCALAR_FLOAT_TYPE_P (t1)
480 || FIXED_POINT_TYPE_P (t1))
481 return true;
483 /* For other types fall through to more complex checks. */
486 /* If the hash values of t1 and t2 are different the types can't
487 possibly be the same. This helps keeping the type-pair hashtable
488 small, only tracking comparisons for hash collisions. */
489 if (gimple_type_hash (t1) != gimple_type_hash (t2))
490 return false;
492 /* Allocate a new cache entry for this comparison. */
493 p = lookup_type_pair (t1, t2);
494 if (p->same_p == 0 || p->same_p == 1)
496 /* We have already decided whether T1 and T2 are the
497 same, return the cached result. */
498 return p->same_p == 1;
501 if ((slot = pointer_map_contains (sccstate, p)) != NULL)
502 cstate = (struct sccs *)*slot;
503 /* Not yet visited. DFS recurse. */
504 if (!cstate)
506 gimple_types_compatible_p_1 (t1, t2, p,
507 sccstack, sccstate, sccstate_obstack);
508 cstate = (struct sccs *)* pointer_map_contains (sccstate, p);
509 state->low = MIN (state->low, cstate->low);
511 /* If the type is still on the SCC stack adjust the parents low. */
512 if (cstate->dfsnum < state->dfsnum
513 && cstate->on_sccstack)
514 state->low = MIN (cstate->dfsnum, state->low);
516 /* Return the current lattice value. We start with an equality
517 assumption so types part of a SCC will be optimistically
518 treated equal unless proven otherwise. */
519 return cstate->u.same_p;
522 /* Worker for gimple_types_compatible.
523 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
525 static bool
526 gimple_types_compatible_p_1 (tree t1, tree t2, type_pair_t p,
527 vec<type_pair_t> *sccstack,
528 struct pointer_map_t *sccstate,
529 struct obstack *sccstate_obstack)
531 struct sccs *state;
533 gcc_assert (p->same_p == -2);
535 state = XOBNEW (sccstate_obstack, struct sccs);
536 *pointer_map_insert (sccstate, p) = state;
538 sccstack->safe_push (p);
539 state->dfsnum = gtc_next_dfs_num++;
540 state->low = state->dfsnum;
541 state->on_sccstack = true;
542 /* Start with an equality assumption. As we DFS recurse into child
543 SCCs this assumption may get revisited. */
544 state->u.same_p = 1;
546 /* The struct tags shall compare equal. */
547 if (!compare_type_names_p (t1, t2))
548 goto different_types;
550 /* The main variant of both types should compare equal. */
551 if (TYPE_MAIN_VARIANT (t1) != t1
552 || TYPE_MAIN_VARIANT (t2) != t2)
554 if (!gtc_visit (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2),
555 state, sccstack, sccstate, sccstate_obstack))
556 goto different_types;
559 /* We may not merge typedef types to the same type in different
560 contexts. */
561 if (TYPE_NAME (t1)
562 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
563 && DECL_CONTEXT (TYPE_NAME (t1))
564 && TYPE_P (DECL_CONTEXT (TYPE_NAME (t1))))
566 if (!gtc_visit (DECL_CONTEXT (TYPE_NAME (t1)),
567 DECL_CONTEXT (TYPE_NAME (t2)),
568 state, sccstack, sccstate, sccstate_obstack))
569 goto different_types;
572 /* If their attributes are not the same they can't be the same type. */
573 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
574 goto different_types;
576 /* Do type-specific comparisons. */
577 switch (TREE_CODE (t1))
579 case VECTOR_TYPE:
580 case COMPLEX_TYPE:
581 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
582 state, sccstack, sccstate, sccstate_obstack))
583 goto different_types;
584 goto same_types;
586 case ARRAY_TYPE:
587 /* Array types are the same if the element types are the same and
588 the number of elements are the same. */
589 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
590 state, sccstack, sccstate, sccstate_obstack)
591 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
592 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
593 goto different_types;
594 else
596 tree i1 = TYPE_DOMAIN (t1);
597 tree i2 = TYPE_DOMAIN (t2);
599 /* For an incomplete external array, the type domain can be
600 NULL_TREE. Check this condition also. */
601 if (i1 == NULL_TREE && i2 == NULL_TREE)
602 goto same_types;
603 else if (i1 == NULL_TREE || i2 == NULL_TREE)
604 goto different_types;
605 else
607 tree min1 = TYPE_MIN_VALUE (i1);
608 tree min2 = TYPE_MIN_VALUE (i2);
609 tree max1 = TYPE_MAX_VALUE (i1);
610 tree max2 = TYPE_MAX_VALUE (i2);
612 /* The minimum/maximum values have to be the same. */
613 if ((min1 == min2
614 || (min1 && min2
615 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
616 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
617 || operand_equal_p (min1, min2, 0))))
618 && (max1 == max2
619 || (max1 && max2
620 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
621 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
622 || operand_equal_p (max1, max2, 0)))))
623 goto same_types;
624 else
625 goto different_types;
629 case METHOD_TYPE:
630 /* Method types should belong to the same class. */
631 if (!gtc_visit (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2),
632 state, sccstack, sccstate, sccstate_obstack))
633 goto different_types;
635 /* Fallthru */
637 case FUNCTION_TYPE:
638 /* Function types are the same if the return type and arguments types
639 are the same. */
640 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
641 state, sccstack, sccstate, sccstate_obstack))
642 goto different_types;
644 if (!comp_type_attributes (t1, t2))
645 goto different_types;
647 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
648 goto same_types;
649 else
651 tree parms1, parms2;
653 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
654 parms1 && parms2;
655 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
657 if (!gtc_visit (TREE_VALUE (parms1), TREE_VALUE (parms2),
658 state, sccstack, sccstate, sccstate_obstack))
659 goto different_types;
662 if (parms1 || parms2)
663 goto different_types;
665 goto same_types;
668 case OFFSET_TYPE:
670 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
671 state, sccstack, sccstate, sccstate_obstack)
672 || !gtc_visit (TYPE_OFFSET_BASETYPE (t1),
673 TYPE_OFFSET_BASETYPE (t2),
674 state, sccstack, sccstate, sccstate_obstack))
675 goto different_types;
677 goto same_types;
680 case POINTER_TYPE:
681 case REFERENCE_TYPE:
683 /* If the two pointers have different ref-all attributes,
684 they can't be the same type. */
685 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
686 goto different_types;
688 /* Otherwise, pointer and reference types are the same if the
689 pointed-to types are the same. */
690 if (gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
691 state, sccstack, sccstate, sccstate_obstack))
692 goto same_types;
694 goto different_types;
697 case INTEGER_TYPE:
698 case BOOLEAN_TYPE:
700 tree min1 = TYPE_MIN_VALUE (t1);
701 tree max1 = TYPE_MAX_VALUE (t1);
702 tree min2 = TYPE_MIN_VALUE (t2);
703 tree max2 = TYPE_MAX_VALUE (t2);
704 bool min_equal_p = false;
705 bool max_equal_p = false;
707 /* If either type has a minimum value, the other type must
708 have the same. */
709 if (min1 == NULL_TREE && min2 == NULL_TREE)
710 min_equal_p = true;
711 else if (min1 && min2 && operand_equal_p (min1, min2, 0))
712 min_equal_p = true;
714 /* Likewise, if either type has a maximum value, the other
715 type must have the same. */
716 if (max1 == NULL_TREE && max2 == NULL_TREE)
717 max_equal_p = true;
718 else if (max1 && max2 && operand_equal_p (max1, max2, 0))
719 max_equal_p = true;
721 if (!min_equal_p || !max_equal_p)
722 goto different_types;
724 goto same_types;
727 case ENUMERAL_TYPE:
729 /* FIXME lto, we cannot check bounds on enumeral types because
730 different front ends will produce different values.
731 In C, enumeral types are integers, while in C++ each element
732 will have its own symbolic value. We should decide how enums
733 are to be represented in GIMPLE and have each front end lower
734 to that. */
735 tree v1, v2;
737 /* For enumeral types, all the values must be the same. */
738 if (TYPE_VALUES (t1) == TYPE_VALUES (t2))
739 goto same_types;
741 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
742 v1 && v2;
743 v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
745 tree c1 = TREE_VALUE (v1);
746 tree c2 = TREE_VALUE (v2);
748 if (TREE_CODE (c1) == CONST_DECL)
749 c1 = DECL_INITIAL (c1);
751 if (TREE_CODE (c2) == CONST_DECL)
752 c2 = DECL_INITIAL (c2);
754 if (tree_int_cst_equal (c1, c2) != 1)
755 goto different_types;
757 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
758 goto different_types;
761 /* If one enumeration has more values than the other, they
762 are not the same. */
763 if (v1 || v2)
764 goto different_types;
766 goto same_types;
769 case RECORD_TYPE:
770 case UNION_TYPE:
771 case QUAL_UNION_TYPE:
773 tree f1, f2;
775 /* For aggregate types, all the fields must be the same. */
776 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
777 f1 && f2;
778 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
780 /* Different field kinds are not compatible. */
781 if (TREE_CODE (f1) != TREE_CODE (f2))
782 goto different_types;
783 /* Field decls must have the same name and offset. */
784 if (TREE_CODE (f1) == FIELD_DECL
785 && (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
786 || !gimple_compare_field_offset (f1, f2)))
787 goto different_types;
788 /* All entities should have the same name and type. */
789 if (DECL_NAME (f1) != DECL_NAME (f2)
790 || !gtc_visit (TREE_TYPE (f1), TREE_TYPE (f2),
791 state, sccstack, sccstate, sccstate_obstack))
792 goto different_types;
795 /* If one aggregate has more fields than the other, they
796 are not the same. */
797 if (f1 || f2)
798 goto different_types;
800 goto same_types;
803 default:
804 gcc_unreachable ();
807 /* Common exit path for types that are not compatible. */
808 different_types:
809 state->u.same_p = 0;
810 goto pop;
812 /* Common exit path for types that are compatible. */
813 same_types:
814 gcc_assert (state->u.same_p == 1);
816 pop:
817 if (state->low == state->dfsnum)
819 type_pair_t x;
821 /* Pop off the SCC and set its cache values to the final
822 comparison result. */
825 struct sccs *cstate;
826 x = sccstack->pop ();
827 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
828 cstate->on_sccstack = false;
829 x->same_p = state->u.same_p;
831 while (x != p);
834 return state->u.same_p;
837 /* Return true iff T1 and T2 are structurally identical. When
838 FOR_MERGING_P is true the an incomplete type and a complete type
839 are considered different, otherwise they are considered compatible. */
841 static bool
842 gimple_types_compatible_p (tree t1, tree t2)
844 vec<type_pair_t> sccstack = vNULL;
845 struct pointer_map_t *sccstate;
846 struct obstack sccstate_obstack;
847 type_pair_t p = NULL;
848 bool res;
850 /* Before starting to set up the SCC machinery handle simple cases. */
852 /* Check first for the obvious case of pointer identity. */
853 if (t1 == t2)
854 return true;
856 /* Check that we have two types to compare. */
857 if (t1 == NULL_TREE || t2 == NULL_TREE)
858 return false;
860 /* Can't be the same type if the types don't have the same code. */
861 if (TREE_CODE (t1) != TREE_CODE (t2))
862 return false;
864 /* Can't be the same type if they have different CV qualifiers. */
865 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
866 return false;
868 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
869 return false;
871 /* Void types and nullptr types are always the same. */
872 if (TREE_CODE (t1) == VOID_TYPE
873 || TREE_CODE (t1) == NULLPTR_TYPE)
874 return true;
876 /* Can't be the same type if they have different alignment or mode. */
877 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
878 || TYPE_MODE (t1) != TYPE_MODE (t2))
879 return false;
881 /* Do some simple checks before doing three hashtable queries. */
882 if (INTEGRAL_TYPE_P (t1)
883 || SCALAR_FLOAT_TYPE_P (t1)
884 || FIXED_POINT_TYPE_P (t1)
885 || TREE_CODE (t1) == VECTOR_TYPE
886 || TREE_CODE (t1) == COMPLEX_TYPE
887 || TREE_CODE (t1) == OFFSET_TYPE
888 || POINTER_TYPE_P (t1))
890 /* Can't be the same type if they have different sign or precision. */
891 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
892 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
893 return false;
895 if (TREE_CODE (t1) == INTEGER_TYPE
896 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
897 return false;
899 /* That's all we need to check for float and fixed-point types. */
900 if (SCALAR_FLOAT_TYPE_P (t1)
901 || FIXED_POINT_TYPE_P (t1))
902 return true;
904 /* For other types fall through to more complex checks. */
907 /* If the hash values of t1 and t2 are different the types can't
908 possibly be the same. This helps keeping the type-pair hashtable
909 small, only tracking comparisons for hash collisions. */
910 if (gimple_type_hash (t1) != gimple_type_hash (t2))
911 return false;
913 /* If we've visited this type pair before (in the case of aggregates
914 with self-referential types), and we made a decision, return it. */
915 p = lookup_type_pair (t1, t2);
916 if (p->same_p == 0 || p->same_p == 1)
918 /* We have already decided whether T1 and T2 are the
919 same, return the cached result. */
920 return p->same_p == 1;
923 /* Now set up the SCC machinery for the comparison. */
924 gtc_next_dfs_num = 1;
925 sccstate = pointer_map_create ();
926 gcc_obstack_init (&sccstate_obstack);
927 res = gimple_types_compatible_p_1 (t1, t2, p,
928 &sccstack, sccstate, &sccstate_obstack);
929 sccstack.release ();
930 pointer_map_destroy (sccstate);
931 obstack_free (&sccstate_obstack, NULL);
933 return res;
936 static hashval_t
937 iterative_hash_gimple_type (tree, hashval_t, vec<tree> *,
938 struct pointer_map_t *, struct obstack *);
940 /* DFS visit the edge from the callers type with state *STATE to T.
941 Update the callers type hash V with the hash for T if it is not part
942 of the SCC containing the callers type and return it.
943 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
945 static hashval_t
946 visit (tree t, struct sccs *state, hashval_t v,
947 vec<tree> *sccstack,
948 struct pointer_map_t *sccstate,
949 struct obstack *sccstate_obstack)
951 struct sccs *cstate = NULL;
952 struct tree_int_map m;
953 void **slot;
955 /* If there is a hash value recorded for this type then it can't
956 possibly be part of our parent SCC. Simply mix in its hash. */
957 m.base.from = t;
958 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
959 && *slot)
960 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, v);
962 if ((slot = pointer_map_contains (sccstate, t)) != NULL)
963 cstate = (struct sccs *)*slot;
964 if (!cstate)
966 hashval_t tem;
967 /* Not yet visited. DFS recurse. */
968 tem = iterative_hash_gimple_type (t, v,
969 sccstack, sccstate, sccstate_obstack);
970 if (!cstate)
971 cstate = (struct sccs *)* pointer_map_contains (sccstate, t);
972 state->low = MIN (state->low, cstate->low);
973 /* If the type is no longer on the SCC stack and thus is not part
974 of the parents SCC mix in its hash value. Otherwise we will
975 ignore the type for hashing purposes and return the unaltered
976 hash value. */
977 if (!cstate->on_sccstack)
978 return tem;
980 if (cstate->dfsnum < state->dfsnum
981 && cstate->on_sccstack)
982 state->low = MIN (cstate->dfsnum, state->low);
984 /* We are part of our parents SCC, skip this type during hashing
985 and return the unaltered hash value. */
986 return v;
989 /* Hash NAME with the previous hash value V and return it. */
991 static hashval_t
992 iterative_hash_name (tree name, hashval_t v)
994 if (!name)
995 return v;
996 v = iterative_hash_hashval_t (TREE_CODE (name), v);
997 if (TREE_CODE (name) == TYPE_DECL)
998 name = DECL_NAME (name);
999 if (!name)
1000 return v;
1001 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1002 return iterative_hash_object (IDENTIFIER_HASH_VALUE (name), v);
1005 /* A type, hashvalue pair for sorting SCC members. */
1007 struct type_hash_pair {
1008 tree type;
1009 hashval_t hash;
1012 /* Compare two type, hashvalue pairs. */
1014 static int
1015 type_hash_pair_compare (const void *p1_, const void *p2_)
1017 const struct type_hash_pair *p1 = (const struct type_hash_pair *) p1_;
1018 const struct type_hash_pair *p2 = (const struct type_hash_pair *) p2_;
1019 if (p1->hash < p2->hash)
1020 return -1;
1021 else if (p1->hash > p2->hash)
1022 return 1;
1023 return 0;
1026 /* Returning a hash value for gimple type TYPE combined with VAL.
1027 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done.
1029 To hash a type we end up hashing in types that are reachable.
1030 Through pointers we can end up with cycles which messes up the
1031 required property that we need to compute the same hash value
1032 for structurally equivalent types. To avoid this we have to
1033 hash all types in a cycle (the SCC) in a commutative way. The
1034 easiest way is to not mix in the hashes of the SCC members at
1035 all. To make this work we have to delay setting the hash
1036 values of the SCC until it is complete. */
1038 static hashval_t
1039 iterative_hash_gimple_type (tree type, hashval_t val,
1040 vec<tree> *sccstack,
1041 struct pointer_map_t *sccstate,
1042 struct obstack *sccstate_obstack)
1044 hashval_t v;
1045 void **slot;
1046 struct sccs *state;
1048 /* Not visited during this DFS walk. */
1049 gcc_checking_assert (!pointer_map_contains (sccstate, type));
1050 state = XOBNEW (sccstate_obstack, struct sccs);
1051 *pointer_map_insert (sccstate, type) = state;
1053 sccstack->safe_push (type);
1054 state->dfsnum = next_dfs_num++;
1055 state->low = state->dfsnum;
1056 state->on_sccstack = true;
1058 /* Combine a few common features of types so that types are grouped into
1059 smaller sets; when searching for existing matching types to merge,
1060 only existing types having the same features as the new type will be
1061 checked. */
1062 v = iterative_hash_name (TYPE_NAME (type), 0);
1063 if (TYPE_NAME (type)
1064 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1065 && DECL_CONTEXT (TYPE_NAME (type))
1066 && TYPE_P (DECL_CONTEXT (TYPE_NAME (type))))
1067 v = visit (DECL_CONTEXT (TYPE_NAME (type)), state, v,
1068 sccstack, sccstate, sccstate_obstack);
1070 /* Factor in the variant structure. */
1071 if (TYPE_MAIN_VARIANT (type) != type)
1072 v = visit (TYPE_MAIN_VARIANT (type), state, v,
1073 sccstack, sccstate, sccstate_obstack);
1075 v = iterative_hash_hashval_t (TREE_CODE (type), v);
1076 v = iterative_hash_hashval_t (TYPE_QUALS (type), v);
1077 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
1079 /* Do not hash the types size as this will cause differences in
1080 hash values for the complete vs. the incomplete type variant. */
1082 /* Incorporate common features of numerical types. */
1083 if (INTEGRAL_TYPE_P (type)
1084 || SCALAR_FLOAT_TYPE_P (type)
1085 || FIXED_POINT_TYPE_P (type))
1087 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
1088 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
1089 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
1092 /* For pointer and reference types, fold in information about the type
1093 pointed to. */
1094 if (POINTER_TYPE_P (type))
1095 v = visit (TREE_TYPE (type), state, v,
1096 sccstack, sccstate, sccstate_obstack);
1098 /* For integer types hash the types min/max values and the string flag. */
1099 if (TREE_CODE (type) == INTEGER_TYPE)
1101 /* OMP lowering can introduce error_mark_node in place of
1102 random local decls in types. */
1103 if (TYPE_MIN_VALUE (type) != error_mark_node)
1104 v = iterative_hash_expr (TYPE_MIN_VALUE (type), v);
1105 if (TYPE_MAX_VALUE (type) != error_mark_node)
1106 v = iterative_hash_expr (TYPE_MAX_VALUE (type), v);
1107 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
1110 /* For array types hash the domain and the string flag. */
1111 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
1113 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
1114 v = visit (TYPE_DOMAIN (type), state, v,
1115 sccstack, sccstate, sccstate_obstack);
1118 /* Recurse for aggregates with a single element type. */
1119 if (TREE_CODE (type) == ARRAY_TYPE
1120 || TREE_CODE (type) == COMPLEX_TYPE
1121 || TREE_CODE (type) == VECTOR_TYPE)
1122 v = visit (TREE_TYPE (type), state, v,
1123 sccstack, sccstate, sccstate_obstack);
1125 /* Incorporate function return and argument types. */
1126 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
1128 unsigned na;
1129 tree p;
1131 /* For method types also incorporate their parent class. */
1132 if (TREE_CODE (type) == METHOD_TYPE)
1133 v = visit (TYPE_METHOD_BASETYPE (type), state, v,
1134 sccstack, sccstate, sccstate_obstack);
1136 /* Check result and argument types. */
1137 v = visit (TREE_TYPE (type), state, v,
1138 sccstack, sccstate, sccstate_obstack);
1139 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
1141 v = visit (TREE_VALUE (p), state, v,
1142 sccstack, sccstate, sccstate_obstack);
1143 na++;
1146 v = iterative_hash_hashval_t (na, v);
1149 if (RECORD_OR_UNION_TYPE_P (type))
1151 unsigned nf;
1152 tree f;
1154 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
1156 v = iterative_hash_name (DECL_NAME (f), v);
1157 v = visit (TREE_TYPE (f), state, v,
1158 sccstack, sccstate, sccstate_obstack);
1159 nf++;
1162 v = iterative_hash_hashval_t (nf, v);
1165 /* Record hash for us. */
1166 state->u.hash = v;
1168 /* See if we found an SCC. */
1169 if (state->low == state->dfsnum)
1171 tree x;
1172 struct tree_int_map *m;
1174 /* Pop off the SCC and set its hash values. */
1175 x = sccstack->pop ();
1176 /* Optimize SCC size one. */
1177 if (x == type)
1179 state->on_sccstack = false;
1180 m = ggc_alloc_cleared_tree_int_map ();
1181 m->base.from = x;
1182 m->to = v;
1183 slot = htab_find_slot (type_hash_cache, m, INSERT);
1184 gcc_assert (!*slot);
1185 *slot = (void *) m;
1187 else
1189 struct sccs *cstate;
1190 unsigned first, i, size, j;
1191 struct type_hash_pair *pairs;
1192 /* Pop off the SCC and build an array of type, hash pairs. */
1193 first = sccstack->length () - 1;
1194 while ((*sccstack)[first] != type)
1195 --first;
1196 size = sccstack->length () - first + 1;
1197 pairs = XALLOCAVEC (struct type_hash_pair, size);
1198 i = 0;
1199 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
1200 cstate->on_sccstack = false;
1201 pairs[i].type = x;
1202 pairs[i].hash = cstate->u.hash;
1205 x = sccstack->pop ();
1206 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
1207 cstate->on_sccstack = false;
1208 ++i;
1209 pairs[i].type = x;
1210 pairs[i].hash = cstate->u.hash;
1212 while (x != type);
1213 gcc_assert (i + 1 == size);
1214 /* Sort the arrays of type, hash pairs so that when we mix in
1215 all members of the SCC the hash value becomes independent on
1216 the order we visited the SCC. Disregard hashes equal to
1217 the hash of the type we mix into because we cannot guarantee
1218 a stable sort for those across different TUs. */
1219 qsort (pairs, size, sizeof (struct type_hash_pair),
1220 type_hash_pair_compare);
1221 for (i = 0; i < size; ++i)
1223 hashval_t hash;
1224 m = ggc_alloc_cleared_tree_int_map ();
1225 m->base.from = pairs[i].type;
1226 hash = pairs[i].hash;
1227 /* Skip same hashes. */
1228 for (j = i + 1; j < size && pairs[j].hash == pairs[i].hash; ++j)
1230 for (; j < size; ++j)
1231 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
1232 for (j = 0; pairs[j].hash != pairs[i].hash; ++j)
1233 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
1234 m->to = hash;
1235 if (pairs[i].type == type)
1236 v = hash;
1237 slot = htab_find_slot (type_hash_cache, m, INSERT);
1238 gcc_assert (!*slot);
1239 *slot = (void *) m;
1244 return iterative_hash_hashval_t (v, val);
1247 /* Returns a hash value for P (assumed to be a type). The hash value
1248 is computed using some distinguishing features of the type. Note
1249 that we cannot use pointer hashing here as we may be dealing with
1250 two distinct instances of the same type.
1252 This function should produce the same hash value for two compatible
1253 types according to gimple_types_compatible_p. */
1255 static hashval_t
1256 gimple_type_hash (const void *p)
1258 const_tree t = (const_tree) p;
1259 vec<tree> sccstack = vNULL;
1260 struct pointer_map_t *sccstate;
1261 struct obstack sccstate_obstack;
1262 hashval_t val;
1263 void **slot;
1264 struct tree_int_map m;
1266 m.base.from = CONST_CAST_TREE (t);
1267 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
1268 && *slot)
1269 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, 0);
1271 /* Perform a DFS walk and pre-hash all reachable types. */
1272 next_dfs_num = 1;
1273 sccstate = pointer_map_create ();
1274 gcc_obstack_init (&sccstate_obstack);
1275 val = iterative_hash_gimple_type (CONST_CAST_TREE (t), 0,
1276 &sccstack, sccstate, &sccstate_obstack);
1277 sccstack.release ();
1278 pointer_map_destroy (sccstate);
1279 obstack_free (&sccstate_obstack, NULL);
1281 return val;
1284 /* Returns nonzero if P1 and P2 are equal. */
1286 static int
1287 gimple_type_eq (const void *p1, const void *p2)
1289 const_tree t1 = (const_tree) p1;
1290 const_tree t2 = (const_tree) p2;
1291 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
1292 CONST_CAST_TREE (t2));
1295 /* Register type T in the global type table gimple_types. */
1297 static tree
1298 gimple_register_type (tree t)
1300 void **slot;
1302 /* See if we already have an equivalent type registered. */
1303 slot = htab_find_slot (gimple_types, t, INSERT);
1304 if (*slot
1305 && *(tree *)slot != t)
1306 return (tree) *((tree *) slot);
1308 /* If not, insert it to the cache and the hash. */
1309 *slot = (void *) t;
1310 return t;
1313 /* End of old merging code. */
1317 /* A hashtable of trees that potentially refer to variables or functions
1318 that must be replaced with their prevailing variant. */
1319 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
1320 tree_with_vars;
1322 /* Remember that T is a tree that (potentially) refers to a variable
1323 or function decl that may be replaced with its prevailing variant. */
1324 static void
1325 remember_with_vars (tree t)
1327 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
1330 #define MAYBE_REMEMBER_WITH_VARS(tt) \
1331 do \
1333 if (tt) \
1335 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
1336 remember_with_vars (t); \
1338 } while (0)
1340 /* Fix up fields of a tree_typed T. */
1342 static void
1343 maybe_remember_with_vars_typed (tree t)
1345 MAYBE_REMEMBER_WITH_VARS (TREE_TYPE (t));
1348 /* Fix up fields of a tree_common T. */
1350 static void
1351 maybe_remember_with_vars_common (tree t)
1353 maybe_remember_with_vars_typed (t);
1354 MAYBE_REMEMBER_WITH_VARS (TREE_CHAIN (t));
1357 /* Fix up fields of a decl_minimal T. */
1359 static void
1360 maybe_remember_with_vars_decl_minimal (tree t)
1362 maybe_remember_with_vars_common (t);
1363 MAYBE_REMEMBER_WITH_VARS (DECL_NAME (t));
1364 MAYBE_REMEMBER_WITH_VARS (DECL_CONTEXT (t));
1367 /* Fix up fields of a decl_common T. */
1369 static void
1370 maybe_remember_with_vars_decl_common (tree t)
1372 maybe_remember_with_vars_decl_minimal (t);
1373 MAYBE_REMEMBER_WITH_VARS (DECL_SIZE (t));
1374 MAYBE_REMEMBER_WITH_VARS (DECL_SIZE_UNIT (t));
1375 MAYBE_REMEMBER_WITH_VARS (DECL_INITIAL (t));
1376 MAYBE_REMEMBER_WITH_VARS (DECL_ATTRIBUTES (t));
1377 MAYBE_REMEMBER_WITH_VARS (DECL_ABSTRACT_ORIGIN (t));
1380 /* Fix up fields of a decl_with_vis T. */
1382 static void
1383 maybe_remember_with_vars_decl_with_vis (tree t)
1385 maybe_remember_with_vars_decl_common (t);
1387 /* Accessor macro has side-effects, use field-name here. */
1388 MAYBE_REMEMBER_WITH_VARS (t->decl_with_vis.assembler_name);
1389 MAYBE_REMEMBER_WITH_VARS (DECL_SECTION_NAME (t));
1392 /* Fix up fields of a decl_non_common T. */
1394 static void
1395 maybe_remember_with_vars_decl_non_common (tree t)
1397 maybe_remember_with_vars_decl_with_vis (t);
1398 MAYBE_REMEMBER_WITH_VARS (DECL_ARGUMENT_FLD (t));
1399 MAYBE_REMEMBER_WITH_VARS (DECL_RESULT_FLD (t));
1400 MAYBE_REMEMBER_WITH_VARS (DECL_VINDEX (t));
1403 /* Fix up fields of a decl_non_common T. */
1405 static void
1406 maybe_remember_with_vars_function (tree t)
1408 maybe_remember_with_vars_decl_non_common (t);
1409 MAYBE_REMEMBER_WITH_VARS (DECL_FUNCTION_PERSONALITY (t));
1412 /* Fix up fields of a field_decl T. */
1414 static void
1415 maybe_remember_with_vars_field_decl (tree t)
1417 maybe_remember_with_vars_decl_common (t);
1418 MAYBE_REMEMBER_WITH_VARS (DECL_FIELD_OFFSET (t));
1419 MAYBE_REMEMBER_WITH_VARS (DECL_BIT_FIELD_TYPE (t));
1420 MAYBE_REMEMBER_WITH_VARS (DECL_QUALIFIER (t));
1421 MAYBE_REMEMBER_WITH_VARS (DECL_FIELD_BIT_OFFSET (t));
1422 MAYBE_REMEMBER_WITH_VARS (DECL_FCONTEXT (t));
1425 /* Fix up fields of a type T. */
1427 static void
1428 maybe_remember_with_vars_type (tree t)
1430 maybe_remember_with_vars_common (t);
1431 MAYBE_REMEMBER_WITH_VARS (TYPE_CACHED_VALUES (t));
1432 MAYBE_REMEMBER_WITH_VARS (TYPE_SIZE (t));
1433 MAYBE_REMEMBER_WITH_VARS (TYPE_SIZE_UNIT (t));
1434 MAYBE_REMEMBER_WITH_VARS (TYPE_ATTRIBUTES (t));
1435 MAYBE_REMEMBER_WITH_VARS (TYPE_NAME (t));
1437 /* Accessors are for derived node types only. */
1438 if (!POINTER_TYPE_P (t))
1439 MAYBE_REMEMBER_WITH_VARS (TYPE_MINVAL (t));
1440 MAYBE_REMEMBER_WITH_VARS (TYPE_MAXVAL (t));
1442 /* Accessor is for derived node types only. */
1443 MAYBE_REMEMBER_WITH_VARS (t->type_non_common.binfo);
1445 MAYBE_REMEMBER_WITH_VARS (TYPE_CONTEXT (t));
1448 /* Fix up fields of a BINFO T. */
1450 static void
1451 maybe_remember_with_vars_binfo (tree t)
1453 unsigned HOST_WIDE_INT i, n;
1455 maybe_remember_with_vars_common (t);
1456 MAYBE_REMEMBER_WITH_VARS (BINFO_VTABLE (t));
1457 MAYBE_REMEMBER_WITH_VARS (BINFO_OFFSET (t));
1458 MAYBE_REMEMBER_WITH_VARS (BINFO_VIRTUALS (t));
1459 MAYBE_REMEMBER_WITH_VARS (BINFO_VPTR_FIELD (t));
1460 n = vec_safe_length (BINFO_BASE_ACCESSES (t));
1461 for (i = 0; i < n; i++)
1462 MAYBE_REMEMBER_WITH_VARS (BINFO_BASE_ACCESS (t, i));
1463 MAYBE_REMEMBER_WITH_VARS (BINFO_INHERITANCE_CHAIN (t));
1464 MAYBE_REMEMBER_WITH_VARS (BINFO_SUBVTT_INDEX (t));
1465 MAYBE_REMEMBER_WITH_VARS (BINFO_VPTR_INDEX (t));
1466 n = BINFO_N_BASE_BINFOS (t);
1467 for (i = 0; i < n; i++)
1468 MAYBE_REMEMBER_WITH_VARS (BINFO_BASE_BINFO (t, i));
1471 /* Fix up fields of a CONSTRUCTOR T. */
1473 static void
1474 maybe_remember_with_vars_constructor (tree t)
1476 unsigned HOST_WIDE_INT idx;
1477 constructor_elt *ce;
1479 maybe_remember_with_vars_typed (t);
1481 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
1483 MAYBE_REMEMBER_WITH_VARS (ce->index);
1484 MAYBE_REMEMBER_WITH_VARS (ce->value);
1488 /* Fix up fields of an expression tree T. */
1490 static void
1491 maybe_remember_with_vars_expr (tree t)
1493 int i;
1494 maybe_remember_with_vars_typed (t);
1495 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
1496 MAYBE_REMEMBER_WITH_VARS (TREE_OPERAND (t, i));
1499 /* Given a tree T fixup fields of T by replacing types with their merged
1500 variant and other entities by an equal entity from an earlier compilation
1501 unit, or an entity being canonical in a different way. This includes
1502 for instance integer or string constants. */
1504 static void
1505 maybe_remember_with_vars (tree t)
1507 switch (TREE_CODE (t))
1509 case IDENTIFIER_NODE:
1510 break;
1512 case TREE_LIST:
1513 MAYBE_REMEMBER_WITH_VARS (TREE_VALUE (t));
1514 MAYBE_REMEMBER_WITH_VARS (TREE_PURPOSE (t));
1515 MAYBE_REMEMBER_WITH_VARS (TREE_CHAIN (t));
1516 break;
1518 case FIELD_DECL:
1519 maybe_remember_with_vars_field_decl (t);
1520 break;
1522 case LABEL_DECL:
1523 case CONST_DECL:
1524 case PARM_DECL:
1525 case RESULT_DECL:
1526 case IMPORTED_DECL:
1527 maybe_remember_with_vars_decl_common (t);
1528 break;
1530 case VAR_DECL:
1531 maybe_remember_with_vars_decl_with_vis (t);
1532 break;
1534 case TYPE_DECL:
1535 maybe_remember_with_vars_decl_non_common (t);
1536 break;
1538 case FUNCTION_DECL:
1539 maybe_remember_with_vars_function (t);
1540 break;
1542 case TREE_BINFO:
1543 maybe_remember_with_vars_binfo (t);
1544 break;
1546 case PLACEHOLDER_EXPR:
1547 maybe_remember_with_vars_common (t);
1548 break;
1550 case BLOCK:
1551 case TRANSLATION_UNIT_DECL:
1552 case OPTIMIZATION_NODE:
1553 case TARGET_OPTION_NODE:
1554 break;
1556 case CONSTRUCTOR:
1557 maybe_remember_with_vars_constructor (t);
1558 break;
1560 default:
1561 if (TYPE_P (t))
1562 maybe_remember_with_vars_type (t);
1563 else if (CONSTANT_CLASS_P (t))
1564 MAYBE_REMEMBER_WITH_VARS (TREE_TYPE (t));
1565 else if (EXPR_P (t))
1566 maybe_remember_with_vars_expr (t);
1567 else
1568 remember_with_vars (t);
1573 /* Return the resolution for the decl with index INDEX from DATA_IN. */
1575 static enum ld_plugin_symbol_resolution
1576 get_resolution (struct data_in *data_in, unsigned index)
1578 if (data_in->globals_resolution.exists ())
1580 ld_plugin_symbol_resolution_t ret;
1581 /* We can have references to not emitted functions in
1582 DECL_FUNCTION_PERSONALITY at least. So we can and have
1583 to indeed return LDPR_UNKNOWN in some cases. */
1584 if (data_in->globals_resolution.length () <= index)
1585 return LDPR_UNKNOWN;
1586 ret = data_in->globals_resolution[index];
1587 return ret;
1589 else
1590 /* Delay resolution finding until decl merging. */
1591 return LDPR_UNKNOWN;
1594 /* We need to record resolutions until symbol table is read. */
1595 static void
1596 register_resolution (struct lto_file_decl_data *file_data, tree decl,
1597 enum ld_plugin_symbol_resolution resolution)
1599 if (resolution == LDPR_UNKNOWN)
1600 return;
1601 if (!file_data->resolution_map)
1602 file_data->resolution_map = pointer_map_create ();
1603 *pointer_map_insert (file_data->resolution_map, decl) = (void *)(size_t)resolution;
1606 /* Register DECL with the global symbol table and change its
1607 name if necessary to avoid name clashes for static globals across
1608 different files. */
1610 static void
1611 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl,
1612 unsigned ix)
1614 tree context;
1616 /* Variable has file scope, not local. */
1617 if (!TREE_PUBLIC (decl)
1618 && !((context = decl_function_context (decl))
1619 && auto_var_in_fn_p (decl, context)))
1620 rest_of_decl_compilation (decl, 1, 0);
1622 /* If this variable has already been declared, queue the
1623 declaration for merging. */
1624 if (TREE_PUBLIC (decl))
1625 register_resolution (data_in->file_data,
1626 decl, get_resolution (data_in, ix));
1630 /* Register DECL with the global symbol table and change its
1631 name if necessary to avoid name clashes for static globals across
1632 different files. DATA_IN contains descriptors and tables for the
1633 file being read. */
1635 static void
1636 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl,
1637 unsigned ix)
1639 /* If this variable has already been declared, queue the
1640 declaration for merging. */
1641 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
1642 register_resolution (data_in->file_data,
1643 decl, get_resolution (data_in, ix));
1647 /* For the type T re-materialize it in the type variant list and
1648 the pointer/reference-to chains. */
1650 static void
1651 lto_fixup_prevailing_type (tree t)
1653 /* The following re-creates proper variant lists while fixing up
1654 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1655 variant list state before fixup is broken. */
1657 /* If we are not our own variant leader link us into our new leaders
1658 variant list. */
1659 if (TYPE_MAIN_VARIANT (t) != t)
1661 tree mv = TYPE_MAIN_VARIANT (t);
1662 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1663 TYPE_NEXT_VARIANT (mv) = t;
1666 /* The following reconstructs the pointer chains
1667 of the new pointed-to type if we are a main variant. We do
1668 not stream those so they are broken before fixup. */
1669 if (TREE_CODE (t) == POINTER_TYPE
1670 && TYPE_MAIN_VARIANT (t) == t)
1672 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1673 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1675 else if (TREE_CODE (t) == REFERENCE_TYPE
1676 && TYPE_MAIN_VARIANT (t) == t)
1678 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1679 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1684 /* We keep prevailing tree SCCs in a hashtable with manual collision
1685 handling (in case all hashes compare the same) and keep the colliding
1686 entries in the tree_scc->next chain. */
1688 struct tree_scc
1690 tree_scc *next;
1691 /* Hash of the whole SCC. */
1692 hashval_t hash;
1693 /* Number of trees in the SCC. */
1694 unsigned len;
1695 /* Number of possible entries into the SCC (tree nodes [0..entry_len-1]
1696 which share the same individual tree hash). */
1697 unsigned entry_len;
1698 /* The members of the SCC.
1699 We only need to remember the first entry node candidate for prevailing
1700 SCCs (but of course have access to all entries for SCCs we are
1701 processing).
1702 ??? For prevailing SCCs we really only need hash and the first
1703 entry candidate, but that's too awkward to implement. */
1704 tree entries[1];
1707 struct tree_scc_hasher : typed_noop_remove <tree_scc>
1709 typedef tree_scc value_type;
1710 typedef tree_scc compare_type;
1711 static inline hashval_t hash (const value_type *);
1712 static inline bool equal (const value_type *, const compare_type *);
1715 hashval_t
1716 tree_scc_hasher::hash (const value_type *scc)
1718 return scc->hash;
1721 bool
1722 tree_scc_hasher::equal (const value_type *scc1, const compare_type *scc2)
1724 if (scc1->hash != scc2->hash
1725 || scc1->len != scc2->len
1726 || scc1->entry_len != scc2->entry_len)
1727 return false;
1728 return true;
1731 static hash_table <tree_scc_hasher> tree_scc_hash;
1732 static struct obstack tree_scc_hash_obstack;
1734 static unsigned long num_merged_types;
1735 static unsigned long num_prevailing_types;
1736 static unsigned long num_not_merged_types;
1737 static unsigned long num_not_merged_types_in_same_scc;
1738 static unsigned long num_not_merged_types_trees;
1739 static unsigned long num_not_merged_types_in_same_scc_trees;
1740 static unsigned long num_type_scc_trees;
1741 static unsigned long total_scc_size;
1742 static unsigned long num_sccs_read;
1743 static unsigned long total_scc_size_merged;
1744 static unsigned long num_sccs_merged;
1745 static unsigned long num_scc_compares;
1746 static unsigned long num_scc_compare_collisions;
1749 /* Compare the two entries T1 and T2 of two SCCs that are possibly equal,
1750 recursing through in-SCC tree edges. Returns true if the SCCs entered
1751 through T1 and T2 are equal and fills in *MAP with the pairs of
1752 SCC entries we visited, starting with (*MAP)[0] = T1 and (*MAP)[1] = T2. */
1754 static bool
1755 compare_tree_sccs_1 (tree t1, tree t2, tree **map)
1757 enum tree_code code;
1759 /* Mark already visited nodes. */
1760 TREE_ASM_WRITTEN (t2) = 1;
1762 /* Push the pair onto map. */
1763 (*map)[0] = t1;
1764 (*map)[1] = t2;
1765 *map = *map + 2;
1767 /* Compare value-fields. */
1768 #define compare_values(X) \
1769 do { \
1770 if (X(t1) != X(t2)) \
1771 return false; \
1772 } while (0)
1774 compare_values (TREE_CODE);
1775 code = TREE_CODE (t1);
1777 if (!TYPE_P (t1))
1779 compare_values (TREE_SIDE_EFFECTS);
1780 compare_values (TREE_CONSTANT);
1781 compare_values (TREE_READONLY);
1782 compare_values (TREE_PUBLIC);
1784 compare_values (TREE_ADDRESSABLE);
1785 compare_values (TREE_THIS_VOLATILE);
1786 if (DECL_P (t1))
1787 compare_values (DECL_UNSIGNED);
1788 else if (TYPE_P (t1))
1789 compare_values (TYPE_UNSIGNED);
1790 if (TYPE_P (t1))
1791 compare_values (TYPE_ARTIFICIAL);
1792 else
1793 compare_values (TREE_NO_WARNING);
1794 compare_values (TREE_NOTHROW);
1795 compare_values (TREE_STATIC);
1796 if (code != TREE_BINFO)
1797 compare_values (TREE_PRIVATE);
1798 compare_values (TREE_PROTECTED);
1799 compare_values (TREE_DEPRECATED);
1800 if (TYPE_P (t1))
1802 compare_values (TYPE_SATURATING);
1803 compare_values (TYPE_ADDR_SPACE);
1805 else if (code == SSA_NAME)
1806 compare_values (SSA_NAME_IS_DEFAULT_DEF);
1808 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1810 compare_values (TREE_INT_CST_LOW);
1811 compare_values (TREE_INT_CST_HIGH);
1814 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1816 /* ??? No suitable compare routine available. */
1817 REAL_VALUE_TYPE r1 = TREE_REAL_CST (t1);
1818 REAL_VALUE_TYPE r2 = TREE_REAL_CST (t2);
1819 if (r1.cl != r2.cl
1820 || r1.decimal != r2.decimal
1821 || r1.sign != r2.sign
1822 || r1.signalling != r2.signalling
1823 || r1.canonical != r2.canonical
1824 || r1.uexp != r2.uexp)
1825 return false;
1826 for (unsigned i = 0; i < SIGSZ; ++i)
1827 if (r1.sig[i] != r2.sig[i])
1828 return false;
1831 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1832 if (!fixed_compare (EQ_EXPR,
1833 TREE_FIXED_CST_PTR (t1), TREE_FIXED_CST_PTR (t2)))
1834 return false;
1837 /* We don't want to compare locations, so there is nothing do compare
1838 for TS_DECL_MINIMAL. */
1840 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1842 compare_values (DECL_MODE);
1843 compare_values (DECL_NONLOCAL);
1844 compare_values (DECL_VIRTUAL_P);
1845 compare_values (DECL_IGNORED_P);
1846 compare_values (DECL_ABSTRACT);
1847 compare_values (DECL_ARTIFICIAL);
1848 compare_values (DECL_USER_ALIGN);
1849 compare_values (DECL_PRESERVE_P);
1850 compare_values (DECL_EXTERNAL);
1851 compare_values (DECL_GIMPLE_REG_P);
1852 compare_values (DECL_ALIGN);
1853 if (code == LABEL_DECL)
1855 compare_values (DECL_ERROR_ISSUED);
1856 compare_values (EH_LANDING_PAD_NR);
1857 compare_values (LABEL_DECL_UID);
1859 else if (code == FIELD_DECL)
1861 compare_values (DECL_PACKED);
1862 compare_values (DECL_NONADDRESSABLE_P);
1863 compare_values (DECL_OFFSET_ALIGN);
1865 else if (code == VAR_DECL)
1867 compare_values (DECL_HAS_DEBUG_EXPR_P);
1868 compare_values (DECL_NONLOCAL_FRAME);
1870 if (code == RESULT_DECL
1871 || code == PARM_DECL
1872 || code == VAR_DECL)
1874 compare_values (DECL_BY_REFERENCE);
1875 if (code == VAR_DECL
1876 || code == PARM_DECL)
1877 compare_values (DECL_HAS_VALUE_EXPR_P);
1881 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1882 compare_values (DECL_REGISTER);
1884 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1886 compare_values (DECL_DEFER_OUTPUT);
1887 compare_values (DECL_COMMON);
1888 compare_values (DECL_DLLIMPORT_P);
1889 compare_values (DECL_WEAK);
1890 compare_values (DECL_SEEN_IN_BIND_EXPR_P);
1891 compare_values (DECL_COMDAT);
1892 compare_values (DECL_VISIBILITY);
1893 compare_values (DECL_VISIBILITY_SPECIFIED);
1894 if (code == VAR_DECL)
1896 compare_values (DECL_HARD_REGISTER);
1897 compare_values (DECL_IN_TEXT_SECTION);
1898 compare_values (DECL_IN_CONSTANT_POOL);
1899 compare_values (DECL_TLS_MODEL);
1901 if (VAR_OR_FUNCTION_DECL_P (t1))
1902 compare_values (DECL_INIT_PRIORITY);
1905 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1907 compare_values (DECL_BUILT_IN_CLASS);
1908 compare_values (DECL_STATIC_CONSTRUCTOR);
1909 compare_values (DECL_STATIC_DESTRUCTOR);
1910 compare_values (DECL_UNINLINABLE);
1911 compare_values (DECL_POSSIBLY_INLINED);
1912 compare_values (DECL_IS_NOVOPS);
1913 compare_values (DECL_IS_RETURNS_TWICE);
1914 compare_values (DECL_IS_MALLOC);
1915 compare_values (DECL_IS_OPERATOR_NEW);
1916 compare_values (DECL_DECLARED_INLINE_P);
1917 compare_values (DECL_STATIC_CHAIN);
1918 compare_values (DECL_NO_INLINE_WARNING_P);
1919 compare_values (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT);
1920 compare_values (DECL_NO_LIMIT_STACK);
1921 compare_values (DECL_DISREGARD_INLINE_LIMITS);
1922 compare_values (DECL_PURE_P);
1923 compare_values (DECL_LOOPING_CONST_OR_PURE_P);
1924 if (DECL_BUILT_IN_CLASS (t1) != NOT_BUILT_IN)
1925 compare_values (DECL_FUNCTION_CODE);
1926 if (DECL_STATIC_DESTRUCTOR (t1))
1927 compare_values (DECL_FINI_PRIORITY);
1930 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1932 compare_values (TYPE_MODE);
1933 compare_values (TYPE_STRING_FLAG);
1934 compare_values (TYPE_NO_FORCE_BLK);
1935 compare_values (TYPE_NEEDS_CONSTRUCTING);
1936 if (RECORD_OR_UNION_TYPE_P (t1))
1937 compare_values (TYPE_TRANSPARENT_AGGR);
1938 else if (code == ARRAY_TYPE)
1939 compare_values (TYPE_NONALIASED_COMPONENT);
1940 compare_values (TYPE_PACKED);
1941 compare_values (TYPE_RESTRICT);
1942 compare_values (TYPE_USER_ALIGN);
1943 compare_values (TYPE_READONLY);
1944 compare_values (TYPE_PRECISION);
1945 compare_values (TYPE_ALIGN);
1946 compare_values (TYPE_ALIAS_SET);
1949 /* We don't want to compare locations, so there is nothing do compare
1950 for TS_EXP. */
1952 /* BLOCKs are function local and we don't merge anything there, so
1953 simply refuse to merge. */
1954 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1955 return false;
1957 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1958 if (strcmp (TRANSLATION_UNIT_LANGUAGE (t1),
1959 TRANSLATION_UNIT_LANGUAGE (t2)) != 0)
1960 return false;
1962 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
1963 if (memcmp (TREE_TARGET_OPTION (t1), TREE_TARGET_OPTION (t2),
1964 sizeof (struct cl_target_option)) != 0)
1965 return false;
1967 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1968 if (memcmp (TREE_OPTIMIZATION (t1), TREE_OPTIMIZATION (t2),
1969 sizeof (struct cl_optimization)) != 0)
1970 return false;
1972 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1973 if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
1974 != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
1975 return false;
1977 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1978 compare_values (CONSTRUCTOR_NELTS);
1980 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1981 if (IDENTIFIER_LENGTH (t1) != IDENTIFIER_LENGTH (t2)
1982 || memcmp (IDENTIFIER_POINTER (t1), IDENTIFIER_POINTER (t2),
1983 IDENTIFIER_LENGTH (t1)) != 0)
1984 return false;
1986 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1987 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)
1988 || memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1989 TREE_STRING_LENGTH (t1)) != 0)
1990 return false;
1992 #undef compare_values
1995 /* Compare pointer fields. */
1997 /* Recurse. Search & Replaced from DFS_write_tree_body.
1998 Folding the early checks into the compare_tree_edges recursion
1999 macro makes debugging way quicker as you are able to break on
2000 compare_tree_sccs_1 and simply finish until a call returns false
2001 to spot the SCC members with the difference. */
2002 #define compare_tree_edges(E1, E2) \
2003 do { \
2004 tree t1_ = (E1), t2_ = (E2); \
2005 if (t1_ != t2_ \
2006 && (!t1_ || !t2_ \
2007 || !TREE_VISITED (t2_) \
2008 || (!TREE_ASM_WRITTEN (t2_) \
2009 && !compare_tree_sccs_1 (t1_, t2_, map)))) \
2010 return false; \
2011 /* Only non-NULL trees outside of the SCC may compare equal. */ \
2012 gcc_checking_assert (t1_ != t2_ || (!t2_ || !TREE_VISITED (t2_))); \
2013 } while (0)
2015 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
2017 if (code != IDENTIFIER_NODE)
2018 compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
2021 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
2023 unsigned i;
2024 /* Note that the number of elements for EXPR has already been emitted
2025 in EXPR's header (see streamer_write_tree_header). */
2026 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
2027 compare_tree_edges (VECTOR_CST_ELT (t1, i), VECTOR_CST_ELT (t2, i));
2030 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
2032 compare_tree_edges (TREE_REALPART (t1), TREE_REALPART (t2));
2033 compare_tree_edges (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
2036 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
2038 compare_tree_edges (DECL_NAME (t1), DECL_NAME (t2));
2039 /* ??? Global decls from different TUs have non-matching
2040 TRANSLATION_UNIT_DECLs. Only consider a small set of
2041 decls equivalent, we should not end up merging others. */
2042 if ((code == TYPE_DECL
2043 || code == NAMESPACE_DECL
2044 || code == IMPORTED_DECL
2045 || code == CONST_DECL
2046 || (VAR_OR_FUNCTION_DECL_P (t1)
2047 && (TREE_PUBLIC (t1) || DECL_EXTERNAL (t1))))
2048 && DECL_FILE_SCOPE_P (t1) && DECL_FILE_SCOPE_P (t2))
2050 else
2051 compare_tree_edges (DECL_CONTEXT (t1), DECL_CONTEXT (t2));
2054 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2056 compare_tree_edges (DECL_SIZE (t1), DECL_SIZE (t2));
2057 compare_tree_edges (DECL_SIZE_UNIT (t1), DECL_SIZE_UNIT (t2));
2058 compare_tree_edges (DECL_ATTRIBUTES (t1), DECL_ATTRIBUTES (t2));
2059 if ((code == VAR_DECL
2060 || code == PARM_DECL)
2061 && DECL_HAS_VALUE_EXPR_P (t1))
2062 compare_tree_edges (DECL_VALUE_EXPR (t1), DECL_VALUE_EXPR (t2));
2063 if (code == VAR_DECL
2064 && DECL_HAS_DEBUG_EXPR_P (t1))
2065 compare_tree_edges (DECL_DEBUG_EXPR (t1), DECL_DEBUG_EXPR (t2));
2066 /* LTO specific edges. */
2067 if (code != FUNCTION_DECL
2068 && code != TRANSLATION_UNIT_DECL)
2069 compare_tree_edges (DECL_INITIAL (t1), DECL_INITIAL (t2));
2072 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2074 if (code == FUNCTION_DECL)
2076 tree a1, a2;
2077 for (a1 = DECL_ARGUMENTS (t1), a2 = DECL_ARGUMENTS (t2);
2078 a1 || a2;
2079 a1 = TREE_CHAIN (a1), a2 = TREE_CHAIN (a2))
2080 compare_tree_edges (a1, a2);
2081 compare_tree_edges (DECL_RESULT (t1), DECL_RESULT (t2));
2083 else if (code == TYPE_DECL)
2084 compare_tree_edges (DECL_ORIGINAL_TYPE (t1), DECL_ORIGINAL_TYPE (t2));
2085 compare_tree_edges (DECL_VINDEX (t1), DECL_VINDEX (t2));
2088 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2090 /* Make sure we don't inadvertently set the assembler name. */
2091 if (DECL_ASSEMBLER_NAME_SET_P (t1))
2092 compare_tree_edges (DECL_ASSEMBLER_NAME (t1),
2093 DECL_ASSEMBLER_NAME (t2));
2094 compare_tree_edges (DECL_SECTION_NAME (t1), DECL_SECTION_NAME (t2));
2095 compare_tree_edges (DECL_COMDAT_GROUP (t1), DECL_COMDAT_GROUP (t2));
2098 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2100 compare_tree_edges (DECL_FIELD_OFFSET (t1), DECL_FIELD_OFFSET (t2));
2101 compare_tree_edges (DECL_BIT_FIELD_TYPE (t1), DECL_BIT_FIELD_TYPE (t2));
2102 compare_tree_edges (DECL_BIT_FIELD_REPRESENTATIVE (t1),
2103 DECL_BIT_FIELD_REPRESENTATIVE (t2));
2104 compare_tree_edges (DECL_FIELD_BIT_OFFSET (t1),
2105 DECL_FIELD_BIT_OFFSET (t2));
2106 compare_tree_edges (DECL_FCONTEXT (t1), DECL_FCONTEXT (t2));
2109 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2111 compare_tree_edges (DECL_FUNCTION_PERSONALITY (t1),
2112 DECL_FUNCTION_PERSONALITY (t2));
2113 compare_tree_edges (DECL_FUNCTION_SPECIFIC_TARGET (t1),
2114 DECL_FUNCTION_SPECIFIC_TARGET (t2));
2115 compare_tree_edges (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t1),
2116 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t2));
2119 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
2121 compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
2122 compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
2123 compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
2124 compare_tree_edges (TYPE_NAME (t1), TYPE_NAME (t2));
2125 /* Do not compare TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
2126 reconstructed during fixup. */
2127 /* Do not compare TYPE_NEXT_VARIANT, we reconstruct the variant lists
2128 during fixup. */
2129 compare_tree_edges (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2));
2130 /* ??? Global types from different TUs have non-matching
2131 TRANSLATION_UNIT_DECLs. Still merge them if they are otherwise
2132 equal. */
2133 if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
2135 else
2136 compare_tree_edges (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
2137 /* TYPE_CANONICAL is re-computed during type merging, so do not
2138 compare it here. */
2139 compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
2142 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
2144 if (code == ENUMERAL_TYPE)
2145 compare_tree_edges (TYPE_VALUES (t1), TYPE_VALUES (t2));
2146 else if (code == ARRAY_TYPE)
2147 compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
2148 else if (RECORD_OR_UNION_TYPE_P (t1))
2150 tree f1, f2;
2151 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
2152 f1 || f2;
2153 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
2154 compare_tree_edges (f1, f2);
2155 compare_tree_edges (TYPE_BINFO (t1), TYPE_BINFO (t2));
2157 else if (code == FUNCTION_TYPE
2158 || code == METHOD_TYPE)
2159 compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
2160 if (!POINTER_TYPE_P (t1))
2161 compare_tree_edges (TYPE_MINVAL (t1), TYPE_MINVAL (t2));
2162 compare_tree_edges (TYPE_MAXVAL (t1), TYPE_MAXVAL (t2));
2165 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
2167 compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
2168 compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
2169 compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
2172 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
2173 for (int i = 0; i < TREE_VEC_LENGTH (t1); i++)
2174 compare_tree_edges (TREE_VEC_ELT (t1, i), TREE_VEC_ELT (t2, i));
2176 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
2178 for (int i = 0; i < TREE_OPERAND_LENGTH (t1); i++)
2179 compare_tree_edges (TREE_OPERAND (t1, i),
2180 TREE_OPERAND (t2, i));
2182 /* BLOCKs are function local and we don't merge anything there. */
2183 if (TREE_BLOCK (t1) || TREE_BLOCK (t2))
2184 return false;
2187 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
2189 unsigned i;
2190 tree t;
2191 /* Lengths have already been compared above. */
2192 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
2193 compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
2194 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
2195 compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
2196 compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
2197 compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
2198 compare_tree_edges (BINFO_VPTR_FIELD (t1), BINFO_VPTR_FIELD (t2));
2199 compare_tree_edges (BINFO_INHERITANCE_CHAIN (t1),
2200 BINFO_INHERITANCE_CHAIN (t2));
2201 compare_tree_edges (BINFO_SUBVTT_INDEX (t1),
2202 BINFO_SUBVTT_INDEX (t2));
2203 compare_tree_edges (BINFO_VPTR_INDEX (t1),
2204 BINFO_VPTR_INDEX (t2));
2207 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
2209 unsigned i;
2210 tree index, value;
2211 /* Lengths have already been compared above. */
2212 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, index, value)
2214 compare_tree_edges (index, CONSTRUCTOR_ELT (t2, i)->index);
2215 compare_tree_edges (value, CONSTRUCTOR_ELT (t2, i)->value);
2219 #undef compare_tree_edges
2221 return true;
2224 /* Compare the tree scc SCC to the prevailing candidate PSCC, filling
2225 out MAP if they are equal. */
2227 static bool
2228 compare_tree_sccs (tree_scc *pscc, tree_scc *scc,
2229 tree *map)
2231 /* Assume SCC entry hashes are sorted after their cardinality. Which
2232 means we can simply take the first n-tuple of equal hashes
2233 (which is recorded as entry_len) and do n SCC entry candidate
2234 comparisons. */
2235 for (unsigned i = 0; i < pscc->entry_len; ++i)
2237 tree *mapp = map;
2238 num_scc_compare_collisions++;
2239 if (compare_tree_sccs_1 (pscc->entries[0], scc->entries[i], &mapp))
2241 /* Equal - no need to reset TREE_VISITED or TREE_ASM_WRITTEN
2242 on the scc as all trees will be freed. */
2243 return true;
2245 /* Reset TREE_ASM_WRITTEN on scc for the next compare or in case
2246 the SCC prevails. */
2247 for (unsigned j = 0; j < scc->len; ++j)
2248 TREE_ASM_WRITTEN (scc->entries[j]) = 0;
2251 return false;
2254 /* QSort sort function to sort a map of two pointers after the 2nd
2255 pointer. */
2257 static int
2258 cmp_tree (const void *p1_, const void *p2_)
2260 tree *p1 = (tree *)(const_cast<void *>(p1_));
2261 tree *p2 = (tree *)(const_cast<void *>(p2_));
2262 if (p1[1] == p2[1])
2263 return 0;
2264 return ((uintptr_t)p1[1] < (uintptr_t)p2[1]) ? -1 : 1;
2267 /* Try to unify the SCC with nodes FROM to FROM + LEN in CACHE and
2268 hash value SCC_HASH with an already recorded SCC. Return true if
2269 that was successful, otherwise return false. */
2271 static bool
2272 unify_scc (struct streamer_tree_cache_d *cache, unsigned from,
2273 unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
2275 bool unified_p = false;
2276 tree_scc *scc
2277 = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
2278 scc->next = NULL;
2279 scc->hash = scc_hash;
2280 scc->len = len;
2281 scc->entry_len = scc_entry_len;
2282 for (unsigned i = 0; i < len; ++i)
2284 tree t = streamer_tree_cache_get_tree (cache, from + i);
2285 scc->entries[i] = t;
2286 /* Do not merge SCCs with local entities inside them. Also do
2287 not merge TRANSLATION_UNIT_DECLs. */
2288 if (TREE_CODE (t) == TRANSLATION_UNIT_DECL
2289 || (VAR_OR_FUNCTION_DECL_P (t)
2290 && !(TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
2291 || TREE_CODE (t) == LABEL_DECL)
2293 /* Avoid doing any work for these cases and do not worry to
2294 record the SCCs for further merging. */
2295 return false;
2299 /* Look for the list of candidate SCCs to compare against. */
2300 tree_scc **slot;
2301 slot = tree_scc_hash.find_slot_with_hash (scc, scc_hash, INSERT);
2302 if (*slot)
2304 /* Try unifying against each candidate. */
2305 num_scc_compares++;
2307 /* Set TREE_VISITED on the scc so we can easily identify tree nodes
2308 outside of the scc when following tree edges. Make sure
2309 that TREE_ASM_WRITTEN is unset so we can use it as 2nd bit
2310 to track whether we visited the SCC member during the compare.
2311 We cannot use TREE_VISITED on the pscc members as the extended
2312 scc and pscc can overlap. */
2313 for (unsigned i = 0; i < scc->len; ++i)
2315 TREE_VISITED (scc->entries[i]) = 1;
2316 gcc_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
2319 tree *map = XALLOCAVEC (tree, 2 * len);
2320 for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
2322 if (!compare_tree_sccs (pscc, scc, map))
2323 continue;
2325 /* Found an equal SCC. */
2326 unified_p = true;
2327 num_scc_compare_collisions--;
2328 num_sccs_merged++;
2329 total_scc_size_merged += len;
2331 #ifdef ENABLE_CHECKING
2332 for (unsigned i = 0; i < len; ++i)
2334 tree t = map[2*i+1];
2335 enum tree_code code = TREE_CODE (t);
2336 /* IDENTIFIER_NODEs should be singletons and are merged by the
2337 streamer. The others should be singletons, too, and we
2338 should not merge them in any way. */
2339 gcc_assert (code != TRANSLATION_UNIT_DECL
2340 && code != IDENTIFIER_NODE
2341 && !streamer_handle_as_builtin_p (t));
2343 #endif
2345 /* Fixup the streamer cache with the prevailing nodes according
2346 to the tree node mapping computed by compare_tree_sccs. */
2347 if (len == 1)
2348 streamer_tree_cache_replace_tree (cache, pscc->entries[0], from);
2349 else
2351 tree *map2 = XALLOCAVEC (tree, 2 * len);
2352 for (unsigned i = 0; i < len; ++i)
2354 map2[i*2] = (tree)(uintptr_t)(from + i);
2355 map2[i*2+1] = scc->entries[i];
2357 qsort (map2, len, 2 * sizeof (tree), cmp_tree);
2358 qsort (map, len, 2 * sizeof (tree), cmp_tree);
2359 for (unsigned i = 0; i < len; ++i)
2360 streamer_tree_cache_replace_tree (cache, map[2*i],
2361 (uintptr_t)map2[2*i]);
2364 /* Free the tree nodes from the read SCC. */
2365 for (unsigned i = 0; i < len; ++i)
2367 if (TYPE_P (scc->entries[i]))
2368 num_merged_types++;
2369 ggc_free (scc->entries[i]);
2372 break;
2375 /* Reset TREE_VISITED if we didn't unify the SCC with another. */
2376 if (!unified_p)
2377 for (unsigned i = 0; i < scc->len; ++i)
2378 TREE_VISITED (scc->entries[i]) = 0;
2381 /* If we didn't unify it to any candidate duplicate the relevant
2382 pieces to permanent storage and link it into the chain. */
2383 if (!unified_p)
2385 tree_scc *pscc
2386 = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
2387 memcpy (pscc, scc, sizeof (tree_scc));
2388 pscc->next = (*slot);
2389 *slot = pscc;
2391 return unified_p;
2395 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
2396 RESOLUTIONS is the set of symbols picked by the linker (read from the
2397 resolution file when the linker plugin is being used). */
2399 static void
2400 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
2401 vec<ld_plugin_symbol_resolution_t> resolutions)
2403 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
2404 const int decl_offset = sizeof (struct lto_decl_header);
2405 const int main_offset = decl_offset + header->decl_state_size;
2406 const int string_offset = main_offset + header->main_size;
2407 struct lto_input_block ib_main;
2408 struct data_in *data_in;
2409 unsigned int i;
2410 const uint32_t *data_ptr, *data_end;
2411 uint32_t num_decl_states;
2413 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2414 header->main_size);
2416 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
2417 header->string_size, resolutions);
2419 /* We do not uniquify the pre-loaded cache entries, those are middle-end
2420 internal types that should not be merged. */
2422 /* Read the global declarations and types. */
2423 while (ib_main.p < ib_main.len)
2425 tree t;
2426 unsigned from = data_in->reader_cache->nodes.length ();
2427 /* Read and uniquify SCCs as in the input stream. */
2428 enum LTO_tags tag = streamer_read_record_start (&ib_main);
2429 if (tag == LTO_tree_scc)
2431 unsigned len_;
2432 unsigned scc_entry_len;
2433 hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
2434 &scc_entry_len);
2435 unsigned len = data_in->reader_cache->nodes.length () - from;
2436 gcc_assert (len == len_);
2438 total_scc_size += len;
2439 num_sccs_read++;
2441 /* We have the special case of size-1 SCCs that are pre-merged
2442 by means of identifier and string sharing for example.
2443 ??? Maybe we should avoid streaming those as SCCs. */
2444 tree first = streamer_tree_cache_get_tree (data_in->reader_cache,
2445 from);
2446 if (len == 1
2447 && (TREE_CODE (first) == IDENTIFIER_NODE
2448 || TREE_CODE (first) == INTEGER_CST
2449 || TREE_CODE (first) == TRANSLATION_UNIT_DECL
2450 || streamer_handle_as_builtin_p (first)))
2451 continue;
2453 /* Try to unify the SCC with already existing ones. */
2454 if (!flag_ltrans
2455 && unify_scc (data_in->reader_cache, from,
2456 len, scc_entry_len, scc_hash))
2457 continue;
2459 /* Do remaining fixup tasks for prevailing nodes. */
2460 bool seen_type = false;
2461 bool not_merged_type_same_scc = false;
2462 bool not_merged_type_not_same_scc = false;
2463 for (unsigned i = 0; i < len; ++i)
2465 tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
2466 from + i);
2467 /* For statistics, see if the old code would have merged
2468 the type. */
2469 if (TYPE_P (t)
2470 && (flag_lto_report || (flag_wpa && flag_lto_report_wpa)))
2472 tree newt = gimple_register_type (t);
2473 if (newt != t)
2475 num_not_merged_types++;
2476 unsigned j;
2477 /* Check if we can never merge the types because
2478 they are in the same SCC and thus the old
2479 code was broken. */
2480 for (j = 0; j < len; ++j)
2481 if (i != j
2482 && streamer_tree_cache_get_tree
2483 (data_in->reader_cache, from + j) == newt)
2485 num_not_merged_types_in_same_scc++;
2486 not_merged_type_same_scc = true;
2487 break;
2489 if (j == len)
2490 not_merged_type_not_same_scc = true;
2493 /* Reconstruct the type variant and pointer-to/reference-to
2494 chains. */
2495 if (TYPE_P (t))
2497 seen_type = true;
2498 num_prevailing_types++;
2499 lto_fixup_prevailing_type (t);
2501 /* Compute the canonical type of all types.
2502 ??? Should be able to assert that !TYPE_CANONICAL. */
2503 if (TYPE_P (t) && !TYPE_CANONICAL (t))
2504 TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
2505 /* Link shared INTEGER_CSTs into TYPE_CACHED_VALUEs of its
2506 type which is also member of this SCC. */
2507 if (TREE_CODE (t) == INTEGER_CST
2508 && !TREE_OVERFLOW (t))
2509 cache_integer_cst (t);
2510 /* Register TYPE_DECLs with the debuginfo machinery. */
2511 if (!flag_wpa
2512 && TREE_CODE (t) == TYPE_DECL)
2513 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
2514 if (!flag_ltrans)
2516 /* Register variables and functions with the
2517 symbol table. */
2518 if (TREE_CODE (t) == VAR_DECL)
2519 lto_register_var_decl_in_symtab (data_in, t, from + i);
2520 else if (TREE_CODE (t) == FUNCTION_DECL
2521 && !DECL_BUILT_IN (t))
2522 lto_register_function_decl_in_symtab (data_in, t, from + i);
2523 /* Scan the tree for references to global functions or
2524 variables and record those for later fixup. */
2525 maybe_remember_with_vars (t);
2528 if (not_merged_type_same_scc)
2530 num_not_merged_types_in_same_scc_trees += len;
2531 num_not_merged_types_trees += len;
2533 else if (not_merged_type_not_same_scc)
2534 num_not_merged_types_trees += len;
2535 if (seen_type)
2536 num_type_scc_trees += len;
2538 else
2540 /* Pickle stray references. */
2541 t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
2542 gcc_assert (t && data_in->reader_cache->nodes.length () == from);
2546 /* Read in lto_in_decl_state objects. */
2547 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
2548 data_end =
2549 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
2550 num_decl_states = *data_ptr++;
2552 gcc_assert (num_decl_states > 0);
2553 decl_data->global_decl_state = lto_new_in_decl_state ();
2554 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
2555 decl_data->global_decl_state);
2557 /* Read in per-function decl states and enter them in hash table. */
2558 decl_data->function_decl_states =
2559 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
2561 for (i = 1; i < num_decl_states; i++)
2563 struct lto_in_decl_state *state = lto_new_in_decl_state ();
2564 void **slot;
2566 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
2567 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
2568 gcc_assert (*slot == NULL);
2569 *slot = state;
2572 if (data_ptr != data_end)
2573 internal_error ("bytecode stream: garbage at the end of symbols section");
2575 /* Set the current decl state to be the global state. */
2576 decl_data->current_decl_state = decl_data->global_decl_state;
2578 lto_data_in_delete (data_in);
2581 /* Custom version of strtoll, which is not portable. */
2583 static HOST_WIDEST_INT
2584 lto_parse_hex (const char *p)
2586 HOST_WIDEST_INT ret = 0;
2588 for (; *p != '\0'; ++p)
2590 char c = *p;
2591 unsigned char part;
2592 ret <<= 4;
2593 if (c >= '0' && c <= '9')
2594 part = c - '0';
2595 else if (c >= 'a' && c <= 'f')
2596 part = c - 'a' + 10;
2597 else if (c >= 'A' && c <= 'F')
2598 part = c - 'A' + 10;
2599 else
2600 internal_error ("could not parse hex number");
2601 ret |= part;
2604 return ret;
2607 /* Read resolution for file named FILE_NAME. The resolution is read from
2608 RESOLUTION. */
2610 static void
2611 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2613 /* We require that objects in the resolution file are in the same
2614 order as the lto1 command line. */
2615 unsigned int name_len;
2616 char *obj_name;
2617 unsigned int num_symbols;
2618 unsigned int i;
2619 struct lto_file_decl_data *file_data;
2620 splay_tree_node nd = NULL;
2622 if (!resolution)
2623 return;
2625 name_len = strlen (file->filename);
2626 obj_name = XNEWVEC (char, name_len + 1);
2627 fscanf (resolution, " "); /* Read white space. */
2629 fread (obj_name, sizeof (char), name_len, resolution);
2630 obj_name[name_len] = '\0';
2631 if (filename_cmp (obj_name, file->filename) != 0)
2632 internal_error ("unexpected file name %s in linker resolution file. "
2633 "Expected %s", obj_name, file->filename);
2634 if (file->offset != 0)
2636 int t;
2637 char offset_p[17];
2638 HOST_WIDEST_INT offset;
2639 t = fscanf (resolution, "@0x%16s", offset_p);
2640 if (t != 1)
2641 internal_error ("could not parse file offset");
2642 offset = lto_parse_hex (offset_p);
2643 if (offset != file->offset)
2644 internal_error ("unexpected offset");
2647 free (obj_name);
2649 fscanf (resolution, "%u", &num_symbols);
2651 for (i = 0; i < num_symbols; i++)
2653 int t;
2654 unsigned index;
2655 unsigned HOST_WIDE_INT id;
2656 char r_str[27];
2657 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2658 unsigned int j;
2659 unsigned int lto_resolution_str_len =
2660 sizeof (lto_resolution_str) / sizeof (char *);
2661 res_pair rp;
2663 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
2664 &index, &id, r_str);
2665 if (t != 3)
2666 internal_error ("invalid line in the resolution file");
2668 for (j = 0; j < lto_resolution_str_len; j++)
2670 if (strcmp (lto_resolution_str[j], r_str) == 0)
2672 r = (enum ld_plugin_symbol_resolution) j;
2673 break;
2676 if (j == lto_resolution_str_len)
2677 internal_error ("invalid resolution in the resolution file");
2679 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2681 nd = lto_splay_tree_lookup (file_ids, id);
2682 if (nd == NULL)
2683 internal_error ("resolution sub id %wx not in object file", id);
2686 file_data = (struct lto_file_decl_data *)nd->value;
2687 /* The indexes are very sparse. To save memory save them in a compact
2688 format that is only unpacked later when the subfile is processed. */
2689 rp.res = r;
2690 rp.index = index;
2691 file_data->respairs.safe_push (rp);
2692 if (file_data->max_index < index)
2693 file_data->max_index = index;
2697 /* List of file_decl_datas */
2698 struct file_data_list
2700 struct lto_file_decl_data *first, *last;
2703 /* Is the name for a id'ed LTO section? */
2705 static int
2706 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2708 const char *s;
2710 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
2711 return 0;
2712 s = strrchr (name, '.');
2713 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2716 /* Create file_data of each sub file id */
2718 static int
2719 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2720 struct file_data_list *list)
2722 struct lto_section_slot s_slot, *new_slot;
2723 unsigned HOST_WIDE_INT id;
2724 splay_tree_node nd;
2725 void **hash_slot;
2726 char *new_name;
2727 struct lto_file_decl_data *file_data;
2729 if (!lto_section_with_id (ls->name, &id))
2730 return 1;
2732 /* Find hash table of sub module id */
2733 nd = lto_splay_tree_lookup (file_ids, id);
2734 if (nd != NULL)
2736 file_data = (struct lto_file_decl_data *)nd->value;
2738 else
2740 file_data = ggc_alloc_lto_file_decl_data ();
2741 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2742 file_data->id = id;
2743 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
2744 lto_splay_tree_insert (file_ids, id, file_data);
2746 /* Maintain list in linker order */
2747 if (!list->first)
2748 list->first = file_data;
2749 if (list->last)
2750 list->last->next = file_data;
2751 list->last = file_data;
2754 /* Copy section into sub module hash table */
2755 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2756 s_slot.name = new_name;
2757 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2758 gcc_assert (*hash_slot == NULL);
2760 new_slot = XDUP (struct lto_section_slot, ls);
2761 new_slot->name = new_name;
2762 *hash_slot = new_slot;
2763 return 1;
2766 /* Read declarations and other initializations for a FILE_DATA. */
2768 static void
2769 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2771 const char *data;
2772 size_t len;
2773 vec<ld_plugin_symbol_resolution_t>
2774 resolutions = vNULL;
2775 int i;
2776 res_pair *rp;
2778 /* Create vector for fast access of resolution. We do this lazily
2779 to save memory. */
2780 resolutions.safe_grow_cleared (file_data->max_index + 1);
2781 for (i = 0; file_data->respairs.iterate (i, &rp); i++)
2782 resolutions[rp->index] = rp->res;
2783 file_data->respairs.release ();
2785 file_data->renaming_hash_table = lto_create_renaming_table ();
2786 file_data->file_name = file->filename;
2787 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2788 if (data == NULL)
2790 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2791 return;
2793 /* Frees resolutions */
2794 lto_read_decls (file_data, data, resolutions);
2795 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2798 /* Finalize FILE_DATA in FILE and increase COUNT. */
2800 static int
2801 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2802 int *count)
2804 lto_file_finalize (file_data, file);
2805 if (cgraph_dump_file)
2806 fprintf (cgraph_dump_file, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2807 file_data->file_name, file_data->id);
2808 (*count)++;
2809 return 0;
2812 /* Generate a TREE representation for all types and external decls
2813 entities in FILE.
2815 Read all of the globals out of the file. Then read the cgraph
2816 and process the .o index into the cgraph nodes so that it can open
2817 the .o file to load the functions and ipa information. */
2819 static struct lto_file_decl_data *
2820 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2822 struct lto_file_decl_data *file_data = NULL;
2823 splay_tree file_ids;
2824 htab_t section_hash_table;
2825 struct lto_section_slot *section;
2826 struct file_data_list file_list;
2827 struct lto_section_list section_list;
2829 memset (&section_list, 0, sizeof (struct lto_section_list));
2830 section_hash_table = lto_obj_build_section_table (file, &section_list);
2832 /* Find all sub modules in the object and put their sections into new hash
2833 tables in a splay tree. */
2834 file_ids = lto_splay_tree_new ();
2835 memset (&file_list, 0, sizeof (struct file_data_list));
2836 for (section = section_list.first; section != NULL; section = section->next)
2837 create_subid_section_table (section, file_ids, &file_list);
2839 /* Add resolutions to file ids */
2840 lto_resolution_read (file_ids, resolution_file, file);
2842 /* Finalize each lto file for each submodule in the merged object */
2843 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2844 lto_create_files_from_ids (file, file_data, count);
2846 splay_tree_delete (file_ids);
2847 htab_delete (section_hash_table);
2849 return file_list.first;
2852 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2853 #define LTO_MMAP_IO 1
2854 #endif
2856 #if LTO_MMAP_IO
2857 /* Page size of machine is used for mmap and munmap calls. */
2858 static size_t page_mask;
2859 #endif
2861 /* Get the section data of length LEN from FILENAME starting at
2862 OFFSET. The data segment must be freed by the caller when the
2863 caller is finished. Returns NULL if all was not well. */
2865 static char *
2866 lto_read_section_data (struct lto_file_decl_data *file_data,
2867 intptr_t offset, size_t len)
2869 char *result;
2870 static int fd = -1;
2871 static char *fd_name;
2872 #if LTO_MMAP_IO
2873 intptr_t computed_len;
2874 intptr_t computed_offset;
2875 intptr_t diff;
2876 #endif
2878 /* Keep a single-entry file-descriptor cache. The last file we
2879 touched will get closed at exit.
2880 ??? Eventually we want to add a more sophisticated larger cache
2881 or rather fix function body streaming to not stream them in
2882 practically random order. */
2883 if (fd != -1
2884 && filename_cmp (fd_name, file_data->file_name) != 0)
2886 free (fd_name);
2887 close (fd);
2888 fd = -1;
2890 if (fd == -1)
2892 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2893 if (fd == -1)
2895 fatal_error ("Cannot open %s", file_data->file_name);
2896 return NULL;
2898 fd_name = xstrdup (file_data->file_name);
2901 #if LTO_MMAP_IO
2902 if (!page_mask)
2904 size_t page_size = sysconf (_SC_PAGE_SIZE);
2905 page_mask = ~(page_size - 1);
2908 computed_offset = offset & page_mask;
2909 diff = offset - computed_offset;
2910 computed_len = len + diff;
2912 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2913 fd, computed_offset);
2914 if (result == MAP_FAILED)
2916 fatal_error ("Cannot map %s", file_data->file_name);
2917 return NULL;
2920 return result + diff;
2921 #else
2922 result = (char *) xmalloc (len);
2923 if (lseek (fd, offset, SEEK_SET) != offset
2924 || read (fd, result, len) != (ssize_t) len)
2926 free (result);
2927 fatal_error ("Cannot read %s", file_data->file_name);
2928 result = NULL;
2930 #ifdef __MINGW32__
2931 /* Native windows doesn't supports delayed unlink on opened file. So
2932 we close file here again. This produces higher I/O load, but at least
2933 it prevents to have dangling file handles preventing unlink. */
2934 free (fd_name);
2935 fd_name = NULL;
2936 close (fd);
2937 fd = -1;
2938 #endif
2939 return result;
2940 #endif
2944 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2945 NAME will be NULL unless the section type is for a function
2946 body. */
2948 static const char *
2949 get_section_data (struct lto_file_decl_data *file_data,
2950 enum lto_section_type section_type,
2951 const char *name,
2952 size_t *len)
2954 htab_t section_hash_table = file_data->section_hash_table;
2955 struct lto_section_slot *f_slot;
2956 struct lto_section_slot s_slot;
2957 const char *section_name = lto_get_section_name (section_type, name, file_data);
2958 char *data = NULL;
2960 *len = 0;
2961 s_slot.name = section_name;
2962 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2963 if (f_slot)
2965 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2966 *len = f_slot->len;
2969 free (CONST_CAST (char *, section_name));
2970 return data;
2974 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2975 starts at OFFSET and has LEN bytes. */
2977 static void
2978 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2979 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2980 const char *name ATTRIBUTE_UNUSED,
2981 const char *offset, size_t len ATTRIBUTE_UNUSED)
2983 #if LTO_MMAP_IO
2984 intptr_t computed_len;
2985 intptr_t computed_offset;
2986 intptr_t diff;
2987 #endif
2989 #if LTO_MMAP_IO
2990 computed_offset = ((intptr_t) offset) & page_mask;
2991 diff = (intptr_t) offset - computed_offset;
2992 computed_len = len + diff;
2994 munmap ((caddr_t) computed_offset, computed_len);
2995 #else
2996 free (CONST_CAST(char *, offset));
2997 #endif
3000 static lto_file *current_lto_file;
3002 /* Helper for qsort; compare partitions and return one with smaller size.
3003 We sort from greatest to smallest so parallel build doesn't stale on the
3004 longest compilation being executed too late. */
3006 static int
3007 cmp_partitions_size (const void *a, const void *b)
3009 const struct ltrans_partition_def *pa
3010 = *(struct ltrans_partition_def *const *)a;
3011 const struct ltrans_partition_def *pb
3012 = *(struct ltrans_partition_def *const *)b;
3013 return pb->insns - pa->insns;
3016 /* Helper for qsort; compare partitions and return one with smaller order. */
3018 static int
3019 cmp_partitions_order (const void *a, const void *b)
3021 const struct ltrans_partition_def *pa
3022 = *(struct ltrans_partition_def *const *)a;
3023 const struct ltrans_partition_def *pb
3024 = *(struct ltrans_partition_def *const *)b;
3025 int ordera = -1, orderb = -1;
3027 if (lto_symtab_encoder_size (pa->encoder))
3028 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->symbol.order;
3029 if (lto_symtab_encoder_size (pb->encoder))
3030 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->symbol.order;
3031 return orderb - ordera;
3034 /* Write all output files in WPA mode and the file with the list of
3035 LTRANS units. */
3037 static void
3038 lto_wpa_write_files (void)
3040 unsigned i, n_sets;
3041 lto_file *file;
3042 ltrans_partition part;
3043 FILE *ltrans_output_list_stream;
3044 char *temp_filename;
3045 size_t blen;
3047 /* Open the LTRANS output list. */
3048 if (!ltrans_output_list)
3049 fatal_error ("no LTRANS output list filename provided");
3050 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
3051 if (ltrans_output_list_stream == NULL)
3052 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
3054 timevar_push (TV_WHOPR_WPA);
3056 FOR_EACH_VEC_ELT (ltrans_partitions, i, part)
3057 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
3059 /* Find out statics that need to be promoted
3060 to globals with hidden visibility because they are accessed from multiple
3061 partitions. */
3062 lto_promote_cross_file_statics ();
3064 timevar_pop (TV_WHOPR_WPA);
3066 timevar_push (TV_WHOPR_WPA_IO);
3068 /* Generate a prefix for the LTRANS unit files. */
3069 blen = strlen (ltrans_output_list);
3070 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
3071 strcpy (temp_filename, ltrans_output_list);
3072 if (blen > sizeof (".out")
3073 && strcmp (temp_filename + blen - sizeof (".out") + 1,
3074 ".out") == 0)
3075 temp_filename[blen - sizeof (".out") + 1] = '\0';
3076 blen = strlen (temp_filename);
3078 n_sets = ltrans_partitions.length ();
3080 /* Sort partitions by size so small ones are compiled last.
3081 FIXME: Even when not reordering we may want to output one list for parallel make
3082 and other for final link command. */
3083 ltrans_partitions.qsort (flag_toplevel_reorder
3084 ? cmp_partitions_size
3085 : cmp_partitions_order);
3086 for (i = 0; i < n_sets; i++)
3088 size_t len;
3089 ltrans_partition part = ltrans_partitions[i];
3091 /* Write all the nodes in SET. */
3092 sprintf (temp_filename + blen, "%u.o", i);
3093 file = lto_obj_file_open (temp_filename, true);
3094 if (!file)
3095 fatal_error ("lto_obj_file_open() failed");
3097 if (!quiet_flag)
3098 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
3099 if (cgraph_dump_file)
3101 lto_symtab_encoder_iterator lsei;
3103 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
3104 part->name, temp_filename, part->insns);
3105 fprintf (cgraph_dump_file, " Symbols in partition: ");
3106 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
3107 lsei_next_in_partition (&lsei))
3109 symtab_node node = lsei_node (lsei);
3110 fprintf (cgraph_dump_file, "%s ", symtab_node_asm_name (node));
3112 fprintf (cgraph_dump_file, "\n Symbols in boundary: ");
3113 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
3114 lsei_next (&lsei))
3116 symtab_node node = lsei_node (lsei);
3117 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
3119 fprintf (cgraph_dump_file, "%s ", symtab_node_asm_name (node));
3120 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
3121 if (cnode
3122 && lto_symtab_encoder_encode_body_p (part->encoder, cnode))
3123 fprintf (cgraph_dump_file, "(body included)");
3124 else
3126 varpool_node *vnode = dyn_cast <varpool_node> (node);
3127 if (vnode
3128 && lto_symtab_encoder_encode_initializer_p (part->encoder, vnode))
3129 fprintf (cgraph_dump_file, "(initializer included)");
3133 fprintf (cgraph_dump_file, "\n");
3135 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
3137 lto_set_current_out_file (file);
3139 ipa_write_optimization_summaries (part->encoder);
3141 lto_set_current_out_file (NULL);
3142 lto_obj_file_close (file);
3143 free (file);
3144 part->encoder = NULL;
3146 len = strlen (temp_filename);
3147 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
3148 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
3149 fatal_error ("writing to LTRANS output list %s: %m",
3150 ltrans_output_list);
3153 lto_stats.num_output_files += n_sets;
3155 /* Close the LTRANS output list. */
3156 if (fclose (ltrans_output_list_stream))
3157 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
3159 free_ltrans_partitions();
3160 free (temp_filename);
3162 timevar_pop (TV_WHOPR_WPA_IO);
3166 /* If TT is a variable or function decl replace it with its
3167 prevailing variant. */
3168 #define LTO_SET_PREVAIL(tt) \
3169 do {\
3170 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
3171 tt = lto_symtab_prevailing_decl (tt); \
3172 } while (0)
3174 /* Ensure that TT isn't a replacable var of function decl. */
3175 #define LTO_NO_PREVAIL(tt) \
3176 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
3178 /* Given a tree T replace all fields referring to variables or functions
3179 with their prevailing variant. */
3180 static void
3181 lto_fixup_prevailing_decls (tree t)
3183 enum tree_code code = TREE_CODE (t);
3184 LTO_NO_PREVAIL (TREE_TYPE (t));
3185 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
3186 LTO_NO_PREVAIL (TREE_CHAIN (t));
3187 if (DECL_P (t))
3189 LTO_NO_PREVAIL (DECL_NAME (t));
3190 LTO_SET_PREVAIL (DECL_CONTEXT (t));
3191 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
3193 LTO_SET_PREVAIL (DECL_SIZE (t));
3194 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
3195 LTO_SET_PREVAIL (DECL_INITIAL (t));
3196 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
3197 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
3199 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
3201 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
3202 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
3204 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
3206 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
3207 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
3208 LTO_NO_PREVAIL (DECL_VINDEX (t));
3210 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
3211 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
3212 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
3214 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
3215 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
3216 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
3217 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
3218 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
3221 else if (TYPE_P (t))
3223 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
3224 LTO_SET_PREVAIL (TYPE_SIZE (t));
3225 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
3226 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
3227 LTO_NO_PREVAIL (TYPE_NAME (t));
3229 LTO_SET_PREVAIL (TYPE_MINVAL (t));
3230 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
3231 LTO_SET_PREVAIL (t->type_non_common.binfo);
3233 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
3235 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
3236 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
3237 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
3239 else if (EXPR_P (t))
3241 int i;
3242 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
3243 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
3245 else
3247 switch (code)
3249 case TREE_LIST:
3250 LTO_SET_PREVAIL (TREE_VALUE (t));
3251 LTO_SET_PREVAIL (TREE_PURPOSE (t));
3252 break;
3253 default:
3254 gcc_unreachable ();
3258 #undef LTO_SET_PREVAIL
3259 #undef LTO_NO_PREVAIL
3261 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
3262 replaces var and function decls with the corresponding prevailing def. */
3264 static void
3265 lto_fixup_state (struct lto_in_decl_state *state)
3267 unsigned i, si;
3268 struct lto_tree_ref_table *table;
3270 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
3271 we still need to walk from all DECLs to find the reachable
3272 FUNCTION_DECLs and VAR_DECLs. */
3273 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
3275 table = &state->streams[si];
3276 for (i = 0; i < table->size; i++)
3278 tree *tp = table->trees + i;
3279 if (VAR_OR_FUNCTION_DECL_P (*tp))
3280 *tp = lto_symtab_prevailing_decl (*tp);
3285 /* A callback of htab_traverse. Just extracts a state from SLOT
3286 and calls lto_fixup_state. */
3288 static int
3289 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
3291 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
3292 lto_fixup_state (state);
3293 return 1;
3296 /* Fix the decls from all FILES. Replaces each decl with the corresponding
3297 prevailing one. */
3299 static void
3300 lto_fixup_decls (struct lto_file_decl_data **files)
3302 unsigned int i;
3303 htab_iterator hi;
3304 tree t;
3306 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
3307 lto_fixup_prevailing_decls (t);
3309 for (i = 0; files[i]; i++)
3311 struct lto_file_decl_data *file = files[i];
3312 struct lto_in_decl_state *state = file->global_decl_state;
3313 lto_fixup_state (state);
3315 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
3319 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
3321 /* Turn file datas for sub files into a single array, so that they look
3322 like separate files for further passes. */
3324 static void
3325 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
3327 struct lto_file_decl_data *n, *next;
3328 int i, k;
3330 lto_stats.num_input_files = count;
3331 all_file_decl_data
3332 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
3333 /* Set the hooks so that all of the ipa passes can read in their data. */
3334 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
3335 for (i = 0, k = 0; i < last_file_ix; i++)
3337 for (n = orig[i]; n != NULL; n = next)
3339 all_file_decl_data[k++] = n;
3340 next = n->next;
3341 n->next = NULL;
3344 all_file_decl_data[k] = NULL;
3345 gcc_assert (k == count);
3348 /* Input file data before flattening (i.e. splitting them to subfiles to support
3349 incremental linking. */
3350 static int real_file_count;
3351 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
3353 static void print_lto_report_1 (void);
3355 /* Read all the symbols from the input files FNAMES. NFILES is the
3356 number of files requested in the command line. Instantiate a
3357 global call graph by aggregating all the sub-graphs found in each
3358 file. */
3360 static void
3361 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
3363 unsigned int i, last_file_ix;
3364 FILE *resolution;
3365 int count = 0;
3366 struct lto_file_decl_data **decl_data;
3367 void **res;
3368 symtab_node snode;
3370 init_cgraph ();
3372 timevar_push (TV_IPA_LTO_DECL_IN);
3374 real_file_decl_data
3375 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
3376 real_file_count = nfiles;
3378 /* Read the resolution file. */
3379 resolution = NULL;
3380 if (resolution_file_name)
3382 int t;
3383 unsigned num_objects;
3385 resolution = fopen (resolution_file_name, "r");
3386 if (resolution == NULL)
3387 fatal_error ("could not open symbol resolution file: %m");
3389 t = fscanf (resolution, "%u", &num_objects);
3390 gcc_assert (t == 1);
3392 /* True, since the plugin splits the archives. */
3393 gcc_assert (num_objects == nfiles);
3395 cgraph_state = CGRAPH_LTO_STREAMING;
3397 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
3398 NULL);
3399 type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
3400 tree_int_map_eq, NULL);
3401 type_pair_cache = XCNEWVEC (struct type_pair_d, GIMPLE_TYPE_PAIR_SIZE);
3402 gimple_types = htab_create_ggc (16381, gimple_type_hash, gimple_type_eq, 0);
3403 gcc_obstack_init (&tree_scc_hash_obstack);
3404 tree_scc_hash.create (4096);
3406 if (!quiet_flag)
3407 fprintf (stderr, "Reading object files:");
3409 /* Read all of the object files specified on the command line. */
3410 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
3412 struct lto_file_decl_data *file_data = NULL;
3413 if (!quiet_flag)
3415 fprintf (stderr, " %s", fnames[i]);
3416 fflush (stderr);
3419 current_lto_file = lto_obj_file_open (fnames[i], false);
3420 if (!current_lto_file)
3421 break;
3423 file_data = lto_file_read (current_lto_file, resolution, &count);
3424 if (!file_data)
3426 lto_obj_file_close (current_lto_file);
3427 free (current_lto_file);
3428 current_lto_file = NULL;
3429 break;
3432 decl_data[last_file_ix++] = file_data;
3434 lto_obj_file_close (current_lto_file);
3435 free (current_lto_file);
3436 current_lto_file = NULL;
3439 lto_flatten_files (decl_data, count, last_file_ix);
3440 lto_stats.num_input_files = count;
3441 ggc_free(decl_data);
3442 real_file_decl_data = NULL;
3444 if (resolution_file_name)
3445 fclose (resolution);
3447 /* Show the LTO report before launching LTRANS. */
3448 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3449 print_lto_report_1 ();
3451 /* Free gimple type merging datastructures. */
3452 htab_delete (gimple_types);
3453 gimple_types = NULL;
3454 htab_delete (type_hash_cache);
3455 type_hash_cache = NULL;
3456 free (type_pair_cache);
3457 type_pair_cache = NULL;
3458 tree_scc_hash.dispose ();
3459 obstack_free (&tree_scc_hash_obstack, NULL);
3460 free_gimple_type_tables ();
3461 ggc_collect ();
3463 /* Set the hooks so that all of the ipa passes can read in their data. */
3464 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
3466 timevar_pop (TV_IPA_LTO_DECL_IN);
3468 if (!quiet_flag)
3469 fprintf (stderr, "\nReading the callgraph\n");
3471 timevar_push (TV_IPA_LTO_CGRAPH_IO);
3472 /* Read the symtab. */
3473 input_symtab ();
3475 /* Store resolutions into the symbol table. */
3477 FOR_EACH_SYMBOL (snode)
3478 if (symtab_real_symbol_p (snode)
3479 && snode->symbol.lto_file_data
3480 && snode->symbol.lto_file_data->resolution_map
3481 && (res = pointer_map_contains (snode->symbol.lto_file_data->resolution_map,
3482 snode->symbol.decl)))
3483 snode->symbol.resolution
3484 = (enum ld_plugin_symbol_resolution)(size_t)*res;
3485 for (i = 0; all_file_decl_data[i]; i++)
3486 if (all_file_decl_data[i]->resolution_map)
3488 pointer_map_destroy (all_file_decl_data[i]->resolution_map);
3489 all_file_decl_data[i]->resolution_map = NULL;
3492 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
3494 if (!quiet_flag)
3495 fprintf (stderr, "Merging declarations\n");
3497 timevar_push (TV_IPA_LTO_DECL_MERGE);
3498 /* Merge global decls. In ltrans mode we read merged cgraph, we do not
3499 need to care about resolving symbols again, we only need to replace
3500 duplicated declarations read from the callgraph and from function
3501 sections. */
3502 if (!flag_ltrans)
3504 lto_symtab_merge_decls ();
3506 /* If there were errors during symbol merging bail out, we have no
3507 good way to recover here. */
3508 if (seen_error ())
3509 fatal_error ("errors during merging of translation units");
3511 /* Fixup all decls. */
3512 lto_fixup_decls (all_file_decl_data);
3514 htab_delete (tree_with_vars);
3515 tree_with_vars = NULL;
3516 ggc_collect ();
3518 timevar_pop (TV_IPA_LTO_DECL_MERGE);
3519 /* Each pass will set the appropriate timer. */
3521 if (!quiet_flag)
3522 fprintf (stderr, "Reading summaries\n");
3524 /* Read the IPA summary data. */
3525 if (flag_ltrans)
3526 ipa_read_optimization_summaries ();
3527 else
3528 ipa_read_summaries ();
3530 for (i = 0; all_file_decl_data[i]; i++)
3532 gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
3533 lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
3534 all_file_decl_data[i]->symtab_node_encoder = NULL;
3537 /* Finally merge the cgraph according to the decl merging decisions. */
3538 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
3539 if (cgraph_dump_file)
3541 fprintf (cgraph_dump_file, "Before merging:\n");
3542 dump_symtab (cgraph_dump_file);
3544 lto_symtab_merge_symbols ();
3545 ggc_collect ();
3546 cgraph_state = CGRAPH_STATE_IPA_SSA;
3548 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
3550 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
3552 /* Indicate that the cgraph is built and ready. */
3553 cgraph_function_flags_ready = true;
3555 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
3556 ggc_free (all_file_decl_data);
3557 all_file_decl_data = NULL;
3561 /* Materialize all the bodies for all the nodes in the callgraph. */
3563 static void
3564 materialize_cgraph (void)
3566 tree decl;
3567 struct cgraph_node *node;
3568 unsigned i;
3569 timevar_id_t lto_timer;
3571 if (!quiet_flag)
3572 fprintf (stderr,
3573 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
3575 /* Now that we have input the cgraph, we need to clear all of the aux
3576 nodes and read the functions if we are not running in WPA mode. */
3577 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3579 FOR_EACH_FUNCTION (node)
3581 if (node->symbol.lto_file_data)
3583 lto_materialize_function (node);
3584 lto_stats.num_input_cgraph_nodes++;
3588 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3590 /* Start the appropriate timer depending on the mode that we are
3591 operating in. */
3592 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
3593 : (flag_ltrans) ? TV_WHOPR_LTRANS
3594 : TV_LTO;
3595 timevar_push (lto_timer);
3597 current_function_decl = NULL;
3598 set_cfun (NULL);
3600 /* Inform the middle end about the global variables we have seen. */
3601 FOR_EACH_VEC_ELT (*lto_global_var_decls, i, decl)
3602 rest_of_decl_compilation (decl, 1, 0);
3604 if (!quiet_flag)
3605 fprintf (stderr, "\n");
3607 timevar_pop (lto_timer);
3611 /* Show various memory usage statistics related to LTO. */
3612 static void
3613 print_lto_report_1 (void)
3615 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3616 fprintf (stderr, "%s statistics\n", pfx);
3618 fprintf (stderr, "[%s] read %lu SCCs of average size %f\n",
3619 pfx, num_sccs_read, total_scc_size / (double)num_sccs_read);
3620 fprintf (stderr, "[%s] %lu tree bodies read in total\n", pfx, total_scc_size);
3621 if (flag_wpa && tree_scc_hash.is_created ())
3623 fprintf (stderr, "[%s] tree SCC table: size %ld, %ld elements, "
3624 "collision ratio: %f\n", pfx,
3625 (long) tree_scc_hash.size (),
3626 (long) tree_scc_hash.elements (),
3627 tree_scc_hash.collisions ());
3628 hash_table<tree_scc_hasher>::iterator hiter;
3629 tree_scc *scc, *max_scc = NULL;
3630 unsigned max_length = 0;
3631 FOR_EACH_HASH_TABLE_ELEMENT (tree_scc_hash, scc, x, hiter)
3633 unsigned length = 0;
3634 tree_scc *s = scc;
3635 for (; s; s = s->next)
3636 length++;
3637 if (length > max_length)
3639 max_length = length;
3640 max_scc = scc;
3643 fprintf (stderr, "[%s] tree SCC max chain length %u (size %u)\n",
3644 pfx, max_length, max_scc->len);
3645 fprintf (stderr, "[%s] Compared %lu SCCs, %lu collisions (%f)\n", pfx,
3646 num_scc_compares, num_scc_compare_collisions,
3647 num_scc_compare_collisions / (double) num_scc_compares);
3648 fprintf (stderr, "[%s] Merged %lu SCCs\n", pfx, num_sccs_merged);
3649 fprintf (stderr, "[%s] Merged %lu tree bodies\n", pfx,
3650 total_scc_size_merged);
3651 fprintf (stderr, "[%s] Merged %lu types\n", pfx, num_merged_types);
3652 fprintf (stderr, "[%s] %lu types prevailed (%lu associated trees)\n",
3653 pfx, num_prevailing_types, num_type_scc_trees);
3654 fprintf (stderr, "[%s] Old merging code merges an additional %lu types"
3655 " of which %lu are in the same SCC with their "
3656 "prevailing variant (%lu and %lu associated trees)\n",
3657 pfx, num_not_merged_types, num_not_merged_types_in_same_scc,
3658 num_not_merged_types_trees,
3659 num_not_merged_types_in_same_scc_trees);
3662 print_gimple_types_stats (pfx);
3663 print_lto_report (pfx);
3666 /* Perform whole program analysis (WPA) on the callgraph and write out the
3667 optimization plan. */
3669 static void
3670 do_whole_program_analysis (void)
3672 symtab_node node;
3674 timevar_start (TV_PHASE_OPT_GEN);
3676 /* Note that since we are in WPA mode, materialize_cgraph will not
3677 actually read in all the function bodies. It only materializes
3678 the decls and cgraph nodes so that analysis can be performed. */
3679 materialize_cgraph ();
3681 /* Reading in the cgraph uses different timers, start timing WPA now. */
3682 timevar_push (TV_WHOPR_WPA);
3684 if (pre_ipa_mem_report)
3686 fprintf (stderr, "Memory consumption before IPA\n");
3687 dump_memory_report (false);
3690 cgraph_function_flags_ready = true;
3692 if (cgraph_dump_file)
3693 dump_symtab (cgraph_dump_file);
3694 bitmap_obstack_initialize (NULL);
3695 cgraph_state = CGRAPH_STATE_IPA_SSA;
3697 execute_ipa_pass_list (all_regular_ipa_passes);
3698 symtab_remove_unreachable_nodes (false, dump_file);
3700 if (cgraph_dump_file)
3702 fprintf (cgraph_dump_file, "Optimized ");
3703 dump_symtab (cgraph_dump_file);
3705 #ifdef ENABLE_CHECKING
3706 verify_cgraph ();
3707 #endif
3708 bitmap_obstack_release (NULL);
3710 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3711 timevar_pop (TV_WHOPR_WPA);
3713 timevar_push (TV_WHOPR_PARTITIONING);
3714 if (flag_lto_partition_1to1)
3715 lto_1_to_1_map ();
3716 else if (flag_lto_partition_max)
3717 lto_max_map ();
3718 else
3719 lto_balanced_map ();
3721 /* AUX pointers are used by partitioning code to bookkeep number of
3722 partitions symbol is in. This is no longer needed. */
3723 FOR_EACH_SYMBOL (node)
3724 node->symbol.aux = NULL;
3726 lto_stats.num_cgraph_partitions += ltrans_partitions.length ();
3727 timevar_pop (TV_WHOPR_PARTITIONING);
3729 timevar_stop (TV_PHASE_OPT_GEN);
3730 timevar_start (TV_PHASE_STREAM_OUT);
3732 if (!quiet_flag)
3734 fprintf (stderr, "\nStreaming out");
3735 fflush (stderr);
3737 lto_wpa_write_files ();
3738 if (!quiet_flag)
3739 fprintf (stderr, "\n");
3741 timevar_stop (TV_PHASE_STREAM_OUT);
3743 ggc_collect ();
3744 if (post_ipa_mem_report)
3746 fprintf (stderr, "Memory consumption after IPA\n");
3747 dump_memory_report (false);
3750 /* Show the LTO report before launching LTRANS. */
3751 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3752 print_lto_report_1 ();
3753 if (mem_report_wpa)
3754 dump_memory_report (true);
3758 static GTY(()) tree lto_eh_personality_decl;
3760 /* Return the LTO personality function decl. */
3762 tree
3763 lto_eh_personality (void)
3765 if (!lto_eh_personality_decl)
3767 /* Use the first personality DECL for our personality if we don't
3768 support multiple ones. This ensures that we don't artificially
3769 create the need for them in a single-language program. */
3770 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3771 lto_eh_personality_decl = first_personality_decl;
3772 else
3773 lto_eh_personality_decl = lhd_gcc_personality ();
3776 return lto_eh_personality_decl;
3779 /* Set the process name based on the LTO mode. */
3781 static void
3782 lto_process_name (void)
3784 if (flag_lto)
3785 setproctitle ("lto1-lto");
3786 if (flag_wpa)
3787 setproctitle ("lto1-wpa");
3788 if (flag_ltrans)
3789 setproctitle ("lto1-ltrans");
3793 /* Initialize the LTO front end. */
3795 static void
3796 lto_init (void)
3798 lto_process_name ();
3799 lto_streamer_hooks_init ();
3800 lto_reader_init ();
3801 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3802 memset (&lto_stats, 0, sizeof (lto_stats));
3803 bitmap_obstack_initialize (NULL);
3804 gimple_register_cfg_hooks ();
3808 /* Main entry point for the GIMPLE front end. This front end has
3809 three main personalities:
3811 - LTO (-flto). All the object files on the command line are
3812 loaded in memory and processed as a single translation unit.
3813 This is the traditional link-time optimization behavior.
3815 - WPA (-fwpa). Only the callgraph and summary information for
3816 files in the command file are loaded. A single callgraph
3817 (without function bodies) is instantiated for the whole set of
3818 files. IPA passes are only allowed to analyze the call graph
3819 and make transformation decisions. The callgraph is
3820 partitioned, each partition is written to a new object file
3821 together with the transformation decisions.
3823 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3824 summary files from running again. Since WPA computed summary
3825 information and decided what transformations to apply, LTRANS
3826 simply applies them. */
3828 void
3829 lto_main (void)
3831 /* LTO is called as a front end, even though it is not a front end.
3832 Because it is called as a front end, TV_PHASE_PARSING and
3833 TV_PARSE_GLOBAL are active, and we need to turn them off while
3834 doing LTO. Later we turn them back on so they are active up in
3835 toplev.c. */
3836 timevar_pop (TV_PARSE_GLOBAL);
3837 timevar_stop (TV_PHASE_PARSING);
3839 timevar_start (TV_PHASE_SETUP);
3841 /* Initialize the LTO front end. */
3842 lto_init ();
3844 timevar_stop (TV_PHASE_SETUP);
3845 timevar_start (TV_PHASE_STREAM_IN);
3847 /* Read all the symbols and call graph from all the files in the
3848 command line. */
3849 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3851 timevar_stop (TV_PHASE_STREAM_IN);
3853 if (!seen_error ())
3855 /* If WPA is enabled analyze the whole call graph and create an
3856 optimization plan. Otherwise, read in all the function
3857 bodies and continue with optimization. */
3858 if (flag_wpa)
3859 do_whole_program_analysis ();
3860 else
3862 struct varpool_node *vnode;
3864 timevar_start (TV_PHASE_OPT_GEN);
3866 materialize_cgraph ();
3867 if (!flag_ltrans)
3868 lto_promote_statics_nonwpa ();
3870 /* Let the middle end know that we have read and merged all of
3871 the input files. */
3872 compile ();
3874 timevar_stop (TV_PHASE_OPT_GEN);
3876 /* FIXME lto, if the processes spawned by WPA fail, we miss
3877 the chance to print WPA's report, so WPA will call
3878 print_lto_report before launching LTRANS. If LTRANS was
3879 launched directly by the driver we would not need to do
3880 this. */
3881 if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
3882 print_lto_report_1 ();
3884 /* Record the global variables. */
3885 FOR_EACH_DEFINED_VARIABLE (vnode)
3886 vec_safe_push (lto_global_var_decls, vnode->symbol.decl);
3890 /* Here we make LTO pretend to be a parser. */
3891 timevar_start (TV_PHASE_PARSING);
3892 timevar_push (TV_PARSE_GLOBAL);
3895 #include "gt-lto-lto.h"