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
;
399 boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
401 if (node
->analyzed
&& !boundary_p
)
402 tag
= LTO_symtab_analyzed_node
;
404 tag
= LTO_symtab_unavail_node
;
406 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
408 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
410 /* In WPA mode, we only output part of the call-graph. Also, we
411 fake cgraph node attributes. There are two cases that we care.
413 Boundary nodes: There are nodes that are not part of SET but are
414 called from within SET. We artificially make them look like
415 externally visible nodes with no function body.
417 Cherry-picked nodes: These are nodes we pulled from other
418 translation units into SET during IPA-inlining. We make them as
419 local static nodes to prevent clashes with other local statics. */
420 if (boundary_p
&& node
->analyzed
421 && symtab_get_symbol_partitioning_class (node
) == SYMBOL_PARTITION
)
423 /* Inline clones can not be part of boundary.
424 gcc_assert (!node->global.inlined_to);
426 FIXME: At the moment they can be, when partition contains an inline
427 clone that is clone of inline clone from outside partition. We can
428 reshape the clone tree and make other tree to be the root, but it
429 needs a bit extra work and will be promplty done by cgraph_remove_node
430 after reading back. */
431 in_other_partition
= 1;
434 clone_of
= node
->clone_of
;
436 && (ref
= lto_symtab_encoder_lookup (encoder
, clone_of
)) == LCC_NOT_FOUND
)
437 if (clone_of
->prev_sibling_clone
)
438 clone_of
= clone_of
->prev_sibling_clone
;
440 clone_of
= clone_of
->clone_of
;
442 /* See if body of the master function is output. If not, we are seeing only
443 an declaration and we do not need to pass down clone tree. */
444 ultimate_clone_of
= clone_of
;
445 while (ultimate_clone_of
&& ultimate_clone_of
->clone_of
)
446 ultimate_clone_of
= ultimate_clone_of
->clone_of
;
448 if (clone_of
&& !lto_symtab_encoder_encode_body_p (encoder
, ultimate_clone_of
))
451 if (tag
== LTO_symtab_analyzed_node
)
452 gcc_assert (clone_of
|| !node
->clone_of
);
454 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
456 streamer_write_hwi_stream (ob
->main_stream
, ref
);
459 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
460 streamer_write_gcov_count_stream (ob
->main_stream
, node
->count
);
461 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
463 streamer_write_hwi_stream (ob
->main_stream
,
464 node
->ipa_transforms_to_apply
.length ());
465 FOR_EACH_VEC_ELT (node
->ipa_transforms_to_apply
, i
, pass
)
466 streamer_write_hwi_stream (ob
->main_stream
, pass
->static_pass_number
);
468 if (tag
== LTO_symtab_analyzed_node
)
470 if (node
->global
.inlined_to
)
472 ref
= lto_symtab_encoder_lookup (encoder
, node
->global
.inlined_to
);
473 gcc_assert (ref
!= LCC_NOT_FOUND
);
478 streamer_write_hwi_stream (ob
->main_stream
, ref
);
481 if (node
->same_comdat_group
&& !boundary_p
)
483 ref
= lto_symtab_encoder_lookup (encoder
,
484 node
->same_comdat_group
);
485 gcc_assert (ref
!= LCC_NOT_FOUND
);
489 streamer_write_hwi_stream (ob
->main_stream
, ref
);
491 streamer_write_hwi_stream (ob
->main_stream
, node
->tp_first_run
);
493 bp
= bitpack_create (ob
->main_stream
);
494 bp_pack_value (&bp
, node
->local
.local
, 1);
495 bp_pack_value (&bp
, node
->externally_visible
, 1);
496 bp_pack_value (&bp
, node
->definition
, 1);
497 bp_pack_value (&bp
, node
->local
.versionable
, 1);
498 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
499 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
500 bp_pack_value (&bp
, node
->force_output
, 1);
501 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
502 bp_pack_value (&bp
, node
->unique_name
, 1);
503 bp_pack_value (&bp
, node
->body_removed
, 1);
504 bp_pack_value (&bp
, node
->address_taken
, 1);
505 bp_pack_value (&bp
, tag
== LTO_symtab_analyzed_node
506 && symtab_get_symbol_partitioning_class (node
) == SYMBOL_PARTITION
507 && (reachable_from_other_partition_p (node
, encoder
)
508 || referenced_from_other_partition_p (&node
->ref_list
,
510 bp_pack_value (&bp
, node
->lowered
, 1);
511 bp_pack_value (&bp
, in_other_partition
, 1);
512 /* Real aliases in a boundary become non-aliases. However we still stream
513 alias info on weakrefs.
514 TODO: We lose a bit of information here - when we know that variable is
515 defined in other unit, we may use the info on aliases to resolve
516 symbol1 != symbol2 type tests that we can do only for locally defined objects
518 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
519 bp_pack_value (&bp
, alias_p
, 1);
520 bp_pack_value (&bp
, node
->weakref
, 1);
521 bp_pack_value (&bp
, node
->frequency
, 2);
522 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
523 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
524 bp_pack_value (&bp
, node
->tm_clone
, 1);
525 bp_pack_value (&bp
, node
->calls_comdat_local
, 1);
526 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
527 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
528 LDPR_NUM_KNOWN
, node
->resolution
);
529 streamer_write_bitpack (&bp
);
531 if (node
->thunk
.thunk_p
&& !boundary_p
)
533 streamer_write_uhwi_stream
535 1 + (node
->thunk
.this_adjusting
!= 0) * 2
536 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
537 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
538 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
540 streamer_write_hwi_stream (ob
->main_stream
, node
->profile_id
);
543 /* Output the varpool NODE to OB.
544 If NODE is not in SET, then NODE is a boundary. */
547 lto_output_varpool_node (struct lto_simple_output_block
*ob
, varpool_node
*node
,
548 lto_symtab_encoder_t encoder
)
550 bool boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
555 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
556 LTO_symtab_variable
);
557 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
558 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
559 bp
= bitpack_create (ob
->main_stream
);
560 bp_pack_value (&bp
, node
->externally_visible
, 1);
561 bp_pack_value (&bp
, node
->force_output
, 1);
562 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
563 bp_pack_value (&bp
, node
->unique_name
, 1);
564 bp_pack_value (&bp
, node
->body_removed
, 1);
565 bp_pack_value (&bp
, node
->definition
, 1);
566 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
567 bp_pack_value (&bp
, alias_p
, 1);
568 bp_pack_value (&bp
, node
->weakref
, 1);
569 bp_pack_value (&bp
, node
->analyzed
&& !boundary_p
, 1);
570 gcc_assert (node
->definition
|| !node
->analyzed
);
571 /* Constant pool initializers can be de-unified into individual ltrans units.
572 FIXME: Alternatively at -Os we may want to avoid generating for them the local
573 labels and share them across LTRANS partitions. */
574 if (symtab_get_symbol_partitioning_class (node
) != SYMBOL_PARTITION
)
576 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
577 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
581 bp_pack_value (&bp
, node
->definition
582 && referenced_from_other_partition_p (&node
->ref_list
,
584 bp_pack_value (&bp
, node
->analyzed
585 && boundary_p
&& !DECL_EXTERNAL (node
->decl
), 1);
586 /* in_other_partition. */
588 streamer_write_bitpack (&bp
);
589 if (node
->same_comdat_group
&& !boundary_p
)
591 ref
= lto_symtab_encoder_lookup (encoder
,
592 node
->same_comdat_group
);
593 gcc_assert (ref
!= LCC_NOT_FOUND
);
597 streamer_write_hwi_stream (ob
->main_stream
, ref
);
598 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
599 LDPR_NUM_KNOWN
, node
->resolution
);
602 /* Output the varpool NODE to OB.
603 If NODE is not in SET, then NODE is a boundary. */
606 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
607 lto_symtab_encoder_t encoder
)
611 int uid
= ref
->lto_stmt_uid
;
612 struct cgraph_node
*node
;
614 bp
= bitpack_create (ob
->main_stream
);
615 bp_pack_value (&bp
, ref
->use
, 2);
616 bp_pack_value (&bp
, ref
->speculative
, 1);
617 streamer_write_bitpack (&bp
);
618 nref
= lto_symtab_encoder_lookup (encoder
, ref
->referred
);
619 gcc_assert (nref
!= LCC_NOT_FOUND
);
620 streamer_write_hwi_stream (ob
->main_stream
, nref
);
622 node
= dyn_cast
<cgraph_node
*> (ref
->referring
);
626 uid
= gimple_uid (ref
->stmt
) + 1;
627 streamer_write_hwi_stream (ob
->main_stream
, uid
);
631 /* Stream out profile_summary to OB. */
634 output_profile_summary (struct lto_simple_output_block
*ob
)
641 /* We do not output num and run_max, they are not used by
642 GCC profile feedback and they are difficult to merge from multiple
644 gcc_assert (profile_info
->runs
);
645 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
646 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_max
);
648 /* sum_all is needed for computing the working set with the
650 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_all
);
652 /* Create and output a bitpack of non-zero histogram entries indices. */
653 bp
= bitpack_create (ob
->main_stream
);
654 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
655 bp_pack_value (&bp
, profile_info
->histogram
[h_ix
].num_counters
> 0, 1);
656 streamer_write_bitpack (&bp
);
657 /* Now stream out only those non-zero entries. */
658 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
660 if (!profile_info
->histogram
[h_ix
].num_counters
)
662 streamer_write_gcov_count_stream (ob
->main_stream
,
663 profile_info
->histogram
[h_ix
].num_counters
);
664 streamer_write_gcov_count_stream (ob
->main_stream
,
665 profile_info
->histogram
[h_ix
].min_value
);
666 streamer_write_gcov_count_stream (ob
->main_stream
,
667 profile_info
->histogram
[h_ix
].cum_value
);
669 /* IPA-profile computes hot bb threshold based on cumulated
670 whole program profile. We need to stream it down to ltrans. */
672 streamer_write_gcov_count_stream (ob
->main_stream
,
673 get_hot_bb_threshold ());
676 streamer_write_uhwi_stream (ob
->main_stream
, 0);
679 /* Output all callees or indirect outgoing edges. EDGE must be the first such
683 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
684 struct lto_simple_output_block
*ob
,
685 lto_symtab_encoder_t encoder
)
690 /* Output edges in backward direction, so the reconstructed callgraph match
691 and it is easy to associate call sites in the IPA pass summaries. */
692 while (edge
->next_callee
)
693 edge
= edge
->next_callee
;
694 for (; edge
; edge
= edge
->prev_callee
)
695 lto_output_edge (ob
, edge
, encoder
);
698 /* Output the part of the cgraph in SET. */
701 output_refs (lto_symtab_encoder_t encoder
)
703 lto_symtab_encoder_iterator lsei
;
704 struct lto_simple_output_block
*ob
;
709 ob
= lto_create_simple_output_block (LTO_section_refs
);
711 for (lsei
= lsei_start_in_partition (encoder
); !lsei_end_p (lsei
);
712 lsei_next_in_partition (&lsei
))
714 symtab_node
*node
= lsei_node (lsei
);
716 count
= ipa_ref_list_nreferences (&node
->ref_list
);
719 streamer_write_gcov_count_stream (ob
->main_stream
, count
);
720 streamer_write_uhwi_stream (ob
->main_stream
,
721 lto_symtab_encoder_lookup (encoder
, node
));
722 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
,
724 lto_output_ref (ob
, ref
, encoder
);
728 streamer_write_uhwi_stream (ob
->main_stream
, 0);
730 lto_destroy_simple_output_block (ob
);
733 /* Add NODE into encoder as well as nodes it is cloned from.
734 Do it in a way so clones appear first. */
737 add_node_to (lto_symtab_encoder_t encoder
, struct cgraph_node
*node
,
741 add_node_to (encoder
, node
->clone_of
, include_body
);
742 else if (include_body
)
743 lto_set_symtab_encoder_encode_body (encoder
, node
);
744 lto_symtab_encoder_encode (encoder
, node
);
747 /* Add all references in LIST to encoders. */
750 add_references (lto_symtab_encoder_t encoder
,
751 struct ipa_ref_list
*list
)
755 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
756 if (is_a
<cgraph_node
*> (ref
->referred
))
757 add_node_to (encoder
, ipa_ref_node (ref
), false);
759 lto_symtab_encoder_encode (encoder
, ref
->referred
);
762 /* Find all symbols we want to stream into given partition and insert them
765 The function actually replaces IN_ENCODER by new one. The reason is that
766 streaming code needs clone's origin to be streamed before clone. This
767 means that we need to insert the nodes in specific order. This order is
768 ignored by the partitioning logic earlier. */
771 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder
)
773 struct cgraph_edge
*edge
;
775 lto_symtab_encoder_t encoder
;
776 lto_symtab_encoder_iterator lsei
;
777 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
779 encoder
= lto_symtab_encoder_new (false);
781 /* Go over all entries in the IN_ENCODER and duplicate them to
782 ENCODER. At the same time insert masters of clones so
783 every master appears before clone. */
784 for (lsei
= lsei_start_function_in_partition (in_encoder
);
785 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
787 struct cgraph_node
*node
= lsei_cgraph_node (lsei
);
788 add_node_to (encoder
, node
, true);
789 lto_set_symtab_encoder_in_partition (encoder
, node
);
790 add_references (encoder
, &node
->ref_list
);
791 /* For proper debug info, we need to ship the origins, too. */
792 if (DECL_ABSTRACT_ORIGIN (node
->decl
))
794 struct cgraph_node
*origin_node
795 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node
->decl
));
796 add_node_to (encoder
, origin_node
, true);
799 for (lsei
= lsei_start_variable_in_partition (in_encoder
);
800 !lsei_end_p (lsei
); lsei_next_variable_in_partition (&lsei
))
802 varpool_node
*vnode
= lsei_varpool_node (lsei
);
804 lto_set_symtab_encoder_in_partition (encoder
, vnode
);
805 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
806 add_references (encoder
, &vnode
->ref_list
);
807 /* For proper debug info, we need to ship the origins, too. */
808 if (DECL_ABSTRACT_ORIGIN (vnode
->decl
))
810 varpool_node
*origin_node
811 = varpool_get_node (DECL_ABSTRACT_ORIGIN (vnode
->decl
));
812 lto_set_symtab_encoder_in_partition (encoder
, origin_node
);
815 /* Pickle in also the initializer of all referenced readonly variables
816 to help folding. Constant pool variables are not shared, so we must
818 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
820 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
821 if (varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
))
823 if (!lto_symtab_encoder_encode_initializer_p (encoder
,
825 && ctor_for_folding (vnode
->decl
) != error_mark_node
)
827 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
828 add_references (encoder
, &vnode
->ref_list
);
833 /* Go over all the nodes again to include callees that are not in
835 for (lsei
= lsei_start_function_in_partition (encoder
);
836 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
838 struct cgraph_node
*node
= lsei_cgraph_node (lsei
);
839 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
841 struct cgraph_node
*callee
= edge
->callee
;
842 if (!lto_symtab_encoder_in_partition_p (encoder
, callee
))
844 /* We should have moved all the inlines. */
845 gcc_assert (!callee
->global
.inlined_to
);
846 add_node_to (encoder
, callee
, false);
849 /* Add all possible targets for late devirtualization. */
850 if (flag_devirtualize
)
851 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
852 if (edge
->indirect_info
->polymorphic
)
857 vec
<cgraph_node
*>targets
858 = possible_polymorphic_call_targets
859 (edge
, &final
, &cache_token
);
860 if (!pointer_set_insert (reachable_call_targets
,
863 for (i
= 0; i
< targets
.length (); i
++)
865 struct cgraph_node
*callee
= targets
[i
];
867 /* Adding an external declarations into the unit serves
868 no purpose and just increases its boundary. */
869 if (callee
->definition
870 && !lto_symtab_encoder_in_partition_p
873 gcc_assert (!callee
->global
.inlined_to
);
874 add_node_to (encoder
, callee
, false);
880 lto_symtab_encoder_delete (in_encoder
);
881 pointer_set_destroy (reachable_call_targets
);
885 /* Output the part of the symtab in SET and VSET. */
890 struct cgraph_node
*node
;
891 struct lto_simple_output_block
*ob
;
892 lto_symtab_encoder_iterator lsei
;
894 lto_symtab_encoder_t encoder
;
897 output_cgraph_opt_summary ();
899 ob
= lto_create_simple_output_block (LTO_section_symtab_nodes
);
901 output_profile_summary (ob
);
903 /* An encoder for cgraph nodes should have been created by
904 ipa_write_summaries_1. */
905 gcc_assert (ob
->decl_state
->symtab_node_encoder
);
906 encoder
= ob
->decl_state
->symtab_node_encoder
;
908 /* Write out the nodes. We must first output a node and then its clones,
909 otherwise at a time reading back the node there would be nothing to clone
911 n_nodes
= lto_symtab_encoder_size (encoder
);
912 for (i
= 0; i
< n_nodes
; i
++)
914 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
915 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
916 lto_output_node (ob
, cnode
, encoder
);
918 lto_output_varpool_node (ob
, varpool (node
), encoder
);
922 /* Go over the nodes in SET again to write edges. */
923 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
924 lsei_next_function_in_partition (&lsei
))
926 node
= lsei_cgraph_node (lsei
);
927 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
928 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
931 streamer_write_uhwi_stream (ob
->main_stream
, 0);
933 lto_destroy_simple_output_block (ob
);
935 /* Emit toplevel asms.
936 When doing WPA we must output every asm just once. Since we do not partition asm
937 nodes at all, output them to first output. This is kind of hack, but should work
939 if (!asm_nodes_output
)
941 asm_nodes_output
= true;
942 lto_output_toplevel_asms ();
945 output_refs (encoder
);
948 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
949 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
950 NODE or to replace the values in it, for instance because the first
951 time we saw it, the function body was not available but now it
952 is. BP is a bitpack with all the bitflags for NODE read from the
956 input_overwrite_node (struct lto_file_decl_data
*file_data
,
957 struct cgraph_node
*node
,
958 enum LTO_symtab_tags tag
,
959 struct bitpack_d
*bp
)
961 node
->aux
= (void *) tag
;
962 node
->lto_file_data
= file_data
;
964 node
->local
.local
= bp_unpack_value (bp
, 1);
965 node
->externally_visible
= bp_unpack_value (bp
, 1);
966 node
->definition
= bp_unpack_value (bp
, 1);
967 node
->local
.versionable
= bp_unpack_value (bp
, 1);
968 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
969 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
970 node
->force_output
= bp_unpack_value (bp
, 1);
971 node
->forced_by_abi
= bp_unpack_value (bp
, 1);
972 node
->unique_name
= bp_unpack_value (bp
, 1);
973 node
->body_removed
= bp_unpack_value (bp
, 1);
974 node
->address_taken
= bp_unpack_value (bp
, 1);
975 node
->used_from_other_partition
= bp_unpack_value (bp
, 1);
976 node
->lowered
= bp_unpack_value (bp
, 1);
977 node
->analyzed
= tag
== LTO_symtab_analyzed_node
;
978 node
->in_other_partition
= bp_unpack_value (bp
, 1);
979 if (node
->in_other_partition
980 /* Avoid updating decl when we are seeing just inline clone.
981 When inlining function that has functions already inlined into it,
982 we produce clones of inline clones.
984 WPA partitioning might put each clone into different unit and
985 we might end up streaming inline clone from other partition
986 to support clone we are interested in. */
988 || node
->clone_of
->decl
!= node
->decl
))
990 DECL_EXTERNAL (node
->decl
) = 1;
991 TREE_STATIC (node
->decl
) = 0;
993 node
->alias
= bp_unpack_value (bp
, 1);
994 node
->weakref
= bp_unpack_value (bp
, 1);
995 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
996 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
997 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
998 node
->tm_clone
= bp_unpack_value (bp
, 1);
999 node
->calls_comdat_local
= bp_unpack_value (bp
, 1);
1000 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
1001 node
->resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
1003 gcc_assert (flag_ltrans
1004 || (!node
->in_other_partition
1005 && !node
->used_from_other_partition
));
1008 /* Return string alias is alias of. */
1011 get_alias_symbol (tree decl
)
1013 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1014 return get_identifier (TREE_STRING_POINTER
1015 (TREE_VALUE (TREE_VALUE (alias
))));
1018 /* Read a node from input_block IB. TAG is the node's tag just read.
1019 Return the node read or overwriten. */
1021 static struct cgraph_node
*
1022 input_node (struct lto_file_decl_data
*file_data
,
1023 struct lto_input_block
*ib
,
1024 enum LTO_symtab_tags tag
,
1025 vec
<symtab_node
*> nodes
)
1027 gcc::pass_manager
*passes
= g
->get_passes ();
1029 struct cgraph_node
*node
;
1030 struct bitpack_d bp
;
1031 unsigned decl_index
;
1032 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
1037 order
= streamer_read_hwi (ib
) + order_base
;
1038 clone_ref
= streamer_read_hwi (ib
);
1040 decl_index
= streamer_read_uhwi (ib
);
1041 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
1043 if (clone_ref
!= LCC_NOT_FOUND
)
1045 node
= cgraph_clone_node (cgraph (nodes
[clone_ref
]), fn_decl
,
1046 0, CGRAPH_FREQ_BASE
, false,
1047 vNULL
, false, NULL
, NULL
);
1051 /* Declaration of functions can be already merged with a declaration
1052 from other input file. We keep cgraph unmerged until after streaming
1053 of ipa passes is done. Alays forcingly create a fresh node. */
1054 node
= cgraph_create_empty_node ();
1055 node
->decl
= fn_decl
;
1056 symtab_register_node (node
);
1059 node
->order
= order
;
1060 if (order
>= symtab_order
)
1061 symtab_order
= order
+ 1;
1063 node
->count
= streamer_read_gcov_count (ib
);
1064 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1066 count
= streamer_read_hwi (ib
);
1067 node
->ipa_transforms_to_apply
= vNULL
;
1068 for (i
= 0; i
< count
; i
++)
1071 int pid
= streamer_read_hwi (ib
);
1073 gcc_assert (pid
< passes
->passes_by_id_size
);
1074 pass
= passes
->passes_by_id
[pid
];
1075 node
->ipa_transforms_to_apply
.safe_push ((ipa_opt_pass_d
*) pass
);
1078 if (tag
== LTO_symtab_analyzed_node
)
1079 ref
= streamer_read_hwi (ib
);
1081 ref2
= streamer_read_hwi (ib
);
1083 /* Make sure that we have not read this node before. Nodes that
1084 have already been read will have their tag stored in the 'aux'
1085 field. Since built-in functions can be referenced in multiple
1086 functions, they are expected to be read more than once. */
1087 if (node
->aux
&& !DECL_BUILT_IN (node
->decl
))
1088 internal_error ("bytecode stream: found multiple instances of cgraph "
1089 "node with uid %d", node
->uid
);
1091 node
->tp_first_run
= streamer_read_uhwi (ib
);
1093 bp
= streamer_read_bitpack (ib
);
1095 input_overwrite_node (file_data
, node
, tag
, &bp
);
1097 /* Store a reference for now, and fix up later to be a pointer. */
1098 node
->global
.inlined_to
= (cgraph_node_ptr
) (intptr_t) ref
;
1100 /* Store a reference for now, and fix up later to be a pointer. */
1101 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref2
;
1103 if (node
->thunk
.thunk_p
)
1105 int type
= streamer_read_uhwi (ib
);
1106 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1107 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1109 node
->thunk
.fixed_offset
= fixed_offset
;
1110 node
->thunk
.this_adjusting
= (type
& 2);
1111 node
->thunk
.virtual_value
= virtual_value
;
1112 node
->thunk
.virtual_offset_p
= (type
& 4);
1114 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1115 node
->alias_target
= get_alias_symbol (node
->decl
);
1116 node
->profile_id
= streamer_read_hwi (ib
);
1120 /* Read a node from input_block IB. TAG is the node's tag just read.
1121 Return the node read or overwriten. */
1123 static varpool_node
*
1124 input_varpool_node (struct lto_file_decl_data
*file_data
,
1125 struct lto_input_block
*ib
)
1130 struct bitpack_d bp
;
1131 int ref
= LCC_NOT_FOUND
;
1134 order
= streamer_read_hwi (ib
) + order_base
;
1135 decl_index
= streamer_read_uhwi (ib
);
1136 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1138 /* Declaration of functions can be already merged with a declaration
1139 from other input file. We keep cgraph unmerged until after streaming
1140 of ipa passes is done. Alays forcingly create a fresh node. */
1141 node
= varpool_create_empty_node ();
1142 node
->decl
= var_decl
;
1143 symtab_register_node (node
);
1145 node
->order
= order
;
1146 if (order
>= symtab_order
)
1147 symtab_order
= order
+ 1;
1148 node
->lto_file_data
= file_data
;
1150 bp
= streamer_read_bitpack (ib
);
1151 node
->externally_visible
= bp_unpack_value (&bp
, 1);
1152 node
->force_output
= bp_unpack_value (&bp
, 1);
1153 node
->forced_by_abi
= bp_unpack_value (&bp
, 1);
1154 node
->unique_name
= bp_unpack_value (&bp
, 1);
1155 node
->body_removed
= bp_unpack_value (&bp
, 1);
1156 node
->definition
= bp_unpack_value (&bp
, 1);
1157 node
->alias
= bp_unpack_value (&bp
, 1);
1158 node
->weakref
= bp_unpack_value (&bp
, 1);
1159 node
->analyzed
= bp_unpack_value (&bp
, 1);
1160 node
->used_from_other_partition
= bp_unpack_value (&bp
, 1);
1161 node
->in_other_partition
= bp_unpack_value (&bp
, 1);
1162 if (node
->in_other_partition
)
1164 DECL_EXTERNAL (node
->decl
) = 1;
1165 TREE_STATIC (node
->decl
) = 0;
1167 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1168 node
->alias_target
= get_alias_symbol (node
->decl
);
1169 ref
= streamer_read_hwi (ib
);
1170 /* Store a reference for now, and fix up later to be a pointer. */
1171 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref
;
1172 node
->resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1174 gcc_assert (flag_ltrans
1175 || (!node
->in_other_partition
1176 && !node
->used_from_other_partition
));
1181 /* Read a node from input_block IB. TAG is the node's tag just read.
1182 Return the node read or overwriten. */
1185 input_ref (struct lto_input_block
*ib
,
1186 symtab_node
*referring_node
,
1187 vec
<symtab_node
*> nodes
)
1189 symtab_node
*node
= NULL
;
1190 struct bitpack_d bp
;
1191 enum ipa_ref_use use
;
1193 struct ipa_ref
*ref
;
1195 bp
= streamer_read_bitpack (ib
);
1196 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1197 speculative
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 1);
1198 node
= nodes
[streamer_read_hwi (ib
)];
1199 ref
= ipa_record_reference (referring_node
, node
, use
, NULL
);
1200 ref
->speculative
= speculative
;
1201 if (is_a
<cgraph_node
*> (referring_node
))
1202 ref
->lto_stmt_uid
= streamer_read_hwi (ib
);
1205 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1206 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1207 edge being read is indirect (in the sense that it has
1208 indirect_unknown_callee set). */
1211 input_edge (struct lto_input_block
*ib
, vec
<symtab_node
*> nodes
,
1214 struct cgraph_node
*caller
, *callee
;
1215 struct cgraph_edge
*edge
;
1216 unsigned int stmt_id
;
1219 cgraph_inline_failed_t inline_failed
;
1220 struct bitpack_d bp
;
1223 caller
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1224 if (caller
== NULL
|| caller
->decl
== NULL_TREE
)
1225 internal_error ("bytecode stream: no caller found while reading edge");
1229 callee
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1230 if (callee
== NULL
|| callee
->decl
== NULL_TREE
)
1231 internal_error ("bytecode stream: no callee found while reading edge");
1236 count
= streamer_read_gcov_count (ib
);
1238 bp
= streamer_read_bitpack (ib
);
1239 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_t
, CIF_N_REASONS
);
1240 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1241 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1244 edge
= cgraph_create_indirect_edge (caller
, NULL
, 0, count
, freq
);
1246 edge
= cgraph_create_edge (caller
, callee
, NULL
, count
, freq
);
1248 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1249 edge
->speculative
= bp_unpack_value (&bp
, 1);
1250 edge
->lto_stmt_uid
= stmt_id
;
1251 edge
->inline_failed
= inline_failed
;
1252 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1253 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1256 if (bp_unpack_value (&bp
, 1))
1257 ecf_flags
|= ECF_CONST
;
1258 if (bp_unpack_value (&bp
, 1))
1259 ecf_flags
|= ECF_PURE
;
1260 if (bp_unpack_value (&bp
, 1))
1261 ecf_flags
|= ECF_NORETURN
;
1262 if (bp_unpack_value (&bp
, 1))
1263 ecf_flags
|= ECF_MALLOC
;
1264 if (bp_unpack_value (&bp
, 1))
1265 ecf_flags
|= ECF_NOTHROW
;
1266 if (bp_unpack_value (&bp
, 1))
1267 ecf_flags
|= ECF_RETURNS_TWICE
;
1268 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1269 edge
->indirect_info
->common_target_id
= streamer_read_hwi (ib
);
1270 if (edge
->indirect_info
->common_target_id
)
1271 edge
->indirect_info
->common_target_probability
= streamer_read_hwi (ib
);
1276 /* Read a cgraph from IB using the info in FILE_DATA. */
1278 static vec
<symtab_node
*>
1279 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1280 struct lto_input_block
*ib
)
1282 enum LTO_symtab_tags tag
;
1283 vec
<symtab_node
*> nodes
= vNULL
;
1287 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1288 order_base
= symtab_order
;
1291 if (tag
== LTO_symtab_edge
)
1292 input_edge (ib
, nodes
, false);
1293 else if (tag
== LTO_symtab_indirect_edge
)
1294 input_edge (ib
, nodes
, true);
1295 else if (tag
== LTO_symtab_variable
)
1297 node
= input_varpool_node (file_data
, ib
);
1298 nodes
.safe_push (node
);
1299 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1303 node
= input_node (file_data
, ib
, tag
, nodes
);
1304 if (node
== NULL
|| node
->decl
== NULL_TREE
)
1305 internal_error ("bytecode stream: found empty cgraph node");
1306 nodes
.safe_push (node
);
1307 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1310 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1313 lto_input_toplevel_asms (file_data
, order_base
);
1315 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1316 #ifdef ENABLE_CHECKING
1317 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1318 gcc_assert (node
->aux
|| !is_a
<cgraph_node
*> (node
));
1320 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1323 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1325 ref
= (int) (intptr_t) cnode
->global
.inlined_to
;
1327 /* We share declaration of builtins, so we may read same node twice. */
1332 /* Fixup inlined_to from reference to pointer. */
1333 if (ref
!= LCC_NOT_FOUND
)
1334 cgraph (node
)->global
.inlined_to
= cgraph (nodes
[ref
]);
1336 cnode
->global
.inlined_to
= NULL
;
1339 ref
= (int) (intptr_t) node
->same_comdat_group
;
1341 /* Fixup same_comdat_group from reference to pointer. */
1342 if (ref
!= LCC_NOT_FOUND
)
1343 node
->same_comdat_group
= nodes
[ref
];
1345 node
->same_comdat_group
= NULL
;
1347 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1348 node
->aux
= is_a
<cgraph_node
*> (node
) ? (void *)1 : NULL
;
1352 /* Input ipa_refs. */
1355 input_refs (struct lto_input_block
*ib
,
1356 vec
<symtab_node
*> nodes
)
1363 count
= streamer_read_uhwi (ib
);
1366 idx
= streamer_read_uhwi (ib
);
1370 input_ref (ib
, node
, nodes
);
1377 static struct gcov_ctr_summary lto_gcov_summary
;
1379 /* Input profile_info from IB. */
1381 input_profile_summary (struct lto_input_block
*ib
,
1382 struct lto_file_decl_data
*file_data
)
1385 struct bitpack_d bp
;
1386 unsigned int runs
= streamer_read_uhwi (ib
);
1389 file_data
->profile_info
.runs
= runs
;
1390 file_data
->profile_info
.sum_max
= streamer_read_gcov_count (ib
);
1391 file_data
->profile_info
.sum_all
= streamer_read_gcov_count (ib
);
1393 memset (file_data
->profile_info
.histogram
, 0,
1394 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1395 /* Input the bitpack of non-zero histogram indices. */
1396 bp
= streamer_read_bitpack (ib
);
1397 /* Read in and unpack the full bitpack, flagging non-zero
1398 histogram entries by setting the num_counters non-zero. */
1399 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1401 file_data
->profile_info
.histogram
[h_ix
].num_counters
1402 = bp_unpack_value (&bp
, 1);
1404 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1406 if (!file_data
->profile_info
.histogram
[h_ix
].num_counters
)
1409 file_data
->profile_info
.histogram
[h_ix
].num_counters
1410 = streamer_read_gcov_count (ib
);
1411 file_data
->profile_info
.histogram
[h_ix
].min_value
1412 = streamer_read_gcov_count (ib
);
1413 file_data
->profile_info
.histogram
[h_ix
].cum_value
1414 = streamer_read_gcov_count (ib
);
1416 /* IPA-profile computes hot bb threshold based on cumulated
1417 whole program profile. We need to stream it down to ltrans. */
1419 set_hot_bb_threshold (streamer_read_gcov_count (ib
));
1424 /* Rescale profile summaries to the same number of runs in the whole unit. */
1427 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1429 struct lto_file_decl_data
*file_data
;
1430 unsigned int j
, h_ix
;
1431 gcov_unsigned_t max_runs
= 0;
1432 struct cgraph_node
*node
;
1433 struct cgraph_edge
*edge
;
1434 gcov_type saved_sum_all
= 0;
1435 gcov_ctr_summary
*saved_profile_info
= 0;
1436 int saved_scale
= 0;
1438 /* Find unit with maximal number of runs. If we ever get serious about
1439 roundoff errors, we might also consider computing smallest common
1441 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1442 if (max_runs
< file_data
->profile_info
.runs
)
1443 max_runs
= file_data
->profile_info
.runs
;
1448 /* Simple overflow check. We probably don't need to support that many train
1449 runs. Such a large value probably imply data corruption anyway. */
1450 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1452 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1453 INT_MAX
/ REG_BR_PROB_BASE
);
1457 profile_info
= <o_gcov_summary
;
1458 lto_gcov_summary
.runs
= max_runs
;
1459 lto_gcov_summary
.sum_max
= 0;
1460 memset (lto_gcov_summary
.histogram
, 0,
1461 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1463 /* Rescale all units to the maximal number of runs.
1464 sum_max can not be easily merged, as we have no idea what files come from
1465 the same run. We do not use the info anyway, so leave it 0. */
1466 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1467 if (file_data
->profile_info
.runs
)
1469 int scale
= GCOV_COMPUTE_SCALE (max_runs
,
1470 file_data
->profile_info
.runs
);
1471 lto_gcov_summary
.sum_max
1472 = MAX (lto_gcov_summary
.sum_max
,
1473 apply_scale (file_data
->profile_info
.sum_max
, scale
));
1474 lto_gcov_summary
.sum_all
1475 = MAX (lto_gcov_summary
.sum_all
,
1476 apply_scale (file_data
->profile_info
.sum_all
, scale
));
1477 /* Save a pointer to the profile_info with the largest
1478 scaled sum_all and the scale for use in merging the
1480 if (!saved_profile_info
1481 || lto_gcov_summary
.sum_all
> saved_sum_all
)
1483 saved_profile_info
= &file_data
->profile_info
;
1484 saved_sum_all
= lto_gcov_summary
.sum_all
;
1485 saved_scale
= scale
;
1489 gcc_assert (saved_profile_info
);
1491 /* Scale up the histogram from the profile that had the largest
1492 scaled sum_all above. */
1493 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1495 /* Scale up the min value as we did the corresponding sum_all
1496 above. Use that to find the new histogram index. */
1497 gcov_type scaled_min
1498 = apply_scale (saved_profile_info
->histogram
[h_ix
].min_value
,
1500 /* The new index may be shared with another scaled histogram entry,
1501 so we need to account for a non-zero histogram entry at new_ix. */
1502 unsigned new_ix
= gcov_histo_index (scaled_min
);
1503 lto_gcov_summary
.histogram
[new_ix
].min_value
1504 = (lto_gcov_summary
.histogram
[new_ix
].num_counters
1505 ? MIN (lto_gcov_summary
.histogram
[new_ix
].min_value
, scaled_min
)
1507 /* Some of the scaled counter values would ostensibly need to be placed
1508 into different (larger) histogram buckets, but we keep things simple
1509 here and place the scaled cumulative counter value in the bucket
1510 corresponding to the scaled minimum counter value. */
1511 lto_gcov_summary
.histogram
[new_ix
].cum_value
1512 += apply_scale (saved_profile_info
->histogram
[h_ix
].cum_value
,
1514 lto_gcov_summary
.histogram
[new_ix
].num_counters
1515 += saved_profile_info
->histogram
[h_ix
].num_counters
;
1518 /* Watch roundoff errors. */
1519 if (lto_gcov_summary
.sum_max
< max_runs
)
1520 lto_gcov_summary
.sum_max
= max_runs
;
1522 /* If merging already happent at WPA time, we are done. */
1526 /* Now compute count_materialization_scale of each node.
1527 During LTRANS we already have values of count_materialization_scale
1528 computed, so just update them. */
1529 FOR_EACH_FUNCTION (node
)
1530 if (node
->lto_file_data
1531 && node
->lto_file_data
->profile_info
.runs
)
1535 scale
= RDIV (node
->count_materialization_scale
* max_runs
,
1536 node
->lto_file_data
->profile_info
.runs
);
1537 node
->count_materialization_scale
= scale
;
1539 fatal_error ("Profile information in %s corrupted",
1540 file_data
->file_name
);
1542 if (scale
== REG_BR_PROB_BASE
)
1544 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1545 edge
->count
= apply_scale (edge
->count
, scale
);
1546 node
->count
= apply_scale (node
->count
, scale
);
1550 /* Input and merge the symtab from each of the .o files passed to
1556 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1557 struct lto_file_decl_data
*file_data
;
1559 struct cgraph_node
*node
;
1561 while ((file_data
= file_data_vec
[j
++]))
1565 struct lto_input_block
*ib
;
1566 vec
<symtab_node
*> nodes
;
1568 ib
= lto_create_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1571 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1572 input_profile_summary (ib
, file_data
);
1573 file_data
->symtab_node_encoder
= lto_symtab_encoder_new (true);
1574 nodes
= input_cgraph_1 (file_data
, ib
);
1575 lto_destroy_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1578 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1581 fatal_error ("cannot find LTO section refs in %s",
1582 file_data
->file_name
);
1583 input_refs (ib
, nodes
);
1584 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1587 input_cgraph_opt_summary (nodes
);
1591 merge_profile_summaries (file_data_vec
);
1592 get_working_sets ();
1595 /* Clear out the aux field that was used to store enough state to
1596 tell which nodes should be overwritten. */
1597 FOR_EACH_FUNCTION (node
)
1599 /* Some nodes may have been created by cgraph_node. This
1600 happens when the callgraph contains nested functions. If the
1601 node for the parent function was never emitted to the gimple
1602 file, cgraph_node will create a node for it when setting the
1603 context of the nested function. */
1604 if (node
->lto_file_data
)
1609 /* True when we need optimization summary for NODE. */
1612 output_cgraph_opt_summary_p (struct cgraph_node
*node
)
1614 return (node
->clone_of
1615 && (node
->clone
.tree_map
1616 || node
->clone
.args_to_skip
1617 || node
->clone
.combined_args_to_skip
));
1620 /* Output optimization summary for EDGE to OB. */
1622 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1623 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1627 /* Output optimization summary for NODE to OB. */
1630 output_node_opt_summary (struct output_block
*ob
,
1631 struct cgraph_node
*node
,
1632 lto_symtab_encoder_t encoder
)
1636 struct ipa_replace_map
*map
;
1637 struct bitpack_d bp
;
1639 struct cgraph_edge
*e
;
1641 if (node
->clone
.args_to_skip
)
1643 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1644 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1645 streamer_write_uhwi (ob
, index
);
1648 streamer_write_uhwi (ob
, 0);
1649 if (node
->clone
.combined_args_to_skip
)
1651 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1652 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1653 streamer_write_uhwi (ob
, index
);
1656 streamer_write_uhwi (ob
, 0);
1657 streamer_write_uhwi (ob
, vec_safe_length (node
->clone
.tree_map
));
1658 FOR_EACH_VEC_SAFE_ELT (node
->clone
.tree_map
, i
, map
)
1660 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1661 mechanism to store function local declarations into summaries. */
1662 gcc_assert (!map
->old_tree
);
1663 streamer_write_uhwi (ob
, map
->parm_num
);
1664 gcc_assert (EXPR_LOCATION (map
->new_tree
) == UNKNOWN_LOCATION
);
1665 stream_write_tree (ob
, map
->new_tree
, true);
1666 bp
= bitpack_create (ob
->main_stream
);
1667 bp_pack_value (&bp
, map
->replace_p
, 1);
1668 bp_pack_value (&bp
, map
->ref_p
, 1);
1669 streamer_write_bitpack (&bp
);
1672 if (lto_symtab_encoder_in_partition_p (encoder
, node
))
1674 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1675 output_edge_opt_summary (ob
, e
);
1676 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1677 output_edge_opt_summary (ob
, e
);
1681 /* Output optimization summaries stored in callgraph.
1682 At the moment it is the clone info structure. */
1685 output_cgraph_opt_summary (void)
1688 lto_symtab_encoder_t encoder
;
1689 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1692 ob
->cgraph_node
= NULL
;
1693 encoder
= ob
->decl_state
->symtab_node_encoder
;
1694 n_nodes
= lto_symtab_encoder_size (encoder
);
1695 for (i
= 0; i
< n_nodes
; i
++)
1697 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1698 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1699 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1702 streamer_write_uhwi (ob
, count
);
1703 for (i
= 0; i
< n_nodes
; i
++)
1705 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1706 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1707 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1709 streamer_write_uhwi (ob
, i
);
1710 output_node_opt_summary (ob
, cnode
, encoder
);
1713 produce_asm (ob
, NULL
);
1714 destroy_output_block (ob
);
1717 /* Input optimisation summary of EDGE. */
1720 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1721 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1725 /* Input optimisation summary of NODE. */
1728 input_node_opt_summary (struct cgraph_node
*node
,
1729 struct lto_input_block
*ib_main
,
1730 struct data_in
*data_in
)
1735 struct bitpack_d bp
;
1736 struct cgraph_edge
*e
;
1738 count
= streamer_read_uhwi (ib_main
);
1740 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1741 for (i
= 0; i
< count
; i
++)
1743 bit
= streamer_read_uhwi (ib_main
);
1744 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1746 count
= streamer_read_uhwi (ib_main
);
1748 node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
1749 for (i
= 0; i
< count
; i
++)
1751 bit
= streamer_read_uhwi (ib_main
);
1752 bitmap_set_bit (node
->clone
.combined_args_to_skip
, bit
);
1754 count
= streamer_read_uhwi (ib_main
);
1755 for (i
= 0; i
< count
; i
++)
1757 struct ipa_replace_map
*map
= ggc_alloc_ipa_replace_map ();
1759 vec_safe_push (node
->clone
.tree_map
, map
);
1760 map
->parm_num
= streamer_read_uhwi (ib_main
);
1761 map
->old_tree
= NULL
;
1762 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1763 bp
= streamer_read_bitpack (ib_main
);
1764 map
->replace_p
= bp_unpack_value (&bp
, 1);
1765 map
->ref_p
= bp_unpack_value (&bp
, 1);
1767 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1768 input_edge_opt_summary (e
, ib_main
);
1769 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1770 input_edge_opt_summary (e
, ib_main
);
1773 /* Read section in file FILE_DATA of length LEN with data DATA. */
1776 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1777 const char *data
, size_t len
,
1778 vec
<symtab_node
*> nodes
)
1780 const struct lto_function_header
*header
=
1781 (const struct lto_function_header
*) data
;
1782 const int cfg_offset
= sizeof (struct lto_function_header
);
1783 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1784 const int string_offset
= main_offset
+ header
->main_size
;
1785 struct data_in
*data_in
;
1786 struct lto_input_block ib_main
;
1790 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1794 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1795 header
->string_size
, vNULL
);
1796 count
= streamer_read_uhwi (&ib_main
);
1798 for (i
= 0; i
< count
; i
++)
1800 int ref
= streamer_read_uhwi (&ib_main
);
1801 input_node_opt_summary (cgraph (nodes
[ref
]),
1804 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1806 lto_data_in_delete (data_in
);
1809 /* Input optimization summary of cgraph. */
1812 input_cgraph_opt_summary (vec
<symtab_node
*> nodes
)
1814 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1815 struct lto_file_decl_data
*file_data
;
1818 while ((file_data
= file_data_vec
[j
++]))
1822 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1826 input_cgraph_opt_section (file_data
, data
, len
, nodes
);