2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / lto-streamer-in.c
blob895c18c8665326e0c7020a8f390365f321e029a5
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 "alias.h"
29 #include "symtab.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "stringpool.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "rtl.h"
36 #include "flags.h"
37 #include "insn-config.h"
38 #include "expmed.h"
39 #include "dojump.h"
40 #include "explow.h"
41 #include "calls.h"
42 #include "emit-rtl.h"
43 #include "varasm.h"
44 #include "stmt.h"
45 #include "expr.h"
46 #include "params.h"
47 #include "predict.h"
48 #include "dominance.h"
49 #include "cfg.h"
50 #include "basic-block.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "gimple-expr.h"
54 #include "gimple.h"
55 #include "gimple-iterator.h"
56 #include "gimple-ssa.h"
57 #include "tree-cfg.h"
58 #include "tree-ssanames.h"
59 #include "tree-into-ssa.h"
60 #include "tree-dfa.h"
61 #include "tree-ssa.h"
62 #include "tree-pass.h"
63 #include "diagnostic.h"
64 #include "except.h"
65 #include "debug.h"
66 #include "plugin-api.h"
67 #include "ipa-ref.h"
68 #include "cgraph.h"
69 #include "ipa-utils.h"
70 #include "data-streamer.h"
71 #include "gimple-streamer.h"
72 #include "lto-streamer.h"
73 #include "tree-streamer.h"
74 #include "streamer-hooks.h"
75 #include "cfgloop.h"
78 struct freeing_string_slot_hasher : string_slot_hasher
80 static inline void remove (value_type *);
83 inline void
84 freeing_string_slot_hasher::remove (value_type *v)
86 free (v);
89 /* The table to hold the file names. */
90 static hash_table<freeing_string_slot_hasher> *file_name_hash_table;
93 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
94 number of valid tag values to check. */
96 void
97 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
99 va_list ap;
100 int i;
102 va_start (ap, ntags);
103 for (i = 0; i < ntags; i++)
104 if ((unsigned) actual == va_arg (ap, unsigned))
106 va_end (ap);
107 return;
110 va_end (ap);
111 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
115 /* Read LENGTH bytes from STREAM to ADDR. */
117 void
118 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
120 size_t i;
121 unsigned char *const buffer = (unsigned char *const) addr;
123 for (i = 0; i < length; i++)
124 buffer[i] = streamer_read_uchar (ib);
128 /* Lookup STRING in file_name_hash_table. If found, return the existing
129 string, otherwise insert STRING as the canonical version. */
131 static const char *
132 canon_file_name (const char *string)
134 string_slot **slot;
135 struct string_slot s_slot;
136 size_t len = strlen (string);
138 s_slot.s = string;
139 s_slot.len = len;
141 slot = file_name_hash_table->find_slot (&s_slot, INSERT);
142 if (*slot == NULL)
144 char *saved_string;
145 struct string_slot *new_slot;
147 saved_string = (char *) xmalloc (len + 1);
148 new_slot = XCNEW (struct string_slot);
149 memcpy (saved_string, string, len + 1);
150 new_slot->s = saved_string;
151 new_slot->len = len;
152 *slot = new_slot;
153 return saved_string;
155 else
157 struct string_slot *old_slot = *slot;
158 return old_slot->s;
162 /* Pointer to currently alive instance of lto_location_cache. */
164 lto_location_cache *lto_location_cache::current_cache;
166 /* Sort locations in source order. Start with file from last application. */
169 lto_location_cache::cmp_loc (const void *pa, const void *pb)
171 const cached_location *a = ((const cached_location *)pa);
172 const cached_location *b = ((const cached_location *)pb);
173 const char *current_file = current_cache->current_file;
174 int current_line = current_cache->current_line;
176 if (a->file == current_file && b->file != current_file)
177 return -1;
178 if (a->file != current_file && b->file == current_file)
179 return 1;
180 if (a->file == current_file && b->file == current_file)
182 if (a->line == current_line && b->line != current_line)
183 return -1;
184 if (a->line != current_line && b->line == current_line)
185 return 1;
187 if (a->file != b->file)
188 return strcmp (a->file, b->file);
189 if (a->line != b->line)
190 return a->line - b->line;
191 return a->col - b->col;
194 /* Apply all changes in location cache. Add locations into linemap and patch
195 trees. */
197 bool
198 lto_location_cache::apply_location_cache ()
200 static const char *prev_file;
201 if (!loc_cache.length ())
202 return false;
203 if (loc_cache.length () > 1)
204 loc_cache.qsort (cmp_loc);
206 for (unsigned int i = 0; i < loc_cache.length (); i++)
208 struct cached_location loc = loc_cache[i];
210 if (current_file != loc.file)
211 linemap_add (line_table, prev_file ? LC_RENAME : LC_ENTER,
212 false, loc.file, loc.line);
213 else if (current_line != loc.line)
215 int max = loc.col;
217 for (unsigned int j = i + 1; j < loc_cache.length (); j++)
218 if (loc.file != loc_cache[j].file
219 || loc.line != loc_cache[j].line)
220 break;
221 else if (max < loc_cache[j].col)
222 max = loc_cache[j].col;
223 linemap_line_start (line_table, loc.line, max + 1);
225 gcc_assert (*loc.loc == BUILTINS_LOCATION + 1);
226 if (current_file == loc.file && current_line == loc.line
227 && current_col == loc.col)
228 *loc.loc = current_loc;
229 else
230 current_loc = *loc.loc = linemap_position_for_column (line_table,
231 loc.col);
232 current_line = loc.line;
233 prev_file = current_file = loc.file;
234 current_col = loc.col;
236 loc_cache.truncate (0);
237 accepted_length = 0;
238 return true;
241 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
243 void
244 lto_location_cache::accept_location_cache ()
246 gcc_assert (current_cache == this);
247 accepted_length = loc_cache.length ();
250 /* Tree merging did suceed; throw away recent changes. */
252 void
253 lto_location_cache::revert_location_cache ()
255 loc_cache.truncate (accepted_length);
258 /* Read a location bitpack from input block IB and either update *LOC directly
259 or add it to the location cache.
260 It is neccesary to call apply_location_cache to get *LOC updated. */
262 void
263 lto_location_cache::input_location (location_t *loc, struct bitpack_d *bp,
264 struct data_in *data_in)
266 static const char *stream_file;
267 static int stream_line;
268 static int stream_col;
269 bool file_change, line_change, column_change;
271 gcc_assert (current_cache == this);
273 *loc = bp_unpack_int_in_range (bp, "location", 0, RESERVED_LOCATION_COUNT);
275 if (*loc < RESERVED_LOCATION_COUNT)
276 return;
278 /* Keep value RESERVED_LOCATION_COUNT in *loc as linemap lookups will
279 ICE on it. */
281 file_change = bp_unpack_value (bp, 1);
282 line_change = bp_unpack_value (bp, 1);
283 column_change = bp_unpack_value (bp, 1);
285 if (file_change)
286 stream_file = canon_file_name (bp_unpack_string (data_in, bp));
288 if (line_change)
289 stream_line = bp_unpack_var_len_unsigned (bp);
291 if (column_change)
292 stream_col = bp_unpack_var_len_unsigned (bp);
294 /* This optimization saves location cache operations druing gimple
295 streaming. */
297 if (current_file == stream_file && current_line == stream_line
298 && current_col == stream_col)
300 *loc = current_loc;
301 return;
304 struct cached_location entry = {stream_file, loc, stream_line, stream_col};
305 loc_cache.safe_push (entry);
308 /* Read a location bitpack from input block IB and either update *LOC directly
309 or add it to the location cache.
310 It is neccesary to call apply_location_cache to get *LOC updated. */
312 void
313 lto_input_location (location_t *loc, struct bitpack_d *bp,
314 struct data_in *data_in)
316 data_in->location_cache.input_location (loc, bp, data_in);
319 /* Read location and return it instead of going through location caching.
320 This should be used only when the resulting location is not going to be
321 discarded. */
323 location_t
324 stream_input_location_now (struct bitpack_d *bp, struct data_in *data_in)
326 location_t loc;
327 stream_input_location (&loc, bp, data_in);
328 data_in->location_cache.apply_location_cache ();
329 return loc;
332 /* Read a reference to a tree node from DATA_IN using input block IB.
333 TAG is the expected node that should be found in IB, if TAG belongs
334 to one of the indexable trees, expect to read a reference index to
335 be looked up in one of the symbol tables, otherwise read the pysical
336 representation of the tree using stream_read_tree. FN is the
337 function scope for the read tree. */
339 tree
340 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
341 struct function *fn, enum LTO_tags tag)
343 unsigned HOST_WIDE_INT ix_u;
344 tree result = NULL_TREE;
346 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_namelist_decl_ref);
348 switch (tag)
350 case LTO_type_ref:
351 ix_u = streamer_read_uhwi (ib);
352 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
353 break;
355 case LTO_ssa_name_ref:
356 ix_u = streamer_read_uhwi (ib);
357 result = (*SSANAMES (fn))[ix_u];
358 break;
360 case LTO_field_decl_ref:
361 ix_u = streamer_read_uhwi (ib);
362 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
363 break;
365 case LTO_function_decl_ref:
366 ix_u = streamer_read_uhwi (ib);
367 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
368 break;
370 case LTO_type_decl_ref:
371 ix_u = streamer_read_uhwi (ib);
372 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
373 break;
375 case LTO_namespace_decl_ref:
376 ix_u = streamer_read_uhwi (ib);
377 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
378 break;
380 case LTO_global_decl_ref:
381 case LTO_result_decl_ref:
382 case LTO_const_decl_ref:
383 case LTO_imported_decl_ref:
384 case LTO_label_decl_ref:
385 case LTO_translation_unit_decl_ref:
386 case LTO_namelist_decl_ref:
387 ix_u = streamer_read_uhwi (ib);
388 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
389 break;
391 default:
392 gcc_unreachable ();
395 gcc_assert (result);
397 return result;
401 /* Read and return a double-linked list of catch handlers from input
402 block IB, using descriptors in DATA_IN. */
404 static struct eh_catch_d *
405 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
406 eh_catch *last_p)
408 eh_catch first;
409 enum LTO_tags tag;
411 *last_p = first = NULL;
412 tag = streamer_read_record_start (ib);
413 while (tag)
415 tree list;
416 eh_catch n;
418 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
420 /* Read the catch node. */
421 n = ggc_cleared_alloc<eh_catch_d> ();
422 n->type_list = stream_read_tree (ib, data_in);
423 n->filter_list = stream_read_tree (ib, data_in);
424 n->label = stream_read_tree (ib, data_in);
426 /* Register all the types in N->FILTER_LIST. */
427 for (list = n->filter_list; list; list = TREE_CHAIN (list))
428 add_type_for_runtime (TREE_VALUE (list));
430 /* Chain N to the end of the list. */
431 if (*last_p)
432 (*last_p)->next_catch = n;
433 n->prev_catch = *last_p;
434 *last_p = n;
436 /* Set the head of the list the first time through the loop. */
437 if (first == NULL)
438 first = n;
440 tag = streamer_read_record_start (ib);
443 return first;
447 /* Read and return EH region IX from input block IB, using descriptors
448 in DATA_IN. */
450 static eh_region
451 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
453 enum LTO_tags tag;
454 eh_region r;
456 /* Read the region header. */
457 tag = streamer_read_record_start (ib);
458 if (tag == LTO_null)
459 return NULL;
461 r = ggc_cleared_alloc<eh_region_d> ();
462 r->index = streamer_read_hwi (ib);
464 gcc_assert (r->index == ix);
466 /* Read all the region pointers as region numbers. We'll fix up
467 the pointers once the whole array has been read. */
468 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
469 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
470 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
472 switch (tag)
474 case LTO_ert_cleanup:
475 r->type = ERT_CLEANUP;
476 break;
478 case LTO_ert_try:
480 struct eh_catch_d *last_catch;
481 r->type = ERT_TRY;
482 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
483 &last_catch);
484 r->u.eh_try.last_catch = last_catch;
485 break;
488 case LTO_ert_allowed_exceptions:
490 tree l;
492 r->type = ERT_ALLOWED_EXCEPTIONS;
493 r->u.allowed.type_list = stream_read_tree (ib, data_in);
494 r->u.allowed.label = stream_read_tree (ib, data_in);
495 r->u.allowed.filter = streamer_read_uhwi (ib);
497 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
498 add_type_for_runtime (TREE_VALUE (l));
500 break;
502 case LTO_ert_must_not_throw:
504 r->type = ERT_MUST_NOT_THROW;
505 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
506 bitpack_d bp = streamer_read_bitpack (ib);
507 r->u.must_not_throw.failure_loc
508 = stream_input_location_now (&bp, data_in);
510 break;
512 default:
513 gcc_unreachable ();
516 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
518 return r;
522 /* Read and return EH landing pad IX from input block IB, using descriptors
523 in DATA_IN. */
525 static eh_landing_pad
526 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
528 enum LTO_tags tag;
529 eh_landing_pad lp;
531 /* Read the landing pad header. */
532 tag = streamer_read_record_start (ib);
533 if (tag == LTO_null)
534 return NULL;
536 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
538 lp = ggc_cleared_alloc<eh_landing_pad_d> ();
539 lp->index = streamer_read_hwi (ib);
540 gcc_assert (lp->index == ix);
541 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
542 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
543 lp->post_landing_pad = stream_read_tree (ib, data_in);
545 return lp;
549 /* After reading the EH regions, pointers to peer and children regions
550 are region numbers. This converts all these region numbers into
551 real pointers into the rematerialized regions for FN. ROOT_REGION
552 is the region number for the root EH region in FN. */
554 static void
555 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
557 unsigned i;
558 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
559 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
560 eh_region r;
561 eh_landing_pad lp;
563 gcc_assert (eh_array && lp_array);
565 gcc_assert (root_region >= 0);
566 fn->eh->region_tree = (*eh_array)[root_region];
568 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
569 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
571 /* Convert all the index numbers stored in pointer fields into
572 pointers to the corresponding slots in the EH region array. */
573 FOR_EACH_VEC_ELT (*eh_array, i, r)
575 /* The array may contain NULL regions. */
576 if (r == NULL)
577 continue;
579 gcc_assert (i == (unsigned) r->index);
580 FIXUP_EH_REGION (r->outer);
581 FIXUP_EH_REGION (r->inner);
582 FIXUP_EH_REGION (r->next_peer);
583 FIXUP_EH_LP (r->landing_pads);
586 /* Convert all the index numbers stored in pointer fields into
587 pointers to the corresponding slots in the EH landing pad array. */
588 FOR_EACH_VEC_ELT (*lp_array, i, lp)
590 /* The array may contain NULL landing pads. */
591 if (lp == NULL)
592 continue;
594 gcc_assert (i == (unsigned) lp->index);
595 FIXUP_EH_LP (lp->next_lp);
596 FIXUP_EH_REGION (lp->region);
599 #undef FIXUP_EH_REGION
600 #undef FIXUP_EH_LP
604 /* Initialize EH support. */
606 void
607 lto_init_eh (void)
609 static bool eh_initialized_p = false;
611 if (eh_initialized_p)
612 return;
614 /* Contrary to most other FEs, we only initialize EH support when at
615 least one of the files in the set contains exception regions in
616 it. Since this happens much later than the call to init_eh in
617 lang_dependent_init, we have to set flag_exceptions and call
618 init_eh again to initialize the EH tables. */
619 flag_exceptions = 1;
620 init_eh ();
622 eh_initialized_p = true;
626 /* Read the exception table for FN from IB using the data descriptors
627 in DATA_IN. */
629 static void
630 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
631 struct function *fn)
633 HOST_WIDE_INT i, root_region, len;
634 enum LTO_tags tag;
636 tag = streamer_read_record_start (ib);
637 if (tag == LTO_null)
638 return;
640 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
642 /* If the file contains EH regions, then it was compiled with
643 -fexceptions. In that case, initialize the backend EH
644 machinery. */
645 lto_init_eh ();
647 gcc_assert (fn->eh);
649 root_region = streamer_read_hwi (ib);
650 gcc_assert (root_region == (int) root_region);
652 /* Read the EH region array. */
653 len = streamer_read_hwi (ib);
654 gcc_assert (len == (int) len);
655 if (len > 0)
657 vec_safe_grow_cleared (fn->eh->region_array, len);
658 for (i = 0; i < len; i++)
660 eh_region r = input_eh_region (ib, data_in, i);
661 (*fn->eh->region_array)[i] = r;
665 /* Read the landing pads. */
666 len = streamer_read_hwi (ib);
667 gcc_assert (len == (int) len);
668 if (len > 0)
670 vec_safe_grow_cleared (fn->eh->lp_array, len);
671 for (i = 0; i < len; i++)
673 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
674 (*fn->eh->lp_array)[i] = lp;
678 /* Read the runtime type data. */
679 len = streamer_read_hwi (ib);
680 gcc_assert (len == (int) len);
681 if (len > 0)
683 vec_safe_grow_cleared (fn->eh->ttype_data, len);
684 for (i = 0; i < len; i++)
686 tree ttype = stream_read_tree (ib, data_in);
687 (*fn->eh->ttype_data)[i] = ttype;
691 /* Read the table of action chains. */
692 len = streamer_read_hwi (ib);
693 gcc_assert (len == (int) len);
694 if (len > 0)
696 if (targetm.arm_eabi_unwinder)
698 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
699 for (i = 0; i < len; i++)
701 tree t = stream_read_tree (ib, data_in);
702 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
705 else
707 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
708 for (i = 0; i < len; i++)
710 uchar c = streamer_read_uchar (ib);
711 (*fn->eh->ehspec_data.other)[i] = c;
716 /* Reconstruct the EH region tree by fixing up the peer/children
717 pointers. */
718 fixup_eh_region_pointers (fn, root_region);
720 tag = streamer_read_record_start (ib);
721 lto_tag_check_range (tag, LTO_null, LTO_null);
725 /* Make a new basic block with index INDEX in function FN. */
727 static basic_block
728 make_new_block (struct function *fn, unsigned int index)
730 basic_block bb = alloc_block ();
731 bb->index = index;
732 SET_BASIC_BLOCK_FOR_FN (fn, index, bb);
733 n_basic_blocks_for_fn (fn)++;
734 return bb;
738 /* Read a wide-int. */
740 static widest_int
741 streamer_read_wi (struct lto_input_block *ib)
743 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
744 int i;
745 int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
746 int len = streamer_read_uhwi (ib);
747 for (i = 0; i < len; i++)
748 a[i] = streamer_read_hwi (ib);
749 return widest_int::from_array (a, len);
753 /* Read the CFG for function FN from input block IB. */
755 static void
756 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
757 struct function *fn,
758 int count_materialization_scale)
760 unsigned int bb_count;
761 basic_block p_bb;
762 unsigned int i;
763 int index;
765 init_empty_tree_cfg_for_function (fn);
766 init_ssa_operands (fn);
768 profile_status_for_fn (fn) = streamer_read_enum (ib, profile_status_d,
769 PROFILE_LAST);
771 bb_count = streamer_read_uhwi (ib);
773 last_basic_block_for_fn (fn) = bb_count;
774 if (bb_count > basic_block_info_for_fn (fn)->length ())
775 vec_safe_grow_cleared (basic_block_info_for_fn (fn), bb_count);
777 if (bb_count > label_to_block_map_for_fn (fn)->length ())
778 vec_safe_grow_cleared (label_to_block_map_for_fn (fn), bb_count);
780 index = streamer_read_hwi (ib);
781 while (index != -1)
783 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
784 unsigned int edge_count;
786 if (bb == NULL)
787 bb = make_new_block (fn, index);
789 edge_count = streamer_read_uhwi (ib);
791 /* Connect up the CFG. */
792 for (i = 0; i < edge_count; i++)
794 unsigned int dest_index;
795 unsigned int edge_flags;
796 basic_block dest;
797 int probability;
798 gcov_type count;
799 edge e;
801 dest_index = streamer_read_uhwi (ib);
802 probability = (int) streamer_read_hwi (ib);
803 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
804 count_materialization_scale);
805 edge_flags = streamer_read_uhwi (ib);
807 dest = BASIC_BLOCK_FOR_FN (fn, dest_index);
809 if (dest == NULL)
810 dest = make_new_block (fn, dest_index);
812 e = make_edge (bb, dest, edge_flags);
813 e->probability = probability;
814 e->count = count;
817 index = streamer_read_hwi (ib);
820 p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
821 index = streamer_read_hwi (ib);
822 while (index != -1)
824 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
825 bb->prev_bb = p_bb;
826 p_bb->next_bb = bb;
827 p_bb = bb;
828 index = streamer_read_hwi (ib);
831 /* ??? The cfgloop interface is tied to cfun. */
832 gcc_assert (cfun == fn);
834 /* Input the loop tree. */
835 unsigned n_loops = streamer_read_uhwi (ib);
836 if (n_loops == 0)
837 return;
839 struct loops *loops = ggc_cleared_alloc<struct loops> ();
840 init_loops_structure (fn, loops, n_loops);
841 set_loops_for_fn (fn, loops);
843 /* Input each loop and associate it with its loop header so
844 flow_loops_find can rebuild the loop tree. */
845 for (unsigned i = 1; i < n_loops; ++i)
847 int header_index = streamer_read_hwi (ib);
848 if (header_index == -1)
850 loops->larray->quick_push (NULL);
851 continue;
854 struct loop *loop = alloc_loop ();
855 loop->header = BASIC_BLOCK_FOR_FN (fn, header_index);
856 loop->header->loop_father = loop;
858 /* Read everything copy_loop_info copies. */
859 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
860 loop->any_upper_bound = streamer_read_hwi (ib);
861 if (loop->any_upper_bound)
862 loop->nb_iterations_upper_bound = streamer_read_wi (ib);
863 loop->any_estimate = streamer_read_hwi (ib);
864 if (loop->any_estimate)
865 loop->nb_iterations_estimate = streamer_read_wi (ib);
867 /* Read OMP SIMD related info. */
868 loop->safelen = streamer_read_hwi (ib);
869 loop->dont_vectorize = streamer_read_hwi (ib);
870 loop->force_vectorize = streamer_read_hwi (ib);
871 loop->simduid = stream_read_tree (ib, data_in);
873 place_new_loop (fn, loop);
875 /* flow_loops_find doesn't like loops not in the tree, hook them
876 all as siblings of the tree root temporarily. */
877 flow_loop_tree_node_add (loops->tree_root, loop);
880 /* Rebuild the loop tree. */
881 flow_loops_find (loops);
885 /* Read the SSA names array for function FN from DATA_IN using input
886 block IB. */
888 static void
889 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
890 struct function *fn)
892 unsigned int i, size;
894 size = streamer_read_uhwi (ib);
895 init_ssanames (fn, size);
897 i = streamer_read_uhwi (ib);
898 while (i)
900 tree ssa_name, name;
901 bool is_default_def;
903 /* Skip over the elements that had been freed. */
904 while (SSANAMES (fn)->length () < i)
905 SSANAMES (fn)->quick_push (NULL_TREE);
907 is_default_def = (streamer_read_uchar (ib) != 0);
908 name = stream_read_tree (ib, data_in);
909 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
911 if (is_default_def)
912 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
914 i = streamer_read_uhwi (ib);
919 /* Go through all NODE edges and fixup call_stmt pointers
920 so they point to STMTS. */
922 static void
923 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
924 struct function *fn)
926 struct cgraph_edge *cedge;
927 struct ipa_ref *ref = NULL;
928 unsigned int i;
930 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
932 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
933 fatal_error (input_location,
934 "Cgraph edge statement index out of range");
935 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
936 if (!cedge->call_stmt)
937 fatal_error (input_location,
938 "Cgraph edge statement index not found");
940 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
942 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
943 fatal_error (input_location,
944 "Cgraph edge statement index out of range");
945 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
946 if (!cedge->call_stmt)
947 fatal_error (input_location, "Cgraph edge statement index not found");
949 for (i = 0; node->iterate_reference (i, ref); i++)
950 if (ref->lto_stmt_uid)
952 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
953 fatal_error (input_location,
954 "Reference statement index out of range");
955 ref->stmt = stmts[ref->lto_stmt_uid - 1];
956 if (!ref->stmt)
957 fatal_error (input_location, "Reference statement index not found");
962 /* Fixup call_stmt pointers in NODE and all clones. */
964 static void
965 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
967 struct cgraph_node *node;
968 struct function *fn;
970 while (orig->clone_of)
971 orig = orig->clone_of;
972 fn = DECL_STRUCT_FUNCTION (orig->decl);
974 fixup_call_stmt_edges_1 (orig, stmts, fn);
975 if (orig->clones)
976 for (node = orig->clones; node != orig;)
978 fixup_call_stmt_edges_1 (node, stmts, fn);
979 if (node->clones)
980 node = node->clones;
981 else if (node->next_sibling_clone)
982 node = node->next_sibling_clone;
983 else
985 while (node != orig && !node->next_sibling_clone)
986 node = node->clone_of;
987 if (node != orig)
988 node = node->next_sibling_clone;
994 /* Input the base body of struct function FN from DATA_IN
995 using input block IB. */
997 static void
998 input_struct_function_base (struct function *fn, struct data_in *data_in,
999 struct lto_input_block *ib)
1001 struct bitpack_d bp;
1002 int len;
1004 /* Read the static chain and non-local goto save area. */
1005 fn->static_chain_decl = stream_read_tree (ib, data_in);
1006 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
1008 /* Read all the local symbols. */
1009 len = streamer_read_hwi (ib);
1010 if (len > 0)
1012 int i;
1013 vec_safe_grow_cleared (fn->local_decls, len);
1014 for (i = 0; i < len; i++)
1016 tree t = stream_read_tree (ib, data_in);
1017 (*fn->local_decls)[i] = t;
1021 /* Input the current IL state of the function. */
1022 fn->curr_properties = streamer_read_uhwi (ib);
1024 /* Read all the attributes for FN. */
1025 bp = streamer_read_bitpack (ib);
1026 fn->is_thunk = bp_unpack_value (&bp, 1);
1027 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
1028 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
1029 fn->returns_struct = bp_unpack_value (&bp, 1);
1030 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
1031 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
1032 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
1033 fn->after_inlining = bp_unpack_value (&bp, 1);
1034 fn->stdarg = bp_unpack_value (&bp, 1);
1035 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
1036 fn->calls_alloca = bp_unpack_value (&bp, 1);
1037 fn->calls_setjmp = bp_unpack_value (&bp, 1);
1038 fn->has_force_vectorize_loops = bp_unpack_value (&bp, 1);
1039 fn->has_simduid_loops = bp_unpack_value (&bp, 1);
1040 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
1041 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
1042 fn->last_clique = bp_unpack_value (&bp, sizeof (short) * 8);
1044 /* Input the function start and end loci. */
1045 fn->function_start_locus = stream_input_location_now (&bp, data_in);
1046 fn->function_end_locus = stream_input_location_now (&bp, data_in);
1050 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1052 static void
1053 input_function (tree fn_decl, struct data_in *data_in,
1054 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
1056 struct function *fn;
1057 enum LTO_tags tag;
1058 gimple *stmts;
1059 basic_block bb;
1060 struct cgraph_node *node;
1062 tag = streamer_read_record_start (ib);
1063 lto_tag_check (tag, LTO_function);
1065 /* Read decls for parameters and args. */
1066 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
1067 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
1069 /* Read the tree of lexical scopes for the function. */
1070 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
1072 if (!streamer_read_uhwi (ib))
1073 return;
1075 push_struct_function (fn_decl);
1076 fn = DECL_STRUCT_FUNCTION (fn_decl);
1077 init_tree_ssa (fn);
1078 /* We input IL in SSA form. */
1079 cfun->gimple_df->in_ssa_p = true;
1081 gimple_register_cfg_hooks ();
1083 node = cgraph_node::get (fn_decl);
1084 if (!node)
1085 node = cgraph_node::create (fn_decl);
1086 input_struct_function_base (fn, data_in, ib);
1087 input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
1089 /* Read all the SSA names. */
1090 input_ssa_names (ib, data_in, fn);
1092 /* Read the exception handling regions in the function. */
1093 input_eh_regions (ib, data_in, fn);
1095 gcc_assert (DECL_INITIAL (fn_decl));
1096 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
1098 /* Read all the basic blocks. */
1099 tag = streamer_read_record_start (ib);
1100 while (tag)
1102 input_bb (ib, tag, data_in, fn,
1103 node->count_materialization_scale);
1104 tag = streamer_read_record_start (ib);
1107 /* Fix up the call statements that are mentioned in the callgraph
1108 edges. */
1109 set_gimple_stmt_max_uid (cfun, 0);
1110 FOR_ALL_BB_FN (bb, cfun)
1112 gimple_stmt_iterator gsi;
1113 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1115 gimple stmt = gsi_stmt (gsi);
1116 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1118 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1120 gimple stmt = gsi_stmt (gsi);
1121 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1124 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1125 FOR_ALL_BB_FN (bb, cfun)
1127 gimple_stmt_iterator bsi = gsi_start_phis (bb);
1128 while (!gsi_end_p (bsi))
1130 gimple stmt = gsi_stmt (bsi);
1131 gsi_next (&bsi);
1132 stmts[gimple_uid (stmt)] = stmt;
1134 bsi = gsi_start_bb (bb);
1135 while (!gsi_end_p (bsi))
1137 gimple stmt = gsi_stmt (bsi);
1138 /* If we're recompiling LTO objects with debug stmts but
1139 we're not supposed to have debug stmts, remove them now.
1140 We can't remove them earlier because this would cause uid
1141 mismatches in fixups, but we can do it at this point, as
1142 long as debug stmts don't require fixups. */
1143 if (!MAY_HAVE_DEBUG_STMTS && !flag_wpa && is_gimple_debug (stmt))
1145 gimple_stmt_iterator gsi = bsi;
1146 gsi_next (&bsi);
1147 gsi_remove (&gsi, true);
1149 else
1151 gsi_next (&bsi);
1152 stmts[gimple_uid (stmt)] = stmt;
1157 /* Set the gimple body to the statement sequence in the entry
1158 basic block. FIXME lto, this is fairly hacky. The existence
1159 of a gimple body is used by the cgraph routines, but we should
1160 really use the presence of the CFG. */
1162 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1163 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1166 fixup_call_stmt_edges (node, stmts);
1167 execute_all_ipa_stmt_fixups (node, stmts);
1169 update_ssa (TODO_update_ssa_only_virtuals);
1170 free_dominance_info (CDI_DOMINATORS);
1171 free_dominance_info (CDI_POST_DOMINATORS);
1172 free (stmts);
1173 pop_cfun ();
1176 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1178 static void
1179 input_constructor (tree var, struct data_in *data_in,
1180 struct lto_input_block *ib)
1182 DECL_INITIAL (var) = stream_read_tree (ib, data_in);
1186 /* Read the body from DATA for function NODE and fill it in.
1187 FILE_DATA are the global decls and types. SECTION_TYPE is either
1188 LTO_section_function_body or LTO_section_static_initializer. If
1189 section type is LTO_section_function_body, FN must be the decl for
1190 that function. */
1192 static void
1193 lto_read_body_or_constructor (struct lto_file_decl_data *file_data, struct symtab_node *node,
1194 const char *data, enum lto_section_type section_type)
1196 const struct lto_function_header *header;
1197 struct data_in *data_in;
1198 int cfg_offset;
1199 int main_offset;
1200 int string_offset;
1201 tree fn_decl = node->decl;
1203 header = (const struct lto_function_header *) data;
1204 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1206 cfg_offset = sizeof (struct lto_function_header);
1207 main_offset = cfg_offset + header->cfg_size;
1208 string_offset = main_offset + header->main_size;
1210 else
1212 main_offset = sizeof (struct lto_function_header);
1213 string_offset = main_offset + header->main_size;
1216 data_in = lto_data_in_create (file_data, data + string_offset,
1217 header->string_size, vNULL);
1219 if (section_type == LTO_section_function_body)
1221 struct lto_in_decl_state *decl_state;
1222 unsigned from;
1224 gcc_checking_assert (node);
1226 /* Use the function's decl state. */
1227 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1228 gcc_assert (decl_state);
1229 file_data->current_decl_state = decl_state;
1232 /* Set up the struct function. */
1233 from = data_in->reader_cache->nodes.length ();
1234 lto_input_block ib_main (data + main_offset, header->main_size,
1235 file_data->mode_table);
1236 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1238 lto_input_block ib_cfg (data + cfg_offset, header->cfg_size,
1239 file_data->mode_table);
1240 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1242 else
1243 input_constructor (fn_decl, data_in, &ib_main);
1244 data_in->location_cache.apply_location_cache ();
1245 /* And fixup types we streamed locally. */
1247 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1248 unsigned len = cache->nodes.length ();
1249 unsigned i;
1250 for (i = len; i-- > from;)
1252 tree t = streamer_tree_cache_get_tree (cache, i);
1253 if (t == NULL_TREE)
1254 continue;
1256 if (TYPE_P (t))
1258 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1259 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1260 if (TYPE_MAIN_VARIANT (t) != t)
1262 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1263 TYPE_NEXT_VARIANT (t)
1264 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1265 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1271 /* Restore decl state */
1272 file_data->current_decl_state = file_data->global_decl_state;
1275 lto_data_in_delete (data_in);
1279 /* Read the body of NODE using DATA. FILE_DATA holds the global
1280 decls and types. */
1282 void
1283 lto_input_function_body (struct lto_file_decl_data *file_data,
1284 struct cgraph_node *node, const char *data)
1286 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1289 /* Read the body of NODE using DATA. FILE_DATA holds the global
1290 decls and types. */
1292 void
1293 lto_input_variable_constructor (struct lto_file_decl_data *file_data,
1294 struct varpool_node *node, const char *data)
1296 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1300 /* Read the physical representation of a tree node EXPR from
1301 input block IB using the per-file context in DATA_IN. */
1303 static void
1304 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1306 /* Read all the bitfield values in EXPR. Note that for LTO, we
1307 only write language-independent bitfields, so no more unpacking is
1308 needed. */
1309 streamer_read_tree_bitfields (ib, data_in, expr);
1311 /* Read all the pointer fields in EXPR. */
1312 streamer_read_tree_body (ib, data_in, expr);
1314 /* Read any LTO-specific data not read by the tree streamer. */
1315 if (DECL_P (expr)
1316 && TREE_CODE (expr) != FUNCTION_DECL
1317 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1318 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1320 /* We should never try to instantiate an MD or NORMAL builtin here. */
1321 if (TREE_CODE (expr) == FUNCTION_DECL)
1322 gcc_assert (!streamer_handle_as_builtin_p (expr));
1324 #ifdef LTO_STREAMER_DEBUG
1325 /* Remove the mapping to RESULT's original address set by
1326 streamer_alloc_tree. */
1327 lto_orig_address_remove (expr);
1328 #endif
1331 /* Read the physical representation of a tree node with tag TAG from
1332 input block IB using the per-file context in DATA_IN. */
1334 static tree
1335 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1336 enum LTO_tags tag, hashval_t hash)
1338 /* Instantiate a new tree node. */
1339 tree result = streamer_alloc_tree (ib, data_in, tag);
1341 /* Enter RESULT in the reader cache. This will make RESULT
1342 available so that circular references in the rest of the tree
1343 structure can be resolved in subsequent calls to stream_read_tree. */
1344 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1346 lto_read_tree_1 (ib, data_in, result);
1348 /* end_marker = */ streamer_read_uchar (ib);
1350 return result;
1354 /* Populate the reader cache with trees materialized from the SCC
1355 following in the IB, DATA_IN stream. */
1357 hashval_t
1358 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1359 unsigned *len, unsigned *entry_len)
1361 /* A blob of unnamed tree nodes, fill the cache from it and
1362 recurse. */
1363 unsigned size = streamer_read_uhwi (ib);
1364 hashval_t scc_hash = streamer_read_uhwi (ib);
1365 unsigned scc_entry_len = 1;
1367 if (size == 1)
1369 enum LTO_tags tag = streamer_read_record_start (ib);
1370 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1372 else
1374 unsigned int first = data_in->reader_cache->nodes.length ();
1375 tree result;
1377 scc_entry_len = streamer_read_uhwi (ib);
1379 /* Materialize size trees by reading their headers. */
1380 for (unsigned i = 0; i < size; ++i)
1382 enum LTO_tags tag = streamer_read_record_start (ib);
1383 if (tag == LTO_null
1384 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1385 || tag == LTO_tree_pickle_reference
1386 || tag == LTO_builtin_decl
1387 || tag == LTO_integer_cst
1388 || tag == LTO_tree_scc)
1389 gcc_unreachable ();
1391 result = streamer_alloc_tree (ib, data_in, tag);
1392 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1395 /* Read the tree bitpacks and references. */
1396 for (unsigned i = 0; i < size; ++i)
1398 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1399 first + i);
1400 lto_read_tree_1 (ib, data_in, result);
1401 /* end_marker = */ streamer_read_uchar (ib);
1405 *len = size;
1406 *entry_len = scc_entry_len;
1407 return scc_hash;
1411 /* Read a tree from input block IB using the per-file context in
1412 DATA_IN. This context is used, for example, to resolve references
1413 to previously read nodes. */
1415 tree
1416 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1417 enum LTO_tags tag, hashval_t hash)
1419 tree result;
1421 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1423 if (tag == LTO_null)
1424 result = NULL_TREE;
1425 else if (tag >= LTO_field_decl_ref && tag <= LTO_namelist_decl_ref)
1427 /* If TAG is a reference to an indexable tree, the next value
1428 in IB is the index into the table where we expect to find
1429 that tree. */
1430 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1432 else if (tag == LTO_tree_pickle_reference)
1434 /* If TAG is a reference to a previously read tree, look it up in
1435 the reader cache. */
1436 result = streamer_get_pickled_tree (ib, data_in);
1438 else if (tag == LTO_builtin_decl)
1440 /* If we are going to read a built-in function, all we need is
1441 the code and class. */
1442 result = streamer_get_builtin_tree (ib, data_in);
1444 else if (tag == LTO_integer_cst)
1446 /* For shared integer constants in singletons we can use the
1447 existing tree integer constant merging code. */
1448 tree type = stream_read_tree (ib, data_in);
1449 unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
1450 unsigned HOST_WIDE_INT i;
1451 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
1453 for (i = 0; i < len; i++)
1454 a[i] = streamer_read_hwi (ib);
1455 gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
1456 result = wide_int_to_tree (type, wide_int::from_array
1457 (a, len, TYPE_PRECISION (type)));
1458 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1460 else if (tag == LTO_tree_scc)
1461 gcc_unreachable ();
1462 else
1464 /* Otherwise, materialize a new node from IB. */
1465 result = lto_read_tree (ib, data_in, tag, hash);
1468 return result;
1471 tree
1472 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1474 enum LTO_tags tag;
1476 /* Input and skip SCCs. */
1477 while ((tag = streamer_read_record_start (ib)) == LTO_tree_scc)
1479 unsigned len, entry_len;
1480 lto_input_scc (ib, data_in, &len, &entry_len);
1482 return lto_input_tree_1 (ib, data_in, tag, 0);
1486 /* Input toplevel asms. */
1488 void
1489 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1491 size_t len;
1492 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1493 NULL, &len);
1494 const struct lto_simple_header_with_strings *header
1495 = (const struct lto_simple_header_with_strings *) data;
1496 int string_offset;
1497 struct data_in *data_in;
1498 tree str;
1500 if (! data)
1501 return;
1503 string_offset = sizeof (*header) + header->main_size;
1505 lto_input_block ib (data + sizeof (*header), header->main_size,
1506 file_data->mode_table);
1508 data_in = lto_data_in_create (file_data, data + string_offset,
1509 header->string_size, vNULL);
1511 while ((str = streamer_read_string_cst (data_in, &ib)))
1513 asm_node *node = symtab->finalize_toplevel_asm (str);
1514 node->order = streamer_read_hwi (&ib) + order_base;
1515 if (node->order >= symtab->order)
1516 symtab->order = node->order + 1;
1519 lto_data_in_delete (data_in);
1521 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1525 /* Input mode table. */
1527 void
1528 lto_input_mode_table (struct lto_file_decl_data *file_data)
1530 size_t len;
1531 const char *data = lto_get_section_data (file_data, LTO_section_mode_table,
1532 NULL, &len);
1533 if (! data)
1535 internal_error ("cannot read LTO mode table from %s",
1536 file_data->file_name);
1537 return;
1540 unsigned char *table = ggc_cleared_vec_alloc<unsigned char> (1 << 8);
1541 file_data->mode_table = table;
1542 const struct lto_simple_header_with_strings *header
1543 = (const struct lto_simple_header_with_strings *) data;
1544 int string_offset;
1545 struct data_in *data_in;
1546 string_offset = sizeof (*header) + header->main_size;
1548 lto_input_block ib (data + sizeof (*header), header->main_size, NULL);
1549 data_in = lto_data_in_create (file_data, data + string_offset,
1550 header->string_size, vNULL);
1551 bitpack_d bp = streamer_read_bitpack (&ib);
1553 table[VOIDmode] = VOIDmode;
1554 table[BLKmode] = BLKmode;
1555 unsigned int m;
1556 while ((m = bp_unpack_value (&bp, 8)) != VOIDmode)
1558 enum mode_class mclass
1559 = bp_unpack_enum (&bp, mode_class, MAX_MODE_CLASS);
1560 unsigned int size = bp_unpack_value (&bp, 8);
1561 unsigned int prec = bp_unpack_value (&bp, 16);
1562 machine_mode inner = (machine_mode) table[bp_unpack_value (&bp, 8)];
1563 unsigned int nunits = bp_unpack_value (&bp, 8);
1564 unsigned int ibit = 0, fbit = 0;
1565 unsigned int real_fmt_len = 0;
1566 const char *real_fmt_name = NULL;
1567 switch (mclass)
1569 case MODE_FRACT:
1570 case MODE_UFRACT:
1571 case MODE_ACCUM:
1572 case MODE_UACCUM:
1573 ibit = bp_unpack_value (&bp, 8);
1574 fbit = bp_unpack_value (&bp, 8);
1575 break;
1576 case MODE_FLOAT:
1577 case MODE_DECIMAL_FLOAT:
1578 real_fmt_name = bp_unpack_indexed_string (data_in, &bp,
1579 &real_fmt_len);
1580 break;
1581 default:
1582 break;
1584 /* First search just the GET_CLASS_NARROWEST_MODE to wider modes,
1585 if not found, fallback to all modes. */
1586 int pass;
1587 for (pass = 0; pass < 2; pass++)
1588 for (machine_mode mr = pass ? VOIDmode
1589 : GET_CLASS_NARROWEST_MODE (mclass);
1590 pass ? mr < MAX_MACHINE_MODE : mr != VOIDmode;
1591 pass ? mr = (machine_mode) (m + 1)
1592 : mr = GET_MODE_WIDER_MODE (mr))
1593 if (GET_MODE_CLASS (mr) != mclass
1594 || GET_MODE_SIZE (mr) != size
1595 || GET_MODE_PRECISION (mr) != prec
1596 || GET_MODE_INNER (mr) != inner
1597 || GET_MODE_IBIT (mr) != ibit
1598 || GET_MODE_FBIT (mr) != fbit
1599 || GET_MODE_NUNITS (mr) != nunits)
1600 continue;
1601 else if ((mclass == MODE_FLOAT || mclass == MODE_DECIMAL_FLOAT)
1602 && strcmp (REAL_MODE_FORMAT (mr)->name, real_fmt_name) != 0)
1603 continue;
1604 else
1606 table[m] = mr;
1607 pass = 2;
1608 break;
1610 unsigned int mname_len;
1611 const char *mname = bp_unpack_indexed_string (data_in, &bp, &mname_len);
1612 if (pass == 2)
1614 switch (mclass)
1616 case MODE_VECTOR_INT:
1617 case MODE_VECTOR_FLOAT:
1618 case MODE_VECTOR_FRACT:
1619 case MODE_VECTOR_UFRACT:
1620 case MODE_VECTOR_ACCUM:
1621 case MODE_VECTOR_UACCUM:
1622 /* For unsupported vector modes just use BLKmode,
1623 if the scalar mode is supported. */
1624 if (inner != VOIDmode)
1626 table[m] = BLKmode;
1627 break;
1629 /* FALLTHRU */
1630 default:
1631 fatal_error (UNKNOWN_LOCATION, "unsupported mode %s\n", mname);
1632 break;
1636 lto_data_in_delete (data_in);
1638 lto_free_section_data (file_data, LTO_section_mode_table, NULL, data, len);
1642 /* Initialization for the LTO reader. */
1644 void
1645 lto_reader_init (void)
1647 lto_streamer_init ();
1648 file_name_hash_table
1649 = new hash_table<freeing_string_slot_hasher> (37);
1653 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1654 table to use with LEN strings. RESOLUTIONS is the vector of linker
1655 resolutions (NULL if not using a linker plugin). */
1657 struct data_in *
1658 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1659 unsigned len,
1660 vec<ld_plugin_symbol_resolution_t> resolutions)
1662 struct data_in *data_in = new (struct data_in);
1663 data_in->file_data = file_data;
1664 data_in->strings = strings;
1665 data_in->strings_len = len;
1666 data_in->globals_resolution = resolutions;
1667 data_in->reader_cache = streamer_tree_cache_create (false, false, true);
1668 return data_in;
1672 /* Remove DATA_IN. */
1674 void
1675 lto_data_in_delete (struct data_in *data_in)
1677 data_in->globals_resolution.release ();
1678 streamer_tree_cache_delete (data_in->reader_cache);
1679 delete data_in;