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