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