3238ab8109cf43e78dbf4fde242bbf18e88a8615
[platform/upstream/gcc.git] / gcc / lto-streamer-in.c
1 /* Read the GIMPLE representation from a file stream.
2
3    Copyright (C) 2009-2014 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 "stringpool.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "hashtab.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
39 #include "is-a.h"
40 #include "gimple.h"
41 #include "gimple-iterator.h"
42 #include "gimple-ssa.h"
43 #include "tree-cfg.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
46 #include "tree-dfa.h"
47 #include "tree-ssa.h"
48 #include "tree-pass.h"
49 #include "function.h"
50 #include "diagnostic.h"
51 #include "except.h"
52 #include "debug.h"
53 #include "ipa-utils.h"
54 #include "data-streamer.h"
55 #include "gimple-streamer.h"
56 #include "lto-streamer.h"
57 #include "tree-streamer.h"
58 #include "tree-pass.h"
59 #include "streamer-hooks.h"
60 #include "cfgloop.h"
61
62
63 struct freeing_string_slot_hasher : string_slot_hasher
64 {
65   static inline void remove (value_type *);
66 };
67
68 inline void
69 freeing_string_slot_hasher::remove (value_type *v)
70 {
71   free (v);
72 }
73
74 /* The table to hold the file names.  */
75 static hash_table <freeing_string_slot_hasher> file_name_hash_table;
76
77
78 /* Check that tag ACTUAL has one of the given values.  NUM_TAGS is the
79    number of valid tag values to check.  */
80
81 void
82 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
83 {
84   va_list ap;
85   int i;
86
87   va_start (ap, ntags);
88   for (i = 0; i < ntags; i++)
89     if ((unsigned) actual == va_arg (ap, unsigned))
90       {
91         va_end (ap);
92         return;
93       }
94
95   va_end (ap);
96   internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
97 }
98
99
100 /* Read LENGTH bytes from STREAM to ADDR.  */
101
102 void
103 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
104 {
105   size_t i;
106   unsigned char *const buffer = (unsigned char *const) addr;
107
108   for (i = 0; i < length; i++)
109     buffer[i] = streamer_read_uchar (ib);
110 }
111
112
113 /* Lookup STRING in file_name_hash_table.  If found, return the existing
114    string, otherwise insert STRING as the canonical version.  */
115
116 static const char *
117 canon_file_name (const char *string)
118 {
119   string_slot **slot;
120   struct string_slot s_slot;
121   size_t len = strlen (string);
122
123   s_slot.s = string;
124   s_slot.len = len;
125
126   slot = file_name_hash_table.find_slot (&s_slot, INSERT);
127   if (*slot == NULL)
128     {
129       char *saved_string;
130       struct string_slot *new_slot;
131
132       saved_string = (char *) xmalloc (len + 1);
133       new_slot = XCNEW (struct string_slot);
134       memcpy (saved_string, string, len + 1);
135       new_slot->s = saved_string;
136       new_slot->len = len;
137       *slot = new_slot;
138       return saved_string;
139     }
140   else
141     {
142       struct string_slot *old_slot = *slot;
143       return old_slot->s;
144     }
145 }
146
147
148 /* Read a location bitpack from input block IB.  */
149
150 location_t
151 lto_input_location (struct bitpack_d *bp, struct data_in *data_in)
152 {
153   static const char *current_file;
154   static int current_line;
155   static int current_col;
156   bool file_change, line_change, column_change;
157   unsigned len;
158   bool prev_file = current_file != NULL;
159
160   if (bp_unpack_value (bp, 1))
161     return UNKNOWN_LOCATION;
162
163   file_change = bp_unpack_value (bp, 1);
164   line_change = bp_unpack_value (bp, 1);
165   column_change = bp_unpack_value (bp, 1);
166
167   if (file_change)
168     current_file = canon_file_name
169                      (string_for_index (data_in,
170                                         bp_unpack_var_len_unsigned (bp),
171                                         &len));
172
173   if (line_change)
174     current_line = bp_unpack_var_len_unsigned (bp);
175
176   if (column_change)
177     current_col = bp_unpack_var_len_unsigned (bp);
178
179   if (file_change)
180     {
181       if (prev_file)
182         linemap_add (line_table, LC_LEAVE, false, NULL, 0);
183
184       linemap_add (line_table, LC_ENTER, false, current_file, current_line);
185     }
186   else if (line_change)
187     linemap_line_start (line_table, current_line, current_col);
188
189   return linemap_position_for_column (line_table, current_col);
190 }
191
192
193 /* Read a reference to a tree node from DATA_IN using input block IB.
194    TAG is the expected node that should be found in IB, if TAG belongs
195    to one of the indexable trees, expect to read a reference index to
196    be looked up in one of the symbol tables, otherwise read the pysical
197    representation of the tree using stream_read_tree.  FN is the
198    function scope for the read tree.  */
199
200 tree
201 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
202                     struct function *fn, enum LTO_tags tag)
203 {
204   unsigned HOST_WIDE_INT ix_u;
205   tree result = NULL_TREE;
206
207   lto_tag_check_range (tag, LTO_field_decl_ref, LTO_namelist_decl_ref);
208
209   switch (tag)
210     {
211     case LTO_type_ref:
212       ix_u = streamer_read_uhwi (ib);
213       result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
214       break;
215
216     case LTO_ssa_name_ref:
217       ix_u = streamer_read_uhwi (ib);
218       result = (*SSANAMES (fn))[ix_u];
219       break;
220
221     case LTO_field_decl_ref:
222       ix_u = streamer_read_uhwi (ib);
223       result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
224       break;
225
226     case LTO_function_decl_ref:
227       ix_u = streamer_read_uhwi (ib);
228       result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
229       break;
230
231     case LTO_type_decl_ref:
232       ix_u = streamer_read_uhwi (ib);
233       result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
234       break;
235
236     case LTO_namespace_decl_ref:
237       ix_u = streamer_read_uhwi (ib);
238       result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
239       break;
240
241     case LTO_global_decl_ref:
242     case LTO_result_decl_ref:
243     case LTO_const_decl_ref:
244     case LTO_imported_decl_ref:
245     case LTO_label_decl_ref:
246     case LTO_translation_unit_decl_ref:
247     case LTO_namelist_decl_ref:
248       ix_u = streamer_read_uhwi (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 = streamer_read_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 = streamer_read_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 = streamer_read_record_start (ib);
319   if (tag == LTO_null)
320     return NULL;
321
322   r = ggc_alloc_cleared_eh_region_d ();
323   r->index = streamer_read_hwi (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) streamer_read_hwi (ib);
330   r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
331   r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (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 = streamer_read_uhwi (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         {
365           r->type = ERT_MUST_NOT_THROW;
366           r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
367           bitpack_d bp = streamer_read_bitpack (ib);
368           r->u.must_not_throw.failure_loc
369            = stream_input_location (&bp, data_in);
370         }
371         break;
372
373       default:
374         gcc_unreachable ();
375     }
376
377   r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
378
379   return r;
380 }
381
382
383 /* Read and return EH landing pad IX from input block IB, using descriptors
384    in DATA_IN.  */
385
386 static eh_landing_pad
387 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
388 {
389   enum LTO_tags tag;
390   eh_landing_pad lp;
391
392   /* Read the landing pad header.  */
393   tag = streamer_read_record_start (ib);
394   if (tag == LTO_null)
395     return NULL;
396
397   lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
398
399   lp = ggc_alloc_cleared_eh_landing_pad_d ();
400   lp->index = streamer_read_hwi (ib);
401   gcc_assert (lp->index == ix);
402   lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
403   lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
404   lp->post_landing_pad = stream_read_tree (ib, data_in);
405
406   return lp;
407 }
408
409
410 /* After reading the EH regions, pointers to peer and children regions
411    are region numbers.  This converts all these region numbers into
412    real pointers into the rematerialized regions for FN.  ROOT_REGION
413    is the region number for the root EH region in FN.  */
414
415 static void
416 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
417 {
418   unsigned i;
419   vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
420   vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
421   eh_region r;
422   eh_landing_pad lp;
423
424   gcc_assert (eh_array && lp_array);
425
426   gcc_assert (root_region >= 0);
427   fn->eh->region_tree = (*eh_array)[root_region];
428
429 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
430 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
431
432   /* Convert all the index numbers stored in pointer fields into
433      pointers to the corresponding slots in the EH region array.  */
434   FOR_EACH_VEC_ELT (*eh_array, i, r)
435     {
436       /* The array may contain NULL regions.  */
437       if (r == NULL)
438         continue;
439
440       gcc_assert (i == (unsigned) r->index);
441       FIXUP_EH_REGION (r->outer);
442       FIXUP_EH_REGION (r->inner);
443       FIXUP_EH_REGION (r->next_peer);
444       FIXUP_EH_LP (r->landing_pads);
445     }
446
447   /* Convert all the index numbers stored in pointer fields into
448      pointers to the corresponding slots in the EH landing pad array.  */
449   FOR_EACH_VEC_ELT (*lp_array, i, lp)
450     {
451       /* The array may contain NULL landing pads.  */
452       if (lp == NULL)
453         continue;
454
455       gcc_assert (i == (unsigned) lp->index);
456       FIXUP_EH_LP (lp->next_lp);
457       FIXUP_EH_REGION (lp->region);
458     }
459
460 #undef FIXUP_EH_REGION
461 #undef FIXUP_EH_LP
462 }
463
464
465 /* Initialize EH support.  */
466
467 void
468 lto_init_eh (void)
469 {
470   static bool eh_initialized_p = false;
471
472   if (eh_initialized_p)
473     return;
474
475   /* Contrary to most other FEs, we only initialize EH support when at
476      least one of the files in the set contains exception regions in
477      it.  Since this happens much later than the call to init_eh in
478      lang_dependent_init, we have to set flag_exceptions and call
479      init_eh again to initialize the EH tables.  */
480   flag_exceptions = 1;
481   init_eh ();
482
483   eh_initialized_p = true;
484 }
485
486
487 /* Read the exception table for FN from IB using the data descriptors
488    in DATA_IN.  */
489
490 static void
491 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
492                   struct function *fn)
493 {
494   HOST_WIDE_INT i, root_region, len;
495   enum LTO_tags tag;
496
497   tag = streamer_read_record_start (ib);
498   if (tag == LTO_null)
499     return;
500
501   lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
502
503   /* If the file contains EH regions, then it was compiled with
504      -fexceptions.  In that case, initialize the backend EH
505      machinery.  */
506   lto_init_eh ();
507
508   gcc_assert (fn->eh);
509
510   root_region = streamer_read_hwi (ib);
511   gcc_assert (root_region == (int) root_region);
512
513   /* Read the EH region array.  */
514   len = streamer_read_hwi (ib);
515   gcc_assert (len == (int) len);
516   if (len > 0)
517     {
518       vec_safe_grow_cleared (fn->eh->region_array, len);
519       for (i = 0; i < len; i++)
520         {
521           eh_region r = input_eh_region (ib, data_in, i);
522           (*fn->eh->region_array)[i] = r;
523         }
524     }
525
526   /* Read the landing pads.  */
527   len = streamer_read_hwi (ib);
528   gcc_assert (len == (int) len);
529   if (len > 0)
530     {
531       vec_safe_grow_cleared (fn->eh->lp_array, len);
532       for (i = 0; i < len; i++)
533         {
534           eh_landing_pad lp = input_eh_lp (ib, data_in, i);
535           (*fn->eh->lp_array)[i] = lp;
536         }
537     }
538
539   /* Read the runtime type data.  */
540   len = streamer_read_hwi (ib);
541   gcc_assert (len == (int) len);
542   if (len > 0)
543     {
544       vec_safe_grow_cleared (fn->eh->ttype_data, len);
545       for (i = 0; i < len; i++)
546         {
547           tree ttype = stream_read_tree (ib, data_in);
548           (*fn->eh->ttype_data)[i] = ttype;
549         }
550     }
551
552   /* Read the table of action chains.  */
553   len = streamer_read_hwi (ib);
554   gcc_assert (len == (int) len);
555   if (len > 0)
556     {
557       if (targetm.arm_eabi_unwinder)
558         {
559           vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
560           for (i = 0; i < len; i++)
561             {
562               tree t = stream_read_tree (ib, data_in);
563               (*fn->eh->ehspec_data.arm_eabi)[i] = t;
564             }
565         }
566       else
567         {
568           vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
569           for (i = 0; i < len; i++)
570             {
571               uchar c = streamer_read_uchar (ib);
572               (*fn->eh->ehspec_data.other)[i] = c;
573             }
574         }
575     }
576
577   /* Reconstruct the EH region tree by fixing up the peer/children
578      pointers.  */
579   fixup_eh_region_pointers (fn, root_region);
580
581   tag = streamer_read_record_start (ib);
582   lto_tag_check_range (tag, LTO_null, LTO_null);
583 }
584
585
586 /* Make a new basic block with index INDEX in function FN.  */
587
588 static basic_block
589 make_new_block (struct function *fn, unsigned int index)
590 {
591   basic_block bb = alloc_block ();
592   bb->index = index;
593   SET_BASIC_BLOCK_FOR_FN (fn, index, bb);
594   n_basic_blocks_for_fn (fn)++;
595   return bb;
596 }
597
598
599 /* Read the CFG for function FN from input block IB.  */
600
601 static void
602 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
603            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 (fn);
613
614   profile_status_for_fn (fn) = streamer_read_enum (ib, profile_status_d,
615                                                    PROFILE_LAST);
616
617   bb_count = streamer_read_uhwi (ib);
618
619   last_basic_block_for_fn (fn) = bb_count;
620   if (bb_count > basic_block_info_for_fn (fn)->length ())
621     vec_safe_grow_cleared (basic_block_info_for_fn (fn), bb_count);
622
623   if (bb_count > label_to_block_map_for_fn (fn)->length ())
624     vec_safe_grow_cleared (label_to_block_map_for_fn (fn), bb_count);
625
626   index = streamer_read_hwi (ib);
627   while (index != -1)
628     {
629       basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
630       unsigned int edge_count;
631
632       if (bb == NULL)
633         bb = make_new_block (fn, index);
634
635       edge_count = streamer_read_uhwi (ib);
636
637       /* Connect up the CFG.  */
638       for (i = 0; i < edge_count; i++)
639         {
640           unsigned int dest_index;
641           unsigned int edge_flags;
642           basic_block dest;
643           int probability;
644           gcov_type count;
645           edge e;
646
647           dest_index = streamer_read_uhwi (ib);
648           probability = (int) streamer_read_hwi (ib);
649           count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
650                                count_materialization_scale);
651           edge_flags = streamer_read_uhwi (ib);
652
653           dest = BASIC_BLOCK_FOR_FN (fn, dest_index);
654
655           if (dest == NULL)
656             dest = make_new_block (fn, dest_index);
657
658           e = make_edge (bb, dest, edge_flags);
659           e->probability = probability;
660           e->count = count;
661         }
662
663       index = streamer_read_hwi (ib);
664     }
665
666   p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
667   index = streamer_read_hwi (ib);
668   while (index != -1)
669     {
670       basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
671       bb->prev_bb = p_bb;
672       p_bb->next_bb = bb;
673       p_bb = bb;
674       index = streamer_read_hwi (ib);
675     }
676
677   /* ???  The cfgloop interface is tied to cfun.  */
678   gcc_assert (cfun == fn);
679
680   /* Input the loop tree.  */
681   unsigned n_loops = streamer_read_uhwi (ib);
682   if (n_loops == 0)
683     return;
684
685   struct loops *loops = ggc_alloc_cleared_loops ();
686   init_loops_structure (fn, loops, n_loops);
687   set_loops_for_fn (fn, loops);
688
689   /* Input each loop and associate it with its loop header so
690      flow_loops_find can rebuild the loop tree.  */
691   for (unsigned i = 1; i < n_loops; ++i)
692     {
693       int header_index = streamer_read_hwi (ib);
694       if (header_index == -1)
695         {
696           loops->larray->quick_push (NULL);
697           continue;
698         }
699
700       struct loop *loop = alloc_loop ();
701       loop->header = BASIC_BLOCK_FOR_FN (fn, header_index);
702       loop->header->loop_father = loop;
703
704       /* Read everything copy_loop_info copies.  */
705       loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
706       loop->any_upper_bound = streamer_read_hwi (ib);
707       if (loop->any_upper_bound)
708         {
709           loop->nb_iterations_upper_bound.low = streamer_read_uhwi (ib);
710           loop->nb_iterations_upper_bound.high = streamer_read_hwi (ib);
711         }
712       loop->any_estimate = streamer_read_hwi (ib);
713       if (loop->any_estimate)
714         {
715           loop->nb_iterations_estimate.low = streamer_read_uhwi (ib);
716           loop->nb_iterations_estimate.high = streamer_read_hwi (ib);
717         }
718
719       /* Read OMP SIMD related info.  */
720       loop->safelen = streamer_read_hwi (ib);
721       loop->force_vect = streamer_read_hwi (ib);
722       loop->simduid = stream_read_tree (ib, data_in);
723
724       place_new_loop (fn, loop);
725
726       /* flow_loops_find doesn't like loops not in the tree, hook them
727          all as siblings of the tree root temporarily.  */
728       flow_loop_tree_node_add (loops->tree_root, loop);
729     }
730
731   /* Rebuild the loop tree.  */
732   flow_loops_find (loops);
733 }
734
735
736 /* Read the SSA names array for function FN from DATA_IN using input
737    block IB.  */
738
739 static void
740 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
741                  struct function *fn)
742 {
743   unsigned int i, size;
744
745   size = streamer_read_uhwi (ib);
746   init_ssanames (fn, size);
747
748   i = streamer_read_uhwi (ib);
749   while (i)
750     {
751       tree ssa_name, name;
752       bool is_default_def;
753
754       /* Skip over the elements that had been freed.  */
755       while (SSANAMES (fn)->length () < i)
756         SSANAMES (fn)->quick_push (NULL_TREE);
757
758       is_default_def = (streamer_read_uchar (ib) != 0);
759       name = stream_read_tree (ib, data_in);
760       ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
761
762       if (is_default_def)
763         set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
764
765       i = streamer_read_uhwi (ib);
766     }
767 }
768
769
770 /* Go through all NODE edges and fixup call_stmt pointers
771    so they point to STMTS.  */
772
773 static void
774 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
775                          struct function *fn)
776 {
777   struct cgraph_edge *cedge;
778   struct ipa_ref *ref;
779   unsigned int i;
780
781   for (cedge = node->callees; cedge; cedge = cedge->next_callee)
782     {
783       if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
784         fatal_error ("Cgraph edge statement index out of range");
785       cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
786       if (!cedge->call_stmt)
787         fatal_error ("Cgraph edge statement index not found");
788     }
789   for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
790     {
791       if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
792         fatal_error ("Cgraph edge statement index out of range");
793       cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
794       if (!cedge->call_stmt)
795         fatal_error ("Cgraph edge statement index not found");
796     }
797   for (i = 0;
798        ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
799        i++)
800     if (ref->lto_stmt_uid)
801       {
802         if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
803           fatal_error ("Reference statement index out of range");
804         ref->stmt = stmts[ref->lto_stmt_uid - 1];
805         if (!ref->stmt)
806           fatal_error ("Reference statement index not found");
807       }
808 }
809
810
811 /* Fixup call_stmt pointers in NODE and all clones.  */
812
813 static void
814 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
815 {
816   struct cgraph_node *node;
817   struct function *fn;
818
819   while (orig->clone_of)
820     orig = orig->clone_of;
821   fn = DECL_STRUCT_FUNCTION (orig->decl);
822
823   fixup_call_stmt_edges_1 (orig, stmts, fn);
824   if (orig->clones)
825     for (node = orig->clones; node != orig;)
826       {
827         fixup_call_stmt_edges_1 (node, stmts, fn);
828         if (node->clones)
829           node = node->clones;
830         else if (node->next_sibling_clone)
831           node = node->next_sibling_clone;
832         else
833           {
834             while (node != orig && !node->next_sibling_clone)
835               node = node->clone_of;
836             if (node != orig)
837               node = node->next_sibling_clone;
838           }
839       }
840 }
841
842
843 /* Input the base body of struct function FN from DATA_IN
844    using input block IB.  */
845
846 static void
847 input_struct_function_base (struct function *fn, struct data_in *data_in,
848                             struct lto_input_block *ib)
849 {
850   struct bitpack_d bp;
851   int len;
852
853   /* Read the static chain and non-local goto save area.  */
854   fn->static_chain_decl = stream_read_tree (ib, data_in);
855   fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
856
857   /* Read all the local symbols.  */
858   len = streamer_read_hwi (ib);
859   if (len > 0)
860     {
861       int i;
862       vec_safe_grow_cleared (fn->local_decls, len);
863       for (i = 0; i < len; i++)
864         {
865           tree t = stream_read_tree (ib, data_in);
866           (*fn->local_decls)[i] = t;
867         }
868     }
869
870   /* Input the current IL state of the function.  */
871   fn->curr_properties = streamer_read_uhwi (ib);
872
873   /* Read all the attributes for FN.  */
874   bp = streamer_read_bitpack (ib);
875   fn->is_thunk = bp_unpack_value (&bp, 1);
876   fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
877   fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
878   fn->returns_struct = bp_unpack_value (&bp, 1);
879   fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
880   fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
881   fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
882   fn->after_inlining = bp_unpack_value (&bp, 1);
883   fn->stdarg = bp_unpack_value (&bp, 1);
884   fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
885   fn->calls_alloca = bp_unpack_value (&bp, 1);
886   fn->calls_setjmp = bp_unpack_value (&bp, 1);
887   fn->has_force_vect_loops = bp_unpack_value (&bp, 1);
888   fn->has_simduid_loops = bp_unpack_value (&bp, 1);
889   fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
890   fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
891
892   /* Input the function start and end loci.  */
893   fn->function_start_locus = stream_input_location (&bp, data_in);
894   fn->function_end_locus = stream_input_location (&bp, data_in);
895 }
896
897
898 /* Read the body of function FN_DECL from DATA_IN using input block IB.  */
899
900 static void
901 input_function (tree fn_decl, struct data_in *data_in,
902                 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
903 {
904   struct function *fn;
905   enum LTO_tags tag;
906   gimple *stmts;
907   basic_block bb;
908   struct cgraph_node *node;
909
910   tag = streamer_read_record_start (ib);
911   lto_tag_check (tag, LTO_function);
912
913   /* Read decls for parameters and args.  */
914   DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
915   DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
916
917   /* Read the tree of lexical scopes for the function.  */
918   DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
919
920   if (!streamer_read_uhwi (ib))
921     return;
922
923   push_struct_function (fn_decl);
924   fn = DECL_STRUCT_FUNCTION (fn_decl);
925   init_tree_ssa (fn);
926   /* We input IL in SSA form.  */
927   cfun->gimple_df->in_ssa_p = true;
928
929   gimple_register_cfg_hooks ();
930
931   node = cgraph_get_node (fn_decl);
932   if (!node)
933     node = cgraph_create_node (fn_decl);
934   input_struct_function_base (fn, data_in, ib);
935   input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
936
937   /* Read all the SSA names.  */
938   input_ssa_names (ib, data_in, fn);
939
940   /* Read the exception handling regions in the function.  */
941   input_eh_regions (ib, data_in, fn);
942
943   gcc_assert (DECL_INITIAL (fn_decl));
944   DECL_SAVED_TREE (fn_decl) = NULL_TREE;
945
946   /* Read all the basic blocks.  */
947   tag = streamer_read_record_start (ib);
948   while (tag)
949     {
950       input_bb (ib, tag, data_in, fn,
951                 node->count_materialization_scale);
952       tag = streamer_read_record_start (ib);
953     }
954
955   /* Fix up the call statements that are mentioned in the callgraph
956      edges.  */
957   set_gimple_stmt_max_uid (cfun, 0);
958   FOR_ALL_BB_FN (bb, cfun)
959     {
960       gimple_stmt_iterator gsi;
961       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
962         {
963           gimple stmt = gsi_stmt (gsi);
964           gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
965         }
966       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
967         {
968           gimple stmt = gsi_stmt (gsi);
969           gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
970         }
971     }
972   stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
973   FOR_ALL_BB_FN (bb, cfun)
974     {
975       gimple_stmt_iterator bsi = gsi_start_phis (bb);
976       while (!gsi_end_p (bsi))
977         {
978           gimple stmt = gsi_stmt (bsi);
979           gsi_next (&bsi);
980           stmts[gimple_uid (stmt)] = stmt;
981         }
982       bsi = gsi_start_bb (bb);
983       while (!gsi_end_p (bsi))
984         {
985           gimple stmt = gsi_stmt (bsi);
986           /* If we're recompiling LTO objects with debug stmts but
987              we're not supposed to have debug stmts, remove them now.
988              We can't remove them earlier because this would cause uid
989              mismatches in fixups, but we can do it at this point, as
990              long as debug stmts don't require fixups.  */
991           if (!MAY_HAVE_DEBUG_STMTS && !flag_wpa && is_gimple_debug (stmt))
992             {
993               gimple_stmt_iterator gsi = bsi;
994               gsi_next (&bsi);
995               gsi_remove (&gsi, true);
996             }
997           else
998             {
999               gsi_next (&bsi);
1000               stmts[gimple_uid (stmt)] = stmt;
1001             }
1002         }
1003     }
1004
1005   /* Set the gimple body to the statement sequence in the entry
1006      basic block.  FIXME lto, this is fairly hacky.  The existence
1007      of a gimple body is used by the cgraph routines, but we should
1008      really use the presence of the CFG.  */
1009   {
1010     edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1011     gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1012   }
1013
1014   fixup_call_stmt_edges (node, stmts);
1015   execute_all_ipa_stmt_fixups (node, stmts);
1016
1017   update_ssa (TODO_update_ssa_only_virtuals);
1018   free_dominance_info (CDI_DOMINATORS);
1019   free_dominance_info (CDI_POST_DOMINATORS);
1020   free (stmts);
1021   pop_cfun ();
1022 }
1023
1024
1025 /* Read the body from DATA for function NODE and fill it in.
1026    FILE_DATA are the global decls and types.  SECTION_TYPE is either
1027    LTO_section_function_body or LTO_section_static_initializer.  If
1028    section type is LTO_section_function_body, FN must be the decl for
1029    that function.  */
1030
1031 static void
1032 lto_read_body (struct lto_file_decl_data *file_data, struct cgraph_node *node,
1033                const char *data, enum lto_section_type section_type)
1034 {
1035   const struct lto_function_header *header;
1036   struct data_in *data_in;
1037   int cfg_offset;
1038   int main_offset;
1039   int string_offset;
1040   struct lto_input_block ib_cfg;
1041   struct lto_input_block ib_main;
1042   tree fn_decl = node->decl;
1043
1044   header = (const struct lto_function_header *) data;
1045   cfg_offset = sizeof (struct lto_function_header);
1046   main_offset = cfg_offset + header->cfg_size;
1047   string_offset = main_offset + header->main_size;
1048
1049   LTO_INIT_INPUT_BLOCK (ib_cfg,
1050                         data + cfg_offset,
1051                         0,
1052                         header->cfg_size);
1053
1054   LTO_INIT_INPUT_BLOCK (ib_main,
1055                         data + main_offset,
1056                         0,
1057                         header->main_size);
1058
1059   data_in = lto_data_in_create (file_data, data + string_offset,
1060                               header->string_size, vNULL);
1061
1062   if (section_type == LTO_section_function_body)
1063     {
1064       struct lto_in_decl_state *decl_state;
1065       unsigned from;
1066
1067       gcc_checking_assert (node);
1068
1069       /* Use the function's decl state. */
1070       decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1071       gcc_assert (decl_state);
1072       file_data->current_decl_state = decl_state;
1073
1074
1075       /* Set up the struct function.  */
1076       from = data_in->reader_cache->nodes.length ();
1077       input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1078       /* And fixup types we streamed locally.  */
1079         {
1080           struct streamer_tree_cache_d *cache = data_in->reader_cache;
1081           unsigned len = cache->nodes.length ();
1082           unsigned i;
1083           for (i = len; i-- > from;)
1084             {
1085               tree t = streamer_tree_cache_get_tree (cache, i);
1086               if (t == NULL_TREE)
1087                 continue;
1088
1089               if (TYPE_P (t))
1090                 {
1091                   gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1092                   TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1093                   if (TYPE_MAIN_VARIANT (t) != t)
1094                     {
1095                       gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1096                       TYPE_NEXT_VARIANT (t)
1097                         = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1098                       TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1099                     }
1100                 }
1101             }
1102         }
1103
1104       /* Restore decl state */
1105       file_data->current_decl_state = file_data->global_decl_state;
1106     }
1107
1108   lto_data_in_delete (data_in);
1109 }
1110
1111
1112 /* Read the body of NODE using DATA.  FILE_DATA holds the global
1113    decls and types.  */
1114
1115 void
1116 lto_input_function_body (struct lto_file_decl_data *file_data,
1117                          struct cgraph_node *node, const char *data)
1118 {
1119   lto_read_body (file_data, node, data, LTO_section_function_body);
1120 }
1121
1122
1123 /* Read the physical representation of a tree node EXPR from
1124    input block IB using the per-file context in DATA_IN.  */
1125
1126 static void
1127 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1128 {
1129   /* Read all the bitfield values in EXPR.  Note that for LTO, we
1130      only write language-independent bitfields, so no more unpacking is
1131      needed.  */
1132   streamer_read_tree_bitfields (ib, data_in, expr);
1133
1134   /* Read all the pointer fields in EXPR.  */
1135   streamer_read_tree_body (ib, data_in, expr);
1136
1137   /* Read any LTO-specific data not read by the tree streamer.  */
1138   if (DECL_P (expr)
1139       && TREE_CODE (expr) != FUNCTION_DECL
1140       && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1141     DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1142
1143   /* We should never try to instantiate an MD or NORMAL builtin here.  */
1144   if (TREE_CODE (expr) == FUNCTION_DECL)
1145     gcc_assert (!streamer_handle_as_builtin_p (expr));
1146
1147 #ifdef LTO_STREAMER_DEBUG
1148   /* Remove the mapping to RESULT's original address set by
1149      streamer_alloc_tree.  */
1150   lto_orig_address_remove (expr);
1151 #endif
1152 }
1153
1154 /* Read the physical representation of a tree node with tag TAG from
1155    input block IB using the per-file context in DATA_IN.  */
1156
1157 static tree
1158 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1159                enum LTO_tags tag, hashval_t hash)
1160 {
1161   /* Instantiate a new tree node.  */
1162   tree result = streamer_alloc_tree (ib, data_in, tag);
1163
1164   /* Enter RESULT in the reader cache.  This will make RESULT
1165      available so that circular references in the rest of the tree
1166      structure can be resolved in subsequent calls to stream_read_tree.  */
1167   streamer_tree_cache_append (data_in->reader_cache, result, hash);
1168
1169   lto_read_tree_1 (ib, data_in, result);
1170
1171   /* end_marker = */ streamer_read_uchar (ib);
1172
1173   return result;
1174 }
1175
1176
1177 /* Populate the reader cache with trees materialized from the SCC
1178    following in the IB, DATA_IN stream.  */
1179
1180 hashval_t
1181 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1182                unsigned *len, unsigned *entry_len)
1183 {
1184   /* A blob of unnamed tree nodes, fill the cache from it and
1185      recurse.  */
1186   unsigned size = streamer_read_uhwi (ib);
1187   hashval_t scc_hash = streamer_read_uhwi (ib);
1188   unsigned scc_entry_len = 1;
1189
1190   if (size == 1)
1191     {
1192       enum LTO_tags tag = streamer_read_record_start (ib);
1193       lto_input_tree_1 (ib, data_in, tag, scc_hash);
1194     }
1195   else
1196     {
1197       unsigned int first = data_in->reader_cache->nodes.length ();
1198       tree result;
1199
1200       scc_entry_len = streamer_read_uhwi (ib);
1201
1202       /* Materialize size trees by reading their headers.  */
1203       for (unsigned i = 0; i < size; ++i)
1204         {
1205           enum LTO_tags tag = streamer_read_record_start (ib);
1206           if (tag == LTO_null
1207               || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1208               || tag == LTO_tree_pickle_reference
1209               || tag == LTO_builtin_decl
1210               || tag == LTO_integer_cst
1211               || tag == LTO_tree_scc)
1212             gcc_unreachable ();
1213
1214           result = streamer_alloc_tree (ib, data_in, tag);
1215           streamer_tree_cache_append (data_in->reader_cache, result, 0);
1216         }
1217
1218       /* Read the tree bitpacks and references.  */
1219       for (unsigned i = 0; i < size; ++i)
1220         {
1221           result = streamer_tree_cache_get_tree (data_in->reader_cache,
1222                                                  first + i);
1223           lto_read_tree_1 (ib, data_in, result);
1224           /* end_marker = */ streamer_read_uchar (ib);
1225         }
1226     }
1227
1228   *len = size;
1229   *entry_len = scc_entry_len;
1230   return scc_hash;
1231 }
1232
1233
1234 /* Read a tree from input block IB using the per-file context in
1235    DATA_IN.  This context is used, for example, to resolve references
1236    to previously read nodes.  */
1237
1238 tree
1239 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1240                   enum LTO_tags tag, hashval_t hash)
1241 {
1242   tree result;
1243
1244   gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1245
1246   if (tag == LTO_null)
1247     result = NULL_TREE;
1248   else if (tag >= LTO_field_decl_ref && tag <= LTO_namelist_decl_ref)
1249     {
1250       /* If TAG is a reference to an indexable tree, the next value
1251          in IB is the index into the table where we expect to find
1252          that tree.  */
1253       result = lto_input_tree_ref (ib, data_in, cfun, tag);
1254     }
1255   else if (tag == LTO_tree_pickle_reference)
1256     {
1257       /* If TAG is a reference to a previously read tree, look it up in
1258          the reader cache.  */
1259       result = streamer_get_pickled_tree (ib, data_in);
1260     }
1261   else if (tag == LTO_builtin_decl)
1262     {
1263       /* If we are going to read a built-in function, all we need is
1264          the code and class.  */
1265       result = streamer_get_builtin_tree (ib, data_in);
1266     }
1267   else if (tag == LTO_integer_cst)
1268     {
1269       /* For shared integer constants in singletons we can use the existing
1270          tree integer constant merging code.  */
1271       tree type = stream_read_tree (ib, data_in);
1272       unsigned HOST_WIDE_INT low = streamer_read_uhwi (ib);
1273       HOST_WIDE_INT high = streamer_read_hwi (ib);
1274       result = build_int_cst_wide (type, low, high);
1275       streamer_tree_cache_append (data_in->reader_cache, result, hash);
1276     }
1277   else if (tag == LTO_tree_scc)
1278     {
1279       unsigned len, entry_len;
1280
1281       /* Input and skip the SCC.  */
1282       lto_input_scc (ib, data_in, &len, &entry_len);
1283
1284       /* Recurse.  */
1285       return lto_input_tree (ib, data_in);
1286     }
1287   else
1288     {
1289       /* Otherwise, materialize a new node from IB.  */
1290       result = lto_read_tree (ib, data_in, tag, hash);
1291     }
1292
1293   return result;
1294 }
1295
1296 tree
1297 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1298 {
1299   return lto_input_tree_1 (ib, data_in, streamer_read_record_start (ib), 0);
1300 }
1301
1302
1303 /* Input toplevel asms.  */
1304
1305 void
1306 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1307 {
1308   size_t len;
1309   const char *data = lto_get_section_data (file_data, LTO_section_asm,
1310                                            NULL, &len);
1311   const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1312   int string_offset;
1313   struct data_in *data_in;
1314   struct lto_input_block ib;
1315   tree str;
1316
1317   if (! data)
1318     return;
1319
1320   string_offset = sizeof (*header) + header->main_size;
1321
1322   LTO_INIT_INPUT_BLOCK (ib,
1323                         data + sizeof (*header),
1324                         0,
1325                         header->main_size);
1326
1327   data_in = lto_data_in_create (file_data, data + string_offset,
1328                               header->string_size, vNULL);
1329
1330   while ((str = streamer_read_string_cst (data_in, &ib)))
1331     {
1332       struct asm_node *node = add_asm_node (str);
1333       node->order = streamer_read_hwi (&ib) + order_base;
1334       if (node->order >= symtab_order)
1335         symtab_order = node->order + 1;
1336     }
1337
1338   lto_data_in_delete (data_in);
1339
1340   lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1341 }
1342
1343
1344 /* Initialization for the LTO reader.  */
1345
1346 void
1347 lto_reader_init (void)
1348 {
1349   lto_streamer_init ();
1350   file_name_hash_table.create (37);
1351 }
1352
1353
1354 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1355    table to use with LEN strings.  RESOLUTIONS is the vector of linker
1356    resolutions (NULL if not using a linker plugin).  */
1357
1358 struct data_in *
1359 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1360                     unsigned len,
1361                     vec<ld_plugin_symbol_resolution_t> resolutions)
1362 {
1363   struct data_in *data_in = XCNEW (struct data_in);
1364   data_in->file_data = file_data;
1365   data_in->strings = strings;
1366   data_in->strings_len = len;
1367   data_in->globals_resolution = resolutions;
1368   data_in->reader_cache = streamer_tree_cache_create (false, false);
1369
1370   return data_in;
1371 }
1372
1373
1374 /* Remove DATA_IN.  */
1375
1376 void
1377 lto_data_in_delete (struct data_in *data_in)
1378 {
1379   data_in->globals_resolution.release ();
1380   streamer_tree_cache_delete (data_in->reader_cache);
1381   free (data_in->labels);
1382   free (data_in);
1383 }