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