LCOV - code coverage report
Current view: top level - gcc - ipa-reference.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.0 % 609 554
Test Date: 2024-03-23 14:05:01 Functions: 90.9 % 33 30
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Callgraph based analysis of static variables.
       2                 :             :    Copyright (C) 2004-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       7                 :             : GCC is free software; you can redistribute it and/or modify it under
       8                 :             : the terms of the GNU General Public License as published by the Free
       9                 :             : Software Foundation; either version 3, or (at your option) any later
      10                 :             : version.
      11                 :             : 
      12                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15                 :             : for more details.
      16                 :             : 
      17                 :             : You should have received a copy of the GNU General Public License
      18                 :             : along with GCC; see the file COPYING3.  If not see
      19                 :             : <http://www.gnu.org/licenses/>.  */
      20                 :             : 
      21                 :             : /* This file gathers information about how variables whose scope is
      22                 :             :    confined to the compilation unit are used.
      23                 :             : 
      24                 :             :    The transitive call site specific clobber effects are computed
      25                 :             :    for the variables whose scope is contained within this compilation
      26                 :             :    unit.
      27                 :             : 
      28                 :             :    First each function and static variable initialization is analyzed
      29                 :             :    to determine which local static variables are either read, written,
      30                 :             :    or have their address taken.  Any local static that has its address
      31                 :             :    taken is removed from consideration.  Once the local read and
      32                 :             :    writes are determined, a transitive closure of this information is
      33                 :             :    performed over the call graph to determine the worst case set of
      34                 :             :    side effects of each call.  In later parts of the compiler, these
      35                 :             :    local and global sets are examined to make the call clobbering less
      36                 :             :    traumatic, promote some statics to registers, and improve aliasing
      37                 :             :    information.  */
      38                 :             : 
      39                 :             : #include "config.h"
      40                 :             : #include "system.h"
      41                 :             : #include "coretypes.h"
      42                 :             : #include "backend.h"
      43                 :             : #include "tree.h"
      44                 :             : #include "gimple.h"
      45                 :             : #include "tree-pass.h"
      46                 :             : #include "cgraph.h"
      47                 :             : #include "data-streamer.h"
      48                 :             : #include "calls.h"
      49                 :             : #include "ipa-utils.h"
      50                 :             : #include "ipa-reference.h"
      51                 :             : #include "alloc-pool.h"
      52                 :             : #include "symbol-summary.h"
      53                 :             : 
      54                 :             : /* The static variables defined within the compilation unit that are
      55                 :             :    loaded or stored directly by function that owns this structure.  */
      56                 :             : 
      57                 :             : struct ipa_reference_local_vars_info_d
      58                 :             : {
      59                 :             :   bitmap statics_read;
      60                 :             :   bitmap statics_written;
      61                 :             : };
      62                 :             : 
      63                 :             : /* Statics that are read and written by some set of functions. The
      64                 :             :    local ones are based on the loads and stores local to the function.
      65                 :             :    The global ones are based on the local info as well as the
      66                 :             :    transitive closure of the functions that are called. */
      67                 :             : 
      68                 :             : struct ipa_reference_global_vars_info_d
      69                 :             : {
      70                 :             :   bitmap statics_read;
      71                 :             :   bitmap statics_written;
      72                 :             : };
      73                 :             : 
      74                 :             : /* Information we save about every function after ipa-reference is completed.  */
      75                 :             : 
      76                 :             : struct ipa_reference_optimization_summary_d
      77                 :             : {
      78                 :             :   bitmap statics_read;
      79                 :             :   bitmap statics_written;
      80                 :             : };
      81                 :             : 
      82                 :             : typedef ipa_reference_local_vars_info_d *ipa_reference_local_vars_info_t;
      83                 :             : typedef ipa_reference_global_vars_info_d *ipa_reference_global_vars_info_t;
      84                 :             : typedef ipa_reference_optimization_summary_d *
      85                 :             :   ipa_reference_optimization_summary_t;
      86                 :             : 
      87                 :             : struct ipa_reference_vars_info_d
      88                 :             : {
      89                 :             :   struct ipa_reference_local_vars_info_d local;
      90                 :             :   struct ipa_reference_global_vars_info_d global;
      91                 :             : };
      92                 :             : 
      93                 :             : typedef struct ipa_reference_vars_info_d *ipa_reference_vars_info_t;
      94                 :             : 
      95                 :             : /* This map contains all of the static variables that are
      96                 :             :    being considered by the compilation level alias analysis.  */
      97                 :             : typedef hash_map<tree, int> reference_vars_map_t;
      98                 :             : static reference_vars_map_t *ipa_reference_vars_map;
      99                 :             : static int ipa_reference_vars_uids;
     100                 :             : static vec<tree> *reference_vars_to_consider;
     101                 :             : varpool_node_hook_list *varpool_node_hooks;
     102                 :             : 
     103                 :             : /* Set of all interesting module statics.  A bit is set for every module
     104                 :             :    static we are considering.  This is added to the local info when asm
     105                 :             :    code is found that clobbers all memory.  */
     106                 :             : static bitmap all_module_statics;
     107                 :             : /* Zero bitmap.  */
     108                 :             : static bitmap no_module_statics;
     109                 :             : /* Set of all statics that should be ignored because they are touched by
     110                 :             :    -fno-ipa-reference code.  */
     111                 :             : static bitmap ignore_module_statics;
     112                 :             : 
     113                 :             : /* Obstack holding bitmaps of local analysis (live from analysis to
     114                 :             :    propagation)  */
     115                 :             : static bitmap_obstack local_info_obstack;
     116                 :             : /* Obstack holding global analysis live forever.  */
     117                 :             : static bitmap_obstack optimization_summary_obstack;
     118                 :             : 
     119                 :             : class ipa_ref_var_info_summary_t: public fast_function_summary
     120                 :             :                           <ipa_reference_vars_info_d *, va_heap>
     121                 :             : {
     122                 :             : public:
     123                 :      145904 :   ipa_ref_var_info_summary_t (symbol_table *symtab):
     124                 :      291808 :     fast_function_summary <ipa_reference_vars_info_d *, va_heap> (symtab) {}
     125                 :             : };
     126                 :             : 
     127                 :             : static ipa_ref_var_info_summary_t *ipa_ref_var_info_summaries = NULL;
     128                 :             : 
     129                 :             : class ipa_ref_opt_summary_t: public fast_function_summary
     130                 :             :                              <ipa_reference_optimization_summary_d *, va_heap>
     131                 :             : {
     132                 :             : public:
     133                 :      155058 :   ipa_ref_opt_summary_t (symbol_table *symtab):
     134                 :      310116 :     fast_function_summary <ipa_reference_optimization_summary_d *, va_heap> (symtab) {}
     135                 :             : 
     136                 :             :   void remove (cgraph_node *src_node,
     137                 :             :                ipa_reference_optimization_summary_d *data) final override;
     138                 :             :   void duplicate (cgraph_node *src_node, cgraph_node *dst_node,
     139                 :             :                   ipa_reference_optimization_summary_d *src_data,
     140                 :             :                   ipa_reference_optimization_summary_d *dst_data) final override;
     141                 :             : };
     142                 :             : 
     143                 :             : static ipa_ref_opt_summary_t *ipa_ref_opt_sum_summaries = NULL;
     144                 :             : 
     145                 :             : /* Return ID used by ipa-reference bitmaps.  -1 if failed.  */
     146                 :             : int
     147                 :    12949153 : ipa_reference_var_uid (tree t)
     148                 :             : {
     149                 :    12949153 :   if (!ipa_reference_vars_map)
     150                 :             :     return -1;
     151                 :     9901229 :   int *id = ipa_reference_vars_map->get
     152                 :     9901229 :     (symtab_node::get (t)->ultimate_alias_target (NULL)->decl);
     153                 :     9901229 :   if (!id)
     154                 :             :     return -1;
     155                 :     3750598 :   return *id;
     156                 :             : }
     157                 :             : 
     158                 :             : /* Return ID used by ipa-reference bitmaps.  Create new entry if
     159                 :             :    T is not in map.  Set EXISTED accordinly  */
     160                 :             : int
     161                 :      319075 : ipa_reference_var_get_or_insert_uid (tree t, bool *existed)
     162                 :             : {
     163                 :      319075 :   int &id = ipa_reference_vars_map->get_or_insert
     164                 :      319075 :     (symtab_node::get (t)->ultimate_alias_target (NULL)->decl, existed);
     165                 :      319075 :   if (!*existed)
     166                 :       51894 :     id = ipa_reference_vars_uids++;
     167                 :      319075 :   return id;
     168                 :             : }
     169                 :             : 
     170                 :             : /* Return the ipa_reference_vars structure starting from the cgraph NODE.  */
     171                 :             : static inline ipa_reference_vars_info_t
     172                 :     6292152 : get_reference_vars_info (struct cgraph_node *node)
     173                 :             : {
     174                 :     6292152 :   if (ipa_ref_var_info_summaries == NULL)
     175                 :             :     return NULL;
     176                 :             : 
     177                 :     6292152 :   ipa_reference_vars_info_t v = ipa_ref_var_info_summaries->get (node);
     178                 :     6292152 :   return v == NULL ? NULL : v;
     179                 :             : }
     180                 :             : 
     181                 :             : /* Return the ipa_reference_vars structure starting from the cgraph NODE.  */
     182                 :             : static inline ipa_reference_optimization_summary_t
     183                 :     3526453 : get_reference_optimization_summary (struct cgraph_node *node)
     184                 :             : {
     185                 :     3526453 :   if (ipa_ref_opt_sum_summaries == NULL)
     186                 :             :     return NULL;
     187                 :             : 
     188                 :     3526453 :   ipa_reference_optimization_summary_t v
     189                 :     3526453 :     = ipa_ref_opt_sum_summaries->get (node);
     190                 :             : 
     191                 :     3526453 :   return v == NULL ? NULL : v;
     192                 :             : }
     193                 :             : 
     194                 :             : /* Return a bitmap indexed by ipa_reference_var_uid for the static variables
     195                 :             :    that are *not* read during the execution of the function FN.  Returns
     196                 :             :    NULL if no data is available.  */
     197                 :             : 
     198                 :             : bitmap
     199                 :       95935 : ipa_reference_get_read_global (struct cgraph_node *fn)
     200                 :             : {
     201                 :       95935 :   if (!opt_for_fn (current_function_decl, flag_ipa_reference))
     202                 :             :     return NULL;
     203                 :             : 
     204                 :       95935 :   enum availability avail;
     205                 :       95935 :   struct cgraph_node *fn2 = fn->function_symbol (&avail);
     206                 :       95935 :   ipa_reference_optimization_summary_t info =
     207                 :       95935 :     get_reference_optimization_summary (fn2);
     208                 :             : 
     209                 :       95935 :   if (info
     210                 :        3572 :       && (avail >= AVAIL_AVAILABLE
     211                 :           0 :           || (avail == AVAIL_INTERPOSABLE
     212                 :           0 :               && flags_from_decl_or_type (fn->decl) & ECF_LEAF))
     213                 :       99507 :       && opt_for_fn (fn2->decl, flag_ipa_reference))
     214                 :        3572 :     return info->statics_read;
     215                 :       92363 :   else if (avail == AVAIL_NOT_AVAILABLE
     216                 :       92363 :            && flags_from_decl_or_type (fn->decl) & ECF_LEAF)
     217                 :        5537 :     return no_module_statics;
     218                 :             :   else
     219                 :       86826 :     return NULL;
     220                 :             : }
     221                 :             : 
     222                 :             : /* Return a bitmap indexed by ipa_reference_var_uid for the static variables
     223                 :             :    that are *not* written during the execution of the function FN.  Note
     224                 :             :    that variables written may or may not be read during the function
     225                 :             :    call.  Returns NULL if no data is available.  */
     226                 :             : 
     227                 :             : bitmap
     228                 :     3382559 : ipa_reference_get_written_global (struct cgraph_node *fn)
     229                 :             : {
     230                 :     3382559 :   if (!opt_for_fn (current_function_decl, flag_ipa_reference))
     231                 :             :     return NULL;
     232                 :             : 
     233                 :     3382559 :   enum availability avail;
     234                 :     3382559 :   struct cgraph_node *fn2 = fn->function_symbol (&avail);
     235                 :     3382559 :   ipa_reference_optimization_summary_t info =
     236                 :     3382559 :     get_reference_optimization_summary (fn2);
     237                 :             : 
     238                 :     3382559 :   if (info
     239                 :       54195 :       && (avail >= AVAIL_AVAILABLE
     240                 :           0 :           || (avail == AVAIL_INTERPOSABLE
     241                 :           0 :               && flags_from_decl_or_type (fn->decl) & ECF_LEAF))
     242                 :     3436754 :       && opt_for_fn (fn2->decl, flag_ipa_reference))
     243                 :       54195 :     return info->statics_written;
     244                 :     3328364 :   else if (avail == AVAIL_NOT_AVAILABLE
     245                 :     3328364 :            && flags_from_decl_or_type (fn->decl) & ECF_LEAF)
     246                 :       16213 :     return no_module_statics;
     247                 :             :   else
     248                 :     3312151 :     return NULL;
     249                 :             : }
     250                 :             : 
     251                 :             : 
     252                 :             : /* Hepler for is_proper_for_analysis.  */
     253                 :             : static bool
     254                 :     3407858 : is_improper (symtab_node *n, void *v ATTRIBUTE_UNUSED)
     255                 :             : {
     256                 :     3407858 :   tree t = n->decl;
     257                 :             :   /* If the variable has the "used" attribute, treat it as if it had a
     258                 :             :      been touched by the devil.  */
     259                 :     3407858 :   if (DECL_PRESERVE_P (t))
     260                 :             :     return true;
     261                 :             : 
     262                 :             :   /* Do not want to do anything with volatile except mark any
     263                 :             :      function that uses one to be not const or pure.  */
     264                 :     3407179 :   if (TREE_THIS_VOLATILE (t))
     265                 :             :     return true;
     266                 :             : 
     267                 :             :   /* We do not need to analyze readonly vars, we already know they do not
     268                 :             :      alias.  */
     269                 :     3089970 :   if (TREE_READONLY (t))
     270                 :             :     return true;
     271                 :             : 
     272                 :             :   /* We cannot track variables with address taken.  */
     273                 :     2200342 :   if (TREE_ADDRESSABLE (t))
     274                 :             :     return true;
     275                 :             : 
     276                 :             :   /* TODO: We could track public variables that are not addressable, but
     277                 :             :      currently frontends don't give us those.  */
     278                 :      769008 :   if (TREE_PUBLIC (t))
     279                 :      451585 :     return true;
     280                 :             : 
     281                 :             :   return false;
     282                 :             : }
     283                 :             : 
     284                 :             : /* Return true if the variable T is the right kind of static variable to
     285                 :             :    perform compilation unit scope escape analysis.  */
     286                 :             : 
     287                 :             : static inline bool
     288                 :     3407843 : is_proper_for_analysis (tree t)
     289                 :             : {
     290                 :     3407843 :   int id = ipa_reference_var_uid (t);
     291                 :             : 
     292                 :     3407843 :   if (id != -1 && bitmap_bit_p (ignore_module_statics, id))
     293                 :             :     return false;
     294                 :             : 
     295                 :     3407843 :   if (symtab_node::get (t)
     296                 :     3407843 :         ->call_for_symbol_and_aliases (is_improper, NULL, true))
     297                 :             :     return false;
     298                 :             : 
     299                 :             :   return true;
     300                 :             : }
     301                 :             : 
     302                 :             : /* Lookup the tree node for the static variable that has UID and
     303                 :             :    convert the name to a string for debugging.  */
     304                 :             : 
     305                 :             : static const char *
     306                 :           0 : get_static_name (int index)
     307                 :             : {
     308                 :           0 :   return fndecl_name ((*reference_vars_to_consider)[index]);
     309                 :             : }
     310                 :             : 
     311                 :             : /* Dump a set of static vars to FILE.  */
     312                 :             : static void
     313                 :         136 : dump_static_vars_set_to_file (FILE *f, bitmap set)
     314                 :             : {
     315                 :         136 :   unsigned int index;
     316                 :         136 :   bitmap_iterator bi;
     317                 :         136 :   if (set == NULL)
     318                 :           0 :     return;
     319                 :         136 :   else if (set == all_module_statics)
     320                 :          36 :     fprintf (f, "ALL");
     321                 :         100 :   else if (set == no_module_statics)
     322                 :           0 :     fprintf (f, "NO");
     323                 :             :   else
     324                 :         100 :     EXECUTE_IF_SET_IN_BITMAP (set, 0, index, bi)
     325                 :             :       {
     326                 :           0 :         fprintf (f, "%s ", get_static_name (index));
     327                 :             :       }
     328                 :             : }
     329                 :             : 
     330                 :             : /* Compute X |= Y, taking into account the possibility that
     331                 :             :    either X or Y is already the maximum set.
     332                 :             :    Return true if X is the maximum set after taking the union with Y.  */
     333                 :             : 
     334                 :             : static bool
     335                 :     2255956 : union_static_var_sets (bitmap &x, bitmap y)
     336                 :             : {
     337                 :     2255956 :   if (x != all_module_statics)
     338                 :             :     {
     339                 :     1687679 :       if (y == all_module_statics)
     340                 :             :         {
     341                 :      627601 :           BITMAP_FREE (x);
     342                 :      627601 :           x = all_module_statics;
     343                 :             :         }
     344                 :     1060078 :       else if (bitmap_ior_into (x, y))
     345                 :             :         {
     346                 :             :           /* The union may have reduced X to the maximum set.
     347                 :             :              In that case, we want to make that visible explicitly.
     348                 :             :              Even though bitmap_equal_p can be very expensive, it
     349                 :             :              turns out to be an overall win to check this here for
     350                 :             :              an LTO bootstrap of GCC itself.  Liberally extrapoliate
     351                 :             :              that result to be applicable to all cases.  */
     352                 :       12757 :           if (bitmap_equal_p (x, all_module_statics))
     353                 :             :             {
     354                 :       11108 :               BITMAP_FREE (x);
     355                 :       11108 :               x = all_module_statics;
     356                 :             :             }
     357                 :             :         }
     358                 :             :     }
     359                 :     2255956 :   return x == all_module_statics;
     360                 :             : }
     361                 :             : 
     362                 :             : /* Return a copy of SET on the bitmap obstack containing SET.
     363                 :             :    But if SET is NULL or the maximum set, return that instead.  */
     364                 :             : 
     365                 :             : static bitmap
     366                 :     2193431 : copy_static_var_set (bitmap set, bool for_propagation)
     367                 :             : {
     368                 :     2193431 :   if (set == NULL || set == all_module_statics)
     369                 :             :     return set;
     370                 :     2193431 :   if (!for_propagation && set == no_module_statics)
     371                 :             :     return set;
     372                 :     2193431 :   bitmap_obstack *o = set->obstack;
     373                 :     2193431 :   gcc_checking_assert (o);
     374                 :     2193431 :   bitmap copy = BITMAP_ALLOC (o);
     375                 :     2193431 :   bitmap_copy (copy, set);
     376                 :     2193431 :   return copy;
     377                 :             : }
     378                 :             : 
     379                 :             : /* Compute the union all of the statics read and written by every callee of X
     380                 :             :    into X_GLOBAL->statics_read and X_GLOBAL->statics_written.  X_GLOBAL is
     381                 :             :    actually the set representing the cycle containing X.  If the read and
     382                 :             :    written sets of X_GLOBAL has been reduced to the maximum set, we don't
     383                 :             :    have to look at the remaining callees.  */
     384                 :             : 
     385                 :             : static void
     386                 :     1127998 : propagate_bits (ipa_reference_global_vars_info_t x_global, struct cgraph_node *x)
     387                 :             : {
     388                 :     1127998 :   struct cgraph_edge *e;
     389                 :     1127998 :   bool read_all = x_global->statics_read == all_module_statics;
     390                 :     1127998 :   bool write_all = x_global->statics_written == all_module_statics;
     391                 :     1127998 :   for (e = x->callees;
     392                 :     3378418 :        e && !(read_all && write_all);
     393                 :     2250420 :        e = e->next_callee)
     394                 :             :     {
     395                 :     2250420 :       enum availability avail;
     396                 :     2250420 :       struct cgraph_node *y = e->callee->function_symbol (&avail);
     397                 :     2250420 :       if (!y)
     398                 :      148817 :         continue;
     399                 :             : 
     400                 :             :       /* Only look into nodes we can propagate something.  */
     401                 :     2250420 :       int flags = flags_from_decl_or_type (y->decl);
     402                 :     2250420 :       if (opt_for_fn (y->decl, flag_ipa_reference)
     403                 :     2250420 :           && (avail > AVAIL_INTERPOSABLE
     404                 :     1030240 :               || (avail == AVAIL_INTERPOSABLE && (flags & ECF_LEAF))))
     405                 :             :         {
     406                 :     1220215 :           if (get_reference_vars_info (y))
     407                 :             :             {
     408                 :     1220215 :               ipa_reference_vars_info_t y_info = get_reference_vars_info (y);
     409                 :     1220215 :               ipa_reference_global_vars_info_t y_global = &y_info->global;
     410                 :             : 
     411                 :             :               /* Calls in the current cycle do not have their global set
     412                 :             :                  computed yet (but everything else does because we're
     413                 :             :                  visiting nodes in topological order).  */
     414                 :     1220215 :               if (!y_global->statics_read)
     415                 :        5868 :                 continue;
     416                 :             : 
     417                 :             :               /* If the function is const, it reads no memory even if it
     418                 :             :                  seems so to local analysis.  */
     419                 :     1214347 :               if (flags & ECF_CONST)
     420                 :       36806 :                 continue;
     421                 :             : 
     422                 :     1177541 :               union_static_var_sets (x_global->statics_read,
     423                 :             :                                      y_global->statics_read);
     424                 :             : 
     425                 :             :               /* If the function is pure, it has no stores even if it
     426                 :             :                  seems so to local analysis.  If we cannot return from
     427                 :             :                  the function, we can safely ignore the call.  */
     428                 :     1283684 :               if ((flags & ECF_PURE)
     429                 :     1177541 :                   || e->cannot_lead_to_return_p ())
     430                 :      106143 :                 continue;
     431                 :             : 
     432                 :     1071398 :               union_static_var_sets (x_global->statics_written,
     433                 :             :                                      y_global->statics_written);
     434                 :             :             }
     435                 :             :           else
     436                 :           0 :             gcc_unreachable ();
     437                 :             :         }
     438                 :             :     }
     439                 :     1127998 : }
     440                 :             : 
     441                 :             : /* Delete NODE from map.  */
     442                 :             : 
     443                 :             : static void
     444                 :       51785 : varpool_removal_hook (varpool_node *node, void *)
     445                 :             : {
     446                 :       51785 :   ipa_reference_vars_map->remove (node->decl);
     447                 :       51785 : }
     448                 :             : 
     449                 :             : static bool ipa_init_p = false;
     450                 :             : 
     451                 :             : /* The init routine for analyzing global static variable usage.  See
     452                 :             :    comments at top for description.  */
     453                 :             : static void
     454                 :      145904 : ipa_init (void)
     455                 :             : {
     456                 :      145904 :   if (ipa_init_p)
     457                 :             :     return;
     458                 :             : 
     459                 :      145904 :   ipa_init_p = true;
     460                 :             : 
     461                 :      145904 :   if (dump_file)
     462                 :          20 :     vec_alloc (reference_vars_to_consider, 10);
     463                 :             : 
     464                 :      145904 :   if (ipa_ref_opt_sum_summaries != NULL)
     465                 :             :     {
     466                 :           0 :       delete ipa_ref_opt_sum_summaries;
     467                 :           0 :       ipa_ref_opt_sum_summaries = NULL;
     468                 :           0 :       delete ipa_reference_vars_map;
     469                 :             :     }
     470                 :      145904 :   ipa_reference_vars_map = new reference_vars_map_t(257);
     471                 :      145904 :   varpool_node_hooks
     472                 :      145904 :          = symtab->add_varpool_removal_hook (varpool_removal_hook, NULL);
     473                 :      145904 :   ipa_reference_vars_uids = 0;
     474                 :             : 
     475                 :      145904 :   bitmap_obstack_initialize (&local_info_obstack);
     476                 :      145904 :   bitmap_obstack_initialize (&optimization_summary_obstack);
     477                 :      145904 :   all_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
     478                 :      145904 :   no_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
     479                 :      145904 :   ignore_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
     480                 :             : 
     481                 :      145904 :   if (ipa_ref_var_info_summaries == NULL)
     482                 :      145904 :     ipa_ref_var_info_summaries = new ipa_ref_var_info_summary_t (symtab);
     483                 :             : }
     484                 :             : 
     485                 :             : 
     486                 :             : /* Set up the persistent info for FN.  */
     487                 :             : 
     488                 :             : static ipa_reference_local_vars_info_t
     489                 :     2069551 : init_function_info (struct cgraph_node *fn)
     490                 :             : {
     491                 :     2069551 :   ipa_reference_vars_info_t info
     492                 :     2069551 :     = ipa_ref_var_info_summaries->get_create (fn);
     493                 :             : 
     494                 :     2069551 :   info->local.statics_read = BITMAP_ALLOC (&local_info_obstack);
     495                 :     2069551 :   info->local.statics_written = BITMAP_ALLOC (&local_info_obstack);
     496                 :     2069551 :   info->global.statics_read = NULL;
     497                 :             : 
     498                 :     2069551 :   return &info->local;
     499                 :             : }
     500                 :             : 
     501                 :             : 
     502                 :             : /* This is the main routine for finding the reference patterns for
     503                 :             :    global variables within a function FN.  */
     504                 :             : 
     505                 :             : static void
     506                 :     2080540 : analyze_function (struct cgraph_node *fn)
     507                 :             : {
     508                 :     2080540 :   ipa_reference_local_vars_info_t local;
     509                 :     2080540 :   struct ipa_ref *ref = NULL;
     510                 :     2080540 :   int i;
     511                 :     2080540 :   tree var;
     512                 :             : 
     513                 :     2080540 :   if (!opt_for_fn (fn->decl, flag_ipa_reference))
     514                 :       10989 :     return;
     515                 :     2069551 :   local = init_function_info (fn);
     516                 :     5732069 :   for (i = 0; fn->iterate_reference (i, ref); i++)
     517                 :             :     {
     518                 :     3662518 :       int id;
     519                 :     3662518 :       bool existed;
     520                 :     3662518 :       if (!is_a <varpool_node *> (ref->referred))
     521                 :     3345231 :         continue;
     522                 :     3406495 :       var = ref->referred->decl;
     523                 :     3406495 :       if (!is_proper_for_analysis (var))
     524                 :     3089208 :         continue;
     525                 :             :       /* This is a variable we care about.  Check if we have seen it
     526                 :             :          before, and if not add it the set of variables we care about.  */
     527                 :      317287 :       id = ipa_reference_var_get_or_insert_uid (var, &existed);
     528                 :      317287 :       if (!existed)
     529                 :             :         {
     530                 :       50106 :           bitmap_set_bit (all_module_statics, id);
     531                 :       50106 :           if (dump_file)
     532                 :           0 :             reference_vars_to_consider->safe_push (var);
     533                 :             :         }
     534                 :      317287 :       switch (ref->use)
     535                 :             :         {
     536                 :      229780 :         case IPA_REF_LOAD:
     537                 :      229780 :           bitmap_set_bit (local->statics_read, id);
     538                 :      229780 :           break;
     539                 :       87507 :         case IPA_REF_STORE:
     540                 :       87507 :           if (ref->cannot_lead_to_return ())
     541                 :             :             break;
     542                 :       86514 :           bitmap_set_bit (local->statics_written, id);
     543                 :       86514 :           break;
     544                 :             :         case IPA_REF_ADDR:
     545                 :             :           break;
     546                 :           0 :         default:
     547                 :           0 :           gcc_unreachable ();
     548                 :             :         }
     549                 :             :     }
     550                 :             : 
     551                 :     2069551 :   if (fn->cannot_return_p ())
     552                 :       24652 :     bitmap_clear (local->statics_written);
     553                 :             : }
     554                 :             : 
     555                 :             : 
     556                 :             : /* Called when new clone is inserted to callgraph late.  */
     557                 :             : 
     558                 :             : void
     559                 :           0 : ipa_ref_opt_summary_t::duplicate (cgraph_node *, cgraph_node *,
     560                 :             :                                   ipa_reference_optimization_summary_d *ginfo,
     561                 :             :                                   ipa_reference_optimization_summary_d
     562                 :             :                                   *dst_ginfo)
     563                 :             : {
     564                 :           0 :   dst_ginfo->statics_read =
     565                 :           0 :     copy_static_var_set (ginfo->statics_read, false);
     566                 :           0 :   dst_ginfo->statics_written =
     567                 :           0 :     copy_static_var_set (ginfo->statics_written, false);
     568                 :           0 : }
     569                 :             : 
     570                 :             : /* Called when node is removed.  */
     571                 :             : 
     572                 :             : void
     573                 :           0 : ipa_ref_opt_summary_t::remove (cgraph_node *,
     574                 :             :                                ipa_reference_optimization_summary_d *ginfo)
     575                 :             : {
     576                 :           0 :   if (ginfo->statics_read
     577                 :           0 :       && ginfo->statics_read != all_module_statics
     578                 :           0 :       && ginfo->statics_read != no_module_statics)
     579                 :           0 :     BITMAP_FREE (ginfo->statics_read);
     580                 :             : 
     581                 :           0 :   if (ginfo->statics_written
     582                 :           0 :       && ginfo->statics_written != all_module_statics
     583                 :           0 :       && ginfo->statics_written != no_module_statics)
     584                 :           0 :     BITMAP_FREE (ginfo->statics_written);
     585                 :           0 : }
     586                 :             : 
     587                 :             : /* Analyze each function in the cgraph to see which global or statics
     588                 :             :    are read or written.  */
     589                 :             : 
     590                 :             : static void
     591                 :      145904 : generate_summary (void)
     592                 :             : {
     593                 :      145904 :   struct cgraph_node *node;
     594                 :      145904 :   unsigned int index;
     595                 :      145904 :   bitmap_iterator bi;
     596                 :             : 
     597                 :      145904 :   ipa_init ();
     598                 :             : 
     599                 :             :   /* Process all of the functions next.  */
     600                 :     2226444 :   FOR_EACH_DEFINED_FUNCTION (node)
     601                 :     2080540 :     if (!node->alias && !opt_for_fn (node->decl, flag_ipa_reference))
     602                 :             :       {
     603                 :       13777 :         struct ipa_ref *ref = NULL;
     604                 :             :         int i;
     605                 :             :         tree var;
     606                 :     2087467 :         for (i = 0; node->iterate_reference (i, ref); i++)
     607                 :             :           {
     608                 :        3815 :             if (!is_a <varpool_node *> (ref->referred))
     609                 :        2467 :               continue;
     610                 :        1348 :             var = ref->referred->decl;
     611                 :        1348 :             if (!is_proper_for_analysis (var))
     612                 :        1227 :               continue;
     613                 :         121 :             bitmap_set_bit (ignore_module_statics, ipa_reference_var_uid (var));
     614                 :             :           }
     615                 :             :       }
     616                 :     2226444 :   FOR_EACH_DEFINED_FUNCTION (node)
     617                 :     2080540 :     analyze_function (node);
     618                 :             : 
     619                 :      145904 :   if (dump_file)
     620                 :          20 :     EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi)
     621                 :             :       {
     622                 :           0 :         fprintf (dump_file, "\nPromotable global:%s (uid=%u)\n",
     623                 :             :                  get_static_name (index), index);
     624                 :             :       }
     625                 :             : 
     626                 :      145904 :   if (dump_file)
     627                 :          54 :     FOR_EACH_DEFINED_FUNCTION (node)
     628                 :          34 :       if (node->get_availability () >= AVAIL_INTERPOSABLE
     629                 :          34 :           && opt_for_fn (node->decl, flag_ipa_reference))
     630                 :             :         {
     631                 :          34 :           ipa_reference_local_vars_info_t l;
     632                 :          34 :           unsigned int index;
     633                 :          34 :           bitmap_iterator bi;
     634                 :             : 
     635                 :          34 :           l = &get_reference_vars_info (node)->local;
     636                 :          34 :           fprintf (dump_file,
     637                 :             :                    "\nFunction name:%s:", node->dump_name ());
     638                 :          34 :           fprintf (dump_file, "\n  locals read: ");
     639                 :          34 :           if (l->statics_read)
     640                 :          34 :             EXECUTE_IF_SET_IN_BITMAP (l->statics_read,
     641                 :             :                                       0, index, bi)
     642                 :             :               {
     643                 :           0 :                 fprintf (dump_file, "%s ",
     644                 :             :                          get_static_name (index));
     645                 :             :               }
     646                 :          34 :           fprintf (dump_file, "\n  locals written: ");
     647                 :          34 :           if (l->statics_written)
     648                 :          34 :             EXECUTE_IF_SET_IN_BITMAP (l->statics_written,
     649                 :             :                                       0, index, bi)
     650                 :             :               {
     651                 :           0 :                 fprintf (dump_file, "%s ", get_static_name (index));
     652                 :             :               }
     653                 :             :         }
     654                 :      145904 : }
     655                 :             : 
     656                 :             : /* Set READ_ALL/WRITE_ALL based on decl flags of NODE.  */
     657                 :             : 
     658                 :             : static void
     659                 :     2138398 : read_write_all_from_decl (struct cgraph_node *node,
     660                 :             :                           bool &read_all, bool &write_all)
     661                 :             : {
     662                 :     2138398 :   tree decl = node->decl;
     663                 :     2138398 :   int flags = flags_from_decl_or_type (decl);
     664                 :     2138398 :   if ((flags & ECF_LEAF)
     665                 :     2138398 :       && node->get_availability () < AVAIL_INTERPOSABLE)
     666                 :             :     ;
     667                 :      998067 :   else if (flags & ECF_CONST)
     668                 :             :     ;
     669                 :      982414 :   else if ((flags & ECF_PURE) || node->cannot_return_p ())
     670                 :             :     {
     671                 :      150084 :       read_all = true;
     672                 :      150084 :       if (dump_file && (dump_flags & TDF_DETAILS))
     673                 :           0 :         fprintf (dump_file, "   %s -> read all\n", node->dump_name ());
     674                 :             :     }
     675                 :             :   else
     676                 :             :     {
     677                 :             :        /* TODO: To be able to produce sane results, we should also handle
     678                 :             :           common builtins, in particular throw.  */
     679                 :      832330 :       read_all = true;
     680                 :      832330 :       write_all = true;
     681                 :      832330 :       if (dump_file && (dump_flags & TDF_DETAILS))
     682                 :           0 :         fprintf (dump_file, "   %s -> read all, write all\n",
     683                 :             :                   node->dump_name ());
     684                 :             :     }
     685                 :     2138398 : }
     686                 :             : 
     687                 :             : /* Set READ_ALL/WRITE_ALL based on decl flags of NODE or any member
     688                 :             :    in the cycle of NODE.  */
     689                 :             : 
     690                 :             : static void
     691                 :     2022830 : get_read_write_all_from_node (struct cgraph_node *node,
     692                 :             :                               bool &read_all, bool &write_all)
     693                 :             : {
     694                 :     2022830 :   struct cgraph_edge *e, *ie;
     695                 :             : 
     696                 :             :   /* When function is overwritable, we cannot assume anything.  */
     697                 :     2022830 :   if (node->get_availability () <= AVAIL_INTERPOSABLE
     698                 :     2022830 :       || (node->analyzed && !opt_for_fn (node->decl, flag_ipa_reference)))
     699                 :       80767 :     read_write_all_from_decl (node, read_all, write_all);
     700                 :             : 
     701                 :     2022830 :   for (e = node->callees;
     702                 :     5613134 :        e && !(read_all && write_all);
     703                 :     3590304 :        e = e->next_callee)
     704                 :             :     {
     705                 :     3590304 :       enum availability avail;
     706                 :     3590304 :       struct cgraph_node *callee = e->callee->function_symbol (&avail);
     707                 :     3590304 :       gcc_checking_assert (callee);
     708                 :     3590304 :       if (avail <= AVAIL_INTERPOSABLE
     709                 :     3590304 :           || (callee->analyzed && !opt_for_fn (callee->decl,
     710                 :             :                                                flag_ipa_reference)))
     711                 :     2057631 :         read_write_all_from_decl (callee, read_all, write_all);
     712                 :             :     }
     713                 :             : 
     714                 :     2022830 :   for (ie = node->indirect_calls;
     715                 :     2081371 :        ie && !(read_all && write_all);
     716                 :       58541 :        ie = ie->next_callee)
     717                 :       58541 :     if (!(ie->indirect_info->ecf_flags & ECF_CONST))
     718                 :             :       {
     719                 :       58531 :         read_all = true;
     720                 :       58531 :         if (dump_file && (dump_flags & TDF_DETAILS))
     721                 :           0 :           fprintf (dump_file, "   indirect call -> read all\n");
     722                 :       58531 :         if (!ie->cannot_lead_to_return_p ()
     723                 :       58531 :             && !(ie->indirect_info->ecf_flags & ECF_PURE))
     724                 :             :           {
     725                 :       58455 :             if (dump_file && (dump_flags & TDF_DETAILS))
     726                 :           0 :               fprintf (dump_file, "   indirect call -> write all\n");
     727                 :       58455 :             write_all = true;
     728                 :             :           }
     729                 :             :       }
     730                 :     2022830 : }
     731                 :             : 
     732                 :             : /* Skip edges from and to nodes without ipa_reference enabled.
     733                 :             :    Ignore not available symbols.  This leave
     734                 :             :    them out of strongly connected components and makes them easy to skip in the
     735                 :             :    propagation loop bellow.  */
     736                 :             : 
     737                 :             : static bool
     738                 :     6310303 : ignore_edge_p (cgraph_edge *e)
     739                 :             : {
     740                 :     6310303 :   enum availability avail;
     741                 :     6310303 :   cgraph_node *ultimate_target
     742                 :     6310303 :     = e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
     743                 :             : 
     744                 :     6310303 :   return (avail < AVAIL_INTERPOSABLE
     745                 :     2225982 :           || (avail == AVAIL_INTERPOSABLE
     746                 :       71482 :               && !(flags_from_decl_or_type (e->callee->decl) & ECF_LEAF))
     747                 :     2154548 :           || !opt_for_fn (e->caller->decl, flag_ipa_reference)
     748                 :     8456105 :           || !opt_for_fn (ultimate_target->decl, flag_ipa_reference));
     749                 :             : }
     750                 :             : 
     751                 :             : /* Produce the global information by preforming a transitive closure
     752                 :             :    on the local information that was produced by ipa_analyze_function.  */
     753                 :             : 
     754                 :             : static unsigned int
     755                 :      145904 : propagate (void)
     756                 :             : {
     757                 :      145904 :   struct cgraph_node *node;
     758                 :      145904 :   struct cgraph_node **order =
     759                 :      145904 :     XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
     760                 :      145904 :   int order_pos;
     761                 :      145904 :   int i;
     762                 :      145904 :   bool remove_p;
     763                 :             : 
     764                 :      145904 :   if (dump_file)
     765                 :          20 :     cgraph_node::dump_cgraph (dump_file);
     766                 :             : 
     767                 :      145904 :   remove_p = ipa_discover_variable_flags ();
     768                 :      145904 :   generate_summary ();
     769                 :             : 
     770                 :             :   /* Propagate the local information through the call graph to produce
     771                 :             :      the global information.  All the nodes within a cycle will have
     772                 :             :      the same info so we collapse cycles first.  Then we can do the
     773                 :             :      propagation in one pass from the leaves to the roots.  */
     774                 :      145904 :   order_pos = ipa_reduced_postorder (order, true, ignore_edge_p);
     775                 :      145904 :   if (dump_file)
     776                 :          20 :     ipa_print_order (dump_file, "reduced", order, order_pos);
     777                 :             : 
     778                 :     2205535 :   for (i = 0; i < order_pos; i++ )
     779                 :             :     {
     780                 :     2059631 :       unsigned x;
     781                 :     2059631 :       struct cgraph_node *w;
     782                 :     2059631 :       ipa_reference_vars_info_t node_info;
     783                 :     2059631 :       ipa_reference_global_vars_info_t node_g;
     784                 :     2059631 :       ipa_reference_local_vars_info_t node_l;
     785                 :     2059631 :       bool read_all = false;
     786                 :     2059631 :       bool write_all = false;
     787                 :             : 
     788                 :     2059631 :       node = order[i];
     789                 :     2059631 :       if (node->alias || !opt_for_fn (node->decl, flag_ipa_reference))
     790                 :       45615 :         continue;
     791                 :             : 
     792                 :     2014016 :       node_info = get_reference_vars_info (node);
     793                 :     2014016 :       gcc_assert (node_info);
     794                 :     2014016 :       node_l = &node_info->local;
     795                 :     2014016 :       node_g = &node_info->global;
     796                 :             : 
     797                 :     2014016 :       if (dump_file && (dump_flags & TDF_DETAILS))
     798                 :           0 :         fprintf (dump_file, "Starting cycle with %s\n", node->dump_name ());
     799                 :             : 
     800                 :     2014016 :       vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
     801                 :             : 
     802                 :             :       /* If any node in a cycle is read_all or write_all, they all are.  */
     803                 :     5160077 :       FOR_EACH_VEC_ELT (cycle_nodes, x, w)
     804                 :             :         {
     805                 :     2022830 :           if (dump_file && (dump_flags & TDF_DETAILS))
     806                 :           0 :             fprintf (dump_file, "  Visiting %s\n", w->dump_asm_name ());
     807                 :     2022830 :           get_read_write_all_from_node (w, read_all, write_all);
     808                 :     2022830 :           if (read_all && write_all)
     809                 :             :             break;
     810                 :             :         }
     811                 :             : 
     812                 :             :       /* Initialized the bitmaps global sets for the reduced node.  */
     813                 :     2014016 :       if (read_all)
     814                 :      943816 :         node_g->statics_read = all_module_statics;
     815                 :             :       else
     816                 :     1070200 :         node_g->statics_read = copy_static_var_set (node_l->statics_read, true);
     817                 :     2014016 :       if (write_all)
     818                 :      890785 :         node_g->statics_written = all_module_statics;
     819                 :             :       else
     820                 :     1123231 :         node_g->statics_written
     821                 :     1123231 :           = copy_static_var_set (node_l->statics_written, true);
     822                 :             : 
     823                 :             :       /* Merge the sets of this cycle with all sets of callees reached
     824                 :             :          from this cycle.  */
     825                 :     3142014 :       FOR_EACH_VEC_ELT (cycle_nodes, x, w)
     826                 :             :         {
     827                 :     2019118 :           if (read_all && write_all)
     828                 :             :             break;
     829                 :             : 
     830                 :     1127998 :           if (w != node)
     831                 :             :             {
     832                 :        4767 :               ipa_reference_vars_info_t w_ri = get_reference_vars_info (w);
     833                 :        4767 :               ipa_reference_local_vars_info_t w_l = &w_ri->local;
     834                 :        4767 :               int flags = flags_from_decl_or_type (w->decl);
     835                 :             : 
     836                 :        4767 :               if (!(flags & ECF_CONST))
     837                 :        3116 :                 read_all = union_static_var_sets (node_g->statics_read,
     838                 :             :                                                   w_l->statics_read);
     839                 :        4767 :               if (!(flags & ECF_PURE)
     840                 :        4767 :                   && !w->cannot_return_p ())
     841                 :        3901 :                 write_all = union_static_var_sets (node_g->statics_written,
     842                 :             :                                                    w_l->statics_written);
     843                 :             :             }
     844                 :             : 
     845                 :     1127998 :           propagate_bits (node_g, w);
     846                 :             :         }
     847                 :             : 
     848                 :             :       /* All nodes within a cycle have the same global info bitmaps.  */
     849                 :     4048937 :       FOR_EACH_VEC_ELT (cycle_nodes, x, w)
     850                 :             :         {
     851                 :     2034921 :           ipa_reference_vars_info_t w_ri = get_reference_vars_info (w);
     852                 :     2034921 :           w_ri->global = *node_g;
     853                 :             :         }
     854                 :             : 
     855                 :     2014016 :       cycle_nodes.release ();
     856                 :             :     }
     857                 :             : 
     858                 :      145904 :   if (dump_file)
     859                 :             :     {
     860                 :          54 :       for (i = 0; i < order_pos; i++)
     861                 :             :         {
     862                 :          34 :           unsigned x;
     863                 :          34 :           struct cgraph_node *w;
     864                 :             : 
     865                 :          34 :           node = order[i];
     866                 :          34 :           if (node->alias || !opt_for_fn (node->decl, flag_ipa_reference))
     867                 :           0 :             continue;
     868                 :             : 
     869                 :          34 :           fprintf (dump_file, "\nFunction name:%s:", node->dump_asm_name ());
     870                 :             : 
     871                 :          34 :           ipa_reference_vars_info_t node_info = get_reference_vars_info (node);
     872                 :          34 :           ipa_reference_global_vars_info_t node_g = &node_info->global;
     873                 :             : 
     874                 :          34 :           vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
     875                 :          68 :           FOR_EACH_VEC_ELT (cycle_nodes, x, w)
     876                 :             :             {
     877                 :          34 :               ipa_reference_vars_info_t w_ri = get_reference_vars_info (w);
     878                 :          34 :               ipa_reference_local_vars_info_t w_l = &w_ri->local;
     879                 :          34 :               if (w != node)
     880                 :           0 :                 fprintf (dump_file, "\n  next cycle: %s ", w->dump_asm_name ());
     881                 :          34 :               fprintf (dump_file, "\n    locals read: ");
     882                 :          34 :               dump_static_vars_set_to_file (dump_file, w_l->statics_read);
     883                 :          34 :               fprintf (dump_file, "\n    locals written: ");
     884                 :          34 :               dump_static_vars_set_to_file (dump_file, w_l->statics_written);
     885                 :             :             }
     886                 :          34 :           cycle_nodes.release ();
     887                 :             : 
     888                 :          34 :           fprintf (dump_file, "\n  globals read: ");
     889                 :          34 :           dump_static_vars_set_to_file (dump_file, node_g->statics_read);
     890                 :          34 :           fprintf (dump_file, "\n  globals written: ");
     891                 :          34 :           dump_static_vars_set_to_file (dump_file, node_g->statics_written);
     892                 :          34 :           fprintf (dump_file, "\n");
     893                 :             :         }
     894                 :             :     }
     895                 :             : 
     896                 :      145904 :   if (ipa_ref_opt_sum_summaries == NULL)
     897                 :             :     {
     898                 :      145904 :       ipa_ref_opt_sum_summaries = new ipa_ref_opt_summary_t (symtab);
     899                 :      145904 :       ipa_ref_opt_sum_summaries->disable_insertion_hook ();
     900                 :             :     }
     901                 :             : 
     902                 :             :   /* Cleanup. */
     903                 :     2226444 :   FOR_EACH_DEFINED_FUNCTION (node)
     904                 :             :     {
     905                 :     2080540 :       ipa_reference_vars_info_t node_info;
     906                 :     2080540 :       ipa_reference_global_vars_info_t node_g;
     907                 :             : 
     908                 :             :       /* No need to produce summaries for inline clones.  */
     909                 :     2080540 :       if (node->inlined_to)
     910                 :     1062409 :         continue;
     911                 :             : 
     912                 :     1018131 :       node_info = get_reference_vars_info (node);
     913                 :     1018131 :       if (!node->alias && opt_for_fn (node->decl, flag_ipa_reference))
     914                 :             :         {
     915                 :      972512 :           node_g = &node_info->global;
     916                 :      972512 :           bool read_all = 
     917                 :      972512 :                 (node_g->statics_read == all_module_statics
     918                 :      972512 :                  || bitmap_equal_p (node_g->statics_read, all_module_statics));
     919                 :      972512 :           bool written_all = 
     920                 :      972512 :                 (node_g->statics_written == all_module_statics
     921                 :      972512 :                  || bitmap_equal_p (node_g->statics_written,
     922                 :      972512 :                                     all_module_statics));
     923                 :             : 
     924                 :             :           /* There is no need to produce summary if we collected nothing
     925                 :             :              useful.  */
     926                 :      972512 :           if (read_all && written_all)
     927                 :      930442 :             continue;
     928                 :             : 
     929                 :       42070 :           ipa_reference_optimization_summary_d *opt
     930                 :       42070 :             = ipa_ref_opt_sum_summaries->get_create (node);
     931                 :             : 
     932                 :             :           /* Create the complimentary sets.  */
     933                 :             : 
     934                 :       42070 :           if (bitmap_empty_p (node_g->statics_read))
     935                 :       33062 :             opt->statics_read = no_module_statics;
     936                 :        9008 :           else if (read_all)
     937                 :        7050 :             opt->statics_read = all_module_statics;
     938                 :             :           else
     939                 :             :             {
     940                 :        1958 :               opt->statics_read
     941                 :        1958 :                  = BITMAP_ALLOC (&optimization_summary_obstack);
     942                 :        1958 :               bitmap_copy (opt->statics_read, node_g->statics_read);
     943                 :             :             }
     944                 :             : 
     945                 :       42070 :           if (bitmap_empty_p (node_g->statics_written))
     946                 :       36782 :             opt->statics_written = no_module_statics;
     947                 :        5288 :           else if (written_all)
     948                 :        3483 :             opt->statics_written = all_module_statics;
     949                 :             :           else
     950                 :             :             {
     951                 :        1805 :               opt->statics_written
     952                 :        1805 :                 = BITMAP_ALLOC (&optimization_summary_obstack);
     953                 :        1805 :               bitmap_copy (opt->statics_written, node_g->statics_written);
     954                 :             :             }
     955                 :             :         }
     956                 :             :    }
     957                 :             : 
     958                 :      145904 :   ipa_free_postorder_info ();
     959                 :      145904 :   free (order);
     960                 :             : 
     961                 :      145904 :   bitmap_obstack_release (&local_info_obstack);
     962                 :             : 
     963                 :      145904 :   if (ipa_ref_var_info_summaries != NULL)
     964                 :             :     {
     965                 :      145904 :       delete ipa_ref_var_info_summaries;
     966                 :      145904 :       ipa_ref_var_info_summaries = NULL;
     967                 :             :     }
     968                 :             : 
     969                 :      145904 :   if (dump_file)
     970                 :             :     {
     971                 :          20 :       vec_free (reference_vars_to_consider);
     972                 :          20 :       reference_vars_to_consider = NULL;
     973                 :             :     }
     974                 :             :   else
     975                 :      145884 :     gcc_checking_assert (!reference_vars_to_consider);
     976                 :      145904 :   return remove_p ? TODO_remove_functions : 0;
     977                 :             : }
     978                 :             : 
     979                 :             : /* Return true if we need to write summary of NODE. */
     980                 :             : 
     981                 :             : static bool
     982                 :       96712 : write_node_summary_p (struct cgraph_node *node,
     983                 :             :                       lto_symtab_encoder_t encoder,
     984                 :             :                       bitmap ltrans_statics)
     985                 :             : {
     986                 :       96712 :   ipa_reference_optimization_summary_t info;
     987                 :             : 
     988                 :             :   /* See if we have (non-empty) info.  */
     989                 :       96712 :   if (!node->definition || node->inlined_to)
     990                 :             :     return false;
     991                 :       44780 :   info = get_reference_optimization_summary (node);
     992                 :       44780 :   if (!info)
     993                 :             :     return false;
     994                 :             : 
     995                 :             :   /* See if we want to encode it.
     996                 :             :      Encode also referenced functions since constant folding might turn it into
     997                 :             :      a direct call.
     998                 :             : 
     999                 :             :      In future we might also want to include summaries of functions references
    1000                 :             :      by initializers of constant variables references in current unit.  */
    1001                 :        7672 :   if (!reachable_from_this_partition_p (node, encoder)
    1002                 :        7672 :       && !referenced_from_this_partition_p (node, encoder))
    1003                 :             :     return false;
    1004                 :             : 
    1005                 :             :   /* See if the info has non-empty intersections with vars we want to
    1006                 :             :      encode.  */
    1007                 :        6358 :   bitmap_iterator bi;
    1008                 :        6358 :   unsigned int i;
    1009                 :        6358 :   EXECUTE_IF_AND_COMPL_IN_BITMAP (ltrans_statics, info->statics_read, 0,
    1010                 :             :                                   i, bi)
    1011                 :             :     return true;
    1012                 :        2022 :   EXECUTE_IF_AND_COMPL_IN_BITMAP (ltrans_statics, info->statics_written, 0,
    1013                 :             :                                   i, bi)
    1014                 :             :     return true;
    1015                 :             :   return false;
    1016                 :             : }
    1017                 :             : 
    1018                 :             : /* Stream out BITS&LTRANS_STATICS as list of decls to OB.
    1019                 :             :    LTRANS_STATICS_BITCOUNT specify number of bits in LTRANS_STATICS
    1020                 :             :    or -1.  When it is positive, just output -1 when
    1021                 :             :    BITS&LTRANS_STATICS == BITS&LTRANS_STATICS.  */
    1022                 :             : 
    1023                 :             : static void
    1024                 :        7166 : stream_out_bitmap (struct lto_simple_output_block *ob,
    1025                 :             :                    bitmap bits, bitmap ltrans_statics,
    1026                 :             :                    int ltrans_statics_bitcount)
    1027                 :             : {
    1028                 :        7166 :   int count = 0;
    1029                 :        7166 :   unsigned int index;
    1030                 :        7166 :   bitmap_iterator bi;
    1031                 :        7166 :   if (bits == all_module_statics)
    1032                 :             :     {
    1033                 :        1286 :       streamer_write_hwi_stream (ob->main_stream, -1);
    1034                 :        6592 :       return;
    1035                 :             :     }
    1036                 :        9196 :   EXECUTE_IF_AND_IN_BITMAP (bits, ltrans_statics, 0, index, bi)
    1037                 :        3316 :     count ++;
    1038                 :        5880 :   if (count == ltrans_statics_bitcount)
    1039                 :             :     {
    1040                 :           0 :       streamer_write_hwi_stream (ob->main_stream, -1);
    1041                 :           0 :       return;
    1042                 :             :     }
    1043                 :        5880 :   streamer_write_hwi_stream (ob->main_stream, count);
    1044                 :        5880 :   if (!count)
    1045                 :             :     return;
    1046                 :        5176 :   EXECUTE_IF_AND_IN_BITMAP (bits, ltrans_statics, 0, index, bi)
    1047                 :             :     {
    1048                 :        3316 :       tree decl = (*reference_vars_to_consider) [index];
    1049                 :        3316 :       lto_output_var_decl_ref (ob->decl_state, ob->main_stream, decl);
    1050                 :             :     }
    1051                 :             : }
    1052                 :             : 
    1053                 :             : /* Serialize the ipa info for lto.  */
    1054                 :             : 
    1055                 :             : static void
    1056                 :        9154 : ipa_reference_write_optimization_summary (void)
    1057                 :             : {
    1058                 :        9154 :   struct lto_simple_output_block *ob
    1059                 :        9154 :     = lto_create_simple_output_block (LTO_section_ipa_reference);
    1060                 :        9154 :   unsigned int count = 0;
    1061                 :        9154 :   int ltrans_statics_bitcount = 0;
    1062                 :        9154 :   lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
    1063                 :        9154 :   auto_bitmap ltrans_statics;
    1064                 :        9154 :   int i;
    1065                 :             : 
    1066                 :        9154 :   gcc_checking_assert (!reference_vars_to_consider);
    1067                 :        9154 :   vec_alloc (reference_vars_to_consider, ipa_reference_vars_uids);
    1068                 :        9154 :   reference_vars_to_consider->safe_grow (ipa_reference_vars_uids, true);
    1069                 :             : 
    1070                 :             :   /* See what variables we are interested in.  */
    1071                 :      226191 :   for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
    1072                 :             :     {
    1073                 :      103946 :       symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
    1074                 :      207892 :       varpool_node *vnode = dyn_cast <varpool_node *> (snode);
    1075                 :       23821 :       int id;
    1076                 :             : 
    1077                 :       23821 :       if (vnode
    1078                 :       23821 :           && (id = ipa_reference_var_uid (vnode->decl)) != -1
    1079                 :        3395 :           && referenced_from_this_partition_p (vnode, encoder))
    1080                 :             :         {
    1081                 :        3389 :           tree decl = vnode->decl;
    1082                 :        3389 :           bitmap_set_bit (ltrans_statics, id);
    1083                 :        3389 :           (*reference_vars_to_consider)[id] = decl;
    1084                 :        3389 :           ltrans_statics_bitcount ++;
    1085                 :             :         }
    1086                 :             :     }
    1087                 :             : 
    1088                 :             : 
    1089                 :        9154 :   if (ltrans_statics_bitcount)
    1090                 :      114536 :     for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
    1091                 :             :       {
    1092                 :       55651 :         symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
    1093                 :      111302 :         cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
    1094                 :       48356 :         if (cnode && write_node_summary_p (cnode, encoder, ltrans_statics))
    1095                 :        3179 :           count++;
    1096                 :             :       }
    1097                 :             : 
    1098                 :        9154 :   streamer_write_uhwi_stream (ob->main_stream, count);
    1099                 :        9154 :   if (count)
    1100                 :         808 :     stream_out_bitmap (ob, ltrans_statics, ltrans_statics,
    1101                 :             :                        -1);
    1102                 :             : 
    1103                 :             :   /* Process all of the functions.  */
    1104                 :        9154 :   if (ltrans_statics_bitcount)
    1105                 :      114536 :     for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
    1106                 :             :       {
    1107                 :       55651 :         symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
    1108                 :      111302 :         cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
    1109                 :       48356 :         if (cnode && write_node_summary_p (cnode, encoder, ltrans_statics))
    1110                 :             :           {
    1111                 :        3179 :             ipa_reference_optimization_summary_t info;
    1112                 :        3179 :             int node_ref;
    1113                 :             : 
    1114                 :        3179 :             info = get_reference_optimization_summary (cnode);
    1115                 :        3179 :             node_ref = lto_symtab_encoder_encode (encoder, snode);
    1116                 :        3179 :             streamer_write_uhwi_stream (ob->main_stream, node_ref);
    1117                 :             : 
    1118                 :        3179 :             stream_out_bitmap (ob, info->statics_read, ltrans_statics,
    1119                 :             :                                ltrans_statics_bitcount);
    1120                 :        3179 :             stream_out_bitmap (ob, info->statics_written, ltrans_statics,
    1121                 :             :                                ltrans_statics_bitcount);
    1122                 :             :           }
    1123                 :             :       }
    1124                 :        9154 :   lto_destroy_simple_output_block (ob);
    1125                 :        9154 :   vec_free (reference_vars_to_consider);
    1126                 :        9154 :   reference_vars_to_consider = NULL;
    1127                 :        9154 : }
    1128                 :             : 
    1129                 :             : /* Deserialize the ipa info for lto.  */
    1130                 :             : 
    1131                 :             : static void
    1132                 :        9154 : ipa_reference_read_optimization_summary (void)
    1133                 :             : {
    1134                 :        9154 :   struct lto_file_decl_data ** file_data_vec
    1135                 :        9154 :     = lto_get_file_decl_data ();
    1136                 :        9154 :   struct lto_file_decl_data * file_data;
    1137                 :        9154 :   unsigned int j = 0;
    1138                 :        9154 :   bitmap_obstack_initialize (&optimization_summary_obstack);
    1139                 :             : 
    1140                 :        9154 :   gcc_checking_assert (ipa_ref_opt_sum_summaries == NULL);
    1141                 :        9154 :   ipa_ref_opt_sum_summaries = new ipa_ref_opt_summary_t (symtab);
    1142                 :        9154 :   ipa_ref_opt_sum_summaries->disable_insertion_hook ();
    1143                 :        9154 :   ipa_reference_vars_map = new reference_vars_map_t(257);
    1144                 :        9154 :   varpool_node_hooks
    1145                 :        9154 :          = symtab->add_varpool_removal_hook (varpool_removal_hook, NULL);
    1146                 :        9154 :   ipa_reference_vars_uids = 0;
    1147                 :             : 
    1148                 :        9154 :   all_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
    1149                 :        9154 :   no_module_statics = BITMAP_ALLOC (&optimization_summary_obstack);
    1150                 :             : 
    1151                 :       18308 :   while ((file_data = file_data_vec[j++]))
    1152                 :             :     {
    1153                 :        9154 :       const char *data;
    1154                 :        9154 :       size_t len;
    1155                 :        9154 :       class lto_input_block *ib
    1156                 :        9154 :         = lto_create_simple_input_block (file_data,
    1157                 :             :                                          LTO_section_ipa_reference,
    1158                 :             :                                          &data, &len);
    1159                 :        9154 :       if (ib)
    1160                 :             :         {
    1161                 :        9154 :           unsigned int i;
    1162                 :        9154 :           unsigned int f_count = streamer_read_uhwi (ib);
    1163                 :        9154 :           int b_count;
    1164                 :        9154 :           if (!f_count)
    1165                 :        8346 :             continue;
    1166                 :         808 :           b_count = streamer_read_hwi (ib);
    1167                 :         808 :           if (dump_file)
    1168                 :           0 :             fprintf (dump_file, "all module statics:");
    1169                 :        2596 :           for (i = 0; i < (unsigned int)b_count; i++)
    1170                 :             :             {
    1171                 :        1788 :               tree v_decl = lto_input_var_decl_ref (ib, file_data);
    1172                 :        1788 :               bool existed;
    1173                 :        1788 :               bitmap_set_bit (all_module_statics,
    1174                 :             :                               ipa_reference_var_get_or_insert_uid
    1175                 :             :                                  (v_decl, &existed));
    1176                 :        1788 :               gcc_checking_assert (!existed);
    1177                 :        1788 :               if (dump_file)
    1178                 :           0 :                 fprintf (dump_file, " %s", fndecl_name (v_decl));
    1179                 :             :             }
    1180                 :             : 
    1181                 :        3987 :           for (i = 0; i < f_count; i++)
    1182                 :             :             {
    1183                 :        3179 :               unsigned int j, index;
    1184                 :        3179 :               struct cgraph_node *node;
    1185                 :        3179 :               int v_count;
    1186                 :        3179 :               lto_symtab_encoder_t encoder;
    1187                 :             : 
    1188                 :        3179 :               index = streamer_read_uhwi (ib);
    1189                 :        3179 :               encoder = file_data->symtab_node_encoder;
    1190                 :        3179 :               node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref
    1191                 :             :                 (encoder, index));
    1192                 :             : 
    1193                 :        3179 :               ipa_reference_optimization_summary_d *info
    1194                 :        3179 :                 = ipa_ref_opt_sum_summaries->get_create (node);
    1195                 :             : 
    1196                 :        3179 :               if (dump_file)
    1197                 :           0 :                 fprintf (dump_file,
    1198                 :             :                          "\nFunction name:%s:\n  static read:",
    1199                 :             :                          node->dump_asm_name ());
    1200                 :             : 
    1201                 :             :               /* Set the statics read.  */
    1202                 :        3179 :               v_count = streamer_read_hwi (ib);
    1203                 :        3179 :               if (v_count == -1)
    1204                 :             :                 {
    1205                 :        1011 :                   info->statics_read = all_module_statics;
    1206                 :        1011 :                   if (dump_file)
    1207                 :           0 :                     fprintf (dump_file, " all module statics");
    1208                 :             :                 }
    1209                 :        2168 :               else if (v_count == 0)
    1210                 :        1598 :                 info->statics_read = no_module_statics;
    1211                 :             :               else
    1212                 :             :                 {
    1213                 :        1140 :                   info->statics_read = BITMAP_ALLOC
    1214                 :         570 :                     (&optimization_summary_obstack);
    1215                 :        1447 :                   for (j = 0; j < (unsigned int)v_count; j++)
    1216                 :             :                     {
    1217                 :         877 :                       tree v_decl = lto_input_var_decl_ref (ib, file_data);
    1218                 :         877 :                       bitmap_set_bit (info->statics_read,
    1219                 :             :                                       ipa_reference_var_uid (v_decl));
    1220                 :         877 :                       if (dump_file)
    1221                 :           0 :                         fprintf (dump_file, " %s", fndecl_name (v_decl));
    1222                 :             :                     }
    1223                 :             :                 }
    1224                 :             : 
    1225                 :        3179 :               if (dump_file)
    1226                 :           0 :                 fprintf (dump_file,
    1227                 :             :                          "\n  static written:");
    1228                 :             :               /* Set the statics written.  */
    1229                 :        3179 :               v_count = streamer_read_hwi (ib);
    1230                 :        3179 :               if (v_count == -1)
    1231                 :             :                 {
    1232                 :         275 :                   info->statics_written = all_module_statics;
    1233                 :         275 :                   if (dump_file)
    1234                 :           0 :                     fprintf (dump_file, " all module statics");
    1235                 :             :                 }
    1236                 :        2904 :               else if (v_count == 0)
    1237                 :        2422 :                 info->statics_written = no_module_statics;
    1238                 :             :               else
    1239                 :             :                 {
    1240                 :         964 :                   info->statics_written = BITMAP_ALLOC
    1241                 :         482 :                     (&optimization_summary_obstack);
    1242                 :        1133 :                   for (j = 0; j < (unsigned int)v_count; j++)
    1243                 :             :                     {
    1244                 :         651 :                       tree v_decl = lto_input_var_decl_ref (ib, file_data);
    1245                 :         651 :                       bitmap_set_bit (info->statics_written,
    1246                 :             :                                       ipa_reference_var_uid (v_decl));
    1247                 :         651 :                       if (dump_file)
    1248                 :           0 :                         fprintf (dump_file, " %s", fndecl_name (v_decl));
    1249                 :             :                     }
    1250                 :             :                 }
    1251                 :        3179 :               if (dump_file)
    1252                 :           0 :                 fprintf (dump_file, "\n");
    1253                 :             :             }
    1254                 :             : 
    1255                 :         808 :           lto_destroy_simple_input_block (file_data,
    1256                 :             :                                           LTO_section_ipa_reference,
    1257                 :             :                                           ib, data, len);
    1258                 :             :         }
    1259                 :             :       else
    1260                 :             :         /* Fatal error here.  We do not want to support compiling ltrans units
    1261                 :             :            with different version of compiler or different flags than
    1262                 :             :            the WPA unit, so this should never happen.  */
    1263                 :           0 :         fatal_error (input_location,
    1264                 :             :                      "ipa reference summary is missing in ltrans unit");
    1265                 :             :     }
    1266                 :        9154 : }
    1267                 :             : 
    1268                 :             : namespace {
    1269                 :             : 
    1270                 :             : const pass_data pass_data_ipa_reference =
    1271                 :             : {
    1272                 :             :   IPA_PASS, /* type */
    1273                 :             :   "static-var", /* name */
    1274                 :             :   OPTGROUP_NONE, /* optinfo_flags */
    1275                 :             :   TV_IPA_REFERENCE, /* tv_id */
    1276                 :             :   0, /* properties_required */
    1277                 :             :   0, /* properties_provided */
    1278                 :             :   0, /* properties_destroyed */
    1279                 :             :   0, /* todo_flags_start */
    1280                 :             :   0, /* todo_flags_finish */
    1281                 :             : };
    1282                 :             : 
    1283                 :             : class pass_ipa_reference : public ipa_opt_pass_d
    1284                 :             : {
    1285                 :             : public:
    1286                 :      285617 :   pass_ipa_reference (gcc::context *ctxt)
    1287                 :             :     : ipa_opt_pass_d (pass_data_ipa_reference, ctxt,
    1288                 :             :                       NULL, /* generate_summary */
    1289                 :             :                       NULL, /* write_summary */
    1290                 :             :                       NULL, /* read_summary */
    1291                 :             :                       ipa_reference_write_optimization_summary, /*
    1292                 :             :                       write_optimization_summary */
    1293                 :             :                       ipa_reference_read_optimization_summary, /*
    1294                 :             :                       read_optimization_summary */
    1295                 :             :                       NULL, /* stmt_fixup */
    1296                 :             :                       0, /* function_transform_todo_flags_start */
    1297                 :             :                       NULL, /* function_transform */
    1298                 :      285617 :                       NULL) /* variable_transform */
    1299                 :      285617 :     {}
    1300                 :             : 
    1301                 :             :   /* opt_pass methods: */
    1302                 :      572229 :   bool gate (function *) final override
    1303                 :             :     {
    1304                 :      446314 :       return ((in_lto_p || flag_ipa_reference)
    1305                 :             :               /* Don't bother doing anything if the program has errors.  */
    1306                 :      846908 :               && !seen_error ());
    1307                 :             :     }
    1308                 :             : 
    1309                 :      145904 :   unsigned int execute (function *) final override { return propagate (); }
    1310                 :             : 
    1311                 :             : }; // class pass_ipa_reference
    1312                 :             : 
    1313                 :             : } // anon namespace
    1314                 :             : 
    1315                 :             : ipa_opt_pass_d *
    1316                 :      285617 : make_pass_ipa_reference (gcc::context *ctxt)
    1317                 :             : {
    1318                 :      285617 :   return new pass_ipa_reference (ctxt);
    1319                 :             : }
    1320                 :             : 
    1321                 :             : /* Reset all state within ipa-reference.cc so that we can rerun the compiler
    1322                 :             :    within the same process.  For use by toplev::finalize.  */
    1323                 :             : 
    1324                 :             : void
    1325                 :      257333 : ipa_reference_cc_finalize (void)
    1326                 :             : {
    1327                 :      257333 :   if (ipa_ref_opt_sum_summaries != NULL)
    1328                 :             :     {
    1329                 :      154845 :       delete ipa_ref_opt_sum_summaries;
    1330                 :      154845 :       ipa_ref_opt_sum_summaries = NULL;
    1331                 :      309690 :       delete ipa_reference_vars_map;
    1332                 :      154845 :       ipa_reference_vars_map = NULL;
    1333                 :      154845 :       symtab->remove_varpool_removal_hook (varpool_node_hooks);
    1334                 :             :     }
    1335                 :             : 
    1336                 :      257333 :   if (ipa_init_p)
    1337                 :             :     {
    1338                 :      145691 :       bitmap_obstack_release (&optimization_summary_obstack);
    1339                 :      145691 :       ipa_init_p = false;
    1340                 :             :     }
    1341                 :      257333 : }
        

Generated by: LCOV version 2.0-1

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.