Fix to expose more LIM when creating mem_ref
[official-gcc.git] / gcc / lto-cgraph.c
blob9c7af2ba50731bbcbb7e86f0220debd42a16c301
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 = flag_wpa ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt);
285 bp_pack_value (&bp, uid, HOST_BITS_PER_INT);
286 bp_pack_value (&bp, edge->inline_failed, HOST_BITS_PER_INT);
287 bp_pack_value (&bp, edge->frequency, HOST_BITS_PER_INT);
288 bp_pack_value (&bp, edge->loop_nest, 30);
289 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
290 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
291 bp_pack_value (&bp, edge->can_throw_external, 1);
292 if (edge->indirect_unknown_callee)
294 int flags = edge->indirect_info->ecf_flags;
295 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
296 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
297 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
298 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
299 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
300 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
301 /* Flags that should not appear on indirect calls. */
302 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
303 | ECF_MAY_BE_ALLOCA
304 | ECF_SIBCALL
305 | ECF_NOVOPS)));
307 lto_output_bitpack (&bp);
310 /* Return if LIST contain references from other partitions. */
312 bool
313 referenced_from_other_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
314 varpool_node_set vset)
316 int i;
317 struct ipa_ref *ref;
318 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
320 if (ref->refering_type == IPA_REF_CGRAPH)
322 if (ipa_ref_refering_node (ref)->in_other_partition
323 || !cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
324 return true;
326 else
328 if (ipa_ref_refering_varpool_node (ref)->in_other_partition
329 || !varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
330 vset))
331 return true;
334 return false;
337 /* Return true when node is reachable from other partition. */
339 bool
340 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
342 struct cgraph_edge *e;
343 if (!node->analyzed)
344 return false;
345 if (node->global.inlined_to)
346 return false;
347 for (e = node->callers; e; e = e->next_caller)
348 if (e->caller->in_other_partition
349 || !cgraph_node_in_set_p (e->caller, set))
350 return true;
351 return false;
354 /* Return if LIST contain references from other partitions. */
356 bool
357 referenced_from_this_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
358 varpool_node_set vset)
360 int i;
361 struct ipa_ref *ref;
362 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
364 if (ref->refering_type == IPA_REF_CGRAPH)
366 if (cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
367 return true;
369 else
371 if (varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
372 vset))
373 return true;
376 return false;
379 /* Return true when node is reachable from other partition. */
381 bool
382 reachable_from_this_partition_p (struct cgraph_node *node, cgraph_node_set set)
384 struct cgraph_edge *e;
385 if (!node->analyzed)
386 return false;
387 if (node->global.inlined_to)
388 return false;
389 for (e = node->callers; e; e = e->next_caller)
390 if (cgraph_node_in_set_p (e->caller, set))
391 return true;
392 return false;
395 /* Output the cgraph NODE to OB. ENCODER is used to find the
396 reference number of NODE->inlined_to. SET is the set of nodes we
397 are writing to the current file. If NODE is not in SET, then NODE
398 is a boundary of a cgraph_node_set and we pretend NODE just has a
399 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
400 that have had their callgraph node written so far. This is used to
401 determine if NODE is a clone of a previously written node. */
403 static void
404 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
405 lto_cgraph_encoder_t encoder, cgraph_node_set set,
406 varpool_node_set vset)
408 unsigned int tag;
409 struct bitpack_d bp;
410 bool boundary_p;
411 intptr_t ref;
412 bool in_other_partition = false;
413 struct cgraph_node *clone_of;
415 boundary_p = !cgraph_node_in_set_p (node, set);
417 if (node->analyzed && !boundary_p)
418 tag = LTO_cgraph_analyzed_node;
419 else
420 tag = LTO_cgraph_unavail_node;
422 lto_output_uleb128_stream (ob->main_stream, tag);
424 /* In WPA mode, we only output part of the call-graph. Also, we
425 fake cgraph node attributes. There are two cases that we care.
427 Boundary nodes: There are nodes that are not part of SET but are
428 called from within SET. We artificially make them look like
429 externally visible nodes with no function body.
431 Cherry-picked nodes: These are nodes we pulled from other
432 translation units into SET during IPA-inlining. We make them as
433 local static nodes to prevent clashes with other local statics. */
434 if (boundary_p && node->analyzed)
436 /* Inline clones can not be part of boundary.
437 gcc_assert (!node->global.inlined_to);
439 FIXME: At the moment they can be, when partition contains an inline
440 clone that is clone of inline clone from outside partition. We can
441 reshape the clone tree and make other tree to be the root, but it
442 needs a bit extra work and will be promplty done by cgraph_remove_node
443 after reading back. */
444 in_other_partition = 1;
447 clone_of = node->clone_of;
448 while (clone_of
449 && (ref = lto_cgraph_encoder_lookup (encoder, node->clone_of)) == LCC_NOT_FOUND)
450 if (clone_of->prev_sibling_clone)
451 clone_of = clone_of->prev_sibling_clone;
452 else
453 clone_of = clone_of->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);
463 if (tag == LTO_cgraph_analyzed_node)
465 lto_output_sleb128_stream (ob->main_stream,
466 node->local.inline_summary.estimated_self_stack_size);
467 lto_output_sleb128_stream (ob->main_stream,
468 node->local.inline_summary.self_size);
469 lto_output_sleb128_stream (ob->main_stream,
470 node->local.inline_summary.size_inlining_benefit);
471 lto_output_sleb128_stream (ob->main_stream,
472 node->local.inline_summary.self_time);
473 lto_output_sleb128_stream (ob->main_stream,
474 node->local.inline_summary.time_inlining_benefit);
475 if (node->global.inlined_to)
477 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
478 gcc_assert (ref != LCC_NOT_FOUND);
480 else
481 ref = LCC_NOT_FOUND;
483 lto_output_sleb128_stream (ob->main_stream, ref);
486 if (node->same_comdat_group && !boundary_p)
488 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
489 gcc_assert (ref != LCC_NOT_FOUND);
491 else
492 ref = LCC_NOT_FOUND;
493 lto_output_sleb128_stream (ob->main_stream, ref);
495 bp = bitpack_create (ob->main_stream);
496 bp_pack_value (&bp, node->local.local, 1);
497 bp_pack_value (&bp, node->local.externally_visible, 1);
498 bp_pack_value (&bp, node->local.finalized, 1);
499 bp_pack_value (&bp, node->local.inlinable, 1);
500 bp_pack_value (&bp, node->local.versionable, 1);
501 bp_pack_value (&bp, node->local.disregard_inline_limits, 1);
502 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
503 bp_pack_value (&bp, node->local.vtable_method, 1);
504 bp_pack_value (&bp, node->needed, 1);
505 bp_pack_value (&bp, node->address_taken, 1);
506 bp_pack_value (&bp, node->abstract_and_needed, 1);
507 bp_pack_value (&bp, tag == LTO_cgraph_analyzed_node
508 && !DECL_EXTERNAL (node->decl)
509 && !DECL_COMDAT (node->decl)
510 && (reachable_from_other_partition_p (node, set)
511 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
512 bp_pack_value (&bp, node->lowered, 1);
513 bp_pack_value (&bp, in_other_partition, 1);
514 bp_pack_value (&bp, node->alias, 1);
515 bp_pack_value (&bp, node->finalized_by_frontend, 1);
516 bp_pack_value (&bp, node->frequency, 2);
517 lto_output_bitpack (&bp);
519 if (node->same_body)
521 struct cgraph_node *alias;
522 unsigned long alias_count = 1;
523 for (alias = node->same_body; alias->next; alias = alias->next)
524 alias_count++;
525 lto_output_uleb128_stream (ob->main_stream, alias_count);
528 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
529 alias->decl);
530 if (alias->thunk.thunk_p)
532 lto_output_uleb128_stream
533 (ob->main_stream,
534 1 + (alias->thunk.this_adjusting != 0) * 2
535 + (alias->thunk.virtual_offset_p != 0) * 4);
536 lto_output_uleb128_stream (ob->main_stream,
537 alias->thunk.fixed_offset);
538 lto_output_uleb128_stream (ob->main_stream,
539 alias->thunk.virtual_value);
540 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
541 alias->thunk.alias);
543 else
545 lto_output_uleb128_stream (ob->main_stream, 0);
546 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
547 alias->thunk.alias);
549 alias = alias->previous;
551 while (alias);
553 else
554 lto_output_uleb128_stream (ob->main_stream, 0);
557 /* Output the varpool NODE to OB.
558 If NODE is not in SET, then NODE is a boundary. */
560 static void
561 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
562 lto_varpool_encoder_t varpool_encoder,
563 cgraph_node_set set, varpool_node_set vset)
565 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
566 struct bitpack_d bp;
567 struct varpool_node *alias;
568 int count = 0;
569 int ref;
571 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
572 bp = bitpack_create (ob->main_stream);
573 bp_pack_value (&bp, node->externally_visible, 1);
574 bp_pack_value (&bp, node->force_output, 1);
575 bp_pack_value (&bp, node->finalized, 1);
576 bp_pack_value (&bp, node->alias, 1);
577 gcc_assert (!node->alias || !node->extra_name);
578 gcc_assert (node->finalized || !node->analyzed);
579 gcc_assert (node->needed);
580 /* Constant pool initializers can be de-unified into individual ltrans units.
581 FIXME: Alternatively at -Os we may want to avoid generating for them the local
582 labels and share them across LTRANS partitions. */
583 if (DECL_IN_CONSTANT_POOL (node->decl)
584 && !DECL_COMDAT (node->decl))
586 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
587 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
589 else
591 bp_pack_value (&bp, node->analyzed
592 && referenced_from_other_partition_p (&node->ref_list,
593 set, vset), 1);
594 bp_pack_value (&bp, boundary_p, 1); /* in_other_partition. */
596 /* Also emit any extra name aliases. */
597 for (alias = node->extra_name; alias; alias = alias->next)
598 count++;
599 bp_pack_value (&bp, count != 0, 1);
600 lto_output_bitpack (&bp);
601 if (node->same_comdat_group && !boundary_p)
603 ref = lto_varpool_encoder_lookup (varpool_encoder, node->same_comdat_group);
604 gcc_assert (ref != LCC_NOT_FOUND);
606 else
607 ref = LCC_NOT_FOUND;
608 lto_output_sleb128_stream (ob->main_stream, ref);
610 if (count)
612 lto_output_uleb128_stream (ob->main_stream, count);
613 for (alias = node->extra_name; alias; alias = alias->next)
614 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
618 /* Output the varpool NODE to OB.
619 If NODE is not in SET, then NODE is a boundary. */
621 static void
622 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
623 lto_cgraph_encoder_t encoder,
624 lto_varpool_encoder_t varpool_encoder)
626 struct bitpack_d bp;
627 bp = bitpack_create (ob->main_stream);
628 bp_pack_value (&bp, ref->refered_type, 1);
629 bp_pack_value (&bp, ref->use, 2);
630 lto_output_bitpack (&bp);
631 if (ref->refered_type == IPA_REF_CGRAPH)
633 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
634 gcc_assert (nref != LCC_NOT_FOUND);
635 lto_output_sleb128_stream (ob->main_stream, nref);
637 else
639 int nref = lto_varpool_encoder_lookup (varpool_encoder,
640 ipa_ref_varpool_node (ref));
641 gcc_assert (nref != LCC_NOT_FOUND);
642 lto_output_sleb128_stream (ob->main_stream, nref);
646 /* Stream out profile_summary to OB. */
648 static void
649 output_profile_summary (struct lto_simple_output_block *ob)
651 if (profile_info)
653 /* We do not output num, it is not terribly useful. */
654 gcc_assert (profile_info->runs);
655 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
656 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
657 lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
658 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
660 else
661 lto_output_uleb128_stream (ob->main_stream, 0);
664 /* Add NODE into encoder as well as nodes it is cloned from.
665 Do it in a way so clones appear first. */
667 static void
668 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node,
669 bool include_body)
671 if (node->clone_of)
672 add_node_to (encoder, node->clone_of, include_body);
673 else if (include_body)
674 lto_set_cgraph_encoder_encode_body (encoder, node);
675 lto_cgraph_encoder_encode (encoder, node);
678 /* Add all references in LIST to encoders. */
680 static void
681 add_references (lto_cgraph_encoder_t encoder,
682 lto_varpool_encoder_t varpool_encoder,
683 struct ipa_ref_list *list)
685 int i;
686 struct ipa_ref *ref;
687 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
688 if (ref->refered_type == IPA_REF_CGRAPH)
689 add_node_to (encoder, ipa_ref_node (ref), false);
690 else
692 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
693 lto_varpool_encoder_encode (varpool_encoder, vnode);
697 /* Output all callees or indirect outgoing edges. EDGE must be the first such
698 edge. */
700 static void
701 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
702 struct lto_simple_output_block *ob,
703 lto_cgraph_encoder_t encoder)
705 if (!edge)
706 return;
708 /* Output edges in backward direction, so the reconstructed callgraph match
709 and it is easy to associate call sites in the IPA pass summaries. */
710 while (edge->next_callee)
711 edge = edge->next_callee;
712 for (; edge; edge = edge->prev_callee)
713 lto_output_edge (ob, edge, encoder);
716 /* Output the part of the cgraph in SET. */
718 static void
719 output_refs (cgraph_node_set set, varpool_node_set vset,
720 lto_cgraph_encoder_t encoder,
721 lto_varpool_encoder_t varpool_encoder)
723 cgraph_node_set_iterator csi;
724 varpool_node_set_iterator vsi;
725 struct lto_simple_output_block *ob;
726 int count;
727 struct ipa_ref *ref;
728 int i;
730 ob = lto_create_simple_output_block (LTO_section_refs);
732 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
734 struct cgraph_node *node = csi_node (csi);
736 count = ipa_ref_list_nreferences (&node->ref_list);
737 if (count)
739 lto_output_uleb128_stream (ob->main_stream, count);
740 lto_output_uleb128_stream (ob->main_stream,
741 lto_cgraph_encoder_lookup (encoder, node));
742 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
743 lto_output_ref (ob, ref, encoder, varpool_encoder);
747 lto_output_uleb128_stream (ob->main_stream, 0);
749 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
751 struct varpool_node *node = vsi_node (vsi);
753 count = ipa_ref_list_nreferences (&node->ref_list);
754 if (count)
756 lto_output_uleb128_stream (ob->main_stream, count);
757 lto_output_uleb128_stream (ob->main_stream,
758 lto_varpool_encoder_lookup (varpool_encoder,
759 node));
760 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
761 lto_output_ref (ob, ref, encoder, varpool_encoder);
765 lto_output_uleb128_stream (ob->main_stream, 0);
767 lto_destroy_simple_output_block (ob);
770 /* Find out all cgraph and varpool nodes we want to encode in current unit
771 and insert them to encoders. */
772 void
773 compute_ltrans_boundary (struct lto_out_decl_state *state,
774 cgraph_node_set set, varpool_node_set vset)
776 struct cgraph_node *node;
777 cgraph_node_set_iterator csi;
778 varpool_node_set_iterator vsi;
779 struct cgraph_edge *edge;
780 int i;
781 lto_cgraph_encoder_t encoder;
782 lto_varpool_encoder_t varpool_encoder;
784 encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
785 varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
787 /* Go over all the nodes in SET and assign references. */
788 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
790 node = csi_node (csi);
791 add_node_to (encoder, node, true);
792 add_references (encoder, varpool_encoder, &node->ref_list);
794 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
796 struct varpool_node *vnode = vsi_node (vsi);
797 gcc_assert (!vnode->alias);
798 lto_varpool_encoder_encode (varpool_encoder, vnode);
799 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
800 add_references (encoder, varpool_encoder, &vnode->ref_list);
802 /* Pickle in also the initializer of all referenced readonly variables
803 to help folding. Constant pool variables are not shared, so we must
804 pickle those too. */
805 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
807 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
808 if (DECL_INITIAL (vnode->decl)
809 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
810 vnode)
811 && (DECL_IN_CONSTANT_POOL (vnode->decl)
812 || TREE_READONLY (vnode->decl)))
814 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
815 add_references (encoder, varpool_encoder, &vnode->ref_list);
819 /* Go over all the nodes again to include callees that are not in
820 SET. */
821 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
823 node = csi_node (csi);
824 for (edge = node->callees; edge; edge = edge->next_callee)
826 struct cgraph_node *callee = edge->callee;
827 if (!cgraph_node_in_set_p (callee, set))
829 /* We should have moved all the inlines. */
830 gcc_assert (!callee->global.inlined_to);
831 add_node_to (encoder, callee, false);
837 /* Output the part of the cgraph in SET. */
839 void
840 output_cgraph (cgraph_node_set set, varpool_node_set vset)
842 struct cgraph_node *node;
843 struct lto_simple_output_block *ob;
844 cgraph_node_set_iterator csi;
845 int i, n_nodes;
846 lto_cgraph_encoder_t encoder;
847 lto_varpool_encoder_t varpool_encoder;
848 struct cgraph_asm_node *can;
849 static bool asm_nodes_output = false;
851 if (flag_wpa)
852 output_cgraph_opt_summary ();
854 ob = lto_create_simple_output_block (LTO_section_cgraph);
856 output_profile_summary (ob);
858 /* An encoder for cgraph nodes should have been created by
859 ipa_write_summaries_1. */
860 gcc_assert (ob->decl_state->cgraph_node_encoder);
861 gcc_assert (ob->decl_state->varpool_node_encoder);
862 encoder = ob->decl_state->cgraph_node_encoder;
863 varpool_encoder = ob->decl_state->varpool_node_encoder;
865 /* Write out the nodes. We must first output a node and then its clones,
866 otherwise at a time reading back the node there would be nothing to clone
867 from. */
868 n_nodes = lto_cgraph_encoder_size (encoder);
869 for (i = 0; i < n_nodes; i++)
871 node = lto_cgraph_encoder_deref (encoder, i);
872 lto_output_node (ob, node, encoder, set, vset);
875 /* Go over the nodes in SET again to write edges. */
876 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
878 node = csi_node (csi);
879 output_outgoing_cgraph_edges (node->callees, ob, encoder);
880 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
883 lto_output_uleb128_stream (ob->main_stream, 0);
885 /* Emit toplevel asms.
886 When doing WPA we must output every asm just once. Since we do not partition asm
887 nodes at all, output them to first output. This is kind of hack, but should work
888 well. */
889 if (!asm_nodes_output)
891 asm_nodes_output = true;
892 for (can = cgraph_asm_nodes; can; can = can->next)
894 int len = TREE_STRING_LENGTH (can->asm_str);
895 lto_output_uleb128_stream (ob->main_stream, len);
896 for (i = 0; i < len; ++i)
897 lto_output_1_stream (ob->main_stream,
898 TREE_STRING_POINTER (can->asm_str)[i]);
902 lto_output_uleb128_stream (ob->main_stream, 0);
904 lto_destroy_simple_output_block (ob);
905 output_varpool (set, vset);
906 output_refs (set, vset, encoder, varpool_encoder);
909 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
910 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
911 NODE or to replace the values in it, for instance because the first
912 time we saw it, the function body was not available but now it
913 is. BP is a bitpack with all the bitflags for NODE read from the
914 stream. */
916 static void
917 input_overwrite_node (struct lto_file_decl_data *file_data,
918 struct cgraph_node *node,
919 enum LTO_cgraph_tags tag,
920 struct bitpack_d *bp,
921 unsigned int stack_size,
922 unsigned int self_time,
923 unsigned int time_inlining_benefit,
924 unsigned int self_size,
925 unsigned int size_inlining_benefit)
927 node->aux = (void *) tag;
928 node->local.inline_summary.estimated_self_stack_size = stack_size;
929 node->local.inline_summary.self_time = self_time;
930 node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
931 node->local.inline_summary.self_size = self_size;
932 node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
933 node->global.time = self_time;
934 node->global.size = self_size;
935 node->global.estimated_stack_size = stack_size;
936 node->global.estimated_growth = INT_MIN;
937 node->local.lto_file_data = file_data;
939 node->local.local = bp_unpack_value (bp, 1);
940 node->local.externally_visible = bp_unpack_value (bp, 1);
941 node->local.finalized = bp_unpack_value (bp, 1);
942 node->local.inlinable = bp_unpack_value (bp, 1);
943 node->local.versionable = bp_unpack_value (bp, 1);
944 node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
945 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
946 node->local.vtable_method = bp_unpack_value (bp, 1);
947 node->needed = bp_unpack_value (bp, 1);
948 node->address_taken = bp_unpack_value (bp, 1);
949 node->abstract_and_needed = bp_unpack_value (bp, 1);
950 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
951 node->lowered = bp_unpack_value (bp, 1);
952 node->analyzed = tag == LTO_cgraph_analyzed_node;
953 node->in_other_partition = bp_unpack_value (bp, 1);
954 node->alias = bp_unpack_value (bp, 1);
955 node->finalized_by_frontend = bp_unpack_value (bp, 1);
956 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
959 /* Output the part of the cgraph in SET. */
961 static void
962 output_varpool (cgraph_node_set set, varpool_node_set vset)
964 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
965 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
966 int len = lto_varpool_encoder_size (varpool_encoder), i;
968 lto_output_uleb128_stream (ob->main_stream, len);
970 /* Write out the nodes. We must first output a node and then its clones,
971 otherwise at a time reading back the node there would be nothing to clone
972 from. */
973 for (i = 0; i < len; i++)
975 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
976 varpool_encoder,
977 set, vset);
980 lto_destroy_simple_output_block (ob);
983 /* Read a node from input_block IB. TAG is the node's tag just read.
984 Return the node read or overwriten. */
986 static struct cgraph_node *
987 input_node (struct lto_file_decl_data *file_data,
988 struct lto_input_block *ib,
989 enum LTO_cgraph_tags tag,
990 VEC(cgraph_node_ptr, heap) *nodes)
992 tree fn_decl;
993 struct cgraph_node *node;
994 struct bitpack_d bp;
995 int stack_size = 0;
996 unsigned decl_index;
997 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
998 int self_time = 0;
999 int self_size = 0;
1000 int time_inlining_benefit = 0;
1001 int size_inlining_benefit = 0;
1002 unsigned long same_body_count = 0;
1003 int clone_ref;
1005 clone_ref = lto_input_sleb128 (ib);
1007 decl_index = lto_input_uleb128 (ib);
1008 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1010 if (clone_ref != LCC_NOT_FOUND)
1012 node = cgraph_clone_node (VEC_index (cgraph_node_ptr, nodes, clone_ref), fn_decl,
1013 0, CGRAPH_FREQ_BASE, 0, false, NULL);
1015 else
1016 node = cgraph_node (fn_decl);
1018 node->count = lto_input_sleb128 (ib);
1020 if (tag == LTO_cgraph_analyzed_node)
1022 stack_size = lto_input_sleb128 (ib);
1023 self_size = lto_input_sleb128 (ib);
1024 size_inlining_benefit = lto_input_sleb128 (ib);
1025 self_time = lto_input_sleb128 (ib);
1026 time_inlining_benefit = lto_input_sleb128 (ib);
1028 ref = lto_input_sleb128 (ib);
1031 ref2 = lto_input_sleb128 (ib);
1033 /* Make sure that we have not read this node before. Nodes that
1034 have already been read will have their tag stored in the 'aux'
1035 field. Since built-in functions can be referenced in multiple
1036 functions, they are expected to be read more than once. */
1037 if (node->aux && !DECL_IS_BUILTIN (node->decl))
1038 internal_error ("bytecode stream: found multiple instances of cgraph "
1039 "node %d", node->uid);
1041 bp = lto_input_bitpack (ib);
1042 input_overwrite_node (file_data, node, tag, &bp, stack_size, self_time,
1043 time_inlining_benefit, self_size,
1044 size_inlining_benefit);
1046 /* Store a reference for now, and fix up later to be a pointer. */
1047 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1049 /* Store a reference for now, and fix up later to be a pointer. */
1050 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1052 same_body_count = lto_input_uleb128 (ib);
1053 while (same_body_count-- > 0)
1055 tree alias_decl;
1056 int type;
1057 decl_index = lto_input_uleb128 (ib);
1058 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1059 type = lto_input_uleb128 (ib);
1060 if (!type)
1062 tree real_alias;
1063 decl_index = lto_input_uleb128 (ib);
1064 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1065 cgraph_same_body_alias (alias_decl, real_alias);
1067 else
1069 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1070 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1071 tree real_alias;
1072 decl_index = lto_input_uleb128 (ib);
1073 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1074 cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
1075 virtual_value,
1076 (type & 4) ? size_int (virtual_value) : NULL_TREE,
1077 real_alias);
1080 return node;
1083 /* Read a node from input_block IB. TAG is the node's tag just read.
1084 Return the node read or overwriten. */
1086 static struct varpool_node *
1087 input_varpool_node (struct lto_file_decl_data *file_data,
1088 struct lto_input_block *ib)
1090 int decl_index;
1091 tree var_decl;
1092 struct varpool_node *node;
1093 struct bitpack_d bp;
1094 bool aliases_p;
1095 int count;
1096 int ref = LCC_NOT_FOUND;
1098 decl_index = lto_input_uleb128 (ib);
1099 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1100 node = varpool_node (var_decl);
1101 node->lto_file_data = file_data;
1103 bp = lto_input_bitpack (ib);
1104 node->externally_visible = bp_unpack_value (&bp, 1);
1105 node->force_output = bp_unpack_value (&bp, 1);
1106 node->finalized = bp_unpack_value (&bp, 1);
1107 node->alias = bp_unpack_value (&bp, 1);
1108 node->analyzed = node->finalized;
1109 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1110 node->in_other_partition = bp_unpack_value (&bp, 1);
1111 aliases_p = bp_unpack_value (&bp, 1);
1112 if (node->finalized)
1113 varpool_mark_needed_node (node);
1114 ref = lto_input_sleb128 (ib);
1115 /* Store a reference for now, and fix up later to be a pointer. */
1116 node->same_comdat_group = (struct varpool_node *) (intptr_t) ref;
1117 if (aliases_p)
1119 count = lto_input_uleb128 (ib);
1120 for (; count > 0; count --)
1122 tree decl = lto_file_decl_data_get_var_decl (file_data,
1123 lto_input_uleb128 (ib));
1124 varpool_extra_name_alias (decl, var_decl);
1127 return node;
1130 /* Read a node from input_block IB. TAG is the node's tag just read.
1131 Return the node read or overwriten. */
1133 static void
1134 input_ref (struct lto_input_block *ib,
1135 struct cgraph_node *refering_node,
1136 struct varpool_node *refering_varpool_node,
1137 VEC(cgraph_node_ptr, heap) *nodes,
1138 VEC(varpool_node_ptr, heap) *varpool_nodes)
1140 struct cgraph_node *node = NULL;
1141 struct varpool_node *varpool_node = NULL;
1142 struct bitpack_d bp;
1143 enum ipa_ref_type type;
1144 enum ipa_ref_use use;
1146 bp = lto_input_bitpack (ib);
1147 type = (enum ipa_ref_type) bp_unpack_value (&bp, 1);
1148 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1149 if (type == IPA_REF_CGRAPH)
1150 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1151 else
1152 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1153 ipa_record_reference (refering_node, refering_varpool_node,
1154 node, varpool_node, use, NULL);
1157 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1158 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1159 edge being read is indirect (in the sense that it has
1160 indirect_unknown_callee set). */
1162 static void
1163 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1164 bool indirect)
1166 struct cgraph_node *caller, *callee;
1167 struct cgraph_edge *edge;
1168 unsigned int stmt_id;
1169 gcov_type count;
1170 int freq;
1171 unsigned int nest;
1172 cgraph_inline_failed_t inline_failed;
1173 struct bitpack_d bp;
1174 int ecf_flags = 0;
1176 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1177 if (caller == NULL || caller->decl == NULL_TREE)
1178 internal_error ("bytecode stream: no caller found while reading edge");
1180 if (!indirect)
1182 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1183 if (callee == NULL || callee->decl == NULL_TREE)
1184 internal_error ("bytecode stream: no callee found while reading edge");
1186 else
1187 callee = NULL;
1189 count = (gcov_type) lto_input_sleb128 (ib);
1191 bp = lto_input_bitpack (ib);
1192 stmt_id = (unsigned int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1193 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (&bp,
1194 HOST_BITS_PER_INT);
1195 freq = (int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1196 nest = (unsigned) bp_unpack_value (&bp, 30);
1198 if (indirect)
1199 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq, nest);
1200 else
1201 edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
1203 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1204 edge->lto_stmt_uid = stmt_id;
1205 edge->inline_failed = inline_failed;
1206 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1207 edge->can_throw_external = bp_unpack_value (&bp, 1);
1208 if (indirect)
1210 if (bp_unpack_value (&bp, 1))
1211 ecf_flags |= ECF_CONST;
1212 if (bp_unpack_value (&bp, 1))
1213 ecf_flags |= ECF_PURE;
1214 if (bp_unpack_value (&bp, 1))
1215 ecf_flags |= ECF_NORETURN;
1216 if (bp_unpack_value (&bp, 1))
1217 ecf_flags |= ECF_MALLOC;
1218 if (bp_unpack_value (&bp, 1))
1219 ecf_flags |= ECF_NOTHROW;
1220 if (bp_unpack_value (&bp, 1))
1221 ecf_flags |= ECF_RETURNS_TWICE;
1222 edge->indirect_info->ecf_flags = ecf_flags;
1227 /* Read a cgraph from IB using the info in FILE_DATA. */
1229 static VEC(cgraph_node_ptr, heap) *
1230 input_cgraph_1 (struct lto_file_decl_data *file_data,
1231 struct lto_input_block *ib)
1233 enum LTO_cgraph_tags tag;
1234 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1235 struct cgraph_node *node;
1236 unsigned i;
1237 unsigned HOST_WIDE_INT len;
1239 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1240 while (tag)
1242 if (tag == LTO_cgraph_edge)
1243 input_edge (ib, nodes, false);
1244 else if (tag == LTO_cgraph_indirect_edge)
1245 input_edge (ib, nodes, true);
1246 else
1248 node = input_node (file_data, ib, tag,nodes);
1249 if (node == NULL || node->decl == NULL_TREE)
1250 internal_error ("bytecode stream: found empty cgraph node");
1251 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1252 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1255 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1258 /* Input toplevel asms. */
1259 len = lto_input_uleb128 (ib);
1260 while (len)
1262 char *str = (char *)xmalloc (len + 1);
1263 for (i = 0; i < len; ++i)
1264 str[i] = lto_input_1_unsigned (ib);
1265 cgraph_add_asm_node (build_string (len, str));
1266 free (str);
1268 len = lto_input_uleb128 (ib);
1271 for (i = 0; VEC_iterate (cgraph_node_ptr, nodes, i, node); i++)
1273 int ref = (int) (intptr_t) node->global.inlined_to;
1275 /* Fixup inlined_to from reference to pointer. */
1276 if (ref != LCC_NOT_FOUND)
1277 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1278 else
1279 node->global.inlined_to = NULL;
1281 ref = (int) (intptr_t) node->same_comdat_group;
1283 /* Fixup same_comdat_group from reference to pointer. */
1284 if (ref != LCC_NOT_FOUND)
1285 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1286 else
1287 node->same_comdat_group = NULL;
1289 return nodes;
1292 /* Read a varpool from IB using the info in FILE_DATA. */
1294 static VEC(varpool_node_ptr, heap) *
1295 input_varpool_1 (struct lto_file_decl_data *file_data,
1296 struct lto_input_block *ib)
1298 unsigned HOST_WIDE_INT len;
1299 VEC(varpool_node_ptr, heap) *varpool = NULL;
1300 int i;
1301 struct varpool_node *node;
1303 len = lto_input_uleb128 (ib);
1304 while (len)
1306 VEC_safe_push (varpool_node_ptr, heap, varpool,
1307 input_varpool_node (file_data, ib));
1308 len--;
1310 for (i = 0; VEC_iterate (varpool_node_ptr, varpool, i, node); i++)
1312 int ref = (int) (intptr_t) node->same_comdat_group;
1314 /* Fixup same_comdat_group from reference to pointer. */
1315 if (ref != LCC_NOT_FOUND)
1316 node->same_comdat_group = VEC_index (varpool_node_ptr, varpool, ref);
1317 else
1318 node->same_comdat_group = NULL;
1320 return varpool;
1323 /* Input ipa_refs. */
1325 static void
1326 input_refs (struct lto_input_block *ib,
1327 VEC(cgraph_node_ptr, heap) *nodes,
1328 VEC(varpool_node_ptr, heap) *varpool)
1330 int count;
1331 int idx;
1332 while (true)
1334 struct cgraph_node *node;
1335 count = lto_input_uleb128 (ib);
1336 if (!count)
1337 break;
1338 idx = lto_input_uleb128 (ib);
1339 node = VEC_index (cgraph_node_ptr, nodes, idx);
1340 while (count)
1342 input_ref (ib, node, NULL, nodes, varpool);
1343 count--;
1346 while (true)
1348 struct varpool_node *node;
1349 count = lto_input_uleb128 (ib);
1350 if (!count)
1351 break;
1352 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1353 while (count)
1355 input_ref (ib, NULL, node, nodes, varpool);
1356 count--;
1362 static struct gcov_ctr_summary lto_gcov_summary;
1364 /* Input profile_info from IB. */
1365 static void
1366 input_profile_summary (struct lto_input_block *ib)
1368 unsigned int runs = lto_input_uleb128 (ib);
1369 if (runs)
1371 if (!profile_info)
1373 profile_info = &lto_gcov_summary;
1374 lto_gcov_summary.runs = runs;
1375 lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
1376 lto_gcov_summary.run_max = lto_input_sleb128 (ib);
1377 lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
1379 /* We can support this by scaling all counts to nearest common multiple
1380 of all different runs, but it is perhaps not worth the effort. */
1381 else if (profile_info->runs != runs
1382 || profile_info->sum_all != lto_input_sleb128 (ib)
1383 || profile_info->run_max != lto_input_sleb128 (ib)
1384 || profile_info->sum_max != lto_input_sleb128 (ib))
1385 sorry ("Combining units with different profiles is not supported.");
1386 /* We allow some units to have profile and other to not have one. This will
1387 just make unprofiled units to be size optimized that is sane. */
1392 /* Input and merge the cgraph from each of the .o files passed to
1393 lto1. */
1395 void
1396 input_cgraph (void)
1398 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1399 struct lto_file_decl_data *file_data;
1400 unsigned int j = 0;
1401 struct cgraph_node *node;
1403 while ((file_data = file_data_vec[j++]))
1405 const char *data;
1406 size_t len;
1407 struct lto_input_block *ib;
1408 VEC(cgraph_node_ptr, heap) *nodes;
1409 VEC(varpool_node_ptr, heap) *varpool;
1411 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1412 &data, &len);
1413 input_profile_summary (ib);
1414 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1415 nodes = input_cgraph_1 (file_data, ib);
1416 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1417 ib, data, len);
1419 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1420 &data, &len);
1421 varpool = input_varpool_1 (file_data, ib);
1422 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1423 ib, data, len);
1425 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1426 &data, &len);
1427 input_refs (ib, nodes, varpool);
1428 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1429 ib, data, len);
1430 if (flag_ltrans)
1431 input_cgraph_opt_summary (nodes);
1432 VEC_free (cgraph_node_ptr, heap, nodes);
1433 VEC_free (varpool_node_ptr, heap, varpool);
1436 /* Clear out the aux field that was used to store enough state to
1437 tell which nodes should be overwritten. */
1438 for (node = cgraph_nodes; node; node = node->next)
1440 /* Some nodes may have been created by cgraph_node. This
1441 happens when the callgraph contains nested functions. If the
1442 node for the parent function was never emitted to the gimple
1443 file, cgraph_node will create a node for it when setting the
1444 context of the nested function. */
1445 if (node->local.lto_file_data)
1446 node->aux = NULL;
1450 /* True when we need optimization summary for NODE. */
1452 static int
1453 output_cgraph_opt_summary_p (struct cgraph_node *node)
1455 if (!node->clone_of)
1456 return false;
1457 return (node->clone.tree_map
1458 || node->clone.args_to_skip
1459 || node->clone.combined_args_to_skip);
1462 /* Output optimization summary for NODE to OB. */
1464 static void
1465 output_node_opt_summary (struct output_block *ob,
1466 struct cgraph_node *node)
1468 unsigned int index;
1469 bitmap_iterator bi;
1470 struct ipa_replace_map *map;
1471 struct bitpack_d bp;
1472 int i;
1474 lto_output_uleb128_stream (ob->main_stream,
1475 bitmap_count_bits (node->clone.args_to_skip));
1476 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1477 lto_output_uleb128_stream (ob->main_stream, index);
1478 lto_output_uleb128_stream (ob->main_stream,
1479 bitmap_count_bits (node->clone.combined_args_to_skip));
1480 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1481 lto_output_uleb128_stream (ob->main_stream, index);
1482 lto_output_uleb128_stream (ob->main_stream,
1483 VEC_length (ipa_replace_map_p, node->clone.tree_map));
1484 for (i = 0; VEC_iterate (ipa_replace_map_p, node->clone.tree_map, i, map); i++)
1486 int parm_num;
1487 tree parm;
1489 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm;
1490 parm = DECL_CHAIN (parm), parm_num++)
1491 if (map->old_tree == parm)
1492 break;
1493 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1494 mechanism to store function local declarations into summaries. */
1495 gcc_assert (parm);
1496 lto_output_uleb128_stream (ob->main_stream, parm_num);
1497 lto_output_tree (ob, map->new_tree, true);
1498 bp = bitpack_create (ob->main_stream);
1499 bp_pack_value (&bp, map->replace_p, 1);
1500 bp_pack_value (&bp, map->ref_p, 1);
1501 lto_output_bitpack (&bp);
1505 /* Output optimization summaries stored in callgraph.
1506 At the moment it is the clone info structure. */
1508 static void
1509 output_cgraph_opt_summary (void)
1511 struct cgraph_node *node;
1512 int i, n_nodes;
1513 lto_cgraph_encoder_t encoder;
1514 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1515 unsigned count = 0;
1517 ob->cgraph_node = NULL;
1518 encoder = ob->decl_state->cgraph_node_encoder;
1519 n_nodes = lto_cgraph_encoder_size (encoder);
1520 for (i = 0; i < n_nodes; i++)
1521 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder, i)))
1522 count++;
1523 lto_output_uleb128_stream (ob->main_stream, count);
1524 for (i = 0; i < n_nodes; i++)
1526 node = lto_cgraph_encoder_deref (encoder, i);
1527 if (output_cgraph_opt_summary_p (node))
1529 lto_output_uleb128_stream (ob->main_stream, i);
1530 output_node_opt_summary (ob, node);
1533 produce_asm (ob, NULL);
1534 destroy_output_block (ob);
1537 /* Input optimiation summary of NODE. */
1539 static void
1540 input_node_opt_summary (struct cgraph_node *node,
1541 struct lto_input_block *ib_main,
1542 struct data_in *data_in)
1544 int i;
1545 int count;
1546 int bit;
1547 struct bitpack_d bp;
1549 count = lto_input_uleb128 (ib_main);
1550 if (count)
1551 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1552 for (i = 0; i < count; i++)
1554 bit = lto_input_uleb128 (ib_main);
1555 bitmap_set_bit (node->clone.args_to_skip, bit);
1557 count = lto_input_uleb128 (ib_main);
1558 if (count)
1559 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1560 for (i = 0; i < count; i++)
1562 bit = lto_input_uleb128 (ib_main);
1563 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1565 count = lto_input_uleb128 (ib_main);
1566 for (i = 0; i < count; i++)
1568 int parm_num;
1569 tree parm;
1570 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1572 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1573 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm_num;
1574 parm = DECL_CHAIN (parm))
1575 parm_num --;
1576 map->parm_num = lto_input_uleb128 (ib_main);
1577 map->old_tree = NULL;
1578 map->new_tree = lto_input_tree (ib_main, data_in);
1579 bp = lto_input_bitpack (ib_main);
1580 map->replace_p = bp_unpack_value (&bp, 1);
1581 map->ref_p = bp_unpack_value (&bp, 1);
1585 /* Read section in file FILE_DATA of length LEN with data DATA. */
1587 static void
1588 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1589 const char *data, size_t len, VEC (cgraph_node_ptr,
1590 heap) * nodes)
1592 const struct lto_function_header *header =
1593 (const struct lto_function_header *) data;
1594 const int32_t cfg_offset = sizeof (struct lto_function_header);
1595 const int32_t main_offset = cfg_offset + header->cfg_size;
1596 const int32_t string_offset = main_offset + header->main_size;
1597 struct data_in *data_in;
1598 struct lto_input_block ib_main;
1599 unsigned int i;
1600 unsigned int count;
1602 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1603 header->main_size);
1605 data_in =
1606 lto_data_in_create (file_data, (const char *) data + string_offset,
1607 header->string_size, NULL);
1608 count = lto_input_uleb128 (&ib_main);
1610 for (i = 0; i < count; i++)
1612 int ref = lto_input_uleb128 (&ib_main);
1613 input_node_opt_summary (VEC_index (cgraph_node_ptr, nodes, ref),
1614 &ib_main, data_in);
1616 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
1617 len);
1618 lto_data_in_delete (data_in);
1621 /* Input optimization summary of cgraph. */
1623 static void
1624 input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes)
1626 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1627 struct lto_file_decl_data *file_data;
1628 unsigned int j = 0;
1630 while ((file_data = file_data_vec[j++]))
1632 size_t len;
1633 const char *data =
1634 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1635 &len);
1637 if (data)
1638 input_cgraph_opt_section (file_data, data, len, nodes);