20090811-1.c: Skip for incompatible options, do not override other options.
[official-gcc.git] / gcc / lto-cgraph.c
blob3b1115b53f63babf4ac8c89f954fd287bc99ef6e
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "hashtab.h"
33 #include "langhooks.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "cgraph.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "diagnostic-core.h"
40 #include "except.h"
41 #include "vec.h"
42 #include "timevar.h"
43 #include "output.h"
44 #include "pointer-set.h"
45 #include "lto-streamer.h"
46 #include "gcov-io.h"
48 static void output_varpool (cgraph_node_set, varpool_node_set);
49 static void output_cgraph_opt_summary (cgraph_node_set set);
50 static void input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes);
52 /* Number of LDPR values known to GCC. */
53 #define LDPR_NUM_KNOWN (LDPR_RESOLVED_DYN + 1)
55 /* Cgraph streaming is organized as set of record whose type
56 is indicated by a tag. */
57 enum LTO_cgraph_tags
59 /* Must leave 0 for the stopper. */
61 /* Cgraph node without body available. */
62 LTO_cgraph_unavail_node = 1,
63 /* Cgraph node with function body. */
64 LTO_cgraph_analyzed_node,
65 /* Cgraph edges. */
66 LTO_cgraph_edge,
67 LTO_cgraph_indirect_edge,
68 LTO_cgraph_last_tag
71 /* Create a new cgraph encoder. */
73 lto_cgraph_encoder_t
74 lto_cgraph_encoder_new (void)
76 lto_cgraph_encoder_t encoder = XCNEW (struct lto_cgraph_encoder_d);
77 encoder->map = pointer_map_create ();
78 encoder->nodes = NULL;
79 encoder->body = pointer_set_create ();
80 return encoder;
84 /* Delete ENCODER and its components. */
86 void
87 lto_cgraph_encoder_delete (lto_cgraph_encoder_t encoder)
89 VEC_free (cgraph_node_ptr, heap, encoder->nodes);
90 pointer_map_destroy (encoder->map);
91 pointer_set_destroy (encoder->body);
92 free (encoder);
96 /* Return the existing reference number of NODE in the cgraph encoder in
97 output block OB. Assign a new reference if this is the first time
98 NODE is encoded. */
101 lto_cgraph_encoder_encode (lto_cgraph_encoder_t encoder,
102 struct cgraph_node *node)
104 int ref;
105 void **slot;
107 slot = pointer_map_contains (encoder->map, node);
108 if (!slot)
110 ref = VEC_length (cgraph_node_ptr, encoder->nodes);
111 slot = pointer_map_insert (encoder->map, node);
112 *slot = (void *) (intptr_t) ref;
113 VEC_safe_push (cgraph_node_ptr, heap, encoder->nodes, node);
115 else
116 ref = (int) (intptr_t) *slot;
118 return ref;
121 #define LCC_NOT_FOUND (-1)
123 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
124 or LCC_NOT_FOUND if it is not there. */
127 lto_cgraph_encoder_lookup (lto_cgraph_encoder_t encoder,
128 struct cgraph_node *node)
130 void **slot = pointer_map_contains (encoder->map, node);
131 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
135 /* Return the cgraph node corresponding to REF using ENCODER. */
137 struct cgraph_node *
138 lto_cgraph_encoder_deref (lto_cgraph_encoder_t encoder, int ref)
140 if (ref == LCC_NOT_FOUND)
141 return NULL;
143 return VEC_index (cgraph_node_ptr, encoder->nodes, ref);
147 /* Return TRUE if we should encode initializer of NODE (if any). */
149 bool
150 lto_cgraph_encoder_encode_body_p (lto_cgraph_encoder_t encoder,
151 struct cgraph_node *node)
153 return pointer_set_contains (encoder->body, node);
156 /* Return TRUE if we should encode body of NODE (if any). */
158 static void
159 lto_set_cgraph_encoder_encode_body (lto_cgraph_encoder_t encoder,
160 struct cgraph_node *node)
162 pointer_set_insert (encoder->body, node);
165 /* Create a new varpool encoder. */
167 lto_varpool_encoder_t
168 lto_varpool_encoder_new (void)
170 lto_varpool_encoder_t encoder = XCNEW (struct lto_varpool_encoder_d);
171 encoder->map = pointer_map_create ();
172 encoder->initializer = pointer_set_create ();
173 encoder->nodes = NULL;
174 return encoder;
178 /* Delete ENCODER and its components. */
180 void
181 lto_varpool_encoder_delete (lto_varpool_encoder_t encoder)
183 VEC_free (varpool_node_ptr, heap, encoder->nodes);
184 pointer_map_destroy (encoder->map);
185 pointer_set_destroy (encoder->initializer);
186 free (encoder);
190 /* Return the existing reference number of NODE in the varpool encoder in
191 output block OB. Assign a new reference if this is the first time
192 NODE is encoded. */
195 lto_varpool_encoder_encode (lto_varpool_encoder_t encoder,
196 struct varpool_node *node)
198 int ref;
199 void **slot;
201 slot = pointer_map_contains (encoder->map, node);
202 if (!slot)
204 ref = VEC_length (varpool_node_ptr, encoder->nodes);
205 slot = pointer_map_insert (encoder->map, node);
206 *slot = (void *) (intptr_t) ref;
207 VEC_safe_push (varpool_node_ptr, heap, encoder->nodes, node);
209 else
210 ref = (int) (intptr_t) *slot;
212 return ref;
215 /* Look up NODE in encoder. Return NODE's reference if it has been encoded
216 or LCC_NOT_FOUND if it is not there. */
219 lto_varpool_encoder_lookup (lto_varpool_encoder_t encoder,
220 struct varpool_node *node)
222 void **slot = pointer_map_contains (encoder->map, node);
223 return (slot ? (int) (intptr_t) *slot : LCC_NOT_FOUND);
227 /* Return the varpool node corresponding to REF using ENCODER. */
229 struct varpool_node *
230 lto_varpool_encoder_deref (lto_varpool_encoder_t encoder, int ref)
232 if (ref == LCC_NOT_FOUND)
233 return NULL;
235 return VEC_index (varpool_node_ptr, encoder->nodes, ref);
239 /* Return TRUE if we should encode initializer of NODE (if any). */
241 bool
242 lto_varpool_encoder_encode_initializer_p (lto_varpool_encoder_t encoder,
243 struct varpool_node *node)
245 return pointer_set_contains (encoder->initializer, node);
248 /* Return TRUE if we should encode initializer of NODE (if any). */
250 static void
251 lto_set_varpool_encoder_encode_initializer (lto_varpool_encoder_t encoder,
252 struct varpool_node *node)
254 pointer_set_insert (encoder->initializer, node);
257 /* Output the cgraph EDGE to OB using ENCODER. */
259 static void
260 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
261 lto_cgraph_encoder_t encoder)
263 unsigned int uid;
264 intptr_t ref;
265 struct bitpack_d bp;
267 if (edge->indirect_unknown_callee)
268 lto_output_enum (ob->main_stream, LTO_cgraph_tags, LTO_cgraph_last_tag,
269 LTO_cgraph_indirect_edge);
270 else
271 lto_output_enum (ob->main_stream, LTO_cgraph_tags, LTO_cgraph_last_tag,
272 LTO_cgraph_edge);
274 ref = lto_cgraph_encoder_lookup (encoder, edge->caller);
275 gcc_assert (ref != LCC_NOT_FOUND);
276 lto_output_sleb128_stream (ob->main_stream, ref);
278 if (!edge->indirect_unknown_callee)
280 ref = lto_cgraph_encoder_lookup (encoder, edge->callee);
281 gcc_assert (ref != LCC_NOT_FOUND);
282 lto_output_sleb128_stream (ob->main_stream, ref);
285 lto_output_sleb128_stream (ob->main_stream, edge->count);
287 bp = bitpack_create (ob->main_stream);
288 uid = (!gimple_has_body_p (edge->caller->decl)
289 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt));
290 bp_pack_enum (&bp, cgraph_inline_failed_enum,
291 CIF_N_REASONS, edge->inline_failed);
292 bp_pack_var_len_unsigned (&bp, uid);
293 bp_pack_var_len_unsigned (&bp, edge->frequency);
294 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
295 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
296 bp_pack_value (&bp, edge->can_throw_external, 1);
297 if (edge->indirect_unknown_callee)
299 int flags = edge->indirect_info->ecf_flags;
300 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
301 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
302 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
303 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
304 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
305 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
306 /* Flags that should not appear on indirect calls. */
307 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
308 | ECF_MAY_BE_ALLOCA
309 | ECF_SIBCALL
310 | ECF_LEAF
311 | ECF_NOVOPS)));
313 lto_output_bitpack (&bp);
316 /* Return if LIST contain references from other partitions. */
318 bool
319 referenced_from_other_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
320 varpool_node_set vset)
322 int i;
323 struct ipa_ref *ref;
324 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
326 if (ref->refering_type == IPA_REF_CGRAPH)
328 if (ipa_ref_refering_node (ref)->in_other_partition
329 || !cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
330 return true;
332 else
334 if (ipa_ref_refering_varpool_node (ref)->in_other_partition
335 || !varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
336 vset))
337 return true;
340 return false;
343 /* Return true when node is reachable from other partition. */
345 bool
346 reachable_from_other_partition_p (struct cgraph_node *node, cgraph_node_set set)
348 struct cgraph_edge *e;
349 if (!node->analyzed)
350 return false;
351 if (node->global.inlined_to)
352 return false;
353 for (e = node->callers; e; e = e->next_caller)
354 if (e->caller->in_other_partition
355 || !cgraph_node_in_set_p (e->caller, set))
356 return true;
357 return false;
360 /* Return if LIST contain references from other partitions. */
362 bool
363 referenced_from_this_partition_p (struct ipa_ref_list *list, cgraph_node_set set,
364 varpool_node_set vset)
366 int i;
367 struct ipa_ref *ref;
368 for (i = 0; ipa_ref_list_refering_iterate (list, i, ref); i++)
370 if (ref->refering_type == IPA_REF_CGRAPH)
372 if (cgraph_node_in_set_p (ipa_ref_refering_node (ref), set))
373 return true;
375 else
377 if (varpool_node_in_set_p (ipa_ref_refering_varpool_node (ref),
378 vset))
379 return true;
382 return false;
385 /* Return true when node is reachable from other partition. */
387 bool
388 reachable_from_this_partition_p (struct cgraph_node *node, cgraph_node_set set)
390 struct cgraph_edge *e;
391 for (e = node->callers; e; e = e->next_caller)
392 if (cgraph_node_in_set_p (e->caller, set))
393 return true;
394 return false;
397 /* Output the cgraph NODE to OB. ENCODER is used to find the
398 reference number of NODE->inlined_to. SET is the set of nodes we
399 are writing to the current file. If NODE is not in SET, then NODE
400 is a boundary of a cgraph_node_set and we pretend NODE just has a
401 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
402 that have had their callgraph node written so far. This is used to
403 determine if NODE is a clone of a previously written node. */
405 static void
406 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
407 lto_cgraph_encoder_t encoder, cgraph_node_set set,
408 varpool_node_set vset)
410 unsigned int tag;
411 struct bitpack_d bp;
412 bool boundary_p;
413 intptr_t ref;
414 bool in_other_partition = false;
415 struct cgraph_node *clone_of;
417 boundary_p = !cgraph_node_in_set_p (node, set);
419 if (node->analyzed && !boundary_p)
420 tag = LTO_cgraph_analyzed_node;
421 else
422 tag = LTO_cgraph_unavail_node;
424 lto_output_enum (ob->main_stream, LTO_cgraph_tags, LTO_cgraph_last_tag, tag);
426 /* In WPA mode, we only output part of the call-graph. Also, we
427 fake cgraph node attributes. There are two cases that we care.
429 Boundary nodes: There are nodes that are not part of SET but are
430 called from within SET. We artificially make them look like
431 externally visible nodes with no function body.
433 Cherry-picked nodes: These are nodes we pulled from other
434 translation units into SET during IPA-inlining. We make them as
435 local static nodes to prevent clashes with other local statics. */
436 if (boundary_p && node->analyzed)
438 /* Inline clones can not be part of boundary.
439 gcc_assert (!node->global.inlined_to);
441 FIXME: At the moment they can be, when partition contains an inline
442 clone that is clone of inline clone from outside partition. We can
443 reshape the clone tree and make other tree to be the root, but it
444 needs a bit extra work and will be promplty done by cgraph_remove_node
445 after reading back. */
446 in_other_partition = 1;
449 clone_of = node->clone_of;
450 while (clone_of
451 && (ref = lto_cgraph_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
452 if (clone_of->prev_sibling_clone)
453 clone_of = clone_of->prev_sibling_clone;
454 else
455 clone_of = clone_of->clone_of;
457 if (LTO_cgraph_analyzed_node)
458 gcc_assert (clone_of || !node->clone_of);
459 if (!clone_of)
460 lto_output_sleb128_stream (ob->main_stream, LCC_NOT_FOUND);
461 else
462 lto_output_sleb128_stream (ob->main_stream, ref);
465 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
466 lto_output_sleb128_stream (ob->main_stream, node->count);
467 lto_output_sleb128_stream (ob->main_stream, node->count_materialization_scale);
469 if (tag == LTO_cgraph_analyzed_node)
471 if (node->global.inlined_to)
473 ref = lto_cgraph_encoder_lookup (encoder, node->global.inlined_to);
474 gcc_assert (ref != LCC_NOT_FOUND);
476 else
477 ref = LCC_NOT_FOUND;
479 lto_output_sleb128_stream (ob->main_stream, ref);
482 if (node->same_comdat_group && !boundary_p)
484 ref = lto_cgraph_encoder_lookup (encoder, node->same_comdat_group);
485 gcc_assert (ref != LCC_NOT_FOUND);
487 else
488 ref = LCC_NOT_FOUND;
489 lto_output_sleb128_stream (ob->main_stream, ref);
491 bp = bitpack_create (ob->main_stream);
492 bp_pack_value (&bp, node->local.local, 1);
493 bp_pack_value (&bp, node->local.externally_visible, 1);
494 bp_pack_value (&bp, node->local.finalized, 1);
495 bp_pack_value (&bp, node->local.can_change_signature, 1);
496 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
497 bp_pack_value (&bp, node->needed, 1);
498 bp_pack_value (&bp, node->address_taken, 1);
499 bp_pack_value (&bp, node->abstract_and_needed, 1);
500 bp_pack_value (&bp, tag == LTO_cgraph_analyzed_node
501 && !DECL_EXTERNAL (node->decl)
502 && !DECL_COMDAT (node->decl)
503 && (reachable_from_other_partition_p (node, set)
504 || referenced_from_other_partition_p (&node->ref_list, set, vset)), 1);
505 bp_pack_value (&bp, node->lowered, 1);
506 bp_pack_value (&bp, in_other_partition, 1);
507 bp_pack_value (&bp, node->alias, 1);
508 bp_pack_value (&bp, node->frequency, 2);
509 bp_pack_value (&bp, node->only_called_at_startup, 1);
510 bp_pack_value (&bp, node->only_called_at_exit, 1);
511 bp_pack_value (&bp, node->thunk.thunk_p && !boundary_p, 1);
512 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
513 LDPR_NUM_KNOWN, node->resolution);
514 lto_output_bitpack (&bp);
516 if (node->thunk.thunk_p && !boundary_p)
518 lto_output_uleb128_stream
519 (ob->main_stream,
520 1 + (node->thunk.this_adjusting != 0) * 2
521 + (node->thunk.virtual_offset_p != 0) * 4);
522 lto_output_uleb128_stream (ob->main_stream,
523 node->thunk.fixed_offset);
524 lto_output_uleb128_stream (ob->main_stream,
525 node->thunk.virtual_value);
526 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
527 node->thunk.alias);
530 if (node->same_body)
532 struct cgraph_node *alias;
533 unsigned long alias_count = 1;
534 for (alias = node->same_body; alias->next; alias = alias->next)
535 alias_count++;
536 lto_output_uleb128_stream (ob->main_stream, alias_count);
539 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
540 alias->decl);
541 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
542 alias->thunk.alias);
543 gcc_assert (cgraph_get_node (alias->thunk.alias) == node);
544 lto_output_enum (ob->main_stream, ld_plugin_symbol_resolution,
545 LDPR_NUM_KNOWN, alias->resolution);
546 alias = alias->previous;
548 while (alias);
550 else
551 lto_output_uleb128_stream (ob->main_stream, 0);
554 /* Output the varpool NODE to OB.
555 If NODE is not in SET, then NODE is a boundary. */
557 static void
558 lto_output_varpool_node (struct lto_simple_output_block *ob, struct varpool_node *node,
559 lto_varpool_encoder_t varpool_encoder,
560 cgraph_node_set set, varpool_node_set vset)
562 bool boundary_p = !varpool_node_in_set_p (node, vset) && node->analyzed;
563 struct bitpack_d bp;
564 struct varpool_node *alias;
565 int count = 0;
566 int ref;
568 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
569 bp = bitpack_create (ob->main_stream);
570 bp_pack_value (&bp, node->externally_visible, 1);
571 bp_pack_value (&bp, node->force_output, 1);
572 bp_pack_value (&bp, node->finalized, 1);
573 bp_pack_value (&bp, node->alias, 1);
574 gcc_assert (!node->alias || !node->extra_name);
575 gcc_assert (node->finalized || !node->analyzed);
576 gcc_assert (node->needed);
577 /* Constant pool initializers can be de-unified into individual ltrans units.
578 FIXME: Alternatively at -Os we may want to avoid generating for them the local
579 labels and share them across LTRANS partitions. */
580 if (DECL_IN_CONSTANT_POOL (node->decl)
581 && !DECL_COMDAT (node->decl))
583 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
584 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
586 else
588 bp_pack_value (&bp, node->analyzed
589 && referenced_from_other_partition_p (&node->ref_list,
590 set, vset), 1);
591 bp_pack_value (&bp, boundary_p, 1); /* in_other_partition. */
593 /* Also emit any extra name aliases. */
594 for (alias = node->extra_name; alias; alias = alias->next)
595 count++;
596 bp_pack_value (&bp, count != 0, 1);
597 lto_output_bitpack (&bp);
598 if (node->same_comdat_group && !boundary_p)
600 ref = lto_varpool_encoder_lookup (varpool_encoder, node->same_comdat_group);
601 gcc_assert (ref != LCC_NOT_FOUND);
603 else
604 ref = LCC_NOT_FOUND;
605 lto_output_sleb128_stream (ob->main_stream, ref);
606 lto_output_enum (ob->main_stream, ld_plugin_symbol_resolution,
607 LDPR_NUM_KNOWN, node->resolution);
609 if (count)
611 lto_output_uleb128_stream (ob->main_stream, count);
612 for (alias = node->extra_name; alias; alias = alias->next)
614 lto_output_var_decl_index (ob->decl_state, ob->main_stream, alias->decl);
615 lto_output_enum (ob->main_stream, ld_plugin_symbol_resolution,
616 LDPR_NUM_KNOWN, alias->resolution);
621 /* Output the varpool NODE to OB.
622 If NODE is not in SET, then NODE is a boundary. */
624 static void
625 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
626 lto_cgraph_encoder_t encoder,
627 lto_varpool_encoder_t varpool_encoder)
629 struct bitpack_d bp;
630 bp = bitpack_create (ob->main_stream);
631 bp_pack_value (&bp, ref->refered_type, 1);
632 bp_pack_value (&bp, ref->use, 2);
633 lto_output_bitpack (&bp);
634 if (ref->refered_type == IPA_REF_CGRAPH)
636 int nref = lto_cgraph_encoder_lookup (encoder, ipa_ref_node (ref));
637 gcc_assert (nref != LCC_NOT_FOUND);
638 lto_output_sleb128_stream (ob->main_stream, nref);
640 else
642 int nref = lto_varpool_encoder_lookup (varpool_encoder,
643 ipa_ref_varpool_node (ref));
644 gcc_assert (nref != LCC_NOT_FOUND);
645 lto_output_sleb128_stream (ob->main_stream, nref);
649 /* Stream out profile_summary to OB. */
651 static void
652 output_profile_summary (struct lto_simple_output_block *ob)
654 if (profile_info)
656 /* We do not output num, sum_all and run_max, they are not used by
657 GCC profile feedback and they are difficult to merge from multiple
658 units. */
659 gcc_assert (profile_info->runs);
660 lto_output_uleb128_stream (ob->main_stream, profile_info->runs);
661 lto_output_uleb128_stream (ob->main_stream, profile_info->sum_max);
663 else
664 lto_output_uleb128_stream (ob->main_stream, 0);
667 /* Add NODE into encoder as well as nodes it is cloned from.
668 Do it in a way so clones appear first. */
670 static void
671 add_node_to (lto_cgraph_encoder_t encoder, struct cgraph_node *node,
672 bool include_body)
674 if (node->clone_of)
675 add_node_to (encoder, node->clone_of, include_body);
676 else if (include_body)
677 lto_set_cgraph_encoder_encode_body (encoder, node);
678 lto_cgraph_encoder_encode (encoder, node);
681 /* Add all references in LIST to encoders. */
683 static void
684 add_references (lto_cgraph_encoder_t encoder,
685 lto_varpool_encoder_t varpool_encoder,
686 struct ipa_ref_list *list)
688 int i;
689 struct ipa_ref *ref;
690 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
691 if (ref->refered_type == IPA_REF_CGRAPH)
692 add_node_to (encoder, ipa_ref_node (ref), false);
693 else
695 struct varpool_node *vnode = ipa_ref_varpool_node (ref);
696 lto_varpool_encoder_encode (varpool_encoder, vnode);
700 /* Output all callees or indirect outgoing edges. EDGE must be the first such
701 edge. */
703 static void
704 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
705 struct lto_simple_output_block *ob,
706 lto_cgraph_encoder_t encoder)
708 if (!edge)
709 return;
711 /* Output edges in backward direction, so the reconstructed callgraph match
712 and it is easy to associate call sites in the IPA pass summaries. */
713 while (edge->next_callee)
714 edge = edge->next_callee;
715 for (; edge; edge = edge->prev_callee)
716 lto_output_edge (ob, edge, encoder);
719 /* Output the part of the cgraph in SET. */
721 static void
722 output_refs (cgraph_node_set set, varpool_node_set vset,
723 lto_cgraph_encoder_t encoder,
724 lto_varpool_encoder_t varpool_encoder)
726 cgraph_node_set_iterator csi;
727 varpool_node_set_iterator vsi;
728 struct lto_simple_output_block *ob;
729 int count;
730 struct ipa_ref *ref;
731 int i;
733 ob = lto_create_simple_output_block (LTO_section_refs);
735 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
737 struct cgraph_node *node = csi_node (csi);
739 count = ipa_ref_list_nreferences (&node->ref_list);
740 if (count)
742 lto_output_uleb128_stream (ob->main_stream, count);
743 lto_output_uleb128_stream (ob->main_stream,
744 lto_cgraph_encoder_lookup (encoder, node));
745 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
746 lto_output_ref (ob, ref, encoder, varpool_encoder);
750 lto_output_uleb128_stream (ob->main_stream, 0);
752 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
754 struct varpool_node *node = vsi_node (vsi);
756 count = ipa_ref_list_nreferences (&node->ref_list);
757 if (count)
759 lto_output_uleb128_stream (ob->main_stream, count);
760 lto_output_uleb128_stream (ob->main_stream,
761 lto_varpool_encoder_lookup (varpool_encoder,
762 node));
763 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
764 lto_output_ref (ob, ref, encoder, varpool_encoder);
768 lto_output_uleb128_stream (ob->main_stream, 0);
770 lto_destroy_simple_output_block (ob);
773 /* Find out all cgraph and varpool nodes we want to encode in current unit
774 and insert them to encoders. */
775 void
776 compute_ltrans_boundary (struct lto_out_decl_state *state,
777 cgraph_node_set set, varpool_node_set vset)
779 struct cgraph_node *node;
780 cgraph_node_set_iterator csi;
781 varpool_node_set_iterator vsi;
782 struct cgraph_edge *edge;
783 int i;
784 lto_cgraph_encoder_t encoder;
785 lto_varpool_encoder_t varpool_encoder;
787 encoder = state->cgraph_node_encoder = lto_cgraph_encoder_new ();
788 varpool_encoder = state->varpool_node_encoder = lto_varpool_encoder_new ();
790 /* Go over all the nodes in SET and assign references. */
791 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
793 node = csi_node (csi);
794 add_node_to (encoder, node, true);
795 add_references (encoder, varpool_encoder, &node->ref_list);
797 for (vsi = vsi_start (vset); !vsi_end_p (vsi); vsi_next (&vsi))
799 struct varpool_node *vnode = vsi_node (vsi);
800 gcc_assert (!vnode->alias);
801 lto_varpool_encoder_encode (varpool_encoder, vnode);
802 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
803 add_references (encoder, varpool_encoder, &vnode->ref_list);
805 /* Pickle in also the initializer of all referenced readonly variables
806 to help folding. Constant pool variables are not shared, so we must
807 pickle those too. */
808 for (i = 0; i < lto_varpool_encoder_size (varpool_encoder); i++)
810 struct varpool_node *vnode = lto_varpool_encoder_deref (varpool_encoder, i);
811 if (DECL_INITIAL (vnode->decl)
812 && !lto_varpool_encoder_encode_initializer_p (varpool_encoder,
813 vnode)
814 && const_value_known_p (vnode->decl))
816 lto_set_varpool_encoder_encode_initializer (varpool_encoder, vnode);
817 add_references (encoder, varpool_encoder, &vnode->ref_list);
821 /* Go over all the nodes again to include callees that are not in
822 SET. */
823 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
825 node = csi_node (csi);
826 for (edge = node->callees; edge; edge = edge->next_callee)
828 struct cgraph_node *callee = edge->callee;
829 if (!cgraph_node_in_set_p (callee, set))
831 /* We should have moved all the inlines. */
832 gcc_assert (!callee->global.inlined_to);
833 add_node_to (encoder, callee, false);
839 /* Output the part of the cgraph in SET. */
841 void
842 output_cgraph (cgraph_node_set set, varpool_node_set vset)
844 struct cgraph_node *node;
845 struct lto_simple_output_block *ob;
846 cgraph_node_set_iterator csi;
847 int i, n_nodes;
848 lto_cgraph_encoder_t encoder;
849 lto_varpool_encoder_t varpool_encoder;
850 struct cgraph_asm_node *can;
851 static bool asm_nodes_output = false;
853 if (flag_wpa)
854 output_cgraph_opt_summary (set);
856 ob = lto_create_simple_output_block (LTO_section_cgraph);
858 output_profile_summary (ob);
860 /* An encoder for cgraph nodes should have been created by
861 ipa_write_summaries_1. */
862 gcc_assert (ob->decl_state->cgraph_node_encoder);
863 gcc_assert (ob->decl_state->varpool_node_encoder);
864 encoder = ob->decl_state->cgraph_node_encoder;
865 varpool_encoder = ob->decl_state->varpool_node_encoder;
867 /* Write out the nodes. We must first output a node and then its clones,
868 otherwise at a time reading back the node there would be nothing to clone
869 from. */
870 n_nodes = lto_cgraph_encoder_size (encoder);
871 for (i = 0; i < n_nodes; i++)
873 node = lto_cgraph_encoder_deref (encoder, i);
874 lto_output_node (ob, node, encoder, set, vset);
877 /* Go over the nodes in SET again to write edges. */
878 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
880 node = csi_node (csi);
881 output_outgoing_cgraph_edges (node->callees, ob, encoder);
882 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
885 lto_output_uleb128_stream (ob->main_stream, 0);
887 /* Emit toplevel asms.
888 When doing WPA we must output every asm just once. Since we do not partition asm
889 nodes at all, output them to first output. This is kind of hack, but should work
890 well. */
891 if (!asm_nodes_output)
893 asm_nodes_output = true;
894 for (can = cgraph_asm_nodes; can; can = can->next)
896 int len = TREE_STRING_LENGTH (can->asm_str);
897 lto_output_uleb128_stream (ob->main_stream, len);
898 for (i = 0; i < len; ++i)
899 lto_output_1_stream (ob->main_stream,
900 TREE_STRING_POINTER (can->asm_str)[i]);
904 lto_output_uleb128_stream (ob->main_stream, 0);
906 lto_destroy_simple_output_block (ob);
907 output_varpool (set, vset);
908 output_refs (set, vset, encoder, varpool_encoder);
911 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
912 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
913 NODE or to replace the values in it, for instance because the first
914 time we saw it, the function body was not available but now it
915 is. BP is a bitpack with all the bitflags for NODE read from the
916 stream. */
918 static void
919 input_overwrite_node (struct lto_file_decl_data *file_data,
920 struct cgraph_node *node,
921 enum LTO_cgraph_tags tag,
922 struct bitpack_d *bp)
924 node->aux = (void *) tag;
925 node->local.lto_file_data = file_data;
927 node->local.local = bp_unpack_value (bp, 1);
928 node->local.externally_visible = bp_unpack_value (bp, 1);
929 node->local.finalized = bp_unpack_value (bp, 1);
930 node->local.can_change_signature = bp_unpack_value (bp, 1);
931 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
932 node->needed = bp_unpack_value (bp, 1);
933 node->address_taken = bp_unpack_value (bp, 1);
934 node->abstract_and_needed = bp_unpack_value (bp, 1);
935 node->reachable_from_other_partition = bp_unpack_value (bp, 1);
936 node->lowered = bp_unpack_value (bp, 1);
937 node->analyzed = tag == LTO_cgraph_analyzed_node;
938 node->in_other_partition = bp_unpack_value (bp, 1);
939 if (node->in_other_partition
940 /* Avoid updating decl when we are seeing just inline clone.
941 When inlining function that has functions already inlined into it,
942 we produce clones of inline clones.
944 WPA partitioning might put each clone into different unit and
945 we might end up streaming inline clone from other partition
946 to support clone we are interested in. */
947 && (!node->clone_of
948 || node->clone_of->decl != node->decl))
950 DECL_EXTERNAL (node->decl) = 1;
951 TREE_STATIC (node->decl) = 0;
953 node->alias = bp_unpack_value (bp, 1);
954 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
955 node->only_called_at_startup = bp_unpack_value (bp, 1);
956 node->only_called_at_exit = bp_unpack_value (bp, 1);
957 node->thunk.thunk_p = bp_unpack_value (bp, 1);
958 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
959 LDPR_NUM_KNOWN);
962 /* Output the part of the cgraph in SET. */
964 static void
965 output_varpool (cgraph_node_set set, varpool_node_set vset)
967 struct lto_simple_output_block *ob = lto_create_simple_output_block (LTO_section_varpool);
968 lto_varpool_encoder_t varpool_encoder = ob->decl_state->varpool_node_encoder;
969 int len = lto_varpool_encoder_size (varpool_encoder), i;
971 lto_output_uleb128_stream (ob->main_stream, len);
973 /* Write out the nodes. We must first output a node and then its clones,
974 otherwise at a time reading back the node there would be nothing to clone
975 from. */
976 for (i = 0; i < len; i++)
978 lto_output_varpool_node (ob, lto_varpool_encoder_deref (varpool_encoder, i),
979 varpool_encoder,
980 set, vset);
983 lto_destroy_simple_output_block (ob);
986 /* Read a node from input_block IB. TAG is the node's tag just read.
987 Return the node read or overwriten. */
989 static struct cgraph_node *
990 input_node (struct lto_file_decl_data *file_data,
991 struct lto_input_block *ib,
992 enum LTO_cgraph_tags tag,
993 VEC(cgraph_node_ptr, heap) *nodes)
995 tree fn_decl;
996 struct cgraph_node *node;
997 struct bitpack_d bp;
998 unsigned decl_index;
999 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1000 unsigned long same_body_count = 0;
1001 int clone_ref;
1003 clone_ref = lto_input_sleb128 (ib);
1005 decl_index = lto_input_uleb128 (ib);
1006 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1008 if (clone_ref != LCC_NOT_FOUND)
1010 node = cgraph_clone_node (VEC_index (cgraph_node_ptr, nodes, clone_ref), fn_decl,
1011 0, CGRAPH_FREQ_BASE, false, NULL, false);
1013 else
1014 node = cgraph_get_create_node (fn_decl);
1016 node->count = lto_input_sleb128 (ib);
1017 node->count_materialization_scale = lto_input_sleb128 (ib);
1019 if (tag == LTO_cgraph_analyzed_node)
1020 ref = lto_input_sleb128 (ib);
1022 ref2 = lto_input_sleb128 (ib);
1024 /* Make sure that we have not read this node before. Nodes that
1025 have already been read will have their tag stored in the 'aux'
1026 field. Since built-in functions can be referenced in multiple
1027 functions, they are expected to be read more than once. */
1028 if (node->aux && !DECL_IS_BUILTIN (node->decl))
1029 internal_error ("bytecode stream: found multiple instances of cgraph "
1030 "node %d", node->uid);
1032 bp = lto_input_bitpack (ib);
1033 input_overwrite_node (file_data, node, tag, &bp);
1035 /* Store a reference for now, and fix up later to be a pointer. */
1036 node->global.inlined_to = (cgraph_node_ptr) (intptr_t) ref;
1038 /* Store a reference for now, and fix up later to be a pointer. */
1039 node->same_comdat_group = (cgraph_node_ptr) (intptr_t) ref2;
1041 if (node->thunk.thunk_p)
1043 int type = lto_input_uleb128 (ib);
1044 HOST_WIDE_INT fixed_offset = lto_input_uleb128 (ib);
1045 HOST_WIDE_INT virtual_value = lto_input_uleb128 (ib);
1046 tree real_alias;
1048 decl_index = lto_input_uleb128 (ib);
1049 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1050 node->thunk.fixed_offset = fixed_offset;
1051 node->thunk.this_adjusting = (type & 2);
1052 node->thunk.virtual_value = virtual_value;
1053 node->thunk.virtual_offset_p = (type & 4);
1054 node->thunk.alias = real_alias;
1057 same_body_count = lto_input_uleb128 (ib);
1058 while (same_body_count-- > 0)
1060 tree alias_decl, real_alias;
1061 struct cgraph_node *alias;
1063 decl_index = lto_input_uleb128 (ib);
1064 alias_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1065 decl_index = lto_input_uleb128 (ib);
1066 real_alias = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1067 alias = cgraph_same_body_alias (node, alias_decl, real_alias);
1068 gcc_assert (alias);
1069 alias->resolution = lto_input_enum (ib, ld_plugin_symbol_resolution,
1070 LDPR_NUM_KNOWN);
1072 return node;
1075 /* Read a node from input_block IB. TAG is the node's tag just read.
1076 Return the node read or overwriten. */
1078 static struct varpool_node *
1079 input_varpool_node (struct lto_file_decl_data *file_data,
1080 struct lto_input_block *ib)
1082 int decl_index;
1083 tree var_decl;
1084 struct varpool_node *node;
1085 struct bitpack_d bp;
1086 bool aliases_p;
1087 int count;
1088 int ref = LCC_NOT_FOUND;
1090 decl_index = lto_input_uleb128 (ib);
1091 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1092 node = varpool_node (var_decl);
1093 node->lto_file_data = file_data;
1095 bp = lto_input_bitpack (ib);
1096 node->externally_visible = bp_unpack_value (&bp, 1);
1097 node->force_output = bp_unpack_value (&bp, 1);
1098 node->finalized = bp_unpack_value (&bp, 1);
1099 node->alias = bp_unpack_value (&bp, 1);
1100 node->analyzed = node->finalized;
1101 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1102 node->in_other_partition = bp_unpack_value (&bp, 1);
1103 if (node->in_other_partition)
1105 DECL_EXTERNAL (node->decl) = 1;
1106 TREE_STATIC (node->decl) = 0;
1108 aliases_p = bp_unpack_value (&bp, 1);
1109 if (node->finalized)
1110 varpool_mark_needed_node (node);
1111 ref = lto_input_sleb128 (ib);
1112 /* Store a reference for now, and fix up later to be a pointer. */
1113 node->same_comdat_group = (struct varpool_node *) (intptr_t) ref;
1114 node->resolution = lto_input_enum (ib, ld_plugin_symbol_resolution,
1115 LDPR_NUM_KNOWN);
1116 if (aliases_p)
1118 count = lto_input_uleb128 (ib);
1119 for (; count > 0; count --)
1121 tree decl = lto_file_decl_data_get_var_decl (file_data,
1122 lto_input_uleb128 (ib));
1123 struct varpool_node *alias;
1124 alias = varpool_extra_name_alias (decl, var_decl);
1125 alias->resolution = lto_input_enum (ib, ld_plugin_symbol_resolution,
1126 LDPR_NUM_KNOWN);
1129 return node;
1132 /* Read a node from input_block IB. TAG is the node's tag just read.
1133 Return the node read or overwriten. */
1135 static void
1136 input_ref (struct lto_input_block *ib,
1137 struct cgraph_node *refering_node,
1138 struct varpool_node *refering_varpool_node,
1139 VEC(cgraph_node_ptr, heap) *nodes,
1140 VEC(varpool_node_ptr, heap) *varpool_nodes)
1142 struct cgraph_node *node = NULL;
1143 struct varpool_node *varpool_node = NULL;
1144 struct bitpack_d bp;
1145 enum ipa_ref_type type;
1146 enum ipa_ref_use use;
1148 bp = lto_input_bitpack (ib);
1149 type = (enum ipa_ref_type) bp_unpack_value (&bp, 1);
1150 use = (enum ipa_ref_use) bp_unpack_value (&bp, 2);
1151 if (type == IPA_REF_CGRAPH)
1152 node = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1153 else
1154 varpool_node = VEC_index (varpool_node_ptr, varpool_nodes, lto_input_sleb128 (ib));
1155 ipa_record_reference (refering_node, refering_varpool_node,
1156 node, varpool_node, use, NULL);
1159 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1160 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1161 edge being read is indirect (in the sense that it has
1162 indirect_unknown_callee set). */
1164 static void
1165 input_edge (struct lto_input_block *ib, VEC(cgraph_node_ptr, heap) *nodes,
1166 bool indirect)
1168 struct cgraph_node *caller, *callee;
1169 struct cgraph_edge *edge;
1170 unsigned int stmt_id;
1171 gcov_type count;
1172 int freq;
1173 cgraph_inline_failed_t inline_failed;
1174 struct bitpack_d bp;
1175 int ecf_flags = 0;
1177 caller = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1178 if (caller == NULL || caller->decl == NULL_TREE)
1179 internal_error ("bytecode stream: no caller found while reading edge");
1181 if (!indirect)
1183 callee = VEC_index (cgraph_node_ptr, nodes, lto_input_sleb128 (ib));
1184 if (callee == NULL || callee->decl == NULL_TREE)
1185 internal_error ("bytecode stream: no callee found while reading edge");
1187 else
1188 callee = NULL;
1190 count = (gcov_type) lto_input_sleb128 (ib);
1192 bp = lto_input_bitpack (ib);
1193 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_enum, CIF_N_REASONS);
1194 stmt_id = bp_unpack_var_len_unsigned (&bp);
1195 freq = (int) bp_unpack_var_len_unsigned (&bp);
1197 if (indirect)
1198 edge = cgraph_create_indirect_edge (caller, NULL, 0, count, freq);
1199 else
1200 edge = cgraph_create_edge (caller, callee, NULL, count, freq);
1202 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1203 edge->lto_stmt_uid = stmt_id;
1204 edge->inline_failed = inline_failed;
1205 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1206 edge->can_throw_external = bp_unpack_value (&bp, 1);
1207 if (indirect)
1209 if (bp_unpack_value (&bp, 1))
1210 ecf_flags |= ECF_CONST;
1211 if (bp_unpack_value (&bp, 1))
1212 ecf_flags |= ECF_PURE;
1213 if (bp_unpack_value (&bp, 1))
1214 ecf_flags |= ECF_NORETURN;
1215 if (bp_unpack_value (&bp, 1))
1216 ecf_flags |= ECF_MALLOC;
1217 if (bp_unpack_value (&bp, 1))
1218 ecf_flags |= ECF_NOTHROW;
1219 if (bp_unpack_value (&bp, 1))
1220 ecf_flags |= ECF_RETURNS_TWICE;
1221 edge->indirect_info->ecf_flags = ecf_flags;
1226 /* Read a cgraph from IB using the info in FILE_DATA. */
1228 static VEC(cgraph_node_ptr, heap) *
1229 input_cgraph_1 (struct lto_file_decl_data *file_data,
1230 struct lto_input_block *ib)
1232 enum LTO_cgraph_tags tag;
1233 VEC(cgraph_node_ptr, heap) *nodes = NULL;
1234 struct cgraph_node *node;
1235 unsigned i;
1236 unsigned HOST_WIDE_INT len;
1238 tag = lto_input_enum (ib, LTO_cgraph_tags, LTO_cgraph_last_tag);
1239 while (tag)
1241 if (tag == LTO_cgraph_edge)
1242 input_edge (ib, nodes, false);
1243 else if (tag == LTO_cgraph_indirect_edge)
1244 input_edge (ib, nodes, true);
1245 else
1247 node = input_node (file_data, ib, tag,nodes);
1248 if (node == NULL || node->decl == NULL_TREE)
1249 internal_error ("bytecode stream: found empty cgraph node");
1250 VEC_safe_push (cgraph_node_ptr, heap, nodes, node);
1251 lto_cgraph_encoder_encode (file_data->cgraph_node_encoder, node);
1254 tag = lto_input_enum (ib, LTO_cgraph_tags, LTO_cgraph_last_tag);
1257 /* Input toplevel asms. */
1258 len = lto_input_uleb128 (ib);
1259 while (len)
1261 char *str = (char *)xmalloc (len + 1);
1262 for (i = 0; i < len; ++i)
1263 str[i] = lto_input_1_unsigned (ib);
1264 cgraph_add_asm_node (build_string (len, str));
1265 free (str);
1267 len = lto_input_uleb128 (ib);
1269 /* AUX pointers should be all non-zero for nodes read from the stream. */
1270 #ifdef ENABLE_CHECKING
1271 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1272 gcc_assert (node->aux);
1273 #endif
1274 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1276 int ref = (int) (intptr_t) node->global.inlined_to;
1278 /* We share declaration of builtins, so we may read same node twice. */
1279 if (!node->aux)
1280 continue;
1281 node->aux = NULL;
1283 /* Fixup inlined_to from reference to pointer. */
1284 if (ref != LCC_NOT_FOUND)
1285 node->global.inlined_to = VEC_index (cgraph_node_ptr, nodes, ref);
1286 else
1287 node->global.inlined_to = NULL;
1289 ref = (int) (intptr_t) node->same_comdat_group;
1291 /* Fixup same_comdat_group from reference to pointer. */
1292 if (ref != LCC_NOT_FOUND)
1293 node->same_comdat_group = VEC_index (cgraph_node_ptr, nodes, ref);
1294 else
1295 node->same_comdat_group = NULL;
1297 FOR_EACH_VEC_ELT (cgraph_node_ptr, nodes, i, node)
1298 node->aux = (void *)1;
1299 return nodes;
1302 /* Read a varpool from IB using the info in FILE_DATA. */
1304 static VEC(varpool_node_ptr, heap) *
1305 input_varpool_1 (struct lto_file_decl_data *file_data,
1306 struct lto_input_block *ib)
1308 unsigned HOST_WIDE_INT len;
1309 VEC(varpool_node_ptr, heap) *varpool = NULL;
1310 int i;
1311 struct varpool_node *node;
1313 len = lto_input_uleb128 (ib);
1314 while (len)
1316 VEC_safe_push (varpool_node_ptr, heap, varpool,
1317 input_varpool_node (file_data, ib));
1318 len--;
1320 #ifdef ENABLE_CHECKING
1321 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1322 gcc_assert (!node->aux);
1323 #endif
1324 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1326 int ref = (int) (intptr_t) node->same_comdat_group;
1327 /* We share declaration of builtins, so we may read same node twice. */
1328 if (node->aux)
1329 continue;
1330 node->aux = (void *)1;
1332 /* Fixup same_comdat_group from reference to pointer. */
1333 if (ref != LCC_NOT_FOUND)
1334 node->same_comdat_group = VEC_index (varpool_node_ptr, varpool, ref);
1335 else
1336 node->same_comdat_group = NULL;
1338 FOR_EACH_VEC_ELT (varpool_node_ptr, varpool, i, node)
1339 node->aux = NULL;
1340 return varpool;
1343 /* Input ipa_refs. */
1345 static void
1346 input_refs (struct lto_input_block *ib,
1347 VEC(cgraph_node_ptr, heap) *nodes,
1348 VEC(varpool_node_ptr, heap) *varpool)
1350 int count;
1351 int idx;
1352 while (true)
1354 struct cgraph_node *node;
1355 count = lto_input_uleb128 (ib);
1356 if (!count)
1357 break;
1358 idx = lto_input_uleb128 (ib);
1359 node = VEC_index (cgraph_node_ptr, nodes, idx);
1360 while (count)
1362 input_ref (ib, node, NULL, nodes, varpool);
1363 count--;
1366 while (true)
1368 struct varpool_node *node;
1369 count = lto_input_uleb128 (ib);
1370 if (!count)
1371 break;
1372 node = VEC_index (varpool_node_ptr, varpool, lto_input_uleb128 (ib));
1373 while (count)
1375 input_ref (ib, NULL, node, nodes, varpool);
1376 count--;
1382 static struct gcov_ctr_summary lto_gcov_summary;
1384 /* Input profile_info from IB. */
1385 static void
1386 input_profile_summary (struct lto_input_block *ib,
1387 struct lto_file_decl_data *file_data)
1389 unsigned int runs = lto_input_uleb128 (ib);
1390 if (runs)
1392 file_data->profile_info.runs = runs;
1393 file_data->profile_info.sum_max = lto_input_uleb128 (ib);
1398 /* Rescale profile summaries to the same number of runs in the whole unit. */
1400 static void
1401 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1403 struct lto_file_decl_data *file_data;
1404 unsigned int j;
1405 gcov_unsigned_t max_runs = 0;
1406 struct cgraph_node *node;
1407 struct cgraph_edge *edge;
1409 /* Find unit with maximal number of runs. If we ever get serious about
1410 roundoff errors, we might also consider computing smallest common
1411 multiply. */
1412 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1413 if (max_runs < file_data->profile_info.runs)
1414 max_runs = file_data->profile_info.runs;
1416 if (!max_runs)
1417 return;
1419 /* Simple overflow check. We probably don't need to support that many train
1420 runs. Such a large value probably imply data corruption anyway. */
1421 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1423 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1424 INT_MAX / REG_BR_PROB_BASE);
1425 return;
1428 profile_info = &lto_gcov_summary;
1429 lto_gcov_summary.runs = max_runs;
1430 lto_gcov_summary.sum_max = 0;
1432 /* Rescale all units to the maximal number of runs.
1433 sum_max can not be easily merged, as we have no idea what files come from
1434 the same run. We do not use the info anyway, so leave it 0. */
1435 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1436 if (file_data->profile_info.runs)
1438 int scale = ((REG_BR_PROB_BASE * max_runs
1439 + file_data->profile_info.runs / 2)
1440 / file_data->profile_info.runs);
1441 lto_gcov_summary.sum_max = MAX (lto_gcov_summary.sum_max,
1442 (file_data->profile_info.sum_max
1443 * scale
1444 + REG_BR_PROB_BASE / 2)
1445 / REG_BR_PROB_BASE);
1448 /* Watch roundoff errors. */
1449 if (lto_gcov_summary.sum_max < max_runs)
1450 lto_gcov_summary.sum_max = max_runs;
1452 /* If merging already happent at WPA time, we are done. */
1453 if (flag_ltrans)
1454 return;
1456 /* Now compute count_materialization_scale of each node.
1457 During LTRANS we already have values of count_materialization_scale
1458 computed, so just update them. */
1459 for (node = cgraph_nodes; node; node = node->next)
1460 if (node->local.lto_file_data
1461 && node->local.lto_file_data->profile_info.runs)
1463 int scale;
1465 scale =
1466 ((node->count_materialization_scale * max_runs
1467 + node->local.lto_file_data->profile_info.runs / 2)
1468 / node->local.lto_file_data->profile_info.runs);
1469 node->count_materialization_scale = scale;
1470 if (scale < 0)
1471 fatal_error ("Profile information in %s corrupted",
1472 file_data->file_name);
1474 if (scale == REG_BR_PROB_BASE)
1475 continue;
1476 for (edge = node->callees; edge; edge = edge->next_callee)
1477 edge->count = ((edge->count * scale + REG_BR_PROB_BASE / 2)
1478 / REG_BR_PROB_BASE);
1479 node->count = ((node->count * scale + REG_BR_PROB_BASE / 2)
1480 / REG_BR_PROB_BASE);
1484 /* Input and merge the cgraph from each of the .o files passed to
1485 lto1. */
1487 void
1488 input_cgraph (void)
1490 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1491 struct lto_file_decl_data *file_data;
1492 unsigned int j = 0;
1493 struct cgraph_node *node;
1495 while ((file_data = file_data_vec[j++]))
1497 const char *data;
1498 size_t len;
1499 struct lto_input_block *ib;
1500 VEC(cgraph_node_ptr, heap) *nodes;
1501 VEC(varpool_node_ptr, heap) *varpool;
1503 ib = lto_create_simple_input_block (file_data, LTO_section_cgraph,
1504 &data, &len);
1505 if (!ib)
1506 fatal_error ("cannot find LTO cgraph in %s", file_data->file_name);
1507 input_profile_summary (ib, file_data);
1508 file_data->cgraph_node_encoder = lto_cgraph_encoder_new ();
1509 nodes = input_cgraph_1 (file_data, ib);
1510 lto_destroy_simple_input_block (file_data, LTO_section_cgraph,
1511 ib, data, len);
1513 ib = lto_create_simple_input_block (file_data, LTO_section_varpool,
1514 &data, &len);
1515 if (!ib)
1516 fatal_error ("cannot find LTO varpool in %s", file_data->file_name);
1517 varpool = input_varpool_1 (file_data, ib);
1518 lto_destroy_simple_input_block (file_data, LTO_section_varpool,
1519 ib, data, len);
1521 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1522 &data, &len);
1523 if (!ib)
1524 fatal_error("cannot find LTO section refs in %s", file_data->file_name);
1525 input_refs (ib, nodes, varpool);
1526 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1527 ib, data, len);
1528 if (flag_ltrans)
1529 input_cgraph_opt_summary (nodes);
1530 VEC_free (cgraph_node_ptr, heap, nodes);
1531 VEC_free (varpool_node_ptr, heap, varpool);
1534 merge_profile_summaries (file_data_vec);
1536 /* Clear out the aux field that was used to store enough state to
1537 tell which nodes should be overwritten. */
1538 for (node = cgraph_nodes; node; node = node->next)
1540 /* Some nodes may have been created by cgraph_node. This
1541 happens when the callgraph contains nested functions. If the
1542 node for the parent function was never emitted to the gimple
1543 file, cgraph_node will create a node for it when setting the
1544 context of the nested function. */
1545 if (node->local.lto_file_data)
1546 node->aux = NULL;
1550 /* True when we need optimization summary for NODE. */
1552 static int
1553 output_cgraph_opt_summary_p (struct cgraph_node *node, cgraph_node_set set)
1555 struct cgraph_edge *e;
1557 if (cgraph_node_in_set_p (node, set))
1559 for (e = node->callees; e; e = e->next_callee)
1560 if (e->indirect_info
1561 && e->indirect_info->thunk_delta != 0)
1562 return true;
1564 for (e = node->indirect_calls; e; e = e->next_callee)
1565 if (e->indirect_info->thunk_delta != 0)
1566 return true;
1569 return (node->clone_of
1570 && (node->clone.tree_map
1571 || node->clone.args_to_skip
1572 || node->clone.combined_args_to_skip));
1575 /* Output optimization summary for EDGE to OB. */
1576 static void
1577 output_edge_opt_summary (struct output_block *ob,
1578 struct cgraph_edge *edge)
1580 if (edge->indirect_info)
1581 lto_output_sleb128_stream (ob->main_stream,
1582 edge->indirect_info->thunk_delta);
1583 else
1584 lto_output_sleb128_stream (ob->main_stream, 0);
1587 /* Output optimization summary for NODE to OB. */
1589 static void
1590 output_node_opt_summary (struct output_block *ob,
1591 struct cgraph_node *node,
1592 cgraph_node_set set)
1594 unsigned int index;
1595 bitmap_iterator bi;
1596 struct ipa_replace_map *map;
1597 struct bitpack_d bp;
1598 int i;
1599 struct cgraph_edge *e;
1601 if (node->clone.args_to_skip)
1603 lto_output_uleb128_stream (ob->main_stream,
1604 bitmap_count_bits (node->clone.args_to_skip));
1605 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1606 lto_output_uleb128_stream (ob->main_stream, index);
1608 else
1609 lto_output_uleb128_stream (ob->main_stream, 0);
1610 if (node->clone.combined_args_to_skip)
1612 lto_output_uleb128_stream (ob->main_stream,
1613 bitmap_count_bits (node->clone.combined_args_to_skip));
1614 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1615 lto_output_uleb128_stream (ob->main_stream, index);
1617 else
1618 lto_output_uleb128_stream (ob->main_stream, 0);
1619 lto_output_uleb128_stream (ob->main_stream,
1620 VEC_length (ipa_replace_map_p, node->clone.tree_map));
1621 FOR_EACH_VEC_ELT (ipa_replace_map_p, node->clone.tree_map, i, map)
1623 int parm_num;
1624 tree parm;
1626 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm;
1627 parm = DECL_CHAIN (parm), parm_num++)
1628 if (map->old_tree == parm)
1629 break;
1630 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1631 mechanism to store function local declarations into summaries. */
1632 gcc_assert (parm);
1633 lto_output_uleb128_stream (ob->main_stream, parm_num);
1634 lto_output_tree (ob, map->new_tree, true);
1635 bp = bitpack_create (ob->main_stream);
1636 bp_pack_value (&bp, map->replace_p, 1);
1637 bp_pack_value (&bp, map->ref_p, 1);
1638 lto_output_bitpack (&bp);
1641 if (cgraph_node_in_set_p (node, set))
1643 for (e = node->callees; e; e = e->next_callee)
1644 output_edge_opt_summary (ob, e);
1645 for (e = node->indirect_calls; e; e = e->next_callee)
1646 output_edge_opt_summary (ob, e);
1650 /* Output optimization summaries stored in callgraph.
1651 At the moment it is the clone info structure. */
1653 static void
1654 output_cgraph_opt_summary (cgraph_node_set set)
1656 struct cgraph_node *node;
1657 int i, n_nodes;
1658 lto_cgraph_encoder_t encoder;
1659 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1660 unsigned count = 0;
1662 ob->cgraph_node = NULL;
1663 encoder = ob->decl_state->cgraph_node_encoder;
1664 n_nodes = lto_cgraph_encoder_size (encoder);
1665 for (i = 0; i < n_nodes; i++)
1666 if (output_cgraph_opt_summary_p (lto_cgraph_encoder_deref (encoder, i),
1667 set))
1668 count++;
1669 lto_output_uleb128_stream (ob->main_stream, count);
1670 for (i = 0; i < n_nodes; i++)
1672 node = lto_cgraph_encoder_deref (encoder, i);
1673 if (output_cgraph_opt_summary_p (node, set))
1675 lto_output_uleb128_stream (ob->main_stream, i);
1676 output_node_opt_summary (ob, node, set);
1679 produce_asm (ob, NULL);
1680 destroy_output_block (ob);
1683 /* Input optimisation summary of EDGE. */
1685 static void
1686 input_edge_opt_summary (struct cgraph_edge *edge,
1687 struct lto_input_block *ib_main)
1689 HOST_WIDE_INT thunk_delta;
1690 thunk_delta = lto_input_sleb128 (ib_main);
1691 if (thunk_delta != 0)
1693 gcc_assert (!edge->indirect_info);
1694 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1695 edge->indirect_info->thunk_delta = thunk_delta;
1699 /* Input optimisation summary of NODE. */
1701 static void
1702 input_node_opt_summary (struct cgraph_node *node,
1703 struct lto_input_block *ib_main,
1704 struct data_in *data_in)
1706 int i;
1707 int count;
1708 int bit;
1709 struct bitpack_d bp;
1710 struct cgraph_edge *e;
1712 count = lto_input_uleb128 (ib_main);
1713 if (count)
1714 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1715 for (i = 0; i < count; i++)
1717 bit = lto_input_uleb128 (ib_main);
1718 bitmap_set_bit (node->clone.args_to_skip, bit);
1720 count = lto_input_uleb128 (ib_main);
1721 if (count)
1722 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1723 for (i = 0; i < count; i++)
1725 bit = lto_input_uleb128 (ib_main);
1726 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1728 count = lto_input_uleb128 (ib_main);
1729 for (i = 0; i < count; i++)
1731 int parm_num;
1732 tree parm;
1733 struct ipa_replace_map *map = ggc_alloc_ipa_replace_map ();
1735 VEC_safe_push (ipa_replace_map_p, gc, node->clone.tree_map, map);
1736 for (parm_num = 0, parm = DECL_ARGUMENTS (node->decl); parm_num;
1737 parm = DECL_CHAIN (parm))
1738 parm_num --;
1739 map->parm_num = lto_input_uleb128 (ib_main);
1740 map->old_tree = NULL;
1741 map->new_tree = lto_input_tree (ib_main, data_in);
1742 bp = lto_input_bitpack (ib_main);
1743 map->replace_p = bp_unpack_value (&bp, 1);
1744 map->ref_p = bp_unpack_value (&bp, 1);
1746 for (e = node->callees; e; e = e->next_callee)
1747 input_edge_opt_summary (e, ib_main);
1748 for (e = node->indirect_calls; e; e = e->next_callee)
1749 input_edge_opt_summary (e, ib_main);
1752 /* Read section in file FILE_DATA of length LEN with data DATA. */
1754 static void
1755 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1756 const char *data, size_t len, VEC (cgraph_node_ptr,
1757 heap) * nodes)
1759 const struct lto_function_header *header =
1760 (const struct lto_function_header *) data;
1761 const int32_t cfg_offset = sizeof (struct lto_function_header);
1762 const int32_t main_offset = cfg_offset + header->cfg_size;
1763 const int32_t string_offset = main_offset + header->main_size;
1764 struct data_in *data_in;
1765 struct lto_input_block ib_main;
1766 unsigned int i;
1767 unsigned int count;
1769 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
1770 header->main_size);
1772 data_in =
1773 lto_data_in_create (file_data, (const char *) data + string_offset,
1774 header->string_size, NULL);
1775 count = lto_input_uleb128 (&ib_main);
1777 for (i = 0; i < count; i++)
1779 int ref = lto_input_uleb128 (&ib_main);
1780 input_node_opt_summary (VEC_index (cgraph_node_ptr, nodes, ref),
1781 &ib_main, data_in);
1783 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1784 len);
1785 lto_data_in_delete (data_in);
1788 /* Input optimization summary of cgraph. */
1790 static void
1791 input_cgraph_opt_summary (VEC (cgraph_node_ptr, heap) * nodes)
1793 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1794 struct lto_file_decl_data *file_data;
1795 unsigned int j = 0;
1797 while ((file_data = file_data_vec[j++]))
1799 size_t len;
1800 const char *data =
1801 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1802 &len);
1804 if (data)
1805 input_cgraph_opt_section (file_data, data, len, nodes);