1 /* Write and read the cgraph to the memory mapped representation of a
4 Copyright 2009, 2010, 2011 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"
35 #include "tree-flow.h"
39 #include "diagnostic-core.h"
44 #include "pointer-set.h"
45 #include "lto-streamer.h"
46 #include "data-streamer.h"
47 #include "tree-streamer.h"
50 static void output_varpool (cgraph_node_set
, varpool_node_set
);
51 static void output_cgraph_opt_summary (cgraph_node_set set
);
52 static void input_cgraph_opt_summary (VEC (cgraph_node_ptr
, heap
) * nodes
);
54 /* Number of LDPR values known to GCC. */
55 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
57 /* All node orders are ofsetted by ORDER_BASE. */
58 static int order_base
;
60 /* Cgraph streaming is organized as set of record whose type
61 is indicated by a tag. */
64 /* Must leave 0 for the stopper. */
66 /* Cgraph node without body available. */
67 LTO_cgraph_unavail_node
= 1,
68 /* Cgraph node with function body. */
69 LTO_cgraph_analyzed_node
,
72 LTO_cgraph_indirect_edge
,
76 /* Create a new cgraph encoder. */
79 lto_cgraph_encoder_new (void)
81 lto_cgraph_encoder_t encoder
= XCNEW (struct lto_cgraph_encoder_d
);
82 encoder
->map
= pointer_map_create ();
83 encoder
->nodes
= NULL
;
84 encoder
->body
= pointer_set_create ();
89 /* Delete ENCODER and its components. */
92 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder
)
94 VEC_free (cgraph_node_ptr
, heap
, encoder
->nodes
);
95 pointer_map_destroy (encoder
->map
);
96 pointer_set_destroy (encoder
->body
);
101 /* Return the existing reference number of NODE in the cgraph encoder in
102 output block OB. Assign a new reference if this is the first time
106 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder
,
107 struct cgraph_node
*node
)
112 slot
= pointer_map_contains (encoder
->map
, node
);
115 ref
= VEC_length (cgraph_node_ptr
, encoder
->nodes
);
116 slot
= pointer_map_insert (encoder
->map
, node
);
117 *slot
= (void *) (intptr_t) ref
;
118 VEC_safe_push (cgraph_node_ptr
, heap
, encoder
->nodes
, node
);
121 ref
= (int) (intptr_t) *slot
;
126 #define LCC_NOT_FOUND (-1)
128 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
129 or LCC_NOT_FOUND if it is not there. */
132 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder
,
133 struct cgraph_node
*node
)
135 void **slot
= pointer_map_contains (encoder
->map
, node
);
136 return (slot
? (int) (intptr_t) *slot
: LCC_NOT_FOUND
);
140 /* Return the cgraph node corresponding to REF using ENCODER. */
143 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder
, int ref
)
145 if (ref
== LCC_NOT_FOUND
)
148 return VEC_index (cgraph_node_ptr
, encoder
->nodes
, ref
);
152 /* Return TRUE if we should encode initializer of NODE (if any). */
155 lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t encoder
,
156 struct cgraph_node
*node
)
158 return pointer_set_contains (encoder
->body
, node
);
161 /* Return TRUE if we should encode body of NODE (if any). */
164 lto_set_cgraph_encoder_encode_body (lto_cgraph_encoder_t encoder
,
165 struct cgraph_node
*node
)
167 pointer_set_insert (encoder
->body
, node
);
170 /* Create a new varpool encoder. */
172 lto_varpool_encoder_t
173 lto_varpool_encoder_new (void)
175 lto_varpool_encoder_t encoder
= XCNEW (struct lto_varpool_encoder_d
);
176 encoder
->map
= pointer_map_create ();
177 encoder
->initializer
= pointer_set_create ();
178 encoder
->nodes
= NULL
;
183 /* Delete ENCODER and its components. */
186 lto_varpool_encoder_delete (lto_varpool_encoder_t encoder
)
188 VEC_free (varpool_node_ptr
, heap
, encoder
->nodes
);
189 pointer_map_destroy (encoder
->map
);
190 pointer_set_destroy (encoder
->initializer
);
195 /* Return the existing reference number of NODE in the varpool encoder in
196 output block OB. Assign a new reference if this is the first time
200 lto_varpool_encoder_encode (lto_varpool_encoder_t encoder
,
201 struct varpool_node
*node
)
206 slot
= pointer_map_contains (encoder
->map
, node
);
209 ref
= VEC_length (varpool_node_ptr
, encoder
->nodes
);
210 slot
= pointer_map_insert (encoder
->map
, node
);
211 *slot
= (void *) (intptr_t) ref
;
212 VEC_safe_push (varpool_node_ptr
, heap
, encoder
->nodes
, node
);
215 ref
= (int) (intptr_t) *slot
;
220 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
221 or LCC_NOT_FOUND if it is not there. */
224 lto_varpool_encoder_lookup (lto_varpool_encoder_t encoder
,
225 struct varpool_node
*node
)
227 void **slot
= pointer_map_contains (encoder
->map
, node
);
228 return (slot
? (int) (intptr_t) *slot
: LCC_NOT_FOUND
);
232 /* Return the varpool node corresponding to REF using ENCODER. */
234 struct varpool_node
*
235 lto_varpool_encoder_deref (lto_varpool_encoder_t encoder
, int ref
)
237 if (ref
== LCC_NOT_FOUND
)
240 return VEC_index (varpool_node_ptr
, encoder
->nodes
, ref
);
244 /* Return TRUE if we should encode initializer of NODE (if any). */
247 lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t encoder
,
248 struct varpool_node
*node
)
250 return pointer_set_contains (encoder
->initializer
, node
);
253 /* Return TRUE if we should encode initializer of NODE (if any). */
256 lto_set_varpool_encoder_encode_initializer (lto_varpool_encoder_t encoder
,
257 struct varpool_node
*node
)
259 pointer_set_insert (encoder
->initializer
, node
);
262 /* Output the cgraph EDGE to OB using ENCODER. */
265 lto_output_edge (struct lto_simple_output_block
*ob
, struct cgraph_edge
*edge
,
266 lto_cgraph_encoder_t encoder
)
272 if (edge
->indirect_unknown_callee
)
273 streamer_write_enum (ob
->main_stream
, LTO_cgraph_tags
, LTO_cgraph_last_tag
,
274 LTO_cgraph_indirect_edge
);
276 streamer_write_enum (ob
->main_stream
, LTO_cgraph_tags
, LTO_cgraph_last_tag
,
279 ref
= lto_cgraph_encoder_lookup (encoder
, edge
->caller
);
280 gcc_assert (ref
!= LCC_NOT_FOUND
);
281 streamer_write_hwi_stream (ob
->main_stream
, ref
);
283 if (!edge
->indirect_unknown_callee
)
285 ref
= lto_cgraph_encoder_lookup (encoder
, edge
->callee
);
286 gcc_assert (ref
!= LCC_NOT_FOUND
);
287 streamer_write_hwi_stream (ob
->main_stream
, ref
);
290 streamer_write_hwi_stream (ob
->main_stream
, edge
->count
);
292 bp
= bitpack_create (ob
->main_stream
);
293 uid
= (!gimple_has_body_p (edge
->caller
->symbol
.decl
)
294 ? edge
->lto_stmt_uid
: gimple_uid (edge
->call_stmt
));
295 bp_pack_enum (&bp
, cgraph_inline_failed_enum
,
296 CIF_N_REASONS
, edge
->inline_failed
);
297 bp_pack_var_len_unsigned (&bp
, uid
);
298 bp_pack_var_len_unsigned (&bp
, edge
->frequency
);
299 bp_pack_value (&bp
, edge
->indirect_inlining_edge
, 1);
300 bp_pack_value (&bp
, edge
->call_stmt_cannot_inline_p
, 1);
301 bp_pack_value (&bp
, edge
->can_throw_external
, 1);
302 if (edge
->indirect_unknown_callee
)
304 int flags
= edge
->indirect_info
->ecf_flags
;
305 bp_pack_value (&bp
, (flags
& ECF_CONST
) != 0, 1);
306 bp_pack_value (&bp
, (flags
& ECF_PURE
) != 0, 1);
307 bp_pack_value (&bp
, (flags
& ECF_NORETURN
) != 0, 1);
308 bp_pack_value (&bp
, (flags
& ECF_MALLOC
) != 0, 1);
309 bp_pack_value (&bp
, (flags
& ECF_NOTHROW
) != 0, 1);
310 bp_pack_value (&bp
, (flags
& ECF_RETURNS_TWICE
) != 0, 1);
311 /* Flags that should not appear on indirect calls. */
312 gcc_assert (!(flags
& (ECF_LOOPING_CONST_OR_PURE
318 streamer_write_bitpack (&bp
);
321 /* Return if LIST contain references from other partitions. */
324 referenced_from_other_partition_p (struct ipa_ref_list
*list
, cgraph_node_set set
,
325 varpool_node_set vset
)
329 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
331 if (symtab_function_p (ref
->referring
))
333 if (ipa_ref_referring_node (ref
)->symbol
.in_other_partition
334 || !cgraph_node_in_set_p (ipa_ref_referring_node (ref
), set
))
339 if (ipa_ref_referring_varpool_node (ref
)->symbol
.in_other_partition
340 || !varpool_node_in_set_p (ipa_ref_referring_varpool_node (ref
),
348 /* Return true when node is reachable from other partition. */
351 reachable_from_other_partition_p (struct cgraph_node
*node
, cgraph_node_set set
)
353 struct cgraph_edge
*e
;
356 if (node
->global
.inlined_to
)
358 for (e
= node
->callers
; e
; e
= e
->next_caller
)
359 if (e
->caller
->symbol
.in_other_partition
360 || !cgraph_node_in_set_p (e
->caller
, set
))
365 /* Return if LIST contain references from other partitions. */
368 referenced_from_this_partition_p (struct ipa_ref_list
*list
, cgraph_node_set set
,
369 varpool_node_set vset
)
373 for (i
= 0; ipa_ref_list_referring_iterate (list
, i
, ref
); i
++)
375 if (symtab_function_p (ref
->referring
))
377 if (cgraph_node_in_set_p (ipa_ref_referring_node (ref
), set
))
382 if (varpool_node_in_set_p (ipa_ref_referring_varpool_node (ref
),
390 /* Return true when node is reachable from other partition. */
393 reachable_from_this_partition_p (struct cgraph_node
*node
, cgraph_node_set set
)
395 struct cgraph_edge
*e
;
396 for (e
= node
->callers
; e
; e
= e
->next_caller
)
397 if (cgraph_node_in_set_p (e
->caller
, set
))
402 /* Output the cgraph NODE to OB. ENCODER is used to find the
403 reference number of NODE->inlined_to. SET is the set of nodes we
404 are writing to the current file. If NODE is not in SET, then NODE
405 is a boundary of a cgraph_node_set and we pretend NODE just has a
406 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
407 that have had their callgraph node written so far. This is used to
408 determine if NODE is a clone of a previously written node. */
411 lto_output_node (struct lto_simple_output_block
*ob
, struct cgraph_node
*node
,
412 lto_cgraph_encoder_t encoder
, cgraph_node_set set
,
413 varpool_node_set vset
)
419 bool in_other_partition
= false;
420 struct cgraph_node
*clone_of
;
422 boundary_p
= !cgraph_node_in_set_p (node
, set
);
424 if (node
->analyzed
&& !boundary_p
)
425 tag
= LTO_cgraph_analyzed_node
;
427 tag
= LTO_cgraph_unavail_node
;
429 streamer_write_enum (ob
->main_stream
, LTO_cgraph_tags
, LTO_cgraph_last_tag
,
431 streamer_write_hwi_stream (ob
->main_stream
, node
->symbol
.order
);
433 /* In WPA mode, we only output part of the call-graph. Also, we
434 fake cgraph node attributes. There are two cases that we care.
436 Boundary nodes: There are nodes that are not part of SET but are
437 called from within SET. We artificially make them look like
438 externally visible nodes with no function body.
440 Cherry-picked nodes: These are nodes we pulled from other
441 translation units into SET during IPA-inlining. We make them as
442 local static nodes to prevent clashes with other local statics. */
443 if (boundary_p
&& node
->analyzed
&& !DECL_EXTERNAL (node
->symbol
.decl
))
445 /* Inline clones can not be part of boundary.
446 gcc_assert (!node->global.inlined_to);
448 FIXME: At the moment they can be, when partition contains an inline
449 clone that is clone of inline clone from outside partition. We can
450 reshape the clone tree and make other tree to be the root, but it
451 needs a bit extra work and will be promplty done by cgraph_remove_node
452 after reading back. */
453 in_other_partition
= 1;
456 clone_of
= node
->clone_of
;
458 && (ref
= lto_cgraph_encoder_lookup (encoder
, clone_of
)) == LCC_NOT_FOUND
)
459 if (clone_of
->prev_sibling_clone
)
460 clone_of
= clone_of
->prev_sibling_clone
;
462 clone_of
= clone_of
->clone_of
;
464 if (LTO_cgraph_analyzed_node
)
465 gcc_assert (clone_of
|| !node
->clone_of
);
467 streamer_write_hwi_stream (ob
->main_stream
, LCC_NOT_FOUND
);
469 streamer_write_hwi_stream (ob
->main_stream
, ref
);
472 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
, node
->symbol
.decl
);
473 streamer_write_hwi_stream (ob
->main_stream
, node
->count
);
474 streamer_write_hwi_stream (ob
->main_stream
, node
->count_materialization_scale
);
476 if (tag
== LTO_cgraph_analyzed_node
)
478 if (node
->global
.inlined_to
)
480 ref
= lto_cgraph_encoder_lookup (encoder
, node
->global
.inlined_to
);
481 gcc_assert (ref
!= LCC_NOT_FOUND
);
486 streamer_write_hwi_stream (ob
->main_stream
, ref
);
489 if (node
->symbol
.same_comdat_group
&& !boundary_p
)
491 ref
= lto_cgraph_encoder_lookup (encoder
,
492 cgraph (node
->symbol
.same_comdat_group
));
493 gcc_assert (ref
!= LCC_NOT_FOUND
);
497 streamer_write_hwi_stream (ob
->main_stream
, ref
);
499 bp
= bitpack_create (ob
->main_stream
);
500 bp_pack_value (&bp
, node
->local
.local
, 1);
501 bp_pack_value (&bp
, node
->symbol
.externally_visible
, 1);
502 bp_pack_value (&bp
, node
->local
.finalized
, 1);
503 bp_pack_value (&bp
, node
->local
.versionable
, 1);
504 bp_pack_value (&bp
, node
->local
.can_change_signature
, 1);
505 bp_pack_value (&bp
, node
->local
.redefined_extern_inline
, 1);
506 bp_pack_value (&bp
, node
->symbol
.force_output
, 1);
507 bp_pack_value (&bp
, node
->symbol
.address_taken
, 1);
508 bp_pack_value (&bp
, node
->abstract_and_needed
, 1);
509 bp_pack_value (&bp
, tag
== LTO_cgraph_analyzed_node
510 && !DECL_EXTERNAL (node
->symbol
.decl
)
511 && !DECL_COMDAT (node
->symbol
.decl
)
512 && (reachable_from_other_partition_p (node
, set
)
513 || referenced_from_other_partition_p (&node
->symbol
.ref_list
,
515 bp_pack_value (&bp
, node
->lowered
, 1);
516 bp_pack_value (&bp
, in_other_partition
, 1);
517 /* Real aliases in a boundary become non-aliases. However we still stream
518 alias info on weakrefs.
519 TODO: We lose a bit of information here - when we know that variable is
520 defined in other unit, we may use the info on aliases to resolve
521 symbol1 != symbol2 type tests that we can do only for locally defined objects
523 bp_pack_value (&bp
, node
->alias
&& (!boundary_p
|| DECL_EXTERNAL (node
->symbol
.decl
)), 1);
524 bp_pack_value (&bp
, node
->frequency
, 2);
525 bp_pack_value (&bp
, node
->only_called_at_startup
, 1);
526 bp_pack_value (&bp
, node
->only_called_at_exit
, 1);
527 bp_pack_value (&bp
, node
->tm_clone
, 1);
528 bp_pack_value (&bp
, node
->thunk
.thunk_p
&& !boundary_p
, 1);
529 bp_pack_enum (&bp
, ld_plugin_symbol_resolution
,
530 LDPR_NUM_KNOWN
, node
->symbol
.resolution
);
531 streamer_write_bitpack (&bp
);
533 if (node
->thunk
.thunk_p
&& !boundary_p
)
535 streamer_write_uhwi_stream
537 1 + (node
->thunk
.this_adjusting
!= 0) * 2
538 + (node
->thunk
.virtual_offset_p
!= 0) * 4);
539 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.fixed_offset
);
540 streamer_write_uhwi_stream (ob
->main_stream
, node
->thunk
.virtual_value
);
542 if ((node
->alias
|| node
->thunk
.thunk_p
)
543 && (!boundary_p
|| (node
->alias
&& DECL_EXTERNAL (node
->symbol
.decl
))))
545 streamer_write_hwi_in_range (ob
->main_stream
, 0, 1,
546 node
->thunk
.alias
!= NULL
);
547 if (node
->thunk
.alias
!= NULL
)
548 lto_output_fn_decl_index (ob
->decl_state
, ob
->main_stream
,
553 /* Output the varpool NODE to OB.
554 If NODE is not in SET, then NODE is a boundary. */
557 lto_output_varpool_node (struct lto_simple_output_block
*ob
, struct varpool_node
*node
,
558 lto_varpool_encoder_t varpool_encoder
,
559 cgraph_node_set set
, varpool_node_set vset
)
561 bool boundary_p
= !varpool_node_in_set_p (node
, vset
) && node
->analyzed
;
565 streamer_write_hwi_stream (ob
->main_stream
, node
->symbol
.order
);
566 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->symbol
.decl
);
567 bp
= bitpack_create (ob
->main_stream
);
568 bp_pack_value (&bp
, node
->symbol
.externally_visible
, 1);
569 bp_pack_value (&bp
, node
->symbol
.force_output
, 1);
570 bp_pack_value (&bp
, node
->finalized
, 1);
571 bp_pack_value (&bp
, node
->alias
, 1);
572 bp_pack_value (&bp
, node
->alias_of
!= NULL
, 1);
573 gcc_assert (node
->finalized
|| !node
->analyzed
);
574 /* Constant pool initializers can be de-unified into individual ltrans units.
575 FIXME: Alternatively at -Os we may want to avoid generating for them the local
576 labels and share them across LTRANS partitions. */
577 if (DECL_IN_CONSTANT_POOL (node
->symbol
.decl
)
578 && !DECL_EXTERNAL (node
->symbol
.decl
)
579 && !DECL_COMDAT (node
->symbol
.decl
))
581 bp_pack_value (&bp
, 0, 1); /* used_from_other_parition. */
582 bp_pack_value (&bp
, 0, 1); /* in_other_partition. */
586 bp_pack_value (&bp
, node
->analyzed
587 && referenced_from_other_partition_p (&node
->symbol
.ref_list
,
589 bp_pack_value (&bp
, boundary_p
&& !DECL_EXTERNAL (node
->symbol
.decl
), 1);
590 /* in_other_partition. */
592 streamer_write_bitpack (&bp
);
594 lto_output_var_decl_index (ob
->decl_state
, ob
->main_stream
, node
->alias_of
);
595 if (node
->symbol
.same_comdat_group
&& !boundary_p
)
597 ref
= lto_varpool_encoder_lookup (varpool_encoder
,
598 varpool (node
->symbol
.same_comdat_group
));
599 gcc_assert (ref
!= LCC_NOT_FOUND
);
603 streamer_write_hwi_stream (ob
->main_stream
, ref
);
604 streamer_write_enum (ob
->main_stream
, ld_plugin_symbol_resolution
,
605 LDPR_NUM_KNOWN
, node
->symbol
.resolution
);
608 /* Output the varpool NODE to OB.
609 If NODE is not in SET, then NODE is a boundary. */
612 lto_output_ref (struct lto_simple_output_block
*ob
, struct ipa_ref
*ref
,
613 lto_cgraph_encoder_t encoder
,
614 lto_varpool_encoder_t varpool_encoder
)
617 bp
= bitpack_create (ob
->main_stream
);
618 bp_pack_value (&bp
, symtab_function_p (ref
->referred
), 1);
619 bp_pack_value (&bp
, ref
->use
, 2);
620 streamer_write_bitpack (&bp
);
621 if (symtab_function_p (ref
->referred
))
623 int nref
= lto_cgraph_encoder_lookup (encoder
, ipa_ref_node (ref
));
624 gcc_assert (nref
!= LCC_NOT_FOUND
);
625 streamer_write_hwi_stream (ob
->main_stream
, nref
);
629 int nref
= lto_varpool_encoder_lookup (varpool_encoder
,
630 ipa_ref_varpool_node (ref
));
631 gcc_assert (nref
!= LCC_NOT_FOUND
);
632 streamer_write_hwi_stream (ob
->main_stream
, nref
);
636 /* Stream out profile_summary to OB. */
639 output_profile_summary (struct lto_simple_output_block
*ob
)
643 /* We do not output num, sum_all and run_max, they are not used by
644 GCC profile feedback and they are difficult to merge from multiple
646 gcc_assert (profile_info
->runs
);
647 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->runs
);
648 streamer_write_uhwi_stream (ob
->main_stream
, profile_info
->sum_max
);
651 streamer_write_uhwi_stream (ob
->main_stream
, 0);
654 /* Add NODE into encoder as well as nodes it is cloned from.
655 Do it in a way so clones appear first. */
658 add_node_to (lto_cgraph_encoder_t encoder
, struct cgraph_node
*node
,
662 add_node_to (encoder
, node
->clone_of
, include_body
);
663 else if (include_body
)
664 lto_set_cgraph_encoder_encode_body (encoder
, node
);
665 lto_cgraph_encoder_encode (encoder
, node
);
668 /* Add all references in LIST to encoders. */
671 add_references (lto_cgraph_encoder_t encoder
,
672 lto_varpool_encoder_t varpool_encoder
,
673 struct ipa_ref_list
*list
)
677 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
678 if (symtab_function_p (ref
->referred
))
679 add_node_to (encoder
, ipa_ref_node (ref
), false);
682 struct varpool_node
*vnode
= ipa_ref_varpool_node (ref
);
683 lto_varpool_encoder_encode (varpool_encoder
, vnode
);
687 /* Output all callees or indirect outgoing edges. EDGE must be the first such
691 output_outgoing_cgraph_edges (struct cgraph_edge
*edge
,
692 struct lto_simple_output_block
*ob
,
693 lto_cgraph_encoder_t encoder
)
698 /* Output edges in backward direction, so the reconstructed callgraph match
699 and it is easy to associate call sites in the IPA pass summaries. */
700 while (edge
->next_callee
)
701 edge
= edge
->next_callee
;
702 for (; edge
; edge
= edge
->prev_callee
)
703 lto_output_edge (ob
, edge
, encoder
);
706 /* Output the part of the cgraph in SET. */
709 output_refs (cgraph_node_set set
, varpool_node_set vset
,
710 lto_cgraph_encoder_t encoder
,
711 lto_varpool_encoder_t varpool_encoder
)
713 cgraph_node_set_iterator csi
;
714 varpool_node_set_iterator vsi
;
715 struct lto_simple_output_block
*ob
;
720 ob
= lto_create_simple_output_block (LTO_section_refs
);
722 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
724 struct cgraph_node
*node
= csi_node (csi
);
726 count
= ipa_ref_list_nreferences (&node
->symbol
.ref_list
);
729 streamer_write_uhwi_stream (ob
->main_stream
, count
);
730 streamer_write_uhwi_stream (ob
->main_stream
,
731 lto_cgraph_encoder_lookup (encoder
, node
));
732 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
,
734 lto_output_ref (ob
, ref
, encoder
, varpool_encoder
);
738 streamer_write_uhwi_stream (ob
->main_stream
, 0);
740 for (vsi
= vsi_start (vset
); !vsi_end_p (vsi
); vsi_next (&vsi
))
742 struct varpool_node
*node
= vsi_node (vsi
);
744 count
= ipa_ref_list_nreferences (&node
->symbol
.ref_list
);
747 streamer_write_uhwi_stream (ob
->main_stream
, count
);
748 streamer_write_uhwi_stream (ob
->main_stream
,
749 lto_varpool_encoder_lookup (varpool_encoder
,
751 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
,
753 lto_output_ref (ob
, ref
, encoder
, varpool_encoder
);
757 streamer_write_uhwi_stream (ob
->main_stream
, 0);
759 lto_destroy_simple_output_block (ob
);
762 /* Find out all cgraph and varpool nodes we want to encode in current unit
763 and insert them to encoders. */
765 compute_ltrans_boundary (struct lto_out_decl_state
*state
,
766 cgraph_node_set set
, varpool_node_set vset
)
768 struct cgraph_node
*node
;
769 cgraph_node_set_iterator csi
;
770 varpool_node_set_iterator vsi
;
771 struct cgraph_edge
*edge
;
773 lto_cgraph_encoder_t encoder
;
774 lto_varpool_encoder_t varpool_encoder
;
776 encoder
= state
->cgraph_node_encoder
= lto_cgraph_encoder_new ();
777 varpool_encoder
= state
->varpool_node_encoder
= lto_varpool_encoder_new ();
779 /* Go over all the nodes in SET and assign references. */
780 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
782 node
= csi_node (csi
);
783 add_node_to (encoder
, node
, true);
784 add_references (encoder
, varpool_encoder
, &node
->symbol
.ref_list
);
786 for (vsi
= vsi_start (vset
); !vsi_end_p (vsi
); vsi_next (&vsi
))
788 struct varpool_node
*vnode
= vsi_node (vsi
);
789 gcc_assert (!vnode
->alias
|| vnode
->alias_of
);
790 lto_varpool_encoder_encode (varpool_encoder
, vnode
);
791 lto_set_varpool_encoder_encode_initializer (varpool_encoder
, vnode
);
792 add_references (encoder
, varpool_encoder
, &vnode
->symbol
.ref_list
);
794 /* Pickle in also the initializer of all referenced readonly variables
795 to help folding. Constant pool variables are not shared, so we must
797 for (i
= 0; i
< lto_varpool_encoder_size (varpool_encoder
); i
++)
799 struct varpool_node
*vnode
= lto_varpool_encoder_deref (varpool_encoder
, i
);
800 if (DECL_INITIAL (vnode
->symbol
.decl
)
801 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder
,
803 && const_value_known_p (vnode
->symbol
.decl
))
805 lto_set_varpool_encoder_encode_initializer (varpool_encoder
, vnode
);
806 add_references (encoder
, varpool_encoder
, &vnode
->symbol
.ref_list
);
808 else if (vnode
->alias
|| vnode
->alias_of
)
809 add_references (encoder
, varpool_encoder
, &vnode
->symbol
.ref_list
);
812 /* Go over all the nodes again to include callees that are not in
814 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
816 node
= csi_node (csi
);
817 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
819 struct cgraph_node
*callee
= edge
->callee
;
820 if (!cgraph_node_in_set_p (callee
, set
))
822 /* We should have moved all the inlines. */
823 gcc_assert (!callee
->global
.inlined_to
);
824 add_node_to (encoder
, callee
, false);
830 /* Output the part of the cgraph in SET. */
833 output_cgraph (cgraph_node_set set
, varpool_node_set vset
)
835 struct cgraph_node
*node
;
836 struct lto_simple_output_block
*ob
;
837 cgraph_node_set_iterator csi
;
839 lto_cgraph_encoder_t encoder
;
840 lto_varpool_encoder_t varpool_encoder
;
841 static bool asm_nodes_output
= false;
844 output_cgraph_opt_summary (set
);
846 ob
= lto_create_simple_output_block (LTO_section_cgraph
);
848 output_profile_summary (ob
);
850 /* An encoder for cgraph nodes should have been created by
851 ipa_write_summaries_1. */
852 gcc_assert (ob
->decl_state
->cgraph_node_encoder
);
853 gcc_assert (ob
->decl_state
->varpool_node_encoder
);
854 encoder
= ob
->decl_state
->cgraph_node_encoder
;
855 varpool_encoder
= ob
->decl_state
->varpool_node_encoder
;
857 /* Write out the nodes. We must first output a node and then its clones,
858 otherwise at a time reading back the node there would be nothing to clone
860 n_nodes
= lto_cgraph_encoder_size (encoder
);
861 for (i
= 0; i
< n_nodes
; i
++)
863 node
= lto_cgraph_encoder_deref (encoder
, i
);
864 lto_output_node (ob
, node
, encoder
, set
, vset
);
867 /* Go over the nodes in SET again to write edges. */
868 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
870 node
= csi_node (csi
);
871 output_outgoing_cgraph_edges (node
->callees
, ob
, encoder
);
872 output_outgoing_cgraph_edges (node
->indirect_calls
, ob
, encoder
);
875 streamer_write_uhwi_stream (ob
->main_stream
, 0);
877 lto_destroy_simple_output_block (ob
);
879 /* Emit toplevel asms.
880 When doing WPA we must output every asm just once. Since we do not partition asm
881 nodes at all, output them to first output. This is kind of hack, but should work
883 if (!asm_nodes_output
)
885 asm_nodes_output
= true;
886 lto_output_toplevel_asms ();
889 output_varpool (set
, vset
);
890 output_refs (set
, vset
, encoder
, varpool_encoder
);
893 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
894 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
895 NODE or to replace the values in it, for instance because the first
896 time we saw it, the function body was not available but now it
897 is. BP is a bitpack with all the bitflags for NODE read from the
901 input_overwrite_node (struct lto_file_decl_data
*file_data
,
902 struct cgraph_node
*node
,
903 enum LTO_cgraph_tags tag
,
904 struct bitpack_d
*bp
)
906 node
->symbol
.aux
= (void *) tag
;
907 node
->symbol
.lto_file_data
= file_data
;
909 node
->local
.local
= bp_unpack_value (bp
, 1);
910 node
->symbol
.externally_visible
= bp_unpack_value (bp
, 1);
911 node
->local
.finalized
= bp_unpack_value (bp
, 1);
912 node
->local
.versionable
= bp_unpack_value (bp
, 1);
913 node
->local
.can_change_signature
= bp_unpack_value (bp
, 1);
914 node
->local
.redefined_extern_inline
= bp_unpack_value (bp
, 1);
915 node
->symbol
.force_output
= bp_unpack_value (bp
, 1);
916 node
->symbol
.address_taken
= bp_unpack_value (bp
, 1);
917 node
->abstract_and_needed
= bp_unpack_value (bp
, 1);
918 node
->symbol
.used_from_other_partition
= bp_unpack_value (bp
, 1);
919 node
->lowered
= bp_unpack_value (bp
, 1);
920 node
->analyzed
= tag
== LTO_cgraph_analyzed_node
;
921 node
->symbol
.in_other_partition
= bp_unpack_value (bp
, 1);
922 if (node
->symbol
.in_other_partition
923 /* Avoid updating decl when we are seeing just inline clone.
924 When inlining function that has functions already inlined into it,
925 we produce clones of inline clones.
927 WPA partitioning might put each clone into different unit and
928 we might end up streaming inline clone from other partition
929 to support clone we are interested in. */
931 || node
->clone_of
->symbol
.decl
!= node
->symbol
.decl
))
933 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
934 TREE_STATIC (node
->symbol
.decl
) = 0;
936 node
->alias
= bp_unpack_value (bp
, 1);
937 node
->frequency
= (enum node_frequency
)bp_unpack_value (bp
, 2);
938 node
->only_called_at_startup
= bp_unpack_value (bp
, 1);
939 node
->only_called_at_exit
= bp_unpack_value (bp
, 1);
940 node
->tm_clone
= bp_unpack_value (bp
, 1);
941 node
->thunk
.thunk_p
= bp_unpack_value (bp
, 1);
942 node
->symbol
.resolution
= bp_unpack_enum (bp
, ld_plugin_symbol_resolution
,
946 /* Output the part of the cgraph in SET. */
949 output_varpool (cgraph_node_set set
, varpool_node_set vset
)
951 struct lto_simple_output_block
*ob
= lto_create_simple_output_block (LTO_section_varpool
);
952 lto_varpool_encoder_t varpool_encoder
= ob
->decl_state
->varpool_node_encoder
;
953 int len
= lto_varpool_encoder_size (varpool_encoder
), i
;
955 streamer_write_uhwi_stream (ob
->main_stream
, len
);
957 /* Write out the nodes. We must first output a node and then its clones,
958 otherwise at a time reading back the node there would be nothing to clone
960 for (i
= 0; i
< len
; i
++)
962 lto_output_varpool_node (ob
, lto_varpool_encoder_deref (varpool_encoder
, i
),
967 lto_destroy_simple_output_block (ob
);
970 /* Read a node from input_block IB. TAG is the node's tag just read.
971 Return the node read or overwriten. */
973 static struct cgraph_node
*
974 input_node (struct lto_file_decl_data
*file_data
,
975 struct lto_input_block
*ib
,
976 enum LTO_cgraph_tags tag
,
977 VEC(cgraph_node_ptr
, heap
) *nodes
)
980 struct cgraph_node
*node
;
983 int ref
= LCC_NOT_FOUND
, ref2
= LCC_NOT_FOUND
;
987 order
= streamer_read_hwi (ib
) + order_base
;
988 clone_ref
= streamer_read_hwi (ib
);
990 decl_index
= streamer_read_uhwi (ib
);
991 fn_decl
= lto_file_decl_data_get_fn_decl (file_data
, decl_index
);
993 if (clone_ref
!= LCC_NOT_FOUND
)
995 node
= cgraph_clone_node (VEC_index (cgraph_node_ptr
, nodes
, clone_ref
), fn_decl
,
996 0, CGRAPH_FREQ_BASE
, false, NULL
, false);
999 node
= cgraph_get_create_node (fn_decl
);
1001 node
->symbol
.order
= order
;
1002 if (order
>= symtab_order
)
1003 symtab_order
= order
+ 1;
1005 node
->count
= streamer_read_hwi (ib
);
1006 node
->count_materialization_scale
= streamer_read_hwi (ib
);
1008 if (tag
== LTO_cgraph_analyzed_node
)
1009 ref
= streamer_read_hwi (ib
);
1011 ref2
= streamer_read_hwi (ib
);
1013 /* Make sure that we have not read this node before. Nodes that
1014 have already been read will have their tag stored in the 'aux'
1015 field. Since built-in functions can be referenced in multiple
1016 functions, they are expected to be read more than once. */
1017 if (node
->symbol
.aux
&& !DECL_BUILT_IN (node
->symbol
.decl
))
1018 internal_error ("bytecode stream: found multiple instances of cgraph "
1019 "node %d", node
->uid
);
1021 bp
= streamer_read_bitpack (ib
);
1022 input_overwrite_node (file_data
, node
, tag
, &bp
);
1024 /* Store a reference for now, and fix up later to be a pointer. */
1025 node
->global
.inlined_to
= (cgraph_node_ptr
) (intptr_t) ref
;
1027 /* Store a reference for now, and fix up later to be a pointer. */
1028 node
->symbol
.same_comdat_group
= (symtab_node
) (intptr_t) ref2
;
1030 if (node
->thunk
.thunk_p
)
1032 int type
= streamer_read_uhwi (ib
);
1033 HOST_WIDE_INT fixed_offset
= streamer_read_uhwi (ib
);
1034 HOST_WIDE_INT virtual_value
= streamer_read_uhwi (ib
);
1036 node
->thunk
.fixed_offset
= fixed_offset
;
1037 node
->thunk
.this_adjusting
= (type
& 2);
1038 node
->thunk
.virtual_value
= virtual_value
;
1039 node
->thunk
.virtual_offset_p
= (type
& 4);
1041 if (node
->thunk
.thunk_p
|| node
->alias
)
1043 if (streamer_read_hwi_in_range (ib
, "alias nonzero flag", 0, 1))
1045 decl_index
= streamer_read_uhwi (ib
);
1046 node
->thunk
.alias
= lto_file_decl_data_get_fn_decl (file_data
,
1053 /* Read a node from input_block IB. TAG is the node's tag just read.
1054 Return the node read or overwriten. */
1056 static struct varpool_node
*
1057 input_varpool_node (struct lto_file_decl_data
*file_data
,
1058 struct lto_input_block
*ib
)
1062 struct varpool_node
*node
;
1063 struct bitpack_d bp
;
1064 int ref
= LCC_NOT_FOUND
;
1065 bool non_null_aliasof
;
1068 order
= streamer_read_hwi (ib
) + order_base
;
1069 decl_index
= streamer_read_uhwi (ib
);
1070 var_decl
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1071 node
= varpool_node (var_decl
);
1072 node
->symbol
.order
= order
;
1073 if (order
>= symtab_order
)
1074 symtab_order
= order
+ 1;
1075 node
->symbol
.lto_file_data
= file_data
;
1077 bp
= streamer_read_bitpack (ib
);
1078 node
->symbol
.externally_visible
= bp_unpack_value (&bp
, 1);
1079 node
->symbol
.force_output
= bp_unpack_value (&bp
, 1);
1080 node
->finalized
= bp_unpack_value (&bp
, 1);
1081 node
->alias
= bp_unpack_value (&bp
, 1);
1082 non_null_aliasof
= bp_unpack_value (&bp
, 1);
1083 node
->symbol
.used_from_other_partition
= bp_unpack_value (&bp
, 1);
1084 node
->symbol
.in_other_partition
= bp_unpack_value (&bp
, 1);
1085 node
->analyzed
= (node
->finalized
&& (!node
->alias
|| !node
->symbol
.in_other_partition
));
1086 if (node
->symbol
.in_other_partition
)
1088 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
1089 TREE_STATIC (node
->symbol
.decl
) = 0;
1091 if (non_null_aliasof
)
1093 decl_index
= streamer_read_uhwi (ib
);
1094 node
->alias_of
= lto_file_decl_data_get_var_decl (file_data
, decl_index
);
1096 ref
= streamer_read_hwi (ib
);
1097 /* Store a reference for now, and fix up later to be a pointer. */
1098 node
->symbol
.same_comdat_group
= (symtab_node
) (intptr_t) ref
;
1099 node
->symbol
.resolution
= streamer_read_enum (ib
, ld_plugin_symbol_resolution
,
1105 /* Read a node from input_block IB. TAG is the node's tag just read.
1106 Return the node read or overwriten. */
1109 input_ref (struct lto_input_block
*ib
,
1110 symtab_node referring_node
,
1111 VEC(cgraph_node_ptr
, heap
) *nodes
,
1112 VEC(varpool_node_ptr
, heap
) *varpool_nodes_vec
)
1114 struct cgraph_node
*node
= NULL
;
1115 struct varpool_node
*varpool_node
= NULL
;
1116 struct bitpack_d bp
;
1118 enum ipa_ref_use use
;
1120 bp
= streamer_read_bitpack (ib
);
1121 type
= bp_unpack_value (&bp
, 1);
1122 use
= (enum ipa_ref_use
) bp_unpack_value (&bp
, 2);
1124 node
= VEC_index (cgraph_node_ptr
, nodes
, streamer_read_hwi (ib
));
1126 varpool_node
= VEC_index (varpool_node_ptr
, varpool_nodes_vec
,
1127 streamer_read_hwi (ib
));
1128 ipa_record_reference (referring_node
,
1129 node
? (symtab_node
) node
: (symtab_node
) varpool_node
, use
, NULL
);
1132 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1133 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1134 edge being read is indirect (in the sense that it has
1135 indirect_unknown_callee set). */
1138 input_edge (struct lto_input_block
*ib
, VEC(cgraph_node_ptr
, heap
) *nodes
,
1141 struct cgraph_node
*caller
, *callee
;
1142 struct cgraph_edge
*edge
;
1143 unsigned int stmt_id
;
1146 cgraph_inline_failed_t inline_failed
;
1147 struct bitpack_d bp
;
1150 caller
= VEC_index (cgraph_node_ptr
, nodes
, streamer_read_hwi (ib
));
1151 if (caller
== NULL
|| caller
->symbol
.decl
== NULL_TREE
)
1152 internal_error ("bytecode stream: no caller found while reading edge");
1156 callee
= VEC_index (cgraph_node_ptr
, nodes
, streamer_read_hwi (ib
));
1157 if (callee
== NULL
|| callee
->symbol
.decl
== NULL_TREE
)
1158 internal_error ("bytecode stream: no callee found while reading edge");
1163 count
= (gcov_type
) streamer_read_hwi (ib
);
1165 bp
= streamer_read_bitpack (ib
);
1166 inline_failed
= bp_unpack_enum (&bp
, cgraph_inline_failed_enum
, CIF_N_REASONS
);
1167 stmt_id
= bp_unpack_var_len_unsigned (&bp
);
1168 freq
= (int) bp_unpack_var_len_unsigned (&bp
);
1171 edge
= cgraph_create_indirect_edge (caller
, NULL
, 0, count
, freq
);
1173 edge
= cgraph_create_edge (caller
, callee
, NULL
, count
, freq
);
1175 edge
->indirect_inlining_edge
= bp_unpack_value (&bp
, 1);
1176 edge
->lto_stmt_uid
= stmt_id
;
1177 edge
->inline_failed
= inline_failed
;
1178 edge
->call_stmt_cannot_inline_p
= bp_unpack_value (&bp
, 1);
1179 edge
->can_throw_external
= bp_unpack_value (&bp
, 1);
1182 if (bp_unpack_value (&bp
, 1))
1183 ecf_flags
|= ECF_CONST
;
1184 if (bp_unpack_value (&bp
, 1))
1185 ecf_flags
|= ECF_PURE
;
1186 if (bp_unpack_value (&bp
, 1))
1187 ecf_flags
|= ECF_NORETURN
;
1188 if (bp_unpack_value (&bp
, 1))
1189 ecf_flags
|= ECF_MALLOC
;
1190 if (bp_unpack_value (&bp
, 1))
1191 ecf_flags
|= ECF_NOTHROW
;
1192 if (bp_unpack_value (&bp
, 1))
1193 ecf_flags
|= ECF_RETURNS_TWICE
;
1194 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1199 /* Read a cgraph from IB using the info in FILE_DATA. */
1201 static VEC(cgraph_node_ptr
, heap
) *
1202 input_cgraph_1 (struct lto_file_decl_data
*file_data
,
1203 struct lto_input_block
*ib
)
1205 enum LTO_cgraph_tags tag
;
1206 VEC(cgraph_node_ptr
, heap
) *nodes
= NULL
;
1207 struct cgraph_node
*node
;
1210 tag
= streamer_read_enum (ib
, LTO_cgraph_tags
, LTO_cgraph_last_tag
);
1211 order_base
= symtab_order
;
1214 if (tag
== LTO_cgraph_edge
)
1215 input_edge (ib
, nodes
, false);
1216 else if (tag
== LTO_cgraph_indirect_edge
)
1217 input_edge (ib
, nodes
, true);
1220 node
= input_node (file_data
, ib
, tag
,nodes
);
1221 if (node
== NULL
|| node
->symbol
.decl
== NULL_TREE
)
1222 internal_error ("bytecode stream: found empty cgraph node");
1223 VEC_safe_push (cgraph_node_ptr
, heap
, nodes
, node
);
1224 lto_cgraph_encoder_encode (file_data
->cgraph_node_encoder
, node
);
1227 tag
= streamer_read_enum (ib
, LTO_cgraph_tags
, LTO_cgraph_last_tag
);
1230 lto_input_toplevel_asms (file_data
, order_base
);
1232 /* AUX pointers should be all non-zero for nodes read from the stream. */
1233 #ifdef ENABLE_CHECKING
1234 FOR_EACH_VEC_ELT (cgraph_node_ptr
, nodes
, i
, node
)
1235 gcc_assert (node
->symbol
.aux
);
1237 FOR_EACH_VEC_ELT (cgraph_node_ptr
, nodes
, i
, node
)
1239 int ref
= (int) (intptr_t) node
->global
.inlined_to
;
1241 /* We share declaration of builtins, so we may read same node twice. */
1242 if (!node
->symbol
.aux
)
1244 node
->symbol
.aux
= NULL
;
1246 /* Fixup inlined_to from reference to pointer. */
1247 if (ref
!= LCC_NOT_FOUND
)
1248 node
->global
.inlined_to
= VEC_index (cgraph_node_ptr
, nodes
, ref
);
1250 node
->global
.inlined_to
= NULL
;
1252 ref
= (int) (intptr_t) node
->symbol
.same_comdat_group
;
1254 /* Fixup same_comdat_group from reference to pointer. */
1255 if (ref
!= LCC_NOT_FOUND
)
1256 node
->symbol
.same_comdat_group
= (symtab_node
)VEC_index (cgraph_node_ptr
, nodes
, ref
);
1258 node
->symbol
.same_comdat_group
= NULL
;
1260 FOR_EACH_VEC_ELT (cgraph_node_ptr
, nodes
, i
, node
)
1261 node
->symbol
.aux
= (void *)1;
1265 /* Read a varpool from IB using the info in FILE_DATA. */
1267 static VEC(varpool_node_ptr
, heap
) *
1268 input_varpool_1 (struct lto_file_decl_data
*file_data
,
1269 struct lto_input_block
*ib
)
1271 unsigned HOST_WIDE_INT len
;
1272 VEC(varpool_node_ptr
, heap
) *varpool
= NULL
;
1274 struct varpool_node
*node
;
1276 len
= streamer_read_uhwi (ib
);
1279 VEC_safe_push (varpool_node_ptr
, heap
, varpool
,
1280 input_varpool_node (file_data
, ib
));
1283 #ifdef ENABLE_CHECKING
1284 FOR_EACH_VEC_ELT (varpool_node_ptr
, varpool
, i
, node
)
1285 gcc_assert (!node
->symbol
.aux
);
1287 FOR_EACH_VEC_ELT (varpool_node_ptr
, varpool
, i
, node
)
1289 int ref
= (int) (intptr_t) node
->symbol
.same_comdat_group
;
1290 /* We share declaration of builtins, so we may read same node twice. */
1291 if (node
->symbol
.aux
)
1293 node
->symbol
.aux
= (void *)1;
1295 /* Fixup same_comdat_group from reference to pointer. */
1296 if (ref
!= LCC_NOT_FOUND
)
1297 node
->symbol
.same_comdat_group
= (symtab_node
)VEC_index (varpool_node_ptr
, varpool
, ref
);
1299 node
->symbol
.same_comdat_group
= NULL
;
1301 FOR_EACH_VEC_ELT (varpool_node_ptr
, varpool
, i
, node
)
1302 node
->symbol
.aux
= NULL
;
1306 /* Input ipa_refs. */
1309 input_refs (struct lto_input_block
*ib
,
1310 VEC(cgraph_node_ptr
, heap
) *nodes
,
1311 VEC(varpool_node_ptr
, heap
) *varpool
)
1317 struct cgraph_node
*node
;
1318 count
= streamer_read_uhwi (ib
);
1321 idx
= streamer_read_uhwi (ib
);
1322 node
= VEC_index (cgraph_node_ptr
, nodes
, idx
);
1325 input_ref (ib
, (symtab_node
) node
, nodes
, varpool
);
1331 struct varpool_node
*node
;
1332 count
= streamer_read_uhwi (ib
);
1335 node
= VEC_index (varpool_node_ptr
, varpool
,
1336 streamer_read_uhwi (ib
));
1339 input_ref (ib
, (symtab_node
) node
, nodes
, varpool
);
1346 static struct gcov_ctr_summary lto_gcov_summary
;
1348 /* Input profile_info from IB. */
1350 input_profile_summary (struct lto_input_block
*ib
,
1351 struct lto_file_decl_data
*file_data
)
1353 unsigned int runs
= streamer_read_uhwi (ib
);
1356 file_data
->profile_info
.runs
= runs
;
1357 file_data
->profile_info
.sum_max
= streamer_read_uhwi (ib
);
1362 /* Rescale profile summaries to the same number of runs in the whole unit. */
1365 merge_profile_summaries (struct lto_file_decl_data
**file_data_vec
)
1367 struct lto_file_decl_data
*file_data
;
1369 gcov_unsigned_t max_runs
= 0;
1370 struct cgraph_node
*node
;
1371 struct cgraph_edge
*edge
;
1373 /* Find unit with maximal number of runs. If we ever get serious about
1374 roundoff errors, we might also consider computing smallest common
1376 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1377 if (max_runs
< file_data
->profile_info
.runs
)
1378 max_runs
= file_data
->profile_info
.runs
;
1383 /* Simple overflow check. We probably don't need to support that many train
1384 runs. Such a large value probably imply data corruption anyway. */
1385 if (max_runs
> INT_MAX
/ REG_BR_PROB_BASE
)
1387 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1388 INT_MAX
/ REG_BR_PROB_BASE
);
1392 profile_info
= <o_gcov_summary
;
1393 lto_gcov_summary
.runs
= max_runs
;
1394 lto_gcov_summary
.sum_max
= 0;
1396 /* Rescale all units to the maximal number of runs.
1397 sum_max can not be easily merged, as we have no idea what files come from
1398 the same run. We do not use the info anyway, so leave it 0. */
1399 for (j
= 0; (file_data
= file_data_vec
[j
]) != NULL
; j
++)
1400 if (file_data
->profile_info
.runs
)
1402 int scale
= ((REG_BR_PROB_BASE
* max_runs
1403 + file_data
->profile_info
.runs
/ 2)
1404 / file_data
->profile_info
.runs
);
1405 lto_gcov_summary
.sum_max
= MAX (lto_gcov_summary
.sum_max
,
1406 (file_data
->profile_info
.sum_max
1408 + REG_BR_PROB_BASE
/ 2)
1409 / REG_BR_PROB_BASE
);
1412 /* Watch roundoff errors. */
1413 if (lto_gcov_summary
.sum_max
< max_runs
)
1414 lto_gcov_summary
.sum_max
= max_runs
;
1416 /* If merging already happent at WPA time, we are done. */
1420 /* Now compute count_materialization_scale of each node.
1421 During LTRANS we already have values of count_materialization_scale
1422 computed, so just update them. */
1423 FOR_EACH_FUNCTION (node
)
1424 if (node
->symbol
.lto_file_data
1425 && node
->symbol
.lto_file_data
->profile_info
.runs
)
1430 ((node
->count_materialization_scale
* max_runs
1431 + node
->symbol
.lto_file_data
->profile_info
.runs
/ 2)
1432 / node
->symbol
.lto_file_data
->profile_info
.runs
);
1433 node
->count_materialization_scale
= scale
;
1435 fatal_error ("Profile information in %s corrupted",
1436 file_data
->file_name
);
1438 if (scale
== REG_BR_PROB_BASE
)
1440 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1441 edge
->count
= ((edge
->count
* scale
+ REG_BR_PROB_BASE
/ 2)
1442 / REG_BR_PROB_BASE
);
1443 node
->count
= ((node
->count
* scale
+ REG_BR_PROB_BASE
/ 2)
1444 / REG_BR_PROB_BASE
);
1448 /* Input and merge the cgraph from each of the .o files passed to
1454 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1455 struct lto_file_decl_data
*file_data
;
1457 struct cgraph_node
*node
;
1459 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
1461 while ((file_data
= file_data_vec
[j
++]))
1465 struct lto_input_block
*ib
;
1466 VEC(cgraph_node_ptr
, heap
) *nodes
;
1467 VEC(varpool_node_ptr
, heap
) *varpool
;
1469 ib
= lto_create_simple_input_block (file_data
, LTO_section_cgraph
,
1472 fatal_error ("cannot find LTO cgraph in %s", file_data
->file_name
);
1473 input_profile_summary (ib
, file_data
);
1474 file_data
->cgraph_node_encoder
= lto_cgraph_encoder_new ();
1475 nodes
= input_cgraph_1 (file_data
, ib
);
1476 lto_destroy_simple_input_block (file_data
, LTO_section_cgraph
,
1479 ib
= lto_create_simple_input_block (file_data
, LTO_section_varpool
,
1482 fatal_error ("cannot find LTO varpool in %s", file_data
->file_name
);
1483 varpool
= input_varpool_1 (file_data
, ib
);
1484 lto_destroy_simple_input_block (file_data
, LTO_section_varpool
,
1487 ib
= lto_create_simple_input_block (file_data
, LTO_section_refs
,
1490 fatal_error("cannot find LTO section refs in %s", file_data
->file_name
);
1491 input_refs (ib
, nodes
, varpool
);
1492 lto_destroy_simple_input_block (file_data
, LTO_section_refs
,
1495 input_cgraph_opt_summary (nodes
);
1496 VEC_free (cgraph_node_ptr
, heap
, nodes
);
1497 VEC_free (varpool_node_ptr
, heap
, varpool
);
1500 merge_profile_summaries (file_data_vec
);
1502 /* Clear out the aux field that was used to store enough state to
1503 tell which nodes should be overwritten. */
1504 FOR_EACH_FUNCTION (node
)
1506 /* Some nodes may have been created by cgraph_node. This
1507 happens when the callgraph contains nested functions. If the
1508 node for the parent function was never emitted to the gimple
1509 file, cgraph_node will create a node for it when setting the
1510 context of the nested function. */
1511 if (node
->symbol
.lto_file_data
)
1512 node
->symbol
.aux
= NULL
;
1516 /* True when we need optimization summary for NODE. */
1519 output_cgraph_opt_summary_p (struct cgraph_node
*node
,
1520 cgraph_node_set set ATTRIBUTE_UNUSED
)
1522 return (node
->clone_of
1523 && (node
->clone
.tree_map
1524 || node
->clone
.args_to_skip
1525 || node
->clone
.combined_args_to_skip
));
1528 /* Output optimization summary for EDGE to OB. */
1530 output_edge_opt_summary (struct output_block
*ob ATTRIBUTE_UNUSED
,
1531 struct cgraph_edge
*edge ATTRIBUTE_UNUSED
)
1535 /* Output optimization summary for NODE to OB. */
1538 output_node_opt_summary (struct output_block
*ob
,
1539 struct cgraph_node
*node
,
1540 cgraph_node_set set
)
1544 struct ipa_replace_map
*map
;
1545 struct bitpack_d bp
;
1547 struct cgraph_edge
*e
;
1549 if (node
->clone
.args_to_skip
)
1551 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.args_to_skip
));
1552 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.args_to_skip
, 0, index
, bi
)
1553 streamer_write_uhwi (ob
, index
);
1556 streamer_write_uhwi (ob
, 0);
1557 if (node
->clone
.combined_args_to_skip
)
1559 streamer_write_uhwi (ob
, bitmap_count_bits (node
->clone
.combined_args_to_skip
));
1560 EXECUTE_IF_SET_IN_BITMAP (node
->clone
.combined_args_to_skip
, 0, index
, bi
)
1561 streamer_write_uhwi (ob
, index
);
1564 streamer_write_uhwi (ob
, 0);
1565 streamer_write_uhwi (ob
, VEC_length (ipa_replace_map_p
,
1566 node
->clone
.tree_map
));
1567 FOR_EACH_VEC_ELT (ipa_replace_map_p
, node
->clone
.tree_map
, i
, map
)
1572 for (parm_num
= 0, parm
= DECL_ARGUMENTS (node
->symbol
.decl
); parm
;
1573 parm
= DECL_CHAIN (parm
), parm_num
++)
1574 if (map
->old_tree
== parm
)
1576 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1577 mechanism to store function local declarations into summaries. */
1579 streamer_write_uhwi (ob
, parm_num
);
1580 stream_write_tree (ob
, map
->new_tree
, true);
1581 bp
= bitpack_create (ob
->main_stream
);
1582 bp_pack_value (&bp
, map
->replace_p
, 1);
1583 bp_pack_value (&bp
, map
->ref_p
, 1);
1584 streamer_write_bitpack (&bp
);
1587 if (cgraph_node_in_set_p (node
, set
))
1589 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1590 output_edge_opt_summary (ob
, e
);
1591 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1592 output_edge_opt_summary (ob
, e
);
1596 /* Output optimization summaries stored in callgraph.
1597 At the moment it is the clone info structure. */
1600 output_cgraph_opt_summary (cgraph_node_set set
)
1602 struct cgraph_node
*node
;
1604 lto_cgraph_encoder_t encoder
;
1605 struct output_block
*ob
= create_output_block (LTO_section_cgraph_opt_sum
);
1608 ob
->cgraph_node
= NULL
;
1609 encoder
= ob
->decl_state
->cgraph_node_encoder
;
1610 n_nodes
= lto_cgraph_encoder_size (encoder
);
1611 for (i
= 0; i
< n_nodes
; i
++)
1612 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder
, i
),
1615 streamer_write_uhwi (ob
, count
);
1616 for (i
= 0; i
< n_nodes
; i
++)
1618 node
= lto_cgraph_encoder_deref (encoder
, i
);
1619 if (output_cgraph_opt_summary_p (node
, set
))
1621 streamer_write_uhwi (ob
, i
);
1622 output_node_opt_summary (ob
, node
, set
);
1625 produce_asm (ob
, NULL
);
1626 destroy_output_block (ob
);
1629 /* Input optimisation summary of EDGE. */
1632 input_edge_opt_summary (struct cgraph_edge
*edge ATTRIBUTE_UNUSED
,
1633 struct lto_input_block
*ib_main ATTRIBUTE_UNUSED
)
1637 /* Input optimisation summary of NODE. */
1640 input_node_opt_summary (struct cgraph_node
*node
,
1641 struct lto_input_block
*ib_main
,
1642 struct data_in
*data_in
)
1647 struct bitpack_d bp
;
1648 struct cgraph_edge
*e
;
1650 count
= streamer_read_uhwi (ib_main
);
1652 node
->clone
.args_to_skip
= BITMAP_GGC_ALLOC ();
1653 for (i
= 0; i
< count
; i
++)
1655 bit
= streamer_read_uhwi (ib_main
);
1656 bitmap_set_bit (node
->clone
.args_to_skip
, bit
);
1658 count
= streamer_read_uhwi (ib_main
);
1660 node
->clone
.combined_args_to_skip
= BITMAP_GGC_ALLOC ();
1661 for (i
= 0; i
< count
; i
++)
1663 bit
= streamer_read_uhwi (ib_main
);
1664 bitmap_set_bit (node
->clone
.combined_args_to_skip
, bit
);
1666 count
= streamer_read_uhwi (ib_main
);
1667 for (i
= 0; i
< count
; i
++)
1671 struct ipa_replace_map
*map
= ggc_alloc_ipa_replace_map ();
1673 VEC_safe_push (ipa_replace_map_p
, gc
, node
->clone
.tree_map
, map
);
1674 for (parm_num
= 0, parm
= DECL_ARGUMENTS (node
->symbol
.decl
); parm_num
;
1675 parm
= DECL_CHAIN (parm
))
1677 map
->parm_num
= streamer_read_uhwi (ib_main
);
1678 map
->old_tree
= NULL
;
1679 map
->new_tree
= stream_read_tree (ib_main
, data_in
);
1680 bp
= streamer_read_bitpack (ib_main
);
1681 map
->replace_p
= bp_unpack_value (&bp
, 1);
1682 map
->ref_p
= bp_unpack_value (&bp
, 1);
1684 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1685 input_edge_opt_summary (e
, ib_main
);
1686 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
1687 input_edge_opt_summary (e
, ib_main
);
1690 /* Read section in file FILE_DATA of length LEN with data DATA. */
1693 input_cgraph_opt_section (struct lto_file_decl_data
*file_data
,
1694 const char *data
, size_t len
, VEC (cgraph_node_ptr
,
1697 const struct lto_function_header
*header
=
1698 (const struct lto_function_header
*) data
;
1699 const int cfg_offset
= sizeof (struct lto_function_header
);
1700 const int main_offset
= cfg_offset
+ header
->cfg_size
;
1701 const int string_offset
= main_offset
+ header
->main_size
;
1702 struct data_in
*data_in
;
1703 struct lto_input_block ib_main
;
1707 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
1711 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
1712 header
->string_size
, NULL
);
1713 count
= streamer_read_uhwi (&ib_main
);
1715 for (i
= 0; i
< count
; i
++)
1717 int ref
= streamer_read_uhwi (&ib_main
);
1718 input_node_opt_summary (VEC_index (cgraph_node_ptr
, nodes
, ref
),
1721 lto_free_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
, data
,
1723 lto_data_in_delete (data_in
);
1726 /* Input optimization summary of cgraph. */
1729 input_cgraph_opt_summary (VEC (cgraph_node_ptr
, heap
) * nodes
)
1731 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
1732 struct lto_file_decl_data
*file_data
;
1735 while ((file_data
= file_data_vec
[j
++]))
1739 lto_get_section_data (file_data
, LTO_section_cgraph_opt_sum
, NULL
,
1743 input_cgraph_opt_section (file_data
, data
, len
, nodes
);