LCOV - code coverage report
Current view: top level - gcc/lto - lto-symtab.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 407 512 79.5 %
Date: 2020-03-28 11:57:23 Functions: 15 15 100.0 %
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-2020 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                 :       1762 : lto_cgraph_replace_node (struct cgraph_node *node,
      44                 :            :                          struct cgraph_node *prevailing_node)
      45                 :            : {
      46                 :       1762 :   struct cgraph_edge *e, *next;
      47                 :       1762 :   bool compatible_p;
      48                 :            : 
      49                 :       1762 :   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                 :       1762 :   if (node->force_output)
      61                 :          0 :     prevailing_node->mark_force_output ();
      62                 :       1762 :   if (node->forced_by_abi)
      63                 :          0 :     prevailing_node->forced_by_abi = true;
      64                 :       1762 :   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                 :       1836 :       && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
      71                 :         69 :     prevailing_node->merged_comdat = true;
      72                 :       1693 :   else if ((node->definition || node->body_removed)
      73                 :          5 :            && DECL_DECLARED_INLINE_P (node->decl)
      74                 :          3 :            && DECL_EXTERNAL (node->decl)
      75                 :       1696 :            && prevailing_node->definition)
      76                 :          3 :     prevailing_node->merged_extern_inline = true;
      77                 :       1762 :   prevailing_node->merged_comdat |= node->merged_comdat;
      78                 :       1762 :   prevailing_node->merged_extern_inline |= node->merged_extern_inline;
      79                 :            : 
      80                 :            :   /* Redirect all incoming edges.  */
      81                 :       1762 :   compatible_p
      82                 :       1762 :     = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
      83                 :       1762 :                           TREE_TYPE (TREE_TYPE (node->decl)));
      84                 :       6748 :   for (e = node->callers; e; e = next)
      85                 :            :     {
      86                 :       4986 :       next = e->next_caller;
      87                 :       4986 :       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                 :       4986 :       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                 :       1762 :   prevailing_node->clone_referring (node);
     100                 :       1762 :   lto_free_function_in_decl_state_for_node (node);
     101                 :            : 
     102                 :       1762 :   if (node->decl != prevailing_node->decl)
     103                 :        978 :     node->release_body ();
     104                 :            : 
     105                 :            :   /* Finally remove the replaced node.  */
     106                 :       1762 :   node->remove ();
     107                 :       1762 : }
     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                 :        715 : lto_varpool_replace_node (varpool_node *vnode,
     114                 :            :                           varpool_node *prevailing_node)
     115                 :            : {
     116                 :        715 :   gcc_assert (!vnode->definition || prevailing_node->definition);
     117                 :        715 :   gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
     118                 :            : 
     119                 :        715 :   prevailing_node->clone_referring (vnode);
     120                 :        715 :   if (vnode->force_output)
     121                 :         11 :     prevailing_node->force_output = true;
     122                 :        715 :   if (vnode->forced_by_abi)
     123                 :          3 :     prevailing_node->forced_by_abi = true;
     124                 :            : 
     125                 :            :   /* Be sure we can garbage collect the initializer.  */
     126                 :        715 :   if (DECL_INITIAL (vnode->decl)
     127                 :        715 :       && 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                 :        715 :   if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
     132                 :         63 :     compare_virtual_tables (prevailing_node, vnode);
     133                 :            : 
     134                 :        715 :   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                 :        715 :   vnode->remove ();
     177                 :        715 : }
     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                 :       2487 : warn_type_compatibility_p (tree prevailing_type, tree type,
     201                 :            :                            bool common_or_extern)
     202                 :            : {
     203                 :       2487 :   int lev = 0;
     204                 :       2487 :   bool odr_p = odr_or_derived_type_p (prevailing_type)
     205                 :       2487 :                && odr_or_derived_type_p (type);
     206                 :            : 
     207                 :       2487 :   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                 :        967 :   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                 :        967 :   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
     218                 :            :     {
     219                 :        350 :       if (TREE_CODE (type) != TREE_CODE (prevailing_type))
     220                 :          0 :         lev |= 1;
     221                 :        700 :       lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
     222                 :        350 :                                         TREE_TYPE (type), false);
     223                 :        350 :       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                 :        660 :       if (prototype_p (prevailing_type) && prototype_p (type)
     228                 :        714 :           && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
     229                 :            :         {
     230                 :         97 :           tree parm1, parm2;
     231                 :         97 :           for (parm1 = TYPE_ARG_TYPES (prevailing_type),
     232                 :        627 :                parm2 = TYPE_ARG_TYPES (type);
     233                 :        627 :                parm1 && parm2;
     234                 :        530 :                parm1 = TREE_CHAIN (parm1),
     235                 :        530 :                parm2 = TREE_CHAIN (parm2))
     236                 :       1060 :             lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
     237                 :        530 :                                               TREE_VALUE (parm2), false);
     238                 :         97 :           if (parm1 || parm2)
     239                 :         24 :             lev |= odr_p ? 3 : 1;
     240                 :            :         }
     241                 :        350 :       if (comp_type_attributes (prevailing_type, type) == 0)
     242                 :          0 :         lev |= 1;
     243                 :        350 :       return lev;
     244                 :            :     }
     245                 :            : 
     246                 :            :   /* Get complete type.  */
     247                 :        617 :   prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
     248                 :        617 :   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                 :        617 :   if (COMPLETE_TYPE_P (prevailing_type)
     254                 :        599 :       && COMPLETE_TYPE_P (type)
     255                 :            :       /* While global declarations are never variadic, we can recurse here
     256                 :            :          for function parameter types.  */
     257                 :        548 :       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
     258                 :        548 :       && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
     259                 :       1165 :       && !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                 :        617 :   if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
     276                 :            :     {
     277                 :        599 :       alias_set_type set1 = get_alias_set (type);
     278                 :        599 :       alias_set_type set2 = get_alias_set (prevailing_type);
     279                 :            : 
     280                 :        599 :       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                 :        239 :           while (TREE_CODE (t1) == ARRAY_TYPE
     287                 :          6 :                  && !TYPE_NONALIASED_COMPONENT (t1)
     288                 :          6 :                  && TREE_CODE (t2) == ARRAY_TYPE
     289                 :        245 :                  && !TYPE_NONALIASED_COMPONENT (t2))
     290                 :            :             {
     291                 :          6 :               t1 = TREE_TYPE (t1);
     292                 :          6 :               t2 = TREE_TYPE (t2);
     293                 :            :             }
     294                 :        184 :           if ((!POINTER_TYPE_P (t1) || !POINTER_TYPE_P (t2))
     295                 :        415 :               || (set1 != TYPE_ALIAS_SET (ptr_type_node)
     296                 :        176 :                   && 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                 :       1490 : lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
     310                 :            : {
     311                 :       1490 :   tree prevailing_decl = prevailing->decl;
     312                 :       1490 :   tree decl = entry->decl;
     313                 :            : 
     314                 :       1490 :   if (prevailing_decl == decl)
     315                 :            :     return true;
     316                 :            : 
     317                 :       1490 :   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                 :       1490 :   TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
     323                 :       1490 :   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                 :       1490 :   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                 :        966 :       DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
     333                 :        966 :       DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
     334                 :            : 
     335                 :       2898 :       if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
     336                 :        966 :                                      TREE_TYPE (decl),
     337                 :        966 :                                      DECL_COMMON (decl)
     338                 :       1932 :                                      || DECL_EXTERNAL (decl)))
     339                 :            :         return false;
     340                 :            : 
     341                 :        946 :       return true;
     342                 :            :     }
     343                 :            : 
     344                 :       1572 :   if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
     345                 :        524 :                                  TREE_TYPE (decl),
     346                 :       1027 :                                  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                 :        570 :   if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
     358                 :        526 :       && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
     359                 :            :     return false;
     360                 :            : 
     361                 :        940 :   if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
     362                 :        940 :       && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl)))
     363                 :            :       {
     364                 :         22 :         if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
     365                 :            :           return false;
     366                 :            : 
     367                 :         14 :         tree type = TREE_TYPE (decl);
     368                 :            : 
     369                 :            :         /* For record type, check for array at the end of the structure.  */
     370                 :         14 :         if (TREE_CODE (type) == RECORD_TYPE)
     371                 :            :           {
     372                 :          8 :             tree field = TYPE_FIELDS (type);
     373                 :         18 :             while (DECL_CHAIN (field) != NULL_TREE)
     374                 :         18 :               field = DECL_CHAIN (field);
     375                 :            : 
     376                 :          8 :             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                 :         12 :                  || (TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type))))
     386                 :          0 :           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                 :        643 : lto_symtab_resolve_replaceable_p (symtab_node *e)
     397                 :            : {
     398                 :        643 :   if (DECL_EXTERNAL (e->decl)
     399                 :        643 :       || DECL_COMDAT (e->decl)
     400                 :        571 :       || DECL_ONE_ONLY (e->decl)
     401                 :       1214 :       || DECL_WEAK (e->decl))
     402                 :         75 :     return true;
     403                 :            : 
     404                 :        568 :   if (TREE_CODE (e->decl) == VAR_DECL)
     405                 :        184 :     return (DECL_COMMON (e->decl)
     406                 :        184 :             || (!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                 :     145711 : lto_symtab_symbol_p (symtab_node *e)
     417                 :            : {
     418                 :     145711 :   if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
     419                 :            :     return false;
     420                 :     114491 :   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                 :       4299 : lto_symtab_resolve_can_prevail_p (symtab_node *e)
     427                 :            : {
     428                 :       4299 :   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                 :       3925 :   if (DECL_EXTERNAL (e->decl))
     435                 :            :     return false;
     436                 :            : 
     437                 :        712 :   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                 :       1962 : lto_symtab_resolve_symbols (symtab_node *first)
     445                 :            : {
     446                 :       1962 :   symtab_node *e;
     447                 :       1962 :   symtab_node *prevailing = NULL;
     448                 :            : 
     449                 :            :   /* Always set e->node so that edges are updated to reflect decl merging. */
     450                 :       5170 :   for (e = first; e; e = e->next_sharing_asm_name)
     451                 :       4108 :     if (lto_symtab_symbol_p (e)
     452                 :       4108 :         && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
     453                 :       3919 :             || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
     454                 :       3019 :             || 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                 :       1962 :   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                 :       1345 :       for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
     468                 :        445 :         if (lto_symtab_symbol_p (e)
     469                 :        437 :             && !DECL_COMMON (prevailing->decl)
     470                 :        427 :             && !DECL_COMMON (e->decl)
     471                 :        445 :             && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
     472                 :        419 :                 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
     473                 :        419 :                 || 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                 :       3785 :   for (e = first; e; e = e->next_sharing_asm_name)
     482                 :            :     {
     483                 :       2723 :       if (!lto_symtab_resolve_can_prevail_p (e))
     484                 :       2080 :         continue;
     485                 :            : 
     486                 :            :       /* If we have a non-replaceable definition it prevails.  */
     487                 :        643 :       if (!lto_symtab_resolve_replaceable_p (e))
     488                 :            :         {
     489                 :        555 :           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                 :       1062 :   if (prevailing)
     500                 :            :     return prevailing;
     501                 :            : 
     502                 :            :   /* Do a second round choosing one from the replaceable prevailing decls.  */
     503                 :       2063 :   for (e = first; e; e = e->next_sharing_asm_name)
     504                 :            :     {
     505                 :       1576 :       if (!lto_symtab_resolve_can_prevail_p (e))
     506                 :       1507 :         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                 :       1490 : lto_symtab_merge_p (tree prevailing, tree decl)
     545                 :            : {
     546                 :       1490 :   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                 :       1490 :   gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
     554                 :            :   
     555                 :       1490 :   if (TREE_CODE (prevailing) == FUNCTION_DECL)
     556                 :            :     {
     557                 :        966 :       if (fndecl_built_in_p (prevailing) != fndecl_built_in_p (decl))
     558                 :            :         {
     559                 :          1 :           if (dump_file)
     560                 :          0 :             fprintf (dump_file, "Not merging decls; "
     561                 :            :                      "DECL_BUILT_IN mismatch\n");
     562                 :          1 :           return false;
     563                 :            :         }
     564                 :        965 :       if (fndecl_built_in_p (prevailing)
     565                 :        965 :           && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
     566                 :        238 :               || (DECL_UNCHECKED_FUNCTION_CODE (prevailing)
     567                 :        238 :                   != 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                 :       1489 :   if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
     577                 :            :     {
     578                 :        187 :       tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
     579                 :        187 :       tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
     580                 :        187 :       if ((prev_attr == NULL) != (attr == NULL)
     581                 :        187 :           || (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                 :        187 :       prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
     590                 :        187 :       attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
     591                 :        187 :       if ((prev_attr == NULL) != (attr == NULL)
     592                 :        187 :           || (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                 :        186 :       prev_attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (prevailing));
     601                 :        186 :       attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (decl));
     602                 :        186 :       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                 :       1870 : lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
     619                 :            : {
     620                 :       1870 :   symtab_node *prevailing;
     621                 :       1870 :   symtab_node *e;
     622                 :       1870 :   vec<tree> mismatches = vNULL;
     623                 :       1870 :   unsigned i;
     624                 :       1870 :   tree decl;
     625                 :       1870 :   bool tbaa_p = false;
     626                 :            : 
     627                 :            :   /* Nothing to do for a single entry.  */
     628                 :       1870 :   prevailing = first;
     629                 :       1870 :   if (!prevailing->next_sharing_asm_name)
     630                 :       1831 :     return;
     631                 :            : 
     632                 :            :   /* Try to merge each entry with the prevailing one.  */
     633                 :            :   symtab_node *last_prevailing = prevailing, *next;
     634                 :       4369 :   for (e = prevailing->next_sharing_asm_name; e; e = next)
     635                 :            :     {
     636                 :       2499 :       next = e->next_sharing_asm_name;
     637                 :            : 
     638                 :            :       /* Skip non-LTO symbols and symbols whose declaration we already
     639                 :            :          visited.  */
     640                 :       2499 :       if (lto_symtab_prevailing_decl (e->decl) != e->decl
     641                 :       2462 :           || !lto_symtab_symbol_p (e)
     642                 :       4948 :           || e->decl == prevailing->decl)
     643                 :       1009 :         continue;
     644                 :            : 
     645                 :       1490 :       if (!lto_symtab_merge (prevailing, e)
     646                 :         48 :           && !diagnosed_p
     647                 :       1538 :           && !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                 :       1490 :           if (this_prevailing->decl != e->decl
     655                 :       1490 :               && lto_symtab_merge_p (this_prevailing->decl, e->decl))
     656                 :            :             break;
     657                 :         10 :           if (this_prevailing == last_prevailing)
     658                 :            :             {
     659                 :            :               this_prevailing = NULL;
     660                 :            :               break;
     661                 :            :             }
     662                 :            :         }
     663                 :            : 
     664                 :       1490 :       if (this_prevailing)
     665                 :       1480 :         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                 :         10 :       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                 :       1870 :   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_BUILTIN (prevailing->decl)
     698                 :         39 :           && DECL_IS_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                 :         39 :           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                 :         78 :   mismatches.release ();
     764                 :            : }
     765                 :            : 
     766                 :            : /* Helper to process the decl chain for the symbol table entry *SLOT.  */
     767                 :            : 
     768                 :            : static void
     769                 :       1962 : lto_symtab_merge_decls_1 (symtab_node *first)
     770                 :            : {
     771                 :       1962 :   symtab_node *e;
     772                 :       1962 :   symtab_node *prevailing;
     773                 :       1962 :   bool diagnosed_p = false;
     774                 :            : 
     775                 :       1962 :   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                 :       1962 :   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                 :       1962 :   if (!prevailing)
     792                 :            :     {
     793                 :        185 :       for (prevailing = first;
     794                 :        641 :            prevailing; prevailing = prevailing->next_sharing_asm_name)
     795                 :        549 :         if (lto_symtab_symbol_p (prevailing))
     796                 :            :           break;
     797                 :        456 :       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                 :        364 :       if (TREE_CODE (prevailing->decl) == VAR_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                 :        287 :       else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
     817                 :            :         {
     818                 :        635 :           for (e = first; e; e = e->next_sharing_asm_name)
     819                 :        463 :             if (TREE_CODE (e->decl) == FUNCTION_DECL
     820                 :        463 :                 && !fndecl_built_in_p (e->decl)
     821                 :        578 :                 && lto_symtab_symbol_p (e))
     822                 :            :               {
     823                 :            :                 prevailing = e;
     824                 :            :                 break;
     825                 :            :               }
     826                 :            :         }
     827                 :            :     }
     828                 :            : 
     829                 :       1870 :   symtab->symtab_prevail_in_asm_name_hash (prevailing);
     830                 :            : 
     831                 :            :   /* Diagnose mismatched objects.  */
     832                 :       1870 :   for (e = prevailing->next_sharing_asm_name;
     833                 :       4369 :        e; e = e->next_sharing_asm_name)
     834                 :            :     {
     835                 :       4998 :       if (TREE_CODE (prevailing->decl)
     836                 :       2499 :           == TREE_CODE (e->decl))
     837                 :       2499 :         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 (TREE_CODE (e->decl) == VAR_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                 :       1870 :   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                 :       1870 :   lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
     870                 :            : 
     871                 :       1870 :   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                 :       8642 : lto_symtab_merge_decls (void)
     883                 :            : {
     884                 :       8642 :   symtab_node *node;
     885                 :            : 
     886                 :       8642 :   gcc_assert (!dump_file);
     887                 :       8642 :   dump_file = dump_begin (decl_merge_dump_id, NULL);
     888                 :            : 
     889                 :            :   /* Populate assembler name hash.   */
     890                 :       8642 :   symtab->symtab_initialize_asm_name_hash ();
     891                 :            : 
     892                 :     142706 :   FOR_EACH_SYMBOL (node)
     893                 :     134064 :     if (!node->previous_sharing_asm_name
     894                 :     131473 :         && node->next_sharing_asm_name)
     895                 :       1962 :       lto_symtab_merge_decls_1 (node);
     896                 :            : 
     897                 :       8642 :   if (dump_file)
     898                 :          0 :     dump_end (decl_merge_dump_id, dump_file);
     899                 :       8642 :   dump_file = NULL;
     900                 :       8642 : }
     901                 :            : 
     902                 :            : /* Helper to process the decl chain for the symbol table entry *SLOT.  */
     903                 :            : 
     904                 :            : static void
     905                 :       1870 : lto_symtab_merge_symbols_1 (symtab_node *prevailing)
     906                 :            : {
     907                 :       1870 :   symtab_node *e;
     908                 :       1870 :   symtab_node *next;
     909                 :            : 
     910                 :       1870 :   prevailing->decl->decl_with_vis.symtab_node = prevailing;
     911                 :            : 
     912                 :            :   /* Replace the cgraph node of each entry with the prevailing one.  */
     913                 :       1870 :   for (e = prevailing->next_sharing_asm_name; e;
     914                 :            :        e = next)
     915                 :            :     {
     916                 :       2499 :       next = e->next_sharing_asm_name;
     917                 :       2499 :       cgraph_node *ce = dyn_cast <cgraph_node *> (e);
     918                 :            : 
     919                 :         13 :       if ((!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
     920                 :       2499 :           || (ce != NULL && ce->inlined_to))
     921                 :         13 :         continue;
     922                 :       2486 :       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                 :       2486 :       if (ce)
     927                 :       1772 :         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                 :       2486 :       if (e != to)
     932                 :            :         {
     933                 :       2476 :           if (ce)
     934                 :       3524 :             lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
     935                 :       5083 :           else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
     936                 :       1428 :             lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
     937                 :            :         }
     938                 :            :       /* Watch out for duplicated symbols for a given declaration.  */
     939                 :         10 :       else if (!e->transparent_alias
     940                 :         10 :                || !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                 :         10 :           if (ce)
     946                 :            :             {
     947                 :         10 :               lto_free_function_in_decl_state_for_node (ce);
     948                 :         10 :               if (!ce->weakref)
     949                 :         10 :                 ce->release_body ();
     950                 :         10 :               ce->reset ();
     951                 :         10 :               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                 :         10 :           e->remove_all_references ();
     964                 :         10 :           e->analyzed = e->body_removed = false;
     965                 :         10 :           e->resolve_alias (prevailing, true);
     966                 :         10 :           gcc_assert (e != prevailing);
     967                 :            :         }
     968                 :            :     }
     969                 :            : 
     970                 :       1870 :   return;
     971                 :            : }
     972                 :            : 
     973                 :            : /* Merge cgraph nodes according to the symbol merging done by
     974                 :            :    lto_symtab_merge_decls.  */
     975                 :            : 
     976                 :            : void
     977                 :       8642 : lto_symtab_merge_symbols (void)
     978                 :            : {
     979                 :       8642 :   symtab_node *node;
     980                 :            : 
     981                 :       8642 :   if (!flag_ltrans)
     982                 :            :     {
     983                 :       8642 :       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 ohter dupliate.  */
     989                 :     142375 :       FOR_EACH_SYMBOL (node)
     990                 :     133733 :         if (lto_symtab_symbol_p (node)
     991                 :     103282 :             && node->next_sharing_asm_name
     992                 :     136197 :             && !node->previous_sharing_asm_name)
     993                 :       1870 :           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                 :     140770 :       FOR_EACH_SYMBOL (node)
     998                 :            :         {
     999                 :     132128 :           cgraph_node *cnode, *cnode2;
    1000                 :     132128 :           varpool_node *vnode;
    1001                 :     132128 :           symtab_node *node2;
    1002                 :            : 
    1003                 :     132128 :           if (!node->analyzed && node->alias_target)
    1004                 :            :             {
    1005                 :         21 :               symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
    1006                 :         21 :               gcc_assert (node->weakref);
    1007                 :         21 :               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                 :     132128 :           if (node->analyzed
    1013                 :     113945 :               && !DECL_EXTERNAL (node->decl)
    1014                 :     132128 :               && (node->resolution == LDPR_PREEMPTED_REG
    1015                 :     113734 :                   || node->resolution == LDPR_RESOLVED_IR
    1016                 :     113724 :                   || node->resolution == LDPR_RESOLVED_EXEC
    1017                 :     113723 :                   || node->resolution == LDPR_RESOLVED_DYN))
    1018                 :            :             {
    1019                 :         11 :               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                 :         11 :               if (node->alias
    1023                 :            :                   && !node->weakref
    1024                 :         11 :                   && !node->transparent_alias
    1025                 :         11 :                   && 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                 :          7 :               else if (!node->alias
    1034                 :          7 :                        && node->definition
    1035                 :          7 :                        && 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                 :     132128 :           if (!(cnode = dyn_cast <cgraph_node *> (node))
    1048                 :      92527 :               || !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                 :      92527 :               if (cnode && fndecl_built_in_p (node->decl)
    1055                 :      13708 :                   && (cnode2 = cgraph_node::get (node->decl))
    1056                 :     145836 :                   && 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                 :     132128 :               if ((vnode = dyn_cast <varpool_node *> (node))
    1063                 :      39601 :                   && 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                 :      92527 :               else if (cnode && DECL_ABSTRACT_P (cnode->decl)
    1073                 :          0 :                        && (cnode2 = cgraph_node::get (node->decl))
    1074                 :     132127 :                        && cnode2 != cnode)
    1075                 :          0 :                 cnode2->remove ();
    1076                 :            : 
    1077                 :     132128 :               node->decl->decl_with_vis.symtab_node = node;
    1078                 :            :             }
    1079                 :            :         }
    1080                 :            :     }
    1081                 :       8642 : }
    1082                 :            : 
    1083                 :            : /* Virtual tables may matter for code generation even if they are not
    1084                 :            :    directly refernced by the code because they may be used for devirtualizaiton.
    1085                 :            :    For this reason it is important to merge even virtual tables that have no
    1086                 :            :    associated symbol table entries.  Without doing so we lose optimization
    1087                 :            :    oppurtunities by losing track of the vtable constructor.
    1088                 :            :    FIXME: we probably ought to introduce explicit symbol table entries for
    1089                 :            :    those before streaming.  */
    1090                 :            : 
    1091                 :            : tree
    1092                 :        969 : lto_symtab_prevailing_virtual_decl (tree decl)
    1093                 :            : {
    1094                 :        969 :   if (DECL_ABSTRACT_P (decl))
    1095                 :            :     return decl;
    1096                 :            : 
    1097                 :        594 :   if (type_in_anonymous_namespace_p (DECL_CONTEXT (decl)))
    1098                 :            :     /* There can't be any other declarations.  */
    1099                 :            :     return decl;
    1100                 :            : 
    1101                 :        593 :   gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
    1102                 :            : 
    1103                 :        593 :   symtab_node *n = symtab_node::get_for_asmname
    1104                 :        593 :                      (DECL_ASSEMBLER_NAME (decl));
    1105                 :        593 :   while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
    1106                 :         52 :                || !DECL_VIRTUAL_P (n->decl)))
    1107                 :          0 :     n = n->next_sharing_asm_name;
    1108                 :        593 :   if (n)
    1109                 :            :     {
    1110                 :            :       /* Merge decl state in both directions, we may still end up using
    1111                 :            :          the other decl.  */
    1112                 :         52 :       TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
    1113                 :         52 :       TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
    1114                 :            : 
    1115                 :         52 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    1116                 :            :         {
    1117                 :            :           /* Merge decl state in both directions, we may still end up using
    1118                 :            :              the other decl.  */
    1119                 :         26 :           DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
    1120                 :         26 :           DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
    1121                 :            :         }
    1122                 :         52 :       lto_symtab_prevail_decl (n->decl, decl);
    1123                 :         52 :       decl = n->decl;
    1124                 :            :     }
    1125                 :            :   else
    1126                 :        541 :     symtab_node::get_create (decl);
    1127                 :            : 
    1128                 :            :   return decl;
    1129                 :            : }

Generated by: LCOV version 1.0

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 --enable-host-shared. GCC test suite is run with the built compiler.