2013-09-04 Teresa Johnson <tejohnson@google.com>
[official-gcc.git] / gcc / lto-streamer-in.c
blobf1d5935c42760bd4524301f1abb6aad2a3b527a4
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,
759 struct function *fn)
761 struct cgraph_edge *cedge;
762 struct ipa_ref *ref;
763 unsigned int i;
765 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
767 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
768 fatal_error ("Cgraph edge statement index out of range");
769 cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
770 if (!cedge->call_stmt)
771 fatal_error ("Cgraph edge statement index not found");
773 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
775 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
776 fatal_error ("Cgraph edge statement index out of range");
777 cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
778 if (!cedge->call_stmt)
779 fatal_error ("Cgraph edge statement index not found");
781 for (i = 0;
782 ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref);
783 i++)
784 if (ref->lto_stmt_uid)
786 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
787 fatal_error ("Reference statement index out of range");
788 ref->stmt = stmts[ref->lto_stmt_uid - 1];
789 if (!ref->stmt)
790 fatal_error ("Reference statement index not found");
795 /* Fixup call_stmt pointers in NODE and all clones. */
797 static void
798 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
800 struct cgraph_node *node;
801 struct function *fn;
803 while (orig->clone_of)
804 orig = orig->clone_of;
805 fn = DECL_STRUCT_FUNCTION (orig->symbol.decl);
807 fixup_call_stmt_edges_1 (orig, stmts, fn);
808 if (orig->clones)
809 for (node = orig->clones; node != orig;)
811 fixup_call_stmt_edges_1 (node, stmts, fn);
812 if (node->clones)
813 node = node->clones;
814 else if (node->next_sibling_clone)
815 node = node->next_sibling_clone;
816 else
818 while (node != orig && !node->next_sibling_clone)
819 node = node->clone_of;
820 if (node != orig)
821 node = node->next_sibling_clone;
827 /* Input the base body of struct function FN from DATA_IN
828 using input block IB. */
830 static void
831 input_struct_function_base (struct function *fn, struct data_in *data_in,
832 struct lto_input_block *ib)
834 struct bitpack_d bp;
835 int len;
837 /* Read the static chain and non-local goto save area. */
838 fn->static_chain_decl = stream_read_tree (ib, data_in);
839 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
841 /* Read all the local symbols. */
842 len = streamer_read_hwi (ib);
843 if (len > 0)
845 int i;
846 vec_safe_grow_cleared (fn->local_decls, len);
847 for (i = 0; i < len; i++)
849 tree t = stream_read_tree (ib, data_in);
850 (*fn->local_decls)[i] = t;
854 /* Input the current IL state of the function. */
855 fn->curr_properties = streamer_read_uhwi (ib);
857 /* Read all the attributes for FN. */
858 bp = streamer_read_bitpack (ib);
859 fn->is_thunk = bp_unpack_value (&bp, 1);
860 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
861 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
862 fn->returns_struct = bp_unpack_value (&bp, 1);
863 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
864 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
865 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
866 fn->after_inlining = bp_unpack_value (&bp, 1);
867 fn->stdarg = bp_unpack_value (&bp, 1);
868 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
869 fn->calls_alloca = bp_unpack_value (&bp, 1);
870 fn->calls_setjmp = bp_unpack_value (&bp, 1);
871 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
872 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
874 /* Input the function start and end loci. */
875 fn->function_start_locus = stream_input_location (&bp, data_in);
876 fn->function_end_locus = stream_input_location (&bp, data_in);
880 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
882 static void
883 input_function (tree fn_decl, struct data_in *data_in,
884 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
886 struct function *fn;
887 enum LTO_tags tag;
888 gimple *stmts;
889 basic_block bb;
890 struct cgraph_node *node;
892 tag = streamer_read_record_start (ib);
893 lto_tag_check (tag, LTO_function);
895 /* Read decls for parameters and args. */
896 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
897 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
899 /* Read the tree of lexical scopes for the function. */
900 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
902 if (!streamer_read_uhwi (ib))
903 return;
905 push_struct_function (fn_decl);
906 fn = DECL_STRUCT_FUNCTION (fn_decl);
907 init_tree_ssa (fn);
908 /* We input IL in SSA form. */
909 cfun->gimple_df->in_ssa_p = true;
911 gimple_register_cfg_hooks ();
913 node = cgraph_get_create_node (fn_decl);
914 input_struct_function_base (fn, data_in, ib);
915 input_cfg (ib_cfg, fn, node->count_materialization_scale);
917 /* Read all the SSA names. */
918 input_ssa_names (ib, data_in, fn);
920 /* Read the exception handling regions in the function. */
921 input_eh_regions (ib, data_in, fn);
923 gcc_assert (DECL_INITIAL (fn_decl));
924 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
926 /* Read all the basic blocks. */
927 tag = streamer_read_record_start (ib);
928 while (tag)
930 input_bb (ib, tag, data_in, fn,
931 node->count_materialization_scale);
932 tag = streamer_read_record_start (ib);
935 /* Fix up the call statements that are mentioned in the callgraph
936 edges. */
937 set_gimple_stmt_max_uid (cfun, 0);
938 FOR_ALL_BB (bb)
940 gimple_stmt_iterator gsi;
941 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
943 gimple stmt = gsi_stmt (gsi);
944 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
946 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
948 gimple stmt = gsi_stmt (gsi);
949 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
952 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
953 FOR_ALL_BB (bb)
955 gimple_stmt_iterator bsi = gsi_start_phis (bb);
956 while (!gsi_end_p (bsi))
958 gimple stmt = gsi_stmt (bsi);
959 gsi_next (&bsi);
960 stmts[gimple_uid (stmt)] = stmt;
962 bsi = gsi_start_bb (bb);
963 while (!gsi_end_p (bsi))
965 gimple stmt = gsi_stmt (bsi);
966 /* If we're recompiling LTO objects with debug stmts but
967 we're not supposed to have debug stmts, remove them now.
968 We can't remove them earlier because this would cause uid
969 mismatches in fixups, but we can do it at this point, as
970 long as debug stmts don't require fixups. */
971 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
973 gimple_stmt_iterator gsi = bsi;
974 gsi_next (&bsi);
975 gsi_remove (&gsi, true);
977 else
979 gsi_next (&bsi);
980 stmts[gimple_uid (stmt)] = stmt;
985 /* Set the gimple body to the statement sequence in the entry
986 basic block. FIXME lto, this is fairly hacky. The existence
987 of a gimple body is used by the cgraph routines, but we should
988 really use the presence of the CFG. */
990 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
991 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
994 fixup_call_stmt_edges (node, stmts);
995 execute_all_ipa_stmt_fixups (node, stmts);
997 update_ssa (TODO_update_ssa_only_virtuals);
998 free_dominance_info (CDI_DOMINATORS);
999 free_dominance_info (CDI_POST_DOMINATORS);
1000 free (stmts);
1004 /* Read the body from DATA for function NODE and fill it in.
1005 FILE_DATA are the global decls and types. SECTION_TYPE is either
1006 LTO_section_function_body or LTO_section_static_initializer. If
1007 section type is LTO_section_function_body, FN must be the decl for
1008 that function. */
1010 static void
1011 lto_read_body (struct lto_file_decl_data *file_data, struct cgraph_node *node,
1012 const char *data, enum lto_section_type section_type)
1014 const struct lto_function_header *header;
1015 struct data_in *data_in;
1016 int cfg_offset;
1017 int main_offset;
1018 int string_offset;
1019 struct lto_input_block ib_cfg;
1020 struct lto_input_block ib_main;
1021 tree fn_decl = node->symbol.decl;
1023 header = (const struct lto_function_header *) data;
1024 cfg_offset = sizeof (struct lto_function_header);
1025 main_offset = cfg_offset + header->cfg_size;
1026 string_offset = main_offset + header->main_size;
1028 LTO_INIT_INPUT_BLOCK (ib_cfg,
1029 data + cfg_offset,
1031 header->cfg_size);
1033 LTO_INIT_INPUT_BLOCK (ib_main,
1034 data + main_offset,
1036 header->main_size);
1038 data_in = lto_data_in_create (file_data, data + string_offset,
1039 header->string_size, vNULL);
1041 /* Make sure the file was generated by the exact same compiler. */
1042 lto_check_version (header->lto_header.major_version,
1043 header->lto_header.minor_version);
1045 if (section_type == LTO_section_function_body)
1047 struct lto_in_decl_state *decl_state;
1048 unsigned from;
1050 gcc_checking_assert (node);
1052 /* Use the function's decl state. */
1053 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1054 gcc_assert (decl_state);
1055 file_data->current_decl_state = decl_state;
1058 /* Set up the struct function. */
1059 from = data_in->reader_cache->nodes.length ();
1060 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1061 /* And fixup types we streamed locally. */
1063 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1064 unsigned len = cache->nodes.length ();
1065 unsigned i;
1066 for (i = len; i-- > from;)
1068 tree t = streamer_tree_cache_get_tree (cache, i);
1069 if (t == NULL_TREE)
1070 continue;
1072 if (TYPE_P (t))
1074 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1075 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1076 if (TYPE_MAIN_VARIANT (t) != t)
1078 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1079 TYPE_NEXT_VARIANT (t)
1080 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1081 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1087 /* Restore decl state */
1088 file_data->current_decl_state = file_data->global_decl_state;
1090 pop_cfun ();
1093 lto_data_in_delete (data_in);
1097 /* Read the body of NODE using DATA. FILE_DATA holds the global
1098 decls and types. */
1100 void
1101 lto_input_function_body (struct lto_file_decl_data *file_data,
1102 struct cgraph_node *node, const char *data)
1104 lto_read_body (file_data, node, data, LTO_section_function_body);
1108 /* Read the physical representation of a tree node EXPR from
1109 input block IB using the per-file context in DATA_IN. */
1111 static void
1112 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1114 /* Read all the bitfield values in EXPR. Note that for LTO, we
1115 only write language-independent bitfields, so no more unpacking is
1116 needed. */
1117 streamer_read_tree_bitfields (ib, data_in, expr);
1119 /* Read all the pointer fields in EXPR. */
1120 streamer_read_tree_body (ib, data_in, expr);
1122 /* Read any LTO-specific data not read by the tree streamer. */
1123 if (DECL_P (expr)
1124 && TREE_CODE (expr) != FUNCTION_DECL
1125 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1126 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1128 /* We should never try to instantiate an MD or NORMAL builtin here. */
1129 if (TREE_CODE (expr) == FUNCTION_DECL)
1130 gcc_assert (!streamer_handle_as_builtin_p (expr));
1132 #ifdef LTO_STREAMER_DEBUG
1133 /* Remove the mapping to RESULT's original address set by
1134 streamer_alloc_tree. */
1135 lto_orig_address_remove (expr);
1136 #endif
1139 /* Read the physical representation of a tree node with tag TAG from
1140 input block IB using the per-file context in DATA_IN. */
1142 static tree
1143 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1144 enum LTO_tags tag, hashval_t hash)
1146 /* Instantiate a new tree node. */
1147 tree result = streamer_alloc_tree (ib, data_in, tag);
1149 /* Enter RESULT in the reader cache. This will make RESULT
1150 available so that circular references in the rest of the tree
1151 structure can be resolved in subsequent calls to stream_read_tree. */
1152 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1154 lto_read_tree_1 (ib, data_in, result);
1156 /* end_marker = */ streamer_read_uchar (ib);
1158 return result;
1162 /* Populate the reader cache with trees materialized from the SCC
1163 following in the IB, DATA_IN stream. */
1165 hashval_t
1166 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1167 unsigned *len, unsigned *entry_len)
1169 /* A blob of unnamed tree nodes, fill the cache from it and
1170 recurse. */
1171 unsigned size = streamer_read_uhwi (ib);
1172 hashval_t scc_hash = streamer_read_uhwi (ib);
1173 unsigned scc_entry_len = 1;
1175 if (size == 1)
1177 enum LTO_tags tag = streamer_read_record_start (ib);
1178 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1180 else
1182 unsigned int first = data_in->reader_cache->nodes.length ();
1183 tree result;
1185 scc_entry_len = streamer_read_uhwi (ib);
1187 /* Materialize size trees by reading their headers. */
1188 for (unsigned i = 0; i < size; ++i)
1190 enum LTO_tags tag = streamer_read_record_start (ib);
1191 if (tag == LTO_null
1192 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1193 || tag == LTO_tree_pickle_reference
1194 || tag == LTO_builtin_decl
1195 || tag == LTO_integer_cst
1196 || tag == LTO_tree_scc)
1197 gcc_unreachable ();
1199 result = streamer_alloc_tree (ib, data_in, tag);
1200 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1203 /* Read the tree bitpacks and references. */
1204 for (unsigned i = 0; i < size; ++i)
1206 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1207 first + i);
1208 lto_read_tree_1 (ib, data_in, result);
1209 /* end_marker = */ streamer_read_uchar (ib);
1213 *len = size;
1214 *entry_len = scc_entry_len;
1215 return scc_hash;
1219 /* Read a tree from input block IB using the per-file context in
1220 DATA_IN. This context is used, for example, to resolve references
1221 to previously read nodes. */
1223 tree
1224 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1225 enum LTO_tags tag, hashval_t hash)
1227 tree result;
1229 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1231 if (tag == LTO_null)
1232 result = NULL_TREE;
1233 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1235 /* If TAG is a reference to an indexable tree, the next value
1236 in IB is the index into the table where we expect to find
1237 that tree. */
1238 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1240 else if (tag == LTO_tree_pickle_reference)
1242 /* If TAG is a reference to a previously read tree, look it up in
1243 the reader cache. */
1244 result = streamer_get_pickled_tree (ib, data_in);
1246 else if (tag == LTO_builtin_decl)
1248 /* If we are going to read a built-in function, all we need is
1249 the code and class. */
1250 result = streamer_get_builtin_tree (ib, data_in);
1252 else if (tag == LTO_integer_cst)
1254 /* For shared integer constants in singletons we can use the existing
1255 tree integer constant merging code. */
1256 tree type = stream_read_tree (ib, data_in);
1257 unsigned HOST_WIDE_INT low = streamer_read_uhwi (ib);
1258 HOST_WIDE_INT high = streamer_read_hwi (ib);
1259 result = build_int_cst_wide (type, low, high);
1260 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1262 else if (tag == LTO_tree_scc)
1264 unsigned len, entry_len;
1266 /* Input and skip the SCC. */
1267 lto_input_scc (ib, data_in, &len, &entry_len);
1269 /* Recurse. */
1270 return lto_input_tree (ib, data_in);
1272 else
1274 /* Otherwise, materialize a new node from IB. */
1275 result = lto_read_tree (ib, data_in, tag, hash);
1278 return result;
1281 tree
1282 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1284 return lto_input_tree_1 (ib, data_in, streamer_read_record_start (ib), 0);
1288 /* Input toplevel asms. */
1290 void
1291 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1293 size_t len;
1294 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1295 NULL, &len);
1296 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1297 int string_offset;
1298 struct data_in *data_in;
1299 struct lto_input_block ib;
1300 tree str;
1302 if (! data)
1303 return;
1305 string_offset = sizeof (*header) + header->main_size;
1307 LTO_INIT_INPUT_BLOCK (ib,
1308 data + sizeof (*header),
1310 header->main_size);
1312 data_in = lto_data_in_create (file_data, data + string_offset,
1313 header->string_size, vNULL);
1315 /* Make sure the file was generated by the exact same compiler. */
1316 lto_check_version (header->lto_header.major_version,
1317 header->lto_header.minor_version);
1319 while ((str = streamer_read_string_cst (data_in, &ib)))
1321 struct asm_node *node = add_asm_node (str);
1322 node->order = streamer_read_hwi (&ib) + order_base;
1323 if (node->order >= symtab_order)
1324 symtab_order = node->order + 1;
1327 lto_data_in_delete (data_in);
1329 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1333 /* Initialization for the LTO reader. */
1335 void
1336 lto_reader_init (void)
1338 lto_streamer_init ();
1339 file_name_hash_table.create (37);
1343 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1344 table to use with LEN strings. RESOLUTIONS is the vector of linker
1345 resolutions (NULL if not using a linker plugin). */
1347 struct data_in *
1348 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1349 unsigned len,
1350 vec<ld_plugin_symbol_resolution_t> resolutions)
1352 struct data_in *data_in = XCNEW (struct data_in);
1353 data_in->file_data = file_data;
1354 data_in->strings = strings;
1355 data_in->strings_len = len;
1356 data_in->globals_resolution = resolutions;
1357 data_in->reader_cache = streamer_tree_cache_create (false, false);
1359 return data_in;
1363 /* Remove DATA_IN. */
1365 void
1366 lto_data_in_delete (struct data_in *data_in)
1368 data_in->globals_resolution.release ();
1369 streamer_tree_cache_delete (data_in->reader_cache);
1370 free (data_in->labels);
1371 free (data_in);