OSDN Git Service

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