OSDN Git Service

libgo: Define CC_FOR_BUILD in Makefile.
[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   eh_initialized_p = true;
661 }
662
663
664 /* Read the exception table for FN from IB using the data descriptors
665    in DATA_IN.  */
666
667 static void
668 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
669                   struct function *fn)
670 {
671   HOST_WIDE_INT i, root_region, len;
672   enum LTO_tags tag;
673
674   tag = input_record_start (ib);
675   if (tag == LTO_null)
676     return;
677
678   lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
679
680   /* If the file contains EH regions, then it was compiled with
681      -fexceptions.  In that case, initialize the backend EH
682      machinery.  */
683   lto_init_eh ();
684
685   gcc_assert (fn->eh);
686
687   root_region = lto_input_sleb128 (ib);
688   gcc_assert (root_region == (int) root_region);
689
690   /* Read the EH region array.  */
691   len = lto_input_sleb128 (ib);
692   gcc_assert (len == (int) len);
693   if (len > 0)
694     {
695       VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
696       for (i = 0; i < len; i++)
697         {
698           eh_region r = input_eh_region (ib, data_in, i);
699           VEC_replace (eh_region, fn->eh->region_array, i, r);
700         }
701     }
702
703   /* Read the landing pads.  */
704   len = lto_input_sleb128 (ib);
705   gcc_assert (len == (int) len);
706   if (len > 0)
707     {
708       VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
709       for (i = 0; i < len; i++)
710         {
711           eh_landing_pad lp = input_eh_lp (ib, data_in, i);
712           VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
713         }
714     }
715
716   /* Read the runtime type data.  */
717   len = lto_input_sleb128 (ib);
718   gcc_assert (len == (int) len);
719   if (len > 0)
720     {
721       VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
722       for (i = 0; i < len; i++)
723         {
724           tree ttype = lto_input_tree (ib, data_in);
725           VEC_replace (tree, fn->eh->ttype_data, i, ttype);
726         }
727     }
728
729   /* Read the table of action chains.  */
730   len = lto_input_sleb128 (ib);
731   gcc_assert (len == (int) len);
732   if (len > 0)
733     {
734       if (targetm.arm_eabi_unwinder)
735         {
736           VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
737           for (i = 0; i < len; i++)
738             {
739               tree t = lto_input_tree (ib, data_in);
740               VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
741             }
742         }
743       else
744         {
745           VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
746           for (i = 0; i < len; i++)
747             {
748               uchar c = lto_input_1_unsigned (ib);
749               VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
750             }
751         }
752     }
753
754   /* Reconstruct the EH region tree by fixing up the peer/children
755      pointers.  */
756   fixup_eh_region_pointers (fn, root_region);
757
758   tag = input_record_start (ib);
759   lto_tag_check_range (tag, LTO_null, LTO_null);
760 }
761
762
763 /* Make a new basic block with index INDEX in function FN.  */
764
765 static basic_block
766 make_new_block (struct function *fn, unsigned int index)
767 {
768   basic_block bb = alloc_block ();
769   bb->index = index;
770   SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
771   bb->il.gimple = ggc_alloc_cleared_gimple_bb_info ();
772   n_basic_blocks_for_function (fn)++;
773   bb->flags = 0;
774   set_bb_seq (bb, gimple_seq_alloc ());
775   return bb;
776 }
777
778
779 /* Read the CFG for function FN from input block IB.  */
780
781 static void
782 input_cfg (struct lto_input_block *ib, struct function *fn,
783            int count_materialization_scale)
784 {
785   unsigned int bb_count;
786   basic_block p_bb;
787   unsigned int i;
788   int index;
789
790   init_empty_tree_cfg_for_function (fn);
791   init_ssa_operands ();
792
793   profile_status_for_function (fn) = lto_input_enum (ib, profile_status_d, PROFILE_LAST);
794
795   bb_count = lto_input_uleb128 (ib);
796
797   last_basic_block_for_function (fn) = bb_count;
798   if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
799     VEC_safe_grow_cleared (basic_block, gc,
800                            basic_block_info_for_function (fn), bb_count);
801
802   if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
803     VEC_safe_grow_cleared (basic_block, gc,
804                            label_to_block_map_for_function (fn), bb_count);
805
806   index = lto_input_sleb128 (ib);
807   while (index != -1)
808     {
809       basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
810       unsigned int edge_count;
811
812       if (bb == NULL)
813         bb = make_new_block (fn, index);
814
815       edge_count = lto_input_uleb128 (ib);
816
817       /* Connect up the CFG.  */
818       for (i = 0; i < edge_count; i++)
819         {
820           unsigned int dest_index;
821           unsigned int edge_flags;
822           basic_block dest;
823           int probability;
824           gcov_type count;
825           edge e;
826
827           dest_index = lto_input_uleb128 (ib);
828           probability = (int) lto_input_sleb128 (ib);
829           count = ((gcov_type) lto_input_sleb128 (ib) * count_materialization_scale
830                    + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
831           edge_flags = lto_input_uleb128 (ib);
832
833           dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
834
835           if (dest == NULL)
836             dest = make_new_block (fn, dest_index);
837
838           e = make_edge (bb, dest, edge_flags);
839           e->probability = probability;
840           e->count = count;
841         }
842
843       index = lto_input_sleb128 (ib);
844     }
845
846   p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
847   index = lto_input_sleb128 (ib);
848   while (index != -1)
849     {
850       basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
851       bb->prev_bb = p_bb;
852       p_bb->next_bb = bb;
853       p_bb = bb;
854       index = lto_input_sleb128 (ib);
855     }
856 }
857
858
859 /* Read a PHI function for basic block BB in function FN.  DATA_IN is
860    the file being read.  IB is the input block to use for reading.  */
861
862 static gimple
863 input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in,
864            struct function *fn)
865 {
866   unsigned HOST_WIDE_INT ix;
867   tree phi_result;
868   int i, len;
869   gimple result;
870
871   ix = lto_input_uleb128 (ib);
872   phi_result = VEC_index (tree, SSANAMES (fn), ix);
873   len = EDGE_COUNT (bb->preds);
874   result = create_phi_node (phi_result, bb);
875   SSA_NAME_DEF_STMT (phi_result) = result;
876
877   /* We have to go through a lookup process here because the preds in the
878      reconstructed graph are generally in a different order than they
879      were in the original program.  */
880   for (i = 0; i < len; i++)
881     {
882       tree def = lto_input_tree (ib, data_in);
883       int src_index = lto_input_uleb128 (ib);
884       location_t arg_loc = lto_input_location (ib, data_in);
885       basic_block sbb = BASIC_BLOCK_FOR_FUNCTION (fn, src_index);
886
887       edge e = NULL;
888       int j;
889
890       for (j = 0; j < len; j++)
891         if (EDGE_PRED (bb, j)->src == sbb)
892           {
893             e = EDGE_PRED (bb, j);
894             break;
895           }
896
897       add_phi_arg (result, def, e, arg_loc);
898     }
899
900   return result;
901 }
902
903
904 /* Read the SSA names array for function FN from DATA_IN using input
905    block IB.  */
906
907 static void
908 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
909                  struct function *fn)
910 {
911   unsigned int i, size;
912
913   size = lto_input_uleb128 (ib);
914   init_ssanames (fn, size);
915
916   i = lto_input_uleb128 (ib);
917   while (i)
918     {
919       tree ssa_name, name;
920       bool is_default_def;
921
922       /* Skip over the elements that had been freed.  */
923       while (VEC_length (tree, SSANAMES (fn)) < i)
924         VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
925
926       is_default_def = (lto_input_1_unsigned (ib) != 0);
927       name = lto_input_tree (ib, data_in);
928       ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
929
930       if (is_default_def)
931         set_default_def (SSA_NAME_VAR (ssa_name), ssa_name);
932
933       i = lto_input_uleb128 (ib);
934     }
935 }
936
937 /* Read a statement with tag TAG in function FN from block IB using
938    descriptors in DATA_IN.  */
939
940 static gimple
941 input_gimple_stmt (struct lto_input_block *ib, struct data_in *data_in,
942                    struct function *fn, enum LTO_tags tag)
943 {
944   gimple stmt;
945   enum gimple_code code;
946   unsigned HOST_WIDE_INT num_ops;
947   size_t i;
948   struct bitpack_d bp;
949
950   code = lto_tag_to_gimple_code (tag);
951
952   /* Read the tuple header.  */
953   bp = lto_input_bitpack (ib);
954   num_ops = bp_unpack_var_len_unsigned (&bp);
955   stmt = gimple_alloc (code, num_ops);
956   stmt->gsbase.no_warning = bp_unpack_value (&bp, 1);
957   if (is_gimple_assign (stmt))
958     stmt->gsbase.nontemporal_move = bp_unpack_value (&bp, 1);
959   stmt->gsbase.has_volatile_ops = bp_unpack_value (&bp, 1);
960   stmt->gsbase.subcode = bp_unpack_var_len_unsigned (&bp);
961
962   /* Read location information.  */
963   gimple_set_location (stmt, lto_input_location (ib, data_in));
964
965   /* Read lexical block reference.  */
966   gimple_set_block (stmt, lto_input_tree (ib, data_in));
967
968   /* Read in all the operands.  */
969   switch (code)
970     {
971     case GIMPLE_RESX:
972       gimple_resx_set_region (stmt, lto_input_sleb128 (ib));
973       break;
974
975     case GIMPLE_EH_MUST_NOT_THROW:
976       gimple_eh_must_not_throw_set_fndecl (stmt, lto_input_tree (ib, data_in));
977       break;
978
979     case GIMPLE_EH_DISPATCH:
980       gimple_eh_dispatch_set_region (stmt, lto_input_sleb128 (ib));
981       break;
982
983     case GIMPLE_ASM:
984       {
985         /* FIXME lto.  Move most of this into a new gimple_asm_set_string().  */
986         tree str;
987         stmt->gimple_asm.ni = lto_input_uleb128 (ib);
988         stmt->gimple_asm.no = lto_input_uleb128 (ib);
989         stmt->gimple_asm.nc = lto_input_uleb128 (ib);
990         stmt->gimple_asm.nl = lto_input_uleb128 (ib);
991         str = input_string_cst (data_in, ib);
992         stmt->gimple_asm.string = TREE_STRING_POINTER (str);
993       }
994       /* Fallthru  */
995
996     case GIMPLE_ASSIGN:
997     case GIMPLE_CALL:
998     case GIMPLE_RETURN:
999     case GIMPLE_SWITCH:
1000     case GIMPLE_LABEL:
1001     case GIMPLE_COND:
1002     case GIMPLE_GOTO:
1003     case GIMPLE_DEBUG:
1004       for (i = 0; i < num_ops; i++)
1005         {
1006           tree op = lto_input_tree (ib, data_in);
1007           gimple_set_op (stmt, i, op);
1008           if (!op)
1009             continue;
1010
1011           /* Fixup FIELD_DECLs in COMPONENT_REFs, they are not handled
1012              by decl merging.  */
1013           if (TREE_CODE (op) == ADDR_EXPR)
1014             op = TREE_OPERAND (op, 0);
1015           while (handled_component_p (op))
1016             {
1017               if (TREE_CODE (op) == COMPONENT_REF)
1018                 {
1019                   tree field, type, tem;
1020                   tree closest_match = NULL_TREE;
1021                   field = TREE_OPERAND (op, 1);
1022                   type = DECL_CONTEXT (field);
1023                   for (tem = TYPE_FIELDS (type); tem; tem = TREE_CHAIN (tem))
1024                     {
1025                       if (tem == field)
1026                         break;
1027                       if (DECL_NONADDRESSABLE_P (tem)
1028                           == DECL_NONADDRESSABLE_P (field)
1029                           && gimple_compare_field_offset (tem, field))
1030                         {
1031                           if (types_compatible_p (TREE_TYPE (tem),
1032                                                   TREE_TYPE (field)))
1033                             break;
1034                           else
1035                             closest_match = tem;
1036                         }
1037                     }
1038                   /* In case of type mismatches across units we can fail
1039                      to unify some types and thus not find a proper
1040                      field-decl here.  */
1041                   if (tem == NULL_TREE)
1042                     {
1043                       /* Thus, emit a ODR violation warning.  */
1044                       if (warning_at (gimple_location (stmt), 0,
1045                                       "use of type %<%E%> with two mismatching "
1046                                       "declarations at field %<%E%>",
1047                                       type, TREE_OPERAND (op, 1)))
1048                         {
1049                           if (TYPE_FIELDS (type))
1050                             inform (DECL_SOURCE_LOCATION (TYPE_FIELDS (type)),
1051                                     "original type declared here");
1052                           inform (DECL_SOURCE_LOCATION (TREE_OPERAND (op, 1)),
1053                                   "field in mismatching type declared here");
1054                           if (TYPE_NAME (TREE_TYPE (field))
1055                               && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
1056                                   == TYPE_DECL))
1057                             inform (DECL_SOURCE_LOCATION
1058                                       (TYPE_NAME (TREE_TYPE (field))),
1059                                     "type of field declared here");
1060                           if (closest_match
1061                               && TYPE_NAME (TREE_TYPE (closest_match))
1062                               && (TREE_CODE (TYPE_NAME
1063                                    (TREE_TYPE (closest_match))) == TYPE_DECL))
1064                             inform (DECL_SOURCE_LOCATION
1065                                       (TYPE_NAME (TREE_TYPE (closest_match))),
1066                                     "type of mismatching field declared here");
1067                         }
1068                       /* And finally fixup the types.  */
1069                       TREE_OPERAND (op, 0)
1070                         = build1 (VIEW_CONVERT_EXPR, type,
1071                                   TREE_OPERAND (op, 0));
1072                     }
1073                   else
1074                     TREE_OPERAND (op, 1) = tem;
1075                 }
1076
1077               op = TREE_OPERAND (op, 0);
1078             }
1079         }
1080       if (is_gimple_call (stmt))
1081         {
1082           if (gimple_call_internal_p (stmt))
1083             gimple_call_set_internal_fn
1084               (stmt, lto_input_enum (ib, internal_fn, IFN_LAST));
1085           else
1086             gimple_call_set_fntype (stmt, lto_input_tree (ib, data_in));
1087         }
1088       break;
1089
1090     case GIMPLE_NOP:
1091     case GIMPLE_PREDICT:
1092       break;
1093
1094     default:
1095       internal_error ("bytecode stream: unknown GIMPLE statement tag %s",
1096                       lto_tag_name (tag));
1097     }
1098
1099   /* Update the properties of symbols, SSA names and labels associated
1100      with STMT.  */
1101   if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
1102     {
1103       tree lhs = gimple_get_lhs (stmt);
1104       if (lhs && TREE_CODE (lhs) == SSA_NAME)
1105         SSA_NAME_DEF_STMT (lhs) = stmt;
1106     }
1107   else if (code == GIMPLE_LABEL)
1108     gcc_assert (emit_label_in_global_context_p (gimple_label_label (stmt))
1109                 || DECL_CONTEXT (gimple_label_label (stmt)) == fn->decl);
1110   else if (code == GIMPLE_ASM)
1111     {
1112       unsigned i;
1113
1114       for (i = 0; i < gimple_asm_noutputs (stmt); i++)
1115         {
1116           tree op = TREE_VALUE (gimple_asm_output_op (stmt, i));
1117           if (TREE_CODE (op) == SSA_NAME)
1118             SSA_NAME_DEF_STMT (op) = stmt;
1119         }
1120     }
1121
1122   /* Reset alias information.  */
1123   if (code == GIMPLE_CALL)
1124     gimple_call_reset_alias_info (stmt);
1125
1126   /* Mark the statement modified so its operand vectors can be filled in.  */
1127   gimple_set_modified (stmt, true);
1128
1129   return stmt;
1130 }
1131
1132
1133 /* Read a basic block with tag TAG from DATA_IN using input block IB.
1134    FN is the function being processed.  */
1135
1136 static void
1137 input_bb (struct lto_input_block *ib, enum LTO_tags tag,
1138           struct data_in *data_in, struct function *fn,
1139           int count_materialization_scale)
1140 {
1141   unsigned int index;
1142   basic_block bb;
1143   gimple_stmt_iterator bsi;
1144
1145   /* This routine assumes that CFUN is set to FN, as it needs to call
1146      basic GIMPLE routines that use CFUN.  */
1147   gcc_assert (cfun == fn);
1148
1149   index = lto_input_uleb128 (ib);
1150   bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
1151
1152   bb->count = (lto_input_sleb128 (ib) * count_materialization_scale
1153                + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
1154   bb->loop_depth = lto_input_sleb128 (ib);
1155   bb->frequency = lto_input_sleb128 (ib);
1156   bb->flags = lto_input_sleb128 (ib);
1157
1158   /* LTO_bb1 has statements.  LTO_bb0 does not.  */
1159   if (tag == LTO_bb0)
1160     return;
1161
1162   bsi = gsi_start_bb (bb);
1163   tag = input_record_start (ib);
1164   while (tag)
1165     {
1166       gimple stmt = input_gimple_stmt (ib, data_in, fn, tag);
1167       if (!is_gimple_debug (stmt))
1168         find_referenced_vars_in (stmt);
1169       gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1170
1171       /* After the statement, expect a 0 delimiter or the EH region
1172          that the previous statement belongs to.  */
1173       tag = input_record_start (ib);
1174       lto_tag_check_set (tag, 2, LTO_eh_region, LTO_null);
1175
1176       if (tag == LTO_eh_region)
1177         {
1178           HOST_WIDE_INT region = lto_input_sleb128 (ib);
1179           gcc_assert (region == (int) region);
1180           add_stmt_to_eh_lp (stmt, region);
1181         }
1182
1183       tag = input_record_start (ib);
1184     }
1185
1186   tag = input_record_start (ib);
1187   while (tag)
1188     {
1189       gimple phi = input_phi (ib, bb, data_in, fn);
1190       find_referenced_vars_in (phi);
1191       tag = input_record_start (ib);
1192     }
1193 }
1194
1195 /* Go through all NODE edges and fixup call_stmt pointers
1196    so they point to STMTS.  */
1197
1198 static void
1199 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
1200 {
1201   struct cgraph_edge *cedge;
1202   for (cedge = node->callees; cedge; cedge = cedge->next_callee)
1203     cedge->call_stmt = stmts[cedge->lto_stmt_uid];
1204   for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
1205     cedge->call_stmt = stmts[cedge->lto_stmt_uid];
1206 }
1207
1208 /* Fixup call_stmt pointers in NODE and all clones.  */
1209
1210 static void
1211 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
1212 {
1213   struct cgraph_node *node;
1214
1215   while (orig->clone_of)
1216     orig = orig->clone_of;
1217
1218   fixup_call_stmt_edges_1 (orig, stmts);
1219   if (orig->clones)
1220     for (node = orig->clones; node != orig;)
1221       {
1222         fixup_call_stmt_edges_1 (node, stmts);
1223         if (node->clones)
1224           node = node->clones;
1225         else if (node->next_sibling_clone)
1226           node = node->next_sibling_clone;
1227         else
1228           {
1229             while (node != orig && !node->next_sibling_clone)
1230               node = node->clone_of;
1231             if (node != orig)
1232               node = node->next_sibling_clone;
1233           }
1234       }
1235 }
1236
1237 /* Read the body of function FN_DECL from DATA_IN using input block IB.  */
1238
1239 static void
1240 input_function (tree fn_decl, struct data_in *data_in,
1241                 struct lto_input_block *ib)
1242 {
1243   struct function *fn;
1244   enum LTO_tags tag;
1245   gimple *stmts;
1246   basic_block bb;
1247   struct bitpack_d bp;
1248   struct cgraph_node *node;
1249   tree args, narg, oarg;
1250   int len;
1251
1252   fn = DECL_STRUCT_FUNCTION (fn_decl);
1253   tag = input_record_start (ib);
1254   clear_line_info (data_in);
1255
1256   gimple_register_cfg_hooks ();
1257   lto_tag_check (tag, LTO_function);
1258
1259   /* Read all the attributes for FN.  */
1260   bp = lto_input_bitpack (ib);
1261   fn->is_thunk = bp_unpack_value (&bp, 1);
1262   fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
1263   fn->after_tree_profile = bp_unpack_value (&bp, 1);
1264   fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
1265   fn->returns_struct = bp_unpack_value (&bp, 1);
1266   fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
1267   fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
1268   fn->after_inlining = bp_unpack_value (&bp, 1);
1269   fn->stdarg = bp_unpack_value (&bp, 1);
1270   fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
1271   fn->calls_alloca = bp_unpack_value (&bp, 1);
1272   fn->calls_setjmp = bp_unpack_value (&bp, 1);
1273   fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
1274   fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
1275
1276   /* Input the function start and end loci.  */
1277   fn->function_start_locus = lto_input_location (ib, data_in);
1278   fn->function_end_locus = lto_input_location (ib, data_in);
1279
1280   /* Input the current IL state of the function.  */
1281   fn->curr_properties = lto_input_uleb128 (ib);
1282
1283   /* Read the static chain and non-local goto save area.  */
1284   fn->static_chain_decl = lto_input_tree (ib, data_in);
1285   fn->nonlocal_goto_save_area = lto_input_tree (ib, data_in);
1286
1287   /* Read all the local symbols.  */
1288   len = lto_input_sleb128 (ib);
1289   if (len > 0)
1290     {
1291       int i;
1292       VEC_safe_grow (tree, gc, fn->local_decls, len);
1293       for (i = 0; i < len; i++)
1294         {
1295           tree t = lto_input_tree (ib, data_in);
1296           VEC_replace (tree, fn->local_decls, i, t);
1297         }
1298     }
1299
1300   /* Read all function arguments.  We need to re-map them here to the
1301      arguments of the merged function declaration.  */
1302   args = lto_input_tree (ib, data_in);
1303   for (oarg = args, narg = DECL_ARGUMENTS (fn_decl);
1304        oarg && narg;
1305        oarg = TREE_CHAIN (oarg), narg = TREE_CHAIN (narg))
1306     {
1307       unsigned ix;
1308       bool res;
1309       res = lto_streamer_cache_lookup (data_in->reader_cache, oarg, &ix);
1310       gcc_assert (res);
1311       /* Replace the argument in the streamer cache.  */
1312       lto_streamer_cache_insert_at (data_in->reader_cache, narg, ix);
1313     }
1314   gcc_assert (!oarg && !narg);
1315
1316   /* Read all the SSA names.  */
1317   input_ssa_names (ib, data_in, fn);
1318
1319   /* Read the exception handling regions in the function.  */
1320   input_eh_regions (ib, data_in, fn);
1321
1322   /* Read the tree of lexical scopes for the function.  */
1323   DECL_INITIAL (fn_decl) = lto_input_tree (ib, data_in);
1324   gcc_assert (DECL_INITIAL (fn_decl));
1325   DECL_SAVED_TREE (fn_decl) = NULL_TREE;
1326   node = cgraph_get_create_node (fn_decl);
1327
1328   /* Read all the basic blocks.  */
1329   tag = input_record_start (ib);
1330   while (tag)
1331     {
1332       input_bb (ib, tag, data_in, fn,
1333                 node->count_materialization_scale);
1334       tag = input_record_start (ib);
1335     }
1336
1337   /* Fix up the call statements that are mentioned in the callgraph
1338      edges.  */
1339   set_gimple_stmt_max_uid (cfun, 0);
1340   FOR_ALL_BB (bb)
1341     {
1342       gimple_stmt_iterator gsi;
1343       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1344         {
1345           gimple stmt = gsi_stmt (gsi);
1346           gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1347         }
1348     }
1349   stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1350   FOR_ALL_BB (bb)
1351     {
1352       gimple_stmt_iterator bsi = gsi_start_bb (bb);
1353       while (!gsi_end_p (bsi))
1354         {
1355           gimple stmt = gsi_stmt (bsi);
1356           /* If we're recompiling LTO objects with debug stmts but
1357              we're not supposed to have debug stmts, remove them now.
1358              We can't remove them earlier because this would cause uid
1359              mismatches in fixups, but we can do it at this point, as
1360              long as debug stmts don't require fixups.  */
1361           if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
1362             {
1363               gimple_stmt_iterator gsi = bsi;
1364               gsi_next (&bsi);
1365               gsi_remove (&gsi, true);
1366             }
1367           else
1368             {
1369               gsi_next (&bsi);
1370               stmts[gimple_uid (stmt)] = stmt;
1371             }
1372         }
1373     }
1374
1375   /* Set the gimple body to the statement sequence in the entry
1376      basic block.  FIXME lto, this is fairly hacky.  The existence
1377      of a gimple body is used by the cgraph routines, but we should
1378      really use the presence of the CFG.  */
1379   {
1380     edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
1381     gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1382   }
1383
1384   fixup_call_stmt_edges (node, stmts);
1385   execute_all_ipa_stmt_fixups (node, stmts);
1386
1387   update_ssa (TODO_update_ssa_only_virtuals);
1388   free_dominance_info (CDI_DOMINATORS);
1389   free_dominance_info (CDI_POST_DOMINATORS);
1390   free (stmts);
1391 }
1392
1393
1394 /* Read initializer expressions for public statics.  DATA_IN is the
1395    file being read.  IB is the input block used for reading.  */
1396
1397 static void
1398 input_alias_pairs (struct lto_input_block *ib, struct data_in *data_in)
1399 {
1400   tree var;
1401
1402   clear_line_info (data_in);
1403
1404   var = lto_input_tree (ib, data_in);
1405   while (var)
1406     {
1407       const char *orig_name, *new_name;
1408       alias_pair *p;
1409
1410       p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
1411       p->decl = var;
1412       p->target = lto_input_tree (ib, data_in);
1413
1414       /* If the target is a static object, we may have registered a
1415          new name for it to avoid clashes between statics coming from
1416          different files.  In that case, use the new name.  */
1417       orig_name = IDENTIFIER_POINTER (p->target);
1418       new_name = lto_get_decl_name_mapping (data_in->file_data, orig_name);
1419       if (strcmp (orig_name, new_name) != 0)
1420         p->target = get_identifier (new_name);
1421
1422       var = lto_input_tree (ib, data_in);
1423     }
1424 }
1425
1426
1427 /* Read the body from DATA for function FN_DECL and fill it in.
1428    FILE_DATA are the global decls and types.  SECTION_TYPE is either
1429    LTO_section_function_body or LTO_section_static_initializer.  If
1430    section type is LTO_section_function_body, FN must be the decl for
1431    that function.  */
1432
1433 static void
1434 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
1435                const char *data, enum lto_section_type section_type)
1436 {
1437   const struct lto_function_header *header;
1438   struct data_in *data_in;
1439   int32_t cfg_offset;
1440   int32_t main_offset;
1441   int32_t string_offset;
1442   struct lto_input_block ib_cfg;
1443   struct lto_input_block ib_main;
1444
1445   header = (const struct lto_function_header *) data;
1446   cfg_offset = sizeof (struct lto_function_header);
1447   main_offset = cfg_offset + header->cfg_size;
1448   string_offset = main_offset + header->main_size;
1449
1450   LTO_INIT_INPUT_BLOCK (ib_cfg,
1451                         data + cfg_offset,
1452                         0,
1453                         header->cfg_size);
1454
1455   LTO_INIT_INPUT_BLOCK (ib_main,
1456                         data + main_offset,
1457                         0,
1458                         header->main_size);
1459
1460   data_in = lto_data_in_create (file_data, data + string_offset,
1461                                 header->string_size, NULL);
1462
1463   /* Make sure the file was generated by the exact same compiler.  */
1464   lto_check_version (header->lto_header.major_version,
1465                      header->lto_header.minor_version);
1466
1467   if (section_type == LTO_section_function_body)
1468     {
1469       struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
1470       struct lto_in_decl_state *decl_state;
1471       struct cgraph_node *node = cgraph_get_node (fn_decl);
1472
1473       gcc_checking_assert (node);
1474       push_cfun (fn);
1475       init_tree_ssa (fn);
1476
1477       /* Use the function's decl state. */
1478       decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1479       gcc_assert (decl_state);
1480       file_data->current_decl_state = decl_state;
1481
1482       input_cfg (&ib_cfg, fn, node->count_materialization_scale);
1483
1484       /* Set up the struct function.  */
1485       input_function (fn_decl, data_in, &ib_main);
1486
1487       /* We should now be in SSA.  */
1488       cfun->gimple_df->in_ssa_p = true;
1489
1490       /* Restore decl state */
1491       file_data->current_decl_state = file_data->global_decl_state;
1492
1493       pop_cfun ();
1494     }
1495   else
1496     {
1497       input_alias_pairs (&ib_main, data_in);
1498     }
1499
1500   clear_line_info (data_in);
1501   lto_data_in_delete (data_in);
1502 }
1503
1504
1505 /* Read the body of FN_DECL using DATA.  FILE_DATA holds the global
1506    decls and types.  */
1507
1508 void
1509 lto_input_function_body (struct lto_file_decl_data *file_data,
1510                          tree fn_decl, const char *data)
1511 {
1512   current_function_decl = fn_decl;
1513   lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1514 }
1515
1516
1517 /* Read in VAR_DECL using DATA.  FILE_DATA holds the global decls and
1518    types.  */
1519
1520 void
1521 lto_input_constructors_and_inits (struct lto_file_decl_data *file_data,
1522                                   const char *data)
1523 {
1524   lto_read_body (file_data, NULL, data, LTO_section_static_initializer);
1525 }
1526
1527
1528 /* Unpack all the non-pointer fields of the TS_BASE structure of
1529    expression EXPR from bitpack BP.  */
1530
1531 static void
1532 unpack_ts_base_value_fields (struct bitpack_d *bp, tree expr)
1533 {
1534   /* Note that the code for EXPR has already been unpacked to create EXPR in
1535      lto_materialize_tree.  */
1536   if (!TYPE_P (expr))
1537     {
1538       TREE_SIDE_EFFECTS (expr) = (unsigned) bp_unpack_value (bp, 1);
1539       TREE_CONSTANT (expr) = (unsigned) bp_unpack_value (bp, 1);
1540       TREE_READONLY (expr) = (unsigned) bp_unpack_value (bp, 1);
1541
1542       /* TREE_PUBLIC is used on types to indicate that the type
1543          has a TYPE_CACHED_VALUES vector.  This is not streamed out,
1544          so we skip it here.  */
1545       TREE_PUBLIC (expr) = (unsigned) bp_unpack_value (bp, 1);
1546     }
1547   else
1548     bp_unpack_value (bp, 4);
1549   TREE_ADDRESSABLE (expr) = (unsigned) bp_unpack_value (bp, 1);
1550   TREE_THIS_VOLATILE (expr) = (unsigned) bp_unpack_value (bp, 1);
1551   if (DECL_P (expr))
1552     DECL_UNSIGNED (expr) = (unsigned) bp_unpack_value (bp, 1);
1553   else if (TYPE_P (expr))
1554     TYPE_UNSIGNED (expr) = (unsigned) bp_unpack_value (bp, 1);
1555   else
1556     bp_unpack_value (bp, 1);
1557   TREE_ASM_WRITTEN (expr) = (unsigned) bp_unpack_value (bp, 1);
1558   if (TYPE_P (expr))
1559     TYPE_ARTIFICIAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1560   else
1561     TREE_NO_WARNING (expr) = (unsigned) bp_unpack_value (bp, 1);
1562   TREE_USED (expr) = (unsigned) bp_unpack_value (bp, 1);
1563   TREE_NOTHROW (expr) = (unsigned) bp_unpack_value (bp, 1);
1564   TREE_STATIC (expr) = (unsigned) bp_unpack_value (bp, 1);
1565   TREE_PRIVATE (expr) = (unsigned) bp_unpack_value (bp, 1);
1566   TREE_PROTECTED (expr) = (unsigned) bp_unpack_value (bp, 1);
1567   TREE_DEPRECATED (expr) = (unsigned) bp_unpack_value (bp, 1);
1568   if (TYPE_P (expr))
1569     TYPE_SATURATING (expr) = (unsigned) bp_unpack_value (bp, 1);
1570   else if (TREE_CODE (expr) == SSA_NAME)
1571     SSA_NAME_IS_DEFAULT_DEF (expr) = (unsigned) bp_unpack_value (bp, 1);
1572   else
1573     bp_unpack_value (bp, 1);
1574 }
1575
1576
1577 /* Unpack all the non-pointer fields of the TS_REAL_CST structure of
1578    expression EXPR from bitpack BP.  */
1579
1580 static void
1581 unpack_ts_real_cst_value_fields (struct bitpack_d *bp, tree expr)
1582 {
1583   unsigned i;
1584   REAL_VALUE_TYPE r;
1585   REAL_VALUE_TYPE *rp;
1586
1587   r.cl = (unsigned) bp_unpack_value (bp, 2);
1588   r.decimal = (unsigned) bp_unpack_value (bp, 1);
1589   r.sign = (unsigned) bp_unpack_value (bp, 1);
1590   r.signalling = (unsigned) bp_unpack_value (bp, 1);
1591   r.canonical = (unsigned) bp_unpack_value (bp, 1);
1592   r.uexp = (unsigned) bp_unpack_value (bp, EXP_BITS);
1593   for (i = 0; i < SIGSZ; i++)
1594     r.sig[i] = (unsigned long) bp_unpack_value (bp, HOST_BITS_PER_LONG);
1595
1596   rp = ggc_alloc_real_value ();
1597   memcpy (rp, &r, sizeof (REAL_VALUE_TYPE));
1598   TREE_REAL_CST_PTR (expr) = rp;
1599 }
1600
1601
1602 /* Unpack all the non-pointer fields of the TS_FIXED_CST structure of
1603    expression EXPR from bitpack BP.  */
1604
1605 static void
1606 unpack_ts_fixed_cst_value_fields (struct bitpack_d *bp, tree expr)
1607 {
1608   struct fixed_value fv;
1609
1610   fv.mode = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
1611   fv.data.low = bp_unpack_var_len_int (bp);
1612   fv.data.high = bp_unpack_var_len_int (bp);
1613   TREE_FIXED_CST (expr) = fv;
1614 }
1615
1616
1617 /* Unpack all the non-pointer fields of the TS_DECL_COMMON structure
1618    of expression EXPR from bitpack BP.  */
1619
1620 static void
1621 unpack_ts_decl_common_value_fields (struct bitpack_d *bp, tree expr)
1622 {
1623   DECL_MODE (expr) = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
1624   DECL_NONLOCAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1625   DECL_VIRTUAL_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1626   DECL_IGNORED_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1627   DECL_ABSTRACT (expr) = (unsigned) bp_unpack_value (bp, 1);
1628   DECL_ARTIFICIAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1629   DECL_USER_ALIGN (expr) = (unsigned) bp_unpack_value (bp, 1);
1630   DECL_PRESERVE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1631   DECL_DEBUG_EXPR_IS_FROM (expr) = (unsigned) bp_unpack_value (bp, 1);
1632   DECL_EXTERNAL (expr) = (unsigned) bp_unpack_value (bp, 1);
1633   DECL_GIMPLE_REG_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1634   DECL_ALIGN (expr) = (unsigned) bp_unpack_var_len_unsigned (bp);
1635
1636   if (TREE_CODE (expr) == LABEL_DECL)
1637     {
1638       DECL_ERROR_ISSUED (expr) = (unsigned) bp_unpack_value (bp, 1);
1639       EH_LANDING_PAD_NR (expr) = (int) bp_unpack_var_len_unsigned (bp);
1640
1641       /* Always assume an initial value of -1 for LABEL_DECL_UID to
1642          force gimple_set_bb to recreate label_to_block_map.  */
1643       LABEL_DECL_UID (expr) = -1;
1644     }
1645
1646   if (TREE_CODE (expr) == FIELD_DECL)
1647     {
1648       DECL_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1);
1649       DECL_NONADDRESSABLE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1650       expr->decl_common.off_align = bp_unpack_value (bp, 8);
1651     }
1652
1653   if (TREE_CODE (expr) == RESULT_DECL
1654       || TREE_CODE (expr) == PARM_DECL
1655       || TREE_CODE (expr) == VAR_DECL)
1656     {
1657       DECL_BY_REFERENCE (expr) = (unsigned) bp_unpack_value (bp, 1);
1658       if (TREE_CODE (expr) == VAR_DECL
1659           || TREE_CODE (expr) == PARM_DECL)
1660         DECL_HAS_VALUE_EXPR_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1661       DECL_RESTRICTED_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1662     }
1663 }
1664
1665
1666 /* Unpack all the non-pointer fields of the TS_DECL_WRTL structure
1667    of expression EXPR from bitpack BP.  */
1668
1669 static void
1670 unpack_ts_decl_wrtl_value_fields (struct bitpack_d *bp, tree expr)
1671 {
1672   DECL_REGISTER (expr) = (unsigned) bp_unpack_value (bp, 1);
1673 }
1674
1675
1676 /* Unpack all the non-pointer fields of the TS_DECL_WITH_VIS structure
1677    of expression EXPR from bitpack BP.  */
1678
1679 static void
1680 unpack_ts_decl_with_vis_value_fields (struct bitpack_d *bp, tree expr)
1681 {
1682   DECL_DEFER_OUTPUT (expr) = (unsigned) bp_unpack_value (bp, 1);
1683   DECL_COMMON (expr) = (unsigned) bp_unpack_value (bp, 1);
1684   DECL_DLLIMPORT_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1685   DECL_WEAK (expr) = (unsigned) bp_unpack_value (bp, 1);
1686   DECL_SEEN_IN_BIND_EXPR_P (expr) = (unsigned) bp_unpack_value (bp,  1);
1687   DECL_COMDAT (expr) = (unsigned) bp_unpack_value (bp,  1);
1688   DECL_VISIBILITY (expr) = (enum symbol_visibility) bp_unpack_value (bp,  2);
1689   DECL_VISIBILITY_SPECIFIED (expr) = (unsigned) bp_unpack_value (bp,  1);
1690
1691   if (TREE_CODE (expr) == VAR_DECL)
1692     {
1693       DECL_HARD_REGISTER (expr) = (unsigned) bp_unpack_value (bp, 1);
1694       DECL_IN_TEXT_SECTION (expr) = (unsigned) bp_unpack_value (bp, 1);
1695       DECL_IN_CONSTANT_POOL (expr) = (unsigned) bp_unpack_value (bp, 1);
1696       DECL_TLS_MODEL (expr) = (enum tls_model) bp_unpack_value (bp,  3);
1697     }
1698
1699   if (VAR_OR_FUNCTION_DECL_P (expr))
1700     {
1701       priority_type p;
1702       p = (priority_type) bp_unpack_var_len_unsigned (bp);
1703       SET_DECL_INIT_PRIORITY (expr, p);
1704     }
1705 }
1706
1707
1708 /* Unpack all the non-pointer fields of the TS_FUNCTION_DECL structure
1709    of expression EXPR from bitpack BP.  */
1710
1711 static void
1712 unpack_ts_function_decl_value_fields (struct bitpack_d *bp, tree expr)
1713 {
1714   DECL_BUILT_IN_CLASS (expr) = bp_unpack_enum (bp, built_in_class,
1715                                                BUILT_IN_LAST);
1716   DECL_STATIC_CONSTRUCTOR (expr) = (unsigned) bp_unpack_value (bp, 1);
1717   DECL_STATIC_DESTRUCTOR (expr) = (unsigned) bp_unpack_value (bp, 1);
1718   DECL_UNINLINABLE (expr) = (unsigned) bp_unpack_value (bp, 1);
1719   DECL_POSSIBLY_INLINED (expr) = (unsigned) bp_unpack_value (bp, 1);
1720   DECL_IS_NOVOPS (expr) = (unsigned) bp_unpack_value (bp, 1);
1721   DECL_IS_RETURNS_TWICE (expr) = (unsigned) bp_unpack_value (bp, 1);
1722   DECL_IS_MALLOC (expr) = (unsigned) bp_unpack_value (bp, 1);
1723   DECL_IS_OPERATOR_NEW (expr) = (unsigned) bp_unpack_value (bp, 1);
1724   DECL_DECLARED_INLINE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1725   DECL_STATIC_CHAIN (expr) = (unsigned) bp_unpack_value (bp, 1);
1726   DECL_NO_INLINE_WARNING_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1727   DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (expr)
1728                         = (unsigned) bp_unpack_value (bp, 1);
1729   DECL_NO_LIMIT_STACK (expr) = (unsigned) bp_unpack_value (bp, 1);
1730   DECL_DISREGARD_INLINE_LIMITS (expr) = (unsigned) bp_unpack_value (bp, 1);
1731   DECL_PURE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1732   DECL_LOOPING_CONST_OR_PURE_P (expr) = (unsigned) bp_unpack_value (bp, 1);
1733   if (DECL_BUILT_IN_CLASS (expr) != NOT_BUILT_IN)
1734     {
1735       DECL_FUNCTION_CODE (expr) = (enum built_in_function) bp_unpack_value (bp, 11);
1736       if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
1737           && DECL_FUNCTION_CODE (expr) >= END_BUILTINS)
1738         fatal_error ("machine independent builtin code out of range");
1739       else if (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD)
1740         {
1741           tree result = targetm.builtin_decl (DECL_FUNCTION_CODE (expr), true);
1742           if (!result || result == error_mark_node)
1743             fatal_error ("target specific builtin not available");
1744         }
1745     }
1746   if (DECL_STATIC_DESTRUCTOR (expr))
1747     {
1748       priority_type p;
1749       p = (priority_type) bp_unpack_var_len_unsigned (bp);
1750       SET_DECL_FINI_PRIORITY (expr, p);
1751     }
1752 }
1753
1754
1755 /* Unpack all the non-pointer fields of the TS_TYPE_COMMON structure
1756    of expression EXPR from bitpack BP.  */
1757
1758 static void
1759 unpack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr)
1760 {
1761   enum machine_mode mode;
1762
1763   mode = bp_unpack_enum (bp, machine_mode, MAX_MACHINE_MODE);
1764   SET_TYPE_MODE (expr, mode);
1765   TYPE_STRING_FLAG (expr) = (unsigned) bp_unpack_value (bp, 1);
1766   TYPE_NO_FORCE_BLK (expr) = (unsigned) bp_unpack_value (bp, 1);
1767   TYPE_NEEDS_CONSTRUCTING (expr) = (unsigned) bp_unpack_value (bp, 1);
1768   if (RECORD_OR_UNION_TYPE_P (expr))
1769     TYPE_TRANSPARENT_AGGR (expr) = (unsigned) bp_unpack_value (bp, 1);
1770   TYPE_PACKED (expr) = (unsigned) bp_unpack_value (bp, 1);
1771   TYPE_RESTRICT (expr) = (unsigned) bp_unpack_value (bp, 1);
1772   TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
1773         = (unsigned) bp_unpack_value (bp, 2);
1774   TYPE_USER_ALIGN (expr) = (unsigned) bp_unpack_value (bp, 1);
1775   TYPE_READONLY (expr) = (unsigned) bp_unpack_value (bp, 1);
1776   TYPE_PRECISION (expr) = bp_unpack_var_len_unsigned (bp);
1777   TYPE_ALIGN (expr) = bp_unpack_var_len_unsigned (bp);
1778   TYPE_ALIAS_SET (expr) = bp_unpack_var_len_int (bp);
1779 }
1780
1781
1782 /* Unpack all the non-pointer fields of the TS_BLOCK structure
1783    of expression EXPR from bitpack BP.  */
1784
1785 static void
1786 unpack_ts_block_value_fields (struct bitpack_d *bp, tree expr)
1787 {
1788   BLOCK_ABSTRACT (expr) = (unsigned) bp_unpack_value (bp, 1);
1789   /* BLOCK_NUMBER is recomputed.  */
1790 }
1791
1792 /* Unpack all the non-pointer fields of the TS_TRANSLATION_UNIT_DECL
1793    structure of expression EXPR from bitpack BP.  */
1794
1795 static void
1796 unpack_ts_translation_unit_decl_value_fields (struct bitpack_d *bp ATTRIBUTE_UNUSED, tree expr ATTRIBUTE_UNUSED)
1797 {
1798 }
1799
1800 /* Unpack all the non-pointer fields in EXPR into a bit pack.  */
1801
1802 static void
1803 unpack_value_fields (struct bitpack_d *bp, tree expr)
1804 {
1805   enum tree_code code;
1806
1807   code = TREE_CODE (expr);
1808
1809   /* Note that all these functions are highly sensitive to changes in
1810      the types and sizes of each of the fields being packed.  */
1811   unpack_ts_base_value_fields (bp, expr);
1812
1813   if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1814     unpack_ts_real_cst_value_fields (bp, expr);
1815
1816   if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1817     unpack_ts_fixed_cst_value_fields (bp, expr);
1818
1819   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1820     unpack_ts_decl_common_value_fields (bp, expr);
1821
1822   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1823     unpack_ts_decl_wrtl_value_fields (bp, expr);
1824
1825   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1826     unpack_ts_decl_with_vis_value_fields (bp, expr);
1827
1828   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1829     unpack_ts_function_decl_value_fields (bp, expr);
1830
1831   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1832     unpack_ts_type_common_value_fields (bp, expr);
1833
1834   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1835     unpack_ts_block_value_fields (bp, expr);
1836
1837   if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1838     unpack_ts_translation_unit_decl_value_fields (bp, expr);
1839
1840   if (streamer_hooks.unpack_value_fields)
1841     streamer_hooks.unpack_value_fields (bp, expr);
1842 }
1843
1844
1845 /* Materialize a new tree from input block IB using descriptors in
1846    DATA_IN.  The code for the new tree should match TAG.  Store in
1847    *IX_P the index into the reader cache where the new tree is stored.  */
1848
1849 static tree
1850 lto_materialize_tree (struct lto_input_block *ib, struct data_in *data_in,
1851                       enum LTO_tags tag)
1852 {
1853   struct bitpack_d bp;
1854   enum tree_code code;
1855   tree result;
1856 #ifdef LTO_STREAMER_DEBUG
1857   HOST_WIDEST_INT orig_address_in_writer;
1858 #endif
1859
1860   result = NULL_TREE;
1861
1862 #ifdef LTO_STREAMER_DEBUG
1863   /* Read the word representing the memory address for the tree
1864      as it was written by the writer.  This is useful when
1865      debugging differences between the writer and reader.  */
1866   orig_address_in_writer = lto_input_sleb128 (ib);
1867   gcc_assert ((intptr_t) orig_address_in_writer == orig_address_in_writer);
1868 #endif
1869
1870   code = lto_tag_to_tree_code (tag);
1871
1872   /* We should never see an SSA_NAME tree.  Only the version numbers of
1873      SSA names are ever written out.  See input_ssa_names.  */
1874   gcc_assert (code != SSA_NAME);
1875
1876   /* Instantiate a new tree using the header data.  */
1877   if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1878     result = input_string_cst (data_in, ib);
1879   else if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1880     result = input_identifier (data_in, ib);
1881   else if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1882     {
1883       HOST_WIDE_INT len = lto_input_sleb128 (ib);
1884       result = make_tree_vec (len);
1885     }
1886   else if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1887     {
1888       unsigned HOST_WIDE_INT len = lto_input_uleb128 (ib);
1889       result = make_tree_binfo (len);
1890     }
1891   else
1892     {
1893       /* For all other nodes, see if the streamer knows how to allocate
1894          it.  */
1895       if (streamer_hooks.alloc_tree)
1896         result = streamer_hooks.alloc_tree (code, ib, data_in);
1897
1898       /* If the hook did not handle it, materialize the tree with a raw
1899          make_node call.  */
1900       if (result == NULL_TREE)
1901         result = make_node (code);
1902     }
1903
1904 #ifdef LTO_STREAMER_DEBUG
1905   /* Store the original address of the tree as seen by the writer
1906      in RESULT's aux field.  This is useful when debugging streaming
1907      problems.  This way, a debugging session can be started on
1908      both writer and reader with a breakpoint using this address
1909      value in both.  */
1910   lto_orig_address_map (result, (intptr_t) orig_address_in_writer);
1911 #endif
1912
1913   /* Read the bitpack of non-pointer values from IB.  */
1914   bp = lto_input_bitpack (ib);
1915
1916   /* The first word in BP contains the code of the tree that we
1917      are about to read.  */
1918   code = (enum tree_code) bp_unpack_value (&bp, 16);
1919   lto_tag_check (lto_tree_code_to_tag (code), tag);
1920
1921   /* Unpack all the value fields from BP.  */
1922   unpack_value_fields (&bp, result);
1923
1924   /* Enter RESULT in the reader cache.  This will make RESULT
1925      available so that circular references in the rest of the tree
1926      structure can be resolved in subsequent calls to lto_input_tree.  */
1927   lto_streamer_cache_append (data_in->reader_cache, result);
1928
1929   return result;
1930 }
1931
1932
1933 /* Read all pointer fields in the TS_COMMON structure of EXPR from input
1934    block IB.  DATA_IN contains tables and descriptors for the
1935    file being read.  */
1936
1937
1938 static void
1939 lto_input_ts_common_tree_pointers (struct lto_input_block *ib,
1940                                    struct data_in *data_in, tree expr)
1941 {
1942   if (TREE_CODE (expr) != IDENTIFIER_NODE)
1943     TREE_TYPE (expr) = lto_input_tree (ib, data_in);
1944 }
1945
1946
1947 /* Read all pointer fields in the TS_VECTOR structure of EXPR from input
1948    block IB.  DATA_IN contains tables and descriptors for the
1949    file being read.  */
1950
1951 static void
1952 lto_input_ts_vector_tree_pointers (struct lto_input_block *ib,
1953                                    struct data_in *data_in, tree expr)
1954 {
1955   TREE_VECTOR_CST_ELTS (expr) = lto_input_chain (ib, data_in);
1956 }
1957
1958
1959 /* Read all pointer fields in the TS_COMPLEX structure of EXPR from input
1960    block IB.  DATA_IN contains tables and descriptors for the
1961    file being read.  */
1962
1963 static void
1964 lto_input_ts_complex_tree_pointers (struct lto_input_block *ib,
1965                                     struct data_in *data_in, tree expr)
1966 {
1967   TREE_REALPART (expr) = lto_input_tree (ib, data_in);
1968   TREE_IMAGPART (expr) = lto_input_tree (ib, data_in);
1969 }
1970
1971
1972 /* Read all pointer fields in the TS_DECL_MINIMAL structure of EXPR
1973    from input block IB.  DATA_IN contains tables and descriptors for the
1974    file being read.  */
1975
1976 static void
1977 lto_input_ts_decl_minimal_tree_pointers (struct lto_input_block *ib,
1978                                          struct data_in *data_in, tree expr)
1979 {
1980   DECL_NAME (expr) = lto_input_tree (ib, data_in);
1981   DECL_CONTEXT (expr) = lto_input_tree (ib, data_in);
1982   DECL_SOURCE_LOCATION (expr) = lto_input_location (ib, data_in);
1983 }
1984
1985
1986 /* Read all pointer fields in the TS_DECL_COMMON structure of EXPR from
1987    input block IB.  DATA_IN contains tables and descriptors for the
1988    file being read.  */
1989
1990 static void
1991 lto_input_ts_decl_common_tree_pointers (struct lto_input_block *ib,
1992                                         struct data_in *data_in, tree expr)
1993 {
1994   DECL_SIZE (expr) = lto_input_tree (ib, data_in);
1995   DECL_SIZE_UNIT (expr) = lto_input_tree (ib, data_in);
1996   DECL_ATTRIBUTES (expr) = lto_input_tree (ib, data_in);
1997
1998   /* Do not stream DECL_ABSTRACT_ORIGIN.  We cannot handle debug information
1999      for early inlining so drop it on the floor instead of ICEing in
2000      dwarf2out.c.  */
2001
2002   if (TREE_CODE (expr) == PARM_DECL)
2003     TREE_CHAIN (expr) = lto_input_chain (ib, data_in);
2004
2005   if ((TREE_CODE (expr) == VAR_DECL
2006        || TREE_CODE (expr) == PARM_DECL)
2007       && DECL_HAS_VALUE_EXPR_P (expr))
2008     SET_DECL_VALUE_EXPR (expr, lto_input_tree (ib, data_in));
2009
2010   if (TREE_CODE (expr) == VAR_DECL)
2011     {
2012       tree dexpr = lto_input_tree (ib, data_in);
2013       if (dexpr)
2014         SET_DECL_DEBUG_EXPR (expr, dexpr);
2015     }
2016 }
2017
2018
2019 /* Read all pointer fields in the TS_DECL_NON_COMMON structure of
2020    EXPR from input block IB.  DATA_IN contains tables and descriptors for the
2021    file being read.  */
2022
2023 static void
2024 lto_input_ts_decl_non_common_tree_pointers (struct lto_input_block *ib,
2025                                             struct data_in *data_in, tree expr)
2026 {
2027   if (TREE_CODE (expr) == FUNCTION_DECL)
2028     {
2029       DECL_ARGUMENTS (expr) = lto_input_tree (ib, data_in);
2030       DECL_RESULT (expr) = lto_input_tree (ib, data_in);
2031     }
2032   DECL_VINDEX (expr) = lto_input_tree (ib, data_in);
2033 }
2034
2035
2036 /* Read all pointer fields in the TS_DECL_WITH_VIS structure of EXPR
2037    from input block IB.  DATA_IN contains tables and descriptors for the
2038    file being read.  */
2039
2040 static void
2041 lto_input_ts_decl_with_vis_tree_pointers (struct lto_input_block *ib,
2042                                           struct data_in *data_in, tree expr)
2043 {
2044   tree id;
2045
2046   id = lto_input_tree (ib, data_in);
2047   if (id)
2048     {
2049       gcc_assert (TREE_CODE (id) == IDENTIFIER_NODE);
2050       SET_DECL_ASSEMBLER_NAME (expr, id);
2051     }
2052
2053   DECL_SECTION_NAME (expr) = lto_input_tree (ib, data_in);
2054   DECL_COMDAT_GROUP (expr) = lto_input_tree (ib, data_in);
2055 }
2056
2057
2058 /* Read all pointer fields in the TS_FIELD_DECL structure of EXPR from
2059    input block IB.  DATA_IN contains tables and descriptors for the
2060    file being read.  */
2061
2062 static void
2063 lto_input_ts_field_decl_tree_pointers (struct lto_input_block *ib,
2064                                        struct data_in *data_in, tree expr)
2065 {
2066   DECL_FIELD_OFFSET (expr) = lto_input_tree (ib, data_in);
2067   DECL_BIT_FIELD_TYPE (expr) = lto_input_tree (ib, data_in);
2068   DECL_QUALIFIER (expr) = lto_input_tree (ib, data_in);
2069   DECL_FIELD_BIT_OFFSET (expr) = lto_input_tree (ib, data_in);
2070   DECL_FCONTEXT (expr) = lto_input_tree (ib, data_in);
2071   TREE_CHAIN (expr) = lto_input_chain (ib, data_in);
2072 }
2073
2074
2075 /* Read all pointer fields in the TS_FUNCTION_DECL structure of EXPR
2076    from input block IB.  DATA_IN contains tables and descriptors for the
2077    file being read.  */
2078
2079 static void
2080 lto_input_ts_function_decl_tree_pointers (struct lto_input_block *ib,
2081                                           struct data_in *data_in, tree expr)
2082 {
2083   /* DECL_STRUCT_FUNCTION is handled by lto_input_function.  FIXME lto,
2084      maybe it should be handled here?  */
2085   DECL_FUNCTION_PERSONALITY (expr) = lto_input_tree (ib, data_in);
2086   DECL_FUNCTION_SPECIFIC_TARGET (expr) = lto_input_tree (ib, data_in);
2087   DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr) = lto_input_tree (ib, data_in);
2088
2089   /* If the file contains a function with an EH personality set,
2090      then it was compiled with -fexceptions.  In that case, initialize
2091      the backend EH machinery.  */
2092   if (DECL_FUNCTION_PERSONALITY (expr))
2093     lto_init_eh ();
2094 }
2095
2096
2097 /* Read all pointer fields in the TS_TYPE_COMMON structure of EXPR from
2098    input block IB.  DATA_IN contains tables and descriptors for the file
2099    being read.  */
2100
2101 static void
2102 lto_input_ts_type_common_tree_pointers (struct lto_input_block *ib,
2103                                         struct data_in *data_in, tree expr)
2104 {
2105   TYPE_SIZE (expr) = lto_input_tree (ib, data_in);
2106   TYPE_SIZE_UNIT (expr) = lto_input_tree (ib, data_in);
2107   TYPE_ATTRIBUTES (expr) = lto_input_tree (ib, data_in);
2108   TYPE_NAME (expr) = lto_input_tree (ib, data_in);
2109   /* Do not stream TYPE_POINTER_TO or TYPE_REFERENCE_TO.  They will be
2110      reconstructed during fixup.  */
2111   /* Do not stream TYPE_NEXT_VARIANT, we reconstruct the variant lists
2112      during fixup.  */
2113   TYPE_MAIN_VARIANT (expr) = lto_input_tree (ib, data_in);
2114   TYPE_CONTEXT (expr) = lto_input_tree (ib, data_in);
2115   /* TYPE_CANONICAL gets re-computed during type merging.  */
2116   TYPE_CANONICAL (expr) = NULL_TREE;
2117   TYPE_STUB_DECL (expr) = lto_input_tree (ib, data_in);
2118 }
2119
2120 /* Read all pointer fields in the TS_TYPE_NON_COMMON structure of EXPR
2121    from input block IB.  DATA_IN contains tables and descriptors for the
2122    file being read.  */
2123
2124 static void
2125 lto_input_ts_type_non_common_tree_pointers (struct lto_input_block *ib,
2126                                             struct data_in *data_in,
2127                                             tree expr)
2128 {
2129   if (TREE_CODE (expr) == ENUMERAL_TYPE)
2130     TYPE_VALUES (expr) = lto_input_tree (ib, data_in);
2131   else if (TREE_CODE (expr) == ARRAY_TYPE)
2132     TYPE_DOMAIN (expr) = lto_input_tree (ib, data_in);
2133   else if (RECORD_OR_UNION_TYPE_P (expr))
2134     TYPE_FIELDS (expr) = lto_input_tree (ib, data_in);
2135   else if (TREE_CODE (expr) == FUNCTION_TYPE
2136            || TREE_CODE (expr) == METHOD_TYPE)
2137     TYPE_ARG_TYPES (expr) = lto_input_tree (ib, data_in);
2138
2139   if (!POINTER_TYPE_P (expr))
2140     TYPE_MINVAL (expr) = lto_input_tree (ib, data_in);
2141   TYPE_MAXVAL (expr) = lto_input_tree (ib, data_in);
2142   if (RECORD_OR_UNION_TYPE_P (expr))
2143     TYPE_BINFO (expr) = lto_input_tree (ib, data_in);
2144 }
2145
2146
2147 /* Read all pointer fields in the TS_LIST structure of EXPR from input
2148    block IB.  DATA_IN contains tables and descriptors for the
2149    file being read.  */
2150
2151 static void
2152 lto_input_ts_list_tree_pointers (struct lto_input_block *ib,
2153                                  struct data_in *data_in, tree expr)
2154 {
2155   TREE_PURPOSE (expr) = lto_input_tree (ib, data_in);
2156   TREE_VALUE (expr) = lto_input_tree (ib, data_in);
2157   TREE_CHAIN (expr) = lto_input_chain (ib, data_in);
2158 }
2159
2160
2161 /* Read all pointer fields in the TS_VEC structure of EXPR from input
2162    block IB.  DATA_IN contains tables and descriptors for the
2163    file being read.  */
2164
2165 static void
2166 lto_input_ts_vec_tree_pointers (struct lto_input_block *ib,
2167                                 struct data_in *data_in, tree expr)
2168 {
2169   int i;
2170
2171   /* Note that TREE_VEC_LENGTH was read by lto_materialize_tree to
2172      instantiate EXPR.  */
2173   for (i = 0; i < TREE_VEC_LENGTH (expr); i++)
2174     TREE_VEC_ELT (expr, i) = lto_input_tree (ib, data_in);
2175 }
2176
2177
2178 /* Read all pointer fields in the TS_EXP structure of EXPR from input
2179    block IB.  DATA_IN contains tables and descriptors for the
2180    file being read.  */
2181
2182
2183 static void
2184 lto_input_ts_exp_tree_pointers (struct lto_input_block *ib,
2185                                 struct data_in *data_in, tree expr)
2186 {
2187   int i, length;
2188   location_t loc;
2189
2190   length = lto_input_sleb128 (ib);
2191   gcc_assert (length == TREE_OPERAND_LENGTH (expr));
2192
2193   for (i = 0; i < length; i++)
2194     TREE_OPERAND (expr, i) = lto_input_tree (ib, data_in);
2195
2196   loc = lto_input_location (ib, data_in);
2197   SET_EXPR_LOCATION (expr, loc);
2198   TREE_BLOCK (expr) = lto_input_tree (ib, data_in);
2199 }
2200
2201
2202 /* Read all pointer fields in the TS_BLOCK structure of EXPR from input
2203    block IB.  DATA_IN contains tables and descriptors for the
2204    file being read.  */
2205
2206 static void
2207 lto_input_ts_block_tree_pointers (struct lto_input_block *ib,
2208                                   struct data_in *data_in, tree expr)
2209 {
2210   /* Do not stream BLOCK_SOURCE_LOCATION.  We cannot handle debug information
2211      for early inlining so drop it on the floor instead of ICEing in
2212      dwarf2out.c.  */
2213   BLOCK_VARS (expr) = lto_input_chain (ib, data_in);
2214
2215   /* Do not stream BLOCK_NONLOCALIZED_VARS.  We cannot handle debug information
2216      for early inlining so drop it on the floor instead of ICEing in
2217      dwarf2out.c.  */
2218
2219   BLOCK_SUPERCONTEXT (expr) = lto_input_tree (ib, data_in);
2220   /* Do not stream BLOCK_ABSTRACT_ORIGIN.  We cannot handle debug information
2221      for early inlining so drop it on the floor instead of ICEing in
2222      dwarf2out.c.  */
2223   BLOCK_FRAGMENT_ORIGIN (expr) = lto_input_tree (ib, data_in);
2224   BLOCK_FRAGMENT_CHAIN (expr) = lto_input_tree (ib, data_in);
2225   /* We re-compute BLOCK_SUBBLOCKS of our parent here instead
2226      of streaming it.  For non-BLOCK BLOCK_SUPERCONTEXTs we still
2227      stream the child relationship explicitly.  */
2228   if (BLOCK_SUPERCONTEXT (expr)
2229       && TREE_CODE (BLOCK_SUPERCONTEXT (expr)) == BLOCK)
2230     {
2231       BLOCK_CHAIN (expr) = BLOCK_SUBBLOCKS (BLOCK_SUPERCONTEXT (expr));
2232       BLOCK_SUBBLOCKS (BLOCK_SUPERCONTEXT (expr)) = expr;
2233     }
2234   /* The global block is rooted at the TU decl.  Hook it here to
2235      avoid the need to stream in this block during WPA time.  */
2236   else if (BLOCK_SUPERCONTEXT (expr)
2237            && TREE_CODE (BLOCK_SUPERCONTEXT (expr)) == TRANSLATION_UNIT_DECL)
2238     DECL_INITIAL (BLOCK_SUPERCONTEXT (expr)) = expr;
2239   /* The function-level block is connected at the time we read in
2240      function bodies for the same reason.  */
2241 }
2242
2243
2244 /* Read all pointer fields in the TS_BINFO structure of EXPR from input
2245    block IB.  DATA_IN contains tables and descriptors for the
2246    file being read.  */
2247
2248 static void
2249 lto_input_ts_binfo_tree_pointers (struct lto_input_block *ib,
2250                                   struct data_in *data_in, tree expr)
2251 {
2252   unsigned i, len;
2253   tree t;
2254
2255   /* Note that the number of slots in EXPR was read in
2256      lto_materialize_tree when instantiating EXPR.  However, the
2257      vector is empty so we cannot rely on VEC_length to know how many
2258      elements to read.  So, this list is emitted as a 0-terminated
2259      list on the writer side.  */
2260   do
2261     {
2262       t = lto_input_tree (ib, data_in);
2263       if (t)
2264         VEC_quick_push (tree, BINFO_BASE_BINFOS (expr), t);
2265     }
2266   while (t);
2267
2268   BINFO_OFFSET (expr) = lto_input_tree (ib, data_in);
2269   BINFO_VTABLE (expr) = lto_input_tree (ib, data_in);
2270   BINFO_VIRTUALS (expr) = lto_input_tree (ib, data_in);
2271   BINFO_VPTR_FIELD (expr) = lto_input_tree (ib, data_in);
2272
2273   len = lto_input_uleb128 (ib);
2274   if (len > 0)
2275     {
2276       VEC_reserve_exact (tree, gc, BINFO_BASE_ACCESSES (expr), len);
2277       for (i = 0; i < len; i++)
2278         {
2279           tree a = lto_input_tree (ib, data_in);
2280           VEC_quick_push (tree, BINFO_BASE_ACCESSES (expr), a);
2281         }
2282     }
2283
2284   BINFO_INHERITANCE_CHAIN (expr) = lto_input_tree (ib, data_in);
2285   BINFO_SUBVTT_INDEX (expr) = lto_input_tree (ib, data_in);
2286   BINFO_VPTR_INDEX (expr) = lto_input_tree (ib, data_in);
2287 }
2288
2289
2290 /* Read all pointer fields in the TS_CONSTRUCTOR structure of EXPR from
2291    input block IB.  DATA_IN contains tables and descriptors for the
2292    file being read.  */
2293
2294 static void
2295 lto_input_ts_constructor_tree_pointers (struct lto_input_block *ib,
2296                                         struct data_in *data_in, tree expr)
2297 {
2298   unsigned i, len;
2299
2300   len = lto_input_uleb128 (ib);
2301   for (i = 0; i < len; i++)
2302     {
2303       tree index, value;
2304
2305       index = lto_input_tree (ib, data_in);
2306       value = lto_input_tree (ib, data_in);
2307       CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (expr), index, value);
2308     }
2309 }
2310
2311
2312 /* Input a TS_TARGET_OPTION tree from IB into EXPR.  */
2313
2314 static void
2315 lto_input_ts_target_option (struct lto_input_block *ib, tree expr)
2316 {
2317   unsigned i, len;
2318   struct bitpack_d bp;
2319   struct cl_target_option *t = TREE_TARGET_OPTION (expr);
2320
2321   bp = lto_input_bitpack (ib);
2322   len = sizeof (struct cl_target_option);
2323   for (i = 0; i < len; i++)
2324     ((unsigned char *)t)[i] = bp_unpack_value (&bp, 8);
2325   if (bp_unpack_value (&bp, 32) != 0x12345678)
2326     fatal_error ("cl_target_option size mismatch in LTO reader and writer");
2327 }
2328
2329 /* Input a TS_TRANSLATION_UNIT_DECL tree from IB and DATA_IN into EXPR.  */
2330
2331 static void
2332 lto_input_ts_translation_unit_decl_tree_pointers (struct lto_input_block *ib,
2333                                                   struct data_in *data_in,
2334                                                   tree expr)
2335 {
2336   TRANSLATION_UNIT_LANGUAGE (expr) = xstrdup (lto_input_string (data_in, ib));
2337   VEC_safe_push (tree, gc, all_translation_units, expr);
2338 }
2339
2340 /* Helper for lto_input_tree.  Read all pointer fields in EXPR from
2341    input block IB.  DATA_IN contains tables and descriptors for the
2342    file being read.  */
2343
2344 static void
2345 lto_input_tree_pointers (struct lto_input_block *ib, struct data_in *data_in,
2346                          tree expr)
2347 {
2348   enum tree_code code;
2349
2350   code = TREE_CODE (expr);
2351
2352   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
2353     lto_input_ts_common_tree_pointers (ib, data_in, expr);
2354
2355   if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
2356     lto_input_ts_vector_tree_pointers (ib, data_in, expr);
2357
2358   if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
2359     lto_input_ts_complex_tree_pointers (ib, data_in, expr);
2360
2361   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
2362     lto_input_ts_decl_minimal_tree_pointers (ib, data_in, expr);
2363
2364   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2365     lto_input_ts_decl_common_tree_pointers (ib, data_in, expr);
2366
2367   if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2368     lto_input_ts_decl_non_common_tree_pointers (ib, data_in, expr);
2369
2370   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2371     lto_input_ts_decl_with_vis_tree_pointers (ib, data_in, expr);
2372
2373   if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2374     lto_input_ts_field_decl_tree_pointers (ib, data_in, expr);
2375
2376   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2377     lto_input_ts_function_decl_tree_pointers (ib, data_in, expr);
2378
2379   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
2380     lto_input_ts_type_common_tree_pointers (ib, data_in, expr);
2381
2382   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
2383     lto_input_ts_type_non_common_tree_pointers (ib, data_in, expr);
2384
2385   if (CODE_CONTAINS_STRUCT (code, TS_LIST))
2386     lto_input_ts_list_tree_pointers (ib, data_in, expr);
2387
2388   if (CODE_CONTAINS_STRUCT (code, TS_VEC))
2389     lto_input_ts_vec_tree_pointers (ib, data_in, expr);
2390
2391   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
2392     lto_input_ts_exp_tree_pointers (ib, data_in, expr);
2393
2394   if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
2395     lto_input_ts_block_tree_pointers (ib, data_in, expr);
2396
2397   if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
2398     lto_input_ts_binfo_tree_pointers (ib, data_in, expr);
2399
2400   if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
2401     lto_input_ts_constructor_tree_pointers (ib, data_in, expr);
2402
2403   if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
2404     lto_input_ts_target_option (ib, expr);
2405
2406   if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
2407     lto_input_ts_translation_unit_decl_tree_pointers (ib, data_in, expr);
2408 }
2409
2410
2411 /* Read an index IX from input block IB and return the tree node at
2412    DATA_IN->FILE_DATA->GLOBALS_INDEX[IX].  */
2413
2414 static tree
2415 lto_get_pickled_tree (struct lto_input_block *ib, struct data_in *data_in)
2416 {
2417   unsigned HOST_WIDE_INT ix;
2418   tree result;
2419   enum LTO_tags expected_tag;
2420
2421   ix = lto_input_uleb128 (ib);
2422   expected_tag = lto_input_enum (ib, LTO_tags, LTO_NUM_TAGS);
2423
2424   result = lto_streamer_cache_get (data_in->reader_cache, ix);
2425   gcc_assert (result
2426               && TREE_CODE (result) == lto_tag_to_tree_code (expected_tag));
2427
2428   return result;
2429 }
2430
2431
2432 /* Read a code and class from input block IB and return the
2433    corresponding builtin.  DATA_IN is as in lto_input_tree.  */
2434
2435 static tree
2436 lto_get_builtin_tree (struct lto_input_block *ib, struct data_in *data_in)
2437 {
2438   enum built_in_class fclass;
2439   enum built_in_function fcode;
2440   const char *asmname;
2441   tree result;
2442
2443   fclass = lto_input_enum (ib, built_in_class, BUILT_IN_LAST);
2444   gcc_assert (fclass == BUILT_IN_NORMAL || fclass == BUILT_IN_MD);
2445
2446   fcode = (enum built_in_function) lto_input_uleb128 (ib);
2447
2448   if (fclass == BUILT_IN_NORMAL)
2449     {
2450       if (fcode >= END_BUILTINS)
2451         fatal_error ("machine independent builtin code out of range");
2452       result = built_in_decls[fcode];
2453       gcc_assert (result);
2454     }
2455   else if (fclass == BUILT_IN_MD)
2456     {
2457       result = targetm.builtin_decl (fcode, true);
2458       if (!result || result == error_mark_node)
2459         fatal_error ("target specific builtin not available");
2460     }
2461   else
2462     gcc_unreachable ();
2463
2464   asmname = lto_input_string (data_in, ib);
2465   if (asmname)
2466     set_builtin_user_assembler_name (result, asmname);
2467
2468   lto_streamer_cache_append (data_in->reader_cache, result);
2469
2470   return result;
2471 }
2472
2473
2474 /* Read the physical representation of a tree node with tag TAG from
2475    input block IB using the per-file context in DATA_IN.  */
2476
2477 static tree
2478 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
2479                enum LTO_tags tag)
2480 {
2481   tree result;
2482
2483   result = lto_materialize_tree (ib, data_in, tag);
2484
2485   /* Read all the pointer fields in RESULT.  */
2486   lto_input_tree_pointers (ib, data_in, result);
2487
2488   /* Call back into the streaming module to read anything else it
2489      may need.  */
2490   if (streamer_hooks.read_tree)
2491     streamer_hooks.read_tree (ib, data_in, result);
2492
2493   /* We should never try to instantiate an MD or NORMAL builtin here.  */
2494   if (TREE_CODE (result) == FUNCTION_DECL)
2495     gcc_assert (!lto_stream_as_builtin_p (result));
2496
2497   /* end_marker = */ lto_input_1_unsigned (ib);
2498
2499 #ifdef LTO_STREAMER_DEBUG
2500   /* Remove the mapping to RESULT's original address set by
2501      lto_materialize_tree.  */
2502   lto_orig_address_remove (result);
2503 #endif
2504
2505   return result;
2506 }
2507
2508
2509 /* LTO streamer hook for reading GIMPLE trees.  IB and DATA_IN are as in
2510    lto_read_tree.  EXPR is the tree was materialized by lto_read_tree and
2511    needs GIMPLE specific data to be filled in.  */
2512
2513 void
2514 lto_streamer_read_tree (struct lto_input_block *ib, struct data_in *data_in,
2515                         tree expr)
2516 {
2517   if (DECL_P (expr)
2518       && TREE_CODE (expr) != FUNCTION_DECL
2519       && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
2520     DECL_INITIAL (expr) = lto_input_tree (ib, data_in);
2521 }
2522
2523
2524 /* Read and INTEGER_CST node from input block IB using the per-file
2525    context in DATA_IN.  */
2526
2527 static tree
2528 lto_input_integer_cst (struct lto_input_block *ib, struct data_in *data_in)
2529 {
2530   tree result, type;
2531   HOST_WIDE_INT low, high;
2532   bool overflow_p;
2533
2534   type = lto_input_tree (ib, data_in);
2535   overflow_p = (lto_input_1_unsigned (ib) != 0);
2536   low = lto_input_uleb128 (ib);
2537   high = lto_input_uleb128 (ib);
2538   result = build_int_cst_wide (type, low, high);
2539
2540   /* If the original constant had overflown, build a replica of RESULT to
2541      avoid modifying the shared constant returned by build_int_cst_wide.  */
2542   if (overflow_p)
2543     {
2544       result = copy_node (result);
2545       TREE_OVERFLOW (result) = 1;
2546     }
2547
2548   return result;
2549 }
2550
2551
2552 /* Read a tree from input block IB using the per-file context in
2553    DATA_IN.  This context is used, for example, to resolve references
2554    to previously read nodes.  */
2555
2556 tree
2557 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
2558 {
2559   enum LTO_tags tag;
2560   tree result;
2561
2562   tag = input_record_start (ib);
2563   gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
2564
2565   if (tag == LTO_null)
2566     result = NULL_TREE;
2567   else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
2568     {
2569       /* If TAG is a reference to an indexable tree, the next value
2570          in IB is the index into the table where we expect to find
2571          that tree.  */
2572       result = lto_input_tree_ref (ib, data_in, cfun, tag);
2573     }
2574   else if (tag == LTO_tree_pickle_reference)
2575     {
2576       /* If TAG is a reference to a previously read tree, look it up in
2577          the reader cache.  */
2578       result = lto_get_pickled_tree (ib, data_in);
2579     }
2580   else if (tag == LTO_builtin_decl)
2581     {
2582       /* If we are going to read a built-in function, all we need is
2583          the code and class.  */
2584       result = lto_get_builtin_tree (ib, data_in);
2585     }
2586   else if (tag == lto_tree_code_to_tag (INTEGER_CST))
2587     {
2588       /* For integer constants we only need the type and its hi/low
2589          words.  */
2590       result = lto_input_integer_cst (ib, data_in);
2591     }
2592   else
2593     {
2594       /* Otherwise, materialize a new node from IB.  */
2595       result = lto_read_tree (ib, data_in, tag);
2596     }
2597
2598   return result;
2599 }
2600
2601
2602 /* Initialization for the LTO reader.  */
2603
2604 void
2605 lto_reader_init (void)
2606 {
2607   lto_streamer_init ();
2608   file_name_hash_table = htab_create (37, hash_string_slot_node,
2609                                       eq_string_slot_node, free);
2610 }
2611
2612
2613 /* Create a new data_in object for FILE_DATA. STRINGS is the string
2614    table to use with LEN strings.  RESOLUTIONS is the vector of linker
2615    resolutions (NULL if not using a linker plugin).  */
2616
2617 struct data_in *
2618 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
2619                     unsigned len,
2620                     VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
2621 {
2622   struct data_in *data_in = XCNEW (struct data_in);
2623   data_in->file_data = file_data;
2624   data_in->strings = strings;
2625   data_in->strings_len = len;
2626   data_in->globals_resolution = resolutions;
2627   data_in->reader_cache = lto_streamer_cache_create ();
2628
2629   return data_in;
2630 }
2631
2632
2633 /* Remove DATA_IN.  */
2634
2635 void
2636 lto_data_in_delete (struct data_in *data_in)
2637 {
2638   VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
2639   lto_streamer_cache_delete (data_in->reader_cache);
2640   free (data_in->labels);
2641   free (data_in);
2642 }