OSDN Git Service

PR rtl-optimization/50249
[pf3gnuchains/gcc-fork.git] / gcc / lto-streamer-in.c
1 /* Read the GIMPLE representation from a file stream.
2
3    Copyright 2009, 2010 Free Software Foundation, Inc.
4    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5    Re-implemented by Diego Novillo <dnovillo@google.com>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "tree-pass.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic.h"
41 #include "libfuncs.h"
42 #include "except.h"
43 #include "debug.h"
44 #include "vec.h"
45 #include "timevar.h"
46 #include "output.h"
47 #include "ipa-utils.h"
48 #include "data-streamer.h"
49 #include "gimple-streamer.h"
50 #include "lto-streamer.h"
51 #include "tree-streamer.h"
52 #include "tree-pass.h"
53
54 /* The table to hold the file names.  */
55 static htab_t file_name_hash_table;
56
57
58 /* Check that tag ACTUAL has one of the given values.  NUM_TAGS is the
59    number of valid tag values to check.  */
60
61 void
62 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
63 {
64   va_list ap;
65   int i;
66
67   va_start (ap, ntags);
68   for (i = 0; i < ntags; i++)
69     if ((unsigned) actual == va_arg (ap, unsigned))
70       {
71         va_end (ap);
72         return;
73       }
74
75   va_end (ap);
76   internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
77 }
78
79
80 /* Read LENGTH bytes from STREAM to ADDR.  */
81
82 void
83 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
84 {
85   size_t i;
86   unsigned char *const buffer = (unsigned char *const) addr;
87
88   for (i = 0; i < length; i++)
89     buffer[i] = streamer_read_uchar (ib);
90 }
91
92
93 /* Lookup STRING in file_name_hash_table.  If found, return the existing
94    string, otherwise insert STRING as the canonical version.  */
95
96 static const char *
97 canon_file_name (const char *string)
98 {
99   void **slot;
100   struct string_slot s_slot;
101   size_t len = strlen (string);
102
103   s_slot.s = string;
104   s_slot.len = len;
105
106   slot = htab_find_slot (file_name_hash_table, &s_slot, INSERT);
107   if (*slot == NULL)
108     {
109       char *saved_string;
110       struct string_slot *new_slot;
111
112       saved_string = (char *) xmalloc (len + 1);
113       new_slot = XCNEW (struct string_slot);
114       memcpy (saved_string, string, len + 1);
115       new_slot->s = saved_string;
116       new_slot->len = len;
117       *slot = new_slot;
118       return saved_string;
119     }
120   else
121     {
122       struct string_slot *old_slot = (struct string_slot *) *slot;
123       return old_slot->s;
124     }
125 }
126
127
128 /* Clear the line info stored in DATA_IN.  */
129
130 static void
131 clear_line_info (struct data_in *data_in)
132 {
133   if (data_in->current_file)
134     linemap_add (line_table, LC_LEAVE, false, NULL, 0);
135   data_in->current_file = NULL;
136   data_in->current_line = 0;
137   data_in->current_col = 0;
138 }
139
140
141 /* Read a location bitpack from input block IB.  */
142
143 static location_t
144 lto_input_location_bitpack (struct data_in *data_in, struct bitpack_d *bp)
145 {
146   bool file_change, line_change, column_change;
147   unsigned len;
148   bool prev_file = data_in->current_file != NULL;
149
150   if (bp_unpack_value (bp, 1))
151     return UNKNOWN_LOCATION;
152
153   file_change = bp_unpack_value (bp, 1);
154   if (file_change)
155     data_in->current_file = canon_file_name
156                               (string_for_index (data_in,
157                                                  bp_unpack_var_len_unsigned (bp),
158                                                  &len));
159
160   line_change = bp_unpack_value (bp, 1);
161   if (line_change)
162     data_in->current_line = bp_unpack_var_len_unsigned (bp);
163
164   column_change = bp_unpack_value (bp, 1);
165   if (column_change)
166     data_in->current_col = bp_unpack_var_len_unsigned (bp);
167
168   if (file_change)
169     {
170       if (prev_file)
171         linemap_add (line_table, LC_LEAVE, false, NULL, 0);
172
173       linemap_add (line_table, LC_ENTER, false, data_in->current_file,
174                    data_in->current_line);
175     }
176   else if (line_change)
177     linemap_line_start (line_table, data_in->current_line, data_in->current_col);
178
179   return linemap_position_for_column (line_table, data_in->current_col);
180 }
181
182
183 /* Read a location from input block IB.  */
184
185 location_t
186 lto_input_location (struct lto_input_block *ib, struct data_in *data_in)
187 {
188   struct bitpack_d bp;
189
190   bp = streamer_read_bitpack (ib);
191   return lto_input_location_bitpack (data_in, &bp);
192 }
193
194
195 /* Read a reference to a tree node from DATA_IN using input block IB.
196    TAG is the expected node that should be found in IB, if TAG belongs
197    to one of the indexable trees, expect to read a reference index to
198    be looked up in one of the symbol tables, otherwise read the pysical
199    representation of the tree using stream_read_tree.  FN is the
200    function scope for the read tree.  */
201
202 tree
203 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
204                     struct function *fn, enum LTO_tags tag)
205 {
206   unsigned HOST_WIDE_INT ix_u;
207   tree result = NULL_TREE;
208
209   lto_tag_check_range (tag, LTO_field_decl_ref, LTO_global_decl_ref);
210
211   switch (tag)
212     {
213     case LTO_type_ref:
214       ix_u = streamer_read_uhwi (ib);
215       result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
216       break;
217
218     case LTO_ssa_name_ref:
219       ix_u = streamer_read_uhwi (ib);
220       result = VEC_index (tree, SSANAMES (fn), ix_u);
221       break;
222
223     case LTO_field_decl_ref:
224       ix_u = streamer_read_uhwi (ib);
225       result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
226       break;
227
228     case LTO_function_decl_ref:
229       ix_u = streamer_read_uhwi (ib);
230       result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
231       break;
232
233     case LTO_type_decl_ref:
234       ix_u = streamer_read_uhwi (ib);
235       result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
236       break;
237
238     case LTO_namespace_decl_ref:
239       ix_u = streamer_read_uhwi (ib);
240       result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
241       break;
242
243     case LTO_global_decl_ref:
244     case LTO_result_decl_ref:
245     case LTO_const_decl_ref:
246     case LTO_imported_decl_ref:
247     case LTO_label_decl_ref:
248     case LTO_translation_unit_decl_ref:
249       ix_u = streamer_read_uhwi (ib);
250       result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
251       break;
252
253     default:
254       gcc_unreachable ();
255     }
256
257   gcc_assert (result);
258
259   return result;
260 }
261
262
263 /* Read and return a double-linked list of catch handlers from input
264    block IB, using descriptors in DATA_IN.  */
265
266 static struct eh_catch_d *
267 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
268                          eh_catch *last_p)
269 {
270   eh_catch first;
271   enum LTO_tags tag;
272
273   *last_p = first = NULL;
274   tag = streamer_read_record_start (ib);
275   while (tag)
276     {
277       tree list;
278       eh_catch n;
279
280       lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
281
282       /* Read the catch node.  */
283       n = ggc_alloc_cleared_eh_catch_d ();
284       n->type_list = stream_read_tree (ib, data_in);
285       n->filter_list = stream_read_tree (ib, data_in);
286       n->label = stream_read_tree (ib, data_in);
287
288       /* Register all the types in N->FILTER_LIST.  */
289       for (list = n->filter_list; list; list = TREE_CHAIN (list))
290         add_type_for_runtime (TREE_VALUE (list));
291
292       /* Chain N to the end of the list.  */
293       if (*last_p)
294         (*last_p)->next_catch = n;
295       n->prev_catch = *last_p;
296       *last_p = n;
297
298       /* Set the head of the list the first time through the loop.  */
299       if (first == NULL)
300         first = n;
301
302       tag = streamer_read_record_start (ib);
303     }
304
305   return first;
306 }
307
308
309 /* Read and return EH region IX from input block IB, using descriptors
310    in DATA_IN.  */
311
312 static eh_region
313 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
314 {
315   enum LTO_tags tag;
316   eh_region r;
317
318   /* Read the region header.  */
319   tag = streamer_read_record_start (ib);
320   if (tag == LTO_null)
321     return NULL;
322
323   r = ggc_alloc_cleared_eh_region_d ();
324   r->index = streamer_read_hwi (ib);
325
326   gcc_assert (r->index == ix);
327
328   /* Read all the region pointers as region numbers.  We'll fix up
329      the pointers once the whole array has been read.  */
330   r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
331   r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
332   r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
333
334   switch (tag)
335     {
336       case LTO_ert_cleanup:
337         r->type = ERT_CLEANUP;
338         break;
339
340       case LTO_ert_try:
341         {
342           struct eh_catch_d *last_catch;
343           r->type = ERT_TRY;
344           r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
345                                                              &last_catch);
346           r->u.eh_try.last_catch = last_catch;
347           break;
348         }
349
350       case LTO_ert_allowed_exceptions:
351         {
352           tree l;
353
354           r->type = ERT_ALLOWED_EXCEPTIONS;
355           r->u.allowed.type_list = stream_read_tree (ib, data_in);
356           r->u.allowed.label = stream_read_tree (ib, data_in);
357           r->u.allowed.filter = streamer_read_uhwi (ib);
358
359           for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
360             add_type_for_runtime (TREE_VALUE (l));
361         }
362         break;
363
364       case LTO_ert_must_not_throw:
365         r->type = ERT_MUST_NOT_THROW;
366         r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
367         r->u.must_not_throw.failure_loc = lto_input_location (ib, data_in);
368         break;
369
370       default:
371         gcc_unreachable ();
372     }
373
374   r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
375
376   return r;
377 }
378
379
380 /* Read and return EH landing pad IX from input block IB, using descriptors
381    in DATA_IN.  */
382
383 static eh_landing_pad
384 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
385 {
386   enum LTO_tags tag;
387   eh_landing_pad lp;
388
389   /* Read the landing pad header.  */
390   tag = streamer_read_record_start (ib);
391   if (tag == LTO_null)
392     return NULL;
393
394   lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
395
396   lp = ggc_alloc_cleared_eh_landing_pad_d ();
397   lp->index = streamer_read_hwi (ib);
398   gcc_assert (lp->index == ix);
399   lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
400   lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
401   lp->post_landing_pad = stream_read_tree (ib, data_in);
402
403   return lp;
404 }
405
406
407 /* After reading the EH regions, pointers to peer and children regions
408    are region numbers.  This converts all these region numbers into
409    real pointers into the rematerialized regions for FN.  ROOT_REGION
410    is the region number for the root EH region in FN.  */
411
412 static void
413 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
414 {
415   unsigned i;
416   VEC(eh_region,gc) *eh_array = fn->eh->region_array;
417   VEC(eh_landing_pad,gc) *lp_array = fn->eh->lp_array;
418   eh_region r;
419   eh_landing_pad lp;
420
421   gcc_assert (eh_array && lp_array);
422
423   gcc_assert (root_region >= 0);
424   fn->eh->region_tree = VEC_index (eh_region, eh_array, root_region);
425
426 #define FIXUP_EH_REGION(r) (r) = VEC_index (eh_region, eh_array, \
427                                             (HOST_WIDE_INT) (intptr_t) (r))
428 #define FIXUP_EH_LP(p) (p) = VEC_index (eh_landing_pad, lp_array, \
429                                         (HOST_WIDE_INT) (intptr_t) (p))
430
431   /* Convert all the index numbers stored in pointer fields into
432      pointers to the corresponding slots in the EH region array.  */
433   FOR_EACH_VEC_ELT (eh_region, eh_array, i, r)
434     {
435       /* The array may contain NULL regions.  */
436       if (r == NULL)
437         continue;
438
439       gcc_assert (i == (unsigned) r->index);
440       FIXUP_EH_REGION (r->outer);
441       FIXUP_EH_REGION (r->inner);
442       FIXUP_EH_REGION (r->next_peer);
443       FIXUP_EH_LP (r->landing_pads);
444     }
445
446   /* Convert all the index numbers stored in pointer fields into
447      pointers to the corresponding slots in the EH landing pad array.  */
448   FOR_EACH_VEC_ELT (eh_landing_pad, lp_array, i, lp)
449     {
450       /* The array may contain NULL landing pads.  */
451       if (lp == NULL)
452         continue;
453
454       gcc_assert (i == (unsigned) lp->index);
455       FIXUP_EH_LP (lp->next_lp);
456       FIXUP_EH_REGION (lp->region);
457     }
458
459 #undef FIXUP_EH_REGION
460 #undef FIXUP_EH_LP
461 }
462
463
464 /* Initialize EH support.  */
465
466 void
467 lto_init_eh (void)
468 {
469   static bool eh_initialized_p = false;
470
471   if (eh_initialized_p)
472     return;
473
474   /* Contrary to most other FEs, we only initialize EH support when at
475      least one of the files in the set contains exception regions in
476      it.  Since this happens much later than the call to init_eh in
477      lang_dependent_init, we have to set flag_exceptions and call
478      init_eh again to initialize the EH tables.  */
479   flag_exceptions = 1;
480   init_eh ();
481
482   eh_initialized_p = true;
483 }
484
485
486 /* Read the exception table for FN from IB using the data descriptors
487    in DATA_IN.  */
488
489 static void
490 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
491                   struct function *fn)
492 {
493   HOST_WIDE_INT i, root_region, len;
494   enum LTO_tags tag;
495
496   tag = streamer_read_record_start (ib);
497   if (tag == LTO_null)
498     return;
499
500   lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
501
502   /* If the file contains EH regions, then it was compiled with
503      -fexceptions.  In that case, initialize the backend EH
504      machinery.  */
505   lto_init_eh ();
506
507   gcc_assert (fn->eh);
508
509   root_region = streamer_read_hwi (ib);
510   gcc_assert (root_region == (int) root_region);
511
512   /* Read the EH region array.  */
513   len = streamer_read_hwi (ib);
514   gcc_assert (len == (int) len);
515   if (len > 0)
516     {
517       VEC_safe_grow (eh_region, gc, fn->eh->region_array, len);
518       for (i = 0; i < len; i++)
519         {
520           eh_region r = input_eh_region (ib, data_in, i);
521           VEC_replace (eh_region, fn->eh->region_array, i, r);
522         }
523     }
524
525   /* Read the landing pads.  */
526   len = streamer_read_hwi (ib);
527   gcc_assert (len == (int) len);
528   if (len > 0)
529     {
530       VEC_safe_grow (eh_landing_pad, gc, fn->eh->lp_array, len);
531       for (i = 0; i < len; i++)
532         {
533           eh_landing_pad lp = input_eh_lp (ib, data_in, i);
534           VEC_replace (eh_landing_pad, fn->eh->lp_array, i, lp);
535         }
536     }
537
538   /* Read the runtime type data.  */
539   len = streamer_read_hwi (ib);
540   gcc_assert (len == (int) len);
541   if (len > 0)
542     {
543       VEC_safe_grow (tree, gc, fn->eh->ttype_data, len);
544       for (i = 0; i < len; i++)
545         {
546           tree ttype = stream_read_tree (ib, data_in);
547           VEC_replace (tree, fn->eh->ttype_data, i, ttype);
548         }
549     }
550
551   /* Read the table of action chains.  */
552   len = streamer_read_hwi (ib);
553   gcc_assert (len == (int) len);
554   if (len > 0)
555     {
556       if (targetm.arm_eabi_unwinder)
557         {
558           VEC_safe_grow (tree, gc, fn->eh->ehspec_data.arm_eabi, len);
559           for (i = 0; i < len; i++)
560             {
561               tree t = stream_read_tree (ib, data_in);
562               VEC_replace (tree, fn->eh->ehspec_data.arm_eabi, i, t);
563             }
564         }
565       else
566         {
567           VEC_safe_grow (uchar, gc, fn->eh->ehspec_data.other, len);
568           for (i = 0; i < len; i++)
569             {
570               uchar c = streamer_read_uchar (ib);
571               VEC_replace (uchar, fn->eh->ehspec_data.other, i, c);
572             }
573         }
574     }
575
576   /* Reconstruct the EH region tree by fixing up the peer/children
577      pointers.  */
578   fixup_eh_region_pointers (fn, root_region);
579
580   tag = streamer_read_record_start (ib);
581   lto_tag_check_range (tag, LTO_null, LTO_null);
582 }
583
584
585 /* Make a new basic block with index INDEX in function FN.  */
586
587 static basic_block
588 make_new_block (struct function *fn, unsigned int index)
589 {
590   basic_block bb = alloc_block ();
591   bb->index = index;
592   SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
593   bb->il.gimple = ggc_alloc_cleared_gimple_bb_info ();
594   n_basic_blocks_for_function (fn)++;
595   bb->flags = 0;
596   set_bb_seq (bb, gimple_seq_alloc ());
597   return bb;
598 }
599
600
601 /* Read the CFG for function FN from input block IB.  */
602
603 static void
604 input_cfg (struct lto_input_block *ib, struct function *fn,
605            int count_materialization_scale)
606 {
607   unsigned int bb_count;
608   basic_block p_bb;
609   unsigned int i;
610   int index;
611
612   init_empty_tree_cfg_for_function (fn);
613   init_ssa_operands ();
614
615   profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
616                                                          PROFILE_LAST);
617
618   bb_count = streamer_read_uhwi (ib);
619
620   last_basic_block_for_function (fn) = bb_count;
621   if (bb_count > VEC_length (basic_block, basic_block_info_for_function (fn)))
622     VEC_safe_grow_cleared (basic_block, gc,
623                            basic_block_info_for_function (fn), bb_count);
624
625   if (bb_count > VEC_length (basic_block, label_to_block_map_for_function (fn)))
626     VEC_safe_grow_cleared (basic_block, gc,
627                            label_to_block_map_for_function (fn), bb_count);
628
629   index = streamer_read_hwi (ib);
630   while (index != -1)
631     {
632       basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
633       unsigned int edge_count;
634
635       if (bb == NULL)
636         bb = make_new_block (fn, index);
637
638       edge_count = streamer_read_uhwi (ib);
639
640       /* Connect up the CFG.  */
641       for (i = 0; i < edge_count; i++)
642         {
643           unsigned int dest_index;
644           unsigned int edge_flags;
645           basic_block dest;
646           int probability;
647           gcov_type count;
648           edge e;
649
650           dest_index = streamer_read_uhwi (ib);
651           probability = (int) streamer_read_hwi (ib);
652           count = ((gcov_type) streamer_read_hwi (ib) * count_materialization_scale
653                    + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
654           edge_flags = streamer_read_uhwi (ib);
655
656           dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
657
658           if (dest == NULL)
659             dest = make_new_block (fn, dest_index);
660
661           e = make_edge (bb, dest, edge_flags);
662           e->probability = probability;
663           e->count = count;
664         }
665
666       index = streamer_read_hwi (ib);
667     }
668
669   p_bb = ENTRY_BLOCK_PTR_FOR_FUNCTION(fn);
670   index = streamer_read_hwi (ib);
671   while (index != -1)
672     {
673       basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
674       bb->prev_bb = p_bb;
675       p_bb->next_bb = bb;
676       p_bb = bb;
677       index = streamer_read_hwi (ib);
678     }
679 }
680
681
682 /* Read the SSA names array for function FN from DATA_IN using input
683    block IB.  */
684
685 static void
686 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
687                  struct function *fn)
688 {
689   unsigned int i, size;
690
691   size = streamer_read_uhwi (ib);
692   init_ssanames (fn, size);
693
694   i = streamer_read_uhwi (ib);
695   while (i)
696     {
697       tree ssa_name, name;
698       bool is_default_def;
699
700       /* Skip over the elements that had been freed.  */
701       while (VEC_length (tree, SSANAMES (fn)) < i)
702         VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
703
704       is_default_def = (streamer_read_uchar (ib) != 0);
705       name = stream_read_tree (ib, data_in);
706       ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
707
708       if (is_default_def)
709         set_default_def (SSA_NAME_VAR (ssa_name), ssa_name);
710
711       i = streamer_read_uhwi (ib);
712     }
713 }
714
715
716 /* Go through all NODE edges and fixup call_stmt pointers
717    so they point to STMTS.  */
718
719 static void
720 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts)
721 {
722   struct cgraph_edge *cedge;
723   for (cedge = node->callees; cedge; cedge = cedge->next_callee)
724     cedge->call_stmt = stmts[cedge->lto_stmt_uid];
725   for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
726     cedge->call_stmt = stmts[cedge->lto_stmt_uid];
727 }
728
729 /* Fixup call_stmt pointers in NODE and all clones.  */
730
731 static void
732 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
733 {
734   struct cgraph_node *node;
735
736   while (orig->clone_of)
737     orig = orig->clone_of;
738
739   fixup_call_stmt_edges_1 (orig, stmts);
740   if (orig->clones)
741     for (node = orig->clones; node != orig;)
742       {
743         fixup_call_stmt_edges_1 (node, stmts);
744         if (node->clones)
745           node = node->clones;
746         else if (node->next_sibling_clone)
747           node = node->next_sibling_clone;
748         else
749           {
750             while (node != orig && !node->next_sibling_clone)
751               node = node->clone_of;
752             if (node != orig)
753               node = node->next_sibling_clone;
754           }
755       }
756 }
757
758 /* Read the body of function FN_DECL from DATA_IN using input block IB.  */
759
760 static void
761 input_function (tree fn_decl, struct data_in *data_in,
762                 struct lto_input_block *ib)
763 {
764   struct function *fn;
765   enum LTO_tags tag;
766   gimple *stmts;
767   basic_block bb;
768   struct bitpack_d bp;
769   struct cgraph_node *node;
770   tree args, narg, oarg;
771   int len;
772
773   fn = DECL_STRUCT_FUNCTION (fn_decl);
774   tag = streamer_read_record_start (ib);
775   clear_line_info (data_in);
776
777   gimple_register_cfg_hooks ();
778   lto_tag_check (tag, LTO_function);
779
780   /* Read all the attributes for FN.  */
781   bp = streamer_read_bitpack (ib);
782   fn->is_thunk = bp_unpack_value (&bp, 1);
783   fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
784   fn->after_tree_profile = bp_unpack_value (&bp, 1);
785   fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
786   fn->returns_struct = bp_unpack_value (&bp, 1);
787   fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
788   fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
789   fn->after_inlining = bp_unpack_value (&bp, 1);
790   fn->stdarg = bp_unpack_value (&bp, 1);
791   fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
792   fn->calls_alloca = bp_unpack_value (&bp, 1);
793   fn->calls_setjmp = bp_unpack_value (&bp, 1);
794   fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
795   fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
796
797   /* Input the function start and end loci.  */
798   fn->function_start_locus = lto_input_location (ib, data_in);
799   fn->function_end_locus = lto_input_location (ib, data_in);
800
801   /* Input the current IL state of the function.  */
802   fn->curr_properties = streamer_read_uhwi (ib);
803
804   /* Read the static chain and non-local goto save area.  */
805   fn->static_chain_decl = stream_read_tree (ib, data_in);
806   fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
807
808   /* Read all the local symbols.  */
809   len = streamer_read_hwi (ib);
810   if (len > 0)
811     {
812       int i;
813       VEC_safe_grow (tree, gc, fn->local_decls, len);
814       for (i = 0; i < len; i++)
815         {
816           tree t = stream_read_tree (ib, data_in);
817           VEC_replace (tree, fn->local_decls, i, t);
818         }
819     }
820
821   /* Read all function arguments.  We need to re-map them here to the
822      arguments of the merged function declaration.  */
823   args = stream_read_tree (ib, data_in);
824   for (oarg = args, narg = DECL_ARGUMENTS (fn_decl);
825        oarg && narg;
826        oarg = TREE_CHAIN (oarg), narg = TREE_CHAIN (narg))
827     {
828       unsigned ix;
829       bool res;
830       res = streamer_tree_cache_lookup (data_in->reader_cache, oarg, &ix);
831       gcc_assert (res);
832       /* Replace the argument in the streamer cache.  */
833       streamer_tree_cache_insert_at (data_in->reader_cache, narg, ix);
834     }
835   gcc_assert (!oarg && !narg);
836
837   /* Read all the SSA names.  */
838   input_ssa_names (ib, data_in, fn);
839
840   /* Read the exception handling regions in the function.  */
841   input_eh_regions (ib, data_in, fn);
842
843   /* Read the tree of lexical scopes for the function.  */
844   DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
845   gcc_assert (DECL_INITIAL (fn_decl));
846   DECL_SAVED_TREE (fn_decl) = NULL_TREE;
847   node = cgraph_get_create_node (fn_decl);
848
849   /* Read all the basic blocks.  */
850   tag = streamer_read_record_start (ib);
851   while (tag)
852     {
853       input_bb (ib, tag, data_in, fn,
854                 node->count_materialization_scale);
855       tag = streamer_read_record_start (ib);
856     }
857
858   /* Fix up the call statements that are mentioned in the callgraph
859      edges.  */
860   set_gimple_stmt_max_uid (cfun, 0);
861   FOR_ALL_BB (bb)
862     {
863       gimple_stmt_iterator gsi;
864       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
865         {
866           gimple stmt = gsi_stmt (gsi);
867           gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
868         }
869     }
870   stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
871   FOR_ALL_BB (bb)
872     {
873       gimple_stmt_iterator bsi = gsi_start_bb (bb);
874       while (!gsi_end_p (bsi))
875         {
876           gimple stmt = gsi_stmt (bsi);
877           /* If we're recompiling LTO objects with debug stmts but
878              we're not supposed to have debug stmts, remove them now.
879              We can't remove them earlier because this would cause uid
880              mismatches in fixups, but we can do it at this point, as
881              long as debug stmts don't require fixups.  */
882           if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
883             {
884               gimple_stmt_iterator gsi = bsi;
885               gsi_next (&bsi);
886               gsi_remove (&gsi, true);
887             }
888           else
889             {
890               gsi_next (&bsi);
891               stmts[gimple_uid (stmt)] = stmt;
892             }
893         }
894     }
895
896   /* Set the gimple body to the statement sequence in the entry
897      basic block.  FIXME lto, this is fairly hacky.  The existence
898      of a gimple body is used by the cgraph routines, but we should
899      really use the presence of the CFG.  */
900   {
901     edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
902     gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
903   }
904
905   fixup_call_stmt_edges (node, stmts);
906   execute_all_ipa_stmt_fixups (node, stmts);
907
908   update_ssa (TODO_update_ssa_only_virtuals);
909   free_dominance_info (CDI_DOMINATORS);
910   free_dominance_info (CDI_POST_DOMINATORS);
911   free (stmts);
912 }
913
914
915 /* Read initializer expressions for public statics.  DATA_IN is the
916    file being read.  IB is the input block used for reading.  */
917
918 static void
919 input_alias_pairs (struct lto_input_block *ib, struct data_in *data_in)
920 {
921   tree var;
922
923   clear_line_info (data_in);
924
925   var = stream_read_tree (ib, data_in);
926   while (var)
927     {
928       const char *orig_name, *new_name;
929       alias_pair *p;
930
931       p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
932       p->decl = var;
933       p->target = stream_read_tree (ib, data_in);
934
935       /* If the target is a static object, we may have registered a
936          new name for it to avoid clashes between statics coming from
937          different files.  In that case, use the new name.  */
938       orig_name = IDENTIFIER_POINTER (p->target);
939       new_name = lto_get_decl_name_mapping (data_in->file_data, orig_name);
940       if (strcmp (orig_name, new_name) != 0)
941         p->target = get_identifier (new_name);
942
943       var = stream_read_tree (ib, data_in);
944     }
945 }
946
947
948 /* Read the body from DATA for function FN_DECL and fill it in.
949    FILE_DATA are the global decls and types.  SECTION_TYPE is either
950    LTO_section_function_body or LTO_section_static_initializer.  If
951    section type is LTO_section_function_body, FN must be the decl for
952    that function.  */
953
954 static void
955 lto_read_body (struct lto_file_decl_data *file_data, tree fn_decl,
956                const char *data, enum lto_section_type section_type)
957 {
958   const struct lto_function_header *header;
959   struct data_in *data_in;
960   int32_t cfg_offset;
961   int32_t main_offset;
962   int32_t string_offset;
963   struct lto_input_block ib_cfg;
964   struct lto_input_block ib_main;
965
966   header = (const struct lto_function_header *) data;
967   cfg_offset = sizeof (struct lto_function_header);
968   main_offset = cfg_offset + header->cfg_size;
969   string_offset = main_offset + header->main_size;
970
971   LTO_INIT_INPUT_BLOCK (ib_cfg,
972                         data + cfg_offset,
973                         0,
974                         header->cfg_size);
975
976   LTO_INIT_INPUT_BLOCK (ib_main,
977                         data + main_offset,
978                         0,
979                         header->main_size);
980
981   data_in = lto_data_in_create (file_data, data + string_offset,
982                                 header->string_size, NULL);
983
984   /* Make sure the file was generated by the exact same compiler.  */
985   lto_check_version (header->lto_header.major_version,
986                      header->lto_header.minor_version);
987
988   if (section_type == LTO_section_function_body)
989     {
990       struct function *fn = DECL_STRUCT_FUNCTION (fn_decl);
991       struct lto_in_decl_state *decl_state;
992       struct cgraph_node *node = cgraph_get_node (fn_decl);
993
994       gcc_checking_assert (node);
995       push_cfun (fn);
996       init_tree_ssa (fn);
997
998       /* Use the function's decl state. */
999       decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1000       gcc_assert (decl_state);
1001       file_data->current_decl_state = decl_state;
1002
1003       input_cfg (&ib_cfg, fn, node->count_materialization_scale);
1004
1005       /* Set up the struct function.  */
1006       input_function (fn_decl, data_in, &ib_main);
1007
1008       /* We should now be in SSA.  */
1009       cfun->gimple_df->in_ssa_p = true;
1010
1011       /* Restore decl state */
1012       file_data->current_decl_state = file_data->global_decl_state;
1013
1014       pop_cfun ();
1015     }
1016   else
1017     {
1018       input_alias_pairs (&ib_main, data_in);
1019     }
1020
1021   clear_line_info (data_in);
1022   lto_data_in_delete (data_in);
1023 }
1024
1025
1026 /* Read the body of FN_DECL using DATA.  FILE_DATA holds the global
1027    decls and types.  */
1028
1029 void
1030 lto_input_function_body (struct lto_file_decl_data *file_data,
1031                          tree fn_decl, const char *data)
1032 {
1033   current_function_decl = fn_decl;
1034   lto_read_body (file_data, fn_decl, data, LTO_section_function_body);
1035 }
1036
1037
1038 /* Read in VAR_DECL using DATA.  FILE_DATA holds the global decls and
1039    types.  */
1040
1041 void
1042 lto_input_constructors_and_inits (struct lto_file_decl_data *file_data,
1043                                   const char *data)
1044 {
1045   lto_read_body (file_data, NULL, data, LTO_section_static_initializer);
1046 }
1047
1048
1049 /* Read the physical representation of a tree node with tag TAG from
1050    input block IB using the per-file context in DATA_IN.  */
1051
1052 static tree
1053 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1054                enum LTO_tags tag)
1055 {
1056   /* Instantiate a new tree node.  */
1057   tree result = streamer_alloc_tree (ib, data_in, tag);
1058
1059   /* Enter RESULT in the reader cache.  This will make RESULT
1060      available so that circular references in the rest of the tree
1061      structure can be resolved in subsequent calls to stream_read_tree.  */
1062   streamer_tree_cache_append (data_in->reader_cache, result);
1063
1064   /* Read all the bitfield values in RESULT.  Note that for LTO, we
1065      only write language-independent bitfields, so no more unpacking is
1066      needed.  */
1067   streamer_read_tree_bitfields (ib, result);
1068
1069   /* Read all the pointer fields in RESULT.  */
1070   streamer_read_tree_body (ib, data_in, result);
1071
1072   /* Read any LTO-specific data not read by the tree streamer.  */
1073   if (DECL_P (result)
1074       && TREE_CODE (result) != FUNCTION_DECL
1075       && TREE_CODE (result) != TRANSLATION_UNIT_DECL)
1076     DECL_INITIAL (result) = stream_read_tree (ib, data_in);
1077
1078   /* We should never try to instantiate an MD or NORMAL builtin here.  */
1079   if (TREE_CODE (result) == FUNCTION_DECL)
1080     gcc_assert (!streamer_handle_as_builtin_p (result));
1081
1082   /* end_marker = */ streamer_read_uchar (ib);
1083
1084 #ifdef LTO_STREAMER_DEBUG
1085   /* Remove the mapping to RESULT's original address set by
1086      streamer_alloc_tree.  */
1087   lto_orig_address_remove (result);
1088 #endif
1089
1090   return result;
1091 }
1092
1093
1094 /* Read a tree from input block IB using the per-file context in
1095    DATA_IN.  This context is used, for example, to resolve references
1096    to previously read nodes.  */
1097
1098 tree
1099 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1100 {
1101   enum LTO_tags tag;
1102   tree result;
1103
1104   tag = streamer_read_record_start (ib);
1105   gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1106
1107   if (tag == LTO_null)
1108     result = NULL_TREE;
1109   else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1110     {
1111       /* If TAG is a reference to an indexable tree, the next value
1112          in IB is the index into the table where we expect to find
1113          that tree.  */
1114       result = lto_input_tree_ref (ib, data_in, cfun, tag);
1115     }
1116   else if (tag == LTO_tree_pickle_reference)
1117     {
1118       /* If TAG is a reference to a previously read tree, look it up in
1119          the reader cache.  */
1120       result = streamer_get_pickled_tree (ib, data_in);
1121     }
1122   else if (tag == LTO_builtin_decl)
1123     {
1124       /* If we are going to read a built-in function, all we need is
1125          the code and class.  */
1126       result = streamer_get_builtin_tree (ib, data_in);
1127     }
1128   else if (tag == lto_tree_code_to_tag (INTEGER_CST))
1129     {
1130       /* For integer constants we only need the type and its hi/low
1131          words.  */
1132       result = streamer_read_integer_cst (ib, data_in);
1133     }
1134   else
1135     {
1136       /* Otherwise, materialize a new node from IB.  */
1137       result = lto_read_tree (ib, data_in, tag);
1138     }
1139
1140   return result;
1141 }
1142
1143
1144 /* Initialization for the LTO reader.  */
1145
1146 void
1147 lto_reader_init (void)
1148 {
1149   lto_streamer_init ();
1150   file_name_hash_table = htab_create (37, hash_string_slot_node,
1151                                       eq_string_slot_node, free);
1152 }
1153
1154
1155 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1156    table to use with LEN strings.  RESOLUTIONS is the vector of linker
1157    resolutions (NULL if not using a linker plugin).  */
1158
1159 struct data_in *
1160 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1161                     unsigned len,
1162                     VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
1163 {
1164   struct data_in *data_in = XCNEW (struct data_in);
1165   data_in->file_data = file_data;
1166   data_in->strings = strings;
1167   data_in->strings_len = len;
1168   data_in->globals_resolution = resolutions;
1169   data_in->reader_cache = streamer_tree_cache_create ();
1170
1171   return data_in;
1172 }
1173
1174
1175 /* Remove DATA_IN.  */
1176
1177 void
1178 lto_data_in_delete (struct data_in *data_in)
1179 {
1180   VEC_free (ld_plugin_symbol_resolution_t, heap, data_in->globals_resolution);
1181   streamer_tree_cache_delete (data_in->reader_cache);
1182   free (data_in->labels);
1183   free (data_in);
1184 }