This patch syncs zlib.m4 with binutils-gdb and uses AM_ZLIB from zlib.m4
[official-gcc.git] / gcc / lto-streamer-in.c
bloba9e31b1284c7ccd7b3a13541d590836112004312
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 "backend.h"
27 #include "cfghooks.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "rtl.h"
31 #include "ssa.h"
32 #include "toplev.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "expmed.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "calls.h"
41 #include "emit-rtl.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "params.h"
46 #include "internal-fn.h"
47 #include "gimple-iterator.h"
48 #include "tree-cfg.h"
49 #include "tree-into-ssa.h"
50 #include "tree-dfa.h"
51 #include "tree-ssa.h"
52 #include "tree-pass.h"
53 #include "diagnostic.h"
54 #include "except.h"
55 #include "debug.h"
56 #include "cgraph.h"
57 #include "ipa-utils.h"
58 #include "target.h"
59 #include "data-streamer.h"
60 #include "gimple-streamer.h"
61 #include "lto-streamer.h"
62 #include "tree-streamer.h"
63 #include "streamer-hooks.h"
64 #include "cfgloop.h"
67 struct freeing_string_slot_hasher : string_slot_hasher
69 static inline void remove (value_type *);
72 inline void
73 freeing_string_slot_hasher::remove (value_type *v)
75 free (v);
78 /* The table to hold the file names. */
79 static hash_table<freeing_string_slot_hasher> *file_name_hash_table;
82 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
83 number of valid tag values to check. */
85 void
86 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
88 va_list ap;
89 int i;
91 va_start (ap, ntags);
92 for (i = 0; i < ntags; i++)
93 if ((unsigned) actual == va_arg (ap, unsigned))
95 va_end (ap);
96 return;
99 va_end (ap);
100 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
104 /* Read LENGTH bytes from STREAM to ADDR. */
106 void
107 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
109 size_t i;
110 unsigned char *const buffer = (unsigned char *const) addr;
112 for (i = 0; i < length; i++)
113 buffer[i] = streamer_read_uchar (ib);
117 /* Lookup STRING in file_name_hash_table. If found, return the existing
118 string, otherwise insert STRING as the canonical version. */
120 static const char *
121 canon_file_name (const char *string)
123 string_slot **slot;
124 struct string_slot s_slot;
125 size_t len = strlen (string);
127 s_slot.s = string;
128 s_slot.len = len;
130 slot = file_name_hash_table->find_slot (&s_slot, INSERT);
131 if (*slot == NULL)
133 char *saved_string;
134 struct string_slot *new_slot;
136 saved_string = (char *) xmalloc (len + 1);
137 new_slot = XCNEW (struct string_slot);
138 memcpy (saved_string, string, len + 1);
139 new_slot->s = saved_string;
140 new_slot->len = len;
141 *slot = new_slot;
142 return saved_string;
144 else
146 struct string_slot *old_slot = *slot;
147 return old_slot->s;
151 /* Pointer to currently alive instance of lto_location_cache. */
153 lto_location_cache *lto_location_cache::current_cache;
155 /* Sort locations in source order. Start with file from last application. */
158 lto_location_cache::cmp_loc (const void *pa, const void *pb)
160 const cached_location *a = ((const cached_location *)pa);
161 const cached_location *b = ((const cached_location *)pb);
162 const char *current_file = current_cache->current_file;
163 int current_line = current_cache->current_line;
165 if (a->file == current_file && b->file != current_file)
166 return -1;
167 if (a->file != current_file && b->file == current_file)
168 return 1;
169 if (a->file == current_file && b->file == current_file)
171 if (a->line == current_line && b->line != current_line)
172 return -1;
173 if (a->line != current_line && b->line == current_line)
174 return 1;
176 if (a->file != b->file)
177 return strcmp (a->file, b->file);
178 if (a->line != b->line)
179 return a->line - b->line;
180 return a->col - b->col;
183 /* Apply all changes in location cache. Add locations into linemap and patch
184 trees. */
186 bool
187 lto_location_cache::apply_location_cache ()
189 static const char *prev_file;
190 if (!loc_cache.length ())
191 return false;
192 if (loc_cache.length () > 1)
193 loc_cache.qsort (cmp_loc);
195 for (unsigned int i = 0; i < loc_cache.length (); i++)
197 struct cached_location loc = loc_cache[i];
199 if (current_file != loc.file)
200 linemap_add (line_table, prev_file ? LC_RENAME : LC_ENTER,
201 false, loc.file, loc.line);
202 else if (current_line != loc.line)
204 int max = loc.col;
206 for (unsigned int j = i + 1; j < loc_cache.length (); j++)
207 if (loc.file != loc_cache[j].file
208 || loc.line != loc_cache[j].line)
209 break;
210 else if (max < loc_cache[j].col)
211 max = loc_cache[j].col;
212 linemap_line_start (line_table, loc.line, max + 1);
214 gcc_assert (*loc.loc == BUILTINS_LOCATION + 1);
215 if (current_file == loc.file && current_line == loc.line
216 && current_col == loc.col)
217 *loc.loc = current_loc;
218 else
219 current_loc = *loc.loc = linemap_position_for_column (line_table,
220 loc.col);
221 current_line = loc.line;
222 prev_file = current_file = loc.file;
223 current_col = loc.col;
225 loc_cache.truncate (0);
226 accepted_length = 0;
227 return true;
230 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
232 void
233 lto_location_cache::accept_location_cache ()
235 gcc_assert (current_cache == this);
236 accepted_length = loc_cache.length ();
239 /* Tree merging did suceed; throw away recent changes. */
241 void
242 lto_location_cache::revert_location_cache ()
244 loc_cache.truncate (accepted_length);
247 /* Read a location bitpack from input block IB and either update *LOC directly
248 or add it to the location cache.
249 It is neccesary to call apply_location_cache to get *LOC updated. */
251 void
252 lto_location_cache::input_location (location_t *loc, struct bitpack_d *bp,
253 struct data_in *data_in)
255 static const char *stream_file;
256 static int stream_line;
257 static int stream_col;
258 bool file_change, line_change, column_change;
260 gcc_assert (current_cache == this);
262 *loc = bp_unpack_int_in_range (bp, "location", 0, RESERVED_LOCATION_COUNT);
264 if (*loc < RESERVED_LOCATION_COUNT)
265 return;
267 /* Keep value RESERVED_LOCATION_COUNT in *loc as linemap lookups will
268 ICE on it. */
270 file_change = bp_unpack_value (bp, 1);
271 line_change = bp_unpack_value (bp, 1);
272 column_change = bp_unpack_value (bp, 1);
274 if (file_change)
275 stream_file = canon_file_name (bp_unpack_string (data_in, bp));
277 if (line_change)
278 stream_line = bp_unpack_var_len_unsigned (bp);
280 if (column_change)
281 stream_col = bp_unpack_var_len_unsigned (bp);
283 /* This optimization saves location cache operations druing gimple
284 streaming. */
286 if (current_file == stream_file && current_line == stream_line
287 && current_col == stream_col)
289 *loc = current_loc;
290 return;
293 struct cached_location entry = {stream_file, loc, stream_line, stream_col};
294 loc_cache.safe_push (entry);
297 /* Read a location bitpack from input block IB and either update *LOC directly
298 or add it to the location cache.
299 It is neccesary to call apply_location_cache to get *LOC updated. */
301 void
302 lto_input_location (location_t *loc, struct bitpack_d *bp,
303 struct data_in *data_in)
305 data_in->location_cache.input_location (loc, bp, data_in);
308 /* Read location and return it instead of going through location caching.
309 This should be used only when the resulting location is not going to be
310 discarded. */
312 location_t
313 stream_input_location_now (struct bitpack_d *bp, struct data_in *data_in)
315 location_t loc;
316 stream_input_location (&loc, bp, data_in);
317 data_in->location_cache.apply_location_cache ();
318 return loc;
321 /* Read a reference to a tree node from DATA_IN using input block IB.
322 TAG is the expected node that should be found in IB, if TAG belongs
323 to one of the indexable trees, expect to read a reference index to
324 be looked up in one of the symbol tables, otherwise read the pysical
325 representation of the tree using stream_read_tree. FN is the
326 function scope for the read tree. */
328 tree
329 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
330 struct function *fn, enum LTO_tags tag)
332 unsigned HOST_WIDE_INT ix_u;
333 tree result = NULL_TREE;
335 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_namelist_decl_ref);
337 switch (tag)
339 case LTO_type_ref:
340 ix_u = streamer_read_uhwi (ib);
341 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
342 break;
344 case LTO_ssa_name_ref:
345 ix_u = streamer_read_uhwi (ib);
346 result = (*SSANAMES (fn))[ix_u];
347 break;
349 case LTO_field_decl_ref:
350 ix_u = streamer_read_uhwi (ib);
351 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
352 break;
354 case LTO_function_decl_ref:
355 ix_u = streamer_read_uhwi (ib);
356 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
357 break;
359 case LTO_type_decl_ref:
360 ix_u = streamer_read_uhwi (ib);
361 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
362 break;
364 case LTO_namespace_decl_ref:
365 ix_u = streamer_read_uhwi (ib);
366 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
367 break;
369 case LTO_global_decl_ref:
370 case LTO_result_decl_ref:
371 case LTO_const_decl_ref:
372 case LTO_imported_decl_ref:
373 case LTO_label_decl_ref:
374 case LTO_translation_unit_decl_ref:
375 case LTO_namelist_decl_ref:
376 ix_u = streamer_read_uhwi (ib);
377 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
378 break;
380 default:
381 gcc_unreachable ();
384 gcc_assert (result);
386 return result;
390 /* Read and return a double-linked list of catch handlers from input
391 block IB, using descriptors in DATA_IN. */
393 static struct eh_catch_d *
394 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
395 eh_catch *last_p)
397 eh_catch first;
398 enum LTO_tags tag;
400 *last_p = first = NULL;
401 tag = streamer_read_record_start (ib);
402 while (tag)
404 tree list;
405 eh_catch n;
407 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
409 /* Read the catch node. */
410 n = ggc_cleared_alloc<eh_catch_d> ();
411 n->type_list = stream_read_tree (ib, data_in);
412 n->filter_list = stream_read_tree (ib, data_in);
413 n->label = stream_read_tree (ib, data_in);
415 /* Register all the types in N->FILTER_LIST. */
416 for (list = n->filter_list; list; list = TREE_CHAIN (list))
417 add_type_for_runtime (TREE_VALUE (list));
419 /* Chain N to the end of the list. */
420 if (*last_p)
421 (*last_p)->next_catch = n;
422 n->prev_catch = *last_p;
423 *last_p = n;
425 /* Set the head of the list the first time through the loop. */
426 if (first == NULL)
427 first = n;
429 tag = streamer_read_record_start (ib);
432 return first;
436 /* Read and return EH region IX from input block IB, using descriptors
437 in DATA_IN. */
439 static eh_region
440 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
442 enum LTO_tags tag;
443 eh_region r;
445 /* Read the region header. */
446 tag = streamer_read_record_start (ib);
447 if (tag == LTO_null)
448 return NULL;
450 r = ggc_cleared_alloc<eh_region_d> ();
451 r->index = streamer_read_hwi (ib);
453 gcc_assert (r->index == ix);
455 /* Read all the region pointers as region numbers. We'll fix up
456 the pointers once the whole array has been read. */
457 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
458 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
459 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
461 switch (tag)
463 case LTO_ert_cleanup:
464 r->type = ERT_CLEANUP;
465 break;
467 case LTO_ert_try:
469 struct eh_catch_d *last_catch;
470 r->type = ERT_TRY;
471 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
472 &last_catch);
473 r->u.eh_try.last_catch = last_catch;
474 break;
477 case LTO_ert_allowed_exceptions:
479 tree l;
481 r->type = ERT_ALLOWED_EXCEPTIONS;
482 r->u.allowed.type_list = stream_read_tree (ib, data_in);
483 r->u.allowed.label = stream_read_tree (ib, data_in);
484 r->u.allowed.filter = streamer_read_uhwi (ib);
486 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
487 add_type_for_runtime (TREE_VALUE (l));
489 break;
491 case LTO_ert_must_not_throw:
493 r->type = ERT_MUST_NOT_THROW;
494 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
495 bitpack_d bp = streamer_read_bitpack (ib);
496 r->u.must_not_throw.failure_loc
497 = stream_input_location_now (&bp, data_in);
499 break;
501 default:
502 gcc_unreachable ();
505 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
507 return r;
511 /* Read and return EH landing pad IX from input block IB, using descriptors
512 in DATA_IN. */
514 static eh_landing_pad
515 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
517 enum LTO_tags tag;
518 eh_landing_pad lp;
520 /* Read the landing pad header. */
521 tag = streamer_read_record_start (ib);
522 if (tag == LTO_null)
523 return NULL;
525 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
527 lp = ggc_cleared_alloc<eh_landing_pad_d> ();
528 lp->index = streamer_read_hwi (ib);
529 gcc_assert (lp->index == ix);
530 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
531 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
532 lp->post_landing_pad = stream_read_tree (ib, data_in);
534 return lp;
538 /* After reading the EH regions, pointers to peer and children regions
539 are region numbers. This converts all these region numbers into
540 real pointers into the rematerialized regions for FN. ROOT_REGION
541 is the region number for the root EH region in FN. */
543 static void
544 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
546 unsigned i;
547 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
548 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
549 eh_region r;
550 eh_landing_pad lp;
552 gcc_assert (eh_array && lp_array);
554 gcc_assert (root_region >= 0);
555 fn->eh->region_tree = (*eh_array)[root_region];
557 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
558 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
560 /* Convert all the index numbers stored in pointer fields into
561 pointers to the corresponding slots in the EH region array. */
562 FOR_EACH_VEC_ELT (*eh_array, i, r)
564 /* The array may contain NULL regions. */
565 if (r == NULL)
566 continue;
568 gcc_assert (i == (unsigned) r->index);
569 FIXUP_EH_REGION (r->outer);
570 FIXUP_EH_REGION (r->inner);
571 FIXUP_EH_REGION (r->next_peer);
572 FIXUP_EH_LP (r->landing_pads);
575 /* Convert all the index numbers stored in pointer fields into
576 pointers to the corresponding slots in the EH landing pad array. */
577 FOR_EACH_VEC_ELT (*lp_array, i, lp)
579 /* The array may contain NULL landing pads. */
580 if (lp == NULL)
581 continue;
583 gcc_assert (i == (unsigned) lp->index);
584 FIXUP_EH_LP (lp->next_lp);
585 FIXUP_EH_REGION (lp->region);
588 #undef FIXUP_EH_REGION
589 #undef FIXUP_EH_LP
593 /* Initialize EH support. */
595 void
596 lto_init_eh (void)
598 static bool eh_initialized_p = false;
600 if (eh_initialized_p)
601 return;
603 /* Contrary to most other FEs, we only initialize EH support when at
604 least one of the files in the set contains exception regions in
605 it. Since this happens much later than the call to init_eh in
606 lang_dependent_init, we have to set flag_exceptions and call
607 init_eh again to initialize the EH tables. */
608 flag_exceptions = 1;
609 init_eh ();
611 eh_initialized_p = true;
615 /* Read the exception table for FN from IB using the data descriptors
616 in DATA_IN. */
618 static void
619 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
620 struct function *fn)
622 HOST_WIDE_INT i, root_region, len;
623 enum LTO_tags tag;
625 tag = streamer_read_record_start (ib);
626 if (tag == LTO_null)
627 return;
629 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
631 /* If the file contains EH regions, then it was compiled with
632 -fexceptions. In that case, initialize the backend EH
633 machinery. */
634 lto_init_eh ();
636 gcc_assert (fn->eh);
638 root_region = streamer_read_hwi (ib);
639 gcc_assert (root_region == (int) root_region);
641 /* Read the EH region array. */
642 len = streamer_read_hwi (ib);
643 gcc_assert (len == (int) len);
644 if (len > 0)
646 vec_safe_grow_cleared (fn->eh->region_array, len);
647 for (i = 0; i < len; i++)
649 eh_region r = input_eh_region (ib, data_in, i);
650 (*fn->eh->region_array)[i] = r;
654 /* Read the landing pads. */
655 len = streamer_read_hwi (ib);
656 gcc_assert (len == (int) len);
657 if (len > 0)
659 vec_safe_grow_cleared (fn->eh->lp_array, len);
660 for (i = 0; i < len; i++)
662 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
663 (*fn->eh->lp_array)[i] = lp;
667 /* Read the runtime type data. */
668 len = streamer_read_hwi (ib);
669 gcc_assert (len == (int) len);
670 if (len > 0)
672 vec_safe_grow_cleared (fn->eh->ttype_data, len);
673 for (i = 0; i < len; i++)
675 tree ttype = stream_read_tree (ib, data_in);
676 (*fn->eh->ttype_data)[i] = ttype;
680 /* Read the table of action chains. */
681 len = streamer_read_hwi (ib);
682 gcc_assert (len == (int) len);
683 if (len > 0)
685 if (targetm.arm_eabi_unwinder)
687 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
688 for (i = 0; i < len; i++)
690 tree t = stream_read_tree (ib, data_in);
691 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
694 else
696 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
697 for (i = 0; i < len; i++)
699 uchar c = streamer_read_uchar (ib);
700 (*fn->eh->ehspec_data.other)[i] = c;
705 /* Reconstruct the EH region tree by fixing up the peer/children
706 pointers. */
707 fixup_eh_region_pointers (fn, root_region);
709 tag = streamer_read_record_start (ib);
710 lto_tag_check_range (tag, LTO_null, LTO_null);
714 /* Make a new basic block with index INDEX in function FN. */
716 static basic_block
717 make_new_block (struct function *fn, unsigned int index)
719 basic_block bb = alloc_block ();
720 bb->index = index;
721 SET_BASIC_BLOCK_FOR_FN (fn, index, bb);
722 n_basic_blocks_for_fn (fn)++;
723 return bb;
727 /* Read a wide-int. */
729 static widest_int
730 streamer_read_wi (struct lto_input_block *ib)
732 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
733 int i;
734 int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
735 int len = streamer_read_uhwi (ib);
736 for (i = 0; i < len; i++)
737 a[i] = streamer_read_hwi (ib);
738 return widest_int::from_array (a, len);
742 /* Read the CFG for function FN from input block IB. */
744 static void
745 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
746 struct function *fn,
747 int count_materialization_scale)
749 unsigned int bb_count;
750 basic_block p_bb;
751 unsigned int i;
752 int index;
754 init_empty_tree_cfg_for_function (fn);
755 init_ssa_operands (fn);
757 profile_status_for_fn (fn) = streamer_read_enum (ib, profile_status_d,
758 PROFILE_LAST);
760 bb_count = streamer_read_uhwi (ib);
762 last_basic_block_for_fn (fn) = bb_count;
763 if (bb_count > basic_block_info_for_fn (fn)->length ())
764 vec_safe_grow_cleared (basic_block_info_for_fn (fn), bb_count);
766 if (bb_count > label_to_block_map_for_fn (fn)->length ())
767 vec_safe_grow_cleared (label_to_block_map_for_fn (fn), bb_count);
769 index = streamer_read_hwi (ib);
770 while (index != -1)
772 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
773 unsigned int edge_count;
775 if (bb == NULL)
776 bb = make_new_block (fn, index);
778 edge_count = streamer_read_uhwi (ib);
780 /* Connect up the CFG. */
781 for (i = 0; i < edge_count; i++)
783 unsigned int dest_index;
784 unsigned int edge_flags;
785 basic_block dest;
786 int probability;
787 gcov_type count;
788 edge e;
790 dest_index = streamer_read_uhwi (ib);
791 probability = (int) streamer_read_hwi (ib);
792 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
793 count_materialization_scale);
794 edge_flags = streamer_read_uhwi (ib);
796 dest = BASIC_BLOCK_FOR_FN (fn, dest_index);
798 if (dest == NULL)
799 dest = make_new_block (fn, dest_index);
801 e = make_edge (bb, dest, edge_flags);
802 e->probability = probability;
803 e->count = count;
806 index = streamer_read_hwi (ib);
809 p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
810 index = streamer_read_hwi (ib);
811 while (index != -1)
813 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
814 bb->prev_bb = p_bb;
815 p_bb->next_bb = bb;
816 p_bb = bb;
817 index = streamer_read_hwi (ib);
820 /* ??? The cfgloop interface is tied to cfun. */
821 gcc_assert (cfun == fn);
823 /* Input the loop tree. */
824 unsigned n_loops = streamer_read_uhwi (ib);
825 if (n_loops == 0)
826 return;
828 struct loops *loops = ggc_cleared_alloc<struct loops> ();
829 init_loops_structure (fn, loops, n_loops);
830 set_loops_for_fn (fn, loops);
832 /* Input each loop and associate it with its loop header so
833 flow_loops_find can rebuild the loop tree. */
834 for (unsigned i = 1; i < n_loops; ++i)
836 int header_index = streamer_read_hwi (ib);
837 if (header_index == -1)
839 loops->larray->quick_push (NULL);
840 continue;
843 struct loop *loop = alloc_loop ();
844 loop->header = BASIC_BLOCK_FOR_FN (fn, header_index);
845 loop->header->loop_father = loop;
847 /* Read everything copy_loop_info copies. */
848 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
849 loop->any_upper_bound = streamer_read_hwi (ib);
850 if (loop->any_upper_bound)
851 loop->nb_iterations_upper_bound = streamer_read_wi (ib);
852 loop->any_estimate = streamer_read_hwi (ib);
853 if (loop->any_estimate)
854 loop->nb_iterations_estimate = streamer_read_wi (ib);
856 /* Read OMP SIMD related info. */
857 loop->safelen = streamer_read_hwi (ib);
858 loop->dont_vectorize = streamer_read_hwi (ib);
859 loop->force_vectorize = streamer_read_hwi (ib);
860 loop->simduid = stream_read_tree (ib, data_in);
862 place_new_loop (fn, loop);
864 /* flow_loops_find doesn't like loops not in the tree, hook them
865 all as siblings of the tree root temporarily. */
866 flow_loop_tree_node_add (loops->tree_root, loop);
869 /* Rebuild the loop tree. */
870 flow_loops_find (loops);
874 /* Read the SSA names array for function FN from DATA_IN using input
875 block IB. */
877 static void
878 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
879 struct function *fn)
881 unsigned int i, size;
883 size = streamer_read_uhwi (ib);
884 init_ssanames (fn, size);
886 i = streamer_read_uhwi (ib);
887 while (i)
889 tree ssa_name, name;
890 bool is_default_def;
892 /* Skip over the elements that had been freed. */
893 while (SSANAMES (fn)->length () < i)
894 SSANAMES (fn)->quick_push (NULL_TREE);
896 is_default_def = (streamer_read_uchar (ib) != 0);
897 name = stream_read_tree (ib, data_in);
898 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
900 if (is_default_def)
901 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
903 i = streamer_read_uhwi (ib);
908 /* Go through all NODE edges and fixup call_stmt pointers
909 so they point to STMTS. */
911 static void
912 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
913 struct function *fn)
915 struct cgraph_edge *cedge;
916 struct ipa_ref *ref = NULL;
917 unsigned int i;
919 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
921 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
922 fatal_error (input_location,
923 "Cgraph edge statement index out of range");
924 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
925 if (!cedge->call_stmt)
926 fatal_error (input_location,
927 "Cgraph edge statement index not found");
929 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
931 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
932 fatal_error (input_location,
933 "Cgraph edge statement index out of range");
934 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
935 if (!cedge->call_stmt)
936 fatal_error (input_location, "Cgraph edge statement index not found");
938 for (i = 0; node->iterate_reference (i, ref); i++)
939 if (ref->lto_stmt_uid)
941 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
942 fatal_error (input_location,
943 "Reference statement index out of range");
944 ref->stmt = stmts[ref->lto_stmt_uid - 1];
945 if (!ref->stmt)
946 fatal_error (input_location, "Reference statement index not found");
951 /* Fixup call_stmt pointers in NODE and all clones. */
953 static void
954 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
956 struct cgraph_node *node;
957 struct function *fn;
959 while (orig->clone_of)
960 orig = orig->clone_of;
961 fn = DECL_STRUCT_FUNCTION (orig->decl);
963 fixup_call_stmt_edges_1 (orig, stmts, fn);
964 if (orig->clones)
965 for (node = orig->clones; node != orig;)
967 fixup_call_stmt_edges_1 (node, stmts, fn);
968 if (node->clones)
969 node = node->clones;
970 else if (node->next_sibling_clone)
971 node = node->next_sibling_clone;
972 else
974 while (node != orig && !node->next_sibling_clone)
975 node = node->clone_of;
976 if (node != orig)
977 node = node->next_sibling_clone;
983 /* Input the base body of struct function FN from DATA_IN
984 using input block IB. */
986 static void
987 input_struct_function_base (struct function *fn, struct data_in *data_in,
988 struct lto_input_block *ib)
990 struct bitpack_d bp;
991 int len;
993 /* Read the static chain and non-local goto save area. */
994 fn->static_chain_decl = stream_read_tree (ib, data_in);
995 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
997 /* Read all the local symbols. */
998 len = streamer_read_hwi (ib);
999 if (len > 0)
1001 int i;
1002 vec_safe_grow_cleared (fn->local_decls, len);
1003 for (i = 0; i < len; i++)
1005 tree t = stream_read_tree (ib, data_in);
1006 (*fn->local_decls)[i] = t;
1010 /* Input the current IL state of the function. */
1011 fn->curr_properties = streamer_read_uhwi (ib);
1013 /* Read all the attributes for FN. */
1014 bp = streamer_read_bitpack (ib);
1015 fn->is_thunk = bp_unpack_value (&bp, 1);
1016 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
1017 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
1018 fn->returns_struct = bp_unpack_value (&bp, 1);
1019 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
1020 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
1021 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
1022 fn->after_inlining = bp_unpack_value (&bp, 1);
1023 fn->stdarg = bp_unpack_value (&bp, 1);
1024 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
1025 fn->calls_alloca = bp_unpack_value (&bp, 1);
1026 fn->calls_setjmp = bp_unpack_value (&bp, 1);
1027 fn->has_force_vectorize_loops = bp_unpack_value (&bp, 1);
1028 fn->has_simduid_loops = bp_unpack_value (&bp, 1);
1029 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
1030 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
1031 fn->last_clique = bp_unpack_value (&bp, sizeof (short) * 8);
1033 /* Input the function start and end loci. */
1034 fn->function_start_locus = stream_input_location_now (&bp, data_in);
1035 fn->function_end_locus = stream_input_location_now (&bp, data_in);
1039 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1041 static void
1042 input_function (tree fn_decl, struct data_in *data_in,
1043 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
1045 struct function *fn;
1046 enum LTO_tags tag;
1047 gimple *stmts;
1048 basic_block bb;
1049 struct cgraph_node *node;
1051 tag = streamer_read_record_start (ib);
1052 lto_tag_check (tag, LTO_function);
1054 /* Read decls for parameters and args. */
1055 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
1056 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
1058 /* Read the tree of lexical scopes for the function. */
1059 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
1061 if (!streamer_read_uhwi (ib))
1062 return;
1064 push_struct_function (fn_decl);
1065 fn = DECL_STRUCT_FUNCTION (fn_decl);
1066 init_tree_ssa (fn);
1067 /* We input IL in SSA form. */
1068 cfun->gimple_df->in_ssa_p = true;
1070 gimple_register_cfg_hooks ();
1072 node = cgraph_node::get (fn_decl);
1073 if (!node)
1074 node = cgraph_node::create (fn_decl);
1075 input_struct_function_base (fn, data_in, ib);
1076 input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
1078 /* Read all the SSA names. */
1079 input_ssa_names (ib, data_in, fn);
1081 /* Read the exception handling regions in the function. */
1082 input_eh_regions (ib, data_in, fn);
1084 gcc_assert (DECL_INITIAL (fn_decl));
1085 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
1087 /* Read all the basic blocks. */
1088 tag = streamer_read_record_start (ib);
1089 while (tag)
1091 input_bb (ib, tag, data_in, fn,
1092 node->count_materialization_scale);
1093 tag = streamer_read_record_start (ib);
1096 /* Fix up the call statements that are mentioned in the callgraph
1097 edges. */
1098 set_gimple_stmt_max_uid (cfun, 0);
1099 FOR_ALL_BB_FN (bb, cfun)
1101 gimple_stmt_iterator gsi;
1102 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1104 gimple stmt = gsi_stmt (gsi);
1105 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1107 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1109 gimple stmt = gsi_stmt (gsi);
1110 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1113 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1114 FOR_ALL_BB_FN (bb, cfun)
1116 gimple_stmt_iterator bsi = gsi_start_phis (bb);
1117 while (!gsi_end_p (bsi))
1119 gimple stmt = gsi_stmt (bsi);
1120 gsi_next (&bsi);
1121 stmts[gimple_uid (stmt)] = stmt;
1123 bsi = gsi_start_bb (bb);
1124 while (!gsi_end_p (bsi))
1126 gimple stmt = gsi_stmt (bsi);
1127 /* If we're recompiling LTO objects with debug stmts but
1128 we're not supposed to have debug stmts, remove them now.
1129 We can't remove them earlier because this would cause uid
1130 mismatches in fixups, but we can do it at this point, as
1131 long as debug stmts don't require fixups. */
1132 if (!MAY_HAVE_DEBUG_STMTS && !flag_wpa && is_gimple_debug (stmt))
1134 gimple_stmt_iterator gsi = bsi;
1135 gsi_next (&bsi);
1136 gsi_remove (&gsi, true);
1138 else
1140 gsi_next (&bsi);
1141 stmts[gimple_uid (stmt)] = stmt;
1146 /* Set the gimple body to the statement sequence in the entry
1147 basic block. FIXME lto, this is fairly hacky. The existence
1148 of a gimple body is used by the cgraph routines, but we should
1149 really use the presence of the CFG. */
1151 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1152 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1155 fixup_call_stmt_edges (node, stmts);
1156 execute_all_ipa_stmt_fixups (node, stmts);
1158 update_ssa (TODO_update_ssa_only_virtuals);
1159 free_dominance_info (CDI_DOMINATORS);
1160 free_dominance_info (CDI_POST_DOMINATORS);
1161 free (stmts);
1162 pop_cfun ();
1165 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1167 static void
1168 input_constructor (tree var, struct data_in *data_in,
1169 struct lto_input_block *ib)
1171 DECL_INITIAL (var) = stream_read_tree (ib, data_in);
1175 /* Read the body from DATA for function NODE and fill it in.
1176 FILE_DATA are the global decls and types. SECTION_TYPE is either
1177 LTO_section_function_body or LTO_section_static_initializer. If
1178 section type is LTO_section_function_body, FN must be the decl for
1179 that function. */
1181 static void
1182 lto_read_body_or_constructor (struct lto_file_decl_data *file_data, struct symtab_node *node,
1183 const char *data, enum lto_section_type section_type)
1185 const struct lto_function_header *header;
1186 struct data_in *data_in;
1187 int cfg_offset;
1188 int main_offset;
1189 int string_offset;
1190 tree fn_decl = node->decl;
1192 header = (const struct lto_function_header *) data;
1193 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1195 cfg_offset = sizeof (struct lto_function_header);
1196 main_offset = cfg_offset + header->cfg_size;
1197 string_offset = main_offset + header->main_size;
1199 else
1201 main_offset = sizeof (struct lto_function_header);
1202 string_offset = main_offset + header->main_size;
1205 data_in = lto_data_in_create (file_data, data + string_offset,
1206 header->string_size, vNULL);
1208 if (section_type == LTO_section_function_body)
1210 struct lto_in_decl_state *decl_state;
1211 unsigned from;
1213 gcc_checking_assert (node);
1215 /* Use the function's decl state. */
1216 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1217 gcc_assert (decl_state);
1218 file_data->current_decl_state = decl_state;
1221 /* Set up the struct function. */
1222 from = data_in->reader_cache->nodes.length ();
1223 lto_input_block ib_main (data + main_offset, header->main_size,
1224 file_data->mode_table);
1225 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1227 lto_input_block ib_cfg (data + cfg_offset, header->cfg_size,
1228 file_data->mode_table);
1229 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1231 else
1232 input_constructor (fn_decl, data_in, &ib_main);
1233 data_in->location_cache.apply_location_cache ();
1234 /* And fixup types we streamed locally. */
1236 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1237 unsigned len = cache->nodes.length ();
1238 unsigned i;
1239 for (i = len; i-- > from;)
1241 tree t = streamer_tree_cache_get_tree (cache, i);
1242 if (t == NULL_TREE)
1243 continue;
1245 if (TYPE_P (t))
1247 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1248 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1249 if (TYPE_MAIN_VARIANT (t) != t)
1251 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1252 TYPE_NEXT_VARIANT (t)
1253 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1254 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1260 /* Restore decl state */
1261 file_data->current_decl_state = file_data->global_decl_state;
1264 lto_data_in_delete (data_in);
1268 /* Read the body of NODE using DATA. FILE_DATA holds the global
1269 decls and types. */
1271 void
1272 lto_input_function_body (struct lto_file_decl_data *file_data,
1273 struct cgraph_node *node, const char *data)
1275 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1278 /* Read the body of NODE using DATA. FILE_DATA holds the global
1279 decls and types. */
1281 void
1282 lto_input_variable_constructor (struct lto_file_decl_data *file_data,
1283 struct varpool_node *node, const char *data)
1285 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1289 /* Read the physical representation of a tree node EXPR from
1290 input block IB using the per-file context in DATA_IN. */
1292 static void
1293 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1295 /* Read all the bitfield values in EXPR. Note that for LTO, we
1296 only write language-independent bitfields, so no more unpacking is
1297 needed. */
1298 streamer_read_tree_bitfields (ib, data_in, expr);
1300 /* Read all the pointer fields in EXPR. */
1301 streamer_read_tree_body (ib, data_in, expr);
1303 /* Read any LTO-specific data not read by the tree streamer. */
1304 if (DECL_P (expr)
1305 && TREE_CODE (expr) != FUNCTION_DECL
1306 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1307 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1309 /* We should never try to instantiate an MD or NORMAL builtin here. */
1310 if (TREE_CODE (expr) == FUNCTION_DECL)
1311 gcc_assert (!streamer_handle_as_builtin_p (expr));
1313 #ifdef LTO_STREAMER_DEBUG
1314 /* Remove the mapping to RESULT's original address set by
1315 streamer_alloc_tree. */
1316 lto_orig_address_remove (expr);
1317 #endif
1320 /* Read the physical representation of a tree node with tag TAG from
1321 input block IB using the per-file context in DATA_IN. */
1323 static tree
1324 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1325 enum LTO_tags tag, hashval_t hash)
1327 /* Instantiate a new tree node. */
1328 tree result = streamer_alloc_tree (ib, data_in, tag);
1330 /* Enter RESULT in the reader cache. This will make RESULT
1331 available so that circular references in the rest of the tree
1332 structure can be resolved in subsequent calls to stream_read_tree. */
1333 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1335 lto_read_tree_1 (ib, data_in, result);
1337 /* end_marker = */ streamer_read_uchar (ib);
1339 return result;
1343 /* Populate the reader cache with trees materialized from the SCC
1344 following in the IB, DATA_IN stream. */
1346 hashval_t
1347 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1348 unsigned *len, unsigned *entry_len)
1350 /* A blob of unnamed tree nodes, fill the cache from it and
1351 recurse. */
1352 unsigned size = streamer_read_uhwi (ib);
1353 hashval_t scc_hash = streamer_read_uhwi (ib);
1354 unsigned scc_entry_len = 1;
1356 if (size == 1)
1358 enum LTO_tags tag = streamer_read_record_start (ib);
1359 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1361 else
1363 unsigned int first = data_in->reader_cache->nodes.length ();
1364 tree result;
1366 scc_entry_len = streamer_read_uhwi (ib);
1368 /* Materialize size trees by reading their headers. */
1369 for (unsigned i = 0; i < size; ++i)
1371 enum LTO_tags tag = streamer_read_record_start (ib);
1372 if (tag == LTO_null
1373 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1374 || tag == LTO_tree_pickle_reference
1375 || tag == LTO_builtin_decl
1376 || tag == LTO_integer_cst
1377 || tag == LTO_tree_scc)
1378 gcc_unreachable ();
1380 result = streamer_alloc_tree (ib, data_in, tag);
1381 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1384 /* Read the tree bitpacks and references. */
1385 for (unsigned i = 0; i < size; ++i)
1387 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1388 first + i);
1389 lto_read_tree_1 (ib, data_in, result);
1390 /* end_marker = */ streamer_read_uchar (ib);
1394 *len = size;
1395 *entry_len = scc_entry_len;
1396 return scc_hash;
1400 /* Read a tree from input block IB using the per-file context in
1401 DATA_IN. This context is used, for example, to resolve references
1402 to previously read nodes. */
1404 tree
1405 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1406 enum LTO_tags tag, hashval_t hash)
1408 tree result;
1410 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1412 if (tag == LTO_null)
1413 result = NULL_TREE;
1414 else if (tag >= LTO_field_decl_ref && tag <= LTO_namelist_decl_ref)
1416 /* If TAG is a reference to an indexable tree, the next value
1417 in IB is the index into the table where we expect to find
1418 that tree. */
1419 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1421 else if (tag == LTO_tree_pickle_reference)
1423 /* If TAG is a reference to a previously read tree, look it up in
1424 the reader cache. */
1425 result = streamer_get_pickled_tree (ib, data_in);
1427 else if (tag == LTO_builtin_decl)
1429 /* If we are going to read a built-in function, all we need is
1430 the code and class. */
1431 result = streamer_get_builtin_tree (ib, data_in);
1433 else if (tag == LTO_integer_cst)
1435 /* For shared integer constants in singletons we can use the
1436 existing tree integer constant merging code. */
1437 tree type = stream_read_tree (ib, data_in);
1438 unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
1439 unsigned HOST_WIDE_INT i;
1440 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
1442 for (i = 0; i < len; i++)
1443 a[i] = streamer_read_hwi (ib);
1444 gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
1445 result = wide_int_to_tree (type, wide_int::from_array
1446 (a, len, TYPE_PRECISION (type)));
1447 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1449 else if (tag == LTO_tree_scc)
1450 gcc_unreachable ();
1451 else
1453 /* Otherwise, materialize a new node from IB. */
1454 result = lto_read_tree (ib, data_in, tag, hash);
1457 return result;
1460 tree
1461 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1463 enum LTO_tags tag;
1465 /* Input and skip SCCs. */
1466 while ((tag = streamer_read_record_start (ib)) == LTO_tree_scc)
1468 unsigned len, entry_len;
1469 lto_input_scc (ib, data_in, &len, &entry_len);
1471 return lto_input_tree_1 (ib, data_in, tag, 0);
1475 /* Input toplevel asms. */
1477 void
1478 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1480 size_t len;
1481 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1482 NULL, &len);
1483 const struct lto_simple_header_with_strings *header
1484 = (const struct lto_simple_header_with_strings *) data;
1485 int string_offset;
1486 struct data_in *data_in;
1487 tree str;
1489 if (! data)
1490 return;
1492 string_offset = sizeof (*header) + header->main_size;
1494 lto_input_block ib (data + sizeof (*header), header->main_size,
1495 file_data->mode_table);
1497 data_in = lto_data_in_create (file_data, data + string_offset,
1498 header->string_size, vNULL);
1500 while ((str = streamer_read_string_cst (data_in, &ib)))
1502 asm_node *node = symtab->finalize_toplevel_asm (str);
1503 node->order = streamer_read_hwi (&ib) + order_base;
1504 if (node->order >= symtab->order)
1505 symtab->order = node->order + 1;
1508 lto_data_in_delete (data_in);
1510 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1514 /* Input mode table. */
1516 void
1517 lto_input_mode_table (struct lto_file_decl_data *file_data)
1519 size_t len;
1520 const char *data = lto_get_section_data (file_data, LTO_section_mode_table,
1521 NULL, &len);
1522 if (! data)
1524 internal_error ("cannot read LTO mode table from %s",
1525 file_data->file_name);
1526 return;
1529 unsigned char *table = ggc_cleared_vec_alloc<unsigned char> (1 << 8);
1530 file_data->mode_table = table;
1531 const struct lto_simple_header_with_strings *header
1532 = (const struct lto_simple_header_with_strings *) data;
1533 int string_offset;
1534 struct data_in *data_in;
1535 string_offset = sizeof (*header) + header->main_size;
1537 lto_input_block ib (data + sizeof (*header), header->main_size, NULL);
1538 data_in = lto_data_in_create (file_data, data + string_offset,
1539 header->string_size, vNULL);
1540 bitpack_d bp = streamer_read_bitpack (&ib);
1542 table[VOIDmode] = VOIDmode;
1543 table[BLKmode] = BLKmode;
1544 unsigned int m;
1545 while ((m = bp_unpack_value (&bp, 8)) != VOIDmode)
1547 enum mode_class mclass
1548 = bp_unpack_enum (&bp, mode_class, MAX_MODE_CLASS);
1549 unsigned int size = bp_unpack_value (&bp, 8);
1550 unsigned int prec = bp_unpack_value (&bp, 16);
1551 machine_mode inner = (machine_mode) table[bp_unpack_value (&bp, 8)];
1552 unsigned int nunits = bp_unpack_value (&bp, 8);
1553 unsigned int ibit = 0, fbit = 0;
1554 unsigned int real_fmt_len = 0;
1555 const char *real_fmt_name = NULL;
1556 switch (mclass)
1558 case MODE_FRACT:
1559 case MODE_UFRACT:
1560 case MODE_ACCUM:
1561 case MODE_UACCUM:
1562 ibit = bp_unpack_value (&bp, 8);
1563 fbit = bp_unpack_value (&bp, 8);
1564 break;
1565 case MODE_FLOAT:
1566 case MODE_DECIMAL_FLOAT:
1567 real_fmt_name = bp_unpack_indexed_string (data_in, &bp,
1568 &real_fmt_len);
1569 break;
1570 default:
1571 break;
1573 /* First search just the GET_CLASS_NARROWEST_MODE to wider modes,
1574 if not found, fallback to all modes. */
1575 int pass;
1576 for (pass = 0; pass < 2; pass++)
1577 for (machine_mode mr = pass ? VOIDmode
1578 : GET_CLASS_NARROWEST_MODE (mclass);
1579 pass ? mr < MAX_MACHINE_MODE : mr != VOIDmode;
1580 pass ? mr = (machine_mode) (m + 1)
1581 : mr = GET_MODE_WIDER_MODE (mr))
1582 if (GET_MODE_CLASS (mr) != mclass
1583 || GET_MODE_SIZE (mr) != size
1584 || GET_MODE_PRECISION (mr) != prec
1585 || GET_MODE_INNER (mr) != inner
1586 || GET_MODE_IBIT (mr) != ibit
1587 || GET_MODE_FBIT (mr) != fbit
1588 || GET_MODE_NUNITS (mr) != nunits)
1589 continue;
1590 else if ((mclass == MODE_FLOAT || mclass == MODE_DECIMAL_FLOAT)
1591 && strcmp (REAL_MODE_FORMAT (mr)->name, real_fmt_name) != 0)
1592 continue;
1593 else
1595 table[m] = mr;
1596 pass = 2;
1597 break;
1599 unsigned int mname_len;
1600 const char *mname = bp_unpack_indexed_string (data_in, &bp, &mname_len);
1601 if (pass == 2)
1603 switch (mclass)
1605 case MODE_VECTOR_INT:
1606 case MODE_VECTOR_FLOAT:
1607 case MODE_VECTOR_FRACT:
1608 case MODE_VECTOR_UFRACT:
1609 case MODE_VECTOR_ACCUM:
1610 case MODE_VECTOR_UACCUM:
1611 /* For unsupported vector modes just use BLKmode,
1612 if the scalar mode is supported. */
1613 if (inner != VOIDmode)
1615 table[m] = BLKmode;
1616 break;
1618 /* FALLTHRU */
1619 default:
1620 fatal_error (UNKNOWN_LOCATION, "unsupported mode %s\n", mname);
1621 break;
1625 lto_data_in_delete (data_in);
1627 lto_free_section_data (file_data, LTO_section_mode_table, NULL, data, len);
1631 /* Initialization for the LTO reader. */
1633 void
1634 lto_reader_init (void)
1636 lto_streamer_init ();
1637 file_name_hash_table
1638 = new hash_table<freeing_string_slot_hasher> (37);
1642 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1643 table to use with LEN strings. RESOLUTIONS is the vector of linker
1644 resolutions (NULL if not using a linker plugin). */
1646 struct data_in *
1647 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1648 unsigned len,
1649 vec<ld_plugin_symbol_resolution_t> resolutions)
1651 struct data_in *data_in = new (struct data_in);
1652 data_in->file_data = file_data;
1653 data_in->strings = strings;
1654 data_in->strings_len = len;
1655 data_in->globals_resolution = resolutions;
1656 data_in->reader_cache = streamer_tree_cache_create (false, false, true);
1657 return data_in;
1661 /* Remove DATA_IN. */
1663 void
1664 lto_data_in_delete (struct data_in *data_in)
1666 data_in->globals_resolution.release ();
1667 streamer_tree_cache_delete (data_in->reader_cache);
1668 delete data_in;