1 /* Write and read the cgraph to the memory mapped representation of a
4 Copyright (C) 2009-2016 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
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
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/>. */
25 #include "coretypes.h"
31 #include "stringpool.h"
32 #include "tree-streamer.h"
34 #include "tree-pass.h"
37 #include "pass_manager.h"
38 #include "ipa-utils.h"
42 /* True when asm nodes has been output. */
43 bool asm_nodes_output = false;
45 static void output_cgraph_opt_summary (void);
46 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
48 /* Number of LDPR values known to GCC. */
49 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
51 /* All node orders are ofsetted by ORDER_BASE. */
52 static int order_base;
54 /* Cgraph streaming is organized as set of record whose type
55 is indicated by a tag. */
58 /* Must leave 0 for the stopper. */
60 /* Cgraph node without body available. */
61 LTO_symtab_unavail_node = 1,
62 /* Cgraph node with function body. */
63 LTO_symtab_analyzed_node,
66 LTO_symtab_indirect_edge,
71 /* Create a new symtab encoder.
72 if FOR_INPUT, the encoder allocate only datastructures needed
73 to read the symtab. */
76 lto_symtab_encoder_new (bool for_input)
78 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
81 encoder->map = new hash_map<symtab_node *, size_t>;
82 encoder->nodes.create (0);
87 /* Delete ENCODER and its components. */
90 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
92 encoder->nodes.release ();
99 /* Return the existing reference number of NODE in the symtab encoder in
100 output block OB. Assign a new reference if this is the first time
104 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
111 lto_encoder_entry entry = {node, false, false, false};
113 ref = encoder->nodes.length ();
114 encoder->nodes.safe_push (entry);
118 size_t *slot = encoder->map->get (node);
121 lto_encoder_entry entry = {node, false, false, false};
122 ref = encoder->nodes.length ();
124 encoder->map->put (node, ref + 1);
125 encoder->nodes.safe_push (entry);
133 /* Remove NODE from encoder. */
136 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
140 lto_encoder_entry last_node;
142 size_t *slot = encoder->map->get (node);
143 if (slot == NULL || !*slot)
147 gcc_checking_assert (encoder->nodes[index].node == node);
149 /* Remove from vector. We do this by swapping node with the last element
151 last_node = encoder->nodes.pop ();
152 if (last_node.node != node)
154 gcc_assert (encoder->map->put (last_node.node, index + 1));
156 /* Move the last element to the original spot of NODE. */
157 encoder->nodes[index] = last_node;
160 /* Remove element from hash table. */
161 encoder->map->remove (node);
166 /* Return TRUE if we should encode the body of NODE (if any). */
169 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
170 struct cgraph_node *node)
172 int index = lto_symtab_encoder_lookup (encoder, node);
173 return encoder->nodes[index].body;
176 /* Specify that we encode the body of NODE in this partition. */
179 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
180 struct cgraph_node *node)
182 int index = lto_symtab_encoder_encode (encoder, node);
183 gcc_checking_assert (encoder->nodes[index].node == node);
184 encoder->nodes[index].body = true;
187 /* Return TRUE if we should encode initializer of NODE (if any). */
190 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
193 int index = lto_symtab_encoder_lookup (encoder, node);
194 if (index == LCC_NOT_FOUND)
196 return encoder->nodes[index].initializer;
199 /* Specify that we should encode initializer of NODE (if any). */
202 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
205 int index = lto_symtab_encoder_lookup (encoder, node);
206 encoder->nodes[index].initializer = true;
209 /* Return TRUE if NODE is in this partition. */
212 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
215 int index = lto_symtab_encoder_lookup (encoder, node);
216 if (index == LCC_NOT_FOUND)
218 return encoder->nodes[index].in_partition;
221 /* Specify that NODE is in this partition. */
224 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
227 int index = lto_symtab_encoder_encode (encoder, node);
228 encoder->nodes[index].in_partition = true;
231 /* Output the cgraph EDGE to OB using ENCODER. */
234 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
235 lto_symtab_encoder_t encoder)
241 if (edge->indirect_unknown_callee)
242 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
243 LTO_symtab_indirect_edge);
245 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
248 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
249 gcc_assert (ref != LCC_NOT_FOUND);
250 streamer_write_hwi_stream (ob->main_stream, ref);
252 if (!edge->indirect_unknown_callee)
254 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
255 gcc_assert (ref != LCC_NOT_FOUND);
256 streamer_write_hwi_stream (ob->main_stream, ref);
259 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
261 bp = bitpack_create (ob->main_stream);
262 uid = (!gimple_has_body_p (edge->caller->decl)
263 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
264 bp_pack_enum (&bp, cgraph_inline_failed_t,
265 CIF_N_REASONS, edge->inline_failed);
266 bp_pack_var_len_unsigned (&bp, uid);
267 bp_pack_var_len_unsigned (&bp, edge->frequency);
268 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
269 bp_pack_value (&bp, edge->speculative, 1);
270 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
271 bp_pack_value (&bp, edge->can_throw_external, 1);
272 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
273 if (edge->indirect_unknown_callee)
275 int flags = edge->indirect_info->ecf_flags;
276 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
277 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
278 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
279 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
280 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
281 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
282 /* Flags that should not appear on indirect calls. */
283 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
289 streamer_write_bitpack (&bp);
290 if (edge->indirect_unknown_callee)
292 streamer_write_hwi_stream (ob->main_stream,
293 edge->indirect_info->common_target_id);
294 if (edge->indirect_info->common_target_id)
295 streamer_write_hwi_stream
296 (ob->main_stream, edge->indirect_info->common_target_probability);
300 /* Return if NODE contain references from other partitions. */
303 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
306 struct ipa_ref *ref = NULL;
308 for (i = 0; node->iterate_referring (i, ref); i++)
310 /* Ignore references from non-offloadable nodes while streaming NODE into
311 offload LTO section. */
312 if (!ref->referring->need_lto_streaming)
315 if (ref->referring->in_other_partition
316 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
322 /* Return true when node is reachable from other partition. */
325 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
327 struct cgraph_edge *e;
328 if (!node->definition)
330 if (node->global.inlined_to)
332 for (e = node->callers; e; e = e->next_caller)
334 /* Ignore references from non-offloadable nodes while streaming NODE into
335 offload LTO section. */
336 if (!e->caller->need_lto_streaming)
339 if (e->caller->in_other_partition
340 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
346 /* Return if NODE contain references from other partitions. */
349 referenced_from_this_partition_p (symtab_node *node,
350 lto_symtab_encoder_t encoder)
353 struct ipa_ref *ref = NULL;
355 for (i = 0; node->iterate_referring (i, ref); i++)
356 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
361 /* Return true when node is reachable from other partition. */
364 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
366 struct cgraph_edge *e;
367 for (e = node->callers; e; e = e->next_caller)
368 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
373 /* Output the cgraph NODE to OB. ENCODER is used to find the
374 reference number of NODE->inlined_to. SET is the set of nodes we
375 are writing to the current file. If NODE is not in SET, then NODE
376 is a boundary of a cgraph_node_set and we pretend NODE just has a
377 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
378 that have had their callgraph node written so far. This is used to
379 determine if NODE is a clone of a previously written node. */
382 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
383 lto_symtab_encoder_t encoder)
389 bool in_other_partition = false;
390 struct cgraph_node *clone_of, *ultimate_clone_of;
391 ipa_opt_pass_d *pass;
397 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
399 if (node->analyzed && (!boundary_p || node->alias || node->thunk.thunk_p))
400 tag = LTO_symtab_analyzed_node;
402 tag = LTO_symtab_unavail_node;
404 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
406 streamer_write_hwi_stream (ob->main_stream, node->order);
408 /* In WPA mode, we only output part of the call-graph. Also, we
409 fake cgraph node attributes. There are two cases that we care.
411 Boundary nodes: There are nodes that are not part of SET but are
412 called from within SET. We artificially make them look like
413 externally visible nodes with no function body.
415 Cherry-picked nodes: These are nodes we pulled from other
416 translation units into SET during IPA-inlining. We make them as
417 local static nodes to prevent clashes with other local statics. */
418 if (boundary_p && node->analyzed
419 && node->get_partitioning_class () == SYMBOL_PARTITION)
421 /* Inline clones can not be part of boundary.
422 gcc_assert (!node->global.inlined_to);
424 FIXME: At the moment they can be, when partition contains an inline
425 clone that is clone of inline clone from outside partition. We can
426 reshape the clone tree and make other tree to be the root, but it
427 needs a bit extra work and will be promplty done by cgraph_remove_node
428 after reading back. */
429 in_other_partition = 1;
432 clone_of = node->clone_of;
434 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
435 if (clone_of->prev_sibling_clone)
436 clone_of = clone_of->prev_sibling_clone;
438 clone_of = clone_of->clone_of;
440 /* See if body of the master function is output. If not, we are seeing only
441 an declaration and we do not need to pass down clone tree. */
442 ultimate_clone_of = clone_of;
443 while (ultimate_clone_of && ultimate_clone_of->clone_of)
444 ultimate_clone_of = ultimate_clone_of->clone_of;
446 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
449 if (tag == LTO_symtab_analyzed_node)
450 gcc_assert (clone_of || !node->clone_of);
452 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
454 streamer_write_hwi_stream (ob->main_stream, ref);
457 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
458 streamer_write_gcov_count_stream (ob->main_stream, node->count);
459 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
461 streamer_write_hwi_stream (ob->main_stream,
462 node->ipa_transforms_to_apply.length ());
463 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
464 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
466 if (tag == LTO_symtab_analyzed_node)
468 if (node->global.inlined_to)
470 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
471 gcc_assert (ref != LCC_NOT_FOUND);
476 streamer_write_hwi_stream (ob->main_stream, ref);
479 group = node->get_comdat_group ();
481 comdat = IDENTIFIER_POINTER (group);
484 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
488 if (node->same_comdat_group)
491 for (struct symtab_node *n = node->same_comdat_group;
492 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
493 ref = lto_symtab_encoder_lookup (encoder, n);
497 streamer_write_hwi_stream (ob->main_stream, ref);
500 section = node->get_section ();
504 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
506 bp = bitpack_create (ob->main_stream);
507 bp_pack_value (&bp, node->local.local, 1);
508 bp_pack_value (&bp, node->externally_visible, 1);
509 bp_pack_value (&bp, node->no_reorder, 1);
510 bp_pack_value (&bp, node->definition, 1);
511 bp_pack_value (&bp, node->local.versionable, 1);
512 bp_pack_value (&bp, node->local.can_change_signature, 1);
513 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
514 bp_pack_value (&bp, node->force_output, 1);
515 bp_pack_value (&bp, node->forced_by_abi, 1);
516 bp_pack_value (&bp, node->unique_name, 1);
517 bp_pack_value (&bp, node->body_removed, 1);
518 bp_pack_value (&bp, node->implicit_section, 1);
519 bp_pack_value (&bp, node->address_taken, 1);
520 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
521 && node->get_partitioning_class () == SYMBOL_PARTITION
522 && (reachable_from_other_partition_p (node, encoder)
523 || referenced_from_other_partition_p (node, encoder)), 1);
524 bp_pack_value (&bp, node->lowered, 1);
525 bp_pack_value (&bp, in_other_partition, 1);
526 bp_pack_value (&bp, node->alias, 1);
527 bp_pack_value (&bp, node->transparent_alias, 1);
528 bp_pack_value (&bp, node->weakref, 1);
529 bp_pack_value (&bp, node->frequency, 2);
530 bp_pack_value (&bp, node->only_called_at_startup, 1);
531 bp_pack_value (&bp, node->only_called_at_exit, 1);
532 bp_pack_value (&bp, node->tm_clone, 1);
533 bp_pack_value (&bp, node->calls_comdat_local, 1);
534 bp_pack_value (&bp, node->icf_merged, 1);
535 bp_pack_value (&bp, node->nonfreeing_fn, 1);
536 bp_pack_value (&bp, node->thunk.thunk_p, 1);
537 bp_pack_value (&bp, node->parallelized_function, 1);
538 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
539 LDPR_NUM_KNOWN, node->resolution);
540 bp_pack_value (&bp, node->instrumentation_clone, 1);
541 bp_pack_value (&bp, node->split_part, 1);
542 streamer_write_bitpack (&bp);
543 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
545 if (node->thunk.thunk_p)
547 streamer_write_uhwi_stream
549 1 + (node->thunk.this_adjusting != 0) * 2
550 + (node->thunk.virtual_offset_p != 0) * 4
551 + (node->thunk.add_pointer_bounds_args != 0) * 8);
552 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
553 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
555 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
556 if (DECL_STATIC_CONSTRUCTOR (node->decl))
557 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
558 if (DECL_STATIC_DESTRUCTOR (node->decl))
559 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
561 if (node->instrumentation_clone)
562 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
565 /* Output the varpool NODE to OB.
566 If NODE is not in SET, then NODE is a boundary. */
569 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
570 lto_symtab_encoder_t encoder)
572 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
573 bool encode_initializer_p
575 && lto_symtab_encoder_encode_initializer_p (encoder, node));
582 gcc_assert (!encode_initializer_p || node->definition);
583 gcc_assert (boundary_p || encode_initializer_p);
585 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
586 LTO_symtab_variable);
587 streamer_write_hwi_stream (ob->main_stream, node->order);
588 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
589 bp = bitpack_create (ob->main_stream);
590 bp_pack_value (&bp, node->externally_visible, 1);
591 bp_pack_value (&bp, node->no_reorder, 1);
592 bp_pack_value (&bp, node->force_output, 1);
593 bp_pack_value (&bp, node->forced_by_abi, 1);
594 bp_pack_value (&bp, node->unique_name, 1);
597 || (!encode_initializer_p && !node->alias && node->definition),
599 bp_pack_value (&bp, node->implicit_section, 1);
600 bp_pack_value (&bp, node->writeonly, 1);
601 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
603 bp_pack_value (&bp, node->alias, 1);
604 bp_pack_value (&bp, node->transparent_alias, 1);
605 bp_pack_value (&bp, node->weakref, 1);
606 bp_pack_value (&bp, node->analyzed && (!boundary_p || node->alias), 1);
607 gcc_assert (node->definition || !node->analyzed);
608 /* Constant pool initializers can be de-unified into individual ltrans units.
609 FIXME: Alternatively at -Os we may want to avoid generating for them the local
610 labels and share them across LTRANS partitions. */
611 if (node->get_partitioning_class () != SYMBOL_PARTITION)
613 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
614 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
618 bp_pack_value (&bp, node->definition
619 && referenced_from_other_partition_p (node, encoder), 1);
620 bp_pack_value (&bp, node->analyzed
621 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
622 /* in_other_partition. */
624 bp_pack_value (&bp, node->tls_model, 3);
625 bp_pack_value (&bp, node->used_by_single_function, 1);
626 bp_pack_value (&bp, node->dynamically_initialized, 1);
627 bp_pack_value (&bp, node->need_bounds_init, 1);
628 streamer_write_bitpack (&bp);
630 group = node->get_comdat_group ();
632 comdat = IDENTIFIER_POINTER (group);
635 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
639 if (node->same_comdat_group)
642 for (struct symtab_node *n = node->same_comdat_group;
643 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
644 ref = lto_symtab_encoder_lookup (encoder, n);
648 streamer_write_hwi_stream (ob->main_stream, ref);
651 section = node->get_section ();
654 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
656 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
657 LDPR_NUM_KNOWN, node->resolution);
660 /* Output the varpool NODE to OB.
661 If NODE is not in SET, then NODE is a boundary. */
664 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
665 lto_symtab_encoder_t encoder)
669 int uid = ref->lto_stmt_uid;
670 struct cgraph_node *node;
672 bp = bitpack_create (ob->main_stream);
673 bp_pack_value (&bp, ref->use, 3);
674 bp_pack_value (&bp, ref->speculative, 1);
675 streamer_write_bitpack (&bp);
676 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
677 gcc_assert (nref != LCC_NOT_FOUND);
678 streamer_write_hwi_stream (ob->main_stream, nref);
680 node = dyn_cast <cgraph_node *> (ref->referring);
684 uid = gimple_uid (ref->stmt) + 1;
685 streamer_write_hwi_stream (ob->main_stream, uid);
689 /* Stream out profile_summary to OB. */
692 output_profile_summary (struct lto_simple_output_block *ob)
699 /* We do not output num and run_max, they are not used by
700 GCC profile feedback and they are difficult to merge from multiple
702 gcc_assert (profile_info->runs);
703 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
704 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
706 /* sum_all is needed for computing the working set with the
708 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
710 /* Create and output a bitpack of non-zero histogram entries indices. */
711 bp = bitpack_create (ob->main_stream);
712 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
713 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
714 streamer_write_bitpack (&bp);
715 /* Now stream out only those non-zero entries. */
716 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
718 if (!profile_info->histogram[h_ix].num_counters)
720 streamer_write_gcov_count_stream (ob->main_stream,
721 profile_info->histogram[h_ix].num_counters);
722 streamer_write_gcov_count_stream (ob->main_stream,
723 profile_info->histogram[h_ix].min_value);
724 streamer_write_gcov_count_stream (ob->main_stream,
725 profile_info->histogram[h_ix].cum_value);
727 /* IPA-profile computes hot bb threshold based on cumulated
728 whole program profile. We need to stream it down to ltrans. */
730 streamer_write_gcov_count_stream (ob->main_stream,
731 get_hot_bb_threshold ());
734 streamer_write_uhwi_stream (ob->main_stream, 0);
737 /* Output all callees or indirect outgoing edges. EDGE must be the first such
741 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
742 struct lto_simple_output_block *ob,
743 lto_symtab_encoder_t encoder)
748 /* Output edges in backward direction, so the reconstructed callgraph match
749 and it is easy to associate call sites in the IPA pass summaries. */
750 while (edge->next_callee)
751 edge = edge->next_callee;
752 for (; edge; edge = edge->prev_callee)
753 lto_output_edge (ob, edge, encoder);
756 /* Output the part of the cgraph in SET. */
759 output_refs (lto_symtab_encoder_t encoder)
761 struct lto_simple_output_block *ob;
765 ob = lto_create_simple_output_block (LTO_section_refs);
767 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
769 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
771 /* IPA_REF_ALIAS and IPA_REF_CHKP references are always preserved
772 in the boundary. Alias node can't have other references and
773 can be always handled as if it's not in the boundary. */
774 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
776 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
777 /* Output IPA_REF_CHKP reference. */
779 && cnode->instrumented_version
780 && !cnode->instrumentation_clone)
782 for (int i = 0; node->iterate_reference (i, ref); i++)
783 if (ref->use == IPA_REF_CHKP)
785 if (lto_symtab_encoder_lookup (encoder, ref->referred)
788 int nref = lto_symtab_encoder_lookup (encoder, node);
789 streamer_write_gcov_count_stream (ob->main_stream, 1);
790 streamer_write_uhwi_stream (ob->main_stream, nref);
791 lto_output_ref (ob, ref, encoder);
799 count = node->ref_list.nreferences ();
802 streamer_write_gcov_count_stream (ob->main_stream, count);
803 streamer_write_uhwi_stream (ob->main_stream,
804 lto_symtab_encoder_lookup (encoder, node));
805 for (int i = 0; node->iterate_reference (i, ref); i++)
806 lto_output_ref (ob, ref, encoder);
810 streamer_write_uhwi_stream (ob->main_stream, 0);
812 lto_destroy_simple_output_block (ob);
815 /* Add NODE into encoder as well as nodes it is cloned from.
816 Do it in a way so clones appear first. */
819 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
823 add_node_to (encoder, node->clone_of, include_body);
824 else if (include_body)
825 lto_set_symtab_encoder_encode_body (encoder, node);
826 lto_symtab_encoder_encode (encoder, node);
829 /* Add all references in NODE to encoders. */
832 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
835 struct ipa_ref *ref = NULL;
836 for (i = 0; node->iterate_reference (i, ref); i++)
837 if (is_a <cgraph_node *> (ref->referred))
838 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
840 lto_symtab_encoder_encode (encoder, ref->referred);
843 /* Select what needs to be streamed out. In regular lto mode stream everything.
844 In offload lto mode stream only nodes marked as offloadable. */
846 select_what_to_stream (void)
848 struct symtab_node *snode;
849 FOR_EACH_SYMBOL (snode)
850 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
853 /* Find all symbols we want to stream into given partition and insert them
856 The function actually replaces IN_ENCODER by new one. The reason is that
857 streaming code needs clone's origin to be streamed before clone. This
858 means that we need to insert the nodes in specific order. This order is
859 ignored by the partitioning logic earlier. */
862 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
864 struct cgraph_edge *edge;
866 lto_symtab_encoder_t encoder;
867 lto_symtab_encoder_iterator lsei;
868 hash_set<void *> reachable_call_targets;
870 encoder = lto_symtab_encoder_new (false);
872 /* Go over all entries in the IN_ENCODER and duplicate them to
873 ENCODER. At the same time insert masters of clones so
874 every master appears before clone. */
875 for (lsei = lsei_start_function_in_partition (in_encoder);
876 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
878 struct cgraph_node *node = lsei_cgraph_node (lsei);
879 if (!node->need_lto_streaming)
881 add_node_to (encoder, node, true);
882 lto_set_symtab_encoder_in_partition (encoder, node);
883 create_references (encoder, node);
885 for (lsei = lsei_start_variable_in_partition (in_encoder);
886 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
888 varpool_node *vnode = lsei_varpool_node (lsei);
890 if (!vnode->need_lto_streaming)
892 lto_set_symtab_encoder_in_partition (encoder, vnode);
893 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
894 create_references (encoder, vnode);
896 /* Pickle in also the initializer of all referenced readonly variables
897 to help folding. Constant pool variables are not shared, so we must
899 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
901 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
902 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
904 if (!lto_symtab_encoder_encode_initializer_p (encoder,
906 && (((vnode->ctor_useable_for_folding_p ()
907 && (!DECL_VIRTUAL_P (vnode->decl)
909 || flag_ltrans_devirtualize))
910 || POINTER_BOUNDS_P (vnode->decl))))
912 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
913 create_references (encoder, vnode);
918 /* Go over all the nodes again to include callees that are not in
920 for (lsei = lsei_start_function_in_partition (encoder);
921 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
923 struct cgraph_node *node = lsei_cgraph_node (lsei);
924 for (edge = node->callees; edge; edge = edge->next_callee)
926 struct cgraph_node *callee = edge->callee;
927 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
929 /* We should have moved all the inlines. */
930 gcc_assert (!callee->global.inlined_to);
931 add_node_to (encoder, callee, false);
934 /* Add all possible targets for late devirtualization. */
935 if (flag_ltrans_devirtualize || !flag_wpa)
936 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
937 if (edge->indirect_info->polymorphic)
942 vec <cgraph_node *>targets
943 = possible_polymorphic_call_targets
944 (edge, &final, &cache_token);
945 if (!reachable_call_targets.add (cache_token))
947 for (i = 0; i < targets.length (); i++)
949 struct cgraph_node *callee = targets[i];
951 /* Adding an external declarations into the unit serves
952 no purpose and just increases its boundary. */
953 if (callee->definition
954 && !lto_symtab_encoder_in_partition_p
957 gcc_assert (!callee->global.inlined_to);
958 add_node_to (encoder, callee, false);
964 /* Be sure to also insert alias targert and thunk callees. These needs
965 to stay to aid local calling conventions. */
966 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
968 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
969 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
971 if (node->alias && node->analyzed)
972 create_references (encoder, node);
974 && cnode->thunk.thunk_p)
975 add_node_to (encoder, cnode->callees->callee, false);
976 while (node->transparent_alias && node->analyzed)
978 node = node->get_alias_target ();
979 if (is_a <cgraph_node *> (node))
980 add_node_to (encoder, dyn_cast <cgraph_node *> (node),
983 lto_symtab_encoder_encode (encoder, node);
986 lto_symtab_encoder_delete (in_encoder);
990 /* Output the part of the symtab in SET and VSET. */
995 struct cgraph_node *node;
996 struct lto_simple_output_block *ob;
998 lto_symtab_encoder_t encoder;
1001 output_cgraph_opt_summary ();
1003 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
1005 output_profile_summary (ob);
1007 /* An encoder for cgraph nodes should have been created by
1008 ipa_write_summaries_1. */
1009 gcc_assert (ob->decl_state->symtab_node_encoder);
1010 encoder = ob->decl_state->symtab_node_encoder;
1012 /* Write out the nodes. We must first output a node and then its clones,
1013 otherwise at a time reading back the node there would be nothing to clone
1015 n_nodes = lto_symtab_encoder_size (encoder);
1016 for (i = 0; i < n_nodes; i++)
1018 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1019 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1020 lto_output_node (ob, cnode, encoder);
1022 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1025 /* Go over the nodes in SET again to write edges. */
1026 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1028 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1030 && (node->thunk.thunk_p
1031 || lto_symtab_encoder_in_partition_p (encoder, node)))
1033 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1034 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1038 streamer_write_uhwi_stream (ob->main_stream, 0);
1040 lto_destroy_simple_output_block (ob);
1042 /* Emit toplevel asms.
1043 When doing WPA we must output every asm just once. Since we do not partition asm
1044 nodes at all, output them to first output. This is kind of hack, but should work
1046 if (!asm_nodes_output)
1048 asm_nodes_output = true;
1049 lto_output_toplevel_asms ();
1052 output_refs (encoder);
1055 /* Return identifier encoded in IB as a plain string. */
1058 read_identifier (struct lto_input_block *ib)
1060 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1063 if (ib->data[ib->p + len])
1064 lto_section_overrun (ib);
1070 id = get_identifier (ib->data + ib->p);
1075 /* Return string encoded in IB, NULL if string is empty. */
1078 read_string (struct lto_input_block *ib)
1080 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1083 if (ib->data[ib->p + len])
1084 lto_section_overrun (ib);
1090 str = ib->data + ib->p;
1095 /* Output function/variable tables that will allow libgomp to look up offload
1097 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1098 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1099 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1102 output_offload_tables (void)
1104 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1107 struct lto_simple_output_block *ob
1108 = lto_create_simple_output_block (LTO_section_offload_table);
1110 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1112 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1113 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1114 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1115 (*offload_funcs)[i]);
1118 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1120 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1121 LTO_symtab_last_tag, LTO_symtab_variable);
1122 lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1123 (*offload_vars)[i]);
1126 streamer_write_uhwi_stream (ob->main_stream, 0);
1127 lto_destroy_simple_output_block (ob);
1129 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1130 streamed to one partition only. That's why we free offload_funcs and
1131 offload_vars after the first call of output_offload_tables. */
1134 vec_free (offload_funcs);
1135 vec_free (offload_vars);
1139 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1140 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1141 NODE or to replace the values in it, for instance because the first
1142 time we saw it, the function body was not available but now it
1143 is. BP is a bitpack with all the bitflags for NODE read from the
1147 input_overwrite_node (struct lto_file_decl_data *file_data,
1148 struct cgraph_node *node,
1149 enum LTO_symtab_tags tag,
1150 struct bitpack_d *bp)
1152 node->aux = (void *) tag;
1153 node->lto_file_data = file_data;
1155 node->local.local = bp_unpack_value (bp, 1);
1156 node->externally_visible = bp_unpack_value (bp, 1);
1157 node->no_reorder = bp_unpack_value (bp, 1);
1158 node->definition = bp_unpack_value (bp, 1);
1159 node->local.versionable = bp_unpack_value (bp, 1);
1160 node->local.can_change_signature = bp_unpack_value (bp, 1);
1161 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1162 node->force_output = bp_unpack_value (bp, 1);
1163 node->forced_by_abi = bp_unpack_value (bp, 1);
1164 node->unique_name = bp_unpack_value (bp, 1);
1165 node->body_removed = bp_unpack_value (bp, 1);
1166 node->implicit_section = bp_unpack_value (bp, 1);
1167 node->address_taken = bp_unpack_value (bp, 1);
1168 node->used_from_other_partition = bp_unpack_value (bp, 1);
1169 node->lowered = bp_unpack_value (bp, 1);
1170 node->analyzed = tag == LTO_symtab_analyzed_node;
1171 node->in_other_partition = bp_unpack_value (bp, 1);
1172 if (node->in_other_partition
1173 /* Avoid updating decl when we are seeing just inline clone.
1174 When inlining function that has functions already inlined into it,
1175 we produce clones of inline clones.
1177 WPA partitioning might put each clone into different unit and
1178 we might end up streaming inline clone from other partition
1179 to support clone we are interested in. */
1181 || node->clone_of->decl != node->decl))
1183 DECL_EXTERNAL (node->decl) = 1;
1184 TREE_STATIC (node->decl) = 0;
1186 node->alias = bp_unpack_value (bp, 1);
1187 node->transparent_alias = bp_unpack_value (bp, 1);
1188 node->weakref = bp_unpack_value (bp, 1);
1189 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1190 node->only_called_at_startup = bp_unpack_value (bp, 1);
1191 node->only_called_at_exit = bp_unpack_value (bp, 1);
1192 node->tm_clone = bp_unpack_value (bp, 1);
1193 node->calls_comdat_local = bp_unpack_value (bp, 1);
1194 node->icf_merged = bp_unpack_value (bp, 1);
1195 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1196 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1197 node->parallelized_function = bp_unpack_value (bp, 1);
1198 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1200 node->instrumentation_clone = bp_unpack_value (bp, 1);
1201 node->split_part = bp_unpack_value (bp, 1);
1202 gcc_assert (flag_ltrans
1203 || (!node->in_other_partition
1204 && !node->used_from_other_partition));
1207 /* Return string alias is alias of. */
1210 get_alias_symbol (tree decl)
1212 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1213 return get_identifier (TREE_STRING_POINTER
1214 (TREE_VALUE (TREE_VALUE (alias))));
1217 /* Read a node from input_block IB. TAG is the node's tag just read.
1218 Return the node read or overwriten. */
1220 static struct cgraph_node *
1221 input_node (struct lto_file_decl_data *file_data,
1222 struct lto_input_block *ib,
1223 enum LTO_symtab_tags tag,
1224 vec<symtab_node *> nodes)
1226 gcc::pass_manager *passes = g->get_passes ();
1228 struct cgraph_node *node;
1229 struct bitpack_d bp;
1230 unsigned decl_index;
1231 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1236 const char *section;
1237 order = streamer_read_hwi (ib) + order_base;
1238 clone_ref = streamer_read_hwi (ib);
1240 decl_index = streamer_read_uhwi (ib);
1241 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1243 if (clone_ref != LCC_NOT_FOUND)
1245 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1246 0, CGRAPH_FREQ_BASE, false,
1247 vNULL, false, NULL, NULL);
1251 /* Declaration of functions can be already merged with a declaration
1252 from other input file. We keep cgraph unmerged until after streaming
1253 of ipa passes is done. Alays forcingly create a fresh node. */
1254 node = symtab->create_empty ();
1255 node->decl = fn_decl;
1256 node->register_symbol ();
1259 node->order = order;
1260 if (order >= symtab->order)
1261 symtab->order = order + 1;
1263 node->count = streamer_read_gcov_count (ib);
1264 node->count_materialization_scale = streamer_read_hwi (ib);
1266 count = streamer_read_hwi (ib);
1267 node->ipa_transforms_to_apply = vNULL;
1268 for (i = 0; i < count; i++)
1271 int pid = streamer_read_hwi (ib);
1273 gcc_assert (pid < passes->passes_by_id_size);
1274 pass = passes->passes_by_id[pid];
1275 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1278 if (tag == LTO_symtab_analyzed_node)
1279 ref = streamer_read_hwi (ib);
1281 group = read_identifier (ib);
1283 ref2 = streamer_read_hwi (ib);
1285 /* Make sure that we have not read this node before. Nodes that
1286 have already been read will have their tag stored in the 'aux'
1287 field. Since built-in functions can be referenced in multiple
1288 functions, they are expected to be read more than once. */
1289 if (node->aux && !DECL_BUILT_IN (node->decl))
1290 internal_error ("bytecode stream: found multiple instances of cgraph "
1291 "node with uid %d", node->uid);
1293 node->tp_first_run = streamer_read_uhwi (ib);
1295 bp = streamer_read_bitpack (ib);
1297 input_overwrite_node (file_data, node, tag, &bp);
1299 /* Store a reference for now, and fix up later to be a pointer. */
1300 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1304 node->set_comdat_group (group);
1305 /* Store a reference for now, and fix up later to be a pointer. */
1306 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1309 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1310 section = read_string (ib);
1312 node->set_section_for_node (section);
1314 if (node->thunk.thunk_p)
1316 int type = streamer_read_uhwi (ib);
1317 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1318 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1320 node->thunk.fixed_offset = fixed_offset;
1321 node->thunk.this_adjusting = (type & 2);
1322 node->thunk.virtual_value = virtual_value;
1323 node->thunk.virtual_offset_p = (type & 4);
1324 node->thunk.add_pointer_bounds_args = (type & 8);
1326 if (node->alias && !node->analyzed && node->weakref)
1327 node->alias_target = get_alias_symbol (node->decl);
1328 node->profile_id = streamer_read_hwi (ib);
1329 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1330 node->set_init_priority (streamer_read_hwi (ib));
1331 if (DECL_STATIC_DESTRUCTOR (node->decl))
1332 node->set_fini_priority (streamer_read_hwi (ib));
1334 if (node->instrumentation_clone)
1336 decl_index = streamer_read_uhwi (ib);
1337 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1338 node->orig_decl = fn_decl;
1344 /* Read a node from input_block IB. TAG is the node's tag just read.
1345 Return the node read or overwriten. */
1347 static varpool_node *
1348 input_varpool_node (struct lto_file_decl_data *file_data,
1349 struct lto_input_block *ib)
1354 struct bitpack_d bp;
1355 int ref = LCC_NOT_FOUND;
1358 const char *section;
1360 order = streamer_read_hwi (ib) + order_base;
1361 decl_index = streamer_read_uhwi (ib);
1362 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1364 /* Declaration of functions can be already merged with a declaration
1365 from other input file. We keep cgraph unmerged until after streaming
1366 of ipa passes is done. Alays forcingly create a fresh node. */
1367 node = varpool_node::create_empty ();
1368 node->decl = var_decl;
1369 node->register_symbol ();
1371 node->order = order;
1372 if (order >= symtab->order)
1373 symtab->order = order + 1;
1374 node->lto_file_data = file_data;
1376 bp = streamer_read_bitpack (ib);
1377 node->externally_visible = bp_unpack_value (&bp, 1);
1378 node->no_reorder = bp_unpack_value (&bp, 1);
1379 node->force_output = bp_unpack_value (&bp, 1);
1380 node->forced_by_abi = bp_unpack_value (&bp, 1);
1381 node->unique_name = bp_unpack_value (&bp, 1);
1382 node->body_removed = bp_unpack_value (&bp, 1);
1383 node->implicit_section = bp_unpack_value (&bp, 1);
1384 node->writeonly = bp_unpack_value (&bp, 1);
1385 node->definition = bp_unpack_value (&bp, 1);
1386 node->alias = bp_unpack_value (&bp, 1);
1387 node->transparent_alias = bp_unpack_value (&bp, 1);
1388 node->weakref = bp_unpack_value (&bp, 1);
1389 node->analyzed = bp_unpack_value (&bp, 1);
1390 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1391 node->in_other_partition = bp_unpack_value (&bp, 1);
1392 if (node->in_other_partition)
1394 DECL_EXTERNAL (node->decl) = 1;
1395 TREE_STATIC (node->decl) = 0;
1397 if (node->alias && !node->analyzed && node->weakref)
1398 node->alias_target = get_alias_symbol (node->decl);
1399 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1400 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1401 node->dynamically_initialized = bp_unpack_value (&bp, 1);
1402 node->need_bounds_init = bp_unpack_value (&bp, 1);
1403 group = read_identifier (ib);
1406 node->set_comdat_group (group);
1407 ref = streamer_read_hwi (ib);
1408 /* Store a reference for now, and fix up later to be a pointer. */
1409 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1412 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1413 section = read_string (ib);
1415 node->set_section_for_node (section);
1416 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1418 gcc_assert (flag_ltrans
1419 || (!node->in_other_partition
1420 && !node->used_from_other_partition));
1425 /* Read a node from input_block IB. TAG is the node's tag just read.
1426 Return the node read or overwriten. */
1429 input_ref (struct lto_input_block *ib,
1430 symtab_node *referring_node,
1431 vec<symtab_node *> nodes)
1433 symtab_node *node = NULL;
1434 struct bitpack_d bp;
1435 enum ipa_ref_use use;
1437 struct ipa_ref *ref;
1439 bp = streamer_read_bitpack (ib);
1440 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1441 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1442 node = nodes[streamer_read_hwi (ib)];
1443 ref = referring_node->create_reference (node, use);
1444 ref->speculative = speculative;
1445 if (is_a <cgraph_node *> (referring_node))
1446 ref->lto_stmt_uid = streamer_read_hwi (ib);
1449 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1450 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1451 edge being read is indirect (in the sense that it has
1452 indirect_unknown_callee set). */
1455 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1458 struct cgraph_node *caller, *callee;
1459 struct cgraph_edge *edge;
1460 unsigned int stmt_id;
1463 cgraph_inline_failed_t inline_failed;
1464 struct bitpack_d bp;
1467 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1468 if (caller == NULL || caller->decl == NULL_TREE)
1469 internal_error ("bytecode stream: no caller found while reading edge");
1473 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1474 if (callee == NULL || callee->decl == NULL_TREE)
1475 internal_error ("bytecode stream: no callee found while reading edge");
1480 count = streamer_read_gcov_count (ib);
1482 bp = streamer_read_bitpack (ib);
1483 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1484 stmt_id = bp_unpack_var_len_unsigned (&bp);
1485 freq = (int) bp_unpack_var_len_unsigned (&bp);
1488 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1490 edge = caller->create_edge (callee, NULL, count, freq);
1492 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1493 edge->speculative = bp_unpack_value (&bp, 1);
1494 edge->lto_stmt_uid = stmt_id;
1495 edge->inline_failed = inline_failed;
1496 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1497 edge->can_throw_external = bp_unpack_value (&bp, 1);
1498 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1501 if (bp_unpack_value (&bp, 1))
1502 ecf_flags |= ECF_CONST;
1503 if (bp_unpack_value (&bp, 1))
1504 ecf_flags |= ECF_PURE;
1505 if (bp_unpack_value (&bp, 1))
1506 ecf_flags |= ECF_NORETURN;
1507 if (bp_unpack_value (&bp, 1))
1508 ecf_flags |= ECF_MALLOC;
1509 if (bp_unpack_value (&bp, 1))
1510 ecf_flags |= ECF_NOTHROW;
1511 if (bp_unpack_value (&bp, 1))
1512 ecf_flags |= ECF_RETURNS_TWICE;
1513 edge->indirect_info->ecf_flags = ecf_flags;
1514 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1515 if (edge->indirect_info->common_target_id)
1516 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1521 /* Read a cgraph from IB using the info in FILE_DATA. */
1523 static vec<symtab_node *>
1524 input_cgraph_1 (struct lto_file_decl_data *file_data,
1525 struct lto_input_block *ib)
1527 enum LTO_symtab_tags tag;
1528 vec<symtab_node *> nodes = vNULL;
1532 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1533 order_base = symtab->order;
1536 if (tag == LTO_symtab_edge)
1537 input_edge (ib, nodes, false);
1538 else if (tag == LTO_symtab_indirect_edge)
1539 input_edge (ib, nodes, true);
1540 else if (tag == LTO_symtab_variable)
1542 node = input_varpool_node (file_data, ib);
1543 nodes.safe_push (node);
1544 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1548 node = input_node (file_data, ib, tag, nodes);
1549 if (node == NULL || node->decl == NULL_TREE)
1550 internal_error ("bytecode stream: found empty cgraph node");
1551 nodes.safe_push (node);
1552 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1555 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1558 lto_input_toplevel_asms (file_data, order_base);
1560 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1563 FOR_EACH_VEC_ELT (nodes, i, node)
1564 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1566 FOR_EACH_VEC_ELT (nodes, i, node)
1569 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1571 ref = (int) (intptr_t) cnode->global.inlined_to;
1573 /* We share declaration of builtins, so we may read same node twice. */
1578 /* Fixup inlined_to from reference to pointer. */
1579 if (ref != LCC_NOT_FOUND)
1580 dyn_cast<cgraph_node *> (node)->global.inlined_to
1581 = dyn_cast<cgraph_node *> (nodes[ref]);
1583 cnode->global.inlined_to = NULL;
1585 /* Compute instrumented_version. */
1586 if (cnode->instrumentation_clone)
1588 gcc_assert (cnode->orig_decl);
1590 cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1591 if (cnode->instrumented_version)
1593 /* We may have multiple nodes for a single function which
1594 will be merged later. To have a proper merge we need
1595 to keep instrumentation_version reference between nodes
1596 consistent: each instrumented_version reference should
1597 have proper reverse reference. Thus don't break existing
1598 instrumented_version reference if it already exists. */
1599 if (cnode->instrumented_version->instrumented_version)
1600 cnode->instrumented_version = NULL;
1602 cnode->instrumented_version->instrumented_version = cnode;
1605 /* Restore decl names reference except for wrapper functions. */
1606 if (!chkp_wrap_function (cnode->orig_decl))
1608 tree name = DECL_ASSEMBLER_NAME (cnode->decl);
1609 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
1610 TREE_CHAIN (name) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1615 ref = (int) (intptr_t) node->same_comdat_group;
1617 /* Fixup same_comdat_group from reference to pointer. */
1618 if (ref != LCC_NOT_FOUND)
1619 node->same_comdat_group = nodes[ref];
1621 node->same_comdat_group = NULL;
1623 FOR_EACH_VEC_ELT (nodes, i, node)
1624 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1628 /* Input ipa_refs. */
1631 input_refs (struct lto_input_block *ib,
1632 vec<symtab_node *> nodes)
1639 count = streamer_read_uhwi (ib);
1642 idx = streamer_read_uhwi (ib);
1646 input_ref (ib, node, nodes);
1653 static struct gcov_ctr_summary lto_gcov_summary;
1655 /* Input profile_info from IB. */
1657 input_profile_summary (struct lto_input_block *ib,
1658 struct lto_file_decl_data *file_data)
1661 struct bitpack_d bp;
1662 unsigned int runs = streamer_read_uhwi (ib);
1665 file_data->profile_info.runs = runs;
1666 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1667 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1669 memset (file_data->profile_info.histogram, 0,
1670 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1671 /* Input the bitpack of non-zero histogram indices. */
1672 bp = streamer_read_bitpack (ib);
1673 /* Read in and unpack the full bitpack, flagging non-zero
1674 histogram entries by setting the num_counters non-zero. */
1675 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1677 file_data->profile_info.histogram[h_ix].num_counters
1678 = bp_unpack_value (&bp, 1);
1680 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1682 if (!file_data->profile_info.histogram[h_ix].num_counters)
1685 file_data->profile_info.histogram[h_ix].num_counters
1686 = streamer_read_gcov_count (ib);
1687 file_data->profile_info.histogram[h_ix].min_value
1688 = streamer_read_gcov_count (ib);
1689 file_data->profile_info.histogram[h_ix].cum_value
1690 = streamer_read_gcov_count (ib);
1692 /* IPA-profile computes hot bb threshold based on cumulated
1693 whole program profile. We need to stream it down to ltrans. */
1695 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1700 /* Rescale profile summaries to the same number of runs in the whole unit. */
1703 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1705 struct lto_file_decl_data *file_data;
1706 unsigned int j, h_ix;
1707 gcov_unsigned_t max_runs = 0;
1708 struct cgraph_node *node;
1709 struct cgraph_edge *edge;
1710 gcov_type saved_sum_all = 0;
1711 gcov_ctr_summary *saved_profile_info = 0;
1712 int saved_scale = 0;
1714 /* Find unit with maximal number of runs. If we ever get serious about
1715 roundoff errors, we might also consider computing smallest common
1717 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1718 if (max_runs < file_data->profile_info.runs)
1719 max_runs = file_data->profile_info.runs;
1724 /* Simple overflow check. We probably don't need to support that many train
1725 runs. Such a large value probably imply data corruption anyway. */
1726 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1728 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1729 INT_MAX / REG_BR_PROB_BASE);
1733 profile_info = <o_gcov_summary;
1734 lto_gcov_summary.runs = max_runs;
1735 lto_gcov_summary.sum_max = 0;
1736 memset (lto_gcov_summary.histogram, 0,
1737 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1739 /* Rescale all units to the maximal number of runs.
1740 sum_max can not be easily merged, as we have no idea what files come from
1741 the same run. We do not use the info anyway, so leave it 0. */
1742 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1743 if (file_data->profile_info.runs)
1745 int scale = GCOV_COMPUTE_SCALE (max_runs,
1746 file_data->profile_info.runs);
1747 lto_gcov_summary.sum_max
1748 = MAX (lto_gcov_summary.sum_max,
1749 apply_scale (file_data->profile_info.sum_max, scale));
1750 lto_gcov_summary.sum_all
1751 = MAX (lto_gcov_summary.sum_all,
1752 apply_scale (file_data->profile_info.sum_all, scale));
1753 /* Save a pointer to the profile_info with the largest
1754 scaled sum_all and the scale for use in merging the
1756 if (!saved_profile_info
1757 || lto_gcov_summary.sum_all > saved_sum_all)
1759 saved_profile_info = &file_data->profile_info;
1760 saved_sum_all = lto_gcov_summary.sum_all;
1761 saved_scale = scale;
1765 gcc_assert (saved_profile_info);
1767 /* Scale up the histogram from the profile that had the largest
1768 scaled sum_all above. */
1769 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1771 /* Scale up the min value as we did the corresponding sum_all
1772 above. Use that to find the new histogram index. */
1773 gcov_type scaled_min
1774 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1776 /* The new index may be shared with another scaled histogram entry,
1777 so we need to account for a non-zero histogram entry at new_ix. */
1778 unsigned new_ix = gcov_histo_index (scaled_min);
1779 lto_gcov_summary.histogram[new_ix].min_value
1780 = (lto_gcov_summary.histogram[new_ix].num_counters
1781 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1783 /* Some of the scaled counter values would ostensibly need to be placed
1784 into different (larger) histogram buckets, but we keep things simple
1785 here and place the scaled cumulative counter value in the bucket
1786 corresponding to the scaled minimum counter value. */
1787 lto_gcov_summary.histogram[new_ix].cum_value
1788 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1790 lto_gcov_summary.histogram[new_ix].num_counters
1791 += saved_profile_info->histogram[h_ix].num_counters;
1794 /* Watch roundoff errors. */
1795 if (lto_gcov_summary.sum_max < max_runs)
1796 lto_gcov_summary.sum_max = max_runs;
1798 /* If merging already happent at WPA time, we are done. */
1802 /* Now compute count_materialization_scale of each node.
1803 During LTRANS we already have values of count_materialization_scale
1804 computed, so just update them. */
1805 FOR_EACH_FUNCTION (node)
1806 if (node->lto_file_data
1807 && node->lto_file_data->profile_info.runs)
1811 scale = RDIV (node->count_materialization_scale * max_runs,
1812 node->lto_file_data->profile_info.runs);
1813 node->count_materialization_scale = scale;
1815 fatal_error (input_location, "Profile information in %s corrupted",
1816 file_data->file_name);
1818 if (scale == REG_BR_PROB_BASE)
1820 for (edge = node->callees; edge; edge = edge->next_callee)
1821 edge->count = apply_scale (edge->count, scale);
1822 node->count = apply_scale (node->count, scale);
1826 /* Input and merge the symtab from each of the .o files passed to
1832 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1833 struct lto_file_decl_data *file_data;
1835 struct cgraph_node *node;
1837 while ((file_data = file_data_vec[j++]))
1841 struct lto_input_block *ib;
1842 vec<symtab_node *> nodes;
1844 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1847 fatal_error (input_location,
1848 "cannot find LTO cgraph in %s", file_data->file_name);
1849 input_profile_summary (ib, file_data);
1850 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1851 nodes = input_cgraph_1 (file_data, ib);
1852 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1855 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1858 fatal_error (input_location, "cannot find LTO section refs in %s",
1859 file_data->file_name);
1860 input_refs (ib, nodes);
1861 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1864 input_cgraph_opt_summary (nodes);
1868 merge_profile_summaries (file_data_vec);
1869 get_working_sets ();
1872 /* Clear out the aux field that was used to store enough state to
1873 tell which nodes should be overwritten. */
1874 FOR_EACH_FUNCTION (node)
1876 /* Some nodes may have been created by cgraph_node. This
1877 happens when the callgraph contains nested functions. If the
1878 node for the parent function was never emitted to the gimple
1879 file, cgraph_node will create a node for it when setting the
1880 context of the nested function. */
1881 if (node->lto_file_data)
1886 /* Input function/variable tables that will allow libgomp to look up offload
1887 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1890 input_offload_tables (bool do_force_output)
1892 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1893 struct lto_file_decl_data *file_data;
1896 while ((file_data = file_data_vec[j++]))
1900 struct lto_input_block *ib
1901 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1906 enum LTO_symtab_tags tag
1907 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1910 if (tag == LTO_symtab_unavail_node)
1912 int decl_index = streamer_read_uhwi (ib);
1914 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1915 vec_safe_push (offload_funcs, fn_decl);
1917 /* Prevent IPA from removing fn_decl as unreachable, since there
1918 may be no refs from the parent function to child_fn in offload
1920 if (do_force_output)
1921 cgraph_node::get (fn_decl)->mark_force_output ();
1923 else if (tag == LTO_symtab_variable)
1925 int decl_index = streamer_read_uhwi (ib);
1927 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1928 vec_safe_push (offload_vars, var_decl);
1930 /* Prevent IPA from removing var_decl as unused, since there
1931 may be no refs to var_decl in offload LTO mode. */
1932 if (do_force_output)
1933 varpool_node::get (var_decl)->force_output = 1;
1936 fatal_error (input_location,
1937 "invalid offload table in %s", file_data->file_name);
1939 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1942 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1947 /* True when we need optimization summary for NODE. */
1950 output_cgraph_opt_summary_p (struct cgraph_node *node)
1952 return (node->clone_of
1953 && (node->clone.tree_map
1954 || node->clone.args_to_skip
1955 || node->clone.combined_args_to_skip));
1958 /* Output optimization summary for EDGE to OB. */
1960 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1961 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1965 /* Output optimization summary for NODE to OB. */
1968 output_node_opt_summary (struct output_block *ob,
1969 struct cgraph_node *node,
1970 lto_symtab_encoder_t encoder)
1974 struct ipa_replace_map *map;
1975 struct bitpack_d bp;
1977 struct cgraph_edge *e;
1979 if (node->clone.args_to_skip)
1981 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1982 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1983 streamer_write_uhwi (ob, index);
1986 streamer_write_uhwi (ob, 0);
1987 if (node->clone.combined_args_to_skip)
1989 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1990 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1991 streamer_write_uhwi (ob, index);
1994 streamer_write_uhwi (ob, 0);
1995 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1996 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1998 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1999 mechanism to store function local declarations into summaries. */
2000 gcc_assert (!map->old_tree);
2001 streamer_write_uhwi (ob, map->parm_num);
2002 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
2003 stream_write_tree (ob, map->new_tree, true);
2004 bp = bitpack_create (ob->main_stream);
2005 bp_pack_value (&bp, map->replace_p, 1);
2006 bp_pack_value (&bp, map->ref_p, 1);
2007 streamer_write_bitpack (&bp);
2010 if (lto_symtab_encoder_in_partition_p (encoder, node))
2012 for (e = node->callees; e; e = e->next_callee)
2013 output_edge_opt_summary (ob, e);
2014 for (e = node->indirect_calls; e; e = e->next_callee)
2015 output_edge_opt_summary (ob, e);
2019 /* Output optimization summaries stored in callgraph.
2020 At the moment it is the clone info structure. */
2023 output_cgraph_opt_summary (void)
2026 lto_symtab_encoder_t encoder;
2027 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2031 encoder = ob->decl_state->symtab_node_encoder;
2032 n_nodes = lto_symtab_encoder_size (encoder);
2033 for (i = 0; i < n_nodes; i++)
2035 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2036 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2037 if (cnode && output_cgraph_opt_summary_p (cnode))
2040 streamer_write_uhwi (ob, count);
2041 for (i = 0; i < n_nodes; i++)
2043 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2044 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2045 if (cnode && output_cgraph_opt_summary_p (cnode))
2047 streamer_write_uhwi (ob, i);
2048 output_node_opt_summary (ob, cnode, encoder);
2051 produce_asm (ob, NULL);
2052 destroy_output_block (ob);
2055 /* Input optimisation summary of EDGE. */
2058 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2059 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
2063 /* Input optimisation summary of NODE. */
2066 input_node_opt_summary (struct cgraph_node *node,
2067 struct lto_input_block *ib_main,
2068 struct data_in *data_in)
2073 struct bitpack_d bp;
2074 struct cgraph_edge *e;
2076 count = streamer_read_uhwi (ib_main);
2078 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
2079 for (i = 0; i < count; i++)
2081 bit = streamer_read_uhwi (ib_main);
2082 bitmap_set_bit (node->clone.args_to_skip, bit);
2084 count = streamer_read_uhwi (ib_main);
2086 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
2087 for (i = 0; i < count; i++)
2089 bit = streamer_read_uhwi (ib_main);
2090 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
2092 count = streamer_read_uhwi (ib_main);
2093 for (i = 0; i < count; i++)
2095 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2097 vec_safe_push (node->clone.tree_map, map);
2098 map->parm_num = streamer_read_uhwi (ib_main);
2099 map->old_tree = NULL;
2100 map->new_tree = stream_read_tree (ib_main, data_in);
2101 bp = streamer_read_bitpack (ib_main);
2102 map->replace_p = bp_unpack_value (&bp, 1);
2103 map->ref_p = bp_unpack_value (&bp, 1);
2105 for (e = node->callees; e; e = e->next_callee)
2106 input_edge_opt_summary (e, ib_main);
2107 for (e = node->indirect_calls; e; e = e->next_callee)
2108 input_edge_opt_summary (e, ib_main);
2111 /* Read section in file FILE_DATA of length LEN with data DATA. */
2114 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2115 const char *data, size_t len,
2116 vec<symtab_node *> nodes)
2118 const struct lto_function_header *header =
2119 (const struct lto_function_header *) data;
2120 const int cfg_offset = sizeof (struct lto_function_header);
2121 const int main_offset = cfg_offset + header->cfg_size;
2122 const int string_offset = main_offset + header->main_size;
2123 struct data_in *data_in;
2127 lto_input_block ib_main ((const char *) data + main_offset,
2128 header->main_size, file_data->mode_table);
2131 lto_data_in_create (file_data, (const char *) data + string_offset,
2132 header->string_size, vNULL);
2133 count = streamer_read_uhwi (&ib_main);
2135 for (i = 0; i < count; i++)
2137 int ref = streamer_read_uhwi (&ib_main);
2138 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2141 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2143 lto_data_in_delete (data_in);
2146 /* Input optimization summary of cgraph. */
2149 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2151 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2152 struct lto_file_decl_data *file_data;
2155 while ((file_data = file_data_vec[j++]))
2159 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
2163 input_cgraph_opt_section (file_data, data, len, nodes);