* lto-streamer-out.c (pack_ts_type_value_fields): Pack all bits
[official-gcc.git] / gcc / lto-streamer.h
blob73afd4607d7c67481000945b5575b07808e918fc
1 /* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
4 Copyright (C) 2009, 2010 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 "tree.h"
28 #include "gimple.h"
29 #include "target.h"
30 #include "cgraph.h"
31 #include "vec.h"
32 #include "vecprim.h"
33 #include "alloc-pool.h"
34 #include "gcov-io.h"
36 /* Forward declarations to avoid included unnecessary headers. */
37 struct output_block;
38 struct lto_input_block;
39 struct data_in;
40 struct bitpack_d;
42 /* Define when debugging the LTO streamer. This causes the writer
43 to output the numeric value for the memory address of the tree node
44 being emitted. When debugging a problem in the reader, check the
45 original address that the writer was emitting using lto_orig_address_get.
46 With this value, set a breakpoint in the writer (e.g., lto_output_tree)
47 to trace how the faulty node is being emitted. */
48 /* #define LTO_STREAMER_DEBUG 1 */
50 /* Streamer hooks. These functions do additional processing as
51 needed by the module. There are two types of callbacks, those that
52 replace the default behavior and those that supplement it. Any
53 or all of these hooks can be NULL. */
54 typedef struct lto_streamer_hooks {
55 /* A string identifying this streamer. */
56 const char *name;
58 /* Called by lto_reader_init after it does basic initialization. */
59 void (*reader_init) (void);
61 /* Called by lto_writer_init after it does basic initalization. */
62 void (*writer_init) (void);
64 /* Return true if the given tree is supported by this streamer. */
65 bool (*is_streamable) (tree);
67 /* Called by lto_write_tree after writing all the common parts of
68 a tree. If defined, the callback is in charge of writing all
69 the fields that lto_write_tree did not write out. Arguments
70 are as in lto_write_tree.
72 The following tree fields are not handled by common code:
74 DECL_ABSTRACT_ORIGIN
75 DECL_INITIAL
76 DECL_SAVED_TREE
78 Callbacks may choose to ignore or handle them. If handled,
79 the reader should read them in the exact same sequence written
80 by the writer. */
81 void (*write_tree) (struct output_block *, tree, bool);
83 /* Called by lto_read_tree after reading all the common parts of
84 a tree. If defined, the callback is in charge of reading all
85 the fields that lto_read_tree did not read in. Arguments
86 are as in lto_read_tree. */
87 void (*read_tree) (struct lto_input_block *, struct data_in *, tree);
89 /* Called by lto_output_tree_ref to determine if the given tree node
90 should be emitted as a reference to the table of declarations
91 (the same table that holds VAR_DECLs). */
92 bool (*indexable_with_decls_p) (tree);
94 /* Called by pack_value_fields to store any non-pointer fields
95 in the tree structure. The arguments are as in pack_value_fields. */
96 void (*pack_value_fields) (struct bitpack_d *, tree);
98 /* Called by unpack_value_fields to retrieve any non-pointer fields
99 in the tree structure. The arguments are as in unpack_value_fields. */
100 void (*unpack_value_fields) (struct bitpack_d *, tree);
102 /* Non-zero if the streamer should register decls in the LTO
103 global symbol tables. */
104 unsigned register_decls_in_symtab_p : 1;
106 /* Called by lto_materialize_tree for tree nodes that it does not
107 know how to allocate memory for. If defined, this hook should
108 return a new tree node of the given code. The data_in and
109 input_block arguments are passed in case the hook needs to
110 read more data from the stream to allocate the node.
111 If this hook returns NULL, then lto_materialize_tree will attempt
112 to allocate the tree by calling make_node directly. */
113 tree (*alloc_tree) (enum tree_code, struct lto_input_block *,
114 struct data_in *);
116 /* Called by lto_output_tree_header to write any streamer-specific
117 information needed to allocate the tree. This hook may assume
118 that the basic header data (tree code, etc) has already been
119 written. It should only write any extra data needed to allocate
120 the node (e.g., in the case of CALL_EXPR, this hook would write
121 the number of arguments to the CALL_EXPR). */
122 void (*output_tree_header) (struct output_block *, tree);
123 } lto_streamer_hooks;
126 /* The encoding for a function consists of the following sections:
128 1) The header.
129 2) FIELD_DECLS.
130 3) FUNCTION_DECLS.
131 4) global VAR_DECLS.
132 5) type_decls
133 6) types.
134 7) Names for the labels that have names
135 8) The SSA names.
136 9) The control flow graph.
137 10-11)Gimple for local decls.
138 12) Gimple for the function.
139 13) Strings.
141 1) THE HEADER.
142 2-6) THE GLOBAL DECLS AND TYPES.
144 The global decls and types are encoded in the same way. For each
145 entry, there is word with the offset within the section to the
146 entry.
148 7) THE LABEL NAMES.
150 Since most labels do not have names, this section my be of zero
151 length. It consists of an array of string table references, one
152 per label. In the lto code, the labels are given either
153 positive or negative indexes. the positive ones have names and
154 the negative ones do not. The positive index can be used to
155 find the name in this array.
157 9) THE CFG.
159 10) Index into the local decls. Since local decls can have local
160 decls inside them, they must be read in randomly in order to
161 properly restore them.
163 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
165 The gimple consists of a set of records.
167 THE FUNCTION
169 At the top level of (8) is the function. It consists of five
170 pieces:
172 LTO_function - The tag.
173 eh tree - This is all of the exception handling regions
174 put out in a post order traversial of the
175 tree. Siblings are output as lists terminated
176 by a 0. The set of fields matches the fields
177 defined in except.c.
179 last_basic_block - in uleb128 form.
181 basic blocks - This is the set of basic blocks.
183 zero - The termination of the basic blocks.
185 BASIC BLOCKS
187 There are two forms of basic blocks depending on if they are
188 empty or not.
190 The basic block consists of:
192 LTO_bb1 or LTO_bb0 - The tag.
194 bb->index - the index in uleb128 form.
196 #succs - The number of successors un uleb128 form.
198 the successors - For each edge, a pair. The first of the
199 pair is the index of the successor in
200 uleb128 form and the second are the flags in
201 uleb128 form.
203 the statements - A gimple tree, as described above.
204 These are only present for LTO_BB1.
205 Following each statement is an optional
206 exception handling record LTO_eh_region
207 which contains the region number (for
208 regions >= 0).
210 zero - This is only present for LTO_BB1 and is used
211 to terminate the statements and exception
212 regions within this block.
214 12) STRINGS
216 String are represented in the table as pairs, a length in ULEB128
217 form followed by the data for the string. */
219 /* The string that is the prefix on the section names we make for lto.
220 For decls the DECL_ASSEMBLER_NAME is appended to make the section
221 name for the functions and static_initializers. For other types of
222 sections a '.' and the section type are appended. */
223 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
225 #define LTO_major_version 2
226 #define LTO_minor_version 0
228 typedef unsigned char lto_decl_flags_t;
231 /* Data structures used to pack values and bitflags into a vector of
232 words. Used to stream values of a fixed number of bits in a space
233 efficient way. */
234 static unsigned const BITS_PER_BITPACK_WORD = HOST_BITS_PER_WIDE_INT;
236 typedef unsigned HOST_WIDE_INT bitpack_word_t;
237 DEF_VEC_I(bitpack_word_t);
238 DEF_VEC_ALLOC_I(bitpack_word_t, heap);
240 struct bitpack_d
242 /* The position of the first unused or unconsumed bit in the word. */
243 unsigned pos;
245 /* The current word we are (un)packing. */
246 bitpack_word_t word;
248 /* The lto_output_stream or the lto_input_block we are streaming to/from. */
249 void *stream;
252 /* Tags representing the various IL objects written to the bytecode file
253 (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
255 NOTE, when adding new LTO tags, also update lto_tag_name. */
256 enum LTO_tags
258 LTO_null = 0,
260 /* Reserve enough entries to fit all the tree and gimple codes handled
261 by the streamer. This guarantees that:
263 1- Given a tree code C:
264 enum LTO_tags tag == C + 1
266 2- Given a gimple code C:
267 enum LTO_tags tag == C + NUM_TREE_CODES + 1
269 Conversely, to map between LTO tags and tree/gimple codes, the
270 reverse operation must be applied. */
271 LTO_bb0 = 1 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
272 LTO_bb1,
274 /* EH region holding the previous statement. */
275 LTO_eh_region,
277 /* An MD or NORMAL builtin. Only the code and class are streamed out. */
278 LTO_builtin_decl,
280 /* Function body. */
281 LTO_function,
283 /* EH table. */
284 LTO_eh_table,
286 /* EH region types. These mirror enum eh_region_type. */
287 LTO_ert_cleanup,
288 LTO_ert_try,
289 LTO_ert_allowed_exceptions,
290 LTO_ert_must_not_throw,
292 /* EH landing pad. */
293 LTO_eh_landing_pad,
295 /* EH try/catch node. */
296 LTO_eh_catch,
298 /* Special for global streamer. Reference to previously-streamed node. */
299 LTO_tree_pickle_reference,
301 /* References to indexable tree nodes. These objects are stored in
302 tables that are written separately from the function bodies that
303 reference them. This way they can be instantiated even when the
304 referencing functions aren't (e.g., during WPA) and it also allows
305 functions to be copied from one file to another without having
306 to unpickle the body first (the references are location
307 independent).
309 NOTE, do not regroup these values as the grouping is exposed
310 in the range checks done in lto_input_tree. */
311 LTO_field_decl_ref, /* Do not change. */
312 LTO_function_decl_ref,
313 LTO_label_decl_ref,
314 LTO_namespace_decl_ref,
315 LTO_result_decl_ref,
316 LTO_ssa_name_ref,
317 LTO_type_decl_ref,
318 LTO_type_ref,
319 LTO_const_decl_ref,
320 LTO_imported_decl_ref,
321 LTO_translation_unit_decl_ref,
322 LTO_global_decl_ref, /* Do not change. */
323 LTO_LAST_TAG,
325 /* This tag must always be last. */
326 LTO_NUM_TAGS = LTO_LAST_TAG + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE
330 /* Set of section types that are in an LTO file. This list will grow
331 as the number of IPA passes grows since each IPA pass will need its
332 own section type to store its summary information.
334 When adding a new section type, you must also extend the
335 LTO_SECTION_NAME array in lto-section-in.c. */
336 enum lto_section_type
338 LTO_section_decls = 0,
339 LTO_section_function_body,
340 LTO_section_static_initializer,
341 LTO_section_cgraph,
342 LTO_section_varpool,
343 LTO_section_refs,
344 LTO_section_jump_functions,
345 LTO_section_ipa_pure_const,
346 LTO_section_ipa_reference,
347 LTO_section_symtab,
348 LTO_section_opts,
349 LTO_section_cgraph_opt_sum,
350 LTO_N_SECTION_TYPES /* Must be last. */
353 /* Indices to the various function, type and symbol streams. */
354 typedef enum
356 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
357 LTO_DECL_STREAM_FIELD_DECL,
358 LTO_DECL_STREAM_FN_DECL,
359 LTO_DECL_STREAM_VAR_DECL,
360 LTO_DECL_STREAM_TYPE_DECL,
361 LTO_DECL_STREAM_NAMESPACE_DECL,
362 LTO_DECL_STREAM_LABEL_DECL,
363 LTO_N_DECL_STREAMS
364 } lto_decl_stream_e_t;
366 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
367 DEF_VEC_I(ld_plugin_symbol_resolution_t);
368 DEF_VEC_ALLOC_I(ld_plugin_symbol_resolution_t, heap);
371 /* Macro to define convenience functions for type and decl streams
372 in lto_file_decl_data. */
373 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
374 static inline tree \
375 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
376 unsigned int idx) \
378 struct lto_in_decl_state *state = data->current_decl_state; \
379 gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
380 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
383 static inline unsigned int \
384 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
386 struct lto_in_decl_state *state = data->current_decl_state; \
387 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
391 /* Return a char pointer to the start of a data stream for an lto pass
392 or function. The first parameter is the file data that contains
393 the information. The second parameter is the type of information
394 to be obtained. The third parameter is the name of the function
395 and is only used when finding a function body; otherwise it is
396 NULL. The fourth parameter is the length of the data returned. */
397 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
398 enum lto_section_type,
399 const char *,
400 size_t *);
402 /* Return the data found from the above call. The first three
403 parameters are the same as above. The fourth parameter is the data
404 itself and the fifth is the lenght of the data. */
405 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
406 enum lto_section_type,
407 const char *,
408 const char *,
409 size_t);
411 /* Cache of pickled nodes. Used to avoid writing the same node more
412 than once. The first time a tree node is streamed out, it is
413 entered in this cache. Subsequent references to the same node are
414 resolved by looking it up in this cache.
416 This is used in two ways:
418 - On the writing side, the first time T is added to STREAMER_CACHE,
419 a new reference index is created for T and T is emitted on the
420 stream. If T needs to be emitted again to the stream, instead of
421 pickling it again, the reference index is emitted.
423 - On the reading side, the first time T is read from the stream, it
424 is reconstructed in memory and a new reference index created for
425 T. The reconstructed T is inserted in some array so that when
426 the reference index for T is found in the input stream, it can be
427 used to look up into the array to get the reconstructed T. */
428 struct lto_streamer_cache_d
430 /* The mapping between tree nodes and slots into the nodes array. */
431 htab_t node_map;
433 /* Node map to store entries into. */
434 alloc_pool node_map_entries;
436 /* Next available slot in the nodes and offsets arrays. */
437 unsigned next_slot;
439 /* The nodes pickled so far. */
440 VEC(tree,heap) *nodes;
442 /* Offset into the stream where the nodes have been written. */
443 VEC(unsigned,heap) *offsets;
447 /* Structure used as buffer for reading an LTO file. */
448 struct lto_input_block
450 const char *data;
451 unsigned int p;
452 unsigned int len;
455 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
456 do { \
457 BASE.data = D; \
458 BASE.p = P; \
459 BASE.len = L; \
460 } while (0)
462 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
463 do { \
464 BASE->data = D; \
465 BASE->p = P; \
466 BASE->len = L; \
467 } while (0)
470 /* The is the first part of the record for a function or constructor
471 in the .o file. */
472 struct lto_header
474 int16_t major_version;
475 int16_t minor_version;
476 enum lto_section_type section_type;
479 /* The header for a function body. */
480 struct lto_function_header
482 /* The header for all types of sections. */
483 struct lto_header lto_header;
485 /* Number of labels with names. */
486 int32_t num_named_labels;
488 /* Number of labels without names. */
489 int32_t num_unnamed_labels;
491 /* Size compressed or 0 if not compressed. */
492 int32_t compressed_size;
494 /* Size of names for named labels. */
495 int32_t named_label_size;
497 /* Size of the cfg. */
498 int32_t cfg_size;
500 /* Size of main gimple body of function. */
501 int32_t main_size;
503 /* Size of the string table. */
504 int32_t string_size;
508 /* Structure describing a symbol section. */
509 struct lto_decl_header
511 /* The header for all types of sections. */
512 struct lto_header lto_header;
514 /* Size of region for decl state. */
515 int32_t decl_state_size;
517 /* Number of nodes in globals stream. */
518 int32_t num_nodes;
520 /* Size of region for expressions, decls, types, etc. */
521 int32_t main_size;
523 /* Size of the string table. */
524 int32_t string_size;
528 /* Statistics gathered during LTO, WPA and LTRANS. */
529 struct lto_stats_d
531 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
532 unsigned HOST_WIDE_INT num_output_cgraph_nodes;
533 unsigned HOST_WIDE_INT num_input_files;
534 unsigned HOST_WIDE_INT num_output_files;
535 unsigned HOST_WIDE_INT num_cgraph_partitions;
536 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
537 unsigned HOST_WIDE_INT num_function_bodies;
538 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
539 unsigned HOST_WIDE_INT num_output_il_bytes;
540 unsigned HOST_WIDE_INT num_compressed_il_bytes;
541 unsigned HOST_WIDE_INT num_input_il_bytes;
542 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
545 /* Encoder data structure used to stream callgraph nodes. */
546 struct lto_cgraph_encoder_d
548 /* Map nodes to reference number. */
549 struct pointer_map_t *map;
551 /* Map reference number to node. */
552 VEC(cgraph_node_ptr,heap) *nodes;
554 /* Map of nodes where we want to output body. */
555 struct pointer_set_t *body;
558 typedef struct lto_cgraph_encoder_d *lto_cgraph_encoder_t;
560 /* Return number of encoded nodes in ENCODER. */
562 static inline int
563 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
565 return VEC_length (cgraph_node_ptr, encoder->nodes);
569 /* Encoder data structure used to stream callgraph nodes. */
570 struct lto_varpool_encoder_d
572 /* Map nodes to reference number. */
573 struct pointer_map_t *map;
575 /* Map reference number to node. */
576 VEC(varpool_node_ptr,heap) *nodes;
578 /* Map of nodes where we want to output initializer. */
579 struct pointer_set_t *initializer;
581 typedef struct lto_varpool_encoder_d *lto_varpool_encoder_t;
583 /* Return number of encoded nodes in ENCODER. */
585 static inline int
586 lto_varpool_encoder_size (lto_varpool_encoder_t encoder)
588 return VEC_length (varpool_node_ptr, encoder->nodes);
591 /* Mapping from indices to trees. */
592 struct GTY(()) lto_tree_ref_table
594 /* Array of referenced trees . */
595 tree * GTY((length ("%h.size"))) trees;
597 /* Size of array. */
598 unsigned int size;
602 /* Mapping between trees and slots in an array. */
603 struct lto_decl_slot
605 tree t;
606 int slot_num;
610 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
612 struct lto_tree_ref_encoder
614 htab_t tree_hash_table; /* Maps pointers to indices. */
615 unsigned int next_index; /* Next available index. */
616 VEC(tree,heap) *trees; /* Maps indices to pointers. */
620 /* Structure to hold states of input scope. */
621 struct GTY(()) lto_in_decl_state
623 /* Array of lto_in_decl_buffers to store type and decls streams. */
624 struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
626 /* If this in-decl state is associated with a function. FN_DECL
627 point to the FUNCTION_DECL. */
628 tree fn_decl;
631 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
634 /* The structure that holds all of the vectors of global types,
635 decls and cgraph nodes used in the serialization of this file. */
636 struct lto_out_decl_state
638 /* The buffers contain the sets of decls of various kinds and types we have
639 seen so far and the indexes assigned to them. */
640 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
642 /* Encoder for cgraph nodes. */
643 lto_cgraph_encoder_t cgraph_node_encoder;
645 /* Encoder for varpool nodes. */
646 lto_varpool_encoder_t varpool_node_encoder;
648 /* If this out-decl state belongs to a function, fn_decl points to that
649 function. Otherwise, it is NULL. */
650 tree fn_decl;
653 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
655 DEF_VEC_P(lto_out_decl_state_ptr);
656 DEF_VEC_ALLOC_P(lto_out_decl_state_ptr, heap);
658 /* One of these is allocated for each object file that being compiled
659 by lto. This structure contains the tables that are needed by the
660 serialized functions and ipa passes to connect themselves to the
661 global types and decls as they are reconstituted. */
662 struct GTY(()) lto_file_decl_data
664 /* Decl state currently used. */
665 struct lto_in_decl_state *current_decl_state;
667 /* Decl state corresponding to regions outside of any functions
668 in the compilation unit. */
669 struct lto_in_decl_state *global_decl_state;
671 /* Table of cgraph nodes present in this file. */
672 lto_cgraph_encoder_t GTY((skip)) cgraph_node_encoder;
674 /* Table of varpool nodes present in this file. */
675 lto_varpool_encoder_t GTY((skip)) varpool_node_encoder;
677 /* Hash table maps lto-related section names to location in file. */
678 htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
680 /* The .o file that these offsets relate to. */
681 const char *GTY((skip)) file_name;
683 /* Hash table maps lto-related section names to location in file. */
684 htab_t GTY((skip)) section_hash_table;
686 /* Hash new name of renamed global declaration to its original name. */
687 htab_t GTY((skip)) renaming_hash_table;
689 /* Linked list used temporarily in reader */
690 struct lto_file_decl_data *next;
692 /* Sub ID for merged objects. */
693 unsigned id;
695 /* Symbol resolutions for this file */
696 VEC(ld_plugin_symbol_resolution_t,heap) * GTY((skip)) resolutions;
698 struct gcov_ctr_summary GTY((skip)) profile_info;
700 /* Any other streamer-specific data needed by the streamer. */
701 void * GTY((skip)) sdata;
704 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
706 struct lto_char_ptr_base
708 char *ptr;
711 /* An incore byte stream to buffer the various parts of the function.
712 The entire structure should be zeroed when created. The record
713 consists of a set of blocks. The first sizeof (ptr) bytes are used
714 as a chain, and the rest store the bytes to be written. */
715 struct lto_output_stream
717 /* The pointer to the first block in the stream. */
718 struct lto_char_ptr_base * first_block;
720 /* The pointer to the last and current block in the stream. */
721 struct lto_char_ptr_base * current_block;
723 /* The pointer to where the next char should be written. */
724 char * current_pointer;
726 /* The number of characters left in the current block. */
727 unsigned int left_in_block;
729 /* The block size of the last block allocated. */
730 unsigned int block_size;
732 /* The total number of characters written. */
733 unsigned int total_size;
736 /* The is the first part of the record in an LTO file for many of the
737 IPA passes. */
738 struct lto_simple_header
740 /* The header for all types of sections. */
741 struct lto_header lto_header;
743 /* Size of main gimple body of function. */
744 int32_t main_size;
746 /* Size of main stream when compressed. */
747 int32_t compressed_size;
750 /* A simple output block. This can be used for simple IPA passes that
751 do not need more than one stream. */
752 struct lto_simple_output_block
754 enum lto_section_type section_type;
755 struct lto_out_decl_state *decl_state;
757 /* The stream that the main tree codes are written to. */
758 struct lto_output_stream *main_stream;
761 /* Data structure holding all the data and descriptors used when writing
762 an LTO file. */
763 struct output_block
765 enum lto_section_type section_type;
766 struct lto_out_decl_state *decl_state;
768 /* The stream that the main tree codes are written to. */
769 struct lto_output_stream *main_stream;
771 /* The stream that contains the string table. */
772 struct lto_output_stream *string_stream;
774 /* The stream that contains the cfg. */
775 struct lto_output_stream *cfg_stream;
777 /* The hash table that contains the set of strings we have seen so
778 far and the indexes assigned to them. */
779 htab_t string_hash_table;
781 /* The current cgraph_node that we are currently serializing. Null
782 if we are serializing something else. */
783 struct cgraph_node *cgraph_node;
785 /* These are the last file and line that were seen in the stream.
786 If the current node differs from these, it needs to insert
787 something into the stream and fix these up. */
788 const char *current_file;
789 int current_line;
790 int current_col;
792 /* True if writing globals and types. */
793 bool global;
795 /* Cache of nodes written in this section. */
796 struct lto_streamer_cache_d *writer_cache;
798 /* Any other streamer-specific data needed by the streamer. */
799 void *sdata;
803 /* Data and descriptors used when reading from an LTO file. */
804 struct data_in
806 /* The global decls and types. */
807 struct lto_file_decl_data *file_data;
809 /* All of the labels. */
810 tree *labels;
812 /* The string table. */
813 const char *strings;
815 /* The length of the string table. */
816 unsigned int strings_len;
818 /* Number of named labels. Used to find the index of unnamed labels
819 since they share space with the named labels. */
820 unsigned int num_named_labels;
822 /* Number of unnamed labels. */
823 unsigned int num_unnamed_labels;
825 const char *current_file;
826 int current_line;
827 int current_col;
829 /* Maps each reference number to the resolution done by the linker. */
830 VEC(ld_plugin_symbol_resolution_t,heap) *globals_resolution;
832 /* Cache of pickled nodes. */
833 struct lto_streamer_cache_d *reader_cache;
835 /* Any other streamer-specific data needed by the streamer. */
836 void *sdata;
840 /* In lto-section-in.c */
841 extern struct lto_input_block * lto_create_simple_input_block (
842 struct lto_file_decl_data *,
843 enum lto_section_type, const char **, size_t *);
844 extern void
845 lto_destroy_simple_input_block (struct lto_file_decl_data *,
846 enum lto_section_type,
847 struct lto_input_block *, const char *, size_t);
848 extern void lto_set_in_hooks (struct lto_file_decl_data **,
849 lto_get_section_data_f *,
850 lto_free_section_data_f *);
851 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
852 extern const char *lto_get_section_data (struct lto_file_decl_data *,
853 enum lto_section_type,
854 const char *, size_t *);
855 extern void lto_free_section_data (struct lto_file_decl_data *,
856 enum lto_section_type,
857 const char *, const char *, size_t);
858 extern unsigned char lto_input_1_unsigned (struct lto_input_block *);
859 extern unsigned HOST_WIDE_INT lto_input_uleb128 (struct lto_input_block *);
860 extern unsigned HOST_WIDEST_INT lto_input_widest_uint_uleb128 (
861 struct lto_input_block *);
862 extern HOST_WIDE_INT lto_input_sleb128 (struct lto_input_block *);
863 extern htab_t lto_create_renaming_table (void);
864 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
865 const char *, const char *);
866 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
867 const char *);
868 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
869 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
870 extern hashval_t lto_hash_in_decl_state (const void *);
871 extern int lto_eq_in_decl_state (const void *, const void *);
872 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
873 struct lto_file_decl_data *, tree);
875 /* In lto-section-out.c */
876 extern hashval_t lto_hash_decl_slot_node (const void *);
877 extern int lto_eq_decl_slot_node (const void *, const void *);
878 extern hashval_t lto_hash_type_slot_node (const void *);
879 extern int lto_eq_type_slot_node (const void *, const void *);
880 extern void lto_begin_section (const char *, bool);
881 extern void lto_end_section (void);
882 extern void lto_write_stream (struct lto_output_stream *);
883 extern void lto_output_1_stream (struct lto_output_stream *, char);
884 extern void lto_output_data_stream (struct lto_output_stream *, const void *,
885 size_t);
886 extern void lto_output_uleb128_stream (struct lto_output_stream *,
887 unsigned HOST_WIDE_INT);
888 extern void lto_output_widest_uint_uleb128_stream (struct lto_output_stream *,
889 unsigned HOST_WIDEST_INT);
890 extern void lto_output_sleb128_stream (struct lto_output_stream *,
891 HOST_WIDE_INT);
892 extern bool lto_output_decl_index (struct lto_output_stream *,
893 struct lto_tree_ref_encoder *,
894 tree, unsigned int *);
895 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
896 struct lto_output_stream *, tree);
897 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
898 struct lto_output_stream *, tree);
899 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
900 struct lto_output_stream *, tree);
901 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
902 struct lto_output_stream *, tree);
903 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
904 struct lto_output_stream *, tree);
905 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
906 struct lto_output_stream *, tree);
907 extern struct lto_simple_output_block *lto_create_simple_output_block (
908 enum lto_section_type);
909 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
910 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
911 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
912 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
913 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
914 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
915 extern void lto_record_function_out_decl_state (tree,
916 struct lto_out_decl_state *);
919 /* In lto-streamer.c. */
920 extern const char *lto_tag_name (enum LTO_tags);
921 extern bitmap lto_bitmap_alloc (void);
922 extern void lto_bitmap_free (bitmap);
923 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
924 extern void print_lto_report (void);
925 extern bool lto_streamer_cache_insert (struct lto_streamer_cache_d *, tree,
926 int *, unsigned *);
927 extern bool lto_streamer_cache_insert_at (struct lto_streamer_cache_d *, tree,
928 int);
929 extern bool lto_streamer_cache_lookup (struct lto_streamer_cache_d *, tree,
930 int *);
931 extern tree lto_streamer_cache_get (struct lto_streamer_cache_d *, int);
932 extern struct lto_streamer_cache_d *lto_streamer_cache_create (void);
933 extern void lto_streamer_cache_delete (struct lto_streamer_cache_d *);
934 extern void lto_streamer_init (void);
935 extern bool gate_lto_out (void);
936 #ifdef LTO_STREAMER_DEBUG
937 extern void lto_orig_address_map (tree, intptr_t);
938 extern intptr_t lto_orig_address_get (tree);
939 extern void lto_orig_address_remove (tree);
940 #endif
941 extern void lto_check_version (int, int);
942 extern void gimple_streamer_hooks_init (void);
943 extern void gimple_streamer_write_tree (struct output_block *, tree, bool);
944 extern void gimple_streamer_read_tree (struct lto_input_block *,
945 struct data_in *, tree);
946 extern lto_streamer_hooks *streamer_hooks (void);
947 extern lto_streamer_hooks *streamer_hooks_init (void);
949 /* In lto-streamer-in.c */
950 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
951 extern void lto_reader_init (void);
952 extern tree lto_input_tree (struct lto_input_block *, struct data_in *);
953 extern tree lto_input_chain (struct lto_input_block *, struct data_in *);
954 extern void lto_input_function_body (struct lto_file_decl_data *, tree,
955 const char *);
956 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
957 const char *);
958 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
959 const char *, unsigned,
960 VEC(ld_plugin_symbol_resolution_t,heap) *);
961 extern void lto_data_in_delete (struct data_in *);
962 extern const char *lto_input_string (struct data_in *,
963 struct lto_input_block *);
964 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
965 extern void gimple_streamer_reader_init (void);
968 /* In lto-streamer-out.c */
969 extern void lto_writer_init (void);
970 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
971 extern struct output_block *create_output_block (enum lto_section_type);
972 extern void destroy_output_block (struct output_block *);
973 extern void lto_output_tree (struct output_block *, tree, bool);
974 extern void lto_output_tree_or_ref (struct output_block *, tree, bool);
975 extern void lto_output_chain (struct output_block *, tree, bool);
976 extern void produce_asm (struct output_block *ob, tree fn);
977 extern void lto_output_string (struct output_block *,
978 struct lto_output_stream *,
979 const char *);
980 extern void lto_output_string_with_length (struct output_block *,
981 struct lto_output_stream *,
982 const char *,
983 unsigned int);
984 void lto_output_decl_state_streams (struct output_block *,
985 struct lto_out_decl_state *);
986 void lto_output_decl_state_refs (struct output_block *,
987 struct lto_output_stream *,
988 struct lto_out_decl_state *);
991 /* In lto-cgraph.c */
992 struct cgraph_node *lto_cgraph_encoder_deref (lto_cgraph_encoder_t, int);
993 int lto_cgraph_encoder_lookup (lto_cgraph_encoder_t, struct cgraph_node *);
994 lto_cgraph_encoder_t lto_cgraph_encoder_new (void);
995 int lto_cgraph_encoder_encode (lto_cgraph_encoder_t, struct cgraph_node *);
996 void lto_cgraph_encoder_delete (lto_cgraph_encoder_t);
997 bool lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t,
998 struct cgraph_node *);
1000 bool lto_varpool_encoder_encode_body_p (lto_varpool_encoder_t,
1001 struct varpool_node *);
1002 struct varpool_node *lto_varpool_encoder_deref (lto_varpool_encoder_t, int);
1003 int lto_varpool_encoder_lookup (lto_varpool_encoder_t, struct varpool_node *);
1004 lto_varpool_encoder_t lto_varpool_encoder_new (void);
1005 int lto_varpool_encoder_encode (lto_varpool_encoder_t, struct varpool_node *);
1006 void lto_varpool_encoder_delete (lto_varpool_encoder_t);
1007 bool lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t,
1008 struct varpool_node *);
1009 void output_cgraph (cgraph_node_set, varpool_node_set);
1010 void input_cgraph (void);
1011 bool referenced_from_other_partition_p (struct ipa_ref_list *,
1012 cgraph_node_set,
1013 varpool_node_set vset);
1014 bool reachable_from_other_partition_p (struct cgraph_node *,
1015 cgraph_node_set);
1016 bool referenced_from_this_partition_p (struct ipa_ref_list *,
1017 cgraph_node_set,
1018 varpool_node_set vset);
1019 bool reachable_from_this_partition_p (struct cgraph_node *,
1020 cgraph_node_set);
1021 void compute_ltrans_boundary (struct lto_out_decl_state *state,
1022 cgraph_node_set, varpool_node_set);
1025 /* In lto-symtab.c. */
1026 extern void lto_symtab_register_decl (tree, ld_plugin_symbol_resolution_t,
1027 struct lto_file_decl_data *);
1028 extern void lto_symtab_merge_decls (void);
1029 extern void lto_symtab_merge_cgraph_nodes (void);
1030 extern tree lto_symtab_prevailing_decl (tree decl);
1031 extern enum ld_plugin_symbol_resolution lto_symtab_get_resolution (tree decl);
1032 extern void lto_symtab_free (void);
1033 extern GTY(()) VEC(tree,gc) *lto_global_var_decls;
1036 /* In lto-opts.c. */
1037 extern void lto_register_user_option (size_t, const char *, int, int);
1038 extern void lto_read_file_options (struct lto_file_decl_data *);
1039 extern void lto_write_options (void);
1040 extern void lto_reissue_options (void);
1041 void lto_clear_user_options (void);
1042 void lto_clear_file_options (void);
1045 /* In lto-wpa-fixup.c */
1046 void lto_mark_nothrow_fndecl (tree);
1047 void lto_fixup_nothrow_decls (void);
1050 /* Statistics gathered during LTO, WPA and LTRANS. */
1051 extern struct lto_stats_d lto_stats;
1053 /* Section names corresponding to the values of enum lto_section_type. */
1054 extern const char *lto_section_name[];
1056 /* Holds all the out decl states of functions output so far in the
1057 current output file. */
1058 extern VEC(lto_out_decl_state_ptr, heap) *lto_function_decl_states;
1060 /* Return true if LTO tag TAG corresponds to a tree code. */
1061 static inline bool
1062 lto_tag_is_tree_code_p (enum LTO_tags tag)
1064 return tag > LTO_null && (unsigned) tag <= MAX_TREE_CODES;
1068 /* Return true if LTO tag TAG corresponds to a gimple code. */
1069 static inline bool
1070 lto_tag_is_gimple_code_p (enum LTO_tags tag)
1072 return (unsigned) tag >= NUM_TREE_CODES + 1
1073 && (unsigned) tag < 1 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
1077 /* Return the LTO tag corresponding to gimple code CODE. See enum
1078 LTO_tags for details on the conversion. */
1079 static inline enum LTO_tags
1080 lto_gimple_code_to_tag (enum gimple_code code)
1082 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 1);
1086 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
1087 details on the conversion. */
1088 static inline enum gimple_code
1089 lto_tag_to_gimple_code (enum LTO_tags tag)
1091 gcc_assert (lto_tag_is_gimple_code_p (tag));
1092 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 1);
1096 /* Return the LTO tag corresponding to tree code CODE. See enum
1097 LTO_tags for details on the conversion. */
1098 static inline enum LTO_tags
1099 lto_tree_code_to_tag (enum tree_code code)
1101 return (enum LTO_tags) ((unsigned) code + 1);
1105 /* Return the tree code corresponding to TAG. See enum LTO_tags for
1106 details on the conversion. */
1107 static inline enum tree_code
1108 lto_tag_to_tree_code (enum LTO_tags tag)
1110 gcc_assert (lto_tag_is_tree_code_p (tag));
1111 return (enum tree_code) ((unsigned) tag - 1);
1114 /* Initialize an lto_out_decl_buffer ENCODER. */
1115 static inline void
1116 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder,
1117 htab_hash hash_fn, htab_eq eq_fn)
1119 encoder->tree_hash_table = htab_create (37, hash_fn, eq_fn, free);
1120 encoder->next_index = 0;
1121 encoder->trees = NULL;
1125 /* Destory an lto_tree_ref_encoder ENCODER by freeing its contents. The
1126 memory used by ENCODER is not freed by this function. */
1127 static inline void
1128 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1130 /* Hash table may be delete already. */
1131 if (encoder->tree_hash_table)
1132 htab_delete (encoder->tree_hash_table);
1133 VEC_free (tree, heap, encoder->trees);
1136 /* Return the number of trees encoded in ENCODER. */
1137 static inline unsigned int
1138 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1140 return VEC_length (tree, encoder->trees);
1143 /* Return the IDX-th tree in ENCODER. */
1144 static inline tree
1145 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1146 unsigned int idx)
1148 return VEC_index (tree, encoder->trees, idx);
1152 /* Return true if LABEL should be emitted in the global context. */
1153 static inline bool
1154 emit_label_in_global_context_p (tree label)
1156 return DECL_NONLOCAL (label) || FORCED_LABEL (label);
1159 /* Return true if tree node EXPR should be streamed as a builtin. For
1160 these nodes, we just emit the class and function code. */
1161 static inline bool
1162 lto_stream_as_builtin_p (tree expr)
1164 return (TREE_CODE (expr) == FUNCTION_DECL
1165 && DECL_IS_BUILTIN (expr)
1166 && (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
1167 || DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD));
1170 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1171 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1172 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1173 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1174 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1175 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1176 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1178 /* Returns a new bit-packing context for bit-packing into S. */
1179 static inline struct bitpack_d
1180 bitpack_create (struct lto_output_stream *s)
1182 struct bitpack_d bp;
1183 bp.pos = 0;
1184 bp.word = 0;
1185 bp.stream = (void *)s;
1186 return bp;
1189 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
1190 static inline void
1191 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
1193 bitpack_word_t word = bp->word;
1194 int pos = bp->pos;
1196 /* We shouldn't try to pack more bits than can fit in a bitpack word. */
1197 gcc_assert (nbits > 0 && nbits <= BITS_PER_BITPACK_WORD);
1199 /* The value to pack should not overflow NBITS. */
1200 gcc_assert (nbits == BITS_PER_BITPACK_WORD
1201 || val <= ((bitpack_word_t) 1 << nbits));
1203 /* If val does not fit into the current bitpack word switch to the
1204 next one. */
1205 if (pos + nbits > BITS_PER_BITPACK_WORD)
1207 lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream, word);
1208 word = val;
1209 pos = nbits;
1211 else
1213 word |= val << pos;
1214 pos += nbits;
1216 bp->word = word;
1217 bp->pos = pos;
1220 /* Finishes bit-packing of BP. */
1221 static inline void
1222 lto_output_bitpack (struct bitpack_d *bp)
1224 lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream,
1225 bp->word);
1226 bp->word = 0;
1227 bp->pos = 0;
1230 /* Returns a new bit-packing context for bit-unpacking from IB. */
1231 static inline struct bitpack_d
1232 lto_input_bitpack (struct lto_input_block *ib)
1234 struct bitpack_d bp;
1235 bp.word = lto_input_uleb128 (ib);
1236 bp.pos = 0;
1237 bp.stream = (void *)ib;
1238 return bp;
1241 /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
1242 static inline bitpack_word_t
1243 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
1245 bitpack_word_t mask, val;
1246 int pos = bp->pos;
1248 mask = (nbits == BITS_PER_BITPACK_WORD
1249 ? (bitpack_word_t) -1
1250 : ((bitpack_word_t) 1 << nbits) - 1);
1252 /* If there are not continuous nbits in the current bitpack word
1253 switch to the next one. */
1254 if (pos + nbits > BITS_PER_BITPACK_WORD)
1256 bp->word = val = lto_input_uleb128 ((struct lto_input_block *)bp->stream);
1257 bp->pos = nbits;
1258 return val & mask;
1260 val = bp->word;
1261 val >>= pos;
1262 bp->pos = pos + nbits;
1264 return val & mask;
1267 #endif /* GCC_LTO_STREAMER_H */