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

Generated by: LCOV version 2.4-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.