1 /* Write and read the cgraph to the memory mapped representation of a
4 Copyright (C) 2009-2013 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"
35 #include "langhooks.h"
36 #include "basic-block.h"
40 #include "diagnostic-core.h"
44 #include "pointer-set.h"
45 #include "lto-streamer.h"
46 #include "data-streamer.h"
47 #include "tree-streamer.h"
49 #include "tree-pass.h"
52 #include "pass_manager.h"
53 #include "ipa-utils.h"
55 static void output_cgraph_opt_summary (void);
56 static void input_cgraph_opt_summary (vec
<symtab_node
*> nodes
);
58 /* Number of LDPR values known to GCC. */
59 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
61 /* All node orders are ofsetted by ORDER_BASE. */
62 static int order_base
;
64 /* Cgraph streaming is organized as set of record whose type
65 is indicated by a tag. */
68 /* Must leave 0 for the stopper. */
70 /* Cgraph node without body available. */
71 LTO_symtab_unavail_node
= 1,
72 /* Cgraph node with function body. */
73 LTO_symtab_analyzed_node
,
76 LTO_symtab_indirect_edge
,
81 /* Create a new symtab encoder.
82 if FOR_INPUT, the encoder allocate only datastructures needed
83 to read the symtab. */
86 lto_symtab_encoder_new (bool for_input
)
88 lto_symtab_encoder_t encoder
= XCNEW (struct lto_symtab_encoder_d
);
91 encoder
->map
= pointer_map_create ();
92 encoder
->nodes
.create (0);
97 /* Delete ENCODER and its components. */
100 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder
)
102 encoder
->nodes
.release ();
104 pointer_map_destroy (encoder
->map
);
109 /* Return the existing reference number of NODE in the symtab encoder in
110 output block OB. Assign a new reference if this is the first time
114 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder
,
122 lto_encoder_entry entry
= {node
, false, false, false};
124 ref
= encoder
->nodes
.length ();
125 encoder
->nodes
.safe_push (entry
);
129 slot
= pointer_map_contains (encoder
->map
, node
);
132 lto_encoder_entry entry
= {node
, false, false, false};
133 ref
= encoder
->nodes
.length ();
135 slot
= pointer_map_insert (encoder
->map
, node
);
136 *slot
= (void *) (intptr_t) (ref
+ 1);
137 encoder
->nodes
.safe_push (entry
);
140 ref
= (size_t) *slot
- 1;
145 /* Remove NODE from encoder. */
148 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder
,
151 void **slot
, **last_slot
;
153 lto_encoder_entry last_node
;
155 slot
= pointer_map_contains (encoder
->map
, node
);
156 if (slot
== NULL
|| !*slot
)
159 index
= (size_t) *slot
- 1;
160 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
162 /* Remove from vector. We do this by swapping node with the last element
164 last_node
= encoder
->nodes
.pop ();
165 if (last_node
.node
!= node
)
167 last_slot
= pointer_map_contains (encoder
->map
, last_node
.node
);
168 gcc_checking_assert (last_slot
&& *last_slot
);
169 *last_slot
= (void *)(size_t) (index
+ 1);
171 /* Move the last element to the original spot of NODE. */
172 encoder
->nodes
[index
] = last_node
;
175 /* Remove element from hash table. */
181 /* Return TRUE if we should encode initializer of NODE (if any). */
184 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder
,
185 struct cgraph_node
*node
)
187 int index
= lto_symtab_encoder_lookup (encoder
, node
);
188 return encoder
->nodes
[index
].body
;
191 /* Return TRUE if we should encode body of NODE (if any). */
194 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder
,
195 struct cgraph_node
*node
)
197 int index
= lto_symtab_encoder_encode (encoder
, node
);
198 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
199 encoder
->nodes
[index
].body
= true;
202 /* Return TRUE if we should encode initializer of NODE (if any). */
205 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder
,
206 struct varpool_node
*node
)
208 int index
= lto_symtab_encoder_lookup (encoder
, node
);
209 if (index
== LCC_NOT_FOUND
)
211 return encoder
->nodes
[index
].initializer
;
214 /* Return TRUE if we should encode initializer of NODE (if any). */
217 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder
,
218 struct varpool_node
*node
)
220 int index
= lto_symtab_encoder_lookup (encoder
, node
);
221 encoder
->nodes
[index
].initializer
= true;
224 /* Return TRUE if we should encode initializer of NODE (if any). */
227 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder
,
230 int index
= lto_symtab_encoder_lookup (encoder
, node
);
231 if (index
== LCC_NOT_FOUND
)
233 return encoder
->nodes
[index
].in_partition
;
236 /* Return TRUE if we should encode body of NODE (if any). */
239 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder
,
242 int index
= lto_symtab_encoder_encode (encoder
, node
);
243 encoder
->nodes
[index
].in_partition
= true;
246 /* Output the cgraph EDGE to OB using ENCODER. */
249 lto_output_edge (struct lto_simple_output_block
*ob
, struct cgraph_edge
*edge
,
250 lto_symtab_encoder_t encoder
)
256 if (edge
->indirect_unknown_callee
)
257 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
258 LTO_symtab_indirect_edge
);
260 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
263 ref
= lto_symtab_encoder_lookup (encoder
, edge
->caller
);
264 gcc_assert (ref
!= LCC_NOT_FOUND
);
265 streamer_write_hwi_stream (ob
->main_stream
, ref
);
267 if (!edge
->indirect_unknown_callee
)
269 ref
= lto_symtab_encoder_lookup (encoder
, edge
->callee
);
270 gcc_assert (ref
!= LCC_NOT_FOUND
);
271 streamer_write_hwi_stream (ob
->main_stream
, ref
);
274 streamer_write_gcov_count_stream (ob
->main_stream
, edge
->count
);
276 bp
= bitpack_create (ob
->main_stream
);
277 uid
= (!gimple_has_body_p (edge
->caller
->decl
)
278 ? edge
->lto_stmt_uid
: gimple_uid (edge
->call_stmt
) + 1);
279 bp_pack_enum (&bp
, cgraph_inline_failed_enum
,
280 CIF_N_REASONS
, edge
->inline_failed
);
281 bp_pack_var_len_unsigned (&bp
, uid
);
282 bp_pack_var_len_unsigned (&bp
, edge
->frequency
);
283 bp_pack_value (&bp
, edge
->indirect_inlining_edge
, 1);
284 bp_pack_value (&bp
, edge
->speculative
, 1);
285 bp_pack_value (&bp
, edge
->call_stmt_cannot_inline_p
, 1);
286 bp_pack_value (&bp
, edge
->can_throw_external
, 1);
287 if (edge
->indirect_unknown_callee
)
289 int flags
= edge
->indirect_info
->ecf_flags
;
290 bp_pack_value (&bp
, (flags
& ECF_CONST
) != 0, 1);
291 bp_pack_value (&bp
, (flags
& ECF_PURE
) != 0, 1);
292 bp_pack_value (&bp
, (flags
& ECF_NORETURN
) != 0, 1);
293 bp_pack_value (&bp
, (flags
& ECF_MALLOC
) != 0, 1);
294 bp_pack_value (&bp
, (flags
& ECF_NOTHROW
) != 0, 1);
295 bp_pack_value (&bp
, (flags
& ECF_RETURNS_TWICE
) != 0, 1);
296 /* Flags that should not appear on indirect calls. */
297 gcc_assert (!(flags
& (ECF_LOOPING_CONST_OR_PURE
303 streamer_write_bitpack (&bp
);
304 if (edge
->indirect_unknown_callee
)
306 streamer_write_hwi_stream (ob
->main_stream
,
307 edge
->indirect_info
->common_target_id
);
308 if (edge
->indirect_info
->common_target_id
)
309 streamer_write_hwi_stream
310 (ob
->main_stream
, edge
->indirect_info
->common_target_probability
);
314 /* Return if LIST contain references from other partitions. */
317 referenced_from_other_partition_p (struct ipa_ref_list
*list
, lto_symtab_encoder_t encoder
)
321 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
323 if (ref
->referring
->in_other_partition
324 || !lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
330 /* Return true when node is reachable from other partition. */
333 reachable_from_other_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
335 struct cgraph_edge
*e
;
336 if (!node
->definition
)
338 if (node
->global
.inlined_to
)
340 for (e
= node
->callers
; e
; e
= e
->next_caller
)
341 if (e
->caller
->in_other_partition
342 || !lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
347 /* Return if LIST contain references from other partitions. */
350 referenced_from_this_partition_p (struct ipa_ref_list
*list
,
351 lto_symtab_encoder_t encoder
)
355 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
356 if (lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
361 /* Return true when node is reachable from other partition. */
364 reachable_from_this_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
366 struct cgraph_edge
*e
;
367 for (e
= node
->callers
; e
; e
= e
->next_caller
)
368 if (lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
373 /* Output the cgraph NODE to OB. ENCODER is used to find the
374 reference number of NODE->inlined_to. SET is the set of nodes we
375 are writing to the current file. If NODE is not in SET, then NODE
376 is a boundary of a cgraph_node_set and we pretend NODE just has a
377 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
378 that have had their callgraph node written so far. This is used to
379 determine if NODE is a clone of a previously written node. */
382 lto_output_node (struct lto_simple_output_block
*ob
, struct cgraph_node
*node
,
383 lto_symtab_encoder_t encoder
)
389 bool in_other_partition
= false;
390 struct cgraph_node
*clone_of
, *ultimate_clone_of
;
391 struct ipa_opt_pass_d
*pass
;
395 boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
397 if (node
->analyzed
&& !boundary_p
)
398 tag
= LTO_symtab_analyzed_node
;
400 tag
= LTO_symtab_unavail_node
;
402 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
404 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
406 /* In WPA mode, we only output part of the call-graph. Also, we
407 fake cgraph node attributes. There are two cases that we care.
409 Boundary nodes: There are nodes that are not part of SET but are
410 called from within SET. We artificially make them look like
411 externally visible nodes with no function body.
413 Cherry-picked nodes: These are nodes we pulled from other
414 translation units into SET during IPA-inlining. We make them as
415 local static nodes to prevent clashes with other local statics. */
416 if (boundary_p
&& node
->analyzed
&& !DECL_EXTERNAL (node
->decl
))
418 /* Inline clones can not be part of boundary.
419 gcc_assert (!node->global.inlined_to);
421 FIXME: At the moment they can be, when partition contains an inline
422 clone that is clone of inline clone from outside partition. We can
423 reshape the clone tree and make other tree to be the root, but it
424 needs a bit extra work and will be promplty done by cgraph_remove_node
425 after reading back. */
426 in_other_partition
= 1;
429 clone_of
= node
->clone_of
;
431 && (ref
= lto_symtab_encoder_lookup (encoder
, clone_of
)) == LCC_NOT_FOUND
)
432 if (clone_of
->prev_sibling_clone
)
433 clone_of
= clone_of
->prev_sibling_clone
;
435 clone_of
= clone_of
->clone_of
;
437 /* See if body of the master function is output. If not, we are seeing only
438 an declaration and we do not need to pass down clone tree. */
439 ultimate_clone_of
= clone_of
;
440 while (ultimate_clone_of
&& ultimate_clone_of
->clone_of
)
441 ultimate_clone_of
= ultimate_clone_of
->clone_of
;
443 if (clone_of
&& !lto_symtab_encoder_encode_body_p (encoder
, ultimate_clone_of
))
446 if (tag
== LTO_symtab_analyzed_node
)
447 gcc_assert (clone_of
|| !node
->clone_of
);
449 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
451 streamer_write_hwi_stream (ob
->main_stream
, ref
);
454 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
455 streamer_write_gcov_count_stream (ob
->main_stream
, node
->count
);
456 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
458 streamer_write_hwi_stream (ob
->main_stream
,
459 node
->ipa_transforms_to_apply
.length ());
460 FOR_EACH_VEC_ELT (node
->ipa_transforms_to_apply
, i
, pass
)
461 streamer_write_hwi_stream (ob
->main_stream
, pass
->static_pass_number
);
463 if (tag
== LTO_symtab_analyzed_node
)
465 if (node
->global
.inlined_to
)
467 ref
= lto_symtab_encoder_lookup (encoder
, node
->global
.inlined_to
);
468 gcc_assert (ref
!= LCC_NOT_FOUND
);
473 streamer_write_hwi_stream (ob
->main_stream
, ref
);
476 if (node
->same_comdat_group
&& !boundary_p
)
478 ref
= lto_symtab_encoder_lookup (encoder
,
479 node
->same_comdat_group
);
480 gcc_assert (ref
!= LCC_NOT_FOUND
);
484 streamer_write_hwi_stream (ob
->main_stream
, ref
);
486 streamer_write_hwi_stream (ob
->main_stream
, node
->tp_first_run
);
488 bp
= bitpack_create (ob
->main_stream
);
489 bp_pack_value (&bp
, node
->local
.local
, 1);
490 bp_pack_value (&bp
, node
->externally_visible
, 1);
491 bp_pack_value (&bp
, node
->definition
, 1);
492 bp_pack_value (&bp
, node
->local
.versionable
, 1);
493 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
494 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
495 bp_pack_value (&bp
, node
->force_output
, 1);
496 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
497 bp_pack_value (&bp
, node
->unique_name
, 1);
498 bp_pack_value (&bp
, node
->address_taken
, 1);
499 bp_pack_value (&bp
, tag
== LTO_symtab_analyzed_node
500 && !DECL_EXTERNAL (node
->decl
)
501 && !DECL_COMDAT (node
->decl
)
502 && (reachable_from_other_partition_p (node
, encoder
)
503 || referenced_from_other_partition_p (&node
->ref_list
,
505 bp_pack_value (&bp
, node
->lowered
, 1);
506 bp_pack_value (&bp
, in_other_partition
, 1);
507 /* Real aliases in a boundary become non-aliases. However we still stream
508 alias info on weakrefs.
509 TODO: We lose a bit of information here - when we know that variable is
510 defined in other unit, we may use the info on aliases to resolve
511 symbol1 != symbol2 type tests that we can do only for locally defined objects
513 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
514 bp_pack_value (&bp
, alias_p
, 1);
515 bp_pack_value (&bp
, node
->weakref
, 1);
516 bp_pack_value (&bp
, node
->frequency
, 2);
517 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
518 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
519 bp_pack_value (&bp
, node
->tm_clone
, 1);
520 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
521 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
522 LDPR_NUM_KNOWN
, node
->resolution
);
523 streamer_write_bitpack (&bp
);
525 if (node
->thunk
.thunk_p
&& !boundary_p
)
527 streamer_write_uhwi_stream
529 1 + (node
->thunk
.this_adjusting
!= 0) * 2
530 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
531 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
532 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
534 streamer_write_hwi_stream (ob
->main_stream
, node
->profile_id
);
537 /* Output the varpool NODE to OB.
538 If NODE is not in SET, then NODE is a boundary. */
541 lto_output_varpool_node (struct lto_simple_output_block
*ob
, struct varpool_node
*node
,
542 lto_symtab_encoder_t encoder
)
544 bool boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
549 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
550 LTO_symtab_variable
);
551 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
552 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
553 bp
= bitpack_create (ob
->main_stream
);
554 bp_pack_value (&bp
, node
->externally_visible
, 1);
555 bp_pack_value (&bp
, node
->force_output
, 1);
556 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
557 bp_pack_value (&bp
, node
->unique_name
, 1);
558 bp_pack_value (&bp
, node
->definition
, 1);
559 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
560 bp_pack_value (&bp
, alias_p
, 1);
561 bp_pack_value (&bp
, node
->weakref
, 1);
562 bp_pack_value (&bp
, node
->analyzed
&& !boundary_p
, 1);
563 gcc_assert (node
->definition
|| !node
->analyzed
);
564 /* Constant pool initializers can be de-unified into individual ltrans units.
565 FIXME: Alternatively at -Os we may want to avoid generating for them the local
566 labels and share them across LTRANS partitions. */
567 if (DECL_IN_CONSTANT_POOL (node
->decl
)
568 && !DECL_EXTERNAL (node
->decl
)
569 && !DECL_COMDAT (node
->decl
))
571 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
572 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
576 bp_pack_value (&bp
, node
->definition
577 && referenced_from_other_partition_p (&node
->ref_list
,
579 bp_pack_value (&bp
, node
->analyzed
580 && boundary_p
&& !DECL_EXTERNAL (node
->decl
), 1);
581 /* in_other_partition. */
583 streamer_write_bitpack (&bp
);
584 if (node
->same_comdat_group
&& !boundary_p
)
586 ref
= lto_symtab_encoder_lookup (encoder
,
587 node
->same_comdat_group
);
588 gcc_assert (ref
!= LCC_NOT_FOUND
);
592 streamer_write_hwi_stream (ob
->main_stream
, ref
);
593 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
594 LDPR_NUM_KNOWN
, node
->resolution
);
597 /* Output the varpool NODE to OB.
598 If NODE is not in SET, then NODE is a boundary. */
601 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
602 lto_symtab_encoder_t encoder
)
606 int uid
= ref
->lto_stmt_uid
;
607 struct cgraph_node
*node
;
609 bp
= bitpack_create (ob
->main_stream
);
610 bp_pack_value (&bp
, ref
->use
, 2);
611 bp_pack_value (&bp
, ref
->speculative
, 1);
612 streamer_write_bitpack (&bp
);
613 nref
= lto_symtab_encoder_lookup (encoder
, ref
->referred
);
614 gcc_assert (nref
!= LCC_NOT_FOUND
);
615 streamer_write_hwi_stream (ob
->main_stream
, nref
);
617 node
= dyn_cast
<cgraph_node
> (ref
->referring
);
621 uid
= gimple_uid (ref
->stmt
) + 1;
622 streamer_write_hwi_stream (ob
->main_stream
, uid
);
626 /* Stream out profile_summary to OB. */
629 output_profile_summary (struct lto_simple_output_block
*ob
)
636 /* We do not output num and run_max, they are not used by
637 GCC profile feedback and they are difficult to merge from multiple
639 gcc_assert (profile_info
->runs
);
640 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
641 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_max
);
643 /* sum_all is needed for computing the working set with the
645 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_all
);
647 /* Create and output a bitpack of non-zero histogram entries indices. */
648 bp
= bitpack_create (ob
->main_stream
);
649 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
650 bp_pack_value (&bp
, profile_info
->histogram
[h_ix
].num_counters
> 0, 1);
651 streamer_write_bitpack (&bp
);
652 /* Now stream out only those non-zero entries. */
653 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
655 if (!profile_info
->histogram
[h_ix
].num_counters
)
657 streamer_write_gcov_count_stream (ob
->main_stream
,
658 profile_info
->histogram
[h_ix
].num_counters
);
659 streamer_write_gcov_count_stream (ob
->main_stream
,
660 profile_info
->histogram
[h_ix
].min_value
);
661 streamer_write_gcov_count_stream (ob
->main_stream
,
662 profile_info
->histogram
[h_ix
].cum_value
);
664 /* IPA-profile computes hot bb threshold based on cumulated
665 whole program profile. We need to stream it down to ltrans. */
667 streamer_write_gcov_count_stream (ob
->main_stream
,
668 get_hot_bb_threshold ());
671 streamer_write_uhwi_stream (ob
->main_stream
, 0);
674 /* Output all callees or indirect outgoing edges. EDGE must be the first such
678 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
679 struct lto_simple_output_block
*ob
,
680 lto_symtab_encoder_t encoder
)
685 /* Output edges in backward direction, so the reconstructed callgraph match
686 and it is easy to associate call sites in the IPA pass summaries. */
687 while (edge
->next_callee
)
688 edge
= edge
->next_callee
;
689 for (; edge
; edge
= edge
->prev_callee
)
690 lto_output_edge (ob
, edge
, encoder
);
693 /* Output the part of the cgraph in SET. */
696 output_refs (lto_symtab_encoder_t encoder
)
698 lto_symtab_encoder_iterator lsei
;
699 struct lto_simple_output_block
*ob
;
704 ob
= lto_create_simple_output_block (LTO_section_refs
);
706 for (lsei
= lsei_start_in_partition (encoder
); !lsei_end_p (lsei
);
707 lsei_next_in_partition (&lsei
))
709 symtab_node
*node
= lsei_node (lsei
);
711 count
= ipa_ref_list_nreferences (&node
->ref_list
);
714 streamer_write_gcov_count_stream (ob
->main_stream
, count
);
715 streamer_write_uhwi_stream (ob
->main_stream
,
716 lto_symtab_encoder_lookup (encoder
, node
));
717 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
,
719 lto_output_ref (ob
, ref
, encoder
);
723 streamer_write_uhwi_stream (ob
->main_stream
, 0);
725 lto_destroy_simple_output_block (ob
);
728 /* Add NODE into encoder as well as nodes it is cloned from.
729 Do it in a way so clones appear first. */
732 add_node_to (lto_symtab_encoder_t encoder
, struct cgraph_node
*node
,
736 add_node_to (encoder
, node
->clone_of
, include_body
);
737 else if (include_body
)
738 lto_set_symtab_encoder_encode_body (encoder
, node
);
739 lto_symtab_encoder_encode (encoder
, node
);
742 /* Add all references in LIST to encoders. */
745 add_references (lto_symtab_encoder_t encoder
,
746 struct ipa_ref_list
*list
)
750 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
751 if (is_a
<cgraph_node
> (ref
->referred
))
752 add_node_to (encoder
, ipa_ref_node (ref
), false);
754 lto_symtab_encoder_encode (encoder
, ref
->referred
);
757 /* Find all symbols we want to stream into given partition and insert them
760 The function actually replaces IN_ENCODER by new one. The reason is that
761 streaming code needs clone's origin to be streamed before clone. This
762 means that we need to insert the nodes in specific order. This order is
763 ignored by the partitioning logic earlier. */
766 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder
)
768 struct cgraph_node
*node
;
769 struct cgraph_edge
*edge
;
771 lto_symtab_encoder_t encoder
;
772 lto_symtab_encoder_iterator lsei
;
773 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
775 encoder
= lto_symtab_encoder_new (false);
777 /* Go over all entries in the IN_ENCODER and duplicate them to
778 ENCODER. At the same time insert masters of clones so
779 every master appears before clone. */
780 for (lsei
= lsei_start_function_in_partition (in_encoder
);
781 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
783 node
= lsei_cgraph_node (lsei
);
784 add_node_to (encoder
, node
, true);
785 lto_set_symtab_encoder_in_partition (encoder
, node
);
786 add_references (encoder
, &node
->ref_list
);
787 /* For proper debug info, we need to ship the origins, too. */
788 if (DECL_ABSTRACT_ORIGIN (node
->decl
))
790 struct cgraph_node
*origin_node
791 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node
->decl
));
792 add_node_to (encoder
, origin_node
, true);
795 for (lsei
= lsei_start_variable_in_partition (in_encoder
);
796 !lsei_end_p (lsei
); lsei_next_variable_in_partition (&lsei
))
798 struct varpool_node
*vnode
= lsei_varpool_node (lsei
);
800 lto_set_symtab_encoder_in_partition (encoder
, vnode
);
801 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
802 add_references (encoder
, &vnode
->ref_list
);
803 /* For proper debug info, we need to ship the origins, too. */
804 if (DECL_ABSTRACT_ORIGIN (vnode
->decl
))
806 struct varpool_node
*origin_node
807 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node
->decl
));
808 lto_set_symtab_encoder_in_partition (encoder
, origin_node
);
811 /* Pickle in also the initializer of all referenced readonly variables
812 to help folding. Constant pool variables are not shared, so we must
814 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
816 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
817 if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
819 if (!lto_symtab_encoder_encode_initializer_p (encoder
,
821 && ctor_for_folding (vnode
->decl
) != error_mark_node
)
823 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
824 add_references (encoder
, &vnode
->ref_list
);
829 /* Go over all the nodes again to include callees that are not in
831 for (lsei
= lsei_start_function_in_partition (encoder
);
832 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
834 node
= lsei_cgraph_node (lsei
);
835 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
837 struct cgraph_node
*callee
= edge
->callee
;
838 if (!lto_symtab_encoder_in_partition_p (encoder
, callee
))
840 /* We should have moved all the inlines. */
841 gcc_assert (!callee
->global
.inlined_to
);
842 add_node_to (encoder
, callee
, false);
845 /* Add all possible targets for late devirtualization. */
846 if (flag_devirtualize
)
847 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
848 if (edge
->indirect_info
->polymorphic
)
853 vec
<cgraph_node
*>targets
854 = possible_polymorphic_call_targets
855 (edge
, &final
, &cache_token
);
856 if (!pointer_set_insert (reachable_call_targets
,
859 for (i
= 0; i
< targets
.length (); i
++)
861 struct cgraph_node
*callee
= targets
[i
];
863 /* Adding an external declarations into the unit serves
864 no purpose and just increases its boundary. */
865 if (callee
->definition
866 && !lto_symtab_encoder_in_partition_p
869 gcc_assert (!callee
->global
.inlined_to
);
870 add_node_to (encoder
, callee
, false);
876 lto_symtab_encoder_delete (in_encoder
);
877 pointer_set_destroy (reachable_call_targets
);
881 /* Output the part of the symtab in SET and VSET. */
886 struct cgraph_node
*node
;
887 struct lto_simple_output_block
*ob
;
888 lto_symtab_encoder_iterator lsei
;
890 lto_symtab_encoder_t encoder
;
891 static bool asm_nodes_output
= false;
894 output_cgraph_opt_summary ();
896 ob
= lto_create_simple_output_block (LTO_section_symtab_nodes
);
898 output_profile_summary (ob
);
900 /* An encoder for cgraph nodes should have been created by
901 ipa_write_summaries_1. */
902 gcc_assert (ob
->decl_state
->symtab_node_encoder
);
903 encoder
= ob
->decl_state
->symtab_node_encoder
;
905 /* Write out the nodes. We must first output a node and then its clones,
906 otherwise at a time reading back the node there would be nothing to clone
908 n_nodes
= lto_symtab_encoder_size (encoder
);
909 for (i
= 0; i
< n_nodes
; i
++)
911 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
912 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
913 lto_output_node (ob
, cnode
, encoder
);
915 lto_output_varpool_node (ob
, varpool (node
), encoder
);
919 /* Go over the nodes in SET again to write edges. */
920 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
921 lsei_next_function_in_partition (&lsei
))
923 node
= lsei_cgraph_node (lsei
);
924 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
925 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
928 streamer_write_uhwi_stream (ob
->main_stream
, 0);
930 lto_destroy_simple_output_block (ob
);
932 /* Emit toplevel asms.
933 When doing WPA we must output every asm just once. Since we do not partition asm
934 nodes at all, output them to first output. This is kind of hack, but should work
936 if (!asm_nodes_output
)
938 asm_nodes_output
= true;
939 lto_output_toplevel_asms ();
942 output_refs (encoder
);
945 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
946 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
947 NODE or to replace the values in it, for instance because the first
948 time we saw it, the function body was not available but now it
949 is. BP is a bitpack with all the bitflags for NODE read from the
953 input_overwrite_node (struct lto_file_decl_data
*file_data
,
954 struct cgraph_node
*node
,
955 enum LTO_symtab_tags tag
,
956 struct bitpack_d
*bp
)
958 node
->aux
= (void *) tag
;
959 node
->lto_file_data
= file_data
;
961 node
->local
.local
= bp_unpack_value (bp
, 1);
962 node
->externally_visible
= bp_unpack_value (bp
, 1);
963 node
->definition
= bp_unpack_value (bp
, 1);
964 node
->local
.versionable
= bp_unpack_value (bp
, 1);
965 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
966 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
967 node
->force_output
= bp_unpack_value (bp
, 1);
968 node
->forced_by_abi
= bp_unpack_value (bp
, 1);
969 node
->unique_name
= bp_unpack_value (bp
, 1);
970 node
->address_taken
= bp_unpack_value (bp
, 1);
971 node
->used_from_other_partition
= bp_unpack_value (bp
, 1);
972 node
->lowered
= bp_unpack_value (bp
, 1);
973 node
->analyzed
= tag
== LTO_symtab_analyzed_node
;
974 node
->in_other_partition
= bp_unpack_value (bp
, 1);
975 if (node
->in_other_partition
976 /* Avoid updating decl when we are seeing just inline clone.
977 When inlining function that has functions already inlined into it,
978 we produce clones of inline clones.
980 WPA partitioning might put each clone into different unit and
981 we might end up streaming inline clone from other partition
982 to support clone we are interested in. */
984 || node
->clone_of
->decl
!= node
->decl
))
986 DECL_EXTERNAL (node
->decl
) = 1;
987 TREE_STATIC (node
->decl
) = 0;
989 node
->alias
= bp_unpack_value (bp
, 1);
990 node
->weakref
= bp_unpack_value (bp
, 1);
991 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
992 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
993 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
994 node
->tm_clone
= bp_unpack_value (bp
, 1);
995 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
996 node
->resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
1000 /* Return string alias is alias of. */
1003 get_alias_symbol (tree decl
)
1005 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1006 return get_identifier (TREE_STRING_POINTER
1007 (TREE_VALUE (TREE_VALUE (alias
))));
1010 /* Read a node from input_block IB. TAG is the node's tag just read.
1011 Return the node read or overwriten. */
1013 static struct cgraph_node
*
1014 input_node (struct lto_file_decl_data
*file_data
,
1015 struct lto_input_block
*ib
,
1016 enum LTO_symtab_tags tag
,
1017 vec
<symtab_node
*> nodes
)
1019 gcc::pass_manager
*passes
= g
->get_passes ();
1021 struct cgraph_node
*node
;
1022 struct bitpack_d bp
;
1023 unsigned decl_index
;
1024 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
1029 order
= streamer_read_hwi (ib
) + order_base
;
1030 clone_ref
= streamer_read_hwi (ib
);
1032 decl_index
= streamer_read_uhwi (ib
);
1033 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
1035 if (clone_ref
!= LCC_NOT_FOUND
)
1037 node
= cgraph_clone_node (cgraph (nodes
[clone_ref
]), fn_decl
,
1038 0, CGRAPH_FREQ_BASE
, false,
1039 vNULL
, false, NULL
);
1043 /* Declaration of functions can be already merged with a declaration
1044 from other input file. We keep cgraph unmerged until after streaming
1045 of ipa passes is done. Alays forcingly create a fresh node. */
1046 node
= cgraph_create_empty_node ();
1047 node
->decl
= fn_decl
;
1048 symtab_register_node (node
);
1051 node
->order
= order
;
1052 if (order
>= symtab_order
)
1053 symtab_order
= order
+ 1;
1055 node
->count
= streamer_read_gcov_count (ib
);
1056 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1058 count
= streamer_read_hwi (ib
);
1059 node
->ipa_transforms_to_apply
= vNULL
;
1060 for (i
= 0; i
< count
; i
++)
1062 struct opt_pass
*pass
;
1063 int pid
= streamer_read_hwi (ib
);
1065 gcc_assert (pid
< passes
->passes_by_id_size
);
1066 pass
= passes
->passes_by_id
[pid
];
1067 node
->ipa_transforms_to_apply
.safe_push ((struct ipa_opt_pass_d
*) pass
);
1070 if (tag
== LTO_symtab_analyzed_node
)
1071 ref
= streamer_read_hwi (ib
);
1073 ref2
= streamer_read_hwi (ib
);
1075 /* Make sure that we have not read this node before. Nodes that
1076 have already been read will have their tag stored in the 'aux'
1077 field. Since built-in functions can be referenced in multiple
1078 functions, they are expected to be read more than once. */
1079 if (node
->aux
&& !DECL_BUILT_IN (node
->decl
))
1080 internal_error ("bytecode stream: found multiple instances of cgraph "
1081 "node with uid %d", node
->uid
);
1083 node
->tp_first_run
= streamer_read_uhwi (ib
);
1085 bp
= streamer_read_bitpack (ib
);
1087 input_overwrite_node (file_data
, node
, tag
, &bp
);
1089 /* Store a reference for now, and fix up later to be a pointer. */
1090 node
->global
.inlined_to
= (cgraph_node_ptr
) (intptr_t) ref
;
1092 /* Store a reference for now, and fix up later to be a pointer. */
1093 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref2
;
1095 if (node
->thunk
.thunk_p
)
1097 int type
= streamer_read_uhwi (ib
);
1098 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1099 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1101 node
->thunk
.fixed_offset
= fixed_offset
;
1102 node
->thunk
.this_adjusting
= (type
& 2);
1103 node
->thunk
.virtual_value
= virtual_value
;
1104 node
->thunk
.virtual_offset_p
= (type
& 4);
1106 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1107 node
->alias_target
= get_alias_symbol (node
->decl
);
1108 node
->profile_id
= streamer_read_hwi (ib
);
1112 /* Read a node from input_block IB. TAG is the node's tag just read.
1113 Return the node read or overwriten. */
1115 static struct varpool_node
*
1116 input_varpool_node (struct lto_file_decl_data
*file_data
,
1117 struct lto_input_block
*ib
)
1121 struct varpool_node
*node
;
1122 struct bitpack_d bp
;
1123 int ref
= LCC_NOT_FOUND
;
1126 order
= streamer_read_hwi (ib
) + order_base
;
1127 decl_index
= streamer_read_uhwi (ib
);
1128 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1130 /* Declaration of functions can be already merged with a declaration
1131 from other input file. We keep cgraph unmerged until after streaming
1132 of ipa passes is done. Alays forcingly create a fresh node. */
1133 node
= varpool_create_empty_node ();
1134 node
->decl
= var_decl
;
1135 symtab_register_node (node
);
1137 node
->order
= order
;
1138 if (order
>= symtab_order
)
1139 symtab_order
= order
+ 1;
1140 node
->lto_file_data
= file_data
;
1142 bp
= streamer_read_bitpack (ib
);
1143 node
->externally_visible
= bp_unpack_value (&bp
, 1);
1144 node
->force_output
= bp_unpack_value (&bp
, 1);
1145 node
->forced_by_abi
= bp_unpack_value (&bp
, 1);
1146 node
->unique_name
= bp_unpack_value (&bp
, 1);
1147 node
->definition
= bp_unpack_value (&bp
, 1);
1148 node
->alias
= bp_unpack_value (&bp
, 1);
1149 node
->weakref
= bp_unpack_value (&bp
, 1);
1150 node
->analyzed
= bp_unpack_value (&bp
, 1);
1151 node
->used_from_other_partition
= bp_unpack_value (&bp
, 1);
1152 node
->in_other_partition
= bp_unpack_value (&bp
, 1);
1153 if (node
->in_other_partition
)
1155 DECL_EXTERNAL (node
->decl
) = 1;
1156 TREE_STATIC (node
->decl
) = 0;
1158 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1159 node
->alias_target
= get_alias_symbol (node
->decl
);
1160 ref
= streamer_read_hwi (ib
);
1161 /* Store a reference for now, and fix up later to be a pointer. */
1162 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref
;
1163 node
->resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1169 /* Read a node from input_block IB. TAG is the node's tag just read.
1170 Return the node read or overwriten. */
1173 input_ref (struct lto_input_block
*ib
,
1174 symtab_node
*referring_node
,
1175 vec
<symtab_node
*> nodes
)
1177 symtab_node
*node
= NULL
;
1178 struct bitpack_d bp
;
1179 enum ipa_ref_use use
;
1181 struct ipa_ref
*ref
;
1183 bp
= streamer_read_bitpack (ib
);
1184 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1185 speculative
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 1);
1186 node
= nodes
[streamer_read_hwi (ib
)];
1187 ref
= ipa_record_reference (referring_node
, node
, use
, NULL
);
1188 ref
->speculative
= speculative
;
1189 if (is_a
<cgraph_node
> (referring_node
))
1190 ref
->lto_stmt_uid
= streamer_read_hwi (ib
);
1193 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1194 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1195 edge being read is indirect (in the sense that it has
1196 indirect_unknown_callee set). */
1199 input_edge (struct lto_input_block
*ib
, vec
<symtab_node
*> nodes
,
1202 struct cgraph_node
*caller
, *callee
;
1203 struct cgraph_edge
*edge
;
1204 unsigned int stmt_id
;
1207 cgraph_inline_failed_t inline_failed
;
1208 struct bitpack_d bp
;
1211 caller
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1212 if (caller
== NULL
|| caller
->decl
== NULL_TREE
)
1213 internal_error ("bytecode stream: no caller found while reading edge");
1217 callee
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1218 if (callee
== NULL
|| callee
->decl
== NULL_TREE
)
1219 internal_error ("bytecode stream: no callee found while reading edge");
1224 count
= streamer_read_gcov_count (ib
);
1226 bp
= streamer_read_bitpack (ib
);
1227 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_enum
, CIF_N_REASONS
);
1228 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1229 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1232 edge
= cgraph_create_indirect_edge (caller
, NULL
, 0, count
, freq
);
1234 edge
= cgraph_create_edge (caller
, callee
, NULL
, count
, freq
);
1236 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1237 edge
->speculative
= bp_unpack_value (&bp
, 1);
1238 edge
->lto_stmt_uid
= stmt_id
;
1239 edge
->inline_failed
= inline_failed
;
1240 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1241 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1244 if (bp_unpack_value (&bp
, 1))
1245 ecf_flags
|= ECF_CONST
;
1246 if (bp_unpack_value (&bp
, 1))
1247 ecf_flags
|= ECF_PURE
;
1248 if (bp_unpack_value (&bp
, 1))
1249 ecf_flags
|= ECF_NORETURN
;
1250 if (bp_unpack_value (&bp
, 1))
1251 ecf_flags
|= ECF_MALLOC
;
1252 if (bp_unpack_value (&bp
, 1))
1253 ecf_flags
|= ECF_NOTHROW
;
1254 if (bp_unpack_value (&bp
, 1))
1255 ecf_flags
|= ECF_RETURNS_TWICE
;
1256 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1257 edge
->indirect_info
->common_target_id
= streamer_read_hwi (ib
);
1258 if (edge
->indirect_info
->common_target_id
)
1259 edge
->indirect_info
->common_target_probability
= streamer_read_hwi (ib
);
1264 /* Read a cgraph from IB using the info in FILE_DATA. */
1266 static vec
<symtab_node
*>
1267 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1268 struct lto_input_block
*ib
)
1270 enum LTO_symtab_tags tag
;
1271 vec
<symtab_node
*> nodes
= vNULL
;
1275 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1276 order_base
= symtab_order
;
1279 if (tag
== LTO_symtab_edge
)
1280 input_edge (ib
, nodes
, false);
1281 else if (tag
== LTO_symtab_indirect_edge
)
1282 input_edge (ib
, nodes
, true);
1283 else if (tag
== LTO_symtab_variable
)
1285 node
= input_varpool_node (file_data
, ib
);
1286 nodes
.safe_push (node
);
1287 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1291 node
= input_node (file_data
, ib
, tag
, nodes
);
1292 if (node
== NULL
|| node
->decl
== NULL_TREE
)
1293 internal_error ("bytecode stream: found empty cgraph node");
1294 nodes
.safe_push (node
);
1295 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1298 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1301 lto_input_toplevel_asms (file_data
, order_base
);
1303 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1304 #ifdef ENABLE_CHECKING
1305 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1306 gcc_assert (node
->aux
|| !is_a
<cgraph_node
> (node
));
1308 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1311 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
1313 ref
= (int) (intptr_t) cnode
->global
.inlined_to
;
1315 /* We share declaration of builtins, so we may read same node twice. */
1320 /* Fixup inlined_to from reference to pointer. */
1321 if (ref
!= LCC_NOT_FOUND
)
1322 cgraph (node
)->global
.inlined_to
= cgraph (nodes
[ref
]);
1324 cnode
->global
.inlined_to
= NULL
;
1327 ref
= (int) (intptr_t) node
->same_comdat_group
;
1329 /* Fixup same_comdat_group from reference to pointer. */
1330 if (ref
!= LCC_NOT_FOUND
)
1331 node
->same_comdat_group
= nodes
[ref
];
1333 node
->same_comdat_group
= NULL
;
1335 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1336 node
->aux
= is_a
<cgraph_node
> (node
) ? (void *)1 : NULL
;
1340 /* Input ipa_refs. */
1343 input_refs (struct lto_input_block
*ib
,
1344 vec
<symtab_node
*> nodes
)
1351 count
= streamer_read_uhwi (ib
);
1354 idx
= streamer_read_uhwi (ib
);
1358 input_ref (ib
, node
, nodes
);
1365 static struct gcov_ctr_summary lto_gcov_summary
;
1367 /* Input profile_info from IB. */
1369 input_profile_summary (struct lto_input_block
*ib
,
1370 struct lto_file_decl_data
*file_data
)
1373 struct bitpack_d bp
;
1374 unsigned int runs
= streamer_read_uhwi (ib
);
1377 file_data
->profile_info
.runs
= runs
;
1378 file_data
->profile_info
.sum_max
= streamer_read_gcov_count (ib
);
1379 file_data
->profile_info
.sum_all
= streamer_read_gcov_count (ib
);
1381 memset (file_data
->profile_info
.histogram
, 0,
1382 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1383 /* Input the bitpack of non-zero histogram indices. */
1384 bp
= streamer_read_bitpack (ib
);
1385 /* Read in and unpack the full bitpack, flagging non-zero
1386 histogram entries by setting the num_counters non-zero. */
1387 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1389 file_data
->profile_info
.histogram
[h_ix
].num_counters
1390 = bp_unpack_value (&bp
, 1);
1392 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1394 if (!file_data
->profile_info
.histogram
[h_ix
].num_counters
)
1397 file_data
->profile_info
.histogram
[h_ix
].num_counters
1398 = streamer_read_gcov_count (ib
);
1399 file_data
->profile_info
.histogram
[h_ix
].min_value
1400 = streamer_read_gcov_count (ib
);
1401 file_data
->profile_info
.histogram
[h_ix
].cum_value
1402 = streamer_read_gcov_count (ib
);
1404 /* IPA-profile computes hot bb threshold based on cumulated
1405 whole program profile. We need to stream it down to ltrans. */
1407 set_hot_bb_threshold (streamer_read_gcov_count (ib
));
1412 /* Rescale profile summaries to the same number of runs in the whole unit. */
1415 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1417 struct lto_file_decl_data
*file_data
;
1418 unsigned int j
, h_ix
;
1419 gcov_unsigned_t max_runs
= 0;
1420 struct cgraph_node
*node
;
1421 struct cgraph_edge
*edge
;
1422 gcov_type saved_sum_all
= 0;
1423 gcov_ctr_summary
*saved_profile_info
= 0;
1424 int saved_scale
= 0;
1426 /* Find unit with maximal number of runs. If we ever get serious about
1427 roundoff errors, we might also consider computing smallest common
1429 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1430 if (max_runs
< file_data
->profile_info
.runs
)
1431 max_runs
= file_data
->profile_info
.runs
;
1436 /* Simple overflow check. We probably don't need to support that many train
1437 runs. Such a large value probably imply data corruption anyway. */
1438 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1440 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1441 INT_MAX
/ REG_BR_PROB_BASE
);
1445 profile_info
= <o_gcov_summary
;
1446 lto_gcov_summary
.runs
= max_runs
;
1447 lto_gcov_summary
.sum_max
= 0;
1448 memset (lto_gcov_summary
.histogram
, 0,
1449 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1451 /* Rescale all units to the maximal number of runs.
1452 sum_max can not be easily merged, as we have no idea what files come from
1453 the same run. We do not use the info anyway, so leave it 0. */
1454 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1455 if (file_data
->profile_info
.runs
)
1457 int scale
= GCOV_COMPUTE_SCALE (max_runs
,
1458 file_data
->profile_info
.runs
);
1459 lto_gcov_summary
.sum_max
1460 = MAX (lto_gcov_summary
.sum_max
,
1461 apply_scale (file_data
->profile_info
.sum_max
, scale
));
1462 lto_gcov_summary
.sum_all
1463 = MAX (lto_gcov_summary
.sum_all
,
1464 apply_scale (file_data
->profile_info
.sum_all
, scale
));
1465 /* Save a pointer to the profile_info with the largest
1466 scaled sum_all and the scale for use in merging the
1468 if (!saved_profile_info
1469 || lto_gcov_summary
.sum_all
> saved_sum_all
)
1471 saved_profile_info
= &file_data
->profile_info
;
1472 saved_sum_all
= lto_gcov_summary
.sum_all
;
1473 saved_scale
= scale
;
1477 gcc_assert (saved_profile_info
);
1479 /* Scale up the histogram from the profile that had the largest
1480 scaled sum_all above. */
1481 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1483 /* Scale up the min value as we did the corresponding sum_all
1484 above. Use that to find the new histogram index. */
1485 gcov_type scaled_min
1486 = apply_scale (saved_profile_info
->histogram
[h_ix
].min_value
,
1488 /* The new index may be shared with another scaled histogram entry,
1489 so we need to account for a non-zero histogram entry at new_ix. */
1490 unsigned new_ix
= gcov_histo_index (scaled_min
);
1491 lto_gcov_summary
.histogram
[new_ix
].min_value
1492 = (lto_gcov_summary
.histogram
[new_ix
].num_counters
1493 ? MIN (lto_gcov_summary
.histogram
[new_ix
].min_value
, scaled_min
)
1495 /* Some of the scaled counter values would ostensibly need to be placed
1496 into different (larger) histogram buckets, but we keep things simple
1497 here and place the scaled cumulative counter value in the bucket
1498 corresponding to the scaled minimum counter value. */
1499 lto_gcov_summary
.histogram
[new_ix
].cum_value
1500 += apply_scale (saved_profile_info
->histogram
[h_ix
].cum_value
,
1502 lto_gcov_summary
.histogram
[new_ix
].num_counters
1503 += saved_profile_info
->histogram
[h_ix
].num_counters
;
1506 /* Watch roundoff errors. */
1507 if (lto_gcov_summary
.sum_max
< max_runs
)
1508 lto_gcov_summary
.sum_max
= max_runs
;
1510 /* If merging already happent at WPA time, we are done. */
1514 /* Now compute count_materialization_scale of each node.
1515 During LTRANS we already have values of count_materialization_scale
1516 computed, so just update them. */
1517 FOR_EACH_FUNCTION (node
)
1518 if (node
->lto_file_data
1519 && node
->lto_file_data
->profile_info
.runs
)
1523 scale
= RDIV (node
->count_materialization_scale
* max_runs
,
1524 node
->lto_file_data
->profile_info
.runs
);
1525 node
->count_materialization_scale
= scale
;
1527 fatal_error ("Profile information in %s corrupted",
1528 file_data
->file_name
);
1530 if (scale
== REG_BR_PROB_BASE
)
1532 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1533 edge
->count
= apply_scale (edge
->count
, scale
);
1534 node
->count
= apply_scale (node
->count
, scale
);
1538 /* Input and merge the symtab from each of the .o files passed to
1544 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1545 struct lto_file_decl_data
*file_data
;
1547 struct cgraph_node
*node
;
1549 while ((file_data
= file_data_vec
[j
++]))
1553 struct lto_input_block
*ib
;
1554 vec
<symtab_node
*> nodes
;
1556 ib
= lto_create_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1559 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1560 input_profile_summary (ib
, file_data
);
1561 file_data
->symtab_node_encoder
= lto_symtab_encoder_new (true);
1562 nodes
= input_cgraph_1 (file_data
, ib
);
1563 lto_destroy_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1566 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1569 fatal_error ("cannot find LTO section refs in %s",
1570 file_data
->file_name
);
1571 input_refs (ib
, nodes
);
1572 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1575 input_cgraph_opt_summary (nodes
);
1579 merge_profile_summaries (file_data_vec
);
1580 get_working_sets ();
1583 /* Clear out the aux field that was used to store enough state to
1584 tell which nodes should be overwritten. */
1585 FOR_EACH_FUNCTION (node
)
1587 /* Some nodes may have been created by cgraph_node. This
1588 happens when the callgraph contains nested functions. If the
1589 node for the parent function was never emitted to the gimple
1590 file, cgraph_node will create a node for it when setting the
1591 context of the nested function. */
1592 if (node
->lto_file_data
)
1597 /* True when we need optimization summary for NODE. */
1600 output_cgraph_opt_summary_p (struct cgraph_node
*node
)
1602 return (node
->clone_of
1603 && (node
->clone
.tree_map
1604 || node
->clone
.args_to_skip
1605 || node
->clone
.combined_args_to_skip
));
1608 /* Output optimization summary for EDGE to OB. */
1610 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1611 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1615 /* Output optimization summary for NODE to OB. */
1618 output_node_opt_summary (struct output_block
*ob
,
1619 struct cgraph_node
*node
,
1620 lto_symtab_encoder_t encoder
)
1624 struct ipa_replace_map
*map
;
1625 struct bitpack_d bp
;
1627 struct cgraph_edge
*e
;
1629 if (node
->clone
.args_to_skip
)
1631 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1632 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1633 streamer_write_uhwi (ob
, index
);
1636 streamer_write_uhwi (ob
, 0);
1637 if (node
->clone
.combined_args_to_skip
)
1639 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1640 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1641 streamer_write_uhwi (ob
, index
);
1644 streamer_write_uhwi (ob
, 0);
1645 streamer_write_uhwi (ob
, vec_safe_length (node
->clone
.tree_map
));
1646 FOR_EACH_VEC_SAFE_ELT (node
->clone
.tree_map
, i
, map
)
1648 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1649 mechanism to store function local declarations into summaries. */
1650 gcc_assert (!map
->old_tree
);
1651 streamer_write_uhwi (ob
, map
->parm_num
);
1652 gcc_assert (EXPR_LOCATION (map
->new_tree
) == UNKNOWN_LOCATION
);
1653 stream_write_tree (ob
, map
->new_tree
, true);
1654 bp
= bitpack_create (ob
->main_stream
);
1655 bp_pack_value (&bp
, map
->replace_p
, 1);
1656 bp_pack_value (&bp
, map
->ref_p
, 1);
1657 streamer_write_bitpack (&bp
);
1660 if (lto_symtab_encoder_in_partition_p (encoder
, node
))
1662 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1663 output_edge_opt_summary (ob
, e
);
1664 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1665 output_edge_opt_summary (ob
, e
);
1669 /* Output optimization summaries stored in callgraph.
1670 At the moment it is the clone info structure. */
1673 output_cgraph_opt_summary (void)
1676 lto_symtab_encoder_t encoder
;
1677 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1680 ob
->cgraph_node
= NULL
;
1681 encoder
= ob
->decl_state
->symtab_node_encoder
;
1682 n_nodes
= lto_symtab_encoder_size (encoder
);
1683 for (i
= 0; i
< n_nodes
; i
++)
1685 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1686 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
1687 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1690 streamer_write_uhwi (ob
, count
);
1691 for (i
= 0; i
< n_nodes
; i
++)
1693 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1694 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
1695 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1697 streamer_write_uhwi (ob
, i
);
1698 output_node_opt_summary (ob
, cnode
, encoder
);
1701 produce_asm (ob
, NULL
);
1702 destroy_output_block (ob
);
1705 /* Input optimisation summary of EDGE. */
1708 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1709 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1713 /* Input optimisation summary of NODE. */
1716 input_node_opt_summary (struct cgraph_node
*node
,
1717 struct lto_input_block
*ib_main
,
1718 struct data_in
*data_in
)
1723 struct bitpack_d bp
;
1724 struct cgraph_edge
*e
;
1726 count
= streamer_read_uhwi (ib_main
);
1728 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1729 for (i
= 0; i
< count
; i
++)
1731 bit
= streamer_read_uhwi (ib_main
);
1732 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1734 count
= streamer_read_uhwi (ib_main
);
1736 node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
1737 for (i
= 0; i
< count
; i
++)
1739 bit
= streamer_read_uhwi (ib_main
);
1740 bitmap_set_bit (node
->clone
.combined_args_to_skip
, bit
);
1742 count
= streamer_read_uhwi (ib_main
);
1743 for (i
= 0; i
< count
; i
++)
1745 struct ipa_replace_map
*map
= ggc_alloc_ipa_replace_map ();
1747 vec_safe_push (node
->clone
.tree_map
, map
);
1748 map
->parm_num
= streamer_read_uhwi (ib_main
);
1749 map
->old_tree
= NULL
;
1750 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1751 bp
= streamer_read_bitpack (ib_main
);
1752 map
->replace_p
= bp_unpack_value (&bp
, 1);
1753 map
->ref_p
= bp_unpack_value (&bp
, 1);
1755 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1756 input_edge_opt_summary (e
, ib_main
);
1757 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1758 input_edge_opt_summary (e
, ib_main
);
1761 /* Read section in file FILE_DATA of length LEN with data DATA. */
1764 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1765 const char *data
, size_t len
,
1766 vec
<symtab_node
*> nodes
)
1768 const struct lto_function_header
*header
=
1769 (const struct lto_function_header
*) data
;
1770 const int cfg_offset
= sizeof (struct lto_function_header
);
1771 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1772 const int string_offset
= main_offset
+ header
->main_size
;
1773 struct data_in
*data_in
;
1774 struct lto_input_block ib_main
;
1778 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1782 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1783 header
->string_size
, vNULL
);
1784 count
= streamer_read_uhwi (&ib_main
);
1786 for (i
= 0; i
< count
; i
++)
1788 int ref
= streamer_read_uhwi (&ib_main
);
1789 input_node_opt_summary (cgraph (nodes
[ref
]),
1792 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1794 lto_data_in_delete (data_in
);
1797 /* Input optimization summary of cgraph. */
1800 input_cgraph_opt_summary (vec
<symtab_node
*> nodes
)
1802 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1803 struct lto_file_decl_data
*file_data
;
1806 while ((file_data
= file_data_vec
[j
++]))
1810 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1814 input_cgraph_opt_section (file_data
, data
, len
, nodes
);