gcc/
[official-gcc.git] / gcc / lto-cgraph.c
blobdd7b954344036dd20647a92b531a6b354d69b97c
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright 2009, 2010 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
12 version.
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
17 for more details.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "langhooks.h"
35 #include "basic-block.h"
36 #include "tree-flow.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic-core.h"
41 #include "except.h"
42 #include "vec.h"
43 #include "timevar.h"
44 #include "output.h"
45 #include "pointer-set.h"
46 #include "lto-streamer.h"
47 #include "gcov-io.h"
49 static void output_varpool (cgraph_node_set, varpool_node_set);
50 static void output_cgraph_opt_summary (void);
51 static void input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes);
54 /* Cgraph streaming is organized as set of record whose type
55 is indicated by a tag. */
56 enum LTO_cgraph_tags
58 /* Must leave 0 for the stopper. */
60 /* Cgraph node without body available. */
61 LTO_cgraph_unavail_node = 1,
62 /* Cgraph node with function body. */
63 LTO_cgraph_analyzed_node,
64 /* Cgraph edges. */
65 LTO_cgraph_edge,
66 LTO_cgraph_indirect_edge
69 /* Create a new cgraph encoder. */
71 lto_cgraph_encoder_t
72 lto_cgraph_encoder_new (void)
74 lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
75 encoder->map = pointer_map_create ();
76 encoder->nodes = NULL;
77 encoder->body = pointer_set_create ();
78 return encoder;
82 /* Delete ENCODER and its components. */
84 void
85 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
87 VEC_free (cgraph_node_ptr, heap, encoder->nodes);
88 pointer_map_destroy (encoder->map);
89 pointer_set_destroy (encoder->body);
90 free (encoder);
94 /* Return the existing reference number of NODE in the cgraph encoder in
95 output block OB. Assign a new reference if this is the first time
96 NODE is encoded. */
98 int
99 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
100 struct cgraph_node *node)
102 int ref;
103 void **slot;
105 slot = pointer_map_contains (encoder->map, node);
106 if (!slot)
108 ref = VEC_length (cgraph_node_ptr, encoder->nodes);
109 slot = pointer_map_insert (encoder->map, node);
110 *slot = (void *) (intptr_t) ref;
111 VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
113 else
114 ref = (int) (intptr_t) *slot;
116 return ref;
119 #define LCC_NOT_FOUND (-1)
121 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
122 or LCC_NOT_FOUND if it is not there. */
125 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
126 struct cgraph_node *node)
128 void **slot = pointer_map_contains (encoder->map, node);
129 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
133 /* Return the cgraph node corresponding to REF using ENCODER. */
135 struct cgraph_node *
136 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
138 if (ref == LCC_NOT_FOUND)
139 return NULL;
141 return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
145 /* Return TRUE if we should encode initializer of NODE (if any). */
147 bool
148 lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t encoder,
149 struct cgraph_node *node)
151 return pointer_set_contains (encoder->body, node);
154 /* Return TRUE if we should encode body of NODE (if any). */
156 static void
157 lto_set_cgraph_encoder_encode_body (lto_cgraph_encoder_t encoder,
158 struct cgraph_node *node)
160 pointer_set_insert (encoder->body, node);
163 /* Create a new varpool encoder. */
165 lto_varpool_encoder_t
166 lto_varpool_encoder_new (void)
168 lto_varpool_encoder_t encoder = XCNEW (struct lto_varpool_encoder_d);
169 encoder->map = pointer_map_create ();
170 encoder->initializer = pointer_set_create ();
171 encoder->nodes = NULL;
172 return encoder;
176 /* Delete ENCODER and its components. */
178 void
179 lto_varpool_encoder_delete (lto_varpool_encoder_t encoder)
181 VEC_free (varpool_node_ptr, heap, encoder->nodes);
182 pointer_map_destroy (encoder->map);
183 pointer_set_destroy (encoder->initializer);
184 free (encoder);
188 /* Return the existing reference number of NODE in the varpool encoder in
189 output block OB. Assign a new reference if this is the first time
190 NODE is encoded. */
193 lto_varpool_encoder_encode (lto_varpool_encoder_t encoder,
194 struct varpool_node *node)
196 int ref;
197 void **slot;
199 slot = pointer_map_contains (encoder->map, node);
200 if (!slot)
202 ref = VEC_length (varpool_node_ptr, encoder->nodes);
203 slot = pointer_map_insert (encoder->map, node);
204 *slot = (void *) (intptr_t) ref;
205 VEC_safe_push (varpool_node_ptr, heap, encoder->nodes, node);
207 else
208 ref = (int) (intptr_t) *slot;
210 return ref;
213 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
214 or LCC_NOT_FOUND if it is not there. */
217 lto_varpool_encoder_lookup (lto_varpool_encoder_t encoder,
218 struct varpool_node *node)
220 void **slot = pointer_map_contains (encoder->map, node);
221 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
225 /* Return the varpool node corresponding to REF using ENCODER. */
227 struct varpool_node *
228 lto_varpool_encoder_deref (lto_varpool_encoder_t encoder, int ref)
230 if (ref == LCC_NOT_FOUND)
231 return NULL;
233 return VEC_index (varpool_node_ptr, encoder->nodes, ref);
237 /* Return TRUE if we should encode initializer of NODE (if any). */
239 bool
240 lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t encoder,
241 struct varpool_node *node)
243 return pointer_set_contains (encoder->initializer, node);
246 /* Return TRUE if we should encode initializer of NODE (if any). */
248 static void
249 lto_set_varpool_encoder_encode_initializer (lto_varpool_encoder_t encoder,
250 struct varpool_node *node)
252 pointer_set_insert (encoder->initializer, node);
255 /* Output the cgraph EDGE to OB using ENCODER. */
257 static void
258 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
259 lto_cgraph_encoder_t encoder)
261 unsigned int uid;
262 intptr_t ref;
263 struct bitpack_d *bp;
265 if (edge->indirect_unknown_callee)
266 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_indirect_edge);
267 else
268 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
270 ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
271 gcc_assert (ref != LCC_NOT_FOUND);
272 lto_output_sleb128_stream (ob->main_stream, ref);
274 if (!edge->indirect_unknown_callee)
276 ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
277 gcc_assert (ref != LCC_NOT_FOUND);
278 lto_output_sleb128_stream (ob->main_stream, ref);
281 lto_output_sleb128_stream (ob->main_stream, edge->count);
283 bp = bitpack_create ();
284 uid = flag_wpa ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt);
285 bp_pack_value (bp, uid, HOST_BITS_PER_INT);
286 bp_pack_value (bp, edge->inline_failed, HOST_BITS_PER_INT);
287 bp_pack_value (bp, edge->frequency, HOST_BITS_PER_INT);
288 bp_pack_value (bp, edge->loop_nest, 30);
289 bp_pack_value (bp, edge->indirect_inlining_edge, 1);
290 bp_pack_value (bp, edge->call_stmt_cannot_inline_p, 1);
291 bp_pack_value (bp, edge->can_throw_external, 1);
292 if (edge->indirect_unknown_callee)
294 int flags = edge->indirect_info->ecf_flags;
295 bp_pack_value (bp, (flags & ECF_CONST) != 0, 1);
296 bp_pack_value (bp, (flags & ECF_PURE) != 0, 1);
297 bp_pack_value (bp, (flags & ECF_NORETURN) != 0, 1);
298 bp_pack_value (bp, (flags & ECF_MALLOC) != 0, 1);
299 bp_pack_value (bp, (flags & ECF_NOTHROW) != 0, 1);
300 bp_pack_value (bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
301 /* Flags that should not appear on indirect calls. */
302 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
303 | ECF_MAY_BE_ALLOCA
304 | ECF_SIBCALL
305 | ECF_NOVOPS)));
307 lto_output_bitpack (ob->main_stream, bp);
308 bitpack_delete (bp);
311 /* Return if LIST contain references from other partitions. */
313 bool
314 referenced_from_other_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
315 varpool_node_set vset)
317 int i;
318 struct ipa_ref *ref;
319 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
321 if (ref->refering_type == IPA_REF_CGRAPH)
323 if (!cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
324 return true;
326 else
328 if (!varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
329 vset))
330 return true;
333 return false;
336 /* Return true when node is reachable from other partition. */
338 bool
339 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
341 struct cgraph_edge *e;
342 if (!node->analyzed)
343 return false;
344 if (node->global.inlined_to)
345 return false;
346 for (e = node->callers; e; e = e->next_caller)
347 if (!cgraph_node_in_set_p (e->caller, set))
348 return true;
349 return false;
352 /* Return if LIST contain references from other partitions. */
354 bool
355 referenced_from_this_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
356 varpool_node_set vset)
358 int i;
359 struct ipa_ref *ref;
360 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
362 if (ref->refering_type == IPA_REF_CGRAPH)
364 if (cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
365 return true;
367 else
369 if (varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
370 vset))
371 return true;
374 return false;
377 /* Return true when node is reachable from other partition. */
379 bool
380 reachable_from_this_partition_p (struct cgraph_node *node, cgraph_node_set set)
382 struct cgraph_edge *e;
383 if (!node->analyzed)
384 return false;
385 if (node->global.inlined_to)
386 return false;
387 for (e = node->callers; e; e = e->next_caller)
388 if (cgraph_node_in_set_p (e->caller, set))
389 return true;
390 return false;
393 /* Output the cgraph NODE to OB. ENCODER is used to find the
394 reference number of NODE->inlined_to. SET is the set of nodes we
395 are writing to the current file. If NODE is not in SET, then NODE
396 is a boundary of a cgraph_node_set and we pretend NODE just has a
397 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
398 that have had their callgraph node written so far. This is used to
399 determine if NODE is a clone of a previously written node. */
401 static void
402 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
403 lto_cgraph_encoder_t encoder, cgraph_node_set set,
404 varpool_node_set vset)
406 unsigned int tag;
407 struct bitpack_d *bp;
408 bool boundary_p;
409 intptr_t ref;
410 bool in_other_partition = false;
411 struct cgraph_node *clone_of;
413 boundary_p = !cgraph_node_in_set_p (node, set);
415 if (node->analyzed && !boundary_p)
416 tag = LTO_cgraph_analyzed_node;
417 else
418 tag = LTO_cgraph_unavail_node;
420 lto_output_uleb128_stream (ob->main_stream, tag);
422 /* In WPA mode, we only output part of the call-graph. Also, we
423 fake cgraph node attributes. There are two cases that we care.
425 Boundary nodes: There are nodes that are not part of SET but are
426 called from within SET. We artificially make them look like
427 externally visible nodes with no function body.
429 Cherry-picked nodes: These are nodes we pulled from other
430 translation units into SET during IPA-inlining. We make them as
431 local static nodes to prevent clashes with other local statics. */
432 if (boundary_p && node->analyzed)
434 /* Inline clones can not be part of boundary.
435 gcc_assert (!node->global.inlined_to);
437 FIXME: At the moment they can be, when partition contains an inline
438 clone that is clone of inline clone from outside partition. We can
439 reshape the clone tree and make other tree to be the root, but it
440 needs a bit extra work and will be promplty done by cgraph_remove_node
441 after reading back. */
442 in_other_partition = 1;
445 clone_of = node->clone_of;
446 while (clone_of
447 && (ref = lto_cgraph_encoder_lookup (encoder, node->clone_of)) == LCC_NOT_FOUND)
448 if (clone_of->prev_sibling_clone)
449 clone_of = clone_of->prev_sibling_clone;
450 else
451 clone_of = clone_of->clone_of;
452 if (!clone_of)
453 lto_output_sleb128_stream (ob->main_stream, LCC_NOT_FOUND);
454 else
455 lto_output_sleb128_stream (ob->main_stream, ref);
458 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
459 lto_output_sleb128_stream (ob->main_stream, node->count);
461 bp = bitpack_create ();
462 bp_pack_value (bp, node->local.local, 1);
463 bp_pack_value (bp, node->local.externally_visible, 1);
464 bp_pack_value (bp, node->local.finalized, 1);
465 bp_pack_value (bp, node->local.inlinable, 1);
466 bp_pack_value (bp, node->local.versionable, 1);
467 bp_pack_value (bp, node->local.disregard_inline_limits, 1);
468 bp_pack_value (bp, node->local.redefined_extern_inline, 1);
469 bp_pack_value (bp, node->local.vtable_method, 1);
470 bp_pack_value (bp, node->needed, 1);
471 bp_pack_value (bp, node->address_taken, 1);
472 bp_pack_value (bp, node->abstract_and_needed, 1);
473 bp_pack_value (bp, tag == LTO_cgraph_analyzed_node
474 && !DECL_EXTERNAL (node->decl)
475 && (reachable_from_other_partition_p (node, set)
476 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
477 bp_pack_value (bp, node->lowered, 1);
478 bp_pack_value (bp, in_other_partition, 1);
479 bp_pack_value (bp, node->alias, 1);
480 bp_pack_value (bp, node->finalized_by_frontend, 1);
481 bp_pack_value (bp, node->frequency, 2);
482 lto_output_bitpack (ob->main_stream, bp);
483 bitpack_delete (bp);
485 if (tag == LTO_cgraph_analyzed_node)
487 lto_output_sleb128_stream (ob->main_stream,
488 node->local.inline_summary.estimated_self_stack_size);
489 lto_output_sleb128_stream (ob->main_stream,
490 node->local.inline_summary.self_size);
491 lto_output_sleb128_stream (ob->main_stream,
492 node->local.inline_summary.size_inlining_benefit);
493 lto_output_sleb128_stream (ob->main_stream,
494 node->local.inline_summary.self_time);
495 lto_output_sleb128_stream (ob->main_stream,
496 node->local.inline_summary.time_inlining_benefit);
497 if (node->global.inlined_to)
499 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
500 gcc_assert (ref != LCC_NOT_FOUND);
502 else
503 ref = LCC_NOT_FOUND;
505 lto_output_sleb128_stream (ob->main_stream, ref);
508 if (node->same_comdat_group && !boundary_p)
510 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
511 gcc_assert (ref != LCC_NOT_FOUND);
513 else
514 ref = LCC_NOT_FOUND;
515 lto_output_sleb128_stream (ob->main_stream, ref);
517 if (node->same_body)
519 struct cgraph_node *alias;
520 unsigned long alias_count = 1;
521 for (alias = node->same_body; alias->next; alias = alias->next)
522 alias_count++;
523 lto_output_uleb128_stream (ob->main_stream, alias_count);
526 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
527 alias->decl);
528 if (alias->thunk.thunk_p)
530 lto_output_uleb128_stream
531 (ob->main_stream,
532 1 + (alias->thunk.this_adjusting != 0) * 2
533 + (alias->thunk.virtual_offset_p != 0) * 4);
534 lto_output_uleb128_stream (ob->main_stream,
535 alias->thunk.fixed_offset);
536 lto_output_uleb128_stream (ob->main_stream,
537 alias->thunk.virtual_value);
538 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
539 alias->thunk.alias);
541 else
543 lto_output_uleb128_stream (ob->main_stream, 0);
544 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
545 alias->thunk.alias);
547 alias = alias->previous;
549 while (alias);
551 else
552 lto_output_uleb128_stream (ob->main_stream, 0);
555 /* Output the varpool NODE to OB.
556 If NODE is not in SET, then NODE is a boundary. */
558 static void
559 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
560 lto_varpool_encoder_t varpool_encoder,
561 cgraph_node_set set, varpool_node_set vset)
563 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
564 struct bitpack_d *bp;
565 struct varpool_node *alias;
566 int count = 0;
567 int ref;
569 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
570 bp = bitpack_create ();
571 bp_pack_value (bp, node->externally_visible, 1);
572 bp_pack_value (bp, node->force_output, 1);
573 bp_pack_value (bp, node->finalized, 1);
574 bp_pack_value (bp, node->alias, 1);
575 gcc_assert (!node->alias || !node->extra_name);
576 gcc_assert (node->finalized || !node->analyzed);
577 gcc_assert (node->needed);
578 /* Constant pool initializers can be de-unified into individual ltrans units.
579 FIXME: Alternatively at -Os we may want to avoid generating for them the local
580 labels and share them across LTRANS partitions. */
581 if (DECL_IN_CONSTANT_POOL (node->decl))
583 bp_pack_value (bp, 0, 1); /* used_from_other_parition. */
584 bp_pack_value (bp, 0, 1); /* in_other_partition. */
586 else
588 bp_pack_value (bp, node->analyzed
589 && referenced_from_other_partition_p (&node->ref_list,
590 set, vset), 1);
591 bp_pack_value (bp, boundary_p, 1); /* in_other_partition. */
593 /* Also emit any extra name aliases. */
594 for (alias = node->extra_name; alias; alias = alias->next)
595 count++;
596 bp_pack_value (bp, count != 0, 1);
597 lto_output_bitpack (ob->main_stream, bp);
598 bitpack_delete (bp);
599 if (node->same_comdat_group && !boundary_p)
601 ref = lto_varpool_encoder_lookup (varpool_encoder, node->same_comdat_group);
602 gcc_assert (ref != LCC_NOT_FOUND);
604 else
605 ref = LCC_NOT_FOUND;
606 lto_output_sleb128_stream (ob->main_stream, ref);
608 if (count)
610 lto_output_uleb128_stream (ob->main_stream, count);
611 for (alias = node->extra_name; alias; alias = alias->next)
612 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
616 /* Output the varpool NODE to OB.
617 If NODE is not in SET, then NODE is a boundary. */
619 static void
620 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
621 lto_cgraph_encoder_t encoder,
622 lto_varpool_encoder_t varpool_encoder)
624 struct bitpack_d *bp = bitpack_create ();
625 bp_pack_value (bp, ref->refered_type, 1);
626 bp_pack_value (bp, ref->use, 2);
627 lto_output_bitpack (ob->main_stream, bp);
628 bitpack_delete (bp);
629 if (ref->refered_type == IPA_REF_CGRAPH)
631 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
632 gcc_assert (nref != LCC_NOT_FOUND);
633 lto_output_sleb128_stream (ob->main_stream, nref);
635 else
637 int nref = lto_varpool_encoder_lookup (varpool_encoder,
638 ipa_ref_varpool_node (ref));
639 gcc_assert (nref != LCC_NOT_FOUND);
640 lto_output_sleb128_stream (ob->main_stream, nref);
644 /* Stream out profile_summary to OB. */
646 static void
647 output_profile_summary (struct lto_simple_output_block *ob)
649 if (profile_info)
651 /* We do not output num, it is not terribly useful. */
652 gcc_assert (profile_info->runs);
653 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
654 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
655 lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
656 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
658 else
659 lto_output_uleb128_stream (ob->main_stream, 0);
662 /* Add NODE into encoder as well as nodes it is cloned from.
663 Do it in a way so clones appear first. */
665 static void
666 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node,
667 bool include_body)
669 if (node->clone_of)
670 add_node_to (encoder, node->clone_of, include_body);
671 else if (include_body)
672 lto_set_cgraph_encoder_encode_body (encoder, node);
673 lto_cgraph_encoder_encode (encoder, node);
676 /* Add all references in LIST to encoders. */
678 static void
679 add_references (lto_cgraph_encoder_t encoder,
680 lto_varpool_encoder_t varpool_encoder,
681 struct ipa_ref_list *list)
683 int i;
684 struct ipa_ref *ref;
685 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
686 if (ref->refered_type == IPA_REF_CGRAPH)
687 add_node_to (encoder, ipa_ref_node (ref), false);
688 else
690 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
691 lto_varpool_encoder_encode (varpool_encoder, vnode);
695 /* Output all callees or indirect outgoing edges. EDGE must be the first such
696 edge. */
698 static void
699 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
700 struct lto_simple_output_block *ob,
701 lto_cgraph_encoder_t encoder)
703 if (!edge)
704 return;
706 /* Output edges in backward direction, so the reconstructed callgraph match
707 and it is easy to associate call sites in the IPA pass summaries. */
708 while (edge->next_callee)
709 edge = edge->next_callee;
710 for (; edge; edge = edge->prev_callee)
711 lto_output_edge (ob, edge, encoder);
714 /* Output the part of the cgraph in SET. */
716 static void
717 output_refs (cgraph_node_set set, varpool_node_set vset,
718 lto_cgraph_encoder_t encoder,
719 lto_varpool_encoder_t varpool_encoder)
721 cgraph_node_set_iterator csi;
722 varpool_node_set_iterator vsi;
723 struct lto_simple_output_block *ob;
724 int count;
725 struct ipa_ref *ref;
726 int i;
728 ob = lto_create_simple_output_block (LTO_section_refs);
730 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
732 struct cgraph_node *node = csi_node (csi);
734 count = ipa_ref_list_nreferences (&node->ref_list);
735 if (count)
737 lto_output_uleb128_stream (ob->main_stream, count);
738 lto_output_uleb128_stream (ob->main_stream,
739 lto_cgraph_encoder_lookup (encoder, node));
740 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
741 lto_output_ref (ob, ref, encoder, varpool_encoder);
745 lto_output_uleb128_stream (ob->main_stream, 0);
747 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
749 struct varpool_node *node = vsi_node (vsi);
751 count = ipa_ref_list_nreferences (&node->ref_list);
752 if (count)
754 lto_output_uleb128_stream (ob->main_stream, count);
755 lto_output_uleb128_stream (ob->main_stream,
756 lto_varpool_encoder_lookup (varpool_encoder,
757 node));
758 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
759 lto_output_ref (ob, ref, encoder, varpool_encoder);
763 lto_output_uleb128_stream (ob->main_stream, 0);
765 lto_destroy_simple_output_block (ob);
768 /* Find out all cgraph and varpool nodes we want to encode in current unit
769 and insert them to encoders. */
770 void
771 compute_ltrans_boundary (struct lto_out_decl_state *state,
772 cgraph_node_set set, varpool_node_set vset)
774 struct cgraph_node *node;
775 cgraph_node_set_iterator csi;
776 varpool_node_set_iterator vsi;
777 struct cgraph_edge *edge;
778 int i;
779 lto_cgraph_encoder_t encoder;
780 lto_varpool_encoder_t varpool_encoder;
782 encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
783 varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
785 /* Go over all the nodes in SET and assign references. */
786 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
788 node = csi_node (csi);
789 add_node_to (encoder, node, true);
790 add_references (encoder, varpool_encoder, &node->ref_list);
792 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
794 struct varpool_node *vnode = vsi_node (vsi);
795 gcc_assert (!vnode->alias);
796 lto_varpool_encoder_encode (varpool_encoder, vnode);
797 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
798 add_references (encoder, varpool_encoder, &vnode->ref_list);
800 /* Pickle in also the initializer of all referenced readonly variables
801 to help folding. Constant pool variables are not shared, so we must
802 pickle those too. */
803 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
805 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
806 if (DECL_INITIAL (vnode->decl)
807 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
808 vnode)
809 && (DECL_IN_CONSTANT_POOL (vnode->decl)
810 || TREE_READONLY (vnode->decl)))
812 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
813 add_references (encoder, varpool_encoder, &vnode->ref_list);
817 /* Go over all the nodes again to include callees that are not in
818 SET. */
819 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
821 node = csi_node (csi);
822 for (edge = node->callees; edge; edge = edge->next_callee)
824 struct cgraph_node *callee = edge->callee;
825 if (!cgraph_node_in_set_p (callee, set))
827 /* We should have moved all the inlines. */
828 gcc_assert (!callee->global.inlined_to);
829 add_node_to (encoder, callee, false);
835 /* Output the part of the cgraph in SET. */
837 void
838 output_cgraph (cgraph_node_set set, varpool_node_set vset)
840 struct cgraph_node *node;
841 struct lto_simple_output_block *ob;
842 cgraph_node_set_iterator csi;
843 int i, n_nodes;
844 lto_cgraph_encoder_t encoder;
845 lto_varpool_encoder_t varpool_encoder;
846 struct cgraph_asm_node *can;
848 if (flag_wpa)
849 output_cgraph_opt_summary ();
851 ob = lto_create_simple_output_block (LTO_section_cgraph);
853 output_profile_summary (ob);
855 /* An encoder for cgraph nodes should have been created by
856 ipa_write_summaries_1. */
857 gcc_assert (ob->decl_state->cgraph_node_encoder);
858 gcc_assert (ob->decl_state->varpool_node_encoder);
859 encoder = ob->decl_state->cgraph_node_encoder;
860 varpool_encoder = ob->decl_state->varpool_node_encoder;
862 /* Write out the nodes. We must first output a node and then its clones,
863 otherwise at a time reading back the node there would be nothing to clone
864 from. */
865 n_nodes = lto_cgraph_encoder_size (encoder);
866 for (i = 0; i < n_nodes; i++)
868 node = lto_cgraph_encoder_deref (encoder, i);
869 lto_output_node (ob, node, encoder, set, vset);
872 /* Go over the nodes in SET again to write edges. */
873 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
875 node = csi_node (csi);
876 output_outgoing_cgraph_edges (node->callees, ob, encoder);
877 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
880 lto_output_uleb128_stream (ob->main_stream, 0);
882 /* Emit toplevel asms. */
883 for (can = cgraph_asm_nodes; can; can = can->next)
885 int len = TREE_STRING_LENGTH (can->asm_str);
886 lto_output_uleb128_stream (ob->main_stream, len);
887 for (i = 0; i < len; ++i)
888 lto_output_1_stream (ob->main_stream,
889 TREE_STRING_POINTER (can->asm_str)[i]);
892 lto_output_uleb128_stream (ob->main_stream, 0);
894 lto_destroy_simple_output_block (ob);
895 output_varpool (set, vset);
896 output_refs (set, vset, encoder, varpool_encoder);
899 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
900 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
901 NODE or to replace the values in it, for instance because the first
902 time we saw it, the function body was not available but now it
903 is. BP is a bitpack with all the bitflags for NODE read from the
904 stream. */
906 static void
907 input_overwrite_node (struct lto_file_decl_data *file_data,
908 struct cgraph_node *node,
909 enum LTO_cgraph_tags tag,
910 struct bitpack_d *bp,
911 unsigned int stack_size,
912 unsigned int self_time,
913 unsigned int time_inlining_benefit,
914 unsigned int self_size,
915 unsigned int size_inlining_benefit)
917 node->aux = (void *) tag;
918 node->local.inline_summary.estimated_self_stack_size = stack_size;
919 node->local.inline_summary.self_time = self_time;
920 node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
921 node->local.inline_summary.self_size = self_size;
922 node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
923 node->global.time = self_time;
924 node->global.size = self_size;
925 node->global.estimated_stack_size = stack_size;
926 node->global.estimated_growth = INT_MIN;
927 node->local.lto_file_data = file_data;
929 node->local.local = bp_unpack_value (bp, 1);
930 node->local.externally_visible = bp_unpack_value (bp, 1);
931 node->local.finalized = bp_unpack_value (bp, 1);
932 node->local.inlinable = bp_unpack_value (bp, 1);
933 node->local.versionable = bp_unpack_value (bp, 1);
934 node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
935 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
936 node->local.vtable_method = bp_unpack_value (bp, 1);
937 node->needed = bp_unpack_value (bp, 1);
938 node->address_taken = bp_unpack_value (bp, 1);
939 node->abstract_and_needed = bp_unpack_value (bp, 1);
940 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
941 node->lowered = bp_unpack_value (bp, 1);
942 node->analyzed = tag == LTO_cgraph_analyzed_node;
943 node->in_other_partition = bp_unpack_value (bp, 1);
944 node->alias = bp_unpack_value (bp, 1);
945 node->finalized_by_frontend = bp_unpack_value (bp, 1);
946 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
949 /* Output the part of the cgraph in SET. */
951 static void
952 output_varpool (cgraph_node_set set, varpool_node_set vset)
954 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
955 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
956 int len = lto_varpool_encoder_size (varpool_encoder), i;
958 lto_output_uleb128_stream (ob->main_stream, len);
960 /* Write out the nodes. We must first output a node and then its clones,
961 otherwise at a time reading back the node there would be nothing to clone
962 from. */
963 for (i = 0; i < len; i++)
965 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
966 varpool_encoder,
967 set, vset);
970 lto_destroy_simple_output_block (ob);
973 /* Read a node from input_block IB. TAG is the node's tag just read.
974 Return the node read or overwriten. */
976 static struct cgraph_node *
977 input_node (struct lto_file_decl_data *file_data,
978 struct lto_input_block *ib,
979 enum LTO_cgraph_tags tag,
980 VEC(cgraph_node_ptr, heap) *nodes)
982 tree fn_decl;
983 struct cgraph_node *node;
984 struct bitpack_d *bp;
985 int stack_size = 0;
986 unsigned decl_index;
987 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
988 int self_time = 0;
989 int self_size = 0;
990 int time_inlining_benefit = 0;
991 int size_inlining_benefit = 0;
992 unsigned long same_body_count = 0;
993 int clone_ref;
995 clone_ref = lto_input_sleb128 (ib);
997 decl_index = lto_input_uleb128 (ib);
998 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1000 if (clone_ref != LCC_NOT_FOUND)
1002 node = cgraph_clone_node (VEC_index (cgraph_node_ptr, nodes, clone_ref), fn_decl,
1003 0, CGRAPH_FREQ_BASE, 0, false, NULL);
1005 else
1006 node = cgraph_node (fn_decl);
1008 node->count = lto_input_sleb128 (ib);
1009 bp = lto_input_bitpack (ib);
1011 if (tag == LTO_cgraph_analyzed_node)
1013 stack_size = lto_input_sleb128 (ib);
1014 self_size = lto_input_sleb128 (ib);
1015 size_inlining_benefit = lto_input_sleb128 (ib);
1016 self_time = lto_input_sleb128 (ib);
1017 time_inlining_benefit = lto_input_sleb128 (ib);
1019 ref = lto_input_sleb128 (ib);
1022 ref2 = lto_input_sleb128 (ib);
1023 same_body_count = lto_input_uleb128 (ib);
1025 /* Make sure that we have not read this node before. Nodes that
1026 have already been read will have their tag stored in the 'aux'
1027 field. Since built-in functions can be referenced in multiple
1028 functions, they are expected to be read more than once. */
1029 if (node->aux && !DECL_IS_BUILTIN (node->decl))
1030 internal_error ("bytecode stream: found multiple instances of cgraph "
1031 "node %d", node->uid);
1033 input_overwrite_node (file_data, node, tag, bp, stack_size, self_time,
1034 time_inlining_benefit, self_size,
1035 size_inlining_benefit);
1036 bitpack_delete (bp);
1038 /* Store a reference for now, and fix up later to be a pointer. */
1039 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1041 /* Store a reference for now, and fix up later to be a pointer. */
1042 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1044 while (same_body_count-- > 0)
1046 tree alias_decl;
1047 int type;
1048 decl_index = lto_input_uleb128 (ib);
1049 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1050 type = lto_input_uleb128 (ib);
1051 if (!type)
1053 tree real_alias;
1054 decl_index = lto_input_uleb128 (ib);
1055 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1056 cgraph_same_body_alias (alias_decl, real_alias);
1058 else
1060 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1061 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1062 tree real_alias;
1063 decl_index = lto_input_uleb128 (ib);
1064 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1065 cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
1066 virtual_value,
1067 (type & 4) ? size_int (virtual_value) : NULL_TREE,
1068 real_alias);
1071 return node;
1074 /* Read a node from input_block IB. TAG is the node's tag just read.
1075 Return the node read or overwriten. */
1077 static struct varpool_node *
1078 input_varpool_node (struct lto_file_decl_data *file_data,
1079 struct lto_input_block *ib)
1081 int decl_index;
1082 tree var_decl;
1083 struct varpool_node *node;
1084 struct bitpack_d *bp;
1085 bool aliases_p;
1086 int count;
1087 int ref = LCC_NOT_FOUND;
1089 decl_index = lto_input_uleb128 (ib);
1090 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1091 node = varpool_node (var_decl);
1092 node->lto_file_data = file_data;
1094 bp = lto_input_bitpack (ib);
1095 node->externally_visible = bp_unpack_value (bp, 1);
1096 node->force_output = bp_unpack_value (bp, 1);
1097 node->finalized = bp_unpack_value (bp, 1);
1098 node->alias = bp_unpack_value (bp, 1);
1099 node->analyzed = node->finalized;
1100 node->used_from_other_partition = bp_unpack_value (bp, 1);
1101 node->in_other_partition = bp_unpack_value (bp, 1);
1102 aliases_p = bp_unpack_value (bp, 1);
1103 if (node->finalized)
1104 varpool_mark_needed_node (node);
1105 bitpack_delete (bp);
1106 ref = lto_input_sleb128 (ib);
1107 /* Store a reference for now, and fix up later to be a pointer. */
1108 node->same_comdat_group = (struct varpool_node *) (intptr_t) ref;
1109 if (aliases_p)
1111 count = lto_input_uleb128 (ib);
1112 for (; count > 0; count --)
1114 tree decl = lto_file_decl_data_get_var_decl (file_data,
1115 lto_input_uleb128 (ib));
1116 varpool_extra_name_alias (decl, var_decl);
1119 return node;
1122 /* Read a node from input_block IB. TAG is the node's tag just read.
1123 Return the node read or overwriten. */
1125 static void
1126 input_ref (struct lto_input_block *ib,
1127 struct cgraph_node *refering_node,
1128 struct varpool_node *refering_varpool_node,
1129 VEC(cgraph_node_ptr, heap) *nodes,
1130 VEC(varpool_node_ptr, heap) *varpool_nodes)
1132 struct cgraph_node *node = NULL;
1133 struct varpool_node *varpool_node = NULL;
1134 struct bitpack_d *bp;
1135 enum ipa_ref_type type;
1136 enum ipa_ref_use use;
1138 bp = lto_input_bitpack (ib);
1139 type = (enum ipa_ref_type) bp_unpack_value (bp, 1);
1140 use = (enum ipa_ref_use) bp_unpack_value (bp, 2);
1141 bitpack_delete (bp);
1142 if (type == IPA_REF_CGRAPH)
1143 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1144 else
1145 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1146 ipa_record_reference (refering_node, refering_varpool_node,
1147 node, varpool_node, use, NULL);
1150 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1151 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1152 edge being read is indirect (in the sense that it has
1153 indirect_unknown_callee set). */
1155 static void
1156 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1157 bool indirect)
1159 struct cgraph_node *caller, *callee;
1160 struct cgraph_edge *edge;
1161 unsigned int stmt_id;
1162 gcov_type count;
1163 int freq;
1164 unsigned int nest;
1165 cgraph_inline_failed_t inline_failed;
1166 struct bitpack_d *bp;
1167 enum ld_plugin_symbol_resolution caller_resolution;
1168 int ecf_flags = 0;
1170 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1171 if (caller == NULL || caller->decl == NULL_TREE)
1172 internal_error ("bytecode stream: no caller found while reading edge");
1174 if (!indirect)
1176 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1177 if (callee == NULL || callee->decl == NULL_TREE)
1178 internal_error ("bytecode stream: no callee found while reading edge");
1180 else
1181 callee = NULL;
1183 count = (gcov_type) lto_input_sleb128 (ib);
1185 bp = lto_input_bitpack (ib);
1186 stmt_id = (unsigned int) bp_unpack_value (bp, HOST_BITS_PER_INT);
1187 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (bp,
1188 HOST_BITS_PER_INT);
1189 freq = (int) bp_unpack_value (bp, HOST_BITS_PER_INT);
1190 nest = (unsigned) bp_unpack_value (bp, 30);
1192 /* If the caller was preempted, don't create the edge.
1193 ??? Should we ever have edges from a preempted caller? */
1194 caller_resolution = lto_symtab_get_resolution (caller->decl);
1195 if (caller_resolution == LDPR_PREEMPTED_REG
1196 || caller_resolution == LDPR_PREEMPTED_IR)
1197 return;
1199 if (indirect)
1200 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq, nest);
1201 else
1202 edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
1204 edge->indirect_inlining_edge = bp_unpack_value (bp, 1);
1205 edge->lto_stmt_uid = stmt_id;
1206 edge->inline_failed = inline_failed;
1207 edge->call_stmt_cannot_inline_p = bp_unpack_value (bp, 1);
1208 edge->can_throw_external = bp_unpack_value (bp, 1);
1209 if (indirect)
1211 if (bp_unpack_value (bp, 1))
1212 ecf_flags |= ECF_CONST;
1213 if (bp_unpack_value (bp, 1))
1214 ecf_flags |= ECF_PURE;
1215 if (bp_unpack_value (bp, 1))
1216 ecf_flags |= ECF_NORETURN;
1217 if (bp_unpack_value (bp, 1))
1218 ecf_flags |= ECF_MALLOC;
1219 if (bp_unpack_value (bp, 1))
1220 ecf_flags |= ECF_NOTHROW;
1221 if (bp_unpack_value (bp, 1))
1222 ecf_flags |= ECF_RETURNS_TWICE;
1223 edge->indirect_info->ecf_flags = ecf_flags;
1225 bitpack_delete (bp);
1229 /* Read a cgraph from IB using the info in FILE_DATA. */
1231 static VEC(cgraph_node_ptr, heap) *
1232 input_cgraph_1 (struct lto_file_decl_data *file_data,
1233 struct lto_input_block *ib)
1235 enum LTO_cgraph_tags tag;
1236 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1237 struct cgraph_node *node;
1238 unsigned i;
1239 unsigned HOST_WIDE_INT len;
1241 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1242 while (tag)
1244 if (tag == LTO_cgraph_edge)
1245 input_edge (ib, nodes, false);
1246 else if (tag == LTO_cgraph_indirect_edge)
1247 input_edge (ib, nodes, true);
1248 else
1250 node = input_node (file_data, ib, tag,nodes);
1251 if (node == NULL || node->decl == NULL_TREE)
1252 internal_error ("bytecode stream: found empty cgraph node");
1253 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1254 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1257 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1260 /* Input toplevel asms. */
1261 len = lto_input_uleb128 (ib);
1262 while (len)
1264 char *str = (char *)xmalloc (len + 1);
1265 for (i = 0; i < len; ++i)
1266 str[i] = lto_input_1_unsigned (ib);
1267 cgraph_add_asm_node (build_string (len, str));
1268 free (str);
1270 len = lto_input_uleb128 (ib);
1273 for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
1275 int ref = (int) (intptr_t) node->global.inlined_to;
1277 /* Fixup inlined_to from reference to pointer. */
1278 if (ref != LCC_NOT_FOUND)
1279 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1280 else
1281 node->global.inlined_to = NULL;
1283 ref = (int) (intptr_t) node->same_comdat_group;
1285 /* Fixup same_comdat_group from reference to pointer. */
1286 if (ref != LCC_NOT_FOUND)
1287 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1288 else
1289 node->same_comdat_group = NULL;
1291 return nodes;
1294 /* Read a varpool from IB using the info in FILE_DATA. */
1296 static VEC(varpool_node_ptr, heap) *
1297 input_varpool_1 (struct lto_file_decl_data *file_data,
1298 struct lto_input_block *ib)
1300 unsigned HOST_WIDE_INT len;
1301 VEC(varpool_node_ptr, heap) *varpool = NULL;
1302 int i;
1303 struct varpool_node *node;
1305 len = lto_input_uleb128 (ib);
1306 while (len)
1308 VEC_safe_push (varpool_node_ptr, heap, varpool,
1309 input_varpool_node (file_data, ib));
1310 len--;
1312 for (i = 0; VEC_iterate (varpool_node_ptr, varpool, i, node); i++)
1314 int ref = (int) (intptr_t) node->same_comdat_group;
1316 /* Fixup same_comdat_group from reference to pointer. */
1317 if (ref != LCC_NOT_FOUND)
1318 node->same_comdat_group = VEC_index (varpool_node_ptr, varpool, ref);
1319 else
1320 node->same_comdat_group = NULL;
1322 return varpool;
1325 /* Input ipa_refs. */
1327 static void
1328 input_refs (struct lto_input_block *ib,
1329 VEC(cgraph_node_ptr, heap) *nodes,
1330 VEC(varpool_node_ptr, heap) *varpool)
1332 int count;
1333 int idx;
1334 while (true)
1336 struct cgraph_node *node;
1337 count = lto_input_uleb128 (ib);
1338 if (!count)
1339 break;
1340 idx = lto_input_uleb128 (ib);
1341 node = VEC_index (cgraph_node_ptr, nodes, idx);
1342 while (count)
1344 input_ref (ib, node, NULL, nodes, varpool);
1345 count--;
1348 while (true)
1350 struct varpool_node *node;
1351 count = lto_input_uleb128 (ib);
1352 if (!count)
1353 break;
1354 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1355 while (count)
1357 input_ref (ib, NULL, node, nodes, varpool);
1358 count--;
1364 static struct gcov_ctr_summary lto_gcov_summary;
1366 /* Input profile_info from IB. */
1367 static void
1368 input_profile_summary (struct lto_input_block *ib)
1370 unsigned int runs = lto_input_uleb128 (ib);
1371 if (runs)
1373 if (!profile_info)
1375 profile_info = &lto_gcov_summary;
1376 lto_gcov_summary.runs = runs;
1377 lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
1378 lto_gcov_summary.run_max = lto_input_sleb128 (ib);
1379 lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
1381 /* We can support this by scaling all counts to nearest common multiple
1382 of all different runs, but it is perhaps not worth the effort. */
1383 else if (profile_info->runs != runs
1384 || profile_info->sum_all != lto_input_sleb128 (ib)
1385 || profile_info->run_max != lto_input_sleb128 (ib)
1386 || profile_info->sum_max != lto_input_sleb128 (ib))
1387 sorry ("Combining units with different profiles is not supported.");
1388 /* We allow some units to have profile and other to not have one. This will
1389 just make unprofiled units to be size optimized that is sane. */
1394 /* Input and merge the cgraph from each of the .o files passed to
1395 lto1. */
1397 void
1398 input_cgraph (void)
1400 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1401 struct lto_file_decl_data *file_data;
1402 unsigned int j = 0;
1403 struct cgraph_node *node;
1405 while ((file_data = file_data_vec[j++]))
1407 const char *data;
1408 size_t len;
1409 struct lto_input_block *ib;
1410 VEC(cgraph_node_ptr, heap) *nodes;
1411 VEC(varpool_node_ptr, heap) *varpool;
1413 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1414 &data, &len);
1415 input_profile_summary (ib);
1416 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1417 nodes = input_cgraph_1 (file_data, ib);
1418 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1419 ib, data, len);
1421 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1422 &data, &len);
1423 varpool = input_varpool_1 (file_data, ib);
1424 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1425 ib, data, len);
1427 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1428 &data, &len);
1429 input_refs (ib, nodes, varpool);
1430 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1431 ib, data, len);
1432 if (flag_ltrans)
1433 input_cgraph_opt_summary (nodes);
1434 VEC_free (cgraph_node_ptr, heap, nodes);
1435 VEC_free (varpool_node_ptr, heap, varpool);
1438 /* Clear out the aux field that was used to store enough state to
1439 tell which nodes should be overwritten. */
1440 for (node = cgraph_nodes; node; node = node->next)
1442 /* Some nodes may have been created by cgraph_node. This
1443 happens when the callgraph contains nested functions. If the
1444 node for the parent function was never emitted to the gimple
1445 file, cgraph_node will create a node for it when setting the
1446 context of the nested function. */
1447 if (node->local.lto_file_data)
1448 node->aux = NULL;
1452 /* True when we need optimization summary for NODE. */
1454 static int
1455 output_cgraph_opt_summary_p (struct cgraph_node *node)
1457 if (!node->clone_of)
1458 return false;
1459 return (node->clone.tree_map
1460 || node->clone.args_to_skip
1461 || node->clone.combined_args_to_skip);
1464 /* Output optimization summary for NODE to OB. */
1466 static void
1467 output_node_opt_summary (struct output_block *ob,
1468 struct cgraph_node *node)
1470 unsigned int index;
1471 bitmap_iterator bi;
1472 struct ipa_replace_map *map;
1473 struct bitpack_d *bp;
1474 int i;
1476 lto_output_uleb128_stream (ob->main_stream,
1477 bitmap_count_bits (node->clone.args_to_skip));
1478 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1479 lto_output_uleb128_stream (ob->main_stream, index);
1480 lto_output_uleb128_stream (ob->main_stream,
1481 bitmap_count_bits (node->clone.combined_args_to_skip));
1482 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1483 lto_output_uleb128_stream (ob->main_stream, index);
1484 lto_output_uleb128_stream (ob->main_stream,
1485 VEC_length (ipa_replace_map_p, node->clone.tree_map));
1486 for (i = 0; VEC_iterate (ipa_replace_map_p, node->clone.tree_map, i, map); i++)
1488 int parm_num;
1489 tree parm;
1491 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm;
1492 parm = TREE_CHAIN (parm), parm_num++)
1493 if (map->old_tree == parm)
1494 break;
1495 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1496 mechanism to store function local declarations into summaries. */
1497 gcc_assert (parm);
1498 lto_output_uleb128_stream (ob->main_stream, parm_num);
1499 lto_output_tree (ob, map->new_tree, true);
1500 bp = bitpack_create ();
1501 bp_pack_value (bp, map->replace_p, 1);
1502 bp_pack_value (bp, map->ref_p, 1);
1503 lto_output_bitpack (ob->main_stream, bp);
1504 bitpack_delete (bp);
1508 /* Output optimization summaries stored in callgraph.
1509 At the moment it is the clone info structure. */
1511 static void
1512 output_cgraph_opt_summary (void)
1514 struct cgraph_node *node;
1515 int i, n_nodes;
1516 lto_cgraph_encoder_t encoder;
1517 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1518 unsigned count = 0;
1520 ob->cgraph_node = NULL;
1521 encoder = ob->decl_state->cgraph_node_encoder;
1522 n_nodes = lto_cgraph_encoder_size (encoder);
1523 for (i = 0; i < n_nodes; i++)
1524 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder, i)))
1525 count++;
1526 lto_output_uleb128_stream (ob->main_stream, count);
1527 for (i = 0; i < n_nodes; i++)
1529 node = lto_cgraph_encoder_deref (encoder, i);
1530 if (output_cgraph_opt_summary_p (node))
1532 lto_output_uleb128_stream (ob->main_stream, i);
1533 output_node_opt_summary (ob, node);
1536 produce_asm (ob, NULL);
1537 destroy_output_block (ob);
1540 /* Input optimiation summary of NODE. */
1542 static void
1543 input_node_opt_summary (struct cgraph_node *node,
1544 struct lto_input_block *ib_main,
1545 struct data_in *data_in)
1547 int i;
1548 int count;
1549 int bit;
1550 struct bitpack_d *bp;
1552 count = lto_input_uleb128 (ib_main);
1553 if (count)
1554 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1555 for (i = 0; i < count; i++)
1557 bit = lto_input_uleb128 (ib_main);
1558 bitmap_set_bit (node->clone.args_to_skip, bit);
1560 count = lto_input_uleb128 (ib_main);
1561 if (count)
1562 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1563 for (i = 0; i < count; i++)
1565 bit = lto_input_uleb128 (ib_main);
1566 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1568 count = lto_input_uleb128 (ib_main);
1569 for (i = 0; i < count; i++)
1571 int parm_num;
1572 tree parm;
1573 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1575 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1576 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm_num;
1577 parm = TREE_CHAIN (parm))
1578 parm_num --;
1579 map->parm_num = lto_input_uleb128 (ib_main);
1580 map->old_tree = NULL;
1581 map->new_tree = lto_input_tree (ib_main, data_in);
1582 bp = lto_input_bitpack (ib_main);
1583 map->replace_p = bp_unpack_value (bp, 1);
1584 map->ref_p = bp_unpack_value (bp, 1);
1585 bitpack_delete (bp);
1589 /* Read section in file FILE_DATA of length LEN with data DATA. */
1591 static void
1592 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1593 const char *data, size_t len, VEC (cgraph_node_ptr,
1594 heap) * nodes)
1596 const struct lto_function_header *header =
1597 (const struct lto_function_header *) data;
1598 const int32_t cfg_offset = sizeof (struct lto_function_header);
1599 const int32_t main_offset = cfg_offset + header->cfg_size;
1600 const int32_t string_offset = main_offset + header->main_size;
1601 struct data_in *data_in;
1602 struct lto_input_block ib_main;
1603 unsigned int i;
1604 unsigned int count;
1606 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1607 header->main_size);
1609 data_in =
1610 lto_data_in_create (file_data, (const char *) data + string_offset,
1611 header->string_size, NULL);
1612 count = lto_input_uleb128 (&ib_main);
1614 for (i = 0; i < count; i++)
1616 int ref = lto_input_uleb128 (&ib_main);
1617 input_node_opt_summary (VEC_index (cgraph_node_ptr, nodes, ref),
1618 &ib_main, data_in);
1620 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
1621 len);
1622 lto_data_in_delete (data_in);
1625 /* Input optimization summary of cgraph. */
1627 static void
1628 input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes)
1630 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1631 struct lto_file_decl_data *file_data;
1632 unsigned int j = 0;
1634 while ((file_data = file_data_vec[j++]))
1636 size_t len;
1637 const char *data =
1638 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1639 &len);
1641 if (data)
1642 input_cgraph_opt_section (file_data, data, len, nodes);