Line data Source code
1 : /* Write the GIMPLE representation to a file stream.
2 :
3 : Copyright (C) 2009-2026 Free Software Foundation, Inc.
4 : Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 : Re-implemented by Diego Novillo <dnovillo@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 : #include "config.h"
24 : #include "system.h"
25 : #include "coretypes.h"
26 : #include "backend.h"
27 : #include "target.h"
28 : #include "rtl.h"
29 : #include "tree.h"
30 : #include "gimple.h"
31 : #include "tree-pass.h"
32 : #include "ssa.h"
33 : #include "gimple-streamer.h"
34 : #include "alias.h"
35 : #include "stor-layout.h"
36 : #include "gimple-iterator.h"
37 : #include "except.h"
38 : #include "lto-symtab.h"
39 : #include "cgraph.h"
40 : #include "cfgloop.h"
41 : #include "builtins.h"
42 : #include "gomp-constants.h"
43 : #include "debug.h"
44 : #include "omp-offload.h"
45 : #include "print-tree.h"
46 : #include "tree-dfa.h"
47 : #include "file-prefix-map.h" /* remap_debug_filename() */
48 : #include "output.h"
49 : #include "ipa-utils.h"
50 : #include "toplev.h"
51 :
52 :
53 : static void lto_write_tree (struct output_block*, tree, bool);
54 :
55 : /* Clear the line info stored in DATA_IN. */
56 :
57 : static void
58 314352 : clear_line_info (struct output_block *ob)
59 : {
60 314352 : ob->current_file = NULL;
61 314352 : ob->current_line = 0;
62 314352 : ob->current_col = 0;
63 314352 : ob->current_sysp = false;
64 314352 : ob->reset_locus = true;
65 314352 : ob->emit_pwd = true;
66 : /* Initialize to something that will never appear as block,
67 : so that the first location with block in a function etc.
68 : always streams a change_block bit and the first block. */
69 314352 : ob->current_block = void_node;
70 314352 : ob->current_discr = UINT_MAX;
71 0 : }
72 :
73 :
74 : /* Create the output block and return it. SECTION_TYPE is
75 : LTO_section_function_body or LTO_static_initializer. */
76 :
77 : struct output_block *
78 314352 : create_output_block (enum lto_section_type section_type)
79 : {
80 314352 : struct output_block *ob = XCNEW (struct output_block);
81 314352 : if (streamer_dump_file)
82 40 : fprintf (streamer_dump_file, "Creating output block for %s\n",
83 40 : lto_section_name[section_type]);
84 :
85 314352 : ob->section_type = section_type;
86 314352 : ob->decl_state = lto_get_out_decl_state ();
87 : /* Only global decl stream in non-wpa will ever be considered by tree
88 : merging. */
89 314352 : if (!flag_wpa && section_type == LTO_section_decls)
90 23036 : ob->local_trees = new (hash_set <tree>);
91 314352 : ob->main_stream = XCNEW (struct lto_output_stream);
92 314352 : ob->string_stream = XCNEW (struct lto_output_stream);
93 314352 : ob->writer_cache = streamer_tree_cache_create (!flag_wpa, true, false);
94 :
95 314352 : if (section_type == LTO_section_function_body)
96 118397 : ob->cfg_stream = XCNEW (struct lto_output_stream);
97 :
98 314352 : clear_line_info (ob);
99 :
100 314352 : ob->string_hash_table = new hash_table<string_slot_hasher> (37);
101 314352 : gcc_obstack_init (&ob->obstack);
102 :
103 314352 : return ob;
104 : }
105 :
106 :
107 : /* Destroy the output block OB. */
108 :
109 : void
110 314352 : destroy_output_block (struct output_block *ob)
111 : {
112 314352 : enum lto_section_type section_type = ob->section_type;
113 :
114 314352 : delete ob->string_hash_table;
115 314352 : ob->string_hash_table = NULL;
116 337388 : delete ob->local_trees;
117 :
118 314352 : free (ob->main_stream);
119 314352 : free (ob->string_stream);
120 314352 : if (section_type == LTO_section_function_body)
121 118397 : free (ob->cfg_stream);
122 :
123 314352 : streamer_tree_cache_delete (ob->writer_cache);
124 314352 : obstack_free (&ob->obstack, NULL);
125 :
126 314352 : free (ob);
127 314352 : }
128 :
129 :
130 : /* Wrapper around variably_modified_type_p avoiding type modification
131 : during WPA streaming. */
132 :
133 : bool
134 14288982 : lto_variably_modified_type_p (tree type)
135 : {
136 14288982 : return (in_lto_p
137 14288982 : ? TYPE_LANG_FLAG_0 (TYPE_MAIN_VARIANT (type))
138 13989287 : : variably_modified_type_p (type, NULL_TREE));
139 : }
140 :
141 :
142 : /* Return true if tree node T is written to various tables. For these
143 : nodes, we sometimes want to write their phyiscal representation
144 : (via lto_output_tree), and sometimes we need to emit an index
145 : reference into a table (via lto_output_tree_ref). */
146 :
147 : static bool
148 35712174 : tree_is_indexable (tree t)
149 : {
150 : /* Parameters and return values of functions of variably modified types
151 : must go to global stream, because they may be used in the type
152 : definition. */
153 34611557 : if ((TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
154 36040730 : && DECL_CONTEXT (t))
155 1429173 : return lto_variably_modified_type_p (TREE_TYPE (DECL_CONTEXT (t)));
156 : /* IMPORTED_DECL is put into BLOCK and thus it never can be shared.
157 : We should no longer need to stream it. */
158 34283001 : else if (TREE_CODE (t) == IMPORTED_DECL)
159 0 : gcc_unreachable ();
160 34283001 : else if (TREE_CODE (t) == LABEL_DECL)
161 80989 : return FORCED_LABEL (t) || DECL_NONLOCAL (t);
162 1655717 : else if (((VAR_P (t) && !TREE_STATIC (t))
163 : || TREE_CODE (t) == TYPE_DECL
164 : || TREE_CODE (t) == CONST_DECL
165 : || TREE_CODE (t) == NAMELIST_DECL)
166 35080702 : && decl_function_context (t))
167 : return false;
168 33333061 : else if (TREE_CODE (t) == DEBUG_EXPR_DECL)
169 : return false;
170 : /* Variably modified types need to be streamed alongside function
171 : bodies because they can refer to local entities. Together with
172 : them we have to localize their members as well.
173 : ??? In theory that includes non-FIELD_DECLs as well. */
174 33331109 : else if (TYPE_P (t)
175 33331109 : && lto_variably_modified_type_p (t))
176 : return false;
177 33319776 : else if (TREE_CODE (t) == FIELD_DECL
178 33319776 : && lto_variably_modified_type_p (DECL_CONTEXT (t)))
179 : return false;
180 : else
181 54051174 : return (IS_TYPE_OR_DECL_P (t) || TREE_CODE (t) == SSA_NAME);
182 : }
183 :
184 :
185 : /* Output info about new location into bitpack BP.
186 : After outputting bitpack, lto_output_location_data has
187 : to be done to output actual data. */
188 :
189 : static void
190 6781442 : lto_output_location_1 (struct output_block *ob, struct bitpack_d *bp,
191 : location_t orig_loc, bool block_p)
192 : {
193 6781442 : location_t loc = LOCATION_LOCUS (orig_loc);
194 :
195 6781442 : if (loc >= RESERVED_LOCATION_COUNT)
196 : {
197 4372454 : expanded_location xloc = expand_location (loc);
198 4372454 : unsigned discr = get_discriminator_from_loc (orig_loc);
199 :
200 4372454 : if (ob->reset_locus)
201 : {
202 138822 : if (xloc.file == NULL)
203 0 : ob->current_file = "";
204 138822 : if (xloc.line == 0)
205 0 : ob->current_line = 1;
206 138822 : if (xloc.column == 0)
207 4 : ob->current_col = 1;
208 138822 : ob->reset_locus = false;
209 : }
210 :
211 : /* As RESERVED_LOCATION_COUNT is 2, we can use the spare value of
212 : 3 without wasting additional bits to signalize file change.
213 : If RESERVED_LOCATION_COUNT changes, reconsider this. */
214 4372454 : gcc_checking_assert (RESERVED_LOCATION_COUNT == 2);
215 4372454 : bp_pack_int_in_range (bp, 0, RESERVED_LOCATION_COUNT + 1,
216 : RESERVED_LOCATION_COUNT
217 4372454 : + (ob->current_file != xloc.file));
218 :
219 4372454 : bp_pack_value (bp, ob->current_line != xloc.line, 1);
220 4372454 : bp_pack_value (bp, ob->current_col != xloc.column, 1);
221 4372454 : bp_pack_value (bp, ob->current_discr != discr, 1);
222 :
223 4372454 : if (ob->current_file != xloc.file)
224 : {
225 217046 : bool stream_pwd = false;
226 217046 : const char *remapped = remap_debug_filename (xloc.file);
227 217046 : if (ob->emit_pwd && remapped && !IS_ABSOLUTE_PATH (remapped))
228 : {
229 5432 : stream_pwd = true;
230 5432 : ob->emit_pwd = false;
231 : }
232 217046 : bp_pack_value (bp, stream_pwd, 1);
233 217046 : if (stream_pwd)
234 5432 : bp_pack_string (ob, bp, get_src_pwd (), true);
235 217046 : bp_pack_string (ob, bp, remapped, true);
236 217046 : bp_pack_value (bp, xloc.sysp, 1);
237 : }
238 4372454 : ob->current_file = xloc.file;
239 4372454 : ob->current_sysp = xloc.sysp;
240 :
241 4372454 : if (ob->current_line != xloc.line)
242 1417423 : bp_pack_var_len_unsigned (bp, xloc.line);
243 4372454 : ob->current_line = xloc.line;
244 :
245 4372454 : if (ob->current_col != xloc.column)
246 1700471 : bp_pack_var_len_unsigned (bp, xloc.column);
247 4372454 : ob->current_col = xloc.column;
248 :
249 4372454 : if (ob->current_discr != discr)
250 1317011 : bp_pack_var_len_unsigned (bp, discr);
251 4372454 : ob->current_discr = discr;
252 : }
253 : else
254 2408988 : bp_pack_int_in_range (bp, 0, RESERVED_LOCATION_COUNT + 1, loc);
255 :
256 6781442 : if (block_p)
257 : {
258 2962671 : tree block = LOCATION_BLOCK (orig_loc);
259 2962671 : bp_pack_value (bp, ob->current_block != block, 1);
260 2962671 : streamer_write_bitpack (bp);
261 2962671 : if (ob->current_block != block)
262 748202 : lto_output_tree (ob, block, true, true);
263 2962671 : ob->current_block = block;
264 : }
265 6781442 : }
266 :
267 : /* Output info about new location into bitpack BP.
268 : After outputting bitpack, lto_output_location_data has
269 : to be done to output actual data. */
270 :
271 : void
272 3818771 : lto_output_location (struct output_block *ob, struct bitpack_d *bp,
273 : location_t loc)
274 : {
275 3818771 : lto_output_location_1 (ob, bp, loc, false);
276 3818771 : }
277 :
278 : /* Output info about new location into bitpack BP.
279 : After outputting bitpack, lto_output_location_data has
280 : to be done to output actual data. Like lto_output_location, but
281 : additionally output LOCATION_BLOCK info too and write the BP bitpack. */
282 :
283 : void
284 2962671 : lto_output_location_and_block (struct output_block *ob, struct bitpack_d *bp,
285 : location_t loc)
286 : {
287 2962671 : lto_output_location_1 (ob, bp, loc, true);
288 2962671 : }
289 :
290 :
291 : /* Lookup NAME in ENCODER. If NAME is not found, create a new entry in
292 : ENCODER for NAME with the next available index of ENCODER, then
293 : print the index to OBS.
294 : Return the index. */
295 :
296 :
297 : static unsigned
298 7960643 : lto_get_index (struct lto_tree_ref_encoder *encoder, tree t)
299 : {
300 7960643 : bool existed_p;
301 :
302 7960643 : unsigned int &index
303 7960643 : = encoder->tree_hash_table->get_or_insert (t, &existed_p);
304 7960643 : if (!existed_p)
305 : {
306 2181521 : index = encoder->trees.length ();
307 2181521 : if (streamer_dump_file)
308 : {
309 176 : print_node_brief (streamer_dump_file, " Encoding indexable ",
310 : t, 4);
311 176 : fprintf (streamer_dump_file, " as %i \n", index);
312 : }
313 2181521 : encoder->trees.safe_push (t);
314 : }
315 :
316 7960643 : return index;
317 : }
318 :
319 :
320 : /* If EXPR is an indexable tree node, output a reference to it to
321 : output block OB. Otherwise, output the physical representation of
322 : EXPR to OB. */
323 :
324 : static void
325 9456499 : lto_indexable_tree_ref (struct output_block *ob, tree expr,
326 : enum LTO_tags *tag, unsigned *index)
327 : {
328 9456499 : gcc_checking_assert (tree_is_indexable (expr));
329 :
330 9456499 : if (TREE_CODE (expr) == SSA_NAME)
331 : {
332 2204829 : *tag = LTO_ssa_name_ref;
333 2204829 : *index = SSA_NAME_VERSION (expr);
334 : }
335 : else
336 : {
337 7251670 : *tag = LTO_global_stream_ref;
338 7251670 : *index = lto_get_index (&ob->decl_state->streams[LTO_DECL_STREAM], expr);
339 : }
340 9456499 : }
341 :
342 :
343 : /* Output a static or extern var DECL to OBS. */
344 :
345 : void
346 289048 : lto_output_var_decl_ref (struct lto_out_decl_state *decl_state,
347 : struct lto_output_stream * obs, tree decl)
348 : {
349 289048 : gcc_checking_assert (VAR_P (decl));
350 289048 : streamer_write_uhwi_stream
351 289048 : (obs, lto_get_index (&decl_state->streams[LTO_DECL_STREAM],
352 : decl));
353 289048 : }
354 :
355 :
356 : /* Output a static or extern var DECL to OBS. */
357 :
358 : void
359 419925 : lto_output_fn_decl_ref (struct lto_out_decl_state *decl_state,
360 : struct lto_output_stream * obs, tree decl)
361 : {
362 419925 : gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL);
363 419925 : streamer_write_uhwi_stream
364 419925 : (obs, lto_get_index (&decl_state->streams[LTO_DECL_STREAM], decl));
365 419925 : }
366 :
367 : /* Return true if EXPR is a tree node that can be written to disk. */
368 :
369 : static inline bool
370 7057289 : lto_is_streamable (tree expr)
371 : {
372 7057289 : enum tree_code code = TREE_CODE (expr);
373 :
374 : /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
375 : name version in lto_output_tree_ref (see output_ssa_names). */
376 7057289 : return !is_lang_specific (expr)
377 7057289 : && code != SSA_NAME
378 7057289 : && code != LANG_TYPE
379 : && code != MODIFY_EXPR
380 7057289 : && code != INIT_EXPR
381 7057289 : && code != TARGET_EXPR
382 7057289 : && code != BIND_EXPR
383 7057289 : && code != WITH_CLEANUP_EXPR
384 7057289 : && code != STATEMENT_LIST
385 7057289 : && (code == CASE_LABEL_EXPR
386 7057289 : || code == DECL_EXPR
387 7049398 : || code == ASM_EXPR
388 7049358 : || TREE_CODE_CLASS (code) != tcc_statement);
389 : }
390 :
391 : /* Very rough estimate of streaming size of the initializer. If we ignored
392 : presence of strings, we could simply just count number of non-indexable
393 : tree nodes and number of references to indexable nodes. Strings however
394 : may be very large and we do not want to dump them int othe global stream.
395 :
396 : Count the size of initializer until the size in DATA is positive. */
397 :
398 : static tree
399 100838 : subtract_estimated_size (tree *tp, int *ws, void *data)
400 : {
401 100838 : long *sum = (long *)data;
402 100838 : if (tree_is_indexable (*tp))
403 : {
404 : /* Indexable tree is one reference to global stream.
405 : Guess it may be about 4 bytes. */
406 1560 : *sum -= 4;
407 1560 : *ws = 0;
408 : }
409 : /* String table entry + base of tree node needs to be streamed. */
410 100838 : if (TREE_CODE (*tp) == STRING_CST)
411 7677 : *sum -= TREE_STRING_LENGTH (*tp) + 8;
412 : else
413 : {
414 : /* Identifiers are also variable length but should not appear
415 : naked in constructor. */
416 93161 : gcc_checking_assert (TREE_CODE (*tp) != IDENTIFIER_NODE);
417 : /* We do not really make attempt to work out size of pickled tree, as
418 : it is very variable. Make it bigger than the reference. */
419 93161 : *sum -= 16;
420 : }
421 100838 : if (*sum < 0)
422 32983 : return *tp;
423 : return NULL_TREE;
424 : }
425 :
426 :
427 : /* For EXPR lookup and return what we want to stream to OB as DECL_INITIAL. */
428 :
429 : static tree
430 2422690 : get_symbol_initial_value (lto_symtab_encoder_t encoder, tree expr)
431 : {
432 2422690 : gcc_checking_assert (DECL_P (expr)
433 : && TREE_CODE (expr) != FUNCTION_DECL
434 : && TREE_CODE (expr) != TRANSLATION_UNIT_DECL);
435 :
436 : /* Handle DECL_INITIAL for symbols. */
437 2422690 : tree initial = DECL_INITIAL (expr);
438 2422690 : if (VAR_P (expr)
439 1206994 : && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
440 859616 : && !DECL_IN_CONSTANT_POOL (expr)
441 3282174 : && initial)
442 : {
443 85401 : varpool_node *vnode;
444 : /* Extra section needs about 30 bytes; do not produce it for simple
445 : scalar values. */
446 85401 : if (!(vnode = varpool_node::get (expr))
447 85401 : || !lto_symtab_encoder_encode_initializer_p (encoder, vnode))
448 3231 : initial = error_mark_node;
449 85401 : if (initial != error_mark_node)
450 : {
451 68728 : long max_size = 30;
452 68728 : if (walk_tree (&initial, subtract_estimated_size, (void *)&max_size,
453 : NULL))
454 32983 : initial = error_mark_node;
455 : }
456 : }
457 :
458 2422690 : return initial;
459 : }
460 :
461 :
462 : /* Output reference to tree T to the stream.
463 : Assume that T is already in encoder cache.
464 : This is used to stream tree bodies where we know the DFS walk arranged
465 : everything to cache. Must be matched with stream_read_tree_ref. */
466 :
467 : void
468 31956327 : stream_write_tree_ref (struct output_block *ob, tree t)
469 : {
470 31956327 : if (!t)
471 12185549 : streamer_write_zero (ob);
472 : else
473 : {
474 19770778 : unsigned int ix;
475 19770778 : bool existed_p = streamer_tree_cache_lookup (ob->writer_cache, t, &ix);
476 19770778 : if (existed_p)
477 15332196 : streamer_write_hwi (ob, ix + 1);
478 : else
479 : {
480 4438582 : enum LTO_tags tag;
481 4438582 : unsigned ix;
482 4438582 : int id = 0;
483 :
484 4438582 : lto_indexable_tree_ref (ob, t, &tag, &ix);
485 4438582 : if (tag == LTO_ssa_name_ref)
486 : id = 1;
487 : else
488 4232914 : gcc_checking_assert (tag == LTO_global_stream_ref);
489 4438582 : streamer_write_hwi (ob, -(int)(ix * 2 + id + 1));
490 : }
491 : }
492 31956327 : }
493 :
494 :
495 :
496 : /* Write a physical representation of tree node EXPR to output block
497 : OB. If REF_P is true, the leaves of EXPR are emitted as references
498 : via lto_output_tree_ref. IX is the index into the streamer cache
499 : where EXPR is stored. */
500 :
501 : static void
502 7057289 : lto_write_tree_1 (struct output_block *ob, tree expr, bool ref_p)
503 : {
504 7057289 : if (streamer_dump_file)
505 : {
506 416 : print_node_brief (streamer_dump_file, " Streaming body of ",
507 : expr, 4);
508 416 : fprintf (streamer_dump_file, " to %s\n",
509 416 : lto_section_name[ob->section_type]);
510 : }
511 :
512 : /* Pack all the non-pointer fields in EXPR into a bitpack and write
513 : the resulting bitpack. */
514 7057289 : streamer_write_tree_bitfields (ob, expr);
515 :
516 : /* Write all the pointer fields in EXPR. */
517 7057289 : streamer_write_tree_body (ob, expr);
518 :
519 : /* Write any LTO-specific data to OB. */
520 7057289 : if (DECL_P (expr)
521 1530226 : && TREE_CODE (expr) != FUNCTION_DECL
522 1098561 : && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
523 : {
524 : /* Handle DECL_INITIAL for symbols. */
525 1066891 : tree initial = get_symbol_initial_value
526 1066891 : (ob->decl_state->symtab_node_encoder, expr);
527 1066891 : stream_write_tree (ob, initial, ref_p);
528 : }
529 :
530 : /* Stream references to early generated DIEs. Keep in sync with the
531 : trees handled in dwarf2out_die_ref_for_decl. */
532 7057289 : if ((DECL_P (expr)
533 : && TREE_CODE (expr) != FIELD_DECL
534 : && TREE_CODE (expr) != DEBUG_EXPR_DECL
535 : && TREE_CODE (expr) != TYPE_DECL)
536 5664545 : || TREE_CODE (expr) == BLOCK)
537 : {
538 1737305 : const char *sym;
539 1737305 : unsigned HOST_WIDE_INT off;
540 1737305 : if (debug_info_level > DINFO_LEVEL_NONE
541 1737305 : && debug_hooks->die_ref_for_decl (expr, &sym, &off))
542 : {
543 48999 : streamer_write_string (ob, ob->main_stream, sym, true);
544 48999 : streamer_write_uhwi (ob, off);
545 : }
546 : else
547 1688306 : streamer_write_string (ob, ob->main_stream, NULL, true);
548 : }
549 7057289 : }
550 :
551 : /* Write a physical representation of tree node EXPR to output block
552 : OB. If REF_P is true, the leaves of EXPR are emitted as references
553 : via lto_output_tree_ref. IX is the index into the streamer cache
554 : where EXPR is stored. */
555 :
556 : static void
557 6784131 : lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
558 : {
559 6784131 : if (!lto_is_streamable (expr))
560 0 : internal_error ("tree code %qs is not supported in LTO streams",
561 0 : get_tree_code_name (TREE_CODE (expr)));
562 :
563 : /* Write the header, containing everything needed to materialize
564 : EXPR on the reading side. */
565 6784131 : streamer_write_tree_header (ob, expr);
566 :
567 6784131 : lto_write_tree_1 (ob, expr, ref_p);
568 6784131 : }
569 :
570 : /* Emit the physical representation of tree node EXPR to output block OB,
571 : If THIS_REF_P is true, the leaves of EXPR are emitted as references via
572 : lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
573 :
574 : static void
575 7840724 : lto_output_tree_1 (struct output_block *ob, tree expr, hashval_t hash,
576 : bool ref_p, bool this_ref_p)
577 : {
578 7840724 : unsigned ix;
579 :
580 7840724 : gcc_checking_assert (expr != NULL_TREE
581 : && !(this_ref_p && tree_is_indexable (expr)));
582 :
583 7840724 : bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
584 : expr, hash, &ix);
585 7840724 : gcc_assert (!exists_p);
586 7840724 : if (TREE_CODE (expr) == INTEGER_CST
587 7840724 : && !TREE_OVERFLOW (expr))
588 : {
589 : /* Shared INTEGER_CST nodes are special because they need their
590 : original type to be materialized by the reader (to implement
591 : TYPE_CACHED_VALUES). */
592 1056593 : streamer_write_integer_cst (ob, expr);
593 : }
594 : else
595 : {
596 : /* This is the first time we see EXPR, write its fields
597 : to OB. */
598 6784131 : lto_write_tree (ob, expr, ref_p);
599 : }
600 7840724 : }
601 :
602 : class DFS
603 : {
604 : public:
605 : DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
606 : bool single_p);
607 : ~DFS ();
608 :
609 : struct scc_entry
610 : {
611 : tree t;
612 : hashval_t hash;
613 : };
614 : auto_vec<scc_entry,32> sccstack;
615 :
616 : private:
617 : struct sccs
618 : {
619 : unsigned int dfsnum;
620 : unsigned int low;
621 : };
622 : struct worklist
623 : {
624 : tree expr;
625 : sccs *from_state;
626 : sccs *cstate;
627 : bool ref_p;
628 : bool this_ref_p;
629 : };
630 : /* Maximum index of scc stack containing a local tree. */
631 : int max_local_entry;
632 :
633 : static int scc_entry_compare (const void *, const void *);
634 :
635 : void DFS_write_tree_body (struct output_block *ob,
636 : tree expr, sccs *expr_state, bool ref_p);
637 :
638 : void DFS_write_tree (struct output_block *ob, sccs *from_state,
639 : tree expr, bool ref_p, bool this_ref_p);
640 :
641 : hashval_t
642 : hash_scc (struct output_block *ob, unsigned first, unsigned size,
643 : bool ref_p, bool this_ref_p);
644 :
645 : hash_map<tree, sccs *> sccstate;
646 : auto_vec<worklist, 32> worklist_vec;
647 : struct obstack sccstate_obstack;
648 : };
649 :
650 : /* Return true if type can not be merged with structurally same tree in
651 : other translation unit. During stream out this information is propagated
652 : to all trees referring to T and they are not streamed with additional
653 : information needed by the tree merging in lto-common.cc (in particular,
654 : scc hash codes are not streamed).
655 :
656 : TRANSLATION_UNIT_DECL is handled specially since references to it does
657 : not make other trees local as well. */
658 :
659 : static bool
660 2856086 : local_tree_p (tree t)
661 : {
662 2856086 : switch (TREE_CODE (t))
663 : {
664 : case LABEL_DECL:
665 : return true;
666 1599 : case NAMESPACE_DECL:
667 1599 : return !DECL_NAME (t);
668 617729 : case VAR_DECL:
669 617729 : case FUNCTION_DECL:
670 617729 : return !TREE_PUBLIC (t) && !DECL_EXTERNAL (t);
671 73497 : case RECORD_TYPE:
672 73497 : case UNION_TYPE:
673 73497 : case ENUMERAL_TYPE:
674 : /* Anonymous namespace types are local.
675 : Only work hard for main variants;
676 : variant types will inherit locality. */
677 73497 : return TYPE_MAIN_VARIANT (t) == t
678 58849 : && odr_type_p (t) && type_with_linkage_p (t)
679 93216 : && type_in_anonymous_namespace_p (t);
680 : default:
681 : return false;
682 : }
683 : }
684 :
685 : /* Emit the physical representation of tree node EXPR to output block OB,
686 : using depth-first search on the subgraph. If THIS_REF_P is true, the
687 : leaves of EXPR are emitted as references via lto_output_tree_ref.
688 : REF_P is used for streaming siblings of EXPR. If SINGLE_P is true,
689 : this is for a rewalk of a single leaf SCC. */
690 :
691 2984490 : DFS::DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
692 2984490 : bool single_p)
693 : {
694 2984490 : unsigned int next_dfs_num = 1;
695 :
696 2984490 : max_local_entry = -1;
697 2984490 : gcc_obstack_init (&sccstate_obstack);
698 2984490 : DFS_write_tree (ob, NULL, expr, ref_p, this_ref_p);
699 21672833 : while (!worklist_vec.is_empty ())
700 : {
701 17759040 : worklist &w = worklist_vec.last ();
702 17759040 : expr = w.expr;
703 17759040 : sccs *from_state = w.from_state;
704 17759040 : sccs *cstate = w.cstate;
705 17759040 : ref_p = w.ref_p;
706 17759040 : this_ref_p = w.this_ref_p;
707 17759040 : if (cstate == NULL)
708 : {
709 9634268 : sccs **slot = &sccstate.get_or_insert (expr);
710 9634268 : cstate = *slot;
711 9634268 : if (cstate)
712 : {
713 1509496 : gcc_checking_assert (from_state);
714 1509496 : if (cstate->dfsnum < from_state->dfsnum)
715 190857 : from_state->low = MIN (cstate->dfsnum, from_state->low);
716 1509496 : worklist_vec.pop ();
717 1509496 : continue;
718 : }
719 :
720 8124772 : scc_entry e = { expr, 0 };
721 : /* Not yet visited. DFS recurse and push it onto the stack. */
722 8124772 : *slot = cstate = XOBNEW (&sccstate_obstack, struct sccs);
723 8124772 : if (ob->local_trees && local_tree_p (expr))
724 85592 : max_local_entry = sccstack.length ();
725 8124772 : sccstack.safe_push (e);
726 8124772 : cstate->dfsnum = next_dfs_num++;
727 8124772 : cstate->low = cstate->dfsnum;
728 8124772 : w.cstate = cstate;
729 :
730 8124772 : if (TREE_CODE (expr) == INTEGER_CST
731 8124772 : && !TREE_OVERFLOW (expr))
732 1100365 : DFS_write_tree (ob, cstate, TREE_TYPE (expr), ref_p, ref_p);
733 : else
734 : {
735 7024407 : DFS_write_tree_body (ob, expr, cstate, ref_p);
736 :
737 : /* Walk any LTO-specific edges. */
738 7024407 : if (DECL_P (expr)
739 1533910 : && TREE_CODE (expr) != FUNCTION_DECL
740 1102245 : && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
741 : {
742 : /* Handle DECL_INITIAL for symbols. */
743 1070575 : tree initial
744 1070575 : = get_symbol_initial_value (ob->decl_state->symtab_node_encoder,
745 : expr);
746 1070575 : DFS_write_tree (ob, cstate, initial, ref_p, ref_p);
747 : }
748 : }
749 8124772 : continue;
750 8124772 : }
751 :
752 : /* See if we found an SCC. */
753 8124772 : if (cstate->low == cstate->dfsnum)
754 : {
755 7922307 : unsigned first, size;
756 7922307 : tree x;
757 :
758 : /* If we are re-walking a single leaf SCC just pop it,
759 : let earlier worklist item access the sccstack. */
760 7922307 : if (single_p)
761 : {
762 343 : worklist_vec.pop ();
763 343 : continue;
764 : }
765 :
766 : /* Pop the SCC and compute its size. */
767 15843928 : first = sccstack.length ();
768 8113882 : do
769 : {
770 8113882 : x = sccstack[--first].t;
771 : }
772 8113882 : while (x != expr);
773 7921964 : size = sccstack.length () - first;
774 :
775 : /* No need to compute hashes for LTRANS units, we don't perform
776 : any merging there. */
777 7921964 : hashval_t scc_hash = 0;
778 7921964 : unsigned scc_entry_len = 0;
779 21114219 : bool local_to_unit = !ob->local_trees
780 7921964 : || max_local_entry >= (int)first;
781 :
782 : /* Remember that trees are local so info gets propagated to other
783 : SCCs. */
784 5270291 : if (local_to_unit && ob->local_trees)
785 : {
786 94774 : for (unsigned i = 0; i < size; ++i)
787 48791 : ob->local_trees->add (sccstack[first + i].t);
788 : }
789 :
790 : /* As a special case do not stream TRANSLATION_UNIT_DECL as shared
791 : tree. We can not mark it local because references to it does not
792 : make other trees local (all global decls reffer to it via
793 : CONTEXT). */
794 7921964 : if (size == 1
795 7921964 : && TREE_CODE (sccstack[first].t) == TRANSLATION_UNIT_DECL)
796 : local_to_unit = true;
797 :
798 7890294 : if (!local_to_unit)
799 : {
800 2628857 : scc_hash = hash_scc (ob, first, size, ref_p, this_ref_p);
801 :
802 : /* Put the entries with the least number of collisions first. */
803 2628857 : unsigned entry_start = 0;
804 2628857 : scc_entry_len = size + 1;
805 5402446 : for (unsigned i = 0; i < size;)
806 : {
807 2773589 : unsigned from = i;
808 2773589 : for (i = i + 1; i < size
809 2773589 : && (sccstack[first + i].hash
810 144732 : == sccstack[first + from].hash); ++i)
811 : ;
812 2773589 : if (i - from < scc_entry_len)
813 : {
814 2628857 : scc_entry_len = i - from;
815 2628857 : entry_start = from;
816 : }
817 : }
818 5257714 : for (unsigned i = 0; i < scc_entry_len; ++i)
819 2628857 : std::swap (sccstack[first + i],
820 2628857 : sccstack[first + entry_start + i]);
821 :
822 : /* We already sorted SCC deterministically in hash_scc. */
823 :
824 : /* Check that we have only one SCC.
825 : Naturally we may have conflicts if hash function is not
826 : strong enough. Lets see how far this gets. */
827 2628857 : gcc_checking_assert (scc_entry_len == 1);
828 : }
829 :
830 7921964 : worklist_vec.pop ();
831 :
832 7921964 : unsigned int prev_size = ob->main_stream->total_size;
833 :
834 : /* Only global decl sections are considered by tree merging. */
835 7921964 : if (ob->section_type != LTO_section_decls)
836 : {
837 : /* If this is the original tree we stream and it forms SCC
838 : by itself then we do not need to stream SCC at all. */
839 4502400 : if (worklist_vec.is_empty () && first == 0 && size == 1)
840 : return;
841 2447213 : if (streamer_dump_file)
842 : {
843 140 : fprintf (streamer_dump_file,
844 : " Start of LTO_trees of size %i\n", size);
845 : }
846 2447213 : streamer_write_record_start (ob, LTO_trees);
847 2447213 : streamer_write_uhwi (ob, size);
848 : }
849 : /* Write LTO_tree_scc if tree merging is going to be performed. */
850 3419564 : else if (!local_to_unit
851 : /* These are special since sharing is not done by tree
852 : merging machinery. We can not special case them earlier
853 : because we still need to compute hash for further sharing
854 : of trees referring to them. */
855 3419564 : && (size != 1
856 2562876 : || (TREE_CODE (sccstack[first].t) != IDENTIFIER_NODE
857 1690577 : && (TREE_CODE (sccstack[first].t) != INTEGER_CST
858 153655 : || TREE_OVERFLOW (sccstack[first].t)))))
859 :
860 : {
861 1602905 : gcc_checking_assert (ob->section_type == LTO_section_decls);
862 1602905 : if (streamer_dump_file)
863 : {
864 172 : fprintf (streamer_dump_file,
865 : " Start of LTO_tree_scc of size %i\n", size);
866 : }
867 1602905 : streamer_write_record_start (ob, LTO_tree_scc);
868 : /* In wast majority of cases scc_entry_len is 1 and size is small
869 : integer. Use extra bit of size to stream info about
870 : exceptions. */
871 1602905 : streamer_write_uhwi (ob, size * 2 + (scc_entry_len != 1));
872 1602905 : if (scc_entry_len != 1)
873 0 : streamer_write_uhwi (ob, scc_entry_len);
874 1602905 : streamer_write_uhwi (ob, scc_hash);
875 : }
876 : /* Non-trivial SCCs must be packed to trees blocks so forward
877 : references work correctly. */
878 1816659 : else if (size != 1)
879 : {
880 15125 : if (streamer_dump_file)
881 : {
882 0 : fprintf (streamer_dump_file,
883 : " Start of LTO_trees of size %i\n", size);
884 : }
885 15125 : streamer_write_record_start (ob, LTO_trees);
886 15125 : streamer_write_uhwi (ob, size);
887 : }
888 1801534 : else if (streamer_dump_file)
889 : {
890 108 : fprintf (streamer_dump_file, " Streaming single tree\n");
891 : }
892 :
893 : /* Write size-1 SCCs without wrapping them inside SCC bundles.
894 : All INTEGER_CSTs need to be handled this way as we need
895 : their type to materialize them. Also builtins are handled
896 : this way. */
897 5866777 : if (size == 1)
898 5785537 : lto_output_tree_1 (ob, expr, scc_hash, ref_p, this_ref_p);
899 : else
900 : {
901 :
902 : /* Write all headers and populate the streamer cache. */
903 354398 : for (unsigned i = 0; i < size; ++i)
904 : {
905 273158 : hashval_t hash = sccstack[first+i].hash;
906 273158 : tree t = sccstack[first+i].t;
907 273158 : bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
908 : t, hash, NULL);
909 273158 : gcc_assert (!exists_p);
910 :
911 273158 : if (!lto_is_streamable (t))
912 0 : internal_error ("tree code %qs is not supported "
913 : "in LTO streams",
914 0 : get_tree_code_name (TREE_CODE (t)));
915 :
916 : /* Write the header, containing everything needed to
917 : materialize EXPR on the reading side. */
918 273158 : streamer_write_tree_header (ob, t);
919 : }
920 :
921 : /* Write the bitpacks and tree references. */
922 354398 : for (unsigned i = 0; i < size; ++i)
923 273158 : lto_write_tree_1 (ob, sccstack[first+i].t, ref_p);
924 : }
925 5866777 : if (streamer_dump_file)
926 420 : fprintf (streamer_dump_file, " %u bytes\n",
927 420 : ob->main_stream->total_size - prev_size);
928 :
929 : /* Finally truncate the vector. */
930 5866777 : sccstack.truncate (first);
931 5866777 : if ((int)first <= max_local_entry)
932 45983 : max_local_entry = first - 1;
933 :
934 5866777 : if (from_state)
935 4937817 : from_state->low = MIN (from_state->low, cstate->low);
936 5866777 : continue;
937 5866777 : }
938 :
939 202465 : gcc_checking_assert (from_state);
940 202465 : from_state->low = MIN (from_state->low, cstate->low);
941 202465 : if (cstate->dfsnum < from_state->dfsnum)
942 0 : from_state->low = MIN (cstate->dfsnum, from_state->low);
943 202465 : worklist_vec.pop ();
944 : }
945 : }
946 :
947 2984490 : DFS::~DFS ()
948 : {
949 2984490 : obstack_free (&sccstate_obstack, NULL);
950 2984490 : }
951 :
952 : /* Handle the tree EXPR in the DFS walk with SCC state EXPR_STATE and
953 : DFS recurse for all tree edges originating from it. */
954 :
955 : void
956 7024407 : DFS::DFS_write_tree_body (struct output_block *ob,
957 : tree expr, sccs *expr_state, bool ref_p)
958 : {
959 : #define DFS_follow_tree_edge(DEST) \
960 : DFS_write_tree (ob, expr_state, DEST, ref_p, ref_p)
961 :
962 7024407 : enum tree_code code;
963 :
964 7024407 : code = TREE_CODE (expr);
965 :
966 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
967 : {
968 6618044 : if (TREE_CODE (expr) != IDENTIFIER_NODE)
969 5133665 : DFS_follow_tree_edge (TREE_TYPE (expr));
970 : }
971 :
972 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
973 : {
974 5770 : unsigned int count = vector_cst_encoded_nelts (expr);
975 23913 : for (unsigned int i = 0; i < count; ++i)
976 18143 : DFS_follow_tree_edge (VECTOR_CST_ENCODED_ELT (expr, i));
977 : }
978 :
979 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_POLY_INT_CST))
980 0 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
981 0 : DFS_follow_tree_edge (POLY_INT_CST_COEFF (expr, i));
982 :
983 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
984 : {
985 8007 : DFS_follow_tree_edge (TREE_REALPART (expr));
986 8007 : DFS_follow_tree_edge (TREE_IMAGPART (expr));
987 : }
988 :
989 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
990 : {
991 : /* Drop names that were created for anonymous entities. */
992 1533910 : if (DECL_NAME (expr)
993 1350559 : && TREE_CODE (DECL_NAME (expr)) == IDENTIFIER_NODE
994 2884469 : && IDENTIFIER_ANON_P (DECL_NAME (expr)))
995 : ;
996 : else
997 1533313 : DFS_follow_tree_edge (DECL_NAME (expr));
998 1533910 : if (TREE_CODE (expr) != TRANSLATION_UNIT_DECL
999 1533910 : && ! DECL_CONTEXT (expr))
1000 15164 : DFS_follow_tree_edge ((*all_translation_units)[0]);
1001 : else
1002 1518746 : DFS_follow_tree_edge (DECL_CONTEXT (expr));
1003 : }
1004 :
1005 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1006 : {
1007 1533910 : DFS_follow_tree_edge (DECL_SIZE (expr));
1008 1533910 : DFS_follow_tree_edge (DECL_SIZE_UNIT (expr));
1009 :
1010 : /* Note, DECL_INITIAL is not handled here. Since DECL_INITIAL needs
1011 : special handling in LTO, it must be handled by streamer hooks. */
1012 :
1013 1533910 : DFS_follow_tree_edge (DECL_ATTRIBUTES (expr));
1014 :
1015 : /* We use DECL_ABSTRACT_ORIGIN == error_mark_node to mark
1016 : declarations which should be eliminated by decl merging. Be sure none
1017 : leaks to this point. */
1018 1533910 : gcc_assert (DECL_ABSTRACT_ORIGIN (expr) != error_mark_node);
1019 1533910 : DFS_follow_tree_edge (DECL_ABSTRACT_ORIGIN (expr));
1020 :
1021 1533910 : if ((VAR_P (expr)
1022 1533910 : || TREE_CODE (expr) == PARM_DECL)
1023 1533910 : && DECL_HAS_VALUE_EXPR_P (expr))
1024 6526 : DFS_follow_tree_edge (DECL_VALUE_EXPR (expr));
1025 1533910 : if (VAR_P (expr)
1026 1533910 : && DECL_HAS_DEBUG_EXPR_P (expr))
1027 1502 : DFS_follow_tree_edge (DECL_DEBUG_EXPR (expr));
1028 : }
1029 :
1030 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1031 : {
1032 : /* Make sure we don't inadvertently set the assembler name. */
1033 922177 : if (DECL_ASSEMBLER_NAME_SET_P (expr))
1034 745205 : DFS_follow_tree_edge (DECL_ASSEMBLER_NAME (expr));
1035 : }
1036 :
1037 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1038 : {
1039 113213 : DFS_follow_tree_edge (DECL_FIELD_OFFSET (expr));
1040 113213 : DFS_follow_tree_edge (DECL_BIT_FIELD_TYPE (expr));
1041 113213 : DFS_follow_tree_edge (DECL_BIT_FIELD_REPRESENTATIVE (expr));
1042 113213 : DFS_follow_tree_edge (DECL_FIELD_BIT_OFFSET (expr));
1043 113213 : gcc_checking_assert (!DECL_FCONTEXT (expr));
1044 : }
1045 :
1046 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1047 : {
1048 431665 : gcc_checking_assert (DECL_VINDEX (expr) == NULL);
1049 431665 : DFS_follow_tree_edge (DECL_FUNCTION_PERSONALITY (expr));
1050 431665 : DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_TARGET (expr));
1051 431665 : DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr));
1052 : }
1053 :
1054 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1055 : {
1056 648443 : DFS_follow_tree_edge (TYPE_SIZE (expr));
1057 648443 : DFS_follow_tree_edge (TYPE_SIZE_UNIT (expr));
1058 648443 : DFS_follow_tree_edge (TYPE_ATTRIBUTES (expr));
1059 648443 : DFS_follow_tree_edge (TYPE_NAME (expr));
1060 : /* Do not follow TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
1061 : reconstructed during fixup. */
1062 : /* Do not follow TYPE_NEXT_VARIANT, we reconstruct the variant lists
1063 : during fixup. */
1064 648443 : DFS_follow_tree_edge (TYPE_MAIN_VARIANT (expr));
1065 648443 : DFS_follow_tree_edge (TYPE_CONTEXT (expr));
1066 : /* TYPE_CANONICAL is re-computed during type merging, so no need
1067 : to follow it here. */
1068 : /* Do not stream TYPE_STUB_DECL; it is not needed by LTO but currently
1069 : it cannot be freed by free_lang_data without triggering ICEs in
1070 : langhooks. */
1071 : }
1072 :
1073 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1074 : {
1075 648443 : if (TREE_CODE (expr) == ARRAY_TYPE)
1076 45909 : DFS_follow_tree_edge (TYPE_DOMAIN (expr));
1077 602534 : else if (RECORD_OR_UNION_TYPE_P (expr))
1078 243498 : for (tree t = TYPE_FIELDS (expr); t; t = TREE_CHAIN (t))
1079 149829 : DFS_follow_tree_edge (t);
1080 508865 : else if (FUNC_OR_METHOD_TYPE_P (expr))
1081 171101 : DFS_follow_tree_edge (TYPE_ARG_TYPES (expr));
1082 :
1083 648443 : if (!POINTER_TYPE_P (expr))
1084 391445 : DFS_follow_tree_edge (TYPE_MIN_VALUE_RAW (expr));
1085 648443 : DFS_follow_tree_edge (TYPE_MAX_VALUE_RAW (expr));
1086 : }
1087 :
1088 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1089 : {
1090 674186 : DFS_follow_tree_edge (TREE_PURPOSE (expr));
1091 674186 : DFS_follow_tree_edge (TREE_VALUE (expr));
1092 674186 : DFS_follow_tree_edge (TREE_CHAIN (expr));
1093 : }
1094 :
1095 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1096 : {
1097 10 : for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
1098 8 : DFS_follow_tree_edge (TREE_VEC_ELT (expr, i));
1099 : }
1100 :
1101 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1102 : {
1103 4186079 : for (int i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
1104 2460257 : DFS_follow_tree_edge (TREE_OPERAND (expr, i));
1105 1725822 : DFS_follow_tree_edge (TREE_BLOCK (expr));
1106 : }
1107 :
1108 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
1109 : {
1110 488906 : for (tree t = BLOCK_VARS (expr); t; t = TREE_CHAIN (t))
1111 : {
1112 : /* We would have to stream externals in the block chain as
1113 : non-references but we should have dropped them in
1114 : free-lang-data. */
1115 144345 : gcc_assert (!VAR_OR_FUNCTION_DECL_P (t) || !DECL_EXTERNAL (t));
1116 144345 : DFS_follow_tree_edge (t);
1117 : }
1118 :
1119 344561 : DFS_follow_tree_edge (BLOCK_SUPERCONTEXT (expr));
1120 344561 : DFS_follow_tree_edge (BLOCK_ABSTRACT_ORIGIN (expr));
1121 :
1122 : /* Do not follow BLOCK_NONLOCALIZED_VARS. We cannot handle debug
1123 : information for early inlined BLOCKs so drop it on the floor instead
1124 : of ICEing in dwarf2out.cc. */
1125 :
1126 : /* BLOCK_FRAGMENT_ORIGIN and BLOCK_FRAGMENT_CHAIN is not live at LTO
1127 : streaming time. */
1128 :
1129 : /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
1130 : list is re-constructed from BLOCK_SUPERCONTEXT. */
1131 : }
1132 :
1133 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1134 : {
1135 : unsigned i;
1136 : tree t;
1137 :
1138 : /* Note that the number of BINFO slots has already been emitted in
1139 : EXPR's header (see streamer_write_tree_header) because this length
1140 : is needed to build the empty BINFO node on the reader side. */
1141 20266 : FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (expr), i, t)
1142 10780 : DFS_follow_tree_edge (t);
1143 9486 : DFS_follow_tree_edge (BINFO_OFFSET (expr));
1144 9486 : DFS_follow_tree_edge (BINFO_VTABLE (expr));
1145 :
1146 : /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX,
1147 : BINFO_BASE_ACCESSES and BINFO_VPTR_INDEX; these are used
1148 : by C++ FE only. */
1149 : }
1150 :
1151 7024407 : if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1152 : {
1153 : unsigned i;
1154 : tree index, value;
1155 :
1156 769360 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
1157 : {
1158 532107 : DFS_follow_tree_edge (index);
1159 532107 : DFS_follow_tree_edge (value);
1160 : }
1161 : }
1162 :
1163 7024407 : if (code == RAW_DATA_CST)
1164 21 : DFS_follow_tree_edge (RAW_DATA_OWNER (expr));
1165 :
1166 7024407 : if (code == OMP_CLAUSE)
1167 : {
1168 : int i;
1169 494 : for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (expr)]; i++)
1170 280 : DFS_follow_tree_edge (OMP_CLAUSE_OPERAND (expr, i));
1171 214 : DFS_follow_tree_edge (OMP_CLAUSE_CHAIN (expr));
1172 : }
1173 :
1174 : #undef DFS_follow_tree_edge
1175 7024407 : }
1176 :
1177 : /* Return a hash value for the tree T.
1178 : CACHE holds hash values of trees outside current SCC. MAP, if non-NULL,
1179 : may hold hash values if trees inside current SCC. */
1180 :
1181 : static hashval_t
1182 2773589 : hash_tree (struct streamer_tree_cache_d *cache, hash_map<tree, hashval_t> *map, tree t)
1183 : {
1184 2773589 : inchash::hash hstate;
1185 :
1186 : #define visit(SIBLING) \
1187 : do { \
1188 : unsigned ix; \
1189 : if (!SIBLING) \
1190 : hstate.add_int (0); \
1191 : else if (streamer_tree_cache_lookup (cache, SIBLING, &ix)) \
1192 : hstate.add_int (streamer_tree_cache_get_hash (cache, ix)); \
1193 : else if (map) \
1194 : hstate.add_int (*map->get (SIBLING)); \
1195 : else \
1196 : hstate.add_int (1); \
1197 : } while (0)
1198 :
1199 : /* Hash TS_BASE. */
1200 2773589 : enum tree_code code = TREE_CODE (t);
1201 2773589 : hstate.add_int (code);
1202 2773589 : if (!TYPE_P (t))
1203 : {
1204 2322684 : hstate.add_flag (TREE_SIDE_EFFECTS (t));
1205 2322684 : hstate.add_flag (TREE_CONSTANT (t));
1206 2322684 : hstate.add_flag (TREE_READONLY (t));
1207 2322684 : hstate.add_flag (TREE_PUBLIC (t));
1208 : }
1209 2773589 : hstate.add_flag (TREE_ADDRESSABLE (t));
1210 2773589 : hstate.add_flag (TREE_THIS_VOLATILE (t));
1211 2773589 : if (DECL_P (t))
1212 681122 : hstate.add_flag (DECL_UNSIGNED (t));
1213 2092467 : else if (TYPE_P (t))
1214 450905 : hstate.add_flag (TYPE_UNSIGNED (t));
1215 2773589 : if (TYPE_P (t))
1216 450905 : hstate.add_flag (TYPE_ARTIFICIAL (t));
1217 : else
1218 2322684 : hstate.add_flag (TREE_NO_WARNING (t));
1219 2773589 : hstate.add_flag (TREE_NOTHROW (t));
1220 2773589 : hstate.add_flag (TREE_STATIC (t));
1221 2773589 : hstate.add_flag (TREE_PROTECTED (t));
1222 2773589 : hstate.add_flag (TREE_DEPRECATED (t));
1223 2773589 : if (code != TREE_BINFO)
1224 2767296 : hstate.add_flag (TREE_PRIVATE (t));
1225 2773589 : if (TYPE_P (t))
1226 : {
1227 450905 : hstate.add_flag (AGGREGATE_TYPE_P (t)
1228 450905 : ? TYPE_REVERSE_STORAGE_ORDER (t) : TYPE_SATURATING (t));
1229 450905 : hstate.add_flag (TYPE_ADDR_SPACE (t));
1230 : }
1231 2322684 : else if (code == SSA_NAME)
1232 0 : hstate.add_flag (SSA_NAME_IS_DEFAULT_DEF (t));
1233 2773589 : hstate.commit_flag ();
1234 :
1235 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
1236 183945 : hstate.add_wide_int (wi::to_widest (t));
1237 :
1238 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
1239 : {
1240 1714 : REAL_VALUE_TYPE r = TREE_REAL_CST (t);
1241 1714 : hstate.add_flag (r.cl);
1242 1714 : hstate.add_flag (r.sign);
1243 1714 : hstate.add_flag (r.signalling);
1244 1714 : hstate.add_flag (r.canonical);
1245 1714 : hstate.commit_flag ();
1246 1714 : hstate.add_int (r.uexp);
1247 1714 : hstate.add (r.sig, sizeof (r.sig));
1248 : }
1249 :
1250 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
1251 : {
1252 0 : FIXED_VALUE_TYPE f = TREE_FIXED_CST (t);
1253 0 : hstate.add_int (f.mode);
1254 0 : hstate.add_int (f.data.low);
1255 0 : hstate.add_int (f.data.high);
1256 : }
1257 :
1258 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1259 : {
1260 : /* Similar to TYPE_MODE, avoid streaming out host-specific DECL_MODE
1261 : for aggregate type with offloading enabled, and while streaming-in
1262 : recompute appropriate DECL_MODE for accelerator. */
1263 681122 : if (lto_stream_offload_p
1264 0 : && (VAR_P (t)
1265 0 : || TREE_CODE (t) == PARM_DECL
1266 0 : || TREE_CODE (t) == FIELD_DECL)
1267 681122 : && AGGREGATE_TYPE_P (TREE_TYPE (t)))
1268 0 : hstate.add_hwi (VOIDmode);
1269 : else
1270 681122 : hstate.add_hwi (DECL_MODE (t));
1271 681122 : hstate.add_flag (DECL_NONLOCAL (t));
1272 681122 : hstate.add_flag (DECL_VIRTUAL_P (t));
1273 681122 : hstate.add_flag (DECL_IGNORED_P (t));
1274 681122 : hstate.add_flag (DECL_ABSTRACT_P (t));
1275 681122 : hstate.add_flag (DECL_ARTIFICIAL (t));
1276 681122 : hstate.add_flag (DECL_USER_ALIGN (t));
1277 681122 : hstate.add_flag (DECL_PRESERVE_P (t));
1278 681122 : hstate.add_flag (DECL_EXTERNAL (t));
1279 681122 : hstate.add_flag (DECL_NOT_GIMPLE_REG_P (t));
1280 681122 : hstate.commit_flag ();
1281 681122 : hstate.add_int (DECL_ALIGN (t));
1282 681122 : if (code == LABEL_DECL)
1283 : {
1284 0 : hstate.add_int (EH_LANDING_PAD_NR (t));
1285 0 : hstate.add_int (LABEL_DECL_UID (t));
1286 : }
1287 681122 : else if (code == FIELD_DECL)
1288 : {
1289 81397 : hstate.add_flag (DECL_PACKED (t));
1290 81397 : hstate.add_flag (DECL_NONADDRESSABLE_P (t));
1291 81397 : hstate.add_flag (DECL_PADDING_P (t));
1292 81397 : if (DECL_BIT_FIELD (t))
1293 4932 : hstate.add_flag (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (t));
1294 : else
1295 156715 : hstate.add_flag (DECL_FIELD_ABI_IGNORED (t));
1296 81397 : hstate.add_int (DECL_OFFSET_ALIGN (t));
1297 : }
1298 599725 : else if (code == VAR_DECL)
1299 : {
1300 234882 : hstate.add_flag (DECL_HAS_DEBUG_EXPR_P (t));
1301 234882 : hstate.add_flag (DECL_NONLOCAL_FRAME (t));
1302 : }
1303 681122 : if (code == RESULT_DECL
1304 681122 : || code == PARM_DECL
1305 : || code == VAR_DECL)
1306 : {
1307 234894 : hstate.add_flag (DECL_BY_REFERENCE (t));
1308 234894 : if (code == VAR_DECL
1309 234894 : || code == PARM_DECL)
1310 234894 : hstate.add_flag (DECL_HAS_VALUE_EXPR_P (t));
1311 : }
1312 681122 : hstate.commit_flag ();
1313 : }
1314 :
1315 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
1316 599721 : hstate.add_int (DECL_REGISTER (t));
1317 :
1318 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1319 : {
1320 599709 : hstate.add_flag (DECL_COMMON (t));
1321 599709 : hstate.add_flag (DECL_DLLIMPORT_P (t));
1322 599709 : hstate.add_flag (DECL_WEAK (t));
1323 599709 : hstate.add_flag (DECL_SEEN_IN_BIND_EXPR_P (t));
1324 599709 : hstate.add_flag (DECL_COMDAT (t));
1325 599709 : hstate.add_flag (DECL_VISIBILITY_SPECIFIED (t));
1326 599709 : hstate.add_int (DECL_VISIBILITY (t));
1327 599709 : if (code == VAR_DECL)
1328 : {
1329 : /* DECL_IN_TEXT_SECTION is set during final asm output only. */
1330 234882 : hstate.add_flag (DECL_HARD_REGISTER (t));
1331 234882 : hstate.add_flag (DECL_IN_CONSTANT_POOL (t));
1332 : }
1333 599709 : if (TREE_CODE (t) == FUNCTION_DECL)
1334 : {
1335 342757 : hstate.add_flag (DECL_FINAL_P (t));
1336 342757 : hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (t));
1337 342757 : hstate.add_flag (DECL_CXX_DESTRUCTOR_P (t));
1338 : }
1339 599709 : hstate.commit_flag ();
1340 : }
1341 :
1342 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1343 : {
1344 342757 : hstate.add_int (DECL_BUILT_IN_CLASS (t));
1345 342757 : hstate.add_flag (DECL_STATIC_CONSTRUCTOR (t));
1346 342757 : hstate.add_flag (DECL_STATIC_DESTRUCTOR (t));
1347 342757 : hstate.add_int ((unsigned)FUNCTION_DECL_DECL_TYPE (t));
1348 342757 : hstate.add_flag (DECL_UNINLINABLE (t));
1349 342757 : hstate.add_flag (DECL_POSSIBLY_INLINED (t));
1350 342757 : hstate.add_flag (DECL_IS_NOVOPS (t));
1351 342757 : hstate.add_flag (DECL_IS_RETURNS_TWICE (t));
1352 342757 : hstate.add_flag (DECL_IS_MALLOC (t));
1353 342757 : hstate.add_flag (DECL_DECLARED_INLINE_P (t));
1354 342757 : hstate.add_flag (DECL_STATIC_CHAIN (t));
1355 342757 : hstate.add_flag (DECL_NO_INLINE_WARNING_P (t));
1356 342757 : hstate.add_flag (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (t));
1357 342757 : hstate.add_flag (DECL_NO_LIMIT_STACK (t));
1358 342757 : hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (t));
1359 342757 : hstate.add_flag (DECL_PURE_P (t));
1360 342757 : hstate.add_flag (DECL_LOOPING_CONST_OR_PURE_P (t));
1361 342757 : hstate.commit_flag ();
1362 342757 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
1363 21981 : hstate.add_int (DECL_UNCHECKED_FUNCTION_CODE (t));
1364 : }
1365 :
1366 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1367 : {
1368 : /* For offloading, avoid streaming out TYPE_MODE for aggregate type since
1369 : it may be host-specific. For eg, aarch64 uses OImode for ARRAY_TYPE
1370 : whose size is 256-bits, which is not representable on accelerator.
1371 : Instead stream out VOIDmode, and while streaming-in, recompute
1372 : appropriate TYPE_MODE for accelerator. */
1373 450905 : if (lto_stream_offload_p
1374 0 : && (AGGREGATE_TYPE_P (t) || VECTOR_TYPE_P (t)))
1375 0 : hstate.add_hwi (VOIDmode);
1376 : /* for VECTOR_TYPE, TYPE_MODE reevaluates the mode using target_flags
1377 : not necessary valid in a global context.
1378 : Use the raw value previously set by layout_type. */
1379 : else
1380 450905 : hstate.add_hwi (TYPE_MODE_RAW (t));
1381 : /* TYPE_NO_FORCE_BLK is private to stor-layout and need
1382 : no streaming. */
1383 450905 : hstate.add_flag (TYPE_PACKED (t));
1384 450905 : hstate.add_flag (TYPE_RESTRICT (t));
1385 450905 : hstate.add_flag (TYPE_USER_ALIGN (t));
1386 450905 : hstate.add_flag (TYPE_READONLY (t));
1387 450905 : if (RECORD_OR_UNION_TYPE_P (t))
1388 : {
1389 70797 : hstate.add_flag (TYPE_TRANSPARENT_AGGR (t));
1390 70797 : hstate.add_flag (TYPE_FINAL_P (t));
1391 70797 : hstate.add_flag (TYPE_CXX_ODR_P (t));
1392 : }
1393 380108 : else if (code == ARRAY_TYPE)
1394 28936 : hstate.add_flag (TYPE_NONALIASED_COMPONENT (t));
1395 450905 : if (code == ARRAY_TYPE || code == INTEGER_TYPE)
1396 73256 : hstate.add_flag (TYPE_STRING_FLAG (t));
1397 450905 : if (AGGREGATE_TYPE_P (t))
1398 99733 : hstate.add_flag (TYPE_TYPELESS_STORAGE (t));
1399 450905 : hstate.commit_flag ();
1400 450905 : hstate.add_int (TYPE_PRECISION_RAW (t));
1401 450905 : hstate.add_int (TYPE_ALIGN (t));
1402 450905 : if (!lto_stream_offload_p)
1403 450905 : hstate.add_int (TYPE_EMPTY_P (t));
1404 : }
1405 :
1406 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
1407 0 : hstate.add (TRANSLATION_UNIT_LANGUAGE (t),
1408 0 : strlen (TRANSLATION_UNIT_LANGUAGE (t)));
1409 :
1410 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION)
1411 : /* We don't stream these when passing things to a different target. */
1412 22529 : && !lto_stream_offload_p)
1413 22529 : hstate.add_hwi (cl_target_option_hash (TREE_TARGET_OPTION (t)));
1414 :
1415 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
1416 22741 : hstate.add_hwi (cl_optimization_hash (TREE_OPTIMIZATION (t)));
1417 :
1418 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
1419 872299 : hstate.merge_hash (IDENTIFIER_HASH_VALUE (t));
1420 :
1421 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_STRING))
1422 4221 : hstate.add (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
1423 :
1424 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
1425 : {
1426 2728319 : if (code != IDENTIFIER_NODE)
1427 1856020 : visit (TREE_TYPE (t));
1428 : }
1429 :
1430 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
1431 : {
1432 0 : unsigned int count = vector_cst_encoded_nelts (t);
1433 0 : for (unsigned int i = 0; i < count; ++i)
1434 0 : visit (VECTOR_CST_ENCODED_ELT (t, i));
1435 : }
1436 :
1437 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_POLY_INT_CST))
1438 0 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1439 0 : visit (POLY_INT_CST_COEFF (t, i));
1440 :
1441 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
1442 : {
1443 0 : visit (TREE_REALPART (t));
1444 0 : visit (TREE_IMAGPART (t));
1445 : }
1446 :
1447 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
1448 : {
1449 : /* Drop names that were created for anonymous entities. */
1450 681122 : if (DECL_NAME (t)
1451 663914 : && TREE_CODE (DECL_NAME (t)) == IDENTIFIER_NODE
1452 1345036 : && IDENTIFIER_ANON_P (DECL_NAME (t)))
1453 : ;
1454 : else
1455 680779 : visit (DECL_NAME (t));
1456 681122 : if (DECL_FILE_SCOPE_P (t))
1457 : ;
1458 : else
1459 109741 : visit (DECL_CONTEXT (t));
1460 : }
1461 :
1462 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1463 : {
1464 681122 : visit (DECL_SIZE (t));
1465 681122 : visit (DECL_SIZE_UNIT (t));
1466 681122 : visit (DECL_ATTRIBUTES (t));
1467 681122 : if ((code == VAR_DECL
1468 681122 : || code == PARM_DECL)
1469 681122 : && DECL_HAS_VALUE_EXPR_P (t))
1470 20 : visit (DECL_VALUE_EXPR (t));
1471 681122 : if (code == VAR_DECL
1472 916004 : && DECL_HAS_DEBUG_EXPR_P (t))
1473 0 : visit (DECL_DEBUG_EXPR (t));
1474 : /* ??? Hash DECL_INITIAL as streamed. Needs the output-block to
1475 : be able to call get_symbol_initial_value. */
1476 : }
1477 :
1478 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1479 : {
1480 599709 : if (DECL_ASSEMBLER_NAME_SET_P (t))
1481 597382 : visit (DECL_ASSEMBLER_NAME (t));
1482 : }
1483 :
1484 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1485 : {
1486 81397 : visit (DECL_FIELD_OFFSET (t));
1487 81397 : visit (DECL_BIT_FIELD_TYPE (t));
1488 81397 : visit (DECL_BIT_FIELD_REPRESENTATIVE (t));
1489 81397 : visit (DECL_FIELD_BIT_OFFSET (t));
1490 : }
1491 :
1492 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1493 : {
1494 342757 : visit (DECL_FUNCTION_PERSONALITY (t));
1495 342757 : visit (DECL_FUNCTION_SPECIFIC_TARGET (t));
1496 342757 : visit (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t));
1497 : }
1498 :
1499 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1500 : {
1501 450905 : visit (TYPE_SIZE (t));
1502 450905 : visit (TYPE_SIZE_UNIT (t));
1503 450905 : visit (TYPE_ATTRIBUTES (t));
1504 450905 : visit (TYPE_NAME (t));
1505 450905 : visit (TYPE_MAIN_VARIANT (t));
1506 450905 : if (TYPE_FILE_SCOPE_P (t))
1507 : ;
1508 : else
1509 15268 : visit (TYPE_CONTEXT (t));
1510 : }
1511 :
1512 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1513 : {
1514 450905 : if (code == ARRAY_TYPE)
1515 28936 : visit (TYPE_DOMAIN (t));
1516 421969 : else if (RECORD_OR_UNION_TYPE_P (t))
1517 175471 : for (tree f = TYPE_FIELDS (t); f; f = TREE_CHAIN (f))
1518 104674 : visit (f);
1519 351172 : else if (code == FUNCTION_TYPE
1520 351172 : || code == METHOD_TYPE)
1521 111348 : visit (TYPE_ARG_TYPES (t));
1522 450905 : if (!POINTER_TYPE_P (t))
1523 264986 : visit (TYPE_MIN_VALUE_RAW (t));
1524 450905 : visit (TYPE_MAX_VALUE_RAW (t));
1525 : }
1526 :
1527 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1528 : {
1529 519775 : visit (TREE_PURPOSE (t));
1530 519775 : visit (TREE_VALUE (t));
1531 519775 : visit (TREE_CHAIN (t));
1532 : }
1533 :
1534 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1535 0 : for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
1536 0 : visit (TREE_VEC_ELT (t, i));
1537 :
1538 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1539 : {
1540 7517 : hstate.add_hwi (TREE_OPERAND_LENGTH (t));
1541 18838 : for (int i = 0; i < TREE_OPERAND_LENGTH (t); ++i)
1542 11321 : visit (TREE_OPERAND (t, i));
1543 : }
1544 :
1545 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1546 : {
1547 : unsigned i;
1548 : tree b;
1549 12680 : FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t), i, b)
1550 6387 : visit (b);
1551 6293 : visit (BINFO_OFFSET (t));
1552 6293 : visit (BINFO_VTABLE (t));
1553 : /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1554 : BINFO_BASE_ACCESSES and BINFO_VPTR_INDEX; these are used
1555 : by C++ FE only. */
1556 : }
1557 :
1558 2773589 : if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1559 : {
1560 388 : unsigned i;
1561 388 : tree index, value;
1562 591 : hstate.add_hwi (CONSTRUCTOR_NELTS (t));
1563 1730 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
1564 : {
1565 1342 : visit (index);
1566 1342 : visit (value);
1567 : }
1568 : }
1569 :
1570 2773589 : if (code == OMP_CLAUSE)
1571 : {
1572 134 : int i;
1573 134 : HOST_WIDE_INT val;
1574 :
1575 134 : hstate.add_hwi (OMP_CLAUSE_CODE (t));
1576 134 : switch (OMP_CLAUSE_CODE (t))
1577 : {
1578 0 : case OMP_CLAUSE_DEFAULT:
1579 0 : val = OMP_CLAUSE_DEFAULT_KIND (t);
1580 0 : break;
1581 0 : case OMP_CLAUSE_SCHEDULE:
1582 0 : val = OMP_CLAUSE_SCHEDULE_KIND (t);
1583 0 : break;
1584 0 : case OMP_CLAUSE_DEPEND:
1585 0 : val = OMP_CLAUSE_DEPEND_KIND (t);
1586 0 : break;
1587 0 : case OMP_CLAUSE_DOACROSS:
1588 0 : val = OMP_CLAUSE_DOACROSS_KIND (t);
1589 0 : break;
1590 0 : case OMP_CLAUSE_MAP:
1591 0 : val = OMP_CLAUSE_MAP_KIND (t);
1592 0 : break;
1593 0 : case OMP_CLAUSE_PROC_BIND:
1594 0 : val = OMP_CLAUSE_PROC_BIND_KIND (t);
1595 0 : break;
1596 0 : case OMP_CLAUSE_REDUCTION:
1597 0 : case OMP_CLAUSE_TASK_REDUCTION:
1598 0 : case OMP_CLAUSE_IN_REDUCTION:
1599 0 : val = OMP_CLAUSE_REDUCTION_CODE (t);
1600 0 : break;
1601 : default:
1602 : val = 0;
1603 : break;
1604 : }
1605 134 : hstate.add_hwi (val);
1606 302 : for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t)]; i++)
1607 168 : visit (OMP_CLAUSE_OPERAND (t, i));
1608 134 : visit (OMP_CLAUSE_CHAIN (t));
1609 : }
1610 :
1611 2773589 : return hstate.end ();
1612 :
1613 : #undef visit
1614 : }
1615 :
1616 : /* Compare two SCC entries by their hash value for qsorting them. */
1617 :
1618 : int
1619 1553543 : DFS::scc_entry_compare (const void *p1_, const void *p2_)
1620 : {
1621 1553543 : const scc_entry *p1 = (const scc_entry *) p1_;
1622 1553543 : const scc_entry *p2 = (const scc_entry *) p2_;
1623 1553543 : if (p1->hash < p2->hash)
1624 : return -1;
1625 701438 : else if (p1->hash > p2->hash)
1626 680009 : return 1;
1627 : return 0;
1628 : }
1629 :
1630 : /* Return a hash value for the SCC on the SCC stack from FIRST with SIZE.
1631 : THIS_REF_P and REF_P are as passed to lto_output_tree for FIRST. */
1632 :
1633 : hashval_t
1634 2628857 : DFS::hash_scc (struct output_block *ob, unsigned first, unsigned size,
1635 : bool ref_p, bool this_ref_p)
1636 : {
1637 2628857 : unsigned int last_classes = 0, iterations = 0;
1638 :
1639 : /* Compute hash values for the SCC members. */
1640 5402446 : for (unsigned i = 0; i < size; ++i)
1641 2773589 : sccstack[first+i].hash
1642 5547178 : = hash_tree (ob->writer_cache, NULL, sccstack[first+i].t);
1643 :
1644 2628857 : if (size == 1)
1645 2562876 : return sccstack[first].hash;
1646 :
1647 : /* We aim to get unique hash for every tree within SCC and compute hash value
1648 : of the whole SCC by combining all values together in a stable (entry-point
1649 : independent) order. This guarantees that the same SCC regions within
1650 : different translation units will get the same hash values and therefore
1651 : will be merged at WPA time.
1652 :
1653 : Often the hashes are already unique. In that case we compute the SCC hash
1654 : by combining individual hash values in an increasing order.
1655 :
1656 : If there are duplicates, we seek at least one tree with unique hash (and
1657 : pick one with minimal hash and this property). Then we obtain a stable
1658 : order by DFS walk starting from this unique tree and then use the index
1659 : within this order to make individual hash values unique.
1660 :
1661 : If there is no tree with unique hash, we iteratively propagate the hash
1662 : values across the internal edges of SCC. This usually quickly leads
1663 : to unique hashes. Consider, for example, an SCC containing two pointers
1664 : that are identical except for the types they point to and assume that
1665 : these types are also part of the SCC. The propagation will add the
1666 : points-to type information into their hash values. */
1667 65981 : do
1668 : {
1669 : /* Sort the SCC so we can easily check for uniqueness. */
1670 65981 : qsort (&sccstack[first], size, sizeof (scc_entry), scc_entry_compare);
1671 :
1672 65981 : unsigned int classes = 1;
1673 65981 : int firstunique = -1;
1674 :
1675 : /* Find the tree with lowest unique hash (if it exists) and compute
1676 : the number of equivalence classes. */
1677 65981 : if (sccstack[first].hash != sccstack[first+1].hash)
1678 65978 : firstunique = 0;
1679 210713 : for (unsigned i = 1; i < size; ++i)
1680 144732 : if (sccstack[first+i-1].hash != sccstack[first+i].hash)
1681 : {
1682 142769 : classes++;
1683 142769 : if (firstunique == -1
1684 142769 : && (i == size - 1
1685 3 : || sccstack[first+i+1].hash != sccstack[first+i].hash))
1686 3 : firstunique = i;
1687 : }
1688 :
1689 : /* If we found a tree with unique hash, stop the iteration. */
1690 65981 : if (firstunique != -1
1691 : /* Also terminate if we run out of iterations or if the number of
1692 : equivalence classes is no longer increasing.
1693 : For example a cyclic list of trees that are all equivalent will
1694 : never have unique entry point; we however do not build such SCCs
1695 : in our IL. */
1696 65981 : || classes <= last_classes || iterations > 16)
1697 : {
1698 65981 : hashval_t scc_hash;
1699 :
1700 : /* If some hashes are not unique (CLASSES != SIZE), use the DFS walk
1701 : starting from FIRSTUNIQUE to obtain a stable order. */
1702 65981 : if (classes != size && firstunique != -1)
1703 : {
1704 343 : hash_map <tree, hashval_t> map(size*2);
1705 :
1706 : /* Store hash values into a map, so we can associate them with
1707 : the reordered SCC. */
1708 11233 : for (unsigned i = 0; i < size; ++i)
1709 10890 : map.put (sccstack[first+i].t, sccstack[first+i].hash);
1710 :
1711 686 : DFS again (ob, sccstack[first+firstunique].t, ref_p, this_ref_p,
1712 343 : true);
1713 686 : gcc_assert (again.sccstack.length () == size);
1714 :
1715 343 : memcpy (sccstack.address () + first,
1716 343 : again.sccstack.address (),
1717 : sizeof (scc_entry) * size);
1718 :
1719 : /* Update hash values of individual members by hashing in the
1720 : index within the stable order. This ensures uniqueness.
1721 : Also compute the SCC hash by mixing in all hash values in
1722 : the stable order we obtained. */
1723 343 : sccstack[first].hash = *map.get (sccstack[first].t);
1724 343 : scc_hash = sccstack[first].hash;
1725 10890 : for (unsigned i = 1; i < size; ++i)
1726 : {
1727 31641 : sccstack[first+i].hash
1728 10547 : = iterative_hash_hashval_t (i,
1729 10547 : *map.get (sccstack[first+i].t));
1730 10547 : scc_hash
1731 10547 : = iterative_hash_hashval_t (scc_hash,
1732 10547 : sccstack[first+i].hash);
1733 : }
1734 343 : }
1735 : /* If we got a unique hash value for each tree, then sort already
1736 : ensured entry-point independent order. Only compute the final
1737 : SCC hash.
1738 :
1739 : If we failed to find the unique entry point, we go by the same
1740 : route. We will eventually introduce unwanted hash conflicts. */
1741 : else
1742 : {
1743 : scc_hash = sccstack[first].hash;
1744 199823 : for (unsigned i = 1; i < size; ++i)
1745 134185 : scc_hash
1746 134185 : = iterative_hash_hashval_t (scc_hash, sccstack[first+i].hash);
1747 :
1748 : /* We cannot 100% guarantee that the hash won't conflict so as
1749 : to make it impossible to find a unique hash. This however
1750 : should be an extremely rare case. ICE for now so possible
1751 : issues are found and evaluated. */
1752 65638 : gcc_checking_assert (classes == size);
1753 : }
1754 :
1755 : /* To avoid conflicts across SCCs, iteratively hash the whole SCC
1756 : hash into the hash of each element. */
1757 276694 : for (unsigned i = 0; i < size; ++i)
1758 421426 : sccstack[first+i].hash
1759 210713 : = iterative_hash_hashval_t (sccstack[first+i].hash, scc_hash);
1760 65981 : return scc_hash;
1761 : }
1762 :
1763 0 : last_classes = classes;
1764 0 : iterations++;
1765 :
1766 : /* We failed to identify the entry point; propagate hash values across
1767 : the edges. */
1768 0 : hash_map <tree, hashval_t> map(size*2);
1769 :
1770 0 : for (unsigned i = 0; i < size; ++i)
1771 0 : map.put (sccstack[first+i].t, sccstack[first+i].hash);
1772 :
1773 0 : for (unsigned i = 0; i < size; i++)
1774 0 : sccstack[first+i].hash
1775 0 : = hash_tree (ob->writer_cache, &map, sccstack[first+i].t);
1776 0 : }
1777 : while (true);
1778 : }
1779 :
1780 : /* DFS walk EXPR and stream SCCs of tree bodies if they are not
1781 : already in the streamer cache. Main routine called for
1782 : each visit of EXPR. */
1783 :
1784 : void
1785 35461173 : DFS::DFS_write_tree (struct output_block *ob, sccs *from_state,
1786 : tree expr, bool ref_p, bool this_ref_p)
1787 : {
1788 : /* Handle special cases. */
1789 35461173 : if (expr == NULL_TREE)
1790 25826905 : return;
1791 :
1792 : /* Do not DFS walk into indexable trees. */
1793 23171214 : if (this_ref_p && tree_is_indexable (expr))
1794 : return;
1795 :
1796 : /* Check if we already streamed EXPR. */
1797 16900633 : if (streamer_tree_cache_lookup (ob->writer_cache, expr, NULL))
1798 : {
1799 : /* Reference to a local tree makes entry also local. We always process
1800 : top of stack entry, so set max to number of entries in stack - 1. */
1801 7266365 : if (ob->local_trees
1802 7266365 : && ob->local_trees->contains (expr))
1803 17070 : max_local_entry = sccstack.length () - 1;
1804 7266365 : return;
1805 : }
1806 :
1807 9634268 : worklist w;
1808 9634268 : w.expr = expr;
1809 9634268 : w.from_state = from_state;
1810 9634268 : w.cstate = NULL;
1811 9634268 : w.ref_p = ref_p;
1812 9634268 : w.this_ref_p = this_ref_p;
1813 9634268 : worklist_vec.safe_push (w);
1814 : }
1815 :
1816 :
1817 : /* Emit the physical representation of tree node EXPR to output block OB.
1818 : If THIS_REF_P is true, the leaves of EXPR are emitted as references via
1819 : lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
1820 :
1821 : void
1822 12770706 : lto_output_tree (struct output_block *ob, tree expr,
1823 : bool ref_p, bool this_ref_p)
1824 : {
1825 12770706 : unsigned ix;
1826 12770706 : bool existed_p;
1827 12770706 : unsigned int size = ob->main_stream->total_size;
1828 : /* This is the first time we see EXPR, write all reachable
1829 : trees to OB. */
1830 12770706 : static bool in_dfs_walk;
1831 :
1832 12770706 : if (expr == NULL_TREE)
1833 : {
1834 2865553 : streamer_write_record_start (ob, LTO_null);
1835 10749023 : return;
1836 : }
1837 :
1838 9905153 : if (this_ref_p && tree_is_indexable (expr))
1839 : {
1840 5017917 : enum LTO_tags tag;
1841 5017917 : unsigned ix;
1842 :
1843 5017917 : lto_indexable_tree_ref (ob, expr, &tag, &ix);
1844 5017917 : streamer_write_record_start (ob, tag);
1845 5017917 : streamer_write_uhwi (ob, ix);
1846 5017917 : return;
1847 : }
1848 :
1849 4887236 : existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1850 4887236 : if (existed_p)
1851 : {
1852 1903089 : if (streamer_dump_file)
1853 : {
1854 88 : if (in_dfs_walk)
1855 8 : print_node_brief (streamer_dump_file, " Streaming ref to ",
1856 : expr, 4);
1857 : else
1858 80 : print_node_brief (streamer_dump_file, " Streaming ref to ",
1859 : expr, 4);
1860 88 : fprintf (streamer_dump_file, "\n");
1861 : }
1862 : /* If a node has already been streamed out, make sure that
1863 : we don't write it more than once. Otherwise, the reader
1864 : will instantiate two different nodes for the same object. */
1865 1903089 : streamer_write_record_start (ob, LTO_tree_pickle_reference);
1866 1903089 : streamer_write_uhwi (ob, ix);
1867 1903089 : lto_stats.num_pickle_refs_output++;
1868 : }
1869 : else
1870 : {
1871 : /* Protect against recursion which means disconnect between
1872 : what tree edges we walk in the DFS walk and what edges
1873 : we stream out. */
1874 2984147 : gcc_assert (!in_dfs_walk);
1875 :
1876 2984147 : if (streamer_dump_file)
1877 : {
1878 168 : print_node_brief (streamer_dump_file, " Streaming tree ",
1879 : expr, 4);
1880 168 : fprintf (streamer_dump_file, "\n");
1881 : }
1882 :
1883 : /* Start the DFS walk. */
1884 : /* Save ob state ... */
1885 : /* let's see ... */
1886 2984147 : in_dfs_walk = true;
1887 2984147 : DFS (ob, expr, ref_p, this_ref_p, false);
1888 :
1889 : /* Finally append a reference to the tree we were writing. */
1890 2984147 : existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1891 :
1892 : /* DFS walk above possibly skipped streaming EXPR itself to let us inline
1893 : it. */
1894 2984147 : if (!existed_p)
1895 2055187 : lto_output_tree_1 (ob, expr, 0, ref_p, this_ref_p);
1896 928960 : else if (this_ref_p)
1897 : {
1898 10 : if (streamer_dump_file)
1899 : {
1900 0 : print_node_brief (streamer_dump_file,
1901 : " Streaming final ref to ",
1902 : expr, 4);
1903 0 : fprintf (streamer_dump_file, "\n");
1904 : }
1905 10 : streamer_write_record_start (ob, LTO_tree_pickle_reference);
1906 10 : streamer_write_uhwi (ob, ix);
1907 : }
1908 2984147 : in_dfs_walk = false;
1909 2984147 : lto_stats.num_pickle_refs_output++;
1910 : }
1911 4887236 : if (streamer_dump_file && !in_dfs_walk)
1912 248 : fprintf (streamer_dump_file, " %u bytes\n",
1913 248 : ob->main_stream->total_size - size);
1914 : }
1915 :
1916 :
1917 : /* Output to OB a list of try/catch handlers starting with FIRST. */
1918 :
1919 : static void
1920 307 : output_eh_try_list (struct output_block *ob, eh_catch first)
1921 : {
1922 307 : eh_catch n;
1923 :
1924 702 : for (n = first; n; n = n->next_catch)
1925 : {
1926 395 : streamer_write_record_start (ob, LTO_eh_catch);
1927 395 : stream_write_tree (ob, n->type_list, true);
1928 395 : stream_write_tree (ob, n->filter_list, true);
1929 395 : stream_write_tree (ob, n->label, true);
1930 : }
1931 :
1932 307 : streamer_write_record_start (ob, LTO_null);
1933 307 : }
1934 :
1935 :
1936 : /* Output EH region R in function FN to OB. CURR_RN is the slot index
1937 : that is being emitted in FN->EH->REGION_ARRAY. This is used to
1938 : detect EH region sharing. */
1939 :
1940 : static void
1941 18147 : output_eh_region (struct output_block *ob, eh_region r)
1942 : {
1943 18147 : enum LTO_tags tag;
1944 :
1945 18147 : if (r == NULL)
1946 : {
1947 7551 : streamer_write_record_start (ob, LTO_null);
1948 7551 : return;
1949 : }
1950 :
1951 10596 : if (r->type == ERT_CLEANUP)
1952 : tag = LTO_ert_cleanup;
1953 : else if (r->type == ERT_TRY)
1954 : tag = LTO_ert_try;
1955 : else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1956 : tag = LTO_ert_allowed_exceptions;
1957 : else if (r->type == ERT_MUST_NOT_THROW)
1958 : tag = LTO_ert_must_not_throw;
1959 : else
1960 0 : gcc_unreachable ();
1961 :
1962 10596 : streamer_write_record_start (ob, tag);
1963 10596 : streamer_write_hwi (ob, r->index);
1964 :
1965 10596 : if (r->outer)
1966 3153 : streamer_write_hwi (ob, r->outer->index);
1967 : else
1968 7443 : streamer_write_zero (ob);
1969 :
1970 10596 : if (r->inner)
1971 1865 : streamer_write_hwi (ob, r->inner->index);
1972 : else
1973 8731 : streamer_write_zero (ob);
1974 :
1975 10596 : if (r->next_peer)
1976 4467 : streamer_write_hwi (ob, r->next_peer->index);
1977 : else
1978 6129 : streamer_write_zero (ob);
1979 :
1980 10596 : if (r->type == ERT_TRY)
1981 : {
1982 307 : output_eh_try_list (ob, r->u.eh_try.first_catch);
1983 : }
1984 10289 : else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1985 : {
1986 324 : stream_write_tree (ob, r->u.allowed.type_list, true);
1987 324 : stream_write_tree (ob, r->u.allowed.label, true);
1988 324 : streamer_write_uhwi (ob, r->u.allowed.filter);
1989 : }
1990 9965 : else if (r->type == ERT_MUST_NOT_THROW)
1991 : {
1992 5315 : stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
1993 5315 : bitpack_d bp = bitpack_create (ob->main_stream);
1994 5315 : stream_output_location (ob, &bp, r->u.must_not_throw.failure_loc);
1995 5315 : streamer_write_bitpack (&bp);
1996 : }
1997 :
1998 10596 : if (r->landing_pads)
1999 3179 : streamer_write_hwi (ob, r->landing_pads->index);
2000 : else
2001 7417 : streamer_write_zero (ob);
2002 : }
2003 :
2004 :
2005 : /* Output landing pad LP to OB. */
2006 :
2007 : static void
2008 8342 : output_eh_lp (struct output_block *ob, eh_landing_pad lp)
2009 : {
2010 8342 : if (lp == NULL)
2011 : {
2012 5115 : streamer_write_record_start (ob, LTO_null);
2013 5115 : return;
2014 : }
2015 :
2016 3227 : streamer_write_record_start (ob, LTO_eh_landing_pad);
2017 3227 : streamer_write_hwi (ob, lp->index);
2018 3227 : if (lp->next_lp)
2019 48 : streamer_write_hwi (ob, lp->next_lp->index);
2020 : else
2021 3179 : streamer_write_zero (ob);
2022 :
2023 3227 : if (lp->region)
2024 3227 : streamer_write_hwi (ob, lp->region->index);
2025 : else
2026 0 : streamer_write_zero (ob);
2027 :
2028 3227 : stream_write_tree (ob, lp->post_landing_pad, true);
2029 : }
2030 :
2031 :
2032 : /* Output the existing eh_table to OB. */
2033 :
2034 : static void
2035 107290 : output_eh_regions (struct output_block *ob, struct function *fn)
2036 : {
2037 107290 : if (fn->eh && fn->eh->region_tree)
2038 : {
2039 4264 : unsigned i;
2040 4264 : eh_region eh;
2041 4264 : eh_landing_pad lp;
2042 4264 : tree ttype;
2043 :
2044 4264 : streamer_write_record_start (ob, LTO_eh_table);
2045 :
2046 : /* Emit the index of the root of the EH region tree. */
2047 4264 : streamer_write_hwi (ob, fn->eh->region_tree->index);
2048 :
2049 : /* Emit all the EH regions in the region array. */
2050 4264 : streamer_write_hwi (ob, vec_safe_length (fn->eh->region_array));
2051 26675 : FOR_EACH_VEC_SAFE_ELT (fn->eh->region_array, i, eh)
2052 18147 : output_eh_region (ob, eh);
2053 :
2054 : /* Emit all landing pads. */
2055 4264 : streamer_write_hwi (ob, vec_safe_length (fn->eh->lp_array));
2056 16870 : FOR_EACH_VEC_SAFE_ELT (fn->eh->lp_array, i, lp)
2057 8342 : output_eh_lp (ob, lp);
2058 :
2059 : /* Emit all the runtime type data. */
2060 4264 : streamer_write_hwi (ob, vec_safe_length (fn->eh->ttype_data));
2061 8528 : FOR_EACH_VEC_SAFE_ELT (fn->eh->ttype_data, i, ttype)
2062 0 : stream_write_tree (ob, ttype, true);
2063 :
2064 : /* Emit the table of action chains. */
2065 4264 : if (targetm.arm_eabi_unwinder)
2066 : {
2067 0 : tree t;
2068 0 : streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.arm_eabi));
2069 0 : FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.arm_eabi, i, t)
2070 0 : stream_write_tree (ob, t, true);
2071 : }
2072 : else
2073 : {
2074 4264 : uchar c;
2075 4264 : streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.other));
2076 8528 : FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.other, i, c)
2077 0 : streamer_write_char_stream (ob->main_stream, c);
2078 : }
2079 : }
2080 :
2081 : /* The LTO_null either terminates the record or indicates that there
2082 : are no eh_records at all. */
2083 107290 : streamer_write_record_start (ob, LTO_null);
2084 107290 : }
2085 :
2086 :
2087 : /* Output all of the active ssa names to the ssa_names stream. */
2088 :
2089 : static void
2090 107290 : output_ssa_names (struct output_block *ob, struct function *fn)
2091 : {
2092 107290 : unsigned int i, len;
2093 :
2094 107290 : len = vec_safe_length (SSANAMES (fn));
2095 107290 : streamer_write_uhwi (ob, len);
2096 :
2097 1727340 : for (i = 1; i < len; i++)
2098 : {
2099 1620050 : tree ptr = (*SSANAMES (fn))[i];
2100 :
2101 2223554 : if (ptr == NULL_TREE
2102 1613835 : || SSA_NAME_IN_FREE_LIST (ptr)
2103 1613835 : || virtual_operand_p (ptr)
2104 : /* Simply skip unreleased SSA names. */
2105 2639488 : || (! SSA_NAME_IS_DEFAULT_DEF (ptr)
2106 913387 : && (! SSA_NAME_DEF_STMT (ptr)
2107 913387 : || ! gimple_bb (SSA_NAME_DEF_STMT (ptr)))))
2108 603504 : continue;
2109 :
2110 1016546 : streamer_write_uhwi (ob, i);
2111 1016546 : streamer_write_char_stream (ob->main_stream,
2112 1016546 : SSA_NAME_IS_DEFAULT_DEF (ptr));
2113 1016546 : if (SSA_NAME_VAR (ptr))
2114 296698 : stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
2115 : else
2116 : /* ??? This drops SSA_NAME_IDENTIFIER on the floor. */
2117 719848 : stream_write_tree (ob, TREE_TYPE (ptr), true);
2118 : }
2119 :
2120 107290 : streamer_write_zero (ob);
2121 107290 : }
2122 :
2123 :
2124 :
2125 : /* Output the cfg. */
2126 :
2127 : static void
2128 107290 : output_cfg (struct output_block *ob, struct function *fn)
2129 : {
2130 107290 : struct lto_output_stream *tmp_stream = ob->main_stream;
2131 107290 : basic_block bb;
2132 :
2133 107290 : ob->main_stream = ob->cfg_stream;
2134 :
2135 107290 : streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
2136 : profile_status_for_fn (fn));
2137 :
2138 : /* Output the number of the highest basic block. */
2139 107290 : streamer_write_uhwi (ob, last_basic_block_for_fn (fn));
2140 :
2141 1018921 : FOR_ALL_BB_FN (bb, fn)
2142 : {
2143 911631 : edge_iterator ei;
2144 911631 : edge e;
2145 :
2146 911631 : streamer_write_hwi (ob, bb->index);
2147 :
2148 : /* Output the successors and the edge flags. */
2149 1713326 : streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
2150 1949149 : FOR_EACH_EDGE (e, ei, bb->succs)
2151 : {
2152 1037518 : bitpack_d bp = bitpack_create (ob->main_stream);
2153 1037518 : bp_pack_var_len_unsigned (&bp, e->dest->index);
2154 1037518 : bp_pack_var_len_unsigned (&bp, e->flags);
2155 1037518 : stream_output_location_and_block (ob, &bp, e->goto_locus);
2156 1037518 : e->probability.stream_out (ob);
2157 : }
2158 : }
2159 :
2160 107290 : streamer_write_hwi (ob, -1);
2161 :
2162 107290 : bb = ENTRY_BLOCK_PTR_FOR_FN (fn);
2163 911631 : while (bb->next_bb)
2164 : {
2165 804341 : streamer_write_hwi (ob, bb->next_bb->index);
2166 804341 : bb = bb->next_bb;
2167 : }
2168 :
2169 107290 : streamer_write_hwi (ob, -1);
2170 :
2171 : /* Output the number of loops. */
2172 107290 : streamer_write_uhwi (ob, number_of_loops (fn));
2173 :
2174 : /* Output each loop, skipping the tree root which has number zero. */
2175 362998 : for (unsigned i = 1; i < number_of_loops (fn); ++i)
2176 : {
2177 74209 : class loop *loop = get_loop (fn, i);
2178 :
2179 : /* Write the index of the loop header. That's enough to rebuild
2180 : the loop tree on the reader side. Stream -1 for an unused
2181 : loop entry. */
2182 74209 : if (!loop)
2183 : {
2184 25283 : streamer_write_hwi (ob, -1);
2185 25283 : continue;
2186 : }
2187 : else
2188 48926 : streamer_write_hwi (ob, loop->header->index);
2189 :
2190 : /* Write everything copy_loop_info copies. */
2191 48926 : streamer_write_enum (ob->main_stream,
2192 : loop_estimation, EST_LAST, loop->estimate_state);
2193 48926 : streamer_write_hwi (ob, loop->any_upper_bound);
2194 48926 : if (loop->any_upper_bound)
2195 : {
2196 40081 : widest_int w = widest_int::from (loop->nb_iterations_upper_bound,
2197 40081 : SIGNED);
2198 40081 : streamer_write_widest_int (ob, w);
2199 40081 : }
2200 48926 : streamer_write_hwi (ob, loop->any_likely_upper_bound);
2201 48926 : if (loop->any_likely_upper_bound)
2202 : {
2203 40083 : widest_int w
2204 40083 : = widest_int::from (loop->nb_iterations_likely_upper_bound,
2205 40083 : SIGNED);
2206 40083 : streamer_write_widest_int (ob, w);
2207 40083 : }
2208 48926 : streamer_write_hwi (ob, loop->any_estimate);
2209 48926 : if (loop->any_estimate)
2210 : {
2211 33206 : widest_int w = widest_int::from (loop->nb_iterations_estimate,
2212 33206 : SIGNED);
2213 33206 : streamer_write_widest_int (ob, w);
2214 33206 : }
2215 :
2216 : /* Write OMP SIMD related info. */
2217 48926 : streamer_write_hwi (ob, loop->safelen);
2218 48926 : streamer_write_hwi (ob, loop->unroll);
2219 48926 : streamer_write_hwi (ob, loop->owned_clique);
2220 48926 : streamer_write_hwi (ob, loop->dont_vectorize);
2221 48926 : streamer_write_hwi (ob, loop->force_vectorize);
2222 48926 : streamer_write_hwi (ob, loop->finite_p);
2223 48926 : stream_write_tree (ob, loop->simduid, true);
2224 : }
2225 :
2226 107290 : ob->main_stream = tmp_stream;
2227 107290 : }
2228 :
2229 : /* Create the header in the file using OB. If the section type is for
2230 : a function, set FN to the decl for that function. */
2231 :
2232 : void
2233 251786 : produce_symbol_asm (struct output_block *ob, tree fn, int output_order)
2234 : {
2235 251786 : enum lto_section_type section_type = ob->section_type;
2236 251786 : struct lto_function_header header;
2237 251786 : char *section_name;
2238 :
2239 251786 : if (section_type == LTO_section_function_body)
2240 : {
2241 118397 : const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
2242 118397 : section_name = lto_get_section_name (section_type, name,
2243 : output_order, NULL);
2244 : }
2245 : else
2246 133389 : section_name = lto_get_section_name (section_type, NULL, 0, NULL);
2247 :
2248 251786 : lto_begin_section (section_name, !flag_wpa);
2249 251786 : free (section_name);
2250 :
2251 : /* The entire header is stream computed here. */
2252 251786 : memset (&header, 0, sizeof (struct lto_function_header));
2253 :
2254 251786 : if (section_type == LTO_section_function_body)
2255 118397 : header.cfg_size = ob->cfg_stream->total_size;
2256 251786 : header.main_size = ob->main_stream->total_size;
2257 251786 : header.string_size = ob->string_stream->total_size;
2258 251786 : lto_write_data (&header, sizeof header);
2259 :
2260 : /* Put all of the gimple and the string table out the asm file as a
2261 : block of text. */
2262 251786 : if (section_type == LTO_section_function_body)
2263 118397 : lto_write_stream (ob->cfg_stream);
2264 251786 : lto_write_stream (ob->main_stream);
2265 251786 : lto_write_stream (ob->string_stream);
2266 :
2267 251786 : lto_end_section ();
2268 251786 : }
2269 :
2270 : /* Wrapper for unused arguments. */
2271 :
2272 : void
2273 133389 : produce_asm (struct output_block *ob)
2274 : {
2275 133389 : produce_symbol_asm (ob, NULL, -1);
2276 133389 : }
2277 :
2278 :
2279 : /* Output the base body of struct function FN using output block OB. */
2280 :
2281 : static void
2282 107290 : output_struct_function_base (struct output_block *ob, struct function *fn)
2283 : {
2284 107290 : struct bitpack_d bp;
2285 107290 : unsigned i;
2286 107290 : tree t;
2287 :
2288 : /* Output the static chain and non-local goto save area. */
2289 107290 : stream_write_tree (ob, fn->static_chain_decl, true);
2290 107290 : stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
2291 :
2292 : /* Output all the local variables in the function. */
2293 107290 : streamer_write_hwi (ob, vec_safe_length (fn->local_decls));
2294 379592 : FOR_EACH_VEC_SAFE_ELT (fn->local_decls, i, t)
2295 165012 : stream_write_tree (ob, t, true);
2296 :
2297 : /* Output current IL state of the function. */
2298 107290 : streamer_write_uhwi (ob, fn->curr_properties);
2299 :
2300 : /* Write all the attributes for FN. */
2301 107290 : bp = bitpack_create (ob->main_stream);
2302 107290 : bp_pack_value (&bp, fn->is_thunk, 1);
2303 107290 : bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
2304 107290 : bp_pack_value (&bp, fn->returns_pcc_struct, 1);
2305 107290 : bp_pack_value (&bp, fn->returns_struct, 1);
2306 107290 : bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
2307 107290 : bp_pack_value (&bp, fn->can_delete_dead_exceptions, 1);
2308 107290 : bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
2309 107290 : bp_pack_value (&bp, fn->after_inlining, 1);
2310 107290 : bp_pack_value (&bp, fn->stdarg, 1);
2311 107290 : bp_pack_value (&bp, fn->has_nonlocal_label, 1);
2312 107290 : bp_pack_value (&bp, fn->has_forced_label_in_static, 1);
2313 107290 : bp_pack_value (&bp, fn->calls_alloca, 1);
2314 107290 : bp_pack_value (&bp, fn->calls_setjmp, 1);
2315 107290 : bp_pack_value (&bp, fn->calls_eh_return, 1);
2316 107290 : bp_pack_value (&bp, fn->has_force_vectorize_loops, 1);
2317 107290 : bp_pack_value (&bp, fn->has_simduid_loops, 1);
2318 107290 : bp_pack_value (&bp, fn->has_musttail, 1);
2319 107290 : bp_pack_value (&bp, fn->has_unroll, 1);
2320 107290 : bp_pack_value (&bp, fn->assume_function, 1);
2321 107290 : bp_pack_value (&bp, fn->va_list_fpr_size, 8);
2322 107290 : bp_pack_value (&bp, fn->va_list_gpr_size, 8);
2323 107290 : bp_pack_value (&bp, fn->last_clique, sizeof (short) * 8);
2324 :
2325 : /* Output the function start and end loci. */
2326 107290 : stream_output_location (ob, &bp, fn->function_start_locus);
2327 107290 : stream_output_location (ob, &bp, fn->function_end_locus);
2328 :
2329 : /* Save the instance discriminator if present. */
2330 107290 : int *instance_number_p = NULL;
2331 107290 : if (decl_to_instance_map)
2332 0 : instance_number_p = decl_to_instance_map->get (fn->decl);
2333 107290 : bp_pack_value (&bp, !!instance_number_p, 1);
2334 107290 : if (instance_number_p)
2335 0 : bp_pack_value (&bp, *instance_number_p, sizeof (int) * CHAR_BIT);
2336 :
2337 107290 : streamer_write_bitpack (&bp);
2338 107290 : }
2339 :
2340 :
2341 : /* Collect all leaf BLOCKs beyond ROOT into LEAFS. */
2342 :
2343 : static void
2344 242868 : collect_block_tree_leafs (tree root, vec<tree> &leafs)
2345 : {
2346 452960 : for (root = BLOCK_SUBBLOCKS (root); root; root = BLOCK_CHAIN (root))
2347 210092 : if (! BLOCK_SUBBLOCKS (root))
2348 74514 : leafs.safe_push (root);
2349 : else
2350 135578 : collect_block_tree_leafs (root, leafs);
2351 242868 : }
2352 :
2353 : /* This performs function body modifications that are needed for streaming
2354 : to work. */
2355 :
2356 : void
2357 107280 : lto_prepare_function_for_streaming (struct cgraph_node *node)
2358 : {
2359 107280 : struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2360 107280 : basic_block bb;
2361 :
2362 214560 : if (number_of_loops (fn))
2363 : {
2364 107280 : push_cfun (fn);
2365 107280 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
2366 107280 : loop_optimizer_finalize ();
2367 107280 : pop_cfun ();
2368 : }
2369 : /* We will renumber the statements. The code that does this uses
2370 : the same ordering that we use for serializing them so we can use
2371 : the same code on the other end and not have to write out the
2372 : statement numbers. We do not assign UIDs to PHIs here because
2373 : virtual PHIs get re-computed on-the-fly which would make numbers
2374 : inconsistent. */
2375 107280 : set_gimple_stmt_max_uid (fn, 0);
2376 1018881 : FOR_ALL_BB_FN (bb, fn)
2377 : {
2378 1068945 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2379 157344 : gsi_next (&gsi))
2380 : {
2381 157344 : gphi *stmt = gsi.phi ();
2382 :
2383 : /* Virtual PHIs are not going to be streamed. */
2384 314688 : if (!virtual_operand_p (gimple_phi_result (stmt)))
2385 91147 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fn));
2386 : }
2387 3508173 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2388 1684971 : gsi_next (&gsi))
2389 : {
2390 1684971 : gimple *stmt = gsi_stmt (gsi);
2391 1684971 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fn));
2392 : }
2393 : }
2394 : /* To avoid keeping duplicate gimple IDs in the statements, renumber
2395 : virtual phis now. */
2396 1018881 : FOR_ALL_BB_FN (bb, fn)
2397 : {
2398 1068945 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2399 157344 : gsi_next (&gsi))
2400 : {
2401 157344 : gphi *stmt = gsi.phi ();
2402 380885 : if (virtual_operand_p (gimple_phi_result (stmt)))
2403 66197 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fn));
2404 : }
2405 : }
2406 :
2407 107280 : }
2408 :
2409 : /* Emit the chain of tree nodes starting at T. OB is the output block
2410 : to write to. REF_P is true if chain elements should be emitted
2411 : as references. */
2412 :
2413 : static void
2414 107437 : streamer_write_chain (struct output_block *ob, tree t, bool ref_p)
2415 : {
2416 425931 : while (t)
2417 : {
2418 : /* We avoid outputting external vars or functions by reference
2419 : to the global decls section as we do not want to have them
2420 : enter decl merging. We should not need to do this anymore because
2421 : free_lang_data removes them from block scopes. */
2422 318494 : gcc_assert (!VAR_OR_FUNCTION_DECL_P (t) || !DECL_EXTERNAL (t));
2423 318494 : stream_write_tree (ob, t, ref_p);
2424 :
2425 318494 : t = TREE_CHAIN (t);
2426 : }
2427 :
2428 : /* Write a sentinel to terminate the chain. */
2429 107437 : stream_write_tree (ob, NULL_TREE, ref_p);
2430 107437 : }
2431 :
2432 : /* Output the body of function NODE->DECL. */
2433 :
2434 : static void
2435 107437 : output_function (struct cgraph_node *node, int output_order)
2436 : {
2437 107437 : tree function;
2438 107437 : struct function *fn;
2439 107437 : basic_block bb;
2440 107437 : struct output_block *ob;
2441 :
2442 107437 : if (streamer_dump_file)
2443 8 : fprintf (streamer_dump_file, "\nStreaming body of %s\n",
2444 : node->dump_name ());
2445 :
2446 107437 : function = node->decl;
2447 107437 : fn = DECL_STRUCT_FUNCTION (function);
2448 107437 : ob = create_output_block (LTO_section_function_body);
2449 :
2450 107437 : ob->symbol = node;
2451 :
2452 107437 : gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
2453 :
2454 : /* Make string 0 be a NULL string. */
2455 107437 : streamer_write_char_stream (ob->string_stream, 0);
2456 :
2457 107437 : streamer_write_record_start (ob, LTO_function);
2458 :
2459 : /* Output decls for parameters and args. */
2460 107437 : stream_write_tree (ob, DECL_RESULT (function), true);
2461 107437 : streamer_write_chain (ob, DECL_ARGUMENTS (function), true);
2462 :
2463 : /* Output debug args if available. */
2464 107437 : vec<tree, va_gc> **debugargs = decl_debug_args_lookup (function);
2465 107437 : if (! debugargs)
2466 107408 : streamer_write_uhwi (ob, 0);
2467 : else
2468 : {
2469 29 : streamer_write_uhwi (ob, (*debugargs)->length ());
2470 103 : for (unsigned i = 0; i < (*debugargs)->length (); ++i)
2471 74 : stream_write_tree (ob, (**debugargs)[i], true);
2472 : }
2473 :
2474 : /* Output DECL_INITIAL for the function, which contains the tree of
2475 : lexical scopes. */
2476 107437 : stream_write_tree (ob, DECL_INITIAL (function), true);
2477 : /* As we do not recurse into BLOCK_SUBBLOCKS but only BLOCK_SUPERCONTEXT
2478 : collect block tree leafs and stream those. */
2479 107437 : auto_vec<tree> block_tree_leafs;
2480 107437 : if (DECL_INITIAL (function) && DECL_INITIAL (function) != error_mark_node)
2481 107290 : collect_block_tree_leafs (DECL_INITIAL (function), block_tree_leafs);
2482 107437 : streamer_write_uhwi (ob, block_tree_leafs.length ());
2483 181951 : for (unsigned i = 0; i < block_tree_leafs.length (); ++i)
2484 74514 : stream_write_tree (ob, block_tree_leafs[i], true);
2485 :
2486 : /* We also stream abstract functions where we stream only stuff needed for
2487 : debug info. */
2488 107437 : if (gimple_has_body_p (function))
2489 : {
2490 107290 : streamer_write_uhwi (ob, 1);
2491 107290 : output_struct_function_base (ob, fn);
2492 :
2493 107290 : output_cfg (ob, fn);
2494 :
2495 : /* Output all the SSA names used in the function. */
2496 107290 : output_ssa_names (ob, fn);
2497 :
2498 : /* Output any exception handling regions. */
2499 107290 : output_eh_regions (ob, fn);
2500 :
2501 : /* Output the code for the function. */
2502 1018921 : FOR_ALL_BB_FN (bb, fn)
2503 911631 : output_bb (ob, bb, fn);
2504 :
2505 : /* The terminator for this function. */
2506 107290 : streamer_write_record_start (ob, LTO_null);
2507 : }
2508 : else
2509 147 : streamer_write_uhwi (ob, 0);
2510 :
2511 : /* Create a section to hold the pickled output of this function. */
2512 107437 : produce_symbol_asm (ob, function, output_order);
2513 :
2514 107437 : destroy_output_block (ob);
2515 107437 : if (streamer_dump_file)
2516 8 : fprintf (streamer_dump_file, "Finished streaming %s\n",
2517 : node->dump_name ());
2518 107437 : }
2519 :
2520 : /* Output the body of function NODE->DECL. */
2521 :
2522 : static void
2523 10960 : output_constructor (struct varpool_node *node, int output_order)
2524 : {
2525 10960 : tree var = node->decl;
2526 10960 : struct output_block *ob;
2527 :
2528 10960 : if (streamer_dump_file)
2529 4 : fprintf (streamer_dump_file, "\nStreaming constructor of %s\n",
2530 : node->dump_name ());
2531 :
2532 10960 : timevar_push (TV_IPA_LTO_CTORS_OUT);
2533 10960 : ob = create_output_block (LTO_section_function_body);
2534 :
2535 10960 : ob->symbol = node;
2536 :
2537 : /* Make string 0 be a NULL string. */
2538 10960 : streamer_write_char_stream (ob->string_stream, 0);
2539 :
2540 : /* Output DECL_INITIAL for the function, which contains the tree of
2541 : lexical scopes. */
2542 10960 : stream_write_tree (ob, DECL_INITIAL (var), true);
2543 :
2544 : /* Create a section to hold the pickled output of this function. */
2545 10960 : produce_symbol_asm (ob, var, output_order);
2546 :
2547 10960 : destroy_output_block (ob);
2548 10960 : if (streamer_dump_file)
2549 4 : fprintf (streamer_dump_file, "Finished streaming %s\n",
2550 : node->dump_name ());
2551 10960 : timevar_pop (TV_IPA_LTO_CTORS_OUT);
2552 10960 : }
2553 :
2554 :
2555 : /* Emit toplevel asms. */
2556 :
2557 : void
2558 31226 : lto_output_toplevel_asms (lto_symtab_encoder_t encoder)
2559 : {
2560 31226 : struct output_block *ob;
2561 31226 : char *section_name;
2562 31226 : struct lto_simple_header_with_strings header;
2563 :
2564 31226 : unsigned asm_count = 0;
2565 1473389 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
2566 1411174 : if (is_a <asm_node*> (lto_symtab_encoder_deref (encoder, i)))
2567 156 : asm_count++;
2568 :
2569 31226 : if (!asm_count)
2570 31112 : return;
2571 :
2572 114 : ob = create_output_block (LTO_section_asm);
2573 :
2574 : /* Stream the length. */
2575 114 : streamer_write_uhwi (ob, asm_count);
2576 26102 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
2577 : {
2578 12937 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
2579 12937 : asm_node *anode = dyn_cast <asm_node*> (tnode);
2580 12937 : if (!anode)
2581 12781 : continue;
2582 :
2583 156 : int output_order = *encoder->order_remap->get (anode->order);
2584 156 : stream_write_tree (ob, anode->asm_str, true);
2585 156 : streamer_write_hwi (ob, output_order);
2586 : }
2587 :
2588 114 : section_name = lto_get_section_name (LTO_section_asm, NULL, 0, NULL);
2589 114 : lto_begin_section (section_name, !flag_wpa);
2590 114 : free (section_name);
2591 :
2592 : /* The entire header stream is computed here. */
2593 114 : memset (&header, 0, sizeof (header));
2594 :
2595 114 : header.main_size = ob->main_stream->total_size;
2596 114 : header.string_size = ob->string_stream->total_size;
2597 114 : lto_write_data (&header, sizeof header);
2598 :
2599 : /* Put all of the gimple and the string table out the asm file as a
2600 : block of text. */
2601 114 : lto_write_stream (ob->main_stream);
2602 114 : lto_write_stream (ob->string_stream);
2603 :
2604 114 : lto_end_section ();
2605 :
2606 114 : destroy_output_block (ob);
2607 : }
2608 :
2609 :
2610 : /* Copy the function body or variable constructor of NODE without deserializing. */
2611 :
2612 : static void
2613 34034 : copy_function_or_variable (struct symtab_node *node, int output_order)
2614 : {
2615 34034 : tree function = node->decl;
2616 34034 : struct lto_file_decl_data *file_data = node->lto_file_data;
2617 34034 : const char *data;
2618 34034 : size_t len;
2619 34034 : const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2620 34034 : char *section_name =
2621 34034 : lto_get_section_name (LTO_section_function_body, name, output_order, NULL);
2622 34034 : size_t i, j;
2623 34034 : struct lto_in_decl_state *in_state;
2624 34034 : struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2625 :
2626 34034 : if (streamer_dump_file)
2627 0 : fprintf (streamer_dump_file, "Copying section for %s\n", name);
2628 34034 : lto_begin_section (section_name, false);
2629 34034 : free (section_name);
2630 :
2631 : /* We may have renamed the declaration, e.g., a static function. */
2632 34034 : name = lto_get_decl_name_mapping (file_data, name);
2633 :
2634 68068 : data = lto_get_raw_section_data (file_data, LTO_section_function_body,
2635 34034 : name, node->order - file_data->order_base,
2636 : &len);
2637 34034 : gcc_assert (data);
2638 :
2639 : /* Do a bit copy of the function body. */
2640 34034 : lto_write_raw_data (data, len);
2641 :
2642 : /* Copy decls. */
2643 34034 : in_state =
2644 34034 : lto_get_function_in_decl_state (node->lto_file_data, function);
2645 34034 : out_state->compressed = in_state->compressed;
2646 34034 : gcc_assert (in_state);
2647 :
2648 68068 : for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2649 : {
2650 34034 : size_t n = vec_safe_length (in_state->streams[i]);
2651 34034 : vec<tree, va_gc> *trees = in_state->streams[i];
2652 34034 : struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2653 :
2654 : /* The out state must have the same indices and the in state.
2655 : So just copy the vector. All the encoders in the in state
2656 : must be empty where we reach here. */
2657 34034 : gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2658 34034 : encoder->trees.reserve_exact (n);
2659 518618 : for (j = 0; j < n; j++)
2660 450550 : encoder->trees.safe_push ((*trees)[j]);
2661 : }
2662 :
2663 34034 : lto_free_raw_section_data (file_data, LTO_section_function_body, name,
2664 : data, len);
2665 34034 : lto_end_section ();
2666 34034 : }
2667 :
2668 : /* Wrap symbol references in *TP inside a type-preserving MEM_REF. */
2669 :
2670 : static tree
2671 792507 : wrap_refs (tree *tp, int *ws, void *)
2672 : {
2673 792507 : tree t = *tp;
2674 792507 : if (handled_component_p (t)
2675 176 : && VAR_P (TREE_OPERAND (t, 0))
2676 120 : && TREE_PUBLIC (TREE_OPERAND (t, 0)))
2677 : {
2678 81 : tree decl = TREE_OPERAND (t, 0);
2679 81 : tree ptrtype = build_pointer_type (TREE_TYPE (decl));
2680 81 : TREE_OPERAND (t, 0) = build2 (MEM_REF, TREE_TYPE (decl),
2681 : build1 (ADDR_EXPR, ptrtype, decl),
2682 : build_int_cst (ptrtype, 0));
2683 81 : TREE_THIS_VOLATILE (TREE_OPERAND (t, 0)) = TREE_THIS_VOLATILE (decl);
2684 81 : *ws = 0;
2685 : }
2686 792426 : else if (TREE_CODE (t) == CONSTRUCTOR)
2687 : ;
2688 574838 : else if (!EXPR_P (t))
2689 335164 : *ws = 0;
2690 792507 : return NULL_TREE;
2691 : }
2692 :
2693 : /* Remove functions that are no longer used from offload_funcs, and mark the
2694 : remaining ones with DECL_PRESERVE_P. */
2695 :
2696 : static void
2697 31226 : prune_offload_funcs (void)
2698 : {
2699 31226 : if (!offload_funcs)
2700 31226 : return;
2701 :
2702 0 : unsigned ix, ix2;
2703 0 : tree *elem_ptr;
2704 0 : VEC_ORDERED_REMOVE_IF (*offload_funcs, ix, ix2, elem_ptr,
2705 : cgraph_node::get (*elem_ptr) == NULL);
2706 :
2707 : tree fn_decl;
2708 0 : FOR_EACH_VEC_ELT (*offload_funcs, ix, fn_decl)
2709 0 : DECL_PRESERVE_P (fn_decl) = 1;
2710 : }
2711 :
2712 : /* Produce LTO section that contains global information
2713 : about LTO bytecode. */
2714 :
2715 : static void
2716 31226 : produce_lto_section ()
2717 : {
2718 : /* Stream LTO meta section. */
2719 31226 : output_block *ob = create_output_block (LTO_section_lto);
2720 :
2721 31226 : char * section_name = lto_get_section_name (LTO_section_lto, NULL, 0, NULL);
2722 31226 : lto_begin_section (section_name, false);
2723 31226 : free (section_name);
2724 :
2725 : #ifdef HAVE_ZSTD_H
2726 31226 : lto_compression compression = ZSTD;
2727 : #else
2728 : lto_compression compression = ZLIB;
2729 : #endif
2730 :
2731 31226 : bool slim_object = flag_generate_lto && !flag_fat_lto_objects;
2732 31226 : lto_section s
2733 31226 : = { LTO_major_version, LTO_minor_version, slim_object, 0, 0 };
2734 31226 : s.set_compression (compression);
2735 31226 : lto_write_data (&s, sizeof s);
2736 31226 : lto_end_section ();
2737 31226 : destroy_output_block (ob);
2738 31226 : }
2739 :
2740 : /* Compare symbols to get them sorted by filename (to optimize streaming) */
2741 :
2742 : static int
2743 3351040 : cmp_symbol_files (const void *pn1, const void *pn2, void *id_map_)
2744 : {
2745 3351040 : const symtab_node *n1 = *(const symtab_node * const *)pn1;
2746 3351040 : const symtab_node *n2 = *(const symtab_node * const *)pn2;
2747 3351040 : hash_map<lto_file_decl_data *, int> *id_map
2748 : = (hash_map<lto_file_decl_data *, int> *)id_map_;
2749 :
2750 3351040 : int file_order1 = n1->lto_file_data ? n1->lto_file_data->order : -1;
2751 3351040 : int file_order2 = n2->lto_file_data ? n2->lto_file_data->order : -1;
2752 :
2753 : /* Order files same way as they appeared in the command line to reduce
2754 : seeking while copying sections. */
2755 3351040 : if (file_order1 != file_order2)
2756 321 : return file_order1 - file_order2;
2757 :
2758 : /* Order within static library. */
2759 3350719 : if (n1->lto_file_data && n1->lto_file_data->id != n2->lto_file_data->id)
2760 9507 : return *id_map->get (n1->lto_file_data) - *id_map->get (n2->lto_file_data);
2761 :
2762 : /* And finaly order by the definition order. */
2763 3341212 : return n1->order - n2->order;
2764 : }
2765 :
2766 : /* Compare ints, callback for qsort. */
2767 :
2768 : static int
2769 31807698 : cmp_int (const void *a, const void *b)
2770 : {
2771 31807698 : int ia = *(int const*) a;
2772 31807698 : int ib = *(int const*) b;
2773 31807698 : return ia - ib;
2774 : }
2775 :
2776 : /* Create order mapping independent on symbols outside of the partition.
2777 : Results in stable order values for incremental LTO.
2778 :
2779 : Remapping is not done in place, because symbols can be used
2780 : by multiple partitions. */
2781 :
2782 : static void
2783 31226 : create_order_remap (lto_symtab_encoder_t encoder)
2784 : {
2785 31226 : auto_vec<int> orders;
2786 31226 : unsigned i;
2787 31226 : encoder->order_remap = new hash_map<int_hash<int, -1, -2>, int>;
2788 31226 : unsigned n_nodes = lto_symtab_encoder_size (encoder);
2789 :
2790 736813 : for (i = 0; i < n_nodes; i++)
2791 705587 : orders.safe_push (lto_symtab_encoder_deref (encoder, i)->order);
2792 :
2793 31226 : orders.qsort (cmp_int);
2794 31226 : int ord = 0;
2795 31226 : int last_order = -1;
2796 736813 : for (i = 0; i < orders.length (); i++)
2797 : {
2798 705587 : int order = orders[i];
2799 705587 : if (order != last_order)
2800 : {
2801 682549 : last_order = order;
2802 682549 : encoder->order_remap->put (order, ord);
2803 682549 : ord++;
2804 : }
2805 : }
2806 31226 : }
2807 :
2808 : /* Main entry point from the pass manager. */
2809 :
2810 : void
2811 31226 : lto_output (void)
2812 : {
2813 31226 : struct lto_out_decl_state *decl_state;
2814 31226 : bitmap output = NULL;
2815 31226 : bitmap_obstack output_obstack;
2816 31226 : unsigned int i, n_nodes;
2817 31226 : lto_symtab_encoder_t encoder = lto_get_out_decl_state ()->symtab_node_encoder;
2818 31226 : auto_vec<symtab_node *> symbols_to_copy;
2819 :
2820 31226 : create_order_remap (encoder);
2821 :
2822 31226 : prune_offload_funcs ();
2823 :
2824 31226 : if (flag_checking)
2825 : {
2826 31220 : bitmap_obstack_initialize (&output_obstack);
2827 31220 : output = BITMAP_ALLOC (&output_obstack);
2828 : }
2829 :
2830 : /* Initialize the streamer. */
2831 31226 : lto_streamer_init ();
2832 :
2833 31226 : produce_lto_section ();
2834 :
2835 31226 : n_nodes = lto_symtab_encoder_size (encoder);
2836 : /* Prepare vector of functions to output and then sort it to optimize
2837 : section copying. */
2838 736813 : for (i = 0; i < n_nodes; i++)
2839 : {
2840 705587 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
2841 705587 : symtab_node *node = dyn_cast <symtab_node *> (tnode);
2842 705431 : if (!node || node->alias)
2843 12887 : continue;
2844 :
2845 692700 : if (cgraph_node *node = dyn_cast <cgraph_node *> (tnode))
2846 : {
2847 407476 : if (lto_symtab_encoder_encode_body_p (encoder, node)
2848 407476 : && !node->clone_of)
2849 136991 : symbols_to_copy.safe_push (node);
2850 : }
2851 990811 : else if (varpool_node *node = dyn_cast <varpool_node *> (tnode))
2852 : {
2853 : /* Wrap symbol references inside the ctor in a type
2854 : preserving MEM_REF. */
2855 285224 : tree ctor = DECL_INITIAL (node->decl);
2856 285224 : if (ctor && !in_lto_p)
2857 19259 : walk_tree (&ctor, wrap_refs, NULL, NULL);
2858 285224 : if (get_symbol_initial_value (encoder, node->decl) == error_mark_node
2859 285224 : && lto_symtab_encoder_encode_initializer_p (encoder, node))
2860 15440 : symbols_to_copy.safe_push (node);
2861 : }
2862 : }
2863 : /* Map the section hash to an order it appears in symbols_to_copy
2864 : since we want to sort same ID symbols next to each other but need
2865 : to avoid making overall order depend on the actual hash value. */
2866 31226 : int order = 0;
2867 31226 : hash_map<lto_file_decl_data *, int> id_map;
2868 183657 : for (i = 0; i < symbols_to_copy.length (); ++i)
2869 : {
2870 152431 : symtab_node *snode = symbols_to_copy[i];
2871 152431 : if (snode->lto_file_data)
2872 : {
2873 39611 : bool existed_p = false;
2874 39611 : int &ord = id_map.get_or_insert (snode->lto_file_data, &existed_p);
2875 39611 : if (!existed_p)
2876 8662 : ord = order++;
2877 : }
2878 : }
2879 31226 : symbols_to_copy.sort (cmp_symbol_files, (void *)&id_map);
2880 183657 : for (i = 0; i < symbols_to_copy.length (); i++)
2881 : {
2882 152431 : symtab_node *snode = symbols_to_copy[i];
2883 152431 : cgraph_node *cnode;
2884 152431 : varpool_node *vnode;
2885 :
2886 152431 : int output_order = *encoder->order_remap->get (snode->order);
2887 :
2888 152431 : if (flag_checking)
2889 152429 : gcc_assert (bitmap_set_bit (output, DECL_UID (snode->decl)));
2890 :
2891 152431 : decl_state = lto_new_out_decl_state ();
2892 152431 : lto_push_out_decl_state (decl_state);
2893 :
2894 152431 : if ((cnode = dyn_cast <cgraph_node *> (snode))
2895 136991 : && (gimple_has_body_p (cnode->decl)
2896 29701 : || (!flag_wpa
2897 286 : && flag_incremental_link != INCREMENTAL_LINK_LTO)
2898 : /* Thunks have no body but they may be synthetized
2899 : at WPA time. */
2900 29555 : || DECL_ARGUMENTS (cnode->decl)))
2901 107437 : output_function (cnode, output_order);
2902 44994 : else if ((vnode = dyn_cast <varpool_node *> (snode))
2903 15440 : && (DECL_INITIAL (vnode->decl) != error_mark_node
2904 4480 : || (!flag_wpa
2905 42 : && flag_incremental_link != INCREMENTAL_LINK_LTO)))
2906 10960 : output_constructor (vnode, output_order);
2907 : else
2908 34034 : copy_function_or_variable (snode, output_order);
2909 152431 : gcc_assert (lto_get_out_decl_state () == decl_state);
2910 152431 : lto_pop_out_decl_state ();
2911 152431 : lto_record_function_out_decl_state (snode->decl, decl_state);
2912 : }
2913 :
2914 : /* Emit the callgraph after emitting function bodies. This needs to
2915 : be done now to make sure that all the statements in every function
2916 : have been renumbered so that edges can be associated with call
2917 : statements using the statement UIDs. */
2918 31226 : output_symtab ();
2919 :
2920 31226 : if (lto_get_out_decl_state ()->output_offload_tables_p)
2921 7817 : output_offload_tables ();
2922 :
2923 31226 : if (flag_checking)
2924 : {
2925 31220 : BITMAP_FREE (output);
2926 31220 : bitmap_obstack_release (&output_obstack);
2927 : }
2928 31226 : }
2929 :
2930 : /* Write each node in encoded by ENCODER to OB, as well as those reachable
2931 : from it and required for correct representation of its semantics.
2932 : Each node in ENCODER must be a global declaration or a type. A node
2933 : is written only once, even if it appears multiple times in the
2934 : vector. Certain transitively-reachable nodes, such as those
2935 : representing expressions, may be duplicated, but such nodes
2936 : must not appear in ENCODER itself. */
2937 :
2938 : static void
2939 183657 : write_global_stream (struct output_block *ob,
2940 : struct lto_tree_ref_encoder *encoder)
2941 : {
2942 183657 : tree t;
2943 183657 : size_t index;
2944 183657 : const size_t size = lto_tree_ref_encoder_size (encoder);
2945 :
2946 2815728 : for (index = 0; index < size; index++)
2947 : {
2948 2632071 : t = lto_tree_ref_encoder_get_tree (encoder, index);
2949 2632071 : if (streamer_dump_file)
2950 : {
2951 176 : fprintf (streamer_dump_file, " %i:", (int)index);
2952 176 : print_node_brief (streamer_dump_file, "", t, 4);
2953 176 : fprintf (streamer_dump_file, "\n");
2954 : }
2955 2632071 : if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
2956 928950 : stream_write_tree (ob, t, false);
2957 : }
2958 183657 : }
2959 :
2960 :
2961 : /* Write a sequence of indices into the globals vector corresponding
2962 : to the trees in ENCODER. These are used by the reader to map the
2963 : indices used to refer to global entities within function bodies to
2964 : their referents. */
2965 :
2966 : static void
2967 183657 : write_global_references (struct output_block *ob,
2968 : struct lto_tree_ref_encoder *encoder)
2969 : {
2970 183657 : tree t;
2971 183657 : uint32_t index;
2972 183657 : const uint32_t size = lto_tree_ref_encoder_size (encoder);
2973 :
2974 : /* Write size and slot indexes as 32-bit unsigned numbers. */
2975 183657 : uint32_t *data = XNEWVEC (uint32_t, size + 1);
2976 183657 : data[0] = size;
2977 :
2978 2815728 : for (index = 0; index < size; index++)
2979 : {
2980 2632071 : unsigned slot_num;
2981 :
2982 2632071 : t = lto_tree_ref_encoder_get_tree (encoder, index);
2983 2632071 : streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
2984 2632071 : gcc_assert (slot_num != (unsigned)-1);
2985 2632071 : data[index + 1] = slot_num;
2986 : }
2987 :
2988 183657 : lto_write_data (data, sizeof (int32_t) * (size + 1));
2989 183657 : free (data);
2990 183657 : }
2991 :
2992 :
2993 : /* Write all the streams in an lto_out_decl_state STATE using
2994 : output block OB and output stream OUT_STREAM. */
2995 :
2996 : void
2997 183657 : lto_output_decl_state_streams (struct output_block *ob,
2998 : struct lto_out_decl_state *state)
2999 : {
3000 183657 : int i;
3001 :
3002 367314 : for (i = 0; i < LTO_N_DECL_STREAMS; i++)
3003 183657 : write_global_stream (ob, &state->streams[i]);
3004 183657 : }
3005 :
3006 :
3007 : /* Write all the references in an lto_out_decl_state STATE using
3008 : output block OB and output stream OUT_STREAM. */
3009 :
3010 : void
3011 183657 : lto_output_decl_state_refs (struct output_block *ob,
3012 : struct lto_out_decl_state *state)
3013 : {
3014 183657 : unsigned i;
3015 183657 : unsigned ref;
3016 183657 : tree decl;
3017 :
3018 : /* Write reference to FUNCTION_DECL. If there is not function,
3019 : write reference to void_type_node. */
3020 183657 : decl = (state->fn_decl) ? state->fn_decl : void_type_node;
3021 183657 : streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
3022 183657 : gcc_assert (ref != (unsigned)-1);
3023 183657 : ref = ref * 2 + (state->compressed ? 1 : 0);
3024 183657 : lto_write_data (&ref, sizeof (uint32_t));
3025 :
3026 550971 : for (i = 0; i < LTO_N_DECL_STREAMS; i++)
3027 183657 : write_global_references (ob, &state->streams[i]);
3028 183657 : }
3029 :
3030 :
3031 : /* Return the written size of STATE. */
3032 :
3033 : static size_t
3034 183657 : lto_out_decl_state_written_size (struct lto_out_decl_state *state)
3035 : {
3036 183657 : int i;
3037 183657 : size_t size;
3038 :
3039 183657 : size = sizeof (int32_t); /* fn_ref. */
3040 0 : for (i = 0; i < LTO_N_DECL_STREAMS; i++)
3041 : {
3042 183657 : size += sizeof (int32_t); /* vector size. */
3043 0 : size += (lto_tree_ref_encoder_size (&state->streams[i])
3044 0 : * sizeof (int32_t));
3045 : }
3046 183657 : return size;
3047 : }
3048 :
3049 :
3050 : /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
3051 : so far. */
3052 :
3053 : static void
3054 542758 : write_symbol (struct streamer_tree_cache_d *cache,
3055 : tree t, hash_set<const char *> *seen, bool alias)
3056 : {
3057 542758 : const char *name;
3058 542758 : enum gcc_plugin_symbol_kind kind;
3059 542758 : enum gcc_plugin_symbol_visibility visibility = GCCPV_DEFAULT;
3060 542758 : unsigned slot_num;
3061 542758 : uint64_t size;
3062 542758 : const char *comdat;
3063 542758 : unsigned char c;
3064 :
3065 542758 : gcc_assert (VAR_OR_FUNCTION_DECL_P (t));
3066 :
3067 542758 : name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
3068 :
3069 : /* This behaves like assemble_name_raw in varasm.cc, performing the
3070 : same name manipulations that ASM_OUTPUT_LABELREF does. */
3071 542758 : name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
3072 :
3073 542758 : if (seen->add (name))
3074 769 : return;
3075 :
3076 541989 : streamer_tree_cache_lookup (cache, t, &slot_num);
3077 541989 : gcc_assert (slot_num != (unsigned)-1);
3078 :
3079 541989 : if (DECL_EXTERNAL (t))
3080 : {
3081 215146 : if (DECL_WEAK (t))
3082 : kind = GCCPK_WEAKUNDEF;
3083 : else
3084 215115 : kind = GCCPK_UNDEF;
3085 : }
3086 : else
3087 : {
3088 326843 : if (DECL_WEAK (t))
3089 : kind = GCCPK_WEAKDEF;
3090 314953 : else if (DECL_COMMON (t))
3091 : kind = GCCPK_COMMON;
3092 : else
3093 314892 : kind = GCCPK_DEF;
3094 :
3095 : /* When something is defined, it should have node attached. */
3096 326843 : gcc_assert (alias || !VAR_P (t) || varpool_node::get (t)->definition);
3097 326843 : gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
3098 : || (cgraph_node::get (t)
3099 : && cgraph_node::get (t)->definition));
3100 : }
3101 :
3102 : /* Imitate what default_elf_asm_output_external do.
3103 : When symbol is external, we need to output it with DEFAULT visibility
3104 : when compiling with -fvisibility=default, while with HIDDEN visibility
3105 : when symbol has attribute (visibility("hidden")) specified.
3106 : targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
3107 : right. */
3108 :
3109 541989 : if (DECL_EXTERNAL (t)
3110 541989 : && !targetm.binds_local_p (t))
3111 : visibility = GCCPV_DEFAULT;
3112 : else
3113 326896 : switch (DECL_VISIBILITY (t))
3114 : {
3115 : case VISIBILITY_DEFAULT:
3116 : visibility = GCCPV_DEFAULT;
3117 : break;
3118 : case VISIBILITY_PROTECTED:
3119 541989 : visibility = GCCPV_PROTECTED;
3120 : break;
3121 : case VISIBILITY_HIDDEN:
3122 : visibility = GCCPV_HIDDEN;
3123 : break;
3124 : case VISIBILITY_INTERNAL:
3125 : visibility = GCCPV_INTERNAL;
3126 : break;
3127 : }
3128 :
3129 541989 : if (kind == GCCPK_COMMON
3130 61 : && DECL_SIZE_UNIT (t)
3131 542050 : && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
3132 61 : size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
3133 : else
3134 541928 : size = 0;
3135 :
3136 541989 : if (DECL_ONE_ONLY (t))
3137 11846 : comdat = IDENTIFIER_POINTER (decl_comdat_group_id (t));
3138 : else
3139 : comdat = "";
3140 :
3141 541989 : lto_write_data (name, strlen (name) + 1);
3142 541989 : lto_write_data (comdat, strlen (comdat) + 1);
3143 541989 : c = (unsigned char) kind;
3144 541989 : lto_write_data (&c, 1);
3145 541989 : c = (unsigned char) visibility;
3146 541989 : lto_write_data (&c, 1);
3147 541989 : lto_write_data (&size, 8);
3148 541989 : lto_write_data (&slot_num, 4);
3149 : }
3150 :
3151 : /* Write extension information for symbols (symbol type, section flags). */
3152 :
3153 : static void
3154 542758 : write_symbol_extension_info (tree t)
3155 : {
3156 542758 : unsigned char c;
3157 542758 : c = ((unsigned char) TREE_CODE (t) == VAR_DECL
3158 : ? GCCST_VARIABLE : GCCST_FUNCTION);
3159 542758 : lto_write_data (&c, 1);
3160 542758 : unsigned char section_kind = 0;
3161 542758 : if (VAR_P (t))
3162 : {
3163 233813 : section *s = get_variable_section (t, false);
3164 233813 : if (s->common.flags & SECTION_BSS)
3165 223894 : section_kind |= GCCSSK_BSS;
3166 : }
3167 542758 : lto_write_data (§ion_kind, 1);
3168 542758 : }
3169 :
3170 : /* Write an IL symbol table to OB.
3171 : SET and VSET are cgraph/varpool node sets we are outputting. */
3172 :
3173 : static unsigned int
3174 23036 : produce_symtab (struct output_block *ob)
3175 : {
3176 23036 : unsigned int streamed_symbols = 0;
3177 23036 : struct streamer_tree_cache_d *cache = ob->writer_cache;
3178 23036 : char *section_name = lto_get_section_name (LTO_section_symtab, NULL, 0, NULL);
3179 23036 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3180 23036 : lto_symtab_encoder_iterator lsei;
3181 :
3182 23036 : lto_begin_section (section_name, false);
3183 23036 : free (section_name);
3184 :
3185 23036 : hash_set<const char *> seen;
3186 :
3187 : /* Write the symbol table.
3188 : First write everything defined and then all declarations.
3189 : This is necessary to handle cases where we have duplicated symbols. */
3190 23036 : for (lsei = lsei_start (encoder);
3191 621085 : !lsei_end_p (lsei); lsei_next (&lsei))
3192 : {
3193 598049 : toplevel_node *tnode = lsei_node (lsei);
3194 598049 : symtab_node *node = dyn_cast<symtab_node*> (tnode);
3195 598049 : if (!node)
3196 84 : continue;
3197 :
3198 597965 : if (DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3199 271122 : continue;
3200 326843 : write_symbol (cache, node->decl, &seen, false);
3201 326843 : ++streamed_symbols;
3202 : }
3203 621085 : for (lsei = lsei_start (encoder);
3204 621085 : !lsei_end_p (lsei); lsei_next (&lsei))
3205 : {
3206 598049 : toplevel_node *tnode = lsei_node (lsei);
3207 598049 : symtab_node *node = dyn_cast<symtab_node*> (tnode);
3208 598049 : if (!node)
3209 84 : continue;
3210 :
3211 597965 : if (!DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3212 382050 : continue;
3213 215915 : write_symbol (cache, node->decl, &seen, false);
3214 215915 : ++streamed_symbols;
3215 : }
3216 :
3217 23036 : lto_end_section ();
3218 :
3219 23036 : return streamed_symbols;
3220 23036 : }
3221 :
3222 : /* Symtab extension version. */
3223 : #define LTO_SYMTAB_EXTENSION_VERSION 1
3224 :
3225 : /* Write an IL symbol table extension to OB.
3226 : SET and VSET are cgraph/varpool node sets we are outputting. */
3227 :
3228 : static void
3229 23036 : produce_symtab_extension (struct output_block *ob,
3230 : unsigned int previous_streamed_symbols)
3231 : {
3232 23036 : unsigned int streamed_symbols = 0;
3233 23036 : char *section_name = lto_get_section_name (LTO_section_symtab_extension,
3234 : NULL, 0, NULL);
3235 23036 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3236 23036 : lto_symtab_encoder_iterator lsei;
3237 :
3238 23036 : lto_begin_section (section_name, false);
3239 23036 : free (section_name);
3240 :
3241 23036 : unsigned char version = LTO_SYMTAB_EXTENSION_VERSION;
3242 23036 : lto_write_data (&version, 1);
3243 :
3244 : /* Write the symbol table.
3245 : First write everything defined and then all declarations.
3246 : This is necessary to handle cases where we have duplicated symbols. */
3247 23036 : for (lsei = lsei_start (encoder);
3248 621085 : !lsei_end_p (lsei); lsei_next (&lsei))
3249 : {
3250 598049 : toplevel_node *tnode = lsei_node (lsei);
3251 598049 : symtab_node *node = dyn_cast<symtab_node*> (tnode);
3252 598049 : if (!node)
3253 84 : continue;
3254 :
3255 597965 : if (DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3256 271122 : continue;
3257 326843 : write_symbol_extension_info (node->decl);
3258 326843 : ++streamed_symbols;
3259 : }
3260 621085 : for (lsei = lsei_start (encoder);
3261 621085 : !lsei_end_p (lsei); lsei_next (&lsei))
3262 : {
3263 598049 : toplevel_node *tnode = lsei_node (lsei);
3264 598049 : symtab_node *node = dyn_cast<symtab_node*> (tnode);
3265 598049 : if (!node)
3266 84 : continue;
3267 :
3268 597965 : if (!DECL_EXTERNAL (node->decl) || !node->output_to_lto_symbol_table_p ())
3269 382050 : continue;
3270 215915 : write_symbol_extension_info (node->decl);
3271 215915 : ++streamed_symbols;
3272 : }
3273 :
3274 23036 : gcc_assert (previous_streamed_symbols == streamed_symbols);
3275 23036 : lto_end_section ();
3276 23036 : }
3277 :
3278 :
3279 : /* Init the streamer_mode_table for output, where we collect info on what
3280 : machine_mode values have been streamed. */
3281 : void
3282 31226 : lto_output_init_mode_table (void)
3283 : {
3284 31226 : memset (streamer_mode_table, '\0', MAX_MACHINE_MODE);
3285 31226 : }
3286 :
3287 :
3288 : /* Write the mode table. */
3289 : static void
3290 0 : lto_write_mode_table (void)
3291 : {
3292 0 : struct output_block *ob;
3293 0 : ob = create_output_block (LTO_section_mode_table);
3294 0 : bitpack_d bp = bitpack_create (ob->main_stream);
3295 :
3296 0 : if (lto_stream_offload_p)
3297 0 : bp_pack_value (&bp, NUM_POLY_INT_COEFFS, MAX_NUM_POLY_INT_COEFFS_BITS);
3298 :
3299 : /* Ensure that for GET_MODE_INNER (m) != m we have
3300 : also the inner mode marked. */
3301 0 : for (int i = 0; i < (int) MAX_MACHINE_MODE; i++)
3302 0 : if (streamer_mode_table[i])
3303 : {
3304 0 : machine_mode m = (machine_mode) i;
3305 0 : machine_mode inner_m = GET_MODE_INNER (m);
3306 0 : if (inner_m != m)
3307 0 : streamer_mode_table[(int) inner_m] = 1;
3308 : }
3309 :
3310 : /* Pack the mode_bits value within 5 bits (up to 31) in the beginning. */
3311 0 : unsigned mode_bits = ceil_log2 (MAX_MACHINE_MODE);
3312 0 : bp_pack_value (&bp, mode_bits, 5);
3313 :
3314 : /* First stream modes that have GET_MODE_INNER (m) == m,
3315 : so that we can refer to them afterwards. */
3316 0 : for (int pass = 0; pass < 2; pass++)
3317 0 : for (int i = 0; i < (int) MAX_MACHINE_MODE; i++)
3318 0 : if (streamer_mode_table[i] && i != (int) VOIDmode && i != (int) BLKmode)
3319 : {
3320 0 : machine_mode m = (machine_mode) i;
3321 0 : if ((GET_MODE_INNER (m) == m) ^ (pass == 0))
3322 0 : continue;
3323 0 : bp_pack_value (&bp, m, mode_bits);
3324 0 : bp_pack_enum (&bp, mode_class, MAX_MODE_CLASS, GET_MODE_CLASS (m));
3325 0 : bp_pack_poly_value (&bp, GET_MODE_SIZE (m), 16);
3326 0 : bp_pack_poly_value (&bp, GET_MODE_PRECISION (m), 16);
3327 0 : bp_pack_value (&bp, GET_MODE_INNER (m), mode_bits);
3328 0 : bp_pack_poly_value (&bp, GET_MODE_NUNITS (m), 16);
3329 0 : switch (GET_MODE_CLASS (m))
3330 : {
3331 0 : case MODE_FRACT:
3332 0 : case MODE_UFRACT:
3333 0 : case MODE_ACCUM:
3334 0 : case MODE_UACCUM:
3335 0 : bp_pack_value (&bp, GET_MODE_IBIT (m), 8);
3336 0 : bp_pack_value (&bp, GET_MODE_FBIT (m), 8);
3337 0 : break;
3338 0 : case MODE_FLOAT:
3339 0 : case MODE_DECIMAL_FLOAT:
3340 0 : bp_pack_string (ob, &bp, REAL_MODE_FORMAT (m)->name, true);
3341 0 : break;
3342 : default:
3343 : break;
3344 : }
3345 0 : bp_pack_string (ob, &bp, GET_MODE_NAME (m), true);
3346 : }
3347 0 : bp_pack_value (&bp, VOIDmode, mode_bits);
3348 :
3349 0 : streamer_write_bitpack (&bp);
3350 :
3351 0 : char *section_name
3352 0 : = lto_get_section_name (LTO_section_mode_table, NULL, 0, NULL);
3353 0 : lto_begin_section (section_name, !flag_wpa);
3354 0 : free (section_name);
3355 :
3356 : /* The entire header stream is computed here. */
3357 0 : struct lto_simple_header_with_strings header;
3358 0 : memset (&header, 0, sizeof (header));
3359 :
3360 0 : header.main_size = ob->main_stream->total_size;
3361 0 : header.string_size = ob->string_stream->total_size;
3362 0 : lto_write_data (&header, sizeof header);
3363 :
3364 : /* Put all of the gimple and the string table out the asm file as a
3365 : block of text. */
3366 0 : lto_write_stream (ob->main_stream);
3367 0 : lto_write_stream (ob->string_stream);
3368 :
3369 0 : lto_end_section ();
3370 0 : destroy_output_block (ob);
3371 0 : }
3372 :
3373 :
3374 : /* This pass is run after all of the functions are serialized and all
3375 : of the IPA passes have written their serialized forms. This pass
3376 : causes the vector of all of the global decls and types used from
3377 : this file to be written in to a section that can then be read in to
3378 : recover these on other side. */
3379 :
3380 : void
3381 31226 : produce_asm_for_decls (void)
3382 : {
3383 31226 : struct lto_out_decl_state *out_state;
3384 31226 : struct lto_out_decl_state *fn_out_state;
3385 31226 : struct lto_decl_header header;
3386 31226 : char *section_name;
3387 31226 : struct output_block *ob;
3388 31226 : unsigned idx, num_fns;
3389 31226 : size_t decl_state_size;
3390 31226 : int32_t num_decl_states;
3391 :
3392 31226 : ob = create_output_block (LTO_section_decls);
3393 :
3394 31226 : memset (&header, 0, sizeof (struct lto_decl_header));
3395 :
3396 31226 : section_name = lto_get_section_name (LTO_section_decls, NULL, 0, NULL);
3397 31226 : lto_begin_section (section_name, !flag_wpa);
3398 31226 : free (section_name);
3399 :
3400 : /* Make string 0 be a NULL string. */
3401 31226 : streamer_write_char_stream (ob->string_stream, 0);
3402 :
3403 31226 : gcc_assert (!alias_pairs);
3404 :
3405 : /* Get rid of the global decl state hash tables to save some memory. */
3406 31226 : out_state = lto_get_out_decl_state ();
3407 93678 : for (int i = 0; i < LTO_N_DECL_STREAMS; i++)
3408 31226 : if (out_state->streams[i].tree_hash_table)
3409 : {
3410 31226 : delete out_state->streams[i].tree_hash_table;
3411 31226 : out_state->streams[i].tree_hash_table = NULL;
3412 : }
3413 :
3414 : /* Write the global symbols. */
3415 31226 : if (streamer_dump_file)
3416 4 : fprintf (streamer_dump_file, "Outputting global stream\n");
3417 31226 : lto_output_decl_state_streams (ob, out_state);
3418 31226 : num_fns = lto_function_decl_states.length ();
3419 183657 : for (idx = 0; idx < num_fns; idx++)
3420 : {
3421 152431 : fn_out_state =
3422 152431 : lto_function_decl_states[idx];
3423 152431 : if (streamer_dump_file)
3424 24 : fprintf (streamer_dump_file, "Outputting stream for %s\n",
3425 12 : IDENTIFIER_POINTER
3426 : (DECL_ASSEMBLER_NAME (fn_out_state->fn_decl)));
3427 152431 : lto_output_decl_state_streams (ob, fn_out_state);
3428 : }
3429 :
3430 : /* Currently not used. This field would allow us to preallocate
3431 : the globals vector, so that it need not be resized as it is extended. */
3432 31226 : header.num_nodes = -1;
3433 :
3434 : /* Compute the total size of all decl out states. */
3435 31226 : decl_state_size = sizeof (int32_t);
3436 31226 : decl_state_size += lto_out_decl_state_written_size (out_state);
3437 183657 : for (idx = 0; idx < num_fns; idx++)
3438 : {
3439 152431 : fn_out_state =
3440 152431 : lto_function_decl_states[idx];
3441 304185 : decl_state_size += lto_out_decl_state_written_size (fn_out_state);
3442 : }
3443 31226 : header.decl_state_size = decl_state_size;
3444 :
3445 31226 : header.main_size = ob->main_stream->total_size;
3446 31226 : header.string_size = ob->string_stream->total_size;
3447 :
3448 31226 : lto_write_data (&header, sizeof header);
3449 :
3450 : /* Write the main out-decl state, followed by out-decl states of
3451 : functions. */
3452 31226 : num_decl_states = num_fns + 1;
3453 31226 : lto_write_data (&num_decl_states, sizeof (num_decl_states));
3454 31226 : lto_output_decl_state_refs (ob, out_state);
3455 214883 : for (idx = 0; idx < num_fns; idx++)
3456 : {
3457 152431 : fn_out_state = lto_function_decl_states[idx];
3458 152431 : lto_output_decl_state_refs (ob, fn_out_state);
3459 : }
3460 :
3461 31226 : lto_write_stream (ob->main_stream);
3462 31226 : lto_write_stream (ob->string_stream);
3463 :
3464 31226 : lto_end_section ();
3465 :
3466 : /* Write the symbol table. It is used by linker to determine dependencies
3467 : and thus we can skip it for WPA. */
3468 31226 : if (!flag_wpa)
3469 : {
3470 23036 : unsigned int streamed_symbols = produce_symtab (ob);
3471 23036 : produce_symtab_extension (ob, streamed_symbols);
3472 : }
3473 :
3474 : /* Write command line opts. */
3475 31226 : lto_write_options ();
3476 :
3477 : /* Deallocate memory and clean up. */
3478 214883 : for (idx = 0; idx < num_fns; idx++)
3479 : {
3480 152431 : fn_out_state =
3481 152431 : lto_function_decl_states[idx];
3482 152431 : lto_delete_out_decl_state (fn_out_state);
3483 : }
3484 31226 : lto_symtab_encoder_delete (ob->decl_state->symtab_node_encoder);
3485 31226 : lto_function_decl_states.release ();
3486 31226 : destroy_output_block (ob);
3487 31226 : if (lto_stream_offload_p)
3488 0 : lto_write_mode_table ();
3489 31226 : }
|