LCOV - code coverage report
Current view: top level - gcc - data-streamer.h (source / functions) Hit Total Coverage
Test: gcc.info Lines: 89 99 89.9 %
Date: 2020-03-28 11:57:23 Functions: 7 7 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* Generic streaming support for various data types.
       2                 :            : 
       3                 :            :    Copyright (C) 2011-2020 Free Software Foundation, Inc.
       4                 :            :    Contributed by Diego Novillo <dnovillo@google.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                 :            : #ifndef GCC_DATA_STREAMER_H
      23                 :            : #define GCC_DATA_STREAMER_H
      24                 :            : 
      25                 :            : #include "lto-streamer.h"
      26                 :            : 
      27                 :            : /* Data structures used to pack values and bitflags into a vector of
      28                 :            :    words.  Used to stream values of a fixed number of bits in a space
      29                 :            :    efficient way.  */
      30                 :            : static unsigned const BITS_PER_BITPACK_WORD = HOST_BITS_PER_WIDE_INT;
      31                 :            : 
      32                 :            : typedef unsigned HOST_WIDE_INT bitpack_word_t;
      33                 :            : 
      34                 :            : struct bitpack_d
      35                 :            : {
      36                 :            :   /* The position of the first unused or unconsumed bit in the word.  */
      37                 :            :   unsigned pos;
      38                 :            : 
      39                 :            :   /* The current word we are (un)packing.  */
      40                 :            :   bitpack_word_t word;
      41                 :            : 
      42                 :            :   /* The lto_output_stream or the lto_input_block we are streaming to/from.  */
      43                 :            :   void *stream;
      44                 :            : };
      45                 :            : 
      46                 :            : /* In data-streamer.c  */
      47                 :            : void bp_pack_var_len_unsigned (struct bitpack_d *, unsigned HOST_WIDE_INT);
      48                 :            : void bp_pack_var_len_int (struct bitpack_d *, HOST_WIDE_INT);
      49                 :            : unsigned HOST_WIDE_INT bp_unpack_var_len_unsigned (struct bitpack_d *);
      50                 :            : HOST_WIDE_INT bp_unpack_var_len_int (struct bitpack_d *);
      51                 :            : 
      52                 :            : /* In data-streamer-out.c  */
      53                 :            : void streamer_write_zero (struct output_block *);
      54                 :            : void streamer_write_uhwi (struct output_block *, unsigned HOST_WIDE_INT);
      55                 :            : void streamer_write_hwi (struct output_block *, HOST_WIDE_INT);
      56                 :            : void streamer_write_poly_uint64 (struct output_block *, poly_uint64);
      57                 :            : void streamer_write_gcov_count (struct output_block *, gcov_type);
      58                 :            : void streamer_write_string (struct output_block *, struct lto_output_stream *,
      59                 :            :                             const char *, bool);
      60                 :            : void streamer_write_string_with_length (struct output_block *,
      61                 :            :                                         struct lto_output_stream *,
      62                 :            :                                         const char *, unsigned int, bool);
      63                 :            : void bp_pack_string_with_length (struct output_block *, struct bitpack_d *,
      64                 :            :                                  const char *, unsigned int, bool);
      65                 :            : void bp_pack_string (struct output_block *, struct bitpack_d *,
      66                 :            :                      const char *, bool);
      67                 :            : void streamer_write_uhwi_stream (struct lto_output_stream *,
      68                 :            :                                  unsigned HOST_WIDE_INT);
      69                 :            : void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT);
      70                 :            : void streamer_write_gcov_count_stream (struct lto_output_stream *, gcov_type);
      71                 :            : void streamer_write_data_stream (struct lto_output_stream *, const void *,
      72                 :            :                                  size_t);
      73                 :            : void streamer_write_wide_int (struct output_block *, const wide_int &);
      74                 :            : void streamer_write_widest_int (struct output_block *, const widest_int &);
      75                 :            : 
      76                 :            : /* In data-streamer-in.c  */
      77                 :            : const char *streamer_read_string (class data_in *, class lto_input_block *);
      78                 :            : const char *streamer_read_indexed_string (class data_in *,
      79                 :            :                                           class lto_input_block *,
      80                 :            :                                           unsigned int *);
      81                 :            : const char *bp_unpack_indexed_string (class data_in *, struct bitpack_d *,
      82                 :            :                                       unsigned int *);
      83                 :            : const char *bp_unpack_string (class data_in *, struct bitpack_d *);
      84                 :            : unsigned HOST_WIDE_INT streamer_read_uhwi (class lto_input_block *);
      85                 :            : HOST_WIDE_INT streamer_read_hwi (class lto_input_block *);
      86                 :            : poly_uint64 streamer_read_poly_uint64 (class lto_input_block *);
      87                 :            : gcov_type streamer_read_gcov_count (class lto_input_block *);
      88                 :            : wide_int streamer_read_wide_int (class lto_input_block *);
      89                 :            : widest_int streamer_read_widest_int (class lto_input_block *);
      90                 :            : 
      91                 :            : /* Returns a new bit-packing context for bit-packing into S.  */
      92                 :            : static inline struct bitpack_d
      93                 :    9380571 : bitpack_create (struct lto_output_stream *s)
      94                 :            : {
      95                 :    9380571 :   struct bitpack_d bp;
      96                 :    9380571 :   bp.pos = 0;
      97                 :    9380571 :   bp.word = 0;
      98                 :    9380571 :   bp.stream = (void *)s;
      99                 :    9380571 :   return bp;
     100                 :            : }
     101                 :            : 
     102                 :            : /* Pack the NBITS bit sized value VAL into the bit-packing context BP.  */
     103                 :            : static inline void
     104                 :  189319592 : bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
     105                 :            : {
     106                 :  189319592 :   bitpack_word_t word = bp->word;
     107                 :  189319592 :   int pos = bp->pos;
     108                 :            : 
     109                 :            :   /* Verify that VAL fits in the NBITS.  */
     110                 :  189319592 :   gcc_checking_assert (nbits == BITS_PER_BITPACK_WORD
     111                 :            :                        || !(val & ~(((bitpack_word_t)1<<nbits)-1)));
     112                 :            : 
     113                 :            :   /* If val does not fit into the current bitpack word switch to the
     114                 :            :      next one.  */
     115                 :  189319592 :   if (pos + nbits > BITS_PER_BITPACK_WORD)
     116                 :            :     {
     117                 :   12750653 :       streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
     118                 :            :                                   word);
     119                 :   12750653 :       word = val;
     120                 :   12750653 :       pos = nbits;
     121                 :            :     }
     122                 :            :   else
     123                 :            :     {
     124                 :  176568968 :       word |= val << pos;
     125                 :  176568968 :       pos += nbits;
     126                 :            :     }
     127                 :  189319592 :   bp->word = word;
     128                 :  189319592 :   bp->pos = pos;
     129                 :  189319592 : }
     130                 :            : 
     131                 :            : /* Pack VAL into the bit-packing context BP, using NBITS for each
     132                 :            :    coefficient.  */
     133                 :            : static inline void
     134                 :            : bp_pack_poly_value (struct bitpack_d *bp,
     135                 :            :                     const poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t> &val,
     136                 :            :                     unsigned nbits)
     137                 :            : {
     138                 :          0 :   for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
     139                 :          0 :     bp_pack_value (bp, val.coeffs[i], nbits);
     140                 :            : }
     141                 :            : 
     142                 :            : /* Finishes bit-packing of BP.  */
     143                 :            : static inline void
     144                 :    9828203 : streamer_write_bitpack (struct bitpack_d *bp)
     145                 :            : {
     146                 :    9591143 :   streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
     147                 :            :                               bp->word);
     148                 :    9828203 :   bp->word = 0;
     149                 :    3892089 :   bp->pos = 0;
     150                 :     250610 : }
     151                 :            : 
     152                 :            : /* Returns a new bit-packing context for bit-unpacking from IB.  */
     153                 :            : static inline struct bitpack_d
     154                 :    5972277 : streamer_read_bitpack (class lto_input_block *ib)
     155                 :            : {
     156                 :    5972277 :   struct bitpack_d bp;
     157                 :    5972277 :   bp.word = streamer_read_uhwi (ib);
     158                 :    5972277 :   bp.pos = 0;
     159                 :    5972277 :   bp.stream = (void *)ib;
     160                 :    5972277 :   return bp;
     161                 :            : }
     162                 :            : 
     163                 :            : /* Unpacks NBITS bits from the bit-packing context BP and returns them.  */
     164                 :            : static inline bitpack_word_t
     165                 :  103025084 : bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
     166                 :            : {
     167                 :  103025084 :   bitpack_word_t mask, val;
     168                 :  103025084 :   int pos = bp->pos;
     169                 :            : 
     170                 :  206050618 :   mask = (nbits == BITS_PER_BITPACK_WORD
     171                 :  103025084 :           ? (bitpack_word_t) -1
     172                 :   95620094 :           : ((bitpack_word_t) 1 << nbits) - 1);
     173                 :            : 
     174                 :            :   /* If there are not continuous nbits in the current bitpack word
     175                 :            :      switch to the next one.  */
     176                 :  103025084 :   if (pos + nbits > BITS_PER_BITPACK_WORD)
     177                 :            :     {
     178                 :   17137828 :       bp->word = val 
     179                 :    8568904 :         = streamer_read_uhwi ((class lto_input_block *)bp->stream);
     180                 :    8568904 :       bp->pos = nbits;
     181                 :    8568904 :       return val & mask;
     182                 :            :     }
     183                 :   94456204 :   val = bp->word;
     184                 :   94456204 :   val >>= pos;
     185                 :   94456204 :   bp->pos = pos + nbits;
     186                 :            : 
     187                 :   94456204 :   return val & mask;
     188                 :            : }
     189                 :            : 
     190                 :            : /* Unpacks a polynomial value from the bit-packing context BP in which each
     191                 :            :    coefficient has NBITS bits.  */
     192                 :            : static inline poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t>
     193                 :          0 : bp_unpack_poly_value (struct bitpack_d *bp, unsigned nbits)
     194                 :            : {
     195                 :          0 :   poly_int_pod<NUM_POLY_INT_COEFFS, bitpack_word_t> x;
     196                 :          0 :   for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
     197                 :          0 :     x.coeffs[i] = bp_unpack_value (bp, nbits);
     198                 :          0 :   return x;
     199                 :            : }
     200                 :            : 
     201                 :            : 
     202                 :            : /* Write a character to the output block.  */
     203                 :            : 
     204                 :            : static inline void
     205                 :    7689554 : streamer_write_char_stream (struct lto_output_stream *obs, char c)
     206                 :            : {
     207                 :            :   /* No space left.  */
     208                 :    7689554 :   if (obs->left_in_block == 0)
     209                 :     126716 :     lto_append_block (obs);
     210                 :            : 
     211                 :            :   /* Write the actual character.  */
     212                 :    7689554 :   char *current_pointer = obs->current_pointer;
     213                 :    7689554 :   *(current_pointer++) = c;
     214                 :    7689554 :   obs->current_pointer = current_pointer;
     215                 :    7689554 :   obs->total_size++;
     216                 :    7689554 :   obs->left_in_block--;
     217                 :    7689554 : }
     218                 :            : 
     219                 :            : 
     220                 :            : /* Read byte from the input block.  */
     221                 :            : 
     222                 :            : static inline unsigned char
     223                 :   17746020 : streamer_read_uchar (class lto_input_block *ib)
     224                 :            : {
     225                 :   17746020 :   if (ib->p >= ib->len)
     226                 :          0 :     lto_section_overrun (ib);
     227                 :   15002510 :   return (ib->data[ib->p++]);
     228                 :            : }
     229                 :            : 
     230                 :            : /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
     231                 :            :    to be compile time constant.
     232                 :            :    Be host independent, limit range to 31bits.  */
     233                 :            : 
     234                 :            : static inline void
     235                 :   67053601 : streamer_write_hwi_in_range (struct lto_output_stream *obs,
     236                 :            :                                   HOST_WIDE_INT min,
     237                 :            :                                   HOST_WIDE_INT max,
     238                 :            :                                   HOST_WIDE_INT val)
     239                 :            : {
     240                 :   67053601 :   HOST_WIDE_INT range = max - min;
     241                 :            : 
     242                 :   66453954 :   gcc_checking_assert (val >= min && val <= max && range > 0
     243                 :            :                        && range < 0x7fffffff);
     244                 :            : 
     245                 :   67053601 :   val -= min;
     246                 :   67053601 :   streamer_write_uhwi_stream (obs, (unsigned HOST_WIDE_INT) val);
     247                 :   66143769 : }
     248                 :            : 
     249                 :            : /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
     250                 :            :    to be compile time constant.  PURPOSE is used for error reporting.  */
     251                 :            : 
     252                 :            : static inline HOST_WIDE_INT
     253                 :   39679273 : streamer_read_hwi_in_range (class lto_input_block *ib,
     254                 :            :                                  const char *purpose,
     255                 :            :                                  HOST_WIDE_INT min,
     256                 :            :                                  HOST_WIDE_INT max)
     257                 :            : {
     258                 :   39679273 :   HOST_WIDE_INT range = max - min;
     259                 :   39679273 :   unsigned HOST_WIDE_INT uval = streamer_read_uhwi (ib);
     260                 :            : 
     261                 :   39679273 :   gcc_checking_assert (range > 0 && range < 0x7fffffff);
     262                 :            : 
     263                 :   39679273 :   HOST_WIDE_INT val = (HOST_WIDE_INT) (uval + (unsigned HOST_WIDE_INT) min);
     264                 :   39679273 :   if (val < min || val > max)
     265                 :          0 :     lto_value_range_error (purpose, val, min, max);
     266                 :   39679273 :   return val;
     267                 :            : }
     268                 :            : 
     269                 :            : /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
     270                 :            :    to be compile time constant.
     271                 :            :    Be host independent, limit range to 31bits.  */
     272                 :            : 
     273                 :            : static inline void
     274                 :    6919326 : bp_pack_int_in_range (struct bitpack_d *bp,
     275                 :            :                       HOST_WIDE_INT min,
     276                 :            :                       HOST_WIDE_INT max,
     277                 :            :                       HOST_WIDE_INT val)
     278                 :            : {
     279                 :    6919326 :   HOST_WIDE_INT range = max - min;
     280                 :    6919326 :   int nbits = floor_log2 (range) + 1;
     281                 :            : 
     282                 :    6919326 :   gcc_checking_assert (val >= min && val <= max && range > 0
     283                 :            :                        && range < 0x7fffffff);
     284                 :            : 
     285                 :    6919326 :   val -= min;
     286                 :    6919326 :   bp_pack_value (bp, val, nbits);
     287                 :    6919326 : }
     288                 :            : 
     289                 :            : /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
     290                 :            :    to be compile time constant.  PURPOSE is used for error reporting.  */
     291                 :            : 
     292                 :            : static inline HOST_WIDE_INT
     293                 :    4161163 : bp_unpack_int_in_range (struct bitpack_d *bp,
     294                 :            :                         const char *purpose,
     295                 :            :                         HOST_WIDE_INT min,
     296                 :            :                         HOST_WIDE_INT max)
     297                 :            : {
     298                 :    4161163 :   HOST_WIDE_INT range = max - min;
     299                 :    4161163 :   int nbits = floor_log2 (range) + 1;
     300                 :    4161163 :   HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
     301                 :            : 
     302                 :    4161163 :   gcc_checking_assert (range > 0 && range < 0x7fffffff);
     303                 :            : 
     304                 :    4161163 :   if (val < min || val > max)
     305                 :          0 :     lto_value_range_error (purpose, val, min, max);
     306                 :    4161163 :   return val;
     307                 :            : }
     308                 :            : 
     309                 :            : /* Output VAL of type "enum enum_name" into OBS.
     310                 :            :    Assume range 0...ENUM_LAST - 1.  */
     311                 :            : #define streamer_write_enum(obs,enum_name,enum_last,val) \
     312                 :            :   streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
     313                 :            : 
     314                 :            : /* Input enum of type "enum enum_name" from IB.
     315                 :            :    Assume range 0...ENUM_LAST - 1.  */
     316                 :            : #define streamer_read_enum(ib,enum_name,enum_last) \
     317                 :            :   (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
     318                 :            :                                               (int)(enum_last) - 1)
     319                 :            : 
     320                 :            : /* Output VAL of type "enum enum_name" into BP.
     321                 :            :    Assume range 0...ENUM_LAST - 1.  */
     322                 :            : #define bp_pack_enum(bp,enum_name,enum_last,val) \
     323                 :            :   bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
     324                 :            : 
     325                 :            : /* Input enum of type "enum enum_name" from BP.
     326                 :            :    Assume range 0...ENUM_LAST - 1.  */
     327                 :            : #define bp_unpack_enum(bp,enum_name,enum_last) \
     328                 :            :   (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
     329                 :            :                                         (int)(enum_last) - 1)
     330                 :            : 
     331                 :            : /* Output the start of a record with TAG to output block OB.  */
     332                 :            : 
     333                 :            : static inline void
     334                 :   50532880 : streamer_write_record_start (struct output_block *ob, enum LTO_tags tag)
     335                 :            : {
     336                 :   50532880 :   streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
     337                 :    1591540 : }
     338                 :            : 
     339                 :            : /* Return the next tag in the input block IB.  */
     340                 :            : 
     341                 :            : static inline enum LTO_tags
     342                 :   30229290 : streamer_read_record_start (class lto_input_block *ib)
     343                 :            : {
     344                 :   30229290 :   return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
     345                 :            : }
     346                 :            : 
     347                 :            : #endif  /* GCC_DATA_STREAMER_H  */

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.