Fix bootstrap/PR63632
[official-gcc.git] / gcc / lto-streamer-out.c
blob7d29d4fc68cce6a287c9be5d9cec6b9a3897276c
1 /* Write the GIMPLE representation to a file stream.
3 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 Re-implemented by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
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.
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.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "stor-layout.h"
29 #include "stringpool.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "hashtab.h"
35 #include "hash-set.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "gimple-iterator.h"
43 #include "gimple-ssa.h"
44 #include "tree-ssanames.h"
45 #include "tree-pass.h"
46 #include "vec.h"
47 #include "machmode.h"
48 #include "hard-reg-set.h"
49 #include "function.h"
50 #include "diagnostic-core.h"
51 #include "inchash.h"
52 #include "except.h"
53 #include "lto-symtab.h"
54 #include "lto-streamer.h"
55 #include "data-streamer.h"
56 #include "gimple-streamer.h"
57 #include "tree-streamer.h"
58 #include "streamer-hooks.h"
59 #include "cfgloop.h"
60 #include "builtins.h"
63 static void lto_write_tree (struct output_block*, tree, bool);
65 /* Clear the line info stored in DATA_IN. */
67 static void
68 clear_line_info (struct output_block *ob)
70 ob->current_file = NULL;
71 ob->current_line = 0;
72 ob->current_col = 0;
76 /* Create the output block and return it. SECTION_TYPE is
77 LTO_section_function_body or LTO_static_initializer. */
79 struct output_block *
80 create_output_block (enum lto_section_type section_type)
82 struct output_block *ob = XCNEW (struct output_block);
84 ob->section_type = section_type;
85 ob->decl_state = lto_get_out_decl_state ();
86 ob->main_stream = XCNEW (struct lto_output_stream);
87 ob->string_stream = XCNEW (struct lto_output_stream);
88 ob->writer_cache = streamer_tree_cache_create (!flag_wpa, true, false);
90 if (section_type == LTO_section_function_body)
91 ob->cfg_stream = XCNEW (struct lto_output_stream);
93 clear_line_info (ob);
95 ob->string_hash_table = new hash_table<string_slot_hasher> (37);
96 gcc_obstack_init (&ob->obstack);
98 return ob;
102 /* Destroy the output block OB. */
104 void
105 destroy_output_block (struct output_block *ob)
107 enum lto_section_type section_type = ob->section_type;
109 delete ob->string_hash_table;
110 ob->string_hash_table = NULL;
112 free (ob->main_stream);
113 free (ob->string_stream);
114 if (section_type == LTO_section_function_body)
115 free (ob->cfg_stream);
117 streamer_tree_cache_delete (ob->writer_cache);
118 obstack_free (&ob->obstack, NULL);
120 free (ob);
124 /* Look up NODE in the type table and write the index for it to OB. */
126 static void
127 output_type_ref (struct output_block *ob, tree node)
129 streamer_write_record_start (ob, LTO_type_ref);
130 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
134 /* Return true if tree node T is written to various tables. For these
135 nodes, we sometimes want to write their phyiscal representation
136 (via lto_output_tree), and sometimes we need to emit an index
137 reference into a table (via lto_output_tree_ref). */
139 static bool
140 tree_is_indexable (tree t)
142 /* Parameters and return values of functions of variably modified types
143 must go to global stream, because they may be used in the type
144 definition. */
145 if (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == RESULT_DECL)
146 return variably_modified_type_p (TREE_TYPE (DECL_CONTEXT (t)), NULL_TREE);
147 /* IMPORTED_DECL is put into BLOCK and thus it never can be shared. */
148 else if (TREE_CODE (t) == IMPORTED_DECL)
149 return false;
150 else if (((TREE_CODE (t) == VAR_DECL && !TREE_STATIC (t))
151 || TREE_CODE (t) == TYPE_DECL
152 || TREE_CODE (t) == CONST_DECL
153 || TREE_CODE (t) == NAMELIST_DECL)
154 && decl_function_context (t))
155 return false;
156 else if (TREE_CODE (t) == DEBUG_EXPR_DECL)
157 return false;
158 /* Variably modified types need to be streamed alongside function
159 bodies because they can refer to local entities. Together with
160 them we have to localize their members as well.
161 ??? In theory that includes non-FIELD_DECLs as well. */
162 else if (TYPE_P (t)
163 && variably_modified_type_p (t, NULL_TREE))
164 return false;
165 else if (TREE_CODE (t) == FIELD_DECL
166 && variably_modified_type_p (DECL_CONTEXT (t), NULL_TREE))
167 return false;
168 else
169 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
173 /* Output info about new location into bitpack BP.
174 After outputting bitpack, lto_output_location_data has
175 to be done to output actual data. */
177 void
178 lto_output_location (struct output_block *ob, struct bitpack_d *bp,
179 location_t loc)
181 expanded_location xloc;
183 loc = LOCATION_LOCUS (loc);
184 bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
185 if (loc == UNKNOWN_LOCATION)
186 return;
188 xloc = expand_location (loc);
190 bp_pack_value (bp, ob->current_file != xloc.file, 1);
191 bp_pack_value (bp, ob->current_line != xloc.line, 1);
192 bp_pack_value (bp, ob->current_col != xloc.column, 1);
194 if (ob->current_file != xloc.file)
195 bp_pack_string (ob, bp, xloc.file, true);
196 ob->current_file = xloc.file;
198 if (ob->current_line != xloc.line)
199 bp_pack_var_len_unsigned (bp, xloc.line);
200 ob->current_line = xloc.line;
202 if (ob->current_col != xloc.column)
203 bp_pack_var_len_unsigned (bp, xloc.column);
204 ob->current_col = xloc.column;
208 /* If EXPR is an indexable tree node, output a reference to it to
209 output block OB. Otherwise, output the physical representation of
210 EXPR to OB. */
212 static void
213 lto_output_tree_ref (struct output_block *ob, tree expr)
215 enum tree_code code;
217 if (TYPE_P (expr))
219 output_type_ref (ob, expr);
220 return;
223 code = TREE_CODE (expr);
224 switch (code)
226 case SSA_NAME:
227 streamer_write_record_start (ob, LTO_ssa_name_ref);
228 streamer_write_uhwi (ob, SSA_NAME_VERSION (expr));
229 break;
231 case FIELD_DECL:
232 streamer_write_record_start (ob, LTO_field_decl_ref);
233 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
234 break;
236 case FUNCTION_DECL:
237 streamer_write_record_start (ob, LTO_function_decl_ref);
238 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
239 break;
241 case VAR_DECL:
242 case DEBUG_EXPR_DECL:
243 gcc_assert (decl_function_context (expr) == NULL || TREE_STATIC (expr));
244 case PARM_DECL:
245 streamer_write_record_start (ob, LTO_global_decl_ref);
246 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
247 break;
249 case CONST_DECL:
250 streamer_write_record_start (ob, LTO_const_decl_ref);
251 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
252 break;
254 case IMPORTED_DECL:
255 gcc_assert (decl_function_context (expr) == NULL);
256 streamer_write_record_start (ob, LTO_imported_decl_ref);
257 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
258 break;
260 case TYPE_DECL:
261 streamer_write_record_start (ob, LTO_type_decl_ref);
262 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
263 break;
265 case NAMELIST_DECL:
266 streamer_write_record_start (ob, LTO_namelist_decl_ref);
267 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
268 break;
270 case NAMESPACE_DECL:
271 streamer_write_record_start (ob, LTO_namespace_decl_ref);
272 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
273 break;
275 case LABEL_DECL:
276 streamer_write_record_start (ob, LTO_label_decl_ref);
277 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
278 break;
280 case RESULT_DECL:
281 streamer_write_record_start (ob, LTO_result_decl_ref);
282 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
283 break;
285 case TRANSLATION_UNIT_DECL:
286 streamer_write_record_start (ob, LTO_translation_unit_decl_ref);
287 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
288 break;
290 default:
291 /* No other node is indexable, so it should have been handled by
292 lto_output_tree. */
293 gcc_unreachable ();
298 /* Return true if EXPR is a tree node that can be written to disk. */
300 static inline bool
301 lto_is_streamable (tree expr)
303 enum tree_code code = TREE_CODE (expr);
305 /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
306 name version in lto_output_tree_ref (see output_ssa_names). */
307 return !is_lang_specific (expr)
308 && code != SSA_NAME
309 && code != CALL_EXPR
310 && code != LANG_TYPE
311 && code != MODIFY_EXPR
312 && code != INIT_EXPR
313 && code != TARGET_EXPR
314 && code != BIND_EXPR
315 && code != WITH_CLEANUP_EXPR
316 && code != STATEMENT_LIST
317 && (code == CASE_LABEL_EXPR
318 || code == DECL_EXPR
319 || TREE_CODE_CLASS (code) != tcc_statement);
323 /* For EXPR lookup and return what we want to stream to OB as DECL_INITIAL. */
325 static tree
326 get_symbol_initial_value (lto_symtab_encoder_t encoder, tree expr)
328 gcc_checking_assert (DECL_P (expr)
329 && TREE_CODE (expr) != FUNCTION_DECL
330 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL);
332 /* Handle DECL_INITIAL for symbols. */
333 tree initial = DECL_INITIAL (expr);
334 if (TREE_CODE (expr) == VAR_DECL
335 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
336 && !DECL_IN_CONSTANT_POOL (expr)
337 && initial)
339 varpool_node *vnode;
340 /* Extra section needs about 30 bytes; do not produce it for simple
341 scalar values. */
342 if (TREE_CODE (DECL_INITIAL (expr)) == CONSTRUCTOR
343 || !(vnode = varpool_node::get (expr))
344 || !lto_symtab_encoder_encode_initializer_p (encoder, vnode))
345 initial = error_mark_node;
348 return initial;
352 /* Write a physical representation of tree node EXPR to output block
353 OB. If REF_P is true, the leaves of EXPR are emitted as references
354 via lto_output_tree_ref. IX is the index into the streamer cache
355 where EXPR is stored. */
357 static void
358 lto_write_tree_1 (struct output_block *ob, tree expr, bool ref_p)
360 /* Pack all the non-pointer fields in EXPR into a bitpack and write
361 the resulting bitpack. */
362 bitpack_d bp = bitpack_create (ob->main_stream);
363 streamer_pack_tree_bitfields (ob, &bp, expr);
364 streamer_write_bitpack (&bp);
366 /* Write all the pointer fields in EXPR. */
367 streamer_write_tree_body (ob, expr, ref_p);
369 /* Write any LTO-specific data to OB. */
370 if (DECL_P (expr)
371 && TREE_CODE (expr) != FUNCTION_DECL
372 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
374 /* Handle DECL_INITIAL for symbols. */
375 tree initial = get_symbol_initial_value
376 (ob->decl_state->symtab_node_encoder, expr);
377 stream_write_tree (ob, initial, ref_p);
381 /* Write a physical representation of tree node EXPR to output block
382 OB. If REF_P is true, the leaves of EXPR are emitted as references
383 via lto_output_tree_ref. IX is the index into the streamer cache
384 where EXPR is stored. */
386 static void
387 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
389 if (!lto_is_streamable (expr))
390 internal_error ("tree code %qs is not supported in LTO streams",
391 get_tree_code_name (TREE_CODE (expr)));
393 /* Write the header, containing everything needed to materialize
394 EXPR on the reading side. */
395 streamer_write_tree_header (ob, expr);
397 lto_write_tree_1 (ob, expr, ref_p);
399 /* Mark the end of EXPR. */
400 streamer_write_zero (ob);
403 /* Emit the physical representation of tree node EXPR to output block
404 OB. If THIS_REF_P is true, the leaves of EXPR are emitted as references
405 via lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
407 static void
408 lto_output_tree_1 (struct output_block *ob, tree expr, hashval_t hash,
409 bool ref_p, bool this_ref_p)
411 unsigned ix;
413 gcc_checking_assert (expr != NULL_TREE
414 && !(this_ref_p && tree_is_indexable (expr)));
416 bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
417 expr, hash, &ix);
418 gcc_assert (!exists_p);
419 if (streamer_handle_as_builtin_p (expr))
421 /* MD and NORMAL builtins do not need to be written out
422 completely as they are always instantiated by the
423 compiler on startup. The only builtins that need to
424 be written out are BUILT_IN_FRONTEND. For all other
425 builtins, we simply write the class and code. */
426 streamer_write_builtin (ob, expr);
428 else if (TREE_CODE (expr) == INTEGER_CST
429 && !TREE_OVERFLOW (expr))
431 /* Shared INTEGER_CST nodes are special because they need their
432 original type to be materialized by the reader (to implement
433 TYPE_CACHED_VALUES). */
434 streamer_write_integer_cst (ob, expr, ref_p);
436 else
438 /* This is the first time we see EXPR, write its fields
439 to OB. */
440 lto_write_tree (ob, expr, ref_p);
444 class DFS
446 public:
447 DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
448 bool single_p);
449 ~DFS ();
451 struct scc_entry
453 tree t;
454 hashval_t hash;
456 vec<scc_entry> sccstack;
458 private:
459 struct sccs
461 unsigned int dfsnum;
462 unsigned int low;
465 static int scc_entry_compare (const void *, const void *);
467 void DFS_write_tree_body (struct output_block *ob,
468 tree expr, sccs *expr_state, bool ref_p,
469 bool single_p);
471 void DFS_write_tree (struct output_block *ob, sccs *from_state,
472 tree expr, bool ref_p, bool this_ref_p,
473 bool single_p);
474 hashval_t
475 hash_scc (struct output_block *ob, unsigned first, unsigned size);
477 unsigned int next_dfs_num;
478 hash_map<tree, sccs *> sccstate;
479 struct obstack sccstate_obstack;
482 DFS::DFS (struct output_block *ob, tree expr, bool ref_p, bool this_ref_p,
483 bool single_p)
485 sccstack.create (0);
486 gcc_obstack_init (&sccstate_obstack);
487 next_dfs_num = 1;
488 DFS_write_tree (ob, NULL, expr, ref_p, this_ref_p, single_p);
491 DFS::~DFS ()
493 sccstack.release ();
494 obstack_free (&sccstate_obstack, NULL);
497 /* Handle the tree EXPR in the DFS walk with SCC state EXPR_STATE and
498 DFS recurse for all tree edges originating from it. */
500 void
501 DFS::DFS_write_tree_body (struct output_block *ob,
502 tree expr, sccs *expr_state, bool ref_p,
503 bool single_p)
505 #define DFS_follow_tree_edge(DEST) \
506 DFS_write_tree (ob, expr_state, DEST, ref_p, ref_p, single_p)
508 enum tree_code code;
510 code = TREE_CODE (expr);
512 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
514 if (TREE_CODE (expr) != IDENTIFIER_NODE)
515 DFS_follow_tree_edge (TREE_TYPE (expr));
518 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
520 for (unsigned i = 0; i < VECTOR_CST_NELTS (expr); ++i)
521 DFS_follow_tree_edge (VECTOR_CST_ELT (expr, i));
524 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
526 DFS_follow_tree_edge (TREE_REALPART (expr));
527 DFS_follow_tree_edge (TREE_IMAGPART (expr));
530 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
532 /* Drop names that were created for anonymous entities. */
533 if (DECL_NAME (expr)
534 && TREE_CODE (DECL_NAME (expr)) == IDENTIFIER_NODE
535 && ANON_AGGRNAME_P (DECL_NAME (expr)))
537 else
538 DFS_follow_tree_edge (DECL_NAME (expr));
539 DFS_follow_tree_edge (DECL_CONTEXT (expr));
542 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
544 DFS_follow_tree_edge (DECL_SIZE (expr));
545 DFS_follow_tree_edge (DECL_SIZE_UNIT (expr));
547 /* Note, DECL_INITIAL is not handled here. Since DECL_INITIAL needs
548 special handling in LTO, it must be handled by streamer hooks. */
550 DFS_follow_tree_edge (DECL_ATTRIBUTES (expr));
552 /* Do not follow DECL_ABSTRACT_ORIGIN. We cannot handle debug information
553 for early inlining so drop it on the floor instead of ICEing in
554 dwarf2out.c. */
556 if ((TREE_CODE (expr) == VAR_DECL
557 || TREE_CODE (expr) == PARM_DECL)
558 && DECL_HAS_VALUE_EXPR_P (expr))
559 DFS_follow_tree_edge (DECL_VALUE_EXPR (expr));
560 if (TREE_CODE (expr) == VAR_DECL)
561 DFS_follow_tree_edge (DECL_DEBUG_EXPR (expr));
564 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
566 if (TREE_CODE (expr) == TYPE_DECL)
567 DFS_follow_tree_edge (DECL_ORIGINAL_TYPE (expr));
570 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
572 /* Make sure we don't inadvertently set the assembler name. */
573 if (DECL_ASSEMBLER_NAME_SET_P (expr))
574 DFS_follow_tree_edge (DECL_ASSEMBLER_NAME (expr));
577 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
579 DFS_follow_tree_edge (DECL_FIELD_OFFSET (expr));
580 DFS_follow_tree_edge (DECL_BIT_FIELD_TYPE (expr));
581 DFS_follow_tree_edge (DECL_BIT_FIELD_REPRESENTATIVE (expr));
582 DFS_follow_tree_edge (DECL_FIELD_BIT_OFFSET (expr));
583 DFS_follow_tree_edge (DECL_FCONTEXT (expr));
586 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
588 DFS_follow_tree_edge (DECL_VINDEX (expr));
589 DFS_follow_tree_edge (DECL_FUNCTION_PERSONALITY (expr));
590 /* Do not DECL_FUNCTION_SPECIFIC_TARGET. They will be regenerated. */
591 DFS_follow_tree_edge (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (expr));
594 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
596 DFS_follow_tree_edge (TYPE_SIZE (expr));
597 DFS_follow_tree_edge (TYPE_SIZE_UNIT (expr));
598 DFS_follow_tree_edge (TYPE_ATTRIBUTES (expr));
599 DFS_follow_tree_edge (TYPE_NAME (expr));
600 /* Do not follow TYPE_POINTER_TO or TYPE_REFERENCE_TO. They will be
601 reconstructed during fixup. */
602 /* Do not follow TYPE_NEXT_VARIANT, we reconstruct the variant lists
603 during fixup. */
604 DFS_follow_tree_edge (TYPE_MAIN_VARIANT (expr));
605 DFS_follow_tree_edge (TYPE_CONTEXT (expr));
606 /* TYPE_CANONICAL is re-computed during type merging, so no need
607 to follow it here. */
608 DFS_follow_tree_edge (TYPE_STUB_DECL (expr));
611 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
613 if (TREE_CODE (expr) == ENUMERAL_TYPE)
614 DFS_follow_tree_edge (TYPE_VALUES (expr));
615 else if (TREE_CODE (expr) == ARRAY_TYPE)
616 DFS_follow_tree_edge (TYPE_DOMAIN (expr));
617 else if (RECORD_OR_UNION_TYPE_P (expr))
618 for (tree t = TYPE_FIELDS (expr); t; t = TREE_CHAIN (t))
619 DFS_follow_tree_edge (t);
620 else if (TREE_CODE (expr) == FUNCTION_TYPE
621 || TREE_CODE (expr) == METHOD_TYPE)
622 DFS_follow_tree_edge (TYPE_ARG_TYPES (expr));
624 if (!POINTER_TYPE_P (expr))
625 DFS_follow_tree_edge (TYPE_MINVAL (expr));
626 DFS_follow_tree_edge (TYPE_MAXVAL (expr));
627 if (RECORD_OR_UNION_TYPE_P (expr))
628 DFS_follow_tree_edge (TYPE_BINFO (expr));
631 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
633 DFS_follow_tree_edge (TREE_PURPOSE (expr));
634 DFS_follow_tree_edge (TREE_VALUE (expr));
635 DFS_follow_tree_edge (TREE_CHAIN (expr));
638 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
640 for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
641 DFS_follow_tree_edge (TREE_VEC_ELT (expr, i));
644 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
646 for (int i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
647 DFS_follow_tree_edge (TREE_OPERAND (expr, i));
648 DFS_follow_tree_edge (TREE_BLOCK (expr));
651 if (CODE_CONTAINS_STRUCT (code, TS_BLOCK))
653 for (tree t = BLOCK_VARS (expr); t; t = TREE_CHAIN (t))
654 if (VAR_OR_FUNCTION_DECL_P (t)
655 && DECL_EXTERNAL (t))
656 /* We have to stream externals in the block chain as
657 non-references. See also
658 tree-streamer-out.c:streamer_write_chain. */
659 DFS_write_tree (ob, expr_state, t, ref_p, false, single_p);
660 else
661 DFS_follow_tree_edge (t);
663 DFS_follow_tree_edge (BLOCK_SUPERCONTEXT (expr));
665 /* Follow BLOCK_ABSTRACT_ORIGIN for the limited cases we can
666 handle - those that represent inlined function scopes.
667 For the drop rest them on the floor instead of ICEing
668 in dwarf2out.c. */
669 if (inlined_function_outer_scope_p (expr))
671 tree ultimate_origin = block_ultimate_origin (expr);
672 DFS_follow_tree_edge (ultimate_origin);
674 /* Do not follow BLOCK_NONLOCALIZED_VARS. We cannot handle debug
675 information for early inlined BLOCKs so drop it on the floor instead
676 of ICEing in dwarf2out.c. */
678 /* BLOCK_FRAGMENT_ORIGIN and BLOCK_FRAGMENT_CHAIN is not live at LTO
679 streaming time. */
681 /* Do not output BLOCK_SUBBLOCKS. Instead on streaming-in this
682 list is re-constructed from BLOCK_SUPERCONTEXT. */
685 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
687 unsigned i;
688 tree t;
690 /* Note that the number of BINFO slots has already been emitted in
691 EXPR's header (see streamer_write_tree_header) because this length
692 is needed to build the empty BINFO node on the reader side. */
693 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (expr), i, t)
694 DFS_follow_tree_edge (t);
695 DFS_follow_tree_edge (BINFO_OFFSET (expr));
696 DFS_follow_tree_edge (BINFO_VTABLE (expr));
697 DFS_follow_tree_edge (BINFO_VPTR_FIELD (expr));
699 /* The number of BINFO_BASE_ACCESSES has already been emitted in
700 EXPR's bitfield section. */
701 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (expr), i, t)
702 DFS_follow_tree_edge (t);
704 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
705 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
708 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
710 unsigned i;
711 tree index, value;
713 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, index, value)
715 DFS_follow_tree_edge (index);
716 DFS_follow_tree_edge (value);
720 if (code == OMP_CLAUSE)
722 int i;
723 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (expr)]; i++)
724 DFS_follow_tree_edge (OMP_CLAUSE_OPERAND (expr, i));
725 DFS_follow_tree_edge (OMP_CLAUSE_CHAIN (expr));
728 #undef DFS_follow_tree_edge
731 /* Return a hash value for the tree T.
732 CACHE holds hash values of trees outside current SCC. MAP, if non-NULL,
733 may hold hash values if trees inside current SCC. */
735 static hashval_t
736 hash_tree (struct streamer_tree_cache_d *cache, hash_map<tree, hashval_t> *map, tree t)
738 inchash::hash hstate;
740 #define visit(SIBLING) \
741 do { \
742 unsigned ix; \
743 if (!SIBLING) \
744 hstate.add_int (0); \
745 else if (streamer_tree_cache_lookup (cache, SIBLING, &ix)) \
746 hstate.add_int (streamer_tree_cache_get_hash (cache, ix)); \
747 else if (map) \
748 hstate.add_int (*map->get (SIBLING)); \
749 else \
750 hstate.add_int (1); \
751 } while (0)
753 /* Hash TS_BASE. */
754 enum tree_code code = TREE_CODE (t);
755 hstate.add_int (code);
756 if (!TYPE_P (t))
758 hstate.add_flag (TREE_SIDE_EFFECTS (t));
759 hstate.add_flag (TREE_CONSTANT (t));
760 hstate.add_flag (TREE_READONLY (t));
761 hstate.add_flag (TREE_PUBLIC (t));
763 hstate.add_flag (TREE_ADDRESSABLE (t));
764 hstate.add_flag (TREE_THIS_VOLATILE (t));
765 if (DECL_P (t))
766 hstate.add_flag (DECL_UNSIGNED (t));
767 else if (TYPE_P (t))
768 hstate.add_flag (TYPE_UNSIGNED (t));
769 if (TYPE_P (t))
770 hstate.add_flag (TYPE_ARTIFICIAL (t));
771 else
772 hstate.add_flag (TREE_NO_WARNING (t));
773 hstate.add_flag (TREE_NOTHROW (t));
774 hstate.add_flag (TREE_STATIC (t));
775 hstate.add_flag (TREE_PROTECTED (t));
776 hstate.add_flag (TREE_DEPRECATED (t));
777 if (code != TREE_BINFO)
778 hstate.add_flag (TREE_PRIVATE (t));
779 if (TYPE_P (t))
781 hstate.add_flag (TYPE_SATURATING (t));
782 hstate.add_flag (TYPE_ADDR_SPACE (t));
784 else if (code == SSA_NAME)
785 hstate.add_flag (SSA_NAME_IS_DEFAULT_DEF (t));
786 hstate.commit_flag ();
788 if (CODE_CONTAINS_STRUCT (code, TS_INT_CST))
790 int i;
791 hstate.add_wide_int (TREE_INT_CST_NUNITS (t));
792 hstate.add_wide_int (TREE_INT_CST_EXT_NUNITS (t));
793 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
794 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
797 if (CODE_CONTAINS_STRUCT (code, TS_REAL_CST))
799 REAL_VALUE_TYPE r = TREE_REAL_CST (t);
800 hstate.add_flag (r.cl);
801 hstate.add_flag (r.sign);
802 hstate.add_flag (r.signalling);
803 hstate.add_flag (r.canonical);
804 hstate.commit_flag ();
805 hstate.add_int (r.uexp);
806 hstate.add (r.sig, sizeof (r.sig));
809 if (CODE_CONTAINS_STRUCT (code, TS_FIXED_CST))
811 FIXED_VALUE_TYPE f = TREE_FIXED_CST (t);
812 hstate.add_int (f.mode);
813 hstate.add_int (f.data.low);
814 hstate.add_int (f.data.high);
817 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
819 hstate.add_wide_int (DECL_MODE (t));
820 hstate.add_flag (DECL_NONLOCAL (t));
821 hstate.add_flag (DECL_VIRTUAL_P (t));
822 hstate.add_flag (DECL_IGNORED_P (t));
823 hstate.add_flag (DECL_ABSTRACT_P (t));
824 hstate.add_flag (DECL_ARTIFICIAL (t));
825 hstate.add_flag (DECL_USER_ALIGN (t));
826 hstate.add_flag (DECL_PRESERVE_P (t));
827 hstate.add_flag (DECL_EXTERNAL (t));
828 hstate.add_flag (DECL_GIMPLE_REG_P (t));
829 hstate.commit_flag ();
830 hstate.add_int (DECL_ALIGN (t));
831 if (code == LABEL_DECL)
833 hstate.add_int (EH_LANDING_PAD_NR (t));
834 hstate.add_int (LABEL_DECL_UID (t));
836 else if (code == FIELD_DECL)
838 hstate.add_flag (DECL_PACKED (t));
839 hstate.add_flag (DECL_NONADDRESSABLE_P (t));
840 hstate.add_int (DECL_OFFSET_ALIGN (t));
842 else if (code == VAR_DECL)
844 hstate.add_flag (DECL_HAS_DEBUG_EXPR_P (t));
845 hstate.add_flag (DECL_NONLOCAL_FRAME (t));
847 if (code == RESULT_DECL
848 || code == PARM_DECL
849 || code == VAR_DECL)
851 hstate.add_flag (DECL_BY_REFERENCE (t));
852 if (code == VAR_DECL
853 || code == PARM_DECL)
854 hstate.add_flag (DECL_HAS_VALUE_EXPR_P (t));
856 hstate.commit_flag ();
859 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WRTL))
860 hstate.add_int (DECL_REGISTER (t));
862 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
864 hstate.add_flag (DECL_COMMON (t));
865 hstate.add_flag (DECL_DLLIMPORT_P (t));
866 hstate.add_flag (DECL_WEAK (t));
867 hstate.add_flag (DECL_SEEN_IN_BIND_EXPR_P (t));
868 hstate.add_flag (DECL_COMDAT (t));
869 hstate.add_flag (DECL_VISIBILITY_SPECIFIED (t));
870 hstate.add_int (DECL_VISIBILITY (t));
871 if (code == VAR_DECL)
873 /* DECL_IN_TEXT_SECTION is set during final asm output only. */
874 hstate.add_flag (DECL_HARD_REGISTER (t));
875 hstate.add_flag (DECL_IN_CONSTANT_POOL (t));
877 if (TREE_CODE (t) == FUNCTION_DECL)
879 hstate.add_flag (DECL_FINAL_P (t));
880 hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (t));
881 hstate.add_flag (DECL_CXX_DESTRUCTOR_P (t));
883 hstate.commit_flag ();
886 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
888 hstate.add_int (DECL_BUILT_IN_CLASS (t));
889 hstate.add_flag (DECL_STATIC_CONSTRUCTOR (t));
890 hstate.add_flag (DECL_STATIC_DESTRUCTOR (t));
891 hstate.add_flag (DECL_UNINLINABLE (t));
892 hstate.add_flag (DECL_POSSIBLY_INLINED (t));
893 hstate.add_flag (DECL_IS_NOVOPS (t));
894 hstate.add_flag (DECL_IS_RETURNS_TWICE (t));
895 hstate.add_flag (DECL_IS_MALLOC (t));
896 hstate.add_flag (DECL_IS_OPERATOR_NEW (t));
897 hstate.add_flag (DECL_DECLARED_INLINE_P (t));
898 hstate.add_flag (DECL_STATIC_CHAIN (t));
899 hstate.add_flag (DECL_NO_INLINE_WARNING_P (t));
900 hstate.add_flag (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (t));
901 hstate.add_flag (DECL_NO_LIMIT_STACK (t));
902 hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (t));
903 hstate.add_flag (DECL_PURE_P (t));
904 hstate.add_flag (DECL_LOOPING_CONST_OR_PURE_P (t));
905 hstate.commit_flag ();
906 if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
907 hstate.add_int (DECL_FUNCTION_CODE (t));
910 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
912 hstate.add_wide_int (TYPE_MODE (t));
913 hstate.add_flag (TYPE_STRING_FLAG (t));
914 hstate.add_flag (TYPE_NO_FORCE_BLK (t));
915 hstate.add_flag (TYPE_NEEDS_CONSTRUCTING (t));
916 hstate.add_flag (TYPE_PACKED (t));
917 hstate.add_flag (TYPE_RESTRICT (t));
918 hstate.add_flag (TYPE_USER_ALIGN (t));
919 hstate.add_flag (TYPE_READONLY (t));
920 if (RECORD_OR_UNION_TYPE_P (t))
922 hstate.add_flag (TYPE_TRANSPARENT_AGGR (t));
923 hstate.add_flag (TYPE_FINAL_P (t));
925 else if (code == ARRAY_TYPE)
926 hstate.add_flag (TYPE_NONALIASED_COMPONENT (t));
927 hstate.commit_flag ();
928 hstate.add_int (TYPE_PRECISION (t));
929 hstate.add_int (TYPE_ALIGN (t));
930 hstate.add_int ((TYPE_ALIAS_SET (t) == 0
931 || (!in_lto_p
932 && get_alias_set (t) == 0))
933 ? 0 : -1);
936 if (CODE_CONTAINS_STRUCT (code, TS_TRANSLATION_UNIT_DECL))
937 hstate.add (TRANSLATION_UNIT_LANGUAGE (t),
938 strlen (TRANSLATION_UNIT_LANGUAGE (t)));
940 if (CODE_CONTAINS_STRUCT (code, TS_TARGET_OPTION))
941 gcc_unreachable ();
943 if (CODE_CONTAINS_STRUCT (code, TS_OPTIMIZATION))
944 hstate.add (t, sizeof (struct cl_optimization));
946 if (CODE_CONTAINS_STRUCT (code, TS_IDENTIFIER))
947 hstate.merge_hash (IDENTIFIER_HASH_VALUE (t));
949 if (CODE_CONTAINS_STRUCT (code, TS_STRING))
950 hstate.add (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
952 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
954 if (code != IDENTIFIER_NODE)
955 visit (TREE_TYPE (t));
958 if (CODE_CONTAINS_STRUCT (code, TS_VECTOR))
959 for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
960 visit (VECTOR_CST_ELT (t, i));
962 if (CODE_CONTAINS_STRUCT (code, TS_COMPLEX))
964 visit (TREE_REALPART (t));
965 visit (TREE_IMAGPART (t));
968 if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
970 /* Drop names that were created for anonymous entities. */
971 if (DECL_NAME (t)
972 && TREE_CODE (DECL_NAME (t)) == IDENTIFIER_NODE
973 && ANON_AGGRNAME_P (DECL_NAME (t)))
975 else
976 visit (DECL_NAME (t));
977 if (DECL_FILE_SCOPE_P (t))
979 else
980 visit (DECL_CONTEXT (t));
983 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
985 visit (DECL_SIZE (t));
986 visit (DECL_SIZE_UNIT (t));
987 visit (DECL_ATTRIBUTES (t));
988 if ((code == VAR_DECL
989 || code == PARM_DECL)
990 && DECL_HAS_VALUE_EXPR_P (t))
991 visit (DECL_VALUE_EXPR (t));
992 if (code == VAR_DECL
993 && DECL_HAS_DEBUG_EXPR_P (t))
994 visit (DECL_DEBUG_EXPR (t));
995 /* ??? Hash DECL_INITIAL as streamed. Needs the output-block to
996 be able to call get_symbol_initial_value. */
999 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
1001 if (code == TYPE_DECL)
1002 visit (DECL_ORIGINAL_TYPE (t));
1005 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
1007 if (DECL_ASSEMBLER_NAME_SET_P (t))
1008 visit (DECL_ASSEMBLER_NAME (t));
1011 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
1013 visit (DECL_FIELD_OFFSET (t));
1014 visit (DECL_BIT_FIELD_TYPE (t));
1015 visit (DECL_BIT_FIELD_REPRESENTATIVE (t));
1016 visit (DECL_FIELD_BIT_OFFSET (t));
1017 visit (DECL_FCONTEXT (t));
1020 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
1022 visit (DECL_VINDEX (t));
1023 visit (DECL_FUNCTION_PERSONALITY (t));
1024 /* Do not follow DECL_FUNCTION_SPECIFIC_TARGET. */
1025 visit (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (t));
1028 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
1030 visit (TYPE_SIZE (t));
1031 visit (TYPE_SIZE_UNIT (t));
1032 visit (TYPE_ATTRIBUTES (t));
1033 visit (TYPE_NAME (t));
1034 visit (TYPE_MAIN_VARIANT (t));
1035 if (TYPE_FILE_SCOPE_P (t))
1037 else
1038 visit (TYPE_CONTEXT (t));
1039 visit (TYPE_STUB_DECL (t));
1042 if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
1044 if (code == ENUMERAL_TYPE)
1045 visit (TYPE_VALUES (t));
1046 else if (code == ARRAY_TYPE)
1047 visit (TYPE_DOMAIN (t));
1048 else if (RECORD_OR_UNION_TYPE_P (t))
1049 for (tree f = TYPE_FIELDS (t); f; f = TREE_CHAIN (f))
1050 visit (f);
1051 else if (code == FUNCTION_TYPE
1052 || code == METHOD_TYPE)
1053 visit (TYPE_ARG_TYPES (t));
1054 if (!POINTER_TYPE_P (t))
1055 visit (TYPE_MINVAL (t));
1056 visit (TYPE_MAXVAL (t));
1057 if (RECORD_OR_UNION_TYPE_P (t))
1058 visit (TYPE_BINFO (t));
1061 if (CODE_CONTAINS_STRUCT (code, TS_LIST))
1063 visit (TREE_PURPOSE (t));
1064 visit (TREE_VALUE (t));
1065 visit (TREE_CHAIN (t));
1068 if (CODE_CONTAINS_STRUCT (code, TS_VEC))
1069 for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
1070 visit (TREE_VEC_ELT (t, i));
1072 if (CODE_CONTAINS_STRUCT (code, TS_EXP))
1074 hstate.add_wide_int (TREE_OPERAND_LENGTH (t));
1075 for (int i = 0; i < TREE_OPERAND_LENGTH (t); ++i)
1076 visit (TREE_OPERAND (t, i));
1079 if (CODE_CONTAINS_STRUCT (code, TS_BINFO))
1081 unsigned i;
1082 tree b;
1083 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (t), i, b)
1084 visit (b);
1085 visit (BINFO_OFFSET (t));
1086 visit (BINFO_VTABLE (t));
1087 visit (BINFO_VPTR_FIELD (t));
1088 FOR_EACH_VEC_SAFE_ELT (BINFO_BASE_ACCESSES (t), i, b)
1089 visit (b);
1090 /* Do not walk BINFO_INHERITANCE_CHAIN, BINFO_SUBVTT_INDEX
1091 and BINFO_VPTR_INDEX; these are used by C++ FE only. */
1094 if (CODE_CONTAINS_STRUCT (code, TS_CONSTRUCTOR))
1096 unsigned i;
1097 tree index, value;
1098 hstate.add_wide_int (CONSTRUCTOR_NELTS (t));
1099 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
1101 visit (index);
1102 visit (value);
1106 if (code == OMP_CLAUSE)
1108 int i;
1109 HOST_WIDE_INT val;
1111 hstate.add_wide_int (OMP_CLAUSE_CODE (t));
1112 switch (OMP_CLAUSE_CODE (t))
1114 case OMP_CLAUSE_DEFAULT:
1115 val = OMP_CLAUSE_DEFAULT_KIND (t);
1116 break;
1117 case OMP_CLAUSE_SCHEDULE:
1118 val = OMP_CLAUSE_SCHEDULE_KIND (t);
1119 break;
1120 case OMP_CLAUSE_DEPEND:
1121 val = OMP_CLAUSE_DEPEND_KIND (t);
1122 break;
1123 case OMP_CLAUSE_MAP:
1124 val = OMP_CLAUSE_MAP_KIND (t);
1125 break;
1126 case OMP_CLAUSE_PROC_BIND:
1127 val = OMP_CLAUSE_PROC_BIND_KIND (t);
1128 break;
1129 case OMP_CLAUSE_REDUCTION:
1130 val = OMP_CLAUSE_REDUCTION_CODE (t);
1131 break;
1132 default:
1133 val = 0;
1134 break;
1136 hstate.add_wide_int (val);
1137 for (i = 0; i < omp_clause_num_ops[OMP_CLAUSE_CODE (t)]; i++)
1138 visit (OMP_CLAUSE_OPERAND (t, i));
1139 visit (OMP_CLAUSE_CHAIN (t));
1142 return hstate.end ();
1144 #undef visit
1147 /* Compare two SCC entries by their hash value for qsorting them. */
1150 DFS::scc_entry_compare (const void *p1_, const void *p2_)
1152 const scc_entry *p1 = (const scc_entry *) p1_;
1153 const scc_entry *p2 = (const scc_entry *) p2_;
1154 if (p1->hash < p2->hash)
1155 return -1;
1156 else if (p1->hash > p2->hash)
1157 return 1;
1158 return 0;
1161 /* Return a hash value for the SCC on the SCC stack from FIRST with
1162 size SIZE. */
1164 hashval_t
1165 DFS::hash_scc (struct output_block *ob,
1166 unsigned first, unsigned size)
1168 unsigned int last_classes = 0, iterations = 0;
1170 /* Compute hash values for the SCC members. */
1171 for (unsigned i = 0; i < size; ++i)
1172 sccstack[first+i].hash = hash_tree (ob->writer_cache, NULL,
1173 sccstack[first+i].t);
1175 if (size == 1)
1176 return sccstack[first].hash;
1178 /* We aim to get unique hash for every tree within SCC and compute hash value
1179 of the whole SCC by combing all values together in an stable (entry point
1180 independent) order. This guarantees that the same SCC regions within
1181 different translation units will get the same hash values and therefore
1182 will be merged at WPA time.
1184 Often the hashes are already unique. In that case we compute scc hash
1185 by combining individual hash values in an increasing order.
1187 If thre are duplicates we seek at least one tree with unique hash (and
1188 pick one with minimal hash and this property). Then we obtain stable
1189 order by DFS walk starting from this unique tree and then use index
1190 within this order to make individual hash values unique.
1192 If there is no tree with unique hash, we iteratively propagate the hash
1193 values across the internal edges of SCC. This usually quickly leads
1194 to unique hashes. Consider, for example, an SCC containing two pointers
1195 that are identical except for type they point and assume that these
1196 types are also part of the SCC.
1197 The propagation will add the points-to type information into their hash
1198 values. */
1201 /* Sort the SCC so we can easily see check for uniqueness. */
1202 qsort (&sccstack[first], size, sizeof (scc_entry), scc_entry_compare);
1204 unsigned int classes = 1;
1205 int firstunique = -1;
1207 /* Find tree with lowest unique hash (if it exists) and compute
1208 number of equivalence classes. */
1209 if (sccstack[first].hash != sccstack[first+1].hash)
1210 firstunique = 0;
1211 for (unsigned i = 1; i < size; ++i)
1212 if (sccstack[first+i-1].hash != sccstack[first+i].hash)
1214 classes++;
1215 if (firstunique == -1
1216 && (i == size - 1
1217 || sccstack[first+i+1].hash != sccstack[first+i].hash))
1218 firstunique = i;
1221 /* If we found tree with unique hash; stop the iteration. */
1222 if (firstunique != -1
1223 /* Also terminate if we run out of iterations or if the number of
1224 equivalence classes is no longer increasing.
1225 For example a cyclic list of trees that are all equivalent will
1226 never have unique entry point; we however do not build such SCCs
1227 in our IL. */
1228 || classes <= last_classes || iterations > 16)
1230 hashval_t scc_hash;
1232 /* If some hashes are not unique (CLASSES != SIZE), use the DFS walk
1233 starting from FIRSTUNIQUE to obstain stable order. */
1234 if (classes != size && firstunique != -1)
1236 hash_map <tree, hashval_t> map(size*2);
1238 /* Store hash values into a map, so we can associate them with
1239 reordered SCC. */
1240 for (unsigned i = 0; i < size; ++i)
1241 map.put (sccstack[first+i].t, sccstack[first+i].hash);
1243 DFS again (ob, sccstack[first+firstunique].t, false, false, true);
1244 gcc_assert (again.sccstack.length () == size);
1246 memcpy (sccstack.address () + first,
1247 again.sccstack.address (),
1248 sizeof (scc_entry) * size);
1250 /* Update hash values of individual members by hashing in the
1251 index within the stable order. This ensures uniqueness.
1252 Also compute the scc_hash by mixing in all hash values in the
1253 stable order we obtained. */
1254 sccstack[first].hash = *map.get (sccstack[first].t);
1255 scc_hash = sccstack[first].hash;
1256 for (unsigned i = 1; i < size; ++i)
1258 sccstack[first+i].hash
1259 = iterative_hash_hashval_t (i,
1260 *map.get (sccstack[first+i].t));
1261 scc_hash = iterative_hash_hashval_t (scc_hash,
1262 sccstack[first+i].hash);
1265 /* If we got unique hash values for each tree, then sort already
1266 ensured entry point independent order. Only compute the final
1267 scc hash.
1269 If we failed to find the unique entry point, we go by the same
1270 route. We will eventually introduce unwanted hash conflicts. */
1271 else
1273 scc_hash = sccstack[first].hash;
1274 for (unsigned i = 1; i < size; ++i)
1275 scc_hash = iterative_hash_hashval_t (scc_hash,
1276 sccstack[first+i].hash);
1277 /* We can not 100% guarantee that the hash will not conflict in
1278 in a way so the unique hash is not found. This however
1279 should be extremely rare situation. ICE for now so possible
1280 issues are found and evaulated. */
1281 gcc_checking_assert (classes == size);
1284 /* To avoid conflicts across SCCs iteratively hash the whole SCC
1285 hash into the hash of each of the elements. */
1286 for (unsigned i = 0; i < size; ++i)
1287 sccstack[first+i].hash
1288 = iterative_hash_hashval_t (sccstack[first+i].hash, scc_hash);
1289 return scc_hash;
1292 last_classes = classes;
1293 iterations++;
1295 /* We failed to identify the entry point; propagate hash values across
1296 the edges. */
1298 hash_map <tree, hashval_t> map(size*2);
1299 for (unsigned i = 0; i < size; ++i)
1300 map.put (sccstack[first+i].t, sccstack[first+i].hash);
1302 for (unsigned i = 0; i < size; i++)
1303 sccstack[first+i].hash = hash_tree (ob->writer_cache, &map,
1304 sccstack[first+i].t);
1307 while (true);
1310 /* DFS walk EXPR and stream SCCs of tree bodies if they are not
1311 already in the streamer cache. Main routine called for
1312 each visit of EXPR. */
1314 void
1315 DFS::DFS_write_tree (struct output_block *ob, sccs *from_state,
1316 tree expr, bool ref_p, bool this_ref_p, bool single_p)
1318 unsigned ix;
1320 /* Handle special cases. */
1321 if (expr == NULL_TREE)
1322 return;
1324 /* Do not DFS walk into indexable trees. */
1325 if (this_ref_p && tree_is_indexable (expr))
1326 return;
1328 /* Check if we already streamed EXPR. */
1329 if (streamer_tree_cache_lookup (ob->writer_cache, expr, &ix))
1330 return;
1332 sccs **slot = &sccstate.get_or_insert (expr);
1333 sccs *cstate = *slot;
1334 if (!cstate)
1336 scc_entry e = { expr, 0 };
1337 /* Not yet visited. DFS recurse and push it onto the stack. */
1338 *slot = cstate = XOBNEW (&sccstate_obstack, struct sccs);
1339 sccstack.safe_push (e);
1340 cstate->dfsnum = next_dfs_num++;
1341 cstate->low = cstate->dfsnum;
1343 if (streamer_handle_as_builtin_p (expr))
1345 else if (TREE_CODE (expr) == INTEGER_CST
1346 && !TREE_OVERFLOW (expr))
1347 DFS_write_tree (ob, cstate, TREE_TYPE (expr), ref_p, ref_p, single_p);
1348 else
1350 DFS_write_tree_body (ob, expr, cstate, ref_p, single_p);
1352 /* Walk any LTO-specific edges. */
1353 if (DECL_P (expr)
1354 && TREE_CODE (expr) != FUNCTION_DECL
1355 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
1357 /* Handle DECL_INITIAL for symbols. */
1358 tree initial = get_symbol_initial_value (ob->decl_state->symtab_node_encoder,
1359 expr);
1360 DFS_write_tree (ob, cstate, initial, ref_p, ref_p, single_p);
1364 /* See if we found an SCC. */
1365 if (cstate->low == cstate->dfsnum)
1367 unsigned first, size;
1368 tree x;
1370 /* If we are re-walking a single leaf-SCC just return and
1371 let the caller access the sccstack. */
1372 if (single_p)
1373 return;
1375 /* Pop the SCC and compute its size. */
1376 first = sccstack.length ();
1379 x = sccstack[--first].t;
1381 while (x != expr);
1382 size = sccstack.length () - first;
1384 /* No need to compute hashes for LTRANS units, we don't perform
1385 any merging there. */
1386 hashval_t scc_hash = 0;
1387 unsigned scc_entry_len = 0;
1388 if (!flag_wpa)
1390 scc_hash = hash_scc (ob, first, size);
1392 /* Put the entries with the least number of collisions first. */
1393 unsigned entry_start = 0;
1394 scc_entry_len = size + 1;
1395 for (unsigned i = 0; i < size;)
1397 unsigned from = i;
1398 for (i = i + 1; i < size
1399 && (sccstack[first + i].hash
1400 == sccstack[first + from].hash); ++i)
1402 if (i - from < scc_entry_len)
1404 scc_entry_len = i - from;
1405 entry_start = from;
1408 for (unsigned i = 0; i < scc_entry_len; ++i)
1410 scc_entry tem = sccstack[first + i];
1411 sccstack[first + i] = sccstack[first + entry_start + i];
1412 sccstack[first + entry_start + i] = tem;
1415 if (scc_entry_len == 1)
1416 ; /* We already sorted SCC deterministically in hash_scc. */
1417 else
1418 /* Check that we have only one SCC.
1419 Naturally we may have conflicts if hash function is not
1420 strong enough. Lets see how far this gets. */
1422 #ifdef ENABLE_CHECKING
1423 gcc_unreachable ();
1424 #endif
1428 /* Write LTO_tree_scc. */
1429 streamer_write_record_start (ob, LTO_tree_scc);
1430 streamer_write_uhwi (ob, size);
1431 streamer_write_uhwi (ob, scc_hash);
1433 /* Write size-1 SCCs without wrapping them inside SCC bundles.
1434 All INTEGER_CSTs need to be handled this way as we need
1435 their type to materialize them. Also builtins are handled
1436 this way.
1437 ??? We still wrap these in LTO_tree_scc so at the
1438 input side we can properly identify the tree we want
1439 to ultimatively return. */
1440 if (size == 1)
1441 lto_output_tree_1 (ob, expr, scc_hash, ref_p, this_ref_p);
1442 else
1444 /* Write the size of the SCC entry candidates. */
1445 streamer_write_uhwi (ob, scc_entry_len);
1447 /* Write all headers and populate the streamer cache. */
1448 for (unsigned i = 0; i < size; ++i)
1450 hashval_t hash = sccstack[first+i].hash;
1451 tree t = sccstack[first+i].t;
1452 bool exists_p = streamer_tree_cache_insert (ob->writer_cache,
1453 t, hash, &ix);
1454 gcc_assert (!exists_p);
1456 if (!lto_is_streamable (t))
1457 internal_error ("tree code %qs is not supported "
1458 "in LTO streams",
1459 get_tree_code_name (TREE_CODE (t)));
1461 gcc_checking_assert (!streamer_handle_as_builtin_p (t));
1463 /* Write the header, containing everything needed to
1464 materialize EXPR on the reading side. */
1465 streamer_write_tree_header (ob, t);
1468 /* Write the bitpacks and tree references. */
1469 for (unsigned i = 0; i < size; ++i)
1471 lto_write_tree_1 (ob, sccstack[first+i].t, ref_p);
1473 /* Mark the end of the tree. */
1474 streamer_write_zero (ob);
1478 /* Finally truncate the vector. */
1479 sccstack.truncate (first);
1481 if (from_state)
1482 from_state->low = MIN (from_state->low, cstate->low);
1483 return;
1486 if (from_state)
1487 from_state->low = MIN (from_state->low, cstate->low);
1489 gcc_checking_assert (from_state);
1490 if (cstate->dfsnum < from_state->dfsnum)
1491 from_state->low = MIN (cstate->dfsnum, from_state->low);
1495 /* Emit the physical representation of tree node EXPR to output block
1496 OB. If THIS_REF_P is true, the leaves of EXPR are emitted as references
1497 via lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
1499 void
1500 lto_output_tree (struct output_block *ob, tree expr,
1501 bool ref_p, bool this_ref_p)
1503 unsigned ix;
1504 bool existed_p;
1506 if (expr == NULL_TREE)
1508 streamer_write_record_start (ob, LTO_null);
1509 return;
1512 if (this_ref_p && tree_is_indexable (expr))
1514 lto_output_tree_ref (ob, expr);
1515 return;
1518 existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1519 if (existed_p)
1521 /* If a node has already been streamed out, make sure that
1522 we don't write it more than once. Otherwise, the reader
1523 will instantiate two different nodes for the same object. */
1524 streamer_write_record_start (ob, LTO_tree_pickle_reference);
1525 streamer_write_uhwi (ob, ix);
1526 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1527 lto_tree_code_to_tag (TREE_CODE (expr)));
1528 lto_stats.num_pickle_refs_output++;
1530 else
1532 /* This is the first time we see EXPR, write all reachable
1533 trees to OB. */
1534 static bool in_dfs_walk;
1536 /* Protect against recursion which means disconnect between
1537 what tree edges we walk in the DFS walk and what edges
1538 we stream out. */
1539 gcc_assert (!in_dfs_walk);
1541 /* Start the DFS walk. */
1542 /* Save ob state ... */
1543 /* let's see ... */
1544 in_dfs_walk = true;
1545 DFS (ob, expr, ref_p, this_ref_p, false);
1546 in_dfs_walk = false;
1548 /* Finally append a reference to the tree we were writing.
1549 ??? If expr ended up as a singleton we could have
1550 inlined it here and avoid outputting a reference. */
1551 existed_p = streamer_tree_cache_lookup (ob->writer_cache, expr, &ix);
1552 gcc_assert (existed_p);
1553 streamer_write_record_start (ob, LTO_tree_pickle_reference);
1554 streamer_write_uhwi (ob, ix);
1555 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
1556 lto_tree_code_to_tag (TREE_CODE (expr)));
1557 lto_stats.num_pickle_refs_output++;
1562 /* Output to OB a list of try/catch handlers starting with FIRST. */
1564 static void
1565 output_eh_try_list (struct output_block *ob, eh_catch first)
1567 eh_catch n;
1569 for (n = first; n; n = n->next_catch)
1571 streamer_write_record_start (ob, LTO_eh_catch);
1572 stream_write_tree (ob, n->type_list, true);
1573 stream_write_tree (ob, n->filter_list, true);
1574 stream_write_tree (ob, n->label, true);
1577 streamer_write_record_start (ob, LTO_null);
1581 /* Output EH region R in function FN to OB. CURR_RN is the slot index
1582 that is being emitted in FN->EH->REGION_ARRAY. This is used to
1583 detect EH region sharing. */
1585 static void
1586 output_eh_region (struct output_block *ob, eh_region r)
1588 enum LTO_tags tag;
1590 if (r == NULL)
1592 streamer_write_record_start (ob, LTO_null);
1593 return;
1596 if (r->type == ERT_CLEANUP)
1597 tag = LTO_ert_cleanup;
1598 else if (r->type == ERT_TRY)
1599 tag = LTO_ert_try;
1600 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1601 tag = LTO_ert_allowed_exceptions;
1602 else if (r->type == ERT_MUST_NOT_THROW)
1603 tag = LTO_ert_must_not_throw;
1604 else
1605 gcc_unreachable ();
1607 streamer_write_record_start (ob, tag);
1608 streamer_write_hwi (ob, r->index);
1610 if (r->outer)
1611 streamer_write_hwi (ob, r->outer->index);
1612 else
1613 streamer_write_zero (ob);
1615 if (r->inner)
1616 streamer_write_hwi (ob, r->inner->index);
1617 else
1618 streamer_write_zero (ob);
1620 if (r->next_peer)
1621 streamer_write_hwi (ob, r->next_peer->index);
1622 else
1623 streamer_write_zero (ob);
1625 if (r->type == ERT_TRY)
1627 output_eh_try_list (ob, r->u.eh_try.first_catch);
1629 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
1631 stream_write_tree (ob, r->u.allowed.type_list, true);
1632 stream_write_tree (ob, r->u.allowed.label, true);
1633 streamer_write_uhwi (ob, r->u.allowed.filter);
1635 else if (r->type == ERT_MUST_NOT_THROW)
1637 stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
1638 bitpack_d bp = bitpack_create (ob->main_stream);
1639 stream_output_location (ob, &bp, r->u.must_not_throw.failure_loc);
1640 streamer_write_bitpack (&bp);
1643 if (r->landing_pads)
1644 streamer_write_hwi (ob, r->landing_pads->index);
1645 else
1646 streamer_write_zero (ob);
1650 /* Output landing pad LP to OB. */
1652 static void
1653 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
1655 if (lp == NULL)
1657 streamer_write_record_start (ob, LTO_null);
1658 return;
1661 streamer_write_record_start (ob, LTO_eh_landing_pad);
1662 streamer_write_hwi (ob, lp->index);
1663 if (lp->next_lp)
1664 streamer_write_hwi (ob, lp->next_lp->index);
1665 else
1666 streamer_write_zero (ob);
1668 if (lp->region)
1669 streamer_write_hwi (ob, lp->region->index);
1670 else
1671 streamer_write_zero (ob);
1673 stream_write_tree (ob, lp->post_landing_pad, true);
1677 /* Output the existing eh_table to OB. */
1679 static void
1680 output_eh_regions (struct output_block *ob, struct function *fn)
1682 if (fn->eh && fn->eh->region_tree)
1684 unsigned i;
1685 eh_region eh;
1686 eh_landing_pad lp;
1687 tree ttype;
1689 streamer_write_record_start (ob, LTO_eh_table);
1691 /* Emit the index of the root of the EH region tree. */
1692 streamer_write_hwi (ob, fn->eh->region_tree->index);
1694 /* Emit all the EH regions in the region array. */
1695 streamer_write_hwi (ob, vec_safe_length (fn->eh->region_array));
1696 FOR_EACH_VEC_SAFE_ELT (fn->eh->region_array, i, eh)
1697 output_eh_region (ob, eh);
1699 /* Emit all landing pads. */
1700 streamer_write_hwi (ob, vec_safe_length (fn->eh->lp_array));
1701 FOR_EACH_VEC_SAFE_ELT (fn->eh->lp_array, i, lp)
1702 output_eh_lp (ob, lp);
1704 /* Emit all the runtime type data. */
1705 streamer_write_hwi (ob, vec_safe_length (fn->eh->ttype_data));
1706 FOR_EACH_VEC_SAFE_ELT (fn->eh->ttype_data, i, ttype)
1707 stream_write_tree (ob, ttype, true);
1709 /* Emit the table of action chains. */
1710 if (targetm.arm_eabi_unwinder)
1712 tree t;
1713 streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.arm_eabi));
1714 FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.arm_eabi, i, t)
1715 stream_write_tree (ob, t, true);
1717 else
1719 uchar c;
1720 streamer_write_hwi (ob, vec_safe_length (fn->eh->ehspec_data.other));
1721 FOR_EACH_VEC_SAFE_ELT (fn->eh->ehspec_data.other, i, c)
1722 streamer_write_char_stream (ob->main_stream, c);
1726 /* The LTO_null either terminates the record or indicates that there
1727 are no eh_records at all. */
1728 streamer_write_record_start (ob, LTO_null);
1732 /* Output all of the active ssa names to the ssa_names stream. */
1734 static void
1735 output_ssa_names (struct output_block *ob, struct function *fn)
1737 unsigned int i, len;
1739 len = vec_safe_length (SSANAMES (fn));
1740 streamer_write_uhwi (ob, len);
1742 for (i = 1; i < len; i++)
1744 tree ptr = (*SSANAMES (fn))[i];
1746 if (ptr == NULL_TREE
1747 || SSA_NAME_IN_FREE_LIST (ptr)
1748 || virtual_operand_p (ptr))
1749 continue;
1751 streamer_write_uhwi (ob, i);
1752 streamer_write_char_stream (ob->main_stream,
1753 SSA_NAME_IS_DEFAULT_DEF (ptr));
1754 if (SSA_NAME_VAR (ptr))
1755 stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
1756 else
1757 /* ??? This drops SSA_NAME_IDENTIFIER on the floor. */
1758 stream_write_tree (ob, TREE_TYPE (ptr), true);
1761 streamer_write_zero (ob);
1765 /* Output a wide-int. */
1767 static void
1768 streamer_write_wi (struct output_block *ob,
1769 const widest_int &w)
1771 int len = w.get_len ();
1773 streamer_write_uhwi (ob, w.get_precision ());
1774 streamer_write_uhwi (ob, len);
1775 for (int i = 0; i < len; i++)
1776 streamer_write_hwi (ob, w.elt (i));
1780 /* Output the cfg. */
1782 static void
1783 output_cfg (struct output_block *ob, struct function *fn)
1785 struct lto_output_stream *tmp_stream = ob->main_stream;
1786 basic_block bb;
1788 ob->main_stream = ob->cfg_stream;
1790 streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
1791 profile_status_for_fn (fn));
1793 /* Output the number of the highest basic block. */
1794 streamer_write_uhwi (ob, last_basic_block_for_fn (fn));
1796 FOR_ALL_BB_FN (bb, fn)
1798 edge_iterator ei;
1799 edge e;
1801 streamer_write_hwi (ob, bb->index);
1803 /* Output the successors and the edge flags. */
1804 streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
1805 FOR_EACH_EDGE (e, ei, bb->succs)
1807 streamer_write_uhwi (ob, e->dest->index);
1808 streamer_write_hwi (ob, e->probability);
1809 streamer_write_gcov_count (ob, e->count);
1810 streamer_write_uhwi (ob, e->flags);
1814 streamer_write_hwi (ob, -1);
1816 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1817 while (bb->next_bb)
1819 streamer_write_hwi (ob, bb->next_bb->index);
1820 bb = bb->next_bb;
1823 streamer_write_hwi (ob, -1);
1825 /* ??? The cfgloop interface is tied to cfun. */
1826 gcc_assert (cfun == fn);
1828 /* Output the number of loops. */
1829 streamer_write_uhwi (ob, number_of_loops (fn));
1831 /* Output each loop, skipping the tree root which has number zero. */
1832 for (unsigned i = 1; i < number_of_loops (fn); ++i)
1834 struct loop *loop = get_loop (fn, i);
1836 /* Write the index of the loop header. That's enough to rebuild
1837 the loop tree on the reader side. Stream -1 for an unused
1838 loop entry. */
1839 if (!loop)
1841 streamer_write_hwi (ob, -1);
1842 continue;
1844 else
1845 streamer_write_hwi (ob, loop->header->index);
1847 /* Write everything copy_loop_info copies. */
1848 streamer_write_enum (ob->main_stream,
1849 loop_estimation, EST_LAST, loop->estimate_state);
1850 streamer_write_hwi (ob, loop->any_upper_bound);
1851 if (loop->any_upper_bound)
1852 streamer_write_wi (ob, loop->nb_iterations_upper_bound);
1853 streamer_write_hwi (ob, loop->any_estimate);
1854 if (loop->any_estimate)
1855 streamer_write_wi (ob, loop->nb_iterations_estimate);
1857 /* Write OMP SIMD related info. */
1858 streamer_write_hwi (ob, loop->safelen);
1859 streamer_write_hwi (ob, loop->dont_vectorize);
1860 streamer_write_hwi (ob, loop->force_vectorize);
1861 stream_write_tree (ob, loop->simduid, true);
1864 ob->main_stream = tmp_stream;
1868 /* Create the header in the file using OB. If the section type is for
1869 a function, set FN to the decl for that function. */
1871 void
1872 produce_asm (struct output_block *ob, tree fn)
1874 enum lto_section_type section_type = ob->section_type;
1875 struct lto_function_header header;
1876 char *section_name;
1878 if (section_type == LTO_section_function_body)
1880 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
1881 section_name = lto_get_section_name (section_type, name, NULL);
1883 else
1884 section_name = lto_get_section_name (section_type, NULL, NULL);
1886 lto_begin_section (section_name, !flag_wpa);
1887 free (section_name);
1889 /* The entire header is stream computed here. */
1890 memset (&header, 0, sizeof (struct lto_function_header));
1892 /* Write the header. */
1893 header.major_version = LTO_major_version;
1894 header.minor_version = LTO_minor_version;
1896 if (section_type == LTO_section_function_body)
1897 header.cfg_size = ob->cfg_stream->total_size;
1898 header.main_size = ob->main_stream->total_size;
1899 header.string_size = ob->string_stream->total_size;
1900 lto_write_data (&header, sizeof header);
1902 /* Put all of the gimple and the string table out the asm file as a
1903 block of text. */
1904 if (section_type == LTO_section_function_body)
1905 lto_write_stream (ob->cfg_stream);
1906 lto_write_stream (ob->main_stream);
1907 lto_write_stream (ob->string_stream);
1909 lto_end_section ();
1913 /* Output the base body of struct function FN using output block OB. */
1915 static void
1916 output_struct_function_base (struct output_block *ob, struct function *fn)
1918 struct bitpack_d bp;
1919 unsigned i;
1920 tree t;
1922 /* Output the static chain and non-local goto save area. */
1923 stream_write_tree (ob, fn->static_chain_decl, true);
1924 stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
1926 /* Output all the local variables in the function. */
1927 streamer_write_hwi (ob, vec_safe_length (fn->local_decls));
1928 FOR_EACH_VEC_SAFE_ELT (fn->local_decls, i, t)
1929 stream_write_tree (ob, t, true);
1931 /* Output current IL state of the function. */
1932 streamer_write_uhwi (ob, fn->curr_properties);
1934 /* Write all the attributes for FN. */
1935 bp = bitpack_create (ob->main_stream);
1936 bp_pack_value (&bp, fn->is_thunk, 1);
1937 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
1938 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
1939 bp_pack_value (&bp, fn->returns_struct, 1);
1940 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
1941 bp_pack_value (&bp, fn->can_delete_dead_exceptions, 1);
1942 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
1943 bp_pack_value (&bp, fn->after_inlining, 1);
1944 bp_pack_value (&bp, fn->stdarg, 1);
1945 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
1946 bp_pack_value (&bp, fn->calls_alloca, 1);
1947 bp_pack_value (&bp, fn->calls_setjmp, 1);
1948 bp_pack_value (&bp, fn->has_force_vectorize_loops, 1);
1949 bp_pack_value (&bp, fn->has_simduid_loops, 1);
1950 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
1951 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
1953 /* Output the function start and end loci. */
1954 stream_output_location (ob, &bp, fn->function_start_locus);
1955 stream_output_location (ob, &bp, fn->function_end_locus);
1957 streamer_write_bitpack (&bp);
1961 /* Output the body of function NODE->DECL. */
1963 static void
1964 output_function (struct cgraph_node *node)
1966 tree function;
1967 struct function *fn;
1968 basic_block bb;
1969 struct output_block *ob;
1971 function = node->decl;
1972 fn = DECL_STRUCT_FUNCTION (function);
1973 ob = create_output_block (LTO_section_function_body);
1975 clear_line_info (ob);
1976 ob->symbol = node;
1978 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
1980 /* Set current_function_decl and cfun. */
1981 push_cfun (fn);
1983 /* Make string 0 be a NULL string. */
1984 streamer_write_char_stream (ob->string_stream, 0);
1986 streamer_write_record_start (ob, LTO_function);
1988 /* Output decls for parameters and args. */
1989 stream_write_tree (ob, DECL_RESULT (function), true);
1990 streamer_write_chain (ob, DECL_ARGUMENTS (function), true);
1992 /* Output DECL_INITIAL for the function, which contains the tree of
1993 lexical scopes. */
1994 stream_write_tree (ob, DECL_INITIAL (function), true);
1996 /* We also stream abstract functions where we stream only stuff needed for
1997 debug info. */
1998 if (gimple_has_body_p (function))
2000 streamer_write_uhwi (ob, 1);
2001 output_struct_function_base (ob, fn);
2003 /* Output all the SSA names used in the function. */
2004 output_ssa_names (ob, fn);
2006 /* Output any exception handling regions. */
2007 output_eh_regions (ob, fn);
2010 /* We will renumber the statements. The code that does this uses
2011 the same ordering that we use for serializing them so we can use
2012 the same code on the other end and not have to write out the
2013 statement numbers. We do not assign UIDs to PHIs here because
2014 virtual PHIs get re-computed on-the-fly which would make numbers
2015 inconsistent. */
2016 set_gimple_stmt_max_uid (cfun, 0);
2017 FOR_ALL_BB_FN (bb, cfun)
2019 gimple_stmt_iterator gsi;
2020 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2022 gimple stmt = gsi_stmt (gsi);
2024 /* Virtual PHIs are not going to be streamed. */
2025 if (!virtual_operand_p (gimple_phi_result (stmt)))
2026 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2028 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2030 gimple stmt = gsi_stmt (gsi);
2031 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2034 /* To avoid keeping duplicate gimple IDs in the statements, renumber
2035 virtual phis now. */
2036 FOR_ALL_BB_FN (bb, cfun)
2038 gimple_stmt_iterator gsi;
2039 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2041 gimple stmt = gsi_stmt (gsi);
2042 if (virtual_operand_p (gimple_phi_result (stmt)))
2043 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
2047 /* Output the code for the function. */
2048 FOR_ALL_BB_FN (bb, fn)
2049 output_bb (ob, bb, fn);
2051 /* The terminator for this function. */
2052 streamer_write_record_start (ob, LTO_null);
2054 output_cfg (ob, fn);
2056 pop_cfun ();
2058 else
2059 streamer_write_uhwi (ob, 0);
2061 /* Create a section to hold the pickled output of this function. */
2062 produce_asm (ob, function);
2064 destroy_output_block (ob);
2067 /* Output the body of function NODE->DECL. */
2069 static void
2070 output_constructor (struct varpool_node *node)
2072 tree var = node->decl;
2073 struct output_block *ob;
2075 ob = create_output_block (LTO_section_function_body);
2077 clear_line_info (ob);
2078 ob->symbol = node;
2080 /* Make string 0 be a NULL string. */
2081 streamer_write_char_stream (ob->string_stream, 0);
2083 /* Output DECL_INITIAL for the function, which contains the tree of
2084 lexical scopes. */
2085 stream_write_tree (ob, DECL_INITIAL (var), true);
2087 /* Create a section to hold the pickled output of this function. */
2088 produce_asm (ob, var);
2090 destroy_output_block (ob);
2094 /* Emit toplevel asms. */
2096 void
2097 lto_output_toplevel_asms (void)
2099 struct output_block *ob;
2100 struct asm_node *can;
2101 char *section_name;
2102 struct lto_simple_header_with_strings header;
2104 if (!symtab->first_asm_symbol ())
2105 return;
2107 ob = create_output_block (LTO_section_asm);
2109 /* Make string 0 be a NULL string. */
2110 streamer_write_char_stream (ob->string_stream, 0);
2112 for (can = symtab->first_asm_symbol (); can; can = can->next)
2114 streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
2115 streamer_write_hwi (ob, can->order);
2118 streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
2120 section_name = lto_get_section_name (LTO_section_asm, NULL, NULL);
2121 lto_begin_section (section_name, !flag_wpa);
2122 free (section_name);
2124 /* The entire header stream is computed here. */
2125 memset (&header, 0, sizeof (header));
2127 /* Write the header. */
2128 header.major_version = LTO_major_version;
2129 header.minor_version = LTO_minor_version;
2131 header.main_size = ob->main_stream->total_size;
2132 header.string_size = ob->string_stream->total_size;
2133 lto_write_data (&header, sizeof header);
2135 /* Put all of the gimple and the string table out the asm file as a
2136 block of text. */
2137 lto_write_stream (ob->main_stream);
2138 lto_write_stream (ob->string_stream);
2140 lto_end_section ();
2142 destroy_output_block (ob);
2146 /* Copy the function body or variable constructor of NODE without deserializing. */
2148 static void
2149 copy_function_or_variable (struct symtab_node *node)
2151 tree function = node->decl;
2152 struct lto_file_decl_data *file_data = node->lto_file_data;
2153 const char *data;
2154 size_t len;
2155 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
2156 char *section_name =
2157 lto_get_section_name (LTO_section_function_body, name, NULL);
2158 size_t i, j;
2159 struct lto_in_decl_state *in_state;
2160 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
2162 lto_begin_section (section_name, !flag_wpa);
2163 free (section_name);
2165 /* We may have renamed the declaration, e.g., a static function. */
2166 name = lto_get_decl_name_mapping (file_data, name);
2168 data = lto_get_section_data (file_data, LTO_section_function_body,
2169 name, &len);
2170 gcc_assert (data);
2172 /* Do a bit copy of the function body. */
2173 lto_write_data (data, len);
2175 /* Copy decls. */
2176 in_state =
2177 lto_get_function_in_decl_state (node->lto_file_data, function);
2178 gcc_assert (in_state);
2180 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2182 size_t n = in_state->streams[i].size;
2183 tree *trees = in_state->streams[i].trees;
2184 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
2186 /* The out state must have the same indices and the in state.
2187 So just copy the vector. All the encoders in the in state
2188 must be empty where we reach here. */
2189 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
2190 encoder->trees.reserve_exact (n);
2191 for (j = 0; j < n; j++)
2192 encoder->trees.safe_push (trees[j]);
2195 lto_free_section_data (file_data, LTO_section_function_body, name,
2196 data, len);
2197 lto_end_section ();
2200 /* Wrap symbol references in *TP inside a type-preserving MEM_REF. */
2202 static tree
2203 wrap_refs (tree *tp, int *ws, void *)
2205 tree t = *tp;
2206 if (handled_component_p (t)
2207 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL)
2209 tree decl = TREE_OPERAND (t, 0);
2210 tree ptrtype = build_pointer_type (TREE_TYPE (decl));
2211 TREE_OPERAND (t, 0) = build2 (MEM_REF, TREE_TYPE (decl),
2212 build1 (ADDR_EXPR, ptrtype, decl),
2213 build_int_cst (ptrtype, 0));
2214 TREE_THIS_VOLATILE (TREE_OPERAND (t, 0)) = TREE_THIS_VOLATILE (decl);
2215 *ws = 0;
2217 else if (TREE_CODE (t) == CONSTRUCTOR)
2219 else if (!EXPR_P (t))
2220 *ws = 0;
2221 return NULL_TREE;
2224 /* Main entry point from the pass manager. */
2226 void
2227 lto_output (void)
2229 struct lto_out_decl_state *decl_state;
2230 #ifdef ENABLE_CHECKING
2231 bitmap output = lto_bitmap_alloc ();
2232 #endif
2233 int i, n_nodes;
2234 lto_symtab_encoder_t encoder = lto_get_out_decl_state ()->symtab_node_encoder;
2236 /* Initialize the streamer. */
2237 lto_streamer_init ();
2239 n_nodes = lto_symtab_encoder_size (encoder);
2240 /* Process only the functions with bodies. */
2241 for (i = 0; i < n_nodes; i++)
2243 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
2244 if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
2246 if (lto_symtab_encoder_encode_body_p (encoder, node)
2247 && !node->alias)
2249 #ifdef ENABLE_CHECKING
2250 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2251 bitmap_set_bit (output, DECL_UID (node->decl));
2252 #endif
2253 decl_state = lto_new_out_decl_state ();
2254 lto_push_out_decl_state (decl_state);
2255 if (gimple_has_body_p (node->decl) || !flag_wpa
2256 /* Thunks have no body but they may be synthetized
2257 at WPA time. */
2258 || DECL_ARGUMENTS (node->decl))
2259 output_function (node);
2260 else
2261 copy_function_or_variable (node);
2262 gcc_assert (lto_get_out_decl_state () == decl_state);
2263 lto_pop_out_decl_state ();
2264 lto_record_function_out_decl_state (node->decl, decl_state);
2267 else if (varpool_node *node = dyn_cast <varpool_node *> (snode))
2269 /* Wrap symbol references inside the ctor in a type
2270 preserving MEM_REF. */
2271 tree ctor = DECL_INITIAL (node->decl);
2272 if (ctor && !in_lto_p)
2273 walk_tree (&ctor, wrap_refs, NULL, NULL);
2274 if (get_symbol_initial_value (encoder, node->decl) == error_mark_node
2275 && lto_symtab_encoder_encode_initializer_p (encoder, node)
2276 && !node->alias)
2278 timevar_push (TV_IPA_LTO_CTORS_OUT);
2279 #ifdef ENABLE_CHECKING
2280 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
2281 bitmap_set_bit (output, DECL_UID (node->decl));
2282 #endif
2283 decl_state = lto_new_out_decl_state ();
2284 lto_push_out_decl_state (decl_state);
2285 if (DECL_INITIAL (node->decl) != error_mark_node
2286 || !flag_wpa)
2287 output_constructor (node);
2288 else
2289 copy_function_or_variable (node);
2290 gcc_assert (lto_get_out_decl_state () == decl_state);
2291 lto_pop_out_decl_state ();
2292 lto_record_function_out_decl_state (node->decl, decl_state);
2293 timevar_pop (TV_IPA_LTO_CTORS_OUT);
2298 /* Emit the callgraph after emitting function bodies. This needs to
2299 be done now to make sure that all the statements in every function
2300 have been renumbered so that edges can be associated with call
2301 statements using the statement UIDs. */
2302 output_symtab ();
2304 #ifdef ENABLE_CHECKING
2305 lto_bitmap_free (output);
2306 #endif
2309 /* Write each node in encoded by ENCODER to OB, as well as those reachable
2310 from it and required for correct representation of its semantics.
2311 Each node in ENCODER must be a global declaration or a type. A node
2312 is written only once, even if it appears multiple times in the
2313 vector. Certain transitively-reachable nodes, such as those
2314 representing expressions, may be duplicated, but such nodes
2315 must not appear in ENCODER itself. */
2317 static void
2318 write_global_stream (struct output_block *ob,
2319 struct lto_tree_ref_encoder *encoder)
2321 tree t;
2322 size_t index;
2323 const size_t size = lto_tree_ref_encoder_size (encoder);
2325 for (index = 0; index < size; index++)
2327 t = lto_tree_ref_encoder_get_tree (encoder, index);
2328 if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
2329 stream_write_tree (ob, t, false);
2334 /* Write a sequence of indices into the globals vector corresponding
2335 to the trees in ENCODER. These are used by the reader to map the
2336 indices used to refer to global entities within function bodies to
2337 their referents. */
2339 static void
2340 write_global_references (struct output_block *ob,
2341 struct lto_tree_ref_encoder *encoder)
2343 tree t;
2344 uint32_t index;
2345 const uint32_t size = lto_tree_ref_encoder_size (encoder);
2347 /* Write size and slot indexes as 32-bit unsigned numbers. */
2348 uint32_t *data = XNEWVEC (uint32_t, size + 1);
2349 data[0] = size;
2351 for (index = 0; index < size; index++)
2353 uint32_t slot_num;
2355 t = lto_tree_ref_encoder_get_tree (encoder, index);
2356 streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
2357 gcc_assert (slot_num != (unsigned)-1);
2358 data[index + 1] = slot_num;
2361 lto_write_data (data, sizeof (int32_t) * (size + 1));
2362 free (data);
2366 /* Write all the streams in an lto_out_decl_state STATE using
2367 output block OB and output stream OUT_STREAM. */
2369 void
2370 lto_output_decl_state_streams (struct output_block *ob,
2371 struct lto_out_decl_state *state)
2373 int i;
2375 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2376 write_global_stream (ob, &state->streams[i]);
2380 /* Write all the references in an lto_out_decl_state STATE using
2381 output block OB and output stream OUT_STREAM. */
2383 void
2384 lto_output_decl_state_refs (struct output_block *ob,
2385 struct lto_out_decl_state *state)
2387 unsigned i;
2388 uint32_t ref;
2389 tree decl;
2391 /* Write reference to FUNCTION_DECL. If there is not function,
2392 write reference to void_type_node. */
2393 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
2394 streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
2395 gcc_assert (ref != (unsigned)-1);
2396 lto_write_data (&ref, sizeof (uint32_t));
2398 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2399 write_global_references (ob, &state->streams[i]);
2403 /* Return the written size of STATE. */
2405 static size_t
2406 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
2408 int i;
2409 size_t size;
2411 size = sizeof (int32_t); /* fn_ref. */
2412 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
2414 size += sizeof (int32_t); /* vector size. */
2415 size += (lto_tree_ref_encoder_size (&state->streams[i])
2416 * sizeof (int32_t));
2418 return size;
2422 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
2423 so far. */
2425 static void
2426 write_symbol (struct streamer_tree_cache_d *cache,
2427 tree t, hash_set<const char *> *seen, bool alias)
2429 const char *name;
2430 enum gcc_plugin_symbol_kind kind;
2431 enum gcc_plugin_symbol_visibility visibility = GCCPV_DEFAULT;
2432 unsigned slot_num;
2433 uint64_t size;
2434 const char *comdat;
2435 unsigned char c;
2437 /* None of the following kinds of symbols are needed in the
2438 symbol table. */
2439 if (!TREE_PUBLIC (t)
2440 || is_builtin_fn (t)
2441 || DECL_ABSTRACT_P (t)
2442 || (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t)))
2443 return;
2444 gcc_assert (TREE_CODE (t) != RESULT_DECL);
2446 gcc_assert (TREE_CODE (t) == VAR_DECL
2447 || TREE_CODE (t) == FUNCTION_DECL);
2449 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
2451 /* This behaves like assemble_name_raw in varasm.c, performing the
2452 same name manipulations that ASM_OUTPUT_LABELREF does. */
2453 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
2455 if (seen->add (name))
2456 return;
2458 streamer_tree_cache_lookup (cache, t, &slot_num);
2459 gcc_assert (slot_num != (unsigned)-1);
2461 if (DECL_EXTERNAL (t))
2463 if (DECL_WEAK (t))
2464 kind = GCCPK_WEAKUNDEF;
2465 else
2466 kind = GCCPK_UNDEF;
2468 else
2470 if (DECL_WEAK (t))
2471 kind = GCCPK_WEAKDEF;
2472 else if (DECL_COMMON (t))
2473 kind = GCCPK_COMMON;
2474 else
2475 kind = GCCPK_DEF;
2477 /* When something is defined, it should have node attached. */
2478 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
2479 || varpool_node::get (t)->definition);
2480 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
2481 || (cgraph_node::get (t)
2482 && cgraph_node::get (t)->definition));
2485 /* Imitate what default_elf_asm_output_external do.
2486 When symbol is external, we need to output it with DEFAULT visibility
2487 when compiling with -fvisibility=default, while with HIDDEN visibility
2488 when symbol has attribute (visibility("hidden")) specified.
2489 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
2490 right. */
2492 if (DECL_EXTERNAL (t)
2493 && !targetm.binds_local_p (t))
2494 visibility = GCCPV_DEFAULT;
2495 else
2496 switch (DECL_VISIBILITY (t))
2498 case VISIBILITY_DEFAULT:
2499 visibility = GCCPV_DEFAULT;
2500 break;
2501 case VISIBILITY_PROTECTED:
2502 visibility = GCCPV_PROTECTED;
2503 break;
2504 case VISIBILITY_HIDDEN:
2505 visibility = GCCPV_HIDDEN;
2506 break;
2507 case VISIBILITY_INTERNAL:
2508 visibility = GCCPV_INTERNAL;
2509 break;
2512 if (kind == GCCPK_COMMON
2513 && DECL_SIZE_UNIT (t)
2514 && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
2515 size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
2516 else
2517 size = 0;
2519 if (DECL_ONE_ONLY (t))
2520 comdat = IDENTIFIER_POINTER (decl_comdat_group_id (t));
2521 else
2522 comdat = "";
2524 lto_write_data (name, strlen (name) + 1);
2525 lto_write_data (comdat, strlen (comdat) + 1);
2526 c = (unsigned char) kind;
2527 lto_write_data (&c, 1);
2528 c = (unsigned char) visibility;
2529 lto_write_data (&c, 1);
2530 lto_write_data (&size, 8);
2531 lto_write_data (&slot_num, 4);
2534 /* Return true if NODE should appear in the plugin symbol table. */
2536 bool
2537 output_symbol_p (symtab_node *node)
2539 struct cgraph_node *cnode;
2540 if (!node->real_symbol_p ())
2541 return false;
2542 /* We keep external functions in symtab for sake of inlining
2543 and devirtualization. We do not want to see them in symbol table as
2544 references unless they are really used. */
2545 cnode = dyn_cast <cgraph_node *> (node);
2546 if (cnode && (!node->definition || DECL_EXTERNAL (cnode->decl))
2547 && cnode->callers)
2548 return true;
2550 /* Ignore all references from external vars initializers - they are not really
2551 part of the compilation unit until they are used by folding. Some symbols,
2552 like references to external construction vtables can not be referred to at all.
2553 We decide this at can_refer_decl_in_current_unit_p. */
2554 if (!node->definition || DECL_EXTERNAL (node->decl))
2556 int i;
2557 struct ipa_ref *ref;
2558 for (i = 0; node->iterate_referring (i, ref); i++)
2560 if (ref->use == IPA_REF_ALIAS)
2561 continue;
2562 if (is_a <cgraph_node *> (ref->referring))
2563 return true;
2564 if (!DECL_EXTERNAL (ref->referring->decl))
2565 return true;
2567 return false;
2569 return true;
2573 /* Write an IL symbol table to OB.
2574 SET and VSET are cgraph/varpool node sets we are outputting. */
2576 static void
2577 produce_symtab (struct output_block *ob)
2579 struct streamer_tree_cache_d *cache = ob->writer_cache;
2580 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
2581 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
2582 lto_symtab_encoder_iterator lsei;
2584 lto_begin_section (section_name, false);
2585 free (section_name);
2587 hash_set<const char *> seen;
2589 /* Write the symbol table.
2590 First write everything defined and then all declarations.
2591 This is necessary to handle cases where we have duplicated symbols. */
2592 for (lsei = lsei_start (encoder);
2593 !lsei_end_p (lsei); lsei_next (&lsei))
2595 symtab_node *node = lsei_node (lsei);
2597 if (!output_symbol_p (node) || DECL_EXTERNAL (node->decl))
2598 continue;
2599 write_symbol (cache, node->decl, &seen, false);
2601 for (lsei = lsei_start (encoder);
2602 !lsei_end_p (lsei); lsei_next (&lsei))
2604 symtab_node *node = lsei_node (lsei);
2606 if (!output_symbol_p (node) || !DECL_EXTERNAL (node->decl))
2607 continue;
2608 write_symbol (cache, node->decl, &seen, false);
2611 lto_end_section ();
2615 /* This pass is run after all of the functions are serialized and all
2616 of the IPA passes have written their serialized forms. This pass
2617 causes the vector of all of the global decls and types used from
2618 this file to be written in to a section that can then be read in to
2619 recover these on other side. */
2621 void
2622 produce_asm_for_decls (void)
2624 struct lto_out_decl_state *out_state;
2625 struct lto_out_decl_state *fn_out_state;
2626 struct lto_decl_header header;
2627 char *section_name;
2628 struct output_block *ob;
2629 unsigned idx, num_fns;
2630 size_t decl_state_size;
2631 int32_t num_decl_states;
2633 ob = create_output_block (LTO_section_decls);
2635 memset (&header, 0, sizeof (struct lto_decl_header));
2637 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
2638 lto_begin_section (section_name, !flag_wpa);
2639 free (section_name);
2641 /* Make string 0 be a NULL string. */
2642 streamer_write_char_stream (ob->string_stream, 0);
2644 gcc_assert (!alias_pairs);
2646 /* Get rid of the global decl state hash tables to save some memory. */
2647 out_state = lto_get_out_decl_state ();
2648 for (int i = 0; i < LTO_N_DECL_STREAMS; i++)
2649 if (out_state->streams[i].tree_hash_table)
2651 delete out_state->streams[i].tree_hash_table;
2652 out_state->streams[i].tree_hash_table = NULL;
2655 /* Write the global symbols. */
2656 lto_output_decl_state_streams (ob, out_state);
2657 num_fns = lto_function_decl_states.length ();
2658 for (idx = 0; idx < num_fns; idx++)
2660 fn_out_state =
2661 lto_function_decl_states[idx];
2662 lto_output_decl_state_streams (ob, fn_out_state);
2665 header.major_version = LTO_major_version;
2666 header.minor_version = LTO_minor_version;
2668 /* Currently not used. This field would allow us to preallocate
2669 the globals vector, so that it need not be resized as it is extended. */
2670 header.num_nodes = -1;
2672 /* Compute the total size of all decl out states. */
2673 decl_state_size = sizeof (int32_t);
2674 decl_state_size += lto_out_decl_state_written_size (out_state);
2675 for (idx = 0; idx < num_fns; idx++)
2677 fn_out_state =
2678 lto_function_decl_states[idx];
2679 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
2681 header.decl_state_size = decl_state_size;
2683 header.main_size = ob->main_stream->total_size;
2684 header.string_size = ob->string_stream->total_size;
2686 lto_write_data (&header, sizeof header);
2688 /* Write the main out-decl state, followed by out-decl states of
2689 functions. */
2690 num_decl_states = num_fns + 1;
2691 lto_write_data (&num_decl_states, sizeof (num_decl_states));
2692 lto_output_decl_state_refs (ob, out_state);
2693 for (idx = 0; idx < num_fns; idx++)
2695 fn_out_state = lto_function_decl_states[idx];
2696 lto_output_decl_state_refs (ob, fn_out_state);
2699 lto_write_stream (ob->main_stream);
2700 lto_write_stream (ob->string_stream);
2702 lto_end_section ();
2704 /* Write the symbol table. It is used by linker to determine dependencies
2705 and thus we can skip it for WPA. */
2706 if (!flag_wpa)
2707 produce_symtab (ob);
2709 /* Write command line opts. */
2710 lto_write_options ();
2712 /* Deallocate memory and clean up. */
2713 for (idx = 0; idx < num_fns; idx++)
2715 fn_out_state =
2716 lto_function_decl_states[idx];
2717 lto_delete_out_decl_state (fn_out_state);
2719 lto_symtab_encoder_delete (ob->decl_state->symtab_node_encoder);
2720 lto_function_decl_states.release ();
2721 destroy_output_block (ob);