Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / lto-cgraph.c
blob42db4d8181af94b37c70522252e81d4345052d1b
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 "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "stringpool.h"
33 #include "predict.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "rtl.h"
43 #include "flags.h"
44 #include "insn-config.h"
45 #include "expmed.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "emit-rtl.h"
50 #include "varasm.h"
51 #include "stmt.h"
52 #include "expr.h"
53 #include "params.h"
54 #include "langhooks.h"
55 #include "bitmap.h"
56 #include "diagnostic-core.h"
57 #include "except.h"
58 #include "timevar.h"
59 #include "plugin-api.h"
60 #include "ipa-ref.h"
61 #include "cgraph.h"
62 #include "lto-streamer.h"
63 #include "data-streamer.h"
64 #include "tree-streamer.h"
65 #include "gcov-io.h"
66 #include "tree-pass.h"
67 #include "profile.h"
68 #include "context.h"
69 #include "pass_manager.h"
70 #include "ipa-utils.h"
71 #include "omp-low.h"
72 #include "ipa-chkp.h"
74 /* True when asm nodes has been output. */
75 bool asm_nodes_output = false;
77 static void output_cgraph_opt_summary (void);
78 static void input_cgraph_opt_summary (vec<symtab_node *> nodes);
80 /* Number of LDPR values known to GCC. */
81 #define LDPR_NUM_KNOWN (LDPR_PREVAILING_DEF_IRONLY_EXP + 1)
83 /* All node orders are ofsetted by ORDER_BASE. */
84 static int order_base;
86 /* Cgraph streaming is organized as set of record whose type
87 is indicated by a tag. */
88 enum LTO_symtab_tags
90 /* Must leave 0 for the stopper. */
92 /* Cgraph node without body available. */
93 LTO_symtab_unavail_node = 1,
94 /* Cgraph node with function body. */
95 LTO_symtab_analyzed_node,
96 /* Cgraph edges. */
97 LTO_symtab_edge,
98 LTO_symtab_indirect_edge,
99 LTO_symtab_variable,
100 LTO_symtab_last_tag
103 /* Create a new symtab encoder.
104 if FOR_INPUT, the encoder allocate only datastructures needed
105 to read the symtab. */
107 lto_symtab_encoder_t
108 lto_symtab_encoder_new (bool for_input)
110 lto_symtab_encoder_t encoder = XCNEW (struct lto_symtab_encoder_d);
112 if (!for_input)
113 encoder->map = new hash_map<symtab_node *, size_t>;
114 encoder->nodes.create (0);
115 return encoder;
119 /* Delete ENCODER and its components. */
121 void
122 lto_symtab_encoder_delete (lto_symtab_encoder_t encoder)
124 encoder->nodes.release ();
125 if (encoder->map)
126 delete encoder->map;
127 free (encoder);
131 /* Return the existing reference number of NODE in the symtab encoder in
132 output block OB. Assign a new reference if this is the first time
133 NODE is encoded. */
136 lto_symtab_encoder_encode (lto_symtab_encoder_t encoder,
137 symtab_node *node)
139 int ref;
141 if (!encoder->map)
143 lto_encoder_entry entry = {node, false, false, false};
145 ref = encoder->nodes.length ();
146 encoder->nodes.safe_push (entry);
147 return ref;
150 size_t *slot = encoder->map->get (node);
151 if (!slot || !*slot)
153 lto_encoder_entry entry = {node, false, false, false};
154 ref = encoder->nodes.length ();
155 if (!slot)
156 encoder->map->put (node, ref + 1);
157 encoder->nodes.safe_push (entry);
159 else
160 ref = *slot - 1;
162 return ref;
165 /* Remove NODE from encoder. */
167 bool
168 lto_symtab_encoder_delete_node (lto_symtab_encoder_t encoder,
169 symtab_node *node)
171 int index;
172 lto_encoder_entry last_node;
174 size_t *slot = encoder->map->get (node);
175 if (slot == NULL || !*slot)
176 return false;
178 index = *slot - 1;
179 gcc_checking_assert (encoder->nodes[index].node == node);
181 /* Remove from vector. We do this by swapping node with the last element
182 of the vector. */
183 last_node = encoder->nodes.pop ();
184 if (last_node.node != node)
186 gcc_assert (encoder->map->put (last_node.node, index + 1));
188 /* Move the last element to the original spot of NODE. */
189 encoder->nodes[index] = last_node;
192 /* Remove element from hash table. */
193 encoder->map->remove (node);
194 return true;
198 /* Return TRUE if we should encode the body of NODE (if any). */
200 bool
201 lto_symtab_encoder_encode_body_p (lto_symtab_encoder_t encoder,
202 struct cgraph_node *node)
204 int index = lto_symtab_encoder_lookup (encoder, node);
205 return encoder->nodes[index].body;
208 /* Specify that we encode the body of NODE in this partition. */
210 static void
211 lto_set_symtab_encoder_encode_body (lto_symtab_encoder_t encoder,
212 struct cgraph_node *node)
214 int index = lto_symtab_encoder_encode (encoder, node);
215 gcc_checking_assert (encoder->nodes[index].node == node);
216 encoder->nodes[index].body = true;
219 /* Return TRUE if we should encode initializer of NODE (if any). */
221 bool
222 lto_symtab_encoder_encode_initializer_p (lto_symtab_encoder_t encoder,
223 varpool_node *node)
225 int index = lto_symtab_encoder_lookup (encoder, node);
226 if (index == LCC_NOT_FOUND)
227 return false;
228 return encoder->nodes[index].initializer;
231 /* Specify that we should encode initializer of NODE (if any). */
233 static void
234 lto_set_symtab_encoder_encode_initializer (lto_symtab_encoder_t encoder,
235 varpool_node *node)
237 int index = lto_symtab_encoder_lookup (encoder, node);
238 encoder->nodes[index].initializer = true;
241 /* Return TRUE if NODE is in this partition. */
243 bool
244 lto_symtab_encoder_in_partition_p (lto_symtab_encoder_t encoder,
245 symtab_node *node)
247 int index = lto_symtab_encoder_lookup (encoder, node);
248 if (index == LCC_NOT_FOUND)
249 return false;
250 return encoder->nodes[index].in_partition;
253 /* Specify that NODE is in this partition. */
255 void
256 lto_set_symtab_encoder_in_partition (lto_symtab_encoder_t encoder,
257 symtab_node *node)
259 int index = lto_symtab_encoder_encode (encoder, node);
260 encoder->nodes[index].in_partition = true;
263 /* Output the cgraph EDGE to OB using ENCODER. */
265 static void
266 lto_output_edge (struct lto_simple_output_block *ob, struct cgraph_edge *edge,
267 lto_symtab_encoder_t encoder)
269 unsigned int uid;
270 intptr_t ref;
271 struct bitpack_d bp;
273 if (edge->indirect_unknown_callee)
274 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
275 LTO_symtab_indirect_edge);
276 else
277 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
278 LTO_symtab_edge);
280 ref = lto_symtab_encoder_lookup (encoder, edge->caller);
281 gcc_assert (ref != LCC_NOT_FOUND);
282 streamer_write_hwi_stream (ob->main_stream, ref);
284 if (!edge->indirect_unknown_callee)
286 ref = lto_symtab_encoder_lookup (encoder, edge->callee);
287 gcc_assert (ref != LCC_NOT_FOUND);
288 streamer_write_hwi_stream (ob->main_stream, ref);
291 streamer_write_gcov_count_stream (ob->main_stream, edge->count);
293 bp = bitpack_create (ob->main_stream);
294 uid = (!gimple_has_body_p (edge->caller->decl)
295 ? edge->lto_stmt_uid : gimple_uid (edge->call_stmt) + 1);
296 bp_pack_enum (&bp, cgraph_inline_failed_t,
297 CIF_N_REASONS, edge->inline_failed);
298 bp_pack_var_len_unsigned (&bp, uid);
299 bp_pack_var_len_unsigned (&bp, edge->frequency);
300 bp_pack_value (&bp, edge->indirect_inlining_edge, 1);
301 bp_pack_value (&bp, edge->speculative, 1);
302 bp_pack_value (&bp, edge->call_stmt_cannot_inline_p, 1);
303 bp_pack_value (&bp, edge->can_throw_external, 1);
304 bp_pack_value (&bp, edge->in_polymorphic_cdtor, 1);
305 if (edge->indirect_unknown_callee)
307 int flags = edge->indirect_info->ecf_flags;
308 bp_pack_value (&bp, (flags & ECF_CONST) != 0, 1);
309 bp_pack_value (&bp, (flags & ECF_PURE) != 0, 1);
310 bp_pack_value (&bp, (flags & ECF_NORETURN) != 0, 1);
311 bp_pack_value (&bp, (flags & ECF_MALLOC) != 0, 1);
312 bp_pack_value (&bp, (flags & ECF_NOTHROW) != 0, 1);
313 bp_pack_value (&bp, (flags & ECF_RETURNS_TWICE) != 0, 1);
314 /* Flags that should not appear on indirect calls. */
315 gcc_assert (!(flags & (ECF_LOOPING_CONST_OR_PURE
316 | ECF_MAY_BE_ALLOCA
317 | ECF_SIBCALL
318 | ECF_LEAF
319 | ECF_NOVOPS)));
321 streamer_write_bitpack (&bp);
322 if (edge->indirect_unknown_callee)
324 streamer_write_hwi_stream (ob->main_stream,
325 edge->indirect_info->common_target_id);
326 if (edge->indirect_info->common_target_id)
327 streamer_write_hwi_stream
328 (ob->main_stream, edge->indirect_info->common_target_probability);
332 /* Return if NODE contain references from other partitions. */
334 bool
335 referenced_from_other_partition_p (symtab_node *node, lto_symtab_encoder_t encoder)
337 int i;
338 struct ipa_ref *ref = NULL;
340 for (i = 0; node->iterate_referring (i, ref); i++)
342 /* Ignore references from non-offloadable nodes while streaming NODE into
343 offload LTO section. */
344 if (!ref->referring->need_lto_streaming)
345 continue;
347 if (ref->referring->in_other_partition
348 || !lto_symtab_encoder_in_partition_p (encoder, ref->referring))
349 return true;
351 return false;
354 /* Return true when node is reachable from other partition. */
356 bool
357 reachable_from_other_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
359 struct cgraph_edge *e;
360 if (!node->definition)
361 return false;
362 if (node->global.inlined_to)
363 return false;
364 for (e = node->callers; e; e = e->next_caller)
366 /* Ignore references from non-offloadable nodes while streaming NODE into
367 offload LTO section. */
368 if (!e->caller->need_lto_streaming)
369 continue;
371 if (e->caller->in_other_partition
372 || !lto_symtab_encoder_in_partition_p (encoder, e->caller))
373 return true;
375 return false;
378 /* Return if NODE contain references from other partitions. */
380 bool
381 referenced_from_this_partition_p (symtab_node *node,
382 lto_symtab_encoder_t encoder)
384 int i;
385 struct ipa_ref *ref = NULL;
387 for (i = 0; node->iterate_referring (i, ref); i++)
388 if (lto_symtab_encoder_in_partition_p (encoder, ref->referring))
389 return true;
390 return false;
393 /* Return true when node is reachable from other partition. */
395 bool
396 reachable_from_this_partition_p (struct cgraph_node *node, lto_symtab_encoder_t encoder)
398 struct cgraph_edge *e;
399 for (e = node->callers; e; e = e->next_caller)
400 if (lto_symtab_encoder_in_partition_p (encoder, e->caller))
401 return true;
402 return false;
405 /* Output the cgraph NODE to OB. ENCODER is used to find the
406 reference number of NODE->inlined_to. SET is the set of nodes we
407 are writing to the current file. If NODE is not in SET, then NODE
408 is a boundary of a cgraph_node_set and we pretend NODE just has a
409 decl and no callees. WRITTEN_DECLS is the set of FUNCTION_DECLs
410 that have had their callgraph node written so far. This is used to
411 determine if NODE is a clone of a previously written node. */
413 static void
414 lto_output_node (struct lto_simple_output_block *ob, struct cgraph_node *node,
415 lto_symtab_encoder_t encoder)
417 unsigned int tag;
418 struct bitpack_d bp;
419 bool boundary_p;
420 intptr_t ref;
421 bool in_other_partition = false;
422 struct cgraph_node *clone_of, *ultimate_clone_of;
423 ipa_opt_pass_d *pass;
424 int i;
425 const char *comdat;
426 const char *section;
427 tree group;
429 boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
431 if (node->analyzed && (!boundary_p || node->alias || node->thunk.thunk_p))
432 tag = LTO_symtab_analyzed_node;
433 else
434 tag = LTO_symtab_unavail_node;
436 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
437 tag);
438 streamer_write_hwi_stream (ob->main_stream, node->order);
440 /* In WPA mode, we only output part of the call-graph. Also, we
441 fake cgraph node attributes. There are two cases that we care.
443 Boundary nodes: There are nodes that are not part of SET but are
444 called from within SET. We artificially make them look like
445 externally visible nodes with no function body.
447 Cherry-picked nodes: These are nodes we pulled from other
448 translation units into SET during IPA-inlining. We make them as
449 local static nodes to prevent clashes with other local statics. */
450 if (boundary_p && node->analyzed
451 && node->get_partitioning_class () == SYMBOL_PARTITION)
453 /* Inline clones can not be part of boundary.
454 gcc_assert (!node->global.inlined_to);
456 FIXME: At the moment they can be, when partition contains an inline
457 clone that is clone of inline clone from outside partition. We can
458 reshape the clone tree and make other tree to be the root, but it
459 needs a bit extra work and will be promplty done by cgraph_remove_node
460 after reading back. */
461 in_other_partition = 1;
464 clone_of = node->clone_of;
465 while (clone_of
466 && (ref = lto_symtab_encoder_lookup (encoder, clone_of)) == LCC_NOT_FOUND)
467 if (clone_of->prev_sibling_clone)
468 clone_of = clone_of->prev_sibling_clone;
469 else
470 clone_of = clone_of->clone_of;
472 /* See if body of the master function is output. If not, we are seeing only
473 an declaration and we do not need to pass down clone tree. */
474 ultimate_clone_of = clone_of;
475 while (ultimate_clone_of && ultimate_clone_of->clone_of)
476 ultimate_clone_of = ultimate_clone_of->clone_of;
478 if (clone_of && !lto_symtab_encoder_encode_body_p (encoder, ultimate_clone_of))
479 clone_of = NULL;
481 if (tag == LTO_symtab_analyzed_node)
482 gcc_assert (clone_of || !node->clone_of);
483 if (!clone_of)
484 streamer_write_hwi_stream (ob->main_stream, LCC_NOT_FOUND);
485 else
486 streamer_write_hwi_stream (ob->main_stream, ref);
489 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->decl);
490 streamer_write_gcov_count_stream (ob->main_stream, node->count);
491 streamer_write_hwi_stream (ob->main_stream, node->count_materialization_scale);
493 streamer_write_hwi_stream (ob->main_stream,
494 node->ipa_transforms_to_apply.length ());
495 FOR_EACH_VEC_ELT (node->ipa_transforms_to_apply, i, pass)
496 streamer_write_hwi_stream (ob->main_stream, pass->static_pass_number);
498 if (tag == LTO_symtab_analyzed_node)
500 if (node->global.inlined_to)
502 ref = lto_symtab_encoder_lookup (encoder, node->global.inlined_to);
503 gcc_assert (ref != LCC_NOT_FOUND);
505 else
506 ref = LCC_NOT_FOUND;
508 streamer_write_hwi_stream (ob->main_stream, ref);
511 group = node->get_comdat_group ();
512 if (group)
513 comdat = IDENTIFIER_POINTER (group);
514 else
515 comdat = "";
516 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
518 if (group)
520 if (node->same_comdat_group && !boundary_p)
522 ref = lto_symtab_encoder_lookup (encoder,
523 node->same_comdat_group);
524 gcc_assert (ref != LCC_NOT_FOUND);
526 else
527 ref = LCC_NOT_FOUND;
528 streamer_write_hwi_stream (ob->main_stream, ref);
531 section = node->get_section ();
532 if (!section)
533 section = "";
535 streamer_write_hwi_stream (ob->main_stream, node->tp_first_run);
537 bp = bitpack_create (ob->main_stream);
538 bp_pack_value (&bp, node->local.local, 1);
539 bp_pack_value (&bp, node->externally_visible, 1);
540 bp_pack_value (&bp, node->no_reorder, 1);
541 bp_pack_value (&bp, node->definition, 1);
542 bp_pack_value (&bp, node->local.versionable, 1);
543 bp_pack_value (&bp, node->local.can_change_signature, 1);
544 bp_pack_value (&bp, node->local.redefined_extern_inline, 1);
545 bp_pack_value (&bp, node->force_output, 1);
546 bp_pack_value (&bp, node->forced_by_abi, 1);
547 bp_pack_value (&bp, node->unique_name, 1);
548 bp_pack_value (&bp, node->body_removed, 1);
549 bp_pack_value (&bp, node->implicit_section, 1);
550 bp_pack_value (&bp, node->address_taken, 1);
551 bp_pack_value (&bp, tag == LTO_symtab_analyzed_node
552 && node->get_partitioning_class () == SYMBOL_PARTITION
553 && (reachable_from_other_partition_p (node, encoder)
554 || referenced_from_other_partition_p (node, encoder)), 1);
555 bp_pack_value (&bp, node->lowered, 1);
556 bp_pack_value (&bp, in_other_partition, 1);
557 bp_pack_value (&bp, node->alias, 1);
558 bp_pack_value (&bp, node->weakref, 1);
559 bp_pack_value (&bp, node->frequency, 2);
560 bp_pack_value (&bp, node->only_called_at_startup, 1);
561 bp_pack_value (&bp, node->only_called_at_exit, 1);
562 bp_pack_value (&bp, node->tm_clone, 1);
563 bp_pack_value (&bp, node->calls_comdat_local, 1);
564 bp_pack_value (&bp, node->icf_merged, 1);
565 bp_pack_value (&bp, node->nonfreeing_fn, 1);
566 bp_pack_value (&bp, node->thunk.thunk_p, 1);
567 bp_pack_value (&bp, node->parallelized_function, 1);
568 bp_pack_enum (&bp, ld_plugin_symbol_resolution,
569 LDPR_NUM_KNOWN, node->resolution);
570 bp_pack_value (&bp, node->instrumentation_clone, 1);
571 bp_pack_value (&bp, node->split_part, 1);
572 streamer_write_bitpack (&bp);
573 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
575 if (node->thunk.thunk_p)
577 streamer_write_uhwi_stream
578 (ob->main_stream,
579 1 + (node->thunk.this_adjusting != 0) * 2
580 + (node->thunk.virtual_offset_p != 0) * 4
581 + (node->thunk.add_pointer_bounds_args != 0) * 8);
582 streamer_write_uhwi_stream (ob->main_stream, node->thunk.fixed_offset);
583 streamer_write_uhwi_stream (ob->main_stream, node->thunk.virtual_value);
585 streamer_write_hwi_stream (ob->main_stream, node->profile_id);
586 if (DECL_STATIC_CONSTRUCTOR (node->decl))
587 streamer_write_hwi_stream (ob->main_stream, node->get_init_priority ());
588 if (DECL_STATIC_DESTRUCTOR (node->decl))
589 streamer_write_hwi_stream (ob->main_stream, node->get_fini_priority ());
591 if (node->instrumentation_clone)
592 lto_output_fn_decl_index (ob->decl_state, ob->main_stream, node->orig_decl);
595 /* Output the varpool NODE to OB.
596 If NODE is not in SET, then NODE is a boundary. */
598 static void
599 lto_output_varpool_node (struct lto_simple_output_block *ob, varpool_node *node,
600 lto_symtab_encoder_t encoder)
602 bool boundary_p = !lto_symtab_encoder_in_partition_p (encoder, node);
603 bool encode_initializer_p
604 = (node->definition
605 && lto_symtab_encoder_encode_initializer_p (encoder, node));
606 struct bitpack_d bp;
607 int ref;
608 const char *comdat;
609 const char *section;
610 tree group;
612 gcc_assert (!encode_initializer_p || node->definition);
613 gcc_assert (boundary_p || encode_initializer_p);
615 streamer_write_enum (ob->main_stream, LTO_symtab_tags, LTO_symtab_last_tag,
616 LTO_symtab_variable);
617 streamer_write_hwi_stream (ob->main_stream, node->order);
618 lto_output_var_decl_index (ob->decl_state, ob->main_stream, node->decl);
619 bp = bitpack_create (ob->main_stream);
620 bp_pack_value (&bp, node->externally_visible, 1);
621 bp_pack_value (&bp, node->no_reorder, 1);
622 bp_pack_value (&bp, node->force_output, 1);
623 bp_pack_value (&bp, node->forced_by_abi, 1);
624 bp_pack_value (&bp, node->unique_name, 1);
625 bp_pack_value (&bp,
626 node->body_removed
627 || (!encode_initializer_p && !node->alias && node->definition),
629 bp_pack_value (&bp, node->implicit_section, 1);
630 bp_pack_value (&bp, node->writeonly, 1);
631 bp_pack_value (&bp, node->definition && (encode_initializer_p || node->alias),
633 bp_pack_value (&bp, node->alias, 1);
634 bp_pack_value (&bp, node->weakref, 1);
635 bp_pack_value (&bp, node->analyzed && !boundary_p, 1);
636 gcc_assert (node->definition || !node->analyzed);
637 /* Constant pool initializers can be de-unified into individual ltrans units.
638 FIXME: Alternatively at -Os we may want to avoid generating for them the local
639 labels and share them across LTRANS partitions. */
640 if (node->get_partitioning_class () != SYMBOL_PARTITION)
642 bp_pack_value (&bp, 0, 1); /* used_from_other_parition. */
643 bp_pack_value (&bp, 0, 1); /* in_other_partition. */
645 else
647 bp_pack_value (&bp, node->definition
648 && referenced_from_other_partition_p (node, encoder), 1);
649 bp_pack_value (&bp, node->analyzed
650 && boundary_p && !DECL_EXTERNAL (node->decl), 1);
651 /* in_other_partition. */
653 bp_pack_value (&bp, node->tls_model, 3);
654 bp_pack_value (&bp, node->used_by_single_function, 1);
655 bp_pack_value (&bp, node->need_bounds_init, 1);
656 streamer_write_bitpack (&bp);
658 group = node->get_comdat_group ();
659 if (group)
660 comdat = IDENTIFIER_POINTER (group);
661 else
662 comdat = "";
663 streamer_write_data_stream (ob->main_stream, comdat, strlen (comdat) + 1);
665 if (group)
667 if (node->same_comdat_group && !boundary_p)
669 ref = lto_symtab_encoder_lookup (encoder,
670 node->same_comdat_group);
671 gcc_assert (ref != LCC_NOT_FOUND);
673 else
674 ref = LCC_NOT_FOUND;
675 streamer_write_hwi_stream (ob->main_stream, ref);
678 section = node->get_section ();
679 if (!section)
680 section = "";
681 streamer_write_data_stream (ob->main_stream, section, strlen (section) + 1);
683 streamer_write_enum (ob->main_stream, ld_plugin_symbol_resolution,
684 LDPR_NUM_KNOWN, node->resolution);
687 /* Output the varpool NODE to OB.
688 If NODE is not in SET, then NODE is a boundary. */
690 static void
691 lto_output_ref (struct lto_simple_output_block *ob, struct ipa_ref *ref,
692 lto_symtab_encoder_t encoder)
694 struct bitpack_d bp;
695 int nref;
696 int uid = ref->lto_stmt_uid;
697 struct cgraph_node *node;
699 bp = bitpack_create (ob->main_stream);
700 bp_pack_value (&bp, ref->use, 3);
701 bp_pack_value (&bp, ref->speculative, 1);
702 streamer_write_bitpack (&bp);
703 nref = lto_symtab_encoder_lookup (encoder, ref->referred);
704 gcc_assert (nref != LCC_NOT_FOUND);
705 streamer_write_hwi_stream (ob->main_stream, nref);
707 node = dyn_cast <cgraph_node *> (ref->referring);
708 if (node)
710 if (ref->stmt)
711 uid = gimple_uid (ref->stmt) + 1;
712 streamer_write_hwi_stream (ob->main_stream, uid);
716 /* Stream out profile_summary to OB. */
718 static void
719 output_profile_summary (struct lto_simple_output_block *ob)
721 unsigned h_ix;
722 struct bitpack_d bp;
724 if (profile_info)
726 /* We do not output num and run_max, they are not used by
727 GCC profile feedback and they are difficult to merge from multiple
728 units. */
729 gcc_assert (profile_info->runs);
730 streamer_write_uhwi_stream (ob->main_stream, profile_info->runs);
731 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_max);
733 /* sum_all is needed for computing the working set with the
734 histogram. */
735 streamer_write_gcov_count_stream (ob->main_stream, profile_info->sum_all);
737 /* Create and output a bitpack of non-zero histogram entries indices. */
738 bp = bitpack_create (ob->main_stream);
739 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
740 bp_pack_value (&bp, profile_info->histogram[h_ix].num_counters > 0, 1);
741 streamer_write_bitpack (&bp);
742 /* Now stream out only those non-zero entries. */
743 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
745 if (!profile_info->histogram[h_ix].num_counters)
746 continue;
747 streamer_write_gcov_count_stream (ob->main_stream,
748 profile_info->histogram[h_ix].num_counters);
749 streamer_write_gcov_count_stream (ob->main_stream,
750 profile_info->histogram[h_ix].min_value);
751 streamer_write_gcov_count_stream (ob->main_stream,
752 profile_info->histogram[h_ix].cum_value);
754 /* IPA-profile computes hot bb threshold based on cumulated
755 whole program profile. We need to stream it down to ltrans. */
756 if (flag_wpa)
757 streamer_write_gcov_count_stream (ob->main_stream,
758 get_hot_bb_threshold ());
760 else
761 streamer_write_uhwi_stream (ob->main_stream, 0);
764 /* Output all callees or indirect outgoing edges. EDGE must be the first such
765 edge. */
767 static void
768 output_outgoing_cgraph_edges (struct cgraph_edge *edge,
769 struct lto_simple_output_block *ob,
770 lto_symtab_encoder_t encoder)
772 if (!edge)
773 return;
775 /* Output edges in backward direction, so the reconstructed callgraph match
776 and it is easy to associate call sites in the IPA pass summaries. */
777 while (edge->next_callee)
778 edge = edge->next_callee;
779 for (; edge; edge = edge->prev_callee)
780 lto_output_edge (ob, edge, encoder);
783 /* Output the part of the cgraph in SET. */
785 static void
786 output_refs (lto_symtab_encoder_t encoder)
788 struct lto_simple_output_block *ob;
789 int count;
790 struct ipa_ref *ref;
792 ob = lto_create_simple_output_block (LTO_section_refs);
794 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
796 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
798 /* IPA_REF_ALIAS and IPA_REF_CHKP references are always preserved
799 in the boundary. Alias node can't have other references and
800 can be always handled as if it's not in the boundary. */
801 if (!node->alias && !lto_symtab_encoder_in_partition_p (encoder, node))
803 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
804 /* Output IPA_REF_CHKP reference. */
805 if (cnode
806 && cnode->instrumented_version
807 && !cnode->instrumentation_clone)
809 for (int i = 0; node->iterate_reference (i, ref); i++)
810 if (ref->use == IPA_REF_CHKP)
812 if (lto_symtab_encoder_lookup (encoder, ref->referred)
813 != LCC_NOT_FOUND)
815 int nref = lto_symtab_encoder_lookup (encoder, node);
816 streamer_write_gcov_count_stream (ob->main_stream, 1);
817 streamer_write_uhwi_stream (ob->main_stream, nref);
818 lto_output_ref (ob, ref, encoder);
820 break;
823 continue;
826 count = node->ref_list.nreferences ();
827 if (count)
829 streamer_write_gcov_count_stream (ob->main_stream, count);
830 streamer_write_uhwi_stream (ob->main_stream,
831 lto_symtab_encoder_lookup (encoder, node));
832 for (int i = 0; node->iterate_reference (i, ref); i++)
833 lto_output_ref (ob, ref, encoder);
837 streamer_write_uhwi_stream (ob->main_stream, 0);
839 lto_destroy_simple_output_block (ob);
842 /* Add NODE into encoder as well as nodes it is cloned from.
843 Do it in a way so clones appear first. */
845 static void
846 add_node_to (lto_symtab_encoder_t encoder, struct cgraph_node *node,
847 bool include_body)
849 if (node->clone_of)
850 add_node_to (encoder, node->clone_of, include_body);
851 else if (include_body)
852 lto_set_symtab_encoder_encode_body (encoder, node);
853 lto_symtab_encoder_encode (encoder, node);
856 /* Add all references in NODE to encoders. */
858 static void
859 create_references (lto_symtab_encoder_t encoder, symtab_node *node)
861 int i;
862 struct ipa_ref *ref = NULL;
863 for (i = 0; node->iterate_reference (i, ref); i++)
864 if (is_a <cgraph_node *> (ref->referred))
865 add_node_to (encoder, dyn_cast <cgraph_node *> (ref->referred), false);
866 else
867 lto_symtab_encoder_encode (encoder, ref->referred);
870 /* Select what needs to be streamed out. In regular lto mode stream everything.
871 In offload lto mode stream only nodes marked as offloadable. */
872 void
873 select_what_to_stream (void)
875 struct symtab_node *snode;
876 FOR_EACH_SYMBOL (snode)
877 snode->need_lto_streaming = !lto_stream_offload_p || snode->offloadable;
880 /* Find all symbols we want to stream into given partition and insert them
881 to encoders.
883 The function actually replaces IN_ENCODER by new one. The reason is that
884 streaming code needs clone's origin to be streamed before clone. This
885 means that we need to insert the nodes in specific order. This order is
886 ignored by the partitioning logic earlier. */
888 lto_symtab_encoder_t
889 compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
891 struct cgraph_edge *edge;
892 int i;
893 lto_symtab_encoder_t encoder;
894 lto_symtab_encoder_iterator lsei;
895 hash_set<void *> reachable_call_targets;
897 encoder = lto_symtab_encoder_new (false);
899 /* Go over all entries in the IN_ENCODER and duplicate them to
900 ENCODER. At the same time insert masters of clones so
901 every master appears before clone. */
902 for (lsei = lsei_start_function_in_partition (in_encoder);
903 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
905 struct cgraph_node *node = lsei_cgraph_node (lsei);
906 if (!node->need_lto_streaming)
907 continue;
908 add_node_to (encoder, node, true);
909 lto_set_symtab_encoder_in_partition (encoder, node);
910 create_references (encoder, node);
911 /* For proper debug info, we need to ship the origins, too. */
912 if (DECL_ABSTRACT_ORIGIN (node->decl))
914 struct cgraph_node *origin_node
915 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
916 origin_node->used_as_abstract_origin = true;
917 add_node_to (encoder, origin_node, true);
920 for (lsei = lsei_start_variable_in_partition (in_encoder);
921 !lsei_end_p (lsei); lsei_next_variable_in_partition (&lsei))
923 varpool_node *vnode = lsei_varpool_node (lsei);
925 if (!vnode->need_lto_streaming)
926 continue;
927 lto_set_symtab_encoder_in_partition (encoder, vnode);
928 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
929 create_references (encoder, vnode);
930 /* For proper debug info, we need to ship the origins, too. */
931 if (DECL_ABSTRACT_ORIGIN (vnode->decl))
933 varpool_node *origin_node
934 = varpool_node::get (DECL_ABSTRACT_ORIGIN (vnode->decl));
935 lto_set_symtab_encoder_in_partition (encoder, origin_node);
938 /* Pickle in also the initializer of all referenced readonly variables
939 to help folding. Constant pool variables are not shared, so we must
940 pickle those too. */
941 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
943 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
944 if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
946 if (!lto_symtab_encoder_encode_initializer_p (encoder,
947 vnode)
948 && (((vnode->ctor_useable_for_folding_p ()
949 && (!DECL_VIRTUAL_P (vnode->decl)
950 || !flag_wpa
951 || flag_ltrans_devirtualize))
952 || POINTER_BOUNDS_P (vnode->decl))))
954 lto_set_symtab_encoder_encode_initializer (encoder, vnode);
955 create_references (encoder, vnode);
960 /* Go over all the nodes again to include callees that are not in
961 SET. */
962 for (lsei = lsei_start_function_in_partition (encoder);
963 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
965 struct cgraph_node *node = lsei_cgraph_node (lsei);
966 for (edge = node->callees; edge; edge = edge->next_callee)
968 struct cgraph_node *callee = edge->callee;
969 if (!lto_symtab_encoder_in_partition_p (encoder, callee))
971 /* We should have moved all the inlines. */
972 gcc_assert (!callee->global.inlined_to);
973 add_node_to (encoder, callee, false);
976 /* Add all possible targets for late devirtualization. */
977 if (flag_ltrans_devirtualize || !flag_wpa)
978 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
979 if (edge->indirect_info->polymorphic)
981 unsigned int i;
982 void *cache_token;
983 bool final;
984 vec <cgraph_node *>targets
985 = possible_polymorphic_call_targets
986 (edge, &final, &cache_token);
987 if (!reachable_call_targets.add (cache_token))
989 for (i = 0; i < targets.length (); i++)
991 struct cgraph_node *callee = targets[i];
993 /* Adding an external declarations into the unit serves
994 no purpose and just increases its boundary. */
995 if (callee->definition
996 && !lto_symtab_encoder_in_partition_p
997 (encoder, callee))
999 gcc_assert (!callee->global.inlined_to);
1000 add_node_to (encoder, callee, false);
1006 /* Be sure to also insert alias targert and thunk callees. These needs
1007 to stay to aid local calling conventions. */
1008 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
1010 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1011 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1013 if (node->alias && node->analyzed)
1014 create_references (encoder, node);
1015 if (cnode
1016 && cnode->thunk.thunk_p)
1017 add_node_to (encoder, cnode->callees->callee, false);
1019 lto_symtab_encoder_delete (in_encoder);
1020 return encoder;
1023 /* Output the part of the symtab in SET and VSET. */
1025 void
1026 output_symtab (void)
1028 struct cgraph_node *node;
1029 struct lto_simple_output_block *ob;
1030 int i, n_nodes;
1031 lto_symtab_encoder_t encoder;
1033 if (flag_wpa)
1034 output_cgraph_opt_summary ();
1036 ob = lto_create_simple_output_block (LTO_section_symtab_nodes);
1038 output_profile_summary (ob);
1040 /* An encoder for cgraph nodes should have been created by
1041 ipa_write_summaries_1. */
1042 gcc_assert (ob->decl_state->symtab_node_encoder);
1043 encoder = ob->decl_state->symtab_node_encoder;
1045 /* Write out the nodes. We must first output a node and then its clones,
1046 otherwise at a time reading back the node there would be nothing to clone
1047 from. */
1048 n_nodes = lto_symtab_encoder_size (encoder);
1049 for (i = 0; i < n_nodes; i++)
1051 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
1052 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1053 lto_output_node (ob, cnode, encoder);
1054 else
1055 lto_output_varpool_node (ob, dyn_cast<varpool_node *> (node), encoder);
1058 /* Go over the nodes in SET again to write edges. */
1059 for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
1061 node = dyn_cast <cgraph_node *> (lto_symtab_encoder_deref (encoder, i));
1062 if (node
1063 && (node->thunk.thunk_p
1064 || lto_symtab_encoder_in_partition_p (encoder, node)))
1066 output_outgoing_cgraph_edges (node->callees, ob, encoder);
1067 output_outgoing_cgraph_edges (node->indirect_calls, ob, encoder);
1071 streamer_write_uhwi_stream (ob->main_stream, 0);
1073 lto_destroy_simple_output_block (ob);
1075 /* Emit toplevel asms.
1076 When doing WPA we must output every asm just once. Since we do not partition asm
1077 nodes at all, output them to first output. This is kind of hack, but should work
1078 well. */
1079 if (!asm_nodes_output)
1081 asm_nodes_output = true;
1082 lto_output_toplevel_asms ();
1085 output_refs (encoder);
1088 /* Return identifier encoded in IB as a plain string. */
1090 static tree
1091 read_identifier (struct lto_input_block *ib)
1093 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1094 tree id;
1096 if (ib->data[ib->p + len])
1097 lto_section_overrun (ib);
1098 if (!len)
1100 ib->p++;
1101 return NULL;
1103 id = get_identifier (ib->data + ib->p);
1104 ib->p += len + 1;
1105 return id;
1108 /* Return string encoded in IB, NULL if string is empty. */
1110 static const char *
1111 read_string (struct lto_input_block *ib)
1113 unsigned int len = strnlen (ib->data + ib->p, ib->len - ib->p - 1);
1114 const char *str;
1116 if (ib->data[ib->p + len])
1117 lto_section_overrun (ib);
1118 if (!len)
1120 ib->p++;
1121 return NULL;
1123 str = ib->data + ib->p;
1124 ib->p += len + 1;
1125 return str;
1128 /* Output function/variable tables that will allow libgomp to look up offload
1129 target code.
1130 OFFLOAD_FUNCS is filled in expand_omp_target, OFFLOAD_VARS is filled in
1131 varpool_node::get_create. In WHOPR (partitioned) mode during the WPA stage
1132 both OFFLOAD_FUNCS and OFFLOAD_VARS are filled by input_offload_tables. */
1134 void
1135 output_offload_tables (void)
1137 if (vec_safe_is_empty (offload_funcs) && vec_safe_is_empty (offload_vars))
1138 return;
1140 struct lto_simple_output_block *ob
1141 = lto_create_simple_output_block (LTO_section_offload_table);
1143 for (unsigned i = 0; i < vec_safe_length (offload_funcs); i++)
1145 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1146 LTO_symtab_last_tag, LTO_symtab_unavail_node);
1147 lto_output_fn_decl_index (ob->decl_state, ob->main_stream,
1148 (*offload_funcs)[i]);
1151 for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
1153 streamer_write_enum (ob->main_stream, LTO_symtab_tags,
1154 LTO_symtab_last_tag, LTO_symtab_variable);
1155 lto_output_var_decl_index (ob->decl_state, ob->main_stream,
1156 (*offload_vars)[i]);
1159 streamer_write_uhwi_stream (ob->main_stream, 0);
1160 lto_destroy_simple_output_block (ob);
1162 /* In WHOPR mode during the WPA stage the joint offload tables need to be
1163 streamed to one partition only. That's why we free offload_funcs and
1164 offload_vars after the first call of output_offload_tables. */
1165 if (flag_wpa)
1167 vec_free (offload_funcs);
1168 vec_free (offload_vars);
1172 /* Overwrite the information in NODE based on FILE_DATA, TAG, FLAGS,
1173 STACK_SIZE, SELF_TIME and SELF_SIZE. This is called either to initialize
1174 NODE or to replace the values in it, for instance because the first
1175 time we saw it, the function body was not available but now it
1176 is. BP is a bitpack with all the bitflags for NODE read from the
1177 stream. */
1179 static void
1180 input_overwrite_node (struct lto_file_decl_data *file_data,
1181 struct cgraph_node *node,
1182 enum LTO_symtab_tags tag,
1183 struct bitpack_d *bp)
1185 node->aux = (void *) tag;
1186 node->lto_file_data = file_data;
1188 node->local.local = bp_unpack_value (bp, 1);
1189 node->externally_visible = bp_unpack_value (bp, 1);
1190 node->no_reorder = bp_unpack_value (bp, 1);
1191 node->definition = bp_unpack_value (bp, 1);
1192 node->local.versionable = bp_unpack_value (bp, 1);
1193 node->local.can_change_signature = bp_unpack_value (bp, 1);
1194 node->local.redefined_extern_inline = bp_unpack_value (bp, 1);
1195 node->force_output = bp_unpack_value (bp, 1);
1196 node->forced_by_abi = bp_unpack_value (bp, 1);
1197 node->unique_name = bp_unpack_value (bp, 1);
1198 node->body_removed = bp_unpack_value (bp, 1);
1199 node->implicit_section = bp_unpack_value (bp, 1);
1200 node->address_taken = bp_unpack_value (bp, 1);
1201 node->used_from_other_partition = bp_unpack_value (bp, 1);
1202 node->lowered = bp_unpack_value (bp, 1);
1203 node->analyzed = tag == LTO_symtab_analyzed_node;
1204 node->in_other_partition = bp_unpack_value (bp, 1);
1205 if (node->in_other_partition
1206 /* Avoid updating decl when we are seeing just inline clone.
1207 When inlining function that has functions already inlined into it,
1208 we produce clones of inline clones.
1210 WPA partitioning might put each clone into different unit and
1211 we might end up streaming inline clone from other partition
1212 to support clone we are interested in. */
1213 && (!node->clone_of
1214 || node->clone_of->decl != node->decl))
1216 DECL_EXTERNAL (node->decl) = 1;
1217 TREE_STATIC (node->decl) = 0;
1219 node->alias = bp_unpack_value (bp, 1);
1220 node->weakref = bp_unpack_value (bp, 1);
1221 node->frequency = (enum node_frequency)bp_unpack_value (bp, 2);
1222 node->only_called_at_startup = bp_unpack_value (bp, 1);
1223 node->only_called_at_exit = bp_unpack_value (bp, 1);
1224 node->tm_clone = bp_unpack_value (bp, 1);
1225 node->calls_comdat_local = bp_unpack_value (bp, 1);
1226 node->icf_merged = bp_unpack_value (bp, 1);
1227 node->nonfreeing_fn = bp_unpack_value (bp, 1);
1228 node->thunk.thunk_p = bp_unpack_value (bp, 1);
1229 node->parallelized_function = bp_unpack_value (bp, 1);
1230 node->resolution = bp_unpack_enum (bp, ld_plugin_symbol_resolution,
1231 LDPR_NUM_KNOWN);
1232 node->instrumentation_clone = bp_unpack_value (bp, 1);
1233 node->split_part = bp_unpack_value (bp, 1);
1234 gcc_assert (flag_ltrans
1235 || (!node->in_other_partition
1236 && !node->used_from_other_partition));
1239 /* Return string alias is alias of. */
1241 static tree
1242 get_alias_symbol (tree decl)
1244 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1245 return get_identifier (TREE_STRING_POINTER
1246 (TREE_VALUE (TREE_VALUE (alias))));
1249 /* Read a node from input_block IB. TAG is the node's tag just read.
1250 Return the node read or overwriten. */
1252 static struct cgraph_node *
1253 input_node (struct lto_file_decl_data *file_data,
1254 struct lto_input_block *ib,
1255 enum LTO_symtab_tags tag,
1256 vec<symtab_node *> nodes)
1258 gcc::pass_manager *passes = g->get_passes ();
1259 tree fn_decl;
1260 struct cgraph_node *node;
1261 struct bitpack_d bp;
1262 unsigned decl_index;
1263 int ref = LCC_NOT_FOUND, ref2 = LCC_NOT_FOUND;
1264 int clone_ref;
1265 int order;
1266 int i, count;
1267 tree group;
1268 const char *section;
1269 order = streamer_read_hwi (ib) + order_base;
1270 clone_ref = streamer_read_hwi (ib);
1272 decl_index = streamer_read_uhwi (ib);
1273 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1275 if (clone_ref != LCC_NOT_FOUND)
1277 node = dyn_cast<cgraph_node *> (nodes[clone_ref])->create_clone (fn_decl,
1278 0, CGRAPH_FREQ_BASE, false,
1279 vNULL, false, NULL, NULL);
1281 else
1283 /* Declaration of functions can be already merged with a declaration
1284 from other input file. We keep cgraph unmerged until after streaming
1285 of ipa passes is done. Alays forcingly create a fresh node. */
1286 node = symtab->create_empty ();
1287 node->decl = fn_decl;
1288 node->register_symbol ();
1291 node->order = order;
1292 if (order >= symtab->order)
1293 symtab->order = order + 1;
1295 node->count = streamer_read_gcov_count (ib);
1296 node->count_materialization_scale = streamer_read_hwi (ib);
1298 count = streamer_read_hwi (ib);
1299 node->ipa_transforms_to_apply = vNULL;
1300 for (i = 0; i < count; i++)
1302 opt_pass *pass;
1303 int pid = streamer_read_hwi (ib);
1305 gcc_assert (pid < passes->passes_by_id_size);
1306 pass = passes->passes_by_id[pid];
1307 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *) pass);
1310 if (tag == LTO_symtab_analyzed_node)
1311 ref = streamer_read_hwi (ib);
1313 group = read_identifier (ib);
1314 if (group)
1315 ref2 = streamer_read_hwi (ib);
1317 /* Make sure that we have not read this node before. Nodes that
1318 have already been read will have their tag stored in the 'aux'
1319 field. Since built-in functions can be referenced in multiple
1320 functions, they are expected to be read more than once. */
1321 if (node->aux && !DECL_BUILT_IN (node->decl))
1322 internal_error ("bytecode stream: found multiple instances of cgraph "
1323 "node with uid %d", node->uid);
1325 node->tp_first_run = streamer_read_uhwi (ib);
1327 bp = streamer_read_bitpack (ib);
1329 input_overwrite_node (file_data, node, tag, &bp);
1331 /* Store a reference for now, and fix up later to be a pointer. */
1332 node->global.inlined_to = (cgraph_node *) (intptr_t) ref;
1334 if (group)
1336 node->set_comdat_group (group);
1337 /* Store a reference for now, and fix up later to be a pointer. */
1338 node->same_comdat_group = (symtab_node *) (intptr_t) ref2;
1340 else
1341 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1342 section = read_string (ib);
1343 if (section)
1344 node->set_section_for_node (section);
1346 if (node->thunk.thunk_p)
1348 int type = streamer_read_uhwi (ib);
1349 HOST_WIDE_INT fixed_offset = streamer_read_uhwi (ib);
1350 HOST_WIDE_INT virtual_value = streamer_read_uhwi (ib);
1352 node->thunk.fixed_offset = fixed_offset;
1353 node->thunk.this_adjusting = (type & 2);
1354 node->thunk.virtual_value = virtual_value;
1355 node->thunk.virtual_offset_p = (type & 4);
1356 node->thunk.add_pointer_bounds_args = (type & 8);
1358 if (node->alias && !node->analyzed && node->weakref)
1359 node->alias_target = get_alias_symbol (node->decl);
1360 node->profile_id = streamer_read_hwi (ib);
1361 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1362 node->set_init_priority (streamer_read_hwi (ib));
1363 if (DECL_STATIC_DESTRUCTOR (node->decl))
1364 node->set_fini_priority (streamer_read_hwi (ib));
1366 if (node->instrumentation_clone)
1368 decl_index = streamer_read_uhwi (ib);
1369 fn_decl = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1370 node->orig_decl = fn_decl;
1373 return node;
1376 /* Read a node from input_block IB. TAG is the node's tag just read.
1377 Return the node read or overwriten. */
1379 static varpool_node *
1380 input_varpool_node (struct lto_file_decl_data *file_data,
1381 struct lto_input_block *ib)
1383 int decl_index;
1384 tree var_decl;
1385 varpool_node *node;
1386 struct bitpack_d bp;
1387 int ref = LCC_NOT_FOUND;
1388 int order;
1389 tree group;
1390 const char *section;
1392 order = streamer_read_hwi (ib) + order_base;
1393 decl_index = streamer_read_uhwi (ib);
1394 var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index);
1396 /* Declaration of functions can be already merged with a declaration
1397 from other input file. We keep cgraph unmerged until after streaming
1398 of ipa passes is done. Alays forcingly create a fresh node. */
1399 node = varpool_node::create_empty ();
1400 node->decl = var_decl;
1401 node->register_symbol ();
1403 node->order = order;
1404 if (order >= symtab->order)
1405 symtab->order = order + 1;
1406 node->lto_file_data = file_data;
1408 bp = streamer_read_bitpack (ib);
1409 node->externally_visible = bp_unpack_value (&bp, 1);
1410 node->no_reorder = bp_unpack_value (&bp, 1);
1411 node->force_output = bp_unpack_value (&bp, 1);
1412 node->forced_by_abi = bp_unpack_value (&bp, 1);
1413 node->unique_name = bp_unpack_value (&bp, 1);
1414 node->body_removed = bp_unpack_value (&bp, 1);
1415 node->implicit_section = bp_unpack_value (&bp, 1);
1416 node->writeonly = bp_unpack_value (&bp, 1);
1417 node->definition = bp_unpack_value (&bp, 1);
1418 node->alias = bp_unpack_value (&bp, 1);
1419 node->weakref = bp_unpack_value (&bp, 1);
1420 node->analyzed = bp_unpack_value (&bp, 1);
1421 node->used_from_other_partition = bp_unpack_value (&bp, 1);
1422 node->in_other_partition = bp_unpack_value (&bp, 1);
1423 if (node->in_other_partition)
1425 DECL_EXTERNAL (node->decl) = 1;
1426 TREE_STATIC (node->decl) = 0;
1428 if (node->alias && !node->analyzed && node->weakref)
1429 node->alias_target = get_alias_symbol (node->decl);
1430 node->tls_model = (enum tls_model)bp_unpack_value (&bp, 3);
1431 node->used_by_single_function = (enum tls_model)bp_unpack_value (&bp, 1);
1432 node->need_bounds_init = bp_unpack_value (&bp, 1);
1433 group = read_identifier (ib);
1434 if (group)
1436 node->set_comdat_group (group);
1437 ref = streamer_read_hwi (ib);
1438 /* Store a reference for now, and fix up later to be a pointer. */
1439 node->same_comdat_group = (symtab_node *) (intptr_t) ref;
1441 else
1442 node->same_comdat_group = (symtab_node *) (intptr_t) LCC_NOT_FOUND;
1443 section = read_string (ib);
1444 if (section)
1445 node->set_section_for_node (section);
1446 node->resolution = streamer_read_enum (ib, ld_plugin_symbol_resolution,
1447 LDPR_NUM_KNOWN);
1448 gcc_assert (flag_ltrans
1449 || (!node->in_other_partition
1450 && !node->used_from_other_partition));
1452 return node;
1455 /* Read a node from input_block IB. TAG is the node's tag just read.
1456 Return the node read or overwriten. */
1458 static void
1459 input_ref (struct lto_input_block *ib,
1460 symtab_node *referring_node,
1461 vec<symtab_node *> nodes)
1463 symtab_node *node = NULL;
1464 struct bitpack_d bp;
1465 enum ipa_ref_use use;
1466 bool speculative;
1467 struct ipa_ref *ref;
1469 bp = streamer_read_bitpack (ib);
1470 use = (enum ipa_ref_use) bp_unpack_value (&bp, 3);
1471 speculative = (enum ipa_ref_use) bp_unpack_value (&bp, 1);
1472 node = nodes[streamer_read_hwi (ib)];
1473 ref = referring_node->create_reference (node, use);
1474 ref->speculative = speculative;
1475 if (is_a <cgraph_node *> (referring_node))
1476 ref->lto_stmt_uid = streamer_read_hwi (ib);
1479 /* Read an edge from IB. NODES points to a vector of previously read nodes for
1480 decoding caller and callee of the edge to be read. If INDIRECT is true, the
1481 edge being read is indirect (in the sense that it has
1482 indirect_unknown_callee set). */
1484 static void
1485 input_edge (struct lto_input_block *ib, vec<symtab_node *> nodes,
1486 bool indirect)
1488 struct cgraph_node *caller, *callee;
1489 struct cgraph_edge *edge;
1490 unsigned int stmt_id;
1491 gcov_type count;
1492 int freq;
1493 cgraph_inline_failed_t inline_failed;
1494 struct bitpack_d bp;
1495 int ecf_flags = 0;
1497 caller = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1498 if (caller == NULL || caller->decl == NULL_TREE)
1499 internal_error ("bytecode stream: no caller found while reading edge");
1501 if (!indirect)
1503 callee = dyn_cast<cgraph_node *> (nodes[streamer_read_hwi (ib)]);
1504 if (callee == NULL || callee->decl == NULL_TREE)
1505 internal_error ("bytecode stream: no callee found while reading edge");
1507 else
1508 callee = NULL;
1510 count = streamer_read_gcov_count (ib);
1512 bp = streamer_read_bitpack (ib);
1513 inline_failed = bp_unpack_enum (&bp, cgraph_inline_failed_t, CIF_N_REASONS);
1514 stmt_id = bp_unpack_var_len_unsigned (&bp);
1515 freq = (int) bp_unpack_var_len_unsigned (&bp);
1517 if (indirect)
1518 edge = caller->create_indirect_edge (NULL, 0, count, freq);
1519 else
1520 edge = caller->create_edge (callee, NULL, count, freq);
1522 edge->indirect_inlining_edge = bp_unpack_value (&bp, 1);
1523 edge->speculative = bp_unpack_value (&bp, 1);
1524 edge->lto_stmt_uid = stmt_id;
1525 edge->inline_failed = inline_failed;
1526 edge->call_stmt_cannot_inline_p = bp_unpack_value (&bp, 1);
1527 edge->can_throw_external = bp_unpack_value (&bp, 1);
1528 edge->in_polymorphic_cdtor = bp_unpack_value (&bp, 1);
1529 if (indirect)
1531 if (bp_unpack_value (&bp, 1))
1532 ecf_flags |= ECF_CONST;
1533 if (bp_unpack_value (&bp, 1))
1534 ecf_flags |= ECF_PURE;
1535 if (bp_unpack_value (&bp, 1))
1536 ecf_flags |= ECF_NORETURN;
1537 if (bp_unpack_value (&bp, 1))
1538 ecf_flags |= ECF_MALLOC;
1539 if (bp_unpack_value (&bp, 1))
1540 ecf_flags |= ECF_NOTHROW;
1541 if (bp_unpack_value (&bp, 1))
1542 ecf_flags |= ECF_RETURNS_TWICE;
1543 edge->indirect_info->ecf_flags = ecf_flags;
1544 edge->indirect_info->common_target_id = streamer_read_hwi (ib);
1545 if (edge->indirect_info->common_target_id)
1546 edge->indirect_info->common_target_probability = streamer_read_hwi (ib);
1551 /* Read a cgraph from IB using the info in FILE_DATA. */
1553 static vec<symtab_node *>
1554 input_cgraph_1 (struct lto_file_decl_data *file_data,
1555 struct lto_input_block *ib)
1557 enum LTO_symtab_tags tag;
1558 vec<symtab_node *> nodes = vNULL;
1559 symtab_node *node;
1560 unsigned i;
1562 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1563 order_base = symtab->order;
1564 while (tag)
1566 if (tag == LTO_symtab_edge)
1567 input_edge (ib, nodes, false);
1568 else if (tag == LTO_symtab_indirect_edge)
1569 input_edge (ib, nodes, true);
1570 else if (tag == LTO_symtab_variable)
1572 node = input_varpool_node (file_data, ib);
1573 nodes.safe_push (node);
1574 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1576 else
1578 node = input_node (file_data, ib, tag, nodes);
1579 if (node == NULL || node->decl == NULL_TREE)
1580 internal_error ("bytecode stream: found empty cgraph node");
1581 nodes.safe_push (node);
1582 lto_symtab_encoder_encode (file_data->symtab_node_encoder, node);
1585 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1588 lto_input_toplevel_asms (file_data, order_base);
1590 /* AUX pointers should be all non-zero for function nodes read from the stream. */
1591 #ifdef ENABLE_CHECKING
1592 FOR_EACH_VEC_ELT (nodes, i, node)
1593 gcc_assert (node->aux || !is_a <cgraph_node *> (node));
1594 #endif
1595 FOR_EACH_VEC_ELT (nodes, i, node)
1597 int ref;
1598 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1600 ref = (int) (intptr_t) cnode->global.inlined_to;
1602 /* We share declaration of builtins, so we may read same node twice. */
1603 if (!node->aux)
1604 continue;
1605 node->aux = NULL;
1607 /* Fixup inlined_to from reference to pointer. */
1608 if (ref != LCC_NOT_FOUND)
1609 dyn_cast<cgraph_node *> (node)->global.inlined_to
1610 = dyn_cast<cgraph_node *> (nodes[ref]);
1611 else
1612 cnode->global.inlined_to = NULL;
1614 /* Compute instrumented_version. */
1615 if (cnode->instrumentation_clone)
1617 gcc_assert (cnode->orig_decl);
1619 cnode->instrumented_version = cgraph_node::get (cnode->orig_decl);
1620 if (cnode->instrumented_version)
1622 /* We may have multiple nodes for a single function which
1623 will be merged later. To have a proper merge we need
1624 to keep instrumentation_version reference between nodes
1625 consistent: each instrumented_version reference should
1626 have proper reverse reference. Thus don't break existing
1627 instrumented_version reference if it already exists. */
1628 if (cnode->instrumented_version->instrumented_version)
1629 cnode->instrumented_version = NULL;
1630 else
1631 cnode->instrumented_version->instrumented_version = cnode;
1634 /* Restore decl names reference except for wrapper functions. */
1635 if (!chkp_wrap_function (cnode->orig_decl))
1637 tree name = DECL_ASSEMBLER_NAME (cnode->decl);
1638 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
1639 TREE_CHAIN (name) = DECL_ASSEMBLER_NAME (cnode->orig_decl);
1644 ref = (int) (intptr_t) node->same_comdat_group;
1646 /* Fixup same_comdat_group from reference to pointer. */
1647 if (ref != LCC_NOT_FOUND)
1648 node->same_comdat_group = nodes[ref];
1649 else
1650 node->same_comdat_group = NULL;
1652 FOR_EACH_VEC_ELT (nodes, i, node)
1653 node->aux = is_a <cgraph_node *> (node) ? (void *)1 : NULL;
1654 return nodes;
1657 /* Input ipa_refs. */
1659 static void
1660 input_refs (struct lto_input_block *ib,
1661 vec<symtab_node *> nodes)
1663 int count;
1664 int idx;
1665 while (true)
1667 symtab_node *node;
1668 count = streamer_read_uhwi (ib);
1669 if (!count)
1670 break;
1671 idx = streamer_read_uhwi (ib);
1672 node = nodes[idx];
1673 while (count)
1675 input_ref (ib, node, nodes);
1676 count--;
1682 static struct gcov_ctr_summary lto_gcov_summary;
1684 /* Input profile_info from IB. */
1685 static void
1686 input_profile_summary (struct lto_input_block *ib,
1687 struct lto_file_decl_data *file_data)
1689 unsigned h_ix;
1690 struct bitpack_d bp;
1691 unsigned int runs = streamer_read_uhwi (ib);
1692 if (runs)
1694 file_data->profile_info.runs = runs;
1695 file_data->profile_info.sum_max = streamer_read_gcov_count (ib);
1696 file_data->profile_info.sum_all = streamer_read_gcov_count (ib);
1698 memset (file_data->profile_info.histogram, 0,
1699 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1700 /* Input the bitpack of non-zero histogram indices. */
1701 bp = streamer_read_bitpack (ib);
1702 /* Read in and unpack the full bitpack, flagging non-zero
1703 histogram entries by setting the num_counters non-zero. */
1704 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1706 file_data->profile_info.histogram[h_ix].num_counters
1707 = bp_unpack_value (&bp, 1);
1709 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1711 if (!file_data->profile_info.histogram[h_ix].num_counters)
1712 continue;
1714 file_data->profile_info.histogram[h_ix].num_counters
1715 = streamer_read_gcov_count (ib);
1716 file_data->profile_info.histogram[h_ix].min_value
1717 = streamer_read_gcov_count (ib);
1718 file_data->profile_info.histogram[h_ix].cum_value
1719 = streamer_read_gcov_count (ib);
1721 /* IPA-profile computes hot bb threshold based on cumulated
1722 whole program profile. We need to stream it down to ltrans. */
1723 if (flag_ltrans)
1724 set_hot_bb_threshold (streamer_read_gcov_count (ib));
1729 /* Rescale profile summaries to the same number of runs in the whole unit. */
1731 static void
1732 merge_profile_summaries (struct lto_file_decl_data **file_data_vec)
1734 struct lto_file_decl_data *file_data;
1735 unsigned int j, h_ix;
1736 gcov_unsigned_t max_runs = 0;
1737 struct cgraph_node *node;
1738 struct cgraph_edge *edge;
1739 gcov_type saved_sum_all = 0;
1740 gcov_ctr_summary *saved_profile_info = 0;
1741 int saved_scale = 0;
1743 /* Find unit with maximal number of runs. If we ever get serious about
1744 roundoff errors, we might also consider computing smallest common
1745 multiply. */
1746 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1747 if (max_runs < file_data->profile_info.runs)
1748 max_runs = file_data->profile_info.runs;
1750 if (!max_runs)
1751 return;
1753 /* Simple overflow check. We probably don't need to support that many train
1754 runs. Such a large value probably imply data corruption anyway. */
1755 if (max_runs > INT_MAX / REG_BR_PROB_BASE)
1757 sorry ("At most %i profile runs is supported. Perhaps corrupted profile?",
1758 INT_MAX / REG_BR_PROB_BASE);
1759 return;
1762 profile_info = &lto_gcov_summary;
1763 lto_gcov_summary.runs = max_runs;
1764 lto_gcov_summary.sum_max = 0;
1765 memset (lto_gcov_summary.histogram, 0,
1766 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
1768 /* Rescale all units to the maximal number of runs.
1769 sum_max can not be easily merged, as we have no idea what files come from
1770 the same run. We do not use the info anyway, so leave it 0. */
1771 for (j = 0; (file_data = file_data_vec[j]) != NULL; j++)
1772 if (file_data->profile_info.runs)
1774 int scale = GCOV_COMPUTE_SCALE (max_runs,
1775 file_data->profile_info.runs);
1776 lto_gcov_summary.sum_max
1777 = MAX (lto_gcov_summary.sum_max,
1778 apply_scale (file_data->profile_info.sum_max, scale));
1779 lto_gcov_summary.sum_all
1780 = MAX (lto_gcov_summary.sum_all,
1781 apply_scale (file_data->profile_info.sum_all, scale));
1782 /* Save a pointer to the profile_info with the largest
1783 scaled sum_all and the scale for use in merging the
1784 histogram. */
1785 if (!saved_profile_info
1786 || lto_gcov_summary.sum_all > saved_sum_all)
1788 saved_profile_info = &file_data->profile_info;
1789 saved_sum_all = lto_gcov_summary.sum_all;
1790 saved_scale = scale;
1794 gcc_assert (saved_profile_info);
1796 /* Scale up the histogram from the profile that had the largest
1797 scaled sum_all above. */
1798 for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
1800 /* Scale up the min value as we did the corresponding sum_all
1801 above. Use that to find the new histogram index. */
1802 gcov_type scaled_min
1803 = apply_scale (saved_profile_info->histogram[h_ix].min_value,
1804 saved_scale);
1805 /* The new index may be shared with another scaled histogram entry,
1806 so we need to account for a non-zero histogram entry at new_ix. */
1807 unsigned new_ix = gcov_histo_index (scaled_min);
1808 lto_gcov_summary.histogram[new_ix].min_value
1809 = (lto_gcov_summary.histogram[new_ix].num_counters
1810 ? MIN (lto_gcov_summary.histogram[new_ix].min_value, scaled_min)
1811 : scaled_min);
1812 /* Some of the scaled counter values would ostensibly need to be placed
1813 into different (larger) histogram buckets, but we keep things simple
1814 here and place the scaled cumulative counter value in the bucket
1815 corresponding to the scaled minimum counter value. */
1816 lto_gcov_summary.histogram[new_ix].cum_value
1817 += apply_scale (saved_profile_info->histogram[h_ix].cum_value,
1818 saved_scale);
1819 lto_gcov_summary.histogram[new_ix].num_counters
1820 += saved_profile_info->histogram[h_ix].num_counters;
1823 /* Watch roundoff errors. */
1824 if (lto_gcov_summary.sum_max < max_runs)
1825 lto_gcov_summary.sum_max = max_runs;
1827 /* If merging already happent at WPA time, we are done. */
1828 if (flag_ltrans)
1829 return;
1831 /* Now compute count_materialization_scale of each node.
1832 During LTRANS we already have values of count_materialization_scale
1833 computed, so just update them. */
1834 FOR_EACH_FUNCTION (node)
1835 if (node->lto_file_data
1836 && node->lto_file_data->profile_info.runs)
1838 int scale;
1840 scale = RDIV (node->count_materialization_scale * max_runs,
1841 node->lto_file_data->profile_info.runs);
1842 node->count_materialization_scale = scale;
1843 if (scale < 0)
1844 fatal_error (input_location, "Profile information in %s corrupted",
1845 file_data->file_name);
1847 if (scale == REG_BR_PROB_BASE)
1848 continue;
1849 for (edge = node->callees; edge; edge = edge->next_callee)
1850 edge->count = apply_scale (edge->count, scale);
1851 node->count = apply_scale (node->count, scale);
1855 /* Input and merge the symtab from each of the .o files passed to
1856 lto1. */
1858 void
1859 input_symtab (void)
1861 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1862 struct lto_file_decl_data *file_data;
1863 unsigned int j = 0;
1864 struct cgraph_node *node;
1866 while ((file_data = file_data_vec[j++]))
1868 const char *data;
1869 size_t len;
1870 struct lto_input_block *ib;
1871 vec<symtab_node *> nodes;
1873 ib = lto_create_simple_input_block (file_data, LTO_section_symtab_nodes,
1874 &data, &len);
1875 if (!ib)
1876 fatal_error (input_location,
1877 "cannot find LTO cgraph in %s", file_data->file_name);
1878 input_profile_summary (ib, file_data);
1879 file_data->symtab_node_encoder = lto_symtab_encoder_new (true);
1880 nodes = input_cgraph_1 (file_data, ib);
1881 lto_destroy_simple_input_block (file_data, LTO_section_symtab_nodes,
1882 ib, data, len);
1884 ib = lto_create_simple_input_block (file_data, LTO_section_refs,
1885 &data, &len);
1886 if (!ib)
1887 fatal_error (input_location, "cannot find LTO section refs in %s",
1888 file_data->file_name);
1889 input_refs (ib, nodes);
1890 lto_destroy_simple_input_block (file_data, LTO_section_refs,
1891 ib, data, len);
1892 if (flag_ltrans)
1893 input_cgraph_opt_summary (nodes);
1894 nodes.release ();
1897 merge_profile_summaries (file_data_vec);
1898 get_working_sets ();
1901 /* Clear out the aux field that was used to store enough state to
1902 tell which nodes should be overwritten. */
1903 FOR_EACH_FUNCTION (node)
1905 /* Some nodes may have been created by cgraph_node. This
1906 happens when the callgraph contains nested functions. If the
1907 node for the parent function was never emitted to the gimple
1908 file, cgraph_node will create a node for it when setting the
1909 context of the nested function. */
1910 if (node->lto_file_data)
1911 node->aux = NULL;
1915 /* Input function/variable tables that will allow libgomp to look up offload
1916 target code, and store them into OFFLOAD_FUNCS and OFFLOAD_VARS. */
1918 void
1919 input_offload_tables (void)
1921 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1922 struct lto_file_decl_data *file_data;
1923 unsigned int j = 0;
1925 while ((file_data = file_data_vec[j++]))
1927 const char *data;
1928 size_t len;
1929 struct lto_input_block *ib
1930 = lto_create_simple_input_block (file_data, LTO_section_offload_table,
1931 &data, &len);
1932 if (!ib)
1933 continue;
1935 enum LTO_symtab_tags tag
1936 = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1937 while (tag)
1939 if (tag == LTO_symtab_unavail_node)
1941 int decl_index = streamer_read_uhwi (ib);
1942 tree fn_decl
1943 = lto_file_decl_data_get_fn_decl (file_data, decl_index);
1944 vec_safe_push (offload_funcs, fn_decl);
1946 else if (tag == LTO_symtab_variable)
1948 int decl_index = streamer_read_uhwi (ib);
1949 tree var_decl
1950 = lto_file_decl_data_get_var_decl (file_data, decl_index);
1951 vec_safe_push (offload_vars, var_decl);
1953 else
1954 fatal_error (input_location,
1955 "invalid offload table in %s", file_data->file_name);
1957 tag = streamer_read_enum (ib, LTO_symtab_tags, LTO_symtab_last_tag);
1960 lto_destroy_simple_input_block (file_data, LTO_section_offload_table,
1961 ib, data, len);
1965 /* True when we need optimization summary for NODE. */
1967 static int
1968 output_cgraph_opt_summary_p (struct cgraph_node *node)
1970 return (node->clone_of
1971 && (node->clone.tree_map
1972 || node->clone.args_to_skip
1973 || node->clone.combined_args_to_skip));
1976 /* Output optimization summary for EDGE to OB. */
1977 static void
1978 output_edge_opt_summary (struct output_block *ob ATTRIBUTE_UNUSED,
1979 struct cgraph_edge *edge ATTRIBUTE_UNUSED)
1983 /* Output optimization summary for NODE to OB. */
1985 static void
1986 output_node_opt_summary (struct output_block *ob,
1987 struct cgraph_node *node,
1988 lto_symtab_encoder_t encoder)
1990 unsigned int index;
1991 bitmap_iterator bi;
1992 struct ipa_replace_map *map;
1993 struct bitpack_d bp;
1994 int i;
1995 struct cgraph_edge *e;
1997 if (node->clone.args_to_skip)
1999 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.args_to_skip));
2000 EXECUTE_IF_SET_IN_BITMAP (node->clone.args_to_skip, 0, index, bi)
2001 streamer_write_uhwi (ob, index);
2003 else
2004 streamer_write_uhwi (ob, 0);
2005 if (node->clone.combined_args_to_skip)
2007 streamer_write_uhwi (ob, bitmap_count_bits (node->clone.combined_args_to_skip));
2008 EXECUTE_IF_SET_IN_BITMAP (node->clone.combined_args_to_skip, 0, index, bi)
2009 streamer_write_uhwi (ob, index);
2011 else
2012 streamer_write_uhwi (ob, 0);
2013 streamer_write_uhwi (ob, vec_safe_length (node->clone.tree_map));
2014 FOR_EACH_VEC_SAFE_ELT (node->clone.tree_map, i, map)
2016 /* At the moment we assume all old trees to be PARM_DECLs, because we have no
2017 mechanism to store function local declarations into summaries. */
2018 gcc_assert (!map->old_tree);
2019 streamer_write_uhwi (ob, map->parm_num);
2020 gcc_assert (EXPR_LOCATION (map->new_tree) == UNKNOWN_LOCATION);
2021 stream_write_tree (ob, map->new_tree, true);
2022 bp = bitpack_create (ob->main_stream);
2023 bp_pack_value (&bp, map->replace_p, 1);
2024 bp_pack_value (&bp, map->ref_p, 1);
2025 streamer_write_bitpack (&bp);
2028 if (lto_symtab_encoder_in_partition_p (encoder, node))
2030 for (e = node->callees; e; e = e->next_callee)
2031 output_edge_opt_summary (ob, e);
2032 for (e = node->indirect_calls; e; e = e->next_callee)
2033 output_edge_opt_summary (ob, e);
2037 /* Output optimization summaries stored in callgraph.
2038 At the moment it is the clone info structure. */
2040 static void
2041 output_cgraph_opt_summary (void)
2043 int i, n_nodes;
2044 lto_symtab_encoder_t encoder;
2045 struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum);
2046 unsigned count = 0;
2048 ob->symbol = NULL;
2049 encoder = ob->decl_state->symtab_node_encoder;
2050 n_nodes = lto_symtab_encoder_size (encoder);
2051 for (i = 0; i < n_nodes; i++)
2053 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2054 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2055 if (cnode && output_cgraph_opt_summary_p (cnode))
2056 count++;
2058 streamer_write_uhwi (ob, count);
2059 for (i = 0; i < n_nodes; i++)
2061 symtab_node *node = lto_symtab_encoder_deref (encoder, i);
2062 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2063 if (cnode && output_cgraph_opt_summary_p (cnode))
2065 streamer_write_uhwi (ob, i);
2066 output_node_opt_summary (ob, cnode, encoder);
2069 produce_asm (ob, NULL);
2070 destroy_output_block (ob);
2073 /* Input optimisation summary of EDGE. */
2075 static void
2076 input_edge_opt_summary (struct cgraph_edge *edge ATTRIBUTE_UNUSED,
2077 struct lto_input_block *ib_main ATTRIBUTE_UNUSED)
2081 /* Input optimisation summary of NODE. */
2083 static void
2084 input_node_opt_summary (struct cgraph_node *node,
2085 struct lto_input_block *ib_main,
2086 struct data_in *data_in)
2088 int i;
2089 int count;
2090 int bit;
2091 struct bitpack_d bp;
2092 struct cgraph_edge *e;
2094 count = streamer_read_uhwi (ib_main);
2095 if (count)
2096 node->clone.args_to_skip = BITMAP_GGC_ALLOC ();
2097 for (i = 0; i < count; i++)
2099 bit = streamer_read_uhwi (ib_main);
2100 bitmap_set_bit (node->clone.args_to_skip, bit);
2102 count = streamer_read_uhwi (ib_main);
2103 if (count)
2104 node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
2105 for (i = 0; i < count; i++)
2107 bit = streamer_read_uhwi (ib_main);
2108 bitmap_set_bit (node->clone.combined_args_to_skip, bit);
2110 count = streamer_read_uhwi (ib_main);
2111 for (i = 0; i < count; i++)
2113 struct ipa_replace_map *map = ggc_alloc<ipa_replace_map> ();
2115 vec_safe_push (node->clone.tree_map, map);
2116 map->parm_num = streamer_read_uhwi (ib_main);
2117 map->old_tree = NULL;
2118 map->new_tree = stream_read_tree (ib_main, data_in);
2119 bp = streamer_read_bitpack (ib_main);
2120 map->replace_p = bp_unpack_value (&bp, 1);
2121 map->ref_p = bp_unpack_value (&bp, 1);
2123 for (e = node->callees; e; e = e->next_callee)
2124 input_edge_opt_summary (e, ib_main);
2125 for (e = node->indirect_calls; e; e = e->next_callee)
2126 input_edge_opt_summary (e, ib_main);
2129 /* Read section in file FILE_DATA of length LEN with data DATA. */
2131 static void
2132 input_cgraph_opt_section (struct lto_file_decl_data *file_data,
2133 const char *data, size_t len,
2134 vec<symtab_node *> nodes)
2136 const struct lto_function_header *header =
2137 (const struct lto_function_header *) data;
2138 const int cfg_offset = sizeof (struct lto_function_header);
2139 const int main_offset = cfg_offset + header->cfg_size;
2140 const int string_offset = main_offset + header->main_size;
2141 struct data_in *data_in;
2142 unsigned int i;
2143 unsigned int count;
2145 lto_input_block ib_main ((const char *) data + main_offset,
2146 header->main_size, file_data->mode_table);
2148 data_in =
2149 lto_data_in_create (file_data, (const char *) data + string_offset,
2150 header->string_size, vNULL);
2151 count = streamer_read_uhwi (&ib_main);
2153 for (i = 0; i < count; i++)
2155 int ref = streamer_read_uhwi (&ib_main);
2156 input_node_opt_summary (dyn_cast<cgraph_node *> (nodes[ref]),
2157 &ib_main, data_in);
2159 lto_free_section_data (file_data, LTO_section_cgraph_opt_sum, NULL, data,
2160 len);
2161 lto_data_in_delete (data_in);
2164 /* Input optimization summary of cgraph. */
2166 static void
2167 input_cgraph_opt_summary (vec<symtab_node *> nodes)
2169 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2170 struct lto_file_decl_data *file_data;
2171 unsigned int j = 0;
2173 while ((file_data = file_data_vec[j++]))
2175 size_t len;
2176 const char *data =
2177 lto_get_section_data (file_data, LTO_section_cgraph_opt_sum, NULL,
2178 &len);
2180 if (data)
2181 input_cgraph_opt_section (file_data, data, len, nodes);