decl.c (value_annotation_hasher::handle_cache_entry): Delete.
[gcc.git] / gcc / lto-streamer-in.c
1 /* Read the GIMPLE representation from a file stream.
2
3 Copyright (C) 2009-2015 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 "alias.h"
29 #include "symtab.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "stringpool.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "rtl.h"
36 #include "flags.h"
37 #include "insn-config.h"
38 #include "expmed.h"
39 #include "dojump.h"
40 #include "explow.h"
41 #include "calls.h"
42 #include "emit-rtl.h"
43 #include "varasm.h"
44 #include "stmt.h"
45 #include "expr.h"
46 #include "params.h"
47 #include "predict.h"
48 #include "dominance.h"
49 #include "cfg.h"
50 #include "basic-block.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "gimple-expr.h"
54 #include "gimple.h"
55 #include "gimple-iterator.h"
56 #include "gimple-ssa.h"
57 #include "tree-cfg.h"
58 #include "tree-ssanames.h"
59 #include "tree-into-ssa.h"
60 #include "tree-dfa.h"
61 #include "tree-ssa.h"
62 #include "tree-pass.h"
63 #include "diagnostic.h"
64 #include "except.h"
65 #include "debug.h"
66 #include "plugin-api.h"
67 #include "ipa-ref.h"
68 #include "cgraph.h"
69 #include "ipa-utils.h"
70 #include "data-streamer.h"
71 #include "gimple-streamer.h"
72 #include "lto-streamer.h"
73 #include "tree-streamer.h"
74 #include "streamer-hooks.h"
75 #include "cfgloop.h"
76
77
78 struct freeing_string_slot_hasher : string_slot_hasher
79 {
80 static inline void remove (value_type *);
81 };
82
83 inline void
84 freeing_string_slot_hasher::remove (value_type *v)
85 {
86 free (v);
87 }
88
89 /* The table to hold the file names. */
90 static hash_table<freeing_string_slot_hasher> *file_name_hash_table;
91
92
93 /* Check that tag ACTUAL has one of the given values. NUM_TAGS is the
94 number of valid tag values to check. */
95
96 void
97 lto_tag_check_set (enum LTO_tags actual, int ntags, ...)
98 {
99 va_list ap;
100 int i;
101
102 va_start (ap, ntags);
103 for (i = 0; i < ntags; i++)
104 if ((unsigned) actual == va_arg (ap, unsigned))
105 {
106 va_end (ap);
107 return;
108 }
109
110 va_end (ap);
111 internal_error ("bytecode stream: unexpected tag %s", lto_tag_name (actual));
112 }
113
114
115 /* Read LENGTH bytes from STREAM to ADDR. */
116
117 void
118 lto_input_data_block (struct lto_input_block *ib, void *addr, size_t length)
119 {
120 size_t i;
121 unsigned char *const buffer = (unsigned char *const) addr;
122
123 for (i = 0; i < length; i++)
124 buffer[i] = streamer_read_uchar (ib);
125 }
126
127
128 /* Lookup STRING in file_name_hash_table. If found, return the existing
129 string, otherwise insert STRING as the canonical version. */
130
131 static const char *
132 canon_file_name (const char *string)
133 {
134 string_slot **slot;
135 struct string_slot s_slot;
136 size_t len = strlen (string);
137
138 s_slot.s = string;
139 s_slot.len = len;
140
141 slot = file_name_hash_table->find_slot (&s_slot, INSERT);
142 if (*slot == NULL)
143 {
144 char *saved_string;
145 struct string_slot *new_slot;
146
147 saved_string = (char *) xmalloc (len + 1);
148 new_slot = XCNEW (struct string_slot);
149 memcpy (saved_string, string, len + 1);
150 new_slot->s = saved_string;
151 new_slot->len = len;
152 *slot = new_slot;
153 return saved_string;
154 }
155 else
156 {
157 struct string_slot *old_slot = *slot;
158 return old_slot->s;
159 }
160 }
161
162 /* Pointer to currently alive instance of lto_location_cache. */
163
164 lto_location_cache *lto_location_cache::current_cache;
165
166 /* Sort locations in source order. Start with file from last application. */
167
168 int
169 lto_location_cache::cmp_loc (const void *pa, const void *pb)
170 {
171 const cached_location *a = ((const cached_location *)pa);
172 const cached_location *b = ((const cached_location *)pb);
173 const char *current_file = current_cache->current_file;
174 int current_line = current_cache->current_line;
175
176 if (a->file == current_file && b->file != current_file)
177 return -1;
178 if (a->file != current_file && b->file == current_file)
179 return 1;
180 if (a->file == current_file && b->file == current_file)
181 {
182 if (a->line == current_line && b->line != current_line)
183 return -1;
184 if (a->line != current_line && b->line == current_line)
185 return 1;
186 }
187 if (a->file != b->file)
188 return strcmp (a->file, b->file);
189 if (a->line != b->line)
190 return a->line - b->line;
191 return a->col - b->col;
192 }
193
194 /* Apply all changes in location cache. Add locations into linemap and patch
195 trees. */
196
197 bool
198 lto_location_cache::apply_location_cache ()
199 {
200 static const char *prev_file;
201 if (!loc_cache.length ())
202 return false;
203 if (loc_cache.length () > 1)
204 loc_cache.qsort (cmp_loc);
205
206 for (unsigned int i = 0; i < loc_cache.length (); i++)
207 {
208 struct cached_location loc = loc_cache[i];
209
210 if (current_file != loc.file)
211 linemap_add (line_table, prev_file ? LC_RENAME : LC_ENTER,
212 false, loc.file, loc.line);
213 else if (current_line != loc.line)
214 {
215 int max = loc.col;
216
217 for (unsigned int j = i + 1; j < loc_cache.length (); j++)
218 if (loc.file != loc_cache[j].file
219 || loc.line != loc_cache[j].line)
220 break;
221 else if (max < loc_cache[j].col)
222 max = loc_cache[j].col;
223 linemap_line_start (line_table, loc.line, max + 1);
224 }
225 gcc_assert (*loc.loc == BUILTINS_LOCATION + 1);
226 if (current_file == loc.file && current_line == loc.line
227 && current_col == loc.col)
228 *loc.loc = current_loc;
229 else
230 current_loc = *loc.loc = linemap_position_for_column (line_table,
231 loc.col);
232 current_line = loc.line;
233 prev_file = current_file = loc.file;
234 current_col = loc.col;
235 }
236 loc_cache.truncate (0);
237 accepted_length = 0;
238 return true;
239 }
240
241 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
242
243 void
244 lto_location_cache::accept_location_cache ()
245 {
246 gcc_assert (current_cache == this);
247 accepted_length = loc_cache.length ();
248 }
249
250 /* Tree merging did suceed; throw away recent changes. */
251
252 void
253 lto_location_cache::revert_location_cache ()
254 {
255 loc_cache.truncate (accepted_length);
256 }
257
258 /* Read a location bitpack from input block IB and either update *LOC directly
259 or add it to the location cache.
260 It is neccesary to call apply_location_cache to get *LOC updated. */
261
262 void
263 lto_location_cache::input_location (location_t *loc, struct bitpack_d *bp,
264 struct data_in *data_in)
265 {
266 static const char *stream_file;
267 static int stream_line;
268 static int stream_col;
269 bool file_change, line_change, column_change;
270
271 gcc_assert (current_cache == this);
272
273 *loc = bp_unpack_int_in_range (bp, "location", 0, RESERVED_LOCATION_COUNT);
274
275 if (*loc < RESERVED_LOCATION_COUNT)
276 return;
277
278 /* Keep value RESERVED_LOCATION_COUNT in *loc as linemap lookups will
279 ICE on it. */
280
281 file_change = bp_unpack_value (bp, 1);
282 line_change = bp_unpack_value (bp, 1);
283 column_change = bp_unpack_value (bp, 1);
284
285 if (file_change)
286 stream_file = canon_file_name (bp_unpack_string (data_in, bp));
287
288 if (line_change)
289 stream_line = bp_unpack_var_len_unsigned (bp);
290
291 if (column_change)
292 stream_col = bp_unpack_var_len_unsigned (bp);
293
294 /* This optimization saves location cache operations druing gimple
295 streaming. */
296
297 if (current_file == stream_file && current_line == stream_line
298 && current_col == stream_col)
299 {
300 *loc = current_loc;
301 return;
302 }
303
304 struct cached_location entry = {stream_file, loc, stream_line, stream_col};
305 loc_cache.safe_push (entry);
306 }
307
308 /* Read a location bitpack from input block IB and either update *LOC directly
309 or add it to the location cache.
310 It is neccesary to call apply_location_cache to get *LOC updated. */
311
312 void
313 lto_input_location (location_t *loc, struct bitpack_d *bp,
314 struct data_in *data_in)
315 {
316 data_in->location_cache.input_location (loc, bp, data_in);
317 }
318
319 /* Read location and return it instead of going through location caching.
320 This should be used only when the resulting location is not going to be
321 discarded. */
322
323 location_t
324 stream_input_location_now (struct bitpack_d *bp, struct data_in *data_in)
325 {
326 location_t loc;
327 stream_input_location (&loc, bp, data_in);
328 data_in->location_cache.apply_location_cache ();
329 return loc;
330 }
331
332 /* Read a reference to a tree node from DATA_IN using input block IB.
333 TAG is the expected node that should be found in IB, if TAG belongs
334 to one of the indexable trees, expect to read a reference index to
335 be looked up in one of the symbol tables, otherwise read the pysical
336 representation of the tree using stream_read_tree. FN is the
337 function scope for the read tree. */
338
339 tree
340 lto_input_tree_ref (struct lto_input_block *ib, struct data_in *data_in,
341 struct function *fn, enum LTO_tags tag)
342 {
343 unsigned HOST_WIDE_INT ix_u;
344 tree result = NULL_TREE;
345
346 lto_tag_check_range (tag, LTO_field_decl_ref, LTO_namelist_decl_ref);
347
348 switch (tag)
349 {
350 case LTO_type_ref:
351 ix_u = streamer_read_uhwi (ib);
352 result = lto_file_decl_data_get_type (data_in->file_data, ix_u);
353 break;
354
355 case LTO_ssa_name_ref:
356 ix_u = streamer_read_uhwi (ib);
357 result = (*SSANAMES (fn))[ix_u];
358 break;
359
360 case LTO_field_decl_ref:
361 ix_u = streamer_read_uhwi (ib);
362 result = lto_file_decl_data_get_field_decl (data_in->file_data, ix_u);
363 break;
364
365 case LTO_function_decl_ref:
366 ix_u = streamer_read_uhwi (ib);
367 result = lto_file_decl_data_get_fn_decl (data_in->file_data, ix_u);
368 break;
369
370 case LTO_type_decl_ref:
371 ix_u = streamer_read_uhwi (ib);
372 result = lto_file_decl_data_get_type_decl (data_in->file_data, ix_u);
373 break;
374
375 case LTO_namespace_decl_ref:
376 ix_u = streamer_read_uhwi (ib);
377 result = lto_file_decl_data_get_namespace_decl (data_in->file_data, ix_u);
378 break;
379
380 case LTO_global_decl_ref:
381 case LTO_result_decl_ref:
382 case LTO_const_decl_ref:
383 case LTO_imported_decl_ref:
384 case LTO_label_decl_ref:
385 case LTO_translation_unit_decl_ref:
386 case LTO_namelist_decl_ref:
387 ix_u = streamer_read_uhwi (ib);
388 result = lto_file_decl_data_get_var_decl (data_in->file_data, ix_u);
389 break;
390
391 default:
392 gcc_unreachable ();
393 }
394
395 gcc_assert (result);
396
397 return result;
398 }
399
400
401 /* Read and return a double-linked list of catch handlers from input
402 block IB, using descriptors in DATA_IN. */
403
404 static struct eh_catch_d *
405 lto_input_eh_catch_list (struct lto_input_block *ib, struct data_in *data_in,
406 eh_catch *last_p)
407 {
408 eh_catch first;
409 enum LTO_tags tag;
410
411 *last_p = first = NULL;
412 tag = streamer_read_record_start (ib);
413 while (tag)
414 {
415 tree list;
416 eh_catch n;
417
418 lto_tag_check_range (tag, LTO_eh_catch, LTO_eh_catch);
419
420 /* Read the catch node. */
421 n = ggc_cleared_alloc<eh_catch_d> ();
422 n->type_list = stream_read_tree (ib, data_in);
423 n->filter_list = stream_read_tree (ib, data_in);
424 n->label = stream_read_tree (ib, data_in);
425
426 /* Register all the types in N->FILTER_LIST. */
427 for (list = n->filter_list; list; list = TREE_CHAIN (list))
428 add_type_for_runtime (TREE_VALUE (list));
429
430 /* Chain N to the end of the list. */
431 if (*last_p)
432 (*last_p)->next_catch = n;
433 n->prev_catch = *last_p;
434 *last_p = n;
435
436 /* Set the head of the list the first time through the loop. */
437 if (first == NULL)
438 first = n;
439
440 tag = streamer_read_record_start (ib);
441 }
442
443 return first;
444 }
445
446
447 /* Read and return EH region IX from input block IB, using descriptors
448 in DATA_IN. */
449
450 static eh_region
451 input_eh_region (struct lto_input_block *ib, struct data_in *data_in, int ix)
452 {
453 enum LTO_tags tag;
454 eh_region r;
455
456 /* Read the region header. */
457 tag = streamer_read_record_start (ib);
458 if (tag == LTO_null)
459 return NULL;
460
461 r = ggc_cleared_alloc<eh_region_d> ();
462 r->index = streamer_read_hwi (ib);
463
464 gcc_assert (r->index == ix);
465
466 /* Read all the region pointers as region numbers. We'll fix up
467 the pointers once the whole array has been read. */
468 r->outer = (eh_region) (intptr_t) streamer_read_hwi (ib);
469 r->inner = (eh_region) (intptr_t) streamer_read_hwi (ib);
470 r->next_peer = (eh_region) (intptr_t) streamer_read_hwi (ib);
471
472 switch (tag)
473 {
474 case LTO_ert_cleanup:
475 r->type = ERT_CLEANUP;
476 break;
477
478 case LTO_ert_try:
479 {
480 struct eh_catch_d *last_catch;
481 r->type = ERT_TRY;
482 r->u.eh_try.first_catch = lto_input_eh_catch_list (ib, data_in,
483 &last_catch);
484 r->u.eh_try.last_catch = last_catch;
485 break;
486 }
487
488 case LTO_ert_allowed_exceptions:
489 {
490 tree l;
491
492 r->type = ERT_ALLOWED_EXCEPTIONS;
493 r->u.allowed.type_list = stream_read_tree (ib, data_in);
494 r->u.allowed.label = stream_read_tree (ib, data_in);
495 r->u.allowed.filter = streamer_read_uhwi (ib);
496
497 for (l = r->u.allowed.type_list; l ; l = TREE_CHAIN (l))
498 add_type_for_runtime (TREE_VALUE (l));
499 }
500 break;
501
502 case LTO_ert_must_not_throw:
503 {
504 r->type = ERT_MUST_NOT_THROW;
505 r->u.must_not_throw.failure_decl = stream_read_tree (ib, data_in);
506 bitpack_d bp = streamer_read_bitpack (ib);
507 r->u.must_not_throw.failure_loc
508 = stream_input_location_now (&bp, data_in);
509 }
510 break;
511
512 default:
513 gcc_unreachable ();
514 }
515
516 r->landing_pads = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
517
518 return r;
519 }
520
521
522 /* Read and return EH landing pad IX from input block IB, using descriptors
523 in DATA_IN. */
524
525 static eh_landing_pad
526 input_eh_lp (struct lto_input_block *ib, struct data_in *data_in, int ix)
527 {
528 enum LTO_tags tag;
529 eh_landing_pad lp;
530
531 /* Read the landing pad header. */
532 tag = streamer_read_record_start (ib);
533 if (tag == LTO_null)
534 return NULL;
535
536 lto_tag_check_range (tag, LTO_eh_landing_pad, LTO_eh_landing_pad);
537
538 lp = ggc_cleared_alloc<eh_landing_pad_d> ();
539 lp->index = streamer_read_hwi (ib);
540 gcc_assert (lp->index == ix);
541 lp->next_lp = (eh_landing_pad) (intptr_t) streamer_read_hwi (ib);
542 lp->region = (eh_region) (intptr_t) streamer_read_hwi (ib);
543 lp->post_landing_pad = stream_read_tree (ib, data_in);
544
545 return lp;
546 }
547
548
549 /* After reading the EH regions, pointers to peer and children regions
550 are region numbers. This converts all these region numbers into
551 real pointers into the rematerialized regions for FN. ROOT_REGION
552 is the region number for the root EH region in FN. */
553
554 static void
555 fixup_eh_region_pointers (struct function *fn, HOST_WIDE_INT root_region)
556 {
557 unsigned i;
558 vec<eh_region, va_gc> *eh_array = fn->eh->region_array;
559 vec<eh_landing_pad, va_gc> *lp_array = fn->eh->lp_array;
560 eh_region r;
561 eh_landing_pad lp;
562
563 gcc_assert (eh_array && lp_array);
564
565 gcc_assert (root_region >= 0);
566 fn->eh->region_tree = (*eh_array)[root_region];
567
568 #define FIXUP_EH_REGION(r) (r) = (*eh_array)[(HOST_WIDE_INT) (intptr_t) (r)]
569 #define FIXUP_EH_LP(p) (p) = (*lp_array)[(HOST_WIDE_INT) (intptr_t) (p)]
570
571 /* Convert all the index numbers stored in pointer fields into
572 pointers to the corresponding slots in the EH region array. */
573 FOR_EACH_VEC_ELT (*eh_array, i, r)
574 {
575 /* The array may contain NULL regions. */
576 if (r == NULL)
577 continue;
578
579 gcc_assert (i == (unsigned) r->index);
580 FIXUP_EH_REGION (r->outer);
581 FIXUP_EH_REGION (r->inner);
582 FIXUP_EH_REGION (r->next_peer);
583 FIXUP_EH_LP (r->landing_pads);
584 }
585
586 /* Convert all the index numbers stored in pointer fields into
587 pointers to the corresponding slots in the EH landing pad array. */
588 FOR_EACH_VEC_ELT (*lp_array, i, lp)
589 {
590 /* The array may contain NULL landing pads. */
591 if (lp == NULL)
592 continue;
593
594 gcc_assert (i == (unsigned) lp->index);
595 FIXUP_EH_LP (lp->next_lp);
596 FIXUP_EH_REGION (lp->region);
597 }
598
599 #undef FIXUP_EH_REGION
600 #undef FIXUP_EH_LP
601 }
602
603
604 /* Initialize EH support. */
605
606 void
607 lto_init_eh (void)
608 {
609 static bool eh_initialized_p = false;
610
611 if (eh_initialized_p)
612 return;
613
614 /* Contrary to most other FEs, we only initialize EH support when at
615 least one of the files in the set contains exception regions in
616 it. Since this happens much later than the call to init_eh in
617 lang_dependent_init, we have to set flag_exceptions and call
618 init_eh again to initialize the EH tables. */
619 flag_exceptions = 1;
620 init_eh ();
621
622 eh_initialized_p = true;
623 }
624
625
626 /* Read the exception table for FN from IB using the data descriptors
627 in DATA_IN. */
628
629 static void
630 input_eh_regions (struct lto_input_block *ib, struct data_in *data_in,
631 struct function *fn)
632 {
633 HOST_WIDE_INT i, root_region, len;
634 enum LTO_tags tag;
635
636 tag = streamer_read_record_start (ib);
637 if (tag == LTO_null)
638 return;
639
640 lto_tag_check_range (tag, LTO_eh_table, LTO_eh_table);
641
642 /* If the file contains EH regions, then it was compiled with
643 -fexceptions. In that case, initialize the backend EH
644 machinery. */
645 lto_init_eh ();
646
647 gcc_assert (fn->eh);
648
649 root_region = streamer_read_hwi (ib);
650 gcc_assert (root_region == (int) root_region);
651
652 /* Read the EH region array. */
653 len = streamer_read_hwi (ib);
654 gcc_assert (len == (int) len);
655 if (len > 0)
656 {
657 vec_safe_grow_cleared (fn->eh->region_array, len);
658 for (i = 0; i < len; i++)
659 {
660 eh_region r = input_eh_region (ib, data_in, i);
661 (*fn->eh->region_array)[i] = r;
662 }
663 }
664
665 /* Read the landing pads. */
666 len = streamer_read_hwi (ib);
667 gcc_assert (len == (int) len);
668 if (len > 0)
669 {
670 vec_safe_grow_cleared (fn->eh->lp_array, len);
671 for (i = 0; i < len; i++)
672 {
673 eh_landing_pad lp = input_eh_lp (ib, data_in, i);
674 (*fn->eh->lp_array)[i] = lp;
675 }
676 }
677
678 /* Read the runtime type data. */
679 len = streamer_read_hwi (ib);
680 gcc_assert (len == (int) len);
681 if (len > 0)
682 {
683 vec_safe_grow_cleared (fn->eh->ttype_data, len);
684 for (i = 0; i < len; i++)
685 {
686 tree ttype = stream_read_tree (ib, data_in);
687 (*fn->eh->ttype_data)[i] = ttype;
688 }
689 }
690
691 /* Read the table of action chains. */
692 len = streamer_read_hwi (ib);
693 gcc_assert (len == (int) len);
694 if (len > 0)
695 {
696 if (targetm.arm_eabi_unwinder)
697 {
698 vec_safe_grow_cleared (fn->eh->ehspec_data.arm_eabi, len);
699 for (i = 0; i < len; i++)
700 {
701 tree t = stream_read_tree (ib, data_in);
702 (*fn->eh->ehspec_data.arm_eabi)[i] = t;
703 }
704 }
705 else
706 {
707 vec_safe_grow_cleared (fn->eh->ehspec_data.other, len);
708 for (i = 0; i < len; i++)
709 {
710 uchar c = streamer_read_uchar (ib);
711 (*fn->eh->ehspec_data.other)[i] = c;
712 }
713 }
714 }
715
716 /* Reconstruct the EH region tree by fixing up the peer/children
717 pointers. */
718 fixup_eh_region_pointers (fn, root_region);
719
720 tag = streamer_read_record_start (ib);
721 lto_tag_check_range (tag, LTO_null, LTO_null);
722 }
723
724
725 /* Make a new basic block with index INDEX in function FN. */
726
727 static basic_block
728 make_new_block (struct function *fn, unsigned int index)
729 {
730 basic_block bb = alloc_block ();
731 bb->index = index;
732 SET_BASIC_BLOCK_FOR_FN (fn, index, bb);
733 n_basic_blocks_for_fn (fn)++;
734 return bb;
735 }
736
737
738 /* Read a wide-int. */
739
740 static widest_int
741 streamer_read_wi (struct lto_input_block *ib)
742 {
743 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
744 int i;
745 int prec ATTRIBUTE_UNUSED = streamer_read_uhwi (ib);
746 int len = streamer_read_uhwi (ib);
747 for (i = 0; i < len; i++)
748 a[i] = streamer_read_hwi (ib);
749 return widest_int::from_array (a, len);
750 }
751
752
753 /* Read the CFG for function FN from input block IB. */
754
755 static void
756 input_cfg (struct lto_input_block *ib, struct data_in *data_in,
757 struct function *fn,
758 int count_materialization_scale)
759 {
760 unsigned int bb_count;
761 basic_block p_bb;
762 unsigned int i;
763 int index;
764
765 init_empty_tree_cfg_for_function (fn);
766 init_ssa_operands (fn);
767
768 profile_status_for_fn (fn) = streamer_read_enum (ib, profile_status_d,
769 PROFILE_LAST);
770
771 bb_count = streamer_read_uhwi (ib);
772
773 last_basic_block_for_fn (fn) = bb_count;
774 if (bb_count > basic_block_info_for_fn (fn)->length ())
775 vec_safe_grow_cleared (basic_block_info_for_fn (fn), bb_count);
776
777 if (bb_count > label_to_block_map_for_fn (fn)->length ())
778 vec_safe_grow_cleared (label_to_block_map_for_fn (fn), bb_count);
779
780 index = streamer_read_hwi (ib);
781 while (index != -1)
782 {
783 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
784 unsigned int edge_count;
785
786 if (bb == NULL)
787 bb = make_new_block (fn, index);
788
789 edge_count = streamer_read_uhwi (ib);
790
791 /* Connect up the CFG. */
792 for (i = 0; i < edge_count; i++)
793 {
794 unsigned int dest_index;
795 unsigned int edge_flags;
796 basic_block dest;
797 int probability;
798 gcov_type count;
799 edge e;
800
801 dest_index = streamer_read_uhwi (ib);
802 probability = (int) streamer_read_hwi (ib);
803 count = apply_scale ((gcov_type) streamer_read_gcov_count (ib),
804 count_materialization_scale);
805 edge_flags = streamer_read_uhwi (ib);
806
807 dest = BASIC_BLOCK_FOR_FN (fn, dest_index);
808
809 if (dest == NULL)
810 dest = make_new_block (fn, dest_index);
811
812 e = make_edge (bb, dest, edge_flags);
813 e->probability = probability;
814 e->count = count;
815 }
816
817 index = streamer_read_hwi (ib);
818 }
819
820 p_bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
821 index = streamer_read_hwi (ib);
822 while (index != -1)
823 {
824 basic_block bb = BASIC_BLOCK_FOR_FN (fn, index);
825 bb->prev_bb = p_bb;
826 p_bb->next_bb = bb;
827 p_bb = bb;
828 index = streamer_read_hwi (ib);
829 }
830
831 /* ??? The cfgloop interface is tied to cfun. */
832 gcc_assert (cfun == fn);
833
834 /* Input the loop tree. */
835 unsigned n_loops = streamer_read_uhwi (ib);
836 if (n_loops == 0)
837 return;
838
839 struct loops *loops = ggc_cleared_alloc<struct loops> ();
840 init_loops_structure (fn, loops, n_loops);
841 set_loops_for_fn (fn, loops);
842
843 /* Input each loop and associate it with its loop header so
844 flow_loops_find can rebuild the loop tree. */
845 for (unsigned i = 1; i < n_loops; ++i)
846 {
847 int header_index = streamer_read_hwi (ib);
848 if (header_index == -1)
849 {
850 loops->larray->quick_push (NULL);
851 continue;
852 }
853
854 struct loop *loop = alloc_loop ();
855 loop->header = BASIC_BLOCK_FOR_FN (fn, header_index);
856 loop->header->loop_father = loop;
857
858 /* Read everything copy_loop_info copies. */
859 loop->estimate_state = streamer_read_enum (ib, loop_estimation, EST_LAST);
860 loop->any_upper_bound = streamer_read_hwi (ib);
861 if (loop->any_upper_bound)
862 loop->nb_iterations_upper_bound = streamer_read_wi (ib);
863 loop->any_estimate = streamer_read_hwi (ib);
864 if (loop->any_estimate)
865 loop->nb_iterations_estimate = streamer_read_wi (ib);
866
867 /* Read OMP SIMD related info. */
868 loop->safelen = streamer_read_hwi (ib);
869 loop->dont_vectorize = streamer_read_hwi (ib);
870 loop->force_vectorize = streamer_read_hwi (ib);
871 loop->simduid = stream_read_tree (ib, data_in);
872
873 place_new_loop (fn, loop);
874
875 /* flow_loops_find doesn't like loops not in the tree, hook them
876 all as siblings of the tree root temporarily. */
877 flow_loop_tree_node_add (loops->tree_root, loop);
878 }
879
880 /* Rebuild the loop tree. */
881 flow_loops_find (loops);
882 }
883
884
885 /* Read the SSA names array for function FN from DATA_IN using input
886 block IB. */
887
888 static void
889 input_ssa_names (struct lto_input_block *ib, struct data_in *data_in,
890 struct function *fn)
891 {
892 unsigned int i, size;
893
894 size = streamer_read_uhwi (ib);
895 init_ssanames (fn, size);
896
897 i = streamer_read_uhwi (ib);
898 while (i)
899 {
900 tree ssa_name, name;
901 bool is_default_def;
902
903 /* Skip over the elements that had been freed. */
904 while (SSANAMES (fn)->length () < i)
905 SSANAMES (fn)->quick_push (NULL_TREE);
906
907 is_default_def = (streamer_read_uchar (ib) != 0);
908 name = stream_read_tree (ib, data_in);
909 ssa_name = make_ssa_name_fn (fn, name, gimple_build_nop ());
910
911 if (is_default_def)
912 set_ssa_default_def (cfun, SSA_NAME_VAR (ssa_name), ssa_name);
913
914 i = streamer_read_uhwi (ib);
915 }
916 }
917
918
919 /* Go through all NODE edges and fixup call_stmt pointers
920 so they point to STMTS. */
921
922 static void
923 fixup_call_stmt_edges_1 (struct cgraph_node *node, gimple *stmts,
924 struct function *fn)
925 {
926 struct cgraph_edge *cedge;
927 struct ipa_ref *ref = NULL;
928 unsigned int i;
929
930 for (cedge = node->callees; cedge; cedge = cedge->next_callee)
931 {
932 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
933 fatal_error (input_location,
934 "Cgraph edge statement index out of range");
935 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
936 if (!cedge->call_stmt)
937 fatal_error (input_location,
938 "Cgraph edge statement index not found");
939 }
940 for (cedge = node->indirect_calls; cedge; cedge = cedge->next_callee)
941 {
942 if (gimple_stmt_max_uid (fn) < cedge->lto_stmt_uid)
943 fatal_error (input_location,
944 "Cgraph edge statement index out of range");
945 cedge->call_stmt = as_a <gcall *> (stmts[cedge->lto_stmt_uid - 1]);
946 if (!cedge->call_stmt)
947 fatal_error (input_location, "Cgraph edge statement index not found");
948 }
949 for (i = 0; node->iterate_reference (i, ref); i++)
950 if (ref->lto_stmt_uid)
951 {
952 if (gimple_stmt_max_uid (fn) < ref->lto_stmt_uid)
953 fatal_error (input_location,
954 "Reference statement index out of range");
955 ref->stmt = stmts[ref->lto_stmt_uid - 1];
956 if (!ref->stmt)
957 fatal_error (input_location, "Reference statement index not found");
958 }
959 }
960
961
962 /* Fixup call_stmt pointers in NODE and all clones. */
963
964 static void
965 fixup_call_stmt_edges (struct cgraph_node *orig, gimple *stmts)
966 {
967 struct cgraph_node *node;
968 struct function *fn;
969
970 while (orig->clone_of)
971 orig = orig->clone_of;
972 fn = DECL_STRUCT_FUNCTION (orig->decl);
973
974 fixup_call_stmt_edges_1 (orig, stmts, fn);
975 if (orig->clones)
976 for (node = orig->clones; node != orig;)
977 {
978 fixup_call_stmt_edges_1 (node, stmts, fn);
979 if (node->clones)
980 node = node->clones;
981 else if (node->next_sibling_clone)
982 node = node->next_sibling_clone;
983 else
984 {
985 while (node != orig && !node->next_sibling_clone)
986 node = node->clone_of;
987 if (node != orig)
988 node = node->next_sibling_clone;
989 }
990 }
991 }
992
993
994 /* Input the base body of struct function FN from DATA_IN
995 using input block IB. */
996
997 static void
998 input_struct_function_base (struct function *fn, struct data_in *data_in,
999 struct lto_input_block *ib)
1000 {
1001 struct bitpack_d bp;
1002 int len;
1003
1004 /* Read the static chain and non-local goto save area. */
1005 fn->static_chain_decl = stream_read_tree (ib, data_in);
1006 fn->nonlocal_goto_save_area = stream_read_tree (ib, data_in);
1007
1008 /* Read all the local symbols. */
1009 len = streamer_read_hwi (ib);
1010 if (len > 0)
1011 {
1012 int i;
1013 vec_safe_grow_cleared (fn->local_decls, len);
1014 for (i = 0; i < len; i++)
1015 {
1016 tree t = stream_read_tree (ib, data_in);
1017 (*fn->local_decls)[i] = t;
1018 }
1019 }
1020
1021 /* Input the current IL state of the function. */
1022 fn->curr_properties = streamer_read_uhwi (ib);
1023
1024 /* Read all the attributes for FN. */
1025 bp = streamer_read_bitpack (ib);
1026 fn->is_thunk = bp_unpack_value (&bp, 1);
1027 fn->has_local_explicit_reg_vars = bp_unpack_value (&bp, 1);
1028 fn->returns_pcc_struct = bp_unpack_value (&bp, 1);
1029 fn->returns_struct = bp_unpack_value (&bp, 1);
1030 fn->can_throw_non_call_exceptions = bp_unpack_value (&bp, 1);
1031 fn->can_delete_dead_exceptions = bp_unpack_value (&bp, 1);
1032 fn->always_inline_functions_inlined = bp_unpack_value (&bp, 1);
1033 fn->after_inlining = bp_unpack_value (&bp, 1);
1034 fn->stdarg = bp_unpack_value (&bp, 1);
1035 fn->has_nonlocal_label = bp_unpack_value (&bp, 1);
1036 fn->calls_alloca = bp_unpack_value (&bp, 1);
1037 fn->calls_setjmp = bp_unpack_value (&bp, 1);
1038 fn->has_force_vectorize_loops = bp_unpack_value (&bp, 1);
1039 fn->has_simduid_loops = bp_unpack_value (&bp, 1);
1040 fn->va_list_fpr_size = bp_unpack_value (&bp, 8);
1041 fn->va_list_gpr_size = bp_unpack_value (&bp, 8);
1042 fn->last_clique = bp_unpack_value (&bp, sizeof (short) * 8);
1043
1044 /* Input the function start and end loci. */
1045 fn->function_start_locus = stream_input_location_now (&bp, data_in);
1046 fn->function_end_locus = stream_input_location_now (&bp, data_in);
1047 }
1048
1049
1050 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1051
1052 static void
1053 input_function (tree fn_decl, struct data_in *data_in,
1054 struct lto_input_block *ib, struct lto_input_block *ib_cfg)
1055 {
1056 struct function *fn;
1057 enum LTO_tags tag;
1058 gimple *stmts;
1059 basic_block bb;
1060 struct cgraph_node *node;
1061
1062 tag = streamer_read_record_start (ib);
1063 lto_tag_check (tag, LTO_function);
1064
1065 /* Read decls for parameters and args. */
1066 DECL_RESULT (fn_decl) = stream_read_tree (ib, data_in);
1067 DECL_ARGUMENTS (fn_decl) = streamer_read_chain (ib, data_in);
1068
1069 /* Read the tree of lexical scopes for the function. */
1070 DECL_INITIAL (fn_decl) = stream_read_tree (ib, data_in);
1071
1072 if (!streamer_read_uhwi (ib))
1073 return;
1074
1075 push_struct_function (fn_decl);
1076 fn = DECL_STRUCT_FUNCTION (fn_decl);
1077 init_tree_ssa (fn);
1078 /* We input IL in SSA form. */
1079 cfun->gimple_df->in_ssa_p = true;
1080
1081 gimple_register_cfg_hooks ();
1082
1083 node = cgraph_node::get (fn_decl);
1084 if (!node)
1085 node = cgraph_node::create (fn_decl);
1086 input_struct_function_base (fn, data_in, ib);
1087 input_cfg (ib_cfg, data_in, fn, node->count_materialization_scale);
1088
1089 /* Read all the SSA names. */
1090 input_ssa_names (ib, data_in, fn);
1091
1092 /* Read the exception handling regions in the function. */
1093 input_eh_regions (ib, data_in, fn);
1094
1095 gcc_assert (DECL_INITIAL (fn_decl));
1096 DECL_SAVED_TREE (fn_decl) = NULL_TREE;
1097
1098 /* Read all the basic blocks. */
1099 tag = streamer_read_record_start (ib);
1100 while (tag)
1101 {
1102 input_bb (ib, tag, data_in, fn,
1103 node->count_materialization_scale);
1104 tag = streamer_read_record_start (ib);
1105 }
1106
1107 /* Fix up the call statements that are mentioned in the callgraph
1108 edges. */
1109 set_gimple_stmt_max_uid (cfun, 0);
1110 FOR_ALL_BB_FN (bb, cfun)
1111 {
1112 gimple_stmt_iterator gsi;
1113 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1114 {
1115 gimple stmt = gsi_stmt (gsi);
1116 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1117 }
1118 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1119 {
1120 gimple stmt = gsi_stmt (gsi);
1121 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
1122 }
1123 }
1124 stmts = (gimple *) xcalloc (gimple_stmt_max_uid (fn), sizeof (gimple));
1125 FOR_ALL_BB_FN (bb, cfun)
1126 {
1127 gimple_stmt_iterator bsi = gsi_start_phis (bb);
1128 while (!gsi_end_p (bsi))
1129 {
1130 gimple stmt = gsi_stmt (bsi);
1131 gsi_next (&bsi);
1132 stmts[gimple_uid (stmt)] = stmt;
1133 }
1134 bsi = gsi_start_bb (bb);
1135 while (!gsi_end_p (bsi))
1136 {
1137 gimple stmt = gsi_stmt (bsi);
1138 /* If we're recompiling LTO objects with debug stmts but
1139 we're not supposed to have debug stmts, remove them now.
1140 We can't remove them earlier because this would cause uid
1141 mismatches in fixups, but we can do it at this point, as
1142 long as debug stmts don't require fixups. */
1143 if (!MAY_HAVE_DEBUG_STMTS && !flag_wpa && is_gimple_debug (stmt))
1144 {
1145 gimple_stmt_iterator gsi = bsi;
1146 gsi_next (&bsi);
1147 gsi_remove (&gsi, true);
1148 }
1149 else
1150 {
1151 gsi_next (&bsi);
1152 stmts[gimple_uid (stmt)] = stmt;
1153 }
1154 }
1155 }
1156
1157 /* Set the gimple body to the statement sequence in the entry
1158 basic block. FIXME lto, this is fairly hacky. The existence
1159 of a gimple body is used by the cgraph routines, but we should
1160 really use the presence of the CFG. */
1161 {
1162 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1163 gimple_set_body (fn_decl, bb_seq (ei_edge (ei)->dest));
1164 }
1165
1166 fixup_call_stmt_edges (node, stmts);
1167 execute_all_ipa_stmt_fixups (node, stmts);
1168
1169 update_ssa (TODO_update_ssa_only_virtuals);
1170 free_dominance_info (CDI_DOMINATORS);
1171 free_dominance_info (CDI_POST_DOMINATORS);
1172 free (stmts);
1173 pop_cfun ();
1174 }
1175
1176 /* Read the body of function FN_DECL from DATA_IN using input block IB. */
1177
1178 static void
1179 input_constructor (tree var, struct data_in *data_in,
1180 struct lto_input_block *ib)
1181 {
1182 DECL_INITIAL (var) = stream_read_tree (ib, data_in);
1183 }
1184
1185
1186 /* Read the body from DATA for function NODE and fill it in.
1187 FILE_DATA are the global decls and types. SECTION_TYPE is either
1188 LTO_section_function_body or LTO_section_static_initializer. If
1189 section type is LTO_section_function_body, FN must be the decl for
1190 that function. */
1191
1192 static void
1193 lto_read_body_or_constructor (struct lto_file_decl_data *file_data, struct symtab_node *node,
1194 const char *data, enum lto_section_type section_type)
1195 {
1196 const struct lto_function_header *header;
1197 struct data_in *data_in;
1198 int cfg_offset;
1199 int main_offset;
1200 int string_offset;
1201 tree fn_decl = node->decl;
1202
1203 header = (const struct lto_function_header *) data;
1204 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1205 {
1206 cfg_offset = sizeof (struct lto_function_header);
1207 main_offset = cfg_offset + header->cfg_size;
1208 string_offset = main_offset + header->main_size;
1209 }
1210 else
1211 {
1212 main_offset = sizeof (struct lto_function_header);
1213 string_offset = main_offset + header->main_size;
1214 }
1215
1216 data_in = lto_data_in_create (file_data, data + string_offset,
1217 header->string_size, vNULL);
1218
1219 if (section_type == LTO_section_function_body)
1220 {
1221 struct lto_in_decl_state *decl_state;
1222 unsigned from;
1223
1224 gcc_checking_assert (node);
1225
1226 /* Use the function's decl state. */
1227 decl_state = lto_get_function_in_decl_state (file_data, fn_decl);
1228 gcc_assert (decl_state);
1229 file_data->current_decl_state = decl_state;
1230
1231
1232 /* Set up the struct function. */
1233 from = data_in->reader_cache->nodes.length ();
1234 lto_input_block ib_main (data + main_offset, header->main_size,
1235 file_data->mode_table);
1236 if (TREE_CODE (node->decl) == FUNCTION_DECL)
1237 {
1238 lto_input_block ib_cfg (data + cfg_offset, header->cfg_size,
1239 file_data->mode_table);
1240 input_function (fn_decl, data_in, &ib_main, &ib_cfg);
1241 }
1242 else
1243 input_constructor (fn_decl, data_in, &ib_main);
1244 data_in->location_cache.apply_location_cache ();
1245 /* And fixup types we streamed locally. */
1246 {
1247 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1248 unsigned len = cache->nodes.length ();
1249 unsigned i;
1250 for (i = len; i-- > from;)
1251 {
1252 tree t = streamer_tree_cache_get_tree (cache, i);
1253 if (t == NULL_TREE)
1254 continue;
1255
1256 if (TYPE_P (t))
1257 {
1258 gcc_assert (TYPE_CANONICAL (t) == NULL_TREE);
1259 TYPE_CANONICAL (t) = TYPE_MAIN_VARIANT (t);
1260 if (TYPE_MAIN_VARIANT (t) != t)
1261 {
1262 gcc_assert (TYPE_NEXT_VARIANT (t) == NULL_TREE);
1263 TYPE_NEXT_VARIANT (t)
1264 = TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t));
1265 TYPE_NEXT_VARIANT (TYPE_MAIN_VARIANT (t)) = t;
1266 }
1267 }
1268 }
1269 }
1270
1271 /* Restore decl state */
1272 file_data->current_decl_state = file_data->global_decl_state;
1273 }
1274
1275 lto_data_in_delete (data_in);
1276 }
1277
1278
1279 /* Read the body of NODE using DATA. FILE_DATA holds the global
1280 decls and types. */
1281
1282 void
1283 lto_input_function_body (struct lto_file_decl_data *file_data,
1284 struct cgraph_node *node, const char *data)
1285 {
1286 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1287 }
1288
1289 /* Read the body of NODE using DATA. FILE_DATA holds the global
1290 decls and types. */
1291
1292 void
1293 lto_input_variable_constructor (struct lto_file_decl_data *file_data,
1294 struct varpool_node *node, const char *data)
1295 {
1296 lto_read_body_or_constructor (file_data, node, data, LTO_section_function_body);
1297 }
1298
1299
1300 /* Read the physical representation of a tree node EXPR from
1301 input block IB using the per-file context in DATA_IN. */
1302
1303 static void
1304 lto_read_tree_1 (struct lto_input_block *ib, struct data_in *data_in, tree expr)
1305 {
1306 /* Read all the bitfield values in EXPR. Note that for LTO, we
1307 only write language-independent bitfields, so no more unpacking is
1308 needed. */
1309 streamer_read_tree_bitfields (ib, data_in, expr);
1310
1311 /* Read all the pointer fields in EXPR. */
1312 streamer_read_tree_body (ib, data_in, expr);
1313
1314 /* Read any LTO-specific data not read by the tree streamer. */
1315 if (DECL_P (expr)
1316 && TREE_CODE (expr) != FUNCTION_DECL
1317 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1318 DECL_INITIAL (expr) = stream_read_tree (ib, data_in);
1319
1320 /* We should never try to instantiate an MD or NORMAL builtin here. */
1321 if (TREE_CODE (expr) == FUNCTION_DECL)
1322 gcc_assert (!streamer_handle_as_builtin_p (expr));
1323
1324 #ifdef LTO_STREAMER_DEBUG
1325 /* Remove the mapping to RESULT's original address set by
1326 streamer_alloc_tree. */
1327 lto_orig_address_remove (expr);
1328 #endif
1329 }
1330
1331 /* Read the physical representation of a tree node with tag TAG from
1332 input block IB using the per-file context in DATA_IN. */
1333
1334 static tree
1335 lto_read_tree (struct lto_input_block *ib, struct data_in *data_in,
1336 enum LTO_tags tag, hashval_t hash)
1337 {
1338 /* Instantiate a new tree node. */
1339 tree result = streamer_alloc_tree (ib, data_in, tag);
1340
1341 /* Enter RESULT in the reader cache. This will make RESULT
1342 available so that circular references in the rest of the tree
1343 structure can be resolved in subsequent calls to stream_read_tree. */
1344 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1345
1346 lto_read_tree_1 (ib, data_in, result);
1347
1348 /* end_marker = */ streamer_read_uchar (ib);
1349
1350 return result;
1351 }
1352
1353
1354 /* Populate the reader cache with trees materialized from the SCC
1355 following in the IB, DATA_IN stream. */
1356
1357 hashval_t
1358 lto_input_scc (struct lto_input_block *ib, struct data_in *data_in,
1359 unsigned *len, unsigned *entry_len)
1360 {
1361 /* A blob of unnamed tree nodes, fill the cache from it and
1362 recurse. */
1363 unsigned size = streamer_read_uhwi (ib);
1364 hashval_t scc_hash = streamer_read_uhwi (ib);
1365 unsigned scc_entry_len = 1;
1366
1367 if (size == 1)
1368 {
1369 enum LTO_tags tag = streamer_read_record_start (ib);
1370 lto_input_tree_1 (ib, data_in, tag, scc_hash);
1371 }
1372 else
1373 {
1374 unsigned int first = data_in->reader_cache->nodes.length ();
1375 tree result;
1376
1377 scc_entry_len = streamer_read_uhwi (ib);
1378
1379 /* Materialize size trees by reading their headers. */
1380 for (unsigned i = 0; i < size; ++i)
1381 {
1382 enum LTO_tags tag = streamer_read_record_start (ib);
1383 if (tag == LTO_null
1384 || (tag >= LTO_field_decl_ref && tag <= LTO_global_decl_ref)
1385 || tag == LTO_tree_pickle_reference
1386 || tag == LTO_builtin_decl
1387 || tag == LTO_integer_cst
1388 || tag == LTO_tree_scc)
1389 gcc_unreachable ();
1390
1391 result = streamer_alloc_tree (ib, data_in, tag);
1392 streamer_tree_cache_append (data_in->reader_cache, result, 0);
1393 }
1394
1395 /* Read the tree bitpacks and references. */
1396 for (unsigned i = 0; i < size; ++i)
1397 {
1398 result = streamer_tree_cache_get_tree (data_in->reader_cache,
1399 first + i);
1400 lto_read_tree_1 (ib, data_in, result);
1401 /* end_marker = */ streamer_read_uchar (ib);
1402 }
1403 }
1404
1405 *len = size;
1406 *entry_len = scc_entry_len;
1407 return scc_hash;
1408 }
1409
1410
1411 /* Read a tree from input block IB using the per-file context in
1412 DATA_IN. This context is used, for example, to resolve references
1413 to previously read nodes. */
1414
1415 tree
1416 lto_input_tree_1 (struct lto_input_block *ib, struct data_in *data_in,
1417 enum LTO_tags tag, hashval_t hash)
1418 {
1419 tree result;
1420
1421 gcc_assert ((unsigned) tag < (unsigned) LTO_NUM_TAGS);
1422
1423 if (tag == LTO_null)
1424 result = NULL_TREE;
1425 else if (tag >= LTO_field_decl_ref && tag <= LTO_namelist_decl_ref)
1426 {
1427 /* If TAG is a reference to an indexable tree, the next value
1428 in IB is the index into the table where we expect to find
1429 that tree. */
1430 result = lto_input_tree_ref (ib, data_in, cfun, tag);
1431 }
1432 else if (tag == LTO_tree_pickle_reference)
1433 {
1434 /* If TAG is a reference to a previously read tree, look it up in
1435 the reader cache. */
1436 result = streamer_get_pickled_tree (ib, data_in);
1437 }
1438 else if (tag == LTO_builtin_decl)
1439 {
1440 /* If we are going to read a built-in function, all we need is
1441 the code and class. */
1442 result = streamer_get_builtin_tree (ib, data_in);
1443 }
1444 else if (tag == LTO_integer_cst)
1445 {
1446 /* For shared integer constants in singletons we can use the
1447 existing tree integer constant merging code. */
1448 tree type = stream_read_tree (ib, data_in);
1449 unsigned HOST_WIDE_INT len = streamer_read_uhwi (ib);
1450 unsigned HOST_WIDE_INT i;
1451 HOST_WIDE_INT a[WIDE_INT_MAX_ELTS];
1452
1453 for (i = 0; i < len; i++)
1454 a[i] = streamer_read_hwi (ib);
1455 gcc_assert (TYPE_PRECISION (type) <= MAX_BITSIZE_MODE_ANY_INT);
1456 result = wide_int_to_tree (type, wide_int::from_array
1457 (a, len, TYPE_PRECISION (type)));
1458 streamer_tree_cache_append (data_in->reader_cache, result, hash);
1459 }
1460 else if (tag == LTO_tree_scc)
1461 gcc_unreachable ();
1462 else
1463 {
1464 /* Otherwise, materialize a new node from IB. */
1465 result = lto_read_tree (ib, data_in, tag, hash);
1466 }
1467
1468 return result;
1469 }
1470
1471 tree
1472 lto_input_tree (struct lto_input_block *ib, struct data_in *data_in)
1473 {
1474 enum LTO_tags tag;
1475
1476 /* Input and skip SCCs. */
1477 while ((tag = streamer_read_record_start (ib)) == LTO_tree_scc)
1478 {
1479 unsigned len, entry_len;
1480 lto_input_scc (ib, data_in, &len, &entry_len);
1481 }
1482 return lto_input_tree_1 (ib, data_in, tag, 0);
1483 }
1484
1485
1486 /* Input toplevel asms. */
1487
1488 void
1489 lto_input_toplevel_asms (struct lto_file_decl_data *file_data, int order_base)
1490 {
1491 size_t len;
1492 const char *data = lto_get_section_data (file_data, LTO_section_asm,
1493 NULL, &len);
1494 const struct lto_simple_header_with_strings *header
1495 = (const struct lto_simple_header_with_strings *) data;
1496 int string_offset;
1497 struct data_in *data_in;
1498 tree str;
1499
1500 if (! data)
1501 return;
1502
1503 string_offset = sizeof (*header) + header->main_size;
1504
1505 lto_input_block ib (data + sizeof (*header), header->main_size,
1506 file_data->mode_table);
1507
1508 data_in = lto_data_in_create (file_data, data + string_offset,
1509 header->string_size, vNULL);
1510
1511 while ((str = streamer_read_string_cst (data_in, &ib)))
1512 {
1513 asm_node *node = symtab->finalize_toplevel_asm (str);
1514 node->order = streamer_read_hwi (&ib) + order_base;
1515 if (node->order >= symtab->order)
1516 symtab->order = node->order + 1;
1517 }
1518
1519 lto_data_in_delete (data_in);
1520
1521 lto_free_section_data (file_data, LTO_section_asm, NULL, data, len);
1522 }
1523
1524
1525 /* Input mode table. */
1526
1527 void
1528 lto_input_mode_table (struct lto_file_decl_data *file_data)
1529 {
1530 size_t len;
1531 const char *data = lto_get_section_data (file_data, LTO_section_mode_table,
1532 NULL, &len);
1533 if (! data)
1534 {
1535 internal_error ("cannot read LTO mode table from %s",
1536 file_data->file_name);
1537 return;
1538 }
1539
1540 unsigned char *table = ggc_cleared_vec_alloc<unsigned char> (1 << 8);
1541 file_data->mode_table = table;
1542 const struct lto_simple_header_with_strings *header
1543 = (const struct lto_simple_header_with_strings *) data;
1544 int string_offset;
1545 struct data_in *data_in;
1546 string_offset = sizeof (*header) + header->main_size;
1547
1548 lto_input_block ib (data + sizeof (*header), header->main_size, NULL);
1549 data_in = lto_data_in_create (file_data, data + string_offset,
1550 header->string_size, vNULL);
1551 bitpack_d bp = streamer_read_bitpack (&ib);
1552
1553 table[VOIDmode] = VOIDmode;
1554 table[BLKmode] = BLKmode;
1555 unsigned int m;
1556 while ((m = bp_unpack_value (&bp, 8)) != VOIDmode)
1557 {
1558 enum mode_class mclass
1559 = bp_unpack_enum (&bp, mode_class, MAX_MODE_CLASS);
1560 unsigned int size = bp_unpack_value (&bp, 8);
1561 unsigned int prec = bp_unpack_value (&bp, 16);
1562 machine_mode inner = (machine_mode) table[bp_unpack_value (&bp, 8)];
1563 unsigned int nunits = bp_unpack_value (&bp, 8);
1564 unsigned int ibit = 0, fbit = 0;
1565 unsigned int real_fmt_len = 0;
1566 const char *real_fmt_name = NULL;
1567 switch (mclass)
1568 {
1569 case MODE_FRACT:
1570 case MODE_UFRACT:
1571 case MODE_ACCUM:
1572 case MODE_UACCUM:
1573 ibit = bp_unpack_value (&bp, 8);
1574 fbit = bp_unpack_value (&bp, 8);
1575 break;
1576 case MODE_FLOAT:
1577 case MODE_DECIMAL_FLOAT:
1578 real_fmt_name = bp_unpack_indexed_string (data_in, &bp,
1579 &real_fmt_len);
1580 break;
1581 default:
1582 break;
1583 }
1584 /* First search just the GET_CLASS_NARROWEST_MODE to wider modes,
1585 if not found, fallback to all modes. */
1586 int pass;
1587 for (pass = 0; pass < 2; pass++)
1588 for (machine_mode mr = pass ? VOIDmode
1589 : GET_CLASS_NARROWEST_MODE (mclass);
1590 pass ? mr < MAX_MACHINE_MODE : mr != VOIDmode;
1591 pass ? mr = (machine_mode) (m + 1)
1592 : mr = GET_MODE_WIDER_MODE (mr))
1593 if (GET_MODE_CLASS (mr) != mclass
1594 || GET_MODE_SIZE (mr) != size
1595 || GET_MODE_PRECISION (mr) != prec
1596 || GET_MODE_INNER (mr) != inner
1597 || GET_MODE_IBIT (mr) != ibit
1598 || GET_MODE_FBIT (mr) != fbit
1599 || GET_MODE_NUNITS (mr) != nunits)
1600 continue;
1601 else if ((mclass == MODE_FLOAT || mclass == MODE_DECIMAL_FLOAT)
1602 && strcmp (REAL_MODE_FORMAT (mr)->name, real_fmt_name) != 0)
1603 continue;
1604 else
1605 {
1606 table[m] = mr;
1607 pass = 2;
1608 break;
1609 }
1610 unsigned int mname_len;
1611 const char *mname = bp_unpack_indexed_string (data_in, &bp, &mname_len);
1612 if (pass == 2)
1613 {
1614 switch (mclass)
1615 {
1616 case MODE_VECTOR_INT:
1617 case MODE_VECTOR_FLOAT:
1618 case MODE_VECTOR_FRACT:
1619 case MODE_VECTOR_UFRACT:
1620 case MODE_VECTOR_ACCUM:
1621 case MODE_VECTOR_UACCUM:
1622 /* For unsupported vector modes just use BLKmode,
1623 if the scalar mode is supported. */
1624 if (inner != VOIDmode)
1625 {
1626 table[m] = BLKmode;
1627 break;
1628 }
1629 /* FALLTHRU */
1630 default:
1631 fatal_error (UNKNOWN_LOCATION, "unsupported mode %s\n", mname);
1632 break;
1633 }
1634 }
1635 }
1636 lto_data_in_delete (data_in);
1637
1638 lto_free_section_data (file_data, LTO_section_mode_table, NULL, data, len);
1639 }
1640
1641
1642 /* Initialization for the LTO reader. */
1643
1644 void
1645 lto_reader_init (void)
1646 {
1647 lto_streamer_init ();
1648 file_name_hash_table
1649 = new hash_table<freeing_string_slot_hasher> (37);
1650 }
1651
1652
1653 /* Create a new data_in object for FILE_DATA. STRINGS is the string
1654 table to use with LEN strings. RESOLUTIONS is the vector of linker
1655 resolutions (NULL if not using a linker plugin). */
1656
1657 struct data_in *
1658 lto_data_in_create (struct lto_file_decl_data *file_data, const char *strings,
1659 unsigned len,
1660 vec<ld_plugin_symbol_resolution_t> resolutions)
1661 {
1662 struct data_in *data_in = new (struct data_in);
1663 data_in->file_data = file_data;
1664 data_in->strings = strings;
1665 data_in->strings_len = len;
1666 data_in->globals_resolution = resolutions;
1667 data_in->reader_cache = streamer_tree_cache_create (false, false, true);
1668 return data_in;
1669 }
1670
1671
1672 /* Remove DATA_IN. */
1673
1674 void
1675 lto_data_in_delete (struct data_in *data_in)
1676 {
1677 data_in->globals_resolution.release ();
1678 streamer_tree_cache_delete (data_in->reader_cache);
1679 delete data_in;
1680 }