PR testsuite/79036 - gcc.dg/tree-ssa/builtin-sprintf.c fails starting with r244037
[official-gcc.git] / gcc / lto-streamer.h
blob08bfc13af7582fafdae215b24f7c14c516147720
1 /* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
4 Copyright (C) 2009-2017 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 /* Define when debugging the LTO streamer. This causes the writer
31 to output the numeric value for the memory address of the tree node
32 being emitted. When debugging a problem in the reader, check the
33 original address that the writer was emitting using lto_orig_address_get.
34 With this value, set a breakpoint in the writer (e.g., lto_output_tree)
35 to trace how the faulty node is being emitted. */
36 /* #define LTO_STREAMER_DEBUG 1 */
38 /* The encoding for a function consists of the following sections:
40 1) The header.
41 2) FIELD_DECLS.
42 3) FUNCTION_DECLS.
43 4) global VAR_DECLS.
44 5) type_decls
45 6) types.
46 7) Names for the labels that have names
47 8) The SSA names.
48 9) The control flow graph.
49 10-11)Gimple for local decls.
50 12) Gimple for the function.
51 13) Strings.
53 1) THE HEADER.
54 2-6) THE GLOBAL DECLS AND TYPES.
56 The global decls and types are encoded in the same way. For each
57 entry, there is word with the offset within the section to the
58 entry.
60 7) THE LABEL NAMES.
62 Since most labels do not have names, this section my be of zero
63 length. It consists of an array of string table references, one
64 per label. In the lto code, the labels are given either
65 positive or negative indexes. the positive ones have names and
66 the negative ones do not. The positive index can be used to
67 find the name in this array.
69 9) THE CFG.
71 10) Index into the local decls. Since local decls can have local
72 decls inside them, they must be read in randomly in order to
73 properly restore them.
75 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
77 The gimple consists of a set of records.
79 THE FUNCTION
81 At the top level of (8) is the function. It consists of five
82 pieces:
84 LTO_function - The tag.
85 eh tree - This is all of the exception handling regions
86 put out in a post order traversial of the
87 tree. Siblings are output as lists terminated
88 by a 0. The set of fields matches the fields
89 defined in except.c.
91 last_basic_block - in uleb128 form.
93 basic blocks - This is the set of basic blocks.
95 zero - The termination of the basic blocks.
97 BASIC BLOCKS
99 There are two forms of basic blocks depending on if they are
100 empty or not.
102 The basic block consists of:
104 LTO_bb1 or LTO_bb0 - The tag.
106 bb->index - the index in uleb128 form.
108 #succs - The number of successors un uleb128 form.
110 the successors - For each edge, a pair. The first of the
111 pair is the index of the successor in
112 uleb128 form and the second are the flags in
113 uleb128 form.
115 the statements - A gimple tree, as described above.
116 These are only present for LTO_BB1.
117 Following each statement is an optional
118 exception handling record LTO_eh_region
119 which contains the region number (for
120 regions >= 0).
122 zero - This is only present for LTO_BB1 and is used
123 to terminate the statements and exception
124 regions within this block.
126 12) STRINGS
128 String are represented in the table as pairs, a length in ULEB128
129 form followed by the data for the string. */
131 #define LTO_major_version 6
132 #define LTO_minor_version 0
134 typedef unsigned char lto_decl_flags_t;
137 /* Tags representing the various IL objects written to the bytecode file
138 (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
140 NOTE, when adding new LTO tags, also update lto_tag_name. */
141 enum LTO_tags
143 LTO_null = 0,
145 /* Special for streamer. Reference to previously-streamed node. */
146 LTO_tree_pickle_reference,
148 /* Reserve enough entries to fit all the tree and gimple codes handled
149 by the streamer. This guarantees that:
151 1- Given a tree code C:
152 enum LTO_tags tag == C + 1
154 2- Given a gimple code C:
155 enum LTO_tags tag == C + NUM_TREE_CODES + 1
157 Conversely, to map between LTO tags and tree/gimple codes, the
158 reverse operation must be applied. */
159 LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
160 LTO_bb1,
162 /* EH region holding the previous statement. */
163 LTO_eh_region,
165 /* Shared INTEGER_CST node. */
166 LTO_integer_cst,
168 /* Function body. */
169 LTO_function,
171 /* EH table. */
172 LTO_eh_table,
174 /* EH region types. These mirror enum eh_region_type. */
175 LTO_ert_cleanup,
176 LTO_ert_try,
177 LTO_ert_allowed_exceptions,
178 LTO_ert_must_not_throw,
180 /* EH landing pad. */
181 LTO_eh_landing_pad,
183 /* EH try/catch node. */
184 LTO_eh_catch,
186 /* Special for global streamer. A blob of unnamed tree nodes. */
187 LTO_tree_scc,
189 /* References to indexable tree nodes. These objects are stored in
190 tables that are written separately from the function bodies that
191 reference them. This way they can be instantiated even when the
192 referencing functions aren't (e.g., during WPA) and it also allows
193 functions to be copied from one file to another without having
194 to unpickle the body first (the references are location
195 independent).
197 NOTE, do not regroup these values as the grouping is exposed
198 in the range checks done in lto_input_tree. */
199 LTO_field_decl_ref, /* Do not change. */
200 LTO_function_decl_ref,
201 LTO_label_decl_ref,
202 LTO_namespace_decl_ref,
203 LTO_result_decl_ref,
204 LTO_ssa_name_ref,
205 LTO_type_decl_ref,
206 LTO_type_ref,
207 LTO_const_decl_ref,
208 LTO_imported_decl_ref,
209 LTO_translation_unit_decl_ref,
210 LTO_global_decl_ref,
211 LTO_namelist_decl_ref, /* Do not change. */
213 /* This tag must always be last. */
214 LTO_NUM_TAGS
218 /* Set of section types that are in an LTO file. This list will grow
219 as the number of IPA passes grows since each IPA pass will need its
220 own section type to store its summary information.
222 When adding a new section type, you must also extend the
223 LTO_SECTION_NAME array in lto-section-in.c. */
224 enum lto_section_type
226 LTO_section_decls = 0,
227 LTO_section_function_body,
228 LTO_section_static_initializer,
229 LTO_section_symtab,
230 LTO_section_refs,
231 LTO_section_asm,
232 LTO_section_jump_functions,
233 LTO_section_ipa_pure_const,
234 LTO_section_ipa_reference,
235 LTO_section_ipa_profile,
236 LTO_section_symtab_nodes,
237 LTO_section_opts,
238 LTO_section_cgraph_opt_sum,
239 LTO_section_inline_summary,
240 LTO_section_ipcp_transform,
241 LTO_section_ipa_icf,
242 LTO_section_offload_table,
243 LTO_section_mode_table,
244 LTO_section_ipa_hsa,
245 LTO_N_SECTION_TYPES /* Must be last. */
248 /* Indices to the various function, type and symbol streams. */
249 enum lto_decl_stream_e_t
251 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
252 LTO_DECL_STREAM_FIELD_DECL,
253 LTO_DECL_STREAM_FN_DECL,
254 LTO_DECL_STREAM_VAR_DECL,
255 LTO_DECL_STREAM_TYPE_DECL,
256 LTO_DECL_STREAM_NAMESPACE_DECL,
257 LTO_DECL_STREAM_LABEL_DECL,
258 LTO_N_DECL_STREAMS
261 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
264 /* Macro to define convenience functions for type and decl streams
265 in lto_file_decl_data. */
266 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
267 static inline tree \
268 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
269 unsigned int idx) \
271 struct lto_in_decl_state *state = data->current_decl_state; \
272 return (*state->streams[LTO_DECL_STREAM_## UPPER_NAME])[idx]; \
275 static inline unsigned int \
276 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
278 struct lto_in_decl_state *state = data->current_decl_state; \
279 return vec_safe_length (state->streams[LTO_DECL_STREAM_## UPPER_NAME]); \
283 /* Return a char pointer to the start of a data stream for an lto pass
284 or function. The first parameter is the file data that contains
285 the information. The second parameter is the type of information
286 to be obtained. The third parameter is the name of the function
287 and is only used when finding a function body; otherwise it is
288 NULL. The fourth parameter is the length of the data returned. */
289 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
290 enum lto_section_type,
291 const char *,
292 size_t *);
294 /* Return the data found from the above call. The first three
295 parameters are the same as above. The fourth parameter is the data
296 itself and the fifth is the length of the data. */
297 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
298 enum lto_section_type,
299 const char *,
300 const char *,
301 size_t);
303 /* The location cache holds expanded locations for streamed in trees.
304 This is done to reduce memory usage of libcpp linemap that strongly preffers
305 locations to be inserted in the soruce order. */
307 class lto_location_cache
309 public:
310 /* Apply all changes in location cache. Add locations into linemap and patch
311 trees. */
312 bool apply_location_cache ();
313 /* Tree merging did not suceed; mark all changes in the cache as accepted. */
314 void accept_location_cache ();
315 /* Tree merging did suceed; throw away recent changes. */
316 void revert_location_cache ();
317 void input_location (location_t *loc, struct bitpack_d *bp,
318 struct data_in *data_in);
319 lto_location_cache ()
320 : loc_cache (), accepted_length (0), current_file (NULL), current_line (0),
321 current_col (0), current_sysp (false), current_loc (UNKNOWN_LOCATION)
323 gcc_assert (!current_cache);
324 current_cache = this;
326 ~lto_location_cache ()
328 apply_location_cache ();
329 gcc_assert (current_cache == this);
330 current_cache = NULL;
333 /* There can be at most one instance of location cache (combining multiple
334 would bring it out of sync with libcpp linemap); point to current
335 one. */
336 static lto_location_cache *current_cache;
338 private:
339 static int cmp_loc (const void *pa, const void *pb);
341 struct cached_location
343 const char *file;
344 location_t *loc;
345 int line, col;
346 bool sysp;
349 /* The location cache. */
351 auto_vec<cached_location> loc_cache;
353 /* Accepted entries are ones used by trees that are known to be not unified
354 by tree merging. */
356 int accepted_length;
358 /* Bookkeeping to remember state in between calls to lto_apply_location_cache
359 When streaming gimple, the location cache is not used and thus
360 lto_apply_location_cache happens per location basis. It is then
361 useful to avoid redundant calls of linemap API. */
363 const char *current_file;
364 int current_line;
365 int current_col;
366 bool current_sysp;
367 location_t current_loc;
370 /* Structure used as buffer for reading an LTO file. */
371 class lto_input_block
373 public:
374 /* Special constructor for the string table, it abuses this to
375 do random access but use the uhwi decoder. */
376 lto_input_block (const char *data_, unsigned int p_, unsigned int len_,
377 const unsigned char *mode_table_)
378 : data (data_), mode_table (mode_table_), p (p_), len (len_) {}
379 lto_input_block (const char *data_, unsigned int len_,
380 const unsigned char *mode_table_)
381 : data (data_), mode_table (mode_table_), p (0), len (len_) {}
383 const char *data;
384 const unsigned char *mode_table;
385 unsigned int p;
386 unsigned int len;
390 /* The is the first part of the record for a function or constructor
391 in the .o file. */
392 struct lto_header
394 int16_t major_version;
395 int16_t minor_version;
398 /* The is the first part of the record in an LTO file for many of the
399 IPA passes. */
400 struct lto_simple_header : lto_header
402 /* Size of main gimple body of function. */
403 int32_t main_size;
406 struct lto_simple_header_with_strings : lto_simple_header
408 /* Size of the string table. */
409 int32_t string_size;
412 /* The header for a function body. */
413 struct lto_function_header : lto_simple_header_with_strings
415 /* Size of the cfg. */
416 int32_t cfg_size;
420 /* Structure describing a symbol section. */
421 struct lto_decl_header : lto_simple_header_with_strings
423 /* Size of region for decl state. */
424 int32_t decl_state_size;
426 /* Number of nodes in globals stream. */
427 int32_t num_nodes;
431 /* Statistics gathered during LTO, WPA and LTRANS. */
432 struct lto_stats_d
434 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
435 unsigned HOST_WIDE_INT num_output_symtab_nodes;
436 unsigned HOST_WIDE_INT num_input_files;
437 unsigned HOST_WIDE_INT num_output_files;
438 unsigned HOST_WIDE_INT num_cgraph_partitions;
439 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
440 unsigned HOST_WIDE_INT num_function_bodies;
441 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
442 unsigned HOST_WIDE_INT num_output_il_bytes;
443 unsigned HOST_WIDE_INT num_compressed_il_bytes;
444 unsigned HOST_WIDE_INT num_input_il_bytes;
445 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
446 unsigned HOST_WIDE_INT num_tree_bodies_output;
447 unsigned HOST_WIDE_INT num_pickle_refs_output;
450 /* Entry of LTO symtab encoder. */
451 struct lto_encoder_entry
453 symtab_node *node;
454 /* Is the node in this partition (i.e. ltrans of this partition will
455 be responsible for outputting it)? */
456 unsigned int in_partition:1;
457 /* Do we encode body in this partition? */
458 unsigned int body:1;
459 /* Do we encode initializer in this partition?
460 For example the readonly variable initializers are encoded to aid
461 constant folding even if they are not in the partition. */
462 unsigned int initializer:1;
466 /* Encoder data structure used to stream callgraph nodes. */
467 struct lto_symtab_encoder_d
469 vec<lto_encoder_entry> nodes;
470 hash_map<symtab_node *, size_t> *map;
473 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
475 /* Iterator structure for cgraph node sets. */
476 struct lto_symtab_encoder_iterator
478 lto_symtab_encoder_t encoder;
479 unsigned index;
484 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
486 struct lto_tree_ref_encoder
488 hash_map<tree, unsigned> *tree_hash_table; /* Maps pointers to indices. */
489 vec<tree> trees; /* Maps indices to pointers. */
493 /* Structure to hold states of input scope. */
494 struct GTY((for_user)) lto_in_decl_state
496 /* Array of lto_in_decl_buffers to store type and decls streams. */
497 vec<tree, va_gc> *streams[LTO_N_DECL_STREAMS];
499 /* If this in-decl state is associated with a function. FN_DECL
500 point to the FUNCTION_DECL. */
501 tree fn_decl;
503 /* True if decl state is compressed. */
504 bool compressed;
507 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
509 struct decl_state_hasher : ggc_ptr_hash<lto_in_decl_state>
511 static hashval_t
512 hash (lto_in_decl_state *s)
514 return htab_hash_pointer (s->fn_decl);
517 static bool
518 equal (lto_in_decl_state *a, lto_in_decl_state *b)
520 return a->fn_decl == b->fn_decl;
524 /* The structure that holds all of the vectors of global types,
525 decls and cgraph nodes used in the serialization of this file. */
526 struct lto_out_decl_state
528 /* The buffers contain the sets of decls of various kinds and types we have
529 seen so far and the indexes assigned to them. */
530 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
532 /* Encoder for cgraph nodes. */
533 lto_symtab_encoder_t symtab_node_encoder;
535 /* If this out-decl state belongs to a function, fn_decl points to that
536 function. Otherwise, it is NULL. */
537 tree fn_decl;
539 /* True if decl state is compressed. */
540 bool compressed;
543 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
546 /* Compact representation of a index <-> resolution pair. Unpacked to an
547 vector later. */
548 struct res_pair
550 ld_plugin_symbol_resolution_t res;
551 unsigned index;
555 /* One of these is allocated for each object file that being compiled
556 by lto. This structure contains the tables that are needed by the
557 serialized functions and ipa passes to connect themselves to the
558 global types and decls as they are reconstituted. */
559 struct GTY(()) lto_file_decl_data
561 /* Decl state currently used. */
562 struct lto_in_decl_state *current_decl_state;
564 /* Decl state corresponding to regions outside of any functions
565 in the compilation unit. */
566 struct lto_in_decl_state *global_decl_state;
568 /* Table of cgraph nodes present in this file. */
569 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
571 /* Hash table maps lto-related section names to location in file. */
572 hash_table<decl_state_hasher> *function_decl_states;
574 /* The .o file that these offsets relate to. */
575 const char *GTY((skip)) file_name;
577 /* Hash table maps lto-related section names to location in file. */
578 htab_t GTY((skip)) section_hash_table;
580 /* Hash new name of renamed global declaration to its original name. */
581 htab_t GTY((skip)) renaming_hash_table;
583 /* Linked list used temporarily in reader */
584 struct lto_file_decl_data *next;
586 /* Sub ID for merged objects. */
587 unsigned HOST_WIDE_INT id;
589 /* Symbol resolutions for this file */
590 vec<res_pair> GTY((skip)) respairs;
591 unsigned max_index;
593 struct gcov_ctr_summary GTY((skip)) profile_info;
595 /* Map assigning declarations their resolutions. */
596 hash_map<tree, ld_plugin_symbol_resolution> * GTY((skip)) resolution_map;
598 /* Mode translation table. */
599 const unsigned char *mode_table;
602 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
604 struct lto_char_ptr_base
606 char *ptr;
609 /* An incore byte stream to buffer the various parts of the function.
610 The entire structure should be zeroed when created. The record
611 consists of a set of blocks. The first sizeof (ptr) bytes are used
612 as a chain, and the rest store the bytes to be written. */
613 struct lto_output_stream
615 /* The pointer to the first block in the stream. */
616 struct lto_char_ptr_base * first_block;
618 /* The pointer to the last and current block in the stream. */
619 struct lto_char_ptr_base * current_block;
621 /* The pointer to where the next char should be written. */
622 char * current_pointer;
624 /* The number of characters left in the current block. */
625 unsigned int left_in_block;
627 /* The block size of the last block allocated. */
628 unsigned int block_size;
630 /* The total number of characters written. */
631 unsigned int total_size;
634 /* A simple output block. This can be used for simple IPA passes that
635 do not need more than one stream. */
636 struct lto_simple_output_block
638 enum lto_section_type section_type;
639 struct lto_out_decl_state *decl_state;
641 /* The stream that the main tree codes are written to. */
642 struct lto_output_stream *main_stream;
645 /* String hashing. */
647 struct string_slot
649 const char *s;
650 int len;
651 unsigned int slot_num;
654 /* Hashtable helpers. */
656 struct string_slot_hasher : nofree_ptr_hash <string_slot>
658 static inline hashval_t hash (const string_slot *);
659 static inline bool equal (const string_slot *, const string_slot *);
662 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
663 to support strings that may not end in '\0'. */
665 inline hashval_t
666 string_slot_hasher::hash (const string_slot *ds)
668 hashval_t r = ds->len;
669 int i;
671 for (i = 0; i < ds->len; i++)
672 r = r * 67 + (unsigned)ds->s[i] - 113;
673 return r;
676 /* Returns nonzero if DS1 and DS2 are equal. */
678 inline bool
679 string_slot_hasher::equal (const string_slot *ds1, const string_slot *ds2)
681 if (ds1->len == ds2->len)
682 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
684 return 0;
687 /* Data structure holding all the data and descriptors used when writing
688 an LTO file. */
689 struct output_block
691 enum lto_section_type section_type;
692 struct lto_out_decl_state *decl_state;
694 /* The stream that the main tree codes are written to. */
695 struct lto_output_stream *main_stream;
697 /* The stream that contains the string table. */
698 struct lto_output_stream *string_stream;
700 /* The stream that contains the cfg. */
701 struct lto_output_stream *cfg_stream;
703 /* The hash table that contains the set of strings we have seen so
704 far and the indexes assigned to them. */
705 hash_table<string_slot_hasher> *string_hash_table;
707 /* The current symbol that we are currently serializing. Null
708 if we are serializing something else. */
709 symtab_node *symbol;
711 /* These are the last file and line that were seen in the stream.
712 If the current node differs from these, it needs to insert
713 something into the stream and fix these up. */
714 const char *current_file;
715 int current_line;
716 int current_col;
717 bool current_sysp;
719 /* Cache of nodes written in this section. */
720 struct streamer_tree_cache_d *writer_cache;
722 /* All data persistent across whole duration of output block
723 can go here. */
724 struct obstack obstack;
728 /* Data and descriptors used when reading from an LTO file. */
729 struct data_in
731 /* The global decls and types. */
732 struct lto_file_decl_data *file_data;
734 /* The string table. */
735 const char *strings;
737 /* The length of the string table. */
738 unsigned int strings_len;
740 /* Maps each reference number to the resolution done by the linker. */
741 vec<ld_plugin_symbol_resolution_t> globals_resolution;
743 /* Cache of pickled nodes. */
744 struct streamer_tree_cache_d *reader_cache;
746 /* Cache of source code location. */
747 lto_location_cache location_cache;
751 /* In lto-section-in.c */
752 extern struct lto_input_block * lto_create_simple_input_block (
753 struct lto_file_decl_data *,
754 enum lto_section_type, const char **, size_t *);
755 extern void
756 lto_destroy_simple_input_block (struct lto_file_decl_data *,
757 enum lto_section_type,
758 struct lto_input_block *, const char *, size_t);
759 extern void lto_set_in_hooks (struct lto_file_decl_data **,
760 lto_get_section_data_f *,
761 lto_free_section_data_f *);
762 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
763 extern const char *lto_get_section_data (struct lto_file_decl_data *,
764 enum lto_section_type,
765 const char *, size_t *,
766 bool decompress = false);
767 extern const char *lto_get_raw_section_data (struct lto_file_decl_data *,
768 enum lto_section_type,
769 const char *, size_t *);
770 extern void lto_free_section_data (struct lto_file_decl_data *,
771 enum lto_section_type,
772 const char *, const char *, size_t,
773 bool decompress = false);
774 extern void lto_free_raw_section_data (struct lto_file_decl_data *,
775 enum lto_section_type,
776 const char *, const char *, size_t);
777 extern htab_t lto_create_renaming_table (void);
778 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
779 const char *, const char *);
780 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
781 const char *);
782 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
783 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
784 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
785 struct lto_file_decl_data *, tree);
786 extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
787 extern void lto_free_function_in_decl_state_for_node (symtab_node *);
788 extern void lto_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
789 extern void lto_value_range_error (const char *,
790 HOST_WIDE_INT, HOST_WIDE_INT,
791 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
793 /* In lto-section-out.c */
794 extern void lto_begin_section (const char *, bool);
795 extern void lto_end_section (void);
796 extern void lto_write_data (const void *, unsigned int);
797 extern void lto_write_raw_data (const void *, unsigned int);
798 extern void lto_write_stream (struct lto_output_stream *);
799 extern bool lto_output_decl_index (struct lto_output_stream *,
800 struct lto_tree_ref_encoder *,
801 tree, unsigned int *);
802 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
803 struct lto_output_stream *, tree);
804 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
805 struct lto_output_stream *, tree);
806 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
807 struct lto_output_stream *, tree);
808 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
809 struct lto_output_stream *, tree);
810 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
811 struct lto_output_stream *, tree);
812 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
813 struct lto_output_stream *, tree);
814 extern struct lto_simple_output_block *lto_create_simple_output_block (
815 enum lto_section_type);
816 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
817 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
818 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
819 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
820 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
821 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
822 extern void lto_record_function_out_decl_state (tree,
823 struct lto_out_decl_state *);
824 extern void lto_append_block (struct lto_output_stream *);
827 /* In lto-streamer.c. */
829 /* Set when streaming LTO for offloading compiler. */
830 extern bool lto_stream_offload_p;
832 extern const char *lto_tag_name (enum LTO_tags);
833 extern bitmap lto_bitmap_alloc (void);
834 extern void lto_bitmap_free (bitmap);
835 extern char *lto_get_section_name (int, const char *, 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 #ifdef LTO_STREAMER_DEBUG
840 extern void lto_orig_address_map (tree, intptr_t);
841 extern intptr_t lto_orig_address_get (tree);
842 extern void lto_orig_address_remove (tree);
843 #endif
844 extern void lto_check_version (int, int, const char *);
845 extern void lto_streamer_hooks_init (void);
847 /* In lto-streamer-in.c */
848 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
849 extern void lto_reader_init (void);
850 extern void lto_input_function_body (struct lto_file_decl_data *,
851 struct cgraph_node *,
852 const char *);
853 extern void lto_input_variable_constructor (struct lto_file_decl_data *,
854 struct varpool_node *,
855 const char *);
856 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
857 const char *);
858 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
859 extern void lto_input_mode_table (struct lto_file_decl_data *);
860 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
861 const char *, unsigned,
862 vec<ld_plugin_symbol_resolution_t> );
863 extern void lto_data_in_delete (struct data_in *);
864 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
865 void lto_input_location (location_t *, struct bitpack_d *, struct data_in *);
866 location_t stream_input_location_now (struct bitpack_d *bp,
867 struct data_in *data);
868 tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
869 struct function *, enum LTO_tags);
870 void lto_tag_check_set (enum LTO_tags, int, ...);
871 void lto_init_eh (void);
872 hashval_t lto_input_scc (struct lto_input_block *, struct data_in *,
873 unsigned *, unsigned *);
874 tree lto_input_tree_1 (struct lto_input_block *, struct data_in *,
875 enum LTO_tags, hashval_t hash);
876 tree lto_input_tree (struct lto_input_block *, struct data_in *);
879 /* In lto-streamer-out.c */
880 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
881 extern struct output_block *create_output_block (enum lto_section_type);
882 extern void destroy_output_block (struct output_block *);
883 extern void lto_output_tree (struct output_block *, tree, bool, bool);
884 extern void lto_output_toplevel_asms (void);
885 extern void produce_asm (struct output_block *ob, tree fn);
886 extern void lto_output ();
887 extern void produce_asm_for_decls ();
888 void lto_output_decl_state_streams (struct output_block *,
889 struct lto_out_decl_state *);
890 void lto_output_decl_state_refs (struct output_block *,
891 struct lto_output_stream *,
892 struct lto_out_decl_state *);
893 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
894 void lto_output_init_mode_table (void);
897 /* In lto-cgraph.c */
898 extern bool asm_nodes_output;
899 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
900 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
901 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
902 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
903 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
904 struct cgraph_node *);
905 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
906 symtab_node *);
907 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
908 symtab_node *);
910 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
911 varpool_node *);
912 void output_symtab (void);
913 void input_symtab (void);
914 void output_offload_tables (void);
915 void input_offload_tables (bool);
916 bool referenced_from_other_partition_p (struct ipa_ref_list *,
917 lto_symtab_encoder_t);
918 bool reachable_from_other_partition_p (struct cgraph_node *,
919 lto_symtab_encoder_t);
920 bool referenced_from_this_partition_p (symtab_node *,
921 lto_symtab_encoder_t);
922 bool reachable_from_this_partition_p (struct cgraph_node *,
923 lto_symtab_encoder_t);
924 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
925 void select_what_to_stream (void);
927 /* In options-save.c. */
928 void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
929 struct cl_target_option *);
931 void cl_target_option_stream_in (struct data_in *,
932 struct bitpack_d *,
933 struct cl_target_option *);
935 void cl_optimization_stream_out (struct bitpack_d *, struct cl_optimization *);
937 void cl_optimization_stream_in (struct bitpack_d *, struct cl_optimization *);
941 /* In lto-opts.c. */
942 extern void lto_write_options (void);
945 /* Statistics gathered during LTO, WPA and LTRANS. */
946 extern struct lto_stats_d lto_stats;
948 /* Section names corresponding to the values of enum lto_section_type. */
949 extern const char *lto_section_name[];
951 /* Holds all the out decl states of functions output so far in the
952 current output file. */
953 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
955 /* Return true if LTO tag TAG corresponds to a tree code. */
956 static inline bool
957 lto_tag_is_tree_code_p (enum LTO_tags tag)
959 return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
963 /* Return true if LTO tag TAG corresponds to a gimple code. */
964 static inline bool
965 lto_tag_is_gimple_code_p (enum LTO_tags tag)
967 return (unsigned) tag >= NUM_TREE_CODES + 2
968 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
972 /* Return the LTO tag corresponding to gimple code CODE. See enum
973 LTO_tags for details on the conversion. */
974 static inline enum LTO_tags
975 lto_gimple_code_to_tag (enum gimple_code code)
977 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
981 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
982 details on the conversion. */
983 static inline enum gimple_code
984 lto_tag_to_gimple_code (enum LTO_tags tag)
986 gcc_assert (lto_tag_is_gimple_code_p (tag));
987 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
991 /* Return the LTO tag corresponding to tree code CODE. See enum
992 LTO_tags for details on the conversion. */
993 static inline enum LTO_tags
994 lto_tree_code_to_tag (enum tree_code code)
996 return (enum LTO_tags) ((unsigned) code + 2);
1000 /* Return the tree code corresponding to TAG. See enum LTO_tags for
1001 details on the conversion. */
1002 static inline enum tree_code
1003 lto_tag_to_tree_code (enum LTO_tags tag)
1005 gcc_assert (lto_tag_is_tree_code_p (tag));
1006 return (enum tree_code) ((unsigned) tag - 2);
1009 /* Check that tag ACTUAL == EXPECTED. */
1010 static inline void
1011 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
1013 if (actual != expected)
1014 internal_error ("bytecode stream: expected tag %s instead of %s",
1015 lto_tag_name (expected), lto_tag_name (actual));
1018 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
1019 static inline void
1020 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
1021 enum LTO_tags tag2)
1023 if (actual < tag1 || actual > tag2)
1024 internal_error ("bytecode stream: tag %s is not in the expected range "
1025 "[%s, %s]",
1026 lto_tag_name (actual),
1027 lto_tag_name (tag1),
1028 lto_tag_name (tag2));
1031 /* Initialize an lto_out_decl_buffer ENCODER. */
1032 static inline void
1033 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1035 encoder->tree_hash_table = new hash_map<tree, unsigned> (251);
1036 encoder->trees.create (0);
1040 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1041 memory used by ENCODER is not freed by this function. */
1042 static inline void
1043 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1045 /* Hash table may be delete already. */
1046 delete encoder->tree_hash_table;
1047 encoder->tree_hash_table = NULL;
1048 encoder->trees.release ();
1051 /* Return the number of trees encoded in ENCODER. */
1052 static inline unsigned int
1053 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1055 return encoder->trees.length ();
1058 /* Return the IDX-th tree in ENCODER. */
1059 static inline tree
1060 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1061 unsigned int idx)
1063 return encoder->trees[idx];
1066 /* Return number of encoded nodes in ENCODER. */
1067 static inline int
1068 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1070 return encoder->nodes.length ();
1073 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1074 #define LCC_NOT_FOUND (-1)
1076 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1077 or LCC_NOT_FOUND if it is not there. */
1079 static inline int
1080 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1081 symtab_node *node)
1083 size_t *slot = encoder->map->get (node);
1084 return (slot && *slot ? *(slot) - 1 : LCC_NOT_FOUND);
1087 /* Return true if iterator LSE points to nothing. */
1088 static inline bool
1089 lsei_end_p (lto_symtab_encoder_iterator lsei)
1091 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1094 /* Advance iterator LSE. */
1095 static inline void
1096 lsei_next (lto_symtab_encoder_iterator *lsei)
1098 lsei->index++;
1101 /* Return the node pointed to by LSI. */
1102 static inline symtab_node *
1103 lsei_node (lto_symtab_encoder_iterator lsei)
1105 return lsei.encoder->nodes[lsei.index].node;
1108 /* Return the node pointed to by LSI. */
1109 static inline struct cgraph_node *
1110 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1112 return dyn_cast<cgraph_node *> (lsei.encoder->nodes[lsei.index].node);
1115 /* Return the node pointed to by LSI. */
1116 static inline varpool_node *
1117 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1119 return dyn_cast<varpool_node *> (lsei.encoder->nodes[lsei.index].node);
1122 /* Return the cgraph node corresponding to REF using ENCODER. */
1124 static inline symtab_node *
1125 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1127 if (ref == LCC_NOT_FOUND)
1128 return NULL;
1130 return encoder->nodes[ref].node;
1133 /* Return an iterator to the first node in LSI. */
1134 static inline lto_symtab_encoder_iterator
1135 lsei_start (lto_symtab_encoder_t encoder)
1137 lto_symtab_encoder_iterator lsei;
1139 lsei.encoder = encoder;
1140 lsei.index = 0;
1141 return lsei;
1144 /* Advance iterator LSE. */
1145 static inline void
1146 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1148 lsei_next (lsei);
1149 while (!lsei_end_p (*lsei)
1150 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1151 lsei_next (lsei);
1154 /* Return an iterator to the first node in LSI. */
1155 static inline lto_symtab_encoder_iterator
1156 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1158 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1160 if (lsei_end_p (lsei))
1161 return lsei;
1162 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1163 lsei_next_in_partition (&lsei);
1165 return lsei;
1168 /* Advance iterator LSE. */
1169 static inline void
1170 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1172 lsei_next (lsei);
1173 while (!lsei_end_p (*lsei)
1174 && (!is_a <cgraph_node *> (lsei_node (*lsei))
1175 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1176 lsei_next (lsei);
1179 /* Return an iterator to the first node in LSI. */
1180 static inline lto_symtab_encoder_iterator
1181 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1183 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1185 if (lsei_end_p (lsei))
1186 return lsei;
1187 if (!is_a <cgraph_node *> (lsei_node (lsei))
1188 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1189 lsei_next_function_in_partition (&lsei);
1191 return lsei;
1194 /* Advance iterator LSE. */
1195 static inline void
1196 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1198 lsei_next (lsei);
1199 while (!lsei_end_p (*lsei)
1200 && (!is_a <varpool_node *> (lsei_node (*lsei))
1201 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1202 lsei_next (lsei);
1205 /* Return an iterator to the first node in LSI. */
1206 static inline lto_symtab_encoder_iterator
1207 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1209 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1211 if (lsei_end_p (lsei))
1212 return lsei;
1213 if (!is_a <varpool_node *> (lsei_node (lsei))
1214 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1215 lsei_next_variable_in_partition (&lsei);
1217 return lsei;
1220 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1221 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1222 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1223 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1224 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1225 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1226 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1228 #endif /* GCC_LTO_STREAMER_H */