Branch data Line data Source code
1 : : /* Routines for restoring various data types from a file stream. This deals
2 : : with various data types like strings, integers, enums, etc.
3 : :
4 : : Copyright (C) 2011-2025 Free Software Foundation, Inc.
5 : : Contributed by Diego Novillo <dnovillo@google.com>
6 : :
7 : : This file is part of GCC.
8 : :
9 : : GCC is free software; you can redistribute it and/or modify it under
10 : : the terms of the GNU General Public License as published by the Free
11 : : Software Foundation; either version 3, or (at your option) any later
12 : : version.
13 : :
14 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 : : for more details.
18 : :
19 : : You should have received a copy of the GNU General Public License
20 : : along with GCC; see the file COPYING3. If not see
21 : : <http://www.gnu.org/licenses/>. */
22 : :
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "backend.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "cgraph.h"
30 : : #include "data-streamer.h"
31 : : #include "value-range.h"
32 : : #include "streamer-hooks.h"
33 : :
34 : : /* Read a string from the string table in DATA_IN using input block
35 : : IB. Write the length to RLEN. */
36 : :
37 : : static const char *
38 : 2358238 : string_for_index (class data_in *data_in, unsigned int loc, unsigned int *rlen)
39 : : {
40 : 2358238 : unsigned int len;
41 : 2358238 : const char *result;
42 : :
43 : 2358238 : if (!loc)
44 : : {
45 : 1064031 : *rlen = 0;
46 : 1064031 : return NULL;
47 : : }
48 : :
49 : : /* Get the string stored at location LOC in DATA_IN->STRINGS. */
50 : 1294207 : lto_input_block str_tab (data_in->strings, loc - 1, data_in->strings_len, NULL);
51 : 1294207 : len = streamer_read_uhwi (&str_tab);
52 : 1294207 : *rlen = len;
53 : :
54 : 1294207 : if (str_tab.p + len > data_in->strings_len)
55 : 0 : internal_error ("bytecode stream: string too long for the string table");
56 : :
57 : 1294207 : result = (const char *)(data_in->strings + str_tab.p);
58 : :
59 : 1294207 : return result;
60 : : }
61 : :
62 : :
63 : : /* Read a string from the string table in DATA_IN using input block
64 : : IB. Write the length to RLEN. */
65 : :
66 : : const char *
67 : 1761579 : streamer_read_indexed_string (class data_in *data_in,
68 : : class lto_input_block *ib, unsigned int *rlen)
69 : : {
70 : 1761579 : return string_for_index (data_in, streamer_read_uhwi (ib), rlen);
71 : : }
72 : :
73 : :
74 : : /* Read a NULL terminated string from the string table in DATA_IN. */
75 : :
76 : : const char *
77 : 902023 : streamer_read_string (class data_in *data_in, class lto_input_block *ib)
78 : : {
79 : 902023 : unsigned int len;
80 : 902023 : const char *ptr;
81 : :
82 : 902023 : ptr = streamer_read_indexed_string (data_in, ib, &len);
83 : 902023 : if (!ptr)
84 : : return NULL;
85 : 45868 : if (ptr[len - 1] != '\0')
86 : 0 : internal_error ("bytecode stream: found non-null terminated string");
87 : :
88 : : return ptr;
89 : : }
90 : :
91 : :
92 : : /* Read a string from the string table in DATA_IN using the bitpack BP.
93 : : Write the length to RLEN. */
94 : :
95 : : const char *
96 : 596659 : bp_unpack_indexed_string (class data_in *data_in,
97 : : struct bitpack_d *bp, unsigned int *rlen)
98 : : {
99 : 596659 : return string_for_index (data_in, bp_unpack_var_len_unsigned (bp), rlen);
100 : : }
101 : :
102 : :
103 : : /* Read a NULL terminated string from the string table in DATA_IN. */
104 : :
105 : : const char *
106 : 596659 : bp_unpack_string (class data_in *data_in, struct bitpack_d *bp)
107 : : {
108 : 596659 : unsigned int len;
109 : 596659 : const char *ptr;
110 : :
111 : 596659 : ptr = bp_unpack_indexed_string (data_in, bp, &len);
112 : 596659 : if (!ptr)
113 : : return NULL;
114 : 388847 : if (ptr[len - 1] != '\0')
115 : 0 : internal_error ("bytecode stream: found non-null terminated string");
116 : :
117 : : return ptr;
118 : : }
119 : :
120 : :
121 : : /* Read an unsigned HOST_WIDE_INT number from IB. */
122 : :
123 : : unsigned HOST_WIDE_INT
124 : 67436379 : streamer_read_uhwi (class lto_input_block *ib)
125 : : {
126 : 67436379 : unsigned HOST_WIDE_INT result;
127 : 67436379 : int shift;
128 : 67436379 : unsigned HOST_WIDE_INT byte;
129 : 67436379 : unsigned int p = ib->p;
130 : 67436379 : unsigned int len = ib->len;
131 : :
132 : 67436379 : const char *data = ib->data;
133 : 67436379 : result = data[p++];
134 : 67436379 : if ((result & 0x80) != 0)
135 : : {
136 : 15855878 : result &= 0x7f;
137 : 15855878 : shift = 7;
138 : 43284792 : do
139 : : {
140 : 43284792 : byte = data[p++];
141 : 43284792 : result |= (byte & 0x7f) << shift;
142 : 43284792 : shift += 7;
143 : : }
144 : 43284792 : while ((byte & 0x80) != 0);
145 : : }
146 : :
147 : : /* We check for section overrun after the fact for performance reason. */
148 : 67436379 : if (p > len)
149 : 0 : lto_section_overrun (ib);
150 : :
151 : 67436379 : ib->p = p;
152 : 67436379 : return result;
153 : : }
154 : :
155 : :
156 : : /* Read a HOST_WIDE_INT number from IB. */
157 : :
158 : : HOST_WIDE_INT
159 : 30374992 : streamer_read_hwi (class lto_input_block *ib)
160 : : {
161 : 30374992 : HOST_WIDE_INT result = 0;
162 : 30374992 : int shift = 0;
163 : 47958783 : unsigned HOST_WIDE_INT byte;
164 : :
165 : 47958783 : while (true)
166 : : {
167 : 47958783 : byte = streamer_read_uchar (ib);
168 : 47958783 : result |= (byte & 0x7f) << shift;
169 : 47958783 : shift += 7;
170 : 47958783 : if ((byte & 0x80) == 0)
171 : : {
172 : 30374992 : if ((shift < HOST_BITS_PER_WIDE_INT) && (byte & 0x40))
173 : 4285689 : result |= - (HOST_WIDE_INT_1U << shift);
174 : :
175 : 30374992 : return result;
176 : : }
177 : : }
178 : : }
179 : :
180 : : /* Read a poly_uint64 from IB. */
181 : :
182 : : poly_uint64
183 : 0 : streamer_read_poly_uint64 (class lto_input_block *ib)
184 : : {
185 : 0 : using coeff_type = poly_int_traits<poly_uint64>::coeff_type;
186 : 0 : return poly_int_read_common<coeff_type> (streamer_read_uhwi, ib);
187 : : }
188 : :
189 : : /* Read a poly_int64 from IB. */
190 : :
191 : : poly_int64
192 : 52904 : streamer_read_poly_int64 (class lto_input_block *ib)
193 : : {
194 : 52904 : using coeff_type = poly_int_traits<poly_int64>::coeff_type;
195 : 52904 : return poly_int_read_common<coeff_type> (streamer_read_hwi, ib);
196 : : }
197 : :
198 : : /* Read gcov_type value from IB. */
199 : :
200 : : gcov_type
201 : 1520487 : streamer_read_gcov_count (class lto_input_block *ib)
202 : : {
203 : 1520487 : gcov_type ret = streamer_read_hwi (ib);
204 : 1520487 : return ret;
205 : : }
206 : :
207 : : /* Read REAL_VALUE_TYPE from IB. */
208 : :
209 : : void
210 : 0 : streamer_read_real_value (class lto_input_block *ib, REAL_VALUE_TYPE *r)
211 : : {
212 : 0 : struct bitpack_d bp = streamer_read_bitpack (ib);
213 : 0 : bp_unpack_real_value (&bp, r);
214 : 0 : }
215 : :
216 : : void
217 : 410717 : streamer_read_value_range (class lto_input_block *ib, data_in *data_in,
218 : : value_range &vr)
219 : : {
220 : : // Read the common fields to all vranges.
221 : 410717 : value_range_kind kind = streamer_read_enum (ib, value_range_kind, VR_LAST);
222 : 410717 : gcc_checking_assert (kind != VR_UNDEFINED);
223 : 410717 : tree type = stream_read_tree (ib, data_in);
224 : :
225 : : // Initialize the value_range to the correct type.
226 : 410717 : vr.set_type (type);
227 : :
228 : 410717 : if (is_a <irange> (vr))
229 : : {
230 : 218919 : irange &r = as_a <irange> (vr);
231 : 218919 : r.set_undefined ();
232 : 218919 : unsigned HOST_WIDE_INT num_pairs = streamer_read_uhwi (ib);
233 : 445015 : for (unsigned i = 0; i < num_pairs; ++i)
234 : : {
235 : 226096 : wide_int lb = streamer_read_wide_int (ib);
236 : 226096 : wide_int ub = streamer_read_wide_int (ib);
237 : 226096 : int_range<2> tmp (type, lb, ub);
238 : 226096 : r.union_ (tmp);
239 : 226098 : }
240 : 218919 : wide_int value = streamer_read_wide_int (ib);
241 : 218919 : wide_int mask = streamer_read_wide_int (ib);
242 : 218919 : irange_bitmask bm (value, mask);
243 : 218919 : r.update_bitmask (bm);
244 : 218919 : return;
245 : 218921 : }
246 : 191798 : if (is_a <frange> (vr))
247 : : {
248 : 0 : frange &r = as_a <frange> (vr);
249 : :
250 : : // Stream in NAN bits.
251 : 0 : struct bitpack_d bp = streamer_read_bitpack (ib);
252 : 0 : bool pos_nan = (bool) bp_unpack_value (&bp, 1);
253 : 0 : bool neg_nan = (bool) bp_unpack_value (&bp, 1);
254 : 0 : nan_state nan (pos_nan, neg_nan);
255 : :
256 : 0 : if (kind == VR_NAN)
257 : 0 : r.set_nan (type, nan);
258 : : else
259 : : {
260 : 0 : REAL_VALUE_TYPE lb, ub;
261 : 0 : streamer_read_real_value (ib, &lb);
262 : 0 : streamer_read_real_value (ib, &ub);
263 : 0 : r.set (type, lb, ub, nan);
264 : : }
265 : 0 : return;
266 : : }
267 : 191798 : if (is_a <prange> (vr))
268 : : {
269 : 191798 : prange &r = as_a <prange> (vr);
270 : 191798 : wide_int lb = streamer_read_wide_int (ib);
271 : 191798 : wide_int ub = streamer_read_wide_int (ib);
272 : 191798 : r.set (type, lb, ub);
273 : 191798 : wide_int value = streamer_read_wide_int (ib);
274 : 191798 : wide_int mask = streamer_read_wide_int (ib);
275 : 191798 : irange_bitmask bm (value, mask);
276 : 191798 : r.update_bitmask (bm);
277 : 191798 : return;
278 : 191798 : }
279 : 0 : gcc_unreachable ();
280 : : }
281 : :
282 : : /* Read the physical representation of a wide_int val from
283 : : input block IB. */
284 : :
285 : : wide_int
286 : 1658866 : streamer_read_wide_int (class lto_input_block *ib)
287 : : {
288 : 1658866 : HOST_WIDE_INT abuf[WIDE_INT_MAX_INL_ELTS], *a = abuf;
289 : 1658866 : int i;
290 : 1658866 : int prec = streamer_read_uhwi (ib);
291 : 1658866 : int len = streamer_read_uhwi (ib);
292 : 1658866 : if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS))
293 : 0 : a = XALLOCAVEC (HOST_WIDE_INT, len);
294 : 3324853 : for (i = 0; i < len; i++)
295 : 1665987 : a[i] = streamer_read_hwi (ib);
296 : 1658866 : return wide_int::from_array (a, len, prec);
297 : : }
298 : :
299 : : /* Read the physical representation of a widest_int val from
300 : : input block IB. */
301 : :
302 : : widest_int
303 : 90862 : streamer_read_widest_int (class lto_input_block *ib)
304 : : {
305 : 90862 : HOST_WIDE_INT abuf[WIDE_INT_MAX_INL_ELTS], *a = abuf;
306 : 90862 : int i;
307 : 90862 : int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
308 : 90862 : int len = streamer_read_uhwi (ib);
309 : 90862 : if (UNLIKELY (len > WIDE_INT_MAX_INL_ELTS))
310 : 0 : a = XALLOCAVEC (HOST_WIDE_INT, len);
311 : 182744 : for (i = 0; i < len; i++)
312 : 91882 : a[i] = streamer_read_hwi (ib);
313 : 90862 : return widest_int::from_array (a, len);
314 : : }
315 : :
|