LCOV - code coverage report
Current view: top level - gcc/lto - lto-common.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.1 % 1481 1319
Test Date: 2026-12-05 16:10:02 Functions: 98.5 % 66 65
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.