]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto-streamer.h
9e0031e5ba3f1a35b8a62f3c76013e3c0dc8b900
[thirdparty/gcc.git] / gcc / lto-streamer.h
1 /* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
3
4 Copyright (C) 2009-2020 Free Software Foundation, Inc.
5 Contributed by Doug Kwan <dougkwan@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 #ifndef GCC_LTO_STREAMER_H
24 #define GCC_LTO_STREAMER_H
25
26 #include "plugin-api.h"
27 #include "gcov-io.h"
28 #include "diagnostic.h"
29
30 /* The encoding for a function consists of the following sections:
31
32 1) The header.
33 2) FIELD_DECLS.
34 3) FUNCTION_DECLS.
35 4) global VAR_DECLS.
36 5) type_decls
37 6) types.
38 7) Names for the labels that have names
39 8) The SSA names.
40 9) The control flow graph.
41 10-11)Gimple for local decls.
42 12) Gimple for the function.
43 13) Strings.
44
45 1) THE HEADER.
46 2-6) THE GLOBAL DECLS AND TYPES.
47
48 The global decls and types are encoded in the same way. For each
49 entry, there is word with the offset within the section to the
50 entry.
51
52 7) THE LABEL NAMES.
53
54 Since most labels do not have names, this section my be of zero
55 length. It consists of an array of string table references, one
56 per label. In the lto code, the labels are given either
57 positive or negative indexes. the positive ones have names and
58 the negative ones do not. The positive index can be used to
59 find the name in this array.
60
61 9) THE CFG.
62
63 10) Index into the local decls. Since local decls can have local
64 decls inside them, they must be read in randomly in order to
65 properly restore them.
66
67 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
68
69 The gimple consists of a set of records.
70
71 THE FUNCTION
72
73 At the top level of (8) is the function. It consists of five
74 pieces:
75
76 LTO_function - The tag.
77 eh tree - This is all of the exception handling regions
78 put out in a post order traversial of the
79 tree. Siblings are output as lists terminated
80 by a 0. The set of fields matches the fields
81 defined in except.c.
82
83 last_basic_block - in uleb128 form.
84
85 basic blocks - This is the set of basic blocks.
86
87 zero - The termination of the basic blocks.
88
89 BASIC BLOCKS
90
91 There are two forms of basic blocks depending on if they are
92 empty or not.
93
94 The basic block consists of:
95
96 LTO_bb1 or LTO_bb0 - The tag.
97
98 bb->index - the index in uleb128 form.
99
100 #succs - The number of successors un uleb128 form.
101
102 the successors - For each edge, a pair. The first of the
103 pair is the index of the successor in
104 uleb128 form and the second are the flags in
105 uleb128 form.
106
107 the statements - A gimple tree, as described above.
108 These are only present for LTO_BB1.
109 Following each statement is an optional
110 exception handling record LTO_eh_region
111 which contains the region number (for
112 regions >= 0).
113
114 zero - This is only present for LTO_BB1 and is used
115 to terminate the statements and exception
116 regions within this block.
117
118 12) STRINGS
119
120 String are represented in the table as pairs, a length in ULEB128
121 form followed by the data for the string. */
122
123 #define LTO_major_version 9
124 #define LTO_minor_version 0
125
126 typedef unsigned char lto_decl_flags_t;
127
128 /* Stream additional data to LTO object files to make it easier to debug
129 streaming code. This changes object files. */
130 static const bool streamer_debugging = false;
131
132 /* Tags representing the various IL objects written to the bytecode file
133 (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
134
135 NOTE, when adding new LTO tags, also update lto_tag_name. */
136 enum LTO_tags
137 {
138 LTO_null = 0,
139
140 /* Reference to previously-streamed node. */
141 LTO_tree_pickle_reference,
142
143 /* References to indexable tree nodes. These objects are stored in
144 tables that are written separately from the function bodies
145 and variable constructors that reference them. This way they can be
146 instantiated even when the referencing functions aren't (e.g., during WPA)
147 and it also allows functions to be copied from one file to another without
148 having to unpickle the body first (the references are location
149 independent). */
150 LTO_global_stream_ref,
151
152 LTO_ssa_name_ref,
153
154 /* Special for global streamer. A blob of unnamed tree nodes. */
155 LTO_tree_scc,
156
157 /* Sequence of trees. */
158 LTO_trees,
159
160 /* Shared INTEGER_CST node. */
161 LTO_integer_cst,
162
163 /* Tags of trees are encoded as
164 LTO_first_tree_tag + TREE_CODE. */
165 LTO_first_tree_tag,
166 /* Tags of gimple typles are encoded as
167 LTO_first_gimple_tag + gimple_code. */
168 LTO_first_gimple_tag = LTO_first_tree_tag + MAX_TREE_CODES,
169
170 /* Entry and exit basic blocks. */
171 LTO_bb0 = LTO_first_gimple_tag + LAST_AND_UNUSED_GIMPLE_CODE,
172 LTO_bb1,
173
174 /* EH region holding the previous statement. */
175 LTO_eh_region,
176
177 /* Function body. */
178 LTO_function,
179
180 /* EH table. */
181 LTO_eh_table,
182
183 /* EH region types. These mirror enum eh_region_type. */
184 LTO_ert_cleanup,
185 LTO_ert_try,
186 LTO_ert_allowed_exceptions,
187 LTO_ert_must_not_throw,
188
189 /* EH landing pad. */
190 LTO_eh_landing_pad,
191
192 /* EH try/catch node. */
193 LTO_eh_catch,
194
195 /* This tag must always be last. */
196 LTO_NUM_TAGS
197 };
198
199
200 /* Set of section types that are in an LTO file. This list will grow
201 as the number of IPA passes grows since each IPA pass will need its
202 own section type to store its summary information.
203
204 When adding a new section type, you must also extend the
205 LTO_SECTION_NAME array in lto-section-in.c. */
206 enum lto_section_type
207 {
208 LTO_section_decls = 0,
209 LTO_section_function_body,
210 LTO_section_static_initializer,
211 LTO_section_symtab,
212 LTO_section_symtab_extension,
213 LTO_section_refs,
214 LTO_section_asm,
215 LTO_section_jump_functions,
216 LTO_section_ipa_pure_const,
217 LTO_section_ipa_reference,
218 LTO_section_ipa_profile,
219 LTO_section_symtab_nodes,
220 LTO_section_opts,
221 LTO_section_cgraph_opt_sum,
222 LTO_section_ipa_fn_summary,
223 LTO_section_ipcp_transform,
224 LTO_section_ipa_icf,
225 LTO_section_offload_table,
226 LTO_section_mode_table,
227 LTO_section_ipa_hsa,
228 LTO_section_lto,
229 LTO_section_ipa_sra,
230 LTO_N_SECTION_TYPES /* Must be last. */
231 };
232
233 /* Indices to the various function, type and symbol streams. */
234 enum lto_decl_stream_e_t
235 {
236 LTO_DECL_STREAM = 0, /* Must be first. */
237 LTO_N_DECL_STREAMS
238 };
239
240 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
241
242 /* Return a char pointer to the start of a data stream for an lto pass
243 or function. The first parameter is the file data that contains
244 the information. The second parameter is the type of information
245 to be obtained. The third parameter is the name of the function
246 and is only used when finding a function body; otherwise it is
247 NULL. The fourth parameter is the length of the data returned. */
248 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
249 enum lto_section_type,
250 const char *,
251 int,
252 size_t *);
253
254 /* Return the data found from the above call. The first three
255 parameters are the same as above. The fourth parameter is the data
256 itself and the fifth is the length of the data. */
257 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
258 enum lto_section_type,
259 const char *,
260 const char *,
261 size_t);
262
263 /* The location cache holds expanded locations for streamed in trees.
264 This is done to reduce memory usage of libcpp linemap that strongly prefers
265 locations to be inserted in the soruce order. */
266
267 class lto_location_cache
268 {
269 public:
270 /* Apply all changes in location cache. Add locations into linemap and patch
271 trees. */
272 bool apply_location_cache ();
273 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
274 void accept_location_cache ();
275 /* Tree merging did suceed; throw away recent changes. */
276 void revert_location_cache ();
277 void input_location (location_t *loc, struct bitpack_d *bp,
278 class data_in *data_in);
279 lto_location_cache ()
280 : loc_cache (), accepted_length (0), current_file (NULL), current_line (0),
281 current_col (0), current_sysp (false), current_loc (UNKNOWN_LOCATION)
282 {
283 gcc_assert (!current_cache);
284 current_cache = this;
285 }
286 ~lto_location_cache ()
287 {
288 apply_location_cache ();
289 gcc_assert (current_cache == this);
290 current_cache = NULL;
291 }
292
293 /* There can be at most one instance of location cache (combining multiple
294 would bring it out of sync with libcpp linemap); point to current
295 one. */
296 static lto_location_cache *current_cache;
297
298 private:
299 static int cmp_loc (const void *pa, const void *pb);
300
301 struct cached_location
302 {
303 const char *file;
304 location_t *loc;
305 int line, col;
306 bool sysp;
307 };
308
309 /* The location cache. */
310
311 auto_vec<cached_location> loc_cache;
312
313 /* Accepted entries are ones used by trees that are known to be not unified
314 by tree merging. */
315
316 int accepted_length;
317
318 /* Bookkeeping to remember state in between calls to lto_apply_location_cache
319 When streaming gimple, the location cache is not used and thus
320 lto_apply_location_cache happens per location basis. It is then
321 useful to avoid redundant calls of linemap API. */
322
323 const char *current_file;
324 int current_line;
325 int current_col;
326 bool current_sysp;
327 location_t current_loc;
328 };
329
330 /* Structure used as buffer for reading an LTO file. */
331 class lto_input_block
332 {
333 public:
334 /* Special constructor for the string table, it abuses this to
335 do random access but use the uhwi decoder. */
336 lto_input_block (const char *data_, unsigned int p_, unsigned int len_,
337 const unsigned char *mode_table_)
338 : data (data_), mode_table (mode_table_), p (p_), len (len_) {}
339 lto_input_block (const char *data_, unsigned int len_,
340 const unsigned char *mode_table_)
341 : data (data_), mode_table (mode_table_), p (0), len (len_) {}
342
343 const char *data;
344 const unsigned char *mode_table;
345 unsigned int p;
346 unsigned int len;
347 };
348
349 /* Compression algorithm used for compression of LTO bytecode. */
350
351 enum lto_compression
352 {
353 ZLIB,
354 ZSTD
355 };
356
357 /* Structure that represents LTO ELF section with information
358 about the format. */
359
360 struct lto_section
361 {
362 int16_t major_version;
363 int16_t minor_version;
364 unsigned char slim_object;
365
366 /* Flags is a private field that is not defined publicly. */
367 uint16_t flags;
368
369 /* Set compression to FLAGS. */
370 inline void set_compression (lto_compression c)
371 {
372 flags = c;
373 }
374
375 /* Get compression from FLAGS. */
376 inline lto_compression get_compression ()
377 {
378 return (lto_compression) flags;
379 }
380 };
381
382 STATIC_ASSERT (sizeof (lto_section) == 8);
383
384 /* The is the first part of the record in an LTO file for many of the
385 IPA passes. */
386 struct lto_simple_header
387 {
388 /* Size of main gimple body of function. */
389 int32_t main_size;
390 };
391
392 struct lto_simple_header_with_strings : lto_simple_header
393 {
394 /* Size of the string table. */
395 int32_t string_size;
396 };
397
398 /* The header for a function body. */
399 struct lto_function_header : lto_simple_header_with_strings
400 {
401 /* Size of the cfg. */
402 int32_t cfg_size;
403 };
404
405
406 /* Structure describing a symbol section. */
407 struct lto_decl_header : lto_simple_header_with_strings
408 {
409 /* Size of region for decl state. */
410 int32_t decl_state_size;
411
412 /* Number of nodes in globals stream. */
413 int32_t num_nodes;
414 };
415
416
417 /* Statistics gathered during LTO, WPA and LTRANS. */
418 struct lto_stats_d
419 {
420 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
421 unsigned HOST_WIDE_INT num_output_symtab_nodes;
422 unsigned HOST_WIDE_INT num_input_files;
423 unsigned HOST_WIDE_INT num_output_files;
424 unsigned HOST_WIDE_INT num_cgraph_partitions;
425 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
426 unsigned HOST_WIDE_INT num_function_bodies;
427 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
428 unsigned HOST_WIDE_INT num_output_il_bytes;
429 unsigned HOST_WIDE_INT num_compressed_il_bytes;
430 unsigned HOST_WIDE_INT num_input_il_bytes;
431 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
432 unsigned HOST_WIDE_INT num_tree_bodies_output;
433 unsigned HOST_WIDE_INT num_pickle_refs_output;
434 };
435
436 /* Entry of LTO symtab encoder. */
437 struct lto_encoder_entry
438 {
439 symtab_node *node;
440 /* Is the node in this partition (i.e. ltrans of this partition will
441 be responsible for outputting it)? */
442 unsigned int in_partition:1;
443 /* Do we encode body in this partition? */
444 unsigned int body:1;
445 /* Do we encode initializer in this partition?
446 For example the readonly variable initializers are encoded to aid
447 constant folding even if they are not in the partition. */
448 unsigned int initializer:1;
449 };
450
451
452 /* Encoder data structure used to stream callgraph nodes. */
453 struct lto_symtab_encoder_d
454 {
455 vec<lto_encoder_entry> nodes;
456 hash_map<symtab_node *, size_t> *map;
457 };
458
459 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
460
461 /* Iterator structure for cgraph node sets. */
462 struct lto_symtab_encoder_iterator
463 {
464 lto_symtab_encoder_t encoder;
465 unsigned index;
466 };
467
468
469
470 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
471
472 struct lto_tree_ref_encoder
473 {
474 hash_map<tree, unsigned> *tree_hash_table; /* Maps pointers to indices. */
475 vec<tree> trees; /* Maps indices to pointers. */
476 };
477
478
479 /* Structure to hold states of input scope. */
480 struct GTY((for_user)) lto_in_decl_state
481 {
482 /* Array of lto_in_decl_buffers to store type and decls streams. */
483 vec<tree, va_gc> *streams[LTO_N_DECL_STREAMS];
484
485 /* If this in-decl state is associated with a function. FN_DECL
486 point to the FUNCTION_DECL. */
487 tree fn_decl;
488
489 /* True if decl state is compressed. */
490 bool compressed;
491 };
492
493 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
494
495 struct decl_state_hasher : ggc_ptr_hash<lto_in_decl_state>
496 {
497 static hashval_t
498 hash (lto_in_decl_state *s)
499 {
500 return htab_hash_pointer (s->fn_decl);
501 }
502
503 static bool
504 equal (lto_in_decl_state *a, lto_in_decl_state *b)
505 {
506 return a->fn_decl == b->fn_decl;
507 }
508 };
509
510 /* The structure that holds all of the vectors of global types,
511 decls and cgraph nodes used in the serialization of this file. */
512 struct lto_out_decl_state
513 {
514 /* The buffers contain the sets of decls of various kinds and types we have
515 seen so far and the indexes assigned to them. */
516 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
517
518 /* Encoder for cgraph nodes. */
519 lto_symtab_encoder_t symtab_node_encoder;
520
521 /* If this out-decl state belongs to a function, fn_decl points to that
522 function. Otherwise, it is NULL. */
523 tree fn_decl;
524
525 /* True if decl state is compressed. */
526 bool compressed;
527 };
528
529 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
530
531
532 /* Compact representation of a index <-> resolution pair. Unpacked to an
533 vector later. */
534 struct res_pair
535 {
536 ld_plugin_symbol_resolution_t res;
537 unsigned index;
538 };
539
540
541 /* One of these is allocated for each object file that being compiled
542 by lto. This structure contains the tables that are needed by the
543 serialized functions and ipa passes to connect themselves to the
544 global types and decls as they are reconstituted. */
545 struct GTY(()) lto_file_decl_data
546 {
547 /* Decl state currently used. */
548 struct lto_in_decl_state *current_decl_state;
549
550 /* Decl state corresponding to regions outside of any functions
551 in the compilation unit. */
552 struct lto_in_decl_state *global_decl_state;
553
554 /* Table of cgraph nodes present in this file. */
555 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
556
557 /* Hash table maps lto-related section names to location in file. */
558 hash_table<decl_state_hasher> *function_decl_states;
559
560 /* The .o file that these offsets relate to. */
561 const char *GTY((skip)) file_name;
562
563 /* Hash table maps lto-related section names to location in file. */
564 htab_t GTY((skip)) section_hash_table;
565
566 /* Hash new name of renamed global declaration to its original name. */
567 htab_t GTY((skip)) renaming_hash_table;
568
569 /* Linked list used temporarily in reader */
570 struct lto_file_decl_data *next;
571
572 /* Order in which the file appears on the command line. */
573 int order;
574
575 /* Sub ID for merged objects. */
576 unsigned HOST_WIDE_INT id;
577
578 /* Symbol resolutions for this file */
579 vec<res_pair> GTY((skip)) respairs;
580 unsigned max_index;
581
582 gcov_summary GTY((skip)) profile_info;
583
584 /* Map assigning declarations their resolutions. */
585 hash_map<tree, ld_plugin_symbol_resolution> * GTY((skip)) resolution_map;
586
587 /* Mode translation table. */
588 const unsigned char *mode_table;
589
590 /* Read LTO section. */
591 lto_section lto_section_header;
592
593 int order_base;
594
595 int unit_base;
596 };
597
598 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
599
600 struct lto_char_ptr_base
601 {
602 char *ptr;
603 };
604
605 /* An incore byte stream to buffer the various parts of the function.
606 The entire structure should be zeroed when created. The record
607 consists of a set of blocks. The first sizeof (ptr) bytes are used
608 as a chain, and the rest store the bytes to be written. */
609 struct lto_output_stream
610 {
611 /* The pointer to the first block in the stream. */
612 struct lto_char_ptr_base * first_block;
613
614 /* The pointer to the last and current block in the stream. */
615 struct lto_char_ptr_base * current_block;
616
617 /* The pointer to where the next char should be written. */
618 char * current_pointer;
619
620 /* The number of characters left in the current block. */
621 unsigned int left_in_block;
622
623 /* The block size of the last block allocated. */
624 unsigned int block_size;
625
626 /* The total number of characters written. */
627 unsigned int total_size;
628 };
629
630 /* A simple output block. This can be used for simple IPA passes that
631 do not need more than one stream. */
632 struct lto_simple_output_block
633 {
634 enum lto_section_type section_type;
635 struct lto_out_decl_state *decl_state;
636
637 /* The stream that the main tree codes are written to. */
638 struct lto_output_stream *main_stream;
639 };
640
641 /* String hashing. */
642
643 struct string_slot
644 {
645 const char *s;
646 int len;
647 unsigned int slot_num;
648 };
649
650 /* Hashtable helpers. */
651
652 struct string_slot_hasher : nofree_ptr_hash <string_slot>
653 {
654 static inline hashval_t hash (const string_slot *);
655 static inline bool equal (const string_slot *, const string_slot *);
656 };
657
658 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
659 to support strings that may not end in '\0'. */
660
661 inline hashval_t
662 string_slot_hasher::hash (const string_slot *ds)
663 {
664 hashval_t r = ds->len;
665 int i;
666
667 for (i = 0; i < ds->len; i++)
668 r = r * 67 + (unsigned)ds->s[i] - 113;
669 return r;
670 }
671
672 /* Returns nonzero if DS1 and DS2 are equal. */
673
674 inline bool
675 string_slot_hasher::equal (const string_slot *ds1, const string_slot *ds2)
676 {
677 if (ds1->len == ds2->len)
678 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
679
680 return 0;
681 }
682
683 /* Data structure holding all the data and descriptors used when writing
684 an LTO file. */
685 struct output_block
686 {
687 enum lto_section_type section_type;
688 struct lto_out_decl_state *decl_state;
689
690 /* The stream that the main tree codes are written to. */
691 struct lto_output_stream *main_stream;
692
693 /* The stream that contains the string table. */
694 struct lto_output_stream *string_stream;
695
696 /* The stream that contains the cfg. */
697 struct lto_output_stream *cfg_stream;
698
699 /* The hash table that contains the set of strings we have seen so
700 far and the indexes assigned to them. */
701 hash_table<string_slot_hasher> *string_hash_table;
702
703 /* The current symbol that we are currently serializing. Null
704 if we are serializing something else. */
705 symtab_node *symbol;
706
707 /* These are the last file and line that were seen in the stream.
708 If the current node differs from these, it needs to insert
709 something into the stream and fix these up. */
710 const char *current_file;
711 int current_line;
712 int current_col;
713 bool current_sysp;
714
715 /* Cache of nodes written in this section. */
716 struct streamer_tree_cache_d *writer_cache;
717
718 /* All trees identified as local to the unit streamed. */
719 hash_set<tree> *local_trees;
720
721 /* All data persistent across whole duration of output block
722 can go here. */
723 struct obstack obstack;
724 };
725
726
727 /* Data and descriptors used when reading from an LTO file. */
728 class data_in
729 {
730 public:
731 /* The global decls and types. */
732 struct lto_file_decl_data *file_data;
733
734 /* The string table. */
735 const char *strings;
736
737 /* The length of the string table. */
738 unsigned int strings_len;
739
740 /* Maps each reference number to the resolution done by the linker. */
741 vec<ld_plugin_symbol_resolution_t> globals_resolution;
742
743 /* Cache of pickled nodes. */
744 struct streamer_tree_cache_d *reader_cache;
745
746 /* Cache of source code location. */
747 lto_location_cache location_cache;
748 };
749
750
751 /* In lto-section-in.c */
752 extern class lto_input_block * lto_create_simple_input_block (
753 struct lto_file_decl_data *,
754 enum lto_section_type, const char **, size_t *);
755 extern void
756 lto_destroy_simple_input_block (struct lto_file_decl_data *,
757 enum lto_section_type,
758 class lto_input_block *, const char *, size_t);
759 extern void lto_set_in_hooks (struct lto_file_decl_data **,
760 lto_get_section_data_f *,
761 lto_free_section_data_f *);
762 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
763 extern const char *lto_get_section_data (struct lto_file_decl_data *,
764 enum lto_section_type,
765 const char *, int, size_t *,
766 bool decompress = false);
767 extern const char *lto_get_summary_section_data (struct lto_file_decl_data *,
768 enum lto_section_type,
769 size_t *);
770 extern const char *lto_get_raw_section_data (struct lto_file_decl_data *,
771 enum lto_section_type,
772 const char *, int, size_t *);
773 extern void lto_free_section_data (struct lto_file_decl_data *,
774 enum lto_section_type,
775 const char *, const char *, size_t,
776 bool decompress = false);
777 extern void lto_free_raw_section_data (struct lto_file_decl_data *,
778 enum lto_section_type,
779 const char *, const char *, size_t);
780 extern htab_t lto_create_renaming_table (void);
781 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
782 const char *, const char *);
783 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
784 const char *);
785 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
786 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
787 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
788 struct lto_file_decl_data *, tree);
789 extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
790 extern void lto_free_function_in_decl_state_for_node (symtab_node *);
791 extern void lto_section_overrun (class lto_input_block *) ATTRIBUTE_NORETURN;
792 extern void lto_value_range_error (const char *,
793 HOST_WIDE_INT, HOST_WIDE_INT,
794 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
795
796 /* In lto-section-out.c */
797 extern void lto_begin_section (const char *, bool);
798 extern void lto_end_section (void);
799 extern void lto_write_data (const void *, unsigned int);
800 extern void lto_write_raw_data (const void *, unsigned int);
801 extern void lto_write_stream (struct lto_output_stream *);
802 extern struct lto_simple_output_block *lto_create_simple_output_block (
803 enum lto_section_type);
804 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
805 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
806 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
807 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
808 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
809 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
810 extern void lto_record_function_out_decl_state (tree,
811 struct lto_out_decl_state *);
812 extern void lto_append_block (struct lto_output_stream *);
813
814
815 /* In lto-streamer.c. */
816
817 /* Set when streaming LTO for offloading compiler. */
818 extern bool lto_stream_offload_p;
819
820 extern const char *lto_tag_name (enum LTO_tags);
821 extern char *lto_get_section_name (int, const char *, int,
822 struct lto_file_decl_data *);
823 extern void print_lto_report (const char *);
824 extern void lto_streamer_init (void);
825 extern bool gate_lto_out (void);
826 extern void lto_check_version (int, int, const char *);
827 extern void lto_streamer_hooks_init (void);
828
829 /* In lto-streamer-in.c */
830 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
831 extern void lto_reader_init (void);
832 extern void lto_free_file_name_hash (void);
833 extern void lto_input_function_body (struct lto_file_decl_data *,
834 struct cgraph_node *,
835 const char *);
836 extern void lto_input_variable_constructor (struct lto_file_decl_data *,
837 struct varpool_node *,
838 const char *);
839 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
840 const char *);
841 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
842 extern void lto_input_mode_table (struct lto_file_decl_data *);
843 extern class data_in *lto_data_in_create (struct lto_file_decl_data *,
844 const char *, unsigned,
845 vec<ld_plugin_symbol_resolution_t> );
846 extern void lto_data_in_delete (class data_in *);
847 extern void lto_input_data_block (class lto_input_block *, void *, size_t);
848 void lto_input_location (location_t *, struct bitpack_d *, class data_in *);
849 location_t stream_input_location_now (struct bitpack_d *bp,
850 class data_in *data);
851 tree lto_input_tree_ref (class lto_input_block *, class data_in *,
852 struct function *, enum LTO_tags);
853 void lto_tag_check_set (enum LTO_tags, int, ...);
854 void lto_init_eh (void);
855 hashval_t lto_input_scc (class lto_input_block *, class data_in *,
856 unsigned *, unsigned *, bool);
857 tree lto_input_tree_1 (class lto_input_block *, class data_in *,
858 enum LTO_tags, hashval_t hash);
859 tree lto_input_tree (class lto_input_block *, class data_in *);
860 tree stream_read_tree_ref (class lto_input_block *, class data_in *);
861
862
863 /* In lto-streamer-out.c */
864 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
865 extern struct output_block *create_output_block (enum lto_section_type);
866 extern void destroy_output_block (struct output_block *);
867 extern void lto_output_tree (struct output_block *, tree, bool, bool);
868 extern void stream_write_tree_ref (struct output_block *, tree);
869 extern void lto_output_var_decl_ref (struct lto_out_decl_state *,
870 struct lto_output_stream *, tree);
871 extern void lto_output_fn_decl_ref (struct lto_out_decl_state *,
872 struct lto_output_stream *, tree);
873 extern tree lto_input_var_decl_ref (lto_input_block *, lto_file_decl_data *);
874 extern tree lto_input_fn_decl_ref (lto_input_block *, lto_file_decl_data *);
875 extern void lto_output_toplevel_asms (void);
876 extern void produce_asm (struct output_block *ob, tree fn);
877 extern void lto_output ();
878 extern void produce_asm_for_decls ();
879 void lto_output_decl_state_streams (struct output_block *,
880 struct lto_out_decl_state *);
881 void lto_output_decl_state_refs (struct output_block *,
882 struct lto_output_stream *,
883 struct lto_out_decl_state *);
884 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
885 void lto_output_init_mode_table (void);
886 void lto_prepare_function_for_streaming (cgraph_node *);
887
888
889 /* In lto-cgraph.c */
890 extern bool asm_nodes_output;
891 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
892 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
893 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
894 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
895 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
896 struct cgraph_node *);
897 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
898 symtab_node *);
899 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
900 symtab_node *);
901
902 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
903 varpool_node *);
904 void output_symtab (void);
905 void input_symtab (void);
906 void output_offload_tables (void);
907 void input_offload_tables (bool);
908 bool referenced_from_other_partition_p (struct ipa_ref_list *,
909 lto_symtab_encoder_t);
910 bool reachable_from_other_partition_p (struct cgraph_node *,
911 lto_symtab_encoder_t);
912 bool referenced_from_this_partition_p (symtab_node *,
913 lto_symtab_encoder_t);
914 bool reachable_from_this_partition_p (struct cgraph_node *,
915 lto_symtab_encoder_t);
916 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
917 void select_what_to_stream (void);
918
919 /* In options-save.c. */
920 void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
921 struct cl_target_option *);
922
923 void cl_target_option_stream_in (class data_in *,
924 struct bitpack_d *,
925 struct cl_target_option *);
926
927 void cl_optimization_stream_out (struct output_block *,
928 struct bitpack_d *, struct cl_optimization *);
929
930 void cl_optimization_stream_in (class data_in *,
931 struct bitpack_d *, struct cl_optimization *);
932
933
934
935 /* In lto-opts.c. */
936 extern void lto_write_options (void);
937
938
939 /* Statistics gathered during LTO, WPA and LTRANS. */
940 extern struct lto_stats_d lto_stats;
941
942 /* Section names corresponding to the values of enum lto_section_type. */
943 extern const char *lto_section_name[];
944
945 /* Holds all the out decl states of functions output so far in the
946 current output file. */
947 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
948
949 /* Return true if LTO tag TAG corresponds to a tree code. */
950 static inline bool
951 lto_tag_is_tree_code_p (enum LTO_tags tag)
952 {
953 return tag > LTO_first_tree_tag && (unsigned) tag <= MAX_TREE_CODES;
954 }
955
956
957 /* Return true if LTO tag TAG corresponds to a gimple code. */
958 static inline bool
959 lto_tag_is_gimple_code_p (enum LTO_tags tag)
960 {
961 return (unsigned) tag >= LTO_first_gimple_tag
962 && (unsigned) tag
963 < LTO_first_gimple_tag + LAST_AND_UNUSED_GIMPLE_CODE;
964 }
965
966
967 /* Return the LTO tag corresponding to gimple code CODE. See enum
968 LTO_tags for details on the conversion. */
969 static inline enum LTO_tags
970 lto_gimple_code_to_tag (enum gimple_code code)
971 {
972 return (enum LTO_tags) ((unsigned) code + LTO_first_gimple_tag);
973 }
974
975
976 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
977 details on the conversion. */
978 static inline enum gimple_code
979 lto_tag_to_gimple_code (enum LTO_tags tag)
980 {
981 gcc_assert (lto_tag_is_gimple_code_p (tag));
982 return (enum gimple_code) ((unsigned) tag - LTO_first_gimple_tag);
983 }
984
985
986 /* Return the LTO tag corresponding to tree code CODE. See enum
987 LTO_tags for details on the conversion. */
988 static inline enum LTO_tags
989 lto_tree_code_to_tag (enum tree_code code)
990 {
991 return (enum LTO_tags) ((unsigned) code + LTO_first_tree_tag);
992 }
993
994
995 /* Return the tree code corresponding to TAG. See enum LTO_tags for
996 details on the conversion. */
997 static inline enum tree_code
998 lto_tag_to_tree_code (enum LTO_tags tag)
999 {
1000 gcc_assert (lto_tag_is_tree_code_p (tag));
1001 return (enum tree_code) ((unsigned) tag - LTO_first_tree_tag);
1002 }
1003
1004 /* Check that tag ACTUAL == EXPECTED. */
1005 static inline void
1006 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
1007 {
1008 if (actual != expected)
1009 internal_error ("bytecode stream: expected tag %s instead of %s",
1010 lto_tag_name (expected), lto_tag_name (actual));
1011 }
1012
1013 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
1014 static inline void
1015 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
1016 enum LTO_tags tag2)
1017 {
1018 if (actual < tag1 || actual > tag2)
1019 internal_error ("bytecode stream: tag %s is not in the expected range "
1020 "[%s, %s]",
1021 lto_tag_name (actual),
1022 lto_tag_name (tag1),
1023 lto_tag_name (tag2));
1024 }
1025
1026 /* Initialize an lto_out_decl_buffer ENCODER. */
1027 static inline void
1028 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1029 {
1030 encoder->tree_hash_table = new hash_map<tree, unsigned> (251);
1031 encoder->trees.create (0);
1032 }
1033
1034
1035 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1036 memory used by ENCODER is not freed by this function. */
1037 static inline void
1038 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1039 {
1040 /* Hash table may be delete already. */
1041 delete encoder->tree_hash_table;
1042 encoder->tree_hash_table = NULL;
1043 encoder->trees.release ();
1044 }
1045
1046 /* Return the number of trees encoded in ENCODER. */
1047 static inline unsigned int
1048 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1049 {
1050 return encoder->trees.length ();
1051 }
1052
1053 /* Return the IDX-th tree in ENCODER. */
1054 static inline tree
1055 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1056 unsigned int idx)
1057 {
1058 return encoder->trees[idx];
1059 }
1060
1061 /* Return number of encoded nodes in ENCODER. */
1062 static inline int
1063 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1064 {
1065 return encoder->nodes.length ();
1066 }
1067
1068 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1069 #define LCC_NOT_FOUND (-1)
1070
1071 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1072 or LCC_NOT_FOUND if it is not there. */
1073
1074 static inline int
1075 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1076 symtab_node *node)
1077 {
1078 size_t *slot = encoder->map->get (node);
1079 return (slot && *slot ? *(slot) - 1 : LCC_NOT_FOUND);
1080 }
1081
1082 /* Return true if iterator LSE points to nothing. */
1083 static inline bool
1084 lsei_end_p (lto_symtab_encoder_iterator lsei)
1085 {
1086 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1087 }
1088
1089 /* Advance iterator LSE. */
1090 static inline void
1091 lsei_next (lto_symtab_encoder_iterator *lsei)
1092 {
1093 lsei->index++;
1094 }
1095
1096 /* Return the node pointed to by LSI. */
1097 static inline symtab_node *
1098 lsei_node (lto_symtab_encoder_iterator lsei)
1099 {
1100 return lsei.encoder->nodes[lsei.index].node;
1101 }
1102
1103 /* Return the node pointed to by LSI. */
1104 static inline struct cgraph_node *
1105 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1106 {
1107 return dyn_cast<cgraph_node *> (lsei.encoder->nodes[lsei.index].node);
1108 }
1109
1110 /* Return the node pointed to by LSI. */
1111 static inline varpool_node *
1112 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1113 {
1114 return dyn_cast<varpool_node *> (lsei.encoder->nodes[lsei.index].node);
1115 }
1116
1117 /* Return the cgraph node corresponding to REF using ENCODER. */
1118
1119 static inline symtab_node *
1120 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1121 {
1122 if (ref == LCC_NOT_FOUND)
1123 return NULL;
1124
1125 return encoder->nodes[ref].node;
1126 }
1127
1128 /* Return an iterator to the first node in LSI. */
1129 static inline lto_symtab_encoder_iterator
1130 lsei_start (lto_symtab_encoder_t encoder)
1131 {
1132 lto_symtab_encoder_iterator lsei;
1133
1134 lsei.encoder = encoder;
1135 lsei.index = 0;
1136 return lsei;
1137 }
1138
1139 /* Advance iterator LSE. */
1140 static inline void
1141 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1142 {
1143 lsei_next (lsei);
1144 while (!lsei_end_p (*lsei)
1145 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1146 lsei_next (lsei);
1147 }
1148
1149 /* Return an iterator to the first node in LSI. */
1150 static inline lto_symtab_encoder_iterator
1151 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1152 {
1153 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1154
1155 if (lsei_end_p (lsei))
1156 return lsei;
1157 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1158 lsei_next_in_partition (&lsei);
1159
1160 return lsei;
1161 }
1162
1163 /* Advance iterator LSE. */
1164 static inline void
1165 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1166 {
1167 lsei_next (lsei);
1168 while (!lsei_end_p (*lsei)
1169 && (!is_a <cgraph_node *> (lsei_node (*lsei))
1170 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1171 lsei_next (lsei);
1172 }
1173
1174 /* Return an iterator to the first node in LSI. */
1175 static inline lto_symtab_encoder_iterator
1176 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1177 {
1178 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1179
1180 if (lsei_end_p (lsei))
1181 return lsei;
1182 if (!is_a <cgraph_node *> (lsei_node (lsei))
1183 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1184 lsei_next_function_in_partition (&lsei);
1185
1186 return lsei;
1187 }
1188
1189 /* Advance iterator LSE. */
1190 static inline void
1191 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1192 {
1193 lsei_next (lsei);
1194 while (!lsei_end_p (*lsei)
1195 && (!is_a <varpool_node *> (lsei_node (*lsei))
1196 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1197 lsei_next (lsei);
1198 }
1199
1200 /* Return an iterator to the first node in LSI. */
1201 static inline lto_symtab_encoder_iterator
1202 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1203 {
1204 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1205
1206 if (lsei_end_p (lsei))
1207 return lsei;
1208 if (!is_a <varpool_node *> (lsei_node (lsei))
1209 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1210 lsei_next_variable_in_partition (&lsei);
1211
1212 return lsei;
1213 }
1214
1215 /* Entry for the delayed registering of decl -> DIE references. */
1216 struct dref_entry {
1217 tree decl;
1218 const char *sym;
1219 unsigned HOST_WIDE_INT off;
1220 };
1221
1222 extern vec<dref_entry> dref_queue;
1223
1224 extern FILE *streamer_dump_file;
1225
1226 #endif /* GCC_LTO_STREAMER_H */