re PR rtl-optimization/53589 (ICE in maybe_record_trace_start with asm goto)
[official-gcc.git] / gcc / lto-streamer-in.c
blobddb91d8557c5257812d333762a1f95e788c8a880
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 "libfuncs.h"
42 #include "except.h"
43 #include "debug.h"
44 #include "vec.h"
45 #include "timevar.h"
46 #include "ipa-utils.h"
47 #include "data-streamer.h"
48 #include "gimple-streamer.h"
49 #include "lto-streamer.h"
50 #include "tree-streamer.h"
51 #include "tree-pass.h"
52 #include "streamer-hooks.h"
54 /* The table to hold the file names. */
55 static htab_t file_name_hash_table;
58 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
59 number of valid tag values to check. */
61 void
62 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
64 va_list ap;
65 int i;
67 va_start (ap, ntags);
68 for (i = 0; i < ntags; i++)
69 if ((unsigned) actual == va_arg (ap, unsigned))
71 va_end (ap);
72 return;
75 va_end (ap);
76 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
80 /* Read LENGTH bytes from STREAM to ADDR. */
82 void
83 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
85 size_t i;
86 unsigned char *const buffer = (unsigned char *const) addr;
88 for (i = 0; i < length; i++)
89 buffer[i] = streamer_read_uchar (ib);
93 /* Lookup STRING in file_name_hash_table. If found, return the existing
94 string, otherwise insert STRING as the canonical version. */
96 static const char *
97 canon_file_name (const char *string)
99 void **slot;
100 struct string_slot s_slot;
101 size_t len = strlen (string);
103 s_slot.s = string;
104 s_slot.len = len;
106 slot = htab_find_slot (file_name_hash_table, &s_slot, INSERT);
107 if (*slot == NULL)
109 char *saved_string;
110 struct string_slot *new_slot;
112 saved_string = (char *) xmalloc (len + 1);
113 new_slot = XCNEW (struct string_slot);
114 memcpy (saved_string, string, len + 1);
115 new_slot->s = saved_string;
116 new_slot->len = len;
117 *slot = new_slot;
118 return saved_string;
120 else
122 struct string_slot *old_slot = (struct string_slot *) *slot;
123 return old_slot->s;
128 /* Clear the line info stored in DATA_IN. */
130 static void
131 clear_line_info (struct data_in *data_in)
133 if (data_in->current_file)
134 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
135 data_in->current_file = NULL;
136 data_in->current_line = 0;
137 data_in->current_col = 0;
141 /* Read a location bitpack from input block IB. */
143 static location_t
144 lto_input_location_bitpack (struct data_in *data_in, struct bitpack_d *bp)
146 bool file_change, line_change, column_change;
147 unsigned len;
148 bool prev_file = data_in->current_file != NULL;
150 if (bp_unpack_value (bp, 1))
151 return UNKNOWN_LOCATION;
153 file_change = bp_unpack_value (bp, 1);
154 if (file_change)
155 data_in->current_file = canon_file_name
156 (string_for_index (data_in,
157 bp_unpack_var_len_unsigned (bp),
158 &len));
160 line_change = bp_unpack_value (bp, 1);
161 if (line_change)
162 data_in->current_line = bp_unpack_var_len_unsigned (bp);
164 column_change = bp_unpack_value (bp, 1);
165 if (column_change)
166 data_in->current_col = bp_unpack_var_len_unsigned (bp);
168 if (file_change)
170 if (prev_file)
171 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
173 linemap_add (line_table, LC_ENTER, false, data_in->current_file,
174 data_in->current_line);
176 else if (line_change)
177 linemap_line_start (line_table, data_in->current_line, data_in->current_col);
179 return linemap_position_for_column (line_table, data_in->current_col);
183 /* Read a location from input block IB.
184 If the input_location streamer hook exists, call it.
185 Otherwise, proceed with reading the location from the
186 expanded location bitpack. */
188 location_t
189 lto_input_location (struct lto_input_block *ib, struct data_in *data_in)
191 if (streamer_hooks.input_location)
192 return streamer_hooks.input_location (ib, data_in);
193 else
195 struct bitpack_d bp;
197 bp = streamer_read_bitpack (ib);
198 return lto_input_location_bitpack (data_in, &bp);
203 /* Read a reference to a tree node from DATA_IN using input block IB.
204 TAG is the expected node that should be found in IB, if TAG belongs
205 to one of the indexable trees, expect to read a reference index to
206 be looked up in one of the symbol tables, otherwise read the pysical
207 representation of the tree using stream_read_tree. FN is the
208 function scope for the read tree. */
210 tree
211 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
212 struct function *fn, enum LTO_tags tag)
214 unsigned HOST_WIDE_INT ix_u;
215 tree result = NULL_TREE;
217 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
219 switch (tag)
221 case LTO_type_ref:
222 ix_u = streamer_read_uhwi (ib);
223 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
224 break;
226 case LTO_ssa_name_ref:
227 ix_u = streamer_read_uhwi (ib);
228 result = VEC_index (tree, SSANAMES (fn), ix_u);
229 break;
231 case LTO_field_decl_ref:
232 ix_u = streamer_read_uhwi (ib);
233 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
234 break;
236 case LTO_function_decl_ref:
237 ix_u = streamer_read_uhwi (ib);
238 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
239 break;
241 case LTO_type_decl_ref:
242 ix_u = streamer_read_uhwi (ib);
243 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
244 break;
246 case LTO_namespace_decl_ref:
247 ix_u = streamer_read_uhwi (ib);
248 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
249 break;
251 case LTO_global_decl_ref:
252 case LTO_result_decl_ref:
253 case LTO_const_decl_ref:
254 case LTO_imported_decl_ref:
255 case LTO_label_decl_ref:
256 case LTO_translation_unit_decl_ref:
257 ix_u = streamer_read_uhwi (ib);
258 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
259 break;
261 default:
262 gcc_unreachable ();
265 gcc_assert (result);
267 return result;
271 /* Read and return a double-linked list of catch handlers from input
272 block IB, using descriptors in DATA_IN. */
274 static struct eh_catch_d *
275 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
276 eh_catch *last_p)
278 eh_catch first;
279 enum LTO_tags tag;
281 *last_p = first = NULL;
282 tag = streamer_read_record_start (ib);
283 while (tag)
285 tree list;
286 eh_catch n;
288 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
290 /* Read the catch node. */
291 n = ggc_alloc_cleared_eh_catch_d ();
292 n->type_list = stream_read_tree (ib, data_in);
293 n->filter_list = stream_read_tree (ib, data_in);
294 n->label = stream_read_tree (ib, data_in);
296 /* Register all the types in N->FILTER_LIST. */
297 for (list = n->filter_list; list; list = TREE_CHAIN (list))
298 add_type_for_runtime (TREE_VALUE (list));
300 /* Chain N to the end of the list. */
301 if (*last_p)
302 (*last_p)->next_catch = n;
303 n->prev_catch = *last_p;
304 *last_p = n;
306 /* Set the head of the list the first time through the loop. */
307 if (first == NULL)
308 first = n;
310 tag = streamer_read_record_start (ib);
313 return first;
317 /* Read and return EH region IX from input block IB, using descriptors
318 in DATA_IN. */
320 static eh_region
321 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
323 enum LTO_tags tag;
324 eh_region r;
326 /* Read the region header. */
327 tag = streamer_read_record_start (ib);
328 if (tag == LTO_null)
329 return NULL;
331 r = ggc_alloc_cleared_eh_region_d ();
332 r->index = streamer_read_hwi (ib);
334 gcc_assert (r->index == ix);
336 /* Read all the region pointers as region numbers. We'll fix up
337 the pointers once the whole array has been read. */
338 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
339 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
340 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
342 switch (tag)
344 case LTO_ert_cleanup:
345 r->type = ERT_CLEANUP;
346 break;
348 case LTO_ert_try:
350 struct eh_catch_d *last_catch;
351 r->type = ERT_TRY;
352 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
353 &last_catch);
354 r->u.eh_try.last_catch = last_catch;
355 break;
358 case LTO_ert_allowed_exceptions:
360 tree l;
362 r->type = ERT_ALLOWED_EXCEPTIONS;
363 r->u.allowed.type_list = stream_read_tree (ib, data_in);
364 r->u.allowed.label = stream_read_tree (ib, data_in);
365 r->u.allowed.filter = streamer_read_uhwi (ib);
367 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
368 add_type_for_runtime (TREE_VALUE (l));
370 break;
372 case LTO_ert_must_not_throw:
373 r->type = ERT_MUST_NOT_THROW;
374 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
375 r->u.must_not_throw.failure_loc = lto_input_location (ib, data_in);
376 break;
378 default:
379 gcc_unreachable ();
382 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
384 return r;
388 /* Read and return EH landing pad IX from input block IB, using descriptors
389 in DATA_IN. */
391 static eh_landing_pad
392 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
394 enum LTO_tags tag;
395 eh_landing_pad lp;
397 /* Read the landing pad header. */
398 tag = streamer_read_record_start (ib);
399 if (tag == LTO_null)
400 return NULL;
402 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
404 lp = ggc_alloc_cleared_eh_landing_pad_d ();
405 lp->index = streamer_read_hwi (ib);
406 gcc_assert (lp->index == ix);
407 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
408 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
409 lp->post_landing_pad = stream_read_tree (ib, data_in);
411 return lp;
415 /* After reading the EH regions, pointers to peer and children regions
416 are region numbers. This converts all these region numbers into
417 real pointers into the rematerialized regions for FN. ROOT_REGION
418 is the region number for the root EH region in FN. */
420 static void
421 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
423 unsigned i;
424 VEC(eh_region,gc) *eh_array = fn->eh->region_array;
425 VEC(eh_landing_pad,gc) *lp_array = fn->eh->lp_array;
426 eh_region r;
427 eh_landing_pad lp;
429 gcc_assert (eh_array && lp_array);
431 gcc_assert (root_region >= 0);
432 fn->eh->region_tree = VEC_index (eh_region, eh_array, root_region);
434 #define FIXUP_EH_REGION(r) (r) = VEC_index (eh_region, eh_array, \
435 (HOST_WIDE_INT) (intptr_t) (r))
436 #define FIXUP_EH_LP(p) (p) = VEC_index (eh_landing_pad, lp_array, \
437 (HOST_WIDE_INT) (intptr_t) (p))
439 /* Convert all the index numbers stored in pointer fields into
440 pointers to the corresponding slots in the EH region array. */
441 FOR_EACH_VEC_ELT (eh_region, eh_array, i, r)
443 /* The array may contain NULL regions. */
444 if (r == NULL)
445 continue;
447 gcc_assert (i == (unsigned) r->index);
448 FIXUP_EH_REGION (r->outer);
449 FIXUP_EH_REGION (r->inner);
450 FIXUP_EH_REGION (r->next_peer);
451 FIXUP_EH_LP (r->landing_pads);
454 /* Convert all the index numbers stored in pointer fields into
455 pointers to the corresponding slots in the EH landing pad array. */
456 FOR_EACH_VEC_ELT (eh_landing_pad, lp_array, i, lp)
458 /* The array may contain NULL landing pads. */
459 if (lp == NULL)
460 continue;
462 gcc_assert (i == (unsigned) lp->index);
463 FIXUP_EH_LP (lp->next_lp);
464 FIXUP_EH_REGION (lp->region);
467 #undef FIXUP_EH_REGION
468 #undef FIXUP_EH_LP
472 /* Initialize EH support. */
474 void
475 lto_init_eh (void)
477 static bool eh_initialized_p = false;
479 if (eh_initialized_p)
480 return;
482 /* Contrary to most other FEs, we only initialize EH support when at
483 least one of the files in the set contains exception regions in
484 it. Since this happens much later than the call to init_eh in
485 lang_dependent_init, we have to set flag_exceptions and call
486 init_eh again to initialize the EH tables. */
487 flag_exceptions = 1;
488 init_eh ();
490 eh_initialized_p = true;
494 /* Read the exception table for FN from IB using the data descriptors
495 in DATA_IN. */
497 static void
498 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
499 struct function *fn)
501 HOST_WIDE_INT i, root_region, len;
502 enum LTO_tags tag;
504 tag = streamer_read_record_start (ib);
505 if (tag == LTO_null)
506 return;
508 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
510 /* If the file contains EH regions, then it was compiled with
511 -fexceptions. In that case, initialize the backend EH
512 machinery. */
513 lto_init_eh ();
515 gcc_assert (fn->eh);
517 root_region = streamer_read_hwi (ib);
518 gcc_assert (root_region == (int) root_region);
520 /* Read the EH region array. */
521 len = streamer_read_hwi (ib);
522 gcc_assert (len == (int) len);
523 if (len > 0)
525 VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
526 for (i = 0; i < len; i++)
528 eh_region r = input_eh_region (ib, data_in, i);
529 VEC_replace (eh_region, fn->eh->region_array, i, r);
533 /* Read the landing pads. */
534 len = streamer_read_hwi (ib);
535 gcc_assert (len == (int) len);
536 if (len > 0)
538 VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
539 for (i = 0; i < len; i++)
541 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
542 VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
546 /* Read the runtime type data. */
547 len = streamer_read_hwi (ib);
548 gcc_assert (len == (int) len);
549 if (len > 0)
551 VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
552 for (i = 0; i < len; i++)
554 tree ttype = stream_read_tree (ib, data_in);
555 VEC_replace (tree, fn->eh->ttype_data, i, ttype);
559 /* Read the table of action chains. */
560 len = streamer_read_hwi (ib);
561 gcc_assert (len == (int) len);
562 if (len > 0)
564 if (targetm.arm_eabi_unwinder)
566 VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
567 for (i = 0; i < len; i++)
569 tree t = stream_read_tree (ib, data_in);
570 VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
573 else
575 VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
576 for (i = 0; i < len; i++)
578 uchar c = streamer_read_uchar (ib);
579 VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
584 /* Reconstruct the EH region tree by fixing up the peer/children
585 pointers. */
586 fixup_eh_region_pointers (fn, root_region);
588 tag = streamer_read_record_start (ib);
589 lto_tag_check_range (tag, LTO_null, LTO_null);
593 /* Make a new basic block with index INDEX in function FN. */
595 static basic_block
596 make_new_block (struct function *fn, unsigned int index)
598 basic_block bb = alloc_block ();
599 bb->index = index;
600 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
601 n_basic_blocks_for_function (fn)++;
602 return bb;
606 /* Read the CFG for function FN from input block IB. */
608 static void
609 input_cfg (struct lto_input_block *ib, struct function *fn,
610 int count_materialization_scale)
612 unsigned int bb_count;
613 basic_block p_bb;
614 unsigned int i;
615 int index;
617 init_empty_tree_cfg_for_function (fn);
618 init_ssa_operands (fn);
620 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
621 PROFILE_LAST);
623 bb_count = streamer_read_uhwi (ib);
625 last_basic_block_for_function (fn) = bb_count;
626 if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
627 VEC_safe_grow_cleared (basic_block, gc,
628 basic_block_info_for_function (fn), bb_count);
630 if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
631 VEC_safe_grow_cleared (basic_block, gc,
632 label_to_block_map_for_function (fn), bb_count);
634 index = streamer_read_hwi (ib);
635 while (index != -1)
637 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
638 unsigned int edge_count;
640 if (bb == NULL)
641 bb = make_new_block (fn, index);
643 edge_count = streamer_read_uhwi (ib);
645 /* Connect up the CFG. */
646 for (i = 0; i < edge_count; i++)
648 unsigned int dest_index;
649 unsigned int edge_flags;
650 basic_block dest;
651 int probability;
652 gcov_type count;
653 edge e;
655 dest_index = streamer_read_uhwi (ib);
656 probability = (int) streamer_read_hwi (ib);
657 count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
658 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
659 edge_flags = streamer_read_uhwi (ib);
661 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
663 if (dest == NULL)
664 dest = make_new_block (fn, dest_index);
666 e = make_edge (bb, dest, edge_flags);
667 e->probability = probability;
668 e->count = count;
671 index = streamer_read_hwi (ib);
674 p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
675 index = streamer_read_hwi (ib);
676 while (index != -1)
678 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
679 bb->prev_bb = p_bb;
680 p_bb->next_bb = bb;
681 p_bb = bb;
682 index = streamer_read_hwi (ib);
687 /* Read the SSA names array for function FN from DATA_IN using input
688 block IB. */
690 static void
691 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
692 struct function *fn)
694 unsigned int i, size;
696 size = streamer_read_uhwi (ib);
697 init_ssanames (fn, size);
699 i = streamer_read_uhwi (ib);
700 while (i)
702 tree ssa_name, name;
703 bool is_default_def;
705 /* Skip over the elements that had been freed. */
706 while (VEC_length (tree, SSANAMES (fn)) < i)
707 VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
709 is_default_def = (streamer_read_uchar (ib) != 0);
710 name = stream_read_tree (ib, data_in);
711 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
713 if (is_default_def)
714 set_default_def (SSA_NAME_VAR (ssa_name), ssa_name);
716 i = streamer_read_uhwi (ib);
721 /* Go through all NODE edges and fixup call_stmt pointers
722 so they point to STMTS. */
724 static void
725 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
727 struct cgraph_edge *cedge;
728 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
729 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
730 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
731 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
734 /* Fixup call_stmt pointers in NODE and all clones. */
736 static void
737 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
739 struct cgraph_node *node;
741 while (orig->clone_of)
742 orig = orig->clone_of;
744 fixup_call_stmt_edges_1 (orig, stmts);
745 if (orig->clones)
746 for (node = orig->clones; node != orig;)
748 fixup_call_stmt_edges_1 (node, stmts);
749 if (node->clones)
750 node = node->clones;
751 else if (node->next_sibling_clone)
752 node = node->next_sibling_clone;
753 else
755 while (node != orig && !node->next_sibling_clone)
756 node = node->clone_of;
757 if (node != orig)
758 node = node->next_sibling_clone;
764 /* Input the base body of struct function FN from DATA_IN
765 using input block IB. */
767 static void
768 input_struct_function_base (struct function *fn, struct data_in *data_in,
769 struct lto_input_block *ib)
771 struct bitpack_d bp;
772 int len;
774 /* Read the static chain and non-local goto save area. */
775 fn->static_chain_decl = stream_read_tree (ib, data_in);
776 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
778 /* Read all the local symbols. */
779 len = streamer_read_hwi (ib);
780 if (len > 0)
782 int i;
783 VEC_safe_grow (tree, gc, fn->local_decls, len);
784 for (i = 0; i < len; i++)
786 tree t = stream_read_tree (ib, data_in);
787 VEC_replace (tree, fn->local_decls, i, t);
791 /* Input the function start and end loci. */
792 fn->function_start_locus = lto_input_location (ib, data_in);
793 fn->function_end_locus = lto_input_location (ib, data_in);
795 /* Input the current IL state of the function. */
796 fn->curr_properties = streamer_read_uhwi (ib);
798 /* Read all the attributes for FN. */
799 bp = streamer_read_bitpack (ib);
800 fn->is_thunk = bp_unpack_value (&bp, 1);
801 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
802 fn->after_tree_profile = bp_unpack_value (&bp, 1);
803 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
804 fn->returns_struct = bp_unpack_value (&bp, 1);
805 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
806 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
807 fn->after_inlining = bp_unpack_value (&bp, 1);
808 fn->stdarg = bp_unpack_value (&bp, 1);
809 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
810 fn->calls_alloca = bp_unpack_value (&bp, 1);
811 fn->calls_setjmp = bp_unpack_value (&bp, 1);
812 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
813 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
817 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
819 static void
820 input_function (tree fn_decl, struct data_in *data_in,
821 struct lto_input_block *ib)
823 struct function *fn;
824 enum LTO_tags tag;
825 gimple *stmts;
826 basic_block bb;
827 struct cgraph_node *node;
828 tree args, narg, oarg;
830 fn = DECL_STRUCT_FUNCTION (fn_decl);
831 tag = streamer_read_record_start (ib);
832 clear_line_info (data_in);
834 gimple_register_cfg_hooks ();
835 lto_tag_check (tag, LTO_function);
837 input_struct_function_base (fn, data_in, ib);
839 /* Read all function arguments. We need to re-map them here to the
840 arguments of the merged function declaration. */
841 args = stream_read_tree (ib, data_in);
842 for (oarg = args, narg = DECL_ARGUMENTS (fn_decl);
843 oarg && narg;
844 oarg = TREE_CHAIN (oarg), narg = TREE_CHAIN (narg))
846 unsigned ix;
847 bool res;
848 res = streamer_tree_cache_lookup (data_in->reader_cache, oarg, &ix);
849 gcc_assert (res);
850 /* Replace the argument in the streamer cache. */
851 streamer_tree_cache_insert_at (data_in->reader_cache, narg, ix);
853 gcc_assert (!oarg && !narg);
855 /* Read all the SSA names. */
856 input_ssa_names (ib, data_in, fn);
858 /* Read the exception handling regions in the function. */
859 input_eh_regions (ib, data_in, fn);
861 /* Read the tree of lexical scopes for the function. */
862 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
863 gcc_assert (DECL_INITIAL (fn_decl));
864 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
865 node = cgraph_get_create_node (fn_decl);
867 /* Read all the basic blocks. */
868 tag = streamer_read_record_start (ib);
869 while (tag)
871 input_bb (ib, tag, data_in, fn,
872 node->count_materialization_scale);
873 tag = streamer_read_record_start (ib);
876 /* Fix up the call statements that are mentioned in the callgraph
877 edges. */
878 set_gimple_stmt_max_uid (cfun, 0);
879 FOR_ALL_BB (bb)
881 gimple_stmt_iterator gsi;
882 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
884 gimple stmt = gsi_stmt (gsi);
885 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
888 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
889 FOR_ALL_BB (bb)
891 gimple_stmt_iterator bsi = gsi_start_bb (bb);
892 while (!gsi_end_p (bsi))
894 gimple stmt = gsi_stmt (bsi);
895 /* If we're recompiling LTO objects with debug stmts but
896 we're not supposed to have debug stmts, remove them now.
897 We can't remove them earlier because this would cause uid
898 mismatches in fixups, but we can do it at this point, as
899 long as debug stmts don't require fixups. */
900 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
902 gimple_stmt_iterator gsi = bsi;
903 gsi_next (&bsi);
904 gsi_remove (&gsi, true);
906 else
908 gsi_next (&bsi);
909 stmts[gimple_uid (stmt)] = stmt;
914 /* Set the gimple body to the statement sequence in the entry
915 basic block. FIXME lto, this is fairly hacky. The existence
916 of a gimple body is used by the cgraph routines, but we should
917 really use the presence of the CFG. */
919 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
920 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
923 fixup_call_stmt_edges (node, stmts);
924 execute_all_ipa_stmt_fixups (node, stmts);
926 update_ssa (TODO_update_ssa_only_virtuals);
927 free_dominance_info (CDI_DOMINATORS);
928 free_dominance_info (CDI_POST_DOMINATORS);
929 free (stmts);
933 /* Read the body from DATA for function FN_DECL and fill it in.
934 FILE_DATA are the global decls and types. SECTION_TYPE is either
935 LTO_section_function_body or LTO_section_static_initializer. If
936 section type is LTO_section_function_body, FN must be the decl for
937 that function. */
939 static void
940 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
941 const char *data, enum lto_section_type section_type)
943 const struct lto_function_header *header;
944 struct data_in *data_in;
945 int cfg_offset;
946 int main_offset;
947 int string_offset;
948 struct lto_input_block ib_cfg;
949 struct lto_input_block ib_main;
951 header = (const struct lto_function_header *) data;
952 cfg_offset = sizeof (struct lto_function_header);
953 main_offset = cfg_offset + header->cfg_size;
954 string_offset = main_offset + header->main_size;
956 LTO_INIT_INPUT_BLOCK (ib_cfg,
957 data + cfg_offset,
959 header->cfg_size);
961 LTO_INIT_INPUT_BLOCK (ib_main,
962 data + main_offset,
964 header->main_size);
966 data_in = lto_data_in_create (file_data, data + string_offset,
967 header->string_size, NULL);
969 /* Make sure the file was generated by the exact same compiler. */
970 lto_check_version (header->lto_header.major_version,
971 header->lto_header.minor_version);
973 if (section_type == LTO_section_function_body)
975 struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
976 struct lto_in_decl_state *decl_state;
977 struct cgraph_node *node = cgraph_get_node (fn_decl);
978 unsigned from;
980 gcc_checking_assert (node);
981 push_cfun (fn);
982 init_tree_ssa (fn);
984 /* Use the function's decl state. */
985 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
986 gcc_assert (decl_state);
987 file_data->current_decl_state = decl_state;
989 input_cfg (&ib_cfg, fn, node->count_materialization_scale);
991 /* Set up the struct function. */
992 from = VEC_length (tree, data_in->reader_cache->nodes);
993 input_function (fn_decl, data_in, &ib_main);
994 /* And fixup types we streamed locally. */
996 struct streamer_tree_cache_d *cache = data_in->reader_cache;
997 unsigned len = VEC_length (tree, cache->nodes);
998 unsigned i;
999 for (i = len; i-- > from;)
1001 tree t = VEC_index (tree, cache->nodes, i);
1002 if (t == NULL_TREE)
1003 continue;
1005 if (TYPE_P (t))
1007 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1008 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1009 if (TYPE_MAIN_VARIANT (t) != t)
1011 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1012 TYPE_NEXT_VARIANT (t)
1013 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1014 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1020 /* We should now be in SSA. */
1021 cfun->gimple_df->in_ssa_p = true;
1023 /* Restore decl state */
1024 file_data->current_decl_state = file_data->global_decl_state;
1026 pop_cfun ();
1029 clear_line_info (data_in);
1030 lto_data_in_delete (data_in);
1034 /* Read the body of FN_DECL using DATA. FILE_DATA holds the global
1035 decls and types. */
1037 void
1038 lto_input_function_body (struct lto_file_decl_data *file_data,
1039 tree fn_decl, const char *data)
1041 current_function_decl = fn_decl;
1042 lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1046 /* Read the physical representation of a tree node with tag TAG from
1047 input block IB using the per-file context in DATA_IN. */
1049 static tree
1050 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1051 enum LTO_tags tag)
1053 /* Instantiate a new tree node. */
1054 tree result = streamer_alloc_tree (ib, data_in, tag);
1056 /* Enter RESULT in the reader cache. This will make RESULT
1057 available so that circular references in the rest of the tree
1058 structure can be resolved in subsequent calls to stream_read_tree. */
1059 streamer_tree_cache_append (data_in->reader_cache, result);
1061 /* Read all the bitfield values in RESULT. Note that for LTO, we
1062 only write language-independent bitfields, so no more unpacking is
1063 needed. */
1064 streamer_read_tree_bitfields (ib, result);
1066 /* Read all the pointer fields in RESULT. */
1067 streamer_read_tree_body (ib, data_in, result);
1069 /* Read any LTO-specific data not read by the tree streamer. */
1070 if (DECL_P (result)
1071 && TREE_CODE (result) != FUNCTION_DECL
1072 && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1073 DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1075 /* We should never try to instantiate an MD or NORMAL builtin here. */
1076 if (TREE_CODE (result) == FUNCTION_DECL)
1077 gcc_assert (!streamer_handle_as_builtin_p (result));
1079 /* end_marker = */ streamer_read_uchar (ib);
1081 #ifdef LTO_STREAMER_DEBUG
1082 /* Remove the mapping to RESULT's original address set by
1083 streamer_alloc_tree. */
1084 lto_orig_address_remove (result);
1085 #endif
1087 return result;
1091 /* Read a tree from input block IB using the per-file context in
1092 DATA_IN. This context is used, for example, to resolve references
1093 to previously read nodes. */
1095 tree
1096 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1098 enum LTO_tags tag;
1099 tree result;
1101 tag = streamer_read_record_start (ib);
1102 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1104 if (tag == LTO_null)
1105 result = NULL_TREE;
1106 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1108 /* If TAG is a reference to an indexable tree, the next value
1109 in IB is the index into the table where we expect to find
1110 that tree. */
1111 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1113 else if (tag == LTO_tree_pickle_reference)
1115 /* If TAG is a reference to a previously read tree, look it up in
1116 the reader cache. */
1117 result = streamer_get_pickled_tree (ib, data_in);
1119 else if (tag == LTO_builtin_decl)
1121 /* If we are going to read a built-in function, all we need is
1122 the code and class. */
1123 result = streamer_get_builtin_tree (ib, data_in);
1125 else if (tag == lto_tree_code_to_tag (INTEGER_CST))
1127 /* For integer constants we only need the type and its hi/low
1128 words. */
1129 result = streamer_read_integer_cst (ib, data_in);
1131 else
1133 /* Otherwise, materialize a new node from IB. */
1134 result = lto_read_tree (ib, data_in, tag);
1137 return result;
1141 /* Input toplevel asms. */
1143 void
1144 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1146 size_t len;
1147 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1148 NULL, &len);
1149 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1150 int string_offset;
1151 struct data_in *data_in;
1152 struct lto_input_block ib;
1153 tree str;
1155 if (! data)
1156 return;
1158 string_offset = sizeof (*header) + header->main_size;
1160 LTO_INIT_INPUT_BLOCK (ib,
1161 data + sizeof (*header),
1163 header->main_size);
1165 data_in = lto_data_in_create (file_data, data + string_offset,
1166 header->string_size, NULL);
1168 /* Make sure the file was generated by the exact same compiler. */
1169 lto_check_version (header->lto_header.major_version,
1170 header->lto_header.minor_version);
1172 while ((str = streamer_read_string_cst (data_in, &ib)))
1174 struct asm_node *node = add_asm_node (str);
1175 node->order = streamer_read_hwi (&ib) + order_base;
1176 if (node->order >= symtab_order)
1177 symtab_order = node->order + 1;
1180 clear_line_info (data_in);
1181 lto_data_in_delete (data_in);
1183 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1187 /* Initialization for the LTO reader. */
1189 void
1190 lto_reader_init (void)
1192 lto_streamer_init ();
1193 file_name_hash_table = htab_create (37, hash_string_slot_node,
1194 eq_string_slot_node, free);
1198 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1199 table to use with LEN strings. RESOLUTIONS is the vector of linker
1200 resolutions (NULL if not using a linker plugin). */
1202 struct data_in *
1203 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1204 unsigned len,
1205 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
1207 struct data_in *data_in = XCNEW (struct data_in);
1208 data_in->file_data = file_data;
1209 data_in->strings = strings;
1210 data_in->strings_len = len;
1211 data_in->globals_resolution = resolutions;
1212 data_in->reader_cache = streamer_tree_cache_create ();
1214 return data_in;
1218 /* Remove DATA_IN. */
1220 void
1221 lto_data_in_delete (struct data_in *data_in)
1223 VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
1224 streamer_tree_cache_delete (data_in->reader_cache);
1225 free (data_in->labels);
1226 free (data_in);