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