LCOV - code coverage report
Current view: top level - gcc - ipa-comdats.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 151 151
Test Date: 2024-04-20 14:03:02 Functions: 100.0 % 9 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Localize comdats.
       2                 :             :    Copyright (C) 2014-2024 Free Software Foundation, Inc.
       3                 :             : 
       4                 :             : This file is part of GCC.
       5                 :             : 
       6                 :             : GCC is free software; you can redistribute it and/or modify it under
       7                 :             : the terms of the GNU General Public License as published by the Free
       8                 :             : Software Foundation; either version 3, or (at your option) any later
       9                 :             : version.
      10                 :             : 
      11                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14                 :             : for more details.
      15                 :             : 
      16                 :             : You should have received a copy of the GNU General Public License
      17                 :             : along with GCC; see the file COPYING3.  If not see
      18                 :             : <http://www.gnu.org/licenses/>.  */
      19                 :             : 
      20                 :             : /* This is very simple pass that looks for static symbols that are used
      21                 :             :    exclusively by symbol within one comdat group.  In this case it makes
      22                 :             :    sense to bring the symbol itself into the group to avoid dead code
      23                 :             :    that would arrise when the comdat group from current unit is replaced
      24                 :             :    by a different copy.  Consider for example:
      25                 :             : 
      26                 :             :     static int q(void)
      27                 :             :     {
      28                 :             :       ....
      29                 :             :     }
      30                 :             :     inline int t(void)
      31                 :             :     {
      32                 :             :       return q();
      33                 :             :     }
      34                 :             : 
      35                 :             :    if Q is used only by T, it makes sense to put Q into T's comdat group.
      36                 :             : 
      37                 :             :    The pass solve simple dataflow across the callgraph trying to prove what
      38                 :             :    symbols are used exclusively from a given comdat group.
      39                 :             : 
      40                 :             :    The implementation maintains a queue linked by AUX pointer terminated by
      41                 :             :    pointer value 1. Lattice values are NULL for TOP, actual comdat group, or
      42                 :             :    ERROR_MARK_NODE for bottom.
      43                 :             : 
      44                 :             :    TODO: When symbol is used only by comdat symbols, but from different groups,
      45                 :             :    it would make sense to produce a new comdat group for it with anonymous name.
      46                 :             : 
      47                 :             :    TODO2: We can't mix variables and functions within one group.  Currently
      48                 :             :    we just give up on references of symbols of different types.  We also should
      49                 :             :    handle this by anonymous comdat group section.  */
      50                 :             : 
      51                 :             : #include "config.h"
      52                 :             : #include "system.h"
      53                 :             : #include "coretypes.h"
      54                 :             : #include "tm.h"
      55                 :             : #include "tree.h"
      56                 :             : #include "tree-pass.h"
      57                 :             : #include "cgraph.h"
      58                 :             : 
      59                 :             : /* Main dataflow loop propagating comdat groups across
      60                 :             :    the symbol table.  All references to SYMBOL are examined
      61                 :             :    and NEWGROUP is updated accordingly. MAP holds current lattice
      62                 :             :    values for individual symbols.  */
      63                 :             : 
      64                 :             : tree
      65                 :       76287 : propagate_comdat_group (struct symtab_node *symbol,
      66                 :             :                         tree newgroup, hash_map<symtab_node *, tree> &map)
      67                 :             : {
      68                 :       76287 :   int i;
      69                 :       76287 :   struct ipa_ref *ref;
      70                 :             : 
      71                 :             :   /* Walk all references to SYMBOL, recursively dive into aliases.  */
      72                 :             : 
      73                 :      100809 :   for (i = 0;
      74                 :      201618 :        symbol->iterate_referring (i, ref)
      75                 :      100809 :        && newgroup != error_mark_node; i++)
      76                 :             :     {
      77                 :       46262 :       struct symtab_node *symbol2 = ref->referring;
      78                 :             : 
      79                 :       46262 :       if (ref->use == IPA_REF_ALIAS)
      80                 :             :         {
      81                 :        1235 :           newgroup = propagate_comdat_group (symbol2, newgroup, map);
      82                 :        1235 :           continue;
      83                 :             :         }
      84                 :             : 
      85                 :             :       /* One COMDAT group cannot hold both variables and functions at
      86                 :             :          a same time.  For now we just go to BOTTOM, in future we may
      87                 :             :          invent special comdat groups for this case.  */
      88                 :             : 
      89                 :       45027 :       if (symbol->type != symbol2->type)
      90                 :             :         {
      91                 :       21740 :           newgroup = error_mark_node;
      92                 :       21740 :           break;
      93                 :             :         }
      94                 :             : 
      95                 :             :       /* If we see inline clone, its comdat group actually
      96                 :             :          corresponds to the comdat group of the function it is inlined
      97                 :             :          to.  */
      98                 :             : 
      99                 :       23287 :       if (cgraph_node * cn = dyn_cast <cgraph_node *> (symbol2))
     100                 :             :         {
     101                 :       13901 :           if (cn->inlined_to)
     102                 :        1849 :             symbol2 = cn->inlined_to;
     103                 :             :         }
     104                 :             : 
     105                 :             :       /* The actual merge operation.  */
     106                 :             : 
     107                 :       23287 :       tree *val2 = map.get (symbol2);
     108                 :             : 
     109                 :       23287 :       if (val2 && *val2 != newgroup)
     110                 :             :         {
     111                 :       21767 :           if (!newgroup)
     112                 :             :             newgroup = *val2;
     113                 :             :           else
     114                 :          12 :             newgroup = error_mark_node;
     115                 :             :         }
     116                 :             :     }
     117                 :             : 
     118                 :             :   /* If we analyze function, walk also callers.  */
     119                 :             : 
     120                 :       76287 :   cgraph_node *cnode = dyn_cast <cgraph_node *> (symbol);
     121                 :             : 
     122                 :       53887 :   if (cnode)
     123                 :       53887 :     for (struct cgraph_edge * edge = cnode->callers;
     124                 :      100385 :          edge && newgroup != error_mark_node; edge = edge->next_caller)
     125                 :             :       {
     126                 :       46498 :         struct symtab_node *symbol2 = edge->caller;
     127                 :             : 
     128                 :       46498 :         if (cgraph_node * cn = dyn_cast <cgraph_node *> (symbol2))
     129                 :             :           {
     130                 :             :             /* Thunks cannot call across section boundary.  */
     131                 :       46498 :             if (cn->thunk)
     132                 :         538 :               newgroup = propagate_comdat_group (symbol2, newgroup, map);
     133                 :             :             /* If we see inline clone, its comdat group actually
     134                 :             :                corresponds to the comdat group of the function it
     135                 :             :                is inlined to.  */
     136                 :       46498 :             if (cn->inlined_to)
     137                 :       16535 :               symbol2 = cn->inlined_to;
     138                 :             :           }
     139                 :             : 
     140                 :             :         /* The actual merge operation.  */
     141                 :             : 
     142                 :       46498 :         tree *val2 = map.get (symbol2);
     143                 :             : 
     144                 :       46498 :         if (val2 && *val2 != newgroup)
     145                 :             :           {
     146                 :       31194 :             if (!newgroup)
     147                 :             :               newgroup = *val2;
     148                 :             :             else
     149                 :        4595 :               newgroup = error_mark_node;
     150                 :             :           }
     151                 :             :       }
     152                 :       76287 :   return newgroup;
     153                 :             : }
     154                 :             : 
     155                 :             : 
     156                 :             : /* Add all references of SYMBOL that are defined into queue started by FIRST
     157                 :             :    and linked by AUX pointer (unless they are already enqueued).
     158                 :             :    Walk recursively inlined functions.  */
     159                 :             : 
     160                 :             : void
     161                 :      157997 : enqueue_references (symtab_node **first,
     162                 :             :                     symtab_node *symbol)
     163                 :             : {
     164                 :      157997 :   int i;
     165                 :      157997 :   struct ipa_ref *ref = NULL;
     166                 :             : 
     167                 :      365409 :   for (i = 0; symbol->iterate_reference (i, ref); i++)
     168                 :             :     {
     169                 :      207412 :       symtab_node *node = ref->referred->ultimate_alias_target ();
     170                 :             : 
     171                 :             :       /* Always keep thunks in same sections as target function.  */
     172                 :      207412 :       if (is_a <cgraph_node *>(node))
     173                 :       23628 :         node = dyn_cast <cgraph_node *> (node)->function_symbol ();
     174                 :      207412 :       if (!node->aux && node->definition)
     175                 :             :         {
     176                 :        1276 :            node->aux = *first;
     177                 :        1276 :            *first = node;
     178                 :             :         }
     179                 :             :     }
     180                 :             : 
     181                 :      157997 :   if (cgraph_node *cnode = dyn_cast <cgraph_node *> (symbol))
     182                 :             :     {
     183                 :      136628 :       struct cgraph_edge *edge;
     184                 :             : 
     185                 :      544202 :       for (edge = cnode->callees; edge; edge = edge->next_callee)
     186                 :      407574 :         if (!edge->inline_failed)
     187                 :       87748 :           enqueue_references (first, edge->callee);
     188                 :             :         else
     189                 :             :           {
     190                 :      319826 :             symtab_node *node = edge->callee->ultimate_alias_target ();
     191                 :             : 
     192                 :             :             /* Always keep thunks in same sections as target function.  */
     193                 :      319826 :             if (is_a <cgraph_node *>(node))
     194                 :      319826 :               node = dyn_cast <cgraph_node *> (node)->function_symbol ();
     195                 :      319826 :             if (!node->aux && node->definition)
     196                 :             :               {
     197                 :        2973 :                  node->aux = *first;
     198                 :        2973 :                  *first = node;
     199                 :             :               }
     200                 :             :           }
     201                 :             :     }
     202                 :      157997 : }
     203                 :             : 
     204                 :             : /* Set comdat group of SYMBOL to GROUP.
     205                 :             :    Callback for for_node_and_aliases.  */
     206                 :             : 
     207                 :             : bool
     208                 :        6607 : set_comdat_group (symtab_node *symbol,
     209                 :             :                   void *head_p)
     210                 :             : {
     211                 :        6607 :   symtab_node *head = (symtab_node *)head_p;
     212                 :             : 
     213                 :        6607 :   gcc_assert (!symbol->get_comdat_group ());
     214                 :        6607 :   if (symbol->real_symbol_p ())
     215                 :             :     {
     216                 :        6607 :       symbol->set_comdat_group (head->get_comdat_group ());
     217                 :        6607 :       symbol->add_to_same_comdat_group (head);
     218                 :             :     }
     219                 :        6607 :   return false;
     220                 :             : }
     221                 :             : 
     222                 :             : /* Set comdat group of SYMBOL to GROUP.
     223                 :             :    Callback for for_node_thunks_and_aliases.  */
     224                 :             : 
     225                 :             : bool
     226                 :        6601 : set_comdat_group_1 (cgraph_node *symbol,
     227                 :             :                     void *head_p)
     228                 :             : {
     229                 :        6601 :   return set_comdat_group (symbol, head_p);
     230                 :             : }
     231                 :             : 
     232                 :             : /* The actual pass with the main dataflow loop.  */
     233                 :             : 
     234                 :             : static unsigned int
     235                 :      227672 : ipa_comdats (void)
     236                 :             : {
     237                 :      227672 :   hash_map<symtab_node *, tree> map (251);
     238                 :      227672 :   hash_map<tree, symtab_node *> comdat_head_map (251);
     239                 :      227672 :   symtab_node *symbol;
     240                 :      227672 :   bool comdat_group_seen = false;
     241                 :      227672 :   symtab_node *first = (symtab_node *) (void *) 1;
     242                 :      227672 :   tree group;
     243                 :             : 
     244                 :             :   /* Start the dataflow by assigning comdat group to symbols that are in comdat
     245                 :             :      groups already.  All other externally visible symbols must stay, we use
     246                 :             :      ERROR_MARK_NODE as bottom for the propagation.  */
     247                 :             : 
     248                 :    11370212 :   FOR_EACH_DEFINED_SYMBOL (symbol)
     249                 :     5457434 :     if (!symbol->real_symbol_p ())
     250                 :             :       ;
     251                 :     4332349 :     else if ((group = symbol->get_comdat_group ()) != NULL)
     252                 :             :       {
     253                 :      520768 :         map.put (symbol, group);
     254                 :      520768 :         comdat_head_map.put (group, symbol);
     255                 :      520768 :         comdat_group_seen = true;
     256                 :             : 
     257                 :             :         /* Mark the symbol so we won't waste time visiting it for dataflow.  */
     258                 :      520768 :         symbol->aux = (symtab_node *) (void *) 1;
     259                 :             :       }
     260                 :             :     /* See symbols that cannot be privatized to comdats; that is externally
     261                 :             :        visible symbols or otherwise used ones.  We also do not want to mangle
     262                 :             :        user section names.  */
     263                 :     3811581 :     else if (symbol->externally_visible
     264                 :             :              || symbol->force_output
     265                 :     3811581 :              || symbol->used_from_other_partition
     266                 :     1461026 :              || TREE_THIS_VOLATILE (symbol->decl)
     267                 :     1454810 :              || symbol->get_section ()
     268                 :     4230747 :              || (TREE_CODE (symbol->decl) == FUNCTION_DECL
     269                 :      173355 :                  && (DECL_STATIC_CONSTRUCTOR (symbol->decl)
     270                 :      171718 :                      || DECL_STATIC_DESTRUCTOR (symbol->decl))))
     271                 :             :       {
     272                 :     3394097 :         symtab_node *target = symbol->ultimate_alias_target ();
     273                 :             : 
     274                 :             :         /* Always keep thunks in same sections as target function.  */
     275                 :     3394097 :         if (is_a <cgraph_node *>(target))
     276                 :     1107036 :           target = dyn_cast <cgraph_node *> (target)->function_symbol ();
     277                 :     3394097 :         map.put (target, error_mark_node);
     278                 :             : 
     279                 :             :         /* Mark the symbol so we won't waste time visiting it for dataflow.  */
     280                 :     3394097 :         symbol->aux = (symtab_node *) (void *) 1;
     281                 :             :       }
     282                 :             :     else
     283                 :             :       {
     284                 :             :         /* Enqueue symbol for dataflow.  */
     285                 :      417484 :         symbol->aux = first;
     286                 :      417484 :         first = symbol;
     287                 :             :       }
     288                 :             : 
     289                 :      227672 :   if (!comdat_group_seen)
     290                 :             :     {
     291                 :     2815895 :       FOR_EACH_DEFINED_SYMBOL (symbol)
     292                 :     2413313 :         symbol->aux = NULL;
     293                 :             :       return 0;
     294                 :             :     }
     295                 :             : 
     296                 :             :   /* The actual dataflow.  */
     297                 :             : 
     298                 :      100902 :   while (first != (void *) 1)
     299                 :             :     {
     300                 :       74521 :       tree group = NULL;
     301                 :       74521 :       tree newgroup, *val;
     302                 :             : 
     303                 :       74521 :       symbol = first;
     304                 :       74521 :       first = (symtab_node *)first->aux;
     305                 :             : 
     306                 :             :       /* Get current lattice value of SYMBOL.  */
     307                 :       74521 :       val = map.get (symbol);
     308                 :       74521 :       if (val)
     309                 :         212 :         group = *val;
     310                 :             : 
     311                 :             :       /* If it is bottom, there is nothing to do; do not clear AUX
     312                 :             :          so we won't re-queue the symbol.  */
     313                 :       74521 :       if (group == error_mark_node)
     314                 :        4272 :         continue;
     315                 :             : 
     316                 :       74514 :       newgroup = propagate_comdat_group (symbol, group, map);
     317                 :             : 
     318                 :             :       /* If nothing changed, proceed to next symbol.  */
     319                 :       74514 :       if (newgroup == group)
     320                 :             :         {
     321                 :        4265 :           symbol->aux = NULL;
     322                 :        4265 :           continue;
     323                 :             :         }
     324                 :             : 
     325                 :             :       /* Update lattice value and enqueue all references for re-visiting.  */
     326                 :       70249 :       gcc_assert (newgroup);
     327                 :       70249 :       if (val)
     328                 :         155 :         *val = newgroup;
     329                 :             :       else
     330                 :       70094 :         map.put (symbol, newgroup);
     331                 :       70249 :       enqueue_references (&first, symbol);
     332                 :             : 
     333                 :             :       /* We may need to revisit the symbol unless it is BOTTOM.  */
     334                 :       70249 :       if (newgroup != error_mark_node)
     335                 :        6913 :         symbol->aux = NULL;
     336                 :             :     }
     337                 :             : 
     338                 :             :   /* Finally assign symbols to the sections.  */
     339                 :             : 
     340                 :     6141004 :   FOR_EACH_DEFINED_SYMBOL (symbol)
     341                 :             :     {
     342                 :     3044121 :       struct cgraph_node *fun;
     343                 :     3044121 :       symbol->aux = NULL; 
     344                 :     3044121 :       if (!symbol->get_comdat_group ()
     345                 :     2447318 :           && !symbol->alias
     346                 :     2436527 :           && (!(fun = dyn_cast <cgraph_node *> (symbol))
     347                 :     1195719 :               || !fun->thunk)
     348                 :     5479782 :           && symbol->real_symbol_p ())
     349                 :             :         {
     350                 :     1585047 :           tree *val = map.get (symbol);
     351                 :             : 
     352                 :             :           /* A NULL here means that SYMBOL is unreachable in the definition
     353                 :             :              of ipa-comdats. Either ipa-comdats is wrong about this or someone
     354                 :             :              forgot to cleanup and remove unreachable functions earlier.  */
     355                 :     1585047 :           gcc_assert (val);
     356                 :             : 
     357                 :     1585047 :           tree group = *val;
     358                 :             : 
     359                 :     1585047 :           if (group == error_mark_node)
     360                 :     1578452 :             continue;
     361                 :        6595 :           if (dump_file)
     362                 :             :             {
     363                 :           4 :               fprintf (dump_file, "Localizing symbol\n");
     364                 :           4 :               symbol->dump (dump_file);
     365                 :           4 :               fprintf (dump_file, "To group: %s\n", IDENTIFIER_POINTER (group));
     366                 :             :             }
     367                 :        6595 :           if (is_a <cgraph_node *> (symbol))
     368                 :        6589 :            dyn_cast <cgraph_node *>(symbol)->call_for_symbol_thunks_and_aliases
     369                 :        6589 :                   (set_comdat_group_1,
     370                 :        6589 :                    *comdat_head_map.get (group),
     371                 :             :                    true);
     372                 :             :           else
     373                 :           6 :            symbol->call_for_symbol_and_aliases
     374                 :           6 :                   (set_comdat_group,
     375                 :           6 :                    *comdat_head_map.get (group),
     376                 :             :                    true);
     377                 :             :         }
     378                 :             :     }
     379                 :             : 
     380                 :             : #if 0
     381                 :             :   /* Recompute calls comdat local flag.  This need to be done after all changes
     382                 :             :      are made.  */
     383                 :             :   cgraph_node *function;
     384                 :             :   FOR_EACH_DEFINED_FUNCTION (function)
     385                 :             :     if (function->get_comdat_group ())
     386                 :             :       function->calls_comdat_local = function->check_calls_comdat_local_p ();
     387                 :             : #endif
     388                 :             :   return 0;
     389                 :      227672 : }
     390                 :             : 
     391                 :             : namespace {
     392                 :             : 
     393                 :             : const pass_data pass_data_ipa_comdats =
     394                 :             : {
     395                 :             :   IPA_PASS, /* type */
     396                 :             :   "comdats", /* name */
     397                 :             :   OPTGROUP_NONE, /* optinfo_flags */
     398                 :             :   TV_IPA_COMDATS, /* tv_id */
     399                 :             :   0, /* properties_required */
     400                 :             :   0, /* properties_provided */
     401                 :             :   0, /* properties_destroyed */
     402                 :             :   0, /* todo_flags_start */
     403                 :             :   0, /* todo_flags_finish */
     404                 :             : };
     405                 :             : 
     406                 :             : class pass_ipa_comdats : public ipa_opt_pass_d
     407                 :             : {
     408                 :             : public:
     409                 :      281914 :   pass_ipa_comdats (gcc::context *ctxt)
     410                 :             :     : ipa_opt_pass_d (pass_data_ipa_comdats, ctxt,
     411                 :             :                       NULL, /* generate_summary */
     412                 :             :                       NULL, /* write_summary */
     413                 :             :                       NULL, /* read_summary */
     414                 :             :                       NULL, /* write_optimization_summary */
     415                 :             :                       NULL, /* read_optimization_summary */
     416                 :             :                       NULL, /* stmt_fixup */
     417                 :             :                       0, /* function_transform_todo_flags_start */
     418                 :             :                       NULL, /* function_transform */
     419                 :      281914 :                       NULL) /* variable_transform */
     420                 :      281914 :   {}
     421                 :             : 
     422                 :             :   /* opt_pass methods: */
     423                 :             :   bool gate (function *) final override;
     424                 :      227672 :   unsigned int execute (function *) final override { return ipa_comdats (); }
     425                 :             : 
     426                 :             : }; // class pass_ipa_comdats
     427                 :             : 
     428                 :             : bool
     429                 :      557041 : pass_ipa_comdats::gate (function *)
     430                 :             : {
     431                 :      557041 :   return HAVE_COMDAT_GROUP;
     432                 :             : }
     433                 :             : 
     434                 :             : } // anon namespace
     435                 :             : 
     436                 :             : ipa_opt_pass_d *
     437                 :      281914 : make_pass_ipa_comdats (gcc::context *ctxt)
     438                 :             : {
     439                 :      281914 :   return new pass_ipa_comdats (ctxt);
     440                 :             : }
        

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.