Remove arc profile histogram in non-LTO mode.
[official-gcc.git] / gcc / lto-cgraph.c
blob6d9eea13f223098f23cd41920e7ebbbb9b8a7dd7
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2018 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 "backend.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "predict.h"
31 #include "stringpool.h"
32 #include "tree-streamer.h"
33 #include "cgraph.h"
34 #include "tree-pass.h"
35 #include "profile.h"
36 #include "context.h"
37 #include "pass_manager.h"
38 #include "ipa-utils.h"
39 #include "omp-offload.h"
40 #include "stringpool.h"
41 #include "attribs.h"
43 /* True when asm nodes has been output. */
44 bool asm_nodes_output = false;
46 static void output_cgraph_opt_summary (void);
47 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
49 /* Number of LDPR values known to GCC. */
50 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
52 /* All node orders are ofsetted by ORDER_BASE. */
53 static int order_base;
55 /* Cgraph streaming is organized as set of record whose type
56 is indicated by a tag. */
57 enum LTO_symtab_tags
59 /* Must leave 0 for the stopper. */
61 /* Cgraph node without body available. */
62 LTO_symtab_unavail_node = 1,
63 /* Cgraph node with function body. */
64 LTO_symtab_analyzed_node,
65 /* Cgraph edges. */
66 LTO_symtab_edge,
67 LTO_symtab_indirect_edge,
68 LTO_symtab_variable,
69 LTO_symtab_last_tag
72 /* Create a new symtab encoder.
73 if FOR_INPUT, the encoder allocate only datastructures needed
74 to read the symtab. */
76 lto_symtab_encoder_t
77 lto_symtab_encoder_new (bool for_input)
79 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
81 if (!for_input)
82 encoder->map = new hash_map<symtab_node *, size_t>;
83 encoder->nodes.create (0);
84 return encoder;
88 /* Delete ENCODER and its components. */
90 void
91 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
93 encoder->nodes.release ();
94 if (encoder->map)
95 delete encoder->map;
96 free (encoder);
100 /* Return the existing reference number of NODE in the symtab encoder in
101 output block OB. Assign a new reference if this is the first time
102 NODE is encoded. */
105 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
106 symtab_node *node)
108 int ref;
110 if (!encoder->map)
112 lto_encoder_entry entry = {node, false, false, false};
114 ref = encoder->nodes.length ();
115 encoder->nodes.safe_push (entry);
116 return ref;
119 size_t *slot = encoder->map->get (node);
120 if (!slot || !*slot)
122 lto_encoder_entry entry = {node, false, false, false};
123 ref = encoder->nodes.length ();
124 if (!slot)
125 encoder->map->put (node, ref + 1);
126 encoder->nodes.safe_push (entry);
128 else
129 ref = *slot - 1;
131 return ref;
134 /* Remove NODE from encoder. */
136 bool
137 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
138 symtab_node *node)
140 int index;
141 lto_encoder_entry last_node;
143 size_t *slot = encoder->map->get (node);
144 if (slot == NULL || !*slot)
145 return false;
147 index = *slot - 1;
148 gcc_checking_assert (encoder->nodes[index].node == node);
150 /* Remove from vector. We do this by swapping node with the last element
151 of the vector. */
152 last_node = encoder->nodes.pop ();
153 if (last_node.node != node)
155 gcc_assert (encoder->map->put (last_node.node, index + 1));
157 /* Move the last element to the original spot of NODE. */
158 encoder->nodes[index] = last_node;
161 /* Remove element from hash table. */
162 encoder->map->remove (node);
163 return true;
167 /* Return TRUE if we should encode the body of NODE (if any). */
169 bool
170 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
171 struct cgraph_node *node)
173 int index = lto_symtab_encoder_lookup (encoder, node);
174 return encoder->nodes[index].body;
177 /* Specify that we encode the body of NODE in this partition. */
179 static void
180 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
181 struct cgraph_node *node)
183 int index = lto_symtab_encoder_encode (encoder, node);
184 gcc_checking_assert (encoder->nodes[index].node == node);
185 encoder->nodes[index].body = true;
188 /* Return TRUE if we should encode initializer of NODE (if any). */
190 bool
191 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
192 varpool_node *node)
194 int index = lto_symtab_encoder_lookup (encoder, node);
195 if (index == LCC_NOT_FOUND)
196 return false;
197 return encoder->nodes[index].initializer;
200 /* Specify that we should encode initializer of NODE (if any). */
202 static void
203 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
204 varpool_node *node)
206 int index = lto_symtab_encoder_lookup (encoder, node);
207 encoder->nodes[index].initializer = true;
210 /* Return TRUE if NODE is in this partition. */
212 bool
213 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
214 symtab_node *node)
216 int index = lto_symtab_encoder_lookup (encoder, node);
217 if (index == LCC_NOT_FOUND)
218 return false;
219 return encoder->nodes[index].in_partition;
222 /* Specify that NODE is in this partition. */
224 void
225 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
226 symtab_node *node)
228 int index = lto_symtab_encoder_encode (encoder, node);
229 encoder->nodes[index].in_partition = true;
232 /* Output the cgraph EDGE to OB using ENCODER. */
234 static void
235 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
236 lto_symtab_encoder_t encoder)
238 unsigned int uid;
239 intptr_t ref;
240 struct bitpack_d bp;
242 if (edge->indirect_unknown_callee)
243 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
244 LTO_symtab_indirect_edge);
245 else
246 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
247 LTO_symtab_edge);
249 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
250 gcc_assert (ref != LCC_NOT_FOUND);
251 streamer_write_hwi_stream (ob->main_stream, ref);
253 if (!edge->indirect_unknown_callee)
255 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
256 gcc_assert (ref != LCC_NOT_FOUND);
257 streamer_write_hwi_stream (ob->main_stream, ref);
260 edge->count.stream_out (ob->main_stream);
262 bp = bitpack_create (ob->main_stream);
263 uid = (!gimple_has_body_p (edge->caller->decl) || edge->caller->thunk.thunk_p
264 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
265 bp_pack_enum (&bp, cgraph_inline_failed_t,
266 CIF_N_REASONS, edge->inline_failed);
267 bp_pack_var_len_unsigned (&bp, uid);
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 gcc_assert (!edge->call_stmt_cannot_inline_p
272 || edge->inline_failed != CIF_BODY_NOT_AVAILABLE);
273 bp_pack_value (&bp, edge->can_throw_external, 1);
274 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
275 if (edge->indirect_unknown_callee)
277 int flags = edge->indirect_info->ecf_flags;
278 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
279 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
280 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
281 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
282 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
283 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
284 /* Flags that should not appear on indirect calls. */
285 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
286 | ECF_MAY_BE_ALLOCA
287 | ECF_SIBCALL
288 | ECF_LEAF
289 | ECF_NOVOPS)));
291 streamer_write_bitpack (&bp);
292 if (edge->indirect_unknown_callee)
294 streamer_write_hwi_stream (ob->main_stream,
295 edge->indirect_info->common_target_id);
296 if (edge->indirect_info->common_target_id)
297 streamer_write_hwi_stream
298 (ob->main_stream, edge->indirect_info->common_target_probability);
302 /* Return if NODE contain references from other partitions. */
304 bool
305 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
307 int i;
308 struct ipa_ref *ref = NULL;
310 for (i = 0; node->iterate_referring (i, ref); i++)
312 /* Ignore references from non-offloadable nodes while streaming NODE into
313 offload LTO section. */
314 if (!ref->referring->need_lto_streaming)
315 continue;
317 if (ref->referring->in_other_partition
318 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
319 return true;
321 return false;
324 /* Return true when node is reachable from other partition. */
326 bool
327 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
329 struct cgraph_edge *e;
330 if (!node->definition)
331 return false;
332 if (node->global.inlined_to)
333 return false;
334 for (e = node->callers; e; e = e->next_caller)
336 /* Ignore references from non-offloadable nodes while streaming NODE into
337 offload LTO section. */
338 if (!e->caller->need_lto_streaming)
339 continue;
341 if (e->caller->in_other_partition
342 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
343 return true;
345 return false;
348 /* Return if NODE contain references from other partitions. */
350 bool
351 referenced_from_this_partition_p (symtab_node *node,
352 lto_symtab_encoder_t encoder)
354 int i;
355 struct ipa_ref *ref = NULL;
357 for (i = 0; node->iterate_referring (i, ref); i++)
358 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
359 return true;
360 return false;
363 /* Return true when node is reachable from other partition. */
365 bool
366 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
368 struct cgraph_edge *e;
369 for (e = node->callers; e; e = e->next_caller)
370 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
371 return true;
372 return false;
375 /* Output the cgraph NODE to OB. ENCODER is used to find the
376 reference number of NODE->inlined_to. SET is the set of nodes we
377 are writing to the current file. If NODE is not in SET, then NODE
378 is a boundary of a cgraph_node_set and we pretend NODE just has a
379 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
380 that have had their callgraph node written so far. This is used to
381 determine if NODE is a clone of a previously written node. */
383 static void
384 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
385 lto_symtab_encoder_t encoder)
387 unsigned int tag;
388 struct bitpack_d bp;
389 bool boundary_p;
390 intptr_t ref;
391 bool in_other_partition = false;
392 struct cgraph_node *clone_of, *ultimate_clone_of;
393 ipa_opt_pass_d *pass;
394 int i;
395 const char *comdat;
396 const char *section;
397 tree group;
399 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
401 if (node->analyzed && (!boundary_p || node->alias
402 || (node->thunk.thunk_p && !node->global.inlined_to)))
403 tag = LTO_symtab_analyzed_node;
404 else
405 tag = LTO_symtab_unavail_node;
407 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
408 tag);
409 streamer_write_hwi_stream (ob->main_stream, node->order);
411 /* In WPA mode, we only output part of the call-graph. Also, we
412 fake cgraph node attributes. There are two cases that we care.
414 Boundary nodes: There are nodes that are not part of SET but are
415 called from within SET. We artificially make them look like
416 externally visible nodes with no function body.
418 Cherry-picked nodes: These are nodes we pulled from other
419 translation units into SET during IPA-inlining. We make them as
420 local static nodes to prevent clashes with other local statics. */
421 if (boundary_p && node->analyzed
422 && node->get_partitioning_class () == SYMBOL_PARTITION)
424 /* Inline clones can not be part of boundary.
425 gcc_assert (!node->global.inlined_to);
427 FIXME: At the moment they can be, when partition contains an inline
428 clone that is clone of inline clone from outside partition. We can
429 reshape the clone tree and make other tree to be the root, but it
430 needs a bit extra work and will be promplty done by cgraph_remove_node
431 after reading back. */
432 in_other_partition = 1;
435 clone_of = node->clone_of;
436 while (clone_of
437 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
438 if (clone_of->prev_sibling_clone)
439 clone_of = clone_of->prev_sibling_clone;
440 else
441 clone_of = clone_of->clone_of;
443 /* See if body of the master function is output. If not, we are seeing only
444 an declaration and we do not need to pass down clone tree. */
445 ultimate_clone_of = clone_of;
446 while (ultimate_clone_of && ultimate_clone_of->clone_of)
447 ultimate_clone_of = ultimate_clone_of->clone_of;
449 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
450 clone_of = NULL;
452 if (tag == LTO_symtab_analyzed_node)
453 gcc_assert (clone_of || !node->clone_of);
454 if (!clone_of)
455 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
456 else
457 streamer_write_hwi_stream (ob->main_stream, ref);
460 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
461 node->count.stream_out (ob->main_stream);
462 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
464 streamer_write_hwi_stream (ob->main_stream,
465 node->ipa_transforms_to_apply.length ());
466 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
467 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
469 if (tag == LTO_symtab_analyzed_node)
471 if (node->global.inlined_to)
473 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
474 gcc_assert (ref != LCC_NOT_FOUND);
476 else
477 ref = LCC_NOT_FOUND;
479 streamer_write_hwi_stream (ob->main_stream, ref);
482 group = node->get_comdat_group ();
483 if (group)
484 comdat = IDENTIFIER_POINTER (group);
485 else
486 comdat = "";
487 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
489 if (group)
491 if (node->same_comdat_group)
493 ref = LCC_NOT_FOUND;
494 for (struct symtab_node *n = node->same_comdat_group;
495 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
496 ref = lto_symtab_encoder_lookup (encoder, n);
498 else
499 ref = LCC_NOT_FOUND;
500 streamer_write_hwi_stream (ob->main_stream, ref);
503 section = node->get_section ();
504 if (!section)
505 section = "";
507 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
509 bp = bitpack_create (ob->main_stream);
510 bp_pack_value (&bp, node->local.local, 1);
511 bp_pack_value (&bp, node->externally_visible, 1);
512 bp_pack_value (&bp, node->no_reorder, 1);
513 bp_pack_value (&bp, node->definition, 1);
514 bp_pack_value (&bp, node->local.versionable, 1);
515 bp_pack_value (&bp, node->local.can_change_signature, 1);
516 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
517 bp_pack_value (&bp, node->force_output, 1);
518 bp_pack_value (&bp, node->forced_by_abi, 1);
519 bp_pack_value (&bp, node->unique_name, 1);
520 bp_pack_value (&bp, node->body_removed, 1);
521 bp_pack_value (&bp, node->implicit_section, 1);
522 bp_pack_value (&bp, node->address_taken, 1);
523 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
524 && node->get_partitioning_class () == SYMBOL_PARTITION
525 && (reachable_from_other_partition_p (node, encoder)
526 || referenced_from_other_partition_p (node, encoder)), 1);
527 bp_pack_value (&bp, node->lowered, 1);
528 bp_pack_value (&bp, in_other_partition, 1);
529 bp_pack_value (&bp, node->alias, 1);
530 bp_pack_value (&bp, node->transparent_alias, 1);
531 bp_pack_value (&bp, node->weakref, 1);
532 bp_pack_value (&bp, node->frequency, 2);
533 bp_pack_value (&bp, node->only_called_at_startup, 1);
534 bp_pack_value (&bp, node->only_called_at_exit, 1);
535 bp_pack_value (&bp, node->tm_clone, 1);
536 bp_pack_value (&bp, node->calls_comdat_local, 1);
537 bp_pack_value (&bp, node->icf_merged, 1);
538 bp_pack_value (&bp, node->nonfreeing_fn, 1);
539 bp_pack_value (&bp, node->thunk.thunk_p, 1);
540 bp_pack_value (&bp, node->parallelized_function, 1);
541 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
542 LDPR_NUM_KNOWN,
543 /* When doing incremental link, we will get new resolution
544 info next time we process the file. */
545 flag_incremental_link ? LDPR_UNKNOWN : node->resolution);
546 bp_pack_value (&bp, node->split_part, 1);
547 streamer_write_bitpack (&bp);
548 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
550 if (node->thunk.thunk_p)
552 streamer_write_uhwi_stream
553 (ob->main_stream,
554 1 + (node->thunk.this_adjusting != 0) * 2
555 + (node->thunk.virtual_offset_p != 0) * 4
556 + (node->thunk.add_pointer_bounds_args != 0) * 8);
557 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
558 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
560 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
561 if (DECL_STATIC_CONSTRUCTOR (node->decl))
562 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
563 if (DECL_STATIC_DESTRUCTOR (node->decl))
564 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
567 /* Output the varpool NODE to OB.
568 If NODE is not in SET, then NODE is a boundary. */
570 static void
571 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
572 lto_symtab_encoder_t encoder)
574 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
575 bool encode_initializer_p
576 = (node->definition
577 && lto_symtab_encoder_encode_initializer_p (encoder, node));
578 struct bitpack_d bp;
579 int ref;
580 const char *comdat;
581 const char *section;
582 tree group;
584 gcc_assert (!encode_initializer_p || node->definition);
585 gcc_assert (boundary_p || encode_initializer_p);
587 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
588 LTO_symtab_variable);
589 streamer_write_hwi_stream (ob->main_stream, node->order);
590 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
591 bp = bitpack_create (ob->main_stream);
592 bp_pack_value (&bp, node->externally_visible, 1);
593 bp_pack_value (&bp, node->no_reorder, 1);
594 bp_pack_value (&bp, node->force_output, 1);
595 bp_pack_value (&bp, node->forced_by_abi, 1);
596 bp_pack_value (&bp, node->unique_name, 1);
597 bp_pack_value (&bp,
598 node->body_removed
599 || (!encode_initializer_p && !node->alias && node->definition),
601 bp_pack_value (&bp, node->implicit_section, 1);
602 bp_pack_value (&bp, node->writeonly, 1);
603 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
605 bp_pack_value (&bp, node->alias, 1);
606 bp_pack_value (&bp, node->transparent_alias, 1);
607 bp_pack_value (&bp, node->weakref, 1);
608 bp_pack_value (&bp, node->analyzed && (!boundary_p || node->alias), 1);
609 gcc_assert (node->definition || !node->analyzed);
610 /* Constant pool initializers can be de-unified into individual ltrans units.
611 FIXME: Alternatively at -Os we may want to avoid generating for them the local
612 labels and share them across LTRANS partitions. */
613 if (node->get_partitioning_class () != SYMBOL_PARTITION)
615 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
616 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
618 else
620 bp_pack_value (&bp, node->definition
621 && referenced_from_other_partition_p (node, encoder), 1);
622 bp_pack_value (&bp, node->analyzed
623 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
624 /* in_other_partition. */
626 bp_pack_value (&bp, node->tls_model, 3);
627 bp_pack_value (&bp, node->used_by_single_function, 1);
628 bp_pack_value (&bp, node->dynamically_initialized, 1);
629 bp_pack_value (&bp, node->need_bounds_init, 1);
630 streamer_write_bitpack (&bp);
632 group = node->get_comdat_group ();
633 if (group)
634 comdat = IDENTIFIER_POINTER (group);
635 else
636 comdat = "";
637 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
639 if (group)
641 if (node->same_comdat_group)
643 ref = LCC_NOT_FOUND;
644 for (struct symtab_node *n = node->same_comdat_group;
645 ref == LCC_NOT_FOUND && n != node; n = n->same_comdat_group)
646 ref = lto_symtab_encoder_lookup (encoder, n);
648 else
649 ref = LCC_NOT_FOUND;
650 streamer_write_hwi_stream (ob->main_stream, ref);
653 section = node->get_section ();
654 if (!section)
655 section = "";
656 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
658 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
659 LDPR_NUM_KNOWN, node->resolution);
662 /* Output the varpool NODE to OB.
663 If NODE is not in SET, then NODE is a boundary. */
665 static void
666 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
667 lto_symtab_encoder_t encoder)
669 struct bitpack_d bp;
670 int nref;
671 int uid = ref->lto_stmt_uid;
672 struct cgraph_node *node;
674 bp = bitpack_create (ob->main_stream);
675 bp_pack_value (&bp, ref->use, 3);
676 bp_pack_value (&bp, ref->speculative, 1);
677 streamer_write_bitpack (&bp);
678 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
679 gcc_assert (nref != LCC_NOT_FOUND);
680 streamer_write_hwi_stream (ob->main_stream, nref);
682 node = dyn_cast <cgraph_node *> (ref->referring);
683 if (node)
685 if (ref->stmt)
686 uid = gimple_uid (ref->stmt) + 1;
687 streamer_write_hwi_stream (ob->main_stream, uid);
691 /* Stream out profile_summary to OB. */
693 static void
694 output_profile_summary (struct lto_simple_output_block *ob)
696 if (profile_info)
698 /* We do not output num and run_max, they are not used by
699 GCC profile feedback and they are difficult to merge from multiple
700 units. */
701 unsigned runs = (profile_info->runs);
702 streamer_write_uhwi_stream (ob->main_stream, runs);
704 /* IPA-profile computes hot bb threshold based on cumulated
705 whole program profile. We need to stream it down to ltrans. */
706 if (flag_wpa)
707 streamer_write_gcov_count_stream (ob->main_stream,
708 get_hot_bb_threshold ());
710 else
711 streamer_write_uhwi_stream (ob->main_stream, 0);
714 /* Output all callees or indirect outgoing edges. EDGE must be the first such
715 edge. */
717 static void
718 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
719 struct lto_simple_output_block *ob,
720 lto_symtab_encoder_t encoder)
722 if (!edge)
723 return;
725 /* Output edges in backward direction, so the reconstructed callgraph match
726 and it is easy to associate call sites in the IPA pass summaries. */
727 while (edge->next_callee)
728 edge = edge->next_callee;
729 for (; edge; edge = edge->prev_callee)
730 lto_output_edge (ob, edge, encoder);
733 /* Output the part of the cgraph in SET. */
735 static void
736 output_refs (lto_symtab_encoder_t encoder)
738 struct lto_simple_output_block *ob;
739 int count;
740 struct ipa_ref *ref;
742 ob = lto_create_simple_output_block (LTO_section_refs);
744 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
746 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
748 /* IPA_REF_ALIAS references are always preserved
749 in the boundary. Alias node can't have other references and
750 can be always handled as if it's not in the boundary. */
751 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
752 continue;
754 count = node->ref_list.nreferences ();
755 if (count)
757 streamer_write_gcov_count_stream (ob->main_stream, count);
758 streamer_write_uhwi_stream (ob->main_stream,
759 lto_symtab_encoder_lookup (encoder, node));
760 for (int i = 0; node->iterate_reference (i, ref); i++)
761 lto_output_ref (ob, ref, encoder);
765 streamer_write_uhwi_stream (ob->main_stream, 0);
767 lto_destroy_simple_output_block (ob);
770 /* Add NODE into encoder as well as nodes it is cloned from.
771 Do it in a way so clones appear first. */
773 static void
774 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
775 bool include_body)
777 if (node->clone_of)
778 add_node_to (encoder, node->clone_of, include_body);
779 else if (include_body)
780 lto_set_symtab_encoder_encode_body (encoder, node);
781 lto_symtab_encoder_encode (encoder, node);
784 /* Add all references in NODE to encoders. */
786 static void
787 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
789 int i;
790 struct ipa_ref *ref = NULL;
791 for (i = 0; node->iterate_reference (i, ref); i++)
792 if (is_a <cgraph_node *> (ref->referred))
793 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
794 else
795 lto_symtab_encoder_encode (encoder, ref->referred);
798 /* Select what needs to be streamed out. In regular lto mode stream everything.
799 In offload lto mode stream only nodes marked as offloadable. */
800 void
801 select_what_to_stream (void)
803 struct symtab_node *snode;
804 FOR_EACH_SYMBOL (snode)
805 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
808 /* Find all symbols we want to stream into given partition and insert them
809 to encoders.
811 The function actually replaces IN_ENCODER by new one. The reason is that
812 streaming code needs clone's origin to be streamed before clone. This
813 means that we need to insert the nodes in specific order. This order is
814 ignored by the partitioning logic earlier. */
816 lto_symtab_encoder_t
817 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
819 struct cgraph_edge *edge;
820 int i;
821 lto_symtab_encoder_t encoder;
822 lto_symtab_encoder_iterator lsei;
823 hash_set<void *> reachable_call_targets;
825 encoder = lto_symtab_encoder_new (false);
827 /* Go over all entries in the IN_ENCODER and duplicate them to
828 ENCODER. At the same time insert masters of clones so
829 every master appears before clone. */
830 for (lsei = lsei_start_function_in_partition (in_encoder);
831 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
833 struct cgraph_node *node = lsei_cgraph_node (lsei);
834 if (!node->need_lto_streaming)
835 continue;
836 add_node_to (encoder, node, true);
837 lto_set_symtab_encoder_in_partition (encoder, node);
838 create_references (encoder, node);
840 for (lsei = lsei_start_variable_in_partition (in_encoder);
841 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
843 varpool_node *vnode = lsei_varpool_node (lsei);
845 if (!vnode->need_lto_streaming)
846 continue;
847 lto_set_symtab_encoder_in_partition (encoder, vnode);
848 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
849 create_references (encoder, vnode);
851 /* Pickle in also the initializer of all referenced readonly variables
852 to help folding. Constant pool variables are not shared, so we must
853 pickle those too. */
854 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
856 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
857 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
859 if (!lto_symtab_encoder_encode_initializer_p (encoder,
860 vnode)
861 && (((vnode->ctor_useable_for_folding_p ()
862 && (!DECL_VIRTUAL_P (vnode->decl)
863 || !flag_wpa
864 || flag_ltrans_devirtualize)))))
866 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
867 create_references (encoder, vnode);
872 /* Go over all the nodes again to include callees that are not in
873 SET. */
874 for (lsei = lsei_start_function_in_partition (encoder);
875 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
877 struct cgraph_node *node = lsei_cgraph_node (lsei);
878 for (edge = node->callees; edge; edge = edge->next_callee)
880 struct cgraph_node *callee = edge->callee;
881 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
883 /* We should have moved all the inlines. */
884 gcc_assert (!callee->global.inlined_to);
885 add_node_to (encoder, callee, false);
888 /* Add all possible targets for late devirtualization. */
889 if (flag_ltrans_devirtualize || !flag_wpa)
890 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
891 if (edge->indirect_info->polymorphic)
893 unsigned int i;
894 void *cache_token;
895 bool final;
896 vec <cgraph_node *>targets
897 = possible_polymorphic_call_targets
898 (edge, &final, &cache_token);
899 if (!reachable_call_targets.add (cache_token))
901 for (i = 0; i < targets.length (); i++)
903 struct cgraph_node *callee = targets[i];
905 /* Adding an external declarations into the unit serves
906 no purpose and just increases its boundary. */
907 if (callee->definition
908 && !lto_symtab_encoder_in_partition_p
909 (encoder, callee))
911 gcc_assert (!callee->global.inlined_to);
912 add_node_to (encoder, callee, false);
918 /* Be sure to also insert alias targert and thunk callees. These needs
919 to stay to aid local calling conventions. */
920 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
922 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
923 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
925 if (node->alias && node->analyzed)
926 create_references (encoder, node);
927 if (cnode
928 && cnode->thunk.thunk_p && !cnode->global.inlined_to)
929 add_node_to (encoder, cnode->callees->callee, false);
930 while (node->transparent_alias && node->analyzed)
932 node = node->get_alias_target ();
933 if (is_a <cgraph_node *> (node))
934 add_node_to (encoder, dyn_cast <cgraph_node *> (node),
935 false);
936 else
937 lto_symtab_encoder_encode (encoder, node);
940 lto_symtab_encoder_delete (in_encoder);
941 return encoder;
944 /* Output the part of the symtab in SET and VSET. */
946 void
947 output_symtab (void)
949 struct cgraph_node *node;
950 struct lto_simple_output_block *ob;
951 int i, n_nodes;
952 lto_symtab_encoder_t encoder;
954 if (flag_wpa)
955 output_cgraph_opt_summary ();
957 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
959 output_profile_summary (ob);
961 /* An encoder for cgraph nodes should have been created by
962 ipa_write_summaries_1. */
963 gcc_assert (ob->decl_state->symtab_node_encoder);
964 encoder = ob->decl_state->symtab_node_encoder;
966 /* Write out the nodes. We must first output a node and then its clones,
967 otherwise at a time reading back the node there would be nothing to clone
968 from. */
969 n_nodes = lto_symtab_encoder_size (encoder);
970 for (i = 0; i < n_nodes; i++)
972 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
973 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
974 lto_output_node (ob, cnode, encoder);
975 else
976 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
979 /* Go over the nodes in SET again to write edges. */
980 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
982 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
983 if (node
984 && ((node->thunk.thunk_p && !node->global.inlined_to)
985 || lto_symtab_encoder_in_partition_p (encoder, node)))
987 output_outgoing_cgraph_edges (node->callees, ob, encoder);
988 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
992 streamer_write_uhwi_stream (ob->main_stream, 0);
994 lto_destroy_simple_output_block (ob);
996 /* Emit toplevel asms.
997 When doing WPA we must output every asm just once. Since we do not partition asm
998 nodes at all, output them to first output. This is kind of hack, but should work
999 well. */
1000 if (!asm_nodes_output)
1002 asm_nodes_output = true;
1003 lto_output_toplevel_asms ();
1006 output_refs (encoder);
1009 /* Return identifier encoded in IB as a plain string. */
1011 static tree
1012 read_identifier (struct lto_input_block *ib)
1014 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1015 tree id;
1017 if (ib->data[ib->p + len])
1018 lto_section_overrun (ib);
1019 if (!len)
1021 ib->p++;
1022 return NULL;
1024 id = get_identifier (ib->data + ib->p);
1025 ib->p += len + 1;
1026 return id;
1029 /* Return string encoded in IB, NULL if string is empty. */
1031 static const char *
1032 read_string (struct lto_input_block *ib)
1034 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1035 const char *str;
1037 if (ib->data[ib->p + len])
1038 lto_section_overrun (ib);
1039 if (!len)
1041 ib->p++;
1042 return NULL;
1044 str = ib->data + ib->p;
1045 ib->p += len + 1;
1046 return str;
1049 /* Output function/variable tables that will allow libgomp to look up offload
1050 target code.
1051 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1052 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1053 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1055 void
1056 output_offload_tables (void)
1058 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1059 return;
1061 struct lto_simple_output_block *ob
1062 = lto_create_simple_output_block (LTO_section_offload_table);
1064 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1066 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1067 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1068 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1069 (*offload_funcs)[i]);
1072 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1074 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1075 LTO_symtab_last_tag, LTO_symtab_variable);
1076 lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1077 (*offload_vars)[i]);
1080 streamer_write_uhwi_stream (ob->main_stream, 0);
1081 lto_destroy_simple_output_block (ob);
1083 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1084 streamed to one partition only. That's why we free offload_funcs and
1085 offload_vars after the first call of output_offload_tables. */
1086 if (flag_wpa)
1088 vec_free (offload_funcs);
1089 vec_free (offload_vars);
1093 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1094 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1095 NODE or to replace the values in it, for instance because the first
1096 time we saw it, the function body was not available but now it
1097 is. BP is a bitpack with all the bitflags for NODE read from the
1098 stream. */
1100 static void
1101 input_overwrite_node (struct lto_file_decl_data *file_data,
1102 struct cgraph_node *node,
1103 enum LTO_symtab_tags tag,
1104 struct bitpack_d *bp)
1106 node->aux = (void *) tag;
1107 node->lto_file_data = file_data;
1109 node->local.local = bp_unpack_value (bp, 1);
1110 node->externally_visible = bp_unpack_value (bp, 1);
1111 node->no_reorder = bp_unpack_value (bp, 1);
1112 node->definition = bp_unpack_value (bp, 1);
1113 node->local.versionable = bp_unpack_value (bp, 1);
1114 node->local.can_change_signature = bp_unpack_value (bp, 1);
1115 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1116 node->force_output = bp_unpack_value (bp, 1);
1117 node->forced_by_abi = bp_unpack_value (bp, 1);
1118 node->unique_name = bp_unpack_value (bp, 1);
1119 node->body_removed = bp_unpack_value (bp, 1);
1120 node->implicit_section = bp_unpack_value (bp, 1);
1121 node->address_taken = bp_unpack_value (bp, 1);
1122 node->used_from_other_partition = bp_unpack_value (bp, 1);
1123 node->lowered = bp_unpack_value (bp, 1);
1124 node->analyzed = tag == LTO_symtab_analyzed_node;
1125 node->in_other_partition = bp_unpack_value (bp, 1);
1126 if (node->in_other_partition
1127 /* Avoid updating decl when we are seeing just inline clone.
1128 When inlining function that has functions already inlined into it,
1129 we produce clones of inline clones.
1131 WPA partitioning might put each clone into different unit and
1132 we might end up streaming inline clone from other partition
1133 to support clone we are interested in. */
1134 && (!node->clone_of
1135 || node->clone_of->decl != node->decl))
1137 DECL_EXTERNAL (node->decl) = 1;
1138 TREE_STATIC (node->decl) = 0;
1140 node->alias = bp_unpack_value (bp, 1);
1141 node->transparent_alias = bp_unpack_value (bp, 1);
1142 node->weakref = bp_unpack_value (bp, 1);
1143 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1144 node->only_called_at_startup = bp_unpack_value (bp, 1);
1145 node->only_called_at_exit = bp_unpack_value (bp, 1);
1146 node->tm_clone = bp_unpack_value (bp, 1);
1147 node->calls_comdat_local = bp_unpack_value (bp, 1);
1148 node->icf_merged = bp_unpack_value (bp, 1);
1149 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1150 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1151 node->parallelized_function = bp_unpack_value (bp, 1);
1152 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1153 LDPR_NUM_KNOWN);
1154 node->split_part = bp_unpack_value (bp, 1);
1155 gcc_assert (flag_ltrans
1156 || (!node->in_other_partition
1157 && !node->used_from_other_partition));
1160 /* Return string alias is alias of. */
1162 static tree
1163 get_alias_symbol (tree decl)
1165 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1166 return get_identifier (TREE_STRING_POINTER
1167 (TREE_VALUE (TREE_VALUE (alias))));
1170 /* Read a node from input_block IB. TAG is the node's tag just read.
1171 Return the node read or overwriten. */
1173 static struct cgraph_node *
1174 input_node (struct lto_file_decl_data *file_data,
1175 struct lto_input_block *ib,
1176 enum LTO_symtab_tags tag,
1177 vec<symtab_node *> nodes)
1179 gcc::pass_manager *passes = g->get_passes ();
1180 tree fn_decl;
1181 struct cgraph_node *node;
1182 struct bitpack_d bp;
1183 unsigned decl_index;
1184 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1185 int clone_ref;
1186 int order;
1187 int i, count;
1188 tree group;
1189 const char *section;
1190 order = streamer_read_hwi (ib) + order_base;
1191 clone_ref = streamer_read_hwi (ib);
1193 decl_index = streamer_read_uhwi (ib);
1194 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1196 if (clone_ref != LCC_NOT_FOUND)
1198 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1199 profile_count::uninitialized (), false,
1200 vNULL, false, NULL, NULL);
1202 else
1204 /* Declaration of functions can be already merged with a declaration
1205 from other input file. We keep cgraph unmerged until after streaming
1206 of ipa passes is done. Alays forcingly create a fresh node. */
1207 node = symtab->create_empty ();
1208 node->decl = fn_decl;
1209 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (fn_decl)))
1210 node->ifunc_resolver = 1;
1211 node->register_symbol ();
1214 node->order = order;
1215 if (order >= symtab->order)
1216 symtab->order = order + 1;
1218 node->count = profile_count::stream_in (ib);
1219 node->count_materialization_scale = streamer_read_hwi (ib);
1221 count = streamer_read_hwi (ib);
1222 node->ipa_transforms_to_apply = vNULL;
1223 for (i = 0; i < count; i++)
1225 opt_pass *pass;
1226 int pid = streamer_read_hwi (ib);
1228 gcc_assert (pid < passes->passes_by_id_size);
1229 pass = passes->passes_by_id[pid];
1230 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1233 if (tag == LTO_symtab_analyzed_node)
1234 ref = streamer_read_hwi (ib);
1236 group = read_identifier (ib);
1237 if (group)
1238 ref2 = streamer_read_hwi (ib);
1240 /* Make sure that we have not read this node before. Nodes that
1241 have already been read will have their tag stored in the 'aux'
1242 field. Since built-in functions can be referenced in multiple
1243 functions, they are expected to be read more than once. */
1244 if (node->aux && !fndecl_built_in_p (node->decl))
1245 internal_error ("bytecode stream: found multiple instances of cgraph "
1246 "node with uid %d", node->get_uid ());
1248 node->tp_first_run = streamer_read_uhwi (ib);
1250 bp = streamer_read_bitpack (ib);
1252 input_overwrite_node (file_data, node, tag, &bp);
1254 /* Store a reference for now, and fix up later to be a pointer. */
1255 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1257 if (group)
1259 node->set_comdat_group (group);
1260 /* Store a reference for now, and fix up later to be a pointer. */
1261 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1263 else
1264 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1265 section = read_string (ib);
1266 if (section)
1267 node->set_section_for_node (section);
1269 if (node->thunk.thunk_p)
1271 int type = streamer_read_uhwi (ib);
1272 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1273 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1275 node->thunk.fixed_offset = fixed_offset;
1276 node->thunk.this_adjusting = (type & 2);
1277 node->thunk.virtual_value = virtual_value;
1278 node->thunk.virtual_offset_p = (type & 4);
1279 node->thunk.add_pointer_bounds_args = (type & 8);
1281 if (node->alias && !node->analyzed && node->weakref)
1282 node->alias_target = get_alias_symbol (node->decl);
1283 node->profile_id = streamer_read_hwi (ib);
1284 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1285 node->set_init_priority (streamer_read_hwi (ib));
1286 if (DECL_STATIC_DESTRUCTOR (node->decl))
1287 node->set_fini_priority (streamer_read_hwi (ib));
1289 return node;
1292 /* Read a node from input_block IB. TAG is the node's tag just read.
1293 Return the node read or overwriten. */
1295 static varpool_node *
1296 input_varpool_node (struct lto_file_decl_data *file_data,
1297 struct lto_input_block *ib)
1299 int decl_index;
1300 tree var_decl;
1301 varpool_node *node;
1302 struct bitpack_d bp;
1303 int ref = LCC_NOT_FOUND;
1304 int order;
1305 tree group;
1306 const char *section;
1308 order = streamer_read_hwi (ib) + order_base;
1309 decl_index = streamer_read_uhwi (ib);
1310 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1312 /* Declaration of functions can be already merged with a declaration
1313 from other input file. We keep cgraph unmerged until after streaming
1314 of ipa passes is done. Alays forcingly create a fresh node. */
1315 node = varpool_node::create_empty ();
1316 node->decl = var_decl;
1317 node->register_symbol ();
1319 node->order = order;
1320 if (order >= symtab->order)
1321 symtab->order = order + 1;
1322 node->lto_file_data = file_data;
1324 bp = streamer_read_bitpack (ib);
1325 node->externally_visible = bp_unpack_value (&bp, 1);
1326 node->no_reorder = bp_unpack_value (&bp, 1);
1327 node->force_output = bp_unpack_value (&bp, 1);
1328 node->forced_by_abi = bp_unpack_value (&bp, 1);
1329 node->unique_name = bp_unpack_value (&bp, 1);
1330 node->body_removed = bp_unpack_value (&bp, 1);
1331 node->implicit_section = bp_unpack_value (&bp, 1);
1332 node->writeonly = bp_unpack_value (&bp, 1);
1333 node->definition = bp_unpack_value (&bp, 1);
1334 node->alias = bp_unpack_value (&bp, 1);
1335 node->transparent_alias = bp_unpack_value (&bp, 1);
1336 node->weakref = bp_unpack_value (&bp, 1);
1337 node->analyzed = bp_unpack_value (&bp, 1);
1338 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1339 node->in_other_partition = bp_unpack_value (&bp, 1);
1340 if (node->in_other_partition)
1342 DECL_EXTERNAL (node->decl) = 1;
1343 TREE_STATIC (node->decl) = 0;
1345 if (node->alias && !node->analyzed && node->weakref)
1346 node->alias_target = get_alias_symbol (node->decl);
1347 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1348 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1349 node->dynamically_initialized = bp_unpack_value (&bp, 1);
1350 node->need_bounds_init = bp_unpack_value (&bp, 1);
1351 group = read_identifier (ib);
1352 if (group)
1354 node->set_comdat_group (group);
1355 ref = streamer_read_hwi (ib);
1356 /* Store a reference for now, and fix up later to be a pointer. */
1357 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1359 else
1360 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1361 section = read_string (ib);
1362 if (section)
1363 node->set_section_for_node (section);
1364 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1365 LDPR_NUM_KNOWN);
1366 gcc_assert (flag_ltrans
1367 || (!node->in_other_partition
1368 && !node->used_from_other_partition));
1370 return node;
1373 /* Read a node from input_block IB. TAG is the node's tag just read.
1374 Return the node read or overwriten. */
1376 static void
1377 input_ref (struct lto_input_block *ib,
1378 symtab_node *referring_node,
1379 vec<symtab_node *> nodes)
1381 symtab_node *node = NULL;
1382 struct bitpack_d bp;
1383 enum ipa_ref_use use;
1384 bool speculative;
1385 struct ipa_ref *ref;
1387 bp = streamer_read_bitpack (ib);
1388 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1389 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1390 node = nodes[streamer_read_hwi (ib)];
1391 ref = referring_node->create_reference (node, use);
1392 ref->speculative = speculative;
1393 if (is_a <cgraph_node *> (referring_node))
1394 ref->lto_stmt_uid = streamer_read_hwi (ib);
1397 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1398 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1399 edge being read is indirect (in the sense that it has
1400 indirect_unknown_callee set). */
1402 static void
1403 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1404 bool indirect)
1406 struct cgraph_node *caller, *callee;
1407 struct cgraph_edge *edge;
1408 unsigned int stmt_id;
1409 profile_count count;
1410 cgraph_inline_failed_t inline_failed;
1411 struct bitpack_d bp;
1412 int ecf_flags = 0;
1414 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1415 if (caller == NULL || caller->decl == NULL_TREE)
1416 internal_error ("bytecode stream: no caller found while reading edge");
1418 if (!indirect)
1420 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1421 if (callee == NULL || callee->decl == NULL_TREE)
1422 internal_error ("bytecode stream: no callee found while reading edge");
1424 else
1425 callee = NULL;
1427 count = profile_count::stream_in (ib);
1429 bp = streamer_read_bitpack (ib);
1430 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1431 stmt_id = bp_unpack_var_len_unsigned (&bp);
1433 if (indirect)
1434 edge = caller->create_indirect_edge (NULL, 0, count);
1435 else
1436 edge = caller->create_edge (callee, NULL, count);
1438 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1439 edge->speculative = bp_unpack_value (&bp, 1);
1440 edge->lto_stmt_uid = stmt_id;
1441 edge->inline_failed = inline_failed;
1442 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1443 edge->can_throw_external = bp_unpack_value (&bp, 1);
1444 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1445 if (indirect)
1447 if (bp_unpack_value (&bp, 1))
1448 ecf_flags |= ECF_CONST;
1449 if (bp_unpack_value (&bp, 1))
1450 ecf_flags |= ECF_PURE;
1451 if (bp_unpack_value (&bp, 1))
1452 ecf_flags |= ECF_NORETURN;
1453 if (bp_unpack_value (&bp, 1))
1454 ecf_flags |= ECF_MALLOC;
1455 if (bp_unpack_value (&bp, 1))
1456 ecf_flags |= ECF_NOTHROW;
1457 if (bp_unpack_value (&bp, 1))
1458 ecf_flags |= ECF_RETURNS_TWICE;
1459 edge->indirect_info->ecf_flags = ecf_flags;
1460 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1461 if (edge->indirect_info->common_target_id)
1462 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1467 /* Read a cgraph from IB using the info in FILE_DATA. */
1469 static vec<symtab_node *>
1470 input_cgraph_1 (struct lto_file_decl_data *file_data,
1471 struct lto_input_block *ib)
1473 enum LTO_symtab_tags tag;
1474 vec<symtab_node *> nodes = vNULL;
1475 symtab_node *node;
1476 unsigned i;
1478 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1479 order_base = symtab->order;
1480 while (tag)
1482 if (tag == LTO_symtab_edge)
1483 input_edge (ib, nodes, false);
1484 else if (tag == LTO_symtab_indirect_edge)
1485 input_edge (ib, nodes, true);
1486 else if (tag == LTO_symtab_variable)
1488 node = input_varpool_node (file_data, ib);
1489 nodes.safe_push (node);
1490 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1492 else
1494 node = input_node (file_data, ib, tag, nodes);
1495 if (node == NULL || node->decl == NULL_TREE)
1496 internal_error ("bytecode stream: found empty cgraph node");
1497 nodes.safe_push (node);
1498 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1501 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1504 lto_input_toplevel_asms (file_data, order_base);
1506 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1507 if (flag_checking)
1509 FOR_EACH_VEC_ELT (nodes, i, node)
1510 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1512 FOR_EACH_VEC_ELT (nodes, i, node)
1514 int ref;
1515 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1517 ref = (int) (intptr_t) cnode->global.inlined_to;
1519 /* We share declaration of builtins, so we may read same node twice. */
1520 if (!node->aux)
1521 continue;
1522 node->aux = NULL;
1524 /* Fixup inlined_to from reference to pointer. */
1525 if (ref != LCC_NOT_FOUND)
1526 dyn_cast<cgraph_node *> (node)->global.inlined_to
1527 = dyn_cast<cgraph_node *> (nodes[ref]);
1528 else
1529 cnode->global.inlined_to = NULL;
1532 ref = (int) (intptr_t) node->same_comdat_group;
1534 /* Fixup same_comdat_group from reference to pointer. */
1535 if (ref != LCC_NOT_FOUND)
1536 node->same_comdat_group = nodes[ref];
1537 else
1538 node->same_comdat_group = NULL;
1540 FOR_EACH_VEC_ELT (nodes, i, node)
1541 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1542 return nodes;
1545 /* Input ipa_refs. */
1547 static void
1548 input_refs (struct lto_input_block *ib,
1549 vec<symtab_node *> nodes)
1551 int count;
1552 int idx;
1553 while (true)
1555 symtab_node *node;
1556 count = streamer_read_uhwi (ib);
1557 if (!count)
1558 break;
1559 idx = streamer_read_uhwi (ib);
1560 node = nodes[idx];
1561 while (count)
1563 input_ref (ib, node, nodes);
1564 count--;
1569 /* Input profile_info from IB. */
1570 static void
1571 input_profile_summary (struct lto_input_block *ib,
1572 struct lto_file_decl_data *file_data)
1574 unsigned int runs = streamer_read_uhwi (ib);
1575 if (runs)
1577 file_data->profile_info.runs = runs;
1579 /* IPA-profile computes hot bb threshold based on cumulated
1580 whole program profile. We need to stream it down to ltrans. */
1581 if (flag_ltrans)
1582 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1587 /* Rescale profile summaries to the same number of runs in the whole unit. */
1589 static void
1590 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1592 struct lto_file_decl_data *file_data;
1593 unsigned int j;
1594 gcov_unsigned_t max_runs = 0;
1595 struct cgraph_node *node;
1596 struct cgraph_edge *edge;
1598 /* Find unit with maximal number of runs. If we ever get serious about
1599 roundoff errors, we might also consider computing smallest common
1600 multiply. */
1601 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1602 if (max_runs < file_data->profile_info.runs)
1603 max_runs = file_data->profile_info.runs;
1605 if (!max_runs)
1606 return;
1608 /* Simple overflow check. We probably don't need to support that many train
1609 runs. Such a large value probably imply data corruption anyway. */
1610 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1612 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1613 INT_MAX / REG_BR_PROB_BASE);
1614 return;
1617 profile_info = XCNEW (gcov_summary);
1618 profile_info->runs = max_runs;
1620 /* If merging already happent at WPA time, we are done. */
1621 if (flag_ltrans)
1622 return;
1624 /* Now compute count_materialization_scale of each node.
1625 During LTRANS we already have values of count_materialization_scale
1626 computed, so just update them. */
1627 FOR_EACH_FUNCTION (node)
1628 if (node->lto_file_data
1629 && node->lto_file_data->profile_info.runs)
1631 int scale;
1633 scale = RDIV (node->count_materialization_scale * max_runs,
1634 node->lto_file_data->profile_info.runs);
1635 node->count_materialization_scale = scale;
1636 if (scale < 0)
1637 fatal_error (input_location, "Profile information in %s corrupted",
1638 file_data->file_name);
1640 if (scale == REG_BR_PROB_BASE)
1641 continue;
1642 for (edge = node->callees; edge; edge = edge->next_callee)
1643 if (edge->count.ipa ().nonzero_p ())
1644 edge->count = edge->count.apply_scale (scale, REG_BR_PROB_BASE);
1645 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1646 if (edge->count.ipa ().nonzero_p ())
1647 edge->count = edge->count.apply_scale (scale, REG_BR_PROB_BASE);
1648 if (node->count.ipa ().nonzero_p ())
1649 node->count = node->count.apply_scale (scale, REG_BR_PROB_BASE);
1653 /* Input and merge the symtab from each of the .o files passed to
1654 lto1. */
1656 void
1657 input_symtab (void)
1659 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1660 struct lto_file_decl_data *file_data;
1661 unsigned int j = 0;
1662 struct cgraph_node *node;
1664 while ((file_data = file_data_vec[j++]))
1666 const char *data;
1667 size_t len;
1668 struct lto_input_block *ib;
1669 vec<symtab_node *> nodes;
1671 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1672 &data, &len);
1673 if (!ib)
1674 fatal_error (input_location,
1675 "cannot find LTO cgraph in %s", file_data->file_name);
1676 input_profile_summary (ib, file_data);
1677 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1678 nodes = input_cgraph_1 (file_data, ib);
1679 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1680 ib, data, len);
1682 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1683 &data, &len);
1684 if (!ib)
1685 fatal_error (input_location, "cannot find LTO section refs in %s",
1686 file_data->file_name);
1687 input_refs (ib, nodes);
1688 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1689 ib, data, len);
1690 if (flag_ltrans)
1691 input_cgraph_opt_summary (nodes);
1692 nodes.release ();
1695 merge_profile_summaries (file_data_vec);
1697 /* Clear out the aux field that was used to store enough state to
1698 tell which nodes should be overwritten. */
1699 FOR_EACH_FUNCTION (node)
1701 /* Some nodes may have been created by cgraph_node. This
1702 happens when the callgraph contains nested functions. If the
1703 node for the parent function was never emitted to the gimple
1704 file, cgraph_node will create a node for it when setting the
1705 context of the nested function. */
1706 if (node->lto_file_data)
1707 node->aux = NULL;
1711 /* Input function/variable tables that will allow libgomp to look up offload
1712 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1714 void
1715 input_offload_tables (bool do_force_output)
1717 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1718 struct lto_file_decl_data *file_data;
1719 unsigned int j = 0;
1721 while ((file_data = file_data_vec[j++]))
1723 const char *data;
1724 size_t len;
1725 struct lto_input_block *ib
1726 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1727 &data, &len);
1728 if (!ib)
1729 continue;
1731 enum LTO_symtab_tags tag
1732 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1733 while (tag)
1735 if (tag == LTO_symtab_unavail_node)
1737 int decl_index = streamer_read_uhwi (ib);
1738 tree fn_decl
1739 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1740 vec_safe_push (offload_funcs, fn_decl);
1742 /* Prevent IPA from removing fn_decl as unreachable, since there
1743 may be no refs from the parent function to child_fn in offload
1744 LTO mode. */
1745 if (do_force_output)
1746 cgraph_node::get (fn_decl)->mark_force_output ();
1748 else if (tag == LTO_symtab_variable)
1750 int decl_index = streamer_read_uhwi (ib);
1751 tree var_decl
1752 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1753 vec_safe_push (offload_vars, var_decl);
1755 /* Prevent IPA from removing var_decl as unused, since there
1756 may be no refs to var_decl in offload LTO mode. */
1757 if (do_force_output)
1758 varpool_node::get (var_decl)->force_output = 1;
1760 else
1761 fatal_error (input_location,
1762 "invalid offload table in %s", file_data->file_name);
1764 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1767 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1768 ib, data, len);
1772 /* True when we need optimization summary for NODE. */
1774 static int
1775 output_cgraph_opt_summary_p (struct cgraph_node *node)
1777 return ((node->clone_of || node->former_clone_of)
1778 && (node->clone.tree_map
1779 || node->clone.args_to_skip
1780 || node->clone.combined_args_to_skip));
1783 /* Output optimization summary for EDGE to OB. */
1784 static void
1785 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1786 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1790 /* Output optimization summary for NODE to OB. */
1792 static void
1793 output_node_opt_summary (struct output_block *ob,
1794 struct cgraph_node *node,
1795 lto_symtab_encoder_t encoder)
1797 unsigned int index;
1798 bitmap_iterator bi;
1799 struct ipa_replace_map *map;
1800 struct bitpack_d bp;
1801 int i;
1802 struct cgraph_edge *e;
1804 if (node->clone.args_to_skip)
1806 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1807 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1808 streamer_write_uhwi (ob, index);
1810 else
1811 streamer_write_uhwi (ob, 0);
1812 if (node->clone.combined_args_to_skip)
1814 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1815 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1816 streamer_write_uhwi (ob, index);
1818 else
1819 streamer_write_uhwi (ob, 0);
1820 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
1821 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
1823 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
1824 mechanism to store function local declarations into summaries. */
1825 gcc_assert (!map->old_tree);
1826 streamer_write_uhwi (ob, map->parm_num);
1827 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
1828 stream_write_tree (ob, map->new_tree, true);
1829 bp = bitpack_create (ob->main_stream);
1830 bp_pack_value (&bp, map->replace_p, 1);
1831 bp_pack_value (&bp, map->ref_p, 1);
1832 streamer_write_bitpack (&bp);
1835 if (lto_symtab_encoder_in_partition_p (encoder, node))
1837 for (e = node->callees; e; e = e->next_callee)
1838 output_edge_opt_summary (ob, e);
1839 for (e = node->indirect_calls; e; e = e->next_callee)
1840 output_edge_opt_summary (ob, e);
1844 /* Output optimization summaries stored in callgraph.
1845 At the moment it is the clone info structure. */
1847 static void
1848 output_cgraph_opt_summary (void)
1850 int i, n_nodes;
1851 lto_symtab_encoder_t encoder;
1852 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
1853 unsigned count = 0;
1855 ob->symbol = NULL;
1856 encoder = ob->decl_state->symtab_node_encoder;
1857 n_nodes = lto_symtab_encoder_size (encoder);
1858 for (i = 0; i < n_nodes; i++)
1860 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1861 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1862 if (cnode && output_cgraph_opt_summary_p (cnode))
1863 count++;
1865 streamer_write_uhwi (ob, count);
1866 for (i = 0; i < n_nodes; i++)
1868 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1869 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1870 if (cnode && output_cgraph_opt_summary_p (cnode))
1872 streamer_write_uhwi (ob, i);
1873 output_node_opt_summary (ob, cnode, encoder);
1876 produce_asm (ob, NULL);
1877 destroy_output_block (ob);
1880 /* Input optimisation summary of EDGE. */
1882 static void
1883 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
1884 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
1888 /* Input optimisation summary of NODE. */
1890 static void
1891 input_node_opt_summary (struct cgraph_node *node,
1892 struct lto_input_block *ib_main,
1893 struct data_in *data_in)
1895 int i;
1896 int count;
1897 int bit;
1898 struct bitpack_d bp;
1899 struct cgraph_edge *e;
1901 count = streamer_read_uhwi (ib_main);
1902 if (count)
1903 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
1904 for (i = 0; i < count; i++)
1906 bit = streamer_read_uhwi (ib_main);
1907 bitmap_set_bit (node->clone.args_to_skip, bit);
1909 count = streamer_read_uhwi (ib_main);
1910 if (count)
1911 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
1912 for (i = 0; i < count; i++)
1914 bit = streamer_read_uhwi (ib_main);
1915 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
1917 count = streamer_read_uhwi (ib_main);
1918 for (i = 0; i < count; i++)
1920 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
1922 vec_safe_push (node->clone.tree_map, map);
1923 map->parm_num = streamer_read_uhwi (ib_main);
1924 map->old_tree = NULL;
1925 map->new_tree = stream_read_tree (ib_main, data_in);
1926 bp = streamer_read_bitpack (ib_main);
1927 map->replace_p = bp_unpack_value (&bp, 1);
1928 map->ref_p = bp_unpack_value (&bp, 1);
1930 for (e = node->callees; e; e = e->next_callee)
1931 input_edge_opt_summary (e, ib_main);
1932 for (e = node->indirect_calls; e; e = e->next_callee)
1933 input_edge_opt_summary (e, ib_main);
1936 /* Read section in file FILE_DATA of length LEN with data DATA. */
1938 static void
1939 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
1940 const char *data, size_t len,
1941 vec<symtab_node *> nodes)
1943 const struct lto_function_header *header =
1944 (const struct lto_function_header *) data;
1945 const int cfg_offset = sizeof (struct lto_function_header);
1946 const int main_offset = cfg_offset + header->cfg_size;
1947 const int string_offset = main_offset + header->main_size;
1948 struct data_in *data_in;
1949 unsigned int i;
1950 unsigned int count;
1952 lto_input_block ib_main ((const char *) data + main_offset,
1953 header->main_size, file_data->mode_table);
1955 data_in =
1956 lto_data_in_create (file_data, (const char *) data + string_offset,
1957 header->string_size, vNULL);
1958 count = streamer_read_uhwi (&ib_main);
1960 for (i = 0; i < count; i++)
1962 int ref = streamer_read_uhwi (&ib_main);
1963 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
1964 &ib_main, data_in);
1966 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
1967 len);
1968 lto_data_in_delete (data_in);
1971 /* Input optimization summary of cgraph. */
1973 static void
1974 input_cgraph_opt_summary (vec<symtab_node *> nodes)
1976 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1977 struct lto_file_decl_data *file_data;
1978 unsigned int j = 0;
1980 while ((file_data = file_data_vec[j++]))
1982 size_t len;
1983 const char *data =
1984 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
1985 &len);
1987 if (data)
1988 input_cgraph_opt_section (file_data, data, len, nodes);