2015-02-05 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / lto-streamer-in.c
blobe12d00ac8137fe60bf67344ca504a4599d2eeb16
1 /* Read the GIMPLE representation from a file stream.
3 Copyright (C) 2009-2015 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 "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "fold-const.h"
39 #include "stringpool.h"
40 #include "hashtab.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "rtl.h"
44 #include "flags.h"
45 #include "statistics.h"
46 #include "real.h"
47 #include "fixed-value.h"
48 #include "insn-config.h"
49 #include "expmed.h"
50 #include "dojump.h"
51 #include "explow.h"
52 #include "calls.h"
53 #include "emit-rtl.h"
54 #include "varasm.h"
55 #include "stmt.h"
56 #include "expr.h"
57 #include "params.h"
58 #include "predict.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "basic-block.h"
62 #include "tree-ssa-alias.h"
63 #include "internal-fn.h"
64 #include "gimple-expr.h"
65 #include "is-a.h"
66 #include "gimple.h"
67 #include "gimple-iterator.h"
68 #include "gimple-ssa.h"
69 #include "tree-cfg.h"
70 #include "tree-ssanames.h"
71 #include "tree-into-ssa.h"
72 #include "tree-dfa.h"
73 #include "tree-ssa.h"
74 #include "tree-pass.h"
75 #include "diagnostic.h"
76 #include "except.h"
77 #include "debug.h"
78 #include "hash-map.h"
79 #include "plugin-api.h"
80 #include "ipa-ref.h"
81 #include "cgraph.h"
82 #include "ipa-utils.h"
83 #include "data-streamer.h"
84 #include "gimple-streamer.h"
85 #include "lto-streamer.h"
86 #include "tree-streamer.h"
87 #include "streamer-hooks.h"
88 #include "cfgloop.h"
91 struct freeing_string_slot_hasher : string_slot_hasher
93 static inline void remove (value_type *);
96 inline void
97 freeing_string_slot_hasher::remove (value_type *v)
99 free (v);
102 /* The table to hold the file names. */
103 static hash_table<freeing_string_slot_hasher> *file_name_hash_table;
106 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
107 number of valid tag values to check. */
109 void
110 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
112 va_list ap;
113 int i;
115 va_start (ap, ntags);
116 for (i = 0; i < ntags; i++)
117 if ((unsigned) actual == va_arg (ap, unsigned))
119 va_end (ap);
120 return;
123 va_end (ap);
124 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
128 /* Read LENGTH bytes from STREAM to ADDR. */
130 void
131 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
133 size_t i;
134 unsigned char *const buffer = (unsigned char *const) addr;
136 for (i = 0; i < length; i++)
137 buffer[i] = streamer_read_uchar (ib);
141 /* Lookup STRING in file_name_hash_table. If found, return the existing
142 string, otherwise insert STRING as the canonical version. */
144 static const char *
145 canon_file_name (const char *string)
147 string_slot **slot;
148 struct string_slot s_slot;
149 size_t len = strlen (string);
151 s_slot.s = string;
152 s_slot.len = len;
154 slot = file_name_hash_table->find_slot (&s_slot, INSERT);
155 if (*slot == NULL)
157 char *saved_string;
158 struct string_slot *new_slot;
160 saved_string = (char *) xmalloc (len + 1);
161 new_slot = XCNEW (struct string_slot);
162 memcpy (saved_string, string, len + 1);
163 new_slot->s = saved_string;
164 new_slot->len = len;
165 *slot = new_slot;
166 return saved_string;
168 else
170 struct string_slot *old_slot = *slot;
171 return old_slot->s;
176 /* Read a location bitpack from input block IB. */
178 location_t
179 lto_input_location (struct bitpack_d *bp, struct data_in *data_in)
181 static const char *current_file;
182 static int current_line;
183 static int current_col;
184 bool file_change, line_change, column_change;
185 bool prev_file = current_file != NULL;
187 if (bp_unpack_value (bp, 1))
188 return UNKNOWN_LOCATION;
190 file_change = bp_unpack_value (bp, 1);
191 line_change = bp_unpack_value (bp, 1);
192 column_change = bp_unpack_value (bp, 1);
194 if (file_change)
195 current_file = canon_file_name (bp_unpack_string (data_in, bp));
197 if (line_change)
198 current_line = bp_unpack_var_len_unsigned (bp);
200 if (column_change)
201 current_col = bp_unpack_var_len_unsigned (bp);
203 if (file_change)
205 if (prev_file)
206 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
208 linemap_add (line_table, LC_ENTER, false, current_file, current_line);
210 else if (line_change)
211 linemap_line_start (line_table, current_line, current_col);
213 return linemap_position_for_column (line_table, current_col);
217 /* Read a reference to a tree node from DATA_IN using input block IB.
218 TAG is the expected node that should be found in IB, if TAG belongs
219 to one of the indexable trees, expect to read a reference index to
220 be looked up in one of the symbol tables, otherwise read the pysical
221 representation of the tree using stream_read_tree. FN is the
222 function scope for the read tree. */
224 tree
225 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
226 struct function *fn, enum LTO_tags tag)
228 unsigned HOST_WIDE_INT ix_u;
229 tree result = NULL_TREE;
231 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_namelist_decl_ref);
233 switch (tag)
235 case LTO_type_ref:
236 ix_u = streamer_read_uhwi (ib);
237 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
238 break;
240 case LTO_ssa_name_ref:
241 ix_u = streamer_read_uhwi (ib);
242 result = (*SSANAMES (fn))[ix_u];
243 break;
245 case LTO_field_decl_ref:
246 ix_u = streamer_read_uhwi (ib);
247 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
248 break;
250 case LTO_function_decl_ref:
251 ix_u = streamer_read_uhwi (ib);
252 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
253 break;
255 case LTO_type_decl_ref:
256 ix_u = streamer_read_uhwi (ib);
257 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
258 break;
260 case LTO_namespace_decl_ref:
261 ix_u = streamer_read_uhwi (ib);
262 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
263 break;
265 case LTO_global_decl_ref:
266 case LTO_result_decl_ref:
267 case LTO_const_decl_ref:
268 case LTO_imported_decl_ref:
269 case LTO_label_decl_ref:
270 case LTO_translation_unit_decl_ref:
271 case LTO_namelist_decl_ref:
272 ix_u = streamer_read_uhwi (ib);
273 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
274 break;
276 default:
277 gcc_unreachable ();
280 gcc_assert (result);
282 return result;
286 /* Read and return a double-linked list of catch handlers from input
287 block IB, using descriptors in DATA_IN. */
289 static struct eh_catch_d *
290 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
291 eh_catch *last_p)
293 eh_catch first;
294 enum LTO_tags tag;
296 *last_p = first = NULL;
297 tag = streamer_read_record_start (ib);
298 while (tag)
300 tree list;
301 eh_catch n;
303 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
305 /* Read the catch node. */
306 n = ggc_cleared_alloc<eh_catch_d> ();
307 n->type_list = stream_read_tree (ib, data_in);
308 n->filter_list = stream_read_tree (ib, data_in);
309 n->label = stream_read_tree (ib, data_in);
311 /* Register all the types in N->FILTER_LIST. */
312 for (list = n->filter_list; list; list = TREE_CHAIN (list))
313 add_type_for_runtime (TREE_VALUE (list));
315 /* Chain N to the end of the list. */
316 if (*last_p)
317 (*last_p)->next_catch = n;
318 n->prev_catch = *last_p;
319 *last_p = n;
321 /* Set the head of the list the first time through the loop. */
322 if (first == NULL)
323 first = n;
325 tag = streamer_read_record_start (ib);
328 return first;
332 /* Read and return EH region IX from input block IB, using descriptors
333 in DATA_IN. */
335 static eh_region
336 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
338 enum LTO_tags tag;
339 eh_region r;
341 /* Read the region header. */
342 tag = streamer_read_record_start (ib);
343 if (tag == LTO_null)
344 return NULL;
346 r = ggc_cleared_alloc<eh_region_d> ();
347 r->index = streamer_read_hwi (ib);
349 gcc_assert (r->index == ix);
351 /* Read all the region pointers as region numbers. We'll fix up
352 the pointers once the whole array has been read. */
353 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
354 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
355 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
357 switch (tag)
359 case LTO_ert_cleanup:
360 r->type = ERT_CLEANUP;
361 break;
363 case LTO_ert_try:
365 struct eh_catch_d *last_catch;
366 r->type = ERT_TRY;
367 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
368 &last_catch);
369 r->u.eh_try.last_catch = last_catch;
370 break;
373 case LTO_ert_allowed_exceptions:
375 tree l;
377 r->type = ERT_ALLOWED_EXCEPTIONS;
378 r->u.allowed.type_list = stream_read_tree (ib, data_in);
379 r->u.allowed.label = stream_read_tree (ib, data_in);
380 r->u.allowed.filter = streamer_read_uhwi (ib);
382 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
383 add_type_for_runtime (TREE_VALUE (l));
385 break;
387 case LTO_ert_must_not_throw:
389 r->type = ERT_MUST_NOT_THROW;
390 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
391 bitpack_d bp = streamer_read_bitpack (ib);
392 r->u.must_not_throw.failure_loc
393 = stream_input_location (&bp, data_in);
395 break;
397 default:
398 gcc_unreachable ();
401 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
403 return r;
407 /* Read and return EH landing pad IX from input block IB, using descriptors
408 in DATA_IN. */
410 static eh_landing_pad
411 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
413 enum LTO_tags tag;
414 eh_landing_pad lp;
416 /* Read the landing pad header. */
417 tag = streamer_read_record_start (ib);
418 if (tag == LTO_null)
419 return NULL;
421 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
423 lp = ggc_cleared_alloc<eh_landing_pad_d> ();
424 lp->index = streamer_read_hwi (ib);
425 gcc_assert (lp->index == ix);
426 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
427 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
428 lp->post_landing_pad = stream_read_tree (ib, data_in);
430 return lp;
434 /* After reading the EH regions, pointers to peer and children regions
435 are region numbers. This converts all these region numbers into
436 real pointers into the rematerialized regions for FN. ROOT_REGION
437 is the region number for the root EH region in FN. */
439 static void
440 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
442 unsigned i;
443 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
444 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
445 eh_region r;
446 eh_landing_pad lp;
448 gcc_assert (eh_array && lp_array);
450 gcc_assert (root_region >= 0);
451 fn->eh->region_tree = (*eh_array)[root_region];
453 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
454 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
456 /* Convert all the index numbers stored in pointer fields into
457 pointers to the corresponding slots in the EH region array. */
458 FOR_EACH_VEC_ELT (*eh_array, i, r)
460 /* The array may contain NULL regions. */
461 if (r == NULL)
462 continue;
464 gcc_assert (i == (unsigned) r->index);
465 FIXUP_EH_REGION (r->outer);
466 FIXUP_EH_REGION (r->inner);
467 FIXUP_EH_REGION (r->next_peer);
468 FIXUP_EH_LP (r->landing_pads);
471 /* Convert all the index numbers stored in pointer fields into
472 pointers to the corresponding slots in the EH landing pad array. */
473 FOR_EACH_VEC_ELT (*lp_array, i, lp)
475 /* The array may contain NULL landing pads. */
476 if (lp == NULL)
477 continue;
479 gcc_assert (i == (unsigned) lp->index);
480 FIXUP_EH_LP (lp->next_lp);
481 FIXUP_EH_REGION (lp->region);
484 #undef FIXUP_EH_REGION
485 #undef FIXUP_EH_LP
489 /* Initialize EH support. */
491 void
492 lto_init_eh (void)
494 static bool eh_initialized_p = false;
496 if (eh_initialized_p)
497 return;
499 /* Contrary to most other FEs, we only initialize EH support when at
500 least one of the files in the set contains exception regions in
501 it. Since this happens much later than the call to init_eh in
502 lang_dependent_init, we have to set flag_exceptions and call
503 init_eh again to initialize the EH tables. */
504 flag_exceptions = 1;
505 init_eh ();
507 eh_initialized_p = true;
511 /* Read the exception table for FN from IB using the data descriptors
512 in DATA_IN. */
514 static void
515 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
516 struct function *fn)
518 HOST_WIDE_INT i, root_region, len;
519 enum LTO_tags tag;
521 tag = streamer_read_record_start (ib);
522 if (tag == LTO_null)
523 return;
525 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
527 /* If the file contains EH regions, then it was compiled with
528 -fexceptions. In that case, initialize the backend EH
529 machinery. */
530 lto_init_eh ();
532 gcc_assert (fn->eh);
534 root_region = streamer_read_hwi (ib);
535 gcc_assert (root_region == (int) root_region);
537 /* Read the EH region array. */
538 len = streamer_read_hwi (ib);
539 gcc_assert (len == (int) len);
540 if (len > 0)
542 vec_safe_grow_cleared (fn->eh->region_array, len);
543 for (i = 0; i < len; i++)
545 eh_region r = input_eh_region (ib, data_in, i);
546 (*fn->eh->region_array)[i] = r;
550 /* Read the landing pads. */
551 len = streamer_read_hwi (ib);
552 gcc_assert (len == (int) len);
553 if (len > 0)
555 vec_safe_grow_cleared (fn->eh->lp_array, len);
556 for (i = 0; i < len; i++)
558 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
559 (*fn->eh->lp_array)[i] = lp;
563 /* Read the runtime type data. */
564 len = streamer_read_hwi (ib);
565 gcc_assert (len == (int) len);
566 if (len > 0)
568 vec_safe_grow_cleared (fn->eh->ttype_data, len);
569 for (i = 0; i < len; i++)
571 tree ttype = stream_read_tree (ib, data_in);
572 (*fn->eh->ttype_data)[i] = ttype;
576 /* Read the table of action chains. */
577 len = streamer_read_hwi (ib);
578 gcc_assert (len == (int) len);
579 if (len > 0)
581 if (targetm.arm_eabi_unwinder)
583 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
584 for (i = 0; i < len; i++)
586 tree t = stream_read_tree (ib, data_in);
587 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
590 else
592 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
593 for (i = 0; i < len; i++)
595 uchar c = streamer_read_uchar (ib);
596 (*fn->eh->ehspec_data.other)[i] = c;
601 /* Reconstruct the EH region tree by fixing up the peer/children
602 pointers. */
603 fixup_eh_region_pointers (fn, root_region);
605 tag = streamer_read_record_start (ib);
606 lto_tag_check_range (tag, LTO_null, LTO_null);
610 /* Make a new basic block with index INDEX in function FN. */
612 static basic_block
613 make_new_block (struct function *fn, unsigned int index)
615 basic_block bb = alloc_block ();
616 bb->index = index;
617 SET_BASIC_BLOCK_FOR_FN (fn, index, bb);
618 n_basic_blocks_for_fn (fn)++;
619 return bb;
623 /* Read a wide-int. */
625 static widest_int
626 streamer_read_wi (struct lto_input_block *ib)
628 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
629 int i;
630 int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
631 int len = streamer_read_uhwi (ib);
632 for (i = 0; i < len; i++)
633 a[i] = streamer_read_hwi (ib);
634 return widest_int::from_array (a, len);
638 /* Read the CFG for function FN from input block IB. */
640 static void
641 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
642 struct function *fn,
643 int count_materialization_scale)
645 unsigned int bb_count;
646 basic_block p_bb;
647 unsigned int i;
648 int index;
650 init_empty_tree_cfg_for_function (fn);
651 init_ssa_operands (fn);
653 profile_status_for_fn (fn) = streamer_read_enum (ib, profile_status_d,
654 PROFILE_LAST);
656 bb_count = streamer_read_uhwi (ib);
658 last_basic_block_for_fn (fn) = bb_count;
659 if (bb_count > basic_block_info_for_fn (fn)->length ())
660 vec_safe_grow_cleared (basic_block_info_for_fn (fn), bb_count);
662 if (bb_count > label_to_block_map_for_fn (fn)->length ())
663 vec_safe_grow_cleared (label_to_block_map_for_fn (fn), bb_count);
665 index = streamer_read_hwi (ib);
666 while (index != -1)
668 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
669 unsigned int edge_count;
671 if (bb == NULL)
672 bb = make_new_block (fn, index);
674 edge_count = streamer_read_uhwi (ib);
676 /* Connect up the CFG. */
677 for (i = 0; i < edge_count; i++)
679 unsigned int dest_index;
680 unsigned int edge_flags;
681 basic_block dest;
682 int probability;
683 gcov_type count;
684 edge e;
686 dest_index = streamer_read_uhwi (ib);
687 probability = (int) streamer_read_hwi (ib);
688 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
689 count_materialization_scale);
690 edge_flags = streamer_read_uhwi (ib);
692 dest = BASIC_BLOCK_FOR_FN (fn, dest_index);
694 if (dest == NULL)
695 dest = make_new_block (fn, dest_index);
697 e = make_edge (bb, dest, edge_flags);
698 e->probability = probability;
699 e->count = count;
702 index = streamer_read_hwi (ib);
705 p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
706 index = streamer_read_hwi (ib);
707 while (index != -1)
709 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
710 bb->prev_bb = p_bb;
711 p_bb->next_bb = bb;
712 p_bb = bb;
713 index = streamer_read_hwi (ib);
716 /* ??? The cfgloop interface is tied to cfun. */
717 gcc_assert (cfun == fn);
719 /* Input the loop tree. */
720 unsigned n_loops = streamer_read_uhwi (ib);
721 if (n_loops == 0)
722 return;
724 struct loops *loops = ggc_cleared_alloc<struct loops> ();
725 init_loops_structure (fn, loops, n_loops);
726 set_loops_for_fn (fn, loops);
728 /* Input each loop and associate it with its loop header so
729 flow_loops_find can rebuild the loop tree. */
730 for (unsigned i = 1; i < n_loops; ++i)
732 int header_index = streamer_read_hwi (ib);
733 if (header_index == -1)
735 loops->larray->quick_push (NULL);
736 continue;
739 struct loop *loop = alloc_loop ();
740 loop->header = BASIC_BLOCK_FOR_FN (fn, header_index);
741 loop->header->loop_father = loop;
743 /* Read everything copy_loop_info copies. */
744 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
745 loop->any_upper_bound = streamer_read_hwi (ib);
746 if (loop->any_upper_bound)
747 loop->nb_iterations_upper_bound = streamer_read_wi (ib);
748 loop->any_estimate = streamer_read_hwi (ib);
749 if (loop->any_estimate)
750 loop->nb_iterations_estimate = streamer_read_wi (ib);
752 /* Read OMP SIMD related info. */
753 loop->safelen = streamer_read_hwi (ib);
754 loop->dont_vectorize = streamer_read_hwi (ib);
755 loop->force_vectorize = streamer_read_hwi (ib);
756 loop->simduid = stream_read_tree (ib, data_in);
758 place_new_loop (fn, loop);
760 /* flow_loops_find doesn't like loops not in the tree, hook them
761 all as siblings of the tree root temporarily. */
762 flow_loop_tree_node_add (loops->tree_root, loop);
765 /* Rebuild the loop tree. */
766 flow_loops_find (loops);
770 /* Read the SSA names array for function FN from DATA_IN using input
771 block IB. */
773 static void
774 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
775 struct function *fn)
777 unsigned int i, size;
779 size = streamer_read_uhwi (ib);
780 init_ssanames (fn, size);
782 i = streamer_read_uhwi (ib);
783 while (i)
785 tree ssa_name, name;
786 bool is_default_def;
788 /* Skip over the elements that had been freed. */
789 while (SSANAMES (fn)->length () < i)
790 SSANAMES (fn)->quick_push (NULL_TREE);
792 is_default_def = (streamer_read_uchar (ib) != 0);
793 name = stream_read_tree (ib, data_in);
794 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
796 if (is_default_def)
797 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
799 i = streamer_read_uhwi (ib);
804 /* Go through all NODE edges and fixup call_stmt pointers
805 so they point to STMTS. */
807 static void
808 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
809 struct function *fn)
811 struct cgraph_edge *cedge;
812 struct ipa_ref *ref = NULL;
813 unsigned int i;
815 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
817 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
818 fatal_error (input_location,
819 "Cgraph edge statement index out of range");
820 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
821 if (!cedge->call_stmt)
822 fatal_error (input_location,
823 "Cgraph edge statement index not found");
825 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
827 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
828 fatal_error (input_location,
829 "Cgraph edge statement index out of range");
830 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
831 if (!cedge->call_stmt)
832 fatal_error (input_location, "Cgraph edge statement index not found");
834 for (i = 0; node->iterate_reference (i, ref); i++)
835 if (ref->lto_stmt_uid)
837 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
838 fatal_error (input_location,
839 "Reference statement index out of range");
840 ref->stmt = stmts[ref->lto_stmt_uid - 1];
841 if (!ref->stmt)
842 fatal_error (input_location, "Reference statement index not found");
847 /* Fixup call_stmt pointers in NODE and all clones. */
849 static void
850 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
852 struct cgraph_node *node;
853 struct function *fn;
855 while (orig->clone_of)
856 orig = orig->clone_of;
857 fn = DECL_STRUCT_FUNCTION (orig->decl);
859 fixup_call_stmt_edges_1 (orig, stmts, fn);
860 if (orig->clones)
861 for (node = orig->clones; node != orig;)
863 fixup_call_stmt_edges_1 (node, stmts, fn);
864 if (node->clones)
865 node = node->clones;
866 else if (node->next_sibling_clone)
867 node = node->next_sibling_clone;
868 else
870 while (node != orig && !node->next_sibling_clone)
871 node = node->clone_of;
872 if (node != orig)
873 node = node->next_sibling_clone;
879 /* Input the base body of struct function FN from DATA_IN
880 using input block IB. */
882 static void
883 input_struct_function_base (struct function *fn, struct data_in *data_in,
884 struct lto_input_block *ib)
886 struct bitpack_d bp;
887 int len;
889 /* Read the static chain and non-local goto save area. */
890 fn->static_chain_decl = stream_read_tree (ib, data_in);
891 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
893 /* Read all the local symbols. */
894 len = streamer_read_hwi (ib);
895 if (len > 0)
897 int i;
898 vec_safe_grow_cleared (fn->local_decls, len);
899 for (i = 0; i < len; i++)
901 tree t = stream_read_tree (ib, data_in);
902 (*fn->local_decls)[i] = t;
906 /* Input the current IL state of the function. */
907 fn->curr_properties = streamer_read_uhwi (ib);
909 /* Read all the attributes for FN. */
910 bp = streamer_read_bitpack (ib);
911 fn->is_thunk = bp_unpack_value (&bp, 1);
912 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
913 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
914 fn->returns_struct = bp_unpack_value (&bp, 1);
915 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
916 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
917 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
918 fn->after_inlining = bp_unpack_value (&bp, 1);
919 fn->stdarg = bp_unpack_value (&bp, 1);
920 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
921 fn->calls_alloca = bp_unpack_value (&bp, 1);
922 fn->calls_setjmp = bp_unpack_value (&bp, 1);
923 fn->has_force_vectorize_loops = bp_unpack_value (&bp, 1);
924 fn->has_simduid_loops = bp_unpack_value (&bp, 1);
925 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
926 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
927 fn->last_clique = bp_unpack_value (&bp, sizeof (short) * 8);
929 /* Input the function start and end loci. */
930 fn->function_start_locus = stream_input_location (&bp, data_in);
931 fn->function_end_locus = stream_input_location (&bp, data_in);
935 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
937 static void
938 input_function (tree fn_decl, struct data_in *data_in,
939 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
941 struct function *fn;
942 enum LTO_tags tag;
943 gimple *stmts;
944 basic_block bb;
945 struct cgraph_node *node;
947 tag = streamer_read_record_start (ib);
948 lto_tag_check (tag, LTO_function);
950 /* Read decls for parameters and args. */
951 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
952 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
954 /* Read the tree of lexical scopes for the function. */
955 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
957 if (!streamer_read_uhwi (ib))
958 return;
960 push_struct_function (fn_decl);
961 fn = DECL_STRUCT_FUNCTION (fn_decl);
962 init_tree_ssa (fn);
963 /* We input IL in SSA form. */
964 cfun->gimple_df->in_ssa_p = true;
966 gimple_register_cfg_hooks ();
968 node = cgraph_node::get (fn_decl);
969 if (!node)
970 node = cgraph_node::create (fn_decl);
971 input_struct_function_base (fn, data_in, ib);
972 input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
974 /* Read all the SSA names. */
975 input_ssa_names (ib, data_in, fn);
977 /* Read the exception handling regions in the function. */
978 input_eh_regions (ib, data_in, fn);
980 gcc_assert (DECL_INITIAL (fn_decl));
981 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
983 /* Read all the basic blocks. */
984 tag = streamer_read_record_start (ib);
985 while (tag)
987 input_bb (ib, tag, data_in, fn,
988 node->count_materialization_scale);
989 tag = streamer_read_record_start (ib);
992 /* Fix up the call statements that are mentioned in the callgraph
993 edges. */
994 set_gimple_stmt_max_uid (cfun, 0);
995 FOR_ALL_BB_FN (bb, cfun)
997 gimple_stmt_iterator gsi;
998 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1000 gimple stmt = gsi_stmt (gsi);
1001 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1003 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1005 gimple stmt = gsi_stmt (gsi);
1006 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1009 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1010 FOR_ALL_BB_FN (bb, cfun)
1012 gimple_stmt_iterator bsi = gsi_start_phis (bb);
1013 while (!gsi_end_p (bsi))
1015 gimple stmt = gsi_stmt (bsi);
1016 gsi_next (&bsi);
1017 stmts[gimple_uid (stmt)] = stmt;
1019 bsi = gsi_start_bb (bb);
1020 while (!gsi_end_p (bsi))
1022 gimple stmt = gsi_stmt (bsi);
1023 /* If we're recompiling LTO objects with debug stmts but
1024 we're not supposed to have debug stmts, remove them now.
1025 We can't remove them earlier because this would cause uid
1026 mismatches in fixups, but we can do it at this point, as
1027 long as debug stmts don't require fixups. */
1028 if (!MAY_HAVE_DEBUG_STMTS && !flag_wpa && is_gimple_debug (stmt))
1030 gimple_stmt_iterator gsi = bsi;
1031 gsi_next (&bsi);
1032 gsi_remove (&gsi, true);
1034 else
1036 gsi_next (&bsi);
1037 stmts[gimple_uid (stmt)] = stmt;
1042 /* Set the gimple body to the statement sequence in the entry
1043 basic block. FIXME lto, this is fairly hacky. The existence
1044 of a gimple body is used by the cgraph routines, but we should
1045 really use the presence of the CFG. */
1047 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1048 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1051 fixup_call_stmt_edges (node, stmts);
1052 execute_all_ipa_stmt_fixups (node, stmts);
1054 update_ssa (TODO_update_ssa_only_virtuals);
1055 free_dominance_info (CDI_DOMINATORS);
1056 free_dominance_info (CDI_POST_DOMINATORS);
1057 free (stmts);
1058 pop_cfun ();
1061 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1063 static void
1064 input_constructor (tree var, struct data_in *data_in,
1065 struct lto_input_block *ib)
1067 DECL_INITIAL (var) = stream_read_tree (ib, data_in);
1071 /* Read the body from DATA for function NODE and fill it in.
1072 FILE_DATA are the global decls and types. SECTION_TYPE is either
1073 LTO_section_function_body or LTO_section_static_initializer. If
1074 section type is LTO_section_function_body, FN must be the decl for
1075 that function. */
1077 static void
1078 lto_read_body_or_constructor (struct lto_file_decl_data *file_data, struct symtab_node *node,
1079 const char *data, enum lto_section_type section_type)
1081 const struct lto_function_header *header;
1082 struct data_in *data_in;
1083 int cfg_offset;
1084 int main_offset;
1085 int string_offset;
1086 tree fn_decl = node->decl;
1088 header = (const struct lto_function_header *) data;
1089 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1091 cfg_offset = sizeof (struct lto_function_header);
1092 main_offset = cfg_offset + header->cfg_size;
1093 string_offset = main_offset + header->main_size;
1095 else
1097 main_offset = sizeof (struct lto_function_header);
1098 string_offset = main_offset + header->main_size;
1101 data_in = lto_data_in_create (file_data, data + string_offset,
1102 header->string_size, vNULL);
1104 if (section_type == LTO_section_function_body)
1106 struct lto_in_decl_state *decl_state;
1107 unsigned from;
1109 gcc_checking_assert (node);
1111 /* Use the function's decl state. */
1112 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1113 gcc_assert (decl_state);
1114 file_data->current_decl_state = decl_state;
1117 /* Set up the struct function. */
1118 from = data_in->reader_cache->nodes.length ();
1119 lto_input_block ib_main (data + main_offset, header->main_size);
1120 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1122 lto_input_block ib_cfg (data + cfg_offset, header->cfg_size);
1123 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1125 else
1126 input_constructor (fn_decl, data_in, &ib_main);
1127 /* And fixup types we streamed locally. */
1129 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1130 unsigned len = cache->nodes.length ();
1131 unsigned i;
1132 for (i = len; i-- > from;)
1134 tree t = streamer_tree_cache_get_tree (cache, i);
1135 if (t == NULL_TREE)
1136 continue;
1138 if (TYPE_P (t))
1140 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1141 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1142 if (TYPE_MAIN_VARIANT (t) != t)
1144 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1145 TYPE_NEXT_VARIANT (t)
1146 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1147 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1153 /* Restore decl state */
1154 file_data->current_decl_state = file_data->global_decl_state;
1157 lto_data_in_delete (data_in);
1161 /* Read the body of NODE using DATA. FILE_DATA holds the global
1162 decls and types. */
1164 void
1165 lto_input_function_body (struct lto_file_decl_data *file_data,
1166 struct cgraph_node *node, const char *data)
1168 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1171 /* Read the body of NODE using DATA. FILE_DATA holds the global
1172 decls and types. */
1174 void
1175 lto_input_variable_constructor (struct lto_file_decl_data *file_data,
1176 struct varpool_node *node, const char *data)
1178 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1182 /* Read the physical representation of a tree node EXPR from
1183 input block IB using the per-file context in DATA_IN. */
1185 static void
1186 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1188 /* Read all the bitfield values in EXPR. Note that for LTO, we
1189 only write language-independent bitfields, so no more unpacking is
1190 needed. */
1191 streamer_read_tree_bitfields (ib, data_in, expr);
1193 /* Read all the pointer fields in EXPR. */
1194 streamer_read_tree_body (ib, data_in, expr);
1196 /* Read any LTO-specific data not read by the tree streamer. */
1197 if (DECL_P (expr)
1198 && TREE_CODE (expr) != FUNCTION_DECL
1199 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1200 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1202 /* We should never try to instantiate an MD or NORMAL builtin here. */
1203 if (TREE_CODE (expr) == FUNCTION_DECL)
1204 gcc_assert (!streamer_handle_as_builtin_p (expr));
1206 #ifdef LTO_STREAMER_DEBUG
1207 /* Remove the mapping to RESULT's original address set by
1208 streamer_alloc_tree. */
1209 lto_orig_address_remove (expr);
1210 #endif
1213 /* Read the physical representation of a tree node with tag TAG from
1214 input block IB using the per-file context in DATA_IN. */
1216 static tree
1217 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1218 enum LTO_tags tag, hashval_t hash)
1220 /* Instantiate a new tree node. */
1221 tree result = streamer_alloc_tree (ib, data_in, tag);
1223 /* Enter RESULT in the reader cache. This will make RESULT
1224 available so that circular references in the rest of the tree
1225 structure can be resolved in subsequent calls to stream_read_tree. */
1226 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1228 lto_read_tree_1 (ib, data_in, result);
1230 /* end_marker = */ streamer_read_uchar (ib);
1232 return result;
1236 /* Populate the reader cache with trees materialized from the SCC
1237 following in the IB, DATA_IN stream. */
1239 hashval_t
1240 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1241 unsigned *len, unsigned *entry_len)
1243 /* A blob of unnamed tree nodes, fill the cache from it and
1244 recurse. */
1245 unsigned size = streamer_read_uhwi (ib);
1246 hashval_t scc_hash = streamer_read_uhwi (ib);
1247 unsigned scc_entry_len = 1;
1249 if (size == 1)
1251 enum LTO_tags tag = streamer_read_record_start (ib);
1252 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1254 else
1256 unsigned int first = data_in->reader_cache->nodes.length ();
1257 tree result;
1259 scc_entry_len = streamer_read_uhwi (ib);
1261 /* Materialize size trees by reading their headers. */
1262 for (unsigned i = 0; i < size; ++i)
1264 enum LTO_tags tag = streamer_read_record_start (ib);
1265 if (tag == LTO_null
1266 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1267 || tag == LTO_tree_pickle_reference
1268 || tag == LTO_builtin_decl
1269 || tag == LTO_integer_cst
1270 || tag == LTO_tree_scc)
1271 gcc_unreachable ();
1273 result = streamer_alloc_tree (ib, data_in, tag);
1274 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1277 /* Read the tree bitpacks and references. */
1278 for (unsigned i = 0; i < size; ++i)
1280 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1281 first + i);
1282 lto_read_tree_1 (ib, data_in, result);
1283 /* end_marker = */ streamer_read_uchar (ib);
1287 *len = size;
1288 *entry_len = scc_entry_len;
1289 return scc_hash;
1293 /* Read a tree from input block IB using the per-file context in
1294 DATA_IN. This context is used, for example, to resolve references
1295 to previously read nodes. */
1297 tree
1298 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1299 enum LTO_tags tag, hashval_t hash)
1301 tree result;
1303 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1305 if (tag == LTO_null)
1306 result = NULL_TREE;
1307 else if (tag >= LTO_field_decl_ref && tag <= LTO_namelist_decl_ref)
1309 /* If TAG is a reference to an indexable tree, the next value
1310 in IB is the index into the table where we expect to find
1311 that tree. */
1312 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1314 else if (tag == LTO_tree_pickle_reference)
1316 /* If TAG is a reference to a previously read tree, look it up in
1317 the reader cache. */
1318 result = streamer_get_pickled_tree (ib, data_in);
1320 else if (tag == LTO_builtin_decl)
1322 /* If we are going to read a built-in function, all we need is
1323 the code and class. */
1324 result = streamer_get_builtin_tree (ib, data_in);
1326 else if (tag == LTO_integer_cst)
1328 /* For shared integer constants in singletons we can use the
1329 existing tree integer constant merging code. */
1330 tree type = stream_read_tree (ib, data_in);
1331 unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
1332 unsigned HOST_WIDE_INT i;
1333 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
1335 for (i = 0; i < len; i++)
1336 a[i] = streamer_read_hwi (ib);
1337 gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
1338 result = wide_int_to_tree (type, wide_int::from_array
1339 (a, len, TYPE_PRECISION (type)));
1340 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1342 else if (tag == LTO_tree_scc)
1343 gcc_unreachable ();
1344 else
1346 /* Otherwise, materialize a new node from IB. */
1347 result = lto_read_tree (ib, data_in, tag, hash);
1350 return result;
1353 tree
1354 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1356 enum LTO_tags tag;
1358 /* Input and skip SCCs. */
1359 while ((tag = streamer_read_record_start (ib)) == LTO_tree_scc)
1361 unsigned len, entry_len;
1362 lto_input_scc (ib, data_in, &len, &entry_len);
1364 return lto_input_tree_1 (ib, data_in, tag, 0);
1368 /* Input toplevel asms. */
1370 void
1371 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1373 size_t len;
1374 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1375 NULL, &len);
1376 const struct lto_simple_header_with_strings *header
1377 = (const struct lto_simple_header_with_strings *) data;
1378 int string_offset;
1379 struct data_in *data_in;
1380 tree str;
1382 if (! data)
1383 return;
1385 string_offset = sizeof (*header) + header->main_size;
1387 lto_input_block ib (data + sizeof (*header), header->main_size);
1389 data_in = lto_data_in_create (file_data, data + string_offset,
1390 header->string_size, vNULL);
1392 while ((str = streamer_read_string_cst (data_in, &ib)))
1394 asm_node *node = symtab->finalize_toplevel_asm (str);
1395 node->order = streamer_read_hwi (&ib) + order_base;
1396 if (node->order >= symtab->order)
1397 symtab->order = node->order + 1;
1400 lto_data_in_delete (data_in);
1402 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1406 /* Initialization for the LTO reader. */
1408 void
1409 lto_reader_init (void)
1411 lto_streamer_init ();
1412 file_name_hash_table
1413 = new hash_table<freeing_string_slot_hasher> (37);
1417 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1418 table to use with LEN strings. RESOLUTIONS is the vector of linker
1419 resolutions (NULL if not using a linker plugin). */
1421 struct data_in *
1422 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1423 unsigned len,
1424 vec<ld_plugin_symbol_resolution_t> resolutions)
1426 struct data_in *data_in = XCNEW (struct data_in);
1427 data_in->file_data = file_data;
1428 data_in->strings = strings;
1429 data_in->strings_len = len;
1430 data_in->globals_resolution = resolutions;
1431 data_in->reader_cache = streamer_tree_cache_create (false, false, true);
1432 return data_in;
1436 /* Remove DATA_IN. */
1438 void
1439 lto_data_in_delete (struct data_in *data_in)
1441 data_in->globals_resolution.release ();
1442 streamer_tree_cache_delete (data_in->reader_cache);
1443 free (data_in);