Fix previous commit
[official-gcc.git] / gcc / lto-streamer.h
blob9f787c4bd1efcce93736a1f3538f3cbc91f1fe93
1 /* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
4 Copyright (C) 2009-2019 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"
30 /* The encoding for a function consists of the following sections:
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.
45 1) THE HEADER.
46 2-6) THE GLOBAL DECLS AND TYPES.
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.
52 7) THE LABEL NAMES.
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.
61 9) THE CFG.
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.
67 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
69 The gimple consists of a set of records.
71 THE FUNCTION
73 At the top level of (8) is the function. It consists of five
74 pieces:
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.
83 last_basic_block - in uleb128 form.
85 basic blocks - This is the set of basic blocks.
87 zero - The termination of the basic blocks.
89 BASIC BLOCKS
91 There are two forms of basic blocks depending on if they are
92 empty or not.
94 The basic block consists of:
96 LTO_bb1 or LTO_bb0 - The tag.
98 bb->index - the index in uleb128 form.
100 #succs - The number of successors un uleb128 form.
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.
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).
114 zero - This is only present for LTO_BB1 and is used
115 to terminate the statements and exception
116 regions within this block.
118 12) STRINGS
120 String are represented in the table as pairs, a length in ULEB128
121 form followed by the data for the string. */
123 #define LTO_major_version 9
124 #define LTO_minor_version 0
126 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 /* Special for streamer. Reference to previously-streamed node. */
138 LTO_tree_pickle_reference,
140 /* Reserve enough entries to fit all the tree and gimple codes handled
141 by the streamer. This guarantees that:
143 1- Given a tree code C:
144 enum LTO_tags tag == C + 1
146 2- Given a gimple code C:
147 enum LTO_tags tag == C + NUM_TREE_CODES + 1
149 Conversely, to map between LTO tags and tree/gimple codes, the
150 reverse operation must be applied. */
151 LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
152 LTO_bb1,
154 /* EH region holding the previous statement. */
155 LTO_eh_region,
157 /* Shared INTEGER_CST node. */
158 LTO_integer_cst,
160 /* Function body. */
161 LTO_function,
163 /* EH table. */
164 LTO_eh_table,
166 /* EH region types. These mirror enum eh_region_type. */
167 LTO_ert_cleanup,
168 LTO_ert_try,
169 LTO_ert_allowed_exceptions,
170 LTO_ert_must_not_throw,
172 /* EH landing pad. */
173 LTO_eh_landing_pad,
175 /* EH try/catch node. */
176 LTO_eh_catch,
178 /* Special for global streamer. A blob of unnamed tree nodes. */
179 LTO_tree_scc,
181 /* References to indexable tree nodes. These objects are stored in
182 tables that are written separately from the function bodies that
183 reference them. This way they can be instantiated even when the
184 referencing functions aren't (e.g., during WPA) and it also allows
185 functions to be copied from one file to another without having
186 to unpickle the body first (the references are location
187 independent).
189 NOTE, do not regroup these values as the grouping is exposed
190 in the range checks done in lto_input_tree. */
191 LTO_field_decl_ref, /* Do not change. */
192 LTO_function_decl_ref,
193 LTO_label_decl_ref,
194 LTO_namespace_decl_ref,
195 LTO_result_decl_ref,
196 LTO_ssa_name_ref,
197 LTO_type_decl_ref,
198 LTO_type_ref,
199 LTO_const_decl_ref,
200 LTO_imported_decl_ref,
201 LTO_translation_unit_decl_ref,
202 LTO_global_decl_ref,
203 LTO_namelist_decl_ref, /* Do not change. */
205 /* This tag must always be last. */
206 LTO_NUM_TAGS
210 /* Set of section types that are in an LTO file. This list will grow
211 as the number of IPA passes grows since each IPA pass will need its
212 own section type to store its summary information.
214 When adding a new section type, you must also extend the
215 LTO_SECTION_NAME array in lto-section-in.c. */
216 enum lto_section_type
218 LTO_section_decls = 0,
219 LTO_section_function_body,
220 LTO_section_static_initializer,
221 LTO_section_symtab,
222 LTO_section_refs,
223 LTO_section_asm,
224 LTO_section_jump_functions,
225 LTO_section_ipa_pure_const,
226 LTO_section_ipa_reference,
227 LTO_section_ipa_profile,
228 LTO_section_symtab_nodes,
229 LTO_section_opts,
230 LTO_section_cgraph_opt_sum,
231 LTO_section_ipa_fn_summary,
232 LTO_section_ipcp_transform,
233 LTO_section_ipa_icf,
234 LTO_section_offload_table,
235 LTO_section_mode_table,
236 LTO_section_ipa_hsa,
237 LTO_section_lto,
238 LTO_section_ipa_sra,
239 LTO_N_SECTION_TYPES /* Must be last. */
242 /* Indices to the various function, type and symbol streams. */
243 enum lto_decl_stream_e_t
245 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
246 LTO_DECL_STREAM_FIELD_DECL,
247 LTO_DECL_STREAM_FN_DECL,
248 LTO_DECL_STREAM_VAR_DECL,
249 LTO_DECL_STREAM_TYPE_DECL,
250 LTO_DECL_STREAM_NAMESPACE_DECL,
251 LTO_DECL_STREAM_LABEL_DECL,
252 LTO_N_DECL_STREAMS
255 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
258 /* Macro to define convenience functions for type and decl streams
259 in lto_file_decl_data. */
260 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
261 static inline tree \
262 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
263 unsigned int idx) \
265 struct lto_in_decl_state *state = data->current_decl_state; \
266 return (*state->streams[LTO_DECL_STREAM_## UPPER_NAME])[idx]; \
269 static inline unsigned int \
270 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
272 struct lto_in_decl_state *state = data->current_decl_state; \
273 return vec_safe_length (state->streams[LTO_DECL_STREAM_## UPPER_NAME]); \
277 /* Return a char pointer to the start of a data stream for an lto pass
278 or function. The first parameter is the file data that contains
279 the information. The second parameter is the type of information
280 to be obtained. The third parameter is the name of the function
281 and is only used when finding a function body; otherwise it is
282 NULL. The fourth parameter is the length of the data returned. */
283 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
284 enum lto_section_type,
285 const char *,
286 size_t *);
288 /* Return the data found from the above call. The first three
289 parameters are the same as above. The fourth parameter is the data
290 itself and the fifth is the length of the data. */
291 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
292 enum lto_section_type,
293 const char *,
294 const char *,
295 size_t);
297 /* The location cache holds expanded locations for streamed in trees.
298 This is done to reduce memory usage of libcpp linemap that strongly preffers
299 locations to be inserted in the soruce order. */
301 class lto_location_cache
303 public:
304 /* Apply all changes in location cache. Add locations into linemap and patch
305 trees. */
306 bool apply_location_cache ();
307 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
308 void accept_location_cache ();
309 /* Tree merging did suceed; throw away recent changes. */
310 void revert_location_cache ();
311 void input_location (location_t *loc, struct bitpack_d *bp,
312 class data_in *data_in);
313 lto_location_cache ()
314 : loc_cache (), accepted_length (0), current_file (NULL), current_line (0),
315 current_col (0), current_sysp (false), current_loc (UNKNOWN_LOCATION)
317 gcc_assert (!current_cache);
318 current_cache = this;
320 ~lto_location_cache ()
322 apply_location_cache ();
323 gcc_assert (current_cache == this);
324 current_cache = NULL;
327 /* There can be at most one instance of location cache (combining multiple
328 would bring it out of sync with libcpp linemap); point to current
329 one. */
330 static lto_location_cache *current_cache;
332 private:
333 static int cmp_loc (const void *pa, const void *pb);
335 struct cached_location
337 const char *file;
338 location_t *loc;
339 int line, col;
340 bool sysp;
343 /* The location cache. */
345 auto_vec<cached_location> loc_cache;
347 /* Accepted entries are ones used by trees that are known to be not unified
348 by tree merging. */
350 int accepted_length;
352 /* Bookkeeping to remember state in between calls to lto_apply_location_cache
353 When streaming gimple, the location cache is not used and thus
354 lto_apply_location_cache happens per location basis. It is then
355 useful to avoid redundant calls of linemap API. */
357 const char *current_file;
358 int current_line;
359 int current_col;
360 bool current_sysp;
361 location_t current_loc;
364 /* Structure used as buffer for reading an LTO file. */
365 class lto_input_block
367 public:
368 /* Special constructor for the string table, it abuses this to
369 do random access but use the uhwi decoder. */
370 lto_input_block (const char *data_, unsigned int p_, unsigned int len_,
371 const unsigned char *mode_table_)
372 : data (data_), mode_table (mode_table_), p (p_), len (len_) {}
373 lto_input_block (const char *data_, unsigned int len_,
374 const unsigned char *mode_table_)
375 : data (data_), mode_table (mode_table_), p (0), len (len_) {}
377 const char *data;
378 const unsigned char *mode_table;
379 unsigned int p;
380 unsigned int len;
383 /* Compression algorithm used for compression of LTO bytecode. */
385 enum lto_compression
387 ZLIB,
388 ZSTD
391 /* Structure that represents LTO ELF section with information
392 about the format. */
394 struct lto_section
396 int16_t major_version;
397 int16_t minor_version;
398 unsigned char slim_object;
400 /* Flags is a private field that is not defined publicly. */
401 uint16_t flags;
403 /* Set compression to FLAGS. */
404 inline void set_compression (lto_compression c)
406 flags = c;
409 /* Get compression from FLAGS. */
410 inline lto_compression get_compression ()
412 return (lto_compression) flags;
416 STATIC_ASSERT (sizeof (lto_section) == 8);
418 /* The is the first part of the record in an LTO file for many of the
419 IPA passes. */
420 struct lto_simple_header
422 /* Size of main gimple body of function. */
423 int32_t main_size;
426 struct lto_simple_header_with_strings : lto_simple_header
428 /* Size of the string table. */
429 int32_t string_size;
432 /* The header for a function body. */
433 struct lto_function_header : lto_simple_header_with_strings
435 /* Size of the cfg. */
436 int32_t cfg_size;
440 /* Structure describing a symbol section. */
441 struct lto_decl_header : lto_simple_header_with_strings
443 /* Size of region for decl state. */
444 int32_t decl_state_size;
446 /* Number of nodes in globals stream. */
447 int32_t num_nodes;
451 /* Statistics gathered during LTO, WPA and LTRANS. */
452 struct lto_stats_d
454 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
455 unsigned HOST_WIDE_INT num_output_symtab_nodes;
456 unsigned HOST_WIDE_INT num_input_files;
457 unsigned HOST_WIDE_INT num_output_files;
458 unsigned HOST_WIDE_INT num_cgraph_partitions;
459 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
460 unsigned HOST_WIDE_INT num_function_bodies;
461 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
462 unsigned HOST_WIDE_INT num_output_il_bytes;
463 unsigned HOST_WIDE_INT num_compressed_il_bytes;
464 unsigned HOST_WIDE_INT num_input_il_bytes;
465 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
466 unsigned HOST_WIDE_INT num_tree_bodies_output;
467 unsigned HOST_WIDE_INT num_pickle_refs_output;
470 /* Entry of LTO symtab encoder. */
471 struct lto_encoder_entry
473 symtab_node *node;
474 /* Is the node in this partition (i.e. ltrans of this partition will
475 be responsible for outputting it)? */
476 unsigned int in_partition:1;
477 /* Do we encode body in this partition? */
478 unsigned int body:1;
479 /* Do we encode initializer in this partition?
480 For example the readonly variable initializers are encoded to aid
481 constant folding even if they are not in the partition. */
482 unsigned int initializer:1;
486 /* Encoder data structure used to stream callgraph nodes. */
487 struct lto_symtab_encoder_d
489 vec<lto_encoder_entry> nodes;
490 hash_map<symtab_node *, size_t> *map;
493 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
495 /* Iterator structure for cgraph node sets. */
496 struct lto_symtab_encoder_iterator
498 lto_symtab_encoder_t encoder;
499 unsigned index;
504 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
506 struct lto_tree_ref_encoder
508 hash_map<tree, unsigned> *tree_hash_table; /* Maps pointers to indices. */
509 vec<tree> trees; /* Maps indices to pointers. */
513 /* Structure to hold states of input scope. */
514 struct GTY((for_user)) lto_in_decl_state
516 /* Array of lto_in_decl_buffers to store type and decls streams. */
517 vec<tree, va_gc> *streams[LTO_N_DECL_STREAMS];
519 /* If this in-decl state is associated with a function. FN_DECL
520 point to the FUNCTION_DECL. */
521 tree fn_decl;
523 /* True if decl state is compressed. */
524 bool compressed;
527 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
529 struct decl_state_hasher : ggc_ptr_hash<lto_in_decl_state>
531 static hashval_t
532 hash (lto_in_decl_state *s)
534 return htab_hash_pointer (s->fn_decl);
537 static bool
538 equal (lto_in_decl_state *a, lto_in_decl_state *b)
540 return a->fn_decl == b->fn_decl;
544 /* The structure that holds all of the vectors of global types,
545 decls and cgraph nodes used in the serialization of this file. */
546 struct lto_out_decl_state
548 /* The buffers contain the sets of decls of various kinds and types we have
549 seen so far and the indexes assigned to them. */
550 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
552 /* Encoder for cgraph nodes. */
553 lto_symtab_encoder_t symtab_node_encoder;
555 /* If this out-decl state belongs to a function, fn_decl points to that
556 function. Otherwise, it is NULL. */
557 tree fn_decl;
559 /* True if decl state is compressed. */
560 bool compressed;
563 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
566 /* Compact representation of a index <-> resolution pair. Unpacked to an
567 vector later. */
568 struct res_pair
570 ld_plugin_symbol_resolution_t res;
571 unsigned index;
575 /* One of these is allocated for each object file that being compiled
576 by lto. This structure contains the tables that are needed by the
577 serialized functions and ipa passes to connect themselves to the
578 global types and decls as they are reconstituted. */
579 struct GTY(()) lto_file_decl_data
581 /* Decl state currently used. */
582 struct lto_in_decl_state *current_decl_state;
584 /* Decl state corresponding to regions outside of any functions
585 in the compilation unit. */
586 struct lto_in_decl_state *global_decl_state;
588 /* Table of cgraph nodes present in this file. */
589 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
591 /* Hash table maps lto-related section names to location in file. */
592 hash_table<decl_state_hasher> *function_decl_states;
594 /* The .o file that these offsets relate to. */
595 const char *GTY((skip)) file_name;
597 /* Hash table maps lto-related section names to location in file. */
598 htab_t GTY((skip)) section_hash_table;
600 /* Hash new name of renamed global declaration to its original name. */
601 htab_t GTY((skip)) renaming_hash_table;
603 /* Linked list used temporarily in reader */
604 struct lto_file_decl_data *next;
606 /* Sub ID for merged objects. */
607 unsigned HOST_WIDE_INT id;
609 /* Symbol resolutions for this file */
610 vec<res_pair> GTY((skip)) respairs;
611 unsigned max_index;
613 gcov_summary GTY((skip)) profile_info;
615 /* Map assigning declarations their resolutions. */
616 hash_map<tree, ld_plugin_symbol_resolution> * GTY((skip)) resolution_map;
618 /* Mode translation table. */
619 const unsigned char *mode_table;
621 /* Read LTO section. */
622 lto_section lto_section_header;
625 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
627 struct lto_char_ptr_base
629 char *ptr;
632 /* An incore byte stream to buffer the various parts of the function.
633 The entire structure should be zeroed when created. The record
634 consists of a set of blocks. The first sizeof (ptr) bytes are used
635 as a chain, and the rest store the bytes to be written. */
636 struct lto_output_stream
638 /* The pointer to the first block in the stream. */
639 struct lto_char_ptr_base * first_block;
641 /* The pointer to the last and current block in the stream. */
642 struct lto_char_ptr_base * current_block;
644 /* The pointer to where the next char should be written. */
645 char * current_pointer;
647 /* The number of characters left in the current block. */
648 unsigned int left_in_block;
650 /* The block size of the last block allocated. */
651 unsigned int block_size;
653 /* The total number of characters written. */
654 unsigned int total_size;
657 /* A simple output block. This can be used for simple IPA passes that
658 do not need more than one stream. */
659 struct lto_simple_output_block
661 enum lto_section_type section_type;
662 struct lto_out_decl_state *decl_state;
664 /* The stream that the main tree codes are written to. */
665 struct lto_output_stream *main_stream;
668 /* String hashing. */
670 struct string_slot
672 const char *s;
673 int len;
674 unsigned int slot_num;
677 /* Hashtable helpers. */
679 struct string_slot_hasher : nofree_ptr_hash <string_slot>
681 static inline hashval_t hash (const string_slot *);
682 static inline bool equal (const string_slot *, const string_slot *);
685 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
686 to support strings that may not end in '\0'. */
688 inline hashval_t
689 string_slot_hasher::hash (const string_slot *ds)
691 hashval_t r = ds->len;
692 int i;
694 for (i = 0; i < ds->len; i++)
695 r = r * 67 + (unsigned)ds->s[i] - 113;
696 return r;
699 /* Returns nonzero if DS1 and DS2 are equal. */
701 inline bool
702 string_slot_hasher::equal (const string_slot *ds1, const string_slot *ds2)
704 if (ds1->len == ds2->len)
705 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
707 return 0;
710 /* Data structure holding all the data and descriptors used when writing
711 an LTO file. */
712 struct output_block
714 enum lto_section_type section_type;
715 struct lto_out_decl_state *decl_state;
717 /* The stream that the main tree codes are written to. */
718 struct lto_output_stream *main_stream;
720 /* The stream that contains the string table. */
721 struct lto_output_stream *string_stream;
723 /* The stream that contains the cfg. */
724 struct lto_output_stream *cfg_stream;
726 /* The hash table that contains the set of strings we have seen so
727 far and the indexes assigned to them. */
728 hash_table<string_slot_hasher> *string_hash_table;
730 /* The current symbol that we are currently serializing. Null
731 if we are serializing something else. */
732 symtab_node *symbol;
734 /* These are the last file and line that were seen in the stream.
735 If the current node differs from these, it needs to insert
736 something into the stream and fix these up. */
737 const char *current_file;
738 int current_line;
739 int current_col;
740 bool current_sysp;
742 /* Cache of nodes written in this section. */
743 struct streamer_tree_cache_d *writer_cache;
745 /* All data persistent across whole duration of output block
746 can go here. */
747 struct obstack obstack;
751 /* Data and descriptors used when reading from an LTO file. */
752 class data_in
754 public:
755 /* The global decls and types. */
756 struct lto_file_decl_data *file_data;
758 /* The string table. */
759 const char *strings;
761 /* The length of the string table. */
762 unsigned int strings_len;
764 /* Maps each reference number to the resolution done by the linker. */
765 vec<ld_plugin_symbol_resolution_t> globals_resolution;
767 /* Cache of pickled nodes. */
768 struct streamer_tree_cache_d *reader_cache;
770 /* Cache of source code location. */
771 lto_location_cache location_cache;
775 /* In lto-section-in.c */
776 extern class lto_input_block * lto_create_simple_input_block (
777 struct lto_file_decl_data *,
778 enum lto_section_type, const char **, size_t *);
779 extern void
780 lto_destroy_simple_input_block (struct lto_file_decl_data *,
781 enum lto_section_type,
782 class lto_input_block *, const char *, size_t);
783 extern void lto_set_in_hooks (struct lto_file_decl_data **,
784 lto_get_section_data_f *,
785 lto_free_section_data_f *);
786 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
787 extern const char *lto_get_section_data (struct lto_file_decl_data *,
788 enum lto_section_type,
789 const char *, size_t *,
790 bool decompress = false);
791 extern const char *lto_get_raw_section_data (struct lto_file_decl_data *,
792 enum lto_section_type,
793 const char *, size_t *);
794 extern void lto_free_section_data (struct lto_file_decl_data *,
795 enum lto_section_type,
796 const char *, const char *, size_t,
797 bool decompress = false);
798 extern void lto_free_raw_section_data (struct lto_file_decl_data *,
799 enum lto_section_type,
800 const char *, const char *, size_t);
801 extern htab_t lto_create_renaming_table (void);
802 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
803 const char *, const char *);
804 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
805 const char *);
806 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
807 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
808 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
809 struct lto_file_decl_data *, tree);
810 extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
811 extern void lto_free_function_in_decl_state_for_node (symtab_node *);
812 extern void lto_section_overrun (class lto_input_block *) ATTRIBUTE_NORETURN;
813 extern void lto_value_range_error (const char *,
814 HOST_WIDE_INT, HOST_WIDE_INT,
815 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
817 /* In lto-section-out.c */
818 extern void lto_begin_section (const char *, bool);
819 extern void lto_end_section (void);
820 extern void lto_write_data (const void *, unsigned int);
821 extern void lto_write_raw_data (const void *, unsigned int);
822 extern void lto_write_stream (struct lto_output_stream *);
823 extern bool lto_output_decl_index (struct lto_output_stream *,
824 struct lto_tree_ref_encoder *,
825 tree, unsigned int *);
826 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
827 struct lto_output_stream *, tree);
828 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
829 struct lto_output_stream *, tree);
830 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
831 struct lto_output_stream *, tree);
832 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
833 struct lto_output_stream *, tree);
834 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
835 struct lto_output_stream *, tree);
836 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
837 struct lto_output_stream *, tree);
838 extern struct lto_simple_output_block *lto_create_simple_output_block (
839 enum lto_section_type);
840 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
841 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
842 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
843 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
844 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
845 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
846 extern void lto_record_function_out_decl_state (tree,
847 struct lto_out_decl_state *);
848 extern void lto_append_block (struct lto_output_stream *);
851 /* In lto-streamer.c. */
853 /* Set when streaming LTO for offloading compiler. */
854 extern bool lto_stream_offload_p;
856 extern const char *lto_tag_name (enum LTO_tags);
857 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
858 extern void print_lto_report (const char *);
859 extern void lto_streamer_init (void);
860 extern bool gate_lto_out (void);
861 extern void lto_check_version (int, int, const char *);
862 extern void lto_streamer_hooks_init (void);
864 /* In lto-streamer-in.c */
865 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
866 extern void lto_reader_init (void);
867 extern void lto_input_function_body (struct lto_file_decl_data *,
868 struct cgraph_node *,
869 const char *);
870 extern void lto_input_variable_constructor (struct lto_file_decl_data *,
871 struct varpool_node *,
872 const char *);
873 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
874 const char *);
875 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
876 extern void lto_input_mode_table (struct lto_file_decl_data *);
877 extern class data_in *lto_data_in_create (struct lto_file_decl_data *,
878 const char *, unsigned,
879 vec<ld_plugin_symbol_resolution_t> );
880 extern void lto_data_in_delete (class data_in *);
881 extern void lto_input_data_block (class lto_input_block *, void *, size_t);
882 void lto_input_location (location_t *, struct bitpack_d *, class data_in *);
883 location_t stream_input_location_now (struct bitpack_d *bp,
884 class data_in *data);
885 tree lto_input_tree_ref (class lto_input_block *, class data_in *,
886 struct function *, enum LTO_tags);
887 void lto_tag_check_set (enum LTO_tags, int, ...);
888 void lto_init_eh (void);
889 hashval_t lto_input_scc (class lto_input_block *, class data_in *,
890 unsigned *, unsigned *);
891 tree lto_input_tree_1 (class lto_input_block *, class data_in *,
892 enum LTO_tags, hashval_t hash);
893 tree lto_input_tree (class lto_input_block *, class data_in *);
896 /* In lto-streamer-out.c */
897 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
898 extern struct output_block *create_output_block (enum lto_section_type);
899 extern void destroy_output_block (struct output_block *);
900 extern void lto_output_tree (struct output_block *, tree, bool, bool);
901 extern void lto_output_toplevel_asms (void);
902 extern void produce_asm (struct output_block *ob, tree fn);
903 extern void lto_output ();
904 extern void produce_asm_for_decls ();
905 void lto_output_decl_state_streams (struct output_block *,
906 struct lto_out_decl_state *);
907 void lto_output_decl_state_refs (struct output_block *,
908 struct lto_output_stream *,
909 struct lto_out_decl_state *);
910 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
911 void lto_output_init_mode_table (void);
912 void lto_prepare_function_for_streaming (cgraph_node *);
915 /* In lto-cgraph.c */
916 extern bool asm_nodes_output;
917 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
918 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
919 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
920 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
921 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
922 struct cgraph_node *);
923 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
924 symtab_node *);
925 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
926 symtab_node *);
928 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
929 varpool_node *);
930 void output_symtab (void);
931 void input_symtab (void);
932 void output_offload_tables (void);
933 void input_offload_tables (bool);
934 bool referenced_from_other_partition_p (struct ipa_ref_list *,
935 lto_symtab_encoder_t);
936 bool reachable_from_other_partition_p (struct cgraph_node *,
937 lto_symtab_encoder_t);
938 bool referenced_from_this_partition_p (symtab_node *,
939 lto_symtab_encoder_t);
940 bool reachable_from_this_partition_p (struct cgraph_node *,
941 lto_symtab_encoder_t);
942 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
943 void select_what_to_stream (void);
945 /* In options-save.c. */
946 void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
947 struct cl_target_option *);
949 void cl_target_option_stream_in (class data_in *,
950 struct bitpack_d *,
951 struct cl_target_option *);
953 void cl_optimization_stream_out (struct output_block *,
954 struct bitpack_d *, struct cl_optimization *);
956 void cl_optimization_stream_in (class data_in *,
957 struct bitpack_d *, struct cl_optimization *);
961 /* In lto-opts.c. */
962 extern void lto_write_options (void);
965 /* Statistics gathered during LTO, WPA and LTRANS. */
966 extern struct lto_stats_d lto_stats;
968 /* Section names corresponding to the values of enum lto_section_type. */
969 extern const char *lto_section_name[];
971 /* Holds all the out decl states of functions output so far in the
972 current output file. */
973 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
975 /* Return true if LTO tag TAG corresponds to a tree code. */
976 static inline bool
977 lto_tag_is_tree_code_p (enum LTO_tags tag)
979 return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
983 /* Return true if LTO tag TAG corresponds to a gimple code. */
984 static inline bool
985 lto_tag_is_gimple_code_p (enum LTO_tags tag)
987 return (unsigned) tag >= NUM_TREE_CODES + 2
988 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
992 /* Return the LTO tag corresponding to gimple code CODE. See enum
993 LTO_tags for details on the conversion. */
994 static inline enum LTO_tags
995 lto_gimple_code_to_tag (enum gimple_code code)
997 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
1001 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
1002 details on the conversion. */
1003 static inline enum gimple_code
1004 lto_tag_to_gimple_code (enum LTO_tags tag)
1006 gcc_assert (lto_tag_is_gimple_code_p (tag));
1007 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
1011 /* Return the LTO tag corresponding to tree code CODE. See enum
1012 LTO_tags for details on the conversion. */
1013 static inline enum LTO_tags
1014 lto_tree_code_to_tag (enum tree_code code)
1016 return (enum LTO_tags) ((unsigned) code + 2);
1020 /* Return the tree code corresponding to TAG. See enum LTO_tags for
1021 details on the conversion. */
1022 static inline enum tree_code
1023 lto_tag_to_tree_code (enum LTO_tags tag)
1025 gcc_assert (lto_tag_is_tree_code_p (tag));
1026 return (enum tree_code) ((unsigned) tag - 2);
1029 /* Check that tag ACTUAL == EXPECTED. */
1030 static inline void
1031 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
1033 if (actual != expected)
1034 internal_error ("bytecode stream: expected tag %s instead of %s",
1035 lto_tag_name (expected), lto_tag_name (actual));
1038 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
1039 static inline void
1040 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
1041 enum LTO_tags tag2)
1043 if (actual < tag1 || actual > tag2)
1044 internal_error ("bytecode stream: tag %s is not in the expected range "
1045 "[%s, %s]",
1046 lto_tag_name (actual),
1047 lto_tag_name (tag1),
1048 lto_tag_name (tag2));
1051 /* Initialize an lto_out_decl_buffer ENCODER. */
1052 static inline void
1053 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1055 encoder->tree_hash_table = new hash_map<tree, unsigned> (251);
1056 encoder->trees.create (0);
1060 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1061 memory used by ENCODER is not freed by this function. */
1062 static inline void
1063 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1065 /* Hash table may be delete already. */
1066 delete encoder->tree_hash_table;
1067 encoder->tree_hash_table = NULL;
1068 encoder->trees.release ();
1071 /* Return the number of trees encoded in ENCODER. */
1072 static inline unsigned int
1073 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1075 return encoder->trees.length ();
1078 /* Return the IDX-th tree in ENCODER. */
1079 static inline tree
1080 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1081 unsigned int idx)
1083 return encoder->trees[idx];
1086 /* Return number of encoded nodes in ENCODER. */
1087 static inline int
1088 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1090 return encoder->nodes.length ();
1093 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1094 #define LCC_NOT_FOUND (-1)
1096 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1097 or LCC_NOT_FOUND if it is not there. */
1099 static inline int
1100 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1101 symtab_node *node)
1103 size_t *slot = encoder->map->get (node);
1104 return (slot && *slot ? *(slot) - 1 : LCC_NOT_FOUND);
1107 /* Return true if iterator LSE points to nothing. */
1108 static inline bool
1109 lsei_end_p (lto_symtab_encoder_iterator lsei)
1111 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1114 /* Advance iterator LSE. */
1115 static inline void
1116 lsei_next (lto_symtab_encoder_iterator *lsei)
1118 lsei->index++;
1121 /* Return the node pointed to by LSI. */
1122 static inline symtab_node *
1123 lsei_node (lto_symtab_encoder_iterator lsei)
1125 return lsei.encoder->nodes[lsei.index].node;
1128 /* Return the node pointed to by LSI. */
1129 static inline struct cgraph_node *
1130 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1132 return dyn_cast<cgraph_node *> (lsei.encoder->nodes[lsei.index].node);
1135 /* Return the node pointed to by LSI. */
1136 static inline varpool_node *
1137 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1139 return dyn_cast<varpool_node *> (lsei.encoder->nodes[lsei.index].node);
1142 /* Return the cgraph node corresponding to REF using ENCODER. */
1144 static inline symtab_node *
1145 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1147 if (ref == LCC_NOT_FOUND)
1148 return NULL;
1150 return encoder->nodes[ref].node;
1153 /* Return an iterator to the first node in LSI. */
1154 static inline lto_symtab_encoder_iterator
1155 lsei_start (lto_symtab_encoder_t encoder)
1157 lto_symtab_encoder_iterator lsei;
1159 lsei.encoder = encoder;
1160 lsei.index = 0;
1161 return lsei;
1164 /* Advance iterator LSE. */
1165 static inline void
1166 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1168 lsei_next (lsei);
1169 while (!lsei_end_p (*lsei)
1170 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1171 lsei_next (lsei);
1174 /* Return an iterator to the first node in LSI. */
1175 static inline lto_symtab_encoder_iterator
1176 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1178 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1180 if (lsei_end_p (lsei))
1181 return lsei;
1182 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1183 lsei_next_in_partition (&lsei);
1185 return lsei;
1188 /* Advance iterator LSE. */
1189 static inline void
1190 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1192 lsei_next (lsei);
1193 while (!lsei_end_p (*lsei)
1194 && (!is_a <cgraph_node *> (lsei_node (*lsei))
1195 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1196 lsei_next (lsei);
1199 /* Return an iterator to the first node in LSI. */
1200 static inline lto_symtab_encoder_iterator
1201 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1203 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1205 if (lsei_end_p (lsei))
1206 return lsei;
1207 if (!is_a <cgraph_node *> (lsei_node (lsei))
1208 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1209 lsei_next_function_in_partition (&lsei);
1211 return lsei;
1214 /* Advance iterator LSE. */
1215 static inline void
1216 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1218 lsei_next (lsei);
1219 while (!lsei_end_p (*lsei)
1220 && (!is_a <varpool_node *> (lsei_node (*lsei))
1221 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1222 lsei_next (lsei);
1225 /* Return an iterator to the first node in LSI. */
1226 static inline lto_symtab_encoder_iterator
1227 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1229 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1231 if (lsei_end_p (lsei))
1232 return lsei;
1233 if (!is_a <varpool_node *> (lsei_node (lsei))
1234 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1235 lsei_next_variable_in_partition (&lsei);
1237 return lsei;
1240 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1241 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1242 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1243 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1244 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1245 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1246 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1248 /* Entry for the delayed registering of decl -> DIE references. */
1249 struct dref_entry {
1250 tree decl;
1251 const char *sym;
1252 unsigned HOST_WIDE_INT off;
1255 extern vec<dref_entry> dref_queue;
1257 extern FILE *streamer_dump_file;
1259 #endif /* GCC_LTO_STREAMER_H */