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