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