]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto-streamer-in.c
include/
[thirdparty/gcc.git] / gcc / lto-streamer-in.c
1 /* Read the GIMPLE representation from a file stream.
2
3 Copyright (C) 2009-2013 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_global_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 ix_u = streamer_read_uhwi (ib);
248 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
249 break;
250
251 default:
252 gcc_unreachable ();
253 }
254
255 gcc_assert (result);
256
257 return result;
258 }
259
260
261 /* Read and return a double-linked list of catch handlers from input
262 block IB, using descriptors in DATA_IN. */
263
264 static struct eh_catch_d *
265 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
266 eh_catch *last_p)
267 {
268 eh_catch first;
269 enum LTO_tags tag;
270
271 *last_p = first = NULL;
272 tag = streamer_read_record_start (ib);
273 while (tag)
274 {
275 tree list;
276 eh_catch n;
277
278 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
279
280 /* Read the catch node. */
281 n = ggc_alloc_cleared_eh_catch_d ();
282 n->type_list = stream_read_tree (ib, data_in);
283 n->filter_list = stream_read_tree (ib, data_in);
284 n->label = stream_read_tree (ib, data_in);
285
286 /* Register all the types in N->FILTER_LIST. */
287 for (list = n->filter_list; list; list = TREE_CHAIN (list))
288 add_type_for_runtime (TREE_VALUE (list));
289
290 /* Chain N to the end of the list. */
291 if (*last_p)
292 (*last_p)->next_catch = n;
293 n->prev_catch = *last_p;
294 *last_p = n;
295
296 /* Set the head of the list the first time through the loop. */
297 if (first == NULL)
298 first = n;
299
300 tag = streamer_read_record_start (ib);
301 }
302
303 return first;
304 }
305
306
307 /* Read and return EH region IX from input block IB, using descriptors
308 in DATA_IN. */
309
310 static eh_region
311 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
312 {
313 enum LTO_tags tag;
314 eh_region r;
315
316 /* Read the region header. */
317 tag = streamer_read_record_start (ib);
318 if (tag == LTO_null)
319 return NULL;
320
321 r = ggc_alloc_cleared_eh_region_d ();
322 r->index = streamer_read_hwi (ib);
323
324 gcc_assert (r->index == ix);
325
326 /* Read all the region pointers as region numbers. We'll fix up
327 the pointers once the whole array has been read. */
328 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
329 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
330 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
331
332 switch (tag)
333 {
334 case LTO_ert_cleanup:
335 r->type = ERT_CLEANUP;
336 break;
337
338 case LTO_ert_try:
339 {
340 struct eh_catch_d *last_catch;
341 r->type = ERT_TRY;
342 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
343 &last_catch);
344 r->u.eh_try.last_catch = last_catch;
345 break;
346 }
347
348 case LTO_ert_allowed_exceptions:
349 {
350 tree l;
351
352 r->type = ERT_ALLOWED_EXCEPTIONS;
353 r->u.allowed.type_list = stream_read_tree (ib, data_in);
354 r->u.allowed.label = stream_read_tree (ib, data_in);
355 r->u.allowed.filter = streamer_read_uhwi (ib);
356
357 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
358 add_type_for_runtime (TREE_VALUE (l));
359 }
360 break;
361
362 case LTO_ert_must_not_throw:
363 {
364 r->type = ERT_MUST_NOT_THROW;
365 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
366 bitpack_d bp = streamer_read_bitpack (ib);
367 r->u.must_not_throw.failure_loc
368 = stream_input_location (&bp, data_in);
369 }
370 break;
371
372 default:
373 gcc_unreachable ();
374 }
375
376 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
377
378 return r;
379 }
380
381
382 /* Read and return EH landing pad IX from input block IB, using descriptors
383 in DATA_IN. */
384
385 static eh_landing_pad
386 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
387 {
388 enum LTO_tags tag;
389 eh_landing_pad lp;
390
391 /* Read the landing pad header. */
392 tag = streamer_read_record_start (ib);
393 if (tag == LTO_null)
394 return NULL;
395
396 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
397
398 lp = ggc_alloc_cleared_eh_landing_pad_d ();
399 lp->index = streamer_read_hwi (ib);
400 gcc_assert (lp->index == ix);
401 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
402 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
403 lp->post_landing_pad = stream_read_tree (ib, data_in);
404
405 return lp;
406 }
407
408
409 /* After reading the EH regions, pointers to peer and children regions
410 are region numbers. This converts all these region numbers into
411 real pointers into the rematerialized regions for FN. ROOT_REGION
412 is the region number for the root EH region in FN. */
413
414 static void
415 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
416 {
417 unsigned i;
418 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
419 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
420 eh_region r;
421 eh_landing_pad lp;
422
423 gcc_assert (eh_array && lp_array);
424
425 gcc_assert (root_region >= 0);
426 fn->eh->region_tree = (*eh_array)[root_region];
427
428 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
429 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
430
431 /* Convert all the index numbers stored in pointer fields into
432 pointers to the corresponding slots in the EH region array. */
433 FOR_EACH_VEC_ELT (*eh_array, i, r)
434 {
435 /* The array may contain NULL regions. */
436 if (r == NULL)
437 continue;
438
439 gcc_assert (i == (unsigned) r->index);
440 FIXUP_EH_REGION (r->outer);
441 FIXUP_EH_REGION (r->inner);
442 FIXUP_EH_REGION (r->next_peer);
443 FIXUP_EH_LP (r->landing_pads);
444 }
445
446 /* Convert all the index numbers stored in pointer fields into
447 pointers to the corresponding slots in the EH landing pad array. */
448 FOR_EACH_VEC_ELT (*lp_array, i, lp)
449 {
450 /* The array may contain NULL landing pads. */
451 if (lp == NULL)
452 continue;
453
454 gcc_assert (i == (unsigned) lp->index);
455 FIXUP_EH_LP (lp->next_lp);
456 FIXUP_EH_REGION (lp->region);
457 }
458
459 #undef FIXUP_EH_REGION
460 #undef FIXUP_EH_LP
461 }
462
463
464 /* Initialize EH support. */
465
466 void
467 lto_init_eh (void)
468 {
469 static bool eh_initialized_p = false;
470
471 if (eh_initialized_p)
472 return;
473
474 /* Contrary to most other FEs, we only initialize EH support when at
475 least one of the files in the set contains exception regions in
476 it. Since this happens much later than the call to init_eh in
477 lang_dependent_init, we have to set flag_exceptions and call
478 init_eh again to initialize the EH tables. */
479 flag_exceptions = 1;
480 init_eh ();
481
482 eh_initialized_p = true;
483 }
484
485
486 /* Read the exception table for FN from IB using the data descriptors
487 in DATA_IN. */
488
489 static void
490 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
491 struct function *fn)
492 {
493 HOST_WIDE_INT i, root_region, len;
494 enum LTO_tags tag;
495
496 tag = streamer_read_record_start (ib);
497 if (tag == LTO_null)
498 return;
499
500 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
501
502 /* If the file contains EH regions, then it was compiled with
503 -fexceptions. In that case, initialize the backend EH
504 machinery. */
505 lto_init_eh ();
506
507 gcc_assert (fn->eh);
508
509 root_region = streamer_read_hwi (ib);
510 gcc_assert (root_region == (int) root_region);
511
512 /* Read the EH region array. */
513 len = streamer_read_hwi (ib);
514 gcc_assert (len == (int) len);
515 if (len > 0)
516 {
517 vec_safe_grow_cleared (fn->eh->region_array, len);
518 for (i = 0; i < len; i++)
519 {
520 eh_region r = input_eh_region (ib, data_in, i);
521 (*fn->eh->region_array)[i] = r;
522 }
523 }
524
525 /* Read the landing pads. */
526 len = streamer_read_hwi (ib);
527 gcc_assert (len == (int) len);
528 if (len > 0)
529 {
530 vec_safe_grow_cleared (fn->eh->lp_array, len);
531 for (i = 0; i < len; i++)
532 {
533 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
534 (*fn->eh->lp_array)[i] = lp;
535 }
536 }
537
538 /* Read the runtime type data. */
539 len = streamer_read_hwi (ib);
540 gcc_assert (len == (int) len);
541 if (len > 0)
542 {
543 vec_safe_grow_cleared (fn->eh->ttype_data, len);
544 for (i = 0; i < len; i++)
545 {
546 tree ttype = stream_read_tree (ib, data_in);
547 (*fn->eh->ttype_data)[i] = ttype;
548 }
549 }
550
551 /* Read the table of action chains. */
552 len = streamer_read_hwi (ib);
553 gcc_assert (len == (int) len);
554 if (len > 0)
555 {
556 if (targetm.arm_eabi_unwinder)
557 {
558 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
559 for (i = 0; i < len; i++)
560 {
561 tree t = stream_read_tree (ib, data_in);
562 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
563 }
564 }
565 else
566 {
567 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
568 for (i = 0; i < len; i++)
569 {
570 uchar c = streamer_read_uchar (ib);
571 (*fn->eh->ehspec_data.other)[i] = c;
572 }
573 }
574 }
575
576 /* Reconstruct the EH region tree by fixing up the peer/children
577 pointers. */
578 fixup_eh_region_pointers (fn, root_region);
579
580 tag = streamer_read_record_start (ib);
581 lto_tag_check_range (tag, LTO_null, LTO_null);
582 }
583
584
585 /* Make a new basic block with index INDEX in function FN. */
586
587 static basic_block
588 make_new_block (struct function *fn, unsigned int index)
589 {
590 basic_block bb = alloc_block ();
591 bb->index = index;
592 SET_BASIC_BLOCK_FOR_FUNCTION (fn, index, bb);
593 n_basic_blocks_for_fn (fn)++;
594 return bb;
595 }
596
597
598 /* Read the CFG for function FN from input block IB. */
599
600 static void
601 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
602 struct function *fn,
603 int count_materialization_scale)
604 {
605 unsigned int bb_count;
606 basic_block p_bb;
607 unsigned int i;
608 int index;
609
610 init_empty_tree_cfg_for_function (fn);
611 init_ssa_operands (fn);
612
613 profile_status_for_function (fn) = streamer_read_enum (ib, profile_status_d,
614 PROFILE_LAST);
615
616 bb_count = streamer_read_uhwi (ib);
617
618 last_basic_block_for_function (fn) = bb_count;
619 if (bb_count > basic_block_info_for_function (fn)->length ())
620 vec_safe_grow_cleared (basic_block_info_for_function (fn), bb_count);
621
622 if (bb_count > label_to_block_map_for_function (fn)->length ())
623 vec_safe_grow_cleared (label_to_block_map_for_function (fn), bb_count);
624
625 index = streamer_read_hwi (ib);
626 while (index != -1)
627 {
628 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
629 unsigned int edge_count;
630
631 if (bb == NULL)
632 bb = make_new_block (fn, index);
633
634 edge_count = streamer_read_uhwi (ib);
635
636 /* Connect up the CFG. */
637 for (i = 0; i < edge_count; i++)
638 {
639 unsigned int dest_index;
640 unsigned int edge_flags;
641 basic_block dest;
642 int probability;
643 gcov_type count;
644 edge e;
645
646 dest_index = streamer_read_uhwi (ib);
647 probability = (int) streamer_read_hwi (ib);
648 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
649 count_materialization_scale);
650 edge_flags = streamer_read_uhwi (ib);
651
652 dest = BASIC_BLOCK_FOR_FUNCTION (fn, dest_index);
653
654 if (dest == NULL)
655 dest = make_new_block (fn, dest_index);
656
657 e = make_edge (bb, dest, edge_flags);
658 e->probability = probability;
659 e->count = count;
660 }
661
662 index = streamer_read_hwi (ib);
663 }
664
665 p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
666 index = streamer_read_hwi (ib);
667 while (index != -1)
668 {
669 basic_block bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
670 bb->prev_bb = p_bb;
671 p_bb->next_bb = bb;
672 p_bb = bb;
673 index = streamer_read_hwi (ib);
674 }
675
676 /* ??? The cfgloop interface is tied to cfun. */
677 gcc_assert (cfun == fn);
678
679 /* Input the loop tree. */
680 unsigned n_loops = streamer_read_uhwi (ib);
681 if (n_loops == 0)
682 return;
683
684 struct loops *loops = ggc_alloc_cleared_loops ();
685 init_loops_structure (fn, loops, n_loops);
686 set_loops_for_fn (fn, loops);
687
688 /* Input each loop and associate it with its loop header so
689 flow_loops_find can rebuild the loop tree. */
690 for (unsigned i = 1; i < n_loops; ++i)
691 {
692 int header_index = streamer_read_hwi (ib);
693 if (header_index == -1)
694 {
695 loops->larray->quick_push (NULL);
696 continue;
697 }
698
699 struct loop *loop = alloc_loop ();
700 loop->header = BASIC_BLOCK_FOR_FUNCTION (fn, header_index);
701 loop->header->loop_father = loop;
702
703 /* Read everything copy_loop_info copies. */
704 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
705 loop->any_upper_bound = streamer_read_hwi (ib);
706 if (loop->any_upper_bound)
707 {
708 loop->nb_iterations_upper_bound.low = streamer_read_uhwi (ib);
709 loop->nb_iterations_upper_bound.high = streamer_read_hwi (ib);
710 }
711 loop->any_estimate = streamer_read_hwi (ib);
712 if (loop->any_estimate)
713 {
714 loop->nb_iterations_estimate.low = streamer_read_uhwi (ib);
715 loop->nb_iterations_estimate.high = streamer_read_hwi (ib);
716 }
717
718 /* Read OMP SIMD related info. */
719 loop->safelen = streamer_read_hwi (ib);
720 loop->force_vect = streamer_read_hwi (ib);
721 loop->simduid = stream_read_tree (ib, data_in);
722
723 place_new_loop (fn, loop);
724
725 /* flow_loops_find doesn't like loops not in the tree, hook them
726 all as siblings of the tree root temporarily. */
727 flow_loop_tree_node_add (loops->tree_root, loop);
728 }
729
730 /* Rebuild the loop tree. */
731 flow_loops_find (loops);
732 }
733
734
735 /* Read the SSA names array for function FN from DATA_IN using input
736 block IB. */
737
738 static void
739 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
740 struct function *fn)
741 {
742 unsigned int i, size;
743
744 size = streamer_read_uhwi (ib);
745 init_ssanames (fn, size);
746
747 i = streamer_read_uhwi (ib);
748 while (i)
749 {
750 tree ssa_name, name;
751 bool is_default_def;
752
753 /* Skip over the elements that had been freed. */
754 while (SSANAMES (fn)->length () < i)
755 SSANAMES (fn)->quick_push (NULL_TREE);
756
757 is_default_def = (streamer_read_uchar (ib) != 0);
758 name = stream_read_tree (ib, data_in);
759 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
760
761 if (is_default_def)
762 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
763
764 i = streamer_read_uhwi (ib);
765 }
766 }
767
768
769 /* Go through all NODE edges and fixup call_stmt pointers
770 so they point to STMTS. */
771
772 static void
773 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
774 struct function *fn)
775 {
776 struct cgraph_edge *cedge;
777 struct ipa_ref *ref;
778 unsigned int i;
779
780 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
781 {
782 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
783 fatal_error ("Cgraph edge statement index out of range");
784 cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
785 if (!cedge->call_stmt)
786 fatal_error ("Cgraph edge statement index not found");
787 }
788 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
789 {
790 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
791 fatal_error ("Cgraph edge statement index out of range");
792 cedge->call_stmt = stmts[cedge->lto_stmt_uid - 1];
793 if (!cedge->call_stmt)
794 fatal_error ("Cgraph edge statement index not found");
795 }
796 for (i = 0;
797 ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
798 i++)
799 if (ref->lto_stmt_uid)
800 {
801 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
802 fatal_error ("Reference statement index out of range");
803 ref->stmt = stmts[ref->lto_stmt_uid - 1];
804 if (!ref->stmt)
805 fatal_error ("Reference statement index not found");
806 }
807 }
808
809
810 /* Fixup call_stmt pointers in NODE and all clones. */
811
812 static void
813 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
814 {
815 struct cgraph_node *node;
816 struct function *fn;
817
818 while (orig->clone_of)
819 orig = orig->clone_of;
820 fn = DECL_STRUCT_FUNCTION (orig->decl);
821
822 fixup_call_stmt_edges_1 (orig, stmts, fn);
823 if (orig->clones)
824 for (node = orig->clones; node != orig;)
825 {
826 fixup_call_stmt_edges_1 (node, stmts, fn);
827 if (node->clones)
828 node = node->clones;
829 else if (node->next_sibling_clone)
830 node = node->next_sibling_clone;
831 else
832 {
833 while (node != orig && !node->next_sibling_clone)
834 node = node->clone_of;
835 if (node != orig)
836 node = node->next_sibling_clone;
837 }
838 }
839 }
840
841
842 /* Input the base body of struct function FN from DATA_IN
843 using input block IB. */
844
845 static void
846 input_struct_function_base (struct function *fn, struct data_in *data_in,
847 struct lto_input_block *ib)
848 {
849 struct bitpack_d bp;
850 int len;
851
852 /* Read the static chain and non-local goto save area. */
853 fn->static_chain_decl = stream_read_tree (ib, data_in);
854 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
855
856 /* Read all the local symbols. */
857 len = streamer_read_hwi (ib);
858 if (len > 0)
859 {
860 int i;
861 vec_safe_grow_cleared (fn->local_decls, len);
862 for (i = 0; i < len; i++)
863 {
864 tree t = stream_read_tree (ib, data_in);
865 (*fn->local_decls)[i] = t;
866 }
867 }
868
869 /* Input the current IL state of the function. */
870 fn->curr_properties = streamer_read_uhwi (ib);
871
872 /* Read all the attributes for FN. */
873 bp = streamer_read_bitpack (ib);
874 fn->is_thunk = bp_unpack_value (&bp, 1);
875 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
876 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
877 fn->returns_struct = bp_unpack_value (&bp, 1);
878 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
879 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
880 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
881 fn->after_inlining = bp_unpack_value (&bp, 1);
882 fn->stdarg = bp_unpack_value (&bp, 1);
883 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
884 fn->calls_alloca = bp_unpack_value (&bp, 1);
885 fn->calls_setjmp = bp_unpack_value (&bp, 1);
886 fn->has_force_vect_loops = bp_unpack_value (&bp, 1);
887 fn->has_simduid_loops = bp_unpack_value (&bp, 1);
888 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
889 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
890
891 /* Input the function start and end loci. */
892 fn->function_start_locus = stream_input_location (&bp, data_in);
893 fn->function_end_locus = stream_input_location (&bp, data_in);
894 }
895
896
897 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
898
899 static void
900 input_function (tree fn_decl, struct data_in *data_in,
901 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
902 {
903 struct function *fn;
904 enum LTO_tags tag;
905 gimple *stmts;
906 basic_block bb;
907 struct cgraph_node *node;
908
909 tag = streamer_read_record_start (ib);
910 lto_tag_check (tag, LTO_function);
911
912 /* Read decls for parameters and args. */
913 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
914 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
915
916 /* Read the tree of lexical scopes for the function. */
917 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
918
919 if (!streamer_read_uhwi (ib))
920 return;
921
922 push_struct_function (fn_decl);
923 fn = DECL_STRUCT_FUNCTION (fn_decl);
924 init_tree_ssa (fn);
925 /* We input IL in SSA form. */
926 cfun->gimple_df->in_ssa_p = true;
927
928 gimple_register_cfg_hooks ();
929
930 node = cgraph_get_node (fn_decl);
931 if (!node)
932 node = cgraph_create_node (fn_decl);
933 input_struct_function_base (fn, data_in, ib);
934 input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
935
936 /* Read all the SSA names. */
937 input_ssa_names (ib, data_in, fn);
938
939 /* Read the exception handling regions in the function. */
940 input_eh_regions (ib, data_in, fn);
941
942 gcc_assert (DECL_INITIAL (fn_decl));
943 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
944
945 /* Read all the basic blocks. */
946 tag = streamer_read_record_start (ib);
947 while (tag)
948 {
949 input_bb (ib, tag, data_in, fn,
950 node->count_materialization_scale);
951 tag = streamer_read_record_start (ib);
952 }
953
954 /* Fix up the call statements that are mentioned in the callgraph
955 edges. */
956 set_gimple_stmt_max_uid (cfun, 0);
957 FOR_ALL_BB (bb)
958 {
959 gimple_stmt_iterator gsi;
960 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
961 {
962 gimple stmt = gsi_stmt (gsi);
963 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
964 }
965 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
966 {
967 gimple stmt = gsi_stmt (gsi);
968 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
969 }
970 }
971 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
972 FOR_ALL_BB (bb)
973 {
974 gimple_stmt_iterator bsi = gsi_start_phis (bb);
975 while (!gsi_end_p (bsi))
976 {
977 gimple stmt = gsi_stmt (bsi);
978 gsi_next (&bsi);
979 stmts[gimple_uid (stmt)] = stmt;
980 }
981 bsi = gsi_start_bb (bb);
982 while (!gsi_end_p (bsi))
983 {
984 gimple stmt = gsi_stmt (bsi);
985 /* If we're recompiling LTO objects with debug stmts but
986 we're not supposed to have debug stmts, remove them now.
987 We can't remove them earlier because this would cause uid
988 mismatches in fixups, but we can do it at this point, as
989 long as debug stmts don't require fixups. */
990 if (!MAY_HAVE_DEBUG_STMTS && is_gimple_debug (stmt))
991 {
992 gimple_stmt_iterator gsi = bsi;
993 gsi_next (&bsi);
994 gsi_remove (&gsi, true);
995 }
996 else
997 {
998 gsi_next (&bsi);
999 stmts[gimple_uid (stmt)] = stmt;
1000 }
1001 }
1002 }
1003
1004 /* Set the gimple body to the statement sequence in the entry
1005 basic block. FIXME lto, this is fairly hacky. The existence
1006 of a gimple body is used by the cgraph routines, but we should
1007 really use the presence of the CFG. */
1008 {
1009 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1010 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1011 }
1012
1013 fixup_call_stmt_edges (node, stmts);
1014 execute_all_ipa_stmt_fixups (node, stmts);
1015
1016 update_ssa (TODO_update_ssa_only_virtuals);
1017 free_dominance_info (CDI_DOMINATORS);
1018 free_dominance_info (CDI_POST_DOMINATORS);
1019 free (stmts);
1020 pop_cfun ();
1021 }
1022
1023
1024 /* Read the body from DATA for function NODE and fill it in.
1025 FILE_DATA are the global decls and types. SECTION_TYPE is either
1026 LTO_section_function_body or LTO_section_static_initializer. If
1027 section type is LTO_section_function_body, FN must be the decl for
1028 that function. */
1029
1030 static void
1031 lto_read_body (struct lto_file_decl_data *file_data, struct cgraph_node *node,
1032 const char *data, enum lto_section_type section_type)
1033 {
1034 const struct lto_function_header *header;
1035 struct data_in *data_in;
1036 int cfg_offset;
1037 int main_offset;
1038 int string_offset;
1039 struct lto_input_block ib_cfg;
1040 struct lto_input_block ib_main;
1041 tree fn_decl = node->decl;
1042
1043 header = (const struct lto_function_header *) data;
1044 cfg_offset = sizeof (struct lto_function_header);
1045 main_offset = cfg_offset + header->cfg_size;
1046 string_offset = main_offset + header->main_size;
1047
1048 LTO_INIT_INPUT_BLOCK (ib_cfg,
1049 data + cfg_offset,
1050 0,
1051 header->cfg_size);
1052
1053 LTO_INIT_INPUT_BLOCK (ib_main,
1054 data + main_offset,
1055 0,
1056 header->main_size);
1057
1058 data_in = lto_data_in_create (file_data, data + string_offset,
1059 header->string_size, vNULL);
1060
1061 /* Make sure the file was generated by the exact same compiler. */
1062 lto_check_version (header->lto_header.major_version,
1063 header->lto_header.minor_version);
1064
1065 if (section_type == LTO_section_function_body)
1066 {
1067 struct lto_in_decl_state *decl_state;
1068 unsigned from;
1069
1070 gcc_checking_assert (node);
1071
1072 /* Use the function's decl state. */
1073 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1074 gcc_assert (decl_state);
1075 file_data->current_decl_state = decl_state;
1076
1077
1078 /* Set up the struct function. */
1079 from = data_in->reader_cache->nodes.length ();
1080 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1081 /* And fixup types we streamed locally. */
1082 {
1083 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1084 unsigned len = cache->nodes.length ();
1085 unsigned i;
1086 for (i = len; i-- > from;)
1087 {
1088 tree t = streamer_tree_cache_get_tree (cache, i);
1089 if (t == NULL_TREE)
1090 continue;
1091
1092 if (TYPE_P (t))
1093 {
1094 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1095 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1096 if (TYPE_MAIN_VARIANT (t) != t)
1097 {
1098 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1099 TYPE_NEXT_VARIANT (t)
1100 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1101 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1102 }
1103 }
1104 }
1105 }
1106
1107 /* Restore decl state */
1108 file_data->current_decl_state = file_data->global_decl_state;
1109 }
1110
1111 lto_data_in_delete (data_in);
1112 }
1113
1114
1115 /* Read the body of NODE using DATA. FILE_DATA holds the global
1116 decls and types. */
1117
1118 void
1119 lto_input_function_body (struct lto_file_decl_data *file_data,
1120 struct cgraph_node *node, const char *data)
1121 {
1122 lto_read_body (file_data, node, data, LTO_section_function_body);
1123 }
1124
1125
1126 /* Read the physical representation of a tree node EXPR from
1127 input block IB using the per-file context in DATA_IN. */
1128
1129 static void
1130 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1131 {
1132 /* Read all the bitfield values in EXPR. Note that for LTO, we
1133 only write language-independent bitfields, so no more unpacking is
1134 needed. */
1135 streamer_read_tree_bitfields (ib, data_in, expr);
1136
1137 /* Read all the pointer fields in EXPR. */
1138 streamer_read_tree_body (ib, data_in, expr);
1139
1140 /* Read any LTO-specific data not read by the tree streamer. */
1141 if (DECL_P (expr)
1142 && TREE_CODE (expr) != FUNCTION_DECL
1143 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1144 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1145
1146 /* We should never try to instantiate an MD or NORMAL builtin here. */
1147 if (TREE_CODE (expr) == FUNCTION_DECL)
1148 gcc_assert (!streamer_handle_as_builtin_p (expr));
1149
1150 #ifdef LTO_STREAMER_DEBUG
1151 /* Remove the mapping to RESULT's original address set by
1152 streamer_alloc_tree. */
1153 lto_orig_address_remove (expr);
1154 #endif
1155 }
1156
1157 /* Read the physical representation of a tree node with tag TAG from
1158 input block IB using the per-file context in DATA_IN. */
1159
1160 static tree
1161 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1162 enum LTO_tags tag, hashval_t hash)
1163 {
1164 /* Instantiate a new tree node. */
1165 tree result = streamer_alloc_tree (ib, data_in, tag);
1166
1167 /* Enter RESULT in the reader cache. This will make RESULT
1168 available so that circular references in the rest of the tree
1169 structure can be resolved in subsequent calls to stream_read_tree. */
1170 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1171
1172 lto_read_tree_1 (ib, data_in, result);
1173
1174 /* end_marker = */ streamer_read_uchar (ib);
1175
1176 return result;
1177 }
1178
1179
1180 /* Populate the reader cache with trees materialized from the SCC
1181 following in the IB, DATA_IN stream. */
1182
1183 hashval_t
1184 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1185 unsigned *len, unsigned *entry_len)
1186 {
1187 /* A blob of unnamed tree nodes, fill the cache from it and
1188 recurse. */
1189 unsigned size = streamer_read_uhwi (ib);
1190 hashval_t scc_hash = streamer_read_uhwi (ib);
1191 unsigned scc_entry_len = 1;
1192
1193 if (size == 1)
1194 {
1195 enum LTO_tags tag = streamer_read_record_start (ib);
1196 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1197 }
1198 else
1199 {
1200 unsigned int first = data_in->reader_cache->nodes.length ();
1201 tree result;
1202
1203 scc_entry_len = streamer_read_uhwi (ib);
1204
1205 /* Materialize size trees by reading their headers. */
1206 for (unsigned i = 0; i < size; ++i)
1207 {
1208 enum LTO_tags tag = streamer_read_record_start (ib);
1209 if (tag == LTO_null
1210 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1211 || tag == LTO_tree_pickle_reference
1212 || tag == LTO_builtin_decl
1213 || tag == LTO_integer_cst
1214 || tag == LTO_tree_scc)
1215 gcc_unreachable ();
1216
1217 result = streamer_alloc_tree (ib, data_in, tag);
1218 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1219 }
1220
1221 /* Read the tree bitpacks and references. */
1222 for (unsigned i = 0; i < size; ++i)
1223 {
1224 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1225 first + i);
1226 lto_read_tree_1 (ib, data_in, result);
1227 /* end_marker = */ streamer_read_uchar (ib);
1228 }
1229 }
1230
1231 *len = size;
1232 *entry_len = scc_entry_len;
1233 return scc_hash;
1234 }
1235
1236
1237 /* Read a tree from input block IB using the per-file context in
1238 DATA_IN. This context is used, for example, to resolve references
1239 to previously read nodes. */
1240
1241 tree
1242 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1243 enum LTO_tags tag, hashval_t hash)
1244 {
1245 tree result;
1246
1247 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1248
1249 if (tag == LTO_null)
1250 result = NULL_TREE;
1251 else if (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1252 {
1253 /* If TAG is a reference to an indexable tree, the next value
1254 in IB is the index into the table where we expect to find
1255 that tree. */
1256 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1257 }
1258 else if (tag == LTO_tree_pickle_reference)
1259 {
1260 /* If TAG is a reference to a previously read tree, look it up in
1261 the reader cache. */
1262 result = streamer_get_pickled_tree (ib, data_in);
1263 }
1264 else if (tag == LTO_builtin_decl)
1265 {
1266 /* If we are going to read a built-in function, all we need is
1267 the code and class. */
1268 result = streamer_get_builtin_tree (ib, data_in);
1269 }
1270 else if (tag == LTO_integer_cst)
1271 {
1272 /* For shared integer constants in singletons we can use the existing
1273 tree integer constant merging code. */
1274 tree type = stream_read_tree (ib, data_in);
1275 unsigned HOST_WIDE_INT low = streamer_read_uhwi (ib);
1276 HOST_WIDE_INT high = streamer_read_hwi (ib);
1277 result = build_int_cst_wide (type, low, high);
1278 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1279 }
1280 else if (tag == LTO_tree_scc)
1281 {
1282 unsigned len, entry_len;
1283
1284 /* Input and skip the SCC. */
1285 lto_input_scc (ib, data_in, &len, &entry_len);
1286
1287 /* Recurse. */
1288 return lto_input_tree (ib, data_in);
1289 }
1290 else
1291 {
1292 /* Otherwise, materialize a new node from IB. */
1293 result = lto_read_tree (ib, data_in, tag, hash);
1294 }
1295
1296 return result;
1297 }
1298
1299 tree
1300 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1301 {
1302 return lto_input_tree_1 (ib, data_in, streamer_read_record_start (ib), 0);
1303 }
1304
1305
1306 /* Input toplevel asms. */
1307
1308 void
1309 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1310 {
1311 size_t len;
1312 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1313 NULL, &len);
1314 const struct lto_asm_header *header = (const struct lto_asm_header *) data;
1315 int string_offset;
1316 struct data_in *data_in;
1317 struct lto_input_block ib;
1318 tree str;
1319
1320 if (! data)
1321 return;
1322
1323 string_offset = sizeof (*header) + header->main_size;
1324
1325 LTO_INIT_INPUT_BLOCK (ib,
1326 data + sizeof (*header),
1327 0,
1328 header->main_size);
1329
1330 data_in = lto_data_in_create (file_data, data + string_offset,
1331 header->string_size, vNULL);
1332
1333 /* Make sure the file was generated by the exact same compiler. */
1334 lto_check_version (header->lto_header.major_version,
1335 header->lto_header.minor_version);
1336
1337 while ((str = streamer_read_string_cst (data_in, &ib)))
1338 {
1339 struct asm_node *node = add_asm_node (str);
1340 node->order = streamer_read_hwi (&ib) + order_base;
1341 if (node->order >= symtab_order)
1342 symtab_order = node->order + 1;
1343 }
1344
1345 lto_data_in_delete (data_in);
1346
1347 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1348 }
1349
1350
1351 /* Initialization for the LTO reader. */
1352
1353 void
1354 lto_reader_init (void)
1355 {
1356 lto_streamer_init ();
1357 file_name_hash_table.create (37);
1358 }
1359
1360
1361 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1362 table to use with LEN strings. RESOLUTIONS is the vector of linker
1363 resolutions (NULL if not using a linker plugin). */
1364
1365 struct data_in *
1366 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1367 unsigned len,
1368 vec<ld_plugin_symbol_resolution_t> resolutions)
1369 {
1370 struct data_in *data_in = XCNEW (struct data_in);
1371 data_in->file_data = file_data;
1372 data_in->strings = strings;
1373 data_in->strings_len = len;
1374 data_in->globals_resolution = resolutions;
1375 data_in->reader_cache = streamer_tree_cache_create (false, false);
1376
1377 return data_in;
1378 }
1379
1380
1381 /* Remove DATA_IN. */
1382
1383 void
1384 lto_data_in_delete (struct data_in *data_in)
1385 {
1386 data_in->globals_resolution.release ();
1387 streamer_tree_cache_delete (data_in->reader_cache);
1388 free (data_in->labels);
1389 free (data_in);
1390 }