LCOV - code coverage report
Current view: top level - gcc - lto-section-out.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 126 135 93.3 %
Date: 2020-04-04 11:58:09 Functions: 19 21 90.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* Functions for writing LTO sections.
       2                 :            : 
       3                 :            :    Copyright (C) 2009-2020 Free Software Foundation, Inc.
       4                 :            :    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
       5                 :            : 
       6                 :            : This file is part of GCC.
       7                 :            : 
       8                 :            : GCC is free software; you can redistribute it and/or modify it under
       9                 :            : the terms of the GNU General Public License as published by the Free
      10                 :            : Software Foundation; either version 3, or (at your option) any later
      11                 :            : version.
      12                 :            : 
      13                 :            : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      14                 :            : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      15                 :            : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      16                 :            : for more details.
      17                 :            : 
      18                 :            : You should have received a copy of the GNU General Public License
      19                 :            : along with GCC; see the file COPYING3.  If not see
      20                 :            : <http://www.gnu.org/licenses/>.  */
      21                 :            : 
      22                 :            : #include "config.h"
      23                 :            : #include "system.h"
      24                 :            : #include "coretypes.h"
      25                 :            : #include "backend.h"
      26                 :            : #include "rtl.h"
      27                 :            : #include "tree.h"
      28                 :            : #include "gimple.h"
      29                 :            : #include "cgraph.h"
      30                 :            : #include "data-streamer.h"
      31                 :            : #include "langhooks.h"
      32                 :            : #include "lto-compress.h"
      33                 :            : #include "print-tree.h"
      34                 :            : 
      35                 :            : static vec<lto_out_decl_state_ptr> decl_state_stack;
      36                 :            : 
      37                 :            : /* List of out decl states used by functions.  We use this to
      38                 :            :    generate the decl directory later. */
      39                 :            : 
      40                 :            : vec<lto_out_decl_state_ptr> lto_function_decl_states;
      41                 :            : 
      42                 :            : 
      43                 :            : /*****************************************************************************
      44                 :            :    Output routines shared by all of the serialization passes.
      45                 :            : *****************************************************************************/
      46                 :            : 
      47                 :            : 
      48                 :            : /* Flush compressed stream data function, sends NUM_CHARS from CHARS
      49                 :            :    to the append lang hook, OPAQUE is currently always NULL.  */
      50                 :            : 
      51                 :            : static void
      52                 :     240819 : lto_append_data (const char *chars, unsigned int num_chars, void *opaque)
      53                 :            : {
      54                 :     240819 :   gcc_assert (opaque == NULL);
      55                 :     240819 :   lang_hooks.lto.append_data (chars, num_chars, opaque);
      56                 :     240819 : }
      57                 :            : 
      58                 :            : /* Pointer to the current compression stream.  */
      59                 :            : 
      60                 :            : static struct lto_compression_stream *compression_stream = NULL;
      61                 :            : 
      62                 :            : /* Begin a new output section named NAME. If COMPRESS is true, zlib compress
      63                 :            :    the section. */
      64                 :            : 
      65                 :            : void
      66                 :     383301 : lto_begin_section (const char *name, bool compress)
      67                 :            : {
      68                 :     383301 :   lang_hooks.lto.begin_section (name);
      69                 :            : 
      70                 :     383301 :   if (streamer_dump_file)
      71                 :            :     {
      72                 :         64 :       if (flag_dump_unnumbered || flag_dump_noaddr)
      73                 :         80 :           fprintf (streamer_dump_file, "Creating %ssection\n",
      74                 :            :                    compress ? "compressed " : "");
      75                 :            :         else
      76                 :          0 :           fprintf (streamer_dump_file, "Creating %ssection %s\n",
      77                 :            :                    compress ? "compressed " : "", name);
      78                 :            :     }
      79                 :     383301 :   gcc_assert (compression_stream == NULL);
      80                 :     383301 :   if (compress)
      81                 :     240819 :     compression_stream = lto_start_compression (lto_append_data, NULL);
      82                 :     383301 : }
      83                 :            : 
      84                 :            : 
      85                 :            : /* End the current output section.  */
      86                 :            : 
      87                 :            : void
      88                 :     383301 : lto_end_section (void)
      89                 :            : {
      90                 :     383301 :   if (compression_stream)
      91                 :            :     {
      92                 :     240819 :       lto_end_compression (compression_stream);
      93                 :     240819 :       compression_stream = NULL;
      94                 :            :     }
      95                 :     383301 :   lang_hooks.lto.end_section ();
      96                 :     383301 : }
      97                 :            : 
      98                 :            : /* Write SIZE bytes starting at DATA to the assembler.  */
      99                 :            : 
     100                 :            : void
     101                 :    5713570 : lto_write_data (const void *data, unsigned int size)
     102                 :            : {
     103                 :    5713570 :   if (compression_stream)
     104                 :    1172430 :     lto_compress_block (compression_stream, (const char *)data, size);
     105                 :            :   else
     106                 :    4541140 :     lang_hooks.lto.append_data ((const char *)data, size, NULL);
     107                 :    5713570 : }
     108                 :            : 
     109                 :            : /* Write SIZE bytes starting at DATA to the assembler.  */
     110                 :            : 
     111                 :            : void
     112                 :      27793 : lto_write_raw_data (const void *data, unsigned int size)
     113                 :            : {
     114                 :      27793 :   lang_hooks.lto.append_data ((const char *)data, size, NULL);
     115                 :      27793 : }
     116                 :            : 
     117                 :            : /* Write all of the chars in OBS to the assembler.  Recycle the blocks
     118                 :            :    in obs as this is being done.  */
     119                 :            : 
     120                 :            : void
     121                 :     581742 : lto_write_stream (struct lto_output_stream *obs)
     122                 :            : {
     123                 :     581742 :   unsigned int block_size = 1024;
     124                 :     581742 :   struct lto_char_ptr_base *block;
     125                 :     581742 :   struct lto_char_ptr_base *next_block;
     126                 :     581742 :   if (!obs->first_block)
     127                 :            :     return;
     128                 :            : 
     129                 :    1042840 :   for (block = obs->first_block; block; block = next_block)
     130                 :            :     {
     131                 :     546358 :       const char *base = ((char *)block) + sizeof (struct lto_char_ptr_base);
     132                 :     546358 :       unsigned int num_chars = block_size - sizeof (struct lto_char_ptr_base);
     133                 :            : 
     134                 :            :       /* If this is not the last block, it is full.  If it is the last
     135                 :            :          block, left_in_block indicates how many chars are unoccupied in
     136                 :            :          this block; subtract from num_chars to obtain occupancy.  */
     137                 :     546358 :       next_block = (struct lto_char_ptr_base *) block->ptr;
     138                 :     546358 :       if (!next_block)
     139                 :     496485 :         num_chars -= obs->left_in_block;
     140                 :            : 
     141                 :     546358 :       if (compression_stream)
     142                 :     483249 :         lto_compress_block (compression_stream, base, num_chars);
     143                 :            :       else
     144                 :      63109 :         lang_hooks.lto.append_data (base, num_chars, block);
     145                 :     546358 :       free (block);
     146                 :     546358 :       block_size *= 2;
     147                 :            :     }
     148                 :            : }
     149                 :            : 
     150                 :            : 
     151                 :            : /* Lookup NAME in ENCODER.  If NAME is not found, create a new entry in
     152                 :            :    ENCODER for NAME with the next available index of ENCODER,  then
     153                 :            :    print the index to OBS.  True is returned if NAME was added to
     154                 :            :    ENCODER.  The resulting index is stored in THIS_INDEX.
     155                 :            : 
     156                 :            :    If OBS is NULL, the only action is to add NAME to the encoder. */
     157                 :            : 
     158                 :            : bool
     159                 :    6376430 : lto_output_decl_index (struct lto_output_stream *obs,
     160                 :            :                        struct lto_tree_ref_encoder *encoder,
     161                 :            :                        tree name, unsigned int *this_index)
     162                 :            : {
     163                 :    6376430 :   bool new_entry_p = FALSE;
     164                 :    6376430 :   bool existed_p;
     165                 :            : 
     166                 :    6376430 :   unsigned int &index
     167                 :    6376430 :     = encoder->tree_hash_table->get_or_insert (name, &existed_p);
     168                 :    6376430 :   if (!existed_p)
     169                 :            :     {
     170                 :    2161400 :       index = encoder->trees.length ();
     171                 :    2161400 :       if (streamer_dump_file)
     172                 :            :         {
     173                 :        176 :           print_node_brief (streamer_dump_file, "    Encoding indexable ",
     174                 :            :                             name, 4);
     175                 :        176 :           fprintf (streamer_dump_file, "  as %i \n", index);
     176                 :            :         }
     177                 :    2161400 :       encoder->trees.safe_push (name);
     178                 :    2161400 :       new_entry_p = TRUE;
     179                 :            :     }
     180                 :            : 
     181                 :    6376430 :   if (obs)
     182                 :    6376430 :     streamer_write_uhwi_stream (obs, index);
     183                 :    6376430 :   *this_index = index;
     184                 :    6376430 :   return new_entry_p;
     185                 :            : }
     186                 :            : 
     187                 :            : /* Output a field DECL to OBS.  */
     188                 :            : 
     189                 :            : void
     190                 :     285294 : lto_output_field_decl_index (struct lto_out_decl_state *decl_state,
     191                 :            :                              struct lto_output_stream * obs, tree decl)
     192                 :            : {
     193                 :     285294 :   unsigned int index;
     194                 :     285294 :   lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FIELD_DECL],
     195                 :            :                          decl, &index);
     196                 :     285294 : }
     197                 :            : 
     198                 :            : /* Output a function DECL to OBS.  */
     199                 :            : 
     200                 :            : void
     201                 :    1426260 : lto_output_fn_decl_index (struct lto_out_decl_state *decl_state,
     202                 :            :                           struct lto_output_stream * obs, tree decl)
     203                 :            : {
     204                 :    1426260 :   unsigned int index;
     205                 :    1426260 :   lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_FN_DECL],
     206                 :            :                          decl, &index);
     207                 :    1426260 : }
     208                 :            : 
     209                 :            : /* Output a namespace DECL to OBS.  */
     210                 :            : 
     211                 :            : void
     212                 :          0 : lto_output_namespace_decl_index (struct lto_out_decl_state *decl_state,
     213                 :            :                                  struct lto_output_stream * obs, tree decl)
     214                 :            : {
     215                 :          0 :   unsigned int index;
     216                 :          0 :   lto_output_decl_index (obs,
     217                 :            :                          &decl_state->streams[LTO_DECL_STREAM_NAMESPACE_DECL],
     218                 :            :                          decl, &index);
     219                 :          0 : }
     220                 :            : 
     221                 :            : /* Output a static or extern var DECL to OBS.  */
     222                 :            : 
     223                 :            : void
     224                 :     496172 : lto_output_var_decl_index (struct lto_out_decl_state *decl_state,
     225                 :            :                            struct lto_output_stream * obs, tree decl)
     226                 :            : {
     227                 :     496172 :   unsigned int index;
     228                 :     496172 :   lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_VAR_DECL],
     229                 :            :                          decl, &index);
     230                 :     496172 : }
     231                 :            : 
     232                 :            : /* Output a type DECL to OBS.  */
     233                 :            : 
     234                 :            : void
     235                 :          0 : lto_output_type_decl_index (struct lto_out_decl_state *decl_state,
     236                 :            :                             struct lto_output_stream * obs, tree decl)
     237                 :            : {
     238                 :          0 :   unsigned int index;
     239                 :          0 :   lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE_DECL],
     240                 :            :                          decl, &index);
     241                 :          0 : }
     242                 :            : 
     243                 :            : /* Output a type REF to OBS.  */
     244                 :            : 
     245                 :            : void
     246                 :    4168700 : lto_output_type_ref_index (struct lto_out_decl_state *decl_state,
     247                 :            :                            struct lto_output_stream *obs, tree ref)
     248                 :            : {
     249                 :    4168700 :   unsigned int index;
     250                 :    4168700 :   lto_output_decl_index (obs, &decl_state->streams[LTO_DECL_STREAM_TYPE],
     251                 :            :                          ref, &index);
     252                 :    4168700 : }
     253                 :            : 
     254                 :            : 
     255                 :            : /* Create the output block and return it.  */
     256                 :            : 
     257                 :            : struct lto_simple_output_block *
     258                 :      79632 : lto_create_simple_output_block (enum lto_section_type section_type)
     259                 :            : {
     260                 :      79632 :   struct lto_simple_output_block *ob
     261                 :            :     = ((struct lto_simple_output_block *)
     262                 :      79632 :        xcalloc (1, sizeof (struct lto_simple_output_block)));
     263                 :            : 
     264                 :      79632 :   ob->section_type = section_type;
     265                 :      79632 :   ob->decl_state = lto_get_out_decl_state ();
     266                 :     159264 :   ob->main_stream = ((struct lto_output_stream *)
     267                 :      79632 :                      xcalloc (1, sizeof (struct lto_output_stream)));
     268                 :            : 
     269                 :      79632 :   return ob;
     270                 :            : }
     271                 :            : 
     272                 :            : 
     273                 :            : /* Produce a simple section for one of the ipa passes.  */
     274                 :            : 
     275                 :            : void
     276                 :      79632 : lto_destroy_simple_output_block (struct lto_simple_output_block *ob)
     277                 :            : {
     278                 :      79632 :   char *section_name;
     279                 :      79632 :   struct lto_simple_header header;
     280                 :            : 
     281                 :      79632 :   section_name = lto_get_section_name (ob->section_type, NULL, 0, NULL);
     282                 :      79632 :   lto_begin_section (section_name, !flag_wpa);
     283                 :      79632 :   free (section_name);
     284                 :            : 
     285                 :            :   /* Write the header which says how to decode the pieces of the
     286                 :            :      t.  */
     287                 :      79632 :   memset (&header, 0, sizeof (struct lto_simple_header));
     288                 :      79632 :   header.main_size = ob->main_stream->total_size;
     289                 :      79632 :   lto_write_data (&header, sizeof header);
     290                 :            : 
     291                 :      79632 :   lto_write_stream (ob->main_stream);
     292                 :            : 
     293                 :            :   /* Put back the assembly section that was there before we started
     294                 :            :      writing lto info.  */
     295                 :      79632 :   lto_end_section ();
     296                 :            : 
     297                 :      79632 :   free (ob->main_stream);
     298                 :      79632 :   free (ob);
     299                 :      79632 : }
     300                 :            : 
     301                 :            : 
     302                 :            : /* Return a new lto_out_decl_state. */
     303                 :            : 
     304                 :            : struct lto_out_decl_state *
     305                 :     153269 : lto_new_out_decl_state (void)
     306                 :            : {
     307                 :     153269 :   struct lto_out_decl_state *state = XCNEW (struct lto_out_decl_state);
     308                 :     153269 :   int i;
     309                 :            : 
     310                 :    1226150 :   for (i = 0; i < LTO_N_DECL_STREAMS; i++)
     311                 :    1072880 :     lto_init_tree_ref_encoder (&state->streams[i]);
     312                 :            : 
     313                 :            :   /* At WPA time we do not compress sections by default.  */
     314                 :     153269 :   state->compressed = !flag_wpa;
     315                 :            : 
     316                 :     153269 :   return state;
     317                 :            : }
     318                 :            : 
     319                 :            : 
     320                 :            : /* Delete STATE and components.  */
     321                 :            : 
     322                 :            : void
     323                 :     153269 : lto_delete_out_decl_state (struct lto_out_decl_state *state)
     324                 :            : {
     325                 :     153269 :   int i;
     326                 :            : 
     327                 :    1226150 :   for (i = 0; i < LTO_N_DECL_STREAMS; i++)
     328                 :    1072880 :     lto_destroy_tree_ref_encoder (&state->streams[i]);
     329                 :            : 
     330                 :     153269 :   free (state);
     331                 :     153269 : }
     332                 :            : 
     333                 :            : 
     334                 :            : /* Get the currently used lto_out_decl_state structure. */
     335                 :            : 
     336                 :            : struct lto_out_decl_state *
     337                 :     540743 : lto_get_out_decl_state (void)
     338                 :            : {
     339                 :     540743 :   return decl_state_stack.last ();
     340                 :            : }
     341                 :            : 
     342                 :            : /* Push STATE to top of out decl stack. */
     343                 :            : 
     344                 :            : void
     345                 :     153269 : lto_push_out_decl_state (struct lto_out_decl_state *state)
     346                 :            : {
     347                 :     153269 :   decl_state_stack.safe_push (state);
     348                 :     153269 : }
     349                 :            : 
     350                 :            : /* Pop the currently used out-decl state from top of stack. */
     351                 :            : 
     352                 :            : struct lto_out_decl_state *
     353                 :     153269 : lto_pop_out_decl_state (void)
     354                 :            : {
     355                 :     153269 :   return decl_state_stack.pop ();
     356                 :            : }
     357                 :            : 
     358                 :            : /* Record STATE after it has been used in serializing the body of
     359                 :            :    FN_DECL.  STATE should no longer be used by the caller.  The ownership
     360                 :            :    of it is taken over from this point.  */
     361                 :            : 
     362                 :            : void
     363                 :     131431 : lto_record_function_out_decl_state (tree fn_decl,
     364                 :            :                                     struct lto_out_decl_state *state)
     365                 :            : {
     366                 :     131431 :   int i;
     367                 :            : 
     368                 :            :   /* Strip all hash tables to save some memory. */
     369                 :    1051450 :   for (i = 0; i < LTO_N_DECL_STREAMS; i++)
     370                 :     920017 :     if (state->streams[i].tree_hash_table)
     371                 :            :       {
     372                 :     920017 :         delete state->streams[i].tree_hash_table;
     373                 :     920017 :         state->streams[i].tree_hash_table = NULL;
     374                 :            :       }
     375                 :     131431 :   state->fn_decl = fn_decl;
     376                 :     131431 :   lto_function_decl_states.safe_push (state);
     377                 :     131431 : }

Generated by: LCOV version 1.0

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto --enable-host-shared. GCC test suite is run with the built compiler.