* inclhack.def (AAB_aix_fcntl): New fix.
[official-gcc.git] / gcc / lto-streamer-in.c
blob91215216f9dbead2a7e69ebaad19a159e9e46b59
1 /* Read the GIMPLE representation from a file stream.
3 Copyright 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 Re-implemented by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
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.
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.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "tree-pass.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic.h"
41 #include "except.h"
42 #include "debug.h"
43 #include "vec.h"
44 #include "ipa-utils.h"
45 #include "data-streamer.h"
46 #include "gimple-streamer.h"
47 #include "lto-streamer.h"
48 #include "tree-streamer.h"
49 #include "tree-pass.h"
50 #include "streamer-hooks.h"
52 /* The table to hold the file names. */
53 static htab_t file_name_hash_table;
56 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
57 number of valid tag values to check. */
59 void
60 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
62 va_list ap;
63 int i;
65 va_start (ap, ntags);
66 for (i = 0; i < ntags; i++)
67 if ((unsigned) actual == va_arg (ap, unsigned))
69 va_end (ap);
70 return;
73 va_end (ap);
74 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
78 /* Read LENGTH bytes from STREAM to ADDR. */
80 void
81 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
83 size_t i;
84 unsigned char *const buffer = (unsigned char *const) addr;
86 for (i = 0; i < length; i++)
87 buffer[i] = streamer_read_uchar (ib);
91 /* Lookup STRING in file_name_hash_table. If found, return the existing
92 string, otherwise insert STRING as the canonical version. */
94 static const char *
95 canon_file_name (const char *string)
97 void **slot;
98 struct string_slot s_slot;
99 size_t len = strlen (string);
101 s_slot.s = string;
102 s_slot.len = len;
104 slot = htab_find_slot (file_name_hash_table, &s_slot, INSERT);
105 if (*slot == NULL)
107 char *saved_string;
108 struct string_slot *new_slot;
110 saved_string = (char *) xmalloc (len + 1);
111 new_slot = XCNEW (struct string_slot);
112 memcpy (saved_string, string, len + 1);
113 new_slot->s = saved_string;
114 new_slot->len = len;
115 *slot = new_slot;
116 return saved_string;
118 else
120 struct string_slot *old_slot = (struct string_slot *) *slot;
121 return old_slot->s;
126 /* Clear the line info stored in DATA_IN. */
128 static void
129 clear_line_info (struct data_in *data_in)
131 if (data_in->current_file)
132 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
133 data_in->current_file = NULL;
134 data_in->current_line = 0;
135 data_in->current_col = 0;
139 /* Read a location bitpack from input block IB. */
141 static location_t
142 lto_input_location_bitpack (struct data_in *data_in, struct bitpack_d *bp)
144 bool file_change, line_change, column_change;
145 unsigned len;
146 bool prev_file = data_in->current_file != NULL;
148 if (bp_unpack_value (bp, 1))
149 return UNKNOWN_LOCATION;
151 file_change = bp_unpack_value (bp, 1);
152 if (file_change)
153 data_in->current_file = canon_file_name
154 (string_for_index (data_in,
155 bp_unpack_var_len_unsigned (bp),
156 &len));
158 line_change = bp_unpack_value (bp, 1);
159 if (line_change)
160 data_in->current_line = bp_unpack_var_len_unsigned (bp);
162 column_change = bp_unpack_value (bp, 1);
163 if (column_change)
164 data_in->current_col = bp_unpack_var_len_unsigned (bp);
166 if (file_change)
168 if (prev_file)
169 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
171 linemap_add (line_table, LC_ENTER, false, data_in->current_file,
172 data_in->current_line);
174 else if (line_change)
175 linemap_line_start (line_table, data_in->current_line, data_in->current_col);
177 return linemap_position_for_column (line_table, data_in->current_col);
181 /* Read a location from input block IB.
182 If the input_location streamer hook exists, call it.
183 Otherwise, proceed with reading the location from the
184 expanded location bitpack. */
186 location_t
187 lto_input_location (struct lto_input_block *ib, struct data_in *data_in)
189 if (streamer_hooks.input_location)
190 return streamer_hooks.input_location (ib, data_in);
191 else
193 struct bitpack_d bp;
195 bp = streamer_read_bitpack (ib);
196 return lto_input_location_bitpack (data_in, &bp);
201 /* Read a reference to a tree node from DATA_IN using input block IB.
202 TAG is the expected node that should be found in IB, if TAG belongs
203 to one of the indexable trees, expect to read a reference index to
204 be looked up in one of the symbol tables, otherwise read the pysical
205 representation of the tree using stream_read_tree. FN is the
206 function scope for the read tree. */
208 tree
209 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
210 struct function *fn, enum LTO_tags tag)
212 unsigned HOST_WIDE_INT ix_u;
213 tree result = NULL_TREE;
215 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
217 switch (tag)
219 case LTO_type_ref:
220 ix_u = streamer_read_uhwi (ib);
221 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
222 break;
224 case LTO_ssa_name_ref:
225 ix_u = streamer_read_uhwi (ib);
226 result = VEC_index (tree, SSANAMES (fn), ix_u);
227 break;
229 case LTO_field_decl_ref:
230 ix_u = streamer_read_uhwi (ib);
231 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
232 break;
234 case LTO_function_decl_ref:
235 ix_u = streamer_read_uhwi (ib);
236 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
237 break;
239 case LTO_type_decl_ref:
240 ix_u = streamer_read_uhwi (ib);
241 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
242 break;
244 case LTO_namespace_decl_ref:
245 ix_u = streamer_read_uhwi (ib);
246 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
247 break;
249 case LTO_global_decl_ref:
250 case LTO_result_decl_ref:
251 case LTO_const_decl_ref:
252 case LTO_imported_decl_ref:
253 case LTO_label_decl_ref:
254 case LTO_translation_unit_decl_ref:
255 ix_u = streamer_read_uhwi (ib);
256 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
257 break;
259 default:
260 gcc_unreachable ();
263 gcc_assert (result);
265 return result;
269 /* Read and return a double-linked list of catch handlers from input
270 block IB, using descriptors in DATA_IN. */
272 static struct eh_catch_d *
273 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
274 eh_catch *last_p)
276 eh_catch first;
277 enum LTO_tags tag;
279 *last_p = first = NULL;
280 tag = streamer_read_record_start (ib);
281 while (tag)
283 tree list;
284 eh_catch n;
286 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
288 /* Read the catch node. */
289 n = ggc_alloc_cleared_eh_catch_d ();
290 n->type_list = stream_read_tree (ib, data_in);
291 n->filter_list = stream_read_tree (ib, data_in);
292 n->label = stream_read_tree (ib, data_in);
294 /* Register all the types in N->FILTER_LIST. */
295 for (list = n->filter_list; list; list = TREE_CHAIN (list))
296 add_type_for_runtime (TREE_VALUE (list));
298 /* Chain N to the end of the list. */
299 if (*last_p)
300 (*last_p)->next_catch = n;
301 n->prev_catch = *last_p;
302 *last_p = n;
304 /* Set the head of the list the first time through the loop. */
305 if (first == NULL)
306 first = n;
308 tag = streamer_read_record_start (ib);
311 return first;
315 /* Read and return EH region IX from input block IB, using descriptors
316 in DATA_IN. */
318 static eh_region
319 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
321 enum LTO_tags tag;
322 eh_region r;
324 /* Read the region header. */
325 tag = streamer_read_record_start (ib);
326 if (tag == LTO_null)
327 return NULL;
329 r = ggc_alloc_cleared_eh_region_d ();
330 r->index = streamer_read_hwi (ib);
332 gcc_assert (r->index == ix);
334 /* Read all the region pointers as region numbers. We'll fix up
335 the pointers once the whole array has been read. */
336 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
337 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
338 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
340 switch (tag)
342 case LTO_ert_cleanup:
343 r->type = ERT_CLEANUP;
344 break;
346 case LTO_ert_try:
348 struct eh_catch_d *last_catch;
349 r->type = ERT_TRY;
350 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
351 &last_catch);
352 r->u.eh_try.last_catch = last_catch;
353 break;
356 case LTO_ert_allowed_exceptions:
358 tree l;
360 r->type = ERT_ALLOWED_EXCEPTIONS;
361 r->u.allowed.type_list = stream_read_tree (ib, data_in);
362 r->u.allowed.label = stream_read_tree (ib, data_in);
363 r->u.allowed.filter = streamer_read_uhwi (ib);
365 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
366 add_type_for_runtime (TREE_VALUE (l));
368 break;
370 case LTO_ert_must_not_throw:
371 r->type = ERT_MUST_NOT_THROW;
372 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
373 r->u.must_not_throw.failure_loc = lto_input_location (ib, data_in);
374 break;
376 default:
377 gcc_unreachable ();
380 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
382 return r;
386 /* Read and return EH landing pad IX from input block IB, using descriptors
387 in DATA_IN. */
389 static eh_landing_pad
390 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
392 enum LTO_tags tag;
393 eh_landing_pad lp;
395 /* Read the landing pad header. */
396 tag = streamer_read_record_start (ib);
397 if (tag == LTO_null)
398 return NULL;
400 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
402 lp = ggc_alloc_cleared_eh_landing_pad_d ();
403 lp->index = streamer_read_hwi (ib);
404 gcc_assert (lp->index == ix);
405 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
406 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
407 lp->post_landing_pad = stream_read_tree (ib, data_in);
409 return lp;
413 /* After reading the EH regions, pointers to peer and children regions
414 are region numbers. This converts all these region numbers into
415 real pointers into the rematerialized regions for FN. ROOT_REGION
416 is the region number for the root EH region in FN. */
418 static void
419 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
421 unsigned i;
422 VEC(eh_region,gc) *eh_array = fn->eh->region_array;
423 VEC(eh_landing_pad,gc) *lp_array = fn->eh->lp_array;
424 eh_region r;
425 eh_landing_pad lp;
427 gcc_assert (eh_array && lp_array);
429 gcc_assert (root_region >= 0);
430 fn->eh->region_tree = VEC_index (eh_region, eh_array, root_region);
432 #define FIXUP_EH_REGION(r) (r) = VEC_index (eh_region, eh_array, \
433 (HOST_WIDE_INT) (intptr_t) (r))
434 #define FIXUP_EH_LP(p) (p) = VEC_index (eh_landing_pad, lp_array, \
435 (HOST_WIDE_INT) (intptr_t) (p))
437 /* Convert all the index numbers stored in pointer fields into
438 pointers to the corresponding slots in the EH region array. */
439 FOR_EACH_VEC_ELT (eh_region, eh_array, i, r)
441 /* The array may contain NULL regions. */
442 if (r == NULL)
443 continue;
445 gcc_assert (i == (unsigned) r->index);
446 FIXUP_EH_REGION (r->outer);
447 FIXUP_EH_REGION (r->inner);
448 FIXUP_EH_REGION (r->next_peer);
449 FIXUP_EH_LP (r->landing_pads);
452 /* Convert all the index numbers stored in pointer fields into
453 pointers to the corresponding slots in the EH landing pad array. */
454 FOR_EACH_VEC_ELT (eh_landing_pad, lp_array, i, lp)
456 /* The array may contain NULL landing pads. */
457 if (lp == NULL)
458 continue;
460 gcc_assert (i == (unsigned) lp->index);
461 FIXUP_EH_LP (lp->next_lp);
462 FIXUP_EH_REGION (lp->region);
465 #undef FIXUP_EH_REGION
466 #undef FIXUP_EH_LP
470 /* Initialize EH support. */
472 void
473 lto_init_eh (void)
475 static bool eh_initialized_p = false;
477 if (eh_initialized_p)
478 return;
480 /* Contrary to most other FEs, we only initialize EH support when at
481 least one of the files in the set contains exception regions in
482 it. Since this happens much later than the call to init_eh in
483 lang_dependent_init, we have to set flag_exceptions and call
484 init_eh again to initialize the EH tables. */
485 flag_exceptions = 1;
486 init_eh ();
488 eh_initialized_p = true;
492 /* Read the exception table for FN from IB using the data descriptors
493 in DATA_IN. */
495 static void
496 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
497 struct function *fn)
499 HOST_WIDE_INT i, root_region, len;
500 enum LTO_tags tag;
502 tag = streamer_read_record_start (ib);
503 if (tag == LTO_null)
504 return;
506 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
508 /* If the file contains EH regions, then it was compiled with
509 -fexceptions. In that case, initialize the backend EH
510 machinery. */
511 lto_init_eh ();
513 gcc_assert (fn->eh);
515 root_region = streamer_read_hwi (ib);
516 gcc_assert (root_region == (int) root_region);
518 /* Read the EH region array. */
519 len = streamer_read_hwi (ib);
520 gcc_assert (len == (int) len);
521 if (len > 0)
523 VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
524 for (i = 0; i < len; i++)
526 eh_region r = input_eh_region (ib, data_in, i);
527 VEC_replace (eh_region, fn->eh->region_array, i, r);
531 /* Read the landing pads. */
532 len = streamer_read_hwi (ib);
533 gcc_assert (len == (int) len);
534 if (len > 0)
536 VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
537 for (i = 0; i < len; i++)
539 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
540 VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
544 /* Read the runtime type data. */
545 len = streamer_read_hwi (ib);
546 gcc_assert (len == (int) len);
547 if (len > 0)
549 VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
550 for (i = 0; i < len; i++)
552 tree ttype = stream_read_tree (ib, data_in);
553 VEC_replace (tree, fn->eh->ttype_data, i, ttype);
557 /* Read the table of action chains. */
558 len = streamer_read_hwi (ib);
559 gcc_assert (len == (int) len);
560 if (len > 0)
562 if (targetm.arm_eabi_unwinder)
564 VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
565 for (i = 0; i < len; i++)
567 tree t = stream_read_tree (ib, data_in);
568 VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
571 else
573 VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
574 for (i = 0; i < len; i++)
576 uchar c = streamer_read_uchar (ib);
577 VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
582 /* Reconstruct the EH region tree by fixing up the peer/children
583 pointers. */
584 fixup_eh_region_pointers (fn, root_region);
586 tag = streamer_read_record_start (ib);
587 lto_tag_check_range (tag, LTO_null, LTO_null);
591 /* Make a new basic block with index INDEX in function FN. */
593 static basic_block
594 make_new_block (struct function *fn, unsigned int index)
596 basic_block bb = alloc_block ();
597 bb->index = index;
598 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
599 n_basic_blocks_for_function (fn)++;
600 return bb;
604 /* Read the CFG for function FN from input block IB. */
606 static void
607 input_cfg (struct lto_input_block *ib, struct function *fn,
608 int count_materialization_scale)
610 unsigned int bb_count;
611 basic_block p_bb;
612 unsigned int i;
613 int index;
615 init_empty_tree_cfg_for_function (fn);
616 init_ssa_operands (fn);
618 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
619 PROFILE_LAST);
621 bb_count = streamer_read_uhwi (ib);
623 last_basic_block_for_function (fn) = bb_count;
624 if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
625 VEC_safe_grow_cleared (basic_block, gc,
626 basic_block_info_for_function (fn), bb_count);
628 if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
629 VEC_safe_grow_cleared (basic_block, gc,
630 label_to_block_map_for_function (fn), bb_count);
632 index = streamer_read_hwi (ib);
633 while (index != -1)
635 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
636 unsigned int edge_count;
638 if (bb == NULL)
639 bb = make_new_block (fn, index);
641 edge_count = streamer_read_uhwi (ib);
643 /* Connect up the CFG. */
644 for (i = 0; i < edge_count; i++)
646 unsigned int dest_index;
647 unsigned int edge_flags;
648 basic_block dest;
649 int probability;
650 gcov_type count;
651 edge e;
653 dest_index = streamer_read_uhwi (ib);
654 probability = (int) streamer_read_hwi (ib);
655 count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
656 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
657 edge_flags = streamer_read_uhwi (ib);
659 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
661 if (dest == NULL)
662 dest = make_new_block (fn, dest_index);
664 e = make_edge (bb, dest, edge_flags);
665 e->probability = probability;
666 e->count = count;
669 index = streamer_read_hwi (ib);
672 p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
673 index = streamer_read_hwi (ib);
674 while (index != -1)
676 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
677 bb->prev_bb = p_bb;
678 p_bb->next_bb = bb;
679 p_bb = bb;
680 index = streamer_read_hwi (ib);
685 /* Read the SSA names array for function FN from DATA_IN using input
686 block IB. */
688 static void
689 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
690 struct function *fn)
692 unsigned int i, size;
694 size = streamer_read_uhwi (ib);
695 init_ssanames (fn, size);
697 i = streamer_read_uhwi (ib);
698 while (i)
700 tree ssa_name, name;
701 bool is_default_def;
703 /* Skip over the elements that had been freed. */
704 while (VEC_length (tree, SSANAMES (fn)) < i)
705 VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
707 is_default_def = (streamer_read_uchar (ib) != 0);
708 name = stream_read_tree (ib, data_in);
709 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
711 if (is_default_def)
712 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
714 i = streamer_read_uhwi (ib);
719 /* Go through all NODE edges and fixup call_stmt pointers
720 so they point to STMTS. */
722 static void
723 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
725 struct cgraph_edge *cedge;
726 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
727 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
728 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
729 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
732 /* Fixup call_stmt pointers in NODE and all clones. */
734 static void
735 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
737 struct cgraph_node *node;
739 while (orig->clone_of)
740 orig = orig->clone_of;
742 fixup_call_stmt_edges_1 (orig, stmts);
743 if (orig->clones)
744 for (node = orig->clones; node != orig;)
746 fixup_call_stmt_edges_1 (node, stmts);
747 if (node->clones)
748 node = node->clones;
749 else if (node->next_sibling_clone)
750 node = node->next_sibling_clone;
751 else
753 while (node != orig && !node->next_sibling_clone)
754 node = node->clone_of;
755 if (node != orig)
756 node = node->next_sibling_clone;
762 /* Input the base body of struct function FN from DATA_IN
763 using input block IB. */
765 static void
766 input_struct_function_base (struct function *fn, struct data_in *data_in,
767 struct lto_input_block *ib)
769 struct bitpack_d bp;
770 int len;
772 /* Read the static chain and non-local goto save area. */
773 fn->static_chain_decl = stream_read_tree (ib, data_in);
774 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
776 /* Read all the local symbols. */
777 len = streamer_read_hwi (ib);
778 if (len > 0)
780 int i;
781 VEC_safe_grow (tree, gc, fn->local_decls, len);
782 for (i = 0; i < len; i++)
784 tree t = stream_read_tree (ib, data_in);
785 VEC_replace (tree, fn->local_decls, i, t);
789 /* Input the function start and end loci. */
790 fn->function_start_locus = lto_input_location (ib, data_in);
791 fn->function_end_locus = lto_input_location (ib, data_in);
793 /* Input the current IL state of the function. */
794 fn->curr_properties = streamer_read_uhwi (ib);
796 /* Read all the attributes for FN. */
797 bp = streamer_read_bitpack (ib);
798 fn->is_thunk = bp_unpack_value (&bp, 1);
799 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
800 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
801 fn->returns_struct = bp_unpack_value (&bp, 1);
802 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
803 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
804 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
805 fn->after_inlining = bp_unpack_value (&bp, 1);
806 fn->stdarg = bp_unpack_value (&bp, 1);
807 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
808 fn->calls_alloca = bp_unpack_value (&bp, 1);
809 fn->calls_setjmp = bp_unpack_value (&bp, 1);
810 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
811 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
815 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
817 static void
818 input_function (tree fn_decl, struct data_in *data_in,
819 struct lto_input_block *ib)
821 struct function *fn;
822 enum LTO_tags tag;
823 gimple *stmts;
824 basic_block bb;
825 struct cgraph_node *node;
827 fn = DECL_STRUCT_FUNCTION (fn_decl);
828 tag = streamer_read_record_start (ib);
829 clear_line_info (data_in);
831 gimple_register_cfg_hooks ();
832 lto_tag_check (tag, LTO_function);
834 input_struct_function_base (fn, data_in, ib);
836 /* Read all the SSA names. */
837 input_ssa_names (ib, data_in, fn);
839 /* Read the exception handling regions in the function. */
840 input_eh_regions (ib, data_in, fn);
842 /* Read the tree of lexical scopes for the function. */
843 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
844 gcc_assert (DECL_INITIAL (fn_decl));
845 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
846 node = cgraph_get_create_node (fn_decl);
848 /* Read all the basic blocks. */
849 tag = streamer_read_record_start (ib);
850 while (tag)
852 input_bb (ib, tag, data_in, fn,
853 node->count_materialization_scale);
854 tag = streamer_read_record_start (ib);
857 /* Fix up the call statements that are mentioned in the callgraph
858 edges. */
859 set_gimple_stmt_max_uid (cfun, 0);
860 FOR_ALL_BB (bb)
862 gimple_stmt_iterator gsi;
863 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
865 gimple stmt = gsi_stmt (gsi);
866 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
869 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
870 FOR_ALL_BB (bb)
872 gimple_stmt_iterator bsi = gsi_start_bb (bb);
873 while (!gsi_end_p (bsi))
875 gimple stmt = gsi_stmt (bsi);
876 /* If we're recompiling LTO objects with debug stmts but
877 we're not supposed to have debug stmts, remove them now.
878 We can't remove them earlier because this would cause uid
879 mismatches in fixups, but we can do it at this point, as
880 long as debug stmts don't require fixups. */
881 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
883 gimple_stmt_iterator gsi = bsi;
884 gsi_next (&bsi);
885 gsi_remove (&gsi, true);
887 else
889 gsi_next (&bsi);
890 stmts[gimple_uid (stmt)] = stmt;
895 /* Set the gimple body to the statement sequence in the entry
896 basic block. FIXME lto, this is fairly hacky. The existence
897 of a gimple body is used by the cgraph routines, but we should
898 really use the presence of the CFG. */
900 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
901 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
904 fixup_call_stmt_edges (node, stmts);
905 execute_all_ipa_stmt_fixups (node, stmts);
907 update_ssa (TODO_update_ssa_only_virtuals);
908 free_dominance_info (CDI_DOMINATORS);
909 free_dominance_info (CDI_POST_DOMINATORS);
910 free (stmts);
914 /* Read the body from DATA for function FN_DECL and fill it in.
915 FILE_DATA are the global decls and types. SECTION_TYPE is either
916 LTO_section_function_body or LTO_section_static_initializer. If
917 section type is LTO_section_function_body, FN must be the decl for
918 that function. */
920 static void
921 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
922 const char *data, enum lto_section_type section_type)
924 const struct lto_function_header *header;
925 struct data_in *data_in;
926 int cfg_offset;
927 int main_offset;
928 int string_offset;
929 struct lto_input_block ib_cfg;
930 struct lto_input_block ib_main;
932 header = (const struct lto_function_header *) data;
933 cfg_offset = sizeof (struct lto_function_header);
934 main_offset = cfg_offset + header->cfg_size;
935 string_offset = main_offset + header->main_size;
937 LTO_INIT_INPUT_BLOCK (ib_cfg,
938 data + cfg_offset,
940 header->cfg_size);
942 LTO_INIT_INPUT_BLOCK (ib_main,
943 data + main_offset,
945 header->main_size);
947 data_in = lto_data_in_create (file_data, data + string_offset,
948 header->string_size, NULL);
950 /* Make sure the file was generated by the exact same compiler. */
951 lto_check_version (header->lto_header.major_version,
952 header->lto_header.minor_version);
954 if (section_type == LTO_section_function_body)
956 struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
957 struct lto_in_decl_state *decl_state;
958 struct cgraph_node *node = cgraph_get_node (fn_decl);
959 unsigned from;
961 gcc_checking_assert (node);
962 push_cfun (fn);
963 init_tree_ssa (fn);
965 /* We input IL in SSA form. */
966 cfun->gimple_df->in_ssa_p = true;
968 /* Use the function's decl state. */
969 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
970 gcc_assert (decl_state);
971 file_data->current_decl_state = decl_state;
973 input_cfg (&ib_cfg, fn, node->count_materialization_scale);
975 /* Set up the struct function. */
976 from = VEC_length (tree, data_in->reader_cache->nodes);
977 input_function (fn_decl, data_in, &ib_main);
978 /* And fixup types we streamed locally. */
980 struct streamer_tree_cache_d *cache = data_in->reader_cache;
981 unsigned len = VEC_length (tree, cache->nodes);
982 unsigned i;
983 for (i = len; i-- > from;)
985 tree t = VEC_index (tree, cache->nodes, i);
986 if (t == NULL_TREE)
987 continue;
989 if (TYPE_P (t))
991 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
992 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
993 if (TYPE_MAIN_VARIANT (t) != t)
995 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
996 TYPE_NEXT_VARIANT (t)
997 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
998 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1004 /* Restore decl state */
1005 file_data->current_decl_state = file_data->global_decl_state;
1007 pop_cfun ();
1010 clear_line_info (data_in);
1011 lto_data_in_delete (data_in);
1015 /* Read the body of FN_DECL using DATA. FILE_DATA holds the global
1016 decls and types. */
1018 void
1019 lto_input_function_body (struct lto_file_decl_data *file_data,
1020 tree fn_decl, const char *data)
1022 lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1026 /* Read the physical representation of a tree node with tag TAG from
1027 input block IB using the per-file context in DATA_IN. */
1029 static tree
1030 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1031 enum LTO_tags tag)
1033 /* Instantiate a new tree node. */
1034 tree result = streamer_alloc_tree (ib, data_in, tag);
1036 /* Enter RESULT in the reader cache. This will make RESULT
1037 available so that circular references in the rest of the tree
1038 structure can be resolved in subsequent calls to stream_read_tree. */
1039 streamer_tree_cache_append (data_in->reader_cache, result);
1041 /* Read all the bitfield values in RESULT. Note that for LTO, we
1042 only write language-independent bitfields, so no more unpacking is
1043 needed. */
1044 streamer_read_tree_bitfields (ib, result);
1046 /* Read all the pointer fields in RESULT. */
1047 streamer_read_tree_body (ib, data_in, result);
1049 /* Read any LTO-specific data not read by the tree streamer. */
1050 if (DECL_P (result)
1051 && TREE_CODE (result) != FUNCTION_DECL
1052 && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1053 DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1055 /* We should never try to instantiate an MD or NORMAL builtin here. */
1056 if (TREE_CODE (result) == FUNCTION_DECL)
1057 gcc_assert (!streamer_handle_as_builtin_p (result));
1059 /* end_marker = */ streamer_read_uchar (ib);
1061 #ifdef LTO_STREAMER_DEBUG
1062 /* Remove the mapping to RESULT's original address set by
1063 streamer_alloc_tree. */
1064 lto_orig_address_remove (result);
1065 #endif
1067 return result;
1071 /* Read a tree from input block IB using the per-file context in
1072 DATA_IN. This context is used, for example, to resolve references
1073 to previously read nodes. */
1075 tree
1076 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1078 enum LTO_tags tag;
1079 tree result;
1081 tag = streamer_read_record_start (ib);
1082 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1084 if (tag == LTO_null)
1085 result = NULL_TREE;
1086 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1088 /* If TAG is a reference to an indexable tree, the next value
1089 in IB is the index into the table where we expect to find
1090 that tree. */
1091 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1093 else if (tag == LTO_tree_pickle_reference)
1095 /* If TAG is a reference to a previously read tree, look it up in
1096 the reader cache. */
1097 result = streamer_get_pickled_tree (ib, data_in);
1099 else if (tag == LTO_builtin_decl)
1101 /* If we are going to read a built-in function, all we need is
1102 the code and class. */
1103 result = streamer_get_builtin_tree (ib, data_in);
1105 else if (tag == lto_tree_code_to_tag (INTEGER_CST))
1107 /* For integer constants we only need the type and its hi/low
1108 words. */
1109 result = streamer_read_integer_cst (ib, data_in);
1111 else
1113 /* Otherwise, materialize a new node from IB. */
1114 result = lto_read_tree (ib, data_in, tag);
1117 return result;
1121 /* Input toplevel asms. */
1123 void
1124 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1126 size_t len;
1127 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1128 NULL, &len);
1129 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1130 int string_offset;
1131 struct data_in *data_in;
1132 struct lto_input_block ib;
1133 tree str;
1135 if (! data)
1136 return;
1138 string_offset = sizeof (*header) + header->main_size;
1140 LTO_INIT_INPUT_BLOCK (ib,
1141 data + sizeof (*header),
1143 header->main_size);
1145 data_in = lto_data_in_create (file_data, data + string_offset,
1146 header->string_size, NULL);
1148 /* Make sure the file was generated by the exact same compiler. */
1149 lto_check_version (header->lto_header.major_version,
1150 header->lto_header.minor_version);
1152 while ((str = streamer_read_string_cst (data_in, &ib)))
1154 struct asm_node *node = add_asm_node (str);
1155 node->order = streamer_read_hwi (&ib) + order_base;
1156 if (node->order >= symtab_order)
1157 symtab_order = node->order + 1;
1160 clear_line_info (data_in);
1161 lto_data_in_delete (data_in);
1163 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1167 /* Initialization for the LTO reader. */
1169 void
1170 lto_reader_init (void)
1172 lto_streamer_init ();
1173 file_name_hash_table = htab_create (37, hash_string_slot_node,
1174 eq_string_slot_node, free);
1178 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1179 table to use with LEN strings. RESOLUTIONS is the vector of linker
1180 resolutions (NULL if not using a linker plugin). */
1182 struct data_in *
1183 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1184 unsigned len,
1185 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
1187 struct data_in *data_in = XCNEW (struct data_in);
1188 data_in->file_data = file_data;
1189 data_in->strings = strings;
1190 data_in->strings_len = len;
1191 data_in->globals_resolution = resolutions;
1192 data_in->reader_cache = streamer_tree_cache_create ();
1194 return data_in;
1198 /* Remove DATA_IN. */
1200 void
1201 lto_data_in_delete (struct data_in *data_in)
1203 VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
1204 streamer_tree_cache_delete (data_in->reader_cache);
1205 free (data_in->labels);
1206 free (data_in);