Branch data Line data Source code
1 : : /* Generic streaming support for various data types.
2 : :
3 : : Copyright (C) 2011-2024 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.cc */
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 : : void bp_pack_real_value (struct bitpack_d *, const REAL_VALUE_TYPE *);
50 : : void bp_unpack_real_value (struct bitpack_d *, REAL_VALUE_TYPE *);
51 : : unsigned HOST_WIDE_INT bp_unpack_var_len_unsigned (struct bitpack_d *);
52 : : HOST_WIDE_INT bp_unpack_var_len_int (struct bitpack_d *);
53 : :
54 : : /* In data-streamer-out.cc */
55 : : void streamer_write_zero (struct output_block *);
56 : : void streamer_write_uhwi (struct output_block *, unsigned HOST_WIDE_INT);
57 : : void streamer_write_hwi (struct output_block *, HOST_WIDE_INT);
58 : : void streamer_write_poly_uint64 (struct output_block *, poly_uint64);
59 : : void streamer_write_poly_int64 (struct output_block *, poly_int64);
60 : : void streamer_write_gcov_count (struct output_block *, gcov_type);
61 : : void streamer_write_string (struct output_block *, struct lto_output_stream *,
62 : : const char *, bool);
63 : : void streamer_write_string_with_length (struct output_block *,
64 : : struct lto_output_stream *,
65 : : const char *, unsigned int, bool);
66 : : void bp_pack_string_with_length (struct output_block *, struct bitpack_d *,
67 : : const char *, unsigned int, bool);
68 : : void bp_pack_string (struct output_block *, struct bitpack_d *,
69 : : const char *, bool);
70 : : void streamer_write_uhwi_stream (struct lto_output_stream *,
71 : : unsigned HOST_WIDE_INT);
72 : : void streamer_write_hwi_stream (struct lto_output_stream *, HOST_WIDE_INT);
73 : : void streamer_write_gcov_count_stream (struct lto_output_stream *, gcov_type);
74 : : void streamer_write_data_stream (struct lto_output_stream *, const void *,
75 : : size_t);
76 : : void streamer_write_wide_int (struct output_block *, const wide_int &);
77 : : void streamer_write_widest_int (struct output_block *, const widest_int &);
78 : : void streamer_write_vrange (struct output_block *, const class vrange &);
79 : :
80 : : /* In data-streamer-in.cc */
81 : : const char *streamer_read_string (class data_in *, class lto_input_block *);
82 : : const char *streamer_read_indexed_string (class data_in *,
83 : : class lto_input_block *,
84 : : unsigned int *);
85 : : const char *bp_unpack_indexed_string (class data_in *, struct bitpack_d *,
86 : : unsigned int *);
87 : : const char *bp_unpack_string (class data_in *, struct bitpack_d *);
88 : : unsigned HOST_WIDE_INT streamer_read_uhwi (class lto_input_block *);
89 : : HOST_WIDE_INT streamer_read_hwi (class lto_input_block *);
90 : : poly_uint64 streamer_read_poly_uint64 (class lto_input_block *);
91 : : poly_int64 streamer_read_poly_int64 (class lto_input_block *);
92 : : gcov_type streamer_read_gcov_count (class lto_input_block *);
93 : : wide_int streamer_read_wide_int (class lto_input_block *);
94 : : widest_int streamer_read_widest_int (class lto_input_block *);
95 : : void streamer_read_value_range (class lto_input_block *, class data_in *,
96 : : class Value_Range &);
97 : :
98 : : /* Returns a new bit-packing context for bit-packing into S. */
99 : : inline struct bitpack_d
100 : 15427459 : bitpack_create (struct lto_output_stream *s)
101 : : {
102 : 15427459 : struct bitpack_d bp;
103 : 15427459 : bp.pos = 0;
104 : 15427459 : bp.word = 0;
105 : 15427459 : bp.stream = (void *)s;
106 : 15427459 : return bp;
107 : : }
108 : :
109 : : /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
110 : : inline void
111 : 260433871 : bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
112 : : {
113 : 260433871 : bitpack_word_t word = bp->word;
114 : 260433871 : int pos = bp->pos;
115 : :
116 : : /* Verify that VAL fits in the NBITS. */
117 : 260433871 : gcc_checking_assert (nbits == BITS_PER_BITPACK_WORD
118 : : || !(val & ~(((bitpack_word_t)1<<nbits)-1)));
119 : :
120 : : /* If val does not fit into the current bitpack word switch to the
121 : : next one. */
122 : 260433871 : if (pos + nbits > BITS_PER_BITPACK_WORD)
123 : : {
124 : 4572645 : streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
125 : : word);
126 : 4572645 : word = val;
127 : 4572645 : pos = nbits;
128 : : }
129 : : else
130 : : {
131 : 255861226 : word |= val << pos;
132 : 255861226 : pos += nbits;
133 : : }
134 : 260433871 : bp->word = word;
135 : 260433871 : bp->pos = pos;
136 : 260433871 : }
137 : :
138 : : /* Pack VAL into the bit-packing context BP, using NBITS for each
139 : : coefficient. */
140 : : inline void
141 : : bp_pack_poly_value (struct bitpack_d *bp,
142 : : const poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t> &val,
143 : : unsigned nbits)
144 : : {
145 : 0 : for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
146 : 0 : bp_pack_value (bp, val.coeffs[i], nbits);
147 : : }
148 : :
149 : : /* Finishes bit-packing of BP. */
150 : : inline void
151 : 15451282 : streamer_write_bitpack (struct bitpack_d *bp)
152 : : {
153 : 14825035 : streamer_write_uhwi_stream ((struct lto_output_stream *) bp->stream,
154 : : bp->word);
155 : 15451282 : bp->word = 0;
156 : 7625337 : bp->pos = 0;
157 : 706417 : }
158 : :
159 : : /* Returns a new bit-packing context for bit-unpacking from IB. */
160 : : inline struct bitpack_d
161 : 11050416 : streamer_read_bitpack (class lto_input_block *ib)
162 : : {
163 : 11050416 : struct bitpack_d bp;
164 : 11050416 : bp.word = streamer_read_uhwi (ib);
165 : 11050416 : bp.pos = 0;
166 : 11050416 : bp.stream = (void *)ib;
167 : 11050416 : return bp;
168 : : }
169 : :
170 : : /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
171 : : inline bitpack_word_t
172 : 163251009 : bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
173 : : {
174 : 163251009 : bitpack_word_t mask, val;
175 : 163251009 : int pos = bp->pos;
176 : :
177 : 326502018 : mask = (nbits == BITS_PER_BITPACK_WORD
178 : 163251009 : ? (bitpack_word_t) -1
179 : 161655564 : : ((bitpack_word_t) 1 << nbits) - 1);
180 : :
181 : : /* If there are not continuous nbits in the current bitpack word
182 : : switch to the next one. */
183 : 163251009 : if (pos + nbits > BITS_PER_BITPACK_WORD)
184 : : {
185 : 6167454 : bp->word = val
186 : 3083727 : = streamer_read_uhwi ((class lto_input_block *)bp->stream);
187 : 3083727 : bp->pos = nbits;
188 : 3083727 : return val & mask;
189 : : }
190 : 160167282 : val = bp->word;
191 : 160167282 : val >>= pos;
192 : 160167282 : bp->pos = pos + nbits;
193 : :
194 : 160167282 : return val & mask;
195 : : }
196 : :
197 : : /* Unpacks a polynomial value from the bit-packing context BP in which each
198 : : coefficient has NBITS bits. */
199 : : inline poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t>
200 : 0 : bp_unpack_poly_value (struct bitpack_d *bp, unsigned nbits)
201 : : {
202 : 0 : poly_int<NUM_POLY_INT_COEFFS, bitpack_word_t> x;
203 : 0 : for (int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
204 : 0 : x.coeffs[i] = bp_unpack_value (bp, nbits);
205 : 0 : return x;
206 : : }
207 : :
208 : :
209 : : /* Write a character to the output block. */
210 : :
211 : : inline void
212 : 14836523 : streamer_write_char_stream (struct lto_output_stream *obs, char c)
213 : : {
214 : : /* No space left. */
215 : 14836523 : if (obs->left_in_block == 0)
216 : 147940 : lto_append_block (obs);
217 : :
218 : : /* Write the actual character. */
219 : 14836523 : char *current_pointer = obs->current_pointer;
220 : 14836523 : *(current_pointer++) = c;
221 : 14836523 : obs->current_pointer = current_pointer;
222 : 14836523 : obs->total_size++;
223 : 14836523 : obs->left_in_block--;
224 : 14836523 : }
225 : :
226 : :
227 : : /* Read byte from the input block. */
228 : :
229 : : inline unsigned char
230 : 47972536 : streamer_read_uchar (class lto_input_block *ib)
231 : : {
232 : 47972536 : if (ib->p >= ib->len)
233 : 0 : lto_section_overrun (ib);
234 : 47972536 : return (ib->data[ib->p++]);
235 : : }
236 : :
237 : : /* Output VAL into OBS and verify it is in range MIN...MAX that is supposed
238 : : to be compile time constant.
239 : : Be host independent, limit range to 31bits. */
240 : :
241 : : inline void
242 : 29188281 : streamer_write_hwi_in_range (struct lto_output_stream *obs,
243 : : HOST_WIDE_INT min,
244 : : HOST_WIDE_INT max,
245 : : HOST_WIDE_INT val)
246 : : {
247 : 29188281 : HOST_WIDE_INT range = max - min;
248 : :
249 : 28288903 : gcc_checking_assert (val >= min && val <= max && range > 0
250 : : && range < 0x7fffffff);
251 : :
252 : 29188281 : val -= min;
253 : 29188281 : streamer_write_uhwi_stream (obs, (unsigned HOST_WIDE_INT) val);
254 : 28213427 : }
255 : :
256 : : /* Input VAL into OBS and verify it is in range MIN...MAX that is supposed
257 : : to be compile time constant. PURPOSE is used for error reporting. */
258 : :
259 : : inline HOST_WIDE_INT
260 : 20672657 : streamer_read_hwi_in_range (class lto_input_block *ib,
261 : : const char *purpose,
262 : : HOST_WIDE_INT min,
263 : : HOST_WIDE_INT max)
264 : : {
265 : 20672657 : HOST_WIDE_INT range = max - min;
266 : 20672657 : unsigned HOST_WIDE_INT uval = streamer_read_uhwi (ib);
267 : :
268 : 20672657 : gcc_checking_assert (range > 0 && range < 0x7fffffff);
269 : :
270 : 20672657 : HOST_WIDE_INT val = (HOST_WIDE_INT) (uval + (unsigned HOST_WIDE_INT) min);
271 : 20672657 : if (val < min || val > max)
272 : 0 : lto_value_range_error (purpose, val, min, max);
273 : 20672657 : return val;
274 : : }
275 : :
276 : : /* Output VAL into BP and verify it is in range MIN...MAX that is supposed
277 : : to be compile time constant.
278 : : Be host independent, limit range to 31bits. */
279 : :
280 : : inline void
281 : 10392964 : bp_pack_int_in_range (struct bitpack_d *bp,
282 : : HOST_WIDE_INT min,
283 : : HOST_WIDE_INT max,
284 : : HOST_WIDE_INT val)
285 : : {
286 : 10392964 : HOST_WIDE_INT range = max - min;
287 : 10392964 : int nbits = floor_log2 (range) + 1;
288 : :
289 : 10392964 : gcc_checking_assert (val >= min && val <= max && range > 0
290 : : && range < 0x7fffffff);
291 : :
292 : 10392964 : val -= min;
293 : 10392964 : bp_pack_value (bp, val, nbits);
294 : 10392964 : }
295 : :
296 : : /* Input VAL into BP and verify it is in range MIN...MAX that is supposed
297 : : to be compile time constant. PURPOSE is used for error reporting. */
298 : :
299 : : inline HOST_WIDE_INT
300 : 6964557 : bp_unpack_int_in_range (struct bitpack_d *bp,
301 : : const char *purpose,
302 : : HOST_WIDE_INT min,
303 : : HOST_WIDE_INT max)
304 : : {
305 : 6964557 : HOST_WIDE_INT range = max - min;
306 : 6964557 : int nbits = floor_log2 (range) + 1;
307 : 6964557 : HOST_WIDE_INT val = bp_unpack_value (bp, nbits);
308 : :
309 : 6964557 : gcc_checking_assert (range > 0 && range < 0x7fffffff);
310 : :
311 : 6964557 : if (val < min || val > max)
312 : 0 : lto_value_range_error (purpose, val, min, max);
313 : 6964557 : return val;
314 : : }
315 : :
316 : : /* Output VAL of type "enum enum_name" into OBS.
317 : : Assume range 0...ENUM_LAST - 1. */
318 : : #define streamer_write_enum(obs,enum_name,enum_last,val) \
319 : : streamer_write_hwi_in_range ((obs), 0, (int)(enum_last) - 1, (int)(val))
320 : :
321 : : /* Input enum of type "enum enum_name" from IB.
322 : : Assume range 0...ENUM_LAST - 1. */
323 : : #define streamer_read_enum(ib,enum_name,enum_last) \
324 : : (enum enum_name)streamer_read_hwi_in_range ((ib), #enum_name, 0, \
325 : : (int)(enum_last) - 1)
326 : :
327 : : /* Output VAL of type "enum enum_name" into BP.
328 : : Assume range 0...ENUM_LAST - 1. */
329 : : #define bp_pack_enum(bp,enum_name,enum_last,val) \
330 : : bp_pack_int_in_range ((bp), 0, (int)(enum_last) - 1, (int)(val))
331 : :
332 : : /* Input enum of type "enum enum_name" from BP.
333 : : Assume range 0...ENUM_LAST - 1. */
334 : : #define bp_unpack_enum(bp,enum_name,enum_last) \
335 : : (enum enum_name)bp_unpack_int_in_range ((bp), #enum_name, 0, \
336 : : (int)(enum_last) - 1)
337 : :
338 : : /* Output the start of a record with TAG to output block OB. */
339 : :
340 : : inline void
341 : 26976491 : streamer_write_record_start (struct output_block *ob, enum LTO_tags tag)
342 : : {
343 : 26976491 : streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS, tag);
344 : 2355110 : }
345 : :
346 : : /* Return the next tag in the input block IB. */
347 : :
348 : : inline enum LTO_tags
349 : 19181336 : streamer_read_record_start (class lto_input_block *ib)
350 : : {
351 : 19181336 : return streamer_read_enum (ib, LTO_tags, LTO_NUM_TAGS);
352 : : }
353 : :
354 : : #endif /* GCC_DATA_STREAMER_H */
|