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