Line data Source code
1 : /* Data structures and declarations used for reading and writing
2 : GIMPLE to a file stream.
3 :
4 : Copyright (C) 2009-2026 Free Software Foundation, Inc.
5 : Contributed by Doug Kwan <dougkwan@google.com>
6 :
7 : This file is part of GCC.
8 :
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.
13 :
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.
18 :
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/>. */
22 :
23 : #ifndef GCC_LTO_STREAMER_H
24 : #define GCC_LTO_STREAMER_H
25 :
26 : #include "plugin-api.h"
27 : #include "gcov-io.h"
28 : #include "diagnostic.h"
29 : #include "version.h"
30 :
31 : /* The encoding for a function consists of the following sections:
32 :
33 : 1) The header.
34 : 2) FIELD_DECLS.
35 : 3) FUNCTION_DECLS.
36 : 4) global VAR_DECLS.
37 : 5) type_decls
38 : 6) types.
39 : 7) Names for the labels that have names
40 : 8) The SSA names.
41 : 9) The control flow graph.
42 : 10-11)Gimple for local decls.
43 : 12) Gimple for the function.
44 : 13) Strings.
45 :
46 : 1) THE HEADER.
47 : 2-6) THE GLOBAL DECLS AND TYPES.
48 :
49 : The global decls and types are encoded in the same way. For each
50 : entry, there is word with the offset within the section to the
51 : entry.
52 :
53 : 7) THE LABEL NAMES.
54 :
55 : Since most labels do not have names, this section my be of zero
56 : length. It consists of an array of string table references, one
57 : per label. In the lto code, the labels are given either
58 : positive or negative indexes. the positive ones have names and
59 : the negative ones do not. The positive index can be used to
60 : find the name in this array.
61 :
62 : 9) THE CFG.
63 :
64 : 10) Index into the local decls. Since local decls can have local
65 : decls inside them, they must be read in randomly in order to
66 : properly restore them.
67 :
68 : 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
69 :
70 : The gimple consists of a set of records.
71 :
72 : THE FUNCTION
73 :
74 : At the top level of (8) is the function. It consists of five
75 : pieces:
76 :
77 : LTO_function - The tag.
78 : eh tree - This is all of the exception handling regions
79 : put out in a post order traversial of the
80 : tree. Siblings are output as lists terminated
81 : by a 0. The set of fields matches the fields
82 : defined in except.cc.
83 :
84 : last_basic_block - in uleb128 form.
85 :
86 : basic blocks - This is the set of basic blocks.
87 :
88 : zero - The termination of the basic blocks.
89 :
90 : BASIC BLOCKS
91 :
92 : There are two forms of basic blocks depending on if they are
93 : empty or not.
94 :
95 : The basic block consists of:
96 :
97 : LTO_bb1 or LTO_bb0 - The tag.
98 :
99 : bb->index - the index in uleb128 form.
100 :
101 : #succs - The number of successors un uleb128 form.
102 :
103 : the successors - For each edge, a pair. The first of the
104 : pair is the index of the successor in
105 : uleb128 form and the second are the flags in
106 : uleb128 form.
107 :
108 : the statements - A gimple tree, as described above.
109 : These are only present for LTO_BB1.
110 : Following each statement is an optional
111 : exception handling record LTO_eh_region
112 : which contains the region number (for
113 : regions >= 0).
114 :
115 : zero - This is only present for LTO_BB1 and is used
116 : to terminate the statements and exception
117 : regions within this block.
118 :
119 : 12) STRINGS
120 :
121 : String are represented in the table as pairs, a length in ULEB128
122 : form followed by the data for the string. */
123 :
124 : #define LTO_major_version GCC_major_version
125 : #define LTO_minor_version 0
126 :
127 : typedef unsigned char lto_decl_flags_t;
128 :
129 : /* Tags representing the various IL objects written to the bytecode file
130 : (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
131 :
132 : NOTE, when adding new LTO tags, also update lto_tag_name. */
133 : enum LTO_tags
134 : {
135 : LTO_null = 0,
136 :
137 : /* Reference to previously-streamed node. */
138 : LTO_tree_pickle_reference,
139 :
140 : /* References to indexable tree nodes. These objects are stored in
141 : tables that are written separately from the function bodies
142 : and variable constructors that reference them. This way they can be
143 : instantiated even when the referencing functions aren't (e.g., during WPA)
144 : and it also allows functions to be copied from one file to another without
145 : having to unpickle the body first (the references are location
146 : independent). */
147 : LTO_global_stream_ref,
148 :
149 : LTO_ssa_name_ref,
150 :
151 : /* Special for global streamer. A blob of unnamed tree nodes. */
152 : LTO_tree_scc,
153 :
154 : /* Sequence of trees. */
155 : LTO_trees,
156 :
157 : /* Shared INTEGER_CST node. */
158 : LTO_integer_cst,
159 :
160 : /* Tags of trees are encoded as
161 : LTO_first_tree_tag + TREE_CODE. */
162 : LTO_first_tree_tag,
163 : /* Tags of gimple typles are encoded as
164 : LTO_first_gimple_tag + gimple_code. */
165 : LTO_first_gimple_tag = LTO_first_tree_tag + MAX_TREE_CODES,
166 :
167 : /* Entry and exit basic blocks. */
168 : LTO_bb0 = LTO_first_gimple_tag + LAST_AND_UNUSED_GIMPLE_CODE,
169 : LTO_bb1,
170 :
171 : /* EH region holding the previous statement. */
172 : LTO_eh_region,
173 :
174 : /* Function body. */
175 : LTO_function,
176 :
177 : /* EH table. */
178 : LTO_eh_table,
179 :
180 : /* EH region types. These mirror enum eh_region_type. */
181 : LTO_ert_cleanup,
182 : LTO_ert_try,
183 : LTO_ert_allowed_exceptions,
184 : LTO_ert_must_not_throw,
185 :
186 : /* EH landing pad. */
187 : LTO_eh_landing_pad,
188 :
189 : /* EH try/catch node. */
190 : LTO_eh_catch,
191 :
192 : /* This tag must always be last. */
193 : LTO_NUM_TAGS
194 : };
195 :
196 :
197 : /* Set of section types that are in an LTO file. This list will grow
198 : as the number of IPA passes grows since each IPA pass will need its
199 : own section type to store its summary information.
200 :
201 : When adding a new section type, you must also extend the
202 : LTO_SECTION_NAME array in lto-section-in.cc. */
203 : enum lto_section_type
204 : {
205 : LTO_section_decls = 0,
206 : LTO_section_function_body,
207 : LTO_section_static_initializer,
208 : LTO_section_symtab,
209 : LTO_section_symtab_extension,
210 : LTO_section_refs,
211 : LTO_section_asm,
212 : LTO_section_jump_functions,
213 : LTO_section_ipa_pure_const,
214 : LTO_section_ipa_reference,
215 : LTO_section_ipa_profile,
216 : LTO_section_symtab_nodes,
217 : LTO_section_opts,
218 : LTO_section_cgraph_opt_sum,
219 : LTO_section_ipa_fn_summary,
220 : LTO_section_ipcp_transform,
221 : LTO_section_ipa_icf,
222 : LTO_section_offload_table,
223 : LTO_section_mode_table,
224 : LTO_section_lto,
225 : LTO_section_ipa_sra,
226 : LTO_section_odr_types,
227 : LTO_section_ipa_modref,
228 : LTO_N_SECTION_TYPES /* Must be last. */
229 : };
230 :
231 : /* Indices to the various function, type and symbol streams. */
232 : enum lto_decl_stream_e_t
233 : {
234 : LTO_DECL_STREAM = 0, /* Must be first. */
235 : LTO_N_DECL_STREAMS
236 : };
237 :
238 : typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
239 :
240 : /* Return a char pointer to the start of a data stream for an lto pass
241 : or function. The first parameter is the file data that contains
242 : the information. The second parameter is the type of information
243 : to be obtained. The third parameter is the name of the function
244 : and is only used when finding a function body; otherwise it is
245 : NULL. The fourth parameter is the length of the data returned. */
246 : typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
247 : enum lto_section_type,
248 : const char *,
249 : int,
250 : size_t *);
251 :
252 : /* Return the data found from the above call. The first three
253 : parameters are the same as above. The fourth parameter is the data
254 : itself and the fifth is the length of the data. */
255 : typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
256 : enum lto_section_type,
257 : const char *,
258 : const char *,
259 : size_t);
260 :
261 : /* The location cache holds expanded locations for streamed in trees.
262 : This is done to reduce memory usage of libcpp linemap that strongly prefers
263 : locations to be inserted in the source order. */
264 :
265 : class lto_location_cache
266 : {
267 : public:
268 : /* Apply all changes in location cache. Add locations into linemap and patch
269 : trees. */
270 : bool apply_location_cache ();
271 : /* Tree merging did not suceed; mark all changes in the cache as accepted. */
272 : void accept_location_cache ();
273 : /* Tree merging did suceed; throw away recent changes. */
274 : void revert_location_cache ();
275 : void input_location (location_t *loc, struct bitpack_d *bp,
276 : class data_in *data_in);
277 : void input_location_and_block (location_t *loc, struct bitpack_d *bp,
278 : class lto_input_block *ib,
279 : class data_in *data_in);
280 198747 : lto_location_cache ()
281 198747 : : loc_cache (), accepted_length (0), current_file (NULL), current_line (0),
282 198747 : current_col (0), current_sysp (false), current_loc (UNKNOWN_LOCATION),
283 198747 : current_block (NULL_TREE)
284 : {
285 198747 : gcc_assert (!current_cache);
286 198747 : current_cache = this;
287 198747 : }
288 198747 : ~lto_location_cache ()
289 : {
290 198747 : apply_location_cache ();
291 198747 : gcc_assert (current_cache == this);
292 198747 : current_cache = NULL;
293 198747 : }
294 :
295 : /* There can be at most one instance of location cache (combining multiple
296 : would bring it out of sync with libcpp linemap); point to current
297 : one. */
298 : static lto_location_cache *current_cache;
299 :
300 : private:
301 : static int cmp_loc (const void *pa, const void *pb);
302 :
303 : struct cached_location
304 : {
305 : const char *file;
306 : location_t *loc;
307 : int line, col;
308 : bool sysp;
309 : tree block;
310 : unsigned discr;
311 : };
312 :
313 : /* The location cache. */
314 :
315 : auto_vec<cached_location> loc_cache;
316 :
317 : /* Accepted entries are ones used by trees that are known to be not unified
318 : by tree merging. */
319 :
320 : int accepted_length;
321 :
322 : /* Bookkeeping to remember state in between calls to lto_apply_location_cache
323 : When streaming gimple, the location cache is not used and thus
324 : lto_apply_location_cache happens per location basis. It is then
325 : useful to avoid redundant calls of linemap API. */
326 :
327 : const char *current_file;
328 : int current_line;
329 : int current_col;
330 : bool current_sysp;
331 : location_t current_loc;
332 : tree current_block;
333 : unsigned current_discr;
334 : };
335 :
336 : /* Structure used as buffer for reading an LTO file. */
337 : class lto_input_block
338 : {
339 : public:
340 : /* Special constructor for the string table, it abuses this to
341 : do random access but use the uhwi decoder. */
342 1335064 : lto_input_block (const char *data_, unsigned int p_, unsigned int len_,
343 : const lto_file_decl_data *file_data_)
344 1335064 : : data (data_), file_data (file_data_), p (p_), len (len_) {}
345 344405 : lto_input_block (const char *data_, unsigned int len_,
346 : const lto_file_decl_data *file_data_)
347 260885 : : data (data_), file_data (file_data_), p (0), len (len_) {}
348 :
349 : const char *data;
350 : const lto_file_decl_data *file_data;
351 : unsigned int p;
352 : unsigned int len;
353 : };
354 :
355 : /* Compression algorithm used for compression of LTO bytecode. */
356 :
357 : enum lto_compression
358 : {
359 : ZLIB,
360 : ZSTD
361 : };
362 :
363 : /* Structure that represents LTO ELF section with information
364 : about the format. */
365 :
366 : struct lto_section
367 : {
368 : int16_t major_version;
369 : int16_t minor_version;
370 : unsigned char slim_object;
371 : unsigned char _padding;
372 :
373 : /* Flags is a private field that is not defined publicly. */
374 : uint16_t flags;
375 :
376 : /* Set compression to FLAGS. */
377 31226 : inline void set_compression (lto_compression c)
378 : {
379 31226 : flags = c;
380 : }
381 :
382 : /* Get compression from FLAGS. */
383 208718 : inline lto_compression get_compression ()
384 : {
385 208718 : return (lto_compression) flags;
386 : }
387 : };
388 :
389 : STATIC_ASSERT (sizeof (lto_section) == 8);
390 :
391 : /* The is the first part of the record in an LTO file for many of the
392 : IPA passes. */
393 : struct lto_simple_header
394 : {
395 : /* Size of main gimple body of function. */
396 : int32_t main_size;
397 : };
398 :
399 : struct lto_simple_header_with_strings : lto_simple_header
400 : {
401 : /* Size of the string table. */
402 : int32_t string_size;
403 : };
404 :
405 : /* The header for a function body. */
406 : struct lto_function_header : lto_simple_header_with_strings
407 : {
408 : /* Size of the cfg. */
409 : int32_t cfg_size;
410 : };
411 :
412 :
413 : /* Structure describing a symbol section. */
414 : struct lto_decl_header : lto_simple_header_with_strings
415 : {
416 : /* Size of region for decl state. */
417 : int32_t decl_state_size;
418 :
419 : /* Number of nodes in globals stream. */
420 : int32_t num_nodes;
421 : };
422 :
423 :
424 : /* Statistics gathered during LTO, WPA and LTRANS. */
425 : struct lto_stats_d
426 : {
427 : unsigned HOST_WIDE_INT num_input_cgraph_nodes;
428 : unsigned HOST_WIDE_INT num_output_symtab_nodes;
429 : unsigned HOST_WIDE_INT num_input_files;
430 : unsigned HOST_WIDE_INT num_output_files;
431 : unsigned HOST_WIDE_INT num_cgraph_partitions;
432 : unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
433 : unsigned HOST_WIDE_INT num_function_bodies;
434 : unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
435 : unsigned HOST_WIDE_INT num_output_il_bytes;
436 : unsigned HOST_WIDE_INT num_compressed_il_bytes;
437 : unsigned HOST_WIDE_INT num_input_il_bytes;
438 : unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
439 : unsigned HOST_WIDE_INT num_tree_bodies_output;
440 : unsigned HOST_WIDE_INT num_pickle_refs_output;
441 : };
442 :
443 : /* Entry of LTO symtab encoder. */
444 : struct lto_encoder_entry
445 : {
446 : /* Constructor. */
447 1420473 : lto_encoder_entry (toplevel_node* n)
448 1420473 : : node (n), in_partition (false), body (false), only_for_inlining (true),
449 1420473 : initializer (false)
450 : {}
451 :
452 : toplevel_node *node;
453 : /* Is the node in this partition (i.e. ltrans of this partition will
454 : be responsible for outputting it)? */
455 : unsigned int in_partition:1;
456 : /* Do we encode body in this partition? */
457 : unsigned int body:1;
458 : /* Do we stream this node only for inlining? */
459 : unsigned int only_for_inlining:1;
460 : /* Do we encode initializer in this partition?
461 : For example the readonly variable initializers are encoded to aid
462 : constant folding even if they are not in the partition. */
463 : unsigned int initializer:1;
464 : };
465 :
466 :
467 : /* Encoder data structure used to stream callgraph nodes. */
468 : struct lto_symtab_encoder_d
469 : {
470 : vec<lto_encoder_entry> nodes;
471 : hash_map<toplevel_node *, size_t> *map;
472 :
473 : /* Mapping of input order of nodes onto output order. */
474 : hash_map<int_hash<int, -1, -2>, int> *order_remap;
475 : };
476 :
477 : typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
478 :
479 : /* Iterator structure for cgraph node sets. */
480 : struct lto_symtab_encoder_iterator
481 : {
482 : lto_symtab_encoder_t encoder;
483 : unsigned index;
484 : };
485 :
486 :
487 :
488 : /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
489 :
490 : struct lto_tree_ref_encoder
491 : {
492 : hash_map<tree, unsigned> *tree_hash_table; /* Maps pointers to indices. */
493 : vec<tree> trees; /* Maps indices to pointers. */
494 : };
495 :
496 :
497 : /* Structure to hold states of input scope. */
498 : struct GTY((for_user)) lto_in_decl_state
499 : {
500 : /* Array of lto_in_decl_buffers to store type and decls streams. */
501 : vec<tree, va_gc> *streams[LTO_N_DECL_STREAMS];
502 :
503 : /* If this in-decl state is associated with a function. FN_DECL
504 : point to the FUNCTION_DECL. */
505 : tree fn_decl;
506 :
507 : /* True if decl state is compressed. */
508 : bool compressed;
509 : };
510 :
511 : typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
512 :
513 : struct decl_state_hasher : ggc_ptr_hash<lto_in_decl_state>
514 : {
515 : static hashval_t
516 1967950 : hash (lto_in_decl_state *s)
517 : {
518 1967950 : return htab_hash_pointer (s->fn_decl);
519 : }
520 :
521 : static bool
522 1631935 : equal (lto_in_decl_state *a, lto_in_decl_state *b)
523 : {
524 1631935 : return a->fn_decl == b->fn_decl;
525 : }
526 : };
527 :
528 : /* The structure that holds all of the vectors of global types,
529 : decls and cgraph nodes used in the serialization of this file. */
530 : struct lto_out_decl_state
531 : {
532 : /* The buffers contain the sets of decls of various kinds and types we have
533 : seen so far and the indexes assigned to them. */
534 : struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
535 :
536 : /* Encoder for cgraph nodes. */
537 : lto_symtab_encoder_t symtab_node_encoder;
538 :
539 : /* If this out-decl state belongs to a function, fn_decl points to that
540 : function. Otherwise, it is NULL. */
541 : tree fn_decl;
542 :
543 : /* True if decl state is compressed. */
544 : bool compressed;
545 :
546 : /* True if offload tables should be output. */
547 : bool output_offload_tables_p;
548 : };
549 :
550 : typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
551 :
552 :
553 : /* Compact representation of a index <-> resolution pair. Unpacked to an
554 : vector later. */
555 : struct res_pair
556 : {
557 : ld_plugin_symbol_resolution_t res;
558 : unsigned index;
559 : };
560 :
561 :
562 : /* One of these is allocated for each object file that being compiled
563 : by lto. This structure contains the tables that are needed by the
564 : serialized functions and ipa passes to connect themselves to the
565 : global types and decls as they are reconstituted. */
566 : struct GTY(()) lto_file_decl_data
567 : {
568 : /* Decl state currently used. */
569 : struct lto_in_decl_state *current_decl_state;
570 :
571 : /* Decl state corresponding to regions outside of any functions
572 : in the compilation unit. */
573 : struct lto_in_decl_state *global_decl_state;
574 :
575 : /* Table of cgraph nodes present in this file. */
576 : lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
577 :
578 : /* Hash table maps lto-related section names to location in file. */
579 : hash_table<decl_state_hasher> *function_decl_states;
580 :
581 : /* The .o file that these offsets relate to. */
582 : const char *GTY((skip)) file_name;
583 :
584 : /* Hash table maps lto-related section names to location in file. */
585 : htab_t GTY((skip)) section_hash_table;
586 :
587 : /* Hash new name of renamed global declaration to its original name. */
588 : htab_t GTY((skip)) renaming_hash_table;
589 :
590 : /* Linked list used temporarily in reader */
591 : struct lto_file_decl_data *next;
592 :
593 : /* Order in which the file appears on the command line. */
594 : int order;
595 :
596 : /* Sub ID for merged objects. */
597 : unsigned HOST_WIDE_INT id;
598 :
599 : /* Symbol resolutions for this file */
600 : vec<res_pair> GTY((skip)) respairs;
601 : unsigned max_index;
602 :
603 : gcov_summary GTY((skip)) profile_info;
604 :
605 : /* Map assigning declarations their resolutions. */
606 : hash_map<tree, ld_plugin_symbol_resolution> * GTY((skip)) resolution_map;
607 :
608 : /* Mode translation table. */
609 : const unsigned char *mode_table;
610 :
611 : /* Read LTO section. */
612 : lto_section lto_section_header;
613 :
614 : int order_base;
615 :
616 : int unit_base;
617 :
618 : unsigned mode_bits;
619 : };
620 :
621 : typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
622 :
623 : struct lto_char_ptr_base
624 : {
625 : char *ptr;
626 : };
627 :
628 : /* An incore byte stream to buffer the various parts of the function.
629 : The entire structure should be zeroed when created. The record
630 : consists of a set of blocks. The first sizeof (ptr) bytes are used
631 : as a chain, and the rest store the bytes to be written. */
632 : struct lto_output_stream
633 : {
634 : /* The pointer to the first block in the stream. */
635 : struct lto_char_ptr_base * first_block;
636 :
637 : /* The pointer to the last and current block in the stream. */
638 : struct lto_char_ptr_base * current_block;
639 :
640 : /* The pointer to where the next char should be written. */
641 : char * current_pointer;
642 :
643 : /* The number of characters left in the current block. */
644 : unsigned int left_in_block;
645 :
646 : /* The block size of the last block allocated. */
647 : unsigned int block_size;
648 :
649 : /* The total number of characters written. */
650 : unsigned int total_size;
651 : };
652 :
653 : /* A simple output block. This can be used for simple IPA passes that
654 : do not need more than one stream. */
655 : struct lto_simple_output_block
656 : {
657 : enum lto_section_type section_type;
658 : struct lto_out_decl_state *decl_state;
659 :
660 : /* The stream that the main tree codes are written to. */
661 : struct lto_output_stream *main_stream;
662 : };
663 :
664 : /* String hashing. */
665 :
666 : struct string_slot
667 : {
668 : const char *s;
669 : int len;
670 : unsigned int slot_num;
671 : };
672 :
673 : /* Hashtable helpers. */
674 :
675 : struct string_slot_hasher : nofree_ptr_hash <string_slot>
676 : {
677 : static inline hashval_t hash (const string_slot *);
678 : static inline bool equal (const string_slot *, const string_slot *);
679 : };
680 :
681 : /* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
682 : to support strings that may not end in '\0'. */
683 :
684 : inline hashval_t
685 11159331 : string_slot_hasher::hash (const string_slot *ds)
686 : {
687 11159331 : hashval_t r = ds->len;
688 11159331 : int i;
689 :
690 282315037 : for (i = 0; i < ds->len; i++)
691 271155706 : r = r * 67 + (unsigned)ds->s[i] - 113;
692 11159331 : return r;
693 : }
694 :
695 : /* Returns nonzero if DS1 and DS2 are equal. */
696 :
697 : inline bool
698 8888673 : string_slot_hasher::equal (const string_slot *ds1, const string_slot *ds2)
699 : {
700 8888673 : if (ds1->len == ds2->len)
701 4938957 : return memcmp (ds1->s, ds2->s, ds1->len) == 0;
702 :
703 : return 0;
704 : }
705 :
706 : /* Data structure holding all the data and descriptors used when writing
707 : an LTO file. */
708 : struct output_block
709 : {
710 : enum lto_section_type section_type;
711 : struct lto_out_decl_state *decl_state;
712 :
713 : /* The stream that the main tree codes are written to. */
714 : struct lto_output_stream *main_stream;
715 :
716 : /* The stream that contains the string table. */
717 : struct lto_output_stream *string_stream;
718 :
719 : /* The stream that contains the cfg. */
720 : struct lto_output_stream *cfg_stream;
721 :
722 : /* The hash table that contains the set of strings we have seen so
723 : far and the indexes assigned to them. */
724 : hash_table<string_slot_hasher> *string_hash_table;
725 :
726 : /* The current symbol that we are currently serializing. Null
727 : if we are serializing something else. */
728 : symtab_node *symbol;
729 :
730 : /* These are the last file and line that were seen in the stream.
731 : If the current node differs from these, it needs to insert
732 : something into the stream and fix these up. */
733 : const char *current_file;
734 : int current_line;
735 : int current_col;
736 : bool current_sysp;
737 : bool reset_locus;
738 : bool emit_pwd;
739 : tree current_block;
740 : unsigned current_discr;
741 :
742 : /* Cache of nodes written in this section. */
743 : struct streamer_tree_cache_d *writer_cache;
744 :
745 : /* All trees identified as local to the unit streamed. */
746 : hash_set<tree> *local_trees;
747 :
748 : /* All data persistent across whole duration of output block
749 : can go here. */
750 : struct obstack obstack;
751 : };
752 :
753 :
754 : /* Data and descriptors used when reading from an LTO file. */
755 397494 : class data_in
756 : {
757 : public:
758 : /* The global decls and types. */
759 : struct lto_file_decl_data *file_data;
760 :
761 : /* The string table. */
762 : const char *strings;
763 :
764 : /* The length of the string table. */
765 : unsigned int strings_len;
766 :
767 : /* Maps each reference number to the resolution done by the linker. */
768 : vec<ld_plugin_symbol_resolution_t> globals_resolution;
769 :
770 : /* Cache of pickled nodes. */
771 : struct streamer_tree_cache_d *reader_cache;
772 :
773 : /* Cache of source code location. */
774 : lto_location_cache location_cache;
775 : };
776 :
777 :
778 : /* In lto-section-in.cc */
779 : extern class lto_input_block * lto_create_simple_input_block (
780 : struct lto_file_decl_data *,
781 : enum lto_section_type, const char **, size_t *);
782 : extern void
783 : lto_destroy_simple_input_block (struct lto_file_decl_data *,
784 : enum lto_section_type,
785 : class lto_input_block *, const char *, size_t);
786 : extern void lto_set_in_hooks (struct lto_file_decl_data **,
787 : lto_get_section_data_f *,
788 : lto_free_section_data_f *);
789 : extern struct lto_file_decl_data **lto_get_file_decl_data (void);
790 : extern const char *lto_get_section_data (struct lto_file_decl_data *,
791 : enum lto_section_type,
792 : const char *, int, size_t *,
793 : bool decompress = false);
794 : extern const char *lto_get_summary_section_data (struct lto_file_decl_data *,
795 : enum lto_section_type,
796 : size_t *);
797 : extern const char *lto_get_raw_section_data (struct lto_file_decl_data *,
798 : enum lto_section_type,
799 : const char *, int, size_t *);
800 : extern void lto_free_section_data (struct lto_file_decl_data *,
801 : enum lto_section_type,
802 : const char *, const char *, size_t,
803 : bool decompress = false);
804 : extern void lto_free_raw_section_data (struct lto_file_decl_data *,
805 : enum lto_section_type,
806 : const char *, const char *, size_t);
807 : extern htab_t lto_create_renaming_table (void);
808 : extern void lto_record_renamed_decl (struct lto_file_decl_data *,
809 : const char *, const char *);
810 : extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
811 : const char *);
812 : extern struct lto_in_decl_state *lto_new_in_decl_state (void);
813 : extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
814 : extern struct lto_in_decl_state *lto_get_function_in_decl_state (
815 : struct lto_file_decl_data *, tree);
816 : extern void lto_free_function_in_decl_state (struct lto_in_decl_state *);
817 : extern void lto_free_function_in_decl_state_for_node (symtab_node *);
818 : extern void lto_section_overrun (class lto_input_block *) ATTRIBUTE_NORETURN;
819 : extern void lto_value_range_error (const char *,
820 : HOST_WIDE_INT, HOST_WIDE_INT,
821 : HOST_WIDE_INT) ATTRIBUTE_NORETURN;
822 :
823 : /* In lto-section-out.cc */
824 : extern void lto_begin_section (const char *, bool);
825 : extern void lto_end_section (void);
826 : extern void lto_write_data (const void *, unsigned int);
827 : extern void lto_write_raw_data (const void *, unsigned int);
828 : extern void lto_write_stream (struct lto_output_stream *);
829 : extern struct lto_simple_output_block *lto_create_simple_output_block (
830 : enum lto_section_type);
831 : extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
832 : extern struct lto_out_decl_state *lto_new_out_decl_state (void);
833 : extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
834 : extern struct lto_out_decl_state *lto_get_out_decl_state (void);
835 : extern void lto_push_out_decl_state (struct lto_out_decl_state *);
836 : extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
837 : extern void lto_record_function_out_decl_state (tree,
838 : struct lto_out_decl_state *);
839 : extern void lto_append_block (struct lto_output_stream *);
840 :
841 :
842 : /* In lto-streamer.cc. */
843 :
844 : /* Set when streaming LTO for offloading compiler. */
845 : extern bool lto_stream_offload_p;
846 :
847 : extern const char *lto_tag_name (enum LTO_tags);
848 : extern char *lto_get_section_name (int, const char *, int,
849 : struct lto_file_decl_data *);
850 : extern void print_lto_report (const char *);
851 : extern void lto_streamer_init (void);
852 : extern bool gate_lto_out (void);
853 : extern void lto_check_version (int, int, const char *);
854 : extern void lto_streamer_hooks_init (void);
855 :
856 : /* In lto-streamer-in.cc */
857 : extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
858 : extern void lto_reader_init (void);
859 : extern void lto_free_file_name_hash (void);
860 : extern void lto_input_function_body (struct lto_file_decl_data *,
861 : struct cgraph_node *,
862 : const char *);
863 : extern void lto_input_variable_constructor (struct lto_file_decl_data *,
864 : struct varpool_node *,
865 : const char *);
866 : extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
867 : const char *);
868 : extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
869 : extern void lto_input_mode_table (struct lto_file_decl_data *);
870 : extern class data_in *lto_data_in_create (struct lto_file_decl_data *,
871 : const char *, unsigned,
872 : vec<ld_plugin_symbol_resolution_t> );
873 : extern void lto_data_in_delete (class data_in *);
874 : extern void lto_input_data_block (class lto_input_block *, void *, size_t);
875 : void lto_input_location (location_t *, struct bitpack_d *, class data_in *);
876 : tree lto_input_tree_ref (class lto_input_block *, class data_in *,
877 : struct function *, enum LTO_tags);
878 : void lto_tag_check_set (enum LTO_tags, int, ...);
879 : void lto_init_eh (void);
880 : hashval_t lto_input_scc (class lto_input_block *, class data_in *,
881 : unsigned *, unsigned *, bool);
882 : tree lto_input_tree_1 (class lto_input_block *, class data_in *,
883 : enum LTO_tags, hashval_t hash);
884 : tree lto_input_tree (class lto_input_block *, class data_in *);
885 : tree stream_read_tree_ref (class lto_input_block *, class data_in *);
886 :
887 :
888 : /* In lto-streamer-out.cc */
889 : extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
890 : extern struct output_block *create_output_block (enum lto_section_type);
891 : extern void destroy_output_block (struct output_block *);
892 : extern void lto_output_tree (struct output_block *, tree, bool, bool);
893 : extern void stream_write_tree_ref (struct output_block *, tree);
894 : extern void lto_output_var_decl_ref (struct lto_out_decl_state *,
895 : struct lto_output_stream *, tree);
896 : extern void lto_output_fn_decl_ref (struct lto_out_decl_state *,
897 : struct lto_output_stream *, tree);
898 : extern tree lto_input_var_decl_ref (lto_input_block *, lto_file_decl_data *);
899 : extern tree lto_input_fn_decl_ref (lto_input_block *, lto_file_decl_data *);
900 : extern void lto_output_toplevel_asms (lto_symtab_encoder_t);
901 : extern void produce_asm (struct output_block *ob);
902 : extern void lto_output ();
903 : extern void produce_asm_for_decls ();
904 : void lto_output_decl_state_streams (struct output_block *,
905 : struct lto_out_decl_state *);
906 : void lto_output_decl_state_refs (struct output_block *,
907 : struct lto_output_stream *,
908 : struct lto_out_decl_state *);
909 : bool lto_variably_modified_type_p (tree);
910 : void lto_output_location (struct output_block *, struct bitpack_d *,
911 : location_t);
912 : void lto_output_location_and_block (struct output_block *, struct bitpack_d *,
913 : location_t);
914 : void lto_output_init_mode_table (void);
915 : void lto_prepare_function_for_streaming (cgraph_node *);
916 :
917 :
918 : /* In lto-cgraph.cc */
919 : lto_symtab_encoder_t lto_symtab_encoder_new (bool);
920 : int lto_symtab_encoder_encode (lto_symtab_encoder_t, toplevel_node *);
921 : void lto_symtab_encoder_delete (lto_symtab_encoder_t);
922 : bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, toplevel_node *);
923 : bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
924 : struct cgraph_node *);
925 : bool lto_symtab_encoder_only_for_inlining_p (lto_symtab_encoder_t,
926 : struct cgraph_node *);
927 : bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
928 : toplevel_node *);
929 : void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
930 : toplevel_node *);
931 :
932 : bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
933 : varpool_node *);
934 : void output_symtab (void);
935 : void input_symtab (void);
936 : void input_toplevel_asms (void);
937 : void output_offload_tables (void);
938 : void input_offload_tables (bool);
939 : bool referenced_from_other_partition_p (struct ipa_ref_list *,
940 : lto_symtab_encoder_t);
941 : bool reachable_from_other_partition_p (struct cgraph_node *,
942 : lto_symtab_encoder_t);
943 : bool referenced_from_this_partition_p (symtab_node *,
944 : lto_symtab_encoder_t);
945 : bool reachable_from_this_partition_p (struct cgraph_node *,
946 : lto_symtab_encoder_t);
947 : lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
948 : void select_what_to_stream (void);
949 :
950 : /* In options-save.cc. */
951 : void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
952 : struct cl_target_option *);
953 :
954 : void cl_target_option_stream_in (class data_in *,
955 : struct bitpack_d *,
956 : struct cl_target_option *);
957 :
958 : void cl_optimization_stream_out (struct output_block *,
959 : struct bitpack_d *, struct cl_optimization *);
960 :
961 : void cl_optimization_stream_in (class data_in *,
962 : struct bitpack_d *, struct cl_optimization *);
963 :
964 :
965 :
966 : /* In lto-opts.cc. */
967 : extern void lto_write_options (void);
968 :
969 :
970 : /* Statistics gathered during LTO, WPA and LTRANS. */
971 : extern struct lto_stats_d lto_stats;
972 :
973 : /* Section names corresponding to the values of enum lto_section_type. */
974 : extern const char *lto_section_name[];
975 :
976 : /* Holds all the out decl states of functions output so far in the
977 : current output file. */
978 : extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
979 :
980 : /* Return true if LTO tag TAG corresponds to a tree code. */
981 : inline bool
982 4344233 : lto_tag_is_tree_code_p (enum LTO_tags tag)
983 : {
984 4344233 : return tag > LTO_first_tree_tag && (unsigned) tag <= MAX_TREE_CODES;
985 : }
986 :
987 :
988 : /* Return true if LTO tag TAG corresponds to a gimple code. */
989 : inline bool
990 1402886 : lto_tag_is_gimple_code_p (enum LTO_tags tag)
991 : {
992 1402886 : return (unsigned) tag >= LTO_first_gimple_tag
993 1402886 : && (unsigned) tag
994 : < (unsigned) LTO_first_gimple_tag + LAST_AND_UNUSED_GIMPLE_CODE;
995 : }
996 :
997 :
998 : /* Return the LTO tag corresponding to gimple code CODE. See enum
999 : LTO_tags for details on the conversion. */
1000 : inline enum LTO_tags
1001 1684990 : lto_gimple_code_to_tag (enum gimple_code code)
1002 : {
1003 1684990 : return (enum LTO_tags) ((unsigned) code + LTO_first_gimple_tag);
1004 : }
1005 :
1006 :
1007 : /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
1008 : details on the conversion. */
1009 : inline enum gimple_code
1010 1402886 : lto_tag_to_gimple_code (enum LTO_tags tag)
1011 : {
1012 1402886 : gcc_assert (lto_tag_is_gimple_code_p (tag));
1013 1402886 : return (enum gimple_code) ((unsigned) tag - LTO_first_gimple_tag);
1014 : }
1015 :
1016 :
1017 : /* Return the LTO tag corresponding to tree code CODE. See enum
1018 : LTO_tags for details on the conversion. */
1019 : inline enum LTO_tags
1020 7057289 : lto_tree_code_to_tag (enum tree_code code)
1021 : {
1022 7057289 : return (enum LTO_tags) ((unsigned) code + LTO_first_tree_tag);
1023 : }
1024 :
1025 :
1026 : /* Return the tree code corresponding to TAG. See enum LTO_tags for
1027 : details on the conversion. */
1028 : inline enum tree_code
1029 4344233 : lto_tag_to_tree_code (enum LTO_tags tag)
1030 : {
1031 4344233 : gcc_assert (lto_tag_is_tree_code_p (tag));
1032 4344233 : return (enum tree_code) ((unsigned) tag - LTO_first_tree_tag);
1033 : }
1034 :
1035 : /* Check that tag ACTUAL == EXPECTED. */
1036 : inline void
1037 83520 : lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
1038 : {
1039 83520 : if (actual != expected)
1040 0 : internal_error ("bytecode stream: expected tag %s instead of %s",
1041 : lto_tag_name (expected), lto_tag_name (actual));
1042 83520 : }
1043 :
1044 : /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
1045 : inline void
1046 10113 : lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
1047 : enum LTO_tags tag2)
1048 : {
1049 10113 : if (actual < tag1 || actual > tag2)
1050 0 : internal_error ("bytecode stream: tag %s is not in the expected range "
1051 : "[%s, %s]",
1052 : lto_tag_name (actual),
1053 : lto_tag_name (tag1),
1054 : lto_tag_name (tag2));
1055 10113 : }
1056 :
1057 : /* Initialize an lto_out_decl_buffer ENCODER. */
1058 : inline void
1059 183657 : lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1060 : {
1061 183657 : encoder->tree_hash_table = new hash_map<tree, unsigned> (251);
1062 183657 : encoder->trees.create (0);
1063 183657 : }
1064 :
1065 :
1066 : /* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
1067 : memory used by ENCODER is not freed by this function. */
1068 : inline void
1069 183657 : lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1070 : {
1071 : /* Hash table may be delete already. */
1072 183657 : delete encoder->tree_hash_table;
1073 183657 : encoder->tree_hash_table = NULL;
1074 183657 : encoder->trees.release ();
1075 183657 : }
1076 :
1077 : /* Return the number of trees encoded in ENCODER. */
1078 : inline unsigned int
1079 585005 : lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1080 : {
1081 1133234 : return encoder->trees.length ();
1082 : }
1083 :
1084 : /* Return the IDX-th tree in ENCODER. */
1085 : inline tree
1086 5264142 : lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1087 : unsigned int idx)
1088 : {
1089 5264142 : return encoder->trees[idx];
1090 : }
1091 :
1092 : /* Return number of encoded nodes in ENCODER. */
1093 : inline int
1094 20118178 : lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1095 : {
1096 32044954 : return encoder->nodes.length ();
1097 : }
1098 :
1099 : /* Value used to represent failure of lto_symtab_encoder_lookup. */
1100 : #define LCC_NOT_FOUND (-1)
1101 :
1102 : /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1103 : or LCC_NOT_FOUND if it is not there. */
1104 :
1105 : inline int
1106 12614679 : lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1107 : toplevel_node *node)
1108 : {
1109 12614679 : size_t *slot = encoder->map->get (node);
1110 12614679 : return (slot && *slot ? *(slot) - 1 : LCC_NOT_FOUND);
1111 : }
1112 :
1113 : /* Return true if iterator LSE points to nothing. */
1114 : inline bool
1115 14816317 : lsei_end_p (lto_symtab_encoder_iterator lsei)
1116 : {
1117 6709342 : return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1118 : }
1119 :
1120 : /* Advance iterator LSE. */
1121 : inline void
1122 11878715 : lsei_next (lto_symtab_encoder_iterator *lsei)
1123 : {
1124 11878715 : lsei->index++;
1125 11124754 : }
1126 :
1127 : /* Return the node pointed to by LSI. */
1128 : inline toplevel_node *
1129 16657587 : lsei_node (lto_symtab_encoder_iterator lsei)
1130 : {
1131 12250663 : return lsei.encoder->nodes[lsei.index].node;
1132 : }
1133 :
1134 : /* Return the node pointed to by LSI. */
1135 : inline struct cgraph_node *
1136 1230446 : lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1137 : {
1138 1230446 : return dyn_cast<cgraph_node *> (lsei.encoder->nodes[lsei.index].node);
1139 : }
1140 :
1141 : /* Return the node pointed to by LSI. */
1142 : inline varpool_node *
1143 280977 : lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1144 : {
1145 280977 : return dyn_cast<varpool_node *> (lsei.encoder->nodes[lsei.index].node);
1146 : }
1147 :
1148 : /* Return the cgraph node corresponding to REF using ENCODER. */
1149 :
1150 : inline toplevel_node *
1151 7814866 : lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1152 : {
1153 7813094 : if (ref == LCC_NOT_FOUND)
1154 : return NULL;
1155 :
1156 7814866 : return encoder->nodes[ref].node;
1157 : }
1158 :
1159 : /* Return an iterator to the first node in LSI. */
1160 : inline lto_symtab_encoder_iterator
1161 : lsei_start (lto_symtab_encoder_t encoder)
1162 : {
1163 : lto_symtab_encoder_iterator lsei;
1164 :
1165 : lsei.encoder = encoder;
1166 : lsei.index = 0;
1167 : return lsei;
1168 : }
1169 :
1170 : /* Advance iterator LSE. */
1171 : inline void
1172 694931 : lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1173 : {
1174 694931 : lsei_next (lsei);
1175 694931 : while (!lsei_end_p (*lsei)
1176 2278965 : && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1177 464128 : lsei_next (lsei);
1178 694931 : }
1179 :
1180 : /* Return an iterator to the first node in LSI. */
1181 : inline lto_symtab_encoder_iterator
1182 39541 : lsei_start_in_partition (lto_symtab_encoder_t encoder)
1183 : {
1184 39541 : lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1185 :
1186 39541 : if (lsei_end_p (lsei))
1187 388 : return lsei;
1188 39153 : if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1189 0 : lsei_next_in_partition (&lsei);
1190 :
1191 39153 : return lsei;
1192 : }
1193 :
1194 : /* Advance iterator LSE. */
1195 : inline void
1196 1234535 : lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1197 : {
1198 1234535 : lsei_next (lsei);
1199 1234535 : while (!lsei_end_p (*lsei)
1200 16993654 : && (!is_a <cgraph_node *> (lsei_node (*lsei))
1201 3516215 : || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1202 5630605 : lsei_next (lsei);
1203 1234535 : }
1204 :
1205 : /* Return an iterator to the first node in LSI. */
1206 : inline lto_symtab_encoder_iterator
1207 274120 : lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1208 : {
1209 274120 : lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1210 :
1211 274120 : if (lsei_end_p (lsei))
1212 2557 : return lsei;
1213 539590 : if (!is_a <cgraph_node *> (lsei_node (lsei))
1214 268027 : || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1215 4089 : lsei_next_function_in_partition (&lsei);
1216 :
1217 271563 : return lsei;
1218 : }
1219 :
1220 : /* Advance iterator LSE. */
1221 : inline void
1222 311302 : lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1223 : {
1224 311302 : lsei_next (lsei);
1225 311302 : while (!lsei_end_p (*lsei)
1226 1148770 : && (!is_a <varpool_node *> (lsei_node (*lsei))
1227 280313 : || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1228 138421 : lsei_next (lsei);
1229 311302 : }
1230 :
1231 : /* Return an iterator to the first node in LSI. */
1232 : inline lto_symtab_encoder_iterator
1233 31226 : lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1234 : {
1235 31226 : lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1236 :
1237 31226 : if (lsei_end_p (lsei))
1238 237 : return lsei;
1239 31653 : if (!is_a <varpool_node *> (lsei_node (lsei))
1240 664 : || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1241 30325 : lsei_next_variable_in_partition (&lsei);
1242 :
1243 30989 : return lsei;
1244 : }
1245 :
1246 : /* Entry for the delayed registering of decl -> DIE references. */
1247 : struct dref_entry {
1248 : tree decl;
1249 : const char *sym;
1250 : unsigned HOST_WIDE_INT off;
1251 : };
1252 :
1253 : extern vec<dref_entry> dref_queue;
1254 :
1255 : extern FILE *streamer_dump_file;
1256 :
1257 : #endif /* GCC_LTO_STREAMER_H */
|