Fix 50988 testsuite failures
[official-gcc.git] / gcc / lto-streamer-out.c
blob4b3be3bb1ff5667a05df4d1708f551996f1eedd3
1 /* Write the GIMPLE representation to a file stream.
3 Copyright 2009, 2010 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 "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "lto-symtab.h"
43 #include "lto-streamer.h"
44 #include "data-streamer.h"
45 #include "gimple-streamer.h"
46 #include "tree-streamer.h"
47 #include "streamer-hooks.h"
50 /* Clear the line info stored in DATA_IN. */
52 static void
53 clear_line_info (struct output_block *ob)
55 ob->current_file = NULL;
56 ob->current_line = 0;
57 ob->current_col = 0;
61 /* Create the output block and return it. SECTION_TYPE is
62 LTO_section_function_body or LTO_static_initializer. */
64 struct output_block *
65 create_output_block (enum lto_section_type section_type)
67 struct output_block *ob = XCNEW (struct output_block);
69 ob->section_type = section_type;
70 ob->decl_state = lto_get_out_decl_state ();
71 ob->main_stream = XCNEW (struct lto_output_stream);
72 ob->string_stream = XCNEW (struct lto_output_stream);
73 ob->writer_cache = streamer_tree_cache_create ();
75 if (section_type == LTO_section_function_body)
76 ob->cfg_stream = XCNEW (struct lto_output_stream);
78 clear_line_info (ob);
80 ob->string_hash_table = htab_create (37, hash_string_slot_node,
81 eq_string_slot_node, NULL);
82 gcc_obstack_init (&ob->obstack);
84 return ob;
88 /* Destroy the output block OB. */
90 void
91 destroy_output_block (struct output_block *ob)
93 enum lto_section_type section_type = ob->section_type;
95 htab_delete (ob->string_hash_table);
97 free (ob->main_stream);
98 free (ob->string_stream);
99 if (section_type == LTO_section_function_body)
100 free (ob->cfg_stream);
102 streamer_tree_cache_delete (ob->writer_cache);
103 obstack_free (&ob->obstack, NULL);
105 free (ob);
109 /* Look up NODE in the type table and write the index for it to OB. */
111 static void
112 output_type_ref (struct output_block *ob, tree node)
114 streamer_write_record_start (ob, LTO_type_ref);
115 lto_output_type_ref_index (ob->decl_state, ob->main_stream, node);
119 /* Return true if tree node T is written to various tables. For these
120 nodes, we sometimes want to write their phyiscal representation
121 (via lto_output_tree), and sometimes we need to emit an index
122 reference into a table (via lto_output_tree_ref). */
124 static bool
125 tree_is_indexable (tree t)
127 if (TREE_CODE (t) == PARM_DECL)
128 return false;
129 else if (TREE_CODE (t) == VAR_DECL && decl_function_context (t)
130 && !TREE_STATIC (t))
131 return false;
132 /* Variably modified types need to be streamed alongside function
133 bodies because they can refer to local entities. Together with
134 them we have to localize their members as well.
135 ??? In theory that includes non-FIELD_DECLs as well. */
136 else if (TYPE_P (t)
137 && variably_modified_type_p (t, NULL_TREE))
138 return false;
139 else if (TREE_CODE (t) == FIELD_DECL
140 && variably_modified_type_p (DECL_CONTEXT (t), NULL_TREE))
141 return false;
142 else
143 return (TYPE_P (t) || DECL_P (t) || TREE_CODE (t) == SSA_NAME);
147 /* Output info about new location into bitpack BP.
148 After outputting bitpack, lto_output_location_data has
149 to be done to output actual data. */
151 static inline void
152 lto_output_location_bitpack (struct bitpack_d *bp,
153 struct output_block *ob,
154 location_t loc)
156 expanded_location xloc;
158 bp_pack_value (bp, loc == UNKNOWN_LOCATION, 1);
159 if (loc == UNKNOWN_LOCATION)
160 return;
162 xloc = expand_location (loc);
164 bp_pack_value (bp, ob->current_file != xloc.file, 1);
165 if (ob->current_file != xloc.file)
166 bp_pack_var_len_unsigned (bp,
167 streamer_string_index (ob, xloc.file,
168 strlen (xloc.file) + 1,
169 true));
170 ob->current_file = xloc.file;
172 bp_pack_value (bp, ob->current_line != xloc.line, 1);
173 if (ob->current_line != xloc.line)
174 bp_pack_var_len_unsigned (bp, xloc.line);
175 ob->current_line = xloc.line;
177 bp_pack_value (bp, ob->current_col != xloc.column, 1);
178 if (ob->current_col != xloc.column)
179 bp_pack_var_len_unsigned (bp, xloc.column);
180 ob->current_col = xloc.column;
184 /* Emit location LOC to output block OB.
185 If the output_location streamer hook exists, call it.
186 Otherwise, when bitpack is handy, it is more space efficient to call
187 lto_output_location_bitpack with existing bitpack. */
189 void
190 lto_output_location (struct output_block *ob, location_t loc)
192 if (streamer_hooks.output_location)
193 streamer_hooks.output_location (ob, loc);
194 else
196 struct bitpack_d bp = bitpack_create (ob->main_stream);
197 lto_output_location_bitpack (&bp, ob, loc);
198 streamer_write_bitpack (&bp);
203 /* If EXPR is an indexable tree node, output a reference to it to
204 output block OB. Otherwise, output the physical representation of
205 EXPR to OB. */
207 static void
208 lto_output_tree_ref (struct output_block *ob, tree expr)
210 enum tree_code code;
212 if (TYPE_P (expr))
214 output_type_ref (ob, expr);
215 return;
218 code = TREE_CODE (expr);
219 switch (code)
221 case SSA_NAME:
222 streamer_write_record_start (ob, LTO_ssa_name_ref);
223 streamer_write_uhwi (ob, SSA_NAME_VERSION (expr));
224 break;
226 case FIELD_DECL:
227 streamer_write_record_start (ob, LTO_field_decl_ref);
228 lto_output_field_decl_index (ob->decl_state, ob->main_stream, expr);
229 break;
231 case FUNCTION_DECL:
232 streamer_write_record_start (ob, LTO_function_decl_ref);
233 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, expr);
234 break;
236 case VAR_DECL:
237 case DEBUG_EXPR_DECL:
238 gcc_assert (decl_function_context (expr) == NULL || TREE_STATIC (expr));
239 streamer_write_record_start (ob, LTO_global_decl_ref);
240 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
241 break;
243 case CONST_DECL:
244 streamer_write_record_start (ob, LTO_const_decl_ref);
245 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
246 break;
248 case IMPORTED_DECL:
249 gcc_assert (decl_function_context (expr) == NULL);
250 streamer_write_record_start (ob, LTO_imported_decl_ref);
251 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
252 break;
254 case TYPE_DECL:
255 streamer_write_record_start (ob, LTO_type_decl_ref);
256 lto_output_type_decl_index (ob->decl_state, ob->main_stream, expr);
257 break;
259 case NAMESPACE_DECL:
260 streamer_write_record_start (ob, LTO_namespace_decl_ref);
261 lto_output_namespace_decl_index (ob->decl_state, ob->main_stream, expr);
262 break;
264 case LABEL_DECL:
265 streamer_write_record_start (ob, LTO_label_decl_ref);
266 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
267 break;
269 case RESULT_DECL:
270 streamer_write_record_start (ob, LTO_result_decl_ref);
271 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
272 break;
274 case TRANSLATION_UNIT_DECL:
275 streamer_write_record_start (ob, LTO_translation_unit_decl_ref);
276 lto_output_var_decl_index (ob->decl_state, ob->main_stream, expr);
277 break;
279 default:
280 /* No other node is indexable, so it should have been handled by
281 lto_output_tree. */
282 gcc_unreachable ();
287 /* Return true if EXPR is a tree node that can be written to disk. */
289 static inline bool
290 lto_is_streamable (tree expr)
292 enum tree_code code = TREE_CODE (expr);
294 /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
295 name version in lto_output_tree_ref (see output_ssa_names). */
296 return !is_lang_specific (expr)
297 && code != SSA_NAME
298 && code != CALL_EXPR
299 && code != LANG_TYPE
300 && code != MODIFY_EXPR
301 && code != INIT_EXPR
302 && code != TARGET_EXPR
303 && code != BIND_EXPR
304 && code != WITH_CLEANUP_EXPR
305 && code != STATEMENT_LIST
306 && code != OMP_CLAUSE
307 && code != OPTIMIZATION_NODE
308 && (code == CASE_LABEL_EXPR
309 || code == DECL_EXPR
310 || TREE_CODE_CLASS (code) != tcc_statement);
314 /* Write a physical representation of tree node EXPR to output block
315 OB. If REF_P is true, the leaves of EXPR are emitted as references
316 via lto_output_tree_ref. IX is the index into the streamer cache
317 where EXPR is stored. */
319 static void
320 lto_write_tree (struct output_block *ob, tree expr, bool ref_p)
322 struct bitpack_d bp;
324 if (!lto_is_streamable (expr))
325 internal_error ("tree code %qs is not supported in LTO streams",
326 tree_code_name[TREE_CODE (expr)]);
328 /* Write the header, containing everything needed to materialize
329 EXPR on the reading side. */
330 streamer_write_tree_header (ob, expr);
332 /* Pack all the non-pointer fields in EXPR into a bitpack and write
333 the resulting bitpack. */
334 bp = bitpack_create (ob->main_stream);
335 streamer_pack_tree_bitfields (&bp, expr);
336 streamer_write_bitpack (&bp);
338 /* Write all the pointer fields in EXPR. */
339 streamer_write_tree_body (ob, expr, ref_p);
341 /* Write any LTO-specific data to OB. */
342 if (DECL_P (expr)
343 && TREE_CODE (expr) != FUNCTION_DECL
344 && TREE_CODE (expr) != TRANSLATION_UNIT_DECL)
346 /* Handle DECL_INITIAL for symbols. */
347 tree initial = DECL_INITIAL (expr);
348 if (TREE_CODE (expr) == VAR_DECL
349 && (TREE_STATIC (expr) || DECL_EXTERNAL (expr))
350 && initial)
352 lto_varpool_encoder_t varpool_encoder;
353 struct varpool_node *vnode;
355 varpool_encoder = ob->decl_state->varpool_node_encoder;
356 vnode = varpool_get_node (expr);
357 if (!vnode)
358 initial = error_mark_node;
359 else if (!lto_varpool_encoder_encode_initializer_p (varpool_encoder,
360 vnode))
361 initial = NULL;
364 stream_write_tree (ob, initial, ref_p);
367 /* Mark the end of EXPR. */
368 streamer_write_zero (ob);
372 /* Emit the physical representation of tree node EXPR to output block
373 OB. If THIS_REF_P is true, the leaves of EXPR are emitted as references
374 via lto_output_tree_ref. REF_P is used for streaming siblings of EXPR. */
376 void
377 lto_output_tree (struct output_block *ob, tree expr,
378 bool ref_p, bool this_ref_p)
380 unsigned ix;
381 bool existed_p;
383 if (expr == NULL_TREE)
385 streamer_write_record_start (ob, LTO_null);
386 return;
389 if (this_ref_p && tree_is_indexable (expr))
391 lto_output_tree_ref (ob, expr);
392 return;
395 /* INTEGER_CST nodes are special because they need their original type
396 to be materialized by the reader (to implement TYPE_CACHED_VALUES). */
397 if (TREE_CODE (expr) == INTEGER_CST)
399 streamer_write_integer_cst (ob, expr, ref_p);
400 return;
403 existed_p = streamer_tree_cache_insert (ob->writer_cache, expr, &ix);
404 if (existed_p)
406 /* If a node has already been streamed out, make sure that
407 we don't write it more than once. Otherwise, the reader
408 will instantiate two different nodes for the same object. */
409 streamer_write_record_start (ob, LTO_tree_pickle_reference);
410 streamer_write_uhwi (ob, ix);
411 streamer_write_enum (ob->main_stream, LTO_tags, LTO_NUM_TAGS,
412 lto_tree_code_to_tag (TREE_CODE (expr)));
414 else if (streamer_handle_as_builtin_p (expr))
416 /* MD and NORMAL builtins do not need to be written out
417 completely as they are always instantiated by the
418 compiler on startup. The only builtins that need to
419 be written out are BUILT_IN_FRONTEND. For all other
420 builtins, we simply write the class and code. */
421 streamer_write_builtin (ob, expr);
423 else
425 /* This is the first time we see EXPR, write its fields
426 to OB. */
427 lto_write_tree (ob, expr, ref_p);
432 /* Output to OB a list of try/catch handlers starting with FIRST. */
434 static void
435 output_eh_try_list (struct output_block *ob, eh_catch first)
437 eh_catch n;
439 for (n = first; n; n = n->next_catch)
441 streamer_write_record_start (ob, LTO_eh_catch);
442 stream_write_tree (ob, n->type_list, true);
443 stream_write_tree (ob, n->filter_list, true);
444 stream_write_tree (ob, n->label, true);
447 streamer_write_record_start (ob, LTO_null);
451 /* Output EH region R in function FN to OB. CURR_RN is the slot index
452 that is being emitted in FN->EH->REGION_ARRAY. This is used to
453 detect EH region sharing. */
455 static void
456 output_eh_region (struct output_block *ob, eh_region r)
458 enum LTO_tags tag;
460 if (r == NULL)
462 streamer_write_record_start (ob, LTO_null);
463 return;
466 if (r->type == ERT_CLEANUP)
467 tag = LTO_ert_cleanup;
468 else if (r->type == ERT_TRY)
469 tag = LTO_ert_try;
470 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
471 tag = LTO_ert_allowed_exceptions;
472 else if (r->type == ERT_MUST_NOT_THROW)
473 tag = LTO_ert_must_not_throw;
474 else
475 gcc_unreachable ();
477 streamer_write_record_start (ob, tag);
478 streamer_write_hwi (ob, r->index);
480 if (r->outer)
481 streamer_write_hwi (ob, r->outer->index);
482 else
483 streamer_write_zero (ob);
485 if (r->inner)
486 streamer_write_hwi (ob, r->inner->index);
487 else
488 streamer_write_zero (ob);
490 if (r->next_peer)
491 streamer_write_hwi (ob, r->next_peer->index);
492 else
493 streamer_write_zero (ob);
495 if (r->type == ERT_TRY)
497 output_eh_try_list (ob, r->u.eh_try.first_catch);
499 else if (r->type == ERT_ALLOWED_EXCEPTIONS)
501 stream_write_tree (ob, r->u.allowed.type_list, true);
502 stream_write_tree (ob, r->u.allowed.label, true);
503 streamer_write_uhwi (ob, r->u.allowed.filter);
505 else if (r->type == ERT_MUST_NOT_THROW)
507 stream_write_tree (ob, r->u.must_not_throw.failure_decl, true);
508 lto_output_location (ob, r->u.must_not_throw.failure_loc);
511 if (r->landing_pads)
512 streamer_write_hwi (ob, r->landing_pads->index);
513 else
514 streamer_write_zero (ob);
518 /* Output landing pad LP to OB. */
520 static void
521 output_eh_lp (struct output_block *ob, eh_landing_pad lp)
523 if (lp == NULL)
525 streamer_write_record_start (ob, LTO_null);
526 return;
529 streamer_write_record_start (ob, LTO_eh_landing_pad);
530 streamer_write_hwi (ob, lp->index);
531 if (lp->next_lp)
532 streamer_write_hwi (ob, lp->next_lp->index);
533 else
534 streamer_write_zero (ob);
536 if (lp->region)
537 streamer_write_hwi (ob, lp->region->index);
538 else
539 streamer_write_zero (ob);
541 stream_write_tree (ob, lp->post_landing_pad, true);
545 /* Output the existing eh_table to OB. */
547 static void
548 output_eh_regions (struct output_block *ob, struct function *fn)
550 if (fn->eh && fn->eh->region_tree)
552 unsigned i;
553 eh_region eh;
554 eh_landing_pad lp;
555 tree ttype;
557 streamer_write_record_start (ob, LTO_eh_table);
559 /* Emit the index of the root of the EH region tree. */
560 streamer_write_hwi (ob, fn->eh->region_tree->index);
562 /* Emit all the EH regions in the region array. */
563 streamer_write_hwi (ob, VEC_length (eh_region, fn->eh->region_array));
564 FOR_EACH_VEC_ELT (eh_region, fn->eh->region_array, i, eh)
565 output_eh_region (ob, eh);
567 /* Emit all landing pads. */
568 streamer_write_hwi (ob, VEC_length (eh_landing_pad, fn->eh->lp_array));
569 FOR_EACH_VEC_ELT (eh_landing_pad, fn->eh->lp_array, i, lp)
570 output_eh_lp (ob, lp);
572 /* Emit all the runtime type data. */
573 streamer_write_hwi (ob, VEC_length (tree, fn->eh->ttype_data));
574 FOR_EACH_VEC_ELT (tree, fn->eh->ttype_data, i, ttype)
575 stream_write_tree (ob, ttype, true);
577 /* Emit the table of action chains. */
578 if (targetm.arm_eabi_unwinder)
580 tree t;
581 streamer_write_hwi (ob, VEC_length (tree,
582 fn->eh->ehspec_data.arm_eabi));
583 FOR_EACH_VEC_ELT (tree, fn->eh->ehspec_data.arm_eabi, i, t)
584 stream_write_tree (ob, t, true);
586 else
588 uchar c;
589 streamer_write_hwi (ob, VEC_length (uchar,
590 fn->eh->ehspec_data.other));
591 FOR_EACH_VEC_ELT (uchar, fn->eh->ehspec_data.other, i, c)
592 streamer_write_char_stream (ob->main_stream, c);
596 /* The LTO_null either terminates the record or indicates that there
597 are no eh_records at all. */
598 streamer_write_record_start (ob, LTO_null);
602 /* Output all of the active ssa names to the ssa_names stream. */
604 static void
605 output_ssa_names (struct output_block *ob, struct function *fn)
607 unsigned int i, len;
609 len = VEC_length (tree, SSANAMES (fn));
610 streamer_write_uhwi (ob, len);
612 for (i = 1; i < len; i++)
614 tree ptr = VEC_index (tree, SSANAMES (fn), i);
616 if (ptr == NULL_TREE
617 || SSA_NAME_IN_FREE_LIST (ptr)
618 || !is_gimple_reg (ptr))
619 continue;
621 streamer_write_uhwi (ob, i);
622 streamer_write_char_stream (ob->main_stream,
623 SSA_NAME_IS_DEFAULT_DEF (ptr));
624 stream_write_tree (ob, SSA_NAME_VAR (ptr), true);
627 streamer_write_zero (ob);
631 /* Output the cfg. */
633 static void
634 output_cfg (struct output_block *ob, struct function *fn)
636 struct lto_output_stream *tmp_stream = ob->main_stream;
637 basic_block bb;
639 ob->main_stream = ob->cfg_stream;
641 streamer_write_enum (ob->main_stream, profile_status_d, PROFILE_LAST,
642 profile_status_for_function (fn));
644 /* Output the number of the highest basic block. */
645 streamer_write_uhwi (ob, last_basic_block_for_function (fn));
647 FOR_ALL_BB_FN (bb, fn)
649 edge_iterator ei;
650 edge e;
652 streamer_write_hwi (ob, bb->index);
654 /* Output the successors and the edge flags. */
655 streamer_write_uhwi (ob, EDGE_COUNT (bb->succs));
656 FOR_EACH_EDGE (e, ei, bb->succs)
658 streamer_write_uhwi (ob, e->dest->index);
659 streamer_write_hwi (ob, e->probability);
660 streamer_write_hwi (ob, e->count);
661 streamer_write_uhwi (ob, e->flags);
665 streamer_write_hwi (ob, -1);
667 bb = ENTRY_BLOCK_PTR;
668 while (bb->next_bb)
670 streamer_write_hwi (ob, bb->next_bb->index);
671 bb = bb->next_bb;
674 streamer_write_hwi (ob, -1);
676 ob->main_stream = tmp_stream;
680 /* Create the header in the file using OB. If the section type is for
681 a function, set FN to the decl for that function. */
683 void
684 produce_asm (struct output_block *ob, tree fn)
686 enum lto_section_type section_type = ob->section_type;
687 struct lto_function_header header;
688 char *section_name;
689 struct lto_output_stream *header_stream;
691 if (section_type == LTO_section_function_body)
693 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn));
694 section_name = lto_get_section_name (section_type, name, NULL);
696 else
697 section_name = lto_get_section_name (section_type, NULL, NULL);
699 lto_begin_section (section_name, !flag_wpa);
700 free (section_name);
702 /* The entire header is stream computed here. */
703 memset (&header, 0, sizeof (struct lto_function_header));
705 /* Write the header. */
706 header.lto_header.major_version = LTO_major_version;
707 header.lto_header.minor_version = LTO_minor_version;
708 header.lto_header.section_type = section_type;
710 header.compressed_size = 0;
712 if (section_type == LTO_section_function_body)
713 header.cfg_size = ob->cfg_stream->total_size;
714 header.main_size = ob->main_stream->total_size;
715 header.string_size = ob->string_stream->total_size;
717 header_stream = XCNEW (struct lto_output_stream);
718 lto_output_data_stream (header_stream, &header, sizeof header);
719 lto_write_stream (header_stream);
720 free (header_stream);
722 /* Put all of the gimple and the string table out the asm file as a
723 block of text. */
724 if (section_type == LTO_section_function_body)
725 lto_write_stream (ob->cfg_stream);
726 lto_write_stream (ob->main_stream);
727 lto_write_stream (ob->string_stream);
729 lto_end_section ();
733 /* Output the base body of struct function FN using output block OB. */
735 static void
736 output_struct_function_base (struct output_block *ob, struct function *fn)
738 struct bitpack_d bp;
739 unsigned i;
740 tree t;
742 /* Output the static chain and non-local goto save area. */
743 stream_write_tree (ob, fn->static_chain_decl, true);
744 stream_write_tree (ob, fn->nonlocal_goto_save_area, true);
746 /* Output all the local variables in the function. */
747 streamer_write_hwi (ob, VEC_length (tree, fn->local_decls));
748 FOR_EACH_VEC_ELT (tree, fn->local_decls, i, t)
749 stream_write_tree (ob, t, true);
751 /* Output the function start and end loci. */
752 lto_output_location (ob, fn->function_start_locus);
753 lto_output_location (ob, fn->function_end_locus);
755 /* Output current IL state of the function. */
756 streamer_write_uhwi (ob, fn->curr_properties);
758 /* Write all the attributes for FN. */
759 bp = bitpack_create (ob->main_stream);
760 bp_pack_value (&bp, fn->is_thunk, 1);
761 bp_pack_value (&bp, fn->has_local_explicit_reg_vars, 1);
762 bp_pack_value (&bp, fn->after_tree_profile, 1);
763 bp_pack_value (&bp, fn->returns_pcc_struct, 1);
764 bp_pack_value (&bp, fn->returns_struct, 1);
765 bp_pack_value (&bp, fn->can_throw_non_call_exceptions, 1);
766 bp_pack_value (&bp, fn->always_inline_functions_inlined, 1);
767 bp_pack_value (&bp, fn->after_inlining, 1);
768 bp_pack_value (&bp, fn->stdarg, 1);
769 bp_pack_value (&bp, fn->has_nonlocal_label, 1);
770 bp_pack_value (&bp, fn->calls_alloca, 1);
771 bp_pack_value (&bp, fn->calls_setjmp, 1);
772 bp_pack_value (&bp, fn->va_list_fpr_size, 8);
773 bp_pack_value (&bp, fn->va_list_gpr_size, 8);
774 streamer_write_bitpack (&bp);
778 /* Output the body of function NODE->DECL. */
780 static void
781 output_function (struct cgraph_node *node)
783 tree function;
784 struct function *fn;
785 basic_block bb;
786 struct output_block *ob;
788 function = node->decl;
789 fn = DECL_STRUCT_FUNCTION (function);
790 ob = create_output_block (LTO_section_function_body);
792 clear_line_info (ob);
793 ob->cgraph_node = node;
795 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
797 /* Set current_function_decl and cfun. */
798 current_function_decl = function;
799 push_cfun (fn);
801 /* Make string 0 be a NULL string. */
802 streamer_write_char_stream (ob->string_stream, 0);
804 streamer_write_record_start (ob, LTO_function);
806 output_struct_function_base (ob, fn);
808 /* Output the head of the arguments list. */
809 stream_write_tree (ob, DECL_ARGUMENTS (function), true);
811 /* Output all the SSA names used in the function. */
812 output_ssa_names (ob, fn);
814 /* Output any exception handling regions. */
815 output_eh_regions (ob, fn);
817 /* Output DECL_INITIAL for the function, which contains the tree of
818 lexical scopes. */
819 stream_write_tree (ob, DECL_INITIAL (function), true);
821 /* We will renumber the statements. The code that does this uses
822 the same ordering that we use for serializing them so we can use
823 the same code on the other end and not have to write out the
824 statement numbers. We do not assign UIDs to PHIs here because
825 virtual PHIs get re-computed on-the-fly which would make numbers
826 inconsistent. */
827 set_gimple_stmt_max_uid (cfun, 0);
828 FOR_ALL_BB (bb)
830 gimple_stmt_iterator gsi;
831 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
833 gimple stmt = gsi_stmt (gsi);
834 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
838 /* Output the code for the function. */
839 FOR_ALL_BB_FN (bb, fn)
840 output_bb (ob, bb, fn);
842 /* The terminator for this function. */
843 streamer_write_record_start (ob, LTO_null);
845 output_cfg (ob, fn);
847 /* Create a section to hold the pickled output of this function. */
848 produce_asm (ob, function);
850 destroy_output_block (ob);
852 current_function_decl = NULL;
853 pop_cfun ();
857 /* Used to pass data to trivally_defined_alias callback. */
858 struct sets {
859 cgraph_node_set set;
860 varpool_node_set vset;
864 /* Return true if alias pair P belongs to the set of cgraph nodes in
865 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
866 However, for FUNCTION_DECL aliases, we should only output the pair
867 if it belongs to a function whose cgraph node is in SET.
868 Otherwise, the LTRANS phase will get into trouble when finalizing
869 aliases because the alias will refer to a function not defined in
870 the file processed by LTRANS. */
872 static bool
873 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED,
874 tree target, void *data)
876 struct sets *set = (struct sets *) data;
877 struct cgraph_node *fnode = NULL;
878 struct varpool_node *vnode = NULL;
880 fnode = cgraph_node_for_asm (target);
881 if (fnode)
882 return cgraph_node_in_set_p (fnode, set->set);
883 vnode = varpool_node_for_asm (target);
884 return vnode && varpool_node_in_set_p (vnode, set->vset);
887 /* Return true if alias pair P should be output in the current
888 partition contains cgrpah nodes SET and varpool nodes VSET.
889 DEFINED is set of all aliases whose targets are defined in
890 the partition.
892 Normal aliases are output when they are defined, while WEAKREF
893 aliases are output when they are used. */
895 static bool
896 output_alias_pair_p (alias_pair *p, symbol_alias_set_t *defined,
897 cgraph_node_set set, varpool_node_set vset)
899 struct cgraph_node *node;
900 struct varpool_node *vnode;
902 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
904 if (TREE_CODE (p->decl) == VAR_DECL)
906 vnode = varpool_get_node (p->decl);
907 return (vnode
908 && referenced_from_this_partition_p (&vnode->ref_list, set, vset));
910 node = cgraph_get_node (p->decl);
911 return (node
912 && (referenced_from_this_partition_p (&node->ref_list, set, vset)
913 || reachable_from_this_partition_p (node, set)));
915 else
916 return symbol_alias_set_contains (defined, p->decl);
919 /* Output any unreferenced global symbol defined in SET, alias pairs
920 and labels. */
922 static void
923 output_unreferenced_globals (cgraph_node_set set, varpool_node_set vset)
925 struct output_block *ob;
926 alias_pair *p;
927 unsigned i;
928 symbol_alias_set_t *defined;
929 struct sets setdata;
931 setdata.set = set;
932 setdata.vset = vset;
934 ob = create_output_block (LTO_section_static_initializer);
935 ob->cgraph_node = NULL;
937 clear_line_info (ob);
939 /* Make string 0 be a NULL string. */
940 streamer_write_char_stream (ob->string_stream, 0);
942 /* We really need to propagate in both directoins:
943 for normal aliases we propagate from first defined alias to
944 all aliases defined based on it. For weakrefs we propagate in
945 the oposite direction. */
946 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
948 /* Emit the alias pairs for the nodes in SET. */
949 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
950 if (output_alias_pair_p (p, defined, set, vset))
952 stream_write_tree (ob, p->decl, true);
953 stream_write_tree (ob, p->target, true);
955 symbol_alias_set_destroy (defined);
957 streamer_write_record_start (ob, LTO_null);
959 produce_asm (ob, NULL);
960 destroy_output_block (ob);
964 /* Emit toplevel asms. */
966 void
967 lto_output_toplevel_asms (void)
969 struct output_block *ob;
970 struct cgraph_asm_node *can;
971 char *section_name;
972 struct lto_output_stream *header_stream;
973 struct lto_asm_header header;
975 if (! cgraph_asm_nodes)
976 return;
978 ob = create_output_block (LTO_section_asm);
980 /* Make string 0 be a NULL string. */
981 streamer_write_char_stream (ob->string_stream, 0);
983 for (can = cgraph_asm_nodes; can; can = can->next)
985 streamer_write_string_cst (ob, ob->main_stream, can->asm_str);
986 streamer_write_hwi (ob, can->order);
989 streamer_write_string_cst (ob, ob->main_stream, NULL_TREE);
991 section_name = lto_get_section_name (LTO_section_asm, NULL, NULL);
992 lto_begin_section (section_name, !flag_wpa);
993 free (section_name);
995 /* The entire header stream is computed here. */
996 memset (&header, 0, sizeof (header));
998 /* Write the header. */
999 header.lto_header.major_version = LTO_major_version;
1000 header.lto_header.minor_version = LTO_minor_version;
1001 header.lto_header.section_type = LTO_section_asm;
1003 header.main_size = ob->main_stream->total_size;
1004 header.string_size = ob->string_stream->total_size;
1006 header_stream = XCNEW (struct lto_output_stream);
1007 lto_output_data_stream (header_stream, &header, sizeof (header));
1008 lto_write_stream (header_stream);
1009 free (header_stream);
1011 /* Put all of the gimple and the string table out the asm file as a
1012 block of text. */
1013 lto_write_stream (ob->main_stream);
1014 lto_write_stream (ob->string_stream);
1016 lto_end_section ();
1018 destroy_output_block (ob);
1022 /* Copy the function body of NODE without deserializing. */
1024 static void
1025 copy_function (struct cgraph_node *node)
1027 tree function = node->decl;
1028 struct lto_file_decl_data *file_data = node->local.lto_file_data;
1029 struct lto_output_stream *output_stream = XCNEW (struct lto_output_stream);
1030 const char *data;
1031 size_t len;
1032 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function));
1033 char *section_name =
1034 lto_get_section_name (LTO_section_function_body, name, NULL);
1035 size_t i, j;
1036 struct lto_in_decl_state *in_state;
1037 struct lto_out_decl_state *out_state = lto_get_out_decl_state ();
1039 lto_begin_section (section_name, !flag_wpa);
1040 free (section_name);
1042 /* We may have renamed the declaration, e.g., a static function. */
1043 name = lto_get_decl_name_mapping (file_data, name);
1045 data = lto_get_section_data (file_data, LTO_section_function_body,
1046 name, &len);
1047 gcc_assert (data);
1049 /* Do a bit copy of the function body. */
1050 lto_output_data_stream (output_stream, data, len);
1051 lto_write_stream (output_stream);
1053 /* Copy decls. */
1054 in_state =
1055 lto_get_function_in_decl_state (node->local.lto_file_data, function);
1056 gcc_assert (in_state);
1058 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1060 size_t n = in_state->streams[i].size;
1061 tree *trees = in_state->streams[i].trees;
1062 struct lto_tree_ref_encoder *encoder = &(out_state->streams[i]);
1064 /* The out state must have the same indices and the in state.
1065 So just copy the vector. All the encoders in the in state
1066 must be empty where we reach here. */
1067 gcc_assert (lto_tree_ref_encoder_size (encoder) == 0);
1068 for (j = 0; j < n; j++)
1069 VEC_safe_push (tree, heap, encoder->trees, trees[j]);
1070 encoder->next_index = n;
1073 lto_free_section_data (file_data, LTO_section_function_body, name,
1074 data, len);
1075 free (output_stream);
1076 lto_end_section ();
1080 /* Main entry point from the pass manager. */
1082 static void
1083 lto_output (cgraph_node_set set, varpool_node_set vset)
1085 struct cgraph_node *node;
1086 struct lto_out_decl_state *decl_state;
1087 #ifdef ENABLE_CHECKING
1088 bitmap output = lto_bitmap_alloc ();
1089 #endif
1090 int i, n_nodes;
1091 lto_cgraph_encoder_t encoder = lto_get_out_decl_state ()->cgraph_node_encoder;
1093 /* Initialize the streamer. */
1094 lto_streamer_init ();
1096 n_nodes = lto_cgraph_encoder_size (encoder);
1097 /* Process only the functions with bodies. */
1098 for (i = 0; i < n_nodes; i++)
1100 node = lto_cgraph_encoder_deref (encoder, i);
1101 if (lto_cgraph_encoder_encode_body_p (encoder, node)
1102 && !node->alias
1103 && !node->thunk.thunk_p)
1105 #ifdef ENABLE_CHECKING
1106 gcc_assert (!bitmap_bit_p (output, DECL_UID (node->decl)));
1107 bitmap_set_bit (output, DECL_UID (node->decl));
1108 #endif
1109 decl_state = lto_new_out_decl_state ();
1110 lto_push_out_decl_state (decl_state);
1111 if (gimple_has_body_p (node->decl))
1112 output_function (node);
1113 else
1114 copy_function (node);
1115 gcc_assert (lto_get_out_decl_state () == decl_state);
1116 lto_pop_out_decl_state ();
1117 lto_record_function_out_decl_state (node->decl, decl_state);
1121 /* Emit the callgraph after emitting function bodies. This needs to
1122 be done now to make sure that all the statements in every function
1123 have been renumbered so that edges can be associated with call
1124 statements using the statement UIDs. */
1125 output_cgraph (set, vset);
1127 #ifdef ENABLE_CHECKING
1128 lto_bitmap_free (output);
1129 #endif
1132 struct ipa_opt_pass_d pass_ipa_lto_gimple_out =
1135 IPA_PASS,
1136 "lto_gimple_out", /* name */
1137 gate_lto_out, /* gate */
1138 NULL, /* execute */
1139 NULL, /* sub */
1140 NULL, /* next */
1141 0, /* static_pass_number */
1142 TV_IPA_LTO_GIMPLE_OUT, /* tv_id */
1143 0, /* properties_required */
1144 0, /* properties_provided */
1145 0, /* properties_destroyed */
1146 0, /* todo_flags_start */
1147 0 /* todo_flags_finish */
1149 NULL, /* generate_summary */
1150 lto_output, /* write_summary */
1151 NULL, /* read_summary */
1152 lto_output, /* write_optimization_summary */
1153 NULL, /* read_optimization_summary */
1154 NULL, /* stmt_fixup */
1155 0, /* TODOs */
1156 NULL, /* function_transform */
1157 NULL /* variable_transform */
1161 /* Write each node in encoded by ENCODER to OB, as well as those reachable
1162 from it and required for correct representation of its semantics.
1163 Each node in ENCODER must be a global declaration or a type. A node
1164 is written only once, even if it appears multiple times in the
1165 vector. Certain transitively-reachable nodes, such as those
1166 representing expressions, may be duplicated, but such nodes
1167 must not appear in ENCODER itself. */
1169 static void
1170 write_global_stream (struct output_block *ob,
1171 struct lto_tree_ref_encoder *encoder)
1173 tree t;
1174 size_t index;
1175 const size_t size = lto_tree_ref_encoder_size (encoder);
1177 for (index = 0; index < size; index++)
1179 t = lto_tree_ref_encoder_get_tree (encoder, index);
1180 if (!streamer_tree_cache_lookup (ob->writer_cache, t, NULL))
1181 stream_write_tree (ob, t, false);
1186 /* Write a sequence of indices into the globals vector corresponding
1187 to the trees in ENCODER. These are used by the reader to map the
1188 indices used to refer to global entities within function bodies to
1189 their referents. */
1191 static void
1192 write_global_references (struct output_block *ob,
1193 struct lto_output_stream *ref_stream,
1194 struct lto_tree_ref_encoder *encoder)
1196 tree t;
1197 uint32_t index;
1198 const uint32_t size = lto_tree_ref_encoder_size (encoder);
1200 /* Write size as 32-bit unsigned. */
1201 lto_output_data_stream (ref_stream, &size, sizeof (int32_t));
1203 for (index = 0; index < size; index++)
1205 uint32_t slot_num;
1207 t = lto_tree_ref_encoder_get_tree (encoder, index);
1208 streamer_tree_cache_lookup (ob->writer_cache, t, &slot_num);
1209 gcc_assert (slot_num != (unsigned)-1);
1210 lto_output_data_stream (ref_stream, &slot_num, sizeof slot_num);
1215 /* Write all the streams in an lto_out_decl_state STATE using
1216 output block OB and output stream OUT_STREAM. */
1218 void
1219 lto_output_decl_state_streams (struct output_block *ob,
1220 struct lto_out_decl_state *state)
1222 int i;
1224 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1225 write_global_stream (ob, &state->streams[i]);
1229 /* Write all the references in an lto_out_decl_state STATE using
1230 output block OB and output stream OUT_STREAM. */
1232 void
1233 lto_output_decl_state_refs (struct output_block *ob,
1234 struct lto_output_stream *out_stream,
1235 struct lto_out_decl_state *state)
1237 unsigned i;
1238 uint32_t ref;
1239 tree decl;
1241 /* Write reference to FUNCTION_DECL. If there is not function,
1242 write reference to void_type_node. */
1243 decl = (state->fn_decl) ? state->fn_decl : void_type_node;
1244 streamer_tree_cache_lookup (ob->writer_cache, decl, &ref);
1245 gcc_assert (ref != (unsigned)-1);
1246 lto_output_data_stream (out_stream, &ref, sizeof (uint32_t));
1248 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1249 write_global_references (ob, out_stream, &state->streams[i]);
1253 /* Return the written size of STATE. */
1255 static size_t
1256 lto_out_decl_state_written_size (struct lto_out_decl_state *state)
1258 int i;
1259 size_t size;
1261 size = sizeof (int32_t); /* fn_ref. */
1262 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
1264 size += sizeof (int32_t); /* vector size. */
1265 size += (lto_tree_ref_encoder_size (&state->streams[i])
1266 * sizeof (int32_t));
1268 return size;
1272 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
1273 so far. */
1275 static void
1276 write_symbol (struct streamer_tree_cache_d *cache,
1277 struct lto_output_stream *stream,
1278 tree t, struct pointer_set_t *seen, bool alias)
1280 const char *name;
1281 enum gcc_plugin_symbol_kind kind;
1282 enum gcc_plugin_symbol_visibility visibility;
1283 unsigned slot_num;
1284 unsigned HOST_WIDEST_INT size;
1285 const char *comdat;
1286 unsigned char c;
1288 /* None of the following kinds of symbols are needed in the
1289 symbol table. */
1290 if (!TREE_PUBLIC (t)
1291 || is_builtin_fn (t)
1292 || DECL_ABSTRACT (t)
1293 || TREE_CODE (t) == RESULT_DECL)
1294 return;
1296 gcc_assert (TREE_CODE (t) == VAR_DECL
1297 || TREE_CODE (t) == FUNCTION_DECL);
1299 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
1301 /* This behaves like assemble_name_raw in varasm.c, performing the
1302 same name manipulations that ASM_OUTPUT_LABELREF does. */
1303 name = IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) (name));
1305 if (pointer_set_contains (seen, name))
1306 return;
1307 pointer_set_insert (seen, name);
1309 streamer_tree_cache_lookup (cache, t, &slot_num);
1310 gcc_assert (slot_num != (unsigned)-1);
1312 if (DECL_EXTERNAL (t))
1314 if (DECL_WEAK (t))
1315 kind = GCCPK_WEAKUNDEF;
1316 else
1317 kind = GCCPK_UNDEF;
1319 else
1321 if (DECL_WEAK (t))
1322 kind = GCCPK_WEAKDEF;
1323 else if (DECL_COMMON (t))
1324 kind = GCCPK_COMMON;
1325 else
1326 kind = GCCPK_DEF;
1328 /* When something is defined, it should have node attached. */
1329 gcc_assert (alias || TREE_CODE (t) != VAR_DECL
1330 || varpool_get_node (t)->finalized);
1331 gcc_assert (alias || TREE_CODE (t) != FUNCTION_DECL
1332 || (cgraph_get_node (t)
1333 && cgraph_get_node (t)->analyzed));
1336 /* Imitate what default_elf_asm_output_external do.
1337 When symbol is external, we need to output it with DEFAULT visibility
1338 when compiling with -fvisibility=default, while with HIDDEN visibility
1339 when symbol has attribute (visibility("hidden")) specified.
1340 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
1341 right. */
1343 if (DECL_EXTERNAL (t)
1344 && !targetm.binds_local_p (t))
1345 visibility = GCCPV_DEFAULT;
1346 else
1347 switch (DECL_VISIBILITY(t))
1349 case VISIBILITY_DEFAULT:
1350 visibility = GCCPV_DEFAULT;
1351 break;
1352 case VISIBILITY_PROTECTED:
1353 visibility = GCCPV_PROTECTED;
1354 break;
1355 case VISIBILITY_HIDDEN:
1356 visibility = GCCPV_HIDDEN;
1357 break;
1358 case VISIBILITY_INTERNAL:
1359 visibility = GCCPV_INTERNAL;
1360 break;
1363 if (kind == GCCPK_COMMON
1364 && DECL_SIZE_UNIT (t)
1365 && TREE_CODE (DECL_SIZE_UNIT (t)) == INTEGER_CST)
1366 size = TREE_INT_CST_LOW (DECL_SIZE_UNIT (t));
1367 else
1368 size = 0;
1370 if (DECL_ONE_ONLY (t))
1371 comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t));
1372 else
1373 comdat = "";
1375 lto_output_data_stream (stream, name, strlen (name) + 1);
1376 lto_output_data_stream (stream, comdat, strlen (comdat) + 1);
1377 c = (unsigned char) kind;
1378 lto_output_data_stream (stream, &c, 1);
1379 c = (unsigned char) visibility;
1380 lto_output_data_stream (stream, &c, 1);
1381 lto_output_data_stream (stream, &size, 8);
1382 lto_output_data_stream (stream, &slot_num, 4);
1386 /* Write an IL symbol table to OB.
1387 SET and VSET are cgraph/varpool node sets we are outputting. */
1389 static void
1390 produce_symtab (struct output_block *ob,
1391 cgraph_node_set set, varpool_node_set vset)
1393 struct streamer_tree_cache_d *cache = ob->writer_cache;
1394 char *section_name = lto_get_section_name (LTO_section_symtab, NULL, NULL);
1395 struct pointer_set_t *seen;
1396 struct cgraph_node *node;
1397 struct varpool_node *vnode;
1398 struct lto_output_stream stream;
1399 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
1400 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
1401 int i;
1402 alias_pair *p;
1403 struct sets setdata;
1404 symbol_alias_set_t *defined;
1406 setdata.set = set;
1407 setdata.vset = vset;
1409 lto_begin_section (section_name, false);
1410 free (section_name);
1412 seen = pointer_set_create ();
1413 memset (&stream, 0, sizeof (stream));
1415 /* Write all functions.
1416 First write all defined functions and then write all used functions.
1417 This is done so only to handle duplicated symbols in cgraph. */
1418 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
1420 node = lto_cgraph_encoder_deref (encoder, i);
1421 if (DECL_EXTERNAL (node->decl))
1422 continue;
1423 if (DECL_COMDAT (node->decl)
1424 && cgraph_comdat_can_be_unshared_p (node))
1425 continue;
1426 if ((node->alias && !node->thunk.alias) || node->global.inlined_to)
1427 continue;
1428 write_symbol (cache, &stream, node->decl, seen, false);
1430 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
1432 node = lto_cgraph_encoder_deref (encoder, i);
1433 if (!DECL_EXTERNAL (node->decl))
1434 continue;
1435 /* We keep around unused extern inlines in order to be able to inline
1436 them indirectly or via vtables. Do not output them to symbol
1437 table: they end up being undefined and just consume space. */
1438 if (!node->address_taken && !node->callers)
1439 continue;
1440 if (DECL_COMDAT (node->decl)
1441 && cgraph_comdat_can_be_unshared_p (node))
1442 continue;
1443 if ((node->alias && !node->thunk.alias) || node->global.inlined_to)
1444 continue;
1445 write_symbol (cache, &stream, node->decl, seen, false);
1448 /* Write all variables. */
1449 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
1451 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
1452 if (DECL_EXTERNAL (vnode->decl))
1453 continue;
1454 /* COMDAT virtual tables can be unshared. Do not declare them
1455 in the LTO symbol table to prevent linker from forcing them
1456 into the output. */
1457 if (DECL_COMDAT (vnode->decl)
1458 && !vnode->force_output
1459 && vnode->finalized
1460 && DECL_VIRTUAL_P (vnode->decl))
1461 continue;
1462 if (vnode->alias && !vnode->alias_of)
1463 continue;
1464 write_symbol (cache, &stream, vnode->decl, seen, false);
1466 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
1468 vnode = lto_varpool_encoder_deref (varpool_encoder, i);
1469 if (!DECL_EXTERNAL (vnode->decl))
1470 continue;
1471 if (DECL_COMDAT (vnode->decl)
1472 && !vnode->force_output
1473 && vnode->finalized
1474 && DECL_VIRTUAL_P (vnode->decl))
1475 continue;
1476 if (vnode->alias && !vnode->alias_of)
1477 continue;
1478 write_symbol (cache, &stream, vnode->decl, seen, false);
1481 /* Write all aliases. */
1482 defined = propagate_aliases_backward (trivally_defined_alias, &setdata);
1483 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
1484 if (output_alias_pair_p (p, defined, set, vset))
1485 write_symbol (cache, &stream, p->decl, seen, true);
1486 symbol_alias_set_destroy (defined);
1488 lto_write_stream (&stream);
1489 pointer_set_destroy (seen);
1491 lto_end_section ();
1495 /* This pass is run after all of the functions are serialized and all
1496 of the IPA passes have written their serialized forms. This pass
1497 causes the vector of all of the global decls and types used from
1498 this file to be written in to a section that can then be read in to
1499 recover these on other side. */
1501 static void
1502 produce_asm_for_decls (cgraph_node_set set, varpool_node_set vset)
1504 struct lto_out_decl_state *out_state;
1505 struct lto_out_decl_state *fn_out_state;
1506 struct lto_decl_header header;
1507 char *section_name;
1508 struct output_block *ob;
1509 struct lto_output_stream *header_stream, *decl_state_stream;
1510 unsigned idx, num_fns;
1511 size_t decl_state_size;
1512 int32_t num_decl_states;
1514 ob = create_output_block (LTO_section_decls);
1515 ob->global = true;
1517 /* Write out unreferenced globals, alias pairs and labels. We defer
1518 doing this until now so that we can write out only what is
1519 needed. */
1520 output_unreferenced_globals (set, vset);
1522 memset (&header, 0, sizeof (struct lto_decl_header));
1524 section_name = lto_get_section_name (LTO_section_decls, NULL, NULL);
1525 lto_begin_section (section_name, !flag_wpa);
1526 free (section_name);
1528 /* Make string 0 be a NULL string. */
1529 streamer_write_char_stream (ob->string_stream, 0);
1531 /* Write the global symbols. */
1532 out_state = lto_get_out_decl_state ();
1533 num_fns = VEC_length (lto_out_decl_state_ptr, lto_function_decl_states);
1534 lto_output_decl_state_streams (ob, out_state);
1535 for (idx = 0; idx < num_fns; idx++)
1537 fn_out_state =
1538 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1539 lto_output_decl_state_streams (ob, fn_out_state);
1542 header.lto_header.major_version = LTO_major_version;
1543 header.lto_header.minor_version = LTO_minor_version;
1544 header.lto_header.section_type = LTO_section_decls;
1546 /* Currently not used. This field would allow us to preallocate
1547 the globals vector, so that it need not be resized as it is extended. */
1548 header.num_nodes = -1;
1550 /* Compute the total size of all decl out states. */
1551 decl_state_size = sizeof (int32_t);
1552 decl_state_size += lto_out_decl_state_written_size (out_state);
1553 for (idx = 0; idx < num_fns; idx++)
1555 fn_out_state =
1556 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1557 decl_state_size += lto_out_decl_state_written_size (fn_out_state);
1559 header.decl_state_size = decl_state_size;
1561 header.main_size = ob->main_stream->total_size;
1562 header.string_size = ob->string_stream->total_size;
1564 header_stream = XCNEW (struct lto_output_stream);
1565 lto_output_data_stream (header_stream, &header, sizeof header);
1566 lto_write_stream (header_stream);
1567 free (header_stream);
1569 /* Write the main out-decl state, followed by out-decl states of
1570 functions. */
1571 decl_state_stream = ((struct lto_output_stream *)
1572 xcalloc (1, sizeof (struct lto_output_stream)));
1573 num_decl_states = num_fns + 1;
1574 lto_output_data_stream (decl_state_stream, &num_decl_states,
1575 sizeof (num_decl_states));
1576 lto_output_decl_state_refs (ob, decl_state_stream, out_state);
1577 for (idx = 0; idx < num_fns; idx++)
1579 fn_out_state =
1580 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1581 lto_output_decl_state_refs (ob, decl_state_stream, fn_out_state);
1583 lto_write_stream (decl_state_stream);
1584 free(decl_state_stream);
1586 lto_write_stream (ob->main_stream);
1587 lto_write_stream (ob->string_stream);
1589 lto_end_section ();
1591 /* Write the symbol table. It is used by linker to determine dependencies
1592 and thus we can skip it for WPA. */
1593 if (!flag_wpa)
1594 produce_symtab (ob, set, vset);
1596 /* Write command line opts. */
1597 lto_write_options ();
1599 /* Deallocate memory and clean up. */
1600 for (idx = 0; idx < num_fns; idx++)
1602 fn_out_state =
1603 VEC_index (lto_out_decl_state_ptr, lto_function_decl_states, idx);
1604 lto_delete_out_decl_state (fn_out_state);
1606 lto_cgraph_encoder_delete (ob->decl_state->cgraph_node_encoder);
1607 lto_varpool_encoder_delete (ob->decl_state->varpool_node_encoder);
1608 VEC_free (lto_out_decl_state_ptr, heap, lto_function_decl_states);
1609 lto_function_decl_states = NULL;
1610 destroy_output_block (ob);
1614 struct ipa_opt_pass_d pass_ipa_lto_finish_out =
1617 IPA_PASS,
1618 "lto_decls_out", /* name */
1619 gate_lto_out, /* gate */
1620 NULL, /* execute */
1621 NULL, /* sub */
1622 NULL, /* next */
1623 0, /* static_pass_number */
1624 TV_IPA_LTO_DECL_OUT, /* tv_id */
1625 0, /* properties_required */
1626 0, /* properties_provided */
1627 0, /* properties_destroyed */
1628 0, /* todo_flags_start */
1629 0 /* todo_flags_finish */
1631 NULL, /* generate_summary */
1632 produce_asm_for_decls, /* write_summary */
1633 NULL, /* read_summary */
1634 produce_asm_for_decls, /* write_optimization_summary */
1635 NULL, /* read_optimization_summary */
1636 NULL, /* stmt_fixup */
1637 0, /* TODOs */
1638 NULL, /* function_transform */
1639 NULL /* variable_transform */