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"
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 static void output_cgraph_opt_summary (void);
57 static void input_cgraph_opt_summary (vec
<symtab_node
*> nodes
);
59 /* Number of LDPR values known to GCC. */
60 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
62 /* All node orders are ofsetted by ORDER_BASE. */
63 static int order_base
;
65 /* Cgraph streaming is organized as set of record whose type
66 is indicated by a tag. */
69 /* Must leave 0 for the stopper. */
71 /* Cgraph node without body available. */
72 LTO_symtab_unavail_node
= 1,
73 /* Cgraph node with function body. */
74 LTO_symtab_analyzed_node
,
77 LTO_symtab_indirect_edge
,
82 /* Create a new symtab encoder.
83 if FOR_INPUT, the encoder allocate only datastructures needed
84 to read the symtab. */
87 lto_symtab_encoder_new (bool for_input
)
89 lto_symtab_encoder_t encoder
= XCNEW (struct lto_symtab_encoder_d
);
92 encoder
->map
= pointer_map_create ();
93 encoder
->nodes
.create (0);
98 /* Delete ENCODER and its components. */
101 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder
)
103 encoder
->nodes
.release ();
105 pointer_map_destroy (encoder
->map
);
110 /* Return the existing reference number of NODE in the symtab encoder in
111 output block OB. Assign a new reference if this is the first time
115 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder
,
123 lto_encoder_entry entry
= {node
, false, false, false};
125 ref
= encoder
->nodes
.length ();
126 encoder
->nodes
.safe_push (entry
);
130 slot
= pointer_map_contains (encoder
->map
, node
);
133 lto_encoder_entry entry
= {node
, false, false, false};
134 ref
= encoder
->nodes
.length ();
136 slot
= pointer_map_insert (encoder
->map
, node
);
137 *slot
= (void *) (intptr_t) (ref
+ 1);
138 encoder
->nodes
.safe_push (entry
);
141 ref
= (size_t) *slot
- 1;
146 /* Remove NODE from encoder. */
149 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder
,
152 void **slot
, **last_slot
;
154 lto_encoder_entry last_node
;
156 slot
= pointer_map_contains (encoder
->map
, node
);
157 if (slot
== NULL
|| !*slot
)
160 index
= (size_t) *slot
- 1;
161 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
163 /* Remove from vector. We do this by swapping node with the last element
165 last_node
= encoder
->nodes
.pop ();
166 if (last_node
.node
!= node
)
168 last_slot
= pointer_map_contains (encoder
->map
, last_node
.node
);
169 gcc_checking_assert (last_slot
&& *last_slot
);
170 *last_slot
= (void *)(size_t) (index
+ 1);
172 /* Move the last element to the original spot of NODE. */
173 encoder
->nodes
[index
] = last_node
;
176 /* Remove element from hash table. */
182 /* Return TRUE if we should encode initializer of NODE (if any). */
185 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder
,
186 struct cgraph_node
*node
)
188 int index
= lto_symtab_encoder_lookup (encoder
, node
);
189 return encoder
->nodes
[index
].body
;
192 /* Return TRUE if we should encode body of NODE (if any). */
195 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder
,
196 struct cgraph_node
*node
)
198 int index
= lto_symtab_encoder_encode (encoder
, node
);
199 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
200 encoder
->nodes
[index
].body
= true;
203 /* Return TRUE if we should encode initializer of NODE (if any). */
206 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder
,
207 struct varpool_node
*node
)
209 int index
= lto_symtab_encoder_lookup (encoder
, node
);
210 if (index
== LCC_NOT_FOUND
)
212 return encoder
->nodes
[index
].initializer
;
215 /* Return TRUE if we should encode initializer of NODE (if any). */
218 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder
,
219 struct varpool_node
*node
)
221 int index
= lto_symtab_encoder_lookup (encoder
, node
);
222 encoder
->nodes
[index
].initializer
= true;
225 /* Return TRUE if we should encode initializer of NODE (if any). */
228 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder
,
231 int index
= lto_symtab_encoder_lookup (encoder
, node
);
232 if (index
== LCC_NOT_FOUND
)
234 return encoder
->nodes
[index
].in_partition
;
237 /* Return TRUE if we should encode body of NODE (if any). */
240 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder
,
243 int index
= lto_symtab_encoder_encode (encoder
, node
);
244 encoder
->nodes
[index
].in_partition
= true;
247 /* Output the cgraph EDGE to OB using ENCODER. */
250 lto_output_edge (struct lto_simple_output_block
*ob
, struct cgraph_edge
*edge
,
251 lto_symtab_encoder_t encoder
)
257 if (edge
->indirect_unknown_callee
)
258 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
259 LTO_symtab_indirect_edge
);
261 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
264 ref
= lto_symtab_encoder_lookup (encoder
, edge
->caller
);
265 gcc_assert (ref
!= LCC_NOT_FOUND
);
266 streamer_write_hwi_stream (ob
->main_stream
, ref
);
268 if (!edge
->indirect_unknown_callee
)
270 ref
= lto_symtab_encoder_lookup (encoder
, edge
->callee
);
271 gcc_assert (ref
!= LCC_NOT_FOUND
);
272 streamer_write_hwi_stream (ob
->main_stream
, ref
);
275 streamer_write_gcov_count_stream (ob
->main_stream
, edge
->count
);
277 bp
= bitpack_create (ob
->main_stream
);
278 uid
= (!gimple_has_body_p (edge
->caller
->decl
)
279 ? edge
->lto_stmt_uid
: gimple_uid (edge
->call_stmt
) + 1);
280 bp_pack_enum (&bp
, cgraph_inline_failed_enum
,
281 CIF_N_REASONS
, edge
->inline_failed
);
282 bp_pack_var_len_unsigned (&bp
, uid
);
283 bp_pack_var_len_unsigned (&bp
, edge
->frequency
);
284 bp_pack_value (&bp
, edge
->indirect_inlining_edge
, 1);
285 bp_pack_value (&bp
, edge
->speculative
, 1);
286 bp_pack_value (&bp
, edge
->call_stmt_cannot_inline_p
, 1);
287 bp_pack_value (&bp
, edge
->can_throw_external
, 1);
288 if (edge
->indirect_unknown_callee
)
290 int flags
= edge
->indirect_info
->ecf_flags
;
291 bp_pack_value (&bp
, (flags
& ECF_CONST
) != 0, 1);
292 bp_pack_value (&bp
, (flags
& ECF_PURE
) != 0, 1);
293 bp_pack_value (&bp
, (flags
& ECF_NORETURN
) != 0, 1);
294 bp_pack_value (&bp
, (flags
& ECF_MALLOC
) != 0, 1);
295 bp_pack_value (&bp
, (flags
& ECF_NOTHROW
) != 0, 1);
296 bp_pack_value (&bp
, (flags
& ECF_RETURNS_TWICE
) != 0, 1);
297 /* Flags that should not appear on indirect calls. */
298 gcc_assert (!(flags
& (ECF_LOOPING_CONST_OR_PURE
304 streamer_write_bitpack (&bp
);
305 if (edge
->indirect_unknown_callee
)
307 streamer_write_hwi_stream (ob
->main_stream
,
308 edge
->indirect_info
->common_target_id
);
309 if (edge
->indirect_info
->common_target_id
)
310 streamer_write_hwi_stream
311 (ob
->main_stream
, edge
->indirect_info
->common_target_probability
);
315 /* Return if LIST contain references from other partitions. */
318 referenced_from_other_partition_p (struct ipa_ref_list
*list
, lto_symtab_encoder_t encoder
)
322 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
324 if (ref
->referring
->in_other_partition
325 || !lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
331 /* Return true when node is reachable from other partition. */
334 reachable_from_other_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
336 struct cgraph_edge
*e
;
337 if (!node
->definition
)
339 if (node
->global
.inlined_to
)
341 for (e
= node
->callers
; e
; e
= e
->next_caller
)
342 if (e
->caller
->in_other_partition
343 || !lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
348 /* Return if LIST contain references from other partitions. */
351 referenced_from_this_partition_p (struct ipa_ref_list
*list
,
352 lto_symtab_encoder_t encoder
)
356 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
357 if (lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
362 /* Return true when node is reachable from other partition. */
365 reachable_from_this_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
367 struct cgraph_edge
*e
;
368 for (e
= node
->callers
; e
; e
= e
->next_caller
)
369 if (lto_symtab_encoder_in_partition_p (encoder
, e
->caller
))
374 /* Output the cgraph NODE to OB. ENCODER is used to find the
375 reference number of NODE->inlined_to. SET is the set of nodes we
376 are writing to the current file. If NODE is not in SET, then NODE
377 is a boundary of a cgraph_node_set and we pretend NODE just has a
378 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
379 that have had their callgraph node written so far. This is used to
380 determine if NODE is a clone of a previously written node. */
383 lto_output_node (struct lto_simple_output_block
*ob
, struct cgraph_node
*node
,
384 lto_symtab_encoder_t encoder
)
390 bool in_other_partition
= false;
391 struct cgraph_node
*clone_of
, *ultimate_clone_of
;
392 struct ipa_opt_pass_d
*pass
;
396 boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
398 if (node
->analyzed
&& !boundary_p
)
399 tag
= LTO_symtab_analyzed_node
;
401 tag
= LTO_symtab_unavail_node
;
403 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
405 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
407 /* In WPA mode, we only output part of the call-graph. Also, we
408 fake cgraph node attributes. There are two cases that we care.
410 Boundary nodes: There are nodes that are not part of SET but are
411 called from within SET. We artificially make them look like
412 externally visible nodes with no function body.
414 Cherry-picked nodes: These are nodes we pulled from other
415 translation units into SET during IPA-inlining. We make them as
416 local static nodes to prevent clashes with other local statics. */
417 if (boundary_p
&& node
->analyzed
&& !DECL_EXTERNAL (node
->decl
))
419 /* Inline clones can not be part of boundary.
420 gcc_assert (!node->global.inlined_to);
422 FIXME: At the moment they can be, when partition contains an inline
423 clone that is clone of inline clone from outside partition. We can
424 reshape the clone tree and make other tree to be the root, but it
425 needs a bit extra work and will be promplty done by cgraph_remove_node
426 after reading back. */
427 in_other_partition
= 1;
430 clone_of
= node
->clone_of
;
432 && (ref
= lto_symtab_encoder_lookup (encoder
, clone_of
)) == LCC_NOT_FOUND
)
433 if (clone_of
->prev_sibling_clone
)
434 clone_of
= clone_of
->prev_sibling_clone
;
436 clone_of
= clone_of
->clone_of
;
438 /* See if body of the master function is output. If not, we are seeing only
439 an declaration and we do not need to pass down clone tree. */
440 ultimate_clone_of
= clone_of
;
441 while (ultimate_clone_of
&& ultimate_clone_of
->clone_of
)
442 ultimate_clone_of
= ultimate_clone_of
->clone_of
;
444 if (clone_of
&& !lto_symtab_encoder_encode_body_p (encoder
, ultimate_clone_of
))
447 if (tag
== LTO_symtab_analyzed_node
)
448 gcc_assert (clone_of
|| !node
->clone_of
);
450 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
452 streamer_write_hwi_stream (ob
->main_stream
, ref
);
455 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
456 streamer_write_gcov_count_stream (ob
->main_stream
, node
->count
);
457 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
459 streamer_write_hwi_stream (ob
->main_stream
,
460 node
->ipa_transforms_to_apply
.length ());
461 FOR_EACH_VEC_ELT (node
->ipa_transforms_to_apply
, i
, pass
)
462 streamer_write_hwi_stream (ob
->main_stream
, pass
->static_pass_number
);
464 if (tag
== LTO_symtab_analyzed_node
)
466 if (node
->global
.inlined_to
)
468 ref
= lto_symtab_encoder_lookup (encoder
, node
->global
.inlined_to
);
469 gcc_assert (ref
!= LCC_NOT_FOUND
);
474 streamer_write_hwi_stream (ob
->main_stream
, ref
);
477 if (node
->same_comdat_group
&& !boundary_p
)
479 ref
= lto_symtab_encoder_lookup (encoder
,
480 node
->same_comdat_group
);
481 gcc_assert (ref
!= LCC_NOT_FOUND
);
485 streamer_write_hwi_stream (ob
->main_stream
, ref
);
487 streamer_write_hwi_stream (ob
->main_stream
, node
->tp_first_run
);
489 bp
= bitpack_create (ob
->main_stream
);
490 bp_pack_value (&bp
, node
->local
.local
, 1);
491 bp_pack_value (&bp
, node
->externally_visible
, 1);
492 bp_pack_value (&bp
, node
->definition
, 1);
493 bp_pack_value (&bp
, node
->local
.versionable
, 1);
494 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
495 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
496 bp_pack_value (&bp
, node
->force_output
, 1);
497 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
498 bp_pack_value (&bp
, node
->unique_name
, 1);
499 bp_pack_value (&bp
, node
->address_taken
, 1);
500 bp_pack_value (&bp
, tag
== LTO_symtab_analyzed_node
501 && !DECL_EXTERNAL (node
->decl
)
502 && !DECL_COMDAT (node
->decl
)
503 && (reachable_from_other_partition_p (node
, encoder
)
504 || referenced_from_other_partition_p (&node
->ref_list
,
506 bp_pack_value (&bp
, node
->lowered
, 1);
507 bp_pack_value (&bp
, in_other_partition
, 1);
508 /* Real aliases in a boundary become non-aliases. However we still stream
509 alias info on weakrefs.
510 TODO: We lose a bit of information here - when we know that variable is
511 defined in other unit, we may use the info on aliases to resolve
512 symbol1 != symbol2 type tests that we can do only for locally defined objects
514 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
515 bp_pack_value (&bp
, alias_p
, 1);
516 bp_pack_value (&bp
, node
->weakref
, 1);
517 bp_pack_value (&bp
, node
->frequency
, 2);
518 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
519 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
520 bp_pack_value (&bp
, node
->tm_clone
, 1);
521 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
522 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
523 LDPR_NUM_KNOWN
, node
->resolution
);
524 streamer_write_bitpack (&bp
);
526 if (node
->thunk
.thunk_p
&& !boundary_p
)
528 streamer_write_uhwi_stream
530 1 + (node
->thunk
.this_adjusting
!= 0) * 2
531 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
532 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
533 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
535 streamer_write_hwi_stream (ob
->main_stream
, node
->profile_id
);
538 /* Output the varpool NODE to OB.
539 If NODE is not in SET, then NODE is a boundary. */
542 lto_output_varpool_node (struct lto_simple_output_block
*ob
, struct varpool_node
*node
,
543 lto_symtab_encoder_t encoder
)
545 bool boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, node
);
550 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
551 LTO_symtab_variable
);
552 streamer_write_hwi_stream (ob
->main_stream
, node
->order
);
553 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->decl
);
554 bp
= bitpack_create (ob
->main_stream
);
555 bp_pack_value (&bp
, node
->externally_visible
, 1);
556 bp_pack_value (&bp
, node
->force_output
, 1);
557 bp_pack_value (&bp
, node
->forced_by_abi
, 1);
558 bp_pack_value (&bp
, node
->unique_name
, 1);
559 bp_pack_value (&bp
, node
->definition
, 1);
560 alias_p
= node
->alias
&& (!boundary_p
|| node
->weakref
);
561 bp_pack_value (&bp
, alias_p
, 1);
562 bp_pack_value (&bp
, node
->weakref
, 1);
563 bp_pack_value (&bp
, node
->analyzed
&& !boundary_p
, 1);
564 gcc_assert (node
->definition
|| !node
->analyzed
);
565 /* Constant pool initializers can be de-unified into individual ltrans units.
566 FIXME: Alternatively at -Os we may want to avoid generating for them the local
567 labels and share them across LTRANS partitions. */
568 if (DECL_IN_CONSTANT_POOL (node
->decl
)
569 && !DECL_EXTERNAL (node
->decl
)
570 && !DECL_COMDAT (node
->decl
))
572 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
573 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
577 bp_pack_value (&bp
, node
->definition
578 && referenced_from_other_partition_p (&node
->ref_list
,
580 bp_pack_value (&bp
, node
->analyzed
581 && boundary_p
&& !DECL_EXTERNAL (node
->decl
), 1);
582 /* in_other_partition. */
584 streamer_write_bitpack (&bp
);
585 if (node
->same_comdat_group
&& !boundary_p
)
587 ref
= lto_symtab_encoder_lookup (encoder
,
588 node
->same_comdat_group
);
589 gcc_assert (ref
!= LCC_NOT_FOUND
);
593 streamer_write_hwi_stream (ob
->main_stream
, ref
);
594 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
595 LDPR_NUM_KNOWN
, node
->resolution
);
598 /* Output the varpool NODE to OB.
599 If NODE is not in SET, then NODE is a boundary. */
602 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
603 lto_symtab_encoder_t encoder
)
607 int uid
= ref
->lto_stmt_uid
;
608 struct cgraph_node
*node
;
610 bp
= bitpack_create (ob
->main_stream
);
611 bp_pack_value (&bp
, ref
->use
, 2);
612 bp_pack_value (&bp
, ref
->speculative
, 1);
613 streamer_write_bitpack (&bp
);
614 nref
= lto_symtab_encoder_lookup (encoder
, ref
->referred
);
615 gcc_assert (nref
!= LCC_NOT_FOUND
);
616 streamer_write_hwi_stream (ob
->main_stream
, nref
);
618 node
= dyn_cast
<cgraph_node
> (ref
->referring
);
622 uid
= gimple_uid (ref
->stmt
) + 1;
623 streamer_write_hwi_stream (ob
->main_stream
, uid
);
627 /* Stream out profile_summary to OB. */
630 output_profile_summary (struct lto_simple_output_block
*ob
)
637 /* We do not output num and run_max, they are not used by
638 GCC profile feedback and they are difficult to merge from multiple
640 gcc_assert (profile_info
->runs
);
641 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
642 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_max
);
644 /* sum_all is needed for computing the working set with the
646 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_all
);
648 /* Create and output a bitpack of non-zero histogram entries indices. */
649 bp
= bitpack_create (ob
->main_stream
);
650 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
651 bp_pack_value (&bp
, profile_info
->histogram
[h_ix
].num_counters
> 0, 1);
652 streamer_write_bitpack (&bp
);
653 /* Now stream out only those non-zero entries. */
654 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
656 if (!profile_info
->histogram
[h_ix
].num_counters
)
658 streamer_write_gcov_count_stream (ob
->main_stream
,
659 profile_info
->histogram
[h_ix
].num_counters
);
660 streamer_write_gcov_count_stream (ob
->main_stream
,
661 profile_info
->histogram
[h_ix
].min_value
);
662 streamer_write_gcov_count_stream (ob
->main_stream
,
663 profile_info
->histogram
[h_ix
].cum_value
);
665 /* IPA-profile computes hot bb threshold based on cumulated
666 whole program profile. We need to stream it down to ltrans. */
668 streamer_write_gcov_count_stream (ob
->main_stream
,
669 get_hot_bb_threshold ());
672 streamer_write_uhwi_stream (ob
->main_stream
, 0);
675 /* Output all callees or indirect outgoing edges. EDGE must be the first such
679 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
680 struct lto_simple_output_block
*ob
,
681 lto_symtab_encoder_t encoder
)
686 /* Output edges in backward direction, so the reconstructed callgraph match
687 and it is easy to associate call sites in the IPA pass summaries. */
688 while (edge
->next_callee
)
689 edge
= edge
->next_callee
;
690 for (; edge
; edge
= edge
->prev_callee
)
691 lto_output_edge (ob
, edge
, encoder
);
694 /* Output the part of the cgraph in SET. */
697 output_refs (lto_symtab_encoder_t encoder
)
699 lto_symtab_encoder_iterator lsei
;
700 struct lto_simple_output_block
*ob
;
705 ob
= lto_create_simple_output_block (LTO_section_refs
);
707 for (lsei
= lsei_start_in_partition (encoder
); !lsei_end_p (lsei
);
708 lsei_next_in_partition (&lsei
))
710 symtab_node
*node
= lsei_node (lsei
);
712 count
= ipa_ref_list_nreferences (&node
->ref_list
);
715 streamer_write_gcov_count_stream (ob
->main_stream
, count
);
716 streamer_write_uhwi_stream (ob
->main_stream
,
717 lto_symtab_encoder_lookup (encoder
, node
));
718 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
,
720 lto_output_ref (ob
, ref
, encoder
);
724 streamer_write_uhwi_stream (ob
->main_stream
, 0);
726 lto_destroy_simple_output_block (ob
);
729 /* Add NODE into encoder as well as nodes it is cloned from.
730 Do it in a way so clones appear first. */
733 add_node_to (lto_symtab_encoder_t encoder
, struct cgraph_node
*node
,
737 add_node_to (encoder
, node
->clone_of
, include_body
);
738 else if (include_body
)
739 lto_set_symtab_encoder_encode_body (encoder
, node
);
740 lto_symtab_encoder_encode (encoder
, node
);
743 /* Add all references in LIST to encoders. */
746 add_references (lto_symtab_encoder_t encoder
,
747 struct ipa_ref_list
*list
)
751 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
752 if (is_a
<cgraph_node
> (ref
->referred
))
753 add_node_to (encoder
, ipa_ref_node (ref
), false);
755 lto_symtab_encoder_encode (encoder
, ref
->referred
);
758 /* Find all symbols we want to stream into given partition and insert them
761 The function actually replaces IN_ENCODER by new one. The reason is that
762 streaming code needs clone's origin to be streamed before clone. This
763 means that we need to insert the nodes in specific order. This order is
764 ignored by the partitioning logic earlier. */
767 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder
)
769 struct cgraph_node
*node
;
770 struct cgraph_edge
*edge
;
772 lto_symtab_encoder_t encoder
;
773 lto_symtab_encoder_iterator lsei
;
774 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
776 encoder
= lto_symtab_encoder_new (false);
778 /* Go over all entries in the IN_ENCODER and duplicate them to
779 ENCODER. At the same time insert masters of clones so
780 every master appears before clone. */
781 for (lsei
= lsei_start_function_in_partition (in_encoder
);
782 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
784 node
= lsei_cgraph_node (lsei
);
785 add_node_to (encoder
, node
, true);
786 lto_set_symtab_encoder_in_partition (encoder
, node
);
787 add_references (encoder
, &node
->ref_list
);
788 /* For proper debug info, we need to ship the origins, too. */
789 if (DECL_ABSTRACT_ORIGIN (node
->decl
))
791 struct cgraph_node
*origin_node
792 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node
->decl
));
793 add_node_to (encoder
, origin_node
, true);
796 for (lsei
= lsei_start_variable_in_partition (in_encoder
);
797 !lsei_end_p (lsei
); lsei_next_variable_in_partition (&lsei
))
799 struct varpool_node
*vnode
= lsei_varpool_node (lsei
);
801 lto_set_symtab_encoder_in_partition (encoder
, vnode
);
802 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
803 add_references (encoder
, &vnode
->ref_list
);
804 /* For proper debug info, we need to ship the origins, too. */
805 if (DECL_ABSTRACT_ORIGIN (vnode
->decl
))
807 struct varpool_node
*origin_node
808 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node
->decl
));
809 lto_set_symtab_encoder_in_partition (encoder
, origin_node
);
812 /* Pickle in also the initializer of all referenced readonly variables
813 to help folding. Constant pool variables are not shared, so we must
815 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
817 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
818 if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
820 if (!lto_symtab_encoder_encode_initializer_p (encoder
,
822 && ctor_for_folding (vnode
->decl
) != error_mark_node
)
824 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
825 add_references (encoder
, &vnode
->ref_list
);
830 /* Go over all the nodes again to include callees that are not in
832 for (lsei
= lsei_start_function_in_partition (encoder
);
833 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
835 node
= lsei_cgraph_node (lsei
);
836 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
838 struct cgraph_node
*callee
= edge
->callee
;
839 if (!lto_symtab_encoder_in_partition_p (encoder
, callee
))
841 /* We should have moved all the inlines. */
842 gcc_assert (!callee
->global
.inlined_to
);
843 add_node_to (encoder
, callee
, false);
846 /* Add all possible targets for late devirtualization. */
847 if (flag_devirtualize
)
848 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
849 if (edge
->indirect_info
->polymorphic
)
854 vec
<cgraph_node
*>targets
855 = possible_polymorphic_call_targets
856 (edge
, &final
, &cache_token
);
857 if (!pointer_set_insert (reachable_call_targets
,
860 for (i
= 0; i
< targets
.length (); i
++)
862 struct cgraph_node
*callee
= targets
[i
];
864 /* Adding an external declarations into the unit serves
865 no purpose and just increases its boundary. */
866 if (callee
->definition
867 && !lto_symtab_encoder_in_partition_p
870 gcc_assert (!callee
->global
.inlined_to
);
871 add_node_to (encoder
, callee
, false);
877 lto_symtab_encoder_delete (in_encoder
);
878 pointer_set_destroy (reachable_call_targets
);
882 /* Output the part of the symtab in SET and VSET. */
887 struct cgraph_node
*node
;
888 struct lto_simple_output_block
*ob
;
889 lto_symtab_encoder_iterator lsei
;
891 lto_symtab_encoder_t encoder
;
892 static bool asm_nodes_output
= false;
895 output_cgraph_opt_summary ();
897 ob
= lto_create_simple_output_block (LTO_section_symtab_nodes
);
899 output_profile_summary (ob
);
901 /* An encoder for cgraph nodes should have been created by
902 ipa_write_summaries_1. */
903 gcc_assert (ob
->decl_state
->symtab_node_encoder
);
904 encoder
= ob
->decl_state
->symtab_node_encoder
;
906 /* Write out the nodes. We must first output a node and then its clones,
907 otherwise at a time reading back the node there would be nothing to clone
909 n_nodes
= lto_symtab_encoder_size (encoder
);
910 for (i
= 0; i
< n_nodes
; i
++)
912 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
913 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
914 lto_output_node (ob
, cnode
, encoder
);
916 lto_output_varpool_node (ob
, varpool (node
), encoder
);
920 /* Go over the nodes in SET again to write edges. */
921 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
922 lsei_next_function_in_partition (&lsei
))
924 node
= lsei_cgraph_node (lsei
);
925 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
926 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
929 streamer_write_uhwi_stream (ob
->main_stream
, 0);
931 lto_destroy_simple_output_block (ob
);
933 /* Emit toplevel asms.
934 When doing WPA we must output every asm just once. Since we do not partition asm
935 nodes at all, output them to first output. This is kind of hack, but should work
937 if (!asm_nodes_output
)
939 asm_nodes_output
= true;
940 lto_output_toplevel_asms ();
943 output_refs (encoder
);
946 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
947 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
948 NODE or to replace the values in it, for instance because the first
949 time we saw it, the function body was not available but now it
950 is. BP is a bitpack with all the bitflags for NODE read from the
954 input_overwrite_node (struct lto_file_decl_data
*file_data
,
955 struct cgraph_node
*node
,
956 enum LTO_symtab_tags tag
,
957 struct bitpack_d
*bp
)
959 node
->aux
= (void *) tag
;
960 node
->lto_file_data
= file_data
;
962 node
->local
.local
= bp_unpack_value (bp
, 1);
963 node
->externally_visible
= bp_unpack_value (bp
, 1);
964 node
->definition
= bp_unpack_value (bp
, 1);
965 node
->local
.versionable
= bp_unpack_value (bp
, 1);
966 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
967 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
968 node
->force_output
= bp_unpack_value (bp
, 1);
969 node
->forced_by_abi
= bp_unpack_value (bp
, 1);
970 node
->unique_name
= bp_unpack_value (bp
, 1);
971 node
->address_taken
= bp_unpack_value (bp
, 1);
972 node
->used_from_other_partition
= bp_unpack_value (bp
, 1);
973 node
->lowered
= bp_unpack_value (bp
, 1);
974 node
->analyzed
= tag
== LTO_symtab_analyzed_node
;
975 node
->in_other_partition
= bp_unpack_value (bp
, 1);
976 if (node
->in_other_partition
977 /* Avoid updating decl when we are seeing just inline clone.
978 When inlining function that has functions already inlined into it,
979 we produce clones of inline clones.
981 WPA partitioning might put each clone into different unit and
982 we might end up streaming inline clone from other partition
983 to support clone we are interested in. */
985 || node
->clone_of
->decl
!= node
->decl
))
987 DECL_EXTERNAL (node
->decl
) = 1;
988 TREE_STATIC (node
->decl
) = 0;
990 node
->alias
= bp_unpack_value (bp
, 1);
991 node
->weakref
= bp_unpack_value (bp
, 1);
992 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
993 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
994 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
995 node
->tm_clone
= bp_unpack_value (bp
, 1);
996 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
997 node
->resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
1001 /* Return string alias is alias of. */
1004 get_alias_symbol (tree decl
)
1006 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1007 return get_identifier (TREE_STRING_POINTER
1008 (TREE_VALUE (TREE_VALUE (alias
))));
1011 /* Read a node from input_block IB. TAG is the node's tag just read.
1012 Return the node read or overwriten. */
1014 static struct cgraph_node
*
1015 input_node (struct lto_file_decl_data
*file_data
,
1016 struct lto_input_block
*ib
,
1017 enum LTO_symtab_tags tag
,
1018 vec
<symtab_node
*> nodes
)
1020 gcc::pass_manager
*passes
= g
->get_passes ();
1022 struct cgraph_node
*node
;
1023 struct bitpack_d bp
;
1024 unsigned decl_index
;
1025 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
1030 order
= streamer_read_hwi (ib
) + order_base
;
1031 clone_ref
= streamer_read_hwi (ib
);
1033 decl_index
= streamer_read_uhwi (ib
);
1034 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
1036 if (clone_ref
!= LCC_NOT_FOUND
)
1038 node
= cgraph_clone_node (cgraph (nodes
[clone_ref
]), fn_decl
,
1039 0, CGRAPH_FREQ_BASE
, false,
1040 vNULL
, false, NULL
);
1044 /* Declaration of functions can be already merged with a declaration
1045 from other input file. We keep cgraph unmerged until after streaming
1046 of ipa passes is done. Alays forcingly create a fresh node. */
1047 node
= cgraph_create_empty_node ();
1048 node
->decl
= fn_decl
;
1049 symtab_register_node (node
);
1052 node
->order
= order
;
1053 if (order
>= symtab_order
)
1054 symtab_order
= order
+ 1;
1056 node
->count
= streamer_read_gcov_count (ib
);
1057 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1059 count
= streamer_read_hwi (ib
);
1060 node
->ipa_transforms_to_apply
= vNULL
;
1061 for (i
= 0; i
< count
; i
++)
1063 struct opt_pass
*pass
;
1064 int pid
= streamer_read_hwi (ib
);
1066 gcc_assert (pid
< passes
->passes_by_id_size
);
1067 pass
= passes
->passes_by_id
[pid
];
1068 node
->ipa_transforms_to_apply
.safe_push ((struct ipa_opt_pass_d
*) pass
);
1071 if (tag
== LTO_symtab_analyzed_node
)
1072 ref
= streamer_read_hwi (ib
);
1074 ref2
= streamer_read_hwi (ib
);
1076 /* Make sure that we have not read this node before. Nodes that
1077 have already been read will have their tag stored in the 'aux'
1078 field. Since built-in functions can be referenced in multiple
1079 functions, they are expected to be read more than once. */
1080 if (node
->aux
&& !DECL_BUILT_IN (node
->decl
))
1081 internal_error ("bytecode stream: found multiple instances of cgraph "
1082 "node with uid %d", node
->uid
);
1084 node
->tp_first_run
= streamer_read_uhwi (ib
);
1086 bp
= streamer_read_bitpack (ib
);
1088 input_overwrite_node (file_data
, node
, tag
, &bp
);
1090 /* Store a reference for now, and fix up later to be a pointer. */
1091 node
->global
.inlined_to
= (cgraph_node_ptr
) (intptr_t) ref
;
1093 /* Store a reference for now, and fix up later to be a pointer. */
1094 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref2
;
1096 if (node
->thunk
.thunk_p
)
1098 int type
= streamer_read_uhwi (ib
);
1099 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1100 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1102 node
->thunk
.fixed_offset
= fixed_offset
;
1103 node
->thunk
.this_adjusting
= (type
& 2);
1104 node
->thunk
.virtual_value
= virtual_value
;
1105 node
->thunk
.virtual_offset_p
= (type
& 4);
1107 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1108 node
->alias_target
= get_alias_symbol (node
->decl
);
1109 node
->profile_id
= streamer_read_hwi (ib
);
1113 /* Read a node from input_block IB. TAG is the node's tag just read.
1114 Return the node read or overwriten. */
1116 static struct varpool_node
*
1117 input_varpool_node (struct lto_file_decl_data
*file_data
,
1118 struct lto_input_block
*ib
)
1122 struct varpool_node
*node
;
1123 struct bitpack_d bp
;
1124 int ref
= LCC_NOT_FOUND
;
1127 order
= streamer_read_hwi (ib
) + order_base
;
1128 decl_index
= streamer_read_uhwi (ib
);
1129 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1131 /* Declaration of functions can be already merged with a declaration
1132 from other input file. We keep cgraph unmerged until after streaming
1133 of ipa passes is done. Alays forcingly create a fresh node. */
1134 node
= varpool_create_empty_node ();
1135 node
->decl
= var_decl
;
1136 symtab_register_node (node
);
1138 node
->order
= order
;
1139 if (order
>= symtab_order
)
1140 symtab_order
= order
+ 1;
1141 node
->lto_file_data
= file_data
;
1143 bp
= streamer_read_bitpack (ib
);
1144 node
->externally_visible
= bp_unpack_value (&bp
, 1);
1145 node
->force_output
= bp_unpack_value (&bp
, 1);
1146 node
->forced_by_abi
= bp_unpack_value (&bp
, 1);
1147 node
->unique_name
= bp_unpack_value (&bp
, 1);
1148 node
->definition
= bp_unpack_value (&bp
, 1);
1149 node
->alias
= bp_unpack_value (&bp
, 1);
1150 node
->weakref
= bp_unpack_value (&bp
, 1);
1151 node
->analyzed
= bp_unpack_value (&bp
, 1);
1152 node
->used_from_other_partition
= bp_unpack_value (&bp
, 1);
1153 node
->in_other_partition
= bp_unpack_value (&bp
, 1);
1154 if (node
->in_other_partition
)
1156 DECL_EXTERNAL (node
->decl
) = 1;
1157 TREE_STATIC (node
->decl
) = 0;
1159 if (node
->alias
&& !node
->analyzed
&& node
->weakref
)
1160 node
->alias_target
= get_alias_symbol (node
->decl
);
1161 ref
= streamer_read_hwi (ib
);
1162 /* Store a reference for now, and fix up later to be a pointer. */
1163 node
->same_comdat_group
= (symtab_node
*) (intptr_t) ref
;
1164 node
->resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1170 /* Read a node from input_block IB. TAG is the node's tag just read.
1171 Return the node read or overwriten. */
1174 input_ref (struct lto_input_block
*ib
,
1175 symtab_node
*referring_node
,
1176 vec
<symtab_node
*> nodes
)
1178 symtab_node
*node
= NULL
;
1179 struct bitpack_d bp
;
1180 enum ipa_ref_use use
;
1182 struct ipa_ref
*ref
;
1184 bp
= streamer_read_bitpack (ib
);
1185 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1186 speculative
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 1);
1187 node
= nodes
[streamer_read_hwi (ib
)];
1188 ref
= ipa_record_reference (referring_node
, node
, use
, NULL
);
1189 ref
->speculative
= speculative
;
1190 if (is_a
<cgraph_node
> (referring_node
))
1191 ref
->lto_stmt_uid
= streamer_read_hwi (ib
);
1194 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1195 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1196 edge being read is indirect (in the sense that it has
1197 indirect_unknown_callee set). */
1200 input_edge (struct lto_input_block
*ib
, vec
<symtab_node
*> nodes
,
1203 struct cgraph_node
*caller
, *callee
;
1204 struct cgraph_edge
*edge
;
1205 unsigned int stmt_id
;
1208 cgraph_inline_failed_t inline_failed
;
1209 struct bitpack_d bp
;
1212 caller
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1213 if (caller
== NULL
|| caller
->decl
== NULL_TREE
)
1214 internal_error ("bytecode stream: no caller found while reading edge");
1218 callee
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1219 if (callee
== NULL
|| callee
->decl
== NULL_TREE
)
1220 internal_error ("bytecode stream: no callee found while reading edge");
1225 count
= streamer_read_gcov_count (ib
);
1227 bp
= streamer_read_bitpack (ib
);
1228 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_enum
, CIF_N_REASONS
);
1229 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1230 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1233 edge
= cgraph_create_indirect_edge (caller
, NULL
, 0, count
, freq
);
1235 edge
= cgraph_create_edge (caller
, callee
, NULL
, count
, freq
);
1237 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1238 edge
->speculative
= bp_unpack_value (&bp
, 1);
1239 edge
->lto_stmt_uid
= stmt_id
;
1240 edge
->inline_failed
= inline_failed
;
1241 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1242 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1245 if (bp_unpack_value (&bp
, 1))
1246 ecf_flags
|= ECF_CONST
;
1247 if (bp_unpack_value (&bp
, 1))
1248 ecf_flags
|= ECF_PURE
;
1249 if (bp_unpack_value (&bp
, 1))
1250 ecf_flags
|= ECF_NORETURN
;
1251 if (bp_unpack_value (&bp
, 1))
1252 ecf_flags
|= ECF_MALLOC
;
1253 if (bp_unpack_value (&bp
, 1))
1254 ecf_flags
|= ECF_NOTHROW
;
1255 if (bp_unpack_value (&bp
, 1))
1256 ecf_flags
|= ECF_RETURNS_TWICE
;
1257 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1258 edge
->indirect_info
->common_target_id
= streamer_read_hwi (ib
);
1259 if (edge
->indirect_info
->common_target_id
)
1260 edge
->indirect_info
->common_target_probability
= streamer_read_hwi (ib
);
1265 /* Read a cgraph from IB using the info in FILE_DATA. */
1267 static vec
<symtab_node
*>
1268 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1269 struct lto_input_block
*ib
)
1271 enum LTO_symtab_tags tag
;
1272 vec
<symtab_node
*> nodes
= vNULL
;
1276 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1277 order_base
= symtab_order
;
1280 if (tag
== LTO_symtab_edge
)
1281 input_edge (ib
, nodes
, false);
1282 else if (tag
== LTO_symtab_indirect_edge
)
1283 input_edge (ib
, nodes
, true);
1284 else if (tag
== LTO_symtab_variable
)
1286 node
= input_varpool_node (file_data
, ib
);
1287 nodes
.safe_push (node
);
1288 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1292 node
= input_node (file_data
, ib
, tag
, nodes
);
1293 if (node
== NULL
|| node
->decl
== NULL_TREE
)
1294 internal_error ("bytecode stream: found empty cgraph node");
1295 nodes
.safe_push (node
);
1296 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1299 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1302 lto_input_toplevel_asms (file_data
, order_base
);
1304 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1305 #ifdef ENABLE_CHECKING
1306 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1307 gcc_assert (node
->aux
|| !is_a
<cgraph_node
> (node
));
1309 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1312 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
1314 ref
= (int) (intptr_t) cnode
->global
.inlined_to
;
1316 /* We share declaration of builtins, so we may read same node twice. */
1321 /* Fixup inlined_to from reference to pointer. */
1322 if (ref
!= LCC_NOT_FOUND
)
1323 cgraph (node
)->global
.inlined_to
= cgraph (nodes
[ref
]);
1325 cnode
->global
.inlined_to
= NULL
;
1328 ref
= (int) (intptr_t) node
->same_comdat_group
;
1330 /* Fixup same_comdat_group from reference to pointer. */
1331 if (ref
!= LCC_NOT_FOUND
)
1332 node
->same_comdat_group
= nodes
[ref
];
1334 node
->same_comdat_group
= NULL
;
1336 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1337 node
->aux
= is_a
<cgraph_node
> (node
) ? (void *)1 : NULL
;
1341 /* Input ipa_refs. */
1344 input_refs (struct lto_input_block
*ib
,
1345 vec
<symtab_node
*> nodes
)
1352 count
= streamer_read_uhwi (ib
);
1355 idx
= streamer_read_uhwi (ib
);
1359 input_ref (ib
, node
, nodes
);
1366 static struct gcov_ctr_summary lto_gcov_summary
;
1368 /* Input profile_info from IB. */
1370 input_profile_summary (struct lto_input_block
*ib
,
1371 struct lto_file_decl_data
*file_data
)
1374 struct bitpack_d bp
;
1375 unsigned int runs
= streamer_read_uhwi (ib
);
1378 file_data
->profile_info
.runs
= runs
;
1379 file_data
->profile_info
.sum_max
= streamer_read_gcov_count (ib
);
1380 file_data
->profile_info
.sum_all
= streamer_read_gcov_count (ib
);
1382 memset (file_data
->profile_info
.histogram
, 0,
1383 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1384 /* Input the bitpack of non-zero histogram indices. */
1385 bp
= streamer_read_bitpack (ib
);
1386 /* Read in and unpack the full bitpack, flagging non-zero
1387 histogram entries by setting the num_counters non-zero. */
1388 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1390 file_data
->profile_info
.histogram
[h_ix
].num_counters
1391 = bp_unpack_value (&bp
, 1);
1393 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1395 if (!file_data
->profile_info
.histogram
[h_ix
].num_counters
)
1398 file_data
->profile_info
.histogram
[h_ix
].num_counters
1399 = streamer_read_gcov_count (ib
);
1400 file_data
->profile_info
.histogram
[h_ix
].min_value
1401 = streamer_read_gcov_count (ib
);
1402 file_data
->profile_info
.histogram
[h_ix
].cum_value
1403 = streamer_read_gcov_count (ib
);
1405 /* IPA-profile computes hot bb threshold based on cumulated
1406 whole program profile. We need to stream it down to ltrans. */
1408 set_hot_bb_threshold (streamer_read_gcov_count (ib
));
1413 /* Rescale profile summaries to the same number of runs in the whole unit. */
1416 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1418 struct lto_file_decl_data
*file_data
;
1419 unsigned int j
, h_ix
;
1420 gcov_unsigned_t max_runs
= 0;
1421 struct cgraph_node
*node
;
1422 struct cgraph_edge
*edge
;
1423 gcov_type saved_sum_all
= 0;
1424 gcov_ctr_summary
*saved_profile_info
= 0;
1425 int saved_scale
= 0;
1427 /* Find unit with maximal number of runs. If we ever get serious about
1428 roundoff errors, we might also consider computing smallest common
1430 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1431 if (max_runs
< file_data
->profile_info
.runs
)
1432 max_runs
= file_data
->profile_info
.runs
;
1437 /* Simple overflow check. We probably don't need to support that many train
1438 runs. Such a large value probably imply data corruption anyway. */
1439 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1441 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1442 INT_MAX
/ REG_BR_PROB_BASE
);
1446 profile_info
= <o_gcov_summary
;
1447 lto_gcov_summary
.runs
= max_runs
;
1448 lto_gcov_summary
.sum_max
= 0;
1449 memset (lto_gcov_summary
.histogram
, 0,
1450 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1452 /* Rescale all units to the maximal number of runs.
1453 sum_max can not be easily merged, as we have no idea what files come from
1454 the same run. We do not use the info anyway, so leave it 0. */
1455 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1456 if (file_data
->profile_info
.runs
)
1458 int scale
= GCOV_COMPUTE_SCALE (max_runs
,
1459 file_data
->profile_info
.runs
);
1460 lto_gcov_summary
.sum_max
1461 = MAX (lto_gcov_summary
.sum_max
,
1462 apply_scale (file_data
->profile_info
.sum_max
, scale
));
1463 lto_gcov_summary
.sum_all
1464 = MAX (lto_gcov_summary
.sum_all
,
1465 apply_scale (file_data
->profile_info
.sum_all
, scale
));
1466 /* Save a pointer to the profile_info with the largest
1467 scaled sum_all and the scale for use in merging the
1469 if (!saved_profile_info
1470 || lto_gcov_summary
.sum_all
> saved_sum_all
)
1472 saved_profile_info
= &file_data
->profile_info
;
1473 saved_sum_all
= lto_gcov_summary
.sum_all
;
1474 saved_scale
= scale
;
1478 gcc_assert (saved_profile_info
);
1480 /* Scale up the histogram from the profile that had the largest
1481 scaled sum_all above. */
1482 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1484 /* Scale up the min value as we did the corresponding sum_all
1485 above. Use that to find the new histogram index. */
1486 gcov_type scaled_min
1487 = apply_scale (saved_profile_info
->histogram
[h_ix
].min_value
,
1489 /* The new index may be shared with another scaled histogram entry,
1490 so we need to account for a non-zero histogram entry at new_ix. */
1491 unsigned new_ix
= gcov_histo_index (scaled_min
);
1492 lto_gcov_summary
.histogram
[new_ix
].min_value
1493 = (lto_gcov_summary
.histogram
[new_ix
].num_counters
1494 ? MIN (lto_gcov_summary
.histogram
[new_ix
].min_value
, scaled_min
)
1496 /* Some of the scaled counter values would ostensibly need to be placed
1497 into different (larger) histogram buckets, but we keep things simple
1498 here and place the scaled cumulative counter value in the bucket
1499 corresponding to the scaled minimum counter value. */
1500 lto_gcov_summary
.histogram
[new_ix
].cum_value
1501 += apply_scale (saved_profile_info
->histogram
[h_ix
].cum_value
,
1503 lto_gcov_summary
.histogram
[new_ix
].num_counters
1504 += saved_profile_info
->histogram
[h_ix
].num_counters
;
1507 /* Watch roundoff errors. */
1508 if (lto_gcov_summary
.sum_max
< max_runs
)
1509 lto_gcov_summary
.sum_max
= max_runs
;
1511 /* If merging already happent at WPA time, we are done. */
1515 /* Now compute count_materialization_scale of each node.
1516 During LTRANS we already have values of count_materialization_scale
1517 computed, so just update them. */
1518 FOR_EACH_FUNCTION (node
)
1519 if (node
->lto_file_data
1520 && node
->lto_file_data
->profile_info
.runs
)
1524 scale
= RDIV (node
->count_materialization_scale
* max_runs
,
1525 node
->lto_file_data
->profile_info
.runs
);
1526 node
->count_materialization_scale
= scale
;
1528 fatal_error ("Profile information in %s corrupted",
1529 file_data
->file_name
);
1531 if (scale
== REG_BR_PROB_BASE
)
1533 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1534 edge
->count
= apply_scale (edge
->count
, scale
);
1535 node
->count
= apply_scale (node
->count
, scale
);
1539 /* Input and merge the symtab from each of the .o files passed to
1545 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1546 struct lto_file_decl_data
*file_data
;
1548 struct cgraph_node
*node
;
1550 while ((file_data
= file_data_vec
[j
++]))
1554 struct lto_input_block
*ib
;
1555 vec
<symtab_node
*> nodes
;
1557 ib
= lto_create_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1560 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1561 input_profile_summary (ib
, file_data
);
1562 file_data
->symtab_node_encoder
= lto_symtab_encoder_new (true);
1563 nodes
= input_cgraph_1 (file_data
, ib
);
1564 lto_destroy_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1567 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1570 fatal_error ("cannot find LTO section refs in %s",
1571 file_data
->file_name
);
1572 input_refs (ib
, nodes
);
1573 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1576 input_cgraph_opt_summary (nodes
);
1580 merge_profile_summaries (file_data_vec
);
1581 get_working_sets ();
1584 /* Clear out the aux field that was used to store enough state to
1585 tell which nodes should be overwritten. */
1586 FOR_EACH_FUNCTION (node
)
1588 /* Some nodes may have been created by cgraph_node. This
1589 happens when the callgraph contains nested functions. If the
1590 node for the parent function was never emitted to the gimple
1591 file, cgraph_node will create a node for it when setting the
1592 context of the nested function. */
1593 if (node
->lto_file_data
)
1598 /* True when we need optimization summary for NODE. */
1601 output_cgraph_opt_summary_p (struct cgraph_node
*node
)
1603 return (node
->clone_of
1604 && (node
->clone
.tree_map
1605 || node
->clone
.args_to_skip
1606 || node
->clone
.combined_args_to_skip
));
1609 /* Output optimization summary for EDGE to OB. */
1611 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1612 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1616 /* Output optimization summary for NODE to OB. */
1619 output_node_opt_summary (struct output_block
*ob
,
1620 struct cgraph_node
*node
,
1621 lto_symtab_encoder_t encoder
)
1625 struct ipa_replace_map
*map
;
1626 struct bitpack_d bp
;
1628 struct cgraph_edge
*e
;
1630 if (node
->clone
.args_to_skip
)
1632 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1633 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1634 streamer_write_uhwi (ob
, index
);
1637 streamer_write_uhwi (ob
, 0);
1638 if (node
->clone
.combined_args_to_skip
)
1640 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1641 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1642 streamer_write_uhwi (ob
, index
);
1645 streamer_write_uhwi (ob
, 0);
1646 streamer_write_uhwi (ob
, vec_safe_length (node
->clone
.tree_map
));
1647 FOR_EACH_VEC_SAFE_ELT (node
->clone
.tree_map
, i
, map
)
1649 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1650 mechanism to store function local declarations into summaries. */
1651 gcc_assert (!map
->old_tree
);
1652 streamer_write_uhwi (ob
, map
->parm_num
);
1653 gcc_assert (EXPR_LOCATION (map
->new_tree
) == UNKNOWN_LOCATION
);
1654 stream_write_tree (ob
, map
->new_tree
, true);
1655 bp
= bitpack_create (ob
->main_stream
);
1656 bp_pack_value (&bp
, map
->replace_p
, 1);
1657 bp_pack_value (&bp
, map
->ref_p
, 1);
1658 streamer_write_bitpack (&bp
);
1661 if (lto_symtab_encoder_in_partition_p (encoder
, node
))
1663 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1664 output_edge_opt_summary (ob
, e
);
1665 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1666 output_edge_opt_summary (ob
, e
);
1670 /* Output optimization summaries stored in callgraph.
1671 At the moment it is the clone info structure. */
1674 output_cgraph_opt_summary (void)
1677 lto_symtab_encoder_t encoder
;
1678 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1681 ob
->cgraph_node
= NULL
;
1682 encoder
= ob
->decl_state
->symtab_node_encoder
;
1683 n_nodes
= lto_symtab_encoder_size (encoder
);
1684 for (i
= 0; i
< n_nodes
; i
++)
1686 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1687 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
1688 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1691 streamer_write_uhwi (ob
, count
);
1692 for (i
= 0; i
< n_nodes
; i
++)
1694 symtab_node
*node
= lto_symtab_encoder_deref (encoder
, i
);
1695 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
1696 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1698 streamer_write_uhwi (ob
, i
);
1699 output_node_opt_summary (ob
, cnode
, encoder
);
1702 produce_asm (ob
, NULL
);
1703 destroy_output_block (ob
);
1706 /* Input optimisation summary of EDGE. */
1709 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1710 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1714 /* Input optimisation summary of NODE. */
1717 input_node_opt_summary (struct cgraph_node
*node
,
1718 struct lto_input_block
*ib_main
,
1719 struct data_in
*data_in
)
1724 struct bitpack_d bp
;
1725 struct cgraph_edge
*e
;
1727 count
= streamer_read_uhwi (ib_main
);
1729 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1730 for (i
= 0; i
< count
; i
++)
1732 bit
= streamer_read_uhwi (ib_main
);
1733 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1735 count
= streamer_read_uhwi (ib_main
);
1737 node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
1738 for (i
= 0; i
< count
; i
++)
1740 bit
= streamer_read_uhwi (ib_main
);
1741 bitmap_set_bit (node
->clone
.combined_args_to_skip
, bit
);
1743 count
= streamer_read_uhwi (ib_main
);
1744 for (i
= 0; i
< count
; i
++)
1746 struct ipa_replace_map
*map
= ggc_alloc_ipa_replace_map ();
1748 vec_safe_push (node
->clone
.tree_map
, map
);
1749 map
->parm_num
= streamer_read_uhwi (ib_main
);
1750 map
->old_tree
= NULL
;
1751 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1752 bp
= streamer_read_bitpack (ib_main
);
1753 map
->replace_p
= bp_unpack_value (&bp
, 1);
1754 map
->ref_p
= bp_unpack_value (&bp
, 1);
1756 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1757 input_edge_opt_summary (e
, ib_main
);
1758 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1759 input_edge_opt_summary (e
, ib_main
);
1762 /* Read section in file FILE_DATA of length LEN with data DATA. */
1765 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1766 const char *data
, size_t len
,
1767 vec
<symtab_node
*> nodes
)
1769 const struct lto_function_header
*header
=
1770 (const struct lto_function_header
*) data
;
1771 const int cfg_offset
= sizeof (struct lto_function_header
);
1772 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1773 const int string_offset
= main_offset
+ header
->main_size
;
1774 struct data_in
*data_in
;
1775 struct lto_input_block ib_main
;
1779 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1783 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1784 header
->string_size
, vNULL
);
1785 count
= streamer_read_uhwi (&ib_main
);
1787 for (i
= 0; i
< count
; i
++)
1789 int ref
= streamer_read_uhwi (&ib_main
);
1790 input_node_opt_summary (cgraph (nodes
[ref
]),
1793 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1795 lto_data_in_delete (data_in
);
1798 /* Input optimization summary of cgraph. */
1801 input_cgraph_opt_summary (vec
<symtab_node
*> nodes
)
1803 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1804 struct lto_file_decl_data
*file_data
;
1807 while ((file_data
= file_data_vec
[j
++]))
1811 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1815 input_cgraph_opt_section (file_data
, data
, len
, nodes
);