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