svn merge -r205223:206958 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / lto-streamer.h
blob4bd7b33c45056259993ec78c2c1f181d843aaeea
1 /* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
4 Copyright (C) 2009-2014 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 "hash-table.h"
28 #include "target.h"
29 #include "cgraph.h"
30 #include "vec.h"
31 #include "alloc-pool.h"
32 #include "gcov-io.h"
33 #include "diagnostic.h"
34 #include "pointer-set.h"
36 /* Define when debugging the LTO streamer. This causes the writer
37 to output the numeric value for the memory address of the tree node
38 being emitted. When debugging a problem in the reader, check the
39 original address that the writer was emitting using lto_orig_address_get.
40 With this value, set a breakpoint in the writer (e.g., lto_output_tree)
41 to trace how the faulty node is being emitted. */
42 /* #define LTO_STREAMER_DEBUG 1 */
44 /* The encoding for a function consists of the following sections:
46 1) The header.
47 2) FIELD_DECLS.
48 3) FUNCTION_DECLS.
49 4) global VAR_DECLS.
50 5) type_decls
51 6) types.
52 7) Names for the labels that have names
53 8) The SSA names.
54 9) The control flow graph.
55 10-11)Gimple for local decls.
56 12) Gimple for the function.
57 13) Strings.
59 1) THE HEADER.
60 2-6) THE GLOBAL DECLS AND TYPES.
62 The global decls and types are encoded in the same way. For each
63 entry, there is word with the offset within the section to the
64 entry.
66 7) THE LABEL NAMES.
68 Since most labels do not have names, this section my be of zero
69 length. It consists of an array of string table references, one
70 per label. In the lto code, the labels are given either
71 positive or negative indexes. the positive ones have names and
72 the negative ones do not. The positive index can be used to
73 find the name in this array.
75 9) THE CFG.
77 10) Index into the local decls. Since local decls can have local
78 decls inside them, they must be read in randomly in order to
79 properly restore them.
81 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
83 The gimple consists of a set of records.
85 THE FUNCTION
87 At the top level of (8) is the function. It consists of five
88 pieces:
90 LTO_function - The tag.
91 eh tree - This is all of the exception handling regions
92 put out in a post order traversial of the
93 tree. Siblings are output as lists terminated
94 by a 0. The set of fields matches the fields
95 defined in except.c.
97 last_basic_block - in uleb128 form.
99 basic blocks - This is the set of basic blocks.
101 zero - The termination of the basic blocks.
103 BASIC BLOCKS
105 There are two forms of basic blocks depending on if they are
106 empty or not.
108 The basic block consists of:
110 LTO_bb1 or LTO_bb0 - The tag.
112 bb->index - the index in uleb128 form.
114 #succs - The number of successors un uleb128 form.
116 the successors - For each edge, a pair. The first of the
117 pair is the index of the successor in
118 uleb128 form and the second are the flags in
119 uleb128 form.
121 the statements - A gimple tree, as described above.
122 These are only present for LTO_BB1.
123 Following each statement is an optional
124 exception handling record LTO_eh_region
125 which contains the region number (for
126 regions >= 0).
128 zero - This is only present for LTO_BB1 and is used
129 to terminate the statements and exception
130 regions within this block.
132 12) STRINGS
134 String are represented in the table as pairs, a length in ULEB128
135 form followed by the data for the string. */
137 /* The string that is the prefix on the section names we make for lto.
138 For decls the DECL_ASSEMBLER_NAME is appended to make the section
139 name for the functions and static_initializers. For other types of
140 sections a '.' and the section type are appended. */
141 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
142 #define OMP_SECTION_NAME_PREFIX ".gnu.target_lto_"
144 /* Can be either OMP_SECTION_NAME_PREFIX when we stream pragma omp target
145 stuff, or LTO_SECTION_NAME_PREFIX for lto case. */
146 extern const char *section_name_prefix;
148 #define LTO_major_version 2
149 #define LTO_minor_version 2
151 typedef unsigned char lto_decl_flags_t;
154 /* Tags representing the various IL objects written to the bytecode file
155 (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
157 NOTE, when adding new LTO tags, also update lto_tag_name. */
158 enum LTO_tags
160 LTO_null = 0,
162 /* Special for streamer. Reference to previously-streamed node. */
163 LTO_tree_pickle_reference,
165 /* Reserve enough entries to fit all the tree and gimple codes handled
166 by the streamer. This guarantees that:
168 1- Given a tree code C:
169 enum LTO_tags tag == C + 1
171 2- Given a gimple code C:
172 enum LTO_tags tag == C + NUM_TREE_CODES + 1
174 Conversely, to map between LTO tags and tree/gimple codes, the
175 reverse operation must be applied. */
176 LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
177 LTO_bb1,
179 /* EH region holding the previous statement. */
180 LTO_eh_region,
182 /* An MD or NORMAL builtin. Only the code and class are streamed out. */
183 LTO_builtin_decl,
185 /* Shared INTEGER_CST node. */
186 LTO_integer_cst,
188 /* Function body. */
189 LTO_function,
191 /* EH table. */
192 LTO_eh_table,
194 /* EH region types. These mirror enum eh_region_type. */
195 LTO_ert_cleanup,
196 LTO_ert_try,
197 LTO_ert_allowed_exceptions,
198 LTO_ert_must_not_throw,
200 /* EH landing pad. */
201 LTO_eh_landing_pad,
203 /* EH try/catch node. */
204 LTO_eh_catch,
206 /* Special for global streamer. A blob of unnamed tree nodes. */
207 LTO_tree_scc,
209 /* References to indexable tree nodes. These objects are stored in
210 tables that are written separately from the function bodies that
211 reference them. This way they can be instantiated even when the
212 referencing functions aren't (e.g., during WPA) and it also allows
213 functions to be copied from one file to another without having
214 to unpickle the body first (the references are location
215 independent).
217 NOTE, do not regroup these values as the grouping is exposed
218 in the range checks done in lto_input_tree. */
219 LTO_field_decl_ref, /* Do not change. */
220 LTO_function_decl_ref,
221 LTO_label_decl_ref,
222 LTO_namespace_decl_ref,
223 LTO_result_decl_ref,
224 LTO_ssa_name_ref,
225 LTO_type_decl_ref,
226 LTO_type_ref,
227 LTO_const_decl_ref,
228 LTO_imported_decl_ref,
229 LTO_translation_unit_decl_ref,
230 LTO_global_decl_ref,
231 LTO_namelist_decl_ref, /* Do not change. */
233 /* This tag must always be last. */
234 LTO_NUM_TAGS
238 /* Set of section types that are in an LTO file. This list will grow
239 as the number of IPA passes grows since each IPA pass will need its
240 own section type to store its summary information.
242 When adding a new section type, you must also extend the
243 LTO_SECTION_NAME array in lto-section-in.c. */
244 enum lto_section_type
246 LTO_section_decls = 0,
247 LTO_section_function_body,
248 LTO_section_static_initializer,
249 LTO_section_symtab,
250 LTO_section_refs,
251 LTO_section_asm,
252 LTO_section_jump_functions,
253 LTO_section_ipa_pure_const,
254 LTO_section_ipa_reference,
255 LTO_section_ipa_profile,
256 LTO_section_symtab_nodes,
257 LTO_section_opts,
258 LTO_section_cgraph_opt_sum,
259 LTO_section_inline_summary,
260 LTO_section_ipcp_transform,
261 LTO_N_SECTION_TYPES /* Must be last. */
264 /* Indices to the various function, type and symbol streams. */
265 enum lto_decl_stream_e_t
267 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
268 LTO_DECL_STREAM_FIELD_DECL,
269 LTO_DECL_STREAM_FN_DECL,
270 LTO_DECL_STREAM_VAR_DECL,
271 LTO_DECL_STREAM_TYPE_DECL,
272 LTO_DECL_STREAM_NAMESPACE_DECL,
273 LTO_DECL_STREAM_LABEL_DECL,
274 LTO_N_DECL_STREAMS
277 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
280 /* Macro to define convenience functions for type and decl streams
281 in lto_file_decl_data. */
282 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
283 static inline tree \
284 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
285 unsigned int idx) \
287 struct lto_in_decl_state *state = data->current_decl_state; \
288 gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
289 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
292 static inline unsigned int \
293 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
295 struct lto_in_decl_state *state = data->current_decl_state; \
296 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
300 /* Return a char pointer to the start of a data stream for an lto pass
301 or function. The first parameter is the file data that contains
302 the information. The second parameter is the type of information
303 to be obtained. The third parameter is the name of the function
304 and is only used when finding a function body; otherwise it is
305 NULL. The fourth parameter is the length of the data returned. */
306 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
307 enum lto_section_type,
308 const char *,
309 size_t *);
311 /* Return the data found from the above call. The first three
312 parameters are the same as above. The fourth parameter is the data
313 itself and the fifth is the length of the data. */
314 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
315 enum lto_section_type,
316 const char *,
317 const char *,
318 size_t);
320 /* Structure used as buffer for reading an LTO file. */
321 struct lto_input_block
323 const char *data;
324 unsigned int p;
325 unsigned int len;
328 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
329 do { \
330 BASE.data = D; \
331 BASE.p = P; \
332 BASE.len = L; \
333 } while (0)
335 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
336 do { \
337 BASE->data = D; \
338 BASE->p = P; \
339 BASE->len = L; \
340 } while (0)
343 /* The is the first part of the record for a function or constructor
344 in the .o file. */
345 struct lto_header
347 int16_t major_version;
348 int16_t minor_version;
351 /* The header for a function body. */
352 struct lto_function_header
354 /* The header for all types of sections. */
355 struct lto_header lto_header;
357 /* Number of labels with names. */
358 int32_t num_named_labels;
360 /* Number of labels without names. */
361 int32_t num_unnamed_labels;
363 /* Size compressed or 0 if not compressed. */
364 int32_t compressed_size;
366 /* Size of names for named labels. */
367 int32_t named_label_size;
369 /* Size of the cfg. */
370 int32_t cfg_size;
372 /* Size of main gimple body of function. */
373 int32_t main_size;
375 /* Size of the string table. */
376 int32_t string_size;
380 /* Structure describing a symbol section. */
381 struct lto_decl_header
383 /* The header for all types of sections. */
384 struct lto_header lto_header;
386 /* Size of region for decl state. */
387 int32_t decl_state_size;
389 /* Number of nodes in globals stream. */
390 int32_t num_nodes;
392 /* Size of region for expressions, decls, types, etc. */
393 int32_t main_size;
395 /* Size of the string table. */
396 int32_t string_size;
400 /* Structure describing top level asm()s. */
401 struct lto_asm_header
403 /* The header for all types of sections. */
404 struct lto_header lto_header;
406 /* Size compressed or 0 if not compressed. */
407 int32_t compressed_size;
409 /* Size of region for expressions, decls, types, etc. */
410 int32_t main_size;
412 /* Size of the string table. */
413 int32_t string_size;
417 /* Statistics gathered during LTO, WPA and LTRANS. */
418 struct lto_stats_d
420 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
421 unsigned HOST_WIDE_INT num_output_symtab_nodes;
422 unsigned HOST_WIDE_INT num_input_files;
423 unsigned HOST_WIDE_INT num_output_files;
424 unsigned HOST_WIDE_INT num_cgraph_partitions;
425 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
426 unsigned HOST_WIDE_INT num_function_bodies;
427 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
428 unsigned HOST_WIDE_INT num_output_il_bytes;
429 unsigned HOST_WIDE_INT num_compressed_il_bytes;
430 unsigned HOST_WIDE_INT num_input_il_bytes;
431 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
432 unsigned HOST_WIDE_INT num_tree_bodies_output;
433 unsigned HOST_WIDE_INT num_pickle_refs_output;
436 /* Entry of LTO symtab encoder. */
437 struct lto_encoder_entry
439 symtab_node *node;
440 /* Is the node in this partition (i.e. ltrans of this partition will
441 be responsible for outputting it)? */
442 unsigned int in_partition:1;
443 /* Do we encode body in this partition? */
444 unsigned int body:1;
445 /* Do we encode initializer in this partition?
446 For example the readonly variable initializers are encoded to aid
447 constant folding even if they are not in the partition. */
448 unsigned int initializer:1;
452 /* Encoder data structure used to stream callgraph nodes. */
453 struct lto_symtab_encoder_d
455 vec<lto_encoder_entry> nodes;
456 pointer_map_t *map;
459 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
461 /* Iterator structure for cgraph node sets. */
462 struct lto_symtab_encoder_iterator
464 lto_symtab_encoder_t encoder;
465 unsigned index;
471 /* Mapping from indices to trees. */
472 struct GTY(()) lto_tree_ref_table
474 /* Array of referenced trees . */
475 tree * GTY((length ("%h.size"))) trees;
477 /* Size of array. */
478 unsigned int size;
482 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
484 struct lto_tree_ref_encoder
486 pointer_map<unsigned> *tree_hash_table; /* Maps pointers to indices. */
487 vec<tree> trees; /* Maps indices to pointers. */
491 /* Structure to hold states of input scope. */
492 struct GTY(()) lto_in_decl_state
494 /* Array of lto_in_decl_buffers to store type and decls streams. */
495 struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
497 /* If this in-decl state is associated with a function. FN_DECL
498 point to the FUNCTION_DECL. */
499 tree fn_decl;
502 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
505 /* The structure that holds all of the vectors of global types,
506 decls and cgraph nodes used in the serialization of this file. */
507 struct lto_out_decl_state
509 /* The buffers contain the sets of decls of various kinds and types we have
510 seen so far and the indexes assigned to them. */
511 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
513 /* Encoder for cgraph nodes. */
514 lto_symtab_encoder_t symtab_node_encoder;
516 /* If this out-decl state belongs to a function, fn_decl points to that
517 function. Otherwise, it is NULL. */
518 tree fn_decl;
521 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
524 /* Compact representation of a index <-> resolution pair. Unpacked to an
525 vector later. */
526 struct res_pair
528 ld_plugin_symbol_resolution_t res;
529 unsigned index;
533 /* One of these is allocated for each object file that being compiled
534 by lto. This structure contains the tables that are needed by the
535 serialized functions and ipa passes to connect themselves to the
536 global types and decls as they are reconstituted. */
537 struct GTY(()) lto_file_decl_data
539 /* Decl state currently used. */
540 struct lto_in_decl_state *current_decl_state;
542 /* Decl state corresponding to regions outside of any functions
543 in the compilation unit. */
544 struct lto_in_decl_state *global_decl_state;
546 /* Table of cgraph nodes present in this file. */
547 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
549 /* Hash table maps lto-related section names to location in file. */
550 htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
552 /* The .o file that these offsets relate to. */
553 const char *GTY((skip)) file_name;
555 /* Hash table maps lto-related section names to location in file. */
556 htab_t GTY((skip)) section_hash_table;
558 /* Hash new name of renamed global declaration to its original name. */
559 htab_t GTY((skip)) renaming_hash_table;
561 /* Linked list used temporarily in reader */
562 struct lto_file_decl_data *next;
564 /* Sub ID for merged objects. */
565 unsigned HOST_WIDE_INT id;
567 /* Symbol resolutions for this file */
568 vec<res_pair> GTY((skip)) respairs;
569 unsigned max_index;
571 struct gcov_ctr_summary GTY((skip)) profile_info;
573 /* Map assigning declarations their resolutions. */
574 pointer_map_t * GTY((skip)) resolution_map;
577 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
579 struct lto_char_ptr_base
581 char *ptr;
584 /* An incore byte stream to buffer the various parts of the function.
585 The entire structure should be zeroed when created. The record
586 consists of a set of blocks. The first sizeof (ptr) bytes are used
587 as a chain, and the rest store the bytes to be written. */
588 struct lto_output_stream
590 /* The pointer to the first block in the stream. */
591 struct lto_char_ptr_base * first_block;
593 /* The pointer to the last and current block in the stream. */
594 struct lto_char_ptr_base * current_block;
596 /* The pointer to where the next char should be written. */
597 char * current_pointer;
599 /* The number of characters left in the current block. */
600 unsigned int left_in_block;
602 /* The block size of the last block allocated. */
603 unsigned int block_size;
605 /* The total number of characters written. */
606 unsigned int total_size;
609 /* The is the first part of the record in an LTO file for many of the
610 IPA passes. */
611 struct lto_simple_header
613 /* The header for all types of sections. */
614 struct lto_header lto_header;
616 /* Size of main gimple body of function. */
617 int32_t main_size;
619 /* Size of main stream when compressed. */
620 int32_t compressed_size;
623 /* A simple output block. This can be used for simple IPA passes that
624 do not need more than one stream. */
625 struct lto_simple_output_block
627 enum lto_section_type section_type;
628 struct lto_out_decl_state *decl_state;
630 /* The stream that the main tree codes are written to. */
631 struct lto_output_stream *main_stream;
634 /* String hashing. */
636 struct string_slot
638 const char *s;
639 int len;
640 unsigned int slot_num;
643 /* Hashtable helpers. */
645 struct string_slot_hasher : typed_noop_remove <string_slot>
647 typedef string_slot value_type;
648 typedef string_slot compare_type;
649 static inline hashval_t hash (const value_type *);
650 static inline bool equal (const value_type *, const compare_type *);
653 /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
654 to support strings that may not end in '\0'. */
656 inline hashval_t
657 string_slot_hasher::hash (const value_type *ds)
659 hashval_t r = ds->len;
660 int i;
662 for (i = 0; i < ds->len; i++)
663 r = r * 67 + (unsigned)ds->s[i] - 113;
664 return r;
667 /* Returns nonzero if DS1 and DS2 are equal. */
669 inline bool
670 string_slot_hasher::equal (const value_type *ds1, const compare_type *ds2)
672 if (ds1->len == ds2->len)
673 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
675 return 0;
678 /* Data structure holding all the data and descriptors used when writing
679 an LTO file. */
680 struct output_block
682 enum lto_section_type section_type;
683 struct lto_out_decl_state *decl_state;
685 /* The stream that the main tree codes are written to. */
686 struct lto_output_stream *main_stream;
688 /* The stream that contains the string table. */
689 struct lto_output_stream *string_stream;
691 /* The stream that contains the cfg. */
692 struct lto_output_stream *cfg_stream;
694 /* The hash table that contains the set of strings we have seen so
695 far and the indexes assigned to them. */
696 hash_table <string_slot_hasher> string_hash_table;
698 /* The current cgraph_node that we are currently serializing. Null
699 if we are serializing something else. */
700 struct cgraph_node *cgraph_node;
702 /* These are the last file and line that were seen in the stream.
703 If the current node differs from these, it needs to insert
704 something into the stream and fix these up. */
705 const char *current_file;
706 int current_line;
707 int current_col;
709 /* True if writing globals and types. */
710 bool global;
712 /* Cache of nodes written in this section. */
713 struct streamer_tree_cache_d *writer_cache;
715 /* All data persistent across whole duration of output block
716 can go here. */
717 struct obstack obstack;
721 /* Data and descriptors used when reading from an LTO file. */
722 struct data_in
724 /* The global decls and types. */
725 struct lto_file_decl_data *file_data;
727 /* All of the labels. */
728 tree *labels;
730 /* The string table. */
731 const char *strings;
733 /* The length of the string table. */
734 unsigned int strings_len;
736 /* Number of named labels. Used to find the index of unnamed labels
737 since they share space with the named labels. */
738 unsigned int num_named_labels;
740 /* Number of unnamed labels. */
741 unsigned int num_unnamed_labels;
743 /* Maps each reference number to the resolution done by the linker. */
744 vec<ld_plugin_symbol_resolution_t> globals_resolution;
746 /* Cache of pickled nodes. */
747 struct streamer_tree_cache_d *reader_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 extern void lto_free_section_data (struct lto_file_decl_data *,
767 enum lto_section_type,
768 const char *, const char *, size_t);
769 extern htab_t lto_create_renaming_table (void);
770 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
771 const char *, const char *);
772 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
773 const char *);
774 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
775 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
776 extern hashval_t lto_hash_in_decl_state (const void *);
777 extern int lto_eq_in_decl_state (const void *, const void *);
778 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
779 struct lto_file_decl_data *, tree);
780 extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
781 extern void lto_free_function_in_decl_state_for_node (symtab_node *);
782 extern void lto_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
783 extern void lto_value_range_error (const char *,
784 HOST_WIDE_INT, HOST_WIDE_INT,
785 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
787 /* In lto-section-out.c */
788 extern void lto_begin_section (const char *, bool);
789 extern void lto_end_section (void);
790 extern void lto_write_stream (struct lto_output_stream *);
791 extern void lto_output_data_stream (struct lto_output_stream *, const void *,
792 size_t);
793 extern bool lto_output_decl_index (struct lto_output_stream *,
794 struct lto_tree_ref_encoder *,
795 tree, unsigned int *);
796 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
797 struct lto_output_stream *, tree);
798 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
799 struct lto_output_stream *, tree);
800 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
801 struct lto_output_stream *, tree);
802 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
803 struct lto_output_stream *, tree);
804 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
805 struct lto_output_stream *, tree);
806 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
807 struct lto_output_stream *, tree);
808 extern struct lto_simple_output_block *lto_create_simple_output_block (
809 enum lto_section_type);
810 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
811 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
812 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
813 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
814 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
815 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
816 extern void lto_record_function_out_decl_state (tree,
817 struct lto_out_decl_state *);
818 extern void lto_append_block (struct lto_output_stream *);
821 /* In lto-streamer.c. */
822 extern const char *lto_tag_name (enum LTO_tags);
823 extern bitmap lto_bitmap_alloc (void);
824 extern void lto_bitmap_free (bitmap);
825 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
826 extern void print_lto_report (const char *);
827 extern void lto_streamer_init (void);
828 extern bool gate_lto_out (void);
829 #ifdef LTO_STREAMER_DEBUG
830 extern void lto_orig_address_map (tree, intptr_t);
831 extern intptr_t lto_orig_address_get (tree);
832 extern void lto_orig_address_remove (tree);
833 #endif
834 extern void lto_check_version (int, int);
835 extern void lto_streamer_hooks_init (void);
837 /* In lto-streamer-in.c */
838 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
839 extern void lto_reader_init (void);
840 extern void lto_input_function_body (struct lto_file_decl_data *,
841 struct cgraph_node *,
842 const char *);
843 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
844 const char *);
845 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
846 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
847 const char *, unsigned,
848 vec<ld_plugin_symbol_resolution_t> );
849 extern void lto_data_in_delete (struct data_in *);
850 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
851 location_t lto_input_location (struct bitpack_d *, struct data_in *);
852 tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
853 struct function *, enum LTO_tags);
854 void lto_tag_check_set (enum LTO_tags, int, ...);
855 void lto_init_eh (void);
856 hashval_t lto_input_scc (struct lto_input_block *, struct data_in *,
857 unsigned *, unsigned *);
858 tree lto_input_tree_1 (struct lto_input_block *, struct data_in *,
859 enum LTO_tags, hashval_t hash);
860 tree lto_input_tree (struct lto_input_block *, struct data_in *);
863 /* In lto-streamer-out.c */
864 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
865 extern struct output_block *create_output_block (enum lto_section_type);
866 extern void destroy_output_block (struct output_block *);
867 extern void lto_output_tree (struct output_block *, tree, bool, bool);
868 extern void lto_output_toplevel_asms (void);
869 extern void produce_asm (struct output_block *ob, tree fn);
870 extern void lto_output ();
871 extern void produce_asm_for_decls ();
872 void lto_output_decl_state_streams (struct output_block *,
873 struct lto_out_decl_state *);
874 void lto_output_decl_state_refs (struct output_block *,
875 struct lto_output_stream *,
876 struct lto_out_decl_state *);
877 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
880 /* In lto-cgraph.c */
881 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
882 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node *);
883 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
884 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node *);
885 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
886 struct cgraph_node *);
887 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
888 symtab_node *);
889 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
890 symtab_node *);
892 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
893 varpool_node *);
894 void output_symtab (void);
895 void input_symtab (void);
896 bool referenced_from_other_partition_p (struct ipa_ref_list *,
897 lto_symtab_encoder_t);
898 bool reachable_from_other_partition_p (struct cgraph_node *,
899 lto_symtab_encoder_t);
900 bool referenced_from_this_partition_p (struct ipa_ref_list *,
901 lto_symtab_encoder_t);
902 bool reachable_from_this_partition_p (struct cgraph_node *,
903 lto_symtab_encoder_t);
904 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
905 void select_what_to_dump (bool);
908 /* In lto-symtab.c. */
909 extern void lto_symtab_merge_decls (void);
910 extern void lto_symtab_merge_symbols (void);
911 extern tree lto_symtab_prevailing_decl (tree decl);
914 /* In lto-opts.c. */
915 extern void lto_write_options (void);
918 /* Statistics gathered during LTO, WPA and LTRANS. */
919 extern struct lto_stats_d lto_stats;
921 /* Section names corresponding to the values of enum lto_section_type. */
922 extern const char *lto_section_name[];
924 /* Holds all the out decl states of functions output so far in the
925 current output file. */
926 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
928 /* Return true if LTO tag TAG corresponds to a tree code. */
929 static inline bool
930 lto_tag_is_tree_code_p (enum LTO_tags tag)
932 return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
936 /* Return true if LTO tag TAG corresponds to a gimple code. */
937 static inline bool
938 lto_tag_is_gimple_code_p (enum LTO_tags tag)
940 return (unsigned) tag >= NUM_TREE_CODES + 2
941 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
945 /* Return the LTO tag corresponding to gimple code CODE. See enum
946 LTO_tags for details on the conversion. */
947 static inline enum LTO_tags
948 lto_gimple_code_to_tag (enum gimple_code code)
950 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
954 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
955 details on the conversion. */
956 static inline enum gimple_code
957 lto_tag_to_gimple_code (enum LTO_tags tag)
959 gcc_assert (lto_tag_is_gimple_code_p (tag));
960 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
964 /* Return the LTO tag corresponding to tree code CODE. See enum
965 LTO_tags for details on the conversion. */
966 static inline enum LTO_tags
967 lto_tree_code_to_tag (enum tree_code code)
969 return (enum LTO_tags) ((unsigned) code + 2);
973 /* Return the tree code corresponding to TAG. See enum LTO_tags for
974 details on the conversion. */
975 static inline enum tree_code
976 lto_tag_to_tree_code (enum LTO_tags tag)
978 gcc_assert (lto_tag_is_tree_code_p (tag));
979 return (enum tree_code) ((unsigned) tag - 2);
982 /* Check that tag ACTUAL == EXPECTED. */
983 static inline void
984 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
986 if (actual != expected)
987 internal_error ("bytecode stream: expected tag %s instead of %s",
988 lto_tag_name (expected), lto_tag_name (actual));
991 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
992 static inline void
993 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
994 enum LTO_tags tag2)
996 if (actual < tag1 || actual > tag2)
997 internal_error ("bytecode stream: tag %s is not in the expected range "
998 "[%s, %s]",
999 lto_tag_name (actual),
1000 lto_tag_name (tag1),
1001 lto_tag_name (tag2));
1004 /* Initialize an lto_out_decl_buffer ENCODER. */
1005 static inline void
1006 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1008 encoder->tree_hash_table = new pointer_map<unsigned>;
1009 encoder->trees.create (0);
1013 /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1014 memory used by ENCODER is not freed by this function. */
1015 static inline void
1016 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1018 /* Hash table may be delete already. */
1019 if (encoder->tree_hash_table)
1020 delete encoder->tree_hash_table;
1021 encoder->trees.release ();
1024 /* Return the number of trees encoded in ENCODER. */
1025 static inline unsigned int
1026 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1028 return encoder->trees.length ();
1031 /* Return the IDX-th tree in ENCODER. */
1032 static inline tree
1033 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1034 unsigned int idx)
1036 return encoder->trees[idx];
1039 /* Return number of encoded nodes in ENCODER. */
1040 static inline int
1041 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1043 return encoder->nodes.length ();
1046 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1047 #define LCC_NOT_FOUND (-1)
1049 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1050 or LCC_NOT_FOUND if it is not there. */
1052 static inline int
1053 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1054 symtab_node *node)
1056 void **slot = pointer_map_contains (encoder->map, node);
1057 return (slot && *slot ? (size_t) *(slot) - 1 : LCC_NOT_FOUND);
1060 /* Return true if iterator LSE points to nothing. */
1061 static inline bool
1062 lsei_end_p (lto_symtab_encoder_iterator lsei)
1064 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1067 /* Advance iterator LSE. */
1068 static inline void
1069 lsei_next (lto_symtab_encoder_iterator *lsei)
1071 lsei->index++;
1074 /* Return the node pointed to by LSI. */
1075 static inline symtab_node *
1076 lsei_node (lto_symtab_encoder_iterator lsei)
1078 return lsei.encoder->nodes[lsei.index].node;
1081 /* Return the node pointed to by LSI. */
1082 static inline struct cgraph_node *
1083 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1085 return cgraph (lsei.encoder->nodes[lsei.index].node);
1088 /* Return the node pointed to by LSI. */
1089 static inline varpool_node *
1090 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1092 return varpool (lsei.encoder->nodes[lsei.index].node);
1095 /* Return the cgraph node corresponding to REF using ENCODER. */
1097 static inline symtab_node *
1098 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1100 if (ref == LCC_NOT_FOUND)
1101 return NULL;
1103 return encoder->nodes[ref].node;
1106 /* Return an iterator to the first node in LSI. */
1107 static inline lto_symtab_encoder_iterator
1108 lsei_start (lto_symtab_encoder_t encoder)
1110 lto_symtab_encoder_iterator lsei;
1112 lsei.encoder = encoder;
1113 lsei.index = 0;
1114 return lsei;
1117 /* Advance iterator LSE. */
1118 static inline void
1119 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1121 lsei_next (lsei);
1122 while (!lsei_end_p (*lsei)
1123 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1124 lsei_next (lsei);
1127 /* Return an iterator to the first node in LSI. */
1128 static inline lto_symtab_encoder_iterator
1129 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1131 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1133 if (lsei_end_p (lsei))
1134 return lsei;
1135 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1136 lsei_next_in_partition (&lsei);
1138 return lsei;
1141 /* Advance iterator LSE. */
1142 static inline void
1143 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1145 lsei_next (lsei);
1146 while (!lsei_end_p (*lsei)
1147 && (!is_a <cgraph_node> (lsei_node (*lsei))
1148 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1149 lsei_next (lsei);
1152 /* Return an iterator to the first node in LSI. */
1153 static inline lto_symtab_encoder_iterator
1154 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1156 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1158 if (lsei_end_p (lsei))
1159 return lsei;
1160 if (!is_a <cgraph_node> (lsei_node (lsei))
1161 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1162 lsei_next_function_in_partition (&lsei);
1164 return lsei;
1167 /* Advance iterator LSE. */
1168 static inline void
1169 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1171 lsei_next (lsei);
1172 while (!lsei_end_p (*lsei)
1173 && (!is_a <varpool_node> (lsei_node (*lsei))
1174 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1175 lsei_next (lsei);
1178 /* Return an iterator to the first node in LSI. */
1179 static inline lto_symtab_encoder_iterator
1180 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1182 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1184 if (lsei_end_p (lsei))
1185 return lsei;
1186 if (!is_a <varpool_node> (lsei_node (lsei))
1187 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1188 lsei_next_variable_in_partition (&lsei);
1190 return lsei;
1193 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1194 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1195 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1196 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1197 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1198 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1199 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1201 #endif /* GCC_LTO_STREAMER_H */