PR c++/66260
[official-gcc.git] / gcc / lto-section-in.c
blob58560a85d493097dcc17b425db27683428853ad9
1 /* Input functions for reading LTO sections.
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "rtl.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "internal-fn.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "calls.h"
38 #include "emit-rtl.h"
39 #include "varasm.h"
40 #include "stmt.h"
41 #include "expr.h"
42 #include "params.h"
43 #include "diagnostic-core.h"
44 #include "except.h"
45 #include "timevar.h"
46 #include "cgraph.h"
47 #include "lto-streamer.h"
48 #include "lto-compress.h"
50 /* Section names. These must correspond to the values of
51 enum lto_section_type. */
52 const char *lto_section_name[LTO_N_SECTION_TYPES] =
54 "decls",
55 "function_body",
56 "statics",
57 "symtab",
58 "refs",
59 "asm",
60 "jmpfuncs",
61 "pureconst",
62 "reference",
63 "profile",
64 "symbol_nodes",
65 "opts",
66 "cgraphopt",
67 "inline",
68 "ipcp_trans",
69 "icf",
70 "offload_table",
71 "mode_table"
75 /* Hooks so that the ipa passes can call into the lto front end to get
76 sections. */
78 static struct lto_file_decl_data ** file_decl_data;
79 static lto_get_section_data_f* get_section_f;
80 static lto_free_section_data_f* free_section_f;
83 /* This is called from the lto front end to set up the hooks that are
84 used by the ipa passes to get the data that they will
85 deserialize. */
87 void
88 lto_set_in_hooks (struct lto_file_decl_data ** data,
89 lto_get_section_data_f* get_f,
90 lto_free_section_data_f* free_f)
92 file_decl_data = data;
93 get_section_f = get_f;
94 free_section_f = free_f;
98 /* Return an array of file decl datas for all of the files passed to
99 this compilation. */
101 struct lto_file_decl_data **
102 lto_get_file_decl_data (void)
104 gcc_assert (file_decl_data);
105 return file_decl_data;
108 /* Buffer structure for accumulating data from compression callbacks. */
110 struct lto_buffer
112 char *data;
113 size_t length;
116 /* Compression callback, append LENGTH bytes from DATA to the buffer pointed
117 to by OPAQUE. */
119 static void
120 lto_append_data (const char *data, unsigned length, void *opaque)
122 struct lto_buffer *buffer = (struct lto_buffer *) opaque;
124 buffer->data = (char *) xrealloc (buffer->data, buffer->length + length);
125 memcpy (buffer->data + buffer->length, data, length);
126 buffer->length += length;
129 /* Header placed in returned uncompressed data streams. Allows the
130 uncompressed allocated data to be mapped back to the underlying
131 compressed data for use with free_section_f. */
133 struct lto_data_header
135 const char *data;
136 size_t len;
139 /* Return a char pointer to the start of a data stream for an LTO pass
140 or function. FILE_DATA indicates where to obtain the data.
141 SECTION_TYPE is the type of information to be obtained. NAME is
142 the name of the function and is only used when finding a function
143 body; otherwise it is NULL. LEN is the size of the data
144 returned. */
146 const char *
147 lto_get_section_data (struct lto_file_decl_data *file_data,
148 enum lto_section_type section_type,
149 const char *name,
150 size_t *len)
152 const char *data = (get_section_f) (file_data, section_type, name, len);
153 const size_t header_length = sizeof (struct lto_data_header);
154 struct lto_data_header *header;
155 struct lto_buffer buffer;
156 struct lto_compression_stream *stream;
157 lto_stats.section_size[section_type] += *len;
159 if (data == NULL)
160 return NULL;
162 /* FIXME lto: WPA mode does not write compressed sections, so for now
163 suppress uncompression if flag_ltrans. */
164 if (!flag_ltrans)
166 /* Create a mapping header containing the underlying data and length,
167 and prepend this to the uncompression buffer. The uncompressed data
168 then follows, and a pointer to the start of the uncompressed data is
169 returned. */
170 header = (struct lto_data_header *) xmalloc (header_length);
171 header->data = data;
172 header->len = *len;
174 buffer.data = (char *) header;
175 buffer.length = header_length;
177 stream = lto_start_uncompression (lto_append_data, &buffer);
178 lto_uncompress_block (stream, data, *len);
179 lto_end_uncompression (stream);
181 *len = buffer.length - header_length;
182 data = buffer.data + header_length;
185 lto_check_version (((const lto_header *)data)->major_version,
186 ((const lto_header *)data)->minor_version);
187 return data;
191 /* Free the data found from the above call. The first three
192 parameters are the same as above. DATA is the data to be freed and
193 LEN is the length of that data. */
195 void
196 lto_free_section_data (struct lto_file_decl_data *file_data,
197 enum lto_section_type section_type,
198 const char *name,
199 const char *data,
200 size_t len)
202 const size_t header_length = sizeof (struct lto_data_header);
203 const char *real_data = data - header_length;
204 const struct lto_data_header *header
205 = (const struct lto_data_header *) real_data;
207 gcc_assert (free_section_f);
209 /* FIXME lto: WPA mode does not write compressed sections, so for now
210 suppress uncompression mapping if flag_ltrans. */
211 if (flag_ltrans)
213 (free_section_f) (file_data, section_type, name, data, len);
214 return;
217 /* The underlying data address has been extracted from the mapping header.
218 Free that, then free the allocated uncompression buffer. */
219 (free_section_f) (file_data, section_type, name, header->data, header->len);
220 free (CONST_CAST (char *, real_data));
224 /* Load a section of type SECTION_TYPE from FILE_DATA, parse the
225 header and then return an input block pointing to the section. The
226 raw pointer to the section is returned in DATAR and LEN. These are
227 used to free the section. Return NULL if the section is not present. */
229 struct lto_input_block *
230 lto_create_simple_input_block (struct lto_file_decl_data *file_data,
231 enum lto_section_type section_type,
232 const char **datar, size_t *len)
234 const char *data = lto_get_section_data (file_data, section_type, NULL, len);
235 const struct lto_simple_header * header
236 = (const struct lto_simple_header *) data;
238 int main_offset = sizeof (struct lto_simple_header);
240 if (!data)
241 return NULL;
243 *datar = data;
244 return new lto_input_block (data + main_offset, header->main_size,
245 file_data->mode_table);
249 /* Close the section returned from a call to
250 LTO_CREATE_SIMPLE_INPUT_BLOCK. IB is the input block returned from
251 that call. The FILE_DATA and SECTION_TYPE are the same as what was
252 passed to that call and the DATA and LEN are what was returned from
253 that call. */
255 void
256 lto_destroy_simple_input_block (struct lto_file_decl_data *file_data,
257 enum lto_section_type section_type,
258 struct lto_input_block *ib,
259 const char *data, size_t len)
261 delete ib;
262 lto_free_section_data (file_data, section_type, NULL, data, len);
265 /*****************************************************************************/
266 /* Record renamings of static declarations */
267 /*****************************************************************************/
269 struct lto_renaming_slot
271 const char *old_name;
272 const char *new_name;
275 /* Returns a hash code for P. */
277 static hashval_t
278 hash_name (const void *p)
280 const struct lto_renaming_slot *ds = (const struct lto_renaming_slot *) p;
281 return (hashval_t) htab_hash_string (ds->new_name);
284 /* Returns nonzero if P1 and P2 are equal. */
286 static int
287 eq_name (const void *p1, const void *p2)
289 const struct lto_renaming_slot *s1 =
290 (const struct lto_renaming_slot *) p1;
291 const struct lto_renaming_slot *s2 =
292 (const struct lto_renaming_slot *) p2;
294 return strcmp (s1->new_name, s2->new_name) == 0;
297 /* Free a renaming table entry. */
299 static void
300 renaming_slot_free (void *slot)
302 struct lto_renaming_slot *s = (struct lto_renaming_slot *) slot;
304 free (CONST_CAST (void *, (const void *) s->old_name));
305 free (CONST_CAST (void *, (const void *) s->new_name));
306 free ((void *) s);
309 /* Create an empty hash table for recording declaration renamings. */
311 htab_t
312 lto_create_renaming_table (void)
314 return htab_create (37, hash_name, eq_name, renaming_slot_free);
317 /* Record a declaration name mapping OLD_NAME -> NEW_NAME. DECL_DATA
318 holds the renaming hash table to use. */
320 void
321 lto_record_renamed_decl (struct lto_file_decl_data *decl_data,
322 const char *old_name, const char *new_name)
324 void **slot;
325 struct lto_renaming_slot r_slot;
327 r_slot.new_name = new_name;
328 slot = htab_find_slot (decl_data->renaming_hash_table, &r_slot, INSERT);
329 if (*slot == NULL)
331 struct lto_renaming_slot *new_slot = XNEW (struct lto_renaming_slot);
332 new_slot->old_name = xstrdup (old_name);
333 new_slot->new_name = xstrdup (new_name);
334 *slot = new_slot;
336 else
337 gcc_unreachable ();
341 /* Given a string NAME, return the string that it has been mapped to
342 by lto_record_renamed_decl. If NAME was not renamed, it is
343 returned unchanged. DECL_DATA holds the renaming hash table to use. */
345 const char *
346 lto_get_decl_name_mapping (struct lto_file_decl_data *decl_data,
347 const char *name)
349 htab_t renaming_hash_table = decl_data->renaming_hash_table;
350 struct lto_renaming_slot *slot;
351 struct lto_renaming_slot r_slot;
353 r_slot.new_name = name;
354 slot = (struct lto_renaming_slot *) htab_find (renaming_hash_table, &r_slot);
355 if (slot)
356 return slot->old_name;
357 else
358 return name;
361 /*****************************************************************************/
362 /* Input decl state object. */
363 /*****************************************************************************/
365 /* Return a newly created in-decl state object. */
367 struct lto_in_decl_state *
368 lto_new_in_decl_state (void)
370 return ggc_cleared_alloc<lto_in_decl_state> ();
373 /* Delete STATE and its components. */
375 void
376 lto_delete_in_decl_state (struct lto_in_decl_state *state)
378 int i;
380 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
381 vec_free (state->streams[i]);
382 ggc_free (state);
385 /* Search the in-decl state of a function FUNC contained in the file
386 associated with FILE_DATA. Return NULL if not found. */
388 struct lto_in_decl_state*
389 lto_get_function_in_decl_state (struct lto_file_decl_data *file_data,
390 tree func)
392 struct lto_in_decl_state temp;
393 lto_in_decl_state **slot;
395 temp.fn_decl = func;
396 slot = file_data->function_decl_states->find_slot (&temp, NO_INSERT);
397 return slot? *slot : NULL;
400 /* Free decl_states. */
402 void
403 lto_free_function_in_decl_state (struct lto_in_decl_state *state)
405 int i;
406 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
407 vec_free (state->streams[i]);
408 ggc_free (state);
411 /* Free decl_states associated with NODE. This makes it possible to furhter
412 release trees needed by the NODE's body. */
414 void
415 lto_free_function_in_decl_state_for_node (symtab_node *node)
417 struct lto_in_decl_state temp;
418 lto_in_decl_state **slot;
420 if (!node->lto_file_data)
421 return;
423 temp.fn_decl = node->decl;
424 slot
425 = node->lto_file_data->function_decl_states->find_slot (&temp, NO_INSERT);
426 if (slot && *slot)
428 lto_free_function_in_decl_state (*slot);
429 node->lto_file_data->function_decl_states->clear_slot (slot);
431 node->lto_file_data = NULL;
435 /* Report read pass end of the section. */
437 void
438 lto_section_overrun (struct lto_input_block *ib)
440 fatal_error (input_location, "bytecode stream: trying to read %d bytes "
441 "after the end of the input buffer", ib->p - ib->len);
444 /* Report out of range value. */
446 void
447 lto_value_range_error (const char *purpose, HOST_WIDE_INT val,
448 HOST_WIDE_INT min, HOST_WIDE_INT max)
450 fatal_error (input_location,
451 "%s out of range: Range is %i to %i, value is %i",
452 purpose, (int)min, (int)max, (int)val);