2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / lto-streamer-in.c
blobde25925b6235d3e06034f485d82a61056c12a3c5
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 "stringpool.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "hashtab.h"
35 #include "basic-block.h"
36 #include "gimple.h"
37 #include "gimple-iterator.h"
38 #include "gimple-ssa.h"
39 #include "tree-cfg.h"
40 #include "tree-ssanames.h"
41 #include "tree-into-ssa.h"
42 #include "tree-dfa.h"
43 #include "tree-ssa.h"
44 #include "tree-pass.h"
45 #include "function.h"
46 #include "ggc.h"
47 #include "diagnostic.h"
48 #include "except.h"
49 #include "debug.h"
50 #include "vec.h"
51 #include "ipa-utils.h"
52 #include "data-streamer.h"
53 #include "gimple-streamer.h"
54 #include "lto-streamer.h"
55 #include "tree-streamer.h"
56 #include "tree-pass.h"
57 #include "streamer-hooks.h"
58 #include "cfgloop.h"
61 struct freeing_string_slot_hasher : string_slot_hasher
63 static inline void remove (value_type *);
66 inline void
67 freeing_string_slot_hasher::remove (value_type *v)
69 free (v);
72 /* The table to hold the file names. */
73 static hash_table <freeing_string_slot_hasher> file_name_hash_table;
76 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
77 number of valid tag values to check. */
79 void
80 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
82 va_list ap;
83 int i;
85 va_start (ap, ntags);
86 for (i = 0; i < ntags; i++)
87 if ((unsigned) actual == va_arg (ap, unsigned))
89 va_end (ap);
90 return;
93 va_end (ap);
94 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
98 /* Read LENGTH bytes from STREAM to ADDR. */
100 void
101 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
103 size_t i;
104 unsigned char *const buffer = (unsigned char *const) addr;
106 for (i = 0; i < length; i++)
107 buffer[i] = streamer_read_uchar (ib);
111 /* Lookup STRING in file_name_hash_table. If found, return the existing
112 string, otherwise insert STRING as the canonical version. */
114 static const char *
115 canon_file_name (const char *string)
117 string_slot **slot;
118 struct string_slot s_slot;
119 size_t len = strlen (string);
121 s_slot.s = string;
122 s_slot.len = len;
124 slot = file_name_hash_table.find_slot (&s_slot, INSERT);
125 if (*slot == NULL)
127 char *saved_string;
128 struct string_slot *new_slot;
130 saved_string = (char *) xmalloc (len + 1);
131 new_slot = XCNEW (struct string_slot);
132 memcpy (saved_string, string, len + 1);
133 new_slot->s = saved_string;
134 new_slot->len = len;
135 *slot = new_slot;
136 return saved_string;
138 else
140 struct string_slot *old_slot = *slot;
141 return old_slot->s;
146 /* Read a location bitpack from input block IB. */
148 location_t
149 lto_input_location (struct bitpack_d *bp, struct data_in *data_in)
151 static const char *current_file;
152 static int current_line;
153 static int current_col;
154 bool file_change, line_change, column_change;
155 unsigned len;
156 bool prev_file = current_file != NULL;
158 if (bp_unpack_value (bp, 1))
159 return UNKNOWN_LOCATION;
161 file_change = bp_unpack_value (bp, 1);
162 line_change = bp_unpack_value (bp, 1);
163 column_change = bp_unpack_value (bp, 1);
165 if (file_change)
166 current_file = canon_file_name
167 (string_for_index (data_in,
168 bp_unpack_var_len_unsigned (bp),
169 &len));
171 if (line_change)
172 current_line = bp_unpack_var_len_unsigned (bp);
174 if (column_change)
175 current_col = bp_unpack_var_len_unsigned (bp);
177 if (file_change)
179 if (prev_file)
180 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
182 linemap_add (line_table, LC_ENTER, false, current_file, current_line);
184 else if (line_change)
185 linemap_line_start (line_table, current_line, current_col);
187 return linemap_position_for_column (line_table, current_col);
191 /* Read a reference to a tree node from DATA_IN using input block IB.
192 TAG is the expected node that should be found in IB, if TAG belongs
193 to one of the indexable trees, expect to read a reference index to
194 be looked up in one of the symbol tables, otherwise read the pysical
195 representation of the tree using stream_read_tree. FN is the
196 function scope for the read tree. */
198 tree
199 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
200 struct function *fn, enum LTO_tags tag)
202 unsigned HOST_WIDE_INT ix_u;
203 tree result = NULL_TREE;
205 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
207 switch (tag)
209 case LTO_type_ref:
210 ix_u = streamer_read_uhwi (ib);
211 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
212 break;
214 case LTO_ssa_name_ref:
215 ix_u = streamer_read_uhwi (ib);
216 result = (*SSANAMES (fn))[ix_u];
217 break;
219 case LTO_field_decl_ref:
220 ix_u = streamer_read_uhwi (ib);
221 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
222 break;
224 case LTO_function_decl_ref:
225 ix_u = streamer_read_uhwi (ib);
226 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
227 break;
229 case LTO_type_decl_ref:
230 ix_u = streamer_read_uhwi (ib);
231 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
232 break;
234 case LTO_namespace_decl_ref:
235 ix_u = streamer_read_uhwi (ib);
236 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
237 break;
239 case LTO_global_decl_ref:
240 case LTO_result_decl_ref:
241 case LTO_const_decl_ref:
242 case LTO_imported_decl_ref:
243 case LTO_label_decl_ref:
244 case LTO_translation_unit_decl_ref:
245 ix_u = streamer_read_uhwi (ib);
246 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
247 break;
249 default:
250 gcc_unreachable ();
253 gcc_assert (result);
255 return result;
259 /* Read and return a double-linked list of catch handlers from input
260 block IB, using descriptors in DATA_IN. */
262 static struct eh_catch_d *
263 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
264 eh_catch *last_p)
266 eh_catch first;
267 enum LTO_tags tag;
269 *last_p = first = NULL;
270 tag = streamer_read_record_start (ib);
271 while (tag)
273 tree list;
274 eh_catch n;
276 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
278 /* Read the catch node. */
279 n = ggc_alloc_cleared_eh_catch_d ();
280 n->type_list = stream_read_tree (ib, data_in);
281 n->filter_list = stream_read_tree (ib, data_in);
282 n->label = stream_read_tree (ib, data_in);
284 /* Register all the types in N->FILTER_LIST. */
285 for (list = n->filter_list; list; list = TREE_CHAIN (list))
286 add_type_for_runtime (TREE_VALUE (list));
288 /* Chain N to the end of the list. */
289 if (*last_p)
290 (*last_p)->next_catch = n;
291 n->prev_catch = *last_p;
292 *last_p = n;
294 /* Set the head of the list the first time through the loop. */
295 if (first == NULL)
296 first = n;
298 tag = streamer_read_record_start (ib);
301 return first;
305 /* Read and return EH region IX from input block IB, using descriptors
306 in DATA_IN. */
308 static eh_region
309 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
311 enum LTO_tags tag;
312 eh_region r;
314 /* Read the region header. */
315 tag = streamer_read_record_start (ib);
316 if (tag == LTO_null)
317 return NULL;
319 r = ggc_alloc_cleared_eh_region_d ();
320 r->index = streamer_read_hwi (ib);
322 gcc_assert (r->index == ix);
324 /* Read all the region pointers as region numbers. We'll fix up
325 the pointers once the whole array has been read. */
326 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
327 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
328 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
330 switch (tag)
332 case LTO_ert_cleanup:
333 r->type = ERT_CLEANUP;
334 break;
336 case LTO_ert_try:
338 struct eh_catch_d *last_catch;
339 r->type = ERT_TRY;
340 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
341 &last_catch);
342 r->u.eh_try.last_catch = last_catch;
343 break;
346 case LTO_ert_allowed_exceptions:
348 tree l;
350 r->type = ERT_ALLOWED_EXCEPTIONS;
351 r->u.allowed.type_list = stream_read_tree (ib, data_in);
352 r->u.allowed.label = stream_read_tree (ib, data_in);
353 r->u.allowed.filter = streamer_read_uhwi (ib);
355 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
356 add_type_for_runtime (TREE_VALUE (l));
358 break;
360 case LTO_ert_must_not_throw:
362 r->type = ERT_MUST_NOT_THROW;
363 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
364 bitpack_d bp = streamer_read_bitpack (ib);
365 r->u.must_not_throw.failure_loc
366 = stream_input_location (&bp, data_in);
368 break;
370 default:
371 gcc_unreachable ();
374 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
376 return r;
380 /* Read and return EH landing pad IX from input block IB, using descriptors
381 in DATA_IN. */
383 static eh_landing_pad
384 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
386 enum LTO_tags tag;
387 eh_landing_pad lp;
389 /* Read the landing pad header. */
390 tag = streamer_read_record_start (ib);
391 if (tag == LTO_null)
392 return NULL;
394 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
396 lp = ggc_alloc_cleared_eh_landing_pad_d ();
397 lp->index = streamer_read_hwi (ib);
398 gcc_assert (lp->index == ix);
399 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
400 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
401 lp->post_landing_pad = stream_read_tree (ib, data_in);
403 return lp;
407 /* After reading the EH regions, pointers to peer and children regions
408 are region numbers. This converts all these region numbers into
409 real pointers into the rematerialized regions for FN. ROOT_REGION
410 is the region number for the root EH region in FN. */
412 static void
413 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
415 unsigned i;
416 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
417 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
418 eh_region r;
419 eh_landing_pad lp;
421 gcc_assert (eh_array && lp_array);
423 gcc_assert (root_region >= 0);
424 fn->eh->region_tree = (*eh_array)[root_region];
426 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
427 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
429 /* Convert all the index numbers stored in pointer fields into
430 pointers to the corresponding slots in the EH region array. */
431 FOR_EACH_VEC_ELT (*eh_array, i, r)
433 /* The array may contain NULL regions. */
434 if (r == NULL)
435 continue;
437 gcc_assert (i == (unsigned) r->index);
438 FIXUP_EH_REGION (r->outer);
439 FIXUP_EH_REGION (r->inner);
440 FIXUP_EH_REGION (r->next_peer);
441 FIXUP_EH_LP (r->landing_pads);
444 /* Convert all the index numbers stored in pointer fields into
445 pointers to the corresponding slots in the EH landing pad array. */
446 FOR_EACH_VEC_ELT (*lp_array, i, lp)
448 /* The array may contain NULL landing pads. */
449 if (lp == NULL)
450 continue;
452 gcc_assert (i == (unsigned) lp->index);
453 FIXUP_EH_LP (lp->next_lp);
454 FIXUP_EH_REGION (lp->region);
457 #undef FIXUP_EH_REGION
458 #undef FIXUP_EH_LP
462 /* Initialize EH support. */
464 void
465 lto_init_eh (void)
467 static bool eh_initialized_p = false;
469 if (eh_initialized_p)
470 return;
472 /* Contrary to most other FEs, we only initialize EH support when at
473 least one of the files in the set contains exception regions in
474 it. Since this happens much later than the call to init_eh in
475 lang_dependent_init, we have to set flag_exceptions and call
476 init_eh again to initialize the EH tables. */
477 flag_exceptions = 1;
478 init_eh ();
480 eh_initialized_p = true;
484 /* Read the exception table for FN from IB using the data descriptors
485 in DATA_IN. */
487 static void
488 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
489 struct function *fn)
491 HOST_WIDE_INT i, root_region, len;
492 enum LTO_tags tag;
494 tag = streamer_read_record_start (ib);
495 if (tag == LTO_null)
496 return;
498 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
500 /* If the file contains EH regions, then it was compiled with
501 -fexceptions. In that case, initialize the backend EH
502 machinery. */
503 lto_init_eh ();
505 gcc_assert (fn->eh);
507 root_region = streamer_read_hwi (ib);
508 gcc_assert (root_region == (int) root_region);
510 /* Read the EH region array. */
511 len = streamer_read_hwi (ib);
512 gcc_assert (len == (int) len);
513 if (len > 0)
515 vec_safe_grow_cleared (fn->eh->region_array, len);
516 for (i = 0; i < len; i++)
518 eh_region r = input_eh_region (ib, data_in, i);
519 (*fn->eh->region_array)[i] = r;
523 /* Read the landing pads. */
524 len = streamer_read_hwi (ib);
525 gcc_assert (len == (int) len);
526 if (len > 0)
528 vec_safe_grow_cleared (fn->eh->lp_array, len);
529 for (i = 0; i < len; i++)
531 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
532 (*fn->eh->lp_array)[i] = lp;
536 /* Read the runtime type data. */
537 len = streamer_read_hwi (ib);
538 gcc_assert (len == (int) len);
539 if (len > 0)
541 vec_safe_grow_cleared (fn->eh->ttype_data, len);
542 for (i = 0; i < len; i++)
544 tree ttype = stream_read_tree (ib, data_in);
545 (*fn->eh->ttype_data)[i] = ttype;
549 /* Read the table of action chains. */
550 len = streamer_read_hwi (ib);
551 gcc_assert (len == (int) len);
552 if (len > 0)
554 if (targetm.arm_eabi_unwinder)
556 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
557 for (i = 0; i < len; i++)
559 tree t = stream_read_tree (ib, data_in);
560 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
563 else
565 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
566 for (i = 0; i < len; i++)
568 uchar c = streamer_read_uchar (ib);
569 (*fn->eh->ehspec_data.other)[i] = c;
574 /* Reconstruct the EH region tree by fixing up the peer/children
575 pointers. */
576 fixup_eh_region_pointers (fn, root_region);
578 tag = streamer_read_record_start (ib);
579 lto_tag_check_range (tag, LTO_null, LTO_null);
583 /* Make a new basic block with index INDEX in function FN. */
585 static basic_block
586 make_new_block (struct function *fn, unsigned int index)
588 basic_block bb = alloc_block ();
589 bb->index = index;
590 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
591 n_basic_blocks_for_fn (fn)++;
592 return bb;
596 /* Read the CFG for function FN from input block IB. */
598 static void
599 input_cfg (struct lto_input_block *ib, struct function *fn,
600 int count_materialization_scale)
602 unsigned int bb_count;
603 basic_block p_bb;
604 unsigned int i;
605 int index;
607 init_empty_tree_cfg_for_function (fn);
608 init_ssa_operands (fn);
610 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
611 PROFILE_LAST);
613 bb_count = streamer_read_uhwi (ib);
615 last_basic_block_for_function (fn) = bb_count;
616 if (bb_count > basic_block_info_for_function (fn)->length ())
617 vec_safe_grow_cleared (basic_block_info_for_function (fn), bb_count);
619 if (bb_count > label_to_block_map_for_function (fn)->length ())
620 vec_safe_grow_cleared (label_to_block_map_for_function (fn), bb_count);
622 index = streamer_read_hwi (ib);
623 while (index != -1)
625 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
626 unsigned int edge_count;
628 if (bb == NULL)
629 bb = make_new_block (fn, index);
631 edge_count = streamer_read_uhwi (ib);
633 /* Connect up the CFG. */
634 for (i = 0; i < edge_count; i++)
636 unsigned int dest_index;
637 unsigned int edge_flags;
638 basic_block dest;
639 int probability;
640 gcov_type count;
641 edge e;
643 dest_index = streamer_read_uhwi (ib);
644 probability = (int) streamer_read_hwi (ib);
645 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
646 count_materialization_scale);
647 edge_flags = streamer_read_uhwi (ib);
649 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
651 if (dest == NULL)
652 dest = make_new_block (fn, dest_index);
654 e = make_edge (bb, dest, edge_flags);
655 e->probability = probability;
656 e->count = count;
659 index = streamer_read_hwi (ib);
662 p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
663 index = streamer_read_hwi (ib);
664 while (index != -1)
666 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
667 bb->prev_bb = p_bb;
668 p_bb->next_bb = bb;
669 p_bb = bb;
670 index = streamer_read_hwi (ib);
673 /* ??? The cfgloop interface is tied to cfun. */
674 gcc_assert (cfun == fn);
676 /* Input the loop tree. */
677 unsigned n_loops = streamer_read_uhwi (ib);
678 if (n_loops == 0)
679 return;
681 struct loops *loops = ggc_alloc_cleared_loops ();
682 init_loops_structure (fn, loops, n_loops);
683 set_loops_for_fn (fn, loops);
685 /* Input each loop and associate it with its loop header so
686 flow_loops_find can rebuild the loop tree. */
687 for (unsigned i = 1; i < n_loops; ++i)
689 int header_index = streamer_read_hwi (ib);
690 if (header_index == -1)
692 loops->larray->quick_push (NULL);
693 continue;
696 struct loop *loop = alloc_loop ();
697 loop->header = BASIC_BLOCK_FOR_FUNCTION (fn, header_index);
698 loop->header->loop_father = loop;
700 /* Read everything copy_loop_info copies. */
701 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
702 loop->any_upper_bound = streamer_read_hwi (ib);
703 if (loop->any_upper_bound)
705 loop->nb_iterations_upper_bound.low = streamer_read_uhwi (ib);
706 loop->nb_iterations_upper_bound.high = streamer_read_hwi (ib);
708 loop->any_estimate = streamer_read_hwi (ib);
709 if (loop->any_estimate)
711 loop->nb_iterations_estimate.low = streamer_read_uhwi (ib);
712 loop->nb_iterations_estimate.high = streamer_read_hwi (ib);
715 place_new_loop (fn, loop);
717 /* flow_loops_find doesn't like loops not in the tree, hook them
718 all as siblings of the tree root temporarily. */
719 flow_loop_tree_node_add (loops->tree_root, loop);
722 /* Rebuild the loop tree. */
723 flow_loops_find (loops);
727 /* Read the SSA names array for function FN from DATA_IN using input
728 block IB. */
730 static void
731 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
732 struct function *fn)
734 unsigned int i, size;
736 size = streamer_read_uhwi (ib);
737 init_ssanames (fn, size);
739 i = streamer_read_uhwi (ib);
740 while (i)
742 tree ssa_name, name;
743 bool is_default_def;
745 /* Skip over the elements that had been freed. */
746 while (SSANAMES (fn)->length () < i)
747 SSANAMES (fn)->quick_push (NULL_TREE);
749 is_default_def = (streamer_read_uchar (ib) != 0);
750 name = stream_read_tree (ib, data_in);
751 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
753 if (is_default_def)
754 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
756 i = streamer_read_uhwi (ib);
761 /* Go through all NODE edges and fixup call_stmt pointers
762 so they point to STMTS. */
764 static void
765 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
766 struct function *fn)
768 struct cgraph_edge *cedge;
769 struct ipa_ref *ref;
770 unsigned int i;
772 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
774 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
775 fatal_error ("Cgraph edge statement index out of range");
776 cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
777 if (!cedge->call_stmt)
778 fatal_error ("Cgraph edge statement index not found");
780 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
782 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
783 fatal_error ("Cgraph edge statement index out of range");
784 cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
785 if (!cedge->call_stmt)
786 fatal_error ("Cgraph edge statement index not found");
788 for (i = 0;
789 ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
790 i++)
791 if (ref->lto_stmt_uid)
793 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
794 fatal_error ("Reference statement index out of range");
795 ref->stmt = stmts[ref->lto_stmt_uid - 1];
796 if (!ref->stmt)
797 fatal_error ("Reference statement index not found");
802 /* Fixup call_stmt pointers in NODE and all clones. */
804 static void
805 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
807 struct cgraph_node *node;
808 struct function *fn;
810 while (orig->clone_of)
811 orig = orig->clone_of;
812 fn = DECL_STRUCT_FUNCTION (orig->decl);
814 fixup_call_stmt_edges_1 (orig, stmts, fn);
815 if (orig->clones)
816 for (node = orig->clones; node != orig;)
818 fixup_call_stmt_edges_1 (node, stmts, fn);
819 if (node->clones)
820 node = node->clones;
821 else if (node->next_sibling_clone)
822 node = node->next_sibling_clone;
823 else
825 while (node != orig && !node->next_sibling_clone)
826 node = node->clone_of;
827 if (node != orig)
828 node = node->next_sibling_clone;
834 /* Input the base body of struct function FN from DATA_IN
835 using input block IB. */
837 static void
838 input_struct_function_base (struct function *fn, struct data_in *data_in,
839 struct lto_input_block *ib)
841 struct bitpack_d bp;
842 int len;
844 /* Read the static chain and non-local goto save area. */
845 fn->static_chain_decl = stream_read_tree (ib, data_in);
846 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
848 /* Read all the local symbols. */
849 len = streamer_read_hwi (ib);
850 if (len > 0)
852 int i;
853 vec_safe_grow_cleared (fn->local_decls, len);
854 for (i = 0; i < len; i++)
856 tree t = stream_read_tree (ib, data_in);
857 (*fn->local_decls)[i] = t;
861 /* Input the current IL state of the function. */
862 fn->curr_properties = streamer_read_uhwi (ib);
864 /* Read all the attributes for FN. */
865 bp = streamer_read_bitpack (ib);
866 fn->is_thunk = bp_unpack_value (&bp, 1);
867 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
868 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
869 fn->returns_struct = bp_unpack_value (&bp, 1);
870 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
871 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
872 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
873 fn->after_inlining = bp_unpack_value (&bp, 1);
874 fn->stdarg = bp_unpack_value (&bp, 1);
875 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
876 fn->calls_alloca = bp_unpack_value (&bp, 1);
877 fn->calls_setjmp = bp_unpack_value (&bp, 1);
878 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
879 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
881 /* Input the function start and end loci. */
882 fn->function_start_locus = stream_input_location (&bp, data_in);
883 fn->function_end_locus = stream_input_location (&bp, data_in);
887 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
889 static void
890 input_function (tree fn_decl, struct data_in *data_in,
891 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
893 struct function *fn;
894 enum LTO_tags tag;
895 gimple *stmts;
896 basic_block bb;
897 struct cgraph_node *node;
899 tag = streamer_read_record_start (ib);
900 lto_tag_check (tag, LTO_function);
902 /* Read decls for parameters and args. */
903 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
904 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
906 /* Read the tree of lexical scopes for the function. */
907 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
909 if (!streamer_read_uhwi (ib))
910 return;
912 push_struct_function (fn_decl);
913 fn = DECL_STRUCT_FUNCTION (fn_decl);
914 init_tree_ssa (fn);
915 /* We input IL in SSA form. */
916 cfun->gimple_df->in_ssa_p = true;
918 gimple_register_cfg_hooks ();
920 node = cgraph_get_node (fn_decl);
921 if (!node)
922 node = cgraph_create_node (fn_decl);
923 input_struct_function_base (fn, data_in, ib);
924 input_cfg (ib_cfg, fn, node->count_materialization_scale);
926 /* Read all the SSA names. */
927 input_ssa_names (ib, data_in, fn);
929 /* Read the exception handling regions in the function. */
930 input_eh_regions (ib, data_in, fn);
932 gcc_assert (DECL_INITIAL (fn_decl));
933 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
935 /* Read all the basic blocks. */
936 tag = streamer_read_record_start (ib);
937 while (tag)
939 input_bb (ib, tag, data_in, fn,
940 node->count_materialization_scale);
941 tag = streamer_read_record_start (ib);
944 /* Fix up the call statements that are mentioned in the callgraph
945 edges. */
946 set_gimple_stmt_max_uid (cfun, 0);
947 FOR_ALL_BB (bb)
949 gimple_stmt_iterator gsi;
950 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
952 gimple stmt = gsi_stmt (gsi);
953 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
955 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
957 gimple stmt = gsi_stmt (gsi);
958 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
961 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
962 FOR_ALL_BB (bb)
964 gimple_stmt_iterator bsi = gsi_start_phis (bb);
965 while (!gsi_end_p (bsi))
967 gimple stmt = gsi_stmt (bsi);
968 gsi_next (&bsi);
969 stmts[gimple_uid (stmt)] = stmt;
971 bsi = gsi_start_bb (bb);
972 while (!gsi_end_p (bsi))
974 gimple stmt = gsi_stmt (bsi);
975 /* If we're recompiling LTO objects with debug stmts but
976 we're not supposed to have debug stmts, remove them now.
977 We can't remove them earlier because this would cause uid
978 mismatches in fixups, but we can do it at this point, as
979 long as debug stmts don't require fixups. */
980 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
982 gimple_stmt_iterator gsi = bsi;
983 gsi_next (&bsi);
984 gsi_remove (&gsi, true);
986 else
988 gsi_next (&bsi);
989 stmts[gimple_uid (stmt)] = stmt;
994 /* Set the gimple body to the statement sequence in the entry
995 basic block. FIXME lto, this is fairly hacky. The existence
996 of a gimple body is used by the cgraph routines, but we should
997 really use the presence of the CFG. */
999 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1000 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1003 fixup_call_stmt_edges (node, stmts);
1004 execute_all_ipa_stmt_fixups (node, stmts);
1006 update_ssa (TODO_update_ssa_only_virtuals);
1007 free_dominance_info (CDI_DOMINATORS);
1008 free_dominance_info (CDI_POST_DOMINATORS);
1009 free (stmts);
1010 pop_cfun ();
1014 /* Read the body from DATA for function NODE and fill it in.
1015 FILE_DATA are the global decls and types. SECTION_TYPE is either
1016 LTO_section_function_body or LTO_section_static_initializer. If
1017 section type is LTO_section_function_body, FN must be the decl for
1018 that function. */
1020 static void
1021 lto_read_body (struct lto_file_decl_data *file_data, struct cgraph_node *node,
1022 const char *data, enum lto_section_type section_type)
1024 const struct lto_function_header *header;
1025 struct data_in *data_in;
1026 int cfg_offset;
1027 int main_offset;
1028 int string_offset;
1029 struct lto_input_block ib_cfg;
1030 struct lto_input_block ib_main;
1031 tree fn_decl = node->decl;
1033 header = (const struct lto_function_header *) data;
1034 cfg_offset = sizeof (struct lto_function_header);
1035 main_offset = cfg_offset + header->cfg_size;
1036 string_offset = main_offset + header->main_size;
1038 LTO_INIT_INPUT_BLOCK (ib_cfg,
1039 data + cfg_offset,
1041 header->cfg_size);
1043 LTO_INIT_INPUT_BLOCK (ib_main,
1044 data + main_offset,
1046 header->main_size);
1048 data_in = lto_data_in_create (file_data, data + string_offset,
1049 header->string_size, vNULL);
1051 /* Make sure the file was generated by the exact same compiler. */
1052 lto_check_version (header->lto_header.major_version,
1053 header->lto_header.minor_version);
1055 if (section_type == LTO_section_function_body)
1057 struct lto_in_decl_state *decl_state;
1058 unsigned from;
1060 gcc_checking_assert (node);
1062 /* Use the function's decl state. */
1063 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1064 gcc_assert (decl_state);
1065 file_data->current_decl_state = decl_state;
1068 /* Set up the struct function. */
1069 from = data_in->reader_cache->nodes.length ();
1070 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1071 /* And fixup types we streamed locally. */
1073 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1074 unsigned len = cache->nodes.length ();
1075 unsigned i;
1076 for (i = len; i-- > from;)
1078 tree t = streamer_tree_cache_get_tree (cache, i);
1079 if (t == NULL_TREE)
1080 continue;
1082 if (TYPE_P (t))
1084 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1085 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1086 if (TYPE_MAIN_VARIANT (t) != t)
1088 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1089 TYPE_NEXT_VARIANT (t)
1090 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1091 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1097 /* Restore decl state */
1098 file_data->current_decl_state = file_data->global_decl_state;
1101 lto_data_in_delete (data_in);
1105 /* Read the body of NODE using DATA. FILE_DATA holds the global
1106 decls and types. */
1108 void
1109 lto_input_function_body (struct lto_file_decl_data *file_data,
1110 struct cgraph_node *node, const char *data)
1112 lto_read_body (file_data, node, data, LTO_section_function_body);
1116 /* Read the physical representation of a tree node EXPR from
1117 input block IB using the per-file context in DATA_IN. */
1119 static void
1120 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1122 /* Read all the bitfield values in EXPR. Note that for LTO, we
1123 only write language-independent bitfields, so no more unpacking is
1124 needed. */
1125 streamer_read_tree_bitfields (ib, data_in, expr);
1127 /* Read all the pointer fields in EXPR. */
1128 streamer_read_tree_body (ib, data_in, expr);
1130 /* Read any LTO-specific data not read by the tree streamer. */
1131 if (DECL_P (expr)
1132 && TREE_CODE (expr) != FUNCTION_DECL
1133 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1134 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1136 /* We should never try to instantiate an MD or NORMAL builtin here. */
1137 if (TREE_CODE (expr) == FUNCTION_DECL)
1138 gcc_assert (!streamer_handle_as_builtin_p (expr));
1140 #ifdef LTO_STREAMER_DEBUG
1141 /* Remove the mapping to RESULT's original address set by
1142 streamer_alloc_tree. */
1143 lto_orig_address_remove (expr);
1144 #endif
1147 /* Read the physical representation of a tree node with tag TAG from
1148 input block IB using the per-file context in DATA_IN. */
1150 static tree
1151 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1152 enum LTO_tags tag, hashval_t hash)
1154 /* Instantiate a new tree node. */
1155 tree result = streamer_alloc_tree (ib, data_in, tag);
1157 /* Enter RESULT in the reader cache. This will make RESULT
1158 available so that circular references in the rest of the tree
1159 structure can be resolved in subsequent calls to stream_read_tree. */
1160 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1162 lto_read_tree_1 (ib, data_in, result);
1164 /* end_marker = */ streamer_read_uchar (ib);
1166 return result;
1170 /* Populate the reader cache with trees materialized from the SCC
1171 following in the IB, DATA_IN stream. */
1173 hashval_t
1174 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1175 unsigned *len, unsigned *entry_len)
1177 /* A blob of unnamed tree nodes, fill the cache from it and
1178 recurse. */
1179 unsigned size = streamer_read_uhwi (ib);
1180 hashval_t scc_hash = streamer_read_uhwi (ib);
1181 unsigned scc_entry_len = 1;
1183 if (size == 1)
1185 enum LTO_tags tag = streamer_read_record_start (ib);
1186 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1188 else
1190 unsigned int first = data_in->reader_cache->nodes.length ();
1191 tree result;
1193 scc_entry_len = streamer_read_uhwi (ib);
1195 /* Materialize size trees by reading their headers. */
1196 for (unsigned i = 0; i < size; ++i)
1198 enum LTO_tags tag = streamer_read_record_start (ib);
1199 if (tag == LTO_null
1200 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1201 || tag == LTO_tree_pickle_reference
1202 || tag == LTO_builtin_decl
1203 || tag == LTO_integer_cst
1204 || tag == LTO_tree_scc)
1205 gcc_unreachable ();
1207 result = streamer_alloc_tree (ib, data_in, tag);
1208 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1211 /* Read the tree bitpacks and references. */
1212 for (unsigned i = 0; i < size; ++i)
1214 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1215 first + i);
1216 lto_read_tree_1 (ib, data_in, result);
1217 /* end_marker = */ streamer_read_uchar (ib);
1221 *len = size;
1222 *entry_len = scc_entry_len;
1223 return scc_hash;
1227 /* Read a tree from input block IB using the per-file context in
1228 DATA_IN. This context is used, for example, to resolve references
1229 to previously read nodes. */
1231 tree
1232 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1233 enum LTO_tags tag, hashval_t hash)
1235 tree result;
1237 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1239 if (tag == LTO_null)
1240 result = NULL_TREE;
1241 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1243 /* If TAG is a reference to an indexable tree, the next value
1244 in IB is the index into the table where we expect to find
1245 that tree. */
1246 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1248 else if (tag == LTO_tree_pickle_reference)
1250 /* If TAG is a reference to a previously read tree, look it up in
1251 the reader cache. */
1252 result = streamer_get_pickled_tree (ib, data_in);
1254 else if (tag == LTO_builtin_decl)
1256 /* If we are going to read a built-in function, all we need is
1257 the code and class. */
1258 result = streamer_get_builtin_tree (ib, data_in);
1260 else if (tag == LTO_integer_cst)
1262 /* For shared integer constants in singletons we can use the existing
1263 tree integer constant merging code. */
1264 tree type = stream_read_tree (ib, data_in);
1265 unsigned HOST_WIDE_INT low = streamer_read_uhwi (ib);
1266 HOST_WIDE_INT high = streamer_read_hwi (ib);
1267 result = build_int_cst_wide (type, low, high);
1268 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1270 else if (tag == LTO_tree_scc)
1272 unsigned len, entry_len;
1274 /* Input and skip the SCC. */
1275 lto_input_scc (ib, data_in, &len, &entry_len);
1277 /* Recurse. */
1278 return lto_input_tree (ib, data_in);
1280 else
1282 /* Otherwise, materialize a new node from IB. */
1283 result = lto_read_tree (ib, data_in, tag, hash);
1286 return result;
1289 tree
1290 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1292 return lto_input_tree_1 (ib, data_in, streamer_read_record_start (ib), 0);
1296 /* Input toplevel asms. */
1298 void
1299 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1301 size_t len;
1302 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1303 NULL, &len);
1304 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1305 int string_offset;
1306 struct data_in *data_in;
1307 struct lto_input_block ib;
1308 tree str;
1310 if (! data)
1311 return;
1313 string_offset = sizeof (*header) + header->main_size;
1315 LTO_INIT_INPUT_BLOCK (ib,
1316 data + sizeof (*header),
1318 header->main_size);
1320 data_in = lto_data_in_create (file_data, data + string_offset,
1321 header->string_size, vNULL);
1323 /* Make sure the file was generated by the exact same compiler. */
1324 lto_check_version (header->lto_header.major_version,
1325 header->lto_header.minor_version);
1327 while ((str = streamer_read_string_cst (data_in, &ib)))
1329 struct asm_node *node = add_asm_node (str);
1330 node->order = streamer_read_hwi (&ib) + order_base;
1331 if (node->order >= symtab_order)
1332 symtab_order = node->order + 1;
1335 lto_data_in_delete (data_in);
1337 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1341 /* Initialization for the LTO reader. */
1343 void
1344 lto_reader_init (void)
1346 lto_streamer_init ();
1347 file_name_hash_table.create (37);
1351 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1352 table to use with LEN strings. RESOLUTIONS is the vector of linker
1353 resolutions (NULL if not using a linker plugin). */
1355 struct data_in *
1356 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1357 unsigned len,
1358 vec<ld_plugin_symbol_resolution_t> resolutions)
1360 struct data_in *data_in = XCNEW (struct data_in);
1361 data_in->file_data = file_data;
1362 data_in->strings = strings;
1363 data_in->strings_len = len;
1364 data_in->globals_resolution = resolutions;
1365 data_in->reader_cache = streamer_tree_cache_create (false, false);
1367 return data_in;
1371 /* Remove DATA_IN. */
1373 void
1374 lto_data_in_delete (struct data_in *data_in)
1376 data_in->globals_resolution.release ();
1377 streamer_tree_cache_delete (data_in->reader_cache);
1378 free (data_in->labels);
1379 free (data_in);