cp/ChangeLog.pph
[official-gcc.git] / gcc / lto-streamer.h
blob9b64619f192f89534fc56ddf1f8d9c58c0339817
1 /* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
4 Copyright (C) 2009, 2010, 2011 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_streamer_cache_create to instantiate a cache of
59 well-known nodes. These are tree nodes that are always
60 instantiated by the compiler on startup. Additionally, these
61 nodes need to be shared. This function produces a vector of
62 these well known trees that are then added to the streamer cache.
63 This way, the writer will only write out a reference to the tree
64 and the reader will instantiate the tree out of this
65 pre-populated cache. */
66 VEC(tree,heap) *(*get_common_nodes) (void);
68 /* Called by lto_reader_init after it does basic initialization. */
69 void (*reader_init) (void);
71 /* Called by lto_writer_init after it does basic initalization. */
72 void (*writer_init) (void);
74 /* Return true if the given tree is supported by this streamer. */
75 bool (*is_streamable) (tree);
77 /* Called by lto_write_tree after writing all the common parts of
78 a tree. If defined, the callback is in charge of writing all
79 the fields that lto_write_tree did not write out. Arguments
80 are as in lto_write_tree.
82 The following tree fields are not handled by common code:
84 DECL_ABSTRACT_ORIGIN
85 DECL_INITIAL
86 DECL_SAVED_TREE
88 Callbacks may choose to ignore or handle them. If handled,
89 the reader should read them in the exact same sequence written
90 by the writer. */
91 void (*write_tree) (struct output_block *, tree, bool);
93 /* Called by lto_read_tree after reading all the common parts of
94 a tree. If defined, the callback is in charge of reading all
95 the fields that lto_read_tree did not read in. Arguments
96 are as in lto_read_tree. */
97 void (*read_tree) (struct lto_input_block *, struct data_in *, tree);
99 /* Called by lto_output_tree_ref to determine if the given tree node
100 should be emitted as a reference to the table of declarations
101 (the same table that holds VAR_DECLs). */
102 bool (*indexable_with_decls_p) (tree);
104 /* Called by pack_value_fields to store any non-pointer fields
105 in the tree structure. The arguments are as in pack_value_fields. */
106 void (*pack_value_fields) (struct bitpack_d *, tree);
108 /* Called by unpack_value_fields to retrieve any non-pointer fields
109 in the tree structure. The arguments are as in unpack_value_fields. */
110 void (*unpack_value_fields) (struct bitpack_d *, tree);
112 /* Non-zero if the streamer should register decls in the LTO
113 global symbol tables. */
114 unsigned register_decls_in_symtab_p : 1;
116 /* Non-zero if the streamer has special constants that cannot be
117 shared and are used in pointer-equality tests (e.g., void_zero_node,
118 truthvalue_false_node, etc). These constants will be present in
119 the streamer cache and should be streamed as references. */
120 unsigned has_unique_integer_csts_p : 1;
122 /* Called by lto_materialize_tree for tree nodes that it does not
123 know how to allocate memory for. If defined, this hook should
124 return a new tree node of the given code. The data_in and
125 input_block arguments are passed in case the hook needs to
126 read more data from the stream to allocate the node.
127 If this hook returns NULL, then lto_materialize_tree will attempt
128 to allocate the tree by calling make_node directly. */
129 tree (*alloc_tree) (enum tree_code, struct lto_input_block *,
130 struct data_in *);
132 /* Called by lto_output_tree_header to write any streamer-specific
133 information needed to allocate the tree. This hook may assume
134 that the basic header data (tree code, etc) has already been
135 written. It should only write any extra data needed to allocate
136 the node (e.g., in the case of CALL_EXPR, this hook would write
137 the number of arguments to the CALL_EXPR). */
138 void (*output_tree_header) (struct output_block *, tree);
139 } lto_streamer_hooks;
142 /* The encoding for a function consists of the following sections:
144 1) The header.
145 2) FIELD_DECLS.
146 3) FUNCTION_DECLS.
147 4) global VAR_DECLS.
148 5) type_decls
149 6) types.
150 7) Names for the labels that have names
151 8) The SSA names.
152 9) The control flow graph.
153 10-11)Gimple for local decls.
154 12) Gimple for the function.
155 13) Strings.
157 1) THE HEADER.
158 2-6) THE GLOBAL DECLS AND TYPES.
160 The global decls and types are encoded in the same way. For each
161 entry, there is word with the offset within the section to the
162 entry.
164 7) THE LABEL NAMES.
166 Since most labels do not have names, this section my be of zero
167 length. It consists of an array of string table references, one
168 per label. In the lto code, the labels are given either
169 positive or negative indexes. the positive ones have names and
170 the negative ones do not. The positive index can be used to
171 find the name in this array.
173 9) THE CFG.
175 10) Index into the local decls. Since local decls can have local
176 decls inside them, they must be read in randomly in order to
177 properly restore them.
179 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
181 The gimple consists of a set of records.
183 THE FUNCTION
185 At the top level of (8) is the function. It consists of five
186 pieces:
188 LTO_function - The tag.
189 eh tree - This is all of the exception handling regions
190 put out in a post order traversial of the
191 tree. Siblings are output as lists terminated
192 by a 0. The set of fields matches the fields
193 defined in except.c.
195 last_basic_block - in uleb128 form.
197 basic blocks - This is the set of basic blocks.
199 zero - The termination of the basic blocks.
201 BASIC BLOCKS
203 There are two forms of basic blocks depending on if they are
204 empty or not.
206 The basic block consists of:
208 LTO_bb1 or LTO_bb0 - The tag.
210 bb->index - the index in uleb128 form.
212 #succs - The number of successors un uleb128 form.
214 the successors - For each edge, a pair. The first of the
215 pair is the index of the successor in
216 uleb128 form and the second are the flags in
217 uleb128 form.
219 the statements - A gimple tree, as described above.
220 These are only present for LTO_BB1.
221 Following each statement is an optional
222 exception handling record LTO_eh_region
223 which contains the region number (for
224 regions >= 0).
226 zero - This is only present for LTO_BB1 and is used
227 to terminate the statements and exception
228 regions within this block.
230 12) STRINGS
232 String are represented in the table as pairs, a length in ULEB128
233 form followed by the data for the string. */
235 /* The string that is the prefix on the section names we make for lto.
236 For decls the DECL_ASSEMBLER_NAME is appended to make the section
237 name for the functions and static_initializers. For other types of
238 sections a '.' and the section type are appended. */
239 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
241 #define LTO_major_version 2
242 #define LTO_minor_version 0
244 typedef unsigned char lto_decl_flags_t;
247 /* Data structures used to pack values and bitflags into a vector of
248 words. Used to stream values of a fixed number of bits in a space
249 efficient way. */
250 static unsigned const BITS_PER_BITPACK_WORD = HOST_BITS_PER_WIDE_INT;
252 typedef unsigned HOST_WIDE_INT bitpack_word_t;
253 DEF_VEC_I(bitpack_word_t);
254 DEF_VEC_ALLOC_I(bitpack_word_t, heap);
256 struct bitpack_d
258 /* The position of the first unused or unconsumed bit in the word. */
259 unsigned pos;
261 /* The current word we are (un)packing. */
262 bitpack_word_t word;
264 /* The lto_output_stream or the lto_input_block we are streaming to/from. */
265 void *stream;
268 /* Tags representing the various IL objects written to the bytecode file
269 (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
271 NOTE, when adding new LTO tags, also update lto_tag_name. */
272 enum LTO_tags
274 LTO_null = 0,
276 /* Reserve enough entries to fit all the tree and gimple codes handled
277 by the streamer. This guarantees that:
279 1- Given a tree code C:
280 enum LTO_tags tag == C + 1
282 2- Given a gimple code C:
283 enum LTO_tags tag == C + NUM_TREE_CODES + 1
285 Conversely, to map between LTO tags and tree/gimple codes, the
286 reverse operation must be applied. */
287 LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
288 LTO_bb1,
290 /* EH region holding the previous statement. */
291 LTO_eh_region,
293 /* An MD or NORMAL builtin. Only the code and class are streamed out. */
294 LTO_builtin_decl,
296 /* Function body. */
297 LTO_function,
299 /* EH table. */
300 LTO_eh_table,
302 /* EH region types. These mirror enum eh_region_type. */
303 LTO_ert_cleanup,
304 LTO_ert_try,
305 LTO_ert_allowed_exceptions,
306 LTO_ert_must_not_throw,
308 /* EH landing pad. */
309 LTO_eh_landing_pad,
311 /* EH try/catch node. */
312 LTO_eh_catch,
314 /* Special for global streamer. Reference to previously-streamed node. */
315 LTO_tree_pickle_reference,
317 /* References to indexable tree nodes. These objects are stored in
318 tables that are written separately from the function bodies that
319 reference them. This way they can be instantiated even when the
320 referencing functions aren't (e.g., during WPA) and it also allows
321 functions to be copied from one file to another without having
322 to unpickle the body first (the references are location
323 independent).
325 NOTE, do not regroup these values as the grouping is exposed
326 in the range checks done in lto_input_tree. */
327 LTO_field_decl_ref, /* Do not change. */
328 LTO_function_decl_ref,
329 LTO_label_decl_ref,
330 LTO_namespace_decl_ref,
331 LTO_result_decl_ref,
332 LTO_ssa_name_ref,
333 LTO_type_decl_ref,
334 LTO_type_ref,
335 LTO_const_decl_ref,
336 LTO_imported_decl_ref,
337 LTO_translation_unit_decl_ref,
338 LTO_global_decl_ref, /* Do not change. */
339 LTO_LAST_TAG,
341 /* This tag must always be last. */
342 LTO_NUM_TAGS = LTO_LAST_TAG + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE
346 /* Set of section types that are in an LTO file. This list will grow
347 as the number of IPA passes grows since each IPA pass will need its
348 own section type to store its summary information.
350 When adding a new section type, you must also extend the
351 LTO_SECTION_NAME array in lto-section-in.c. */
352 enum lto_section_type
354 LTO_section_decls = 0,
355 LTO_section_function_body,
356 LTO_section_static_initializer,
357 LTO_section_cgraph,
358 LTO_section_varpool,
359 LTO_section_refs,
360 LTO_section_jump_functions,
361 LTO_section_ipa_pure_const,
362 LTO_section_ipa_reference,
363 LTO_section_symtab,
364 LTO_section_opts,
365 LTO_section_cgraph_opt_sum,
366 LTO_section_inline_summary,
367 LTO_N_SECTION_TYPES /* Must be last. */
370 /* Indices to the various function, type and symbol streams. */
371 typedef enum
373 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
374 LTO_DECL_STREAM_FIELD_DECL,
375 LTO_DECL_STREAM_FN_DECL,
376 LTO_DECL_STREAM_VAR_DECL,
377 LTO_DECL_STREAM_TYPE_DECL,
378 LTO_DECL_STREAM_NAMESPACE_DECL,
379 LTO_DECL_STREAM_LABEL_DECL,
380 LTO_N_DECL_STREAMS
381 } lto_decl_stream_e_t;
383 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
384 DEF_VEC_I(ld_plugin_symbol_resolution_t);
385 DEF_VEC_ALLOC_I(ld_plugin_symbol_resolution_t, heap);
388 /* Macro to define convenience functions for type and decl streams
389 in lto_file_decl_data. */
390 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
391 static inline tree \
392 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
393 unsigned int idx) \
395 struct lto_in_decl_state *state = data->current_decl_state; \
396 gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
397 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
400 static inline unsigned int \
401 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
403 struct lto_in_decl_state *state = data->current_decl_state; \
404 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
408 /* Return a char pointer to the start of a data stream for an lto pass
409 or function. The first parameter is the file data that contains
410 the information. The second parameter is the type of information
411 to be obtained. The third parameter is the name of the function
412 and is only used when finding a function body; otherwise it is
413 NULL. The fourth parameter is the length of the data returned. */
414 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
415 enum lto_section_type,
416 const char *,
417 size_t *);
419 /* Return the data found from the above call. The first three
420 parameters are the same as above. The fourth parameter is the data
421 itself and the fifth is the lenght of the data. */
422 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
423 enum lto_section_type,
424 const char *,
425 const char *,
426 size_t);
428 /* Cache of pickled nodes. Used to avoid writing the same node more
429 than once. The first time a tree node is streamed out, it is
430 entered in this cache. Subsequent references to the same node are
431 resolved by looking it up in this cache.
433 This is used in two ways:
435 - On the writing side, the first time T is added to STREAMER_CACHE,
436 a new reference index is created for T and T is emitted on the
437 stream. If T needs to be emitted again to the stream, instead of
438 pickling it again, the reference index is emitted.
440 - On the reading side, the first time T is read from the stream, it
441 is reconstructed in memory and a new reference index created for
442 T. The reconstructed T is inserted in some array so that when
443 the reference index for T is found in the input stream, it can be
444 used to look up into the array to get the reconstructed T. */
445 struct lto_streamer_cache_d
447 /* The mapping between tree nodes and slots into the nodes array. */
448 htab_t node_map;
450 /* Node map to store entries into. */
451 alloc_pool node_map_entries;
453 /* The nodes pickled so far. */
454 VEC(tree,heap) *nodes;
458 /* Structure used as buffer for reading an LTO file. */
459 struct lto_input_block
461 const char *data;
462 unsigned int p;
463 unsigned int len;
466 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
467 do { \
468 BASE.data = D; \
469 BASE.p = P; \
470 BASE.len = L; \
471 } while (0)
473 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
474 do { \
475 BASE->data = D; \
476 BASE->p = P; \
477 BASE->len = L; \
478 } while (0)
481 /* The is the first part of the record for a function or constructor
482 in the .o file. */
483 struct lto_header
485 int16_t major_version;
486 int16_t minor_version;
487 enum lto_section_type section_type;
490 /* The header for a function body. */
491 struct lto_function_header
493 /* The header for all types of sections. */
494 struct lto_header lto_header;
496 /* Number of labels with names. */
497 int32_t num_named_labels;
499 /* Number of labels without names. */
500 int32_t num_unnamed_labels;
502 /* Size compressed or 0 if not compressed. */
503 int32_t compressed_size;
505 /* Size of names for named labels. */
506 int32_t named_label_size;
508 /* Size of the cfg. */
509 int32_t cfg_size;
511 /* Size of main gimple body of function. */
512 int32_t main_size;
514 /* Size of the string table. */
515 int32_t string_size;
519 /* Structure describing a symbol section. */
520 struct lto_decl_header
522 /* The header for all types of sections. */
523 struct lto_header lto_header;
525 /* Size of region for decl state. */
526 int32_t decl_state_size;
528 /* Number of nodes in globals stream. */
529 int32_t num_nodes;
531 /* Size of region for expressions, decls, types, etc. */
532 int32_t main_size;
534 /* Size of the string table. */
535 int32_t string_size;
539 /* Statistics gathered during LTO, WPA and LTRANS. */
540 struct lto_stats_d
542 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
543 unsigned HOST_WIDE_INT num_output_cgraph_nodes;
544 unsigned HOST_WIDE_INT num_input_files;
545 unsigned HOST_WIDE_INT num_output_files;
546 unsigned HOST_WIDE_INT num_cgraph_partitions;
547 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
548 unsigned HOST_WIDE_INT num_function_bodies;
549 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
550 unsigned HOST_WIDE_INT num_output_il_bytes;
551 unsigned HOST_WIDE_INT num_compressed_il_bytes;
552 unsigned HOST_WIDE_INT num_input_il_bytes;
553 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
556 /* Encoder data structure used to stream callgraph nodes. */
557 struct lto_cgraph_encoder_d
559 /* Map nodes to reference number. */
560 struct pointer_map_t *map;
562 /* Map reference number to node. */
563 VEC(cgraph_node_ptr,heap) *nodes;
565 /* Map of nodes where we want to output body. */
566 struct pointer_set_t *body;
569 typedef struct lto_cgraph_encoder_d *lto_cgraph_encoder_t;
571 /* Return number of encoded nodes in ENCODER. */
573 static inline int
574 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
576 return VEC_length (cgraph_node_ptr, encoder->nodes);
580 /* Encoder data structure used to stream callgraph nodes. */
581 struct lto_varpool_encoder_d
583 /* Map nodes to reference number. */
584 struct pointer_map_t *map;
586 /* Map reference number to node. */
587 VEC(varpool_node_ptr,heap) *nodes;
589 /* Map of nodes where we want to output initializer. */
590 struct pointer_set_t *initializer;
592 typedef struct lto_varpool_encoder_d *lto_varpool_encoder_t;
594 /* Return number of encoded nodes in ENCODER. */
596 static inline int
597 lto_varpool_encoder_size (lto_varpool_encoder_t encoder)
599 return VEC_length (varpool_node_ptr, encoder->nodes);
602 /* Mapping from indices to trees. */
603 struct GTY(()) lto_tree_ref_table
605 /* Array of referenced trees . */
606 tree * GTY((length ("%h.size"))) trees;
608 /* Size of array. */
609 unsigned int size;
613 /* Mapping between trees and slots in an array. */
614 struct lto_decl_slot
616 tree t;
617 int slot_num;
621 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
623 struct lto_tree_ref_encoder
625 htab_t tree_hash_table; /* Maps pointers to indices. */
626 unsigned int next_index; /* Next available index. */
627 VEC(tree,heap) *trees; /* Maps indices to pointers. */
631 /* Structure to hold states of input scope. */
632 struct GTY(()) lto_in_decl_state
634 /* Array of lto_in_decl_buffers to store type and decls streams. */
635 struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
637 /* If this in-decl state is associated with a function. FN_DECL
638 point to the FUNCTION_DECL. */
639 tree fn_decl;
642 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
645 /* The structure that holds all of the vectors of global types,
646 decls and cgraph nodes used in the serialization of this file. */
647 struct lto_out_decl_state
649 /* The buffers contain the sets of decls of various kinds and types we have
650 seen so far and the indexes assigned to them. */
651 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
653 /* Encoder for cgraph nodes. */
654 lto_cgraph_encoder_t cgraph_node_encoder;
656 /* Encoder for varpool nodes. */
657 lto_varpool_encoder_t varpool_node_encoder;
659 /* If this out-decl state belongs to a function, fn_decl points to that
660 function. Otherwise, it is NULL. */
661 tree fn_decl;
664 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
666 DEF_VEC_P(lto_out_decl_state_ptr);
667 DEF_VEC_ALLOC_P(lto_out_decl_state_ptr, heap);
669 /* One of these is allocated for each object file that being compiled
670 by lto. This structure contains the tables that are needed by the
671 serialized functions and ipa passes to connect themselves to the
672 global types and decls as they are reconstituted. */
673 struct GTY(()) lto_file_decl_data
675 /* Decl state currently used. */
676 struct lto_in_decl_state *current_decl_state;
678 /* Decl state corresponding to regions outside of any functions
679 in the compilation unit. */
680 struct lto_in_decl_state *global_decl_state;
682 /* Table of cgraph nodes present in this file. */
683 lto_cgraph_encoder_t GTY((skip)) cgraph_node_encoder;
685 /* Table of varpool nodes present in this file. */
686 lto_varpool_encoder_t GTY((skip)) varpool_node_encoder;
688 /* Hash table maps lto-related section names to location in file. */
689 htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
691 /* The .o file that these offsets relate to. */
692 const char *GTY((skip)) file_name;
694 /* Hash table maps lto-related section names to location in file. */
695 htab_t GTY((skip)) section_hash_table;
697 /* Hash new name of renamed global declaration to its original name. */
698 htab_t GTY((skip)) renaming_hash_table;
700 /* Linked list used temporarily in reader */
701 struct lto_file_decl_data *next;
703 /* Sub ID for merged objects. */
704 unsigned id;
706 /* Symbol resolutions for this file */
707 VEC(ld_plugin_symbol_resolution_t,heap) * GTY((skip)) resolutions;
709 struct gcov_ctr_summary GTY((skip)) profile_info;
711 /* Any other streamer-specific data needed by the streamer. */
712 void * GTY((skip)) sdata;
715 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
717 struct lto_char_ptr_base
719 char *ptr;
722 /* An incore byte stream to buffer the various parts of the function.
723 The entire structure should be zeroed when created. The record
724 consists of a set of blocks. The first sizeof (ptr) bytes are used
725 as a chain, and the rest store the bytes to be written. */
726 struct lto_output_stream
728 /* The pointer to the first block in the stream. */
729 struct lto_char_ptr_base * first_block;
731 /* The pointer to the last and current block in the stream. */
732 struct lto_char_ptr_base * current_block;
734 /* The pointer to where the next char should be written. */
735 char * current_pointer;
737 /* The number of characters left in the current block. */
738 unsigned int left_in_block;
740 /* The block size of the last block allocated. */
741 unsigned int block_size;
743 /* The total number of characters written. */
744 unsigned int total_size;
747 /* The is the first part of the record in an LTO file for many of the
748 IPA passes. */
749 struct lto_simple_header
751 /* The header for all types of sections. */
752 struct lto_header lto_header;
754 /* Size of main gimple body of function. */
755 int32_t main_size;
757 /* Size of main stream when compressed. */
758 int32_t compressed_size;
761 /* A simple output block. This can be used for simple IPA passes that
762 do not need more than one stream. */
763 struct lto_simple_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;
772 /* Data structure holding all the data and descriptors used when writing
773 an LTO file. */
774 struct output_block
776 enum lto_section_type section_type;
777 struct lto_out_decl_state *decl_state;
779 /* The stream that the main tree codes are written to. */
780 struct lto_output_stream *main_stream;
782 /* The stream that contains the string table. */
783 struct lto_output_stream *string_stream;
785 /* The stream that contains the cfg. */
786 struct lto_output_stream *cfg_stream;
788 /* The hash table that contains the set of strings we have seen so
789 far and the indexes assigned to them. */
790 htab_t string_hash_table;
792 /* The current cgraph_node that we are currently serializing. Null
793 if we are serializing something else. */
794 struct cgraph_node *cgraph_node;
796 /* These are the last file and line that were seen in the stream.
797 If the current node differs from these, it needs to insert
798 something into the stream and fix these up. */
799 const char *current_file;
800 int current_line;
801 int current_col;
803 /* True if writing globals and types. */
804 bool global;
806 /* Cache of nodes written in this section. */
807 struct lto_streamer_cache_d *writer_cache;
809 /* Any other streamer-specific data needed by the streamer. */
810 void *sdata;
814 /* Data and descriptors used when reading from an LTO file. */
815 struct data_in
817 /* The global decls and types. */
818 struct lto_file_decl_data *file_data;
820 /* All of the labels. */
821 tree *labels;
823 /* The string table. */
824 const char *strings;
826 /* The length of the string table. */
827 unsigned int strings_len;
829 /* Number of named labels. Used to find the index of unnamed labels
830 since they share space with the named labels. */
831 unsigned int num_named_labels;
833 /* Number of unnamed labels. */
834 unsigned int num_unnamed_labels;
836 const char *current_file;
837 int current_line;
838 int current_col;
840 /* Maps each reference number to the resolution done by the linker. */
841 VEC(ld_plugin_symbol_resolution_t,heap) *globals_resolution;
843 /* Cache of pickled nodes. */
844 struct lto_streamer_cache_d *reader_cache;
846 /* Any other streamer-specific data needed by the streamer. */
847 void *sdata;
851 /* In lto-section-in.c */
852 extern struct lto_input_block * lto_create_simple_input_block (
853 struct lto_file_decl_data *,
854 enum lto_section_type, const char **, size_t *);
855 extern void
856 lto_destroy_simple_input_block (struct lto_file_decl_data *,
857 enum lto_section_type,
858 struct lto_input_block *, const char *, size_t);
859 extern void lto_set_in_hooks (struct lto_file_decl_data **,
860 lto_get_section_data_f *,
861 lto_free_section_data_f *);
862 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
863 extern const char *lto_get_section_data (struct lto_file_decl_data *,
864 enum lto_section_type,
865 const char *, size_t *);
866 extern void lto_free_section_data (struct lto_file_decl_data *,
867 enum lto_section_type,
868 const char *, const char *, size_t);
869 extern unsigned char lto_input_1_unsigned (struct lto_input_block *);
870 extern unsigned HOST_WIDE_INT lto_input_uleb128 (struct lto_input_block *);
871 extern unsigned HOST_WIDEST_INT lto_input_widest_uint_uleb128 (
872 struct lto_input_block *);
873 extern HOST_WIDE_INT lto_input_sleb128 (struct lto_input_block *);
874 extern htab_t lto_create_renaming_table (void);
875 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
876 const char *, const char *);
877 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
878 const char *);
879 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
880 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
881 extern hashval_t lto_hash_in_decl_state (const void *);
882 extern int lto_eq_in_decl_state (const void *, const void *);
883 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
884 struct lto_file_decl_data *, tree);
886 /* In lto-section-out.c */
887 extern hashval_t lto_hash_decl_slot_node (const void *);
888 extern int lto_eq_decl_slot_node (const void *, const void *);
889 extern hashval_t lto_hash_type_slot_node (const void *);
890 extern int lto_eq_type_slot_node (const void *, const void *);
891 extern void lto_begin_section (const char *, bool);
892 extern void lto_end_section (void);
893 extern void lto_write_stream (struct lto_output_stream *);
894 extern void lto_output_1_stream (struct lto_output_stream *, char);
895 extern void lto_output_data_stream (struct lto_output_stream *, const void *,
896 size_t);
897 extern void lto_output_uleb128_stream (struct lto_output_stream *,
898 unsigned HOST_WIDE_INT);
899 extern void lto_output_widest_uint_uleb128_stream (struct lto_output_stream *,
900 unsigned HOST_WIDEST_INT);
901 extern void lto_output_sleb128_stream (struct lto_output_stream *,
902 HOST_WIDE_INT);
903 extern bool lto_output_decl_index (struct lto_output_stream *,
904 struct lto_tree_ref_encoder *,
905 tree, unsigned int *);
906 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
907 struct lto_output_stream *, tree);
908 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
909 struct lto_output_stream *, tree);
910 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
911 struct lto_output_stream *, tree);
912 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
913 struct lto_output_stream *, tree);
914 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
915 struct lto_output_stream *, tree);
916 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
917 struct lto_output_stream *, tree);
918 extern struct lto_simple_output_block *lto_create_simple_output_block (
919 enum lto_section_type);
920 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
921 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
922 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
923 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
924 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
925 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
926 extern void lto_record_function_out_decl_state (tree,
927 struct lto_out_decl_state *);
930 /* In lto-streamer.c. */
931 extern const char *lto_tag_name (enum LTO_tags);
932 extern bitmap lto_bitmap_alloc (void);
933 extern void lto_bitmap_free (bitmap);
934 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
935 extern void print_lto_report (void);
936 extern bool lto_streamer_cache_insert (struct lto_streamer_cache_d *, tree,
937 unsigned *);
938 extern bool lto_streamer_cache_insert_at (struct lto_streamer_cache_d *, tree,
939 unsigned);
940 extern void lto_streamer_cache_append (struct lto_streamer_cache_d *, tree);
941 extern bool lto_streamer_cache_lookup (struct lto_streamer_cache_d *, tree,
942 unsigned *);
943 extern tree lto_streamer_cache_get (struct lto_streamer_cache_d *, unsigned);
944 extern struct lto_streamer_cache_d *lto_streamer_cache_create (void);
945 extern void lto_streamer_cache_delete (struct lto_streamer_cache_d *);
946 extern void lto_streamer_init (void);
947 extern bool gate_lto_out (void);
948 #ifdef LTO_STREAMER_DEBUG
949 extern void lto_orig_address_map (tree, intptr_t);
950 extern intptr_t lto_orig_address_get (tree);
951 extern void lto_orig_address_remove (tree);
952 #endif
953 extern void lto_check_version (int, int);
954 extern void gimple_streamer_hooks_init (void);
955 extern void gimple_streamer_write_tree (struct output_block *, tree, bool);
956 extern void gimple_streamer_read_tree (struct lto_input_block *,
957 struct data_in *, tree);
958 extern lto_streamer_hooks *streamer_hooks (void);
959 extern lto_streamer_hooks *streamer_hooks_init (void);
961 /* In lto-streamer-in.c */
962 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
963 extern void lto_reader_init (void);
964 extern tree lto_input_tree (struct lto_input_block *, struct data_in *);
965 extern tree lto_input_chain (struct lto_input_block *, struct data_in *);
966 extern void lto_input_function_body (struct lto_file_decl_data *, tree,
967 const char *);
968 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
969 const char *);
970 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
971 const char *, unsigned,
972 VEC(ld_plugin_symbol_resolution_t,heap) *);
973 extern void lto_data_in_delete (struct data_in *);
974 extern const char *lto_input_string (struct data_in *,
975 struct lto_input_block *);
976 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
977 extern void gimple_streamer_reader_init (void);
980 /* In lto-streamer-out.c */
981 extern void lto_writer_init (void);
982 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
983 extern struct output_block *create_output_block (enum lto_section_type);
984 extern void destroy_output_block (struct output_block *);
985 extern void lto_output_tree (struct output_block *, tree, bool);
986 extern void lto_output_tree_or_ref (struct output_block *, tree, bool);
987 extern void lto_output_chain (struct output_block *, tree, bool);
988 extern void produce_asm (struct output_block *ob, tree fn);
989 extern void lto_output_string (struct output_block *,
990 struct lto_output_stream *,
991 const char *);
992 extern void lto_output_string_with_length (struct output_block *,
993 struct lto_output_stream *,
994 const char *,
995 unsigned int);
996 void lto_output_decl_state_streams (struct output_block *,
997 struct lto_out_decl_state *);
998 void lto_output_decl_state_refs (struct output_block *,
999 struct lto_output_stream *,
1000 struct lto_out_decl_state *);
1003 /* In lto-cgraph.c */
1004 struct cgraph_node *lto_cgraph_encoder_deref (lto_cgraph_encoder_t, int);
1005 int lto_cgraph_encoder_lookup (lto_cgraph_encoder_t, struct cgraph_node *);
1006 lto_cgraph_encoder_t lto_cgraph_encoder_new (void);
1007 int lto_cgraph_encoder_encode (lto_cgraph_encoder_t, struct cgraph_node *);
1008 void lto_cgraph_encoder_delete (lto_cgraph_encoder_t);
1009 bool lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t,
1010 struct cgraph_node *);
1012 bool lto_varpool_encoder_encode_body_p (lto_varpool_encoder_t,
1013 struct varpool_node *);
1014 struct varpool_node *lto_varpool_encoder_deref (lto_varpool_encoder_t, int);
1015 int lto_varpool_encoder_lookup (lto_varpool_encoder_t, struct varpool_node *);
1016 lto_varpool_encoder_t lto_varpool_encoder_new (void);
1017 int lto_varpool_encoder_encode (lto_varpool_encoder_t, struct varpool_node *);
1018 void lto_varpool_encoder_delete (lto_varpool_encoder_t);
1019 bool lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t,
1020 struct varpool_node *);
1021 void output_cgraph (cgraph_node_set, varpool_node_set);
1022 void input_cgraph (void);
1023 bool referenced_from_other_partition_p (struct ipa_ref_list *,
1024 cgraph_node_set,
1025 varpool_node_set vset);
1026 bool reachable_from_other_partition_p (struct cgraph_node *,
1027 cgraph_node_set);
1028 bool referenced_from_this_partition_p (struct ipa_ref_list *,
1029 cgraph_node_set,
1030 varpool_node_set vset);
1031 bool reachable_from_this_partition_p (struct cgraph_node *,
1032 cgraph_node_set);
1033 void compute_ltrans_boundary (struct lto_out_decl_state *state,
1034 cgraph_node_set, varpool_node_set);
1037 /* In lto-symtab.c. */
1038 extern void lto_symtab_register_decl (tree, ld_plugin_symbol_resolution_t,
1039 struct lto_file_decl_data *);
1040 extern void lto_symtab_merge_decls (void);
1041 extern void lto_symtab_merge_cgraph_nodes (void);
1042 extern tree lto_symtab_prevailing_decl (tree decl);
1043 extern enum ld_plugin_symbol_resolution lto_symtab_get_resolution (tree decl);
1044 extern void lto_symtab_free (void);
1045 extern GTY(()) VEC(tree,gc) *lto_global_var_decls;
1048 /* In lto-opts.c. */
1049 extern void lto_register_user_option (size_t, const char *, int, unsigned int);
1050 extern void lto_read_file_options (struct lto_file_decl_data *);
1051 extern void lto_write_options (void);
1052 extern void lto_reissue_options (void);
1053 void lto_clear_user_options (void);
1054 void lto_clear_file_options (void);
1057 /* In lto-wpa-fixup.c */
1058 void lto_mark_nothrow_fndecl (tree);
1059 void lto_fixup_nothrow_decls (void);
1062 /* Statistics gathered during LTO, WPA and LTRANS. */
1063 extern struct lto_stats_d lto_stats;
1065 /* Section names corresponding to the values of enum lto_section_type. */
1066 extern const char *lto_section_name[];
1068 /* Holds all the out decl states of functions output so far in the
1069 current output file. */
1070 extern VEC(lto_out_decl_state_ptr, heap) *lto_function_decl_states;
1072 /* Return true if LTO tag TAG corresponds to a tree code. */
1073 static inline bool
1074 lto_tag_is_tree_code_p (enum LTO_tags tag)
1076 return tag > LTO_null && (unsigned) tag <= MAX_TREE_CODES;
1080 /* Return true if LTO tag TAG corresponds to a gimple code. */
1081 static inline bool
1082 lto_tag_is_gimple_code_p (enum LTO_tags tag)
1084 return (unsigned) tag >= NUM_TREE_CODES + 1
1085 && (unsigned) tag < 1 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
1089 /* Return the LTO tag corresponding to gimple code CODE. See enum
1090 LTO_tags for details on the conversion. */
1091 static inline enum LTO_tags
1092 lto_gimple_code_to_tag (enum gimple_code code)
1094 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 1);
1098 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
1099 details on the conversion. */
1100 static inline enum gimple_code
1101 lto_tag_to_gimple_code (enum LTO_tags tag)
1103 gcc_assert (lto_tag_is_gimple_code_p (tag));
1104 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 1);
1108 /* Return the LTO tag corresponding to tree code CODE. See enum
1109 LTO_tags for details on the conversion. */
1110 static inline enum LTO_tags
1111 lto_tree_code_to_tag (enum tree_code code)
1113 return (enum LTO_tags) ((unsigned) code + 1);
1117 /* Return the tree code corresponding to TAG. See enum LTO_tags for
1118 details on the conversion. */
1119 static inline enum tree_code
1120 lto_tag_to_tree_code (enum LTO_tags tag)
1122 gcc_assert (lto_tag_is_tree_code_p (tag));
1123 return (enum tree_code) ((unsigned) tag - 1);
1126 /* Initialize an lto_out_decl_buffer ENCODER. */
1127 static inline void
1128 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder,
1129 htab_hash hash_fn, htab_eq eq_fn)
1131 encoder->tree_hash_table = htab_create (37, hash_fn, eq_fn, free);
1132 encoder->next_index = 0;
1133 encoder->trees = NULL;
1137 /* Destory an lto_tree_ref_encoder ENCODER by freeing its contents. The
1138 memory used by ENCODER is not freed by this function. */
1139 static inline void
1140 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1142 /* Hash table may be delete already. */
1143 if (encoder->tree_hash_table)
1144 htab_delete (encoder->tree_hash_table);
1145 VEC_free (tree, heap, encoder->trees);
1148 /* Return the number of trees encoded in ENCODER. */
1149 static inline unsigned int
1150 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1152 return VEC_length (tree, encoder->trees);
1155 /* Return the IDX-th tree in ENCODER. */
1156 static inline tree
1157 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1158 unsigned int idx)
1160 return VEC_index (tree, encoder->trees, idx);
1164 /* Return true if LABEL should be emitted in the global context. */
1165 static inline bool
1166 emit_label_in_global_context_p (tree label)
1168 return DECL_NONLOCAL (label) || FORCED_LABEL (label);
1171 /* Return true if tree node EXPR should be streamed as a builtin. For
1172 these nodes, we just emit the class and function code. */
1173 static inline bool
1174 lto_stream_as_builtin_p (tree expr)
1176 return (TREE_CODE (expr) == FUNCTION_DECL
1177 && DECL_IS_BUILTIN (expr)
1178 && (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
1179 || DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD));
1182 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1183 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1184 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1185 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1186 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1187 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1188 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1190 /* Returns a new bit-packing context for bit-packing into S. */
1191 static inline struct bitpack_d
1192 bitpack_create (struct lto_output_stream *s)
1194 struct bitpack_d bp;
1195 bp.pos = 0;
1196 bp.word = 0;
1197 bp.stream = (void *)s;
1198 return bp;
1201 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
1202 static inline void
1203 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
1205 bitpack_word_t word = bp->word;
1206 int pos = bp->pos;
1208 /* We shouldn't try to pack more bits than can fit in a bitpack word. */
1209 gcc_assert (nbits > 0 && nbits <= BITS_PER_BITPACK_WORD);
1211 /* The value to pack should not overflow NBITS. */
1212 gcc_assert (nbits == BITS_PER_BITPACK_WORD
1213 || val <= ((bitpack_word_t) 1 << nbits));
1215 /* If val does not fit into the current bitpack word switch to the
1216 next one. */
1217 if (pos + nbits > BITS_PER_BITPACK_WORD)
1219 lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream, word);
1220 word = val;
1221 pos = nbits;
1223 else
1225 word |= val << pos;
1226 pos += nbits;
1228 bp->word = word;
1229 bp->pos = pos;
1232 /* Finishes bit-packing of BP. */
1233 static inline void
1234 lto_output_bitpack (struct bitpack_d *bp)
1236 lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream,
1237 bp->word);
1238 bp->word = 0;
1239 bp->pos = 0;
1242 /* Returns a new bit-packing context for bit-unpacking from IB. */
1243 static inline struct bitpack_d
1244 lto_input_bitpack (struct lto_input_block *ib)
1246 struct bitpack_d bp;
1247 bp.word = lto_input_uleb128 (ib);
1248 bp.pos = 0;
1249 bp.stream = (void *)ib;
1250 return bp;
1253 /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
1254 static inline bitpack_word_t
1255 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
1257 bitpack_word_t mask, val;
1258 int pos = bp->pos;
1260 mask = (nbits == BITS_PER_BITPACK_WORD
1261 ? (bitpack_word_t) -1
1262 : ((bitpack_word_t) 1 << nbits) - 1);
1264 /* If there are not continuous nbits in the current bitpack word
1265 switch to the next one. */
1266 if (pos + nbits > BITS_PER_BITPACK_WORD)
1268 bp->word = val = lto_input_uleb128 ((struct lto_input_block *)bp->stream);
1269 bp->pos = nbits;
1270 return val & mask;
1272 val = bp->word;
1273 val >>= pos;
1274 bp->pos = pos + nbits;
1276 return val & mask;
1279 #endif /* GCC_LTO_STREAMER_H */