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