2012-12-01 Alessandro Fanfarillo <alessandro.fanfarillo@gmail.com>
[official-gcc.git] / gcc / lto-streamer-in.c
blobe2629254782e3ab5edb06736292e2b175ff9ac89
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 = (*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, va_gc> *eh_array = fn->eh->region_array;
407 vec<eh_landing_pad, va_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 = (*eh_array)[root_region];
416 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
417 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
419 /* Convert all the index numbers stored in pointer fields into
420 pointers to the corresponding slots in the EH region array. */
421 FOR_EACH_VEC_ELT (*eh_array, i, r)
423 /* The array may contain NULL regions. */
424 if (r == NULL)
425 continue;
427 gcc_assert (i == (unsigned) r->index);
428 FIXUP_EH_REGION (r->outer);
429 FIXUP_EH_REGION (r->inner);
430 FIXUP_EH_REGION (r->next_peer);
431 FIXUP_EH_LP (r->landing_pads);
434 /* Convert all the index numbers stored in pointer fields into
435 pointers to the corresponding slots in the EH landing pad array. */
436 FOR_EACH_VEC_ELT (*lp_array, i, lp)
438 /* The array may contain NULL landing pads. */
439 if (lp == NULL)
440 continue;
442 gcc_assert (i == (unsigned) lp->index);
443 FIXUP_EH_LP (lp->next_lp);
444 FIXUP_EH_REGION (lp->region);
447 #undef FIXUP_EH_REGION
448 #undef FIXUP_EH_LP
452 /* Initialize EH support. */
454 void
455 lto_init_eh (void)
457 static bool eh_initialized_p = false;
459 if (eh_initialized_p)
460 return;
462 /* Contrary to most other FEs, we only initialize EH support when at
463 least one of the files in the set contains exception regions in
464 it. Since this happens much later than the call to init_eh in
465 lang_dependent_init, we have to set flag_exceptions and call
466 init_eh again to initialize the EH tables. */
467 flag_exceptions = 1;
468 init_eh ();
470 eh_initialized_p = true;
474 /* Read the exception table for FN from IB using the data descriptors
475 in DATA_IN. */
477 static void
478 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
479 struct function *fn)
481 HOST_WIDE_INT i, root_region, len;
482 enum LTO_tags tag;
484 tag = streamer_read_record_start (ib);
485 if (tag == LTO_null)
486 return;
488 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
490 /* If the file contains EH regions, then it was compiled with
491 -fexceptions. In that case, initialize the backend EH
492 machinery. */
493 lto_init_eh ();
495 gcc_assert (fn->eh);
497 root_region = streamer_read_hwi (ib);
498 gcc_assert (root_region == (int) root_region);
500 /* Read the EH region array. */
501 len = streamer_read_hwi (ib);
502 gcc_assert (len == (int) len);
503 if (len > 0)
505 vec_safe_grow_cleared (fn->eh->region_array, len);
506 for (i = 0; i < len; i++)
508 eh_region r = input_eh_region (ib, data_in, i);
509 (*fn->eh->region_array)[i] = r;
513 /* Read the landing pads. */
514 len = streamer_read_hwi (ib);
515 gcc_assert (len == (int) len);
516 if (len > 0)
518 vec_safe_grow_cleared (fn->eh->lp_array, len);
519 for (i = 0; i < len; i++)
521 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
522 (*fn->eh->lp_array)[i] = lp;
526 /* Read the runtime type data. */
527 len = streamer_read_hwi (ib);
528 gcc_assert (len == (int) len);
529 if (len > 0)
531 vec_safe_grow_cleared (fn->eh->ttype_data, len);
532 for (i = 0; i < len; i++)
534 tree ttype = stream_read_tree (ib, data_in);
535 (*fn->eh->ttype_data)[i] = ttype;
539 /* Read the table of action chains. */
540 len = streamer_read_hwi (ib);
541 gcc_assert (len == (int) len);
542 if (len > 0)
544 if (targetm.arm_eabi_unwinder)
546 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
547 for (i = 0; i < len; i++)
549 tree t = stream_read_tree (ib, data_in);
550 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
553 else
555 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
556 for (i = 0; i < len; i++)
558 uchar c = streamer_read_uchar (ib);
559 (*fn->eh->ehspec_data.other)[i] = c;
564 /* Reconstruct the EH region tree by fixing up the peer/children
565 pointers. */
566 fixup_eh_region_pointers (fn, root_region);
568 tag = streamer_read_record_start (ib);
569 lto_tag_check_range (tag, LTO_null, LTO_null);
573 /* Make a new basic block with index INDEX in function FN. */
575 static basic_block
576 make_new_block (struct function *fn, unsigned int index)
578 basic_block bb = alloc_block ();
579 bb->index = index;
580 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
581 n_basic_blocks_for_function (fn)++;
582 return bb;
586 /* Read the CFG for function FN from input block IB. */
588 static void
589 input_cfg (struct lto_input_block *ib, struct function *fn,
590 int count_materialization_scale)
592 unsigned int bb_count;
593 basic_block p_bb;
594 unsigned int i;
595 int index;
597 init_empty_tree_cfg_for_function (fn);
598 init_ssa_operands (fn);
600 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
601 PROFILE_LAST);
603 bb_count = streamer_read_uhwi (ib);
605 last_basic_block_for_function (fn) = bb_count;
606 if (bb_count > basic_block_info_for_function (fn)->length ())
607 vec_safe_grow_cleared (basic_block_info_for_function (fn), bb_count);
609 if (bb_count > label_to_block_map_for_function (fn)->length ())
610 vec_safe_grow_cleared (label_to_block_map_for_function (fn), bb_count);
612 index = streamer_read_hwi (ib);
613 while (index != -1)
615 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
616 unsigned int edge_count;
618 if (bb == NULL)
619 bb = make_new_block (fn, index);
621 edge_count = streamer_read_uhwi (ib);
623 /* Connect up the CFG. */
624 for (i = 0; i < edge_count; i++)
626 unsigned int dest_index;
627 unsigned int edge_flags;
628 basic_block dest;
629 int probability;
630 gcov_type count;
631 edge e;
633 dest_index = streamer_read_uhwi (ib);
634 probability = (int) streamer_read_hwi (ib);
635 count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
636 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
637 edge_flags = streamer_read_uhwi (ib);
639 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
641 if (dest == NULL)
642 dest = make_new_block (fn, dest_index);
644 e = make_edge (bb, dest, edge_flags);
645 e->probability = probability;
646 e->count = count;
649 index = streamer_read_hwi (ib);
652 p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
653 index = streamer_read_hwi (ib);
654 while (index != -1)
656 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
657 bb->prev_bb = p_bb;
658 p_bb->next_bb = bb;
659 p_bb = bb;
660 index = streamer_read_hwi (ib);
665 /* Read the SSA names array for function FN from DATA_IN using input
666 block IB. */
668 static void
669 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
670 struct function *fn)
672 unsigned int i, size;
674 size = streamer_read_uhwi (ib);
675 init_ssanames (fn, size);
677 i = streamer_read_uhwi (ib);
678 while (i)
680 tree ssa_name, name;
681 bool is_default_def;
683 /* Skip over the elements that had been freed. */
684 while (SSANAMES (fn)->length () < i)
685 SSANAMES (fn)->quick_push (NULL_TREE);
687 is_default_def = (streamer_read_uchar (ib) != 0);
688 name = stream_read_tree (ib, data_in);
689 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
691 if (is_default_def)
692 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
694 i = streamer_read_uhwi (ib);
699 /* Go through all NODE edges and fixup call_stmt pointers
700 so they point to STMTS. */
702 static void
703 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
705 struct cgraph_edge *cedge;
706 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
707 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
708 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
709 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
712 /* Fixup call_stmt pointers in NODE and all clones. */
714 static void
715 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
717 struct cgraph_node *node;
719 while (orig->clone_of)
720 orig = orig->clone_of;
722 fixup_call_stmt_edges_1 (orig, stmts);
723 if (orig->clones)
724 for (node = orig->clones; node != orig;)
726 fixup_call_stmt_edges_1 (node, stmts);
727 if (node->clones)
728 node = node->clones;
729 else if (node->next_sibling_clone)
730 node = node->next_sibling_clone;
731 else
733 while (node != orig && !node->next_sibling_clone)
734 node = node->clone_of;
735 if (node != orig)
736 node = node->next_sibling_clone;
742 /* Input the base body of struct function FN from DATA_IN
743 using input block IB. */
745 static void
746 input_struct_function_base (struct function *fn, struct data_in *data_in,
747 struct lto_input_block *ib)
749 struct bitpack_d bp;
750 int len;
752 /* Read the static chain and non-local goto save area. */
753 fn->static_chain_decl = stream_read_tree (ib, data_in);
754 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
756 /* Read all the local symbols. */
757 len = streamer_read_hwi (ib);
758 if (len > 0)
760 int i;
761 vec_safe_grow_cleared (fn->local_decls, len);
762 for (i = 0; i < len; i++)
764 tree t = stream_read_tree (ib, data_in);
765 (*fn->local_decls)[i] = t;
769 /* Input the current IL state of the function. */
770 fn->curr_properties = streamer_read_uhwi (ib);
772 /* Read all the attributes for FN. */
773 bp = streamer_read_bitpack (ib);
774 fn->is_thunk = bp_unpack_value (&bp, 1);
775 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
776 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
777 fn->returns_struct = bp_unpack_value (&bp, 1);
778 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
779 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
780 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
781 fn->after_inlining = bp_unpack_value (&bp, 1);
782 fn->stdarg = bp_unpack_value (&bp, 1);
783 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
784 fn->calls_alloca = bp_unpack_value (&bp, 1);
785 fn->calls_setjmp = bp_unpack_value (&bp, 1);
786 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
787 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
789 /* Input the function start and end loci. */
790 fn->function_start_locus = stream_input_location (&bp, data_in);
791 fn->function_end_locus = stream_input_location (&bp, data_in);
795 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
797 static void
798 input_function (tree fn_decl, struct data_in *data_in,
799 struct lto_input_block *ib)
801 struct function *fn;
802 enum LTO_tags tag;
803 gimple *stmts;
804 basic_block bb;
805 struct cgraph_node *node;
807 fn = DECL_STRUCT_FUNCTION (fn_decl);
808 tag = streamer_read_record_start (ib);
809 clear_line_info (data_in);
811 gimple_register_cfg_hooks ();
812 lto_tag_check (tag, LTO_function);
814 input_struct_function_base (fn, data_in, ib);
816 /* Read all the SSA names. */
817 input_ssa_names (ib, data_in, fn);
819 /* Read the exception handling regions in the function. */
820 input_eh_regions (ib, data_in, fn);
822 /* Read the tree of lexical scopes for the function. */
823 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
824 gcc_assert (DECL_INITIAL (fn_decl));
825 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
826 node = cgraph_get_create_node (fn_decl);
828 /* Read all the basic blocks. */
829 tag = streamer_read_record_start (ib);
830 while (tag)
832 input_bb (ib, tag, data_in, fn,
833 node->count_materialization_scale);
834 tag = streamer_read_record_start (ib);
837 /* Fix up the call statements that are mentioned in the callgraph
838 edges. */
839 set_gimple_stmt_max_uid (cfun, 0);
840 FOR_ALL_BB (bb)
842 gimple_stmt_iterator gsi;
843 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
845 gimple stmt = gsi_stmt (gsi);
846 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
849 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
850 FOR_ALL_BB (bb)
852 gimple_stmt_iterator bsi = gsi_start_bb (bb);
853 while (!gsi_end_p (bsi))
855 gimple stmt = gsi_stmt (bsi);
856 /* If we're recompiling LTO objects with debug stmts but
857 we're not supposed to have debug stmts, remove them now.
858 We can't remove them earlier because this would cause uid
859 mismatches in fixups, but we can do it at this point, as
860 long as debug stmts don't require fixups. */
861 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
863 gimple_stmt_iterator gsi = bsi;
864 gsi_next (&bsi);
865 gsi_remove (&gsi, true);
867 else
869 gsi_next (&bsi);
870 stmts[gimple_uid (stmt)] = stmt;
875 /* Set the gimple body to the statement sequence in the entry
876 basic block. FIXME lto, this is fairly hacky. The existence
877 of a gimple body is used by the cgraph routines, but we should
878 really use the presence of the CFG. */
880 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
881 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
884 fixup_call_stmt_edges (node, stmts);
885 execute_all_ipa_stmt_fixups (node, stmts);
887 update_ssa (TODO_update_ssa_only_virtuals);
888 free_dominance_info (CDI_DOMINATORS);
889 free_dominance_info (CDI_POST_DOMINATORS);
890 free (stmts);
894 /* Read the body from DATA for function FN_DECL and fill it in.
895 FILE_DATA are the global decls and types. SECTION_TYPE is either
896 LTO_section_function_body or LTO_section_static_initializer. If
897 section type is LTO_section_function_body, FN must be the decl for
898 that function. */
900 static void
901 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
902 const char *data, enum lto_section_type section_type)
904 const struct lto_function_header *header;
905 struct data_in *data_in;
906 int cfg_offset;
907 int main_offset;
908 int string_offset;
909 struct lto_input_block ib_cfg;
910 struct lto_input_block ib_main;
912 header = (const struct lto_function_header *) data;
913 cfg_offset = sizeof (struct lto_function_header);
914 main_offset = cfg_offset + header->cfg_size;
915 string_offset = main_offset + header->main_size;
917 LTO_INIT_INPUT_BLOCK (ib_cfg,
918 data + cfg_offset,
920 header->cfg_size);
922 LTO_INIT_INPUT_BLOCK (ib_main,
923 data + main_offset,
925 header->main_size);
927 data_in = lto_data_in_create (file_data, data + string_offset,
928 header->string_size, vNULL);
930 /* Make sure the file was generated by the exact same compiler. */
931 lto_check_version (header->lto_header.major_version,
932 header->lto_header.minor_version);
934 if (section_type == LTO_section_function_body)
936 struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
937 struct lto_in_decl_state *decl_state;
938 struct cgraph_node *node = cgraph_get_node (fn_decl);
939 unsigned from;
941 gcc_checking_assert (node);
942 push_cfun (fn);
943 init_tree_ssa (fn);
945 /* We input IL in SSA form. */
946 cfun->gimple_df->in_ssa_p = true;
948 /* Use the function's decl state. */
949 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
950 gcc_assert (decl_state);
951 file_data->current_decl_state = decl_state;
953 input_cfg (&ib_cfg, fn, node->count_materialization_scale);
955 /* Set up the struct function. */
956 from = data_in->reader_cache->nodes.length ();
957 input_function (fn_decl, data_in, &ib_main);
958 /* And fixup types we streamed locally. */
960 struct streamer_tree_cache_d *cache = data_in->reader_cache;
961 unsigned len = cache->nodes.length ();
962 unsigned i;
963 for (i = len; i-- > from;)
965 tree t = cache->nodes[i];
966 if (t == NULL_TREE)
967 continue;
969 if (TYPE_P (t))
971 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
972 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
973 if (TYPE_MAIN_VARIANT (t) != t)
975 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
976 TYPE_NEXT_VARIANT (t)
977 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
978 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
984 /* Restore decl state */
985 file_data->current_decl_state = file_data->global_decl_state;
987 pop_cfun ();
990 clear_line_info (data_in);
991 lto_data_in_delete (data_in);
995 /* Read the body of FN_DECL using DATA. FILE_DATA holds the global
996 decls and types. */
998 void
999 lto_input_function_body (struct lto_file_decl_data *file_data,
1000 tree fn_decl, const char *data)
1002 lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1006 /* Read the physical representation of a tree node with tag TAG from
1007 input block IB using the per-file context in DATA_IN. */
1009 static tree
1010 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1011 enum LTO_tags tag)
1013 /* Instantiate a new tree node. */
1014 tree result = streamer_alloc_tree (ib, data_in, tag);
1016 /* Enter RESULT in the reader cache. This will make RESULT
1017 available so that circular references in the rest of the tree
1018 structure can be resolved in subsequent calls to stream_read_tree. */
1019 streamer_tree_cache_append (data_in->reader_cache, result);
1021 /* Read all the bitfield values in RESULT. Note that for LTO, we
1022 only write language-independent bitfields, so no more unpacking is
1023 needed. */
1024 streamer_read_tree_bitfields (ib, data_in, result);
1026 /* Read all the pointer fields in RESULT. */
1027 streamer_read_tree_body (ib, data_in, result);
1029 /* Read any LTO-specific data not read by the tree streamer. */
1030 if (DECL_P (result)
1031 && TREE_CODE (result) != FUNCTION_DECL
1032 && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1033 DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1035 /* We should never try to instantiate an MD or NORMAL builtin here. */
1036 if (TREE_CODE (result) == FUNCTION_DECL)
1037 gcc_assert (!streamer_handle_as_builtin_p (result));
1039 /* end_marker = */ streamer_read_uchar (ib);
1041 #ifdef LTO_STREAMER_DEBUG
1042 /* Remove the mapping to RESULT's original address set by
1043 streamer_alloc_tree. */
1044 lto_orig_address_remove (result);
1045 #endif
1047 return result;
1051 /* Read a tree from input block IB using the per-file context in
1052 DATA_IN. This context is used, for example, to resolve references
1053 to previously read nodes. */
1055 tree
1056 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1058 enum LTO_tags tag;
1059 tree result;
1061 tag = streamer_read_record_start (ib);
1062 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1064 if (tag == LTO_null)
1065 result = NULL_TREE;
1066 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1068 /* If TAG is a reference to an indexable tree, the next value
1069 in IB is the index into the table where we expect to find
1070 that tree. */
1071 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1073 else if (tag == LTO_tree_pickle_reference)
1075 /* If TAG is a reference to a previously read tree, look it up in
1076 the reader cache. */
1077 result = streamer_get_pickled_tree (ib, data_in);
1079 else if (tag == LTO_builtin_decl)
1081 /* If we are going to read a built-in function, all we need is
1082 the code and class. */
1083 result = streamer_get_builtin_tree (ib, data_in);
1085 else if (tag == LTO_integer_cst)
1087 /* For shared integer constants we only need the type and its hi/low
1088 words. */
1089 result = streamer_read_integer_cst (ib, data_in);
1091 else
1093 /* Otherwise, materialize a new node from IB. */
1094 result = lto_read_tree (ib, data_in, tag);
1097 return result;
1101 /* Input toplevel asms. */
1103 void
1104 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1106 size_t len;
1107 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1108 NULL, &len);
1109 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1110 int string_offset;
1111 struct data_in *data_in;
1112 struct lto_input_block ib;
1113 tree str;
1115 if (! data)
1116 return;
1118 string_offset = sizeof (*header) + header->main_size;
1120 LTO_INIT_INPUT_BLOCK (ib,
1121 data + sizeof (*header),
1123 header->main_size);
1125 data_in = lto_data_in_create (file_data, data + string_offset,
1126 header->string_size, vNULL);
1128 /* Make sure the file was generated by the exact same compiler. */
1129 lto_check_version (header->lto_header.major_version,
1130 header->lto_header.minor_version);
1132 while ((str = streamer_read_string_cst (data_in, &ib)))
1134 struct asm_node *node = add_asm_node (str);
1135 node->order = streamer_read_hwi (&ib) + order_base;
1136 if (node->order >= symtab_order)
1137 symtab_order = node->order + 1;
1140 clear_line_info (data_in);
1141 lto_data_in_delete (data_in);
1143 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1147 /* Initialization for the LTO reader. */
1149 void
1150 lto_reader_init (void)
1152 lto_streamer_init ();
1153 file_name_hash_table = htab_create (37, hash_string_slot_node,
1154 eq_string_slot_node, free);
1158 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1159 table to use with LEN strings. RESOLUTIONS is the vector of linker
1160 resolutions (NULL if not using a linker plugin). */
1162 struct data_in *
1163 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1164 unsigned len,
1165 vec<ld_plugin_symbol_resolution_t> resolutions)
1167 struct data_in *data_in = XCNEW (struct data_in);
1168 data_in->file_data = file_data;
1169 data_in->strings = strings;
1170 data_in->strings_len = len;
1171 data_in->globals_resolution = resolutions;
1172 data_in->reader_cache = streamer_tree_cache_create ();
1174 return data_in;
1178 /* Remove DATA_IN. */
1180 void
1181 lto_data_in_delete (struct data_in *data_in)
1183 data_in->globals_resolution.release ();
1184 streamer_tree_cache_delete (data_in->reader_cache);
1185 free (data_in->labels);
1186 free (data_in);