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