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