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