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