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"
34 #include "hard-reg-set.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
46 #include "langhooks.h"
48 #include "diagnostic-core.h"
51 #include "lto-streamer.h"
52 #include "data-streamer.h"
53 #include "tree-streamer.h"
55 #include "tree-pass.h"
58 #include "pass_manager.h"
59 #include "ipa-utils.h"
61 /* True when asm nodes has been output. */
62 bool asm_nodes_output
= false;
64 static void output_cgraph_opt_summary (void);
65 static void input_cgraph_opt_summary (vec
<symtab_node
*> nodes
);
67 /* Number of LDPR values known to GCC. */
68 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
70 /* All node orders are ofsetted by ORDER_BASE. */
71 static int order_base
;
73 /* Cgraph streaming is organized as set of record whose type
74 is indicated by a tag. */
77 /* Must leave 0 for the stopper. */
79 /* Cgraph node without body available. */
80 LTO_symtab_unavail_node
= 1,
81 /* Cgraph node with function body. */
82 LTO_symtab_analyzed_node
,
85 LTO_symtab_indirect_edge
,
90 /* Create a new symtab encoder.
91 if FOR_INPUT, the encoder allocate only datastructures needed
92 to read the symtab. */
95 lto_symtab_encoder_new (bool for_input
)
97 lto_symtab_encoder_t encoder
= XCNEW (struct lto_symtab_encoder_d
);
100 encoder
->map
= new hash_map
<symtab_node
*, size_t>;
101 encoder
->nodes
.create (0);
106 /* Delete ENCODER and its components. */
109 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder
)
111 encoder
->nodes
.release ();
118 /* Return the existing reference number of NODE in the symtab encoder in
119 output block OB. Assign a new reference if this is the first time
123 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder
,
130 lto_encoder_entry entry
= {node
, false, false, false};
132 ref
= encoder
->nodes
.length ();
133 encoder
->nodes
.safe_push (entry
);
137 size_t *slot
= encoder
->map
->get (node
);
140 lto_encoder_entry entry
= {node
, false, false, false};
141 ref
= encoder
->nodes
.length ();
143 encoder
->map
->put (node
, ref
+ 1);
144 encoder
->nodes
.safe_push (entry
);
152 /* Remove NODE from encoder. */
155 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder
,
159 lto_encoder_entry last_node
;
161 size_t *slot
= encoder
->map
->get (node
);
162 if (slot
== NULL
|| !*slot
)
166 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
168 /* Remove from vector. We do this by swapping node with the last element
170 last_node
= encoder
->nodes
.pop ();
171 if (last_node
.node
!= node
)
173 gcc_assert (encoder
->map
->put (last_node
.node
, 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. */
180 encoder
->map
->remove (node
);
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 bp_pack_value (&bp
, edge
->in_polymorphic_cdtor
, 1);
292 if (edge
->indirect_unknown_callee
)
294 int flags
= edge
->indirect_info
->ecf_flags
;
295 bp_pack_value (&bp
, (flags
& ECF_CONST
) != 0, 1);
296 bp_pack_value (&bp
, (flags
& ECF_PURE
) != 0, 1);
297 bp_pack_value (&bp
, (flags
& ECF_NORETURN
) != 0, 1);
298 bp_pack_value (&bp
, (flags
& ECF_MALLOC
) != 0, 1);
299 bp_pack_value (&bp
, (flags
& ECF_NOTHROW
) != 0, 1);
300 bp_pack_value (&bp
, (flags
& ECF_RETURNS_TWICE
) != 0, 1);
301 /* Flags that should not appear on indirect calls. */
302 gcc_assert (!(flags
& (ECF_LOOPING_CONST_OR_PURE
308 streamer_write_bitpack (&bp
);
309 if (edge
->indirect_unknown_callee
)
311 streamer_write_hwi_stream (ob
->main_stream
,
312 edge
->indirect_info
->common_target_id
);
313 if (edge
->indirect_info
->common_target_id
)
314 streamer_write_hwi_stream
315 (ob
->main_stream
, edge
->indirect_info
->common_target_probability
);
319 /* Return if NODE contain references from other partitions. */
322 referenced_from_other_partition_p (symtab_node
*node
, lto_symtab_encoder_t encoder
)
325 struct ipa_ref
*ref
= NULL
;
327 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
329 if (ref
->referring
->in_other_partition
330 || !lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
336 /* Return true when node is reachable from other partition. */
339 reachable_from_other_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
341 struct cgraph_edge
*e
;
342 if (!node
->definition
)
344 if (node
->global
.inlined_to
)
346 for (e
= node
->callers
; e
; e
= e
->next_caller
)
347 if (e
->caller
->in_other_partition
348 || !lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
353 /* Return if NODE contain references from other partitions. */
356 referenced_from_this_partition_p (symtab_node
*node
,
357 lto_symtab_encoder_t encoder
)
360 struct ipa_ref
*ref
= NULL
;
362 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
363 if (lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
368 /* Return true when node is reachable from other partition. */
371 reachable_from_this_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
373 struct cgraph_edge
*e
;
374 for (e
= node
->callers
; e
; e
= e
->next_caller
)
375 if (lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
380 /* Output the cgraph NODE to OB. ENCODER is used to find the
381 reference number of NODE->inlined_to. SET is the set of nodes we
382 are writing to the current file. If NODE is not in SET, then NODE
383 is a boundary of a cgraph_node_set and we pretend NODE just has a
384 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
385 that have had their callgraph node written so far. This is used to
386 determine if NODE is a clone of a previously written node. */
389 lto_output_node (struct lto_simple_output_block
*ob
, struct cgraph_node
*node
,
390 lto_symtab_encoder_t encoder
)
396 bool in_other_partition
= false;
397 struct cgraph_node
*clone_of
, *ultimate_clone_of
;
398 ipa_opt_pass_d
*pass
;
405 boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
407 if (node
->analyzed
&& !boundary_p
)
408 tag
= LTO_symtab_analyzed_node
;
410 tag
= LTO_symtab_unavail_node
;
412 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
414 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
416 /* In WPA mode, we only output part of the call-graph. Also, we
417 fake cgraph node attributes. There are two cases that we care.
419 Boundary nodes: There are nodes that are not part of SET but are
420 called from within SET. We artificially make them look like
421 externally visible nodes with no function body.
423 Cherry-picked nodes: These are nodes we pulled from other
424 translation units into SET during IPA-inlining. We make them as
425 local static nodes to prevent clashes with other local statics. */
426 if (boundary_p
&& node
->analyzed
427 && node
->get_partitioning_class () == SYMBOL_PARTITION
)
429 /* Inline clones can not be part of boundary.
430 gcc_assert (!node->global.inlined_to);
432 FIXME: At the moment they can be, when partition contains an inline
433 clone that is clone of inline clone from outside partition. We can
434 reshape the clone tree and make other tree to be the root, but it
435 needs a bit extra work and will be promplty done by cgraph_remove_node
436 after reading back. */
437 in_other_partition
= 1;
440 clone_of
= node
->clone_of
;
442 && (ref
= lto_symtab_encoder_lookup (encoder
, clone_of
)) == LCC_NOT_FOUND
)
443 if (clone_of
->prev_sibling_clone
)
444 clone_of
= clone_of
->prev_sibling_clone
;
446 clone_of
= clone_of
->clone_of
;
448 /* See if body of the master function is output. If not, we are seeing only
449 an declaration and we do not need to pass down clone tree. */
450 ultimate_clone_of
= clone_of
;
451 while (ultimate_clone_of
&& ultimate_clone_of
->clone_of
)
452 ultimate_clone_of
= ultimate_clone_of
->clone_of
;
454 if (clone_of
&& !lto_symtab_encoder_encode_body_p (encoder
, ultimate_clone_of
))
457 if (tag
== LTO_symtab_analyzed_node
)
458 gcc_assert (clone_of
|| !node
->clone_of
);
460 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
462 streamer_write_hwi_stream (ob
->main_stream
, ref
);
465 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
466 streamer_write_gcov_count_stream (ob
->main_stream
, node
->count
);
467 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
469 streamer_write_hwi_stream (ob
->main_stream
,
470 node
->ipa_transforms_to_apply
.length ());
471 FOR_EACH_VEC_ELT (node
->ipa_transforms_to_apply
, i
, pass
)
472 streamer_write_hwi_stream (ob
->main_stream
, pass
->static_pass_number
);
474 if (tag
== LTO_symtab_analyzed_node
)
476 if (node
->global
.inlined_to
)
478 ref
= lto_symtab_encoder_lookup (encoder
, node
->global
.inlined_to
);
479 gcc_assert (ref
!= LCC_NOT_FOUND
);
484 streamer_write_hwi_stream (ob
->main_stream
, ref
);
487 group
= node
->get_comdat_group ();
489 comdat
= IDENTIFIER_POINTER (group
);
492 streamer_write_data_stream (ob
->main_stream
, comdat
, strlen (comdat
) + 1);
496 if (node
->same_comdat_group
&& !boundary_p
)
498 ref
= lto_symtab_encoder_lookup (encoder
,
499 node
->same_comdat_group
);
500 gcc_assert (ref
!= LCC_NOT_FOUND
);
504 streamer_write_hwi_stream (ob
->main_stream
, ref
);
507 section
= node
->get_section ();
511 streamer_write_hwi_stream (ob
->main_stream
, node
->tp_first_run
);
513 bp
= bitpack_create (ob
->main_stream
);
514 bp_pack_value (&bp
, node
->local
.local
, 1);
515 bp_pack_value (&bp
, node
->externally_visible
, 1);
516 bp_pack_value (&bp
, node
->no_reorder
, 1);
517 bp_pack_value (&bp
, node
->definition
, 1);
518 bp_pack_value (&bp
, node
->local
.versionable
, 1);
519 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
520 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
521 bp_pack_value (&bp
, node
->force_output
, 1);
522 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
523 bp_pack_value (&bp
, node
->unique_name
, 1);
524 bp_pack_value (&bp
, node
->body_removed
, 1);
525 bp_pack_value (&bp
, node
->implicit_section
, 1);
526 bp_pack_value (&bp
, node
->address_taken
, 1);
527 bp_pack_value (&bp
, tag
== LTO_symtab_analyzed_node
528 && node
->get_partitioning_class () == SYMBOL_PARTITION
529 && (reachable_from_other_partition_p (node
, encoder
)
530 || referenced_from_other_partition_p (node
, encoder
)), 1);
531 bp_pack_value (&bp
, node
->lowered
, 1);
532 bp_pack_value (&bp
, in_other_partition
, 1);
533 /* Real aliases in a boundary become non-aliases. However we still stream
534 alias info on weakrefs.
535 TODO: We lose a bit of information here - when we know that variable is
536 defined in other unit, we may use the info on aliases to resolve
537 symbol1 != symbol2 type tests that we can do only for locally defined objects
539 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
540 bp_pack_value (&bp
, alias_p
, 1);
541 bp_pack_value (&bp
, node
->weakref
, 1);
542 bp_pack_value (&bp
, node
->frequency
, 2);
543 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
544 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
545 bp_pack_value (&bp
, node
->tm_clone
, 1);
546 bp_pack_value (&bp
, node
->calls_comdat_local
, 1);
547 bp_pack_value (&bp
, node
->icf_merged
, 1);
548 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
549 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
550 LDPR_NUM_KNOWN
, node
->resolution
);
551 streamer_write_bitpack (&bp
);
552 streamer_write_data_stream (ob
->main_stream
, section
, strlen (section
) + 1);
554 if (node
->thunk
.thunk_p
&& !boundary_p
)
556 streamer_write_uhwi_stream
558 1 + (node
->thunk
.this_adjusting
!= 0) * 2
559 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
560 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
561 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
563 streamer_write_hwi_stream (ob
->main_stream
, node
->profile_id
);
564 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
565 streamer_write_hwi_stream (ob
->main_stream
, node
->get_init_priority ());
566 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
567 streamer_write_hwi_stream (ob
->main_stream
, node
->get_fini_priority ());
570 /* Output the varpool NODE to OB.
571 If NODE is not in SET, then NODE is a boundary. */
574 lto_output_varpool_node (struct lto_simple_output_block
*ob
, varpool_node
*node
,
575 lto_symtab_encoder_t encoder
)
577 bool boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
585 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
586 LTO_symtab_variable
);
587 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
588 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
589 bp
= bitpack_create (ob
->main_stream
);
590 bp_pack_value (&bp
, node
->externally_visible
, 1);
591 bp_pack_value (&bp
, node
->no_reorder
, 1);
592 bp_pack_value (&bp
, node
->force_output
, 1);
593 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
594 bp_pack_value (&bp
, node
->unique_name
, 1);
595 bp_pack_value (&bp
, node
->body_removed
, 1);
596 bp_pack_value (&bp
, node
->implicit_section
, 1);
597 bp_pack_value (&bp
, node
->writeonly
, 1);
598 bp_pack_value (&bp
, node
->definition
, 1);
599 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
600 bp_pack_value (&bp
, alias_p
, 1);
601 bp_pack_value (&bp
, node
->weakref
, 1);
602 bp_pack_value (&bp
, node
->analyzed
&& !boundary_p
, 1);
603 gcc_assert (node
->definition
|| !node
->analyzed
);
604 /* Constant pool initializers can be de-unified into individual ltrans units.
605 FIXME: Alternatively at -Os we may want to avoid generating for them the local
606 labels and share them across LTRANS partitions. */
607 if (node
->get_partitioning_class () != SYMBOL_PARTITION
)
609 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
610 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
614 bp_pack_value (&bp
, node
->definition
615 && referenced_from_other_partition_p (node
, encoder
), 1);
616 bp_pack_value (&bp
, node
->analyzed
617 && boundary_p
&& !DECL_EXTERNAL (node
->decl
), 1);
618 /* in_other_partition. */
620 bp_pack_value (&bp
, node
->tls_model
, 3);
621 bp_pack_value (&bp
, node
->used_by_single_function
, 1);
622 streamer_write_bitpack (&bp
);
624 group
= node
->get_comdat_group ();
626 comdat
= IDENTIFIER_POINTER (group
);
629 streamer_write_data_stream (ob
->main_stream
, comdat
, strlen (comdat
) + 1);
633 if (node
->same_comdat_group
&& !boundary_p
)
635 ref
= lto_symtab_encoder_lookup (encoder
,
636 node
->same_comdat_group
);
637 gcc_assert (ref
!= LCC_NOT_FOUND
);
641 streamer_write_hwi_stream (ob
->main_stream
, ref
);
644 section
= node
->get_section ();
647 streamer_write_data_stream (ob
->main_stream
, section
, strlen (section
) + 1);
649 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
650 LDPR_NUM_KNOWN
, node
->resolution
);
653 /* Output the varpool NODE to OB.
654 If NODE is not in SET, then NODE is a boundary. */
657 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
658 lto_symtab_encoder_t encoder
)
662 int uid
= ref
->lto_stmt_uid
;
663 struct cgraph_node
*node
;
665 bp
= bitpack_create (ob
->main_stream
);
666 bp_pack_value (&bp
, ref
->use
, 2);
667 bp_pack_value (&bp
, ref
->speculative
, 1);
668 streamer_write_bitpack (&bp
);
669 nref
= lto_symtab_encoder_lookup (encoder
, ref
->referred
);
670 gcc_assert (nref
!= LCC_NOT_FOUND
);
671 streamer_write_hwi_stream (ob
->main_stream
, nref
);
673 node
= dyn_cast
<cgraph_node
*> (ref
->referring
);
677 uid
= gimple_uid (ref
->stmt
) + 1;
678 streamer_write_hwi_stream (ob
->main_stream
, uid
);
682 /* Stream out profile_summary to OB. */
685 output_profile_summary (struct lto_simple_output_block
*ob
)
692 /* We do not output num and run_max, they are not used by
693 GCC profile feedback and they are difficult to merge from multiple
695 gcc_assert (profile_info
->runs
);
696 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
697 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_max
);
699 /* sum_all is needed for computing the working set with the
701 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_all
);
703 /* Create and output a bitpack of non-zero histogram entries indices. */
704 bp
= bitpack_create (ob
->main_stream
);
705 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
706 bp_pack_value (&bp
, profile_info
->histogram
[h_ix
].num_counters
> 0, 1);
707 streamer_write_bitpack (&bp
);
708 /* Now stream out only those non-zero entries. */
709 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
711 if (!profile_info
->histogram
[h_ix
].num_counters
)
713 streamer_write_gcov_count_stream (ob
->main_stream
,
714 profile_info
->histogram
[h_ix
].num_counters
);
715 streamer_write_gcov_count_stream (ob
->main_stream
,
716 profile_info
->histogram
[h_ix
].min_value
);
717 streamer_write_gcov_count_stream (ob
->main_stream
,
718 profile_info
->histogram
[h_ix
].cum_value
);
720 /* IPA-profile computes hot bb threshold based on cumulated
721 whole program profile. We need to stream it down to ltrans. */
723 streamer_write_gcov_count_stream (ob
->main_stream
,
724 get_hot_bb_threshold ());
727 streamer_write_uhwi_stream (ob
->main_stream
, 0);
730 /* Output all callees or indirect outgoing edges. EDGE must be the first such
734 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
735 struct lto_simple_output_block
*ob
,
736 lto_symtab_encoder_t encoder
)
741 /* Output edges in backward direction, so the reconstructed callgraph match
742 and it is easy to associate call sites in the IPA pass summaries. */
743 while (edge
->next_callee
)
744 edge
= edge
->next_callee
;
745 for (; edge
; edge
= edge
->prev_callee
)
746 lto_output_edge (ob
, edge
, encoder
);
749 /* Output the part of the cgraph in SET. */
752 output_refs (lto_symtab_encoder_t encoder
)
754 lto_symtab_encoder_iterator lsei
;
755 struct lto_simple_output_block
*ob
;
760 ob
= lto_create_simple_output_block (LTO_section_refs
);
762 for (lsei
= lsei_start_in_partition (encoder
); !lsei_end_p (lsei
);
763 lsei_next_in_partition (&lsei
))
765 symtab_node
*node
= lsei_node (lsei
);
767 count
= node
->ref_list
.nreferences ();
770 streamer_write_gcov_count_stream (ob
->main_stream
, count
);
771 streamer_write_uhwi_stream (ob
->main_stream
,
772 lto_symtab_encoder_lookup (encoder
, node
));
773 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
774 lto_output_ref (ob
, ref
, encoder
);
778 streamer_write_uhwi_stream (ob
->main_stream
, 0);
780 lto_destroy_simple_output_block (ob
);
783 /* Add NODE into encoder as well as nodes it is cloned from.
784 Do it in a way so clones appear first. */
787 add_node_to (lto_symtab_encoder_t encoder
, struct cgraph_node
*node
,
791 add_node_to (encoder
, node
->clone_of
, include_body
);
792 else if (include_body
)
793 lto_set_symtab_encoder_encode_body (encoder
, node
);
794 lto_symtab_encoder_encode (encoder
, node
);
797 /* Add all references in NODE to encoders. */
800 create_references (lto_symtab_encoder_t encoder
, symtab_node
*node
)
803 struct ipa_ref
*ref
= NULL
;
804 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
805 if (is_a
<cgraph_node
*> (ref
->referred
))
806 add_node_to (encoder
, dyn_cast
<cgraph_node
*> (ref
->referred
), false);
808 lto_symtab_encoder_encode (encoder
, ref
->referred
);
811 /* Find all symbols we want to stream into given partition and insert them
814 The function actually replaces IN_ENCODER by new one. The reason is that
815 streaming code needs clone's origin to be streamed before clone. This
816 means that we need to insert the nodes in specific order. This order is
817 ignored by the partitioning logic earlier. */
820 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder
)
822 struct cgraph_edge
*edge
;
824 lto_symtab_encoder_t encoder
;
825 lto_symtab_encoder_iterator lsei
;
826 hash_set
<void *> reachable_call_targets
;
828 encoder
= lto_symtab_encoder_new (false);
830 /* Go over all entries in the IN_ENCODER and duplicate them to
831 ENCODER. At the same time insert masters of clones so
832 every master appears before clone. */
833 for (lsei
= lsei_start_function_in_partition (in_encoder
);
834 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
836 struct cgraph_node
*node
= lsei_cgraph_node (lsei
);
837 add_node_to (encoder
, node
, true);
838 lto_set_symtab_encoder_in_partition (encoder
, node
);
839 create_references (encoder
, node
);
840 /* For proper debug info, we need to ship the origins, too. */
841 if (DECL_ABSTRACT_ORIGIN (node
->decl
))
843 struct cgraph_node
*origin_node
844 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node
->decl
));
845 add_node_to (encoder
, origin_node
, true);
848 for (lsei
= lsei_start_variable_in_partition (in_encoder
);
849 !lsei_end_p (lsei
); lsei_next_variable_in_partition (&lsei
))
851 varpool_node
*vnode
= lsei_varpool_node (lsei
);
853 lto_set_symtab_encoder_in_partition (encoder
, vnode
);
854 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
855 create_references (encoder
, vnode
);
856 /* For proper debug info, we need to ship the origins, too. */
857 if (DECL_ABSTRACT_ORIGIN (vnode
->decl
))
859 varpool_node
*origin_node
860 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode
->decl
));
861 lto_set_symtab_encoder_in_partition (encoder
, origin_node
);
864 /* Pickle in also the initializer of all referenced readonly variables
865 to help folding. Constant pool variables are not shared, so we must
867 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
869 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
870 if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
872 if (!lto_symtab_encoder_encode_initializer_p (encoder
,
874 && vnode
->ctor_useable_for_folding_p ())
876 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
877 create_references (encoder
, vnode
);
882 /* Go over all the nodes again to include callees that are not in
884 for (lsei
= lsei_start_function_in_partition (encoder
);
885 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
887 struct cgraph_node
*node
= lsei_cgraph_node (lsei
);
888 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
890 struct cgraph_node
*callee
= edge
->callee
;
891 if (!lto_symtab_encoder_in_partition_p (encoder
, callee
))
893 /* We should have moved all the inlines. */
894 gcc_assert (!callee
->global
.inlined_to
);
895 add_node_to (encoder
, callee
, false);
898 /* Add all possible targets for late devirtualization. */
899 if (flag_devirtualize
)
900 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
901 if (edge
->indirect_info
->polymorphic
)
906 vec
<cgraph_node
*>targets
907 = possible_polymorphic_call_targets
908 (edge
, &final
, &cache_token
);
909 if (!reachable_call_targets
.add (cache_token
))
911 for (i
= 0; i
< targets
.length (); i
++)
913 struct cgraph_node
*callee
= targets
[i
];
915 /* Adding an external declarations into the unit serves
916 no purpose and just increases its boundary. */
917 if (callee
->definition
918 && !lto_symtab_encoder_in_partition_p
921 gcc_assert (!callee
->global
.inlined_to
);
922 add_node_to (encoder
, callee
, false);
928 lto_symtab_encoder_delete (in_encoder
);
932 /* Output the part of the symtab in SET and VSET. */
937 struct cgraph_node
*node
;
938 struct lto_simple_output_block
*ob
;
939 lto_symtab_encoder_iterator lsei
;
941 lto_symtab_encoder_t encoder
;
944 output_cgraph_opt_summary ();
946 ob
= lto_create_simple_output_block (LTO_section_symtab_nodes
);
948 output_profile_summary (ob
);
950 /* An encoder for cgraph nodes should have been created by
951 ipa_write_summaries_1. */
952 gcc_assert (ob
->decl_state
->symtab_node_encoder
);
953 encoder
= ob
->decl_state
->symtab_node_encoder
;
955 /* Write out the nodes. We must first output a node and then its clones,
956 otherwise at a time reading back the node there would be nothing to clone
958 n_nodes
= lto_symtab_encoder_size (encoder
);
959 for (i
= 0; i
< n_nodes
; i
++)
961 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
962 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
963 lto_output_node (ob
, cnode
, encoder
);
965 lto_output_varpool_node (ob
, dyn_cast
<varpool_node
*> (node
), encoder
);
968 /* Go over the nodes in SET again to write edges. */
969 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
970 lsei_next_function_in_partition (&lsei
))
972 node
= lsei_cgraph_node (lsei
);
973 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
974 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
977 streamer_write_uhwi_stream (ob
->main_stream
, 0);
979 lto_destroy_simple_output_block (ob
);
981 /* Emit toplevel asms.
982 When doing WPA we must output every asm just once. Since we do not partition asm
983 nodes at all, output them to first output. This is kind of hack, but should work
985 if (!asm_nodes_output
)
987 asm_nodes_output
= true;
988 lto_output_toplevel_asms ();
991 output_refs (encoder
);
994 /* Return identifier encoded in IB as a plain string. */
997 read_identifier (struct lto_input_block
*ib
)
999 unsigned int len
= strnlen (ib
->data
+ ib
->p
, ib
->len
- ib
->p
- 1);
1002 if (ib
->data
[ib
->p
+ len
])
1003 lto_section_overrun (ib
);
1009 id
= get_identifier (ib
->data
+ ib
->p
);
1014 /* Return string encoded in IB, NULL if string is empty. */
1017 read_string (struct lto_input_block
*ib
)
1019 unsigned int len
= strnlen (ib
->data
+ ib
->p
, ib
->len
- ib
->p
- 1);
1022 if (ib
->data
[ib
->p
+ len
])
1023 lto_section_overrun (ib
);
1029 str
= ib
->data
+ ib
->p
;
1034 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1035 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1036 NODE or to replace the values in it, for instance because the first
1037 time we saw it, the function body was not available but now it
1038 is. BP is a bitpack with all the bitflags for NODE read from the
1042 input_overwrite_node (struct lto_file_decl_data
*file_data
,
1043 struct cgraph_node
*node
,
1044 enum LTO_symtab_tags tag
,
1045 struct bitpack_d
*bp
)
1047 node
->aux
= (void *) tag
;
1048 node
->lto_file_data
= file_data
;
1050 node
->local
.local
= bp_unpack_value (bp
, 1);
1051 node
->externally_visible
= bp_unpack_value (bp
, 1);
1052 node
->no_reorder
= bp_unpack_value (bp
, 1);
1053 node
->definition
= bp_unpack_value (bp
, 1);
1054 node
->local
.versionable
= bp_unpack_value (bp
, 1);
1055 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
1056 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
1057 node
->force_output
= bp_unpack_value (bp
, 1);
1058 node
->forced_by_abi
= bp_unpack_value (bp
, 1);
1059 node
->unique_name
= bp_unpack_value (bp
, 1);
1060 node
->body_removed
= bp_unpack_value (bp
, 1);
1061 node
->implicit_section
= bp_unpack_value (bp
, 1);
1062 node
->address_taken
= bp_unpack_value (bp
, 1);
1063 node
->used_from_other_partition
= bp_unpack_value (bp
, 1);
1064 node
->lowered
= bp_unpack_value (bp
, 1);
1065 node
->analyzed
= tag
== LTO_symtab_analyzed_node
;
1066 node
->in_other_partition
= bp_unpack_value (bp
, 1);
1067 if (node
->in_other_partition
1068 /* Avoid updating decl when we are seeing just inline clone.
1069 When inlining function that has functions already inlined into it,
1070 we produce clones of inline clones.
1072 WPA partitioning might put each clone into different unit and
1073 we might end up streaming inline clone from other partition
1074 to support clone we are interested in. */
1076 || node
->clone_of
->decl
!= node
->decl
))
1078 DECL_EXTERNAL (node
->decl
) = 1;
1079 TREE_STATIC (node
->decl
) = 0;
1081 node
->alias
= bp_unpack_value (bp
, 1);
1082 node
->weakref
= bp_unpack_value (bp
, 1);
1083 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
1084 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
1085 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
1086 node
->tm_clone
= bp_unpack_value (bp
, 1);
1087 node
->calls_comdat_local
= bp_unpack_value (bp
, 1);
1088 node
->icf_merged
= bp_unpack_value (bp
, 1);
1089 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
1090 node
->resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
1092 gcc_assert (flag_ltrans
1093 || (!node
->in_other_partition
1094 && !node
->used_from_other_partition
));
1097 /* Return string alias is alias of. */
1100 get_alias_symbol (tree decl
)
1102 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1103 return get_identifier (TREE_STRING_POINTER
1104 (TREE_VALUE (TREE_VALUE (alias
))));
1107 /* Read a node from input_block IB. TAG is the node's tag just read.
1108 Return the node read or overwriten. */
1110 static struct cgraph_node
*
1111 input_node (struct lto_file_decl_data
*file_data
,
1112 struct lto_input_block
*ib
,
1113 enum LTO_symtab_tags tag
,
1114 vec
<symtab_node
*> nodes
)
1116 gcc::pass_manager
*passes
= g
->get_passes ();
1118 struct cgraph_node
*node
;
1119 struct bitpack_d bp
;
1120 unsigned decl_index
;
1121 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
1126 const char *section
;
1127 order
= streamer_read_hwi (ib
) + order_base
;
1128 clone_ref
= streamer_read_hwi (ib
);
1130 decl_index
= streamer_read_uhwi (ib
);
1131 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
1133 if (clone_ref
!= LCC_NOT_FOUND
)
1135 node
= dyn_cast
<cgraph_node
*> (nodes
[clone_ref
])->create_clone (fn_decl
,
1136 0, CGRAPH_FREQ_BASE
, false,
1137 vNULL
, false, NULL
, NULL
);
1141 /* Declaration of functions can be already merged with a declaration
1142 from other input file. We keep cgraph unmerged until after streaming
1143 of ipa passes is done. Alays forcingly create a fresh node. */
1144 node
= symtab
->create_empty ();
1145 node
->decl
= fn_decl
;
1146 node
->register_symbol ();
1149 node
->order
= order
;
1150 if (order
>= symtab
->order
)
1151 symtab
->order
= order
+ 1;
1153 node
->count
= streamer_read_gcov_count (ib
);
1154 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1156 count
= streamer_read_hwi (ib
);
1157 node
->ipa_transforms_to_apply
= vNULL
;
1158 for (i
= 0; i
< count
; i
++)
1161 int pid
= streamer_read_hwi (ib
);
1163 gcc_assert (pid
< passes
->passes_by_id_size
);
1164 pass
= passes
->passes_by_id
[pid
];
1165 node
->ipa_transforms_to_apply
.safe_push ((ipa_opt_pass_d
*) pass
);
1168 if (tag
== LTO_symtab_analyzed_node
)
1169 ref
= streamer_read_hwi (ib
);
1171 group
= read_identifier (ib
);
1173 ref2
= streamer_read_hwi (ib
);
1175 /* Make sure that we have not read this node before. Nodes that
1176 have already been read will have their tag stored in the 'aux'
1177 field. Since built-in functions can be referenced in multiple
1178 functions, they are expected to be read more than once. */
1179 if (node
->aux
&& !DECL_BUILT_IN (node
->decl
))
1180 internal_error ("bytecode stream: found multiple instances of cgraph "
1181 "node with uid %d", node
->uid
);
1183 node
->tp_first_run
= streamer_read_uhwi (ib
);
1185 bp
= streamer_read_bitpack (ib
);
1187 input_overwrite_node (file_data
, node
, tag
, &bp
);
1189 /* Store a reference for now, and fix up later to be a pointer. */
1190 node
->global
.inlined_to
= (cgraph_node
*) (intptr_t) ref
;
1194 node
->set_comdat_group (group
);
1195 /* Store a reference for now, and fix up later to be a pointer. */
1196 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref2
;
1199 node
->same_comdat_group
= (symtab_node
*) (intptr_t) LCC_NOT_FOUND
;
1200 section
= read_string (ib
);
1202 node
->set_section_for_node (section
);
1204 if (node
->thunk
.thunk_p
)
1206 int type
= streamer_read_uhwi (ib
);
1207 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1208 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1210 node
->thunk
.fixed_offset
= fixed_offset
;
1211 node
->thunk
.this_adjusting
= (type
& 2);
1212 node
->thunk
.virtual_value
= virtual_value
;
1213 node
->thunk
.virtual_offset_p
= (type
& 4);
1215 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1216 node
->alias_target
= get_alias_symbol (node
->decl
);
1217 node
->profile_id
= streamer_read_hwi (ib
);
1218 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
1219 node
->set_init_priority (streamer_read_hwi (ib
));
1220 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
1221 node
->set_fini_priority (streamer_read_hwi (ib
));
1225 /* Read a node from input_block IB. TAG is the node's tag just read.
1226 Return the node read or overwriten. */
1228 static varpool_node
*
1229 input_varpool_node (struct lto_file_decl_data
*file_data
,
1230 struct lto_input_block
*ib
)
1235 struct bitpack_d bp
;
1236 int ref
= LCC_NOT_FOUND
;
1239 const char *section
;
1241 order
= streamer_read_hwi (ib
) + order_base
;
1242 decl_index
= streamer_read_uhwi (ib
);
1243 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1245 /* Declaration of functions can be already merged with a declaration
1246 from other input file. We keep cgraph unmerged until after streaming
1247 of ipa passes is done. Alays forcingly create a fresh node. */
1248 node
= varpool_node::create_empty ();
1249 node
->decl
= var_decl
;
1250 node
->register_symbol ();
1252 node
->order
= order
;
1253 if (order
>= symtab
->order
)
1254 symtab
->order
= order
+ 1;
1255 node
->lto_file_data
= file_data
;
1257 bp
= streamer_read_bitpack (ib
);
1258 node
->externally_visible
= bp_unpack_value (&bp
, 1);
1259 node
->no_reorder
= bp_unpack_value (&bp
, 1);
1260 node
->force_output
= bp_unpack_value (&bp
, 1);
1261 node
->forced_by_abi
= bp_unpack_value (&bp
, 1);
1262 node
->unique_name
= bp_unpack_value (&bp
, 1);
1263 node
->body_removed
= bp_unpack_value (&bp
, 1);
1264 node
->implicit_section
= bp_unpack_value (&bp
, 1);
1265 node
->writeonly
= bp_unpack_value (&bp
, 1);
1266 node
->definition
= bp_unpack_value (&bp
, 1);
1267 node
->alias
= bp_unpack_value (&bp
, 1);
1268 node
->weakref
= bp_unpack_value (&bp
, 1);
1269 node
->analyzed
= bp_unpack_value (&bp
, 1);
1270 node
->used_from_other_partition
= bp_unpack_value (&bp
, 1);
1271 node
->in_other_partition
= bp_unpack_value (&bp
, 1);
1272 if (node
->in_other_partition
)
1274 DECL_EXTERNAL (node
->decl
) = 1;
1275 TREE_STATIC (node
->decl
) = 0;
1277 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1278 node
->alias_target
= get_alias_symbol (node
->decl
);
1279 node
->tls_model
= (enum tls_model
)bp_unpack_value (&bp
, 3);
1280 node
->used_by_single_function
= (enum tls_model
)bp_unpack_value (&bp
, 1);
1281 group
= read_identifier (ib
);
1284 node
->set_comdat_group (group
);
1285 ref
= streamer_read_hwi (ib
);
1286 /* Store a reference for now, and fix up later to be a pointer. */
1287 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref
;
1290 node
->same_comdat_group
= (symtab_node
*) (intptr_t) LCC_NOT_FOUND
;
1291 section
= read_string (ib
);
1293 node
->set_section_for_node (section
);
1294 node
->resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1296 gcc_assert (flag_ltrans
1297 || (!node
->in_other_partition
1298 && !node
->used_from_other_partition
));
1303 /* Read a node from input_block IB. TAG is the node's tag just read.
1304 Return the node read or overwriten. */
1307 input_ref (struct lto_input_block
*ib
,
1308 symtab_node
*referring_node
,
1309 vec
<symtab_node
*> nodes
)
1311 symtab_node
*node
= NULL
;
1312 struct bitpack_d bp
;
1313 enum ipa_ref_use use
;
1315 struct ipa_ref
*ref
;
1317 bp
= streamer_read_bitpack (ib
);
1318 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1319 speculative
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 1);
1320 node
= nodes
[streamer_read_hwi (ib
)];
1321 ref
= referring_node
->create_reference (node
, use
);
1322 ref
->speculative
= speculative
;
1323 if (is_a
<cgraph_node
*> (referring_node
))
1324 ref
->lto_stmt_uid
= streamer_read_hwi (ib
);
1327 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1328 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1329 edge being read is indirect (in the sense that it has
1330 indirect_unknown_callee set). */
1333 input_edge (struct lto_input_block
*ib
, vec
<symtab_node
*> nodes
,
1336 struct cgraph_node
*caller
, *callee
;
1337 struct cgraph_edge
*edge
;
1338 unsigned int stmt_id
;
1341 cgraph_inline_failed_t inline_failed
;
1342 struct bitpack_d bp
;
1345 caller
= dyn_cast
<cgraph_node
*> (nodes
[streamer_read_hwi (ib
)]);
1346 if (caller
== NULL
|| caller
->decl
== NULL_TREE
)
1347 internal_error ("bytecode stream: no caller found while reading edge");
1351 callee
= dyn_cast
<cgraph_node
*> (nodes
[streamer_read_hwi (ib
)]);
1352 if (callee
== NULL
|| callee
->decl
== NULL_TREE
)
1353 internal_error ("bytecode stream: no callee found while reading edge");
1358 count
= streamer_read_gcov_count (ib
);
1360 bp
= streamer_read_bitpack (ib
);
1361 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_t
, CIF_N_REASONS
);
1362 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1363 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1366 edge
= caller
->create_indirect_edge (NULL
, 0, count
, freq
);
1368 edge
= caller
->create_edge (callee
, NULL
, count
, freq
);
1370 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1371 edge
->speculative
= bp_unpack_value (&bp
, 1);
1372 edge
->lto_stmt_uid
= stmt_id
;
1373 edge
->inline_failed
= inline_failed
;
1374 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1375 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1376 edge
->in_polymorphic_cdtor
= bp_unpack_value (&bp
, 1);
1379 if (bp_unpack_value (&bp
, 1))
1380 ecf_flags
|= ECF_CONST
;
1381 if (bp_unpack_value (&bp
, 1))
1382 ecf_flags
|= ECF_PURE
;
1383 if (bp_unpack_value (&bp
, 1))
1384 ecf_flags
|= ECF_NORETURN
;
1385 if (bp_unpack_value (&bp
, 1))
1386 ecf_flags
|= ECF_MALLOC
;
1387 if (bp_unpack_value (&bp
, 1))
1388 ecf_flags
|= ECF_NOTHROW
;
1389 if (bp_unpack_value (&bp
, 1))
1390 ecf_flags
|= ECF_RETURNS_TWICE
;
1391 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1392 edge
->indirect_info
->common_target_id
= streamer_read_hwi (ib
);
1393 if (edge
->indirect_info
->common_target_id
)
1394 edge
->indirect_info
->common_target_probability
= streamer_read_hwi (ib
);
1399 /* Read a cgraph from IB using the info in FILE_DATA. */
1401 static vec
<symtab_node
*>
1402 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1403 struct lto_input_block
*ib
)
1405 enum LTO_symtab_tags tag
;
1406 vec
<symtab_node
*> nodes
= vNULL
;
1410 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1411 order_base
= symtab
->order
;
1414 if (tag
== LTO_symtab_edge
)
1415 input_edge (ib
, nodes
, false);
1416 else if (tag
== LTO_symtab_indirect_edge
)
1417 input_edge (ib
, nodes
, true);
1418 else if (tag
== LTO_symtab_variable
)
1420 node
= input_varpool_node (file_data
, ib
);
1421 nodes
.safe_push (node
);
1422 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1426 node
= input_node (file_data
, ib
, tag
, nodes
);
1427 if (node
== NULL
|| node
->decl
== NULL_TREE
)
1428 internal_error ("bytecode stream: found empty cgraph node");
1429 nodes
.safe_push (node
);
1430 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1433 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1436 lto_input_toplevel_asms (file_data
, order_base
);
1438 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1439 #ifdef ENABLE_CHECKING
1440 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1441 gcc_assert (node
->aux
|| !is_a
<cgraph_node
*> (node
));
1443 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1446 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1448 ref
= (int) (intptr_t) cnode
->global
.inlined_to
;
1450 /* We share declaration of builtins, so we may read same node twice. */
1455 /* Fixup inlined_to from reference to pointer. */
1456 if (ref
!= LCC_NOT_FOUND
)
1457 dyn_cast
<cgraph_node
*> (node
)->global
.inlined_to
1458 = dyn_cast
<cgraph_node
*> (nodes
[ref
]);
1460 cnode
->global
.inlined_to
= NULL
;
1463 ref
= (int) (intptr_t) node
->same_comdat_group
;
1465 /* Fixup same_comdat_group from reference to pointer. */
1466 if (ref
!= LCC_NOT_FOUND
)
1467 node
->same_comdat_group
= nodes
[ref
];
1469 node
->same_comdat_group
= NULL
;
1471 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1472 node
->aux
= is_a
<cgraph_node
*> (node
) ? (void *)1 : NULL
;
1476 /* Input ipa_refs. */
1479 input_refs (struct lto_input_block
*ib
,
1480 vec
<symtab_node
*> nodes
)
1487 count
= streamer_read_uhwi (ib
);
1490 idx
= streamer_read_uhwi (ib
);
1494 input_ref (ib
, node
, nodes
);
1501 static struct gcov_ctr_summary lto_gcov_summary
;
1503 /* Input profile_info from IB. */
1505 input_profile_summary (struct lto_input_block
*ib
,
1506 struct lto_file_decl_data
*file_data
)
1509 struct bitpack_d bp
;
1510 unsigned int runs
= streamer_read_uhwi (ib
);
1513 file_data
->profile_info
.runs
= runs
;
1514 file_data
->profile_info
.sum_max
= streamer_read_gcov_count (ib
);
1515 file_data
->profile_info
.sum_all
= streamer_read_gcov_count (ib
);
1517 memset (file_data
->profile_info
.histogram
, 0,
1518 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1519 /* Input the bitpack of non-zero histogram indices. */
1520 bp
= streamer_read_bitpack (ib
);
1521 /* Read in and unpack the full bitpack, flagging non-zero
1522 histogram entries by setting the num_counters non-zero. */
1523 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1525 file_data
->profile_info
.histogram
[h_ix
].num_counters
1526 = bp_unpack_value (&bp
, 1);
1528 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1530 if (!file_data
->profile_info
.histogram
[h_ix
].num_counters
)
1533 file_data
->profile_info
.histogram
[h_ix
].num_counters
1534 = streamer_read_gcov_count (ib
);
1535 file_data
->profile_info
.histogram
[h_ix
].min_value
1536 = streamer_read_gcov_count (ib
);
1537 file_data
->profile_info
.histogram
[h_ix
].cum_value
1538 = streamer_read_gcov_count (ib
);
1540 /* IPA-profile computes hot bb threshold based on cumulated
1541 whole program profile. We need to stream it down to ltrans. */
1543 set_hot_bb_threshold (streamer_read_gcov_count (ib
));
1548 /* Rescale profile summaries to the same number of runs in the whole unit. */
1551 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1553 struct lto_file_decl_data
*file_data
;
1554 unsigned int j
, h_ix
;
1555 gcov_unsigned_t max_runs
= 0;
1556 struct cgraph_node
*node
;
1557 struct cgraph_edge
*edge
;
1558 gcov_type saved_sum_all
= 0;
1559 gcov_ctr_summary
*saved_profile_info
= 0;
1560 int saved_scale
= 0;
1562 /* Find unit with maximal number of runs. If we ever get serious about
1563 roundoff errors, we might also consider computing smallest common
1565 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1566 if (max_runs
< file_data
->profile_info
.runs
)
1567 max_runs
= file_data
->profile_info
.runs
;
1572 /* Simple overflow check. We probably don't need to support that many train
1573 runs. Such a large value probably imply data corruption anyway. */
1574 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1576 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1577 INT_MAX
/ REG_BR_PROB_BASE
);
1581 profile_info
= <o_gcov_summary
;
1582 lto_gcov_summary
.runs
= max_runs
;
1583 lto_gcov_summary
.sum_max
= 0;
1584 memset (lto_gcov_summary
.histogram
, 0,
1585 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1587 /* Rescale all units to the maximal number of runs.
1588 sum_max can not be easily merged, as we have no idea what files come from
1589 the same run. We do not use the info anyway, so leave it 0. */
1590 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1591 if (file_data
->profile_info
.runs
)
1593 int scale
= GCOV_COMPUTE_SCALE (max_runs
,
1594 file_data
->profile_info
.runs
);
1595 lto_gcov_summary
.sum_max
1596 = MAX (lto_gcov_summary
.sum_max
,
1597 apply_scale (file_data
->profile_info
.sum_max
, scale
));
1598 lto_gcov_summary
.sum_all
1599 = MAX (lto_gcov_summary
.sum_all
,
1600 apply_scale (file_data
->profile_info
.sum_all
, scale
));
1601 /* Save a pointer to the profile_info with the largest
1602 scaled sum_all and the scale for use in merging the
1604 if (!saved_profile_info
1605 || lto_gcov_summary
.sum_all
> saved_sum_all
)
1607 saved_profile_info
= &file_data
->profile_info
;
1608 saved_sum_all
= lto_gcov_summary
.sum_all
;
1609 saved_scale
= scale
;
1613 gcc_assert (saved_profile_info
);
1615 /* Scale up the histogram from the profile that had the largest
1616 scaled sum_all above. */
1617 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1619 /* Scale up the min value as we did the corresponding sum_all
1620 above. Use that to find the new histogram index. */
1621 gcov_type scaled_min
1622 = apply_scale (saved_profile_info
->histogram
[h_ix
].min_value
,
1624 /* The new index may be shared with another scaled histogram entry,
1625 so we need to account for a non-zero histogram entry at new_ix. */
1626 unsigned new_ix
= gcov_histo_index (scaled_min
);
1627 lto_gcov_summary
.histogram
[new_ix
].min_value
1628 = (lto_gcov_summary
.histogram
[new_ix
].num_counters
1629 ? MIN (lto_gcov_summary
.histogram
[new_ix
].min_value
, scaled_min
)
1631 /* Some of the scaled counter values would ostensibly need to be placed
1632 into different (larger) histogram buckets, but we keep things simple
1633 here and place the scaled cumulative counter value in the bucket
1634 corresponding to the scaled minimum counter value. */
1635 lto_gcov_summary
.histogram
[new_ix
].cum_value
1636 += apply_scale (saved_profile_info
->histogram
[h_ix
].cum_value
,
1638 lto_gcov_summary
.histogram
[new_ix
].num_counters
1639 += saved_profile_info
->histogram
[h_ix
].num_counters
;
1642 /* Watch roundoff errors. */
1643 if (lto_gcov_summary
.sum_max
< max_runs
)
1644 lto_gcov_summary
.sum_max
= max_runs
;
1646 /* If merging already happent at WPA time, we are done. */
1650 /* Now compute count_materialization_scale of each node.
1651 During LTRANS we already have values of count_materialization_scale
1652 computed, so just update them. */
1653 FOR_EACH_FUNCTION (node
)
1654 if (node
->lto_file_data
1655 && node
->lto_file_data
->profile_info
.runs
)
1659 scale
= RDIV (node
->count_materialization_scale
* max_runs
,
1660 node
->lto_file_data
->profile_info
.runs
);
1661 node
->count_materialization_scale
= scale
;
1663 fatal_error ("Profile information in %s corrupted",
1664 file_data
->file_name
);
1666 if (scale
== REG_BR_PROB_BASE
)
1668 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1669 edge
->count
= apply_scale (edge
->count
, scale
);
1670 node
->count
= apply_scale (node
->count
, scale
);
1674 /* Input and merge the symtab from each of the .o files passed to
1680 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1681 struct lto_file_decl_data
*file_data
;
1683 struct cgraph_node
*node
;
1685 while ((file_data
= file_data_vec
[j
++]))
1689 struct lto_input_block
*ib
;
1690 vec
<symtab_node
*> nodes
;
1692 ib
= lto_create_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1695 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1696 input_profile_summary (ib
, file_data
);
1697 file_data
->symtab_node_encoder
= lto_symtab_encoder_new (true);
1698 nodes
= input_cgraph_1 (file_data
, ib
);
1699 lto_destroy_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1702 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1705 fatal_error ("cannot find LTO section refs in %s",
1706 file_data
->file_name
);
1707 input_refs (ib
, nodes
);
1708 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1711 input_cgraph_opt_summary (nodes
);
1715 merge_profile_summaries (file_data_vec
);
1716 get_working_sets ();
1719 /* Clear out the aux field that was used to store enough state to
1720 tell which nodes should be overwritten. */
1721 FOR_EACH_FUNCTION (node
)
1723 /* Some nodes may have been created by cgraph_node. This
1724 happens when the callgraph contains nested functions. If the
1725 node for the parent function was never emitted to the gimple
1726 file, cgraph_node will create a node for it when setting the
1727 context of the nested function. */
1728 if (node
->lto_file_data
)
1733 /* True when we need optimization summary for NODE. */
1736 output_cgraph_opt_summary_p (struct cgraph_node
*node
)
1738 return (node
->clone_of
1739 && (node
->clone
.tree_map
1740 || node
->clone
.args_to_skip
1741 || node
->clone
.combined_args_to_skip
));
1744 /* Output optimization summary for EDGE to OB. */
1746 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1747 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1751 /* Output optimization summary for NODE to OB. */
1754 output_node_opt_summary (struct output_block
*ob
,
1755 struct cgraph_node
*node
,
1756 lto_symtab_encoder_t encoder
)
1760 struct ipa_replace_map
*map
;
1761 struct bitpack_d bp
;
1763 struct cgraph_edge
*e
;
1765 if (node
->clone
.args_to_skip
)
1767 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1768 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1769 streamer_write_uhwi (ob
, index
);
1772 streamer_write_uhwi (ob
, 0);
1773 if (node
->clone
.combined_args_to_skip
)
1775 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1776 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1777 streamer_write_uhwi (ob
, index
);
1780 streamer_write_uhwi (ob
, 0);
1781 streamer_write_uhwi (ob
, vec_safe_length (node
->clone
.tree_map
));
1782 FOR_EACH_VEC_SAFE_ELT (node
->clone
.tree_map
, i
, map
)
1784 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1785 mechanism to store function local declarations into summaries. */
1786 gcc_assert (!map
->old_tree
);
1787 streamer_write_uhwi (ob
, map
->parm_num
);
1788 gcc_assert (EXPR_LOCATION (map
->new_tree
) == UNKNOWN_LOCATION
);
1789 stream_write_tree (ob
, map
->new_tree
, true);
1790 bp
= bitpack_create (ob
->main_stream
);
1791 bp_pack_value (&bp
, map
->replace_p
, 1);
1792 bp_pack_value (&bp
, map
->ref_p
, 1);
1793 streamer_write_bitpack (&bp
);
1796 if (lto_symtab_encoder_in_partition_p (encoder
, node
))
1798 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1799 output_edge_opt_summary (ob
, e
);
1800 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1801 output_edge_opt_summary (ob
, e
);
1805 /* Output optimization summaries stored in callgraph.
1806 At the moment it is the clone info structure. */
1809 output_cgraph_opt_summary (void)
1812 lto_symtab_encoder_t encoder
;
1813 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1817 encoder
= ob
->decl_state
->symtab_node_encoder
;
1818 n_nodes
= lto_symtab_encoder_size (encoder
);
1819 for (i
= 0; i
< n_nodes
; i
++)
1821 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1822 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1823 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1826 streamer_write_uhwi (ob
, count
);
1827 for (i
= 0; i
< n_nodes
; i
++)
1829 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1830 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1831 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1833 streamer_write_uhwi (ob
, i
);
1834 output_node_opt_summary (ob
, cnode
, encoder
);
1837 produce_asm (ob
, NULL
);
1838 destroy_output_block (ob
);
1841 /* Input optimisation summary of EDGE. */
1844 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1845 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1849 /* Input optimisation summary of NODE. */
1852 input_node_opt_summary (struct cgraph_node
*node
,
1853 struct lto_input_block
*ib_main
,
1854 struct data_in
*data_in
)
1859 struct bitpack_d bp
;
1860 struct cgraph_edge
*e
;
1862 count
= streamer_read_uhwi (ib_main
);
1864 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1865 for (i
= 0; i
< count
; i
++)
1867 bit
= streamer_read_uhwi (ib_main
);
1868 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1870 count
= streamer_read_uhwi (ib_main
);
1872 node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
1873 for (i
= 0; i
< count
; i
++)
1875 bit
= streamer_read_uhwi (ib_main
);
1876 bitmap_set_bit (node
->clone
.combined_args_to_skip
, bit
);
1878 count
= streamer_read_uhwi (ib_main
);
1879 for (i
= 0; i
< count
; i
++)
1881 struct ipa_replace_map
*map
= ggc_alloc
<ipa_replace_map
> ();
1883 vec_safe_push (node
->clone
.tree_map
, map
);
1884 map
->parm_num
= streamer_read_uhwi (ib_main
);
1885 map
->old_tree
= NULL
;
1886 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1887 bp
= streamer_read_bitpack (ib_main
);
1888 map
->replace_p
= bp_unpack_value (&bp
, 1);
1889 map
->ref_p
= bp_unpack_value (&bp
, 1);
1891 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1892 input_edge_opt_summary (e
, ib_main
);
1893 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1894 input_edge_opt_summary (e
, ib_main
);
1897 /* Read section in file FILE_DATA of length LEN with data DATA. */
1900 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1901 const char *data
, size_t len
,
1902 vec
<symtab_node
*> nodes
)
1904 const struct lto_function_header
*header
=
1905 (const struct lto_function_header
*) data
;
1906 const int cfg_offset
= sizeof (struct lto_function_header
);
1907 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1908 const int string_offset
= main_offset
+ header
->main_size
;
1909 struct data_in
*data_in
;
1913 lto_input_block
ib_main ((const char *) data
+ main_offset
,
1917 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1918 header
->string_size
, vNULL
);
1919 count
= streamer_read_uhwi (&ib_main
);
1921 for (i
= 0; i
< count
; i
++)
1923 int ref
= streamer_read_uhwi (&ib_main
);
1924 input_node_opt_summary (dyn_cast
<cgraph_node
*> (nodes
[ref
]),
1927 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1929 lto_data_in_delete (data_in
);
1932 /* Input optimization summary of cgraph. */
1935 input_cgraph_opt_summary (vec
<symtab_node
*> nodes
)
1937 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1938 struct lto_file_decl_data
*file_data
;
1941 while ((file_data
= file_data_vec
[j
++]))
1945 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1949 input_cgraph_opt_section (file_data
, data
, len
, nodes
);