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-03-28 14:25:54 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      1337054 : hash_name (const void *p)
      73              : {
      74      1337054 :   const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
      75      1337054 :   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       818584 : eq_name (const void *p1, const void *p2)
      83              : {
      84       818584 :   const struct lto_section_slot *s1
      85              :     = (const struct lto_section_slot *) p1;
      86       818584 :   const struct lto_section_slot *s2
      87              :     = (const struct lto_section_slot *) p2;
      88              : 
      89       818584 :   return strcmp (s1->name, s2->name) == 0;
      90              : }
      91              : 
      92              : /* Free lto_section_slot.  */
      93              : 
      94              : static void
      95       384614 : free_with_string (void *arg)
      96              : {
      97       384614 :   struct lto_section_slot *s = (struct lto_section_slot *)arg;
      98              : 
      99       384614 :   free (const_cast<char *> (s->name));
     100       384614 :   free (arg);
     101       384614 : }
     102              : 
     103              : /* Create section hash table.  */
     104              : 
     105              : htab_t
     106        42912 : lto_obj_create_section_hash_table (void)
     107              : {
     108        42912 :   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        21456 : lto_splay_tree_delete_id (splay_tree_key key)
     115              : {
     116        21456 :   free ((void *) key);
     117        21456 : }
     118              : 
     119              : /* Compare splay tree node ids A and B.  */
     120              : 
     121              : static int
     122       716866 : lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
     123              : {
     124       716866 :   unsigned HOST_WIDE_INT ai;
     125       716866 :   unsigned HOST_WIDE_INT bi;
     126              : 
     127       716866 :   ai = *(unsigned HOST_WIDE_INT *) a;
     128       716866 :   bi = *(unsigned HOST_WIDE_INT *) b;
     129              : 
     130       716866 :   if (ai < bi)
     131              :     return -1;
     132       716866 :   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       379889 : 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        59411 : lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
     149              : {
     150        59411 :   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        21456 : lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
     159              :                        struct lto_file_decl_data *file_data)
     160              : {
     161        21456 :   unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
     162        21456 :   *idp = id;
     163        21456 :   splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
     164        21456 : }
     165              : 
     166              : /* Create a splay tree.  */
     167              : 
     168              : static splay_tree
     169        21456 : 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       155928 : lto_read_in_decl_state (class data_in *data_in, const uint32_t *data,
     183              :                         struct lto_in_decl_state *state)
     184              : {
     185       155928 :   uint32_t ix;
     186       155928 :   tree decl;
     187       155928 :   uint32_t i, j;
     188              : 
     189       155928 :   ix = *data++;
     190       155928 :   state->compressed = ix & 1;
     191       155928 :   ix /= 2;
     192       155928 :   decl = streamer_tree_cache_get_tree (data_in->reader_cache, ix);
     193       155928 :   if (!VAR_OR_FUNCTION_DECL_P (decl))
     194              :     {
     195        21456 :       gcc_assert (decl == void_type_node);
     196              :       decl = NULL_TREE;
     197              :     }
     198       155928 :   state->fn_decl = decl;
     199              : 
     200       311856 :   for (i = 0; i < LTO_N_DECL_STREAMS; i++)
     201              :     {
     202       155928 :       uint32_t size = *data++;
     203       155928 :       vec<tree, va_gc> *decls = NULL;
     204       155928 :       vec_alloc (decls, size);
     205              : 
     206      2116498 :       for (j = 0; j < size; j++)
     207      3609284 :         vec_safe_push (decls,
     208      1804642 :                        streamer_tree_cache_get_tree (data_in->reader_cache,
     209      1804642 :                                                      data[j]));
     210              : 
     211       155928 :       state->streams[i] = decls;
     212       155928 :       data += size;
     213              :     }
     214              : 
     215       155928 :   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      1766306 : hash_canonical_type (tree type)
     241              : {
     242      1766306 :   inchash::hash hstate;
     243      1766306 :   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      1766306 :   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      1766306 :   code = tree_code_for_canonical_type_merging (TREE_CODE (type));
     256      1766306 :   hstate.add_int (code);
     257      1766306 :   if (!RECORD_OR_UNION_TYPE_P (type))
     258      1723621 :     hstate.add_int (TYPE_MODE (type));
     259              : 
     260              :   /* Incorporate common features of numerical types.  */
     261      1766306 :   if (INTEGRAL_TYPE_P (type)
     262      1766306 :       || SCALAR_FLOAT_TYPE_P (type)
     263      1046596 :       || FIXED_POINT_TYPE_P (type)
     264       312160 :       || TREE_CODE (type) == OFFSET_TYPE
     265       312160 :       || POINTER_TYPE_P (type))
     266              :     {
     267      1516267 :       hstate.add_int (TYPE_PRECISION (type));
     268      1516267 :       if (!type_with_interoperable_signedness (type))
     269      1274858 :         hstate.add_int (TYPE_UNSIGNED (type));
     270              :     }
     271              : 
     272      1766306 :   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      1766306 :   if (TREE_CODE (type) == COMPLEX_TYPE)
     279       204689 :     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      1766306 :   if (POINTER_TYPE_P (type))
     289        62121 :     hstate.add_int (TYPE_ADDR_SPACE (TREE_TYPE (type)));
     290              : 
     291              :   /* For array types hash the domain bounds and the string flag.  */
     292      1766306 :   if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
     293              :     {
     294         2518 :       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         2518 :       if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
     298         2518 :         inchash::add_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), hstate);
     299         2518 :       if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
     300         2518 :         inchash::add_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), hstate);
     301              :     }
     302              : 
     303              :   /* Recurse for aggregates with a single element type.  */
     304      1766306 :   if (TREE_CODE (type) == ARRAY_TYPE
     305      1763788 :       || TREE_CODE (type) == COMPLEX_TYPE
     306      1559099 :       || TREE_CODE (type) == VECTOR_TYPE)
     307       207308 :     iterative_hash_canonical_type (TREE_TYPE (type), hstate);
     308              : 
     309              :   /* Incorporate function return and argument types.  */
     310      1766306 :   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      1766306 :   if (RECORD_OR_UNION_TYPE_P (type))
     327              :     {
     328        42685 :       unsigned nf;
     329        42685 :       tree f;
     330              : 
     331       191675 :       for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
     332       148990 :         if (TREE_CODE (f) == FIELD_DECL
     333       148990 :             && (! DECL_SIZE (f)
     334       148735 :                 || ! integer_zerop (DECL_SIZE (f))))
     335              :           {
     336       147412 :             tree t = TREE_TYPE (f);
     337       147412 :             if (!TREE_CHAIN (f)
     338       147412 :                 && TREE_CODE (t) == ARRAY_TYPE)
     339         2592 :               t = TREE_TYPE  (t);
     340       147412 :             iterative_hash_canonical_type (t, hstate);
     341       147412 :             nf++;
     342              :           }
     343              : 
     344        42685 :       hstate.add_int (nf);
     345              :     }
     346              : 
     347      1766306 :   return hstate.end();
     348              : }
     349              : 
     350              : /* Returning a hash value for gimple type TYPE combined with VAL.  */
     351              : 
     352              : static void
     353       354720 : iterative_hash_canonical_type (tree type, inchash::hash &hstate)
     354              : {
     355       354720 :   hashval_t v;
     356              : 
     357              :   /* All type variants have same TYPE_CANONICAL.  */
     358       354720 :   type = TYPE_MAIN_VARIANT (type);
     359              : 
     360       354720 :   if (!canonical_type_used_p (type))
     361        64740 :     v = hash_canonical_type (type);
     362              :   /* An already processed type.  */
     363       289980 :   else if (TYPE_CANONICAL (type))
     364              :     {
     365       289368 :       type = TYPE_CANONICAL (type);
     366       289368 :       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          612 :       v = hash_canonical_type (type);
     375          612 :       v = gimple_register_canonical_type_1 (type, v);
     376              :     }
     377       354720 :   hstate.merge_hash (v);
     378       354720 : }
     379              : 
     380              : /* Returns the hash for a canonical type P.  */
     381              : 
     382              : static hashval_t
     383       289368 : gimple_canonical_type_hash (const void *p)
     384              : {
     385       289368 :   num_canonical_type_hash_queries++;
     386       289368 :   hashval_t *slot = canonical_type_hash_cache->get ((const_tree) p);
     387       289368 :   gcc_assert (slot != NULL);
     388       289368 :   return *slot;
     389              : }
     390              : 
     391              : 
     392              : 
     393              : /* Returns nonzero if P1 and P2 are equal.  */
     394              : 
     395              : static int
     396       800633 : gimple_canonical_type_eq (const void *p1, const void *p2)
     397              : {
     398       800633 :   const_tree t1 = (const_tree) p1;
     399       800633 :   const_tree t2 = (const_tree) p2;
     400       800633 :   return gimple_canonical_types_compatible_p (const_cast<tree> (t1),
     401       800633 :                                               const_cast<tree> (t2));
     402              : }
     403              : 
     404              : /* Main worker for gimple_register_canonical_type.  */
     405              : 
     406              : static hashval_t
     407      1701566 : gimple_register_canonical_type_1 (tree t, hashval_t hash)
     408              : {
     409      1701566 :   void **slot;
     410              : 
     411      1701566 :   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        42685 :   if (RECORD_OR_UNION_TYPE_P (t) && odr_type_p (t)
     424      1712227 :       && TYPE_CXX_ODR_P (t) && !odr_type_violation_reported_p (t))
     425              :     {
     426              :       /* Anonymous namespace types never conflict with non-C++ types.  */
     427        10509 :       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        10068 :           gcc_checking_assert (type_streaming_finished
     435              :                                && TYPE_MAIN_VARIANT (t) == t);
     436        10068 :           slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash,
     437              :                                            NO_INSERT);
     438              :         }
     439        10068 :       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        10485 :           tree prevail = prevailing_odr_type (t);
     461              : 
     462        10485 :           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        10485 :           set_type_canonical_for_odr_type (t, prevail);
     474        10485 :           enable_odr_based_tbaa (t);
     475        10485 :           if (!type_in_anonymous_namespace_p (t))
     476        10044 :             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        10485 :           num_canonical_type_hash_entries++;
     485        10485 :           bool existed_p = canonical_type_hash_cache->put (prevail, hash);
     486        10485 :           gcc_checking_assert (!existed_p);
     487              :         }
     488        10509 :       return hash;
     489              :     }
     490              : 
     491      1691057 :   slot = htab_find_slot_with_hash (gimple_canonical_types, t, hash, INSERT);
     492      1691057 :   if (*slot)
     493              :     {
     494       800510 :       tree new_type = (tree)(*slot);
     495       800510 :       gcc_checking_assert (new_type != t);
     496       800510 :       TYPE_CANONICAL (t) = new_type;
     497              :     }
     498              :   else
     499              :     {
     500       890547 :       TYPE_CANONICAL (t) = t;
     501       890547 :       *slot = (void *) t;
     502              :       /* Cache the just computed hash value.  */
     503       890547 :       num_canonical_type_hash_entries++;
     504       890547 :       bool existed_p = canonical_type_hash_cache->put (t, hash);
     505       890547 :       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      3943750 : gimple_register_canonical_type (tree t)
     522              : {
     523      6581836 :   if (TYPE_CANONICAL (t) || !type_with_alias_set_p (t)
     524      3943750 :       || !canonical_type_used_p (t))
     525              :     return;
     526              : 
     527              :   /* Canonical types are same among all complete variants.  */
     528      1823769 :   if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (t)))
     529       122815 :     TYPE_CANONICAL (t) = TYPE_CANONICAL (TYPE_MAIN_VARIANT (t));
     530              :   else
     531              :     {
     532      1700954 :       hashval_t h = hash_canonical_type (TYPE_MAIN_VARIANT (t));
     533      1700954 :       gimple_register_canonical_type_1 (TYPE_MAIN_VARIANT (t), h);
     534      1700954 :       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      8486816 : lto_register_canonical_types (tree node, bool first_p)
     542              : {
     543      8486816 :   if (!node
     544      8037994 :       || !TYPE_P (node))
     545              :     return;
     546              : 
     547      6977142 :   if (first_p)
     548      3488571 :     TYPE_CANONICAL (node) = NULL_TREE;
     549              : 
     550      6977142 :   if (POINTER_TYPE_P (node)
     551              :       || TREE_CODE (node) == COMPLEX_TYPE
     552              :       || TREE_CODE (node) == ARRAY_TYPE)
     553       938446 :     lto_register_canonical_types (TREE_TYPE (node), first_p);
     554              : 
     555      6977142 :  if (!first_p)
     556      3488571 :     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        20401 : lto_register_canonical_types_for_odr_types ()
     566              : {
     567        20401 :   tree t;
     568        20401 :   unsigned int i;
     569              : 
     570        20401 :   if (!types_to_register)
     571        20401 :     return;
     572              : 
     573         1689 :   type_streaming_finished = true;
     574              : 
     575              :   /* Be sure that no types derived from ODR types was
     576              :      not inserted into the hash table.  */
     577         1689 :   if (flag_checking)
     578        21252 :     FOR_EACH_VEC_ELT (*types_to_register, i, t)
     579        19563 :       gcc_assert (!TYPE_CANONICAL (t));
     580              : 
     581              :   /* Register all remaining types.  */
     582        21252 :   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        19563 :       TYPE_CXX_ODR_P (t) = TYPE_CXX_ODR_P (TYPE_MAIN_VARIANT (t));
     591        19563 :       if (!TYPE_CANONICAL (t))
     592        13060 :         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       522981 : mentions_vars_p_typed (tree t)
     615              : {
     616       522981 :   CHECK_NO_VAR (TREE_TYPE (t));
     617       522981 :   return false;
     618              : }
     619              : 
     620              : /* Check presence of pointers to decls in fields of a tree_common T.  */
     621              : 
     622              : static inline bool
     623       517461 : mentions_vars_p_common (tree t)
     624              : {
     625       517461 :   if (mentions_vars_p_typed (t))
     626              :     return true;
     627       517461 :   CHECK_NO_VAR (TREE_CHAIN (t));
     628       517461 :   return false;
     629              : }
     630              : 
     631              : /* Check presence of pointers to decls in fields of a decl_minimal T.  */
     632              : 
     633              : static inline bool
     634       229855 : mentions_vars_p_decl_minimal (tree t)
     635              : {
     636       229855 :   if (mentions_vars_p_common (t))
     637              :     return true;
     638       229855 :   CHECK_NO_VAR (DECL_NAME (t));
     639       229855 :   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       229855 : mentions_vars_p_decl_common (tree t)
     647              : {
     648       229855 :   if (mentions_vars_p_decl_minimal (t))
     649              :     return true;
     650       211687 :   CHECK_VAR (DECL_SIZE (t));
     651       211687 :   CHECK_VAR (DECL_SIZE_UNIT (t));
     652       211687 :   CHECK_VAR (DECL_INITIAL (t));
     653       211687 :   CHECK_NO_VAR (DECL_ATTRIBUTES (t));
     654       211687 :   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       184669 : mentions_vars_p_decl_with_vis (tree t)
     662              : {
     663       184669 :   if (mentions_vars_p_decl_common (t))
     664              :     return true;
     665              : 
     666              :   /* Accessor macro has side-effects, use field-name here.  */
     667       163509 :   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       138358 : mentions_vars_p_decl_non_common (tree t)
     675              : {
     676       138358 :   if (mentions_vars_p_decl_with_vis (t))
     677              :     return true;
     678       134281 :   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       125572 : mentions_vars_p_function (tree t)
     686              : {
     687       125572 :   if (mentions_vars_p_decl_non_common (t))
     688              :     return true;
     689       121835 :   CHECK_NO_VAR (DECL_ARGUMENTS (t));
     690       121835 :   CHECK_NO_VAR (DECL_VINDEX (t));
     691       121835 :   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        41858 : mentions_vars_p_field_decl (tree t)
     699              : {
     700        41858 :   if (mentions_vars_p_decl_common (t))
     701              :     return true;
     702        41858 :   CHECK_VAR (DECL_FIELD_OFFSET (t));
     703        41858 :   CHECK_NO_VAR (DECL_BIT_FIELD_TYPE (t));
     704        41858 :   CHECK_NO_VAR (DECL_QUALIFIER (t));
     705        41858 :   CHECK_NO_VAR (DECL_FIELD_BIT_OFFSET (t));
     706        41858 :   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       282870 : mentions_vars_p_type (tree t)
     714              : {
     715       282870 :   if (mentions_vars_p_common (t))
     716              :     return true;
     717       282870 :   CHECK_NO_VAR (TYPE_CACHED_VALUES (t));
     718       282870 :   CHECK_VAR (TYPE_SIZE (t));
     719       282870 :   CHECK_VAR (TYPE_SIZE_UNIT (t));
     720       282870 :   CHECK_NO_VAR (TYPE_ATTRIBUTES (t));
     721       282870 :   CHECK_NO_VAR (TYPE_NAME (t));
     722              : 
     723       282870 :   CHECK_VAR (TYPE_MIN_VALUE_RAW (t));
     724       282870 :   CHECK_VAR (TYPE_MAX_VALUE_RAW (t));
     725              : 
     726              :   /* Accessor is for derived node types only.  */
     727       282870 :   CHECK_NO_VAR (TYPE_LANG_SLOT_1 (t));
     728              : 
     729       282870 :   CHECK_VAR (TYPE_CONTEXT (t));
     730       282149 :   CHECK_NO_VAR (TYPE_CANONICAL (t));
     731       282149 :   CHECK_NO_VAR (TYPE_MAIN_VARIANT (t));
     732       282149 :   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         4656 : mentions_vars_p_binfo (tree t)
     740              : {
     741         4656 :   unsigned HOST_WIDE_INT i, n;
     742              : 
     743         4656 :   if (mentions_vars_p_common (t))
     744              :     return true;
     745         4656 :   CHECK_VAR (BINFO_VTABLE (t));
     746         4656 :   CHECK_NO_VAR (BINFO_OFFSET (t));
     747         4656 :   CHECK_NO_VAR (BINFO_VIRTUALS (t));
     748         4656 :   CHECK_NO_VAR (BINFO_VPTR_FIELD (t));
     749         4656 :   n = vec_safe_length (BINFO_BASE_ACCESSES (t));
     750         4656 :   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         4656 :   n = BINFO_N_BASE_BINFOS (t);
     755        10284 :   for (i = 0; i < n; i++)
     756         5628 :     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         5379 : mentions_vars_p_expr (tree t)
     783              : {
     784         5379 :   int i;
     785         5379 :   if (mentions_vars_p_typed (t))
     786              :     return true;
     787        10889 :   for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
     788         8130 :     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       738592 : mentions_vars_p (tree t)
     809              : {
     810       738592 :   switch (TREE_CODE (t))
     811              :     {
     812              :     case IDENTIFIER_NODE:
     813              :       break;
     814              : 
     815       153088 :     case TREE_LIST:
     816       153088 :       CHECK_VAR (TREE_VALUE (t));
     817       152898 :       CHECK_VAR (TREE_PURPOSE (t));
     818       152895 :       CHECK_NO_VAR (TREE_CHAIN (t));
     819              :       break;
     820              : 
     821        41858 :     case FIELD_DECL:
     822        41858 :       return mentions_vars_p_field_decl (t);
     823              : 
     824         3328 :     case LABEL_DECL:
     825         3328 :     case CONST_DECL:
     826         3328 :     case PARM_DECL:
     827         3328 :     case RESULT_DECL:
     828         3328 :     case IMPORTED_DECL:
     829         3328 :     case NAMESPACE_DECL:
     830         3328 :     case NAMELIST_DECL:
     831         3328 :       return mentions_vars_p_decl_common (t);
     832              : 
     833        46311 :     case VAR_DECL:
     834        46311 :       return mentions_vars_p_decl_with_vis (t);
     835              : 
     836        12786 :     case TYPE_DECL:
     837        12786 :       return mentions_vars_p_decl_non_common (t);
     838              : 
     839       125572 :     case FUNCTION_DECL:
     840       125572 :       return mentions_vars_p_function (t);
     841              : 
     842         4656 :     case TREE_BINFO:
     843         4656 :       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       312864 :     default:
     861       312864 :       if (TYPE_P (t))
     862              :         {
     863       282870 :           if (mentions_vars_p_type (t))
     864              :             return true;
     865              :         }
     866        29994 :       else if (EXPR_P (t))
     867              :         {
     868         5379 :           if (mentions_vars_p_expr (t))
     869              :             return true;
     870              :         }
     871        24615 :       else if (CONSTANT_CLASS_P (t))
     872        24615 :         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       134353 : get_resolution (class data_in *data_in, unsigned index)
     884              : {
     885       134353 :   if (data_in->globals_resolution.exists ())
     886              :     {
     887       134353 :       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       134353 :       if (data_in->globals_resolution.length () <= index)
     892              :         return LDPR_UNKNOWN;
     893        68383 :       ret = data_in->globals_resolution[index];
     894        68383 :       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       134353 : register_resolution (struct lto_file_decl_data *file_data, tree decl,
     904              :                      enum ld_plugin_symbol_resolution resolution)
     905              : {
     906       134353 :   bool existed;
     907       134353 :   if (resolution == LDPR_UNKNOWN)
     908        67094 :     return;
     909        67259 :   if (!file_data->resolution_map)
     910         8538 :     file_data->resolution_map
     911         8538 :       = new hash_map<tree, ld_plugin_symbol_resolution>;
     912        67259 :   ld_plugin_symbol_resolution_t &res
     913        67259 :      = file_data->resolution_map->get_or_insert (decl, &existed);
     914        67259 :   if (!existed
     915              :       || resolution == LDPR_PREVAILING_DEF_IRONLY
     916           11 :       || resolution == LDPR_PREVAILING_DEF
     917            3 :       || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
     918        67259 :     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        95921 : lto_register_var_decl_in_symtab (class data_in *data_in, tree decl,
     927              :                                  unsigned ix)
     928              : {
     929        95921 :   tree context;
     930              : 
     931              :   /* Variable has file scope, not local.  */
     932        95921 :   if (!TREE_PUBLIC (decl)
     933       145238 :       && !((context = decl_function_context (decl))
     934        49317 :            && auto_var_in_fn_p (decl, context)))
     935        71763 :     rest_of_decl_compilation (decl, 1, 0);
     936              : 
     937              :   /* If this variable has already been declared, queue the
     938              :      declaration for merging.  */
     939        95921 :   if (TREE_PUBLIC (decl))
     940        23825 :     register_resolution (data_in->file_data,
     941              :                          decl, get_resolution (data_in, ix));
     942        95921 : }
     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       183235 : 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       183235 :   if (TREE_PUBLIC (decl) && !DECL_ABSTRACT_P (decl))
     957       110528 :     register_resolution (data_in->file_data,
     958              :                          decl, get_resolution (data_in, ix));
     959       183235 : }
     960              : 
     961              : /* Check if T is a decl and needs register its resolution info.  */
     962              : 
     963              : static void
     964      1369973 : lto_maybe_register_decl (class data_in *data_in, tree t, unsigned ix)
     965              : {
     966      1369973 :   if (VAR_P (t))
     967        95921 :     lto_register_var_decl_in_symtab (data_in, t, ix);
     968      1274052 :   else if (TREE_CODE (t) == FUNCTION_DECL
     969      1274052 :            && !fndecl_built_in_p (t))
     970       183235 :     lto_register_function_decl_in_symtab (data_in, t, ix);
     971      1369973 : }
     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       474694 : 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       474694 :   if (TYPE_MAIN_VARIANT (t) != t)
     987              :     {
     988        80982 :       tree mv = TYPE_MAIN_VARIANT (t);
     989        80982 :       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
     990        80982 :       TYPE_NEXT_VARIANT (mv) = t;
     991              :     }
     992       393712 :   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       368785 :       if (TREE_CODE (t) == POINTER_TYPE)
    1002              :         {
    1003       151519 :           TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
    1004       151519 :           TYPE_POINTER_TO (TREE_TYPE (t)) = t;
    1005              :         }
    1006       217266 :       else if (TREE_CODE (t) == REFERENCE_TYPE && !TYPE_REF_IS_RVALUE (t))
    1007              :         {
    1008         8811 :           TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
    1009         8811 :           TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
    1010              :         }
    1011              :     }
    1012       474694 : }
    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       151260 : tree_scc_hasher::hash (const tree_scc *scc)
    1046              : {
    1047       151260 :   return scc->hash;
    1048              : }
    1049              : 
    1050              : bool
    1051       318021 : tree_scc_hasher::equal (const tree_scc *scc1, const tree_scc *scc2)
    1052              : {
    1053       318021 :   if (scc1->hash != scc2->hash
    1054       144607 :       || scc1->len != scc2->len
    1055       144607 :       || 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       153804 : compare_tree_sccs_1 (tree t1, tree t2, tree **map)
    1082              : {
    1083       153804 :   enum tree_code code;
    1084              : 
    1085              :   /* Mark already visited nodes.  */
    1086       153804 :   TREE_ASM_WRITTEN (t2) = 1;
    1087              : 
    1088              :   /* Push the pair onto map.  */
    1089       153804 :   (*map)[0] = t1;
    1090       153804 :   (*map)[1] = t2;
    1091       153804 :   *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       153804 :   compare_values (TREE_CODE);
    1101       153804 :   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       153804 :   gcc_checking_assert (code != TRANSLATION_UNIT_DECL);
    1106              : 
    1107       153804 :   if (!TYPE_P (t1))
    1108              :     {
    1109       140623 :       compare_values (TREE_SIDE_EFFECTS);
    1110       140623 :       compare_values (TREE_CONSTANT);
    1111       140623 :       compare_values (TREE_READONLY);
    1112       140623 :       compare_values (TREE_PUBLIC);
    1113              :     }
    1114       153804 :   compare_values (TREE_ADDRESSABLE);
    1115       153804 :   compare_values (TREE_THIS_VOLATILE);
    1116       153804 :   if (DECL_P (t1))
    1117         9433 :     compare_values (DECL_UNSIGNED);
    1118       144371 :   else if (TYPE_P (t1))
    1119        13181 :     compare_values (TYPE_UNSIGNED);
    1120       153804 :   if (TYPE_P (t1))
    1121        13181 :     compare_values (TYPE_ARTIFICIAL);
    1122              :   else
    1123       140623 :     compare_values (TREE_NO_WARNING);
    1124       153804 :   compare_values (TREE_NOTHROW);
    1125       153804 :   compare_values (TREE_STATIC);
    1126       153804 :   if (code != TREE_BINFO)
    1127       153435 :     compare_values (TREE_PRIVATE);
    1128       153804 :   compare_values (TREE_PROTECTED);
    1129       153804 :   compare_values (TREE_DEPRECATED);
    1130       153804 :   if (TYPE_P (t1))
    1131              :     {
    1132        13181 :       if (AGGREGATE_TYPE_P (t1))
    1133         4354 :         compare_values (TYPE_REVERSE_STORAGE_ORDER);
    1134              :       else
    1135         8827 :         compare_values (TYPE_SATURATING);
    1136        13181 :       compare_values (TYPE_ADDR_SPACE);
    1137              :     }
    1138       140623 :   else if (code == SSA_NAME)
    1139            0 :     compare_values (SSA_NAME_IS_DEFAULT_DEF);
    1140              : 
    1141       153804 :   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       153804 :   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       153804 :   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       153804 :   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       153804 :   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       153804 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
    1220         1731 :     compare_values (DECL_REGISTER);
    1221              : 
    1222       153804 :   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       153804 :   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       153804 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    1267              :     {
    1268        13181 :       compare_values (TYPE_MODE);
    1269        13181 :       compare_values (TYPE_NEEDS_CONSTRUCTING);
    1270        13181 :       if (RECORD_OR_UNION_TYPE_P (t1))
    1271              :         {
    1272         3948 :           compare_values (TYPE_TRANSPARENT_AGGR);
    1273         3948 :           compare_values (TYPE_FINAL_P);
    1274         3948 :           compare_values (TYPE_CXX_ODR_P);
    1275              :         }
    1276         9233 :       else if (code == ARRAY_TYPE)
    1277          406 :         compare_values (TYPE_NONALIASED_COMPONENT);
    1278        13181 :       if (code == ARRAY_TYPE || code == INTEGER_TYPE)
    1279         1783 :         compare_values (TYPE_STRING_FLAG);
    1280        13181 :       if (AGGREGATE_TYPE_P (t1))
    1281         4354 :         compare_values (TYPE_TYPELESS_STORAGE);
    1282        13181 :       compare_values (TYPE_EMPTY_P);
    1283        13181 :       if (FUNC_OR_METHOD_TYPE_P (t1))
    1284         2025 :         compare_values (TYPE_NO_NAMED_ARGS_STDARG_P);
    1285        13181 :       if (RECORD_OR_UNION_TYPE_P (t1))
    1286         3948 :         compare_values (TYPE_INCLUDES_FLEXARRAY);
    1287        13181 :       compare_values (TYPE_PACKED);
    1288        13181 :       compare_values (TYPE_RESTRICT);
    1289        13181 :       compare_values (TYPE_USER_ALIGN);
    1290        13181 :       compare_values (TYPE_READONLY);
    1291        13181 :       compare_values (TYPE_PRECISION_RAW);
    1292        13181 :       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       153804 :   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
    1304              :     return false;
    1305              : 
    1306       153804 :   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       153804 :   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       153804 :   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       153804 :   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
    1321          369 :     if (vec_safe_length (BINFO_BASE_ACCESSES (t1))
    1322          369 :         != vec_safe_length (BINFO_BASE_ACCESSES (t2)))
    1323              :       return false;
    1324              : 
    1325       153804 :   if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
    1326              :     {
    1327          194 :       compare_values (CLOBBER_KIND);
    1328          508 :       compare_values (CONSTRUCTOR_NELTS);
    1329              :     }
    1330              : 
    1331       153804 :   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       153804 :   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       153804 :   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       153802 :   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       153802 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    1403              :     {
    1404       152077 :       if (code != IDENTIFIER_NODE)
    1405       152077 :         compare_tree_edges (TREE_TYPE (t1), TREE_TYPE (t2));
    1406              :     }
    1407              : 
    1408       153802 :   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       153802 :   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       153802 :   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       153802 :   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       153802 :   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       153802 :   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       153802 :   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       153802 :   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       153802 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    1507              :     {
    1508        13181 :       compare_tree_edges (TYPE_SIZE (t1), TYPE_SIZE (t2));
    1509        13181 :       compare_tree_edges (TYPE_SIZE_UNIT (t1), TYPE_SIZE_UNIT (t2));
    1510        13181 :       compare_tree_edges (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2));
    1511        13181 :       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        13181 :       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        13181 :       if (TYPE_FILE_SCOPE_P (t1) && TYPE_FILE_SCOPE_P (t2))
    1521              :         ;
    1522              :       else
    1523          409 :         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        13181 :       compare_tree_edges (TYPE_STUB_DECL (t1), TYPE_STUB_DECL (t2));
    1527              :     }
    1528              : 
    1529       153802 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    1530              :     {
    1531        13181 :       if (code == ARRAY_TYPE)
    1532          406 :         compare_tree_edges (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2));
    1533        12775 :       else if (RECORD_OR_UNION_TYPE_P (t1))
    1534              :         {
    1535         3948 :           tree f1, f2;
    1536         3948 :           for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
    1537        12697 :                f1 || f2;
    1538         8749 :                f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
    1539         8749 :             compare_tree_edges (f1, f2);
    1540              :         }
    1541         8827 :       else if (code == FUNCTION_TYPE
    1542         8827 :                || code == METHOD_TYPE)
    1543         2025 :         compare_tree_edges (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2));
    1544              : 
    1545        13181 :       if (!POINTER_TYPE_P (t1))
    1546         7852 :         compare_tree_edges (TYPE_MIN_VALUE_RAW (t1), TYPE_MIN_VALUE_RAW (t2));
    1547        13181 :       compare_tree_edges (TYPE_MAX_VALUE_RAW (t1), TYPE_MAX_VALUE_RAW (t2));
    1548              :     }
    1549              : 
    1550       153802 :   if (CODE_CONTAINS_STRUCT (code, TS_LIST))
    1551              :     {
    1552       126885 :       compare_tree_edges (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
    1553       126885 :       compare_tree_edges (TREE_VALUE (t1), TREE_VALUE (t2));
    1554       126885 :       compare_tree_edges (TREE_CHAIN (t1), TREE_CHAIN (t2));
    1555              :     }
    1556              : 
    1557       153802 :   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       153802 :   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       153802 :   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
    1573              :     {
    1574              :       unsigned i;
    1575              :       tree t;
    1576              :       /* Lengths have already been compared above.  */
    1577          397 :       FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t1), i, t)
    1578           28 :         compare_tree_edges (t, BINFO_BASE_BINFO (t2, i));
    1579          369 :       FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t1), i, t)
    1580            0 :         compare_tree_edges (t, BINFO_BASE_ACCESS (t2, i));
    1581          369 :       compare_tree_edges (BINFO_OFFSET (t1), BINFO_OFFSET (t2));
    1582          369 :       compare_tree_edges (BINFO_VTABLE (t1), BINFO_VTABLE (t2));
    1583          369 :       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       153802 :   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       153801 :   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       144607 : 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       144610 :   for (unsigned i = 0; i < pscc->entry_len; ++i)
    1627              :     {
    1628       144607 :       tree *mapp = map;
    1629       144607 :       num_scc_compare_collisions++;
    1630       144607 :       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       144604 :           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       133745 : cmp_tree (const void *p1_, const void *p2_)
    1650              : {
    1651       133745 :   tree *p1 = (tree *)(const_cast<void *>(p1_));
    1652       133745 :   tree *p2 = (tree *)(const_cast<void *>(p2_));
    1653       133745 :   if (p1[1] == p2[1])
    1654              :     return 0;
    1655       133745 :   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      1368621 : process_dref (class data_in *data_in, tree t, unsigned from)
    1663              : {
    1664      1368621 :   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      1368621 :   if (dref_queue.length () == 1)
    1669              :     {
    1670        12497 :       dref_entry e = dref_queue.pop ();
    1671        12497 :       gcc_assert (e.decl
    1672              :                   == streamer_tree_cache_get_tree (cache, from));
    1673        12497 :       const char *sym;
    1674        12497 :       unsigned HOST_WIDE_INT off;
    1675        12497 :       if (!debug_hooks->die_ref_for_decl (t, &sym, &off))
    1676        12486 :         debug_hooks->register_external_die (t, e.sym, e.off);
    1677              :     }
    1678      1368621 : }
    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       744647 : unify_scc (class data_in *data_in, unsigned from,
    1686              :            unsigned len, unsigned scc_entry_len, hashval_t scc_hash)
    1687              : {
    1688       744647 :   bool unified_p = false;
    1689       744647 :   struct streamer_tree_cache_d *cache = data_in->reader_cache;
    1690       744647 :   tree_scc *scc
    1691       744647 :     = (tree_scc *) alloca (sizeof (tree_scc) + (len - 1) * sizeof (tree));
    1692       744647 :   scc->next = NULL;
    1693       744647 :   scc->hash = scc_hash;
    1694       744647 :   scc->len = len;
    1695       744647 :   scc->entry_len = scc_entry_len;
    1696      1581670 :   for (unsigned i = 0; i < len; ++i)
    1697              :     {
    1698       837023 :       tree t = streamer_tree_cache_get_tree (cache, from + i);
    1699       837023 :       scc->entries[i] = t;
    1700              :       /* These types should be streamed as unshared.  */
    1701       837023 :       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       744647 :   tree_scc **slot;
    1714       744647 :   slot = tree_scc_hash->find_slot_with_hash (scc, scc_hash, INSERT);
    1715       744647 :   if (*slot)
    1716              :     {
    1717              :       /* Try unifying against each candidate.  */
    1718       144607 :       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       298411 :       for (unsigned i = 0; i < scc->len; ++i)
    1727              :         {
    1728       153804 :           TREE_VISITED (scc->entries[i]) = 1;
    1729       153804 :           gcc_checking_assert (!TREE_ASM_WRITTEN (scc->entries[i]));
    1730              :         }
    1731              : 
    1732       144607 :       tree *map = XALLOCAVEC (tree, 2 * len);
    1733       144610 :       for (tree_scc *pscc = *slot; pscc; pscc = pscc->next)
    1734              :         {
    1735       144607 :           if (!compare_tree_sccs (pscc, scc, map))
    1736            3 :             continue;
    1737              : 
    1738              :           /* Found an equal SCC.  */
    1739       144604 :           unified_p = true;
    1740       144604 :           num_scc_compare_collisions--;
    1741       144604 :           num_sccs_merged++;
    1742       144604 :           total_scc_size_merged += len;
    1743              : 
    1744       144604 :           if (flag_checking)
    1745       298405 :             for (unsigned i = 0; i < len; ++i)
    1746              :               {
    1747       153801 :                 tree t = map[2*i+1];
    1748       153801 :                 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       153801 :                 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       144604 :           if (len == 1)
    1759              :             {
    1760       141333 :               process_dref (data_in, pscc->entries[0], from);
    1761       141333 :               lto_maybe_register_decl (data_in, pscc->entries[0], from);
    1762       141333 :               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       144604 :           data_in->location_cache.revert_location_cache ();
    1785       298405 :           for (unsigned i = 0; i < len; ++i)
    1786              :             {
    1787       153801 :               if (TYPE_P (scc->entries[i]))
    1788        13181 :                 num_merged_types++;
    1789       153801 :               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       144604 :           dref_queue.truncate (0);
    1796              : 
    1797       144604 :           break;
    1798              :         }
    1799              : 
    1800              :       /* Reset TREE_VISITED if we didn't unify the SCC with another.  */
    1801       144604 :       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       144607 :   if (!unified_p)
    1809              :     {
    1810       600043 :       tree_scc *pscc
    1811       600043 :         = XOBNEWVAR (&tree_scc_hash_obstack, tree_scc, sizeof (tree_scc));
    1812       600043 :       memcpy (pscc, scc, sizeof (tree_scc));
    1813       600043 :       pscc->next = (*slot);
    1814       600043 :       *slot = pscc;
    1815              :     }
    1816       744647 :   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      1222283 : 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      1222283 :   if (TYPE_P (t))
    1831              :     {
    1832              :       /* Map the tree types to their frequencies.  */
    1833       474694 :       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       474694 :       num_prevailing_types++;
    1842       474694 :       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       474694 :       if (!TYPE_CANONICAL (t))
    1854              :         {
    1855       474680 :           if (!RECORD_OR_UNION_TYPE_P (t)
    1856       474680 :               || !TYPE_CXX_ODR_P (t))
    1857       442119 :             gimple_register_canonical_type (t);
    1858        32561 :           else if (COMPLETE_TYPE_P (t))
    1859        19563 :             vec_safe_push (types_to_register, t);
    1860              :         }
    1861       474694 :       if (TYPE_MAIN_VARIANT (t) == t && odr_type_p (t))
    1862        21092 :         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      1222283 :   if (TREE_CODE (t) == INTEGER_CST
    1867      1222283 :       && !TREE_OVERFLOW (t))
    1868        34426 :     cache_integer_cst (t);
    1869      1222283 :   if (!flag_ltrans)
    1870              :     {
    1871       738592 :       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       738592 :       if (mentions_vars_p (t))
    1875        27833 :         vec_safe_push (tree_with_vars, t);
    1876              :     }
    1877      1222283 : }
    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        21456 : lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
    1885              :                 vec<ld_plugin_symbol_resolution_t> resolutions)
    1886              : {
    1887        21456 :   const struct lto_decl_header *header = (const struct lto_decl_header *) data;
    1888        21456 :   const int decl_offset = sizeof (struct lto_decl_header);
    1889        21456 :   const int main_offset = decl_offset + header->decl_state_size;
    1890        21456 :   const int string_offset = main_offset + header->main_size;
    1891        21456 :   class data_in *data_in;
    1892        21456 :   unsigned int i;
    1893        21456 :   const uint32_t *data_ptr, *data_end;
    1894        21456 :   uint32_t num_decl_states;
    1895              : 
    1896        21456 :   lto_input_block ib_main ((const char *) data + main_offset,
    1897        21456 :                            header->main_size, decl_data);
    1898              : 
    1899        42912 :   data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
    1900        21456 :                                 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        21456 :   hash_map <code_id_hash, unsigned> hm;
    1906        21456 :   unsigned total = 0;
    1907              : 
    1908              :   /* Read the global declarations and types.  */
    1909      2008394 :   while (ib_main.p < ib_main.len)
    1910              :     {
    1911      1986938 :       tree t;
    1912      1986938 :       unsigned from = data_in->reader_cache->nodes.length ();
    1913              :       /* Read and uniquify SCCs as in the input stream.  */
    1914      1986938 :       enum LTO_tags tag = streamer_read_record_start (&ib_main);
    1915      1986938 :       if (tag == LTO_tree_scc || tag == LTO_trees)
    1916              :         {
    1917       759650 :           unsigned len_;
    1918       759650 :           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       759650 :           hashval_t scc_hash = lto_input_scc (&ib_main, data_in, &len_,
    1923              :                                               &scc_entry_len,
    1924              :                                               tag == LTO_tree_scc);
    1925       759650 :           unsigned len = data_in->reader_cache->nodes.length () - from;
    1926       759650 :           gcc_assert (len == len_);
    1927              : 
    1928       759650 :           if (tag == LTO_tree_scc)
    1929              :             {
    1930       744647 :               total_scc_size += len;
    1931       744647 :               num_sccs_read++;
    1932              :             }
    1933              :           else
    1934        15003 :             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       759650 :           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       759650 :           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       745377 :           if (!flag_ltrans && tag != LTO_trees
    1950      1504297 :               && unify_scc (data_in, from,
    1951              :                             len, scc_entry_len, scc_hash))
    1952       144604 :             continue;
    1953              : 
    1954              :           /* Tree merging failed, mark entries in location cache as
    1955              :              permanent.  */
    1956       615046 :           data_in->location_cache.accept_location_cache ();
    1957              : 
    1958       615046 :           bool seen_type = false;
    1959      1359749 :           for (unsigned i = 0; i < len; ++i)
    1960              :             {
    1961       744703 :               tree t = streamer_tree_cache_get_tree (data_in->reader_cache,
    1962              :                                                      from + i);
    1963       744703 :               process_new_tree (t, &hm, from + i, &total, data_in);
    1964       744703 :               if (TYPE_P (t))
    1965       296002 :                 seen_type = true;
    1966              :             }
    1967              : 
    1968              :           /* Register DECLs with the debuginfo machinery.  */
    1969       626594 :           while (!dref_queue.is_empty ())
    1970              :             {
    1971        11548 :               dref_entry e = dref_queue.pop ();
    1972        11548 :               debug_hooks->register_external_die (e.decl, e.sym, e.off);
    1973              :             }
    1974              : 
    1975       615046 :           if (seen_type)
    1976       294463 :             num_type_scc_trees += len;
    1977              :         }
    1978              :       else
    1979              :         {
    1980      1227288 :           t = lto_input_tree_1 (&ib_main, data_in, tag, 0);
    1981      2454576 :           gcc_assert (data_in->reader_cache->nodes.length () == from + 1);
    1982      1227288 :           num_unshared_trees_read++;
    1983      1227288 :           data_in->location_cache.accept_location_cache ();
    1984      1227288 :           process_dref (data_in, t, from);
    1985      1227288 :           if (TREE_CODE (t) == IDENTIFIER_NODE
    1986      1227288 :               || (TREE_CODE (t) == INTEGER_CST
    1987       178433 :                   && !TREE_OVERFLOW (t)))
    1988              :             ;
    1989              :           else
    1990              :             {
    1991       477580 :               lto_maybe_register_decl (data_in, t, from);
    1992       477580 :               process_new_tree (t, &hm, from, &total, data_in);
    1993              :             }
    1994              :         }
    1995              :     }
    1996              : 
    1997              :   /* Dump type statistics.  */
    1998        21456 :   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        21456 :   data_in->location_cache.apply_location_cache ();
    2013              : 
    2014              :   /* Read in lto_in_decl_state objects.  */
    2015        21456 :   data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
    2016        21456 :   data_end
    2017        21456 :     = (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
    2018        21456 :   num_decl_states = *data_ptr++;
    2019              : 
    2020        21456 :   gcc_assert (num_decl_states > 0);
    2021        21456 :   decl_data->global_decl_state = lto_new_in_decl_state ();
    2022        21456 :   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        21456 :   decl_data->function_decl_states
    2027        21456 :     = hash_table<decl_state_hasher>::create_ggc (37);
    2028              : 
    2029       155928 :   for (i = 1; i < num_decl_states; i++)
    2030              :     {
    2031       134472 :       struct lto_in_decl_state *state = lto_new_in_decl_state ();
    2032              : 
    2033       134472 :       data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
    2034       134472 :       lto_in_decl_state **slot
    2035       134472 :         = decl_data->function_decl_states->find_slot (state, INSERT);
    2036       134472 :       gcc_assert (*slot == NULL);
    2037       134472 :       *slot = state;
    2038              :     }
    2039              : 
    2040        21456 :   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        21456 :   decl_data->current_decl_state = decl_data->global_decl_state;
    2045              : 
    2046        21456 :   lto_data_in_delete (data_in);
    2047        21456 : }
    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        21456 : 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        21456 :   unsigned int name_len;
    2084        21456 :   char *obj_name;
    2085        21456 :   unsigned int num_symbols;
    2086        21456 :   unsigned int i;
    2087        21456 :   struct lto_file_decl_data *file_data;
    2088        21456 :   splay_tree_node nd = NULL;
    2089              : 
    2090        21456 :   if (!resolution)
    2091        12875 :     return;
    2092              : 
    2093         8581 :   name_len = strlen (file->filename);
    2094         8581 :   obj_name = XNEWVEC (char, name_len + 1);
    2095         8581 :   fscanf (resolution, " ");   /* Read white space.  */
    2096              : 
    2097         8581 :   fread (obj_name, sizeof (char), name_len, resolution);
    2098         8581 :   obj_name[name_len] = '\0';
    2099         8581 :   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         8581 :   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         8581 :   free (obj_name);
    2116              : 
    2117         8581 :   fscanf (resolution, "%u", &num_symbols);
    2118              : 
    2119        76533 :   for (i = 0; i < num_symbols; i++)
    2120              :     {
    2121        67952 :       int t;
    2122        67952 :       unsigned index;
    2123        67952 :       unsigned HOST_WIDE_INT id;
    2124        67952 :       char r_str[27];
    2125        67952 :       enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
    2126        67952 :       unsigned int j;
    2127        67952 :       unsigned int lto_resolution_str_len = ARRAY_SIZE (lto_resolution_str);
    2128        67952 :       res_pair rp;
    2129              : 
    2130        67952 :       t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE
    2131              :                   " %26s %*[^\n]\n", &index, &id, r_str);
    2132        67952 :       if (t != 3)
    2133            0 :         internal_error ("invalid line in the resolution file");
    2134              : 
    2135       302758 :       for (j = 0; j < lto_resolution_str_len; j++)
    2136              :         {
    2137       302758 :           if (strcmp (lto_resolution_str[j], r_str) == 0)
    2138              :             {
    2139        67952 :               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        67952 :               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        67952 :       if (j == lto_resolution_str_len)
    2155            0 :         internal_error ("invalid resolution in the resolution file");
    2156              : 
    2157        67952 :       if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
    2158              :         {
    2159         8541 :           nd = lto_splay_tree_lookup (file_ids, id);
    2160         8541 :           if (nd == NULL)
    2161            0 :             internal_error ("resolution sub id %wx not in object file", id);
    2162              :         }
    2163              : 
    2164        67952 :       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        67952 :       rp.res = r;
    2168        67952 :       rp.index = index;
    2169        67952 :       file_data->respairs.safe_push (rp);
    2170        67952 :       if (file_data->max_index < index)
    2171        63034 :         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       384614 : lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
    2185              : {
    2186       384614 :   const char *s;
    2187              : 
    2188       384614 :   if (strncmp (name, section_name_prefix, strlen (section_name_prefix)))
    2189              :     return 0;
    2190              : 
    2191       384614 :   if (flag_ltrans)
    2192              :     {
    2193       113271 :       *id = 0;
    2194       113271 :       return 1;
    2195              :     }
    2196              : 
    2197       271343 :   s = strrchr (name, '.');
    2198       271343 :   if (!s)
    2199              :     return 0;
    2200              :   /* If the section is not suffixed with an ID return.  */
    2201       271343 :   if ((size_t)(s - name) == strlen (section_name_prefix))
    2202              :     return 0;
    2203       258077 :   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       384614 : create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
    2210              :                             struct file_data_list *list)
    2211              : {
    2212       384614 :   struct lto_section_slot s_slot, *new_slot;
    2213       384614 :   unsigned HOST_WIDE_INT id;
    2214       384614 :   splay_tree_node nd;
    2215       384614 :   void **hash_slot;
    2216       384614 :   char *new_name;
    2217       384614 :   struct lto_file_decl_data *file_data;
    2218              : 
    2219       384614 :   if (!lto_section_with_id (ls->name, &id))
    2220              :     return 1;
    2221              : 
    2222              :   /* Find hash table of sub module id.  */
    2223       371348 :   nd = lto_splay_tree_lookup (file_ids, id);
    2224       371348 :   if (nd != NULL)
    2225              :     {
    2226       349892 :       file_data = (struct lto_file_decl_data *)nd->value;
    2227              :     }
    2228              :   else
    2229              :     {
    2230        21456 :       file_data = ggc_alloc<lto_file_decl_data> ();
    2231        21456 :       memset(file_data, 0, sizeof (struct lto_file_decl_data));
    2232        21456 :       file_data->id = id;
    2233        21456 :       file_data->section_hash_table = lto_obj_create_section_hash_table ();
    2234        21456 :       lto_splay_tree_insert (file_ids, id, file_data);
    2235              : 
    2236              :       /* Maintain list in linker order.  */
    2237        21456 :       if (!list->first)
    2238        21456 :         list->first = file_data;
    2239        21456 :       if (list->last)
    2240            0 :         list->last->next = file_data;
    2241              : 
    2242        21456 :       list->last = file_data;
    2243              :     }
    2244              : 
    2245              :   /* Copy section into sub module hash table.  */
    2246       371348 :   new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
    2247       371348 :   s_slot.name = new_name;
    2248       371348 :   hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
    2249       371348 :   gcc_assert (*hash_slot == NULL);
    2250              : 
    2251       371348 :   new_slot = XDUP (struct lto_section_slot, ls);
    2252       371348 :   new_slot->name = new_name;
    2253       371348 :   *hash_slot = new_slot;
    2254       371348 :   return 1;
    2255              : }
    2256              : 
    2257              : /* Read declarations and other initializations for a FILE_DATA.  */
    2258              : 
    2259              : static void
    2260        21456 : lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file,
    2261              :                    int order)
    2262              : {
    2263        21456 :   const char *data;
    2264        21456 :   size_t len;
    2265        21456 :   vec<ld_plugin_symbol_resolution_t>
    2266        21456 :         resolutions = vNULL;
    2267        21456 :   int i;
    2268        21456 :   res_pair *rp;
    2269              : 
    2270              :   /* Create vector for fast access of resolution.  We do this lazily
    2271              :      to save memory.  */
    2272        21456 :   resolutions.safe_grow_cleared (file_data->max_index + 1, true);
    2273       110864 :   for (i = 0; file_data->respairs.iterate (i, &rp); i++)
    2274        67952 :     resolutions[rp->index] = rp->res;
    2275        21456 :   file_data->respairs.release ();
    2276              : 
    2277        21456 :   file_data->renaming_hash_table = lto_create_renaming_table ();
    2278        21456 :   file_data->file_name = file->filename;
    2279        21456 :   file_data->order = order;
    2280              : 
    2281              :   /* Read and verify LTO section.  */
    2282        21456 :   data = lto_get_summary_section_data (file_data, LTO_section_lto, &len);
    2283        21456 :   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        21456 :   memcpy (&file_data->lto_section_header, data, sizeof (lto_section));
    2291        21456 :   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        21456 :   file_data->mode_table = NULL;
    2299        21456 :   file_data->mode_bits = ceil_log2 (MAX_MACHINE_MODE);
    2300              : #endif
    2301              : 
    2302        21456 :   data = lto_get_summary_section_data (file_data, LTO_section_decls, &len);
    2303        21456 :   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        21456 :   lto_read_decls (file_data, data, resolutions);
    2311        21456 :   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        21456 : lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
    2318              :                            int *count, int order)
    2319              : {
    2320        21456 :   lto_file_finalize (file_data, file, order);
    2321        21456 :   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        21456 :   (*count)++;
    2326        21456 :   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        21456 : lto_file_read (lto_file *file, FILE *resolution_file, int *count)
    2338              : {
    2339        21456 :   struct lto_file_decl_data *file_data = NULL;
    2340        21456 :   splay_tree file_ids;
    2341        21456 :   htab_t section_hash_table;
    2342        21456 :   struct lto_section_slot *section;
    2343        21456 :   struct file_data_list file_list;
    2344        21456 :   struct lto_section_list section_list;
    2345              : 
    2346        21456 :   memset (&section_list, 0, sizeof (struct lto_section_list));
    2347        21456 :   section_hash_table = lto_obj_build_section_table (file, &section_list);
    2348              : 
    2349              :   /* Dump the details of LTO objects.  */
    2350        21456 :   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        21456 :   file_ids = lto_splay_tree_new ();
    2364        21456 :   memset (&file_list, 0, sizeof (struct file_data_list));
    2365       406070 :   for (section = section_list.first; section != NULL; section = section->next)
    2366       384614 :     create_subid_section_table (section, file_ids, &file_list);
    2367              : 
    2368              :   /* Add resolutions to file ids.  */
    2369        21456 :   lto_resolution_read (file_ids, resolution_file, file);
    2370              : 
    2371              :   /* Finalize each lto file for each submodule in the merged object.  */
    2372        21456 :   int order = 0;
    2373        42912 :   for (file_data = file_list.first; file_data != NULL;
    2374        21456 :        file_data = file_data->next)
    2375        21456 :     lto_create_files_from_ids (file, file_data, count, order++);
    2376              : 
    2377        21456 :   splay_tree_delete (file_ids);
    2378        21456 :   htab_delete (section_hash_table);
    2379              : 
    2380        21456 :   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       327568 : lto_read_section_data (struct lto_file_decl_data *file_data,
    2398              :                        off_t offset, size_t len)
    2399              : {
    2400       327568 :   char *result;
    2401       327568 :   static int fd = -1;
    2402       327568 :   static char *fd_name;
    2403              : #if LTO_MMAP_IO
    2404       327568 :   size_t computed_len;
    2405       327568 :   off_t computed_offset;
    2406       327568 :   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       327568 :   if (fd != -1
    2415       327568 :       && filename_cmp (fd_name, file_data->file_name) != 0)
    2416              :     {
    2417        15577 :       free (fd_name);
    2418        15577 :       close (fd);
    2419        15577 :       fd = -1;
    2420              :     }
    2421       327568 :   if (fd == -1)
    2422              :     {
    2423        35978 :       fd = open (file_data->file_name, O_RDONLY|O_BINARY);
    2424        35978 :       if (fd == -1)
    2425              :         {
    2426            0 :           fatal_error (input_location, "Cannot open %s", file_data->file_name);
    2427              :           return NULL;
    2428              :         }
    2429        35978 :       fd_name = xstrdup (file_data->file_name);
    2430              :     }
    2431              : 
    2432              : #if LTO_MMAP_IO
    2433       327568 :   if (!page_mask)
    2434              :     {
    2435        20401 :       size_t page_size = sysconf (_SC_PAGE_SIZE);
    2436        20401 :       page_mask = ~(page_size - 1);
    2437              :     }
    2438              : 
    2439       327568 :   computed_offset = offset & ((off_t) page_mask);
    2440       327568 :   diff = offset - computed_offset;
    2441       327568 :   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       327568 :   computed_len = (size_t) diff + len;
    2448              : 
    2449       327568 :   result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
    2450              :                           fd, computed_offset);
    2451       327568 :   if (result == MAP_FAILED)
    2452              :     {
    2453            0 :       fatal_error (input_location, "Cannot map %s", file_data->file_name);
    2454              :       return NULL;
    2455              :     }
    2456              : 
    2457       327568 :   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       390618 : 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       390618 :   htab_t section_hash_table = file_data->section_hash_table;
    2492       390618 :   struct lto_section_slot *f_slot;
    2493       390618 :   struct lto_section_slot s_slot;
    2494       390618 :   const char *section_name = lto_get_section_name (section_type, name,
    2495              :                                                    order, file_data);
    2496       390618 :   char *data = NULL;
    2497              : 
    2498       390618 :   *len = 0;
    2499       390618 :   s_slot.name = section_name;
    2500       390618 :   f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
    2501       390618 :   if (f_slot)
    2502              :     {
    2503       327568 :       data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
    2504       327568 :       *len = f_slot->len;
    2505              :     }
    2506              : 
    2507       390618 :   free (const_cast<char *> (section_name));
    2508       390618 :   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       298833 : 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       298833 :   intptr_t computed_len;
    2523       298833 :   intptr_t computed_offset;
    2524       298833 :   intptr_t diff;
    2525              : #endif
    2526              : 
    2527              : #if LTO_MMAP_IO
    2528       298833 :   computed_offset = ((intptr_t) offset) & page_mask;
    2529       298833 :   diff = (intptr_t) offset - computed_offset;
    2530       298833 :   computed_len = len + diff;
    2531              : 
    2532       298833 :   munmap ((caddr_t) computed_offset, computed_len);
    2533              : #else
    2534              :   free (const_cast<char *> (offset));
    2535              : #endif
    2536       298833 : }
    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        27833 : lto_fixup_prevailing_decls (tree t)
    2560              : {
    2561        27833 :   enum tree_code code = TREE_CODE (t);
    2562        27833 :   bool fixed = false;
    2563              : 
    2564        27833 :   gcc_checking_assert (code != TREE_BINFO);
    2565        27833 :   LTO_NO_PREVAIL (TREE_TYPE (t));
    2566        27833 :   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        25213 :       && !VAR_OR_FUNCTION_DECL_P (t))
    2570         1565 :     LTO_NO_PREVAIL (TREE_CHAIN (t));
    2571        27833 :   if (DECL_P (t))
    2572              :     {
    2573        24299 :       LTO_NO_PREVAIL (DECL_NAME (t));
    2574        24299 :       LTO_SET_PREVAIL (DECL_CONTEXT (t));
    2575        24299 :       if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    2576              :         {
    2577        24299 :           LTO_SET_PREVAIL (DECL_SIZE (t));
    2578        24299 :           LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
    2579        24299 :           LTO_SET_PREVAIL (DECL_INITIAL (t));
    2580        24299 :           LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
    2581        24299 :           LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
    2582              :         }
    2583        24299 :       if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    2584              :         {
    2585        23988 :           LTO_NO_PREVAIL (DECL_ASSEMBLER_NAME_RAW (t));
    2586              :         }
    2587        24299 :       if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
    2588              :         {
    2589         6905 :           LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
    2590              :         }
    2591        24299 :       if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    2592              :         {
    2593         6565 :           LTO_NO_PREVAIL (DECL_ARGUMENTS (t));
    2594         6565 :           LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
    2595         6565 :           LTO_NO_PREVAIL (DECL_VINDEX (t));
    2596              :         }
    2597        24299 :       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         3534 :   else if (TYPE_P (t))
    2607              :     {
    2608          721 :       LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
    2609          721 :       LTO_SET_PREVAIL (TYPE_SIZE (t));
    2610          721 :       LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
    2611          721 :       LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
    2612          721 :       LTO_NO_PREVAIL (TYPE_NAME (t));
    2613              : 
    2614          721 :       LTO_SET_PREVAIL (TYPE_MIN_VALUE_RAW (t));
    2615          721 :       LTO_SET_PREVAIL (TYPE_MAX_VALUE_RAW (t));
    2616          721 :       LTO_NO_PREVAIL (TYPE_LANG_SLOT_1 (t));
    2617              : 
    2618          721 :       LTO_SET_PREVAIL (TYPE_CONTEXT (t));
    2619              : 
    2620          721 :       LTO_NO_PREVAIL (TYPE_CANONICAL (t));
    2621          721 :       LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
    2622          721 :       LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
    2623              :     }
    2624         2813 :   else if (EXPR_P (t))
    2625              :     {
    2626         2620 :       int i;
    2627         5343 :       for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
    2628         2723 :         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        27833 :   gcc_checking_assert (fixed);
    2652        27833 : }
    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       108228 : lto_fixup_state (struct lto_in_decl_state *state)
    2661              : {
    2662       108228 :   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       108228 :   for (si = 0; si < LTO_N_DECL_STREAMS; si++)
    2668              :     {
    2669       108228 :       vec<tree, va_gc> *trees = state->streams[si];
    2670      1430911 :       for (i = 0; i < vec_safe_length (trees); i++)
    2671              :         {
    2672      1214455 :           tree t = (*trees)[i];
    2673      1214455 :           if (flag_checking && TYPE_P (t))
    2674       677549 :             verify_type (t);
    2675      1096640 :           if (VAR_OR_FUNCTION_DECL_P (t)
    2676      1582485 :               && (TREE_PUBLIC (t) || DECL_EXTERNAL (t)))
    2677       405765 :             (*trees)[i] = lto_symtab_prevailing_decl (t);
    2678              :         }
    2679              :     }
    2680       108228 : }
    2681              : 
    2682              : /* Fix the decls from all FILES.  Replaces each decl with the corresponding
    2683              :    prevailing one.  */
    2684              : 
    2685              : static void
    2686        12211 : lto_fixup_decls (struct lto_file_decl_data **files)
    2687              : {
    2688        12211 :   unsigned int i;
    2689        12211 :   tree t;
    2690              : 
    2691        12211 :   if (tree_with_vars)
    2692        29545 :     FOR_EACH_VEC_ELT ((*tree_with_vars), i, t)
    2693        27833 :       lto_fixup_prevailing_decls (t);
    2694              : 
    2695        25477 :   for (i = 0; files[i]; i++)
    2696              :     {
    2697        13266 :       struct lto_file_decl_data *file = files[i];
    2698        13266 :       struct lto_in_decl_state *state = file->global_decl_state;
    2699        13266 :       lto_fixup_state (state);
    2700              : 
    2701        13266 :       hash_table<decl_state_hasher>::iterator iter;
    2702        13266 :       lto_in_decl_state *elt;
    2703       216456 :       FOR_EACH_HASH_TABLE_ELEMENT (*file->function_decl_states, elt,
    2704              :                                    lto_in_decl_state *, iter)
    2705        94962 :         lto_fixup_state (elt);
    2706              :     }
    2707        12211 : }
    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        20401 : lto_flatten_files (struct lto_file_decl_data **orig, int count,
    2716              :                    int last_file_ix)
    2717              : {
    2718        20401 :   struct lto_file_decl_data *n, *next;
    2719        20401 :   int i, k;
    2720              : 
    2721        20401 :   lto_stats.num_input_files = count;
    2722        20401 :   all_file_decl_data
    2723        20401 :     = 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        20401 :   lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
    2726        62258 :   for (i = 0, k = 0; i < last_file_ix; i++)
    2727              :     {
    2728        42912 :       for (n = orig[i]; n != NULL; n = next)
    2729              :         {
    2730        21456 :           all_file_decl_data[k++] = n;
    2731        21456 :           next = n->next;
    2732        21456 :           n->next = NULL;
    2733              :         }
    2734              :     }
    2735        20401 :   all_file_decl_data[k] = NULL;
    2736        20401 :   gcc_assert (k == count);
    2737        20401 : }
    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        20401 : read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
    2751              : {
    2752        20401 :   unsigned int i, last_file_ix;
    2753        20401 :   FILE *resolution;
    2754        20401 :   unsigned resolution_objects = 0;
    2755        20401 :   int count = 0;
    2756        20401 :   struct lto_file_decl_data **decl_data;
    2757        20401 :   symtab_node *snode;
    2758              : 
    2759        20401 :   symtab->initialize ();
    2760              : 
    2761        20401 :   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        20401 :   real_file_decl_data
    2769        20401 :     = decl_data = ggc_cleared_vec_alloc<lto_file_decl_data_ptr> (nfiles + 1);
    2770        20401 :   real_file_count = nfiles;
    2771              : 
    2772              :   /* Read the resolution file.  */
    2773        20401 :   resolution = NULL;
    2774        20401 :   if (resolution_file_name)
    2775              :     {
    2776         7843 :       int t;
    2777              : 
    2778         7843 :       resolution = fopen (resolution_file_name, "r");
    2779         7843 :       if (resolution == NULL)
    2780            0 :         fatal_error (input_location,
    2781              :                      "could not open symbol resolution file: %m");
    2782              : 
    2783         7843 :       t = fscanf (resolution, "%u", &resolution_objects);
    2784         7843 :       gcc_assert (t == 1);
    2785              :     }
    2786        20401 :   symtab->state = LTO_STREAMING;
    2787              : 
    2788        20401 :   canonical_type_hash_cache = new hash_map<const_tree, hashval_t> (251);
    2789        20401 :   gimple_canonical_types = htab_create (16381, gimple_canonical_type_hash,
    2790              :                                         gimple_canonical_type_eq, NULL);
    2791        20401 :   gcc_obstack_init (&tree_scc_hash_obstack);
    2792        20401 :   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       408020 :   for (i = 0; i < itk_none; ++i)
    2800       387619 :     lto_register_canonical_types (integer_types[i], true);
    2801       102005 :   for (i = 0; i < stk_type_kind_last; ++i)
    2802        81604 :     lto_register_canonical_types (sizetype_tab[i], true);
    2803      3325363 :   for (i = 0; i < TI_MAX; ++i)
    2804      3304962 :     lto_register_canonical_types (global_trees[i], true);
    2805       408020 :   for (i = 0; i < itk_none; ++i)
    2806       387619 :     lto_register_canonical_types (integer_types[i], false);
    2807       102005 :   for (i = 0; i < stk_type_kind_last; ++i)
    2808        81604 :     lto_register_canonical_types (sizetype_tab[i], false);
    2809      3325363 :   for (i = 0; i < TI_MAX; ++i)
    2810      3304962 :     lto_register_canonical_types (global_trees[i], false);
    2811              : 
    2812        20401 :   if (!quiet_flag)
    2813            0 :     fprintf (stderr, "Reading object files:");
    2814              : 
    2815              :   /* Read all of the object files specified on the command line.  */
    2816        41857 :   for (i = 0, last_file_ix = 0; i < nfiles; ++i)
    2817              :     {
    2818        21456 :       struct lto_file_decl_data *file_data = NULL;
    2819        21456 :       if (!quiet_flag)
    2820              :         {
    2821            0 :           fprintf (stderr, " %s", fnames[i]);
    2822            0 :           fflush (stderr);
    2823              :         }
    2824              : 
    2825        21456 :       current_lto_file = lto_obj_file_open (fnames[i], false);
    2826        21456 :       if (!current_lto_file)
    2827              :         break;
    2828              : 
    2829        21456 :       file_data = lto_file_read (current_lto_file, resolution, &count);
    2830        21456 :       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        21456 :       decl_data[last_file_ix++] = file_data;
    2839              : 
    2840        21456 :       lto_obj_file_close (current_lto_file);
    2841        21456 :       free (current_lto_file);
    2842        21456 :       current_lto_file = NULL;
    2843              :     }
    2844              : 
    2845        20401 :   lto_flatten_files (decl_data, count, last_file_ix);
    2846        20401 :   lto_stats.num_input_files = count;
    2847        20401 :   ggc_free(decl_data);
    2848        20401 :   real_file_decl_data = NULL;
    2849              : 
    2850        20401 :   lto_register_canonical_types_for_odr_types ();
    2851              : 
    2852        20401 :   if (resolution_file_name)
    2853              :     {
    2854              :       /* True, since the plugin splits the archives.  */
    2855         7843 :       gcc_assert (resolution_objects == nfiles);
    2856         7843 :       fclose (resolution);
    2857              :     }
    2858              : 
    2859              :   /* Show the LTO report before launching LTRANS.  */
    2860        20401 :   if (flag_lto_report || (flag_wpa && flag_lto_report_wpa))
    2861            0 :     print_lto_report_1 ();
    2862              : 
    2863              :   /* Free gimple type merging datastructures.  */
    2864        20401 :   delete tree_scc_hash;
    2865        20401 :   tree_scc_hash = NULL;
    2866        20401 :   obstack_free (&tree_scc_hash_obstack, NULL);
    2867        20401 :   htab_delete (gimple_canonical_types);
    2868        20401 :   gimple_canonical_types = NULL;
    2869        40802 :   delete canonical_type_hash_cache;
    2870        20401 :   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        20401 :   ggc_grow ();
    2875        20401 :   report_heap_memory_use ();
    2876              : 
    2877              :   /* Set the hooks so that all of the ipa passes can read in their data.  */
    2878        20401 :   lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
    2879              : 
    2880        20401 :   timevar_pop (TV_IPA_LTO_DECL_IN);
    2881              : 
    2882        20401 :   if (!quiet_flag)
    2883            0 :     fprintf (stderr, "\nReading the symbol table:");
    2884              : 
    2885        20401 :   timevar_push (TV_IPA_LTO_CGRAPH_IO);
    2886              :   /* Read the symtab.  */
    2887        20401 :   input_symtab ();
    2888              : 
    2889        20401 :   input_offload_tables (!flag_ltrans);
    2890              : 
    2891              :   /* Store resolutions into the symbol table.  */
    2892              : 
    2893       285951 :   FOR_EACH_SYMBOL (snode)
    2894       111334 :     if (snode->externally_visible && snode->real_symbol_p ()
    2895       111330 :         && snode->lto_file_data && snode->lto_file_data->resolution_map
    2896       110800 :         && !(TREE_CODE (snode->decl) == FUNCTION_DECL
    2897        48990 :              && fndecl_built_in_p (snode->decl))
    2898       326800 :         && !(VAR_P (snode->decl) && DECL_HARD_REGISTER (snode->decl)))
    2899              :       {
    2900        61246 :         ld_plugin_symbol_resolution_t *res;
    2901              : 
    2902        61246 :         res = snode->lto_file_data->resolution_map->get (snode->decl);
    2903        61246 :         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        61236 :         else if (snode->symver && *res == LDPR_PREVAILING_DEF_IRONLY)
    2914            0 :           snode->resolution = LDPR_PREVAILING_DEF_IRONLY_EXP;
    2915              :         else
    2916        61236 :           snode->resolution = *res;
    2917              :       }
    2918        41857 :   for (i = 0; all_file_decl_data[i]; i++)
    2919        21456 :     if (all_file_decl_data[i]->resolution_map)
    2920              :       {
    2921         8538 :         delete all_file_decl_data[i]->resolution_map;
    2922         8538 :         all_file_decl_data[i]->resolution_map = NULL;
    2923              :       }
    2924              : 
    2925        20401 :   timevar_pop (TV_IPA_LTO_CGRAPH_IO);
    2926              : 
    2927        20401 :   if (!quiet_flag)
    2928            0 :     fprintf (stderr, "\nMerging declarations:");
    2929              : 
    2930        20401 :   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        20401 :   if (!flag_ltrans)
    2936              :     {
    2937        12211 :       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        12211 :       if (seen_error ())
    2942            0 :         fatal_error (input_location,
    2943              :                      "errors during merging of translation units");
    2944              : 
    2945              :       /* Fixup all decls.  */
    2946        12211 :       lto_fixup_decls (all_file_decl_data);
    2947              :     }
    2948        20401 :   if (tree_with_vars)
    2949         1712 :     ggc_free (tree_with_vars);
    2950        20401 :   tree_with_vars = NULL;
    2951              : 
    2952        20401 :   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        20401 :   ggc_grow ();
    2959        20401 :   report_heap_memory_use ();
    2960              : 
    2961        20401 :   timevar_pop (TV_IPA_LTO_DECL_MERGE);
    2962              :   /* Each pass will set the appropriate timer.  */
    2963              : 
    2964        20401 :   if (!quiet_flag)
    2965            0 :     fprintf (stderr, "\nReading summaries:");
    2966              : 
    2967              :   /* Read the IPA summary data.  */
    2968        20401 :   if (flag_ltrans)
    2969         8190 :     ipa_read_optimization_summaries ();
    2970              :   else
    2971        12211 :     ipa_read_summaries ();
    2972              : 
    2973        20401 :   ggc_grow ();
    2974              : 
    2975        41857 :   for (i = 0; all_file_decl_data[i]; i++)
    2976              :     {
    2977        21456 :       gcc_assert (all_file_decl_data[i]->symtab_node_encoder);
    2978        21456 :       lto_symtab_encoder_delete (all_file_decl_data[i]->symtab_node_encoder);
    2979        21456 :       all_file_decl_data[i]->symtab_node_encoder = NULL;
    2980        21456 :       lto_in_decl_state *global_decl_state
    2981              :         = all_file_decl_data[i]->global_decl_state;
    2982        21456 :       lto_free_function_in_decl_state (global_decl_state);
    2983        21456 :       all_file_decl_data[i]->global_decl_state = NULL;
    2984        21456 :       all_file_decl_data[i]->current_decl_state = NULL;
    2985              :     }
    2986              : 
    2987        20401 :   if (!flag_ltrans)
    2988              :     {
    2989              :       /* Finally merge the cgraph according to the decl merging decisions.  */
    2990        12211 :       timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
    2991              : 
    2992        12211 :       if (!quiet_flag)
    2993            0 :         fprintf (stderr, "\nMerging symbols:");
    2994              : 
    2995        12211 :       gcc_assert (!dump_file);
    2996        12211 :       dump_file = dump_begin (lto_link_dump_id, NULL);
    2997              : 
    2998        12211 :       if (dump_file)
    2999              :         {
    3000            0 :           fprintf (dump_file, "Before merging:\n");
    3001            0 :           symtab->dump (dump_file);
    3002              :         }
    3003        12211 :       lto_symtab_merge_symbols ();
    3004        12211 :       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        12211 :       symtab->remove_unreachable_nodes (dump_file);
    3009        12211 :       ggc_collect ();
    3010        12211 :       report_heap_memory_use ();
    3011              : 
    3012        12211 :       if (dump_file)
    3013            0 :         dump_end (lto_link_dump_id, dump_file);
    3014        12211 :       dump_file = NULL;
    3015        12211 :       timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
    3016              :     }
    3017        20401 :   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        20401 :   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        20401 :   symtab->function_flags_ready = true;
    3032              : 
    3033        20401 :   ggc_free (all_file_decl_data);
    3034        20401 :   all_file_decl_data = NULL;
    3035        20401 : }
    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          554 : lto_eh_personality (void)
    3108              : {
    3109          554 :   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           87 :       if (first_personality_decl && !dwarf2out_do_cfi_asm ())
    3115            0 :         lto_eh_personality_decl = first_personality_decl;
    3116              :       else
    3117           87 :         lto_eh_personality_decl = lhd_gcc_personality ();
    3118              :     }
    3119              : 
    3120          554 :   return lto_eh_personality_decl;
    3121              : }
    3122              : 
    3123              : /* Set the process name based on the LTO mode.  */
    3124              : 
    3125              : static void
    3126        20401 : lto_process_name (void)
    3127              : {
    3128        20401 :   if (flag_lto)
    3129           32 :     setproctitle (flag_incremental_link == INCREMENTAL_LINK_LTO
    3130              :                   ? "lto1-inclink" : "lto1-lto");
    3131        20401 :   if (flag_wpa)
    3132         7815 :     setproctitle ("lto1-wpa");
    3133        20401 :   if (flag_ltrans)
    3134         8190 :     setproctitle ("lto1-ltrans");
    3135        20401 : }
    3136              : 
    3137              : 
    3138              : /* Initialize the LTO front end.  */
    3139              : 
    3140              : void
    3141        20401 : lto_fe_init (void)
    3142              : {
    3143        20401 :   lto_process_name ();
    3144        20401 :   lto_streamer_hooks_init ();
    3145        20401 :   lto_reader_init ();
    3146        20401 :   lto_set_in_hooks (NULL, get_section_data, free_section_data);
    3147        20401 :   memset (&lto_stats, 0, sizeof (lto_stats));
    3148        20401 :   bitmap_obstack_initialize (NULL);
    3149        20401 :   gimple_register_cfg_hooks ();
    3150        20401 : }
    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.