]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto-streamer.h
2013-06-06 Richard Biener <rguenther@suse.de>
[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
37 /* Define when debugging the LTO streamer. This causes the writer
38 to output the numeric value for the memory address of the tree node
39 being emitted. When debugging a problem in the reader, check the
40 original address that the writer was emitting using lto_orig_address_get.
41 With this value, set a breakpoint in the writer (e.g., lto_output_tree)
42 to trace how the faulty node is being emitted. */
43 /* #define LTO_STREAMER_DEBUG 1 */
44
45 /* The encoding for a function consists of the following sections:
46
47 1) The header.
48 2) FIELD_DECLS.
49 3) FUNCTION_DECLS.
50 4) global VAR_DECLS.
51 5) type_decls
52 6) types.
53 7) Names for the labels that have names
54 8) The SSA names.
55 9) The control flow graph.
56 10-11)Gimple for local decls.
57 12) Gimple for the function.
58 13) Strings.
59
60 1) THE HEADER.
61 2-6) THE GLOBAL DECLS AND TYPES.
62
63 The global decls and types are encoded in the same way. For each
64 entry, there is word with the offset within the section to the
65 entry.
66
67 7) THE LABEL NAMES.
68
69 Since most labels do not have names, this section my be of zero
70 length. It consists of an array of string table references, one
71 per label. In the lto code, the labels are given either
72 positive or negative indexes. the positive ones have names and
73 the negative ones do not. The positive index can be used to
74 find the name in this array.
75
76 9) THE CFG.
77
78 10) Index into the local decls. Since local decls can have local
79 decls inside them, they must be read in randomly in order to
80 properly restore them.
81
82 11-12) GIMPLE FOR THE LOCAL DECLS AND THE FUNCTION BODY.
83
84 The gimple consists of a set of records.
85
86 THE FUNCTION
87
88 At the top level of (8) is the function. It consists of five
89 pieces:
90
91 LTO_function - The tag.
92 eh tree - This is all of the exception handling regions
93 put out in a post order traversial of the
94 tree. Siblings are output as lists terminated
95 by a 0. The set of fields matches the fields
96 defined in except.c.
97
98 last_basic_block - in uleb128 form.
99
100 basic blocks - This is the set of basic blocks.
101
102 zero - The termination of the basic blocks.
103
104 BASIC BLOCKS
105
106 There are two forms of basic blocks depending on if they are
107 empty or not.
108
109 The basic block consists of:
110
111 LTO_bb1 or LTO_bb0 - The tag.
112
113 bb->index - the index in uleb128 form.
114
115 #succs - The number of successors un uleb128 form.
116
117 the successors - For each edge, a pair. The first of the
118 pair is the index of the successor in
119 uleb128 form and the second are the flags in
120 uleb128 form.
121
122 the statements - A gimple tree, as described above.
123 These are only present for LTO_BB1.
124 Following each statement is an optional
125 exception handling record LTO_eh_region
126 which contains the region number (for
127 regions >= 0).
128
129 zero - This is only present for LTO_BB1 and is used
130 to terminate the statements and exception
131 regions within this block.
132
133 12) STRINGS
134
135 String are represented in the table as pairs, a length in ULEB128
136 form followed by the data for the string. */
137
138 /* The string that is the prefix on the section names we make for lto.
139 For decls the DECL_ASSEMBLER_NAME is appended to make the section
140 name for the functions and static_initializers. For other types of
141 sections a '.' and the section type are appended. */
142 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
143
144 #define LTO_major_version 2
145 #define LTO_minor_version 2
146
147 typedef unsigned char lto_decl_flags_t;
148
149
150 /* Tags representing the various IL objects written to the bytecode file
151 (GIMPLE statements, basic blocks, EH regions, tree nodes, etc).
152
153 NOTE, when adding new LTO tags, also update lto_tag_name. */
154 enum LTO_tags
155 {
156 LTO_null = 0,
157
158 /* Special for streamer. Reference to previously-streamed node. */
159 LTO_tree_pickle_reference,
160
161 /* Reserve enough entries to fit all the tree and gimple codes handled
162 by the streamer. This guarantees that:
163
164 1- Given a tree code C:
165 enum LTO_tags tag == C + 1
166
167 2- Given a gimple code C:
168 enum LTO_tags tag == C + NUM_TREE_CODES + 1
169
170 Conversely, to map between LTO tags and tree/gimple codes, the
171 reverse operation must be applied. */
172 LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
173 LTO_bb1,
174
175 /* EH region holding the previous statement. */
176 LTO_eh_region,
177
178 /* An MD or NORMAL builtin. Only the code and class are streamed out. */
179 LTO_builtin_decl,
180
181 /* Shared INTEGER_CST node. */
182 LTO_integer_cst,
183
184 /* Function body. */
185 LTO_function,
186
187 /* EH table. */
188 LTO_eh_table,
189
190 /* EH region types. These mirror enum eh_region_type. */
191 LTO_ert_cleanup,
192 LTO_ert_try,
193 LTO_ert_allowed_exceptions,
194 LTO_ert_must_not_throw,
195
196 /* EH landing pad. */
197 LTO_eh_landing_pad,
198
199 /* EH try/catch node. */
200 LTO_eh_catch,
201
202 /* References to indexable tree nodes. These objects are stored in
203 tables that are written separately from the function bodies that
204 reference them. This way they can be instantiated even when the
205 referencing functions aren't (e.g., during WPA) and it also allows
206 functions to be copied from one file to another without having
207 to unpickle the body first (the references are location
208 independent).
209
210 NOTE, do not regroup these values as the grouping is exposed
211 in the range checks done in lto_input_tree. */
212 LTO_field_decl_ref, /* Do not change. */
213 LTO_function_decl_ref,
214 LTO_label_decl_ref,
215 LTO_namespace_decl_ref,
216 LTO_result_decl_ref,
217 LTO_ssa_name_ref,
218 LTO_type_decl_ref,
219 LTO_type_ref,
220 LTO_const_decl_ref,
221 LTO_imported_decl_ref,
222 LTO_translation_unit_decl_ref,
223 LTO_global_decl_ref, /* Do not change. */
224
225 /* This tag must always be last. */
226 LTO_NUM_TAGS
227 };
228
229
230 /* Set of section types that are in an LTO file. This list will grow
231 as the number of IPA passes grows since each IPA pass will need its
232 own section type to store its summary information.
233
234 When adding a new section type, you must also extend the
235 LTO_SECTION_NAME array in lto-section-in.c. */
236 enum lto_section_type
237 {
238 LTO_section_decls = 0,
239 LTO_section_function_body,
240 LTO_section_static_initializer,
241 LTO_section_symtab,
242 LTO_section_refs,
243 LTO_section_asm,
244 LTO_section_jump_functions,
245 LTO_section_ipa_pure_const,
246 LTO_section_ipa_reference,
247 LTO_section_ipa_profile,
248 LTO_section_symtab_nodes,
249 LTO_section_opts,
250 LTO_section_cgraph_opt_sum,
251 LTO_section_inline_summary,
252 LTO_section_ipcp_transform,
253 LTO_N_SECTION_TYPES /* Must be last. */
254 };
255
256 /* Indices to the various function, type and symbol streams. */
257 typedef enum
258 {
259 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
260 LTO_DECL_STREAM_FIELD_DECL,
261 LTO_DECL_STREAM_FN_DECL,
262 LTO_DECL_STREAM_VAR_DECL,
263 LTO_DECL_STREAM_TYPE_DECL,
264 LTO_DECL_STREAM_NAMESPACE_DECL,
265 LTO_DECL_STREAM_LABEL_DECL,
266 LTO_N_DECL_STREAMS
267 } lto_decl_stream_e_t;
268
269 typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
270
271
272 /* Macro to define convenience functions for type and decl streams
273 in lto_file_decl_data. */
274 #define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
275 static inline tree \
276 lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
277 unsigned int idx) \
278 { \
279 struct lto_in_decl_state *state = data->current_decl_state; \
280 gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
281 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
282 } \
283 \
284 static inline unsigned int \
285 lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
286 { \
287 struct lto_in_decl_state *state = data->current_decl_state; \
288 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
289 }
290
291
292 /* Return a char pointer to the start of a data stream for an lto pass
293 or function. The first parameter is the file data that contains
294 the information. The second parameter is the type of information
295 to be obtained. The third parameter is the name of the function
296 and is only used when finding a function body; otherwise it is
297 NULL. The fourth parameter is the length of the data returned. */
298 typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
299 enum lto_section_type,
300 const char *,
301 size_t *);
302
303 /* Return the data found from the above call. The first three
304 parameters are the same as above. The fourth parameter is the data
305 itself and the fifth is the length of the data. */
306 typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
307 enum lto_section_type,
308 const char *,
309 const char *,
310 size_t);
311
312 /* Structure used as buffer for reading an LTO file. */
313 struct lto_input_block
314 {
315 const char *data;
316 unsigned int p;
317 unsigned int len;
318 };
319
320 #define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
321 do { \
322 BASE.data = D; \
323 BASE.p = P; \
324 BASE.len = L; \
325 } while (0)
326
327 #define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
328 do { \
329 BASE->data = D; \
330 BASE->p = P; \
331 BASE->len = L; \
332 } while (0)
333
334
335 /* The is the first part of the record for a function or constructor
336 in the .o file. */
337 struct lto_header
338 {
339 int16_t major_version;
340 int16_t minor_version;
341 };
342
343 /* The header for a function body. */
344 struct lto_function_header
345 {
346 /* The header for all types of sections. */
347 struct lto_header lto_header;
348
349 /* Number of labels with names. */
350 int32_t num_named_labels;
351
352 /* Number of labels without names. */
353 int32_t num_unnamed_labels;
354
355 /* Size compressed or 0 if not compressed. */
356 int32_t compressed_size;
357
358 /* Size of names for named labels. */
359 int32_t named_label_size;
360
361 /* Size of the cfg. */
362 int32_t cfg_size;
363
364 /* Size of main gimple body of function. */
365 int32_t main_size;
366
367 /* Size of the string table. */
368 int32_t string_size;
369 };
370
371
372 /* Structure describing a symbol section. */
373 struct lto_decl_header
374 {
375 /* The header for all types of sections. */
376 struct lto_header lto_header;
377
378 /* Size of region for decl state. */
379 int32_t decl_state_size;
380
381 /* Number of nodes in globals stream. */
382 int32_t num_nodes;
383
384 /* Size of region for expressions, decls, types, etc. */
385 int32_t main_size;
386
387 /* Size of the string table. */
388 int32_t string_size;
389 };
390
391
392 /* Structure describing top level asm()s. */
393 struct lto_asm_header
394 {
395 /* The header for all types of sections. */
396 struct lto_header lto_header;
397
398 /* Size compressed or 0 if not compressed. */
399 int32_t compressed_size;
400
401 /* Size of region for expressions, decls, types, etc. */
402 int32_t main_size;
403
404 /* Size of the string table. */
405 int32_t string_size;
406 };
407
408
409 /* Statistics gathered during LTO, WPA and LTRANS. */
410 struct lto_stats_d
411 {
412 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
413 unsigned HOST_WIDE_INT num_output_symtab_nodes;
414 unsigned HOST_WIDE_INT num_input_files;
415 unsigned HOST_WIDE_INT num_output_files;
416 unsigned HOST_WIDE_INT num_cgraph_partitions;
417 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
418 unsigned HOST_WIDE_INT num_function_bodies;
419 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
420 unsigned HOST_WIDE_INT num_output_il_bytes;
421 unsigned HOST_WIDE_INT num_compressed_il_bytes;
422 unsigned HOST_WIDE_INT num_input_il_bytes;
423 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
424 };
425
426 /* Entry of LTO symtab encoder. */
427 typedef struct
428 {
429 symtab_node node;
430 /* Is the node in this partition (i.e. ltrans of this partition will
431 be responsible for outputting it)? */
432 unsigned int in_partition:1;
433 /* Do we encode body in this partition? */
434 unsigned int body:1;
435 /* Do we encode initializer in this partition?
436 For example the readonly variable initializers are encoded to aid
437 constant folding even if they are not in the partition. */
438 unsigned int initializer:1;
439 } lto_encoder_entry;
440
441
442 /* Encoder data structure used to stream callgraph nodes. */
443 struct lto_symtab_encoder_d
444 {
445 vec<lto_encoder_entry> nodes;
446 pointer_map_t *map;
447 };
448
449 typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
450
451 /* Iterator structure for cgraph node sets. */
452 typedef struct
453 {
454 lto_symtab_encoder_t encoder;
455 unsigned index;
456 } lto_symtab_encoder_iterator;
457
458
459
460
461 /* Mapping from indices to trees. */
462 struct GTY(()) lto_tree_ref_table
463 {
464 /* Array of referenced trees . */
465 tree * GTY((length ("%h.size"))) trees;
466
467 /* Size of array. */
468 unsigned int size;
469 };
470
471
472 /* Mapping between trees and slots in an array. */
473 struct lto_decl_slot
474 {
475 tree t;
476 int slot_num;
477 };
478
479
480 /* The lto_tree_ref_encoder struct is used to encode trees into indices. */
481
482 struct lto_tree_ref_encoder
483 {
484 htab_t tree_hash_table; /* Maps pointers to indices. */
485 unsigned int next_index; /* Next available index. */
486 vec<tree> trees; /* Maps indices to pointers. */
487 };
488
489
490 /* Structure to hold states of input scope. */
491 struct GTY(()) lto_in_decl_state
492 {
493 /* Array of lto_in_decl_buffers to store type and decls streams. */
494 struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
495
496 /* If this in-decl state is associated with a function. FN_DECL
497 point to the FUNCTION_DECL. */
498 tree fn_decl;
499 };
500
501 typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
502
503
504 /* The structure that holds all of the vectors of global types,
505 decls and cgraph nodes used in the serialization of this file. */
506 struct lto_out_decl_state
507 {
508 /* The buffers contain the sets of decls of various kinds and types we have
509 seen so far and the indexes assigned to them. */
510 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
511
512 /* Encoder for cgraph nodes. */
513 lto_symtab_encoder_t symtab_node_encoder;
514
515 /* If this out-decl state belongs to a function, fn_decl points to that
516 function. Otherwise, it is NULL. */
517 tree fn_decl;
518 };
519
520 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
521
522
523 /* Compact representation of a index <-> resolution pair. Unpacked to an
524 vector later. */
525 struct res_pair
526 {
527 ld_plugin_symbol_resolution_t res;
528 unsigned index;
529 };
530 typedef struct res_pair res_pair;
531
532
533 /* One of these is allocated for each object file that being compiled
534 by lto. This structure contains the tables that are needed by the
535 serialized functions and ipa passes to connect themselves to the
536 global types and decls as they are reconstituted. */
537 struct GTY(()) lto_file_decl_data
538 {
539 /* Decl state currently used. */
540 struct lto_in_decl_state *current_decl_state;
541
542 /* Decl state corresponding to regions outside of any functions
543 in the compilation unit. */
544 struct lto_in_decl_state *global_decl_state;
545
546 /* Table of cgraph nodes present in this file. */
547 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
548
549 /* Hash table maps lto-related section names to location in file. */
550 htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
551
552 /* The .o file that these offsets relate to. */
553 const char *GTY((skip)) file_name;
554
555 /* Hash table maps lto-related section names to location in file. */
556 htab_t GTY((skip)) section_hash_table;
557
558 /* Hash new name of renamed global declaration to its original name. */
559 htab_t GTY((skip)) renaming_hash_table;
560
561 /* Linked list used temporarily in reader */
562 struct lto_file_decl_data *next;
563
564 /* Sub ID for merged objects. */
565 unsigned HOST_WIDE_INT id;
566
567 /* Symbol resolutions for this file */
568 vec<res_pair> GTY((skip)) respairs;
569 unsigned max_index;
570
571 struct gcov_ctr_summary GTY((skip)) profile_info;
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_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
778 extern void lto_value_range_error (const char *,
779 HOST_WIDE_INT, HOST_WIDE_INT,
780 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
781
782 /* In lto-section-out.c */
783 extern hashval_t lto_hash_decl_slot_node (const void *);
784 extern int lto_eq_decl_slot_node (const void *, const void *);
785 extern hashval_t lto_hash_type_slot_node (const void *);
786 extern int lto_eq_type_slot_node (const void *, const void *);
787 extern void lto_begin_section (const char *, bool);
788 extern void lto_end_section (void);
789 extern void lto_write_stream (struct lto_output_stream *);
790 extern void lto_output_data_stream (struct lto_output_stream *, const void *,
791 size_t);
792 extern bool lto_output_decl_index (struct lto_output_stream *,
793 struct lto_tree_ref_encoder *,
794 tree, unsigned int *);
795 extern void lto_output_field_decl_index (struct lto_out_decl_state *,
796 struct lto_output_stream *, tree);
797 extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
798 struct lto_output_stream *, tree);
799 extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
800 struct lto_output_stream *, tree);
801 extern void lto_output_var_decl_index (struct lto_out_decl_state *,
802 struct lto_output_stream *, tree);
803 extern void lto_output_type_decl_index (struct lto_out_decl_state *,
804 struct lto_output_stream *, tree);
805 extern void lto_output_type_ref_index (struct lto_out_decl_state *,
806 struct lto_output_stream *, tree);
807 extern struct lto_simple_output_block *lto_create_simple_output_block (
808 enum lto_section_type);
809 extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
810 extern struct lto_out_decl_state *lto_new_out_decl_state (void);
811 extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
812 extern struct lto_out_decl_state *lto_get_out_decl_state (void);
813 extern void lto_push_out_decl_state (struct lto_out_decl_state *);
814 extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
815 extern void lto_record_function_out_decl_state (tree,
816 struct lto_out_decl_state *);
817 extern void lto_append_block (struct lto_output_stream *);
818
819
820 /* In lto-streamer.c. */
821 extern const char *lto_tag_name (enum LTO_tags);
822 extern bitmap lto_bitmap_alloc (void);
823 extern void lto_bitmap_free (bitmap);
824 extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
825 extern void print_lto_report (const char *);
826 extern void lto_streamer_init (void);
827 extern bool gate_lto_out (void);
828 #ifdef LTO_STREAMER_DEBUG
829 extern void lto_orig_address_map (tree, intptr_t);
830 extern intptr_t lto_orig_address_get (tree);
831 extern void lto_orig_address_remove (tree);
832 #endif
833 extern void lto_check_version (int, int);
834 extern void lto_streamer_hooks_init (void);
835
836 /* In lto-streamer-in.c */
837 extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
838 extern void lto_reader_init (void);
839 extern void lto_input_function_body (struct lto_file_decl_data *, tree,
840 const char *);
841 extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
842 const char *);
843 extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
844 extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
845 const char *, unsigned,
846 vec<ld_plugin_symbol_resolution_t> );
847 extern void lto_data_in_delete (struct data_in *);
848 extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
849 location_t lto_input_location (struct bitpack_d *, struct data_in *);
850 tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
851 struct function *, enum LTO_tags);
852 void lto_tag_check_set (enum LTO_tags, int, ...);
853 void lto_init_eh (void);
854 tree lto_input_tree (struct lto_input_block *, struct data_in *);
855
856
857 /* In lto-streamer-out.c */
858 extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
859 extern struct output_block *create_output_block (enum lto_section_type);
860 extern void destroy_output_block (struct output_block *);
861 extern void lto_output_tree (struct output_block *, tree, bool, bool);
862 extern void lto_output_toplevel_asms (void);
863 extern void produce_asm (struct output_block *ob, tree fn);
864 void lto_output_decl_state_streams (struct output_block *,
865 struct lto_out_decl_state *);
866 void lto_output_decl_state_refs (struct output_block *,
867 struct lto_output_stream *,
868 struct lto_out_decl_state *);
869 void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
870
871
872 /* In lto-cgraph.c */
873 lto_symtab_encoder_t lto_symtab_encoder_new (bool);
874 int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node);
875 void lto_symtab_encoder_delete (lto_symtab_encoder_t);
876 bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node);
877 bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
878 struct cgraph_node *);
879 bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
880 symtab_node);
881 void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
882 symtab_node);
883
884 bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
885 struct varpool_node *);
886 void output_symtab (void);
887 void input_symtab (void);
888 bool referenced_from_other_partition_p (struct ipa_ref_list *,
889 lto_symtab_encoder_t);
890 bool reachable_from_other_partition_p (struct cgraph_node *,
891 lto_symtab_encoder_t);
892 bool referenced_from_this_partition_p (struct ipa_ref_list *,
893 lto_symtab_encoder_t);
894 bool reachable_from_this_partition_p (struct cgraph_node *,
895 lto_symtab_encoder_t);
896 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
897
898
899 /* In lto-symtab.c. */
900 extern void lto_symtab_merge_decls (void);
901 extern void lto_symtab_merge_symbols (void);
902 extern tree lto_symtab_prevailing_decl (tree decl);
903 extern GTY(()) vec<tree, va_gc> *lto_global_var_decls;
904
905
906 /* In lto-opts.c. */
907 extern void lto_write_options (void);
908
909
910 /* Statistics gathered during LTO, WPA and LTRANS. */
911 extern struct lto_stats_d lto_stats;
912
913 /* Section names corresponding to the values of enum lto_section_type. */
914 extern const char *lto_section_name[];
915
916 /* Holds all the out decl states of functions output so far in the
917 current output file. */
918 extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
919
920 /* Return true if LTO tag TAG corresponds to a tree code. */
921 static inline bool
922 lto_tag_is_tree_code_p (enum LTO_tags tag)
923 {
924 return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
925 }
926
927
928 /* Return true if LTO tag TAG corresponds to a gimple code. */
929 static inline bool
930 lto_tag_is_gimple_code_p (enum LTO_tags tag)
931 {
932 return (unsigned) tag >= NUM_TREE_CODES + 2
933 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
934 }
935
936
937 /* Return the LTO tag corresponding to gimple code CODE. See enum
938 LTO_tags for details on the conversion. */
939 static inline enum LTO_tags
940 lto_gimple_code_to_tag (enum gimple_code code)
941 {
942 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
943 }
944
945
946 /* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
947 details on the conversion. */
948 static inline enum gimple_code
949 lto_tag_to_gimple_code (enum LTO_tags tag)
950 {
951 gcc_assert (lto_tag_is_gimple_code_p (tag));
952 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
953 }
954
955
956 /* Return the LTO tag corresponding to tree code CODE. See enum
957 LTO_tags for details on the conversion. */
958 static inline enum LTO_tags
959 lto_tree_code_to_tag (enum tree_code code)
960 {
961 return (enum LTO_tags) ((unsigned) code + 2);
962 }
963
964
965 /* Return the tree code corresponding to TAG. See enum LTO_tags for
966 details on the conversion. */
967 static inline enum tree_code
968 lto_tag_to_tree_code (enum LTO_tags tag)
969 {
970 gcc_assert (lto_tag_is_tree_code_p (tag));
971 return (enum tree_code) ((unsigned) tag - 2);
972 }
973
974 /* Check that tag ACTUAL == EXPECTED. */
975 static inline void
976 lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
977 {
978 if (actual != expected)
979 internal_error ("bytecode stream: expected tag %s instead of %s",
980 lto_tag_name (expected), lto_tag_name (actual));
981 }
982
983 /* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
984 static inline void
985 lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
986 enum LTO_tags tag2)
987 {
988 if (actual < tag1 || actual > tag2)
989 internal_error ("bytecode stream: tag %s is not in the expected range "
990 "[%s, %s]",
991 lto_tag_name (actual),
992 lto_tag_name (tag1),
993 lto_tag_name (tag2));
994 }
995
996 /* Initialize an lto_out_decl_buffer ENCODER. */
997 static inline void
998 lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder,
999 htab_hash hash_fn, htab_eq eq_fn)
1000 {
1001 encoder->tree_hash_table = htab_create (37, hash_fn, eq_fn, free);
1002 encoder->next_index = 0;
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 htab_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
1034 /* Return true if LABEL should be emitted in the global context. */
1035 static inline bool
1036 emit_label_in_global_context_p (tree label)
1037 {
1038 return DECL_NONLOCAL (label) || FORCED_LABEL (label);
1039 }
1040
1041 /* Return number of encoded nodes in ENCODER. */
1042 static inline int
1043 lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1044 {
1045 return encoder->nodes.length ();
1046 }
1047
1048 /* Value used to represent failure of lto_symtab_encoder_lookup. */
1049 #define LCC_NOT_FOUND (-1)
1050
1051 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
1052 or LCC_NOT_FOUND if it is not there. */
1053
1054 static inline int
1055 lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1056 symtab_node node)
1057 {
1058 void **slot = pointer_map_contains (encoder->map, node);
1059 return (slot && *slot ? (size_t) *(slot) - 1 : LCC_NOT_FOUND);
1060 }
1061
1062 /* Return true if iterator LSE points to nothing. */
1063 static inline bool
1064 lsei_end_p (lto_symtab_encoder_iterator lsei)
1065 {
1066 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1067 }
1068
1069 /* Advance iterator LSE. */
1070 static inline void
1071 lsei_next (lto_symtab_encoder_iterator *lsei)
1072 {
1073 lsei->index++;
1074 }
1075
1076 /* Return the node pointed to by LSI. */
1077 static inline symtab_node
1078 lsei_node (lto_symtab_encoder_iterator lsei)
1079 {
1080 return lsei.encoder->nodes[lsei.index].node;
1081 }
1082
1083 /* Return the node pointed to by LSI. */
1084 static inline struct cgraph_node *
1085 lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1086 {
1087 return cgraph (lsei.encoder->nodes[lsei.index].node);
1088 }
1089
1090 /* Return the node pointed to by LSI. */
1091 static inline struct varpool_node *
1092 lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1093 {
1094 return varpool (lsei.encoder->nodes[lsei.index].node);
1095 }
1096
1097 /* Return the cgraph node corresponding to REF using ENCODER. */
1098
1099 static inline symtab_node
1100 lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1101 {
1102 if (ref == LCC_NOT_FOUND)
1103 return NULL;
1104
1105 return encoder->nodes[ref].node;
1106 }
1107
1108 /* Return an iterator to the first node in LSI. */
1109 static inline lto_symtab_encoder_iterator
1110 lsei_start (lto_symtab_encoder_t encoder)
1111 {
1112 lto_symtab_encoder_iterator lsei;
1113
1114 lsei.encoder = encoder;
1115 lsei.index = 0;
1116 return lsei;
1117 }
1118
1119 /* Advance iterator LSE. */
1120 static inline void
1121 lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1122 {
1123 lsei_next (lsei);
1124 while (!lsei_end_p (*lsei)
1125 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1126 lsei_next (lsei);
1127 }
1128
1129 /* Return an iterator to the first node in LSI. */
1130 static inline lto_symtab_encoder_iterator
1131 lsei_start_in_partition (lto_symtab_encoder_t encoder)
1132 {
1133 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1134
1135 if (lsei_end_p (lsei))
1136 return lsei;
1137 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1138 lsei_next_in_partition (&lsei);
1139
1140 return lsei;
1141 }
1142
1143 /* Advance iterator LSE. */
1144 static inline void
1145 lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1146 {
1147 lsei_next (lsei);
1148 while (!lsei_end_p (*lsei)
1149 && (!is_a <cgraph_node> (lsei_node (*lsei))
1150 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1151 lsei_next (lsei);
1152 }
1153
1154 /* Return an iterator to the first node in LSI. */
1155 static inline lto_symtab_encoder_iterator
1156 lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1157 {
1158 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1159
1160 if (lsei_end_p (lsei))
1161 return lsei;
1162 if (!is_a <cgraph_node> (lsei_node (lsei))
1163 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1164 lsei_next_function_in_partition (&lsei);
1165
1166 return lsei;
1167 }
1168
1169 /* Advance iterator LSE. */
1170 static inline void
1171 lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1172 {
1173 lsei_next (lsei);
1174 while (!lsei_end_p (*lsei)
1175 && (!is_a <varpool_node> (lsei_node (*lsei))
1176 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1177 lsei_next (lsei);
1178 }
1179
1180 /* Return an iterator to the first node in LSI. */
1181 static inline lto_symtab_encoder_iterator
1182 lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1183 {
1184 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1185
1186 if (lsei_end_p (lsei))
1187 return lsei;
1188 if (!is_a <varpool_node> (lsei_node (lsei))
1189 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1190 lsei_next_variable_in_partition (&lsei);
1191
1192 return lsei;
1193 }
1194
1195 DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1196 DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1197 DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1198 DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1199 DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1200 DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1201 DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1202
1203 #endif /* GCC_LTO_STREAMER_H */