lto-section-in.cc revision 1.1 1 1.1 mrg /* Input functions for reading LTO sections.
2 1.1 mrg
3 1.1 mrg Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 1.1 mrg Contributed by Kenneth Zadeck <zadeck (at) naturalbridge.com>
5 1.1 mrg
6 1.1 mrg This file is part of GCC.
7 1.1 mrg
8 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
9 1.1 mrg the terms of the GNU General Public License as published by the Free
10 1.1 mrg Software Foundation; either version 3, or (at your option) any later
11 1.1 mrg version.
12 1.1 mrg
13 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 1.1 mrg for more details.
17 1.1 mrg
18 1.1 mrg You should have received a copy of the GNU General Public License
19 1.1 mrg along with GCC; see the file COPYING3. If not see
20 1.1 mrg <http://www.gnu.org/licenses/>. */
21 1.1 mrg
22 1.1 mrg #include "config.h"
23 1.1 mrg #include "system.h"
24 1.1 mrg #include "coretypes.h"
25 1.1 mrg #include "backend.h"
26 1.1 mrg #include "rtl.h"
27 1.1 mrg #include "tree.h"
28 1.1 mrg #include "gimple.h"
29 1.1 mrg #include "cgraph.h"
30 1.1 mrg #include "lto-streamer.h"
31 1.1 mrg #include "lto-compress.h"
32 1.1 mrg
33 1.1 mrg /* Section names. These must correspond to the values of
34 1.1 mrg enum lto_section_type. */
35 1.1 mrg const char *lto_section_name[LTO_N_SECTION_TYPES] =
36 1.1 mrg {
37 1.1 mrg "decls",
38 1.1 mrg "function_body",
39 1.1 mrg "statics",
40 1.1 mrg "symtab",
41 1.1 mrg "ext_symtab",
42 1.1 mrg "refs",
43 1.1 mrg "asm",
44 1.1 mrg "jmpfuncs",
45 1.1 mrg "pureconst",
46 1.1 mrg "reference",
47 1.1 mrg "profile",
48 1.1 mrg "symbol_nodes",
49 1.1 mrg "opts",
50 1.1 mrg "cgraphopt",
51 1.1 mrg "inline",
52 1.1 mrg "ipcp_trans",
53 1.1 mrg "icf",
54 1.1 mrg "offload_table",
55 1.1 mrg "mode_table",
56 1.1 mrg "lto",
57 1.1 mrg "ipa_sra",
58 1.1 mrg "odr_types",
59 1.1 mrg "ipa_modref",
60 1.1 mrg };
61 1.1 mrg
62 1.1 mrg /* Hooks so that the ipa passes can call into the lto front end to get
63 1.1 mrg sections. */
64 1.1 mrg
65 1.1 mrg static struct lto_file_decl_data ** file_decl_data;
66 1.1 mrg static lto_get_section_data_f* get_section_f;
67 1.1 mrg static lto_free_section_data_f* free_section_f;
68 1.1 mrg
69 1.1 mrg
70 1.1 mrg /* This is called from the lto front end to set up the hooks that are
71 1.1 mrg used by the ipa passes to get the data that they will
72 1.1 mrg deserialize. */
73 1.1 mrg
74 1.1 mrg void
75 1.1 mrg lto_set_in_hooks (struct lto_file_decl_data ** data,
76 1.1 mrg lto_get_section_data_f* get_f,
77 1.1 mrg lto_free_section_data_f* free_f)
78 1.1 mrg {
79 1.1 mrg file_decl_data = data;
80 1.1 mrg get_section_f = get_f;
81 1.1 mrg free_section_f = free_f;
82 1.1 mrg }
83 1.1 mrg
84 1.1 mrg
85 1.1 mrg /* Return an array of file decl datas for all of the files passed to
86 1.1 mrg this compilation. */
87 1.1 mrg
88 1.1 mrg struct lto_file_decl_data **
89 1.1 mrg lto_get_file_decl_data (void)
90 1.1 mrg {
91 1.1 mrg gcc_assert (file_decl_data);
92 1.1 mrg return file_decl_data;
93 1.1 mrg }
94 1.1 mrg
95 1.1 mrg /* Buffer structure for accumulating data from compression callbacks. */
96 1.1 mrg
97 1.1 mrg struct lto_buffer
98 1.1 mrg {
99 1.1 mrg char *data;
100 1.1 mrg size_t length;
101 1.1 mrg };
102 1.1 mrg
103 1.1 mrg /* Compression callback, append LENGTH bytes from DATA to the buffer pointed
104 1.1 mrg to by OPAQUE. */
105 1.1 mrg
106 1.1 mrg static void
107 1.1 mrg lto_append_data (const char *data, unsigned length, void *opaque)
108 1.1 mrg {
109 1.1 mrg struct lto_buffer *buffer = (struct lto_buffer *) opaque;
110 1.1 mrg
111 1.1 mrg buffer->data = (char *) xrealloc (buffer->data, buffer->length + length);
112 1.1 mrg memcpy (buffer->data + buffer->length, data, length);
113 1.1 mrg buffer->length += length;
114 1.1 mrg }
115 1.1 mrg
116 1.1 mrg /* Header placed in returned uncompressed data streams. Allows the
117 1.1 mrg uncompressed allocated data to be mapped back to the underlying
118 1.1 mrg compressed data for use with free_section_f. */
119 1.1 mrg
120 1.1 mrg struct lto_data_header
121 1.1 mrg {
122 1.1 mrg const char *data;
123 1.1 mrg size_t len;
124 1.1 mrg };
125 1.1 mrg
126 1.1 mrg /* Return a char pointer to the start of a data stream for an LTO pass
127 1.1 mrg or function. FILE_DATA indicates where to obtain the data.
128 1.1 mrg SECTION_TYPE is the type of information to be obtained. NAME is
129 1.1 mrg the name of the function and is only used when finding a function
130 1.1 mrg body; otherwise it is NULL. LEN is the size of the data
131 1.1 mrg returned. */
132 1.1 mrg
133 1.1 mrg const char *
134 1.1 mrg lto_get_section_data (struct lto_file_decl_data *file_data,
135 1.1 mrg enum lto_section_type section_type,
136 1.1 mrg const char *name, int order,
137 1.1 mrg size_t *len, bool decompress)
138 1.1 mrg {
139 1.1 mrg const char *data = (get_section_f) (file_data, section_type, name, order,
140 1.1 mrg len);
141 1.1 mrg const size_t header_length = sizeof (struct lto_data_header);
142 1.1 mrg struct lto_data_header *header;
143 1.1 mrg struct lto_buffer buffer;
144 1.1 mrg struct lto_compression_stream *stream;
145 1.1 mrg lto_stats.section_size[section_type] += *len;
146 1.1 mrg
147 1.1 mrg if (data == NULL)
148 1.1 mrg return NULL;
149 1.1 mrg
150 1.1 mrg /* WPA->ltrans streams are not compressed with exception of function bodies
151 1.1 mrg and variable initializers that has been verbatim copied from earlier
152 1.1 mrg compilations. */
153 1.1 mrg if ((!flag_ltrans || decompress) && section_type != LTO_section_lto)
154 1.1 mrg {
155 1.1 mrg /* Create a mapping header containing the underlying data and length,
156 1.1 mrg and prepend this to the uncompression buffer. The uncompressed data
157 1.1 mrg then follows, and a pointer to the start of the uncompressed data is
158 1.1 mrg returned. */
159 1.1 mrg header = (struct lto_data_header *) xmalloc (header_length);
160 1.1 mrg header->data = data;
161 1.1 mrg header->len = *len;
162 1.1 mrg
163 1.1 mrg buffer.data = (char *) header;
164 1.1 mrg buffer.length = header_length;
165 1.1 mrg
166 1.1 mrg stream = lto_start_uncompression (lto_append_data, &buffer);
167 1.1 mrg lto_uncompress_block (stream, data, *len);
168 1.1 mrg lto_end_uncompression (stream,
169 1.1 mrg file_data->lto_section_header.get_compression ());
170 1.1 mrg
171 1.1 mrg *len = buffer.length - header_length;
172 1.1 mrg data = buffer.data + header_length;
173 1.1 mrg }
174 1.1 mrg
175 1.1 mrg return data;
176 1.1 mrg }
177 1.1 mrg
178 1.1 mrg /* Return a char pointer to the start of a data stream for an LTO pass.
179 1.1 mrg FILE_DATA indicates where to obtain the data.
180 1.1 mrg SECTION_TYPE is the type of information to be obtained.
181 1.1 mrg LEN is the size of the data returned. */
182 1.1 mrg
183 1.1 mrg const char *
184 1.1 mrg lto_get_summary_section_data (struct lto_file_decl_data *file_data,
185 1.1 mrg enum lto_section_type section_type, size_t *len)
186 1.1 mrg {
187 1.1 mrg return lto_get_section_data (file_data, section_type, NULL, 0, len);
188 1.1 mrg }
189 1.1 mrg
190 1.1 mrg /* Get the section data without any header parsing or uncompression. */
191 1.1 mrg
192 1.1 mrg const char *
193 1.1 mrg lto_get_raw_section_data (struct lto_file_decl_data *file_data,
194 1.1 mrg enum lto_section_type section_type,
195 1.1 mrg const char *name, int order,
196 1.1 mrg size_t *len)
197 1.1 mrg {
198 1.1 mrg return (get_section_f) (file_data, section_type, name, order, len);
199 1.1 mrg }
200 1.1 mrg
201 1.1 mrg /* Free the data found from the above call. The first three
202 1.1 mrg parameters are the same as above. DATA is the data to be freed and
203 1.1 mrg LEN is the length of that data. */
204 1.1 mrg
205 1.1 mrg void
206 1.1 mrg lto_free_section_data (struct lto_file_decl_data *file_data,
207 1.1 mrg enum lto_section_type section_type,
208 1.1 mrg const char *name,
209 1.1 mrg const char *data,
210 1.1 mrg size_t len, bool decompress)
211 1.1 mrg {
212 1.1 mrg const size_t header_length = sizeof (struct lto_data_header);
213 1.1 mrg const char *real_data = data - header_length;
214 1.1 mrg const struct lto_data_header *header
215 1.1 mrg = (const struct lto_data_header *) real_data;
216 1.1 mrg
217 1.1 mrg gcc_assert (free_section_f);
218 1.1 mrg
219 1.1 mrg if (flag_ltrans && !decompress)
220 1.1 mrg {
221 1.1 mrg (free_section_f) (file_data, section_type, name, data, len);
222 1.1 mrg return;
223 1.1 mrg }
224 1.1 mrg
225 1.1 mrg /* The underlying data address has been extracted from the mapping header.
226 1.1 mrg Free that, then free the allocated uncompression buffer. */
227 1.1 mrg (free_section_f) (file_data, section_type, name, header->data, header->len);
228 1.1 mrg free (CONST_CAST (char *, real_data));
229 1.1 mrg }
230 1.1 mrg
231 1.1 mrg /* Free data allocated by lto_get_raw_section_data. */
232 1.1 mrg
233 1.1 mrg void
234 1.1 mrg lto_free_raw_section_data (struct lto_file_decl_data *file_data,
235 1.1 mrg enum lto_section_type section_type,
236 1.1 mrg const char *name,
237 1.1 mrg const char *data,
238 1.1 mrg size_t len)
239 1.1 mrg {
240 1.1 mrg (free_section_f) (file_data, section_type, name, data, len);
241 1.1 mrg }
242 1.1 mrg
243 1.1 mrg /* Load a section of type SECTION_TYPE from FILE_DATA, parse the
244 1.1 mrg header and then return an input block pointing to the section. The
245 1.1 mrg raw pointer to the section is returned in DATAR and LEN. These are
246 1.1 mrg used to free the section. Return NULL if the section is not present. */
247 1.1 mrg
248 1.1 mrg class lto_input_block *
249 1.1 mrg lto_create_simple_input_block (struct lto_file_decl_data *file_data,
250 1.1 mrg enum lto_section_type section_type,
251 1.1 mrg const char **datar, size_t *len)
252 1.1 mrg {
253 1.1 mrg const char *data = lto_get_section_data (file_data, section_type, NULL, 0,
254 1.1 mrg len);
255 1.1 mrg const struct lto_simple_header * header
256 1.1 mrg = (const struct lto_simple_header *) data;
257 1.1 mrg
258 1.1 mrg int main_offset = sizeof (struct lto_simple_header);
259 1.1 mrg
260 1.1 mrg if (!data)
261 1.1 mrg return NULL;
262 1.1 mrg
263 1.1 mrg *datar = data;
264 1.1 mrg return new lto_input_block (data + main_offset, header->main_size,
265 1.1 mrg file_data->mode_table);
266 1.1 mrg }
267 1.1 mrg
268 1.1 mrg
269 1.1 mrg /* Close the section returned from a call to
270 1.1 mrg LTO_CREATE_SIMPLE_INPUT_BLOCK. IB is the input block returned from
271 1.1 mrg that call. The FILE_DATA and SECTION_TYPE are the same as what was
272 1.1 mrg passed to that call and the DATA and LEN are what was returned from
273 1.1 mrg that call. */
274 1.1 mrg
275 1.1 mrg void
276 1.1 mrg lto_destroy_simple_input_block (struct lto_file_decl_data *file_data,
277 1.1 mrg enum lto_section_type section_type,
278 1.1 mrg class lto_input_block *ib,
279 1.1 mrg const char *data, size_t len)
280 1.1 mrg {
281 1.1 mrg delete ib;
282 1.1 mrg lto_free_section_data (file_data, section_type, NULL, data, len);
283 1.1 mrg }
284 1.1 mrg
285 1.1 mrg /*****************************************************************************/
286 1.1 mrg /* Record renamings of static declarations */
287 1.1 mrg /*****************************************************************************/
288 1.1 mrg
289 1.1 mrg struct lto_renaming_slot
290 1.1 mrg {
291 1.1 mrg const char *old_name;
292 1.1 mrg const char *new_name;
293 1.1 mrg };
294 1.1 mrg
295 1.1 mrg /* Returns a hash code for P. */
296 1.1 mrg
297 1.1 mrg static hashval_t
298 1.1 mrg hash_name (const void *p)
299 1.1 mrg {
300 1.1 mrg const struct lto_renaming_slot *ds = (const struct lto_renaming_slot *) p;
301 1.1 mrg return (hashval_t) htab_hash_string (ds->new_name);
302 1.1 mrg }
303 1.1 mrg
304 1.1 mrg /* Returns nonzero if P1 and P2 are equal. */
305 1.1 mrg
306 1.1 mrg static int
307 1.1 mrg eq_name (const void *p1, const void *p2)
308 1.1 mrg {
309 1.1 mrg const struct lto_renaming_slot *s1 =
310 1.1 mrg (const struct lto_renaming_slot *) p1;
311 1.1 mrg const struct lto_renaming_slot *s2 =
312 1.1 mrg (const struct lto_renaming_slot *) p2;
313 1.1 mrg
314 1.1 mrg return strcmp (s1->new_name, s2->new_name) == 0;
315 1.1 mrg }
316 1.1 mrg
317 1.1 mrg /* Free a renaming table entry. */
318 1.1 mrg
319 1.1 mrg static void
320 1.1 mrg renaming_slot_free (void *slot)
321 1.1 mrg {
322 1.1 mrg struct lto_renaming_slot *s = (struct lto_renaming_slot *) slot;
323 1.1 mrg
324 1.1 mrg free (CONST_CAST (void *, (const void *) s->old_name));
325 1.1 mrg free (CONST_CAST (void *, (const void *) s->new_name));
326 1.1 mrg free ((void *) s);
327 1.1 mrg }
328 1.1 mrg
329 1.1 mrg /* Create an empty hash table for recording declaration renamings. */
330 1.1 mrg
331 1.1 mrg htab_t
332 1.1 mrg lto_create_renaming_table (void)
333 1.1 mrg {
334 1.1 mrg return htab_create (37, hash_name, eq_name, renaming_slot_free);
335 1.1 mrg }
336 1.1 mrg
337 1.1 mrg /* Record a declaration name mapping OLD_NAME -> NEW_NAME. DECL_DATA
338 1.1 mrg holds the renaming hash table to use. */
339 1.1 mrg
340 1.1 mrg void
341 1.1 mrg lto_record_renamed_decl (struct lto_file_decl_data *decl_data,
342 1.1 mrg const char *old_name, const char *new_name)
343 1.1 mrg {
344 1.1 mrg void **slot;
345 1.1 mrg struct lto_renaming_slot r_slot;
346 1.1 mrg
347 1.1 mrg r_slot.new_name = new_name;
348 1.1 mrg slot = htab_find_slot (decl_data->renaming_hash_table, &r_slot, INSERT);
349 1.1 mrg if (*slot == NULL)
350 1.1 mrg {
351 1.1 mrg struct lto_renaming_slot *new_slot = XNEW (struct lto_renaming_slot);
352 1.1 mrg new_slot->old_name = xstrdup (old_name);
353 1.1 mrg new_slot->new_name = xstrdup (new_name);
354 1.1 mrg *slot = new_slot;
355 1.1 mrg }
356 1.1 mrg else
357 1.1 mrg gcc_unreachable ();
358 1.1 mrg }
359 1.1 mrg
360 1.1 mrg
361 1.1 mrg /* Given a string NAME, return the string that it has been mapped to
362 1.1 mrg by lto_record_renamed_decl. If NAME was not renamed, it is
363 1.1 mrg returned unchanged. DECL_DATA holds the renaming hash table to use. */
364 1.1 mrg
365 1.1 mrg const char *
366 1.1 mrg lto_get_decl_name_mapping (struct lto_file_decl_data *decl_data,
367 1.1 mrg const char *name)
368 1.1 mrg {
369 1.1 mrg htab_t renaming_hash_table = decl_data->renaming_hash_table;
370 1.1 mrg struct lto_renaming_slot *slot;
371 1.1 mrg struct lto_renaming_slot r_slot;
372 1.1 mrg
373 1.1 mrg r_slot.new_name = name;
374 1.1 mrg slot = (struct lto_renaming_slot *) htab_find (renaming_hash_table, &r_slot);
375 1.1 mrg if (slot)
376 1.1 mrg return slot->old_name;
377 1.1 mrg else
378 1.1 mrg return name;
379 1.1 mrg }
380 1.1 mrg
381 1.1 mrg /*****************************************************************************/
382 1.1 mrg /* Input decl state object. */
383 1.1 mrg /*****************************************************************************/
384 1.1 mrg
385 1.1 mrg /* Return a newly created in-decl state object. */
386 1.1 mrg
387 1.1 mrg struct lto_in_decl_state *
388 1.1 mrg lto_new_in_decl_state (void)
389 1.1 mrg {
390 1.1 mrg return ggc_cleared_alloc<lto_in_decl_state> ();
391 1.1 mrg }
392 1.1 mrg
393 1.1 mrg /* Delete STATE and its components. */
394 1.1 mrg
395 1.1 mrg void
396 1.1 mrg lto_delete_in_decl_state (struct lto_in_decl_state *state)
397 1.1 mrg {
398 1.1 mrg int i;
399 1.1 mrg
400 1.1 mrg for (i = 0; i < LTO_N_DECL_STREAMS; i++)
401 1.1 mrg vec_free (state->streams[i]);
402 1.1 mrg ggc_free (state);
403 1.1 mrg }
404 1.1 mrg
405 1.1 mrg /* Search the in-decl state of a function FUNC contained in the file
406 1.1 mrg associated with FILE_DATA. Return NULL if not found. */
407 1.1 mrg
408 1.1 mrg struct lto_in_decl_state*
409 1.1 mrg lto_get_function_in_decl_state (struct lto_file_decl_data *file_data,
410 1.1 mrg tree func)
411 1.1 mrg {
412 1.1 mrg struct lto_in_decl_state temp;
413 1.1 mrg lto_in_decl_state **slot;
414 1.1 mrg
415 1.1 mrg temp.fn_decl = func;
416 1.1 mrg slot = file_data->function_decl_states->find_slot (&temp, NO_INSERT);
417 1.1 mrg return slot? *slot : NULL;
418 1.1 mrg }
419 1.1 mrg
420 1.1 mrg /* Free decl_states. */
421 1.1 mrg
422 1.1 mrg void
423 1.1 mrg lto_free_function_in_decl_state (struct lto_in_decl_state *state)
424 1.1 mrg {
425 1.1 mrg int i;
426 1.1 mrg for (i = 0; i < LTO_N_DECL_STREAMS; i++)
427 1.1 mrg vec_free (state->streams[i]);
428 1.1 mrg ggc_free (state);
429 1.1 mrg }
430 1.1 mrg
431 1.1 mrg /* Free decl_states associated with NODE. This makes it possible to furhter
432 1.1 mrg release trees needed by the NODE's body. */
433 1.1 mrg
434 1.1 mrg void
435 1.1 mrg lto_free_function_in_decl_state_for_node (symtab_node *node)
436 1.1 mrg {
437 1.1 mrg struct lto_in_decl_state temp;
438 1.1 mrg lto_in_decl_state **slot;
439 1.1 mrg
440 1.1 mrg if (!node->lto_file_data)
441 1.1 mrg return;
442 1.1 mrg
443 1.1 mrg temp.fn_decl = node->decl;
444 1.1 mrg slot
445 1.1 mrg = node->lto_file_data->function_decl_states->find_slot (&temp, NO_INSERT);
446 1.1 mrg if (slot && *slot)
447 1.1 mrg {
448 1.1 mrg lto_free_function_in_decl_state (*slot);
449 1.1 mrg node->lto_file_data->function_decl_states->clear_slot (slot);
450 1.1 mrg }
451 1.1 mrg node->lto_file_data = NULL;
452 1.1 mrg }
453 1.1 mrg
454 1.1 mrg
455 1.1 mrg /* Report read pass end of the section. */
456 1.1 mrg
457 1.1 mrg void
458 1.1 mrg lto_section_overrun (class lto_input_block *ib)
459 1.1 mrg {
460 1.1 mrg fatal_error (input_location, "bytecode stream: trying to read %d bytes "
461 1.1 mrg "after the end of the input buffer", ib->p - ib->len);
462 1.1 mrg }
463 1.1 mrg
464 1.1 mrg /* Report out of range value. */
465 1.1 mrg
466 1.1 mrg void
467 1.1 mrg lto_value_range_error (const char *purpose, HOST_WIDE_INT val,
468 1.1 mrg HOST_WIDE_INT min, HOST_WIDE_INT max)
469 1.1 mrg {
470 1.1 mrg fatal_error (input_location,
471 1.1 mrg "%s out of range: Range is %i to %i, value is %i",
472 1.1 mrg purpose, (int)min, (int)max, (int)val);
473 1.1 mrg }
474