]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lto-streamer.h
Makefile.in (cgraphunit.o): Add $(CFGLOOP_H) dependency.
[thirdparty/gcc.git] / gcc / lto-streamer.h
CommitLineData
d7f09764
DN
1/* Data structures and declarations used for reading and writing
2 GIMPLE to a file stream.
3
d1e082c2 4 Copyright (C) 2009-2013 Free Software Foundation, Inc.
d7f09764
DN
5 Contributed by Doug Kwan <dougkwan@google.com>
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along 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"
4a8fb1a1 27#include "hash-table.h"
d7f09764
DN
28#include "tree.h"
29#include "gimple.h"
30#include "target.h"
31#include "cgraph.h"
32#include "vec.h"
5c4f225f 33#include "alloc-pool.h"
db0bf14f 34#include "gcov-io.h"
f0efc7aa 35#include "diagnostic.h"
47c79d56 36
d7f09764
DN
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
b8698a0f 67 7) THE LABEL NAMES.
d7f09764
DN
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
b8698a0f 76 9) THE CFG.
d7f09764
DN
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
b8698a0f 80 properly restore them.
d7f09764
DN
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
b8698a0f 87
d7f09764
DN
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
8529e24b 144#define LTO_major_version 2
9713d329 145#define LTO_minor_version 2
d7f09764
DN
146
147typedef unsigned char lto_decl_flags_t;
148
149
d7f09764
DN
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. */
b8698a0f 154enum LTO_tags
d7f09764
DN
155{
156 LTO_null = 0,
157
15d16c8a
RB
158 /* Special for streamer. Reference to previously-streamed node. */
159 LTO_tree_pickle_reference,
160
d7f09764
DN
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. */
07233947 172 LTO_bb0 = 1 + MAX_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE,
d7f09764
DN
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
c61f8c3b
RG
181 /* Shared INTEGER_CST node. */
182 LTO_integer_cst,
183
d7f09764
DN
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
ee03e71d
RB
202 /* Special for global streamer. A blob of unnamed tree nodes. */
203 LTO_tree_scc,
204
d7f09764
DN
205 /* References to indexable tree nodes. These objects are stored in
206 tables that are written separately from the function bodies that
207 reference them. This way they can be instantiated even when the
208 referencing functions aren't (e.g., during WPA) and it also allows
209 functions to be copied from one file to another without having
210 to unpickle the body first (the references are location
211 independent).
212
213 NOTE, do not regroup these values as the grouping is exposed
214 in the range checks done in lto_input_tree. */
215 LTO_field_decl_ref, /* Do not change. */
216 LTO_function_decl_ref,
217 LTO_label_decl_ref,
218 LTO_namespace_decl_ref,
219 LTO_result_decl_ref,
220 LTO_ssa_name_ref,
221 LTO_type_decl_ref,
222 LTO_type_ref,
223 LTO_const_decl_ref,
224 LTO_imported_decl_ref,
6be14c0e 225 LTO_translation_unit_decl_ref,
d7f09764
DN
226 LTO_global_decl_ref, /* Do not change. */
227
228 /* This tag must always be last. */
229 LTO_NUM_TAGS
230};
231
232
233/* Set of section types that are in an LTO file. This list will grow
234 as the number of IPA passes grows since each IPA pass will need its
235 own section type to store its summary information.
236
237 When adding a new section type, you must also extend the
238 LTO_SECTION_NAME array in lto-section-in.c. */
239enum lto_section_type
240{
241 LTO_section_decls = 0,
242 LTO_section_function_body,
243 LTO_section_static_initializer,
ab96cc5b 244 LTO_section_symtab,
369451ec 245 LTO_section_refs,
49f836ba 246 LTO_section_asm,
fb3f88cc 247 LTO_section_jump_functions,
d7f09764
DN
248 LTO_section_ipa_pure_const,
249 LTO_section_ipa_reference,
0208f7da 250 LTO_section_ipa_profile,
ab96cc5b 251 LTO_section_symtab_nodes,
d7f09764 252 LTO_section_opts,
922f15c2 253 LTO_section_cgraph_opt_sum,
10a5dd5d 254 LTO_section_inline_summary,
2c9561b5 255 LTO_section_ipcp_transform,
d7f09764
DN
256 LTO_N_SECTION_TYPES /* Must be last. */
257};
258
259/* Indices to the various function, type and symbol streams. */
260typedef enum
261{
262 LTO_DECL_STREAM_TYPE = 0, /* Must be first. */
263 LTO_DECL_STREAM_FIELD_DECL,
264 LTO_DECL_STREAM_FN_DECL,
265 LTO_DECL_STREAM_VAR_DECL,
266 LTO_DECL_STREAM_TYPE_DECL,
267 LTO_DECL_STREAM_NAMESPACE_DECL,
268 LTO_DECL_STREAM_LABEL_DECL,
269 LTO_N_DECL_STREAMS
270} lto_decl_stream_e_t;
271
272typedef enum ld_plugin_symbol_resolution ld_plugin_symbol_resolution_t;
d7f09764
DN
273
274
275/* Macro to define convenience functions for type and decl streams
b8698a0f 276 in lto_file_decl_data. */
d7f09764
DN
277#define DEFINE_DECL_STREAM_FUNCS(UPPER_NAME, name) \
278static inline tree \
279lto_file_decl_data_get_ ## name (struct lto_file_decl_data *data, \
280 unsigned int idx) \
281{ \
282 struct lto_in_decl_state *state = data->current_decl_state; \
283 gcc_assert (idx < state->streams[LTO_DECL_STREAM_## UPPER_NAME].size); \
284 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].trees[idx]; \
285} \
286\
287static inline unsigned int \
288lto_file_decl_data_num_ ## name ## s (struct lto_file_decl_data *data) \
289{ \
290 struct lto_in_decl_state *state = data->current_decl_state; \
291 return state->streams[LTO_DECL_STREAM_## UPPER_NAME].size; \
292}
293
294
295/* Return a char pointer to the start of a data stream for an lto pass
296 or function. The first parameter is the file data that contains
297 the information. The second parameter is the type of information
298 to be obtained. The third parameter is the name of the function
b8698a0f 299 and is only used when finding a function body; otherwise it is
d7f09764 300 NULL. The fourth parameter is the length of the data returned. */
b8698a0f 301typedef const char* (lto_get_section_data_f) (struct lto_file_decl_data *,
d7f09764 302 enum lto_section_type,
b8698a0f 303 const char *,
d7f09764
DN
304 size_t *);
305
306/* Return the data found from the above call. The first three
307 parameters are the same as above. The fourth parameter is the data
073a8998 308 itself and the fifth is the length of the data. */
b8698a0f 309typedef void (lto_free_section_data_f) (struct lto_file_decl_data *,
d7f09764
DN
310 enum lto_section_type,
311 const char *,
312 const char *,
313 size_t);
314
d7f09764 315/* Structure used as buffer for reading an LTO file. */
b8698a0f 316struct lto_input_block
d7f09764
DN
317{
318 const char *data;
319 unsigned int p;
320 unsigned int len;
321};
322
323#define LTO_INIT_INPUT_BLOCK(BASE,D,P,L) \
324 do { \
325 BASE.data = D; \
326 BASE.p = P; \
327 BASE.len = L; \
328 } while (0)
329
330#define LTO_INIT_INPUT_BLOCK_PTR(BASE,D,P,L) \
331 do { \
332 BASE->data = D; \
333 BASE->p = P; \
334 BASE->len = L; \
335 } while (0)
336
337
338/* The is the first part of the record for a function or constructor
339 in the .o file. */
340struct lto_header
341{
342 int16_t major_version;
343 int16_t minor_version;
d7f09764
DN
344};
345
346/* The header for a function body. */
347struct lto_function_header
348{
349 /* The header for all types of sections. */
350 struct lto_header lto_header;
351
352 /* Number of labels with names. */
353 int32_t num_named_labels;
354
355 /* Number of labels without names. */
356 int32_t num_unnamed_labels;
357
358 /* Size compressed or 0 if not compressed. */
359 int32_t compressed_size;
360
361 /* Size of names for named labels. */
362 int32_t named_label_size;
363
364 /* Size of the cfg. */
365 int32_t cfg_size;
366
367 /* Size of main gimple body of function. */
368 int32_t main_size;
369
370 /* Size of the string table. */
371 int32_t string_size;
372};
373
374
375/* Structure describing a symbol section. */
376struct lto_decl_header
377{
378 /* The header for all types of sections. */
379 struct lto_header lto_header;
380
381 /* Size of region for decl state. */
382 int32_t decl_state_size;
383
384 /* Number of nodes in globals stream. */
385 int32_t num_nodes;
386
387 /* Size of region for expressions, decls, types, etc. */
388 int32_t main_size;
389
390 /* Size of the string table. */
391 int32_t string_size;
392};
393
394
49f836ba
JB
395/* Structure describing top level asm()s. */
396struct lto_asm_header
397{
398 /* The header for all types of sections. */
399 struct lto_header lto_header;
400
401 /* Size compressed or 0 if not compressed. */
402 int32_t compressed_size;
403
404 /* Size of region for expressions, decls, types, etc. */
405 int32_t main_size;
406
407 /* Size of the string table. */
408 int32_t string_size;
409};
410
411
d7f09764
DN
412/* Statistics gathered during LTO, WPA and LTRANS. */
413struct lto_stats_d
414{
415 unsigned HOST_WIDE_INT num_input_cgraph_nodes;
7b99cca4 416 unsigned HOST_WIDE_INT num_output_symtab_nodes;
d7f09764
DN
417 unsigned HOST_WIDE_INT num_input_files;
418 unsigned HOST_WIDE_INT num_output_files;
419 unsigned HOST_WIDE_INT num_cgraph_partitions;
420 unsigned HOST_WIDE_INT section_size[LTO_N_SECTION_TYPES];
421 unsigned HOST_WIDE_INT num_function_bodies;
422 unsigned HOST_WIDE_INT num_trees[NUM_TREE_CODES];
423 unsigned HOST_WIDE_INT num_output_il_bytes;
424 unsigned HOST_WIDE_INT num_compressed_il_bytes;
425 unsigned HOST_WIDE_INT num_input_il_bytes;
426 unsigned HOST_WIDE_INT num_uncompressed_il_bytes;
ee03e71d
RB
427 unsigned HOST_WIDE_INT num_tree_bodies_output;
428 unsigned HOST_WIDE_INT num_pickle_refs_output;
d7f09764
DN
429};
430
7b99cca4
JH
431/* Entry of LTO symtab encoder. */
432typedef struct
433{
434 symtab_node node;
435 /* Is the node in this partition (i.e. ltrans of this partition will
436 be responsible for outputting it)? */
437 unsigned int in_partition:1;
438 /* Do we encode body in this partition? */
439 unsigned int body:1;
440 /* Do we encode initializer in this partition?
441 For example the readonly variable initializers are encoded to aid
442 constant folding even if they are not in the partition. */
443 unsigned int initializer:1;
444} lto_encoder_entry;
445
7b99cca4 446
d7f09764 447/* Encoder data structure used to stream callgraph nodes. */
7380e6ef 448struct lto_symtab_encoder_d
d7f09764 449{
9771b263 450 vec<lto_encoder_entry> nodes;
7b99cca4 451 pointer_map_t *map;
2f41ecf5 452};
7380e6ef
JH
453
454typedef struct lto_symtab_encoder_d *lto_symtab_encoder_t;
2f41ecf5 455
f27c1867
JH
456/* Iterator structure for cgraph node sets. */
457typedef struct
703e95cf 458{
f27c1867
JH
459 lto_symtab_encoder_t encoder;
460 unsigned index;
461} lto_symtab_encoder_iterator;
462
463
464
703e95cf 465
d7f09764 466/* Mapping from indices to trees. */
49ba8180 467struct GTY(()) lto_tree_ref_table
d7f09764
DN
468{
469 /* Array of referenced trees . */
49ba8180 470 tree * GTY((length ("%h.size"))) trees;
d7f09764
DN
471
472 /* Size of array. */
473 unsigned int size;
474};
475
476
477/* Mapping between trees and slots in an array. */
478struct lto_decl_slot
479{
480 tree t;
481 int slot_num;
482};
483
484
485/* The lto_tree_ref_encoder struct is used to encode trees into indices. */
486
487struct lto_tree_ref_encoder
488{
489 htab_t tree_hash_table; /* Maps pointers to indices. */
490 unsigned int next_index; /* Next available index. */
9771b263 491 vec<tree> trees; /* Maps indices to pointers. */
d7f09764
DN
492};
493
494
495/* Structure to hold states of input scope. */
49ba8180 496struct GTY(()) lto_in_decl_state
d7f09764
DN
497{
498 /* Array of lto_in_decl_buffers to store type and decls streams. */
499 struct lto_tree_ref_table streams[LTO_N_DECL_STREAMS];
500
501 /* If this in-decl state is associated with a function. FN_DECL
502 point to the FUNCTION_DECL. */
503 tree fn_decl;
504};
505
506typedef struct lto_in_decl_state *lto_in_decl_state_ptr;
507
508
509/* The structure that holds all of the vectors of global types,
510 decls and cgraph nodes used in the serialization of this file. */
511struct lto_out_decl_state
512{
513 /* The buffers contain the sets of decls of various kinds and types we have
514 seen so far and the indexes assigned to them. */
515 struct lto_tree_ref_encoder streams[LTO_N_DECL_STREAMS];
516
517 /* Encoder for cgraph nodes. */
7380e6ef 518 lto_symtab_encoder_t symtab_node_encoder;
2f41ecf5 519
d7f09764
DN
520 /* If this out-decl state belongs to a function, fn_decl points to that
521 function. Otherwise, it is NULL. */
522 tree fn_decl;
523};
524
525typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
526
d7f09764 527
aed7d7cf
AK
528/* Compact representation of a index <-> resolution pair. Unpacked to an
529 vector later. */
530struct res_pair
531{
532 ld_plugin_symbol_resolution_t res;
533 unsigned index;
534};
535typedef struct res_pair res_pair;
536
aed7d7cf 537
d7f09764
DN
538/* One of these is allocated for each object file that being compiled
539 by lto. This structure contains the tables that are needed by the
540 serialized functions and ipa passes to connect themselves to the
541 global types and decls as they are reconstituted. */
49ba8180 542struct GTY(()) lto_file_decl_data
d7f09764
DN
543{
544 /* Decl state currently used. */
545 struct lto_in_decl_state *current_decl_state;
546
547 /* Decl state corresponding to regions outside of any functions
548 in the compilation unit. */
549 struct lto_in_decl_state *global_decl_state;
550
551 /* Table of cgraph nodes present in this file. */
7380e6ef 552 lto_symtab_encoder_t GTY((skip)) symtab_node_encoder;
2f41ecf5 553
d7f09764 554 /* Hash table maps lto-related section names to location in file. */
49ba8180 555 htab_t GTY((param_is (struct lto_in_decl_state))) function_decl_states;
d7f09764
DN
556
557 /* The .o file that these offsets relate to. */
49ba8180 558 const char *GTY((skip)) file_name;
d7f09764 559
d7f09764 560 /* Hash table maps lto-related section names to location in file. */
49ba8180 561 htab_t GTY((skip)) section_hash_table;
d7f09764
DN
562
563 /* Hash new name of renamed global declaration to its original name. */
49ba8180 564 htab_t GTY((skip)) renaming_hash_table;
73ce4d1e
AK
565
566 /* Linked list used temporarily in reader */
567 struct lto_file_decl_data *next;
568
569 /* Sub ID for merged objects. */
dde8b360 570 unsigned HOST_WIDE_INT id;
73ce4d1e
AK
571
572 /* Symbol resolutions for this file */
9771b263 573 vec<res_pair> GTY((skip)) respairs;
aed7d7cf 574 unsigned max_index;
db0bf14f
JH
575
576 struct gcov_ctr_summary GTY((skip)) profile_info;
bbf9ad07
JH
577
578 /* Map assigning declarations their resolutions. */
579 pointer_map_t * GTY((skip)) resolution_map;
d7f09764
DN
580};
581
a9429e29
LB
582typedef struct lto_file_decl_data *lto_file_decl_data_ptr;
583
d7f09764
DN
584struct lto_char_ptr_base
585{
586 char *ptr;
587};
588
589/* An incore byte stream to buffer the various parts of the function.
590 The entire structure should be zeroed when created. The record
591 consists of a set of blocks. The first sizeof (ptr) bytes are used
592 as a chain, and the rest store the bytes to be written. */
593struct lto_output_stream
594{
595 /* The pointer to the first block in the stream. */
596 struct lto_char_ptr_base * first_block;
597
598 /* The pointer to the last and current block in the stream. */
599 struct lto_char_ptr_base * current_block;
600
601 /* The pointer to where the next char should be written. */
602 char * current_pointer;
603
604 /* The number of characters left in the current block. */
605 unsigned int left_in_block;
606
607 /* The block size of the last block allocated. */
608 unsigned int block_size;
609
610 /* The total number of characters written. */
611 unsigned int total_size;
612};
613
614/* The is the first part of the record in an LTO file for many of the
615 IPA passes. */
616struct lto_simple_header
617{
618 /* The header for all types of sections. */
619 struct lto_header lto_header;
620
621 /* Size of main gimple body of function. */
622 int32_t main_size;
623
624 /* Size of main stream when compressed. */
625 int32_t compressed_size;
626};
627
628/* A simple output block. This can be used for simple IPA passes that
629 do not need more than one stream. */
630struct lto_simple_output_block
631{
632 enum lto_section_type section_type;
633 struct lto_out_decl_state *decl_state;
634
635 /* The stream that the main tree codes are written to. */
636 struct lto_output_stream *main_stream;
637};
638
4a8fb1a1
LC
639/* String hashing. */
640
641struct string_slot
642{
643 const char *s;
644 int len;
645 unsigned int slot_num;
646};
647
648/* Hashtable helpers. */
649
650struct string_slot_hasher : typed_noop_remove <string_slot>
651{
652 typedef string_slot value_type;
653 typedef string_slot compare_type;
654 static inline hashval_t hash (const value_type *);
655 static inline bool equal (const value_type *, const compare_type *);
656};
657
658/* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
659 to support strings that may not end in '\0'. */
660
661inline hashval_t
662string_slot_hasher::hash (const value_type *ds)
663{
664 hashval_t r = ds->len;
665 int i;
666
667 for (i = 0; i < ds->len; i++)
668 r = r * 67 + (unsigned)ds->s[i] - 113;
669 return r;
670}
671
672/* Returns nonzero if DS1 and DS2 are equal. */
673
674inline bool
675string_slot_hasher::equal (const value_type *ds1, const compare_type *ds2)
676{
677 if (ds1->len == ds2->len)
678 return memcmp (ds1->s, ds2->s, ds1->len) == 0;
679
680 return 0;
681}
682
d7f09764
DN
683/* Data structure holding all the data and descriptors used when writing
684 an LTO file. */
685struct output_block
686{
687 enum lto_section_type section_type;
688 struct lto_out_decl_state *decl_state;
689
690 /* The stream that the main tree codes are written to. */
691 struct lto_output_stream *main_stream;
692
693 /* The stream that contains the string table. */
694 struct lto_output_stream *string_stream;
695
696 /* The stream that contains the cfg. */
697 struct lto_output_stream *cfg_stream;
698
699 /* The hash table that contains the set of strings we have seen so
700 far and the indexes assigned to them. */
4a8fb1a1 701 hash_table <string_slot_hasher> string_hash_table;
d7f09764
DN
702
703 /* The current cgraph_node that we are currently serializing. Null
704 if we are serializing something else. */
705 struct cgraph_node *cgraph_node;
706
707 /* These are the last file and line that were seen in the stream.
708 If the current node differs from these, it needs to insert
709 something into the stream and fix these up. */
710 const char *current_file;
711 int current_line;
712 int current_col;
713
714 /* True if writing globals and types. */
715 bool global;
716
717 /* Cache of nodes written in this section. */
412288f1 718 struct streamer_tree_cache_d *writer_cache;
fac009a8
JH
719
720 /* All data persistent across whole duration of output block
721 can go here. */
722 struct obstack obstack;
d7f09764
DN
723};
724
725
726/* Data and descriptors used when reading from an LTO file. */
727struct data_in
728{
729 /* The global decls and types. */
730 struct lto_file_decl_data *file_data;
731
732 /* All of the labels. */
733 tree *labels;
734
735 /* The string table. */
736 const char *strings;
737
738 /* The length of the string table. */
739 unsigned int strings_len;
740
741 /* Number of named labels. Used to find the index of unnamed labels
742 since they share space with the named labels. */
b8698a0f 743 unsigned int num_named_labels;
d7f09764
DN
744
745 /* Number of unnamed labels. */
746 unsigned int num_unnamed_labels;
747
d7f09764 748 /* Maps each reference number to the resolution done by the linker. */
9771b263 749 vec<ld_plugin_symbol_resolution_t> globals_resolution;
d7f09764
DN
750
751 /* Cache of pickled nodes. */
412288f1 752 struct streamer_tree_cache_d *reader_cache;
d7f09764
DN
753};
754
755
756/* In lto-section-in.c */
757extern struct lto_input_block * lto_create_simple_input_block (
b8698a0f 758 struct lto_file_decl_data *,
d7f09764
DN
759 enum lto_section_type, const char **, size_t *);
760extern void
b8698a0f 761lto_destroy_simple_input_block (struct lto_file_decl_data *,
d7f09764
DN
762 enum lto_section_type,
763 struct lto_input_block *, const char *, size_t);
b8698a0f 764extern void lto_set_in_hooks (struct lto_file_decl_data **,
d7f09764
DN
765 lto_get_section_data_f *,
766 lto_free_section_data_f *);
767extern struct lto_file_decl_data **lto_get_file_decl_data (void);
768extern const char *lto_get_section_data (struct lto_file_decl_data *,
769 enum lto_section_type,
770 const char *, size_t *);
771extern void lto_free_section_data (struct lto_file_decl_data *,
772 enum lto_section_type,
773 const char *, const char *, size_t);
d7f09764
DN
774extern htab_t lto_create_renaming_table (void);
775extern void lto_record_renamed_decl (struct lto_file_decl_data *,
776 const char *, const char *);
777extern const char *lto_get_decl_name_mapping (struct lto_file_decl_data *,
778 const char *);
779extern struct lto_in_decl_state *lto_new_in_decl_state (void);
780extern void lto_delete_in_decl_state (struct lto_in_decl_state *);
781extern hashval_t lto_hash_in_decl_state (const void *);
782extern int lto_eq_in_decl_state (const void *, const void *);
783extern struct lto_in_decl_state *lto_get_function_in_decl_state (
784 struct lto_file_decl_data *, tree);
bc0fe8cb 785extern void lto_section_overrun (struct lto_input_block *) ATTRIBUTE_NORETURN;
f242c0a5
JH
786extern void lto_value_range_error (const char *,
787 HOST_WIDE_INT, HOST_WIDE_INT,
788 HOST_WIDE_INT) ATTRIBUTE_NORETURN;
d7f09764
DN
789
790/* In lto-section-out.c */
791extern hashval_t lto_hash_decl_slot_node (const void *);
792extern int lto_eq_decl_slot_node (const void *, const void *);
793extern hashval_t lto_hash_type_slot_node (const void *);
794extern int lto_eq_type_slot_node (const void *, const void *);
795extern void lto_begin_section (const char *, bool);
796extern void lto_end_section (void);
797extern void lto_write_stream (struct lto_output_stream *);
d7f09764
DN
798extern void lto_output_data_stream (struct lto_output_stream *, const void *,
799 size_t);
d7f09764
DN
800extern bool lto_output_decl_index (struct lto_output_stream *,
801 struct lto_tree_ref_encoder *,
802 tree, unsigned int *);
803extern void lto_output_field_decl_index (struct lto_out_decl_state *,
804 struct lto_output_stream *, tree);
805extern void lto_output_fn_decl_index (struct lto_out_decl_state *,
806 struct lto_output_stream *, tree);
807extern void lto_output_namespace_decl_index (struct lto_out_decl_state *,
808 struct lto_output_stream *, tree);
809extern void lto_output_var_decl_index (struct lto_out_decl_state *,
810 struct lto_output_stream *, tree);
811extern void lto_output_type_decl_index (struct lto_out_decl_state *,
812 struct lto_output_stream *, tree);
813extern void lto_output_type_ref_index (struct lto_out_decl_state *,
814 struct lto_output_stream *, tree);
815extern struct lto_simple_output_block *lto_create_simple_output_block (
816 enum lto_section_type);
817extern void lto_destroy_simple_output_block (struct lto_simple_output_block *);
818extern struct lto_out_decl_state *lto_new_out_decl_state (void);
819extern void lto_delete_out_decl_state (struct lto_out_decl_state *);
820extern struct lto_out_decl_state *lto_get_out_decl_state (void);
821extern void lto_push_out_decl_state (struct lto_out_decl_state *);
822extern struct lto_out_decl_state *lto_pop_out_decl_state (void);
823extern void lto_record_function_out_decl_state (tree,
824 struct lto_out_decl_state *);
bc0fe8cb 825extern void lto_append_block (struct lto_output_stream *);
d7f09764
DN
826
827
828/* In lto-streamer.c. */
829extern const char *lto_tag_name (enum LTO_tags);
830extern bitmap lto_bitmap_alloc (void);
831extern void lto_bitmap_free (bitmap);
73ce4d1e 832extern char *lto_get_section_name (int, const char *, struct lto_file_decl_data *);
b8f4e58f 833extern void print_lto_report (const char *);
d7f09764
DN
834extern void lto_streamer_init (void);
835extern bool gate_lto_out (void);
836#ifdef LTO_STREAMER_DEBUG
837extern void lto_orig_address_map (tree, intptr_t);
838extern intptr_t lto_orig_address_get (tree);
839extern void lto_orig_address_remove (tree);
840#endif
841extern void lto_check_version (int, int);
47c79d56 842extern void lto_streamer_hooks_init (void);
d7f09764
DN
843
844/* In lto-streamer-in.c */
d7f09764 845extern void lto_input_cgraph (struct lto_file_decl_data *, const char *);
47c79d56 846extern void lto_reader_init (void);
d7f09764
DN
847extern void lto_input_function_body (struct lto_file_decl_data *, tree,
848 const char *);
849extern void lto_input_constructors_and_inits (struct lto_file_decl_data *,
850 const char *);
398f05da 851extern void lto_input_toplevel_asms (struct lto_file_decl_data *, int);
d7f09764
DN
852extern struct data_in *lto_data_in_create (struct lto_file_decl_data *,
853 const char *, unsigned,
9771b263 854 vec<ld_plugin_symbol_resolution_t> );
d7f09764 855extern void lto_data_in_delete (struct data_in *);
a183b5c7 856extern void lto_input_data_block (struct lto_input_block *, void *, size_t);
7cb7d208 857location_t lto_input_location (struct bitpack_d *, struct data_in *);
f0efc7aa
DN
858tree lto_input_tree_ref (struct lto_input_block *, struct data_in *,
859 struct function *, enum LTO_tags);
860void lto_tag_check_set (enum LTO_tags, int, ...);
861void lto_init_eh (void);
ee03e71d
RB
862hashval_t lto_input_scc (struct lto_input_block *, struct data_in *,
863 unsigned *, unsigned *);
864tree lto_input_tree_1 (struct lto_input_block *, struct data_in *,
865 enum LTO_tags, hashval_t hash);
b9393656 866tree lto_input_tree (struct lto_input_block *, struct data_in *);
d7f09764
DN
867
868
869/* In lto-streamer-out.c */
870extern void lto_register_decl_definition (tree, struct lto_file_decl_data *);
871extern struct output_block *create_output_block (enum lto_section_type);
872extern void destroy_output_block (struct output_block *);
7e54c608 873extern void lto_output_tree (struct output_block *, tree, bool, bool);
49f836ba 874extern void lto_output_toplevel_asms (void);
fb3f88cc 875extern void produce_asm (struct output_block *ob, tree fn);
a183b5c7
DN
876void lto_output_decl_state_streams (struct output_block *,
877 struct lto_out_decl_state *);
878void lto_output_decl_state_refs (struct output_block *,
879 struct lto_output_stream *,
880 struct lto_out_decl_state *);
7cb7d208 881void lto_output_location (struct output_block *, struct bitpack_d *, location_t);
d7f09764
DN
882
883
884/* In lto-cgraph.c */
e75f8f79 885lto_symtab_encoder_t lto_symtab_encoder_new (bool);
7380e6ef
JH
886int lto_symtab_encoder_encode (lto_symtab_encoder_t, symtab_node);
887void lto_symtab_encoder_delete (lto_symtab_encoder_t);
7b99cca4 888bool lto_symtab_encoder_delete_node (lto_symtab_encoder_t, symtab_node);
7380e6ef 889bool lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t,
91fbf0c7 890 struct cgraph_node *);
f27c1867
JH
891bool lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t,
892 symtab_node);
893void lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t,
894 symtab_node);
91fbf0c7 895
7380e6ef
JH
896bool lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t,
897 struct varpool_node *);
f27c1867 898void output_symtab (void);
ab96cc5b 899void input_symtab (void);
369451ec 900bool referenced_from_other_partition_p (struct ipa_ref_list *,
f27c1867 901 lto_symtab_encoder_t);
9a809897 902bool reachable_from_other_partition_p (struct cgraph_node *,
f27c1867 903 lto_symtab_encoder_t);
f3380641 904bool referenced_from_this_partition_p (struct ipa_ref_list *,
f27c1867 905 lto_symtab_encoder_t);
f3380641 906bool reachable_from_this_partition_p (struct cgraph_node *,
f27c1867 907 lto_symtab_encoder_t);
b4661bfe 908lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
d7f09764
DN
909
910
911/* In lto-symtab.c. */
1a735925 912extern void lto_symtab_merge_decls (void);
40a7fe1e 913extern void lto_symtab_merge_symbols (void);
d7f09764 914extern tree lto_symtab_prevailing_decl (tree decl);
9771b263 915extern GTY(()) vec<tree, va_gc> *lto_global_var_decls;
d7f09764
DN
916
917
918/* In lto-opts.c. */
d7f09764 919extern void lto_write_options (void);
d7f09764
DN
920
921
d7f09764
DN
922/* Statistics gathered during LTO, WPA and LTRANS. */
923extern struct lto_stats_d lto_stats;
924
925/* Section names corresponding to the values of enum lto_section_type. */
926extern const char *lto_section_name[];
927
928/* Holds all the out decl states of functions output so far in the
929 current output file. */
9771b263 930extern vec<lto_out_decl_state_ptr> lto_function_decl_states;
d7f09764
DN
931
932/* Return true if LTO tag TAG corresponds to a tree code. */
933static inline bool
934lto_tag_is_tree_code_p (enum LTO_tags tag)
935{
15d16c8a 936 return tag > LTO_tree_pickle_reference && (unsigned) tag <= MAX_TREE_CODES;
d7f09764
DN
937}
938
939
940/* Return true if LTO tag TAG corresponds to a gimple code. */
941static inline bool
942lto_tag_is_gimple_code_p (enum LTO_tags tag)
943{
15d16c8a
RB
944 return (unsigned) tag >= NUM_TREE_CODES + 2
945 && (unsigned) tag < 2 + NUM_TREE_CODES + LAST_AND_UNUSED_GIMPLE_CODE;
d7f09764
DN
946}
947
948
949/* Return the LTO tag corresponding to gimple code CODE. See enum
950 LTO_tags for details on the conversion. */
951static inline enum LTO_tags
952lto_gimple_code_to_tag (enum gimple_code code)
953{
15d16c8a 954 return (enum LTO_tags) ((unsigned) code + NUM_TREE_CODES + 2);
d7f09764
DN
955}
956
957
958/* Return the GIMPLE code corresponding to TAG. See enum LTO_tags for
959 details on the conversion. */
960static inline enum gimple_code
961lto_tag_to_gimple_code (enum LTO_tags tag)
962{
963 gcc_assert (lto_tag_is_gimple_code_p (tag));
15d16c8a 964 return (enum gimple_code) ((unsigned) tag - NUM_TREE_CODES - 2);
d7f09764
DN
965}
966
967
968/* Return the LTO tag corresponding to tree code CODE. See enum
969 LTO_tags for details on the conversion. */
970static inline enum LTO_tags
971lto_tree_code_to_tag (enum tree_code code)
972{
15d16c8a 973 return (enum LTO_tags) ((unsigned) code + 2);
d7f09764
DN
974}
975
976
977/* Return the tree code corresponding to TAG. See enum LTO_tags for
978 details on the conversion. */
979static inline enum tree_code
980lto_tag_to_tree_code (enum LTO_tags tag)
981{
982 gcc_assert (lto_tag_is_tree_code_p (tag));
15d16c8a 983 return (enum tree_code) ((unsigned) tag - 2);
d7f09764
DN
984}
985
f0efc7aa
DN
986/* Check that tag ACTUAL == EXPECTED. */
987static inline void
988lto_tag_check (enum LTO_tags actual, enum LTO_tags expected)
989{
990 if (actual != expected)
991 internal_error ("bytecode stream: expected tag %s instead of %s",
992 lto_tag_name (expected), lto_tag_name (actual));
993}
994
995/* Check that tag ACTUAL is in the range [TAG1, TAG2]. */
996static inline void
997lto_tag_check_range (enum LTO_tags actual, enum LTO_tags tag1,
998 enum LTO_tags tag2)
999{
1000 if (actual < tag1 || actual > tag2)
1001 internal_error ("bytecode stream: tag %s is not in the expected range "
1002 "[%s, %s]",
1003 lto_tag_name (actual),
1004 lto_tag_name (tag1),
1005 lto_tag_name (tag2));
1006}
1007
d7f09764
DN
1008/* Initialize an lto_out_decl_buffer ENCODER. */
1009static inline void
1010lto_init_tree_ref_encoder (struct lto_tree_ref_encoder *encoder,
1011 htab_hash hash_fn, htab_eq eq_fn)
1012{
1013 encoder->tree_hash_table = htab_create (37, hash_fn, eq_fn, free);
1014 encoder->next_index = 0;
9771b263 1015 encoder->trees.create (0);
d7f09764
DN
1016}
1017
1018
073a8998 1019/* Destroy an lto_tree_ref_encoder ENCODER by freeing its contents. The
d7f09764
DN
1020 memory used by ENCODER is not freed by this function. */
1021static inline void
1022lto_destroy_tree_ref_encoder (struct lto_tree_ref_encoder *encoder)
1023{
1024 /* Hash table may be delete already. */
1025 if (encoder->tree_hash_table)
1026 htab_delete (encoder->tree_hash_table);
9771b263 1027 encoder->trees.release ();
d7f09764
DN
1028}
1029
1030/* Return the number of trees encoded in ENCODER. */
1031static inline unsigned int
1032lto_tree_ref_encoder_size (struct lto_tree_ref_encoder *encoder)
1033{
9771b263 1034 return encoder->trees.length ();
d7f09764
DN
1035}
1036
1037/* Return the IDX-th tree in ENCODER. */
1038static inline tree
1039lto_tree_ref_encoder_get_tree (struct lto_tree_ref_encoder *encoder,
1040 unsigned int idx)
1041{
9771b263 1042 return encoder->trees[idx];
d7f09764
DN
1043}
1044
1045
1046/* Return true if LABEL should be emitted in the global context. */
1047static inline bool
1048emit_label_in_global_context_p (tree label)
1049{
1050 return DECL_NONLOCAL (label) || FORCED_LABEL (label);
1051}
1052
f27c1867
JH
1053/* Return number of encoded nodes in ENCODER. */
1054static inline int
1055lto_symtab_encoder_size (lto_symtab_encoder_t encoder)
1056{
9771b263 1057 return encoder->nodes.length ();
7b99cca4
JH
1058}
1059
1060/* Value used to represent failure of lto_symtab_encoder_lookup. */
1061#define LCC_NOT_FOUND (-1)
1062
1063/* Look up NODE in encoder. Return NODE's reference if it has been encoded
1064 or LCC_NOT_FOUND if it is not there. */
1065
1066static inline int
1067lto_symtab_encoder_lookup (lto_symtab_encoder_t encoder,
1068 symtab_node node)
1069{
1070 void **slot = pointer_map_contains (encoder->map, node);
1071 return (slot && *slot ? (size_t) *(slot) - 1 : LCC_NOT_FOUND);
f27c1867
JH
1072}
1073
1074/* Return true if iterator LSE points to nothing. */
1075static inline bool
1076lsei_end_p (lto_symtab_encoder_iterator lsei)
1077{
1078 return lsei.index >= (unsigned)lto_symtab_encoder_size (lsei.encoder);
1079}
1080
1081/* Advance iterator LSE. */
1082static inline void
1083lsei_next (lto_symtab_encoder_iterator *lsei)
1084{
1085 lsei->index++;
1086}
1087
1088/* Return the node pointed to by LSI. */
1089static inline symtab_node
1090lsei_node (lto_symtab_encoder_iterator lsei)
1091{
9771b263 1092 return lsei.encoder->nodes[lsei.index].node;
f27c1867
JH
1093}
1094
1095/* Return the node pointed to by LSI. */
1096static inline struct cgraph_node *
1097lsei_cgraph_node (lto_symtab_encoder_iterator lsei)
1098{
9771b263 1099 return cgraph (lsei.encoder->nodes[lsei.index].node);
f27c1867
JH
1100}
1101
1102/* Return the node pointed to by LSI. */
1103static inline struct varpool_node *
1104lsei_varpool_node (lto_symtab_encoder_iterator lsei)
1105{
9771b263 1106 return varpool (lsei.encoder->nodes[lsei.index].node);
f27c1867
JH
1107}
1108
f27c1867
JH
1109/* Return the cgraph node corresponding to REF using ENCODER. */
1110
1111static inline symtab_node
1112lto_symtab_encoder_deref (lto_symtab_encoder_t encoder, int ref)
1113{
1114 if (ref == LCC_NOT_FOUND)
1115 return NULL;
1116
9771b263 1117 return encoder->nodes[ref].node;
f27c1867
JH
1118}
1119
1120/* Return an iterator to the first node in LSI. */
1121static inline lto_symtab_encoder_iterator
1122lsei_start (lto_symtab_encoder_t encoder)
1123{
1124 lto_symtab_encoder_iterator lsei;
1125
1126 lsei.encoder = encoder;
1127 lsei.index = 0;
1128 return lsei;
1129}
1130
1131/* Advance iterator LSE. */
1132static inline void
1133lsei_next_in_partition (lto_symtab_encoder_iterator *lsei)
1134{
1135 lsei_next (lsei);
1136 while (!lsei_end_p (*lsei)
1137 && !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei)))
1138 lsei_next (lsei);
1139}
1140
1141/* Return an iterator to the first node in LSI. */
1142static inline lto_symtab_encoder_iterator
1143lsei_start_in_partition (lto_symtab_encoder_t encoder)
1144{
1145 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1146
1147 if (lsei_end_p (lsei))
1148 return lsei;
1149 if (!lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1150 lsei_next_in_partition (&lsei);
1151
1152 return lsei;
1153}
1154
1155/* Advance iterator LSE. */
1156static inline void
1157lsei_next_function_in_partition (lto_symtab_encoder_iterator *lsei)
1158{
1159 lsei_next (lsei);
1160 while (!lsei_end_p (*lsei)
5d59b5e1 1161 && (!is_a <cgraph_node> (lsei_node (*lsei))
f27c1867
JH
1162 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1163 lsei_next (lsei);
1164}
1165
1166/* Return an iterator to the first node in LSI. */
1167static inline lto_symtab_encoder_iterator
1168lsei_start_function_in_partition (lto_symtab_encoder_t encoder)
1169{
1170 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1171
1172 if (lsei_end_p (lsei))
1173 return lsei;
5d59b5e1 1174 if (!is_a <cgraph_node> (lsei_node (lsei))
f27c1867
JH
1175 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1176 lsei_next_function_in_partition (&lsei);
1177
1178 return lsei;
1179}
1180
1181/* Advance iterator LSE. */
1182static inline void
1183lsei_next_variable_in_partition (lto_symtab_encoder_iterator *lsei)
1184{
1185 lsei_next (lsei);
1186 while (!lsei_end_p (*lsei)
5d59b5e1 1187 && (!is_a <varpool_node> (lsei_node (*lsei))
f27c1867
JH
1188 || !lto_symtab_encoder_in_partition_p (lsei->encoder, lsei_node (*lsei))))
1189 lsei_next (lsei);
1190}
1191
1192/* Return an iterator to the first node in LSI. */
1193static inline lto_symtab_encoder_iterator
1194lsei_start_variable_in_partition (lto_symtab_encoder_t encoder)
1195{
1196 lto_symtab_encoder_iterator lsei = lsei_start (encoder);
1197
1198 if (lsei_end_p (lsei))
1199 return lsei;
5d59b5e1 1200 if (!is_a <varpool_node> (lsei_node (lsei))
f27c1867
JH
1201 || !lto_symtab_encoder_in_partition_p (encoder, lsei_node (lsei)))
1202 lsei_next_variable_in_partition (&lsei);
1203
1204 return lsei;
1205}
1206
d7f09764
DN
1207DEFINE_DECL_STREAM_FUNCS (TYPE, type)
1208DEFINE_DECL_STREAM_FUNCS (FIELD_DECL, field_decl)
1209DEFINE_DECL_STREAM_FUNCS (FN_DECL, fn_decl)
1210DEFINE_DECL_STREAM_FUNCS (VAR_DECL, var_decl)
1211DEFINE_DECL_STREAM_FUNCS (TYPE_DECL, type_decl)
1212DEFINE_DECL_STREAM_FUNCS (NAMESPACE_DECL, namespace_decl)
1213DEFINE_DECL_STREAM_FUNCS (LABEL_DECL, label_decl)
1214
1215#endif /* GCC_LTO_STREAMER_H */