xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/lto-streamer-in.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* Read the GIMPLE representation from a file stream.
2 
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>
6 
7 This file is part of GCC.
8 
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.
13 
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.
18 
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/>.  */
22 
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "fold-const.h"
39 #include "stringpool.h"
40 #include "hashtab.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "rtl.h"
44 #include "flags.h"
45 #include "statistics.h"
46 #include "real.h"
47 #include "fixed-value.h"
48 #include "insn-config.h"
49 #include "expmed.h"
50 #include "dojump.h"
51 #include "explow.h"
52 #include "calls.h"
53 #include "emit-rtl.h"
54 #include "varasm.h"
55 #include "stmt.h"
56 #include "expr.h"
57 #include "params.h"
58 #include "predict.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "basic-block.h"
62 #include "tree-ssa-alias.h"
63 #include "internal-fn.h"
64 #include "gimple-expr.h"
65 #include "is-a.h"
66 #include "gimple.h"
67 #include "gimple-iterator.h"
68 #include "gimple-ssa.h"
69 #include "tree-cfg.h"
70 #include "tree-ssanames.h"
71 #include "tree-into-ssa.h"
72 #include "tree-dfa.h"
73 #include "tree-ssa.h"
74 #include "tree-pass.h"
75 #include "diagnostic.h"
76 #include "except.h"
77 #include "debug.h"
78 #include "hash-map.h"
79 #include "plugin-api.h"
80 #include "ipa-ref.h"
81 #include "cgraph.h"
82 #include "ipa-utils.h"
83 #include "data-streamer.h"
84 #include "gimple-streamer.h"
85 #include "lto-streamer.h"
86 #include "tree-streamer.h"
87 #include "streamer-hooks.h"
88 #include "cfgloop.h"
89 
90 
91 struct freeing_string_slot_hasher : string_slot_hasher
92 {
93   static inline void remove (value_type *);
94 };
95 
96 inline void
97 freeing_string_slot_hasher::remove (value_type *v)
98 {
99   free (v);
100 }
101 
102 /* The table to hold the file names.  */
103 static hash_table<freeing_string_slot_hasher> *file_name_hash_table;
104 
105 
106 /* Check that tag ACTUAL has one of the given values.  NUM_TAGS is the
107    number of valid tag values to check.  */
108 
109 void
110 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
111 {
112   va_list ap;
113   int i;
114 
115   va_start (ap, ntags);
116   for (i = 0; i < ntags; i++)
117     if ((unsigned) actual == va_arg (ap, unsigned))
118       {
119 	va_end (ap);
120 	return;
121       }
122 
123   va_end (ap);
124   internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
125 }
126 
127 
128 /* Read LENGTH bytes from STREAM to ADDR.  */
129 
130 void
131 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
132 {
133   size_t i;
134   unsigned char *const buffer = (unsigned char *const) addr;
135 
136   for (i = 0; i < length; i++)
137     buffer[i] = streamer_read_uchar (ib);
138 }
139 
140 
141 /* Lookup STRING in file_name_hash_table.  If found, return the existing
142    string, otherwise insert STRING as the canonical version.  */
143 
144 static const char *
145 canon_file_name (const char *string)
146 {
147   string_slot **slot;
148   struct string_slot s_slot;
149   size_t len = strlen (string);
150 
151   s_slot.s = string;
152   s_slot.len = len;
153 
154   slot = file_name_hash_table->find_slot (&s_slot, INSERT);
155   if (*slot == NULL)
156     {
157       char *saved_string;
158       struct string_slot *new_slot;
159 
160       saved_string = (char *) xmalloc (len + 1);
161       new_slot = XCNEW (struct string_slot);
162       memcpy (saved_string, string, len + 1);
163       new_slot->s = saved_string;
164       new_slot->len = len;
165       *slot = new_slot;
166       return saved_string;
167     }
168   else
169     {
170       struct string_slot *old_slot = *slot;
171       return old_slot->s;
172     }
173 }
174 
175 /* Pointer to currently alive instance of lto_location_cache.  */
176 
177 lto_location_cache *lto_location_cache::current_cache;
178 
179 /* Sort locations in source order. Start with file from last application.  */
180 
181 int
182 lto_location_cache::cmp_loc (const void *pa, const void *pb)
183 {
184   const cached_location *a = ((const cached_location *)pa);
185   const cached_location *b = ((const cached_location *)pb);
186   const char *current_file = current_cache->current_file;
187   int current_line = current_cache->current_line;
188 
189   if (a->file == current_file && b->file != current_file)
190     return -1;
191   if (a->file != current_file && b->file == current_file)
192     return 1;
193   if (a->file == current_file && b->file == current_file)
194     {
195       if (a->line == current_line && b->line != current_line)
196 	return -1;
197       if (a->line != current_line && b->line == current_line)
198 	return 1;
199     }
200   if (a->file != b->file)
201     return strcmp (a->file, b->file);
202   if (a->line != b->line)
203     return a->line - b->line;
204   return a->col - b->col;
205 }
206 
207 /* Apply all changes in location cache.  Add locations into linemap and patch
208    trees.  */
209 
210 bool
211 lto_location_cache::apply_location_cache ()
212 {
213   static const char *prev_file;
214   if (!loc_cache.length ())
215     return false;
216   if (loc_cache.length () > 1)
217     loc_cache.qsort (cmp_loc);
218 
219   for (unsigned int i = 0; i < loc_cache.length (); i++)
220     {
221       struct cached_location loc = loc_cache[i];
222 
223       if (current_file != loc.file)
224 	linemap_add (line_table, prev_file ? LC_RENAME : LC_ENTER,
225 		     false, loc.file, loc.line);
226       else if (current_line != loc.line)
227 	{
228 	  int max = loc.col;
229 
230 	  for (unsigned int j = i + 1; j < loc_cache.length (); j++)
231 	    if (loc.file != loc_cache[j].file
232 		|| loc.line != loc_cache[j].line)
233 	      break;
234 	    else if (max < loc_cache[j].col)
235 	      max = loc_cache[j].col;
236 	  linemap_line_start (line_table, loc.line, max + 1);
237 	}
238       gcc_assert (*loc.loc == BUILTINS_LOCATION + 1);
239       if (current_file == loc.file && current_line == loc.line
240 	  && current_col == loc.col)
241 	*loc.loc = current_loc;
242       else
243         current_loc = *loc.loc = linemap_position_for_column (line_table,
244 							      loc.col);
245       current_line = loc.line;
246       prev_file = current_file = loc.file;
247       current_col = loc.col;
248     }
249   loc_cache.truncate (0);
250   accepted_length = 0;
251   return true;
252 }
253 
254 /* Tree merging did not suceed; mark all changes in the cache as accepted.  */
255 
256 void
257 lto_location_cache::accept_location_cache ()
258 {
259   gcc_assert (current_cache == this);
260   accepted_length = loc_cache.length ();
261 }
262 
263 /* Tree merging did suceed; throw away recent changes.  */
264 
265 void
266 lto_location_cache::revert_location_cache ()
267 {
268   loc_cache.truncate (accepted_length);
269 }
270 
271 /* Read a location bitpack from input block IB and either update *LOC directly
272    or add it to the location cache.
273    It is neccesary to call apply_location_cache to get *LOC updated.  */
274 
275 void
276 lto_location_cache::input_location (location_t *loc, struct bitpack_d *bp,
277 				    struct data_in *data_in)
278 {
279   static const char *stream_file;
280   static int stream_line;
281   static int stream_col;
282   bool file_change, line_change, column_change;
283 
284   gcc_assert (current_cache == this);
285 
286   if (bp_unpack_value (bp, 1))
287     {
288       *loc = UNKNOWN_LOCATION;
289       return;
290     }
291   *loc = BUILTINS_LOCATION + 1;
292 
293   file_change = bp_unpack_value (bp, 1);
294   line_change = bp_unpack_value (bp, 1);
295   column_change = bp_unpack_value (bp, 1);
296 
297   if (file_change)
298     stream_file = canon_file_name (bp_unpack_string (data_in, bp));
299 
300   if (line_change)
301     stream_line = bp_unpack_var_len_unsigned (bp);
302 
303   if (column_change)
304     stream_col = bp_unpack_var_len_unsigned (bp);
305 
306   /* This optimization saves location cache operations druing gimple
307      streaming.  */
308 
309   if (current_file == stream_file && current_line == stream_line
310       && current_col == stream_col)
311     {
312       *loc = current_loc;
313       return;
314     }
315 
316   struct cached_location entry = {stream_file, loc, stream_line, stream_col};
317   loc_cache.safe_push (entry);
318 }
319 
320 /* Read a location bitpack from input block IB and either update *LOC directly
321    or add it to the location cache.
322    It is neccesary to call apply_location_cache to get *LOC updated.  */
323 
324 void
325 lto_input_location (location_t *loc, struct bitpack_d *bp,
326 		    struct data_in *data_in)
327 {
328   data_in->location_cache.input_location (loc, bp, data_in);
329 }
330 
331 /* Read location and return it instead of going through location caching.
332    This should be used only when the resulting location is not going to be
333    discarded.  */
334 
335 location_t
336 stream_input_location_now (struct bitpack_d *bp, struct data_in *data_in)
337 {
338   location_t loc;
339   stream_input_location (&loc, bp, data_in);
340   data_in->location_cache.apply_location_cache ();
341   return loc;
342 }
343 
344 /* Read a reference to a tree node from DATA_IN using input block IB.
345    TAG is the expected node that should be found in IB, if TAG belongs
346    to one of the indexable trees, expect to read a reference index to
347    be looked up in one of the symbol tables, otherwise read the pysical
348    representation of the tree using stream_read_tree.  FN is the
349    function scope for the read tree.  */
350 
351 tree
352 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
353 		    struct function *fn, enum LTO_tags tag)
354 {
355   unsigned HOST_WIDE_INT ix_u;
356   tree result = NULL_TREE;
357 
358   lto_tag_check_range (tag, LTO_field_decl_ref, LTO_namelist_decl_ref);
359 
360   switch (tag)
361     {
362     case LTO_type_ref:
363       ix_u = streamer_read_uhwi (ib);
364       result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
365       break;
366 
367     case LTO_ssa_name_ref:
368       ix_u = streamer_read_uhwi (ib);
369       result = (*SSANAMES (fn))[ix_u];
370       break;
371 
372     case LTO_field_decl_ref:
373       ix_u = streamer_read_uhwi (ib);
374       result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
375       break;
376 
377     case LTO_function_decl_ref:
378       ix_u = streamer_read_uhwi (ib);
379       result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
380       break;
381 
382     case LTO_type_decl_ref:
383       ix_u = streamer_read_uhwi (ib);
384       result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
385       break;
386 
387     case LTO_namespace_decl_ref:
388       ix_u = streamer_read_uhwi (ib);
389       result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
390       break;
391 
392     case LTO_global_decl_ref:
393     case LTO_result_decl_ref:
394     case LTO_const_decl_ref:
395     case LTO_imported_decl_ref:
396     case LTO_label_decl_ref:
397     case LTO_translation_unit_decl_ref:
398     case LTO_namelist_decl_ref:
399       ix_u = streamer_read_uhwi (ib);
400       result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
401       break;
402 
403     default:
404       gcc_unreachable ();
405     }
406 
407   gcc_assert (result);
408 
409   return result;
410 }
411 
412 
413 /* Read and return a double-linked list of catch handlers from input
414    block IB, using descriptors in DATA_IN.  */
415 
416 static struct eh_catch_d *
417 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
418 			 eh_catch *last_p)
419 {
420   eh_catch first;
421   enum LTO_tags tag;
422 
423   *last_p = first = NULL;
424   tag = streamer_read_record_start (ib);
425   while (tag)
426     {
427       tree list;
428       eh_catch n;
429 
430       lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
431 
432       /* Read the catch node.  */
433       n = ggc_cleared_alloc<eh_catch_d> ();
434       n->type_list = stream_read_tree (ib, data_in);
435       n->filter_list = stream_read_tree (ib, data_in);
436       n->label = stream_read_tree (ib, data_in);
437 
438       /* Register all the types in N->FILTER_LIST.  */
439       for (list = n->filter_list; list; list = TREE_CHAIN (list))
440 	add_type_for_runtime (TREE_VALUE (list));
441 
442       /* Chain N to the end of the list.  */
443       if (*last_p)
444 	(*last_p)->next_catch = n;
445       n->prev_catch = *last_p;
446       *last_p = n;
447 
448       /* Set the head of the list the first time through the loop.  */
449       if (first == NULL)
450 	first = n;
451 
452       tag = streamer_read_record_start (ib);
453     }
454 
455   return first;
456 }
457 
458 
459 /* Read and return EH region IX from input block IB, using descriptors
460    in DATA_IN.  */
461 
462 static eh_region
463 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
464 {
465   enum LTO_tags tag;
466   eh_region r;
467 
468   /* Read the region header.  */
469   tag = streamer_read_record_start (ib);
470   if (tag == LTO_null)
471     return NULL;
472 
473   r = ggc_cleared_alloc<eh_region_d> ();
474   r->index = streamer_read_hwi (ib);
475 
476   gcc_assert (r->index == ix);
477 
478   /* Read all the region pointers as region numbers.  We'll fix up
479      the pointers once the whole array has been read.  */
480   r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
481   r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
482   r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
483 
484   switch (tag)
485     {
486       case LTO_ert_cleanup:
487 	r->type = ERT_CLEANUP;
488 	break;
489 
490       case LTO_ert_try:
491 	{
492 	  struct eh_catch_d *last_catch;
493 	  r->type = ERT_TRY;
494 	  r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
495 							     &last_catch);
496 	  r->u.eh_try.last_catch = last_catch;
497 	  break;
498 	}
499 
500       case LTO_ert_allowed_exceptions:
501 	{
502 	  tree l;
503 
504 	  r->type = ERT_ALLOWED_EXCEPTIONS;
505 	  r->u.allowed.type_list = stream_read_tree (ib, data_in);
506 	  r->u.allowed.label = stream_read_tree (ib, data_in);
507 	  r->u.allowed.filter = streamer_read_uhwi (ib);
508 
509 	  for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
510 	    add_type_for_runtime (TREE_VALUE (l));
511 	}
512 	break;
513 
514       case LTO_ert_must_not_throw:
515 	{
516 	  r->type = ERT_MUST_NOT_THROW;
517 	  r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
518 	  bitpack_d bp = streamer_read_bitpack (ib);
519 	  r->u.must_not_throw.failure_loc
520 	   = stream_input_location_now (&bp, data_in);
521 	}
522 	break;
523 
524       default:
525 	gcc_unreachable ();
526     }
527 
528   r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
529 
530   return r;
531 }
532 
533 
534 /* Read and return EH landing pad IX from input block IB, using descriptors
535    in DATA_IN.  */
536 
537 static eh_landing_pad
538 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
539 {
540   enum LTO_tags tag;
541   eh_landing_pad lp;
542 
543   /* Read the landing pad header.  */
544   tag = streamer_read_record_start (ib);
545   if (tag == LTO_null)
546     return NULL;
547 
548   lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
549 
550   lp = ggc_cleared_alloc<eh_landing_pad_d> ();
551   lp->index = streamer_read_hwi (ib);
552   gcc_assert (lp->index == ix);
553   lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
554   lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
555   lp->post_landing_pad = stream_read_tree (ib, data_in);
556 
557   return lp;
558 }
559 
560 
561 /* After reading the EH regions, pointers to peer and children regions
562    are region numbers.  This converts all these region numbers into
563    real pointers into the rematerialized regions for FN.  ROOT_REGION
564    is the region number for the root EH region in FN.  */
565 
566 static void
567 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
568 {
569   unsigned i;
570   vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
571   vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
572   eh_region r;
573   eh_landing_pad lp;
574 
575   gcc_assert (eh_array && lp_array);
576 
577   gcc_assert (root_region >= 0);
578   fn->eh->region_tree = (*eh_array)[root_region];
579 
580 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
581 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
582 
583   /* Convert all the index numbers stored in pointer fields into
584      pointers to the corresponding slots in the EH region array.  */
585   FOR_EACH_VEC_ELT (*eh_array, i, r)
586     {
587       /* The array may contain NULL regions.  */
588       if (r == NULL)
589 	continue;
590 
591       gcc_assert (i == (unsigned) r->index);
592       FIXUP_EH_REGION (r->outer);
593       FIXUP_EH_REGION (r->inner);
594       FIXUP_EH_REGION (r->next_peer);
595       FIXUP_EH_LP (r->landing_pads);
596     }
597 
598   /* Convert all the index numbers stored in pointer fields into
599      pointers to the corresponding slots in the EH landing pad array.  */
600   FOR_EACH_VEC_ELT (*lp_array, i, lp)
601     {
602       /* The array may contain NULL landing pads.  */
603       if (lp == NULL)
604 	continue;
605 
606       gcc_assert (i == (unsigned) lp->index);
607       FIXUP_EH_LP (lp->next_lp);
608       FIXUP_EH_REGION (lp->region);
609     }
610 
611 #undef FIXUP_EH_REGION
612 #undef FIXUP_EH_LP
613 }
614 
615 
616 /* Initialize EH support.  */
617 
618 void
619 lto_init_eh (void)
620 {
621   static bool eh_initialized_p = false;
622 
623   if (eh_initialized_p)
624     return;
625 
626   /* Contrary to most other FEs, we only initialize EH support when at
627      least one of the files in the set contains exception regions in
628      it.  Since this happens much later than the call to init_eh in
629      lang_dependent_init, we have to set flag_exceptions and call
630      init_eh again to initialize the EH tables.  */
631   flag_exceptions = 1;
632   init_eh ();
633 
634   eh_initialized_p = true;
635 }
636 
637 
638 /* Read the exception table for FN from IB using the data descriptors
639    in DATA_IN.  */
640 
641 static void
642 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
643 		  struct function *fn)
644 {
645   HOST_WIDE_INT i, root_region, len;
646   enum LTO_tags tag;
647 
648   tag = streamer_read_record_start (ib);
649   if (tag == LTO_null)
650     return;
651 
652   lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
653 
654   /* If the file contains EH regions, then it was compiled with
655      -fexceptions.  In that case, initialize the backend EH
656      machinery.  */
657   lto_init_eh ();
658 
659   gcc_assert (fn->eh);
660 
661   root_region = streamer_read_hwi (ib);
662   gcc_assert (root_region == (int) root_region);
663 
664   /* Read the EH region array.  */
665   len = streamer_read_hwi (ib);
666   gcc_assert (len == (int) len);
667   if (len > 0)
668     {
669       vec_safe_grow_cleared (fn->eh->region_array, len);
670       for (i = 0; i < len; i++)
671 	{
672 	  eh_region r = input_eh_region (ib, data_in, i);
673 	  (*fn->eh->region_array)[i] = r;
674 	}
675     }
676 
677   /* Read the landing pads.  */
678   len = streamer_read_hwi (ib);
679   gcc_assert (len == (int) len);
680   if (len > 0)
681     {
682       vec_safe_grow_cleared (fn->eh->lp_array, len);
683       for (i = 0; i < len; i++)
684 	{
685 	  eh_landing_pad lp = input_eh_lp (ib, data_in, i);
686 	  (*fn->eh->lp_array)[i] = lp;
687 	}
688     }
689 
690   /* Read the runtime type data.  */
691   len = streamer_read_hwi (ib);
692   gcc_assert (len == (int) len);
693   if (len > 0)
694     {
695       vec_safe_grow_cleared (fn->eh->ttype_data, len);
696       for (i = 0; i < len; i++)
697 	{
698 	  tree ttype = stream_read_tree (ib, data_in);
699 	  (*fn->eh->ttype_data)[i] = ttype;
700 	}
701     }
702 
703   /* Read the table of action chains.  */
704   len = streamer_read_hwi (ib);
705   gcc_assert (len == (int) len);
706   if (len > 0)
707     {
708       if (targetm.arm_eabi_unwinder)
709 	{
710 	  vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
711 	  for (i = 0; i < len; i++)
712 	    {
713 	      tree t = stream_read_tree (ib, data_in);
714 	      (*fn->eh->ehspec_data.arm_eabi)[i] = t;
715 	    }
716 	}
717       else
718 	{
719 	  vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
720 	  for (i = 0; i < len; i++)
721 	    {
722 	      uchar c = streamer_read_uchar (ib);
723 	      (*fn->eh->ehspec_data.other)[i] = c;
724 	    }
725 	}
726     }
727 
728   /* Reconstruct the EH region tree by fixing up the peer/children
729      pointers.  */
730   fixup_eh_region_pointers (fn, root_region);
731 
732   tag = streamer_read_record_start (ib);
733   lto_tag_check_range (tag, LTO_null, LTO_null);
734 }
735 
736 
737 /* Make a new basic block with index INDEX in function FN.  */
738 
739 static basic_block
740 make_new_block (struct function *fn, unsigned int index)
741 {
742   basic_block bb = alloc_block ();
743   bb->index = index;
744   SET_BASIC_BLOCK_FOR_FN (fn, index, bb);
745   n_basic_blocks_for_fn (fn)++;
746   return bb;
747 }
748 
749 
750 /* Read a wide-int.  */
751 
752 static widest_int
753 streamer_read_wi (struct lto_input_block *ib)
754 {
755   HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
756   int i;
757   int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
758   int len = streamer_read_uhwi (ib);
759   for (i = 0; i < len; i++)
760     a[i] = streamer_read_hwi (ib);
761   return widest_int::from_array (a, len);
762 }
763 
764 
765 /* Read the CFG for function FN from input block IB.  */
766 
767 static void
768 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
769 	   struct function *fn,
770 	   int count_materialization_scale)
771 {
772   unsigned int bb_count;
773   basic_block p_bb;
774   unsigned int i;
775   int index;
776 
777   init_empty_tree_cfg_for_function (fn);
778   init_ssa_operands (fn);
779 
780   profile_status_for_fn (fn) = streamer_read_enum (ib, profile_status_d,
781 						   PROFILE_LAST);
782 
783   bb_count = streamer_read_uhwi (ib);
784 
785   last_basic_block_for_fn (fn) = bb_count;
786   if (bb_count > basic_block_info_for_fn (fn)->length ())
787     vec_safe_grow_cleared (basic_block_info_for_fn (fn), bb_count);
788 
789   if (bb_count > label_to_block_map_for_fn (fn)->length ())
790     vec_safe_grow_cleared (label_to_block_map_for_fn (fn), bb_count);
791 
792   index = streamer_read_hwi (ib);
793   while (index != -1)
794     {
795       basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
796       unsigned int edge_count;
797 
798       if (bb == NULL)
799 	bb = make_new_block (fn, index);
800 
801       edge_count = streamer_read_uhwi (ib);
802 
803       /* Connect up the CFG.  */
804       for (i = 0; i < edge_count; i++)
805 	{
806 	  unsigned int dest_index;
807 	  unsigned int edge_flags;
808 	  basic_block dest;
809 	  int probability;
810 	  gcov_type count;
811 	  edge e;
812 
813 	  dest_index = streamer_read_uhwi (ib);
814 	  probability = (int) streamer_read_hwi (ib);
815 	  count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
816                                count_materialization_scale);
817 	  edge_flags = streamer_read_uhwi (ib);
818 
819 	  dest = BASIC_BLOCK_FOR_FN (fn, dest_index);
820 
821 	  if (dest == NULL)
822 	    dest = make_new_block (fn, dest_index);
823 
824 	  e = make_edge (bb, dest, edge_flags);
825 	  e->probability = probability;
826 	  e->count = count;
827 	}
828 
829       index = streamer_read_hwi (ib);
830     }
831 
832   p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
833   index = streamer_read_hwi (ib);
834   while (index != -1)
835     {
836       basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
837       bb->prev_bb = p_bb;
838       p_bb->next_bb = bb;
839       p_bb = bb;
840       index = streamer_read_hwi (ib);
841     }
842 
843   /* ???  The cfgloop interface is tied to cfun.  */
844   gcc_assert (cfun == fn);
845 
846   /* Input the loop tree.  */
847   unsigned n_loops = streamer_read_uhwi (ib);
848   if (n_loops == 0)
849     return;
850 
851   struct loops *loops = ggc_cleared_alloc<struct loops> ();
852   init_loops_structure (fn, loops, n_loops);
853   set_loops_for_fn (fn, loops);
854 
855   /* Input each loop and associate it with its loop header so
856      flow_loops_find can rebuild the loop tree.  */
857   for (unsigned i = 1; i < n_loops; ++i)
858     {
859       int header_index = streamer_read_hwi (ib);
860       if (header_index == -1)
861 	{
862 	  loops->larray->quick_push (NULL);
863 	  continue;
864 	}
865 
866       struct loop *loop = alloc_loop ();
867       loop->header = BASIC_BLOCK_FOR_FN (fn, header_index);
868       loop->header->loop_father = loop;
869 
870       /* Read everything copy_loop_info copies.  */
871       loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
872       loop->any_upper_bound = streamer_read_hwi (ib);
873       if (loop->any_upper_bound)
874 	loop->nb_iterations_upper_bound = streamer_read_wi (ib);
875       loop->any_estimate = streamer_read_hwi (ib);
876       if (loop->any_estimate)
877 	loop->nb_iterations_estimate = streamer_read_wi (ib);
878 
879       /* Read OMP SIMD related info.  */
880       loop->safelen = streamer_read_hwi (ib);
881       loop->dont_vectorize = streamer_read_hwi (ib);
882       loop->force_vectorize = streamer_read_hwi (ib);
883       loop->simduid = stream_read_tree (ib, data_in);
884 
885       place_new_loop (fn, loop);
886 
887       /* flow_loops_find doesn't like loops not in the tree, hook them
888          all as siblings of the tree root temporarily.  */
889       flow_loop_tree_node_add (loops->tree_root, loop);
890     }
891 
892   /* Rebuild the loop tree.  */
893   flow_loops_find (loops);
894 }
895 
896 
897 /* Read the SSA names array for function FN from DATA_IN using input
898    block IB.  */
899 
900 static void
901 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
902 		 struct function *fn)
903 {
904   unsigned int i, size;
905 
906   size = streamer_read_uhwi (ib);
907   init_ssanames (fn, size);
908 
909   i = streamer_read_uhwi (ib);
910   while (i)
911     {
912       tree ssa_name, name;
913       bool is_default_def;
914 
915       /* Skip over the elements that had been freed.  */
916       while (SSANAMES (fn)->length () < i)
917 	SSANAMES (fn)->quick_push (NULL_TREE);
918 
919       is_default_def = (streamer_read_uchar (ib) != 0);
920       name = stream_read_tree (ib, data_in);
921       ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
922 
923       if (is_default_def)
924 	set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
925 
926       i = streamer_read_uhwi (ib);
927     }
928 }
929 
930 
931 /* Go through all NODE edges and fixup call_stmt pointers
932    so they point to STMTS.  */
933 
934 static void
935 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
936 			 struct function *fn)
937 {
938   struct cgraph_edge *cedge;
939   struct ipa_ref *ref = NULL;
940   unsigned int i;
941 
942   for (cedge = node->callees; cedge; cedge = cedge->next_callee)
943     {
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,
950 		     "Cgraph edge statement index not found");
951     }
952   for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
953     {
954       if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
955         fatal_error (input_location,
956 		     "Cgraph edge statement index out of range");
957       cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
958       if (!cedge->call_stmt)
959         fatal_error (input_location, "Cgraph edge statement index not found");
960     }
961   for (i = 0; node->iterate_reference (i, ref); i++)
962     if (ref->lto_stmt_uid)
963       {
964 	if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
965 	  fatal_error (input_location,
966 		       "Reference statement index out of range");
967 	ref->stmt = stmts[ref->lto_stmt_uid - 1];
968 	if (!ref->stmt)
969 	  fatal_error (input_location, "Reference statement index not found");
970       }
971 }
972 
973 
974 /* Fixup call_stmt pointers in NODE and all clones.  */
975 
976 static void
977 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
978 {
979   struct cgraph_node *node;
980   struct function *fn;
981 
982   while (orig->clone_of)
983     orig = orig->clone_of;
984   fn = DECL_STRUCT_FUNCTION (orig->decl);
985 
986   fixup_call_stmt_edges_1 (orig, stmts, fn);
987   if (orig->clones)
988     for (node = orig->clones; node != orig;)
989       {
990 	fixup_call_stmt_edges_1 (node, stmts, fn);
991 	if (node->clones)
992 	  node = node->clones;
993 	else if (node->next_sibling_clone)
994 	  node = node->next_sibling_clone;
995 	else
996 	  {
997 	    while (node != orig && !node->next_sibling_clone)
998 	      node = node->clone_of;
999 	    if (node != orig)
1000 	      node = node->next_sibling_clone;
1001 	  }
1002       }
1003 }
1004 
1005 
1006 /* Input the base body of struct function FN from DATA_IN
1007    using input block IB.  */
1008 
1009 static void
1010 input_struct_function_base (struct function *fn, struct data_in *data_in,
1011                             struct lto_input_block *ib)
1012 {
1013   struct bitpack_d bp;
1014   int len;
1015 
1016   /* Read the static chain and non-local goto save area.  */
1017   fn->static_chain_decl = stream_read_tree (ib, data_in);
1018   fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
1019 
1020   /* Read all the local symbols.  */
1021   len = streamer_read_hwi (ib);
1022   if (len > 0)
1023     {
1024       int i;
1025       vec_safe_grow_cleared (fn->local_decls, len);
1026       for (i = 0; i < len; i++)
1027 	{
1028 	  tree t = stream_read_tree (ib, data_in);
1029 	  (*fn->local_decls)[i] = t;
1030 	}
1031     }
1032 
1033   /* Input the current IL state of the function.  */
1034   fn->curr_properties = streamer_read_uhwi (ib);
1035 
1036   /* Read all the attributes for FN.  */
1037   bp = streamer_read_bitpack (ib);
1038   fn->is_thunk = bp_unpack_value (&bp, 1);
1039   fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
1040   fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
1041   fn->returns_struct = bp_unpack_value (&bp, 1);
1042   fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
1043   fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
1044   fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
1045   fn->after_inlining = bp_unpack_value (&bp, 1);
1046   fn->stdarg = bp_unpack_value (&bp, 1);
1047   fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
1048   fn->calls_alloca = bp_unpack_value (&bp, 1);
1049   fn->calls_setjmp = bp_unpack_value (&bp, 1);
1050   fn->has_force_vectorize_loops = bp_unpack_value (&bp, 1);
1051   fn->has_simduid_loops = bp_unpack_value (&bp, 1);
1052   fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
1053   fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
1054   fn->last_clique = bp_unpack_value (&bp, sizeof (short) * 8);
1055 
1056   /* Input the function start and end loci.  */
1057   fn->function_start_locus = stream_input_location_now (&bp, data_in);
1058   fn->function_end_locus = stream_input_location_now (&bp, data_in);
1059 }
1060 
1061 
1062 /* Read the body of function FN_DECL from DATA_IN using input block IB.  */
1063 
1064 static void
1065 input_function (tree fn_decl, struct data_in *data_in,
1066 		struct lto_input_block *ib, struct lto_input_block *ib_cfg)
1067 {
1068   struct function *fn;
1069   enum LTO_tags tag;
1070   gimple *stmts;
1071   basic_block bb;
1072   struct cgraph_node *node;
1073 
1074   tag = streamer_read_record_start (ib);
1075   lto_tag_check (tag, LTO_function);
1076 
1077   /* Read decls for parameters and args.  */
1078   DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
1079   DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
1080 
1081   /* Read the tree of lexical scopes for the function.  */
1082   DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
1083 
1084   if (!streamer_read_uhwi (ib))
1085     return;
1086 
1087   push_struct_function (fn_decl);
1088   fn = DECL_STRUCT_FUNCTION (fn_decl);
1089   init_tree_ssa (fn);
1090   /* We input IL in SSA form.  */
1091   cfun->gimple_df->in_ssa_p = true;
1092 
1093   gimple_register_cfg_hooks ();
1094 
1095   node = cgraph_node::get (fn_decl);
1096   if (!node)
1097     node = cgraph_node::create (fn_decl);
1098   input_struct_function_base (fn, data_in, ib);
1099   input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
1100 
1101   /* Read all the SSA names.  */
1102   input_ssa_names (ib, data_in, fn);
1103 
1104   /* Read the exception handling regions in the function.  */
1105   input_eh_regions (ib, data_in, fn);
1106 
1107   gcc_assert (DECL_INITIAL (fn_decl));
1108   DECL_SAVED_TREE (fn_decl) = NULL_TREE;
1109 
1110   /* Read all the basic blocks.  */
1111   tag = streamer_read_record_start (ib);
1112   while (tag)
1113     {
1114       input_bb (ib, tag, data_in, fn,
1115 		node->count_materialization_scale);
1116       tag = streamer_read_record_start (ib);
1117     }
1118 
1119   /* Fix up the call statements that are mentioned in the callgraph
1120      edges.  */
1121   set_gimple_stmt_max_uid (cfun, 0);
1122   FOR_ALL_BB_FN (bb, cfun)
1123     {
1124       gimple_stmt_iterator gsi;
1125       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1126 	{
1127 	  gimple stmt = gsi_stmt (gsi);
1128 	  gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1129 	}
1130       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1131 	{
1132 	  gimple stmt = gsi_stmt (gsi);
1133 	  gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1134 	}
1135     }
1136   stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1137   FOR_ALL_BB_FN (bb, cfun)
1138     {
1139       gimple_stmt_iterator bsi = gsi_start_phis (bb);
1140       while (!gsi_end_p (bsi))
1141 	{
1142 	  gimple stmt = gsi_stmt (bsi);
1143 	  gsi_next (&bsi);
1144 	  stmts[gimple_uid (stmt)] = stmt;
1145 	}
1146       bsi = gsi_start_bb (bb);
1147       while (!gsi_end_p (bsi))
1148 	{
1149 	  gimple stmt = gsi_stmt (bsi);
1150 	  /* If we're recompiling LTO objects with debug stmts but
1151 	     we're not supposed to have debug stmts, remove them now.
1152 	     We can't remove them earlier because this would cause uid
1153 	     mismatches in fixups, but we can do it at this point, as
1154 	     long as debug stmts don't require fixups.  */
1155 	  if (!MAY_HAVE_DEBUG_STMTS && !flag_wpa && is_gimple_debug (stmt))
1156 	    {
1157 	      gimple_stmt_iterator gsi = bsi;
1158 	      gsi_next (&bsi);
1159 	      gsi_remove (&gsi, true);
1160 	    }
1161 	  else
1162 	    {
1163 	      gsi_next (&bsi);
1164 	      stmts[gimple_uid (stmt)] = stmt;
1165 	    }
1166 	}
1167     }
1168 
1169   /* Set the gimple body to the statement sequence in the entry
1170      basic block.  FIXME lto, this is fairly hacky.  The existence
1171      of a gimple body is used by the cgraph routines, but we should
1172      really use the presence of the CFG.  */
1173   {
1174     edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1175     gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1176   }
1177 
1178   fixup_call_stmt_edges (node, stmts);
1179   execute_all_ipa_stmt_fixups (node, stmts);
1180 
1181   update_ssa (TODO_update_ssa_only_virtuals);
1182   free_dominance_info (CDI_DOMINATORS);
1183   free_dominance_info (CDI_POST_DOMINATORS);
1184   free (stmts);
1185   pop_cfun ();
1186 }
1187 
1188 /* Read the body of function FN_DECL from DATA_IN using input block IB.  */
1189 
1190 static void
1191 input_constructor (tree var, struct data_in *data_in,
1192 		   struct lto_input_block *ib)
1193 {
1194   DECL_INITIAL (var) = stream_read_tree (ib, data_in);
1195 }
1196 
1197 
1198 /* Read the body from DATA for function NODE and fill it in.
1199    FILE_DATA are the global decls and types.  SECTION_TYPE is either
1200    LTO_section_function_body or LTO_section_static_initializer.  If
1201    section type is LTO_section_function_body, FN must be the decl for
1202    that function.  */
1203 
1204 static void
1205 lto_read_body_or_constructor (struct lto_file_decl_data *file_data, struct symtab_node *node,
1206 			      const char *data, enum lto_section_type section_type)
1207 {
1208   const struct lto_function_header *header;
1209   struct data_in *data_in;
1210   int cfg_offset;
1211   int main_offset;
1212   int string_offset;
1213   tree fn_decl = node->decl;
1214 
1215   header = (const struct lto_function_header *) data;
1216   if (TREE_CODE (node->decl) == FUNCTION_DECL)
1217     {
1218       cfg_offset = sizeof (struct lto_function_header);
1219       main_offset = cfg_offset + header->cfg_size;
1220       string_offset = main_offset + header->main_size;
1221     }
1222   else
1223     {
1224       main_offset = sizeof (struct lto_function_header);
1225       string_offset = main_offset + header->main_size;
1226     }
1227 
1228   data_in = lto_data_in_create (file_data, data + string_offset,
1229 			      header->string_size, vNULL);
1230 
1231   if (section_type == LTO_section_function_body)
1232     {
1233       struct lto_in_decl_state *decl_state;
1234       unsigned from;
1235 
1236       gcc_checking_assert (node);
1237 
1238       /* Use the function's decl state. */
1239       decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1240       gcc_assert (decl_state);
1241       file_data->current_decl_state = decl_state;
1242 
1243 
1244       /* Set up the struct function.  */
1245       from = data_in->reader_cache->nodes.length ();
1246       lto_input_block ib_main (data + main_offset, header->main_size,
1247 			       file_data->mode_table);
1248       if (TREE_CODE (node->decl) == FUNCTION_DECL)
1249 	{
1250 	  lto_input_block ib_cfg (data + cfg_offset, header->cfg_size,
1251 				  file_data->mode_table);
1252 	  input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1253 	}
1254       else
1255         input_constructor (fn_decl, data_in, &ib_main);
1256       data_in->location_cache.apply_location_cache ();
1257       /* And fixup types we streamed locally.  */
1258 	{
1259 	  struct streamer_tree_cache_d *cache = data_in->reader_cache;
1260 	  unsigned len = cache->nodes.length ();
1261 	  unsigned i;
1262 	  for (i = len; i-- > from;)
1263 	    {
1264 	      tree t = streamer_tree_cache_get_tree (cache, i);
1265 	      if (t == NULL_TREE)
1266 		continue;
1267 
1268 	      if (TYPE_P (t))
1269 		{
1270 		  gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1271 		  TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1272 		  if (TYPE_MAIN_VARIANT (t) != t)
1273 		    {
1274 		      gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1275 		      TYPE_NEXT_VARIANT (t)
1276 			= TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1277 		      TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1278 		    }
1279 		}
1280 	    }
1281 	}
1282 
1283       /* Restore decl state */
1284       file_data->current_decl_state = file_data->global_decl_state;
1285     }
1286 
1287   lto_data_in_delete (data_in);
1288 }
1289 
1290 
1291 /* Read the body of NODE using DATA.  FILE_DATA holds the global
1292    decls and types.  */
1293 
1294 void
1295 lto_input_function_body (struct lto_file_decl_data *file_data,
1296 			 struct cgraph_node *node, const char *data)
1297 {
1298   lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1299 }
1300 
1301 /* Read the body of NODE using DATA.  FILE_DATA holds the global
1302    decls and types.  */
1303 
1304 void
1305 lto_input_variable_constructor (struct lto_file_decl_data *file_data,
1306 				struct varpool_node *node, const char *data)
1307 {
1308   lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1309 }
1310 
1311 
1312 /* Read the physical representation of a tree node EXPR from
1313    input block IB using the per-file context in DATA_IN.  */
1314 
1315 static void
1316 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1317 {
1318   /* Read all the bitfield values in EXPR.  Note that for LTO, we
1319      only write language-independent bitfields, so no more unpacking is
1320      needed.  */
1321   streamer_read_tree_bitfields (ib, data_in, expr);
1322 
1323   /* Read all the pointer fields in EXPR.  */
1324   streamer_read_tree_body (ib, data_in, expr);
1325 
1326   /* Read any LTO-specific data not read by the tree streamer.  */
1327   if (DECL_P (expr)
1328       && TREE_CODE (expr) != FUNCTION_DECL
1329       && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1330     DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1331 
1332   /* We should never try to instantiate an MD or NORMAL builtin here.  */
1333   if (TREE_CODE (expr) == FUNCTION_DECL)
1334     gcc_assert (!streamer_handle_as_builtin_p (expr));
1335 
1336 #ifdef LTO_STREAMER_DEBUG
1337   /* Remove the mapping to RESULT's original address set by
1338      streamer_alloc_tree.  */
1339   lto_orig_address_remove (expr);
1340 #endif
1341 }
1342 
1343 /* Read the physical representation of a tree node with tag TAG from
1344    input block IB using the per-file context in DATA_IN.  */
1345 
1346 static tree
1347 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1348 	       enum LTO_tags tag, hashval_t hash)
1349 {
1350   /* Instantiate a new tree node.  */
1351   tree result = streamer_alloc_tree (ib, data_in, tag);
1352 
1353   /* Enter RESULT in the reader cache.  This will make RESULT
1354      available so that circular references in the rest of the tree
1355      structure can be resolved in subsequent calls to stream_read_tree.  */
1356   streamer_tree_cache_append (data_in->reader_cache, result, hash);
1357 
1358   lto_read_tree_1 (ib, data_in, result);
1359 
1360   /* end_marker = */ streamer_read_uchar (ib);
1361 
1362   return result;
1363 }
1364 
1365 
1366 /* Populate the reader cache with trees materialized from the SCC
1367    following in the IB, DATA_IN stream.  */
1368 
1369 hashval_t
1370 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1371 	       unsigned *len, unsigned *entry_len)
1372 {
1373   /* A blob of unnamed tree nodes, fill the cache from it and
1374      recurse.  */
1375   unsigned size = streamer_read_uhwi (ib);
1376   hashval_t scc_hash = streamer_read_uhwi (ib);
1377   unsigned scc_entry_len = 1;
1378 
1379   if (size == 1)
1380     {
1381       enum LTO_tags tag = streamer_read_record_start (ib);
1382       lto_input_tree_1 (ib, data_in, tag, scc_hash);
1383     }
1384   else
1385     {
1386       unsigned int first = data_in->reader_cache->nodes.length ();
1387       tree result;
1388 
1389       scc_entry_len = streamer_read_uhwi (ib);
1390 
1391       /* Materialize size trees by reading their headers.  */
1392       for (unsigned i = 0; i < size; ++i)
1393 	{
1394 	  enum LTO_tags tag = streamer_read_record_start (ib);
1395 	  if (tag == LTO_null
1396 	      || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1397 	      || tag == LTO_tree_pickle_reference
1398 	      || tag == LTO_builtin_decl
1399 	      || tag == LTO_integer_cst
1400 	      || tag == LTO_tree_scc)
1401 	    gcc_unreachable ();
1402 
1403 	  result = streamer_alloc_tree (ib, data_in, tag);
1404 	  streamer_tree_cache_append (data_in->reader_cache, result, 0);
1405 	}
1406 
1407       /* Read the tree bitpacks and references.  */
1408       for (unsigned i = 0; i < size; ++i)
1409 	{
1410 	  result = streamer_tree_cache_get_tree (data_in->reader_cache,
1411 						 first + i);
1412 	  lto_read_tree_1 (ib, data_in, result);
1413 	  /* end_marker = */ streamer_read_uchar (ib);
1414 	}
1415     }
1416 
1417   *len = size;
1418   *entry_len = scc_entry_len;
1419   return scc_hash;
1420 }
1421 
1422 
1423 /* Read a tree from input block IB using the per-file context in
1424    DATA_IN.  This context is used, for example, to resolve references
1425    to previously read nodes.  */
1426 
1427 tree
1428 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1429 		  enum LTO_tags tag, hashval_t hash)
1430 {
1431   tree result;
1432 
1433   gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1434 
1435   if (tag == LTO_null)
1436     result = NULL_TREE;
1437   else if (tag >= LTO_field_decl_ref && tag <= LTO_namelist_decl_ref)
1438     {
1439       /* If TAG is a reference to an indexable tree, the next value
1440 	 in IB is the index into the table where we expect to find
1441 	 that tree.  */
1442       result = lto_input_tree_ref (ib, data_in, cfun, tag);
1443     }
1444   else if (tag == LTO_tree_pickle_reference)
1445     {
1446       /* If TAG is a reference to a previously read tree, look it up in
1447 	 the reader cache.  */
1448       result = streamer_get_pickled_tree (ib, data_in);
1449     }
1450   else if (tag == LTO_builtin_decl)
1451     {
1452       /* If we are going to read a built-in function, all we need is
1453 	 the code and class.  */
1454       result = streamer_get_builtin_tree (ib, data_in);
1455     }
1456   else if (tag == LTO_integer_cst)
1457     {
1458       /* For shared integer constants in singletons we can use the
1459          existing tree integer constant merging code.  */
1460       tree type = stream_read_tree (ib, data_in);
1461       unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
1462       unsigned HOST_WIDE_INT i;
1463       HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
1464 
1465       for (i = 0; i < len; i++)
1466 	a[i] = streamer_read_hwi (ib);
1467       gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
1468       result = wide_int_to_tree (type, wide_int::from_array
1469 				 (a, len, TYPE_PRECISION (type)));
1470       streamer_tree_cache_append (data_in->reader_cache, result, hash);
1471     }
1472   else if (tag == LTO_tree_scc)
1473     gcc_unreachable ();
1474   else
1475     {
1476       /* Otherwise, materialize a new node from IB.  */
1477       result = lto_read_tree (ib, data_in, tag, hash);
1478     }
1479 
1480   return result;
1481 }
1482 
1483 tree
1484 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1485 {
1486   enum LTO_tags tag;
1487 
1488   /* Input and skip SCCs.  */
1489   while ((tag = streamer_read_record_start (ib)) == LTO_tree_scc)
1490     {
1491       unsigned len, entry_len;
1492       lto_input_scc (ib, data_in, &len, &entry_len);
1493     }
1494   return lto_input_tree_1 (ib, data_in, tag, 0);
1495 }
1496 
1497 
1498 /* Input toplevel asms.  */
1499 
1500 void
1501 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1502 {
1503   size_t len;
1504   const char *data = lto_get_section_data (file_data, LTO_section_asm,
1505 					   NULL, &len);
1506   const struct lto_simple_header_with_strings *header
1507     = (const struct lto_simple_header_with_strings *) data;
1508   int string_offset;
1509   struct data_in *data_in;
1510   tree str;
1511 
1512   if (! data)
1513     return;
1514 
1515   string_offset = sizeof (*header) + header->main_size;
1516 
1517   lto_input_block ib (data + sizeof (*header), header->main_size,
1518 		      file_data->mode_table);
1519 
1520   data_in = lto_data_in_create (file_data, data + string_offset,
1521 			      header->string_size, vNULL);
1522 
1523   while ((str = streamer_read_string_cst (data_in, &ib)))
1524     {
1525       asm_node *node = symtab->finalize_toplevel_asm (str);
1526       node->order = streamer_read_hwi (&ib) + order_base;
1527       if (node->order >= symtab->order)
1528 	symtab->order = node->order + 1;
1529     }
1530 
1531   lto_data_in_delete (data_in);
1532 
1533   lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1534 }
1535 
1536 
1537 /* Input mode table.  */
1538 
1539 void
1540 lto_input_mode_table (struct lto_file_decl_data *file_data)
1541 {
1542   size_t len;
1543   const char *data = lto_get_section_data (file_data, LTO_section_mode_table,
1544 					   NULL, &len);
1545   if (! data)
1546     {
1547       internal_error ("cannot read LTO mode table from %s",
1548 		      file_data->file_name);
1549       return;
1550     }
1551 
1552   unsigned char *table = ggc_cleared_vec_alloc<unsigned char> (1 << 8);
1553   file_data->mode_table = table;
1554   const struct lto_simple_header_with_strings *header
1555     = (const struct lto_simple_header_with_strings *) data;
1556   int string_offset;
1557   struct data_in *data_in;
1558   string_offset = sizeof (*header) + header->main_size;
1559 
1560   lto_input_block ib (data + sizeof (*header), header->main_size, NULL);
1561   data_in = lto_data_in_create (file_data, data + string_offset,
1562 				header->string_size, vNULL);
1563   bitpack_d bp = streamer_read_bitpack (&ib);
1564 
1565   table[VOIDmode] = VOIDmode;
1566   table[BLKmode] = BLKmode;
1567   unsigned int m;
1568   while ((m = bp_unpack_value (&bp, 8)) != VOIDmode)
1569     {
1570       enum mode_class mclass
1571 	= bp_unpack_enum (&bp, mode_class, MAX_MODE_CLASS);
1572       unsigned int size = bp_unpack_value (&bp, 8);
1573       unsigned int prec = bp_unpack_value (&bp, 16);
1574       machine_mode inner = (machine_mode) table[bp_unpack_value (&bp, 8)];
1575       unsigned int nunits = bp_unpack_value (&bp, 8);
1576       unsigned int ibit = 0, fbit = 0;
1577       unsigned int real_fmt_len = 0;
1578       const char *real_fmt_name = NULL;
1579       switch (mclass)
1580 	{
1581 	case MODE_FRACT:
1582 	case MODE_UFRACT:
1583 	case MODE_ACCUM:
1584 	case MODE_UACCUM:
1585 	  ibit = bp_unpack_value (&bp, 8);
1586 	  fbit = bp_unpack_value (&bp, 8);
1587 	  break;
1588 	case MODE_FLOAT:
1589 	case MODE_DECIMAL_FLOAT:
1590 	  real_fmt_name = bp_unpack_indexed_string (data_in, &bp,
1591 						    &real_fmt_len);
1592 	  break;
1593 	default:
1594 	  break;
1595 	}
1596       /* First search just the GET_CLASS_NARROWEST_MODE to wider modes,
1597 	 if not found, fallback to all modes.  */
1598       int pass;
1599       for (pass = 0; pass < 2; pass++)
1600 	for (machine_mode mr = pass ? VOIDmode
1601 				    : GET_CLASS_NARROWEST_MODE (mclass);
1602 	     pass ? mr < MAX_MACHINE_MODE : mr != VOIDmode;
1603 	     pass ? mr = (machine_mode) (m + 1)
1604 		  : mr = GET_MODE_WIDER_MODE (mr))
1605 	  if (GET_MODE_CLASS (mr) != mclass
1606 	      || GET_MODE_SIZE (mr) != size
1607 	      || GET_MODE_PRECISION (mr) != prec
1608 	      || GET_MODE_INNER (mr) != inner
1609 	      || GET_MODE_IBIT (mr) != ibit
1610 	      || GET_MODE_FBIT (mr) != fbit
1611 	      || GET_MODE_NUNITS (mr) != nunits)
1612 	    continue;
1613 	  else if ((mclass == MODE_FLOAT || mclass == MODE_DECIMAL_FLOAT)
1614 		   && strcmp (REAL_MODE_FORMAT (mr)->name, real_fmt_name) != 0)
1615 	    continue;
1616 	  else
1617 	    {
1618 	      table[m] = mr;
1619 	      pass = 2;
1620 	      break;
1621 	    }
1622       unsigned int mname_len;
1623       const char *mname = bp_unpack_indexed_string (data_in, &bp, &mname_len);
1624       if (pass == 2)
1625 	{
1626 	  switch (mclass)
1627 	    {
1628 	    case MODE_VECTOR_INT:
1629 	    case MODE_VECTOR_FLOAT:
1630 	    case MODE_VECTOR_FRACT:
1631 	    case MODE_VECTOR_UFRACT:
1632 	    case MODE_VECTOR_ACCUM:
1633 	    case MODE_VECTOR_UACCUM:
1634 	      /* For unsupported vector modes just use BLKmode,
1635 		 if the scalar mode is supported.  */
1636 	      if (inner != VOIDmode)
1637 		{
1638 		  table[m] = BLKmode;
1639 		  break;
1640 		}
1641 	      /* FALLTHRU */
1642 	    default:
1643 	      fatal_error (UNKNOWN_LOCATION, "unsupported mode %s\n", mname);
1644 	      break;
1645 	    }
1646 	}
1647     }
1648   lto_data_in_delete (data_in);
1649 
1650   lto_free_section_data (file_data, LTO_section_mode_table, NULL, data, len);
1651 }
1652 
1653 
1654 /* Initialization for the LTO reader.  */
1655 
1656 void
1657 lto_reader_init (void)
1658 {
1659   lto_streamer_init ();
1660   file_name_hash_table
1661     = new hash_table<freeing_string_slot_hasher> (37);
1662 }
1663 
1664 
1665 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1666    table to use with LEN strings.  RESOLUTIONS is the vector of linker
1667    resolutions (NULL if not using a linker plugin).  */
1668 
1669 struct data_in *
1670 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1671 		    unsigned len,
1672 		    vec<ld_plugin_symbol_resolution_t> resolutions)
1673 {
1674   struct data_in *data_in = new (struct data_in);
1675   data_in->file_data = file_data;
1676   data_in->strings = strings;
1677   data_in->strings_len = len;
1678   data_in->globals_resolution = resolutions;
1679   data_in->reader_cache = streamer_tree_cache_create (false, false, true);
1680   return data_in;
1681 }
1682 
1683 
1684 /* Remove DATA_IN.  */
1685 
1686 void
1687 lto_data_in_delete (struct data_in *data_in)
1688 {
1689   data_in->globals_resolution.release ();
1690   streamer_tree_cache_delete (data_in->reader_cache);
1691   delete data_in;
1692 }
1693