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