* inclhack.def (aix_null): New.
[official-gcc.git] / gcc / lto-streamer-in.c
blob02889a99501144b29c94a625c3918928e14776a8
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"
51 #include "cfgloop.h"
54 struct freeing_string_slot_hasher : string_slot_hasher
56 static inline void remove (value_type *);
59 inline void
60 freeing_string_slot_hasher::remove (value_type *v)
62 free (v);
65 /* The table to hold the file names. */
66 static hash_table <freeing_string_slot_hasher> file_name_hash_table;
69 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
70 number of valid tag values to check. */
72 void
73 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
75 va_list ap;
76 int i;
78 va_start (ap, ntags);
79 for (i = 0; i < ntags; i++)
80 if ((unsigned) actual == va_arg (ap, unsigned))
82 va_end (ap);
83 return;
86 va_end (ap);
87 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
91 /* Read LENGTH bytes from STREAM to ADDR. */
93 void
94 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
96 size_t i;
97 unsigned char *const buffer = (unsigned char *const) addr;
99 for (i = 0; i < length; i++)
100 buffer[i] = streamer_read_uchar (ib);
104 /* Lookup STRING in file_name_hash_table. If found, return the existing
105 string, otherwise insert STRING as the canonical version. */
107 static const char *
108 canon_file_name (const char *string)
110 string_slot **slot;
111 struct string_slot s_slot;
112 size_t len = strlen (string);
114 s_slot.s = string;
115 s_slot.len = len;
117 slot = file_name_hash_table.find_slot (&s_slot, INSERT);
118 if (*slot == NULL)
120 char *saved_string;
121 struct string_slot *new_slot;
123 saved_string = (char *) xmalloc (len + 1);
124 new_slot = XCNEW (struct string_slot);
125 memcpy (saved_string, string, len + 1);
126 new_slot->s = saved_string;
127 new_slot->len = len;
128 *slot = new_slot;
129 return saved_string;
131 else
133 struct string_slot *old_slot = *slot;
134 return old_slot->s;
139 /* Read a location bitpack from input block IB. */
141 location_t
142 lto_input_location (struct bitpack_d *bp, struct data_in *data_in)
144 static const char *current_file;
145 static int current_line;
146 static int current_col;
147 bool file_change, line_change, column_change;
148 unsigned len;
149 bool prev_file = current_file != NULL;
151 if (bp_unpack_value (bp, 1))
152 return UNKNOWN_LOCATION;
154 file_change = bp_unpack_value (bp, 1);
155 line_change = bp_unpack_value (bp, 1);
156 column_change = bp_unpack_value (bp, 1);
158 if (file_change)
159 current_file = canon_file_name
160 (string_for_index (data_in,
161 bp_unpack_var_len_unsigned (bp),
162 &len));
164 if (line_change)
165 current_line = bp_unpack_var_len_unsigned (bp);
167 if (column_change)
168 current_col = bp_unpack_var_len_unsigned (bp);
170 if (file_change)
172 if (prev_file)
173 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
175 linemap_add (line_table, LC_ENTER, false, current_file, current_line);
177 else if (line_change)
178 linemap_line_start (line_table, current_line, current_col);
180 return linemap_position_for_column (line_table, current_col);
184 /* Read a reference to a tree node from DATA_IN using input block IB.
185 TAG is the expected node that should be found in IB, if TAG belongs
186 to one of the indexable trees, expect to read a reference index to
187 be looked up in one of the symbol tables, otherwise read the pysical
188 representation of the tree using stream_read_tree. FN is the
189 function scope for the read tree. */
191 tree
192 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
193 struct function *fn, enum LTO_tags tag)
195 unsigned HOST_WIDE_INT ix_u;
196 tree result = NULL_TREE;
198 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
200 switch (tag)
202 case LTO_type_ref:
203 ix_u = streamer_read_uhwi (ib);
204 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
205 break;
207 case LTO_ssa_name_ref:
208 ix_u = streamer_read_uhwi (ib);
209 result = (*SSANAMES (fn))[ix_u];
210 break;
212 case LTO_field_decl_ref:
213 ix_u = streamer_read_uhwi (ib);
214 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
215 break;
217 case LTO_function_decl_ref:
218 ix_u = streamer_read_uhwi (ib);
219 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
220 break;
222 case LTO_type_decl_ref:
223 ix_u = streamer_read_uhwi (ib);
224 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
225 break;
227 case LTO_namespace_decl_ref:
228 ix_u = streamer_read_uhwi (ib);
229 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
230 break;
232 case LTO_global_decl_ref:
233 case LTO_result_decl_ref:
234 case LTO_const_decl_ref:
235 case LTO_imported_decl_ref:
236 case LTO_label_decl_ref:
237 case LTO_translation_unit_decl_ref:
238 ix_u = streamer_read_uhwi (ib);
239 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
240 break;
242 default:
243 gcc_unreachable ();
246 gcc_assert (result);
248 return result;
252 /* Read and return a double-linked list of catch handlers from input
253 block IB, using descriptors in DATA_IN. */
255 static struct eh_catch_d *
256 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
257 eh_catch *last_p)
259 eh_catch first;
260 enum LTO_tags tag;
262 *last_p = first = NULL;
263 tag = streamer_read_record_start (ib);
264 while (tag)
266 tree list;
267 eh_catch n;
269 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
271 /* Read the catch node. */
272 n = ggc_alloc_cleared_eh_catch_d ();
273 n->type_list = stream_read_tree (ib, data_in);
274 n->filter_list = stream_read_tree (ib, data_in);
275 n->label = stream_read_tree (ib, data_in);
277 /* Register all the types in N->FILTER_LIST. */
278 for (list = n->filter_list; list; list = TREE_CHAIN (list))
279 add_type_for_runtime (TREE_VALUE (list));
281 /* Chain N to the end of the list. */
282 if (*last_p)
283 (*last_p)->next_catch = n;
284 n->prev_catch = *last_p;
285 *last_p = n;
287 /* Set the head of the list the first time through the loop. */
288 if (first == NULL)
289 first = n;
291 tag = streamer_read_record_start (ib);
294 return first;
298 /* Read and return EH region IX from input block IB, using descriptors
299 in DATA_IN. */
301 static eh_region
302 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
304 enum LTO_tags tag;
305 eh_region r;
307 /* Read the region header. */
308 tag = streamer_read_record_start (ib);
309 if (tag == LTO_null)
310 return NULL;
312 r = ggc_alloc_cleared_eh_region_d ();
313 r->index = streamer_read_hwi (ib);
315 gcc_assert (r->index == ix);
317 /* Read all the region pointers as region numbers. We'll fix up
318 the pointers once the whole array has been read. */
319 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
320 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
321 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
323 switch (tag)
325 case LTO_ert_cleanup:
326 r->type = ERT_CLEANUP;
327 break;
329 case LTO_ert_try:
331 struct eh_catch_d *last_catch;
332 r->type = ERT_TRY;
333 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
334 &last_catch);
335 r->u.eh_try.last_catch = last_catch;
336 break;
339 case LTO_ert_allowed_exceptions:
341 tree l;
343 r->type = ERT_ALLOWED_EXCEPTIONS;
344 r->u.allowed.type_list = stream_read_tree (ib, data_in);
345 r->u.allowed.label = stream_read_tree (ib, data_in);
346 r->u.allowed.filter = streamer_read_uhwi (ib);
348 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
349 add_type_for_runtime (TREE_VALUE (l));
351 break;
353 case LTO_ert_must_not_throw:
355 r->type = ERT_MUST_NOT_THROW;
356 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
357 bitpack_d bp = streamer_read_bitpack (ib);
358 r->u.must_not_throw.failure_loc
359 = stream_input_location (&bp, data_in);
361 break;
363 default:
364 gcc_unreachable ();
367 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
369 return r;
373 /* Read and return EH landing pad IX from input block IB, using descriptors
374 in DATA_IN. */
376 static eh_landing_pad
377 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
379 enum LTO_tags tag;
380 eh_landing_pad lp;
382 /* Read the landing pad header. */
383 tag = streamer_read_record_start (ib);
384 if (tag == LTO_null)
385 return NULL;
387 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
389 lp = ggc_alloc_cleared_eh_landing_pad_d ();
390 lp->index = streamer_read_hwi (ib);
391 gcc_assert (lp->index == ix);
392 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
393 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
394 lp->post_landing_pad = stream_read_tree (ib, data_in);
396 return lp;
400 /* After reading the EH regions, pointers to peer and children regions
401 are region numbers. This converts all these region numbers into
402 real pointers into the rematerialized regions for FN. ROOT_REGION
403 is the region number for the root EH region in FN. */
405 static void
406 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
408 unsigned i;
409 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
410 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
411 eh_region r;
412 eh_landing_pad lp;
414 gcc_assert (eh_array && lp_array);
416 gcc_assert (root_region >= 0);
417 fn->eh->region_tree = (*eh_array)[root_region];
419 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
420 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
422 /* Convert all the index numbers stored in pointer fields into
423 pointers to the corresponding slots in the EH region array. */
424 FOR_EACH_VEC_ELT (*eh_array, i, r)
426 /* The array may contain NULL regions. */
427 if (r == NULL)
428 continue;
430 gcc_assert (i == (unsigned) r->index);
431 FIXUP_EH_REGION (r->outer);
432 FIXUP_EH_REGION (r->inner);
433 FIXUP_EH_REGION (r->next_peer);
434 FIXUP_EH_LP (r->landing_pads);
437 /* Convert all the index numbers stored in pointer fields into
438 pointers to the corresponding slots in the EH landing pad array. */
439 FOR_EACH_VEC_ELT (*lp_array, i, lp)
441 /* The array may contain NULL landing pads. */
442 if (lp == NULL)
443 continue;
445 gcc_assert (i == (unsigned) lp->index);
446 FIXUP_EH_LP (lp->next_lp);
447 FIXUP_EH_REGION (lp->region);
450 #undef FIXUP_EH_REGION
451 #undef FIXUP_EH_LP
455 /* Initialize EH support. */
457 void
458 lto_init_eh (void)
460 static bool eh_initialized_p = false;
462 if (eh_initialized_p)
463 return;
465 /* Contrary to most other FEs, we only initialize EH support when at
466 least one of the files in the set contains exception regions in
467 it. Since this happens much later than the call to init_eh in
468 lang_dependent_init, we have to set flag_exceptions and call
469 init_eh again to initialize the EH tables. */
470 flag_exceptions = 1;
471 init_eh ();
473 eh_initialized_p = true;
477 /* Read the exception table for FN from IB using the data descriptors
478 in DATA_IN. */
480 static void
481 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
482 struct function *fn)
484 HOST_WIDE_INT i, root_region, len;
485 enum LTO_tags tag;
487 tag = streamer_read_record_start (ib);
488 if (tag == LTO_null)
489 return;
491 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
493 /* If the file contains EH regions, then it was compiled with
494 -fexceptions. In that case, initialize the backend EH
495 machinery. */
496 lto_init_eh ();
498 gcc_assert (fn->eh);
500 root_region = streamer_read_hwi (ib);
501 gcc_assert (root_region == (int) root_region);
503 /* Read the EH region array. */
504 len = streamer_read_hwi (ib);
505 gcc_assert (len == (int) len);
506 if (len > 0)
508 vec_safe_grow_cleared (fn->eh->region_array, len);
509 for (i = 0; i < len; i++)
511 eh_region r = input_eh_region (ib, data_in, i);
512 (*fn->eh->region_array)[i] = r;
516 /* Read the landing pads. */
517 len = streamer_read_hwi (ib);
518 gcc_assert (len == (int) len);
519 if (len > 0)
521 vec_safe_grow_cleared (fn->eh->lp_array, len);
522 for (i = 0; i < len; i++)
524 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
525 (*fn->eh->lp_array)[i] = lp;
529 /* Read the runtime type data. */
530 len = streamer_read_hwi (ib);
531 gcc_assert (len == (int) len);
532 if (len > 0)
534 vec_safe_grow_cleared (fn->eh->ttype_data, len);
535 for (i = 0; i < len; i++)
537 tree ttype = stream_read_tree (ib, data_in);
538 (*fn->eh->ttype_data)[i] = ttype;
542 /* Read the table of action chains. */
543 len = streamer_read_hwi (ib);
544 gcc_assert (len == (int) len);
545 if (len > 0)
547 if (targetm.arm_eabi_unwinder)
549 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
550 for (i = 0; i < len; i++)
552 tree t = stream_read_tree (ib, data_in);
553 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
556 else
558 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
559 for (i = 0; i < len; i++)
561 uchar c = streamer_read_uchar (ib);
562 (*fn->eh->ehspec_data.other)[i] = c;
567 /* Reconstruct the EH region tree by fixing up the peer/children
568 pointers. */
569 fixup_eh_region_pointers (fn, root_region);
571 tag = streamer_read_record_start (ib);
572 lto_tag_check_range (tag, LTO_null, LTO_null);
576 /* Make a new basic block with index INDEX in function FN. */
578 static basic_block
579 make_new_block (struct function *fn, unsigned int index)
581 basic_block bb = alloc_block ();
582 bb->index = index;
583 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
584 n_basic_blocks_for_function (fn)++;
585 return bb;
589 /* Read the CFG for function FN from input block IB. */
591 static void
592 input_cfg (struct lto_input_block *ib, struct function *fn,
593 int count_materialization_scale)
595 unsigned int bb_count;
596 basic_block p_bb;
597 unsigned int i;
598 int index;
600 init_empty_tree_cfg_for_function (fn);
601 init_ssa_operands (fn);
603 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
604 PROFILE_LAST);
606 bb_count = streamer_read_uhwi (ib);
608 last_basic_block_for_function (fn) = bb_count;
609 if (bb_count > basic_block_info_for_function (fn)->length ())
610 vec_safe_grow_cleared (basic_block_info_for_function (fn), bb_count);
612 if (bb_count > label_to_block_map_for_function (fn)->length ())
613 vec_safe_grow_cleared (label_to_block_map_for_function (fn), bb_count);
615 index = streamer_read_hwi (ib);
616 while (index != -1)
618 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
619 unsigned int edge_count;
621 if (bb == NULL)
622 bb = make_new_block (fn, index);
624 edge_count = streamer_read_uhwi (ib);
626 /* Connect up the CFG. */
627 for (i = 0; i < edge_count; i++)
629 unsigned int dest_index;
630 unsigned int edge_flags;
631 basic_block dest;
632 int probability;
633 gcov_type count;
634 edge e;
636 dest_index = streamer_read_uhwi (ib);
637 probability = (int) streamer_read_hwi (ib);
638 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
639 count_materialization_scale);
640 edge_flags = streamer_read_uhwi (ib);
642 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
644 if (dest == NULL)
645 dest = make_new_block (fn, dest_index);
647 e = make_edge (bb, dest, edge_flags);
648 e->probability = probability;
649 e->count = count;
652 index = streamer_read_hwi (ib);
655 p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
656 index = streamer_read_hwi (ib);
657 while (index != -1)
659 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
660 bb->prev_bb = p_bb;
661 p_bb->next_bb = bb;
662 p_bb = bb;
663 index = streamer_read_hwi (ib);
666 /* ??? The cfgloop interface is tied to cfun. */
667 gcc_assert (cfun == fn);
669 /* Input the loop tree. */
670 unsigned n_loops = streamer_read_uhwi (ib);
671 if (n_loops == 0)
672 return;
674 struct loops *loops = ggc_alloc_cleared_loops ();
675 init_loops_structure (fn, loops, n_loops);
676 set_loops_for_fn (fn, loops);
678 /* Input each loop and associate it with its loop header so
679 flow_loops_find can rebuild the loop tree. */
680 for (unsigned i = 1; i < n_loops; ++i)
682 int header_index = streamer_read_hwi (ib);
683 if (header_index == -1)
685 loops->larray->quick_push (NULL);
686 continue;
689 struct loop *loop = alloc_loop ();
690 loop->header = BASIC_BLOCK_FOR_FUNCTION (fn, header_index);
691 loop->header->loop_father = loop;
693 /* Read everything copy_loop_info copies. */
694 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
695 loop->any_upper_bound = streamer_read_hwi (ib);
696 if (loop->any_upper_bound)
698 loop->nb_iterations_upper_bound.low = streamer_read_uhwi (ib);
699 loop->nb_iterations_upper_bound.high = streamer_read_hwi (ib);
701 loop->any_estimate = streamer_read_hwi (ib);
702 if (loop->any_estimate)
704 loop->nb_iterations_estimate.low = streamer_read_uhwi (ib);
705 loop->nb_iterations_estimate.high = streamer_read_hwi (ib);
708 place_new_loop (fn, loop);
710 /* flow_loops_find doesn't like loops not in the tree, hook them
711 all as siblings of the tree root temporarily. */
712 flow_loop_tree_node_add (loops->tree_root, loop);
715 /* Rebuild the loop tree. */
716 flow_loops_find (loops);
720 /* Read the SSA names array for function FN from DATA_IN using input
721 block IB. */
723 static void
724 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
725 struct function *fn)
727 unsigned int i, size;
729 size = streamer_read_uhwi (ib);
730 init_ssanames (fn, size);
732 i = streamer_read_uhwi (ib);
733 while (i)
735 tree ssa_name, name;
736 bool is_default_def;
738 /* Skip over the elements that had been freed. */
739 while (SSANAMES (fn)->length () < i)
740 SSANAMES (fn)->quick_push (NULL_TREE);
742 is_default_def = (streamer_read_uchar (ib) != 0);
743 name = stream_read_tree (ib, data_in);
744 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
746 if (is_default_def)
747 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
749 i = streamer_read_uhwi (ib);
754 /* Go through all NODE edges and fixup call_stmt pointers
755 so they point to STMTS. */
757 static void
758 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
760 struct cgraph_edge *cedge;
761 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
762 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
763 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
764 cedge->call_stmt = stmts[cedge->lto_stmt_uid];
767 /* Fixup call_stmt pointers in NODE and all clones. */
769 static void
770 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
772 struct cgraph_node *node;
774 while (orig->clone_of)
775 orig = orig->clone_of;
777 fixup_call_stmt_edges_1 (orig, stmts);
778 if (orig->clones)
779 for (node = orig->clones; node != orig;)
781 fixup_call_stmt_edges_1 (node, stmts);
782 if (node->clones)
783 node = node->clones;
784 else if (node->next_sibling_clone)
785 node = node->next_sibling_clone;
786 else
788 while (node != orig && !node->next_sibling_clone)
789 node = node->clone_of;
790 if (node != orig)
791 node = node->next_sibling_clone;
797 /* Input the base body of struct function FN from DATA_IN
798 using input block IB. */
800 static void
801 input_struct_function_base (struct function *fn, struct data_in *data_in,
802 struct lto_input_block *ib)
804 struct bitpack_d bp;
805 int len;
807 /* Read the static chain and non-local goto save area. */
808 fn->static_chain_decl = stream_read_tree (ib, data_in);
809 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
811 /* Read all the local symbols. */
812 len = streamer_read_hwi (ib);
813 if (len > 0)
815 int i;
816 vec_safe_grow_cleared (fn->local_decls, len);
817 for (i = 0; i < len; i++)
819 tree t = stream_read_tree (ib, data_in);
820 (*fn->local_decls)[i] = t;
824 /* Input the current IL state of the function. */
825 fn->curr_properties = streamer_read_uhwi (ib);
827 /* Read all the attributes for FN. */
828 bp = streamer_read_bitpack (ib);
829 fn->is_thunk = bp_unpack_value (&bp, 1);
830 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
831 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
832 fn->returns_struct = bp_unpack_value (&bp, 1);
833 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
834 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
835 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
836 fn->after_inlining = bp_unpack_value (&bp, 1);
837 fn->stdarg = bp_unpack_value (&bp, 1);
838 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
839 fn->calls_alloca = bp_unpack_value (&bp, 1);
840 fn->calls_setjmp = bp_unpack_value (&bp, 1);
841 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
842 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
844 /* Input the function start and end loci. */
845 fn->function_start_locus = stream_input_location (&bp, data_in);
846 fn->function_end_locus = stream_input_location (&bp, data_in);
850 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
852 static void
853 input_function (tree fn_decl, struct data_in *data_in,
854 struct lto_input_block *ib)
856 struct function *fn;
857 enum LTO_tags tag;
858 gimple *stmts;
859 basic_block bb;
860 struct cgraph_node *node;
862 fn = DECL_STRUCT_FUNCTION (fn_decl);
863 tag = streamer_read_record_start (ib);
865 gimple_register_cfg_hooks ();
866 lto_tag_check (tag, LTO_function);
868 input_struct_function_base (fn, data_in, ib);
870 /* Read all the SSA names. */
871 input_ssa_names (ib, data_in, fn);
873 /* Read the exception handling regions in the function. */
874 input_eh_regions (ib, data_in, fn);
876 /* Read the tree of lexical scopes for the function. */
877 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
878 gcc_assert (DECL_INITIAL (fn_decl));
879 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
880 node = cgraph_get_create_node (fn_decl);
882 /* Read all the basic blocks. */
883 tag = streamer_read_record_start (ib);
884 while (tag)
886 input_bb (ib, tag, data_in, fn,
887 node->count_materialization_scale);
888 tag = streamer_read_record_start (ib);
891 /* Fix up the call statements that are mentioned in the callgraph
892 edges. */
893 set_gimple_stmt_max_uid (cfun, 0);
894 FOR_ALL_BB (bb)
896 gimple_stmt_iterator gsi;
897 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
899 gimple stmt = gsi_stmt (gsi);
900 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
903 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
904 FOR_ALL_BB (bb)
906 gimple_stmt_iterator bsi = gsi_start_bb (bb);
907 while (!gsi_end_p (bsi))
909 gimple stmt = gsi_stmt (bsi);
910 /* If we're recompiling LTO objects with debug stmts but
911 we're not supposed to have debug stmts, remove them now.
912 We can't remove them earlier because this would cause uid
913 mismatches in fixups, but we can do it at this point, as
914 long as debug stmts don't require fixups. */
915 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
917 gimple_stmt_iterator gsi = bsi;
918 gsi_next (&bsi);
919 gsi_remove (&gsi, true);
921 else
923 gsi_next (&bsi);
924 stmts[gimple_uid (stmt)] = stmt;
929 /* Set the gimple body to the statement sequence in the entry
930 basic block. FIXME lto, this is fairly hacky. The existence
931 of a gimple body is used by the cgraph routines, but we should
932 really use the presence of the CFG. */
934 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
935 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
938 fixup_call_stmt_edges (node, stmts);
939 execute_all_ipa_stmt_fixups (node, stmts);
941 update_ssa (TODO_update_ssa_only_virtuals);
942 free_dominance_info (CDI_DOMINATORS);
943 free_dominance_info (CDI_POST_DOMINATORS);
944 free (stmts);
948 /* Read the body from DATA for function FN_DECL and fill it in.
949 FILE_DATA are the global decls and types. SECTION_TYPE is either
950 LTO_section_function_body or LTO_section_static_initializer. If
951 section type is LTO_section_function_body, FN must be the decl for
952 that function. */
954 static void
955 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
956 const char *data, enum lto_section_type section_type)
958 const struct lto_function_header *header;
959 struct data_in *data_in;
960 int cfg_offset;
961 int main_offset;
962 int string_offset;
963 struct lto_input_block ib_cfg;
964 struct lto_input_block ib_main;
966 header = (const struct lto_function_header *) data;
967 cfg_offset = sizeof (struct lto_function_header);
968 main_offset = cfg_offset + header->cfg_size;
969 string_offset = main_offset + header->main_size;
971 LTO_INIT_INPUT_BLOCK (ib_cfg,
972 data + cfg_offset,
974 header->cfg_size);
976 LTO_INIT_INPUT_BLOCK (ib_main,
977 data + main_offset,
979 header->main_size);
981 data_in = lto_data_in_create (file_data, data + string_offset,
982 header->string_size, vNULL);
984 /* Make sure the file was generated by the exact same compiler. */
985 lto_check_version (header->lto_header.major_version,
986 header->lto_header.minor_version);
988 if (section_type == LTO_section_function_body)
990 struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
991 struct lto_in_decl_state *decl_state;
992 struct cgraph_node *node = cgraph_get_node (fn_decl);
993 unsigned from;
995 gcc_checking_assert (node);
996 push_cfun (fn);
997 init_tree_ssa (fn);
999 /* We input IL in SSA form. */
1000 cfun->gimple_df->in_ssa_p = true;
1002 /* Use the function's decl state. */
1003 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1004 gcc_assert (decl_state);
1005 file_data->current_decl_state = decl_state;
1007 input_cfg (&ib_cfg, fn, node->count_materialization_scale);
1009 /* Set up the struct function. */
1010 from = data_in->reader_cache->nodes.length ();
1011 input_function (fn_decl, data_in, &ib_main);
1012 /* And fixup types we streamed locally. */
1014 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1015 unsigned len = cache->nodes.length ();
1016 unsigned i;
1017 for (i = len; i-- > from;)
1019 tree t = cache->nodes[i];
1020 if (t == NULL_TREE)
1021 continue;
1023 if (TYPE_P (t))
1025 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1026 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1027 if (TYPE_MAIN_VARIANT (t) != t)
1029 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1030 TYPE_NEXT_VARIANT (t)
1031 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1032 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1038 /* Restore decl state */
1039 file_data->current_decl_state = file_data->global_decl_state;
1041 pop_cfun ();
1044 lto_data_in_delete (data_in);
1048 /* Read the body of FN_DECL using DATA. FILE_DATA holds the global
1049 decls and types. */
1051 void
1052 lto_input_function_body (struct lto_file_decl_data *file_data,
1053 tree fn_decl, const char *data)
1055 lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1059 /* Read the physical representation of a tree node with tag TAG from
1060 input block IB using the per-file context in DATA_IN. */
1062 static tree
1063 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1064 enum LTO_tags tag)
1066 /* Instantiate a new tree node. */
1067 tree result = streamer_alloc_tree (ib, data_in, tag);
1069 /* Enter RESULT in the reader cache. This will make RESULT
1070 available so that circular references in the rest of the tree
1071 structure can be resolved in subsequent calls to stream_read_tree. */
1072 streamer_tree_cache_append (data_in->reader_cache, result);
1074 /* Read all the bitfield values in RESULT. Note that for LTO, we
1075 only write language-independent bitfields, so no more unpacking is
1076 needed. */
1077 streamer_read_tree_bitfields (ib, data_in, result);
1079 /* Read all the pointer fields in RESULT. */
1080 streamer_read_tree_body (ib, data_in, result);
1082 /* Read any LTO-specific data not read by the tree streamer. */
1083 if (DECL_P (result)
1084 && TREE_CODE (result) != FUNCTION_DECL
1085 && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1086 DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1088 /* We should never try to instantiate an MD or NORMAL builtin here. */
1089 if (TREE_CODE (result) == FUNCTION_DECL)
1090 gcc_assert (!streamer_handle_as_builtin_p (result));
1092 /* end_marker = */ streamer_read_uchar (ib);
1094 #ifdef LTO_STREAMER_DEBUG
1095 /* Remove the mapping to RESULT's original address set by
1096 streamer_alloc_tree. */
1097 lto_orig_address_remove (result);
1098 #endif
1100 return result;
1104 /* Read a tree from input block IB using the per-file context in
1105 DATA_IN. This context is used, for example, to resolve references
1106 to previously read nodes. */
1108 tree
1109 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1111 enum LTO_tags tag;
1112 tree result;
1114 tag = streamer_read_record_start (ib);
1115 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1117 if (tag == LTO_null)
1118 result = NULL_TREE;
1119 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1121 /* If TAG is a reference to an indexable tree, the next value
1122 in IB is the index into the table where we expect to find
1123 that tree. */
1124 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1126 else if (tag == LTO_tree_pickle_reference)
1128 /* If TAG is a reference to a previously read tree, look it up in
1129 the reader cache. */
1130 result = streamer_get_pickled_tree (ib, data_in);
1132 else if (tag == LTO_builtin_decl)
1134 /* If we are going to read a built-in function, all we need is
1135 the code and class. */
1136 result = streamer_get_builtin_tree (ib, data_in);
1138 else if (tag == LTO_integer_cst)
1140 /* For shared integer constants we only need the type and its hi/low
1141 words. */
1142 result = streamer_read_integer_cst (ib, data_in);
1144 else
1146 /* Otherwise, materialize a new node from IB. */
1147 result = lto_read_tree (ib, data_in, tag);
1150 return result;
1154 /* Input toplevel asms. */
1156 void
1157 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1159 size_t len;
1160 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1161 NULL, &len);
1162 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1163 int string_offset;
1164 struct data_in *data_in;
1165 struct lto_input_block ib;
1166 tree str;
1168 if (! data)
1169 return;
1171 string_offset = sizeof (*header) + header->main_size;
1173 LTO_INIT_INPUT_BLOCK (ib,
1174 data + sizeof (*header),
1176 header->main_size);
1178 data_in = lto_data_in_create (file_data, data + string_offset,
1179 header->string_size, vNULL);
1181 /* Make sure the file was generated by the exact same compiler. */
1182 lto_check_version (header->lto_header.major_version,
1183 header->lto_header.minor_version);
1185 while ((str = streamer_read_string_cst (data_in, &ib)))
1187 struct asm_node *node = add_asm_node (str);
1188 node->order = streamer_read_hwi (&ib) + order_base;
1189 if (node->order >= symtab_order)
1190 symtab_order = node->order + 1;
1193 lto_data_in_delete (data_in);
1195 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1199 /* Initialization for the LTO reader. */
1201 void
1202 lto_reader_init (void)
1204 lto_streamer_init ();
1205 file_name_hash_table.create (37);
1209 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1210 table to use with LEN strings. RESOLUTIONS is the vector of linker
1211 resolutions (NULL if not using a linker plugin). */
1213 struct data_in *
1214 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1215 unsigned len,
1216 vec<ld_plugin_symbol_resolution_t> resolutions)
1218 struct data_in *data_in = XCNEW (struct data_in);
1219 data_in->file_data = file_data;
1220 data_in->strings = strings;
1221 data_in->strings_len = len;
1222 data_in->globals_resolution = resolutions;
1223 data_in->reader_cache = streamer_tree_cache_create ();
1225 return data_in;
1229 /* Remove DATA_IN. */
1231 void
1232 lto_data_in_delete (struct data_in *data_in)
1234 data_in->globals_resolution.release ();
1235 streamer_tree_cache_delete (data_in->reader_cache);
1236 free (data_in->labels);
1237 free (data_in);