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