[Ada] Missing range check on assignment to bit-packed array
[official-gcc.git] / gcc / lto-streamer.h
blobf1593d64c7120aea3ddf2cc7221171bf6485fc9c
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_N_SECTION_TYPES /* Must be last. */
241 /* Indices to the various function, type and symbol streams. */
242 enum lto_decl_stream_e_t
244 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
245 LTO_DECL_STREAM_FIELD_DECL,
246 LTO_DECL_STREAM_FN_DECL,
247 LTO_DECL_STREAM_VAR_DECL,
248 LTO_DECL_STREAM_TYPE_DECL,
249 LTO_DECL_STREAM_NAMESPACE_DECL,
250 LTO_DECL_STREAM_LABEL_DECL,
251 LTO_N_DECL_STREAMS
254 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
257 /* Macro to define convenience functions for type and decl streams
258 in lto_file_decl_data. */
259 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
260 static inline tree \
261 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
262 unsigned int idx) \
264 struct lto_in_decl_state *state = data->current_decl_state; \
265 return (*state->streams[LTO_DECL_STREAM_## UPPER_NAME])[idx]; \
268 static inline unsigned int \
269 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
271 struct lto_in_decl_state *state = data->current_decl_state; \
272 return vec_safe_length (state->streams[LTO_DECL_STREAM_## UPPER_NAME]); \
276 /* Return a char pointer to the start of a data stream for an lto pass
277 or function. The first parameter is the file data that contains
278 the information. The second parameter is the type of information
279 to be obtained. The third parameter is the name of the function
280 and is only used when finding a function body; otherwise it is
281 NULL. The fourth parameter is the length of the data returned. */
282 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
283 enum lto_section_type,
284 const char *,
285 size_t *);
287 /* Return the data found from the above call. The first three
288 parameters are the same as above. The fourth parameter is the data
289 itself and the fifth is the length of the data. */
290 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
291 enum lto_section_type,
292 const char *,
293 const char *,
294 size_t);
296 /* The location cache holds expanded locations for streamed in trees.
297 This is done to reduce memory usage of libcpp linemap that strongly preffers
298 locations to be inserted in the soruce order. */
300 class lto_location_cache
302 public:
303 /* Apply all changes in location cache. Add locations into linemap and patch
304 trees. */
305 bool apply_location_cache ();
306 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
307 void accept_location_cache ();
308 /* Tree merging did suceed; throw away recent changes. */
309 void revert_location_cache ();
310 void input_location (location_t *loc, struct bitpack_d *bp,
311 struct data_in *data_in);
312 lto_location_cache ()
313 : loc_cache (), accepted_length (0), current_file (NULL), current_line (0),
314 current_col (0), current_sysp (false), current_loc (UNKNOWN_LOCATION)
316 gcc_assert (!current_cache);
317 current_cache = this;
319 ~lto_location_cache ()
321 apply_location_cache ();
322 gcc_assert (current_cache == this);
323 current_cache = NULL;
326 /* There can be at most one instance of location cache (combining multiple
327 would bring it out of sync with libcpp linemap); point to current
328 one. */
329 static lto_location_cache *current_cache;
331 private:
332 static int cmp_loc (const void *pa, const void *pb);
334 struct cached_location
336 const char *file;
337 location_t *loc;
338 int line, col;
339 bool sysp;
342 /* The location cache. */
344 auto_vec<cached_location> loc_cache;
346 /* Accepted entries are ones used by trees that are known to be not unified
347 by tree merging. */
349 int accepted_length;
351 /* Bookkeeping to remember state in between calls to lto_apply_location_cache
352 When streaming gimple, the location cache is not used and thus
353 lto_apply_location_cache happens per location basis. It is then
354 useful to avoid redundant calls of linemap API. */
356 const char *current_file;
357 int current_line;
358 int current_col;
359 bool current_sysp;
360 location_t current_loc;
363 /* Structure used as buffer for reading an LTO file. */
364 class lto_input_block
366 public:
367 /* Special constructor for the string table, it abuses this to
368 do random access but use the uhwi decoder. */
369 lto_input_block (const char *data_, unsigned int p_, unsigned int len_,
370 const unsigned char *mode_table_)
371 : data (data_), mode_table (mode_table_), p (p_), len (len_) {}
372 lto_input_block (const char *data_, unsigned int len_,
373 const unsigned char *mode_table_)
374 : data (data_), mode_table (mode_table_), p (0), len (len_) {}
376 const char *data;
377 const unsigned char *mode_table;
378 unsigned int p;
379 unsigned int len;
382 /* Compression algorithm used for compression of LTO bytecode. */
384 enum lto_compression
386 ZLIB,
387 ZSTD
390 /* Structure that represents LTO ELF section with information
391 about the format. */
393 struct lto_section
395 int16_t major_version;
396 int16_t minor_version;
397 unsigned char slim_object: 1;
398 lto_compression compression: 4;
399 int32_t reserved0: 27;
402 STATIC_ASSERT (sizeof (lto_section) == 8);
404 /* The is the first part of the record in an LTO file for many of the
405 IPA passes. */
406 struct lto_simple_header
408 /* Size of main gimple body of function. */
409 int32_t main_size;
412 struct lto_simple_header_with_strings : lto_simple_header
414 /* Size of the string table. */
415 int32_t string_size;
418 /* The header for a function body. */
419 struct lto_function_header : lto_simple_header_with_strings
421 /* Size of the cfg. */
422 int32_t cfg_size;
426 /* Structure describing a symbol section. */
427 struct lto_decl_header : lto_simple_header_with_strings
429 /* Size of region for decl state. */
430 int32_t decl_state_size;
432 /* Number of nodes in globals stream. */
433 int32_t num_nodes;
437 /* Statistics gathered during LTO, WPA and LTRANS. */
438 struct lto_stats_d
440 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
441 unsigned HOST_WIDE_INT num_output_symtab_nodes;
442 unsigned HOST_WIDE_INT num_input_files;
443 unsigned HOST_WIDE_INT num_output_files;
444 unsigned HOST_WIDE_INT num_cgraph_partitions;
445 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
446 unsigned HOST_WIDE_INT num_function_bodies;
447 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
448 unsigned HOST_WIDE_INT num_output_il_bytes;
449 unsigned HOST_WIDE_INT num_compressed_il_bytes;
450 unsigned HOST_WIDE_INT num_input_il_bytes;
451 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
452 unsigned HOST_WIDE_INT num_tree_bodies_output;
453 unsigned HOST_WIDE_INT num_pickle_refs_output;
456 /* Entry of LTO symtab encoder. */
457 struct lto_encoder_entry
459 symtab_node *node;
460 /* Is the node in this partition (i.e. ltrans of this partition will
461 be responsible for outputting it)? */
462 unsigned int in_partition:1;
463 /* Do we encode body in this partition? */
464 unsigned int body:1;
465 /* Do we encode initializer in this partition?
466 For example the readonly variable initializers are encoded to aid
467 constant folding even if they are not in the partition. */
468 unsigned int initializer:1;
472 /* Encoder data structure used to stream callgraph nodes. */
473 struct lto_symtab_encoder_d
475 vec<lto_encoder_entry> nodes;
476 hash_map<symtab_node *, size_t> *map;
479 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
481 /* Iterator structure for cgraph node sets. */
482 struct lto_symtab_encoder_iterator
484 lto_symtab_encoder_t encoder;
485 unsigned index;
490 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
492 struct lto_tree_ref_encoder
494 hash_map<tree, unsigned> *tree_hash_table; /* Maps pointers to indices. */
495 vec<tree> trees; /* Maps indices to pointers. */
499 /* Structure to hold states of input scope. */
500 struct GTY((for_user)) lto_in_decl_state
502 /* Array of lto_in_decl_buffers to store type and decls streams. */
503 vec<tree, va_gc> *streams[LTO_N_DECL_STREAMS];
505 /* If this in-decl state is associated with a function. FN_DECL
506 point to the FUNCTION_DECL. */
507 tree fn_decl;
509 /* True if decl state is compressed. */
510 bool compressed;
513 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
515 struct decl_state_hasher : ggc_ptr_hash<lto_in_decl_state>
517 static hashval_t
518 hash (lto_in_decl_state *s)
520 return htab_hash_pointer (s->fn_decl);
523 static bool
524 equal (lto_in_decl_state *a, lto_in_decl_state *b)
526 return a->fn_decl == b->fn_decl;
530 /* The structure that holds all of the vectors of global types,
531 decls and cgraph nodes used in the serialization of this file. */
532 struct lto_out_decl_state
534 /* The buffers contain the sets of decls of various kinds and types we have
535 seen so far and the indexes assigned to them. */
536 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
538 /* Encoder for cgraph nodes. */
539 lto_symtab_encoder_t symtab_node_encoder;
541 /* If this out-decl state belongs to a function, fn_decl points to that
542 function. Otherwise, it is NULL. */
543 tree fn_decl;
545 /* True if decl state is compressed. */
546 bool compressed;
549 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
552 /* Compact representation of a index <-> resolution pair. Unpacked to an
553 vector later. */
554 struct res_pair
556 ld_plugin_symbol_resolution_t res;
557 unsigned index;
561 /* One of these is allocated for each object file that being compiled
562 by lto. This structure contains the tables that are needed by the
563 serialized functions and ipa passes to connect themselves to the
564 global types and decls as they are reconstituted. */
565 struct GTY(()) lto_file_decl_data
567 /* Decl state currently used. */
568 struct lto_in_decl_state *current_decl_state;
570 /* Decl state corresponding to regions outside of any functions
571 in the compilation unit. */
572 struct lto_in_decl_state *global_decl_state;
574 /* Table of cgraph nodes present in this file. */
575 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
577 /* Hash table maps lto-related section names to location in file. */
578 hash_table<decl_state_hasher> *function_decl_states;
580 /* The .o file that these offsets relate to. */
581 const char *GTY((skip)) file_name;
583 /* Hash table maps lto-related section names to location in file. */
584 htab_t GTY((skip)) section_hash_table;
586 /* Hash new name of renamed global declaration to its original name. */
587 htab_t GTY((skip)) renaming_hash_table;
589 /* Linked list used temporarily in reader */
590 struct lto_file_decl_data *next;
592 /* Sub ID for merged objects. */
593 unsigned HOST_WIDE_INT id;
595 /* Symbol resolutions for this file */
596 vec<res_pair> GTY((skip)) respairs;
597 unsigned max_index;
599 gcov_summary GTY((skip)) profile_info;
601 /* Map assigning declarations their resolutions. */
602 hash_map<tree, ld_plugin_symbol_resolution> * GTY((skip)) resolution_map;
604 /* Mode translation table. */
605 const unsigned char *mode_table;
607 /* Read LTO section. */
608 lto_section lto_section_header;
611 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
613 struct lto_char_ptr_base
615 char *ptr;
618 /* An incore byte stream to buffer the various parts of the function.
619 The entire structure should be zeroed when created. The record
620 consists of a set of blocks. The first sizeof (ptr) bytes are used
621 as a chain, and the rest store the bytes to be written. */
622 struct lto_output_stream
624 /* The pointer to the first block in the stream. */
625 struct lto_char_ptr_base * first_block;
627 /* The pointer to the last and current block in the stream. */
628 struct lto_char_ptr_base * current_block;
630 /* The pointer to where the next char should be written. */
631 char * current_pointer;
633 /* The number of characters left in the current block. */
634 unsigned int left_in_block;
636 /* The block size of the last block allocated. */
637 unsigned int block_size;
639 /* The total number of characters written. */
640 unsigned int total_size;
643 /* A simple output block. This can be used for simple IPA passes that
644 do not need more than one stream. */
645 struct lto_simple_output_block
647 enum lto_section_type section_type;
648 struct lto_out_decl_state *decl_state;
650 /* The stream that the main tree codes are written to. */
651 struct lto_output_stream *main_stream;
654 /* String hashing. */
656 struct string_slot
658 const char *s;
659 int len;
660 unsigned int slot_num;
663 /* Hashtable helpers. */
665 struct string_slot_hasher : nofree_ptr_hash <string_slot>
667 static inline hashval_t hash (const string_slot *);
668 static inline bool equal (const string_slot *, const string_slot *);
671 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
672 to support strings that may not end in '\0'. */
674 inline hashval_t
675 string_slot_hasher::hash (const string_slot *ds)
677 hashval_t r = ds->len;
678 int i;
680 for (i = 0; i < ds->len; i++)
681 r = r * 67 + (unsigned)ds->s[i] - 113;
682 return r;
685 /* Returns nonzero if DS1 and DS2 are equal. */
687 inline bool
688 string_slot_hasher::equal (const string_slot *ds1, const string_slot *ds2)
690 if (ds1->len == ds2->len)
691 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
693 return 0;
696 /* Data structure holding all the data and descriptors used when writing
697 an LTO file. */
698 struct output_block
700 enum lto_section_type section_type;
701 struct lto_out_decl_state *decl_state;
703 /* The stream that the main tree codes are written to. */
704 struct lto_output_stream *main_stream;
706 /* The stream that contains the string table. */
707 struct lto_output_stream *string_stream;
709 /* The stream that contains the cfg. */
710 struct lto_output_stream *cfg_stream;
712 /* The hash table that contains the set of strings we have seen so
713 far and the indexes assigned to them. */
714 hash_table<string_slot_hasher> *string_hash_table;
716 /* The current symbol that we are currently serializing. Null
717 if we are serializing something else. */
718 symtab_node *symbol;
720 /* These are the last file and line that were seen in the stream.
721 If the current node differs from these, it needs to insert
722 something into the stream and fix these up. */
723 const char *current_file;
724 int current_line;
725 int current_col;
726 bool current_sysp;
728 /* Cache of nodes written in this section. */
729 struct streamer_tree_cache_d *writer_cache;
731 /* All data persistent across whole duration of output block
732 can go here. */
733 struct obstack obstack;
737 /* Data and descriptors used when reading from an LTO file. */
738 struct data_in
740 /* The global decls and types. */
741 struct lto_file_decl_data *file_data;
743 /* The string table. */
744 const char *strings;
746 /* The length of the string table. */
747 unsigned int strings_len;
749 /* Maps each reference number to the resolution done by the linker. */
750 vec<ld_plugin_symbol_resolution_t> globals_resolution;
752 /* Cache of pickled nodes. */
753 struct streamer_tree_cache_d *reader_cache;
755 /* Cache of source code location. */
756 lto_location_cache location_cache;
760 /* In lto-section-in.c */
761 extern struct lto_input_block * lto_create_simple_input_block (
762 struct lto_file_decl_data *,
763 enum lto_section_type, const char **, size_t *);
764 extern void
765 lto_destroy_simple_input_block (struct lto_file_decl_data *,
766 enum lto_section_type,
767 struct lto_input_block *, const char *, size_t);
768 extern void lto_set_in_hooks (struct lto_file_decl_data **,
769 lto_get_section_data_f *,
770 lto_free_section_data_f *);
771 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
772 extern const char *lto_get_section_data (struct lto_file_decl_data *,
773 enum lto_section_type,
774 const char *, size_t *,
775 bool decompress = false);
776 extern const char *lto_get_raw_section_data (struct lto_file_decl_data *,
777 enum lto_section_type,
778 const char *, size_t *);
779 extern void lto_free_section_data (struct lto_file_decl_data *,
780 enum lto_section_type,
781 const char *, const char *, size_t,
782 bool decompress = false);
783 extern void lto_free_raw_section_data (struct lto_file_decl_data *,
784 enum lto_section_type,
785 const char *, const char *, size_t);
786 extern htab_t lto_create_renaming_table (void);
787 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
788 const char *, const char *);
789 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
790 const char *);
791 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
792 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
793 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
794 struct lto_file_decl_data *, tree);
795 extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
796 extern void lto_free_function_in_decl_state_for_node (symtab_node *);
797 extern void lto_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
798 extern void lto_value_range_error (const char *,
799 HOST_WIDE_INT, HOST_WIDE_INT,
800 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
802 /* In lto-section-out.c */
803 extern void lto_begin_section (const char *, bool);
804 extern void lto_end_section (void);
805 extern void lto_write_data (const void *, unsigned int);
806 extern void lto_write_raw_data (const void *, unsigned int);
807 extern void lto_write_stream (struct lto_output_stream *);
808 extern bool lto_output_decl_index (struct lto_output_stream *,
809 struct lto_tree_ref_encoder *,
810 tree, unsigned int *);
811 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
812 struct lto_output_stream *, tree);
813 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
814 struct lto_output_stream *, tree);
815 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
816 struct lto_output_stream *, tree);
817 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
818 struct lto_output_stream *, tree);
819 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
820 struct lto_output_stream *, tree);
821 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
822 struct lto_output_stream *, tree);
823 extern struct lto_simple_output_block *lto_create_simple_output_block (
824 enum lto_section_type);
825 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
826 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
827 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
828 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
829 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
830 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
831 extern void lto_record_function_out_decl_state (tree,
832 struct lto_out_decl_state *);
833 extern void lto_append_block (struct lto_output_stream *);
836 /* In lto-streamer.c. */
838 /* Set when streaming LTO for offloading compiler. */
839 extern bool lto_stream_offload_p;
841 extern const char *lto_tag_name (enum LTO_tags);
842 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
843 extern void print_lto_report (const char *);
844 extern void lto_streamer_init (void);
845 extern bool gate_lto_out (void);
846 extern void lto_check_version (int, int, const char *);
847 extern void lto_streamer_hooks_init (void);
849 /* In lto-streamer-in.c */
850 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
851 extern void lto_reader_init (void);
852 extern void lto_input_function_body (struct lto_file_decl_data *,
853 struct cgraph_node *,
854 const char *);
855 extern void lto_input_variable_constructor (struct lto_file_decl_data *,
856 struct varpool_node *,
857 const char *);
858 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
859 const char *);
860 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
861 extern void lto_input_mode_table (struct lto_file_decl_data *);
862 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
863 const char *, unsigned,
864 vec<ld_plugin_symbol_resolution_t> );
865 extern void lto_data_in_delete (struct data_in *);
866 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
867 void lto_input_location (location_t *, struct bitpack_d *, struct data_in *);
868 location_t stream_input_location_now (struct bitpack_d *bp,
869 struct data_in *data);
870 tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
871 struct function *, enum LTO_tags);
872 void lto_tag_check_set (enum LTO_tags, int, ...);
873 void lto_init_eh (void);
874 hashval_t lto_input_scc (struct lto_input_block *, struct data_in *,
875 unsigned *, unsigned *);
876 tree lto_input_tree_1 (struct lto_input_block *, struct data_in *,
877 enum LTO_tags, hashval_t hash);
878 tree lto_input_tree (struct lto_input_block *, struct data_in *);
881 /* In lto-streamer-out.c */
882 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
883 extern struct output_block *create_output_block (enum lto_section_type);
884 extern void destroy_output_block (struct output_block *);
885 extern void lto_output_tree (struct output_block *, tree, bool, bool);
886 extern void lto_output_toplevel_asms (void);
887 extern void produce_asm (struct output_block *ob, tree fn);
888 extern void lto_output ();
889 extern void produce_asm_for_decls ();
890 void lto_output_decl_state_streams (struct output_block *,
891 struct lto_out_decl_state *);
892 void lto_output_decl_state_refs (struct output_block *,
893 struct lto_output_stream *,
894 struct lto_out_decl_state *);
895 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
896 void lto_output_init_mode_table (void);
899 /* In lto-cgraph.c */
900 extern bool asm_nodes_output;
901 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
902 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
903 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
904 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
905 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
906 struct cgraph_node *);
907 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
908 symtab_node *);
909 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
910 symtab_node *);
912 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
913 varpool_node *);
914 void output_symtab (void);
915 void input_symtab (void);
916 void output_offload_tables (void);
917 void input_offload_tables (bool);
918 bool referenced_from_other_partition_p (struct ipa_ref_list *,
919 lto_symtab_encoder_t);
920 bool reachable_from_other_partition_p (struct cgraph_node *,
921 lto_symtab_encoder_t);
922 bool referenced_from_this_partition_p (symtab_node *,
923 lto_symtab_encoder_t);
924 bool reachable_from_this_partition_p (struct cgraph_node *,
925 lto_symtab_encoder_t);
926 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
927 void select_what_to_stream (void);
929 /* In options-save.c. */
930 void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
931 struct cl_target_option *);
933 void cl_target_option_stream_in (struct data_in *,
934 struct bitpack_d *,
935 struct cl_target_option *);
937 void cl_optimization_stream_out (struct output_block *,
938 struct bitpack_d *, struct cl_optimization *);
940 void cl_optimization_stream_in (struct data_in *,
941 struct bitpack_d *, struct cl_optimization *);
945 /* In lto-opts.c. */
946 extern void lto_write_options (void);
949 /* Statistics gathered during LTO, WPA and LTRANS. */
950 extern struct lto_stats_d lto_stats;
952 /* Section names corresponding to the values of enum lto_section_type. */
953 extern const char *lto_section_name[];
955 /* Holds all the out decl states of functions output so far in the
956 current output file. */
957 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
959 /* Return true if LTO tag TAG corresponds to a tree code. */
960 static inline bool
961 lto_tag_is_tree_code_p (enum LTO_tags tag)
963 return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
967 /* Return true if LTO tag TAG corresponds to a gimple code. */
968 static inline bool
969 lto_tag_is_gimple_code_p (enum LTO_tags tag)
971 return (unsigned) tag >= NUM_TREE_CODES + 2
972 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
976 /* Return the LTO tag corresponding to gimple code CODE. See enum
977 LTO_tags for details on the conversion. */
978 static inline enum LTO_tags
979 lto_gimple_code_to_tag (enum gimple_code code)
981 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
985 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
986 details on the conversion. */
987 static inline enum gimple_code
988 lto_tag_to_gimple_code (enum LTO_tags tag)
990 gcc_assert (lto_tag_is_gimple_code_p (tag));
991 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
995 /* Return the LTO tag corresponding to tree code CODE. See enum
996 LTO_tags for details on the conversion. */
997 static inline enum LTO_tags
998 lto_tree_code_to_tag (enum tree_code code)
1000 return (enum LTO_tags) ((unsigned) code + 2);
1004 /* Return the tree code corresponding to TAG. See enum LTO_tags for
1005 details on the conversion. */
1006 static inline enum tree_code
1007 lto_tag_to_tree_code (enum LTO_tags tag)
1009 gcc_assert (lto_tag_is_tree_code_p (tag));
1010 return (enum tree_code) ((unsigned) tag - 2);
1013 /* Check that tag ACTUAL == EXPECTED. */
1014 static inline void
1015 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
1017 if (actual != expected)
1018 internal_error ("bytecode stream: expected tag %s instead of %s",
1019 lto_tag_name (expected), lto_tag_name (actual));
1022 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
1023 static inline void
1024 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
1025 enum LTO_tags tag2)
1027 if (actual < tag1 || actual > tag2)
1028 internal_error ("bytecode stream: tag %s is not in the expected range "
1029 "[%s, %s]",
1030 lto_tag_name (actual),
1031 lto_tag_name (tag1),
1032 lto_tag_name (tag2));
1035 /* Initialize an lto_out_decl_buffer ENCODER. */
1036 static inline void
1037 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1039 encoder->tree_hash_table = new hash_map<tree, unsigned> (251);
1040 encoder->trees.create (0);
1044 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1045 memory used by ENCODER is not freed by this function. */
1046 static inline void
1047 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1049 /* Hash table may be delete already. */
1050 delete encoder->tree_hash_table;
1051 encoder->tree_hash_table = NULL;
1052 encoder->trees.release ();
1055 /* Return the number of trees encoded in ENCODER. */
1056 static inline unsigned int
1057 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1059 return encoder->trees.length ();
1062 /* Return the IDX-th tree in ENCODER. */
1063 static inline tree
1064 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1065 unsigned int idx)
1067 return encoder->trees[idx];
1070 /* Return number of encoded nodes in ENCODER. */
1071 static inline int
1072 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1074 return encoder->nodes.length ();
1077 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1078 #define LCC_NOT_FOUND (-1)
1080 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1081 or LCC_NOT_FOUND if it is not there. */
1083 static inline int
1084 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1085 symtab_node *node)
1087 size_t *slot = encoder->map->get (node);
1088 return (slot && *slot ? *(slot) - 1 : LCC_NOT_FOUND);
1091 /* Return true if iterator LSE points to nothing. */
1092 static inline bool
1093 lsei_end_p (lto_symtab_encoder_iterator lsei)
1095 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1098 /* Advance iterator LSE. */
1099 static inline void
1100 lsei_next (lto_symtab_encoder_iterator *lsei)
1102 lsei->index++;
1105 /* Return the node pointed to by LSI. */
1106 static inline symtab_node *
1107 lsei_node (lto_symtab_encoder_iterator lsei)
1109 return lsei.encoder->nodes[lsei.index].node;
1112 /* Return the node pointed to by LSI. */
1113 static inline struct cgraph_node *
1114 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1116 return dyn_cast<cgraph_node *> (lsei.encoder->nodes[lsei.index].node);
1119 /* Return the node pointed to by LSI. */
1120 static inline varpool_node *
1121 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1123 return dyn_cast<varpool_node *> (lsei.encoder->nodes[lsei.index].node);
1126 /* Return the cgraph node corresponding to REF using ENCODER. */
1128 static inline symtab_node *
1129 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1131 if (ref == LCC_NOT_FOUND)
1132 return NULL;
1134 return encoder->nodes[ref].node;
1137 /* Return an iterator to the first node in LSI. */
1138 static inline lto_symtab_encoder_iterator
1139 lsei_start (lto_symtab_encoder_t encoder)
1141 lto_symtab_encoder_iterator lsei;
1143 lsei.encoder = encoder;
1144 lsei.index = 0;
1145 return lsei;
1148 /* Advance iterator LSE. */
1149 static inline void
1150 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1152 lsei_next (lsei);
1153 while (!lsei_end_p (*lsei)
1154 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1155 lsei_next (lsei);
1158 /* Return an iterator to the first node in LSI. */
1159 static inline lto_symtab_encoder_iterator
1160 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1162 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1164 if (lsei_end_p (lsei))
1165 return lsei;
1166 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1167 lsei_next_in_partition (&lsei);
1169 return lsei;
1172 /* Advance iterator LSE. */
1173 static inline void
1174 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1176 lsei_next (lsei);
1177 while (!lsei_end_p (*lsei)
1178 && (!is_a <cgraph_node *> (lsei_node (*lsei))
1179 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1180 lsei_next (lsei);
1183 /* Return an iterator to the first node in LSI. */
1184 static inline lto_symtab_encoder_iterator
1185 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1187 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1189 if (lsei_end_p (lsei))
1190 return lsei;
1191 if (!is_a <cgraph_node *> (lsei_node (lsei))
1192 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1193 lsei_next_function_in_partition (&lsei);
1195 return lsei;
1198 /* Advance iterator LSE. */
1199 static inline void
1200 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1202 lsei_next (lsei);
1203 while (!lsei_end_p (*lsei)
1204 && (!is_a <varpool_node *> (lsei_node (*lsei))
1205 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1206 lsei_next (lsei);
1209 /* Return an iterator to the first node in LSI. */
1210 static inline lto_symtab_encoder_iterator
1211 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1213 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1215 if (lsei_end_p (lsei))
1216 return lsei;
1217 if (!is_a <varpool_node *> (lsei_node (lsei))
1218 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1219 lsei_next_variable_in_partition (&lsei);
1221 return lsei;
1224 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1225 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1226 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1227 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1228 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1229 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1230 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1232 /* Entry for the delayed registering of decl -> DIE references. */
1233 struct dref_entry {
1234 tree decl;
1235 const char *sym;
1236 unsigned HOST_WIDE_INT off;
1239 extern vec<dref_entry> dref_queue;
1241 extern FILE *streamer_dump_file;
1243 #endif /* GCC_LTO_STREAMER_H */