2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lto-streamer-in.c
blob7729b6cf6978d5af58c92319829dd60d612c7d30
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 "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "tree.h"
32 #include "fold-const.h"
33 #include "stringpool.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "insn-config.h"
39 #include "expmed.h"
40 #include "dojump.h"
41 #include "explow.h"
42 #include "calls.h"
43 #include "emit-rtl.h"
44 #include "varasm.h"
45 #include "stmt.h"
46 #include "expr.h"
47 #include "params.h"
48 #include "predict.h"
49 #include "dominance.h"
50 #include "cfg.h"
51 #include "basic-block.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "gimple-expr.h"
55 #include "is-a.h"
56 #include "gimple.h"
57 #include "gimple-iterator.h"
58 #include "gimple-ssa.h"
59 #include "tree-cfg.h"
60 #include "tree-ssanames.h"
61 #include "tree-into-ssa.h"
62 #include "tree-dfa.h"
63 #include "tree-ssa.h"
64 #include "tree-pass.h"
65 #include "diagnostic.h"
66 #include "except.h"
67 #include "debug.h"
68 #include "plugin-api.h"
69 #include "ipa-ref.h"
70 #include "cgraph.h"
71 #include "ipa-utils.h"
72 #include "data-streamer.h"
73 #include "gimple-streamer.h"
74 #include "lto-streamer.h"
75 #include "tree-streamer.h"
76 #include "streamer-hooks.h"
77 #include "cfgloop.h"
80 struct freeing_string_slot_hasher : string_slot_hasher
82 static inline void remove (value_type *);
85 inline void
86 freeing_string_slot_hasher::remove (value_type *v)
88 free (v);
91 /* The table to hold the file names. */
92 static hash_table<freeing_string_slot_hasher> *file_name_hash_table;
95 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
96 number of valid tag values to check. */
98 void
99 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
101 va_list ap;
102 int i;
104 va_start (ap, ntags);
105 for (i = 0; i < ntags; i++)
106 if ((unsigned) actual == va_arg (ap, unsigned))
108 va_end (ap);
109 return;
112 va_end (ap);
113 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
117 /* Read LENGTH bytes from STREAM to ADDR. */
119 void
120 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
122 size_t i;
123 unsigned char *const buffer = (unsigned char *const) addr;
125 for (i = 0; i < length; i++)
126 buffer[i] = streamer_read_uchar (ib);
130 /* Lookup STRING in file_name_hash_table. If found, return the existing
131 string, otherwise insert STRING as the canonical version. */
133 static const char *
134 canon_file_name (const char *string)
136 string_slot **slot;
137 struct string_slot s_slot;
138 size_t len = strlen (string);
140 s_slot.s = string;
141 s_slot.len = len;
143 slot = file_name_hash_table->find_slot (&s_slot, INSERT);
144 if (*slot == NULL)
146 char *saved_string;
147 struct string_slot *new_slot;
149 saved_string = (char *) xmalloc (len + 1);
150 new_slot = XCNEW (struct string_slot);
151 memcpy (saved_string, string, len + 1);
152 new_slot->s = saved_string;
153 new_slot->len = len;
154 *slot = new_slot;
155 return saved_string;
157 else
159 struct string_slot *old_slot = *slot;
160 return old_slot->s;
164 /* Pointer to currently alive instance of lto_location_cache. */
166 lto_location_cache *lto_location_cache::current_cache;
168 /* Sort locations in source order. Start with file from last application. */
171 lto_location_cache::cmp_loc (const void *pa, const void *pb)
173 const cached_location *a = ((const cached_location *)pa);
174 const cached_location *b = ((const cached_location *)pb);
175 const char *current_file = current_cache->current_file;
176 int current_line = current_cache->current_line;
178 if (a->file == current_file && b->file != current_file)
179 return -1;
180 if (a->file != current_file && b->file == current_file)
181 return 1;
182 if (a->file == current_file && b->file == current_file)
184 if (a->line == current_line && b->line != current_line)
185 return -1;
186 if (a->line != current_line && b->line == current_line)
187 return 1;
189 if (a->file != b->file)
190 return strcmp (a->file, b->file);
191 if (a->line != b->line)
192 return a->line - b->line;
193 return a->col - b->col;
196 /* Apply all changes in location cache. Add locations into linemap and patch
197 trees. */
199 bool
200 lto_location_cache::apply_location_cache ()
202 static const char *prev_file;
203 if (!loc_cache.length ())
204 return false;
205 if (loc_cache.length () > 1)
206 loc_cache.qsort (cmp_loc);
208 for (unsigned int i = 0; i < loc_cache.length (); i++)
210 struct cached_location loc = loc_cache[i];
212 if (current_file != loc.file)
213 linemap_add (line_table, prev_file ? LC_RENAME : LC_ENTER,
214 false, loc.file, loc.line);
215 else if (current_line != loc.line)
217 int max = loc.col;
219 for (unsigned int j = i + 1; j < loc_cache.length (); j++)
220 if (loc.file != loc_cache[j].file
221 || loc.line != loc_cache[j].line)
222 break;
223 else if (max < loc_cache[j].col)
224 max = loc_cache[j].col;
225 linemap_line_start (line_table, loc.line, max + 1);
227 gcc_assert (*loc.loc == BUILTINS_LOCATION + 1);
228 if (current_file == loc.file && current_line == loc.line
229 && current_col == loc.col)
230 *loc.loc = current_loc;
231 else
232 current_loc = *loc.loc = linemap_position_for_column (line_table,
233 loc.col);
234 current_line = loc.line;
235 prev_file = current_file = loc.file;
236 current_col = loc.col;
238 loc_cache.truncate (0);
239 accepted_length = 0;
240 return true;
243 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
245 void
246 lto_location_cache::accept_location_cache ()
248 gcc_assert (current_cache == this);
249 accepted_length = loc_cache.length ();
252 /* Tree merging did suceed; throw away recent changes. */
254 void
255 lto_location_cache::revert_location_cache ()
257 loc_cache.truncate (accepted_length);
260 /* Read a location bitpack from input block IB and either update *LOC directly
261 or add it to the location cache.
262 It is neccesary to call apply_location_cache to get *LOC updated. */
264 void
265 lto_location_cache::input_location (location_t *loc, struct bitpack_d *bp,
266 struct data_in *data_in)
268 static const char *stream_file;
269 static int stream_line;
270 static int stream_col;
271 bool file_change, line_change, column_change;
273 gcc_assert (current_cache == this);
275 *loc = bp_unpack_int_in_range (bp, "location", 0, RESERVED_LOCATION_COUNT);
277 if (*loc < RESERVED_LOCATION_COUNT)
278 return;
280 /* Keep value RESERVED_LOCATION_COUNT in *loc as linemap lookups will
281 ICE on it. */
283 file_change = bp_unpack_value (bp, 1);
284 line_change = bp_unpack_value (bp, 1);
285 column_change = bp_unpack_value (bp, 1);
287 if (file_change)
288 stream_file = canon_file_name (bp_unpack_string (data_in, bp));
290 if (line_change)
291 stream_line = bp_unpack_var_len_unsigned (bp);
293 if (column_change)
294 stream_col = bp_unpack_var_len_unsigned (bp);
296 /* This optimization saves location cache operations druing gimple
297 streaming. */
299 if (current_file == stream_file && current_line == stream_line
300 && current_col == stream_col)
302 *loc = current_loc;
303 return;
306 struct cached_location entry = {stream_file, loc, stream_line, stream_col};
307 loc_cache.safe_push (entry);
310 /* Read a location bitpack from input block IB and either update *LOC directly
311 or add it to the location cache.
312 It is neccesary to call apply_location_cache to get *LOC updated. */
314 void
315 lto_input_location (location_t *loc, struct bitpack_d *bp,
316 struct data_in *data_in)
318 data_in->location_cache.input_location (loc, bp, data_in);
321 /* Read location and return it instead of going through location caching.
322 This should be used only when the resulting location is not going to be
323 discarded. */
325 location_t
326 stream_input_location_now (struct bitpack_d *bp, struct data_in *data_in)
328 location_t loc;
329 stream_input_location (&loc, bp, data_in);
330 data_in->location_cache.apply_location_cache ();
331 return loc;
334 /* Read a reference to a tree node from DATA_IN using input block IB.
335 TAG is the expected node that should be found in IB, if TAG belongs
336 to one of the indexable trees, expect to read a reference index to
337 be looked up in one of the symbol tables, otherwise read the pysical
338 representation of the tree using stream_read_tree. FN is the
339 function scope for the read tree. */
341 tree
342 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
343 struct function *fn, enum LTO_tags tag)
345 unsigned HOST_WIDE_INT ix_u;
346 tree result = NULL_TREE;
348 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_namelist_decl_ref);
350 switch (tag)
352 case LTO_type_ref:
353 ix_u = streamer_read_uhwi (ib);
354 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
355 break;
357 case LTO_ssa_name_ref:
358 ix_u = streamer_read_uhwi (ib);
359 result = (*SSANAMES (fn))[ix_u];
360 break;
362 case LTO_field_decl_ref:
363 ix_u = streamer_read_uhwi (ib);
364 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
365 break;
367 case LTO_function_decl_ref:
368 ix_u = streamer_read_uhwi (ib);
369 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
370 break;
372 case LTO_type_decl_ref:
373 ix_u = streamer_read_uhwi (ib);
374 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
375 break;
377 case LTO_namespace_decl_ref:
378 ix_u = streamer_read_uhwi (ib);
379 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
380 break;
382 case LTO_global_decl_ref:
383 case LTO_result_decl_ref:
384 case LTO_const_decl_ref:
385 case LTO_imported_decl_ref:
386 case LTO_label_decl_ref:
387 case LTO_translation_unit_decl_ref:
388 case LTO_namelist_decl_ref:
389 ix_u = streamer_read_uhwi (ib);
390 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
391 break;
393 default:
394 gcc_unreachable ();
397 gcc_assert (result);
399 return result;
403 /* Read and return a double-linked list of catch handlers from input
404 block IB, using descriptors in DATA_IN. */
406 static struct eh_catch_d *
407 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
408 eh_catch *last_p)
410 eh_catch first;
411 enum LTO_tags tag;
413 *last_p = first = NULL;
414 tag = streamer_read_record_start (ib);
415 while (tag)
417 tree list;
418 eh_catch n;
420 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
422 /* Read the catch node. */
423 n = ggc_cleared_alloc<eh_catch_d> ();
424 n->type_list = stream_read_tree (ib, data_in);
425 n->filter_list = stream_read_tree (ib, data_in);
426 n->label = stream_read_tree (ib, data_in);
428 /* Register all the types in N->FILTER_LIST. */
429 for (list = n->filter_list; list; list = TREE_CHAIN (list))
430 add_type_for_runtime (TREE_VALUE (list));
432 /* Chain N to the end of the list. */
433 if (*last_p)
434 (*last_p)->next_catch = n;
435 n->prev_catch = *last_p;
436 *last_p = n;
438 /* Set the head of the list the first time through the loop. */
439 if (first == NULL)
440 first = n;
442 tag = streamer_read_record_start (ib);
445 return first;
449 /* Read and return EH region IX from input block IB, using descriptors
450 in DATA_IN. */
452 static eh_region
453 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
455 enum LTO_tags tag;
456 eh_region r;
458 /* Read the region header. */
459 tag = streamer_read_record_start (ib);
460 if (tag == LTO_null)
461 return NULL;
463 r = ggc_cleared_alloc<eh_region_d> ();
464 r->index = streamer_read_hwi (ib);
466 gcc_assert (r->index == ix);
468 /* Read all the region pointers as region numbers. We'll fix up
469 the pointers once the whole array has been read. */
470 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
471 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
472 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
474 switch (tag)
476 case LTO_ert_cleanup:
477 r->type = ERT_CLEANUP;
478 break;
480 case LTO_ert_try:
482 struct eh_catch_d *last_catch;
483 r->type = ERT_TRY;
484 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
485 &last_catch);
486 r->u.eh_try.last_catch = last_catch;
487 break;
490 case LTO_ert_allowed_exceptions:
492 tree l;
494 r->type = ERT_ALLOWED_EXCEPTIONS;
495 r->u.allowed.type_list = stream_read_tree (ib, data_in);
496 r->u.allowed.label = stream_read_tree (ib, data_in);
497 r->u.allowed.filter = streamer_read_uhwi (ib);
499 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
500 add_type_for_runtime (TREE_VALUE (l));
502 break;
504 case LTO_ert_must_not_throw:
506 r->type = ERT_MUST_NOT_THROW;
507 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
508 bitpack_d bp = streamer_read_bitpack (ib);
509 r->u.must_not_throw.failure_loc
510 = stream_input_location_now (&bp, data_in);
512 break;
514 default:
515 gcc_unreachable ();
518 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
520 return r;
524 /* Read and return EH landing pad IX from input block IB, using descriptors
525 in DATA_IN. */
527 static eh_landing_pad
528 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
530 enum LTO_tags tag;
531 eh_landing_pad lp;
533 /* Read the landing pad header. */
534 tag = streamer_read_record_start (ib);
535 if (tag == LTO_null)
536 return NULL;
538 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
540 lp = ggc_cleared_alloc<eh_landing_pad_d> ();
541 lp->index = streamer_read_hwi (ib);
542 gcc_assert (lp->index == ix);
543 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
544 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
545 lp->post_landing_pad = stream_read_tree (ib, data_in);
547 return lp;
551 /* After reading the EH regions, pointers to peer and children regions
552 are region numbers. This converts all these region numbers into
553 real pointers into the rematerialized regions for FN. ROOT_REGION
554 is the region number for the root EH region in FN. */
556 static void
557 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
559 unsigned i;
560 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
561 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
562 eh_region r;
563 eh_landing_pad lp;
565 gcc_assert (eh_array && lp_array);
567 gcc_assert (root_region >= 0);
568 fn->eh->region_tree = (*eh_array)[root_region];
570 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
571 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
573 /* Convert all the index numbers stored in pointer fields into
574 pointers to the corresponding slots in the EH region array. */
575 FOR_EACH_VEC_ELT (*eh_array, i, r)
577 /* The array may contain NULL regions. */
578 if (r == NULL)
579 continue;
581 gcc_assert (i == (unsigned) r->index);
582 FIXUP_EH_REGION (r->outer);
583 FIXUP_EH_REGION (r->inner);
584 FIXUP_EH_REGION (r->next_peer);
585 FIXUP_EH_LP (r->landing_pads);
588 /* Convert all the index numbers stored in pointer fields into
589 pointers to the corresponding slots in the EH landing pad array. */
590 FOR_EACH_VEC_ELT (*lp_array, i, lp)
592 /* The array may contain NULL landing pads. */
593 if (lp == NULL)
594 continue;
596 gcc_assert (i == (unsigned) lp->index);
597 FIXUP_EH_LP (lp->next_lp);
598 FIXUP_EH_REGION (lp->region);
601 #undef FIXUP_EH_REGION
602 #undef FIXUP_EH_LP
606 /* Initialize EH support. */
608 void
609 lto_init_eh (void)
611 static bool eh_initialized_p = false;
613 if (eh_initialized_p)
614 return;
616 /* Contrary to most other FEs, we only initialize EH support when at
617 least one of the files in the set contains exception regions in
618 it. Since this happens much later than the call to init_eh in
619 lang_dependent_init, we have to set flag_exceptions and call
620 init_eh again to initialize the EH tables. */
621 flag_exceptions = 1;
622 init_eh ();
624 eh_initialized_p = true;
628 /* Read the exception table for FN from IB using the data descriptors
629 in DATA_IN. */
631 static void
632 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
633 struct function *fn)
635 HOST_WIDE_INT i, root_region, len;
636 enum LTO_tags tag;
638 tag = streamer_read_record_start (ib);
639 if (tag == LTO_null)
640 return;
642 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
644 /* If the file contains EH regions, then it was compiled with
645 -fexceptions. In that case, initialize the backend EH
646 machinery. */
647 lto_init_eh ();
649 gcc_assert (fn->eh);
651 root_region = streamer_read_hwi (ib);
652 gcc_assert (root_region == (int) root_region);
654 /* Read the EH region array. */
655 len = streamer_read_hwi (ib);
656 gcc_assert (len == (int) len);
657 if (len > 0)
659 vec_safe_grow_cleared (fn->eh->region_array, len);
660 for (i = 0; i < len; i++)
662 eh_region r = input_eh_region (ib, data_in, i);
663 (*fn->eh->region_array)[i] = r;
667 /* Read the landing pads. */
668 len = streamer_read_hwi (ib);
669 gcc_assert (len == (int) len);
670 if (len > 0)
672 vec_safe_grow_cleared (fn->eh->lp_array, len);
673 for (i = 0; i < len; i++)
675 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
676 (*fn->eh->lp_array)[i] = lp;
680 /* Read the runtime type data. */
681 len = streamer_read_hwi (ib);
682 gcc_assert (len == (int) len);
683 if (len > 0)
685 vec_safe_grow_cleared (fn->eh->ttype_data, len);
686 for (i = 0; i < len; i++)
688 tree ttype = stream_read_tree (ib, data_in);
689 (*fn->eh->ttype_data)[i] = ttype;
693 /* Read the table of action chains. */
694 len = streamer_read_hwi (ib);
695 gcc_assert (len == (int) len);
696 if (len > 0)
698 if (targetm.arm_eabi_unwinder)
700 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
701 for (i = 0; i < len; i++)
703 tree t = stream_read_tree (ib, data_in);
704 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
707 else
709 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
710 for (i = 0; i < len; i++)
712 uchar c = streamer_read_uchar (ib);
713 (*fn->eh->ehspec_data.other)[i] = c;
718 /* Reconstruct the EH region tree by fixing up the peer/children
719 pointers. */
720 fixup_eh_region_pointers (fn, root_region);
722 tag = streamer_read_record_start (ib);
723 lto_tag_check_range (tag, LTO_null, LTO_null);
727 /* Make a new basic block with index INDEX in function FN. */
729 static basic_block
730 make_new_block (struct function *fn, unsigned int index)
732 basic_block bb = alloc_block ();
733 bb->index = index;
734 SET_BASIC_BLOCK_FOR_FN (fn, index, bb);
735 n_basic_blocks_for_fn (fn)++;
736 return bb;
740 /* Read a wide-int. */
742 static widest_int
743 streamer_read_wi (struct lto_input_block *ib)
745 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
746 int i;
747 int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
748 int len = streamer_read_uhwi (ib);
749 for (i = 0; i < len; i++)
750 a[i] = streamer_read_hwi (ib);
751 return widest_int::from_array (a, len);
755 /* Read the CFG for function FN from input block IB. */
757 static void
758 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
759 struct function *fn,
760 int count_materialization_scale)
762 unsigned int bb_count;
763 basic_block p_bb;
764 unsigned int i;
765 int index;
767 init_empty_tree_cfg_for_function (fn);
768 init_ssa_operands (fn);
770 profile_status_for_fn (fn) = streamer_read_enum (ib, profile_status_d,
771 PROFILE_LAST);
773 bb_count = streamer_read_uhwi (ib);
775 last_basic_block_for_fn (fn) = bb_count;
776 if (bb_count > basic_block_info_for_fn (fn)->length ())
777 vec_safe_grow_cleared (basic_block_info_for_fn (fn), bb_count);
779 if (bb_count > label_to_block_map_for_fn (fn)->length ())
780 vec_safe_grow_cleared (label_to_block_map_for_fn (fn), bb_count);
782 index = streamer_read_hwi (ib);
783 while (index != -1)
785 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
786 unsigned int edge_count;
788 if (bb == NULL)
789 bb = make_new_block (fn, index);
791 edge_count = streamer_read_uhwi (ib);
793 /* Connect up the CFG. */
794 for (i = 0; i < edge_count; i++)
796 unsigned int dest_index;
797 unsigned int edge_flags;
798 basic_block dest;
799 int probability;
800 gcov_type count;
801 edge e;
803 dest_index = streamer_read_uhwi (ib);
804 probability = (int) streamer_read_hwi (ib);
805 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
806 count_materialization_scale);
807 edge_flags = streamer_read_uhwi (ib);
809 dest = BASIC_BLOCK_FOR_FN (fn, dest_index);
811 if (dest == NULL)
812 dest = make_new_block (fn, dest_index);
814 e = make_edge (bb, dest, edge_flags);
815 e->probability = probability;
816 e->count = count;
819 index = streamer_read_hwi (ib);
822 p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
823 index = streamer_read_hwi (ib);
824 while (index != -1)
826 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
827 bb->prev_bb = p_bb;
828 p_bb->next_bb = bb;
829 p_bb = bb;
830 index = streamer_read_hwi (ib);
833 /* ??? The cfgloop interface is tied to cfun. */
834 gcc_assert (cfun == fn);
836 /* Input the loop tree. */
837 unsigned n_loops = streamer_read_uhwi (ib);
838 if (n_loops == 0)
839 return;
841 struct loops *loops = ggc_cleared_alloc<struct loops> ();
842 init_loops_structure (fn, loops, n_loops);
843 set_loops_for_fn (fn, loops);
845 /* Input each loop and associate it with its loop header so
846 flow_loops_find can rebuild the loop tree. */
847 for (unsigned i = 1; i < n_loops; ++i)
849 int header_index = streamer_read_hwi (ib);
850 if (header_index == -1)
852 loops->larray->quick_push (NULL);
853 continue;
856 struct loop *loop = alloc_loop ();
857 loop->header = BASIC_BLOCK_FOR_FN (fn, header_index);
858 loop->header->loop_father = loop;
860 /* Read everything copy_loop_info copies. */
861 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
862 loop->any_upper_bound = streamer_read_hwi (ib);
863 if (loop->any_upper_bound)
864 loop->nb_iterations_upper_bound = streamer_read_wi (ib);
865 loop->any_estimate = streamer_read_hwi (ib);
866 if (loop->any_estimate)
867 loop->nb_iterations_estimate = streamer_read_wi (ib);
869 /* Read OMP SIMD related info. */
870 loop->safelen = streamer_read_hwi (ib);
871 loop->dont_vectorize = streamer_read_hwi (ib);
872 loop->force_vectorize = streamer_read_hwi (ib);
873 loop->simduid = stream_read_tree (ib, data_in);
875 place_new_loop (fn, loop);
877 /* flow_loops_find doesn't like loops not in the tree, hook them
878 all as siblings of the tree root temporarily. */
879 flow_loop_tree_node_add (loops->tree_root, loop);
882 /* Rebuild the loop tree. */
883 flow_loops_find (loops);
887 /* Read the SSA names array for function FN from DATA_IN using input
888 block IB. */
890 static void
891 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
892 struct function *fn)
894 unsigned int i, size;
896 size = streamer_read_uhwi (ib);
897 init_ssanames (fn, size);
899 i = streamer_read_uhwi (ib);
900 while (i)
902 tree ssa_name, name;
903 bool is_default_def;
905 /* Skip over the elements that had been freed. */
906 while (SSANAMES (fn)->length () < i)
907 SSANAMES (fn)->quick_push (NULL_TREE);
909 is_default_def = (streamer_read_uchar (ib) != 0);
910 name = stream_read_tree (ib, data_in);
911 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
913 if (is_default_def)
914 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
916 i = streamer_read_uhwi (ib);
921 /* Go through all NODE edges and fixup call_stmt pointers
922 so they point to STMTS. */
924 static void
925 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
926 struct function *fn)
928 struct cgraph_edge *cedge;
929 struct ipa_ref *ref = NULL;
930 unsigned int i;
932 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
934 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
935 fatal_error (input_location,
936 "Cgraph edge statement index out of range");
937 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
938 if (!cedge->call_stmt)
939 fatal_error (input_location,
940 "Cgraph edge statement index not found");
942 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
944 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
945 fatal_error (input_location,
946 "Cgraph edge statement index out of range");
947 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
948 if (!cedge->call_stmt)
949 fatal_error (input_location, "Cgraph edge statement index not found");
951 for (i = 0; node->iterate_reference (i, ref); i++)
952 if (ref->lto_stmt_uid)
954 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
955 fatal_error (input_location,
956 "Reference statement index out of range");
957 ref->stmt = stmts[ref->lto_stmt_uid - 1];
958 if (!ref->stmt)
959 fatal_error (input_location, "Reference statement index not found");
964 /* Fixup call_stmt pointers in NODE and all clones. */
966 static void
967 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
969 struct cgraph_node *node;
970 struct function *fn;
972 while (orig->clone_of)
973 orig = orig->clone_of;
974 fn = DECL_STRUCT_FUNCTION (orig->decl);
976 fixup_call_stmt_edges_1 (orig, stmts, fn);
977 if (orig->clones)
978 for (node = orig->clones; node != orig;)
980 fixup_call_stmt_edges_1 (node, stmts, fn);
981 if (node->clones)
982 node = node->clones;
983 else if (node->next_sibling_clone)
984 node = node->next_sibling_clone;
985 else
987 while (node != orig && !node->next_sibling_clone)
988 node = node->clone_of;
989 if (node != orig)
990 node = node->next_sibling_clone;
996 /* Input the base body of struct function FN from DATA_IN
997 using input block IB. */
999 static void
1000 input_struct_function_base (struct function *fn, struct data_in *data_in,
1001 struct lto_input_block *ib)
1003 struct bitpack_d bp;
1004 int len;
1006 /* Read the static chain and non-local goto save area. */
1007 fn->static_chain_decl = stream_read_tree (ib, data_in);
1008 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
1010 /* Read all the local symbols. */
1011 len = streamer_read_hwi (ib);
1012 if (len > 0)
1014 int i;
1015 vec_safe_grow_cleared (fn->local_decls, len);
1016 for (i = 0; i < len; i++)
1018 tree t = stream_read_tree (ib, data_in);
1019 (*fn->local_decls)[i] = t;
1023 /* Input the current IL state of the function. */
1024 fn->curr_properties = streamer_read_uhwi (ib);
1026 /* Read all the attributes for FN. */
1027 bp = streamer_read_bitpack (ib);
1028 fn->is_thunk = bp_unpack_value (&bp, 1);
1029 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
1030 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
1031 fn->returns_struct = bp_unpack_value (&bp, 1);
1032 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
1033 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
1034 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
1035 fn->after_inlining = bp_unpack_value (&bp, 1);
1036 fn->stdarg = bp_unpack_value (&bp, 1);
1037 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
1038 fn->calls_alloca = bp_unpack_value (&bp, 1);
1039 fn->calls_setjmp = bp_unpack_value (&bp, 1);
1040 fn->has_force_vectorize_loops = bp_unpack_value (&bp, 1);
1041 fn->has_simduid_loops = bp_unpack_value (&bp, 1);
1042 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
1043 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
1044 fn->last_clique = bp_unpack_value (&bp, sizeof (short) * 8);
1046 /* Input the function start and end loci. */
1047 fn->function_start_locus = stream_input_location_now (&bp, data_in);
1048 fn->function_end_locus = stream_input_location_now (&bp, data_in);
1052 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1054 static void
1055 input_function (tree fn_decl, struct data_in *data_in,
1056 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
1058 struct function *fn;
1059 enum LTO_tags tag;
1060 gimple *stmts;
1061 basic_block bb;
1062 struct cgraph_node *node;
1064 tag = streamer_read_record_start (ib);
1065 lto_tag_check (tag, LTO_function);
1067 /* Read decls for parameters and args. */
1068 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
1069 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
1071 /* Read the tree of lexical scopes for the function. */
1072 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
1074 if (!streamer_read_uhwi (ib))
1075 return;
1077 push_struct_function (fn_decl);
1078 fn = DECL_STRUCT_FUNCTION (fn_decl);
1079 init_tree_ssa (fn);
1080 /* We input IL in SSA form. */
1081 cfun->gimple_df->in_ssa_p = true;
1083 gimple_register_cfg_hooks ();
1085 node = cgraph_node::get (fn_decl);
1086 if (!node)
1087 node = cgraph_node::create (fn_decl);
1088 input_struct_function_base (fn, data_in, ib);
1089 input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
1091 /* Read all the SSA names. */
1092 input_ssa_names (ib, data_in, fn);
1094 /* Read the exception handling regions in the function. */
1095 input_eh_regions (ib, data_in, fn);
1097 gcc_assert (DECL_INITIAL (fn_decl));
1098 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
1100 /* Read all the basic blocks. */
1101 tag = streamer_read_record_start (ib);
1102 while (tag)
1104 input_bb (ib, tag, data_in, fn,
1105 node->count_materialization_scale);
1106 tag = streamer_read_record_start (ib);
1109 /* Fix up the call statements that are mentioned in the callgraph
1110 edges. */
1111 set_gimple_stmt_max_uid (cfun, 0);
1112 FOR_ALL_BB_FN (bb, cfun)
1114 gimple_stmt_iterator gsi;
1115 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1117 gimple stmt = gsi_stmt (gsi);
1118 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1120 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1122 gimple stmt = gsi_stmt (gsi);
1123 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1126 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1127 FOR_ALL_BB_FN (bb, cfun)
1129 gimple_stmt_iterator bsi = gsi_start_phis (bb);
1130 while (!gsi_end_p (bsi))
1132 gimple stmt = gsi_stmt (bsi);
1133 gsi_next (&bsi);
1134 stmts[gimple_uid (stmt)] = stmt;
1136 bsi = gsi_start_bb (bb);
1137 while (!gsi_end_p (bsi))
1139 gimple stmt = gsi_stmt (bsi);
1140 /* If we're recompiling LTO objects with debug stmts but
1141 we're not supposed to have debug stmts, remove them now.
1142 We can't remove them earlier because this would cause uid
1143 mismatches in fixups, but we can do it at this point, as
1144 long as debug stmts don't require fixups. */
1145 if (!MAY_HAVE_DEBUG_STMTS && !flag_wpa && is_gimple_debug (stmt))
1147 gimple_stmt_iterator gsi = bsi;
1148 gsi_next (&bsi);
1149 gsi_remove (&gsi, true);
1151 else
1153 gsi_next (&bsi);
1154 stmts[gimple_uid (stmt)] = stmt;
1159 /* Set the gimple body to the statement sequence in the entry
1160 basic block. FIXME lto, this is fairly hacky. The existence
1161 of a gimple body is used by the cgraph routines, but we should
1162 really use the presence of the CFG. */
1164 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1165 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1168 fixup_call_stmt_edges (node, stmts);
1169 execute_all_ipa_stmt_fixups (node, stmts);
1171 update_ssa (TODO_update_ssa_only_virtuals);
1172 free_dominance_info (CDI_DOMINATORS);
1173 free_dominance_info (CDI_POST_DOMINATORS);
1174 free (stmts);
1175 pop_cfun ();
1178 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1180 static void
1181 input_constructor (tree var, struct data_in *data_in,
1182 struct lto_input_block *ib)
1184 DECL_INITIAL (var) = stream_read_tree (ib, data_in);
1188 /* Read the body from DATA for function NODE and fill it in.
1189 FILE_DATA are the global decls and types. SECTION_TYPE is either
1190 LTO_section_function_body or LTO_section_static_initializer. If
1191 section type is LTO_section_function_body, FN must be the decl for
1192 that function. */
1194 static void
1195 lto_read_body_or_constructor (struct lto_file_decl_data *file_data, struct symtab_node *node,
1196 const char *data, enum lto_section_type section_type)
1198 const struct lto_function_header *header;
1199 struct data_in *data_in;
1200 int cfg_offset;
1201 int main_offset;
1202 int string_offset;
1203 tree fn_decl = node->decl;
1205 header = (const struct lto_function_header *) data;
1206 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1208 cfg_offset = sizeof (struct lto_function_header);
1209 main_offset = cfg_offset + header->cfg_size;
1210 string_offset = main_offset + header->main_size;
1212 else
1214 main_offset = sizeof (struct lto_function_header);
1215 string_offset = main_offset + header->main_size;
1218 data_in = lto_data_in_create (file_data, data + string_offset,
1219 header->string_size, vNULL);
1221 if (section_type == LTO_section_function_body)
1223 struct lto_in_decl_state *decl_state;
1224 unsigned from;
1226 gcc_checking_assert (node);
1228 /* Use the function's decl state. */
1229 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1230 gcc_assert (decl_state);
1231 file_data->current_decl_state = decl_state;
1234 /* Set up the struct function. */
1235 from = data_in->reader_cache->nodes.length ();
1236 lto_input_block ib_main (data + main_offset, header->main_size,
1237 file_data->mode_table);
1238 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1240 lto_input_block ib_cfg (data + cfg_offset, header->cfg_size,
1241 file_data->mode_table);
1242 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1244 else
1245 input_constructor (fn_decl, data_in, &ib_main);
1246 data_in->location_cache.apply_location_cache ();
1247 /* And fixup types we streamed locally. */
1249 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1250 unsigned len = cache->nodes.length ();
1251 unsigned i;
1252 for (i = len; i-- > from;)
1254 tree t = streamer_tree_cache_get_tree (cache, i);
1255 if (t == NULL_TREE)
1256 continue;
1258 if (TYPE_P (t))
1260 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1261 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1262 if (TYPE_MAIN_VARIANT (t) != t)
1264 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1265 TYPE_NEXT_VARIANT (t)
1266 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1267 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1273 /* Restore decl state */
1274 file_data->current_decl_state = file_data->global_decl_state;
1277 lto_data_in_delete (data_in);
1281 /* Read the body of NODE using DATA. FILE_DATA holds the global
1282 decls and types. */
1284 void
1285 lto_input_function_body (struct lto_file_decl_data *file_data,
1286 struct cgraph_node *node, const char *data)
1288 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1291 /* Read the body of NODE using DATA. FILE_DATA holds the global
1292 decls and types. */
1294 void
1295 lto_input_variable_constructor (struct lto_file_decl_data *file_data,
1296 struct varpool_node *node, const char *data)
1298 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1302 /* Read the physical representation of a tree node EXPR from
1303 input block IB using the per-file context in DATA_IN. */
1305 static void
1306 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1308 /* Read all the bitfield values in EXPR. Note that for LTO, we
1309 only write language-independent bitfields, so no more unpacking is
1310 needed. */
1311 streamer_read_tree_bitfields (ib, data_in, expr);
1313 /* Read all the pointer fields in EXPR. */
1314 streamer_read_tree_body (ib, data_in, expr);
1316 /* Read any LTO-specific data not read by the tree streamer. */
1317 if (DECL_P (expr)
1318 && TREE_CODE (expr) != FUNCTION_DECL
1319 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1320 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1322 /* We should never try to instantiate an MD or NORMAL builtin here. */
1323 if (TREE_CODE (expr) == FUNCTION_DECL)
1324 gcc_assert (!streamer_handle_as_builtin_p (expr));
1326 #ifdef LTO_STREAMER_DEBUG
1327 /* Remove the mapping to RESULT's original address set by
1328 streamer_alloc_tree. */
1329 lto_orig_address_remove (expr);
1330 #endif
1333 /* Read the physical representation of a tree node with tag TAG from
1334 input block IB using the per-file context in DATA_IN. */
1336 static tree
1337 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1338 enum LTO_tags tag, hashval_t hash)
1340 /* Instantiate a new tree node. */
1341 tree result = streamer_alloc_tree (ib, data_in, tag);
1343 /* Enter RESULT in the reader cache. This will make RESULT
1344 available so that circular references in the rest of the tree
1345 structure can be resolved in subsequent calls to stream_read_tree. */
1346 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1348 lto_read_tree_1 (ib, data_in, result);
1350 /* end_marker = */ streamer_read_uchar (ib);
1352 return result;
1356 /* Populate the reader cache with trees materialized from the SCC
1357 following in the IB, DATA_IN stream. */
1359 hashval_t
1360 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1361 unsigned *len, unsigned *entry_len)
1363 /* A blob of unnamed tree nodes, fill the cache from it and
1364 recurse. */
1365 unsigned size = streamer_read_uhwi (ib);
1366 hashval_t scc_hash = streamer_read_uhwi (ib);
1367 unsigned scc_entry_len = 1;
1369 if (size == 1)
1371 enum LTO_tags tag = streamer_read_record_start (ib);
1372 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1374 else
1376 unsigned int first = data_in->reader_cache->nodes.length ();
1377 tree result;
1379 scc_entry_len = streamer_read_uhwi (ib);
1381 /* Materialize size trees by reading their headers. */
1382 for (unsigned i = 0; i < size; ++i)
1384 enum LTO_tags tag = streamer_read_record_start (ib);
1385 if (tag == LTO_null
1386 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1387 || tag == LTO_tree_pickle_reference
1388 || tag == LTO_builtin_decl
1389 || tag == LTO_integer_cst
1390 || tag == LTO_tree_scc)
1391 gcc_unreachable ();
1393 result = streamer_alloc_tree (ib, data_in, tag);
1394 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1397 /* Read the tree bitpacks and references. */
1398 for (unsigned i = 0; i < size; ++i)
1400 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1401 first + i);
1402 lto_read_tree_1 (ib, data_in, result);
1403 /* end_marker = */ streamer_read_uchar (ib);
1407 *len = size;
1408 *entry_len = scc_entry_len;
1409 return scc_hash;
1413 /* Read a tree from input block IB using the per-file context in
1414 DATA_IN. This context is used, for example, to resolve references
1415 to previously read nodes. */
1417 tree
1418 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1419 enum LTO_tags tag, hashval_t hash)
1421 tree result;
1423 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1425 if (tag == LTO_null)
1426 result = NULL_TREE;
1427 else if (tag >= LTO_field_decl_ref && tag <= LTO_namelist_decl_ref)
1429 /* If TAG is a reference to an indexable tree, the next value
1430 in IB is the index into the table where we expect to find
1431 that tree. */
1432 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1434 else if (tag == LTO_tree_pickle_reference)
1436 /* If TAG is a reference to a previously read tree, look it up in
1437 the reader cache. */
1438 result = streamer_get_pickled_tree (ib, data_in);
1440 else if (tag == LTO_builtin_decl)
1442 /* If we are going to read a built-in function, all we need is
1443 the code and class. */
1444 result = streamer_get_builtin_tree (ib, data_in);
1446 else if (tag == LTO_integer_cst)
1448 /* For shared integer constants in singletons we can use the
1449 existing tree integer constant merging code. */
1450 tree type = stream_read_tree (ib, data_in);
1451 unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
1452 unsigned HOST_WIDE_INT i;
1453 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
1455 for (i = 0; i < len; i++)
1456 a[i] = streamer_read_hwi (ib);
1457 gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
1458 result = wide_int_to_tree (type, wide_int::from_array
1459 (a, len, TYPE_PRECISION (type)));
1460 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1462 else if (tag == LTO_tree_scc)
1463 gcc_unreachable ();
1464 else
1466 /* Otherwise, materialize a new node from IB. */
1467 result = lto_read_tree (ib, data_in, tag, hash);
1470 return result;
1473 tree
1474 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1476 enum LTO_tags tag;
1478 /* Input and skip SCCs. */
1479 while ((tag = streamer_read_record_start (ib)) == LTO_tree_scc)
1481 unsigned len, entry_len;
1482 lto_input_scc (ib, data_in, &len, &entry_len);
1484 return lto_input_tree_1 (ib, data_in, tag, 0);
1488 /* Input toplevel asms. */
1490 void
1491 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1493 size_t len;
1494 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1495 NULL, &len);
1496 const struct lto_simple_header_with_strings *header
1497 = (const struct lto_simple_header_with_strings *) data;
1498 int string_offset;
1499 struct data_in *data_in;
1500 tree str;
1502 if (! data)
1503 return;
1505 string_offset = sizeof (*header) + header->main_size;
1507 lto_input_block ib (data + sizeof (*header), header->main_size,
1508 file_data->mode_table);
1510 data_in = lto_data_in_create (file_data, data + string_offset,
1511 header->string_size, vNULL);
1513 while ((str = streamer_read_string_cst (data_in, &ib)))
1515 asm_node *node = symtab->finalize_toplevel_asm (str);
1516 node->order = streamer_read_hwi (&ib) + order_base;
1517 if (node->order >= symtab->order)
1518 symtab->order = node->order + 1;
1521 lto_data_in_delete (data_in);
1523 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1527 /* Input mode table. */
1529 void
1530 lto_input_mode_table (struct lto_file_decl_data *file_data)
1532 size_t len;
1533 const char *data = lto_get_section_data (file_data, LTO_section_mode_table,
1534 NULL, &len);
1535 if (! data)
1537 internal_error ("cannot read LTO mode table from %s",
1538 file_data->file_name);
1539 return;
1542 unsigned char *table = ggc_cleared_vec_alloc<unsigned char> (1 << 8);
1543 file_data->mode_table = table;
1544 const struct lto_simple_header_with_strings *header
1545 = (const struct lto_simple_header_with_strings *) data;
1546 int string_offset;
1547 struct data_in *data_in;
1548 string_offset = sizeof (*header) + header->main_size;
1550 lto_input_block ib (data + sizeof (*header), header->main_size, NULL);
1551 data_in = lto_data_in_create (file_data, data + string_offset,
1552 header->string_size, vNULL);
1553 bitpack_d bp = streamer_read_bitpack (&ib);
1555 table[VOIDmode] = VOIDmode;
1556 table[BLKmode] = BLKmode;
1557 unsigned int m;
1558 while ((m = bp_unpack_value (&bp, 8)) != VOIDmode)
1560 enum mode_class mclass
1561 = bp_unpack_enum (&bp, mode_class, MAX_MODE_CLASS);
1562 unsigned int size = bp_unpack_value (&bp, 8);
1563 unsigned int prec = bp_unpack_value (&bp, 16);
1564 machine_mode inner = (machine_mode) table[bp_unpack_value (&bp, 8)];
1565 unsigned int nunits = bp_unpack_value (&bp, 8);
1566 unsigned int ibit = 0, fbit = 0;
1567 unsigned int real_fmt_len = 0;
1568 const char *real_fmt_name = NULL;
1569 switch (mclass)
1571 case MODE_FRACT:
1572 case MODE_UFRACT:
1573 case MODE_ACCUM:
1574 case MODE_UACCUM:
1575 ibit = bp_unpack_value (&bp, 8);
1576 fbit = bp_unpack_value (&bp, 8);
1577 break;
1578 case MODE_FLOAT:
1579 case MODE_DECIMAL_FLOAT:
1580 real_fmt_name = bp_unpack_indexed_string (data_in, &bp,
1581 &real_fmt_len);
1582 break;
1583 default:
1584 break;
1586 /* First search just the GET_CLASS_NARROWEST_MODE to wider modes,
1587 if not found, fallback to all modes. */
1588 int pass;
1589 for (pass = 0; pass < 2; pass++)
1590 for (machine_mode mr = pass ? VOIDmode
1591 : GET_CLASS_NARROWEST_MODE (mclass);
1592 pass ? mr < MAX_MACHINE_MODE : mr != VOIDmode;
1593 pass ? mr = (machine_mode) (m + 1)
1594 : mr = GET_MODE_WIDER_MODE (mr))
1595 if (GET_MODE_CLASS (mr) != mclass
1596 || GET_MODE_SIZE (mr) != size
1597 || GET_MODE_PRECISION (mr) != prec
1598 || GET_MODE_INNER (mr) != inner
1599 || GET_MODE_IBIT (mr) != ibit
1600 || GET_MODE_FBIT (mr) != fbit
1601 || GET_MODE_NUNITS (mr) != nunits)
1602 continue;
1603 else if ((mclass == MODE_FLOAT || mclass == MODE_DECIMAL_FLOAT)
1604 && strcmp (REAL_MODE_FORMAT (mr)->name, real_fmt_name) != 0)
1605 continue;
1606 else
1608 table[m] = mr;
1609 pass = 2;
1610 break;
1612 unsigned int mname_len;
1613 const char *mname = bp_unpack_indexed_string (data_in, &bp, &mname_len);
1614 if (pass == 2)
1616 switch (mclass)
1618 case MODE_VECTOR_INT:
1619 case MODE_VECTOR_FLOAT:
1620 case MODE_VECTOR_FRACT:
1621 case MODE_VECTOR_UFRACT:
1622 case MODE_VECTOR_ACCUM:
1623 case MODE_VECTOR_UACCUM:
1624 /* For unsupported vector modes just use BLKmode,
1625 if the scalar mode is supported. */
1626 if (inner != VOIDmode)
1628 table[m] = BLKmode;
1629 break;
1631 /* FALLTHRU */
1632 default:
1633 fatal_error (UNKNOWN_LOCATION, "unsupported mode %s\n", mname);
1634 break;
1638 lto_data_in_delete (data_in);
1640 lto_free_section_data (file_data, LTO_section_mode_table, NULL, data, len);
1644 /* Initialization for the LTO reader. */
1646 void
1647 lto_reader_init (void)
1649 lto_streamer_init ();
1650 file_name_hash_table
1651 = new hash_table<freeing_string_slot_hasher> (37);
1655 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1656 table to use with LEN strings. RESOLUTIONS is the vector of linker
1657 resolutions (NULL if not using a linker plugin). */
1659 struct data_in *
1660 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1661 unsigned len,
1662 vec<ld_plugin_symbol_resolution_t> resolutions)
1664 struct data_in *data_in = new (struct data_in);
1665 data_in->file_data = file_data;
1666 data_in->strings = strings;
1667 data_in->strings_len = len;
1668 data_in->globals_resolution = resolutions;
1669 data_in->reader_cache = streamer_tree_cache_create (false, false, true);
1670 return data_in;
1674 /* Remove DATA_IN. */
1676 void
1677 lto_data_in_delete (struct data_in *data_in)
1679 data_in->globals_resolution.release ();
1680 streamer_tree_cache_delete (data_in->reader_cache);
1681 delete data_in;