LCOV - code coverage report
Current view: top level - gcc/lto - lto-partition.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.7 % 518 475
Test Date: 2024-04-20 14:03:02 Functions: 91.7 % 24 22
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* LTO partitioning logic routines.
       2                 :             :    Copyright (C) 2009-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                 :             : #include "config.h"
      21                 :             : #include "system.h"
      22                 :             : #include "coretypes.h"
      23                 :             : #include "target.h"
      24                 :             : #include "function.h"
      25                 :             : #include "basic-block.h"
      26                 :             : #include "tree.h"
      27                 :             : #include "gimple.h"
      28                 :             : #include "alloc-pool.h"
      29                 :             : #include "stringpool.h"
      30                 :             : #include "cgraph.h"
      31                 :             : #include "lto-streamer.h"
      32                 :             : #include "symbol-summary.h"
      33                 :             : #include "tree-vrp.h"
      34                 :             : #include "sreal.h"
      35                 :             : #include "ipa-cp.h"
      36                 :             : #include "ipa-prop.h"
      37                 :             : #include "ipa-fnsummary.h"
      38                 :             : #include "lto-partition.h"
      39                 :             : 
      40                 :             : vec<ltrans_partition> ltrans_partitions;
      41                 :             : 
      42                 :             : static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
      43                 :             : 
      44                 :             : 
      45                 :             : /* Helper for qsort; compare partitions and return one with smaller order.  */
      46                 :             : 
      47                 :             : static int
      48                 :         833 : cmp_partitions_order (const void *a, const void *b)
      49                 :             : {
      50                 :         833 :   const struct ltrans_partition_def *pa
      51                 :             :      = *(struct ltrans_partition_def *const *)a;
      52                 :         833 :   const struct ltrans_partition_def *pb
      53                 :             :      = *(struct ltrans_partition_def *const *)b;
      54                 :         833 :   int ordera = -1, orderb = -1;
      55                 :             : 
      56                 :         833 :   if (lto_symtab_encoder_size (pa->encoder))
      57                 :         833 :     ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
      58                 :         833 :   if (lto_symtab_encoder_size (pb->encoder))
      59                 :         833 :     orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
      60                 :         833 :   return orderb - ordera;
      61                 :             : }
      62                 :             : 
      63                 :             : /* Create new partition with name NAME.  */
      64                 :             : 
      65                 :             : static ltrans_partition
      66                 :        8324 : new_partition (const char *name)
      67                 :             : {
      68                 :        8324 :   ltrans_partition part = XCNEW (struct ltrans_partition_def);
      69                 :        8324 :   part->encoder = lto_symtab_encoder_new (false);
      70                 :        8324 :   part->name = name;
      71                 :        8324 :   part->insns = 0;
      72                 :        8324 :   part->symbols = 0;
      73                 :        8324 :   ltrans_partitions.safe_push (part);
      74                 :        8324 :   return part;
      75                 :             : }
      76                 :             : 
      77                 :             : /* Free memory used by ltrans datastructures.  */
      78                 :             : 
      79                 :             : void
      80                 :        7967 : free_ltrans_partitions (void)
      81                 :             : {
      82                 :        7967 :   unsigned int idx;
      83                 :        7967 :   ltrans_partition part;
      84                 :       16291 :   for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
      85                 :             :     {
      86                 :        8324 :       if (part->initializers_visited)
      87                 :        1314 :         delete part->initializers_visited;
      88                 :             :       /* Symtab encoder is freed after streaming.  */
      89                 :        8324 :       free (part);
      90                 :             :     }
      91                 :        7967 :   ltrans_partitions.release ();
      92                 :        7967 : }
      93                 :             : 
      94                 :             : /* Return true if symbol is already in some partition.  */
      95                 :             : 
      96                 :             : static inline bool
      97                 :      577315 : symbol_partitioned_p (symtab_node *node)
      98                 :             : {
      99                 :      577315 :   return node->aux;
     100                 :             : }
     101                 :             : 
     102                 :             : /* Add references into the partition.  */
     103                 :             : static void
     104                 :       89308 : add_references_to_partition (ltrans_partition part, symtab_node *node)
     105                 :             : {
     106                 :       89308 :   int i;
     107                 :       89308 :   struct ipa_ref *ref = NULL;
     108                 :             : 
     109                 :             :   /* Add all duplicated references to the partition.  */
     110                 :      275947 :   for (i = 0; node->iterate_reference (i, ref); i++)
     111                 :      186639 :     if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
     112                 :         198 :       add_symbol_to_partition (part, ref->referred);
     113                 :             :     /* References to a readonly variable may be constant foled into its value.
     114                 :             :        Recursively look into the initializers of the constant variable and add
     115                 :             :        references, too.  */
     116                 :      373080 :     else if (is_a <varpool_node *> (ref->referred)
     117                 :      171152 :              && (dyn_cast <varpool_node *> (ref->referred)
     118                 :      171152 :                  ->ctor_useable_for_folding_p ())
     119                 :        9096 :              && !lto_symtab_encoder_in_partition_p (part->encoder, ref->referred))
     120                 :             :       {
     121                 :        7391 :         if (!part->initializers_visited)
     122                 :        1314 :           part->initializers_visited = new hash_set<symtab_node *>;
     123                 :        7391 :         if (!part->initializers_visited->add (ref->referred))
     124                 :        3630 :           add_references_to_partition (part, ref->referred);
     125                 :             :       }
     126                 :       89308 : }
     127                 :             : 
     128                 :             : /* Helper function for add_symbol_to_partition doing the actual dirty work
     129                 :             :    of adding NODE to PART.  */
     130                 :             : 
     131                 :             : static bool
     132                 :       85797 : add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
     133                 :             : {
     134                 :       85797 :   enum symbol_partitioning_class c = node->get_partitioning_class ();
     135                 :       85797 :   struct ipa_ref *ref;
     136                 :       85797 :   symtab_node *node1;
     137                 :             : 
     138                 :             :   /* If NODE is already there, we have nothing to do.  */
     139                 :       85797 :   if (lto_symtab_encoder_in_partition_p (part->encoder, node))
     140                 :             :     return true;
     141                 :             : 
     142                 :             :   /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
     143                 :             :      just once.
     144                 :             : 
     145                 :             :      Be lax about comdats; they may or may not be duplicated and we may
     146                 :             :      end up in need to duplicate keyed comdat because it has unkeyed alias.  */
     147                 :       63886 :   if (c == SYMBOL_PARTITION && !DECL_COMDAT (node->decl)
     148                 :      149416 :       && symbol_partitioned_p (node))
     149                 :             :     return false;
     150                 :             : 
     151                 :             :   /* Be sure that we never try to duplicate partitioned symbol
     152                 :             :      or add external symbol.  */
     153                 :       85678 :   gcc_assert (c != SYMBOL_EXTERNAL
     154                 :             :               && (c == SYMBOL_DUPLICATE || !symbol_partitioned_p (node)));
     155                 :             : 
     156                 :       85678 :   part->symbols++;
     157                 :             : 
     158                 :       85678 :   lto_set_symtab_encoder_in_partition (part->encoder, node);
     159                 :             : 
     160                 :       85678 :   if (symbol_partitioned_p (node))
     161                 :             :     {
     162                 :          16 :       node->in_other_partition = 1;
     163                 :          16 :       if (dump_file)
     164                 :           0 :         fprintf (dump_file,
     165                 :             :                  "Symbol node %s now used in multiple partitions\n",
     166                 :             :                  node->dump_name ());
     167                 :             :     }
     168                 :       85678 :   node->aux = (void *)((size_t)node->aux + 1);
     169                 :             : 
     170                 :       85678 :   if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
     171                 :             :     {
     172                 :       62798 :       struct cgraph_edge *e;
     173                 :       62798 :       if (!node->alias && c == SYMBOL_PARTITION)
     174                 :       31175 :         part->insns += ipa_size_summaries->get (cnode)->size;
     175                 :             : 
     176                 :             :       /* Add all inline clones and callees that are duplicated.  */
     177                 :      309589 :       for (e = cnode->callees; e; e = e->next_callee)
     178                 :      246791 :         if (!e->inline_failed)
     179                 :       21619 :           add_symbol_to_partition_1 (part, e->callee);
     180                 :      225172 :         else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
     181                 :          29 :           add_symbol_to_partition (part, e->callee);
     182                 :             : 
     183                 :             :       /* Add all thunks associated with the function.  */
     184                 :      187007 :       for (e = cnode->callers; e; e = e->next_caller)
     185                 :      124209 :         if (e->caller->thunk && !e->caller->inlined_to)
     186                 :          14 :           add_symbol_to_partition_1 (part, e->caller);
     187                 :             :     }
     188                 :             : 
     189                 :       85678 :   add_references_to_partition (part, node);
     190                 :             : 
     191                 :             :   /* Add all aliases associated with the symbol.  */
     192                 :             : 
     193                 :       95871 :   FOR_EACH_ALIAS (node, ref)
     194                 :       10193 :     if (!ref->referring->transparent_alias)
     195                 :       10189 :       add_symbol_to_partition_1 (part, ref->referring);
     196                 :             :     else
     197                 :             :       {
     198                 :             :         struct ipa_ref *ref2;
     199                 :             :         /* We do not need to add transparent aliases if they are not used.
     200                 :             :            However we must add aliases of transparent aliases if they exist.  */
     201                 :           4 :         FOR_EACH_ALIAS (ref->referring, ref2)
     202                 :             :           {
     203                 :             :             /* Nested transparent aliases are not permitted.  */
     204                 :           0 :             gcc_checking_assert (!ref2->referring->transparent_alias);
     205                 :           0 :             add_symbol_to_partition_1 (part, ref2->referring);
     206                 :             :           }
     207                 :             :       }
     208                 :             : 
     209                 :             :   /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group.  */
     210                 :       85678 :   if (node->same_comdat_group)
     211                 :          86 :     for (node1 = node->same_comdat_group;
     212                 :         136 :          node1 != node; node1 = node1->same_comdat_group)
     213                 :          86 :       if (!node->alias)
     214                 :             :         {
     215                 :          58 :           bool added = add_symbol_to_partition_1 (part, node1);
     216                 :          58 :           gcc_assert (added);
     217                 :             :         }
     218                 :             :   return true;
     219                 :             : }
     220                 :             : 
     221                 :             : /* If symbol NODE is really part of other symbol's definition (i.e. it is
     222                 :             :    internal label, thunk, alias or so), return the outer symbol. 
     223                 :             :    When add_symbol_to_partition_1 is called on the outer symbol it must
     224                 :             :    eventually add NODE, too.  */
     225                 :             : static symtab_node *
     226                 :      579063 : contained_in_symbol (symtab_node *node)
     227                 :             : {
     228                 :             :   /* There is no need to consider transparent aliases to be part of the
     229                 :             :      definition: they are only useful insite the partition they are output
     230                 :             :      and thus we will always see an explicit reference to it.  */
     231                 :      579063 :   if (node->transparent_alias)
     232                 :             :     return node;
     233                 :      579056 :   if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
     234                 :             :     {
     235                 :      550139 :       cnode = cnode->function_symbol ();
     236                 :      550139 :       if (cnode->inlined_to)
     237                 :       73177 :         cnode = cnode->inlined_to;
     238                 :      550139 :       return cnode;
     239                 :             :     }
     240                 :       28917 :   else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
     241                 :       28917 :     return vnode->ultimate_alias_target ();
     242                 :             :   return node;
     243                 :             : }
     244                 :             : 
     245                 :             : /* Add symbol NODE to partition.  When definition of NODE is part
     246                 :             :    of other symbol definition, add the other symbol, too.  */
     247                 :             : 
     248                 :             : static void
     249                 :       53917 : add_symbol_to_partition (ltrans_partition part, symtab_node *node)
     250                 :             : {
     251                 :       53917 :   symtab_node *node1;
     252                 :             : 
     253                 :             :   /* Verify that we do not try to duplicate something that cannot be.  */
     254                 :       53917 :   gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
     255                 :             :                        || !symbol_partitioned_p (node));
     256                 :             : 
     257                 :       54893 :   while ((node1 = contained_in_symbol (node)) != node)
     258                 :             :     node = node1;
     259                 :             : 
     260                 :             :   /* If we have duplicated symbol contained in something we cannot duplicate,
     261                 :             :      we are very badly screwed.  The other way is possible, so we do not
     262                 :             :      assert this in add_symbol_to_partition_1. 
     263                 :             : 
     264                 :             :      Be lax about comdats; they may or may not be duplicated and we may
     265                 :             :      end up in need to duplicate keyed comdat because it has unkeyed alias.  */
     266                 :             : 
     267                 :       53917 :   gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
     268                 :             :               || DECL_COMDAT (node->decl)
     269                 :             :               || !symbol_partitioned_p (node));
     270                 :             : 
     271                 :       53917 :   add_symbol_to_partition_1 (part, node);
     272                 :       53917 : }
     273                 :             : 
     274                 :             : /* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
     275                 :             :    and number of varpool nodes is N_VARPOOL_NODES.  */
     276                 :             : 
     277                 :             : static void
     278                 :           1 : undo_partition (ltrans_partition partition, unsigned int n_nodes)
     279                 :             : {
     280                 :         450 :   while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
     281                 :             :     {
     282                 :         224 :       symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
     283                 :             :                                                    n_nodes);
     284                 :         224 :       partition->symbols--;
     285                 :         224 :       cgraph_node *cnode;
     286                 :             : 
     287                 :             :       /* After UNDO we no longer know what was visited.  */
     288                 :         224 :       if (partition->initializers_visited)
     289                 :           0 :         delete partition->initializers_visited;
     290                 :         224 :       partition->initializers_visited = NULL;
     291                 :             : 
     292                 :         450 :       if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node))
     293                 :         226 :           && node->get_partitioning_class () == SYMBOL_PARTITION)
     294                 :           2 :         partition->insns -= ipa_size_summaries->get (cnode)->size;
     295                 :         224 :       lto_symtab_encoder_delete_node (partition->encoder, node);
     296                 :         224 :       node->aux = (void *)((size_t)node->aux - 1);
     297                 :             :     }
     298                 :           1 : }
     299                 :             : 
     300                 :             : /* Group cgrah nodes by input files.  This is used mainly for testing
     301                 :             :    right now.  */
     302                 :             : 
     303                 :             : void
     304                 :         295 : lto_1_to_1_map (void)
     305                 :             : {
     306                 :         295 :   symtab_node *node;
     307                 :         295 :   struct lto_file_decl_data *file_data;
     308                 :         295 :   hash_map<lto_file_decl_data *, ltrans_partition> pmap;
     309                 :         295 :   ltrans_partition partition;
     310                 :         295 :   int npartitions = 0;
     311                 :             : 
     312                 :        2053 :   FOR_EACH_SYMBOL (node)
     313                 :             :     {
     314                 :        1758 :       if (node->get_partitioning_class () != SYMBOL_PARTITION
     315                 :        1758 :           || symbol_partitioned_p (node))
     316                 :         686 :         continue;
     317                 :             : 
     318                 :        1072 :       file_data = node->lto_file_data;
     319                 :             : 
     320                 :        1072 :       if (file_data)
     321                 :             :         {
     322                 :        1036 :           ltrans_partition *slot = &pmap.get_or_insert (file_data);
     323                 :        1036 :           if (*slot)
     324                 :             :             partition = *slot;
     325                 :             :           else
     326                 :             :             {
     327                 :         479 :               partition = new_partition (file_data->file_name);
     328                 :         479 :               *slot = partition;
     329                 :         479 :               npartitions++;
     330                 :             :             }
     331                 :             :         }
     332                 :          36 :       else if (!file_data && ltrans_partitions.length ())
     333                 :          27 :         partition = ltrans_partitions[0];
     334                 :             :       else
     335                 :             :         {
     336                 :           9 :           partition = new_partition ("");
     337                 :           9 :           npartitions++;
     338                 :             :         }
     339                 :             : 
     340                 :        1072 :       add_symbol_to_partition (partition, node);
     341                 :             :     }
     342                 :             : 
     343                 :             :   /* If the cgraph is empty, create one cgraph node set so that there is still
     344                 :             :      an output file for any variables that need to be exported in a DSO.  */
     345                 :         295 :   if (!npartitions)
     346                 :           2 :     new_partition ("empty");
     347                 :             : 
     348                 :             :   /* Order partitions by order of symbols because they are linked into binary
     349                 :             :      that way.  */
     350                 :         590 :   ltrans_partitions.qsort (cmp_partitions_order);
     351                 :         295 : }
     352                 :             : 
     353                 :             : /* Maximal partitioning.  Put every new symbol into new partition if possible.  */
     354                 :             : 
     355                 :             : void
     356                 :          11 : lto_max_map (void)
     357                 :             : {
     358                 :          11 :   symtab_node *node;
     359                 :          11 :   ltrans_partition partition;
     360                 :          11 :   int npartitions = 0;
     361                 :             : 
     362                 :         219 :   FOR_EACH_SYMBOL (node)
     363                 :             :     {
     364                 :         208 :       if (node->get_partitioning_class () != SYMBOL_PARTITION
     365                 :         208 :           || symbol_partitioned_p (node))
     366                 :          37 :         continue;
     367                 :         171 :       partition = new_partition (node->asm_name ());
     368                 :         171 :       add_symbol_to_partition (partition, node);
     369                 :         171 :       npartitions++;
     370                 :             :     }
     371                 :          11 :   if (!npartitions)
     372                 :           0 :     new_partition ("empty");
     373                 :          11 : }
     374                 :             : 
     375                 :             : /* Helper function for qsort; sort nodes by order.  */
     376                 :             : static int
     377                 :      578370 : node_cmp (const void *pa, const void *pb)
     378                 :             : {
     379                 :      578370 :   const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
     380                 :      578370 :   const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
     381                 :      578370 :   return b->order - a->order;
     382                 :             : }
     383                 :             : 
     384                 :             : /* Add all symtab nodes from NEXT_NODE to PARTITION in order.  */
     385                 :             : 
     386                 :             : static void
     387                 :       30731 : add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
     388                 :             : {
     389                 :       30731 :   unsigned i;
     390                 :       30731 :   symtab_node *node;
     391                 :             : 
     392                 :       30731 :   next_nodes.qsort (node_cmp);
     393                 :       41906 :   FOR_EACH_VEC_ELT (next_nodes, i, node)
     394                 :       11175 :     if (!symbol_partitioned_p (node))
     395                 :        8824 :       add_symbol_to_partition (partition, node);
     396                 :       30731 : }
     397                 :             : 
     398                 :             : /* Return true if we should account reference from N1 to N2 in cost
     399                 :             :    of partition boundary.  */
     400                 :             : 
     401                 :             : bool
     402                 :      665548 : account_reference_p (symtab_node *n1, symtab_node *n2)
     403                 :             : {
     404                 :      665548 :   if (cgraph_node *cnode = dyn_cast <cgraph_node *> (n1))
     405                 :             :     n1 = cnode;
     406                 :             :   /* Do not account references from aliases - they are never split across
     407                 :             :      partitions.  */
     408                 :      665548 :   if (n1->alias)
     409                 :             :     return false;
     410                 :             :   /* Do not account recursion - the code below will handle it incorrectly
     411                 :             :      otherwise.  Do not account references to external symbols: they will
     412                 :             :      never become local.  Finally do not account references to duplicated
     413                 :             :      symbols: they will be always local.  */
     414                 :      645342 :   if (n1 == n2 
     415                 :      645230 :       || !n2->definition
     416                 :     1169653 :       || n2->get_partitioning_class () != SYMBOL_PARTITION)
     417                 :      121172 :     return false;
     418                 :             :   /* If referring node is external symbol do not account it to boundary
     419                 :             :      cost. Those are added into units only to enable possible constant
     420                 :             :      folding and devirtulization.
     421                 :             : 
     422                 :             :      Here we do not know if it will ever be added to some partition
     423                 :             :      (this is decided by compute_ltrans_boundary) and second it is not
     424                 :             :      that likely that constant folding will actually use the reference.  */
     425                 :      524170 :   if (contained_in_symbol (n1)
     426                 :      524170 :         ->get_partitioning_class () == SYMBOL_EXTERNAL)
     427                 :             :     return false;
     428                 :             :   return true;
     429                 :             : }
     430                 :             : 
     431                 :             : 
     432                 :             : /* Group cgraph nodes into equally-sized partitions.
     433                 :             : 
     434                 :             :    The partitioning algorithm is simple: nodes are taken in predefined order.
     435                 :             :    The order corresponds to the order we want functions to have in the final
     436                 :             :    output.  In the future this will be given by function reordering pass, but
     437                 :             :    at the moment we use the topological order, which is a good approximation.
     438                 :             : 
     439                 :             :    The goal is to partition this linear order into intervals (partitions) so
     440                 :             :    that all the partitions have approximately the same size and the number of
     441                 :             :    callgraph or IPA reference edges crossing boundaries is minimal.
     442                 :             : 
     443                 :             :    This is a lot faster (O(n) in size of callgraph) than algorithms doing
     444                 :             :    priority-based graph clustering that are generally O(n^2) and, since
     445                 :             :    WHOPR is designed to make things go well across partitions, it leads
     446                 :             :    to good results.
     447                 :             : 
     448                 :             :    We compute the expected size of a partition as:
     449                 :             : 
     450                 :             :      max (total_size / lto_partitions, min_partition_size)
     451                 :             : 
     452                 :             :    We use dynamic expected size of partition so small programs are partitioned
     453                 :             :    into enough partitions to allow use of multiple CPUs, while large programs
     454                 :             :    are not partitioned too much.  Creating too many partitions significantly
     455                 :             :    increases the streaming overhead.
     456                 :             : 
     457                 :             :    In the future, we would like to bound the maximal size of partitions so as
     458                 :             :    to prevent the LTRANS stage from consuming too much memory.  At the moment,
     459                 :             :    however, the WPA stage is the most memory intensive for large benchmarks,
     460                 :             :    since too many types and declarations are read into memory.
     461                 :             : 
     462                 :             :    The function implements a simple greedy algorithm.  Nodes are being added
     463                 :             :    to the current partition until after 3/4 of the expected partition size is
     464                 :             :    reached.  Past this threshold, we keep track of boundary size (number of
     465                 :             :    edges going to other partitions) and continue adding functions until after
     466                 :             :    the current partition has grown to twice the expected partition size.  Then
     467                 :             :    the process is undone to the point where the minimal ratio of boundary size
     468                 :             :    and in-partition calls was reached.  */
     469                 :             : 
     470                 :             : void
     471                 :        7661 : lto_balanced_map (int n_lto_partitions, int max_partition_size)
     472                 :             : {
     473                 :        7661 :   int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
     474                 :        7661 :   int best_noreorder_pos = 0;
     475                 :        7661 :   auto_vec <cgraph_node *> order (symtab->cgraph_count);
     476                 :        7661 :   auto_vec<cgraph_node *> noreorder;
     477                 :        7661 :   auto_vec<varpool_node *> varpool_order;
     478                 :        7661 :   struct cgraph_node *node;
     479                 :        7661 :   int64_t original_total_size, total_size = 0;
     480                 :        7661 :   int64_t partition_size;
     481                 :        7661 :   ltrans_partition partition;
     482                 :        7661 :   int last_visited_node = 0;
     483                 :        7661 :   varpool_node *vnode;
     484                 :        7661 :   int64_t cost = 0, internal = 0;
     485                 :        7661 :   unsigned int best_n_nodes = 0, best_i = 0;
     486                 :        7661 :   int64_t best_cost = -1, best_internal = 0, best_size = 0;
     487                 :        7661 :   int npartitions;
     488                 :        7661 :   int current_order = -1;
     489                 :        7661 :   int noreorder_pos = 0;
     490                 :             : 
     491                 :       61444 :   FOR_EACH_VARIABLE (vnode)
     492                 :       23061 :     gcc_assert (!vnode->aux);
     493                 :             : 
     494                 :       69455 :   FOR_EACH_DEFINED_FUNCTION (node)
     495                 :       61794 :     if (node->get_partitioning_class () == SYMBOL_PARTITION)
     496                 :             :       {
     497                 :       40220 :         if (node->no_reorder)
     498                 :        8149 :           noreorder.safe_push (node);
     499                 :             :         else
     500                 :       32071 :           order.safe_push (node);
     501                 :       40220 :         if (!node->alias)
     502                 :       30345 :           total_size += ipa_size_summaries->get (node)->size;
     503                 :             :       }
     504                 :             : 
     505                 :        7661 :   original_total_size = total_size;
     506                 :             : 
     507                 :             :   /* Streaming works best when the source units do not cross partition
     508                 :             :      boundaries much.  This is because importing function from a source
     509                 :             :      unit tends to import a lot of global trees defined there.  We should
     510                 :             :      get better about minimizing the function bounday, but until that
     511                 :             :      things works smoother if we order in source order.  */
     512                 :        7661 :   order.qsort (tp_first_run_node_cmp);
     513                 :        7661 :   noreorder.qsort (node_cmp);
     514                 :             : 
     515                 :        7661 :   if (dump_file)
     516                 :             :     {
     517                 :           0 :       for (unsigned i = 0; i < order.length (); i++)
     518                 :           0 :         fprintf (dump_file, "Balanced map symbol order:%s:%u\n",
     519                 :           0 :                  order[i]->dump_name (), order[i]->tp_first_run);
     520                 :           0 :       for (unsigned i = 0; i < noreorder.length (); i++)
     521                 :           0 :         fprintf (dump_file, "Balanced map symbol no_reorder:%s:%u\n",
     522                 :           0 :                  noreorder[i]->dump_name (), noreorder[i]->tp_first_run);
     523                 :             :     }
     524                 :             : 
     525                 :             :   /* Collect all variables that should not be reordered.  */
     526                 :       61444 :   FOR_EACH_VARIABLE (vnode)
     527                 :       23061 :     if (vnode->get_partitioning_class () == SYMBOL_PARTITION
     528                 :       23061 :         && vnode->no_reorder)
     529                 :        1492 :       varpool_order.safe_push (vnode);
     530                 :        7661 :   n_varpool_nodes = varpool_order.length ();
     531                 :        7661 :   varpool_order.qsort (node_cmp);
     532                 :             : 
     533                 :             :   /* Compute partition size and create the first partition.  */
     534                 :        7661 :   if (param_min_partition_size > max_partition_size)
     535                 :           0 :     fatal_error (input_location, "min partition size cannot be greater "
     536                 :             :                  "than max partition size");
     537                 :             : 
     538                 :        7661 :   partition_size = total_size / n_lto_partitions;
     539                 :        7661 :   if (partition_size < param_min_partition_size)
     540                 :             :     partition_size = param_min_partition_size;
     541                 :        7661 :   npartitions = 1;
     542                 :        7661 :   partition = new_partition ("");
     543                 :        7661 :   if (dump_file)
     544                 :           0 :     fprintf (dump_file, "Total unit size: %" PRId64 ", partition size: %" PRId64 "\n",
     545                 :             :              total_size, partition_size);
     546                 :             : 
     547                 :        7661 :   auto_vec<symtab_node *> next_nodes;
     548                 :             : 
     549                 :       79453 :   for (unsigned i = 0; i < order.length (); i++)
     550                 :             :     {
     551                 :       32073 :       if (symbol_partitioned_p (order[i]))
     552                 :        9003 :         continue;
     553                 :             : 
     554                 :       23070 :       current_order = order[i]->order;
     555                 :             : 
     556                 :             :       /* Output noreorder and varpool in program order first.  */
     557                 :       23070 :       next_nodes.truncate (0);
     558                 :       23070 :       while (varpool_pos < n_varpool_nodes
     559                 :       23086 :              && varpool_order[varpool_pos]->order < current_order)
     560                 :          16 :         next_nodes.safe_push (varpool_order[varpool_pos++]);
     561                 :       23580 :       while (noreorder_pos < (int)noreorder.length ()
     562                 :       24998 :              && noreorder[noreorder_pos]->order < current_order)
     563                 :         510 :         next_nodes.safe_push (noreorder[noreorder_pos++]);
     564                 :       23070 :       add_sorted_nodes (next_nodes, partition);
     565                 :             : 
     566                 :       23070 :       if (!symbol_partitioned_p (order[i]))
     567                 :       23061 :         add_symbol_to_partition (partition, order[i]);
     568                 :             :           
     569                 :             : 
     570                 :             :       /* Once we added a new node to the partition, we also want to add
     571                 :             :          all referenced variables unless they was already added into some
     572                 :             :          earlier partition.
     573                 :             :          add_symbol_to_partition adds possibly multiple nodes and
     574                 :             :          variables that are needed to satisfy needs of ORDER[i].
     575                 :             :          We remember last visited cgraph and varpool node from last iteration
     576                 :             :          of outer loop that allows us to process every new addition. 
     577                 :             : 
     578                 :             :          At the same time we compute size of the boundary into COST.  Every
     579                 :             :          callgraph or IPA reference edge leaving the partition contributes into
     580                 :             :          COST.  Every edge inside partition was earlier computed as one leaving
     581                 :             :          it and thus we need to subtract it from COST.  */
     582                 :      197562 :       while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
     583                 :             :         {
     584                 :       75711 :           int j;
     585                 :       75711 :           struct ipa_ref *ref = NULL;
     586                 :       75711 :           symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
     587                 :             :                                                         last_visited_node);
     588                 :             : 
     589                 :       75711 :           if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
     590                 :             :             {
     591                 :       54881 :               struct cgraph_edge *edge;
     592                 :             : 
     593                 :             : 
     594                 :       54881 :               last_visited_node++;
     595                 :             : 
     596                 :       54881 :               gcc_assert (node->definition || node->weakref
     597                 :             :                           || node->declare_variant_alt);
     598                 :             : 
     599                 :             :               /* Compute boundary cost of callgraph edges.  */
     600                 :      291577 :               for (edge = node->callees; edge; edge = edge->next_callee)
     601                 :             :                 /* Inline edges will always end up local.  */
     602                 :      236696 :                 if (edge->inline_failed
     603                 :      236696 :                     && account_reference_p (node, edge->callee))
     604                 :             :                   {
     605                 :       96047 :                     int edge_cost = edge->frequency ();
     606                 :       96047 :                     int index;
     607                 :             : 
     608                 :       96047 :                     if (!edge_cost)
     609                 :             :                       edge_cost = 1;
     610                 :       38741 :                     gcc_assert (edge_cost > 0);
     611                 :      192094 :                     index = lto_symtab_encoder_lookup (partition->encoder,
     612                 :       96047 :                                                        edge->callee);
     613                 :       96047 :                     if (index != LCC_NOT_FOUND
     614                 :       90931 :                         && index < last_visited_node - 1)
     615                 :       90700 :                       cost -= edge_cost, internal += edge_cost;
     616                 :             :                     else
     617                 :        5347 :                       cost += edge_cost;
     618                 :             :                   }
     619                 :      172885 :               for (edge = node->callers; edge; edge = edge->next_caller)
     620                 :      118004 :                 if (edge->inline_failed
     621                 :      118004 :                     && account_reference_p (edge->caller, node))
     622                 :             :                 {
     623                 :       96469 :                   int edge_cost = edge->frequency ();
     624                 :       96469 :                   int index;
     625                 :             : 
     626                 :       96469 :                   gcc_assert (edge->caller->definition);
     627                 :       96469 :                   if (!edge_cost)
     628                 :             :                     edge_cost = 1;
     629                 :       39163 :                   gcc_assert (edge_cost > 0);
     630                 :       96469 :                   index = lto_symtab_encoder_lookup (partition->encoder,
     631                 :             :                                                      edge->caller);
     632                 :       96469 :                   if (index != LCC_NOT_FOUND
     633                 :        4828 :                       && index < last_visited_node - 1)
     634                 :        4482 :                     cost -= edge_cost, internal += edge_cost;
     635                 :             :                   else
     636                 :       91987 :                     cost += edge_cost;
     637                 :             :                 }
     638                 :             :             }
     639                 :             :           else
     640                 :       20830 :             last_visited_node++;
     641                 :             : 
     642                 :             :           /* Compute boundary cost of IPA REF edges and at the same time look into
     643                 :             :              variables referenced from current partition and try to add them.  */
     644                 :      507184 :           for (j = 0; snode->iterate_reference (j, ref); j++)
     645                 :      177881 :             if (!account_reference_p (snode, ref->referred))
     646                 :             :               ;
     647                 :      165853 :             else if (is_a <varpool_node *> (ref->referred))
     648                 :             :               {
     649                 :      163889 :                 int index;
     650                 :             : 
     651                 :      163889 :                 vnode = dyn_cast <varpool_node *> (ref->referred);
     652                 :      163889 :                 if (!symbol_partitioned_p (vnode)
     653                 :       20630 :                     && !vnode->no_reorder
     654                 :      184430 :                     && vnode->get_partitioning_class () == SYMBOL_PARTITION)
     655                 :       20541 :                   add_symbol_to_partition (partition, vnode);
     656                 :      163889 :                 index = lto_symtab_encoder_lookup (partition->encoder,
     657                 :             :                                                    vnode);
     658                 :      163889 :                 if (index != LCC_NOT_FOUND
     659                 :      163800 :                     && index < last_visited_node - 1)
     660                 :      104046 :                   cost--, internal++;
     661                 :             :                 else
     662                 :       59843 :                   cost++;
     663                 :             :               }
     664                 :             :             else
     665                 :             :               {
     666                 :        1964 :                 int index;
     667                 :             : 
     668                 :        1964 :                 node = dyn_cast <cgraph_node *> (ref->referred);
     669                 :        1964 :                 index = lto_symtab_encoder_lookup (partition->encoder,
     670                 :             :                                                    node);
     671                 :        1964 :                 if (index != LCC_NOT_FOUND
     672                 :        1772 :                     && index < last_visited_node - 1)
     673                 :        1771 :                   cost--, internal++;
     674                 :             :                 else
     675                 :         193 :                   cost++;
     676                 :             :               }
     677                 :      251696 :           for (j = 0; snode->iterate_referring (j, ref); j++)
     678                 :      175985 :             if (!account_reference_p (ref->referring, snode))
     679                 :             :               ;
     680                 :      165801 :             else if (is_a <varpool_node *> (ref->referring))
     681                 :             :               {
     682                 :        3118 :                 int index;
     683                 :             : 
     684                 :        3118 :                 vnode = dyn_cast <varpool_node *> (ref->referring);
     685                 :        3118 :                 gcc_assert (vnode->definition);
     686                 :             :                 /* It is better to couple variables with their users,
     687                 :             :                    because it allows them to be removed.  Coupling
     688                 :             :                    with objects they refer to only helps to reduce
     689                 :             :                    number of symbols promoted to hidden.  */
     690                 :        3118 :                 if (!symbol_partitioned_p (vnode)
     691                 :         653 :                     && !vnode->no_reorder
     692                 :         621 :                     && !vnode->can_remove_if_no_refs_p ()
     693                 :        3139 :                     && vnode->get_partitioning_class () == SYMBOL_PARTITION)
     694                 :          21 :                   add_symbol_to_partition (partition, vnode);
     695                 :        3118 :                 index = lto_symtab_encoder_lookup (partition->encoder,
     696                 :             :                                                    vnode);
     697                 :        3118 :                 if (index != LCC_NOT_FOUND
     698                 :        2486 :                     && index < last_visited_node - 1)
     699                 :        2395 :                   cost--, internal++;
     700                 :             :                 else
     701                 :         723 :                   cost++;
     702                 :             :               }
     703                 :             :             else
     704                 :             :               {
     705                 :      162683 :                 int index;
     706                 :             : 
     707                 :      162683 :                 node = dyn_cast <cgraph_node *> (ref->referring);
     708                 :      162683 :                 gcc_assert (node->definition || node->declare_variant_alt);
     709                 :      162683 :                 index = lto_symtab_encoder_lookup (partition->encoder,
     710                 :             :                                                    node);
     711                 :      162683 :                 if (index != LCC_NOT_FOUND
     712                 :       57550 :                     && index < last_visited_node - 1)
     713                 :       57544 :                   cost--, internal++;
     714                 :             :                 else
     715                 :      105139 :                   cost++;
     716                 :             :               }
     717                 :             :         }
     718                 :             : 
     719                 :       23070 :       gcc_assert (cost >= 0 && internal >= 0);
     720                 :             : 
     721                 :             :       /* If the partition is large enough, start looking for smallest boundary cost.
     722                 :             :          If partition still seems too small (less than 7/8 of target weight) accept
     723                 :             :          any cost.  If partition has right size, optimize for highest internal/cost.
     724                 :             :          Later we stop building partition if its size is 9/8 of the target wight.  */
     725                 :       23070 :       if (partition->insns < partition_size * 7 / 8
     726                 :          43 :           || best_cost == -1
     727                 :       23111 :           || (!cost 
     728                 :          40 :               || ((sreal)best_internal * (sreal) cost
     729                 :          80 :                   < ((sreal) internal * (sreal)best_cost))))
     730                 :             :         {
     731                 :       23059 :           best_cost = cost;
     732                 :       23059 :           best_internal = internal;
     733                 :       23059 :           best_size = partition->insns;
     734                 :       23059 :           best_i = i;
     735                 :       23059 :           best_n_nodes = lto_symtab_encoder_size (partition->encoder);
     736                 :       23059 :           best_varpool_pos = varpool_pos;
     737                 :       23059 :           best_noreorder_pos = noreorder_pos;
     738                 :             :         }
     739                 :       23070 :       if (dump_file)
     740                 :           0 :         fprintf (dump_file, "Step %i: added %s, size %i, "
     741                 :             :                  "cost %" PRId64 "/%" PRId64 " "
     742                 :             :                  "best %" PRId64 "/%" PRId64", step %i\n", i,
     743                 :           0 :                  order[i]->dump_name (),
     744                 :             :                  partition->insns, cost, internal,
     745                 :             :                  best_cost, best_internal, best_i);
     746                 :             :       /* Partition is too large, unwind into step when best cost was reached and
     747                 :             :          start new partition.  */
     748                 :       23070 :       if (partition->insns > 9 * partition_size / 8
     749                 :       23066 :           || partition->insns > max_partition_size)
     750                 :             :         {
     751                 :           4 :           if (best_i != i)
     752                 :             :             {
     753                 :           1 :               if (dump_file)
     754                 :           0 :                 fprintf (dump_file, "Unwinding %i insertions to step %i\n",
     755                 :             :                          i - best_i, best_i);
     756                 :           1 :               undo_partition (partition, best_n_nodes);
     757                 :           1 :               varpool_pos = best_varpool_pos;
     758                 :           1 :               noreorder_pos = best_noreorder_pos;
     759                 :             :             }
     760                 :           4 :           gcc_assert (best_size == partition->insns);
     761                 :           4 :           i = best_i;
     762                 :           4 :           if (dump_file)
     763                 :           0 :             fprintf (dump_file,
     764                 :             :                      "Partition insns: %i (want %" PRId64 ")\n",
     765                 :             :                      partition->insns, partition_size);
     766                 :             :           /* When we are finished, avoid creating empty partition.  */
     767                 :           4 :           while (i < order.length () - 1 && symbol_partitioned_p (order[i + 1]))
     768                 :             :             i++;
     769                 :           4 :           if (i == order.length () - 1)
     770                 :             :             break;
     771                 :           2 :           total_size -= partition->insns;
     772                 :           2 :           partition = new_partition ("");
     773                 :           2 :           last_visited_node = 0;
     774                 :           2 :           cost = 0;
     775                 :             : 
     776                 :           2 :           if (dump_file)
     777                 :           0 :             fprintf (dump_file, "New partition\n");
     778                 :           2 :           best_n_nodes = 0;
     779                 :           2 :           best_cost = -1;
     780                 :             : 
     781                 :             :           /* Since the size of partitions is just approximate, update the size after
     782                 :             :              we finished current one.  */
     783                 :           2 :           if (npartitions < n_lto_partitions)
     784                 :           2 :             partition_size = total_size / (n_lto_partitions - npartitions);
     785                 :             :           else
     786                 :             :             /* Watch for overflow.  */
     787                 :             :             partition_size = INT_MAX / 16;
     788                 :             : 
     789                 :           2 :           if (dump_file)
     790                 :           0 :             fprintf (dump_file,
     791                 :             :                      "Total size: %" PRId64 " partition_size: %" PRId64 "\n",
     792                 :             :                      total_size, partition_size);
     793                 :           2 :           if (partition_size < param_min_partition_size)
     794                 :             :             partition_size = param_min_partition_size;
     795                 :           2 :           npartitions ++;
     796                 :             :         }
     797                 :             :     }
     798                 :             : 
     799                 :        7661 :   next_nodes.truncate (0);
     800                 :             : 
     801                 :             :   /* Varables that are not reachable from the code go into last partition.  */
     802                 :       61444 :   FOR_EACH_VARIABLE (vnode)
     803                 :       23061 :     if (vnode->get_partitioning_class () == SYMBOL_PARTITION
     804                 :       23061 :         && !symbol_partitioned_p (vnode))
     805                 :        1534 :       next_nodes.safe_push (vnode);
     806                 :             : 
     807                 :             :   /* Output remaining ordered symbols.  */
     808                 :        9137 :   while (varpool_pos < n_varpool_nodes)
     809                 :        1476 :     next_nodes.safe_push (varpool_order[varpool_pos++]);
     810                 :       25335 :   while (noreorder_pos < (int)noreorder.length ())
     811                 :        7639 :     next_nodes.safe_push (noreorder[noreorder_pos++]);
     812                 :             :   /* For one partition the cost of boundary should be 0 unless we added final
     813                 :             :      symbols here (these are not accounted) or we have accounting bug.  */
     814                 :        7661 :   gcc_assert (next_nodes.length () || npartitions != 1 || !best_cost || best_cost == -1);
     815                 :        7661 :   add_sorted_nodes (next_nodes, partition);
     816                 :             : 
     817                 :        7661 :   if (dump_file)
     818                 :             :     {
     819                 :           0 :       fprintf (dump_file, "\nPartition sizes:\n");
     820                 :           0 :       unsigned partitions = ltrans_partitions.length ();
     821                 :             : 
     822                 :           0 :       for (unsigned i = 0; i < partitions ; i++)
     823                 :             :         {
     824                 :           0 :           ltrans_partition p = ltrans_partitions[i];
     825                 :           0 :           fprintf (dump_file, "partition %d contains %d (%2.2f%%)"
     826                 :             :                    " symbols and %d (%2.2f%%) insns\n", i, p->symbols,
     827                 :           0 :                    100.0 * p->symbols / order.length (), p->insns,
     828                 :           0 :                    100.0 * p->insns / original_total_size);
     829                 :             :         }
     830                 :             : 
     831                 :           0 :       fprintf (dump_file, "\n");
     832                 :             :     }
     833                 :        7661 : }
     834                 :             : 
     835                 :             : /* Return true if we must not change the name of the NODE.  The name as
     836                 :             :    extracted from the corresponding decl should be passed in NAME.  */
     837                 :             : 
     838                 :             : static bool
     839                 :      160825 : must_not_rename (symtab_node *node, const char *name)
     840                 :             : {
     841                 :             :   /* Our renaming machinery do not handle more than one change of assembler name.
     842                 :             :      We should not need more than one anyway.  */
     843                 :      160825 :   if (node->lto_file_data
     844                 :      160825 :       && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
     845                 :             :     {
     846                 :         221 :       if (dump_file)
     847                 :           0 :         fprintf (dump_file,
     848                 :             :                  "Not privatizing symbol name: %s. It privatized already.\n",
     849                 :             :                  name);
     850                 :         221 :       return true;
     851                 :             :     }
     852                 :             :   /* Avoid mangling of already mangled clones. 
     853                 :             :      ???  should have a flag whether a symbol has a 'private' name already,
     854                 :             :      since we produce some symbols like that i.e. for global constructors
     855                 :             :      that are not really clones.
     856                 :             :      ???  it is what unique_name means.  We only need to set it when doing
     857                 :             :      private symbols.  */
     858                 :      160604 :   if (node->unique_name)
     859                 :             :     {
     860                 :       40448 :       if (dump_file)
     861                 :           0 :         fprintf (dump_file,
     862                 :             :                  "Not privatizing symbol name: %s. Has unique name.\n",
     863                 :             :                  name);
     864                 :       40448 :       return true;
     865                 :             :     }
     866                 :             :   return false;
     867                 :             : }
     868                 :             : 
     869                 :             : /* If we are an offload compiler, we may have to rewrite symbols to be
     870                 :             :    valid on this target.  Return either PTR or a modified version of it.  */
     871                 :             : 
     872                 :             : static const char *
     873                 :           0 : maybe_rewrite_identifier (const char *ptr)
     874                 :             : {
     875                 :             : #if defined ACCEL_COMPILER && (defined NO_DOT_IN_LABEL || defined NO_DOLLAR_IN_LABEL)
     876                 :             : #ifndef NO_DOT_IN_LABEL
     877                 :             :   char valid = '.';
     878                 :             :   const char reject[] = "$";
     879                 :             : #elif !defined NO_DOLLAR_IN_LABEL
     880                 :             :   char valid = '$';
     881                 :             :   const char reject[] = ".";
     882                 :             : #else
     883                 :             :   char valid = '_';
     884                 :             :   const char reject[] = ".$";
     885                 :             : #endif
     886                 :             : 
     887                 :             :   char *copy = NULL;
     888                 :             :   const char *match = ptr;
     889                 :             :   for (;;)
     890                 :             :     {
     891                 :             :       size_t off = strcspn (match, reject);
     892                 :             :       if (match[off] == '\0')
     893                 :             :         break;
     894                 :             :       if (copy == NULL)
     895                 :             :         {
     896                 :             :           copy = xstrdup (ptr);
     897                 :             :           match = copy;
     898                 :             :         }
     899                 :             :       copy[off] = valid;
     900                 :             :     }
     901                 :             :   if (copy)
     902                 :             :     {
     903                 :             :       match = IDENTIFIER_POINTER (get_identifier (copy));
     904                 :             :       free (copy);
     905                 :             :     }
     906                 :             :   return match;
     907                 :             : #else
     908                 :           0 :   return ptr;
     909                 :             : #endif
     910                 :             : }
     911                 :             : 
     912                 :             : /* Ensure that the symbol in NODE is valid for the target, and if not,
     913                 :             :    rewrite it.  */
     914                 :             : 
     915                 :             : static void
     916                 :      160441 : validize_symbol_for_target (symtab_node *node)
     917                 :             : {
     918                 :      160441 :   tree decl = node->decl;
     919                 :      160441 :   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
     920                 :             : 
     921                 :      160441 :   if (must_not_rename (node, name))
     922                 :             :     return;
     923                 :             : 
     924                 :             :   const char *name2 = maybe_rewrite_identifier (name);
     925                 :             :   if (name2 != name)
     926                 :             :     {
     927                 :             :       symtab->change_decl_assembler_name (decl, get_identifier (name2));
     928                 :             :       if (node->lto_file_data)
     929                 :             :         lto_record_renamed_decl (node->lto_file_data, name, name2);
     930                 :             :     }
     931                 :             : }
     932                 :             : 
     933                 :             : /* Maps symbol names to unique lto clone counters.  */
     934                 :             : static hash_map<const char *, unsigned> *lto_clone_numbers;
     935                 :             : 
     936                 :             : /* Helper for privatize_symbol_name.  Mangle NODE symbol name
     937                 :             :    represented by DECL.  */
     938                 :             : 
     939                 :             : static bool
     940                 :         384 : privatize_symbol_name_1 (symtab_node *node, tree decl)
     941                 :             : {
     942                 :         384 :   const char *name0 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
     943                 :             : 
     944                 :         384 :   if (must_not_rename (node, name0))
     945                 :             :     return false;
     946                 :             : 
     947                 :         334 :   const char *name = maybe_rewrite_identifier (name0);
     948                 :         334 :   unsigned &clone_number = lto_clone_numbers->get_or_insert (name);
     949                 :         334 :   symtab->change_decl_assembler_name (decl,
     950                 :             :                                       clone_function_name (
     951                 :         334 :                                           name, "lto_priv", clone_number));
     952                 :         334 :   clone_number++;
     953                 :             : 
     954                 :         334 :   if (node->lto_file_data)
     955                 :         640 :     lto_record_renamed_decl (node->lto_file_data, name0,
     956                 :         320 :                              IDENTIFIER_POINTER
     957                 :             :                              (DECL_ASSEMBLER_NAME (decl)));
     958                 :             : 
     959                 :         334 :   if (dump_file)
     960                 :           0 :     fprintf (dump_file,
     961                 :             :              "Privatizing symbol name: %s -> %s\n",
     962                 :           0 :              name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
     963                 :             : 
     964                 :             :   return true;
     965                 :             : }
     966                 :             : 
     967                 :             : /* Mangle NODE symbol name into a local name.
     968                 :             :    This is necessary to do
     969                 :             :    1) if two or more static vars of same assembler name
     970                 :             :       are merged into single ltrans unit.
     971                 :             :    2) if previously static var was promoted hidden to avoid possible conflict
     972                 :             :       with symbols defined out of the LTO world.  */
     973                 :             : 
     974                 :             : static bool
     975                 :         384 : privatize_symbol_name (symtab_node *node)
     976                 :             : {
     977                 :           0 :   if (!privatize_symbol_name_1 (node, node->decl))
     978                 :             :     return false;
     979                 :             : 
     980                 :             :   return true;
     981                 :             : }
     982                 :             : 
     983                 :             : /* Promote variable VNODE to be static.  */
     984                 :             : 
     985                 :             : static void
     986                 :         219 : promote_symbol (symtab_node *node)
     987                 :             : {
     988                 :             :   /* We already promoted ... */
     989                 :         219 :   if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
     990                 :           7 :       && DECL_VISIBILITY_SPECIFIED (node->decl)
     991                 :         226 :       && TREE_PUBLIC (node->decl))
     992                 :             :     {
     993                 :           7 :       validize_symbol_for_target (node);
     994                 :           7 :       return;
     995                 :             :     }
     996                 :             : 
     997                 :         212 :   gcc_checking_assert (!TREE_PUBLIC (node->decl)
     998                 :             :                        && !DECL_EXTERNAL (node->decl));
     999                 :             :   /* Be sure that newly public symbol does not conflict with anything already
    1000                 :             :      defined by the non-LTO part.  */
    1001                 :         212 :   privatize_symbol_name (node);
    1002                 :         212 :   TREE_PUBLIC (node->decl) = 1;
    1003                 :             :   /* After privatization the node should not conflict with any other symbol,
    1004                 :             :      so it is prevailing.  This is important to keep binds_to_current_def_p
    1005                 :             :      to work across partitions.  */
    1006                 :         212 :   node->resolution = LDPR_PREVAILING_DEF_IRONLY;
    1007                 :         212 :   node->semantic_interposition = false;
    1008                 :         212 :   DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
    1009                 :         212 :   DECL_VISIBILITY_SPECIFIED (node->decl) = true;
    1010                 :         212 :   if (dump_file)
    1011                 :           0 :     fprintf (dump_file,
    1012                 :             :              "Promoting as hidden: %s (%s)\n", node->dump_name (),
    1013                 :           0 :              IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
    1014                 :             : 
    1015                 :             :   /* Promoting a symbol also promotes all transparent aliases with exception
    1016                 :             :      of weakref where the visibility flags are always wrong and set to 
    1017                 :             :      !PUBLIC.  */
    1018                 :             :   ipa_ref *ref;
    1019                 :         218 :   for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
    1020                 :             :     {
    1021                 :           6 :       struct symtab_node *alias = ref->referring;
    1022                 :           6 :       if (alias->transparent_alias && !alias->weakref)
    1023                 :             :         {
    1024                 :           0 :           TREE_PUBLIC (alias->decl) = 1;
    1025                 :           0 :           DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
    1026                 :           0 :           DECL_VISIBILITY_SPECIFIED (alias->decl) = true;
    1027                 :           0 :           if (dump_file)
    1028                 :           0 :             fprintf (dump_file,
    1029                 :             :                      "Promoting alias as hidden: %s\n",
    1030                 :           0 :                      IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
    1031                 :             :         }
    1032                 :           6 :       gcc_assert (!alias->weakref || TREE_PUBLIC (alias->decl));
    1033                 :             :     }
    1034                 :             : }
    1035                 :             : 
    1036                 :             : /* Return true if NODE needs named section even if it won't land in
    1037                 :             :    the partition symbol table.
    1038                 :             : 
    1039                 :             :    FIXME: we should really not use named sections for master clones.  */
    1040                 :             : 
    1041                 :             : static bool
    1042                 :      111427 : may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
    1043                 :             : {
    1044                 :      220295 :   struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
    1045                 :             :   /* We do not need to handle variables since we never clone them.  */
    1046                 :      103483 :   if (!cnode)
    1047                 :             :     return false;
    1048                 :             :   /* Only master clones will have bodies streamed.  */
    1049                 :      103483 :   if (cnode->clone_of)
    1050                 :             :     return false;
    1051                 :       64140 :   if (node->real_symbol_p ())
    1052                 :             :     return false;
    1053                 :        2559 :   return (!encoder
    1054                 :        2559 :           || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
    1055                 :        2547 :               && lto_symtab_encoder_encode_body_p (encoder,
    1056                 :             :                                                    cnode)));
    1057                 :             : }
    1058                 :             : 
    1059                 :             : /* If NODE represents a static variable.  See if there are other variables
    1060                 :             :    of the same name in partition ENCODER (or in whole compilation unit if
    1061                 :             :    ENCODER is NULL) and if so, mangle the statics.  Always mangle all
    1062                 :             :    conflicting statics, so we reduce changes of silently miscompiling
    1063                 :             :    asm statements referring to them by symbol name.  */
    1064                 :             : 
    1065                 :             : static void
    1066                 :      160653 : rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
    1067                 :             : {
    1068                 :      160653 :   tree decl = node->decl;
    1069                 :      160653 :   symtab_node *s;
    1070                 :      160653 :   tree name = DECL_ASSEMBLER_NAME (decl);
    1071                 :             : 
    1072                 :             :   /* See if this is static symbol. */
    1073                 :      160653 :   if (((node->externally_visible && !node->weakref)
    1074                 :             :       /* FIXME: externally_visible is somewhat illogically not set for
    1075                 :             :          external symbols (i.e. those not defined).  Remove this test
    1076                 :             :          once this is fixed.  */
    1077                 :      112745 :         || DECL_EXTERNAL (node->decl)
    1078                 :       91083 :         || !node->real_symbol_p ())
    1079                 :      203903 :        && !may_need_named_section_p (encoder, node))
    1080                 :             :     return;
    1081                 :             : 
    1082                 :             :   /* Now walk symbols sharing the same name and see if there are any conflicts.
    1083                 :             :      (all types of symbols counts here, since we cannot have static of the
    1084                 :             :      same name as external or public symbol.)  */
    1085                 :       70777 :   for (s = symtab_node::get_for_asmname (name);
    1086                 :      160504 :        s; s = s->next_sharing_asm_name)
    1087                 :      110083 :     if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
    1088                 :       70830 :         && s->decl != node->decl
    1089                 :       89936 :         && (!encoder
    1090                 :          72 :             || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
    1091                 :             :        break;
    1092                 :             : 
    1093                 :             :   /* OK, no confict, so we have nothing to do.  */
    1094                 :       70777 :   if (!s)
    1095                 :             :     return;
    1096                 :             : 
    1097                 :          93 :   if (dump_file)
    1098                 :           0 :     fprintf (dump_file,
    1099                 :             :             "Renaming statics with asm name: %s\n", node->dump_name ());
    1100                 :             : 
    1101                 :             :   /* Assign every symbol in the set that shares the same ASM name an unique
    1102                 :             :      mangled name.  */
    1103                 :         292 :   for (s = symtab_node::get_for_asmname (name); s;)
    1104                 :         199 :     if ((!s->externally_visible || s->weakref)
    1105                 :             :         /* Transparent aliases having same name as target are renamed at a
    1106                 :             :            time their target gets new name.  Transparent aliases that use
    1107                 :             :            separate assembler name require the name to be unique.  */
    1108                 :         180 :         && (!s->transparent_alias || !s->definition || s->weakref
    1109                 :           6 :             || !symbol_table::assembler_names_equal_p
    1110                 :           6 :                  (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (s->decl)),
    1111                 :           6 :                   IDENTIFIER_POINTER
    1112                 :             :                     (DECL_ASSEMBLER_NAME (s->get_alias_target()->decl))))
    1113                 :         174 :         && ((s->real_symbol_p ()
    1114                 :         170 :              && !DECL_EXTERNAL (s->decl)
    1115                 :         168 :              && !TREE_PUBLIC (s->decl))
    1116                 :           6 :             || may_need_named_section_p (encoder, s))
    1117                 :         371 :         && (!encoder
    1118                 :          88 :             || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
    1119                 :             :       {
    1120                 :         172 :         if (privatize_symbol_name (s))
    1121                 :             :           /* Re-start from beginning since we do not know how many
    1122                 :             :              symbols changed a name.  */
    1123                 :         167 :           s = symtab_node::get_for_asmname (name);
    1124                 :           5 :         else s = s->next_sharing_asm_name;
    1125                 :             :       }
    1126                 :          27 :     else s = s->next_sharing_asm_name;
    1127                 :             : }
    1128                 :             : 
    1129                 :             : /* Find out all static decls that need to be promoted to global because
    1130                 :             :    of cross file sharing.  This function must be run in the WPA mode after
    1131                 :             :    all inlinees are added.  */
    1132                 :             : 
    1133                 :             : void
    1134                 :        7967 : lto_promote_cross_file_statics (void)
    1135                 :             : {
    1136                 :        7967 :   unsigned i, n_sets;
    1137                 :             : 
    1138                 :        7967 :   gcc_assert (flag_wpa);
    1139                 :             : 
    1140                 :        7967 :   lto_stream_offload_p = false;
    1141                 :        7967 :   select_what_to_stream ();
    1142                 :             : 
    1143                 :             :   /* First compute boundaries.  */
    1144                 :        7967 :   n_sets = ltrans_partitions.length ();
    1145                 :       16291 :   for (i = 0; i < n_sets; i++)
    1146                 :             :     {
    1147                 :        8324 :       ltrans_partition part
    1148                 :        8324 :         = ltrans_partitions[i];
    1149                 :        8324 :       part->encoder = compute_ltrans_boundary (part->encoder);
    1150                 :             :     }
    1151                 :             : 
    1152                 :        7967 :   lto_clone_numbers = new hash_map<const char *, unsigned>;
    1153                 :             : 
    1154                 :             :   /* Look at boundaries and promote symbols as needed.  */
    1155                 :       16291 :   for (i = 0; i < n_sets; i++)
    1156                 :             :     {
    1157                 :        8324 :       lto_symtab_encoder_iterator lsei;
    1158                 :        8324 :       lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
    1159                 :             : 
    1160                 :      223171 :       for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
    1161                 :      103266 :            lsei_next (&lsei))
    1162                 :             :         {
    1163                 :      103266 :           symtab_node *node = lsei_node (lsei);
    1164                 :             : 
    1165                 :             :           /* If symbol is static, rename it if its assembler name
    1166                 :             :              clashes with anything else in this unit.  */
    1167                 :      103266 :           rename_statics (encoder, node);
    1168                 :             : 
    1169                 :             :           /* No need to promote if symbol already is externally visible ... */
    1170                 :      206313 :           if (node->externally_visible
    1171                 :             :               /* ... or if it is part of current partition ... */
    1172                 :       92232 :               || lto_symtab_encoder_in_partition_p (encoder, node)
    1173                 :             :               /* ... or if we do not partition it. This mean that it will
    1174                 :             :                  appear in every partition referencing it.  */
    1175                 :      120857 :               || node->get_partitioning_class () != SYMBOL_PARTITION)
    1176                 :             :             {
    1177                 :      103047 :               validize_symbol_for_target (node);
    1178                 :      103047 :               continue;
    1179                 :             :             }
    1180                 :             : 
    1181                 :         219 :           promote_symbol (node);
    1182                 :             :         }
    1183                 :             :     }
    1184                 :       15934 :   delete lto_clone_numbers;
    1185                 :        7967 : }
    1186                 :             : 
    1187                 :             : /* Rename statics in the whole unit in the case that 
    1188                 :             :    we do -flto-partition=none.  */
    1189                 :             : 
    1190                 :             : void
    1191                 :        4083 : lto_promote_statics_nonwpa (void)
    1192                 :             : {
    1193                 :        4083 :   symtab_node *node;
    1194                 :             : 
    1195                 :        4083 :   lto_clone_numbers = new hash_map<const char *, unsigned>;
    1196                 :       61470 :   FOR_EACH_SYMBOL (node)
    1197                 :             :     {
    1198                 :       57387 :       rename_statics (NULL, node);
    1199                 :       57387 :       validize_symbol_for_target (node);
    1200                 :             :     }
    1201                 :        8166 :   delete lto_clone_numbers;
    1202                 :        4083 : }
        

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.