[AArch64] PR target/65491: Classify V1TF vectors as AAPCS64 short vectors rather...
[official-gcc.git] / gcc / lto-cgraph.c
blobb306c28d37bdcdfb51bf561471fd99777e7e42d5
1 /* Write and read the cgraph to the memory mapped representation of a
2 .o file.
4 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "stringpool.h"
39 #include "predict.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "hashtab.h"
49 #include "rtl.h"
50 #include "flags.h"
51 #include "statistics.h"
52 #include "real.h"
53 #include "fixed-value.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "varasm.h"
61 #include "stmt.h"
62 #include "expr.h"
63 #include "params.h"
64 #include "langhooks.h"
65 #include "bitmap.h"
66 #include "diagnostic-core.h"
67 #include "except.h"
68 #include "timevar.h"
69 #include "hash-map.h"
70 #include "plugin-api.h"
71 #include "ipa-ref.h"
72 #include "cgraph.h"
73 #include "lto-streamer.h"
74 #include "data-streamer.h"
75 #include "tree-streamer.h"
76 #include "gcov-io.h"
77 #include "tree-pass.h"
78 #include "profile.h"
79 #include "context.h"
80 #include "pass_manager.h"
81 #include "ipa-utils.h"
82 #include "omp-low.h"
83 #include "ipa-chkp.h"
85 /* True when asm nodes has been output. */
86 bool asm_nodes_output = false;
88 static void output_cgraph_opt_summary (void);
89 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
91 /* Number of LDPR values known to GCC. */
92 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
94 /* All node orders are ofsetted by ORDER_BASE. */
95 static int order_base;
97 /* Cgraph streaming is organized as set of record whose type
98 is indicated by a tag. */
99 enum LTO_symtab_tags
101 /* Must leave 0 for the stopper. */
103 /* Cgraph node without body available. */
104 LTO_symtab_unavail_node = 1,
105 /* Cgraph node with function body. */
106 LTO_symtab_analyzed_node,
107 /* Cgraph edges. */
108 LTO_symtab_edge,
109 LTO_symtab_indirect_edge,
110 LTO_symtab_variable,
111 LTO_symtab_last_tag
114 /* Create a new symtab encoder.
115 if FOR_INPUT, the encoder allocate only datastructures needed
116 to read the symtab. */
118 lto_symtab_encoder_t
119 lto_symtab_encoder_new (bool for_input)
121 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
123 if (!for_input)
124 encoder->map = new hash_map<symtab_node *, size_t>;
125 encoder->nodes.create (0);
126 return encoder;
130 /* Delete ENCODER and its components. */
132 void
133 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
135 encoder->nodes.release ();
136 if (encoder->map)
137 delete encoder->map;
138 free (encoder);
142 /* Return the existing reference number of NODE in the symtab encoder in
143 output block OB. Assign a new reference if this is the first time
144 NODE is encoded. */
147 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
148 symtab_node *node)
150 int ref;
152 if (!encoder->map)
154 lto_encoder_entry entry = {node, false, false, false};
156 ref = encoder->nodes.length ();
157 encoder->nodes.safe_push (entry);
158 return ref;
161 size_t *slot = encoder->map->get (node);
162 if (!slot || !*slot)
164 lto_encoder_entry entry = {node, false, false, false};
165 ref = encoder->nodes.length ();
166 if (!slot)
167 encoder->map->put (node, ref + 1);
168 encoder->nodes.safe_push (entry);
170 else
171 ref = *slot - 1;
173 return ref;
176 /* Remove NODE from encoder. */
178 bool
179 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
180 symtab_node *node)
182 int index;
183 lto_encoder_entry last_node;
185 size_t *slot = encoder->map->get (node);
186 if (slot == NULL || !*slot)
187 return false;
189 index = *slot - 1;
190 gcc_checking_assert (encoder->nodes[index].node == node);
192 /* Remove from vector. We do this by swapping node with the last element
193 of the vector. */
194 last_node = encoder->nodes.pop ();
195 if (last_node.node != node)
197 gcc_assert (encoder->map->put (last_node.node, index + 1));
199 /* Move the last element to the original spot of NODE. */
200 encoder->nodes[index] = last_node;
203 /* Remove element from hash table. */
204 encoder->map->remove (node);
205 return true;
209 /* Return TRUE if we should encode the body of NODE (if any). */
211 bool
212 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
213 struct cgraph_node *node)
215 int index = lto_symtab_encoder_lookup (encoder, node);
216 return encoder->nodes[index].body;
219 /* Specify that we encode the body of NODE in this partition. */
221 static void
222 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
223 struct cgraph_node *node)
225 int index = lto_symtab_encoder_encode (encoder, node);
226 gcc_checking_assert (encoder->nodes[index].node == node);
227 encoder->nodes[index].body = true;
230 /* Return TRUE if we should encode initializer of NODE (if any). */
232 bool
233 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
234 varpool_node *node)
236 int index = lto_symtab_encoder_lookup (encoder, node);
237 if (index == LCC_NOT_FOUND)
238 return false;
239 return encoder->nodes[index].initializer;
242 /* Specify that we should encode initializer of NODE (if any). */
244 static void
245 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
246 varpool_node *node)
248 int index = lto_symtab_encoder_lookup (encoder, node);
249 encoder->nodes[index].initializer = true;
252 /* Return TRUE if NODE is in this partition. */
254 bool
255 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
256 symtab_node *node)
258 int index = lto_symtab_encoder_lookup (encoder, node);
259 if (index == LCC_NOT_FOUND)
260 return false;
261 return encoder->nodes[index].in_partition;
264 /* Specify that NODE is in this partition. */
266 void
267 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
268 symtab_node *node)
270 int index = lto_symtab_encoder_encode (encoder, node);
271 encoder->nodes[index].in_partition = true;
274 /* Output the cgraph EDGE to OB using ENCODER. */
276 static void
277 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
278 lto_symtab_encoder_t encoder)
280 unsigned int uid;
281 intptr_t ref;
282 struct bitpack_d bp;
284 if (edge->indirect_unknown_callee)
285 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
286 LTO_symtab_indirect_edge);
287 else
288 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
289 LTO_symtab_edge);
291 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
292 gcc_assert (ref != LCC_NOT_FOUND);
293 streamer_write_hwi_stream (ob->main_stream, ref);
295 if (!edge->indirect_unknown_callee)
297 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
298 gcc_assert (ref != LCC_NOT_FOUND);
299 streamer_write_hwi_stream (ob->main_stream, ref);
302 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
304 bp = bitpack_create (ob->main_stream);
305 uid = (!gimple_has_body_p (edge->caller->decl)
306 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
307 bp_pack_enum (&bp, cgraph_inline_failed_t,
308 CIF_N_REASONS, edge->inline_failed);
309 bp_pack_var_len_unsigned (&bp, uid);
310 bp_pack_var_len_unsigned (&bp, edge->frequency);
311 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
312 bp_pack_value (&bp, edge->speculative, 1);
313 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
314 bp_pack_value (&bp, edge->can_throw_external, 1);
315 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
316 if (edge->indirect_unknown_callee)
318 int flags = edge->indirect_info->ecf_flags;
319 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
320 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
321 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
322 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
323 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
324 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
325 /* Flags that should not appear on indirect calls. */
326 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
327 | ECF_MAY_BE_ALLOCA
328 | ECF_SIBCALL
329 | ECF_LEAF
330 | ECF_NOVOPS)));
332 streamer_write_bitpack (&bp);
333 if (edge->indirect_unknown_callee)
335 streamer_write_hwi_stream (ob->main_stream,
336 edge->indirect_info->common_target_id);
337 if (edge->indirect_info->common_target_id)
338 streamer_write_hwi_stream
339 (ob->main_stream, edge->indirect_info->common_target_probability);
343 /* Return if NODE contain references from other partitions. */
345 bool
346 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
348 int i;
349 struct ipa_ref *ref = NULL;
351 for (i = 0; node->iterate_referring (i, ref); i++)
353 /* Ignore references from non-offloadable nodes while streaming NODE into
354 offload LTO section. */
355 if (!ref->referring->need_lto_streaming)
356 continue;
358 if (ref->referring->in_other_partition
359 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
360 return true;
362 return false;
365 /* Return true when node is reachable from other partition. */
367 bool
368 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
370 struct cgraph_edge *e;
371 if (!node->definition)
372 return false;
373 if (node->global.inlined_to)
374 return false;
375 for (e = node->callers; e; e = e->next_caller)
377 /* Ignore references from non-offloadable nodes while streaming NODE into
378 offload LTO section. */
379 if (!e->caller->need_lto_streaming)
380 continue;
382 if (e->caller->in_other_partition
383 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
384 return true;
386 return false;
389 /* Return if NODE contain references from other partitions. */
391 bool
392 referenced_from_this_partition_p (symtab_node *node,
393 lto_symtab_encoder_t encoder)
395 int i;
396 struct ipa_ref *ref = NULL;
398 for (i = 0; node->iterate_referring (i, ref); i++)
399 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
400 return true;
401 return false;
404 /* Return true when node is reachable from other partition. */
406 bool
407 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
409 struct cgraph_edge *e;
410 for (e = node->callers; e; e = e->next_caller)
411 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
412 return true;
413 return false;
416 /* Output the cgraph NODE to OB. ENCODER is used to find the
417 reference number of NODE->inlined_to. SET is the set of nodes we
418 are writing to the current file. If NODE is not in SET, then NODE
419 is a boundary of a cgraph_node_set and we pretend NODE just has a
420 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
421 that have had their callgraph node written so far. This is used to
422 determine if NODE is a clone of a previously written node. */
424 static void
425 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
426 lto_symtab_encoder_t encoder)
428 unsigned int tag;
429 struct bitpack_d bp;
430 bool boundary_p;
431 intptr_t ref;
432 bool in_other_partition = false;
433 struct cgraph_node *clone_of, *ultimate_clone_of;
434 ipa_opt_pass_d *pass;
435 int i;
436 const char *comdat;
437 const char *section;
438 tree group;
440 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
442 if (node->analyzed && (!boundary_p || node->alias || node->thunk.thunk_p))
443 tag = LTO_symtab_analyzed_node;
444 else
445 tag = LTO_symtab_unavail_node;
447 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
448 tag);
449 streamer_write_hwi_stream (ob->main_stream, node->order);
451 /* In WPA mode, we only output part of the call-graph. Also, we
452 fake cgraph node attributes. There are two cases that we care.
454 Boundary nodes: There are nodes that are not part of SET but are
455 called from within SET. We artificially make them look like
456 externally visible nodes with no function body.
458 Cherry-picked nodes: These are nodes we pulled from other
459 translation units into SET during IPA-inlining. We make them as
460 local static nodes to prevent clashes with other local statics. */
461 if (boundary_p && node->analyzed
462 && node->get_partitioning_class () == SYMBOL_PARTITION)
464 /* Inline clones can not be part of boundary.
465 gcc_assert (!node->global.inlined_to);
467 FIXME: At the moment they can be, when partition contains an inline
468 clone that is clone of inline clone from outside partition. We can
469 reshape the clone tree and make other tree to be the root, but it
470 needs a bit extra work and will be promplty done by cgraph_remove_node
471 after reading back. */
472 in_other_partition = 1;
475 clone_of = node->clone_of;
476 while (clone_of
477 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
478 if (clone_of->prev_sibling_clone)
479 clone_of = clone_of->prev_sibling_clone;
480 else
481 clone_of = clone_of->clone_of;
483 /* See if body of the master function is output. If not, we are seeing only
484 an declaration and we do not need to pass down clone tree. */
485 ultimate_clone_of = clone_of;
486 while (ultimate_clone_of && ultimate_clone_of->clone_of)
487 ultimate_clone_of = ultimate_clone_of->clone_of;
489 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
490 clone_of = NULL;
492 if (tag == LTO_symtab_analyzed_node)
493 gcc_assert (clone_of || !node->clone_of);
494 if (!clone_of)
495 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
496 else
497 streamer_write_hwi_stream (ob->main_stream, ref);
500 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
501 streamer_write_gcov_count_stream (ob->main_stream, node->count);
502 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
504 streamer_write_hwi_stream (ob->main_stream,
505 node->ipa_transforms_to_apply.length ());
506 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
507 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
509 if (tag == LTO_symtab_analyzed_node)
511 if (node->global.inlined_to)
513 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
514 gcc_assert (ref != LCC_NOT_FOUND);
516 else
517 ref = LCC_NOT_FOUND;
519 streamer_write_hwi_stream (ob->main_stream, ref);
522 group = node->get_comdat_group ();
523 if (group)
524 comdat = IDENTIFIER_POINTER (group);
525 else
526 comdat = "";
527 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
529 if (group)
531 if (node->same_comdat_group && !boundary_p)
533 ref = lto_symtab_encoder_lookup (encoder,
534 node->same_comdat_group);
535 gcc_assert (ref != LCC_NOT_FOUND);
537 else
538 ref = LCC_NOT_FOUND;
539 streamer_write_hwi_stream (ob->main_stream, ref);
542 section = node->get_section ();
543 if (!section)
544 section = "";
546 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
548 bp = bitpack_create (ob->main_stream);
549 bp_pack_value (&bp, node->local.local, 1);
550 bp_pack_value (&bp, node->externally_visible, 1);
551 bp_pack_value (&bp, node->no_reorder, 1);
552 bp_pack_value (&bp, node->definition, 1);
553 bp_pack_value (&bp, node->local.versionable, 1);
554 bp_pack_value (&bp, node->local.can_change_signature, 1);
555 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
556 bp_pack_value (&bp, node->force_output, 1);
557 bp_pack_value (&bp, node->forced_by_abi, 1);
558 bp_pack_value (&bp, node->unique_name, 1);
559 bp_pack_value (&bp, node->body_removed, 1);
560 bp_pack_value (&bp, node->implicit_section, 1);
561 bp_pack_value (&bp, node->address_taken, 1);
562 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
563 && node->get_partitioning_class () == SYMBOL_PARTITION
564 && (reachable_from_other_partition_p (node, encoder)
565 || referenced_from_other_partition_p (node, encoder)), 1);
566 bp_pack_value (&bp, node->lowered, 1);
567 bp_pack_value (&bp, in_other_partition, 1);
568 bp_pack_value (&bp, node->alias, 1);
569 bp_pack_value (&bp, node->weakref, 1);
570 bp_pack_value (&bp, node->frequency, 2);
571 bp_pack_value (&bp, node->only_called_at_startup, 1);
572 bp_pack_value (&bp, node->only_called_at_exit, 1);
573 bp_pack_value (&bp, node->tm_clone, 1);
574 bp_pack_value (&bp, node->calls_comdat_local, 1);
575 bp_pack_value (&bp, node->icf_merged, 1);
576 bp_pack_value (&bp, node->nonfreeing_fn, 1);
577 bp_pack_value (&bp, node->thunk.thunk_p, 1);
578 bp_pack_value (&bp, node->parallelized_function, 1);
579 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
580 LDPR_NUM_KNOWN, node->resolution);
581 bp_pack_value (&bp, node->instrumentation_clone, 1);
582 bp_pack_value (&bp, node->split_part, 1);
583 streamer_write_bitpack (&bp);
584 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
586 if (node->thunk.thunk_p)
588 streamer_write_uhwi_stream
589 (ob->main_stream,
590 1 + (node->thunk.this_adjusting != 0) * 2
591 + (node->thunk.virtual_offset_p != 0) * 4
592 + (node->thunk.add_pointer_bounds_args != 0) * 8);
593 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
594 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
596 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
597 if (DECL_STATIC_CONSTRUCTOR (node->decl))
598 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
599 if (DECL_STATIC_DESTRUCTOR (node->decl))
600 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
602 if (node->instrumentation_clone)
603 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
606 /* Output the varpool NODE to OB.
607 If NODE is not in SET, then NODE is a boundary. */
609 static void
610 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
611 lto_symtab_encoder_t encoder)
613 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
614 bool encode_initializer_p
615 = (node->definition
616 && lto_symtab_encoder_encode_initializer_p (encoder, node));
617 struct bitpack_d bp;
618 int ref;
619 const char *comdat;
620 const char *section;
621 tree group;
623 gcc_assert (!encode_initializer_p || node->definition);
624 gcc_assert (boundary_p || encode_initializer_p);
626 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
627 LTO_symtab_variable);
628 streamer_write_hwi_stream (ob->main_stream, node->order);
629 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
630 bp = bitpack_create (ob->main_stream);
631 bp_pack_value (&bp, node->externally_visible, 1);
632 bp_pack_value (&bp, node->no_reorder, 1);
633 bp_pack_value (&bp, node->force_output, 1);
634 bp_pack_value (&bp, node->forced_by_abi, 1);
635 bp_pack_value (&bp, node->unique_name, 1);
636 bp_pack_value (&bp,
637 node->body_removed
638 || (!encode_initializer_p && !node->alias && node->definition),
640 bp_pack_value (&bp, node->implicit_section, 1);
641 bp_pack_value (&bp, node->writeonly, 1);
642 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
644 bp_pack_value (&bp, node->alias, 1);
645 bp_pack_value (&bp, node->weakref, 1);
646 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
647 gcc_assert (node->definition || !node->analyzed);
648 /* Constant pool initializers can be de-unified into individual ltrans units.
649 FIXME: Alternatively at -Os we may want to avoid generating for them the local
650 labels and share them across LTRANS partitions. */
651 if (node->get_partitioning_class () != SYMBOL_PARTITION)
653 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
654 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
656 else
658 bp_pack_value (&bp, node->definition
659 && referenced_from_other_partition_p (node, encoder), 1);
660 bp_pack_value (&bp, node->analyzed
661 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
662 /* in_other_partition. */
664 bp_pack_value (&bp, node->tls_model, 3);
665 bp_pack_value (&bp, node->used_by_single_function, 1);
666 bp_pack_value (&bp, node->need_bounds_init, 1);
667 streamer_write_bitpack (&bp);
669 group = node->get_comdat_group ();
670 if (group)
671 comdat = IDENTIFIER_POINTER (group);
672 else
673 comdat = "";
674 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
676 if (group)
678 if (node->same_comdat_group && !boundary_p)
680 ref = lto_symtab_encoder_lookup (encoder,
681 node->same_comdat_group);
682 gcc_assert (ref != LCC_NOT_FOUND);
684 else
685 ref = LCC_NOT_FOUND;
686 streamer_write_hwi_stream (ob->main_stream, ref);
689 section = node->get_section ();
690 if (!section)
691 section = "";
692 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
694 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
695 LDPR_NUM_KNOWN, node->resolution);
698 /* Output the varpool NODE to OB.
699 If NODE is not in SET, then NODE is a boundary. */
701 static void
702 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
703 lto_symtab_encoder_t encoder)
705 struct bitpack_d bp;
706 int nref;
707 int uid = ref->lto_stmt_uid;
708 struct cgraph_node *node;
710 bp = bitpack_create (ob->main_stream);
711 bp_pack_value (&bp, ref->use, 3);
712 bp_pack_value (&bp, ref->speculative, 1);
713 streamer_write_bitpack (&bp);
714 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
715 gcc_assert (nref != LCC_NOT_FOUND);
716 streamer_write_hwi_stream (ob->main_stream, nref);
718 node = dyn_cast <cgraph_node *> (ref->referring);
719 if (node)
721 if (ref->stmt)
722 uid = gimple_uid (ref->stmt) + 1;
723 streamer_write_hwi_stream (ob->main_stream, uid);
727 /* Stream out profile_summary to OB. */
729 static void
730 output_profile_summary (struct lto_simple_output_block *ob)
732 unsigned h_ix;
733 struct bitpack_d bp;
735 if (profile_info)
737 /* We do not output num and run_max, they are not used by
738 GCC profile feedback and they are difficult to merge from multiple
739 units. */
740 gcc_assert (profile_info->runs);
741 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
742 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
744 /* sum_all is needed for computing the working set with the
745 histogram. */
746 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
748 /* Create and output a bitpack of non-zero histogram entries indices. */
749 bp = bitpack_create (ob->main_stream);
750 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
751 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
752 streamer_write_bitpack (&bp);
753 /* Now stream out only those non-zero entries. */
754 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
756 if (!profile_info->histogram[h_ix].num_counters)
757 continue;
758 streamer_write_gcov_count_stream (ob->main_stream,
759 profile_info->histogram[h_ix].num_counters);
760 streamer_write_gcov_count_stream (ob->main_stream,
761 profile_info->histogram[h_ix].min_value);
762 streamer_write_gcov_count_stream (ob->main_stream,
763 profile_info->histogram[h_ix].cum_value);
765 /* IPA-profile computes hot bb threshold based on cumulated
766 whole program profile. We need to stream it down to ltrans. */
767 if (flag_wpa)
768 streamer_write_gcov_count_stream (ob->main_stream,
769 get_hot_bb_threshold ());
771 else
772 streamer_write_uhwi_stream (ob->main_stream, 0);
775 /* Output all callees or indirect outgoing edges. EDGE must be the first such
776 edge. */
778 static void
779 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
780 struct lto_simple_output_block *ob,
781 lto_symtab_encoder_t encoder)
783 if (!edge)
784 return;
786 /* Output edges in backward direction, so the reconstructed callgraph match
787 and it is easy to associate call sites in the IPA pass summaries. */
788 while (edge->next_callee)
789 edge = edge->next_callee;
790 for (; edge; edge = edge->prev_callee)
791 lto_output_edge (ob, edge, encoder);
794 /* Output the part of the cgraph in SET. */
796 static void
797 output_refs (lto_symtab_encoder_t encoder)
799 struct lto_simple_output_block *ob;
800 int count;
801 struct ipa_ref *ref;
803 ob = lto_create_simple_output_block (LTO_section_refs);
805 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
807 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
809 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
810 continue;
812 count = node->ref_list.nreferences ();
813 if (count)
815 streamer_write_gcov_count_stream (ob->main_stream, count);
816 streamer_write_uhwi_stream (ob->main_stream,
817 lto_symtab_encoder_lookup (encoder, node));
818 for (int i = 0; node->iterate_reference (i, ref); i++)
819 lto_output_ref (ob, ref, encoder);
823 streamer_write_uhwi_stream (ob->main_stream, 0);
825 lto_destroy_simple_output_block (ob);
828 /* Add NODE into encoder as well as nodes it is cloned from.
829 Do it in a way so clones appear first. */
831 static void
832 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
833 bool include_body)
835 if (node->clone_of)
836 add_node_to (encoder, node->clone_of, include_body);
837 else if (include_body)
838 lto_set_symtab_encoder_encode_body (encoder, node);
839 lto_symtab_encoder_encode (encoder, node);
842 /* Add all references in NODE to encoders. */
844 static void
845 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
847 int i;
848 struct ipa_ref *ref = NULL;
849 for (i = 0; node->iterate_reference (i, ref); i++)
850 if (is_a <cgraph_node *> (ref->referred))
851 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
852 else
853 lto_symtab_encoder_encode (encoder, ref->referred);
856 /* Select what needs to be streamed out. In regular lto mode stream everything.
857 In offload lto mode stream only nodes marked as offloadable. */
858 void
859 select_what_to_stream (void)
861 struct symtab_node *snode;
862 FOR_EACH_SYMBOL (snode)
863 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
866 /* Find all symbols we want to stream into given partition and insert them
867 to encoders.
869 The function actually replaces IN_ENCODER by new one. The reason is that
870 streaming code needs clone's origin to be streamed before clone. This
871 means that we need to insert the nodes in specific order. This order is
872 ignored by the partitioning logic earlier. */
874 lto_symtab_encoder_t
875 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
877 struct cgraph_edge *edge;
878 int i;
879 lto_symtab_encoder_t encoder;
880 lto_symtab_encoder_iterator lsei;
881 hash_set<void *> reachable_call_targets;
883 encoder = lto_symtab_encoder_new (false);
885 /* Go over all entries in the IN_ENCODER and duplicate them to
886 ENCODER. At the same time insert masters of clones so
887 every master appears before clone. */
888 for (lsei = lsei_start_function_in_partition (in_encoder);
889 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
891 struct cgraph_node *node = lsei_cgraph_node (lsei);
892 if (!node->need_lto_streaming)
893 continue;
894 add_node_to (encoder, node, true);
895 lto_set_symtab_encoder_in_partition (encoder, node);
896 create_references (encoder, node);
897 /* For proper debug info, we need to ship the origins, too. */
898 if (DECL_ABSTRACT_ORIGIN (node->decl))
900 struct cgraph_node *origin_node
901 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
902 origin_node->used_as_abstract_origin = true;
903 add_node_to (encoder, origin_node, true);
906 for (lsei = lsei_start_variable_in_partition (in_encoder);
907 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
909 varpool_node *vnode = lsei_varpool_node (lsei);
911 if (!vnode->need_lto_streaming)
912 continue;
913 lto_set_symtab_encoder_in_partition (encoder, vnode);
914 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
915 create_references (encoder, vnode);
916 /* For proper debug info, we need to ship the origins, too. */
917 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
919 varpool_node *origin_node
920 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
921 lto_set_symtab_encoder_in_partition (encoder, origin_node);
924 /* Pickle in also the initializer of all referenced readonly variables
925 to help folding. Constant pool variables are not shared, so we must
926 pickle those too. */
927 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
929 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
930 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
932 if (!lto_symtab_encoder_encode_initializer_p (encoder,
933 vnode)
934 && (((vnode->ctor_useable_for_folding_p ()
935 && (!DECL_VIRTUAL_P (vnode->decl)
936 || !flag_wpa
937 || flag_ltrans_devirtualize))
938 || POINTER_BOUNDS_P (vnode->decl))))
940 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
941 create_references (encoder, vnode);
946 /* Go over all the nodes again to include callees that are not in
947 SET. */
948 for (lsei = lsei_start_function_in_partition (encoder);
949 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
951 struct cgraph_node *node = lsei_cgraph_node (lsei);
952 for (edge = node->callees; edge; edge = edge->next_callee)
954 struct cgraph_node *callee = edge->callee;
955 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
957 /* We should have moved all the inlines. */
958 gcc_assert (!callee->global.inlined_to);
959 add_node_to (encoder, callee, false);
962 /* Add all possible targets for late devirtualization. */
963 if (flag_ltrans_devirtualize || !flag_wpa)
964 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
965 if (edge->indirect_info->polymorphic)
967 unsigned int i;
968 void *cache_token;
969 bool final;
970 vec <cgraph_node *>targets
971 = possible_polymorphic_call_targets
972 (edge, &final, &cache_token);
973 if (!reachable_call_targets.add (cache_token))
975 for (i = 0; i < targets.length (); i++)
977 struct cgraph_node *callee = targets[i];
979 /* Adding an external declarations into the unit serves
980 no purpose and just increases its boundary. */
981 if (callee->definition
982 && !lto_symtab_encoder_in_partition_p
983 (encoder, callee))
985 gcc_assert (!callee->global.inlined_to);
986 add_node_to (encoder, callee, false);
992 /* Be sure to also insert alias targert and thunk callees. These needs
993 to stay to aid local calling conventions. */
994 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
996 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
997 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
999 if (node->alias && node->analyzed)
1000 create_references (encoder, node);
1001 if (cnode
1002 && cnode->thunk.thunk_p)
1003 add_node_to (encoder, cnode->callees->callee, false);
1005 lto_symtab_encoder_delete (in_encoder);
1006 return encoder;
1009 /* Output the part of the symtab in SET and VSET. */
1011 void
1012 output_symtab (void)
1014 struct cgraph_node *node;
1015 struct lto_simple_output_block *ob;
1016 int i, n_nodes;
1017 lto_symtab_encoder_t encoder;
1019 if (flag_wpa)
1020 output_cgraph_opt_summary ();
1022 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
1024 output_profile_summary (ob);
1026 /* An encoder for cgraph nodes should have been created by
1027 ipa_write_summaries_1. */
1028 gcc_assert (ob->decl_state->symtab_node_encoder);
1029 encoder = ob->decl_state->symtab_node_encoder;
1031 /* Write out the nodes. We must first output a node and then its clones,
1032 otherwise at a time reading back the node there would be nothing to clone
1033 from. */
1034 n_nodes = lto_symtab_encoder_size (encoder);
1035 for (i = 0; i < n_nodes; i++)
1037 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1038 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1039 lto_output_node (ob, cnode, encoder);
1040 else
1041 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1044 /* Go over the nodes in SET again to write edges. */
1045 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1047 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1048 if (node
1049 && (node->thunk.thunk_p
1050 || lto_symtab_encoder_in_partition_p (encoder, node)))
1052 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1053 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1057 streamer_write_uhwi_stream (ob->main_stream, 0);
1059 lto_destroy_simple_output_block (ob);
1061 /* Emit toplevel asms.
1062 When doing WPA we must output every asm just once. Since we do not partition asm
1063 nodes at all, output them to first output. This is kind of hack, but should work
1064 well. */
1065 if (!asm_nodes_output)
1067 asm_nodes_output = true;
1068 lto_output_toplevel_asms ();
1071 output_refs (encoder);
1074 /* Return identifier encoded in IB as a plain string. */
1076 static tree
1077 read_identifier (struct lto_input_block *ib)
1079 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1080 tree id;
1082 if (ib->data[ib->p + len])
1083 lto_section_overrun (ib);
1084 if (!len)
1086 ib->p++;
1087 return NULL;
1089 id = get_identifier (ib->data + ib->p);
1090 ib->p += len + 1;
1091 return id;
1094 /* Return string encoded in IB, NULL if string is empty. */
1096 static const char *
1097 read_string (struct lto_input_block *ib)
1099 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1100 const char *str;
1102 if (ib->data[ib->p + len])
1103 lto_section_overrun (ib);
1104 if (!len)
1106 ib->p++;
1107 return NULL;
1109 str = ib->data + ib->p;
1110 ib->p += len + 1;
1111 return str;
1114 /* Output function/variable tables that will allow libgomp to look up offload
1115 target code.
1116 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1117 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1118 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1120 void
1121 output_offload_tables (void)
1123 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1124 return;
1126 struct lto_simple_output_block *ob
1127 = lto_create_simple_output_block (LTO_section_offload_table);
1129 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1131 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1132 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1133 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1134 (*offload_funcs)[i]);
1137 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1139 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1140 LTO_symtab_last_tag, LTO_symtab_variable);
1141 lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1142 (*offload_vars)[i]);
1145 streamer_write_uhwi_stream (ob->main_stream, 0);
1146 lto_destroy_simple_output_block (ob);
1148 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1149 streamed to one partition only. That's why we free offload_funcs and
1150 offload_vars after the first call of output_offload_tables. */
1151 if (flag_wpa)
1153 vec_free (offload_funcs);
1154 vec_free (offload_vars);
1158 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1159 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1160 NODE or to replace the values in it, for instance because the first
1161 time we saw it, the function body was not available but now it
1162 is. BP is a bitpack with all the bitflags for NODE read from the
1163 stream. */
1165 static void
1166 input_overwrite_node (struct lto_file_decl_data *file_data,
1167 struct cgraph_node *node,
1168 enum LTO_symtab_tags tag,
1169 struct bitpack_d *bp)
1171 node->aux = (void *) tag;
1172 node->lto_file_data = file_data;
1174 node->local.local = bp_unpack_value (bp, 1);
1175 node->externally_visible = bp_unpack_value (bp, 1);
1176 node->no_reorder = bp_unpack_value (bp, 1);
1177 node->definition = bp_unpack_value (bp, 1);
1178 node->local.versionable = bp_unpack_value (bp, 1);
1179 node->local.can_change_signature = bp_unpack_value (bp, 1);
1180 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1181 node->force_output = bp_unpack_value (bp, 1);
1182 node->forced_by_abi = bp_unpack_value (bp, 1);
1183 node->unique_name = bp_unpack_value (bp, 1);
1184 node->body_removed = bp_unpack_value (bp, 1);
1185 node->implicit_section = bp_unpack_value (bp, 1);
1186 node->address_taken = bp_unpack_value (bp, 1);
1187 node->used_from_other_partition = bp_unpack_value (bp, 1);
1188 node->lowered = bp_unpack_value (bp, 1);
1189 node->analyzed = tag == LTO_symtab_analyzed_node;
1190 node->in_other_partition = bp_unpack_value (bp, 1);
1191 if (node->in_other_partition
1192 /* Avoid updating decl when we are seeing just inline clone.
1193 When inlining function that has functions already inlined into it,
1194 we produce clones of inline clones.
1196 WPA partitioning might put each clone into different unit and
1197 we might end up streaming inline clone from other partition
1198 to support clone we are interested in. */
1199 && (!node->clone_of
1200 || node->clone_of->decl != node->decl))
1202 DECL_EXTERNAL (node->decl) = 1;
1203 TREE_STATIC (node->decl) = 0;
1205 node->alias = bp_unpack_value (bp, 1);
1206 node->weakref = bp_unpack_value (bp, 1);
1207 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1208 node->only_called_at_startup = bp_unpack_value (bp, 1);
1209 node->only_called_at_exit = bp_unpack_value (bp, 1);
1210 node->tm_clone = bp_unpack_value (bp, 1);
1211 node->calls_comdat_local = bp_unpack_value (bp, 1);
1212 node->icf_merged = bp_unpack_value (bp, 1);
1213 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1214 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1215 node->parallelized_function = bp_unpack_value (bp, 1);
1216 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1217 LDPR_NUM_KNOWN);
1218 node->instrumentation_clone = bp_unpack_value (bp, 1);
1219 node->split_part = bp_unpack_value (bp, 1);
1220 gcc_assert (flag_ltrans
1221 || (!node->in_other_partition
1222 && !node->used_from_other_partition));
1225 /* Return string alias is alias of. */
1227 static tree
1228 get_alias_symbol (tree decl)
1230 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1231 return get_identifier (TREE_STRING_POINTER
1232 (TREE_VALUE (TREE_VALUE (alias))));
1235 /* Read a node from input_block IB. TAG is the node's tag just read.
1236 Return the node read or overwriten. */
1238 static struct cgraph_node *
1239 input_node (struct lto_file_decl_data *file_data,
1240 struct lto_input_block *ib,
1241 enum LTO_symtab_tags tag,
1242 vec<symtab_node *> nodes)
1244 gcc::pass_manager *passes = g->get_passes ();
1245 tree fn_decl;
1246 struct cgraph_node *node;
1247 struct bitpack_d bp;
1248 unsigned decl_index;
1249 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1250 int clone_ref;
1251 int order;
1252 int i, count;
1253 tree group;
1254 const char *section;
1255 order = streamer_read_hwi (ib) + order_base;
1256 clone_ref = streamer_read_hwi (ib);
1258 decl_index = streamer_read_uhwi (ib);
1259 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1261 if (clone_ref != LCC_NOT_FOUND)
1263 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1264 0, CGRAPH_FREQ_BASE, false,
1265 vNULL, false, NULL, NULL);
1267 else
1269 /* Declaration of functions can be already merged with a declaration
1270 from other input file. We keep cgraph unmerged until after streaming
1271 of ipa passes is done. Alays forcingly create a fresh node. */
1272 node = symtab->create_empty ();
1273 node->decl = fn_decl;
1274 node->register_symbol ();
1277 node->order = order;
1278 if (order >= symtab->order)
1279 symtab->order = order + 1;
1281 node->count = streamer_read_gcov_count (ib);
1282 node->count_materialization_scale = streamer_read_hwi (ib);
1284 count = streamer_read_hwi (ib);
1285 node->ipa_transforms_to_apply = vNULL;
1286 for (i = 0; i < count; i++)
1288 opt_pass *pass;
1289 int pid = streamer_read_hwi (ib);
1291 gcc_assert (pid < passes->passes_by_id_size);
1292 pass = passes->passes_by_id[pid];
1293 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1296 if (tag == LTO_symtab_analyzed_node)
1297 ref = streamer_read_hwi (ib);
1299 group = read_identifier (ib);
1300 if (group)
1301 ref2 = streamer_read_hwi (ib);
1303 /* Make sure that we have not read this node before. Nodes that
1304 have already been read will have their tag stored in the 'aux'
1305 field. Since built-in functions can be referenced in multiple
1306 functions, they are expected to be read more than once. */
1307 if (node->aux && !DECL_BUILT_IN (node->decl))
1308 internal_error ("bytecode stream: found multiple instances of cgraph "
1309 "node with uid %d", node->uid);
1311 node->tp_first_run = streamer_read_uhwi (ib);
1313 bp = streamer_read_bitpack (ib);
1315 input_overwrite_node (file_data, node, tag, &bp);
1317 /* Store a reference for now, and fix up later to be a pointer. */
1318 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1320 if (group)
1322 node->set_comdat_group (group);
1323 /* Store a reference for now, and fix up later to be a pointer. */
1324 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1326 else
1327 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1328 section = read_string (ib);
1329 if (section)
1330 node->set_section_for_node (section);
1332 if (node->thunk.thunk_p)
1334 int type = streamer_read_uhwi (ib);
1335 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1336 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1338 node->thunk.fixed_offset = fixed_offset;
1339 node->thunk.this_adjusting = (type & 2);
1340 node->thunk.virtual_value = virtual_value;
1341 node->thunk.virtual_offset_p = (type & 4);
1342 node->thunk.add_pointer_bounds_args = (type & 8);
1344 if (node->alias && !node->analyzed && node->weakref)
1345 node->alias_target = get_alias_symbol (node->decl);
1346 node->profile_id = streamer_read_hwi (ib);
1347 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1348 node->set_init_priority (streamer_read_hwi (ib));
1349 if (DECL_STATIC_DESTRUCTOR (node->decl))
1350 node->set_fini_priority (streamer_read_hwi (ib));
1352 if (node->instrumentation_clone)
1354 decl_index = streamer_read_uhwi (ib);
1355 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1356 node->orig_decl = fn_decl;
1359 return node;
1362 /* Read a node from input_block IB. TAG is the node's tag just read.
1363 Return the node read or overwriten. */
1365 static varpool_node *
1366 input_varpool_node (struct lto_file_decl_data *file_data,
1367 struct lto_input_block *ib)
1369 int decl_index;
1370 tree var_decl;
1371 varpool_node *node;
1372 struct bitpack_d bp;
1373 int ref = LCC_NOT_FOUND;
1374 int order;
1375 tree group;
1376 const char *section;
1378 order = streamer_read_hwi (ib) + order_base;
1379 decl_index = streamer_read_uhwi (ib);
1380 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1382 /* Declaration of functions can be already merged with a declaration
1383 from other input file. We keep cgraph unmerged until after streaming
1384 of ipa passes is done. Alays forcingly create a fresh node. */
1385 node = varpool_node::create_empty ();
1386 node->decl = var_decl;
1387 node->register_symbol ();
1389 node->order = order;
1390 if (order >= symtab->order)
1391 symtab->order = order + 1;
1392 node->lto_file_data = file_data;
1394 bp = streamer_read_bitpack (ib);
1395 node->externally_visible = bp_unpack_value (&bp, 1);
1396 node->no_reorder = bp_unpack_value (&bp, 1);
1397 node->force_output = bp_unpack_value (&bp, 1);
1398 node->forced_by_abi = bp_unpack_value (&bp, 1);
1399 node->unique_name = bp_unpack_value (&bp, 1);
1400 node->body_removed = bp_unpack_value (&bp, 1);
1401 node->implicit_section = bp_unpack_value (&bp, 1);
1402 node->writeonly = bp_unpack_value (&bp, 1);
1403 node->definition = bp_unpack_value (&bp, 1);
1404 node->alias = bp_unpack_value (&bp, 1);
1405 node->weakref = bp_unpack_value (&bp, 1);
1406 node->analyzed = bp_unpack_value (&bp, 1);
1407 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1408 node->in_other_partition = bp_unpack_value (&bp, 1);
1409 if (node->in_other_partition)
1411 DECL_EXTERNAL (node->decl) = 1;
1412 TREE_STATIC (node->decl) = 0;
1414 if (node->alias && !node->analyzed && node->weakref)
1415 node->alias_target = get_alias_symbol (node->decl);
1416 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1417 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1418 node->need_bounds_init = bp_unpack_value (&bp, 1);
1419 group = read_identifier (ib);
1420 if (group)
1422 node->set_comdat_group (group);
1423 ref = streamer_read_hwi (ib);
1424 /* Store a reference for now, and fix up later to be a pointer. */
1425 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1427 else
1428 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1429 section = read_string (ib);
1430 if (section)
1431 node->set_section_for_node (section);
1432 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1433 LDPR_NUM_KNOWN);
1434 gcc_assert (flag_ltrans
1435 || (!node->in_other_partition
1436 && !node->used_from_other_partition));
1438 return node;
1441 /* Read a node from input_block IB. TAG is the node's tag just read.
1442 Return the node read or overwriten. */
1444 static void
1445 input_ref (struct lto_input_block *ib,
1446 symtab_node *referring_node,
1447 vec<symtab_node *> nodes)
1449 symtab_node *node = NULL;
1450 struct bitpack_d bp;
1451 enum ipa_ref_use use;
1452 bool speculative;
1453 struct ipa_ref *ref;
1455 bp = streamer_read_bitpack (ib);
1456 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1457 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1458 node = nodes[streamer_read_hwi (ib)];
1459 ref = referring_node->create_reference (node, use);
1460 ref->speculative = speculative;
1461 if (is_a <cgraph_node *> (referring_node))
1462 ref->lto_stmt_uid = streamer_read_hwi (ib);
1465 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1466 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1467 edge being read is indirect (in the sense that it has
1468 indirect_unknown_callee set). */
1470 static void
1471 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1472 bool indirect)
1474 struct cgraph_node *caller, *callee;
1475 struct cgraph_edge *edge;
1476 unsigned int stmt_id;
1477 gcov_type count;
1478 int freq;
1479 cgraph_inline_failed_t inline_failed;
1480 struct bitpack_d bp;
1481 int ecf_flags = 0;
1483 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1484 if (caller == NULL || caller->decl == NULL_TREE)
1485 internal_error ("bytecode stream: no caller found while reading edge");
1487 if (!indirect)
1489 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1490 if (callee == NULL || callee->decl == NULL_TREE)
1491 internal_error ("bytecode stream: no callee found while reading edge");
1493 else
1494 callee = NULL;
1496 count = streamer_read_gcov_count (ib);
1498 bp = streamer_read_bitpack (ib);
1499 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1500 stmt_id = bp_unpack_var_len_unsigned (&bp);
1501 freq = (int) bp_unpack_var_len_unsigned (&bp);
1503 if (indirect)
1504 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1505 else
1506 edge = caller->create_edge (callee, NULL, count, freq);
1508 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1509 edge->speculative = bp_unpack_value (&bp, 1);
1510 edge->lto_stmt_uid = stmt_id;
1511 edge->inline_failed = inline_failed;
1512 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1513 edge->can_throw_external = bp_unpack_value (&bp, 1);
1514 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1515 if (indirect)
1517 if (bp_unpack_value (&bp, 1))
1518 ecf_flags |= ECF_CONST;
1519 if (bp_unpack_value (&bp, 1))
1520 ecf_flags |= ECF_PURE;
1521 if (bp_unpack_value (&bp, 1))
1522 ecf_flags |= ECF_NORETURN;
1523 if (bp_unpack_value (&bp, 1))
1524 ecf_flags |= ECF_MALLOC;
1525 if (bp_unpack_value (&bp, 1))
1526 ecf_flags |= ECF_NOTHROW;
1527 if (bp_unpack_value (&bp, 1))
1528 ecf_flags |= ECF_RETURNS_TWICE;
1529 edge->indirect_info->ecf_flags = ecf_flags;
1530 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1531 if (edge->indirect_info->common_target_id)
1532 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1537 /* Read a cgraph from IB using the info in FILE_DATA. */
1539 static vec<symtab_node *>
1540 input_cgraph_1 (struct lto_file_decl_data *file_data,
1541 struct lto_input_block *ib)
1543 enum LTO_symtab_tags tag;
1544 vec<symtab_node *> nodes = vNULL;
1545 symtab_node *node;
1546 unsigned i;
1548 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1549 order_base = symtab->order;
1550 while (tag)
1552 if (tag == LTO_symtab_edge)
1553 input_edge (ib, nodes, false);
1554 else if (tag == LTO_symtab_indirect_edge)
1555 input_edge (ib, nodes, true);
1556 else if (tag == LTO_symtab_variable)
1558 node = input_varpool_node (file_data, ib);
1559 nodes.safe_push (node);
1560 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1562 else
1564 node = input_node (file_data, ib, tag, nodes);
1565 if (node == NULL || node->decl == NULL_TREE)
1566 internal_error ("bytecode stream: found empty cgraph node");
1567 nodes.safe_push (node);
1568 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1571 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1574 lto_input_toplevel_asms (file_data, order_base);
1576 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1577 #ifdef ENABLE_CHECKING
1578 FOR_EACH_VEC_ELT (nodes, i, node)
1579 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1580 #endif
1581 FOR_EACH_VEC_ELT (nodes, i, node)
1583 int ref;
1584 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1586 ref = (int) (intptr_t) cnode->global.inlined_to;
1588 /* We share declaration of builtins, so we may read same node twice. */
1589 if (!node->aux)
1590 continue;
1591 node->aux = NULL;
1593 /* Fixup inlined_to from reference to pointer. */
1594 if (ref != LCC_NOT_FOUND)
1595 dyn_cast<cgraph_node *> (node)->global.inlined_to
1596 = dyn_cast<cgraph_node *> (nodes[ref]);
1597 else
1598 cnode->global.inlined_to = NULL;
1600 /* Compute instrumented_version. */
1601 if (cnode->instrumentation_clone)
1603 gcc_assert (cnode->orig_decl);
1605 cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1606 if (cnode->instrumented_version)
1608 /* We may have multiple nodes for a single function which
1609 will be merged later. To have a proper merge we need
1610 to keep instrumentation_version reference between nodes
1611 consistent: each instrumented_version reference should
1612 have proper reverse reference. Thus don't break existing
1613 instrumented_version reference if it already exists. */
1614 if (cnode->instrumented_version->instrumented_version)
1615 cnode->instrumented_version = NULL;
1616 else
1617 cnode->instrumented_version->instrumented_version = cnode;
1620 /* Restore decl names reference except for wrapper functions. */
1621 if (!chkp_wrap_function (cnode->orig_decl))
1623 tree name = DECL_ASSEMBLER_NAME (cnode->decl);
1624 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
1625 TREE_CHAIN (name) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1630 ref = (int) (intptr_t) node->same_comdat_group;
1632 /* Fixup same_comdat_group from reference to pointer. */
1633 if (ref != LCC_NOT_FOUND)
1634 node->same_comdat_group = nodes[ref];
1635 else
1636 node->same_comdat_group = NULL;
1638 FOR_EACH_VEC_ELT (nodes, i, node)
1639 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1640 return nodes;
1643 /* Input ipa_refs. */
1645 static void
1646 input_refs (struct lto_input_block *ib,
1647 vec<symtab_node *> nodes)
1649 int count;
1650 int idx;
1651 while (true)
1653 symtab_node *node;
1654 count = streamer_read_uhwi (ib);
1655 if (!count)
1656 break;
1657 idx = streamer_read_uhwi (ib);
1658 node = nodes[idx];
1659 while (count)
1661 input_ref (ib, node, nodes);
1662 count--;
1668 static struct gcov_ctr_summary lto_gcov_summary;
1670 /* Input profile_info from IB. */
1671 static void
1672 input_profile_summary (struct lto_input_block *ib,
1673 struct lto_file_decl_data *file_data)
1675 unsigned h_ix;
1676 struct bitpack_d bp;
1677 unsigned int runs = streamer_read_uhwi (ib);
1678 if (runs)
1680 file_data->profile_info.runs = runs;
1681 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1682 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1684 memset (file_data->profile_info.histogram, 0,
1685 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1686 /* Input the bitpack of non-zero histogram indices. */
1687 bp = streamer_read_bitpack (ib);
1688 /* Read in and unpack the full bitpack, flagging non-zero
1689 histogram entries by setting the num_counters non-zero. */
1690 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1692 file_data->profile_info.histogram[h_ix].num_counters
1693 = bp_unpack_value (&bp, 1);
1695 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1697 if (!file_data->profile_info.histogram[h_ix].num_counters)
1698 continue;
1700 file_data->profile_info.histogram[h_ix].num_counters
1701 = streamer_read_gcov_count (ib);
1702 file_data->profile_info.histogram[h_ix].min_value
1703 = streamer_read_gcov_count (ib);
1704 file_data->profile_info.histogram[h_ix].cum_value
1705 = streamer_read_gcov_count (ib);
1707 /* IPA-profile computes hot bb threshold based on cumulated
1708 whole program profile. We need to stream it down to ltrans. */
1709 if (flag_ltrans)
1710 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1715 /* Rescale profile summaries to the same number of runs in the whole unit. */
1717 static void
1718 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1720 struct lto_file_decl_data *file_data;
1721 unsigned int j, h_ix;
1722 gcov_unsigned_t max_runs = 0;
1723 struct cgraph_node *node;
1724 struct cgraph_edge *edge;
1725 gcov_type saved_sum_all = 0;
1726 gcov_ctr_summary *saved_profile_info = 0;
1727 int saved_scale = 0;
1729 /* Find unit with maximal number of runs. If we ever get serious about
1730 roundoff errors, we might also consider computing smallest common
1731 multiply. */
1732 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1733 if (max_runs < file_data->profile_info.runs)
1734 max_runs = file_data->profile_info.runs;
1736 if (!max_runs)
1737 return;
1739 /* Simple overflow check. We probably don't need to support that many train
1740 runs. Such a large value probably imply data corruption anyway. */
1741 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1743 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1744 INT_MAX / REG_BR_PROB_BASE);
1745 return;
1748 profile_info = &lto_gcov_summary;
1749 lto_gcov_summary.runs = max_runs;
1750 lto_gcov_summary.sum_max = 0;
1751 memset (lto_gcov_summary.histogram, 0,
1752 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1754 /* Rescale all units to the maximal number of runs.
1755 sum_max can not be easily merged, as we have no idea what files come from
1756 the same run. We do not use the info anyway, so leave it 0. */
1757 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1758 if (file_data->profile_info.runs)
1760 int scale = GCOV_COMPUTE_SCALE (max_runs,
1761 file_data->profile_info.runs);
1762 lto_gcov_summary.sum_max
1763 = MAX (lto_gcov_summary.sum_max,
1764 apply_scale (file_data->profile_info.sum_max, scale));
1765 lto_gcov_summary.sum_all
1766 = MAX (lto_gcov_summary.sum_all,
1767 apply_scale (file_data->profile_info.sum_all, scale));
1768 /* Save a pointer to the profile_info with the largest
1769 scaled sum_all and the scale for use in merging the
1770 histogram. */
1771 if (!saved_profile_info
1772 || lto_gcov_summary.sum_all > saved_sum_all)
1774 saved_profile_info = &file_data->profile_info;
1775 saved_sum_all = lto_gcov_summary.sum_all;
1776 saved_scale = scale;
1780 gcc_assert (saved_profile_info);
1782 /* Scale up the histogram from the profile that had the largest
1783 scaled sum_all above. */
1784 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1786 /* Scale up the min value as we did the corresponding sum_all
1787 above. Use that to find the new histogram index. */
1788 gcov_type scaled_min
1789 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1790 saved_scale);
1791 /* The new index may be shared with another scaled histogram entry,
1792 so we need to account for a non-zero histogram entry at new_ix. */
1793 unsigned new_ix = gcov_histo_index (scaled_min);
1794 lto_gcov_summary.histogram[new_ix].min_value
1795 = (lto_gcov_summary.histogram[new_ix].num_counters
1796 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1797 : scaled_min);
1798 /* Some of the scaled counter values would ostensibly need to be placed
1799 into different (larger) histogram buckets, but we keep things simple
1800 here and place the scaled cumulative counter value in the bucket
1801 corresponding to the scaled minimum counter value. */
1802 lto_gcov_summary.histogram[new_ix].cum_value
1803 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1804 saved_scale);
1805 lto_gcov_summary.histogram[new_ix].num_counters
1806 += saved_profile_info->histogram[h_ix].num_counters;
1809 /* Watch roundoff errors. */
1810 if (lto_gcov_summary.sum_max < max_runs)
1811 lto_gcov_summary.sum_max = max_runs;
1813 /* If merging already happent at WPA time, we are done. */
1814 if (flag_ltrans)
1815 return;
1817 /* Now compute count_materialization_scale of each node.
1818 During LTRANS we already have values of count_materialization_scale
1819 computed, so just update them. */
1820 FOR_EACH_FUNCTION (node)
1821 if (node->lto_file_data
1822 && node->lto_file_data->profile_info.runs)
1824 int scale;
1826 scale = RDIV (node->count_materialization_scale * max_runs,
1827 node->lto_file_data->profile_info.runs);
1828 node->count_materialization_scale = scale;
1829 if (scale < 0)
1830 fatal_error (input_location, "Profile information in %s corrupted",
1831 file_data->file_name);
1833 if (scale == REG_BR_PROB_BASE)
1834 continue;
1835 for (edge = node->callees; edge; edge = edge->next_callee)
1836 edge->count = apply_scale (edge->count, scale);
1837 node->count = apply_scale (node->count, scale);
1841 /* Input and merge the symtab from each of the .o files passed to
1842 lto1. */
1844 void
1845 input_symtab (void)
1847 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1848 struct lto_file_decl_data *file_data;
1849 unsigned int j = 0;
1850 struct cgraph_node *node;
1852 while ((file_data = file_data_vec[j++]))
1854 const char *data;
1855 size_t len;
1856 struct lto_input_block *ib;
1857 vec<symtab_node *> nodes;
1859 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1860 &data, &len);
1861 if (!ib)
1862 fatal_error (input_location,
1863 "cannot find LTO cgraph in %s", file_data->file_name);
1864 input_profile_summary (ib, file_data);
1865 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1866 nodes = input_cgraph_1 (file_data, ib);
1867 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1868 ib, data, len);
1870 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1871 &data, &len);
1872 if (!ib)
1873 fatal_error (input_location, "cannot find LTO section refs in %s",
1874 file_data->file_name);
1875 input_refs (ib, nodes);
1876 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1877 ib, data, len);
1878 if (flag_ltrans)
1879 input_cgraph_opt_summary (nodes);
1880 nodes.release ();
1883 merge_profile_summaries (file_data_vec);
1884 get_working_sets ();
1887 /* Clear out the aux field that was used to store enough state to
1888 tell which nodes should be overwritten. */
1889 FOR_EACH_FUNCTION (node)
1891 /* Some nodes may have been created by cgraph_node. This
1892 happens when the callgraph contains nested functions. If the
1893 node for the parent function was never emitted to the gimple
1894 file, cgraph_node will create a node for it when setting the
1895 context of the nested function. */
1896 if (node->lto_file_data)
1897 node->aux = NULL;
1901 /* Input function/variable tables that will allow libgomp to look up offload
1902 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1904 void
1905 input_offload_tables (void)
1907 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1908 struct lto_file_decl_data *file_data;
1909 unsigned int j = 0;
1911 while ((file_data = file_data_vec[j++]))
1913 const char *data;
1914 size_t len;
1915 struct lto_input_block *ib
1916 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1917 &data, &len);
1918 if (!ib)
1919 continue;
1921 enum LTO_symtab_tags tag
1922 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1923 while (tag)
1925 if (tag == LTO_symtab_unavail_node)
1927 int decl_index = streamer_read_uhwi (ib);
1928 tree fn_decl
1929 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1930 vec_safe_push (offload_funcs, fn_decl);
1932 else if (tag == LTO_symtab_variable)
1934 int decl_index = streamer_read_uhwi (ib);
1935 tree var_decl
1936 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1937 vec_safe_push (offload_vars, var_decl);
1939 else
1940 fatal_error (input_location,
1941 "invalid offload table in %s", file_data->file_name);
1943 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1946 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1947 ib, data, len);
1951 /* True when we need optimization summary for NODE. */
1953 static int
1954 output_cgraph_opt_summary_p (struct cgraph_node *node)
1956 return (node->clone_of
1957 && (node->clone.tree_map
1958 || node->clone.args_to_skip
1959 || node->clone.combined_args_to_skip));
1962 /* Output optimization summary for EDGE to OB. */
1963 static void
1964 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1965 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1969 /* Output optimization summary for NODE to OB. */
1971 static void
1972 output_node_opt_summary (struct output_block *ob,
1973 struct cgraph_node *node,
1974 lto_symtab_encoder_t encoder)
1976 unsigned int index;
1977 bitmap_iterator bi;
1978 struct ipa_replace_map *map;
1979 struct bitpack_d bp;
1980 int i;
1981 struct cgraph_edge *e;
1983 if (node->clone.args_to_skip)
1985 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
1986 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
1987 streamer_write_uhwi (ob, index);
1989 else
1990 streamer_write_uhwi (ob, 0);
1991 if (node->clone.combined_args_to_skip)
1993 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
1994 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
1995 streamer_write_uhwi (ob, index);
1997 else
1998 streamer_write_uhwi (ob, 0);
1999 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
2000 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
2002 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
2003 mechanism to store function local declarations into summaries. */
2004 gcc_assert (!map->old_tree);
2005 streamer_write_uhwi (ob, map->parm_num);
2006 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
2007 stream_write_tree (ob, map->new_tree, true);
2008 bp = bitpack_create (ob->main_stream);
2009 bp_pack_value (&bp, map->replace_p, 1);
2010 bp_pack_value (&bp, map->ref_p, 1);
2011 streamer_write_bitpack (&bp);
2014 if (lto_symtab_encoder_in_partition_p (encoder, node))
2016 for (e = node->callees; e; e = e->next_callee)
2017 output_edge_opt_summary (ob, e);
2018 for (e = node->indirect_calls; e; e = e->next_callee)
2019 output_edge_opt_summary (ob, e);
2023 /* Output optimization summaries stored in callgraph.
2024 At the moment it is the clone info structure. */
2026 static void
2027 output_cgraph_opt_summary (void)
2029 int i, n_nodes;
2030 lto_symtab_encoder_t encoder;
2031 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2032 unsigned count = 0;
2034 ob->symbol = NULL;
2035 encoder = ob->decl_state->symtab_node_encoder;
2036 n_nodes = lto_symtab_encoder_size (encoder);
2037 for (i = 0; i < n_nodes; i++)
2039 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2040 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2041 if (cnode && output_cgraph_opt_summary_p (cnode))
2042 count++;
2044 streamer_write_uhwi (ob, count);
2045 for (i = 0; i < n_nodes; i++)
2047 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2048 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2049 if (cnode && output_cgraph_opt_summary_p (cnode))
2051 streamer_write_uhwi (ob, i);
2052 output_node_opt_summary (ob, cnode, encoder);
2055 produce_asm (ob, NULL);
2056 destroy_output_block (ob);
2059 /* Input optimisation summary of EDGE. */
2061 static void
2062 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2063 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
2067 /* Input optimisation summary of NODE. */
2069 static void
2070 input_node_opt_summary (struct cgraph_node *node,
2071 struct lto_input_block *ib_main,
2072 struct data_in *data_in)
2074 int i;
2075 int count;
2076 int bit;
2077 struct bitpack_d bp;
2078 struct cgraph_edge *e;
2080 count = streamer_read_uhwi (ib_main);
2081 if (count)
2082 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
2083 for (i = 0; i < count; i++)
2085 bit = streamer_read_uhwi (ib_main);
2086 bitmap_set_bit (node->clone.args_to_skip, bit);
2088 count = streamer_read_uhwi (ib_main);
2089 if (count)
2090 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
2091 for (i = 0; i < count; i++)
2093 bit = streamer_read_uhwi (ib_main);
2094 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
2096 count = streamer_read_uhwi (ib_main);
2097 for (i = 0; i < count; i++)
2099 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2101 vec_safe_push (node->clone.tree_map, map);
2102 map->parm_num = streamer_read_uhwi (ib_main);
2103 map->old_tree = NULL;
2104 map->new_tree = stream_read_tree (ib_main, data_in);
2105 bp = streamer_read_bitpack (ib_main);
2106 map->replace_p = bp_unpack_value (&bp, 1);
2107 map->ref_p = bp_unpack_value (&bp, 1);
2109 for (e = node->callees; e; e = e->next_callee)
2110 input_edge_opt_summary (e, ib_main);
2111 for (e = node->indirect_calls; e; e = e->next_callee)
2112 input_edge_opt_summary (e, ib_main);
2115 /* Read section in file FILE_DATA of length LEN with data DATA. */
2117 static void
2118 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2119 const char *data, size_t len,
2120 vec<symtab_node *> nodes)
2122 const struct lto_function_header *header =
2123 (const struct lto_function_header *) data;
2124 const int cfg_offset = sizeof (struct lto_function_header);
2125 const int main_offset = cfg_offset + header->cfg_size;
2126 const int string_offset = main_offset + header->main_size;
2127 struct data_in *data_in;
2128 unsigned int i;
2129 unsigned int count;
2131 lto_input_block ib_main ((const char *) data + main_offset,
2132 header->main_size, file_data->mode_table);
2134 data_in =
2135 lto_data_in_create (file_data, (const char *) data + string_offset,
2136 header->string_size, vNULL);
2137 count = streamer_read_uhwi (&ib_main);
2139 for (i = 0; i < count; i++)
2141 int ref = streamer_read_uhwi (&ib_main);
2142 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2143 &ib_main, data_in);
2145 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2146 len);
2147 lto_data_in_delete (data_in);
2150 /* Input optimization summary of cgraph. */
2152 static void
2153 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2155 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2156 struct lto_file_decl_data *file_data;
2157 unsigned int j = 0;
2159 while ((file_data = file_data_vec[j++]))
2161 size_t len;
2162 const char *data =
2163 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
2164 &len);
2166 if (data)
2167 input_cgraph_opt_section (file_data, data, len, nodes);