ChangeLog config
[official-gcc.git] / gcc / lto-cgraph.c
blob78c809b55209c3c8888706eae39666b6bbfbd627
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright 2009, 2010 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "tree.h"
29 #include "expr.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "hashtab.h"
34 #include "langhooks.h"
35 #include "basic-block.h"
36 #include "tree-flow.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic-core.h"
41 #include "except.h"
42 #include "vec.h"
43 #include "timevar.h"
44 #include "output.h"
45 #include "pointer-set.h"
46 #include "lto-streamer.h"
47 #include "gcov-io.h"
49 static void output_varpool (cgraph_node_set, varpool_node_set);
50 static void output_cgraph_opt_summary (void);
51 static void input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes);
54 /* Cgraph streaming is organized as set of record whose type
55 is indicated by a tag. */
56 enum LTO_cgraph_tags
58 /* Must leave 0 for the stopper. */
60 /* Cgraph node without body available. */
61 LTO_cgraph_unavail_node = 1,
62 /* Cgraph node with function body. */
63 LTO_cgraph_analyzed_node,
64 /* Cgraph edges. */
65 LTO_cgraph_edge,
66 LTO_cgraph_indirect_edge
69 /* Create a new cgraph encoder. */
71 lto_cgraph_encoder_t
72 lto_cgraph_encoder_new (void)
74 lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
75 encoder->map = pointer_map_create ();
76 encoder->nodes = NULL;
77 encoder->body = pointer_set_create ();
78 return encoder;
82 /* Delete ENCODER and its components. */
84 void
85 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
87 VEC_free (cgraph_node_ptr, heap, encoder->nodes);
88 pointer_map_destroy (encoder->map);
89 pointer_set_destroy (encoder->body);
90 free (encoder);
94 /* Return the existing reference number of NODE in the cgraph encoder in
95 output block OB. Assign a new reference if this is the first time
96 NODE is encoded. */
98 int
99 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
100 struct cgraph_node *node)
102 int ref;
103 void **slot;
105 slot = pointer_map_contains (encoder->map, node);
106 if (!slot)
108 ref = VEC_length (cgraph_node_ptr, encoder->nodes);
109 slot = pointer_map_insert (encoder->map, node);
110 *slot = (void *) (intptr_t) ref;
111 VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
113 else
114 ref = (int) (intptr_t) *slot;
116 return ref;
119 #define LCC_NOT_FOUND (-1)
121 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
122 or LCC_NOT_FOUND if it is not there. */
125 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
126 struct cgraph_node *node)
128 void **slot = pointer_map_contains (encoder->map, node);
129 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
133 /* Return the cgraph node corresponding to REF using ENCODER. */
135 struct cgraph_node *
136 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
138 if (ref == LCC_NOT_FOUND)
139 return NULL;
141 return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
145 /* Return TRUE if we should encode initializer of NODE (if any). */
147 bool
148 lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t encoder,
149 struct cgraph_node *node)
151 return pointer_set_contains (encoder->body, node);
154 /* Return TRUE if we should encode body of NODE (if any). */
156 static void
157 lto_set_cgraph_encoder_encode_body (lto_cgraph_encoder_t encoder,
158 struct cgraph_node *node)
160 pointer_set_insert (encoder->body, node);
163 /* Create a new varpool encoder. */
165 lto_varpool_encoder_t
166 lto_varpool_encoder_new (void)
168 lto_varpool_encoder_t encoder = XCNEW (struct lto_varpool_encoder_d);
169 encoder->map = pointer_map_create ();
170 encoder->initializer = pointer_set_create ();
171 encoder->nodes = NULL;
172 return encoder;
176 /* Delete ENCODER and its components. */
178 void
179 lto_varpool_encoder_delete (lto_varpool_encoder_t encoder)
181 VEC_free (varpool_node_ptr, heap, encoder->nodes);
182 pointer_map_destroy (encoder->map);
183 pointer_set_destroy (encoder->initializer);
184 free (encoder);
188 /* Return the existing reference number of NODE in the varpool encoder in
189 output block OB. Assign a new reference if this is the first time
190 NODE is encoded. */
193 lto_varpool_encoder_encode (lto_varpool_encoder_t encoder,
194 struct varpool_node *node)
196 int ref;
197 void **slot;
199 slot = pointer_map_contains (encoder->map, node);
200 if (!slot)
202 ref = VEC_length (varpool_node_ptr, encoder->nodes);
203 slot = pointer_map_insert (encoder->map, node);
204 *slot = (void *) (intptr_t) ref;
205 VEC_safe_push (varpool_node_ptr, heap, encoder->nodes, node);
207 else
208 ref = (int) (intptr_t) *slot;
210 return ref;
213 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
214 or LCC_NOT_FOUND if it is not there. */
217 lto_varpool_encoder_lookup (lto_varpool_encoder_t encoder,
218 struct varpool_node *node)
220 void **slot = pointer_map_contains (encoder->map, node);
221 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
225 /* Return the varpool node corresponding to REF using ENCODER. */
227 struct varpool_node *
228 lto_varpool_encoder_deref (lto_varpool_encoder_t encoder, int ref)
230 if (ref == LCC_NOT_FOUND)
231 return NULL;
233 return VEC_index (varpool_node_ptr, encoder->nodes, ref);
237 /* Return TRUE if we should encode initializer of NODE (if any). */
239 bool
240 lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t encoder,
241 struct varpool_node *node)
243 return pointer_set_contains (encoder->initializer, node);
246 /* Return TRUE if we should encode initializer of NODE (if any). */
248 static void
249 lto_set_varpool_encoder_encode_initializer (lto_varpool_encoder_t encoder,
250 struct varpool_node *node)
252 pointer_set_insert (encoder->initializer, node);
255 /* Output the cgraph EDGE to OB using ENCODER. */
257 static void
258 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
259 lto_cgraph_encoder_t encoder)
261 unsigned int uid;
262 intptr_t ref;
263 struct bitpack_d bp;
265 if (edge->indirect_unknown_callee)
266 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_indirect_edge);
267 else
268 lto_output_uleb128_stream (ob->main_stream, LTO_cgraph_edge);
270 ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
271 gcc_assert (ref != LCC_NOT_FOUND);
272 lto_output_sleb128_stream (ob->main_stream, ref);
274 if (!edge->indirect_unknown_callee)
276 ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
277 gcc_assert (ref != LCC_NOT_FOUND);
278 lto_output_sleb128_stream (ob->main_stream, ref);
281 lto_output_sleb128_stream (ob->main_stream, edge->count);
283 bp = bitpack_create (ob->main_stream);
284 uid = (!gimple_has_body_p (edge->caller->decl)
285 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt));
286 bp_pack_value (&bp, uid, HOST_BITS_PER_INT);
287 bp_pack_value (&bp, edge->inline_failed, HOST_BITS_PER_INT);
288 bp_pack_value (&bp, edge->frequency, HOST_BITS_PER_INT);
289 bp_pack_value (&bp, edge->loop_nest, 30);
290 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
291 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
292 bp_pack_value (&bp, edge->can_throw_external, 1);
293 if (edge->indirect_unknown_callee)
295 int flags = edge->indirect_info->ecf_flags;
296 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
297 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
300 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
301 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
302 /* Flags that should not appear on indirect calls. */
303 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
304 | ECF_MAY_BE_ALLOCA
305 | ECF_SIBCALL
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 if (!node->analyzed)
387 return false;
388 if (node->global.inlined_to)
389 return false;
390 for (e = node->callers; e; e = e->next_caller)
391 if (cgraph_node_in_set_p (e->caller, set))
392 return true;
393 return false;
396 /* Output the cgraph NODE to OB. ENCODER is used to find the
397 reference number of NODE->inlined_to. SET is the set of nodes we
398 are writing to the current file. If NODE is not in SET, then NODE
399 is a boundary of a cgraph_node_set and we pretend NODE just has a
400 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
401 that have had their callgraph node written so far. This is used to
402 determine if NODE is a clone of a previously written node. */
404 static void
405 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
406 lto_cgraph_encoder_t encoder, cgraph_node_set set,
407 varpool_node_set vset)
409 unsigned int tag;
410 struct bitpack_d bp;
411 bool boundary_p;
412 intptr_t ref;
413 bool in_other_partition = false;
414 struct cgraph_node *clone_of;
416 boundary_p = !cgraph_node_in_set_p (node, set);
418 if (node->analyzed && !boundary_p)
419 tag = LTO_cgraph_analyzed_node;
420 else
421 tag = LTO_cgraph_unavail_node;
423 lto_output_uleb128_stream (ob->main_stream, tag);
425 /* In WPA mode, we only output part of the call-graph. Also, we
426 fake cgraph node attributes. There are two cases that we care.
428 Boundary nodes: There are nodes that are not part of SET but are
429 called from within SET. We artificially make them look like
430 externally visible nodes with no function body.
432 Cherry-picked nodes: These are nodes we pulled from other
433 translation units into SET during IPA-inlining. We make them as
434 local static nodes to prevent clashes with other local statics. */
435 if (boundary_p && node->analyzed)
437 /* Inline clones can not be part of boundary.
438 gcc_assert (!node->global.inlined_to);
440 FIXME: At the moment they can be, when partition contains an inline
441 clone that is clone of inline clone from outside partition. We can
442 reshape the clone tree and make other tree to be the root, but it
443 needs a bit extra work and will be promplty done by cgraph_remove_node
444 after reading back. */
445 in_other_partition = 1;
448 clone_of = node->clone_of;
449 while (clone_of
450 && (ref = lto_cgraph_encoder_lookup (encoder, node->clone_of)) == LCC_NOT_FOUND)
451 if (clone_of->prev_sibling_clone)
452 clone_of = clone_of->prev_sibling_clone;
453 else
454 clone_of = clone_of->clone_of;
455 if (!clone_of)
456 lto_output_sleb128_stream (ob->main_stream, LCC_NOT_FOUND);
457 else
458 lto_output_sleb128_stream (ob->main_stream, ref);
461 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
462 lto_output_sleb128_stream (ob->main_stream, node->count);
464 if (tag == LTO_cgraph_analyzed_node)
466 lto_output_sleb128_stream (ob->main_stream,
467 node->local.inline_summary.estimated_self_stack_size);
468 lto_output_sleb128_stream (ob->main_stream,
469 node->local.inline_summary.self_size);
470 lto_output_sleb128_stream (ob->main_stream,
471 node->local.inline_summary.size_inlining_benefit);
472 lto_output_sleb128_stream (ob->main_stream,
473 node->local.inline_summary.self_time);
474 lto_output_sleb128_stream (ob->main_stream,
475 node->local.inline_summary.time_inlining_benefit);
476 if (node->global.inlined_to)
478 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
479 gcc_assert (ref != LCC_NOT_FOUND);
481 else
482 ref = LCC_NOT_FOUND;
484 lto_output_sleb128_stream (ob->main_stream, ref);
487 if (node->same_comdat_group && !boundary_p)
489 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
490 gcc_assert (ref != LCC_NOT_FOUND);
492 else
493 ref = LCC_NOT_FOUND;
494 lto_output_sleb128_stream (ob->main_stream, ref);
496 bp = bitpack_create (ob->main_stream);
497 bp_pack_value (&bp, node->local.local, 1);
498 bp_pack_value (&bp, node->local.externally_visible, 1);
499 bp_pack_value (&bp, node->local.finalized, 1);
500 bp_pack_value (&bp, node->local.inlinable, 1);
501 bp_pack_value (&bp, node->local.versionable, 1);
502 bp_pack_value (&bp, node->local.disregard_inline_limits, 1);
503 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
504 bp_pack_value (&bp, node->local.vtable_method, 1);
505 bp_pack_value (&bp, node->needed, 1);
506 bp_pack_value (&bp, node->address_taken, 1);
507 bp_pack_value (&bp, node->abstract_and_needed, 1);
508 bp_pack_value (&bp, tag == LTO_cgraph_analyzed_node
509 && !DECL_EXTERNAL (node->decl)
510 && !DECL_COMDAT (node->decl)
511 && (reachable_from_other_partition_p (node, set)
512 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
513 bp_pack_value (&bp, node->lowered, 1);
514 bp_pack_value (&bp, in_other_partition, 1);
515 bp_pack_value (&bp, node->alias, 1);
516 bp_pack_value (&bp, node->finalized_by_frontend, 1);
517 bp_pack_value (&bp, node->frequency, 2);
518 lto_output_bitpack (&bp);
520 if (node->same_body)
522 struct cgraph_node *alias;
523 unsigned long alias_count = 1;
524 for (alias = node->same_body; alias->next; alias = alias->next)
525 alias_count++;
526 lto_output_uleb128_stream (ob->main_stream, alias_count);
529 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
530 alias->decl);
531 if (alias->thunk.thunk_p)
533 lto_output_uleb128_stream
534 (ob->main_stream,
535 1 + (alias->thunk.this_adjusting != 0) * 2
536 + (alias->thunk.virtual_offset_p != 0) * 4);
537 lto_output_uleb128_stream (ob->main_stream,
538 alias->thunk.fixed_offset);
539 lto_output_uleb128_stream (ob->main_stream,
540 alias->thunk.virtual_value);
541 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
542 alias->thunk.alias);
544 else
546 lto_output_uleb128_stream (ob->main_stream, 0);
547 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
548 alias->thunk.alias);
550 alias = alias->previous;
552 while (alias);
554 else
555 lto_output_uleb128_stream (ob->main_stream, 0);
558 /* Output the varpool NODE to OB.
559 If NODE is not in SET, then NODE is a boundary. */
561 static void
562 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
563 lto_varpool_encoder_t varpool_encoder,
564 cgraph_node_set set, varpool_node_set vset)
566 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
567 struct bitpack_d bp;
568 struct varpool_node *alias;
569 int count = 0;
570 int ref;
572 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
573 bp = bitpack_create (ob->main_stream);
574 bp_pack_value (&bp, node->externally_visible, 1);
575 bp_pack_value (&bp, node->force_output, 1);
576 bp_pack_value (&bp, node->finalized, 1);
577 bp_pack_value (&bp, node->alias, 1);
578 bp_pack_value (&bp, node->const_value_known, 1);
579 gcc_assert (!node->alias || !node->extra_name);
580 gcc_assert (node->finalized || !node->analyzed);
581 gcc_assert (node->needed);
582 /* Constant pool initializers can be de-unified into individual ltrans units.
583 FIXME: Alternatively at -Os we may want to avoid generating for them the local
584 labels and share them across LTRANS partitions. */
585 if (DECL_IN_CONSTANT_POOL (node->decl)
586 && !DECL_COMDAT (node->decl))
588 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
589 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
591 else
593 bp_pack_value (&bp, node->analyzed
594 && referenced_from_other_partition_p (&node->ref_list,
595 set, vset), 1);
596 bp_pack_value (&bp, boundary_p, 1); /* in_other_partition. */
598 /* Also emit any extra name aliases. */
599 for (alias = node->extra_name; alias; alias = alias->next)
600 count++;
601 bp_pack_value (&bp, count != 0, 1);
602 lto_output_bitpack (&bp);
603 if (node->same_comdat_group && !boundary_p)
605 ref = lto_varpool_encoder_lookup (varpool_encoder, node->same_comdat_group);
606 gcc_assert (ref != LCC_NOT_FOUND);
608 else
609 ref = LCC_NOT_FOUND;
610 lto_output_sleb128_stream (ob->main_stream, ref);
612 if (count)
614 lto_output_uleb128_stream (ob->main_stream, count);
615 for (alias = node->extra_name; alias; alias = alias->next)
616 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
620 /* Output the varpool NODE to OB.
621 If NODE is not in SET, then NODE is a boundary. */
623 static void
624 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
625 lto_cgraph_encoder_t encoder,
626 lto_varpool_encoder_t varpool_encoder)
628 struct bitpack_d bp;
629 bp = bitpack_create (ob->main_stream);
630 bp_pack_value (&bp, ref->refered_type, 1);
631 bp_pack_value (&bp, ref->use, 2);
632 lto_output_bitpack (&bp);
633 if (ref->refered_type == IPA_REF_CGRAPH)
635 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
636 gcc_assert (nref != LCC_NOT_FOUND);
637 lto_output_sleb128_stream (ob->main_stream, nref);
639 else
641 int nref = lto_varpool_encoder_lookup (varpool_encoder,
642 ipa_ref_varpool_node (ref));
643 gcc_assert (nref != LCC_NOT_FOUND);
644 lto_output_sleb128_stream (ob->main_stream, nref);
648 /* Stream out profile_summary to OB. */
650 static void
651 output_profile_summary (struct lto_simple_output_block *ob)
653 if (profile_info)
655 /* We do not output num, it is not terribly useful. */
656 gcc_assert (profile_info->runs);
657 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
658 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
659 lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
660 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
662 else
663 lto_output_uleb128_stream (ob->main_stream, 0);
666 /* Add NODE into encoder as well as nodes it is cloned from.
667 Do it in a way so clones appear first. */
669 static void
670 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node,
671 bool include_body)
673 if (node->clone_of)
674 add_node_to (encoder, node->clone_of, include_body);
675 else if (include_body)
676 lto_set_cgraph_encoder_encode_body (encoder, node);
677 lto_cgraph_encoder_encode (encoder, node);
680 /* Add all references in LIST to encoders. */
682 static void
683 add_references (lto_cgraph_encoder_t encoder,
684 lto_varpool_encoder_t varpool_encoder,
685 struct ipa_ref_list *list)
687 int i;
688 struct ipa_ref *ref;
689 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
690 if (ref->refered_type == IPA_REF_CGRAPH)
691 add_node_to (encoder, ipa_ref_node (ref), false);
692 else
694 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
695 lto_varpool_encoder_encode (varpool_encoder, vnode);
699 /* Output all callees or indirect outgoing edges. EDGE must be the first such
700 edge. */
702 static void
703 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
704 struct lto_simple_output_block *ob,
705 lto_cgraph_encoder_t encoder)
707 if (!edge)
708 return;
710 /* Output edges in backward direction, so the reconstructed callgraph match
711 and it is easy to associate call sites in the IPA pass summaries. */
712 while (edge->next_callee)
713 edge = edge->next_callee;
714 for (; edge; edge = edge->prev_callee)
715 lto_output_edge (ob, edge, encoder);
718 /* Output the part of the cgraph in SET. */
720 static void
721 output_refs (cgraph_node_set set, varpool_node_set vset,
722 lto_cgraph_encoder_t encoder,
723 lto_varpool_encoder_t varpool_encoder)
725 cgraph_node_set_iterator csi;
726 varpool_node_set_iterator vsi;
727 struct lto_simple_output_block *ob;
728 int count;
729 struct ipa_ref *ref;
730 int i;
732 ob = lto_create_simple_output_block (LTO_section_refs);
734 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
736 struct cgraph_node *node = csi_node (csi);
738 count = ipa_ref_list_nreferences (&node->ref_list);
739 if (count)
741 lto_output_uleb128_stream (ob->main_stream, count);
742 lto_output_uleb128_stream (ob->main_stream,
743 lto_cgraph_encoder_lookup (encoder, node));
744 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
745 lto_output_ref (ob, ref, encoder, varpool_encoder);
749 lto_output_uleb128_stream (ob->main_stream, 0);
751 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
753 struct varpool_node *node = vsi_node (vsi);
755 count = ipa_ref_list_nreferences (&node->ref_list);
756 if (count)
758 lto_output_uleb128_stream (ob->main_stream, count);
759 lto_output_uleb128_stream (ob->main_stream,
760 lto_varpool_encoder_lookup (varpool_encoder,
761 node));
762 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
763 lto_output_ref (ob, ref, encoder, varpool_encoder);
767 lto_output_uleb128_stream (ob->main_stream, 0);
769 lto_destroy_simple_output_block (ob);
772 /* Find out all cgraph and varpool nodes we want to encode in current unit
773 and insert them to encoders. */
774 void
775 compute_ltrans_boundary (struct lto_out_decl_state *state,
776 cgraph_node_set set, varpool_node_set vset)
778 struct cgraph_node *node;
779 cgraph_node_set_iterator csi;
780 varpool_node_set_iterator vsi;
781 struct cgraph_edge *edge;
782 int i;
783 lto_cgraph_encoder_t encoder;
784 lto_varpool_encoder_t varpool_encoder;
786 encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
787 varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
789 /* Go over all the nodes in SET and assign references. */
790 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
792 node = csi_node (csi);
793 add_node_to (encoder, node, true);
794 add_references (encoder, varpool_encoder, &node->ref_list);
796 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
798 struct varpool_node *vnode = vsi_node (vsi);
799 gcc_assert (!vnode->alias);
800 lto_varpool_encoder_encode (varpool_encoder, vnode);
801 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
802 add_references (encoder, varpool_encoder, &vnode->ref_list);
804 /* Pickle in also the initializer of all referenced readonly variables
805 to help folding. Constant pool variables are not shared, so we must
806 pickle those too. */
807 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
809 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
810 if (DECL_INITIAL (vnode->decl)
811 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
812 vnode)
813 && (DECL_IN_CONSTANT_POOL (vnode->decl)
814 || TREE_READONLY (vnode->decl)))
816 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
817 add_references (encoder, varpool_encoder, &vnode->ref_list);
821 /* Go over all the nodes again to include callees that are not in
822 SET. */
823 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
825 node = csi_node (csi);
826 for (edge = node->callees; edge; edge = edge->next_callee)
828 struct cgraph_node *callee = edge->callee;
829 if (!cgraph_node_in_set_p (callee, set))
831 /* We should have moved all the inlines. */
832 gcc_assert (!callee->global.inlined_to);
833 add_node_to (encoder, callee, false);
839 /* Output the part of the cgraph in SET. */
841 void
842 output_cgraph (cgraph_node_set set, varpool_node_set vset)
844 struct cgraph_node *node;
845 struct lto_simple_output_block *ob;
846 cgraph_node_set_iterator csi;
847 int i, n_nodes;
848 lto_cgraph_encoder_t encoder;
849 lto_varpool_encoder_t varpool_encoder;
850 struct cgraph_asm_node *can;
851 static bool asm_nodes_output = false;
853 if (flag_wpa)
854 output_cgraph_opt_summary ();
856 ob = lto_create_simple_output_block (LTO_section_cgraph);
858 output_profile_summary (ob);
860 /* An encoder for cgraph nodes should have been created by
861 ipa_write_summaries_1. */
862 gcc_assert (ob->decl_state->cgraph_node_encoder);
863 gcc_assert (ob->decl_state->varpool_node_encoder);
864 encoder = ob->decl_state->cgraph_node_encoder;
865 varpool_encoder = ob->decl_state->varpool_node_encoder;
867 /* Write out the nodes. We must first output a node and then its clones,
868 otherwise at a time reading back the node there would be nothing to clone
869 from. */
870 n_nodes = lto_cgraph_encoder_size (encoder);
871 for (i = 0; i < n_nodes; i++)
873 node = lto_cgraph_encoder_deref (encoder, i);
874 lto_output_node (ob, node, encoder, set, vset);
877 /* Go over the nodes in SET again to write edges. */
878 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
880 node = csi_node (csi);
881 output_outgoing_cgraph_edges (node->callees, ob, encoder);
882 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
885 lto_output_uleb128_stream (ob->main_stream, 0);
887 /* Emit toplevel asms.
888 When doing WPA we must output every asm just once. Since we do not partition asm
889 nodes at all, output them to first output. This is kind of hack, but should work
890 well. */
891 if (!asm_nodes_output)
893 asm_nodes_output = true;
894 for (can = cgraph_asm_nodes; can; can = can->next)
896 int len = TREE_STRING_LENGTH (can->asm_str);
897 lto_output_uleb128_stream (ob->main_stream, len);
898 for (i = 0; i < len; ++i)
899 lto_output_1_stream (ob->main_stream,
900 TREE_STRING_POINTER (can->asm_str)[i]);
904 lto_output_uleb128_stream (ob->main_stream, 0);
906 lto_destroy_simple_output_block (ob);
907 output_varpool (set, vset);
908 output_refs (set, vset, encoder, varpool_encoder);
911 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
912 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
913 NODE or to replace the values in it, for instance because the first
914 time we saw it, the function body was not available but now it
915 is. BP is a bitpack with all the bitflags for NODE read from the
916 stream. */
918 static void
919 input_overwrite_node (struct lto_file_decl_data *file_data,
920 struct cgraph_node *node,
921 enum LTO_cgraph_tags tag,
922 struct bitpack_d *bp,
923 unsigned int stack_size,
924 unsigned int self_time,
925 unsigned int time_inlining_benefit,
926 unsigned int self_size,
927 unsigned int size_inlining_benefit)
929 node->aux = (void *) tag;
930 node->local.inline_summary.estimated_self_stack_size = stack_size;
931 node->local.inline_summary.self_time = self_time;
932 node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
933 node->local.inline_summary.self_size = self_size;
934 node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
935 node->global.time = self_time;
936 node->global.size = self_size;
937 node->global.estimated_stack_size = stack_size;
938 node->global.estimated_growth = INT_MIN;
939 node->local.lto_file_data = file_data;
941 node->local.local = bp_unpack_value (bp, 1);
942 node->local.externally_visible = bp_unpack_value (bp, 1);
943 node->local.finalized = bp_unpack_value (bp, 1);
944 node->local.inlinable = bp_unpack_value (bp, 1);
945 node->local.versionable = bp_unpack_value (bp, 1);
946 node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
947 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
948 node->local.vtable_method = bp_unpack_value (bp, 1);
949 node->needed = bp_unpack_value (bp, 1);
950 node->address_taken = bp_unpack_value (bp, 1);
951 node->abstract_and_needed = bp_unpack_value (bp, 1);
952 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
953 node->lowered = bp_unpack_value (bp, 1);
954 node->analyzed = tag == LTO_cgraph_analyzed_node;
955 node->in_other_partition = bp_unpack_value (bp, 1);
956 if (node->in_other_partition)
958 DECL_EXTERNAL (node->decl) = 1;
959 TREE_STATIC (node->decl) = 0;
961 node->alias = bp_unpack_value (bp, 1);
962 node->finalized_by_frontend = bp_unpack_value (bp, 1);
963 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
966 /* Output the part of the cgraph in SET. */
968 static void
969 output_varpool (cgraph_node_set set, varpool_node_set vset)
971 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
972 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
973 int len = lto_varpool_encoder_size (varpool_encoder), i;
975 lto_output_uleb128_stream (ob->main_stream, len);
977 /* Write out the nodes. We must first output a node and then its clones,
978 otherwise at a time reading back the node there would be nothing to clone
979 from. */
980 for (i = 0; i < len; i++)
982 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
983 varpool_encoder,
984 set, vset);
987 lto_destroy_simple_output_block (ob);
990 /* Read a node from input_block IB. TAG is the node's tag just read.
991 Return the node read or overwriten. */
993 static struct cgraph_node *
994 input_node (struct lto_file_decl_data *file_data,
995 struct lto_input_block *ib,
996 enum LTO_cgraph_tags tag,
997 VEC(cgraph_node_ptr, heap) *nodes)
999 tree fn_decl;
1000 struct cgraph_node *node;
1001 struct bitpack_d bp;
1002 int stack_size = 0;
1003 unsigned decl_index;
1004 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1005 int self_time = 0;
1006 int self_size = 0;
1007 int time_inlining_benefit = 0;
1008 int size_inlining_benefit = 0;
1009 unsigned long same_body_count = 0;
1010 int clone_ref;
1012 clone_ref = lto_input_sleb128 (ib);
1014 decl_index = lto_input_uleb128 (ib);
1015 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1017 if (clone_ref != LCC_NOT_FOUND)
1019 node = cgraph_clone_node (VEC_index (cgraph_node_ptr, nodes, clone_ref), fn_decl,
1020 0, CGRAPH_FREQ_BASE, 0, false, NULL);
1022 else
1023 node = cgraph_node (fn_decl);
1025 node->count = lto_input_sleb128 (ib);
1027 if (tag == LTO_cgraph_analyzed_node)
1029 stack_size = lto_input_sleb128 (ib);
1030 self_size = lto_input_sleb128 (ib);
1031 size_inlining_benefit = lto_input_sleb128 (ib);
1032 self_time = lto_input_sleb128 (ib);
1033 time_inlining_benefit = lto_input_sleb128 (ib);
1035 ref = lto_input_sleb128 (ib);
1038 ref2 = lto_input_sleb128 (ib);
1040 /* Make sure that we have not read this node before. Nodes that
1041 have already been read will have their tag stored in the 'aux'
1042 field. Since built-in functions can be referenced in multiple
1043 functions, they are expected to be read more than once. */
1044 if (node->aux && !DECL_IS_BUILTIN (node->decl))
1045 internal_error ("bytecode stream: found multiple instances of cgraph "
1046 "node %d", node->uid);
1048 bp = lto_input_bitpack (ib);
1049 input_overwrite_node (file_data, node, tag, &bp, stack_size, self_time,
1050 time_inlining_benefit, self_size,
1051 size_inlining_benefit);
1053 /* Store a reference for now, and fix up later to be a pointer. */
1054 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1056 /* Store a reference for now, and fix up later to be a pointer. */
1057 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1059 same_body_count = lto_input_uleb128 (ib);
1060 while (same_body_count-- > 0)
1062 tree alias_decl;
1063 int type;
1064 decl_index = lto_input_uleb128 (ib);
1065 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1066 type = lto_input_uleb128 (ib);
1067 if (!type)
1069 tree real_alias;
1070 decl_index = lto_input_uleb128 (ib);
1071 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1072 cgraph_same_body_alias (alias_decl, real_alias);
1074 else
1076 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1077 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1078 tree real_alias;
1079 decl_index = lto_input_uleb128 (ib);
1080 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1081 cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
1082 virtual_value,
1083 (type & 4) ? size_int (virtual_value) : NULL_TREE,
1084 real_alias);
1087 return node;
1090 /* Read a node from input_block IB. TAG is the node's tag just read.
1091 Return the node read or overwriten. */
1093 static struct varpool_node *
1094 input_varpool_node (struct lto_file_decl_data *file_data,
1095 struct lto_input_block *ib)
1097 int decl_index;
1098 tree var_decl;
1099 struct varpool_node *node;
1100 struct bitpack_d bp;
1101 bool aliases_p;
1102 int count;
1103 int ref = LCC_NOT_FOUND;
1105 decl_index = lto_input_uleb128 (ib);
1106 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1107 node = varpool_node (var_decl);
1108 node->lto_file_data = file_data;
1110 bp = lto_input_bitpack (ib);
1111 node->externally_visible = bp_unpack_value (&bp, 1);
1112 node->force_output = bp_unpack_value (&bp, 1);
1113 node->finalized = bp_unpack_value (&bp, 1);
1114 node->alias = bp_unpack_value (&bp, 1);
1115 node->const_value_known = bp_unpack_value (&bp, 1);
1116 node->analyzed = node->finalized;
1117 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1118 node->in_other_partition = bp_unpack_value (&bp, 1);
1119 if (node->in_other_partition)
1121 DECL_EXTERNAL (node->decl) = 1;
1122 TREE_STATIC (node->decl) = 0;
1124 aliases_p = bp_unpack_value (&bp, 1);
1125 if (node->finalized)
1126 varpool_mark_needed_node (node);
1127 ref = lto_input_sleb128 (ib);
1128 /* Store a reference for now, and fix up later to be a pointer. */
1129 node->same_comdat_group = (struct varpool_node *) (intptr_t) ref;
1130 if (aliases_p)
1132 count = lto_input_uleb128 (ib);
1133 for (; count > 0; count --)
1135 tree decl = lto_file_decl_data_get_var_decl (file_data,
1136 lto_input_uleb128 (ib));
1137 varpool_extra_name_alias (decl, var_decl);
1140 return node;
1143 /* Read a node from input_block IB. TAG is the node's tag just read.
1144 Return the node read or overwriten. */
1146 static void
1147 input_ref (struct lto_input_block *ib,
1148 struct cgraph_node *refering_node,
1149 struct varpool_node *refering_varpool_node,
1150 VEC(cgraph_node_ptr, heap) *nodes,
1151 VEC(varpool_node_ptr, heap) *varpool_nodes)
1153 struct cgraph_node *node = NULL;
1154 struct varpool_node *varpool_node = NULL;
1155 struct bitpack_d bp;
1156 enum ipa_ref_type type;
1157 enum ipa_ref_use use;
1159 bp = lto_input_bitpack (ib);
1160 type = (enum ipa_ref_type) bp_unpack_value (&bp, 1);
1161 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1162 if (type == IPA_REF_CGRAPH)
1163 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1164 else
1165 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1166 ipa_record_reference (refering_node, refering_varpool_node,
1167 node, varpool_node, use, NULL);
1170 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1171 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1172 edge being read is indirect (in the sense that it has
1173 indirect_unknown_callee set). */
1175 static void
1176 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1177 bool indirect)
1179 struct cgraph_node *caller, *callee;
1180 struct cgraph_edge *edge;
1181 unsigned int stmt_id;
1182 gcov_type count;
1183 int freq;
1184 unsigned int nest;
1185 cgraph_inline_failed_t inline_failed;
1186 struct bitpack_d bp;
1187 int ecf_flags = 0;
1189 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1190 if (caller == NULL || caller->decl == NULL_TREE)
1191 internal_error ("bytecode stream: no caller found while reading edge");
1193 if (!indirect)
1195 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1196 if (callee == NULL || callee->decl == NULL_TREE)
1197 internal_error ("bytecode stream: no callee found while reading edge");
1199 else
1200 callee = NULL;
1202 count = (gcov_type) lto_input_sleb128 (ib);
1204 bp = lto_input_bitpack (ib);
1205 stmt_id = (unsigned int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1206 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (&bp,
1207 HOST_BITS_PER_INT);
1208 freq = (int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1209 nest = (unsigned) bp_unpack_value (&bp, 30);
1211 if (indirect)
1212 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq, nest);
1213 else
1214 edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
1216 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1217 edge->lto_stmt_uid = stmt_id;
1218 edge->inline_failed = inline_failed;
1219 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1220 edge->can_throw_external = bp_unpack_value (&bp, 1);
1221 if (indirect)
1223 if (bp_unpack_value (&bp, 1))
1224 ecf_flags |= ECF_CONST;
1225 if (bp_unpack_value (&bp, 1))
1226 ecf_flags |= ECF_PURE;
1227 if (bp_unpack_value (&bp, 1))
1228 ecf_flags |= ECF_NORETURN;
1229 if (bp_unpack_value (&bp, 1))
1230 ecf_flags |= ECF_MALLOC;
1231 if (bp_unpack_value (&bp, 1))
1232 ecf_flags |= ECF_NOTHROW;
1233 if (bp_unpack_value (&bp, 1))
1234 ecf_flags |= ECF_RETURNS_TWICE;
1235 edge->indirect_info->ecf_flags = ecf_flags;
1240 /* Read a cgraph from IB using the info in FILE_DATA. */
1242 static VEC(cgraph_node_ptr, heap) *
1243 input_cgraph_1 (struct lto_file_decl_data *file_data,
1244 struct lto_input_block *ib)
1246 enum LTO_cgraph_tags tag;
1247 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1248 struct cgraph_node *node;
1249 unsigned i;
1250 unsigned HOST_WIDE_INT len;
1252 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1253 while (tag)
1255 if (tag == LTO_cgraph_edge)
1256 input_edge (ib, nodes, false);
1257 else if (tag == LTO_cgraph_indirect_edge)
1258 input_edge (ib, nodes, true);
1259 else
1261 node = input_node (file_data, ib, tag,nodes);
1262 if (node == NULL || node->decl == NULL_TREE)
1263 internal_error ("bytecode stream: found empty cgraph node");
1264 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1265 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1268 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1271 /* Input toplevel asms. */
1272 len = lto_input_uleb128 (ib);
1273 while (len)
1275 char *str = (char *)xmalloc (len + 1);
1276 for (i = 0; i < len; ++i)
1277 str[i] = lto_input_1_unsigned (ib);
1278 cgraph_add_asm_node (build_string (len, str));
1279 free (str);
1281 len = lto_input_uleb128 (ib);
1284 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1286 int ref = (int) (intptr_t) node->global.inlined_to;
1288 /* Fixup inlined_to from reference to pointer. */
1289 if (ref != LCC_NOT_FOUND)
1290 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1291 else
1292 node->global.inlined_to = NULL;
1294 ref = (int) (intptr_t) node->same_comdat_group;
1296 /* Fixup same_comdat_group from reference to pointer. */
1297 if (ref != LCC_NOT_FOUND)
1298 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1299 else
1300 node->same_comdat_group = NULL;
1302 return nodes;
1305 /* Read a varpool from IB using the info in FILE_DATA. */
1307 static VEC(varpool_node_ptr, heap) *
1308 input_varpool_1 (struct lto_file_decl_data *file_data,
1309 struct lto_input_block *ib)
1311 unsigned HOST_WIDE_INT len;
1312 VEC(varpool_node_ptr, heap) *varpool = NULL;
1313 int i;
1314 struct varpool_node *node;
1316 len = lto_input_uleb128 (ib);
1317 while (len)
1319 VEC_safe_push (varpool_node_ptr, heap, varpool,
1320 input_varpool_node (file_data, ib));
1321 len--;
1323 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1325 int ref = (int) (intptr_t) node->same_comdat_group;
1327 /* Fixup same_comdat_group from reference to pointer. */
1328 if (ref != LCC_NOT_FOUND)
1329 node->same_comdat_group = VEC_index (varpool_node_ptr, varpool, ref);
1330 else
1331 node->same_comdat_group = NULL;
1333 return varpool;
1336 /* Input ipa_refs. */
1338 static void
1339 input_refs (struct lto_input_block *ib,
1340 VEC(cgraph_node_ptr, heap) *nodes,
1341 VEC(varpool_node_ptr, heap) *varpool)
1343 int count;
1344 int idx;
1345 while (true)
1347 struct cgraph_node *node;
1348 count = lto_input_uleb128 (ib);
1349 if (!count)
1350 break;
1351 idx = lto_input_uleb128 (ib);
1352 node = VEC_index (cgraph_node_ptr, nodes, idx);
1353 while (count)
1355 input_ref (ib, node, NULL, nodes, varpool);
1356 count--;
1359 while (true)
1361 struct varpool_node *node;
1362 count = lto_input_uleb128 (ib);
1363 if (!count)
1364 break;
1365 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1366 while (count)
1368 input_ref (ib, NULL, node, nodes, varpool);
1369 count--;
1375 static struct gcov_ctr_summary lto_gcov_summary;
1377 /* Input profile_info from IB. */
1378 static void
1379 input_profile_summary (struct lto_input_block *ib)
1381 unsigned int runs = lto_input_uleb128 (ib);
1382 if (runs)
1384 if (!profile_info)
1386 profile_info = &lto_gcov_summary;
1387 lto_gcov_summary.runs = runs;
1388 lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
1389 lto_gcov_summary.run_max = lto_input_sleb128 (ib);
1390 lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
1392 /* We can support this by scaling all counts to nearest common multiple
1393 of all different runs, but it is perhaps not worth the effort. */
1394 else if (profile_info->runs != runs
1395 || profile_info->sum_all != lto_input_sleb128 (ib)
1396 || profile_info->run_max != lto_input_sleb128 (ib)
1397 || profile_info->sum_max != lto_input_sleb128 (ib))
1398 sorry ("Combining units with different profiles is not supported.");
1399 /* We allow some units to have profile and other to not have one. This will
1400 just make unprofiled units to be size optimized that is sane. */
1405 /* Input and merge the cgraph from each of the .o files passed to
1406 lto1. */
1408 void
1409 input_cgraph (void)
1411 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1412 struct lto_file_decl_data *file_data;
1413 unsigned int j = 0;
1414 struct cgraph_node *node;
1416 while ((file_data = file_data_vec[j++]))
1418 const char *data;
1419 size_t len;
1420 struct lto_input_block *ib;
1421 VEC(cgraph_node_ptr, heap) *nodes;
1422 VEC(varpool_node_ptr, heap) *varpool;
1424 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1425 &data, &len);
1426 input_profile_summary (ib);
1427 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1428 nodes = input_cgraph_1 (file_data, ib);
1429 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1430 ib, data, len);
1432 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1433 &data, &len);
1434 varpool = input_varpool_1 (file_data, ib);
1435 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1436 ib, data, len);
1438 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1439 &data, &len);
1440 input_refs (ib, nodes, varpool);
1441 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1442 ib, data, len);
1443 if (flag_ltrans)
1444 input_cgraph_opt_summary (nodes);
1445 VEC_free (cgraph_node_ptr, heap, nodes);
1446 VEC_free (varpool_node_ptr, heap, varpool);
1449 /* Clear out the aux field that was used to store enough state to
1450 tell which nodes should be overwritten. */
1451 for (node = cgraph_nodes; node; node = node->next)
1453 /* Some nodes may have been created by cgraph_node. This
1454 happens when the callgraph contains nested functions. If the
1455 node for the parent function was never emitted to the gimple
1456 file, cgraph_node will create a node for it when setting the
1457 context of the nested function. */
1458 if (node->local.lto_file_data)
1459 node->aux = NULL;
1463 /* True when we need optimization summary for NODE. */
1465 static int
1466 output_cgraph_opt_summary_p (struct cgraph_node *node)
1468 if (!node->clone_of)
1469 return false;
1470 return (node->clone.tree_map
1471 || node->clone.args_to_skip
1472 || node->clone.combined_args_to_skip);
1475 /* Output optimization summary for NODE to OB. */
1477 static void
1478 output_node_opt_summary (struct output_block *ob,
1479 struct cgraph_node *node)
1481 unsigned int index;
1482 bitmap_iterator bi;
1483 struct ipa_replace_map *map;
1484 struct bitpack_d bp;
1485 int i;
1487 lto_output_uleb128_stream (ob->main_stream,
1488 bitmap_count_bits (node->clone.args_to_skip));
1489 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1490 lto_output_uleb128_stream (ob->main_stream, index);
1491 lto_output_uleb128_stream (ob->main_stream,
1492 bitmap_count_bits (node->clone.combined_args_to_skip));
1493 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1494 lto_output_uleb128_stream (ob->main_stream, index);
1495 lto_output_uleb128_stream (ob->main_stream,
1496 VEC_length (ipa_replace_map_p, node->clone.tree_map));
1497 FOR_EACH_VEC_ELT (ipa_replace_map_p, node->clone.tree_map, i, map)
1499 int parm_num;
1500 tree parm;
1502 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm;
1503 parm = DECL_CHAIN (parm), parm_num++)
1504 if (map->old_tree == parm)
1505 break;
1506 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1507 mechanism to store function local declarations into summaries. */
1508 gcc_assert (parm);
1509 lto_output_uleb128_stream (ob->main_stream, parm_num);
1510 lto_output_tree (ob, map->new_tree, true);
1511 bp = bitpack_create (ob->main_stream);
1512 bp_pack_value (&bp, map->replace_p, 1);
1513 bp_pack_value (&bp, map->ref_p, 1);
1514 lto_output_bitpack (&bp);
1518 /* Output optimization summaries stored in callgraph.
1519 At the moment it is the clone info structure. */
1521 static void
1522 output_cgraph_opt_summary (void)
1524 struct cgraph_node *node;
1525 int i, n_nodes;
1526 lto_cgraph_encoder_t encoder;
1527 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1528 unsigned count = 0;
1530 ob->cgraph_node = NULL;
1531 encoder = ob->decl_state->cgraph_node_encoder;
1532 n_nodes = lto_cgraph_encoder_size (encoder);
1533 for (i = 0; i < n_nodes; i++)
1534 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder, i)))
1535 count++;
1536 lto_output_uleb128_stream (ob->main_stream, count);
1537 for (i = 0; i < n_nodes; i++)
1539 node = lto_cgraph_encoder_deref (encoder, i);
1540 if (output_cgraph_opt_summary_p (node))
1542 lto_output_uleb128_stream (ob->main_stream, i);
1543 output_node_opt_summary (ob, node);
1546 produce_asm (ob, NULL);
1547 destroy_output_block (ob);
1550 /* Input optimiation summary of NODE. */
1552 static void
1553 input_node_opt_summary (struct cgraph_node *node,
1554 struct lto_input_block *ib_main,
1555 struct data_in *data_in)
1557 int i;
1558 int count;
1559 int bit;
1560 struct bitpack_d bp;
1562 count = lto_input_uleb128 (ib_main);
1563 if (count)
1564 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1565 for (i = 0; i < count; i++)
1567 bit = lto_input_uleb128 (ib_main);
1568 bitmap_set_bit (node->clone.args_to_skip, bit);
1570 count = lto_input_uleb128 (ib_main);
1571 if (count)
1572 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1573 for (i = 0; i < count; i++)
1575 bit = lto_input_uleb128 (ib_main);
1576 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1578 count = lto_input_uleb128 (ib_main);
1579 for (i = 0; i < count; i++)
1581 int parm_num;
1582 tree parm;
1583 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1585 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1586 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm_num;
1587 parm = DECL_CHAIN (parm))
1588 parm_num --;
1589 map->parm_num = lto_input_uleb128 (ib_main);
1590 map->old_tree = NULL;
1591 map->new_tree = lto_input_tree (ib_main, data_in);
1592 bp = lto_input_bitpack (ib_main);
1593 map->replace_p = bp_unpack_value (&bp, 1);
1594 map->ref_p = bp_unpack_value (&bp, 1);
1598 /* Read section in file FILE_DATA of length LEN with data DATA. */
1600 static void
1601 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1602 const char *data, size_t len, VEC (cgraph_node_ptr,
1603 heap) * nodes)
1605 const struct lto_function_header *header =
1606 (const struct lto_function_header *) data;
1607 const int32_t cfg_offset = sizeof (struct lto_function_header);
1608 const int32_t main_offset = cfg_offset + header->cfg_size;
1609 const int32_t string_offset = main_offset + header->main_size;
1610 struct data_in *data_in;
1611 struct lto_input_block ib_main;
1612 unsigned int i;
1613 unsigned int count;
1615 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1616 header->main_size);
1618 data_in =
1619 lto_data_in_create (file_data, (const char *) data + string_offset,
1620 header->string_size, NULL);
1621 count = lto_input_uleb128 (&ib_main);
1623 for (i = 0; i < count; i++)
1625 int ref = lto_input_uleb128 (&ib_main);
1626 input_node_opt_summary (VEC_index (cgraph_node_ptr, nodes, ref),
1627 &ib_main, data_in);
1629 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
1630 len);
1631 lto_data_in_delete (data_in);
1634 /* Input optimization summary of cgraph. */
1636 static void
1637 input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes)
1639 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1640 struct lto_file_decl_data *file_data;
1641 unsigned int j = 0;
1643 while ((file_data = file_data_vec[j++]))
1645 size_t len;
1646 const char *data =
1647 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1648 &len);
1650 if (data)
1651 input_cgraph_opt_section (file_data, data, len, nodes);