* pph-streamer.h (pph_output_tree_or_ref_1): New.
[official-gcc.git] / gcc / lto-streamer.h
blobb6f4b79cac5bb5ffd7c9ed3615ba5b0a74d03a8f
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_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 + MAX_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_section_inline_summary,
351 LTO_N_SECTION_TYPES /* Must be last. */
354 /* Indices to the various function, type and symbol streams. */
355 typedef enum
357 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
358 LTO_DECL_STREAM_FIELD_DECL,
359 LTO_DECL_STREAM_FN_DECL,
360 LTO_DECL_STREAM_VAR_DECL,
361 LTO_DECL_STREAM_TYPE_DECL,
362 LTO_DECL_STREAM_NAMESPACE_DECL,
363 LTO_DECL_STREAM_LABEL_DECL,
364 LTO_N_DECL_STREAMS
365 } lto_decl_stream_e_t;
367 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
368 DEF_VEC_I(ld_plugin_symbol_resolution_t);
369 DEF_VEC_ALLOC_I(ld_plugin_symbol_resolution_t, heap);
372 /* Macro to define convenience functions for type and decl streams
373 in lto_file_decl_data. */
374 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
375 static inline tree \
376 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
377 unsigned int idx) \
379 struct lto_in_decl_state *state = data->current_decl_state; \
380 gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
381 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
384 static inline unsigned int \
385 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
387 struct lto_in_decl_state *state = data->current_decl_state; \
388 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
392 /* Return a char pointer to the start of a data stream for an lto pass
393 or function. The first parameter is the file data that contains
394 the information. The second parameter is the type of information
395 to be obtained. The third parameter is the name of the function
396 and is only used when finding a function body; otherwise it is
397 NULL. The fourth parameter is the length of the data returned. */
398 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
399 enum lto_section_type,
400 const char *,
401 size_t *);
403 /* Return the data found from the above call. The first three
404 parameters are the same as above. The fourth parameter is the data
405 itself and the fifth is the lenght of the data. */
406 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
407 enum lto_section_type,
408 const char *,
409 const char *,
410 size_t);
412 /* Cache of pickled nodes. Used to avoid writing the same node more
413 than once. The first time a tree node is streamed out, it is
414 entered in this cache. Subsequent references to the same node are
415 resolved by looking it up in this cache.
417 This is used in two ways:
419 - On the writing side, the first time T is added to STREAMER_CACHE,
420 a new reference index is created for T and T is emitted on the
421 stream. If T needs to be emitted again to the stream, instead of
422 pickling it again, the reference index is emitted.
424 - On the reading side, the first time T is read from the stream, it
425 is reconstructed in memory and a new reference index created for
426 T. The reconstructed T is inserted in some array so that when
427 the reference index for T is found in the input stream, it can be
428 used to look up into the array to get the reconstructed T. */
429 struct lto_streamer_cache_d
431 /* The mapping between tree nodes and slots into the nodes array. */
432 htab_t node_map;
434 /* Node map to store entries into. */
435 alloc_pool node_map_entries;
437 /* The nodes pickled so far. */
438 VEC(tree,heap) *nodes;
442 /* Structure used as buffer for reading an LTO file. */
443 struct lto_input_block
445 const char *data;
446 unsigned int p;
447 unsigned int len;
450 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
451 do { \
452 BASE.data = D; \
453 BASE.p = P; \
454 BASE.len = L; \
455 } while (0)
457 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
458 do { \
459 BASE->data = D; \
460 BASE->p = P; \
461 BASE->len = L; \
462 } while (0)
465 /* The is the first part of the record for a function or constructor
466 in the .o file. */
467 struct lto_header
469 int16_t major_version;
470 int16_t minor_version;
471 enum lto_section_type section_type;
474 /* The header for a function body. */
475 struct lto_function_header
477 /* The header for all types of sections. */
478 struct lto_header lto_header;
480 /* Number of labels with names. */
481 int32_t num_named_labels;
483 /* Number of labels without names. */
484 int32_t num_unnamed_labels;
486 /* Size compressed or 0 if not compressed. */
487 int32_t compressed_size;
489 /* Size of names for named labels. */
490 int32_t named_label_size;
492 /* Size of the cfg. */
493 int32_t cfg_size;
495 /* Size of main gimple body of function. */
496 int32_t main_size;
498 /* Size of the string table. */
499 int32_t string_size;
503 /* Structure describing a symbol section. */
504 struct lto_decl_header
506 /* The header for all types of sections. */
507 struct lto_header lto_header;
509 /* Size of region for decl state. */
510 int32_t decl_state_size;
512 /* Number of nodes in globals stream. */
513 int32_t num_nodes;
515 /* Size of region for expressions, decls, types, etc. */
516 int32_t main_size;
518 /* Size of the string table. */
519 int32_t string_size;
523 /* Statistics gathered during LTO, WPA and LTRANS. */
524 struct lto_stats_d
526 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
527 unsigned HOST_WIDE_INT num_output_cgraph_nodes;
528 unsigned HOST_WIDE_INT num_input_files;
529 unsigned HOST_WIDE_INT num_output_files;
530 unsigned HOST_WIDE_INT num_cgraph_partitions;
531 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
532 unsigned HOST_WIDE_INT num_function_bodies;
533 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
534 unsigned HOST_WIDE_INT num_output_il_bytes;
535 unsigned HOST_WIDE_INT num_compressed_il_bytes;
536 unsigned HOST_WIDE_INT num_input_il_bytes;
537 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
540 /* Encoder data structure used to stream callgraph nodes. */
541 struct lto_cgraph_encoder_d
543 /* Map nodes to reference number. */
544 struct pointer_map_t *map;
546 /* Map reference number to node. */
547 VEC(cgraph_node_ptr,heap) *nodes;
549 /* Map of nodes where we want to output body. */
550 struct pointer_set_t *body;
553 typedef struct lto_cgraph_encoder_d *lto_cgraph_encoder_t;
555 /* Return number of encoded nodes in ENCODER. */
557 static inline int
558 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
560 return VEC_length (cgraph_node_ptr, encoder->nodes);
564 /* Encoder data structure used to stream callgraph nodes. */
565 struct lto_varpool_encoder_d
567 /* Map nodes to reference number. */
568 struct pointer_map_t *map;
570 /* Map reference number to node. */
571 VEC(varpool_node_ptr,heap) *nodes;
573 /* Map of nodes where we want to output initializer. */
574 struct pointer_set_t *initializer;
576 typedef struct lto_varpool_encoder_d *lto_varpool_encoder_t;
578 /* Return number of encoded nodes in ENCODER. */
580 static inline int
581 lto_varpool_encoder_size (lto_varpool_encoder_t encoder)
583 return VEC_length (varpool_node_ptr, encoder->nodes);
586 /* Mapping from indices to trees. */
587 struct GTY(()) lto_tree_ref_table
589 /* Array of referenced trees . */
590 tree * GTY((length ("%h.size"))) trees;
592 /* Size of array. */
593 unsigned int size;
597 /* Mapping between trees and slots in an array. */
598 struct lto_decl_slot
600 tree t;
601 int slot_num;
605 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
607 struct lto_tree_ref_encoder
609 htab_t tree_hash_table; /* Maps pointers to indices. */
610 unsigned int next_index; /* Next available index. */
611 VEC(tree,heap) *trees; /* Maps indices to pointers. */
615 /* Structure to hold states of input scope. */
616 struct GTY(()) lto_in_decl_state
618 /* Array of lto_in_decl_buffers to store type and decls streams. */
619 struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
621 /* If this in-decl state is associated with a function. FN_DECL
622 point to the FUNCTION_DECL. */
623 tree fn_decl;
626 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
629 /* The structure that holds all of the vectors of global types,
630 decls and cgraph nodes used in the serialization of this file. */
631 struct lto_out_decl_state
633 /* The buffers contain the sets of decls of various kinds and types we have
634 seen so far and the indexes assigned to them. */
635 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
637 /* Encoder for cgraph nodes. */
638 lto_cgraph_encoder_t cgraph_node_encoder;
640 /* Encoder for varpool nodes. */
641 lto_varpool_encoder_t varpool_node_encoder;
643 /* If this out-decl state belongs to a function, fn_decl points to that
644 function. Otherwise, it is NULL. */
645 tree fn_decl;
648 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
650 DEF_VEC_P(lto_out_decl_state_ptr);
651 DEF_VEC_ALLOC_P(lto_out_decl_state_ptr, heap);
653 /* One of these is allocated for each object file that being compiled
654 by lto. This structure contains the tables that are needed by the
655 serialized functions and ipa passes to connect themselves to the
656 global types and decls as they are reconstituted. */
657 struct GTY(()) lto_file_decl_data
659 /* Decl state currently used. */
660 struct lto_in_decl_state *current_decl_state;
662 /* Decl state corresponding to regions outside of any functions
663 in the compilation unit. */
664 struct lto_in_decl_state *global_decl_state;
666 /* Table of cgraph nodes present in this file. */
667 lto_cgraph_encoder_t GTY((skip)) cgraph_node_encoder;
669 /* Table of varpool nodes present in this file. */
670 lto_varpool_encoder_t GTY((skip)) varpool_node_encoder;
672 /* Hash table maps lto-related section names to location in file. */
673 htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
675 /* The .o file that these offsets relate to. */
676 const char *GTY((skip)) file_name;
678 /* Hash table maps lto-related section names to location in file. */
679 htab_t GTY((skip)) section_hash_table;
681 /* Hash new name of renamed global declaration to its original name. */
682 htab_t GTY((skip)) renaming_hash_table;
684 /* Linked list used temporarily in reader */
685 struct lto_file_decl_data *next;
687 /* Sub ID for merged objects. */
688 unsigned id;
690 /* Symbol resolutions for this file */
691 VEC(ld_plugin_symbol_resolution_t,heap) * GTY((skip)) resolutions;
693 struct gcov_ctr_summary GTY((skip)) profile_info;
695 /* Any other streamer-specific data needed by the streamer. */
696 void * GTY((skip)) sdata;
699 typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
701 struct lto_char_ptr_base
703 char *ptr;
706 /* An incore byte stream to buffer the various parts of the function.
707 The entire structure should be zeroed when created. The record
708 consists of a set of blocks. The first sizeof (ptr) bytes are used
709 as a chain, and the rest store the bytes to be written. */
710 struct lto_output_stream
712 /* The pointer to the first block in the stream. */
713 struct lto_char_ptr_base * first_block;
715 /* The pointer to the last and current block in the stream. */
716 struct lto_char_ptr_base * current_block;
718 /* The pointer to where the next char should be written. */
719 char * current_pointer;
721 /* The number of characters left in the current block. */
722 unsigned int left_in_block;
724 /* The block size of the last block allocated. */
725 unsigned int block_size;
727 /* The total number of characters written. */
728 unsigned int total_size;
731 /* The is the first part of the record in an LTO file for many of the
732 IPA passes. */
733 struct lto_simple_header
735 /* The header for all types of sections. */
736 struct lto_header lto_header;
738 /* Size of main gimple body of function. */
739 int32_t main_size;
741 /* Size of main stream when compressed. */
742 int32_t compressed_size;
745 /* A simple output block. This can be used for simple IPA passes that
746 do not need more than one stream. */
747 struct lto_simple_output_block
749 enum lto_section_type section_type;
750 struct lto_out_decl_state *decl_state;
752 /* The stream that the main tree codes are written to. */
753 struct lto_output_stream *main_stream;
756 /* Data structure holding all the data and descriptors used when writing
757 an LTO file. */
758 struct output_block
760 enum lto_section_type section_type;
761 struct lto_out_decl_state *decl_state;
763 /* The stream that the main tree codes are written to. */
764 struct lto_output_stream *main_stream;
766 /* The stream that contains the string table. */
767 struct lto_output_stream *string_stream;
769 /* The stream that contains the cfg. */
770 struct lto_output_stream *cfg_stream;
772 /* The hash table that contains the set of strings we have seen so
773 far and the indexes assigned to them. */
774 htab_t string_hash_table;
776 /* The current cgraph_node that we are currently serializing. Null
777 if we are serializing something else. */
778 struct cgraph_node *cgraph_node;
780 /* These are the last file and line that were seen in the stream.
781 If the current node differs from these, it needs to insert
782 something into the stream and fix these up. */
783 const char *current_file;
784 int current_line;
785 int current_col;
787 /* True if writing globals and types. */
788 bool global;
790 /* Cache of nodes written in this section. */
791 struct lto_streamer_cache_d *writer_cache;
793 /* Any other streamer-specific data needed by the streamer. */
794 void *sdata;
798 /* Data and descriptors used when reading from an LTO file. */
799 struct data_in
801 /* The global decls and types. */
802 struct lto_file_decl_data *file_data;
804 /* All of the labels. */
805 tree *labels;
807 /* The string table. */
808 const char *strings;
810 /* The length of the string table. */
811 unsigned int strings_len;
813 /* Number of named labels. Used to find the index of unnamed labels
814 since they share space with the named labels. */
815 unsigned int num_named_labels;
817 /* Number of unnamed labels. */
818 unsigned int num_unnamed_labels;
820 const char *current_file;
821 int current_line;
822 int current_col;
824 /* Maps each reference number to the resolution done by the linker. */
825 VEC(ld_plugin_symbol_resolution_t,heap) *globals_resolution;
827 /* Cache of pickled nodes. */
828 struct lto_streamer_cache_d *reader_cache;
830 /* Any other streamer-specific data needed by the streamer. */
831 void *sdata;
835 /* In lto-section-in.c */
836 extern struct lto_input_block * lto_create_simple_input_block (
837 struct lto_file_decl_data *,
838 enum lto_section_type, const char **, size_t *);
839 extern void
840 lto_destroy_simple_input_block (struct lto_file_decl_data *,
841 enum lto_section_type,
842 struct lto_input_block *, const char *, size_t);
843 extern void lto_set_in_hooks (struct lto_file_decl_data **,
844 lto_get_section_data_f *,
845 lto_free_section_data_f *);
846 extern struct lto_file_decl_data **lto_get_file_decl_data (void);
847 extern const char *lto_get_section_data (struct lto_file_decl_data *,
848 enum lto_section_type,
849 const char *, size_t *);
850 extern void lto_free_section_data (struct lto_file_decl_data *,
851 enum lto_section_type,
852 const char *, const char *, size_t);
853 extern unsigned char lto_input_1_unsigned (struct lto_input_block *);
854 extern unsigned HOST_WIDE_INT lto_input_uleb128 (struct lto_input_block *);
855 extern unsigned HOST_WIDEST_INT lto_input_widest_uint_uleb128 (
856 struct lto_input_block *);
857 extern HOST_WIDE_INT lto_input_sleb128 (struct lto_input_block *);
858 extern htab_t lto_create_renaming_table (void);
859 extern void lto_record_renamed_decl (struct lto_file_decl_data *,
860 const char *, const char *);
861 extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
862 const char *);
863 extern struct lto_in_decl_state *lto_new_in_decl_state (void);
864 extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
865 extern hashval_t lto_hash_in_decl_state (const void *);
866 extern int lto_eq_in_decl_state (const void *, const void *);
867 extern struct lto_in_decl_state *lto_get_function_in_decl_state (
868 struct lto_file_decl_data *, tree);
870 /* In lto-section-out.c */
871 extern hashval_t lto_hash_decl_slot_node (const void *);
872 extern int lto_eq_decl_slot_node (const void *, const void *);
873 extern hashval_t lto_hash_type_slot_node (const void *);
874 extern int lto_eq_type_slot_node (const void *, const void *);
875 extern void lto_begin_section (const char *, bool);
876 extern void lto_end_section (void);
877 extern void lto_write_stream (struct lto_output_stream *);
878 extern void lto_output_1_stream (struct lto_output_stream *, char);
879 extern void lto_output_data_stream (struct lto_output_stream *, const void *,
880 size_t);
881 extern void lto_output_uleb128_stream (struct lto_output_stream *,
882 unsigned HOST_WIDE_INT);
883 extern void lto_output_widest_uint_uleb128_stream (struct lto_output_stream *,
884 unsigned HOST_WIDEST_INT);
885 extern void lto_output_sleb128_stream (struct lto_output_stream *,
886 HOST_WIDE_INT);
887 extern bool lto_output_decl_index (struct lto_output_stream *,
888 struct lto_tree_ref_encoder *,
889 tree, unsigned int *);
890 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
891 struct lto_output_stream *, tree);
892 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
893 struct lto_output_stream *, tree);
894 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
895 struct lto_output_stream *, tree);
896 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
897 struct lto_output_stream *, tree);
898 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
899 struct lto_output_stream *, tree);
900 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
901 struct lto_output_stream *, tree);
902 extern struct lto_simple_output_block *lto_create_simple_output_block (
903 enum lto_section_type);
904 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
905 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
906 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
907 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
908 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
909 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
910 extern void lto_record_function_out_decl_state (tree,
911 struct lto_out_decl_state *);
914 /* In lto-streamer.c. */
915 extern const char *lto_tag_name (enum LTO_tags);
916 extern bitmap lto_bitmap_alloc (void);
917 extern void lto_bitmap_free (bitmap);
918 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
919 extern void print_lto_report (void);
920 extern bool lto_streamer_cache_insert (struct lto_streamer_cache_d *, tree,
921 unsigned *);
922 extern bool lto_streamer_cache_insert_at (struct lto_streamer_cache_d *, tree,
923 unsigned);
924 extern void lto_streamer_cache_append (struct lto_streamer_cache_d *, tree);
925 extern bool lto_streamer_cache_lookup (struct lto_streamer_cache_d *, tree,
926 unsigned *);
927 extern tree lto_streamer_cache_get (struct lto_streamer_cache_d *, unsigned);
928 extern struct lto_streamer_cache_d *lto_streamer_cache_create (void);
929 extern void lto_streamer_cache_delete (struct lto_streamer_cache_d *);
930 extern void lto_streamer_init (void);
931 extern bool gate_lto_out (void);
932 #ifdef LTO_STREAMER_DEBUG
933 extern void lto_orig_address_map (tree, intptr_t);
934 extern intptr_t lto_orig_address_get (tree);
935 extern void lto_orig_address_remove (tree);
936 #endif
937 extern void lto_check_version (int, int);
938 extern void gimple_streamer_hooks_init (void);
939 extern void gimple_streamer_write_tree (struct output_block *, tree, bool);
940 extern void gimple_streamer_read_tree (struct lto_input_block *,
941 struct data_in *, tree);
942 extern lto_streamer_hooks *streamer_hooks (void);
943 extern lto_streamer_hooks *streamer_hooks_init (void);
945 /* In lto-streamer-in.c */
946 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
947 extern void lto_reader_init (void);
948 extern tree lto_input_tree (struct lto_input_block *, struct data_in *);
949 extern tree lto_input_chain (struct lto_input_block *, struct data_in *);
950 extern void lto_input_function_body (struct lto_file_decl_data *, tree,
951 const char *);
952 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
953 const char *);
954 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
955 const char *, unsigned,
956 VEC(ld_plugin_symbol_resolution_t,heap) *);
957 extern void lto_data_in_delete (struct data_in *);
958 extern const char *lto_input_string (struct data_in *,
959 struct lto_input_block *);
960 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
961 extern void gimple_streamer_reader_init (void);
964 /* In lto-streamer-out.c */
965 extern void lto_writer_init (void);
966 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
967 extern struct output_block *create_output_block (enum lto_section_type);
968 extern void destroy_output_block (struct output_block *);
969 extern void lto_output_tree (struct output_block *, tree, bool);
970 extern void lto_output_tree_or_ref (struct output_block *, tree, bool);
971 extern void lto_output_chain (struct output_block *, tree, bool);
972 extern void produce_asm (struct output_block *ob, tree fn);
973 extern void lto_output_string (struct output_block *,
974 struct lto_output_stream *,
975 const char *);
976 extern void lto_output_string_with_length (struct output_block *,
977 struct lto_output_stream *,
978 const char *,
979 unsigned int);
980 void lto_output_decl_state_streams (struct output_block *,
981 struct lto_out_decl_state *);
982 void lto_output_decl_state_refs (struct output_block *,
983 struct lto_output_stream *,
984 struct lto_out_decl_state *);
987 /* In lto-cgraph.c */
988 struct cgraph_node *lto_cgraph_encoder_deref (lto_cgraph_encoder_t, int);
989 int lto_cgraph_encoder_lookup (lto_cgraph_encoder_t, struct cgraph_node *);
990 lto_cgraph_encoder_t lto_cgraph_encoder_new (void);
991 int lto_cgraph_encoder_encode (lto_cgraph_encoder_t, struct cgraph_node *);
992 void lto_cgraph_encoder_delete (lto_cgraph_encoder_t);
993 bool lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t,
994 struct cgraph_node *);
996 bool lto_varpool_encoder_encode_body_p (lto_varpool_encoder_t,
997 struct varpool_node *);
998 struct varpool_node *lto_varpool_encoder_deref (lto_varpool_encoder_t, int);
999 int lto_varpool_encoder_lookup (lto_varpool_encoder_t, struct varpool_node *);
1000 lto_varpool_encoder_t lto_varpool_encoder_new (void);
1001 int lto_varpool_encoder_encode (lto_varpool_encoder_t, struct varpool_node *);
1002 void lto_varpool_encoder_delete (lto_varpool_encoder_t);
1003 bool lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t,
1004 struct varpool_node *);
1005 void output_cgraph (cgraph_node_set, varpool_node_set);
1006 void input_cgraph (void);
1007 bool referenced_from_other_partition_p (struct ipa_ref_list *,
1008 cgraph_node_set,
1009 varpool_node_set vset);
1010 bool reachable_from_other_partition_p (struct cgraph_node *,
1011 cgraph_node_set);
1012 bool referenced_from_this_partition_p (struct ipa_ref_list *,
1013 cgraph_node_set,
1014 varpool_node_set vset);
1015 bool reachable_from_this_partition_p (struct cgraph_node *,
1016 cgraph_node_set);
1017 void compute_ltrans_boundary (struct lto_out_decl_state *state,
1018 cgraph_node_set, varpool_node_set);
1021 /* In lto-symtab.c. */
1022 extern void lto_symtab_register_decl (tree, ld_plugin_symbol_resolution_t,
1023 struct lto_file_decl_data *);
1024 extern void lto_symtab_merge_decls (void);
1025 extern void lto_symtab_merge_cgraph_nodes (void);
1026 extern tree lto_symtab_prevailing_decl (tree decl);
1027 extern enum ld_plugin_symbol_resolution lto_symtab_get_resolution (tree decl);
1028 extern void lto_symtab_free (void);
1029 extern GTY(()) VEC(tree,gc) *lto_global_var_decls;
1032 /* In lto-opts.c. */
1033 extern void lto_register_user_option (size_t, const char *, int, unsigned int);
1034 extern void lto_read_file_options (struct lto_file_decl_data *);
1035 extern void lto_write_options (void);
1036 extern void lto_reissue_options (void);
1037 void lto_clear_user_options (void);
1038 void lto_clear_file_options (void);
1041 /* In lto-wpa-fixup.c */
1042 void lto_mark_nothrow_fndecl (tree);
1043 void lto_fixup_nothrow_decls (void);
1046 /* Statistics gathered during LTO, WPA and LTRANS. */
1047 extern struct lto_stats_d lto_stats;
1049 /* Section names corresponding to the values of enum lto_section_type. */
1050 extern const char *lto_section_name[];
1052 /* Holds all the out decl states of functions output so far in the
1053 current output file. */
1054 extern VEC(lto_out_decl_state_ptr, heap) *lto_function_decl_states;
1056 /* Return true if LTO tag TAG corresponds to a tree code. */
1057 static inline bool
1058 lto_tag_is_tree_code_p (enum LTO_tags tag)
1060 return tag > LTO_null && (unsigned) tag <= MAX_TREE_CODES;
1064 /* Return true if LTO tag TAG corresponds to a gimple code. */
1065 static inline bool
1066 lto_tag_is_gimple_code_p (enum LTO_tags tag)
1068 return (unsigned) tag >= NUM_TREE_CODES + 1
1069 && (unsigned) tag < 1 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
1073 /* Return the LTO tag corresponding to gimple code CODE. See enum
1074 LTO_tags for details on the conversion. */
1075 static inline enum LTO_tags
1076 lto_gimple_code_to_tag (enum gimple_code code)
1078 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 1);
1082 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
1083 details on the conversion. */
1084 static inline enum gimple_code
1085 lto_tag_to_gimple_code (enum LTO_tags tag)
1087 gcc_assert (lto_tag_is_gimple_code_p (tag));
1088 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 1);
1092 /* Return the LTO tag corresponding to tree code CODE. See enum
1093 LTO_tags for details on the conversion. */
1094 static inline enum LTO_tags
1095 lto_tree_code_to_tag (enum tree_code code)
1097 return (enum LTO_tags) ((unsigned) code + 1);
1101 /* Return the tree code corresponding to TAG. See enum LTO_tags for
1102 details on the conversion. */
1103 static inline enum tree_code
1104 lto_tag_to_tree_code (enum LTO_tags tag)
1106 gcc_assert (lto_tag_is_tree_code_p (tag));
1107 return (enum tree_code) ((unsigned) tag - 1);
1110 /* Initialize an lto_out_decl_buffer ENCODER. */
1111 static inline void
1112 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder,
1113 htab_hash hash_fn, htab_eq eq_fn)
1115 encoder->tree_hash_table = htab_create (37, hash_fn, eq_fn, free);
1116 encoder->next_index = 0;
1117 encoder->trees = NULL;
1121 /* Destory an lto_tree_ref_encoder ENCODER by freeing its contents. The
1122 memory used by ENCODER is not freed by this function. */
1123 static inline void
1124 lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1126 /* Hash table may be delete already. */
1127 if (encoder->tree_hash_table)
1128 htab_delete (encoder->tree_hash_table);
1129 VEC_free (tree, heap, encoder->trees);
1132 /* Return the number of trees encoded in ENCODER. */
1133 static inline unsigned int
1134 lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1136 return VEC_length (tree, encoder->trees);
1139 /* Return the IDX-th tree in ENCODER. */
1140 static inline tree
1141 lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1142 unsigned int idx)
1144 return VEC_index (tree, encoder->trees, idx);
1148 /* Return true if LABEL should be emitted in the global context. */
1149 static inline bool
1150 emit_label_in_global_context_p (tree label)
1152 return DECL_NONLOCAL (label) || FORCED_LABEL (label);
1155 /* Return true if tree node EXPR should be streamed as a builtin. For
1156 these nodes, we just emit the class and function code. */
1157 static inline bool
1158 lto_stream_as_builtin_p (tree expr)
1160 return (TREE_CODE (expr) == FUNCTION_DECL
1161 && DECL_IS_BUILTIN (expr)
1162 && (DECL_BUILT_IN_CLASS (expr) == BUILT_IN_NORMAL
1163 || DECL_BUILT_IN_CLASS (expr) == BUILT_IN_MD));
1166 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1167 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1168 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1169 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1170 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1171 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1172 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1174 /* Returns a new bit-packing context for bit-packing into S. */
1175 static inline struct bitpack_d
1176 bitpack_create (struct lto_output_stream *s)
1178 struct bitpack_d bp;
1179 bp.pos = 0;
1180 bp.word = 0;
1181 bp.stream = (void *)s;
1182 return bp;
1185 /* Pack the NBITS bit sized value VAL into the bit-packing context BP. */
1186 static inline void
1187 bp_pack_value (struct bitpack_d *bp, bitpack_word_t val, unsigned nbits)
1189 bitpack_word_t word = bp->word;
1190 int pos = bp->pos;
1192 /* We shouldn't try to pack more bits than can fit in a bitpack word. */
1193 gcc_assert (nbits > 0 && nbits <= BITS_PER_BITPACK_WORD);
1195 /* The value to pack should not overflow NBITS. */
1196 gcc_assert (nbits == BITS_PER_BITPACK_WORD
1197 || val <= ((bitpack_word_t) 1 << nbits));
1199 /* If val does not fit into the current bitpack word switch to the
1200 next one. */
1201 if (pos + nbits > BITS_PER_BITPACK_WORD)
1203 lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream, word);
1204 word = val;
1205 pos = nbits;
1207 else
1209 word |= val << pos;
1210 pos += nbits;
1212 bp->word = word;
1213 bp->pos = pos;
1216 /* Finishes bit-packing of BP. */
1217 static inline void
1218 lto_output_bitpack (struct bitpack_d *bp)
1220 lto_output_uleb128_stream ((struct lto_output_stream *) bp->stream,
1221 bp->word);
1222 bp->word = 0;
1223 bp->pos = 0;
1226 /* Returns a new bit-packing context for bit-unpacking from IB. */
1227 static inline struct bitpack_d
1228 lto_input_bitpack (struct lto_input_block *ib)
1230 struct bitpack_d bp;
1231 bp.word = lto_input_uleb128 (ib);
1232 bp.pos = 0;
1233 bp.stream = (void *)ib;
1234 return bp;
1237 /* Unpacks NBITS bits from the bit-packing context BP and returns them. */
1238 static inline bitpack_word_t
1239 bp_unpack_value (struct bitpack_d *bp, unsigned nbits)
1241 bitpack_word_t mask, val;
1242 int pos = bp->pos;
1244 mask = (nbits == BITS_PER_BITPACK_WORD
1245 ? (bitpack_word_t) -1
1246 : ((bitpack_word_t) 1 << nbits) - 1);
1248 /* If there are not continuous nbits in the current bitpack word
1249 switch to the next one. */
1250 if (pos + nbits > BITS_PER_BITPACK_WORD)
1252 bp->word = val = lto_input_uleb128 ((struct lto_input_block *)bp->stream);
1253 bp->pos = nbits;
1254 return val & mask;
1256 val = bp->word;
1257 val >>= pos;
1258 bp->pos = pos + nbits;
1260 return val & mask;
1263 #endif /* GCC_LTO_STREAMER_H */