OSDN Git Service

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