LCOV - code coverage report
Current view: top level - gcc/lto - lto-symtab.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 79.4 % 506 402
Test Date: 2024-04-13 14:00:49 Functions: 100.0 % 15 15
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* LTO symbol table.
       2                 :             :    Copyright (C) 2009-2024 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 "target.h"
      25                 :             : #include "function.h"
      26                 :             : #include "basic-block.h"
      27                 :             : #include "tree.h"
      28                 :             : #include "gimple.h"
      29                 :             : #include "cgraph.h"
      30                 :             : #include "lto-streamer.h"
      31                 :             : #include "ipa-utils.h"
      32                 :             : #include "builtins.h"
      33                 :             : #include "alias.h"
      34                 :             : #include "lto.h"
      35                 :             : #include "lto-symtab.h"
      36                 :             : #include "stringpool.h"
      37                 :             : #include "attribs.h"
      38                 :             : 
      39                 :             : /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
      40                 :             :    all edges and removing the old node.  */
      41                 :             : 
      42                 :             : static void
      43                 :        1881 : lto_cgraph_replace_node (struct cgraph_node *node,
      44                 :             :                          struct cgraph_node *prevailing_node)
      45                 :             : {
      46                 :        1881 :   struct cgraph_edge *e, *next;
      47                 :        1881 :   bool compatible_p;
      48                 :             : 
      49                 :        1881 :   if (dump_file)
      50                 :             :     {
      51                 :           0 :       fprintf (dump_file, "Replacing cgraph node %s by %s"
      52                 :             :                " for symbol %s\n",
      53                 :             :                node->dump_name (),
      54                 :             :                prevailing_node->dump_name (),
      55                 :           0 :                IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
      56                 :             :                  (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
      57                 :             :     }
      58                 :             : 
      59                 :             :   /* Merge node flags.  */
      60                 :        1881 :   if (node->force_output)
      61                 :           0 :     prevailing_node->mark_force_output ();
      62                 :        1881 :   if (node->forced_by_abi)
      63                 :           0 :     prevailing_node->forced_by_abi = true;
      64                 :        1881 :   if (node->address_taken)
      65                 :             :     {
      66                 :         142 :       gcc_assert (!prevailing_node->inlined_to);
      67                 :         142 :       prevailing_node->mark_address_taken ();
      68                 :             :     }
      69                 :          74 :   if (node->definition && prevailing_node->definition
      70                 :        1955 :       && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
      71                 :          69 :     prevailing_node->merged_comdat = true;
      72                 :        1812 :   else if ((node->definition || node->body_removed)
      73                 :           5 :            && DECL_DECLARED_INLINE_P (node->decl)
      74                 :           3 :            && DECL_EXTERNAL (node->decl)
      75                 :        1815 :            && prevailing_node->definition)
      76                 :           3 :     prevailing_node->merged_extern_inline = true;
      77                 :        1881 :   prevailing_node->merged_comdat |= node->merged_comdat;
      78                 :        1881 :   prevailing_node->merged_extern_inline |= node->merged_extern_inline;
      79                 :             : 
      80                 :             :   /* Redirect all incoming edges.  */
      81                 :        1881 :   compatible_p
      82                 :        1881 :     = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
      83                 :        1881 :                           TREE_TYPE (TREE_TYPE (node->decl)));
      84                 :        7054 :   for (e = node->callers; e; e = next)
      85                 :             :     {
      86                 :        5173 :       next = e->next_caller;
      87                 :        5173 :       e->redirect_callee (prevailing_node);
      88                 :             :       /* If there is a mismatch between the supposed callee return type and
      89                 :             :          the real one do not attempt to inline this function.
      90                 :             :          ???  We really need a way to match function signatures for ABI
      91                 :             :          compatibility and perform related promotions at inlining time.  */
      92                 :        5173 :       if (!compatible_p)
      93                 :             :         {
      94                 :           0 :           e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS;
      95                 :           0 :           e->call_stmt_cannot_inline_p = 1;
      96                 :             :         }
      97                 :             :     }
      98                 :             :   /* Redirect incomming references.  */
      99                 :        1881 :   prevailing_node->clone_referring (node);
     100                 :        1881 :   lto_free_function_in_decl_state_for_node (node);
     101                 :             : 
     102                 :        1881 :   if (node->decl != prevailing_node->decl)
     103                 :        1095 :     node->release_body ();
     104                 :             : 
     105                 :             :   /* Finally remove the replaced node.  */
     106                 :        1881 :   node->remove ();
     107                 :        1881 : }
     108                 :             : 
     109                 :             : /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
     110                 :             :    all edges and removing the old node.  */
     111                 :             : 
     112                 :             : static void
     113                 :         716 : lto_varpool_replace_node (varpool_node *vnode,
     114                 :             :                           varpool_node *prevailing_node)
     115                 :             : {
     116                 :         716 :   gcc_assert (!vnode->definition || prevailing_node->definition);
     117                 :         716 :   gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
     118                 :             : 
     119                 :         716 :   prevailing_node->clone_referring (vnode);
     120                 :         716 :   if (vnode->force_output)
     121                 :          11 :     prevailing_node->force_output = true;
     122                 :         716 :   if (vnode->forced_by_abi)
     123                 :           3 :     prevailing_node->forced_by_abi = true;
     124                 :             : 
     125                 :             :   /* Be sure we can garbage collect the initializer.  */
     126                 :         716 :   if (DECL_INITIAL (vnode->decl)
     127                 :         716 :       && vnode->decl != prevailing_node->decl)
     128                 :          29 :     DECL_INITIAL (vnode->decl) = error_mark_node;
     129                 :             : 
     130                 :             :   /* Check and report ODR violations on virtual tables.  */
     131                 :         716 :   if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
     132                 :          63 :     compare_virtual_tables (prevailing_node, vnode);
     133                 :             : 
     134                 :         716 :   if (vnode->tls_model != prevailing_node->tls_model)
     135                 :             :     {
     136                 :           0 :       bool error = false;
     137                 :             : 
     138                 :             :       /* Non-TLS and TLS never mix together.  Also emulated model is not
     139                 :             :          compatible with anything else.  */
     140                 :           0 :       if (prevailing_node->tls_model == TLS_MODEL_NONE
     141                 :           0 :           || prevailing_node->tls_model == TLS_MODEL_EMULATED
     142                 :           0 :           || vnode->tls_model == TLS_MODEL_NONE
     143                 :           0 :           || vnode->tls_model == TLS_MODEL_EMULATED)
     144                 :             :         error = true;
     145                 :             :       /* Linked is silently supporting transitions
     146                 :             :          GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
     147                 :             :          Do the same transitions and error out on others.  */
     148                 :           0 :       else if ((prevailing_node->tls_model == TLS_MODEL_REAL
     149                 :           0 :                 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
     150                 :           0 :                && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
     151                 :           0 :                    || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
     152                 :           0 :         prevailing_node->tls_model = vnode->tls_model;
     153                 :           0 :       else if ((vnode->tls_model == TLS_MODEL_REAL
     154                 :           0 :                 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
     155                 :           0 :                && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
     156                 :           0 :                    || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
     157                 :             :         ;
     158                 :           0 :       else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
     159                 :           0 :                && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
     160                 :           0 :         prevailing_node->tls_model = vnode->tls_model;
     161                 :           0 :       else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
     162                 :           0 :                && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
     163                 :             :         ;
     164                 :             :       else
     165                 :             :         error = true;
     166                 :           0 :       if (error)
     167                 :             :         {
     168                 :           0 :           error_at (DECL_SOURCE_LOCATION (vnode->decl),
     169                 :           0 :                     "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
     170                 :           0 :           inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
     171                 :             :                   "previously defined here as %s",
     172                 :           0 :                   tls_model_names [prevailing_node->tls_model]);
     173                 :             :         }
     174                 :             :     }
     175                 :             :   /* Finally remove the replaced node.  */
     176                 :         716 :   vnode->remove ();
     177                 :         716 : }
     178                 :             : 
     179                 :             : /* Return non-zero if we want to output waring about T1 and T2.
     180                 :             :    Return value is a bitmask of reasons of violation:
     181                 :             :    Bit 0 indicates that types are not compatible.
     182                 :             :    Bit 1 indicates that types are not compatible because of C++ ODR rule.
     183                 :             :    If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
     184                 :             :    Bit 2 indicates that types are not ODR compatible
     185                 :             : 
     186                 :             :    The interoperability rules are language specific.  At present we do only
     187                 :             :    full checking for C++ ODR rule and for other languages we do basic check
     188                 :             :    that data structures are of same size and TBAA compatible.  Our TBAA
     189                 :             :    implementation should be coarse enough so all valid type transitions
     190                 :             :    across different languages are allowed.
     191                 :             : 
     192                 :             :    In partiucular we thus allow almost arbitrary type changes with
     193                 :             :    -fno-strict-aliasing which may be tough of as a feature rather than bug
     194                 :             :    as it allows to implement dodgy tricks in the language runtimes.
     195                 :             : 
     196                 :             :    Naturally this code can be strenghtened significantly if we could track
     197                 :             :    down the language of origin.  */
     198                 :             : 
     199                 :             : static int
     200                 :        2535 : warn_type_compatibility_p (tree prevailing_type, tree type,
     201                 :             :                            bool common_or_extern)
     202                 :             : {
     203                 :        2535 :   int lev = 0;
     204                 :        2535 :   bool odr_p = odr_or_derived_type_p (prevailing_type)
     205                 :        2535 :                && odr_or_derived_type_p (type);
     206                 :             : 
     207                 :        2535 :   if (prevailing_type == type)
     208                 :             :     return 0;
     209                 :             : 
     210                 :             :   /* C++ provide a robust way to check for type compatibility via the ODR
     211                 :             :      rule.  */
     212                 :         929 :   if (odr_p && !odr_types_equivalent_p (prevailing_type, type))
     213                 :             :     lev |= 2;
     214                 :             : 
     215                 :             :   /* Function types needs special care, because types_compatible_p never
     216                 :             :      thinks prototype is compatible to non-prototype.  */
     217                 :         929 :   if (FUNC_OR_METHOD_TYPE_P (type))
     218                 :             :     {
     219                 :         354 :       if (TREE_CODE (type) != TREE_CODE (prevailing_type))
     220                 :           0 :         lev |= 1;
     221                 :         354 :       lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
     222                 :         354 :                                         TREE_TYPE (type), false);
     223                 :         354 :       if (TREE_CODE (type) == METHOD_TYPE
     224                 :          78 :           && TREE_CODE (prevailing_type) == METHOD_TYPE)
     225                 :          78 :         lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
     226                 :          78 :                                           TYPE_METHOD_BASETYPE (type), false);
     227                 :         626 :       if (prototype_p (prevailing_type) && prototype_p (type)
     228                 :         602 :           && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
     229                 :             :         {
     230                 :          71 :           tree parm1, parm2;
     231                 :          71 :           for (parm1 = TYPE_ARG_TYPES (prevailing_type),
     232                 :          71 :                parm2 = TYPE_ARG_TYPES (type);
     233                 :         527 :                parm1 && parm2;
     234                 :         912 :                parm1 = TREE_CHAIN (parm1),
     235                 :         456 :                parm2 = TREE_CHAIN (parm2))
     236                 :         912 :             lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
     237                 :         456 :                                               TREE_VALUE (parm2), false);
     238                 :          71 :           if (parm1 || parm2)
     239                 :          24 :             lev |= odr_p ? 3 : 1;
     240                 :             :         }
     241                 :         354 :       if (comp_type_attributes (prevailing_type, type) == 0)
     242                 :           0 :         lev |= 1;
     243                 :         354 :       return lev;
     244                 :             :     }
     245                 :             : 
     246                 :             :   /* Get complete type.  */
     247                 :         575 :   prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
     248                 :         575 :   type = TYPE_MAIN_VARIANT (type);
     249                 :             : 
     250                 :             :   /* We cannot use types_compatible_p because we permit some changes
     251                 :             :      across types.  For example unsigned size_t and "signed size_t" may be
     252                 :             :      compatible when merging C and Fortran types.  */
     253                 :         575 :   if (COMPLETE_TYPE_P (prevailing_type)
     254                 :         557 :       && COMPLETE_TYPE_P (type)
     255                 :             :       /* While global declarations are never variadic, we can recurse here
     256                 :             :          for function parameter types.  */
     257                 :         498 :       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
     258                 :         498 :       && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
     259                 :        1073 :       && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
     260                 :             :     {
     261                 :             :        /* As a special case do not warn about merging
     262                 :             :           int a[];
     263                 :             :           and
     264                 :             :           int a[]={1,2,3};
     265                 :             :           here the first declaration is COMMON or EXTERN
     266                 :             :           and sizeof(a) == sizeof (int).  */
     267                 :          53 :        if (!common_or_extern
     268                 :          34 :            || TREE_CODE (type) != ARRAY_TYPE
     269                 :          59 :            || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
     270                 :          47 :        lev |= 1;
     271                 :             :     }
     272                 :             : 
     273                 :             :   /* Verify TBAA compatibility.  Take care of alias set 0 and the fact that
     274                 :             :      we make ptr_type_node to TBAA compatible with every other type.  */
     275                 :         575 :   if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
     276                 :             :     {
     277                 :         557 :       alias_set_type set1 = get_alias_set (type);
     278                 :         557 :       alias_set_type set2 = get_alias_set (prevailing_type);
     279                 :             : 
     280                 :         557 :       if (set1 && set2 && set1 != set2)
     281                 :             :         {
     282                 :             :            tree t1 = type, t2 = prevailing_type;
     283                 :             : 
     284                 :             :           /* Alias sets of arrays with aliased components are the same as alias
     285                 :             :              sets of the inner types.  */
     286                 :         234 :           while (TREE_CODE (t1) == ARRAY_TYPE
     287                 :           6 :                  && !TYPE_NONALIASED_COMPONENT (t1)
     288                 :           6 :                  && TREE_CODE (t2) == ARRAY_TYPE
     289                 :         240 :                  && !TYPE_NONALIASED_COMPONENT (t2))
     290                 :             :             {
     291                 :           6 :               t1 = TREE_TYPE (t1);
     292                 :           6 :               t2 = TREE_TYPE (t2);
     293                 :             :             }
     294                 :         179 :           if ((!POINTER_TYPE_P (t1) || !POINTER_TYPE_P (t2))
     295                 :         405 :               || (set1 != TYPE_ALIAS_SET (ptr_type_node)
     296                 :         171 :                   && set2 != TYPE_ALIAS_SET (ptr_type_node)))
     297                 :         207 :             lev |= 5;
     298                 :             :         }
     299                 :             :     }
     300                 :             : 
     301                 :             :   return lev;
     302                 :             : }
     303                 :             : 
     304                 :             : /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
     305                 :             :    Return false if the symbols are not fully compatible and a diagnostic
     306                 :             :    should be emitted.  */
     307                 :             : 
     308                 :             : static bool
     309                 :        1608 : lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
     310                 :             : {
     311                 :        1608 :   tree prevailing_decl = prevailing->decl;
     312                 :        1608 :   tree decl = entry->decl;
     313                 :             : 
     314                 :        1608 :   if (prevailing_decl == decl)
     315                 :             :     return true;
     316                 :             : 
     317                 :        1608 :   if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
     318                 :             :     return false;
     319                 :             : 
     320                 :             :   /* Merge decl state in both directions, we may still end up using
     321                 :             :      the new decl.  */
     322                 :        1608 :   TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
     323                 :        1608 :   TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
     324                 :             : 
     325                 :             :   /* The linker may ask us to combine two incompatible symbols.
     326                 :             :      Detect this case and notify the caller of required diagnostics.  */
     327                 :             : 
     328                 :        1608 :   if (TREE_CODE (decl) == FUNCTION_DECL)
     329                 :             :     {
     330                 :             :       /* Merge decl state in both directions, we may still end up using
     331                 :             :          the new decl.  */
     332                 :        1083 :       DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
     333                 :        1083 :       DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
     334                 :             : 
     335                 :        1083 :       if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
     336                 :        1083 :                                      TREE_TYPE (decl),
     337                 :        1083 :                                      DECL_COMMON (decl)
     338                 :        1083 :                                      || DECL_EXTERNAL (decl)))
     339                 :             :         return false;
     340                 :             : 
     341                 :             :       return true;
     342                 :             :     }
     343                 :             : 
     344                 :         525 :   if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
     345                 :         525 :                                  TREE_TYPE (decl),
     346                 :         525 :                                  DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
     347                 :             :     return false;
     348                 :             : 
     349                 :             :   /* There is no point in comparing too many details of the decls here.
     350                 :             :      The type compatibility checks or the completing of types has properly
     351                 :             :      dealt with most issues.  */
     352                 :             : 
     353                 :             :   /* The following should all not invoke fatal errors as in non-LTO
     354                 :             :      mode the linker wouldn't complain either.  Just emit warnings.  */
     355                 :             : 
     356                 :             :   /* Report a warning if user-specified alignments do not match.  */
     357                 :         571 :   if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
     358                 :         527 :       && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
     359                 :             :     return false;
     360                 :             : 
     361                 :         935 :   if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
     362                 :         935 :       && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl)))
     363                 :             :       {
     364                 :          20 :         if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
     365                 :             :           return false;
     366                 :             : 
     367                 :          20 :         tree type = TREE_TYPE (decl);
     368                 :             : 
     369                 :             :         /* For record type, check for array at the end of the structure.  */
     370                 :          20 :         if (TREE_CODE (type) == RECORD_TYPE)
     371                 :             :           {
     372                 :          14 :             tree field = TYPE_FIELDS (type);
     373                 :          30 :             while (DECL_CHAIN (field) != NULL_TREE)
     374                 :          16 :               field = DECL_CHAIN (field);
     375                 :             : 
     376                 :          14 :             return TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE;
     377                 :             :           }
     378                 :             :       /* As a special case do not warn about merging
     379                 :             :          int a[];
     380                 :             :          and
     381                 :             :          int a[]={1,2,3};
     382                 :             :          here the first declaration is COMMON
     383                 :             :          and sizeof(a) == sizeof (int).  */
     384                 :           6 :         else if (TREE_CODE (type) != ARRAY_TYPE
     385                 :           6 :                  || (TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type))))
     386                 :             :           return false;
     387                 :             :       }
     388                 :             : 
     389                 :             :   return true;
     390                 :             : }
     391                 :             : 
     392                 :             : /* Return true if the symtab entry E can be replaced by another symtab
     393                 :             :    entry.  */
     394                 :             : 
     395                 :             : static bool
     396                 :         662 : lto_symtab_resolve_replaceable_p (symtab_node *e)
     397                 :             : {
     398                 :         662 :   if (DECL_EXTERNAL (e->decl)
     399                 :         662 :       || DECL_COMDAT (e->decl)
     400                 :         590 :       || DECL_ONE_ONLY (e->decl)
     401                 :        1252 :       || DECL_WEAK (e->decl))
     402                 :          75 :     return true;
     403                 :             : 
     404                 :         587 :   if (VAR_P (e->decl))
     405                 :         179 :     return (DECL_COMMON (e->decl)
     406                 :         179 :             || (!flag_no_common && !DECL_INITIAL (e->decl)));
     407                 :             : 
     408                 :             :   return false;
     409                 :             : }
     410                 :             : 
     411                 :             : /* Return true, if the symbol E should be resolved by lto-symtab.
     412                 :             :    Those are all external symbols and all real symbols that are not static (we
     413                 :             :    handle renaming of static later in partitioning).  */
     414                 :             : 
     415                 :             : static bool
     416                 :      163737 : lto_symtab_symbol_p (symtab_node *e)
     417                 :             : {
     418                 :      163737 :   if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
     419                 :             :     return false;
     420                 :      132471 :   return e->real_symbol_p ();
     421                 :             : }
     422                 :             : 
     423                 :             : /* Return true if the symtab entry E can be the prevailing one.  */
     424                 :             : 
     425                 :             : static bool
     426                 :        4357 : lto_symtab_resolve_can_prevail_p (symtab_node *e)
     427                 :             : {
     428                 :        4357 :   if (!lto_symtab_symbol_p (e))
     429                 :             :     return false;
     430                 :             : 
     431                 :             :   /* The C++ frontend ends up neither setting TREE_STATIC nor
     432                 :             :      DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
     433                 :             :      So do not reject !TREE_STATIC here but only DECL_EXTERNAL.  */
     434                 :        3983 :   if (DECL_EXTERNAL (e->decl))
     435                 :             :     return false;
     436                 :             : 
     437                 :         731 :   return e->definition;
     438                 :             : }
     439                 :             : 
     440                 :             : /* Resolve the symbol with the candidates in the chain *SLOT and store
     441                 :             :    their resolutions.  */
     442                 :             : 
     443                 :             : static symtab_node *
     444                 :        2083 : lto_symtab_resolve_symbols (symtab_node *first)
     445                 :             : {
     446                 :        2083 :   symtab_node *e;
     447                 :        2083 :   symtab_node *prevailing = NULL;
     448                 :             : 
     449                 :             :   /* Always set e->node so that edges are updated to reflect decl merging. */
     450                 :        5425 :   for (e = first; e; e = e->next_sharing_asm_name)
     451                 :        4339 :     if (lto_symtab_symbol_p (e)
     452                 :        4339 :         && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
     453                 :             :             || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
     454                 :             :             || e->resolution == LDPR_PREVAILING_DEF))
     455                 :             :       {
     456                 :             :         prevailing = e;
     457                 :             :         break;
     458                 :             :       }
     459                 :             : 
     460                 :             :   /* If the chain is already resolved there is nothing else to do.  */
     461                 :        2083 :   if (prevailing)
     462                 :             :     {
     463                 :             :       /* Assert it's the only one.
     464                 :             :          GCC should silence multiple PREVAILING_DEF_IRONLY defs error
     465                 :             :          on COMMON symbols since it isn't error.
     466                 :             :          See: https://sourceware.org/bugzilla/show_bug.cgi?id=23079.  */
     467                 :        1454 :       for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
     468                 :         457 :         if (lto_symtab_symbol_p (e)
     469                 :         449 :             && !DECL_COMMON (prevailing->decl)
     470                 :         439 :             && !DECL_COMMON (e->decl)
     471                 :         888 :             && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
     472                 :             :                 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
     473                 :             :                 || e->resolution == LDPR_PREVAILING_DEF))
     474                 :           0 :           fatal_error (input_location, "multiple prevailing defs for %qE",
     475                 :           0 :                        DECL_NAME (prevailing->decl));
     476                 :             :       return prevailing;
     477                 :             :     }
     478                 :             : 
     479                 :             :   /* Find the single non-replaceable prevailing symbol and
     480                 :             :      diagnose ODR violations.  */
     481                 :        3857 :   for (e = first; e; e = e->next_sharing_asm_name)
     482                 :             :     {
     483                 :        2771 :       if (!lto_symtab_resolve_can_prevail_p (e))
     484                 :        2109 :         continue;
     485                 :             : 
     486                 :             :       /* If we have a non-replaceable definition it prevails.  */
     487                 :         662 :       if (!lto_symtab_resolve_replaceable_p (e))
     488                 :             :         {
     489                 :         574 :           if (prevailing)
     490                 :             :             {
     491                 :           0 :               error_at (DECL_SOURCE_LOCATION (e->decl),
     492                 :             :                         "%qD has already been defined", e->decl);
     493                 :           0 :               inform (DECL_SOURCE_LOCATION (prevailing->decl),
     494                 :             :                       "previously defined here");
     495                 :             :             }
     496                 :             :           prevailing = e;
     497                 :             :         }
     498                 :             :     }
     499                 :        1086 :   if (prevailing)
     500                 :             :     return prevailing;
     501                 :             : 
     502                 :             :   /* Do a second round choosing one from the replaceable prevailing decls.  */
     503                 :        2078 :   for (e = first; e; e = e->next_sharing_asm_name)
     504                 :             :     {
     505                 :        1586 :       if (!lto_symtab_resolve_can_prevail_p (e))
     506                 :        1517 :         continue;
     507                 :             : 
     508                 :             :       /* Choose the first function that can prevail as prevailing.  */
     509                 :          69 :       if (TREE_CODE (e->decl) == FUNCTION_DECL)
     510                 :             :         {
     511                 :             :           prevailing = e;
     512                 :             :           break;
     513                 :             :         }
     514                 :             : 
     515                 :             :       /* From variables that can prevail choose the largest one.  */
     516                 :          49 :       if (!prevailing
     517                 :          18 :           || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
     518                 :          18 :                               DECL_SIZE (e->decl))
     519                 :             :           /* When variables are equivalent try to chose one that has useful
     520                 :             :              DECL_INITIAL.  This makes sense for keyed vtables that are
     521                 :             :              DECL_EXTERNAL but initialized.  In units that do not need them
     522                 :             :              we replace the initializer by error_mark_node to conserve
     523                 :             :              memory.
     524                 :             : 
     525                 :             :              We know that the vtable is keyed outside the LTO unit - otherwise
     526                 :             :              the keyed instance would prevail.  We still can preserve useful
     527                 :             :              info in the initializer.  */
     528                 :          65 :           || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
     529                 :          12 :               && (DECL_INITIAL (e->decl)
     530                 :          12 :                   && DECL_INITIAL (e->decl) != error_mark_node)
     531                 :           4 :               && (!DECL_INITIAL (prevailing->decl)
     532                 :           4 :                   || DECL_INITIAL (prevailing->decl) == error_mark_node)))
     533                 :             :         prevailing = e;
     534                 :             :     }
     535                 :             : 
     536                 :             :   return prevailing;
     537                 :             : }
     538                 :             : 
     539                 :             : /* Decide if it is OK to merge DECL into PREVAILING.
     540                 :             :    Because we wrap most of uses of declarations in MEM_REF, we can tolerate
     541                 :             :    some differences but other code may inspect directly the DECL.  */
     542                 :             : 
     543                 :             : static bool
     544                 :        1608 : lto_symtab_merge_p (tree prevailing, tree decl)
     545                 :             : {
     546                 :        1608 :   if (TREE_CODE (prevailing) != TREE_CODE (decl))
     547                 :             :     {
     548                 :           0 :       if (dump_file)
     549                 :           0 :         fprintf (dump_file, "Not merging decls; "
     550                 :             :                  "TREE_CODE mismatch\n");
     551                 :           0 :       return false;
     552                 :             :     }
     553                 :        1608 :   gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
     554                 :             :   
     555                 :        1608 :   if (TREE_CODE (prevailing) == FUNCTION_DECL)
     556                 :             :     {
     557                 :        1083 :       if (fndecl_built_in_p (prevailing) != fndecl_built_in_p (decl))
     558                 :             :         {
     559                 :           2 :           if (dump_file)
     560                 :           0 :             fprintf (dump_file, "Not merging decls; "
     561                 :             :                      "DECL_BUILT_IN mismatch\n");
     562                 :           2 :           return false;
     563                 :             :         }
     564                 :        1081 :       if (fndecl_built_in_p (prevailing)
     565                 :        1081 :           && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
     566                 :         243 :               || (DECL_UNCHECKED_FUNCTION_CODE (prevailing)
     567                 :         243 :                   != DECL_UNCHECKED_FUNCTION_CODE (decl))))
     568                 :             :         {
     569                 :           0 :           if (dump_file)
     570                 :           0 :             fprintf (dump_file, "Not merging decls; "
     571                 :             :                      "DECL_BUILT_IN_CLASS or CODE mismatch\n");
     572                 :           0 :           return false;
     573                 :             :         }
     574                 :             :     }
     575                 :             : 
     576                 :        1606 :   if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
     577                 :             :     {
     578                 :         313 :       tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
     579                 :         313 :       tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
     580                 :         313 :       if ((prev_attr == NULL) != (attr == NULL)
     581                 :         313 :           || (prev_attr && !attribute_value_equal (prev_attr, attr)))
     582                 :             :         {
     583                 :           0 :           if (dump_file)
     584                 :           0 :             fprintf (dump_file, "Not merging decls; "
     585                 :             :                      "error attribute mismatch\n");
     586                 :           0 :           return false;
     587                 :             :         }
     588                 :             : 
     589                 :         313 :       prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
     590                 :         313 :       attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
     591                 :         313 :       if ((prev_attr == NULL) != (attr == NULL)
     592                 :         313 :           || (prev_attr && !attribute_value_equal (prev_attr, attr)))
     593                 :             :         {
     594                 :           1 :           if (dump_file)
     595                 :           0 :             fprintf (dump_file, "Not merging decls; "
     596                 :             :                      "warning attribute mismatch\n");
     597                 :           1 :           return false;
     598                 :             :         }
     599                 :             : 
     600                 :         312 :       prev_attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (prevailing));
     601                 :         312 :       attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (decl));
     602                 :         312 :       if ((prev_attr == NULL) != (attr == NULL))
     603                 :             :         {
     604                 :           8 :           if (dump_file)
     605                 :           0 :             fprintf (dump_file, "Not merging decls; "
     606                 :             :                      "noreturn attribute mismatch\n");
     607                 :           8 :           return false;
     608                 :             :         }
     609                 :             :     }
     610                 :             :   return true;
     611                 :             : }
     612                 :             : 
     613                 :             : /* Merge all decls in the symbol table chain to the prevailing decl and
     614                 :             :    issue diagnostics about type mismatches.  If DIAGNOSED_P is true
     615                 :             :    do not issue further diagnostics.*/
     616                 :             : 
     617                 :             : static void
     618                 :        1991 : lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
     619                 :             : {
     620                 :        1991 :   symtab_node *prevailing;
     621                 :        1991 :   symtab_node *e;
     622                 :        1991 :   vec<tree> mismatches = vNULL;
     623                 :        1991 :   unsigned i;
     624                 :        1991 :   tree decl;
     625                 :        1991 :   bool tbaa_p = false;
     626                 :             : 
     627                 :             :   /* Nothing to do for a single entry.  */
     628                 :        1991 :   prevailing = first;
     629                 :        1991 :   if (!prevailing->next_sharing_asm_name)
     630                 :        1952 :     return;
     631                 :             : 
     632                 :             :   /* Try to merge each entry with the prevailing one.  */
     633                 :             :   symtab_node *last_prevailing = prevailing, *next;
     634                 :        4612 :   for (e = prevailing->next_sharing_asm_name; e; e = next)
     635                 :             :     {
     636                 :        2621 :       next = e->next_sharing_asm_name;
     637                 :             : 
     638                 :             :       /* Skip non-LTO symbols and symbols whose declaration we already
     639                 :             :          visited.  */
     640                 :        2621 :       if (lto_symtab_prevailing_decl (e->decl) != e->decl
     641                 :        2583 :           || !lto_symtab_symbol_p (e)
     642                 :        5190 :           || e->decl == prevailing->decl)
     643                 :        1013 :         continue;
     644                 :             : 
     645                 :        1608 :       if (!lto_symtab_merge (prevailing, e)
     646                 :          48 :           && !diagnosed_p
     647                 :        1656 :           && !DECL_ARTIFICIAL (e->decl))
     648                 :          39 :         mismatches.safe_push (e->decl);
     649                 :             : 
     650                 :             :       symtab_node *this_prevailing;
     651                 :           0 :       for (this_prevailing = prevailing; ;
     652                 :           0 :            this_prevailing = this_prevailing->next_sharing_asm_name)
     653                 :             :         {
     654                 :        1608 :           if (this_prevailing->decl != e->decl
     655                 :        1608 :               && lto_symtab_merge_p (this_prevailing->decl, e->decl))
     656                 :             :             break;
     657                 :          11 :           if (this_prevailing == last_prevailing)
     658                 :             :             {
     659                 :             :               this_prevailing = NULL;
     660                 :             :               break;
     661                 :             :             }
     662                 :             :         }
     663                 :             : 
     664                 :        1608 :       if (this_prevailing)
     665                 :        1597 :         lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
     666                 :             :       /* Maintain LRU list: relink the new prevaililng symbol
     667                 :             :          just after previaling node in the chain and update last_prevailing.
     668                 :             :          Since the number of possible declarations of a given symbol is
     669                 :             :          small, this should be faster than building a hash.  */
     670                 :          11 :       else if (e == prevailing->next_sharing_asm_name)
     671                 :             :         last_prevailing = e;
     672                 :             :       else
     673                 :             :         {
     674                 :           0 :           if (e->next_sharing_asm_name)
     675                 :           0 :             e->next_sharing_asm_name->previous_sharing_asm_name
     676                 :           0 :               = e->previous_sharing_asm_name;
     677                 :           0 :           e->previous_sharing_asm_name->next_sharing_asm_name
     678                 :           0 :             = e->next_sharing_asm_name;
     679                 :           0 :           e->previous_sharing_asm_name = prevailing;
     680                 :           0 :           e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
     681                 :           0 :           prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
     682                 :           0 :           prevailing->next_sharing_asm_name = e;
     683                 :           0 :           if (last_prevailing == prevailing)
     684                 :           0 :             last_prevailing = e;
     685                 :             :         }
     686                 :             :     }
     687                 :        1991 :   if (mismatches.is_empty ())
     688                 :             :     return;
     689                 :             : 
     690                 :             :   /* Diagnose all mismatched re-declarations.  */
     691                 :          78 :   FOR_EACH_VEC_ELT (mismatches, i, decl)
     692                 :             :     {
     693                 :             :       /* Do not diagnose two built-in declarations, there is no useful
     694                 :             :          location in that case.  It also happens for AVR if two built-ins
     695                 :             :          use the same asm name because their libgcc assembler code is the
     696                 :             :          same, see PR78562.  */
     697                 :          39 :       if (DECL_IS_UNDECLARED_BUILTIN (prevailing->decl)
     698                 :          39 :           && DECL_IS_UNDECLARED_BUILTIN (decl))
     699                 :           0 :         continue;
     700                 :             : 
     701                 :          39 :       int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
     702                 :          39 :                                              TREE_TYPE (decl),
     703                 :          39 :                                              DECL_COMDAT (decl));
     704                 :          39 :       if (level)
     705                 :             :         {
     706                 :          39 :           bool diag = false;
     707                 :          39 :           if (level & 2)
     708                 :             :             {
     709                 :             :               /* Silence warning for method and variables which belong
     710                 :             :                  to types which already have ODR violation reported.  Complaining
     711                 :             :                  once is enough.  */
     712                 :           6 :               if (TREE_CODE (decl) != FUNCTION_DECL
     713                 :           0 :                   || TREE_CODE (TREE_TYPE (decl)) != METHOD_TYPE
     714                 :           0 :                   || !TYPE_METHOD_BASETYPE (TREE_TYPE (decl))
     715                 :           0 :                   || !odr_type_p (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)))
     716                 :           6 :                   || !odr_type_violation_reported_p 
     717                 :           0 :                         (TYPE_METHOD_BASETYPE (TREE_TYPE (decl))))
     718                 :           6 :                 diag = warning_at (DECL_SOURCE_LOCATION (decl),
     719                 :             :                                    OPT_Wodr,
     720                 :             :                                    "%qD violates the C++ One Definition Rule",
     721                 :             :                                    decl);
     722                 :             :             }
     723                 :          39 :           if (!diag && (level & 1))
     724                 :          33 :             diag = warning_at (DECL_SOURCE_LOCATION (decl),
     725                 :             :                                OPT_Wlto_type_mismatch,
     726                 :             :                                "type of %qD does not match original "
     727                 :             :                                "declaration", decl);
     728                 :          33 :           if (diag)
     729                 :             :             {
     730                 :          13 :               warn_types_mismatch (TREE_TYPE (prevailing->decl),
     731                 :          13 :                                    TREE_TYPE (decl),
     732                 :          13 :                                    DECL_SOURCE_LOCATION (prevailing->decl),
     733                 :          13 :                                    DECL_SOURCE_LOCATION (decl));
     734                 :          13 :               if ((level & 4)
     735                 :          13 :                   && !TREE_READONLY (prevailing->decl))
     736                 :             :                 tbaa_p = true;
     737                 :             :             }
     738                 :          39 :           diagnosed_p |= diag;
     739                 :             :         }
     740                 :           0 :       else if ((DECL_USER_ALIGN (prevailing->decl)
     741                 :           0 :                 && DECL_USER_ALIGN (decl))
     742                 :           0 :                && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
     743                 :             :         {
     744                 :           0 :           diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
     745                 :             :                                      OPT_Wlto_type_mismatch,
     746                 :             :                                      "alignment of %qD is bigger than "
     747                 :             :                                      "original declaration", decl);
     748                 :             :         }
     749                 :             :       else
     750                 :           0 :         diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
     751                 :             :                                    OPT_Wlto_type_mismatch,
     752                 :             :                                    "size of %qD differ from the size of "
     753                 :             :                                    "original declaration", decl);
     754                 :             :     }
     755                 :          39 :   if (diagnosed_p)
     756                 :          13 :     inform (DECL_SOURCE_LOCATION (prevailing->decl),
     757                 :             :             "%qD was previously declared here", prevailing->decl);
     758                 :          39 :   if (tbaa_p)
     759                 :          13 :     inform (DECL_SOURCE_LOCATION (prevailing->decl),
     760                 :             :             "code may be misoptimized unless "
     761                 :             :             "%<-fno-strict-aliasing%> is used");
     762                 :             : 
     763                 :          39 :   mismatches.release ();
     764                 :             : }
     765                 :             : 
     766                 :             : /* Helper to process the decl chain for the symbol table entry *SLOT.  */
     767                 :             : 
     768                 :             : static void
     769                 :        2083 : lto_symtab_merge_decls_1 (symtab_node *first)
     770                 :             : {
     771                 :        2083 :   symtab_node *e;
     772                 :        2083 :   symtab_node *prevailing;
     773                 :        2083 :   bool diagnosed_p = false;
     774                 :             : 
     775                 :        2083 :   if (dump_file)
     776                 :             :     {
     777                 :           0 :       fprintf (dump_file, "Merging nodes for %s. Candidates:\n",
     778                 :             :                first->asm_name ());
     779                 :           0 :       for (e = first; e; e = e->next_sharing_asm_name)
     780                 :           0 :         if (TREE_PUBLIC (e->decl))
     781                 :           0 :           e->dump (dump_file);
     782                 :             :     }
     783                 :             : 
     784                 :             :   /* Compute the symbol resolutions.  This is a no-op when using the
     785                 :             :      linker plugin and resolution was decided by the linker.  */
     786                 :        2083 :   prevailing = lto_symtab_resolve_symbols (first);
     787                 :             : 
     788                 :             :   /* If there's not a prevailing symbol yet it's an external reference.
     789                 :             :      Happens a lot during ltrans.  Choose the first symbol with a
     790                 :             :      cgraph or a varpool node.  */
     791                 :        2083 :   if (!prevailing)
     792                 :             :     {
     793                 :         185 :       for (prevailing = first;
     794                 :         646 :            prevailing; prevailing = prevailing->next_sharing_asm_name)
     795                 :         554 :         if (lto_symtab_symbol_p (prevailing))
     796                 :             :           break;
     797                 :         461 :       if (!prevailing)
     798                 :             :         return;
     799                 :             :       /* For variables chose with a priority variant with vnode
     800                 :             :          attached (i.e. from unit where external declaration of
     801                 :             :          variable is actually used).
     802                 :             :          When there are multiple variants, chose one with size.
     803                 :             :          This is needed for C++ typeinfos, for example in
     804                 :             :          lto/20081204-1 there are typeifos in both units, just
     805                 :             :          one of them do have size.  */
     806                 :         369 :       if (VAR_P (prevailing->decl))
     807                 :             :         {
     808                 :          77 :           for (e = prevailing->next_sharing_asm_name;
     809                 :         208 :                e; e = e->next_sharing_asm_name)
     810                 :         131 :             if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
     811                 :          23 :                 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
     812                 :         131 :                 && lto_symtab_symbol_p (e))
     813                 :             :               prevailing = e;
     814                 :             :         }
     815                 :             :       /* For functions prefer the non-builtin if one is available.  */
     816                 :         292 :       else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
     817                 :             :         {
     818                 :         652 :           for (e = first; e; e = e->next_sharing_asm_name)
     819                 :         474 :             if (TREE_CODE (e->decl) == FUNCTION_DECL
     820                 :         474 :                 && !fndecl_built_in_p (e->decl)
     821                 :         588 :                 && lto_symtab_symbol_p (e))
     822                 :             :               {
     823                 :             :                 prevailing = e;
     824                 :             :                 break;
     825                 :             :               }
     826                 :             :         }
     827                 :             :     }
     828                 :             : 
     829                 :        1991 :   symtab->symtab_prevail_in_asm_name_hash (prevailing);
     830                 :             : 
     831                 :             :   /* Diagnose mismatched objects.  */
     832                 :        1991 :   for (e = prevailing->next_sharing_asm_name;
     833                 :        4612 :        e; e = e->next_sharing_asm_name)
     834                 :             :     {
     835                 :        5242 :       if (TREE_CODE (prevailing->decl)
     836                 :        2621 :           == TREE_CODE (e->decl))
     837                 :        2621 :         continue;
     838                 :           0 :       if (!lto_symtab_symbol_p (e))
     839                 :           0 :         continue;
     840                 :             : 
     841                 :           0 :       switch (TREE_CODE (prevailing->decl))
     842                 :             :         {
     843                 :           0 :         case VAR_DECL:
     844                 :           0 :           gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
     845                 :           0 :           error_at (DECL_SOURCE_LOCATION (e->decl),
     846                 :             :                     "variable %qD redeclared as function",
     847                 :             :                     prevailing->decl);
     848                 :           0 :           break;
     849                 :             : 
     850                 :           0 :         case FUNCTION_DECL:
     851                 :           0 :           gcc_assert (VAR_P (e->decl));
     852                 :           0 :           error_at (DECL_SOURCE_LOCATION (e->decl),
     853                 :             :                     "function %qD redeclared as variable",
     854                 :             :                     prevailing->decl);
     855                 :           0 :           break;
     856                 :             : 
     857                 :           0 :         default:
     858                 :           0 :           gcc_unreachable ();
     859                 :             :         }
     860                 :             : 
     861                 :             :       diagnosed_p = true;
     862                 :             :     }
     863                 :        1991 :   if (diagnosed_p)
     864                 :           0 :       inform (DECL_SOURCE_LOCATION (prevailing->decl),
     865                 :             :               "previously declared here");
     866                 :             : 
     867                 :             :   /* Merge the chain to the single prevailing decl and diagnose
     868                 :             :      mismatches.  */
     869                 :        1991 :   lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
     870                 :             : 
     871                 :        1991 :   if (dump_file)
     872                 :             :     {
     873                 :           0 :       fprintf (dump_file, "After resolution:\n");
     874                 :           0 :       for (e = prevailing; e; e = e->next_sharing_asm_name)
     875                 :           0 :         e->dump (dump_file);
     876                 :             :     }
     877                 :             : }
     878                 :             : 
     879                 :             : /* Resolve and merge all symbol table chains to a prevailing decl.  */
     880                 :             : 
     881                 :             : void
     882                 :       12737 : lto_symtab_merge_decls (void)
     883                 :             : {
     884                 :       12737 :   symtab_node *node;
     885                 :             : 
     886                 :       12737 :   gcc_assert (!dump_file);
     887                 :       12737 :   dump_file = dump_begin (decl_merge_dump_id, NULL);
     888                 :             : 
     889                 :             :   /* Populate assembler name hash.   */
     890                 :       12737 :   symtab->symtab_initialize_asm_name_hash ();
     891                 :             : 
     892                 :      164454 :   FOR_EACH_SYMBOL (node)
     893                 :      151717 :     if (!node->previous_sharing_asm_name
     894                 :      149004 :         && node->next_sharing_asm_name)
     895                 :        2083 :       lto_symtab_merge_decls_1 (node);
     896                 :             : 
     897                 :       12737 :   if (dump_file)
     898                 :           0 :     dump_end (decl_merge_dump_id, dump_file);
     899                 :       12737 :   dump_file = NULL;
     900                 :       12737 : }
     901                 :             : 
     902                 :             : /* Helper to process the decl chain for the symbol table entry *SLOT.  */
     903                 :             : 
     904                 :             : static void
     905                 :        1991 : lto_symtab_merge_symbols_1 (symtab_node *prevailing)
     906                 :             : {
     907                 :        1991 :   symtab_node *e;
     908                 :        1991 :   symtab_node *next;
     909                 :             : 
     910                 :        1991 :   prevailing->decl->decl_with_vis.symtab_node = prevailing;
     911                 :             : 
     912                 :             :   /* Replace the cgraph node of each entry with the prevailing one.  */
     913                 :        1991 :   for (e = prevailing->next_sharing_asm_name; e;
     914                 :             :        e = next)
     915                 :             :     {
     916                 :        2621 :       next = e->next_sharing_asm_name;
     917                 :        2621 :       cgraph_node *ce = dyn_cast <cgraph_node *> (e);
     918                 :             : 
     919                 :          14 :       if ((!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
     920                 :        2621 :           || (ce != NULL && ce->inlined_to))
     921                 :          14 :         continue;
     922                 :        2607 :       symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
     923                 :             : 
     924                 :             :       /* No matter how we are going to deal with resolution, we will ultimately
     925                 :             :          use prevailing definition.  */
     926                 :        2607 :       if (ce)
     927                 :        1892 :         ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
     928                 :             :                               dyn_cast<cgraph_node *> (e));
     929                 :             : 
     930                 :             :       /* If we decided to replace the node by TO, do it.  */
     931                 :        2607 :       if (e != to)
     932                 :             :         {
     933                 :        2596 :           if (ce)
     934                 :        3762 :             lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
     935                 :        5327 :           else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
     936                 :        1430 :             lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
     937                 :             :         }
     938                 :             :       /* Watch out for duplicated symbols for a given declaration.  */
     939                 :          11 :       else if (!e->transparent_alias
     940                 :          11 :                || !e->definition || e->get_alias_target () != to)
     941                 :             :         {
     942                 :             :           /* We got a new declaration we do not want to merge.  In this case
     943                 :             :              get rid of the existing definition and create a transparent
     944                 :             :              alias.  */
     945                 :          11 :           if (ce)
     946                 :             :             {
     947                 :          11 :               lto_free_function_in_decl_state_for_node (ce);
     948                 :          11 :               if (!ce->weakref)
     949                 :          11 :                 ce->release_body ();
     950                 :          11 :               ce->reset ();
     951                 :          11 :               symtab->call_cgraph_removal_hooks (ce);
     952                 :             :             }
     953                 :             :           else
     954                 :             :             {
     955                 :           0 :               DECL_INITIAL (e->decl) = error_mark_node;
     956                 :           0 :               if (e->lto_file_data)
     957                 :             :                 {
     958                 :           0 :                   lto_free_function_in_decl_state_for_node (e);
     959                 :           0 :                   e->lto_file_data = NULL;
     960                 :             :                 }
     961                 :           0 :               symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
     962                 :             :             }
     963                 :          11 :           e->remove_all_references ();
     964                 :          11 :           e->analyzed = e->body_removed = false;
     965                 :          11 :           e->resolve_alias (prevailing, true);
     966                 :          11 :           gcc_assert (e != prevailing);
     967                 :             :         }
     968                 :             :     }
     969                 :             : 
     970                 :        1991 :   return;
     971                 :             : }
     972                 :             : 
     973                 :             : /* Merge cgraph nodes according to the symbol merging done by
     974                 :             :    lto_symtab_merge_decls.  */
     975                 :             : 
     976                 :             : void
     977                 :       12737 : lto_symtab_merge_symbols (void)
     978                 :             : {
     979                 :       12737 :   symtab_node *node;
     980                 :             : 
     981                 :       12737 :   if (!flag_ltrans)
     982                 :             :     {
     983                 :       12737 :       symtab->symtab_initialize_asm_name_hash ();
     984                 :             : 
     985                 :             :       /* Do the actual merging.  
     986                 :             :          At this point we invalidate hash translating decls into symtab nodes
     987                 :             :          because after removing one of duplicate decls the hash is not correcly
     988                 :             :          updated to the other duplicate.  */
     989                 :      164070 :       FOR_EACH_SYMBOL (node)
     990                 :      151333 :         if (lto_symtab_symbol_p (node)
     991                 :      120838 :             && node->next_sharing_asm_name
     992                 :      153918 :             && !node->previous_sharing_asm_name)
     993                 :        1991 :           lto_symtab_merge_symbols_1 (node);
     994                 :             : 
     995                 :             :       /* Resolve weakref aliases whose target are now in the compilation unit.  
     996                 :             :          also re-populate the hash translating decls into symtab nodes*/
     997                 :      162451 :       FOR_EACH_SYMBOL (node)
     998                 :             :         {
     999                 :      149714 :           cgraph_node *cnode, *cnode2;
    1000                 :      149714 :           varpool_node *vnode;
    1001                 :      149714 :           symtab_node *node2;
    1002                 :             : 
    1003                 :      149714 :           if (!node->analyzed && node->alias_target)
    1004                 :             :             {
    1005                 :          20 :               symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
    1006                 :          20 :               gcc_assert (node->weakref);
    1007                 :          20 :               if (tgt)
    1008                 :          12 :                 node->resolve_alias (tgt, true);
    1009                 :             :             }
    1010                 :             :           /* If the symbol was preempted outside IR, see if we want to get rid
    1011                 :             :              of the definition.  */
    1012                 :      149714 :           if (node->analyzed
    1013                 :      127551 :               && !DECL_EXTERNAL (node->decl)
    1014                 :      277058 :               && (node->resolution == LDPR_PREEMPTED_REG
    1015                 :      127337 :                   || node->resolution == LDPR_RESOLVED_IR
    1016                 :      127337 :                   || node->resolution == LDPR_RESOLVED_EXEC
    1017                 :      127336 :                   || node->resolution == LDPR_RESOLVED_DYN))
    1018                 :             :             {
    1019                 :           8 :               DECL_EXTERNAL (node->decl) = 1;
    1020                 :             :               /* If alias to local symbol was preempted by external definition,
    1021                 :             :                  we know it is not pointing to the local symbol.  Remove it.  */
    1022                 :           8 :               if (node->alias
    1023                 :             :                   && !node->weakref
    1024                 :           8 :                   && !node->transparent_alias
    1025                 :           8 :                   && node->get_alias_target ()->binds_to_current_def_p ())
    1026                 :             :                 {
    1027                 :           4 :                   node->alias = false;
    1028                 :           4 :                   node->remove_all_references ();
    1029                 :           4 :                   node->definition = false;
    1030                 :           4 :                   node->analyzed = false;
    1031                 :           4 :                   node->cpp_implicit_alias = false;
    1032                 :             :                 }
    1033                 :           4 :               else if (!node->alias
    1034                 :           4 :                        && node->definition
    1035                 :           4 :                        && node->get_availability () <= AVAIL_INTERPOSABLE)
    1036                 :             :                 {
    1037                 :           1 :                   if ((cnode = dyn_cast <cgraph_node *> (node)) != NULL)
    1038                 :           0 :                     cnode->reset ();
    1039                 :             :                   else
    1040                 :             :                     {
    1041                 :           1 :                       node->analyzed = node->definition = false;
    1042                 :           1 :                       node->remove_all_references ();
    1043                 :             :                     }
    1044                 :             :                 }
    1045                 :             :             }
    1046                 :             : 
    1047                 :      149714 :           if (!(cnode = dyn_cast <cgraph_node *> (node))
    1048                 :      106056 :               || !cnode->clone_of
    1049                 :           0 :               || cnode->clone_of->decl != cnode->decl)
    1050                 :             :             {
    1051                 :             :               /* Builtins are not merged via decl merging.  It is however
    1052                 :             :                  possible that tree merging unified the declaration.  We
    1053                 :             :                  do not want duplicate entries in symbol table.  */
    1054                 :      106056 :               if (cnode && fndecl_built_in_p (node->decl)
    1055                 :       16580 :                   && (cnode2 = cgraph_node::get (node->decl))
    1056                 :      166294 :                   && cnode2 != cnode)
    1057                 :           0 :                 lto_cgraph_replace_node (cnode2, cnode);
    1058                 :             : 
    1059                 :             :               /* The user defined assembler variables are also not unified by their
    1060                 :             :                  symbol name (since it is irrelevant), but we need to unify symbol
    1061                 :             :                  nodes if tree merging occurred.  */
    1062                 :      149714 :               if ((vnode = dyn_cast <varpool_node *> (node))
    1063                 :       43658 :                   && DECL_HARD_REGISTER (vnode->decl)
    1064                 :           6 :                   && (node2 = symtab_node::get (vnode->decl))
    1065                 :           6 :                   && node2 != node)
    1066                 :           1 :                 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
    1067                 :             :                                           vnode);
    1068                 :             :           
    1069                 :             : 
    1070                 :             :               /* Abstract functions may have duplicated cgraph nodes attached;
    1071                 :             :                  remove them.  */
    1072                 :      106056 :               else if (cnode && DECL_ABSTRACT_P (cnode->decl)
    1073                 :           0 :                        && (cnode2 = cgraph_node::get (node->decl))
    1074                 :      149713 :                        && cnode2 != cnode)
    1075                 :           0 :                 cnode2->remove ();
    1076                 :             : 
    1077                 :      149714 :               node->decl->decl_with_vis.symtab_node = node;
    1078                 :             :             }
    1079                 :             :         }
    1080                 :             :     }
    1081                 :       12737 : }
    1082                 :             : 
    1083                 :             : /* Virtual tables may matter for code generation even if they are not
    1084                 :             :    directly referenced by the code because they may be used for
    1085                 :             :    devirtualization.
    1086                 :             :    For this reason it is important to merge even virtual tables that have no
    1087                 :             :    associated symbol table entries.  Without doing so we lose optimization
    1088                 :             :    oppurtunities by losing track of the vtable constructor.
    1089                 :             :    FIXME: we probably ought to introduce explicit symbol table entries for
    1090                 :             :    those before streaming.  */
    1091                 :             : 
    1092                 :             : tree
    1093                 :         992 : lto_symtab_prevailing_virtual_decl (tree decl)
    1094                 :             : {
    1095                 :         992 :   if (DECL_ABSTRACT_P (decl))
    1096                 :             :     return decl;
    1097                 :             : 
    1098                 :         647 :   if (type_in_anonymous_namespace_p (DECL_CONTEXT (decl)))
    1099                 :             :     /* There can't be any other declarations.  */
    1100                 :             :     return decl;
    1101                 :             : 
    1102                 :         646 :   gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
    1103                 :             : 
    1104                 :         646 :   symtab_node *n = symtab_node::get_for_asmname
    1105                 :         646 :                      (DECL_ASSEMBLER_NAME (decl));
    1106                 :        1292 :   while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
    1107                 :          52 :                || !DECL_VIRTUAL_P (n->decl)))
    1108                 :           0 :     n = n->next_sharing_asm_name;
    1109                 :         646 :   if (n)
    1110                 :             :     {
    1111                 :             :       /* Merge decl state in both directions, we may still end up using
    1112                 :             :          the other decl.  */
    1113                 :          52 :       TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
    1114                 :          52 :       TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
    1115                 :             : 
    1116                 :          52 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    1117                 :             :         {
    1118                 :             :           /* Merge decl state in both directions, we may still end up using
    1119                 :             :              the other decl.  */
    1120                 :          26 :           DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
    1121                 :          26 :           DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
    1122                 :             :         }
    1123                 :          52 :       lto_symtab_prevail_decl (n->decl, decl);
    1124                 :          52 :       decl = n->decl;
    1125                 :             :     }
    1126                 :             :   else
    1127                 :         594 :     symtab_node::get_create (decl);
    1128                 :             : 
    1129                 :             :   return decl;
    1130                 :             : }
        

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