2011-05-31 Gabriel Charette <gchare@google.com>
[official-gcc.git] / gcc / lto-cgraph.c
blob22f44ad978392ad31c34e3f1f70abaad7618be9b
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
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
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 "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "langhooks.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "timevar.h"
43 #include "output.h"
44 #include "pointer-set.h"
45 #include "lto-streamer.h"
46 #include "gcov-io.h"
48 static void output_varpool (cgraph_node_set, varpool_node_set);
49 static void output_cgraph_opt_summary (cgraph_node_set set);
50 static void input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes);
53 /* Cgraph streaming is organized as set of record whose type
54 is indicated by a tag. */
55 enum LTO_cgraph_tags
57 /* Must leave 0 for the stopper. */
59 /* Cgraph node without body available. */
60 LTO_cgraph_unavail_node = 1,
61 /* Cgraph node with function body. */
62 LTO_cgraph_analyzed_node,
63 /* Cgraph edges. */
64 LTO_cgraph_edge,
65 LTO_cgraph_indirect_edge
68 /* Create a new cgraph encoder. */
70 lto_cgraph_encoder_t
71 lto_cgraph_encoder_new (void)
73 lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
74 encoder->map = pointer_map_create ();
75 encoder->nodes = NULL;
76 encoder->body = pointer_set_create ();
77 return encoder;
81 /* Delete ENCODER and its components. */
83 void
84 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
86 VEC_free (cgraph_node_ptr, heap, encoder->nodes);
87 pointer_map_destroy (encoder->map);
88 pointer_set_destroy (encoder->body);
89 free (encoder);
93 /* Return the existing reference number of NODE in the cgraph encoder in
94 output block OB. Assign a new reference if this is the first time
95 NODE is encoded. */
97 int
98 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
99 struct cgraph_node *node)
101 int ref;
102 void **slot;
104 slot = pointer_map_contains (encoder->map, node);
105 if (!slot)
107 ref = VEC_length (cgraph_node_ptr, encoder->nodes);
108 slot = pointer_map_insert (encoder->map, node);
109 *slot = (void *) (intptr_t) ref;
110 VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
112 else
113 ref = (int) (intptr_t) *slot;
115 return ref;
118 #define LCC_NOT_FOUND (-1)
120 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
121 or LCC_NOT_FOUND if it is not there. */
124 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
125 struct cgraph_node *node)
127 void **slot = pointer_map_contains (encoder->map, node);
128 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
132 /* Return the cgraph node corresponding to REF using ENCODER. */
134 struct cgraph_node *
135 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
137 if (ref == LCC_NOT_FOUND)
138 return NULL;
140 return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
144 /* Return TRUE if we should encode initializer of NODE (if any). */
146 bool
147 lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t encoder,
148 struct cgraph_node *node)
150 return pointer_set_contains (encoder->body, node);
153 /* Return TRUE if we should encode body of NODE (if any). */
155 static void
156 lto_set_cgraph_encoder_encode_body (lto_cgraph_encoder_t encoder,
157 struct cgraph_node *node)
159 pointer_set_insert (encoder->body, node);
162 /* Create a new varpool encoder. */
164 lto_varpool_encoder_t
165 lto_varpool_encoder_new (void)
167 lto_varpool_encoder_t encoder = XCNEW (struct lto_varpool_encoder_d);
168 encoder->map = pointer_map_create ();
169 encoder->initializer = pointer_set_create ();
170 encoder->nodes = NULL;
171 return encoder;
175 /* Delete ENCODER and its components. */
177 void
178 lto_varpool_encoder_delete (lto_varpool_encoder_t encoder)
180 VEC_free (varpool_node_ptr, heap, encoder->nodes);
181 pointer_map_destroy (encoder->map);
182 pointer_set_destroy (encoder->initializer);
183 free (encoder);
187 /* Return the existing reference number of NODE in the varpool encoder in
188 output block OB. Assign a new reference if this is the first time
189 NODE is encoded. */
192 lto_varpool_encoder_encode (lto_varpool_encoder_t encoder,
193 struct varpool_node *node)
195 int ref;
196 void **slot;
198 slot = pointer_map_contains (encoder->map, node);
199 if (!slot)
201 ref = VEC_length (varpool_node_ptr, encoder->nodes);
202 slot = pointer_map_insert (encoder->map, node);
203 *slot = (void *) (intptr_t) ref;
204 VEC_safe_push (varpool_node_ptr, heap, encoder->nodes, node);
206 else
207 ref = (int) (intptr_t) *slot;
209 return ref;
212 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
213 or LCC_NOT_FOUND if it is not there. */
216 lto_varpool_encoder_lookup (lto_varpool_encoder_t encoder,
217 struct varpool_node *node)
219 void **slot = pointer_map_contains (encoder->map, node);
220 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
224 /* Return the varpool node corresponding to REF using ENCODER. */
226 struct varpool_node *
227 lto_varpool_encoder_deref (lto_varpool_encoder_t encoder, int ref)
229 if (ref == LCC_NOT_FOUND)
230 return NULL;
232 return VEC_index (varpool_node_ptr, encoder->nodes, ref);
236 /* Return TRUE if we should encode initializer of NODE (if any). */
238 bool
239 lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t encoder,
240 struct varpool_node *node)
242 return pointer_set_contains (encoder->initializer, node);
245 /* Return TRUE if we should encode initializer of NODE (if any). */
247 static void
248 lto_set_varpool_encoder_encode_initializer (lto_varpool_encoder_t encoder,
249 struct varpool_node *node)
251 pointer_set_insert (encoder->initializer, node);
254 /* Output the cgraph EDGE to OB using ENCODER. */
256 static void
257 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
258 lto_cgraph_encoder_t encoder)
260 unsigned int uid;
261 intptr_t ref;
262 struct bitpack_d bp;
264 if (edge->indirect_unknown_callee)
265 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_indirect_edge);
266 else
267 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
269 ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
270 gcc_assert (ref != LCC_NOT_FOUND);
271 lto_output_sleb128_stream (ob->main_stream, ref);
273 if (!edge->indirect_unknown_callee)
275 ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
276 gcc_assert (ref != LCC_NOT_FOUND);
277 lto_output_sleb128_stream (ob->main_stream, ref);
280 lto_output_sleb128_stream (ob->main_stream, edge->count);
282 bp = bitpack_create (ob->main_stream);
283 uid = (!gimple_has_body_p (edge->caller->decl)
284 ? 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->indirect_inlining_edge, 1);
289 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
290 bp_pack_value (&bp, edge->can_throw_external, 1);
291 if (edge->indirect_unknown_callee)
293 int flags = edge->indirect_info->ecf_flags;
294 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
295 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
296 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
297 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
300 /* Flags that should not appear on indirect calls. */
301 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
302 | ECF_MAY_BE_ALLOCA
303 | ECF_SIBCALL
304 | ECF_LEAF
305 | ECF_NOVOPS)));
307 lto_output_bitpack (&bp);
310 /* Return if LIST contain references from other partitions. */
312 bool
313 referenced_from_other_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
314 varpool_node_set vset)
316 int i;
317 struct ipa_ref *ref;
318 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
320 if (ref->refering_type == IPA_REF_CGRAPH)
322 if (ipa_ref_refering_node (ref)->in_other_partition
323 || !cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
324 return true;
326 else
328 if (ipa_ref_refering_varpool_node (ref)->in_other_partition
329 || !varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
330 vset))
331 return true;
334 return false;
337 /* Return true when node is reachable from other partition. */
339 bool
340 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
342 struct cgraph_edge *e;
343 if (!node->analyzed)
344 return false;
345 if (node->global.inlined_to)
346 return false;
347 for (e = node->callers; e; e = e->next_caller)
348 if (e->caller->in_other_partition
349 || !cgraph_node_in_set_p (e->caller, set))
350 return true;
351 return false;
354 /* Return if LIST contain references from other partitions. */
356 bool
357 referenced_from_this_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
358 varpool_node_set vset)
360 int i;
361 struct ipa_ref *ref;
362 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
364 if (ref->refering_type == IPA_REF_CGRAPH)
366 if (cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
367 return true;
369 else
371 if (varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
372 vset))
373 return true;
376 return false;
379 /* Return true when node is reachable from other partition. */
381 bool
382 reachable_from_this_partition_p (struct cgraph_node *node, cgraph_node_set set)
384 struct cgraph_edge *e;
385 for (e = node->callers; e; e = e->next_caller)
386 if (cgraph_node_in_set_p (e->caller, set))
387 return true;
388 return false;
391 /* Output the cgraph NODE to OB. ENCODER is used to find the
392 reference number of NODE->inlined_to. SET is the set of nodes we
393 are writing to the current file. If NODE is not in SET, then NODE
394 is a boundary of a cgraph_node_set and we pretend NODE just has a
395 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
396 that have had their callgraph node written so far. This is used to
397 determine if NODE is a clone of a previously written node. */
399 static void
400 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
401 lto_cgraph_encoder_t encoder, cgraph_node_set set,
402 varpool_node_set vset)
404 unsigned int tag;
405 struct bitpack_d bp;
406 bool boundary_p;
407 intptr_t ref;
408 bool in_other_partition = false;
409 struct cgraph_node *clone_of;
411 boundary_p = !cgraph_node_in_set_p (node, set);
413 if (node->analyzed && !boundary_p)
414 tag = LTO_cgraph_analyzed_node;
415 else
416 tag = LTO_cgraph_unavail_node;
418 lto_output_uleb128_stream (ob->main_stream, tag);
420 /* In WPA mode, we only output part of the call-graph. Also, we
421 fake cgraph node attributes. There are two cases that we care.
423 Boundary nodes: There are nodes that are not part of SET but are
424 called from within SET. We artificially make them look like
425 externally visible nodes with no function body.
427 Cherry-picked nodes: These are nodes we pulled from other
428 translation units into SET during IPA-inlining. We make them as
429 local static nodes to prevent clashes with other local statics. */
430 if (boundary_p && node->analyzed)
432 /* Inline clones can not be part of boundary.
433 gcc_assert (!node->global.inlined_to);
435 FIXME: At the moment they can be, when partition contains an inline
436 clone that is clone of inline clone from outside partition. We can
437 reshape the clone tree and make other tree to be the root, but it
438 needs a bit extra work and will be promplty done by cgraph_remove_node
439 after reading back. */
440 in_other_partition = 1;
443 clone_of = node->clone_of;
444 while (clone_of
445 && (ref = lto_cgraph_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
446 if (clone_of->prev_sibling_clone)
447 clone_of = clone_of->prev_sibling_clone;
448 else
449 clone_of = clone_of->clone_of;
451 if (LTO_cgraph_analyzed_node)
452 gcc_assert (clone_of || !node->clone_of);
453 if (!clone_of)
454 lto_output_sleb128_stream (ob->main_stream, LCC_NOT_FOUND);
455 else
456 lto_output_sleb128_stream (ob->main_stream, ref);
459 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
460 lto_output_sleb128_stream (ob->main_stream, node->count);
461 lto_output_sleb128_stream (ob->main_stream, node->count_materialization_scale);
463 if (tag == LTO_cgraph_analyzed_node)
465 if (node->global.inlined_to)
467 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
468 gcc_assert (ref != LCC_NOT_FOUND);
470 else
471 ref = LCC_NOT_FOUND;
473 lto_output_sleb128_stream (ob->main_stream, ref);
476 if (node->same_comdat_group && !boundary_p)
478 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
479 gcc_assert (ref != LCC_NOT_FOUND);
481 else
482 ref = LCC_NOT_FOUND;
483 lto_output_sleb128_stream (ob->main_stream, ref);
485 bp = bitpack_create (ob->main_stream);
486 bp_pack_value (&bp, node->local.local, 1);
487 bp_pack_value (&bp, node->local.externally_visible, 1);
488 bp_pack_value (&bp, node->local.finalized, 1);
489 bp_pack_value (&bp, node->local.can_change_signature, 1);
490 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
491 bp_pack_value (&bp, node->needed, 1);
492 bp_pack_value (&bp, node->address_taken, 1);
493 bp_pack_value (&bp, node->abstract_and_needed, 1);
494 bp_pack_value (&bp, tag == LTO_cgraph_analyzed_node
495 && !DECL_EXTERNAL (node->decl)
496 && !DECL_COMDAT (node->decl)
497 && (reachable_from_other_partition_p (node, set)
498 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
499 bp_pack_value (&bp, node->lowered, 1);
500 bp_pack_value (&bp, in_other_partition, 1);
501 bp_pack_value (&bp, node->alias, 1);
502 bp_pack_value (&bp, node->frequency, 2);
503 bp_pack_value (&bp, node->only_called_at_startup, 1);
504 bp_pack_value (&bp, node->only_called_at_exit, 1);
505 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
506 lto_output_bitpack (&bp);
507 lto_output_uleb128_stream (ob->main_stream, node->resolution);
509 if (node->thunk.thunk_p && !boundary_p)
511 lto_output_uleb128_stream
512 (ob->main_stream,
513 1 + (node->thunk.this_adjusting != 0) * 2
514 + (node->thunk.virtual_offset_p != 0) * 4);
515 lto_output_uleb128_stream (ob->main_stream,
516 node->thunk.fixed_offset);
517 lto_output_uleb128_stream (ob->main_stream,
518 node->thunk.virtual_value);
519 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
520 node->thunk.alias);
523 if (node->same_body)
525 struct cgraph_node *alias;
526 unsigned long alias_count = 1;
527 for (alias = node->same_body; alias->next; alias = alias->next)
528 alias_count++;
529 lto_output_uleb128_stream (ob->main_stream, alias_count);
532 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
533 alias->decl);
534 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
535 alias->thunk.alias);
536 gcc_assert (cgraph_get_node (alias->thunk.alias) == node);
537 lto_output_uleb128_stream (ob->main_stream, alias->resolution);
538 alias = alias->previous;
540 while (alias);
542 else
543 lto_output_uleb128_stream (ob->main_stream, 0);
546 /* Output the varpool NODE to OB.
547 If NODE is not in SET, then NODE is a boundary. */
549 static void
550 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
551 lto_varpool_encoder_t varpool_encoder,
552 cgraph_node_set set, varpool_node_set vset)
554 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
555 struct bitpack_d bp;
556 struct varpool_node *alias;
557 int count = 0;
558 int ref;
560 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
561 bp = bitpack_create (ob->main_stream);
562 bp_pack_value (&bp, node->externally_visible, 1);
563 bp_pack_value (&bp, node->force_output, 1);
564 bp_pack_value (&bp, node->finalized, 1);
565 bp_pack_value (&bp, node->alias, 1);
566 gcc_assert (!node->alias || !node->extra_name);
567 gcc_assert (node->finalized || !node->analyzed);
568 gcc_assert (node->needed);
569 /* Constant pool initializers can be de-unified into individual ltrans units.
570 FIXME: Alternatively at -Os we may want to avoid generating for them the local
571 labels and share them across LTRANS partitions. */
572 if (DECL_IN_CONSTANT_POOL (node->decl)
573 && !DECL_COMDAT (node->decl))
575 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
576 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
578 else
580 bp_pack_value (&bp, node->analyzed
581 && referenced_from_other_partition_p (&node->ref_list,
582 set, vset), 1);
583 bp_pack_value (&bp, boundary_p, 1); /* in_other_partition. */
585 /* Also emit any extra name aliases. */
586 for (alias = node->extra_name; alias; alias = alias->next)
587 count++;
588 bp_pack_value (&bp, count != 0, 1);
589 lto_output_bitpack (&bp);
590 if (node->same_comdat_group && !boundary_p)
592 ref = lto_varpool_encoder_lookup (varpool_encoder, node->same_comdat_group);
593 gcc_assert (ref != LCC_NOT_FOUND);
595 else
596 ref = LCC_NOT_FOUND;
597 lto_output_sleb128_stream (ob->main_stream, ref);
598 lto_output_uleb128_stream (ob->main_stream, node->resolution);
600 if (count)
602 lto_output_uleb128_stream (ob->main_stream, count);
603 for (alias = node->extra_name; alias; alias = alias->next)
605 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
606 lto_output_uleb128_stream (ob->main_stream, alias->resolution);
611 /* Output the varpool NODE to OB.
612 If NODE is not in SET, then NODE is a boundary. */
614 static void
615 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
616 lto_cgraph_encoder_t encoder,
617 lto_varpool_encoder_t varpool_encoder)
619 struct bitpack_d bp;
620 bp = bitpack_create (ob->main_stream);
621 bp_pack_value (&bp, ref->refered_type, 1);
622 bp_pack_value (&bp, ref->use, 2);
623 lto_output_bitpack (&bp);
624 if (ref->refered_type == IPA_REF_CGRAPH)
626 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
627 gcc_assert (nref != LCC_NOT_FOUND);
628 lto_output_sleb128_stream (ob->main_stream, nref);
630 else
632 int nref = lto_varpool_encoder_lookup (varpool_encoder,
633 ipa_ref_varpool_node (ref));
634 gcc_assert (nref != LCC_NOT_FOUND);
635 lto_output_sleb128_stream (ob->main_stream, nref);
639 /* Stream out profile_summary to OB. */
641 static void
642 output_profile_summary (struct lto_simple_output_block *ob)
644 if (profile_info)
646 /* We do not output num, sum_all and run_max, they are not used by
647 GCC profile feedback and they are difficult to merge from multiple
648 units. */
649 gcc_assert (profile_info->runs);
650 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
651 lto_output_uleb128_stream (ob->main_stream, profile_info->sum_max);
653 else
654 lto_output_uleb128_stream (ob->main_stream, 0);
657 /* Add NODE into encoder as well as nodes it is cloned from.
658 Do it in a way so clones appear first. */
660 static void
661 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node,
662 bool include_body)
664 if (node->clone_of)
665 add_node_to (encoder, node->clone_of, include_body);
666 else if (include_body)
667 lto_set_cgraph_encoder_encode_body (encoder, node);
668 lto_cgraph_encoder_encode (encoder, node);
671 /* Add all references in LIST to encoders. */
673 static void
674 add_references (lto_cgraph_encoder_t encoder,
675 lto_varpool_encoder_t varpool_encoder,
676 struct ipa_ref_list *list)
678 int i;
679 struct ipa_ref *ref;
680 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
681 if (ref->refered_type == IPA_REF_CGRAPH)
682 add_node_to (encoder, ipa_ref_node (ref), false);
683 else
685 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
686 lto_varpool_encoder_encode (varpool_encoder, vnode);
690 /* Output all callees or indirect outgoing edges. EDGE must be the first such
691 edge. */
693 static void
694 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
695 struct lto_simple_output_block *ob,
696 lto_cgraph_encoder_t encoder)
698 if (!edge)
699 return;
701 /* Output edges in backward direction, so the reconstructed callgraph match
702 and it is easy to associate call sites in the IPA pass summaries. */
703 while (edge->next_callee)
704 edge = edge->next_callee;
705 for (; edge; edge = edge->prev_callee)
706 lto_output_edge (ob, edge, encoder);
709 /* Output the part of the cgraph in SET. */
711 static void
712 output_refs (cgraph_node_set set, varpool_node_set vset,
713 lto_cgraph_encoder_t encoder,
714 lto_varpool_encoder_t varpool_encoder)
716 cgraph_node_set_iterator csi;
717 varpool_node_set_iterator vsi;
718 struct lto_simple_output_block *ob;
719 int count;
720 struct ipa_ref *ref;
721 int i;
723 ob = lto_create_simple_output_block (LTO_section_refs);
725 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
727 struct cgraph_node *node = csi_node (csi);
729 count = ipa_ref_list_nreferences (&node->ref_list);
730 if (count)
732 lto_output_uleb128_stream (ob->main_stream, count);
733 lto_output_uleb128_stream (ob->main_stream,
734 lto_cgraph_encoder_lookup (encoder, node));
735 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
736 lto_output_ref (ob, ref, encoder, varpool_encoder);
740 lto_output_uleb128_stream (ob->main_stream, 0);
742 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
744 struct varpool_node *node = vsi_node (vsi);
746 count = ipa_ref_list_nreferences (&node->ref_list);
747 if (count)
749 lto_output_uleb128_stream (ob->main_stream, count);
750 lto_output_uleb128_stream (ob->main_stream,
751 lto_varpool_encoder_lookup (varpool_encoder,
752 node));
753 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
754 lto_output_ref (ob, ref, encoder, varpool_encoder);
758 lto_output_uleb128_stream (ob->main_stream, 0);
760 lto_destroy_simple_output_block (ob);
763 /* Find out all cgraph and varpool nodes we want to encode in current unit
764 and insert them to encoders. */
765 void
766 compute_ltrans_boundary (struct lto_out_decl_state *state,
767 cgraph_node_set set, varpool_node_set vset)
769 struct cgraph_node *node;
770 cgraph_node_set_iterator csi;
771 varpool_node_set_iterator vsi;
772 struct cgraph_edge *edge;
773 int i;
774 lto_cgraph_encoder_t encoder;
775 lto_varpool_encoder_t varpool_encoder;
777 encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
778 varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
780 /* Go over all the nodes in SET and assign references. */
781 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
783 node = csi_node (csi);
784 add_node_to (encoder, node, true);
785 add_references (encoder, varpool_encoder, &node->ref_list);
787 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
789 struct varpool_node *vnode = vsi_node (vsi);
790 gcc_assert (!vnode->alias);
791 lto_varpool_encoder_encode (varpool_encoder, vnode);
792 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
793 add_references (encoder, varpool_encoder, &vnode->ref_list);
795 /* Pickle in also the initializer of all referenced readonly variables
796 to help folding. Constant pool variables are not shared, so we must
797 pickle those too. */
798 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
800 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
801 if (DECL_INITIAL (vnode->decl)
802 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
803 vnode)
804 && const_value_known_p (vnode->decl))
806 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
807 add_references (encoder, varpool_encoder, &vnode->ref_list);
811 /* Go over all the nodes again to include callees that are not in
812 SET. */
813 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
815 node = csi_node (csi);
816 for (edge = node->callees; edge; edge = edge->next_callee)
818 struct cgraph_node *callee = edge->callee;
819 if (!cgraph_node_in_set_p (callee, set))
821 /* We should have moved all the inlines. */
822 gcc_assert (!callee->global.inlined_to);
823 add_node_to (encoder, callee, false);
829 /* Output the part of the cgraph in SET. */
831 void
832 output_cgraph (cgraph_node_set set, varpool_node_set vset)
834 struct cgraph_node *node;
835 struct lto_simple_output_block *ob;
836 cgraph_node_set_iterator csi;
837 int i, n_nodes;
838 lto_cgraph_encoder_t encoder;
839 lto_varpool_encoder_t varpool_encoder;
840 struct cgraph_asm_node *can;
841 static bool asm_nodes_output = false;
843 if (flag_wpa)
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
859 from. */
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 lto_output_uleb128_stream (ob->main_stream, 0);
877 /* Emit toplevel asms.
878 When doing WPA we must output every asm just once. Since we do not partition asm
879 nodes at all, output them to first output. This is kind of hack, but should work
880 well. */
881 if (!asm_nodes_output)
883 asm_nodes_output = true;
884 for (can = cgraph_asm_nodes; can; can = can->next)
886 int len = TREE_STRING_LENGTH (can->asm_str);
887 lto_output_uleb128_stream (ob->main_stream, len);
888 for (i = 0; i < len; ++i)
889 lto_output_1_stream (ob->main_stream,
890 TREE_STRING_POINTER (can->asm_str)[i]);
894 lto_output_uleb128_stream (ob->main_stream, 0);
896 lto_destroy_simple_output_block (ob);
897 output_varpool (set, vset);
898 output_refs (set, vset, encoder, varpool_encoder);
901 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
902 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
903 NODE or to replace the values in it, for instance because the first
904 time we saw it, the function body was not available but now it
905 is. BP is a bitpack with all the bitflags for NODE read from the
906 stream. */
908 static void
909 input_overwrite_node (struct lto_file_decl_data *file_data,
910 struct cgraph_node *node,
911 enum LTO_cgraph_tags tag,
912 struct bitpack_d *bp,
913 enum ld_plugin_symbol_resolution resolution)
915 node->aux = (void *) tag;
916 node->local.lto_file_data = file_data;
918 node->local.local = bp_unpack_value (bp, 1);
919 node->local.externally_visible = bp_unpack_value (bp, 1);
920 node->local.finalized = bp_unpack_value (bp, 1);
921 node->local.can_change_signature = bp_unpack_value (bp, 1);
922 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
923 node->needed = bp_unpack_value (bp, 1);
924 node->address_taken = bp_unpack_value (bp, 1);
925 node->abstract_and_needed = bp_unpack_value (bp, 1);
926 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
927 node->lowered = bp_unpack_value (bp, 1);
928 node->analyzed = tag == LTO_cgraph_analyzed_node;
929 node->in_other_partition = bp_unpack_value (bp, 1);
930 if (node->in_other_partition
931 /* Avoid updating decl when we are seeing just inline clone.
932 When inlining function that has functions already inlined into it,
933 we produce clones of inline clones.
935 WPA partitioning might put each clone into different unit and
936 we might end up streaming inline clone from other partition
937 to support clone we are interested in. */
938 && (!node->clone_of
939 || node->clone_of->decl != node->decl))
941 DECL_EXTERNAL (node->decl) = 1;
942 TREE_STATIC (node->decl) = 0;
944 node->alias = bp_unpack_value (bp, 1);
945 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
946 node->only_called_at_startup = bp_unpack_value (bp, 1);
947 node->only_called_at_exit = bp_unpack_value (bp, 1);
948 node->thunk.thunk_p = bp_unpack_value (bp, 1);
949 node->resolution = resolution;
952 /* Output the part of the cgraph in SET. */
954 static void
955 output_varpool (cgraph_node_set set, varpool_node_set vset)
957 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
958 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
959 int len = lto_varpool_encoder_size (varpool_encoder), i;
961 lto_output_uleb128_stream (ob->main_stream, len);
963 /* Write out the nodes. We must first output a node and then its clones,
964 otherwise at a time reading back the node there would be nothing to clone
965 from. */
966 for (i = 0; i < len; i++)
968 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
969 varpool_encoder,
970 set, vset);
973 lto_destroy_simple_output_block (ob);
976 /* Read a node from input_block IB. TAG is the node's tag just read.
977 Return the node read or overwriten. */
979 static struct cgraph_node *
980 input_node (struct lto_file_decl_data *file_data,
981 struct lto_input_block *ib,
982 enum LTO_cgraph_tags tag,
983 VEC(cgraph_node_ptr, heap) *nodes)
985 tree fn_decl;
986 struct cgraph_node *node;
987 struct bitpack_d bp;
988 unsigned decl_index;
989 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
990 unsigned long same_body_count = 0;
991 int clone_ref;
992 enum ld_plugin_symbol_resolution resolution;
994 clone_ref = lto_input_sleb128 (ib);
996 decl_index = lto_input_uleb128 (ib);
997 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
999 if (clone_ref != LCC_NOT_FOUND)
1001 node = cgraph_clone_node (VEC_index (cgraph_node_ptr, nodes, clone_ref), fn_decl,
1002 0, CGRAPH_FREQ_BASE, false, NULL, false);
1004 else
1005 node = cgraph_get_create_node (fn_decl);
1007 node->count = lto_input_sleb128 (ib);
1008 node->count_materialization_scale = lto_input_sleb128 (ib);
1010 if (tag == LTO_cgraph_analyzed_node)
1011 ref = lto_input_sleb128 (ib);
1013 ref2 = lto_input_sleb128 (ib);
1015 /* Make sure that we have not read this node before. Nodes that
1016 have already been read will have their tag stored in the 'aux'
1017 field. Since built-in functions can be referenced in multiple
1018 functions, they are expected to be read more than once. */
1019 if (node->aux && !DECL_IS_BUILTIN (node->decl))
1020 internal_error ("bytecode stream: found multiple instances of cgraph "
1021 "node %d", node->uid);
1023 bp = lto_input_bitpack (ib);
1024 resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1025 input_overwrite_node (file_data, node, tag, &bp, resolution);
1027 /* Store a reference for now, and fix up later to be a pointer. */
1028 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1030 /* Store a reference for now, and fix up later to be a pointer. */
1031 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1033 if (node->thunk.thunk_p)
1035 int type = lto_input_uleb128 (ib);
1036 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1037 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1038 tree real_alias;
1040 decl_index = lto_input_uleb128 (ib);
1041 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1042 node->thunk.fixed_offset = fixed_offset;
1043 node->thunk.this_adjusting = (type & 2);
1044 node->thunk.virtual_value = virtual_value;
1045 node->thunk.virtual_offset_p = (type & 4);
1046 node->thunk.alias = real_alias;
1049 same_body_count = lto_input_uleb128 (ib);
1050 while (same_body_count-- > 0)
1052 tree alias_decl, real_alias;
1053 struct cgraph_node *alias;
1055 decl_index = lto_input_uleb128 (ib);
1056 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1057 decl_index = lto_input_uleb128 (ib);
1058 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1059 alias = cgraph_same_body_alias (node, alias_decl, real_alias);
1060 gcc_assert (alias);
1061 alias->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1063 return node;
1066 /* Read a node from input_block IB. TAG is the node's tag just read.
1067 Return the node read or overwriten. */
1069 static struct varpool_node *
1070 input_varpool_node (struct lto_file_decl_data *file_data,
1071 struct lto_input_block *ib)
1073 int decl_index;
1074 tree var_decl;
1075 struct varpool_node *node;
1076 struct bitpack_d bp;
1077 bool aliases_p;
1078 int count;
1079 int ref = LCC_NOT_FOUND;
1081 decl_index = lto_input_uleb128 (ib);
1082 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1083 node = varpool_node (var_decl);
1084 node->lto_file_data = file_data;
1086 bp = lto_input_bitpack (ib);
1087 node->externally_visible = bp_unpack_value (&bp, 1);
1088 node->force_output = bp_unpack_value (&bp, 1);
1089 node->finalized = bp_unpack_value (&bp, 1);
1090 node->alias = bp_unpack_value (&bp, 1);
1091 node->analyzed = node->finalized;
1092 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1093 node->in_other_partition = bp_unpack_value (&bp, 1);
1094 if (node->in_other_partition)
1096 DECL_EXTERNAL (node->decl) = 1;
1097 TREE_STATIC (node->decl) = 0;
1099 aliases_p = bp_unpack_value (&bp, 1);
1100 if (node->finalized)
1101 varpool_mark_needed_node (node);
1102 ref = lto_input_sleb128 (ib);
1103 /* Store a reference for now, and fix up later to be a pointer. */
1104 node->same_comdat_group = (struct varpool_node *) (intptr_t) ref;
1105 node->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1106 if (aliases_p)
1108 count = lto_input_uleb128 (ib);
1109 for (; count > 0; count --)
1111 tree decl = lto_file_decl_data_get_var_decl (file_data,
1112 lto_input_uleb128 (ib));
1113 struct varpool_node *alias;
1114 alias = varpool_extra_name_alias (decl, var_decl);
1115 alias->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1118 return node;
1121 /* Read a node from input_block IB. TAG is the node's tag just read.
1122 Return the node read or overwriten. */
1124 static void
1125 input_ref (struct lto_input_block *ib,
1126 struct cgraph_node *refering_node,
1127 struct varpool_node *refering_varpool_node,
1128 VEC(cgraph_node_ptr, heap) *nodes,
1129 VEC(varpool_node_ptr, heap) *varpool_nodes)
1131 struct cgraph_node *node = NULL;
1132 struct varpool_node *varpool_node = NULL;
1133 struct bitpack_d bp;
1134 enum ipa_ref_type type;
1135 enum ipa_ref_use use;
1137 bp = lto_input_bitpack (ib);
1138 type = (enum ipa_ref_type) bp_unpack_value (&bp, 1);
1139 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1140 if (type == IPA_REF_CGRAPH)
1141 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1142 else
1143 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1144 ipa_record_reference (refering_node, refering_varpool_node,
1145 node, varpool_node, use, NULL);
1148 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1149 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1150 edge being read is indirect (in the sense that it has
1151 indirect_unknown_callee set). */
1153 static void
1154 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1155 bool indirect)
1157 struct cgraph_node *caller, *callee;
1158 struct cgraph_edge *edge;
1159 unsigned int stmt_id;
1160 gcov_type count;
1161 int freq;
1162 cgraph_inline_failed_t inline_failed;
1163 struct bitpack_d bp;
1164 int ecf_flags = 0;
1166 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1167 if (caller == NULL || caller->decl == NULL_TREE)
1168 internal_error ("bytecode stream: no caller found while reading edge");
1170 if (!indirect)
1172 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1173 if (callee == NULL || callee->decl == NULL_TREE)
1174 internal_error ("bytecode stream: no callee found while reading edge");
1176 else
1177 callee = NULL;
1179 count = (gcov_type) lto_input_sleb128 (ib);
1181 bp = lto_input_bitpack (ib);
1182 stmt_id = (unsigned int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1183 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (&bp,
1184 HOST_BITS_PER_INT);
1185 freq = (int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1187 if (indirect)
1188 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1189 else
1190 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1192 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1193 edge->lto_stmt_uid = stmt_id;
1194 edge->inline_failed = inline_failed;
1195 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1196 edge->can_throw_external = bp_unpack_value (&bp, 1);
1197 if (indirect)
1199 if (bp_unpack_value (&bp, 1))
1200 ecf_flags |= ECF_CONST;
1201 if (bp_unpack_value (&bp, 1))
1202 ecf_flags |= ECF_PURE;
1203 if (bp_unpack_value (&bp, 1))
1204 ecf_flags |= ECF_NORETURN;
1205 if (bp_unpack_value (&bp, 1))
1206 ecf_flags |= ECF_MALLOC;
1207 if (bp_unpack_value (&bp, 1))
1208 ecf_flags |= ECF_NOTHROW;
1209 if (bp_unpack_value (&bp, 1))
1210 ecf_flags |= ECF_RETURNS_TWICE;
1211 edge->indirect_info->ecf_flags = ecf_flags;
1216 /* Read a cgraph from IB using the info in FILE_DATA. */
1218 static VEC(cgraph_node_ptr, heap) *
1219 input_cgraph_1 (struct lto_file_decl_data *file_data,
1220 struct lto_input_block *ib)
1222 enum LTO_cgraph_tags tag;
1223 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1224 struct cgraph_node *node;
1225 unsigned i;
1226 unsigned HOST_WIDE_INT len;
1228 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1229 while (tag)
1231 if (tag == LTO_cgraph_edge)
1232 input_edge (ib, nodes, false);
1233 else if (tag == LTO_cgraph_indirect_edge)
1234 input_edge (ib, nodes, true);
1235 else
1237 node = input_node (file_data, ib, tag,nodes);
1238 if (node == NULL || node->decl == NULL_TREE)
1239 internal_error ("bytecode stream: found empty cgraph node");
1240 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1241 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1244 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1247 /* Input toplevel asms. */
1248 len = lto_input_uleb128 (ib);
1249 while (len)
1251 char *str = (char *)xmalloc (len + 1);
1252 for (i = 0; i < len; ++i)
1253 str[i] = lto_input_1_unsigned (ib);
1254 cgraph_add_asm_node (build_string (len, str));
1255 free (str);
1257 len = lto_input_uleb128 (ib);
1259 /* AUX pointers should be all non-zero for nodes read from the stream. */
1260 #ifdef ENABLE_CHECKING
1261 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1262 gcc_assert (node->aux);
1263 #endif
1264 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1266 int ref = (int) (intptr_t) node->global.inlined_to;
1268 /* We share declaration of builtins, so we may read same node twice. */
1269 if (!node->aux)
1270 continue;
1271 node->aux = NULL;
1273 /* Fixup inlined_to from reference to pointer. */
1274 if (ref != LCC_NOT_FOUND)
1275 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1276 else
1277 node->global.inlined_to = NULL;
1279 ref = (int) (intptr_t) node->same_comdat_group;
1281 /* Fixup same_comdat_group from reference to pointer. */
1282 if (ref != LCC_NOT_FOUND)
1283 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1284 else
1285 node->same_comdat_group = NULL;
1287 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1288 node->aux = (void *)1;
1289 return nodes;
1292 /* Read a varpool from IB using the info in FILE_DATA. */
1294 static VEC(varpool_node_ptr, heap) *
1295 input_varpool_1 (struct lto_file_decl_data *file_data,
1296 struct lto_input_block *ib)
1298 unsigned HOST_WIDE_INT len;
1299 VEC(varpool_node_ptr, heap) *varpool = NULL;
1300 int i;
1301 struct varpool_node *node;
1303 len = lto_input_uleb128 (ib);
1304 while (len)
1306 VEC_safe_push (varpool_node_ptr, heap, varpool,
1307 input_varpool_node (file_data, ib));
1308 len--;
1310 #ifdef ENABLE_CHECKING
1311 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1312 gcc_assert (!node->aux);
1313 #endif
1314 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1316 int ref = (int) (intptr_t) node->same_comdat_group;
1317 /* We share declaration of builtins, so we may read same node twice. */
1318 if (node->aux)
1319 continue;
1320 node->aux = (void *)1;
1322 /* Fixup same_comdat_group from reference to pointer. */
1323 if (ref != LCC_NOT_FOUND)
1324 node->same_comdat_group = VEC_index (varpool_node_ptr, varpool, ref);
1325 else
1326 node->same_comdat_group = NULL;
1328 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1329 node->aux = NULL;
1330 return varpool;
1333 /* Input ipa_refs. */
1335 static void
1336 input_refs (struct lto_input_block *ib,
1337 VEC(cgraph_node_ptr, heap) *nodes,
1338 VEC(varpool_node_ptr, heap) *varpool)
1340 int count;
1341 int idx;
1342 while (true)
1344 struct cgraph_node *node;
1345 count = lto_input_uleb128 (ib);
1346 if (!count)
1347 break;
1348 idx = lto_input_uleb128 (ib);
1349 node = VEC_index (cgraph_node_ptr, nodes, idx);
1350 while (count)
1352 input_ref (ib, node, NULL, nodes, varpool);
1353 count--;
1356 while (true)
1358 struct varpool_node *node;
1359 count = lto_input_uleb128 (ib);
1360 if (!count)
1361 break;
1362 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1363 while (count)
1365 input_ref (ib, NULL, node, nodes, varpool);
1366 count--;
1372 static struct gcov_ctr_summary lto_gcov_summary;
1374 /* Input profile_info from IB. */
1375 static void
1376 input_profile_summary (struct lto_input_block *ib,
1377 struct lto_file_decl_data *file_data)
1379 unsigned int runs = lto_input_uleb128 (ib);
1380 if (runs)
1382 file_data->profile_info.runs = runs;
1383 file_data->profile_info.sum_max = lto_input_uleb128 (ib);
1388 /* Rescale profile summaries to the same number of runs in the whole unit. */
1390 static void
1391 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1393 struct lto_file_decl_data *file_data;
1394 unsigned int j;
1395 gcov_unsigned_t max_runs = 0;
1396 struct cgraph_node *node;
1397 struct cgraph_edge *edge;
1399 /* Find unit with maximal number of runs. If we ever get serious about
1400 roundoff errors, we might also consider computing smallest common
1401 multiply. */
1402 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1403 if (max_runs < file_data->profile_info.runs)
1404 max_runs = file_data->profile_info.runs;
1406 if (!max_runs)
1407 return;
1409 /* Simple overflow check. We probably don't need to support that many train
1410 runs. Such a large value probably imply data corruption anyway. */
1411 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1413 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1414 INT_MAX / REG_BR_PROB_BASE);
1415 return;
1418 profile_info = &lto_gcov_summary;
1419 lto_gcov_summary.runs = max_runs;
1420 lto_gcov_summary.sum_max = 0;
1422 /* Rescale all units to the maximal number of runs.
1423 sum_max can not be easily merged, as we have no idea what files come from
1424 the same run. We do not use the info anyway, so leave it 0. */
1425 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1426 if (file_data->profile_info.runs)
1428 int scale = ((REG_BR_PROB_BASE * max_runs
1429 + file_data->profile_info.runs / 2)
1430 / file_data->profile_info.runs);
1431 lto_gcov_summary.sum_max = MAX (lto_gcov_summary.sum_max,
1432 (file_data->profile_info.sum_max
1433 * scale
1434 + REG_BR_PROB_BASE / 2)
1435 / REG_BR_PROB_BASE);
1438 /* Watch roundoff errors. */
1439 if (lto_gcov_summary.sum_max < max_runs)
1440 lto_gcov_summary.sum_max = max_runs;
1442 /* If merging already happent at WPA time, we are done. */
1443 if (flag_ltrans)
1444 return;
1446 /* Now compute count_materialization_scale of each node.
1447 During LTRANS we already have values of count_materialization_scale
1448 computed, so just update them. */
1449 for (node = cgraph_nodes; node; node = node->next)
1450 if (node->local.lto_file_data
1451 && node->local.lto_file_data->profile_info.runs)
1453 int scale;
1455 scale =
1456 ((node->count_materialization_scale * max_runs
1457 + node->local.lto_file_data->profile_info.runs / 2)
1458 / node->local.lto_file_data->profile_info.runs);
1459 node->count_materialization_scale = scale;
1460 if (scale < 0)
1461 fatal_error ("Profile information in %s corrupted",
1462 file_data->file_name);
1464 if (scale == REG_BR_PROB_BASE)
1465 continue;
1466 for (edge = node->callees; edge; edge = edge->next_callee)
1467 edge->count = ((edge->count * scale + REG_BR_PROB_BASE / 2)
1468 / REG_BR_PROB_BASE);
1469 node->count = ((node->count * scale + REG_BR_PROB_BASE / 2)
1470 / REG_BR_PROB_BASE);
1474 /* Input and merge the cgraph from each of the .o files passed to
1475 lto1. */
1477 void
1478 input_cgraph (void)
1480 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1481 struct lto_file_decl_data *file_data;
1482 unsigned int j = 0;
1483 struct cgraph_node *node;
1485 while ((file_data = file_data_vec[j++]))
1487 const char *data;
1488 size_t len;
1489 struct lto_input_block *ib;
1490 VEC(cgraph_node_ptr, heap) *nodes;
1491 VEC(varpool_node_ptr, heap) *varpool;
1493 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1494 &data, &len);
1495 if (!ib)
1496 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1497 input_profile_summary (ib, file_data);
1498 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1499 nodes = input_cgraph_1 (file_data, ib);
1500 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1501 ib, data, len);
1503 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1504 &data, &len);
1505 if (!ib)
1506 fatal_error ("cannot find LTO varpool in %s", file_data->file_name);
1507 varpool = input_varpool_1 (file_data, ib);
1508 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1509 ib, data, len);
1511 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1512 &data, &len);
1513 if (!ib)
1514 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1515 input_refs (ib, nodes, varpool);
1516 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1517 ib, data, len);
1518 if (flag_ltrans)
1519 input_cgraph_opt_summary (nodes);
1520 VEC_free (cgraph_node_ptr, heap, nodes);
1521 VEC_free (varpool_node_ptr, heap, varpool);
1524 merge_profile_summaries (file_data_vec);
1526 /* Clear out the aux field that was used to store enough state to
1527 tell which nodes should be overwritten. */
1528 for (node = cgraph_nodes; node; node = node->next)
1530 /* Some nodes may have been created by cgraph_node. This
1531 happens when the callgraph contains nested functions. If the
1532 node for the parent function was never emitted to the gimple
1533 file, cgraph_node will create a node for it when setting the
1534 context of the nested function. */
1535 if (node->local.lto_file_data)
1536 node->aux = NULL;
1540 /* True when we need optimization summary for NODE. */
1542 static int
1543 output_cgraph_opt_summary_p (struct cgraph_node *node, cgraph_node_set set)
1545 struct cgraph_edge *e;
1547 if (cgraph_node_in_set_p (node, set))
1549 for (e = node->callees; e; e = e->next_callee)
1550 if (e->indirect_info
1551 && e->indirect_info->thunk_delta != 0)
1552 return true;
1554 for (e = node->indirect_calls; e; e = e->next_callee)
1555 if (e->indirect_info->thunk_delta != 0)
1556 return true;
1559 return (node->clone_of
1560 && (node->clone.tree_map
1561 || node->clone.args_to_skip
1562 || node->clone.combined_args_to_skip));
1565 /* Output optimization summary for EDGE to OB. */
1566 static void
1567 output_edge_opt_summary (struct output_block *ob,
1568 struct cgraph_edge *edge)
1570 if (edge->indirect_info)
1571 lto_output_sleb128_stream (ob->main_stream,
1572 edge->indirect_info->thunk_delta);
1573 else
1574 lto_output_sleb128_stream (ob->main_stream, 0);
1577 /* Output optimization summary for NODE to OB. */
1579 static void
1580 output_node_opt_summary (struct output_block *ob,
1581 struct cgraph_node *node,
1582 cgraph_node_set set)
1584 unsigned int index;
1585 bitmap_iterator bi;
1586 struct ipa_replace_map *map;
1587 struct bitpack_d bp;
1588 int i;
1589 struct cgraph_edge *e;
1591 lto_output_uleb128_stream (ob->main_stream,
1592 bitmap_count_bits (node->clone.args_to_skip));
1593 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1594 lto_output_uleb128_stream (ob->main_stream, index);
1595 lto_output_uleb128_stream (ob->main_stream,
1596 bitmap_count_bits (node->clone.combined_args_to_skip));
1597 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1598 lto_output_uleb128_stream (ob->main_stream, index);
1599 lto_output_uleb128_stream (ob->main_stream,
1600 VEC_length (ipa_replace_map_p, node->clone.tree_map));
1601 FOR_EACH_VEC_ELT (ipa_replace_map_p, node->clone.tree_map, i, map)
1603 int parm_num;
1604 tree parm;
1606 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm;
1607 parm = DECL_CHAIN (parm), parm_num++)
1608 if (map->old_tree == parm)
1609 break;
1610 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1611 mechanism to store function local declarations into summaries. */
1612 gcc_assert (parm);
1613 lto_output_uleb128_stream (ob->main_stream, parm_num);
1614 lto_output_tree (ob, map->new_tree, true);
1615 bp = bitpack_create (ob->main_stream);
1616 bp_pack_value (&bp, map->replace_p, 1);
1617 bp_pack_value (&bp, map->ref_p, 1);
1618 lto_output_bitpack (&bp);
1621 if (cgraph_node_in_set_p (node, set))
1623 for (e = node->callees; e; e = e->next_callee)
1624 output_edge_opt_summary (ob, e);
1625 for (e = node->indirect_calls; e; e = e->next_callee)
1626 output_edge_opt_summary (ob, e);
1630 /* Output optimization summaries stored in callgraph.
1631 At the moment it is the clone info structure. */
1633 static void
1634 output_cgraph_opt_summary (cgraph_node_set set)
1636 struct cgraph_node *node;
1637 int i, n_nodes;
1638 lto_cgraph_encoder_t encoder;
1639 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1640 unsigned count = 0;
1642 ob->cgraph_node = NULL;
1643 encoder = ob->decl_state->cgraph_node_encoder;
1644 n_nodes = lto_cgraph_encoder_size (encoder);
1645 for (i = 0; i < n_nodes; i++)
1646 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder, i),
1647 set))
1648 count++;
1649 lto_output_uleb128_stream (ob->main_stream, count);
1650 for (i = 0; i < n_nodes; i++)
1652 node = lto_cgraph_encoder_deref (encoder, i);
1653 if (output_cgraph_opt_summary_p (node, set))
1655 lto_output_uleb128_stream (ob->main_stream, i);
1656 output_node_opt_summary (ob, node, set);
1659 produce_asm (ob, NULL);
1660 destroy_output_block (ob);
1663 /* Input optimisation summary of EDGE. */
1665 static void
1666 input_edge_opt_summary (struct cgraph_edge *edge,
1667 struct lto_input_block *ib_main)
1669 HOST_WIDE_INT thunk_delta;
1670 thunk_delta = lto_input_sleb128 (ib_main);
1671 if (thunk_delta != 0)
1673 gcc_assert (!edge->indirect_info);
1674 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1675 edge->indirect_info->thunk_delta = thunk_delta;
1679 /* Input optimisation summary of NODE. */
1681 static void
1682 input_node_opt_summary (struct cgraph_node *node,
1683 struct lto_input_block *ib_main,
1684 struct data_in *data_in)
1686 int i;
1687 int count;
1688 int bit;
1689 struct bitpack_d bp;
1690 struct cgraph_edge *e;
1692 count = lto_input_uleb128 (ib_main);
1693 if (count)
1694 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1695 for (i = 0; i < count; i++)
1697 bit = lto_input_uleb128 (ib_main);
1698 bitmap_set_bit (node->clone.args_to_skip, bit);
1700 count = lto_input_uleb128 (ib_main);
1701 if (count)
1702 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1703 for (i = 0; i < count; i++)
1705 bit = lto_input_uleb128 (ib_main);
1706 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1708 count = lto_input_uleb128 (ib_main);
1709 for (i = 0; i < count; i++)
1711 int parm_num;
1712 tree parm;
1713 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1715 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1716 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm_num;
1717 parm = DECL_CHAIN (parm))
1718 parm_num --;
1719 map->parm_num = lto_input_uleb128 (ib_main);
1720 map->old_tree = NULL;
1721 map->new_tree = lto_input_tree (ib_main, data_in);
1722 bp = lto_input_bitpack (ib_main);
1723 map->replace_p = bp_unpack_value (&bp, 1);
1724 map->ref_p = bp_unpack_value (&bp, 1);
1726 for (e = node->callees; e; e = e->next_callee)
1727 input_edge_opt_summary (e, ib_main);
1728 for (e = node->indirect_calls; e; e = e->next_callee)
1729 input_edge_opt_summary (e, ib_main);
1732 /* Read section in file FILE_DATA of length LEN with data DATA. */
1734 static void
1735 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1736 const char *data, size_t len, VEC (cgraph_node_ptr,
1737 heap) * nodes)
1739 const struct lto_function_header *header =
1740 (const struct lto_function_header *) data;
1741 const int32_t cfg_offset = sizeof (struct lto_function_header);
1742 const int32_t main_offset = cfg_offset + header->cfg_size;
1743 const int32_t string_offset = main_offset + header->main_size;
1744 struct data_in *data_in;
1745 struct lto_input_block ib_main;
1746 unsigned int i;
1747 unsigned int count;
1749 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1750 header->main_size);
1752 data_in =
1753 lto_data_in_create (file_data, (const char *) data + string_offset,
1754 header->string_size, NULL);
1755 count = lto_input_uleb128 (&ib_main);
1757 for (i = 0; i < count; i++)
1759 int ref = lto_input_uleb128 (&ib_main);
1760 input_node_opt_summary (VEC_index (cgraph_node_ptr, nodes, ref),
1761 &ib_main, data_in);
1763 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1764 len);
1765 lto_data_in_delete (data_in);
1768 /* Input optimization summary of cgraph. */
1770 static void
1771 input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes)
1773 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1774 struct lto_file_decl_data *file_data;
1775 unsigned int j = 0;
1777 while ((file_data = file_data_vec[j++]))
1779 size_t len;
1780 const char *data =
1781 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1782 &len);
1784 if (data)
1785 input_cgraph_opt_section (file_data, data, len, nodes);