* c-parser.c (c_parser_for_statement): Initialize incr.
[official-gcc.git] / gcc / lto-cgraph.c
blobb61e5d167615591ea41848a020a249683704d81f
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, 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;
456 if (LTO_cgraph_analyzed_node)
457 gcc_assert (clone_of || !node->clone_of);
458 if (!clone_of)
459 lto_output_sleb128_stream (ob->main_stream, LCC_NOT_FOUND);
460 else
461 lto_output_sleb128_stream (ob->main_stream, ref);
464 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
465 lto_output_sleb128_stream (ob->main_stream, node->count);
467 if (tag == LTO_cgraph_analyzed_node)
469 lto_output_sleb128_stream (ob->main_stream,
470 node->local.inline_summary.estimated_self_stack_size);
471 lto_output_sleb128_stream (ob->main_stream,
472 node->local.inline_summary.self_size);
473 lto_output_sleb128_stream (ob->main_stream,
474 node->local.inline_summary.size_inlining_benefit);
475 lto_output_sleb128_stream (ob->main_stream,
476 node->local.inline_summary.self_time);
477 lto_output_sleb128_stream (ob->main_stream,
478 node->local.inline_summary.time_inlining_benefit);
479 if (node->global.inlined_to)
481 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
482 gcc_assert (ref != LCC_NOT_FOUND);
484 else
485 ref = LCC_NOT_FOUND;
487 lto_output_sleb128_stream (ob->main_stream, ref);
490 if (node->same_comdat_group && !boundary_p)
492 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
493 gcc_assert (ref != LCC_NOT_FOUND);
495 else
496 ref = LCC_NOT_FOUND;
497 lto_output_sleb128_stream (ob->main_stream, ref);
499 bp = bitpack_create (ob->main_stream);
500 bp_pack_value (&bp, node->local.local, 1);
501 bp_pack_value (&bp, node->local.externally_visible, 1);
502 bp_pack_value (&bp, node->local.finalized, 1);
503 bp_pack_value (&bp, node->local.inlinable, 1);
504 bp_pack_value (&bp, node->local.versionable, 1);
505 bp_pack_value (&bp, node->local.disregard_inline_limits, 1);
506 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
507 bp_pack_value (&bp, node->local.vtable_method, 1);
508 bp_pack_value (&bp, node->needed, 1);
509 bp_pack_value (&bp, node->address_taken, 1);
510 bp_pack_value (&bp, node->abstract_and_needed, 1);
511 bp_pack_value (&bp, tag == LTO_cgraph_analyzed_node
512 && !DECL_EXTERNAL (node->decl)
513 && !DECL_COMDAT (node->decl)
514 && (reachable_from_other_partition_p (node, set)
515 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
516 bp_pack_value (&bp, node->lowered, 1);
517 bp_pack_value (&bp, in_other_partition, 1);
518 bp_pack_value (&bp, node->alias, 1);
519 bp_pack_value (&bp, node->finalized_by_frontend, 1);
520 bp_pack_value (&bp, node->frequency, 2);
521 lto_output_bitpack (&bp);
522 lto_output_uleb128_stream (ob->main_stream, node->resolution);
524 if (node->same_body)
526 struct cgraph_node *alias;
527 unsigned long alias_count = 1;
528 for (alias = node->same_body; alias->next; alias = alias->next)
529 alias_count++;
530 lto_output_uleb128_stream (ob->main_stream, alias_count);
533 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
534 alias->decl);
535 if (alias->thunk.thunk_p)
537 lto_output_uleb128_stream
538 (ob->main_stream,
539 1 + (alias->thunk.this_adjusting != 0) * 2
540 + (alias->thunk.virtual_offset_p != 0) * 4);
541 lto_output_uleb128_stream (ob->main_stream,
542 alias->thunk.fixed_offset);
543 lto_output_uleb128_stream (ob->main_stream,
544 alias->thunk.virtual_value);
545 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
546 alias->thunk.alias);
548 else
550 lto_output_uleb128_stream (ob->main_stream, 0);
551 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
552 alias->thunk.alias);
554 lto_output_uleb128_stream (ob->main_stream, alias->resolution);
555 alias = alias->previous;
557 while (alias);
559 else
560 lto_output_uleb128_stream (ob->main_stream, 0);
563 /* Output the varpool NODE to OB.
564 If NODE is not in SET, then NODE is a boundary. */
566 static void
567 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
568 lto_varpool_encoder_t varpool_encoder,
569 cgraph_node_set set, varpool_node_set vset)
571 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
572 struct bitpack_d bp;
573 struct varpool_node *alias;
574 int count = 0;
575 int ref;
577 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
578 bp = bitpack_create (ob->main_stream);
579 bp_pack_value (&bp, node->externally_visible, 1);
580 bp_pack_value (&bp, node->force_output, 1);
581 bp_pack_value (&bp, node->finalized, 1);
582 bp_pack_value (&bp, node->alias, 1);
583 gcc_assert (!node->alias || !node->extra_name);
584 gcc_assert (node->finalized || !node->analyzed);
585 gcc_assert (node->needed);
586 /* Constant pool initializers can be de-unified into individual ltrans units.
587 FIXME: Alternatively at -Os we may want to avoid generating for them the local
588 labels and share them across LTRANS partitions. */
589 if (DECL_IN_CONSTANT_POOL (node->decl)
590 && !DECL_COMDAT (node->decl))
592 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
593 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
595 else
597 bp_pack_value (&bp, node->analyzed
598 && referenced_from_other_partition_p (&node->ref_list,
599 set, vset), 1);
600 bp_pack_value (&bp, boundary_p, 1); /* in_other_partition. */
602 /* Also emit any extra name aliases. */
603 for (alias = node->extra_name; alias; alias = alias->next)
604 count++;
605 bp_pack_value (&bp, count != 0, 1);
606 lto_output_bitpack (&bp);
607 if (node->same_comdat_group && !boundary_p)
609 ref = lto_varpool_encoder_lookup (varpool_encoder, node->same_comdat_group);
610 gcc_assert (ref != LCC_NOT_FOUND);
612 else
613 ref = LCC_NOT_FOUND;
614 lto_output_sleb128_stream (ob->main_stream, ref);
615 lto_output_uleb128_stream (ob->main_stream, node->resolution);
617 if (count)
619 lto_output_uleb128_stream (ob->main_stream, count);
620 for (alias = node->extra_name; alias; alias = alias->next)
622 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
623 lto_output_uleb128_stream (ob->main_stream, alias->resolution);
628 /* Output the varpool NODE to OB.
629 If NODE is not in SET, then NODE is a boundary. */
631 static void
632 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
633 lto_cgraph_encoder_t encoder,
634 lto_varpool_encoder_t varpool_encoder)
636 struct bitpack_d bp;
637 bp = bitpack_create (ob->main_stream);
638 bp_pack_value (&bp, ref->refered_type, 1);
639 bp_pack_value (&bp, ref->use, 2);
640 lto_output_bitpack (&bp);
641 if (ref->refered_type == IPA_REF_CGRAPH)
643 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
644 gcc_assert (nref != LCC_NOT_FOUND);
645 lto_output_sleb128_stream (ob->main_stream, nref);
647 else
649 int nref = lto_varpool_encoder_lookup (varpool_encoder,
650 ipa_ref_varpool_node (ref));
651 gcc_assert (nref != LCC_NOT_FOUND);
652 lto_output_sleb128_stream (ob->main_stream, nref);
656 /* Stream out profile_summary to OB. */
658 static void
659 output_profile_summary (struct lto_simple_output_block *ob)
661 if (profile_info)
663 /* We do not output num, it is not terribly useful. */
664 gcc_assert (profile_info->runs);
665 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
666 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_all);
667 lto_output_sleb128_stream (ob->main_stream, profile_info->run_max);
668 lto_output_sleb128_stream (ob->main_stream, profile_info->sum_max);
670 else
671 lto_output_uleb128_stream (ob->main_stream, 0);
674 /* Add NODE into encoder as well as nodes it is cloned from.
675 Do it in a way so clones appear first. */
677 static void
678 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node,
679 bool include_body)
681 if (node->clone_of)
682 add_node_to (encoder, node->clone_of, include_body);
683 else if (include_body)
684 lto_set_cgraph_encoder_encode_body (encoder, node);
685 lto_cgraph_encoder_encode (encoder, node);
688 /* Add all references in LIST to encoders. */
690 static void
691 add_references (lto_cgraph_encoder_t encoder,
692 lto_varpool_encoder_t varpool_encoder,
693 struct ipa_ref_list *list)
695 int i;
696 struct ipa_ref *ref;
697 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
698 if (ref->refered_type == IPA_REF_CGRAPH)
699 add_node_to (encoder, ipa_ref_node (ref), false);
700 else
702 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
703 lto_varpool_encoder_encode (varpool_encoder, vnode);
707 /* Output all callees or indirect outgoing edges. EDGE must be the first such
708 edge. */
710 static void
711 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
712 struct lto_simple_output_block *ob,
713 lto_cgraph_encoder_t encoder)
715 if (!edge)
716 return;
718 /* Output edges in backward direction, so the reconstructed callgraph match
719 and it is easy to associate call sites in the IPA pass summaries. */
720 while (edge->next_callee)
721 edge = edge->next_callee;
722 for (; edge; edge = edge->prev_callee)
723 lto_output_edge (ob, edge, encoder);
726 /* Output the part of the cgraph in SET. */
728 static void
729 output_refs (cgraph_node_set set, varpool_node_set vset,
730 lto_cgraph_encoder_t encoder,
731 lto_varpool_encoder_t varpool_encoder)
733 cgraph_node_set_iterator csi;
734 varpool_node_set_iterator vsi;
735 struct lto_simple_output_block *ob;
736 int count;
737 struct ipa_ref *ref;
738 int i;
740 ob = lto_create_simple_output_block (LTO_section_refs);
742 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
744 struct cgraph_node *node = csi_node (csi);
746 count = ipa_ref_list_nreferences (&node->ref_list);
747 if (count)
749 lto_output_uleb128_stream (ob->main_stream, count);
750 lto_output_uleb128_stream (ob->main_stream,
751 lto_cgraph_encoder_lookup (encoder, node));
752 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
753 lto_output_ref (ob, ref, encoder, varpool_encoder);
757 lto_output_uleb128_stream (ob->main_stream, 0);
759 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
761 struct varpool_node *node = vsi_node (vsi);
763 count = ipa_ref_list_nreferences (&node->ref_list);
764 if (count)
766 lto_output_uleb128_stream (ob->main_stream, count);
767 lto_output_uleb128_stream (ob->main_stream,
768 lto_varpool_encoder_lookup (varpool_encoder,
769 node));
770 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
771 lto_output_ref (ob, ref, encoder, varpool_encoder);
775 lto_output_uleb128_stream (ob->main_stream, 0);
777 lto_destroy_simple_output_block (ob);
780 /* Find out all cgraph and varpool nodes we want to encode in current unit
781 and insert them to encoders. */
782 void
783 compute_ltrans_boundary (struct lto_out_decl_state *state,
784 cgraph_node_set set, varpool_node_set vset)
786 struct cgraph_node *node;
787 cgraph_node_set_iterator csi;
788 varpool_node_set_iterator vsi;
789 struct cgraph_edge *edge;
790 int i;
791 lto_cgraph_encoder_t encoder;
792 lto_varpool_encoder_t varpool_encoder;
794 encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
795 varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
797 /* Go over all the nodes in SET and assign references. */
798 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
800 node = csi_node (csi);
801 add_node_to (encoder, node, true);
802 add_references (encoder, varpool_encoder, &node->ref_list);
804 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
806 struct varpool_node *vnode = vsi_node (vsi);
807 gcc_assert (!vnode->alias);
808 lto_varpool_encoder_encode (varpool_encoder, vnode);
809 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
810 add_references (encoder, varpool_encoder, &vnode->ref_list);
812 /* Pickle in also the initializer of all referenced readonly variables
813 to help folding. Constant pool variables are not shared, so we must
814 pickle those too. */
815 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
817 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
818 if (DECL_INITIAL (vnode->decl)
819 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
820 vnode)
821 && const_value_known_p (vnode->decl))
823 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
824 add_references (encoder, varpool_encoder, &vnode->ref_list);
828 /* Go over all the nodes again to include callees that are not in
829 SET. */
830 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
832 node = csi_node (csi);
833 for (edge = node->callees; edge; edge = edge->next_callee)
835 struct cgraph_node *callee = edge->callee;
836 if (!cgraph_node_in_set_p (callee, set))
838 /* We should have moved all the inlines. */
839 gcc_assert (!callee->global.inlined_to);
840 add_node_to (encoder, callee, false);
846 /* Output the part of the cgraph in SET. */
848 void
849 output_cgraph (cgraph_node_set set, varpool_node_set vset)
851 struct cgraph_node *node;
852 struct lto_simple_output_block *ob;
853 cgraph_node_set_iterator csi;
854 int i, n_nodes;
855 lto_cgraph_encoder_t encoder;
856 lto_varpool_encoder_t varpool_encoder;
857 struct cgraph_asm_node *can;
858 static bool asm_nodes_output = false;
860 if (flag_wpa)
861 output_cgraph_opt_summary ();
863 ob = lto_create_simple_output_block (LTO_section_cgraph);
865 output_profile_summary (ob);
867 /* An encoder for cgraph nodes should have been created by
868 ipa_write_summaries_1. */
869 gcc_assert (ob->decl_state->cgraph_node_encoder);
870 gcc_assert (ob->decl_state->varpool_node_encoder);
871 encoder = ob->decl_state->cgraph_node_encoder;
872 varpool_encoder = ob->decl_state->varpool_node_encoder;
874 /* Write out the nodes. We must first output a node and then its clones,
875 otherwise at a time reading back the node there would be nothing to clone
876 from. */
877 n_nodes = lto_cgraph_encoder_size (encoder);
878 for (i = 0; i < n_nodes; i++)
880 node = lto_cgraph_encoder_deref (encoder, i);
881 lto_output_node (ob, node, encoder, set, vset);
884 /* Go over the nodes in SET again to write edges. */
885 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
887 node = csi_node (csi);
888 output_outgoing_cgraph_edges (node->callees, ob, encoder);
889 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
892 lto_output_uleb128_stream (ob->main_stream, 0);
894 /* Emit toplevel asms.
895 When doing WPA we must output every asm just once. Since we do not partition asm
896 nodes at all, output them to first output. This is kind of hack, but should work
897 well. */
898 if (!asm_nodes_output)
900 asm_nodes_output = true;
901 for (can = cgraph_asm_nodes; can; can = can->next)
903 int len = TREE_STRING_LENGTH (can->asm_str);
904 lto_output_uleb128_stream (ob->main_stream, len);
905 for (i = 0; i < len; ++i)
906 lto_output_1_stream (ob->main_stream,
907 TREE_STRING_POINTER (can->asm_str)[i]);
911 lto_output_uleb128_stream (ob->main_stream, 0);
913 lto_destroy_simple_output_block (ob);
914 output_varpool (set, vset);
915 output_refs (set, vset, encoder, varpool_encoder);
918 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
919 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
920 NODE or to replace the values in it, for instance because the first
921 time we saw it, the function body was not available but now it
922 is. BP is a bitpack with all the bitflags for NODE read from the
923 stream. */
925 static void
926 input_overwrite_node (struct lto_file_decl_data *file_data,
927 struct cgraph_node *node,
928 enum LTO_cgraph_tags tag,
929 struct bitpack_d *bp,
930 unsigned int stack_size,
931 unsigned int self_time,
932 unsigned int time_inlining_benefit,
933 unsigned int self_size,
934 unsigned int size_inlining_benefit,
935 enum ld_plugin_symbol_resolution resolution)
937 node->aux = (void *) tag;
938 node->local.inline_summary.estimated_self_stack_size = stack_size;
939 node->local.inline_summary.self_time = self_time;
940 node->local.inline_summary.time_inlining_benefit = time_inlining_benefit;
941 node->local.inline_summary.self_size = self_size;
942 node->local.inline_summary.size_inlining_benefit = size_inlining_benefit;
943 node->global.time = self_time;
944 node->global.size = self_size;
945 node->global.estimated_stack_size = stack_size;
946 node->global.estimated_growth = INT_MIN;
947 node->local.lto_file_data = file_data;
949 node->local.local = bp_unpack_value (bp, 1);
950 node->local.externally_visible = bp_unpack_value (bp, 1);
951 node->local.finalized = bp_unpack_value (bp, 1);
952 node->local.inlinable = bp_unpack_value (bp, 1);
953 node->local.versionable = bp_unpack_value (bp, 1);
954 node->local.disregard_inline_limits = bp_unpack_value (bp, 1);
955 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
956 node->local.vtable_method = bp_unpack_value (bp, 1);
957 node->needed = bp_unpack_value (bp, 1);
958 node->address_taken = bp_unpack_value (bp, 1);
959 node->abstract_and_needed = bp_unpack_value (bp, 1);
960 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
961 node->lowered = bp_unpack_value (bp, 1);
962 node->analyzed = tag == LTO_cgraph_analyzed_node;
963 node->in_other_partition = bp_unpack_value (bp, 1);
964 if (node->in_other_partition
965 /* Avoid updating decl when we are seeing just inline clone.
966 When inlining function that has functions already inlined into it,
967 we produce clones of inline clones.
969 WPA partitioning might put each clone into different unit and
970 we might end up streaming inline clone from other partition
971 to support clone we are interested in. */
972 && (!node->clone_of
973 || node->clone_of->decl != node->decl))
975 DECL_EXTERNAL (node->decl) = 1;
976 TREE_STATIC (node->decl) = 0;
978 node->alias = bp_unpack_value (bp, 1);
979 node->finalized_by_frontend = bp_unpack_value (bp, 1);
980 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
981 node->resolution = resolution;
984 /* Output the part of the cgraph in SET. */
986 static void
987 output_varpool (cgraph_node_set set, varpool_node_set vset)
989 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
990 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
991 int len = lto_varpool_encoder_size (varpool_encoder), i;
993 lto_output_uleb128_stream (ob->main_stream, len);
995 /* Write out the nodes. We must first output a node and then its clones,
996 otherwise at a time reading back the node there would be nothing to clone
997 from. */
998 for (i = 0; i < len; i++)
1000 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
1001 varpool_encoder,
1002 set, vset);
1005 lto_destroy_simple_output_block (ob);
1008 /* Read a node from input_block IB. TAG is the node's tag just read.
1009 Return the node read or overwriten. */
1011 static struct cgraph_node *
1012 input_node (struct lto_file_decl_data *file_data,
1013 struct lto_input_block *ib,
1014 enum LTO_cgraph_tags tag,
1015 VEC(cgraph_node_ptr, heap) *nodes)
1017 tree fn_decl;
1018 struct cgraph_node *node;
1019 struct bitpack_d bp;
1020 int stack_size = 0;
1021 unsigned decl_index;
1022 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1023 int self_time = 0;
1024 int self_size = 0;
1025 int time_inlining_benefit = 0;
1026 int size_inlining_benefit = 0;
1027 unsigned long same_body_count = 0;
1028 int clone_ref;
1029 enum ld_plugin_symbol_resolution resolution;
1031 clone_ref = lto_input_sleb128 (ib);
1033 decl_index = lto_input_uleb128 (ib);
1034 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1036 if (clone_ref != LCC_NOT_FOUND)
1038 node = cgraph_clone_node (VEC_index (cgraph_node_ptr, nodes, clone_ref), fn_decl,
1039 0, CGRAPH_FREQ_BASE, 0, false, NULL);
1041 else
1042 node = cgraph_node (fn_decl);
1044 node->count = lto_input_sleb128 (ib);
1046 if (tag == LTO_cgraph_analyzed_node)
1048 stack_size = lto_input_sleb128 (ib);
1049 self_size = lto_input_sleb128 (ib);
1050 size_inlining_benefit = lto_input_sleb128 (ib);
1051 self_time = lto_input_sleb128 (ib);
1052 time_inlining_benefit = lto_input_sleb128 (ib);
1054 ref = lto_input_sleb128 (ib);
1057 ref2 = lto_input_sleb128 (ib);
1059 /* Make sure that we have not read this node before. Nodes that
1060 have already been read will have their tag stored in the 'aux'
1061 field. Since built-in functions can be referenced in multiple
1062 functions, they are expected to be read more than once. */
1063 if (node->aux && !DECL_IS_BUILTIN (node->decl))
1064 internal_error ("bytecode stream: found multiple instances of cgraph "
1065 "node %d", node->uid);
1067 bp = lto_input_bitpack (ib);
1068 resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1069 input_overwrite_node (file_data, node, tag, &bp, stack_size, self_time,
1070 time_inlining_benefit, self_size,
1071 size_inlining_benefit, resolution);
1073 /* Store a reference for now, and fix up later to be a pointer. */
1074 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1076 /* Store a reference for now, and fix up later to be a pointer. */
1077 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1079 same_body_count = lto_input_uleb128 (ib);
1080 while (same_body_count-- > 0)
1082 tree alias_decl;
1083 int type;
1084 struct cgraph_node *alias;
1085 decl_index = lto_input_uleb128 (ib);
1086 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1087 type = lto_input_uleb128 (ib);
1088 if (!type)
1090 tree real_alias;
1091 decl_index = lto_input_uleb128 (ib);
1092 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1093 alias = cgraph_same_body_alias (alias_decl, real_alias);
1095 else
1097 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1098 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1099 tree real_alias;
1100 decl_index = lto_input_uleb128 (ib);
1101 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1102 alias = cgraph_add_thunk (alias_decl, fn_decl, type & 2, fixed_offset,
1103 virtual_value,
1104 (type & 4) ? size_int (virtual_value) : NULL_TREE,
1105 real_alias);
1107 alias->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1109 return node;
1112 /* Read a node from input_block IB. TAG is the node's tag just read.
1113 Return the node read or overwriten. */
1115 static struct varpool_node *
1116 input_varpool_node (struct lto_file_decl_data *file_data,
1117 struct lto_input_block *ib)
1119 int decl_index;
1120 tree var_decl;
1121 struct varpool_node *node;
1122 struct bitpack_d bp;
1123 bool aliases_p;
1124 int count;
1125 int ref = LCC_NOT_FOUND;
1127 decl_index = lto_input_uleb128 (ib);
1128 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1129 node = varpool_node (var_decl);
1130 node->lto_file_data = file_data;
1132 bp = lto_input_bitpack (ib);
1133 node->externally_visible = bp_unpack_value (&bp, 1);
1134 node->force_output = bp_unpack_value (&bp, 1);
1135 node->finalized = bp_unpack_value (&bp, 1);
1136 node->alias = bp_unpack_value (&bp, 1);
1137 node->analyzed = node->finalized;
1138 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1139 node->in_other_partition = bp_unpack_value (&bp, 1);
1140 if (node->in_other_partition)
1142 DECL_EXTERNAL (node->decl) = 1;
1143 TREE_STATIC (node->decl) = 0;
1145 aliases_p = bp_unpack_value (&bp, 1);
1146 if (node->finalized)
1147 varpool_mark_needed_node (node);
1148 ref = lto_input_sleb128 (ib);
1149 /* Store a reference for now, and fix up later to be a pointer. */
1150 node->same_comdat_group = (struct varpool_node *) (intptr_t) ref;
1151 node->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1152 if (aliases_p)
1154 count = lto_input_uleb128 (ib);
1155 for (; count > 0; count --)
1157 tree decl = lto_file_decl_data_get_var_decl (file_data,
1158 lto_input_uleb128 (ib));
1159 struct varpool_node *alias;
1160 alias = varpool_extra_name_alias (decl, var_decl);
1161 alias->resolution = (enum ld_plugin_symbol_resolution)lto_input_uleb128 (ib);
1164 return node;
1167 /* Read a node from input_block IB. TAG is the node's tag just read.
1168 Return the node read or overwriten. */
1170 static void
1171 input_ref (struct lto_input_block *ib,
1172 struct cgraph_node *refering_node,
1173 struct varpool_node *refering_varpool_node,
1174 VEC(cgraph_node_ptr, heap) *nodes,
1175 VEC(varpool_node_ptr, heap) *varpool_nodes)
1177 struct cgraph_node *node = NULL;
1178 struct varpool_node *varpool_node = NULL;
1179 struct bitpack_d bp;
1180 enum ipa_ref_type type;
1181 enum ipa_ref_use use;
1183 bp = lto_input_bitpack (ib);
1184 type = (enum ipa_ref_type) bp_unpack_value (&bp, 1);
1185 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1186 if (type == IPA_REF_CGRAPH)
1187 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1188 else
1189 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1190 ipa_record_reference (refering_node, refering_varpool_node,
1191 node, varpool_node, use, NULL);
1194 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1195 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1196 edge being read is indirect (in the sense that it has
1197 indirect_unknown_callee set). */
1199 static void
1200 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1201 bool indirect)
1203 struct cgraph_node *caller, *callee;
1204 struct cgraph_edge *edge;
1205 unsigned int stmt_id;
1206 gcov_type count;
1207 int freq;
1208 unsigned int nest;
1209 cgraph_inline_failed_t inline_failed;
1210 struct bitpack_d bp;
1211 int ecf_flags = 0;
1213 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1214 if (caller == NULL || caller->decl == NULL_TREE)
1215 internal_error ("bytecode stream: no caller found while reading edge");
1217 if (!indirect)
1219 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1220 if (callee == NULL || callee->decl == NULL_TREE)
1221 internal_error ("bytecode stream: no callee found while reading edge");
1223 else
1224 callee = NULL;
1226 count = (gcov_type) lto_input_sleb128 (ib);
1228 bp = lto_input_bitpack (ib);
1229 stmt_id = (unsigned int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1230 inline_failed = (cgraph_inline_failed_t) bp_unpack_value (&bp,
1231 HOST_BITS_PER_INT);
1232 freq = (int) bp_unpack_value (&bp, HOST_BITS_PER_INT);
1233 nest = (unsigned) bp_unpack_value (&bp, 30);
1235 if (indirect)
1236 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq, nest);
1237 else
1238 edge = cgraph_create_edge (caller, callee, NULL, count, freq, nest);
1240 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1241 edge->lto_stmt_uid = stmt_id;
1242 edge->inline_failed = inline_failed;
1243 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1244 edge->can_throw_external = bp_unpack_value (&bp, 1);
1245 if (indirect)
1247 if (bp_unpack_value (&bp, 1))
1248 ecf_flags |= ECF_CONST;
1249 if (bp_unpack_value (&bp, 1))
1250 ecf_flags |= ECF_PURE;
1251 if (bp_unpack_value (&bp, 1))
1252 ecf_flags |= ECF_NORETURN;
1253 if (bp_unpack_value (&bp, 1))
1254 ecf_flags |= ECF_MALLOC;
1255 if (bp_unpack_value (&bp, 1))
1256 ecf_flags |= ECF_NOTHROW;
1257 if (bp_unpack_value (&bp, 1))
1258 ecf_flags |= ECF_RETURNS_TWICE;
1259 edge->indirect_info->ecf_flags = ecf_flags;
1264 /* Read a cgraph from IB using the info in FILE_DATA. */
1266 static VEC(cgraph_node_ptr, heap) *
1267 input_cgraph_1 (struct lto_file_decl_data *file_data,
1268 struct lto_input_block *ib)
1270 enum LTO_cgraph_tags tag;
1271 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1272 struct cgraph_node *node;
1273 unsigned i;
1274 unsigned HOST_WIDE_INT len;
1276 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1277 while (tag)
1279 if (tag == LTO_cgraph_edge)
1280 input_edge (ib, nodes, false);
1281 else if (tag == LTO_cgraph_indirect_edge)
1282 input_edge (ib, nodes, true);
1283 else
1285 node = input_node (file_data, ib, tag,nodes);
1286 if (node == NULL || node->decl == NULL_TREE)
1287 internal_error ("bytecode stream: found empty cgraph node");
1288 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1289 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1292 tag = (enum LTO_cgraph_tags) lto_input_uleb128 (ib);
1295 /* Input toplevel asms. */
1296 len = lto_input_uleb128 (ib);
1297 while (len)
1299 char *str = (char *)xmalloc (len + 1);
1300 for (i = 0; i < len; ++i)
1301 str[i] = lto_input_1_unsigned (ib);
1302 cgraph_add_asm_node (build_string (len, str));
1303 free (str);
1305 len = lto_input_uleb128 (ib);
1307 /* AUX pointers should be all non-zero for nodes read from the stream. */
1308 #ifdef ENABLE_CHECKING
1309 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1310 gcc_assert (node->aux);
1311 #endif
1312 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1314 int ref = (int) (intptr_t) node->global.inlined_to;
1316 /* We share declaration of builtins, so we may read same node twice. */
1317 if (!node->aux)
1318 continue;
1319 node->aux = NULL;
1321 /* Fixup inlined_to from reference to pointer. */
1322 if (ref != LCC_NOT_FOUND)
1323 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1324 else
1325 node->global.inlined_to = NULL;
1327 ref = (int) (intptr_t) node->same_comdat_group;
1329 /* Fixup same_comdat_group from reference to pointer. */
1330 if (ref != LCC_NOT_FOUND)
1331 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1332 else
1333 node->same_comdat_group = NULL;
1335 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1336 node->aux = (void *)1;
1337 return nodes;
1340 /* Read a varpool from IB using the info in FILE_DATA. */
1342 static VEC(varpool_node_ptr, heap) *
1343 input_varpool_1 (struct lto_file_decl_data *file_data,
1344 struct lto_input_block *ib)
1346 unsigned HOST_WIDE_INT len;
1347 VEC(varpool_node_ptr, heap) *varpool = NULL;
1348 int i;
1349 struct varpool_node *node;
1351 len = lto_input_uleb128 (ib);
1352 while (len)
1354 VEC_safe_push (varpool_node_ptr, heap, varpool,
1355 input_varpool_node (file_data, ib));
1356 len--;
1358 #ifdef ENABLE_CHECKING
1359 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1360 gcc_assert (!node->aux);
1361 #endif
1362 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1364 int ref = (int) (intptr_t) node->same_comdat_group;
1365 /* We share declaration of builtins, so we may read same node twice. */
1366 if (node->aux)
1367 continue;
1368 node->aux = (void *)1;
1370 /* Fixup same_comdat_group from reference to pointer. */
1371 if (ref != LCC_NOT_FOUND)
1372 node->same_comdat_group = VEC_index (varpool_node_ptr, varpool, ref);
1373 else
1374 node->same_comdat_group = NULL;
1376 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1377 node->aux = NULL;
1378 return varpool;
1381 /* Input ipa_refs. */
1383 static void
1384 input_refs (struct lto_input_block *ib,
1385 VEC(cgraph_node_ptr, heap) *nodes,
1386 VEC(varpool_node_ptr, heap) *varpool)
1388 int count;
1389 int idx;
1390 while (true)
1392 struct cgraph_node *node;
1393 count = lto_input_uleb128 (ib);
1394 if (!count)
1395 break;
1396 idx = lto_input_uleb128 (ib);
1397 node = VEC_index (cgraph_node_ptr, nodes, idx);
1398 while (count)
1400 input_ref (ib, node, NULL, nodes, varpool);
1401 count--;
1404 while (true)
1406 struct varpool_node *node;
1407 count = lto_input_uleb128 (ib);
1408 if (!count)
1409 break;
1410 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1411 while (count)
1413 input_ref (ib, NULL, node, nodes, varpool);
1414 count--;
1420 static struct gcov_ctr_summary lto_gcov_summary;
1422 /* Input profile_info from IB. */
1423 static void
1424 input_profile_summary (struct lto_input_block *ib)
1426 unsigned int runs = lto_input_uleb128 (ib);
1427 if (runs)
1429 if (!profile_info)
1431 profile_info = &lto_gcov_summary;
1432 lto_gcov_summary.runs = runs;
1433 lto_gcov_summary.sum_all = lto_input_sleb128 (ib);
1434 lto_gcov_summary.run_max = lto_input_sleb128 (ib);
1435 lto_gcov_summary.sum_max = lto_input_sleb128 (ib);
1437 /* We can support this by scaling all counts to nearest common multiple
1438 of all different runs, but it is perhaps not worth the effort. */
1439 else if (profile_info->runs != runs
1440 || profile_info->sum_all != lto_input_sleb128 (ib)
1441 || profile_info->run_max != lto_input_sleb128 (ib)
1442 || profile_info->sum_max != lto_input_sleb128 (ib))
1443 sorry ("Combining units with different profiles is not supported.");
1444 /* We allow some units to have profile and other to not have one. This will
1445 just make unprofiled units to be size optimized that is sane. */
1450 /* Input and merge the cgraph from each of the .o files passed to
1451 lto1. */
1453 void
1454 input_cgraph (void)
1456 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1457 struct lto_file_decl_data *file_data;
1458 unsigned int j = 0;
1459 struct cgraph_node *node;
1461 while ((file_data = file_data_vec[j++]))
1463 const char *data;
1464 size_t len;
1465 struct lto_input_block *ib;
1466 VEC(cgraph_node_ptr, heap) *nodes;
1467 VEC(varpool_node_ptr, heap) *varpool;
1469 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1470 &data, &len);
1471 if (!ib)
1472 fatal_error ("Cannot find LTO cgraph in %s\n", file_data->file_name);
1473 input_profile_summary (ib);
1474 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1475 nodes = input_cgraph_1 (file_data, ib);
1476 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1477 ib, data, len);
1479 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1480 &data, &len);
1481 if (!ib)
1482 fatal_error ("Cannot find LTO varpool in %s\n", file_data->file_name);
1483 varpool = input_varpool_1 (file_data, ib);
1484 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1485 ib, data, len);
1487 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1488 &data, &len);
1489 if (!ib)
1490 fatal_error("Cannot find LTO section refs in %s\n", file_data->file_name);
1491 input_refs (ib, nodes, varpool);
1492 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1493 ib, data, len);
1494 if (flag_ltrans)
1495 input_cgraph_opt_summary (nodes);
1496 VEC_free (cgraph_node_ptr, heap, nodes);
1497 VEC_free (varpool_node_ptr, heap, varpool);
1500 /* Clear out the aux field that was used to store enough state to
1501 tell which nodes should be overwritten. */
1502 for (node = cgraph_nodes; node; node = node->next)
1504 /* Some nodes may have been created by cgraph_node. This
1505 happens when the callgraph contains nested functions. If the
1506 node for the parent function was never emitted to the gimple
1507 file, cgraph_node will create a node for it when setting the
1508 context of the nested function. */
1509 if (node->local.lto_file_data)
1510 node->aux = NULL;
1514 /* True when we need optimization summary for NODE. */
1516 static int
1517 output_cgraph_opt_summary_p (struct cgraph_node *node)
1519 if (!node->clone_of)
1520 return false;
1521 return (node->clone.tree_map
1522 || node->clone.args_to_skip
1523 || node->clone.combined_args_to_skip);
1526 /* Output optimization summary for NODE to OB. */
1528 static void
1529 output_node_opt_summary (struct output_block *ob,
1530 struct cgraph_node *node)
1532 unsigned int index;
1533 bitmap_iterator bi;
1534 struct ipa_replace_map *map;
1535 struct bitpack_d bp;
1536 int i;
1538 lto_output_uleb128_stream (ob->main_stream,
1539 bitmap_count_bits (node->clone.args_to_skip));
1540 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1541 lto_output_uleb128_stream (ob->main_stream, index);
1542 lto_output_uleb128_stream (ob->main_stream,
1543 bitmap_count_bits (node->clone.combined_args_to_skip));
1544 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1545 lto_output_uleb128_stream (ob->main_stream, index);
1546 lto_output_uleb128_stream (ob->main_stream,
1547 VEC_length (ipa_replace_map_p, node->clone.tree_map));
1548 FOR_EACH_VEC_ELT (ipa_replace_map_p, node->clone.tree_map, i, map)
1550 int parm_num;
1551 tree parm;
1553 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm;
1554 parm = DECL_CHAIN (parm), parm_num++)
1555 if (map->old_tree == parm)
1556 break;
1557 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1558 mechanism to store function local declarations into summaries. */
1559 gcc_assert (parm);
1560 lto_output_uleb128_stream (ob->main_stream, parm_num);
1561 lto_output_tree (ob, map->new_tree, true);
1562 bp = bitpack_create (ob->main_stream);
1563 bp_pack_value (&bp, map->replace_p, 1);
1564 bp_pack_value (&bp, map->ref_p, 1);
1565 lto_output_bitpack (&bp);
1569 /* Output optimization summaries stored in callgraph.
1570 At the moment it is the clone info structure. */
1572 static void
1573 output_cgraph_opt_summary (void)
1575 struct cgraph_node *node;
1576 int i, n_nodes;
1577 lto_cgraph_encoder_t encoder;
1578 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1579 unsigned count = 0;
1581 ob->cgraph_node = NULL;
1582 encoder = ob->decl_state->cgraph_node_encoder;
1583 n_nodes = lto_cgraph_encoder_size (encoder);
1584 for (i = 0; i < n_nodes; i++)
1585 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder, i)))
1586 count++;
1587 lto_output_uleb128_stream (ob->main_stream, count);
1588 for (i = 0; i < n_nodes; i++)
1590 node = lto_cgraph_encoder_deref (encoder, i);
1591 if (output_cgraph_opt_summary_p (node))
1593 lto_output_uleb128_stream (ob->main_stream, i);
1594 output_node_opt_summary (ob, node);
1597 produce_asm (ob, NULL);
1598 destroy_output_block (ob);
1601 /* Input optimiation summary of NODE. */
1603 static void
1604 input_node_opt_summary (struct cgraph_node *node,
1605 struct lto_input_block *ib_main,
1606 struct data_in *data_in)
1608 int i;
1609 int count;
1610 int bit;
1611 struct bitpack_d bp;
1613 count = lto_input_uleb128 (ib_main);
1614 if (count)
1615 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1616 for (i = 0; i < count; i++)
1618 bit = lto_input_uleb128 (ib_main);
1619 bitmap_set_bit (node->clone.args_to_skip, bit);
1621 count = lto_input_uleb128 (ib_main);
1622 if (count)
1623 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1624 for (i = 0; i < count; i++)
1626 bit = lto_input_uleb128 (ib_main);
1627 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1629 count = lto_input_uleb128 (ib_main);
1630 for (i = 0; i < count; i++)
1632 int parm_num;
1633 tree parm;
1634 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1636 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1637 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm_num;
1638 parm = DECL_CHAIN (parm))
1639 parm_num --;
1640 map->parm_num = lto_input_uleb128 (ib_main);
1641 map->old_tree = NULL;
1642 map->new_tree = lto_input_tree (ib_main, data_in);
1643 bp = lto_input_bitpack (ib_main);
1644 map->replace_p = bp_unpack_value (&bp, 1);
1645 map->ref_p = bp_unpack_value (&bp, 1);
1649 /* Read section in file FILE_DATA of length LEN with data DATA. */
1651 static void
1652 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1653 const char *data, size_t len, VEC (cgraph_node_ptr,
1654 heap) * nodes)
1656 const struct lto_function_header *header =
1657 (const struct lto_function_header *) data;
1658 const int32_t cfg_offset = sizeof (struct lto_function_header);
1659 const int32_t main_offset = cfg_offset + header->cfg_size;
1660 const int32_t string_offset = main_offset + header->main_size;
1661 struct data_in *data_in;
1662 struct lto_input_block ib_main;
1663 unsigned int i;
1664 unsigned int count;
1666 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1667 header->main_size);
1669 data_in =
1670 lto_data_in_create (file_data, (const char *) data + string_offset,
1671 header->string_size, NULL);
1672 count = lto_input_uleb128 (&ib_main);
1674 for (i = 0; i < count; i++)
1676 int ref = lto_input_uleb128 (&ib_main);
1677 input_node_opt_summary (VEC_index (cgraph_node_ptr, nodes, ref),
1678 &ib_main, data_in);
1680 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
1681 len);
1682 lto_data_in_delete (data_in);
1685 /* Input optimization summary of cgraph. */
1687 static void
1688 input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes)
1690 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1691 struct lto_file_decl_data *file_data;
1692 unsigned int j = 0;
1694 while ((file_data = file_data_vec[j++]))
1696 size_t len;
1697 const char *data =
1698 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1699 &len);
1701 if (data)
1702 input_cgraph_opt_section (file_data, data, len, nodes);