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
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
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/>. */
25 #include "coretypes.h"
33 #include "basic-block.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
39 #include "diagnostic-core.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. */
53 clear_line_info (struct output_block
*ob
)
55 ob
->current_file
= NULL
;
61 /* Create the output block and return it. SECTION_TYPE is
62 LTO_section_function_body or LTO_static_initializer. */
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
);
80 ob
->string_hash_table
= htab_create (37, hash_string_slot_node
,
81 eq_string_slot_node
, NULL
);
82 gcc_obstack_init (&ob
->obstack
);
88 /* Destroy the output block OB. */
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
);
109 /* Look up NODE in the type table and write the index for it to OB. */
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). */
125 tree_is_indexable (tree t
)
127 if (TREE_CODE (t
) == PARM_DECL
)
129 else if (TREE_CODE (t
) == VAR_DECL
&& decl_function_context (t
)
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. */
137 && variably_modified_type_p (t
, NULL_TREE
))
139 else if (TREE_CODE (t
) == FIELD_DECL
140 && variably_modified_type_p (DECL_CONTEXT (t
), NULL_TREE
))
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. */
152 lto_output_location_bitpack (struct bitpack_d
*bp
,
153 struct output_block
*ob
,
156 expanded_location xloc
;
158 loc
= LOCATION_LOCUS (loc
);
159 bp_pack_value (bp
, loc
== UNKNOWN_LOCATION
, 1);
160 if (loc
== UNKNOWN_LOCATION
)
163 xloc
= expand_location (loc
);
165 bp_pack_value (bp
, ob
->current_file
!= xloc
.file
, 1);
166 if (ob
->current_file
!= xloc
.file
)
167 bp_pack_var_len_unsigned (bp
,
168 streamer_string_index (ob
, xloc
.file
,
169 strlen (xloc
.file
) + 1,
171 ob
->current_file
= xloc
.file
;
173 bp_pack_value (bp
, ob
->current_line
!= xloc
.line
, 1);
174 if (ob
->current_line
!= xloc
.line
)
175 bp_pack_var_len_unsigned (bp
, xloc
.line
);
176 ob
->current_line
= xloc
.line
;
178 bp_pack_value (bp
, ob
->current_col
!= xloc
.column
, 1);
179 if (ob
->current_col
!= xloc
.column
)
180 bp_pack_var_len_unsigned (bp
, xloc
.column
);
181 ob
->current_col
= xloc
.column
;
185 /* Emit location LOC to output block OB.
186 If the output_location streamer hook exists, call it.
187 Otherwise, when bitpack is handy, it is more space efficient to call
188 lto_output_location_bitpack with existing bitpack. */
191 lto_output_location (struct output_block
*ob
, location_t loc
)
193 if (streamer_hooks
.output_location
)
194 streamer_hooks
.output_location (ob
, loc
);
197 struct bitpack_d bp
= bitpack_create (ob
->main_stream
);
198 lto_output_location_bitpack (&bp
, ob
, loc
);
199 streamer_write_bitpack (&bp
);
204 /* If EXPR is an indexable tree node, output a reference to it to
205 output block OB. Otherwise, output the physical representation of
209 lto_output_tree_ref (struct output_block
*ob
, tree expr
)
215 output_type_ref (ob
, expr
);
219 code
= TREE_CODE (expr
);
223 streamer_write_record_start (ob
, LTO_ssa_name_ref
);
224 streamer_write_uhwi (ob
, SSA_NAME_VERSION (expr
));
228 streamer_write_record_start (ob
, LTO_field_decl_ref
);
229 lto_output_field_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
233 streamer_write_record_start (ob
, LTO_function_decl_ref
);
234 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
238 case DEBUG_EXPR_DECL
:
239 gcc_assert (decl_function_context (expr
) == NULL
|| TREE_STATIC (expr
));
241 streamer_write_record_start (ob
, LTO_global_decl_ref
);
242 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
246 streamer_write_record_start (ob
, LTO_const_decl_ref
);
247 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
251 gcc_assert (decl_function_context (expr
) == NULL
);
252 streamer_write_record_start (ob
, LTO_imported_decl_ref
);
253 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
257 streamer_write_record_start (ob
, LTO_type_decl_ref
);
258 lto_output_type_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
262 streamer_write_record_start (ob
, LTO_namespace_decl_ref
);
263 lto_output_namespace_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
267 streamer_write_record_start (ob
, LTO_label_decl_ref
);
268 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
272 streamer_write_record_start (ob
, LTO_result_decl_ref
);
273 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
276 case TRANSLATION_UNIT_DECL
:
277 streamer_write_record_start (ob
, LTO_translation_unit_decl_ref
);
278 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
282 /* No other node is indexable, so it should have been handled by
289 /* Return true if EXPR is a tree node that can be written to disk. */
292 lto_is_streamable (tree expr
)
294 enum tree_code code
= TREE_CODE (expr
);
296 /* Notice that we reject SSA_NAMEs as well. We only emit the SSA
297 name version in lto_output_tree_ref (see output_ssa_names). */
298 return !is_lang_specific (expr
)
302 && code
!= MODIFY_EXPR
304 && code
!= TARGET_EXPR
306 && code
!= WITH_CLEANUP_EXPR
307 && code
!= STATEMENT_LIST
308 && code
!= OMP_CLAUSE
309 && (code
== CASE_LABEL_EXPR
311 || TREE_CODE_CLASS (code
) != tcc_statement
);
315 /* Write a physical representation of tree node EXPR to output block
316 OB. If REF_P is true, the leaves of EXPR are emitted as references
317 via lto_output_tree_ref. IX is the index into the streamer cache
318 where EXPR is stored. */
321 lto_write_tree (struct output_block
*ob
, tree expr
, bool ref_p
)
325 if (!lto_is_streamable (expr
))
326 internal_error ("tree code %qs is not supported in LTO streams",
327 tree_code_name
[TREE_CODE (expr
)]);
329 /* Write the header, containing everything needed to materialize
330 EXPR on the reading side. */
331 streamer_write_tree_header (ob
, expr
);
333 /* Pack all the non-pointer fields in EXPR into a bitpack and write
334 the resulting bitpack. */
335 bp
= bitpack_create (ob
->main_stream
);
336 streamer_pack_tree_bitfields (&bp
, expr
);
337 streamer_write_bitpack (&bp
);
339 /* Write all the pointer fields in EXPR. */
340 streamer_write_tree_body (ob
, expr
, ref_p
);
342 /* Write any LTO-specific data to OB. */
344 && TREE_CODE (expr
) != FUNCTION_DECL
345 && TREE_CODE (expr
) != TRANSLATION_UNIT_DECL
)
347 /* Handle DECL_INITIAL for symbols. */
348 tree initial
= DECL_INITIAL (expr
);
349 if (TREE_CODE (expr
) == VAR_DECL
350 && (TREE_STATIC (expr
) || DECL_EXTERNAL (expr
))
353 lto_symtab_encoder_t encoder
;
354 struct varpool_node
*vnode
;
356 encoder
= ob
->decl_state
->symtab_node_encoder
;
357 vnode
= varpool_get_node (expr
);
359 || !lto_symtab_encoder_encode_initializer_p (encoder
,
361 initial
= error_mark_node
;
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. */
377 lto_output_tree (struct output_block
*ob
, tree expr
,
378 bool ref_p
, bool this_ref_p
)
383 if (expr
== NULL_TREE
)
385 streamer_write_record_start (ob
, LTO_null
);
389 if (this_ref_p
&& tree_is_indexable (expr
))
391 lto_output_tree_ref (ob
, expr
);
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
);
403 existed_p
= streamer_tree_cache_insert (ob
->writer_cache
, expr
, &ix
);
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
);
425 /* This is the first time we see EXPR, write its fields
427 lto_write_tree (ob
, expr
, ref_p
);
432 /* Output to OB a list of try/catch handlers starting with FIRST. */
435 output_eh_try_list (struct output_block
*ob
, eh_catch first
)
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. */
456 output_eh_region (struct output_block
*ob
, eh_region r
)
462 streamer_write_record_start (ob
, LTO_null
);
466 if (r
->type
== ERT_CLEANUP
)
467 tag
= LTO_ert_cleanup
;
468 else if (r
->type
== 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
;
477 streamer_write_record_start (ob
, tag
);
478 streamer_write_hwi (ob
, r
->index
);
481 streamer_write_hwi (ob
, r
->outer
->index
);
483 streamer_write_zero (ob
);
486 streamer_write_hwi (ob
, r
->inner
->index
);
488 streamer_write_zero (ob
);
491 streamer_write_hwi (ob
, r
->next_peer
->index
);
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
);
512 streamer_write_hwi (ob
, r
->landing_pads
->index
);
514 streamer_write_zero (ob
);
518 /* Output landing pad LP to OB. */
521 output_eh_lp (struct output_block
*ob
, eh_landing_pad lp
)
525 streamer_write_record_start (ob
, LTO_null
);
529 streamer_write_record_start (ob
, LTO_eh_landing_pad
);
530 streamer_write_hwi (ob
, lp
->index
);
532 streamer_write_hwi (ob
, lp
->next_lp
->index
);
534 streamer_write_zero (ob
);
537 streamer_write_hwi (ob
, lp
->region
->index
);
539 streamer_write_zero (ob
);
541 stream_write_tree (ob
, lp
->post_landing_pad
, true);
545 /* Output the existing eh_table to OB. */
548 output_eh_regions (struct output_block
*ob
, struct function
*fn
)
550 if (fn
->eh
&& fn
->eh
->region_tree
)
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
)
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);
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. */
605 output_ssa_names (struct output_block
*ob
, struct function
*fn
)
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
);
617 || SSA_NAME_IN_FREE_LIST (ptr
)
618 || virtual_operand_p (ptr
))
621 streamer_write_uhwi (ob
, i
);
622 streamer_write_char_stream (ob
->main_stream
,
623 SSA_NAME_IS_DEFAULT_DEF (ptr
));
624 if (SSA_NAME_VAR (ptr
))
625 stream_write_tree (ob
, SSA_NAME_VAR (ptr
), true);
627 /* ??? This drops SSA_NAME_IDENTIFIER on the floor. */
628 stream_write_tree (ob
, TREE_TYPE (ptr
), true);
631 streamer_write_zero (ob
);
635 /* Output the cfg. */
638 output_cfg (struct output_block
*ob
, struct function
*fn
)
640 struct lto_output_stream
*tmp_stream
= ob
->main_stream
;
643 ob
->main_stream
= ob
->cfg_stream
;
645 streamer_write_enum (ob
->main_stream
, profile_status_d
, PROFILE_LAST
,
646 profile_status_for_function (fn
));
648 /* Output the number of the highest basic block. */
649 streamer_write_uhwi (ob
, last_basic_block_for_function (fn
));
651 FOR_ALL_BB_FN (bb
, fn
)
656 streamer_write_hwi (ob
, bb
->index
);
658 /* Output the successors and the edge flags. */
659 streamer_write_uhwi (ob
, EDGE_COUNT (bb
->succs
));
660 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
662 streamer_write_uhwi (ob
, e
->dest
->index
);
663 streamer_write_hwi (ob
, e
->probability
);
664 streamer_write_hwi (ob
, e
->count
);
665 streamer_write_uhwi (ob
, e
->flags
);
669 streamer_write_hwi (ob
, -1);
671 bb
= ENTRY_BLOCK_PTR
;
674 streamer_write_hwi (ob
, bb
->next_bb
->index
);
678 streamer_write_hwi (ob
, -1);
680 ob
->main_stream
= tmp_stream
;
684 /* Create the header in the file using OB. If the section type is for
685 a function, set FN to the decl for that function. */
688 produce_asm (struct output_block
*ob
, tree fn
)
690 enum lto_section_type section_type
= ob
->section_type
;
691 struct lto_function_header header
;
693 struct lto_output_stream
*header_stream
;
695 if (section_type
== LTO_section_function_body
)
697 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn
));
698 section_name
= lto_get_section_name (section_type
, name
, NULL
);
701 section_name
= lto_get_section_name (section_type
, NULL
, NULL
);
703 lto_begin_section (section_name
, !flag_wpa
);
706 /* The entire header is stream computed here. */
707 memset (&header
, 0, sizeof (struct lto_function_header
));
709 /* Write the header. */
710 header
.lto_header
.major_version
= LTO_major_version
;
711 header
.lto_header
.minor_version
= LTO_minor_version
;
713 header
.compressed_size
= 0;
715 if (section_type
== LTO_section_function_body
)
716 header
.cfg_size
= ob
->cfg_stream
->total_size
;
717 header
.main_size
= ob
->main_stream
->total_size
;
718 header
.string_size
= ob
->string_stream
->total_size
;
720 header_stream
= XCNEW (struct lto_output_stream
);
721 lto_output_data_stream (header_stream
, &header
, sizeof header
);
722 lto_write_stream (header_stream
);
723 free (header_stream
);
725 /* Put all of the gimple and the string table out the asm file as a
727 if (section_type
== LTO_section_function_body
)
728 lto_write_stream (ob
->cfg_stream
);
729 lto_write_stream (ob
->main_stream
);
730 lto_write_stream (ob
->string_stream
);
736 /* Output the base body of struct function FN using output block OB. */
739 output_struct_function_base (struct output_block
*ob
, struct function
*fn
)
745 /* Output the static chain and non-local goto save area. */
746 stream_write_tree (ob
, fn
->static_chain_decl
, true);
747 stream_write_tree (ob
, fn
->nonlocal_goto_save_area
, true);
749 /* Output all the local variables in the function. */
750 streamer_write_hwi (ob
, VEC_length (tree
, fn
->local_decls
));
751 FOR_EACH_VEC_ELT (tree
, fn
->local_decls
, i
, t
)
752 stream_write_tree (ob
, t
, true);
754 /* Output the function start and end loci. */
755 lto_output_location (ob
, fn
->function_start_locus
);
756 lto_output_location (ob
, fn
->function_end_locus
);
758 /* Output current IL state of the function. */
759 streamer_write_uhwi (ob
, fn
->curr_properties
);
761 /* Write all the attributes for FN. */
762 bp
= bitpack_create (ob
->main_stream
);
763 bp_pack_value (&bp
, fn
->is_thunk
, 1);
764 bp_pack_value (&bp
, fn
->has_local_explicit_reg_vars
, 1);
765 bp_pack_value (&bp
, fn
->returns_pcc_struct
, 1);
766 bp_pack_value (&bp
, fn
->returns_struct
, 1);
767 bp_pack_value (&bp
, fn
->can_throw_non_call_exceptions
, 1);
768 bp_pack_value (&bp
, fn
->can_delete_dead_exceptions
, 1);
769 bp_pack_value (&bp
, fn
->always_inline_functions_inlined
, 1);
770 bp_pack_value (&bp
, fn
->after_inlining
, 1);
771 bp_pack_value (&bp
, fn
->stdarg
, 1);
772 bp_pack_value (&bp
, fn
->has_nonlocal_label
, 1);
773 bp_pack_value (&bp
, fn
->calls_alloca
, 1);
774 bp_pack_value (&bp
, fn
->calls_setjmp
, 1);
775 bp_pack_value (&bp
, fn
->va_list_fpr_size
, 8);
776 bp_pack_value (&bp
, fn
->va_list_gpr_size
, 8);
777 streamer_write_bitpack (&bp
);
781 /* Output the body of function NODE->DECL. */
784 output_function (struct cgraph_node
*node
)
789 struct output_block
*ob
;
791 function
= node
->symbol
.decl
;
792 fn
= DECL_STRUCT_FUNCTION (function
);
793 ob
= create_output_block (LTO_section_function_body
);
795 clear_line_info (ob
);
796 ob
->cgraph_node
= node
;
798 gcc_assert (current_function_decl
== NULL_TREE
&& cfun
== NULL
);
800 /* Set current_function_decl and cfun. */
803 /* Make string 0 be a NULL string. */
804 streamer_write_char_stream (ob
->string_stream
, 0);
806 streamer_write_record_start (ob
, LTO_function
);
808 output_struct_function_base (ob
, fn
);
810 /* Output all the SSA names used in the function. */
811 output_ssa_names (ob
, fn
);
813 /* Output any exception handling regions. */
814 output_eh_regions (ob
, fn
);
816 /* Output DECL_INITIAL for the function, which contains the tree of
818 stream_write_tree (ob
, DECL_INITIAL (function
), true);
820 /* We will renumber the statements. The code that does this uses
821 the same ordering that we use for serializing them so we can use
822 the same code on the other end and not have to write out the
823 statement numbers. We do not assign UIDs to PHIs here because
824 virtual PHIs get re-computed on-the-fly which would make numbers
826 set_gimple_stmt_max_uid (cfun
, 0);
829 gimple_stmt_iterator gsi
;
830 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
832 gimple stmt
= gsi_stmt (gsi
);
833 gimple_set_uid (stmt
, inc_gimple_stmt_max_uid (cfun
));
837 /* Output the code for the function. */
838 FOR_ALL_BB_FN (bb
, fn
)
839 output_bb (ob
, bb
, fn
);
841 /* The terminator for this function. */
842 streamer_write_record_start (ob
, LTO_null
);
846 /* Create a section to hold the pickled output of this function. */
847 produce_asm (ob
, function
);
849 destroy_output_block (ob
);
855 /* Emit toplevel asms. */
858 lto_output_toplevel_asms (void)
860 struct output_block
*ob
;
861 struct asm_node
*can
;
863 struct lto_output_stream
*header_stream
;
864 struct lto_asm_header header
;
869 ob
= create_output_block (LTO_section_asm
);
871 /* Make string 0 be a NULL string. */
872 streamer_write_char_stream (ob
->string_stream
, 0);
874 for (can
= asm_nodes
; can
; can
= can
->next
)
876 streamer_write_string_cst (ob
, ob
->main_stream
, can
->asm_str
);
877 streamer_write_hwi (ob
, can
->order
);
880 streamer_write_string_cst (ob
, ob
->main_stream
, NULL_TREE
);
882 section_name
= lto_get_section_name (LTO_section_asm
, NULL
, NULL
);
883 lto_begin_section (section_name
, !flag_wpa
);
886 /* The entire header stream is computed here. */
887 memset (&header
, 0, sizeof (header
));
889 /* Write the header. */
890 header
.lto_header
.major_version
= LTO_major_version
;
891 header
.lto_header
.minor_version
= LTO_minor_version
;
893 header
.main_size
= ob
->main_stream
->total_size
;
894 header
.string_size
= ob
->string_stream
->total_size
;
896 header_stream
= XCNEW (struct lto_output_stream
);
897 lto_output_data_stream (header_stream
, &header
, sizeof (header
));
898 lto_write_stream (header_stream
);
899 free (header_stream
);
901 /* Put all of the gimple and the string table out the asm file as a
903 lto_write_stream (ob
->main_stream
);
904 lto_write_stream (ob
->string_stream
);
908 destroy_output_block (ob
);
912 /* Copy the function body of NODE without deserializing. */
915 copy_function (struct cgraph_node
*node
)
917 tree function
= node
->symbol
.decl
;
918 struct lto_file_decl_data
*file_data
= node
->symbol
.lto_file_data
;
919 struct lto_output_stream
*output_stream
= XCNEW (struct lto_output_stream
);
922 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
));
924 lto_get_section_name (LTO_section_function_body
, name
, NULL
);
926 struct lto_in_decl_state
*in_state
;
927 struct lto_out_decl_state
*out_state
= lto_get_out_decl_state ();
929 lto_begin_section (section_name
, !flag_wpa
);
932 /* We may have renamed the declaration, e.g., a static function. */
933 name
= lto_get_decl_name_mapping (file_data
, name
);
935 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
939 /* Do a bit copy of the function body. */
940 lto_output_data_stream (output_stream
, data
, len
);
941 lto_write_stream (output_stream
);
945 lto_get_function_in_decl_state (node
->symbol
.lto_file_data
, function
);
946 gcc_assert (in_state
);
948 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
950 size_t n
= in_state
->streams
[i
].size
;
951 tree
*trees
= in_state
->streams
[i
].trees
;
952 struct lto_tree_ref_encoder
*encoder
= &(out_state
->streams
[i
]);
954 /* The out state must have the same indices and the in state.
955 So just copy the vector. All the encoders in the in state
956 must be empty where we reach here. */
957 gcc_assert (lto_tree_ref_encoder_size (encoder
) == 0);
958 for (j
= 0; j
< n
; j
++)
959 VEC_safe_push (tree
, heap
, encoder
->trees
, trees
[j
]);
960 encoder
->next_index
= n
;
963 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
965 free (output_stream
);
970 /* Main entry point from the pass manager. */
975 struct cgraph_node
*node
;
976 struct lto_out_decl_state
*decl_state
;
977 #ifdef ENABLE_CHECKING
978 bitmap output
= lto_bitmap_alloc ();
981 lto_symtab_encoder_t encoder
= lto_get_out_decl_state ()->symtab_node_encoder
;
983 /* Initialize the streamer. */
984 lto_streamer_init ();
986 n_nodes
= lto_symtab_encoder_size (encoder
);
987 /* Process only the functions with bodies. */
988 for (i
= 0; i
< n_nodes
; i
++)
990 symtab_node snode
= lto_symtab_encoder_deref (encoder
, i
);
991 if (!symtab_function_p (snode
))
993 node
= cgraph (snode
);
994 if (lto_symtab_encoder_encode_body_p (encoder
, node
)
996 && !node
->thunk
.thunk_p
)
998 #ifdef ENABLE_CHECKING
999 gcc_assert (!bitmap_bit_p (output
, DECL_UID (node
->symbol
.decl
)));
1000 bitmap_set_bit (output
, DECL_UID (node
->symbol
.decl
));
1002 decl_state
= lto_new_out_decl_state ();
1003 lto_push_out_decl_state (decl_state
);
1004 if (gimple_has_body_p (node
->symbol
.decl
))
1005 output_function (node
);
1007 copy_function (node
);
1008 gcc_assert (lto_get_out_decl_state () == decl_state
);
1009 lto_pop_out_decl_state ();
1010 lto_record_function_out_decl_state (node
->symbol
.decl
, decl_state
);
1014 /* Emit the callgraph after emitting function bodies. This needs to
1015 be done now to make sure that all the statements in every function
1016 have been renumbered so that edges can be associated with call
1017 statements using the statement UIDs. */
1020 #ifdef ENABLE_CHECKING
1021 lto_bitmap_free (output
);
1025 struct ipa_opt_pass_d pass_ipa_lto_gimple_out
=
1029 "lto_gimple_out", /* name */
1030 gate_lto_out
, /* gate */
1034 0, /* static_pass_number */
1035 TV_IPA_LTO_GIMPLE_OUT
, /* tv_id */
1036 0, /* properties_required */
1037 0, /* properties_provided */
1038 0, /* properties_destroyed */
1039 0, /* todo_flags_start */
1040 0 /* todo_flags_finish */
1042 NULL
, /* generate_summary */
1043 lto_output
, /* write_summary */
1044 NULL
, /* read_summary */
1045 lto_output
, /* write_optimization_summary */
1046 NULL
, /* read_optimization_summary */
1047 NULL
, /* stmt_fixup */
1049 NULL
, /* function_transform */
1050 NULL
/* variable_transform */
1054 /* Write each node in encoded by ENCODER to OB, as well as those reachable
1055 from it and required for correct representation of its semantics.
1056 Each node in ENCODER must be a global declaration or a type. A node
1057 is written only once, even if it appears multiple times in the
1058 vector. Certain transitively-reachable nodes, such as those
1059 representing expressions, may be duplicated, but such nodes
1060 must not appear in ENCODER itself. */
1063 write_global_stream (struct output_block
*ob
,
1064 struct lto_tree_ref_encoder
*encoder
)
1068 const size_t size
= lto_tree_ref_encoder_size (encoder
);
1070 for (index
= 0; index
< size
; index
++)
1072 t
= lto_tree_ref_encoder_get_tree (encoder
, index
);
1073 if (!streamer_tree_cache_lookup (ob
->writer_cache
, t
, NULL
))
1074 stream_write_tree (ob
, t
, false);
1079 /* Write a sequence of indices into the globals vector corresponding
1080 to the trees in ENCODER. These are used by the reader to map the
1081 indices used to refer to global entities within function bodies to
1085 write_global_references (struct output_block
*ob
,
1086 struct lto_output_stream
*ref_stream
,
1087 struct lto_tree_ref_encoder
*encoder
)
1091 const uint32_t size
= lto_tree_ref_encoder_size (encoder
);
1093 /* Write size as 32-bit unsigned. */
1094 lto_output_data_stream (ref_stream
, &size
, sizeof (int32_t));
1096 for (index
= 0; index
< size
; index
++)
1100 t
= lto_tree_ref_encoder_get_tree (encoder
, index
);
1101 streamer_tree_cache_lookup (ob
->writer_cache
, t
, &slot_num
);
1102 gcc_assert (slot_num
!= (unsigned)-1);
1103 lto_output_data_stream (ref_stream
, &slot_num
, sizeof slot_num
);
1108 /* Write all the streams in an lto_out_decl_state STATE using
1109 output block OB and output stream OUT_STREAM. */
1112 lto_output_decl_state_streams (struct output_block
*ob
,
1113 struct lto_out_decl_state
*state
)
1117 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
1118 write_global_stream (ob
, &state
->streams
[i
]);
1122 /* Write all the references in an lto_out_decl_state STATE using
1123 output block OB and output stream OUT_STREAM. */
1126 lto_output_decl_state_refs (struct output_block
*ob
,
1127 struct lto_output_stream
*out_stream
,
1128 struct lto_out_decl_state
*state
)
1134 /* Write reference to FUNCTION_DECL. If there is not function,
1135 write reference to void_type_node. */
1136 decl
= (state
->fn_decl
) ? state
->fn_decl
: void_type_node
;
1137 streamer_tree_cache_lookup (ob
->writer_cache
, decl
, &ref
);
1138 gcc_assert (ref
!= (unsigned)-1);
1139 lto_output_data_stream (out_stream
, &ref
, sizeof (uint32_t));
1141 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
1142 write_global_references (ob
, out_stream
, &state
->streams
[i
]);
1146 /* Return the written size of STATE. */
1149 lto_out_decl_state_written_size (struct lto_out_decl_state
*state
)
1154 size
= sizeof (int32_t); /* fn_ref. */
1155 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
1157 size
+= sizeof (int32_t); /* vector size. */
1158 size
+= (lto_tree_ref_encoder_size (&state
->streams
[i
])
1159 * sizeof (int32_t));
1165 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
1169 write_symbol (struct streamer_tree_cache_d
*cache
,
1170 struct lto_output_stream
*stream
,
1171 tree t
, struct pointer_set_t
*seen
, bool alias
)
1174 enum gcc_plugin_symbol_kind kind
;
1175 enum gcc_plugin_symbol_visibility visibility
;
1177 unsigned HOST_WIDEST_INT size
;
1181 /* None of the following kinds of symbols are needed in the
1183 if (!TREE_PUBLIC (t
)
1184 || is_builtin_fn (t
)
1185 || DECL_ABSTRACT (t
)
1186 || TREE_CODE (t
) == RESULT_DECL
)
1189 gcc_assert (TREE_CODE (t
) == VAR_DECL
1190 || TREE_CODE (t
) == FUNCTION_DECL
);
1192 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
));
1194 /* This behaves like assemble_name_raw in varasm.c, performing the
1195 same name manipulations that ASM_OUTPUT_LABELREF does. */
1196 name
= IDENTIFIER_POINTER ((*targetm
.asm_out
.mangle_assembler_name
) (name
));
1198 if (pointer_set_contains (seen
, name
))
1200 pointer_set_insert (seen
, name
);
1202 streamer_tree_cache_lookup (cache
, t
, &slot_num
);
1203 gcc_assert (slot_num
!= (unsigned)-1);
1205 if (DECL_EXTERNAL (t
))
1208 kind
= GCCPK_WEAKUNDEF
;
1215 kind
= GCCPK_WEAKDEF
;
1216 else if (DECL_COMMON (t
))
1217 kind
= GCCPK_COMMON
;
1221 /* When something is defined, it should have node attached. */
1222 gcc_assert (alias
|| TREE_CODE (t
) != VAR_DECL
1223 || varpool_get_node (t
)->finalized
);
1224 gcc_assert (alias
|| TREE_CODE (t
) != FUNCTION_DECL
1225 || (cgraph_get_node (t
)
1226 && cgraph_get_node (t
)->analyzed
));
1229 /* Imitate what default_elf_asm_output_external do.
1230 When symbol is external, we need to output it with DEFAULT visibility
1231 when compiling with -fvisibility=default, while with HIDDEN visibility
1232 when symbol has attribute (visibility("hidden")) specified.
1233 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
1236 if (DECL_EXTERNAL (t
)
1237 && !targetm
.binds_local_p (t
))
1238 visibility
= GCCPV_DEFAULT
;
1240 switch (DECL_VISIBILITY(t
))
1242 case VISIBILITY_DEFAULT
:
1243 visibility
= GCCPV_DEFAULT
;
1245 case VISIBILITY_PROTECTED
:
1246 visibility
= GCCPV_PROTECTED
;
1248 case VISIBILITY_HIDDEN
:
1249 visibility
= GCCPV_HIDDEN
;
1251 case VISIBILITY_INTERNAL
:
1252 visibility
= GCCPV_INTERNAL
;
1256 if (kind
== GCCPK_COMMON
1257 && DECL_SIZE_UNIT (t
)
1258 && TREE_CODE (DECL_SIZE_UNIT (t
)) == INTEGER_CST
)
1259 size
= TREE_INT_CST_LOW (DECL_SIZE_UNIT (t
));
1263 if (DECL_ONE_ONLY (t
))
1264 comdat
= IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t
));
1268 lto_output_data_stream (stream
, name
, strlen (name
) + 1);
1269 lto_output_data_stream (stream
, comdat
, strlen (comdat
) + 1);
1270 c
= (unsigned char) kind
;
1271 lto_output_data_stream (stream
, &c
, 1);
1272 c
= (unsigned char) visibility
;
1273 lto_output_data_stream (stream
, &c
, 1);
1274 lto_output_data_stream (stream
, &size
, 8);
1275 lto_output_data_stream (stream
, &slot_num
, 4);
1279 /* Write an IL symbol table to OB.
1280 SET and VSET are cgraph/varpool node sets we are outputting. */
1283 produce_symtab (struct output_block
*ob
)
1285 struct streamer_tree_cache_d
*cache
= ob
->writer_cache
;
1286 char *section_name
= lto_get_section_name (LTO_section_symtab
, NULL
, NULL
);
1287 struct pointer_set_t
*seen
;
1288 struct cgraph_node
*node
;
1289 struct varpool_node
*vnode
;
1290 struct lto_output_stream stream
;
1291 lto_symtab_encoder_t encoder
= ob
->decl_state
->symtab_node_encoder
;
1294 lto_begin_section (section_name
, false);
1295 free (section_name
);
1297 seen
= pointer_set_create ();
1298 memset (&stream
, 0, sizeof (stream
));
1300 /* Write all functions.
1301 First write all defined functions and then write all used functions.
1302 This is done so only to handle duplicated symbols in cgraph. */
1303 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
1305 if (!symtab_function_p (lto_symtab_encoder_deref (encoder
, i
)))
1307 node
= cgraph (lto_symtab_encoder_deref (encoder
, i
));
1308 if (DECL_EXTERNAL (node
->symbol
.decl
))
1310 if (DECL_COMDAT (node
->symbol
.decl
)
1311 && cgraph_comdat_can_be_unshared_p (node
))
1313 if ((node
->alias
&& !node
->thunk
.alias
) || node
->global
.inlined_to
)
1315 write_symbol (cache
, &stream
, node
->symbol
.decl
, seen
, false);
1317 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
1319 if (!symtab_function_p (lto_symtab_encoder_deref (encoder
, i
)))
1321 node
= cgraph (lto_symtab_encoder_deref (encoder
, i
));
1322 if (!DECL_EXTERNAL (node
->symbol
.decl
))
1324 /* We keep around unused extern inlines in order to be able to inline
1325 them indirectly or via vtables. Do not output them to symbol
1326 table: they end up being undefined and just consume space. */
1327 if (!node
->symbol
.address_taken
&& !node
->callers
)
1329 if (DECL_COMDAT (node
->symbol
.decl
)
1330 && cgraph_comdat_can_be_unshared_p (node
))
1332 if ((node
->alias
&& !node
->thunk
.alias
) || node
->global
.inlined_to
)
1334 write_symbol (cache
, &stream
, node
->symbol
.decl
, seen
, false);
1337 /* Write all variables. */
1338 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
1340 if (!symtab_variable_p (lto_symtab_encoder_deref (encoder
, i
)))
1342 vnode
= varpool (lto_symtab_encoder_deref (encoder
, i
));
1343 if (DECL_EXTERNAL (vnode
->symbol
.decl
))
1345 /* COMDAT virtual tables can be unshared. Do not declare them
1346 in the LTO symbol table to prevent linker from forcing them
1348 if (DECL_COMDAT (vnode
->symbol
.decl
)
1349 && !vnode
->symbol
.force_output
1351 && DECL_VIRTUAL_P (vnode
->symbol
.decl
))
1353 if (vnode
->alias
&& !vnode
->alias_of
)
1355 write_symbol (cache
, &stream
, vnode
->symbol
.decl
, seen
, false);
1357 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
1359 if (!symtab_variable_p (lto_symtab_encoder_deref (encoder
, i
)))
1361 vnode
= varpool (lto_symtab_encoder_deref (encoder
, i
));
1362 if (!DECL_EXTERNAL (vnode
->symbol
.decl
))
1364 if (DECL_COMDAT (vnode
->symbol
.decl
)
1365 && !vnode
->symbol
.force_output
1367 && DECL_VIRTUAL_P (vnode
->symbol
.decl
))
1369 if (vnode
->alias
&& !vnode
->alias_of
)
1371 write_symbol (cache
, &stream
, vnode
->symbol
.decl
, seen
, false);
1374 lto_write_stream (&stream
);
1375 pointer_set_destroy (seen
);
1381 /* This pass is run after all of the functions are serialized and all
1382 of the IPA passes have written their serialized forms. This pass
1383 causes the vector of all of the global decls and types used from
1384 this file to be written in to a section that can then be read in to
1385 recover these on other side. */
1388 produce_asm_for_decls (void)
1390 struct lto_out_decl_state
*out_state
;
1391 struct lto_out_decl_state
*fn_out_state
;
1392 struct lto_decl_header header
;
1394 struct output_block
*ob
;
1395 struct lto_output_stream
*header_stream
, *decl_state_stream
;
1396 unsigned idx
, num_fns
;
1397 size_t decl_state_size
;
1398 int32_t num_decl_states
;
1400 ob
= create_output_block (LTO_section_decls
);
1403 memset (&header
, 0, sizeof (struct lto_decl_header
));
1405 section_name
= lto_get_section_name (LTO_section_decls
, NULL
, NULL
);
1406 lto_begin_section (section_name
, !flag_wpa
);
1407 free (section_name
);
1409 /* Make string 0 be a NULL string. */
1410 streamer_write_char_stream (ob
->string_stream
, 0);
1412 gcc_assert (!alias_pairs
);
1414 /* Write the global symbols. */
1415 out_state
= lto_get_out_decl_state ();
1416 num_fns
= VEC_length (lto_out_decl_state_ptr
, lto_function_decl_states
);
1417 lto_output_decl_state_streams (ob
, out_state
);
1418 for (idx
= 0; idx
< num_fns
; idx
++)
1421 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1422 lto_output_decl_state_streams (ob
, fn_out_state
);
1425 header
.lto_header
.major_version
= LTO_major_version
;
1426 header
.lto_header
.minor_version
= LTO_minor_version
;
1428 /* Currently not used. This field would allow us to preallocate
1429 the globals vector, so that it need not be resized as it is extended. */
1430 header
.num_nodes
= -1;
1432 /* Compute the total size of all decl out states. */
1433 decl_state_size
= sizeof (int32_t);
1434 decl_state_size
+= lto_out_decl_state_written_size (out_state
);
1435 for (idx
= 0; idx
< num_fns
; idx
++)
1438 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1439 decl_state_size
+= lto_out_decl_state_written_size (fn_out_state
);
1441 header
.decl_state_size
= decl_state_size
;
1443 header
.main_size
= ob
->main_stream
->total_size
;
1444 header
.string_size
= ob
->string_stream
->total_size
;
1446 header_stream
= XCNEW (struct lto_output_stream
);
1447 lto_output_data_stream (header_stream
, &header
, sizeof header
);
1448 lto_write_stream (header_stream
);
1449 free (header_stream
);
1451 /* Write the main out-decl state, followed by out-decl states of
1453 decl_state_stream
= XCNEW (struct lto_output_stream
);
1454 num_decl_states
= num_fns
+ 1;
1455 lto_output_data_stream (decl_state_stream
, &num_decl_states
,
1456 sizeof (num_decl_states
));
1457 lto_output_decl_state_refs (ob
, decl_state_stream
, out_state
);
1458 for (idx
= 0; idx
< num_fns
; idx
++)
1461 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1462 lto_output_decl_state_refs (ob
, decl_state_stream
, fn_out_state
);
1464 lto_write_stream (decl_state_stream
);
1465 free(decl_state_stream
);
1467 lto_write_stream (ob
->main_stream
);
1468 lto_write_stream (ob
->string_stream
);
1472 /* Write the symbol table. It is used by linker to determine dependencies
1473 and thus we can skip it for WPA. */
1475 produce_symtab (ob
);
1477 /* Write command line opts. */
1478 lto_write_options ();
1480 /* Deallocate memory and clean up. */
1481 for (idx
= 0; idx
< num_fns
; idx
++)
1484 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1485 lto_delete_out_decl_state (fn_out_state
);
1487 lto_symtab_encoder_delete (ob
->decl_state
->symtab_node_encoder
);
1488 VEC_free (lto_out_decl_state_ptr
, heap
, lto_function_decl_states
);
1489 lto_function_decl_states
= NULL
;
1490 destroy_output_block (ob
);
1494 struct ipa_opt_pass_d pass_ipa_lto_finish_out
=
1498 "lto_decls_out", /* name */
1499 gate_lto_out
, /* gate */
1503 0, /* static_pass_number */
1504 TV_IPA_LTO_DECL_OUT
, /* tv_id */
1505 0, /* properties_required */
1506 0, /* properties_provided */
1507 0, /* properties_destroyed */
1508 0, /* todo_flags_start */
1509 0 /* todo_flags_finish */
1511 NULL
, /* generate_summary */
1512 produce_asm_for_decls
, /* write_summary */
1513 NULL
, /* read_summary */
1514 produce_asm_for_decls
, /* write_optimization_summary */
1515 NULL
, /* read_optimization_summary */
1516 NULL
, /* stmt_fixup */
1518 NULL
, /* function_transform */
1519 NULL
/* variable_transform */