2013-11-19 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lto-streamer-in.c
blobf3a08884b53e2c9029c543be2befd847c8631e39
1 /* Read the GIMPLE representation from a file stream.
3 Copyright (C) 2009-2013 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 /* Read a location bitpack from input block IB. */
128 location_t
129 lto_input_location (struct bitpack_d *bp, struct data_in *data_in)
131 static const char *current_file;
132 static int current_line;
133 static int current_col;
134 bool file_change, line_change, column_change;
135 unsigned len;
136 bool prev_file = current_file != NULL;
138 if (bp_unpack_value (bp, 1))
139 return UNKNOWN_LOCATION;
141 file_change = bp_unpack_value (bp, 1);
142 line_change = bp_unpack_value (bp, 1);
143 column_change = bp_unpack_value (bp, 1);
145 if (file_change)
146 current_file = canon_file_name
147 (string_for_index (data_in,
148 bp_unpack_var_len_unsigned (bp),
149 &len));
151 if (line_change)
152 current_line = bp_unpack_var_len_unsigned (bp);
154 if (column_change)
155 current_col = bp_unpack_var_len_unsigned (bp);
157 if (file_change)
159 if (prev_file)
160 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
162 linemap_add (line_table, LC_ENTER, false, current_file, current_line);
164 else if (line_change)
165 linemap_line_start (line_table, current_line, current_col);
167 return linemap_position_for_column (line_table, current_col);
171 /* Read a reference to a tree node from DATA_IN using input block IB.
172 TAG is the expected node that should be found in IB, if TAG belongs
173 to one of the indexable trees, expect to read a reference index to
174 be looked up in one of the symbol tables, otherwise read the pysical
175 representation of the tree using stream_read_tree. FN is the
176 function scope for the read tree. */
178 tree
179 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
180 struct function *fn, enum LTO_tags tag)
182 unsigned HOST_WIDE_INT ix_u;
183 tree result = NULL_TREE;
185 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
187 switch (tag)
189 case LTO_type_ref:
190 ix_u = streamer_read_uhwi (ib);
191 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
192 break;
194 case LTO_ssa_name_ref:
195 ix_u = streamer_read_uhwi (ib);
196 result = (*SSANAMES (fn))[ix_u];
197 break;
199 case LTO_field_decl_ref:
200 ix_u = streamer_read_uhwi (ib);
201 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
202 break;
204 case LTO_function_decl_ref:
205 ix_u = streamer_read_uhwi (ib);
206 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
207 break;
209 case LTO_type_decl_ref:
210 ix_u = streamer_read_uhwi (ib);
211 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
212 break;
214 case LTO_namespace_decl_ref:
215 ix_u = streamer_read_uhwi (ib);
216 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
217 break;
219 case LTO_global_decl_ref:
220 case LTO_result_decl_ref:
221 case LTO_const_decl_ref:
222 case LTO_imported_decl_ref:
223 case LTO_label_decl_ref:
224 case LTO_translation_unit_decl_ref:
225 ix_u = streamer_read_uhwi (ib);
226 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
227 break;
229 default:
230 gcc_unreachable ();
233 gcc_assert (result);
235 return result;
239 /* Read and return a double-linked list of catch handlers from input
240 block IB, using descriptors in DATA_IN. */
242 static struct eh_catch_d *
243 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
244 eh_catch *last_p)
246 eh_catch first;
247 enum LTO_tags tag;
249 *last_p = first = NULL;
250 tag = streamer_read_record_start (ib);
251 while (tag)
253 tree list;
254 eh_catch n;
256 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
258 /* Read the catch node. */
259 n = ggc_alloc_cleared_eh_catch_d ();
260 n->type_list = stream_read_tree (ib, data_in);
261 n->filter_list = stream_read_tree (ib, data_in);
262 n->label = stream_read_tree (ib, data_in);
264 /* Register all the types in N->FILTER_LIST. */
265 for (list = n->filter_list; list; list = TREE_CHAIN (list))
266 add_type_for_runtime (TREE_VALUE (list));
268 /* Chain N to the end of the list. */
269 if (*last_p)
270 (*last_p)->next_catch = n;
271 n->prev_catch = *last_p;
272 *last_p = n;
274 /* Set the head of the list the first time through the loop. */
275 if (first == NULL)
276 first = n;
278 tag = streamer_read_record_start (ib);
281 return first;
285 /* Read and return EH region IX from input block IB, using descriptors
286 in DATA_IN. */
288 static eh_region
289 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
291 enum LTO_tags tag;
292 eh_region r;
294 /* Read the region header. */
295 tag = streamer_read_record_start (ib);
296 if (tag == LTO_null)
297 return NULL;
299 r = ggc_alloc_cleared_eh_region_d ();
300 r->index = streamer_read_hwi (ib);
302 gcc_assert (r->index == ix);
304 /* Read all the region pointers as region numbers. We'll fix up
305 the pointers once the whole array has been read. */
306 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
307 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
308 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
310 switch (tag)
312 case LTO_ert_cleanup:
313 r->type = ERT_CLEANUP;
314 break;
316 case LTO_ert_try:
318 struct eh_catch_d *last_catch;
319 r->type = ERT_TRY;
320 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
321 &last_catch);
322 r->u.eh_try.last_catch = last_catch;
323 break;
326 case LTO_ert_allowed_exceptions:
328 tree l;
330 r->type = ERT_ALLOWED_EXCEPTIONS;
331 r->u.allowed.type_list = stream_read_tree (ib, data_in);
332 r->u.allowed.label = stream_read_tree (ib, data_in);
333 r->u.allowed.filter = streamer_read_uhwi (ib);
335 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
336 add_type_for_runtime (TREE_VALUE (l));
338 break;
340 case LTO_ert_must_not_throw:
342 r->type = ERT_MUST_NOT_THROW;
343 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
344 bitpack_d bp = streamer_read_bitpack (ib);
345 r->u.must_not_throw.failure_loc
346 = stream_input_location (&bp, data_in);
348 break;
350 default:
351 gcc_unreachable ();
354 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
356 return r;
360 /* Read and return EH landing pad IX from input block IB, using descriptors
361 in DATA_IN. */
363 static eh_landing_pad
364 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
366 enum LTO_tags tag;
367 eh_landing_pad lp;
369 /* Read the landing pad header. */
370 tag = streamer_read_record_start (ib);
371 if (tag == LTO_null)
372 return NULL;
374 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
376 lp = ggc_alloc_cleared_eh_landing_pad_d ();
377 lp->index = streamer_read_hwi (ib);
378 gcc_assert (lp->index == ix);
379 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
380 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
381 lp->post_landing_pad = stream_read_tree (ib, data_in);
383 return lp;
387 /* After reading the EH regions, pointers to peer and children regions
388 are region numbers. This converts all these region numbers into
389 real pointers into the rematerialized regions for FN. ROOT_REGION
390 is the region number for the root EH region in FN. */
392 static void
393 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
395 unsigned i;
396 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
397 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
398 eh_region r;
399 eh_landing_pad lp;
401 gcc_assert (eh_array && lp_array);
403 gcc_assert (root_region >= 0);
404 fn->eh->region_tree = (*eh_array)[root_region];
406 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
407 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
409 /* Convert all the index numbers stored in pointer fields into
410 pointers to the corresponding slots in the EH region array. */
411 FOR_EACH_VEC_ELT (*eh_array, i, r)
413 /* The array may contain NULL regions. */
414 if (r == NULL)
415 continue;
417 gcc_assert (i == (unsigned) r->index);
418 FIXUP_EH_REGION (r->outer);
419 FIXUP_EH_REGION (r->inner);
420 FIXUP_EH_REGION (r->next_peer);
421 FIXUP_EH_LP (r->landing_pads);
424 /* Convert all the index numbers stored in pointer fields into
425 pointers to the corresponding slots in the EH landing pad array. */
426 FOR_EACH_VEC_ELT (*lp_array, i, lp)
428 /* The array may contain NULL landing pads. */
429 if (lp == NULL)
430 continue;
432 gcc_assert (i == (unsigned) lp->index);
433 FIXUP_EH_LP (lp->next_lp);
434 FIXUP_EH_REGION (lp->region);
437 #undef FIXUP_EH_REGION
438 #undef FIXUP_EH_LP
442 /* Initialize EH support. */
444 void
445 lto_init_eh (void)
447 static bool eh_initialized_p = false;
449 if (eh_initialized_p)
450 return;
452 /* Contrary to most other FEs, we only initialize EH support when at
453 least one of the files in the set contains exception regions in
454 it. Since this happens much later than the call to init_eh in
455 lang_dependent_init, we have to set flag_exceptions and call
456 init_eh again to initialize the EH tables. */
457 flag_exceptions = 1;
458 init_eh ();
460 eh_initialized_p = true;
464 /* Read the exception table for FN from IB using the data descriptors
465 in DATA_IN. */
467 static void
468 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
469 struct function *fn)
471 HOST_WIDE_INT i, root_region, len;
472 enum LTO_tags tag;
474 tag = streamer_read_record_start (ib);
475 if (tag == LTO_null)
476 return;
478 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
480 /* If the file contains EH regions, then it was compiled with
481 -fexceptions. In that case, initialize the backend EH
482 machinery. */
483 lto_init_eh ();
485 gcc_assert (fn->eh);
487 root_region = streamer_read_hwi (ib);
488 gcc_assert (root_region == (int) root_region);
490 /* Read the EH region array. */
491 len = streamer_read_hwi (ib);
492 gcc_assert (len == (int) len);
493 if (len > 0)
495 vec_safe_grow_cleared (fn->eh->region_array, len);
496 for (i = 0; i < len; i++)
498 eh_region r = input_eh_region (ib, data_in, i);
499 (*fn->eh->region_array)[i] = r;
503 /* Read the landing pads. */
504 len = streamer_read_hwi (ib);
505 gcc_assert (len == (int) len);
506 if (len > 0)
508 vec_safe_grow_cleared (fn->eh->lp_array, len);
509 for (i = 0; i < len; i++)
511 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
512 (*fn->eh->lp_array)[i] = lp;
516 /* Read the runtime type data. */
517 len = streamer_read_hwi (ib);
518 gcc_assert (len == (int) len);
519 if (len > 0)
521 vec_safe_grow_cleared (fn->eh->ttype_data, len);
522 for (i = 0; i < len; i++)
524 tree ttype = stream_read_tree (ib, data_in);
525 (*fn->eh->ttype_data)[i] = ttype;
529 /* Read the table of action chains. */
530 len = streamer_read_hwi (ib);
531 gcc_assert (len == (int) len);
532 if (len > 0)
534 if (targetm.arm_eabi_unwinder)
536 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
537 for (i = 0; i < len; i++)
539 tree t = stream_read_tree (ib, data_in);
540 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
543 else
545 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
546 for (i = 0; i < len; i++)
548 uchar c = streamer_read_uchar (ib);
549 (*fn->eh->ehspec_data.other)[i] = c;
554 /* Reconstruct the EH region tree by fixing up the peer/children
555 pointers. */
556 fixup_eh_region_pointers (fn, root_region);
558 tag = streamer_read_record_start (ib);
559 lto_tag_check_range (tag, LTO_null, LTO_null);
563 /* Make a new basic block with index INDEX in function FN. */
565 static basic_block
566 make_new_block (struct function *fn, unsigned int index)
568 basic_block bb = alloc_block ();
569 bb->index = index;
570 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
571 n_basic_blocks_for_function (fn)++;
572 return bb;
576 /* Read the CFG for function FN from input block IB. */
578 static void
579 input_cfg (struct lto_input_block *ib, struct function *fn,
580 int count_materialization_scale)
582 unsigned int bb_count;
583 basic_block p_bb;
584 unsigned int i;
585 int index;
587 init_empty_tree_cfg_for_function (fn);
588 init_ssa_operands (fn);
590 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
591 PROFILE_LAST);
593 bb_count = streamer_read_uhwi (ib);
595 last_basic_block_for_function (fn) = bb_count;
596 if (bb_count > basic_block_info_for_function (fn)->length ())
597 vec_safe_grow_cleared (basic_block_info_for_function (fn), bb_count);
599 if (bb_count > label_to_block_map_for_function (fn)->length ())
600 vec_safe_grow_cleared (label_to_block_map_for_function (fn), bb_count);
602 index = streamer_read_hwi (ib);
603 while (index != -1)
605 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
606 unsigned int edge_count;
608 if (bb == NULL)
609 bb = make_new_block (fn, index);
611 edge_count = streamer_read_uhwi (ib);
613 /* Connect up the CFG. */
614 for (i = 0; i < edge_count; i++)
616 unsigned int dest_index;
617 unsigned int edge_flags;
618 basic_block dest;
619 int probability;
620 gcov_type count;
621 edge e;
623 dest_index = streamer_read_uhwi (ib);
624 probability = (int) streamer_read_hwi (ib);
625 count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
626 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
627 edge_flags = streamer_read_uhwi (ib);
629 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
631 if (dest == NULL)
632 dest = make_new_block (fn, dest_index);
634 e = make_edge (bb, dest, edge_flags);
635 e->probability = probability;
636 e->count = count;
639 index = streamer_read_hwi (ib);
642 p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
643 index = streamer_read_hwi (ib);
644 while (index != -1)
646 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
647 bb->prev_bb = p_bb;
648 p_bb->next_bb = bb;
649 p_bb = bb;
650 index = streamer_read_hwi (ib);
655 /* Read the SSA names array for function FN from DATA_IN using input
656 block IB. */
658 static void
659 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
660 struct function *fn)
662 unsigned int i, size;
664 size = streamer_read_uhwi (ib);
665 init_ssanames (fn, size);
667 i = streamer_read_uhwi (ib);
668 while (i)
670 tree ssa_name, name;
671 bool is_default_def;
673 /* Skip over the elements that had been freed. */
674 while (SSANAMES (fn)->length () < i)
675 SSANAMES (fn)->quick_push (NULL_TREE);
677 is_default_def = (streamer_read_uchar (ib) != 0);
678 name = stream_read_tree (ib, data_in);
679 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
681 if (is_default_def)
682 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
684 i = streamer_read_uhwi (ib);
689 /* Go through all NODE edges and fixup call_stmt pointers
690 so they point to STMTS. */
692 static void
693 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
695 struct cgraph_edge *cedge;
696 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
697 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
698 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
699 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
702 /* Fixup call_stmt pointers in NODE and all clones. */
704 static void
705 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
707 struct cgraph_node *node;
709 while (orig->clone_of)
710 orig = orig->clone_of;
712 fixup_call_stmt_edges_1 (orig, stmts);
713 if (orig->clones)
714 for (node = orig->clones; node != orig;)
716 fixup_call_stmt_edges_1 (node, stmts);
717 if (node->clones)
718 node = node->clones;
719 else if (node->next_sibling_clone)
720 node = node->next_sibling_clone;
721 else
723 while (node != orig && !node->next_sibling_clone)
724 node = node->clone_of;
725 if (node != orig)
726 node = node->next_sibling_clone;
732 /* Input the base body of struct function FN from DATA_IN
733 using input block IB. */
735 static void
736 input_struct_function_base (struct function *fn, struct data_in *data_in,
737 struct lto_input_block *ib)
739 struct bitpack_d bp;
740 int len;
742 /* Read the static chain and non-local goto save area. */
743 fn->static_chain_decl = stream_read_tree (ib, data_in);
744 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
746 /* Read all the local symbols. */
747 len = streamer_read_hwi (ib);
748 if (len > 0)
750 int i;
751 vec_safe_grow_cleared (fn->local_decls, len);
752 for (i = 0; i < len; i++)
754 tree t = stream_read_tree (ib, data_in);
755 (*fn->local_decls)[i] = t;
759 /* Input the current IL state of the function. */
760 fn->curr_properties = streamer_read_uhwi (ib);
762 /* Read all the attributes for FN. */
763 bp = streamer_read_bitpack (ib);
764 fn->is_thunk = bp_unpack_value (&bp, 1);
765 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
766 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
767 fn->returns_struct = bp_unpack_value (&bp, 1);
768 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
769 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
770 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
771 fn->after_inlining = bp_unpack_value (&bp, 1);
772 fn->stdarg = bp_unpack_value (&bp, 1);
773 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
774 fn->calls_alloca = bp_unpack_value (&bp, 1);
775 fn->calls_setjmp = bp_unpack_value (&bp, 1);
776 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
777 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
779 /* Input the function start and end loci. */
780 fn->function_start_locus = stream_input_location (&bp, data_in);
781 fn->function_end_locus = stream_input_location (&bp, data_in);
785 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
787 static void
788 input_function (tree fn_decl, struct data_in *data_in,
789 struct lto_input_block *ib)
791 struct function *fn;
792 enum LTO_tags tag;
793 gimple *stmts;
794 basic_block bb;
795 struct cgraph_node *node;
797 fn = DECL_STRUCT_FUNCTION (fn_decl);
798 tag = streamer_read_record_start (ib);
800 gimple_register_cfg_hooks ();
801 lto_tag_check (tag, LTO_function);
803 input_struct_function_base (fn, data_in, ib);
805 /* Read all the SSA names. */
806 input_ssa_names (ib, data_in, fn);
808 /* Read the exception handling regions in the function. */
809 input_eh_regions (ib, data_in, fn);
811 /* Read the tree of lexical scopes for the function. */
812 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
813 gcc_assert (DECL_INITIAL (fn_decl));
814 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
815 node = cgraph_get_create_node (fn_decl);
817 /* Read all the basic blocks. */
818 tag = streamer_read_record_start (ib);
819 while (tag)
821 input_bb (ib, tag, data_in, fn,
822 node->count_materialization_scale);
823 tag = streamer_read_record_start (ib);
826 /* Fix up the call statements that are mentioned in the callgraph
827 edges. */
828 set_gimple_stmt_max_uid (cfun, 0);
829 FOR_ALL_BB (bb)
831 gimple_stmt_iterator gsi;
832 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
834 gimple stmt = gsi_stmt (gsi);
835 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
838 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
839 FOR_ALL_BB (bb)
841 gimple_stmt_iterator bsi = gsi_start_bb (bb);
842 while (!gsi_end_p (bsi))
844 gimple stmt = gsi_stmt (bsi);
845 /* If we're recompiling LTO objects with debug stmts but
846 we're not supposed to have debug stmts, remove them now.
847 We can't remove them earlier because this would cause uid
848 mismatches in fixups, but we can do it at this point, as
849 long as debug stmts don't require fixups. */
850 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
852 gimple_stmt_iterator gsi = bsi;
853 gsi_next (&bsi);
854 gsi_remove (&gsi, true);
856 else
858 gsi_next (&bsi);
859 stmts[gimple_uid (stmt)] = stmt;
864 /* Set the gimple body to the statement sequence in the entry
865 basic block. FIXME lto, this is fairly hacky. The existence
866 of a gimple body is used by the cgraph routines, but we should
867 really use the presence of the CFG. */
869 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
870 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
873 fixup_call_stmt_edges (node, stmts);
874 execute_all_ipa_stmt_fixups (node, stmts);
876 update_ssa (TODO_update_ssa_only_virtuals);
877 free_dominance_info (CDI_DOMINATORS);
878 free_dominance_info (CDI_POST_DOMINATORS);
879 free (stmts);
883 /* Read the body from DATA for function FN_DECL and fill it in.
884 FILE_DATA are the global decls and types. SECTION_TYPE is either
885 LTO_section_function_body or LTO_section_static_initializer. If
886 section type is LTO_section_function_body, FN must be the decl for
887 that function. */
889 static void
890 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
891 const char *data, enum lto_section_type section_type)
893 const struct lto_function_header *header;
894 struct data_in *data_in;
895 int cfg_offset;
896 int main_offset;
897 int string_offset;
898 struct lto_input_block ib_cfg;
899 struct lto_input_block ib_main;
901 header = (const struct lto_function_header *) data;
902 cfg_offset = sizeof (struct lto_function_header);
903 main_offset = cfg_offset + header->cfg_size;
904 string_offset = main_offset + header->main_size;
906 LTO_INIT_INPUT_BLOCK (ib_cfg,
907 data + cfg_offset,
909 header->cfg_size);
911 LTO_INIT_INPUT_BLOCK (ib_main,
912 data + main_offset,
914 header->main_size);
916 data_in = lto_data_in_create (file_data, data + string_offset,
917 header->string_size, vNULL);
919 /* Make sure the file was generated by the exact same compiler. */
920 lto_check_version (header->lto_header.major_version,
921 header->lto_header.minor_version);
923 if (section_type == LTO_section_function_body)
925 struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
926 struct lto_in_decl_state *decl_state;
927 struct cgraph_node *node = cgraph_get_node (fn_decl);
928 unsigned from;
930 gcc_checking_assert (node);
931 push_cfun (fn);
932 init_tree_ssa (fn);
934 /* We input IL in SSA form. */
935 cfun->gimple_df->in_ssa_p = true;
937 /* Use the function's decl state. */
938 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
939 gcc_assert (decl_state);
940 file_data->current_decl_state = decl_state;
942 input_cfg (&ib_cfg, fn, node->count_materialization_scale);
944 /* Set up the struct function. */
945 from = data_in->reader_cache->nodes.length ();
946 input_function (fn_decl, data_in, &ib_main);
947 /* And fixup types we streamed locally. */
949 struct streamer_tree_cache_d *cache = data_in->reader_cache;
950 unsigned len = cache->nodes.length ();
951 unsigned i;
952 for (i = len; i-- > from;)
954 tree t = cache->nodes[i];
955 if (t == NULL_TREE)
956 continue;
958 if (TYPE_P (t))
960 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
961 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
962 if (TYPE_MAIN_VARIANT (t) != t)
964 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
965 TYPE_NEXT_VARIANT (t)
966 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
967 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
973 /* Restore decl state */
974 file_data->current_decl_state = file_data->global_decl_state;
976 pop_cfun ();
979 lto_data_in_delete (data_in);
983 /* Read the body of FN_DECL using DATA. FILE_DATA holds the global
984 decls and types. */
986 void
987 lto_input_function_body (struct lto_file_decl_data *file_data,
988 tree fn_decl, const char *data)
990 lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
994 /* Read the physical representation of a tree node with tag TAG from
995 input block IB using the per-file context in DATA_IN. */
997 static tree
998 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
999 enum LTO_tags tag)
1001 /* Instantiate a new tree node. */
1002 tree result = streamer_alloc_tree (ib, data_in, tag);
1004 /* Enter RESULT in the reader cache. This will make RESULT
1005 available so that circular references in the rest of the tree
1006 structure can be resolved in subsequent calls to stream_read_tree. */
1007 streamer_tree_cache_append (data_in->reader_cache, result);
1009 /* Read all the bitfield values in RESULT. Note that for LTO, we
1010 only write language-independent bitfields, so no more unpacking is
1011 needed. */
1012 streamer_read_tree_bitfields (ib, data_in, result);
1014 /* Read all the pointer fields in RESULT. */
1015 streamer_read_tree_body (ib, data_in, result);
1017 /* Read any LTO-specific data not read by the tree streamer. */
1018 if (DECL_P (result)
1019 && TREE_CODE (result) != FUNCTION_DECL
1020 && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1021 DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1023 /* We should never try to instantiate an MD or NORMAL builtin here. */
1024 if (TREE_CODE (result) == FUNCTION_DECL)
1025 gcc_assert (!streamer_handle_as_builtin_p (result));
1027 /* end_marker = */ streamer_read_uchar (ib);
1029 #ifdef LTO_STREAMER_DEBUG
1030 /* Remove the mapping to RESULT's original address set by
1031 streamer_alloc_tree. */
1032 lto_orig_address_remove (result);
1033 #endif
1035 return result;
1039 /* Read a tree from input block IB using the per-file context in
1040 DATA_IN. This context is used, for example, to resolve references
1041 to previously read nodes. */
1043 tree
1044 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1046 enum LTO_tags tag;
1047 tree result;
1049 tag = streamer_read_record_start (ib);
1050 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1052 if (tag == LTO_null)
1053 result = NULL_TREE;
1054 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1056 /* If TAG is a reference to an indexable tree, the next value
1057 in IB is the index into the table where we expect to find
1058 that tree. */
1059 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1061 else if (tag == LTO_tree_pickle_reference)
1063 /* If TAG is a reference to a previously read tree, look it up in
1064 the reader cache. */
1065 result = streamer_get_pickled_tree (ib, data_in);
1067 else if (tag == LTO_builtin_decl)
1069 /* If we are going to read a built-in function, all we need is
1070 the code and class. */
1071 result = streamer_get_builtin_tree (ib, data_in);
1073 else if (tag == LTO_integer_cst)
1075 /* For shared integer constants we only need the type and its hi/low
1076 words. */
1077 result = streamer_read_integer_cst (ib, data_in);
1079 else
1081 /* Otherwise, materialize a new node from IB. */
1082 result = lto_read_tree (ib, data_in, tag);
1085 return result;
1089 /* Input toplevel asms. */
1091 void
1092 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1094 size_t len;
1095 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1096 NULL, &len);
1097 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1098 int string_offset;
1099 struct data_in *data_in;
1100 struct lto_input_block ib;
1101 tree str;
1103 if (! data)
1104 return;
1106 string_offset = sizeof (*header) + header->main_size;
1108 LTO_INIT_INPUT_BLOCK (ib,
1109 data + sizeof (*header),
1111 header->main_size);
1113 data_in = lto_data_in_create (file_data, data + string_offset,
1114 header->string_size, vNULL);
1116 /* Make sure the file was generated by the exact same compiler. */
1117 lto_check_version (header->lto_header.major_version,
1118 header->lto_header.minor_version);
1120 while ((str = streamer_read_string_cst (data_in, &ib)))
1122 struct asm_node *node = add_asm_node (str);
1123 node->order = streamer_read_hwi (&ib) + order_base;
1124 if (node->order >= symtab_order)
1125 symtab_order = node->order + 1;
1128 lto_data_in_delete (data_in);
1130 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1134 /* Initialization for the LTO reader. */
1136 void
1137 lto_reader_init (void)
1139 lto_streamer_init ();
1140 file_name_hash_table = htab_create (37, hash_string_slot_node,
1141 eq_string_slot_node, free);
1145 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1146 table to use with LEN strings. RESOLUTIONS is the vector of linker
1147 resolutions (NULL if not using a linker plugin). */
1149 struct data_in *
1150 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1151 unsigned len,
1152 vec<ld_plugin_symbol_resolution_t> resolutions)
1154 struct data_in *data_in = XCNEW (struct data_in);
1155 data_in->file_data = file_data;
1156 data_in->strings = strings;
1157 data_in->strings_len = len;
1158 data_in->globals_resolution = resolutions;
1159 data_in->reader_cache = streamer_tree_cache_create ();
1161 return data_in;
1165 /* Remove DATA_IN. */
1167 void
1168 lto_data_in_delete (struct data_in *data_in)
1170 data_in->globals_resolution.release ();
1171 streamer_tree_cache_delete (data_in->reader_cache);
1172 free (data_in->labels);
1173 free (data_in);