1 /* Write and read the cgraph to the memory mapped representation of a
4 Copyright 2009 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
34 #include "langhooks.h"
35 #include "basic-block.h"
36 #include "tree-flow.h"
40 #include "diagnostic.h"
45 #include "pointer-set.h"
46 #include "lto-streamer.h"
49 static void output_varpool (cgraph_node_set, varpool_node_set);
51 /* Cgraph streaming is organized as set of record whose type
52 is indicated by a tag. */
55 /* Must leave 0 for the stopper. */
57 /* Cgraph node without body available. */
58 LTO_cgraph_unavail_node = 1,
59 /* Cgraph node with function body. */
60 LTO_cgraph_analyzed_node,
63 LTO_cgraph_indirect_edge
66 /* Create a new cgraph encoder. */
69 lto_cgraph_encoder_new (void)
71 lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
72 encoder->map = pointer_map_create ();
73 encoder->nodes = NULL;
78 /* Delete ENCODER and its components. */
81 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
83 VEC_free (cgraph_node_ptr, heap, encoder->nodes);
84 pointer_map_destroy (encoder->map);
89 /* Return the existing reference number of NODE in the cgraph encoder in
90 output block OB. Assign a new reference if this is the first time
94 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
95 struct cgraph_node *node)
100 slot = pointer_map_contains (encoder->map, node);
103 ref = VEC_length (cgraph_node_ptr, encoder->nodes);
104 slot = pointer_map_insert (encoder->map, node);
105 *slot = (void *) (intptr_t) ref;
106 VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
109 ref = (int) (intptr_t) *slot;
114 #define LCC_NOT_FOUND (-1)
116 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
117 or LCC_NOT_FOUND if it is not there. */
120 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
121 struct cgraph_node *node)
123 void **slot = pointer_map_contains (encoder->map, node);
124 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
128 /* Return the cgraph node corresponding to REF using ENCODER. */
131 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
133 if (ref == LCC_NOT_FOUND)
136 return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
140 /* Return number of encoded nodes in ENCODER. */
143 lto_cgraph_encoder_size (lto_cgraph_encoder_t encoder)
145 return VEC_length (cgraph_node_ptr, encoder->nodes);
148 /* Create a new varpool encoder. */
150 lto_varpool_encoder_t
151 lto_varpool_encoder_new (void)
153 lto_varpool_encoder_t encoder = XCNEW (struct lto_varpool_encoder_d);
154 encoder->map = pointer_map_create ();
155 encoder->initializer = pointer_set_create ();
156 encoder->nodes = NULL;
161 /* Delete ENCODER and its components. */
164 lto_varpool_encoder_delete (lto_varpool_encoder_t encoder)
166 VEC_free (varpool_node_ptr, heap, encoder->nodes);
167 pointer_map_destroy (encoder->map);
168 pointer_set_destroy (encoder->initializer);
173 /* Return the existing reference number of NODE in the varpool encoder in
174 output block OB. Assign a new reference if this is the first time
178 lto_varpool_encoder_encode (lto_varpool_encoder_t encoder,
179 struct varpool_node *node)
184 slot = pointer_map_contains (encoder->map, node);
187 ref = VEC_length (varpool_node_ptr, encoder->nodes);
188 slot = pointer_map_insert (encoder->map, node);
189 *slot = (void *) (intptr_t) ref;
190 VEC_safe_push (varpool_node_ptr, heap, encoder->nodes, node);
193 ref = (int) (intptr_t) *slot;
198 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
199 or LCC_NOT_FOUND if it is not there. */
202 lto_varpool_encoder_lookup (lto_varpool_encoder_t encoder,
203 struct varpool_node *node)
205 void **slot = pointer_map_contains (encoder->map, node);
206 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
210 /* Return the varpool node corresponding to REF using ENCODER. */
212 struct varpool_node *
213 lto_varpool_encoder_deref (lto_varpool_encoder_t encoder, int ref)
215 if (ref == LCC_NOT_FOUND)
218 return VEC_index (varpool_node_ptr, encoder->nodes, ref);
222 /* Return number of encoded nodes in ENCODER. */
225 lto_varpool_encoder_size (lto_varpool_encoder_t encoder)
227 return VEC_length (varpool_node_ptr, encoder->nodes);
230 /* Return TRUE if we should encode initializer of NODE (if any). */
233 lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t encoder,
234 struct varpool_node *node)
236 return pointer_set_contains (encoder->initializer, node);
239 /* Return TRUE if we should encode initializer of NODE (if any). */
242 lto_set_varpool_encoder_encode_initializer (lto_varpool_encoder_t encoder,
243 struct varpool_node *node)
245 pointer_set_insert (encoder->initializer, node);
248 /* Output the cgraph EDGE to OB using ENCODER. */
251 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
252 lto_cgraph_encoder_t encoder)
256 struct bitpack_d *bp;
258 if (edge->indirect_unknown_callee)
259 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_indirect_edge);
261 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
263 ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
264 gcc_assert (ref != LCC_NOT_FOUND);
265 lto_output_sleb128_stream (ob->main_stream, ref);
267 if (!edge->indirect_unknown_callee)
269 ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
270 gcc_assert (ref != LCC_NOT_FOUND);
271 lto_output_sleb128_stream (ob->main_stream, ref);
274 lto_output_sleb128_stream (ob->main_stream, edge->count);
276 bp = bitpack_create ();
277 uid = flag_wpa ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt);
278 bp_pack_value (bp, uid, HOST_BITS_PER_INT);
279 bp_pack_value (bp, edge->inline_failed, HOST_BITS_PER_INT);
280 bp_pack_value (bp, edge->frequency, HOST_BITS_PER_INT);
281 bp_pack_value (bp, edge->loop_nest, 30);
282 bp_pack_value (bp, edge->indirect_inlining_edge, 1);
283 bp_pack_value (bp, edge->call_stmt_cannot_inline_p, 1);
284 bp_pack_value (bp, edge->can_throw_external, 1);
285 lto_output_bitpack (ob->main_stream, bp);
289 /* Return if LIST contain references from other partitions. */
291 referenced_from_other_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
292 varpool_node_set vset)
296 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
298 if (ref->refering_type == IPA_REF_CGRAPH)
300 if (!cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
305 if (!varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
313 /* Return true when node is reachable from other partition. */
316 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
318 struct cgraph_edge *e;
321 if (node->global.inlined_to)
323 for (e = node->callers; e; e = e->next_caller)
324 if (!cgraph_node_in_set_p (e->caller, set))
329 /* Output the cgraph NODE to OB. ENCODER is used to find the
330 reference number of NODE->inlined_to. SET is the set of nodes we
331 are writing to the current file. If NODE is not in SET, then NODE
332 is a boundary of a cgraph_node_set and we pretend NODE just has a
333 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
334 that have had their callgraph node written so far. This is used to
335 determine if NODE is a clone of a previously written node. */
338 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
339 lto_cgraph_encoder_t encoder, cgraph_node_set set,
340 varpool_node_set vset,
341 bitmap written_decls)
344 struct bitpack_d *bp;
345 bool boundary_p, wrote_decl_p;
347 bool in_other_partition = false;
349 boundary_p = !cgraph_node_in_set_p (node, set);
350 wrote_decl_p = bitmap_bit_p (written_decls, DECL_UID (node->decl));
352 if (node->analyzed && !boundary_p)
353 tag = LTO_cgraph_analyzed_node;
355 tag = LTO_cgraph_unavail_node;
357 lto_output_uleb128_stream (ob->main_stream, tag);
359 /* In WPA mode, we only output part of the call-graph. Also, we
360 fake cgraph node attributes. There are two cases that we care.
362 Boundary nodes: There are nodes that are not part of SET but are
363 called from within SET. We artificially make them look like
364 externally visible nodes with no function body.
366 Cherry-picked nodes: These are nodes we pulled from other
367 translation units into SET during IPA-inlining. We make them as
368 local static nodes to prevent clashes with other local statics. */
369 if (boundary_p && node->analyzed)
371 /* Inline clones can not be part of boundary.
372 gcc_assert (!node->global.inlined_to);
374 FIXME: At the moment they can be, when partition contains an inline
375 clone that is clone of inline clone from outside partition. We can
376 reshape the clone tree and make other tree to be the root, but it
377 needs a bit extra work and will be promplty done by cgraph_remove_node
378 after reading back. */
379 in_other_partition = 1;
382 lto_output_uleb128_stream (ob->main_stream, wrote_decl_p);
385 bitmap_set_bit (written_decls, DECL_UID (node->decl));
387 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
388 lto_output_sleb128_stream (ob->main_stream, node->count);
390 bp = bitpack_create ();
391 bp_pack_value (bp, node->local.local, 1);
392 bp_pack_value (bp, node->local.externally_visible, 1);
393 bp_pack_value (bp, node->local.finalized, 1);
394 bp_pack_value (bp, node->local.inlinable, 1);
395 bp_pack_value (bp, node->local.disregard_inline_limits, 1);
396 bp_pack_value (bp, node->local.redefined_extern_inline, 1);
397 bp_pack_value (bp, node->local.vtable_method, 1);
398 bp_pack_value (bp, node->needed, 1);
399 bp_pack_value (bp, node->address_taken, 1);
400 bp_pack_value (bp, node->abstract_and_needed, 1);
401 bp_pack_value (bp, tag == LTO_cgraph_analyzed_node
402 && !DECL_EXTERNAL (node->decl)
403 && (reachable_from_other_partition_p (node, set)
404 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
405 bp_pack_value (bp, node->lowered, 1);
406 bp_pack_value (bp, in_other_partition, 1);
407 bp_pack_value (bp, node->alias, 1);
408 bp_pack_value (bp, node->finalized_by_frontend, 1);
409 bp_pack_value (bp, node->frequency, 2);
410 lto_output_bitpack (ob->main_stream, bp);
413 if (tag == LTO_cgraph_analyzed_node)
415 lto_output_sleb128_stream (ob->main_stream,
416 node->local.inline_summary.estimated_self_stack_size);
417 lto_output_sleb128_stream (ob->main_stream,
418 node->local.inline_summary.self_size);
419 lto_output_sleb128_stream (ob->main_stream,
420 node->local.inline_summary.size_inlining_benefit);
421 lto_output_sleb128_stream (ob->main_stream,
422 node->local.inline_summary.self_time);
423 lto_output_sleb128_stream (ob->main_stream,
424 node->local.inline_summary.time_inlining_benefit);
425 if (node->global.inlined_to)
427 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
428 gcc_assert (ref != LCC_NOT_FOUND);
433 lto_output_sleb128_stream (ob->main_stream, ref);
436 if (node->same_comdat_group && !boundary_p)
438 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
439 gcc_assert (ref != LCC_NOT_FOUND);
443 lto_output_sleb128_stream (ob->main_stream, ref);
447 struct cgraph_node *alias;
448 unsigned long alias_count = 1;
449 for (alias = node->same_body; alias->next; alias = alias->next)
451 lto_output_uleb128_stream (ob->main_stream, alias_count);
454 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
456 if (alias->thunk.thunk_p)
458 lto_output_uleb128_stream
460 1 + (alias->thunk.this_adjusting != 0) * 2
461 + (alias->thunk.virtual_offset_p != 0) * 4);
462 lto_output_uleb128_stream (ob->main_stream,
463 alias->thunk.fixed_offset);
464 lto_output_uleb128_stream (ob->main_stream,
465 alias->thunk.virtual_value);
466 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
471 lto_output_uleb128_stream (ob->main_stream, 0);
472 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
475 alias = alias->previous;
480 lto_output_uleb128_stream (ob->main_stream, 0);
483 /* Output the varpool NODE to OB.
484 If NODE is not in SET, then NODE is a boundary. */
487 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
488 cgraph_node_set set, varpool_node_set vset)
490 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
491 struct bitpack_d *bp;
492 struct varpool_node *alias;
495 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
496 bp = bitpack_create ();
497 bp_pack_value (bp, node->externally_visible, 1);
498 bp_pack_value (bp, node->force_output, 1);
499 bp_pack_value (bp, node->finalized, 1);
500 bp_pack_value (bp, node->alias, 1);
501 gcc_assert (!node->alias || !node->extra_name);
502 gcc_assert (node->finalized || !node->analyzed);
503 gcc_assert (node->needed);
504 /* Constant pool initializers can be de-unified into individual ltrans units.
505 FIXME: Alternatively at -Os we may want to avoid generating for them the local
506 labels and share them across LTRANS partitions. */
507 if (DECL_IN_CONSTANT_POOL (node->decl))
509 bp_pack_value (bp, 0, 1); /* used_from_other_parition. */
510 bp_pack_value (bp, 0, 1); /* in_other_partition. */
514 bp_pack_value (bp, node->analyzed
515 && referenced_from_other_partition_p (&node->ref_list,
517 bp_pack_value (bp, boundary_p, 1); /* in_other_partition. */
519 /* Also emit any extra name aliases. */
520 for (alias = node->extra_name; alias; alias = alias->next)
522 bp_pack_value (bp, count != 0, 1);
523 lto_output_bitpack (ob->main_stream, bp);
528 lto_output_uleb128_stream (ob->main_stream, count);
529 for (alias = node->extra_name; alias; alias = alias->next)
530 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
534 /* Output the varpool NODE to OB.
535 If NODE is not in SET, then NODE is a boundary. */
538 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
539 lto_cgraph_encoder_t encoder,
540 lto_varpool_encoder_t varpool_encoder)
542 struct bitpack_d *bp = bitpack_create ();
543 bp_pack_value (bp, ref->refered_type, 1);
544 bp_pack_value (bp, ref->use, 2);
545 lto_output_bitpack (ob->main_stream, bp);
547 if (ref->refered_type == IPA_REF_CGRAPH)
549 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
550 gcc_assert (nref != LCC_NOT_FOUND);
551 lto_output_sleb128_stream (ob->main_stream, nref);
555 int nref = lto_varpool_encoder_lookup (varpool_encoder,
556 ipa_ref_varpool_node (ref));
557 gcc_assert (nref != LCC_NOT_FOUND);
558 lto_output_sleb128_stream (ob->main_stream, nref);
562 /* Stream out profile_summary to OB. */
565 output_profile_summary (struct lto_simple_output_block *ob)
569 /* We do not output num, it is not terribly useful. */
570 gcc_assert (profile_info->runs);
571 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
572 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
573 lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
574 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
577 lto_output_uleb128_stream (ob->main_stream, 0);
580 /* Add NODE into encoder as well as nodes it is cloned from.
581 Do it in a way so clones appear first. */
583 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node)
586 add_node_to (encoder, node->clone_of);
587 lto_cgraph_encoder_encode (encoder, node);
590 /* Add all references in LIST to encoders. */
593 add_references (lto_cgraph_encoder_t encoder,
594 lto_varpool_encoder_t varpool_encoder,
595 struct ipa_ref_list *list)
599 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
600 if (ref->refered_type == IPA_REF_CGRAPH)
601 add_node_to (encoder, ipa_ref_node (ref));
604 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
605 lto_varpool_encoder_encode (varpool_encoder, vnode);
609 /* Output all callees or indirect outgoing edges. EDGE must be the first such
613 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
614 struct lto_simple_output_block *ob,
615 lto_cgraph_encoder_t encoder)
620 /* Output edges in backward direction, so the reconstructed callgraph match
621 and it is easy to associate call sites in the IPA pass summaries. */
622 while (edge->next_callee)
623 edge = edge->next_callee;
624 for (; edge; edge = edge->prev_callee)
625 lto_output_edge (ob, edge, encoder);
628 /* Output the part of the cgraph in SET. */
631 output_refs (cgraph_node_set set, varpool_node_set vset,
632 lto_cgraph_encoder_t encoder,
633 lto_varpool_encoder_t varpool_encoder)
635 cgraph_node_set_iterator csi;
636 varpool_node_set_iterator vsi;
637 struct lto_simple_output_block *ob;
642 ob = lto_create_simple_output_block (LTO_section_refs);
644 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
646 struct cgraph_node *node = csi_node (csi);
648 count = ipa_ref_list_nreferences (&node->ref_list);
651 lto_output_uleb128_stream (ob->main_stream, count);
652 lto_output_uleb128_stream (ob->main_stream,
653 lto_cgraph_encoder_lookup (encoder, node));
654 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
655 lto_output_ref (ob, ref, encoder, varpool_encoder);
659 lto_output_uleb128_stream (ob->main_stream, 0);
661 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
663 struct varpool_node *node = vsi_node (vsi);
665 count = ipa_ref_list_nreferences (&node->ref_list);
668 lto_output_uleb128_stream (ob->main_stream, count);
669 lto_output_uleb128_stream (ob->main_stream,
670 lto_varpool_encoder_lookup (varpool_encoder,
672 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
673 lto_output_ref (ob, ref, encoder, varpool_encoder);
677 lto_output_uleb128_stream (ob->main_stream, 0);
679 lto_destroy_simple_output_block (ob);
683 /* Output the part of the cgraph in SET. */
686 output_cgraph (cgraph_node_set set, varpool_node_set vset)
688 struct cgraph_node *node;
689 struct lto_simple_output_block *ob;
690 cgraph_node_set_iterator csi;
691 varpool_node_set_iterator vsi;
692 struct cgraph_edge *edge;
694 bitmap written_decls;
695 lto_cgraph_encoder_t encoder;
696 lto_varpool_encoder_t varpool_encoder;
697 struct cgraph_asm_node *can;
699 ob = lto_create_simple_output_block (LTO_section_cgraph);
701 output_profile_summary (ob);
703 /* An encoder for cgraph nodes should have been created by
704 ipa_write_summaries_1. */
705 gcc_assert (ob->decl_state->cgraph_node_encoder);
706 gcc_assert (ob->decl_state->varpool_node_encoder);
707 encoder = ob->decl_state->cgraph_node_encoder;
708 varpool_encoder = ob->decl_state->varpool_node_encoder;
710 /* The FUNCTION_DECLs for which we have written a node. The first
711 node found is written as the "original" node, the remaining nodes
712 are considered its clones. */
713 written_decls = lto_bitmap_alloc ();
715 /* Go over all the nodes in SET and assign references. */
716 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
718 node = csi_node (csi);
719 add_node_to (encoder, node);
720 add_references (encoder, varpool_encoder, &node->ref_list);
722 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
724 struct varpool_node *vnode = vsi_node (vsi);
725 gcc_assert (!vnode->alias);
726 lto_varpool_encoder_encode (varpool_encoder, vnode);
727 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
728 add_references (encoder, varpool_encoder, &vnode->ref_list);
730 /* Pickle in also the initializer of all referenced readonly variables
731 to help folding. Constant pool variables are not shared, so we must
733 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
735 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
736 if (DECL_INITIAL (vnode->decl)
737 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
739 && (DECL_IN_CONSTANT_POOL (vnode->decl)
740 || TREE_READONLY (vnode->decl)))
742 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
743 add_references (encoder, varpool_encoder, &vnode->ref_list);
747 /* Go over all the nodes again to include callees that are not in
749 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
751 node = csi_node (csi);
752 for (edge = node->callees; edge; edge = edge->next_callee)
754 struct cgraph_node *callee = edge->callee;
755 if (!cgraph_node_in_set_p (callee, set))
757 /* We should have moved all the inlines. */
758 gcc_assert (!callee->global.inlined_to);
759 add_node_to (encoder, callee);
764 /* Write out the nodes. We must first output a node and then its clones,
765 otherwise at a time reading back the node there would be nothing to clone
767 n_nodes = lto_cgraph_encoder_size (encoder);
768 for (i = 0; i < n_nodes; i++)
770 node = lto_cgraph_encoder_deref (encoder, i);
771 lto_output_node (ob, node, encoder, set, vset, written_decls);
774 lto_bitmap_free (written_decls);
776 /* Go over the nodes in SET again to write edges. */
777 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
779 node = csi_node (csi);
780 output_outgoing_cgraph_edges (node->callees, ob, encoder);
781 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
784 lto_output_uleb128_stream (ob->main_stream, 0);
786 /* Emit toplevel asms. */
787 for (can = cgraph_asm_nodes; can; can = can->next)
789 int len = TREE_STRING_LENGTH (can->asm_str);
790 lto_output_uleb128_stream (ob->main_stream, len);
791 for (i = 0; i < len; ++i)
792 lto_output_1_stream (ob->main_stream,
793 TREE_STRING_POINTER (can->asm_str)[i]);
796 lto_output_uleb128_stream (ob->main_stream, 0);
798 lto_destroy_simple_output_block (ob);
799 output_varpool (set, vset);
800 output_refs (set, vset, encoder, varpool_encoder);
803 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
804 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
805 NODE or to replace the values in it, for instance because the first
806 time we saw it, the function body was not available but now it
807 is. BP is a bitpack with all the bitflags for NODE read from the
811 input_overwrite_node (struct lto_file_decl_data *file_data,
812 struct cgraph_node *node,
813 enum LTO_cgraph_tags tag,
814 struct bitpack_d *bp,
815 unsigned int stack_size,
816 unsigned int self_time,
817 unsigned int time_inlining_benefit,
818 unsigned int self_size,
819 unsigned int size_inlining_benefit)
821 node->aux = (void *) tag;
822 node->local.inline_summary.estimated_self_stack_size = stack_size;
823 node->local.inline_summary.self_time = self_time;
824 node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
825 node->local.inline_summary.self_size = self_size;
826 node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
827 node->global.time = self_time;
828 node->global.size = self_size;
829 node->global.estimated_stack_size = stack_size;
830 node->global.estimated_growth = INT_MIN;
831 node->local.lto_file_data = file_data;
833 node->local.local = bp_unpack_value (bp, 1);
834 node->local.externally_visible = bp_unpack_value (bp, 1);
835 node->local.finalized = bp_unpack_value (bp, 1);
836 node->local.inlinable = bp_unpack_value (bp, 1);
837 node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
838 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
839 node->local.vtable_method = bp_unpack_value (bp, 1);
840 node->needed = bp_unpack_value (bp, 1);
841 node->address_taken = bp_unpack_value (bp, 1);
842 node->abstract_and_needed = bp_unpack_value (bp, 1);
843 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
844 node->lowered = bp_unpack_value (bp, 1);
845 node->analyzed = tag == LTO_cgraph_analyzed_node;
846 node->in_other_partition = bp_unpack_value (bp, 1);
847 node->alias = bp_unpack_value (bp, 1);
848 node->finalized_by_frontend = bp_unpack_value (bp, 1);
849 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
852 /* Output the part of the cgraph in SET. */
855 output_varpool (cgraph_node_set set, varpool_node_set vset)
857 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
858 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
859 int len = lto_varpool_encoder_size (varpool_encoder), i;
861 lto_output_uleb128_stream (ob->main_stream, len);
863 /* Write out the nodes. We must first output a node and then its clones,
864 otherwise at a time reading back the node there would be nothing to clone
866 for (i = 0; i < len; i++)
868 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
872 lto_destroy_simple_output_block (ob);
875 /* Read a node from input_block IB. TAG is the node's tag just read.
876 Return the node read or overwriten. */
878 static struct cgraph_node *
879 input_node (struct lto_file_decl_data *file_data,
880 struct lto_input_block *ib,
881 enum LTO_cgraph_tags tag)
884 struct cgraph_node *node;
885 struct bitpack_d *bp;
889 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
892 int time_inlining_benefit = 0;
893 int size_inlining_benefit = 0;
894 unsigned long same_body_count = 0;
896 clone_p = (lto_input_uleb128 (ib) != 0);
898 decl_index = lto_input_uleb128 (ib);
899 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
902 node = cgraph_clone_node (cgraph_node (fn_decl), 0,
903 CGRAPH_FREQ_BASE, 0, false, NULL);
906 node = cgraph_node (fn_decl);
908 node->count = lto_input_sleb128 (ib);
909 bp = lto_input_bitpack (ib);
911 if (tag == LTO_cgraph_analyzed_node)
913 stack_size = lto_input_sleb128 (ib);
914 self_size = lto_input_sleb128 (ib);
915 size_inlining_benefit = lto_input_sleb128 (ib);
916 self_time = lto_input_sleb128 (ib);
917 time_inlining_benefit = lto_input_sleb128 (ib);
919 ref = lto_input_sleb128 (ib);
922 ref2 = lto_input_sleb128 (ib);
923 same_body_count = lto_input_uleb128 (ib);
925 /* Make sure that we have not read this node before. Nodes that
926 have already been read will have their tag stored in the 'aux'
927 field. Since built-in functions can be referenced in multiple
928 functions, they are expected to be read more than once. */
929 if (node->aux && !DECL_IS_BUILTIN (node->decl))
930 internal_error ("bytecode stream: found multiple instances of cgraph "
931 "node %d", node->uid);
933 input_overwrite_node (file_data, node, tag, bp, stack_size, self_time,
934 time_inlining_benefit, self_size,
935 size_inlining_benefit);
938 /* Store a reference for now, and fix up later to be a pointer. */
939 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
941 /* Store a reference for now, and fix up later to be a pointer. */
942 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
944 while (same_body_count-- > 0)
948 decl_index = lto_input_uleb128 (ib);
949 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
950 type = lto_input_uleb128 (ib);
954 decl_index = lto_input_uleb128 (ib);
955 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
956 cgraph_same_body_alias (alias_decl, real_alias);
960 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
961 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
963 decl_index = lto_input_uleb128 (ib);
964 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
965 cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
967 (type & 4) ? size_int (virtual_value) : NULL_TREE,
974 /* Read a node from input_block IB. TAG is the node's tag just read.
975 Return the node read or overwriten. */
977 static struct varpool_node *
978 input_varpool_node (struct lto_file_decl_data *file_data,
979 struct lto_input_block *ib)
983 struct varpool_node *node;
984 struct bitpack_d *bp;
988 decl_index = lto_input_uleb128 (ib);
989 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
990 node = varpool_node (var_decl);
992 bp = lto_input_bitpack (ib);
993 node->externally_visible = bp_unpack_value (bp, 1);
994 node->force_output = bp_unpack_value (bp, 1);
995 node->finalized = bp_unpack_value (bp, 1);
996 node->alias = bp_unpack_value (bp, 1);
997 node->analyzed = node->finalized;
998 node->used_from_other_partition = bp_unpack_value (bp, 1);
999 node->in_other_partition = bp_unpack_value (bp, 1);
1000 aliases_p = bp_unpack_value (bp, 1);
1001 if (node->finalized)
1002 varpool_mark_needed_node (node);
1003 bitpack_delete (bp);
1006 count = lto_input_uleb128 (ib);
1007 for (; count > 0; count --)
1009 tree decl = lto_file_decl_data_get_var_decl (file_data,
1010 lto_input_uleb128 (ib));
1011 varpool_extra_name_alias (decl, var_decl);
1017 /* Read a node from input_block IB. TAG is the node's tag just read.
1018 Return the node read or overwriten. */
1021 input_ref (struct lto_input_block *ib,
1022 struct cgraph_node *refering_node,
1023 struct varpool_node *refering_varpool_node,
1024 VEC(cgraph_node_ptr, heap) *nodes,
1025 VEC(varpool_node_ptr, heap) *varpool_nodes)
1027 struct cgraph_node *node = NULL;
1028 struct varpool_node *varpool_node = NULL;
1029 struct bitpack_d *bp;
1030 enum ipa_ref_type type;
1031 enum ipa_ref_use use;
1033 bp = lto_input_bitpack (ib);
1034 type = (enum ipa_ref_type) bp_unpack_value (bp, 1);
1035 use = (enum ipa_ref_use) bp_unpack_value (bp, 2);
1036 bitpack_delete (bp);
1037 if (type == IPA_REF_CGRAPH)
1038 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1040 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1041 ipa_record_reference (refering_node, refering_varpool_node,
1042 node, varpool_node, use, NULL);
1045 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1046 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1047 edge being read is indirect (in the sense that it has
1048 indirect_unknown_callee set). */
1051 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1054 struct cgraph_node *caller, *callee;
1055 struct cgraph_edge *edge;
1056 unsigned int stmt_id;
1060 cgraph_inline_failed_t inline_failed;
1061 struct bitpack_d *bp;
1062 enum ld_plugin_symbol_resolution caller_resolution;
1064 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1065 if (caller == NULL || caller->decl == NULL_TREE)
1066 internal_error ("bytecode stream: no caller found while reading edge");
1070 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1071 if (callee == NULL || callee->decl == NULL_TREE)
1072 internal_error ("bytecode stream: no callee found while reading edge");
1077 count = (gcov_type) lto_input_sleb128 (ib);
1079 bp = lto_input_bitpack (ib);
1080 stmt_id = (unsigned int) bp_unpack_value (bp, HOST_BITS_PER_INT);
1081 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (bp,
1083 freq = (int) bp_unpack_value (bp, HOST_BITS_PER_INT);
1084 nest = (unsigned) bp_unpack_value (bp, 30);
1086 /* If the caller was preempted, don't create the edge.
1087 ??? Should we ever have edges from a preempted caller? */
1088 caller_resolution = lto_symtab_get_resolution (caller->decl);
1089 if (caller_resolution == LDPR_PREEMPTED_REG
1090 || caller_resolution == LDPR_PREEMPTED_IR)
1094 edge = cgraph_create_indirect_edge (caller, NULL, count, freq, nest);
1096 edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
1098 edge->indirect_inlining_edge = bp_unpack_value (bp, 1);
1099 edge->lto_stmt_uid = stmt_id;
1100 edge->inline_failed = inline_failed;
1101 edge->call_stmt_cannot_inline_p = bp_unpack_value (bp, 1);
1102 edge->can_throw_external = bp_unpack_value (bp, 1);
1103 bitpack_delete (bp);
1107 /* Read a cgraph from IB using the info in FILE_DATA. */
1109 static VEC(cgraph_node_ptr, heap) *
1110 input_cgraph_1 (struct lto_file_decl_data *file_data,
1111 struct lto_input_block *ib)
1113 enum LTO_cgraph_tags tag;
1114 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1115 struct cgraph_node *node;
1117 unsigned HOST_WIDE_INT len;
1119 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1122 if (tag == LTO_cgraph_edge)
1123 input_edge (ib, nodes, false);
1124 else if (tag == LTO_cgraph_indirect_edge)
1125 input_edge (ib, nodes, true);
1128 node = input_node (file_data, ib, tag);
1129 if (node == NULL || node->decl == NULL_TREE)
1130 internal_error ("bytecode stream: found empty cgraph node");
1131 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1132 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1135 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1138 /* Input toplevel asms. */
1139 len = lto_input_uleb128 (ib);
1142 char *str = (char *)xmalloc (len + 1);
1143 for (i = 0; i < len; ++i)
1144 str[i] = lto_input_1_unsigned (ib);
1145 cgraph_add_asm_node (build_string (len, str));
1148 len = lto_input_uleb128 (ib);
1151 for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
1153 int ref = (int) (intptr_t) node->global.inlined_to;
1155 /* Fixup inlined_to from reference to pointer. */
1156 if (ref != LCC_NOT_FOUND)
1157 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1159 node->global.inlined_to = NULL;
1161 ref = (int) (intptr_t) node->same_comdat_group;
1163 /* Fixup same_comdat_group from reference to pointer. */
1164 if (ref != LCC_NOT_FOUND)
1165 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1167 node->same_comdat_group = NULL;
1172 /* Read a varpool from IB using the info in FILE_DATA. */
1174 static VEC(varpool_node_ptr, heap) *
1175 input_varpool_1 (struct lto_file_decl_data *file_data,
1176 struct lto_input_block *ib)
1178 unsigned HOST_WIDE_INT len;
1179 VEC(varpool_node_ptr, heap) *varpool = NULL;
1181 len = lto_input_uleb128 (ib);
1184 VEC_safe_push (varpool_node_ptr, heap, varpool,
1185 input_varpool_node (file_data, ib));
1191 /* Input ipa_refs. */
1194 input_refs (struct lto_input_block *ib,
1195 VEC(cgraph_node_ptr, heap) *nodes,
1196 VEC(varpool_node_ptr, heap) *varpool)
1202 struct cgraph_node *node;
1203 count = lto_input_uleb128 (ib);
1206 idx = lto_input_uleb128 (ib);
1207 node = VEC_index (cgraph_node_ptr, nodes, idx);
1210 input_ref (ib, node, NULL, nodes, varpool);
1216 struct varpool_node *node;
1217 count = lto_input_uleb128 (ib);
1220 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1223 input_ref (ib, NULL, node, nodes, varpool);
1230 static struct gcov_ctr_summary lto_gcov_summary;
1232 /* Input profile_info from IB. */
1234 input_profile_summary (struct lto_input_block *ib)
1236 unsigned int runs = lto_input_uleb128 (ib);
1241 profile_info = <o_gcov_summary;
1242 lto_gcov_summary.runs = runs;
1243 lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
1244 lto_gcov_summary.run_max = lto_input_sleb128 (ib);
1245 lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
1247 /* We can support this by scaling all counts to nearest common multiple
1248 of all different runs, but it is perhaps not worth the effort. */
1249 else if (profile_info->runs != runs
1250 || profile_info->sum_all != lto_input_sleb128 (ib)
1251 || profile_info->run_max != lto_input_sleb128 (ib)
1252 || profile_info->sum_max != lto_input_sleb128 (ib))
1253 sorry ("Combining units with different profiles is not supported.");
1254 /* We allow some units to have profile and other to not have one. This will
1255 just make unprofiled units to be size optimized that is sane. */
1260 /* Input and merge the cgraph from each of the .o files passed to
1266 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1267 struct lto_file_decl_data *file_data;
1269 struct cgraph_node *node;
1271 while ((file_data = file_data_vec[j++]))
1275 struct lto_input_block *ib;
1276 VEC(cgraph_node_ptr, heap) *nodes;
1277 VEC(varpool_node_ptr, heap) *varpool;
1279 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1281 input_profile_summary (ib);
1282 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1283 nodes = input_cgraph_1 (file_data, ib);
1284 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1287 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1289 varpool = input_varpool_1 (file_data, ib);
1290 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1293 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1295 input_refs (ib, nodes, varpool);
1296 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1298 VEC_free (cgraph_node_ptr, heap, nodes);
1299 VEC_free (varpool_node_ptr, heap, varpool);
1302 /* Clear out the aux field that was used to store enough state to
1303 tell which nodes should be overwritten. */
1304 for (node = cgraph_nodes; node; node = node->next)
1306 /* Some nodes may have been created by cgraph_node. This
1307 happens when the callgraph contains nested functions. If the
1308 node for the parent function was never emitted to the gimple
1309 file, cgraph_node will create a node for it when setting the
1310 context of the nested function. */
1311 if (node->local.lto_file_data)