Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_6_3-mobile / gcc / lto-cgraph.c
blobdc2a69332988e66a56a1972607ef2fc37a07c684
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 "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->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_LEAF
306 | ECF_NOVOPS)));
308 lto_output_bitpack (&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 (ipa_ref_refering_node (ref)->in_other_partition
324 || !cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
325 return true;
327 else
329 if (ipa_ref_refering_varpool_node (ref)->in_other_partition
330 || !varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
331 vset))
332 return true;
335 return false;
338 /* Return true when node is reachable from other partition. */
340 bool
341 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
343 struct cgraph_edge *e;
344 if (!node->analyzed)
345 return false;
346 if (node->global.inlined_to)
347 return false;
348 for (e = node->callers; e; e = e->next_caller)
349 if (e->caller->in_other_partition
350 || !cgraph_node_in_set_p (e->caller, set))
351 return true;
352 return false;
355 /* Return if LIST contain references from other partitions. */
357 bool
358 referenced_from_this_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
359 varpool_node_set vset)
361 int i;
362 struct ipa_ref *ref;
363 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
365 if (ref->refering_type == IPA_REF_CGRAPH)
367 if (cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
368 return true;
370 else
372 if (varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
373 vset))
374 return true;
377 return false;
380 /* Return true when node is reachable from other partition. */
382 bool
383 reachable_from_this_partition_p (struct cgraph_node *node, cgraph_node_set set)
385 struct cgraph_edge *e;
386 for (e = node->callers; e; e = e->next_caller)
387 if (cgraph_node_in_set_p (e->caller, set))
388 return true;
389 return false;
392 /* Output the cgraph NODE to OB. ENCODER is used to find the
393 reference number of NODE->inlined_to. SET is the set of nodes we
394 are writing to the current file. If NODE is not in SET, then NODE
395 is a boundary of a cgraph_node_set and we pretend NODE just has a
396 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
397 that have had their callgraph node written so far. This is used to
398 determine if NODE is a clone of a previously written node. */
400 static void
401 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
402 lto_cgraph_encoder_t encoder, cgraph_node_set set,
403 varpool_node_set vset)
405 unsigned int tag;
406 struct bitpack_d bp;
407 bool boundary_p;
408 intptr_t ref;
409 bool in_other_partition = false;
410 struct cgraph_node *clone_of;
412 boundary_p = !cgraph_node_in_set_p (node, set);
414 if (node->analyzed && !boundary_p)
415 tag = LTO_cgraph_analyzed_node;
416 else
417 tag = LTO_cgraph_unavail_node;
419 lto_output_uleb128_stream (ob->main_stream, tag);
421 /* In WPA mode, we only output part of the call-graph. Also, we
422 fake cgraph node attributes. There are two cases that we care.
424 Boundary nodes: There are nodes that are not part of SET but are
425 called from within SET. We artificially make them look like
426 externally visible nodes with no function body.
428 Cherry-picked nodes: These are nodes we pulled from other
429 translation units into SET during IPA-inlining. We make them as
430 local static nodes to prevent clashes with other local statics. */
431 if (boundary_p && node->analyzed)
433 /* Inline clones can not be part of boundary.
434 gcc_assert (!node->global.inlined_to);
436 FIXME: At the moment they can be, when partition contains an inline
437 clone that is clone of inline clone from outside partition. We can
438 reshape the clone tree and make other tree to be the root, but it
439 needs a bit extra work and will be promplty done by cgraph_remove_node
440 after reading back. */
441 in_other_partition = 1;
444 clone_of = node->clone_of;
445 while (clone_of
446 && (ref = lto_cgraph_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
447 if (clone_of->prev_sibling_clone)
448 clone_of = clone_of->prev_sibling_clone;
449 else
450 clone_of = clone_of->clone_of;
452 if (LTO_cgraph_analyzed_node)
453 gcc_assert (clone_of || !node->clone_of);
454 if (!clone_of)
455 lto_output_sleb128_stream (ob->main_stream, LCC_NOT_FOUND);
456 else
457 lto_output_sleb128_stream (ob->main_stream, ref);
460 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
461 lto_output_sleb128_stream (ob->main_stream, node->count);
462 lto_output_sleb128_stream (ob->main_stream, node->max_bb_count);
463 lto_output_sleb128_stream (ob->main_stream, node->count_materialization_scale);
465 if (tag == LTO_cgraph_analyzed_node)
467 lto_output_sleb128_stream (ob->main_stream,
468 node->local.inline_summary.estimated_self_stack_size);
469 lto_output_sleb128_stream (ob->main_stream,
470 node->local.inline_summary.self_size);
471 lto_output_sleb128_stream (ob->main_stream,
472 node->local.inline_summary.size_inlining_benefit);
473 lto_output_sleb128_stream (ob->main_stream,
474 node->local.inline_summary.self_time);
475 lto_output_sleb128_stream (ob->main_stream,
476 node->local.inline_summary.time_inlining_benefit);
477 if (node->global.inlined_to)
479 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
480 gcc_assert (ref != LCC_NOT_FOUND);
482 else
483 ref = LCC_NOT_FOUND;
485 lto_output_sleb128_stream (ob->main_stream, ref);
488 if (node->same_comdat_group && !boundary_p)
490 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
491 gcc_assert (ref != LCC_NOT_FOUND);
493 else
494 ref = LCC_NOT_FOUND;
495 lto_output_sleb128_stream (ob->main_stream, ref);
497 bp = bitpack_create (ob->main_stream);
498 bp_pack_value (&bp, node->local.local, 1);
499 bp_pack_value (&bp, node->local.externally_visible, 1);
500 bp_pack_value (&bp, node->local.finalized, 1);
501 bp_pack_value (&bp, node->local.inlinable, 1);
502 bp_pack_value (&bp, node->local.versionable, 1);
503 bp_pack_value (&bp, node->local.can_change_signature, 1);
504 bp_pack_value (&bp, node->local.disregard_inline_limits, 1);
505 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
506 bp_pack_value (&bp, node->local.vtable_method, 1);
507 bp_pack_value (&bp, node->needed, 1);
508 bp_pack_value (&bp, node->address_taken, 1);
509 bp_pack_value (&bp, node->abstract_and_needed, 1);
510 bp_pack_value (&bp, tag == LTO_cgraph_analyzed_node
511 && !DECL_EXTERNAL (node->decl)
512 && !DECL_COMDAT (node->decl)
513 && (reachable_from_other_partition_p (node, set)
514 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
515 bp_pack_value (&bp, node->lowered, 1);
516 bp_pack_value (&bp, in_other_partition, 1);
517 bp_pack_value (&bp, node->alias, 1);
518 bp_pack_value (&bp, node->finalized_by_frontend, 1);
519 bp_pack_value (&bp, node->frequency, 2);
520 bp_pack_value (&bp, node->only_called_at_startup, 1);
521 bp_pack_value (&bp, node->only_called_at_exit, 1);
522 lto_output_bitpack (&bp);
523 lto_output_uleb128_stream (ob->main_stream, node->resolution);
525 if (node->same_body)
527 struct cgraph_node *alias;
528 unsigned long alias_count = 1;
529 for (alias = node->same_body; alias->next; alias = alias->next)
530 alias_count++;
531 lto_output_uleb128_stream (ob->main_stream, alias_count);
534 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
535 alias->decl);
536 if (alias->thunk.thunk_p)
538 lto_output_uleb128_stream
539 (ob->main_stream,
540 1 + (alias->thunk.this_adjusting != 0) * 2
541 + (alias->thunk.virtual_offset_p != 0) * 4);
542 lto_output_uleb128_stream (ob->main_stream,
543 alias->thunk.fixed_offset);
544 lto_output_uleb128_stream (ob->main_stream,
545 alias->thunk.virtual_value);
546 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
547 alias->thunk.alias);
549 else
551 lto_output_uleb128_stream (ob->main_stream, 0);
552 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
553 alias->thunk.alias);
555 gcc_assert (cgraph_get_node (alias->thunk.alias) == node);
556 lto_output_uleb128_stream (ob->main_stream, alias->resolution);
557 alias = alias->previous;
559 while (alias);
561 else
562 lto_output_uleb128_stream (ob->main_stream, 0);
565 /* Output the varpool NODE to OB.
566 If NODE is not in SET, then NODE is a boundary. */
568 static void
569 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
570 lto_varpool_encoder_t varpool_encoder,
571 cgraph_node_set set, varpool_node_set vset)
573 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
574 struct bitpack_d bp;
575 struct varpool_node *alias;
576 int count = 0;
577 int ref;
579 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
580 bp = bitpack_create (ob->main_stream);
581 bp_pack_value (&bp, node->externally_visible, 1);
582 bp_pack_value (&bp, node->force_output, 1);
583 bp_pack_value (&bp, node->finalized, 1);
584 bp_pack_value (&bp, node->alias, 1);
585 gcc_assert (!node->alias || !node->extra_name);
586 gcc_assert (node->finalized || !node->analyzed);
587 gcc_assert (node->needed);
588 /* Constant pool initializers can be de-unified into individual ltrans units.
589 FIXME: Alternatively at -Os we may want to avoid generating for them the local
590 labels and share them across LTRANS partitions. */
591 if (DECL_IN_CONSTANT_POOL (node->decl)
592 && !DECL_COMDAT (node->decl))
594 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
595 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
597 else
599 bp_pack_value (&bp, node->analyzed
600 && referenced_from_other_partition_p (&node->ref_list,
601 set, vset), 1);
602 bp_pack_value (&bp, boundary_p, 1); /* in_other_partition. */
604 /* Also emit any extra name aliases. */
605 for (alias = node->extra_name; alias; alias = alias->next)
606 count++;
607 bp_pack_value (&bp, count != 0, 1);
608 lto_output_bitpack (&bp);
609 if (node->same_comdat_group && !boundary_p)
611 ref = lto_varpool_encoder_lookup (varpool_encoder, node->same_comdat_group);
612 gcc_assert (ref != LCC_NOT_FOUND);
614 else
615 ref = LCC_NOT_FOUND;
616 lto_output_sleb128_stream (ob->main_stream, ref);
617 lto_output_uleb128_stream (ob->main_stream, node->resolution);
618 lto_output_uleb128_stream (ob->main_stream, node->module_id);
620 if (count)
622 lto_output_uleb128_stream (ob->main_stream, count);
623 for (alias = node->extra_name; alias; alias = alias->next)
625 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
626 lto_output_uleb128_stream (ob->main_stream, alias->resolution);
631 /* Output the varpool NODE to OB.
632 If NODE is not in SET, then NODE is a boundary. */
634 static void
635 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
636 lto_cgraph_encoder_t encoder,
637 lto_varpool_encoder_t varpool_encoder)
639 struct bitpack_d bp;
640 bp = bitpack_create (ob->main_stream);
641 bp_pack_value (&bp, ref->refered_type, 1);
642 bp_pack_value (&bp, ref->use, 2);
643 lto_output_bitpack (&bp);
644 if (ref->refered_type == IPA_REF_CGRAPH)
646 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
647 gcc_assert (nref != LCC_NOT_FOUND);
648 lto_output_sleb128_stream (ob->main_stream, nref);
650 else
652 int nref = lto_varpool_encoder_lookup (varpool_encoder,
653 ipa_ref_varpool_node (ref));
654 gcc_assert (nref != LCC_NOT_FOUND);
655 lto_output_sleb128_stream (ob->main_stream, nref);
659 /* Stream out profile_summary to OB. */
661 static void
662 output_profile_summary (struct lto_simple_output_block *ob)
664 if (profile_info)
666 /* We do not output num, sum_all and run_max, they are not used by
667 GCC profile feedback and they are difficult to merge from multiple
668 units. */
669 gcc_assert (profile_info->runs);
670 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
671 lto_output_uleb128_stream (ob->main_stream, profile_info->sum_max);
673 else
674 lto_output_uleb128_stream (ob->main_stream, 0);
677 /* Add NODE into encoder as well as nodes it is cloned from.
678 Do it in a way so clones appear first. */
680 static void
681 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node,
682 bool include_body)
684 if (node->clone_of)
685 add_node_to (encoder, node->clone_of, include_body);
686 else if (include_body)
687 lto_set_cgraph_encoder_encode_body (encoder, node);
688 lto_cgraph_encoder_encode (encoder, node);
691 /* Add all references in LIST to encoders. */
693 static void
694 add_references (lto_cgraph_encoder_t encoder,
695 lto_varpool_encoder_t varpool_encoder,
696 struct ipa_ref_list *list)
698 int i;
699 struct ipa_ref *ref;
700 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
701 if (ref->refered_type == IPA_REF_CGRAPH)
702 add_node_to (encoder, ipa_ref_node (ref), false);
703 else
705 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
706 lto_varpool_encoder_encode (varpool_encoder, vnode);
710 /* Output all callees or indirect outgoing edges. EDGE must be the first such
711 edge. */
713 static void
714 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
715 struct lto_simple_output_block *ob,
716 lto_cgraph_encoder_t encoder)
718 if (!edge)
719 return;
721 /* Output edges in backward direction, so the reconstructed callgraph match
722 and it is easy to associate call sites in the IPA pass summaries. */
723 while (edge->next_callee)
724 edge = edge->next_callee;
725 for (; edge; edge = edge->prev_callee)
726 lto_output_edge (ob, edge, encoder);
729 /* Output the part of the cgraph in SET. */
731 static void
732 output_refs (cgraph_node_set set, varpool_node_set vset,
733 lto_cgraph_encoder_t encoder,
734 lto_varpool_encoder_t varpool_encoder)
736 cgraph_node_set_iterator csi;
737 varpool_node_set_iterator vsi;
738 struct lto_simple_output_block *ob;
739 int count;
740 struct ipa_ref *ref;
741 int i;
743 ob = lto_create_simple_output_block (LTO_section_refs);
745 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
747 struct cgraph_node *node = csi_node (csi);
749 count = ipa_ref_list_nreferences (&node->ref_list);
750 if (count)
752 lto_output_uleb128_stream (ob->main_stream, count);
753 lto_output_uleb128_stream (ob->main_stream,
754 lto_cgraph_encoder_lookup (encoder, node));
755 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
756 lto_output_ref (ob, ref, encoder, varpool_encoder);
760 lto_output_uleb128_stream (ob->main_stream, 0);
762 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
764 struct varpool_node *node = vsi_node (vsi);
766 count = ipa_ref_list_nreferences (&node->ref_list);
767 if (count)
769 lto_output_uleb128_stream (ob->main_stream, count);
770 lto_output_uleb128_stream (ob->main_stream,
771 lto_varpool_encoder_lookup (varpool_encoder,
772 node));
773 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
774 lto_output_ref (ob, ref, encoder, varpool_encoder);
778 lto_output_uleb128_stream (ob->main_stream, 0);
780 lto_destroy_simple_output_block (ob);
783 /* Find out all cgraph and varpool nodes we want to encode in current unit
784 and insert them to encoders. */
785 void
786 compute_ltrans_boundary (struct lto_out_decl_state *state,
787 cgraph_node_set set, varpool_node_set vset)
789 struct cgraph_node *node;
790 cgraph_node_set_iterator csi;
791 varpool_node_set_iterator vsi;
792 struct cgraph_edge *edge;
793 int i;
794 lto_cgraph_encoder_t encoder;
795 lto_varpool_encoder_t varpool_encoder;
797 encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
798 varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
800 /* Go over all the nodes in SET and assign references. */
801 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
803 node = csi_node (csi);
804 add_node_to (encoder, node, true);
805 add_references (encoder, varpool_encoder, &node->ref_list);
807 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
809 struct varpool_node *vnode = vsi_node (vsi);
810 gcc_assert (!vnode->alias);
811 lto_varpool_encoder_encode (varpool_encoder, vnode);
812 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
813 add_references (encoder, varpool_encoder, &vnode->ref_list);
815 /* Pickle in also the initializer of all referenced readonly variables
816 to help folding. Constant pool variables are not shared, so we must
817 pickle those too. */
818 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
820 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
821 if (DECL_INITIAL (vnode->decl)
822 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
823 vnode)
824 && const_value_known_p (vnode->decl))
826 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
827 add_references (encoder, varpool_encoder, &vnode->ref_list);
831 /* Go over all the nodes again to include callees that are not in
832 SET. */
833 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
835 node = csi_node (csi);
836 for (edge = node->callees; edge; edge = edge->next_callee)
838 struct cgraph_node *callee = edge->callee;
839 if (!cgraph_node_in_set_p (callee, set))
841 /* We should have moved all the inlines. */
842 gcc_assert (!callee->global.inlined_to);
843 add_node_to (encoder, callee, false);
849 /* Output the part of the cgraph in SET. */
851 void
852 output_cgraph (cgraph_node_set set, varpool_node_set vset)
854 struct cgraph_node *node;
855 struct lto_simple_output_block *ob;
856 cgraph_node_set_iterator csi;
857 int i, n_nodes;
858 lto_cgraph_encoder_t encoder;
859 lto_varpool_encoder_t varpool_encoder;
860 struct cgraph_asm_node *can;
861 static bool asm_nodes_output = false;
863 if (flag_wpa)
864 output_cgraph_opt_summary (set);
866 ob = lto_create_simple_output_block (LTO_section_cgraph);
868 output_profile_summary (ob);
870 /* An encoder for cgraph nodes should have been created by
871 ipa_write_summaries_1. */
872 gcc_assert (ob->decl_state->cgraph_node_encoder);
873 gcc_assert (ob->decl_state->varpool_node_encoder);
874 encoder = ob->decl_state->cgraph_node_encoder;
875 varpool_encoder = ob->decl_state->varpool_node_encoder;
877 /* Write out the nodes. We must first output a node and then its clones,
878 otherwise at a time reading back the node there would be nothing to clone
879 from. */
880 n_nodes = lto_cgraph_encoder_size (encoder);
881 for (i = 0; i < n_nodes; i++)
883 node = lto_cgraph_encoder_deref (encoder, i);
884 lto_output_node (ob, node, encoder, set, vset);
887 /* Go over the nodes in SET again to write edges. */
888 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
890 node = csi_node (csi);
891 output_outgoing_cgraph_edges (node->callees, ob, encoder);
892 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
895 lto_output_uleb128_stream (ob->main_stream, 0);
897 /* Emit toplevel asms.
898 When doing WPA we must output every asm just once. Since we do not partition asm
899 nodes at all, output them to first output. This is kind of hack, but should work
900 well. */
901 if (!asm_nodes_output)
903 asm_nodes_output = true;
904 for (can = cgraph_asm_nodes; can; can = can->next)
906 int len = TREE_STRING_LENGTH (can->asm_str);
907 lto_output_uleb128_stream (ob->main_stream, len);
908 for (i = 0; i < len; ++i)
909 lto_output_1_stream (ob->main_stream,
910 TREE_STRING_POINTER (can->asm_str)[i]);
914 lto_output_uleb128_stream (ob->main_stream, 0);
916 lto_destroy_simple_output_block (ob);
917 output_varpool (set, vset);
918 output_refs (set, vset, encoder, varpool_encoder);
921 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
922 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
923 NODE or to replace the values in it, for instance because the first
924 time we saw it, the function body was not available but now it
925 is. BP is a bitpack with all the bitflags for NODE read from the
926 stream. */
928 static void
929 input_overwrite_node (struct lto_file_decl_data *file_data,
930 struct cgraph_node *node,
931 enum LTO_cgraph_tags tag,
932 struct bitpack_d *bp,
933 unsigned int stack_size,
934 unsigned int self_time,
935 unsigned int time_inlining_benefit,
936 unsigned int self_size,
937 unsigned int size_inlining_benefit,
938 enum ld_plugin_symbol_resolution resolution)
940 node->aux = (void *) tag;
941 node->local.inline_summary.estimated_self_stack_size = stack_size;
942 node->local.inline_summary.self_time = self_time;
943 node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
944 node->local.inline_summary.self_size = self_size;
945 node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
946 node->global.time = self_time;
947 node->global.size = self_size;
948 node->global.estimated_stack_size = stack_size;
949 node->global.estimated_growth = INT_MIN;
950 node->local.lto_file_data = file_data;
952 node->local.local = bp_unpack_value (bp, 1);
953 node->local.externally_visible = bp_unpack_value (bp, 1);
954 node->local.finalized = bp_unpack_value (bp, 1);
955 node->local.inlinable = bp_unpack_value (bp, 1);
956 node->local.versionable = bp_unpack_value (bp, 1);
957 node->local.can_change_signature = bp_unpack_value (bp, 1);
958 node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
959 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
960 node->local.vtable_method = bp_unpack_value (bp, 1);
961 node->needed = bp_unpack_value (bp, 1);
962 node->address_taken = bp_unpack_value (bp, 1);
963 node->abstract_and_needed = bp_unpack_value (bp, 1);
964 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
965 node->lowered = bp_unpack_value (bp, 1);
966 node->analyzed = tag == LTO_cgraph_analyzed_node;
967 node->in_other_partition = bp_unpack_value (bp, 1);
968 if (node->in_other_partition
969 /* Avoid updating decl when we are seeing just inline clone.
970 When inlining function that has functions already inlined into it,
971 we produce clones of inline clones.
973 WPA partitioning might put each clone into different unit and
974 we might end up streaming inline clone from other partition
975 to support clone we are interested in. */
976 && (!node->clone_of
977 || node->clone_of->decl != node->decl))
979 DECL_EXTERNAL (node->decl) = 1;
980 TREE_STATIC (node->decl) = 0;
982 node->alias = bp_unpack_value (bp, 1);
983 node->finalized_by_frontend = bp_unpack_value (bp, 1);
984 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
985 node->only_called_at_startup = bp_unpack_value (bp, 1);
986 node->only_called_at_exit = bp_unpack_value (bp, 1);
987 node->resolution = resolution;
990 /* Output the part of the cgraph in SET. */
992 static void
993 output_varpool (cgraph_node_set set, varpool_node_set vset)
995 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
996 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
997 int len = lto_varpool_encoder_size (varpool_encoder), i;
999 lto_output_uleb128_stream (ob->main_stream, len);
1001 /* Write out the nodes. We must first output a node and then its clones,
1002 otherwise at a time reading back the node there would be nothing to clone
1003 from. */
1004 for (i = 0; i < len; i++)
1006 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
1007 varpool_encoder,
1008 set, vset);
1011 lto_destroy_simple_output_block (ob);
1014 /* Read a node from input_block IB. TAG is the node's tag just read.
1015 Return the node read or overwriten. */
1017 static struct cgraph_node *
1018 input_node (struct lto_file_decl_data *file_data,
1019 struct lto_input_block *ib,
1020 enum LTO_cgraph_tags tag,
1021 VEC(cgraph_node_ptr, heap) *nodes)
1023 tree fn_decl;
1024 struct cgraph_node *node;
1025 struct bitpack_d bp;
1026 int stack_size = 0;
1027 unsigned decl_index;
1028 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1029 int self_time = 0;
1030 int self_size = 0;
1031 int time_inlining_benefit = 0;
1032 int size_inlining_benefit = 0;
1033 unsigned long same_body_count = 0;
1034 int clone_ref;
1035 enum ld_plugin_symbol_resolution resolution;
1037 clone_ref = lto_input_sleb128 (ib);
1039 decl_index = lto_input_uleb128 (ib);
1040 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1042 if (clone_ref != LCC_NOT_FOUND)
1044 node = cgraph_clone_node (VEC_index (cgraph_node_ptr, nodes, clone_ref), fn_decl,
1045 0, CGRAPH_FREQ_BASE, 0, false, NULL);
1047 else
1048 node = cgraph_node (fn_decl);
1050 node->count = lto_input_sleb128 (ib);
1051 node->max_bb_count = lto_input_sleb128 (ib);
1052 node->count_materialization_scale = lto_input_sleb128 (ib);
1054 if (tag == LTO_cgraph_analyzed_node)
1056 stack_size = lto_input_sleb128 (ib);
1057 self_size = lto_input_sleb128 (ib);
1058 size_inlining_benefit = lto_input_sleb128 (ib);
1059 self_time = lto_input_sleb128 (ib);
1060 time_inlining_benefit = lto_input_sleb128 (ib);
1062 ref = lto_input_sleb128 (ib);
1065 ref2 = lto_input_sleb128 (ib);
1067 /* Make sure that we have not read this node before. Nodes that
1068 have already been read will have their tag stored in the 'aux'
1069 field. Since built-in functions can be referenced in multiple
1070 functions, they are expected to be read more than once. */
1071 if (node->aux && !DECL_IS_BUILTIN (node->decl))
1072 internal_error ("bytecode stream: found multiple instances of cgraph "
1073 "node %d", node->uid);
1075 bp = lto_input_bitpack (ib);
1076 resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1077 input_overwrite_node (file_data, node, tag, &bp, stack_size, self_time,
1078 time_inlining_benefit, self_size,
1079 size_inlining_benefit, resolution);
1081 /* Store a reference for now, and fix up later to be a pointer. */
1082 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1084 /* Store a reference for now, and fix up later to be a pointer. */
1085 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1087 same_body_count = lto_input_uleb128 (ib);
1088 while (same_body_count-- > 0)
1090 tree alias_decl;
1091 int type;
1092 struct cgraph_node *alias;
1093 decl_index = lto_input_uleb128 (ib);
1094 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1095 type = lto_input_uleb128 (ib);
1096 if (!type)
1098 tree real_alias;
1099 decl_index = lto_input_uleb128 (ib);
1100 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1101 alias = cgraph_same_body_alias (node, alias_decl, real_alias);
1103 else
1105 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1106 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1107 tree real_alias;
1108 decl_index = lto_input_uleb128 (ib);
1109 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1110 alias = cgraph_add_thunk (node, alias_decl, fn_decl, type & 2, fixed_offset,
1111 virtual_value,
1112 (type & 4) ? size_int (virtual_value) : NULL_TREE,
1113 real_alias);
1115 gcc_assert (alias);
1116 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 struct varpool_node *
1125 input_varpool_node (struct lto_file_decl_data *file_data,
1126 struct lto_input_block *ib)
1128 int decl_index;
1129 tree var_decl;
1130 struct varpool_node *node;
1131 struct bitpack_d bp;
1132 bool aliases_p;
1133 int count;
1134 int ref = LCC_NOT_FOUND;
1136 decl_index = lto_input_uleb128 (ib);
1137 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1138 node = varpool_node (var_decl);
1139 node->lto_file_data = file_data;
1141 bp = lto_input_bitpack (ib);
1142 node->externally_visible = bp_unpack_value (&bp, 1);
1143 node->force_output = bp_unpack_value (&bp, 1);
1144 node->finalized = bp_unpack_value (&bp, 1);
1145 node->alias = bp_unpack_value (&bp, 1);
1146 node->analyzed = node->finalized;
1147 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1148 node->in_other_partition = bp_unpack_value (&bp, 1);
1149 if (node->in_other_partition)
1151 DECL_EXTERNAL (node->decl) = 1;
1152 TREE_STATIC (node->decl) = 0;
1154 aliases_p = bp_unpack_value (&bp, 1);
1155 if (node->finalized)
1156 varpool_mark_needed_node (node);
1157 ref = lto_input_sleb128 (ib);
1158 /* Store a reference for now, and fix up later to be a pointer. */
1159 node->same_comdat_group = (struct varpool_node *) (intptr_t) ref;
1160 node->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1161 node->module_id = lto_input_uleb128(ib);
1162 if (aliases_p)
1164 count = lto_input_uleb128 (ib);
1165 for (; count > 0; count --)
1167 tree decl = lto_file_decl_data_get_var_decl (file_data,
1168 lto_input_uleb128 (ib));
1169 struct varpool_node *alias;
1170 alias = varpool_extra_name_alias (decl, var_decl);
1171 alias->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1174 return node;
1177 /* Read a node from input_block IB. TAG is the node's tag just read.
1178 Return the node read or overwriten. */
1180 static void
1181 input_ref (struct lto_input_block *ib,
1182 struct cgraph_node *refering_node,
1183 struct varpool_node *refering_varpool_node,
1184 VEC(cgraph_node_ptr, heap) *nodes,
1185 VEC(varpool_node_ptr, heap) *varpool_nodes)
1187 struct cgraph_node *node = NULL;
1188 struct varpool_node *varpool_node = NULL;
1189 struct bitpack_d bp;
1190 enum ipa_ref_type type;
1191 enum ipa_ref_use use;
1193 bp = lto_input_bitpack (ib);
1194 type = (enum ipa_ref_type) bp_unpack_value (&bp, 1);
1195 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1196 if (type == IPA_REF_CGRAPH)
1197 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1198 else
1199 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1200 ipa_record_reference (refering_node, refering_varpool_node,
1201 node, varpool_node, use, NULL);
1204 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1205 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1206 edge being read is indirect (in the sense that it has
1207 indirect_unknown_callee set). */
1209 static void
1210 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1211 bool indirect)
1213 struct cgraph_node *caller, *callee;
1214 struct cgraph_edge *edge;
1215 unsigned int stmt_id;
1216 gcov_type count;
1217 int freq;
1218 unsigned int nest;
1219 cgraph_inline_failed_t inline_failed;
1220 struct bitpack_d bp;
1221 int ecf_flags = 0;
1223 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1224 if (caller == NULL || caller->decl == NULL_TREE)
1225 internal_error ("bytecode stream: no caller found while reading edge");
1227 if (!indirect)
1229 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1230 if (callee == NULL || callee->decl == NULL_TREE)
1231 internal_error ("bytecode stream: no callee found while reading edge");
1233 else
1234 callee = NULL;
1236 count = (gcov_type) lto_input_sleb128 (ib);
1238 bp = lto_input_bitpack (ib);
1239 stmt_id = (unsigned int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1240 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (&bp,
1241 HOST_BITS_PER_INT);
1242 freq = (int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1243 nest = (unsigned) bp_unpack_value (&bp, 30);
1245 if (indirect)
1246 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq, nest);
1247 else
1248 edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
1250 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1251 edge->lto_stmt_uid = stmt_id;
1252 edge->inline_failed = inline_failed;
1253 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1254 edge->can_throw_external = bp_unpack_value (&bp, 1);
1255 if (indirect)
1257 if (bp_unpack_value (&bp, 1))
1258 ecf_flags |= ECF_CONST;
1259 if (bp_unpack_value (&bp, 1))
1260 ecf_flags |= ECF_PURE;
1261 if (bp_unpack_value (&bp, 1))
1262 ecf_flags |= ECF_NORETURN;
1263 if (bp_unpack_value (&bp, 1))
1264 ecf_flags |= ECF_MALLOC;
1265 if (bp_unpack_value (&bp, 1))
1266 ecf_flags |= ECF_NOTHROW;
1267 if (bp_unpack_value (&bp, 1))
1268 ecf_flags |= ECF_RETURNS_TWICE;
1269 edge->indirect_info->ecf_flags = ecf_flags;
1274 /* Read a cgraph from IB using the info in FILE_DATA. */
1276 static VEC(cgraph_node_ptr, heap) *
1277 input_cgraph_1 (struct lto_file_decl_data *file_data,
1278 struct lto_input_block *ib)
1280 enum LTO_cgraph_tags tag;
1281 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1282 struct cgraph_node *node;
1283 unsigned i;
1284 unsigned HOST_WIDE_INT len;
1286 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1287 while (tag)
1289 if (tag == LTO_cgraph_edge)
1290 input_edge (ib, nodes, false);
1291 else if (tag == LTO_cgraph_indirect_edge)
1292 input_edge (ib, nodes, true);
1293 else
1295 node = input_node (file_data, ib, tag,nodes);
1296 if (node == NULL || node->decl == NULL_TREE)
1297 internal_error ("bytecode stream: found empty cgraph node");
1298 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1299 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1302 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1305 /* Input toplevel asms. */
1306 len = lto_input_uleb128 (ib);
1307 while (len)
1309 char *str = (char *)xmalloc (len + 1);
1310 for (i = 0; i < len; ++i)
1311 str[i] = lto_input_1_unsigned (ib);
1312 cgraph_add_asm_node (build_string (len, str));
1313 free (str);
1315 len = lto_input_uleb128 (ib);
1317 /* AUX pointers should be all non-zero for nodes read from the stream. */
1318 #ifdef ENABLE_CHECKING
1319 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1320 gcc_assert (node->aux);
1321 #endif
1322 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1324 int ref = (int) (intptr_t) node->global.inlined_to;
1326 /* We share declaration of builtins, so we may read same node twice. */
1327 if (!node->aux)
1328 continue;
1329 node->aux = NULL;
1331 /* Fixup inlined_to from reference to pointer. */
1332 if (ref != LCC_NOT_FOUND)
1333 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1334 else
1335 node->global.inlined_to = NULL;
1337 ref = (int) (intptr_t) node->same_comdat_group;
1339 /* Fixup same_comdat_group from reference to pointer. */
1340 if (ref != LCC_NOT_FOUND)
1341 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1342 else
1343 node->same_comdat_group = NULL;
1345 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1346 node->aux = (void *)1;
1347 return nodes;
1350 /* Read a varpool from IB using the info in FILE_DATA. */
1352 static VEC(varpool_node_ptr, heap) *
1353 input_varpool_1 (struct lto_file_decl_data *file_data,
1354 struct lto_input_block *ib)
1356 unsigned HOST_WIDE_INT len;
1357 VEC(varpool_node_ptr, heap) *varpool = NULL;
1358 int i;
1359 struct varpool_node *node;
1361 len = lto_input_uleb128 (ib);
1362 while (len)
1364 VEC_safe_push (varpool_node_ptr, heap, varpool,
1365 input_varpool_node (file_data, ib));
1366 len--;
1368 #ifdef ENABLE_CHECKING
1369 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1370 gcc_assert (!node->aux);
1371 #endif
1372 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1374 int ref = (int) (intptr_t) node->same_comdat_group;
1375 /* We share declaration of builtins, so we may read same node twice. */
1376 if (node->aux)
1377 continue;
1378 node->aux = (void *)1;
1380 /* Fixup same_comdat_group from reference to pointer. */
1381 if (ref != LCC_NOT_FOUND)
1382 node->same_comdat_group = VEC_index (varpool_node_ptr, varpool, ref);
1383 else
1384 node->same_comdat_group = NULL;
1386 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1387 node->aux = NULL;
1388 return varpool;
1391 /* Input ipa_refs. */
1393 static void
1394 input_refs (struct lto_input_block *ib,
1395 VEC(cgraph_node_ptr, heap) *nodes,
1396 VEC(varpool_node_ptr, heap) *varpool)
1398 int count;
1399 int idx;
1400 while (true)
1402 struct cgraph_node *node;
1403 count = lto_input_uleb128 (ib);
1404 if (!count)
1405 break;
1406 idx = lto_input_uleb128 (ib);
1407 node = VEC_index (cgraph_node_ptr, nodes, idx);
1408 while (count)
1410 input_ref (ib, node, NULL, nodes, varpool);
1411 count--;
1414 while (true)
1416 struct varpool_node *node;
1417 count = lto_input_uleb128 (ib);
1418 if (!count)
1419 break;
1420 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1421 while (count)
1423 input_ref (ib, NULL, node, nodes, varpool);
1424 count--;
1430 static struct gcov_ctr_summary lto_gcov_summary;
1432 /* Input profile_info from IB. */
1433 static void
1434 input_profile_summary (struct lto_input_block *ib,
1435 struct lto_file_decl_data *file_data)
1437 unsigned int runs = lto_input_uleb128 (ib);
1438 if (runs)
1440 file_data->profile_info.runs = runs;
1441 file_data->profile_info.sum_max = lto_input_uleb128 (ib);
1446 /* Rescale profile summaries to the same number of runs in the whole unit. */
1448 static void
1449 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1451 struct lto_file_decl_data *file_data;
1452 unsigned int j;
1453 gcov_unsigned_t max_runs = 0;
1454 struct cgraph_node *node;
1455 struct cgraph_edge *edge;
1457 /* Find unit with maximal number of runs. If we ever get serious about
1458 roundoff errors, we might also consider computing smallest common
1459 multiply. */
1460 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1461 if (max_runs < file_data->profile_info.runs)
1462 max_runs = file_data->profile_info.runs;
1464 if (!max_runs)
1465 return;
1467 /* Simple overflow check. We probably don't need to support that many train
1468 runs. Such a large value probably imply data corruption anyway. */
1469 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1471 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1472 INT_MAX / REG_BR_PROB_BASE);
1473 return;
1476 profile_info = &lto_gcov_summary;
1477 lto_gcov_summary.runs = max_runs;
1478 lto_gcov_summary.sum_max = 0;
1480 /* Rescale all units to the maximal number of runs.
1481 sum_max can not be easily merged, as we have no idea what files come from
1482 the same run. We do not use the info anyway, so leave it 0. */
1483 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1484 if (file_data->profile_info.runs)
1486 int scale = ((REG_BR_PROB_BASE * max_runs
1487 + file_data->profile_info.runs / 2)
1488 / file_data->profile_info.runs);
1489 lto_gcov_summary.sum_max = MAX (lto_gcov_summary.sum_max,
1490 (file_data->profile_info.sum_max
1491 * scale
1492 + REG_BR_PROB_BASE / 2)
1493 / REG_BR_PROB_BASE);
1496 /* Watch roundoff errors. */
1497 if (lto_gcov_summary.sum_max < max_runs)
1498 lto_gcov_summary.sum_max = max_runs;
1500 /* If merging already happent at WPA time, we are done. */
1501 if (flag_ltrans)
1502 return;
1504 /* Now compute count_materialization_scale of each node.
1505 During LTRANS we already have values of count_materialization_scale
1506 computed, so just update them. */
1507 for (node = cgraph_nodes; node; node = node->next)
1508 if (node->local.lto_file_data
1509 && node->local.lto_file_data->profile_info.runs)
1511 int scale;
1513 scale =
1514 ((node->count_materialization_scale * max_runs
1515 + node->local.lto_file_data->profile_info.runs / 2)
1516 / node->local.lto_file_data->profile_info.runs);
1517 node->count_materialization_scale = scale;
1518 if (scale < 0)
1519 fatal_error ("Profile information in %s corrupted",
1520 file_data->file_name);
1522 if (scale == REG_BR_PROB_BASE)
1523 continue;
1524 for (edge = node->callees; edge; edge = edge->next_callee)
1525 edge->count = ((edge->count * scale + REG_BR_PROB_BASE / 2)
1526 / REG_BR_PROB_BASE);
1527 node->count = ((node->count * scale + REG_BR_PROB_BASE / 2)
1528 / REG_BR_PROB_BASE);
1532 /* Input and merge the cgraph from each of the .o files passed to
1533 lto1. */
1535 void
1536 input_cgraph (void)
1538 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1539 struct lto_file_decl_data *file_data;
1540 unsigned int j = 0;
1541 struct cgraph_node *node;
1543 while ((file_data = file_data_vec[j++]))
1545 const char *data;
1546 size_t len;
1547 struct lto_input_block *ib;
1548 VEC(cgraph_node_ptr, heap) *nodes;
1549 VEC(varpool_node_ptr, heap) *varpool;
1551 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1552 &data, &len);
1553 if (!ib)
1554 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1555 input_profile_summary (ib, file_data);
1556 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1557 nodes = input_cgraph_1 (file_data, ib);
1558 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1559 ib, data, len);
1561 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1562 &data, &len);
1563 if (!ib)
1564 fatal_error ("cannot find LTO varpool in %s", file_data->file_name);
1565 varpool = input_varpool_1 (file_data, ib);
1566 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1567 ib, data, len);
1569 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1570 &data, &len);
1571 if (!ib)
1572 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1573 input_refs (ib, nodes, varpool);
1574 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1575 ib, data, len);
1576 if (flag_ltrans)
1577 input_cgraph_opt_summary (nodes);
1578 VEC_free (cgraph_node_ptr, heap, nodes);
1579 VEC_free (varpool_node_ptr, heap, varpool);
1582 merge_profile_summaries (file_data_vec);
1584 /* Clear out the aux field that was used to store enough state to
1585 tell which nodes should be overwritten. */
1586 for (node = cgraph_nodes; node; node = node->next)
1588 /* Some nodes may have been created by cgraph_node. This
1589 happens when the callgraph contains nested functions. If the
1590 node for the parent function was never emitted to the gimple
1591 file, cgraph_node will create a node for it when setting the
1592 context of the nested function. */
1593 if (node->local.lto_file_data)
1594 node->aux = NULL;
1598 /* True when we need optimization summary for NODE. */
1600 static int
1601 output_cgraph_opt_summary_p (struct cgraph_node *node, cgraph_node_set set)
1603 struct cgraph_edge *e;
1605 if (cgraph_node_in_set_p (node, set))
1607 for (e = node->callees; e; e = e->next_callee)
1608 if (e->indirect_info
1609 && e->indirect_info->thunk_delta != 0)
1610 return true;
1612 for (e = node->indirect_calls; e; e = e->next_callee)
1613 if (e->indirect_info->thunk_delta != 0)
1614 return true;
1617 return (node->clone_of
1618 && (node->clone.tree_map
1619 || node->clone.args_to_skip
1620 || node->clone.combined_args_to_skip));
1623 /* Output optimization summary for EDGE to OB. */
1624 static void
1625 output_edge_opt_summary (struct output_block *ob,
1626 struct cgraph_edge *edge)
1628 if (edge->indirect_info)
1629 lto_output_sleb128_stream (ob->main_stream,
1630 edge->indirect_info->thunk_delta);
1631 else
1632 lto_output_sleb128_stream (ob->main_stream, 0);
1635 /* Output optimization summary for NODE to OB. */
1637 static void
1638 output_node_opt_summary (struct output_block *ob,
1639 struct cgraph_node *node,
1640 cgraph_node_set set)
1642 unsigned int index;
1643 bitmap_iterator bi;
1644 struct ipa_replace_map *map;
1645 struct bitpack_d bp;
1646 int i;
1647 struct cgraph_edge *e;
1649 lto_output_uleb128_stream (ob->main_stream,
1650 bitmap_count_bits (node->clone.args_to_skip));
1651 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1652 lto_output_uleb128_stream (ob->main_stream, index);
1653 lto_output_uleb128_stream (ob->main_stream,
1654 bitmap_count_bits (node->clone.combined_args_to_skip));
1655 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1656 lto_output_uleb128_stream (ob->main_stream, index);
1657 lto_output_uleb128_stream (ob->main_stream,
1658 VEC_length (ipa_replace_map_p, node->clone.tree_map));
1659 FOR_EACH_VEC_ELT (ipa_replace_map_p, node->clone.tree_map, i, map)
1661 int parm_num;
1662 tree parm;
1664 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm;
1665 parm = DECL_CHAIN (parm), parm_num++)
1666 if (map->old_tree == parm)
1667 break;
1668 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1669 mechanism to store function local declarations into summaries. */
1670 gcc_assert (parm);
1671 lto_output_uleb128_stream (ob->main_stream, parm_num);
1672 lto_output_tree (ob, map->new_tree, true);
1673 bp = bitpack_create (ob->main_stream);
1674 bp_pack_value (&bp, map->replace_p, 1);
1675 bp_pack_value (&bp, map->ref_p, 1);
1676 lto_output_bitpack (&bp);
1679 if (cgraph_node_in_set_p (node, set))
1681 for (e = node->callees; e; e = e->next_callee)
1682 output_edge_opt_summary (ob, e);
1683 for (e = node->indirect_calls; e; e = e->next_callee)
1684 output_edge_opt_summary (ob, e);
1688 /* Output optimization summaries stored in callgraph.
1689 At the moment it is the clone info structure. */
1691 static void
1692 output_cgraph_opt_summary (cgraph_node_set set)
1694 struct cgraph_node *node;
1695 int i, n_nodes;
1696 lto_cgraph_encoder_t encoder;
1697 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1698 unsigned count = 0;
1700 ob->cgraph_node = NULL;
1701 encoder = ob->decl_state->cgraph_node_encoder;
1702 n_nodes = lto_cgraph_encoder_size (encoder);
1703 for (i = 0; i < n_nodes; i++)
1704 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder, i),
1705 set))
1706 count++;
1707 lto_output_uleb128_stream (ob->main_stream, count);
1708 for (i = 0; i < n_nodes; i++)
1710 node = lto_cgraph_encoder_deref (encoder, i);
1711 if (output_cgraph_opt_summary_p (node, set))
1713 lto_output_uleb128_stream (ob->main_stream, i);
1714 output_node_opt_summary (ob, node, set);
1717 produce_asm (ob, NULL);
1718 destroy_output_block (ob);
1721 /* Input optimisation summary of EDGE. */
1723 static void
1724 input_edge_opt_summary (struct cgraph_edge *edge,
1725 struct lto_input_block *ib_main)
1727 HOST_WIDE_INT thunk_delta;
1728 thunk_delta = lto_input_sleb128 (ib_main);
1729 if (thunk_delta != 0)
1731 gcc_assert (!edge->indirect_info);
1732 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1733 edge->indirect_info->thunk_delta = thunk_delta;
1737 /* Input optimisation summary of NODE. */
1739 static void
1740 input_node_opt_summary (struct cgraph_node *node,
1741 struct lto_input_block *ib_main,
1742 struct data_in *data_in)
1744 int i;
1745 int count;
1746 int bit;
1747 struct bitpack_d bp;
1748 struct cgraph_edge *e;
1750 count = lto_input_uleb128 (ib_main);
1751 if (count)
1752 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1753 for (i = 0; i < count; i++)
1755 bit = lto_input_uleb128 (ib_main);
1756 bitmap_set_bit (node->clone.args_to_skip, bit);
1758 count = lto_input_uleb128 (ib_main);
1759 if (count)
1760 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1761 for (i = 0; i < count; i++)
1763 bit = lto_input_uleb128 (ib_main);
1764 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1766 count = lto_input_uleb128 (ib_main);
1767 for (i = 0; i < count; i++)
1769 int parm_num;
1770 tree parm;
1771 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1773 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1774 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm_num;
1775 parm = DECL_CHAIN (parm))
1776 parm_num --;
1777 map->parm_num = lto_input_uleb128 (ib_main);
1778 map->old_tree = NULL;
1779 map->new_tree = lto_input_tree (ib_main, data_in);
1780 bp = lto_input_bitpack (ib_main);
1781 map->replace_p = bp_unpack_value (&bp, 1);
1782 map->ref_p = bp_unpack_value (&bp, 1);
1784 for (e = node->callees; e; e = e->next_callee)
1785 input_edge_opt_summary (e, ib_main);
1786 for (e = node->indirect_calls; e; e = e->next_callee)
1787 input_edge_opt_summary (e, ib_main);
1790 /* Read section in file FILE_DATA of length LEN with data DATA. */
1792 static void
1793 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1794 const char *data, size_t len, VEC (cgraph_node_ptr,
1795 heap) * nodes)
1797 const struct lto_function_header *header =
1798 (const struct lto_function_header *) data;
1799 const int cfg_offset = sizeof (struct lto_function_header);
1800 const int main_offset = cfg_offset + header->cfg_size;
1801 const int string_offset = main_offset + header->main_size;
1802 struct data_in *data_in;
1803 struct lto_input_block ib_main;
1804 unsigned int i;
1805 unsigned int count;
1807 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1808 header->main_size);
1810 data_in =
1811 lto_data_in_create (file_data, (const char *) data + string_offset,
1812 header->string_size, NULL);
1813 count = lto_input_uleb128 (&ib_main);
1815 for (i = 0; i < count; i++)
1817 int ref = lto_input_uleb128 (&ib_main);
1818 input_node_opt_summary (VEC_index (cgraph_node_ptr, nodes, ref),
1819 &ib_main, data_in);
1821 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1822 len);
1823 lto_data_in_delete (data_in);
1826 /* Input optimization summary of cgraph. */
1828 static void
1829 input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes)
1831 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1832 struct lto_file_decl_data *file_data;
1833 unsigned int j = 0;
1835 while ((file_data = file_data_vec[j++]))
1837 size_t len;
1838 const char *data =
1839 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1840 &len);
1842 if (data)
1843 input_cgraph_opt_section (file_data, data, len, nodes);