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"
34 #include "langhooks.h"
35 #include "basic-block.h"
39 #include "diagnostic-core.h"
43 #include "pointer-set.h"
44 #include "lto-streamer.h"
45 #include "data-streamer.h"
46 #include "tree-streamer.h"
48 #include "tree-pass.h"
51 #include "pass_manager.h"
52 #include "ipa-utils.h"
54 static void output_cgraph_opt_summary (void);
55 static void input_cgraph_opt_summary (vec
<symtab_node
> nodes
);
57 /* Number of LDPR values known to GCC. */
58 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
60 /* All node orders are ofsetted by ORDER_BASE. */
61 static int order_base
;
63 /* Cgraph streaming is organized as set of record whose type
64 is indicated by a tag. */
67 /* Must leave 0 for the stopper. */
69 /* Cgraph node without body available. */
70 LTO_symtab_unavail_node
= 1,
71 /* Cgraph node with function body. */
72 LTO_symtab_analyzed_node
,
75 LTO_symtab_indirect_edge
,
80 /* Create a new symtab encoder.
81 if FOR_INPUT, the encoder allocate only datastructures needed
82 to read the symtab. */
85 lto_symtab_encoder_new (bool for_input
)
87 lto_symtab_encoder_t encoder
= XCNEW (struct lto_symtab_encoder_d
);
90 encoder
->map
= pointer_map_create ();
91 encoder
->nodes
.create (0);
96 /* Delete ENCODER and its components. */
99 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder
)
101 encoder
->nodes
.release ();
103 pointer_map_destroy (encoder
->map
);
108 /* Return the existing reference number of NODE in the symtab encoder in
109 output block OB. Assign a new reference if this is the first time
113 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder
,
121 lto_encoder_entry entry
= {node
, false, false, false};
123 ref
= encoder
->nodes
.length ();
124 encoder
->nodes
.safe_push (entry
);
128 slot
= pointer_map_contains (encoder
->map
, node
);
131 lto_encoder_entry entry
= {node
, false, false, false};
132 ref
= encoder
->nodes
.length ();
134 slot
= pointer_map_insert (encoder
->map
, node
);
135 *slot
= (void *) (intptr_t) (ref
+ 1);
136 encoder
->nodes
.safe_push (entry
);
139 ref
= (size_t) *slot
- 1;
144 /* Remove NODE from encoder. */
147 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder
,
150 void **slot
, **last_slot
;
152 lto_encoder_entry last_node
;
154 slot
= pointer_map_contains (encoder
->map
, node
);
155 if (slot
== NULL
|| !*slot
)
158 index
= (size_t) *slot
- 1;
159 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
161 /* Remove from vector. We do this by swapping node with the last element
163 last_node
= encoder
->nodes
.pop ();
164 if (last_node
.node
!= node
)
166 last_slot
= pointer_map_contains (encoder
->map
, last_node
.node
);
167 gcc_checking_assert (last_slot
&& *last_slot
);
168 *last_slot
= (void *)(size_t) (index
+ 1);
170 /* Move the last element to the original spot of NODE. */
171 encoder
->nodes
[index
] = last_node
;
174 /* Remove element from hash table. */
180 /* Return TRUE if we should encode initializer of NODE (if any). */
183 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder
,
184 struct cgraph_node
*node
)
186 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
187 return encoder
->nodes
[index
].body
;
190 /* Return TRUE if we should encode body of NODE (if any). */
193 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder
,
194 struct cgraph_node
*node
)
196 int index
= lto_symtab_encoder_encode (encoder
, (symtab_node
)node
);
197 gcc_checking_assert (encoder
->nodes
[index
].node
== (symtab_node
)node
);
198 encoder
->nodes
[index
].body
= true;
201 /* Return TRUE if we should encode initializer of NODE (if any). */
204 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder
,
205 struct varpool_node
*node
)
207 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
208 if (index
== LCC_NOT_FOUND
)
210 return encoder
->nodes
[index
].initializer
;
213 /* Return TRUE if we should encode initializer of NODE (if any). */
216 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder
,
217 struct varpool_node
*node
)
219 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
220 encoder
->nodes
[index
].initializer
= true;
223 /* Return TRUE if we should encode initializer of NODE (if any). */
226 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder
,
229 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
230 if (index
== LCC_NOT_FOUND
)
232 return encoder
->nodes
[index
].in_partition
;
235 /* Return TRUE if we should encode body of NODE (if any). */
238 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder
,
241 int index
= lto_symtab_encoder_encode (encoder
, (symtab_node
)node
);
242 encoder
->nodes
[index
].in_partition
= true;
245 /* Output the cgraph EDGE to OB using ENCODER. */
248 lto_output_edge (struct lto_simple_output_block
*ob
, struct cgraph_edge
*edge
,
249 lto_symtab_encoder_t encoder
)
255 if (edge
->indirect_unknown_callee
)
256 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
257 LTO_symtab_indirect_edge
);
259 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
262 ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)edge
->caller
);
263 gcc_assert (ref
!= LCC_NOT_FOUND
);
264 streamer_write_hwi_stream (ob
->main_stream
, ref
);
266 if (!edge
->indirect_unknown_callee
)
268 ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)edge
->callee
);
269 gcc_assert (ref
!= LCC_NOT_FOUND
);
270 streamer_write_hwi_stream (ob
->main_stream
, ref
);
273 streamer_write_gcov_count_stream (ob
->main_stream
, edge
->count
);
275 bp
= bitpack_create (ob
->main_stream
);
276 uid
= (!gimple_has_body_p (edge
->caller
->symbol
.decl
)
277 ? edge
->lto_stmt_uid
: gimple_uid (edge
->call_stmt
) + 1);
278 bp_pack_enum (&bp
, cgraph_inline_failed_enum
,
279 CIF_N_REASONS
, edge
->inline_failed
);
280 bp_pack_var_len_unsigned (&bp
, uid
);
281 bp_pack_var_len_unsigned (&bp
, edge
->frequency
);
282 bp_pack_value (&bp
, edge
->indirect_inlining_edge
, 1);
283 bp_pack_value (&bp
, edge
->speculative
, 1);
284 bp_pack_value (&bp
, edge
->call_stmt_cannot_inline_p
, 1);
285 bp_pack_value (&bp
, edge
->can_throw_external
, 1);
286 if (edge
->indirect_unknown_callee
)
288 int flags
= edge
->indirect_info
->ecf_flags
;
289 bp_pack_value (&bp
, (flags
& ECF_CONST
) != 0, 1);
290 bp_pack_value (&bp
, (flags
& ECF_PURE
) != 0, 1);
291 bp_pack_value (&bp
, (flags
& ECF_NORETURN
) != 0, 1);
292 bp_pack_value (&bp
, (flags
& ECF_MALLOC
) != 0, 1);
293 bp_pack_value (&bp
, (flags
& ECF_NOTHROW
) != 0, 1);
294 bp_pack_value (&bp
, (flags
& ECF_RETURNS_TWICE
) != 0, 1);
295 /* Flags that should not appear on indirect calls. */
296 gcc_assert (!(flags
& (ECF_LOOPING_CONST_OR_PURE
302 streamer_write_bitpack (&bp
);
303 if (edge
->indirect_unknown_callee
)
305 streamer_write_hwi_stream (ob
->main_stream
,
306 edge
->indirect_info
->common_target_id
);
307 if (edge
->indirect_info
->common_target_id
)
308 streamer_write_hwi_stream
309 (ob
->main_stream
, edge
->indirect_info
->common_target_probability
);
313 /* Return if LIST contain references from other partitions. */
316 referenced_from_other_partition_p (struct ipa_ref_list
*list
, lto_symtab_encoder_t encoder
)
320 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
322 if (ref
->referring
->symbol
.in_other_partition
323 || !lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
329 /* Return true when node is reachable from other partition. */
332 reachable_from_other_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
334 struct cgraph_edge
*e
;
335 if (!node
->symbol
.definition
)
337 if (node
->global
.inlined_to
)
339 for (e
= node
->callers
; e
; e
= e
->next_caller
)
340 if (e
->caller
->symbol
.in_other_partition
341 || !lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)e
->caller
))
346 /* Return if LIST contain references from other partitions. */
349 referenced_from_this_partition_p (struct ipa_ref_list
*list
,
350 lto_symtab_encoder_t encoder
)
354 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
355 if (lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
360 /* Return true when node is reachable from other partition. */
363 reachable_from_this_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
365 struct cgraph_edge
*e
;
366 for (e
= node
->callers
; e
; e
= e
->next_caller
)
367 if (lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)e
->caller
))
372 /* Output the cgraph NODE to OB. ENCODER is used to find the
373 reference number of NODE->inlined_to. SET is the set of nodes we
374 are writing to the current file. If NODE is not in SET, then NODE
375 is a boundary of a cgraph_node_set and we pretend NODE just has a
376 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
377 that have had their callgraph node written so far. This is used to
378 determine if NODE is a clone of a previously written node. */
381 lto_output_node (struct lto_simple_output_block
*ob
, struct cgraph_node
*node
,
382 lto_symtab_encoder_t encoder
)
388 bool in_other_partition
= false;
389 struct cgraph_node
*clone_of
, *ultimate_clone_of
;
390 struct ipa_opt_pass_d
*pass
;
394 boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)node
);
396 if (node
->symbol
.analyzed
&& !boundary_p
)
397 tag
= LTO_symtab_analyzed_node
;
399 tag
= LTO_symtab_unavail_node
;
401 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
403 streamer_write_hwi_stream (ob
->main_stream
, node
->symbol
.order
);
405 /* In WPA mode, we only output part of the call-graph. Also, we
406 fake cgraph node attributes. There are two cases that we care.
408 Boundary nodes: There are nodes that are not part of SET but are
409 called from within SET. We artificially make them look like
410 externally visible nodes with no function body.
412 Cherry-picked nodes: These are nodes we pulled from other
413 translation units into SET during IPA-inlining. We make them as
414 local static nodes to prevent clashes with other local statics. */
415 if (boundary_p
&& node
->symbol
.analyzed
&& !DECL_EXTERNAL (node
->symbol
.decl
))
417 /* Inline clones can not be part of boundary.
418 gcc_assert (!node->global.inlined_to);
420 FIXME: At the moment they can be, when partition contains an inline
421 clone that is clone of inline clone from outside partition. We can
422 reshape the clone tree and make other tree to be the root, but it
423 needs a bit extra work and will be promplty done by cgraph_remove_node
424 after reading back. */
425 in_other_partition
= 1;
428 clone_of
= node
->clone_of
;
430 && (ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)clone_of
)) == LCC_NOT_FOUND
)
431 if (clone_of
->prev_sibling_clone
)
432 clone_of
= clone_of
->prev_sibling_clone
;
434 clone_of
= clone_of
->clone_of
;
436 /* See if body of the master function is output. If not, we are seeing only
437 an declaration and we do not need to pass down clone tree. */
438 ultimate_clone_of
= clone_of
;
439 while (ultimate_clone_of
&& ultimate_clone_of
->clone_of
)
440 ultimate_clone_of
= ultimate_clone_of
->clone_of
;
442 if (clone_of
&& !lto_symtab_encoder_encode_body_p (encoder
, ultimate_clone_of
))
445 if (tag
== LTO_symtab_analyzed_node
)
446 gcc_assert (clone_of
|| !node
->clone_of
);
448 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
450 streamer_write_hwi_stream (ob
->main_stream
, ref
);
453 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->symbol
.decl
);
454 streamer_write_gcov_count_stream (ob
->main_stream
, node
->count
);
455 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
457 streamer_write_hwi_stream (ob
->main_stream
,
458 node
->ipa_transforms_to_apply
.length ());
459 FOR_EACH_VEC_ELT (node
->ipa_transforms_to_apply
, i
, pass
)
460 streamer_write_hwi_stream (ob
->main_stream
, pass
->static_pass_number
);
462 if (tag
== LTO_symtab_analyzed_node
)
464 if (node
->global
.inlined_to
)
466 ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
->global
.inlined_to
);
467 gcc_assert (ref
!= LCC_NOT_FOUND
);
472 streamer_write_hwi_stream (ob
->main_stream
, ref
);
475 if (node
->symbol
.same_comdat_group
&& !boundary_p
)
477 ref
= lto_symtab_encoder_lookup (encoder
,
478 node
->symbol
.same_comdat_group
);
479 gcc_assert (ref
!= LCC_NOT_FOUND
);
483 streamer_write_hwi_stream (ob
->main_stream
, ref
);
485 bp
= bitpack_create (ob
->main_stream
);
486 bp_pack_value (&bp
, node
->local
.local
, 1);
487 bp_pack_value (&bp
, node
->symbol
.externally_visible
, 1);
488 bp_pack_value (&bp
, node
->symbol
.definition
, 1);
489 bp_pack_value (&bp
, node
->local
.versionable
, 1);
490 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
491 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
492 bp_pack_value (&bp
, node
->symbol
.force_output
, 1);
493 bp_pack_value (&bp
, node
->symbol
.forced_by_abi
, 1);
494 bp_pack_value (&bp
, node
->symbol
.unique_name
, 1);
495 bp_pack_value (&bp
, node
->symbol
.address_taken
, 1);
496 bp_pack_value (&bp
, tag
== LTO_symtab_analyzed_node
497 && !DECL_EXTERNAL (node
->symbol
.decl
)
498 && !DECL_COMDAT (node
->symbol
.decl
)
499 && (reachable_from_other_partition_p (node
, encoder
)
500 || referenced_from_other_partition_p (&node
->symbol
.ref_list
,
502 bp_pack_value (&bp
, node
->lowered
, 1);
503 bp_pack_value (&bp
, in_other_partition
, 1);
504 /* Real aliases in a boundary become non-aliases. However we still stream
505 alias info on weakrefs.
506 TODO: We lose a bit of information here - when we know that variable is
507 defined in other unit, we may use the info on aliases to resolve
508 symbol1 != symbol2 type tests that we can do only for locally defined objects
510 alias_p
= node
->symbol
.alias
&& (!boundary_p
|| node
->symbol
.weakref
);
511 bp_pack_value (&bp
, alias_p
, 1);
512 bp_pack_value (&bp
, node
->symbol
.weakref
, 1);
513 bp_pack_value (&bp
, node
->frequency
, 2);
514 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
515 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
516 bp_pack_value (&bp
, node
->tm_clone
, 1);
517 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
518 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
519 LDPR_NUM_KNOWN
, node
->symbol
.resolution
);
520 streamer_write_bitpack (&bp
);
522 if (node
->thunk
.thunk_p
&& !boundary_p
)
524 streamer_write_uhwi_stream
526 1 + (node
->thunk
.this_adjusting
!= 0) * 2
527 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
528 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
529 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
531 streamer_write_hwi_stream (ob
->main_stream
, node
->profile_id
);
534 /* Output the varpool NODE to OB.
535 If NODE is not in SET, then NODE is a boundary. */
538 lto_output_varpool_node (struct lto_simple_output_block
*ob
, struct varpool_node
*node
,
539 lto_symtab_encoder_t encoder
)
541 bool boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)node
);
546 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
547 LTO_symtab_variable
);
548 streamer_write_hwi_stream (ob
->main_stream
, node
->symbol
.order
);
549 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->symbol
.decl
);
550 bp
= bitpack_create (ob
->main_stream
);
551 bp_pack_value (&bp
, node
->symbol
.externally_visible
, 1);
552 bp_pack_value (&bp
, node
->symbol
.force_output
, 1);
553 bp_pack_value (&bp
, node
->symbol
.forced_by_abi
, 1);
554 bp_pack_value (&bp
, node
->symbol
.unique_name
, 1);
555 bp_pack_value (&bp
, node
->symbol
.definition
, 1);
556 alias_p
= node
->symbol
.alias
&& (!boundary_p
|| node
->symbol
.weakref
);
557 bp_pack_value (&bp
, alias_p
, 1);
558 bp_pack_value (&bp
, node
->symbol
.weakref
, 1);
559 bp_pack_value (&bp
, node
->symbol
.analyzed
&& !boundary_p
, 1);
560 gcc_assert (node
->symbol
.definition
|| !node
->symbol
.analyzed
);
561 /* Constant pool initializers can be de-unified into individual ltrans units.
562 FIXME: Alternatively at -Os we may want to avoid generating for them the local
563 labels and share them across LTRANS partitions. */
564 if (DECL_IN_CONSTANT_POOL (node
->symbol
.decl
)
565 && !DECL_EXTERNAL (node
->symbol
.decl
)
566 && !DECL_COMDAT (node
->symbol
.decl
))
568 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
569 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
573 bp_pack_value (&bp
, node
->symbol
.definition
574 && referenced_from_other_partition_p (&node
->symbol
.ref_list
,
576 bp_pack_value (&bp
, node
->symbol
.analyzed
577 && boundary_p
&& !DECL_EXTERNAL (node
->symbol
.decl
), 1);
578 /* in_other_partition. */
580 streamer_write_bitpack (&bp
);
581 if (node
->symbol
.same_comdat_group
&& !boundary_p
)
583 ref
= lto_symtab_encoder_lookup (encoder
,
584 node
->symbol
.same_comdat_group
);
585 gcc_assert (ref
!= LCC_NOT_FOUND
);
589 streamer_write_hwi_stream (ob
->main_stream
, ref
);
590 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
591 LDPR_NUM_KNOWN
, node
->symbol
.resolution
);
594 /* Output the varpool NODE to OB.
595 If NODE is not in SET, then NODE is a boundary. */
598 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
599 lto_symtab_encoder_t encoder
)
603 int uid
= ref
->lto_stmt_uid
;
604 struct cgraph_node
*node
;
606 bp
= bitpack_create (ob
->main_stream
);
607 bp_pack_value (&bp
, ref
->use
, 2);
608 bp_pack_value (&bp
, ref
->speculative
, 1);
609 streamer_write_bitpack (&bp
);
610 nref
= lto_symtab_encoder_lookup (encoder
, ref
->referred
);
611 gcc_assert (nref
!= LCC_NOT_FOUND
);
612 streamer_write_hwi_stream (ob
->main_stream
, nref
);
614 node
= dyn_cast
<cgraph_node
> (ref
->referring
);
618 uid
= gimple_uid (ref
->stmt
) + 1;
619 streamer_write_hwi_stream (ob
->main_stream
, uid
);
623 /* Stream out profile_summary to OB. */
626 output_profile_summary (struct lto_simple_output_block
*ob
)
633 /* We do not output num and run_max, they are not used by
634 GCC profile feedback and they are difficult to merge from multiple
636 gcc_assert (profile_info
->runs
);
637 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
638 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_max
);
640 /* sum_all is needed for computing the working set with the
642 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_all
);
644 /* Create and output a bitpack of non-zero histogram entries indices. */
645 bp
= bitpack_create (ob
->main_stream
);
646 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
647 bp_pack_value (&bp
, profile_info
->histogram
[h_ix
].num_counters
> 0, 1);
648 streamer_write_bitpack (&bp
);
649 /* Now stream out only those non-zero entries. */
650 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
652 if (!profile_info
->histogram
[h_ix
].num_counters
)
654 streamer_write_gcov_count_stream (ob
->main_stream
,
655 profile_info
->histogram
[h_ix
].num_counters
);
656 streamer_write_gcov_count_stream (ob
->main_stream
,
657 profile_info
->histogram
[h_ix
].min_value
);
658 streamer_write_gcov_count_stream (ob
->main_stream
,
659 profile_info
->histogram
[h_ix
].cum_value
);
661 /* IPA-profile computes hot bb threshold based on cumulated
662 whole program profile. We need to stream it down to ltrans. */
664 streamer_write_gcov_count_stream (ob
->main_stream
,
665 get_hot_bb_threshold ());
668 streamer_write_uhwi_stream (ob
->main_stream
, 0);
671 /* Output all callees or indirect outgoing edges. EDGE must be the first such
675 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
676 struct lto_simple_output_block
*ob
,
677 lto_symtab_encoder_t encoder
)
682 /* Output edges in backward direction, so the reconstructed callgraph match
683 and it is easy to associate call sites in the IPA pass summaries. */
684 while (edge
->next_callee
)
685 edge
= edge
->next_callee
;
686 for (; edge
; edge
= edge
->prev_callee
)
687 lto_output_edge (ob
, edge
, encoder
);
690 /* Output the part of the cgraph in SET. */
693 output_refs (lto_symtab_encoder_t encoder
)
695 lto_symtab_encoder_iterator lsei
;
696 struct lto_simple_output_block
*ob
;
701 ob
= lto_create_simple_output_block (LTO_section_refs
);
703 for (lsei
= lsei_start_in_partition (encoder
); !lsei_end_p (lsei
);
704 lsei_next_in_partition (&lsei
))
706 symtab_node node
= lsei_node (lsei
);
708 count
= ipa_ref_list_nreferences (&node
->symbol
.ref_list
);
711 streamer_write_gcov_count_stream (ob
->main_stream
, count
);
712 streamer_write_uhwi_stream (ob
->main_stream
,
713 lto_symtab_encoder_lookup (encoder
, node
));
714 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
,
716 lto_output_ref (ob
, ref
, encoder
);
720 streamer_write_uhwi_stream (ob
->main_stream
, 0);
722 lto_destroy_simple_output_block (ob
);
725 /* Add NODE into encoder as well as nodes it is cloned from.
726 Do it in a way so clones appear first. */
729 add_node_to (lto_symtab_encoder_t encoder
, struct cgraph_node
*node
,
733 add_node_to (encoder
, node
->clone_of
, include_body
);
734 else if (include_body
)
735 lto_set_symtab_encoder_encode_body (encoder
, node
);
736 lto_symtab_encoder_encode (encoder
, (symtab_node
)node
);
739 /* Add all references in LIST to encoders. */
742 add_references (lto_symtab_encoder_t encoder
,
743 struct ipa_ref_list
*list
)
747 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
748 if (is_a
<cgraph_node
> (ref
->referred
))
749 add_node_to (encoder
, ipa_ref_node (ref
), false);
751 lto_symtab_encoder_encode (encoder
, ref
->referred
);
754 /* Find all symbols we want to stream into given partition and insert them
757 The function actually replaces IN_ENCODER by new one. The reason is that
758 streaming code needs clone's origin to be streamed before clone. This
759 means that we need to insert the nodes in specific order. This order is
760 ignored by the partitioning logic earlier. */
763 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder
)
765 struct cgraph_node
*node
;
766 struct cgraph_edge
*edge
;
768 lto_symtab_encoder_t encoder
;
769 lto_symtab_encoder_iterator lsei
;
770 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
772 encoder
= lto_symtab_encoder_new (false);
774 /* Go over all entries in the IN_ENCODER and duplicate them to
775 ENCODER. At the same time insert masters of clones so
776 every master appears before clone. */
777 for (lsei
= lsei_start_function_in_partition (in_encoder
);
778 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
780 node
= lsei_cgraph_node (lsei
);
781 add_node_to (encoder
, node
, true);
782 lto_set_symtab_encoder_in_partition (encoder
, (symtab_node
)node
);
783 add_references (encoder
, &node
->symbol
.ref_list
);
784 /* For proper debug info, we need to ship the origins, too. */
785 if (DECL_ABSTRACT_ORIGIN (node
->symbol
.decl
))
787 struct cgraph_node
*origin_node
788 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node
->symbol
.decl
));
789 add_node_to (encoder
, origin_node
, true);
792 for (lsei
= lsei_start_variable_in_partition (in_encoder
);
793 !lsei_end_p (lsei
); lsei_next_variable_in_partition (&lsei
))
795 struct varpool_node
*vnode
= lsei_varpool_node (lsei
);
797 lto_set_symtab_encoder_in_partition (encoder
, (symtab_node
)vnode
);
798 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
799 add_references (encoder
, &vnode
->symbol
.ref_list
);
800 /* For proper debug info, we need to ship the origins, too. */
801 if (DECL_ABSTRACT_ORIGIN (vnode
->symbol
.decl
))
803 struct varpool_node
*origin_node
804 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node
->symbol
.decl
));
805 lto_set_symtab_encoder_in_partition (encoder
, (symtab_node
)origin_node
);
808 /* Pickle in also the initializer of all referenced readonly variables
809 to help folding. Constant pool variables are not shared, so we must
811 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
813 symtab_node node
= lto_symtab_encoder_deref (encoder
, i
);
814 if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
816 if (!lto_symtab_encoder_encode_initializer_p (encoder
,
818 && ctor_for_folding (vnode
->symbol
.decl
) != error_mark_node
)
820 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
821 add_references (encoder
, &vnode
->symbol
.ref_list
);
826 /* Go over all the nodes again to include callees that are not in
828 for (lsei
= lsei_start_function_in_partition (encoder
);
829 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
831 node
= lsei_cgraph_node (lsei
);
832 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
834 struct cgraph_node
*callee
= edge
->callee
;
835 if (!lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)callee
))
837 /* We should have moved all the inlines. */
838 gcc_assert (!callee
->global
.inlined_to
);
839 add_node_to (encoder
, callee
, false);
842 /* Add all possible targets for late devirtualization. */
843 if (flag_devirtualize
)
844 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
845 if (edge
->indirect_info
->polymorphic
)
850 vec
<cgraph_node
*>targets
851 = possible_polymorphic_call_targets
852 (edge
, &final
, &cache_token
);
853 if (!pointer_set_insert (reachable_call_targets
,
856 for (i
= 0; i
< targets
.length (); i
++)
858 struct cgraph_node
*callee
= targets
[i
];
860 /* Adding an external declarations into the unit serves
861 no purpose and just increases its boundary. */
862 if (callee
->symbol
.definition
863 && !lto_symtab_encoder_in_partition_p
864 (encoder
, (symtab_node
)callee
))
866 gcc_assert (!callee
->global
.inlined_to
);
867 add_node_to (encoder
, callee
, false);
873 lto_symtab_encoder_delete (in_encoder
);
874 pointer_set_destroy (reachable_call_targets
);
878 /* Output the part of the symtab in SET and VSET. */
883 struct cgraph_node
*node
;
884 struct lto_simple_output_block
*ob
;
885 lto_symtab_encoder_iterator lsei
;
887 lto_symtab_encoder_t encoder
;
888 static bool asm_nodes_output
= false;
891 output_cgraph_opt_summary ();
893 ob
= lto_create_simple_output_block (LTO_section_symtab_nodes
);
895 output_profile_summary (ob
);
897 /* An encoder for cgraph nodes should have been created by
898 ipa_write_summaries_1. */
899 gcc_assert (ob
->decl_state
->symtab_node_encoder
);
900 encoder
= ob
->decl_state
->symtab_node_encoder
;
902 /* Write out the nodes. We must first output a node and then its clones,
903 otherwise at a time reading back the node there would be nothing to clone
905 n_nodes
= lto_symtab_encoder_size (encoder
);
906 for (i
= 0; i
< n_nodes
; i
++)
908 symtab_node node
= lto_symtab_encoder_deref (encoder
, i
);
909 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
910 lto_output_node (ob
, cnode
, encoder
);
912 lto_output_varpool_node (ob
, varpool (node
), encoder
);
916 /* Go over the nodes in SET again to write edges. */
917 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
918 lsei_next_function_in_partition (&lsei
))
920 node
= lsei_cgraph_node (lsei
);
921 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
922 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
925 streamer_write_uhwi_stream (ob
->main_stream
, 0);
927 lto_destroy_simple_output_block (ob
);
929 /* Emit toplevel asms.
930 When doing WPA we must output every asm just once. Since we do not partition asm
931 nodes at all, output them to first output. This is kind of hack, but should work
933 if (!asm_nodes_output
)
935 asm_nodes_output
= true;
936 lto_output_toplevel_asms ();
939 output_refs (encoder
);
942 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
943 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
944 NODE or to replace the values in it, for instance because the first
945 time we saw it, the function body was not available but now it
946 is. BP is a bitpack with all the bitflags for NODE read from the
950 input_overwrite_node (struct lto_file_decl_data
*file_data
,
951 struct cgraph_node
*node
,
952 enum LTO_symtab_tags tag
,
953 struct bitpack_d
*bp
)
955 node
->symbol
.aux
= (void *) tag
;
956 node
->symbol
.lto_file_data
= file_data
;
958 node
->local
.local
= bp_unpack_value (bp
, 1);
959 node
->symbol
.externally_visible
= bp_unpack_value (bp
, 1);
960 node
->symbol
.definition
= bp_unpack_value (bp
, 1);
961 node
->local
.versionable
= bp_unpack_value (bp
, 1);
962 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
963 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
964 node
->symbol
.force_output
= bp_unpack_value (bp
, 1);
965 node
->symbol
.forced_by_abi
= bp_unpack_value (bp
, 1);
966 node
->symbol
.unique_name
= bp_unpack_value (bp
, 1);
967 node
->symbol
.address_taken
= bp_unpack_value (bp
, 1);
968 node
->symbol
.used_from_other_partition
= bp_unpack_value (bp
, 1);
969 node
->lowered
= bp_unpack_value (bp
, 1);
970 node
->symbol
.analyzed
= tag
== LTO_symtab_analyzed_node
;
971 node
->symbol
.in_other_partition
= bp_unpack_value (bp
, 1);
972 if (node
->symbol
.in_other_partition
973 /* Avoid updating decl when we are seeing just inline clone.
974 When inlining function that has functions already inlined into it,
975 we produce clones of inline clones.
977 WPA partitioning might put each clone into different unit and
978 we might end up streaming inline clone from other partition
979 to support clone we are interested in. */
981 || node
->clone_of
->symbol
.decl
!= node
->symbol
.decl
))
983 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
984 TREE_STATIC (node
->symbol
.decl
) = 0;
986 node
->symbol
.alias
= bp_unpack_value (bp
, 1);
987 node
->symbol
.weakref
= bp_unpack_value (bp
, 1);
988 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
989 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
990 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
991 node
->tm_clone
= bp_unpack_value (bp
, 1);
992 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
993 node
->symbol
.resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
997 /* Return string alias is alias of. */
1000 get_alias_symbol (tree decl
)
1002 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1003 return get_identifier (TREE_STRING_POINTER
1004 (TREE_VALUE (TREE_VALUE (alias
))));
1007 /* Read a node from input_block IB. TAG is the node's tag just read.
1008 Return the node read or overwriten. */
1010 static struct cgraph_node
*
1011 input_node (struct lto_file_decl_data
*file_data
,
1012 struct lto_input_block
*ib
,
1013 enum LTO_symtab_tags tag
,
1014 vec
<symtab_node
> nodes
)
1016 gcc::pass_manager
*passes
= g
->get_passes ();
1018 struct cgraph_node
*node
;
1019 struct bitpack_d bp
;
1020 unsigned decl_index
;
1021 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
1026 order
= streamer_read_hwi (ib
) + order_base
;
1027 clone_ref
= streamer_read_hwi (ib
);
1029 decl_index
= streamer_read_uhwi (ib
);
1030 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
1032 if (clone_ref
!= LCC_NOT_FOUND
)
1034 node
= cgraph_clone_node (cgraph (nodes
[clone_ref
]), fn_decl
,
1035 0, CGRAPH_FREQ_BASE
, false,
1036 vNULL
, false, NULL
);
1040 /* Declaration of functions can be already merged with a declaration
1041 from other input file. We keep cgraph unmerged until after streaming
1042 of ipa passes is done. Alays forcingly create a fresh node. */
1043 node
= cgraph_create_empty_node ();
1044 node
->symbol
.decl
= fn_decl
;
1045 symtab_register_node ((symtab_node
)node
);
1048 node
->symbol
.order
= order
;
1049 if (order
>= symtab_order
)
1050 symtab_order
= order
+ 1;
1052 node
->count
= streamer_read_gcov_count (ib
);
1053 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1055 count
= streamer_read_hwi (ib
);
1056 node
->ipa_transforms_to_apply
= vNULL
;
1057 for (i
= 0; i
< count
; i
++)
1059 struct opt_pass
*pass
;
1060 int pid
= streamer_read_hwi (ib
);
1062 gcc_assert (pid
< passes
->passes_by_id_size
);
1063 pass
= passes
->passes_by_id
[pid
];
1064 node
->ipa_transforms_to_apply
.safe_push ((struct ipa_opt_pass_d
*) pass
);
1067 if (tag
== LTO_symtab_analyzed_node
)
1068 ref
= streamer_read_hwi (ib
);
1070 ref2
= streamer_read_hwi (ib
);
1072 /* Make sure that we have not read this node before. Nodes that
1073 have already been read will have their tag stored in the 'aux'
1074 field. Since built-in functions can be referenced in multiple
1075 functions, they are expected to be read more than once. */
1076 if (node
->symbol
.aux
&& !DECL_BUILT_IN (node
->symbol
.decl
))
1077 internal_error ("bytecode stream: found multiple instances of cgraph "
1078 "node with uid %d", node
->uid
);
1080 bp
= streamer_read_bitpack (ib
);
1081 input_overwrite_node (file_data
, node
, tag
, &bp
);
1083 /* Store a reference for now, and fix up later to be a pointer. */
1084 node
->global
.inlined_to
= (cgraph_node_ptr
) (intptr_t) ref
;
1086 /* Store a reference for now, and fix up later to be a pointer. */
1087 node
->symbol
.same_comdat_group
= (symtab_node
) (intptr_t) ref2
;
1089 if (node
->thunk
.thunk_p
)
1091 int type
= streamer_read_uhwi (ib
);
1092 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1093 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1095 node
->thunk
.fixed_offset
= fixed_offset
;
1096 node
->thunk
.this_adjusting
= (type
& 2);
1097 node
->thunk
.virtual_value
= virtual_value
;
1098 node
->thunk
.virtual_offset_p
= (type
& 4);
1100 if (node
->symbol
.alias
&& !node
->symbol
.analyzed
&& node
->symbol
.weakref
)
1101 node
->symbol
.alias_target
= get_alias_symbol (node
->symbol
.decl
);
1102 node
->profile_id
= streamer_read_hwi (ib
);
1106 /* Read a node from input_block IB. TAG is the node's tag just read.
1107 Return the node read or overwriten. */
1109 static struct varpool_node
*
1110 input_varpool_node (struct lto_file_decl_data
*file_data
,
1111 struct lto_input_block
*ib
)
1115 struct varpool_node
*node
;
1116 struct bitpack_d bp
;
1117 int ref
= LCC_NOT_FOUND
;
1120 order
= streamer_read_hwi (ib
) + order_base
;
1121 decl_index
= streamer_read_uhwi (ib
);
1122 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1124 /* Declaration of functions can be already merged with a declaration
1125 from other input file. We keep cgraph unmerged until after streaming
1126 of ipa passes is done. Alays forcingly create a fresh node. */
1127 node
= varpool_create_empty_node ();
1128 node
->symbol
.decl
= var_decl
;
1129 symtab_register_node ((symtab_node
)node
);
1131 node
->symbol
.order
= order
;
1132 if (order
>= symtab_order
)
1133 symtab_order
= order
+ 1;
1134 node
->symbol
.lto_file_data
= file_data
;
1136 bp
= streamer_read_bitpack (ib
);
1137 node
->symbol
.externally_visible
= bp_unpack_value (&bp
, 1);
1138 node
->symbol
.force_output
= bp_unpack_value (&bp
, 1);
1139 node
->symbol
.forced_by_abi
= bp_unpack_value (&bp
, 1);
1140 node
->symbol
.unique_name
= bp_unpack_value (&bp
, 1);
1141 node
->symbol
.definition
= bp_unpack_value (&bp
, 1);
1142 node
->symbol
.alias
= bp_unpack_value (&bp
, 1);
1143 node
->symbol
.weakref
= bp_unpack_value (&bp
, 1);
1144 node
->symbol
.analyzed
= bp_unpack_value (&bp
, 1);
1145 node
->symbol
.used_from_other_partition
= bp_unpack_value (&bp
, 1);
1146 node
->symbol
.in_other_partition
= bp_unpack_value (&bp
, 1);
1147 if (node
->symbol
.in_other_partition
)
1149 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
1150 TREE_STATIC (node
->symbol
.decl
) = 0;
1152 if (node
->symbol
.alias
&& !node
->symbol
.analyzed
&& node
->symbol
.weakref
)
1153 node
->symbol
.alias_target
= get_alias_symbol (node
->symbol
.decl
);
1154 ref
= streamer_read_hwi (ib
);
1155 /* Store a reference for now, and fix up later to be a pointer. */
1156 node
->symbol
.same_comdat_group
= (symtab_node
) (intptr_t) ref
;
1157 node
->symbol
.resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1163 /* Read a node from input_block IB. TAG is the node's tag just read.
1164 Return the node read or overwriten. */
1167 input_ref (struct lto_input_block
*ib
,
1168 symtab_node referring_node
,
1169 vec
<symtab_node
> nodes
)
1171 symtab_node node
= NULL
;
1172 struct bitpack_d bp
;
1173 enum ipa_ref_use use
;
1175 struct ipa_ref
*ref
;
1177 bp
= streamer_read_bitpack (ib
);
1178 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1179 speculative
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 1);
1180 node
= nodes
[streamer_read_hwi (ib
)];
1181 ref
= ipa_record_reference (referring_node
, node
, use
, NULL
);
1182 ref
->speculative
= speculative
;
1183 if (is_a
<cgraph_node
> (referring_node
))
1184 ref
->lto_stmt_uid
= streamer_read_hwi (ib
);
1187 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1188 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1189 edge being read is indirect (in the sense that it has
1190 indirect_unknown_callee set). */
1193 input_edge (struct lto_input_block
*ib
, vec
<symtab_node
> nodes
,
1196 struct cgraph_node
*caller
, *callee
;
1197 struct cgraph_edge
*edge
;
1198 unsigned int stmt_id
;
1201 cgraph_inline_failed_t inline_failed
;
1202 struct bitpack_d bp
;
1205 caller
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1206 if (caller
== NULL
|| caller
->symbol
.decl
== NULL_TREE
)
1207 internal_error ("bytecode stream: no caller found while reading edge");
1211 callee
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1212 if (callee
== NULL
|| callee
->symbol
.decl
== NULL_TREE
)
1213 internal_error ("bytecode stream: no callee found while reading edge");
1218 count
= streamer_read_gcov_count (ib
);
1220 bp
= streamer_read_bitpack (ib
);
1221 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_enum
, CIF_N_REASONS
);
1222 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1223 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1226 edge
= cgraph_create_indirect_edge (caller
, NULL
, 0, count
, freq
);
1228 edge
= cgraph_create_edge (caller
, callee
, NULL
, count
, freq
);
1230 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1231 edge
->speculative
= bp_unpack_value (&bp
, 1);
1232 edge
->lto_stmt_uid
= stmt_id
;
1233 edge
->inline_failed
= inline_failed
;
1234 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1235 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1238 if (bp_unpack_value (&bp
, 1))
1239 ecf_flags
|= ECF_CONST
;
1240 if (bp_unpack_value (&bp
, 1))
1241 ecf_flags
|= ECF_PURE
;
1242 if (bp_unpack_value (&bp
, 1))
1243 ecf_flags
|= ECF_NORETURN
;
1244 if (bp_unpack_value (&bp
, 1))
1245 ecf_flags
|= ECF_MALLOC
;
1246 if (bp_unpack_value (&bp
, 1))
1247 ecf_flags
|= ECF_NOTHROW
;
1248 if (bp_unpack_value (&bp
, 1))
1249 ecf_flags
|= ECF_RETURNS_TWICE
;
1250 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1251 edge
->indirect_info
->common_target_id
= streamer_read_hwi (ib
);
1252 if (edge
->indirect_info
->common_target_id
)
1253 edge
->indirect_info
->common_target_probability
= streamer_read_hwi (ib
);
1258 /* Read a cgraph from IB using the info in FILE_DATA. */
1260 static vec
<symtab_node
>
1261 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1262 struct lto_input_block
*ib
)
1264 enum LTO_symtab_tags tag
;
1265 vec
<symtab_node
> nodes
= vNULL
;
1269 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1270 order_base
= symtab_order
;
1273 if (tag
== LTO_symtab_edge
)
1274 input_edge (ib
, nodes
, false);
1275 else if (tag
== LTO_symtab_indirect_edge
)
1276 input_edge (ib
, nodes
, true);
1277 else if (tag
== LTO_symtab_variable
)
1279 node
= (symtab_node
)input_varpool_node (file_data
, ib
);
1280 nodes
.safe_push (node
);
1281 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1285 node
= (symtab_node
)input_node (file_data
, ib
, tag
, nodes
);
1286 if (node
== NULL
|| node
->symbol
.decl
== NULL_TREE
)
1287 internal_error ("bytecode stream: found empty cgraph node");
1288 nodes
.safe_push (node
);
1289 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1292 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1295 lto_input_toplevel_asms (file_data
, order_base
);
1297 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1298 #ifdef ENABLE_CHECKING
1299 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1300 gcc_assert (node
->symbol
.aux
|| !is_a
<cgraph_node
> (node
));
1302 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1305 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
1307 ref
= (int) (intptr_t) cnode
->global
.inlined_to
;
1309 /* We share declaration of builtins, so we may read same node twice. */
1310 if (!node
->symbol
.aux
)
1312 node
->symbol
.aux
= NULL
;
1314 /* Fixup inlined_to from reference to pointer. */
1315 if (ref
!= LCC_NOT_FOUND
)
1316 cgraph (node
)->global
.inlined_to
= cgraph (nodes
[ref
]);
1318 cnode
->global
.inlined_to
= NULL
;
1321 ref
= (int) (intptr_t) node
->symbol
.same_comdat_group
;
1323 /* Fixup same_comdat_group from reference to pointer. */
1324 if (ref
!= LCC_NOT_FOUND
)
1325 node
->symbol
.same_comdat_group
= nodes
[ref
];
1327 node
->symbol
.same_comdat_group
= NULL
;
1329 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1330 node
->symbol
.aux
= is_a
<cgraph_node
> (node
) ? (void *)1 : NULL
;
1334 /* Input ipa_refs. */
1337 input_refs (struct lto_input_block
*ib
,
1338 vec
<symtab_node
> nodes
)
1345 count
= streamer_read_uhwi (ib
);
1348 idx
= streamer_read_uhwi (ib
);
1352 input_ref (ib
, node
, nodes
);
1359 static struct gcov_ctr_summary lto_gcov_summary
;
1361 /* Input profile_info from IB. */
1363 input_profile_summary (struct lto_input_block
*ib
,
1364 struct lto_file_decl_data
*file_data
)
1367 struct bitpack_d bp
;
1368 unsigned int runs
= streamer_read_uhwi (ib
);
1371 file_data
->profile_info
.runs
= runs
;
1372 file_data
->profile_info
.sum_max
= streamer_read_gcov_count (ib
);
1373 file_data
->profile_info
.sum_all
= streamer_read_gcov_count (ib
);
1375 memset (file_data
->profile_info
.histogram
, 0,
1376 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1377 /* Input the bitpack of non-zero histogram indices. */
1378 bp
= streamer_read_bitpack (ib
);
1379 /* Read in and unpack the full bitpack, flagging non-zero
1380 histogram entries by setting the num_counters non-zero. */
1381 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1383 file_data
->profile_info
.histogram
[h_ix
].num_counters
1384 = bp_unpack_value (&bp
, 1);
1386 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1388 if (!file_data
->profile_info
.histogram
[h_ix
].num_counters
)
1391 file_data
->profile_info
.histogram
[h_ix
].num_counters
1392 = streamer_read_gcov_count (ib
);
1393 file_data
->profile_info
.histogram
[h_ix
].min_value
1394 = streamer_read_gcov_count (ib
);
1395 file_data
->profile_info
.histogram
[h_ix
].cum_value
1396 = streamer_read_gcov_count (ib
);
1398 /* IPA-profile computes hot bb threshold based on cumulated
1399 whole program profile. We need to stream it down to ltrans. */
1401 set_hot_bb_threshold (streamer_read_gcov_count (ib
));
1406 /* Rescale profile summaries to the same number of runs in the whole unit. */
1409 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1411 struct lto_file_decl_data
*file_data
;
1412 unsigned int j
, h_ix
;
1413 gcov_unsigned_t max_runs
= 0;
1414 struct cgraph_node
*node
;
1415 struct cgraph_edge
*edge
;
1416 gcov_type saved_sum_all
= 0;
1417 gcov_ctr_summary
*saved_profile_info
= 0;
1418 int saved_scale
= 0;
1420 /* Find unit with maximal number of runs. If we ever get serious about
1421 roundoff errors, we might also consider computing smallest common
1423 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1424 if (max_runs
< file_data
->profile_info
.runs
)
1425 max_runs
= file_data
->profile_info
.runs
;
1430 /* Simple overflow check. We probably don't need to support that many train
1431 runs. Such a large value probably imply data corruption anyway. */
1432 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1434 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1435 INT_MAX
/ REG_BR_PROB_BASE
);
1439 profile_info
= <o_gcov_summary
;
1440 lto_gcov_summary
.runs
= max_runs
;
1441 lto_gcov_summary
.sum_max
= 0;
1442 memset (lto_gcov_summary
.histogram
, 0,
1443 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1445 /* Rescale all units to the maximal number of runs.
1446 sum_max can not be easily merged, as we have no idea what files come from
1447 the same run. We do not use the info anyway, so leave it 0. */
1448 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1449 if (file_data
->profile_info
.runs
)
1451 int scale
= GCOV_COMPUTE_SCALE (max_runs
,
1452 file_data
->profile_info
.runs
);
1453 lto_gcov_summary
.sum_max
1454 = MAX (lto_gcov_summary
.sum_max
,
1455 apply_scale (file_data
->profile_info
.sum_max
, scale
));
1456 lto_gcov_summary
.sum_all
1457 = MAX (lto_gcov_summary
.sum_all
,
1458 apply_scale (file_data
->profile_info
.sum_all
, scale
));
1459 /* Save a pointer to the profile_info with the largest
1460 scaled sum_all and the scale for use in merging the
1462 if (!saved_profile_info
1463 || lto_gcov_summary
.sum_all
> saved_sum_all
)
1465 saved_profile_info
= &file_data
->profile_info
;
1466 saved_sum_all
= lto_gcov_summary
.sum_all
;
1467 saved_scale
= scale
;
1471 gcc_assert (saved_profile_info
);
1473 /* Scale up the histogram from the profile that had the largest
1474 scaled sum_all above. */
1475 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1477 /* Scale up the min value as we did the corresponding sum_all
1478 above. Use that to find the new histogram index. */
1479 gcov_type scaled_min
1480 = apply_scale (saved_profile_info
->histogram
[h_ix
].min_value
,
1482 /* The new index may be shared with another scaled histogram entry,
1483 so we need to account for a non-zero histogram entry at new_ix. */
1484 unsigned new_ix
= gcov_histo_index (scaled_min
);
1485 lto_gcov_summary
.histogram
[new_ix
].min_value
1486 = (lto_gcov_summary
.histogram
[new_ix
].num_counters
1487 ? MIN (lto_gcov_summary
.histogram
[new_ix
].min_value
, scaled_min
)
1489 /* Some of the scaled counter values would ostensibly need to be placed
1490 into different (larger) histogram buckets, but we keep things simple
1491 here and place the scaled cumulative counter value in the bucket
1492 corresponding to the scaled minimum counter value. */
1493 lto_gcov_summary
.histogram
[new_ix
].cum_value
1494 += apply_scale (saved_profile_info
->histogram
[h_ix
].cum_value
,
1496 lto_gcov_summary
.histogram
[new_ix
].num_counters
1497 += saved_profile_info
->histogram
[h_ix
].num_counters
;
1500 /* Watch roundoff errors. */
1501 if (lto_gcov_summary
.sum_max
< max_runs
)
1502 lto_gcov_summary
.sum_max
= max_runs
;
1504 /* If merging already happent at WPA time, we are done. */
1508 /* Now compute count_materialization_scale of each node.
1509 During LTRANS we already have values of count_materialization_scale
1510 computed, so just update them. */
1511 FOR_EACH_FUNCTION (node
)
1512 if (node
->symbol
.lto_file_data
1513 && node
->symbol
.lto_file_data
->profile_info
.runs
)
1517 scale
= RDIV (node
->count_materialization_scale
* max_runs
,
1518 node
->symbol
.lto_file_data
->profile_info
.runs
);
1519 node
->count_materialization_scale
= scale
;
1521 fatal_error ("Profile information in %s corrupted",
1522 file_data
->file_name
);
1524 if (scale
== REG_BR_PROB_BASE
)
1526 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1527 edge
->count
= apply_scale (edge
->count
, scale
);
1528 node
->count
= apply_scale (node
->count
, scale
);
1532 /* Input and merge the symtab from each of the .o files passed to
1538 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1539 struct lto_file_decl_data
*file_data
;
1541 struct cgraph_node
*node
;
1543 while ((file_data
= file_data_vec
[j
++]))
1547 struct lto_input_block
*ib
;
1548 vec
<symtab_node
> nodes
;
1550 ib
= lto_create_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1553 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1554 input_profile_summary (ib
, file_data
);
1555 file_data
->symtab_node_encoder
= lto_symtab_encoder_new (true);
1556 nodes
= input_cgraph_1 (file_data
, ib
);
1557 lto_destroy_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1560 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1563 fatal_error ("cannot find LTO section refs in %s",
1564 file_data
->file_name
);
1565 input_refs (ib
, nodes
);
1566 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1569 input_cgraph_opt_summary (nodes
);
1573 merge_profile_summaries (file_data_vec
);
1574 get_working_sets ();
1577 /* Clear out the aux field that was used to store enough state to
1578 tell which nodes should be overwritten. */
1579 FOR_EACH_FUNCTION (node
)
1581 /* Some nodes may have been created by cgraph_node. This
1582 happens when the callgraph contains nested functions. If the
1583 node for the parent function was never emitted to the gimple
1584 file, cgraph_node will create a node for it when setting the
1585 context of the nested function. */
1586 if (node
->symbol
.lto_file_data
)
1587 node
->symbol
.aux
= NULL
;
1591 /* True when we need optimization summary for NODE. */
1594 output_cgraph_opt_summary_p (struct cgraph_node
*node
)
1596 return (node
->clone_of
1597 && (node
->clone
.tree_map
1598 || node
->clone
.args_to_skip
1599 || node
->clone
.combined_args_to_skip
));
1602 /* Output optimization summary for EDGE to OB. */
1604 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1605 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1609 /* Output optimization summary for NODE to OB. */
1612 output_node_opt_summary (struct output_block
*ob
,
1613 struct cgraph_node
*node
,
1614 lto_symtab_encoder_t encoder
)
1618 struct ipa_replace_map
*map
;
1619 struct bitpack_d bp
;
1621 struct cgraph_edge
*e
;
1623 if (node
->clone
.args_to_skip
)
1625 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1626 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1627 streamer_write_uhwi (ob
, index
);
1630 streamer_write_uhwi (ob
, 0);
1631 if (node
->clone
.combined_args_to_skip
)
1633 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1634 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1635 streamer_write_uhwi (ob
, index
);
1638 streamer_write_uhwi (ob
, 0);
1639 streamer_write_uhwi (ob
, vec_safe_length (node
->clone
.tree_map
));
1640 FOR_EACH_VEC_SAFE_ELT (node
->clone
.tree_map
, i
, map
)
1642 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1643 mechanism to store function local declarations into summaries. */
1644 gcc_assert (!map
->old_tree
);
1645 streamer_write_uhwi (ob
, map
->parm_num
);
1646 gcc_assert (EXPR_LOCATION (map
->new_tree
) == UNKNOWN_LOCATION
);
1647 stream_write_tree (ob
, map
->new_tree
, true);
1648 bp
= bitpack_create (ob
->main_stream
);
1649 bp_pack_value (&bp
, map
->replace_p
, 1);
1650 bp_pack_value (&bp
, map
->ref_p
, 1);
1651 streamer_write_bitpack (&bp
);
1654 if (lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
) node
))
1656 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1657 output_edge_opt_summary (ob
, e
);
1658 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1659 output_edge_opt_summary (ob
, e
);
1663 /* Output optimization summaries stored in callgraph.
1664 At the moment it is the clone info structure. */
1667 output_cgraph_opt_summary (void)
1670 lto_symtab_encoder_t encoder
;
1671 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1674 ob
->cgraph_node
= NULL
;
1675 encoder
= ob
->decl_state
->symtab_node_encoder
;
1676 n_nodes
= lto_symtab_encoder_size (encoder
);
1677 for (i
= 0; i
< n_nodes
; i
++)
1679 symtab_node node
= lto_symtab_encoder_deref (encoder
, i
);
1680 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
1681 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1684 streamer_write_uhwi (ob
, count
);
1685 for (i
= 0; i
< n_nodes
; i
++)
1687 symtab_node node
= lto_symtab_encoder_deref (encoder
, i
);
1688 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
1689 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1691 streamer_write_uhwi (ob
, i
);
1692 output_node_opt_summary (ob
, cnode
, encoder
);
1695 produce_asm (ob
, NULL
);
1696 destroy_output_block (ob
);
1699 /* Input optimisation summary of EDGE. */
1702 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1703 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1707 /* Input optimisation summary of NODE. */
1710 input_node_opt_summary (struct cgraph_node
*node
,
1711 struct lto_input_block
*ib_main
,
1712 struct data_in
*data_in
)
1717 struct bitpack_d bp
;
1718 struct cgraph_edge
*e
;
1720 count
= streamer_read_uhwi (ib_main
);
1722 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1723 for (i
= 0; i
< count
; i
++)
1725 bit
= streamer_read_uhwi (ib_main
);
1726 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1728 count
= streamer_read_uhwi (ib_main
);
1730 node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
1731 for (i
= 0; i
< count
; i
++)
1733 bit
= streamer_read_uhwi (ib_main
);
1734 bitmap_set_bit (node
->clone
.combined_args_to_skip
, bit
);
1736 count
= streamer_read_uhwi (ib_main
);
1737 for (i
= 0; i
< count
; i
++)
1739 struct ipa_replace_map
*map
= ggc_alloc_ipa_replace_map ();
1741 vec_safe_push (node
->clone
.tree_map
, map
);
1742 map
->parm_num
= streamer_read_uhwi (ib_main
);
1743 map
->old_tree
= NULL
;
1744 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1745 bp
= streamer_read_bitpack (ib_main
);
1746 map
->replace_p
= bp_unpack_value (&bp
, 1);
1747 map
->ref_p
= bp_unpack_value (&bp
, 1);
1749 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1750 input_edge_opt_summary (e
, ib_main
);
1751 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1752 input_edge_opt_summary (e
, ib_main
);
1755 /* Read section in file FILE_DATA of length LEN with data DATA. */
1758 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1759 const char *data
, size_t len
,
1760 vec
<symtab_node
> nodes
)
1762 const struct lto_function_header
*header
=
1763 (const struct lto_function_header
*) data
;
1764 const int cfg_offset
= sizeof (struct lto_function_header
);
1765 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1766 const int string_offset
= main_offset
+ header
->main_size
;
1767 struct data_in
*data_in
;
1768 struct lto_input_block ib_main
;
1772 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1776 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1777 header
->string_size
, vNULL
);
1778 count
= streamer_read_uhwi (&ib_main
);
1780 for (i
= 0; i
< count
; i
++)
1782 int ref
= streamer_read_uhwi (&ib_main
);
1783 input_node_opt_summary (cgraph (nodes
[ref
]),
1786 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1788 lto_data_in_delete (data_in
);
1791 /* Input optimization summary of cgraph. */
1794 input_cgraph_opt_summary (vec
<symtab_node
> nodes
)
1796 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1797 struct lto_file_decl_data
*file_data
;
1800 while ((file_data
= file_data_vec
[j
++]))
1804 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1808 input_cgraph_opt_section (file_data
, data
, len
, nodes
);