gcc/testsuite/
[official-gcc.git] / gcc / lto-streamer-in.c
blob15905f859a48ac5fa4826272933097422b18cb4f
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 location_t
142 lto_input_location (struct bitpack_d *bp, struct data_in *data_in)
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 reference to a tree node from DATA_IN using input block IB.
182 TAG is the expected node that should be found in IB, if TAG belongs
183 to one of the indexable trees, expect to read a reference index to
184 be looked up in one of the symbol tables, otherwise read the pysical
185 representation of the tree using stream_read_tree. FN is the
186 function scope for the read tree. */
188 tree
189 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
190 struct function *fn, enum LTO_tags tag)
192 unsigned HOST_WIDE_INT ix_u;
193 tree result = NULL_TREE;
195 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
197 switch (tag)
199 case LTO_type_ref:
200 ix_u = streamer_read_uhwi (ib);
201 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
202 break;
204 case LTO_ssa_name_ref:
205 ix_u = streamer_read_uhwi (ib);
206 result = VEC_index (tree, SSANAMES (fn), ix_u);
207 break;
209 case LTO_field_decl_ref:
210 ix_u = streamer_read_uhwi (ib);
211 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
212 break;
214 case LTO_function_decl_ref:
215 ix_u = streamer_read_uhwi (ib);
216 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
217 break;
219 case LTO_type_decl_ref:
220 ix_u = streamer_read_uhwi (ib);
221 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
222 break;
224 case LTO_namespace_decl_ref:
225 ix_u = streamer_read_uhwi (ib);
226 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
227 break;
229 case LTO_global_decl_ref:
230 case LTO_result_decl_ref:
231 case LTO_const_decl_ref:
232 case LTO_imported_decl_ref:
233 case LTO_label_decl_ref:
234 case LTO_translation_unit_decl_ref:
235 ix_u = streamer_read_uhwi (ib);
236 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
237 break;
239 default:
240 gcc_unreachable ();
243 gcc_assert (result);
245 return result;
249 /* Read and return a double-linked list of catch handlers from input
250 block IB, using descriptors in DATA_IN. */
252 static struct eh_catch_d *
253 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
254 eh_catch *last_p)
256 eh_catch first;
257 enum LTO_tags tag;
259 *last_p = first = NULL;
260 tag = streamer_read_record_start (ib);
261 while (tag)
263 tree list;
264 eh_catch n;
266 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
268 /* Read the catch node. */
269 n = ggc_alloc_cleared_eh_catch_d ();
270 n->type_list = stream_read_tree (ib, data_in);
271 n->filter_list = stream_read_tree (ib, data_in);
272 n->label = stream_read_tree (ib, data_in);
274 /* Register all the types in N->FILTER_LIST. */
275 for (list = n->filter_list; list; list = TREE_CHAIN (list))
276 add_type_for_runtime (TREE_VALUE (list));
278 /* Chain N to the end of the list. */
279 if (*last_p)
280 (*last_p)->next_catch = n;
281 n->prev_catch = *last_p;
282 *last_p = n;
284 /* Set the head of the list the first time through the loop. */
285 if (first == NULL)
286 first = n;
288 tag = streamer_read_record_start (ib);
291 return first;
295 /* Read and return EH region IX from input block IB, using descriptors
296 in DATA_IN. */
298 static eh_region
299 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
301 enum LTO_tags tag;
302 eh_region r;
304 /* Read the region header. */
305 tag = streamer_read_record_start (ib);
306 if (tag == LTO_null)
307 return NULL;
309 r = ggc_alloc_cleared_eh_region_d ();
310 r->index = streamer_read_hwi (ib);
312 gcc_assert (r->index == ix);
314 /* Read all the region pointers as region numbers. We'll fix up
315 the pointers once the whole array has been read. */
316 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
317 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
318 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
320 switch (tag)
322 case LTO_ert_cleanup:
323 r->type = ERT_CLEANUP;
324 break;
326 case LTO_ert_try:
328 struct eh_catch_d *last_catch;
329 r->type = ERT_TRY;
330 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
331 &last_catch);
332 r->u.eh_try.last_catch = last_catch;
333 break;
336 case LTO_ert_allowed_exceptions:
338 tree l;
340 r->type = ERT_ALLOWED_EXCEPTIONS;
341 r->u.allowed.type_list = stream_read_tree (ib, data_in);
342 r->u.allowed.label = stream_read_tree (ib, data_in);
343 r->u.allowed.filter = streamer_read_uhwi (ib);
345 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
346 add_type_for_runtime (TREE_VALUE (l));
348 break;
350 case LTO_ert_must_not_throw:
352 r->type = ERT_MUST_NOT_THROW;
353 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
354 bitpack_d bp = streamer_read_bitpack (ib);
355 r->u.must_not_throw.failure_loc
356 = stream_input_location (&bp, data_in);
358 break;
360 default:
361 gcc_unreachable ();
364 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
366 return r;
370 /* Read and return EH landing pad IX from input block IB, using descriptors
371 in DATA_IN. */
373 static eh_landing_pad
374 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
376 enum LTO_tags tag;
377 eh_landing_pad lp;
379 /* Read the landing pad header. */
380 tag = streamer_read_record_start (ib);
381 if (tag == LTO_null)
382 return NULL;
384 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
386 lp = ggc_alloc_cleared_eh_landing_pad_d ();
387 lp->index = streamer_read_hwi (ib);
388 gcc_assert (lp->index == ix);
389 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
390 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
391 lp->post_landing_pad = stream_read_tree (ib, data_in);
393 return lp;
397 /* After reading the EH regions, pointers to peer and children regions
398 are region numbers. This converts all these region numbers into
399 real pointers into the rematerialized regions for FN. ROOT_REGION
400 is the region number for the root EH region in FN. */
402 static void
403 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
405 unsigned i;
406 VEC(eh_region,gc) *eh_array = fn->eh->region_array;
407 VEC(eh_landing_pad,gc) *lp_array = fn->eh->lp_array;
408 eh_region r;
409 eh_landing_pad lp;
411 gcc_assert (eh_array && lp_array);
413 gcc_assert (root_region >= 0);
414 fn->eh->region_tree = VEC_index (eh_region, eh_array, root_region);
416 #define FIXUP_EH_REGION(r) (r) = VEC_index (eh_region, eh_array, \
417 (HOST_WIDE_INT) (intptr_t) (r))
418 #define FIXUP_EH_LP(p) (p) = VEC_index (eh_landing_pad, lp_array, \
419 (HOST_WIDE_INT) (intptr_t) (p))
421 /* Convert all the index numbers stored in pointer fields into
422 pointers to the corresponding slots in the EH region array. */
423 FOR_EACH_VEC_ELT (eh_region, eh_array, i, r)
425 /* The array may contain NULL regions. */
426 if (r == NULL)
427 continue;
429 gcc_assert (i == (unsigned) r->index);
430 FIXUP_EH_REGION (r->outer);
431 FIXUP_EH_REGION (r->inner);
432 FIXUP_EH_REGION (r->next_peer);
433 FIXUP_EH_LP (r->landing_pads);
436 /* Convert all the index numbers stored in pointer fields into
437 pointers to the corresponding slots in the EH landing pad array. */
438 FOR_EACH_VEC_ELT (eh_landing_pad, lp_array, i, lp)
440 /* The array may contain NULL landing pads. */
441 if (lp == NULL)
442 continue;
444 gcc_assert (i == (unsigned) lp->index);
445 FIXUP_EH_LP (lp->next_lp);
446 FIXUP_EH_REGION (lp->region);
449 #undef FIXUP_EH_REGION
450 #undef FIXUP_EH_LP
454 /* Initialize EH support. */
456 void
457 lto_init_eh (void)
459 static bool eh_initialized_p = false;
461 if (eh_initialized_p)
462 return;
464 /* Contrary to most other FEs, we only initialize EH support when at
465 least one of the files in the set contains exception regions in
466 it. Since this happens much later than the call to init_eh in
467 lang_dependent_init, we have to set flag_exceptions and call
468 init_eh again to initialize the EH tables. */
469 flag_exceptions = 1;
470 init_eh ();
472 eh_initialized_p = true;
476 /* Read the exception table for FN from IB using the data descriptors
477 in DATA_IN. */
479 static void
480 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
481 struct function *fn)
483 HOST_WIDE_INT i, root_region, len;
484 enum LTO_tags tag;
486 tag = streamer_read_record_start (ib);
487 if (tag == LTO_null)
488 return;
490 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
492 /* If the file contains EH regions, then it was compiled with
493 -fexceptions. In that case, initialize the backend EH
494 machinery. */
495 lto_init_eh ();
497 gcc_assert (fn->eh);
499 root_region = streamer_read_hwi (ib);
500 gcc_assert (root_region == (int) root_region);
502 /* Read the EH region array. */
503 len = streamer_read_hwi (ib);
504 gcc_assert (len == (int) len);
505 if (len > 0)
507 VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
508 for (i = 0; i < len; i++)
510 eh_region r = input_eh_region (ib, data_in, i);
511 VEC_replace (eh_region, fn->eh->region_array, i, r);
515 /* Read the landing pads. */
516 len = streamer_read_hwi (ib);
517 gcc_assert (len == (int) len);
518 if (len > 0)
520 VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
521 for (i = 0; i < len; i++)
523 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
524 VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
528 /* Read the runtime type data. */
529 len = streamer_read_hwi (ib);
530 gcc_assert (len == (int) len);
531 if (len > 0)
533 VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
534 for (i = 0; i < len; i++)
536 tree ttype = stream_read_tree (ib, data_in);
537 VEC_replace (tree, fn->eh->ttype_data, i, ttype);
541 /* Read the table of action chains. */
542 len = streamer_read_hwi (ib);
543 gcc_assert (len == (int) len);
544 if (len > 0)
546 if (targetm.arm_eabi_unwinder)
548 VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
549 for (i = 0; i < len; i++)
551 tree t = stream_read_tree (ib, data_in);
552 VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
555 else
557 VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
558 for (i = 0; i < len; i++)
560 uchar c = streamer_read_uchar (ib);
561 VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
566 /* Reconstruct the EH region tree by fixing up the peer/children
567 pointers. */
568 fixup_eh_region_pointers (fn, root_region);
570 tag = streamer_read_record_start (ib);
571 lto_tag_check_range (tag, LTO_null, LTO_null);
575 /* Make a new basic block with index INDEX in function FN. */
577 static basic_block
578 make_new_block (struct function *fn, unsigned int index)
580 basic_block bb = alloc_block ();
581 bb->index = index;
582 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
583 n_basic_blocks_for_function (fn)++;
584 return bb;
588 /* Read the CFG for function FN from input block IB. */
590 static void
591 input_cfg (struct lto_input_block *ib, struct function *fn,
592 int count_materialization_scale)
594 unsigned int bb_count;
595 basic_block p_bb;
596 unsigned int i;
597 int index;
599 init_empty_tree_cfg_for_function (fn);
600 init_ssa_operands (fn);
602 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
603 PROFILE_LAST);
605 bb_count = streamer_read_uhwi (ib);
607 last_basic_block_for_function (fn) = bb_count;
608 if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
609 VEC_safe_grow_cleared (basic_block, gc,
610 basic_block_info_for_function (fn), bb_count);
612 if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
613 VEC_safe_grow_cleared (basic_block, gc,
614 label_to_block_map_for_function (fn), bb_count);
616 index = streamer_read_hwi (ib);
617 while (index != -1)
619 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
620 unsigned int edge_count;
622 if (bb == NULL)
623 bb = make_new_block (fn, index);
625 edge_count = streamer_read_uhwi (ib);
627 /* Connect up the CFG. */
628 for (i = 0; i < edge_count; i++)
630 unsigned int dest_index;
631 unsigned int edge_flags;
632 basic_block dest;
633 int probability;
634 gcov_type count;
635 edge e;
637 dest_index = streamer_read_uhwi (ib);
638 probability = (int) streamer_read_hwi (ib);
639 count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
640 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
641 edge_flags = streamer_read_uhwi (ib);
643 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
645 if (dest == NULL)
646 dest = make_new_block (fn, dest_index);
648 e = make_edge (bb, dest, edge_flags);
649 e->probability = probability;
650 e->count = count;
653 index = streamer_read_hwi (ib);
656 p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
657 index = streamer_read_hwi (ib);
658 while (index != -1)
660 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
661 bb->prev_bb = p_bb;
662 p_bb->next_bb = bb;
663 p_bb = bb;
664 index = streamer_read_hwi (ib);
669 /* Read the SSA names array for function FN from DATA_IN using input
670 block IB. */
672 static void
673 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
674 struct function *fn)
676 unsigned int i, size;
678 size = streamer_read_uhwi (ib);
679 init_ssanames (fn, size);
681 i = streamer_read_uhwi (ib);
682 while (i)
684 tree ssa_name, name;
685 bool is_default_def;
687 /* Skip over the elements that had been freed. */
688 while (VEC_length (tree, SSANAMES (fn)) < i)
689 VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
691 is_default_def = (streamer_read_uchar (ib) != 0);
692 name = stream_read_tree (ib, data_in);
693 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
695 if (is_default_def)
696 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
698 i = streamer_read_uhwi (ib);
703 /* Go through all NODE edges and fixup call_stmt pointers
704 so they point to STMTS. */
706 static void
707 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
709 struct cgraph_edge *cedge;
710 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
711 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
712 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
713 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
716 /* Fixup call_stmt pointers in NODE and all clones. */
718 static void
719 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
721 struct cgraph_node *node;
723 while (orig->clone_of)
724 orig = orig->clone_of;
726 fixup_call_stmt_edges_1 (orig, stmts);
727 if (orig->clones)
728 for (node = orig->clones; node != orig;)
730 fixup_call_stmt_edges_1 (node, stmts);
731 if (node->clones)
732 node = node->clones;
733 else if (node->next_sibling_clone)
734 node = node->next_sibling_clone;
735 else
737 while (node != orig && !node->next_sibling_clone)
738 node = node->clone_of;
739 if (node != orig)
740 node = node->next_sibling_clone;
746 /* Input the base body of struct function FN from DATA_IN
747 using input block IB. */
749 static void
750 input_struct_function_base (struct function *fn, struct data_in *data_in,
751 struct lto_input_block *ib)
753 struct bitpack_d bp;
754 int len;
756 /* Read the static chain and non-local goto save area. */
757 fn->static_chain_decl = stream_read_tree (ib, data_in);
758 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
760 /* Read all the local symbols. */
761 len = streamer_read_hwi (ib);
762 if (len > 0)
764 int i;
765 VEC_safe_grow (tree, gc, fn->local_decls, len);
766 for (i = 0; i < len; i++)
768 tree t = stream_read_tree (ib, data_in);
769 VEC_replace (tree, fn->local_decls, i, t);
773 /* Input the current IL state of the function. */
774 fn->curr_properties = streamer_read_uhwi (ib);
776 /* Read all the attributes for FN. */
777 bp = streamer_read_bitpack (ib);
778 fn->is_thunk = bp_unpack_value (&bp, 1);
779 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
780 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
781 fn->returns_struct = bp_unpack_value (&bp, 1);
782 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
783 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
784 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
785 fn->after_inlining = bp_unpack_value (&bp, 1);
786 fn->stdarg = bp_unpack_value (&bp, 1);
787 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
788 fn->calls_alloca = bp_unpack_value (&bp, 1);
789 fn->calls_setjmp = bp_unpack_value (&bp, 1);
790 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
791 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
793 /* Input the function start and end loci. */
794 fn->function_start_locus = stream_input_location (&bp, data_in);
795 fn->function_end_locus = stream_input_location (&bp, data_in);
799 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
801 static void
802 input_function (tree fn_decl, struct data_in *data_in,
803 struct lto_input_block *ib)
805 struct function *fn;
806 enum LTO_tags tag;
807 gimple *stmts;
808 basic_block bb;
809 struct cgraph_node *node;
811 fn = DECL_STRUCT_FUNCTION (fn_decl);
812 tag = streamer_read_record_start (ib);
813 clear_line_info (data_in);
815 gimple_register_cfg_hooks ();
816 lto_tag_check (tag, LTO_function);
818 input_struct_function_base (fn, data_in, ib);
820 /* Read all the SSA names. */
821 input_ssa_names (ib, data_in, fn);
823 /* Read the exception handling regions in the function. */
824 input_eh_regions (ib, data_in, fn);
826 /* Read the tree of lexical scopes for the function. */
827 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
828 gcc_assert (DECL_INITIAL (fn_decl));
829 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
830 node = cgraph_get_create_node (fn_decl);
832 /* Read all the basic blocks. */
833 tag = streamer_read_record_start (ib);
834 while (tag)
836 input_bb (ib, tag, data_in, fn,
837 node->count_materialization_scale);
838 tag = streamer_read_record_start (ib);
841 /* Fix up the call statements that are mentioned in the callgraph
842 edges. */
843 set_gimple_stmt_max_uid (cfun, 0);
844 FOR_ALL_BB (bb)
846 gimple_stmt_iterator gsi;
847 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
849 gimple stmt = gsi_stmt (gsi);
850 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
853 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
854 FOR_ALL_BB (bb)
856 gimple_stmt_iterator bsi = gsi_start_bb (bb);
857 while (!gsi_end_p (bsi))
859 gimple stmt = gsi_stmt (bsi);
860 /* If we're recompiling LTO objects with debug stmts but
861 we're not supposed to have debug stmts, remove them now.
862 We can't remove them earlier because this would cause uid
863 mismatches in fixups, but we can do it at this point, as
864 long as debug stmts don't require fixups. */
865 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
867 gimple_stmt_iterator gsi = bsi;
868 gsi_next (&bsi);
869 gsi_remove (&gsi, true);
871 else
873 gsi_next (&bsi);
874 stmts[gimple_uid (stmt)] = stmt;
879 /* Set the gimple body to the statement sequence in the entry
880 basic block. FIXME lto, this is fairly hacky. The existence
881 of a gimple body is used by the cgraph routines, but we should
882 really use the presence of the CFG. */
884 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
885 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
888 fixup_call_stmt_edges (node, stmts);
889 execute_all_ipa_stmt_fixups (node, stmts);
891 update_ssa (TODO_update_ssa_only_virtuals);
892 free_dominance_info (CDI_DOMINATORS);
893 free_dominance_info (CDI_POST_DOMINATORS);
894 free (stmts);
898 /* Read the body from DATA for function FN_DECL and fill it in.
899 FILE_DATA are the global decls and types. SECTION_TYPE is either
900 LTO_section_function_body or LTO_section_static_initializer. If
901 section type is LTO_section_function_body, FN must be the decl for
902 that function. */
904 static void
905 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
906 const char *data, enum lto_section_type section_type)
908 const struct lto_function_header *header;
909 struct data_in *data_in;
910 int cfg_offset;
911 int main_offset;
912 int string_offset;
913 struct lto_input_block ib_cfg;
914 struct lto_input_block ib_main;
916 header = (const struct lto_function_header *) data;
917 cfg_offset = sizeof (struct lto_function_header);
918 main_offset = cfg_offset + header->cfg_size;
919 string_offset = main_offset + header->main_size;
921 LTO_INIT_INPUT_BLOCK (ib_cfg,
922 data + cfg_offset,
924 header->cfg_size);
926 LTO_INIT_INPUT_BLOCK (ib_main,
927 data + main_offset,
929 header->main_size);
931 data_in = lto_data_in_create (file_data, data + string_offset,
932 header->string_size, NULL);
934 /* Make sure the file was generated by the exact same compiler. */
935 lto_check_version (header->lto_header.major_version,
936 header->lto_header.minor_version);
938 if (section_type == LTO_section_function_body)
940 struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
941 struct lto_in_decl_state *decl_state;
942 struct cgraph_node *node = cgraph_get_node (fn_decl);
943 unsigned from;
945 gcc_checking_assert (node);
946 push_cfun (fn);
947 init_tree_ssa (fn);
949 /* We input IL in SSA form. */
950 cfun->gimple_df->in_ssa_p = true;
952 /* Use the function's decl state. */
953 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
954 gcc_assert (decl_state);
955 file_data->current_decl_state = decl_state;
957 input_cfg (&ib_cfg, fn, node->count_materialization_scale);
959 /* Set up the struct function. */
960 from = VEC_length (tree, data_in->reader_cache->nodes);
961 input_function (fn_decl, data_in, &ib_main);
962 /* And fixup types we streamed locally. */
964 struct streamer_tree_cache_d *cache = data_in->reader_cache;
965 unsigned len = VEC_length (tree, cache->nodes);
966 unsigned i;
967 for (i = len; i-- > from;)
969 tree t = VEC_index (tree, cache->nodes, i);
970 if (t == NULL_TREE)
971 continue;
973 if (TYPE_P (t))
975 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
976 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
977 if (TYPE_MAIN_VARIANT (t) != t)
979 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
980 TYPE_NEXT_VARIANT (t)
981 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
982 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
988 /* Restore decl state */
989 file_data->current_decl_state = file_data->global_decl_state;
991 pop_cfun ();
994 clear_line_info (data_in);
995 lto_data_in_delete (data_in);
999 /* Read the body of FN_DECL using DATA. FILE_DATA holds the global
1000 decls and types. */
1002 void
1003 lto_input_function_body (struct lto_file_decl_data *file_data,
1004 tree fn_decl, const char *data)
1006 lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1010 /* Read the physical representation of a tree node with tag TAG from
1011 input block IB using the per-file context in DATA_IN. */
1013 static tree
1014 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1015 enum LTO_tags tag)
1017 /* Instantiate a new tree node. */
1018 tree result = streamer_alloc_tree (ib, data_in, tag);
1020 /* Enter RESULT in the reader cache. This will make RESULT
1021 available so that circular references in the rest of the tree
1022 structure can be resolved in subsequent calls to stream_read_tree. */
1023 streamer_tree_cache_append (data_in->reader_cache, result);
1025 /* Read all the bitfield values in RESULT. Note that for LTO, we
1026 only write language-independent bitfields, so no more unpacking is
1027 needed. */
1028 streamer_read_tree_bitfields (ib, data_in, result);
1030 /* Read all the pointer fields in RESULT. */
1031 streamer_read_tree_body (ib, data_in, result);
1033 /* Read any LTO-specific data not read by the tree streamer. */
1034 if (DECL_P (result)
1035 && TREE_CODE (result) != FUNCTION_DECL
1036 && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1037 DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1039 /* We should never try to instantiate an MD or NORMAL builtin here. */
1040 if (TREE_CODE (result) == FUNCTION_DECL)
1041 gcc_assert (!streamer_handle_as_builtin_p (result));
1043 /* end_marker = */ streamer_read_uchar (ib);
1045 #ifdef LTO_STREAMER_DEBUG
1046 /* Remove the mapping to RESULT's original address set by
1047 streamer_alloc_tree. */
1048 lto_orig_address_remove (result);
1049 #endif
1051 return result;
1055 /* Read a tree from input block IB using the per-file context in
1056 DATA_IN. This context is used, for example, to resolve references
1057 to previously read nodes. */
1059 tree
1060 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1062 enum LTO_tags tag;
1063 tree result;
1065 tag = streamer_read_record_start (ib);
1066 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1068 if (tag == LTO_null)
1069 result = NULL_TREE;
1070 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1072 /* If TAG is a reference to an indexable tree, the next value
1073 in IB is the index into the table where we expect to find
1074 that tree. */
1075 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1077 else if (tag == LTO_tree_pickle_reference)
1079 /* If TAG is a reference to a previously read tree, look it up in
1080 the reader cache. */
1081 result = streamer_get_pickled_tree (ib, data_in);
1083 else if (tag == LTO_builtin_decl)
1085 /* If we are going to read a built-in function, all we need is
1086 the code and class. */
1087 result = streamer_get_builtin_tree (ib, data_in);
1089 else if (tag == LTO_integer_cst)
1091 /* For shared integer constants we only need the type and its hi/low
1092 words. */
1093 result = streamer_read_integer_cst (ib, data_in);
1095 else
1097 /* Otherwise, materialize a new node from IB. */
1098 result = lto_read_tree (ib, data_in, tag);
1101 return result;
1105 /* Input toplevel asms. */
1107 void
1108 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1110 size_t len;
1111 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1112 NULL, &len);
1113 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1114 int string_offset;
1115 struct data_in *data_in;
1116 struct lto_input_block ib;
1117 tree str;
1119 if (! data)
1120 return;
1122 string_offset = sizeof (*header) + header->main_size;
1124 LTO_INIT_INPUT_BLOCK (ib,
1125 data + sizeof (*header),
1127 header->main_size);
1129 data_in = lto_data_in_create (file_data, data + string_offset,
1130 header->string_size, NULL);
1132 /* Make sure the file was generated by the exact same compiler. */
1133 lto_check_version (header->lto_header.major_version,
1134 header->lto_header.minor_version);
1136 while ((str = streamer_read_string_cst (data_in, &ib)))
1138 struct asm_node *node = add_asm_node (str);
1139 node->order = streamer_read_hwi (&ib) + order_base;
1140 if (node->order >= symtab_order)
1141 symtab_order = node->order + 1;
1144 clear_line_info (data_in);
1145 lto_data_in_delete (data_in);
1147 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1151 /* Initialization for the LTO reader. */
1153 void
1154 lto_reader_init (void)
1156 lto_streamer_init ();
1157 file_name_hash_table = htab_create (37, hash_string_slot_node,
1158 eq_string_slot_node, free);
1162 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1163 table to use with LEN strings. RESOLUTIONS is the vector of linker
1164 resolutions (NULL if not using a linker plugin). */
1166 struct data_in *
1167 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1168 unsigned len,
1169 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
1171 struct data_in *data_in = XCNEW (struct data_in);
1172 data_in->file_data = file_data;
1173 data_in->strings = strings;
1174 data_in->strings_len = len;
1175 data_in->globals_resolution = resolutions;
1176 data_in->reader_cache = streamer_tree_cache_create ();
1178 return data_in;
1182 /* Remove DATA_IN. */
1184 void
1185 lto_data_in_delete (struct data_in *data_in)
1187 VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
1188 streamer_tree_cache_delete (data_in->reader_cache);
1189 free (data_in->labels);
1190 free (data_in);