1 /* Write and read the cgraph to the memory mapped representation of a
4 Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.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"
28 #include "stringpool.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-expr.h"
40 #include "langhooks.h"
43 #include "diagnostic-core.h"
46 #include "lto-streamer.h"
47 #include "data-streamer.h"
48 #include "tree-streamer.h"
50 #include "tree-pass.h"
53 #include "pass_manager.h"
54 #include "ipa-utils.h"
56 /* True when asm nodes has been output. */
57 bool asm_nodes_output
= false;
59 static void output_cgraph_opt_summary (void);
60 static void input_cgraph_opt_summary (vec
<symtab_node
*> nodes
);
62 /* Number of LDPR values known to GCC. */
63 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
65 /* All node orders are ofsetted by ORDER_BASE. */
66 static int order_base
;
68 /* Cgraph streaming is organized as set of record whose type
69 is indicated by a tag. */
72 /* Must leave 0 for the stopper. */
74 /* Cgraph node without body available. */
75 LTO_symtab_unavail_node
= 1,
76 /* Cgraph node with function body. */
77 LTO_symtab_analyzed_node
,
80 LTO_symtab_indirect_edge
,
85 /* Create a new symtab encoder.
86 if FOR_INPUT, the encoder allocate only datastructures needed
87 to read the symtab. */
90 lto_symtab_encoder_new (bool for_input
)
92 lto_symtab_encoder_t encoder
= XCNEW (struct lto_symtab_encoder_d
);
95 encoder
->map
= pointer_map_create ();
96 encoder
->nodes
.create (0);
101 /* Delete ENCODER and its components. */
104 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder
)
106 encoder
->nodes
.release ();
108 pointer_map_destroy (encoder
->map
);
113 /* Return the existing reference number of NODE in the symtab encoder in
114 output block OB. Assign a new reference if this is the first time
118 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder
,
126 lto_encoder_entry entry
= {node
, false, false, false};
128 ref
= encoder
->nodes
.length ();
129 encoder
->nodes
.safe_push (entry
);
133 slot
= pointer_map_contains (encoder
->map
, node
);
136 lto_encoder_entry entry
= {node
, false, false, false};
137 ref
= encoder
->nodes
.length ();
139 slot
= pointer_map_insert (encoder
->map
, node
);
140 *slot
= (void *) (intptr_t) (ref
+ 1);
141 encoder
->nodes
.safe_push (entry
);
144 ref
= (size_t) *slot
- 1;
149 /* Remove NODE from encoder. */
152 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder
,
155 void **slot
, **last_slot
;
157 lto_encoder_entry last_node
;
159 slot
= pointer_map_contains (encoder
->map
, node
);
160 if (slot
== NULL
|| !*slot
)
163 index
= (size_t) *slot
- 1;
164 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
166 /* Remove from vector. We do this by swapping node with the last element
168 last_node
= encoder
->nodes
.pop ();
169 if (last_node
.node
!= node
)
171 last_slot
= pointer_map_contains (encoder
->map
, last_node
.node
);
172 gcc_checking_assert (last_slot
&& *last_slot
);
173 *last_slot
= (void *)(size_t) (index
+ 1);
175 /* Move the last element to the original spot of NODE. */
176 encoder
->nodes
[index
] = last_node
;
179 /* Remove element from hash table. */
185 /* Return TRUE if we should encode initializer of NODE (if any). */
188 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder
,
189 struct cgraph_node
*node
)
191 int index
= lto_symtab_encoder_lookup (encoder
, node
);
192 return encoder
->nodes
[index
].body
;
195 /* Return TRUE if we should encode body of NODE (if any). */
198 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder
,
199 struct cgraph_node
*node
)
201 int index
= lto_symtab_encoder_encode (encoder
, node
);
202 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
203 encoder
->nodes
[index
].body
= true;
206 /* Return TRUE if we should encode initializer of NODE (if any). */
209 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder
,
212 int index
= lto_symtab_encoder_lookup (encoder
, node
);
213 if (index
== LCC_NOT_FOUND
)
215 return encoder
->nodes
[index
].initializer
;
218 /* Return TRUE if we should encode initializer of NODE (if any). */
221 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder
,
224 int index
= lto_symtab_encoder_lookup (encoder
, node
);
225 encoder
->nodes
[index
].initializer
= true;
228 /* Return TRUE if we should encode initializer of NODE (if any). */
231 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder
,
234 int index
= lto_symtab_encoder_lookup (encoder
, node
);
235 if (index
== LCC_NOT_FOUND
)
237 return encoder
->nodes
[index
].in_partition
;
240 /* Return TRUE if we should encode body of NODE (if any). */
243 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder
,
246 int index
= lto_symtab_encoder_encode (encoder
, node
);
247 encoder
->nodes
[index
].in_partition
= true;
250 /* Output the cgraph EDGE to OB using ENCODER. */
253 lto_output_edge (struct lto_simple_output_block
*ob
, struct cgraph_edge
*edge
,
254 lto_symtab_encoder_t encoder
)
260 if (edge
->indirect_unknown_callee
)
261 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
262 LTO_symtab_indirect_edge
);
264 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
267 ref
= lto_symtab_encoder_lookup (encoder
, edge
->caller
);
268 gcc_assert (ref
!= LCC_NOT_FOUND
);
269 streamer_write_hwi_stream (ob
->main_stream
, ref
);
271 if (!edge
->indirect_unknown_callee
)
273 ref
= lto_symtab_encoder_lookup (encoder
, edge
->callee
);
274 gcc_assert (ref
!= LCC_NOT_FOUND
);
275 streamer_write_hwi_stream (ob
->main_stream
, ref
);
278 streamer_write_gcov_count_stream (ob
->main_stream
, edge
->count
);
280 bp
= bitpack_create (ob
->main_stream
);
281 uid
= (!gimple_has_body_p (edge
->caller
->decl
)
282 ? edge
->lto_stmt_uid
: gimple_uid (edge
->call_stmt
) + 1);
283 bp_pack_enum (&bp
, cgraph_inline_failed_t
,
284 CIF_N_REASONS
, edge
->inline_failed
);
285 bp_pack_var_len_unsigned (&bp
, uid
);
286 bp_pack_var_len_unsigned (&bp
, edge
->frequency
);
287 bp_pack_value (&bp
, edge
->indirect_inlining_edge
, 1);
288 bp_pack_value (&bp
, edge
->speculative
, 1);
289 bp_pack_value (&bp
, edge
->call_stmt_cannot_inline_p
, 1);
290 bp_pack_value (&bp
, edge
->can_throw_external
, 1);
291 if (edge
->indirect_unknown_callee
)
293 int flags
= edge
->indirect_info
->ecf_flags
;
294 bp_pack_value (&bp
, (flags
& ECF_CONST
) != 0, 1);
295 bp_pack_value (&bp
, (flags
& ECF_PURE
) != 0, 1);
296 bp_pack_value (&bp
, (flags
& ECF_NORETURN
) != 0, 1);
297 bp_pack_value (&bp
, (flags
& ECF_MALLOC
) != 0, 1);
298 bp_pack_value (&bp
, (flags
& ECF_NOTHROW
) != 0, 1);
299 bp_pack_value (&bp
, (flags
& ECF_RETURNS_TWICE
) != 0, 1);
300 /* Flags that should not appear on indirect calls. */
301 gcc_assert (!(flags
& (ECF_LOOPING_CONST_OR_PURE
307 streamer_write_bitpack (&bp
);
308 if (edge
->indirect_unknown_callee
)
310 streamer_write_hwi_stream (ob
->main_stream
,
311 edge
->indirect_info
->common_target_id
);
312 if (edge
->indirect_info
->common_target_id
)
313 streamer_write_hwi_stream
314 (ob
->main_stream
, edge
->indirect_info
->common_target_probability
);
318 /* Return if LIST contain references from other partitions. */
321 referenced_from_other_partition_p (struct ipa_ref_list
*list
, lto_symtab_encoder_t encoder
)
325 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
327 if (ref
->referring
->in_other_partition
328 || !lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
334 /* Return true when node is reachable from other partition. */
337 reachable_from_other_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
339 struct cgraph_edge
*e
;
340 if (!node
->definition
)
342 if (node
->global
.inlined_to
)
344 for (e
= node
->callers
; e
; e
= e
->next_caller
)
345 if (e
->caller
->in_other_partition
346 || !lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
351 /* Return if LIST contain references from other partitions. */
354 referenced_from_this_partition_p (struct ipa_ref_list
*list
,
355 lto_symtab_encoder_t encoder
)
359 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
360 if (lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
365 /* Return true when node is reachable from other partition. */
368 reachable_from_this_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
370 struct cgraph_edge
*e
;
371 for (e
= node
->callers
; e
; e
= e
->next_caller
)
372 if (lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
377 /* Output the cgraph NODE to OB. ENCODER is used to find the
378 reference number of NODE->inlined_to. SET is the set of nodes we
379 are writing to the current file. If NODE is not in SET, then NODE
380 is a boundary of a cgraph_node_set and we pretend NODE just has a
381 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
382 that have had their callgraph node written so far. This is used to
383 determine if NODE is a clone of a previously written node. */
386 lto_output_node (struct lto_simple_output_block
*ob
, struct cgraph_node
*node
,
387 lto_symtab_encoder_t encoder
)
393 bool in_other_partition
= false;
394 struct cgraph_node
*clone_of
, *ultimate_clone_of
;
395 ipa_opt_pass_d
*pass
;
402 boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
404 if (node
->analyzed
&& !boundary_p
)
405 tag
= LTO_symtab_analyzed_node
;
407 tag
= LTO_symtab_unavail_node
;
409 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
411 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
413 /* In WPA mode, we only output part of the call-graph. Also, we
414 fake cgraph node attributes. There are two cases that we care.
416 Boundary nodes: There are nodes that are not part of SET but are
417 called from within SET. We artificially make them look like
418 externally visible nodes with no function body.
420 Cherry-picked nodes: These are nodes we pulled from other
421 translation units into SET during IPA-inlining. We make them as
422 local static nodes to prevent clashes with other local statics. */
423 if (boundary_p
&& node
->analyzed
424 && symtab_get_symbol_partitioning_class (node
) == SYMBOL_PARTITION
)
426 /* Inline clones can not be part of boundary.
427 gcc_assert (!node->global.inlined_to);
429 FIXME: At the moment they can be, when partition contains an inline
430 clone that is clone of inline clone from outside partition. We can
431 reshape the clone tree and make other tree to be the root, but it
432 needs a bit extra work and will be promplty done by cgraph_remove_node
433 after reading back. */
434 in_other_partition
= 1;
437 clone_of
= node
->clone_of
;
439 && (ref
= lto_symtab_encoder_lookup (encoder
, clone_of
)) == LCC_NOT_FOUND
)
440 if (clone_of
->prev_sibling_clone
)
441 clone_of
= clone_of
->prev_sibling_clone
;
443 clone_of
= clone_of
->clone_of
;
445 /* See if body of the master function is output. If not, we are seeing only
446 an declaration and we do not need to pass down clone tree. */
447 ultimate_clone_of
= clone_of
;
448 while (ultimate_clone_of
&& ultimate_clone_of
->clone_of
)
449 ultimate_clone_of
= ultimate_clone_of
->clone_of
;
451 if (clone_of
&& !lto_symtab_encoder_encode_body_p (encoder
, ultimate_clone_of
))
454 if (tag
== LTO_symtab_analyzed_node
)
455 gcc_assert (clone_of
|| !node
->clone_of
);
457 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
459 streamer_write_hwi_stream (ob
->main_stream
, ref
);
462 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
463 streamer_write_gcov_count_stream (ob
->main_stream
, node
->count
);
464 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
466 streamer_write_hwi_stream (ob
->main_stream
,
467 node
->ipa_transforms_to_apply
.length ());
468 FOR_EACH_VEC_ELT (node
->ipa_transforms_to_apply
, i
, pass
)
469 streamer_write_hwi_stream (ob
->main_stream
, pass
->static_pass_number
);
471 if (tag
== LTO_symtab_analyzed_node
)
473 if (node
->global
.inlined_to
)
475 ref
= lto_symtab_encoder_lookup (encoder
, node
->global
.inlined_to
);
476 gcc_assert (ref
!= LCC_NOT_FOUND
);
481 streamer_write_hwi_stream (ob
->main_stream
, ref
);
484 group
= node
->get_comdat_group ();
486 comdat
= IDENTIFIER_POINTER (group
);
489 lto_output_data_stream (ob
->main_stream
, comdat
, strlen (comdat
) + 1);
493 if (node
->same_comdat_group
&& !boundary_p
)
495 ref
= lto_symtab_encoder_lookup (encoder
,
496 node
->same_comdat_group
);
497 gcc_assert (ref
!= LCC_NOT_FOUND
);
501 streamer_write_hwi_stream (ob
->main_stream
, ref
);
504 section
= node
->get_section ();
508 streamer_write_hwi_stream (ob
->main_stream
, node
->tp_first_run
);
510 bp
= bitpack_create (ob
->main_stream
);
511 bp_pack_value (&bp
, node
->local
.local
, 1);
512 bp_pack_value (&bp
, node
->externally_visible
, 1);
513 bp_pack_value (&bp
, node
->definition
, 1);
514 bp_pack_value (&bp
, node
->local
.versionable
, 1);
515 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
516 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
517 bp_pack_value (&bp
, node
->force_output
, 1);
518 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
519 bp_pack_value (&bp
, node
->unique_name
, 1);
520 bp_pack_value (&bp
, node
->body_removed
, 1);
521 bp_pack_value (&bp
, node
->implicit_section
, 1);
522 bp_pack_value (&bp
, node
->address_taken
, 1);
523 bp_pack_value (&bp
, tag
== LTO_symtab_analyzed_node
524 && symtab_get_symbol_partitioning_class (node
) == SYMBOL_PARTITION
525 && (reachable_from_other_partition_p (node
, encoder
)
526 || referenced_from_other_partition_p (&node
->ref_list
,
528 bp_pack_value (&bp
, node
->lowered
, 1);
529 bp_pack_value (&bp
, in_other_partition
, 1);
530 /* Real aliases in a boundary become non-aliases. However we still stream
531 alias info on weakrefs.
532 TODO: We lose a bit of information here - when we know that variable is
533 defined in other unit, we may use the info on aliases to resolve
534 symbol1 != symbol2 type tests that we can do only for locally defined objects
536 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
537 bp_pack_value (&bp
, alias_p
, 1);
538 bp_pack_value (&bp
, node
->weakref
, 1);
539 bp_pack_value (&bp
, node
->frequency
, 2);
540 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
541 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
542 bp_pack_value (&bp
, node
->tm_clone
, 1);
543 bp_pack_value (&bp
, node
->calls_comdat_local
, 1);
544 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
545 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
546 LDPR_NUM_KNOWN
, node
->resolution
);
547 streamer_write_bitpack (&bp
);
548 lto_output_data_stream (ob
->main_stream
, section
, strlen (section
) + 1);
550 if (node
->thunk
.thunk_p
&& !boundary_p
)
552 streamer_write_uhwi_stream
554 1 + (node
->thunk
.this_adjusting
!= 0) * 2
555 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
556 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
557 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
559 streamer_write_hwi_stream (ob
->main_stream
, node
->profile_id
);
562 /* Output the varpool NODE to OB.
563 If NODE is not in SET, then NODE is a boundary. */
566 lto_output_varpool_node (struct lto_simple_output_block
*ob
, varpool_node
*node
,
567 lto_symtab_encoder_t encoder
)
569 bool boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
577 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
578 LTO_symtab_variable
);
579 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
580 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
581 bp
= bitpack_create (ob
->main_stream
);
582 bp_pack_value (&bp
, node
->externally_visible
, 1);
583 bp_pack_value (&bp
, node
->force_output
, 1);
584 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
585 bp_pack_value (&bp
, node
->unique_name
, 1);
586 bp_pack_value (&bp
, node
->body_removed
, 1);
587 bp_pack_value (&bp
, node
->implicit_section
, 1);
588 bp_pack_value (&bp
, node
->writeonly
, 1);
589 bp_pack_value (&bp
, node
->definition
, 1);
590 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
591 bp_pack_value (&bp
, alias_p
, 1);
592 bp_pack_value (&bp
, node
->weakref
, 1);
593 bp_pack_value (&bp
, node
->analyzed
&& !boundary_p
, 1);
594 gcc_assert (node
->definition
|| !node
->analyzed
);
595 /* Constant pool initializers can be de-unified into individual ltrans units.
596 FIXME: Alternatively at -Os we may want to avoid generating for them the local
597 labels and share them across LTRANS partitions. */
598 if (symtab_get_symbol_partitioning_class (node
) != SYMBOL_PARTITION
)
600 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
601 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
605 bp_pack_value (&bp
, node
->definition
606 && referenced_from_other_partition_p (&node
->ref_list
,
608 bp_pack_value (&bp
, node
->analyzed
609 && boundary_p
&& !DECL_EXTERNAL (node
->decl
), 1);
610 /* in_other_partition. */
612 streamer_write_bitpack (&bp
);
614 group
= node
->get_comdat_group ();
616 comdat
= IDENTIFIER_POINTER (group
);
619 lto_output_data_stream (ob
->main_stream
, comdat
, strlen (comdat
) + 1);
623 if (node
->same_comdat_group
&& !boundary_p
)
625 ref
= lto_symtab_encoder_lookup (encoder
,
626 node
->same_comdat_group
);
627 gcc_assert (ref
!= LCC_NOT_FOUND
);
631 streamer_write_hwi_stream (ob
->main_stream
, ref
);
634 section
= node
->get_section ();
637 lto_output_data_stream (ob
->main_stream
, section
, strlen (section
) + 1);
639 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
640 LDPR_NUM_KNOWN
, node
->resolution
);
643 /* Output the varpool NODE to OB.
644 If NODE is not in SET, then NODE is a boundary. */
647 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
648 lto_symtab_encoder_t encoder
)
652 int uid
= ref
->lto_stmt_uid
;
653 struct cgraph_node
*node
;
655 bp
= bitpack_create (ob
->main_stream
);
656 bp_pack_value (&bp
, ref
->use
, 2);
657 bp_pack_value (&bp
, ref
->speculative
, 1);
658 streamer_write_bitpack (&bp
);
659 nref
= lto_symtab_encoder_lookup (encoder
, ref
->referred
);
660 gcc_assert (nref
!= LCC_NOT_FOUND
);
661 streamer_write_hwi_stream (ob
->main_stream
, nref
);
663 node
= dyn_cast
<cgraph_node
*> (ref
->referring
);
667 uid
= gimple_uid (ref
->stmt
) + 1;
668 streamer_write_hwi_stream (ob
->main_stream
, uid
);
672 /* Stream out profile_summary to OB. */
675 output_profile_summary (struct lto_simple_output_block
*ob
)
682 /* We do not output num and run_max, they are not used by
683 GCC profile feedback and they are difficult to merge from multiple
685 gcc_assert (profile_info
->runs
);
686 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
687 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_max
);
689 /* sum_all is needed for computing the working set with the
691 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_all
);
693 /* Create and output a bitpack of non-zero histogram entries indices. */
694 bp
= bitpack_create (ob
->main_stream
);
695 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
696 bp_pack_value (&bp
, profile_info
->histogram
[h_ix
].num_counters
> 0, 1);
697 streamer_write_bitpack (&bp
);
698 /* Now stream out only those non-zero entries. */
699 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
701 if (!profile_info
->histogram
[h_ix
].num_counters
)
703 streamer_write_gcov_count_stream (ob
->main_stream
,
704 profile_info
->histogram
[h_ix
].num_counters
);
705 streamer_write_gcov_count_stream (ob
->main_stream
,
706 profile_info
->histogram
[h_ix
].min_value
);
707 streamer_write_gcov_count_stream (ob
->main_stream
,
708 profile_info
->histogram
[h_ix
].cum_value
);
710 /* IPA-profile computes hot bb threshold based on cumulated
711 whole program profile. We need to stream it down to ltrans. */
713 streamer_write_gcov_count_stream (ob
->main_stream
,
714 get_hot_bb_threshold ());
717 streamer_write_uhwi_stream (ob
->main_stream
, 0);
720 /* Output all callees or indirect outgoing edges. EDGE must be the first such
724 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
725 struct lto_simple_output_block
*ob
,
726 lto_symtab_encoder_t encoder
)
731 /* Output edges in backward direction, so the reconstructed callgraph match
732 and it is easy to associate call sites in the IPA pass summaries. */
733 while (edge
->next_callee
)
734 edge
= edge
->next_callee
;
735 for (; edge
; edge
= edge
->prev_callee
)
736 lto_output_edge (ob
, edge
, encoder
);
739 /* Output the part of the cgraph in SET. */
742 output_refs (lto_symtab_encoder_t encoder
)
744 lto_symtab_encoder_iterator lsei
;
745 struct lto_simple_output_block
*ob
;
750 ob
= lto_create_simple_output_block (LTO_section_refs
);
752 for (lsei
= lsei_start_in_partition (encoder
); !lsei_end_p (lsei
);
753 lsei_next_in_partition (&lsei
))
755 symtab_node
*node
= lsei_node (lsei
);
757 count
= ipa_ref_list_nreferences (&node
->ref_list
);
760 streamer_write_gcov_count_stream (ob
->main_stream
, count
);
761 streamer_write_uhwi_stream (ob
->main_stream
,
762 lto_symtab_encoder_lookup (encoder
, node
));
763 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
,
765 lto_output_ref (ob
, ref
, encoder
);
769 streamer_write_uhwi_stream (ob
->main_stream
, 0);
771 lto_destroy_simple_output_block (ob
);
774 /* Add NODE into encoder as well as nodes it is cloned from.
775 Do it in a way so clones appear first. */
778 add_node_to (lto_symtab_encoder_t encoder
, struct cgraph_node
*node
,
782 add_node_to (encoder
, node
->clone_of
, include_body
);
783 else if (include_body
)
784 lto_set_symtab_encoder_encode_body (encoder
, node
);
785 lto_symtab_encoder_encode (encoder
, node
);
788 /* Add all references in LIST to encoders. */
791 add_references (lto_symtab_encoder_t encoder
,
792 struct ipa_ref_list
*list
)
796 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
797 if (is_a
<cgraph_node
*> (ref
->referred
))
798 add_node_to (encoder
, ipa_ref_node (ref
), false);
800 lto_symtab_encoder_encode (encoder
, ref
->referred
);
803 /* Find all symbols we want to stream into given partition and insert them
806 The function actually replaces IN_ENCODER by new one. The reason is that
807 streaming code needs clone's origin to be streamed before clone. This
808 means that we need to insert the nodes in specific order. This order is
809 ignored by the partitioning logic earlier. */
812 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder
)
814 struct cgraph_edge
*edge
;
816 lto_symtab_encoder_t encoder
;
817 lto_symtab_encoder_iterator lsei
;
818 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
820 encoder
= lto_symtab_encoder_new (false);
822 /* Go over all entries in the IN_ENCODER and duplicate them to
823 ENCODER. At the same time insert masters of clones so
824 every master appears before clone. */
825 for (lsei
= lsei_start_function_in_partition (in_encoder
);
826 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
828 struct cgraph_node
*node
= lsei_cgraph_node (lsei
);
829 add_node_to (encoder
, node
, true);
830 lto_set_symtab_encoder_in_partition (encoder
, node
);
831 add_references (encoder
, &node
->ref_list
);
832 /* For proper debug info, we need to ship the origins, too. */
833 if (DECL_ABSTRACT_ORIGIN (node
->decl
))
835 struct cgraph_node
*origin_node
836 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node
->decl
));
837 add_node_to (encoder
, origin_node
, true);
840 for (lsei
= lsei_start_variable_in_partition (in_encoder
);
841 !lsei_end_p (lsei
); lsei_next_variable_in_partition (&lsei
))
843 varpool_node
*vnode
= lsei_varpool_node (lsei
);
845 lto_set_symtab_encoder_in_partition (encoder
, vnode
);
846 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
847 add_references (encoder
, &vnode
->ref_list
);
848 /* For proper debug info, we need to ship the origins, too. */
849 if (DECL_ABSTRACT_ORIGIN (vnode
->decl
))
851 varpool_node
*origin_node
852 = varpool_get_node (DECL_ABSTRACT_ORIGIN (vnode
->decl
));
853 lto_set_symtab_encoder_in_partition (encoder
, origin_node
);
856 /* Pickle in also the initializer of all referenced readonly variables
857 to help folding. Constant pool variables are not shared, so we must
859 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
861 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
862 if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
864 if (!lto_symtab_encoder_encode_initializer_p (encoder
,
866 && ctor_for_folding (vnode
->decl
) != error_mark_node
)
868 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
869 add_references (encoder
, &vnode
->ref_list
);
874 /* Go over all the nodes again to include callees that are not in
876 for (lsei
= lsei_start_function_in_partition (encoder
);
877 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
879 struct cgraph_node
*node
= lsei_cgraph_node (lsei
);
880 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
882 struct cgraph_node
*callee
= edge
->callee
;
883 if (!lto_symtab_encoder_in_partition_p (encoder
, callee
))
885 /* We should have moved all the inlines. */
886 gcc_assert (!callee
->global
.inlined_to
);
887 add_node_to (encoder
, callee
, false);
890 /* Add all possible targets for late devirtualization. */
891 if (flag_devirtualize
)
892 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
893 if (edge
->indirect_info
->polymorphic
)
898 vec
<cgraph_node
*>targets
899 = possible_polymorphic_call_targets
900 (edge
, &final
, &cache_token
);
901 if (!pointer_set_insert (reachable_call_targets
,
904 for (i
= 0; i
< targets
.length (); i
++)
906 struct cgraph_node
*callee
= targets
[i
];
908 /* Adding an external declarations into the unit serves
909 no purpose and just increases its boundary. */
910 if (callee
->definition
911 && !lto_symtab_encoder_in_partition_p
914 gcc_assert (!callee
->global
.inlined_to
);
915 add_node_to (encoder
, callee
, false);
921 lto_symtab_encoder_delete (in_encoder
);
922 pointer_set_destroy (reachable_call_targets
);
926 /* Output the part of the symtab in SET and VSET. */
931 struct cgraph_node
*node
;
932 struct lto_simple_output_block
*ob
;
933 lto_symtab_encoder_iterator lsei
;
935 lto_symtab_encoder_t encoder
;
938 output_cgraph_opt_summary ();
940 ob
= lto_create_simple_output_block (LTO_section_symtab_nodes
);
942 output_profile_summary (ob
);
944 /* An encoder for cgraph nodes should have been created by
945 ipa_write_summaries_1. */
946 gcc_assert (ob
->decl_state
->symtab_node_encoder
);
947 encoder
= ob
->decl_state
->symtab_node_encoder
;
949 /* Write out the nodes. We must first output a node and then its clones,
950 otherwise at a time reading back the node there would be nothing to clone
952 n_nodes
= lto_symtab_encoder_size (encoder
);
953 for (i
= 0; i
< n_nodes
; i
++)
955 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
956 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
957 lto_output_node (ob
, cnode
, encoder
);
959 lto_output_varpool_node (ob
, varpool (node
), encoder
);
963 /* Go over the nodes in SET again to write edges. */
964 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
965 lsei_next_function_in_partition (&lsei
))
967 node
= lsei_cgraph_node (lsei
);
968 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
969 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
972 streamer_write_uhwi_stream (ob
->main_stream
, 0);
974 lto_destroy_simple_output_block (ob
);
976 /* Emit toplevel asms.
977 When doing WPA we must output every asm just once. Since we do not partition asm
978 nodes at all, output them to first output. This is kind of hack, but should work
980 if (!asm_nodes_output
)
982 asm_nodes_output
= true;
983 lto_output_toplevel_asms ();
986 output_refs (encoder
);
989 /* Return identifier encoded in IB as a plain string. */
992 read_identifier (struct lto_input_block
*ib
)
994 unsigned int len
= strnlen (ib
->data
+ ib
->p
, ib
->len
- ib
->p
- 1);
997 if (ib
->data
[ib
->p
+ len
])
998 lto_section_overrun (ib
);
1004 id
= get_identifier (ib
->data
+ ib
->p
);
1009 /* Return string encoded in IB, NULL if string is empty. */
1012 read_string (struct lto_input_block
*ib
)
1014 unsigned int len
= strnlen (ib
->data
+ ib
->p
, ib
->len
- ib
->p
- 1);
1017 if (ib
->data
[ib
->p
+ len
])
1018 lto_section_overrun (ib
);
1024 str
= ib
->data
+ ib
->p
;
1029 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1030 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1031 NODE or to replace the values in it, for instance because the first
1032 time we saw it, the function body was not available but now it
1033 is. BP is a bitpack with all the bitflags for NODE read from the
1037 input_overwrite_node (struct lto_file_decl_data
*file_data
,
1038 struct cgraph_node
*node
,
1039 enum LTO_symtab_tags tag
,
1040 struct bitpack_d
*bp
)
1042 node
->aux
= (void *) tag
;
1043 node
->lto_file_data
= file_data
;
1045 node
->local
.local
= bp_unpack_value (bp
, 1);
1046 node
->externally_visible
= bp_unpack_value (bp
, 1);
1047 node
->definition
= bp_unpack_value (bp
, 1);
1048 node
->local
.versionable
= bp_unpack_value (bp
, 1);
1049 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
1050 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
1051 node
->force_output
= bp_unpack_value (bp
, 1);
1052 node
->forced_by_abi
= bp_unpack_value (bp
, 1);
1053 node
->unique_name
= bp_unpack_value (bp
, 1);
1054 node
->body_removed
= bp_unpack_value (bp
, 1);
1055 node
->implicit_section
= bp_unpack_value (bp
, 1);
1056 node
->address_taken
= bp_unpack_value (bp
, 1);
1057 node
->used_from_other_partition
= bp_unpack_value (bp
, 1);
1058 node
->lowered
= bp_unpack_value (bp
, 1);
1059 node
->analyzed
= tag
== LTO_symtab_analyzed_node
;
1060 node
->in_other_partition
= bp_unpack_value (bp
, 1);
1061 if (node
->in_other_partition
1062 /* Avoid updating decl when we are seeing just inline clone.
1063 When inlining function that has functions already inlined into it,
1064 we produce clones of inline clones.
1066 WPA partitioning might put each clone into different unit and
1067 we might end up streaming inline clone from other partition
1068 to support clone we are interested in. */
1070 || node
->clone_of
->decl
!= node
->decl
))
1072 DECL_EXTERNAL (node
->decl
) = 1;
1073 TREE_STATIC (node
->decl
) = 0;
1075 node
->alias
= bp_unpack_value (bp
, 1);
1076 node
->weakref
= bp_unpack_value (bp
, 1);
1077 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
1078 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
1079 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
1080 node
->tm_clone
= bp_unpack_value (bp
, 1);
1081 node
->calls_comdat_local
= bp_unpack_value (bp
, 1);
1082 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
1083 node
->resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
1085 gcc_assert (flag_ltrans
1086 || (!node
->in_other_partition
1087 && !node
->used_from_other_partition
));
1090 /* Return string alias is alias of. */
1093 get_alias_symbol (tree decl
)
1095 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1096 return get_identifier (TREE_STRING_POINTER
1097 (TREE_VALUE (TREE_VALUE (alias
))));
1100 /* Read a node from input_block IB. TAG is the node's tag just read.
1101 Return the node read or overwriten. */
1103 static struct cgraph_node
*
1104 input_node (struct lto_file_decl_data
*file_data
,
1105 struct lto_input_block
*ib
,
1106 enum LTO_symtab_tags tag
,
1107 vec
<symtab_node
*> nodes
)
1109 gcc::pass_manager
*passes
= g
->get_passes ();
1111 struct cgraph_node
*node
;
1112 struct bitpack_d bp
;
1113 unsigned decl_index
;
1114 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
1119 const char *section
;
1121 order
= streamer_read_hwi (ib
) + order_base
;
1122 clone_ref
= streamer_read_hwi (ib
);
1124 decl_index
= streamer_read_uhwi (ib
);
1125 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
1127 if (clone_ref
!= LCC_NOT_FOUND
)
1129 node
= cgraph_clone_node (cgraph (nodes
[clone_ref
]), fn_decl
,
1130 0, CGRAPH_FREQ_BASE
, false,
1131 vNULL
, false, NULL
, NULL
);
1135 /* Declaration of functions can be already merged with a declaration
1136 from other input file. We keep cgraph unmerged until after streaming
1137 of ipa passes is done. Alays forcingly create a fresh node. */
1138 node
= cgraph_create_empty_node ();
1139 node
->decl
= fn_decl
;
1140 symtab_register_node (node
);
1143 node
->order
= order
;
1144 if (order
>= symtab_order
)
1145 symtab_order
= order
+ 1;
1147 node
->count
= streamer_read_gcov_count (ib
);
1148 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1150 count
= streamer_read_hwi (ib
);
1151 node
->ipa_transforms_to_apply
= vNULL
;
1152 for (i
= 0; i
< count
; i
++)
1155 int pid
= streamer_read_hwi (ib
);
1157 gcc_assert (pid
< passes
->passes_by_id_size
);
1158 pass
= passes
->passes_by_id
[pid
];
1159 node
->ipa_transforms_to_apply
.safe_push ((ipa_opt_pass_d
*) pass
);
1162 if (tag
== LTO_symtab_analyzed_node
)
1163 ref
= streamer_read_hwi (ib
);
1165 group
= read_identifier (ib
);
1167 ref2
= streamer_read_hwi (ib
);
1169 /* Make sure that we have not read this node before. Nodes that
1170 have already been read will have their tag stored in the 'aux'
1171 field. Since built-in functions can be referenced in multiple
1172 functions, they are expected to be read more than once. */
1173 if (node
->aux
&& !DECL_BUILT_IN (node
->decl
))
1174 internal_error ("bytecode stream: found multiple instances of cgraph "
1175 "node with uid %d", node
->uid
);
1177 node
->tp_first_run
= streamer_read_uhwi (ib
);
1179 bp
= streamer_read_bitpack (ib
);
1181 input_overwrite_node (file_data
, node
, tag
, &bp
);
1183 /* Store a reference for now, and fix up later to be a pointer. */
1184 node
->global
.inlined_to
= (cgraph_node_ptr
) (intptr_t) ref
;
1188 node
->set_comdat_group (group
);
1189 /* Store a reference for now, and fix up later to be a pointer. */
1190 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref2
;
1193 node
->same_comdat_group
= (symtab_node
*) (intptr_t) LCC_NOT_FOUND
;
1194 section
= read_string (ib
);
1196 node
->set_section_for_node (section
);
1198 if (node
->thunk
.thunk_p
)
1200 int type
= streamer_read_uhwi (ib
);
1201 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1202 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1204 node
->thunk
.fixed_offset
= fixed_offset
;
1205 node
->thunk
.this_adjusting
= (type
& 2);
1206 node
->thunk
.virtual_value
= virtual_value
;
1207 node
->thunk
.virtual_offset_p
= (type
& 4);
1209 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1210 node
->alias_target
= get_alias_symbol (node
->decl
);
1211 node
->profile_id
= streamer_read_hwi (ib
);
1215 /* Read a node from input_block IB. TAG is the node's tag just read.
1216 Return the node read or overwriten. */
1218 static varpool_node
*
1219 input_varpool_node (struct lto_file_decl_data
*file_data
,
1220 struct lto_input_block
*ib
)
1225 struct bitpack_d bp
;
1226 int ref
= LCC_NOT_FOUND
;
1229 const char *section
;
1231 order
= streamer_read_hwi (ib
) + order_base
;
1232 decl_index
= streamer_read_uhwi (ib
);
1233 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1235 /* Declaration of functions can be already merged with a declaration
1236 from other input file. We keep cgraph unmerged until after streaming
1237 of ipa passes is done. Alays forcingly create a fresh node. */
1238 node
= varpool_create_empty_node ();
1239 node
->decl
= var_decl
;
1240 symtab_register_node (node
);
1242 node
->order
= order
;
1243 if (order
>= symtab_order
)
1244 symtab_order
= order
+ 1;
1245 node
->lto_file_data
= file_data
;
1247 bp
= streamer_read_bitpack (ib
);
1248 node
->externally_visible
= bp_unpack_value (&bp
, 1);
1249 node
->force_output
= bp_unpack_value (&bp
, 1);
1250 node
->forced_by_abi
= bp_unpack_value (&bp
, 1);
1251 node
->unique_name
= bp_unpack_value (&bp
, 1);
1252 node
->body_removed
= bp_unpack_value (&bp
, 1);
1253 node
->implicit_section
= bp_unpack_value (&bp
, 1);
1254 node
->writeonly
= bp_unpack_value (&bp
, 1);
1255 node
->definition
= bp_unpack_value (&bp
, 1);
1256 node
->alias
= bp_unpack_value (&bp
, 1);
1257 node
->weakref
= bp_unpack_value (&bp
, 1);
1258 node
->analyzed
= bp_unpack_value (&bp
, 1);
1259 node
->used_from_other_partition
= bp_unpack_value (&bp
, 1);
1260 node
->in_other_partition
= bp_unpack_value (&bp
, 1);
1261 if (node
->in_other_partition
)
1263 DECL_EXTERNAL (node
->decl
) = 1;
1264 TREE_STATIC (node
->decl
) = 0;
1266 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1267 node
->alias_target
= get_alias_symbol (node
->decl
);
1268 group
= read_identifier (ib
);
1271 node
->set_comdat_group (group
);
1272 ref
= streamer_read_hwi (ib
);
1273 /* Store a reference for now, and fix up later to be a pointer. */
1274 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref
;
1277 node
->same_comdat_group
= (symtab_node
*) (intptr_t) LCC_NOT_FOUND
;
1278 section
= read_string (ib
);
1280 node
->set_section_for_node (section
);
1281 node
->resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1283 gcc_assert (flag_ltrans
1284 || (!node
->in_other_partition
1285 && !node
->used_from_other_partition
));
1290 /* Read a node from input_block IB. TAG is the node's tag just read.
1291 Return the node read or overwriten. */
1294 input_ref (struct lto_input_block
*ib
,
1295 symtab_node
*referring_node
,
1296 vec
<symtab_node
*> nodes
)
1298 symtab_node
*node
= NULL
;
1299 struct bitpack_d bp
;
1300 enum ipa_ref_use use
;
1302 struct ipa_ref
*ref
;
1304 bp
= streamer_read_bitpack (ib
);
1305 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1306 speculative
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 1);
1307 node
= nodes
[streamer_read_hwi (ib
)];
1308 ref
= ipa_record_reference (referring_node
, node
, use
, NULL
);
1309 ref
->speculative
= speculative
;
1310 if (is_a
<cgraph_node
*> (referring_node
))
1311 ref
->lto_stmt_uid
= streamer_read_hwi (ib
);
1314 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1315 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1316 edge being read is indirect (in the sense that it has
1317 indirect_unknown_callee set). */
1320 input_edge (struct lto_input_block
*ib
, vec
<symtab_node
*> nodes
,
1323 struct cgraph_node
*caller
, *callee
;
1324 struct cgraph_edge
*edge
;
1325 unsigned int stmt_id
;
1328 cgraph_inline_failed_t inline_failed
;
1329 struct bitpack_d bp
;
1332 caller
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1333 if (caller
== NULL
|| caller
->decl
== NULL_TREE
)
1334 internal_error ("bytecode stream: no caller found while reading edge");
1338 callee
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1339 if (callee
== NULL
|| callee
->decl
== NULL_TREE
)
1340 internal_error ("bytecode stream: no callee found while reading edge");
1345 count
= streamer_read_gcov_count (ib
);
1347 bp
= streamer_read_bitpack (ib
);
1348 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_t
, CIF_N_REASONS
);
1349 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1350 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1353 edge
= cgraph_create_indirect_edge (caller
, NULL
, 0, count
, freq
);
1355 edge
= cgraph_create_edge (caller
, callee
, NULL
, count
, freq
);
1357 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1358 edge
->speculative
= bp_unpack_value (&bp
, 1);
1359 edge
->lto_stmt_uid
= stmt_id
;
1360 edge
->inline_failed
= inline_failed
;
1361 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1362 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1365 if (bp_unpack_value (&bp
, 1))
1366 ecf_flags
|= ECF_CONST
;
1367 if (bp_unpack_value (&bp
, 1))
1368 ecf_flags
|= ECF_PURE
;
1369 if (bp_unpack_value (&bp
, 1))
1370 ecf_flags
|= ECF_NORETURN
;
1371 if (bp_unpack_value (&bp
, 1))
1372 ecf_flags
|= ECF_MALLOC
;
1373 if (bp_unpack_value (&bp
, 1))
1374 ecf_flags
|= ECF_NOTHROW
;
1375 if (bp_unpack_value (&bp
, 1))
1376 ecf_flags
|= ECF_RETURNS_TWICE
;
1377 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1378 edge
->indirect_info
->common_target_id
= streamer_read_hwi (ib
);
1379 if (edge
->indirect_info
->common_target_id
)
1380 edge
->indirect_info
->common_target_probability
= streamer_read_hwi (ib
);
1385 /* Read a cgraph from IB using the info in FILE_DATA. */
1387 static vec
<symtab_node
*>
1388 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1389 struct lto_input_block
*ib
)
1391 enum LTO_symtab_tags tag
;
1392 vec
<symtab_node
*> nodes
= vNULL
;
1396 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1397 order_base
= symtab_order
;
1400 if (tag
== LTO_symtab_edge
)
1401 input_edge (ib
, nodes
, false);
1402 else if (tag
== LTO_symtab_indirect_edge
)
1403 input_edge (ib
, nodes
, true);
1404 else if (tag
== LTO_symtab_variable
)
1406 node
= input_varpool_node (file_data
, ib
);
1407 nodes
.safe_push (node
);
1408 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1412 node
= input_node (file_data
, ib
, tag
, nodes
);
1413 if (node
== NULL
|| node
->decl
== NULL_TREE
)
1414 internal_error ("bytecode stream: found empty cgraph node");
1415 nodes
.safe_push (node
);
1416 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1419 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1422 lto_input_toplevel_asms (file_data
, order_base
);
1424 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1425 #ifdef ENABLE_CHECKING
1426 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1427 gcc_assert (node
->aux
|| !is_a
<cgraph_node
*> (node
));
1429 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1432 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1434 ref
= (int) (intptr_t) cnode
->global
.inlined_to
;
1436 /* We share declaration of builtins, so we may read same node twice. */
1441 /* Fixup inlined_to from reference to pointer. */
1442 if (ref
!= LCC_NOT_FOUND
)
1443 cgraph (node
)->global
.inlined_to
= cgraph (nodes
[ref
]);
1445 cnode
->global
.inlined_to
= NULL
;
1448 ref
= (int) (intptr_t) node
->same_comdat_group
;
1450 /* Fixup same_comdat_group from reference to pointer. */
1451 if (ref
!= LCC_NOT_FOUND
)
1452 node
->same_comdat_group
= nodes
[ref
];
1454 node
->same_comdat_group
= NULL
;
1456 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1457 node
->aux
= is_a
<cgraph_node
*> (node
) ? (void *)1 : NULL
;
1461 /* Input ipa_refs. */
1464 input_refs (struct lto_input_block
*ib
,
1465 vec
<symtab_node
*> nodes
)
1472 count
= streamer_read_uhwi (ib
);
1475 idx
= streamer_read_uhwi (ib
);
1479 input_ref (ib
, node
, nodes
);
1486 static struct gcov_ctr_summary lto_gcov_summary
;
1488 /* Input profile_info from IB. */
1490 input_profile_summary (struct lto_input_block
*ib
,
1491 struct lto_file_decl_data
*file_data
)
1494 struct bitpack_d bp
;
1495 unsigned int runs
= streamer_read_uhwi (ib
);
1498 file_data
->profile_info
.runs
= runs
;
1499 file_data
->profile_info
.sum_max
= streamer_read_gcov_count (ib
);
1500 file_data
->profile_info
.sum_all
= streamer_read_gcov_count (ib
);
1502 memset (file_data
->profile_info
.histogram
, 0,
1503 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1504 /* Input the bitpack of non-zero histogram indices. */
1505 bp
= streamer_read_bitpack (ib
);
1506 /* Read in and unpack the full bitpack, flagging non-zero
1507 histogram entries by setting the num_counters non-zero. */
1508 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1510 file_data
->profile_info
.histogram
[h_ix
].num_counters
1511 = bp_unpack_value (&bp
, 1);
1513 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1515 if (!file_data
->profile_info
.histogram
[h_ix
].num_counters
)
1518 file_data
->profile_info
.histogram
[h_ix
].num_counters
1519 = streamer_read_gcov_count (ib
);
1520 file_data
->profile_info
.histogram
[h_ix
].min_value
1521 = streamer_read_gcov_count (ib
);
1522 file_data
->profile_info
.histogram
[h_ix
].cum_value
1523 = streamer_read_gcov_count (ib
);
1525 /* IPA-profile computes hot bb threshold based on cumulated
1526 whole program profile. We need to stream it down to ltrans. */
1528 set_hot_bb_threshold (streamer_read_gcov_count (ib
));
1533 /* Rescale profile summaries to the same number of runs in the whole unit. */
1536 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1538 struct lto_file_decl_data
*file_data
;
1539 unsigned int j
, h_ix
;
1540 gcov_unsigned_t max_runs
= 0;
1541 struct cgraph_node
*node
;
1542 struct cgraph_edge
*edge
;
1543 gcov_type saved_sum_all
= 0;
1544 gcov_ctr_summary
*saved_profile_info
= 0;
1545 int saved_scale
= 0;
1547 /* Find unit with maximal number of runs. If we ever get serious about
1548 roundoff errors, we might also consider computing smallest common
1550 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1551 if (max_runs
< file_data
->profile_info
.runs
)
1552 max_runs
= file_data
->profile_info
.runs
;
1557 /* Simple overflow check. We probably don't need to support that many train
1558 runs. Such a large value probably imply data corruption anyway. */
1559 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1561 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1562 INT_MAX
/ REG_BR_PROB_BASE
);
1566 profile_info
= <o_gcov_summary
;
1567 lto_gcov_summary
.runs
= max_runs
;
1568 lto_gcov_summary
.sum_max
= 0;
1569 memset (lto_gcov_summary
.histogram
, 0,
1570 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1572 /* Rescale all units to the maximal number of runs.
1573 sum_max can not be easily merged, as we have no idea what files come from
1574 the same run. We do not use the info anyway, so leave it 0. */
1575 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1576 if (file_data
->profile_info
.runs
)
1578 int scale
= GCOV_COMPUTE_SCALE (max_runs
,
1579 file_data
->profile_info
.runs
);
1580 lto_gcov_summary
.sum_max
1581 = MAX (lto_gcov_summary
.sum_max
,
1582 apply_scale (file_data
->profile_info
.sum_max
, scale
));
1583 lto_gcov_summary
.sum_all
1584 = MAX (lto_gcov_summary
.sum_all
,
1585 apply_scale (file_data
->profile_info
.sum_all
, scale
));
1586 /* Save a pointer to the profile_info with the largest
1587 scaled sum_all and the scale for use in merging the
1589 if (!saved_profile_info
1590 || lto_gcov_summary
.sum_all
> saved_sum_all
)
1592 saved_profile_info
= &file_data
->profile_info
;
1593 saved_sum_all
= lto_gcov_summary
.sum_all
;
1594 saved_scale
= scale
;
1598 gcc_assert (saved_profile_info
);
1600 /* Scale up the histogram from the profile that had the largest
1601 scaled sum_all above. */
1602 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1604 /* Scale up the min value as we did the corresponding sum_all
1605 above. Use that to find the new histogram index. */
1606 gcov_type scaled_min
1607 = apply_scale (saved_profile_info
->histogram
[h_ix
].min_value
,
1609 /* The new index may be shared with another scaled histogram entry,
1610 so we need to account for a non-zero histogram entry at new_ix. */
1611 unsigned new_ix
= gcov_histo_index (scaled_min
);
1612 lto_gcov_summary
.histogram
[new_ix
].min_value
1613 = (lto_gcov_summary
.histogram
[new_ix
].num_counters
1614 ? MIN (lto_gcov_summary
.histogram
[new_ix
].min_value
, scaled_min
)
1616 /* Some of the scaled counter values would ostensibly need to be placed
1617 into different (larger) histogram buckets, but we keep things simple
1618 here and place the scaled cumulative counter value in the bucket
1619 corresponding to the scaled minimum counter value. */
1620 lto_gcov_summary
.histogram
[new_ix
].cum_value
1621 += apply_scale (saved_profile_info
->histogram
[h_ix
].cum_value
,
1623 lto_gcov_summary
.histogram
[new_ix
].num_counters
1624 += saved_profile_info
->histogram
[h_ix
].num_counters
;
1627 /* Watch roundoff errors. */
1628 if (lto_gcov_summary
.sum_max
< max_runs
)
1629 lto_gcov_summary
.sum_max
= max_runs
;
1631 /* If merging already happent at WPA time, we are done. */
1635 /* Now compute count_materialization_scale of each node.
1636 During LTRANS we already have values of count_materialization_scale
1637 computed, so just update them. */
1638 FOR_EACH_FUNCTION (node
)
1639 if (node
->lto_file_data
1640 && node
->lto_file_data
->profile_info
.runs
)
1644 scale
= RDIV (node
->count_materialization_scale
* max_runs
,
1645 node
->lto_file_data
->profile_info
.runs
);
1646 node
->count_materialization_scale
= scale
;
1648 fatal_error ("Profile information in %s corrupted",
1649 file_data
->file_name
);
1651 if (scale
== REG_BR_PROB_BASE
)
1653 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1654 edge
->count
= apply_scale (edge
->count
, scale
);
1655 node
->count
= apply_scale (node
->count
, scale
);
1659 /* Input and merge the symtab from each of the .o files passed to
1665 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1666 struct lto_file_decl_data
*file_data
;
1668 struct cgraph_node
*node
;
1670 while ((file_data
= file_data_vec
[j
++]))
1674 struct lto_input_block
*ib
;
1675 vec
<symtab_node
*> nodes
;
1677 ib
= lto_create_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1680 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1681 input_profile_summary (ib
, file_data
);
1682 file_data
->symtab_node_encoder
= lto_symtab_encoder_new (true);
1683 nodes
= input_cgraph_1 (file_data
, ib
);
1684 lto_destroy_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1687 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1690 fatal_error ("cannot find LTO section refs in %s",
1691 file_data
->file_name
);
1692 input_refs (ib
, nodes
);
1693 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1696 input_cgraph_opt_summary (nodes
);
1700 merge_profile_summaries (file_data_vec
);
1701 get_working_sets ();
1704 /* Clear out the aux field that was used to store enough state to
1705 tell which nodes should be overwritten. */
1706 FOR_EACH_FUNCTION (node
)
1708 /* Some nodes may have been created by cgraph_node. This
1709 happens when the callgraph contains nested functions. If the
1710 node for the parent function was never emitted to the gimple
1711 file, cgraph_node will create a node for it when setting the
1712 context of the nested function. */
1713 if (node
->lto_file_data
)
1718 /* True when we need optimization summary for NODE. */
1721 output_cgraph_opt_summary_p (struct cgraph_node
*node
)
1723 return (node
->clone_of
1724 && (node
->clone
.tree_map
1725 || node
->clone
.args_to_skip
1726 || node
->clone
.combined_args_to_skip
));
1729 /* Output optimization summary for EDGE to OB. */
1731 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1732 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1736 /* Output optimization summary for NODE to OB. */
1739 output_node_opt_summary (struct output_block
*ob
,
1740 struct cgraph_node
*node
,
1741 lto_symtab_encoder_t encoder
)
1745 struct ipa_replace_map
*map
;
1746 struct bitpack_d bp
;
1748 struct cgraph_edge
*e
;
1750 if (node
->clone
.args_to_skip
)
1752 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1753 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1754 streamer_write_uhwi (ob
, index
);
1757 streamer_write_uhwi (ob
, 0);
1758 if (node
->clone
.combined_args_to_skip
)
1760 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1761 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1762 streamer_write_uhwi (ob
, index
);
1765 streamer_write_uhwi (ob
, 0);
1766 streamer_write_uhwi (ob
, vec_safe_length (node
->clone
.tree_map
));
1767 FOR_EACH_VEC_SAFE_ELT (node
->clone
.tree_map
, i
, map
)
1769 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1770 mechanism to store function local declarations into summaries. */
1771 gcc_assert (!map
->old_tree
);
1772 streamer_write_uhwi (ob
, map
->parm_num
);
1773 gcc_assert (EXPR_LOCATION (map
->new_tree
) == UNKNOWN_LOCATION
);
1774 stream_write_tree (ob
, map
->new_tree
, true);
1775 bp
= bitpack_create (ob
->main_stream
);
1776 bp_pack_value (&bp
, map
->replace_p
, 1);
1777 bp_pack_value (&bp
, map
->ref_p
, 1);
1778 streamer_write_bitpack (&bp
);
1781 if (lto_symtab_encoder_in_partition_p (encoder
, node
))
1783 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1784 output_edge_opt_summary (ob
, e
);
1785 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1786 output_edge_opt_summary (ob
, e
);
1790 /* Output optimization summaries stored in callgraph.
1791 At the moment it is the clone info structure. */
1794 output_cgraph_opt_summary (void)
1797 lto_symtab_encoder_t encoder
;
1798 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1801 ob
->cgraph_node
= NULL
;
1802 encoder
= ob
->decl_state
->symtab_node_encoder
;
1803 n_nodes
= lto_symtab_encoder_size (encoder
);
1804 for (i
= 0; i
< n_nodes
; i
++)
1806 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1807 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1808 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1811 streamer_write_uhwi (ob
, count
);
1812 for (i
= 0; i
< n_nodes
; i
++)
1814 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1815 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1816 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1818 streamer_write_uhwi (ob
, i
);
1819 output_node_opt_summary (ob
, cnode
, encoder
);
1822 produce_asm (ob
, NULL
);
1823 destroy_output_block (ob
);
1826 /* Input optimisation summary of EDGE. */
1829 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1830 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1834 /* Input optimisation summary of NODE. */
1837 input_node_opt_summary (struct cgraph_node
*node
,
1838 struct lto_input_block
*ib_main
,
1839 struct data_in
*data_in
)
1844 struct bitpack_d bp
;
1845 struct cgraph_edge
*e
;
1847 count
= streamer_read_uhwi (ib_main
);
1849 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1850 for (i
= 0; i
< count
; i
++)
1852 bit
= streamer_read_uhwi (ib_main
);
1853 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1855 count
= streamer_read_uhwi (ib_main
);
1857 node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
1858 for (i
= 0; i
< count
; i
++)
1860 bit
= streamer_read_uhwi (ib_main
);
1861 bitmap_set_bit (node
->clone
.combined_args_to_skip
, bit
);
1863 count
= streamer_read_uhwi (ib_main
);
1864 for (i
= 0; i
< count
; i
++)
1866 struct ipa_replace_map
*map
= ggc_alloc
<ipa_replace_map
> ();
1868 vec_safe_push (node
->clone
.tree_map
, map
);
1869 map
->parm_num
= streamer_read_uhwi (ib_main
);
1870 map
->old_tree
= NULL
;
1871 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1872 bp
= streamer_read_bitpack (ib_main
);
1873 map
->replace_p
= bp_unpack_value (&bp
, 1);
1874 map
->ref_p
= bp_unpack_value (&bp
, 1);
1876 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1877 input_edge_opt_summary (e
, ib_main
);
1878 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1879 input_edge_opt_summary (e
, ib_main
);
1882 /* Read section in file FILE_DATA of length LEN with data DATA. */
1885 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1886 const char *data
, size_t len
,
1887 vec
<symtab_node
*> nodes
)
1889 const struct lto_function_header
*header
=
1890 (const struct lto_function_header
*) data
;
1891 const int cfg_offset
= sizeof (struct lto_function_header
);
1892 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1893 const int string_offset
= main_offset
+ header
->main_size
;
1894 struct data_in
*data_in
;
1895 struct lto_input_block ib_main
;
1899 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1903 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1904 header
->string_size
, vNULL
);
1905 count
= streamer_read_uhwi (&ib_main
);
1907 for (i
= 0; i
< count
; i
++)
1909 int ref
= streamer_read_uhwi (&ib_main
);
1910 input_node_opt_summary (cgraph (nodes
[ref
]),
1913 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1915 lto_data_in_delete (data_in
);
1918 /* Input optimization summary of cgraph. */
1921 input_cgraph_opt_summary (vec
<symtab_node
*> nodes
)
1923 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1924 struct lto_file_decl_data
*file_data
;
1927 while ((file_data
= file_data_vec
[j
++]))
1931 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1935 input_cgraph_opt_section (file_data
, data
, len
, nodes
);