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"
33 #include "langhooks.h"
34 #include "basic-block.h"
38 #include "diagnostic-core.h"
42 #include "pointer-set.h"
43 #include "lto-streamer.h"
44 #include "data-streamer.h"
45 #include "tree-streamer.h"
47 #include "tree-pass.h"
50 #include "pass_manager.h"
51 #include "ipa-utils.h"
53 static void output_cgraph_opt_summary (void);
54 static void input_cgraph_opt_summary (vec
<symtab_node
> nodes
);
56 /* Number of LDPR values known to GCC. */
57 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
59 /* All node orders are ofsetted by ORDER_BASE. */
60 static int order_base
;
62 /* Cgraph streaming is organized as set of record whose type
63 is indicated by a tag. */
66 /* Must leave 0 for the stopper. */
68 /* Cgraph node without body available. */
69 LTO_symtab_unavail_node
= 1,
70 /* Cgraph node with function body. */
71 LTO_symtab_analyzed_node
,
74 LTO_symtab_indirect_edge
,
79 /* Create a new symtab encoder.
80 if FOR_INPUT, the encoder allocate only datastructures needed
81 to read the symtab. */
84 lto_symtab_encoder_new (bool for_input
)
86 lto_symtab_encoder_t encoder
= XCNEW (struct lto_symtab_encoder_d
);
89 encoder
->map
= pointer_map_create ();
90 encoder
->nodes
.create (0);
95 /* Delete ENCODER and its components. */
98 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder
)
100 encoder
->nodes
.release ();
102 pointer_map_destroy (encoder
->map
);
107 /* Return the existing reference number of NODE in the symtab encoder in
108 output block OB. Assign a new reference if this is the first time
112 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder
,
120 lto_encoder_entry entry
= {node
, false, false, false};
122 ref
= encoder
->nodes
.length ();
123 encoder
->nodes
.safe_push (entry
);
127 slot
= pointer_map_contains (encoder
->map
, node
);
130 lto_encoder_entry entry
= {node
, false, false, false};
131 ref
= encoder
->nodes
.length ();
133 slot
= pointer_map_insert (encoder
->map
, node
);
134 *slot
= (void *) (intptr_t) (ref
+ 1);
135 encoder
->nodes
.safe_push (entry
);
138 ref
= (size_t) *slot
- 1;
143 /* Remove NODE from encoder. */
146 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder
,
149 void **slot
, **last_slot
;
151 lto_encoder_entry last_node
;
153 slot
= pointer_map_contains (encoder
->map
, node
);
154 if (slot
== NULL
|| !*slot
)
157 index
= (size_t) *slot
- 1;
158 gcc_checking_assert (encoder
->nodes
[index
].node
== node
);
160 /* Remove from vector. We do this by swapping node with the last element
162 last_node
= encoder
->nodes
.pop ();
163 if (last_node
.node
!= node
)
165 last_slot
= pointer_map_contains (encoder
->map
, last_node
.node
);
166 gcc_checking_assert (last_slot
&& *last_slot
);
167 *last_slot
= (void *)(size_t) (index
+ 1);
169 /* Move the last element to the original spot of NODE. */
170 encoder
->nodes
[index
] = last_node
;
173 /* Remove element from hash table. */
179 /* Return TRUE if we should encode initializer of NODE (if any). */
182 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder
,
183 struct cgraph_node
*node
)
185 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
186 return encoder
->nodes
[index
].body
;
189 /* Return TRUE if we should encode body of NODE (if any). */
192 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder
,
193 struct cgraph_node
*node
)
195 int index
= lto_symtab_encoder_encode (encoder
, (symtab_node
)node
);
196 gcc_checking_assert (encoder
->nodes
[index
].node
== (symtab_node
)node
);
197 encoder
->nodes
[index
].body
= true;
200 /* Return TRUE if we should encode initializer of NODE (if any). */
203 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder
,
204 struct varpool_node
*node
)
206 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
207 if (index
== LCC_NOT_FOUND
)
209 return encoder
->nodes
[index
].initializer
;
212 /* Return TRUE if we should encode initializer of NODE (if any). */
215 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder
,
216 struct varpool_node
*node
)
218 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
219 encoder
->nodes
[index
].initializer
= true;
222 /* Return TRUE if we should encode initializer of NODE (if any). */
225 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder
,
228 int index
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
);
229 if (index
== LCC_NOT_FOUND
)
231 return encoder
->nodes
[index
].in_partition
;
234 /* Return TRUE if we should encode body of NODE (if any). */
237 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder
,
240 int index
= lto_symtab_encoder_encode (encoder
, (symtab_node
)node
);
241 encoder
->nodes
[index
].in_partition
= true;
244 /* Output the cgraph EDGE to OB using ENCODER. */
247 lto_output_edge (struct lto_simple_output_block
*ob
, struct cgraph_edge
*edge
,
248 lto_symtab_encoder_t encoder
)
254 if (edge
->indirect_unknown_callee
)
255 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
256 LTO_symtab_indirect_edge
);
258 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
261 ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)edge
->caller
);
262 gcc_assert (ref
!= LCC_NOT_FOUND
);
263 streamer_write_hwi_stream (ob
->main_stream
, ref
);
265 if (!edge
->indirect_unknown_callee
)
267 ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)edge
->callee
);
268 gcc_assert (ref
!= LCC_NOT_FOUND
);
269 streamer_write_hwi_stream (ob
->main_stream
, ref
);
272 streamer_write_gcov_count_stream (ob
->main_stream
, edge
->count
);
274 bp
= bitpack_create (ob
->main_stream
);
275 uid
= (!gimple_has_body_p (edge
->caller
->symbol
.decl
)
276 ? edge
->lto_stmt_uid
: gimple_uid (edge
->call_stmt
) + 1);
277 bp_pack_enum (&bp
, cgraph_inline_failed_enum
,
278 CIF_N_REASONS
, edge
->inline_failed
);
279 bp_pack_var_len_unsigned (&bp
, uid
);
280 bp_pack_var_len_unsigned (&bp
, edge
->frequency
);
281 bp_pack_value (&bp
, edge
->indirect_inlining_edge
, 1);
282 bp_pack_value (&bp
, edge
->speculative
, 1);
283 bp_pack_value (&bp
, edge
->call_stmt_cannot_inline_p
, 1);
284 bp_pack_value (&bp
, edge
->can_throw_external
, 1);
285 if (edge
->indirect_unknown_callee
)
287 int flags
= edge
->indirect_info
->ecf_flags
;
288 bp_pack_value (&bp
, (flags
& ECF_CONST
) != 0, 1);
289 bp_pack_value (&bp
, (flags
& ECF_PURE
) != 0, 1);
290 bp_pack_value (&bp
, (flags
& ECF_NORETURN
) != 0, 1);
291 bp_pack_value (&bp
, (flags
& ECF_MALLOC
) != 0, 1);
292 bp_pack_value (&bp
, (flags
& ECF_NOTHROW
) != 0, 1);
293 bp_pack_value (&bp
, (flags
& ECF_RETURNS_TWICE
) != 0, 1);
294 /* Flags that should not appear on indirect calls. */
295 gcc_assert (!(flags
& (ECF_LOOPING_CONST_OR_PURE
301 streamer_write_bitpack (&bp
);
302 if (edge
->indirect_unknown_callee
)
304 streamer_write_hwi_stream (ob
->main_stream
,
305 edge
->indirect_info
->common_target_id
);
306 if (edge
->indirect_info
->common_target_id
)
307 streamer_write_hwi_stream
308 (ob
->main_stream
, edge
->indirect_info
->common_target_probability
);
312 /* Return if LIST contain references from other partitions. */
315 referenced_from_other_partition_p (struct ipa_ref_list
*list
, lto_symtab_encoder_t encoder
)
319 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
321 if (ref
->referring
->symbol
.in_other_partition
322 || !lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
328 /* Return true when node is reachable from other partition. */
331 reachable_from_other_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
333 struct cgraph_edge
*e
;
334 if (!node
->symbol
.definition
)
336 if (node
->global
.inlined_to
)
338 for (e
= node
->callers
; e
; e
= e
->next_caller
)
339 if (e
->caller
->symbol
.in_other_partition
340 || !lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)e
->caller
))
345 /* Return if LIST contain references from other partitions. */
348 referenced_from_this_partition_p (struct ipa_ref_list
*list
,
349 lto_symtab_encoder_t encoder
)
353 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
354 if (lto_symtab_encoder_in_partition_p (encoder
, ref
->referring
))
359 /* Return true when node is reachable from other partition. */
362 reachable_from_this_partition_p (struct cgraph_node
*node
, lto_symtab_encoder_t encoder
)
364 struct cgraph_edge
*e
;
365 for (e
= node
->callers
; e
; e
= e
->next_caller
)
366 if (lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)e
->caller
))
371 /* Output the cgraph NODE to OB. ENCODER is used to find the
372 reference number of NODE->inlined_to. SET is the set of nodes we
373 are writing to the current file. If NODE is not in SET, then NODE
374 is a boundary of a cgraph_node_set and we pretend NODE just has a
375 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
376 that have had their callgraph node written so far. This is used to
377 determine if NODE is a clone of a previously written node. */
380 lto_output_node (struct lto_simple_output_block
*ob
, struct cgraph_node
*node
,
381 lto_symtab_encoder_t encoder
)
387 bool in_other_partition
= false;
388 struct cgraph_node
*clone_of
, *ultimate_clone_of
;
389 struct ipa_opt_pass_d
*pass
;
393 boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)node
);
395 if (node
->symbol
.analyzed
&& !boundary_p
)
396 tag
= LTO_symtab_analyzed_node
;
398 tag
= LTO_symtab_unavail_node
;
400 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
402 streamer_write_hwi_stream (ob
->main_stream
, node
->symbol
.order
);
404 /* In WPA mode, we only output part of the call-graph. Also, we
405 fake cgraph node attributes. There are two cases that we care.
407 Boundary nodes: There are nodes that are not part of SET but are
408 called from within SET. We artificially make them look like
409 externally visible nodes with no function body.
411 Cherry-picked nodes: These are nodes we pulled from other
412 translation units into SET during IPA-inlining. We make them as
413 local static nodes to prevent clashes with other local statics. */
414 if (boundary_p
&& node
->symbol
.analyzed
&& !DECL_EXTERNAL (node
->symbol
.decl
))
416 /* Inline clones can not be part of boundary.
417 gcc_assert (!node->global.inlined_to);
419 FIXME: At the moment they can be, when partition contains an inline
420 clone that is clone of inline clone from outside partition. We can
421 reshape the clone tree and make other tree to be the root, but it
422 needs a bit extra work and will be promplty done by cgraph_remove_node
423 after reading back. */
424 in_other_partition
= 1;
427 clone_of
= node
->clone_of
;
429 && (ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)clone_of
)) == LCC_NOT_FOUND
)
430 if (clone_of
->prev_sibling_clone
)
431 clone_of
= clone_of
->prev_sibling_clone
;
433 clone_of
= clone_of
->clone_of
;
435 /* See if body of the master function is output. If not, we are seeing only
436 an declaration and we do not need to pass down clone tree. */
437 ultimate_clone_of
= clone_of
;
438 while (ultimate_clone_of
&& ultimate_clone_of
->clone_of
)
439 ultimate_clone_of
= ultimate_clone_of
->clone_of
;
441 if (clone_of
&& !lto_symtab_encoder_encode_body_p (encoder
, ultimate_clone_of
))
444 if (tag
== LTO_symtab_analyzed_node
)
445 gcc_assert (clone_of
|| !node
->clone_of
);
447 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
449 streamer_write_hwi_stream (ob
->main_stream
, ref
);
452 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->symbol
.decl
);
453 streamer_write_gcov_count_stream (ob
->main_stream
, node
->count
);
454 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
456 streamer_write_hwi_stream (ob
->main_stream
,
457 node
->ipa_transforms_to_apply
.length ());
458 FOR_EACH_VEC_ELT (node
->ipa_transforms_to_apply
, i
, pass
)
459 streamer_write_hwi_stream (ob
->main_stream
, pass
->static_pass_number
);
461 if (tag
== LTO_symtab_analyzed_node
)
463 if (node
->global
.inlined_to
)
465 ref
= lto_symtab_encoder_lookup (encoder
, (symtab_node
)node
->global
.inlined_to
);
466 gcc_assert (ref
!= LCC_NOT_FOUND
);
471 streamer_write_hwi_stream (ob
->main_stream
, ref
);
474 if (node
->symbol
.same_comdat_group
&& !boundary_p
)
476 ref
= lto_symtab_encoder_lookup (encoder
,
477 node
->symbol
.same_comdat_group
);
478 gcc_assert (ref
!= LCC_NOT_FOUND
);
482 streamer_write_hwi_stream (ob
->main_stream
, ref
);
484 bp
= bitpack_create (ob
->main_stream
);
485 bp_pack_value (&bp
, node
->local
.local
, 1);
486 bp_pack_value (&bp
, node
->symbol
.externally_visible
, 1);
487 bp_pack_value (&bp
, node
->symbol
.definition
, 1);
488 bp_pack_value (&bp
, node
->local
.versionable
, 1);
489 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
490 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
491 bp_pack_value (&bp
, node
->symbol
.force_output
, 1);
492 bp_pack_value (&bp
, node
->symbol
.forced_by_abi
, 1);
493 bp_pack_value (&bp
, node
->symbol
.unique_name
, 1);
494 bp_pack_value (&bp
, node
->symbol
.address_taken
, 1);
495 bp_pack_value (&bp
, tag
== LTO_symtab_analyzed_node
496 && !DECL_EXTERNAL (node
->symbol
.decl
)
497 && !DECL_COMDAT (node
->symbol
.decl
)
498 && (reachable_from_other_partition_p (node
, encoder
)
499 || referenced_from_other_partition_p (&node
->symbol
.ref_list
,
501 bp_pack_value (&bp
, node
->lowered
, 1);
502 bp_pack_value (&bp
, in_other_partition
, 1);
503 /* Real aliases in a boundary become non-aliases. However we still stream
504 alias info on weakrefs.
505 TODO: We lose a bit of information here - when we know that variable is
506 defined in other unit, we may use the info on aliases to resolve
507 symbol1 != symbol2 type tests that we can do only for locally defined objects
509 alias_p
= node
->symbol
.alias
&& (!boundary_p
|| node
->symbol
.weakref
);
510 bp_pack_value (&bp
, alias_p
, 1);
511 bp_pack_value (&bp
, node
->symbol
.weakref
, 1);
512 bp_pack_value (&bp
, node
->frequency
, 2);
513 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
514 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
515 bp_pack_value (&bp
, node
->tm_clone
, 1);
516 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
517 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
518 LDPR_NUM_KNOWN
, node
->symbol
.resolution
);
519 streamer_write_bitpack (&bp
);
521 if (node
->thunk
.thunk_p
&& !boundary_p
)
523 streamer_write_uhwi_stream
525 1 + (node
->thunk
.this_adjusting
!= 0) * 2
526 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
527 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
528 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
530 streamer_write_hwi_stream (ob
->main_stream
, node
->profile_id
);
533 /* Output the varpool NODE to OB.
534 If NODE is not in SET, then NODE is a boundary. */
537 lto_output_varpool_node (struct lto_simple_output_block
*ob
, struct varpool_node
*node
,
538 lto_symtab_encoder_t encoder
)
540 bool boundary_p
= !lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)node
);
545 streamer_write_enum (ob
->main_stream
, LTO_symtab_tags
, LTO_symtab_last_tag
,
546 LTO_symtab_variable
);
547 streamer_write_hwi_stream (ob
->main_stream
, node
->symbol
.order
);
548 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->symbol
.decl
);
549 bp
= bitpack_create (ob
->main_stream
);
550 bp_pack_value (&bp
, node
->symbol
.externally_visible
, 1);
551 bp_pack_value (&bp
, node
->symbol
.force_output
, 1);
552 bp_pack_value (&bp
, node
->symbol
.forced_by_abi
, 1);
553 bp_pack_value (&bp
, node
->symbol
.unique_name
, 1);
554 bp_pack_value (&bp
, node
->symbol
.definition
, 1);
555 alias_p
= node
->symbol
.alias
&& (!boundary_p
|| node
->symbol
.weakref
);
556 bp_pack_value (&bp
, alias_p
, 1);
557 bp_pack_value (&bp
, node
->symbol
.weakref
, 1);
558 bp_pack_value (&bp
, node
->symbol
.analyzed
&& !boundary_p
, 1);
559 gcc_assert (node
->symbol
.definition
|| !node
->symbol
.analyzed
);
560 /* Constant pool initializers can be de-unified into individual ltrans units.
561 FIXME: Alternatively at -Os we may want to avoid generating for them the local
562 labels and share them across LTRANS partitions. */
563 if (DECL_IN_CONSTANT_POOL (node
->symbol
.decl
)
564 && !DECL_EXTERNAL (node
->symbol
.decl
)
565 && !DECL_COMDAT (node
->symbol
.decl
))
567 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
568 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
572 bp_pack_value (&bp
, node
->symbol
.definition
573 && referenced_from_other_partition_p (&node
->symbol
.ref_list
,
575 bp_pack_value (&bp
, node
->symbol
.analyzed
576 && boundary_p
&& !DECL_EXTERNAL (node
->symbol
.decl
), 1);
577 /* in_other_partition. */
579 streamer_write_bitpack (&bp
);
580 if (node
->symbol
.same_comdat_group
&& !boundary_p
)
582 ref
= lto_symtab_encoder_lookup (encoder
,
583 node
->symbol
.same_comdat_group
);
584 gcc_assert (ref
!= LCC_NOT_FOUND
);
588 streamer_write_hwi_stream (ob
->main_stream
, ref
);
589 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
590 LDPR_NUM_KNOWN
, node
->symbol
.resolution
);
593 /* Output the varpool NODE to OB.
594 If NODE is not in SET, then NODE is a boundary. */
597 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
598 lto_symtab_encoder_t encoder
)
602 int uid
= ref
->lto_stmt_uid
;
603 struct cgraph_node
*node
;
605 bp
= bitpack_create (ob
->main_stream
);
606 bp_pack_value (&bp
, ref
->use
, 2);
607 bp_pack_value (&bp
, ref
->speculative
, 1);
608 streamer_write_bitpack (&bp
);
609 nref
= lto_symtab_encoder_lookup (encoder
, ref
->referred
);
610 gcc_assert (nref
!= LCC_NOT_FOUND
);
611 streamer_write_hwi_stream (ob
->main_stream
, nref
);
613 node
= dyn_cast
<cgraph_node
> (ref
->referring
);
617 uid
= gimple_uid (ref
->stmt
) + 1;
618 streamer_write_hwi_stream (ob
->main_stream
, uid
);
622 /* Stream out profile_summary to OB. */
625 output_profile_summary (struct lto_simple_output_block
*ob
)
632 /* We do not output num and run_max, they are not used by
633 GCC profile feedback and they are difficult to merge from multiple
635 gcc_assert (profile_info
->runs
);
636 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
637 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_max
);
639 /* sum_all is needed for computing the working set with the
641 streamer_write_gcov_count_stream (ob
->main_stream
, profile_info
->sum_all
);
643 /* Create and output a bitpack of non-zero histogram entries indices. */
644 bp
= bitpack_create (ob
->main_stream
);
645 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
646 bp_pack_value (&bp
, profile_info
->histogram
[h_ix
].num_counters
> 0, 1);
647 streamer_write_bitpack (&bp
);
648 /* Now stream out only those non-zero entries. */
649 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
651 if (!profile_info
->histogram
[h_ix
].num_counters
)
653 streamer_write_gcov_count_stream (ob
->main_stream
,
654 profile_info
->histogram
[h_ix
].num_counters
);
655 streamer_write_gcov_count_stream (ob
->main_stream
,
656 profile_info
->histogram
[h_ix
].min_value
);
657 streamer_write_gcov_count_stream (ob
->main_stream
,
658 profile_info
->histogram
[h_ix
].cum_value
);
660 /* IPA-profile computes hot bb threshold based on cumulated
661 whole program profile. We need to stream it down to ltrans. */
663 streamer_write_gcov_count_stream (ob
->main_stream
,
664 get_hot_bb_threshold ());
667 streamer_write_uhwi_stream (ob
->main_stream
, 0);
670 /* Output all callees or indirect outgoing edges. EDGE must be the first such
674 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
675 struct lto_simple_output_block
*ob
,
676 lto_symtab_encoder_t encoder
)
681 /* Output edges in backward direction, so the reconstructed callgraph match
682 and it is easy to associate call sites in the IPA pass summaries. */
683 while (edge
->next_callee
)
684 edge
= edge
->next_callee
;
685 for (; edge
; edge
= edge
->prev_callee
)
686 lto_output_edge (ob
, edge
, encoder
);
689 /* Output the part of the cgraph in SET. */
692 output_refs (lto_symtab_encoder_t encoder
)
694 lto_symtab_encoder_iterator lsei
;
695 struct lto_simple_output_block
*ob
;
700 ob
= lto_create_simple_output_block (LTO_section_refs
);
702 for (lsei
= lsei_start_in_partition (encoder
); !lsei_end_p (lsei
);
703 lsei_next_in_partition (&lsei
))
705 symtab_node node
= lsei_node (lsei
);
707 count
= ipa_ref_list_nreferences (&node
->symbol
.ref_list
);
710 streamer_write_gcov_count_stream (ob
->main_stream
, count
);
711 streamer_write_uhwi_stream (ob
->main_stream
,
712 lto_symtab_encoder_lookup (encoder
, node
));
713 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
,
715 lto_output_ref (ob
, ref
, encoder
);
719 streamer_write_uhwi_stream (ob
->main_stream
, 0);
721 lto_destroy_simple_output_block (ob
);
724 /* Add NODE into encoder as well as nodes it is cloned from.
725 Do it in a way so clones appear first. */
728 add_node_to (lto_symtab_encoder_t encoder
, struct cgraph_node
*node
,
732 add_node_to (encoder
, node
->clone_of
, include_body
);
733 else if (include_body
)
734 lto_set_symtab_encoder_encode_body (encoder
, node
);
735 lto_symtab_encoder_encode (encoder
, (symtab_node
)node
);
738 /* Add all references in LIST to encoders. */
741 add_references (lto_symtab_encoder_t encoder
,
742 struct ipa_ref_list
*list
)
746 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
747 if (is_a
<cgraph_node
> (ref
->referred
))
748 add_node_to (encoder
, ipa_ref_node (ref
), false);
750 lto_symtab_encoder_encode (encoder
, ref
->referred
);
753 /* Find all symbols we want to stream into given partition and insert them
756 The function actually replaces IN_ENCODER by new one. The reason is that
757 streaming code needs clone's origin to be streamed before clone. This
758 means that we need to insert the nodes in specific order. This order is
759 ignored by the partitioning logic earlier. */
762 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder
)
764 struct cgraph_node
*node
;
765 struct cgraph_edge
*edge
;
767 lto_symtab_encoder_t encoder
;
768 lto_symtab_encoder_iterator lsei
;
769 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
771 encoder
= lto_symtab_encoder_new (false);
773 /* Go over all entries in the IN_ENCODER and duplicate them to
774 ENCODER. At the same time insert masters of clones so
775 every master appears before clone. */
776 for (lsei
= lsei_start_function_in_partition (in_encoder
);
777 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
779 node
= lsei_cgraph_node (lsei
);
780 add_node_to (encoder
, node
, true);
781 lto_set_symtab_encoder_in_partition (encoder
, (symtab_node
)node
);
782 add_references (encoder
, &node
->symbol
.ref_list
);
783 /* For proper debug info, we need to ship the origins, too. */
784 if (DECL_ABSTRACT_ORIGIN (node
->symbol
.decl
))
786 struct cgraph_node
*origin_node
787 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node
->symbol
.decl
));
788 add_node_to (encoder
, origin_node
, true);
791 for (lsei
= lsei_start_variable_in_partition (in_encoder
);
792 !lsei_end_p (lsei
); lsei_next_variable_in_partition (&lsei
))
794 struct varpool_node
*vnode
= lsei_varpool_node (lsei
);
796 lto_set_symtab_encoder_in_partition (encoder
, (symtab_node
)vnode
);
797 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
798 add_references (encoder
, &vnode
->symbol
.ref_list
);
799 /* For proper debug info, we need to ship the origins, too. */
800 if (DECL_ABSTRACT_ORIGIN (vnode
->symbol
.decl
))
802 struct varpool_node
*origin_node
803 = varpool_get_node (DECL_ABSTRACT_ORIGIN (node
->symbol
.decl
));
804 lto_set_symtab_encoder_in_partition (encoder
, (symtab_node
)origin_node
);
807 /* Pickle in also the initializer of all referenced readonly variables
808 to help folding. Constant pool variables are not shared, so we must
810 for (i
= 0; i
< lto_symtab_encoder_size (encoder
); i
++)
812 symtab_node node
= lto_symtab_encoder_deref (encoder
, i
);
813 if (varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
))
815 if (!lto_symtab_encoder_encode_initializer_p (encoder
,
817 && ctor_for_folding (vnode
->symbol
.decl
) != error_mark_node
)
819 lto_set_symtab_encoder_encode_initializer (encoder
, vnode
);
820 add_references (encoder
, &vnode
->symbol
.ref_list
);
825 /* Go over all the nodes again to include callees that are not in
827 for (lsei
= lsei_start_function_in_partition (encoder
);
828 !lsei_end_p (lsei
); lsei_next_function_in_partition (&lsei
))
830 node
= lsei_cgraph_node (lsei
);
831 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
833 struct cgraph_node
*callee
= edge
->callee
;
834 if (!lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
)callee
))
836 /* We should have moved all the inlines. */
837 gcc_assert (!callee
->global
.inlined_to
);
838 add_node_to (encoder
, callee
, false);
841 /* Add all possible targets for late devirtualization. */
842 if (flag_devirtualize
)
843 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
844 if (edge
->indirect_info
->polymorphic
)
849 vec
<cgraph_node
*>targets
850 = possible_polymorphic_call_targets
851 (edge
, &final
, &cache_token
);
852 if (!pointer_set_insert (reachable_call_targets
,
855 for (i
= 0; i
< targets
.length (); i
++)
857 struct cgraph_node
*callee
= targets
[i
];
859 /* Adding an external declarations into the unit serves
860 no purpose and just increases its boundary. */
861 if (callee
->symbol
.definition
862 && !lto_symtab_encoder_in_partition_p
863 (encoder
, (symtab_node
)callee
))
865 gcc_assert (!callee
->global
.inlined_to
);
866 add_node_to (encoder
, callee
, false);
872 lto_symtab_encoder_delete (in_encoder
);
873 pointer_set_destroy (reachable_call_targets
);
877 /* Output the part of the symtab in SET and VSET. */
882 struct cgraph_node
*node
;
883 struct lto_simple_output_block
*ob
;
884 lto_symtab_encoder_iterator lsei
;
886 lto_symtab_encoder_t encoder
;
887 static bool asm_nodes_output
= false;
890 output_cgraph_opt_summary ();
892 ob
= lto_create_simple_output_block (LTO_section_symtab_nodes
);
894 output_profile_summary (ob
);
896 /* An encoder for cgraph nodes should have been created by
897 ipa_write_summaries_1. */
898 gcc_assert (ob
->decl_state
->symtab_node_encoder
);
899 encoder
= ob
->decl_state
->symtab_node_encoder
;
901 /* Write out the nodes. We must first output a node and then its clones,
902 otherwise at a time reading back the node there would be nothing to clone
904 n_nodes
= lto_symtab_encoder_size (encoder
);
905 for (i
= 0; i
< n_nodes
; i
++)
907 symtab_node node
= lto_symtab_encoder_deref (encoder
, i
);
908 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
909 lto_output_node (ob
, cnode
, encoder
);
911 lto_output_varpool_node (ob
, varpool (node
), encoder
);
915 /* Go over the nodes in SET again to write edges. */
916 for (lsei
= lsei_start_function_in_partition (encoder
); !lsei_end_p (lsei
);
917 lsei_next_function_in_partition (&lsei
))
919 node
= lsei_cgraph_node (lsei
);
920 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
921 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
924 streamer_write_uhwi_stream (ob
->main_stream
, 0);
926 lto_destroy_simple_output_block (ob
);
928 /* Emit toplevel asms.
929 When doing WPA we must output every asm just once. Since we do not partition asm
930 nodes at all, output them to first output. This is kind of hack, but should work
932 if (!asm_nodes_output
)
934 asm_nodes_output
= true;
935 lto_output_toplevel_asms ();
938 output_refs (encoder
);
941 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
942 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
943 NODE or to replace the values in it, for instance because the first
944 time we saw it, the function body was not available but now it
945 is. BP is a bitpack with all the bitflags for NODE read from the
949 input_overwrite_node (struct lto_file_decl_data
*file_data
,
950 struct cgraph_node
*node
,
951 enum LTO_symtab_tags tag
,
952 struct bitpack_d
*bp
)
954 node
->symbol
.aux
= (void *) tag
;
955 node
->symbol
.lto_file_data
= file_data
;
957 node
->local
.local
= bp_unpack_value (bp
, 1);
958 node
->symbol
.externally_visible
= bp_unpack_value (bp
, 1);
959 node
->symbol
.definition
= bp_unpack_value (bp
, 1);
960 node
->local
.versionable
= bp_unpack_value (bp
, 1);
961 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
962 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
963 node
->symbol
.force_output
= bp_unpack_value (bp
, 1);
964 node
->symbol
.forced_by_abi
= bp_unpack_value (bp
, 1);
965 node
->symbol
.unique_name
= bp_unpack_value (bp
, 1);
966 node
->symbol
.address_taken
= bp_unpack_value (bp
, 1);
967 node
->symbol
.used_from_other_partition
= bp_unpack_value (bp
, 1);
968 node
->lowered
= bp_unpack_value (bp
, 1);
969 node
->symbol
.analyzed
= tag
== LTO_symtab_analyzed_node
;
970 node
->symbol
.in_other_partition
= bp_unpack_value (bp
, 1);
971 if (node
->symbol
.in_other_partition
972 /* Avoid updating decl when we are seeing just inline clone.
973 When inlining function that has functions already inlined into it,
974 we produce clones of inline clones.
976 WPA partitioning might put each clone into different unit and
977 we might end up streaming inline clone from other partition
978 to support clone we are interested in. */
980 || node
->clone_of
->symbol
.decl
!= node
->symbol
.decl
))
982 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
983 TREE_STATIC (node
->symbol
.decl
) = 0;
985 node
->symbol
.alias
= bp_unpack_value (bp
, 1);
986 node
->symbol
.weakref
= bp_unpack_value (bp
, 1);
987 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
988 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
989 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
990 node
->tm_clone
= bp_unpack_value (bp
, 1);
991 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
992 node
->symbol
.resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
996 /* Return string alias is alias of. */
999 get_alias_symbol (tree decl
)
1001 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1002 return get_identifier (TREE_STRING_POINTER
1003 (TREE_VALUE (TREE_VALUE (alias
))));
1006 /* Read a node from input_block IB. TAG is the node's tag just read.
1007 Return the node read or overwriten. */
1009 static struct cgraph_node
*
1010 input_node (struct lto_file_decl_data
*file_data
,
1011 struct lto_input_block
*ib
,
1012 enum LTO_symtab_tags tag
,
1013 vec
<symtab_node
> nodes
)
1015 gcc::pass_manager
*passes
= g
->get_passes ();
1017 struct cgraph_node
*node
;
1018 struct bitpack_d bp
;
1019 unsigned decl_index
;
1020 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
1025 order
= streamer_read_hwi (ib
) + order_base
;
1026 clone_ref
= streamer_read_hwi (ib
);
1028 decl_index
= streamer_read_uhwi (ib
);
1029 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
1031 if (clone_ref
!= LCC_NOT_FOUND
)
1033 node
= cgraph_clone_node (cgraph (nodes
[clone_ref
]), fn_decl
,
1034 0, CGRAPH_FREQ_BASE
, false,
1035 vNULL
, false, NULL
);
1039 /* Declaration of functions can be already merged with a declaration
1040 from other input file. We keep cgraph unmerged until after streaming
1041 of ipa passes is done. Alays forcingly create a fresh node. */
1042 node
= cgraph_create_empty_node ();
1043 node
->symbol
.decl
= fn_decl
;
1044 symtab_register_node ((symtab_node
)node
);
1047 node
->symbol
.order
= order
;
1048 if (order
>= symtab_order
)
1049 symtab_order
= order
+ 1;
1051 node
->count
= streamer_read_gcov_count (ib
);
1052 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1054 count
= streamer_read_hwi (ib
);
1055 node
->ipa_transforms_to_apply
= vNULL
;
1056 for (i
= 0; i
< count
; i
++)
1058 struct opt_pass
*pass
;
1059 int pid
= streamer_read_hwi (ib
);
1061 gcc_assert (pid
< passes
->passes_by_id_size
);
1062 pass
= passes
->passes_by_id
[pid
];
1063 node
->ipa_transforms_to_apply
.safe_push ((struct ipa_opt_pass_d
*) pass
);
1066 if (tag
== LTO_symtab_analyzed_node
)
1067 ref
= streamer_read_hwi (ib
);
1069 ref2
= streamer_read_hwi (ib
);
1071 /* Make sure that we have not read this node before. Nodes that
1072 have already been read will have their tag stored in the 'aux'
1073 field. Since built-in functions can be referenced in multiple
1074 functions, they are expected to be read more than once. */
1075 if (node
->symbol
.aux
&& !DECL_BUILT_IN (node
->symbol
.decl
))
1076 internal_error ("bytecode stream: found multiple instances of cgraph "
1077 "node with uid %d", node
->uid
);
1079 bp
= streamer_read_bitpack (ib
);
1080 input_overwrite_node (file_data
, node
, tag
, &bp
);
1082 /* Store a reference for now, and fix up later to be a pointer. */
1083 node
->global
.inlined_to
= (cgraph_node_ptr
) (intptr_t) ref
;
1085 /* Store a reference for now, and fix up later to be a pointer. */
1086 node
->symbol
.same_comdat_group
= (symtab_node
) (intptr_t) ref2
;
1088 if (node
->thunk
.thunk_p
)
1090 int type
= streamer_read_uhwi (ib
);
1091 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1092 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1094 node
->thunk
.fixed_offset
= fixed_offset
;
1095 node
->thunk
.this_adjusting
= (type
& 2);
1096 node
->thunk
.virtual_value
= virtual_value
;
1097 node
->thunk
.virtual_offset_p
= (type
& 4);
1099 if (node
->symbol
.alias
&& !node
->symbol
.analyzed
&& node
->symbol
.weakref
)
1100 node
->symbol
.alias_target
= get_alias_symbol (node
->symbol
.decl
);
1101 node
->profile_id
= streamer_read_hwi (ib
);
1105 /* Read a node from input_block IB. TAG is the node's tag just read.
1106 Return the node read or overwriten. */
1108 static struct varpool_node
*
1109 input_varpool_node (struct lto_file_decl_data
*file_data
,
1110 struct lto_input_block
*ib
)
1114 struct varpool_node
*node
;
1115 struct bitpack_d bp
;
1116 int ref
= LCC_NOT_FOUND
;
1119 order
= streamer_read_hwi (ib
) + order_base
;
1120 decl_index
= streamer_read_uhwi (ib
);
1121 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1123 /* Declaration of functions can be already merged with a declaration
1124 from other input file. We keep cgraph unmerged until after streaming
1125 of ipa passes is done. Alays forcingly create a fresh node. */
1126 node
= varpool_create_empty_node ();
1127 node
->symbol
.decl
= var_decl
;
1128 symtab_register_node ((symtab_node
)node
);
1130 node
->symbol
.order
= order
;
1131 if (order
>= symtab_order
)
1132 symtab_order
= order
+ 1;
1133 node
->symbol
.lto_file_data
= file_data
;
1135 bp
= streamer_read_bitpack (ib
);
1136 node
->symbol
.externally_visible
= bp_unpack_value (&bp
, 1);
1137 node
->symbol
.force_output
= bp_unpack_value (&bp
, 1);
1138 node
->symbol
.forced_by_abi
= bp_unpack_value (&bp
, 1);
1139 node
->symbol
.unique_name
= bp_unpack_value (&bp
, 1);
1140 node
->symbol
.definition
= bp_unpack_value (&bp
, 1);
1141 node
->symbol
.alias
= bp_unpack_value (&bp
, 1);
1142 node
->symbol
.weakref
= bp_unpack_value (&bp
, 1);
1143 node
->symbol
.analyzed
= bp_unpack_value (&bp
, 1);
1144 node
->symbol
.used_from_other_partition
= bp_unpack_value (&bp
, 1);
1145 node
->symbol
.in_other_partition
= bp_unpack_value (&bp
, 1);
1146 if (node
->symbol
.in_other_partition
)
1148 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
1149 TREE_STATIC (node
->symbol
.decl
) = 0;
1151 if (node
->symbol
.alias
&& !node
->symbol
.analyzed
&& node
->symbol
.weakref
)
1152 node
->symbol
.alias_target
= get_alias_symbol (node
->symbol
.decl
);
1153 ref
= streamer_read_hwi (ib
);
1154 /* Store a reference for now, and fix up later to be a pointer. */
1155 node
->symbol
.same_comdat_group
= (symtab_node
) (intptr_t) ref
;
1156 node
->symbol
.resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1162 /* Read a node from input_block IB. TAG is the node's tag just read.
1163 Return the node read or overwriten. */
1166 input_ref (struct lto_input_block
*ib
,
1167 symtab_node referring_node
,
1168 vec
<symtab_node
> nodes
)
1170 symtab_node node
= NULL
;
1171 struct bitpack_d bp
;
1172 enum ipa_ref_use use
;
1174 struct ipa_ref
*ref
;
1176 bp
= streamer_read_bitpack (ib
);
1177 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1178 speculative
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 1);
1179 node
= nodes
[streamer_read_hwi (ib
)];
1180 ref
= ipa_record_reference (referring_node
, node
, use
, NULL
);
1181 ref
->speculative
= speculative
;
1182 if (is_a
<cgraph_node
> (referring_node
))
1183 ref
->lto_stmt_uid
= streamer_read_hwi (ib
);
1186 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1187 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1188 edge being read is indirect (in the sense that it has
1189 indirect_unknown_callee set). */
1192 input_edge (struct lto_input_block
*ib
, vec
<symtab_node
> nodes
,
1195 struct cgraph_node
*caller
, *callee
;
1196 struct cgraph_edge
*edge
;
1197 unsigned int stmt_id
;
1200 cgraph_inline_failed_t inline_failed
;
1201 struct bitpack_d bp
;
1204 caller
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1205 if (caller
== NULL
|| caller
->symbol
.decl
== NULL_TREE
)
1206 internal_error ("bytecode stream: no caller found while reading edge");
1210 callee
= cgraph (nodes
[streamer_read_hwi (ib
)]);
1211 if (callee
== NULL
|| callee
->symbol
.decl
== NULL_TREE
)
1212 internal_error ("bytecode stream: no callee found while reading edge");
1217 count
= streamer_read_gcov_count (ib
);
1219 bp
= streamer_read_bitpack (ib
);
1220 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_enum
, CIF_N_REASONS
);
1221 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1222 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1225 edge
= cgraph_create_indirect_edge (caller
, NULL
, 0, count
, freq
);
1227 edge
= cgraph_create_edge (caller
, callee
, NULL
, count
, freq
);
1229 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1230 edge
->speculative
= bp_unpack_value (&bp
, 1);
1231 edge
->lto_stmt_uid
= stmt_id
;
1232 edge
->inline_failed
= inline_failed
;
1233 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1234 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1237 if (bp_unpack_value (&bp
, 1))
1238 ecf_flags
|= ECF_CONST
;
1239 if (bp_unpack_value (&bp
, 1))
1240 ecf_flags
|= ECF_PURE
;
1241 if (bp_unpack_value (&bp
, 1))
1242 ecf_flags
|= ECF_NORETURN
;
1243 if (bp_unpack_value (&bp
, 1))
1244 ecf_flags
|= ECF_MALLOC
;
1245 if (bp_unpack_value (&bp
, 1))
1246 ecf_flags
|= ECF_NOTHROW
;
1247 if (bp_unpack_value (&bp
, 1))
1248 ecf_flags
|= ECF_RETURNS_TWICE
;
1249 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1250 edge
->indirect_info
->common_target_id
= streamer_read_hwi (ib
);
1251 if (edge
->indirect_info
->common_target_id
)
1252 edge
->indirect_info
->common_target_probability
= streamer_read_hwi (ib
);
1257 /* Read a cgraph from IB using the info in FILE_DATA. */
1259 static vec
<symtab_node
>
1260 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1261 struct lto_input_block
*ib
)
1263 enum LTO_symtab_tags tag
;
1264 vec
<symtab_node
> nodes
= vNULL
;
1268 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1269 order_base
= symtab_order
;
1272 if (tag
== LTO_symtab_edge
)
1273 input_edge (ib
, nodes
, false);
1274 else if (tag
== LTO_symtab_indirect_edge
)
1275 input_edge (ib
, nodes
, true);
1276 else if (tag
== LTO_symtab_variable
)
1278 node
= (symtab_node
)input_varpool_node (file_data
, ib
);
1279 nodes
.safe_push (node
);
1280 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1284 node
= (symtab_node
)input_node (file_data
, ib
, tag
, nodes
);
1285 if (node
== NULL
|| node
->symbol
.decl
== NULL_TREE
)
1286 internal_error ("bytecode stream: found empty cgraph node");
1287 nodes
.safe_push (node
);
1288 lto_symtab_encoder_encode (file_data
->symtab_node_encoder
, node
);
1291 tag
= streamer_read_enum (ib
, LTO_symtab_tags
, LTO_symtab_last_tag
);
1294 lto_input_toplevel_asms (file_data
, order_base
);
1296 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1297 #ifdef ENABLE_CHECKING
1298 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1299 gcc_assert (node
->symbol
.aux
|| !is_a
<cgraph_node
> (node
));
1301 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1304 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
1306 ref
= (int) (intptr_t) cnode
->global
.inlined_to
;
1308 /* We share declaration of builtins, so we may read same node twice. */
1309 if (!node
->symbol
.aux
)
1311 node
->symbol
.aux
= NULL
;
1313 /* Fixup inlined_to from reference to pointer. */
1314 if (ref
!= LCC_NOT_FOUND
)
1315 cgraph (node
)->global
.inlined_to
= cgraph (nodes
[ref
]);
1317 cnode
->global
.inlined_to
= NULL
;
1320 ref
= (int) (intptr_t) node
->symbol
.same_comdat_group
;
1322 /* Fixup same_comdat_group from reference to pointer. */
1323 if (ref
!= LCC_NOT_FOUND
)
1324 node
->symbol
.same_comdat_group
= nodes
[ref
];
1326 node
->symbol
.same_comdat_group
= NULL
;
1328 FOR_EACH_VEC_ELT (nodes
, i
, node
)
1329 node
->symbol
.aux
= is_a
<cgraph_node
> (node
) ? (void *)1 : NULL
;
1333 /* Input ipa_refs. */
1336 input_refs (struct lto_input_block
*ib
,
1337 vec
<symtab_node
> nodes
)
1344 count
= streamer_read_uhwi (ib
);
1347 idx
= streamer_read_uhwi (ib
);
1351 input_ref (ib
, node
, nodes
);
1358 static struct gcov_ctr_summary lto_gcov_summary
;
1360 /* Input profile_info from IB. */
1362 input_profile_summary (struct lto_input_block
*ib
,
1363 struct lto_file_decl_data
*file_data
)
1366 struct bitpack_d bp
;
1367 unsigned int runs
= streamer_read_uhwi (ib
);
1370 file_data
->profile_info
.runs
= runs
;
1371 file_data
->profile_info
.sum_max
= streamer_read_gcov_count (ib
);
1372 file_data
->profile_info
.sum_all
= streamer_read_gcov_count (ib
);
1374 memset (file_data
->profile_info
.histogram
, 0,
1375 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1376 /* Input the bitpack of non-zero histogram indices. */
1377 bp
= streamer_read_bitpack (ib
);
1378 /* Read in and unpack the full bitpack, flagging non-zero
1379 histogram entries by setting the num_counters non-zero. */
1380 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1382 file_data
->profile_info
.histogram
[h_ix
].num_counters
1383 = bp_unpack_value (&bp
, 1);
1385 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1387 if (!file_data
->profile_info
.histogram
[h_ix
].num_counters
)
1390 file_data
->profile_info
.histogram
[h_ix
].num_counters
1391 = streamer_read_gcov_count (ib
);
1392 file_data
->profile_info
.histogram
[h_ix
].min_value
1393 = streamer_read_gcov_count (ib
);
1394 file_data
->profile_info
.histogram
[h_ix
].cum_value
1395 = streamer_read_gcov_count (ib
);
1397 /* IPA-profile computes hot bb threshold based on cumulated
1398 whole program profile. We need to stream it down to ltrans. */
1400 set_hot_bb_threshold (streamer_read_gcov_count (ib
));
1405 /* Rescale profile summaries to the same number of runs in the whole unit. */
1408 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1410 struct lto_file_decl_data
*file_data
;
1411 unsigned int j
, h_ix
;
1412 gcov_unsigned_t max_runs
= 0;
1413 struct cgraph_node
*node
;
1414 struct cgraph_edge
*edge
;
1415 gcov_type saved_sum_all
= 0;
1416 gcov_ctr_summary
*saved_profile_info
= 0;
1417 int saved_scale
= 0;
1419 /* Find unit with maximal number of runs. If we ever get serious about
1420 roundoff errors, we might also consider computing smallest common
1422 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1423 if (max_runs
< file_data
->profile_info
.runs
)
1424 max_runs
= file_data
->profile_info
.runs
;
1429 /* Simple overflow check. We probably don't need to support that many train
1430 runs. Such a large value probably imply data corruption anyway. */
1431 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1433 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1434 INT_MAX
/ REG_BR_PROB_BASE
);
1438 profile_info
= <o_gcov_summary
;
1439 lto_gcov_summary
.runs
= max_runs
;
1440 lto_gcov_summary
.sum_max
= 0;
1441 memset (lto_gcov_summary
.histogram
, 0,
1442 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
1444 /* Rescale all units to the maximal number of runs.
1445 sum_max can not be easily merged, as we have no idea what files come from
1446 the same run. We do not use the info anyway, so leave it 0. */
1447 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1448 if (file_data
->profile_info
.runs
)
1450 int scale
= GCOV_COMPUTE_SCALE (max_runs
,
1451 file_data
->profile_info
.runs
);
1452 lto_gcov_summary
.sum_max
1453 = MAX (lto_gcov_summary
.sum_max
,
1454 apply_scale (file_data
->profile_info
.sum_max
, scale
));
1455 lto_gcov_summary
.sum_all
1456 = MAX (lto_gcov_summary
.sum_all
,
1457 apply_scale (file_data
->profile_info
.sum_all
, scale
));
1458 /* Save a pointer to the profile_info with the largest
1459 scaled sum_all and the scale for use in merging the
1461 if (!saved_profile_info
1462 || lto_gcov_summary
.sum_all
> saved_sum_all
)
1464 saved_profile_info
= &file_data
->profile_info
;
1465 saved_sum_all
= lto_gcov_summary
.sum_all
;
1466 saved_scale
= scale
;
1470 gcc_assert (saved_profile_info
);
1472 /* Scale up the histogram from the profile that had the largest
1473 scaled sum_all above. */
1474 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
1476 /* Scale up the min value as we did the corresponding sum_all
1477 above. Use that to find the new histogram index. */
1478 gcov_type scaled_min
1479 = apply_scale (saved_profile_info
->histogram
[h_ix
].min_value
,
1481 /* The new index may be shared with another scaled histogram entry,
1482 so we need to account for a non-zero histogram entry at new_ix. */
1483 unsigned new_ix
= gcov_histo_index (scaled_min
);
1484 lto_gcov_summary
.histogram
[new_ix
].min_value
1485 = (lto_gcov_summary
.histogram
[new_ix
].num_counters
1486 ? MIN (lto_gcov_summary
.histogram
[new_ix
].min_value
, scaled_min
)
1488 /* Some of the scaled counter values would ostensibly need to be placed
1489 into different (larger) histogram buckets, but we keep things simple
1490 here and place the scaled cumulative counter value in the bucket
1491 corresponding to the scaled minimum counter value. */
1492 lto_gcov_summary
.histogram
[new_ix
].cum_value
1493 += apply_scale (saved_profile_info
->histogram
[h_ix
].cum_value
,
1495 lto_gcov_summary
.histogram
[new_ix
].num_counters
1496 += saved_profile_info
->histogram
[h_ix
].num_counters
;
1499 /* Watch roundoff errors. */
1500 if (lto_gcov_summary
.sum_max
< max_runs
)
1501 lto_gcov_summary
.sum_max
= max_runs
;
1503 /* If merging already happent at WPA time, we are done. */
1507 /* Now compute count_materialization_scale of each node.
1508 During LTRANS we already have values of count_materialization_scale
1509 computed, so just update them. */
1510 FOR_EACH_FUNCTION (node
)
1511 if (node
->symbol
.lto_file_data
1512 && node
->symbol
.lto_file_data
->profile_info
.runs
)
1516 scale
= RDIV (node
->count_materialization_scale
* max_runs
,
1517 node
->symbol
.lto_file_data
->profile_info
.runs
);
1518 node
->count_materialization_scale
= scale
;
1520 fatal_error ("Profile information in %s corrupted",
1521 file_data
->file_name
);
1523 if (scale
== REG_BR_PROB_BASE
)
1525 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1526 edge
->count
= apply_scale (edge
->count
, scale
);
1527 node
->count
= apply_scale (node
->count
, scale
);
1531 /* Input and merge the symtab from each of the .o files passed to
1537 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1538 struct lto_file_decl_data
*file_data
;
1540 struct cgraph_node
*node
;
1542 while ((file_data
= file_data_vec
[j
++]))
1546 struct lto_input_block
*ib
;
1547 vec
<symtab_node
> nodes
;
1549 ib
= lto_create_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1552 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1553 input_profile_summary (ib
, file_data
);
1554 file_data
->symtab_node_encoder
= lto_symtab_encoder_new (true);
1555 nodes
= input_cgraph_1 (file_data
, ib
);
1556 lto_destroy_simple_input_block (file_data
, LTO_section_symtab_nodes
,
1559 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1562 fatal_error ("cannot find LTO section refs in %s",
1563 file_data
->file_name
);
1564 input_refs (ib
, nodes
);
1565 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1568 input_cgraph_opt_summary (nodes
);
1572 merge_profile_summaries (file_data_vec
);
1573 get_working_sets ();
1576 /* Clear out the aux field that was used to store enough state to
1577 tell which nodes should be overwritten. */
1578 FOR_EACH_FUNCTION (node
)
1580 /* Some nodes may have been created by cgraph_node. This
1581 happens when the callgraph contains nested functions. If the
1582 node for the parent function was never emitted to the gimple
1583 file, cgraph_node will create a node for it when setting the
1584 context of the nested function. */
1585 if (node
->symbol
.lto_file_data
)
1586 node
->symbol
.aux
= NULL
;
1590 /* True when we need optimization summary for NODE. */
1593 output_cgraph_opt_summary_p (struct cgraph_node
*node
)
1595 return (node
->clone_of
1596 && (node
->clone
.tree_map
1597 || node
->clone
.args_to_skip
1598 || node
->clone
.combined_args_to_skip
));
1601 /* Output optimization summary for EDGE to OB. */
1603 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1604 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1608 /* Output optimization summary for NODE to OB. */
1611 output_node_opt_summary (struct output_block
*ob
,
1612 struct cgraph_node
*node
,
1613 lto_symtab_encoder_t encoder
)
1617 struct ipa_replace_map
*map
;
1618 struct bitpack_d bp
;
1620 struct cgraph_edge
*e
;
1622 if (node
->clone
.args_to_skip
)
1624 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1625 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1626 streamer_write_uhwi (ob
, index
);
1629 streamer_write_uhwi (ob
, 0);
1630 if (node
->clone
.combined_args_to_skip
)
1632 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1633 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1634 streamer_write_uhwi (ob
, index
);
1637 streamer_write_uhwi (ob
, 0);
1638 streamer_write_uhwi (ob
, vec_safe_length (node
->clone
.tree_map
));
1639 FOR_EACH_VEC_SAFE_ELT (node
->clone
.tree_map
, i
, map
)
1641 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1642 mechanism to store function local declarations into summaries. */
1643 gcc_assert (!map
->old_tree
);
1644 streamer_write_uhwi (ob
, map
->parm_num
);
1645 gcc_assert (EXPR_LOCATION (map
->new_tree
) == UNKNOWN_LOCATION
);
1646 stream_write_tree (ob
, map
->new_tree
, true);
1647 bp
= bitpack_create (ob
->main_stream
);
1648 bp_pack_value (&bp
, map
->replace_p
, 1);
1649 bp_pack_value (&bp
, map
->ref_p
, 1);
1650 streamer_write_bitpack (&bp
);
1653 if (lto_symtab_encoder_in_partition_p (encoder
, (symtab_node
) node
))
1655 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1656 output_edge_opt_summary (ob
, e
);
1657 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1658 output_edge_opt_summary (ob
, e
);
1662 /* Output optimization summaries stored in callgraph.
1663 At the moment it is the clone info structure. */
1666 output_cgraph_opt_summary (void)
1669 lto_symtab_encoder_t encoder
;
1670 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1673 ob
->cgraph_node
= NULL
;
1674 encoder
= ob
->decl_state
->symtab_node_encoder
;
1675 n_nodes
= lto_symtab_encoder_size (encoder
);
1676 for (i
= 0; i
< n_nodes
; i
++)
1678 symtab_node node
= lto_symtab_encoder_deref (encoder
, i
);
1679 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
1680 if (cnode
&& output_cgraph_opt_summary_p (cnode
))
1683 streamer_write_uhwi (ob
, count
);
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
))
1690 streamer_write_uhwi (ob
, i
);
1691 output_node_opt_summary (ob
, cnode
, encoder
);
1694 produce_asm (ob
, NULL
);
1695 destroy_output_block (ob
);
1698 /* Input optimisation summary of EDGE. */
1701 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1702 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1706 /* Input optimisation summary of NODE. */
1709 input_node_opt_summary (struct cgraph_node
*node
,
1710 struct lto_input_block
*ib_main
,
1711 struct data_in
*data_in
)
1716 struct bitpack_d bp
;
1717 struct cgraph_edge
*e
;
1719 count
= streamer_read_uhwi (ib_main
);
1721 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1722 for (i
= 0; i
< count
; i
++)
1724 bit
= streamer_read_uhwi (ib_main
);
1725 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1727 count
= streamer_read_uhwi (ib_main
);
1729 node
->clone
.combined_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
.combined_args_to_skip
, bit
);
1735 count
= streamer_read_uhwi (ib_main
);
1736 for (i
= 0; i
< count
; i
++)
1738 struct ipa_replace_map
*map
= ggc_alloc_ipa_replace_map ();
1740 vec_safe_push (node
->clone
.tree_map
, map
);
1741 map
->parm_num
= streamer_read_uhwi (ib_main
);
1742 map
->old_tree
= NULL
;
1743 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1744 bp
= streamer_read_bitpack (ib_main
);
1745 map
->replace_p
= bp_unpack_value (&bp
, 1);
1746 map
->ref_p
= bp_unpack_value (&bp
, 1);
1748 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1749 input_edge_opt_summary (e
, ib_main
);
1750 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1751 input_edge_opt_summary (e
, ib_main
);
1754 /* Read section in file FILE_DATA of length LEN with data DATA. */
1757 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1758 const char *data
, size_t len
,
1759 vec
<symtab_node
> nodes
)
1761 const struct lto_function_header
*header
=
1762 (const struct lto_function_header
*) data
;
1763 const int cfg_offset
= sizeof (struct lto_function_header
);
1764 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1765 const int string_offset
= main_offset
+ header
->main_size
;
1766 struct data_in
*data_in
;
1767 struct lto_input_block ib_main
;
1771 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1775 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1776 header
->string_size
, vNULL
);
1777 count
= streamer_read_uhwi (&ib_main
);
1779 for (i
= 0; i
< count
; i
++)
1781 int ref
= streamer_read_uhwi (&ib_main
);
1782 input_node_opt_summary (cgraph (nodes
[ref
]),
1785 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1787 lto_data_in_delete (data_in
);
1790 /* Input optimization summary of cgraph. */
1793 input_cgraph_opt_summary (vec
<symtab_node
> nodes
)
1795 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1796 struct lto_file_decl_data
*file_data
;
1799 while ((file_data
= file_data_vec
[j
++]))
1803 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1807 input_cgraph_opt_section (file_data
, data
, len
, nodes
);