Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / cpp / gcc / lto-streamer.h
blob0c46fd07ccabe5c5c007c7b4f81258e6c0cc840f
1 /* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
4 Copyright (C) 2009-2022 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 #if 0 // sdcpp ndef 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 /* Stream additional data to LTO object files to make it easier to debug
130 streaming code. This changes object files. */
131 static const bool streamer_debugging = false;
133 /* Tags representing the various IL objects written to the bytecode file
134 (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
136 NOTE, when adding new LTO tags, also update lto_tag_name. */
137 enum LTO_tags
139 LTO_null = 0,
141 /* Reference to previously-streamed node. */
142 LTO_tree_pickle_reference,
144 /* References to indexable tree nodes. These objects are stored in
145 tables that are written separately from the function bodies
146 and variable constructors that reference them. This way they can be
147 instantiated even when the referencing functions aren't (e.g., during WPA)
148 and it also allows functions to be copied from one file to another without
149 having to unpickle the body first (the references are location
150 independent). */
151 LTO_global_stream_ref,
153 LTO_ssa_name_ref,
155 /* Special for global streamer. A blob of unnamed tree nodes. */
156 LTO_tree_scc,
158 /* Sequence of trees. */
159 LTO_trees,
161 /* Shared INTEGER_CST node. */
162 LTO_integer_cst,
164 /* Tags of trees are encoded as
165 LTO_first_tree_tag + TREE_CODE. */
166 LTO_first_tree_tag,
167 /* Tags of gimple typles are encoded as
168 LTO_first_gimple_tag + gimple_code. */
169 LTO_first_gimple_tag = LTO_first_tree_tag + MAX_TREE_CODES,
171 /* Entry and exit basic blocks. */
172 LTO_bb0 = LTO_first_gimple_tag + LAST_AND_UNUSED_GIMPLE_CODE,
173 LTO_bb1,
175 /* EH region holding the previous statement. */
176 LTO_eh_region,
178 /* Function body. */
179 LTO_function,
181 /* EH table. */
182 LTO_eh_table,
184 /* EH region types. These mirror enum eh_region_type. */
185 LTO_ert_cleanup,
186 LTO_ert_try,
187 LTO_ert_allowed_exceptions,
188 LTO_ert_must_not_throw,
190 /* EH landing pad. */
191 LTO_eh_landing_pad,
193 /* EH try/catch node. */
194 LTO_eh_catch,
196 /* This tag must always be last. */
197 LTO_NUM_TAGS
201 /* Set of section types that are in an LTO file. This list will grow
202 as the number of IPA passes grows since each IPA pass will need its
203 own section type to store its summary information.
205 When adding a new section type, you must also extend the
206 LTO_SECTION_NAME array in lto-section-in.cc. */
207 enum lto_section_type
209 LTO_section_decls = 0,
210 LTO_section_function_body,
211 LTO_section_static_initializer,
212 LTO_section_symtab,
213 LTO_section_symtab_extension,
214 LTO_section_refs,
215 LTO_section_asm,
216 LTO_section_jump_functions,
217 LTO_section_ipa_pure_const,
218 LTO_section_ipa_reference,
219 LTO_section_ipa_profile,
220 LTO_section_symtab_nodes,
221 LTO_section_opts,
222 LTO_section_cgraph_opt_sum,
223 LTO_section_ipa_fn_summary,
224 LTO_section_ipcp_transform,
225 LTO_section_ipa_icf,
226 LTO_section_offload_table,
227 LTO_section_mode_table,
228 LTO_section_lto,
229 LTO_section_ipa_sra,
230 LTO_section_odr_types,
231 LTO_section_ipa_modref,
232 LTO_N_SECTION_TYPES /* Must be last. */
235 /* Indices to the various function, type and symbol streams. */
236 enum lto_decl_stream_e_t
238 LTO_DECL_STREAM = 0, /* Must be first. */
239 LTO_N_DECL_STREAMS
242 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
244 /* Return a char pointer to the start of a data stream for an lto pass
245 or function. The first parameter is the file data that contains
246 the information. The second parameter is the type of information
247 to be obtained. The third parameter is the name of the function
248 and is only used when finding a function body; otherwise it is
249 NULL. The fourth parameter is the length of the data returned. */
250 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
251 enum lto_section_type,
252 const char *,
253 int,
254 size_t *);
256 /* Return the data found from the above call. The first three
257 parameters are the same as above. The fourth parameter is the data
258 itself and the fifth is the length of the data. */
259 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
260 enum lto_section_type,
261 const char *,
262 const char *,
263 size_t);
265 /* The location cache holds expanded locations for streamed in trees.
266 This is done to reduce memory usage of libcpp linemap that strongly prefers
267 locations to be inserted in the source order. */
269 class lto_location_cache
271 public:
272 /* Apply all changes in location cache. Add locations into linemap and patch
273 trees. */
274 bool apply_location_cache ();
275 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
276 void accept_location_cache ();
277 /* Tree merging did suceed; throw away recent changes. */
278 void revert_location_cache ();
279 void input_location (location_t *loc, struct bitpack_d *bp,
280 class data_in *data_in);
281 void input_location_and_block (location_t *loc, struct bitpack_d *bp,
282 class lto_input_block *ib,
283 class data_in *data_in);
284 lto_location_cache ()
285 : loc_cache (), accepted_length (0), current_file (NULL), current_line (0),
286 current_col (0), current_sysp (false), current_loc (UNKNOWN_LOCATION),
287 current_block (NULL_TREE)
289 gcc_assert (!current_cache);
290 current_cache = this;
292 ~lto_location_cache ()
294 apply_location_cache ();
295 gcc_assert (current_cache == this);
296 current_cache = NULL;
299 /* There can be at most one instance of location cache (combining multiple
300 would bring it out of sync with libcpp linemap); point to current
301 one. */
302 static lto_location_cache *current_cache;
304 private:
305 static int cmp_loc (const void *pa, const void *pb);
307 struct cached_location
309 const char *file;
310 location_t *loc;
311 int line, col;
312 bool sysp;
313 tree block;
316 /* The location cache. */
318 auto_vec<cached_location> loc_cache;
320 /* Accepted entries are ones used by trees that are known to be not unified
321 by tree merging. */
323 int accepted_length;
325 /* Bookkeeping to remember state in between calls to lto_apply_location_cache
326 When streaming gimple, the location cache is not used and thus
327 lto_apply_location_cache happens per location basis. It is then
328 useful to avoid redundant calls of linemap API. */
330 const char *current_file;
331 int current_line;
332 int current_col;
333 bool current_sysp;
334 location_t current_loc;
335 tree current_block;
338 /* Structure used as buffer for reading an LTO file. */
339 class lto_input_block
341 public:
342 /* Special constructor for the string table, it abuses this to
343 do random access but use the uhwi decoder. */
344 lto_input_block (const char *data_, unsigned int p_, unsigned int len_,
345 const unsigned char *mode_table_)
346 : data (data_), mode_table (mode_table_), p (p_), len (len_) {}
347 lto_input_block (const char *data_, unsigned int len_,
348 const unsigned char *mode_table_)
349 : data (data_), mode_table (mode_table_), p (0), len (len_) {}
351 const char *data;
352 const unsigned char *mode_table;
353 unsigned int p;
354 unsigned int len;
357 /* Compression algorithm used for compression of LTO bytecode. */
359 enum lto_compression
361 ZLIB,
362 ZSTD
365 /* Structure that represents LTO ELF section with information
366 about the format. */
368 struct lto_section
370 int16_t major_version;
371 int16_t minor_version;
372 unsigned char slim_object;
373 unsigned char _padding;
375 /* Flags is a private field that is not defined publicly. */
376 uint16_t flags;
378 /* Set compression to FLAGS. */
379 inline void set_compression (lto_compression c)
381 flags = c;
384 /* Get compression from FLAGS. */
385 inline lto_compression get_compression ()
387 return (lto_compression) flags;
391 STATIC_ASSERT (sizeof (lto_section) == 8);
393 /* The is the first part of the record in an LTO file for many of the
394 IPA passes. */
395 struct lto_simple_header
397 /* Size of main gimple body of function. */
398 int32_t main_size;
401 struct lto_simple_header_with_strings : lto_simple_header
403 /* Size of the string table. */
404 int32_t string_size;
407 /* The header for a function body. */
408 struct lto_function_header : lto_simple_header_with_strings
410 /* Size of the cfg. */
411 int32_t cfg_size;
415 /* Structure describing a symbol section. */
416 struct lto_decl_header : lto_simple_header_with_strings
418 /* Size of region for decl state. */
419 int32_t decl_state_size;
421 /* Number of nodes in globals stream. */
422 int32_t num_nodes;
426 /* Statistics gathered during LTO, WPA and LTRANS. */
427 struct lto_stats_d
429 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
430 unsigned HOST_WIDE_INT num_output_symtab_nodes;
431 unsigned HOST_WIDE_INT num_input_files;
432 unsigned HOST_WIDE_INT num_output_files;
433 unsigned HOST_WIDE_INT num_cgraph_partitions;
434 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
435 unsigned HOST_WIDE_INT num_function_bodies;
436 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
437 unsigned HOST_WIDE_INT num_output_il_bytes;
438 unsigned HOST_WIDE_INT num_compressed_il_bytes;
439 unsigned HOST_WIDE_INT num_input_il_bytes;
440 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
441 unsigned HOST_WIDE_INT num_tree_bodies_output;
442 unsigned HOST_WIDE_INT num_pickle_refs_output;
445 /* Entry of LTO symtab encoder. */
446 struct lto_encoder_entry
448 symtab_node *node;
449 /* Is the node in this partition (i.e. ltrans of this partition will
450 be responsible for outputting it)? */
451 unsigned int in_partition:1;
452 /* Do we encode body in this partition? */
453 unsigned int body:1;
454 /* Do we encode initializer in this partition?
455 For example the readonly variable initializers are encoded to aid
456 constant folding even if they are not in the partition. */
457 unsigned int initializer:1;
461 /* Encoder data structure used to stream callgraph nodes. */
462 struct lto_symtab_encoder_d
464 vec<lto_encoder_entry> nodes;
465 hash_map<symtab_node *, size_t> *map;
468 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
470 /* Iterator structure for cgraph node sets. */
471 struct lto_symtab_encoder_iterator
473 lto_symtab_encoder_t encoder;
474 unsigned index;
479 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
481 struct lto_tree_ref_encoder
483 hash_map<tree, unsigned> *tree_hash_table; /* Maps pointers to indices. */
484 vec<tree> trees; /* Maps indices to pointers. */
488 /* Structure to hold states of input scope. */
489 struct GTY((for_user)) lto_in_decl_state
491 /* Array of lto_in_decl_buffers to store type and decls streams. */
492 vec<tree, va_gc> *streams[LTO_N_DECL_STREAMS];
494 /* If this in-decl state is associated with a function. FN_DECL
495 point to the FUNCTION_DECL. */
496 tree fn_decl;
498 /* True if decl state is compressed. */
499 bool compressed;
502 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
504 struct decl_state_hasher : ggc_ptr_hash<lto_in_decl_state>
506 static hashval_t
507 hash (lto_in_decl_state *s)
509 return htab_hash_pointer (s->fn_decl);
512 static bool
513 equal (lto_in_decl_state *a, lto_in_decl_state *b)
515 return a->fn_decl == b->fn_decl;
519 /* The structure that holds all of the vectors of global types,
520 decls and cgraph nodes used in the serialization of this file. */
521 struct lto_out_decl_state
523 /* The buffers contain the sets of decls of various kinds and types we have
524 seen so far and the indexes assigned to them. */
525 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
527 /* Encoder for cgraph nodes. */
528 lto_symtab_encoder_t symtab_node_encoder;
530 /* If this out-decl state belongs to a function, fn_decl points to that
531 function. Otherwise, it is NULL. */
532 tree fn_decl;
534 /* True if decl state is compressed. */
535 bool compressed;
538 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
541 /* Compact representation of a index <-> resolution pair. Unpacked to an
542 vector later. */
543 struct res_pair
545 ld_plugin_symbol_resolution_t res;
546 unsigned index;
550 /* One of these is allocated for each object file that being compiled
551 by lto. This structure contains the tables that are needed by the
552 serialized functions and ipa passes to connect themselves to the
553 global types and decls as they are reconstituted. */
554 struct GTY(()) lto_file_decl_data
556 /* Decl state currently used. */
557 struct lto_in_decl_state *current_decl_state;
559 /* Decl state corresponding to regions outside of any functions
560 in the compilation unit. */
561 struct lto_in_decl_state *global_decl_state;
563 /* Table of cgraph nodes present in this file. */
564 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
566 /* Hash table maps lto-related section names to location in file. */
567 hash_table<decl_state_hasher> *function_decl_states;
569 /* The .o file that these offsets relate to. */
570 const char *GTY((skip)) file_name;
572 /* Hash table maps lto-related section names to location in file. */
573 htab_t GTY((skip)) section_hash_table;
575 /* Hash new name of renamed global declaration to its original name. */
576 htab_t GTY((skip)) renaming_hash_table;
578 /* Linked list used temporarily in reader */
579 struct lto_file_decl_data *next;
581 /* Order in which the file appears on the command line. */
582 int order;
584 /* Sub ID for merged objects. */
585 unsigned HOST_WIDE_INT id;
587 /* Symbol resolutions for this file */
588 vec<res_pair> GTY((skip)) respairs;
589 unsigned max_index;
591 // sdcpp
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;
608 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
610 struct lto_char_ptr_base
612 char *ptr;
615 /* An incore byte stream to buffer the various parts of the function.
616 The entire structure should be zeroed when created. The record
617 consists of a set of blocks. The first sizeof (ptr) bytes are used
618 as a chain, and the rest store the bytes to be written. */
619 struct lto_output_stream
621 /* The pointer to the first block in the stream. */
622 struct lto_char_ptr_base * first_block;
624 /* The pointer to the last and current block in the stream. */
625 struct lto_char_ptr_base * current_block;
627 /* The pointer to where the next char should be written. */
628 char * current_pointer;
630 /* The number of characters left in the current block. */
631 unsigned int left_in_block;
633 /* The block size of the last block allocated. */
634 unsigned int block_size;
636 /* The total number of characters written. */
637 unsigned int total_size;
640 /* A simple output block. This can be used for simple IPA passes that
641 do not need more than one stream. */
642 struct lto_simple_output_block
644 enum lto_section_type section_type;
645 struct lto_out_decl_state *decl_state;
647 /* The stream that the main tree codes are written to. */
648 struct lto_output_stream *main_stream;
651 /* String hashing. */
653 struct string_slot
655 const char *s;
656 int len;
657 unsigned int slot_num;
660 /* Hashtable helpers. */
662 struct string_slot_hasher : nofree_ptr_hash <string_slot>
664 static inline hashval_t hash (const string_slot *);
665 static inline bool equal (const string_slot *, const string_slot *);
668 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
669 to support strings that may not end in '\0'. */
671 inline hashval_t
672 string_slot_hasher::hash (const string_slot *ds)
674 hashval_t r = ds->len;
675 int i;
677 for (i = 0; i < ds->len; i++)
678 r = r * 67 + (unsigned)ds->s[i] - 113;
679 return r;
682 /* Returns nonzero if DS1 and DS2 are equal. */
684 inline bool
685 string_slot_hasher::equal (const string_slot *ds1, const string_slot *ds2)
687 if (ds1->len == ds2->len)
688 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
690 return 0;
693 /* Data structure holding all the data and descriptors used when writing
694 an LTO file. */
695 struct output_block
697 enum lto_section_type section_type;
698 struct lto_out_decl_state *decl_state;
700 /* The stream that the main tree codes are written to. */
701 struct lto_output_stream *main_stream;
703 /* The stream that contains the string table. */
704 struct lto_output_stream *string_stream;
706 /* The stream that contains the cfg. */
707 struct lto_output_stream *cfg_stream;
709 /* The hash table that contains the set of strings we have seen so
710 far and the indexes assigned to them. */
711 hash_table<string_slot_hasher> *string_hash_table;
713 /* The current symbol that we are currently serializing. Null
714 if we are serializing something else. */
715 symtab_node *symbol;
717 /* These are the last file and line that were seen in the stream.
718 If the current node differs from these, it needs to insert
719 something into the stream and fix these up. */
720 const char *current_file;
721 int current_line;
722 int current_col;
723 bool current_sysp;
724 bool reset_locus;
725 bool emit_pwd;
726 tree current_block;
728 /* Cache of nodes written in this section. */
729 struct streamer_tree_cache_d *writer_cache;
731 /* All trees identified as local to the unit streamed. */
732 hash_set<tree> *local_trees;
734 /* All data persistent across whole duration of output block
735 can go here. */
736 struct obstack obstack;
740 /* Data and descriptors used when reading from an LTO file. */
741 class data_in
743 public:
744 /* The global decls and types. */
745 struct lto_file_decl_data *file_data;
747 /* The string table. */
748 const char *strings;
750 /* The length of the string table. */
751 unsigned int strings_len;
753 /* Maps each reference number to the resolution done by the linker. */
754 vec<ld_plugin_symbol_resolution_t> globals_resolution;
756 /* Cache of pickled nodes. */
757 struct streamer_tree_cache_d *reader_cache;
759 /* Cache of source code location. */
760 lto_location_cache location_cache;
764 /* In lto-section-in.cc */
765 extern class lto_input_block * lto_create_simple_input_block (
766 struct lto_file_decl_data *,
767 enum lto_section_type, const char **, size_t *);
768 extern void
769 lto_destroy_simple_input_block (struct lto_file_decl_data *,
770 enum lto_section_type,
771 class lto_input_block *, const char *, size_t);
772 extern void lto_set_in_hooks (struct lto_file_decl_data **,
773 lto_get_section_data_f *,
774 lto_free_section_data_f *);
775 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
776 extern const char *lto_get_section_data (struct lto_file_decl_data *,
777 enum lto_section_type,
778 const char *, int, size_t *,
779 bool decompress = false);
780 extern const char *lto_get_summary_section_data (struct lto_file_decl_data *,
781 enum lto_section_type,
782 size_t *);
783 extern const char *lto_get_raw_section_data (struct lto_file_decl_data *,
784 enum lto_section_type,
785 const char *, int, size_t *);
786 extern void lto_free_section_data (struct lto_file_decl_data *,
787 enum lto_section_type,
788 const char *, const char *, size_t,
789 bool decompress = false);
790 extern void lto_free_raw_section_data (struct lto_file_decl_data *,
791 enum lto_section_type,
792 const char *, const char *, size_t);
793 extern htab_t lto_create_renaming_table (void);
794 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
795 const char *, const char *);
796 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
797 const char *);
798 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
799 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
800 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
801 struct lto_file_decl_data *, tree);
802 extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
803 extern void lto_free_function_in_decl_state_for_node (symtab_node *);
804 extern void lto_section_overrun (class lto_input_block *) ATTRIBUTE_NORETURN;
805 extern void lto_value_range_error (const char *,
806 HOST_WIDE_INT, HOST_WIDE_INT,
807 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
809 /* In lto-section-out.cc */
810 extern void lto_begin_section (const char *, bool);
811 extern void lto_end_section (void);
812 extern void lto_write_data (const void *, unsigned int);
813 extern void lto_write_raw_data (const void *, unsigned int);
814 extern void lto_write_stream (struct lto_output_stream *);
815 extern struct lto_simple_output_block *lto_create_simple_output_block (
816 enum lto_section_type);
817 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
818 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
819 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
820 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
821 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
822 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
823 extern void lto_record_function_out_decl_state (tree,
824 struct lto_out_decl_state *);
825 extern void lto_append_block (struct lto_output_stream *);
828 /* In lto-streamer.cc. */
830 /* Set when streaming LTO for offloading compiler. */
831 extern bool lto_stream_offload_p;
833 extern const char *lto_tag_name (enum LTO_tags);
834 extern char *lto_get_section_name (int, const char *, int,
835 struct lto_file_decl_data *);
836 extern void print_lto_report (const char *);
837 extern void lto_streamer_init (void);
838 extern bool gate_lto_out (void);
839 extern void lto_check_version (int, int, const char *);
840 extern void lto_streamer_hooks_init (void);
842 /* In lto-streamer-in.cc */
843 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
844 extern void lto_reader_init (void);
845 extern void lto_free_file_name_hash (void);
846 extern void lto_input_function_body (struct lto_file_decl_data *,
847 struct cgraph_node *,
848 const char *);
849 extern void lto_input_variable_constructor (struct lto_file_decl_data *,
850 struct varpool_node *,
851 const char *);
852 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
853 const char *);
854 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
855 extern void lto_input_mode_table (struct lto_file_decl_data *);
856 extern class data_in *lto_data_in_create (struct lto_file_decl_data *,
857 const char *, unsigned,
858 vec<ld_plugin_symbol_resolution_t> );
859 extern void lto_data_in_delete (class data_in *);
860 extern void lto_input_data_block (class lto_input_block *, void *, size_t);
861 void lto_input_location (location_t *, struct bitpack_d *, class data_in *);
862 tree lto_input_tree_ref (class lto_input_block *, class data_in *,
863 struct function *, enum LTO_tags);
864 void lto_tag_check_set (enum LTO_tags, int, ...);
865 void lto_init_eh (void);
866 hashval_t lto_input_scc (class lto_input_block *, class data_in *,
867 unsigned *, unsigned *, bool);
868 tree lto_input_tree_1 (class lto_input_block *, class data_in *,
869 enum LTO_tags, hashval_t hash);
870 tree lto_input_tree (class lto_input_block *, class data_in *);
871 tree stream_read_tree_ref (class lto_input_block *, class data_in *);
874 /* In lto-streamer-out.cc */
875 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
876 extern struct output_block *create_output_block (enum lto_section_type);
877 extern void destroy_output_block (struct output_block *);
878 extern void lto_output_tree (struct output_block *, tree, bool, bool);
879 extern void stream_write_tree_ref (struct output_block *, tree);
880 extern void lto_output_var_decl_ref (struct lto_out_decl_state *,
881 struct lto_output_stream *, tree);
882 extern void lto_output_fn_decl_ref (struct lto_out_decl_state *,
883 struct lto_output_stream *, tree);
884 extern tree lto_input_var_decl_ref (lto_input_block *, lto_file_decl_data *);
885 extern tree lto_input_fn_decl_ref (lto_input_block *, lto_file_decl_data *);
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 *,
896 location_t);
897 void lto_output_location_and_block (struct output_block *, struct bitpack_d *,
898 location_t);
899 void lto_output_init_mode_table (void);
900 void lto_prepare_function_for_streaming (cgraph_node *);
903 /* In lto-cgraph.cc */
904 extern bool asm_nodes_output;
905 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
906 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
907 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
908 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
909 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
910 struct cgraph_node *);
911 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
912 symtab_node *);
913 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
914 symtab_node *);
916 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
917 varpool_node *);
918 void output_symtab (void);
919 void input_symtab (void);
920 void output_offload_tables (void);
921 void input_offload_tables (bool);
922 bool referenced_from_other_partition_p (struct ipa_ref_list *,
923 lto_symtab_encoder_t);
924 bool reachable_from_other_partition_p (struct cgraph_node *,
925 lto_symtab_encoder_t);
926 bool referenced_from_this_partition_p (symtab_node *,
927 lto_symtab_encoder_t);
928 bool reachable_from_this_partition_p (struct cgraph_node *,
929 lto_symtab_encoder_t);
930 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
931 void select_what_to_stream (void);
933 /* In omp-general.cc. */
934 void omp_lto_output_declare_variant_alt (lto_simple_output_block *,
935 cgraph_node *, lto_symtab_encoder_t);
936 void omp_lto_input_declare_variant_alt (lto_input_block *, cgraph_node *,
937 vec<symtab_node *>);
939 /* In options-save.cc. */
940 void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
941 struct cl_target_option *);
943 void cl_target_option_stream_in (class data_in *,
944 struct bitpack_d *,
945 struct cl_target_option *);
947 void cl_optimization_stream_out (struct output_block *,
948 struct bitpack_d *, struct cl_optimization *);
950 void cl_optimization_stream_in (class data_in *,
951 struct bitpack_d *, struct cl_optimization *);
955 /* In lto-opts.cc. */
956 extern void lto_write_options (void);
959 /* Statistics gathered during LTO, WPA and LTRANS. */
960 extern struct lto_stats_d lto_stats;
962 /* Section names corresponding to the values of enum lto_section_type. */
963 extern const char *lto_section_name[];
965 /* Holds all the out decl states of functions output so far in the
966 current output file. */
967 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
969 /* Return true if LTO tag TAG corresponds to a tree code. */
970 static inline bool
971 lto_tag_is_tree_code_p (enum LTO_tags tag)
973 return tag > LTO_first_tree_tag && (unsigned) tag <= MAX_TREE_CODES;
977 /* Return true if LTO tag TAG corresponds to a gimple code. */
978 static inline bool
979 lto_tag_is_gimple_code_p (enum LTO_tags tag)
981 return (unsigned) tag >= LTO_first_gimple_tag
982 && (unsigned) tag
983 < LTO_first_gimple_tag + LAST_AND_UNUSED_GIMPLE_CODE;
987 /* Return the LTO tag corresponding to gimple code CODE. See enum
988 LTO_tags for details on the conversion. */
989 static inline enum LTO_tags
990 lto_gimple_code_to_tag (enum gimple_code code)
992 return (enum LTO_tags) ((unsigned) code + LTO_first_gimple_tag);
996 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
997 details on the conversion. */
998 static inline enum gimple_code
999 lto_tag_to_gimple_code (enum LTO_tags tag)
1001 gcc_assert (lto_tag_is_gimple_code_p (tag));
1002 return (enum gimple_code) ((unsigned) tag - LTO_first_gimple_tag);
1006 /* Return the LTO tag corresponding to tree code CODE. See enum
1007 LTO_tags for details on the conversion. */
1008 static inline enum LTO_tags
1009 lto_tree_code_to_tag (enum tree_code code)
1011 return (enum LTO_tags) ((unsigned) code + LTO_first_tree_tag);
1015 /* Return the tree code corresponding to TAG. See enum LTO_tags for
1016 details on the conversion. */
1017 static inline enum tree_code
1018 lto_tag_to_tree_code (enum LTO_tags tag)
1020 gcc_assert (lto_tag_is_tree_code_p (tag));
1021 return (enum tree_code) ((unsigned) tag - LTO_first_tree_tag);
1024 /* Check that tag ACTUAL == EXPECTED. */
1025 static inline void
1026 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
1028 if (actual != expected)
1029 internal_error ("bytecode stream: expected tag %s instead of %s",
1030 lto_tag_name (expected), lto_tag_name (actual));
1033 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
1034 static inline void
1035 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
1036 enum LTO_tags tag2)
1038 if (actual < tag1 || actual > tag2)
1039 internal_error ("bytecode stream: tag %s is not in the expected range "
1040 "[%s, %s]",
1041 lto_tag_name (actual),
1042 lto_tag_name (tag1),
1043 lto_tag_name (tag2));
1046 /* Initialize an lto_out_decl_buffer ENCODER. */
1047 static inline void
1048 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1050 encoder->tree_hash_table = new hash_map<tree, unsigned> (251);
1051 encoder->trees.create (0);
1055 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1056 memory used by ENCODER is not freed by this function. */
1057 static inline void
1058 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1060 /* Hash table may be delete already. */
1061 delete encoder->tree_hash_table;
1062 encoder->tree_hash_table = NULL;
1063 encoder->trees.release ();
1066 /* Return the number of trees encoded in ENCODER. */
1067 static inline unsigned int
1068 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1070 return encoder->trees.length ();
1073 /* Return the IDX-th tree in ENCODER. */
1074 static inline tree
1075 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1076 unsigned int idx)
1078 return encoder->trees[idx];
1081 /* Return number of encoded nodes in ENCODER. */
1082 static inline int
1083 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1085 return encoder->nodes.length ();
1088 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1089 #define LCC_NOT_FOUND (-1)
1091 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1092 or LCC_NOT_FOUND if it is not there. */
1094 static inline int
1095 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1096 symtab_node *node)
1098 size_t *slot = encoder->map->get (node);
1099 return (slot && *slot ? *(slot) - 1 : LCC_NOT_FOUND);
1102 /* Return true if iterator LSE points to nothing. */
1103 static inline bool
1104 lsei_end_p (lto_symtab_encoder_iterator lsei)
1106 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1109 /* Advance iterator LSE. */
1110 static inline void
1111 lsei_next (lto_symtab_encoder_iterator *lsei)
1113 lsei->index++;
1116 /* Return the node pointed to by LSI. */
1117 static inline symtab_node *
1118 lsei_node (lto_symtab_encoder_iterator lsei)
1120 return lsei.encoder->nodes[lsei.index].node;
1123 /* Return the node pointed to by LSI. */
1124 static inline struct cgraph_node *
1125 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1127 return dyn_cast<cgraph_node *> (lsei.encoder->nodes[lsei.index].node);
1130 /* Return the node pointed to by LSI. */
1131 static inline varpool_node *
1132 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1134 return dyn_cast<varpool_node *> (lsei.encoder->nodes[lsei.index].node);
1137 /* Return the cgraph node corresponding to REF using ENCODER. */
1139 static inline symtab_node *
1140 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1142 if (ref == LCC_NOT_FOUND)
1143 return NULL;
1145 return encoder->nodes[ref].node;
1148 /* Return an iterator to the first node in LSI. */
1149 static inline lto_symtab_encoder_iterator
1150 lsei_start (lto_symtab_encoder_t encoder)
1152 lto_symtab_encoder_iterator lsei;
1154 lsei.encoder = encoder;
1155 lsei.index = 0;
1156 return lsei;
1159 /* Advance iterator LSE. */
1160 static inline void
1161 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1163 lsei_next (lsei);
1164 while (!lsei_end_p (*lsei)
1165 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1166 lsei_next (lsei);
1169 /* Return an iterator to the first node in LSI. */
1170 static inline lto_symtab_encoder_iterator
1171 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1173 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1175 if (lsei_end_p (lsei))
1176 return lsei;
1177 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1178 lsei_next_in_partition (&lsei);
1180 return lsei;
1183 /* Advance iterator LSE. */
1184 static inline void
1185 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1187 lsei_next (lsei);
1188 while (!lsei_end_p (*lsei)
1189 && (!is_a <cgraph_node *> (lsei_node (*lsei))
1190 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1191 lsei_next (lsei);
1194 /* Return an iterator to the first node in LSI. */
1195 static inline lto_symtab_encoder_iterator
1196 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1198 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1200 if (lsei_end_p (lsei))
1201 return lsei;
1202 if (!is_a <cgraph_node *> (lsei_node (lsei))
1203 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1204 lsei_next_function_in_partition (&lsei);
1206 return lsei;
1209 /* Advance iterator LSE. */
1210 static inline void
1211 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1213 lsei_next (lsei);
1214 while (!lsei_end_p (*lsei)
1215 && (!is_a <varpool_node *> (lsei_node (*lsei))
1216 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1217 lsei_next (lsei);
1220 /* Return an iterator to the first node in LSI. */
1221 static inline lto_symtab_encoder_iterator
1222 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1224 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1226 if (lsei_end_p (lsei))
1227 return lsei;
1228 if (!is_a <varpool_node *> (lsei_node (lsei))
1229 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1230 lsei_next_variable_in_partition (&lsei);
1232 return lsei;
1235 /* Entry for the delayed registering of decl -> DIE references. */
1236 struct dref_entry {
1237 tree decl;
1238 const char *sym;
1239 unsigned HOST_WIDE_INT off;
1242 extern vec<dref_entry> dref_queue;
1244 extern FILE *streamer_dump_file;
1246 #endif /* GCC_LTO_STREAMER_H */