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
= lto_streamer_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 lto_streamer_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 output_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
)
133 return (TYPE_P (t
) || DECL_P (t
) || TREE_CODE (t
) == SSA_NAME
);
137 /* Output info about new location into bitpack BP.
138 After outputting bitpack, lto_output_location_data has
139 to be done to output actual data. */
142 lto_output_location_bitpack (struct bitpack_d
*bp
,
143 struct output_block
*ob
,
146 expanded_location xloc
;
148 bp_pack_value (bp
, loc
== UNKNOWN_LOCATION
, 1);
149 if (loc
== UNKNOWN_LOCATION
)
152 xloc
= expand_location (loc
);
154 bp_pack_value (bp
, ob
->current_file
!= xloc
.file
, 1);
155 if (ob
->current_file
!= xloc
.file
)
156 bp_pack_var_len_unsigned (bp
, lto_string_index (ob
,
158 strlen (xloc
.file
) + 1,
160 ob
->current_file
= xloc
.file
;
162 bp_pack_value (bp
, ob
->current_line
!= xloc
.line
, 1);
163 if (ob
->current_line
!= xloc
.line
)
164 bp_pack_var_len_unsigned (bp
, xloc
.line
);
165 ob
->current_line
= xloc
.line
;
167 bp_pack_value (bp
, ob
->current_col
!= xloc
.column
, 1);
168 if (ob
->current_col
!= xloc
.column
)
169 bp_pack_var_len_unsigned (bp
, xloc
.column
);
170 ob
->current_col
= xloc
.column
;
174 /* Emit location LOC to output block OB.
175 When bitpack is handy, it is more space effecient to call
176 lto_output_location_bitpack with existing bitpack. */
179 lto_output_location (struct output_block
*ob
, location_t loc
)
181 struct bitpack_d bp
= bitpack_create (ob
->main_stream
);
182 lto_output_location_bitpack (&bp
, ob
, loc
);
183 lto_output_bitpack (&bp
);
187 /* If EXPR is an indexable tree node, output a reference to it to
188 output block OB. Otherwise, output the physical representation of
192 lto_output_tree_ref (struct output_block
*ob
, tree expr
)
196 if (expr
== NULL_TREE
)
198 output_record_start (ob
, LTO_null
);
202 if (!tree_is_indexable (expr
))
204 /* Even though we are emitting the physical representation of
205 EXPR, its leaves must be emitted as references. */
206 lto_output_tree (ob
, expr
, true);
212 output_type_ref (ob
, expr
);
216 code
= TREE_CODE (expr
);
220 output_record_start (ob
, LTO_ssa_name_ref
);
221 output_uleb128 (ob
, SSA_NAME_VERSION (expr
));
225 output_record_start (ob
, LTO_field_decl_ref
);
226 lto_output_field_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
230 output_record_start (ob
, LTO_function_decl_ref
);
231 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
235 case DEBUG_EXPR_DECL
:
236 gcc_assert (decl_function_context (expr
) == NULL
237 || TREE_STATIC (expr
));
238 output_record_start (ob
, LTO_global_decl_ref
);
239 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
243 output_record_start (ob
, LTO_const_decl_ref
);
244 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
248 gcc_assert (decl_function_context (expr
) == NULL
);
249 output_record_start (ob
, LTO_imported_decl_ref
);
250 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
254 output_record_start (ob
, LTO_type_decl_ref
);
255 lto_output_type_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
259 output_record_start (ob
, LTO_namespace_decl_ref
);
260 lto_output_namespace_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
264 output_record_start (ob
, LTO_label_decl_ref
);
265 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
269 output_record_start (ob
, LTO_result_decl_ref
);
270 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
273 case TRANSLATION_UNIT_DECL
:
274 output_record_start (ob
, LTO_translation_unit_decl_ref
);
275 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
280 /* See if the streamer allows this node to be indexable
281 like other global declarations. */
282 if (streamer_hooks
.indexable_with_decls_p
283 && streamer_hooks
.indexable_with_decls_p (expr
))
285 output_record_start (ob
, LTO_global_decl_ref
);
286 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, expr
);
290 /* No other node is indexable, so it should have been
291 handled by lto_output_tree. */
299 /* Output to OB a list of try/catch handlers starting with FIRST. */
302 output_eh_try_list (struct output_block
*ob
, eh_catch first
)
306 for (n
= first
; n
; n
= n
->next_catch
)
308 output_record_start (ob
, LTO_eh_catch
);
309 lto_output_tree_ref (ob
, n
->type_list
);
310 lto_output_tree_ref (ob
, n
->filter_list
);
311 lto_output_tree_ref (ob
, n
->label
);
314 output_record_start (ob
, LTO_null
);
318 /* Output EH region R in function FN to OB. CURR_RN is the slot index
319 that is being emitted in FN->EH->REGION_ARRAY. This is used to
320 detect EH region sharing. */
323 output_eh_region (struct output_block
*ob
, eh_region r
)
329 output_record_start (ob
, LTO_null
);
333 if (r
->type
== ERT_CLEANUP
)
334 tag
= LTO_ert_cleanup
;
335 else if (r
->type
== ERT_TRY
)
337 else if (r
->type
== ERT_ALLOWED_EXCEPTIONS
)
338 tag
= LTO_ert_allowed_exceptions
;
339 else if (r
->type
== ERT_MUST_NOT_THROW
)
340 tag
= LTO_ert_must_not_throw
;
344 output_record_start (ob
, tag
);
345 output_sleb128 (ob
, r
->index
);
348 output_sleb128 (ob
, r
->outer
->index
);
353 output_sleb128 (ob
, r
->inner
->index
);
358 output_sleb128 (ob
, r
->next_peer
->index
);
362 if (r
->type
== ERT_TRY
)
364 output_eh_try_list (ob
, r
->u
.eh_try
.first_catch
);
366 else if (r
->type
== ERT_ALLOWED_EXCEPTIONS
)
368 lto_output_tree_ref (ob
, r
->u
.allowed
.type_list
);
369 lto_output_tree_ref (ob
, r
->u
.allowed
.label
);
370 output_uleb128 (ob
, r
->u
.allowed
.filter
);
372 else if (r
->type
== ERT_MUST_NOT_THROW
)
374 lto_output_tree_ref (ob
, r
->u
.must_not_throw
.failure_decl
);
375 lto_output_location (ob
, r
->u
.must_not_throw
.failure_loc
);
379 output_sleb128 (ob
, r
->landing_pads
->index
);
385 /* Output landing pad LP to OB. */
388 output_eh_lp (struct output_block
*ob
, eh_landing_pad lp
)
392 output_record_start (ob
, LTO_null
);
396 output_record_start (ob
, LTO_eh_landing_pad
);
397 output_sleb128 (ob
, lp
->index
);
399 output_sleb128 (ob
, lp
->next_lp
->index
);
404 output_sleb128 (ob
, lp
->region
->index
);
408 lto_output_tree_ref (ob
, lp
->post_landing_pad
);
412 /* Output the existing eh_table to OB. */
415 output_eh_regions (struct output_block
*ob
, struct function
*fn
)
417 if (fn
->eh
&& fn
->eh
->region_tree
)
424 output_record_start (ob
, LTO_eh_table
);
426 /* Emit the index of the root of the EH region tree. */
427 output_sleb128 (ob
, fn
->eh
->region_tree
->index
);
429 /* Emit all the EH regions in the region array. */
430 output_sleb128 (ob
, VEC_length (eh_region
, fn
->eh
->region_array
));
431 FOR_EACH_VEC_ELT (eh_region
, fn
->eh
->region_array
, i
, eh
)
432 output_eh_region (ob
, eh
);
434 /* Emit all landing pads. */
435 output_sleb128 (ob
, VEC_length (eh_landing_pad
, fn
->eh
->lp_array
));
436 FOR_EACH_VEC_ELT (eh_landing_pad
, fn
->eh
->lp_array
, i
, lp
)
437 output_eh_lp (ob
, lp
);
439 /* Emit all the runtime type data. */
440 output_sleb128 (ob
, VEC_length (tree
, fn
->eh
->ttype_data
));
441 FOR_EACH_VEC_ELT (tree
, fn
->eh
->ttype_data
, i
, ttype
)
442 lto_output_tree_ref (ob
, ttype
);
444 /* Emit the table of action chains. */
445 if (targetm
.arm_eabi_unwinder
)
448 output_sleb128 (ob
, VEC_length (tree
, fn
->eh
->ehspec_data
.arm_eabi
));
449 FOR_EACH_VEC_ELT (tree
, fn
->eh
->ehspec_data
.arm_eabi
, i
, t
)
450 lto_output_tree_ref (ob
, t
);
455 output_sleb128 (ob
, VEC_length (uchar
, fn
->eh
->ehspec_data
.other
));
456 FOR_EACH_VEC_ELT (uchar
, fn
->eh
->ehspec_data
.other
, i
, c
)
457 lto_output_1_stream (ob
->main_stream
, c
);
461 /* The LTO_null either terminates the record or indicates that there
462 are no eh_records at all. */
463 output_record_start (ob
, LTO_null
);
467 /* Output all of the active ssa names to the ssa_names stream. */
470 output_ssa_names (struct output_block
*ob
, struct function
*fn
)
474 len
= VEC_length (tree
, SSANAMES (fn
));
475 output_uleb128 (ob
, len
);
477 for (i
= 1; i
< len
; i
++)
479 tree ptr
= VEC_index (tree
, SSANAMES (fn
), i
);
482 || SSA_NAME_IN_FREE_LIST (ptr
)
483 || !is_gimple_reg (ptr
))
486 output_uleb128 (ob
, i
);
487 lto_output_1_stream (ob
->main_stream
, SSA_NAME_IS_DEFAULT_DEF (ptr
));
488 lto_output_tree_ref (ob
, SSA_NAME_VAR (ptr
));
495 /* Output the cfg. */
498 output_cfg (struct output_block
*ob
, struct function
*fn
)
500 struct lto_output_stream
*tmp_stream
= ob
->main_stream
;
503 ob
->main_stream
= ob
->cfg_stream
;
505 lto_output_enum (ob
->main_stream
, profile_status_d
, PROFILE_LAST
,
506 profile_status_for_function (fn
));
508 /* Output the number of the highest basic block. */
509 output_uleb128 (ob
, last_basic_block_for_function (fn
));
511 FOR_ALL_BB_FN (bb
, fn
)
516 output_sleb128 (ob
, bb
->index
);
518 /* Output the successors and the edge flags. */
519 output_uleb128 (ob
, EDGE_COUNT (bb
->succs
));
520 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
522 output_uleb128 (ob
, e
->dest
->index
);
523 output_sleb128 (ob
, e
->probability
);
524 output_sleb128 (ob
, e
->count
);
525 output_uleb128 (ob
, e
->flags
);
529 output_sleb128 (ob
, -1);
531 bb
= ENTRY_BLOCK_PTR
;
534 output_sleb128 (ob
, bb
->next_bb
->index
);
538 output_sleb128 (ob
, -1);
540 ob
->main_stream
= tmp_stream
;
544 /* Create the header in the file using OB. If the section type is for
545 a function, set FN to the decl for that function. */
548 produce_asm (struct output_block
*ob
, tree fn
)
550 enum lto_section_type section_type
= ob
->section_type
;
551 struct lto_function_header header
;
553 struct lto_output_stream
*header_stream
;
555 if (section_type
== LTO_section_function_body
)
557 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn
));
558 section_name
= lto_get_section_name (section_type
, name
, NULL
);
561 section_name
= lto_get_section_name (section_type
, NULL
, NULL
);
563 lto_begin_section (section_name
, !flag_wpa
);
566 /* The entire header is stream computed here. */
567 memset (&header
, 0, sizeof (struct lto_function_header
));
569 /* Write the header. */
570 header
.lto_header
.major_version
= LTO_major_version
;
571 header
.lto_header
.minor_version
= LTO_minor_version
;
572 header
.lto_header
.section_type
= section_type
;
574 header
.compressed_size
= 0;
576 if (section_type
== LTO_section_function_body
)
577 header
.cfg_size
= ob
->cfg_stream
->total_size
;
578 header
.main_size
= ob
->main_stream
->total_size
;
579 header
.string_size
= ob
->string_stream
->total_size
;
581 header_stream
= XCNEW (struct lto_output_stream
);
582 lto_output_data_stream (header_stream
, &header
, sizeof header
);
583 lto_write_stream (header_stream
);
584 free (header_stream
);
586 /* Put all of the gimple and the string table out the asm file as a
588 if (section_type
== LTO_section_function_body
)
589 lto_write_stream (ob
->cfg_stream
);
590 lto_write_stream (ob
->main_stream
);
591 lto_write_stream (ob
->string_stream
);
597 /* Output the body of function NODE->DECL. */
600 output_function (struct cgraph_node
*node
)
606 struct output_block
*ob
;
610 function
= node
->decl
;
611 fn
= DECL_STRUCT_FUNCTION (function
);
612 ob
= create_output_block (LTO_section_function_body
);
614 clear_line_info (ob
);
615 ob
->cgraph_node
= node
;
617 gcc_assert (current_function_decl
== NULL_TREE
&& cfun
== NULL
);
619 /* Set current_function_decl and cfun. */
620 current_function_decl
= function
;
623 /* Make string 0 be a NULL string. */
624 lto_output_1_stream (ob
->string_stream
, 0);
626 output_record_start (ob
, LTO_function
);
628 /* Write all the attributes for FN. */
629 bp
= bitpack_create (ob
->main_stream
);
630 bp_pack_value (&bp
, fn
->is_thunk
, 1);
631 bp_pack_value (&bp
, fn
->has_local_explicit_reg_vars
, 1);
632 bp_pack_value (&bp
, fn
->after_tree_profile
, 1);
633 bp_pack_value (&bp
, fn
->returns_pcc_struct
, 1);
634 bp_pack_value (&bp
, fn
->returns_struct
, 1);
635 bp_pack_value (&bp
, fn
->can_throw_non_call_exceptions
, 1);
636 bp_pack_value (&bp
, fn
->always_inline_functions_inlined
, 1);
637 bp_pack_value (&bp
, fn
->after_inlining
, 1);
638 bp_pack_value (&bp
, fn
->stdarg
, 1);
639 bp_pack_value (&bp
, fn
->has_nonlocal_label
, 1);
640 bp_pack_value (&bp
, fn
->calls_alloca
, 1);
641 bp_pack_value (&bp
, fn
->calls_setjmp
, 1);
642 bp_pack_value (&bp
, fn
->va_list_fpr_size
, 8);
643 bp_pack_value (&bp
, fn
->va_list_gpr_size
, 8);
644 lto_output_bitpack (&bp
);
646 /* Output the function start and end loci. */
647 lto_output_location (ob
, fn
->function_start_locus
);
648 lto_output_location (ob
, fn
->function_end_locus
);
650 /* Output current IL state of the function. */
651 output_uleb128 (ob
, fn
->curr_properties
);
653 /* Output the static chain and non-local goto save area. */
654 lto_output_tree_ref (ob
, fn
->static_chain_decl
);
655 lto_output_tree_ref (ob
, fn
->nonlocal_goto_save_area
);
657 /* Output all the local variables in the function. */
658 output_sleb128 (ob
, VEC_length (tree
, fn
->local_decls
));
659 FOR_EACH_VEC_ELT (tree
, fn
->local_decls
, i
, t
)
660 lto_output_tree_ref (ob
, t
);
662 /* Output the head of the arguments list. */
663 lto_output_tree_ref (ob
, DECL_ARGUMENTS (function
));
665 /* Output all the SSA names used in the function. */
666 output_ssa_names (ob
, fn
);
668 /* Output any exception handling regions. */
669 output_eh_regions (ob
, fn
);
671 /* Output DECL_INITIAL for the function, which contains the tree of
673 lto_output_tree (ob
, DECL_INITIAL (function
), true);
675 /* We will renumber the statements. The code that does this uses
676 the same ordering that we use for serializing them so we can use
677 the same code on the other end and not have to write out the
678 statement numbers. We do not assign UIDs to PHIs here because
679 virtual PHIs get re-computed on-the-fly which would make numbers
681 set_gimple_stmt_max_uid (cfun
, 0);
684 gimple_stmt_iterator gsi
;
685 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
687 gimple stmt
= gsi_stmt (gsi
);
688 gimple_set_uid (stmt
, inc_gimple_stmt_max_uid (cfun
));
692 /* Output the code for the function. */
693 FOR_ALL_BB_FN (bb
, fn
)
694 output_bb (ob
, bb
, fn
);
696 /* The terminator for this function. */
697 output_record_start (ob
, LTO_null
);
701 /* Create a section to hold the pickled output of this function. */
702 produce_asm (ob
, function
);
704 destroy_output_block (ob
);
706 current_function_decl
= NULL
;
711 /* Used to pass data to trivally_defined_alias callback. */
714 varpool_node_set vset
;
718 /* Return true if alias pair P belongs to the set of cgraph nodes in
719 SET. If P is a an alias for a VAR_DECL, it can always be emitted.
720 However, for FUNCTION_DECL aliases, we should only output the pair
721 if it belongs to a function whose cgraph node is in SET.
722 Otherwise, the LTRANS phase will get into trouble when finalizing
723 aliases because the alias will refer to a function not defined in
724 the file processed by LTRANS. */
727 trivally_defined_alias (tree decl ATTRIBUTE_UNUSED
,
728 tree target
, void *data
)
730 struct sets
*set
= (struct sets
*) data
;
731 struct cgraph_node
*fnode
= NULL
;
732 struct varpool_node
*vnode
= NULL
;
734 fnode
= cgraph_node_for_asm (target
);
736 return cgraph_node_in_set_p (fnode
, set
->set
);
737 vnode
= varpool_node_for_asm (target
);
738 return vnode
&& varpool_node_in_set_p (vnode
, set
->vset
);
741 /* Return true if alias pair P should be output in the current
742 partition contains cgrpah nodes SET and varpool nodes VSET.
743 DEFINED is set of all aliases whose targets are defined in
746 Normal aliases are output when they are defined, while WEAKREF
747 aliases are output when they are used. */
750 output_alias_pair_p (alias_pair
*p
, symbol_alias_set_t
*defined
,
751 cgraph_node_set set
, varpool_node_set vset
)
753 struct cgraph_node
*node
;
754 struct varpool_node
*vnode
;
756 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
758 if (TREE_CODE (p
->decl
) == VAR_DECL
)
760 vnode
= varpool_get_node (p
->decl
);
762 && referenced_from_this_partition_p (&vnode
->ref_list
, set
, vset
));
764 node
= cgraph_get_node (p
->decl
);
766 && (referenced_from_this_partition_p (&node
->ref_list
, set
, vset
)
767 || reachable_from_this_partition_p (node
, set
)));
770 return symbol_alias_set_contains (defined
, p
->decl
);
773 /* Output any unreferenced global symbol defined in SET, alias pairs
777 output_unreferenced_globals (cgraph_node_set set
, varpool_node_set vset
)
779 struct output_block
*ob
;
782 symbol_alias_set_t
*defined
;
788 ob
= create_output_block (LTO_section_static_initializer
);
789 ob
->cgraph_node
= NULL
;
791 clear_line_info (ob
);
793 /* Make string 0 be a NULL string. */
794 lto_output_1_stream (ob
->string_stream
, 0);
796 /* We really need to propagate in both directoins:
797 for normal aliases we propagate from first defined alias to
798 all aliases defined based on it. For weakrefs we propagate in
799 the oposite direction. */
800 defined
= propagate_aliases_backward (trivally_defined_alias
, &setdata
);
802 /* Emit the alias pairs for the nodes in SET. */
803 FOR_EACH_VEC_ELT (alias_pair
, alias_pairs
, i
, p
)
804 if (output_alias_pair_p (p
, defined
, set
, vset
))
806 lto_output_tree_ref (ob
, p
->decl
);
807 lto_output_tree_ref (ob
, p
->target
);
809 symbol_alias_set_destroy (defined
);
811 output_record_start (ob
, LTO_null
);
813 produce_asm (ob
, NULL
);
814 destroy_output_block (ob
);
818 /* Copy the function body of NODE without deserializing. */
821 copy_function (struct cgraph_node
*node
)
823 tree function
= node
->decl
;
824 struct lto_file_decl_data
*file_data
= node
->local
.lto_file_data
;
825 struct lto_output_stream
*output_stream
= XCNEW (struct lto_output_stream
);
828 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function
));
830 lto_get_section_name (LTO_section_function_body
, name
, NULL
);
832 struct lto_in_decl_state
*in_state
;
833 struct lto_out_decl_state
*out_state
= lto_get_out_decl_state ();
835 lto_begin_section (section_name
, !flag_wpa
);
838 /* We may have renamed the declaration, e.g., a static function. */
839 name
= lto_get_decl_name_mapping (file_data
, name
);
841 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
845 /* Do a bit copy of the function body. */
846 lto_output_data_stream (output_stream
, data
, len
);
847 lto_write_stream (output_stream
);
851 lto_get_function_in_decl_state (node
->local
.lto_file_data
, function
);
852 gcc_assert (in_state
);
854 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
856 size_t n
= in_state
->streams
[i
].size
;
857 tree
*trees
= in_state
->streams
[i
].trees
;
858 struct lto_tree_ref_encoder
*encoder
= &(out_state
->streams
[i
]);
860 /* The out state must have the same indices and the in state.
861 So just copy the vector. All the encoders in the in state
862 must be empty where we reach here. */
863 gcc_assert (lto_tree_ref_encoder_size (encoder
) == 0);
864 for (j
= 0; j
< n
; j
++)
865 VEC_safe_push (tree
, heap
, encoder
->trees
, trees
[j
]);
866 encoder
->next_index
= n
;
869 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
871 free (output_stream
);
876 /* Main entry point from the pass manager. */
879 lto_output (cgraph_node_set set
, varpool_node_set vset
)
881 struct cgraph_node
*node
;
882 struct lto_out_decl_state
*decl_state
;
883 #ifdef ENABLE_CHECKING
884 bitmap output
= lto_bitmap_alloc ();
887 lto_cgraph_encoder_t encoder
= lto_get_out_decl_state ()->cgraph_node_encoder
;
889 /* Initialize the streamer. */
890 lto_streamer_init ();
892 n_nodes
= lto_cgraph_encoder_size (encoder
);
893 /* Process only the functions with bodies. */
894 for (i
= 0; i
< n_nodes
; i
++)
896 node
= lto_cgraph_encoder_deref (encoder
, i
);
897 if (lto_cgraph_encoder_encode_body_p (encoder
, node
)
899 && !node
->thunk
.thunk_p
)
901 #ifdef ENABLE_CHECKING
902 gcc_assert (!bitmap_bit_p (output
, DECL_UID (node
->decl
)));
903 bitmap_set_bit (output
, DECL_UID (node
->decl
));
905 decl_state
= lto_new_out_decl_state ();
906 lto_push_out_decl_state (decl_state
);
907 if (gimple_has_body_p (node
->decl
))
908 output_function (node
);
910 copy_function (node
);
911 gcc_assert (lto_get_out_decl_state () == decl_state
);
912 lto_pop_out_decl_state ();
913 lto_record_function_out_decl_state (node
->decl
, decl_state
);
917 /* Emit the callgraph after emitting function bodies. This needs to
918 be done now to make sure that all the statements in every function
919 have been renumbered so that edges can be associated with call
920 statements using the statement UIDs. */
921 output_cgraph (set
, vset
);
923 #ifdef ENABLE_CHECKING
924 lto_bitmap_free (output
);
928 struct ipa_opt_pass_d pass_ipa_lto_gimple_out
=
932 "lto_gimple_out", /* name */
933 gate_lto_out
, /* gate */
937 0, /* static_pass_number */
938 TV_IPA_LTO_GIMPLE_OUT
, /* tv_id */
939 0, /* properties_required */
940 0, /* properties_provided */
941 0, /* properties_destroyed */
942 0, /* todo_flags_start */
943 0 /* todo_flags_finish */
945 NULL
, /* generate_summary */
946 lto_output
, /* write_summary */
947 NULL
, /* read_summary */
948 lto_output
, /* write_optimization_summary */
949 NULL
, /* read_optimization_summary */
950 NULL
, /* stmt_fixup */
952 NULL
, /* function_transform */
953 NULL
/* variable_transform */
957 /* Write each node in encoded by ENCODER to OB, as well as those reachable
958 from it and required for correct representation of its semantics.
959 Each node in ENCODER must be a global declaration or a type. A node
960 is written only once, even if it appears multiple times in the
961 vector. Certain transitively-reachable nodes, such as those
962 representing expressions, may be duplicated, but such nodes
963 must not appear in ENCODER itself. */
966 write_global_stream (struct output_block
*ob
,
967 struct lto_tree_ref_encoder
*encoder
)
971 const size_t size
= lto_tree_ref_encoder_size (encoder
);
973 for (index
= 0; index
< size
; index
++)
975 t
= lto_tree_ref_encoder_get_tree (encoder
, index
);
976 if (!lto_streamer_cache_lookup (ob
->writer_cache
, t
, NULL
))
977 lto_output_tree (ob
, t
, false);
982 /* Write a sequence of indices into the globals vector corresponding
983 to the trees in ENCODER. These are used by the reader to map the
984 indices used to refer to global entities within function bodies to
988 write_global_references (struct output_block
*ob
,
989 struct lto_output_stream
*ref_stream
,
990 struct lto_tree_ref_encoder
*encoder
)
994 const uint32_t size
= lto_tree_ref_encoder_size (encoder
);
996 /* Write size as 32-bit unsigned. */
997 lto_output_data_stream (ref_stream
, &size
, sizeof (int32_t));
999 for (index
= 0; index
< size
; index
++)
1003 t
= lto_tree_ref_encoder_get_tree (encoder
, index
);
1004 lto_streamer_cache_lookup (ob
->writer_cache
, t
, &slot_num
);
1005 gcc_assert (slot_num
!= (unsigned)-1);
1006 lto_output_data_stream (ref_stream
, &slot_num
, sizeof slot_num
);
1011 /* Write all the streams in an lto_out_decl_state STATE using
1012 output block OB and output stream OUT_STREAM. */
1015 lto_output_decl_state_streams (struct output_block
*ob
,
1016 struct lto_out_decl_state
*state
)
1020 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
1021 write_global_stream (ob
, &state
->streams
[i
]);
1025 /* Write all the references in an lto_out_decl_state STATE using
1026 output block OB and output stream OUT_STREAM. */
1029 lto_output_decl_state_refs (struct output_block
*ob
,
1030 struct lto_output_stream
*out_stream
,
1031 struct lto_out_decl_state
*state
)
1037 /* Write reference to FUNCTION_DECL. If there is not function,
1038 write reference to void_type_node. */
1039 decl
= (state
->fn_decl
) ? state
->fn_decl
: void_type_node
;
1040 lto_streamer_cache_lookup (ob
->writer_cache
, decl
, &ref
);
1041 gcc_assert (ref
!= (unsigned)-1);
1042 lto_output_data_stream (out_stream
, &ref
, sizeof (uint32_t));
1044 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
1045 write_global_references (ob
, out_stream
, &state
->streams
[i
]);
1049 /* Return the written size of STATE. */
1052 lto_out_decl_state_written_size (struct lto_out_decl_state
*state
)
1057 size
= sizeof (int32_t); /* fn_ref. */
1058 for (i
= 0; i
< LTO_N_DECL_STREAMS
; i
++)
1060 size
+= sizeof (int32_t); /* vector size. */
1061 size
+= (lto_tree_ref_encoder_size (&state
->streams
[i
])
1062 * sizeof (int32_t));
1068 /* Write symbol T into STREAM in CACHE. SEEN specifies symbols we wrote
1072 write_symbol (struct lto_streamer_cache_d
*cache
,
1073 struct lto_output_stream
*stream
,
1074 tree t
, struct pointer_set_t
*seen
, bool alias
)
1077 enum gcc_plugin_symbol_kind kind
;
1078 enum gcc_plugin_symbol_visibility visibility
;
1084 /* None of the following kinds of symbols are needed in the
1086 if (!TREE_PUBLIC (t
)
1087 || is_builtin_fn (t
)
1088 || DECL_ABSTRACT (t
)
1089 || TREE_CODE (t
) == RESULT_DECL
)
1092 gcc_assert (TREE_CODE (t
) == VAR_DECL
1093 || TREE_CODE (t
) == FUNCTION_DECL
);
1095 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
));
1097 /* This behaves like assemble_name_raw in varasm.c, performing the
1098 same name manipulations that ASM_OUTPUT_LABELREF does. */
1099 name
= IDENTIFIER_POINTER ((*targetm
.asm_out
.mangle_assembler_name
) (name
));
1101 if (pointer_set_contains (seen
, name
))
1103 pointer_set_insert (seen
, name
);
1105 lto_streamer_cache_lookup (cache
, t
, &slot_num
);
1106 gcc_assert (slot_num
!= (unsigned)-1);
1108 if (DECL_EXTERNAL (t
))
1111 kind
= GCCPK_WEAKUNDEF
;
1118 kind
= GCCPK_WEAKDEF
;
1119 else if (DECL_COMMON (t
))
1120 kind
= GCCPK_COMMON
;
1124 /* When something is defined, it should have node attached. */
1125 gcc_assert (alias
|| TREE_CODE (t
) != VAR_DECL
1126 || varpool_get_node (t
)->finalized
);
1127 gcc_assert (alias
|| TREE_CODE (t
) != FUNCTION_DECL
1128 || (cgraph_get_node (t
)
1129 && cgraph_get_node (t
)->analyzed
));
1132 /* Imitate what default_elf_asm_output_external do.
1133 When symbol is external, we need to output it with DEFAULT visibility
1134 when compiling with -fvisibility=default, while with HIDDEN visibility
1135 when symbol has attribute (visibility("hidden")) specified.
1136 targetm.binds_local_p check DECL_VISIBILITY_SPECIFIED and gets this
1139 if (DECL_EXTERNAL (t
)
1140 && !targetm
.binds_local_p (t
))
1141 visibility
= GCCPV_DEFAULT
;
1143 switch (DECL_VISIBILITY(t
))
1145 case VISIBILITY_DEFAULT
:
1146 visibility
= GCCPV_DEFAULT
;
1148 case VISIBILITY_PROTECTED
:
1149 visibility
= GCCPV_PROTECTED
;
1151 case VISIBILITY_HIDDEN
:
1152 visibility
= GCCPV_HIDDEN
;
1154 case VISIBILITY_INTERNAL
:
1155 visibility
= GCCPV_INTERNAL
;
1159 if (kind
== GCCPK_COMMON
1161 && TREE_CODE (DECL_SIZE (t
)) == INTEGER_CST
)
1163 size
= (HOST_BITS_PER_WIDE_INT
>= 64)
1164 ? (uint64_t) int_size_in_bytes (TREE_TYPE (t
))
1165 : (((uint64_t) TREE_INT_CST_HIGH (DECL_SIZE_UNIT (t
))) << 32)
1166 | TREE_INT_CST_LOW (DECL_SIZE_UNIT (t
));
1171 if (DECL_ONE_ONLY (t
))
1172 comdat
= IDENTIFIER_POINTER (DECL_COMDAT_GROUP (t
));
1176 lto_output_data_stream (stream
, name
, strlen (name
) + 1);
1177 lto_output_data_stream (stream
, comdat
, strlen (comdat
) + 1);
1178 c
= (unsigned char) kind
;
1179 lto_output_data_stream (stream
, &c
, 1);
1180 c
= (unsigned char) visibility
;
1181 lto_output_data_stream (stream
, &c
, 1);
1182 lto_output_data_stream (stream
, &size
, 8);
1183 lto_output_data_stream (stream
, &slot_num
, 4);
1187 /* Write an IL symbol table to OB.
1188 SET and VSET are cgraph/varpool node sets we are outputting. */
1191 produce_symtab (struct output_block
*ob
,
1192 cgraph_node_set set
, varpool_node_set vset
)
1194 struct lto_streamer_cache_d
*cache
= ob
->writer_cache
;
1195 char *section_name
= lto_get_section_name (LTO_section_symtab
, NULL
, NULL
);
1196 struct pointer_set_t
*seen
;
1197 struct cgraph_node
*node
;
1198 struct varpool_node
*vnode
;
1199 struct lto_output_stream stream
;
1200 lto_varpool_encoder_t varpool_encoder
= ob
->decl_state
->varpool_node_encoder
;
1201 lto_cgraph_encoder_t encoder
= ob
->decl_state
->cgraph_node_encoder
;
1204 struct sets setdata
;
1205 symbol_alias_set_t
*defined
;
1208 setdata
.vset
= vset
;
1210 lto_begin_section (section_name
, false);
1211 free (section_name
);
1213 seen
= pointer_set_create ();
1214 memset (&stream
, 0, sizeof (stream
));
1216 /* Write all functions.
1217 First write all defined functions and then write all used functions.
1218 This is done so only to handle duplicated symbols in cgraph. */
1219 for (i
= 0; i
< lto_cgraph_encoder_size (encoder
); i
++)
1221 node
= lto_cgraph_encoder_deref (encoder
, i
);
1222 if (DECL_EXTERNAL (node
->decl
))
1224 if (DECL_COMDAT (node
->decl
)
1225 && cgraph_comdat_can_be_unshared_p (node
))
1227 if ((node
->alias
&& !node
->thunk
.alias
) || node
->global
.inlined_to
)
1229 write_symbol (cache
, &stream
, node
->decl
, seen
, false);
1231 for (i
= 0; i
< lto_cgraph_encoder_size (encoder
); i
++)
1233 node
= lto_cgraph_encoder_deref (encoder
, i
);
1234 if (!DECL_EXTERNAL (node
->decl
))
1236 if (DECL_COMDAT (node
->decl
)
1237 && cgraph_comdat_can_be_unshared_p (node
))
1239 if ((node
->alias
&& !node
->thunk
.alias
) || node
->global
.inlined_to
)
1241 write_symbol (cache
, &stream
, node
->decl
, seen
, false);
1244 /* Write all variables. */
1245 for (i
= 0; i
< lto_varpool_encoder_size (varpool_encoder
); i
++)
1247 vnode
= lto_varpool_encoder_deref (varpool_encoder
, i
);
1248 if (DECL_EXTERNAL (vnode
->decl
))
1250 /* COMDAT virtual tables can be unshared. Do not declare them
1251 in the LTO symbol table to prevent linker from forcing them
1253 if (DECL_COMDAT (vnode
->decl
)
1254 && !vnode
->force_output
1256 && DECL_VIRTUAL_P (vnode
->decl
))
1258 if (vnode
->alias
&& !vnode
->alias_of
)
1260 write_symbol (cache
, &stream
, vnode
->decl
, seen
, false);
1262 for (i
= 0; i
< lto_varpool_encoder_size (varpool_encoder
); i
++)
1264 vnode
= lto_varpool_encoder_deref (varpool_encoder
, i
);
1265 if (!DECL_EXTERNAL (vnode
->decl
))
1267 if (DECL_COMDAT (vnode
->decl
)
1268 && !vnode
->force_output
1270 && DECL_VIRTUAL_P (vnode
->decl
))
1272 if (vnode
->alias
&& !vnode
->alias_of
)
1274 write_symbol (cache
, &stream
, vnode
->decl
, seen
, false);
1277 /* Write all aliases. */
1278 defined
= propagate_aliases_backward (trivally_defined_alias
, &setdata
);
1279 FOR_EACH_VEC_ELT (alias_pair
, alias_pairs
, i
, p
)
1280 if (output_alias_pair_p (p
, defined
, set
, vset
))
1281 write_symbol (cache
, &stream
, p
->decl
, seen
, true);
1282 symbol_alias_set_destroy (defined
);
1284 lto_write_stream (&stream
);
1285 pointer_set_destroy (seen
);
1291 /* This pass is run after all of the functions are serialized and all
1292 of the IPA passes have written their serialized forms. This pass
1293 causes the vector of all of the global decls and types used from
1294 this file to be written in to a section that can then be read in to
1295 recover these on other side. */
1298 produce_asm_for_decls (cgraph_node_set set
, varpool_node_set vset
)
1300 struct lto_out_decl_state
*out_state
;
1301 struct lto_out_decl_state
*fn_out_state
;
1302 struct lto_decl_header header
;
1304 struct output_block
*ob
;
1305 struct lto_output_stream
*header_stream
, *decl_state_stream
;
1306 unsigned idx
, num_fns
;
1307 size_t decl_state_size
;
1308 int32_t num_decl_states
;
1310 ob
= create_output_block (LTO_section_decls
);
1313 /* Write out unreferenced globals, alias pairs and labels. We defer
1314 doing this until now so that we can write out only what is
1316 output_unreferenced_globals (set
, vset
);
1318 memset (&header
, 0, sizeof (struct lto_decl_header
));
1320 section_name
= lto_get_section_name (LTO_section_decls
, NULL
, NULL
);
1321 lto_begin_section (section_name
, !flag_wpa
);
1322 free (section_name
);
1324 /* Make string 0 be a NULL string. */
1325 lto_output_1_stream (ob
->string_stream
, 0);
1327 /* Write the global symbols. */
1328 out_state
= lto_get_out_decl_state ();
1329 num_fns
= VEC_length (lto_out_decl_state_ptr
, lto_function_decl_states
);
1330 lto_output_decl_state_streams (ob
, out_state
);
1331 for (idx
= 0; idx
< num_fns
; idx
++)
1334 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1335 lto_output_decl_state_streams (ob
, fn_out_state
);
1338 header
.lto_header
.major_version
= LTO_major_version
;
1339 header
.lto_header
.minor_version
= LTO_minor_version
;
1340 header
.lto_header
.section_type
= LTO_section_decls
;
1342 /* Currently not used. This field would allow us to preallocate
1343 the globals vector, so that it need not be resized as it is extended. */
1344 header
.num_nodes
= -1;
1346 /* Compute the total size of all decl out states. */
1347 decl_state_size
= sizeof (int32_t);
1348 decl_state_size
+= lto_out_decl_state_written_size (out_state
);
1349 for (idx
= 0; idx
< num_fns
; idx
++)
1352 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1353 decl_state_size
+= lto_out_decl_state_written_size (fn_out_state
);
1355 header
.decl_state_size
= decl_state_size
;
1357 header
.main_size
= ob
->main_stream
->total_size
;
1358 header
.string_size
= ob
->string_stream
->total_size
;
1360 header_stream
= XCNEW (struct lto_output_stream
);
1361 lto_output_data_stream (header_stream
, &header
, sizeof header
);
1362 lto_write_stream (header_stream
);
1363 free (header_stream
);
1365 /* Write the main out-decl state, followed by out-decl states of
1367 decl_state_stream
= ((struct lto_output_stream
*)
1368 xcalloc (1, sizeof (struct lto_output_stream
)));
1369 num_decl_states
= num_fns
+ 1;
1370 lto_output_data_stream (decl_state_stream
, &num_decl_states
,
1371 sizeof (num_decl_states
));
1372 lto_output_decl_state_refs (ob
, decl_state_stream
, out_state
);
1373 for (idx
= 0; idx
< num_fns
; idx
++)
1376 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1377 lto_output_decl_state_refs (ob
, decl_state_stream
, fn_out_state
);
1379 lto_write_stream (decl_state_stream
);
1380 free(decl_state_stream
);
1382 lto_write_stream (ob
->main_stream
);
1383 lto_write_stream (ob
->string_stream
);
1387 /* Write the symbol table. It is used by linker to determine dependencies
1388 and thus we can skip it for WPA. */
1390 produce_symtab (ob
, set
, vset
);
1392 /* Write command line opts. */
1393 lto_write_options ();
1395 /* Deallocate memory and clean up. */
1396 for (idx
= 0; idx
< num_fns
; idx
++)
1399 VEC_index (lto_out_decl_state_ptr
, lto_function_decl_states
, idx
);
1400 lto_delete_out_decl_state (fn_out_state
);
1402 lto_cgraph_encoder_delete (ob
->decl_state
->cgraph_node_encoder
);
1403 lto_varpool_encoder_delete (ob
->decl_state
->varpool_node_encoder
);
1404 VEC_free (lto_out_decl_state_ptr
, heap
, lto_function_decl_states
);
1405 lto_function_decl_states
= NULL
;
1406 destroy_output_block (ob
);
1410 struct ipa_opt_pass_d pass_ipa_lto_finish_out
=
1414 "lto_decls_out", /* name */
1415 gate_lto_out
, /* gate */
1419 0, /* static_pass_number */
1420 TV_IPA_LTO_DECL_OUT
, /* tv_id */
1421 0, /* properties_required */
1422 0, /* properties_provided */
1423 0, /* properties_destroyed */
1424 0, /* todo_flags_start */
1425 0 /* todo_flags_finish */
1427 NULL
, /* generate_summary */
1428 produce_asm_for_decls
, /* write_summary */
1429 NULL
, /* read_summary */
1430 produce_asm_for_decls
, /* write_optimization_summary */
1431 NULL
, /* read_optimization_summary */
1432 NULL
, /* stmt_fixup */
1434 NULL
, /* function_transform */
1435 NULL
/* variable_transform */