2012-07-24 Roland McGrath <mcgrathr@google.com>
[official-gcc.git] / gcc / lto-streamer-in.c
blobc7257d9be16a769337b7d53743304fddb1661443
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 "ipa-utils.h"
46 #include "data-streamer.h"
47 #include "gimple-streamer.h"
48 #include "lto-streamer.h"
49 #include "tree-streamer.h"
50 #include "tree-pass.h"
51 #include "streamer-hooks.h"
53 /* The table to hold the file names. */
54 static htab_t file_name_hash_table;
57 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
58 number of valid tag values to check. */
60 void
61 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
63 va_list ap;
64 int i;
66 va_start (ap, ntags);
67 for (i = 0; i < ntags; i++)
68 if ((unsigned) actual == va_arg (ap, unsigned))
70 va_end (ap);
71 return;
74 va_end (ap);
75 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
79 /* Read LENGTH bytes from STREAM to ADDR. */
81 void
82 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
84 size_t i;
85 unsigned char *const buffer = (unsigned char *const) addr;
87 for (i = 0; i < length; i++)
88 buffer[i] = streamer_read_uchar (ib);
92 /* Lookup STRING in file_name_hash_table. If found, return the existing
93 string, otherwise insert STRING as the canonical version. */
95 static const char *
96 canon_file_name (const char *string)
98 void **slot;
99 struct string_slot s_slot;
100 size_t len = strlen (string);
102 s_slot.s = string;
103 s_slot.len = len;
105 slot = htab_find_slot (file_name_hash_table, &s_slot, INSERT);
106 if (*slot == NULL)
108 char *saved_string;
109 struct string_slot *new_slot;
111 saved_string = (char *) xmalloc (len + 1);
112 new_slot = XCNEW (struct string_slot);
113 memcpy (saved_string, string, len + 1);
114 new_slot->s = saved_string;
115 new_slot->len = len;
116 *slot = new_slot;
117 return saved_string;
119 else
121 struct string_slot *old_slot = (struct string_slot *) *slot;
122 return old_slot->s;
127 /* Clear the line info stored in DATA_IN. */
129 static void
130 clear_line_info (struct data_in *data_in)
132 if (data_in->current_file)
133 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
134 data_in->current_file = NULL;
135 data_in->current_line = 0;
136 data_in->current_col = 0;
140 /* Read a location bitpack from input block IB. */
142 static location_t
143 lto_input_location_bitpack (struct data_in *data_in, struct bitpack_d *bp)
145 bool file_change, line_change, column_change;
146 unsigned len;
147 bool prev_file = data_in->current_file != NULL;
149 if (bp_unpack_value (bp, 1))
150 return UNKNOWN_LOCATION;
152 file_change = bp_unpack_value (bp, 1);
153 if (file_change)
154 data_in->current_file = canon_file_name
155 (string_for_index (data_in,
156 bp_unpack_var_len_unsigned (bp),
157 &len));
159 line_change = bp_unpack_value (bp, 1);
160 if (line_change)
161 data_in->current_line = bp_unpack_var_len_unsigned (bp);
163 column_change = bp_unpack_value (bp, 1);
164 if (column_change)
165 data_in->current_col = bp_unpack_var_len_unsigned (bp);
167 if (file_change)
169 if (prev_file)
170 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
172 linemap_add (line_table, LC_ENTER, false, data_in->current_file,
173 data_in->current_line);
175 else if (line_change)
176 linemap_line_start (line_table, data_in->current_line, data_in->current_col);
178 return linemap_position_for_column (line_table, data_in->current_col);
182 /* Read a location from input block IB.
183 If the input_location streamer hook exists, call it.
184 Otherwise, proceed with reading the location from the
185 expanded location bitpack. */
187 location_t
188 lto_input_location (struct lto_input_block *ib, struct data_in *data_in)
190 if (streamer_hooks.input_location)
191 return streamer_hooks.input_location (ib, data_in);
192 else
194 struct bitpack_d bp;
196 bp = streamer_read_bitpack (ib);
197 return lto_input_location_bitpack (data_in, &bp);
202 /* Read a reference to a tree node from DATA_IN using input block IB.
203 TAG is the expected node that should be found in IB, if TAG belongs
204 to one of the indexable trees, expect to read a reference index to
205 be looked up in one of the symbol tables, otherwise read the pysical
206 representation of the tree using stream_read_tree. FN is the
207 function scope for the read tree. */
209 tree
210 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
211 struct function *fn, enum LTO_tags tag)
213 unsigned HOST_WIDE_INT ix_u;
214 tree result = NULL_TREE;
216 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
218 switch (tag)
220 case LTO_type_ref:
221 ix_u = streamer_read_uhwi (ib);
222 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
223 break;
225 case LTO_ssa_name_ref:
226 ix_u = streamer_read_uhwi (ib);
227 result = VEC_index (tree, SSANAMES (fn), ix_u);
228 break;
230 case LTO_field_decl_ref:
231 ix_u = streamer_read_uhwi (ib);
232 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
233 break;
235 case LTO_function_decl_ref:
236 ix_u = streamer_read_uhwi (ib);
237 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
238 break;
240 case LTO_type_decl_ref:
241 ix_u = streamer_read_uhwi (ib);
242 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
243 break;
245 case LTO_namespace_decl_ref:
246 ix_u = streamer_read_uhwi (ib);
247 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
248 break;
250 case LTO_global_decl_ref:
251 case LTO_result_decl_ref:
252 case LTO_const_decl_ref:
253 case LTO_imported_decl_ref:
254 case LTO_label_decl_ref:
255 case LTO_translation_unit_decl_ref:
256 ix_u = streamer_read_uhwi (ib);
257 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
258 break;
260 default:
261 gcc_unreachable ();
264 gcc_assert (result);
266 return result;
270 /* Read and return a double-linked list of catch handlers from input
271 block IB, using descriptors in DATA_IN. */
273 static struct eh_catch_d *
274 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
275 eh_catch *last_p)
277 eh_catch first;
278 enum LTO_tags tag;
280 *last_p = first = NULL;
281 tag = streamer_read_record_start (ib);
282 while (tag)
284 tree list;
285 eh_catch n;
287 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
289 /* Read the catch node. */
290 n = ggc_alloc_cleared_eh_catch_d ();
291 n->type_list = stream_read_tree (ib, data_in);
292 n->filter_list = stream_read_tree (ib, data_in);
293 n->label = stream_read_tree (ib, data_in);
295 /* Register all the types in N->FILTER_LIST. */
296 for (list = n->filter_list; list; list = TREE_CHAIN (list))
297 add_type_for_runtime (TREE_VALUE (list));
299 /* Chain N to the end of the list. */
300 if (*last_p)
301 (*last_p)->next_catch = n;
302 n->prev_catch = *last_p;
303 *last_p = n;
305 /* Set the head of the list the first time through the loop. */
306 if (first == NULL)
307 first = n;
309 tag = streamer_read_record_start (ib);
312 return first;
316 /* Read and return EH region IX from input block IB, using descriptors
317 in DATA_IN. */
319 static eh_region
320 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
322 enum LTO_tags tag;
323 eh_region r;
325 /* Read the region header. */
326 tag = streamer_read_record_start (ib);
327 if (tag == LTO_null)
328 return NULL;
330 r = ggc_alloc_cleared_eh_region_d ();
331 r->index = streamer_read_hwi (ib);
333 gcc_assert (r->index == ix);
335 /* Read all the region pointers as region numbers. We'll fix up
336 the pointers once the whole array has been read. */
337 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
338 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
339 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
341 switch (tag)
343 case LTO_ert_cleanup:
344 r->type = ERT_CLEANUP;
345 break;
347 case LTO_ert_try:
349 struct eh_catch_d *last_catch;
350 r->type = ERT_TRY;
351 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
352 &last_catch);
353 r->u.eh_try.last_catch = last_catch;
354 break;
357 case LTO_ert_allowed_exceptions:
359 tree l;
361 r->type = ERT_ALLOWED_EXCEPTIONS;
362 r->u.allowed.type_list = stream_read_tree (ib, data_in);
363 r->u.allowed.label = stream_read_tree (ib, data_in);
364 r->u.allowed.filter = streamer_read_uhwi (ib);
366 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
367 add_type_for_runtime (TREE_VALUE (l));
369 break;
371 case LTO_ert_must_not_throw:
372 r->type = ERT_MUST_NOT_THROW;
373 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
374 r->u.must_not_throw.failure_loc = lto_input_location (ib, data_in);
375 break;
377 default:
378 gcc_unreachable ();
381 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
383 return r;
387 /* Read and return EH landing pad IX from input block IB, using descriptors
388 in DATA_IN. */
390 static eh_landing_pad
391 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
393 enum LTO_tags tag;
394 eh_landing_pad lp;
396 /* Read the landing pad header. */
397 tag = streamer_read_record_start (ib);
398 if (tag == LTO_null)
399 return NULL;
401 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
403 lp = ggc_alloc_cleared_eh_landing_pad_d ();
404 lp->index = streamer_read_hwi (ib);
405 gcc_assert (lp->index == ix);
406 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
407 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
408 lp->post_landing_pad = stream_read_tree (ib, data_in);
410 return lp;
414 /* After reading the EH regions, pointers to peer and children regions
415 are region numbers. This converts all these region numbers into
416 real pointers into the rematerialized regions for FN. ROOT_REGION
417 is the region number for the root EH region in FN. */
419 static void
420 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
422 unsigned i;
423 VEC(eh_region,gc) *eh_array = fn->eh->region_array;
424 VEC(eh_landing_pad,gc) *lp_array = fn->eh->lp_array;
425 eh_region r;
426 eh_landing_pad lp;
428 gcc_assert (eh_array && lp_array);
430 gcc_assert (root_region >= 0);
431 fn->eh->region_tree = VEC_index (eh_region, eh_array, root_region);
433 #define FIXUP_EH_REGION(r) (r) = VEC_index (eh_region, eh_array, \
434 (HOST_WIDE_INT) (intptr_t) (r))
435 #define FIXUP_EH_LP(p) (p) = VEC_index (eh_landing_pad, lp_array, \
436 (HOST_WIDE_INT) (intptr_t) (p))
438 /* Convert all the index numbers stored in pointer fields into
439 pointers to the corresponding slots in the EH region array. */
440 FOR_EACH_VEC_ELT (eh_region, eh_array, i, r)
442 /* The array may contain NULL regions. */
443 if (r == NULL)
444 continue;
446 gcc_assert (i == (unsigned) r->index);
447 FIXUP_EH_REGION (r->outer);
448 FIXUP_EH_REGION (r->inner);
449 FIXUP_EH_REGION (r->next_peer);
450 FIXUP_EH_LP (r->landing_pads);
453 /* Convert all the index numbers stored in pointer fields into
454 pointers to the corresponding slots in the EH landing pad array. */
455 FOR_EACH_VEC_ELT (eh_landing_pad, lp_array, i, lp)
457 /* The array may contain NULL landing pads. */
458 if (lp == NULL)
459 continue;
461 gcc_assert (i == (unsigned) lp->index);
462 FIXUP_EH_LP (lp->next_lp);
463 FIXUP_EH_REGION (lp->region);
466 #undef FIXUP_EH_REGION
467 #undef FIXUP_EH_LP
471 /* Initialize EH support. */
473 void
474 lto_init_eh (void)
476 static bool eh_initialized_p = false;
478 if (eh_initialized_p)
479 return;
481 /* Contrary to most other FEs, we only initialize EH support when at
482 least one of the files in the set contains exception regions in
483 it. Since this happens much later than the call to init_eh in
484 lang_dependent_init, we have to set flag_exceptions and call
485 init_eh again to initialize the EH tables. */
486 flag_exceptions = 1;
487 init_eh ();
489 eh_initialized_p = true;
493 /* Read the exception table for FN from IB using the data descriptors
494 in DATA_IN. */
496 static void
497 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
498 struct function *fn)
500 HOST_WIDE_INT i, root_region, len;
501 enum LTO_tags tag;
503 tag = streamer_read_record_start (ib);
504 if (tag == LTO_null)
505 return;
507 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
509 /* If the file contains EH regions, then it was compiled with
510 -fexceptions. In that case, initialize the backend EH
511 machinery. */
512 lto_init_eh ();
514 gcc_assert (fn->eh);
516 root_region = streamer_read_hwi (ib);
517 gcc_assert (root_region == (int) root_region);
519 /* Read the EH region array. */
520 len = streamer_read_hwi (ib);
521 gcc_assert (len == (int) len);
522 if (len > 0)
524 VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
525 for (i = 0; i < len; i++)
527 eh_region r = input_eh_region (ib, data_in, i);
528 VEC_replace (eh_region, fn->eh->region_array, i, r);
532 /* Read the landing pads. */
533 len = streamer_read_hwi (ib);
534 gcc_assert (len == (int) len);
535 if (len > 0)
537 VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
538 for (i = 0; i < len; i++)
540 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
541 VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
545 /* Read the runtime type data. */
546 len = streamer_read_hwi (ib);
547 gcc_assert (len == (int) len);
548 if (len > 0)
550 VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
551 for (i = 0; i < len; i++)
553 tree ttype = stream_read_tree (ib, data_in);
554 VEC_replace (tree, fn->eh->ttype_data, i, ttype);
558 /* Read the table of action chains. */
559 len = streamer_read_hwi (ib);
560 gcc_assert (len == (int) len);
561 if (len > 0)
563 if (targetm.arm_eabi_unwinder)
565 VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
566 for (i = 0; i < len; i++)
568 tree t = stream_read_tree (ib, data_in);
569 VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
572 else
574 VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
575 for (i = 0; i < len; i++)
577 uchar c = streamer_read_uchar (ib);
578 VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
583 /* Reconstruct the EH region tree by fixing up the peer/children
584 pointers. */
585 fixup_eh_region_pointers (fn, root_region);
587 tag = streamer_read_record_start (ib);
588 lto_tag_check_range (tag, LTO_null, LTO_null);
592 /* Make a new basic block with index INDEX in function FN. */
594 static basic_block
595 make_new_block (struct function *fn, unsigned int index)
597 basic_block bb = alloc_block ();
598 bb->index = index;
599 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
600 n_basic_blocks_for_function (fn)++;
601 return bb;
605 /* Read the CFG for function FN from input block IB. */
607 static void
608 input_cfg (struct lto_input_block *ib, struct function *fn,
609 int count_materialization_scale)
611 unsigned int bb_count;
612 basic_block p_bb;
613 unsigned int i;
614 int index;
616 init_empty_tree_cfg_for_function (fn);
617 init_ssa_operands (fn);
619 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
620 PROFILE_LAST);
622 bb_count = streamer_read_uhwi (ib);
624 last_basic_block_for_function (fn) = bb_count;
625 if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
626 VEC_safe_grow_cleared (basic_block, gc,
627 basic_block_info_for_function (fn), bb_count);
629 if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
630 VEC_safe_grow_cleared (basic_block, gc,
631 label_to_block_map_for_function (fn), bb_count);
633 index = streamer_read_hwi (ib);
634 while (index != -1)
636 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
637 unsigned int edge_count;
639 if (bb == NULL)
640 bb = make_new_block (fn, index);
642 edge_count = streamer_read_uhwi (ib);
644 /* Connect up the CFG. */
645 for (i = 0; i < edge_count; i++)
647 unsigned int dest_index;
648 unsigned int edge_flags;
649 basic_block dest;
650 int probability;
651 gcov_type count;
652 edge e;
654 dest_index = streamer_read_uhwi (ib);
655 probability = (int) streamer_read_hwi (ib);
656 count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
657 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
658 edge_flags = streamer_read_uhwi (ib);
660 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
662 if (dest == NULL)
663 dest = make_new_block (fn, dest_index);
665 e = make_edge (bb, dest, edge_flags);
666 e->probability = probability;
667 e->count = count;
670 index = streamer_read_hwi (ib);
673 p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
674 index = streamer_read_hwi (ib);
675 while (index != -1)
677 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
678 bb->prev_bb = p_bb;
679 p_bb->next_bb = bb;
680 p_bb = bb;
681 index = streamer_read_hwi (ib);
686 /* Read the SSA names array for function FN from DATA_IN using input
687 block IB. */
689 static void
690 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
691 struct function *fn)
693 unsigned int i, size;
695 size = streamer_read_uhwi (ib);
696 init_ssanames (fn, size);
698 i = streamer_read_uhwi (ib);
699 while (i)
701 tree ssa_name, name;
702 bool is_default_def;
704 /* Skip over the elements that had been freed. */
705 while (VEC_length (tree, SSANAMES (fn)) < i)
706 VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
708 is_default_def = (streamer_read_uchar (ib) != 0);
709 name = stream_read_tree (ib, data_in);
710 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
712 if (is_default_def)
713 set_default_def (SSA_NAME_VAR (ssa_name), ssa_name);
715 i = streamer_read_uhwi (ib);
720 /* Go through all NODE edges and fixup call_stmt pointers
721 so they point to STMTS. */
723 static void
724 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
726 struct cgraph_edge *cedge;
727 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
728 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
729 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
730 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
733 /* Fixup call_stmt pointers in NODE and all clones. */
735 static void
736 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
738 struct cgraph_node *node;
740 while (orig->clone_of)
741 orig = orig->clone_of;
743 fixup_call_stmt_edges_1 (orig, stmts);
744 if (orig->clones)
745 for (node = orig->clones; node != orig;)
747 fixup_call_stmt_edges_1 (node, stmts);
748 if (node->clones)
749 node = node->clones;
750 else if (node->next_sibling_clone)
751 node = node->next_sibling_clone;
752 else
754 while (node != orig && !node->next_sibling_clone)
755 node = node->clone_of;
756 if (node != orig)
757 node = node->next_sibling_clone;
763 /* Input the base body of struct function FN from DATA_IN
764 using input block IB. */
766 static void
767 input_struct_function_base (struct function *fn, struct data_in *data_in,
768 struct lto_input_block *ib)
770 struct bitpack_d bp;
771 int len;
773 /* Read the static chain and non-local goto save area. */
774 fn->static_chain_decl = stream_read_tree (ib, data_in);
775 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
777 /* Read all the local symbols. */
778 len = streamer_read_hwi (ib);
779 if (len > 0)
781 int i;
782 VEC_safe_grow (tree, gc, fn->local_decls, len);
783 for (i = 0; i < len; i++)
785 tree t = stream_read_tree (ib, data_in);
786 VEC_replace (tree, fn->local_decls, i, t);
790 /* Input the function start and end loci. */
791 fn->function_start_locus = lto_input_location (ib, data_in);
792 fn->function_end_locus = lto_input_location (ib, data_in);
794 /* Input the current IL state of the function. */
795 fn->curr_properties = streamer_read_uhwi (ib);
797 /* Read all the attributes for FN. */
798 bp = streamer_read_bitpack (ib);
799 fn->is_thunk = bp_unpack_value (&bp, 1);
800 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
801 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
802 fn->returns_struct = bp_unpack_value (&bp, 1);
803 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
804 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
805 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
806 fn->after_inlining = bp_unpack_value (&bp, 1);
807 fn->stdarg = bp_unpack_value (&bp, 1);
808 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
809 fn->calls_alloca = bp_unpack_value (&bp, 1);
810 fn->calls_setjmp = bp_unpack_value (&bp, 1);
811 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
812 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
816 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
818 static void
819 input_function (tree fn_decl, struct data_in *data_in,
820 struct lto_input_block *ib)
822 struct function *fn;
823 enum LTO_tags tag;
824 gimple *stmts;
825 basic_block bb;
826 struct cgraph_node *node;
827 tree args, narg, oarg;
829 fn = DECL_STRUCT_FUNCTION (fn_decl);
830 tag = streamer_read_record_start (ib);
831 clear_line_info (data_in);
833 gimple_register_cfg_hooks ();
834 lto_tag_check (tag, LTO_function);
836 input_struct_function_base (fn, data_in, ib);
838 /* Read all function arguments. We need to re-map them here to the
839 arguments of the merged function declaration. */
840 args = stream_read_tree (ib, data_in);
841 for (oarg = args, narg = DECL_ARGUMENTS (fn_decl);
842 oarg && narg;
843 oarg = TREE_CHAIN (oarg), narg = TREE_CHAIN (narg))
845 unsigned ix;
846 bool res;
847 res = streamer_tree_cache_lookup (data_in->reader_cache, oarg, &ix);
848 gcc_assert (res);
849 /* Replace the argument in the streamer cache. */
850 streamer_tree_cache_insert_at (data_in->reader_cache, narg, ix);
852 gcc_assert (!oarg && !narg);
854 /* Read all the SSA names. */
855 input_ssa_names (ib, data_in, fn);
857 /* Read the exception handling regions in the function. */
858 input_eh_regions (ib, data_in, fn);
860 /* Read the tree of lexical scopes for the function. */
861 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
862 gcc_assert (DECL_INITIAL (fn_decl));
863 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
864 node = cgraph_get_create_node (fn_decl);
866 /* Read all the basic blocks. */
867 tag = streamer_read_record_start (ib);
868 while (tag)
870 input_bb (ib, tag, data_in, fn,
871 node->count_materialization_scale);
872 tag = streamer_read_record_start (ib);
875 /* Fix up the call statements that are mentioned in the callgraph
876 edges. */
877 set_gimple_stmt_max_uid (cfun, 0);
878 FOR_ALL_BB (bb)
880 gimple_stmt_iterator gsi;
881 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
883 gimple stmt = gsi_stmt (gsi);
884 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
887 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
888 FOR_ALL_BB (bb)
890 gimple_stmt_iterator bsi = gsi_start_bb (bb);
891 while (!gsi_end_p (bsi))
893 gimple stmt = gsi_stmt (bsi);
894 /* If we're recompiling LTO objects with debug stmts but
895 we're not supposed to have debug stmts, remove them now.
896 We can't remove them earlier because this would cause uid
897 mismatches in fixups, but we can do it at this point, as
898 long as debug stmts don't require fixups. */
899 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
901 gimple_stmt_iterator gsi = bsi;
902 gsi_next (&bsi);
903 gsi_remove (&gsi, true);
905 else
907 gsi_next (&bsi);
908 stmts[gimple_uid (stmt)] = stmt;
913 /* Set the gimple body to the statement sequence in the entry
914 basic block. FIXME lto, this is fairly hacky. The existence
915 of a gimple body is used by the cgraph routines, but we should
916 really use the presence of the CFG. */
918 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
919 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
922 fixup_call_stmt_edges (node, stmts);
923 execute_all_ipa_stmt_fixups (node, stmts);
925 update_ssa (TODO_update_ssa_only_virtuals);
926 free_dominance_info (CDI_DOMINATORS);
927 free_dominance_info (CDI_POST_DOMINATORS);
928 free (stmts);
932 /* Read the body from DATA for function FN_DECL and fill it in.
933 FILE_DATA are the global decls and types. SECTION_TYPE is either
934 LTO_section_function_body or LTO_section_static_initializer. If
935 section type is LTO_section_function_body, FN must be the decl for
936 that function. */
938 static void
939 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
940 const char *data, enum lto_section_type section_type)
942 const struct lto_function_header *header;
943 struct data_in *data_in;
944 int cfg_offset;
945 int main_offset;
946 int string_offset;
947 struct lto_input_block ib_cfg;
948 struct lto_input_block ib_main;
950 header = (const struct lto_function_header *) data;
951 cfg_offset = sizeof (struct lto_function_header);
952 main_offset = cfg_offset + header->cfg_size;
953 string_offset = main_offset + header->main_size;
955 LTO_INIT_INPUT_BLOCK (ib_cfg,
956 data + cfg_offset,
958 header->cfg_size);
960 LTO_INIT_INPUT_BLOCK (ib_main,
961 data + main_offset,
963 header->main_size);
965 data_in = lto_data_in_create (file_data, data + string_offset,
966 header->string_size, NULL);
968 /* Make sure the file was generated by the exact same compiler. */
969 lto_check_version (header->lto_header.major_version,
970 header->lto_header.minor_version);
972 if (section_type == LTO_section_function_body)
974 struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
975 struct lto_in_decl_state *decl_state;
976 struct cgraph_node *node = cgraph_get_node (fn_decl);
977 unsigned from;
979 gcc_checking_assert (node);
980 push_cfun (fn);
981 init_tree_ssa (fn);
983 /* Use the function's decl state. */
984 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
985 gcc_assert (decl_state);
986 file_data->current_decl_state = decl_state;
988 input_cfg (&ib_cfg, fn, node->count_materialization_scale);
990 /* Set up the struct function. */
991 from = VEC_length (tree, data_in->reader_cache->nodes);
992 input_function (fn_decl, data_in, &ib_main);
993 /* And fixup types we streamed locally. */
995 struct streamer_tree_cache_d *cache = data_in->reader_cache;
996 unsigned len = VEC_length (tree, cache->nodes);
997 unsigned i;
998 for (i = len; i-- > from;)
1000 tree t = VEC_index (tree, cache->nodes, i);
1001 if (t == NULL_TREE)
1002 continue;
1004 if (TYPE_P (t))
1006 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1007 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1008 if (TYPE_MAIN_VARIANT (t) != t)
1010 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1011 TYPE_NEXT_VARIANT (t)
1012 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1013 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1019 /* We should now be in SSA. */
1020 cfun->gimple_df->in_ssa_p = true;
1022 /* Restore decl state */
1023 file_data->current_decl_state = file_data->global_decl_state;
1025 pop_cfun ();
1028 clear_line_info (data_in);
1029 lto_data_in_delete (data_in);
1033 /* Read the body of FN_DECL using DATA. FILE_DATA holds the global
1034 decls and types. */
1036 void
1037 lto_input_function_body (struct lto_file_decl_data *file_data,
1038 tree fn_decl, const char *data)
1040 current_function_decl = fn_decl;
1041 lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1045 /* Read the physical representation of a tree node with tag TAG from
1046 input block IB using the per-file context in DATA_IN. */
1048 static tree
1049 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1050 enum LTO_tags tag)
1052 /* Instantiate a new tree node. */
1053 tree result = streamer_alloc_tree (ib, data_in, tag);
1055 /* Enter RESULT in the reader cache. This will make RESULT
1056 available so that circular references in the rest of the tree
1057 structure can be resolved in subsequent calls to stream_read_tree. */
1058 streamer_tree_cache_append (data_in->reader_cache, result);
1060 /* Read all the bitfield values in RESULT. Note that for LTO, we
1061 only write language-independent bitfields, so no more unpacking is
1062 needed. */
1063 streamer_read_tree_bitfields (ib, result);
1065 /* Read all the pointer fields in RESULT. */
1066 streamer_read_tree_body (ib, data_in, result);
1068 /* Read any LTO-specific data not read by the tree streamer. */
1069 if (DECL_P (result)
1070 && TREE_CODE (result) != FUNCTION_DECL
1071 && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1072 DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1074 /* We should never try to instantiate an MD or NORMAL builtin here. */
1075 if (TREE_CODE (result) == FUNCTION_DECL)
1076 gcc_assert (!streamer_handle_as_builtin_p (result));
1078 /* end_marker = */ streamer_read_uchar (ib);
1080 #ifdef LTO_STREAMER_DEBUG
1081 /* Remove the mapping to RESULT's original address set by
1082 streamer_alloc_tree. */
1083 lto_orig_address_remove (result);
1084 #endif
1086 return result;
1090 /* Read a tree from input block IB using the per-file context in
1091 DATA_IN. This context is used, for example, to resolve references
1092 to previously read nodes. */
1094 tree
1095 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1097 enum LTO_tags tag;
1098 tree result;
1100 tag = streamer_read_record_start (ib);
1101 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1103 if (tag == LTO_null)
1104 result = NULL_TREE;
1105 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1107 /* If TAG is a reference to an indexable tree, the next value
1108 in IB is the index into the table where we expect to find
1109 that tree. */
1110 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1112 else if (tag == LTO_tree_pickle_reference)
1114 /* If TAG is a reference to a previously read tree, look it up in
1115 the reader cache. */
1116 result = streamer_get_pickled_tree (ib, data_in);
1118 else if (tag == LTO_builtin_decl)
1120 /* If we are going to read a built-in function, all we need is
1121 the code and class. */
1122 result = streamer_get_builtin_tree (ib, data_in);
1124 else if (tag == lto_tree_code_to_tag (INTEGER_CST))
1126 /* For integer constants we only need the type and its hi/low
1127 words. */
1128 result = streamer_read_integer_cst (ib, data_in);
1130 else
1132 /* Otherwise, materialize a new node from IB. */
1133 result = lto_read_tree (ib, data_in, tag);
1136 return result;
1140 /* Input toplevel asms. */
1142 void
1143 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1145 size_t len;
1146 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1147 NULL, &len);
1148 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1149 int string_offset;
1150 struct data_in *data_in;
1151 struct lto_input_block ib;
1152 tree str;
1154 if (! data)
1155 return;
1157 string_offset = sizeof (*header) + header->main_size;
1159 LTO_INIT_INPUT_BLOCK (ib,
1160 data + sizeof (*header),
1162 header->main_size);
1164 data_in = lto_data_in_create (file_data, data + string_offset,
1165 header->string_size, NULL);
1167 /* Make sure the file was generated by the exact same compiler. */
1168 lto_check_version (header->lto_header.major_version,
1169 header->lto_header.minor_version);
1171 while ((str = streamer_read_string_cst (data_in, &ib)))
1173 struct asm_node *node = add_asm_node (str);
1174 node->order = streamer_read_hwi (&ib) + order_base;
1175 if (node->order >= symtab_order)
1176 symtab_order = node->order + 1;
1179 clear_line_info (data_in);
1180 lto_data_in_delete (data_in);
1182 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1186 /* Initialization for the LTO reader. */
1188 void
1189 lto_reader_init (void)
1191 lto_streamer_init ();
1192 file_name_hash_table = htab_create (37, hash_string_slot_node,
1193 eq_string_slot_node, free);
1197 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1198 table to use with LEN strings. RESOLUTIONS is the vector of linker
1199 resolutions (NULL if not using a linker plugin). */
1201 struct data_in *
1202 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1203 unsigned len,
1204 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
1206 struct data_in *data_in = XCNEW (struct data_in);
1207 data_in->file_data = file_data;
1208 data_in->strings = strings;
1209 data_in->strings_len = len;
1210 data_in->globals_resolution = resolutions;
1211 data_in->reader_cache = streamer_tree_cache_create ();
1213 return data_in;
1217 /* Remove DATA_IN. */
1219 void
1220 lto_data_in_delete (struct data_in *data_in)
1222 VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
1223 streamer_tree_cache_delete (data_in->reader_cache);
1224 free (data_in->labels);
1225 free (data_in);