OSDN Git Service

* tree.h (TYPE_ARTIFICIAL): New flag.
[pf3gnuchains/gcc-fork.git] / gcc / lto-streamer-in.c
1 /* Read the GIMPLE representation from a file stream.
2
3    Copyright 2009, 2010 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 "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "tree-pass.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic.h"
41 #include "libfuncs.h"
42 #include "except.h"
43 #include "debug.h"
44 #include "vec.h"
45 #include "timevar.h"
46 #include "output.h"
47 #include "ipa-utils.h"
48 #include "lto-streamer.h"
49 #include "tree-pass.h"
50
51 /* Data structure used to hash file names in the source_location field.  */
52 struct string_slot
53 {
54   const char *s;
55   unsigned int slot_num;
56 };
57
58 /* The table to hold the file names.  */
59 static htab_t file_name_hash_table;
60
61
62 /* Check that tag ACTUAL has one of the given values.  NUM_TAGS is the
63    number of valid tag values to check.  */
64
65 static void
66 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
67 {
68   va_list ap;
69   int i;
70
71   va_start (ap, ntags);
72   for (i = 0; i < ntags; i++)
73     if ((unsigned) actual == va_arg (ap, unsigned))
74       {
75         va_end (ap);
76         return;
77       }
78
79   va_end (ap);
80   internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
81 }
82
83
84 /* Check that tag ACTUAL is in the range [TAG1, TAG2].  */
85
86 static void
87 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
88                      enum LTO_tags tag2)
89 {
90   if (actual < tag1 || actual > tag2)
91     internal_error ("bytecode stream: tag %s is not in the expected range "
92                     "[%s, %s]",
93                     lto_tag_name (actual),
94                     lto_tag_name (tag1),
95                     lto_tag_name (tag2));
96 }
97
98
99 /* Check that tag ACTUAL == EXPECTED.  */
100
101 static void
102 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
103 {
104   if (actual != expected)
105     internal_error ("bytecode stream: expected tag %s instead of %s",
106                     lto_tag_name (expected), lto_tag_name (actual));
107 }
108
109
110 /* Return a hash code for P.  */
111
112 static hashval_t
113 hash_string_slot_node (const void *p)
114 {
115   const struct string_slot *ds = (const struct string_slot *) p;
116   return (hashval_t) htab_hash_string (ds->s);
117 }
118
119
120 /* Returns nonzero if P1 and P2 are equal.  */
121
122 static int
123 eq_string_slot_node (const void *p1, const void *p2)
124 {
125   const struct string_slot *ds1 = (const struct string_slot *) p1;
126   const struct string_slot *ds2 = (const struct string_slot *) p2;
127   return strcmp (ds1->s, ds2->s) == 0;
128 }
129
130
131 /* Read a string from the string table in DATA_IN using input block
132    IB.  Write the length to RLEN.  */
133
134 static const char *
135 string_for_index (struct data_in *data_in,
136                   unsigned int loc,
137                   unsigned int *rlen)
138 {
139   struct lto_input_block str_tab;
140   unsigned int len;
141   const char *result;
142
143   if (!loc)
144     {
145       *rlen = 0;
146       return NULL;
147     }
148
149   /* Get the string stored at location LOC in DATA_IN->STRINGS.  */
150   LTO_INIT_INPUT_BLOCK (str_tab, data_in->strings, loc - 1, data_in->strings_len);
151   len = lto_input_uleb128 (&str_tab);
152   *rlen = len;
153
154   if (str_tab.p + len > data_in->strings_len)
155     internal_error ("bytecode stream: string too long for the string table");
156
157   result = (const char *)(data_in->strings + str_tab.p);
158
159   return result;
160 }
161
162
163 /* Read a string from the string table in DATA_IN using input block
164    IB.  Write the length to RLEN.  */
165
166 static const char *
167 input_string_internal (struct data_in *data_in, struct lto_input_block *ib,
168                        unsigned int *rlen)
169 {
170   return string_for_index (data_in, lto_input_uleb128 (ib), rlen);
171 }
172
173
174 /* Read a STRING_CST from the string table in DATA_IN using input
175    block IB.  */
176
177 static tree
178 input_string_cst (struct data_in *data_in, struct lto_input_block *ib)
179 {
180   unsigned int len;
181   const char * ptr;
182
183   ptr = input_string_internal (data_in, ib, &len);
184   if (!ptr)
185     return NULL;
186   return build_string (len, ptr);
187 }
188
189
190 /* Read an IDENTIFIER from the string table in DATA_IN using input
191    block IB.  */
192
193 static tree
194 input_identifier (struct data_in *data_in, struct lto_input_block *ib)
195 {
196   unsigned int len;
197   const char *ptr;
198
199   ptr = input_string_internal (data_in, ib, &len);
200   if (!ptr)
201     return NULL;
202   return get_identifier_with_length (ptr, len);
203 }
204
205
206 /* Read LENGTH bytes from STREAM to ADDR.  */
207
208 void
209 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
210 {
211   size_t i;
212   unsigned char *const buffer = (unsigned char *const) addr;
213
214   for (i = 0; i < length; i++)
215     buffer[i] = lto_input_1_unsigned (ib);
216 }
217
218
219 /* Read a NULL terminated string from the string table in DATA_IN.  */
220
221 const char *
222 lto_input_string (struct data_in *data_in, struct lto_input_block *ib)
223 {
224   unsigned int len;
225   const char *ptr;
226
227   ptr = input_string_internal (data_in, ib, &len);
228   if (!ptr)
229     return NULL;
230   if (ptr[len - 1] != '\0')
231     internal_error ("bytecode stream: found non-null terminated string");
232
233   return ptr;
234 }
235
236
237 /* Return the next tag in the input block IB.  */
238
239 static inline enum LTO_tags
240 input_record_start (struct lto_input_block *ib)
241 {
242   return lto_input_enum (ib, LTO_tags, LTO_NUM_TAGS);
243 }
244
245
246 /* Lookup STRING in file_name_hash_table.  If found, return the existing
247    string, otherwise insert STRING as the canonical version.  */
248
249 static const char *
250 canon_file_name (const char *string)
251 {
252   void **slot;
253   struct string_slot s_slot;
254   s_slot.s = string;
255
256   slot = htab_find_slot (file_name_hash_table, &s_slot, INSERT);
257   if (*slot == NULL)
258     {
259       size_t len;
260       char *saved_string;
261       struct string_slot *new_slot;
262
263       len = strlen (string);
264       saved_string = (char *) xmalloc (len + 1);
265       new_slot = XCNEW (struct string_slot);
266       strcpy (saved_string, string);
267       new_slot->s = saved_string;
268       *slot = new_slot;
269       return saved_string;
270     }
271   else
272     {
273       struct string_slot *old_slot = (struct string_slot *) *slot;
274       return old_slot->s;
275     }
276 }
277
278
279 /* Clear the line info stored in DATA_IN.  */
280
281 static void
282 clear_line_info (struct data_in *data_in)
283 {
284   if (data_in->current_file)
285     linemap_add (line_table, LC_LEAVE, false, NULL, 0);
286   data_in->current_file = NULL;
287   data_in->current_line = 0;
288   data_in->current_col = 0;
289 }
290
291
292 /* Read a location bitpack from input block IB.  */
293
294 static location_t
295 lto_input_location_bitpack (struct data_in *data_in, struct bitpack_d *bp)
296 {
297   bool file_change, line_change, column_change;
298   unsigned len;
299   bool prev_file = data_in->current_file != NULL;
300
301   if (bp_unpack_value (bp, 1))
302     return UNKNOWN_LOCATION;
303
304   file_change = bp_unpack_value (bp, 1);
305   if (file_change)
306     data_in->current_file = canon_file_name
307                               (string_for_index (data_in,
308                                                  bp_unpack_var_len_unsigned (bp),
309                                                  &len));
310
311   line_change = bp_unpack_value (bp, 1);
312   if (line_change)
313     data_in->current_line = bp_unpack_var_len_unsigned (bp);
314
315   column_change = bp_unpack_value (bp, 1);
316   if (column_change)
317     data_in->current_col = bp_unpack_var_len_unsigned (bp);
318
319   if (file_change)
320     {
321       if (prev_file)
322         linemap_add (line_table, LC_LEAVE, false, NULL, 0);
323
324       linemap_add (line_table, LC_ENTER, false, data_in->current_file,
325                    data_in->current_line);
326     }
327   else if (line_change)
328     linemap_line_start (line_table, data_in->current_line, data_in->current_col);
329
330   return linemap_position_for_column (line_table, data_in->current_col);
331 }
332
333
334 /* Read a location from input block IB.  */
335
336 static location_t
337 lto_input_location (struct lto_input_block *ib, struct data_in *data_in)
338 {
339   struct bitpack_d bp;
340
341   bp = lto_input_bitpack (ib);
342   return lto_input_location_bitpack (data_in, &bp);
343 }
344
345
346 /* Read a reference to a tree node from DATA_IN using input block IB.
347    TAG is the expected node that should be found in IB, if TAG belongs
348    to one of the indexable trees, expect to read a reference index to
349    be looked up in one of the symbol tables, otherwise read the pysical
350    representation of the tree using lto_input_tree.  FN is the
351    function scope for the read tree.  */
352
353 static tree
354 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
355                     struct function *fn, enum LTO_tags tag)
356 {
357   unsigned HOST_WIDE_INT ix_u;
358   tree result = NULL_TREE;
359
360   lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
361
362   switch (tag)
363     {
364     case LTO_type_ref:
365       ix_u = lto_input_uleb128 (ib);
366       result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
367       break;
368
369     case LTO_ssa_name_ref:
370       ix_u = lto_input_uleb128 (ib);
371       result = VEC_index (tree, SSANAMES (fn), ix_u);
372       break;
373
374     case LTO_field_decl_ref:
375       ix_u = lto_input_uleb128 (ib);
376       result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
377       break;
378
379     case LTO_function_decl_ref:
380       ix_u = lto_input_uleb128 (ib);
381       result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
382       break;
383
384     case LTO_type_decl_ref:
385       ix_u = lto_input_uleb128 (ib);
386       result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
387       break;
388
389     case LTO_namespace_decl_ref:
390       ix_u = lto_input_uleb128 (ib);
391       result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
392       break;
393
394     case LTO_global_decl_ref:
395     case LTO_result_decl_ref:
396     case LTO_const_decl_ref:
397     case LTO_imported_decl_ref:
398     case LTO_label_decl_ref:
399     case LTO_translation_unit_decl_ref:
400       ix_u = lto_input_uleb128 (ib);
401       result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
402       break;
403
404     default:
405       gcc_unreachable ();
406     }
407
408   gcc_assert (result);
409
410   return result;
411 }
412
413
414 /* Read a chain of tree nodes from input block IB. DATA_IN contains
415    tables and descriptors for the file being read.  */
416
417 static tree
418 lto_input_chain (struct lto_input_block *ib, struct data_in *data_in)
419 {
420   int i, count;
421   tree first, prev, curr;
422
423   first = prev = NULL_TREE;
424   count = lto_input_sleb128 (ib);
425   for (i = 0; i < count; i++)
426     {
427       curr = lto_input_tree (ib, data_in);
428       if (prev)
429         TREE_CHAIN (prev) = curr;
430       else
431         first = curr;
432
433       TREE_CHAIN (curr) = NULL_TREE;
434       prev = curr;
435     }
436
437   return first;
438 }
439
440
441 /* Read and return a double-linked list of catch handlers from input
442    block IB, using descriptors in DATA_IN.  */
443
444 static struct eh_catch_d *
445 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
446                          eh_catch *last_p)
447 {
448   eh_catch first;
449   enum LTO_tags tag;
450
451   *last_p = first = NULL;
452   tag = input_record_start (ib);
453   while (tag)
454     {
455       tree list;
456       eh_catch n;
457
458       lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
459
460       /* Read the catch node.  */
461       n = ggc_alloc_cleared_eh_catch_d ();
462       n->type_list = lto_input_tree (ib, data_in);
463       n->filter_list = lto_input_tree (ib, data_in);
464       n->label = lto_input_tree (ib, data_in);
465
466       /* Register all the types in N->FILTER_LIST.  */
467       for (list = n->filter_list; list; list = TREE_CHAIN (list))
468         add_type_for_runtime (TREE_VALUE (list));
469
470       /* Chain N to the end of the list.  */
471       if (*last_p)
472         (*last_p)->next_catch = n;
473       n->prev_catch = *last_p;
474       *last_p = n;
475
476       /* Set the head of the list the first time through the loop.  */
477       if (first == NULL)
478         first = n;
479
480       tag = input_record_start (ib);
481     }
482
483   return first;
484 }
485
486
487 /* Read and return EH region IX from input block IB, using descriptors
488    in DATA_IN.  */
489
490 static eh_region
491 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
492 {
493   enum LTO_tags tag;
494   eh_region r;
495
496   /* Read the region header.  */
497   tag = input_record_start (ib);
498   if (tag == LTO_null)
499     return NULL;
500
501   r = ggc_alloc_cleared_eh_region_d ();
502   r->index = lto_input_sleb128 (ib);
503
504   gcc_assert (r->index == ix);
505
506   /* Read all the region pointers as region numbers.  We'll fix up
507      the pointers once the whole array has been read.  */
508   r->outer = (eh_region) (intptr_t) lto_input_sleb128 (ib);
509   r->inner = (eh_region) (intptr_t) lto_input_sleb128 (ib);
510   r->next_peer = (eh_region) (intptr_t) lto_input_sleb128 (ib);
511
512   switch (tag)
513     {
514       case LTO_ert_cleanup:
515         r->type = ERT_CLEANUP;
516         break;
517
518       case LTO_ert_try:
519         {
520           struct eh_catch_d *last_catch;
521           r->type = ERT_TRY;
522           r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
523                                                              &last_catch);
524           r->u.eh_try.last_catch = last_catch;
525           break;
526         }
527
528       case LTO_ert_allowed_exceptions:
529         {
530           tree l;
531
532           r->type = ERT_ALLOWED_EXCEPTIONS;
533           r->u.allowed.type_list = lto_input_tree (ib, data_in);
534           r->u.allowed.label = lto_input_tree (ib, data_in);
535           r->u.allowed.filter = lto_input_uleb128 (ib);
536
537           for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
538             add_type_for_runtime (TREE_VALUE (l));
539         }
540         break;
541
542       case LTO_ert_must_not_throw:
543         r->type = ERT_MUST_NOT_THROW;
544         r->u.must_not_throw.failure_decl = lto_input_tree (ib, data_in);
545         r->u.must_not_throw.failure_loc = lto_input_location (ib, data_in);
546         break;
547
548       default:
549         gcc_unreachable ();
550     }
551
552   r->landing_pads = (eh_landing_pad) (intptr_t) lto_input_sleb128 (ib);
553
554   return r;
555 }
556
557
558 /* Read and return EH landing pad IX from input block IB, using descriptors
559    in DATA_IN.  */
560
561 static eh_landing_pad
562 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
563 {
564   enum LTO_tags tag;
565   eh_landing_pad lp;
566
567   /* Read the landing pad header.  */
568   tag = input_record_start (ib);
569   if (tag == LTO_null)
570     return NULL;
571
572   lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
573
574   lp = ggc_alloc_cleared_eh_landing_pad_d ();
575   lp->index = lto_input_sleb128 (ib);
576   gcc_assert (lp->index == ix);
577   lp->next_lp = (eh_landing_pad) (intptr_t) lto_input_sleb128 (ib);
578   lp->region = (eh_region) (intptr_t) lto_input_sleb128 (ib);
579   lp->post_landing_pad = lto_input_tree (ib, data_in);
580
581   return lp;
582 }
583
584
585 /* After reading the EH regions, pointers to peer and children regions
586    are region numbers.  This converts all these region numbers into
587    real pointers into the rematerialized regions for FN.  ROOT_REGION
588    is the region number for the root EH region in FN.  */
589
590 static void
591 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
592 {
593   unsigned i;
594   VEC(eh_region,gc) *eh_array = fn->eh->region_array;
595   VEC(eh_landing_pad,gc) *lp_array = fn->eh->lp_array;
596   eh_region r;
597   eh_landing_pad lp;
598
599   gcc_assert (eh_array && lp_array);
600
601   gcc_assert (root_region >= 0);
602   fn->eh->region_tree = VEC_index (eh_region, eh_array, root_region);
603
604 #define FIXUP_EH_REGION(r) (r) = VEC_index (eh_region, eh_array, \
605                                             (HOST_WIDE_INT) (intptr_t) (r))
606 #define FIXUP_EH_LP(p) (p) = VEC_index (eh_landing_pad, lp_array, \
607                                         (HOST_WIDE_INT) (intptr_t) (p))
608
609   /* Convert all the index numbers stored in pointer fields into
610      pointers to the corresponding slots in the EH region array.  */
611   FOR_EACH_VEC_ELT (eh_region, eh_array, i, r)
612     {
613       /* The array may contain NULL regions.  */
614       if (r == NULL)
615         continue;
616
617       gcc_assert (i == (unsigned) r->index);
618       FIXUP_EH_REGION (r->outer);
619       FIXUP_EH_REGION (r->inner);
620       FIXUP_EH_REGION (r->next_peer);
621       FIXUP_EH_LP (r->landing_pads);
622     }
623
624   /* Convert all the index numbers stored in pointer fields into
625      pointers to the corresponding slots in the EH landing pad array.  */
626   FOR_EACH_VEC_ELT (eh_landing_pad, lp_array, i, lp)
627     {
628       /* The array may contain NULL landing pads.  */
629       if (lp == NULL)
630         continue;
631
632       gcc_assert (i == (unsigned) lp->index);
633       FIXUP_EH_LP (lp->next_lp);
634       FIXUP_EH_REGION (lp->region);
635     }
636
637 #undef FIXUP_EH_REGION
638 #undef FIXUP_EH_LP
639 }
640
641
642 /* Initialize EH support.  */
643
644 static void
645 lto_init_eh (void)
646 {
647   static bool eh_initialized_p = false;
648
649   if (eh_initialized_p)
650     return;
651
652   /* Contrary to most other FEs, we only initialize EH support when at
653      least one of the files in the set contains exception regions in
654      it.  Since this happens much later than the call to init_eh in
655      lang_dependent_init, we have to set flag_exceptions and call
656      init_eh again to initialize the EH tables.  */
657   flag_exceptions = 1;
658   init_eh ();
659
660   /* Initialize dwarf2 tables.  Since dwarf2out_do_frame() returns
661      true only when exceptions are enabled, this initialization is
662      never done during lang_dependent_init.  */
663 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
664   if (dwarf2out_do_frame ())
665     dwarf2out_frame_init ();
666 #endif
667
668   eh_initialized_p = true;
669 }
670
671
672 /* Read the exception table for FN from IB using the data descriptors
673    in DATA_IN.  */
674
675 static void
676 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
677                   struct function *fn)
678 {
679   HOST_WIDE_INT i, root_region, len;
680   enum LTO_tags tag;
681
682   tag = input_record_start (ib);
683   if (tag == LTO_null)
684     return;
685
686   lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
687
688   /* If the file contains EH regions, then it was compiled with
689      -fexceptions.  In that case, initialize the backend EH
690      machinery.  */
691   lto_init_eh ();
692
693   gcc_assert (fn->eh);
694
695   root_region = lto_input_sleb128 (ib);
696   gcc_assert (root_region == (int) root_region);
697
698   /* Read the EH region array.  */
699   len = lto_input_sleb128 (ib);
700   gcc_assert (len == (int) len);
701   if (len > 0)
702     {
703       VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
704       for (i = 0; i < len; i++)
705         {
706           eh_region r = input_eh_region (ib, data_in, i);
707           VEC_replace (eh_region, fn->eh->region_array, i, r);
708         }
709     }
710
711   /* Read the landing pads.  */
712   len = lto_input_sleb128 (ib);
713   gcc_assert (len == (int) len);
714   if (len > 0)
715     {
716       VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
717       for (i = 0; i < len; i++)
718         {
719           eh_landing_pad lp = input_eh_lp (ib, data_in, i);
720           VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
721         }
722     }
723
724   /* Read the runtime type data.  */
725   len = lto_input_sleb128 (ib);
726   gcc_assert (len == (int) len);
727   if (len > 0)
728     {
729       VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
730       for (i = 0; i < len; i++)
731         {
732           tree ttype = lto_input_tree (ib, data_in);
733           VEC_replace (tree, fn->eh->ttype_data, i, ttype);
734         }
735     }
736
737   /* Read the table of action chains.  */
738   len = lto_input_sleb128 (ib);
739   gcc_assert (len == (int) len);
740   if (len > 0)
741     {
742       if (targetm.arm_eabi_unwinder)
743         {
744           VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
745           for (i = 0; i < len; i++)
746             {
747               tree t = lto_input_tree (ib, data_in);
748               VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
749             }
750         }
751       else
752         {
753           VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
754           for (i = 0; i < len; i++)
755             {
756               uchar c = lto_input_1_unsigned (ib);
757               VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
758             }
759         }
760     }
761
762   /* Reconstruct the EH region tree by fixing up the peer/children
763      pointers.  */
764   fixup_eh_region_pointers (fn, root_region);
765
766   tag = input_record_start (ib);
767   lto_tag_check_range (tag, LTO_null, LTO_null);
768 }
769
770
771 /* Make a new basic block with index INDEX in function FN.  */
772
773 static basic_block
774 make_new_block (struct function *fn, unsigned int index)
775 {
776   basic_block bb = alloc_block ();
777   bb->index = index;
778   SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
779   bb->il.gimple = ggc_alloc_cleared_gimple_bb_info ();
780   n_basic_blocks_for_function (fn)++;
781   bb->flags = 0;
782   set_bb_seq (bb, gimple_seq_alloc ());
783   return bb;
784 }
785
786
787 /* Read the CFG for function FN from input block IB.  */
788
789 static void
790 input_cfg (struct lto_input_block *ib, struct function *fn,
791            int count_materialization_scale)
792 {
793   unsigned int bb_count;
794   basic_block p_bb;
795   unsigned int i;
796   int index;
797
798   init_empty_tree_cfg_for_function (fn);
799   init_ssa_operands ();
800
801   profile_status_for_function (fn) = lto_input_enum (ib, profile_status_d, PROFILE_LAST);
802
803   bb_count = lto_input_uleb128 (ib);
804
805   last_basic_block_for_function (fn) = bb_count;
806   if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
807     VEC_safe_grow_cleared (basic_block, gc,
808                            basic_block_info_for_function (fn), bb_count);
809
810   if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
811     VEC_safe_grow_cleared (basic_block, gc,
812                            label_to_block_map_for_function (fn), bb_count);
813
814   index = lto_input_sleb128 (ib);
815   while (index != -1)
816     {
817       basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
818       unsigned int edge_count;
819
820       if (bb == NULL)
821         bb = make_new_block (fn, index);
822
823       edge_count = lto_input_uleb128 (ib);
824
825       /* Connect up the CFG.  */
826       for (i = 0; i < edge_count; i++)
827         {
828           unsigned int dest_index;
829           unsigned int edge_flags;
830           basic_block dest;
831           int probability;
832           gcov_type count;
833           edge e;
834
835           dest_index = lto_input_uleb128 (ib);
836           probability = (int) lto_input_sleb128 (ib);
837           count = ((gcov_type) lto_input_sleb128 (ib) * count_materialization_scale
838                    + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
839           edge_flags = lto_input_uleb128 (ib);
840
841           dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
842
843           if (dest == NULL)
844             dest = make_new_block (fn, dest_index);
845
846           e = make_edge (bb, dest, edge_flags);
847           e->probability = probability;
848           e->count = count;
849         }
850
851       index = lto_input_sleb128 (ib);
852     }
853
854   p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
855   index = lto_input_sleb128 (ib);
856   while (index != -1)
857     {
858       basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
859       bb->prev_bb = p_bb;
860       p_bb->next_bb = bb;
861       p_bb = bb;
862       index = lto_input_sleb128 (ib);
863     }
864 }
865
866
867 /* Read a PHI function for basic block BB in function FN.  DATA_IN is
868    the file being read.  IB is the input block to use for reading.  */
869
870 static gimple
871 input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in,
872            struct function *fn)
873 {
874   unsigned HOST_WIDE_INT ix;
875   tree phi_result;
876   int i, len;
877   gimple result;
878
879   ix = lto_input_uleb128 (ib);
880   phi_result = VEC_index (tree, SSANAMES (fn), ix);
881   len = EDGE_COUNT (bb->preds);
882   result = create_phi_node (phi_result, bb);
883   SSA_NAME_DEF_STMT (phi_result) = result;
884
885   /* We have to go through a lookup process here because the preds in the
886      reconstructed graph are generally in a different order than they
887      were in the original program.  */
888   for (i = 0; i < len; i++)
889     {
890       tree def = lto_input_tree (ib, data_in);
891       int src_index = lto_input_uleb128 (ib);
892       location_t arg_loc = lto_input_location (ib, data_in);
893       basic_block sbb = BASIC_BLOCK_FOR_FUNCTION (fn, src_index);
894
895       edge e = NULL;
896       int j;
897
898       for (j = 0; j < len; j++)
899         if (EDGE_PRED (bb, j)->src == sbb)
900           {
901             e = EDGE_PRED (bb, j);
902             break;
903           }
904
905       add_phi_arg (result, def, e, arg_loc);
906     }
907
908   return result;
909 }
910
911
912 /* Read the SSA names array for function FN from DATA_IN using input
913    block IB.  */
914
915 static void
916 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
917                  struct function *fn)
918 {
919   unsigned int i, size;
920
921   size = lto_input_uleb128 (ib);
922   init_ssanames (fn, size);
923
924   i = lto_input_uleb128 (ib);
925   while (i)
926     {
927       tree ssa_name, name;
928       bool is_default_def;
929
930       /* Skip over the elements that had been freed.  */
931       while (VEC_length (tree, SSANAMES (fn)) < i)
932         VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
933
934       is_default_def = (lto_input_1_unsigned (ib) != 0);
935       name = lto_input_tree (ib, data_in);
936       ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
937
938       if (is_default_def)
939         set_default_def (SSA_NAME_VAR (ssa_name), ssa_name);
940
941       i = lto_input_uleb128 (ib);
942     }
943 }
944
945 /* Read a statement with tag TAG in function FN from block IB using
946    descriptors in DATA_IN.  */
947
948 static gimple
949 input_gimple_stmt (struct lto_input_block *ib, struct data_in *data_in,
950                    struct function *fn, enum LTO_tags tag)
951 {
952   gimple stmt;
953   enum gimple_code code;
954   unsigned HOST_WIDE_INT num_ops;
955   size_t i;
956   struct bitpack_d bp;
957
958   code = lto_tag_to_gimple_code (tag);
959
960   /* Read the tuple header.  */
961   bp = lto_input_bitpack (ib);
962   num_ops = bp_unpack_var_len_unsigned (&bp);
963   stmt = gimple_alloc (code, num_ops);
964   stmt->gsbase.no_warning = bp_unpack_value (&bp, 1);
965   if (is_gimple_assign (stmt))
966     stmt->gsbase.nontemporal_move = bp_unpack_value (&bp, 1);
967   stmt->gsbase.has_volatile_ops = bp_unpack_value (&bp, 1);
968   stmt->gsbase.subcode = bp_unpack_var_len_unsigned (&bp);
969
970   /* Read location information.  */
971   gimple_set_location (stmt, lto_input_location (ib, data_in));
972
973   /* Read lexical block reference.  */
974   gimple_set_block (stmt, lto_input_tree (ib, data_in));
975
976   /* Read in all the operands.  */
977   switch (code)
978     {
979     case GIMPLE_RESX:
980       gimple_resx_set_region (stmt, lto_input_sleb128 (ib));
981       break;
982
983     case GIMPLE_EH_MUST_NOT_THROW:
984       gimple_eh_must_not_throw_set_fndecl (stmt, lto_input_tree (ib, data_in));
985       break;
986
987     case GIMPLE_EH_DISPATCH:
988       gimple_eh_dispatch_set_region (stmt, lto_input_sleb128 (ib));
989       break;
990
991     case GIMPLE_ASM:
992       {
993         /* FIXME lto.  Move most of this into a new gimple_asm_set_string().  */
994         tree str;
995         stmt->gimple_asm.ni = lto_input_uleb128 (ib);
996         stmt->gimple_asm.no = lto_input_uleb128 (ib);
997         stmt->gimple_asm.nc = lto_input_uleb128 (ib);
998         stmt->gimple_asm.nl = lto_input_uleb128 (ib);
999         str = input_string_cst (data_in, ib);
1000         stmt->gimple_asm.string = TREE_STRING_POINTER (str);
1001       }
1002       /* Fallthru  */
1003
1004     case GIMPLE_ASSIGN:
1005     case GIMPLE_CALL:
1006     case GIMPLE_RETURN:
1007     case GIMPLE_SWITCH:
1008     case GIMPLE_LABEL:
1009     case GIMPLE_COND:
1010     case GIMPLE_GOTO:
1011     case GIMPLE_DEBUG:
1012       for (i = 0; i < num_ops; i++)
1013         {
1014           tree op = lto_input_tree (ib, data_in);
1015           gimple_set_op (stmt, i, op);
1016           if (!op)
1017             continue;
1018
1019           /* Fixup FIELD_DECLs in COMPONENT_REFs, they are not handled
1020              by decl merging.  */
1021           if (TREE_CODE (op) == ADDR_EXPR)
1022             op = TREE_OPERAND (op, 0);
1023           while (handled_component_p (op))
1024             {
1025               if (TREE_CODE (op) == COMPONENT_REF)
1026                 {
1027                   tree field, type, tem;
1028                   tree closest_match = NULL_TREE;
1029                   field = TREE_OPERAND (op, 1);
1030                   type = DECL_CONTEXT (field);
1031                   for (tem = TYPE_FIELDS (type); tem; tem = TREE_CHAIN (tem))
1032                     {
1033                       if (tem == field)
1034                         break;
1035                       if (DECL_NONADDRESSABLE_P (tem)
1036                           == DECL_NONADDRESSABLE_P (field)
1037                           && gimple_compare_field_offset (tem, field))
1038                         {
1039                           if (types_compatible_p (TREE_TYPE (tem),
1040                                                   TREE_TYPE (field)))
1041                             break;
1042                           else
1043                             closest_match = tem;
1044                         }
1045                     }
1046                   /* In case of type mismatches across units we can fail
1047                      to unify some types and thus not find a proper
1048                      field-decl here.  */
1049                   if (tem == NULL_TREE)
1050                     {
1051                       /* Thus, emit a ODR violation warning.  */
1052                       if (warning_at (gimple_location (stmt), 0,
1053                                       "use of type %<%E%> with two mismatching "
1054                                       "declarations at field %<%E%>",
1055                                       type, TREE_OPERAND (op, 1)))
1056                         {
1057                           if (TYPE_FIELDS (type))
1058                             inform (DECL_SOURCE_LOCATION (TYPE_FIELDS (type)),
1059                                     "original type declared here");
1060                           inform (DECL_SOURCE_LOCATION (TREE_OPERAND (op, 1)),
1061                                   "field in mismatching type declared here");
1062                           if (TYPE_NAME (TREE_TYPE (field))
1063                               && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
1064                                   == TYPE_DECL))
1065                             inform (DECL_SOURCE_LOCATION
1066                                       (TYPE_NAME (TREE_TYPE (field))),
1067                                     "type of field declared here");
1068                           if (closest_match
1069                               && TYPE_NAME (TREE_TYPE (closest_match))
1070                               && (TREE_CODE (TYPE_NAME
1071                                    (TREE_TYPE (closest_match))) == TYPE_DECL))
1072                             inform (DECL_SOURCE_LOCATION
1073                                       (TYPE_NAME (TREE_TYPE (closest_match))),
1074                                     "type of mismatching field declared here");
1075                         }
1076                       /* And finally fixup the types.  */
1077                       TREE_OPERAND (op, 0)
1078                         = build1 (VIEW_CONVERT_EXPR, type,
1079                                   TREE_OPERAND (op, 0));
1080                     }
1081                   else
1082                     TREE_OPERAND (op, 1) = tem;
1083                 }
1084
1085               op = TREE_OPERAND (op, 0);
1086             }
1087         }
1088       if (is_gimple_call (stmt))
1089         {
1090           if (gimple_call_internal_p (stmt))
1091             gimple_call_set_internal_fn
1092               (stmt, lto_input_enum (ib, internal_fn, IFN_LAST));
1093           else
1094             gimple_call_set_fntype (stmt, lto_input_tree (ib, data_in));
1095         }
1096       break;
1097
1098     case GIMPLE_NOP:
1099     case GIMPLE_PREDICT:
1100       break;
1101
1102     default:
1103       internal_error ("bytecode stream: unknown GIMPLE statement tag %s",
1104                       lto_tag_name (tag));
1105     }
1106
1107   /* Update the properties of symbols, SSA names and labels associated
1108      with STMT.  */
1109   if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
1110     {
1111       tree lhs = gimple_get_lhs (stmt);
1112       if (lhs && TREE_CODE (lhs) == SSA_NAME)
1113         SSA_NAME_DEF_STMT (lhs) = stmt;
1114     }
1115   else if (code == GIMPLE_LABEL)
1116     gcc_assert (emit_label_in_global_context_p (gimple_label_label (stmt))
1117                 || DECL_CONTEXT (gimple_label_label (stmt)) == fn->decl);
1118   else if (code == GIMPLE_ASM)
1119     {
1120       unsigned i;
1121
1122       for (i = 0; i < gimple_asm_noutputs (stmt); i++)
1123         {
1124           tree op = TREE_VALUE (gimple_asm_output_op (stmt, i));
1125           if (TREE_CODE (op) == SSA_NAME)
1126             SSA_NAME_DEF_STMT (op) = stmt;
1127         }
1128     }
1129
1130   /* Reset alias information.  */
1131   if (code == GIMPLE_CALL)
1132     gimple_call_reset_alias_info (stmt);
1133
1134   /* Mark the statement modified so its operand vectors can be filled in.  */
1135   gimple_set_modified (stmt, true);
1136
1137   return stmt;
1138 }
1139
1140
1141 /* Read a basic block with tag TAG from DATA_IN using input block IB.
1142    FN is the function being processed.  */
1143
1144 static void
1145 input_bb (struct lto_input_block *ib, enum LTO_tags tag,
1146           struct data_in *data_in, struct function *fn,
1147           int count_materialization_scale)
1148 {
1149   unsigned int index;
1150   basic_block bb;
1151   gimple_stmt_iterator bsi;
1152
1153   /* This routine assumes that CFUN is set to FN, as it needs to call
1154      basic GIMPLE routines that use CFUN.  */
1155   gcc_assert (cfun == fn);
1156
1157   index = lto_input_uleb128 (ib);
1158   bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
1159
1160   bb->count = (lto_input_sleb128 (ib) * count_materialization_scale
1161                + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
1162   bb->loop_depth = lto_input_sleb128 (ib);
1163   bb->frequency = lto_input_sleb128 (ib);
1164   bb->flags = lto_input_sleb128 (ib);
1165
1166   /* LTO_bb1 has statements.  LTO_bb0 does not.  */
1167   if (tag == LTO_bb0)
1168     return;
1169
1170   bsi = gsi_start_bb (bb);
1171   tag = input_record_start (ib);
1172   while (tag)
1173     {
1174       gimple stmt = input_gimple_stmt (ib, data_in, fn, tag);
1175       if (!is_gimple_debug (stmt))
1176         find_referenced_vars_in (stmt);
1177       gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1178
1179       /* After the statement, expect a 0 delimiter or the EH region
1180          that the previous statement belongs to.  */
1181       tag = input_record_start (ib);
1182       lto_tag_check_set (tag, 2, LTO_eh_region, LTO_null);
1183
1184       if (tag == LTO_eh_region)
1185         {
1186           HOST_WIDE_INT region = lto_input_sleb128 (ib);
1187           gcc_assert (region == (int) region);
1188           add_stmt_to_eh_lp (stmt, region);
1189         }
1190
1191       tag = input_record_start (ib);
1192     }
1193
1194   tag = input_record_start (ib);
1195   while (tag)
1196     {
1197       gimple phi = input_phi (ib, bb, data_in, fn);
1198       find_referenced_vars_in (phi);
1199       tag = input_record_start (ib);
1200     }
1201 }
1202
1203 /* Go through all NODE edges and fixup call_stmt pointers
1204    so they point to STMTS.  */
1205
1206 static void
1207 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
1208 {
1209   struct cgraph_edge *cedge;
1210   for (cedge = node->callees; cedge; cedge = cedge->next_callee)
1211     cedge->call_stmt = stmts[cedge->lto_stmt_uid];
1212   for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
1213     cedge->call_stmt = stmts[cedge->lto_stmt_uid];
1214 }
1215
1216 /* Fixup call_stmt pointers in NODE and all clones.  */
1217
1218 static void
1219 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
1220 {
1221   struct cgraph_node *node;
1222
1223   while (orig->clone_of)
1224     orig = orig->clone_of;
1225
1226   fixup_call_stmt_edges_1 (orig, stmts);
1227   if (orig->clones)
1228     for (node = orig->clones; node != orig;)
1229       {
1230         fixup_call_stmt_edges_1 (node, stmts);
1231         if (node->clones)
1232           node = node->clones;
1233         else if (node->next_sibling_clone)
1234           node = node->next_sibling_clone;
1235         else
1236           {
1237             while (node != orig && !node->next_sibling_clone)
1238               node = node->clone_of;
1239             if (node != orig)
1240               node = node->next_sibling_clone;
1241           }
1242       }
1243 }
1244
1245 /* Read the body of function FN_DECL from DATA_IN using input block IB.  */
1246
1247 static void
1248 input_function (tree fn_decl, struct data_in *data_in,
1249                 struct lto_input_block *ib)
1250 {
1251   struct function *fn;
1252   enum LTO_tags tag;
1253   gimple *stmts;
1254   basic_block bb;
1255   struct bitpack_d bp;
1256   struct cgraph_node *node;
1257   tree args, narg, oarg;
1258   int len;
1259
1260   fn = DECL_STRUCT_FUNCTION (fn_decl);
1261   tag = input_record_start (ib);
1262   clear_line_info (data_in);
1263
1264   gimple_register_cfg_hooks ();
1265   lto_tag_check (tag, LTO_function);
1266
1267   /* Read all the attributes for FN.  */
1268   bp = lto_input_bitpack (ib);
1269   fn->is_thunk = bp_unpack_value (&bp, 1);
1270   fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
1271   fn->after_tree_profile = bp_unpack_value (&bp, 1);
1272   fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
1273   fn->returns_struct = bp_unpack_value (&bp, 1);
1274   fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
1275   fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
1276   fn->after_inlining = bp_unpack_value (&bp, 1);
1277   fn->stdarg = bp_unpack_value (&bp, 1);
1278   fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
1279   fn->calls_alloca = bp_unpack_value (&bp, 1);
1280   fn->calls_setjmp = bp_unpack_value (&bp, 1);
1281   fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
1282   fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
1283
1284   /* Input the function start and end loci.  */
1285   fn->function_start_locus = lto_input_location (ib, data_in);
1286   fn->function_end_locus = lto_input_location (ib, data_in);
1287
1288   /* Input the current IL state of the function.  */
1289   fn->curr_properties = lto_input_uleb128 (ib);
1290
1291   /* Read the static chain and non-local goto save area.  */
1292   fn->static_chain_decl = lto_input_tree (ib, data_in);
1293   fn->nonlocal_goto_save_area = lto_input_tree (ib, data_in);
1294
1295   /* Read all the local symbols.  */
1296   len = lto_input_sleb128 (ib);
1297   if (len > 0)
1298     {
1299       int i;
1300       VEC_safe_grow (tree, gc, fn->local_decls, len);
1301       for (i = 0; i < len; i++)
1302         {
1303           tree t = lto_input_tree (ib, data_in);
1304           VEC_replace (tree, fn->local_decls, i, t);
1305         }
1306     }
1307
1308   /* Read all function arguments.  We need to re-map them here to the
1309      arguments of the merged function declaration.  */
1310   args = lto_input_tree (ib, data_in);
1311   for (oarg = args, narg = DECL_ARGUMENTS (fn_decl);
1312        oarg && narg;
1313        oarg = TREE_CHAIN (oarg), narg = TREE_CHAIN (narg))
1314     {
1315       unsigned ix;
1316       bool res;
1317       res = lto_streamer_cache_lookup (data_in->reader_cache, oarg, &ix);
1318       gcc_assert (res);
1319       /* Replace the argument in the streamer cache.  */
1320       lto_streamer_cache_insert_at (data_in->reader_cache, narg, ix);
1321     }
1322   gcc_assert (!oarg && !narg);
1323
1324   /* Read all the SSA names.  */
1325   input_ssa_names (ib, data_in, fn);
1326
1327   /* Read the exception handling regions in the function.  */
1328   input_eh_regions (ib, data_in, fn);
1329
1330   /* Read the tree of lexical scopes for the function.  */
1331   DECL_INITIAL (fn_decl) = lto_input_tree (ib, data_in);
1332   gcc_assert (DECL_INITIAL (fn_decl));
1333   DECL_SAVED_TREE (fn_decl) = NULL_TREE;
1334   node = cgraph_get_create_node (fn_decl);
1335
1336   /* Read all the basic blocks.  */
1337   tag = input_record_start (ib);
1338   while (tag)
1339     {
1340       input_bb (ib, tag, data_in, fn,
1341                 node->count_materialization_scale);
1342       tag = input_record_start (ib);
1343     }
1344
1345   /* Fix up the call statements that are mentioned in the callgraph
1346      edges.  */
1347   set_gimple_stmt_max_uid (cfun, 0);
1348   FOR_ALL_BB (bb)
1349     {
1350       gimple_stmt_iterator gsi;
1351       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1352         {
1353           gimple stmt = gsi_stmt (gsi);
1354           gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1355         }
1356     }
1357   stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1358   FOR_ALL_BB (bb)
1359     {
1360       gimple_stmt_iterator bsi = gsi_start_bb (bb);
1361       while (!gsi_end_p (bsi))
1362         {
1363           gimple stmt = gsi_stmt (bsi);
1364           /* If we're recompiling LTO objects with debug stmts but
1365              we're not supposed to have debug stmts, remove them now.
1366              We can't remove them earlier because this would cause uid
1367              mismatches in fixups, but we can do it at this point, as
1368              long as debug stmts don't require fixups.  */
1369           if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
1370             {
1371               gimple_stmt_iterator gsi = bsi;
1372               gsi_next (&bsi);
1373               gsi_remove (&gsi, true);
1374             }
1375           else
1376             {
1377               gsi_next (&bsi);
1378               stmts[gimple_uid (stmt)] = stmt;
1379             }
1380         }
1381     }
1382
1383   /* Set the gimple body to the statement sequence in the entry
1384      basic block.  FIXME lto, this is fairly hacky.  The existence
1385      of a gimple body is used by the cgraph routines, but we should
1386      really use the presence of the CFG.  */
1387   {
1388     edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
1389     gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1390   }
1391
1392   fixup_call_stmt_edges (node, stmts);
1393   execute_all_ipa_stmt_fixups (node, stmts);
1394
1395   update_ssa (TODO_update_ssa_only_virtuals);
1396   free_dominance_info (CDI_DOMINATORS);
1397   free_dominance_info (CDI_POST_DOMINATORS);
1398   free (stmts);
1399 }
1400
1401
1402 /* Read initializer expressions for public statics.  DATA_IN is the
1403    file being read.  IB is the input block used for reading.  */
1404
1405 static void
1406 input_alias_pairs (struct lto_input_block *ib, struct data_in *data_in)
1407 {
1408   tree var;
1409
1410   clear_line_info (data_in);
1411
1412   var = lto_input_tree (ib, data_in);
1413   while (var)
1414     {
1415       const char *orig_name, *new_name;
1416       alias_pair *p;
1417
1418       p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
1419       p->decl = var;
1420       p->target = lto_input_tree (ib, data_in);
1421
1422       /* If the target is a static object, we may have registered a
1423          new name for it to avoid clashes between statics coming from
1424          different files.  In that case, use the new name.  */
1425       orig_name = IDENTIFIER_POINTER (p->target);
1426       new_name = lto_get_decl_name_mapping (data_in->file_data, orig_name);
1427       if (strcmp (orig_name, new_name) != 0)
1428         p->target = get_identifier (new_name);
1429
1430       var = lto_input_tree (ib, data_in);
1431     }
1432 }
1433
1434
1435 /* Read the body from DATA for function FN_DECL and fill it in.
1436    FILE_DATA are the global decls and types.  SECTION_TYPE is either
1437    LTO_section_function_body or LTO_section_static_initializer.  If
1438    section type is LTO_section_function_body, FN must be the decl for
1439    that function.  */
1440
1441 static void
1442 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
1443                const char *data, enum lto_section_type section_type)
1444 {
1445   const struct lto_function_header *header;
1446   struct data_in *data_in;
1447   int32_t cfg_offset;
1448   int32_t main_offset;
1449   int32_t string_offset;
1450   struct lto_input_block ib_cfg;
1451   struct lto_input_block ib_main;
1452
1453   header = (const struct lto_function_header *) data;
1454   cfg_offset = sizeof (struct lto_function_header);
1455   main_offset = cfg_offset + header->cfg_size;
1456   string_offset = main_offset + header->main_size;
1457
1458   LTO_INIT_INPUT_BLOCK (ib_cfg,
1459                         data + cfg_offset,
1460                         0,
1461                         header->cfg_size);
1462
1463   LTO_INIT_INPUT_BLOCK (ib_main,
1464                         data + main_offset,
1465                         0,
1466                         header->main_size);
1467
1468   data_in = lto_data_in_create (file_data, data + string_offset,
1469                                 header->string_size, NULL);
1470
1471   /* Make sure the file was generated by the exact same compiler.  */
1472   lto_check_version (header->lto_header.major_version,
1473                      header->lto_header.minor_version);
1474
1475   if (section_type == LTO_section_function_body)
1476     {
1477       struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
1478       struct lto_in_decl_state *decl_state;
1479       struct cgraph_node *node = cgraph_get_node (fn_decl);
1480
1481       gcc_checking_assert (node);
1482       push_cfun (fn);
1483       init_tree_ssa (fn);
1484
1485       /* Use the function's decl state. */
1486       decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1487       gcc_assert (decl_state);
1488       file_data->current_decl_state = decl_state;
1489
1490       input_cfg (&ib_cfg, fn, node->count_materialization_scale);
1491
1492       /* Set up the struct function.  */
1493       input_function (fn_decl, data_in, &ib_main);
1494
1495       /* We should now be in SSA.  */
1496       cfun->gimple_df->in_ssa_p = true;
1497
1498       /* Restore decl state */
1499       file_data->current_decl_state = file_data->global_decl_state;
1500
1501       pop_cfun ();
1502     }
1503   else
1504     {
1505       input_alias_pairs (&ib_main, data_in);
1506     }
1507
1508   clear_line_info (data_in);
1509   lto_data_in_delete (data_in);
1510 }
1511
1512
1513 /* Read the body of FN_DECL using DATA.  FILE_DATA holds the global
1514    decls and types.  */
1515
1516 void
1517 lto_input_function_body (struct lto_file_decl_data *file_data,
1518                          tree fn_decl, const char *data)
1519 {
1520   current_function_decl = fn_decl;
1521   lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1522 }
1523
1524
1525 /* Read in VAR_DECL using DATA.  FILE_DATA holds the global decls and
1526    types.  */
1527
1528 void
1529 lto_input_constructors_and_inits (struct lto_file_decl_data *file_data,
1530                                   const char *data)
1531 {
1532   lto_read_body (file_data, NULL, data, LTO_section_static_initializer);
1533 }
1534
1535
1536 /* Unpack all the non-pointer fields of the TS_BASE structure of
1537    expression EXPR from bitpack BP.  */
1538
1539 static void
1540 unpack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
1541 {
1542   /* Note that the code for EXPR has already been unpacked to create EXPR in
1543      lto_materialize_tree.  */
1544   if (!TYPE_P (expr))
1545     {
1546       TREE_SIDE_EFFECTS (expr) = (unsigned) bp_unpack_value (bp, 1);
1547       TREE_CONSTANT (expr) = (unsigned) bp_unpack_value (bp, 1);
1548       TREE_READONLY (expr) = (unsigned) bp_unpack_value (bp, 1);
1549
1550       /* TREE_PUBLIC is used on types to indicate that the type
1551          has a TYPE_CACHED_VALUES vector.  This is not streamed out,
1552          so we skip it here.  */
1553       TREE_PUBLIC (expr) = (unsigned) bp_unpack_value (bp, 1);
1554     }
1555   else
1556     bp_unpack_value (bp, 4);
1557   TREE_ADDRESSABLE (expr) = (unsigned) bp_unpack_value (bp, 1);
1558   TREE_THIS_VOLATILE (expr) = (unsigned) bp_unpack_value (bp, 1);
1559   if (DECL_P (expr))
1560     DECL_UNSIGNED (expr) = (unsigned) bp_unpack_value (bp, 1);
1561   else if (TYPE_P (expr))
1562     TYPE_UNSIGNED (expr) = (unsigned) bp_unpack_value (bp, 1);
1563   else
1564     bp_unpack_value (bp, 1);
1565   TREE_ASM_WRITTEN (expr) = (unsigned) bp_unpack_value (bp, 1);
1566   if (TYPE_P (expr))
1567     TYPE_ARTIFICIAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1568   else
1569     TREE_NO_WARNING (expr) = (unsigned) bp_unpack_value (bp, 1);
1570   TREE_USED (expr) = (unsigned) bp_unpack_value (bp, 1);
1571   TREE_NOTHROW (expr) = (unsigned) bp_unpack_value (bp, 1);
1572   TREE_STATIC (expr) = (unsigned) bp_unpack_value (bp, 1);
1573   TREE_PRIVATE (expr) = (unsigned) bp_unpack_value (bp, 1);
1574   TREE_PROTECTED (expr) = (unsigned) bp_unpack_value (bp, 1);
1575   TREE_DEPRECATED (expr) = (unsigned) bp_unpack_value (bp, 1);
1576   if (TYPE_P (expr))
1577     TYPE_SATURATING (expr) = (unsigned) bp_unpack_value (bp, 1);
1578   else if (TREE_CODE (expr) == SSA_NAME)
1579     SSA_NAME_IS_DEFAULT_DEF (expr) = (unsigned) bp_unpack_value (bp, 1);
1580   else
1581     bp_unpack_value (bp, 1);
1582 }
1583
1584
1585 /* Unpack all the non-pointer fields of the TS_REAL_CST structure of
1586    expression EXPR from bitpack BP.  */
1587
1588 static void
1589 unpack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
1590 {
1591   unsigned i;
1592   REAL_VALUE_TYPE r;
1593   REAL_VALUE_TYPE *rp;
1594
1595   r.cl = (unsigned) bp_unpack_value (bp, 2);
1596   r.decimal = (unsigned) bp_unpack_value (bp, 1);
1597   r.sign = (unsigned) bp_unpack_value (bp, 1);
1598   r.signalling = (unsigned) bp_unpack_value (bp, 1);
1599   r.canonical = (unsigned) bp_unpack_value (bp, 1);
1600   r.uexp = (unsigned) bp_unpack_value (bp, EXP_BITS);
1601   for (i = 0; i < SIGSZ; i++)
1602     r.sig[i] = (unsigned long) bp_unpack_value (bp, HOST_BITS_PER_LONG);
1603
1604   rp = ggc_alloc_real_value ();
1605   memcpy (rp, &r, sizeof (REAL_VALUE_TYPE));
1606   TREE_REAL_CST_PTR (expr) = rp;
1607 }
1608
1609
1610 /* Unpack all the non-pointer fields of the TS_FIXED_CST structure of
1611    expression EXPR from bitpack BP.  */
1612
1613 static void
1614 unpack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
1615 {
1616   struct fixed_value fv;
1617
1618   fv.mode = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
1619   fv.data.low = bp_unpack_var_len_int (bp);
1620   fv.data.high = bp_unpack_var_len_int (bp);
1621   TREE_FIXED_CST (expr) = fv;
1622 }
1623
1624
1625 /* Unpack all the non-pointer fields of the TS_DECL_COMMON structure
1626    of expression EXPR from bitpack BP.  */
1627
1628 static void
1629 unpack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
1630 {
1631   DECL_MODE (expr) = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
1632   DECL_NONLOCAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1633   DECL_VIRTUAL_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1634   DECL_IGNORED_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1635   DECL_ABSTRACT (expr) = (unsigned) bp_unpack_value (bp, 1);
1636   DECL_ARTIFICIAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1637   DECL_USER_ALIGN (expr) = (unsigned) bp_unpack_value (bp, 1);
1638   DECL_PRESERVE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1639   DECL_DEBUG_EXPR_IS_FROM (expr) = (unsigned) bp_unpack_value (bp, 1);
1640   DECL_EXTERNAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1641   DECL_GIMPLE_REG_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1642   DECL_ALIGN (expr) = (unsigned) bp_unpack_var_len_unsigned (bp);
1643
1644   if (TREE_CODE (expr) == LABEL_DECL)
1645     {
1646       DECL_ERROR_ISSUED (expr) = (unsigned) bp_unpack_value (bp, 1);
1647       EH_LANDING_PAD_NR (expr) = (int) bp_unpack_var_len_unsigned (bp);
1648
1649       /* Always assume an initial value of -1 for LABEL_DECL_UID to
1650          force gimple_set_bb to recreate label_to_block_map.  */
1651       LABEL_DECL_UID (expr) = -1;
1652     }
1653
1654   if (TREE_CODE (expr) == FIELD_DECL)
1655     {
1656       DECL_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1);
1657       DECL_NONADDRESSABLE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1658       expr->decl_common.off_align = bp_unpack_value (bp, 8);
1659     }
1660
1661   if (TREE_CODE (expr) == RESULT_DECL
1662       || TREE_CODE (expr) == PARM_DECL
1663       || TREE_CODE (expr) == VAR_DECL)
1664     {
1665       DECL_BY_REFERENCE (expr) = (unsigned) bp_unpack_value (bp, 1);
1666       if (TREE_CODE (expr) == VAR_DECL
1667           || TREE_CODE (expr) == PARM_DECL)
1668         DECL_HAS_VALUE_EXPR_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1669       DECL_RESTRICTED_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1670     }
1671 }
1672
1673
1674 /* Unpack all the non-pointer fields of the TS_DECL_WRTL structure
1675    of expression EXPR from bitpack BP.  */
1676
1677 static void
1678 unpack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
1679 {
1680   DECL_REGISTER (expr) = (unsigned) bp_unpack_value (bp, 1);
1681 }
1682
1683
1684 /* Unpack all the non-pointer fields of the TS_DECL_WITH_VIS structure
1685    of expression EXPR from bitpack BP.  */
1686
1687 static void
1688 unpack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
1689 {
1690   DECL_DEFER_OUTPUT (expr) = (unsigned) bp_unpack_value (bp, 1);
1691   DECL_COMMON (expr) = (unsigned) bp_unpack_value (bp, 1);
1692   DECL_DLLIMPORT_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1693   DECL_WEAK (expr) = (unsigned) bp_unpack_value (bp, 1);
1694   DECL_SEEN_IN_BIND_EXPR_P (expr) = (unsigned) bp_unpack_value (bp,  1);
1695   DECL_COMDAT (expr) = (unsigned) bp_unpack_value (bp,  1);
1696   DECL_VISIBILITY (expr) = (enum symbol_visibility) bp_unpack_value (bp,  2);
1697   DECL_VISIBILITY_SPECIFIED (expr) = (unsigned) bp_unpack_value (bp,  1);
1698
1699   if (TREE_CODE (expr) == VAR_DECL)
1700     {
1701       DECL_HARD_REGISTER (expr) = (unsigned) bp_unpack_value (bp, 1);
1702       DECL_IN_TEXT_SECTION (expr) = (unsigned) bp_unpack_value (bp, 1);
1703       DECL_IN_CONSTANT_POOL (expr) = (unsigned) bp_unpack_value (bp, 1);
1704       DECL_TLS_MODEL (expr) = (enum tls_model) bp_unpack_value (bp,  3);
1705     }
1706
1707   if (VAR_OR_FUNCTION_DECL_P (expr))
1708     {
1709       priority_type p;
1710       p = (priority_type) bp_unpack_var_len_unsigned (bp);
1711       SET_DECL_INIT_PRIORITY (expr, p);
1712     }
1713 }
1714
1715
1716 /* Unpack all the non-pointer fields of the TS_FUNCTION_DECL structure
1717    of expression EXPR from bitpack BP.  */
1718
1719 static void
1720 unpack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
1721 {
1722   DECL_BUILT_IN_CLASS (expr) = bp_unpack_enum (bp, built_in_class,
1723                                                BUILT_IN_LAST);
1724   DECL_STATIC_CONSTRUCTOR (expr) = (unsigned) bp_unpack_value (bp, 1);
1725   DECL_STATIC_DESTRUCTOR (expr) = (unsigned) bp_unpack_value (bp, 1);
1726   DECL_UNINLINABLE (expr) = (unsigned) bp_unpack_value (bp, 1);
1727   DECL_POSSIBLY_INLINED (expr) = (unsigned) bp_unpack_value (bp, 1);
1728   DECL_IS_NOVOPS (expr) = (unsigned) bp_unpack_value (bp, 1);
1729   DECL_IS_RETURNS_TWICE (expr) = (unsigned) bp_unpack_value (bp, 1);
1730   DECL_IS_MALLOC (expr) = (unsigned) bp_unpack_value (bp, 1);
1731   DECL_IS_OPERATOR_NEW (expr) = (unsigned) bp_unpack_value (bp, 1);
1732   DECL_DECLARED_INLINE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1733   DECL_STATIC_CHAIN (expr) = (unsigned) bp_unpack_value (bp, 1);
1734   DECL_NO_INLINE_WARNING_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1735   DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr)
1736                         = (unsigned) bp_unpack_value (bp, 1);
1737   DECL_NO_LIMIT_STACK (expr) = (unsigned) bp_unpack_value (bp, 1);
1738   DECL_DISREGARD_INLINE_LIMITS (expr) = (unsigned) bp_unpack_value (bp, 1);
1739   DECL_PURE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1740   DECL_LOOPING_CONST_OR_PURE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1741   if (DECL_BUILT_IN_CLASS (expr) != NOT_BUILT_IN)
1742     {
1743       DECL_FUNCTION_CODE (expr) = (enum built_in_function) bp_unpack_value (bp, 11);
1744       if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
1745           && DECL_FUNCTION_CODE (expr) >= END_BUILTINS)
1746         fatal_error ("machine independent builtin code out of range");
1747       else if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD)
1748         {
1749           tree result = targetm.builtin_decl (DECL_FUNCTION_CODE (expr), true);
1750           if (!result || result == error_mark_node)
1751             fatal_error ("target specific builtin not available");
1752         }
1753     }
1754   if (DECL_STATIC_DESTRUCTOR (expr))
1755     {
1756       priority_type p;
1757       p = (priority_type) bp_unpack_var_len_unsigned (bp);
1758       SET_DECL_FINI_PRIORITY (expr, p);
1759     }
1760 }
1761
1762
1763 /* Unpack all the non-pointer fields of the TS_TYPE_COMMON structure
1764    of expression EXPR from bitpack BP.  */
1765
1766 static void
1767 unpack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
1768 {
1769   enum machine_mode mode;
1770
1771   mode = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
1772   SET_TYPE_MODE (expr, mode);
1773   TYPE_STRING_FLAG (expr) = (unsigned) bp_unpack_value (bp, 1);
1774   TYPE_NO_FORCE_BLK (expr) = (unsigned) bp_unpack_value (bp, 1);
1775   TYPE_NEEDS_CONSTRUCTING (expr) = (unsigned) bp_unpack_value (bp, 1);
1776   if (RECORD_OR_UNION_TYPE_P (expr))
1777     TYPE_TRANSPARENT_AGGR (expr) = (unsigned) bp_unpack_value (bp, 1);
1778   TYPE_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1);
1779   TYPE_RESTRICT (expr) = (unsigned) bp_unpack_value (bp, 1);
1780   TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
1781         = (unsigned) bp_unpack_value (bp, 2);
1782   TYPE_USER_ALIGN (expr) = (unsigned) bp_unpack_value (bp, 1);
1783   TYPE_READONLY (expr) = (unsigned) bp_unpack_value (bp, 1);
1784   TYPE_PRECISION (expr) = bp_unpack_var_len_unsigned (bp);
1785   TYPE_ALIGN (expr) = bp_unpack_var_len_unsigned (bp);
1786   TYPE_ALIAS_SET (expr) = bp_unpack_var_len_int (bp);
1787 }
1788
1789
1790 /* Unpack all the non-pointer fields of the TS_BLOCK structure
1791    of expression EXPR from bitpack BP.  */
1792
1793 static void
1794 unpack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
1795 {
1796   BLOCK_ABSTRACT (expr) = (unsigned) bp_unpack_value (bp, 1);
1797   /* BLOCK_NUMBER is recomputed.  */
1798 }
1799
1800 /* Unpack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL
1801    structure of expression EXPR from bitpack BP.  */
1802
1803 static void
1804 unpack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
1805 {
1806 }
1807
1808 /* Unpack all the non-pointer fields in EXPR into a bit pack.  */
1809
1810 static void
1811 unpack_value_fields (struct bitpack_d *bp, tree expr)
1812 {
1813   enum tree_code code;
1814
1815   code = TREE_CODE (expr);
1816
1817   /* Note that all these functions are highly sensitive to changes in
1818      the types and sizes of each of the fields being packed.  */
1819   unpack_ts_base_value_fields (bp, expr);
1820
1821   if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1822     unpack_ts_real_cst_value_fields (bp, expr);
1823
1824   if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1825     unpack_ts_fixed_cst_value_fields (bp, expr);
1826
1827   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1828     unpack_ts_decl_common_value_fields (bp, expr);
1829
1830   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1831     unpack_ts_decl_wrtl_value_fields (bp, expr);
1832
1833   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1834     unpack_ts_decl_with_vis_value_fields (bp, expr);
1835
1836   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1837     unpack_ts_function_decl_value_fields (bp, expr);
1838
1839   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1840     unpack_ts_type_common_value_fields (bp, expr);
1841
1842   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1843     unpack_ts_block_value_fields (bp, expr);
1844
1845   if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1846     unpack_ts_translation_unit_decl_value_fields (bp, expr);
1847
1848   if (streamer_hooks.unpack_value_fields)
1849     streamer_hooks.unpack_value_fields (bp, expr);
1850 }
1851
1852
1853 /* Materialize a new tree from input block IB using descriptors in
1854    DATA_IN.  The code for the new tree should match TAG.  Store in
1855    *IX_P the index into the reader cache where the new tree is stored.  */
1856
1857 static tree
1858 lto_materialize_tree (struct lto_input_block *ib, struct data_in *data_in,
1859                       enum LTO_tags tag)
1860 {
1861   struct bitpack_d bp;
1862   enum tree_code code;
1863   tree result;
1864 #ifdef LTO_STREAMER_DEBUG
1865   HOST_WIDEST_INT orig_address_in_writer;
1866 #endif
1867
1868   result = NULL_TREE;
1869
1870 #ifdef LTO_STREAMER_DEBUG
1871   /* Read the word representing the memory address for the tree
1872      as it was written by the writer.  This is useful when
1873      debugging differences between the writer and reader.  */
1874   orig_address_in_writer = lto_input_sleb128 (ib);
1875   gcc_assert ((intptr_t) orig_address_in_writer == orig_address_in_writer);
1876 #endif
1877
1878   code = lto_tag_to_tree_code (tag);
1879
1880   /* We should never see an SSA_NAME tree.  Only the version numbers of
1881      SSA names are ever written out.  See input_ssa_names.  */
1882   gcc_assert (code != SSA_NAME);
1883
1884   /* Instantiate a new tree using the header data.  */
1885   if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1886     result = input_string_cst (data_in, ib);
1887   else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1888     result = input_identifier (data_in, ib);
1889   else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1890     {
1891       HOST_WIDE_INT len = lto_input_sleb128 (ib);
1892       result = make_tree_vec (len);
1893     }
1894   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1895     {
1896       unsigned HOST_WIDE_INT len = lto_input_uleb128 (ib);
1897       result = make_tree_binfo (len);
1898     }
1899   else
1900     {
1901       /* For all other nodes, see if the streamer knows how to allocate
1902          it.  */
1903       if (streamer_hooks.alloc_tree)
1904         result = streamer_hooks.alloc_tree (code, ib, data_in);
1905
1906       /* If the hook did not handle it, materialize the tree with a raw
1907          make_node call.  */
1908       if (result == NULL_TREE)
1909         result = make_node (code);
1910     }
1911
1912 #ifdef LTO_STREAMER_DEBUG
1913   /* Store the original address of the tree as seen by the writer
1914      in RESULT's aux field.  This is useful when debugging streaming
1915      problems.  This way, a debugging session can be started on
1916      both writer and reader with a breakpoint using this address
1917      value in both.  */
1918   lto_orig_address_map (result, (intptr_t) orig_address_in_writer);
1919 #endif
1920
1921   /* Read the bitpack of non-pointer values from IB.  */
1922   bp = lto_input_bitpack (ib);
1923
1924   /* The first word in BP contains the code of the tree that we
1925      are about to read.  */
1926   code = (enum tree_code) bp_unpack_value (&bp, 16);
1927   lto_tag_check (lto_tree_code_to_tag (code), tag);
1928
1929   /* Unpack all the value fields from BP.  */
1930   unpack_value_fields (&bp, result);
1931
1932   /* Enter RESULT in the reader cache.  This will make RESULT
1933      available so that circular references in the rest of the tree
1934      structure can be resolved in subsequent calls to lto_input_tree.  */
1935   lto_streamer_cache_append (data_in->reader_cache, result);
1936
1937   return result;
1938 }
1939
1940
1941 /* Read all pointer fields in the TS_COMMON structure of EXPR from input
1942    block IB.  DATA_IN contains tables and descriptors for the
1943    file being read.  */
1944
1945
1946 static void
1947 lto_input_ts_common_tree_pointers (struct lto_input_block *ib,
1948                                    struct data_in *data_in, tree expr)
1949 {
1950   if (TREE_CODE (expr) != IDENTIFIER_NODE)
1951     TREE_TYPE (expr) = lto_input_tree (ib, data_in);
1952 }
1953
1954
1955 /* Read all pointer fields in the TS_VECTOR structure of EXPR from input
1956    block IB.  DATA_IN contains tables and descriptors for the
1957    file being read.  */
1958
1959 static void
1960 lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
1961                                    struct data_in *data_in, tree expr)
1962 {
1963   TREE_VECTOR_CST_ELTS (expr) = lto_input_chain (ib, data_in);
1964 }
1965
1966
1967 /* Read all pointer fields in the TS_COMPLEX structure of EXPR from input
1968    block IB.  DATA_IN contains tables and descriptors for the
1969    file being read.  */
1970
1971 static void
1972 lto_input_ts_complex_tree_pointers (struct lto_input_block *ib,
1973                                     struct data_in *data_in, tree expr)
1974 {
1975   TREE_REALPART (expr) = lto_input_tree (ib, data_in);
1976   TREE_IMAGPART (expr) = lto_input_tree (ib, data_in);
1977 }
1978
1979
1980 /* Read all pointer fields in the TS_DECL_MINIMAL structure of EXPR
1981    from input block IB.  DATA_IN contains tables and descriptors for the
1982    file being read.  */
1983
1984 static void
1985 lto_input_ts_decl_minimal_tree_pointers (struct lto_input_block *ib,
1986                                          struct data_in *data_in, tree expr)
1987 {
1988   DECL_NAME (expr) = lto_input_tree (ib, data_in);
1989   DECL_CONTEXT (expr) = lto_input_tree (ib, data_in);
1990   DECL_SOURCE_LOCATION (expr) = lto_input_location (ib, data_in);
1991 }
1992
1993
1994 /* Read all pointer fields in the TS_DECL_COMMON structure of EXPR from
1995    input block IB.  DATA_IN contains tables and descriptors for the
1996    file being read.  */
1997
1998 static void
1999 lto_input_ts_decl_common_tree_pointers (struct lto_input_block *ib,
2000                                         struct data_in *data_in, tree expr)
2001 {
2002   DECL_SIZE (expr) = lto_input_tree (ib, data_in);
2003   DECL_SIZE_UNIT (expr) = lto_input_tree (ib, data_in);
2004   DECL_ATTRIBUTES (expr) = lto_input_tree (ib, data_in);
2005
2006   /* Do not stream DECL_ABSTRACT_ORIGIN.  We cannot handle debug information
2007      for early inlining so drop it on the floor instead of ICEing in
2008      dwarf2out.c.  */
2009
2010   if (TREE_CODE (expr) == PARM_DECL)
2011     TREE_CHAIN (expr) = lto_input_chain (ib, data_in);
2012
2013   if ((TREE_CODE (expr) == VAR_DECL
2014        || TREE_CODE (expr) == PARM_DECL)
2015       && DECL_HAS_VALUE_EXPR_P (expr))
2016     SET_DECL_VALUE_EXPR (expr, lto_input_tree (ib, data_in));
2017
2018   if (TREE_CODE (expr) == VAR_DECL)
2019     {
2020       tree dexpr = lto_input_tree (ib, data_in);
2021       if (dexpr)
2022         SET_DECL_DEBUG_EXPR (expr, dexpr);
2023     }
2024 }
2025
2026
2027 /* Read all pointer fields in the TS_DECL_NON_COMMON structure of
2028    EXPR from input block IB.  DATA_IN contains tables and descriptors for the
2029    file being read.  */
2030
2031 static void
2032 lto_input_ts_decl_non_common_tree_pointers (struct lto_input_block *ib,
2033                                             struct data_in *data_in, tree expr)
2034 {
2035   if (TREE_CODE (expr) == FUNCTION_DECL)
2036     {
2037       DECL_ARGUMENTS (expr) = lto_input_tree (ib, data_in);
2038       DECL_RESULT (expr) = lto_input_tree (ib, data_in);
2039     }
2040   DECL_VINDEX (expr) = lto_input_tree (ib, data_in);
2041 }
2042
2043
2044 /* Read all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
2045    from input block IB.  DATA_IN contains tables and descriptors for the
2046    file being read.  */
2047
2048 static void
2049 lto_input_ts_decl_with_vis_tree_pointers (struct lto_input_block *ib,
2050                                           struct data_in *data_in, tree expr)
2051 {
2052   tree id;
2053
2054   id = lto_input_tree (ib, data_in);
2055   if (id)
2056     {
2057       gcc_assert (TREE_CODE (id) == IDENTIFIER_NODE);
2058       SET_DECL_ASSEMBLER_NAME (expr, id);
2059     }
2060
2061   DECL_SECTION_NAME (expr) = lto_input_tree (ib, data_in);
2062   DECL_COMDAT_GROUP (expr) = lto_input_tree (ib, data_in);
2063 }
2064
2065
2066 /* Read all pointer fields in the TS_FIELD_DECL structure of EXPR from
2067    input block IB.  DATA_IN contains tables and descriptors for the
2068    file being read.  */
2069
2070 static void
2071 lto_input_ts_field_decl_tree_pointers (struct lto_input_block *ib,
2072                                        struct data_in *data_in, tree expr)
2073 {
2074   DECL_FIELD_OFFSET (expr) = lto_input_tree (ib, data_in);
2075   DECL_BIT_FIELD_TYPE (expr) = lto_input_tree (ib, data_in);
2076   DECL_QUALIFIER (expr) = lto_input_tree (ib, data_in);
2077   DECL_FIELD_BIT_OFFSET (expr) = lto_input_tree (ib, data_in);
2078   DECL_FCONTEXT (expr) = lto_input_tree (ib, data_in);
2079   TREE_CHAIN (expr) = lto_input_chain (ib, data_in);
2080 }
2081
2082
2083 /* Read all pointer fields in the TS_FUNCTION_DECL structure of EXPR
2084    from input block IB.  DATA_IN contains tables and descriptors for the
2085    file being read.  */
2086
2087 static void
2088 lto_input_ts_function_decl_tree_pointers (struct lto_input_block *ib,
2089                                           struct data_in *data_in, tree expr)
2090 {
2091   /* DECL_STRUCT_FUNCTION is handled by lto_input_function.  FIXME lto,
2092      maybe it should be handled here?  */
2093   DECL_FUNCTION_PERSONALITY (expr) = lto_input_tree (ib, data_in);
2094   DECL_FUNCTION_SPECIFIC_TARGET (expr) = lto_input_tree (ib, data_in);
2095   DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr) = lto_input_tree (ib, data_in);
2096
2097   /* If the file contains a function with an EH personality set,
2098      then it was compiled with -fexceptions.  In that case, initialize
2099      the backend EH machinery.  */
2100   if (DECL_FUNCTION_PERSONALITY (expr))
2101     lto_init_eh ();
2102 }
2103
2104
2105 /* Read all pointer fields in the TS_TYPE_COMMON structure of EXPR from
2106    input block IB.  DATA_IN contains tables and descriptors for the file
2107    being read.  */
2108
2109 static void
2110 lto_input_ts_type_common_tree_pointers (struct lto_input_block *ib,
2111                                         struct data_in *data_in, tree expr)
2112 {
2113   TYPE_SIZE (expr) = lto_input_tree (ib, data_in);
2114   TYPE_SIZE_UNIT (expr) = lto_input_tree (ib, data_in);
2115   TYPE_ATTRIBUTES (expr) = lto_input_tree (ib, data_in);
2116   TYPE_NAME (expr) = lto_input_tree (ib, data_in);
2117   /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO.  They will be
2118      reconstructed during fixup.  */
2119   /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
2120      during fixup.  */
2121   TYPE_MAIN_VARIANT (expr) = lto_input_tree (ib, data_in);
2122   TYPE_CONTEXT (expr) = lto_input_tree (ib, data_in);
2123   /* TYPE_CANONICAL gets re-computed during type merging.  */
2124   TYPE_CANONICAL (expr) = NULL_TREE;
2125   TYPE_STUB_DECL (expr) = lto_input_tree (ib, data_in);
2126 }
2127
2128 /* Read all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
2129    from input block IB.  DATA_IN contains tables and descriptors for the
2130    file being read.  */
2131
2132 static void
2133 lto_input_ts_type_non_common_tree_pointers (struct lto_input_block *ib,
2134                                             struct data_in *data_in,
2135                                             tree expr)
2136 {
2137   if (TREE_CODE (expr) == ENUMERAL_TYPE)
2138     TYPE_VALUES (expr) = lto_input_tree (ib, data_in);
2139   else if (TREE_CODE (expr) == ARRAY_TYPE)
2140     TYPE_DOMAIN (expr) = lto_input_tree (ib, data_in);
2141   else if (RECORD_OR_UNION_TYPE_P (expr))
2142     TYPE_FIELDS (expr) = lto_input_tree (ib, data_in);
2143   else if (TREE_CODE (expr) == FUNCTION_TYPE
2144            || TREE_CODE (expr) == METHOD_TYPE)
2145     TYPE_ARG_TYPES (expr) = lto_input_tree (ib, data_in);
2146
2147   if (!POINTER_TYPE_P (expr))
2148     TYPE_MINVAL (expr) = lto_input_tree (ib, data_in);
2149   TYPE_MAXVAL (expr) = lto_input_tree (ib, data_in);
2150   if (RECORD_OR_UNION_TYPE_P (expr))
2151     TYPE_BINFO (expr) = lto_input_tree (ib, data_in);
2152 }
2153
2154
2155 /* Read all pointer fields in the TS_LIST structure of EXPR from input
2156    block IB.  DATA_IN contains tables and descriptors for the
2157    file being read.  */
2158
2159 static void
2160 lto_input_ts_list_tree_pointers (struct lto_input_block *ib,
2161                                  struct data_in *data_in, tree expr)
2162 {
2163   TREE_PURPOSE (expr) = lto_input_tree (ib, data_in);
2164   TREE_VALUE (expr) = lto_input_tree (ib, data_in);
2165   TREE_CHAIN (expr) = lto_input_chain (ib, data_in);
2166 }
2167
2168
2169 /* Read all pointer fields in the TS_VEC structure of EXPR from input
2170    block IB.  DATA_IN contains tables and descriptors for the
2171    file being read.  */
2172
2173 static void
2174 lto_input_ts_vec_tree_pointers (struct lto_input_block *ib,
2175                                 struct data_in *data_in, tree expr)
2176 {
2177   int i;
2178
2179   /* Note that TREE_VEC_LENGTH was read by lto_materialize_tree to
2180      instantiate EXPR.  */
2181   for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
2182     TREE_VEC_ELT (expr, i) = lto_input_tree (ib, data_in);
2183 }
2184
2185
2186 /* Read all pointer fields in the TS_EXP structure of EXPR from input
2187    block IB.  DATA_IN contains tables and descriptors for the
2188    file being read.  */
2189
2190
2191 static void
2192 lto_input_ts_exp_tree_pointers (struct lto_input_block *ib,
2193                                 struct data_in *data_in, tree expr)
2194 {
2195   int i, length;
2196   location_t loc;
2197
2198   length = lto_input_sleb128 (ib);
2199   gcc_assert (length == TREE_OPERAND_LENGTH (expr));
2200
2201   for (i = 0; i < length; i++)
2202     TREE_OPERAND (expr, i) = lto_input_tree (ib, data_in);
2203
2204   loc = lto_input_location (ib, data_in);
2205   SET_EXPR_LOCATION (expr, loc);
2206   TREE_BLOCK (expr) = lto_input_tree (ib, data_in);
2207 }
2208
2209
2210 /* Read all pointer fields in the TS_BLOCK structure of EXPR from input
2211    block IB.  DATA_IN contains tables and descriptors for the
2212    file being read.  */
2213
2214 static void
2215 lto_input_ts_block_tree_pointers (struct lto_input_block *ib,
2216                                   struct data_in *data_in, tree expr)
2217 {
2218   /* Do not stream BLOCK_SOURCE_LOCATION.  We cannot handle debug information
2219      for early inlining so drop it on the floor instead of ICEing in
2220      dwarf2out.c.  */
2221   BLOCK_VARS (expr) = lto_input_chain (ib, data_in);
2222
2223   /* Do not stream BLOCK_NONLOCALIZED_VARS.  We cannot handle debug information
2224      for early inlining so drop it on the floor instead of ICEing in
2225      dwarf2out.c.  */
2226
2227   BLOCK_SUPERCONTEXT (expr) = lto_input_tree (ib, data_in);
2228   /* Do not stream BLOCK_ABSTRACT_ORIGIN.  We cannot handle debug information
2229      for early inlining so drop it on the floor instead of ICEing in
2230      dwarf2out.c.  */
2231   BLOCK_FRAGMENT_ORIGIN (expr) = lto_input_tree (ib, data_in);
2232   BLOCK_FRAGMENT_CHAIN (expr) = lto_input_tree (ib, data_in);
2233   /* We re-compute BLOCK_SUBBLOCKS of our parent here instead
2234      of streaming it.  For non-BLOCK BLOCK_SUPERCONTEXTs we still
2235      stream the child relationship explicitly.  */
2236   if (BLOCK_SUPERCONTEXT (expr)
2237       && TREE_CODE (BLOCK_SUPERCONTEXT (expr)) == BLOCK)
2238     {
2239       BLOCK_CHAIN (expr) = BLOCK_SUBBLOCKS (BLOCK_SUPERCONTEXT (expr));
2240       BLOCK_SUBBLOCKS (BLOCK_SUPERCONTEXT (expr)) = expr;
2241     }
2242   /* The global block is rooted at the TU decl.  Hook it here to
2243      avoid the need to stream in this block during WPA time.  */
2244   else if (BLOCK_SUPERCONTEXT (expr)
2245            && TREE_CODE (BLOCK_SUPERCONTEXT (expr)) == TRANSLATION_UNIT_DECL)
2246     DECL_INITIAL (BLOCK_SUPERCONTEXT (expr)) = expr;
2247   /* The function-level block is connected at the time we read in
2248      function bodies for the same reason.  */
2249 }
2250
2251
2252 /* Read all pointer fields in the TS_BINFO structure of EXPR from input
2253    block IB.  DATA_IN contains tables and descriptors for the
2254    file being read.  */
2255
2256 static void
2257 lto_input_ts_binfo_tree_pointers (struct lto_input_block *ib,
2258                                   struct data_in *data_in, tree expr)
2259 {
2260   unsigned i, len;
2261   tree t;
2262
2263   /* Note that the number of slots in EXPR was read in
2264      lto_materialize_tree when instantiating EXPR.  However, the
2265      vector is empty so we cannot rely on VEC_length to know how many
2266      elements to read.  So, this list is emitted as a 0-terminated
2267      list on the writer side.  */
2268   do
2269     {
2270       t = lto_input_tree (ib, data_in);
2271       if (t)
2272         VEC_quick_push (tree, BINFO_BASE_BINFOS (expr), t);
2273     }
2274   while (t);
2275
2276   BINFO_OFFSET (expr) = lto_input_tree (ib, data_in);
2277   BINFO_VTABLE (expr) = lto_input_tree (ib, data_in);
2278   BINFO_VIRTUALS (expr) = lto_input_tree (ib, data_in);
2279   BINFO_VPTR_FIELD (expr) = lto_input_tree (ib, data_in);
2280
2281   len = lto_input_uleb128 (ib);
2282   if (len > 0)
2283     {
2284       VEC_reserve_exact (tree, gc, BINFO_BASE_ACCESSES (expr), len);
2285       for (i = 0; i < len; i++)
2286         {
2287           tree a = lto_input_tree (ib, data_in);
2288           VEC_quick_push (tree, BINFO_BASE_ACCESSES (expr), a);
2289         }
2290     }
2291
2292   BINFO_INHERITANCE_CHAIN (expr) = lto_input_tree (ib, data_in);
2293   BINFO_SUBVTT_INDEX (expr) = lto_input_tree (ib, data_in);
2294   BINFO_VPTR_INDEX (expr) = lto_input_tree (ib, data_in);
2295 }
2296
2297
2298 /* Read all pointer fields in the TS_CONSTRUCTOR structure of EXPR from
2299    input block IB.  DATA_IN contains tables and descriptors for the
2300    file being read.  */
2301
2302 static void
2303 lto_input_ts_constructor_tree_pointers (struct lto_input_block *ib,
2304                                         struct data_in *data_in, tree expr)
2305 {
2306   unsigned i, len;
2307
2308   len = lto_input_uleb128 (ib);
2309   for (i = 0; i < len; i++)
2310     {
2311       tree index, value;
2312
2313       index = lto_input_tree (ib, data_in);
2314       value = lto_input_tree (ib, data_in);
2315       CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (expr), index, value);
2316     }
2317 }
2318
2319
2320 /* Input a TS_TARGET_OPTION tree from IB into EXPR.  */
2321
2322 static void
2323 lto_input_ts_target_option (struct lto_input_block *ib, tree expr)
2324 {
2325   unsigned i, len;
2326   struct bitpack_d bp;
2327   struct cl_target_option *t = TREE_TARGET_OPTION (expr);
2328
2329   bp = lto_input_bitpack (ib);
2330   len = sizeof (struct cl_target_option);
2331   for (i = 0; i < len; i++)
2332     ((unsigned char *)t)[i] = bp_unpack_value (&bp, 8);
2333   if (bp_unpack_value (&bp, 32) != 0x12345678)
2334     fatal_error ("cl_target_option size mismatch in LTO reader and writer");
2335 }
2336
2337 /* Input a TS_TRANSLATION_UNIT_DECL tree from IB and DATA_IN into EXPR.  */
2338
2339 static void
2340 lto_input_ts_translation_unit_decl_tree_pointers (struct lto_input_block *ib,
2341                                                   struct data_in *data_in,
2342                                                   tree expr)
2343 {
2344   TRANSLATION_UNIT_LANGUAGE (expr) = xstrdup (lto_input_string (data_in, ib));
2345   VEC_safe_push (tree, gc, all_translation_units, expr);
2346 }
2347
2348 /* Helper for lto_input_tree.  Read all pointer fields in EXPR from
2349    input block IB.  DATA_IN contains tables and descriptors for the
2350    file being read.  */
2351
2352 static void
2353 lto_input_tree_pointers (struct lto_input_block *ib, struct data_in *data_in,
2354                          tree expr)
2355 {
2356   enum tree_code code;
2357
2358   code = TREE_CODE (expr);
2359
2360   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
2361     lto_input_ts_common_tree_pointers (ib, data_in, expr);
2362
2363   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
2364     lto_input_ts_vector_tree_pointers (ib, data_in, expr);
2365
2366   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
2367     lto_input_ts_complex_tree_pointers (ib, data_in, expr);
2368
2369   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
2370     lto_input_ts_decl_minimal_tree_pointers (ib, data_in, expr);
2371
2372   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2373     lto_input_ts_decl_common_tree_pointers (ib, data_in, expr);
2374
2375   if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2376     lto_input_ts_decl_non_common_tree_pointers (ib, data_in, expr);
2377
2378   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2379     lto_input_ts_decl_with_vis_tree_pointers (ib, data_in, expr);
2380
2381   if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2382     lto_input_ts_field_decl_tree_pointers (ib, data_in, expr);
2383
2384   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2385     lto_input_ts_function_decl_tree_pointers (ib, data_in, expr);
2386
2387   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
2388     lto_input_ts_type_common_tree_pointers (ib, data_in, expr);
2389
2390   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
2391     lto_input_ts_type_non_common_tree_pointers (ib, data_in, expr);
2392
2393   if (CODE_CONTAINS_STRUCT (code, TS_LIST))
2394     lto_input_ts_list_tree_pointers (ib, data_in, expr);
2395
2396   if (CODE_CONTAINS_STRUCT (code, TS_VEC))
2397     lto_input_ts_vec_tree_pointers (ib, data_in, expr);
2398
2399   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
2400     lto_input_ts_exp_tree_pointers (ib, data_in, expr);
2401
2402   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
2403     lto_input_ts_block_tree_pointers (ib, data_in, expr);
2404
2405   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
2406     lto_input_ts_binfo_tree_pointers (ib, data_in, expr);
2407
2408   if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
2409     lto_input_ts_constructor_tree_pointers (ib, data_in, expr);
2410
2411   if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
2412     lto_input_ts_target_option (ib, expr);
2413
2414   if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
2415     lto_input_ts_translation_unit_decl_tree_pointers (ib, data_in, expr);
2416 }
2417
2418
2419 /* Read an index IX from input block IB and return the tree node at
2420    DATA_IN->FILE_DATA->GLOBALS_INDEX[IX].  */
2421
2422 static tree
2423 lto_get_pickled_tree (struct lto_input_block *ib, struct data_in *data_in)
2424 {
2425   unsigned HOST_WIDE_INT ix;
2426   tree result;
2427   enum LTO_tags expected_tag;
2428
2429   ix = lto_input_uleb128 (ib);
2430   expected_tag = lto_input_enum (ib, LTO_tags, LTO_NUM_TAGS);
2431
2432   result = lto_streamer_cache_get (data_in->reader_cache, ix);
2433   gcc_assert (result
2434               && TREE_CODE (result) == lto_tag_to_tree_code (expected_tag));
2435
2436   return result;
2437 }
2438
2439
2440 /* Read a code and class from input block IB and return the
2441    corresponding builtin.  DATA_IN is as in lto_input_tree.  */
2442
2443 static tree
2444 lto_get_builtin_tree (struct lto_input_block *ib, struct data_in *data_in)
2445 {
2446   enum built_in_class fclass;
2447   enum built_in_function fcode;
2448   const char *asmname;
2449   tree result;
2450
2451   fclass = lto_input_enum (ib, built_in_class, BUILT_IN_LAST);
2452   gcc_assert (fclass == BUILT_IN_NORMAL || fclass == BUILT_IN_MD);
2453
2454   fcode = (enum built_in_function) lto_input_uleb128 (ib);
2455
2456   if (fclass == BUILT_IN_NORMAL)
2457     {
2458       if (fcode >= END_BUILTINS)
2459         fatal_error ("machine independent builtin code out of range");
2460       result = built_in_decls[fcode];
2461       gcc_assert (result);
2462     }
2463   else if (fclass == BUILT_IN_MD)
2464     {
2465       result = targetm.builtin_decl (fcode, true);
2466       if (!result || result == error_mark_node)
2467         fatal_error ("target specific builtin not available");
2468     }
2469   else
2470     gcc_unreachable ();
2471
2472   asmname = lto_input_string (data_in, ib);
2473   if (asmname)
2474     set_builtin_user_assembler_name (result, asmname);
2475
2476   lto_streamer_cache_append (data_in->reader_cache, result);
2477
2478   return result;
2479 }
2480
2481
2482 /* Read the physical representation of a tree node with tag TAG from
2483    input block IB using the per-file context in DATA_IN.  */
2484
2485 static tree
2486 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
2487                enum LTO_tags tag)
2488 {
2489   tree result;
2490
2491   result = lto_materialize_tree (ib, data_in, tag);
2492
2493   /* Read all the pointer fields in RESULT.  */
2494   lto_input_tree_pointers (ib, data_in, result);
2495
2496   /* Call back into the streaming module to read anything else it
2497      may need.  */
2498   if (streamer_hooks.read_tree)
2499     streamer_hooks.read_tree (ib, data_in, result);
2500
2501   /* We should never try to instantiate an MD or NORMAL builtin here.  */
2502   if (TREE_CODE (result) == FUNCTION_DECL)
2503     gcc_assert (!lto_stream_as_builtin_p (result));
2504
2505   /* end_marker = */ lto_input_1_unsigned (ib);
2506
2507 #ifdef LTO_STREAMER_DEBUG
2508   /* Remove the mapping to RESULT's original address set by
2509      lto_materialize_tree.  */
2510   lto_orig_address_remove (result);
2511 #endif
2512
2513   return result;
2514 }
2515
2516
2517 /* LTO streamer hook for reading GIMPLE trees.  IB and DATA_IN are as in
2518    lto_read_tree.  EXPR is the tree was materialized by lto_read_tree and
2519    needs GIMPLE specific data to be filled in.  */
2520
2521 void
2522 lto_streamer_read_tree (struct lto_input_block *ib, struct data_in *data_in,
2523                         tree expr)
2524 {
2525   if (DECL_P (expr)
2526       && TREE_CODE (expr) != FUNCTION_DECL
2527       && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
2528     DECL_INITIAL (expr) = lto_input_tree (ib, data_in);
2529 }
2530
2531
2532 /* Read and INTEGER_CST node from input block IB using the per-file
2533    context in DATA_IN.  */
2534
2535 static tree
2536 lto_input_integer_cst (struct lto_input_block *ib, struct data_in *data_in)
2537 {
2538   tree result, type;
2539   HOST_WIDE_INT low, high;
2540   bool overflow_p;
2541
2542   type = lto_input_tree (ib, data_in);
2543   overflow_p = (lto_input_1_unsigned (ib) != 0);
2544   low = lto_input_uleb128 (ib);
2545   high = lto_input_uleb128 (ib);
2546   result = build_int_cst_wide (type, low, high);
2547
2548   /* If the original constant had overflown, build a replica of RESULT to
2549      avoid modifying the shared constant returned by build_int_cst_wide.  */
2550   if (overflow_p)
2551     {
2552       result = copy_node (result);
2553       TREE_OVERFLOW (result) = 1;
2554     }
2555
2556   return result;
2557 }
2558
2559
2560 /* Read a tree from input block IB using the per-file context in
2561    DATA_IN.  This context is used, for example, to resolve references
2562    to previously read nodes.  */
2563
2564 tree
2565 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
2566 {
2567   enum LTO_tags tag;
2568   tree result;
2569
2570   tag = input_record_start (ib);
2571   gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
2572
2573   if (tag == LTO_null)
2574     result = NULL_TREE;
2575   else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
2576     {
2577       /* If TAG is a reference to an indexable tree, the next value
2578          in IB is the index into the table where we expect to find
2579          that tree.  */
2580       result = lto_input_tree_ref (ib, data_in, cfun, tag);
2581     }
2582   else if (tag == LTO_tree_pickle_reference)
2583     {
2584       /* If TAG is a reference to a previously read tree, look it up in
2585          the reader cache.  */
2586       result = lto_get_pickled_tree (ib, data_in);
2587     }
2588   else if (tag == LTO_builtin_decl)
2589     {
2590       /* If we are going to read a built-in function, all we need is
2591          the code and class.  */
2592       result = lto_get_builtin_tree (ib, data_in);
2593     }
2594   else if (tag == lto_tree_code_to_tag (INTEGER_CST))
2595     {
2596       /* For integer constants we only need the type and its hi/low
2597          words.  */
2598       result = lto_input_integer_cst (ib, data_in);
2599     }
2600   else
2601     {
2602       /* Otherwise, materialize a new node from IB.  */
2603       result = lto_read_tree (ib, data_in, tag);
2604     }
2605
2606   return result;
2607 }
2608
2609
2610 /* Initialization for the LTO reader.  */
2611
2612 void
2613 lto_reader_init (void)
2614 {
2615   lto_streamer_init ();
2616   file_name_hash_table = htab_create (37, hash_string_slot_node,
2617                                       eq_string_slot_node, free);
2618 }
2619
2620
2621 /* Create a new data_in object for FILE_DATA. STRINGS is the string
2622    table to use with LEN strings.  RESOLUTIONS is the vector of linker
2623    resolutions (NULL if not using a linker plugin).  */
2624
2625 struct data_in *
2626 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
2627                     unsigned len,
2628                     VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
2629 {
2630   struct data_in *data_in = XCNEW (struct data_in);
2631   data_in->file_data = file_data;
2632   data_in->strings = strings;
2633   data_in->strings_len = len;
2634   data_in->globals_resolution = resolutions;
2635   data_in->reader_cache = lto_streamer_cache_create ();
2636
2637   return data_in;
2638 }
2639
2640
2641 /* Remove DATA_IN.  */
2642
2643 void
2644 lto_data_in_delete (struct data_in *data_in)
2645 {
2646   VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
2647   lto_streamer_cache_delete (data_in->reader_cache);
2648   free (data_in->labels);
2649   free (data_in);
2650 }